2 * Window messaging support
4 * Copyright 2001 Alexandre Julliard
13 #include "wine/unicode.h"
14 #include "wine/server.h"
22 #include "debugtools.h"
24 DEFAULT_DEBUG_CHANNEL(msg);
26 #define WM_NCMOUSEFIRST WM_NCMOUSEMOVE
27 #define WM_NCMOUSELAST WM_NCMBUTTONDBLCLK
29 #define MAX_PACK_COUNT 4
31 /* description of the data fields that need to be packed along with a sent message */
35 const void *data[MAX_PACK_COUNT];
36 size_t size[MAX_PACK_COUNT];
39 /* info about the message currently being received by the current thread */
40 struct received_message_info
42 enum message_type type;
44 UINT flags; /* InSendMessageEx return flags */
47 /* structure to group all parameters for sent messages of the various kinds */
48 struct send_message_info
50 enum message_type type;
55 UINT flags; /* flags for SendMessageTimeout */
56 UINT timeout; /* timeout for SendMessageTimeout */
57 SENDASYNCPROC callback; /* callback function for SendMessageCallback */
58 ULONG_PTR data; /* callback data */
62 /* flag for messages that contain pointers */
63 /* 32 messages per entry, messages 0..31 map to bits 0..31 */
65 #define SET(msg) (1 << ((msg) & 31))
67 static const unsigned int message_pointer_flags[] =
70 SET(WM_CREATE) | SET(WM_SETTEXT) | SET(WM_GETTEXT) |
71 SET(WM_WININICHANGE) | SET(WM_DEVMODECHANGE),
73 SET(WM_GETMINMAXINFO) | SET(WM_DRAWITEM) | SET(WM_MEASUREITEM) | SET(WM_DELETEITEM) |
76 SET(WM_WINDOWPOSCHANGING) | SET(WM_WINDOWPOSCHANGED) | SET(WM_COPYDATA) |
77 SET(WM_NOTIFY) | SET(WM_HELP),
79 SET(WM_STYLECHANGING) | SET(WM_STYLECHANGED),
81 SET(WM_NCCREATE) | SET(WM_NCCALCSIZE) | SET(WM_GETDLGCODE),
83 SET(EM_GETSEL) | SET(EM_GETRECT) | SET(EM_SETRECT) | SET(EM_SETRECTNP),
85 SET(EM_REPLACESEL) | SET(EM_GETLINE) | SET(EM_SETTABSTOPS),
87 SET(SBM_GETRANGE) | SET(SBM_SETSCROLLINFO) | SET(SBM_GETSCROLLINFO),
93 SET(CB_GETEDITSEL) | SET(CB_ADDSTRING) | SET(CB_DIR) | SET(CB_GETLBTEXT) |
94 SET(CB_INSERTSTRING) | SET(CB_FINDSTRING) | SET(CB_SELECTSTRING) |
95 SET(CB_GETDROPPEDCONTROLRECT) | SET(CB_FINDSTRINGEXACT),
99 SET(LB_ADDSTRING) | SET(LB_INSERTSTRING) | SET(LB_GETTEXT) | SET(LB_SELECTSTRING) |
100 SET(LB_DIR) | SET(LB_FINDSTRING) |
101 SET(LB_GETSELITEMS) | SET(LB_SETTABSTOPS) | SET(LB_ADDFILE) | SET(LB_GETITEMRECT),
103 SET(LB_FINDSTRINGEXACT),
109 SET(WM_NEXTMENU) | SET(WM_SIZING) | SET(WM_MOVING) | SET(WM_DEVICECHANGE),
111 SET(WM_MDICREATE) | SET(WM_MDIGETACTIVE) | SET(WM_DROPOBJECT) |
112 SET(WM_QUERYDROPOBJECT) | SET(WM_DRAGLOOP) | SET(WM_DRAGSELECT) | SET(WM_DRAGMOVE),
126 SET(WM_ASKCBFORMATNAME)
129 /* flags for messages that contain Unicode strings */
130 static const unsigned int message_unicode_flags[] =
133 SET(WM_CREATE) | SET(WM_SETTEXT) | SET(WM_GETTEXT) | SET(WM_GETTEXTLENGTH) |
134 SET(WM_WININICHANGE) | SET(WM_DEVMODECHANGE),
146 SET(EM_REPLACESEL) | SET(EM_GETLINE) | SET(EM_SETPASSWORDCHAR),
150 SET(WM_CHAR) | SET(WM_DEADCHAR) | SET(WM_SYSCHAR) | SET(WM_SYSDEADCHAR),
154 SET(CB_ADDSTRING) | SET(CB_DIR) | SET(CB_GETLBTEXT) | SET(CB_GETLBTEXTLEN) |
155 SET(CB_INSERTSTRING) | SET(CB_FINDSTRING) | SET(CB_SELECTSTRING) | SET(CB_FINDSTRINGEXACT),
159 SET(LB_ADDSTRING) | SET(LB_INSERTSTRING) | SET(LB_GETTEXT) | SET(LB_GETTEXTLEN) |
160 SET(LB_SELECTSTRING) | SET(LB_DIR) | SET(LB_FINDSTRING) | SET(LB_ADDFILE),
162 SET(LB_FINDSTRINGEXACT),
184 SET(WM_PAINTCLIPBOARD) | SET(WM_SIZECLIPBOARD) | SET(WM_ASKCBFORMATNAME)
187 /* check whether a given message type includes pointers */
188 inline static int is_pointer_message( UINT message )
190 if (message >= 8*sizeof(message_pointer_flags)) return FALSE;
191 return (message_pointer_flags[message / 32] & SET(message)) != 0;
194 /* check whether a given message type contains Unicode (or ASCII) chars */
195 inline static int is_unicode_message( UINT message )
197 if (message >= 8*sizeof(message_unicode_flags)) return FALSE;
198 return (message_unicode_flags[message / 32] & SET(message)) != 0;
203 /* add a data field to a packed message */
204 inline static void push_data( struct packed_message *data, const void *ptr, size_t size )
206 data->data[data->count] = ptr;
207 data->size[data->count] = size;
211 /* add a string to a packed message */
212 inline static void push_string( struct packed_message *data, LPCWSTR str )
214 push_data( data, str, (strlenW(str) + 1) * sizeof(WCHAR) );
217 /* retrieve a pointer to data from a packed message and increment the buffer pointer */
218 inline static void *get_data( void **buffer, size_t size )
221 *buffer = (char *)*buffer + size;
225 /* make sure that the buffer contains a valid null-terminated Unicode string */
226 inline static BOOL check_string( LPCWSTR str, size_t size )
228 for (size /= sizeof(WCHAR); size; size--, str++)
229 if (!*str) return TRUE;
233 /* make sure that there is space for 'size' bytes in buffer, growing it if needed */
234 inline static void *get_buffer_space( void **buffer, size_t size )
237 if (!(ret = HeapReAlloc( GetProcessHeap(), 0, *buffer, size )))
238 HeapFree( GetProcessHeap(), 0, *buffer );
243 /* retrieve a string pointer from packed data */
244 inline static LPWSTR get_string( void **buffer )
246 return get_data( buffer, (strlenW( (LPWSTR)*buffer ) + 1) * sizeof(WCHAR) );
249 /* check whether a combobox expects strings or ids in CB_ADDSTRING/CB_INSERTSTRING */
250 inline static BOOL combobox_has_strings( HWND hwnd )
252 DWORD style = GetWindowLongA( hwnd, GWL_STYLE );
253 return (!(style & (CBS_OWNERDRAWFIXED | CBS_OWNERDRAWVARIABLE)) || (style & CBS_HASSTRINGS));
256 /* check whether a listbox expects strings or ids in LB_ADDSTRING/LB_INSERTSTRING */
257 inline static BOOL listbox_has_strings( HWND hwnd )
259 DWORD style = GetWindowLongA( hwnd, GWL_STYLE );
260 return (!(style & (LBS_OWNERDRAWFIXED | LBS_OWNERDRAWVARIABLE)) || (style & LBS_HASSTRINGS));
263 /* check if hwnd is a broadcast magic handle */
264 inline static BOOL is_broadcast( HWND hwnd )
266 return (hwnd == HWND_BROADCAST || hwnd == HWND_TOPMOST);
270 /***********************************************************************
271 * broadcast_message_callback
273 * Helper callback for broadcasting messages.
275 static BOOL CALLBACK broadcast_message_callback( HWND hwnd, LPARAM lparam )
277 struct send_message_info *info = (struct send_message_info *)lparam;
278 if (!(GetWindowLongW( hwnd, GWL_STYLE ) & (WS_POPUP|WS_CAPTION))) return TRUE;
282 SendMessageTimeoutW( hwnd, info->msg, info->wparam, info->lparam,
283 info->flags, info->timeout, NULL );
286 SendMessageTimeoutA( hwnd, info->msg, info->wparam, info->lparam,
287 info->flags, info->timeout, NULL );
290 SendNotifyMessageW( hwnd, info->msg, info->wparam, info->lparam );
293 SendMessageCallbackW( hwnd, info->msg, info->wparam, info->lparam,
294 info->callback, info->data );
297 PostMessageW( hwnd, info->msg, info->wparam, info->lparam );
300 ERR( "bad type %d\n", info->type );
307 /***********************************************************************
310 * Convert the wparam of an ASCII message to Unicode.
312 static WPARAM map_wparam_AtoW( UINT message, WPARAM wparam )
314 if (message == WM_CHARTOITEM ||
315 message == EM_SETPASSWORDCHAR ||
316 message == WM_CHAR ||
317 message == WM_DEADCHAR ||
318 message == WM_SYSCHAR ||
319 message == WM_SYSDEADCHAR ||
320 message == WM_MENUCHAR)
322 char ch = LOWORD(wparam);
324 MultiByteToWideChar(CP_ACP, 0, &ch, 1, &wch, 1);
325 wparam = MAKEWPARAM( wch, HIWORD(wparam) );
331 /***********************************************************************
334 * Convert the wparam of a Unicode message to ASCII.
336 static WPARAM map_wparam_WtoA( UINT message, WPARAM wparam )
338 if (message == WM_CHARTOITEM ||
339 message == EM_SETPASSWORDCHAR ||
340 message == WM_CHAR ||
341 message == WM_DEADCHAR ||
342 message == WM_SYSCHAR ||
343 message == WM_SYSDEADCHAR ||
344 message == WM_MENUCHAR)
346 WCHAR wch = LOWORD(wparam);
348 WideCharToMultiByte( CP_ACP, 0, &wch, 1, &ch, 1, NULL, NULL );
349 wparam = MAKEWPARAM( (unsigned char)ch, HIWORD(wparam) );
355 /***********************************************************************
358 * Pack a message for sending to another process.
359 * Return the size of the data we expect in the message reply.
360 * Set data->count to -1 if there is an error.
362 static size_t pack_message( HWND hwnd, UINT message, WPARAM wparam, LPARAM lparam,
363 struct packed_message *data )
371 CREATESTRUCTW *cs = (CREATESTRUCTW *)lparam;
372 push_data( data, cs, sizeof(*cs) );
373 if (HIWORD(cs->lpszName)) push_string( data, cs->lpszName );
374 if (HIWORD(cs->lpszClass)) push_string( data, cs->lpszClass );
378 case WM_ASKCBFORMATNAME:
379 return wparam * sizeof(WCHAR);
381 case WM_WININICHANGE:
382 case WM_DEVMODECHANGE:
385 case CB_FINDSTRINGEXACT:
386 case CB_SELECTSTRING:
390 case LB_FINDSTRINGEXACT:
391 case LB_SELECTSTRING:
393 push_string( data, (LPWSTR)lparam );
395 case WM_GETMINMAXINFO:
396 push_data( data, (MINMAXINFO *)lparam, sizeof(MINMAXINFO) );
397 return sizeof(MINMAXINFO);
399 push_data( data, (DRAWITEMSTRUCT *)lparam, sizeof(DRAWITEMSTRUCT) );
402 push_data( data, (MEASUREITEMSTRUCT *)lparam, sizeof(MEASUREITEMSTRUCT) );
403 return sizeof(MEASUREITEMSTRUCT);
405 push_data( data, (DELETEITEMSTRUCT *)lparam, sizeof(DELETEITEMSTRUCT) );
408 push_data( data, (COMPAREITEMSTRUCT *)lparam, sizeof(COMPAREITEMSTRUCT) );
410 case WM_WINDOWPOSCHANGING:
411 case WM_WINDOWPOSCHANGED:
412 push_data( data, (WINDOWPOS *)lparam, sizeof(WINDOWPOS) );
413 return sizeof(WINDOWPOS);
416 COPYDATASTRUCT *cp = (COPYDATASTRUCT *)lparam;
417 push_data( data, cp, sizeof(*cp) );
418 if (cp->lpData) push_data( data, cp->lpData, cp->cbData );
422 /* WM_NOTIFY cannot be sent across processes (MSDN) */
426 push_data( data, (HELPINFO *)lparam, sizeof(HELPINFO) );
428 case WM_STYLECHANGING:
429 case WM_STYLECHANGED:
430 push_data( data, (STYLESTRUCT *)lparam, sizeof(STYLESTRUCT) );
435 push_data( data, (RECT *)lparam, sizeof(RECT) );
440 NCCALCSIZE_PARAMS *nc = (NCCALCSIZE_PARAMS *)lparam;
441 push_data( data, nc, sizeof(*nc) );
442 push_data( data, nc->lppos, sizeof(*nc->lppos) );
443 return sizeof(*nc) + sizeof(*nc->lppos);
446 if (lparam) push_data( data, (MSG *)lparam, sizeof(MSG) );
448 case SBM_SETSCROLLINFO:
449 push_data( data, (SCROLLINFO *)lparam, sizeof(SCROLLINFO) );
451 case SBM_GETSCROLLINFO:
452 push_data( data, (SCROLLINFO *)lparam, sizeof(SCROLLINFO) );
453 return sizeof(SCROLLINFO);
459 if (wparam) size += sizeof(DWORD);
460 if (lparam) size += sizeof(DWORD);
465 case CB_GETDROPPEDCONTROLRECT:
469 push_data( data, (RECT *)lparam, sizeof(RECT) );
473 WORD *pw = (WORD *)lparam;
474 push_data( data, pw, sizeof(*pw) );
475 return *pw * sizeof(WCHAR);
479 if (wparam) push_data( data, (UINT *)lparam, sizeof(UINT) * wparam );
482 case CB_INSERTSTRING:
483 if (combobox_has_strings( hwnd )) push_string( data, (LPWSTR)lparam );
486 if (!combobox_has_strings( hwnd )) return sizeof(ULONG_PTR);
487 return (SendMessageW( hwnd, CB_GETLBTEXTLEN, wparam, 0 ) + 1) * sizeof(WCHAR);
489 case LB_INSERTSTRING:
490 if (listbox_has_strings( hwnd )) push_string( data, (LPWSTR)lparam );
493 if (!listbox_has_strings( hwnd )) return sizeof(ULONG_PTR);
494 return (SendMessageW( hwnd, LB_GETTEXTLEN, wparam, 0 ) + 1) * sizeof(WCHAR);
496 return wparam * sizeof(UINT);
498 push_data( data, (MDINEXTMENU *)lparam, sizeof(MDINEXTMENU) );
499 return sizeof(MDINEXTMENU);
502 push_data( data, (RECT *)lparam, sizeof(RECT) );
506 MDICREATESTRUCTW *cs = (MDICREATESTRUCTW *)lparam;
507 push_data( data, cs, sizeof(*cs) );
508 if (HIWORD(cs->szTitle)) push_string( data, cs->szTitle );
509 if (HIWORD(cs->szClass)) push_string( data, cs->szClass );
512 case WM_MDIGETACTIVE:
513 if (lparam) return sizeof(BOOL);
516 case WM_WINE_SETWINDOWPOS:
517 push_data( data, (WINDOWPOS *)lparam, sizeof(WINDOWPOS) );
520 /* these contain an HFONT */
523 /* these contain an HDC */
526 case WM_ICONERASEBKGND:
528 case WM_CTLCOLORMSGBOX:
529 case WM_CTLCOLOREDIT:
530 case WM_CTLCOLORLISTBOX:
533 case WM_CTLCOLORSCROLLBAR:
534 case WM_CTLCOLORSTATIC:
537 /* these contain an HGLOBAL */
538 case WM_PAINTCLIPBOARD:
539 case WM_SIZECLIPBOARD:
540 case WM_DDE_INITIATE:
542 case WM_DDE_UNADVISE:
547 /* these contain pointers */
549 case WM_QUERYDROPOBJECT:
553 case WM_DEVICECHANGE:
554 FIXME( "msg %x (%s) not supported yet\n", message, SPY_GetMsgName(message, hwnd) );
562 /***********************************************************************
565 * Unpack a message received from another process.
567 static BOOL unpack_message( HWND hwnd, UINT message, WPARAM *wparam, LPARAM *lparam,
568 void **buffer, size_t size )
577 CREATESTRUCTW *cs = *buffer;
578 WCHAR *str = (WCHAR *)(cs + 1);
579 if (size < sizeof(*cs)) return FALSE;
581 if (HIWORD(cs->lpszName))
583 if (!check_string( str, size )) return FALSE;
585 size -= (strlenW(str) + 1) * sizeof(WCHAR);
586 str += strlenW(str) + 1;
588 if (HIWORD(cs->lpszClass))
590 if (!check_string( str, size )) return FALSE;
596 case WM_ASKCBFORMATNAME:
597 if (!get_buffer_space( buffer, (*wparam * sizeof(WCHAR)) )) return FALSE;
600 case WM_WININICHANGE:
601 case WM_DEVMODECHANGE:
604 case CB_FINDSTRINGEXACT:
605 case CB_SELECTSTRING:
609 case LB_FINDSTRINGEXACT:
610 case LB_SELECTSTRING:
612 if (!check_string( *buffer, size )) return FALSE;
614 case WM_GETMINMAXINFO:
615 minsize = sizeof(MINMAXINFO);
618 minsize = sizeof(DRAWITEMSTRUCT);
621 minsize = sizeof(MEASUREITEMSTRUCT);
624 minsize = sizeof(DELETEITEMSTRUCT);
627 minsize = sizeof(COMPAREITEMSTRUCT);
629 case WM_WINDOWPOSCHANGING:
630 case WM_WINDOWPOSCHANGED:
631 case WM_WINE_SETWINDOWPOS:
632 minsize = sizeof(WINDOWPOS);
636 COPYDATASTRUCT *cp = *buffer;
637 if (size < sizeof(*cp)) return FALSE;
640 minsize = sizeof(*cp) + cp->cbData;
646 /* WM_NOTIFY cannot be sent across processes (MSDN) */
649 minsize = sizeof(HELPINFO);
651 case WM_STYLECHANGING:
652 case WM_STYLECHANGED:
653 minsize = sizeof(STYLESTRUCT);
656 if (!*wparam) minsize = sizeof(RECT);
659 NCCALCSIZE_PARAMS *nc = *buffer;
660 if (size < sizeof(*nc) + sizeof(*nc->lppos)) return FALSE;
661 nc->lppos = (WINDOWPOS *)(nc + 1);
665 if (!*lparam) return TRUE;
666 minsize = sizeof(MSG);
668 case SBM_SETSCROLLINFO:
669 minsize = sizeof(SCROLLINFO);
671 case SBM_GETSCROLLINFO:
672 if (!get_buffer_space( buffer, sizeof(SCROLLINFO ))) return FALSE;
677 if (*wparam || *lparam)
679 if (!get_buffer_space( buffer, 2*sizeof(DWORD) )) return FALSE;
680 if (*wparam) *wparam = (WPARAM)*buffer;
681 if (*lparam) *lparam = (LPARAM)((DWORD *)*buffer + 1);
686 case CB_GETDROPPEDCONTROLRECT:
687 if (!get_buffer_space( buffer, sizeof(RECT) )) return FALSE;
691 minsize = sizeof(RECT);
696 if (size < sizeof(WORD)) return FALSE;
697 len = *(WORD *)*buffer;
698 if (!get_buffer_space( buffer, (len + 1) * sizeof(WCHAR) )) return FALSE;
699 *lparam = (LPARAM)*buffer + sizeof(WORD); /* don't erase WORD at start of buffer */
704 if (!*wparam) return TRUE;
705 minsize = *wparam * sizeof(UINT);
708 case CB_INSERTSTRING:
710 case LB_INSERTSTRING:
711 if (!*buffer) return TRUE;
712 if (!check_string( *buffer, size )) return FALSE;
716 size = sizeof(ULONG_PTR);
717 if (combobox_has_strings( hwnd ))
718 size = (SendMessageW( hwnd, CB_GETLBTEXTLEN, *wparam, 0 ) + 1) * sizeof(WCHAR);
719 if (!get_buffer_space( buffer, size )) return FALSE;
724 size = sizeof(ULONG_PTR);
725 if (listbox_has_strings( hwnd ))
726 size = (SendMessageW( hwnd, LB_GETTEXTLEN, *wparam, 0 ) + 1) * sizeof(WCHAR);
727 if (!get_buffer_space( buffer, size )) return FALSE;
731 if (!get_buffer_space( buffer, *wparam * sizeof(UINT) )) return FALSE;
734 minsize = sizeof(MDINEXTMENU);
735 if (!get_buffer_space( buffer, sizeof(MDINEXTMENU) )) return FALSE;
739 minsize = sizeof(RECT);
740 if (!get_buffer_space( buffer, sizeof(RECT) )) return FALSE;
744 MDICREATESTRUCTW *cs = *buffer;
745 WCHAR *str = (WCHAR *)(cs + 1);
746 if (size < sizeof(*cs)) return FALSE;
748 if (HIWORD(cs->szTitle))
750 if (!check_string( str, size )) return FALSE;
752 size -= (strlenW(str) + 1) * sizeof(WCHAR);
753 str += strlenW(str) + 1;
755 if (HIWORD(cs->szClass))
757 if (!check_string( str, size )) return FALSE;
762 case WM_MDIGETACTIVE:
763 if (!*lparam) return TRUE;
764 if (!get_buffer_space( buffer, sizeof(BOOL) )) return FALSE;
767 /* these contain an HFONT */
770 /* these contain an HDC */
773 case WM_ICONERASEBKGND:
775 case WM_CTLCOLORMSGBOX:
776 case WM_CTLCOLOREDIT:
777 case WM_CTLCOLORLISTBOX:
780 case WM_CTLCOLORSCROLLBAR:
781 case WM_CTLCOLORSTATIC:
784 /* these contain an HGLOBAL */
785 case WM_PAINTCLIPBOARD:
786 case WM_SIZECLIPBOARD:
787 case WM_DDE_INITIATE:
789 case WM_DDE_UNADVISE:
794 /* these contain pointers */
796 case WM_QUERYDROPOBJECT:
800 case WM_DEVICECHANGE:
801 FIXME( "msg %x (%s) not supported yet\n", message, SPY_GetMsgName(message, hwnd) );
805 return TRUE; /* message doesn't need any unpacking */
808 /* default exit for most messages: check minsize and store buffer in lparam */
809 if (size < minsize) return FALSE;
810 *lparam = (LPARAM)*buffer;
815 /***********************************************************************
818 * Pack a reply to a message for sending to another process.
820 static void pack_reply( HWND hwnd, UINT message, WPARAM wparam, LPARAM lparam,
821 LRESULT res, struct packed_message *data )
828 push_data( data, (CREATESTRUCTW *)lparam, sizeof(CREATESTRUCTW) );
833 push_data( data, (WCHAR *)lparam, (res + 1) * sizeof(WCHAR) );
835 case WM_GETMINMAXINFO:
836 push_data( data, (MINMAXINFO *)lparam, sizeof(MINMAXINFO) );
839 push_data( data, (MEASUREITEMSTRUCT *)lparam, sizeof(MEASUREITEMSTRUCT) );
841 case WM_WINDOWPOSCHANGING:
842 case WM_WINDOWPOSCHANGED:
843 push_data( data, (WINDOWPOS *)lparam, sizeof(WINDOWPOS) );
846 if (lparam) push_data( data, (MSG *)lparam, sizeof(MSG) );
848 case SBM_GETSCROLLINFO:
849 push_data( data, (SCROLLINFO *)lparam, sizeof(SCROLLINFO) );
853 case CB_GETDROPPEDCONTROLRECT:
856 push_data( data, (RECT *)lparam, sizeof(RECT) );
860 WORD *ptr = (WORD *)lparam;
861 push_data( data, ptr, ptr[-1] * sizeof(WCHAR) );
865 push_data( data, (UINT *)lparam, wparam * sizeof(UINT) );
867 case WM_MDIGETACTIVE:
868 if (lparam) push_data( data, (BOOL *)lparam, sizeof(BOOL) );
872 push_data( data, (RECT *)lparam, sizeof(RECT) );
875 NCCALCSIZE_PARAMS *nc = (NCCALCSIZE_PARAMS *)lparam;
876 push_data( data, nc, sizeof(*nc) );
877 push_data( data, nc->lppos, sizeof(*nc->lppos) );
883 if (wparam) push_data( data, (DWORD *)wparam, sizeof(DWORD) );
884 if (lparam) push_data( data, (DWORD *)lparam, sizeof(DWORD) );
887 push_data( data, (MDINEXTMENU *)lparam, sizeof(MDINEXTMENU) );
890 push_data( data, (MDICREATESTRUCTW *)lparam, sizeof(MDICREATESTRUCTW) );
892 case WM_ASKCBFORMATNAME:
893 push_data( data, (WCHAR *)lparam, (strlenW((WCHAR *)lparam) + 1) * sizeof(WCHAR) );
899 /***********************************************************************
902 * Unpack a message reply received from another process.
904 static void unpack_reply( HWND hwnd, UINT message, WPARAM wparam, LPARAM lparam,
905 void *buffer, size_t size )
912 CREATESTRUCTW *cs = (CREATESTRUCTW *)lparam;
913 LPCWSTR name = cs->lpszName, class = cs->lpszClass;
914 memcpy( cs, buffer, min( sizeof(*cs), size ));
915 cs->lpszName = name; /* restore the original pointers */
916 cs->lpszClass = class;
920 case WM_ASKCBFORMATNAME:
921 memcpy( (WCHAR *)lparam, buffer, min( wparam*sizeof(WCHAR), size ));
923 case WM_GETMINMAXINFO:
924 memcpy( (MINMAXINFO *)lparam, buffer, min( sizeof(MINMAXINFO), size ));
927 memcpy( (MEASUREITEMSTRUCT *)lparam, buffer, min( sizeof(MEASUREITEMSTRUCT), size ));
929 case WM_WINDOWPOSCHANGING:
930 case WM_WINDOWPOSCHANGED:
931 memcpy( (WINDOWPOS *)lparam, buffer, min( sizeof(WINDOWPOS), size ));
934 if (lparam) memcpy( (MSG *)lparam, buffer, min( sizeof(MSG), size ));
936 case SBM_GETSCROLLINFO:
937 memcpy( (SCROLLINFO *)lparam, buffer, min( sizeof(SCROLLINFO), size ));
940 case CB_GETDROPPEDCONTROLRECT:
944 memcpy( (RECT *)lparam, buffer, min( sizeof(RECT), size ));
947 size = min( size, *(WORD *)lparam );
948 memcpy( (WCHAR *)lparam, buffer, size );
951 memcpy( (UINT *)lparam, buffer, min( wparam*sizeof(UINT), size ));
955 memcpy( (WCHAR *)lparam, buffer, size );
958 memcpy( (MDINEXTMENU *)lparam, buffer, min( sizeof(MDINEXTMENU), size ));
960 case WM_MDIGETACTIVE:
961 if (lparam) memcpy( (BOOL *)lparam, buffer, min( sizeof(BOOL), size ));
965 memcpy( (RECT *)lparam, buffer, min( sizeof(RECT), size ));
968 NCCALCSIZE_PARAMS *nc = (NCCALCSIZE_PARAMS *)lparam;
969 WINDOWPOS *wp = nc->lppos;
970 memcpy( nc, buffer, min( sizeof(*nc), size ));
971 if (size > sizeof(*nc))
974 memcpy( wp, (NCCALCSIZE_PARAMS*)buffer + 1, min( sizeof(*wp), size ));
976 nc->lppos = wp; /* restore the original pointer */
984 memcpy( (DWORD *)wparam, buffer, min( sizeof(DWORD), size ));
985 if (size <= sizeof(DWORD)) break;
986 size -= sizeof(DWORD);
987 buffer = (DWORD *)buffer + 1;
989 if (lparam) memcpy( (DWORD *)lparam, buffer, min( sizeof(DWORD), size ));
993 MDICREATESTRUCTW *cs = (MDICREATESTRUCTW *)lparam;
994 LPCWSTR title = cs->szTitle, class = cs->szClass;
995 memcpy( cs, buffer, min( sizeof(*cs), size ));
996 cs->szTitle = title; /* restore the original pointers */
1001 ERR( "should not happen: unexpected message %x\n", message );
1007 /***********************************************************************
1010 * Send a reply to a sent message.
1012 static void reply_message( struct received_message_info *info, LRESULT result, BOOL remove )
1014 struct packed_message data;
1015 int i, replied = info->flags & ISMEX_REPLIED;
1017 if (info->flags & ISMEX_NOTIFY) return; /* notify messages don't get replies */
1018 if (!remove && replied) return; /* replied already */
1021 info->flags |= ISMEX_REPLIED;
1023 if (info->type == MSG_OTHER_PROCESS && !replied)
1025 pack_reply( info->msg.hwnd, info->msg.message, info->msg.wParam,
1026 info->msg.lParam, result, &data );
1029 SERVER_START_REQ( reply_message )
1031 req->result = result;
1032 req->remove = remove;
1033 for (i = 0; i < data.count; i++) wine_server_add_data( req, data.data[i], data.size[i] );
1034 wine_server_call( req );
1040 /***********************************************************************
1041 * handle_internal_message
1043 * Handle an internal Wine message instead of calling the window proc.
1045 static LRESULT handle_internal_message( HWND hwnd, UINT msg, WPARAM wparam, LPARAM lparam )
1047 if (hwnd == GetDesktopWindow()) return 0;
1050 case WM_WINE_DESTROYWINDOW:
1051 return WIN_DestroyWindow( hwnd );
1052 case WM_WINE_SETWINDOWPOS:
1053 return USER_Driver.pSetWindowPos( (WINDOWPOS *)lparam );
1054 case WM_WINE_SHOWWINDOW:
1055 return ShowWindow( hwnd, wparam );
1056 case WM_WINE_SETPARENT:
1057 return (LRESULT)SetParent( hwnd, (HWND)wparam );
1058 case WM_WINE_SETWINDOWLONG:
1059 return (LRESULT)SetWindowLongW( hwnd, wparam, lparam );
1060 case WM_WINE_ENABLEWINDOW:
1061 return EnableWindow( hwnd, wparam );
1063 FIXME( "unknown internal message %x\n", msg );
1069 /***********************************************************************
1072 * Call a window procedure and the corresponding hooks.
1074 static LRESULT call_window_proc( HWND hwnd, UINT msg, WPARAM wparam, LPARAM lparam, BOOL unicode )
1079 if (msg & 0x80000000) return handle_internal_message( hwnd, msg, wparam, lparam );
1081 /* first the WH_CALLWNDPROC hook */
1082 if (HOOK_IsHooked( WH_CALLWNDPROC ))
1085 cwp.lParam = lparam;
1086 cwp.wParam = wparam;
1088 cwp.hwnd = WIN_GetFullHandle( hwnd );
1089 if (unicode) HOOK_CallHooksW( WH_CALLWNDPROC, HC_ACTION, 1, (LPARAM)&cwp );
1090 else HOOK_CallHooksA( WH_CALLWNDPROC, HC_ACTION, 1, (LPARAM)&cwp );
1091 lparam = cwp.lParam;
1092 wparam = cwp.wParam;
1097 /* now call the window procedure */
1100 if (!(winproc = (WNDPROC)GetWindowLongW( hwnd, GWL_WNDPROC ))) return 0;
1101 result = CallWindowProcW( winproc, hwnd, msg, wparam, lparam );
1105 if (!(winproc = (WNDPROC)GetWindowLongA( hwnd, GWL_WNDPROC ))) return 0;
1106 result = CallWindowProcA( winproc, hwnd, msg, wparam, lparam );
1109 /* and finally the WH_CALLWNDPROCRET hook */
1110 if (HOOK_IsHooked( WH_CALLWNDPROCRET ))
1113 cwp.lResult = result;
1114 cwp.lParam = lparam;
1115 cwp.wParam = wparam;
1117 cwp.hwnd = WIN_GetFullHandle( hwnd );
1118 if (unicode) HOOK_CallHooksW( WH_CALLWNDPROCRET, HC_ACTION, 1, (LPARAM)&cwp );
1119 else HOOK_CallHooksA( WH_CALLWNDPROCRET, HC_ACTION, 1, (LPARAM)&cwp );
1125 /***********************************************************************
1128 * Peek for a message matching the given parameters. Return FALSE if none available.
1129 * All pending sent messages are processed before returning.
1131 BOOL MSG_peek_message( MSG *msg, HWND hwnd, UINT first, UINT last, int flags )
1134 ULONG_PTR extra_info = 0;
1135 MESSAGEQUEUE *queue = QUEUE_Current();
1136 struct received_message_info info, *old_info;
1138 if (!first && !last) last = ~0;
1143 void *buffer = NULL;
1144 size_t size = 0, buffer_size = 0;
1146 do /* loop while buffer is too small */
1148 if (buffer_size && !(buffer = HeapAlloc( GetProcessHeap(), 0, buffer_size )))
1150 SERVER_START_REQ( get_message )
1153 req->get_win = hwnd;
1154 req->get_first = first;
1155 req->get_last = last;
1156 if (buffer_size) wine_server_set_reply( req, buffer, buffer_size );
1157 if (!(res = wine_server_call( req )))
1159 size = wine_server_reply_size( reply );
1160 info.type = reply->type;
1161 info.msg.hwnd = reply->win;
1162 info.msg.message = reply->msg;
1163 info.msg.wParam = reply->wparam;
1164 info.msg.lParam = reply->lparam;
1165 info.msg.time = reply->time;
1166 info.msg.pt.x = reply->x;
1167 info.msg.pt.y = reply->y;
1168 extra_info = reply->info;
1172 if (buffer) HeapFree( GetProcessHeap(), 0, buffer );
1173 buffer_size = reply->total;
1177 } while (res == STATUS_BUFFER_OVERFLOW);
1179 if (res) return FALSE;
1181 TRACE( "got type %d msg %x hwnd %x wp %x lp %lx\n",
1182 info.type, info.msg.message, info.msg.hwnd, info.msg.wParam, info.msg.lParam );
1188 info.flags = ISMEX_SEND;
1191 info.flags = ISMEX_NOTIFY;
1194 info.flags = ISMEX_CALLBACK;
1196 case MSG_OTHER_PROCESS:
1197 info.flags = ISMEX_SEND;
1198 if (!unpack_message( info.msg.hwnd, info.msg.message, &info.msg.wParam,
1199 &info.msg.lParam, &buffer, size ))
1201 ERR( "invalid packed message %x (%s) hwnd %x wp %x lp %lx size %d\n",
1202 info.msg.message, SPY_GetMsgName(info.msg.message, info.msg.hwnd), info.msg.hwnd,
1203 info.msg.wParam, info.msg.lParam, size );
1205 reply_message( &info, 0, TRUE );
1209 case MSG_HARDWARE_RAW:
1210 if (!MSG_process_raw_hardware_message( &info.msg, extra_info,
1211 hwnd, first, last, flags & GET_MSG_REMOVE ))
1214 case MSG_HARDWARE_COOKED:
1215 if (!MSG_process_cooked_hardware_message( &info.msg, extra_info,
1216 flags & GET_MSG_REMOVE ))
1218 flags |= GET_MSG_REMOVE_LAST;
1221 queue->GetMessagePosVal = MAKELONG( info.msg.pt.x, info.msg.pt.y );
1224 queue->GetMessageExtraInfoVal = extra_info;
1226 if (buffer) HeapFree( GetProcessHeap(), 0, buffer );
1230 /* if we get here, we have a sent message; call the window procedure */
1231 old_info = queue->receive_info;
1232 queue->receive_info = &info;
1233 result = call_window_proc( info.msg.hwnd, info.msg.message, info.msg.wParam,
1234 info.msg.lParam, (info.type != MSG_ASCII) );
1235 reply_message( &info, result, TRUE );
1236 queue->receive_info = old_info;
1238 if (buffer) HeapFree( GetProcessHeap(), 0, buffer );
1243 /***********************************************************************
1244 * wait_message_reply
1246 * Wait until a sent message gets replied to.
1248 static void wait_message_reply( UINT flags )
1250 MESSAGEQUEUE *queue;
1252 if (!(queue = QUEUE_Current())) return;
1256 unsigned int wake_bits = 0, changed_bits = 0;
1259 SERVER_START_REQ( set_queue_mask )
1261 req->wake_mask = (flags & SMTO_BLOCK) ? 0 : QS_SENDMESSAGE;
1262 req->changed_mask = QS_SMRESULT | req->wake_mask;
1264 if (!wine_server_call( req ))
1266 wake_bits = reply->wake_bits;
1267 changed_bits = reply->changed_bits;
1272 if (changed_bits & QS_SMRESULT) return; /* got a result */
1273 if (wake_bits & QS_SENDMESSAGE)
1275 /* Process the sent message immediately */
1277 MSG_peek_message( &msg, 0, 0, 0, GET_MSG_REMOVE | GET_MSG_SENT_ONLY );
1281 /* now wait for it */
1283 ReleaseThunkLock( &dwlc );
1285 if (USER_Driver.pMsgWaitForMultipleObjectsEx)
1286 res = USER_Driver.pMsgWaitForMultipleObjectsEx( 1, &queue->server_queue,
1289 res = WaitForSingleObject( queue->server_queue, INFINITE );
1291 if (dwlc) RestoreThunkLock( dwlc );
1295 /***********************************************************************
1296 * put_message_in_queue
1298 * Put a sent message into the destination queue.
1299 * For inter-process message, reply_size is set to expected size of reply data.
1301 static BOOL put_message_in_queue( DWORD dest_tid, const struct send_message_info *info,
1302 size_t *reply_size )
1304 struct packed_message data;
1306 int i, timeout = -1;
1308 if (info->type != MSG_NOTIFY &&
1309 info->type != MSG_CALLBACK &&
1310 info->type != MSG_POSTED &&
1311 info->timeout != INFINITE)
1312 timeout = info->timeout;
1315 if (info->type == MSG_OTHER_PROCESS)
1317 *reply_size = pack_message( info->hwnd, info->msg, info->wparam, info->lparam, &data );
1318 if (data.count == -1)
1320 WARN( "cannot pack message %x\n", info->msg );
1325 SERVER_START_REQ( send_message )
1327 req->id = (void *)dest_tid;
1328 req->type = info->type;
1329 req->win = info->hwnd;
1330 req->msg = info->msg;
1331 req->wparam = info->wparam;
1332 req->lparam = info->lparam;
1333 req->time = GetCurrentTime();
1334 req->timeout = timeout;
1335 for (i = 0; i < data.count; i++) wine_server_add_data( req, data.data[i], data.size[i] );
1336 if ((res = wine_server_call( req )))
1338 if (res == STATUS_INVALID_PARAMETER)
1339 /* FIXME: find a STATUS_ value for this one */
1340 SetLastError( ERROR_INVALID_THREAD_ID );
1342 SetLastError( RtlNtStatusToDosError(res) );
1350 /***********************************************************************
1353 * Retrieve a message reply from the server.
1355 static LRESULT retrieve_reply( const struct send_message_info *info,
1356 size_t reply_size, LRESULT *result )
1359 void *reply_data = NULL;
1363 if (!(reply_data = HeapAlloc( GetProcessHeap(), 0, reply_size )))
1365 WARN( "no memory for reply %d bytes, will be truncated\n", reply_size );
1369 SERVER_START_REQ( get_message_reply )
1372 if (reply_size) wine_server_set_reply( req, reply_data, reply_size );
1373 if (!(status = wine_server_call( req ))) *result = reply->result;
1374 reply_size = wine_server_reply_size( reply );
1377 if (!status && reply_size)
1378 unpack_reply( info->hwnd, info->msg, info->wparam, info->lparam, reply_data, reply_size );
1380 if (reply_data) HeapFree( GetProcessHeap(), 0, reply_data );
1382 TRACE( "hwnd %x msg %x (%s) wp %x lp %lx got reply %lx (err=%ld)\n",
1383 info->hwnd, info->msg, SPY_GetMsgName(info->msg, info->hwnd), info->wparam,
1384 info->lparam, *result, status );
1386 if (!status) return 1;
1387 if (status == STATUS_TIMEOUT) SetLastError(0); /* timeout */
1388 else SetLastError( RtlNtStatusToDosError(status) );
1393 /***********************************************************************
1394 * send_inter_thread_message
1396 static LRESULT send_inter_thread_message( DWORD dest_tid, const struct send_message_info *info,
1401 size_t reply_size = 0;
1403 TRACE( "hwnd %x msg %x (%s) wp %x lp %lx\n",
1404 info->hwnd, info->msg, SPY_GetMsgName(info->msg, info->hwnd), info->wparam, info->lparam );
1406 if (!put_message_in_queue( dest_tid, info, &reply_size )) return 0;
1408 /* there's no reply to wait for on notify/callback messages */
1409 if (info->type == MSG_NOTIFY || info->type == MSG_CALLBACK) return 1;
1411 locks = WIN_SuspendWndsLock();
1413 wait_message_reply( info->flags );
1414 ret = retrieve_reply( info, reply_size, res_ptr );
1416 WIN_RestoreWndsLock( locks );
1421 /***********************************************************************
1422 * SendMessageTimeoutW (USER32.@)
1424 LRESULT WINAPI SendMessageTimeoutW( HWND hwnd, UINT msg, WPARAM wparam, LPARAM lparam,
1425 UINT flags, UINT timeout, LPDWORD res_ptr )
1427 struct send_message_info info;
1428 DWORD dest_tid, dest_pid;
1429 LRESULT ret, result;
1431 info.type = MSG_UNICODE;
1434 info.wparam = wparam;
1435 info.lparam = lparam;
1437 info.timeout = timeout;
1439 if (is_broadcast(hwnd))
1441 EnumWindows( broadcast_message_callback, (LPARAM)&info );
1442 if (res_ptr) *res_ptr = 1;
1446 if (!(dest_tid = GetWindowThreadProcessId( hwnd, &dest_pid ))) return 0;
1448 if (USER_IsExitingThread( dest_tid )) return 0;
1450 SPY_EnterMessage( SPY_SENDMESSAGE, hwnd, msg, wparam, lparam );
1452 if (dest_tid == GetCurrentThreadId())
1454 result = call_window_proc( hwnd, msg, wparam, lparam, TRUE );
1459 if (dest_pid != GetCurrentProcessId()) info.type = MSG_OTHER_PROCESS;
1460 ret = send_inter_thread_message( dest_tid, &info, &result );
1463 SPY_ExitMessage( SPY_RESULT_OK, hwnd, msg, result, wparam, lparam );
1464 if (ret && res_ptr) *res_ptr = result;
1469 /***********************************************************************
1470 * SendMessageTimeoutA (USER32.@)
1472 LRESULT WINAPI SendMessageTimeoutA( HWND hwnd, UINT msg, WPARAM wparam, LPARAM lparam,
1473 UINT flags, UINT timeout, LPDWORD res_ptr )
1475 struct send_message_info info;
1476 DWORD dest_tid, dest_pid;
1477 LRESULT ret, result;
1479 info.type = MSG_ASCII;
1482 info.wparam = wparam;
1483 info.lparam = lparam;
1485 info.timeout = timeout;
1487 if (is_broadcast(hwnd))
1489 EnumWindows( broadcast_message_callback, (LPARAM)&info );
1490 if (res_ptr) *res_ptr = 1;
1494 if (!(dest_tid = GetWindowThreadProcessId( hwnd, &dest_pid ))) return 0;
1496 if (USER_IsExitingThread( dest_tid )) return 0;
1498 SPY_EnterMessage( SPY_SENDMESSAGE, hwnd, msg, wparam, lparam );
1500 if (dest_tid == GetCurrentThreadId())
1502 result = call_window_proc( hwnd, msg, wparam, lparam, FALSE );
1505 else if (dest_pid == GetCurrentProcessId())
1507 ret = send_inter_thread_message( dest_tid, &info, &result );
1511 /* inter-process message: need to map to Unicode */
1512 info.type = MSG_OTHER_PROCESS;
1513 if (is_unicode_message( info.msg ))
1515 if (WINPROC_MapMsg32ATo32W( info.hwnd, info.msg, &info.wparam, &info.lparam ) == -1)
1517 ret = send_inter_thread_message( dest_tid, &info, &result );
1518 result = WINPROC_UnmapMsg32ATo32W( info.hwnd, info.msg, info.wparam,
1519 info.lparam, result );
1521 else ret = send_inter_thread_message( dest_tid, &info, &result );
1523 SPY_ExitMessage( SPY_RESULT_OK, hwnd, msg, result, wparam, lparam );
1524 if (ret && res_ptr) *res_ptr = result;
1529 /***********************************************************************
1530 * SendMessageW (USER32.@)
1532 LRESULT WINAPI SendMessageW( HWND hwnd, UINT msg, WPARAM wparam, LPARAM lparam )
1535 SendMessageTimeoutW( hwnd, msg, wparam, lparam, SMTO_NORMAL, INFINITE, &res );
1540 /***********************************************************************
1541 * SendMessageA (USER32.@)
1543 LRESULT WINAPI SendMessageA( HWND hwnd, UINT msg, WPARAM wparam, LPARAM lparam )
1546 SendMessageTimeoutA( hwnd, msg, wparam, lparam, SMTO_NORMAL, INFINITE, &res );
1551 /***********************************************************************
1552 * SendNotifyMessageA (USER32.@)
1554 BOOL WINAPI SendNotifyMessageA( HWND hwnd, UINT msg, WPARAM wparam, LPARAM lparam )
1556 return SendNotifyMessageW( hwnd, msg, map_wparam_AtoW( msg, wparam ), lparam );
1560 /***********************************************************************
1561 * SendNotifyMessageW (USER32.@)
1563 BOOL WINAPI SendNotifyMessageW( HWND hwnd, UINT msg, WPARAM wparam, LPARAM lparam )
1565 struct send_message_info info;
1569 if (is_pointer_message(msg))
1571 SetLastError(ERROR_INVALID_PARAMETER);
1575 info.type = MSG_NOTIFY;
1578 info.wparam = wparam;
1579 info.lparam = lparam;
1581 if (is_broadcast(hwnd))
1583 EnumWindows( broadcast_message_callback, (LPARAM)&info );
1587 if (!(dest_tid = GetWindowThreadProcessId( hwnd, NULL ))) return FALSE;
1589 if (USER_IsExitingThread( dest_tid )) return TRUE;
1591 if (dest_tid == GetCurrentThreadId())
1593 call_window_proc( hwnd, msg, wparam, lparam, TRUE );
1596 return send_inter_thread_message( dest_tid, &info, &result );
1600 /***********************************************************************
1601 * SendMessageCallbackA (USER32.@)
1603 BOOL WINAPI SendMessageCallbackA( HWND hwnd, UINT msg, WPARAM wparam, LPARAM lparam,
1604 SENDASYNCPROC callback, ULONG_PTR data )
1606 return SendMessageCallbackW( hwnd, msg, map_wparam_AtoW( msg, wparam ),
1607 lparam, callback, data );
1611 /***********************************************************************
1612 * SendMessageCallbackW (USER32.@)
1614 BOOL WINAPI SendMessageCallbackW( HWND hwnd, UINT msg, WPARAM wparam, LPARAM lparam,
1615 SENDASYNCPROC callback, ULONG_PTR data )
1617 struct send_message_info info;
1621 if (is_pointer_message(msg))
1623 SetLastError(ERROR_INVALID_PARAMETER);
1627 info.type = MSG_CALLBACK;
1630 info.wparam = wparam;
1631 info.lparam = lparam;
1632 info.callback = callback;
1635 if (is_broadcast(hwnd))
1637 EnumWindows( broadcast_message_callback, (LPARAM)&info );
1641 if (!(dest_tid = GetWindowThreadProcessId( hwnd, NULL ))) return FALSE;
1643 if (USER_IsExitingThread( dest_tid )) return TRUE;
1645 if (dest_tid == GetCurrentThreadId())
1647 result = call_window_proc( hwnd, msg, wparam, lparam, TRUE );
1648 callback( hwnd, msg, data, result );
1651 FIXME( "callback will not be called\n" );
1652 return send_inter_thread_message( dest_tid, &info, &result );
1656 /***********************************************************************
1657 * ReplyMessage (USER32.@)
1659 BOOL WINAPI ReplyMessage( LRESULT result )
1661 MESSAGEQUEUE *queue = QUEUE_Current();
1662 struct received_message_info *info = queue->receive_info;
1664 if (!info) return FALSE;
1665 reply_message( info, result, FALSE );
1670 /***********************************************************************
1671 * InSendMessage (USER32.@)
1673 BOOL WINAPI InSendMessage(void)
1675 return (InSendMessageEx(NULL) & (ISMEX_SEND|ISMEX_REPLIED)) == ISMEX_SEND;
1679 /***********************************************************************
1680 * InSendMessageEx (USER32.@)
1682 DWORD WINAPI InSendMessageEx( LPVOID reserved )
1684 MESSAGEQUEUE *queue = QUEUE_Current();
1685 struct received_message_info *info = queue->receive_info;
1687 if (info) return info->flags;
1688 return ISMEX_NOSEND;
1692 /***********************************************************************
1693 * PostMessageA (USER32.@)
1695 BOOL WINAPI PostMessageA( HWND hwnd, UINT msg, WPARAM wparam, LPARAM lparam )
1697 return PostMessageW( hwnd, msg, map_wparam_AtoW( msg, wparam ), lparam );
1701 /***********************************************************************
1702 * PostMessageW (USER32.@)
1704 BOOL WINAPI PostMessageW( HWND hwnd, UINT msg, WPARAM wparam, LPARAM lparam )
1706 struct send_message_info info;
1709 if (is_pointer_message( msg ))
1711 SetLastError( ERROR_INVALID_PARAMETER );
1715 info.type = MSG_POSTED;
1718 info.wparam = wparam;
1719 info.lparam = lparam;
1721 if (is_broadcast(hwnd))
1723 EnumWindows( broadcast_message_callback, (LPARAM)&info );
1726 if (!(dest_tid = GetWindowThreadProcessId( hwnd, NULL ))) return FALSE;
1728 if (USER_IsExitingThread( dest_tid )) return TRUE;
1730 return put_message_in_queue( dest_tid, &info, NULL );
1734 /**********************************************************************
1735 * PostThreadMessageA (USER32.@)
1737 BOOL WINAPI PostThreadMessageA( DWORD thread, UINT msg, WPARAM wparam, LPARAM lparam )
1739 return PostThreadMessageW( thread, msg, map_wparam_AtoW( msg, wparam ), lparam );
1743 /**********************************************************************
1744 * PostThreadMessageW (USER32.@)
1746 BOOL WINAPI PostThreadMessageW( DWORD thread, UINT msg, WPARAM wparam, LPARAM lparam )
1748 struct send_message_info info;
1750 if (is_pointer_message( msg ))
1752 SetLastError( ERROR_INVALID_PARAMETER );
1755 if (USER_IsExitingThread( thread )) return TRUE;
1757 info.type = MSG_POSTED;
1760 info.wparam = wparam;
1761 info.lparam = lparam;
1762 return put_message_in_queue( thread, &info, NULL );
1766 /***********************************************************************
1767 * PostQuitMessage (USER32.@)
1769 void WINAPI PostQuitMessage( INT exitCode )
1771 PostThreadMessageW( GetCurrentThreadId(), WM_QUIT, exitCode, 0 );
1775 /***********************************************************************
1776 * PeekMessageW (USER32.@)
1778 BOOL WINAPI PeekMessageW( MSG *msg_out, HWND hwnd, UINT first, UINT last, UINT flags )
1780 MESSAGEQUEUE *queue;
1784 /* check for graphics events */
1785 if (USER_Driver.pMsgWaitForMultipleObjectsEx)
1786 USER_Driver.pMsgWaitForMultipleObjectsEx( 0, NULL, 0, 0, 0 );
1788 hwnd = WIN_GetFullHandle( hwnd );
1789 locks = WIN_SuspendWndsLock();
1791 if (!MSG_peek_message( &msg, hwnd, first, last,
1792 (flags & PM_REMOVE) ? GET_MSG_REMOVE : 0 ))
1794 /* FIXME: should be done before checking for hw events */
1795 MSG_JournalPlayBackMsg();
1797 if (!(flags & PM_NOYIELD))
1800 ReleaseThunkLock(&count);
1801 if (count) RestoreThunkLock(count);
1803 WIN_RestoreWndsLock( locks );
1807 WIN_RestoreWndsLock( locks );
1809 /* need to fill the window handle for WM_PAINT message */
1810 if (msg.message == WM_PAINT)
1812 if (IsIconic( msg.hwnd ) && GetClassLongA( msg.hwnd, GCL_HICON ))
1814 msg.message = WM_PAINTICON;
1817 /* clear internal paint flag */
1818 RedrawWindow( msg.hwnd, NULL, 0, RDW_NOINTERNALPAINT | RDW_NOCHILDREN );
1821 if ((queue = QUEUE_Current()))
1823 queue->GetMessageTimeVal = msg.time;
1824 msg.pt.x = LOWORD( queue->GetMessagePosVal );
1825 msg.pt.y = HIWORD( queue->GetMessagePosVal );
1828 HOOK_CallHooksW( WH_GETMESSAGE, HC_ACTION, flags & PM_REMOVE, (LPARAM)&msg );
1830 /* copy back our internal safe copy of message data to msg_out.
1831 * msg_out is a variable from the *program*, so it can't be used
1832 * internally as it can get "corrupted" by our use of SendMessage()
1833 * (back to the program) inside the message handling itself. */
1839 /***********************************************************************
1840 * PeekMessageA (USER32.@)
1842 BOOL WINAPI PeekMessageA( MSG *msg, HWND hwnd, UINT first, UINT last, UINT flags )
1844 BOOL ret = PeekMessageW( msg, hwnd, first, last, flags );
1845 if (ret) msg->wParam = map_wparam_WtoA( msg->message, msg->wParam );
1850 /***********************************************************************
1851 * GetMessageW (USER32.@)
1853 BOOL WINAPI GetMessageW( MSG *msg, HWND hwnd, UINT first, UINT last )
1855 MESSAGEQUEUE *queue = QUEUE_Current();
1858 mask = QS_POSTMESSAGE | QS_SENDMESSAGE; /* Always selected */
1861 if ((first <= WM_KEYLAST) && (last >= WM_KEYFIRST)) mask |= QS_KEY;
1862 if ( ((first <= WM_MOUSELAST) && (last >= WM_MOUSEFIRST)) ||
1863 ((first <= WM_NCMOUSELAST) && (last >= WM_NCMOUSEFIRST)) ) mask |= QS_MOUSE;
1864 if ((first <= WM_TIMER) && (last >= WM_TIMER)) mask |= QS_TIMER;
1865 if ((first <= WM_SYSTIMER) && (last >= WM_SYSTIMER)) mask |= QS_TIMER;
1866 if ((first <= WM_PAINT) && (last >= WM_PAINT)) mask |= QS_PAINT;
1868 else mask |= QS_MOUSE | QS_KEY | QS_TIMER | QS_PAINT;
1870 locks = WIN_SuspendWndsLock();
1872 while (!PeekMessageW( msg, hwnd, first, last, PM_REMOVE ))
1874 /* wait until one of the bits is set */
1875 unsigned int wake_bits = 0, changed_bits = 0;
1878 SERVER_START_REQ( set_queue_mask )
1880 req->wake_mask = QS_SENDMESSAGE;
1881 req->changed_mask = mask;
1883 if (!wine_server_call( req ))
1885 wake_bits = reply->wake_bits;
1886 changed_bits = reply->changed_bits;
1891 if (changed_bits & mask) continue;
1892 if (wake_bits & QS_SENDMESSAGE) continue;
1894 TRACE( "(%04x) mask=%08x, bits=%08x, changed=%08x, waiting\n",
1895 queue->self, mask, wake_bits, changed_bits );
1897 ReleaseThunkLock( &dwlc );
1898 if (USER_Driver.pMsgWaitForMultipleObjectsEx)
1899 USER_Driver.pMsgWaitForMultipleObjectsEx( 1, &queue->server_queue, INFINITE, 0, 0 );
1901 WaitForSingleObject( queue->server_queue, INFINITE );
1902 if (dwlc) RestoreThunkLock( dwlc );
1905 WIN_RestoreWndsLock( locks );
1907 return (msg->message != WM_QUIT);
1911 /***********************************************************************
1912 * GetMessageA (USER32.@)
1914 BOOL WINAPI GetMessageA( MSG *msg, HWND hwnd, UINT first, UINT last )
1916 GetMessageW( msg, hwnd, first, last );
1917 msg->wParam = map_wparam_WtoA( msg->message, msg->wParam );
1918 return (msg->message != WM_QUIT);
1922 /***********************************************************************
1923 * SetMessageQueue (USER32.@)
1925 BOOL WINAPI SetMessageQueue( INT size )
1927 /* now obsolete the message queue will be expanded dynamically as necessary */