2 * Copyright(c) 2021 Samsung Electronics Co., Ltd.
4 * Licensed under the Apache License, Version 2.0 (the "License");
5 * you may not use this file except in compliance with the License.
6 * You may obtain a copy of the License at
8 * http://www.apache.org/licenses/LICENSE-2.0
10 * Unless required by applicable law or agreed to in writing, software
11 * distributed under the License is distributed on an "AS IS" BASIS,
12 * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
13 * See the License for the specific language governing permissions and
14 * limitations under the License.
19 using System.ComponentModel;
20 using System.Runtime.InteropServices;
22 namespace Tizen.NUI.BaseComponents
25 /// A control which provides a single line editable text field.
27 /// <since_tizen> 3 </since_tizen>
28 public partial class TextField
30 private EventHandler<TextChangedEventArgs> textFieldTextChangedEventHandler;
31 private TextChangedCallbackDelegate textFieldTextChangedCallbackDelegate;
32 private EventHandler textFieldCursorPositionChangedEventHandler;
33 private CursorPositionChangedCallbackDelegate textFieldCursorPositionChangedCallbackDelegate;
34 private EventHandler<MaxLengthReachedEventArgs> textFieldMaxLengthReachedEventHandler;
35 private MaxLengthReachedCallbackDelegate textFieldMaxLengthReachedCallbackDelegate;
36 private EventHandler<AnchorClickedEventArgs> textFieldAnchorClickedEventHandler;
37 private AnchorClickedCallbackDelegate textFieldAnchorClickedCallbackDelegate;
39 private EventHandler textFieldSelectionChangedEventHandler;
40 private SelectionChangedCallbackDelegate textFieldSelectionChangedCallbackDelegate;
42 private EventHandler<InputFilteredEventArgs> textFieldInputFilteredEventHandler;
43 private InputFilteredCallbackDelegate textFieldInputFilteredCallbackDelegate;
44 private EventHandler textFieldSelectionClearedEventHandler;
45 private SelectionClearedCallbackDelegate textFieldSelectionClearedCallbackDelegate;
47 private EventHandler textFieldSelectionStartedEventHandler;
48 private SelectionStartedCallbackDelegate textFieldSelectionStartedCallbackDelegate;
50 [UnmanagedFunctionPointer(CallingConvention.StdCall)]
51 private delegate void TextChangedCallbackDelegate(IntPtr textField);
53 [UnmanagedFunctionPointer(CallingConvention.StdCall)]
54 private delegate void CursorPositionChangedCallbackDelegate(IntPtr textField, uint oldPosition);
56 [UnmanagedFunctionPointer(CallingConvention.StdCall)]
57 private delegate void MaxLengthReachedCallbackDelegate(IntPtr textField);
59 [UnmanagedFunctionPointer(CallingConvention.StdCall)]
60 private delegate void AnchorClickedCallbackDelegate(IntPtr textField, IntPtr href, uint hrefLength);
62 [UnmanagedFunctionPointer(CallingConvention.StdCall)]
63 private delegate void SelectionChangedCallbackDelegate(IntPtr textField, uint oldStart, uint oldEnd);
65 [UnmanagedFunctionPointer(CallingConvention.StdCall)]
66 private delegate void InputFilteredCallbackDelegate(IntPtr textField, InputFilterType type);
68 [UnmanagedFunctionPointer(CallingConvention.StdCall)]
69 private delegate void SelectionClearedCallbackDelegate(IntPtr textField);
71 [UnmanagedFunctionPointer(CallingConvention.StdCall)]
72 private delegate void SelectionStartedCallbackDelegate(IntPtr textField);
74 private bool invokeTextChanged = true;
77 /// The TextChanged event.
79 /// <since_tizen> 3 </since_tizen>
80 public event EventHandler<TextChangedEventArgs> TextChanged
84 if (textFieldTextChangedEventHandler == null)
86 textFieldTextChangedCallbackDelegate = (OnTextChanged);
87 TextChangedSignal().Connect(textFieldTextChangedCallbackDelegate);
89 textFieldTextChangedEventHandler += value;
93 textFieldTextChangedEventHandler -= value;
94 if (textFieldTextChangedEventHandler == null && TextChangedSignal().Empty() == false)
96 TextChangedSignal().Disconnect(textFieldTextChangedCallbackDelegate);
102 /// The CursorPositionChanged event is emitted whenever the primary cursor position changed.
104 /// <since_tizen> 9 </since_tizen>
105 public event EventHandler CursorPositionChanged
109 if (textFieldCursorPositionChangedEventHandler == null)
111 textFieldCursorPositionChangedCallbackDelegate = (OnCursorPositionChanged);
112 CursorPositionChangedSignal().Connect(textFieldCursorPositionChangedCallbackDelegate);
114 textFieldCursorPositionChangedEventHandler += value;
118 if (textFieldCursorPositionChangedEventHandler == null && CursorPositionChangedSignal().Empty() == false)
120 this.CursorPositionChangedSignal().Disconnect(textFieldCursorPositionChangedCallbackDelegate);
122 textFieldCursorPositionChangedEventHandler -= value;
127 /// The MaxLengthReached event.
129 /// <since_tizen> 3 </since_tizen>
130 public event EventHandler<MaxLengthReachedEventArgs> MaxLengthReached
134 if (textFieldMaxLengthReachedEventHandler == null)
136 textFieldMaxLengthReachedCallbackDelegate = (OnMaxLengthReached);
137 MaxLengthReachedSignal().Connect(textFieldMaxLengthReachedCallbackDelegate);
139 textFieldMaxLengthReachedEventHandler += value;
143 if (textFieldMaxLengthReachedEventHandler == null && MaxLengthReachedSignal().Empty() == false)
145 this.MaxLengthReachedSignal().Disconnect(textFieldMaxLengthReachedCallbackDelegate);
147 textFieldMaxLengthReachedEventHandler -= value;
152 /// The SelectionStarted event is emitted when the selection start.
154 /// This will be public opened in after ACR done. Before ACR, need to be hidden as inhouse API.
155 [EditorBrowsable(EditorBrowsableState.Never)]
156 public event EventHandler SelectionStarted
160 if (textFieldSelectionStartedEventHandler == null)
162 textFieldSelectionStartedCallbackDelegate = (OnSelectionStarted);
163 SelectionStartedSignal().Connect(textFieldSelectionStartedCallbackDelegate);
165 textFieldSelectionStartedEventHandler += value;
169 if (textFieldSelectionStartedEventHandler == null && SelectionStartedSignal().Empty() == false)
171 this.SelectionStartedSignal().Disconnect(textFieldSelectionStartedCallbackDelegate);
173 textFieldSelectionStartedEventHandler -= value;
178 /// The SelectionCleared signal is emitted when selection is cleared.
180 /// <since_tizen> 9 </since_tizen>
181 public event EventHandler SelectionCleared
185 if (textFieldSelectionClearedEventHandler == null)
187 textFieldSelectionClearedCallbackDelegate = (OnSelectionCleared);
188 SelectionClearedSignal().Connect(textFieldSelectionClearedCallbackDelegate);
190 textFieldSelectionClearedEventHandler += value;
194 if (textFieldSelectionClearedEventHandler == null && SelectionClearedSignal().Empty() == false)
196 this.SelectionClearedSignal().Disconnect(textFieldSelectionClearedCallbackDelegate);
198 textFieldSelectionClearedEventHandler -= value;
203 /// The AnchorClicked signal is emitted when the anchor is clicked.
205 /// <since_tizen> 9 </since_tizen>
206 public event EventHandler<AnchorClickedEventArgs> AnchorClicked
210 if (textFieldAnchorClickedEventHandler == null)
212 textFieldAnchorClickedCallbackDelegate = (OnAnchorClicked);
213 AnchorClickedSignal().Connect(textFieldAnchorClickedCallbackDelegate);
215 textFieldAnchorClickedEventHandler += value;
219 textFieldAnchorClickedEventHandler -= value;
220 if (textFieldAnchorClickedEventHandler == null && AnchorClickedSignal().Empty() == false)
222 AnchorClickedSignal().Disconnect(textFieldAnchorClickedCallbackDelegate);
228 /// The SelectionChanged event is emitted whenever the selected text is changed.
230 /// <since_tizen> 9 </since_tizen>
231 public event EventHandler SelectionChanged
235 if (textFieldSelectionChangedEventHandler == null)
237 textFieldSelectionChangedCallbackDelegate = (OnSelectionChanged);
238 SelectionChangedSignal().Connect(textFieldSelectionChangedCallbackDelegate);
240 textFieldSelectionChangedEventHandler += value;
244 if (textFieldSelectionChangedEventHandler == null && SelectionChangedSignal().Empty() == false)
246 this.SelectionChangedSignal().Disconnect(textFieldSelectionChangedCallbackDelegate);
248 textFieldSelectionChangedEventHandler -= value;
253 /// The InputFiltered signal is emitted when the input is filtered by InputFilter. <br />
256 /// See <see cref="InputFilterType"/> and <see cref="InputFilteredEventArgs"/> for a detailed description. <br />
259 /// The following example demonstrates how to use the InputFiltered event.
261 /// field.InputFiltered += (s, e) =>
263 /// if (e.Type == InputFilterType.Accept)
265 /// // If input is filtered by InputFilter of Accept type.
267 /// else if (e.Type == InputFilterType.Reject)
269 /// // If input is filtered by InputFilter of Reject type.
274 /// <since_tizen> 9 </since_tizen>
275 public event EventHandler<InputFilteredEventArgs> InputFiltered
279 if (textFieldInputFilteredEventHandler == null)
281 textFieldInputFilteredCallbackDelegate = (OnInputFiltered);
282 InputFilteredSignal().Connect(textFieldInputFilteredCallbackDelegate);
284 textFieldInputFilteredEventHandler += value;
288 textFieldInputFilteredEventHandler -= value;
289 if (textFieldInputFilteredEventHandler == null && InputFilteredSignal().Empty() == false)
291 InputFilteredSignal().Disconnect(textFieldInputFilteredCallbackDelegate);
296 internal TextFieldSignal SelectionStartedSignal()
298 TextFieldSignal ret = new TextFieldSignal(Interop.TextField.SelectionStartedSignal(SwigCPtr), false);
299 if (NDalicPINVOKE.SWIGPendingException.Pending) throw NDalicPINVOKE.SWIGPendingException.Retrieve();
303 internal TextFieldSignal SelectionClearedSignal()
305 TextFieldSignal ret = new TextFieldSignal(Interop.TextField.SelectionClearedSignal(SwigCPtr), false);
306 if (NDalicPINVOKE.SWIGPendingException.Pending) throw NDalicPINVOKE.SWIGPendingException.Retrieve();
310 internal TextFieldSignal TextChangedSignal()
312 TextFieldSignal ret = new TextFieldSignal(Interop.TextField.TextChangedSignal(SwigCPtr), false);
313 if (NDalicPINVOKE.SWIGPendingException.Pending) throw NDalicPINVOKE.SWIGPendingException.Retrieve();
317 internal TextFieldSignal CursorPositionChangedSignal()
319 TextFieldSignal ret = new TextFieldSignal(Interop.TextField.CursorPositionChangedSignal(SwigCPtr), false);
320 if (NDalicPINVOKE.SWIGPendingException.Pending) throw NDalicPINVOKE.SWIGPendingException.Retrieve();
324 internal TextFieldSignal MaxLengthReachedSignal()
326 TextFieldSignal ret = new TextFieldSignal(Interop.TextField.MaxLengthReachedSignal(SwigCPtr), false);
327 if (NDalicPINVOKE.SWIGPendingException.Pending) throw NDalicPINVOKE.SWIGPendingException.Retrieve();
331 internal TextFieldSignal AnchorClickedSignal()
333 TextFieldSignal ret = new TextFieldSignal(Interop.TextField.AnchorClickedSignal(SwigCPtr), false);
334 if (NDalicPINVOKE.SWIGPendingException.Pending) throw NDalicPINVOKE.SWIGPendingException.Retrieve();
338 internal TextFieldSignal SelectionChangedSignal()
340 TextFieldSignal ret = new TextFieldSignal(Interop.TextField.SelectionChangedSignal(SwigCPtr), false);
341 if (NDalicPINVOKE.SWIGPendingException.Pending) throw NDalicPINVOKE.SWIGPendingException.Retrieve();
345 internal TextFieldSignal InputFilteredSignal()
347 TextFieldSignal ret = new TextFieldSignal(Interop.TextField.InputFilteredSignal(SwigCPtr), false);
348 if (NDalicPINVOKE.SWIGPendingException.Pending) throw NDalicPINVOKE.SWIGPendingException.Retrieve();
352 private void OnSelectionStarted(IntPtr textField)
354 //no data to be sent to the user
355 textFieldSelectionStartedEventHandler?.Invoke(this, EventArgs.Empty);
358 private void OnSelectionCleared(IntPtr textField)
360 //no data to be sent to the user
361 textFieldSelectionClearedEventHandler?.Invoke(this, EventArgs.Empty);
364 private void OnTextChanged(IntPtr textField)
366 if (textFieldTextChangedEventHandler != null && invokeTextChanged)
368 TextChangedEventArgs e = new TextChangedEventArgs();
370 // Populate all members of "e" (TextChangedEventArgs) with real data
371 e.TextField = Registry.GetManagedBaseHandleFromNativePtr(textField) as TextField;
372 //here we send all data to user event handlers
373 textFieldTextChangedEventHandler(this, e);
377 private void OnCursorPositionChanged(IntPtr textField, uint oldPosition)
379 // no data to be sent to the user, as in NUI there is no event provide old values.
380 textFieldCursorPositionChangedEventHandler?.Invoke(this, EventArgs.Empty);
383 private void OnMaxLengthReached(IntPtr textField)
385 if (textFieldMaxLengthReachedEventHandler != null)
387 MaxLengthReachedEventArgs e = new MaxLengthReachedEventArgs();
389 // Populate all members of "e" (MaxLengthReachedEventArgs) with real data
390 e.TextField = Registry.GetManagedBaseHandleFromNativePtr(textField) as TextField;
391 //here we send all data to user event handlers
392 textFieldMaxLengthReachedEventHandler(this, e);
396 private void OnAnchorClicked(IntPtr textField, IntPtr href, uint hrefLength)
398 // Note: hrefLength is useful for get the length of a const char* (href) in dali-toolkit.
399 // But NUI can get the length of string (href), so hrefLength is not necessary in NUI.
400 AnchorClickedEventArgs e = new AnchorClickedEventArgs();
402 // Populate all members of "e" (AnchorClickedEventArgs) with real data
403 e.Href = Marshal.PtrToStringAnsi(href);
404 //here we send all data to user event handlers
405 textFieldAnchorClickedEventHandler?.Invoke(this, e);
408 private void OnSelectionChanged(IntPtr textField, uint oldStart, uint oldEnd)
410 // no data to be sent to the user, as in NUI there is no event provide old values.
411 textFieldSelectionChangedEventHandler?.Invoke(this, EventArgs.Empty);
414 private void OnInputFiltered(IntPtr textField, InputFilterType type)
416 InputFilteredEventArgs e = new InputFilteredEventArgs();
418 // Populate all members of "e" (InputFilteredEventArgs) with real data
420 //here we send all data to user event handlers
421 textFieldInputFilteredEventHandler?.Invoke(this, e);
425 /// The TextChanged event arguments.
427 /// <since_tizen> 3 </since_tizen>
428 public class TextChangedEventArgs : EventArgs
430 private TextField textField;
435 /// <since_tizen> 3 </since_tizen>
436 public TextField TextField
450 /// The MaxLengthReached event arguments.
452 /// <since_tizen> 3 </since_tizen>
453 public class MaxLengthReachedEventArgs : EventArgs
455 private TextField textField;
460 /// <since_tizen> 3 </since_tizen>
461 public TextField TextField