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