[NUI] Fix build warning & clean code [CA1051]
[platform/core/csapi/tizenfx.git] / src / Tizen.NUI / src / public / BaseComponents / TextFieldEvent.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 single line editable text field.
25     /// </summary>
26     /// <since_tizen> 3 </since_tizen>
27     public partial class TextField
28     {
29         private EventHandler<TextChangedEventArgs> _textFieldTextChangedEventHandler;
30         private TextChangedCallbackDelegate _textFieldTextChangedCallbackDelegate;
31         private EventHandler<MaxLengthReachedEventArgs> _textFieldMaxLengthReachedEventHandler;
32         private MaxLengthReachedCallbackDelegate _textFieldMaxLengthReachedCallbackDelegate;
33
34         [UnmanagedFunctionPointer(CallingConvention.StdCall)]
35         private delegate void TextChangedCallbackDelegate(IntPtr textField);
36
37         [UnmanagedFunctionPointer(CallingConvention.StdCall)]
38         private delegate void MaxLengthReachedCallbackDelegate(IntPtr textField);
39
40         /// <summary>
41         /// The TextChanged event.
42         /// </summary>
43         /// <since_tizen> 3 </since_tizen>
44         public event EventHandler<TextChangedEventArgs> TextChanged
45         {
46             add
47             {
48                 if (_textFieldTextChangedEventHandler == null)
49                 {
50                     _textFieldTextChangedCallbackDelegate = (OnTextChanged);
51                     TextChangedSignal().Connect(_textFieldTextChangedCallbackDelegate);
52                 }
53                 _textFieldTextChangedEventHandler += value;
54             }
55             remove
56             {
57                 _textFieldTextChangedEventHandler -= value;
58                 if (_textFieldTextChangedEventHandler == null && TextChangedSignal().Empty() == false)
59                 {
60                     TextChangedSignal().Disconnect(_textFieldTextChangedCallbackDelegate);
61                 }
62             }
63         }
64
65         /// <summary>
66         /// The MaxLengthReached event.
67         /// </summary>
68         /// <since_tizen> 3 </since_tizen>
69         public event EventHandler<MaxLengthReachedEventArgs> MaxLengthReached
70         {
71             add
72             {
73                 if (_textFieldMaxLengthReachedEventHandler == null)
74                 {
75                     _textFieldMaxLengthReachedCallbackDelegate = (OnMaxLengthReached);
76                     MaxLengthReachedSignal().Connect(_textFieldMaxLengthReachedCallbackDelegate);
77                 }
78                 _textFieldMaxLengthReachedEventHandler += value;
79             }
80             remove
81             {
82                 if (_textFieldMaxLengthReachedEventHandler == null && MaxLengthReachedSignal().Empty() == false)
83                 {
84                     this.MaxLengthReachedSignal().Disconnect(_textFieldMaxLengthReachedCallbackDelegate);
85                 }
86                 _textFieldMaxLengthReachedEventHandler -= value;
87             }
88         }
89
90         internal TextFieldSignal TextChangedSignal()
91         {
92             TextFieldSignal ret = new TextFieldSignal(Interop.TextField.TextChangedSignal(SwigCPtr), false);
93             if (NDalicPINVOKE.SWIGPendingException.Pending) throw NDalicPINVOKE.SWIGPendingException.Retrieve();
94             return ret;
95         }
96
97         internal TextFieldSignal MaxLengthReachedSignal()
98         {
99             TextFieldSignal ret = new TextFieldSignal(Interop.TextField.MaxLengthReachedSignal(SwigCPtr), false);
100             if (NDalicPINVOKE.SWIGPendingException.Pending) throw NDalicPINVOKE.SWIGPendingException.Retrieve();
101             return ret;
102         }
103
104         private void OnTextChanged(IntPtr textField)
105         {
106             TextChangedEventArgs e = new TextChangedEventArgs();
107
108             // Populate all members of "e" (TextChangedEventArgs) with real data
109             e.TextField = Registry.GetManagedBaseHandleFromNativePtr(textField) as TextField;
110
111             if (_textFieldTextChangedEventHandler != null)
112             {
113                 //here we send all data to user event handlers
114                 _textFieldTextChangedEventHandler(this, e);
115             }
116         }
117
118         private void OnMaxLengthReached(IntPtr textField)
119         {
120             MaxLengthReachedEventArgs e = new MaxLengthReachedEventArgs();
121
122             // Populate all members of "e" (MaxLengthReachedEventArgs) with real data
123             e.TextField = Registry.GetManagedBaseHandleFromNativePtr(textField) as TextField;
124
125             if (_textFieldMaxLengthReachedEventHandler != null)
126             {
127                 //here we send all data to user event handlers
128                 _textFieldMaxLengthReachedEventHandler(this, e);
129             }
130         }
131
132         /// <summary>
133         /// The TextChanged event arguments.
134         /// </summary>
135         /// <since_tizen> 3 </since_tizen>
136         public class TextChangedEventArgs : EventArgs
137         {
138             private TextField _textField;
139
140             /// <summary>
141             /// TextField.
142             /// </summary>
143             /// <since_tizen> 3 </since_tizen>
144             public TextField TextField
145             {
146                 get
147                 {
148                     return _textField;
149                 }
150                 set
151                 {
152                     _textField = value;
153                 }
154             }
155         }
156
157         /// <summary>
158         /// The MaxLengthReached event arguments.
159         /// </summary>
160         /// <since_tizen> 3 </since_tizen>
161         public class MaxLengthReachedEventArgs : EventArgs
162         {
163             private TextField _textField;
164
165             /// <summary>
166             /// TextField.
167             /// </summary>
168             /// <since_tizen> 3 </since_tizen>
169             public TextField TextField
170             {
171                 get
172                 {
173                     return _textField;
174                 }
175                 set
176                 {
177                     _textField = value;
178                 }
179             }
180         }
181     }
182 }