PageRenderTime 375ms CodeModel.GetById 201ms app.highlight 11ms RepoModel.GetById 161ms app.codeStats 0ms

/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/*
 26 * History:
 27 * ================================================================
 28 * 2010-01-30 Li XianJing <xianjimli@hotmail.com> created
 29 *
 30 */
 31
 32#include "ftk_allocator.h"
 33#include "ftk_input_method_manager.h"
 34
 35#ifdef USE_HANDWRITE
 36#include "ftk_input_method_hw.h"
 37#endif
 38
 39#ifdef IPHONE
 40#include "ftk_input_method_iphone.h"
 41#define IME_CREATE ftk_input_method_iphone_create 
 42#elif defined(ANDROID) && defined(ANDROID_NDK)
 43#define IME_CREATE ftk_input_method_android_create 
 44#include "ftk_input_method_android.h"
 45#elif defined(USE_GPINYIN)
 46#include "ftk_input_method_gpinyin.h"
 47#define IME_CREATE ftk_input_method_gpinyin_create 
 48#elif defined(WINCE)
 49#include "ftk_input_method_wince.h"
 50#define IME_CREATE ftk_input_method_wince_create
 51#elif defined(UCOS_SIM) || defined(RT_THREAD)
 52#define IME_CREATE NULL
 53#elif defined(WIN32)
 54#include "ftk_input_method_win32.h"
 55#define IME_CREATE ftk_input_method_win32_create
 56#endif
 57
 58#define FTK_INPUT_METHOD_MAX_NR 6
 59
 60struct _FtkInputMethodManager
 61{
 62	size_t nr;
 63	size_t current;
 64	FtkWidget* widget;
 65	FtkInputMethod* methods[FTK_INPUT_METHOD_MAX_NR+1];
 66};
 67
 68FtkInputMethodManager* ftk_input_method_manager_create(void)
 69{
 70	FtkInputMethodManager* thiz = FTK_NEW(FtkInputMethodManager);
 71	if(thiz != NULL)
 72	{
 73		thiz->current = 0;
 74#if !(defined(UCOS_SIM) || defined(RT_THREAD))
 75#ifdef IME_CREATE
 76		ftk_input_method_manager_register(thiz, IME_CREATE());
 77#endif
 78#endif
 79
 80#ifdef USE_HANDWRITE
 81		ftk_input_method_manager_register(thiz, ftk_input_method_hw_create());
 82#endif
 83
 84	}
 85	return thiz;
 86}
 87
 88size_t  ftk_input_method_manager_count(FtkInputMethodManager* thiz)
 89{
 90	return thiz != NULL ? thiz->nr : 0;
 91}
 92
 93Ret  ftk_input_method_manager_get(FtkInputMethodManager* thiz, size_t index, FtkInputMethod** im)
 94{
 95	return_val_if_fail(thiz != NULL && im != NULL, RET_FAIL);
 96
 97	if(index >= thiz->nr)
 98	{
 99		*im = NULL;
100		return RET_FAIL;
101	}
102
103	*im = thiz->methods[index];
104
105	return RET_OK;
106}
107
108Ret  ftk_input_method_manager_get_current(FtkInputMethodManager* thiz, FtkInputMethod** im)
109{
110	return ftk_input_method_manager_get(thiz, thiz->current, im);
111}
112
113Ret  ftk_input_method_manager_set_current(FtkInputMethodManager* thiz, size_t index)
114{
115	return_val_if_fail(thiz != NULL, RET_FAIL);
116
117	thiz->current = index;
118
119	return RET_OK;
120}
121
122Ret  ftk_input_method_manager_set_current_type(FtkInputMethodManager* thiz, FtkInputType type)
123{
124	return_val_if_fail(thiz != NULL, RET_FAIL);
125
126	if(thiz->current < thiz->nr)
127	{
128		ftk_input_method_set_type(thiz->methods[thiz->current], type);
129	}
130
131	return RET_OK;
132}
133
134Ret  ftk_input_method_manager_register(FtkInputMethodManager* thiz, FtkInputMethod* im)
135{
136	return_val_if_fail(thiz != NULL && im != NULL && thiz->nr < FTK_INPUT_METHOD_MAX_NR, RET_FAIL);
137
138	thiz->methods[thiz->nr] = im;
139	thiz->nr++;
140
141	return RET_OK;
142}
143
144Ret  ftk_input_method_manager_unregister(FtkInputMethodManager* thiz, FtkInputMethod* im)
145{
146	size_t i = 0;
147	return_val_if_fail(thiz != NULL && im != NULL, RET_FAIL);
148
149	for(i = 0; i < thiz->nr; i++)
150	{
151		if(thiz->methods[i] == im)
152		{
153			ftk_input_method_destroy(thiz->methods[i]);
154			thiz->methods[i] = NULL;
155			break;
156		}
157	}
158	
159	for(; i < thiz->nr; i++)
160	{
161		thiz->methods[i] = thiz->methods[i+1];
162	}
163
164	return RET_OK;
165}
166
167void ftk_input_method_manager_destroy(FtkInputMethodManager* thiz)
168{
169	size_t i = 0;
170	if(thiz != NULL)
171	{
172		for(i = 0; i < thiz->nr; i++)
173		{
174			ftk_input_method_destroy(thiz->methods[i]);
175			thiz->methods[i] = NULL;
176		}
177		
178		FTK_ZFREE(thiz, sizeof(FtkInputMethodManager));
179	}
180
181	return;
182}
183
184Ret  ftk_input_method_manager_focus_in(FtkInputMethodManager* thiz, FtkWidget* widget)
185{
186	return_val_if_fail(thiz != NULL, RET_FAIL);
187
188	if(thiz->current < thiz->nr)
189	{
190		if(ftk_input_method_focus_in(thiz->methods[thiz->current], widget) == RET_OK)
191		{
192			thiz->widget = widget;
193		}
194	}
195
196	return RET_OK;
197}
198
199Ret  ftk_input_method_manager_focus_out(FtkInputMethodManager* thiz, FtkWidget* widget)
200{
201	return_val_if_fail(thiz != NULL, RET_FAIL);
202
203	if(thiz->current < thiz->nr && thiz->widget == widget)
204	{
205		thiz->widget = NULL;
206		ftk_input_method_focus_out(thiz->methods[thiz->current]);
207	}
208
209	return RET_OK;
210}
211
212Ret  ftk_input_method_manager_focus_ack_commit(FtkInputMethodManager* thiz)
213{
214	return_val_if_fail(thiz != NULL, RET_FAIL);
215
216	if(thiz->current < thiz->nr)
217	{
218		FtkEvent event;
219		ftk_event_init(&event, FTK_EVT_IM_ACT_COMMIT);
220		ftk_input_method_handle_event(thiz->methods[thiz->current], &event);
221	}
222
223	return RET_OK;
224}