9df9299ce35a4755150e4a1966c610c8258d9fe5
[platform/core/csapi/tizenfx.git] / src / Tizen.NUI / src / public / BaseComponents / TextEditorEvent.cs
1 /*
2  * Copyright(c) 2019 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.Runtime.InteropServices;
20
21 namespace Tizen.NUI.BaseComponents
22 {
23     /// <summary>
24     /// A control which provides a multi-line editable text editor.
25     /// </summary>
26     /// <since_tizen> 3 </since_tizen>
27     public partial class TextEditor
28     {
29         private EventHandler<TextChangedEventArgs> _textEditorTextChangedEventHandler;
30         private TextChangedCallbackDelegate _textEditorTextChangedCallbackDelegate;
31
32         private EventHandler<ScrollStateChangedEventArgs> _textEditorScrollStateChangedEventHandler;
33         private ScrollStateChangedCallbackDelegate _textEditorScrollStateChangedCallbackDelegate;
34
35         [UnmanagedFunctionPointer(CallingConvention.StdCall)]
36         private delegate void TextChangedCallbackDelegate(IntPtr textEditor);
37
38         [UnmanagedFunctionPointer(CallingConvention.StdCall)]
39         private delegate void ScrollStateChangedCallbackDelegate(IntPtr textEditor, ScrollState state);
40
41         /// <summary>
42         /// An event for the TextChanged signal which can be used to subscribe or unsubscribe the event handler
43         /// provided by the user. The TextChanged signal is emitted when the text changes.<br />
44         /// </summary>
45         /// <since_tizen> 3 </since_tizen>
46         public event EventHandler<TextChangedEventArgs> TextChanged
47         {
48             add
49             {
50                 if (_textEditorTextChangedEventHandler == null)
51                 {
52                     _textEditorTextChangedCallbackDelegate = (OnTextChanged);
53                     TextChangedSignal().Connect(_textEditorTextChangedCallbackDelegate);
54                 }
55                 _textEditorTextChangedEventHandler += value;
56             }
57             remove
58             {
59                 _textEditorTextChangedEventHandler -= value;
60                 if (_textEditorTextChangedEventHandler == null && TextChangedSignal().Empty() == false)
61                 {
62                     TextChangedSignal().Disconnect(_textEditorTextChangedCallbackDelegate);
63                 }
64             }
65         }
66
67         /// <summary>
68         /// Event for the ScrollStateChanged signal which can be used to subscribe or unsubscribe the event handler
69         /// provided by the user. The ScrollStateChanged signal is emitted when the scroll state changes.<br />
70         /// </summary>
71         /// <since_tizen> 3 </since_tizen>
72         public event EventHandler<ScrollStateChangedEventArgs> ScrollStateChanged
73         {
74             add
75             {
76                 if (_textEditorScrollStateChangedEventHandler == null)
77                 {
78                     _textEditorScrollStateChangedCallbackDelegate = OnScrollStateChanged;
79                     ScrollStateChangedSignal(this).Connect(_textEditorScrollStateChangedCallbackDelegate);
80                 }
81                 _textEditorScrollStateChangedEventHandler += value;
82             }
83             remove
84             {
85                 _textEditorScrollStateChangedEventHandler -= value;
86                 if (_textEditorScrollStateChangedEventHandler == null && ScrollStateChangedSignal(this).Empty() == false)
87                 {
88                     ScrollStateChangedSignal(this).Disconnect(_textEditorScrollStateChangedCallbackDelegate);
89                 }
90             }
91         }
92
93         internal TextEditorSignal TextChangedSignal()
94         {
95             TextEditorSignal ret = new TextEditorSignal(Interop.TextEditor.TextEditor_TextChangedSignal(swigCPtr), false);
96             if (NDalicPINVOKE.SWIGPendingException.Pending) throw NDalicPINVOKE.SWIGPendingException.Retrieve();
97             return ret;
98         }
99
100         internal ScrollStateChangedSignal ScrollStateChangedSignal(TextEditor textEditor)
101         {
102             ScrollStateChangedSignal ret = new ScrollStateChangedSignal(Interop.TextEditor.TextEditor_ScrollStateChangedSignal(TextEditor.getCPtr(textEditor)), false);
103             if (NDalicPINVOKE.SWIGPendingException.Pending) throw NDalicPINVOKE.SWIGPendingException.Retrieve();
104             return ret;
105         }
106
107         private void OnTextChanged(IntPtr textEditor)
108         {
109             TextChangedEventArgs e = new TextChangedEventArgs();
110
111             // Populate all members of "e" (TextChangedEventArgs) with real data
112             e.TextEditor = Registry.GetManagedBaseHandleFromNativePtr(textEditor) as TextEditor;
113
114             if (_textEditorTextChangedEventHandler != null)
115             {
116                 //here we send all data to user event handlers
117                 _textEditorTextChangedEventHandler(this, e);
118             }
119         }
120
121         private void OnScrollStateChanged(IntPtr textEditor, ScrollState state)
122         {
123             ScrollStateChangedEventArgs e = new ScrollStateChangedEventArgs();
124
125             if (textEditor != global::System.IntPtr.Zero)
126             {
127                 // Populate all members of "e" (ScrollStateChangedEventArgs) with real data
128                 e.TextEditor = Registry.GetManagedBaseHandleFromNativePtr(textEditor) as TextEditor;
129                 e.ScrollState = state;
130             }
131
132             if (_textEditorScrollStateChangedEventHandler != null)
133             {
134                 //here we send all data to user event handlers
135                 _textEditorScrollStateChangedEventHandler(this, e);
136             }
137         }
138
139         /// <summary>
140         /// Event arguments that passed via the TextChanged signal.
141         /// </summary>
142         /// <since_tizen> 3 </since_tizen>
143         public class TextChangedEventArgs : EventArgs
144         {
145             private TextEditor _textEditor;
146
147             /// <summary>
148             /// TextEditor - is the texteditor control which has the text contents changed.
149             /// </summary>
150             /// <since_tizen> 3 </since_tizen>
151             public TextEditor TextEditor
152             {
153                 get
154                 {
155                     return _textEditor;
156                 }
157                 set
158                 {
159                     _textEditor = value;
160                 }
161             }
162         }
163
164         /// <summary>
165         /// Event arguments that passed via the ScrollStateChanged signal.
166         /// </summary>
167         /// <since_tizen> 3 </since_tizen>
168         public class ScrollStateChangedEventArgs : EventArgs
169         {
170             private TextEditor _textEditor;
171             private ScrollState _scrollState;
172
173             /// <summary>
174             /// TextEditor - is the texteditor control which has the scroll state changed.
175             /// </summary>
176             /// <since_tizen> 3 </since_tizen>
177             public TextEditor TextEditor
178             {
179                 get
180                 {
181                     return _textEditor;
182                 }
183                 set
184                 {
185                     _textEditor = value;
186                 }
187             }
188
189             /// <summary>
190             /// ScrollState - is the texteditor control scroll state.
191             /// </summary>
192             /// <since_tizen> 3 </since_tizen>
193             public ScrollState ScrollState
194             {
195                 get
196                 {
197                     return _scrollState;
198                 }
199                 set
200                 {
201                     _scrollState = value;
202                 }
203             }
204         }
205     }
206 }