Make WM_MOUSEHOVER coordinates client relative.
[wine] / windows / message.c
1 /*
2  * Message queues related functions
3  *
4  * Copyright 1993, 1994 Alexandre Julliard
5  *
6  * This library is free software; you can redistribute it and/or
7  * modify it under the terms of the GNU Lesser General Public
8  * License as published by the Free Software Foundation; either
9  * version 2.1 of the License, or (at your option) any later version.
10  *
11  * This library is distributed in the hope that it will be useful,
12  * but WITHOUT ANY WARRANTY; without even the implied warranty of
13  * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the GNU
14  * Lesser General Public License for more details.
15  *
16  * You should have received a copy of the GNU Lesser General Public
17  * License along with this library; if not, write to the Free Software
18  * Foundation, Inc., 59 Temple Place, Suite 330, Boston, MA  02111-1307  USA
19  */
20
21 #include "config.h"
22
23 #include <stdlib.h>
24 #include <string.h>
25 #include <ctype.h>
26 #ifdef HAVE_SYS_TIME_H
27 # include <sys/time.h>
28 #endif
29 #include <sys/types.h>
30
31 #include "winbase.h"
32 #include "wingdi.h"
33 #include "winuser.h"
34 #include "message.h"
35 #include "winerror.h"
36 #include "wine/server.h"
37 #include "controls.h"
38 #include "dde.h"
39 #include "heap.h"
40 #include "message.h"
41 #include "thread.h"
42 #include "user.h"
43 #include "win.h"
44 #include "winpos.h"
45 #include "winproc.h"
46 #include "wine/debug.h"
47
48 WINE_DEFAULT_DEBUG_CHANNEL(msg);
49 WINE_DECLARE_DEBUG_CHANNEL(key);
50
51 #define WM_NCMOUSEFIRST         WM_NCMOUSEMOVE
52 #define WM_NCMOUSELAST          WM_NCMBUTTONDBLCLK
53
54 static BYTE QueueKeyStateTable[256];
55
56
57 /***********************************************************************
58  *           is_keyboard_message
59  */
60 inline static BOOL is_keyboard_message( UINT message )
61 {
62     return (message >= WM_KEYFIRST && message <= WM_KEYLAST);
63 }
64
65
66 /***********************************************************************
67  *           is_mouse_message
68  */
69 inline static BOOL is_mouse_message( UINT message )
70 {
71     return ((message >= WM_NCMOUSEFIRST && message <= WM_NCMOUSELAST) ||
72             (message >= WM_MOUSEFIRST && message <= WM_MOUSELAST));
73 }
74
75
76 /***********************************************************************
77  *           check_message_filter
78  */
79 inline static BOOL check_message_filter( const MSG *msg, HWND hwnd, UINT first, UINT last )
80 {
81     if (hwnd)
82     {
83         if (msg->hwnd != hwnd && !IsChild( hwnd, msg->hwnd )) return FALSE;
84     }
85     if (first || last)
86     {
87        return (msg->message >= first && msg->message <= last);
88     }
89     return TRUE;
90 }
91
92
93 /***********************************************************************
94  *           process_sent_messages
95  *
96  * Process all pending sent messages.
97  */
98 inline static void process_sent_messages(void)
99 {
100     MSG msg;
101     MSG_peek_message( &msg, 0, 0, 0, GET_MSG_REMOVE | GET_MSG_SENT_ONLY );
102 }
103
104
105 /***********************************************************************
106  *           queue_hardware_message
107  *
108  * store a hardware message in the thread queue
109  */
110 static void queue_hardware_message( MSG *msg, ULONG_PTR extra_info, enum message_type type )
111 {
112     SERVER_START_REQ( send_message )
113     {
114         req->type   = type;
115         req->id     = GetWindowThreadProcessId( msg->hwnd, NULL );
116         req->win    = msg->hwnd;
117         req->msg    = msg->message;
118         req->wparam = msg->wParam;
119         req->lparam = msg->lParam;
120         req->x      = msg->pt.x;
121         req->y      = msg->pt.y;
122         req->time   = msg->time;
123         req->info   = extra_info;
124         req->timeout = 0;
125         wine_server_call( req );
126     }
127     SERVER_END_REQ;
128 }
129
130
131 /***********************************************************************
132  *           update_queue_key_state
133  */
134 static void update_queue_key_state( UINT msg, WPARAM wp, LPARAM lp )
135 {
136     BOOL down = FALSE, iskey = FALSE;
137     WPARAM dualkey = 0;
138
139     switch (msg)
140     {
141     case WM_LBUTTONDOWN:
142         down = TRUE;
143         /* fall through */
144     case WM_LBUTTONUP:
145         wp = VK_LBUTTON;
146         break;
147     case WM_MBUTTONDOWN:
148         down = TRUE;
149         /* fall through */
150     case WM_MBUTTONUP:
151         wp = VK_MBUTTON;
152         break;
153     case WM_RBUTTONDOWN:
154         down = TRUE;
155         /* fall through */
156     case WM_RBUTTONUP:
157         wp = VK_RBUTTON;
158         break;
159     case WM_KEYDOWN:
160     case WM_SYSKEYDOWN:
161         down = TRUE;
162         /* fall through */
163     case WM_KEYUP:
164     case WM_SYSKEYUP:
165         wp = wp & 0xff;
166         iskey = TRUE;
167         break;
168     }
169     if (iskey)
170     {
171         switch(wp)
172         {
173             case VK_SHIFT:
174                 dualkey = (HIWORD(lp) & KF_EXTENDED) ? VK_RSHIFT : VK_LSHIFT;
175                 break;
176             case VK_CONTROL:
177                 dualkey = (HIWORD(lp) & KF_EXTENDED) ? VK_RCONTROL : VK_LCONTROL;
178                 break;
179             case VK_MENU:
180                 dualkey = (HIWORD(lp) & KF_EXTENDED) ? VK_RMENU : VK_LMENU;
181                 break;
182             
183         }
184     }
185     if (down)
186     {
187         BYTE *p = &QueueKeyStateTable[wp];
188         if (!(*p & 0x80)) *p ^= 0x01;
189         *p |= 0x80;
190     }
191     else QueueKeyStateTable[wp] &= ~0x80;
192     if (dualkey)
193     { /* also update the "dual" keys properly */
194         if (down)
195         {
196             BYTE *p = &QueueKeyStateTable[dualkey];
197             if (!(*p & 0x80)) *p ^= 0x01;
198             *p |= 0x80;
199         }
200         else QueueKeyStateTable[dualkey] &= ~0x80;
201     }
202 }
203
204
205 /***********************************************************************
206  *           MSG_SendParentNotify
207  *
208  * Send a WM_PARENTNOTIFY to all ancestors of the given window, unless
209  * the window has the WS_EX_NOPARENTNOTIFY style.
210  */
211 static void MSG_SendParentNotify( HWND hwnd, WORD event, WORD idChild, POINT pt )
212 {
213     /* pt has to be in the client coordinates of the parent window */
214     MapWindowPoints( 0, hwnd, &pt, 1 );
215     for (;;)
216     {
217         HWND parent;
218
219         if (!(GetWindowLongA( hwnd, GWL_STYLE ) & WS_CHILD)) break;
220         if (GetWindowLongA( hwnd, GWL_EXSTYLE ) & WS_EX_NOPARENTNOTIFY) break;
221         if (!(parent = GetParent(hwnd))) break;
222         MapWindowPoints( hwnd, parent, &pt, 1 );
223         hwnd = parent;
224         SendMessageA( hwnd, WM_PARENTNOTIFY,
225                       MAKEWPARAM( event, idChild ), MAKELPARAM( pt.x, pt.y ) );
226     }
227 }
228
229
230 #if 0  /* this is broken for now, will require proper support in the server */
231
232 /***********************************************************************
233  *          MSG_JournalPlayBackMsg
234  *
235  * Get an EVENTMSG struct via call JOURNALPLAYBACK hook function
236  */
237 void MSG_JournalPlayBackMsg(void)
238 {
239     EVENTMSG tmpMsg;
240     MSG msg;
241     LRESULT wtime;
242     int keyDown,i;
243
244     wtime=HOOK_CallHooks( WH_JOURNALPLAYBACK, HC_GETNEXT, 0, (LPARAM)&tmpMsg, TRUE );
245     /*  TRACE(msg,"Playback wait time =%ld\n",wtime); */
246     if (wtime<=0)
247     {
248         wtime=0;
249         msg.message = tmpMsg.message;
250         msg.hwnd    = tmpMsg.hwnd;
251         msg.time    = tmpMsg.time;
252         if ((tmpMsg.message >= WM_KEYFIRST) && (tmpMsg.message <= WM_KEYLAST))
253         {
254             msg.wParam  = tmpMsg.paramL & 0xFF;
255             msg.lParam  = MAKELONG(tmpMsg.paramH&0x7ffff,tmpMsg.paramL>>8);
256             if (tmpMsg.message == WM_KEYDOWN || tmpMsg.message == WM_SYSKEYDOWN)
257             {
258                 for (keyDown=i=0; i<256 && !keyDown; i++)
259                     if (InputKeyStateTable[i] & 0x80)
260                         keyDown++;
261                 if (!keyDown)
262                     msg.lParam |= 0x40000000;
263                 InputKeyStateTable[msg.wParam] |= 0x80;
264                 AsyncKeyStateTable[msg.wParam] |= 0x80;
265             }
266             else                                       /* WM_KEYUP, WM_SYSKEYUP */
267             {
268                 msg.lParam |= 0xC0000000;
269                 InputKeyStateTable[msg.wParam] &= ~0x80;
270             }
271             if (InputKeyStateTable[VK_MENU] & 0x80)
272                 msg.lParam |= 0x20000000;
273             if (tmpMsg.paramH & 0x8000)              /*special_key bit*/
274                 msg.lParam |= 0x01000000;
275
276             msg.pt.x = msg.pt.y = 0;
277             queue_hardware_message( &msg, 0, MSG_HARDWARE_RAW );
278         }
279         else if ((tmpMsg.message>= WM_MOUSEFIRST) && (tmpMsg.message <= WM_MOUSELAST))
280         {
281             switch (tmpMsg.message)
282             {
283             case WM_LBUTTONDOWN:
284                 InputKeyStateTable[VK_LBUTTON] |= 0x80;
285                 AsyncKeyStateTable[VK_LBUTTON] |= 0x80;
286                 break;
287             case WM_LBUTTONUP:
288                 InputKeyStateTable[VK_LBUTTON] &= ~0x80;
289                 break;
290             case WM_MBUTTONDOWN:
291                 InputKeyStateTable[VK_MBUTTON] |= 0x80;
292                 AsyncKeyStateTable[VK_MBUTTON] |= 0x80;
293                 break;
294             case WM_MBUTTONUP:
295                 InputKeyStateTable[VK_MBUTTON] &= ~0x80;
296                 break;
297             case WM_RBUTTONDOWN:
298                 InputKeyStateTable[VK_RBUTTON] |= 0x80;
299                 AsyncKeyStateTable[VK_RBUTTON] |= 0x80;
300                 break;
301             case WM_RBUTTONUP:
302                 InputKeyStateTable[VK_RBUTTON] &= ~0x80;
303                 break;
304             }
305             SetCursorPos(tmpMsg.paramL,tmpMsg.paramH);
306             msg.lParam=MAKELONG(tmpMsg.paramL,tmpMsg.paramH);
307             msg.wParam=0;
308             if (InputKeyStateTable[VK_LBUTTON] & 0x80) msg.wParam |= MK_LBUTTON;
309             if (InputKeyStateTable[VK_MBUTTON] & 0x80) msg.wParam |= MK_MBUTTON;
310             if (InputKeyStateTable[VK_RBUTTON] & 0x80) msg.wParam |= MK_RBUTTON;
311
312             msg.pt.x = tmpMsg.paramL;
313             msg.pt.y = tmpMsg.paramH;
314             queue_hardware_message( &msg, 0, MSG_HARDWARE_RAW );
315         }
316         HOOK_CallHooks( WH_JOURNALPLAYBACK, HC_SKIP, 0, (LPARAM)&tmpMsg, TRUE );
317     }
318     else
319     {
320         if( tmpMsg.message == WM_QUEUESYNC ) HOOK_CallHooks( WH_CBT, HCBT_QS, 0, 0, TRUE );
321     }
322 }
323 #endif
324
325
326 /***********************************************************************
327  *          process_raw_keyboard_message
328  *
329  * returns TRUE if the contents of 'msg' should be passed to the application
330  */
331 static BOOL process_raw_keyboard_message( MSG *msg, ULONG_PTR extra_info )
332 {
333     EVENTMSG event;
334
335     if (!(msg->hwnd = GetFocus()))
336     {
337         /* Send the message to the active window instead,  */
338         /* translating messages to their WM_SYS equivalent */
339         msg->hwnd = GetActiveWindow();
340         if (msg->message < WM_SYSKEYDOWN) msg->message += WM_SYSKEYDOWN - WM_KEYDOWN;
341     }
342
343     event.message = msg->message;
344     event.hwnd    = msg->hwnd;
345     event.time    = msg->time;
346     event.paramL  = (msg->wParam & 0xFF) | (HIWORD(msg->lParam) << 8);
347     event.paramH  = msg->lParam & 0x7FFF;
348     if (HIWORD(msg->lParam) & 0x0100) event.paramH |= 0x8000; /* special_key - bit */
349     HOOK_CallHooks( WH_JOURNALRECORD, HC_ACTION, 0, (LPARAM)&event, TRUE );
350
351     /* if we are going to throw away the message, update the queue state now */
352     if (!msg->hwnd) update_queue_key_state( msg->message, msg->wParam, msg->lParam );
353
354     return (msg->hwnd != 0);
355 }
356
357
358 /***********************************************************************
359  *          process_cooked_keyboard_message
360  *
361  * returns TRUE if the contents of 'msg' should be passed to the application
362  */
363 static BOOL process_cooked_keyboard_message( MSG *msg, BOOL remove )
364 {
365     if (remove)
366     {
367         update_queue_key_state( msg->message, msg->wParam, msg->lParam );
368
369         /* Handle F1 key by sending out WM_HELP message */
370         if ((msg->message == WM_KEYUP) &&
371             (msg->wParam == VK_F1) &&
372             (msg->hwnd != GetDesktopWindow()) &&
373             !MENU_IsMenuActive())
374         {
375             HELPINFO hi;
376             hi.cbSize = sizeof(HELPINFO);
377             hi.iContextType = HELPINFO_WINDOW;
378             hi.iCtrlId = GetWindowLongA( msg->hwnd, GWL_ID );
379             hi.hItemHandle = msg->hwnd;
380             hi.dwContextId = GetWindowContextHelpId( msg->hwnd );
381             hi.MousePos = msg->pt;
382             SendMessageA(msg->hwnd, WM_HELP, 0, (LPARAM)&hi);
383         }
384     }
385
386     if (HOOK_CallHooks( WH_KEYBOARD, remove ? HC_ACTION : HC_NOREMOVE,
387                         LOWORD(msg->wParam), msg->lParam, TRUE ))
388     {
389         /* skip this message */
390         HOOK_CallHooks( WH_CBT, HCBT_KEYSKIPPED, LOWORD(msg->wParam), msg->lParam, TRUE );
391         return FALSE;
392     }
393     return TRUE;
394 }
395
396
397 /***********************************************************************
398  *          process_raw_mouse_message
399  *
400  * returns TRUE if the contents of 'msg' should be passed to the application
401  */
402 static BOOL process_raw_mouse_message( MSG *msg, ULONG_PTR extra_info )
403 {
404     static MSG clk_msg;
405
406     POINT pt;
407     INT hittest;
408     EVENTMSG event;
409     GUITHREADINFO info;
410
411     /* find the window to dispatch this mouse message to */
412
413     hittest = HTCLIENT;
414     GetGUIThreadInfo( GetCurrentThreadId(), &info );
415     if (!(msg->hwnd = info.hwndCapture))
416     {
417         /* If no capture HWND, find window which contains the mouse position.
418          * Also find the position of the cursor hot spot (hittest) */
419         HWND hWndScope = (HWND)extra_info;
420
421         if (!IsWindow(hWndScope)) hWndScope = 0;
422         if (!(msg->hwnd = WINPOS_WindowFromPoint( hWndScope, msg->pt, &hittest )))
423             msg->hwnd = GetDesktopWindow();
424     }
425
426     event.message = msg->message;
427     event.time    = msg->time;
428     event.hwnd    = msg->hwnd;
429     event.paramL  = msg->pt.x;
430     event.paramH  = msg->pt.y;
431     HOOK_CallHooks( WH_JOURNALRECORD, HC_ACTION, 0, (LPARAM)&event, TRUE );
432
433     /* translate double clicks */
434
435     if ((msg->message == WM_LBUTTONDOWN) ||
436         (msg->message == WM_RBUTTONDOWN) ||
437         (msg->message == WM_MBUTTONDOWN))
438     {
439         BOOL update = TRUE;
440         /* translate double clicks -
441          * note that ...MOUSEMOVEs can slip in between
442          * ...BUTTONDOWN and ...BUTTONDBLCLK messages */
443
444         if ((info.flags & (GUI_INMENUMODE|GUI_INMOVESIZE)) ||
445             hittest != HTCLIENT ||
446             (GetClassLongA( msg->hwnd, GCL_STYLE ) & CS_DBLCLKS))
447         {
448            if ((msg->message == clk_msg.message) &&
449                (msg->hwnd == clk_msg.hwnd) &&
450                (msg->time - clk_msg.time < GetDoubleClickTime()) &&
451                (abs(msg->pt.x - clk_msg.pt.x) < GetSystemMetrics(SM_CXDOUBLECLK)/2) &&
452                (abs(msg->pt.y - clk_msg.pt.y) < GetSystemMetrics(SM_CYDOUBLECLK)/2))
453            {
454                msg->message += (WM_LBUTTONDBLCLK - WM_LBUTTONDOWN);
455                clk_msg.message = 0;
456                update = FALSE;
457            }
458         }
459         /* update static double click conditions */
460         if (update) clk_msg = *msg;
461     }
462
463     pt = msg->pt;
464     /* Note: windows has no concept of a non-client wheel message */
465     if (hittest != HTCLIENT && msg->message != WM_MOUSEWHEEL)
466     {
467         msg->message += WM_NCMOUSEMOVE - WM_MOUSEMOVE;
468         msg->wParam = hittest;
469     }
470     else
471     {
472         /* coordinates don't get translated while tracking a menu */
473         /* FIXME: should differentiate popups and top-level menus */
474         if (!(info.flags & GUI_INMENUMODE)) ScreenToClient( msg->hwnd, &pt );
475     }
476     msg->lParam = MAKELONG( pt.x, pt.y );
477     return TRUE;
478 }
479
480
481 /***********************************************************************
482  *          process_cooked_mouse_message
483  *
484  * returns TRUE if the contents of 'msg' should be passed to the application
485  */
486 static BOOL process_cooked_mouse_message( MSG *msg, ULONG_PTR extra_info, BOOL remove )
487 {
488     MOUSEHOOKSTRUCT hook;
489     INT hittest = HTCLIENT;
490     UINT raw_message = msg->message;
491     BOOL eatMsg;
492
493     if (msg->message >= WM_NCMOUSEFIRST && msg->message <= WM_NCMOUSELAST)
494     {
495         raw_message += WM_MOUSEFIRST - WM_NCMOUSEFIRST;
496         hittest = msg->wParam;
497     }
498     if (raw_message == WM_LBUTTONDBLCLK ||
499         raw_message == WM_RBUTTONDBLCLK ||
500         raw_message == WM_MBUTTONDBLCLK)
501     {
502         raw_message += WM_LBUTTONDOWN - WM_LBUTTONDBLCLK;
503     }
504
505     if (remove) update_queue_key_state( raw_message, 0, 0 );
506
507     hook.pt           = msg->pt;
508     hook.hwnd         = msg->hwnd;
509     hook.wHitTestCode = hittest;
510     hook.dwExtraInfo  = extra_info;
511     if (HOOK_CallHooks( WH_MOUSE, remove ? HC_ACTION : HC_NOREMOVE,
512                         msg->message, (LPARAM)&hook, TRUE ))
513     {
514         hook.pt           = msg->pt;
515         hook.hwnd         = msg->hwnd;
516         hook.wHitTestCode = hittest;
517         hook.dwExtraInfo  = extra_info;
518         HOOK_CallHooks( WH_CBT, HCBT_CLICKSKIPPED, msg->message, (LPARAM)&hook, TRUE );
519         return FALSE;
520     }
521
522     if ((hittest == HTERROR) || (hittest == HTNOWHERE))
523     {
524         SendMessageA( msg->hwnd, WM_SETCURSOR, (WPARAM)msg->hwnd,
525                       MAKELONG( hittest, raw_message ));
526         return FALSE;
527     }
528
529     if (!remove || GetCapture()) return TRUE;
530
531     eatMsg = FALSE;
532
533     if ((raw_message == WM_LBUTTONDOWN) ||
534         (raw_message == WM_RBUTTONDOWN) ||
535         (raw_message == WM_MBUTTONDOWN))
536     {
537         /* Send the WM_PARENTNOTIFY,
538          * note that even for double/nonclient clicks
539          * notification message is still WM_L/M/RBUTTONDOWN.
540          */
541         MSG_SendParentNotify( msg->hwnd, raw_message, 0, msg->pt );
542
543         /* Activate the window if needed */
544
545         if (msg->hwnd != GetActiveWindow())
546         {
547             HWND hwndTop = msg->hwnd;
548             while (hwndTop)
549             {
550                 if ((GetWindowLongW( hwndTop, GWL_STYLE ) & (WS_POPUP|WS_CHILD)) != WS_CHILD) break;
551                 hwndTop = GetParent( hwndTop );
552             }
553
554             if (hwndTop && hwndTop != GetDesktopWindow())
555             {
556                 LONG ret = SendMessageA( msg->hwnd, WM_MOUSEACTIVATE, (WPARAM)hwndTop,
557                                          MAKELONG( hittest, raw_message ) );
558                 switch(ret)
559                 {
560                 case MA_NOACTIVATEANDEAT:
561                     eatMsg = TRUE;
562                     /* fall through */
563                 case MA_NOACTIVATE:
564                     break;
565                 case MA_ACTIVATEANDEAT:
566                     eatMsg = TRUE;
567                     /* fall through */
568                 case MA_ACTIVATE:
569                 case 0:
570                     if (!FOCUS_MouseActivate( hwndTop )) eatMsg = TRUE;
571                     break;
572                 default:
573                     WARN( "unknown WM_MOUSEACTIVATE code %ld\n", ret );
574                     break;
575                 }
576             }
577         }
578     }
579
580     /* send the WM_SETCURSOR message */
581
582     /* Windows sends the normal mouse message as the message parameter
583        in the WM_SETCURSOR message even if it's non-client mouse message */
584     SendMessageA( msg->hwnd, WM_SETCURSOR, (WPARAM)msg->hwnd,
585                   MAKELONG( hittest, raw_message ));
586
587     return !eatMsg;
588 }
589
590
591 /***********************************************************************
592  *          process_hardware_message
593  *
594  * returns TRUE if the contents of 'msg' should be passed to the application
595  */
596 BOOL MSG_process_raw_hardware_message( MSG *msg, ULONG_PTR extra_info, HWND hwnd_filter,
597                                        UINT first, UINT last, BOOL remove )
598 {
599     if (is_keyboard_message( msg->message ))
600     {
601         if (!process_raw_keyboard_message( msg, extra_info )) return FALSE;
602     }
603     else if (is_mouse_message( msg->message ))
604     {
605         if (!process_raw_mouse_message( msg, extra_info )) return FALSE;
606     }
607     else
608     {
609         ERR( "unknown message type %x\n", msg->message );
610         return FALSE;
611     }
612
613     /* check destination thread and filters */
614     if (!check_message_filter( msg, hwnd_filter, first, last ) ||
615         !WIN_IsCurrentThread( msg->hwnd ))
616     {
617         /* queue it for later, or for another thread */
618         queue_hardware_message( msg, extra_info, MSG_HARDWARE_COOKED );
619         return FALSE;
620     }
621
622     /* save the message in the cooked queue if we didn't want to remove it */
623     if (!remove) queue_hardware_message( msg, extra_info, MSG_HARDWARE_COOKED );
624     return TRUE;
625 }
626
627
628 /***********************************************************************
629  *          MSG_process_cooked_hardware_message
630  *
631  * returns TRUE if the contents of 'msg' should be passed to the application
632  */
633 BOOL MSG_process_cooked_hardware_message( MSG *msg, ULONG_PTR extra_info, BOOL remove )
634 {
635     if (is_keyboard_message( msg->message ))
636         return process_cooked_keyboard_message( msg, remove );
637
638     if (is_mouse_message( msg->message ))
639         return process_cooked_mouse_message( msg, extra_info, remove );
640
641     ERR( "unknown message type %x\n", msg->message );
642     return FALSE;
643 }
644
645
646 /**********************************************************************
647  *              GetKeyState (USER.106)
648  */
649 INT16 WINAPI GetKeyState16(INT16 vkey)
650 {
651     return GetKeyState(vkey);
652 }
653
654
655 /**********************************************************************
656  *              GetKeyState (USER32.@)
657  *
658  * An application calls the GetKeyState function in response to a
659  * keyboard-input message.  This function retrieves the state of the key
660  * at the time the input message was generated.  (SDK 3.1 Vol 2. p 390)
661  */
662 SHORT WINAPI GetKeyState(INT vkey)
663 {
664     INT retval;
665
666     if (vkey >= 'a' && vkey <= 'z') vkey += 'A' - 'a';
667     retval = ((WORD)(QueueKeyStateTable[vkey] & 0x80) << 8 ) |
668               (QueueKeyStateTable[vkey] & 0x80) |
669               (QueueKeyStateTable[vkey] & 0x01);
670     TRACE("key (0x%x) -> %x\n", vkey, retval);
671     return retval;
672 }
673
674
675 /**********************************************************************
676  *              GetKeyboardState (USER.222)
677  *              GetKeyboardState (USER32.@)
678  *
679  * An application calls the GetKeyboardState function in response to a
680  * keyboard input message.  This function retrieves the state of the keyboard
681  * at the time the input message was generated.  (SDK 3.1 Vol 2. p 387)
682  */
683 BOOL WINAPI GetKeyboardState(LPBYTE lpKeyState)
684 {
685     TRACE_(key)("(%p)\n", lpKeyState);
686     if (lpKeyState) memcpy(lpKeyState, QueueKeyStateTable, 256);
687     return TRUE;
688 }
689
690
691 /**********************************************************************
692  *              SetKeyboardState (USER.223)
693  *              SetKeyboardState (USER32.@)
694  */
695 BOOL WINAPI SetKeyboardState(LPBYTE lpKeyState)
696 {
697     TRACE_(key)("(%p)\n", lpKeyState);
698     if (lpKeyState) memcpy(QueueKeyStateTable, lpKeyState, 256);
699     return TRUE;
700 }
701
702
703 /***********************************************************************
704  *              WaitMessage (USER.112) Suspend thread pending messages
705  *              WaitMessage (USER32.@) Suspend thread pending messages
706  *
707  * WaitMessage() suspends a thread until events appear in the thread's
708  * queue.
709  */
710 BOOL WINAPI WaitMessage(void)
711 {
712     return (MsgWaitForMultipleObjectsEx( 0, NULL, INFINITE, QS_ALLINPUT, 0 ) != WAIT_FAILED);
713 }
714
715
716 /***********************************************************************
717  *              MsgWaitForMultipleObjectsEx   (USER32.@)
718  */
719 DWORD WINAPI MsgWaitForMultipleObjectsEx( DWORD count, CONST HANDLE *pHandles,
720                                           DWORD timeout, DWORD mask, DWORD flags )
721 {
722     HANDLE handles[MAXIMUM_WAIT_OBJECTS];
723     DWORD i, ret, lock;
724     MESSAGEQUEUE *msgQueue;
725
726     if (count > MAXIMUM_WAIT_OBJECTS-1)
727     {
728         SetLastError( ERROR_INVALID_PARAMETER );
729         return WAIT_FAILED;
730     }
731
732     if (!(msgQueue = QUEUE_Current())) return WAIT_FAILED;
733
734     /* set the queue mask */
735     SERVER_START_REQ( set_queue_mask )
736     {
737         req->wake_mask    = (flags & MWMO_INPUTAVAILABLE) ? mask : 0;
738         req->changed_mask = mask;
739         req->skip_wait    = 0;
740         wine_server_call( req );
741     }
742     SERVER_END_REQ;
743
744     /* Add the thread event to the handle list */
745     for (i = 0; i < count; i++) handles[i] = pHandles[i];
746     handles[count] = msgQueue->server_queue;
747
748     ReleaseThunkLock( &lock );
749     if (USER_Driver.pMsgWaitForMultipleObjectsEx)
750     {
751         ret = USER_Driver.pMsgWaitForMultipleObjectsEx( count+1, handles, timeout, mask, flags );
752         if (ret == count+1) ret = count; /* pretend the msg queue is ready */
753     }
754     else
755         ret = WaitForMultipleObjectsEx( count+1, handles, flags & MWMO_WAITALL,
756                                         timeout, flags & MWMO_ALERTABLE );
757     if (lock) RestoreThunkLock( lock );
758     return ret;
759 }
760
761
762 /***********************************************************************
763  *              MsgWaitForMultipleObjects (USER32.@)
764  */
765 DWORD WINAPI MsgWaitForMultipleObjects( DWORD count, CONST HANDLE *handles,
766                                         BOOL wait_all, DWORD timeout, DWORD mask )
767 {
768     return MsgWaitForMultipleObjectsEx( count, handles, timeout, mask,
769                                         wait_all ? MWMO_WAITALL : 0 );
770 }
771
772
773 /***********************************************************************
774  *              WaitForInputIdle (USER32.@)
775  */
776 DWORD WINAPI WaitForInputIdle( HANDLE hProcess, DWORD dwTimeOut )
777 {
778     DWORD start_time, elapsed, ret;
779     HANDLE idle_event = (HANDLE)-1;
780
781     SERVER_START_REQ( wait_input_idle )
782     {
783         req->handle = hProcess;
784         req->timeout = dwTimeOut;
785         if (!(ret = wine_server_call_err( req ))) idle_event = reply->event;
786     }
787     SERVER_END_REQ;
788     if (ret) return WAIT_FAILED;  /* error */
789     if (!idle_event) return 0;  /* no event to wait on */
790
791     start_time = GetTickCount();
792     elapsed = 0;
793
794     TRACE("waiting for %p\n", idle_event );
795     do
796     {
797         ret = MsgWaitForMultipleObjects ( 1, &idle_event, FALSE, dwTimeOut - elapsed, QS_SENDMESSAGE );
798         switch (ret)
799         {
800         case WAIT_OBJECT_0+1:
801             process_sent_messages();
802             break;
803         case WAIT_TIMEOUT:
804         case WAIT_FAILED:
805             TRACE("timeout or error\n");
806             return ret;
807         default:
808             TRACE("finished\n");
809             return 0;
810         }
811         if (dwTimeOut != INFINITE)
812         {
813             elapsed = GetTickCount() - start_time;
814             if (elapsed > dwTimeOut)
815                 break;
816         }
817     }
818     while (1);
819
820     return WAIT_TIMEOUT;
821 }
822
823
824 /***********************************************************************
825  *              UserYield (USER.332)
826  */
827 void WINAPI UserYield16(void)
828 {
829    DWORD count;
830
831     /* Handle sent messages */
832     process_sent_messages();
833
834     /* Yield */
835     ReleaseThunkLock(&count);
836     if (count)
837     {
838         RestoreThunkLock(count);
839         /* Handle sent messages again */
840         process_sent_messages();
841     }
842 }
843
844
845 struct accent_char
846 {
847     BYTE ac_accent;
848     BYTE ac_char;
849     BYTE ac_result;
850 };
851
852 static const struct accent_char accent_chars[] =
853 {
854 /* A good idea should be to read /usr/X11/lib/X11/locale/iso8859-x/Compose */
855     {'`', 'A', '\300'},  {'`', 'a', '\340'},
856     {'\'', 'A', '\301'}, {'\'', 'a', '\341'},
857     {'^', 'A', '\302'},  {'^', 'a', '\342'},
858     {'~', 'A', '\303'},  {'~', 'a', '\343'},
859     {'"', 'A', '\304'},  {'"', 'a', '\344'},
860     {'O', 'A', '\305'},  {'o', 'a', '\345'},
861     {'0', 'A', '\305'},  {'0', 'a', '\345'},
862     {'A', 'A', '\305'},  {'a', 'a', '\345'},
863     {'A', 'E', '\306'},  {'a', 'e', '\346'},
864     {',', 'C', '\307'},  {',', 'c', '\347'},
865     {'`', 'E', '\310'},  {'`', 'e', '\350'},
866     {'\'', 'E', '\311'}, {'\'', 'e', '\351'},
867     {'^', 'E', '\312'},  {'^', 'e', '\352'},
868     {'"', 'E', '\313'},  {'"', 'e', '\353'},
869     {'`', 'I', '\314'},  {'`', 'i', '\354'},
870     {'\'', 'I', '\315'}, {'\'', 'i', '\355'},
871     {'^', 'I', '\316'},  {'^', 'i', '\356'},
872     {'"', 'I', '\317'},  {'"', 'i', '\357'},
873     {'-', 'D', '\320'},  {'-', 'd', '\360'},
874     {'~', 'N', '\321'},  {'~', 'n', '\361'},
875     {'`', 'O', '\322'},  {'`', 'o', '\362'},
876     {'\'', 'O', '\323'}, {'\'', 'o', '\363'},
877     {'^', 'O', '\324'},  {'^', 'o', '\364'},
878     {'~', 'O', '\325'},  {'~', 'o', '\365'},
879     {'"', 'O', '\326'},  {'"', 'o', '\366'},
880     {'/', 'O', '\330'},  {'/', 'o', '\370'},
881     {'`', 'U', '\331'},  {'`', 'u', '\371'},
882     {'\'', 'U', '\332'}, {'\'', 'u', '\372'},
883     {'^', 'U', '\333'},  {'^', 'u', '\373'},
884     {'"', 'U', '\334'},  {'"', 'u', '\374'},
885     {'\'', 'Y', '\335'}, {'\'', 'y', '\375'},
886     {'T', 'H', '\336'},  {'t', 'h', '\376'},
887     {'s', 's', '\337'},  {'"', 'y', '\377'},
888     {'s', 'z', '\337'},  {'i', 'j', '\377'},
889         /* iso-8859-2 uses this */
890     {'<', 'L', '\245'},  {'<', 'l', '\265'},    /* caron */
891     {'<', 'S', '\251'},  {'<', 's', '\271'},
892     {'<', 'T', '\253'},  {'<', 't', '\273'},
893     {'<', 'Z', '\256'},  {'<', 'z', '\276'},
894     {'<', 'C', '\310'},  {'<', 'c', '\350'},
895     {'<', 'E', '\314'},  {'<', 'e', '\354'},
896     {'<', 'D', '\317'},  {'<', 'd', '\357'},
897     {'<', 'N', '\322'},  {'<', 'n', '\362'},
898     {'<', 'R', '\330'},  {'<', 'r', '\370'},
899     {';', 'A', '\241'},  {';', 'a', '\261'},    /* ogonek */
900     {';', 'E', '\312'},  {';', 'e', '\332'},
901     {'\'', 'Z', '\254'}, {'\'', 'z', '\274'},   /* acute */
902     {'\'', 'R', '\300'}, {'\'', 'r', '\340'},
903     {'\'', 'L', '\305'}, {'\'', 'l', '\345'},
904     {'\'', 'C', '\306'}, {'\'', 'c', '\346'},
905     {'\'', 'N', '\321'}, {'\'', 'n', '\361'},
906 /*  collision whith S, from iso-8859-9 !!! */
907     {',', 'S', '\252'},  {',', 's', '\272'},    /* cedilla */
908     {',', 'T', '\336'},  {',', 't', '\376'},
909     {'.', 'Z', '\257'},  {'.', 'z', '\277'},    /* dot above */
910     {'/', 'L', '\243'},  {'/', 'l', '\263'},    /* slash */
911     {'/', 'D', '\320'},  {'/', 'd', '\360'},
912     {'(', 'A', '\303'},  {'(', 'a', '\343'},    /* breve */
913     {'\275', 'O', '\325'}, {'\275', 'o', '\365'},       /* double acute */
914     {'\275', 'U', '\334'}, {'\275', 'u', '\374'},
915     {'0', 'U', '\332'},  {'0', 'u', '\372'},    /* ring above */
916         /* iso-8859-3 uses this */
917     {'/', 'H', '\241'},  {'/', 'h', '\261'},    /* slash */
918     {'>', 'H', '\246'},  {'>', 'h', '\266'},    /* circumflex */
919     {'>', 'J', '\254'},  {'>', 'j', '\274'},
920     {'>', 'C', '\306'},  {'>', 'c', '\346'},
921     {'>', 'G', '\330'},  {'>', 'g', '\370'},
922     {'>', 'S', '\336'},  {'>', 's', '\376'},
923 /*  collision whith G( from iso-8859-9 !!!   */
924     {'(', 'G', '\253'},  {'(', 'g', '\273'},    /* breve */
925     {'(', 'U', '\335'},  {'(', 'u', '\375'},
926 /*  collision whith I. from iso-8859-3 !!!   */
927     {'.', 'I', '\251'},  {'.', 'i', '\271'},    /* dot above */
928     {'.', 'C', '\305'},  {'.', 'c', '\345'},
929     {'.', 'G', '\325'},  {'.', 'g', '\365'},
930         /* iso-8859-4 uses this */
931     {',', 'R', '\243'},  {',', 'r', '\263'},    /* cedilla */
932     {',', 'L', '\246'},  {',', 'l', '\266'},
933     {',', 'G', '\253'},  {',', 'g', '\273'},
934     {',', 'N', '\321'},  {',', 'n', '\361'},
935     {',', 'K', '\323'},  {',', 'k', '\363'},
936     {'~', 'I', '\245'},  {'~', 'i', '\265'},    /* tilde */
937     {'-', 'E', '\252'},  {'-', 'e', '\272'},    /* macron */
938     {'-', 'A', '\300'},  {'-', 'a', '\340'},
939     {'-', 'I', '\317'},  {'-', 'i', '\357'},
940     {'-', 'O', '\322'},  {'-', 'o', '\362'},
941     {'-', 'U', '\336'},  {'-', 'u', '\376'},
942     {'/', 'T', '\254'},  {'/', 't', '\274'},    /* slash */
943     {'.', 'E', '\314'},  {'.', 'e', '\344'},    /* dot above */
944     {';', 'I', '\307'},  {';', 'i', '\347'},    /* ogonek */
945     {';', 'U', '\331'},  {';', 'u', '\371'},
946         /* iso-8859-9 uses this */
947         /* iso-8859-9 has really bad choosen G( S, and I. as they collide
948          * whith the same letters on other iso-8859-x (that is they are on
949          * different places :-( ), if you use turkish uncomment these and
950          * comment out the lines in iso-8859-2 and iso-8859-3 sections
951          * FIXME: should be dynamic according to chosen language
952          *        if/when Wine has turkish support.
953          */
954 /*  collision whith G( from iso-8859-3 !!!   */
955 /*  {'(', 'G', '\320'},  {'(', 'g', '\360'}, */ /* breve */
956 /*  collision whith S, from iso-8859-2 !!! */
957 /*  {',', 'S', '\336'},  {',', 's', '\376'}, */ /* cedilla */
958 /*  collision whith I. from iso-8859-3 !!!   */
959 /*  {'.', 'I', '\335'},  {'.', 'i', '\375'}, */ /* dot above */
960 };
961
962
963 /***********************************************************************
964  *              TranslateMessage (USER32.@)
965  *
966  * Implementation of TranslateMessage.
967  *
968  * TranslateMessage translates virtual-key messages into character-messages,
969  * as follows :
970  * WM_KEYDOWN/WM_KEYUP combinations produce a WM_CHAR or WM_DEADCHAR message.
971  * ditto replacing WM_* with WM_SYS*
972  * This produces WM_CHAR messages only for keys mapped to ASCII characters
973  * by the keyboard driver.
974  *
975  * If the message is WM_KEYDOWN, WM_KEYUP, WM_SYSKEYDOWN, or WM_SYSKEYUP, the
976  * return value is nonzero, regardless of the translation.
977  *
978  */
979 BOOL WINAPI TranslateMessage( const MSG *msg )
980 {
981     static int dead_char;
982     UINT message;
983     WCHAR wp[2];
984     BOOL rc = FALSE;
985
986     if (msg->message >= WM_KEYFIRST && msg->message <= WM_KEYLAST)
987     {
988         TRACE_(key)("(%s, %04X, %08lX)\n",
989                     SPY_GetMsgName(msg->message, msg->hwnd), msg->wParam, msg->lParam );
990
991         /* Return code must be TRUE no matter what! */
992         rc = TRUE;
993     }
994
995     if ((msg->message != WM_KEYDOWN) && (msg->message != WM_SYSKEYDOWN)) return rc;
996
997     TRACE_(key)("Translating key %s (%04x), scancode %02x\n",
998                  SPY_GetVKeyName(msg->wParam), msg->wParam, LOBYTE(HIWORD(msg->lParam)));
999
1000     /* FIXME : should handle ToUnicode yielding 2 */
1001     switch (ToUnicode(msg->wParam, HIWORD(msg->lParam), QueueKeyStateTable, wp, 2, 0))
1002     {
1003     case 1:
1004         message = (msg->message == WM_KEYDOWN) ? WM_CHAR : WM_SYSCHAR;
1005         /* Should dead chars handling go in ToAscii ? */
1006         if (dead_char)
1007         {
1008             int i;
1009
1010             if (wp[0] == ' ') wp[0] =  dead_char;
1011             if (dead_char == 0xa2) dead_char = '(';
1012             else if (dead_char == 0xa8) dead_char = '"';
1013             else if (dead_char == 0xb2) dead_char = ';';
1014             else if (dead_char == 0xb4) dead_char = '\'';
1015             else if (dead_char == 0xb7) dead_char = '<';
1016             else if (dead_char == 0xb8) dead_char = ',';
1017             else if (dead_char == 0xff) dead_char = '.';
1018             for (i = 0; i < sizeof(accent_chars)/sizeof(accent_chars[0]); i++)
1019                 if ((accent_chars[i].ac_accent == dead_char) &&
1020                     (accent_chars[i].ac_char == wp[0]))
1021                 {
1022                     wp[0] = accent_chars[i].ac_result;
1023                     break;
1024                 }
1025             dead_char = 0;
1026         }
1027         TRACE_(key)("1 -> PostMessage(%s)\n", SPY_GetMsgName(message, msg->hwnd));
1028         PostMessageW( msg->hwnd, message, wp[0], msg->lParam );
1029         break;
1030
1031     case -1:
1032         message = (msg->message == WM_KEYDOWN) ? WM_DEADCHAR : WM_SYSDEADCHAR;
1033         dead_char = wp[0];
1034         TRACE_(key)("-1 -> PostMessage(%s)\n", SPY_GetMsgName(message, msg->hwnd));
1035         PostMessageW( msg->hwnd, message, wp[0], msg->lParam );
1036         return TRUE;
1037     }
1038     return rc;
1039 }
1040
1041
1042 /***********************************************************************
1043  *              DispatchMessageA (USER32.@)
1044  */
1045 LONG WINAPI DispatchMessageA( const MSG* msg )
1046 {
1047     WND * wndPtr;
1048     LONG retval;
1049     int painting;
1050     WNDPROC winproc;
1051
1052       /* Process timer messages */
1053     if ((msg->message == WM_TIMER) || (msg->message == WM_SYSTIMER))
1054     {
1055         if (msg->lParam)
1056         {
1057 /*            HOOK_CallHooks32A( WH_CALLWNDPROC, HC_ACTION, 0, FIXME ); */
1058
1059             /* before calling window proc, verify whether timer is still valid;
1060                there's a slim chance that the application kills the timer
1061                between GetMessage and DispatchMessage API calls */
1062             if (!TIMER_IsTimerValid(msg->hwnd, (UINT) msg->wParam, (WNDPROC)msg->lParam))
1063                 return 0; /* invalid winproc */
1064
1065             return CallWindowProcA( (WNDPROC)msg->lParam, msg->hwnd,
1066                                    msg->message, msg->wParam, GetTickCount() );
1067         }
1068     }
1069
1070     if (!(wndPtr = WIN_GetPtr( msg->hwnd )))
1071     {
1072         if (msg->hwnd) SetLastError( ERROR_INVALID_WINDOW_HANDLE );
1073         return 0;
1074     }
1075     if (wndPtr == WND_OTHER_PROCESS)
1076     {
1077         if (IsWindow( msg->hwnd ))
1078             ERR( "cannot dispatch msg to other process window %p\n", msg->hwnd );
1079         SetLastError( ERROR_INVALID_WINDOW_HANDLE );
1080         return 0;
1081     }
1082     if (!(winproc = wndPtr->winproc))
1083     {
1084         WIN_ReleasePtr( wndPtr );
1085         return 0;
1086     }
1087     painting = (msg->message == WM_PAINT);
1088     if (painting) wndPtr->flags |= WIN_NEEDS_BEGINPAINT;
1089     WIN_ReleasePtr( wndPtr );
1090 /*    hook_CallHooks32A( WH_CALLWNDPROC, HC_ACTION, 0, FIXME ); */
1091
1092     SPY_EnterMessage( SPY_DISPATCHMESSAGE, msg->hwnd, msg->message,
1093                       msg->wParam, msg->lParam );
1094     retval = CallWindowProcA( winproc, msg->hwnd, msg->message,
1095                               msg->wParam, msg->lParam );
1096     SPY_ExitMessage( SPY_RESULT_OK, msg->hwnd, msg->message, retval,
1097                      msg->wParam, msg->lParam );
1098
1099     if (painting && (wndPtr = WIN_GetPtr( msg->hwnd )) && (wndPtr != WND_OTHER_PROCESS))
1100     {
1101         BOOL validate = ((wndPtr->flags & WIN_NEEDS_BEGINPAINT) && wndPtr->hrgnUpdate);
1102         wndPtr->flags &= ~WIN_NEEDS_BEGINPAINT;
1103         WIN_ReleasePtr( wndPtr );
1104         if (validate)
1105         {
1106             ERR( "BeginPaint not called on WM_PAINT for hwnd %p!\n", msg->hwnd );
1107             /* Validate the update region to avoid infinite WM_PAINT loop */
1108             RedrawWindow( msg->hwnd, NULL, 0,
1109                           RDW_NOFRAME | RDW_VALIDATE | RDW_NOCHILDREN | RDW_NOINTERNALPAINT );
1110         }
1111     }
1112     return retval;
1113 }
1114
1115
1116 /***********************************************************************
1117  *              DispatchMessageW (USER32.@) Process Message
1118  *
1119  * Process the message specified in the structure *_msg_.
1120  *
1121  * If the lpMsg parameter points to a WM_TIMER message and the
1122  * parameter of the WM_TIMER message is not NULL, the lParam parameter
1123  * points to the function that is called instead of the window
1124  * procedure.
1125  *
1126  * The message must be valid.
1127  *
1128  * RETURNS
1129  *
1130  *   DispatchMessage() returns the result of the window procedure invoked.
1131  *
1132  * CONFORMANCE
1133  *
1134  *   ECMA-234, Win32
1135  *
1136  */
1137 LONG WINAPI DispatchMessageW( const MSG* msg )
1138 {
1139     WND * wndPtr;
1140     LONG retval;
1141     int painting;
1142     WNDPROC winproc;
1143
1144       /* Process timer messages */
1145     if ((msg->message == WM_TIMER) || (msg->message == WM_SYSTIMER))
1146     {
1147         if (msg->lParam)
1148         {
1149 /*            HOOK_CallHooks32W( WH_CALLWNDPROC, HC_ACTION, 0, FIXME ); */
1150
1151             /* before calling window proc, verify whether timer is still valid;
1152                there's a slim chance that the application kills the timer
1153                between GetMessage and DispatchMessage API calls */
1154             if (!TIMER_IsTimerValid(msg->hwnd, (UINT) msg->wParam, (WNDPROC)msg->lParam))
1155                 return 0; /* invalid winproc */
1156
1157             return CallWindowProcW( (WNDPROC)msg->lParam, msg->hwnd,
1158                                    msg->message, msg->wParam, GetTickCount() );
1159         }
1160     }
1161
1162     if (!(wndPtr = WIN_GetPtr( msg->hwnd )))
1163     {
1164         if (msg->hwnd) SetLastError( ERROR_INVALID_WINDOW_HANDLE );
1165         return 0;
1166     }
1167     if (wndPtr == WND_OTHER_PROCESS)
1168     {
1169         if (IsWindow( msg->hwnd ))
1170             ERR( "cannot dispatch msg to other process window %p\n", msg->hwnd );
1171         SetLastError( ERROR_INVALID_WINDOW_HANDLE );
1172         return 0;
1173     }
1174     if (!(winproc = wndPtr->winproc))
1175     {
1176         WIN_ReleasePtr( wndPtr );
1177         return 0;
1178     }
1179     painting = (msg->message == WM_PAINT);
1180     if (painting) wndPtr->flags |= WIN_NEEDS_BEGINPAINT;
1181     WIN_ReleasePtr( wndPtr );
1182 /*    HOOK_CallHooks32W( WH_CALLWNDPROC, HC_ACTION, 0, FIXME ); */
1183
1184     SPY_EnterMessage( SPY_DISPATCHMESSAGE, msg->hwnd, msg->message,
1185                       msg->wParam, msg->lParam );
1186     retval = CallWindowProcW( winproc, msg->hwnd, msg->message,
1187                               msg->wParam, msg->lParam );
1188     SPY_ExitMessage( SPY_RESULT_OK, msg->hwnd, msg->message, retval,
1189                      msg->wParam, msg->lParam );
1190
1191     if (painting && (wndPtr = WIN_GetPtr( msg->hwnd )) && (wndPtr != WND_OTHER_PROCESS))
1192     {
1193         BOOL validate = ((wndPtr->flags & WIN_NEEDS_BEGINPAINT) && wndPtr->hrgnUpdate);
1194         wndPtr->flags &= ~WIN_NEEDS_BEGINPAINT;
1195         WIN_ReleasePtr( wndPtr );
1196         if (validate)
1197         {
1198             ERR( "BeginPaint not called on WM_PAINT for hwnd %p!\n", msg->hwnd );
1199             /* Validate the update region to avoid infinite WM_PAINT loop */
1200             RedrawWindow( msg->hwnd, NULL, 0,
1201                           RDW_NOFRAME | RDW_VALIDATE | RDW_NOCHILDREN | RDW_NOINTERNALPAINT );
1202         }
1203     }
1204     return retval;
1205 }
1206
1207
1208 /***********************************************************************
1209  *              RegisterWindowMessage (USER.118)
1210  *              RegisterWindowMessageA (USER32.@)
1211  */
1212 WORD WINAPI RegisterWindowMessageA( LPCSTR str )
1213 {
1214     TRACE("%s\n", str );
1215     return GlobalAddAtomA( str );
1216 }
1217
1218
1219 /***********************************************************************
1220  *              RegisterWindowMessageW (USER32.@)
1221  */
1222 WORD WINAPI RegisterWindowMessageW( LPCWSTR str )
1223 {
1224     TRACE("%p\n", str );
1225     return GlobalAddAtomW( str );
1226 }
1227
1228
1229 /***********************************************************************
1230  *              BroadcastSystemMessage  (USER32.@)
1231  *              BroadcastSystemMessageA (USER32.@)
1232  */
1233 LONG WINAPI BroadcastSystemMessage(
1234         DWORD dwFlags,LPDWORD recipients,UINT uMessage,WPARAM wParam,
1235         LPARAM lParam )
1236 {
1237     if ((*recipients & BSM_APPLICATIONS)||
1238         (*recipients == BSM_ALLCOMPONENTS))
1239     {
1240         FIXME("(%08lx,%08lx,%08x,%08x,%08lx): semi-stub!\n",
1241               dwFlags,*recipients,uMessage,wParam,lParam);
1242         PostMessageA(HWND_BROADCAST,uMessage,wParam,lParam);
1243         return 1;
1244     }
1245     else
1246     {
1247         FIXME("(%08lx,%08lx,%08x,%08x,%08lx): stub!\n",
1248               dwFlags,*recipients,uMessage,wParam,lParam);
1249         return -1;
1250     }
1251 }