[NUI] update event description
[platform/core/csapi/tizenfx.git] / src / Tizen.NUI / src / public / BaseComponents / TextEditorEvent.cs
1 /*
2  * Copyright(c) 2021 Samsung Electronics Co., Ltd.
3  *
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
7  *
8  * http://www.apache.org/licenses/LICENSE-2.0
9  *
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.
15  *
16  */
17
18 using System;
19 using System.ComponentModel;
20 using System.Runtime.InteropServices;
21
22 namespace Tizen.NUI.BaseComponents
23 {
24     /// <summary>
25     /// A control which provides a multi-line editable text editor.
26     /// </summary>
27     /// <since_tizen> 3 </since_tizen>
28     public partial class TextEditor
29     {
30         private EventHandler<TextChangedEventArgs> textEditorTextChangedEventHandler;
31         private TextChangedCallbackDelegate textEditorTextChangedCallbackDelegate;
32
33         private EventHandler<ScrollStateChangedEventArgs> textEditorScrollStateChangedEventHandler;
34         private ScrollStateChangedCallbackDelegate textEditorScrollStateChangedCallbackDelegate;
35
36         private EventHandler textEditorCursorPositionChangedEventHandler;
37         private CursorPositionChangedCallbackDelegate textEditorCursorPositionChangedCallbackDelegate;
38
39         private EventHandler<MaxLengthReachedEventArgs> textEditorMaxLengthReachedEventHandler;
40         private MaxLengthReachedCallbackDelegate textEditorMaxLengthReachedCallbackDelegate;
41
42         private EventHandler<AnchorClickedEventArgs> textEditorAnchorClickedEventHandler;
43         private AnchorClickedCallbackDelegate textEditorAnchorClickedCallbackDelegate;
44
45         private EventHandler textEditorSelectionClearedEventHandler;
46         private SelectionClearedCallbackDelegate textEditorSelectionClearedCallbackDelegate;
47
48         private EventHandler textEditorSelectionChangedEventHandler;
49         private SelectionChangedCallbackDelegate textEditorSelectionChangedCallbackDelegate;
50
51         private EventHandler<InputFilteredEventArgs> textEditorInputFilteredEventHandler;
52         private InputFilteredCallbackDelegate textEditorInputFilteredCallbackDelegate;
53
54         [UnmanagedFunctionPointer(CallingConvention.StdCall)]
55         private delegate void TextChangedCallbackDelegate(IntPtr textEditor);
56
57         [UnmanagedFunctionPointer(CallingConvention.StdCall)]
58         private delegate void ScrollStateChangedCallbackDelegate(IntPtr textEditor, ScrollState state);
59
60         [UnmanagedFunctionPointer(CallingConvention.StdCall)]
61         private delegate void CursorPositionChangedCallbackDelegate(IntPtr textEditor, uint oldPosition);
62
63         [UnmanagedFunctionPointer(CallingConvention.StdCall)]
64         private delegate void MaxLengthReachedCallbackDelegate(IntPtr textEditor);
65
66         [UnmanagedFunctionPointer(CallingConvention.StdCall)]
67         private delegate void SelectionClearedCallbackDelegate(IntPtr textEditor);
68
69         [UnmanagedFunctionPointer(CallingConvention.StdCall)]
70         private delegate void AnchorClickedCallbackDelegate(IntPtr textEditor, IntPtr href, uint hrefLength);
71
72         [UnmanagedFunctionPointer(CallingConvention.StdCall)]
73         private delegate void SelectionChangedCallbackDelegate(IntPtr textEditor, uint oldStart, uint oldEnd);
74
75         [UnmanagedFunctionPointer(CallingConvention.StdCall)]
76         private delegate void InputFilteredCallbackDelegate(IntPtr textEditor, InputFilterType type);
77
78         /// <summary>
79         /// An event for the TextChanged signal which can be used to subscribe or unsubscribe the event handler
80         /// provided by the user. The TextChanged signal is emitted when the text changes.<br />
81         /// </summary>
82         /// <since_tizen> 3 </since_tizen>
83         public event EventHandler<TextChangedEventArgs> TextChanged
84         {
85             add
86             {
87                 if (textEditorTextChangedEventHandler == null)
88                 {
89                     textEditorTextChangedCallbackDelegate = (OnTextChanged);
90                     TextChangedSignal().Connect(textEditorTextChangedCallbackDelegate);
91                 }
92                 textEditorTextChangedEventHandler += value;
93             }
94             remove
95             {
96                 textEditorTextChangedEventHandler -= value;
97                 if (textEditorTextChangedEventHandler == null && TextChangedSignal().Empty() == false)
98                 {
99                     TextChangedSignal().Disconnect(textEditorTextChangedCallbackDelegate);
100                 }
101             }
102         }
103
104         /// <summary>
105         /// Event for the ScrollStateChanged signal which can be used to subscribe or unsubscribe the event handler
106         /// provided by the user. The ScrollStateChanged signal is emitted when the scroll state changes.<br />
107         /// </summary>
108         /// <since_tizen> 3 </since_tizen>
109         public event EventHandler<ScrollStateChangedEventArgs> ScrollStateChanged
110         {
111             add
112             {
113                 if (textEditorScrollStateChangedEventHandler == null)
114                 {
115                     textEditorScrollStateChangedCallbackDelegate = OnScrollStateChanged;
116                     ScrollStateChangedSignal(this).Connect(textEditorScrollStateChangedCallbackDelegate);
117                 }
118                 textEditorScrollStateChangedEventHandler += value;
119             }
120             remove
121             {
122                 textEditorScrollStateChangedEventHandler -= value;
123                 if (textEditorScrollStateChangedEventHandler == null && ScrollStateChangedSignal(this).Empty() == false)
124                 {
125                     ScrollStateChangedSignal(this).Disconnect(textEditorScrollStateChangedCallbackDelegate);
126                 }
127             }
128         }
129
130         /// <summary>
131         /// The CursorPositionChanged event.
132         /// </summary>
133         /// This will be public opened after ACR done. Before ACR, need to be hidden as inhouse API.
134         [EditorBrowsable(EditorBrowsableState.Never)]
135         public event EventHandler CursorPositionChanged
136         {
137             add
138             {
139                 if (textEditorCursorPositionChangedEventHandler == null)
140                 {
141                     textEditorCursorPositionChangedCallbackDelegate = (OnCursorPositionChanged);
142                     CursorPositionChangedSignal().Connect(textEditorCursorPositionChangedCallbackDelegate);
143                 }
144                 textEditorCursorPositionChangedEventHandler += value;
145             }
146             remove
147             {
148                 if (textEditorCursorPositionChangedEventHandler == null && CursorPositionChangedSignal().Empty() == false)
149                 {
150                     this.CursorPositionChangedSignal().Disconnect(textEditorCursorPositionChangedCallbackDelegate);
151                 }
152                 textEditorCursorPositionChangedEventHandler -= value;
153             }
154         }
155
156         /// <summary>
157         /// The MaxLengthReached event.
158         /// </summary>
159         /// This will be public opened in tizen_6.5 after ACR done. Before ACR, need to be hidden as inhouse API.
160         [EditorBrowsable(EditorBrowsableState.Never)]
161         public event EventHandler<MaxLengthReachedEventArgs> MaxLengthReached
162         {
163             add
164             {
165                 if (textEditorMaxLengthReachedEventHandler == null)
166                 {
167                     textEditorMaxLengthReachedCallbackDelegate = (OnMaxLengthReached);
168                     MaxLengthReachedSignal().Connect(textEditorMaxLengthReachedCallbackDelegate);
169                 }
170                 textEditorMaxLengthReachedEventHandler += value;
171             }
172             remove
173             {
174                 if (textEditorMaxLengthReachedEventHandler == null && MaxLengthReachedSignal().Empty() == false)
175                 {
176                     this.MaxLengthReachedSignal().Disconnect(textEditorMaxLengthReachedCallbackDelegate);
177                 }
178                 textEditorMaxLengthReachedEventHandler -= value;
179             }
180         }
181
182         /// <summary>
183         /// The AnchorClicked signal is emitted when the anchor is clicked.
184         /// </summary>
185         /// <since_tizen> 9 </since_tizen>
186         public event EventHandler<AnchorClickedEventArgs> AnchorClicked
187         {
188             add
189             {
190                 if (textEditorAnchorClickedEventHandler == null)
191                 {
192                     textEditorAnchorClickedCallbackDelegate = (OnAnchorClicked);
193                     AnchorClickedSignal().Connect(textEditorAnchorClickedCallbackDelegate);
194                 }
195                 textEditorAnchorClickedEventHandler += value;
196             }
197             remove
198             {
199                 textEditorAnchorClickedEventHandler -= value;
200                 if (textEditorAnchorClickedEventHandler == null && AnchorClickedSignal().Empty() == false)
201                 {
202                     AnchorClickedSignal().Disconnect(textEditorAnchorClickedCallbackDelegate);
203                 }
204             }
205         }
206
207         /// <summary>
208         /// The SelectionCleared signal is emitted when selection is cleared.
209         /// </summary>
210         /// This will be public opened after ACR done. Before ACR, need to be hidden as inhouse API.
211         [EditorBrowsable(EditorBrowsableState.Never)]
212         public event EventHandler SelectionCleared
213         {
214             add
215             {
216                 if (textEditorSelectionClearedEventHandler == null)
217                 {
218                     textEditorSelectionClearedCallbackDelegate = (OnSelectionCleared);
219                     SelectionClearedSignal().Connect(textEditorSelectionClearedCallbackDelegate);
220                 }
221                 textEditorSelectionClearedEventHandler += value;
222             }
223             remove
224             {
225                 if (textEditorSelectionClearedEventHandler == null && SelectionClearedSignal().Empty() == false)
226                 {
227                     this.SelectionClearedSignal().Disconnect(textEditorSelectionClearedCallbackDelegate);
228                 }
229                 textEditorSelectionClearedEventHandler -= value;
230             }
231         }
232
233         /// <summary>
234         /// The SelectionChanged signal is emitted whenever the selected text changed.
235         /// </summary>
236         /// <since_tizen> 9 </since_tizen>
237         public event EventHandler SelectionChanged
238         {
239             add
240             {
241                 if (textEditorSelectionChangedEventHandler == null)
242                 {
243                     textEditorSelectionChangedCallbackDelegate = (OnSelectionChanged);
244                     SelectionChangedSignal().Connect(textEditorSelectionChangedCallbackDelegate);
245                 }
246                 textEditorSelectionChangedEventHandler += value;
247             }
248             remove
249             {
250                 if (textEditorSelectionChangedEventHandler == null && SelectionChangedSignal().Empty() == false)
251                 {
252                     this.SelectionChangedSignal().Disconnect(textEditorSelectionChangedCallbackDelegate);
253                 }
254                 textEditorSelectionChangedEventHandler -= value;
255             }
256         }
257
258         /// <summary>
259         /// The InputFiltered signal is emitted when the input is filtered by InputFilter. <br />
260         /// </summary>
261         /// <remarks>
262         /// See <see cref="InputFilterType"/> and <see cref="InputFilteredEventArgs"/> for a detailed description. <br />
263         /// </remarks>
264         /// <example>
265         /// The following example demonstrates how to use the InputFiltered event.
266         /// <code>
267         /// editor.InputFiltered += (s, e) =>
268         /// {
269         ///     if (e.Type == InputFilterType.Accept)
270         ///     {
271         ///         // If input is filtered by InputFilter of Accept type.
272         ///     }
273         ///     else if (e.Type == InputFilterType.Reject)
274         ///     {
275         ///         // If input is filtered by InputFilter of Reject type.
276         ///     }
277         /// };
278         /// </code>
279         /// </example>
280         [EditorBrowsable(EditorBrowsableState.Never)]
281         public event EventHandler<InputFilteredEventArgs> InputFiltered
282         {
283             add
284             {
285                 if (textEditorInputFilteredEventHandler == null)
286                 {
287                     textEditorInputFilteredCallbackDelegate = (OnInputFiltered);
288                     InputFilteredSignal().Connect(textEditorInputFilteredCallbackDelegate);
289                 }
290                 textEditorInputFilteredEventHandler += value;
291             }
292             remove
293             {
294                 textEditorInputFilteredEventHandler -= value;
295                 if (textEditorInputFilteredEventHandler == null && InputFilteredSignal().Empty() == false)
296                 {
297                     InputFilteredSignal().Disconnect(textEditorInputFilteredCallbackDelegate);
298                 }
299             }
300         }
301
302         internal TextEditorSignal SelectionClearedSignal()
303         {
304             TextEditorSignal ret = new TextEditorSignal(Interop.TextEditor.SelectionClearedSignal(SwigCPtr), false);
305             if (NDalicPINVOKE.SWIGPendingException.Pending) throw NDalicPINVOKE.SWIGPendingException.Retrieve();
306             return ret;
307         }
308
309         internal TextEditorSignal TextChangedSignal()
310         {
311             TextEditorSignal ret = new TextEditorSignal(Interop.TextEditor.TextChangedSignal(SwigCPtr), false);
312             if (NDalicPINVOKE.SWIGPendingException.Pending) throw NDalicPINVOKE.SWIGPendingException.Retrieve();
313             return ret;
314         }
315
316         internal ScrollStateChangedSignal ScrollStateChangedSignal(TextEditor textEditor)
317         {
318             ScrollStateChangedSignal ret = new ScrollStateChangedSignal(Interop.TextEditor.ScrollStateChangedSignal(TextEditor.getCPtr(textEditor)), false);
319             if (NDalicPINVOKE.SWIGPendingException.Pending) throw NDalicPINVOKE.SWIGPendingException.Retrieve();
320             return ret;
321         }
322
323         internal TextEditorSignal CursorPositionChangedSignal()
324         {
325             TextEditorSignal ret = new TextEditorSignal(Interop.TextEditor.CursorPositionChangedSignal(SwigCPtr), false);
326             if (NDalicPINVOKE.SWIGPendingException.Pending) throw NDalicPINVOKE.SWIGPendingException.Retrieve();
327             return ret;
328         }
329
330         internal TextEditorSignal MaxLengthReachedSignal()
331         {
332             TextEditorSignal ret = new TextEditorSignal(Interop.TextEditor.MaxLengthReachedSignal(SwigCPtr), false);
333             if (NDalicPINVOKE.SWIGPendingException.Pending) throw NDalicPINVOKE.SWIGPendingException.Retrieve();
334             return ret;
335         }
336
337         internal TextEditorSignal AnchorClickedSignal()
338         {
339             TextEditorSignal ret = new TextEditorSignal(Interop.TextEditor.AnchorClickedSignal(SwigCPtr), false);
340             if (NDalicPINVOKE.SWIGPendingException.Pending) throw NDalicPINVOKE.SWIGPendingException.Retrieve();
341             return ret;
342         }
343
344         internal TextEditorSignal SelectionChangedSignal()
345         {
346             TextEditorSignal ret = new TextEditorSignal(Interop.TextEditor.SelectionChangedSignal(SwigCPtr), false);
347             if (NDalicPINVOKE.SWIGPendingException.Pending) throw NDalicPINVOKE.SWIGPendingException.Retrieve();
348             return ret;
349         }
350
351         internal TextEditorSignal InputFilteredSignal()
352         {
353             TextEditorSignal ret = new TextEditorSignal(Interop.TextEditor.InputFilteredSignal(SwigCPtr), false);
354             if (NDalicPINVOKE.SWIGPendingException.Pending) throw NDalicPINVOKE.SWIGPendingException.Retrieve();
355             return ret;
356         }
357
358         private void OnTextChanged(IntPtr textEditor)
359         {
360             if (textEditorTextChangedEventHandler != null)
361             {
362                 TextChangedEventArgs e = new TextChangedEventArgs();
363
364                 // Populate all members of "e" (TextChangedEventArgs) with real data
365                 e.TextEditor = Registry.GetManagedBaseHandleFromNativePtr(textEditor) as TextEditor;
366                 //here we send all data to user event handlers
367                 textEditorTextChangedEventHandler(this, e);
368             }
369         }
370
371         private void OnSelectionCleared(IntPtr textEditor)
372         {
373             //no data to be sent to the user
374             textEditorSelectionClearedEventHandler?.Invoke(this, EventArgs.Empty);
375         }
376
377         private void OnScrollStateChanged(IntPtr textEditor, ScrollState state)
378         {
379             if (textEditorScrollStateChangedEventHandler != null)
380             {
381                 ScrollStateChangedEventArgs e = new ScrollStateChangedEventArgs();
382
383                 if (textEditor != global::System.IntPtr.Zero)
384                 {
385                     // Populate all members of "e" (ScrollStateChangedEventArgs) with real data
386                     e.TextEditor = Registry.GetManagedBaseHandleFromNativePtr(textEditor) as TextEditor;
387                     e.ScrollState = state;
388                 }
389                 //here we send all data to user event handlers
390                 textEditorScrollStateChangedEventHandler(this, e);
391             }
392         }
393
394         private void OnCursorPositionChanged(IntPtr textEditor, uint oldPosition)
395         {
396             // no data to be sent to the user, as in NUI there is no event provide old values.
397             textEditorCursorPositionChangedEventHandler?.Invoke(this, EventArgs.Empty);
398         }
399
400         private void OnMaxLengthReached(IntPtr textEditor)
401         {
402             if (textEditorMaxLengthReachedEventHandler != null)
403             {
404                 MaxLengthReachedEventArgs e = new MaxLengthReachedEventArgs();
405
406                 // Populate all members of "e" (MaxLengthReachedEventArgs) with real data
407                 e.TextEditor = Registry.GetManagedBaseHandleFromNativePtr(textEditor) as TextEditor;
408                 //here we send all data to user event handlers
409                 textEditorMaxLengthReachedEventHandler(this, e);
410             }
411         }
412
413         private void OnAnchorClicked(IntPtr textEditor, IntPtr href, uint hrefLength)
414         {
415             // Note: hrefLength is useful for get the length of a const char* (href) in dali-toolkit.
416             // But NUI can get the length of string (href), so hrefLength is not necessary in NUI.
417             AnchorClickedEventArgs e = new AnchorClickedEventArgs();
418
419             // Populate all members of "e" (AnchorClickedEventArgs) with real data
420             e.Href = Marshal.PtrToStringAnsi(href);
421             //here we send all data to user event handlers
422             textEditorAnchorClickedEventHandler?.Invoke(this, e);
423         }
424
425         private void OnSelectionChanged(IntPtr textEditor, uint oldStart, uint oldEnd)
426         {
427             // no data to be sent to the user, as in NUI there is no event provide old values.
428             textEditorSelectionChangedEventHandler?.Invoke(this, EventArgs.Empty);
429         }
430
431         private void OnInputFiltered(IntPtr textEditor, InputFilterType type)
432         {
433             InputFilteredEventArgs e = new InputFilteredEventArgs();
434
435             // Populate all members of "e" (InputFilteredEventArgs) with real data
436             e.Type = type;
437             //here we send all data to user event handlers
438             textEditorInputFilteredEventHandler?.Invoke(this, e);
439         }
440
441         /// <summary>
442         /// Event arguments that passed via the TextChanged signal.
443         /// </summary>
444         /// <since_tizen> 3 </since_tizen>
445         public class TextChangedEventArgs : EventArgs
446         {
447             private TextEditor textEditor;
448
449             /// <summary>
450             /// TextEditor - is the texteditor control which has the text contents changed.
451             /// </summary>
452             /// <since_tizen> 3 </since_tizen>
453             public TextEditor TextEditor
454             {
455                 get
456                 {
457                     return textEditor;
458                 }
459                 set
460                 {
461                     textEditor = value;
462                 }
463             }
464         }
465
466         /// <summary>
467         /// Event arguments that passed via the ScrollStateChanged signal.
468         /// </summary>
469         /// <since_tizen> 3 </since_tizen>
470         public class ScrollStateChangedEventArgs : EventArgs
471         {
472             private TextEditor textEditor;
473             private ScrollState scrollState;
474
475             /// <summary>
476             /// TextEditor - is the texteditor control which has the scroll state changed.
477             /// </summary>
478             /// <since_tizen> 3 </since_tizen>
479             public TextEditor TextEditor
480             {
481                 get
482                 {
483                     return textEditor;
484                 }
485                 set
486                 {
487                     textEditor = value;
488                 }
489             }
490
491             /// <summary>
492             /// ScrollState - is the texteditor control scroll state.
493             /// </summary>
494             /// <since_tizen> 3 </since_tizen>
495             public ScrollState ScrollState
496             {
497                 get
498                 {
499                     return scrollState;
500                 }
501                 set
502                 {
503                     scrollState = value;
504                 }
505             }
506         }
507
508         /// <summary>
509         /// The MaxLengthReached event arguments.
510         /// </summary>
511         /// This will be public opened in tizen_6.5 after ACR done. Before ACR, need to be hidden as inhouse API.
512         [EditorBrowsable(EditorBrowsableState.Never)]
513         public class MaxLengthReachedEventArgs : EventArgs
514         {
515             private TextEditor textEditor;
516
517             /// <summary>
518             /// TextEditor.
519             /// </summary>
520             /// This will be public opened in tizen_6.5 after ACR done. Before ACR, need to be hidden as inhouse API.
521             [EditorBrowsable(EditorBrowsableState.Never)]
522             public TextEditor TextEditor
523             {
524                 get
525                 {
526                     return textEditor;
527                 }
528                 set
529                 {
530                     textEditor = value;
531                 }
532             }
533         }
534     }
535 }