2 * X events handling functions
4 * Copyright 1993 Alexandre Julliard
7 static char Copyright[] = "Copyright Alexandre Julliard, 1993";
9 #include <X11/Intrinsic.h>
10 #include <X11/StringDefs.h>
18 #define NB_BUTTONS 3 /* Windows can handle 3 buttons */
19 static WORD dblclick_time = 300; /* Max. time for a double click (milliseconds) */
22 static void EVENT_expose();
23 static void EVENT_key();
24 static void EVENT_mouse_motion();
25 static void EVENT_mouse_button();
26 static void EVENT_structure();
27 static void EVENT_focus_change();
30 static HWND captureWnd = 0;
32 extern HWND hWndFocus;
34 /***********************************************************************
37 * Add the event handlers to the given window
39 void EVENT_AddHandlers( Widget w, int hwnd )
41 XtAddEventHandler(w, ExposureMask, FALSE,
42 EVENT_expose, (XtPointer)hwnd );
43 XtAddEventHandler(w, KeyPressMask | KeyReleaseMask, FALSE,
44 EVENT_key, (XtPointer)hwnd );
45 XtAddEventHandler(w, PointerMotionMask, FALSE,
46 EVENT_mouse_motion, (XtPointer)hwnd );
47 XtAddEventHandler(w, ButtonPressMask | ButtonReleaseMask, FALSE,
48 EVENT_mouse_button, (XtPointer)hwnd );
49 XtAddEventHandler(w, StructureNotifyMask, FALSE,
50 EVENT_structure, (XtPointer)hwnd );
51 XtAddEventHandler(w, FocusChangeMask, FALSE,
52 EVENT_focus_change, (XtPointer)hwnd );
56 /***********************************************************************
57 * EVENT_RemoveHandlers
59 * Remove the event handlers of the given window
61 void EVENT_RemoveHandlers( Widget w, int hwnd )
63 XtRemoveEventHandler(w, ExposureMask, FALSE,
64 EVENT_expose, (XtPointer)hwnd );
65 XtRemoveEventHandler(w, KeyPressMask | KeyReleaseMask, FALSE,
66 EVENT_key, (XtPointer)hwnd );
67 XtRemoveEventHandler(w, PointerMotionMask, FALSE,
68 EVENT_mouse_motion, (XtPointer)hwnd );
69 XtRemoveEventHandler(w, ButtonPressMask | ButtonReleaseMask, FALSE,
70 EVENT_mouse_button, (XtPointer)hwnd );
71 XtRemoveEventHandler(w, StructureNotifyMask, FALSE,
72 EVENT_structure, (XtPointer)hwnd );
73 XtRemoveEventHandler(w, FocusChangeMask, FALSE,
74 EVENT_focus_change, (XtPointer)hwnd );
78 /***********************************************************************
79 * EVENT_XStateToKeyState
81 * Translate a X event state (Button1Mask, ShiftMask, etc...) to
82 * a Windows key state (MK_SHIFT, MK_CONTROL, etc...)
84 static WORD EVENT_XStateToKeyState( int state )
88 if (state & Button1Mask) kstate |= MK_LBUTTON;
89 if (state & Button2Mask) kstate |= MK_MBUTTON;
90 if (state & Button3Mask) kstate |= MK_RBUTTON;
91 if (state & ShiftMask) kstate |= MK_SHIFT;
92 if (state & ControlMask) kstate |= MK_CONTROL;
97 /***********************************************************************
100 * Handle a X expose event
102 static void EVENT_expose( Widget w, int hwnd, XExposeEvent *event,
103 Boolean *cont_dispatch )
106 rect.left = event->x;
108 rect.right = event->x + event->width;
109 rect.bottom = event->y + event->height;
111 InvalidateRect( hwnd, &rect, TRUE );
115 /***********************************************************************
118 * Handle a X key event
120 static void EVENT_key( Widget w, int hwnd, XKeyEvent *event,
121 Boolean *cont_dispatch )
128 int count = XLookupString(event, Str, 1, &key, &cs);
131 printf("WM_KEY??? : count=%u / %X / '%s'\n",count, Str[0], Str);
134 msg.message = (event->type == KeyRelease) ? WM_KEYUP : WM_KEYDOWN;
136 msg.lParam = (event->x & 0xffff) | (event->y << 16);
137 msg.time = event->time;
138 msg.pt.x = event->x & 0xffff;
139 msg.pt.y = event->y & 0xffff;
145 /***********************************************************************
148 * Handle a X mouse motion event
150 static void EVENT_mouse_motion( Widget w, int hwnd, XMotionEvent *event,
151 Boolean *cont_dispatch )
156 msg.message = WM_MOUSEMOVE;
157 msg.wParam = EVENT_XStateToKeyState( event->state );
158 msg.lParam = (event->x & 0xffff) | (event->y << 16);
159 msg.time = event->time;
160 msg.pt.x = event->x & 0xffff;
161 msg.pt.y = event->y & 0xffff;
167 /***********************************************************************
170 * Handle a X mouse button event
172 static void EVENT_mouse_button( Widget w, int hwnd, XButtonEvent *event,
173 Boolean *cont_dispatch )
175 static WORD messages[3][NB_BUTTONS] =
177 { WM_LBUTTONDOWN, WM_MBUTTONDOWN, WM_RBUTTONDOWN },
178 { WM_LBUTTONUP, WM_MBUTTONUP, WM_RBUTTONUP },
179 { WM_LBUTTONDBLCLK, WM_MBUTTONDBLCLK, WM_RBUTTONDBLCLK }
181 static unsigned long lastClickTime[NB_BUTTONS] = { 0, 0, 0 };
184 int buttonNum, prevTime, type;
186 buttonNum = event->button-1;
187 if (buttonNum >= NB_BUTTONS) return;
188 if (event->type == ButtonRelease) type = 1;
190 { /* Check if double-click */
191 prevTime = lastClickTime[buttonNum];
192 lastClickTime[buttonNum] = event->time;
193 if (event->time - prevTime < dblclick_time)
197 if (!(wndPtr = WIN_FindWndPtr( hwnd ))) return;
198 if (!(classPtr = CLASS_FindClassPtr( wndPtr->hClass ))) return;
199 type = (classPtr->wc.style & CS_DBLCLKS) ? 2 : 0;
205 msg.message = messages[type][buttonNum];
206 msg.wParam = EVENT_XStateToKeyState( event->state );
207 msg.lParam = (event->x & 0xffff) | (event->y << 16);
208 msg.time = event->time;
209 msg.pt.x = event->x & 0xffff;
210 msg.pt.y = event->y & 0xffff;
216 /***********************************************************************
219 * Handle a X StructureNotify event
221 static void EVENT_structure( Widget w, int hwnd, XEvent *event,
222 Boolean *cont_dispatch )
227 msg.time = GetTickCount();
233 case ConfigureNotify:
235 XConfigureEvent * evt = (XConfigureEvent *)event;
236 WND * wndPtr = WIN_FindWndPtr( hwnd );
238 wndPtr->rectClient.right = wndPtr->rectClient.left + evt->width;
239 wndPtr->rectClient.bottom = wndPtr->rectClient.top + evt->height;
240 PostMessage( hwnd, WM_SIZE, SIZE_RESTORED,
241 (evt->width & 0xffff) | (evt->height << 16) );
249 /**********************************************************************
252 * Handle an X FocusChange event
254 static void EVENT_focus_change( Widget w, int hwnd, XEvent *event,
255 Boolean *cont_dispatch )
260 msg.time = GetTickCount();
268 msg.message = WM_SETFOCUS;
279 msg.message = WM_KILLFOCUS;
289 /**********************************************************************
290 * SetCapture (USER.18)
292 HWND SetCapture(HWND wnd)
295 HWND old_capture_wnd = captureWnd;
296 WND *wnd_p = WIN_FindWndPtr(wnd);
300 rv = XtGrabPointer(wnd_p->winWidget, False,
301 ButtonPressMask | ButtonReleaseMask | ButtonMotionMask,
302 GrabModeAsync, GrabModeSync, None, None, CurrentTime);
304 if (rv == GrabSuccess)
307 return old_capture_wnd;
313 /**********************************************************************
314 * ReleaseCapture (USER.19)
316 void ReleaseCapture()
323 wnd_p = WIN_FindWndPtr(captureWnd);
327 XtUngrabPointer(wnd_p->winWidget, CurrentTime);
332 /**********************************************************************
333 * SetDoubleClickTime (USER.20)
335 void SetDoubleClickTime (WORD interval)
340 dblclick_time = interval;
343 /**********************************************************************
344 * GetDoubleClickTime (USER.21)
346 WORD GetDoubleClickTime ()
348 return ((WORD)dblclick_time);