2 * Window messaging support
4 * Copyright 2001 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
22 #include "wine/port.h"
30 #include "wine/unicode.h"
31 #include "wine/server.h"
39 #include "wine/debug.h"
41 WINE_DEFAULT_DEBUG_CHANNEL(msg);
43 #define WM_NCMOUSEFIRST WM_NCMOUSEMOVE
44 #define WM_NCMOUSELAST WM_NCMBUTTONDBLCLK
46 #define MAX_PACK_COUNT 4
48 /* description of the data fields that need to be packed along with a sent message */
52 const void *data[MAX_PACK_COUNT];
53 size_t size[MAX_PACK_COUNT];
56 /* info about the message currently being received by the current thread */
57 struct received_message_info
59 enum message_type type;
61 UINT flags; /* InSendMessageEx return flags */
64 /* structure to group all parameters for sent messages of the various kinds */
65 struct send_message_info
67 enum message_type type;
72 UINT flags; /* flags for SendMessageTimeout */
73 UINT timeout; /* timeout for SendMessageTimeout */
74 SENDASYNCPROC callback; /* callback function for SendMessageCallback */
75 ULONG_PTR data; /* callback data */
79 /* flag for messages that contain pointers */
80 /* 32 messages per entry, messages 0..31 map to bits 0..31 */
82 #define SET(msg) (1 << ((msg) & 31))
84 static const unsigned int message_pointer_flags[] =
87 SET(WM_CREATE) | SET(WM_SETTEXT) | SET(WM_GETTEXT) |
88 SET(WM_WININICHANGE) | SET(WM_DEVMODECHANGE),
90 SET(WM_GETMINMAXINFO) | SET(WM_DRAWITEM) | SET(WM_MEASUREITEM) | SET(WM_DELETEITEM) |
93 SET(WM_WINDOWPOSCHANGING) | SET(WM_WINDOWPOSCHANGED) | SET(WM_COPYDATA) |
94 SET(WM_NOTIFY) | SET(WM_HELP),
96 SET(WM_STYLECHANGING) | SET(WM_STYLECHANGED),
98 SET(WM_NCCREATE) | SET(WM_NCCALCSIZE) | SET(WM_GETDLGCODE),
100 SET(EM_GETSEL) | SET(EM_GETRECT) | SET(EM_SETRECT) | SET(EM_SETRECTNP),
102 SET(EM_REPLACESEL) | SET(EM_GETLINE) | SET(EM_SETTABSTOPS),
104 SET(SBM_GETRANGE) | SET(SBM_SETSCROLLINFO) | SET(SBM_GETSCROLLINFO),
110 SET(CB_GETEDITSEL) | SET(CB_ADDSTRING) | SET(CB_DIR) | SET(CB_GETLBTEXT) |
111 SET(CB_INSERTSTRING) | SET(CB_FINDSTRING) | SET(CB_SELECTSTRING) |
112 SET(CB_GETDROPPEDCONTROLRECT) | SET(CB_FINDSTRINGEXACT),
116 SET(LB_ADDSTRING) | SET(LB_INSERTSTRING) | SET(LB_GETTEXT) | SET(LB_SELECTSTRING) |
117 SET(LB_DIR) | SET(LB_FINDSTRING) |
118 SET(LB_GETSELITEMS) | SET(LB_SETTABSTOPS) | SET(LB_ADDFILE) | SET(LB_GETITEMRECT),
120 SET(LB_FINDSTRINGEXACT),
126 SET(WM_NEXTMENU) | SET(WM_SIZING) | SET(WM_MOVING) | SET(WM_DEVICECHANGE),
128 SET(WM_MDICREATE) | SET(WM_MDIGETACTIVE) | SET(WM_DROPOBJECT) |
129 SET(WM_QUERYDROPOBJECT) | SET(WM_DRAGLOOP) | SET(WM_DRAGSELECT) | SET(WM_DRAGMOVE),
143 SET(WM_ASKCBFORMATNAME)
146 /* flags for messages that contain Unicode strings */
147 static const unsigned int message_unicode_flags[] =
150 SET(WM_CREATE) | SET(WM_SETTEXT) | SET(WM_GETTEXT) | SET(WM_GETTEXTLENGTH) |
151 SET(WM_WININICHANGE) | SET(WM_DEVMODECHANGE),
163 SET(EM_REPLACESEL) | SET(EM_GETLINE) | SET(EM_SETPASSWORDCHAR),
167 SET(WM_CHAR) | SET(WM_DEADCHAR) | SET(WM_SYSCHAR) | SET(WM_SYSDEADCHAR),
171 SET(CB_ADDSTRING) | SET(CB_DIR) | SET(CB_GETLBTEXT) | SET(CB_GETLBTEXTLEN) |
172 SET(CB_INSERTSTRING) | SET(CB_FINDSTRING) | SET(CB_SELECTSTRING) | SET(CB_FINDSTRINGEXACT),
176 SET(LB_ADDSTRING) | SET(LB_INSERTSTRING) | SET(LB_GETTEXT) | SET(LB_GETTEXTLEN) |
177 SET(LB_SELECTSTRING) | SET(LB_DIR) | SET(LB_FINDSTRING) | SET(LB_ADDFILE),
179 SET(LB_FINDSTRINGEXACT),
201 SET(WM_PAINTCLIPBOARD) | SET(WM_SIZECLIPBOARD) | SET(WM_ASKCBFORMATNAME)
204 /* check whether a given message type includes pointers */
205 inline static int is_pointer_message( UINT message )
207 if (message >= 8*sizeof(message_pointer_flags)) return FALSE;
208 return (message_pointer_flags[message / 32] & SET(message)) != 0;
211 /* check whether a given message type contains Unicode (or ASCII) chars */
212 inline static int is_unicode_message( UINT message )
214 if (message >= 8*sizeof(message_unicode_flags)) return FALSE;
215 return (message_unicode_flags[message / 32] & SET(message)) != 0;
220 /* add a data field to a packed message */
221 inline static void push_data( struct packed_message *data, const void *ptr, size_t size )
223 data->data[data->count] = ptr;
224 data->size[data->count] = size;
228 /* add a string to a packed message */
229 inline static void push_string( struct packed_message *data, LPCWSTR str )
231 push_data( data, str, (strlenW(str) + 1) * sizeof(WCHAR) );
234 /* retrieve a pointer to data from a packed message and increment the buffer pointer */
235 inline static void *get_data( void **buffer, size_t size )
238 *buffer = (char *)*buffer + size;
242 /* make sure that the buffer contains a valid null-terminated Unicode string */
243 inline static BOOL check_string( LPCWSTR str, size_t size )
245 for (size /= sizeof(WCHAR); size; size--, str++)
246 if (!*str) return TRUE;
250 /* make sure that there is space for 'size' bytes in buffer, growing it if needed */
251 inline static void *get_buffer_space( void **buffer, size_t size )
254 if (!(ret = HeapReAlloc( GetProcessHeap(), 0, *buffer, size )))
255 HeapFree( GetProcessHeap(), 0, *buffer );
260 /* retrieve a string pointer from packed data */
261 inline static LPWSTR get_string( void **buffer )
263 return get_data( buffer, (strlenW( (LPWSTR)*buffer ) + 1) * sizeof(WCHAR) );
266 /* check whether a combobox expects strings or ids in CB_ADDSTRING/CB_INSERTSTRING */
267 inline static BOOL combobox_has_strings( HWND hwnd )
269 DWORD style = GetWindowLongA( hwnd, GWL_STYLE );
270 return (!(style & (CBS_OWNERDRAWFIXED | CBS_OWNERDRAWVARIABLE)) || (style & CBS_HASSTRINGS));
273 /* check whether a listbox expects strings or ids in LB_ADDSTRING/LB_INSERTSTRING */
274 inline static BOOL listbox_has_strings( HWND hwnd )
276 DWORD style = GetWindowLongA( hwnd, GWL_STYLE );
277 return (!(style & (LBS_OWNERDRAWFIXED | LBS_OWNERDRAWVARIABLE)) || (style & LBS_HASSTRINGS));
280 /* check if hwnd is a broadcast magic handle */
281 inline static BOOL is_broadcast( HWND hwnd )
283 return (hwnd == HWND_BROADCAST || hwnd == HWND_TOPMOST);
287 /***********************************************************************
288 * broadcast_message_callback
290 * Helper callback for broadcasting messages.
292 static BOOL CALLBACK broadcast_message_callback( HWND hwnd, LPARAM lparam )
294 struct send_message_info *info = (struct send_message_info *)lparam;
295 if (!(GetWindowLongW( hwnd, GWL_STYLE ) & (WS_POPUP|WS_CAPTION))) return TRUE;
299 SendMessageTimeoutW( hwnd, info->msg, info->wparam, info->lparam,
300 info->flags, info->timeout, NULL );
303 SendMessageTimeoutA( hwnd, info->msg, info->wparam, info->lparam,
304 info->flags, info->timeout, NULL );
307 SendNotifyMessageW( hwnd, info->msg, info->wparam, info->lparam );
310 SendMessageCallbackW( hwnd, info->msg, info->wparam, info->lparam,
311 info->callback, info->data );
314 PostMessageW( hwnd, info->msg, info->wparam, info->lparam );
317 ERR( "bad type %d\n", info->type );
324 /***********************************************************************
327 * Convert the wparam of an ASCII message to Unicode.
329 static WPARAM map_wparam_AtoW( UINT message, WPARAM wparam )
331 if (message == WM_CHARTOITEM ||
332 message == EM_SETPASSWORDCHAR ||
333 message == WM_CHAR ||
334 message == WM_DEADCHAR ||
335 message == WM_SYSCHAR ||
336 message == WM_SYSDEADCHAR ||
337 message == WM_MENUCHAR)
339 char ch = LOWORD(wparam);
341 MultiByteToWideChar(CP_ACP, 0, &ch, 1, &wch, 1);
342 wparam = MAKEWPARAM( wch, HIWORD(wparam) );
348 /***********************************************************************
351 * Convert the wparam of a Unicode message to ASCII.
353 static WPARAM map_wparam_WtoA( UINT message, WPARAM wparam )
355 if (message == WM_CHARTOITEM ||
356 message == EM_SETPASSWORDCHAR ||
357 message == WM_CHAR ||
358 message == WM_DEADCHAR ||
359 message == WM_SYSCHAR ||
360 message == WM_SYSDEADCHAR ||
361 message == WM_MENUCHAR)
363 WCHAR wch = LOWORD(wparam);
365 WideCharToMultiByte( CP_ACP, 0, &wch, 1, &ch, 1, NULL, NULL );
366 wparam = MAKEWPARAM( (unsigned char)ch, HIWORD(wparam) );
372 /***********************************************************************
375 * Pack a message for sending to another process.
376 * Return the size of the data we expect in the message reply.
377 * Set data->count to -1 if there is an error.
379 static size_t pack_message( HWND hwnd, UINT message, WPARAM wparam, LPARAM lparam,
380 struct packed_message *data )
388 CREATESTRUCTW *cs = (CREATESTRUCTW *)lparam;
389 push_data( data, cs, sizeof(*cs) );
390 if (HIWORD(cs->lpszName)) push_string( data, cs->lpszName );
391 if (HIWORD(cs->lpszClass)) push_string( data, cs->lpszClass );
395 case WM_ASKCBFORMATNAME:
396 return wparam * sizeof(WCHAR);
398 case WM_WININICHANGE:
399 case WM_DEVMODECHANGE:
404 push_string( data, (LPWSTR)lparam );
406 case WM_GETMINMAXINFO:
407 push_data( data, (MINMAXINFO *)lparam, sizeof(MINMAXINFO) );
408 return sizeof(MINMAXINFO);
410 push_data( data, (DRAWITEMSTRUCT *)lparam, sizeof(DRAWITEMSTRUCT) );
413 push_data( data, (MEASUREITEMSTRUCT *)lparam, sizeof(MEASUREITEMSTRUCT) );
414 return sizeof(MEASUREITEMSTRUCT);
416 push_data( data, (DELETEITEMSTRUCT *)lparam, sizeof(DELETEITEMSTRUCT) );
419 push_data( data, (COMPAREITEMSTRUCT *)lparam, sizeof(COMPAREITEMSTRUCT) );
421 case WM_WINDOWPOSCHANGING:
422 case WM_WINDOWPOSCHANGED:
423 push_data( data, (WINDOWPOS *)lparam, sizeof(WINDOWPOS) );
424 return sizeof(WINDOWPOS);
427 COPYDATASTRUCT *cp = (COPYDATASTRUCT *)lparam;
428 push_data( data, cp, sizeof(*cp) );
429 if (cp->lpData) push_data( data, cp->lpData, cp->cbData );
433 /* WM_NOTIFY cannot be sent across processes (MSDN) */
437 push_data( data, (HELPINFO *)lparam, sizeof(HELPINFO) );
439 case WM_STYLECHANGING:
440 case WM_STYLECHANGED:
441 push_data( data, (STYLESTRUCT *)lparam, sizeof(STYLESTRUCT) );
446 push_data( data, (RECT *)lparam, sizeof(RECT) );
451 NCCALCSIZE_PARAMS *nc = (NCCALCSIZE_PARAMS *)lparam;
452 push_data( data, nc, sizeof(*nc) );
453 push_data( data, nc->lppos, sizeof(*nc->lppos) );
454 return sizeof(*nc) + sizeof(*nc->lppos);
457 if (lparam) push_data( data, (MSG *)lparam, sizeof(MSG) );
459 case SBM_SETSCROLLINFO:
460 push_data( data, (SCROLLINFO *)lparam, sizeof(SCROLLINFO) );
462 case SBM_GETSCROLLINFO:
463 push_data( data, (SCROLLINFO *)lparam, sizeof(SCROLLINFO) );
464 return sizeof(SCROLLINFO);
470 if (wparam) size += sizeof(DWORD);
471 if (lparam) size += sizeof(DWORD);
476 case CB_GETDROPPEDCONTROLRECT:
480 push_data( data, (RECT *)lparam, sizeof(RECT) );
484 WORD *pw = (WORD *)lparam;
485 push_data( data, pw, sizeof(*pw) );
486 return *pw * sizeof(WCHAR);
490 if (wparam) push_data( data, (UINT *)lparam, sizeof(UINT) * wparam );
493 case CB_INSERTSTRING:
495 case CB_FINDSTRINGEXACT:
496 case CB_SELECTSTRING:
497 if (combobox_has_strings( hwnd )) push_string( data, (LPWSTR)lparam );
500 if (!combobox_has_strings( hwnd )) return sizeof(ULONG_PTR);
501 return (SendMessageW( hwnd, CB_GETLBTEXTLEN, wparam, 0 ) + 1) * sizeof(WCHAR);
503 case LB_INSERTSTRING:
505 case LB_FINDSTRINGEXACT:
506 case LB_SELECTSTRING:
507 if (listbox_has_strings( hwnd )) push_string( data, (LPWSTR)lparam );
510 if (!listbox_has_strings( hwnd )) return sizeof(ULONG_PTR);
511 return (SendMessageW( hwnd, LB_GETTEXTLEN, wparam, 0 ) + 1) * sizeof(WCHAR);
513 return wparam * sizeof(UINT);
515 push_data( data, (MDINEXTMENU *)lparam, sizeof(MDINEXTMENU) );
516 return sizeof(MDINEXTMENU);
519 push_data( data, (RECT *)lparam, sizeof(RECT) );
523 MDICREATESTRUCTW *cs = (MDICREATESTRUCTW *)lparam;
524 push_data( data, cs, sizeof(*cs) );
525 if (HIWORD(cs->szTitle)) push_string( data, cs->szTitle );
526 if (HIWORD(cs->szClass)) push_string( data, cs->szClass );
529 case WM_MDIGETACTIVE:
530 if (lparam) return sizeof(BOOL);
532 case WM_WINE_SETWINDOWPOS:
533 push_data( data, (WINDOWPOS *)lparam, sizeof(WINDOWPOS) );
536 /* these contain an HFONT */
539 /* these contain an HDC */
542 case WM_ICONERASEBKGND:
544 case WM_CTLCOLORMSGBOX:
545 case WM_CTLCOLOREDIT:
546 case WM_CTLCOLORLISTBOX:
549 case WM_CTLCOLORSCROLLBAR:
550 case WM_CTLCOLORSTATIC:
553 /* these contain an HGLOBAL */
554 case WM_PAINTCLIPBOARD:
555 case WM_SIZECLIPBOARD:
556 /* these contain pointers */
558 case WM_QUERYDROPOBJECT:
562 case WM_DEVICECHANGE:
563 FIXME( "msg %x (%s) not supported yet\n", message, SPY_GetMsgName(message, hwnd) );
571 /***********************************************************************
574 * Unpack a message received from another process.
576 static BOOL unpack_message( HWND hwnd, UINT message, WPARAM *wparam, LPARAM *lparam,
577 void **buffer, size_t size )
586 CREATESTRUCTW *cs = *buffer;
587 WCHAR *str = (WCHAR *)(cs + 1);
588 if (size < sizeof(*cs)) return FALSE;
590 if (HIWORD(cs->lpszName))
592 if (!check_string( str, size )) return FALSE;
594 size -= (strlenW(str) + 1) * sizeof(WCHAR);
595 str += strlenW(str) + 1;
597 if (HIWORD(cs->lpszClass))
599 if (!check_string( str, size )) return FALSE;
605 case WM_ASKCBFORMATNAME:
606 if (!get_buffer_space( buffer, (*wparam * sizeof(WCHAR)) )) return FALSE;
609 case WM_WININICHANGE:
610 case WM_DEVMODECHANGE:
615 if (!check_string( *buffer, size )) return FALSE;
617 case WM_GETMINMAXINFO:
618 minsize = sizeof(MINMAXINFO);
621 minsize = sizeof(DRAWITEMSTRUCT);
624 minsize = sizeof(MEASUREITEMSTRUCT);
627 minsize = sizeof(DELETEITEMSTRUCT);
630 minsize = sizeof(COMPAREITEMSTRUCT);
632 case WM_WINDOWPOSCHANGING:
633 case WM_WINDOWPOSCHANGED:
634 case WM_WINE_SETWINDOWPOS:
635 minsize = sizeof(WINDOWPOS);
639 COPYDATASTRUCT *cp = *buffer;
640 if (size < sizeof(*cp)) return FALSE;
643 minsize = sizeof(*cp) + cp->cbData;
649 /* WM_NOTIFY cannot be sent across processes (MSDN) */
652 minsize = sizeof(HELPINFO);
654 case WM_STYLECHANGING:
655 case WM_STYLECHANGED:
656 minsize = sizeof(STYLESTRUCT);
659 if (!*wparam) minsize = sizeof(RECT);
662 NCCALCSIZE_PARAMS *nc = *buffer;
663 if (size < sizeof(*nc) + sizeof(*nc->lppos)) return FALSE;
664 nc->lppos = (WINDOWPOS *)(nc + 1);
668 if (!*lparam) return TRUE;
669 minsize = sizeof(MSG);
671 case SBM_SETSCROLLINFO:
672 minsize = sizeof(SCROLLINFO);
674 case SBM_GETSCROLLINFO:
675 if (!get_buffer_space( buffer, sizeof(SCROLLINFO ))) return FALSE;
680 if (*wparam || *lparam)
682 if (!get_buffer_space( buffer, 2*sizeof(DWORD) )) return FALSE;
683 if (*wparam) *wparam = (WPARAM)*buffer;
684 if (*lparam) *lparam = (LPARAM)((DWORD *)*buffer + 1);
689 case CB_GETDROPPEDCONTROLRECT:
690 if (!get_buffer_space( buffer, sizeof(RECT) )) return FALSE;
694 minsize = sizeof(RECT);
699 if (size < sizeof(WORD)) return FALSE;
700 len = *(WORD *)*buffer;
701 if (!get_buffer_space( buffer, (len + 1) * sizeof(WCHAR) )) return FALSE;
702 *lparam = (LPARAM)*buffer + sizeof(WORD); /* don't erase WORD at start of buffer */
707 if (!*wparam) return TRUE;
708 minsize = *wparam * sizeof(UINT);
711 case CB_INSERTSTRING:
713 case CB_FINDSTRINGEXACT:
714 case CB_SELECTSTRING:
716 case LB_INSERTSTRING:
718 case LB_FINDSTRINGEXACT:
719 case LB_SELECTSTRING:
720 if (!*buffer) return TRUE;
721 if (!check_string( *buffer, size )) return FALSE;
725 size = sizeof(ULONG_PTR);
726 if (combobox_has_strings( hwnd ))
727 size = (SendMessageW( hwnd, CB_GETLBTEXTLEN, *wparam, 0 ) + 1) * sizeof(WCHAR);
728 if (!get_buffer_space( buffer, size )) return FALSE;
733 size = sizeof(ULONG_PTR);
734 if (listbox_has_strings( hwnd ))
735 size = (SendMessageW( hwnd, LB_GETTEXTLEN, *wparam, 0 ) + 1) * sizeof(WCHAR);
736 if (!get_buffer_space( buffer, size )) return FALSE;
740 if (!get_buffer_space( buffer, *wparam * sizeof(UINT) )) return FALSE;
743 minsize = sizeof(MDINEXTMENU);
744 if (!get_buffer_space( buffer, sizeof(MDINEXTMENU) )) return FALSE;
748 minsize = sizeof(RECT);
749 if (!get_buffer_space( buffer, sizeof(RECT) )) return FALSE;
753 MDICREATESTRUCTW *cs = *buffer;
754 WCHAR *str = (WCHAR *)(cs + 1);
755 if (size < sizeof(*cs)) return FALSE;
757 if (HIWORD(cs->szTitle))
759 if (!check_string( str, size )) return FALSE;
761 size -= (strlenW(str) + 1) * sizeof(WCHAR);
762 str += strlenW(str) + 1;
764 if (HIWORD(cs->szClass))
766 if (!check_string( str, size )) return FALSE;
771 case WM_MDIGETACTIVE:
772 if (!*lparam) return TRUE;
773 if (!get_buffer_space( buffer, sizeof(BOOL) )) return FALSE;
775 /* these contain an HFONT */
778 /* these contain an HDC */
781 case WM_ICONERASEBKGND:
783 case WM_CTLCOLORMSGBOX:
784 case WM_CTLCOLOREDIT:
785 case WM_CTLCOLORLISTBOX:
788 case WM_CTLCOLORSCROLLBAR:
789 case WM_CTLCOLORSTATIC:
792 /* these contain an HGLOBAL */
793 case WM_PAINTCLIPBOARD:
794 case WM_SIZECLIPBOARD:
795 /* these contain pointers */
797 case WM_QUERYDROPOBJECT:
801 case WM_DEVICECHANGE:
802 FIXME( "msg %x (%s) not supported yet\n", message, SPY_GetMsgName(message, hwnd) );
806 return TRUE; /* message doesn't need any unpacking */
809 /* default exit for most messages: check minsize and store buffer in lparam */
810 if (size < minsize) return FALSE;
811 *lparam = (LPARAM)*buffer;
816 /***********************************************************************
819 * Pack a reply to a message for sending to another process.
821 static void pack_reply( HWND hwnd, UINT message, WPARAM wparam, LPARAM lparam,
822 LRESULT res, struct packed_message *data )
829 push_data( data, (CREATESTRUCTW *)lparam, sizeof(CREATESTRUCTW) );
834 push_data( data, (WCHAR *)lparam, (res + 1) * sizeof(WCHAR) );
836 case WM_GETMINMAXINFO:
837 push_data( data, (MINMAXINFO *)lparam, sizeof(MINMAXINFO) );
840 push_data( data, (MEASUREITEMSTRUCT *)lparam, sizeof(MEASUREITEMSTRUCT) );
842 case WM_WINDOWPOSCHANGING:
843 case WM_WINDOWPOSCHANGED:
844 push_data( data, (WINDOWPOS *)lparam, sizeof(WINDOWPOS) );
847 if (lparam) push_data( data, (MSG *)lparam, sizeof(MSG) );
849 case SBM_GETSCROLLINFO:
850 push_data( data, (SCROLLINFO *)lparam, sizeof(SCROLLINFO) );
854 case CB_GETDROPPEDCONTROLRECT:
857 push_data( data, (RECT *)lparam, sizeof(RECT) );
861 WORD *ptr = (WORD *)lparam;
862 push_data( data, ptr, ptr[-1] * sizeof(WCHAR) );
866 push_data( data, (UINT *)lparam, wparam * sizeof(UINT) );
868 case WM_MDIGETACTIVE:
869 if (lparam) push_data( data, (BOOL *)lparam, sizeof(BOOL) );
873 push_data( data, (RECT *)lparam, sizeof(RECT) );
876 NCCALCSIZE_PARAMS *nc = (NCCALCSIZE_PARAMS *)lparam;
877 push_data( data, nc, sizeof(*nc) );
878 push_data( data, nc->lppos, sizeof(*nc->lppos) );
884 if (wparam) push_data( data, (DWORD *)wparam, sizeof(DWORD) );
885 if (lparam) push_data( data, (DWORD *)lparam, sizeof(DWORD) );
888 push_data( data, (MDINEXTMENU *)lparam, sizeof(MDINEXTMENU) );
891 push_data( data, (MDICREATESTRUCTW *)lparam, sizeof(MDICREATESTRUCTW) );
893 case WM_ASKCBFORMATNAME:
894 push_data( data, (WCHAR *)lparam, (strlenW((WCHAR *)lparam) + 1) * sizeof(WCHAR) );
900 /***********************************************************************
903 * Unpack a message reply received from another process.
905 static void unpack_reply( HWND hwnd, UINT message, WPARAM wparam, LPARAM lparam,
906 void *buffer, size_t size )
913 CREATESTRUCTW *cs = (CREATESTRUCTW *)lparam;
914 LPCWSTR name = cs->lpszName, class = cs->lpszClass;
915 memcpy( cs, buffer, min( sizeof(*cs), size ));
916 cs->lpszName = name; /* restore the original pointers */
917 cs->lpszClass = class;
921 case WM_ASKCBFORMATNAME:
922 memcpy( (WCHAR *)lparam, buffer, min( wparam*sizeof(WCHAR), size ));
924 case WM_GETMINMAXINFO:
925 memcpy( (MINMAXINFO *)lparam, buffer, min( sizeof(MINMAXINFO), size ));
928 memcpy( (MEASUREITEMSTRUCT *)lparam, buffer, min( sizeof(MEASUREITEMSTRUCT), size ));
930 case WM_WINDOWPOSCHANGING:
931 case WM_WINDOWPOSCHANGED:
932 memcpy( (WINDOWPOS *)lparam, buffer, min( sizeof(WINDOWPOS), size ));
935 if (lparam) memcpy( (MSG *)lparam, buffer, min( sizeof(MSG), size ));
937 case SBM_GETSCROLLINFO:
938 memcpy( (SCROLLINFO *)lparam, buffer, min( sizeof(SCROLLINFO), size ));
941 case CB_GETDROPPEDCONTROLRECT:
945 memcpy( (RECT *)lparam, buffer, min( sizeof(RECT), size ));
948 size = min( size, (size_t)*(WORD *)lparam );
949 memcpy( (WCHAR *)lparam, buffer, size );
952 memcpy( (UINT *)lparam, buffer, min( wparam*sizeof(UINT), size ));
956 memcpy( (WCHAR *)lparam, buffer, size );
959 memcpy( (MDINEXTMENU *)lparam, buffer, min( sizeof(MDINEXTMENU), size ));
961 case WM_MDIGETACTIVE:
962 if (lparam) memcpy( (BOOL *)lparam, buffer, min( sizeof(BOOL), size ));
966 memcpy( (RECT *)lparam, buffer, min( sizeof(RECT), size ));
969 NCCALCSIZE_PARAMS *nc = (NCCALCSIZE_PARAMS *)lparam;
970 WINDOWPOS *wp = nc->lppos;
971 memcpy( nc, buffer, min( sizeof(*nc), size ));
972 if (size > sizeof(*nc))
975 memcpy( wp, (NCCALCSIZE_PARAMS*)buffer + 1, min( sizeof(*wp), size ));
977 nc->lppos = wp; /* restore the original pointer */
985 memcpy( (DWORD *)wparam, buffer, min( sizeof(DWORD), size ));
986 if (size <= sizeof(DWORD)) break;
987 size -= sizeof(DWORD);
988 buffer = (DWORD *)buffer + 1;
990 if (lparam) memcpy( (DWORD *)lparam, buffer, min( sizeof(DWORD), size ));
994 MDICREATESTRUCTW *cs = (MDICREATESTRUCTW *)lparam;
995 LPCWSTR title = cs->szTitle, class = cs->szClass;
996 memcpy( cs, buffer, min( sizeof(*cs), size ));
997 cs->szTitle = title; /* restore the original pointers */
1002 ERR( "should not happen: unexpected message %x\n", message );
1008 /***********************************************************************
1011 * Send a reply to a sent message.
1013 static void reply_message( struct received_message_info *info, LRESULT result, BOOL remove )
1015 struct packed_message data;
1016 int i, replied = info->flags & ISMEX_REPLIED;
1018 if (info->flags & ISMEX_NOTIFY) return; /* notify messages don't get replies */
1019 if (!remove && replied) return; /* replied already */
1022 info->flags |= ISMEX_REPLIED;
1024 if (info->type == MSG_OTHER_PROCESS && !replied)
1026 pack_reply( info->msg.hwnd, info->msg.message, info->msg.wParam,
1027 info->msg.lParam, result, &data );
1030 SERVER_START_REQ( reply_message )
1032 req->result = result;
1033 req->remove = remove;
1034 for (i = 0; i < data.count; i++) wine_server_add_data( req, data.data[i], data.size[i] );
1035 wine_server_call( req );
1041 /***********************************************************************
1042 * handle_internal_message
1044 * Handle an internal Wine message instead of calling the window proc.
1046 static LRESULT handle_internal_message( HWND hwnd, UINT msg, WPARAM wparam, LPARAM lparam )
1048 if (hwnd == GetDesktopWindow()) return 0;
1051 case WM_WINE_DESTROYWINDOW:
1052 return WIN_DestroyWindow( hwnd );
1053 case WM_WINE_SETWINDOWPOS:
1054 return USER_Driver.pSetWindowPos( (WINDOWPOS *)lparam );
1055 case WM_WINE_SHOWWINDOW:
1056 return ShowWindow( hwnd, wparam );
1057 case WM_WINE_SETPARENT:
1058 return (LRESULT)SetParent( hwnd, (HWND)wparam );
1059 case WM_WINE_SETWINDOWLONG:
1060 return (LRESULT)SetWindowLongW( hwnd, wparam, lparam );
1061 case WM_WINE_ENABLEWINDOW:
1062 return EnableWindow( hwnd, wparam );
1063 case WM_WINE_SETACTIVEWINDOW:
1064 return SetActiveWindow( (HWND)wparam );
1066 FIXME( "unknown internal message %x\n", msg );
1071 /* since the WM_DDE_ACK response to a WM_DDE_EXECUTE message should contain the handle
1072 * to the memory handle, we keep track (in the server side) of all pairs of handle
1073 * used (the client passes its value and the content of the memory handle), and
1074 * the server stored both values (the client, and the local one, created after the
1075 * content). When a ACK message is generated, the list of pair is searched for a
1076 * matching pair, so that the client memory handle can be returned.
1079 HGLOBAL client_hMem;
1080 HGLOBAL server_hMem;
1083 static struct DDE_pair* dde_pairs;
1084 static int dde_num_alloc;
1085 static int dde_num_used;
1086 static CRITICAL_SECTION dde_crst = CRITICAL_SECTION_INIT("Raw_DDE_CritSect");
1088 static BOOL dde_add_pair(HGLOBAL chm, HGLOBAL shm)
1093 EnterCriticalSection(&dde_crst);
1095 /* now remember the pair of hMem on both sides */
1096 if (dde_num_used == dde_num_alloc)
1098 struct DDE_pair* tmp = HeapReAlloc( GetProcessHeap(), 0, dde_pairs,
1099 (dde_num_alloc + GROWBY) * sizeof(struct DDE_pair));
1102 LeaveCriticalSection(&dde_crst);
1106 /* zero out newly allocated part */
1107 memset(&dde_pairs[dde_num_alloc], 0, GROWBY * sizeof(struct DDE_pair));
1108 dde_num_alloc += GROWBY;
1111 for (i = 0; i < dde_num_alloc; i++)
1113 if (dde_pairs[i].server_hMem == 0)
1115 dde_pairs[i].client_hMem = chm;
1116 dde_pairs[i].server_hMem = shm;
1121 LeaveCriticalSection(&dde_crst);
1125 static HGLOBAL dde_get_pair(HGLOBAL shm)
1130 EnterCriticalSection(&dde_crst);
1131 for (i = 0; i < dde_num_alloc; i++)
1133 if (dde_pairs[i].server_hMem == shm)
1135 /* free this pair */
1136 dde_pairs[i].server_hMem = 0;
1138 ret = dde_pairs[i].client_hMem;
1142 LeaveCriticalSection(&dde_crst);
1146 /***********************************************************************
1151 static BOOL post_dde_message( DWORD dest_tid, struct packed_message *data, const struct send_message_info *info )
1157 HGLOBAL hunlock = 0;
1161 if (!UnpackDDElParam( info->msg, info->lparam, &uiLo, &uiHi ))
1167 /* DDE messages which don't require packing are:
1176 /* uiHi should contain a hMem from WM_DDE_EXECUTE */
1177 HGLOBAL h = dde_get_pair( uiHi );
1180 /* send back the value of h on the other side */
1181 push_data( data, &h, sizeof(HGLOBAL) );
1183 TRACE( "send dde-ack %x %08x => %08lx\n", uiLo, uiHi, (DWORD)h );
1188 /* uiHi should contain either an atom or 0 */
1189 TRACE( "send dde-ack %x atom=%x\n", uiLo, uiHi );
1190 lp = MAKELONG( uiLo, uiHi );
1199 size = GlobalSize( (HGLOBAL)uiLo ) ;
1200 if ((info->msg == WM_DDE_ADVISE && size < sizeof(DDEADVISE)) ||
1201 (info->msg == WM_DDE_DATA && size < sizeof(DDEDATA)) ||
1202 (info->msg == WM_DDE_POKE && size < sizeof(DDEPOKE))
1206 else if (info->msg != WM_DDE_DATA) return FALSE;
1211 if ((ptr = GlobalLock( (HGLOBAL)uiLo) ))
1213 push_data( data, ptr, size );
1214 hunlock = (HGLOBAL)uiLo;
1217 TRACE( "send ddepack %u %x\n", size, uiHi );
1219 case WM_DDE_EXECUTE:
1222 if ((ptr = GlobalLock( (HGLOBAL)info->lparam) ))
1224 push_data(data, ptr, GlobalSize( (HGLOBAL)info->lparam ));
1225 /* so that the other side can send it back on ACK */
1227 hunlock = (HGLOBAL)info->lparam;
1232 SERVER_START_REQ( send_message )
1235 req->type = info->type;
1236 req->win = info->hwnd;
1237 req->msg = info->msg;
1238 req->wparam = info->wparam;
1240 req->time = GetCurrentTime();
1242 for (i = 0; i < data->count; i++)
1243 wine_server_add_data( req, data->data[i], data->size[i] );
1244 if ((res = wine_server_call( req )))
1246 if (res == STATUS_INVALID_PARAMETER)
1247 /* FIXME: find a STATUS_ value for this one */
1248 SetLastError( ERROR_INVALID_THREAD_ID );
1250 SetLastError( RtlNtStatusToDosError(res) );
1253 FreeDDElParam(info->msg, info->lparam);
1256 if (hunlock) GlobalUnlock(hunlock);
1261 /***********************************************************************
1262 * unpack_dde_message
1264 * Unpack a posted DDE message received from another process.
1266 static BOOL unpack_dde_message( HWND hwnd, UINT message, WPARAM *wparam, LPARAM *lparam,
1267 void **buffer, size_t size )
1278 /* hMem is being passed */
1279 if (size != sizeof(HGLOBAL)) return FALSE;
1280 if (!buffer || !*buffer) return FALSE;
1282 memcpy( &hMem, *buffer, size );
1284 TRACE("recv dde-ack %u mem=%x[%lx]\n", uiLo, uiHi, GlobalSize( uiHi ));
1288 uiLo = LOWORD( *lparam );
1289 uiHi = HIWORD( *lparam );
1290 TRACE("recv dde-ack %x atom=%x\n", uiLo, uiHi);
1292 *lparam = PackDDElParam( WM_DDE_ACK, uiLo, uiHi );
1297 if ((!buffer || !*buffer) && message != WM_DDE_DATA) return FALSE;
1299 TRACE( "recv ddepack %u %x\n", size, uiHi );
1302 hMem = GlobalAlloc( GMEM_MOVEABLE|GMEM_DDESHARE, size );
1303 if (hMem && (ptr = GlobalLock( hMem )))
1305 memcpy( ptr, *buffer, size );
1306 GlobalUnlock( hMem );
1312 *lparam = PackDDElParam( message, uiLo, uiHi );
1314 case WM_DDE_EXECUTE:
1317 if (!buffer || !*buffer) return FALSE;
1318 hMem = GlobalAlloc( GMEM_MOVEABLE|GMEM_DDESHARE, size );
1319 if (hMem && (ptr = GlobalLock( hMem )))
1321 memcpy( ptr, *buffer, size );
1322 GlobalUnlock( hMem );
1323 TRACE( "exec: pairing c=%08lx s=%08lx\n", *lparam, (DWORD)hMem );
1324 if (!dde_add_pair( *lparam, hMem ))
1330 } else return FALSE;
1337 /***********************************************************************
1340 * Call a window procedure and the corresponding hooks.
1342 static LRESULT call_window_proc( HWND hwnd, UINT msg, WPARAM wparam, LPARAM lparam, BOOL unicode )
1346 MESSAGEQUEUE *queue = QUEUE_Current();
1348 if (queue->recursion_count > MAX_SENDMSG_RECURSION) return 0;
1349 queue->recursion_count++;
1351 if (msg & 0x80000000)
1353 result = handle_internal_message( hwnd, msg, wparam, lparam );
1357 /* first the WH_CALLWNDPROC hook */
1358 if (HOOK_IsHooked( WH_CALLWNDPROC ))
1361 cwp.lParam = lparam;
1362 cwp.wParam = wparam;
1364 cwp.hwnd = WIN_GetFullHandle( hwnd );
1365 if (unicode) HOOK_CallHooksW( WH_CALLWNDPROC, HC_ACTION, 1, (LPARAM)&cwp );
1366 else HOOK_CallHooksA( WH_CALLWNDPROC, HC_ACTION, 1, (LPARAM)&cwp );
1367 lparam = cwp.lParam;
1368 wparam = cwp.wParam;
1373 /* now call the window procedure */
1376 if (!(winproc = (WNDPROC)GetWindowLongW( hwnd, GWL_WNDPROC ))) goto done;
1377 result = CallWindowProcW( winproc, hwnd, msg, wparam, lparam );
1381 if (!(winproc = (WNDPROC)GetWindowLongA( hwnd, GWL_WNDPROC ))) goto done;
1382 result = CallWindowProcA( winproc, hwnd, msg, wparam, lparam );
1385 /* and finally the WH_CALLWNDPROCRET hook */
1386 if (HOOK_IsHooked( WH_CALLWNDPROCRET ))
1389 cwp.lResult = result;
1390 cwp.lParam = lparam;
1391 cwp.wParam = wparam;
1393 cwp.hwnd = WIN_GetFullHandle( hwnd );
1394 if (unicode) HOOK_CallHooksW( WH_CALLWNDPROCRET, HC_ACTION, 1, (LPARAM)&cwp );
1395 else HOOK_CallHooksA( WH_CALLWNDPROCRET, HC_ACTION, 1, (LPARAM)&cwp );
1398 queue->recursion_count--;
1403 /***********************************************************************
1406 * Peek for a message matching the given parameters. Return FALSE if none available.
1407 * All pending sent messages are processed before returning.
1409 BOOL MSG_peek_message( MSG *msg, HWND hwnd, UINT first, UINT last, int flags )
1412 ULONG_PTR extra_info = 0;
1413 MESSAGEQUEUE *queue = QUEUE_Current();
1414 struct received_message_info info, *old_info;
1416 if (!first && !last) last = ~0;
1421 void *buffer = NULL;
1422 size_t size = 0, buffer_size = 0;
1424 do /* loop while buffer is too small */
1426 if (buffer_size && !(buffer = HeapAlloc( GetProcessHeap(), 0, buffer_size )))
1428 SERVER_START_REQ( get_message )
1431 req->get_win = hwnd;
1432 req->get_first = first;
1433 req->get_last = last;
1434 if (buffer_size) wine_server_set_reply( req, buffer, buffer_size );
1435 if (!(res = wine_server_call( req )))
1437 size = wine_server_reply_size( reply );
1438 info.type = reply->type;
1439 info.msg.hwnd = reply->win;
1440 info.msg.message = reply->msg;
1441 info.msg.wParam = reply->wparam;
1442 info.msg.lParam = reply->lparam;
1443 info.msg.time = reply->time;
1444 info.msg.pt.x = reply->x;
1445 info.msg.pt.y = reply->y;
1446 extra_info = reply->info;
1450 if (buffer) HeapFree( GetProcessHeap(), 0, buffer );
1451 buffer_size = reply->total;
1455 } while (res == STATUS_BUFFER_OVERFLOW);
1457 if (res) return FALSE;
1459 TRACE( "got type %d msg %x hwnd %x wp %x lp %lx\n",
1460 info.type, info.msg.message, info.msg.hwnd, info.msg.wParam, info.msg.lParam );
1466 info.flags = ISMEX_SEND;
1469 info.flags = ISMEX_NOTIFY;
1472 info.flags = ISMEX_CALLBACK;
1474 case MSG_OTHER_PROCESS:
1475 info.flags = ISMEX_SEND;
1476 if (!unpack_message( info.msg.hwnd, info.msg.message, &info.msg.wParam,
1477 &info.msg.lParam, &buffer, size ))
1479 ERR( "invalid packed message %x (%s) hwnd %x wp %x lp %lx size %d\n",
1480 info.msg.message, SPY_GetMsgName(info.msg.message, info.msg.hwnd), info.msg.hwnd,
1481 info.msg.wParam, info.msg.lParam, size );
1483 reply_message( &info, 0, TRUE );
1487 case MSG_HARDWARE_RAW:
1488 if (!MSG_process_raw_hardware_message( &info.msg, extra_info,
1489 hwnd, first, last, flags & GET_MSG_REMOVE ))
1492 case MSG_HARDWARE_COOKED:
1493 if (!MSG_process_cooked_hardware_message( &info.msg, extra_info,
1494 flags & GET_MSG_REMOVE ))
1496 flags |= GET_MSG_REMOVE_LAST;
1499 queue->GetMessagePosVal = MAKELONG( info.msg.pt.x, info.msg.pt.y );
1502 queue->GetMessageExtraInfoVal = extra_info;
1503 if (info.msg.message >= WM_DDE_FIRST && info.msg.message <= WM_DDE_LAST)
1505 if (!unpack_dde_message( info.msg.hwnd, info.msg.message, &info.msg.wParam,
1506 &info.msg.lParam, &buffer, size ))
1508 ERR( "invalid packed dde-message %x (%s) hwnd %x wp %x lp %lx size %d\n",
1509 info.msg.message, SPY_GetMsgName(info.msg.message, info.msg.hwnd),
1510 info.msg.hwnd, info.msg.wParam, info.msg.lParam, size );
1516 if (buffer) HeapFree( GetProcessHeap(), 0, buffer );
1520 /* if we get here, we have a sent message; call the window procedure */
1521 old_info = queue->receive_info;
1522 queue->receive_info = &info;
1523 result = call_window_proc( info.msg.hwnd, info.msg.message, info.msg.wParam,
1524 info.msg.lParam, (info.type != MSG_ASCII) );
1525 reply_message( &info, result, TRUE );
1526 queue->receive_info = old_info;
1528 if (buffer) HeapFree( GetProcessHeap(), 0, buffer );
1533 /***********************************************************************
1534 * wait_message_reply
1536 * Wait until a sent message gets replied to.
1538 static void wait_message_reply( UINT flags )
1540 MESSAGEQUEUE *queue;
1542 if (!(queue = QUEUE_Current())) return;
1546 unsigned int wake_bits = 0, changed_bits = 0;
1549 SERVER_START_REQ( set_queue_mask )
1551 req->wake_mask = QS_SMRESULT | ((flags & SMTO_BLOCK) ? 0 : QS_SENDMESSAGE);
1552 req->changed_mask = req->wake_mask;
1554 if (!wine_server_call( req ))
1556 wake_bits = reply->wake_bits;
1557 changed_bits = reply->changed_bits;
1562 if (wake_bits & QS_SMRESULT) return; /* got a result */
1563 if (wake_bits & QS_SENDMESSAGE)
1565 /* Process the sent message immediately */
1567 MSG_peek_message( &msg, 0, 0, 0, GET_MSG_REMOVE | GET_MSG_SENT_ONLY );
1571 /* now wait for it */
1573 ReleaseThunkLock( &dwlc );
1575 if (USER_Driver.pMsgWaitForMultipleObjectsEx)
1576 res = USER_Driver.pMsgWaitForMultipleObjectsEx( 1, &queue->server_queue,
1579 res = WaitForSingleObject( queue->server_queue, INFINITE );
1581 if (dwlc) RestoreThunkLock( dwlc );
1585 /***********************************************************************
1586 * put_message_in_queue
1588 * Put a sent message into the destination queue.
1589 * For inter-process message, reply_size is set to expected size of reply data.
1591 static BOOL put_message_in_queue( DWORD dest_tid, const struct send_message_info *info,
1592 size_t *reply_size )
1594 struct packed_message data;
1596 int i, timeout = -1;
1598 if (info->type != MSG_NOTIFY &&
1599 info->type != MSG_CALLBACK &&
1600 info->type != MSG_POSTED &&
1601 info->timeout != INFINITE)
1602 timeout = info->timeout;
1605 if (info->type == MSG_OTHER_PROCESS)
1607 *reply_size = pack_message( info->hwnd, info->msg, info->wparam, info->lparam, &data );
1608 if (data.count == -1)
1610 WARN( "cannot pack message %x\n", info->msg );
1614 else if (info->type == MSG_POSTED && info->msg >= WM_DDE_FIRST && info->msg <= WM_DDE_LAST)
1616 return post_dde_message( dest_tid, &data, info );
1619 SERVER_START_REQ( send_message )
1622 req->type = info->type;
1623 req->win = info->hwnd;
1624 req->msg = info->msg;
1625 req->wparam = info->wparam;
1626 req->lparam = info->lparam;
1627 req->time = GetCurrentTime();
1628 req->timeout = timeout;
1629 for (i = 0; i < data.count; i++) wine_server_add_data( req, data.data[i], data.size[i] );
1630 if ((res = wine_server_call( req )))
1632 if (res == STATUS_INVALID_PARAMETER)
1633 /* FIXME: find a STATUS_ value for this one */
1634 SetLastError( ERROR_INVALID_THREAD_ID );
1636 SetLastError( RtlNtStatusToDosError(res) );
1644 /***********************************************************************
1647 * Retrieve a message reply from the server.
1649 static LRESULT retrieve_reply( const struct send_message_info *info,
1650 size_t reply_size, LRESULT *result )
1653 void *reply_data = NULL;
1657 if (!(reply_data = HeapAlloc( GetProcessHeap(), 0, reply_size )))
1659 WARN( "no memory for reply %d bytes, will be truncated\n", reply_size );
1663 SERVER_START_REQ( get_message_reply )
1666 if (reply_size) wine_server_set_reply( req, reply_data, reply_size );
1667 if (!(status = wine_server_call( req ))) *result = reply->result;
1668 reply_size = wine_server_reply_size( reply );
1671 if (!status && reply_size)
1672 unpack_reply( info->hwnd, info->msg, info->wparam, info->lparam, reply_data, reply_size );
1674 if (reply_data) HeapFree( GetProcessHeap(), 0, reply_data );
1676 TRACE( "hwnd %x msg %x (%s) wp %x lp %lx got reply %lx (err=%ld)\n",
1677 info->hwnd, info->msg, SPY_GetMsgName(info->msg, info->hwnd), info->wparam,
1678 info->lparam, *result, status );
1680 if (!status) return 1;
1681 if (status == STATUS_TIMEOUT) SetLastError(0); /* timeout */
1682 else SetLastError( RtlNtStatusToDosError(status) );
1687 /***********************************************************************
1688 * send_inter_thread_message
1690 static LRESULT send_inter_thread_message( DWORD dest_tid, const struct send_message_info *info,
1695 size_t reply_size = 0;
1697 TRACE( "hwnd %x msg %x (%s) wp %x lp %lx\n",
1698 info->hwnd, info->msg, SPY_GetMsgName(info->msg, info->hwnd), info->wparam, info->lparam );
1700 if (!put_message_in_queue( dest_tid, info, &reply_size )) return 0;
1702 /* there's no reply to wait for on notify/callback messages */
1703 if (info->type == MSG_NOTIFY || info->type == MSG_CALLBACK) return 1;
1705 locks = WIN_SuspendWndsLock();
1707 wait_message_reply( info->flags );
1708 ret = retrieve_reply( info, reply_size, res_ptr );
1710 WIN_RestoreWndsLock( locks );
1715 /***********************************************************************
1716 * SendMessageTimeoutW (USER32.@)
1718 LRESULT WINAPI SendMessageTimeoutW( HWND hwnd, UINT msg, WPARAM wparam, LPARAM lparam,
1719 UINT flags, UINT timeout, LPDWORD res_ptr )
1721 struct send_message_info info;
1722 DWORD dest_tid, dest_pid;
1723 LRESULT ret, result;
1725 info.type = MSG_UNICODE;
1728 info.wparam = wparam;
1729 info.lparam = lparam;
1731 info.timeout = timeout;
1733 if (is_broadcast(hwnd))
1735 EnumWindows( broadcast_message_callback, (LPARAM)&info );
1736 if (res_ptr) *res_ptr = 1;
1740 if (!(dest_tid = GetWindowThreadProcessId( hwnd, &dest_pid ))) return 0;
1742 if (USER_IsExitingThread( dest_tid )) return 0;
1744 SPY_EnterMessage( SPY_SENDMESSAGE, hwnd, msg, wparam, lparam );
1746 if (dest_tid == GetCurrentThreadId())
1748 result = call_window_proc( hwnd, msg, wparam, lparam, TRUE );
1753 if (dest_pid != GetCurrentProcessId()) info.type = MSG_OTHER_PROCESS;
1754 ret = send_inter_thread_message( dest_tid, &info, &result );
1757 SPY_ExitMessage( SPY_RESULT_OK, hwnd, msg, result, wparam, lparam );
1758 if (ret && res_ptr) *res_ptr = result;
1763 /***********************************************************************
1764 * SendMessageTimeoutA (USER32.@)
1766 LRESULT WINAPI SendMessageTimeoutA( HWND hwnd, UINT msg, WPARAM wparam, LPARAM lparam,
1767 UINT flags, UINT timeout, LPDWORD res_ptr )
1769 struct send_message_info info;
1770 DWORD dest_tid, dest_pid;
1771 LRESULT ret, result;
1773 info.type = MSG_ASCII;
1776 info.wparam = wparam;
1777 info.lparam = lparam;
1779 info.timeout = timeout;
1781 if (is_broadcast(hwnd))
1783 EnumWindows( broadcast_message_callback, (LPARAM)&info );
1784 if (res_ptr) *res_ptr = 1;
1788 if (!(dest_tid = GetWindowThreadProcessId( hwnd, &dest_pid ))) return 0;
1790 if (USER_IsExitingThread( dest_tid )) return 0;
1792 SPY_EnterMessage( SPY_SENDMESSAGE, hwnd, msg, wparam, lparam );
1794 if (dest_tid == GetCurrentThreadId())
1796 result = call_window_proc( hwnd, msg, wparam, lparam, FALSE );
1799 else if (dest_pid == GetCurrentProcessId())
1801 ret = send_inter_thread_message( dest_tid, &info, &result );
1805 /* inter-process message: need to map to Unicode */
1806 info.type = MSG_OTHER_PROCESS;
1807 if (is_unicode_message( info.msg ))
1809 if (WINPROC_MapMsg32ATo32W( info.hwnd, info.msg, &info.wparam, &info.lparam ) == -1)
1811 ret = send_inter_thread_message( dest_tid, &info, &result );
1812 result = WINPROC_UnmapMsg32ATo32W( info.hwnd, info.msg, info.wparam,
1813 info.lparam, result );
1815 else ret = send_inter_thread_message( dest_tid, &info, &result );
1817 SPY_ExitMessage( SPY_RESULT_OK, hwnd, msg, result, wparam, lparam );
1818 if (ret && res_ptr) *res_ptr = result;
1823 /***********************************************************************
1824 * SendMessageW (USER32.@)
1826 LRESULT WINAPI SendMessageW( HWND hwnd, UINT msg, WPARAM wparam, LPARAM lparam )
1829 SendMessageTimeoutW( hwnd, msg, wparam, lparam, SMTO_NORMAL, INFINITE, &res );
1834 /***********************************************************************
1835 * SendMessageA (USER32.@)
1837 LRESULT WINAPI SendMessageA( HWND hwnd, UINT msg, WPARAM wparam, LPARAM lparam )
1840 SendMessageTimeoutA( hwnd, msg, wparam, lparam, SMTO_NORMAL, INFINITE, &res );
1845 /***********************************************************************
1846 * SendNotifyMessageA (USER32.@)
1848 BOOL WINAPI SendNotifyMessageA( HWND hwnd, UINT msg, WPARAM wparam, LPARAM lparam )
1850 return SendNotifyMessageW( hwnd, msg, map_wparam_AtoW( msg, wparam ), lparam );
1854 /***********************************************************************
1855 * SendNotifyMessageW (USER32.@)
1857 BOOL WINAPI SendNotifyMessageW( HWND hwnd, UINT msg, WPARAM wparam, LPARAM lparam )
1859 struct send_message_info info;
1863 if (is_pointer_message(msg))
1865 SetLastError(ERROR_INVALID_PARAMETER);
1869 info.type = MSG_NOTIFY;
1872 info.wparam = wparam;
1873 info.lparam = lparam;
1875 if (is_broadcast(hwnd))
1877 EnumWindows( broadcast_message_callback, (LPARAM)&info );
1881 if (!(dest_tid = GetWindowThreadProcessId( hwnd, NULL ))) return FALSE;
1883 if (USER_IsExitingThread( dest_tid )) return TRUE;
1885 if (dest_tid == GetCurrentThreadId())
1887 call_window_proc( hwnd, msg, wparam, lparam, TRUE );
1890 return send_inter_thread_message( dest_tid, &info, &result );
1894 /***********************************************************************
1895 * SendMessageCallbackA (USER32.@)
1897 BOOL WINAPI SendMessageCallbackA( HWND hwnd, UINT msg, WPARAM wparam, LPARAM lparam,
1898 SENDASYNCPROC callback, ULONG_PTR data )
1900 return SendMessageCallbackW( hwnd, msg, map_wparam_AtoW( msg, wparam ),
1901 lparam, callback, data );
1905 /***********************************************************************
1906 * SendMessageCallbackW (USER32.@)
1908 BOOL WINAPI SendMessageCallbackW( HWND hwnd, UINT msg, WPARAM wparam, LPARAM lparam,
1909 SENDASYNCPROC callback, ULONG_PTR data )
1911 struct send_message_info info;
1915 if (is_pointer_message(msg))
1917 SetLastError(ERROR_INVALID_PARAMETER);
1921 info.type = MSG_CALLBACK;
1924 info.wparam = wparam;
1925 info.lparam = lparam;
1926 info.callback = callback;
1929 if (is_broadcast(hwnd))
1931 EnumWindows( broadcast_message_callback, (LPARAM)&info );
1935 if (!(dest_tid = GetWindowThreadProcessId( hwnd, NULL ))) return FALSE;
1937 if (USER_IsExitingThread( dest_tid )) return TRUE;
1939 if (dest_tid == GetCurrentThreadId())
1941 result = call_window_proc( hwnd, msg, wparam, lparam, TRUE );
1942 callback( hwnd, msg, data, result );
1945 FIXME( "callback will not be called\n" );
1946 return send_inter_thread_message( dest_tid, &info, &result );
1950 /***********************************************************************
1951 * ReplyMessage (USER32.@)
1953 BOOL WINAPI ReplyMessage( LRESULT result )
1955 MESSAGEQUEUE *queue = QUEUE_Current();
1956 struct received_message_info *info = queue->receive_info;
1958 if (!info) return FALSE;
1959 reply_message( info, result, FALSE );
1964 /***********************************************************************
1965 * InSendMessage (USER32.@)
1967 BOOL WINAPI InSendMessage(void)
1969 return (InSendMessageEx(NULL) & (ISMEX_SEND|ISMEX_REPLIED)) == ISMEX_SEND;
1973 /***********************************************************************
1974 * InSendMessageEx (USER32.@)
1976 DWORD WINAPI InSendMessageEx( LPVOID reserved )
1978 MESSAGEQUEUE *queue = QUEUE_Current();
1979 struct received_message_info *info = queue->receive_info;
1981 if (info) return info->flags;
1982 return ISMEX_NOSEND;
1986 /***********************************************************************
1987 * PostMessageA (USER32.@)
1989 BOOL WINAPI PostMessageA( HWND hwnd, UINT msg, WPARAM wparam, LPARAM lparam )
1991 return PostMessageW( hwnd, msg, map_wparam_AtoW( msg, wparam ), lparam );
1995 /***********************************************************************
1996 * PostMessageW (USER32.@)
1998 BOOL WINAPI PostMessageW( HWND hwnd, UINT msg, WPARAM wparam, LPARAM lparam )
2000 struct send_message_info info;
2003 if (is_pointer_message( msg ))
2005 SetLastError( ERROR_INVALID_PARAMETER );
2009 info.type = MSG_POSTED;
2012 info.wparam = wparam;
2013 info.lparam = lparam;
2015 if (is_broadcast(hwnd))
2017 EnumWindows( broadcast_message_callback, (LPARAM)&info );
2020 if (!(dest_tid = GetWindowThreadProcessId( hwnd, NULL ))) return FALSE;
2022 if (USER_IsExitingThread( dest_tid )) return TRUE;
2024 return put_message_in_queue( dest_tid, &info, NULL );
2028 /**********************************************************************
2029 * PostThreadMessageA (USER32.@)
2031 BOOL WINAPI PostThreadMessageA( DWORD thread, UINT msg, WPARAM wparam, LPARAM lparam )
2033 return PostThreadMessageW( thread, msg, map_wparam_AtoW( msg, wparam ), lparam );
2037 /**********************************************************************
2038 * PostThreadMessageW (USER32.@)
2040 BOOL WINAPI PostThreadMessageW( DWORD thread, UINT msg, WPARAM wparam, LPARAM lparam )
2042 struct send_message_info info;
2044 if (is_pointer_message( msg ))
2046 SetLastError( ERROR_INVALID_PARAMETER );
2049 if (USER_IsExitingThread( thread )) return TRUE;
2051 info.type = MSG_POSTED;
2054 info.wparam = wparam;
2055 info.lparam = lparam;
2056 return put_message_in_queue( thread, &info, NULL );
2060 /***********************************************************************
2061 * PostQuitMessage (USER32.@)
2063 void WINAPI PostQuitMessage( INT exitCode )
2065 PostThreadMessageW( GetCurrentThreadId(), WM_QUIT, exitCode, 0 );
2069 /***********************************************************************
2070 * PeekMessageW (USER32.@)
2072 BOOL WINAPI PeekMessageW( MSG *msg_out, HWND hwnd, UINT first, UINT last, UINT flags )
2074 MESSAGEQUEUE *queue;
2078 /* check for graphics events */
2079 if (USER_Driver.pMsgWaitForMultipleObjectsEx)
2080 USER_Driver.pMsgWaitForMultipleObjectsEx( 0, NULL, 0, 0, 0 );
2082 hwnd = WIN_GetFullHandle( hwnd );
2083 locks = WIN_SuspendWndsLock();
2085 if (!MSG_peek_message( &msg, hwnd, first, last,
2086 (flags & PM_REMOVE) ? GET_MSG_REMOVE : 0 ))
2088 /* FIXME: should be done before checking for hw events */
2089 MSG_JournalPlayBackMsg();
2091 if (!(flags & PM_NOYIELD))
2094 ReleaseThunkLock(&count);
2095 if (count) RestoreThunkLock(count);
2097 WIN_RestoreWndsLock( locks );
2101 WIN_RestoreWndsLock( locks );
2103 /* need to fill the window handle for WM_PAINT message */
2104 if (msg.message == WM_PAINT)
2106 if (IsIconic( msg.hwnd ) && GetClassLongA( msg.hwnd, GCL_HICON ))
2108 msg.message = WM_PAINTICON;
2111 /* clear internal paint flag */
2112 RedrawWindow( msg.hwnd, NULL, 0, RDW_NOINTERNALPAINT | RDW_NOCHILDREN );
2115 if ((queue = QUEUE_Current()))
2117 queue->GetMessageTimeVal = msg.time;
2118 msg.pt.x = LOWORD( queue->GetMessagePosVal );
2119 msg.pt.y = HIWORD( queue->GetMessagePosVal );
2122 HOOK_CallHooksW( WH_GETMESSAGE, HC_ACTION, flags & PM_REMOVE, (LPARAM)&msg );
2124 /* copy back our internal safe copy of message data to msg_out.
2125 * msg_out is a variable from the *program*, so it can't be used
2126 * internally as it can get "corrupted" by our use of SendMessage()
2127 * (back to the program) inside the message handling itself. */
2133 /***********************************************************************
2134 * PeekMessageA (USER32.@)
2136 BOOL WINAPI PeekMessageA( MSG *msg, HWND hwnd, UINT first, UINT last, UINT flags )
2138 BOOL ret = PeekMessageW( msg, hwnd, first, last, flags );
2139 if (ret) msg->wParam = map_wparam_WtoA( msg->message, msg->wParam );
2144 /***********************************************************************
2145 * GetMessageW (USER32.@)
2147 BOOL WINAPI GetMessageW( MSG *msg, HWND hwnd, UINT first, UINT last )
2149 MESSAGEQUEUE *queue = QUEUE_Current();
2152 mask = QS_POSTMESSAGE | QS_SENDMESSAGE; /* Always selected */
2155 if ((first <= WM_KEYLAST) && (last >= WM_KEYFIRST)) mask |= QS_KEY;
2156 if ( ((first <= WM_MOUSELAST) && (last >= WM_MOUSEFIRST)) ||
2157 ((first <= WM_NCMOUSELAST) && (last >= WM_NCMOUSEFIRST)) ) mask |= QS_MOUSE;
2158 if ((first <= WM_TIMER) && (last >= WM_TIMER)) mask |= QS_TIMER;
2159 if ((first <= WM_SYSTIMER) && (last >= WM_SYSTIMER)) mask |= QS_TIMER;
2160 if ((first <= WM_PAINT) && (last >= WM_PAINT)) mask |= QS_PAINT;
2162 else mask |= QS_MOUSE | QS_KEY | QS_TIMER | QS_PAINT;
2164 locks = WIN_SuspendWndsLock();
2166 while (!PeekMessageW( msg, hwnd, first, last, PM_REMOVE ))
2168 /* wait until one of the bits is set */
2169 unsigned int wake_bits = 0, changed_bits = 0;
2172 SERVER_START_REQ( set_queue_mask )
2174 req->wake_mask = QS_SENDMESSAGE;
2175 req->changed_mask = mask;
2177 if (!wine_server_call( req ))
2179 wake_bits = reply->wake_bits;
2180 changed_bits = reply->changed_bits;
2185 if (changed_bits & mask) continue;
2186 if (wake_bits & QS_SENDMESSAGE) continue;
2188 TRACE( "(%04x) mask=%08x, bits=%08x, changed=%08x, waiting\n",
2189 queue->self, mask, wake_bits, changed_bits );
2191 ReleaseThunkLock( &dwlc );
2192 if (USER_Driver.pMsgWaitForMultipleObjectsEx)
2193 USER_Driver.pMsgWaitForMultipleObjectsEx( 1, &queue->server_queue, INFINITE, 0, 0 );
2195 WaitForSingleObject( queue->server_queue, INFINITE );
2196 if (dwlc) RestoreThunkLock( dwlc );
2199 WIN_RestoreWndsLock( locks );
2201 return (msg->message != WM_QUIT);
2205 /***********************************************************************
2206 * GetMessageA (USER32.@)
2208 BOOL WINAPI GetMessageA( MSG *msg, HWND hwnd, UINT first, UINT last )
2210 GetMessageW( msg, hwnd, first, last );
2211 msg->wParam = map_wparam_WtoA( msg->message, msg->wParam );
2212 return (msg->message != WM_QUIT);
2216 /***********************************************************************
2217 * IsDialogMessage (USER32.@)
2218 * IsDialogMessageA (USER32.@)
2220 BOOL WINAPI IsDialogMessageA( HWND hwndDlg, LPMSG pmsg )
2223 msg.wParam = map_wparam_AtoW( msg.message, msg.wParam );
2224 return IsDialogMessageW( hwndDlg, &msg );
2228 /***********************************************************************
2229 * SetMessageQueue (USER32.@)
2231 BOOL WINAPI SetMessageQueue( INT size )
2233 /* now obsolete the message queue will be expanded dynamically as necessary */
2238 /**********************************************************************
2239 * AttachThreadInput (USER32.@)
2241 * Attaches the input processing mechanism of one thread to that of
2244 BOOL WINAPI AttachThreadInput( DWORD from, DWORD to, BOOL attach )
2248 SERVER_START_REQ( attach_thread_input )
2250 req->tid_from = from;
2252 req->attach = attach;
2253 ret = !wine_server_call_err( req );
2260 /**********************************************************************
2261 * GetGUIThreadInfo (USER32.@)
2263 BOOL WINAPI GetGUIThreadInfo( DWORD id, GUITHREADINFO *info )
2267 SERVER_START_REQ( get_thread_input )
2270 if ((ret = !wine_server_call_err( req )))
2273 info->hwndActive = reply->active;
2274 info->hwndFocus = reply->focus;
2275 info->hwndCapture = reply->capture;
2276 info->hwndMenuOwner = reply->menu_owner;
2277 info->hwndMoveSize = reply->move_size;
2278 info->hwndCaret = reply->caret;
2279 info->rcCaret.left = reply->rect.left;
2280 info->rcCaret.top = reply->rect.top;
2281 info->rcCaret.right = reply->rect.right;
2282 info->rcCaret.bottom = reply->rect.bottom;
2283 if (reply->menu_owner) info->flags |= GUI_INMENUMODE;
2284 if (reply->move_size) info->flags |= GUI_INMOVESIZE;
2285 if (reply->caret) info->flags |= GUI_CARETBLINKING;