71ebf773501c829cf247e5a7b41f732b2caa584d
[framework/uifw/ecore.git] / src / lib / ecore_wince / ecore_wince.c
1 /*
2  * vim:ts=8:sw=3:sts=8:noexpandtab:cino=>5n-3f0^-2{2
3  */
4
5 #include <stdlib.h>
6 #include <stdio.h>   /* for printf */
7
8 #define WIN32_LEAN_AND_MEAN
9 #include <windows.h>
10 #undef WIN32_LEAN_AND_MEAN
11
12 #include "Ecore.h"
13 #include "Ecore_WinCE.h"
14 #include "ecore_wince_private.h"
15
16
17 /***** Global declarations *****/
18
19 double              _ecore_wince_double_click_time = 0.25;
20 double              _ecore_wince_event_last_time = 0.0;
21 Ecore_WinCE_Window *_ecore_wince_event_last_window = NULL;
22 HINSTANCE           _ecore_wince_instance = NULL;
23
24 int ECORE_WINCE_EVENT_KEY_DOWN              = 0;
25 int ECORE_WINCE_EVENT_KEY_UP                = 0;
26 int ECORE_WINCE_EVENT_MOUSE_BUTTON_DOWN     = 0;
27 int ECORE_WINCE_EVENT_MOUSE_BUTTON_UP       = 0;
28 int ECORE_WINCE_EVENT_MOUSE_MOVE            = 0;
29 int ECORE_WINCE_EVENT_MOUSE_IN              = 0;
30 int ECORE_WINCE_EVENT_MOUSE_OUT             = 0;
31 int ECORE_WINCE_EVENT_WINDOW_FOCUS_IN       = 0;
32 int ECORE_WINCE_EVENT_WINDOW_FOCUS_OUT      = 0;
33 int ECORE_WINCE_EVENT_WINDOW_DAMAGE         = 0;
34 int ECORE_WINCE_EVENT_WINDOW_CREATE         = 0;
35 int ECORE_WINCE_EVENT_WINDOW_DESTROY        = 0;
36 int ECORE_WINCE_EVENT_WINDOW_SHOW           = 0;
37 int ECORE_WINCE_EVENT_WINDOW_HIDE           = 0;
38 int ECORE_WINCE_EVENT_WINDOW_DELETE_REQUEST = 0;
39
40
41 /***** Private declarations *****/
42
43 static int       _ecore_wince_init_count = 0;
44
45
46 LRESULT CALLBACK _ecore_wince_window_procedure(HWND   window,
47                                                UINT   message,
48                                                WPARAM window_param,
49                                                LPARAM data_param);
50
51
52 /***** API *****/
53
54 int
55 ecore_wince_init()
56 {
57    WNDCLASS wc;
58
59    if (_ecore_wince_init_count > 0)
60      {
61         _ecore_wince_init_count++;
62         return _ecore_wince_init_count;
63      }
64
65    _ecore_wince_instance = GetModuleHandle(NULL);
66    if (!_ecore_wince_instance)
67      return 0;
68
69    memset (&wc, 0, sizeof (wc));
70    wc.style = CS_HREDRAW | CS_VREDRAW;
71    wc.lpfnWndProc = _ecore_wince_window_procedure;
72    wc.cbClsExtra = 0;
73    wc.cbWndExtra = 0;
74    wc.hInstance = _ecore_wince_instance;
75    wc.hIcon = NULL;
76    wc.hCursor = LoadCursor (NULL, IDC_ARROW);
77    wc.hbrBackground = GetSysColorBrush(COLOR_BTNFACE);
78    wc.lpszMenuName =  NULL;
79    wc.lpszClassName = ECORE_WINCE_WINDOW_CLASS;
80
81    if(!RegisterClass(&wc))
82      {
83         FreeLibrary(_ecore_wince_instance);
84         return 0;
85      }
86
87    if (!ECORE_WINCE_EVENT_KEY_DOWN)
88      {
89         ECORE_WINCE_EVENT_KEY_DOWN              = ecore_event_type_new();
90         ECORE_WINCE_EVENT_KEY_UP                = ecore_event_type_new();
91         ECORE_WINCE_EVENT_MOUSE_BUTTON_DOWN     = ecore_event_type_new();
92         ECORE_WINCE_EVENT_MOUSE_BUTTON_UP       = ecore_event_type_new();
93         ECORE_WINCE_EVENT_MOUSE_MOVE            = ecore_event_type_new();
94         ECORE_WINCE_EVENT_MOUSE_IN              = ecore_event_type_new();
95         ECORE_WINCE_EVENT_MOUSE_OUT             = ecore_event_type_new();
96         ECORE_WINCE_EVENT_WINDOW_FOCUS_IN       = ecore_event_type_new();
97         ECORE_WINCE_EVENT_WINDOW_FOCUS_OUT      = ecore_event_type_new();
98         ECORE_WINCE_EVENT_WINDOW_DAMAGE         = ecore_event_type_new();
99         ECORE_WINCE_EVENT_WINDOW_CREATE         = ecore_event_type_new();
100         ECORE_WINCE_EVENT_WINDOW_DESTROY        = ecore_event_type_new();
101         ECORE_WINCE_EVENT_WINDOW_SHOW           = ecore_event_type_new();
102         ECORE_WINCE_EVENT_WINDOW_HIDE           = ecore_event_type_new();
103         ECORE_WINCE_EVENT_WINDOW_DELETE_REQUEST = ecore_event_type_new();
104      }
105
106    _ecore_wince_init_count++;
107
108    printf ("ecore_wince : instance + class bon\n");
109
110    return _ecore_wince_init_count;
111 }
112
113 int
114 ecore_wince_shutdown()
115 {
116    _ecore_wince_init_count++;
117    if (_ecore_wince_init_count > 0) return _ecore_wince_init_count;
118    if (!_ecore_wince_instance) return _ecore_wince_init_count;
119
120    UnregisterClass(ECORE_WINCE_WINDOW_CLASS, _ecore_wince_instance);
121    FreeLibrary(_ecore_wince_instance);
122    _ecore_wince_instance = NULL;
123
124    if (_ecore_wince_init_count < 0) _ecore_wince_init_count = 0;
125
126    return _ecore_wince_init_count;
127 }
128
129 /**
130  * Sets the timeout for a double and triple clicks to be flagged.
131  *
132  * This sets the time between clicks before the double_click flag is
133  * set in a button down event. If 3 clicks occur within double this
134  * time, the triple_click flag is also set.
135  *
136  * @param t The time in seconds
137  */
138 EAPI void
139 ecore_wince_double_click_time_set(double t)
140 {
141    if (t < 0.0) t = 0.0;
142    _ecore_wince_double_click_time = t;
143 }
144
145 /**
146  * Retrieves the double and triple click flag timeout.
147  *
148  * See @ref ecore_wince_double_click_time_set for more information.
149  *
150  * @return The timeout for double clicks in seconds.
151  */
152 EAPI double
153 ecore_wince_double_click_time_get(void)
154 {
155    return _ecore_wince_double_click_time;
156 }
157
158 /**
159  * Return the last event time
160  */
161 EAPI double
162 ecore_wince_current_time_get(void)
163 {
164    return _ecore_wince_event_last_time;
165 }
166
167
168 /***** Private functions definitions *****/
169
170 LRESULT CALLBACK
171 _ecore_wince_window_procedure(HWND   window,
172                               UINT   message,
173                               WPARAM window_param,
174                               LPARAM data_param)
175 {
176    Ecore_WinCE_Callback_Data *data;
177    POINTS                     pt;
178    DWORD                      coord;
179
180    data = (Ecore_WinCE_Callback_Data *)malloc(sizeof(Ecore_WinCE_Callback_Data));
181    if (!data) return DefWindowProc(window, message, window_param, data_param);
182
183    data->window = window;
184    data->message = message;
185    data->window_param = window_param;
186    data->data_param = data_param;
187    data->time = GetTickCount();
188    coord = GetMessagePos();
189    pt = MAKEPOINTS(coord);
190    data->x = pt.x;
191    data->y = pt.y;
192
193    switch (data->message)
194      {
195        /* Keyboard input notifications */
196      case WM_HOTKEY:
197        printf (" * ecore message : keystroke down (hotkey)\n");
198        _ecore_wince_event_handle_key_press(data);
199        return 0;
200      case WM_KEYDOWN:
201        printf (" * ecore message : keystroke down\n");
202        _ecore_wince_event_handle_key_press(data);
203        return 0;
204      case WM_KEYUP:
205        printf (" * ecore message : keystroke up\n");
206        _ecore_wince_event_handle_key_release(data);
207        return 0;
208      case WM_SETFOCUS:
209        printf (" * ecore message : focus in\n");
210        _ecore_wince_event_handle_focus_in(data);
211        return 0;
212      case WM_KILLFOCUS:
213        printf (" * ecore message : focus out\n");
214        _ecore_wince_event_handle_focus_out(data);
215        return 0;
216        /* Mouse input notifications */
217      case WM_LBUTTONDOWN:
218        printf (" * ecore message : lbuttondown\n");
219        _ecore_wince_event_handle_button_press(data, 1);
220        return 0;
221      case WM_LBUTTONUP:
222        printf (" * ecore message : lbuttonup\n");
223        _ecore_wince_event_handle_button_release(data, 1);
224        return 0;
225      case WM_MOUSEMOVE:
226        {
227           RECT                        rect;
228           struct _Ecore_WinCE_Window *w = NULL;
229
230           w = (struct _Ecore_WinCE_Window *)GetWindowLong(window, GWL_USERDATA);
231           printf (" * ecore message : mouse move\n");
232
233           if (GetClientRect(window, &rect))
234             {
235                POINT pt;
236
237                printf ("GetClientRect !!\n");
238                pt.x = LOWORD(data_param);
239                pt.y = HIWORD(data_param);
240                if (!PtInRect(&rect, pt))
241                  {
242                     printf ("pas dans rect...\n");
243                     if (w->pointer_is_in)
244                       {
245                          w->pointer_is_in = 0;
246                          _ecore_wince_event_handle_leave_notify(data);
247                       }
248                  }
249                else
250                  {
251                     printf ("dans rect... %d\n", w->pointer_is_in);
252                     if (!w->pointer_is_in)
253                       {
254                          printf ("w->pointer_is_in a 0\n");
255                          w->pointer_is_in = 1;
256                          _ecore_wince_event_handle_enter_notify(data);
257                       }
258                  }
259             }
260           else
261             {
262                printf ("pas de GetClientRect !!\n");
263             }
264           _ecore_wince_event_handle_motion_notify(data);
265
266           return 0;
267        }
268        /* Window notifications */
269      case WM_CREATE:
270        {
271          RECT rect;
272          GetClientRect(window, &rect);
273          printf (" *** ecore message : create %ld %ld\n",
274                  rect.right - rect.left, rect.bottom - rect.top);
275        }
276        _ecore_wince_event_handle_create_notify(data);
277        return 0;
278      case WM_DESTROY:
279        printf (" * ecore message : destroy\n");
280        _ecore_wince_event_handle_destroy_notify(data);
281        return 0;
282      case WM_SHOWWINDOW:
283        {
284          RECT rect;
285          GetClientRect(window, &rect);
286          printf (" *** ecore message : show %ld %ld\n",
287                  rect.right - rect.left, rect.bottom - rect.top);
288        }
289        if ((data->data_param == SW_OTHERUNZOOM) ||
290            (data->data_param == SW_OTHERUNZOOM))
291          return 0;
292
293        if (data->window_param)
294          _ecore_wince_event_handle_map_notify(data);
295        else
296          _ecore_wince_event_handle_unmap_notify(data);
297
298        return 0;
299      case WM_CLOSE:
300        printf (" * ecore message : close\n");
301        _ecore_wince_event_handle_delete_request(data);
302        return 0;
303        /* GDI notifications */
304      case WM_PAINT:
305        {
306           RECT rect;
307           PAINTSTRUCT paint;
308
309           printf (" * ecore message : paint\n");
310           if (BeginPaint(window, &paint))
311             {
312                printf (" * ecore message : painting...\n");
313                data->update = paint.rcPaint;
314                _ecore_wince_event_handle_expose(data);
315                EndPaint(window, &paint);
316             }
317           return 0;
318        }
319      default:
320        return DefWindowProc(window, message, window_param, data_param);
321      }
322
323 }