Removed useless #ifdef WINELIB.
[wine/gsoc_dplay.git] / windows / message.c
blobcd4de4ac4f3dbddb245a7e2f7b6d2808f3a23043
1 /*
2 * Message queues related functions
4 * Copyright 1993, 1994 Alexandre Julliard
6 * This library is free software; you can redistribute it and/or
7 * modify it under the terms of the GNU Lesser General Public
8 * License as published by the Free Software Foundation; either
9 * version 2.1 of the License, or (at your option) any later version.
11 * This library is distributed in the hope that it will be useful,
12 * but WITHOUT ANY WARRANTY; without even the implied warranty of
13 * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the GNU
14 * Lesser General Public License for more details.
16 * You should have received a copy of the GNU Lesser General Public
17 * License along with this library; if not, write to the Free Software
18 * Foundation, Inc., 59 Temple Place, Suite 330, Boston, MA 02111-1307 USA
21 #include "config.h"
22 #include "wine/port.h"
24 #include <stdarg.h>
25 #include <stdlib.h>
26 #include <string.h>
27 #include <ctype.h>
28 #ifdef HAVE_SYS_TIME_H
29 # include <sys/time.h>
30 #endif
31 #include <sys/types.h>
33 #include "windef.h"
34 #include "winbase.h"
35 #include "wingdi.h"
36 #include "winuser.h"
37 #include "message.h"
38 #include "winerror.h"
39 #include "ntstatus.h"
40 #include "wine/server.h"
41 #include "controls.h"
42 #include "dde.h"
43 #include "message.h"
44 #include "user.h"
45 #include "win.h"
46 #include "winpos.h"
47 #include "winproc.h"
48 #include "wine/debug.h"
50 WINE_DEFAULT_DEBUG_CHANNEL(msg);
51 WINE_DECLARE_DEBUG_CHANNEL(key);
53 #define WM_NCMOUSEFIRST WM_NCMOUSEMOVE
54 #define WM_NCMOUSELAST WM_NCMBUTTONDBLCLK
57 /***********************************************************************
58 * is_keyboard_message
60 inline static BOOL is_keyboard_message( UINT message )
62 return (message >= WM_KEYFIRST && message <= WM_KEYLAST);
66 /***********************************************************************
67 * is_mouse_message
69 inline static BOOL is_mouse_message( UINT message )
71 return ((message >= WM_NCMOUSEFIRST && message <= WM_NCMOUSELAST) ||
72 (message >= WM_MOUSEFIRST && message <= WM_MOUSELAST));
76 /***********************************************************************
77 * check_message_filter
79 inline static BOOL check_message_filter( const MSG *msg, HWND hwnd, UINT first, UINT last )
81 if (hwnd)
83 if (msg->hwnd != hwnd && !IsChild( hwnd, msg->hwnd )) return FALSE;
85 if (first || last)
87 return (msg->message >= first && msg->message <= last);
89 return TRUE;
93 /***********************************************************************
94 * process_sent_messages
96 * Process all pending sent messages.
98 inline static void process_sent_messages(void)
100 MSG msg;
101 MSG_peek_message( &msg, 0, 0, 0, GET_MSG_REMOVE | GET_MSG_SENT_ONLY );
105 /***********************************************************************
106 * queue_hardware_message
108 * store a hardware message in the thread queue
110 #if 0
111 static void queue_hardware_message( MSG *msg, ULONG_PTR extra_info )
113 SERVER_START_REQ( send_message )
115 req->type = MSG_HARDWARE;
116 req->id = GetWindowThreadProcessId( msg->hwnd, NULL );
117 req->win = msg->hwnd;
118 req->msg = msg->message;
119 req->wparam = msg->wParam;
120 req->lparam = msg->lParam;
121 req->x = msg->pt.x;
122 req->y = msg->pt.y;
123 req->time = msg->time;
124 req->info = extra_info;
125 req->timeout = 0;
126 wine_server_call( req );
128 SERVER_END_REQ;
130 #endif
133 /***********************************************************************
134 * MSG_SendParentNotify
136 * Send a WM_PARENTNOTIFY to all ancestors of the given window, unless
137 * the window has the WS_EX_NOPARENTNOTIFY style.
139 static void MSG_SendParentNotify( HWND hwnd, WORD event, WORD idChild, POINT pt )
141 /* pt has to be in the client coordinates of the parent window */
142 MapWindowPoints( 0, hwnd, &pt, 1 );
143 for (;;)
145 HWND parent;
147 if (!(GetWindowLongA( hwnd, GWL_STYLE ) & WS_CHILD)) break;
148 if (GetWindowLongA( hwnd, GWL_EXSTYLE ) & WS_EX_NOPARENTNOTIFY) break;
149 if (!(parent = GetParent(hwnd))) break;
150 MapWindowPoints( hwnd, parent, &pt, 1 );
151 hwnd = parent;
152 SendMessageA( hwnd, WM_PARENTNOTIFY,
153 MAKEWPARAM( event, idChild ), MAKELPARAM( pt.x, pt.y ) );
158 #if 0 /* this is broken for now, will require proper support in the server */
160 /***********************************************************************
161 * MSG_JournalPlayBackMsg
163 * Get an EVENTMSG struct via call JOURNALPLAYBACK hook function
165 void MSG_JournalPlayBackMsg(void)
167 EVENTMSG tmpMsg;
168 MSG msg;
169 LRESULT wtime;
170 int keyDown,i;
172 wtime=HOOK_CallHooks( WH_JOURNALPLAYBACK, HC_GETNEXT, 0, (LPARAM)&tmpMsg, TRUE );
173 /* TRACE(msg,"Playback wait time =%ld\n",wtime); */
174 if (wtime<=0)
176 wtime=0;
177 msg.message = tmpMsg.message;
178 msg.hwnd = tmpMsg.hwnd;
179 msg.time = tmpMsg.time;
180 if ((tmpMsg.message >= WM_KEYFIRST) && (tmpMsg.message <= WM_KEYLAST))
182 msg.wParam = tmpMsg.paramL & 0xFF;
183 msg.lParam = MAKELONG(tmpMsg.paramH&0x7ffff,tmpMsg.paramL>>8);
184 if (tmpMsg.message == WM_KEYDOWN || tmpMsg.message == WM_SYSKEYDOWN)
186 for (keyDown=i=0; i<256 && !keyDown; i++)
187 if (InputKeyStateTable[i] & 0x80)
188 keyDown++;
189 if (!keyDown)
190 msg.lParam |= 0x40000000;
191 InputKeyStateTable[msg.wParam] |= 0x80;
192 AsyncKeyStateTable[msg.wParam] |= 0x80;
194 else /* WM_KEYUP, WM_SYSKEYUP */
196 msg.lParam |= 0xC0000000;
197 InputKeyStateTable[msg.wParam] &= ~0x80;
199 if (InputKeyStateTable[VK_MENU] & 0x80)
200 msg.lParam |= 0x20000000;
201 if (tmpMsg.paramH & 0x8000) /*special_key bit*/
202 msg.lParam |= 0x01000000;
204 msg.pt.x = msg.pt.y = 0;
205 queue_hardware_message( &msg, 0 );
207 else if ((tmpMsg.message>= WM_MOUSEFIRST) && (tmpMsg.message <= WM_MOUSELAST))
209 switch (tmpMsg.message)
211 case WM_LBUTTONDOWN:
212 InputKeyStateTable[VK_LBUTTON] |= 0x80;
213 AsyncKeyStateTable[VK_LBUTTON] |= 0x80;
214 break;
215 case WM_LBUTTONUP:
216 InputKeyStateTable[VK_LBUTTON] &= ~0x80;
217 break;
218 case WM_MBUTTONDOWN:
219 InputKeyStateTable[VK_MBUTTON] |= 0x80;
220 AsyncKeyStateTable[VK_MBUTTON] |= 0x80;
221 break;
222 case WM_MBUTTONUP:
223 InputKeyStateTable[VK_MBUTTON] &= ~0x80;
224 break;
225 case WM_RBUTTONDOWN:
226 InputKeyStateTable[VK_RBUTTON] |= 0x80;
227 AsyncKeyStateTable[VK_RBUTTON] |= 0x80;
228 break;
229 case WM_RBUTTONUP:
230 InputKeyStateTable[VK_RBUTTON] &= ~0x80;
231 break;
233 SetCursorPos(tmpMsg.paramL,tmpMsg.paramH);
234 msg.lParam=MAKELONG(tmpMsg.paramL,tmpMsg.paramH);
235 msg.wParam=0;
236 if (InputKeyStateTable[VK_LBUTTON] & 0x80) msg.wParam |= MK_LBUTTON;
237 if (InputKeyStateTable[VK_MBUTTON] & 0x80) msg.wParam |= MK_MBUTTON;
238 if (InputKeyStateTable[VK_RBUTTON] & 0x80) msg.wParam |= MK_RBUTTON;
240 msg.pt.x = tmpMsg.paramL;
241 msg.pt.y = tmpMsg.paramH;
242 queue_hardware_message( &msg, 0 );
244 HOOK_CallHooks( WH_JOURNALPLAYBACK, HC_SKIP, 0, (LPARAM)&tmpMsg, TRUE );
246 else
248 if( tmpMsg.message == WM_QUEUESYNC ) HOOK_CallHooks( WH_CBT, HCBT_QS, 0, 0, TRUE );
251 #endif
254 /***********************************************************************
255 * process_raw_keyboard_message
257 * returns TRUE if the contents of 'msg' should be passed to the application
259 static void process_raw_keyboard_message( MSG *msg )
261 EVENTMSG event;
263 event.message = msg->message;
264 event.hwnd = msg->hwnd;
265 event.time = msg->time;
266 event.paramL = (msg->wParam & 0xFF) | (HIWORD(msg->lParam) << 8);
267 event.paramH = msg->lParam & 0x7FFF;
268 if (HIWORD(msg->lParam) & 0x0100) event.paramH |= 0x8000; /* special_key - bit */
269 HOOK_CallHooks( WH_JOURNALRECORD, HC_ACTION, 0, (LPARAM)&event, TRUE );
273 /***********************************************************************
274 * process_cooked_keyboard_message
276 * returns TRUE if the contents of 'msg' should be passed to the application
278 static BOOL process_cooked_keyboard_message( MSG *msg, BOOL remove )
280 if (remove)
282 /* Handle F1 key by sending out WM_HELP message */
283 if ((msg->message == WM_KEYUP) &&
284 (msg->wParam == VK_F1) &&
285 (msg->hwnd != GetDesktopWindow()) &&
286 !MENU_IsMenuActive())
288 HELPINFO hi;
289 hi.cbSize = sizeof(HELPINFO);
290 hi.iContextType = HELPINFO_WINDOW;
291 hi.iCtrlId = GetWindowLongA( msg->hwnd, GWL_ID );
292 hi.hItemHandle = msg->hwnd;
293 hi.dwContextId = GetWindowContextHelpId( msg->hwnd );
294 hi.MousePos = msg->pt;
295 SendMessageA(msg->hwnd, WM_HELP, 0, (LPARAM)&hi);
299 if (HOOK_CallHooks( WH_KEYBOARD, remove ? HC_ACTION : HC_NOREMOVE,
300 LOWORD(msg->wParam), msg->lParam, TRUE ))
302 /* skip this message */
303 HOOK_CallHooks( WH_CBT, HCBT_KEYSKIPPED, LOWORD(msg->wParam), msg->lParam, TRUE );
304 return FALSE;
306 return TRUE;
310 /***********************************************************************
311 * process_raw_mouse_message
313 static void process_raw_mouse_message( MSG *msg, BOOL remove )
315 static MSG clk_msg;
317 POINT pt;
318 INT hittest;
319 EVENTMSG event;
320 GUITHREADINFO info;
321 HWND hWndScope = msg->hwnd;
323 /* find the window to dispatch this mouse message to */
325 hittest = HTCLIENT;
326 GetGUIThreadInfo( GetCurrentThreadId(), &info );
327 if (!(msg->hwnd = info.hwndCapture))
329 /* If no capture HWND, find window which contains the mouse position.
330 * Also find the position of the cursor hot spot (hittest) */
331 if (!IsWindow(hWndScope)) hWndScope = 0;
332 if (!(msg->hwnd = WINPOS_WindowFromPoint( hWndScope, msg->pt, &hittest )))
333 msg->hwnd = GetDesktopWindow();
336 event.message = msg->message;
337 event.time = msg->time;
338 event.hwnd = msg->hwnd;
339 event.paramL = msg->pt.x;
340 event.paramH = msg->pt.y;
341 HOOK_CallHooks( WH_JOURNALRECORD, HC_ACTION, 0, (LPARAM)&event, TRUE );
343 /* translate double clicks */
345 if ((msg->message == WM_LBUTTONDOWN) ||
346 (msg->message == WM_RBUTTONDOWN) ||
347 (msg->message == WM_MBUTTONDOWN))
349 BOOL update = remove;
350 /* translate double clicks -
351 * note that ...MOUSEMOVEs can slip in between
352 * ...BUTTONDOWN and ...BUTTONDBLCLK messages */
354 if ((info.flags & (GUI_INMENUMODE|GUI_INMOVESIZE)) ||
355 hittest != HTCLIENT ||
356 (GetClassLongA( msg->hwnd, GCL_STYLE ) & CS_DBLCLKS))
358 if ((msg->message == clk_msg.message) &&
359 (msg->hwnd == clk_msg.hwnd) &&
360 (msg->time - clk_msg.time < GetDoubleClickTime()) &&
361 (abs(msg->pt.x - clk_msg.pt.x) < GetSystemMetrics(SM_CXDOUBLECLK)/2) &&
362 (abs(msg->pt.y - clk_msg.pt.y) < GetSystemMetrics(SM_CYDOUBLECLK)/2))
364 msg->message += (WM_LBUTTONDBLCLK - WM_LBUTTONDOWN);
365 if (remove)
367 clk_msg.message = 0;
368 update = FALSE;
372 /* update static double click conditions */
373 if (update) clk_msg = *msg;
376 pt = msg->pt;
377 /* Note: windows has no concept of a non-client wheel message */
378 if (hittest != HTCLIENT && msg->message != WM_MOUSEWHEEL)
380 msg->message += WM_NCMOUSEMOVE - WM_MOUSEMOVE;
381 msg->wParam = hittest;
383 else
385 /* coordinates don't get translated while tracking a menu */
386 /* FIXME: should differentiate popups and top-level menus */
387 if (!(info.flags & GUI_INMENUMODE)) ScreenToClient( msg->hwnd, &pt );
389 msg->lParam = MAKELONG( pt.x, pt.y );
393 /***********************************************************************
394 * process_cooked_mouse_message
396 * returns TRUE if the contents of 'msg' should be passed to the application
398 static BOOL process_cooked_mouse_message( MSG *msg, ULONG_PTR extra_info, BOOL remove )
400 MOUSEHOOKSTRUCT hook;
401 INT hittest = HTCLIENT;
402 UINT raw_message = msg->message;
403 BOOL eatMsg;
405 if (msg->message >= WM_NCMOUSEFIRST && msg->message <= WM_NCMOUSELAST)
407 raw_message += WM_MOUSEFIRST - WM_NCMOUSEFIRST;
408 hittest = msg->wParam;
410 if (raw_message == WM_LBUTTONDBLCLK ||
411 raw_message == WM_RBUTTONDBLCLK ||
412 raw_message == WM_MBUTTONDBLCLK)
414 raw_message += WM_LBUTTONDOWN - WM_LBUTTONDBLCLK;
417 hook.pt = msg->pt;
418 hook.hwnd = msg->hwnd;
419 hook.wHitTestCode = hittest;
420 hook.dwExtraInfo = extra_info;
421 if (HOOK_CallHooks( WH_MOUSE, remove ? HC_ACTION : HC_NOREMOVE,
422 msg->message, (LPARAM)&hook, TRUE ))
424 hook.pt = msg->pt;
425 hook.hwnd = msg->hwnd;
426 hook.wHitTestCode = hittest;
427 hook.dwExtraInfo = extra_info;
428 HOOK_CallHooks( WH_CBT, HCBT_CLICKSKIPPED, msg->message, (LPARAM)&hook, TRUE );
429 return FALSE;
432 if ((hittest == HTERROR) || (hittest == HTNOWHERE))
434 SendMessageA( msg->hwnd, WM_SETCURSOR, (WPARAM)msg->hwnd,
435 MAKELONG( hittest, raw_message ));
436 return FALSE;
439 if (!remove || GetCapture()) return TRUE;
441 eatMsg = FALSE;
443 if ((raw_message == WM_LBUTTONDOWN) ||
444 (raw_message == WM_RBUTTONDOWN) ||
445 (raw_message == WM_MBUTTONDOWN))
447 /* Send the WM_PARENTNOTIFY,
448 * note that even for double/nonclient clicks
449 * notification message is still WM_L/M/RBUTTONDOWN.
451 MSG_SendParentNotify( msg->hwnd, raw_message, 0, msg->pt );
453 /* Activate the window if needed */
455 if (msg->hwnd != GetActiveWindow())
457 HWND hwndTop = msg->hwnd;
458 while (hwndTop)
460 if ((GetWindowLongW( hwndTop, GWL_STYLE ) & (WS_POPUP|WS_CHILD)) != WS_CHILD) break;
461 hwndTop = GetParent( hwndTop );
464 if (hwndTop && hwndTop != GetDesktopWindow())
466 LONG ret = SendMessageA( msg->hwnd, WM_MOUSEACTIVATE, (WPARAM)hwndTop,
467 MAKELONG( hittest, raw_message ) );
468 switch(ret)
470 case MA_NOACTIVATEANDEAT:
471 eatMsg = TRUE;
472 /* fall through */
473 case MA_NOACTIVATE:
474 break;
475 case MA_ACTIVATEANDEAT:
476 eatMsg = TRUE;
477 /* fall through */
478 case MA_ACTIVATE:
479 case 0:
480 if (!FOCUS_MouseActivate( hwndTop )) eatMsg = TRUE;
481 break;
482 default:
483 WARN( "unknown WM_MOUSEACTIVATE code %ld\n", ret );
484 break;
490 /* send the WM_SETCURSOR message */
492 /* Windows sends the normal mouse message as the message parameter
493 in the WM_SETCURSOR message even if it's non-client mouse message */
494 SendMessageA( msg->hwnd, WM_SETCURSOR, (WPARAM)msg->hwnd,
495 MAKELONG( hittest, raw_message ));
497 return !eatMsg;
501 /***********************************************************************
502 * process_hardware_message
504 * returns TRUE if the contents of 'msg' should be passed to the application
506 BOOL MSG_process_raw_hardware_message( MSG *msg, ULONG_PTR extra_info, HWND hwnd_filter,
507 UINT first, UINT last, BOOL remove )
509 if (is_keyboard_message( msg->message ))
511 process_raw_keyboard_message( msg );
513 else if (is_mouse_message( msg->message ))
515 process_raw_mouse_message( msg, remove );
517 else
519 ERR( "unknown message type %x\n", msg->message );
520 return FALSE;
522 return check_message_filter( msg, hwnd_filter, first, last );
526 /***********************************************************************
527 * MSG_process_cooked_hardware_message
529 * returns TRUE if the contents of 'msg' should be passed to the application
531 BOOL MSG_process_cooked_hardware_message( MSG *msg, ULONG_PTR extra_info, BOOL remove )
533 if (is_keyboard_message( msg->message ))
534 return process_cooked_keyboard_message( msg, remove );
536 if (is_mouse_message( msg->message ))
537 return process_cooked_mouse_message( msg, extra_info, remove );
539 ERR( "unknown message type %x\n", msg->message );
540 return FALSE;
544 /***********************************************************************
545 * WaitMessage (USER.112) Suspend thread pending messages
546 * WaitMessage (USER32.@) Suspend thread pending messages
548 * WaitMessage() suspends a thread until events appear in the thread's
549 * queue.
551 BOOL WINAPI WaitMessage(void)
553 return (MsgWaitForMultipleObjectsEx( 0, NULL, INFINITE, QS_ALLINPUT, 0 ) != WAIT_FAILED);
557 /***********************************************************************
558 * MsgWaitForMultipleObjectsEx (USER32.@)
560 DWORD WINAPI MsgWaitForMultipleObjectsEx( DWORD count, CONST HANDLE *pHandles,
561 DWORD timeout, DWORD mask, DWORD flags )
563 HANDLE handles[MAXIMUM_WAIT_OBJECTS];
564 DWORD i, ret, lock;
565 MESSAGEQUEUE *msgQueue;
567 if (count > MAXIMUM_WAIT_OBJECTS-1)
569 SetLastError( ERROR_INVALID_PARAMETER );
570 return WAIT_FAILED;
573 if (!(msgQueue = QUEUE_Current())) return WAIT_FAILED;
575 /* set the queue mask */
576 SERVER_START_REQ( set_queue_mask )
578 req->wake_mask = (flags & MWMO_INPUTAVAILABLE) ? mask : 0;
579 req->changed_mask = mask;
580 req->skip_wait = 0;
581 wine_server_call( req );
583 SERVER_END_REQ;
585 /* Add the thread event to the handle list */
586 for (i = 0; i < count; i++) handles[i] = pHandles[i];
587 handles[count] = msgQueue->server_queue;
589 ReleaseThunkLock( &lock );
590 if (USER_Driver.pMsgWaitForMultipleObjectsEx)
592 ret = USER_Driver.pMsgWaitForMultipleObjectsEx( count+1, handles, timeout, mask, flags );
593 if (ret == count+1) ret = count; /* pretend the msg queue is ready */
595 else
596 ret = WaitForMultipleObjectsEx( count+1, handles, flags & MWMO_WAITALL,
597 timeout, flags & MWMO_ALERTABLE );
598 if (lock) RestoreThunkLock( lock );
599 return ret;
603 /***********************************************************************
604 * MsgWaitForMultipleObjects (USER32.@)
606 DWORD WINAPI MsgWaitForMultipleObjects( DWORD count, CONST HANDLE *handles,
607 BOOL wait_all, DWORD timeout, DWORD mask )
609 return MsgWaitForMultipleObjectsEx( count, handles, timeout, mask,
610 wait_all ? MWMO_WAITALL : 0 );
614 /***********************************************************************
615 * WaitForInputIdle (USER32.@)
617 DWORD WINAPI WaitForInputIdle( HANDLE hProcess, DWORD dwTimeOut )
619 DWORD start_time, elapsed, ret;
620 HANDLE idle_event = (HANDLE)-1;
622 SERVER_START_REQ( wait_input_idle )
624 req->handle = hProcess;
625 req->timeout = dwTimeOut;
626 if (!(ret = wine_server_call_err( req ))) idle_event = reply->event;
628 SERVER_END_REQ;
629 if (ret) return WAIT_FAILED; /* error */
630 if (!idle_event) return 0; /* no event to wait on */
632 start_time = GetTickCount();
633 elapsed = 0;
635 TRACE("waiting for %p\n", idle_event );
638 ret = MsgWaitForMultipleObjects ( 1, &idle_event, FALSE, dwTimeOut - elapsed, QS_SENDMESSAGE );
639 switch (ret)
641 case WAIT_OBJECT_0+1:
642 process_sent_messages();
643 break;
644 case WAIT_TIMEOUT:
645 case WAIT_FAILED:
646 TRACE("timeout or error\n");
647 return ret;
648 default:
649 TRACE("finished\n");
650 return 0;
652 if (dwTimeOut != INFINITE)
654 elapsed = GetTickCount() - start_time;
655 if (elapsed > dwTimeOut)
656 break;
659 while (1);
661 return WAIT_TIMEOUT;
665 /***********************************************************************
666 * UserYield (USER.332)
668 void WINAPI UserYield16(void)
670 DWORD count;
672 /* Handle sent messages */
673 process_sent_messages();
675 /* Yield */
676 ReleaseThunkLock(&count);
677 if (count)
679 RestoreThunkLock(count);
680 /* Handle sent messages again */
681 process_sent_messages();
686 /***********************************************************************
687 * TranslateMessage (USER32.@)
689 * Implementation of TranslateMessage.
691 * TranslateMessage translates virtual-key messages into character-messages,
692 * as follows :
693 * WM_KEYDOWN/WM_KEYUP combinations produce a WM_CHAR or WM_DEADCHAR message.
694 * ditto replacing WM_* with WM_SYS*
695 * This produces WM_CHAR messages only for keys mapped to ASCII characters
696 * by the keyboard driver.
698 * If the message is WM_KEYDOWN, WM_KEYUP, WM_SYSKEYDOWN, or WM_SYSKEYUP, the
699 * return value is nonzero, regardless of the translation.
702 BOOL WINAPI TranslateMessage( const MSG *msg )
704 UINT message;
705 WCHAR wp[2];
706 BOOL rc = FALSE;
707 BYTE state[256];
709 if (msg->message >= WM_KEYFIRST && msg->message <= WM_KEYLAST)
711 TRACE_(key)("(%s, %04X, %08lX)\n",
712 SPY_GetMsgName(msg->message, msg->hwnd), msg->wParam, msg->lParam );
714 /* Return code must be TRUE no matter what! */
715 rc = TRUE;
718 if ((msg->message != WM_KEYDOWN) && (msg->message != WM_SYSKEYDOWN)) return rc;
720 TRACE_(key)("Translating key %s (%04x), scancode %02x\n",
721 SPY_GetVKeyName(msg->wParam), msg->wParam, LOBYTE(HIWORD(msg->lParam)));
723 GetKeyboardState( state );
724 /* FIXME : should handle ToUnicode yielding 2 */
725 switch (ToUnicode(msg->wParam, HIWORD(msg->lParam), state, wp, 2, 0))
727 case 1:
728 message = (msg->message == WM_KEYDOWN) ? WM_CHAR : WM_SYSCHAR;
729 TRACE_(key)("1 -> PostMessageW(%p,%s,%04x,%08lx)\n",
730 msg->hwnd, SPY_GetMsgName(message, msg->hwnd), wp[0], msg->lParam);
731 PostMessageW( msg->hwnd, message, wp[0], msg->lParam );
732 break;
734 case -1:
735 message = (msg->message == WM_KEYDOWN) ? WM_DEADCHAR : WM_SYSDEADCHAR;
736 TRACE_(key)("-1 -> PostMessageW(%p,%s,%04x,%08lx)\n",
737 msg->hwnd, SPY_GetMsgName(message, msg->hwnd), wp[0], msg->lParam);
738 PostMessageW( msg->hwnd, message, wp[0], msg->lParam );
739 return TRUE;
741 return rc;
745 /***********************************************************************
746 * DispatchMessageA (USER32.@)
748 LONG WINAPI DispatchMessageA( const MSG* msg )
750 WND * wndPtr;
751 LONG retval;
752 int painting;
753 WNDPROC winproc;
755 /* Process timer messages */
756 if ((msg->message == WM_TIMER) || (msg->message == WM_SYSTIMER))
758 if (msg->lParam)
760 /* HOOK_CallHooks32A( WH_CALLWNDPROC, HC_ACTION, 0, FIXME ); */
762 /* before calling window proc, verify whether timer is still valid;
763 there's a slim chance that the application kills the timer
764 between GetMessage and DispatchMessage API calls */
765 if (!TIMER_IsTimerValid(msg->hwnd, (UINT) msg->wParam, (WNDPROC)msg->lParam))
766 return 0; /* invalid winproc */
768 return CallWindowProcA( (WNDPROC)msg->lParam, msg->hwnd,
769 msg->message, msg->wParam, GetTickCount() );
773 if (!(wndPtr = WIN_GetPtr( msg->hwnd )))
775 if (msg->hwnd) SetLastError( ERROR_INVALID_WINDOW_HANDLE );
776 return 0;
778 if (wndPtr == WND_OTHER_PROCESS)
780 if (IsWindow( msg->hwnd ))
781 ERR( "cannot dispatch msg to other process window %p\n", msg->hwnd );
782 SetLastError( ERROR_INVALID_WINDOW_HANDLE );
783 return 0;
785 if (!(winproc = wndPtr->winproc))
787 WIN_ReleasePtr( wndPtr );
788 return 0;
790 painting = (msg->message == WM_PAINT);
791 if (painting) wndPtr->flags |= WIN_NEEDS_BEGINPAINT;
792 WIN_ReleasePtr( wndPtr );
793 /* hook_CallHooks32A( WH_CALLWNDPROC, HC_ACTION, 0, FIXME ); */
795 SPY_EnterMessage( SPY_DISPATCHMESSAGE, msg->hwnd, msg->message,
796 msg->wParam, msg->lParam );
797 retval = CallWindowProcA( winproc, msg->hwnd, msg->message,
798 msg->wParam, msg->lParam );
799 SPY_ExitMessage( SPY_RESULT_OK, msg->hwnd, msg->message, retval,
800 msg->wParam, msg->lParam );
802 if (painting && (wndPtr = WIN_GetPtr( msg->hwnd )) && (wndPtr != WND_OTHER_PROCESS))
804 BOOL validate = ((wndPtr->flags & WIN_NEEDS_BEGINPAINT) && wndPtr->hrgnUpdate);
805 wndPtr->flags &= ~WIN_NEEDS_BEGINPAINT;
806 WIN_ReleasePtr( wndPtr );
807 if (validate)
809 ERR( "BeginPaint not called on WM_PAINT for hwnd %p!\n", msg->hwnd );
810 /* Validate the update region to avoid infinite WM_PAINT loop */
811 RedrawWindow( msg->hwnd, NULL, 0,
812 RDW_NOFRAME | RDW_VALIDATE | RDW_NOCHILDREN | RDW_NOINTERNALPAINT );
815 return retval;
819 /***********************************************************************
820 * DispatchMessageW (USER32.@) Process Message
822 * Process the message specified in the structure *_msg_.
824 * If the lpMsg parameter points to a WM_TIMER message and the
825 * parameter of the WM_TIMER message is not NULL, the lParam parameter
826 * points to the function that is called instead of the window
827 * procedure.
829 * The message must be valid.
831 * RETURNS
833 * DispatchMessage() returns the result of the window procedure invoked.
835 * CONFORMANCE
837 * ECMA-234, Win32
840 LONG WINAPI DispatchMessageW( const MSG* msg )
842 WND * wndPtr;
843 LONG retval;
844 int painting;
845 WNDPROC winproc;
847 /* Process timer messages */
848 if ((msg->message == WM_TIMER) || (msg->message == WM_SYSTIMER))
850 if (msg->lParam)
852 /* HOOK_CallHooks32W( WH_CALLWNDPROC, HC_ACTION, 0, FIXME ); */
854 /* before calling window proc, verify whether timer is still valid;
855 there's a slim chance that the application kills the timer
856 between GetMessage and DispatchMessage API calls */
857 if (!TIMER_IsTimerValid(msg->hwnd, (UINT) msg->wParam, (WNDPROC)msg->lParam))
858 return 0; /* invalid winproc */
860 return CallWindowProcW( (WNDPROC)msg->lParam, msg->hwnd,
861 msg->message, msg->wParam, GetTickCount() );
865 if (!(wndPtr = WIN_GetPtr( msg->hwnd )))
867 if (msg->hwnd) SetLastError( ERROR_INVALID_WINDOW_HANDLE );
868 return 0;
870 if (wndPtr == WND_OTHER_PROCESS)
872 if (IsWindow( msg->hwnd ))
873 ERR( "cannot dispatch msg to other process window %p\n", msg->hwnd );
874 SetLastError( ERROR_INVALID_WINDOW_HANDLE );
875 return 0;
877 if (!(winproc = wndPtr->winproc))
879 WIN_ReleasePtr( wndPtr );
880 return 0;
882 painting = (msg->message == WM_PAINT);
883 if (painting) wndPtr->flags |= WIN_NEEDS_BEGINPAINT;
884 WIN_ReleasePtr( wndPtr );
885 /* HOOK_CallHooks32W( WH_CALLWNDPROC, HC_ACTION, 0, FIXME ); */
887 SPY_EnterMessage( SPY_DISPATCHMESSAGE, msg->hwnd, msg->message,
888 msg->wParam, msg->lParam );
889 retval = CallWindowProcW( winproc, msg->hwnd, msg->message,
890 msg->wParam, msg->lParam );
891 SPY_ExitMessage( SPY_RESULT_OK, msg->hwnd, msg->message, retval,
892 msg->wParam, msg->lParam );
894 if (painting && (wndPtr = WIN_GetPtr( msg->hwnd )) && (wndPtr != WND_OTHER_PROCESS))
896 BOOL validate = ((wndPtr->flags & WIN_NEEDS_BEGINPAINT) && wndPtr->hrgnUpdate);
897 wndPtr->flags &= ~WIN_NEEDS_BEGINPAINT;
898 WIN_ReleasePtr( wndPtr );
899 if (validate)
901 ERR( "BeginPaint not called on WM_PAINT for hwnd %p!\n", msg->hwnd );
902 /* Validate the update region to avoid infinite WM_PAINT loop */
903 RedrawWindow( msg->hwnd, NULL, 0,
904 RDW_NOFRAME | RDW_VALIDATE | RDW_NOCHILDREN | RDW_NOINTERNALPAINT );
907 return retval;
911 /***********************************************************************
912 * RegisterWindowMessage (USER.118)
913 * RegisterWindowMessageA (USER32.@)
915 WORD WINAPI RegisterWindowMessageA( LPCSTR str )
917 TRACE("%s\n", str );
918 return GlobalAddAtomA( str );
922 /***********************************************************************
923 * RegisterWindowMessageW (USER32.@)
925 WORD WINAPI RegisterWindowMessageW( LPCWSTR str )
927 TRACE("%p\n", str );
928 return GlobalAddAtomW( str );
932 /***********************************************************************
933 * BroadcastSystemMessage (USER32.@)
934 * BroadcastSystemMessageA (USER32.@)
936 LONG WINAPI BroadcastSystemMessageA(
937 DWORD dwFlags,LPDWORD recipients,UINT uMessage,WPARAM wParam,
938 LPARAM lParam )
940 if ((*recipients & BSM_APPLICATIONS)||
941 (*recipients == BSM_ALLCOMPONENTS))
943 FIXME("(%08lx,%08lx,%08x,%08x,%08lx): semi-stub!\n",
944 dwFlags,*recipients,uMessage,wParam,lParam);
945 PostMessageA(HWND_BROADCAST,uMessage,wParam,lParam);
946 return 1;
948 else
950 FIXME("(%08lx,%08lx,%08x,%08x,%08lx): stub!\n",
951 dwFlags,*recipients,uMessage,wParam,lParam);
952 return -1;
956 /***********************************************************************
957 * BroadcastSystemMessageW (USER32.@)
959 LONG WINAPI BroadcastSystemMessageW(
960 DWORD dwFlags,LPDWORD recipients,UINT uMessage,WPARAM wParam,
961 LPARAM lParam )
963 if ((*recipients & BSM_APPLICATIONS)||
964 (*recipients == BSM_ALLCOMPONENTS))
966 FIXME("(%08lx,%08lx,%08x,%08x,%08lx): semi-stub!\n",
967 dwFlags,*recipients,uMessage,wParam,lParam);
968 PostMessageW(HWND_BROADCAST,uMessage,wParam,lParam);
969 return 1;
971 else
973 FIXME("(%08lx,%08lx,%08x,%08x,%08lx): stub!\n",
974 dwFlags,*recipients,uMessage,wParam,lParam);
975 return -1;