PageRenderTime 431ms CodeModel.GetById 168ms app.highlight 10ms RepoModel.GetById 251ms app.codeStats 0ms

/src/os/android/ftk_input_method_android.c

http://ftk.googlecode.com/
C | 148 lines | 113 code | 34 blank | 1 comment | 14 complexity | a0ca2d03214dc1c9d6a75b5f0025f4fa MD5 | raw file
  1
  2#include "ftk_jni.h"
  3#include "ftk_log.h"
  4#include "ftk_event.h"
  5#include "ftk_globals.h"
  6#include "ftk_input_method_android.h"
  7
  8typedef struct _PrivInfo
  9{
 10	FtkWidget* editor;
 11	FtkInputType input_type;
 12}PrivInfo;
 13
 14static Ret  ftk_input_method_android_handle_event(FtkInputMethod* thiz, FtkEvent* event);
 15
 16static Ret  ftk_input_method_android_init(FtkInputMethod* thiz)
 17{
 18	return RET_OK;
 19}
 20
 21static Ret  ftk_input_method_android_reset(FtkInputMethod* thiz)
 22{
 23	return RET_OK;
 24}
 25
 26static Ret  ftk_input_method_android_activate(FtkInputMethod* thiz)
 27{
 28	ftk_input_method_android_init(thiz);
 29
 30	ftk_wnd_manager_add_global_listener(ftk_default_wnd_manager(),
 31		(FtkListener)ftk_input_method_android_handle_event, thiz);
 32
 33	ftk_logd("%s\n", __func__);
 34
 35	return RET_OK;
 36}
 37
 38static Ret  ftk_input_method_android_deactivate(FtkInputMethod* thiz)
 39{
 40	ftk_input_method_android_reset(thiz);
 41	
 42	ftk_wnd_manager_remove_global_listener(ftk_default_wnd_manager(), 
 43		(FtkListener)ftk_input_method_android_handle_event, thiz);
 44
 45	ftk_logd("%s\n", __func__);
 46
 47	return RET_OK;
 48}
 49
 50static Ret  ftk_input_method_android_focus_in(FtkInputMethod* thiz, FtkWidget* editor)
 51{
 52	DECL_PRIV(thiz, priv);
 53	return_val_if_fail(priv != NULL && editor != NULL, RET_FAIL);
 54
 55	priv->editor = editor;
 56
 57	Android_ShowKeyboard();
 58
 59	return ftk_input_method_android_activate(thiz);
 60}
 61
 62static Ret  ftk_input_method_android_focus_out(FtkInputMethod* thiz)
 63{
 64	DECL_PRIV(thiz, priv);
 65	return_val_if_fail(priv != NULL, RET_FAIL);
 66
 67	Android_HideKeyboard();
 68
 69	priv->editor = NULL;
 70	
 71	return ftk_input_method_android_deactivate(thiz);
 72}
 73
 74static Ret  ftk_input_method_android_set_type(FtkInputMethod* thiz, FtkInputType input_type)
 75{
 76	DECL_PRIV(thiz, priv);
 77	return_val_if_fail(priv != NULL, RET_FAIL);
 78
 79	priv->input_type = input_type;
 80
 81	return RET_OK;
 82}
 83
 84static Ret  ftk_input_method_android_handle_event(FtkInputMethod* thiz, FtkEvent* event)
 85{
 86	Ret ret = RET_OK;
 87	DECL_PRIV(thiz, priv);
 88	return_val_if_fail(priv != NULL && event != NULL, RET_FAIL);
 89
 90	switch(event->type)
 91	{
 92		case FTK_EVT_OS_IM_COMMIT:
 93		{
 94			FtkEvent evt = {0};
 95			if(priv->editor == NULL)
 96			{
 97				FTK_FREE(event->u.extra);
 98				ret = RET_REMOVE;
 99				break;
100			}
101
102			/* TODO: priv->input_type */
103
104			evt.type = FTK_EVT_IM_COMMIT;
105			evt.u.extra = event->u.extra;
106			evt.widget = priv->editor;
107			ftk_widget_event(priv->editor, &evt);
108
109			FTK_FREE(event->u.extra);
110			ret = RET_REMOVE;
111			break;
112		}
113		case FTK_EVT_IM_ACT_COMMIT:
114		{
115			break;
116		}
117		default:break;
118	}
119
120	return ret;
121}
122
123static void ftk_input_method_android_destroy(FtkInputMethod* thiz)
124{
125	if(thiz != NULL)
126	{
127		DECL_PRIV(thiz, priv);
128		FTK_ZFREE(thiz, sizeof(FtkInputMethod) + sizeof(PrivInfo));
129	}
130}
131
132FtkInputMethod* ftk_input_method_android_create(void)
133{
134	FtkInputMethod* thiz = FTK_ZALLOC(sizeof(FtkInputMethod) + sizeof(PrivInfo));
135
136	if(thiz != NULL)
137	{
138		thiz->ref  = 1;
139		thiz->name = "android ime";
140		thiz->set_type     = ftk_input_method_android_set_type;
141		thiz->focus_in     = ftk_input_method_android_focus_in;
142		thiz->focus_out    = ftk_input_method_android_focus_out;
143		thiz->handle_event = ftk_input_method_android_handle_event;
144		thiz->destroy      = ftk_input_method_android_destroy;
145	}
146
147	return thiz;
148}