PageRenderTime 357ms CodeModel.GetById 141ms app.highlight 95ms RepoModel.GetById 116ms app.codeStats 0ms

/src/backend/wince/ftk_display_wince_gdi.c

http://ftk.googlecode.com/
C | 404 lines | 351 code | 46 blank | 7 comment | 31 complexity | c76d0c4bc100f09a3b59be9f960a3bb1 MD5 | raw file
  1
  2#include "ftk_typedef.h"
  3#include "ftk.h"
  4#include "ftk_log.h"
  5#include "ftk_bitmap.h"
  6#include "ftk_event.h"
  7#include "ftk_globals.h"
  8#include "ftk_display_wince.h"
  9#include "ftk_input_method_manager.h"
 10#include "ftk_input_method_wince.h"
 11#include <aygshell.h>
 12
 13typedef struct _PrivInfo
 14{
 15	HWND wnd;
 16	HBITMAP hBitmap;
 17	int width;
 18	int height;
 19	void* bits;
 20	FtkEvent event;
 21}PrivInfo;
 22
 23static int s_key_map[0xff] = {0};
 24static wchar_t szClassName[] = L"FtkEmulator";
 25
 26static void ftk_display_wince_set_fullscreen(PrivInfo* priv)
 27{
 28	SHFullScreen(priv->wnd, SHFS_HIDESIPBUTTON|SHFS_HIDETASKBAR|SHFS_HIDESTARTICON);
 29	SetWindowPos(priv->wnd, HWND_TOPMOST, 0, 0, priv->width, priv->height, SWP_NOOWNERZORDER|SWP_SHOWWINDOW);
 30}
 31
 32static void ftk_display_wince_keymap_init(void)
 33{
 34	s_key_map[0xBD] = FTK_KEY_MINUS;
 35	s_key_map[0x20] = FTK_KEY_SPACE;
 36	s_key_map[0xBB] = FTK_KEY_EQUAL;
 37	s_key_map[0x08] = FTK_KEY_BACKSPACE;
 38	s_key_map[0x09] = FTK_KEY_TAB;
 39	s_key_map[0xC0] = FTK_KEY_QUOTELEFT;
 40	s_key_map[0xDE] = FTK_KEY_QUOTERIGHT;
 41	s_key_map[0xDB] = FTK_KEY_BRACKETLEFT;
 42	s_key_map[0xDD] = FTK_KEY_BRACKETRIGHT;
 43	s_key_map[0x0d] = FTK_KEY_ENTER;
 44	s_key_map[0xBA] = FTK_KEY_SEMICOLON;
 45	s_key_map[0x30] = FTK_KEY_0;
 46	s_key_map[0x31] = FTK_KEY_1; 
 47	s_key_map[0x32] = FTK_KEY_2;
 48	s_key_map[0x33] = FTK_KEY_3;
 49	s_key_map[0x34] = FTK_KEY_4;
 50	s_key_map[0x35] = FTK_KEY_5;
 51	s_key_map[0x36] = FTK_KEY_6;
 52	s_key_map[0x37] = FTK_KEY_7;
 53	s_key_map[0x38] = FTK_KEY_8;
 54	s_key_map[0x39] = FTK_KEY_9;
 55	s_key_map[0x70] = FTK_KEY_F1; 
 56	s_key_map[0x71] = FTK_KEY_F2;
 57	s_key_map[0x72] = FTK_KEY_F3;
 58	s_key_map[0x73] = FTK_KEY_F4;
 59	s_key_map[0x74] = FTK_KEY_F5;
 60	s_key_map[0x75] = FTK_KEY_F6;
 61	s_key_map[0x76] = FTK_KEY_F7;
 62	s_key_map[0x77] = FTK_KEY_F8;
 63	s_key_map[0x78] = FTK_KEY_F9;
 64	s_key_map[0xBC] = FTK_KEY_COMMA;
 65	s_key_map[0xBE] = FTK_KEY_DOT;
 66	s_key_map[0xBF] = FTK_KEY_SLASH;
 67	s_key_map[0x10] = FTK_KEY_RIGHTSHIFT;
 68	s_key_map[0x11] = FTK_KEY_LEFTCTRL;
 69	s_key_map[0x14] = FTK_KEY_CAPSLOCK;
 70	s_key_map[0x40] = FTK_KEY_LEFTALT;
 71	s_key_map[0x41] = FTK_KEY_a;
 72	s_key_map[0x42] = FTK_KEY_b;
 73	s_key_map[0x43] = FTK_KEY_c;
 74	s_key_map[0x44] = FTK_KEY_d;
 75	s_key_map[0x45] = FTK_KEY_e;
 76	s_key_map[0x46] = FTK_KEY_f;
 77	s_key_map[0x47] = FTK_KEY_g;
 78	s_key_map[0x48] = FTK_KEY_h;
 79	s_key_map[0x49] = FTK_KEY_i;
 80	s_key_map[0x4a] = FTK_KEY_j;
 81	s_key_map[0x4b] = FTK_KEY_k;
 82	s_key_map[0x4c] = FTK_KEY_l;
 83	s_key_map[0x4d] = FTK_KEY_m;
 84	s_key_map[0x4e] = FTK_KEY_n;
 85	s_key_map[0x4f] = FTK_KEY_o;
 86	s_key_map[0x50] = FTK_KEY_p;
 87	s_key_map[0x51] = FTK_KEY_q;
 88	s_key_map[0x52] = FTK_KEY_r;
 89	s_key_map[0x53] = FTK_KEY_s;
 90	s_key_map[0x54] = FTK_KEY_t;
 91	s_key_map[0x55] = FTK_KEY_u;
 92	s_key_map[0x56] = FTK_KEY_v;
 93	s_key_map[0x57] = FTK_KEY_w;
 94	s_key_map[0x58] = FTK_KEY_x;
 95	s_key_map[0x59] = FTK_KEY_y;
 96	s_key_map[0x5a] = FTK_KEY_z;
 97	s_key_map[0x21] = FTK_KEY_PAGEUP;
 98	s_key_map[0x25] = FTK_KEY_LEFT;
 99	s_key_map[0x27] = FTK_KEY_RIGHT;
100	s_key_map[0x24] = FTK_KEY_HOME;
101	s_key_map[0x23] = FTK_KEY_END;
102	s_key_map[0x28] = FTK_KEY_DOWN;
103	s_key_map[0x22] = FTK_KEY_PAGEDOWN;
104	s_key_map[0x2d] = FTK_KEY_INSERT;
105	s_key_map[0x2e] = FTK_KEY_DELETE;
106	s_key_map[0x26] = FTK_KEY_UP;
107}
108
109static char* ftk_display_wince_on_ime(HWND hwnd, LPARAM lParam)
110{
111	LONG n;
112	wchar_t* unicode;
113	char* utf8;
114	HIMC himc;
115	FtkInputMethod* im = NULL;
116
117	if(!(lParam & GCS_RESULTSTR))
118	{
119		return NULL;
120	}
121	if(ftk_input_method_manager_get_current(ftk_default_input_method_manager(), &im) == RET_FAIL || im == NULL)
122	{
123		return NULL;
124	}
125	if(ftk_input_method_wince_get_editor(im) == NULL)
126	{
127		return NULL;
128	}
129
130	himc = ImmGetContext(hwnd);
131	if((void*)himc == NULL)
132	{
133		return NULL;
134	}
135	n = ImmGetCompositionStringW(himc, GCS_RESULTSTR, NULL, 0);
136	if(n <= 0)
137	{
138		ImmReleaseContext(hwnd, himc);
139		return NULL;
140	}
141	unicode = (wchar_t*)FTK_ALLOC(n + 2);
142	if(unicode == NULL)
143	{
144		ImmReleaseContext(hwnd, himc);
145		return NULL;
146	}
147	if(ImmGetCompositionStringW(himc, GCS_RESULTSTR, unicode, n) != n)
148	{
149		FTK_FREE(unicode);
150		ImmReleaseContext(hwnd, himc);
151		return NULL;
152	}
153	ImmReleaseContext(hwnd, himc);
154
155	ftk_logd("%s:%d WM_IME_COMPOSITION:%s\n", __FILE__, __LINE__, unicode);
156	unicode[n / 2] = L'\0';
157	ftk_logd("%s:%d WM_IME_COMPOSITION:%s\n", __FILE__, __LINE__, unicode);
158	n = WideCharToMultiByte(CP_UTF8, 0, unicode, -1, NULL, 0, NULL, NULL);
159	if(n < 1)
160	{
161		FTK_FREE(unicode);
162		return NULL;
163	}
164	utf8 = (char*) FTK_ALLOC(n);
165	if(utf8 == NULL)
166	{
167		FTK_FREE(unicode);
168		return NULL;
169	}
170	n = WideCharToMultiByte(CP_UTF8, 0, unicode, -1, utf8, n, NULL, NULL);
171	if(n < 1)
172	{
173		FTK_FREE(unicode);
174		FTK_FREE(utf8);
175		return NULL;
176	}
177
178	FTK_FREE(unicode);
179
180	ftk_logd("%s:%d WM_IME_COMPOSITION:%s\n", __FILE__, __LINE__, utf8);
181
182	return utf8;
183}
184
185static void ftk_display_wince_on_paint(PrivInfo* priv)
186{
187	HDC dc;
188	int x, y, w, h;
189	PAINTSTRUCT ps;
190	HBITMAP bmp, old_bmp;
191
192	BeginPaint(priv->wnd, &ps);
193
194	x = ps.rcPaint.left;
195	y = ps.rcPaint.top;
196	w = ps.rcPaint.right - ps.rcPaint.left;
197	h = ps.rcPaint.bottom - ps.rcPaint.top;
198
199	bmp = CreateBitmap(priv->width, priv->height, 1, 32, priv->bits);
200
201	dc = CreateCompatibleDC(ps.hdc);
202	old_bmp = (HBITMAP)SelectObject(dc, bmp);
203	BitBlt(ps.hdc, x, y, w, h, dc, x, y, SRCCOPY);
204	SelectObject(dc, old_bmp);
205	DeleteObject(dc);
206
207	DeleteObject(bmp);
208	EndPaint(priv->wnd, &ps);
209}
210
211static LRESULT CALLBACK WinProc(HWND hwnd, UINT message, WPARAM wParam, LPARAM lParam)
212{
213	char* buf = NULL;
214	PrivInfo* priv = (PrivInfo*) GetWindowLongW(hwnd, GWL_USERDATA);
215
216	switch(message)
217	{
218		case WM_DESTROY:
219			PostQuitMessage(0);
220			FTK_QUIT();
221			break;
222		//case WM_ACTIVATE:
223		//	set_fullscreen(priv);
224		//	break;
225		case WM_PAINT:
226			ftk_display_wince_on_paint(priv);
227			break;
228		case WM_KEYDOWN:
229		case WM_KEYUP:
230			priv->event.type = message == WM_KEYDOWN ? FTK_EVT_KEY_DOWN : FTK_EVT_KEY_UP;
231			priv->event.u.key.code = s_key_map[0xFF & wParam];
232			break;
233		case WM_LBUTTONUP:
234		case WM_LBUTTONDOWN:
235			priv->event.u.mouse.x = LOWORD(lParam);
236			priv->event.u.mouse.y = HIWORD(lParam);
237			priv->event.type = message == WM_LBUTTONUP? FTK_EVT_MOUSE_UP : FTK_EVT_MOUSE_DOWN;
238			break;
239		case WM_MOUSEMOVE:
240			priv->event.u.mouse.x = LOWORD(lParam);
241			priv->event.u.mouse.y = HIWORD(lParam);
242			priv->event.type = FTK_EVT_MOUSE_MOVE;
243			break;
244		case WM_IME_COMPOSITION:
245			buf = ftk_display_wince_on_ime(hwnd, lParam);
246			if(buf == NULL)
247			{
248				return DefWindowProcW(hwnd, message, wParam, lParam);
249			}
250			priv->event.u.extra = buf;
251			priv->event.type = FTK_EVT_OS_IM_COMMIT;
252			break;
253		default:
254			return DefWindowProcW(hwnd, message, wParam, lParam);
255	}
256
257	if(priv->event.type != FTK_EVT_NOP)
258	{
259		ftk_wnd_manager_queue_event_auto_rotate(ftk_default_wnd_manager(), &priv->event);
260		priv->event.type = FTK_EVT_NOP;
261	}
262
263	return 0;
264}
265
266static HWND ftk_display_wince_create_win(PrivInfo *priv)
267{
268	HWND hwnd;
269	WNDCLASSW wincl;
270
271	wincl.hInstance = NULL;
272	wincl.lpszClassName = szClassName;
273	wincl.lpfnWndProc = WinProc;
274	wincl.style = CS_DBLCLKS;
275	wincl.hIcon = NULL;
276	wincl.hCursor = LoadCursorW(NULL, IDC_ARROW);
277	wincl.lpszMenuName = NULL;
278	wincl.cbClsExtra = 0;
279	wincl.cbWndExtra = 64;
280	wincl.hbrBackground = (HBRUSH) COLOR_BACKGROUND;
281
282	if (!RegisterClassW(&wincl))
283	{
284		return NULL;
285	}
286
287	hwnd = CreateWindowExW(0, szClassName, L"FtkEmulator", WS_DLGFRAME,
288		CW_USEDEFAULT, CW_USEDEFAULT, priv->width, priv->height,
289		HWND_DESKTOP, NULL, NULL, NULL);
290
291	return hwnd;
292}
293
294static Ret ftk_display_wince_update(FtkDisplay* thiz, FtkBitmap* bitmap, FtkRect* rect, int xoffset, int yoffset)
295{
296	//HDC hdc_src, hdc_dest;
297	RECT dirty_rect;
298	//HBITMAP hbm_src;
299	DECL_PRIV(thiz, priv);
300	//BITMAPINFO bmi;
301
302	ftk_bitmap_copy_to_data_bgra32(bitmap, rect, 
303		priv->bits, xoffset, yoffset, priv->width, priv->height); 
304#if 0
305	memset(&bmi, 0x00, sizeof(bmi));
306	bmi.bmiHeader.biBitCount = 32;
307	bmi.bmiHeader.biCompression = BI_RGB;
308	bmi.bmiHeader.biSize = sizeof(bmi.bmiHeader);
309	bmi.bmiHeader.biWidth = priv->width;
310	bmi.bmiHeader.biHeight = priv->height;
311	bmi.bmiHeader.biPlanes = 1;
312
313	hdc_dest = CreateCompatibleDC(0);
314	SelectObject(hdc_dest, priv->hBitmap);
315	hdc_src = CreateCompatibleDC(0);
316	hbm_src = CreateBitmap(priv->width, priv->height, 1, 32, priv->bits);
317	SelectObject(hdc_src, hbm_src);
318	BitBlt(hdc_dest, 0, 0, priv->width, priv->height, hdc_src, 0, 0, SRCCOPY);
319	DeleteDC(hdc_src);
320	DeleteObject(hbm_src);
321	DeleteDC(hdc_dest);
322#endif
323	dirty_rect.left = xoffset;
324	dirty_rect.top = yoffset;
325	dirty_rect.right = xoffset + rect->width;
326	dirty_rect.bottom = yoffset + rect->height;
327
328	InvalidateRect(priv->wnd, &dirty_rect, FALSE);
329
330	return RET_OK;
331}
332
333static int ftk_display_wince_width(FtkDisplay* thiz)
334{
335	DECL_PRIV(thiz, priv);
336	return_val_if_fail(priv != NULL, 0);
337	return priv->width;
338}
339
340static int ftk_display_wince_height(FtkDisplay* thiz)
341{
342	DECL_PRIV(thiz, priv);
343	return_val_if_fail(priv != NULL, 0);
344	return priv->height;
345}
346
347static Ret ftk_display_wince_snap(FtkDisplay* thiz, FtkRect* r, FtkBitmap* bitmap)
348{
349	FtkRect rect = {0};
350	DECL_PRIV(thiz, priv);
351	int w = ftk_display_width(thiz);
352	int h = ftk_display_height(thiz);
353	int bw = ftk_bitmap_width(bitmap);
354	int bh = ftk_bitmap_height(bitmap);
355
356	rect.x = r->x;
357	rect.y = r->y;
358	rect.width = FTK_MIN(bw, r->width);
359	rect.height = FTK_MIN(bh, r->height);
360
361	return ftk_bitmap_copy_from_data_bgra32(bitmap, priv->bits, w, h, &rect);
362}
363
364static void ftk_display_wince_destroy(FtkDisplay* thiz)
365{
366	if(thiz != NULL)
367	{
368		DECL_PRIV(thiz, priv);
369		FTK_FREE(priv->bits);
370		FTK_ZFREE(thiz, sizeof(FtkDisplay) + sizeof(PrivInfo));
371	}
372}
373
374FtkDisplay* ftk_display_wince_create(void)
375{
376	FtkDisplay* thiz;
377
378	thiz = (FtkDisplay*)FTK_ZALLOC(sizeof(FtkDisplay) + sizeof(PrivInfo));
379	if(thiz != NULL)
380	{
381		DECL_PRIV(thiz, priv);
382		thiz->update = ftk_display_wince_update;
383		thiz->snap = ftk_display_wince_snap;
384		thiz->width = ftk_display_wince_width;
385		thiz->height = ftk_display_wince_height;
386		thiz->destroy = ftk_display_wince_destroy;
387
388		priv->width = GetSystemMetrics(SM_CXSCREEN);
389		priv->height = GetSystemMetrics(SM_CYSCREEN);
390		priv->bits = FTK_ZALLOC(priv->width * priv->height * 4);
391
392		priv->wnd = ftk_display_wince_create_win(priv);
393
394		ftk_display_wince_keymap_init();
395
396		SetWindowLongW(priv->wnd, GWL_USERDATA, (LONG)priv);
397		ShowWindow(priv->wnd, SW_SHOW);
398		//UpdateWindow(priv->wnd);
399
400		ftk_display_wince_set_fullscreen(priv);
401	}
402
403	return thiz;
404}