sync with tizen branch to finalize API
[platform/core/csapi/tizenfx.git] / src / Tizen.NUI / src / public / Key.cs
1 /** Copyright (c) 2017 Samsung Electronics Co., Ltd.
2 *
3 * Licensed under the Apache License, Version 2.0 (the "License");
4 * you may not use this file except in compliance with the License.
5 * You may obtain a copy of the License at
6 *
7 * http://www.apache.org/licenses/LICENSE-2.0
8 *
9 * Unless required by applicable law or agreed to in writing, software
10 * distributed under the License is distributed on an "AS IS" BASIS,
11 * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
12 * See the License for the specific language governing permissions and
13 * limitations under the License.
14 *
15 */
16
17 namespace Tizen.NUI
18 {
19
20     /// <summary>
21     /// The key structure is used to store a key press.
22     /// </summary>
23     public class Key : global::System.IDisposable
24     {
25         private global::System.Runtime.InteropServices.HandleRef swigCPtr;
26         protected bool swigCMemOwn;
27
28         internal Key(global::System.IntPtr cPtr, bool cMemoryOwn)
29         {
30             swigCMemOwn = cMemoryOwn;
31             swigCPtr = new global::System.Runtime.InteropServices.HandleRef(this, cPtr);
32         }
33
34         internal static global::System.Runtime.InteropServices.HandleRef getCPtr(Key obj)
35         {
36             return (obj == null) ? new global::System.Runtime.InteropServices.HandleRef(null, global::System.IntPtr.Zero) : obj.swigCPtr;
37         }
38
39         //A Flag to check who called Dispose(). (By User or DisposeQueue)
40         private bool isDisposeQueued = false;
41         //A Flat to check if it is already disposed.
42         protected bool disposed = false;
43
44         ~Key()
45         {
46             if(!isDisposeQueued)
47             {
48                 isDisposeQueued = true;
49                 DisposeQueue.Instance.Add(this);
50             }
51         }
52
53         public void Dispose()
54         {
55             //Throw excpetion if Dispose() is called in separate thread.
56             if (!Window.IsInstalled())
57             {
58                 throw new System.InvalidOperationException("This API called from separate thread. This API must be called from MainThread.");
59             }
60
61             if (isDisposeQueued)
62             {
63                 Dispose(DisposeTypes.Implicit);
64             }
65             else
66             {
67                 Dispose(DisposeTypes.Explicit);
68                 System.GC.SuppressFinalize(this);
69             }
70         }
71
72         protected virtual void Dispose(DisposeTypes type)
73         {
74             if (disposed)
75             {
76                 return;
77             }
78
79             if(type == DisposeTypes.Explicit)
80             {
81                 //Called by User
82                 //Release your own managed resources here.
83                 //You should release all of your own disposable objects here.
84             }
85
86             //Release your own unmanaged resources here.
87             //You should not access any managed member here except static instance.
88             //because the execution order of Finalizes is non-deterministic.
89
90             if (swigCPtr.Handle != global::System.IntPtr.Zero)
91             {
92                 if (swigCMemOwn)
93                 {
94                     swigCMemOwn = false;
95                     NDalicPINVOKE.delete_Key(swigCPtr);
96                 }
97                 swigCPtr = new global::System.Runtime.InteropServices.HandleRef(null, global::System.IntPtr.Zero);
98             }
99             disposed = true;
100         }
101
102
103         internal static Key GetKeyFromPtr(global::System.IntPtr cPtr)
104         {
105             Key ret = new Key(cPtr, false);
106             if (NDalicPINVOKE.SWIGPendingException.Pending) throw NDalicPINVOKE.SWIGPendingException.Retrieve();
107             return ret;
108         }
109
110         public string DeviceName
111         {
112             get
113             {
114                 string ret = NDalicPINVOKE.GetDeviceName(swigCPtr);
115                 if (NDalicPINVOKE.SWIGPendingException.Pending) throw NDalicPINVOKE.SWIGPendingException.Retrieve();
116                 return ret;
117             }
118         }
119
120         /// <summary>
121         /// Name given to the key pressed
122         /// </summary>
123         public string KeyPressedName
124         {
125             get
126             {
127                 return keyPressedName;
128             }
129         }
130
131         /* duplicated with KeyPressedName : removed
132         public string KeyPressed
133         {
134             get
135             {
136                 return keyPressed;
137             }
138         }
139         */
140
141         /// <summary>
142         /// Keycode for the key pressed.
143         /// </summary>
144         public int KeyCode
145         {
146             get
147             {
148                 return keyCode;
149             }
150         }
151
152         /// <summary>
153         /// Special keys like shift, alt and control which modify the next key pressed.
154         /// </summary>
155         public int KeyModifier
156         {
157             get
158             {
159                 return keyModifier;
160             }
161         }
162
163         /// <summary>
164         /// The time (in ms) that the key event occurred.
165         /// </summary>
166         public uint Time
167         {
168             get
169             {
170                 return time;
171             }
172         }
173
174         /// <summary>
175         /// State of the key event.
176         /// </summary>
177         public Key.StateType State
178         {
179             get
180             {
181                 return state;
182             }
183         }
184
185         public DeviceClassType DeviceClass
186         {
187             get
188             {
189                 int ret = NDalicPINVOKE.GetDeviceClass(swigCPtr);
190                 if (NDalicPINVOKE.SWIGPendingException.Pending) throw NDalicPINVOKE.SWIGPendingException.Retrieve();
191                 return (DeviceClassType)ret;
192             }
193         }
194
195
196         /// <summary>
197         /// Default Constructor.
198         /// </summary>
199         public Key() : this(NDalicPINVOKE.new_Key__SWIG_0(), true)
200         {
201             if (NDalicPINVOKE.SWIGPendingException.Pending) throw NDalicPINVOKE.SWIGPendingException.Retrieve();
202         }
203
204         /// <summary>
205         /// Constructor.
206         /// </summary>
207         /// <param name="keyName">The name of the key pressed or command from the IMF, if later then the some following parameters will be needed</param>
208         /// <param name="keyString">A string of input characters or key pressed</param>
209         /// <param name="keyCode">The unique key code for the key pressed</param>
210         /// <param name="keyModifier">The key modifier for special keys like shift and alt</param>
211         /// <param name="timeStamp">The time (in ms) that the key event occurred</param>
212         /// <param name="keyState">The state of the key event</param>
213         internal Key(string keyName, string keyString, int keyCode, int keyModifier, uint timeStamp, Key.StateType keyState) : this(NDalicPINVOKE.new_Key__SWIG_1(keyName, keyString, keyCode, keyModifier, timeStamp, (int)keyState), true)
214         {
215             if (NDalicPINVOKE.SWIGPendingException.Pending) throw NDalicPINVOKE.SWIGPendingException.Retrieve();
216         }
217
218         /// <summary>
219         /// Checks to see if Shift key modifier has been supplied.
220         /// </summary>
221         /// <returns>True if shift modifier</returns>
222         public bool IsShiftModifier()
223         {
224             bool ret = NDalicPINVOKE.Key_IsShiftModifier(swigCPtr);
225             if (NDalicPINVOKE.SWIGPendingException.Pending) throw NDalicPINVOKE.SWIGPendingException.Retrieve();
226             return ret;
227         }
228
229         /// <summary>
230         /// Checks to see if Ctrl (control) key modifier has been supplied.
231         /// </summary>
232         /// <returns>True if ctrl modifier</returns>
233         public bool IsCtrlModifier()
234         {
235             bool ret = NDalicPINVOKE.Key_IsCtrlModifier(swigCPtr);
236             if (NDalicPINVOKE.SWIGPendingException.Pending) throw NDalicPINVOKE.SWIGPendingException.Retrieve();
237             return ret;
238         }
239
240         /// <summary>
241         /// Checks to see if Alt key modifier has been supplied.
242         /// </summary>
243         /// <returns>True if alt modifier</returns>
244         public bool IsAltModifier()
245         {
246             bool ret = NDalicPINVOKE.Key_IsAltModifier(swigCPtr);
247             if (NDalicPINVOKE.SWIGPendingException.Pending) throw NDalicPINVOKE.SWIGPendingException.Retrieve();
248             return ret;
249         }
250
251         private string keyPressedName
252         {
253             set
254             {
255                 NDalicPINVOKE.Key_keyPressedName_set(swigCPtr, value);
256                 if (NDalicPINVOKE.SWIGPendingException.Pending) throw NDalicPINVOKE.SWIGPendingException.Retrieve();
257             }
258             get
259             {
260                 string ret = NDalicPINVOKE.Key_keyPressedName_get(swigCPtr);
261                 if (NDalicPINVOKE.SWIGPendingException.Pending) throw NDalicPINVOKE.SWIGPendingException.Retrieve();
262                 return ret;
263             }
264         }
265
266         private string keyPressed
267         {
268             set
269             {
270                 NDalicPINVOKE.Key_keyPressed_set(swigCPtr, value);
271                 if (NDalicPINVOKE.SWIGPendingException.Pending) throw NDalicPINVOKE.SWIGPendingException.Retrieve();
272             }
273             get
274             {
275                 string ret = NDalicPINVOKE.Key_keyPressed_get(swigCPtr);
276                 if (NDalicPINVOKE.SWIGPendingException.Pending) throw NDalicPINVOKE.SWIGPendingException.Retrieve();
277                 return ret;
278             }
279         }
280
281         private int keyCode
282         {
283             set
284             {
285                 NDalicPINVOKE.Key_keyCode_set(swigCPtr, value);
286                 if (NDalicPINVOKE.SWIGPendingException.Pending) throw NDalicPINVOKE.SWIGPendingException.Retrieve();
287             }
288             get
289             {
290                 int ret = NDalicPINVOKE.Key_keyCode_get(swigCPtr);
291                 if (NDalicPINVOKE.SWIGPendingException.Pending) throw NDalicPINVOKE.SWIGPendingException.Retrieve();
292                 return ret;
293             }
294         }
295
296         private int keyModifier
297         {
298             set
299             {
300                 NDalicPINVOKE.Key_keyModifier_set(swigCPtr, value);
301                 if (NDalicPINVOKE.SWIGPendingException.Pending) throw NDalicPINVOKE.SWIGPendingException.Retrieve();
302             }
303             get
304             {
305                 int ret = NDalicPINVOKE.Key_keyModifier_get(swigCPtr);
306                 if (NDalicPINVOKE.SWIGPendingException.Pending) throw NDalicPINVOKE.SWIGPendingException.Retrieve();
307                 return ret;
308             }
309         }
310
311         private uint time
312         {
313             set
314             {
315                 NDalicPINVOKE.Key_time_set(swigCPtr, value);
316                 if (NDalicPINVOKE.SWIGPendingException.Pending) throw NDalicPINVOKE.SWIGPendingException.Retrieve();
317             }
318             get
319             {
320                 uint ret = NDalicPINVOKE.Key_time_get(swigCPtr);
321                 if (NDalicPINVOKE.SWIGPendingException.Pending) throw NDalicPINVOKE.SWIGPendingException.Retrieve();
322                 return ret;
323             }
324         }
325
326         private Key.StateType state
327         {
328             set
329             {
330                 NDalicPINVOKE.Key_state_set(swigCPtr, (int)value);
331                 if (NDalicPINVOKE.SWIGPendingException.Pending) throw NDalicPINVOKE.SWIGPendingException.Retrieve();
332             }
333             get
334             {
335                 Key.StateType ret = (Key.StateType)NDalicPINVOKE.Key_state_get(swigCPtr);
336                 if (NDalicPINVOKE.SWIGPendingException.Pending) throw NDalicPINVOKE.SWIGPendingException.Retrieve();
337                 return ret;
338             }
339         }
340
341         /// <summary>
342         /// Enumeration for specifying the state of the key event.
343         /// </summary>
344         public enum StateType
345         {
346             Down,
347             Up,
348             Last
349         }
350
351     }
352 }