/src/os/iphone/ftk_input_method_iphone.c

http://ftk.googlecode.com/ · C · 151 lines · 114 code · 36 blank · 1 comment · 15 complexity · 765df34fc29fb1d29c6ba8f40a68d3cb MD5 · raw file

  1. #include "ftk_log.h"
  2. #include "ftk_event.h"
  3. #include "ftk_globals.h"
  4. #include "ftk_input_method_iphone.h"
  5. typedef struct _PrivInfo
  6. {
  7. FtkWidget* editor;
  8. FtkInputType input_type;
  9. }PrivInfo;
  10. static Ret ftk_input_method_iphone_handle_event(FtkInputMethod* thiz, FtkEvent* event);
  11. static Ret ftk_input_method_iphone_init(FtkInputMethod* thiz)
  12. {
  13. return RET_OK;
  14. }
  15. static Ret ftk_input_method_iphone_reset(FtkInputMethod* thiz)
  16. {
  17. return RET_OK;
  18. }
  19. static Ret ftk_input_method_iphone_activate(FtkInputMethod* thiz)
  20. {
  21. ftk_input_method_iphone_init(thiz);
  22. ftk_wnd_manager_add_global_listener(ftk_default_wnd_manager(),
  23. (FtkListener)ftk_input_method_iphone_handle_event, thiz);
  24. ftk_logd("%s\n", __func__);
  25. return RET_OK;
  26. }
  27. static Ret ftk_input_method_iphone_deactivate(FtkInputMethod* thiz)
  28. {
  29. ftk_input_method_iphone_reset(thiz);
  30. ftk_wnd_manager_remove_global_listener(ftk_default_wnd_manager(),
  31. (FtkListener)ftk_input_method_iphone_handle_event, thiz);
  32. ftk_logd("%s\n", __func__);
  33. return RET_OK;
  34. }
  35. static Ret ftk_input_method_iphone_focus_in(FtkInputMethod* thiz, FtkWidget* editor)
  36. {
  37. DECL_PRIV(thiz, priv);
  38. return_val_if_fail(priv != NULL && editor != NULL, RET_FAIL);
  39. ftk_logd("%s:%d ftk_input_method_iphone_focus_in\n", __FILE__, __LINE__);
  40. priv->editor = editor;
  41. ftk_display_iphone_show_keyboard(ftk_display_get_real_display(ftk_default_display()));
  42. return ftk_input_method_iphone_activate(thiz);
  43. }
  44. static Ret ftk_input_method_iphone_focus_out(FtkInputMethod* thiz)
  45. {
  46. DECL_PRIV(thiz, priv);
  47. return_val_if_fail(priv != NULL, RET_FAIL);
  48. ftk_logd("%s:%d ftk_input_method_iphone_focus_out\n", __FILE__, __LINE__);
  49. ftk_display_iphone_hide_keyboard(ftk_display_get_real_display(ftk_default_display()));
  50. priv->editor = NULL;
  51. return ftk_input_method_iphone_deactivate(thiz);
  52. }
  53. static Ret ftk_input_method_iphone_set_type(FtkInputMethod* thiz, FtkInputType input_type)
  54. {
  55. DECL_PRIV(thiz, priv);
  56. return_val_if_fail(priv != NULL, RET_FAIL);
  57. priv->input_type = input_type;
  58. return RET_OK;
  59. }
  60. static Ret ftk_input_method_iphone_handle_event(FtkInputMethod* thiz, FtkEvent* event)
  61. {
  62. Ret ret = RET_OK;
  63. DECL_PRIV(thiz, priv);
  64. return_val_if_fail(priv != NULL && event != NULL, RET_FAIL);
  65. switch(event->type)
  66. {
  67. case FTK_EVT_OS_IM_COMMIT:
  68. {
  69. FtkEvent evt = {0};
  70. if(priv->editor == NULL)
  71. {
  72. FTK_FREE(event->u.extra);
  73. ret = RET_REMOVE;
  74. break;
  75. }
  76. /* TODO: priv->input_type */
  77. evt.type = FTK_EVT_IM_COMMIT;
  78. evt.u.extra = event->u.extra;
  79. evt.widget = priv->editor;
  80. ftk_widget_event(priv->editor, &evt);
  81. FTK_FREE(event->u.extra);
  82. ret = RET_REMOVE;
  83. break;
  84. }
  85. case FTK_EVT_IM_ACT_COMMIT:
  86. {
  87. break;
  88. }
  89. default:break;
  90. }
  91. return ret;
  92. }
  93. static void ftk_input_method_iphone_destroy(FtkInputMethod* thiz)
  94. {
  95. if(thiz != NULL)
  96. {
  97. DECL_PRIV(thiz, priv);
  98. FTK_ZFREE(thiz, sizeof(FtkInputMethod) + sizeof(PrivInfo));
  99. }
  100. }
  101. FtkInputMethod* ftk_input_method_iphone_create(void)
  102. {
  103. FtkInputMethod* thiz = FTK_ZALLOC(sizeof(FtkInputMethod) + sizeof(PrivInfo));
  104. if(thiz != NULL)
  105. {
  106. thiz->ref = 1;
  107. thiz->name = "iphone ime";
  108. thiz->set_type = ftk_input_method_iphone_set_type;
  109. thiz->focus_in = ftk_input_method_iphone_focus_in;
  110. thiz->focus_out = ftk_input_method_iphone_focus_out;
  111. thiz->handle_event = ftk_input_method_iphone_handle_event;
  112. thiz->destroy = ftk_input_method_iphone_destroy;
  113. }
  114. return thiz;
  115. }