Moved idle event handling to the server.
[wine] / windows / message.c
1 /*
2  * Message queues related functions
3  *
4  * Copyright 1993, 1994 Alexandre Julliard
5  */
6
7 #include <stdlib.h>
8 #include <string.h>
9 #include <ctype.h>
10 #include <sys/time.h>
11 #include <sys/types.h>
12
13 #include "wine/winbase16.h"
14 #include "message.h"
15 #include "winerror.h"
16 #include "win.h"
17 #include "heap.h"
18 #include "hook.h"
19 #include "input.h"
20 #include "spy.h"
21 #include "winpos.h"
22 #include "dde.h"
23 #include "queue.h"
24 #include "winproc.h"
25 #include "task.h"
26 #include "selectors.h"
27 #include "thread.h"
28 #include "options.h"
29 #include "menu.h"
30 #include "struct32.h"
31 #include "debugtools.h"
32
33 DEFAULT_DEBUG_CHANNEL(msg);
34 DECLARE_DEBUG_CHANNEL(key);
35 DECLARE_DEBUG_CHANNEL(sendmsg);
36
37 #define WM_NCMOUSEFIRST         WM_NCMOUSEMOVE
38 #define WM_NCMOUSELAST          WM_NCMBUTTONDBLCLK
39
40     
41 typedef enum { SYSQ_MSG_ABANDON, SYSQ_MSG_SKIP, 
42                SYSQ_MSG_ACCEPT, SYSQ_MSG_CONTINUE } SYSQ_STATUS;
43
44 extern HQUEUE16 hCursorQueue;                    /* queue.c */
45
46 DWORD MSG_WineStartTicks; /* Ticks at Wine startup */
47
48 static UINT doubleClickSpeed = 452;
49
50 /***********************************************************************
51  *           MSG_CheckFilter
52  */
53 static BOOL MSG_CheckFilter(DWORD uMsg, DWORD first, DWORD last)
54 {
55    if( first || last )
56        return (uMsg >= first && uMsg <= last);
57    return TRUE;
58 }
59
60 /***********************************************************************
61  *           MSG_SendParentNotify
62  *
63  * Send a WM_PARENTNOTIFY to all ancestors of the given window, unless
64  * the window has the WS_EX_NOPARENTNOTIFY style.
65  */
66 static void MSG_SendParentNotify(WND* wndPtr, WORD event, WORD idChild, LPARAM lValue)
67 {
68 #define lppt ((LPPOINT16)&lValue)
69
70     /* pt has to be in the client coordinates of the parent window */
71     WND *tmpWnd = WIN_LockWndPtr(wndPtr);
72
73     MapWindowPoints16( 0, tmpWnd->hwndSelf, lppt, 1 );
74     while (tmpWnd)
75     {
76         if (!(tmpWnd->dwStyle & WS_CHILD) || (tmpWnd->dwExStyle & WS_EX_NOPARENTNOTIFY))
77     {
78             WIN_ReleaseWndPtr(tmpWnd);
79             break;
80         }
81         lppt->x += tmpWnd->rectClient.left;
82         lppt->y += tmpWnd->rectClient.top;
83         WIN_UpdateWndPtr(&tmpWnd,tmpWnd->parent);
84         SendMessageA( tmpWnd->hwndSelf, WM_PARENTNOTIFY,
85                         MAKEWPARAM( event, idChild ), lValue );
86     }
87
88 #undef lppt
89 }
90
91
92 /***********************************************************************
93  *           MSG_TranslateMouseMsg
94  *
95  * Translate an mouse hardware event into a real mouse message.
96  *
97  * Returns:
98  *
99  *  SYSQ_MSG_ABANDON  - abandon the peek message loop
100  *  SYSQ_MSG_CONTINUE - leave the message in the queue and
101  *                      continue with the translation loop
102  *  SYSQ_MSG_ACCEPT   - proceed to process the translated message
103  */
104 static DWORD MSG_TranslateMouseMsg( HWND hTopWnd, DWORD first, DWORD last,
105                                     MSG *msg, BOOL remove, WND* pWndScope,
106                                     INT16 *pHitTest, POINT16 *pScreen_pt, BOOL *pmouseClick )
107 {
108     static DWORD   dblclk_time_limit = 0;
109     static UINT16     clk_message = 0;
110     static HWND16     clk_hwnd = 0;
111     static POINT16    clk_pos = { 0, 0 };
112
113     WND *pWnd;
114     HWND hWnd;
115     INT16 ht, hittest;
116     UINT message = msg->message;
117     POINT16 screen_pt, pt;
118     HANDLE16 hQ = GetFastQueue16();
119     MESSAGEQUEUE *queue = (MESSAGEQUEUE *)QUEUE_Lock(hQ);
120     BOOL mouseClick = ((message == WM_LBUTTONDOWN) ||
121                          (message == WM_RBUTTONDOWN) ||
122                          (message == WM_MBUTTONDOWN))?1:0;
123     DWORD retvalue;
124
125     /* Find the window to dispatch this mouse message to */
126
127     CONV_POINT32TO16( &msg->pt, &pt );
128     
129     hWnd = GetCapture();
130
131     /* If no capture HWND, find window which contains the mouse position.
132      * Also find the position of the cursor hot spot (hittest) */
133     if( !hWnd )
134     {
135         ht = hittest = WINPOS_WindowFromPoint( pWndScope, pt, &pWnd );
136         if( !pWnd ) pWnd = WIN_GetDesktop();
137         else WIN_LockWndPtr(pWnd);
138         hWnd = pWnd->hwndSelf;
139     } 
140     else 
141     {
142         ht = hittest = HTCLIENT;
143         pWnd = WIN_FindWndPtr(hWnd);
144         if (queue)
145             ht = PERQDATA_GetCaptureInfo( queue->pQData );
146     }
147
148     /* Save hittest for return */
149     *pHitTest = hittest;
150         
151         /* stop if not the right queue */
152
153     if (pWnd->hmemTaskQ != hQ)
154     {
155         /* Not for the current task */
156         if (queue) QUEUE_ClearWakeBit( queue, QS_MOUSE );
157         /* Wake up the other task */
158         QUEUE_Unlock( queue );
159         queue = (MESSAGEQUEUE *)QUEUE_Lock( pWnd->hmemTaskQ );
160         if (queue) QUEUE_SetWakeBit( queue, QS_MOUSE );
161
162         QUEUE_Unlock( queue );
163         retvalue = SYSQ_MSG_ABANDON;
164         goto END;
165     }
166
167         /* check if hWnd is within hWndScope */
168
169     if( hTopWnd && hWnd != hTopWnd )
170         if( !IsChild(hTopWnd, hWnd) )
171         {
172             QUEUE_Unlock( queue );
173             retvalue = SYSQ_MSG_CONTINUE;
174             goto END;
175         }
176
177     /* Was it a mouse click message */
178     if( mouseClick )
179     {
180         /* translate double clicks -
181          * note that ...MOUSEMOVEs can slip in between
182          * ...BUTTONDOWN and ...BUTTONDBLCLK messages */
183
184         if(GetClassLongA(hWnd, GCL_STYLE) & CS_DBLCLKS || ht != HTCLIENT )
185         {
186            if ((message == clk_message) && (hWnd == clk_hwnd) &&
187                (msg->time - dblclk_time_limit < doubleClickSpeed) &&
188                (abs(msg->pt.x - clk_pos.x) < GetSystemMetrics(SM_CXDOUBLECLK)/2) &&
189                (abs(msg->pt.y - clk_pos.y) < GetSystemMetrics(SM_CYDOUBLECLK)/2))
190            {
191               message += (WM_LBUTTONDBLCLK - WM_LBUTTONDOWN);
192               mouseClick++;   /* == 2 */
193            }
194         }
195     }
196     /* save mouse position */
197     screen_pt = pt;
198     *pScreen_pt = pt;
199
200     if (hittest != HTCLIENT)
201     {
202         message += WM_NCMOUSEMOVE - WM_MOUSEMOVE;
203         msg->wParam = hittest;
204     }
205     else
206         ScreenToClient16( hWnd, &pt );
207
208         /* check message filter */
209
210     if (!MSG_CheckFilter(message, first, last))
211     {
212         QUEUE_Unlock(queue);
213         retvalue = SYSQ_MSG_CONTINUE;
214         goto END;
215     }
216
217     /* Update global hCursorQueue */
218     hCursorQueue = queue->self;
219     
220     /* Update static double click conditions */
221     if( remove && mouseClick )
222     {
223         if( mouseClick == 1 )
224         {
225             /* set conditions */
226             dblclk_time_limit = msg->time;
227                clk_message = msg->message;
228                clk_hwnd = hWnd;
229                clk_pos = screen_pt;
230         } else 
231             /* got double click - zero them out */
232             dblclk_time_limit = clk_hwnd = 0;
233     }
234
235     QUEUE_Unlock(queue);
236
237     /* Update message params */
238     msg->hwnd    = hWnd;
239     msg->message = message;
240     msg->lParam  = MAKELONG( pt.x, pt.y );
241     retvalue = SYSQ_MSG_ACCEPT;
242
243 END:
244     WIN_ReleaseWndPtr(pWnd);
245
246     /* Return mouseclick flag */
247     *pmouseClick = mouseClick;
248     
249     return retvalue;
250 }
251
252
253 /***********************************************************************
254  *           MSG_ProcessMouseMsg
255  *
256  * Processes a translated mouse hardware event.
257  * The passed in msg structure should contain the updated hWnd, 
258  * lParam, wParam and message fields from MSG_TranslateMouseMsg.
259  *
260  * Returns:
261  *
262  *  SYSQ_MSG_SKIP     - Message should be skipped entirely (in this case
263  *                      HIWORD contains hit test code). Continue translating..
264  *  SYSQ_MSG_ACCEPT   - the translated message must be passed to the user
265  *                      MSG_PeekHardwareMsg should return TRUE.
266  */
267 static DWORD MSG_ProcessMouseMsg( MSG *msg, BOOL remove, INT16 hittest,
268                                   POINT16 screen_pt, BOOL mouseClick )
269 {
270     WND *pWnd;
271     HWND hWnd = msg->hwnd;
272     INT16 sendSC = (GetCapture() == 0);
273     UINT message = msg->message;
274     BOOL eatMsg = FALSE;
275     DWORD retvalue;
276
277     pWnd = WIN_FindWndPtr(hWnd);
278     
279         /* call WH_MOUSE */
280
281     if (HOOK_IsHooked( WH_MOUSE ))
282     { 
283         SYSQ_STATUS ret = 0;
284         MOUSEHOOKSTRUCT16 *hook = SEGPTR_NEW(MOUSEHOOKSTRUCT16);
285         if( hook )
286         {
287             hook->pt           = screen_pt;
288             hook->hwnd         = hWnd;
289             hook->wHitTestCode = hittest;
290             hook->dwExtraInfo  = 0;
291             ret = HOOK_CallHooks16( WH_MOUSE, remove ? HC_ACTION : HC_NOREMOVE,
292                                     message, (LPARAM)SEGPTR_GET(hook) );
293             SEGPTR_FREE(hook);
294         }
295         if( ret )
296         {
297             retvalue = MAKELONG((INT16)SYSQ_MSG_SKIP, hittest);
298             goto END;
299         }
300     }
301
302     if (message >= WM_NCMOUSEFIRST && message <= WM_NCMOUSELAST)
303         message += WM_MOUSEFIRST - WM_NCMOUSEFIRST;
304
305     if ((hittest == HTERROR) || (hittest == HTNOWHERE)) 
306         eatMsg = sendSC = 1;
307     else if( remove && mouseClick )
308     {
309         HWND hwndTop = WIN_GetTopParent( hWnd );
310
311         if( sendSC )
312         {
313             /* Send the WM_PARENTNOTIFY,
314              * note that even for double/nonclient clicks
315              * notification message is still WM_L/M/RBUTTONDOWN.
316              */
317
318             MSG_SendParentNotify( pWnd, message, 0, MAKELPARAM(screen_pt.x, screen_pt.y) );
319
320             /* Activate the window if needed */
321
322             if (hWnd != GetActiveWindow() && hWnd != GetDesktopWindow())
323             {
324                 LONG ret = SendMessageA( hWnd, WM_MOUSEACTIVATE, hwndTop,
325                                           MAKELONG( hittest, message ) );
326
327                 if ((ret == MA_ACTIVATEANDEAT) || (ret == MA_NOACTIVATEANDEAT))
328                          eatMsg = TRUE;
329
330                 if (((ret == MA_ACTIVATE) || (ret == MA_ACTIVATEANDEAT))
331                         && hwndTop != GetForegroundWindow() )
332                 {
333                       if (!WINPOS_SetActiveWindow( hwndTop, TRUE , TRUE ))
334                          eatMsg = TRUE;
335                 }
336             }
337         }
338     } else sendSC = (remove && sendSC);
339
340      /* Send the WM_SETCURSOR message */
341
342     if (sendSC)
343     {
344         /* Windows sends the normal mouse message as the message parameter
345            in the WM_SETCURSOR message even if it's non-client mouse message */
346         SendMessageA( hWnd, WM_SETCURSOR, hWnd,
347                        MAKELONG( hittest, message ));
348     }
349     if (eatMsg)
350     {
351         retvalue = MAKELONG( (UINT16)SYSQ_MSG_SKIP, hittest);
352         goto END;
353     }
354
355     retvalue = SYSQ_MSG_ACCEPT;
356 END:
357     WIN_ReleaseWndPtr(pWnd);
358
359     return retvalue;
360 }
361
362
363 /***********************************************************************
364  *           MSG_TranslateKbdMsg
365  *
366  * Translate an keyboard hardware event into a real message.
367  */
368 static DWORD MSG_TranslateKbdMsg( HWND hTopWnd, DWORD first, DWORD last,
369                                   MSG *msg, BOOL remove )
370 {
371     WORD message = msg->message;
372     HWND hWnd = GetFocus();
373     WND *pWnd;
374
375       /* Should check Ctrl-Esc and PrintScreen here */
376
377     if (!hWnd)
378     {
379           /* Send the message to the active window instead,  */
380           /* translating messages to their WM_SYS equivalent */
381
382         hWnd = GetActiveWindow();
383
384         if( message < WM_SYSKEYDOWN )
385             message += WM_SYSKEYDOWN - WM_KEYDOWN;
386     }
387     pWnd = WIN_FindWndPtr( hWnd );
388     if (pWnd && (pWnd->hmemTaskQ != GetFastQueue16()))
389     {
390         /* Not for the current task */
391         MESSAGEQUEUE *queue = (MESSAGEQUEUE *)QUEUE_Lock( GetFastQueue16() );
392         if (queue) QUEUE_ClearWakeBit( queue, QS_KEY );
393         QUEUE_Unlock( queue );
394         
395         /* Wake up the other task */
396         queue = (MESSAGEQUEUE *)QUEUE_Lock( pWnd->hmemTaskQ );
397         if (queue) QUEUE_SetWakeBit( queue, QS_KEY );
398         QUEUE_Unlock( queue );
399         WIN_ReleaseWndPtr(pWnd);
400         return SYSQ_MSG_ABANDON;
401     }
402     WIN_ReleaseWndPtr(pWnd);
403
404     if (hTopWnd && hWnd != hTopWnd)
405         if (!IsChild(hTopWnd, hWnd)) return SYSQ_MSG_CONTINUE;
406     if (!MSG_CheckFilter(message, first, last)) return SYSQ_MSG_CONTINUE;
407
408     msg->hwnd = hWnd;
409     msg->message = message;
410
411     return SYSQ_MSG_ACCEPT;
412 }
413
414
415 /***********************************************************************
416  *           MSG_ProcessKbdMsg
417  *
418  *  Processes a translated keyboard message
419  */
420 static DWORD MSG_ProcessKbdMsg( MSG *msg, BOOL remove )
421 {
422     /* Handle F1 key by sending out WM_HELP message */
423     if ((msg->message == WM_KEYUP) && 
424         (msg->wParam == VK_F1) &&
425         remove &&
426         (msg->hwnd != GetDesktopWindow()) &&
427         !MENU_IsMenuActive())
428     {   
429         HELPINFO hi;
430         WND *pWnd = WIN_FindWndPtr(msg->hwnd);
431
432         if (NULL != pWnd)
433         {
434             hi.cbSize = sizeof(HELPINFO);
435             hi.iContextType = HELPINFO_WINDOW;
436             hi.iCtrlId = pWnd->wIDmenu; 
437             hi.hItemHandle = msg->hwnd;
438             hi.dwContextId = pWnd->helpContext;
439             hi.MousePos = msg->pt;
440             SendMessageA(msg->hwnd, WM_HELP, 0, (LPARAM)&hi);
441         }
442         WIN_ReleaseWndPtr(pWnd);
443     }
444
445     return (HOOK_CallHooks16( WH_KEYBOARD, remove ? HC_ACTION : HC_NOREMOVE,
446                               LOWORD (msg->wParam), msg->lParam )
447             ? SYSQ_MSG_SKIP : SYSQ_MSG_ACCEPT);
448 }
449
450
451 /***********************************************************************
452  *           MSG_JournalRecordMsg
453  *
454  * Build an EVENTMSG structure and call JOURNALRECORD hook
455  */
456 static void MSG_JournalRecordMsg( MSG *msg )
457 {
458     EVENTMSG *event = (EVENTMSG *) HeapAlloc(SystemHeap, 0, sizeof(EVENTMSG));
459     if (!event) return;
460     event->message = msg->message;
461     event->time = msg->time;
462     if ((msg->message >= WM_KEYFIRST) && (msg->message <= WM_KEYLAST))
463     {
464         event->paramL = (msg->wParam & 0xFF) | (HIWORD(msg->lParam) << 8);
465         event->paramH = msg->lParam & 0x7FFF;  
466         if (HIWORD(msg->lParam) & 0x0100)
467             event->paramH |= 0x8000;               /* special_key - bit */
468         HOOK_CallHooksA( WH_JOURNALRECORD, HC_ACTION, 0, (LPARAM)event );
469     }
470     else if ((msg->message >= WM_MOUSEFIRST) && (msg->message <= WM_MOUSELAST))
471     {
472         event->paramL = LOWORD(msg->lParam);       /* X pos */
473         event->paramH = HIWORD(msg->lParam);       /* Y pos */ 
474         ClientToScreen16( msg->hwnd, (LPPOINT16)&event->paramL );
475         HOOK_CallHooksA( WH_JOURNALRECORD, HC_ACTION, 0, (LPARAM)event );
476     }
477     else if ((msg->message >= WM_NCMOUSEFIRST) &&
478              (msg->message <= WM_NCMOUSELAST))
479     {
480         event->paramL = LOWORD(msg->lParam);       /* X pos */
481         event->paramH = HIWORD(msg->lParam);       /* Y pos */ 
482         event->message += WM_MOUSEMOVE-WM_NCMOUSEMOVE;/* give no info about NC area */
483         HOOK_CallHooksA( WH_JOURNALRECORD, HC_ACTION, 0, (LPARAM)event );
484     }
485     
486     HeapFree(SystemHeap, 0, event);
487 }
488
489 /***********************************************************************
490  *          MSG_JournalPlayBackMsg
491  *
492  * Get an EVENTMSG struct via call JOURNALPLAYBACK hook function 
493  */
494 static int MSG_JournalPlayBackMsg(void)
495 {
496  EVENTMSG *tmpMsg;
497  long wtime,lParam,wParam;
498  WORD keyDown,i,result=0;
499
500  if ( HOOK_IsHooked( WH_JOURNALPLAYBACK ) )
501  {
502   tmpMsg = (EVENTMSG *) HeapAlloc(SystemHeap, 0, sizeof(EVENTMSG));
503   if (!tmpMsg) return result;
504   
505   wtime=HOOK_CallHooksA( WH_JOURNALPLAYBACK, HC_GETNEXT, 0,
506                            (LPARAM) tmpMsg );
507   /*  TRACE(msg,"Playback wait time =%ld\n",wtime); */
508   if (wtime<=0)
509   {
510    wtime=0;
511    if ((tmpMsg->message>= WM_KEYFIRST) && (tmpMsg->message <= WM_KEYLAST))
512    {
513      wParam=tmpMsg->paramL & 0xFF;
514      lParam=MAKELONG(tmpMsg->paramH&0x7ffff,tmpMsg->paramL>>8);
515      if (tmpMsg->message == WM_KEYDOWN || tmpMsg->message == WM_SYSKEYDOWN)
516      {
517        for (keyDown=i=0; i<256 && !keyDown; i++)
518           if (InputKeyStateTable[i] & 0x80)
519             keyDown++;
520        if (!keyDown)
521          lParam |= 0x40000000;       
522        AsyncKeyStateTable[wParam]=InputKeyStateTable[wParam] |= 0x80;
523      }  
524      else                                       /* WM_KEYUP, WM_SYSKEYUP */
525      {
526        lParam |= 0xC0000000;      
527        AsyncKeyStateTable[wParam]=InputKeyStateTable[wParam] &= ~0x80;
528      }
529      if (InputKeyStateTable[VK_MENU] & 0x80)
530        lParam |= 0x20000000;     
531      if (tmpMsg->paramH & 0x8000)              /*special_key bit*/
532        lParam |= 0x01000000;
533      hardware_event( tmpMsg->message & 0xffff, LOWORD(wParam), lParam,
534                      0, 0, tmpMsg->time, 0 );
535    }
536    else
537    {
538     if ((tmpMsg->message>= WM_MOUSEFIRST) && (tmpMsg->message <= WM_MOUSELAST))
539     {
540      switch (tmpMsg->message)
541      {
542       case WM_LBUTTONDOWN:
543           MouseButtonsStates[0]=AsyncMouseButtonsStates[0]=TRUE;break;
544       case WM_LBUTTONUP:
545           MouseButtonsStates[0]=AsyncMouseButtonsStates[0]=FALSE;break;
546       case WM_MBUTTONDOWN:
547           MouseButtonsStates[1]=AsyncMouseButtonsStates[1]=TRUE;break;
548       case WM_MBUTTONUP:
549           MouseButtonsStates[1]=AsyncMouseButtonsStates[1]=FALSE;break;
550       case WM_RBUTTONDOWN:
551           MouseButtonsStates[2]=AsyncMouseButtonsStates[2]=TRUE;break;
552       case WM_RBUTTONUP:
553           MouseButtonsStates[2]=AsyncMouseButtonsStates[2]=FALSE;break;      
554      }
555      AsyncKeyStateTable[VK_LBUTTON]= InputKeyStateTable[VK_LBUTTON] = MouseButtonsStates[0] ? 0x80 : 0;
556      AsyncKeyStateTable[VK_MBUTTON]= InputKeyStateTable[VK_MBUTTON] = MouseButtonsStates[1] ? 0x80 : 0;
557      AsyncKeyStateTable[VK_RBUTTON]= InputKeyStateTable[VK_RBUTTON] = MouseButtonsStates[2] ? 0x80 : 0;
558      SetCursorPos(tmpMsg->paramL,tmpMsg->paramH);
559      lParam=MAKELONG(tmpMsg->paramL,tmpMsg->paramH);
560      wParam=0;             
561      if (MouseButtonsStates[0]) wParam |= MK_LBUTTON;
562      if (MouseButtonsStates[1]) wParam |= MK_MBUTTON;
563      if (MouseButtonsStates[2]) wParam |= MK_RBUTTON;
564      hardware_event( tmpMsg->message & 0xffff, LOWORD (wParam), lParam,
565                      tmpMsg->paramL, tmpMsg->paramH, tmpMsg->time, 0 );
566     }
567    }
568    HOOK_CallHooksA( WH_JOURNALPLAYBACK, HC_SKIP, 0,
569                       (LPARAM) tmpMsg);
570   }
571   else
572   {
573       
574     if( tmpMsg->message == WM_QUEUESYNC )
575         if (HOOK_IsHooked( WH_CBT ))
576             HOOK_CallHooksA( WH_CBT, HCBT_QS, 0, 0L);
577
578     result= QS_MOUSE | QS_KEY; /* ? */
579   }
580   HeapFree(SystemHeap, 0, tmpMsg);
581  }
582  return result;
583
584
585 /***********************************************************************
586  *           MSG_PeekHardwareMsg
587  *
588  * Peek for a hardware message matching the hwnd and message filters.
589  */
590 static BOOL MSG_PeekHardwareMsg( MSG *msg, HWND hwnd, DWORD first, DWORD last,
591                                    BOOL remove )
592 {
593     /* FIXME: should deal with MSG32 instead of MSG16 */
594     
595     DWORD status = SYSQ_MSG_ACCEPT;
596     MESSAGEQUEUE *sysMsgQueue = QUEUE_GetSysQueue();
597     enum { MOUSE_MSG = 0, KEYBOARD_MSG, HARDWARE_MSG } msgType;
598     QMSG *nextqmsg, *qmsg = 0;
599     BOOL bRet = FALSE;
600
601     EnterCriticalSection(&sysMsgQueue->cSection);
602
603     /* Loop through the Q and translate the message we wish to process
604      * while we own the lock. Based on the translation status (abandon/cont/accept)
605      * we then process the message accordingly
606      */
607
608     for ( qmsg = sysMsgQueue->firstMsg; qmsg; qmsg = nextqmsg )
609     {
610         INT16 hittest;
611         POINT16 screen_pt;
612         BOOL mouseClick;
613
614         *msg = qmsg->msg;
615
616         nextqmsg = qmsg->nextMsg;
617
618           /* Translate message */
619
620         if ((msg->message >= WM_MOUSEFIRST) && (msg->message <= WM_MOUSELAST))
621         {
622             HWND hWndScope = (HWND)qmsg->extraInfo;
623             WND *tmpWnd = (Options.managed && IsWindow(hWndScope) ) 
624                            ? WIN_FindWndPtr(hWndScope) : WIN_GetDesktop();
625
626             status = MSG_TranslateMouseMsg(hwnd, first, last, msg, remove, tmpWnd,
627                                            &hittest, &screen_pt, &mouseClick );
628             msgType = MOUSE_MSG;
629             
630             WIN_ReleaseWndPtr(tmpWnd);
631
632         }
633         else if ((msg->message >= WM_KEYFIRST) && (msg->message <= WM_KEYLAST))
634         {
635             status = MSG_TranslateKbdMsg(hwnd, first, last, msg, remove);
636             msgType = KEYBOARD_MSG;
637         }
638         else /* Non-standard hardware event */
639         {
640             HARDWAREHOOKSTRUCT16 *hook;
641             msgType = HARDWARE_MSG;
642             if ((hook = SEGPTR_NEW(HARDWAREHOOKSTRUCT16)))
643             {
644                 BOOL ret;
645                 hook->hWnd     = msg->hwnd;
646                 hook->wMessage = msg->message & 0xffff;
647                 hook->wParam   = LOWORD (msg->wParam);
648                 hook->lParam   = msg->lParam;
649                 ret = HOOK_CallHooks16( WH_HARDWARE,
650                                         remove ? HC_ACTION : HC_NOREMOVE,
651                                         0, (LPARAM)SEGPTR_GET(hook) );
652                 SEGPTR_FREE(hook);
653                 if (ret) 
654                 {
655                     QUEUE_RemoveMsg( sysMsgQueue, qmsg );
656                     continue;
657                 }
658                 status = SYSQ_MSG_ACCEPT; 
659             }
660         }
661
662         
663         switch (LOWORD(status))
664         {
665            case SYSQ_MSG_ACCEPT:
666            {
667                /* Remove the message from the system msg Q while it is still locked,
668                 * before accepting it */
669                if (remove)
670                {
671                    if (HOOK_IsHooked( WH_JOURNALRECORD )) MSG_JournalRecordMsg( msg );
672                    QUEUE_RemoveMsg( sysMsgQueue, qmsg );
673                }
674                /* Now actually process the message, after we unlock the system msg Q.
675                 * We should not hold on to the crst since SendMessage calls during processing 
676                 * will potentially cause callbacks to PeekMessage from the application.
677                 * If we're holding the crst and QUEUE_WaitBits is called with a
678                 * QS_SENDMESSAGE mask we will deadlock in hardware_event() when a
679                 * message is being posted to the Q.
680                 */
681                LeaveCriticalSection(&sysMsgQueue->cSection);
682                if( msgType == KEYBOARD_MSG )
683                    status = MSG_ProcessKbdMsg( msg, remove );
684                else if ( msgType == MOUSE_MSG )
685                    status = MSG_ProcessMouseMsg( msg, remove, hittest, screen_pt, mouseClick );
686
687                /* Reclaim the sys msg Q crst */
688                EnterCriticalSection(&sysMsgQueue->cSection);
689                
690                /* Pass the translated message to the user if it was accepted */
691                if (status == SYSQ_MSG_ACCEPT)
692                 break;
693
694                /* If not accepted, fall through into the SYSQ_MSG_SKIP case */
695            }
696                 
697            case SYSQ_MSG_SKIP:
698                 if (HOOK_IsHooked( WH_CBT ))
699                 {
700                    if( msgType == KEYBOARD_MSG )
701                        HOOK_CallHooks16( WH_CBT, HCBT_KEYSKIPPED, 
702                                          LOWORD (msg->wParam), msg->lParam );
703                    else if ( msgType == MOUSE_MSG )
704                    {
705                        MOUSEHOOKSTRUCT16 *hook = SEGPTR_NEW(MOUSEHOOKSTRUCT16);
706                        if (hook)
707                        {
708                            CONV_POINT32TO16( &msg->pt,&hook->pt );
709                            hook->hwnd         = msg->hwnd;
710                            hook->wHitTestCode = HIWORD(status);
711                            hook->dwExtraInfo  = 0;
712                            HOOK_CallHooks16( WH_CBT, HCBT_CLICKSKIPPED ,msg->message & 0xffff,
713                                           (LPARAM)SEGPTR_GET(hook) );
714                            SEGPTR_FREE(hook);
715                        }
716                    }
717                 }
718
719                 /* If the message was removed earlier set up nextqmsg so that we start 
720                  * at the top of the queue again.  We need to do this since our next pointer
721                  * could be invalid due to us unlocking the system message Q to process the message.
722                  * If not removed just refresh nextqmsg to point to the next msg.
723                  */
724                 if (remove)
725                     nextqmsg = sysMsgQueue->firstMsg;
726                 else
727                     nextqmsg = qmsg->nextMsg;
728
729                 continue;
730                 
731            case SYSQ_MSG_CONTINUE:
732                 continue;
733
734            case SYSQ_MSG_ABANDON: 
735                bRet = FALSE;
736                goto END;
737         }
738
739         bRet = TRUE;
740         goto END;
741     }
742
743 END:
744     LeaveCriticalSection(&sysMsgQueue->cSection);
745     return bRet;
746 }
747
748
749 /**********************************************************************
750  *           SetDoubleClickTime16   (USER.20)
751  */
752 void WINAPI SetDoubleClickTime16( UINT16 interval )
753 {
754     SetDoubleClickTime( interval );
755 }               
756
757
758 /**********************************************************************
759  *           SetDoubleClickTime   (USER32.480)
760  */
761 BOOL WINAPI SetDoubleClickTime( UINT interval )
762 {
763     doubleClickSpeed = interval ? interval : 500;
764     return TRUE;
765 }               
766
767
768 /**********************************************************************
769  *           GetDoubleClickTime16   (USER.21)
770  */
771 UINT16 WINAPI GetDoubleClickTime16(void)
772 {
773     return doubleClickSpeed;
774 }               
775
776
777 /**********************************************************************
778  *           GetDoubleClickTime   (USER32.239)
779  */
780 UINT WINAPI GetDoubleClickTime(void)
781 {
782     return doubleClickSpeed;
783 }               
784
785
786 /***********************************************************************
787  *           MSG_SendMessageInterThread
788  *
789  * Implementation of an inter-task SendMessage.
790  * Return values:
791  *    0 if error or timeout
792  *    1 if successflul
793  */
794 static LRESULT MSG_SendMessageInterThread( HQUEUE16 hDestQueue,
795                                            HWND hwnd, UINT msg,
796                                            WPARAM wParam, LPARAM lParam,
797                                            DWORD timeout, WORD flags,
798                                            LRESULT *pRes)
799 {
800     MESSAGEQUEUE *queue, *destQ;
801     SMSG         *smsg;
802     LRESULT      retVal = 1;
803     int          iWndsLocks;
804     
805     *pRes = 0;
806
807     if (IsTaskLocked16() || !IsWindow(hwnd))
808         return 0;
809
810     /* create a SMSG structure to hold SendMessage() parameters */
811     if (! (smsg = (SMSG *) HeapAlloc( SystemHeap, 0, sizeof(SMSG) )) )
812         return 0;
813     if (!(queue = (MESSAGEQUEUE*)QUEUE_Lock( GetFastQueue16() ))) return 0;
814
815     if (!(destQ = (MESSAGEQUEUE*)QUEUE_Lock( hDestQueue )))
816     {
817         QUEUE_Unlock( queue );
818         return 0;
819     }
820
821     TRACE_(sendmsg)("SM: %s [%04x] (%04x -> %04x)\n",
822                     SPY_GetMsgName(msg), msg, queue->self, hDestQueue );
823
824     /* fill up SMSG structure */
825     smsg->hWnd = hwnd;
826     smsg->msg = msg;
827     smsg->wParam = wParam;
828     smsg->lParam = lParam;
829     
830     smsg->lResult = 0;
831     smsg->hSrcQueue = GetFastQueue16();
832     smsg->hDstQueue = hDestQueue;
833     smsg->flags = flags;
834
835     /* add smsg struct in the processing SM list of the source queue */
836     QUEUE_AddSMSG(queue, SM_PROCESSING_LIST, smsg);
837
838     /* add smsg struct in the pending list of the destination queue */
839     if (QUEUE_AddSMSG(destQ, SM_PENDING_LIST, smsg) == FALSE)
840         return 0;
841
842     iWndsLocks = WIN_SuspendWndsLock();
843
844     /* force destination task to run next, if 16 bit threads */
845     if ( THREAD_IsWin16(NtCurrentTeb()) && THREAD_IsWin16(destQ->teb) )
846         DirectedYield16( destQ->teb->htask16 );
847
848     /* wait for the result, note that 16-bit apps almost always get out of
849      * DirectedYield() with SMSG_HAVE_RESULT flag already set */
850
851     while ( TRUE )
852     {
853         /*
854          * The sequence is crucial to avoid deadlock situations:
855          * - first, we clear the QS_SMRESULT bit
856          * - then, we check the SMSG_HAVE_RESULT bit
857          * - only if this isn't set, we enter the wait state.
858          *
859          * As the receiver first sets the SMSG_HAVE_RESULT and then wakes us,
860          * we are guaranteed that -should we now clear the QS_SMRESULT that
861          * was signalled already by the receiver- we will not start waiting.
862          */
863
864         if ( smsg->flags & SMSG_HAVE_RESULT )
865         {
866 got:
867              *pRes = smsg->lResult;
868              TRACE_(sendmsg)("smResult = %08x\n", (unsigned)*pRes );
869              break;
870         }
871
872         QUEUE_ClearWakeBit( queue, QS_SMRESULT );
873
874         if ( smsg->flags & SMSG_HAVE_RESULT )
875              goto got;
876
877         if(  QUEUE_WaitBits( QS_SMRESULT, timeout ) == 0 )
878         {
879              /* return with timeout */
880              SetLastError( 0 );
881              retVal = 0;
882              break;
883         }
884     }
885     WIN_RestoreWndsLock(iWndsLocks);
886
887     /* remove the smsg from the processingg list of the source queue */
888     QUEUE_RemoveSMSG( queue, SM_PROCESSING_LIST, smsg );
889
890     /* Note: the destination thread is in charge of removing the smsg from
891        the pending list */
892
893     /* In the case of an early reply (or a timeout), sender thread will
894        released the smsg structure if the receiver thread is done
895        (SMSG_RECEIVED set). If the receiver thread isn't done,
896        SMSG_RECEIVER_CLEANS_UP flag is set, and it will be the receiver
897        responsability to released smsg */
898         EnterCriticalSection( &queue->cSection );
899     
900         if (smsg->flags & SMSG_RECEIVED)
901             HeapFree(SystemHeap, 0, smsg);
902         else
903             smsg->flags |= SMSG_RECEIVER_CLEANS;
904         
905         LeaveCriticalSection( &queue->cSection );
906
907
908     QUEUE_Unlock( queue );
909     QUEUE_Unlock( destQ );
910     
911     TRACE_(sendmsg)("done!\n");
912     return retVal;
913 }
914
915
916 /***********************************************************************
917  *           ReplyMessage16   (USER.115)
918  */
919 void WINAPI ReplyMessage16( LRESULT result )
920 {
921     ReplyMessage( result );
922 }
923
924 /***********************************************************************
925  *           ReplyMessage   (USER.115)
926  */
927 BOOL WINAPI ReplyMessage( LRESULT result )
928 {
929     MESSAGEQUEUE *senderQ = 0;
930     MESSAGEQUEUE *queue = 0;
931     SMSG         *smsg;
932     BOOL       ret = FALSE;
933
934     if (!(queue = (MESSAGEQUEUE*)QUEUE_Lock( GetFastQueue16() )))
935         return FALSE;
936
937     TRACE_(sendmsg)("ReplyMessage, queue %04x\n", queue->self);
938
939
940     if (    !(smsg = queue->smWaiting)
941          || !(senderQ = QUEUE_Lock( smsg->hSrcQueue )) )
942         goto ReplyMessageEnd;
943
944     if ( !(smsg->flags & SMSG_ALREADY_REPLIED) )
945     {
946         /* This is the first reply, so pass result to sender */
947
948         TRACE_(sendmsg)("\trpm: smResult = %08lx\n", (long) result );
949
950         EnterCriticalSection(&senderQ->cSection);
951         
952         smsg->lResult = result;
953         smsg->flags |= SMSG_ALREADY_REPLIED;
954
955         /* check if it's an early reply (called by the application) or
956            a regular reply (called by ReceiveMessage) */
957         if ( !(smsg->flags & SMSG_SENDING_REPLY) )
958             smsg->flags |= SMSG_EARLY_REPLY;
959
960         smsg->flags |= SMSG_HAVE_RESULT;
961
962         LeaveCriticalSection(&senderQ->cSection);
963
964         /* tell the sending task that its reply is ready */
965         QUEUE_SetWakeBit( senderQ, QS_SMRESULT );
966
967         /* switch directly to sending task (16 bit thread only) */
968         if ( THREAD_IsWin16( NtCurrentTeb() ) && THREAD_IsWin16( senderQ->teb ) )
969             DirectedYield16( senderQ->teb->htask16 );
970
971         ret = TRUE;
972     }
973     
974     if (smsg->flags & SMSG_SENDING_REPLY)
975     {
976         /* remove msg from the waiting list, since this  is the last
977           ReplyMessage */
978         QUEUE_RemoveSMSG( queue, SM_WAITING_LIST, smsg );
979         
980         EnterCriticalSection(&senderQ->cSection);
981         
982         /* tell the sender we're all done with smsg structure */
983         smsg->flags |= SMSG_RECEIVED;
984
985         /* sender will set SMSG_RECEIVER_CLEANS_UP if it wants the
986          receiver to clean up smsg, it could only happens when there is
987          an early reply or a timeout */
988         if ( smsg->flags & SMSG_RECEIVER_CLEANS )
989         {
990             TRACE_(sendmsg)("Receiver cleans up!\n" );
991             HeapFree( SystemHeap, 0, smsg );
992         }
993         
994         LeaveCriticalSection(&senderQ->cSection);
995     }
996
997 ReplyMessageEnd:
998     if ( senderQ )
999     QUEUE_Unlock( senderQ );
1000     if ( queue )
1001     QUEUE_Unlock( queue );
1002
1003     return ret;
1004 }
1005
1006 /***********************************************************************
1007  *           MSG_ConvertMsg
1008  */
1009 static BOOL MSG_ConvertMsg( MSG *msg, int srcType, int dstType )
1010 {
1011     UINT16 msg16;
1012     MSGPARAM16 mp16;
1013
1014     switch ( MAKELONG( srcType, dstType ) )
1015     {
1016     case MAKELONG( QMSG_WIN16,  QMSG_WIN16 ):
1017     case MAKELONG( QMSG_WIN32A, QMSG_WIN32A ):
1018     case MAKELONG( QMSG_WIN32W, QMSG_WIN32W ):
1019         return TRUE;
1020
1021     case MAKELONG( QMSG_WIN16, QMSG_WIN32A ):
1022         switch ( WINPROC_MapMsg16To32A( msg->message, msg->wParam,
1023                                        &msg->message, &msg->wParam, &msg->lParam ) )
1024         {
1025         case 0:
1026             return TRUE;
1027         case 1:
1028             /* Pointer messages were mapped --> need to free allocated memory and fail */
1029             WINPROC_UnmapMsg16To32A( msg->hwnd, msg->message, msg->wParam, msg->lParam, 0 );
1030         default:
1031             return FALSE;
1032         }
1033
1034     case MAKELONG( QMSG_WIN16, QMSG_WIN32W ):
1035         switch ( WINPROC_MapMsg16To32W( msg->hwnd, msg->message, msg->wParam,
1036                                        &msg->message, &msg->wParam, &msg->lParam ) )
1037         {
1038         case 0:
1039             return TRUE;
1040         case 1:
1041             /* Pointer messages were mapped --> need to free allocated memory and fail */
1042             WINPROC_UnmapMsg16To32W( msg->hwnd, msg->message, msg->wParam, msg->lParam, 0 );
1043         default:
1044             return FALSE;
1045         }
1046
1047     case MAKELONG( QMSG_WIN32A, QMSG_WIN16 ):
1048         mp16.lParam = msg->lParam;
1049         switch ( WINPROC_MapMsg32ATo16( msg->hwnd, msg->message, msg->wParam,
1050                                         &msg16, &mp16.wParam, &mp16.lParam ) )
1051         {
1052         case 0:
1053             msg->message = msg16; 
1054             msg->wParam  = mp16.wParam;
1055             msg->lParam  = mp16.lParam;
1056             return TRUE;
1057         case 1:
1058             /* Pointer messages were mapped --> need to free allocated memory and fail */
1059             WINPROC_UnmapMsg32ATo16( msg->hwnd, msg->message, msg->wParam, msg->lParam, &mp16 );
1060         default:
1061             return FALSE;
1062         }
1063
1064     case MAKELONG( QMSG_WIN32W, QMSG_WIN16 ):
1065         mp16.lParam = msg->lParam;
1066         switch ( WINPROC_MapMsg32WTo16( msg->hwnd, msg->message, msg->wParam,
1067                                         &msg16, &mp16.wParam, &mp16.lParam ) )
1068         {
1069         case 0:
1070             msg->message = msg16; 
1071             msg->wParam  = mp16.wParam;
1072             msg->lParam  = mp16.lParam;
1073             return TRUE;
1074         case 1:
1075             /* Pointer messages were mapped --> need to free allocated memory and fail */
1076             WINPROC_UnmapMsg32WTo16( msg->hwnd, msg->message, msg->wParam, msg->lParam, &mp16 );
1077         default:
1078             return FALSE;
1079         }
1080
1081     case MAKELONG( QMSG_WIN32A, QMSG_WIN32W ):
1082         switch ( WINPROC_MapMsg32ATo32W( msg->hwnd, msg->message, msg->wParam, &msg->lParam ) )
1083         {
1084         case 0:
1085             return TRUE;
1086         case 1:
1087             /* Pointer messages were mapped --> need to free allocated memory and fail */
1088             WINPROC_UnmapMsg32ATo32W( msg->hwnd, msg->message, msg->wParam, msg->lParam );
1089         default:
1090             return FALSE;
1091         }
1092
1093     case MAKELONG( QMSG_WIN32W, QMSG_WIN32A ):
1094         switch ( WINPROC_MapMsg32WTo32A( msg->hwnd, msg->message, msg->wParam, &msg->lParam ) )
1095         {
1096         case 0:
1097             return TRUE;
1098         case 1:
1099             /* Pointer messages were mapped --> need to free allocated memory and fail */
1100             WINPROC_UnmapMsg32WTo32A( msg->hwnd, msg->message, msg->wParam, msg->lParam );
1101         default:
1102             return FALSE;
1103         }
1104
1105     default:    
1106         FIXME( "Invalid message type(s): %d / %d\n", srcType, dstType );
1107         return FALSE;
1108     }
1109 }
1110
1111 /***********************************************************************
1112  *           MSG_PeekMessage
1113  */
1114 static BOOL MSG_PeekMessage( int type, LPMSG msg, HWND hwnd, 
1115                              DWORD first, DWORD last, WORD flags, BOOL peek )
1116 {
1117     int mask;
1118     MESSAGEQUEUE *msgQueue;
1119     HQUEUE16 hQueue;
1120     POINT16 pt16;
1121     int iWndsLocks;
1122
1123     mask = QS_POSTMESSAGE | QS_SENDMESSAGE;  /* Always selected */
1124     if (first || last)
1125     {
1126         if ((first <= WM_KEYLAST) && (last >= WM_KEYFIRST)) mask |= QS_KEY;
1127         if ( ((first <= WM_MOUSELAST) && (last >= WM_MOUSEFIRST)) ||
1128              ((first <= WM_NCMOUSELAST) && (last >= WM_NCMOUSEFIRST)) ) mask |= QS_MOUSE;
1129         if ((first <= WM_TIMER) && (last >= WM_TIMER)) mask |= QS_TIMER;
1130         if ((first <= WM_SYSTIMER) && (last >= WM_SYSTIMER)) mask |= QS_TIMER;
1131         if ((first <= WM_PAINT) && (last >= WM_PAINT)) mask |= QS_PAINT;
1132     }
1133     else mask |= QS_MOUSE | QS_KEY | QS_TIMER | QS_PAINT;
1134
1135     if (IsTaskLocked16()) flags |= PM_NOYIELD;
1136
1137     /* Never yield on Win32 threads */
1138     if (!THREAD_IsWin16(NtCurrentTeb())) flags |= PM_NOYIELD;
1139
1140     iWndsLocks = WIN_SuspendWndsLock();
1141
1142     while(1)
1143     {    
1144         QMSG *qmsg;
1145         
1146         hQueue   = GetFastQueue16();
1147         msgQueue = (MESSAGEQUEUE *)QUEUE_Lock( hQueue );
1148         if (!msgQueue)
1149         {
1150             WIN_RestoreWndsLock(iWndsLocks);
1151             return FALSE;
1152         }
1153         msgQueue->changeBits = 0;
1154
1155         /* First handle a message put by SendMessage() */
1156
1157         while (msgQueue->wakeBits & QS_SENDMESSAGE)
1158             QUEUE_ReceiveMessage( msgQueue );
1159
1160         /* Now handle a WM_QUIT message */
1161
1162         if (msgQueue->wPostQMsg &&
1163            (!first || WM_QUIT >= first) && 
1164            (!last || WM_QUIT <= last) )
1165         {
1166             msg->hwnd    = hwnd;
1167             msg->message = WM_QUIT;
1168             msg->wParam  = msgQueue->wExitCode;
1169             msg->lParam  = 0;
1170             if (flags & PM_REMOVE) msgQueue->wPostQMsg = 0;
1171             break;
1172         }
1173     
1174         /* Now find a normal message */
1175
1176   retry:
1177         if (((msgQueue->wakeBits & mask) & QS_POSTMESSAGE) &&
1178             ((qmsg = QUEUE_FindMsg( msgQueue, hwnd, first, last )) != 0))
1179         {
1180             /* Try to convert message to requested type */
1181             MSG tmpMsg = qmsg->msg;
1182             if ( !MSG_ConvertMsg( &tmpMsg, qmsg->type, type ) )
1183             {
1184                 ERR( "Message of wrong type contains pointer parameters. Skipped!\n ");
1185                 QUEUE_RemoveMsg( msgQueue, qmsg );
1186                 goto retry;
1187             }
1188
1189             *msg = tmpMsg;
1190             msgQueue->GetMessageTimeVal      = msg->time;
1191             CONV_POINT32TO16(&msg->pt, &pt16);
1192             msgQueue->GetMessagePosVal       = *(DWORD *)&pt16;
1193             msgQueue->GetMessageExtraInfoVal = qmsg->extraInfo;
1194
1195             if (flags & PM_REMOVE) QUEUE_RemoveMsg( msgQueue, qmsg );
1196             break;
1197         }
1198
1199         msgQueue->changeBits |= MSG_JournalPlayBackMsg();
1200
1201         /* Now find a hardware event */
1202
1203         if (((msgQueue->wakeBits & mask) & (QS_MOUSE | QS_KEY)) &&
1204             MSG_PeekHardwareMsg( msg, hwnd, first, last, flags & PM_REMOVE ))
1205         {
1206             /* Got one */
1207             msgQueue->GetMessageTimeVal      = msg->time;
1208             CONV_POINT32TO16(&msg->pt, &pt16);
1209             msgQueue->GetMessagePosVal       = *(DWORD *)&pt16;
1210             msgQueue->GetMessageExtraInfoVal = 0;  /* Always 0 for now */
1211             break;
1212         }
1213
1214         /* Check again for SendMessage */
1215
1216         while (msgQueue->wakeBits & QS_SENDMESSAGE)
1217             QUEUE_ReceiveMessage( msgQueue );
1218
1219         /* Now find a WM_PAINT message */
1220
1221         if ((msgQueue->wakeBits & mask) & QS_PAINT)
1222         {
1223             WND* wndPtr;
1224             msg->hwnd = WIN_FindWinToRepaint( hwnd , hQueue );
1225             msg->message = WM_PAINT;
1226             msg->wParam = 0;
1227             msg->lParam = 0;
1228
1229             if ((wndPtr = WIN_FindWndPtr(msg->hwnd)))
1230             {
1231                 if( wndPtr->dwStyle & WS_MINIMIZE &&
1232                     (HICON) GetClassLongA(wndPtr->hwndSelf, GCL_HICON) )
1233                 {
1234                     msg->message = WM_PAINTICON;
1235                     msg->wParam = 1;
1236                 }
1237
1238                 if( !hwnd || msg->hwnd == hwnd || IsChild16(hwnd,msg->hwnd) )
1239                 {
1240                     if( wndPtr->flags & WIN_INTERNAL_PAINT && !wndPtr->hrgnUpdate)
1241                     {
1242                         wndPtr->flags &= ~WIN_INTERNAL_PAINT;
1243                         QUEUE_DecPaintCount( hQueue );
1244                     }
1245                     WIN_ReleaseWndPtr(wndPtr);
1246                     break;
1247                 }
1248                 WIN_ReleaseWndPtr(wndPtr);
1249             }
1250         }
1251
1252         /* Check for timer messages, but yield first */
1253
1254         if (!(flags & PM_NOYIELD))
1255         {
1256             UserYield16();
1257             while (msgQueue->wakeBits & QS_SENDMESSAGE)
1258                 QUEUE_ReceiveMessage( msgQueue );
1259         }
1260         if ((msgQueue->wakeBits & mask) & QS_TIMER)
1261         {
1262             if (TIMER_GetTimerMsg(msg, hwnd, hQueue, flags & PM_REMOVE)) break;
1263         }
1264
1265         if (peek)
1266         {
1267             if (!(flags & PM_NOYIELD)) UserYield16();
1268             
1269             QUEUE_Unlock( msgQueue );
1270             WIN_RestoreWndsLock(iWndsLocks);
1271             return FALSE;
1272         }
1273         msgQueue->wakeMask = mask;
1274         QUEUE_WaitBits( mask, INFINITE );
1275         QUEUE_Unlock( msgQueue );
1276     }
1277
1278     WIN_RestoreWndsLock(iWndsLocks);
1279     
1280     /* instead of unlocking queue for every break condition, all break
1281        condition will fall here */
1282     QUEUE_Unlock( msgQueue );
1283     
1284       /* We got a message */
1285     if (flags & PM_REMOVE)
1286     {
1287         WORD message = msg->message;
1288
1289         if (message == WM_KEYDOWN || message == WM_SYSKEYDOWN)
1290         {
1291             BYTE *p = &QueueKeyStateTable[msg->wParam & 0xff];
1292
1293             if (!(*p & 0x80))
1294                 *p ^= 0x01;
1295             *p |= 0x80;
1296         }
1297         else if (message == WM_KEYUP || message == WM_SYSKEYUP)
1298             QueueKeyStateTable[msg->wParam & 0xff] &= ~0x80;
1299     }
1300
1301     if (peek)
1302         return TRUE;
1303
1304     else
1305         return (msg->message != WM_QUIT);
1306 }
1307
1308 /***********************************************************************
1309  *           MSG_InternalGetMessage
1310  *
1311  * GetMessage() function for internal use. Behave like GetMessage(),
1312  * but also call message filters and optionally send WM_ENTERIDLE messages.
1313  * 'hwnd' must be the handle of the dialog or menu window.
1314  * 'code' is the message filter value (MSGF_??? codes).
1315  */
1316 BOOL MSG_InternalGetMessage( int type, MSG *msg, HWND hwnd, HWND hwndOwner,
1317                              WPARAM code, WORD flags, BOOL sendIdle, BOOL* idleSent ) 
1318 {
1319     for (;;)
1320     {
1321         if (sendIdle)
1322         {
1323             if (!MSG_PeekMessage( type, msg, 0, 0, 0, flags, TRUE ))
1324             {
1325                   /* No message present -> send ENTERIDLE and wait */
1326                 if (IsWindow(hwndOwner))
1327                 {
1328                     SendMessageA( hwndOwner, WM_ENTERIDLE,
1329                                    code, (LPARAM)hwnd );
1330
1331                     if (idleSent!=NULL)
1332                       *idleSent=TRUE;
1333                 }
1334                 MSG_PeekMessage( type, msg, 0, 0, 0, flags, FALSE );
1335             }
1336         }
1337         else  /* Always wait for a message */
1338             MSG_PeekMessage( type, msg, 0, 0, 0, flags, FALSE );
1339
1340         /* Call message filters */
1341
1342         if (HOOK_IsHooked( WH_SYSMSGFILTER ) || HOOK_IsHooked( WH_MSGFILTER ))
1343         {
1344             MSG *pmsg = HeapAlloc( SystemHeap, 0, sizeof(MSG) );
1345             if (pmsg)
1346             {
1347                 BOOL ret;
1348                 *pmsg = *msg;
1349                 ret = (HOOK_CallHooksA( WH_SYSMSGFILTER, code, 0,
1350                                           (LPARAM) pmsg ) ||
1351                        HOOK_CallHooksA( WH_MSGFILTER, code, 0,
1352                                           (LPARAM) pmsg ));
1353                        
1354                 HeapFree( SystemHeap, 0, pmsg );
1355                 if (ret)
1356                 {
1357                     /* Message filtered -> remove it from the queue */
1358                     /* if it's still there. */
1359                     if (!(flags & PM_REMOVE))
1360                         MSG_PeekMessage( type, msg, 0, 0, 0, PM_REMOVE, TRUE );
1361                     continue;
1362                 }
1363             }
1364         }
1365
1366         return (msg->message != WM_QUIT);
1367     }
1368 }
1369
1370
1371 /***********************************************************************
1372  *         PeekMessage32_16   (USER.819)
1373  */
1374 BOOL16 WINAPI PeekMessage32_16( LPMSG16_32 lpmsg16_32, HWND16 hwnd,
1375                                 UINT16 first, UINT16 last, UINT16 flags, 
1376                                 BOOL16 wHaveParamHigh )
1377 {
1378     BOOL ret;
1379     MSG msg;
1380
1381     ret = MSG_PeekMessage( QMSG_WIN16, &msg, hwnd, first, last, flags, TRUE );
1382
1383     lpmsg16_32->msg.hwnd    = msg.hwnd;
1384     lpmsg16_32->msg.message = msg.message;
1385     lpmsg16_32->msg.wParam  = LOWORD(msg.wParam);
1386     lpmsg16_32->msg.lParam  = msg.lParam;
1387     lpmsg16_32->msg.time    = msg.time;
1388     lpmsg16_32->msg.pt.x    = (INT16)msg.pt.x;
1389     lpmsg16_32->msg.pt.y    = (INT16)msg.pt.y;
1390
1391     if ( wHaveParamHigh )
1392         lpmsg16_32->wParamHigh = HIWORD(msg.wParam);
1393
1394     return ret;
1395 }
1396
1397 /***********************************************************************
1398  *           PeekMessage16   (USER.109)
1399  */
1400 BOOL16 WINAPI PeekMessage16( LPMSG16 lpmsg, HWND16 hwnd, 
1401                              UINT16 first, UINT16 last, UINT16 flags )
1402 {
1403     return PeekMessage32_16( (LPMSG16_32)lpmsg, hwnd, first, last, flags, FALSE );
1404 }
1405
1406 /***********************************************************************
1407  *         PeekMessageA
1408  */
1409 BOOL WINAPI PeekMessageA( LPMSG lpmsg, HWND hwnd,
1410                           UINT min, UINT max, UINT wRemoveMsg)
1411 {
1412     return MSG_PeekMessage( QMSG_WIN32A, lpmsg, hwnd, min, max, wRemoveMsg, TRUE );
1413 }
1414
1415 /***********************************************************************
1416  *         PeekMessageW             Check queue for messages
1417  *
1418  * Checks for a message in the thread's queue, filtered as for
1419  * GetMessage(). Returns immediately whether a message is available
1420  * or not.
1421  *
1422  * Whether a retrieved message is removed from the queue is set by the
1423  * _wRemoveMsg_ flags, which should be one of the following values:
1424  *
1425  *    PM_NOREMOVE    Do not remove the message from the queue. 
1426  *
1427  *    PM_REMOVE      Remove the message from the queue.
1428  *
1429  * In addition, PM_NOYIELD may be combined into _wRemoveMsg_ to
1430  * request that the system not yield control during PeekMessage();
1431  * however applications may not rely on scheduling behavior.
1432  * 
1433  * RETURNS
1434  *
1435  *  Nonzero if a message is available and is retrieved, zero otherwise.
1436  *
1437  * CONFORMANCE
1438  *
1439  * ECMA-234, Win32
1440  *
1441  */
1442 BOOL WINAPI PeekMessageW( 
1443   LPMSG lpmsg,    /* buffer to receive message */
1444   HWND hwnd,      /* restrict to messages for hwnd */
1445   UINT min,       /* minimum message to receive */
1446   UINT max,       /* maximum message to receive */
1447   UINT wRemoveMsg /* removal flags */ 
1448
1449 {
1450     return MSG_PeekMessage( QMSG_WIN32W, lpmsg, hwnd, min, max, wRemoveMsg, TRUE );
1451 }
1452
1453
1454 /***********************************************************************
1455  *          GetMessage32_16   (USER.820)
1456  */
1457 BOOL16 WINAPI GetMessage32_16( SEGPTR msg16_32, HWND16 hWnd, UINT16 first,
1458                                UINT16 last, BOOL16 wHaveParamHigh )
1459 {
1460     MSG32_16 *lpmsg16_32 = (MSG32_16 *)PTR_SEG_TO_LIN(msg16_32);
1461     MSG msg;
1462
1463     MSG_PeekMessage( QMSG_WIN16, &msg, hWnd, first, last, PM_REMOVE, FALSE );
1464
1465     lpmsg16_32->msg.hwnd    = msg.hwnd;
1466     lpmsg16_32->msg.message = msg.message;
1467     lpmsg16_32->msg.wParam  = LOWORD(msg.wParam);
1468     lpmsg16_32->msg.lParam  = msg.lParam;
1469     lpmsg16_32->msg.time    = msg.time;
1470     lpmsg16_32->msg.pt.x    = (INT16)msg.pt.x;
1471     lpmsg16_32->msg.pt.y    = (INT16)msg.pt.y;
1472
1473     if ( wHaveParamHigh )
1474         lpmsg16_32->wParamHigh = HIWORD(msg.wParam);
1475
1476     TRACE( "message %04x, hwnd %04x, filter(%04x - %04x)\n",
1477            lpmsg16_32->msg.message, hWnd, first, last );
1478
1479     HOOK_CallHooks16( WH_GETMESSAGE, HC_ACTION, 0, (LPARAM)msg16_32 );
1480     return lpmsg16_32->msg.message != WM_QUIT;
1481 }
1482
1483 /***********************************************************************
1484  *           GetMessage16   (USER.108)
1485  */
1486 BOOL16 WINAPI GetMessage16( SEGPTR msg, HWND16 hwnd, UINT16 first, UINT16 last)
1487 {
1488     return GetMessage32_16( msg, hwnd, first, last, FALSE );
1489 }
1490
1491 /***********************************************************************
1492  *          GetMessageA   (USER32.270)
1493  */
1494 BOOL WINAPI GetMessageA( MSG *lpmsg, HWND hwnd, UINT min, UINT max )
1495 {
1496     MSG_PeekMessage( QMSG_WIN32A, lpmsg, hwnd, min, max, PM_REMOVE, FALSE );
1497     
1498     TRACE( "message %04x, hwnd %04x, filter(%04x - %04x)\n", 
1499            lpmsg->message, hwnd, min, max );
1500     
1501     HOOK_CallHooksA( WH_GETMESSAGE, HC_ACTION, 0, (LPARAM)lpmsg );
1502     return lpmsg->message != WM_QUIT;
1503 }
1504
1505 /***********************************************************************
1506  *          GetMessageW   (USER32.274) Retrieve next message
1507  *
1508  * GetMessage retrieves the next event from the calling thread's
1509  * queue and deposits it in *lpmsg.
1510  *
1511  * If _hwnd_ is not NULL, only messages for window _hwnd_ and its
1512  * children as specified by IsChild() are retrieved. If _hwnd_ is NULL
1513  * all application messages are retrieved.
1514  *
1515  * _min_ and _max_ specify the range of messages of interest. If
1516  * min==max==0, no filtering is performed. Useful examples are
1517  * WM_KEYFIRST and WM_KEYLAST to retrieve keyboard input, and
1518  * WM_MOUSEFIRST and WM_MOUSELAST to retrieve mouse input.
1519  *
1520  * WM_PAINT messages are not removed from the queue; they remain until
1521  * processed. Other messages are removed from the queue.
1522  *
1523  * RETURNS
1524  *
1525  * -1 on error, 0 if message is WM_QUIT, nonzero otherwise.
1526  *
1527  * CONFORMANCE
1528  *
1529  * ECMA-234, Win32
1530  * 
1531  */
1532 BOOL WINAPI GetMessageW(
1533   MSG* lpmsg, /* buffer to receive message */
1534   HWND hwnd,  /* restrict to messages for hwnd */
1535   UINT min,   /* minimum message to receive */
1536   UINT max    /* maximum message to receive */
1537
1538 {
1539     MSG_PeekMessage( QMSG_WIN32W, lpmsg, hwnd, min, max, PM_REMOVE, FALSE );
1540     
1541     TRACE( "message %04x, hwnd %04x, filter(%04x - %04x)\n", 
1542            lpmsg->message, hwnd, min, max );
1543     
1544     HOOK_CallHooksW( WH_GETMESSAGE, HC_ACTION, 0, (LPARAM)lpmsg );
1545     return lpmsg->message != WM_QUIT;
1546 }
1547
1548 /***********************************************************************
1549  *           MSG_PostToQueue
1550  */
1551 static BOOL MSG_PostToQueue( HQUEUE16 hQueue, int type, HWND hwnd, 
1552                              UINT message, WPARAM wParam, LPARAM lParam )
1553 {
1554     MSG msg;
1555
1556     if ( !hQueue ) return FALSE;
1557
1558     msg.hwnd    = hwnd;
1559     msg.message = message;
1560     msg.wParam  = wParam;
1561     msg.lParam  = lParam;
1562     msg.time    = GetTickCount();
1563     msg.pt.x    = 0;
1564     msg.pt.y    = 0;
1565
1566     return QUEUE_AddMsg( hQueue, type, &msg, 0 );
1567 }
1568
1569 /***********************************************************************
1570  *           MSG_PostMessage
1571  */
1572 static BOOL MSG_PostMessage( int type, HWND hwnd, UINT message, 
1573                              WPARAM wParam, LPARAM lParam )
1574 {
1575     HQUEUE16 hQueue;
1576     WND *wndPtr;
1577
1578     if (hwnd == HWND_BROADCAST)
1579     {
1580         WND *pDesktop = WIN_GetDesktop();
1581         TRACE("HWND_BROADCAST !\n");
1582         
1583         for (wndPtr=WIN_LockWndPtr(pDesktop->child); wndPtr; WIN_UpdateWndPtr(&wndPtr,wndPtr->next))
1584         {
1585             if (wndPtr->dwStyle & WS_POPUP || wndPtr->dwStyle & WS_CAPTION)
1586             {
1587                 TRACE("BROADCAST Message to hWnd=%04x m=%04X w=%04X l=%08lX !\n",
1588                       wndPtr->hwndSelf, message, wParam, lParam);
1589                 MSG_PostToQueue( wndPtr->hmemTaskQ, type, 
1590                                  wndPtr->hwndSelf, message, wParam, lParam );
1591             }
1592         }
1593         WIN_ReleaseDesktop();
1594         TRACE("End of HWND_BROADCAST !\n");
1595         return TRUE;
1596     }
1597
1598     wndPtr = WIN_FindWndPtr( hwnd );
1599     hQueue = wndPtr? wndPtr->hmemTaskQ : 0;
1600     WIN_ReleaseWndPtr(wndPtr);
1601
1602     return MSG_PostToQueue( hQueue, type, hwnd, message, wParam, lParam );
1603 }
1604
1605 /***********************************************************************
1606  *           PostMessage16   (USER.110)
1607  */
1608 BOOL16 WINAPI PostMessage16( HWND16 hwnd, UINT16 message, WPARAM16 wParam,
1609                              LPARAM lParam )
1610 {
1611     return (BOOL16) MSG_PostMessage( QMSG_WIN16, hwnd, message, wParam, lParam );
1612 }
1613
1614 /***********************************************************************
1615  *           PostMessageA   (USER32.419)
1616  */
1617 BOOL WINAPI PostMessageA( HWND hwnd, UINT message, WPARAM wParam,
1618                           LPARAM lParam )
1619 {
1620     return MSG_PostMessage( QMSG_WIN32A, hwnd, message, wParam, lParam );
1621 }
1622
1623 /***********************************************************************
1624  *           PostMessageW   (USER32.420)
1625  */
1626 BOOL WINAPI PostMessageW( HWND hwnd, UINT message, WPARAM wParam,
1627                           LPARAM lParam )
1628 {
1629     return MSG_PostMessage( QMSG_WIN32W, hwnd, message, wParam, lParam );
1630 }
1631
1632 /***********************************************************************
1633  *           PostAppMessage16   (USER.116)
1634  */
1635 BOOL16 WINAPI PostAppMessage16( HTASK16 hTask, UINT16 message, 
1636                                 WPARAM16 wParam, LPARAM lParam )
1637 {
1638     return MSG_PostToQueue( GetTaskQueue16(hTask), QMSG_WIN16, 
1639                             0, message, wParam, lParam );
1640 }
1641
1642 /**********************************************************************
1643  *           PostThreadMessageA    (USER32.422)
1644  */
1645 BOOL WINAPI PostThreadMessageA( DWORD idThread, UINT message,
1646                                 WPARAM wParam, LPARAM lParam )
1647 {
1648     return MSG_PostToQueue( GetThreadQueue16(idThread), QMSG_WIN32A, 
1649                             0, message, wParam, lParam );
1650 }
1651
1652 /**********************************************************************
1653  *           PostThreadMessageW    (USER32.423)
1654  */
1655 BOOL WINAPI PostThreadMessageW( DWORD idThread, UINT message,
1656                                  WPARAM wParam, LPARAM lParam )
1657 {
1658     return MSG_PostToQueue( GetThreadQueue16(idThread), QMSG_WIN32W, 
1659                             0, message, wParam, lParam );
1660 }
1661
1662
1663 /************************************************************************
1664  *           MSG_CallWndProcHook32
1665  */
1666 static void  MSG_CallWndProcHook( LPMSG pmsg, BOOL bUnicode )
1667 {
1668    CWPSTRUCT cwp;
1669
1670    cwp.lParam = pmsg->lParam;
1671    cwp.wParam = pmsg->wParam;
1672    cwp.message = pmsg->message;
1673    cwp.hwnd = pmsg->hwnd;
1674
1675    if (bUnicode) HOOK_CallHooksW(WH_CALLWNDPROC, HC_ACTION, 1, (LPARAM)&cwp);
1676    else HOOK_CallHooksA( WH_CALLWNDPROC, HC_ACTION, 1, (LPARAM)&cwp );
1677
1678    pmsg->lParam = cwp.lParam;
1679    pmsg->wParam = cwp.wParam;
1680    pmsg->message = cwp.message;
1681    pmsg->hwnd = cwp.hwnd;
1682 }
1683
1684
1685 /***********************************************************************
1686  *           MSG_SendMessage
1687  *
1688  * return values: 0 if timeout occurs
1689  *                1 otherwise
1690  */
1691 static LRESULT MSG_SendMessage( HWND hwnd, UINT msg, WPARAM wParam,
1692                          LPARAM lParam, DWORD timeout, WORD flags,
1693                          LRESULT *pRes)
1694 {
1695     WND * wndPtr = 0;
1696     WND **list, **ppWnd;
1697     LRESULT ret = 1;
1698
1699     *pRes = 0;
1700
1701     if (hwnd == HWND_BROADCAST|| hwnd == HWND_TOPMOST)
1702     {
1703         *pRes = 1;
1704         
1705         if (!(list = WIN_BuildWinArray( WIN_GetDesktop(), 0, NULL )))
1706         {
1707             WIN_ReleaseDesktop();
1708             return 1;
1709         }
1710         WIN_ReleaseDesktop();
1711
1712         TRACE("HWND_BROADCAST !\n");
1713         for (ppWnd = list; *ppWnd; ppWnd++)
1714         {
1715             WIN_UpdateWndPtr(&wndPtr,*ppWnd);
1716             if (!IsWindow(wndPtr->hwndSelf)) continue;
1717             if (wndPtr->dwStyle & WS_POPUP || wndPtr->dwStyle & WS_CAPTION)
1718             {
1719                 TRACE("BROADCAST Message to hWnd=%04x m=%04X w=%04lX l=%08lX !\n",
1720                       wndPtr->hwndSelf, msg, (DWORD)wParam, lParam);
1721                 MSG_SendMessage( wndPtr->hwndSelf, msg, wParam, lParam,
1722                                timeout, flags, pRes);
1723             }
1724         }
1725         WIN_ReleaseWndPtr(wndPtr);
1726         WIN_ReleaseWinArray(list);
1727         TRACE("End of HWND_BROADCAST !\n");
1728         return 1;
1729     }
1730
1731     if (HOOK_IsHooked( WH_CALLWNDPROC ))
1732     {
1733         if (flags & SMSG_UNICODE)
1734             MSG_CallWndProcHook( (LPMSG)&hwnd, TRUE);
1735         else if (flags & SMSG_WIN32)
1736             MSG_CallWndProcHook( (LPMSG)&hwnd, FALSE);
1737         else
1738         {
1739         LPCWPSTRUCT16 pmsg;
1740
1741         if ((pmsg = SEGPTR_NEW(CWPSTRUCT16)))
1742         {
1743                 pmsg->hwnd   = hwnd & 0xffff;
1744                 pmsg->message= msg & 0xffff;
1745                 pmsg->wParam = wParam & 0xffff;
1746             pmsg->lParam = lParam;
1747             HOOK_CallHooks16( WH_CALLWNDPROC, HC_ACTION, 1,
1748                               (LPARAM)SEGPTR_GET(pmsg) );
1749             hwnd   = pmsg->hwnd;
1750             msg    = pmsg->message;
1751             wParam = pmsg->wParam;
1752             lParam = pmsg->lParam;
1753             SEGPTR_FREE( pmsg );
1754         }
1755     }
1756     }
1757
1758     if (!(wndPtr = WIN_FindWndPtr( hwnd )))
1759     {
1760         WARN("invalid hwnd %04x\n", hwnd );
1761         return 0;
1762     }
1763     if (QUEUE_IsExitingQueue(wndPtr->hmemTaskQ))
1764     {
1765         ret = 0;  /* Don't send anything if the task is dying */
1766         goto END;
1767     }
1768     if (flags & SMSG_WIN32)
1769         SPY_EnterMessage( SPY_SENDMESSAGE, hwnd, msg, wParam, lParam );
1770     else
1771     SPY_EnterMessage( SPY_SENDMESSAGE16, hwnd, msg, wParam, lParam );
1772
1773     if (wndPtr->hmemTaskQ != GetFastQueue16())
1774         ret = MSG_SendMessageInterThread( wndPtr->hmemTaskQ, hwnd, msg,
1775                                           wParam, lParam, timeout, flags, pRes );
1776     else
1777     {
1778         /* Call the right CallWindowProc flavor */
1779         if (flags & SMSG_UNICODE)
1780             *pRes = CallWindowProcW( (WNDPROC)wndPtr->winproc,
1781                                      hwnd, msg, wParam, lParam );
1782         else if (flags & SMSG_WIN32)
1783             *pRes = CallWindowProcA( (WNDPROC)wndPtr->winproc,
1784                                 hwnd, msg, wParam, lParam );
1785         else
1786             *pRes = CallWindowProc16( (WNDPROC16)wndPtr->winproc,
1787                                     (HWND16) hwnd, (UINT16) msg,
1788                                     (WPARAM16) wParam, lParam );
1789     }
1790
1791     if (flags & SMSG_WIN32)
1792         SPY_ExitMessage( SPY_RESULT_OK, hwnd, msg, *pRes );
1793     else
1794     SPY_ExitMessage( SPY_RESULT_OK16, hwnd, msg, *pRes );
1795 END:
1796     WIN_ReleaseWndPtr(wndPtr);
1797     return ret;
1798 }
1799
1800
1801 /***********************************************************************
1802  *           SendMessage16   (USER.111)
1803  */
1804 LRESULT WINAPI SendMessage16( HWND16 hwnd, UINT16 msg, WPARAM16 wParam,
1805                               LPARAM lParam)
1806 {
1807     LRESULT res;
1808     MSG_SendMessage(hwnd, msg, wParam, lParam, INFINITE, 0, &res);
1809     return res;
1810 }
1811
1812
1813 /***********************************************************************
1814  *           SendMessageA   (USER32.454)
1815  */
1816 LRESULT WINAPI SendMessageA( HWND hwnd, UINT msg, WPARAM wParam,
1817                                LPARAM lParam )
1818         {
1819     LRESULT res;
1820
1821     MSG_SendMessage(hwnd, msg, wParam, lParam, INFINITE,
1822                     SMSG_WIN32, &res);
1823
1824     return res;
1825 }
1826
1827
1828 /***********************************************************************
1829  *           SendMessageW   (USER32.459)  Send Window Message
1830  *
1831  *  Sends a message to the window procedure of the specified window.
1832  *  SendMessage() will not return until the called window procedure
1833  *  either returns or calls ReplyMessage().
1834  *
1835  *  Use PostMessage() to send message and return immediately. A window
1836  *  procedure may use InSendMessage() to detect
1837  *  SendMessage()-originated messages.
1838  *
1839  *  Applications which communicate via HWND_BROADCAST may use
1840  *  RegisterWindowMessage() to obtain a unique message to avoid conflicts
1841  *  with other applications.
1842  *
1843  * CONFORMANCE
1844  * 
1845  *  ECMA-234, Win32 
1846  */
1847 LRESULT WINAPI SendMessageW( 
1848   HWND hwnd,    /* Window to send message to. If HWND_BROADCAST, 
1849                  the message will be sent to all top-level windows. */
1850
1851   UINT msg,      /* message */
1852   WPARAM wParam, /* message parameter */
1853   LPARAM lParam    /* additional message parameter */
1854 ) {
1855     LRESULT res;
1856
1857     MSG_SendMessage(hwnd, msg, wParam, lParam, INFINITE,
1858                     SMSG_WIN32 | SMSG_UNICODE, &res);
1859
1860     return res;
1861 }
1862
1863
1864 /***********************************************************************
1865  *           SendMessageTimeout16    (not a WINAPI)
1866  */
1867 LRESULT WINAPI SendMessageTimeout16( HWND16 hwnd, UINT16 msg, WPARAM16 wParam,
1868                                      LPARAM lParam, UINT16 flags,
1869                                      UINT16 timeout, LPWORD resultp)
1870 {
1871     LRESULT ret;
1872     LRESULT msgRet;
1873     
1874     /* FIXME: need support for SMTO_BLOCK */
1875     
1876     ret = MSG_SendMessage(hwnd, msg, wParam, lParam, timeout, 0, &msgRet);
1877     if (resultp) *resultp = (WORD) msgRet;
1878     return ret;
1879 }
1880
1881
1882 /***********************************************************************
1883  *           SendMessageTimeoutA   (USER32.457)
1884  */
1885 LRESULT WINAPI SendMessageTimeoutA( HWND hwnd, UINT msg, WPARAM wParam,
1886                                       LPARAM lParam, UINT flags,
1887                                       UINT timeout, LPDWORD resultp)
1888 {
1889     LRESULT ret;
1890     LRESULT msgRet;
1891
1892     /* FIXME: need support for SMTO_BLOCK */
1893     
1894     ret = MSG_SendMessage(hwnd, msg, wParam, lParam, timeout, SMSG_WIN32,
1895                           &msgRet);
1896
1897     if (resultp) *resultp = (DWORD) msgRet;
1898     return ret;
1899 }
1900
1901
1902 /***********************************************************************
1903  *           SendMessageTimeoutW   (USER32.458)
1904  */
1905 LRESULT WINAPI SendMessageTimeoutW( HWND hwnd, UINT msg, WPARAM wParam,
1906                                       LPARAM lParam, UINT flags,
1907                                       UINT timeout, LPDWORD resultp)
1908 {
1909     LRESULT ret;
1910     LRESULT msgRet;
1911     
1912     /* FIXME: need support for SMTO_BLOCK */
1913
1914     ret = MSG_SendMessage(hwnd, msg, wParam, lParam, timeout,
1915                           SMSG_WIN32 | SMSG_UNICODE, &msgRet);
1916     
1917     if (resultp) *resultp = (DWORD) msgRet;
1918     return ret;
1919 }
1920
1921
1922 /***********************************************************************
1923  *  WaitMessage    (USER.112) (USER32.578)  Suspend thread pending messages
1924  *
1925  * WaitMessage() suspends a thread until events appear in the thread's
1926  * queue.
1927  *
1928  * BUGS
1929  *
1930  * Is supposed to return BOOL under Win32.
1931  *
1932  * Thread-local message queues are not supported.
1933  *
1934  * CONFORMANCE
1935  *
1936  * ECMA-234, Win32
1937  * 
1938  */
1939 void WINAPI WaitMessage( void )
1940 {
1941     QUEUE_WaitBits( QS_ALLINPUT, INFINITE );
1942 }
1943
1944 /***********************************************************************
1945  *           MsgWaitForMultipleObjects    (USER32.400)
1946  */
1947 DWORD WINAPI MsgWaitForMultipleObjects( DWORD nCount, HANDLE *pHandles,
1948                                         BOOL fWaitAll, DWORD dwMilliseconds,
1949                                         DWORD dwWakeMask )
1950 {
1951     DWORD i;
1952     HANDLE handles[MAXIMUM_WAIT_OBJECTS];
1953     DWORD ret;
1954
1955     HQUEUE16 hQueue = GetFastQueue16();
1956     MESSAGEQUEUE *msgQueue = (MESSAGEQUEUE *)QUEUE_Lock( hQueue );
1957     if (!msgQueue) return WAIT_FAILED;
1958
1959     if (nCount > MAXIMUM_WAIT_OBJECTS-1)
1960     {
1961         SetLastError( ERROR_INVALID_PARAMETER );
1962         QUEUE_Unlock( msgQueue );
1963         return WAIT_FAILED;
1964     }
1965
1966     msgQueue->changeBits = 0;
1967     msgQueue->wakeMask = dwWakeMask;
1968
1969     if (THREAD_IsWin16(NtCurrentTeb()))
1970     {
1971       /*
1972        * This is a temporary solution to a big problem.
1973        * You see, the main thread of all Win32 programs is created as a 16 bit
1974        * task. This means that if you want on an event using Win32 synchronization
1975        * methods, the 16 bit scheduler is stopped and things might just stop happening.
1976        * This implements a semi-busy loop that checks the handles to wait on and
1977        * also the message queue. When either one is ready, the wait function returns.
1978        *
1979        * This will all go away when the real Win32 threads are implemented for all
1980        * the threads of an applications. Including the main thread.
1981        */
1982       DWORD curTime = GetCurrentTime();
1983
1984       do
1985       {
1986         /*
1987          * Check the handles in the list.
1988          */
1989         ret = WaitForMultipleObjects(nCount, pHandles, fWaitAll, 5L);
1990
1991         /*
1992          * If the handles have been triggered, return.
1993          */
1994         if (ret != WAIT_TIMEOUT)
1995           break;
1996
1997         /*
1998          * Then, let the 16 bit scheduler do it's thing.
1999          */
2000         Yield16();
2001
2002         /*
2003          * If a message matching the wait mask has arrived, return.
2004          */
2005         if (msgQueue->changeBits & dwWakeMask)
2006         {
2007           ret = nCount;
2008           break;
2009         }
2010
2011         /*
2012          * And continue doing this until we hit the timeout.
2013          */
2014       } while ((dwMilliseconds == INFINITE) || (GetCurrentTime()-curTime < dwMilliseconds) );
2015     }
2016     else
2017     {
2018     /* Add the thread event to the handle list */
2019       for (i = 0; i < nCount; i++)
2020         handles[i] = pHandles[i];
2021       handles[nCount] = msgQueue->server_queue;
2022       ret = WaitForMultipleObjects( nCount+1, handles, fWaitAll, dwMilliseconds );
2023     } 
2024     QUEUE_Unlock( msgQueue );
2025     return ret;
2026 }
2027
2028
2029
2030 struct accent_char
2031 {
2032     BYTE ac_accent;
2033     BYTE ac_char;
2034     BYTE ac_result;
2035 };
2036
2037 static const struct accent_char accent_chars[] =
2038 {
2039 /* A good idea should be to read /usr/X11/lib/X11/locale/iso8859-x/Compose */
2040     {'`', 'A', '\300'},  {'`', 'a', '\340'},
2041     {'\'', 'A', '\301'}, {'\'', 'a', '\341'},
2042     {'^', 'A', '\302'},  {'^', 'a', '\342'},
2043     {'~', 'A', '\303'},  {'~', 'a', '\343'},
2044     {'"', 'A', '\304'},  {'"', 'a', '\344'},
2045     {'O', 'A', '\305'},  {'o', 'a', '\345'},
2046     {'0', 'A', '\305'},  {'0', 'a', '\345'},
2047     {'A', 'A', '\305'},  {'a', 'a', '\345'},
2048     {'A', 'E', '\306'},  {'a', 'e', '\346'},
2049     {',', 'C', '\307'},  {',', 'c', '\347'},
2050     {'`', 'E', '\310'},  {'`', 'e', '\350'},
2051     {'\'', 'E', '\311'}, {'\'', 'e', '\351'},
2052     {'^', 'E', '\312'},  {'^', 'e', '\352'},
2053     {'"', 'E', '\313'},  {'"', 'e', '\353'},
2054     {'`', 'I', '\314'},  {'`', 'i', '\354'},
2055     {'\'', 'I', '\315'}, {'\'', 'i', '\355'},
2056     {'^', 'I', '\316'},  {'^', 'i', '\356'},
2057     {'"', 'I', '\317'},  {'"', 'i', '\357'},
2058     {'-', 'D', '\320'},  {'-', 'd', '\360'},
2059     {'~', 'N', '\321'},  {'~', 'n', '\361'},
2060     {'`', 'O', '\322'},  {'`', 'o', '\362'},
2061     {'\'', 'O', '\323'}, {'\'', 'o', '\363'},
2062     {'^', 'O', '\324'},  {'^', 'o', '\364'},
2063     {'~', 'O', '\325'},  {'~', 'o', '\365'},
2064     {'"', 'O', '\326'},  {'"', 'o', '\366'},
2065     {'/', 'O', '\330'},  {'/', 'o', '\370'},
2066     {'`', 'U', '\331'},  {'`', 'u', '\371'},
2067     {'\'', 'U', '\332'}, {'\'', 'u', '\372'},
2068     {'^', 'U', '\333'},  {'^', 'u', '\373'},
2069     {'"', 'U', '\334'},  {'"', 'u', '\374'},
2070     {'\'', 'Y', '\335'}, {'\'', 'y', '\375'},
2071     {'T', 'H', '\336'},  {'t', 'h', '\376'},
2072     {'s', 's', '\337'},  {'"', 'y', '\377'},
2073     {'s', 'z', '\337'},  {'i', 'j', '\377'},
2074         /* iso-8859-2 uses this */
2075     {'<', 'L', '\245'},  {'<', 'l', '\265'},    /* caron */
2076     {'<', 'S', '\251'},  {'<', 's', '\271'},
2077     {'<', 'T', '\253'},  {'<', 't', '\273'},
2078     {'<', 'Z', '\256'},  {'<', 'z', '\276'},
2079     {'<', 'C', '\310'},  {'<', 'c', '\350'},
2080     {'<', 'E', '\314'},  {'<', 'e', '\354'},
2081     {'<', 'D', '\317'},  {'<', 'd', '\357'},
2082     {'<', 'N', '\322'},  {'<', 'n', '\362'},
2083     {'<', 'R', '\330'},  {'<', 'r', '\370'},
2084     {';', 'A', '\241'},  {';', 'a', '\261'},    /* ogonek */
2085     {';', 'E', '\312'},  {';', 'e', '\332'},
2086     {'\'', 'Z', '\254'}, {'\'', 'z', '\274'},   /* acute */
2087     {'\'', 'R', '\300'}, {'\'', 'r', '\340'},
2088     {'\'', 'L', '\305'}, {'\'', 'l', '\345'},
2089     {'\'', 'C', '\306'}, {'\'', 'c', '\346'},
2090     {'\'', 'N', '\321'}, {'\'', 'n', '\361'},
2091 /*  collision whith S, from iso-8859-9 !!! */
2092     {',', 'S', '\252'},  {',', 's', '\272'},    /* cedilla */
2093     {',', 'T', '\336'},  {',', 't', '\376'},
2094     {'.', 'Z', '\257'},  {'.', 'z', '\277'},    /* dot above */
2095     {'/', 'L', '\243'},  {'/', 'l', '\263'},    /* slash */
2096     {'/', 'D', '\320'},  {'/', 'd', '\360'},
2097     {'(', 'A', '\303'},  {'(', 'a', '\343'},    /* breve */
2098     {'\275', 'O', '\325'}, {'\275', 'o', '\365'},       /* double acute */
2099     {'\275', 'U', '\334'}, {'\275', 'u', '\374'},
2100     {'0', 'U', '\332'},  {'0', 'u', '\372'},    /* ring above */
2101         /* iso-8859-3 uses this */
2102     {'/', 'H', '\241'},  {'/', 'h', '\261'},    /* slash */
2103     {'>', 'H', '\246'},  {'>', 'h', '\266'},    /* circumflex */
2104     {'>', 'J', '\254'},  {'>', 'j', '\274'},
2105     {'>', 'C', '\306'},  {'>', 'c', '\346'},
2106     {'>', 'G', '\330'},  {'>', 'g', '\370'},
2107     {'>', 'S', '\336'},  {'>', 's', '\376'},
2108 /*  collision whith G( from iso-8859-9 !!!   */
2109     {'(', 'G', '\253'},  {'(', 'g', '\273'},    /* breve */
2110     {'(', 'U', '\335'},  {'(', 'u', '\375'},
2111 /*  collision whith I. from iso-8859-3 !!!   */
2112     {'.', 'I', '\251'},  {'.', 'i', '\271'},    /* dot above */
2113     {'.', 'C', '\305'},  {'.', 'c', '\345'},
2114     {'.', 'G', '\325'},  {'.', 'g', '\365'},
2115         /* iso-8859-4 uses this */
2116     {',', 'R', '\243'},  {',', 'r', '\263'},    /* cedilla */
2117     {',', 'L', '\246'},  {',', 'l', '\266'},
2118     {',', 'G', '\253'},  {',', 'g', '\273'},
2119     {',', 'N', '\321'},  {',', 'n', '\361'},
2120     {',', 'K', '\323'},  {',', 'k', '\363'},
2121     {'~', 'I', '\245'},  {'~', 'i', '\265'},    /* tilde */
2122     {'-', 'E', '\252'},  {'-', 'e', '\272'},    /* macron */
2123     {'-', 'A', '\300'},  {'-', 'a', '\340'},
2124     {'-', 'I', '\317'},  {'-', 'i', '\357'},
2125     {'-', 'O', '\322'},  {'-', 'o', '\362'},
2126     {'-', 'U', '\336'},  {'-', 'u', '\376'},
2127     {'/', 'T', '\254'},  {'/', 't', '\274'},    /* slash */
2128     {'.', 'E', '\314'},  {'.', 'e', '\344'},    /* dot above */
2129     {';', 'I', '\307'},  {';', 'i', '\347'},    /* ogonek */
2130     {';', 'U', '\331'},  {';', 'u', '\371'},
2131         /* iso-8859-9 uses this */
2132         /* iso-8859-9 has really bad choosen G( S, and I. as they collide
2133          * whith the same letters on other iso-8859-x (that is they are on
2134          * different places :-( ), if you use turkish uncomment these and
2135          * comment out the lines in iso-8859-2 and iso-8859-3 sections
2136          * FIXME: should be dynamic according to chosen language
2137          *        if/when Wine has turkish support.  
2138          */ 
2139 /*  collision whith G( from iso-8859-3 !!!   */
2140 /*  {'(', 'G', '\320'},  {'(', 'g', '\360'}, */ /* breve */
2141 /*  collision whith S, from iso-8859-2 !!! */
2142 /*  {',', 'S', '\336'},  {',', 's', '\376'}, */ /* cedilla */
2143 /*  collision whith I. from iso-8859-3 !!!   */
2144 /*  {'.', 'I', '\335'},  {'.', 'i', '\375'}, */ /* dot above */
2145 };
2146
2147
2148 /***********************************************************************
2149  *           MSG_DoTranslateMessage
2150  *
2151  * Implementation of TranslateMessage.
2152  *
2153  * TranslateMessage translates virtual-key messages into character-messages,
2154  * as follows :
2155  * WM_KEYDOWN/WM_KEYUP combinations produce a WM_CHAR or WM_DEADCHAR message.
2156  * ditto replacing WM_* with WM_SYS*
2157  * This produces WM_CHAR messages only for keys mapped to ASCII characters
2158  * by the keyboard driver.
2159  */
2160 static BOOL MSG_DoTranslateMessage( UINT message, HWND hwnd,
2161                                       WPARAM wParam, LPARAM lParam )
2162 {
2163     static int dead_char;
2164     BYTE wp[2];
2165     
2166     if (message != WM_MOUSEMOVE && message != WM_TIMER)
2167         TRACE("(%s, %04X, %08lX)\n",
2168                      SPY_GetMsgName(message), wParam, lParam );
2169     if(message >= WM_KEYFIRST && message <= WM_KEYLAST)
2170         TRACE_(key)("(%s, %04X, %08lX)\n",
2171                      SPY_GetMsgName(message), wParam, lParam );
2172
2173     if ((message != WM_KEYDOWN) && (message != WM_SYSKEYDOWN))  return FALSE;
2174
2175     TRACE_(key)("Translating key %04X, scancode %04X\n",
2176                  wParam, HIWORD(lParam) );
2177
2178     /* FIXME : should handle ToAscii yielding 2 */
2179     switch (ToAscii(wParam, HIWORD(lParam),
2180                       QueueKeyStateTable,(LPWORD)wp, 0)) 
2181     {
2182     case 1 :
2183         message = (message == WM_KEYDOWN) ? WM_CHAR : WM_SYSCHAR;
2184         /* Should dead chars handling go in ToAscii ? */
2185         if (dead_char)
2186         {
2187             int i;
2188
2189             if (wp[0] == ' ') wp[0] =  dead_char;
2190             if (dead_char == 0xa2) dead_char = '(';
2191             else if (dead_char == 0xa8) dead_char = '"';
2192             else if (dead_char == 0xb2) dead_char = ';';
2193             else if (dead_char == 0xb4) dead_char = '\'';
2194             else if (dead_char == 0xb7) dead_char = '<';
2195             else if (dead_char == 0xb8) dead_char = ',';
2196             else if (dead_char == 0xff) dead_char = '.';
2197             for (i = 0; i < sizeof(accent_chars)/sizeof(accent_chars[0]); i++)
2198                 if ((accent_chars[i].ac_accent == dead_char) &&
2199                     (accent_chars[i].ac_char == wp[0]))
2200                 {
2201                     wp[0] = accent_chars[i].ac_result;
2202                     break;
2203                 }
2204             dead_char = 0;
2205         }
2206         TRACE_(key)("1 -> PostMessage(%s)\n", SPY_GetMsgName(message));
2207         PostMessage16( hwnd, message, wp[0], lParam );
2208         return TRUE;
2209
2210     case -1 :
2211         message = (message == WM_KEYDOWN) ? WM_DEADCHAR : WM_SYSDEADCHAR;
2212         dead_char = wp[0];
2213         TRACE_(key)("-1 -> PostMessage(%s)\n",
2214                      SPY_GetMsgName(message));
2215         PostMessage16( hwnd, message, wp[0], lParam );
2216         return TRUE;
2217     }
2218     return FALSE;
2219 }
2220
2221
2222 /***********************************************************************
2223  *           TranslateMessage16   (USER.113)
2224  */
2225 BOOL16 WINAPI TranslateMessage16( const MSG16 *msg )
2226 {
2227     return MSG_DoTranslateMessage( msg->message, msg->hwnd,
2228                                    msg->wParam, msg->lParam );
2229 }
2230
2231
2232 /***********************************************************************
2233  *           TranslateMessage32   (USER.821)
2234  */
2235 BOOL16 WINAPI TranslateMessage32_16( const MSG32_16 *msg, BOOL16 wHaveParamHigh )
2236 {
2237     WPARAM wParam;
2238
2239     if (wHaveParamHigh)
2240         wParam = MAKELONG(msg->msg.wParam, msg->wParamHigh);
2241     else
2242         wParam = (WPARAM)msg->msg.wParam;
2243
2244     return MSG_DoTranslateMessage( msg->msg.message, msg->msg.hwnd,
2245                                    wParam, msg->msg.lParam );
2246 }
2247
2248 /***********************************************************************
2249  *           TranslateMessage   (USER32.556)
2250  */
2251 BOOL WINAPI TranslateMessage( const MSG *msg )
2252 {
2253     return MSG_DoTranslateMessage( msg->message, msg->hwnd,
2254                                    msg->wParam, msg->lParam );
2255 }
2256
2257
2258 /***********************************************************************
2259  *           DispatchMessage16   (USER.114)
2260  */
2261 LONG WINAPI DispatchMessage16( const MSG16* msg )
2262 {
2263     WND * wndPtr;
2264     LONG retval;
2265     int painting;
2266     
2267       /* Process timer messages */
2268     if ((msg->message == WM_TIMER) || (msg->message == WM_SYSTIMER))
2269     {
2270         if (msg->lParam)
2271         {
2272             return CallWindowProc16( (WNDPROC16)msg->lParam, msg->hwnd,
2273                                    msg->message, msg->wParam, GetTickCount() );
2274         }
2275     }
2276
2277     if (!msg->hwnd) return 0;
2278     if (!(wndPtr = WIN_FindWndPtr( msg->hwnd ))) return 0;
2279     if (!wndPtr->winproc)
2280     {
2281         retval = 0;
2282         goto END;
2283     }
2284     painting = (msg->message == WM_PAINT);
2285     if (painting) wndPtr->flags |= WIN_NEEDS_BEGINPAINT;
2286
2287     SPY_EnterMessage( SPY_DISPATCHMESSAGE16, msg->hwnd, msg->message,
2288                       msg->wParam, msg->lParam );
2289     retval = CallWindowProc16( (WNDPROC16)wndPtr->winproc,
2290                                msg->hwnd, msg->message,
2291                                msg->wParam, msg->lParam );
2292     SPY_ExitMessage( SPY_RESULT_OK16, msg->hwnd, msg->message, retval );
2293
2294     WIN_ReleaseWndPtr(wndPtr);
2295     wndPtr = WIN_FindWndPtr(msg->hwnd);
2296     if (painting && wndPtr &&
2297         (wndPtr->flags & WIN_NEEDS_BEGINPAINT) && wndPtr->hrgnUpdate)
2298     {
2299         ERR("BeginPaint not called on WM_PAINT for hwnd %04x!\n", 
2300             msg->hwnd);
2301         wndPtr->flags &= ~WIN_NEEDS_BEGINPAINT;
2302         /* Validate the update region to avoid infinite WM_PAINT loop */
2303         ValidateRect( msg->hwnd, NULL );
2304     }
2305 END:
2306     WIN_ReleaseWndPtr(wndPtr);
2307     return retval;
2308 }
2309
2310
2311 /***********************************************************************
2312  *           DispatchMessage32   (USER.822)
2313  */
2314 LONG WINAPI DispatchMessage32_16( const MSG32_16* lpmsg16_32, BOOL16 wHaveParamHigh )
2315 {
2316     if (wHaveParamHigh == FALSE)
2317         return DispatchMessage16(&(lpmsg16_32->msg));
2318     else
2319     {
2320         MSG msg;
2321
2322         msg.hwnd = lpmsg16_32->msg.hwnd;
2323         msg.message = lpmsg16_32->msg.message;
2324         msg.wParam = MAKELONG(lpmsg16_32->msg.wParam, lpmsg16_32->wParamHigh);
2325         msg.lParam = lpmsg16_32->msg.lParam;
2326         msg.time = lpmsg16_32->msg.time;
2327         msg.pt.x = (INT)lpmsg16_32->msg.pt.x;
2328         msg.pt.y = (INT)lpmsg16_32->msg.pt.y;
2329         return DispatchMessageA(&msg);
2330     }
2331 }
2332
2333 /***********************************************************************
2334  *           DispatchMessageA   (USER32.141)
2335  */
2336 LONG WINAPI DispatchMessageA( const MSG* msg )
2337 {
2338     WND * wndPtr;
2339     LONG retval;
2340     int painting;
2341     
2342       /* Process timer messages */
2343     if ((msg->message == WM_TIMER) || (msg->message == WM_SYSTIMER))
2344     {
2345         if (msg->lParam)
2346         {
2347 /*            HOOK_CallHooks32A( WH_CALLWNDPROC, HC_ACTION, 0, FIXME ); */
2348             return CallWindowProcA( (WNDPROC)msg->lParam, msg->hwnd,
2349                                    msg->message, msg->wParam, GetTickCount() );
2350         }
2351     }
2352
2353     if (!msg->hwnd) return 0;
2354     if (!(wndPtr = WIN_FindWndPtr( msg->hwnd ))) return 0;
2355     if (!wndPtr->winproc)
2356     {
2357         retval = 0;
2358         goto END;
2359     }
2360     painting = (msg->message == WM_PAINT);
2361     if (painting) wndPtr->flags |= WIN_NEEDS_BEGINPAINT;
2362 /*    HOOK_CallHooks32A( WH_CALLWNDPROC, HC_ACTION, 0, FIXME ); */
2363
2364     SPY_EnterMessage( SPY_DISPATCHMESSAGE, msg->hwnd, msg->message,
2365                       msg->wParam, msg->lParam );
2366     retval = CallWindowProcA( (WNDPROC)wndPtr->winproc,
2367                                 msg->hwnd, msg->message,
2368                                 msg->wParam, msg->lParam );
2369     SPY_ExitMessage( SPY_RESULT_OK, msg->hwnd, msg->message, retval );
2370
2371     WIN_ReleaseWndPtr(wndPtr);
2372     wndPtr = WIN_FindWndPtr(msg->hwnd);
2373
2374     if (painting && wndPtr &&
2375         (wndPtr->flags & WIN_NEEDS_BEGINPAINT) && wndPtr->hrgnUpdate)
2376     {
2377         ERR("BeginPaint not called on WM_PAINT for hwnd %04x!\n", 
2378             msg->hwnd);
2379         wndPtr->flags &= ~WIN_NEEDS_BEGINPAINT;
2380         /* Validate the update region to avoid infinite WM_PAINT loop */
2381         ValidateRect( msg->hwnd, NULL );
2382     }
2383 END:
2384     WIN_ReleaseWndPtr(wndPtr);
2385     return retval;
2386 }
2387
2388
2389 /***********************************************************************
2390  *           DispatchMessageW   (USER32.142)     Process Message
2391  *
2392  * Process the message specified in the structure *_msg_.
2393  *
2394  * If the lpMsg parameter points to a WM_TIMER message and the
2395  * parameter of the WM_TIMER message is not NULL, the lParam parameter
2396  * points to the function that is called instead of the window
2397  * procedure.
2398  *  
2399  * The message must be valid.
2400  *
2401  * RETURNS
2402  *
2403  *   DispatchMessage() returns the result of the window procedure invoked.
2404  *
2405  * CONFORMANCE
2406  *
2407  *   ECMA-234, Win32 
2408  *
2409  */
2410 LONG WINAPI DispatchMessageW( const MSG* msg )
2411 {
2412     WND * wndPtr;
2413     LONG retval;
2414     int painting;
2415     
2416       /* Process timer messages */
2417     if ((msg->message == WM_TIMER) || (msg->message == WM_SYSTIMER))
2418     {
2419         if (msg->lParam)
2420         {
2421 /*            HOOK_CallHooks32W( WH_CALLWNDPROC, HC_ACTION, 0, FIXME ); */
2422             return CallWindowProcW( (WNDPROC)msg->lParam, msg->hwnd,
2423                                    msg->message, msg->wParam, GetTickCount() );
2424         }
2425     }
2426
2427     if (!msg->hwnd) return 0;
2428     if (!(wndPtr = WIN_FindWndPtr( msg->hwnd ))) return 0;
2429     if (!wndPtr->winproc)
2430     {
2431         retval = 0;
2432         goto END;
2433     }
2434     painting = (msg->message == WM_PAINT);
2435     if (painting) wndPtr->flags |= WIN_NEEDS_BEGINPAINT;
2436 /*    HOOK_CallHooks32W( WH_CALLWNDPROC, HC_ACTION, 0, FIXME ); */
2437
2438     SPY_EnterMessage( SPY_DISPATCHMESSAGE, msg->hwnd, msg->message,
2439                       msg->wParam, msg->lParam );
2440     retval = CallWindowProcW( (WNDPROC)wndPtr->winproc,
2441                                 msg->hwnd, msg->message,
2442                                 msg->wParam, msg->lParam );
2443     SPY_ExitMessage( SPY_RESULT_OK, msg->hwnd, msg->message, retval );
2444
2445     WIN_ReleaseWndPtr(wndPtr);
2446     wndPtr = WIN_FindWndPtr(msg->hwnd);
2447
2448     if (painting && wndPtr &&
2449         (wndPtr->flags & WIN_NEEDS_BEGINPAINT) && wndPtr->hrgnUpdate)
2450     {
2451         ERR("BeginPaint not called on WM_PAINT for hwnd %04x!\n", 
2452             msg->hwnd);
2453         wndPtr->flags &= ~WIN_NEEDS_BEGINPAINT;
2454         /* Validate the update region to avoid infinite WM_PAINT loop */
2455         ValidateRect( msg->hwnd, NULL );
2456     }
2457 END:
2458     WIN_ReleaseWndPtr(wndPtr);
2459     return retval;
2460 }
2461
2462
2463 /***********************************************************************
2464  *           RegisterWindowMessageA   (USER.118) (USER32.437)
2465  */
2466 WORD WINAPI RegisterWindowMessageA( LPCSTR str )
2467 {
2468     TRACE("%s\n", str );
2469     return GlobalAddAtomA( str );
2470 }
2471
2472
2473 /***********************************************************************
2474  *           RegisterWindowMessageW   (USER32.438)
2475  */
2476 WORD WINAPI RegisterWindowMessageW( LPCWSTR str )
2477 {
2478     TRACE("%p\n", str );
2479     return GlobalAddAtomW( str );
2480 }
2481
2482
2483 /***********************************************************************
2484  *           GetCurrentTime16    (USER.15)
2485  *
2486  * (effectively identical to GetTickCount)
2487  */
2488 DWORD WINAPI GetCurrentTime16(void)
2489 {
2490     return GetTickCount();
2491 }
2492
2493
2494 /***********************************************************************
2495  *           InSendMessage16    (USER.192)
2496  */
2497 BOOL16 WINAPI InSendMessage16(void)
2498 {
2499     return InSendMessage();
2500 }
2501
2502
2503 /***********************************************************************
2504  *           InSendMessage    (USER32.320)
2505  */
2506 BOOL WINAPI InSendMessage(void)
2507 {
2508     MESSAGEQUEUE *queue;
2509     BOOL ret;
2510
2511     if (!(queue = (MESSAGEQUEUE *)QUEUE_Lock( GetFastQueue16() )))
2512         return 0;
2513     ret = (BOOL)queue->smWaiting;
2514
2515     QUEUE_Unlock( queue );
2516     return ret;
2517 }
2518
2519 /***********************************************************************
2520  *           BroadcastSystemMessage    (USER32.12)
2521  */
2522 LONG WINAPI BroadcastSystemMessage(
2523         DWORD dwFlags,LPDWORD recipients,UINT uMessage,WPARAM wParam,
2524         LPARAM lParam
2525 ) {
2526         FIXME_(sendmsg)("(%08lx,%08lx,%08x,%08x,%08lx): stub!\n",
2527               dwFlags,*recipients,uMessage,wParam,lParam
2528         );
2529         return 0;
2530 }
2531
2532 /***********************************************************************
2533  *           SendNotifyMessageA    (USER32.460)
2534  * FIXME
2535  *  The message sended with PostMessage has to be put in the queue
2536  *  with a higher priority as the other "Posted" messages.
2537  *  QUEUE_AddMsg has to be modifyed.
2538  */
2539 BOOL WINAPI SendNotifyMessageA(HWND hwnd,UINT msg,WPARAM wParam,LPARAM lParam)
2540 {       BOOL ret = TRUE;
2541         FIXME("(%04x,%08x,%08x,%08lx) not complete\n",
2542               hwnd, msg, wParam, lParam);
2543               
2544         if ( GetCurrentThreadId() == GetWindowThreadProcessId ( hwnd, NULL))
2545         {       ret=SendMessageA ( hwnd, msg, wParam, lParam );
2546         }
2547         else
2548         {       PostMessageA ( hwnd, msg, wParam, lParam );
2549         }
2550         return ret;
2551 }
2552
2553 /***********************************************************************
2554  *           SendNotifyMessageW    (USER32.461)
2555  * FIXME
2556  *  The message sended with PostMessage has to be put in the queue
2557  *  with a higher priority as the other "Posted" messages.
2558  *  QUEUE_AddMsg has to be modifyed.
2559  */
2560 BOOL WINAPI SendNotifyMessageW(HWND hwnd,UINT msg,WPARAM wParam,LPARAM lParam)
2561 {       BOOL ret = TRUE;
2562         FIXME("(%04x,%08x,%08x,%08lx) not complete\n",
2563               hwnd, msg, wParam, lParam);
2564
2565         if ( GetCurrentThreadId() == GetWindowThreadProcessId ( hwnd, NULL))
2566         {       ret=SendMessageW ( hwnd, msg, wParam, lParam );
2567         }
2568         else
2569         {       PostMessageW ( hwnd, msg, wParam, lParam );
2570         }
2571         return ret;
2572 }
2573
2574 /***********************************************************************
2575  *           SendMessageCallbackA
2576  * FIXME: It's like PostMessage. The callback gets called when the message
2577  * is processed. We have to modify the message processing for a exact
2578  * implementation...
2579  */
2580 BOOL WINAPI SendMessageCallbackA(
2581         HWND hWnd,UINT Msg,WPARAM wParam,LPARAM lParam,
2582         FARPROC lpResultCallBack,DWORD dwData)
2583 {       
2584         FIXME("(0x%04x,0x%04x,0x%08x,0x%08lx,%p,0x%08lx),stub!\n",
2585               hWnd,Msg,wParam,lParam,lpResultCallBack,dwData);
2586         if ( hWnd == HWND_BROADCAST)
2587         {       PostMessageA( hWnd, Msg, wParam, lParam);
2588                 FIXME("Broadcast: Callback will not be called!\n");
2589                 return TRUE;
2590         }
2591         (lpResultCallBack)( hWnd, Msg, dwData, SendMessageA ( hWnd, Msg, wParam, lParam ));
2592                 return TRUE;
2593 }
2594 /***********************************************************************
2595  *           SendMessageCallbackW
2596  * FIXME: It's like PostMessage. The callback gets called when the message
2597  * is processed. We have to modify the message processing for a exact
2598  * implementation...
2599  */
2600 BOOL WINAPI SendMessageCallbackW(
2601         HWND hWnd,UINT Msg,WPARAM wParam,LPARAM lParam,
2602         FARPROC lpResultCallBack,DWORD dwData)
2603 {       
2604         FIXME("(0x%04x,0x%04x,0x%08x,0x%08lx,%p,0x%08lx),stub!\n",
2605               hWnd,Msg,wParam,lParam,lpResultCallBack,dwData);
2606         if ( hWnd == HWND_BROADCAST)
2607         {       PostMessageW( hWnd, Msg, wParam, lParam);
2608                 FIXME("Broadcast: Callback will not be called!\n");
2609                 return TRUE;
2610         }
2611         (lpResultCallBack)( hWnd, Msg, dwData, SendMessageA ( hWnd, Msg, wParam, lParam ));
2612                 return TRUE;
2613 }