2 * Copyright (c) 2016 Samsung Electronics Co., Ltd All Rights Reserved
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
8 * http://www.apache.org/licenses/LICENSE-2.0
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.
19 using System.Collections.Generic;
20 using System.Runtime.InteropServices;
21 using static Interop.InputMethod;
23 namespace Tizen.Uix.InputMethod
26 /// Enumeration of the key codes.
27 /// If keycode & 0xff000000 == 0x01000000 then this key code is directly encoded 24-bit UCS character.The UCS value is keycode & 0x00ffffff.
28 /// Defines the list of keys supported by the system.Note that certain keys may not be available on all devices.
57 /// The scroll lock key
73 /* Cursor control & motion */
79 /// The left directional key
83 /// The up directional key
87 /// The right directional key
91 /// The down directional key
95 /// The prior, previous key
107 /// The page down key
129 /// The execute, run, do key
153 /// The cancel, stop, abort, exit key
165 /// The character set switch key
167 Mode_switch = 0xFF7E,
175 /// The Numpad space key
179 /// The Numpad tab key
183 /// The Numpad enter key
187 /// The Numpad function 1 key
191 /// The Numpad function 2 key
195 /// The Numpad function 3 key
199 /// The Numpad function 4 key
203 /// The Numpad home key
207 /// The Numpad left key
211 /// The Numpad up key
215 /// The Numpad right key
219 /// The Numpad down key
223 /// The Numpad prior, previous key
227 /// The Numpad page up key
231 /// The Numpad next key
235 /// The Numpad page down key
237 KPPage_Down = 0xFF9B,
239 /// The Numpad end key
243 /// The Numpad begin key
247 /// The Numpad insert key
251 /// The Numpad delete key
255 /// The Numpad equal key
259 /// The Numpad multiply key
263 /// The Numpad add key
267 /// The Numpad separator key
269 KPSeparator = 0xFFAC,
271 /// The Numpad subtract key
275 /// The Numpad decimal key
279 /// The Numpad divide key
323 /* Auxilliary Functions */
325 /// The function 1 key
329 /// The function 2 key
333 /// The function 3 key
337 /// The function 4 key
341 /// The function 5 key
345 /// The function 6 key
349 /// The function 7 key
353 /// The function 8 key
357 /// The function 9 key
361 /// The function 10 key
365 /// The function 11 key
369 /// The function 12 key
373 /// The function 13 key
377 /// The function 14 key
381 /// The function 15 key
385 /// The function 16 key
389 /// The function 17 key
393 /// The function 18 key
397 /// The function 19 key
401 /// The function 20 key
405 /// The function 21 key
409 /// The function 22 key
413 /// The function 23 key
417 /// The function 24 key
421 /// The function 25 key
425 /// The function 26 key
429 /// The function 27 key
433 /// The function 28 key
437 /// The function 29 key
441 /// The function 30 key
445 /// The function 31 key
449 /// The function 32 key
453 /// The function 33 key
457 /// The function 34 key
461 /// The function 35 key
467 /// The left shift key
471 /// The right shift key
475 /// The left control key
479 /// The right control key
483 /// The caps lock key
487 /// The shift lock key
491 /// The left meta key
495 /// The right meta key
503 /// The right alt key
507 /// The left super key
511 /// The right super key
515 /// The left hyper key
519 /// The right hyper key
529 /// The exclamation key
537 /// The number sign key
549 /// The ampersand key
553 /// The apostrophe key
557 /// The parenleft key
561 /// The parenright key
633 /// The semicolon key
761 /// The left bracket key
765 /// The backslash key
769 /// The right bracket key
771 BracketRight = 0x05d,
773 /// The circumflex key
777 /// The underscore key
889 /// The left brace key
897 /// The right brace key
907 /// Enumeration of the key masks.
908 /// The key masks indicate which modifier keys is pressed down during the keyboard hit.The special MASK_RELEASED indicates the key release event.
913 /// Key press event without modifier key
917 /// The Shift key is pressed down
921 /// The CapsLock key is pressed down
925 /// The Control key is pressed down
929 /// The Alt key is pressed down
933 /// The Meta key is pressed down
937 /// The Win (between Control and Alt) is pressed down
941 /// The Hyper key is pressed down
945 /// The NumLock key is pressed down
949 /// Key release event
951 Released = (1 << 15),
955 /// This class contains api's related to IME(Input method editor)
957 public static class InputMethodEditor
959 private static Object thisLock = new Object();
960 private static ImeCallbackStructGCHandle _imeCallbackStructGCHandle = new ImeCallbackStructGCHandle();
961 private static event EventHandler<FocusedInEventArgs> _focusIn;
962 private static ImeFocusedInCb _imeFocusedInDelegate;
963 private static event EventHandler<FocusedOutEventArgs> _focusOut;
964 private static ImeFocusedOutCb _imeFocusedOutDelegate;
965 private static event EventHandler<SurroundingTextUpdatedEventArgs> _surroundingTextUpdated;
966 private static ImeSurroundingTextUpdatedCb _imeSurroundingTextUpdatedDelegate;
967 private static event EventHandler<EventArgs> _inputContextReset;
968 private static ImeInputContextResetCb _imeInputContextResetDelegate;
969 private static event EventHandler<CursorPositionitionUpdatedEventArgs> _cursorPositionUpdated;
970 private static ImeCursorPositionitionUpdatedCb _imeCursorPositionitionUpdatedDelegate;
971 private static event EventHandler<LanguageSetEventArgs> _langaugeSet;
972 private static ImeLanguageSetCb _imeLanguageSetDelegate;
973 private static event EventHandler<SetDataEventArgs> _imDataSet;
974 private static ImeImdataSetCb _imeDataSetDelegate;
975 private static event EventHandler<LayoutSetEventArgs> _layoutSet;
976 private static ImeLayoutSetCb _imeLayoutSetDelegate;
977 private static event EventHandler<ReturnKeySetEventArgs> _returnKeyTypeSet;
978 private static ImeReturnKeySetCb _imeReturnKeySetDelegate;
979 private static event EventHandler<ReturnKeyStateSetEventArgs> _returnKeyStateSet;
980 private static ImeReturnKeyStateSetCb _imeReturnKeyStateSetDelegate;
981 private static ImeProcessKeyEventCb _imeProcessKeyDelegate;
982 private static event EventHandler<DisplayLanaguageChangedEventArgs> _displayLanaguageChanged;
983 private static ImeDisplayLanguageChangedCb _imeDisplayLanaguageChangedDelegate;
984 private static event EventHandler<RotationChangedEventArgs> _rotationDegreeChanged;
985 private static ImeRotationChangedCb _imeRotationChangedDelegate;
986 private static event EventHandler<AccessibilityStateChangedEventArgs> _accessibilityStateChanged;
987 private static ImeAccessibilityStateChangedCb _imeAccessibilityStateChangedDelegate;
988 private static event EventHandler<OptionWindowCreatedEventArgs> _optionWindowCreated;
989 private static ImeOptionWindowCreatedCb _imeOptionWindowCreatedDelegate;
990 private static event EventHandler<OptionWindowDestroyedEventArgs> _optionWindowDestroyed;
991 private static ImeOptionWindowDestroyedCb _imeOptionWindowDestroyedDelegate;
992 private static ImeLanguageRequestedCb _imeLanguageRequestedDelegate;
993 private static OutAction<string> _languageRequestedDelegate;
994 private static BoolAction<KeyCode, KeyMask, VoiceControlDeviceInformation> _processKeyDelagate;
995 private static ImeImdataRequestedCb _imeImDataRequestedDelegate;
996 private static OutArrayAction<byte> _imDataRequestedDelegate;
997 private static ImeGeometryRequestedCb _imeGeometryRequestedDelegate;
998 private static OutAction<Rect> _geometryRequestedDelegate;
999 private static Action _userCreate;
1000 private static Action _userTerminate;
1001 private static Action<ContextId, InputMethodContext> _userShow;
1002 private static Action<ContextId> _userHide;
1003 private static ImeCreateCb _create = (IntPtr userData) =>
1005 Log.Info(LogTag, "In Create Delegate");
1006 _userCreate?.Invoke();
1008 private static ImeTerminateCb _terminate = (IntPtr userData) =>
1010 Log.Info(LogTag, "In terminate Delegate");
1011 _userTerminate?.Invoke();
1012 _imeCallbackStructGCHandle.Dispose();
1014 private static ImeShowCb _show = (int contextId, IntPtr context, IntPtr userData) =>
1016 Log.Info(LogTag, "In Show Delegate");
1017 _userShow?.Invoke(new ContextId(contextId), new InputMethodContext(context));
1019 private static ImeHideCb _hide = (int contextId, IntPtr userData) =>
1021 Log.Info(LogTag, "In Hide Delegate");
1022 _userHide?.Invoke(new ContextId(contextId));
1026 /// Structure representing ContextId
1028 public struct ContextId : IEquatable<ContextId>
1030 internal ContextId(int id)
1041 public bool Equals(ContextId other)
1043 return this.Id == other.Id;
1048 /// rectangle representing the position and size of UI Control
1053 /// The x position in screen
1058 /// The y position in screen
1063 /// The window width
1068 /// The window height
1074 /// An Action with 1 out parameter
1076 /// <typeparam name="T">Generic Type</typeparam>
1077 /// <param name="a">The out parameter</param>
1078 public delegate void OutAction<T>(out T a);
1081 /// An Action with an array out parameter
1083 /// <typeparam name="T">Generic Type</typeparam>
1084 /// <param name="a">The out parameter 1</param>
1085 public delegate void OutArrayAction<T>(out T[] a);
1088 /// An Action with 3 Input Parameter returning a bool
1090 /// <typeparam name="T">Generic Type for Parameter 1</typeparam>
1091 /// <typeparam name="T1">Generic Type for Parameter 2</typeparam>
1092 /// <typeparam name="T2">Generic Type for Parameter 3</typeparam>
1093 /// <param name="a">The Input Parameter 1</param>
1094 /// <param name="b">The Input Parameter 2</param>
1095 /// <param name="c">The Input Parameter 3</param>
1096 /// <returns></returns>
1097 public delegate bool BoolAction<T, T1, T2>(T a, T1 b, T2 c);
1100 /// Called when an associated text input UI control has focus.
1102 public static event EventHandler<FocusedInEventArgs> FocusedIn
1108 _imeFocusedInDelegate = (int contextId, IntPtr userData) =>
1110 FocusedInEventArgs args = new FocusedInEventArgs(contextId);
1111 _focusIn?.Invoke(null, args);
1113 ErrorCode error = ImeEventSetFocusedInCb(_imeFocusedInDelegate, IntPtr.Zero);
1114 if (error != ErrorCode.None)
1116 Log.Error(LogTag, "Add FocusedIn Failed with error " + error);
1134 /// Called when an associated text input UI control loses focus.
1136 public static event EventHandler<FocusedOutEventArgs> FocusedOut
1142 _imeFocusedOutDelegate = (int contextId, IntPtr userData) =>
1144 FocusedOutEventArgs args = new FocusedOutEventArgs(contextId);
1145 _focusOut?.Invoke(null, args);
1147 ErrorCode error = ImeEventSetFocusedOutCb(_imeFocusedOutDelegate, IntPtr.Zero);
1148 if (error != ErrorCode.None)
1150 Log.Error(LogTag, "Add FocusedOut Failed with error " + error);
1168 /// Called when an associated text input UI control responds to a request with the surrounding text.
1170 public static event EventHandler<SurroundingTextUpdatedEventArgs> SurroundingTextUpdated
1176 _imeSurroundingTextUpdatedDelegate = (int contextId, IntPtr text, int cursorPos, IntPtr userData) =>
1178 SurroundingTextUpdatedEventArgs args = new SurroundingTextUpdatedEventArgs(contextId, Marshal.PtrToStringAnsi(text), cursorPos);
1179 _surroundingTextUpdated?.Invoke(null, args);
1181 ErrorCode error = ImeEventSetSurroundingTextUpdatedCb(_imeSurroundingTextUpdatedDelegate, IntPtr.Zero);
1182 if (error != ErrorCode.None)
1184 Log.Error(LogTag, "Add SurroundingTextUpdated Failed with error " + error);
1188 _surroundingTextUpdated += value;
1196 _surroundingTextUpdated -= value;
1202 /// Called to reset the input context of an associated text input UI control.
1204 public static event EventHandler<EventArgs> InputContextReset
1210 _imeInputContextResetDelegate = (IntPtr userData) =>
1212 _inputContextReset?.Invoke(null, EventArgs.Empty);
1214 ErrorCode error = ImeEventSetInputContextResetCb(_imeInputContextResetDelegate, IntPtr.Zero);
1215 if (error != ErrorCode.None)
1217 Log.Error(LogTag, "Add InputContextReset Failed with error " + error);
1221 _inputContextReset += value;
1229 _inputContextReset -= value;
1235 /// Called when the position of the cursor in an associated text input UI control changes.
1237 public static event EventHandler<CursorPositionitionUpdatedEventArgs> CursorPositionitionUpdated
1243 _imeCursorPositionitionUpdatedDelegate = (int cursorPos, IntPtr userData) =>
1245 CursorPositionitionUpdatedEventArgs args = new CursorPositionitionUpdatedEventArgs(cursorPos);
1246 _cursorPositionUpdated?.Invoke(null, args);
1248 ErrorCode error = ImeEventSetCursorPositionitionUpdatedCb(_imeCursorPositionitionUpdatedDelegate, IntPtr.Zero);
1249 if (error != ErrorCode.None)
1251 Log.Error(LogTag, "Add CursorPositionitionUpdated Failed with error " + error);
1255 _cursorPositionUpdated += value;
1263 _cursorPositionUpdated -= value;
1269 /// Called to set the preferred language to the input panel.
1270 /// It will be only called when the client application changes the edit field's language attribute after the input panel is shown.
1272 public static event EventHandler<LanguageSetEventArgs> LanguageSet
1278 _imeLanguageSetDelegate = (InputPanelLanguage language, IntPtr userData) =>
1280 LanguageSetEventArgs args = new LanguageSetEventArgs(language);
1281 _langaugeSet?.Invoke(null, args);
1283 ErrorCode error = ImeEventSetLanguageSetCb(_imeLanguageSetDelegate, IntPtr.Zero);
1284 if (error != ErrorCode.None)
1286 Log.Error(LogTag, "Add LanguageSet Failed with error " + error);
1290 _langaugeSet += value;
1298 _langaugeSet -= value;
1304 /// Called to set the application specific data to deliver to the input panel.
1306 public static event EventHandler<SetDataEventArgs> DataSet
1312 _imeDataSetDelegate = (IntPtr data, uint dataLength, IntPtr userData) =>
1314 byte[] destination = new byte[dataLength];
1315 Marshal.Copy(data, destination, 0, (int)dataLength);
1316 SetDataEventArgs args = new SetDataEventArgs(destination, dataLength);
1317 _imDataSet?.Invoke(null, args);
1319 ErrorCode error = ImeEventSetImdataSetCb(_imeDataSetDelegate, IntPtr.Zero);
1320 if (error != ErrorCode.None)
1322 Log.Error(LogTag, "Add DataSet Failed with error " + error);
1326 _imDataSet += value;
1334 _imDataSet -= value;
1340 /// Called when an associated text input UI control requests the input panel to set its layout.
1341 /// It will be only called when the client application changes the edit field's layout attribute after the input panel is shown.
1343 public static event EventHandler<LayoutSetEventArgs> LayoutSet
1349 _imeLayoutSetDelegate = (InputPanelLayout layout, IntPtr userData) =>
1351 LayoutSetEventArgs args = new LayoutSetEventArgs(layout);
1352 _layoutSet?.Invoke(null, args);
1354 ErrorCode error = ImeEventSetLayoutSetCb(_imeLayoutSetDelegate, IntPtr.Zero);
1355 if (error != ErrorCode.None)
1357 Log.Error(LogTag, "Add LayoutSet Failed with error " + error);
1361 _layoutSet += value;
1369 _layoutSet -= value;
1375 /// Called when an associated text input UI control requests the input panel to set the Return key label.
1376 /// The input panel can show text or image on the Return button according to the Return key action.
1378 public static event EventHandler<ReturnKeySetEventArgs> ReturnKeySet
1384 _imeReturnKeySetDelegate = (InputPanelReturnKey type, IntPtr userData) =>
1386 ReturnKeySetEventArgs args = new ReturnKeySetEventArgs(type);
1387 _returnKeyTypeSet?.Invoke(null, args);
1389 ErrorCode error = ImeEventSetReturnKeySetCb(_imeReturnKeySetDelegate, IntPtr.Zero);
1390 if (error != ErrorCode.None)
1392 Log.Error(LogTag, "Add ReturnKeySet Failed with error " + error);
1396 _returnKeyTypeSet += value;
1404 _returnKeyTypeSet -= value;
1410 /// Called when an associated text input UI control requests the input panel to enable or disable the Return key state.
1412 public static event EventHandler<ReturnKeyStateSetEventArgs> ReturnKeyStateSet
1418 _imeReturnKeyStateSetDelegate = (bool state, IntPtr userData) =>
1420 ReturnKeyStateSetEventArgs args = new ReturnKeyStateSetEventArgs(state);
1421 _returnKeyStateSet?.Invoke(null, args);
1423 ErrorCode error = ImeEventSetReturnKeyStateSetCb(_imeReturnKeyStateSetDelegate, IntPtr.Zero);
1424 if (error != ErrorCode.None)
1426 Log.Error(LogTag, "Add ReturnKeyStateSet Failed with error " + error);
1430 _returnKeyStateSet += value;
1438 _returnKeyStateSet -= value;
1444 /// Called when the system display language is changed.
1446 public static event EventHandler<DisplayLanaguageChangedEventArgs> DisplayLanaguageChanged
1452 _imeDisplayLanaguageChangedDelegate = (IntPtr language, IntPtr userData) =>
1454 DisplayLanaguageChangedEventArgs args = new DisplayLanaguageChangedEventArgs(Marshal.PtrToStringAnsi(language));
1455 _displayLanaguageChanged?.Invoke(null, args);
1457 ErrorCode error = ImeEventSetDisplayLanguageChangedCb(_imeDisplayLanaguageChangedDelegate, IntPtr.Zero);
1458 if (error != ErrorCode.None)
1460 Log.Error(LogTag, "Add DisplayLanaguageChanged Failed with error " + error);
1464 _displayLanaguageChanged += value;
1472 _displayLanaguageChanged -= value;
1478 /// Called when the device is rotated.
1480 public static event EventHandler<RotationChangedEventArgs> RotationChanged
1486 _imeRotationChangedDelegate = (int degree, IntPtr userData) =>
1488 RotationChangedEventArgs args = new RotationChangedEventArgs(degree);
1489 _rotationDegreeChanged?.Invoke(null, args);
1491 ErrorCode error = ImeEventSetRotationChangedCb(_imeRotationChangedDelegate, IntPtr.Zero);
1492 if (error != ErrorCode.None)
1494 Log.Error(LogTag, "Add RotationChanged Failed with error " + error);
1498 _rotationDegreeChanged += value;
1506 _rotationDegreeChanged -= value;
1512 /// Called when Accessibility in Settings application is on or off.
1514 public static event EventHandler<AccessibilityStateChangedEventArgs> AccessibilityStateChanged
1520 _imeAccessibilityStateChangedDelegate = (bool state, IntPtr userData) =>
1522 AccessibilityStateChangedEventArgs args = new AccessibilityStateChangedEventArgs(state);
1523 _accessibilityStateChanged?.Invoke(null, args);
1525 ErrorCode error = ImeEventSetAccessibilityStateChangedCb(_imeAccessibilityStateChangedDelegate, IntPtr.Zero);
1526 if (error != ErrorCode.None)
1528 Log.Error(LogTag, "Add AccessibilityStateChanged Failed with error " + error);
1532 _accessibilityStateChanged += value;
1540 _accessibilityStateChanged -= value;
1546 /// Called to create the option window.
1549 /// if Input panel requests to open the option window, type will be OptionWindowType.Keyboard.
1550 /// And if Settings application requests to open it, type will be OptionWindowType.SettingApplication.
1552 public static event EventHandler<OptionWindowCreatedEventArgs> OptionWindowCreated
1558 _imeOptionWindowCreatedDelegate = (IntPtr window, OptionWindowType type, IntPtr userData) =>
1560 OptionWindow._handle = window;
1561 OptionWindowCreatedEventArgs args = new OptionWindowCreatedEventArgs(new OptionWindow(), type);
1562 _optionWindowCreated?.Invoke(null, args);
1564 ErrorCode error = ImeEventSetOptionWindowCreatedCb(_imeOptionWindowCreatedDelegate, IntPtr.Zero);
1565 if (error != ErrorCode.None)
1567 Log.Error(LogTag, "Add OptionWindowCreated Failed with error " + error);
1571 _optionWindowCreated += value;
1579 _optionWindowCreated -= value;
1585 /// Called to destroy the option window.
1587 public static event EventHandler<OptionWindowDestroyedEventArgs> OptionWindowDestroyed
1593 _imeOptionWindowDestroyedDelegate = (IntPtr window, IntPtr userData) =>
1595 OptionWindow._handle = window;
1596 OptionWindowDestroyedEventArgs args = new OptionWindowDestroyedEventArgs(new OptionWindow());
1597 _optionWindowDestroyed?.Invoke(null, args);
1599 ErrorCode error = ImeEventSetOptionWindowDestroyedCb(_imeOptionWindowDestroyedDelegate, IntPtr.Zero);
1600 if (error != ErrorCode.None)
1602 Log.Error(LogTag, "Add OptionWindowDestroyed Failed with error " + error);
1606 _optionWindowDestroyed += value;
1614 _optionWindowDestroyed -= value;
1620 /// Sets the languageRequested Action
1622 /// <param name="languageRequested">
1623 /// Called when an associated text input UI control requests the language from the input panel, requesting for language code.
1625 public static void SetLanguageRequestedCallback(OutAction<string> languageRequested)
1627 _imeLanguageRequestedDelegate = (IntPtr userData, out IntPtr langCode) =>
1630 _languageRequestedDelegate(out langauage);
1631 char[] languageArray = langauage.ToCharArray();
1632 langCode = new IntPtr();
1633 Marshal.Copy(languageArray, 0, langCode, languageArray.Length);
1635 ErrorCode error = ImeEventSetLanguageRequestedCallbackCb(_imeLanguageRequestedDelegate, IntPtr.Zero);
1636 if (error != ErrorCode.None)
1638 Log.Error(LogTag, "Add SetLanguageRequestedCallback Failed with error " + error);
1640 _languageRequestedDelegate = languageRequested;
1644 /// Sets the processKey Action
1645 /// If the key event is from the external device, DeviceInfo will have its name, class and subclass information.
1647 /// <param name="processKey">
1648 /// The Action is alled when the key event is received from the external devices or SendKey function.
1649 /// This Event processes the key event before an associated text input UI control does.
1651 public static void SetProcessKeyCallback(BoolAction<KeyCode, KeyMask, VoiceControlDeviceInformation> processKey)
1653 _imeProcessKeyDelegate = (KeyCode keyCode, KeyMask keyMask, IntPtr devInfo, IntPtr userData) =>
1655 return _processKeyDelagate(keyCode, keyMask, new VoiceControlDeviceInformation(devInfo));
1657 ErrorCode error = ImeEventSetProcessKeyEventCb(_imeProcessKeyDelegate, IntPtr.Zero);
1658 if (error != ErrorCode.None)
1660 Log.Error(LogTag, "Add ProcessKey Failed with error " + error);
1662 _processKeyDelagate = processKey;
1666 /// Sets the imDataRequested Action
1668 /// <param name="imDataRequested">
1669 /// Called when an associated text input UI control requests the application specific data from the input panel, requesting for data array and it's length.
1671 public static void SetDataRequestedCallback(OutArrayAction<byte> imDataRequested)
1673 _imeImDataRequestedDelegate = (IntPtr userData, out IntPtr data, out uint dataLength) =>
1676 _imDataRequestedDelegate(out dataArr);
1677 data = new IntPtr();
1678 Marshal.Copy(dataArr, 0, data, dataArr.Length);
1679 dataLength = (uint)dataArr.Length;
1681 ErrorCode error = ImeEventSetImdataRequestedCb(_imeImDataRequestedDelegate, IntPtr.Zero);
1682 if (error != ErrorCode.None)
1684 Log.Error(LogTag, "Add SetDataRequestedCallback Failed with error " + error);
1686 _imDataRequestedDelegate = imDataRequested;
1690 /// Sets the GeometryRequested Action
1692 /// <param name="geometryRequested">
1693 /// Called when an associated text input UI control requests the position and size from the input panel, requesting for x,y,w,h values.
1695 public static void SetGeometryRequestedCallback(OutAction<Rect> geometryRequested)
1697 _imeGeometryRequestedDelegate = (IntPtr userData, out int x, out int y, out int w, out int h) =>
1699 Rect rect = new Rect();
1700 _geometryRequestedDelegate(out rect);
1706 ErrorCode error = ImeEventSetGeometryRequestedCallbackCb(_imeGeometryRequestedDelegate, IntPtr.Zero);
1707 if (error != ErrorCode.None)
1709 Log.Error(LogTag, "Add SetGeometryRequestedCallback Failed with error " + error);
1711 _geometryRequestedDelegate = geometryRequested;
1715 /// Runs the main loop of IME application.
1716 /// This function starts to run IME application's main loop.
1719 /// http://tizen.org/privilege/ime
1722 /// This API is a blocking call, as it starts the main loop of the application.
1724 /// <param name="create">This is called to initialize IME application before the main loop starts up</param>
1725 /// <param name="terminate">This is called when IME application is terminated</param>
1726 /// <param name="show">
1727 /// This is called when IME application is shown
1728 /// It provides the Context Inofrmation and the Context Id
1730 /// <param name="hide">
1731 /// This is called when IME application is hidden
1732 /// It provides the Context Id
1734 /// <exception cref="InvalidOperationException">
1735 /// This can occur due to the following reasons:
1736 /// 1) The application does not have the privilege to call this function
1737 /// 2) Operation failed
1739 public static void Run(Action create, Action terminate, Action<ContextId, InputMethodContext> show, Action<ContextId> hide)
1741 _userCreate = create;
1742 _userTerminate = terminate;
1745 _imeCallbackStructGCHandle._imeCallbackStruct.create = _create;
1746 _imeCallbackStructGCHandle._imeCallbackStruct.terminate = _terminate;
1747 _imeCallbackStructGCHandle._imeCallbackStruct.hide = _hide;
1748 _imeCallbackStructGCHandle._imeCallbackStruct.show = _show;
1750 ErrorCode error = ImeRun(ref _imeCallbackStructGCHandle._imeCallbackStruct, IntPtr.Zero);
1751 if (error != ErrorCode.None)
1753 Log.Error(LogTag, "Run Failed with error " + error);
1754 throw InputMethodExceptionFactory.CreateException(error);
1760 /// Sends a key event to the associated text input UI control.
1763 /// This function sends key down or up event with key mask to the client application. If forwardKey is true, this key event goes to the edit filed directly.
1764 /// And if forwardKey is false, the ProcessKey event receives the key event before the edit field.
1767 /// http://tizen.org/privilege/ime
1769 /// <param name="keyCode">The key code to be sent</param>
1770 /// <param name="keyMask">The modifier key mask</param>
1771 /// <param name="forwardKey">The flag to send the key event directly to the edit field</param>
1772 /// <exception cref="InvalidOperationException">
1773 /// This can occur due to the following reasons:
1774 /// 1) The application does not have the privilege to call this function
1775 /// 2) IME main loop isn't started yet
1777 public static void SendKeyEvent(KeyCode keyCode, KeyMask keyMask, bool forwardKey = false)
1779 ErrorCode error = ImeSendKeyEvent(keyCode, keyMask, forwardKey);
1780 if (error != ErrorCode.None)
1782 Log.Error(LogTag, "SendEvent Failed with error " + error);
1783 throw InputMethodExceptionFactory.CreateException(error);
1788 /// Sends the text to the associated text input UI control.
1791 /// http://tizen.org/privilege/ime
1793 /// <param name="str">The string to be committed</param>
1794 /// <exception cref="InvalidOperationException">
1795 /// This can occur due to the following reasons:
1796 /// 1) The application does not have the privilege to call this function
1797 /// 2) IME main loop isn't started yet
1799 public static void CommitString(string str)
1801 ErrorCode error = ImeCommitString(str);
1802 if (error != ErrorCode.None)
1804 Log.Error(LogTag, "CommitString Failed with error " + error);
1805 throw InputMethodExceptionFactory.CreateException(error);
1810 /// Requests to show preedit string.
1813 /// http://tizen.org/privilege/ime
1815 /// <exception cref="InvalidOperationException">
1816 /// This can occur due to the following reasons:
1817 /// 1) The application does not have the privilege to call this function
1818 /// 2) IME main loop isn't started yet
1820 public static void ShowPreEditString()
1822 ErrorCode error = ImeShowPreeditString();
1823 if (error != ErrorCode.None)
1825 Log.Error(LogTag, "ShowPreEditString Failed with error " + error);
1826 throw InputMethodExceptionFactory.CreateException(error);
1831 /// Requests to hide preedit string.
1834 /// http://tizen.org/privilege/ime
1836 /// <exception cref="InvalidOperationException">
1837 /// This can occur due to the following reasons:
1838 /// 1) The application does not have the privilege to call this function
1839 /// 2) IME main loop isn't started yet
1841 public static void HidePreEditString()
1843 ErrorCode error = ImeHidePreeditString();
1844 if (error != ErrorCode.None)
1846 Log.Error(LogTag, "HidePreEditString Failed with error " + error);
1847 throw InputMethodExceptionFactory.CreateException(error);
1852 /// Updates a new preedit string.
1855 /// http://tizen.org/privilege/ime
1857 /// <param name="str">The string to be updated in preedit</param>
1858 /// <param name="attrs">
1859 /// The list which has ime_preedit_attribute lists, strings can be composed of multiple string attributes: underline, highlight color and reversal color.
1860 /// The attrs list can be empty if no attributes to set
1862 /// <exception cref="InvalidOperationException">
1863 /// This can occur due to the following reasons:
1864 /// 1) The application does not have the privilege to call this function
1865 /// 2) IME main loop isn't started yet
1866 /// 3) Invalid Parameter
1868 public static void UpdatePreEditString(string str, IEnumerable<PreEditAttribute> attrs)
1870 IntPtr einaList = IntPtr.Zero;
1871 List<GCHandle> attributeHandleList = new List<GCHandle>();
1872 foreach (PreEditAttribute attribute in attrs)
1874 ImePreEditAttributeStruct imePreEditAttribute = new ImePreEditAttributeStruct();
1875 imePreEditAttribute.start = attribute.Start;
1876 imePreEditAttribute.length = attribute.Length;
1877 imePreEditAttribute.type = (int)attribute.Type;
1878 imePreEditAttribute.value = attribute.Value;
1879 GCHandle attributeHandle = GCHandle.Alloc(imePreEditAttribute, GCHandleType.Pinned);
1880 attributeHandleList.Add(attributeHandle);
1881 einaList = Interop.EinaList.EinaListAppend(einaList, attributeHandle.AddrOfPinnedObject());
1883 ErrorCode error = ImeUpdatePreeditString(str, einaList);
1884 foreach (GCHandle handle in attributeHandleList)
1888 if (error != ErrorCode.None)
1890 Log.Error(LogTag, "UpdatePreEditString Failed with error " + error);
1891 throw InputMethodExceptionFactory.CreateException(error);
1896 /// Requests the surrounding text from the position of the cursor, asynchronously.
1899 /// http://tizen.org/privilege/ime
1901 /// <param name="maxLenBefore">The maximum length of string to be retrieved before the cursor, -1 means unlimited</param>
1902 /// <param name="maxLenAfter">The maximum length of string to be retrieved after the cursor, -1 means unlimited</param>
1903 /// <exception cref="InvalidOperationException">
1904 /// This can occur due to the following reasons:
1905 /// 1) The application does not have the privilege to call this function
1906 /// 2) IME main loop isn't started yet
1909 /// The requested surrounding text can be received using the SurroundingTextUpdated Event, only if it is set.
1910 /// </postcondition>
1911 public static void RequestSurroundingText(int maxLenBefore, int maxLenAfter)
1913 ErrorCode error = ImeRequestSurroundingText(maxLenBefore, maxLenAfter);
1914 if (error != ErrorCode.None)
1916 Log.Error(LogTag, "RequestSurroundingText Failed with error " + error);
1917 throw InputMethodExceptionFactory.CreateException(error);
1922 /// Requests to delete surrounding text.
1925 /// http://tizen.org/privilege/ime
1927 /// <param name="offset">The offset value from the cursor position</param>
1928 /// <param name="len">The length of the text to delete</param>
1929 /// <exception cref="InvalidOperationException">
1930 /// This can occur due to the following reasons:
1931 /// 1) The application does not have the privilege to call this function
1932 /// 2) IME main loop isn't started yet
1933 /// 3) Invalid Parameter
1935 public static void DeleteSurroundingText(int offset, int len)
1937 ErrorCode error = ImeDeleteSurroundingText(offset, len);
1938 if (error != ErrorCode.None)
1940 Log.Error(LogTag, "DeleteSurroundingText Failed with error " + error);
1941 throw InputMethodExceptionFactory.CreateException(error);
1946 /// Gets the surrounding text from the position of the cursor, synchronously.
1949 /// http://tizen.org/privilege/ime
1951 /// <param name="maxLenBefore">The maximum length of string to be retrieved before the cursor, -1 means unlimited</param>
1952 /// <param name="maxLenAfter">The maximum length of string to be retrieved after the cursor, -1 means unlimited</param>
1953 /// <param name="text">The surrounding text</param>
1954 /// <param name="cursorPosition">The cursor position</param>
1955 /// <exception cref="InvalidOperationException">
1956 /// This can occur due to the following reasons:
1957 /// 1) The application does not have the privilege to call this function
1958 /// 2) IME main loop isn't started yet
1959 /// 3) Invalid Parameter
1960 /// 4) Failed to obtain text due to out of memory
1962 public static void GetSurroundingText(int maxLenBefore, int maxLenAfter, out string text, out int cursorPosition)
1965 ErrorCode error = ImeGetSurroundingText(maxLenBefore, maxLenAfter, out txt, out cursorPosition);
1966 if (error != ErrorCode.None)
1968 Log.Error(LogTag, "GetSurroundingText Failed with error " + error);
1969 throw InputMethodExceptionFactory.CreateException(error);
1971 text = Marshal.PtrToStringAnsi(txt);
1975 /// Requests to set selection.
1978 /// http://tizen.org/privilege/ime
1980 /// <param name="start">The start cursor position in text (in characters not bytes)</param>
1981 /// <param name="end">The end cursor position in text (in characters not bytes)</param>
1982 /// <exception cref="InvalidOperationException">
1983 /// This can occur due to the following reasons:
1984 /// 1) The application does not have the privilege to call this function
1985 /// 2) IME main loop isn't started yet
1986 /// 3) Invalid Parameter
1988 public static void SetSelection(int start, int end)
1990 ErrorCode error = ImeSetSelection(start, end);
1991 if (error != ErrorCode.None)
1993 Log.Error(LogTag, "SetSelection Failed with error " + error);
1994 throw InputMethodExceptionFactory.CreateException(error);
1999 /// This API returns the input panel main window.
2002 /// http://tizen.org/privilege/ime
2004 /// <returns>The input panel main window object on success, otherwise null</returns>
2005 /// <exception cref="InvalidOperationException">
2006 /// This can occur due to the following reasons:
2007 /// 1) The application does not have the privilege to call this function
2008 /// 2) IME main loop isn't started yet
2009 /// 3) Operation Failed
2011 public static EditorWindow GetMainWindow()
2013 EditorWindow._handle = ImeGetMainWindow();
2014 EditorWindow obj = new EditorWindow();
2015 ErrorCode error = (ErrorCode)Tizen.Internals.Errors.ErrorFacts.GetLastResult();
2016 if (error != ErrorCode.None)
2018 Log.Error(LogTag, "GetMainWindow Failed with error " + error);
2019 throw InputMethodExceptionFactory.CreateException(error);
2025 /// This API requests the InputMethodEditor to initialize
2028 /// http://tizen.org/privilege/ime
2030 /// <exception cref="InvalidOperationException">
2031 /// This can occur due to the following reasons:
2032 /// 1) The application does not have the privilege to call this function
2033 /// 2) Operation Failed
2035 public static void Create()
2037 ErrorCode error = ImeInitialize();
2038 Log.Info(LogTag, "ImeInitialize result : " + error);
2039 if (error != ErrorCode.None)
2041 Log.Error(LogTag, "ImeInitialize Failed with error " + error);
2042 throw InputMethodExceptionFactory.CreateException(error);
2045 error = ImePrepare();
2046 Log.Info(LogTag, "ImePrepare result : " + error);
2047 if (error != ErrorCode.None)
2049 Log.Error(LogTag, "ImePrepare Failed with error " + error);
2050 throw InputMethodExceptionFactory.CreateException(error);
2055 /// This API requests the InputMethodEditor to finalize
2058 /// http://tizen.org/privilege/ime
2060 /// <exception cref="InvalidOperationException">
2061 /// This can occur due to the following reasons:
2062 /// 1) The application does not have the privilege to call this function
2063 /// 2) Operation Failed
2065 public static void Destroy()
2067 ErrorCode error = ImeFinalize();
2068 Log.Info(LogTag, "ImeFinalize result : " + error);
2069 if (error != ErrorCode.None)
2071 Log.Error(LogTag, "ImeFinalize Failed with error " + error);
2072 throw InputMethodExceptionFactory.CreateException(error);
2077 /// Requests to create an option window from the input panel.
2078 /// The input panel can call this function to open the option window. This function calls OptionWindowCreated Event with OptionWindowType.Keyboard.
2081 /// http://tizen.org/privilege/ime
2083 /// <exception cref="InvalidOperationException">
2084 /// This can occur due to the following reasons:
2085 /// 1) The application does not have the privilege to call this function
2086 /// 2) Operation failed
2087 /// 3) IME main loop isn't started yet
2088 /// 4) OptionWindowCreated event has not been set
2091 /// OptionWindowCreated and OptionWindowDestroyed event should be set
2093 public static void CreateOptionWindow()
2095 ErrorCode error = ImeCreateOptionWindow();
2096 if (error != ErrorCode.None)
2098 Log.Error(LogTag, "CreapteOptionWindow Failed with error " + error);
2099 throw InputMethodExceptionFactory.CreateException(error);
2104 /// Requests to destroy an option window.
2105 /// The input panel can call this function to close the option window which is created from either the input panel or Settings application.
2108 /// http://tizen.org/privilege/ime
2110 /// <param name="window">The option window to destroy</param>
2111 /// <exception cref="InvalidOperationException">
2112 /// This can occur due to the following reasons:
2113 /// 1) The application does not have the privilege to call this function
2114 /// 2) Invalid Parameter
2115 /// 3) IME main loop isn't started yet
2118 /// OptionWindowDestroyed Event must be set.
2121 /// This function triggers the OptionWindowDestroyed Event if it is set.
2122 /// </postcondition>
2123 public static void DestroyOptionWindow(OptionWindow window)
2125 ErrorCode error = ImeDestroyOptionWindow(window);
2126 if (error != ErrorCode.None)
2128 Log.Error(LogTag, "DestroyOptionWindow Failed with error " + error);
2129 throw InputMethodExceptionFactory.CreateException(error);