/src/ftk_input_method_manager.c

http://ftk.googlecode.com/ · C · 224 lines · 155 code · 40 blank · 29 comment · 37 complexity · 379e9fac15a607793627df8aca8b7529 MD5 · raw file

  1. /*
  2. * File: ftk_input_method_manager.c
  3. * Author: Li XianJing <xianjimli@hotmail.com>
  4. * Brief: input method manager.
  5. *
  6. * Copyright (c) 2009 - 2010 Li XianJing <xianjimli@hotmail.com>
  7. *
  8. * Licensed under the Academic Free License version 2.1
  9. *
  10. * This program is free software; you can redistribute it and/or modify
  11. * it under the terms of the GNU General Public License as published by
  12. * the Free Software Foundation; either version 2 of the License, or
  13. * (at your option) any later version.
  14. *
  15. * This program is distributed in the hope that it will be useful,
  16. * but WITHOUT ANY WARRANTY; without even the implied warranty of
  17. * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
  18. * GNU General Public License for more details.
  19. *
  20. * You should have received a copy of the GNU General Public License
  21. * along with this program; if not, write to the Free Software
  22. * Foundation, Inc., 59 Temple Place, Suite 330, Boston, MA 02111-1307 USA
  23. */
  24. /*
  25. * History:
  26. * ================================================================
  27. * 2010-01-30 Li XianJing <xianjimli@hotmail.com> created
  28. *
  29. */
  30. #include "ftk_allocator.h"
  31. #include "ftk_input_method_manager.h"
  32. #ifdef USE_HANDWRITE
  33. #include "ftk_input_method_hw.h"
  34. #endif
  35. #ifdef IPHONE
  36. #include "ftk_input_method_iphone.h"
  37. #define IME_CREATE ftk_input_method_iphone_create
  38. #elif defined(ANDROID) && defined(ANDROID_NDK)
  39. #define IME_CREATE ftk_input_method_android_create
  40. #include "ftk_input_method_android.h"
  41. #elif defined(USE_GPINYIN)
  42. #include "ftk_input_method_gpinyin.h"
  43. #define IME_CREATE ftk_input_method_gpinyin_create
  44. #elif defined(WINCE)
  45. #include "ftk_input_method_wince.h"
  46. #define IME_CREATE ftk_input_method_wince_create
  47. #elif defined(UCOS_SIM) || defined(RT_THREAD)
  48. #define IME_CREATE NULL
  49. #elif defined(WIN32)
  50. #include "ftk_input_method_win32.h"
  51. #define IME_CREATE ftk_input_method_win32_create
  52. #endif
  53. #define FTK_INPUT_METHOD_MAX_NR 6
  54. struct _FtkInputMethodManager
  55. {
  56. size_t nr;
  57. size_t current;
  58. FtkWidget* widget;
  59. FtkInputMethod* methods[FTK_INPUT_METHOD_MAX_NR+1];
  60. };
  61. FtkInputMethodManager* ftk_input_method_manager_create(void)
  62. {
  63. FtkInputMethodManager* thiz = FTK_NEW(FtkInputMethodManager);
  64. if(thiz != NULL)
  65. {
  66. thiz->current = 0;
  67. #if !(defined(UCOS_SIM) || defined(RT_THREAD))
  68. #ifdef IME_CREATE
  69. ftk_input_method_manager_register(thiz, IME_CREATE());
  70. #endif
  71. #endif
  72. #ifdef USE_HANDWRITE
  73. ftk_input_method_manager_register(thiz, ftk_input_method_hw_create());
  74. #endif
  75. }
  76. return thiz;
  77. }
  78. size_t ftk_input_method_manager_count(FtkInputMethodManager* thiz)
  79. {
  80. return thiz != NULL ? thiz->nr : 0;
  81. }
  82. Ret ftk_input_method_manager_get(FtkInputMethodManager* thiz, size_t index, FtkInputMethod** im)
  83. {
  84. return_val_if_fail(thiz != NULL && im != NULL, RET_FAIL);
  85. if(index >= thiz->nr)
  86. {
  87. *im = NULL;
  88. return RET_FAIL;
  89. }
  90. *im = thiz->methods[index];
  91. return RET_OK;
  92. }
  93. Ret ftk_input_method_manager_get_current(FtkInputMethodManager* thiz, FtkInputMethod** im)
  94. {
  95. return ftk_input_method_manager_get(thiz, thiz->current, im);
  96. }
  97. Ret ftk_input_method_manager_set_current(FtkInputMethodManager* thiz, size_t index)
  98. {
  99. return_val_if_fail(thiz != NULL, RET_FAIL);
  100. thiz->current = index;
  101. return RET_OK;
  102. }
  103. Ret ftk_input_method_manager_set_current_type(FtkInputMethodManager* thiz, FtkInputType type)
  104. {
  105. return_val_if_fail(thiz != NULL, RET_FAIL);
  106. if(thiz->current < thiz->nr)
  107. {
  108. ftk_input_method_set_type(thiz->methods[thiz->current], type);
  109. }
  110. return RET_OK;
  111. }
  112. Ret ftk_input_method_manager_register(FtkInputMethodManager* thiz, FtkInputMethod* im)
  113. {
  114. return_val_if_fail(thiz != NULL && im != NULL && thiz->nr < FTK_INPUT_METHOD_MAX_NR, RET_FAIL);
  115. thiz->methods[thiz->nr] = im;
  116. thiz->nr++;
  117. return RET_OK;
  118. }
  119. Ret ftk_input_method_manager_unregister(FtkInputMethodManager* thiz, FtkInputMethod* im)
  120. {
  121. size_t i = 0;
  122. return_val_if_fail(thiz != NULL && im != NULL, RET_FAIL);
  123. for(i = 0; i < thiz->nr; i++)
  124. {
  125. if(thiz->methods[i] == im)
  126. {
  127. ftk_input_method_destroy(thiz->methods[i]);
  128. thiz->methods[i] = NULL;
  129. break;
  130. }
  131. }
  132. for(; i < thiz->nr; i++)
  133. {
  134. thiz->methods[i] = thiz->methods[i+1];
  135. }
  136. return RET_OK;
  137. }
  138. void ftk_input_method_manager_destroy(FtkInputMethodManager* thiz)
  139. {
  140. size_t i = 0;
  141. if(thiz != NULL)
  142. {
  143. for(i = 0; i < thiz->nr; i++)
  144. {
  145. ftk_input_method_destroy(thiz->methods[i]);
  146. thiz->methods[i] = NULL;
  147. }
  148. FTK_ZFREE(thiz, sizeof(FtkInputMethodManager));
  149. }
  150. return;
  151. }
  152. Ret ftk_input_method_manager_focus_in(FtkInputMethodManager* thiz, FtkWidget* widget)
  153. {
  154. return_val_if_fail(thiz != NULL, RET_FAIL);
  155. if(thiz->current < thiz->nr)
  156. {
  157. if(ftk_input_method_focus_in(thiz->methods[thiz->current], widget) == RET_OK)
  158. {
  159. thiz->widget = widget;
  160. }
  161. }
  162. return RET_OK;
  163. }
  164. Ret ftk_input_method_manager_focus_out(FtkInputMethodManager* thiz, FtkWidget* widget)
  165. {
  166. return_val_if_fail(thiz != NULL, RET_FAIL);
  167. if(thiz->current < thiz->nr && thiz->widget == widget)
  168. {
  169. thiz->widget = NULL;
  170. ftk_input_method_focus_out(thiz->methods[thiz->current]);
  171. }
  172. return RET_OK;
  173. }
  174. Ret ftk_input_method_manager_focus_ack_commit(FtkInputMethodManager* thiz)
  175. {
  176. return_val_if_fail(thiz != NULL, RET_FAIL);
  177. if(thiz->current < thiz->nr)
  178. {
  179. FtkEvent event;
  180. ftk_event_init(&event, FTK_EVT_IM_ACT_COMMIT);
  181. ftk_input_method_handle_event(thiz->methods[thiz->current], &event);
  182. }
  183. return RET_OK;
  184. }