New stubs PrivateExtractIconEx[AW], PrivateExtractIconsW,
[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 "message.h"
14 #include "win.h"
15 #include "gdi.h"
16 #include "sysmetrics.h"
17 #include "heap.h"
18 #include "hook.h"
19 #include "keyboard.h"
20 #include "spy.h"
21 #include "winpos.h"
22 #include "atom.h"
23 #include "dde.h"
24 #include "queue.h"
25 #include "winproc.h"
26 #include "task.h"
27 #include "process.h"
28 #include "thread.h"
29 #include "options.h"
30 #include "struct32.h"
31 #include "debug.h"
32
33 #define WM_NCMOUSEFIRST         WM_NCMOUSEMOVE
34 #define WM_NCMOUSELAST          WM_NCMBUTTONDBLCLK
35
36 typedef enum { SYSQ_MSG_ABANDON, SYSQ_MSG_SKIP, 
37                SYSQ_MSG_ACCEPT, SYSQ_MSG_CONTINUE } SYSQ_STATUS;
38
39 extern MESSAGEQUEUE *pCursorQueue;                       /* queue.c */
40 extern MESSAGEQUEUE *pActiveQueue;
41
42 DWORD MSG_WineStartTicks; /* Ticks at Wine startup */
43
44 static UINT32 doubleClickSpeed = 452;
45 static INT32 debugSMRL = 0;       /* intertask SendMessage() recursion level */
46
47 /***********************************************************************
48  *           MSG_CheckFilter
49  */
50 BOOL32 MSG_CheckFilter(WORD uMsg, DWORD filter)
51 {
52    if( filter )
53        return (uMsg >= LOWORD(filter) && uMsg <= HIWORD(filter));
54    return TRUE;
55 }
56
57 /***********************************************************************
58  *           MSG_SendParentNotify
59  *
60  * Send a WM_PARENTNOTIFY to all ancestors of the given window, unless
61  * the window has the WS_EX_NOPARENTNOTIFY style.
62  */
63 static void MSG_SendParentNotify(WND* wndPtr, WORD event, WORD idChild, LPARAM lValue)
64 {
65 #define lppt ((LPPOINT16)&lValue)
66
67     /* pt has to be in the client coordinates of the parent window */
68
69     MapWindowPoints16( 0, wndPtr->hwndSelf, lppt, 1 );
70     while (wndPtr)
71     {
72         if (!(wndPtr->dwStyle & WS_CHILD) || (wndPtr->dwExStyle & WS_EX_NOPARENTNOTIFY)) break;
73         lppt->x += wndPtr->rectClient.left;
74         lppt->y += wndPtr->rectClient.top;
75         wndPtr = wndPtr->parent;
76         SendMessage32A( wndPtr->hwndSelf, WM_PARENTNOTIFY,
77                         MAKEWPARAM( event, idChild ), lValue );
78     }
79 #undef lppt
80 }
81
82
83 /***********************************************************************
84  *           MSG_TranslateMouseMsg
85  *
86  * Translate an mouse hardware event into a real mouse message.
87  * Return value indicates whether the translated message must be passed
88  * to the user, left in the queue, or skipped entirely (in this case
89  * HIWORD contains hit test code).
90  */
91 static DWORD MSG_TranslateMouseMsg( HWND16 hTopWnd, DWORD filter, 
92                                     MSG16 *msg, BOOL32 remove, WND* pWndScope )
93 {
94     static DWORD   dblclk_time_limit = 0;
95     static UINT16     clk_message = 0;
96     static HWND16     clk_hwnd = 0;
97     static POINT16    clk_pos = { 0, 0 };
98
99     WND *pWnd;
100     HWND16 hWnd;
101     INT16 ht, hittest, sendSC = 0;
102     UINT16 message = msg->message;
103     POINT16 screen_pt, pt;
104     HANDLE16 hQ = GetTaskQueue(0);
105     MESSAGEQUEUE *queue = (MESSAGEQUEUE *)GlobalLock16(hQ);
106     BOOL32 eatMsg = FALSE;
107     BOOL32 mouseClick = ((message == WM_LBUTTONDOWN) ||
108                          (message == WM_RBUTTONDOWN) ||
109                          (message == WM_MBUTTONDOWN))?1:0;
110     SYSQ_STATUS ret = 0;
111
112       /* Find the window */
113
114     ht = hittest = HTCLIENT;
115     hWnd = GetCapture16();
116     if( !hWnd )
117     {
118         ht = hittest = WINPOS_WindowFromPoint( pWndScope, msg->pt, &pWnd );
119         if( !pWnd ) pWnd = WIN_GetDesktop();
120         hWnd = pWnd->hwndSelf;
121         sendSC = 1;
122     } 
123     else 
124     {
125         pWnd = WIN_FindWndPtr(hWnd);
126         ht = EVENT_GetCaptureInfo();
127     }
128
129         /* stop if not the right queue */
130
131     if (pWnd->hmemTaskQ != hQ)
132     {
133         /* Not for the current task */
134         if (queue) QUEUE_ClearWakeBit( queue, QS_MOUSE );
135         /* Wake up the other task */
136         queue = (MESSAGEQUEUE *)GlobalLock16( pWnd->hmemTaskQ );
137         if (queue) QUEUE_SetWakeBit( queue, QS_MOUSE );
138         return SYSQ_MSG_ABANDON;
139     }
140
141         /* check if hWnd is within hWndScope */
142
143     if( hTopWnd && hWnd != hTopWnd )
144         if( !IsChild16(hTopWnd, hWnd) ) return SYSQ_MSG_CONTINUE;
145
146     if( mouseClick )
147     {
148         /* translate double clicks -
149          * note that ...MOUSEMOVEs can slip in between
150          * ...BUTTONDOWN and ...BUTTONDBLCLK messages */
151
152         if( pWnd->class->style & CS_DBLCLKS || ht != HTCLIENT )
153         {
154            if ((message == clk_message) && (hWnd == clk_hwnd) &&
155                (msg->time - dblclk_time_limit < doubleClickSpeed) &&
156                (abs(msg->pt.x - clk_pos.x) < SYSMETRICS_CXDOUBLECLK/2) &&
157                (abs(msg->pt.y - clk_pos.y) < SYSMETRICS_CYDOUBLECLK/2))
158            {
159               message += (WM_LBUTTONDBLCLK - WM_LBUTTONDOWN);
160               mouseClick++;   /* == 2 */
161            }
162         }
163     }
164     screen_pt = pt = msg->pt;
165
166     if (hittest != HTCLIENT)
167     {
168         message += ((INT16)WM_NCMOUSEMOVE - WM_MOUSEMOVE);
169         msg->wParam = hittest;
170     }
171     else ScreenToClient16( hWnd, &pt );
172
173         /* check message filter */
174
175     if (!MSG_CheckFilter(message, filter)) return SYSQ_MSG_CONTINUE;
176
177     pCursorQueue = queue;
178
179         /* call WH_MOUSE */
180
181     if (HOOK_IsHooked( WH_MOUSE ))
182     { 
183         MOUSEHOOKSTRUCT16 *hook = SEGPTR_NEW(MOUSEHOOKSTRUCT16);
184         if( hook )
185         {
186             hook->pt           = screen_pt;
187             hook->hwnd         = hWnd;
188             hook->wHitTestCode = hittest;
189             hook->dwExtraInfo  = 0;
190             ret = HOOK_CallHooks16( WH_MOUSE, remove ? HC_ACTION : HC_NOREMOVE,
191                                     message, (LPARAM)SEGPTR_GET(hook) );
192             SEGPTR_FREE(hook);
193         }
194         if( ret ) return MAKELONG((INT16)SYSQ_MSG_SKIP, hittest);
195     }
196
197     if ((hittest == HTERROR) || (hittest == HTNOWHERE)) 
198         eatMsg = sendSC = 1;
199     else if( remove && mouseClick )
200     {
201         HWND32 hwndTop = WIN_GetTopParent( hWnd );
202
203         if( mouseClick == 1 )
204         {
205             /* set conditions */
206             dblclk_time_limit = msg->time;
207                clk_message = msg->message;
208                clk_hwnd = hWnd;
209                clk_pos = screen_pt;
210         } else 
211             /* got double click - zero them out */
212             dblclk_time_limit = clk_hwnd = 0;
213
214         if( sendSC )
215         {
216             /* Send the WM_PARENTNOTIFY,
217              * note that even for double/nonclient clicks
218              * notification message is still WM_L/M/RBUTTONDOWN.
219              */
220
221             MSG_SendParentNotify( pWnd, msg->message, 0, MAKELPARAM(screen_pt.x, screen_pt.y) );
222
223             /* Activate the window if needed */
224
225             if (hWnd != GetActiveWindow16() && hWnd != GetDesktopWindow16())
226             {
227                 LONG ret = SendMessage16( hWnd, WM_MOUSEACTIVATE, hwndTop,
228                                           MAKELONG( hittest, message ) );
229
230                 if ((ret == MA_ACTIVATEANDEAT) || (ret == MA_NOACTIVATEANDEAT))
231                          eatMsg = TRUE;
232
233                 if (((ret == MA_ACTIVATE) || (ret == MA_ACTIVATEANDEAT)) 
234                       && hwndTop != GetActiveWindow16() )
235                       if (!WINPOS_SetActiveWindow( hwndTop, TRUE , TRUE ))
236                          eatMsg = TRUE;
237             }
238         }
239     } else sendSC = (remove && sendSC);
240
241      /* Send the WM_SETCURSOR message */
242
243     if (sendSC)
244         SendMessage16( hWnd, WM_SETCURSOR, (WPARAM16)hWnd,
245                        MAKELONG( hittest, message ));
246     if (eatMsg) return MAKELONG( (UINT16)SYSQ_MSG_SKIP, hittest);
247
248     msg->hwnd    = hWnd;
249     msg->message = message;
250     msg->lParam  = MAKELONG( pt.x, pt.y );
251     return SYSQ_MSG_ACCEPT;
252 }
253
254
255 /***********************************************************************
256  *           MSG_TranslateKbdMsg
257  *
258  * Translate an keyboard hardware event into a real message.
259  */
260 static DWORD MSG_TranslateKbdMsg( HWND16 hTopWnd, DWORD filter,
261                                   MSG16 *msg, BOOL32 remove )
262 {
263     WORD message = msg->message;
264     HWND16 hWnd = GetFocus16();
265     WND *pWnd;
266
267       /* Should check Ctrl-Esc and PrintScreen here */
268
269     if (!hWnd)
270     {
271           /* Send the message to the active window instead,  */
272           /* translating messages to their WM_SYS equivalent */
273
274         hWnd = GetActiveWindow16();
275
276         if( message < WM_SYSKEYDOWN )
277             message += WM_SYSKEYDOWN - WM_KEYDOWN;
278     }
279     pWnd = WIN_FindWndPtr( hWnd );
280     if (pWnd && (pWnd->hmemTaskQ != GetTaskQueue(0)))
281     {
282         /* Not for the current task */
283         MESSAGEQUEUE *queue = (MESSAGEQUEUE *)GlobalLock16( GetTaskQueue(0) );
284         if (queue) QUEUE_ClearWakeBit( queue, QS_KEY );
285         /* Wake up the other task */
286         queue = (MESSAGEQUEUE *)GlobalLock16( pWnd->hmemTaskQ );
287         if (queue) QUEUE_SetWakeBit( queue, QS_KEY );
288         return SYSQ_MSG_ABANDON;
289     }
290
291     if (hTopWnd && hWnd != hTopWnd)
292         if (!IsChild16(hTopWnd, hWnd)) return SYSQ_MSG_CONTINUE;
293     if (!MSG_CheckFilter(message, filter)) return SYSQ_MSG_CONTINUE;
294
295     msg->hwnd = hWnd;
296     msg->message = message;
297
298     return (HOOK_CallHooks16( WH_KEYBOARD, remove ? HC_ACTION : HC_NOREMOVE,
299                               msg->wParam, msg->lParam )
300             ? SYSQ_MSG_SKIP : SYSQ_MSG_ACCEPT);
301 }
302
303
304 /***********************************************************************
305  *           MSG_JournalRecordMsg
306  *
307  * Build an EVENTMSG structure and call JOURNALRECORD hook
308  */
309 static void MSG_JournalRecordMsg( MSG16 *msg )
310 {
311     EVENTMSG16 *event = SEGPTR_NEW(EVENTMSG16);
312     if (!event) return;
313     event->message = msg->message;
314     event->time = msg->time;
315     if ((msg->message >= WM_KEYFIRST) && (msg->message <= WM_KEYLAST))
316     {
317         event->paramL = (msg->wParam & 0xFF) | (HIWORD(msg->lParam) << 8);
318         event->paramH = msg->lParam & 0x7FFF;  
319         if (HIWORD(msg->lParam) & 0x0100)
320             event->paramH |= 0x8000;               /* special_key - bit */
321         HOOK_CallHooks16( WH_JOURNALRECORD, HC_ACTION, 0,
322                           (LPARAM)SEGPTR_GET(event) );
323     }
324     else if ((msg->message >= WM_MOUSEFIRST) && (msg->message <= WM_MOUSELAST))
325     {
326         event->paramL = LOWORD(msg->lParam);       /* X pos */
327         event->paramH = HIWORD(msg->lParam);       /* Y pos */ 
328         ClientToScreen16( msg->hwnd, (LPPOINT16)&event->paramL );
329         HOOK_CallHooks16( WH_JOURNALRECORD, HC_ACTION, 0,
330                           (LPARAM)SEGPTR_GET(event) );
331     }
332     else if ((msg->message >= WM_NCMOUSEFIRST) &&
333              (msg->message <= WM_NCMOUSELAST))
334     {
335         event->paramL = LOWORD(msg->lParam);       /* X pos */
336         event->paramH = HIWORD(msg->lParam);       /* Y pos */ 
337         event->message += WM_MOUSEMOVE-WM_NCMOUSEMOVE;/* give no info about NC area */
338         HOOK_CallHooks16( WH_JOURNALRECORD, HC_ACTION, 0,
339                           (LPARAM)SEGPTR_GET(event) );
340     }
341     SEGPTR_FREE(event);
342 }
343
344 /***********************************************************************
345  *          MSG_JournalPlayBackMsg
346  *
347  * Get an EVENTMSG struct via call JOURNALPLAYBACK hook function 
348  */
349 static int MSG_JournalPlayBackMsg(void)
350 {
351  EVENTMSG16 *tmpMsg;
352  long wtime,lParam;
353  WORD keyDown,i,wParam,result=0;
354
355  if ( HOOK_IsHooked( WH_JOURNALPLAYBACK ) )
356  {
357   tmpMsg = SEGPTR_NEW(EVENTMSG16);
358   wtime=HOOK_CallHooks16( WH_JOURNALPLAYBACK, HC_GETNEXT, 0,
359                           (LPARAM)SEGPTR_GET(tmpMsg));
360   /*  TRACE(msg,"Playback wait time =%ld\n",wtime); */
361   if (wtime<=0)
362   {
363    wtime=0;
364    if ((tmpMsg->message>= WM_KEYFIRST) && (tmpMsg->message <= WM_KEYLAST))
365    {
366      wParam=tmpMsg->paramL & 0xFF;
367      lParam=MAKELONG(tmpMsg->paramH&0x7ffff,tmpMsg->paramL>>8);
368      if (tmpMsg->message == WM_KEYDOWN || tmpMsg->message == WM_SYSKEYDOWN)
369      {
370        for (keyDown=i=0; i<256 && !keyDown; i++)
371           if (InputKeyStateTable[i] & 0x80)
372             keyDown++;
373        if (!keyDown)
374          lParam |= 0x40000000;       
375        AsyncKeyStateTable[wParam]=InputKeyStateTable[wParam] |= 0x80;
376      }  
377      else                                       /* WM_KEYUP, WM_SYSKEYUP */
378      {
379        lParam |= 0xC0000000;      
380        AsyncKeyStateTable[wParam]=InputKeyStateTable[wParam] &= ~0x80;
381      }
382      if (InputKeyStateTable[VK_MENU] & 0x80)
383        lParam |= 0x20000000;     
384      if (tmpMsg->paramH & 0x8000)              /*special_key bit*/
385        lParam |= 0x01000000;
386      hardware_event( tmpMsg->message, wParam, lParam,0, 0, tmpMsg->time, 0 );     
387    }
388    else
389    {
390     if ((tmpMsg->message>= WM_MOUSEFIRST) && (tmpMsg->message <= WM_MOUSELAST))
391     {
392      switch (tmpMsg->message)
393      {
394       case WM_LBUTTONDOWN:
395           MouseButtonsStates[0]=AsyncMouseButtonsStates[0]=TRUE;break;
396       case WM_LBUTTONUP:
397           MouseButtonsStates[0]=AsyncMouseButtonsStates[0]=FALSE;break;
398       case WM_MBUTTONDOWN:
399           MouseButtonsStates[1]=AsyncMouseButtonsStates[1]=TRUE;break;
400       case WM_MBUTTONUP:
401           MouseButtonsStates[1]=AsyncMouseButtonsStates[1]=FALSE;break;
402       case WM_RBUTTONDOWN:
403           MouseButtonsStates[2]=AsyncMouseButtonsStates[2]=TRUE;break;
404       case WM_RBUTTONUP:
405           MouseButtonsStates[2]=AsyncMouseButtonsStates[2]=FALSE;break;      
406      }
407      AsyncKeyStateTable[VK_LBUTTON]= InputKeyStateTable[VK_LBUTTON] = MouseButtonsStates[0] ? 0x80 : 0;
408      AsyncKeyStateTable[VK_MBUTTON]= InputKeyStateTable[VK_MBUTTON] = MouseButtonsStates[1] ? 0x80 : 0;
409      AsyncKeyStateTable[VK_RBUTTON]= InputKeyStateTable[VK_RBUTTON] = MouseButtonsStates[2] ? 0x80 : 0;
410      SetCursorPos32(tmpMsg->paramL,tmpMsg->paramH);
411      lParam=MAKELONG(tmpMsg->paramL,tmpMsg->paramH);
412      wParam=0;             
413      if (MouseButtonsStates[0]) wParam |= MK_LBUTTON;
414      if (MouseButtonsStates[1]) wParam |= MK_MBUTTON;
415      if (MouseButtonsStates[2]) wParam |= MK_RBUTTON;
416      hardware_event( tmpMsg->message, wParam, lParam,  
417                      tmpMsg->paramL, tmpMsg->paramH, tmpMsg->time, 0 );
418     }
419    }
420    HOOK_CallHooks16( WH_JOURNALPLAYBACK, HC_SKIP, 0,
421                      (LPARAM)SEGPTR_GET(tmpMsg));
422   }
423   else
424   {
425     if( tmpMsg->message == WM_QUEUESYNC )
426         if (HOOK_IsHooked( WH_CBT ))
427             HOOK_CallHooks16( WH_CBT, HCBT_QS, 0, 0L);
428
429     result= QS_MOUSE | QS_KEY; /* ? */
430   }
431   SEGPTR_FREE(tmpMsg);
432  }
433  return result;
434
435
436 /***********************************************************************
437  *           MSG_PeekHardwareMsg
438  *
439  * Peek for a hardware message matching the hwnd and message filters.
440  */
441 static BOOL32 MSG_PeekHardwareMsg( MSG16 *msg, HWND16 hwnd, DWORD filter,
442                                    BOOL32 remove )
443 {
444     DWORD status = SYSQ_MSG_ACCEPT;
445     MESSAGEQUEUE *sysMsgQueue = QUEUE_GetSysQueue();
446     int i, kbd_msg, pos = sysMsgQueue->nextMessage;
447
448     /* FIXME: there has to be a better way to do this */
449     joySendMessages();
450
451     /* If the queue is empty, attempt to fill it */
452     if (!sysMsgQueue->msgCount && THREAD_IsWin16( THREAD_Current() )
453                                && TSXPending(display))
454         EVENT_WaitNetEvent( FALSE, FALSE );
455
456     for (i = kbd_msg = 0; i < sysMsgQueue->msgCount; i++, pos++)
457     {
458         if (pos >= sysMsgQueue->queueSize) pos = 0;
459         *msg = sysMsgQueue->messages[pos].msg;
460
461           /* Translate message */
462
463         if ((msg->message >= WM_MOUSEFIRST) && (msg->message <= WM_MOUSELAST))
464         {
465             HWND32 hWndScope = (HWND32)sysMsgQueue->messages[pos].extraInfo;
466
467             status = MSG_TranslateMouseMsg(hwnd, filter, msg, remove, 
468                                           (Options.managed && IsWindow32(hWndScope) ) 
469                                            ? WIN_FindWndPtr(hWndScope) : WIN_GetDesktop() );
470             kbd_msg = 0;
471         }
472         else if ((msg->message >= WM_KEYFIRST) && (msg->message <= WM_KEYLAST))
473         {
474             status = MSG_TranslateKbdMsg(hwnd, filter, msg, remove);
475             kbd_msg = 1;
476         }
477         else /* Non-standard hardware event */
478         {
479             HARDWAREHOOKSTRUCT16 *hook;
480             if ((hook = SEGPTR_NEW(HARDWAREHOOKSTRUCT16)))
481             {
482                 BOOL32 ret;
483                 hook->hWnd     = msg->hwnd;
484                 hook->wMessage = msg->message;
485                 hook->wParam   = msg->wParam;
486                 hook->lParam   = msg->lParam;
487                 ret = HOOK_CallHooks16( WH_HARDWARE,
488                                         remove ? HC_ACTION : HC_NOREMOVE,
489                                         0, (LPARAM)SEGPTR_GET(hook) );
490                 SEGPTR_FREE(hook);
491                 if (ret) 
492                 {
493                     QUEUE_RemoveMsg( sysMsgQueue, pos );
494                     continue;
495                 }
496                 status = SYSQ_MSG_ACCEPT; 
497             }
498         }
499
500         switch (LOWORD(status))
501         {
502            case SYSQ_MSG_ACCEPT:
503                 break;
504
505            case SYSQ_MSG_SKIP:
506                 if (HOOK_IsHooked( WH_CBT ))
507                    if( kbd_msg )
508                        HOOK_CallHooks16( WH_CBT, HCBT_KEYSKIPPED, 
509                                                  msg->wParam, msg->lParam );
510                    else
511                    {
512                        MOUSEHOOKSTRUCT16 *hook = SEGPTR_NEW(MOUSEHOOKSTRUCT16);
513                        if (hook)
514                        {
515                            hook->pt           = msg->pt;
516                            hook->hwnd         = msg->hwnd;
517                            hook->wHitTestCode = HIWORD(status);
518                            hook->dwExtraInfo  = 0;
519                            HOOK_CallHooks16( WH_CBT, HCBT_CLICKSKIPPED ,msg->message,
520                                           (LPARAM)SEGPTR_GET(hook) );
521                            SEGPTR_FREE(hook);
522                        }
523                    }
524
525                 if (remove)
526                     QUEUE_RemoveMsg( sysMsgQueue, pos );
527                 /* continue */
528
529            case SYSQ_MSG_CONTINUE:
530                 continue;
531
532            case SYSQ_MSG_ABANDON: 
533                 return FALSE;
534         }
535
536         if (remove)
537         {
538             if (HOOK_IsHooked( WH_JOURNALRECORD )) MSG_JournalRecordMsg( msg );
539             QUEUE_RemoveMsg( sysMsgQueue, pos );
540         }
541         return TRUE;
542     }
543     return FALSE;
544 }
545
546
547 /**********************************************************************
548  *           SetDoubleClickTime16   (USER.20)
549  */
550 void WINAPI SetDoubleClickTime16( UINT16 interval )
551 {
552     SetDoubleClickTime32( interval );
553 }               
554
555
556 /**********************************************************************
557  *           SetDoubleClickTime32   (USER32.480)
558  */
559 BOOL32 WINAPI SetDoubleClickTime32( UINT32 interval )
560 {
561     doubleClickSpeed = interval ? interval : 500;
562     return TRUE;
563 }               
564
565
566 /**********************************************************************
567  *           GetDoubleClickTime16   (USER.21)
568  */
569 UINT16 WINAPI GetDoubleClickTime16(void)
570 {
571     return doubleClickSpeed;
572 }               
573
574
575 /**********************************************************************
576  *           GetDoubleClickTime32   (USER32.239)
577  */
578 UINT32 WINAPI GetDoubleClickTime32(void)
579 {
580     return doubleClickSpeed;
581 }               
582
583
584 /***********************************************************************
585  *           MSG_SendMessage
586  *
587  * Implementation of an inter-task SendMessage.
588  */
589 static LRESULT MSG_SendMessage( HQUEUE16 hDestQueue, HWND16 hwnd, UINT16 msg,
590                                 WPARAM32 wParam, LPARAM lParam, WORD flags )
591 {
592     INT32         prevSMRL = debugSMRL;
593     QSMCTRL       qCtrl = { 0, 1};
594     MESSAGEQUEUE *queue, *destQ;
595
596     if (!(queue = (MESSAGEQUEUE*)GlobalLock16( GetTaskQueue(0) ))) return 0;
597     if (!(destQ = (MESSAGEQUEUE*)GlobalLock16( hDestQueue ))) return 0;
598
599     if (IsTaskLocked() || !IsWindow32(hwnd)) return 0;
600
601     debugSMRL+=4;
602     TRACE(sendmsg,"%*sSM: %s [%04x] (%04x -> %04x)\n", 
603                     prevSMRL, "", SPY_GetMsgName(msg), msg, queue->self, hDestQueue );
604
605     if( !(queue->wakeBits & QS_SMPARAMSFREE) )
606     {
607       TRACE(sendmsg,"\tIntertask SendMessage: sleeping since unreplied SendMessage pending\n");
608       queue->changeBits &= ~QS_SMPARAMSFREE;
609       QUEUE_WaitBits( QS_SMPARAMSFREE );
610     }
611
612     /* resume sending */ 
613
614     queue->hWnd       = hwnd;
615     queue->msg        = msg;
616     queue->wParam     = LOWORD(wParam);
617     queue->wParamHigh = HIWORD(wParam);
618     queue->lParam     = lParam;
619     queue->hPrevSendingTask = destQ->hSendingTask;
620     destQ->hSendingTask = GetTaskQueue(0);
621
622     queue->wakeBits &= ~QS_SMPARAMSFREE;
623     queue->flags = (queue->flags & ~(QUEUE_SM_WIN32|QUEUE_SM_UNICODE)) | flags;
624
625     TRACE(sendmsg,"%*ssm: smResultInit = %08x\n", prevSMRL, "", (unsigned)&qCtrl);
626
627     queue->smResultInit = &qCtrl;
628
629     QUEUE_SetWakeBit( destQ, QS_SENDMESSAGE );
630
631     /* perform task switch and wait for the result */
632
633     while( qCtrl.bPending )
634     {
635       if (!(queue->wakeBits & QS_SMRESULT))
636       {
637         queue->changeBits &= ~QS_SMRESULT;
638         if (THREAD_IsWin16( THREAD_Current() ))
639           DirectedYield( destQ->hTask );
640         else
641           QUEUE_Signal( destQ->hTask );
642         QUEUE_WaitBits( QS_SMRESULT );
643         TRACE(sendmsg,"\tsm: have result!\n");
644       }
645       /* got something */
646
647       TRACE(sendmsg,"%*ssm: smResult = %08x\n", prevSMRL, "", (unsigned)queue->smResult );
648
649       if (queue->smResult) { /* FIXME, smResult should always be set */
650         queue->smResult->lResult = queue->SendMessageReturn;
651         queue->smResult->bPending = FALSE;
652       }
653       queue->wakeBits &= ~QS_SMRESULT;
654
655       if( queue->smResult != &qCtrl )
656           ERR(sendmsg, "%*ssm: weird scenes inside the goldmine!\n", prevSMRL, "");
657     }
658     queue->smResultInit = NULL;
659     
660     TRACE(sendmsg,"%*sSM: [%04x] returning %08lx\n", prevSMRL, "", msg, qCtrl.lResult);
661     debugSMRL-=4;
662
663     return qCtrl.lResult;
664 }
665
666
667 /***********************************************************************
668  *           ReplyMessage16   (USER.115)
669  */
670 void WINAPI ReplyMessage16( LRESULT result )
671 {
672     MESSAGEQUEUE *senderQ;
673     MESSAGEQUEUE *queue;
674
675     if (!(queue = (MESSAGEQUEUE*)GlobalLock16( GetTaskQueue(0) ))) return;
676
677     TRACE(msg,"ReplyMessage, queue %04x\n", queue->self);
678
679     while( (senderQ = (MESSAGEQUEUE*)GlobalLock16( queue->InSendMessageHandle)))
680     {
681       TRACE(msg,"\trpm: replying to %04x (%04x -> %04x)\n",
682                           queue->msg, queue->self, senderQ->self);
683
684       if( queue->wakeBits & QS_SENDMESSAGE )
685       {
686         QUEUE_ReceiveMessage( queue );
687         continue; /* ReceiveMessage() already called us */
688       }
689
690       if(!(senderQ->wakeBits & QS_SMRESULT) ) break;
691       if (THREAD_IsWin16(THREAD_Current())) OldYield();
692     } 
693     if( !senderQ ) { TRACE(msg,"\trpm: done\n"); return; }
694
695     senderQ->SendMessageReturn = result;
696     TRACE(msg,"\trpm: smResult = %08x, result = %08lx\n", 
697                         (unsigned)queue->smResultCurrent, result );
698
699     senderQ->smResult = queue->smResultCurrent;
700     queue->InSendMessageHandle = 0;
701
702     QUEUE_SetWakeBit( senderQ, QS_SMRESULT );
703     if (THREAD_IsWin16(THREAD_Current())) DirectedYield( queue->hSendingTask );
704 }
705
706
707 /***********************************************************************
708  *           MSG_PeekMessage
709  */
710 static BOOL32 MSG_PeekMessage( LPMSG16 msg, HWND16 hwnd, WORD first, WORD last,
711                                WORD flags, BOOL32 peek )
712 {
713     int pos, mask;
714     MESSAGEQUEUE *msgQueue;
715     HQUEUE16 hQueue;
716
717 #ifdef CONFIG_IPC
718     DDE_TestDDE(hwnd);  /* do we have dde handling in the window ?*/
719     DDE_GetRemoteMessage();
720 #endif  /* CONFIG_IPC */
721
722     mask = QS_POSTMESSAGE | QS_SENDMESSAGE;  /* Always selected */
723     if (first || last)
724     {
725         if ((first <= WM_KEYLAST) && (last >= WM_KEYFIRST)) mask |= QS_KEY;
726         if ( ((first <= WM_MOUSELAST) && (last >= WM_MOUSEFIRST)) ||
727              ((first <= WM_NCMOUSELAST) && (last >= WM_NCMOUSEFIRST)) ) mask |= QS_MOUSE;
728         if ((first <= WM_TIMER) && (last >= WM_TIMER)) mask |= QS_TIMER;
729         if ((first <= WM_SYSTIMER) && (last >= WM_SYSTIMER)) mask |= QS_TIMER;
730         if ((first <= WM_PAINT) && (last >= WM_PAINT)) mask |= QS_PAINT;
731     }
732     else mask |= QS_MOUSE | QS_KEY | QS_TIMER | QS_PAINT;
733
734     if (IsTaskLocked()) flags |= PM_NOYIELD;
735
736     /* Never yield on Win32 threads */
737     if (!THREAD_IsWin16(THREAD_Current())) flags |= PM_NOYIELD;
738
739     while(1)
740     {    
741         hQueue   = GetTaskQueue(0);
742         msgQueue = (MESSAGEQUEUE *)GlobalLock16( hQueue );
743         if (!msgQueue) return FALSE;
744         msgQueue->changeBits = 0;
745
746         /* First handle a message put by SendMessage() */
747
748         while (msgQueue->wakeBits & QS_SENDMESSAGE)
749             QUEUE_ReceiveMessage( msgQueue );
750
751         /* Now handle a WM_QUIT message */
752
753         if (msgQueue->wPostQMsg &&
754            (!first || WM_QUIT >= first) && 
755            (!last || WM_QUIT <= last) )
756         {
757             msg->hwnd    = hwnd;
758             msg->message = WM_QUIT;
759             msg->wParam  = msgQueue->wExitCode;
760             msg->lParam  = 0;
761             if (flags & PM_REMOVE) msgQueue->wPostQMsg = 0;
762             break;
763         }
764     
765         /* Now find a normal message */
766
767         if (((msgQueue->wakeBits & mask) & QS_POSTMESSAGE) &&
768             ((pos = QUEUE_FindMsg( msgQueue, hwnd, first, last )) != -1))
769         {
770             QMSG *qmsg = &msgQueue->messages[pos];
771             *msg = qmsg->msg;
772             msgQueue->GetMessageTimeVal      = msg->time;
773             msgQueue->GetMessagePosVal       = *(DWORD *)&msg->pt;
774             msgQueue->GetMessageExtraInfoVal = qmsg->extraInfo;
775
776             if (flags & PM_REMOVE) QUEUE_RemoveMsg( msgQueue, pos );
777             break;
778         }
779
780         msgQueue->changeBits |= MSG_JournalPlayBackMsg();
781
782         /* Now find a hardware event */
783
784         if (((msgQueue->wakeBits & mask) & (QS_MOUSE | QS_KEY)) &&
785             MSG_PeekHardwareMsg( msg, hwnd, MAKELONG(first,last), flags & PM_REMOVE ))
786         {
787             /* Got one */
788             msgQueue->GetMessageTimeVal      = msg->time;
789             msgQueue->GetMessagePosVal       = *(DWORD *)&msg->pt;
790             msgQueue->GetMessageExtraInfoVal = 0;  /* Always 0 for now */
791             break;
792         }
793
794         /* Check again for SendMessage */
795
796         while (msgQueue->wakeBits & QS_SENDMESSAGE)
797             QUEUE_ReceiveMessage( msgQueue );
798
799         /* Now find a WM_PAINT message */
800
801         if ((msgQueue->wakeBits & mask) & QS_PAINT)
802         {
803             WND* wndPtr;
804             msg->hwnd = WIN_FindWinToRepaint( hwnd , hQueue );
805             msg->message = WM_PAINT;
806             msg->wParam = 0;
807             msg->lParam = 0;
808
809             if ((wndPtr = WIN_FindWndPtr(msg->hwnd)))
810             {
811                 if( wndPtr->dwStyle & WS_MINIMIZE &&
812                     wndPtr->class->hIcon )
813                 {
814                     msg->message = WM_PAINTICON;
815                     msg->wParam = 1;
816                 }
817
818                 if( !hwnd || msg->hwnd == hwnd || IsChild16(hwnd,msg->hwnd) )
819                 {
820                     if( wndPtr->flags & WIN_INTERNAL_PAINT && !wndPtr->hrgnUpdate)
821                     {
822                         wndPtr->flags &= ~WIN_INTERNAL_PAINT;
823                         QUEUE_DecPaintCount( hQueue );
824                     }
825                     break;
826                 }
827             }
828         }
829
830         /* Check for timer messages, but yield first */
831
832         if (!(flags & PM_NOYIELD))
833         {
834             UserYield();
835             while (msgQueue->wakeBits & QS_SENDMESSAGE)
836                 QUEUE_ReceiveMessage( msgQueue );
837         }
838         if ((msgQueue->wakeBits & mask) & QS_TIMER)
839         {
840             if (TIMER_GetTimerMsg(msg, hwnd, hQueue, flags & PM_REMOVE)) break;
841         }
842
843         if (peek)
844         {
845             if (!(flags & PM_NOYIELD)) UserYield();
846             return FALSE;
847         }
848         msgQueue->wakeMask = mask;
849         QUEUE_WaitBits( mask );
850     }
851
852       /* We got a message */
853     if (flags & PM_REMOVE)
854     {
855         WORD message = msg->message;
856
857         if (message == WM_KEYDOWN || message == WM_SYSKEYDOWN)
858         {
859             BYTE *p = &QueueKeyStateTable[msg->wParam & 0xff];
860
861             if (!(*p & 0x80))
862                 *p ^= 0x01;
863             *p |= 0x80;
864         }
865         else if (message == WM_KEYUP || message == WM_SYSKEYUP)
866             QueueKeyStateTable[msg->wParam & 0xff] &= ~0x80;
867     }
868     if (peek) return TRUE;
869     else return (msg->message != WM_QUIT);
870 }
871
872
873 /***********************************************************************
874  *           MSG_InternalGetMessage
875  *
876  * GetMessage() function for internal use. Behave like GetMessage(),
877  * but also call message filters and optionally send WM_ENTERIDLE messages.
878  * 'hwnd' must be the handle of the dialog or menu window.
879  * 'code' is the message filter value (MSGF_??? codes).
880  */
881 BOOL32 MSG_InternalGetMessage( MSG16 *msg, HWND32 hwnd, HWND32 hwndOwner,
882                                WPARAM32 code, WORD flags, BOOL32 sendIdle ) 
883 {
884     for (;;)
885     {
886         if (sendIdle)
887         {
888             if (!MSG_PeekMessage( msg, 0, 0, 0, flags, TRUE ))
889             {
890                   /* No message present -> send ENTERIDLE and wait */
891                 if (IsWindow32(hwndOwner))
892                     SendMessage16( hwndOwner, WM_ENTERIDLE,
893                                    code, (LPARAM)hwnd );
894                 MSG_PeekMessage( msg, 0, 0, 0, flags, FALSE );
895             }
896         }
897         else  /* Always wait for a message */
898             MSG_PeekMessage( msg, 0, 0, 0, flags, FALSE );
899
900         /* Call message filters */
901
902         if (HOOK_IsHooked( WH_SYSMSGFILTER ) || HOOK_IsHooked( WH_MSGFILTER ))
903         {
904             MSG16 *pmsg = SEGPTR_NEW(MSG16);
905             if (pmsg)
906             {
907                 BOOL32 ret;
908                 *pmsg = *msg;
909                 ret = ((BOOL16)HOOK_CallHooks16( WH_SYSMSGFILTER, code, 0,
910                                                  (LPARAM)SEGPTR_GET(pmsg) ) ||
911                        (BOOL16)HOOK_CallHooks16( WH_MSGFILTER, code, 0,
912                                                  (LPARAM)SEGPTR_GET(pmsg) ));
913                 SEGPTR_FREE(pmsg);
914                 if (ret)
915                 {
916                     /* Message filtered -> remove it from the queue */
917                     /* if it's still there. */
918                     if (!(flags & PM_REMOVE))
919                         MSG_PeekMessage( msg, 0, 0, 0, PM_REMOVE, TRUE );
920                     continue;
921                 }
922             }
923         }
924
925         return (msg->message != WM_QUIT);
926     }
927 }
928
929
930 /***********************************************************************
931  *           PeekMessage16   (USER.109)
932  */
933 BOOL16 WINAPI PeekMessage16( LPMSG16 msg, HWND16 hwnd, UINT16 first,
934                              UINT16 last, UINT16 flags )
935 {
936     return MSG_PeekMessage( msg, hwnd, first, last, flags, TRUE );
937 }
938
939 /***********************************************************************
940  *         PeekMessageA
941  */
942 BOOL32 WINAPI PeekMessage32A( LPMSG32 lpmsg, HWND32 hwnd,
943                               UINT32 min,UINT32 max,UINT32 wRemoveMsg)
944 {
945         MSG16 msg;
946         BOOL32 ret;
947         ret=PeekMessage16(&msg,hwnd,min,max,wRemoveMsg);
948         /* FIXME: should translate the message to Win32 */
949         STRUCT32_MSG16to32(&msg,lpmsg);
950         return ret;
951 }
952
953 /***********************************************************************
954  *         PeekMessageW             Check queue for messages
955  *
956  * Checks for a message in the thread's queue, filtered as for
957  * GetMessage(). Returns immediately whether a message is available
958  * or not.
959  *
960  * Whether a retrieved message is removed from the queue is set by the
961  * _wRemoveMsg_ flags, which should be one of the following values:
962  *
963  *    PM_NOREMOVE    Do not remove the message from the queue. 
964  *
965  *    PM_REMOVE      Remove the message from the queue.
966  *
967  * In addition, PM_NOYIELD may be combined into _wRemoveMsg_ to
968  * request that the system not yield control during PeekMessage();
969  * however applications may not rely on scheduling behavior.
970  * 
971  * RETURNS
972  *
973  *  Nonzero if a message is available and is retrieved, zero otherwise.
974  *
975  * CONFORMANCE
976  *
977  * ECMA-234, Win32
978  *
979  */
980 BOOL32 WINAPI PeekMessage32W( 
981   LPMSG32 lpmsg,    /* buffer to receive message */
982   HWND32 hwnd,      /* restrict to messages for hwnd */
983   UINT32 min,       /* minimum message to receive */
984   UINT32 max,       /* maximum message to receive */
985   UINT32 wRemoveMsg /* removal flags */ 
986 ) {
987         /* FIXME: Should perform Unicode translation on specific messages */
988         return PeekMessage32A(lpmsg,hwnd,min,max,wRemoveMsg);
989 }
990
991 /***********************************************************************
992  *           GetMessage16   (USER.108)
993  */
994 BOOL16 WINAPI GetMessage16( SEGPTR msg, HWND16 hwnd, UINT16 first, UINT16 last)
995 {
996     MSG16 *lpmsg = (MSG16 *)PTR_SEG_TO_LIN(msg);
997     MSG_PeekMessage( lpmsg,
998                      hwnd, first, last, PM_REMOVE, FALSE );
999
1000     TRACE(msg,"message %04x, hwnd %04x, filter(%04x - %04x)\n", lpmsg->message,
1001                                                                  hwnd, first, last );
1002     HOOK_CallHooks16( WH_GETMESSAGE, HC_ACTION, 0, (LPARAM)msg );
1003     return (lpmsg->message != WM_QUIT);
1004 }
1005
1006 /***********************************************************************
1007  *          GetMessage32A   (USER32.270)
1008  */
1009 BOOL32 WINAPI GetMessage32A(MSG32* lpmsg,HWND32 hwnd,UINT32 min,UINT32 max)
1010 {
1011     BOOL32 ret;
1012     MSG16 *msg = SEGPTR_NEW(MSG16);
1013     if (!msg) return 0;
1014     ret=GetMessage16(SEGPTR_GET(msg),(HWND16)hwnd,min,max);
1015     /* FIXME */
1016     STRUCT32_MSG16to32(msg,lpmsg);
1017     SEGPTR_FREE(msg);
1018     return ret;
1019 }
1020
1021 /***********************************************************************
1022  *          GetMessage32W   (USER32.274) Retrieve next message
1023  *
1024  * GetMessage retrieves the next event from the calling thread's
1025  * queue and deposits it in *lpmsg.
1026  *
1027  * If _hwnd_ is not NULL, only messages for window _hwnd_ and its
1028  * children as specified by IsChild() are retrieved. If _hwnd_ is NULL
1029  * all application messages are retrieved.
1030  *
1031  * _min_ and _max_ specify the range of messages of interest. If
1032  * min==max==0, no filtering is performed. Useful examples are
1033  * WM_KEYFIRST and WM_KEYLAST to retrieve keyboard input, and
1034  * WM_MOUSEFIRST and WM_MOUSELAST to retrieve mouse input.
1035  *
1036  * WM_PAINT messages are not removed from the queue; they remain until
1037  * processed. Other messages are removed from the queue.
1038  *
1039  * RETURNS
1040  *
1041  * -1 on error, 0 if message is WM_QUIT, nonzero otherwise.
1042  *
1043  * CONFORMANCE
1044  *
1045  * ECMA-234, Win32
1046  * 
1047  */
1048 BOOL32 WINAPI GetMessage32W(
1049   MSG32* lpmsg, /* buffer to receive message */
1050   HWND32 hwnd,  /* restrict to messages for hwnd */
1051   UINT32 min,   /* minimum message to receive */
1052   UINT32 max    /* maximum message to receive */
1053 ) {
1054     BOOL32 ret;
1055     MSG16 *msg = SEGPTR_NEW(MSG16);
1056     if (!msg) return 0;
1057     ret=GetMessage16(SEGPTR_GET(msg),(HWND16)hwnd,min,max);
1058     /* FIXME */
1059     STRUCT32_MSG16to32(msg,lpmsg);
1060     SEGPTR_FREE(msg);
1061     return ret;
1062 }
1063
1064
1065 /***********************************************************************
1066  *           PostMessage16   (USER.110)
1067  */
1068 BOOL16 WINAPI PostMessage16( HWND16 hwnd, UINT16 message, WPARAM16 wParam,
1069                              LPARAM lParam )
1070 {
1071     MSG16       msg;
1072     WND         *wndPtr;
1073
1074     msg.hwnd    = hwnd;
1075     msg.message = message;
1076     msg.wParam  = wParam;
1077     msg.lParam  = lParam;
1078     msg.time    = GetTickCount();
1079     msg.pt.x    = 0;
1080     msg.pt.y    = 0;
1081
1082 #ifdef CONFIG_IPC
1083     if (DDE_PostMessage(&msg))
1084        return TRUE;
1085 #endif  /* CONFIG_IPC */
1086     
1087     if (hwnd == HWND_BROADCAST)
1088     {
1089         TRACE(msg,"HWND_BROADCAST !\n");
1090         for (wndPtr = WIN_GetDesktop()->child; wndPtr; wndPtr = wndPtr->next)
1091         {
1092             if (wndPtr->dwStyle & WS_POPUP || wndPtr->dwStyle & WS_CAPTION)
1093             {
1094                 TRACE(msg,"BROADCAST Message to hWnd=%04x m=%04X w=%04X l=%08lX !\n",
1095                             wndPtr->hwndSelf, message, wParam, lParam);
1096                 PostMessage16( wndPtr->hwndSelf, message, wParam, lParam );
1097             }
1098         }
1099         TRACE(msg,"End of HWND_BROADCAST !\n");
1100         return TRUE;
1101     }
1102
1103     wndPtr = WIN_FindWndPtr( hwnd );
1104     if (!wndPtr || !wndPtr->hmemTaskQ) return FALSE;
1105
1106     return QUEUE_AddMsg( wndPtr->hmemTaskQ, &msg, 0 );
1107 }
1108
1109
1110 /***********************************************************************
1111  *           PostMessage32A   (USER32.419)
1112  */
1113 BOOL32 WINAPI PostMessage32A( HWND32 hwnd, UINT32 message, WPARAM32 wParam,
1114                               LPARAM lParam )
1115 {
1116     /* FIXME */
1117     return PostMessage16( hwnd, message, wParam, lParam );
1118 }
1119
1120
1121 /***********************************************************************
1122  *           PostMessage32W   (USER32.420)
1123  */
1124 BOOL32 WINAPI PostMessage32W( HWND32 hwnd, UINT32 message, WPARAM32 wParam,
1125                               LPARAM lParam )
1126 {
1127     /* FIXME */
1128     return PostMessage16( hwnd, message, wParam, lParam );
1129 }
1130
1131
1132 /***********************************************************************
1133  *           PostAppMessage16   (USER.116)
1134  */
1135 BOOL16 WINAPI PostAppMessage16( HTASK16 hTask, UINT16 message, WPARAM16 wParam,
1136                                 LPARAM lParam )
1137 {
1138     MSG16 msg;
1139
1140     if (GetTaskQueue(hTask) == 0) return FALSE;
1141     msg.hwnd    = 0;
1142     msg.message = message;
1143     msg.wParam  = wParam;
1144     msg.lParam  = lParam;
1145     msg.time    = GetTickCount();
1146     msg.pt.x    = 0;
1147     msg.pt.y    = 0;
1148
1149     return QUEUE_AddMsg( GetTaskQueue(hTask), &msg, 0 );
1150 }
1151
1152
1153 /***********************************************************************
1154  *           SendMessage16   (USER.111)
1155  */
1156 LRESULT WINAPI SendMessage16( HWND16 hwnd, UINT16 msg, WPARAM16 wParam,
1157                               LPARAM lParam)
1158 {
1159     WND * wndPtr;
1160     WND **list, **ppWnd;
1161     LRESULT ret;
1162
1163 #ifdef CONFIG_IPC
1164     MSG16 DDE_msg = { hwnd, msg, wParam, lParam };
1165     if (DDE_SendMessage(&DDE_msg)) return TRUE;
1166 #endif  /* CONFIG_IPC */
1167
1168     if (hwnd == HWND_BROADCAST)
1169     {
1170         if (!(list = WIN_BuildWinArray( WIN_GetDesktop(), 0, NULL )))
1171             return TRUE;
1172         TRACE(msg,"HWND_BROADCAST !\n");
1173         for (ppWnd = list; *ppWnd; ppWnd++)
1174         {
1175             wndPtr = *ppWnd;
1176             if (!IsWindow32(wndPtr->hwndSelf)) continue;
1177             if (wndPtr->dwStyle & WS_POPUP || wndPtr->dwStyle & WS_CAPTION)
1178             {
1179                 TRACE(msg,"BROADCAST Message to hWnd=%04x m=%04X w=%04lX l=%08lX !\n",
1180                             wndPtr->hwndSelf, msg, (DWORD)wParam, lParam);
1181                 SendMessage16( wndPtr->hwndSelf, msg, wParam, lParam );
1182             }
1183         }
1184         HeapFree( SystemHeap, 0, list );
1185         TRACE(msg,"End of HWND_BROADCAST !\n");
1186         return TRUE;
1187     }
1188
1189     if (HOOK_IsHooked( WH_CALLWNDPROC ))
1190     {
1191         LPCWPSTRUCT16 pmsg;
1192
1193         if ((pmsg = SEGPTR_NEW(CWPSTRUCT16)))
1194         {
1195             pmsg->hwnd   = hwnd;
1196             pmsg->message= msg;
1197             pmsg->wParam = wParam;
1198             pmsg->lParam = lParam;
1199             HOOK_CallHooks16( WH_CALLWNDPROC, HC_ACTION, 1,
1200                               (LPARAM)SEGPTR_GET(pmsg) );
1201             hwnd   = pmsg->hwnd;
1202             msg    = pmsg->message;
1203             wParam = pmsg->wParam;
1204             lParam = pmsg->lParam;
1205             SEGPTR_FREE( pmsg );
1206         }
1207     }
1208
1209     if (!(wndPtr = WIN_FindWndPtr( hwnd )))
1210     {
1211         WARN(msg, "invalid hwnd %04x\n", hwnd );
1212         return 0;
1213     }
1214     if (QUEUE_IsExitingQueue(wndPtr->hmemTaskQ))
1215         return 0;  /* Don't send anything if the task is dying */
1216
1217     SPY_EnterMessage( SPY_SENDMESSAGE16, hwnd, msg, wParam, lParam );
1218
1219     if (wndPtr->hmemTaskQ != GetTaskQueue(0))
1220         ret = MSG_SendMessage( wndPtr->hmemTaskQ, hwnd, msg,
1221                                wParam, lParam, 0 );
1222     else
1223         ret = CallWindowProc16( (WNDPROC16)wndPtr->winproc,
1224                                 hwnd, msg, wParam, lParam );
1225
1226     SPY_ExitMessage( SPY_RESULT_OK16, hwnd, msg, ret );
1227     return ret;
1228 }
1229
1230 /************************************************************************
1231  *           MSG_CallWndProcHook32
1232  */
1233 static void  MSG_CallWndProcHook32( LPMSG32 pmsg, BOOL32 bUnicode )
1234 {
1235    CWPSTRUCT32 cwp;
1236
1237    cwp.lParam = pmsg->lParam;
1238    cwp.wParam = pmsg->wParam;
1239    cwp.message = pmsg->message;
1240    cwp.hwnd = pmsg->hwnd;
1241
1242    if (bUnicode) HOOK_CallHooks32W(WH_CALLWNDPROC, HC_ACTION, 1, (LPARAM)&cwp);
1243    else HOOK_CallHooks32A( WH_CALLWNDPROC, HC_ACTION, 1, (LPARAM)&cwp );
1244
1245    pmsg->lParam = cwp.lParam;
1246    pmsg->wParam = cwp.wParam;
1247    pmsg->message = cwp.message;
1248    pmsg->hwnd = cwp.hwnd;
1249 }
1250
1251 /**********************************************************************
1252  *           PostThreadMessage32A    (USER32.422)
1253  */
1254 BOOL32 WINAPI PostThreadMessage32A(DWORD idThread , UINT32 message,
1255                                    WPARAM32 wParam, LPARAM lParam )
1256 {
1257    THDB *thdb = THREAD_ID_TO_THDB(idThread);
1258    if (!thdb || !thdb->process) return FALSE;
1259
1260    FIXME(sendmsg, "(...): Should use thread-local message queue!\n");
1261    return PostAppMessage16(thdb->process->task, message, wParam, lParam);
1262 }
1263
1264 /***********************************************************************
1265  *           SendMessage32A   (USER32.454)
1266  */
1267 LRESULT WINAPI SendMessage32A( HWND32 hwnd, UINT32 msg, WPARAM32 wParam,
1268                                LPARAM lParam )
1269 {
1270     WND * wndPtr;
1271     WND **list, **ppWnd;
1272     LRESULT ret;
1273
1274     if (hwnd == HWND_BROADCAST || hwnd == HWND_TOPMOST)
1275     {
1276         if (!(list = WIN_BuildWinArray( WIN_GetDesktop(), 0, NULL )))
1277             return TRUE;
1278         for (ppWnd = list; *ppWnd; ppWnd++)
1279         {
1280             wndPtr = *ppWnd;
1281             if (!IsWindow32(wndPtr->hwndSelf)) continue;
1282             if (wndPtr->dwStyle & WS_POPUP || wndPtr->dwStyle & WS_CAPTION)
1283                 SendMessage32A( wndPtr->hwndSelf, msg, wParam, lParam );
1284         }
1285         HeapFree( SystemHeap, 0, list );
1286         return TRUE;
1287     }
1288
1289     if (HOOK_IsHooked( WH_CALLWNDPROC ))
1290         MSG_CallWndProcHook32( (LPMSG32)&hwnd, FALSE);
1291
1292     if (!(wndPtr = WIN_FindWndPtr( hwnd )))
1293     {
1294         WARN(msg, "invalid hwnd %08x\n", hwnd );
1295         return 0;
1296     }
1297
1298     if (QUEUE_IsExitingQueue(wndPtr->hmemTaskQ))
1299         return 0;  /* Don't send anything if the task is dying */
1300
1301     SPY_EnterMessage( SPY_SENDMESSAGE32, hwnd, msg, wParam, lParam );
1302
1303     if (wndPtr->hmemTaskQ != GetTaskQueue(0))
1304         ret = MSG_SendMessage( wndPtr->hmemTaskQ, hwnd, msg, wParam, lParam,
1305                                QUEUE_SM_WIN32 );
1306     else
1307         ret = CallWindowProc32A( (WNDPROC32)wndPtr->winproc,
1308                                  hwnd, msg, wParam, lParam );
1309
1310     SPY_ExitMessage( SPY_RESULT_OK32, hwnd, msg, ret );
1311     return ret;
1312 }
1313
1314
1315 /***********************************************************************
1316  *           SendMessage32W   (USER32.459)
1317  */
1318 LRESULT WINAPI SendMessage32W( HWND32 hwnd, UINT32 msg, WPARAM32 wParam,
1319                                LPARAM lParam )
1320 {
1321     WND * wndPtr;
1322     WND **list, **ppWnd;
1323     LRESULT ret;
1324
1325     if (hwnd == HWND_BROADCAST || hwnd == HWND_TOPMOST)
1326     {
1327         if (!(list = WIN_BuildWinArray( WIN_GetDesktop(), 0, NULL )))
1328             return TRUE;
1329         for (ppWnd = list; *ppWnd; ppWnd++)
1330         {
1331             wndPtr = *ppWnd;
1332             if (!IsWindow32(wndPtr->hwndSelf)) continue;
1333             if (wndPtr->dwStyle & WS_POPUP || wndPtr->dwStyle & WS_CAPTION)
1334                 SendMessage32W( wndPtr->hwndSelf, msg, wParam, lParam );
1335         }
1336         HeapFree( SystemHeap, 0, list );
1337         return TRUE;
1338     }
1339
1340     if (HOOK_IsHooked( WH_CALLWNDPROC ))
1341         MSG_CallWndProcHook32( (LPMSG32)&hwnd, TRUE);
1342
1343     if (!(wndPtr = WIN_FindWndPtr( hwnd )))
1344     {
1345         WARN(msg, "invalid hwnd %08x\n", hwnd );
1346         return 0;
1347     }
1348     if (QUEUE_IsExitingQueue(wndPtr->hmemTaskQ))
1349         return 0;  /* Don't send anything if the task is dying */
1350
1351     SPY_EnterMessage( SPY_SENDMESSAGE32, hwnd, msg, wParam, lParam );
1352
1353     if (wndPtr->hmemTaskQ != GetTaskQueue(0))
1354         ret = MSG_SendMessage( wndPtr->hmemTaskQ, hwnd, msg, wParam, lParam,
1355                                 QUEUE_SM_WIN32 | QUEUE_SM_UNICODE );
1356     else
1357         ret = CallWindowProc32W( (WNDPROC32)wndPtr->winproc,
1358                                  hwnd, msg, wParam, lParam );
1359
1360     SPY_ExitMessage( SPY_RESULT_OK32, hwnd, msg, ret );
1361     return ret;
1362 }
1363
1364
1365 /***********************************************************************
1366  *           SendMessageTimeout16    (not a WINAPI)
1367  */
1368 LRESULT WINAPI SendMessageTimeout16( HWND16 hwnd, UINT16 msg, WPARAM16 wParam,
1369                                      LPARAM lParam, UINT16 flags,
1370                                      UINT16 timeout, LPWORD resultp)
1371 {
1372   FIXME(sendmsg, "(...): semistub\n");
1373   return SendMessage16 (hwnd, msg, wParam, lParam);
1374 }
1375
1376
1377 /***********************************************************************
1378  *           SendMessageTimeout32A   (USER32.457)
1379  */
1380 LRESULT WINAPI SendMessageTimeout32A( HWND32 hwnd, UINT32 msg, WPARAM32 wParam,
1381                                       LPARAM lParam, UINT32 flags,
1382                                       UINT32 timeout, LPDWORD resultp)
1383 {
1384   FIXME(sendmsg, "(...): semistub\n");
1385   return SendMessage32A (hwnd, msg, wParam, lParam);
1386 }
1387
1388
1389 /***********************************************************************
1390  *           SendMessageTimeout32W   (USER32.458)
1391  */
1392 LRESULT WINAPI SendMessageTimeout32W( HWND32 hwnd, UINT32 msg, WPARAM32 wParam,
1393                                       LPARAM lParam, UINT32 flags,
1394                                       UINT32 timeout, LPDWORD resultp)
1395 {
1396   FIXME(sendmsg, "(...): semistub\n");
1397   return SendMessage32W (hwnd, msg, wParam, lParam);
1398 }
1399
1400
1401 /***********************************************************************
1402  *  WaitMessage    (USER.112) (USER32.578)  Suspend thread pending messages
1403  *
1404  * WaitMessage() suspends a thread until events appear in the thread's
1405  * queue.
1406  *
1407  * BUGS
1408  *
1409  * Is supposed to return BOOL under Win32.
1410  *
1411  * CONFORMANCE
1412  *
1413  * ECMA-234, Win32
1414  * 
1415  */
1416 void WINAPI WaitMessage( void )
1417 {
1418     QUEUE_WaitBits( QS_ALLINPUT );
1419 }
1420
1421 /***********************************************************************
1422  *           MsgWaitForMultipleObjects    (USER32.400)
1423  */
1424 DWORD WINAPI MsgWaitForMultipleObjects( DWORD nCount, HANDLE32 *pHandles,
1425                                         BOOL32 fWaitAll, DWORD dwMilliseconds,
1426                                         DWORD dwWakeMask )
1427 {
1428     DWORD retv;
1429
1430     TDB *currTask = (TDB *)GlobalLock16( GetCurrentTask() );
1431     HQUEUE16 hQueue = currTask? currTask->hQueue : 0;
1432     MESSAGEQUEUE *msgQueue = (MESSAGEQUEUE *)GlobalLock16( hQueue );
1433     if (!msgQueue) return 0xFFFFFFFF;
1434
1435     msgQueue->changeBits = 0;
1436     msgQueue->wakeMask = dwWakeMask;
1437
1438     retv = SYNC_DoWait( nCount, pHandles, fWaitAll, dwMilliseconds, FALSE, TRUE );
1439
1440     return retv;
1441 }
1442
1443
1444
1445 struct accent_char
1446 {
1447     BYTE ac_accent;
1448     BYTE ac_char;
1449     BYTE ac_result;
1450 };
1451
1452 static const struct accent_char accent_chars[] =
1453 {
1454 /* A good idea should be to read /usr/X11/lib/X11/locale/iso8859-x/Compose */
1455     {'`', 'A', '\300'},  {'`', 'a', '\340'},
1456     {'\'', 'A', '\301'}, {'\'', 'a', '\341'},
1457     {'^', 'A', '\302'},  {'^', 'a', '\342'},
1458     {'~', 'A', '\303'},  {'~', 'a', '\343'},
1459     {'"', 'A', '\304'},  {'"', 'a', '\344'},
1460     {'O', 'A', '\305'},  {'o', 'a', '\345'},
1461     {'0', 'A', '\305'},  {'0', 'a', '\345'},
1462     {'A', 'A', '\305'},  {'a', 'a', '\345'},
1463     {'A', 'E', '\306'},  {'a', 'e', '\346'},
1464     {',', 'C', '\307'},  {',', 'c', '\347'},
1465     {'`', 'E', '\310'},  {'`', 'e', '\350'},
1466     {'\'', 'E', '\311'}, {'\'', 'e', '\351'},
1467     {'^', 'E', '\312'},  {'^', 'e', '\352'},
1468     {'"', 'E', '\313'},  {'"', 'e', '\353'},
1469     {'`', 'I', '\314'},  {'`', 'i', '\354'},
1470     {'\'', 'I', '\315'}, {'\'', 'i', '\355'},
1471     {'^', 'I', '\316'},  {'^', 'i', '\356'},
1472     {'"', 'I', '\317'},  {'"', 'i', '\357'},
1473     {'-', 'D', '\320'},  {'-', 'd', '\360'},
1474     {'~', 'N', '\321'},  {'~', 'n', '\361'},
1475     {'`', 'O', '\322'},  {'`', 'o', '\362'},
1476     {'\'', 'O', '\323'}, {'\'', 'o', '\363'},
1477     {'^', 'O', '\324'},  {'^', 'o', '\364'},
1478     {'~', 'O', '\325'},  {'~', 'o', '\365'},
1479     {'"', 'O', '\326'},  {'"', 'o', '\366'},
1480     {'/', 'O', '\330'},  {'/', 'o', '\370'},
1481     {'`', 'U', '\331'},  {'`', 'u', '\371'},
1482     {'\'', 'U', '\332'}, {'\'', 'u', '\372'},
1483     {'^', 'U', '\333'},  {'^', 'u', '\373'},
1484     {'"', 'U', '\334'},  {'"', 'u', '\374'},
1485     {'\'', 'Y', '\335'}, {'\'', 'y', '\375'},
1486     {'T', 'H', '\336'},  {'t', 'h', '\376'},
1487     {'s', 's', '\337'},  {'"', 'y', '\377'},
1488     {'s', 'z', '\337'},  {'i', 'j', '\377'},
1489         /* iso-8859-2 uses this */
1490     {'<', 'L', '\245'},  {'<', 'l', '\265'},    /* caron */
1491     {'<', 'S', '\251'},  {'<', 's', '\271'},
1492     {'<', 'T', '\253'},  {'<', 't', '\273'},
1493     {'<', 'Z', '\256'},  {'<', 'z', '\276'},
1494     {'<', 'C', '\310'},  {'<', 'c', '\350'},
1495     {'<', 'E', '\314'},  {'<', 'e', '\354'},
1496     {'<', 'D', '\317'},  {'<', 'd', '\357'},
1497     {'<', 'N', '\322'},  {'<', 'n', '\362'},
1498     {'<', 'R', '\330'},  {'<', 'r', '\370'},
1499     {';', 'A', '\241'},  {';', 'a', '\261'},    /* ogonek */
1500     {';', 'E', '\312'},  {';', 'e', '\332'},
1501     {'\'', 'Z', '\254'}, {'\'', 'z', '\274'},   /* acute */
1502     {'\'', 'R', '\300'}, {'\'', 'r', '\340'},
1503     {'\'', 'L', '\305'}, {'\'', 'l', '\345'},
1504     {'\'', 'C', '\306'}, {'\'', 'c', '\346'},
1505     {'\'', 'N', '\321'}, {'\'', 'n', '\361'},
1506 /*  collision whith S, from iso-8859-9 !!! */
1507     {',', 'S', '\252'},  {',', 's', '\272'},    /* cedilla */
1508     {',', 'T', '\336'},  {',', 't', '\376'},
1509     {'.', 'Z', '\257'},  {'.', 'z', '\277'},    /* dot above */
1510     {'/', 'L', '\243'},  {'/', 'l', '\263'},    /* slash */
1511     {'/', 'D', '\320'},  {'/', 'd', '\360'},
1512     {'(', 'A', '\303'},  {'(', 'a', '\343'},    /* breve */
1513     {'\275', 'O', '\325'}, {'\275', 'o', '\365'},       /* double acute */
1514     {'\275', 'U', '\334'}, {'\275', 'u', '\374'},
1515     {'0', 'U', '\332'},  {'0', 'u', '\372'},    /* ring above */
1516         /* iso-8859-3 uses this */
1517     {'/', 'H', '\241'},  {'/', 'h', '\261'},    /* slash */
1518     {'>', 'H', '\246'},  {'>', 'h', '\266'},    /* circumflex */
1519     {'>', 'J', '\254'},  {'>', 'j', '\274'},
1520     {'>', 'C', '\306'},  {'>', 'c', '\346'},
1521     {'>', 'G', '\330'},  {'>', 'g', '\370'},
1522     {'>', 'S', '\336'},  {'>', 's', '\376'},
1523 /*  collision whith G( from iso-8859-9 !!!   */
1524     {'(', 'G', '\253'},  {'(', 'g', '\273'},    /* breve */
1525     {'(', 'U', '\335'},  {'(', 'u', '\375'},
1526 /*  collision whith I. from iso-8859-3 !!!   */
1527     {'.', 'I', '\251'},  {'.', 'i', '\271'},    /* dot above */
1528     {'.', 'C', '\305'},  {'.', 'c', '\345'},
1529     {'.', 'G', '\325'},  {'.', 'g', '\365'},
1530         /* iso-8859-4 uses this */
1531     {',', 'R', '\243'},  {',', 'r', '\263'},    /* cedilla */
1532     {',', 'L', '\246'},  {',', 'l', '\266'},
1533     {',', 'G', '\253'},  {',', 'g', '\273'},
1534     {',', 'N', '\321'},  {',', 'n', '\361'},
1535     {',', 'K', '\323'},  {',', 'k', '\363'},
1536     {'~', 'I', '\245'},  {'~', 'i', '\265'},    /* tilde */
1537     {'-', 'E', '\252'},  {'-', 'e', '\272'},    /* macron */
1538     {'-', 'A', '\300'},  {'-', 'a', '\340'},
1539     {'-', 'I', '\317'},  {'-', 'i', '\357'},
1540     {'-', 'O', '\322'},  {'-', 'o', '\362'},
1541     {'-', 'U', '\336'},  {'-', 'u', '\376'},
1542     {'/', 'T', '\254'},  {'/', 't', '\274'},    /* slash */
1543     {'.', 'E', '\314'},  {'.', 'e', '\344'},    /* dot above */
1544     {';', 'I', '\307'},  {';', 'i', '\347'},    /* ogonek */
1545     {';', 'U', '\331'},  {';', 'u', '\371'},
1546         /* iso-8859-9 uses this */
1547         /* iso-8859-9 has really bad choosen G( S, and I. as they collide
1548          * whith the same letters on other iso-8859-x (that is they are on
1549          * different places :-( ), if you use turkish uncomment these and
1550          * comment out the lines in iso-8859-2 and iso-8859-3 sections
1551          * FIXME: should be dynamic according to chosen language
1552          *        if/when Wine has turkish support.  
1553          */ 
1554 /*  collision whith G( from iso-8859-3 !!!   */
1555 /*  {'(', 'G', '\320'},  {'(', 'g', '\360'}, */ /* breve */
1556 /*  collision whith S, from iso-8859-2 !!! */
1557 /*  {',', 'S', '\336'},  {',', 's', '\376'}, */ /* cedilla */
1558 /*  collision whith I. from iso-8859-3 !!!   */
1559 /*  {'.', 'I', '\335'},  {'.', 'i', '\375'}, */ /* dot above */
1560 };
1561
1562
1563 /***********************************************************************
1564  *           MSG_DoTranslateMessage
1565  *
1566  * Implementation of TranslateMessage.
1567  *
1568  * TranslateMessage translates virtual-key messages into character-messages,
1569  * as follows :
1570  * WM_KEYDOWN/WM_KEYUP combinations produce a WM_CHAR or WM_DEADCHAR message.
1571  * ditto replacing WM_* with WM_SYS*
1572  * This produces WM_CHAR messages only for keys mapped to ASCII characters
1573  * by the keyboard driver.
1574  */
1575 static BOOL32 MSG_DoTranslateMessage( UINT32 message, HWND32 hwnd,
1576                                       WPARAM32 wParam, LPARAM lParam )
1577 {
1578     static int dead_char;
1579     BYTE wp[2];
1580     
1581     if (message != WM_MOUSEMOVE && message != WM_TIMER)
1582         TRACE(msg, "(%s, %04X, %08lX)\n",
1583                      SPY_GetMsgName(message), wParam, lParam );
1584     if(message >= WM_KEYFIRST && message <= WM_KEYLAST)
1585         TRACE(key, "(%s, %04X, %08lX)\n",
1586                      SPY_GetMsgName(message), wParam, lParam );
1587
1588     if ((message != WM_KEYDOWN) && (message != WM_SYSKEYDOWN))  return FALSE;
1589
1590     TRACE(key, "Translating key %04X, scancode %04X\n",
1591                  wParam, HIWORD(lParam) );
1592
1593     /* FIXME : should handle ToAscii yielding 2 */
1594     switch (ToAscii32(wParam, HIWORD(lParam),
1595                       QueueKeyStateTable,(LPWORD)wp, 0)) 
1596     {
1597     case 1 :
1598         message = (message == WM_KEYDOWN) ? WM_CHAR : WM_SYSCHAR;
1599         /* Should dead chars handling go in ToAscii ? */
1600         if (dead_char)
1601         {
1602             int i;
1603
1604             if (wp[0] == ' ') wp[0] =  dead_char;
1605             if (dead_char == 0xa2) dead_char = '(';
1606             else if (dead_char == 0xa8) dead_char = '"';
1607             else if (dead_char == 0xb2) dead_char = ';';
1608             else if (dead_char == 0xb4) dead_char = '\'';
1609             else if (dead_char == 0xb7) dead_char = '<';
1610             else if (dead_char == 0xb8) dead_char = ',';
1611             else if (dead_char == 0xff) dead_char = '.';
1612             for (i = 0; i < sizeof(accent_chars)/sizeof(accent_chars[0]); i++)
1613                 if ((accent_chars[i].ac_accent == dead_char) &&
1614                     (accent_chars[i].ac_char == wp[0]))
1615                 {
1616                     wp[0] = accent_chars[i].ac_result;
1617                     break;
1618                 }
1619             dead_char = 0;
1620         }
1621         TRACE(key, "1 -> PostMessage(%s)\n", SPY_GetMsgName(message));
1622         PostMessage16( hwnd, message, wp[0], lParam );
1623         return TRUE;
1624
1625     case -1 :
1626         message = (message == WM_KEYDOWN) ? WM_DEADCHAR : WM_SYSDEADCHAR;
1627         dead_char = wp[0];
1628         TRACE(key, "-1 -> PostMessage(%s)\n",
1629                      SPY_GetMsgName(message));
1630         PostMessage16( hwnd, message, wp[0], lParam );
1631         return TRUE;
1632     }
1633     return FALSE;
1634 }
1635
1636
1637 /***********************************************************************
1638  *           TranslateMessage16   (USER.113)
1639  */
1640 BOOL16 WINAPI TranslateMessage16( const MSG16 *msg )
1641 {
1642     return MSG_DoTranslateMessage( msg->message, msg->hwnd,
1643                                    msg->wParam, msg->lParam );
1644 }
1645
1646
1647 /***********************************************************************
1648  *           TranslateMessage32   (USER32.556)
1649  */
1650 BOOL32 WINAPI TranslateMessage32( const MSG32 *msg )
1651 {
1652     return MSG_DoTranslateMessage( msg->message, msg->hwnd,
1653                                    msg->wParam, msg->lParam );
1654 }
1655
1656
1657 /***********************************************************************
1658  *           DispatchMessage16   (USER.114)
1659  */
1660 LONG WINAPI DispatchMessage16( const MSG16* msg )
1661 {
1662     WND * wndPtr;
1663     LONG retval;
1664     int painting;
1665     
1666       /* Process timer messages */
1667     if ((msg->message == WM_TIMER) || (msg->message == WM_SYSTIMER))
1668     {
1669         if (msg->lParam)
1670         {
1671             return CallWindowProc16( (WNDPROC16)msg->lParam, msg->hwnd,
1672                                    msg->message, msg->wParam, GetTickCount() );
1673         }
1674     }
1675
1676     if (!msg->hwnd) return 0;
1677     if (!(wndPtr = WIN_FindWndPtr( msg->hwnd ))) return 0;
1678     if (!wndPtr->winproc) return 0;
1679     painting = (msg->message == WM_PAINT);
1680     if (painting) wndPtr->flags |= WIN_NEEDS_BEGINPAINT;
1681
1682     SPY_EnterMessage( SPY_DISPATCHMESSAGE16, msg->hwnd, msg->message,
1683                       msg->wParam, msg->lParam );
1684     retval = CallWindowProc16( (WNDPROC16)wndPtr->winproc,
1685                                msg->hwnd, msg->message,
1686                                msg->wParam, msg->lParam );
1687     SPY_ExitMessage( SPY_RESULT_OK16, msg->hwnd, msg->message, retval );
1688
1689     if (painting && (wndPtr = WIN_FindWndPtr( msg->hwnd )) &&
1690         (wndPtr->flags & WIN_NEEDS_BEGINPAINT) && wndPtr->hrgnUpdate)
1691     {
1692         ERR(msg, "BeginPaint not called on WM_PAINT for hwnd %04x!\n", 
1693             msg->hwnd);
1694         wndPtr->flags &= ~WIN_NEEDS_BEGINPAINT;
1695         /* Validate the update region to avoid infinite WM_PAINT loop */
1696         ValidateRect32( msg->hwnd, NULL );
1697     }
1698     return retval;
1699 }
1700
1701
1702 /***********************************************************************
1703  *           DispatchMessage32A   (USER32.141)
1704  */
1705 LONG WINAPI DispatchMessage32A( const MSG32* msg )
1706 {
1707     WND * wndPtr;
1708     LONG retval;
1709     int painting;
1710     
1711       /* Process timer messages */
1712     if ((msg->message == WM_TIMER) || (msg->message == WM_SYSTIMER))
1713     {
1714         if (msg->lParam)
1715         {
1716 /*            HOOK_CallHooks32A( WH_CALLWNDPROC, HC_ACTION, 0, FIXME ); */
1717             return CallWindowProc32A( (WNDPROC32)msg->lParam, msg->hwnd,
1718                                    msg->message, msg->wParam, GetTickCount() );
1719         }
1720     }
1721
1722     if (!msg->hwnd) return 0;
1723     if (!(wndPtr = WIN_FindWndPtr( msg->hwnd ))) return 0;
1724     if (!wndPtr->winproc) return 0;
1725     painting = (msg->message == WM_PAINT);
1726     if (painting) wndPtr->flags |= WIN_NEEDS_BEGINPAINT;
1727 /*    HOOK_CallHooks32A( WH_CALLWNDPROC, HC_ACTION, 0, FIXME ); */
1728
1729     SPY_EnterMessage( SPY_DISPATCHMESSAGE32, msg->hwnd, msg->message,
1730                       msg->wParam, msg->lParam );
1731     retval = CallWindowProc32A( (WNDPROC32)wndPtr->winproc,
1732                                 msg->hwnd, msg->message,
1733                                 msg->wParam, msg->lParam );
1734     SPY_ExitMessage( SPY_RESULT_OK32, msg->hwnd, msg->message, retval );
1735
1736     if (painting && (wndPtr = WIN_FindWndPtr( msg->hwnd )) &&
1737         (wndPtr->flags & WIN_NEEDS_BEGINPAINT) && wndPtr->hrgnUpdate)
1738     {
1739         ERR(msg, "BeginPaint not called on WM_PAINT for hwnd %04x!\n", 
1740             msg->hwnd);
1741         wndPtr->flags &= ~WIN_NEEDS_BEGINPAINT;
1742         /* Validate the update region to avoid infinite WM_PAINT loop */
1743         ValidateRect32( msg->hwnd, NULL );
1744     }
1745     return retval;
1746 }
1747
1748
1749 /***********************************************************************
1750  *           DispatchMessage32W   (USER32.142)
1751  *
1752  * Process the message specified in the structure *_msg_.
1753  *
1754  * If the lpMsg parameter points to a WM_TIMER message and the
1755  * parameter of the WM_TIMER message is not NULL, the lParam parameter
1756  * points to the function that is called instead of the window
1757  * procedure.
1758  *  
1759  * The message must be valid.
1760  *
1761  * RETURNS
1762  *
1763  *   DispatchMessage() returns the result of the window procedure invoked.
1764  *
1765  * CONFORMANCE
1766  *
1767  *   ECMA-234, Win32 
1768  *
1769  */
1770 LONG WINAPI DispatchMessage32W( const MSG32* msg )
1771 {
1772     WND * wndPtr;
1773     LONG retval;
1774     int painting;
1775     
1776       /* Process timer messages */
1777     if ((msg->message == WM_TIMER) || (msg->message == WM_SYSTIMER))
1778     {
1779         if (msg->lParam)
1780         {
1781 /*            HOOK_CallHooks32W( WH_CALLWNDPROC, HC_ACTION, 0, FIXME ); */
1782             return CallWindowProc32W( (WNDPROC32)msg->lParam, msg->hwnd,
1783                                    msg->message, msg->wParam, GetTickCount() );
1784         }
1785     }
1786
1787     if (!msg->hwnd) return 0;
1788     if (!(wndPtr = WIN_FindWndPtr( msg->hwnd ))) return 0;
1789     if (!wndPtr->winproc) return 0;
1790     painting = (msg->message == WM_PAINT);
1791     if (painting) wndPtr->flags |= WIN_NEEDS_BEGINPAINT;
1792 /*    HOOK_CallHooks32W( WH_CALLWNDPROC, HC_ACTION, 0, FIXME ); */
1793
1794     SPY_EnterMessage( SPY_DISPATCHMESSAGE32, msg->hwnd, msg->message,
1795                       msg->wParam, msg->lParam );
1796     retval = CallWindowProc32W( (WNDPROC32)wndPtr->winproc,
1797                                 msg->hwnd, msg->message,
1798                                 msg->wParam, msg->lParam );
1799     SPY_ExitMessage( SPY_RESULT_OK32, msg->hwnd, msg->message, retval );
1800
1801     if (painting && (wndPtr = WIN_FindWndPtr( msg->hwnd )) &&
1802         (wndPtr->flags & WIN_NEEDS_BEGINPAINT) && wndPtr->hrgnUpdate)
1803     {
1804         ERR(msg, "BeginPaint not called on WM_PAINT for hwnd %04x!\n", 
1805             msg->hwnd);
1806         wndPtr->flags &= ~WIN_NEEDS_BEGINPAINT;
1807         /* Validate the update region to avoid infinite WM_PAINT loop */
1808         ValidateRect32( msg->hwnd, NULL );
1809     }
1810     return retval;
1811 }
1812
1813
1814 /***********************************************************************
1815  *           RegisterWindowMessage16   (USER.118)
1816  */
1817 WORD WINAPI RegisterWindowMessage16( SEGPTR str )
1818 {
1819     TRACE(msg, "%08lx\n", (DWORD)str );
1820     return GlobalAddAtom16( str );
1821 }
1822
1823
1824 /***********************************************************************
1825  *           RegisterWindowMessage32A   (USER32.437)
1826  */
1827 WORD WINAPI RegisterWindowMessage32A( LPCSTR str )
1828 {
1829     TRACE(msg, "%s\n", str );
1830     return GlobalAddAtom32A( str );
1831 }
1832
1833
1834 /***********************************************************************
1835  *           RegisterWindowMessage32W   (USER32.438)
1836  */
1837 WORD WINAPI RegisterWindowMessage32W( LPCWSTR str )
1838 {
1839     TRACE(msg, "%p\n", str );
1840     return GlobalAddAtom32W( str );
1841 }
1842
1843
1844 /***********************************************************************
1845  *           GetTickCount   (USER.13) (KERNEL32.299)
1846  */
1847 DWORD WINAPI GetTickCount(void)
1848 {
1849     struct timeval t;
1850     gettimeofday( &t, NULL );
1851     /* make extremely compatible: granularity is 25 msec */
1852     return ((t.tv_sec * 1000) + (t.tv_usec / 25000) * 25) - MSG_WineStartTicks;
1853 }
1854
1855
1856 /***********************************************************************
1857  *           GetCurrentTime16    (USER.15)
1858  *
1859  * (effectively identical to GetTickCount)
1860  */
1861 DWORD WINAPI GetCurrentTime16(void)
1862 {
1863     return GetTickCount();
1864 }
1865
1866
1867 /***********************************************************************
1868  *           InSendMessage16    (USER.192)
1869  */
1870 BOOL16 WINAPI InSendMessage16(void)
1871 {
1872     return InSendMessage32();
1873 }
1874
1875
1876 /***********************************************************************
1877  *           InSendMessage32    (USER32.320)
1878  */
1879 BOOL32 WINAPI InSendMessage32(void)
1880 {
1881     MESSAGEQUEUE *queue;
1882
1883     if (!(queue = (MESSAGEQUEUE *)GlobalLock16( GetTaskQueue(0) )))
1884         return 0;
1885     return (BOOL32)queue->InSendMessageHandle;
1886 }
1887
1888 /***********************************************************************
1889  *           BroadcastSystemMessage    (USER32.12)
1890  */
1891 LONG WINAPI BroadcastSystemMessage(
1892         DWORD dwFlags,LPDWORD recipients,UINT32 uMessage,WPARAM32 wParam,
1893         LPARAM lParam
1894 ) {
1895         FIXME(sendmsg,"(%08lx,%08lx,%08x,%08x,%08lx): stub!\n",
1896               dwFlags,*recipients,uMessage,wParam,lParam
1897         );
1898         return 0;
1899 }
1900
1901 /***********************************************************************
1902  *           SendNotifyMessageA    (USER32.460)
1903  * FIXME
1904  *  The message sended with PostMessage has to be put in the queue
1905  *  with a higher priority as the other "Posted" messages.
1906  *  QUEUE_AddMsg has to be modifyed.
1907  */
1908 LONG WINAPI SendNotifyMessage32A(HWND32 hwnd,UINT32 msg,WPARAM32 wParam,LPARAM lParam)
1909 {       BOOL32 ret = TRUE;
1910         FIXME(msg,"(%04x,%08x,%08x,%08lx) not complete\n",
1911               hwnd, msg, wParam, lParam);
1912               
1913         if ( GetCurrentThreadId() == GetWindowThreadProcessId ( hwnd, NULL))
1914         {       ret=SendMessage32A ( hwnd, msg, wParam, lParam );
1915         }
1916         else
1917         {       PostMessage32A ( hwnd, msg, wParam, lParam );
1918         }
1919         return ret;
1920 }
1921 /***********************************************************************
1922  *           SendMessageCallBack32A
1923  * FIXME: It's like PostMessage. The callback gets called when the message
1924  * is processed. We have to modify the message processing for a exact
1925  * implementation...
1926  */
1927 BOOL32 WINAPI SendMessageCallBack32A(
1928         HWND32 hWnd,UINT32 Msg,WPARAM32 wParam,LPARAM lParam,
1929         FARPROC32 lpResultCallBack,DWORD dwData)
1930 {       
1931         FIXME(msg,"(0x%04x,0x%04x,0x%08x,0x%08lx,%p,0x%08lx),stub!\n",
1932                 hWnd,Msg,wParam,lParam,lpResultCallBack,dwData);
1933         if ( hWnd == HWND_BROADCAST)
1934         {       PostMessage32A( hWnd, Msg, wParam, lParam);
1935                 FIXME(msg,"Broadcast: Callback will not be called!\n");
1936                 return TRUE;
1937         }
1938         (lpResultCallBack)( hWnd, Msg, dwData, SendMessage32A ( hWnd, Msg, wParam, lParam ));
1939                 return TRUE;
1940 }