From 21cb8b301b45f8feadbb050e0b4b107c044f41fd Mon Sep 17 00:00:00 2001 From: Xavier Leroy Date: Tue, 16 Apr 2002 10:00:33 +0000 Subject: [PATCH] Corrige la gestion des evenements git-svn-id: http://caml.inria.fr/svn/ocaml/trunk@4679 f963ae5c-01c2-4b8c-9fe0-0dff7051ff02 --- otherlibs/win32graph/draw.c | 127 +++++++------------------------- otherlibs/win32graph/libgraph.h | 7 +- otherlibs/win32graph/open.c | 27 ++++++- 3 files changed, 57 insertions(+), 104 deletions(-) diff --git a/otherlibs/win32graph/draw.c b/otherlibs/win32graph/draw.c index e4ac44eab..785942f3e 100644 --- a/otherlibs/win32graph/draw.c +++ b/otherlibs/win32graph/draw.c @@ -433,8 +433,7 @@ void gr_enqueue_char(unsigned char c) #define Key_pressed 4 #define Mouse_motion 8 #define Poll 16 -int InspectMessages; -MSG msg; +MSG * InspectMessages = NULL; CAMLprim value gr_wait_event(value eventlist) { @@ -446,7 +445,7 @@ CAMLprim value gr_wait_event(value eventlist) int r,i,stop; unsigned int modifiers; POINT pt; - unsigned char keystate[256]; + MSG msg; gr_check_open(); mask = 0; @@ -477,107 +476,37 @@ CAMLprim value gr_wait_event(value eventlist) key = -1; if (poll) { - // Poll uses peek message - r = 0; - if (mask & Button_down) { - r |= PeekMessage(&msg,grwindow.hwnd,WM_LBUTTONDOWN,WM_LBUTTONDOWN,PM_REMOVE); - if (r) - button = 1; - else { - r |= PeekMessage(&msg,grwindow.hwnd,WM_MBUTTONDOWN,WM_LBUTTONDOWN,PM_REMOVE); - if (r) - button = 2; - else { - r |= PeekMessage(&msg,grwindow.hwnd,WM_RBUTTONDOWN,WM_LBUTTONDOWN,PM_REMOVE); - } - if (r) - button = 3; - } - } - if (mask & Button_up) { - r |= PeekMessage(&msg,grwindow.hwnd,WM_LBUTTONUP,WM_LBUTTONUP,PM_REMOVE); - if (r) - button = 1; - else { - r |= PeekMessage(&msg,grwindow.hwnd,WM_MBUTTONUP,WM_LBUTTONUP,PM_REMOVE); - if (r) - button = 2; - else { - r |= PeekMessage(&msg,grwindow.hwnd,WM_RBUTTONUP,WM_LBUTTONUP,PM_REMOVE); - } - if (r) - button = 3; - } - } - if (mask & Mouse_motion) { - r |= PeekMessage(&msg,grwindow.hwnd,WM_MOUSEMOVE,WM_MOUSEMOVE,PM_REMOVE); - } - if (r) { - pt = msg.pt; - MapWindowPoints(HWND_DESKTOP,grwindow.hwnd,&pt,1); - mouse_x = pt.x; - mouse_y = grwindow.height-pt.y; - } - if (mask & Key_pressed) { - r = PeekMessage(&msg,grwindow.hwnd,WM_KEYFIRST,WM_KEYLAST,PM_REMOVE); - if (r) { - GetKeyboardState(keystate); - for (i=0; i<256;i++) { - if (keystate[i]&(~1)) { - key = i; - break; - } - } - } - } + // Poll uses info on last event stored in global variables + mouse_x = MouseLastX; + mouse_y = MouseLastY; + button = MouseLbuttonDown | MouseMbuttonDown | MouseRbuttonDown; + key = LastKey; } else { // Not polled. Block for a message - InspectMessages = 1; + InspectMessages = &msg; while (1) { WaitForSingleObject(EventHandle,INFINITE); stop = 0; - if (msg.message == WM_LBUTTONDOWN && (mask&Button_down)) { - stop = 1; + switch (msg.message) { + case WM_LBUTTONDOWN: + case WM_MBUTTONDOWN: + case WM_RBUTTONDOWN: button = 1; - } - if (msg.message == WM_MBUTTONDOWN && (mask&Button_down)) { - stop = 1; - button = 2; - } - if (msg.message == WM_RBUTTONDOWN && (mask&Button_down)) { - stop = 1; - button = 3; - } - if (mask&Button_up) { - if (msg.message == WM_LBUTTONUP) { - stop = 1; - button = 1; - } - if (msg.message == WM_MBUTTONUP) { - stop = 1; - button = 2; - } - if (msg.message == WM_RBUTTONUP) { - stop = 1; - button = 3; - } - } - if (mask&Mouse_motion) { - if (msg.message == WM_MOUSEMOVE) { - stop = 1; - } - } - if (mask&Key_pressed) { - if (msg.message >= WM_KEYFIRST && msg.message <= WM_KEYLAST) { - stop = 1; - GetKeyboardState(keystate); - for (i=0; i<256;i++) { - if (keystate[i]&(~1)) { - key = i; - break; - } - } - } + if (mask&Button_down) stop = 1; + break; + case WM_LBUTTONUP: + case WM_MBUTTONUP: + case WM_RBUTTONUP: + button = 0; + if (mask&Button_up) stop = 1; + break; + case WM_MOUSEMOVE: + if (mask&Mouse_motion) stop = 1; + break; + case WM_CHAR: + key = msg.wParam & 0xFF; + if (mask&Key_pressed) stop = 1; + break; } if (stop) { pt = msg.pt; @@ -589,7 +518,7 @@ CAMLprim value gr_wait_event(value eventlist) if (msg.message == WM_CLOSE) break; } - InspectMessages = 0; + InspectMessages = NULL; } res = alloc_small(5, 0); Field(res, 0) = Val_int(mouse_x); diff --git a/otherlibs/win32graph/libgraph.h b/otherlibs/win32graph/libgraph.h index 50915d931..067f157a3 100644 --- a/otherlibs/win32graph/libgraph.h +++ b/otherlibs/win32graph/libgraph.h @@ -15,6 +15,7 @@ #include #include +#include struct canvas { int w, h; /* Dimensions of the drawable */ @@ -103,6 +104,8 @@ extern GR_WINDOW grwindow; HFONT CreationFont(char *name); extern int MouseLbuttonDown,MouseMbuttonDown,MouseRbuttonDown; extern HANDLE EventHandle; -extern int InspectMessages; -extern MSG msg; +extern MSG * InspectMessages; +extern int MouseLbuttonDown,MouseMbuttonDown,MouseRbuttonDown; +extern int MouseLastX, MouseLastY; +extern int LastKey; diff --git a/otherlibs/win32graph/open.c b/otherlibs/win32graph/open.c index 2b1b6271b..08f5701f0 100644 --- a/otherlibs/win32graph/open.c +++ b/otherlibs/win32graph/open.c @@ -19,6 +19,8 @@ #include static value gr_reset(void); int MouseLbuttonDown,MouseMbuttonDown,MouseRbuttonDown; +int MouseLastX, MouseLastY; +int LastKey = -1; static long tid; static HANDLE threadHandle; HWND grdisplay = NULL; @@ -73,6 +75,7 @@ static LRESULT CALLBACK GraphicsWndProc(HWND hwnd,UINT msg,WPARAM wParam,LPARAM { PAINTSTRUCT ps; HDC hdc; + switch (msg) { // Create the MDI client invisible window case WM_CREATE: @@ -113,7 +116,23 @@ static LRESULT CALLBACK GraphicsWndProc(HWND hwnd,UINT msg,WPARAM wParam,LPARAM case WM_MBUTTONUP: MouseMbuttonDown = 0; break; - default: + case WM_CHAR: + LastKey = wParam & 0xFF; + break; + case WM_KEYUP: + LastKey = -1; + break; + case WM_MOUSEMOVE: +#if 0 + pt.x = GET_X_LPARAM(lParam); + pt.y = GET_Y_LPARAM(lParam); + MapWindowPoints(HWND_DESKTOP,grwindow.hwnd,&pt,1); + MouseLastX = pt.x; + MouseLastY = grwindow.height - 1 - pt.y; +#else + MouseLastX = GET_X_LPARAM(lParam); + MouseLastY = grwindow.height - 1 - GET_Y_LPARAM(lParam); +#endif break; } return DefWindowProc(hwnd,msg,wParam,lParam); @@ -201,7 +220,8 @@ static DWORD WINAPI gr_open_graph_internal(value arg) int screenx,screeny; int attributes; static int registered; - + MSG msg; + gr_initialized = TRUE; hInst = GetModuleHandle(NULL); x = y = w = h = CW_USEDEFAULT; @@ -255,7 +275,8 @@ static DWORD WINAPI gr_open_graph_internal(value arg) /* Enter the message handling loop */ while (GetMessage(&msg,NULL,0,0)) { - if (InspectMessages) { + if (InspectMessages != NULL) { + *InspectMessages = msg; SetEvent(EventHandle); Sleep(10); }