7a3e362195acbd4c726f6a675892ca1f1ed4d789
[platform/core/csapi/tizenfx.git] / src / Tizen.NUI / src / public / Key.cs
1 /*
2  * Copyright(c) 2017 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.ComponentModel;
19
20 namespace Tizen.NUI
21 {
22
23     /// <summary>
24     /// The key structure is used to store a key press.
25     /// </summary>
26     /// <since_tizen> 3 </since_tizen>
27     public class Key : global::System.IDisposable
28     {
29         /// <summary>
30         /// swigCMemOwn
31         /// </summary>
32         /// <since_tizen> 3 </since_tizen>
33         protected bool swigCMemOwn;
34
35         /// <summary>
36         /// A Flat to check if it is already disposed.
37         /// </summary>
38         /// <since_tizen> 3 </since_tizen>
39         protected bool disposed = false;
40
41         private global::System.Runtime.InteropServices.HandleRef swigCPtr;
42
43         //A Flag to check who called Dispose(). (By User or DisposeQueue)
44         private bool isDisposeQueued = false;
45
46         /// <summary>
47         /// The default constructor.
48         /// </summary>
49         /// <since_tizen> 3 </since_tizen>
50         public Key() : this(Interop.Key.new_Key__SWIG_0(), true)
51         {
52             if (NDalicPINVOKE.SWIGPendingException.Pending) throw NDalicPINVOKE.SWIGPendingException.Retrieve();
53         }
54
55         /// <summary>
56         /// The constructor.
57         /// </summary>
58         /// <param name="keyName">The name of the key pressed or command from the IMF, if later, then the following parameters will be needed.</param>
59         /// <param name="keyString">A string of input characters or key pressed.</param>
60         /// <param name="keyCode">The unique key code for the key pressed.</param>
61         /// <param name="keyModifier">The key modifier for special keys like Shift and Alt.</param>
62         /// <param name="timeStamp">The time (in ms) that the key event occurred.</param>
63         /// <param name="keyState">The state of the key event.</param>
64         internal Key(string keyName, string keyString, int keyCode, int keyModifier, uint timeStamp, Key.StateType keyState) : this(Interop.Key.new_Key__SWIG_1(keyName, keyString, keyCode, keyModifier, timeStamp, (int)keyState), true)
65         {
66             if (NDalicPINVOKE.SWIGPendingException.Pending) throw NDalicPINVOKE.SWIGPendingException.Retrieve();
67         }
68
69         internal Key(global::System.IntPtr cPtr, bool cMemoryOwn)
70         {
71             swigCMemOwn = cMemoryOwn;
72             swigCPtr = new global::System.Runtime.InteropServices.HandleRef(this, cPtr);
73         }
74
75         /// <summary>
76         /// Dispose.
77         /// </summary>
78         /// <since_tizen> 3 </since_tizen>
79         ~Key()
80         {
81             if (!isDisposeQueued)
82             {
83                 isDisposeQueued = true;
84                 DisposeQueue.Instance.Add(this);
85             }
86         }
87
88         /// <summary>
89         /// Enumeration for specifying the state of the key event.
90         /// </summary>
91         /// <since_tizen> 3 </since_tizen>
92         public enum StateType
93         {
94             /// <summary>
95             /// Key Down.
96             /// </summary>
97             /// <since_tizen> 3 </since_tizen>
98             Down,
99             /// <summary>
100             /// Key Up.
101             /// </summary>
102             /// <since_tizen> 3 </since_tizen>
103             Up,
104             /// <summary>
105             /// Key Last.
106             /// </summary>
107             /// <since_tizen> 3 </since_tizen>
108             Last
109         }
110
111         /// <summary>
112         /// Device name
113         /// </summary>
114         /// <since_tizen> 3 </since_tizen>
115         public string DeviceName
116         {
117             get
118             {
119                 string ret = Interop.NDalic.GetDeviceName(swigCPtr);
120                 if (NDalicPINVOKE.SWIGPendingException.Pending) throw NDalicPINVOKE.SWIGPendingException.Retrieve();
121                 return ret;
122             }
123         }
124
125         /// <summary>
126         /// Name given to the key pressed.
127         /// </summary>
128         /// <since_tizen> 3 </since_tizen>
129         public string KeyPressedName
130         {
131             get
132             {
133                 return keyPressedName;
134             }
135             set
136             {
137                 keyPressedName = value;
138             }
139         }
140
141         /// <summary>
142         /// Get the logical key string. (eg. shift + 1 == "exclamation")
143         /// </summary>
144         /// <returns>The logical key symbol</returns>
145         /// This will be public opened in tizen_5.5 after ACR done. Before ACR, need to be hidden as inhouse API.
146         [EditorBrowsable(EditorBrowsableState.Never)]
147         public string LogicalKey
148         {
149             get
150             {
151                 return logicalKey;
152             }
153         }
154
155         /// <summary>
156         /// Get the actual string returned that should be used for input editors.
157         /// </summary>
158         /// <returns>The key string</returns>
159         /// This will be public opened in tizen_5.5 after ACR done. Before ACR, need to be hidden as inhouse API.
160         [EditorBrowsable(EditorBrowsableState.Never)]
161         public string KeyPressed
162         {
163             get
164             {
165                 return keyPressed;
166             }
167         }
168
169         /// <summary>
170         /// Get the actual string returned that should be used for input editors.
171         /// </summary>
172         /// <returns>The key string</returns>
173         /// This will be public opened in tizen_5.5 after ACR done. Before ACR, need to be hidden as inhouse API.
174         [EditorBrowsable(EditorBrowsableState.Never)]
175         public string KeyString
176         {
177             get
178             {
179                 return keyString;
180             }
181             set
182             {
183                 keyString = value;
184             }
185         }
186
187         /// <summary>
188         /// Keycode for the key pressed.
189         /// </summary>
190         /// <since_tizen> 3 </since_tizen>
191         public int KeyCode
192         {
193             get
194             {
195                 return keyCode;
196             }
197             set
198             {
199                 keyCode = value;
200             }
201         }
202
203         /// <summary>
204         /// Special keys like Shift, Alt, and Ctrl which modify the next key pressed.
205         /// </summary>
206         /// <since_tizen> 3 </since_tizen>
207         public int KeyModifier
208         {
209             get
210             {
211                 return keyModifier;
212             }
213             set
214             {
215                 keyModifier = value;
216             }
217         }
218
219         /// <summary>
220         /// The time (in ms) that the key event occurred.
221         /// </summary>
222         /// <since_tizen> 3 </since_tizen>
223         public uint Time
224         {
225             get
226             {
227                 return time;
228             }
229             set
230             {
231                 time = value;
232             }
233         }
234
235         /// <summary>
236         /// State of the key event.
237         /// </summary>
238         /// <since_tizen> 3 </since_tizen>
239         public Key.StateType State
240         {
241             get
242             {
243                 return state;
244             }
245             set
246             {
247                 state = value;
248             }
249         }
250
251         /// <summary>
252         /// Get the device class the key event originated from.
253         /// </summary>
254         /// <since_tizen> 3 </since_tizen>
255         public DeviceClassType DeviceClass
256         {
257             get
258             {
259                 int ret = Interop.NDalic.GetDeviceClass(swigCPtr);
260                 if (NDalicPINVOKE.SWIGPendingException.Pending) throw NDalicPINVOKE.SWIGPendingException.Retrieve();
261                 return (DeviceClassType)ret;
262             }
263         }
264
265         /// <summary>
266         /// Get the device subclass the key event originated from.
267         /// </summary>
268         /// <since_tizen> 4 </since_tizen>
269         public DeviceSubClassType DeviceSubClass
270         {
271             get
272             {
273                 int ret = Interop.NDalic.GetDeviceSubClass(swigCPtr);
274                 if (NDalicPINVOKE.SWIGPendingException.Pending) throw NDalicPINVOKE.SWIGPendingException.Retrieve();
275                 return (DeviceSubClassType)ret;
276             }
277         }
278
279         private string keyPressedName
280         {
281             set
282             {
283                 Interop.Key.Key_keyPressedName_set(swigCPtr, value);
284                 if (NDalicPINVOKE.SWIGPendingException.Pending) throw NDalicPINVOKE.SWIGPendingException.Retrieve();
285             }
286             get
287             {
288                 string ret = Interop.Key.Key_keyPressedName_get(swigCPtr);
289                 if (NDalicPINVOKE.SWIGPendingException.Pending) throw NDalicPINVOKE.SWIGPendingException.Retrieve();
290                 return ret;
291             }
292         }
293
294         private string keyPressed
295         {
296             set
297             {
298                 Interop.Key.Key_keyPressed_set(swigCPtr, value);
299                 if (NDalicPINVOKE.SWIGPendingException.Pending) throw NDalicPINVOKE.SWIGPendingException.Retrieve();
300             }
301             get
302             {
303                 string ret = Interop.Key.Key_keyPressed_get(swigCPtr);
304                 if (NDalicPINVOKE.SWIGPendingException.Pending) throw NDalicPINVOKE.SWIGPendingException.Retrieve();
305                 return ret;
306             }
307         }
308
309         private string keyString
310         {
311             set
312             {
313                 Interop.Key.Key_keyString_set(swigCPtr, value);
314                 if (NDalicPINVOKE.SWIGPendingException.Pending) throw NDalicPINVOKE.SWIGPendingException.Retrieve();
315             }
316             get
317             {
318                 string ret = Interop.Key.Key_keyString_get(swigCPtr);
319                 if (NDalicPINVOKE.SWIGPendingException.Pending) throw NDalicPINVOKE.SWIGPendingException.Retrieve();
320                 return ret;
321             }
322         }
323
324         private int keyCode
325         {
326             set
327             {
328                 Interop.Key.Key_keyCode_set(swigCPtr, value);
329                 if (NDalicPINVOKE.SWIGPendingException.Pending) throw NDalicPINVOKE.SWIGPendingException.Retrieve();
330             }
331             get
332             {
333                 int ret = Interop.Key.Key_keyCode_get(swigCPtr);
334                 if (NDalicPINVOKE.SWIGPendingException.Pending) throw NDalicPINVOKE.SWIGPendingException.Retrieve();
335                 return ret;
336             }
337         }
338
339         private int keyModifier
340         {
341             set
342             {
343                 Interop.Key.Key_keyModifier_set(swigCPtr, value);
344                 if (NDalicPINVOKE.SWIGPendingException.Pending) throw NDalicPINVOKE.SWIGPendingException.Retrieve();
345             }
346             get
347             {
348                 int ret = Interop.Key.Key_keyModifier_get(swigCPtr);
349                 if (NDalicPINVOKE.SWIGPendingException.Pending) throw NDalicPINVOKE.SWIGPendingException.Retrieve();
350                 return ret;
351             }
352         }
353
354         private uint time
355         {
356             set
357             {
358                 Interop.Key.Key_time_set(swigCPtr, value);
359                 if (NDalicPINVOKE.SWIGPendingException.Pending) throw NDalicPINVOKE.SWIGPendingException.Retrieve();
360             }
361             get
362             {
363                 uint ret = Interop.Key.Key_time_get(swigCPtr);
364                 if (NDalicPINVOKE.SWIGPendingException.Pending) throw NDalicPINVOKE.SWIGPendingException.Retrieve();
365                 return ret;
366             }
367         }
368
369         private Key.StateType state
370         {
371             set
372             {
373                 Interop.Key.Key_state_set(swigCPtr, (int)value);
374                 if (NDalicPINVOKE.SWIGPendingException.Pending) throw NDalicPINVOKE.SWIGPendingException.Retrieve();
375             }
376             get
377             {
378                 Key.StateType ret = (Key.StateType)Interop.Key.Key_state_get(swigCPtr);
379                 if (NDalicPINVOKE.SWIGPendingException.Pending) throw NDalicPINVOKE.SWIGPendingException.Retrieve();
380                 return ret;
381             }
382         }
383
384         private string logicalKey
385         {
386             get
387             {
388                 string ret = Interop.Key.Key_logicalKey_get(swigCPtr);
389                 if (NDalicPINVOKE.SWIGPendingException.Pending) throw NDalicPINVOKE.SWIGPendingException.Retrieve();
390                 return "";
391             }
392         }
393
394         /// <summary>
395         /// Dispose.
396         /// </summary>
397         /// <since_tizen> 3 </since_tizen>
398         public void Dispose()
399         {
400             //Throw excpetion if Dispose() is called in separate thread.
401             if (!Window.IsInstalled())
402             {
403                 throw new System.InvalidOperationException("This API called from separate thread. This API must be called from MainThread.");
404             }
405
406             if (isDisposeQueued)
407             {
408                 Dispose(DisposeTypes.Implicit);
409             }
410             else
411             {
412                 Dispose(DisposeTypes.Explicit);
413                 System.GC.SuppressFinalize(this);
414             }
415         }
416
417         /// <summary>
418         /// Checks to see if the Shift key modifier has been supplied.
419         /// </summary>
420         /// <returns>True if Shift modifier.</returns>
421         /// <since_tizen> 3 </since_tizen>
422         public bool IsShiftModifier()
423         {
424             bool ret = Interop.Key.Key_IsShiftModifier(swigCPtr);
425             if (NDalicPINVOKE.SWIGPendingException.Pending) throw NDalicPINVOKE.SWIGPendingException.Retrieve();
426             return ret;
427         }
428
429         /// <summary>
430         /// Checks to see if Ctrl (control) key modifier has been supplied.
431         /// </summary>
432         /// <returns>True if Ctrl modifier.</returns>
433         /// <since_tizen> 3 </since_tizen>
434         public bool IsCtrlModifier()
435         {
436             bool ret = Interop.Key.Key_IsCtrlModifier(swigCPtr);
437             if (NDalicPINVOKE.SWIGPendingException.Pending) throw NDalicPINVOKE.SWIGPendingException.Retrieve();
438             return ret;
439         }
440
441         /// <summary>
442         /// Checks to see if Alt key modifier has been supplied.
443         /// </summary>
444         /// <returns>True if Alt modifier.</returns>
445         /// <since_tizen> 3 </since_tizen>
446         public bool IsAltModifier()
447         {
448             bool ret = Interop.Key.Key_IsAltModifier(swigCPtr);
449             if (NDalicPINVOKE.SWIGPendingException.Pending) throw NDalicPINVOKE.SWIGPendingException.Retrieve();
450             return ret;
451         }
452
453         internal static global::System.Runtime.InteropServices.HandleRef getCPtr(Key obj)
454         {
455             return (obj == null) ? new global::System.Runtime.InteropServices.HandleRef(null, global::System.IntPtr.Zero) : obj.swigCPtr;
456         }
457
458         internal static Key GetKeyFromPtr(global::System.IntPtr cPtr)
459         {
460             Key ret = new Key(cPtr, false);
461             if (NDalicPINVOKE.SWIGPendingException.Pending) throw NDalicPINVOKE.SWIGPendingException.Retrieve();
462             return ret;
463         }
464
465         /// <summary>
466         /// Dispose.
467         /// </summary>
468         /// <param name="type">The dispose type.</param>
469         /// <since_tizen> 3 </since_tizen>
470         protected virtual void Dispose(DisposeTypes type)
471         {
472             if (disposed)
473             {
474                 return;
475             }
476
477             if(type == DisposeTypes.Explicit)
478             {
479                 //Called by User
480                 //Release your own managed resources here.
481                 //You should release all of your own disposable objects here.
482             }
483
484             //Release your own unmanaged resources here.
485             //You should not access any managed member here except static instance.
486             //because the execution order of Finalizes is non-deterministic.
487
488             if (swigCPtr.Handle != global::System.IntPtr.Zero)
489             {
490                 if (swigCMemOwn)
491                 {
492                     swigCMemOwn = false;
493                     Interop.Key.delete_Key(swigCPtr);
494                 }
495                 swigCPtr = new global::System.Runtime.InteropServices.HandleRef(null, global::System.IntPtr.Zero);
496             }
497             disposed = true;
498         }
499     }
500 }