user32/tests: Flush events before the key flags test.
[wine] / dlls / user32 / tests / msg.c
1 /*
2  * Unit tests for window message handling
3  *
4  * Copyright 1999 Ove Kaaven
5  * Copyright 2003 Dimitrie O. Paun
6  * Copyright 2004, 2005 Dmitry Timoshkov
7  *
8  * This library is free software; you can redistribute it and/or
9  * modify it under the terms of the GNU Lesser General Public
10  * License as published by the Free Software Foundation; either
11  * version 2.1 of the License, or (at your option) any later version.
12  *
13  * This library is distributed in the hope that it will be useful,
14  * but WITHOUT ANY WARRANTY; without even the implied warranty of
15  * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the GNU
16  * Lesser General Public License for more details.
17  *
18  * You should have received a copy of the GNU Lesser General Public
19  * License along with this library; if not, write to the Free Software
20  * Foundation, Inc., 51 Franklin St, Fifth Floor, Boston, MA 02110-1301, USA
21  */
22
23 #define _WIN32_WINNT 0x0600 /* For WM_CHANGEUISTATE,QS_RAWINPUT,WM_DWMxxxx */
24 #define WINVER 0x0600 /* for WM_GETTITLEBARINFOEX */
25
26 #include <assert.h>
27 #include <stdarg.h>
28 #include <stdio.h>
29
30 #include "windef.h"
31 #include "winbase.h"
32 #include "wingdi.h"
33 #include "winuser.h"
34 #include "winnls.h"
35
36 #include "wine/test.h"
37
38 #define MDI_FIRST_CHILD_ID 2004
39
40 /* undocumented SWP flags - from SDK 3.1 */
41 #define SWP_NOCLIENTSIZE        0x0800
42 #define SWP_NOCLIENTMOVE        0x1000
43 #define SWP_STATECHANGED        0x8000
44
45 #define SW_NORMALNA             0xCC    /* undoc. flag in MinMaximize */
46
47 #ifndef WM_KEYF1
48 #define WM_KEYF1 0x004d
49 #endif
50
51 #ifndef WM_SYSTIMER
52 #define WM_SYSTIMER         0x0118
53 #endif
54
55 #define WND_PARENT_ID           1
56 #define WND_POPUP_ID            2
57 #define WND_CHILD_ID            3
58
59 #ifndef WM_LBTRACKPOINT
60 #define WM_LBTRACKPOINT  0x0131
61 #endif
62
63 /* encoded DRAWITEMSTRUCT into an LPARAM */
64 typedef struct
65 {
66     union
67     {
68         struct
69         {
70             UINT type    : 4;  /* ODT_* flags */
71             UINT ctl_id  : 4;  /* Control ID */
72             UINT item_id : 4;  /* Menu item ID */
73             UINT action  : 4;  /* ODA_* flags */
74             UINT state   : 16; /* ODS_* flags */
75         } item;
76         LPARAM lp;
77     } u;
78 } DRAW_ITEM_STRUCT;
79
80 static BOOL test_DestroyWindow_flag;
81 static HWINEVENTHOOK hEvent_hook;
82 static HHOOK hKBD_hook;
83 static HHOOK hCBT_hook;
84 static DWORD cbt_hook_thread_id;
85
86 static const WCHAR testWindowClassW[] =
87 { 'T','e','s','t','W','i','n','d','o','w','C','l','a','s','s','W',0 };
88
89 /*
90 FIXME: add tests for these
91 Window Edge Styles (Win31/Win95/98 look), in order of precedence:
92  WS_EX_DLGMODALFRAME: double border, WS_CAPTION allowed
93  WS_THICKFRAME: thick border
94  WS_DLGFRAME: double border, WS_CAPTION not allowed (but possibly shown anyway)
95  WS_BORDER (default for overlapped windows): single black border
96  none (default for child (and popup?) windows): no border
97 */
98
99 typedef enum {
100     sent=0x1,
101     posted=0x2,
102     parent=0x4,
103     wparam=0x8,
104     lparam=0x10,
105     defwinproc=0x20,
106     beginpaint=0x40,
107     optional=0x80,
108     hook=0x100,
109     winevent_hook=0x200,
110     kbd_hook=0x400
111 } msg_flags_t;
112
113 struct message {
114     UINT message;          /* the WM_* code */
115     msg_flags_t flags;     /* message props */
116     WPARAM wParam;         /* expected value of wParam */
117     LPARAM lParam;         /* expected value of lParam */
118     WPARAM wp_mask;        /* mask for wParam checks */
119     LPARAM lp_mask;        /* mask for lParam checks */
120 };
121
122 struct recvd_message {
123     UINT message;          /* the WM_* code */
124     msg_flags_t flags;     /* message props */
125     HWND hwnd;             /* window that received the message */
126     WPARAM wParam;         /* expected value of wParam */
127     LPARAM lParam;         /* expected value of lParam */
128     int line;              /* source line where logged */
129     const char *descr;     /* description for trace output */
130     char output[512];      /* trace output */
131 };
132
133 /* Empty message sequence */
134 static const struct message WmEmptySeq[] =
135 {
136     { 0 }
137 };
138 /* CreateWindow (for overlapped window, not initially visible) (16/32) */
139 static const struct message WmCreateOverlappedSeq[] = {
140     { HCBT_CREATEWND, hook },
141     { WM_GETMINMAXINFO, sent },
142     { WM_NCCREATE, sent },
143     { WM_NCCALCSIZE, sent|wparam, 0 },
144     { 0x0093, sent|defwinproc|optional },
145     { 0x0094, sent|defwinproc|optional },
146     { EVENT_OBJECT_REORDER, winevent_hook|wparam|lparam, 0, 0 },
147     { WM_CREATE, sent },
148     { EVENT_OBJECT_CREATE, winevent_hook|wparam|lparam, 0, 0 },
149     { 0 }
150 };
151 /* SetWindowPos(SWP_SHOWWINDOW|SWP_NOSIZE|SWP_NOMOVE)
152  * for a not visible overlapped window.
153  */
154 static const struct message WmSWP_ShowOverlappedSeq[] = {
155     { WM_WINDOWPOSCHANGING, sent|wparam, SWP_SHOWWINDOW|SWP_NOSIZE|SWP_NOMOVE },
156     { EVENT_OBJECT_SHOW, winevent_hook|wparam|lparam, 0, 0 },
157     { WM_NCPAINT, sent|wparam|optional, 1 },
158     { WM_GETTEXT, sent|defwinproc|optional },
159     { WM_ERASEBKGND, sent|optional },
160     { HCBT_ACTIVATE, hook },
161     { EVENT_SYSTEM_FOREGROUND, winevent_hook|wparam|lparam, 0, 0 },
162     { WM_NOTIFYFORMAT, sent|optional },
163     { WM_QUERYUISTATE, sent|optional },
164     { WM_QUERYNEWPALETTE, sent|wparam|lparam|optional, 0, 0 },
165     { WM_WINDOWPOSCHANGING, sent|wparam|optional, SWP_NOSIZE|SWP_NOMOVE }, /* Win9x: SWP_NOSENDCHANGING */
166     { WM_ACTIVATEAPP, sent|wparam, 1 },
167     { WM_NCACTIVATE, sent },
168     { WM_GETTEXT, sent|defwinproc|optional },
169     { WM_ACTIVATE, sent|wparam, 1 },
170     { HCBT_SETFOCUS, hook },
171     { WM_IME_SETCONTEXT, sent|wparam|defwinproc|optional, 1 },
172     { WM_IME_NOTIFY, sent|wparam|defwinproc|optional, 2 },
173     { EVENT_OBJECT_FOCUS, winevent_hook|wparam|lparam, OBJID_CLIENT, 0 },
174     { WM_SETFOCUS, sent|wparam|defwinproc, 0 },
175     { WM_GETTEXT, sent|optional },
176     { WM_NCPAINT, sent|wparam|optional, 1 },
177     { WM_GETTEXT, sent|defwinproc|optional },
178     { WM_ERASEBKGND, sent|optional },
179     /* Win9x adds SWP_NOZORDER below */
180     { WM_WINDOWPOSCHANGED, sent|wparam, SWP_SHOWWINDOW|SWP_NOSIZE|SWP_NOMOVE|SWP_NOCLIENTSIZE|SWP_NOCLIENTMOVE },
181     { WM_GETTEXT, sent|optional },
182     { WM_NCCALCSIZE, sent|wparam|optional, 1 },
183     { WM_NCPAINT, sent|wparam|optional, 1 },
184     { WM_ERASEBKGND, sent|optional },
185     { EVENT_OBJECT_LOCATIONCHANGE, winevent_hook|wparam|lparam, 0, 0 },
186     { WM_SYNCPAINT, sent|optional },
187     { WM_GETTITLEBARINFOEX, sent|optional },
188     { WM_PAINT, sent|optional },
189     { WM_NCPAINT, sent|beginpaint|optional },
190     { WM_GETTEXT, sent|beginpaint|defwinproc|optional },
191     { WM_ERASEBKGND, sent|beginpaint|optional },
192     { 0 }
193 };
194 /* SetWindowPos(SWP_HIDEWINDOW|SWP_NOSIZE|SWP_NOMOVE)
195  * for a visible overlapped window.
196  */
197 static const struct message WmSWP_HideOverlappedSeq[] = {
198     { WM_WINDOWPOSCHANGING, sent|wparam, SWP_HIDEWINDOW|SWP_NOSIZE|SWP_NOMOVE },
199     { EVENT_OBJECT_HIDE, winevent_hook|wparam|lparam, 0, 0 },
200     { HCBT_ACTIVATE, hook|optional },
201     { WM_WINDOWPOSCHANGING, sent|wparam|optional, SWP_NOSIZE|SWP_NOMOVE },
202     { WM_ACTIVATEAPP, sent|wparam|optional, 1 },
203     { WM_NCACTIVATE, sent|optional },
204     { WM_ACTIVATE, sent|optional },
205     { WM_WINDOWPOSCHANGED, sent|wparam, SWP_HIDEWINDOW|SWP_NOSIZE|SWP_NOMOVE|SWP_NOCLIENTSIZE|SWP_NOCLIENTMOVE },
206     { 0 }
207 };
208
209 /* SetWindowPos(SWP_NOZORDER|SWP_NOACTIVATE|SWP_NOMOVE)
210  * for a visible overlapped window.
211  */
212 static const struct message WmSWP_ResizeSeq[] = {
213     { WM_WINDOWPOSCHANGING, sent|wparam, SWP_NOACTIVATE|SWP_NOMOVE },
214     { WM_GETMINMAXINFO, sent|defwinproc },
215     { WM_NCCALCSIZE, sent|wparam|optional, TRUE },
216     { WM_NCPAINT, sent|optional },
217     { WM_GETTEXT, sent|defwinproc|optional },
218     { WM_ERASEBKGND, sent|optional },
219     { WM_WINDOWPOSCHANGED, sent|wparam|optional, SWP_NOACTIVATE|SWP_NOMOVE|SWP_NOCLIENTMOVE },
220     { WM_SIZE, sent|defwinproc|optional },
221     { WM_NCCALCSIZE, sent|wparam|optional, TRUE },
222     { WM_NCPAINT, sent|optional },
223     { WM_GETTEXT, sent|defwinproc|optional },
224     { WM_ERASEBKGND, sent|optional },
225     { EVENT_OBJECT_LOCATIONCHANGE, winevent_hook|wparam|lparam, 0, 0 },
226     { EVENT_OBJECT_LOCATIONCHANGE, winevent_hook|wparam|lparam|optional, 0, 0 }, /* XP sends a duplicate */
227     { 0 }
228 };
229
230 /* SetWindowPos(SWP_NOZORDER|SWP_NOACTIVATE|SWP_NOMOVE)
231  * for a visible popup window.
232  */
233 static const struct message WmSWP_ResizePopupSeq[] = {
234     { WM_WINDOWPOSCHANGING, sent|wparam, SWP_NOACTIVATE|SWP_NOMOVE },
235     { WM_GETMINMAXINFO, sent|defwinproc|optional }, /* Win9x */
236     { WM_NCCALCSIZE, sent|wparam|optional, TRUE },
237     { WM_NCPAINT, sent|optional },
238     { WM_GETTEXT, sent|defwinproc|optional },
239     { WM_ERASEBKGND, sent|optional },
240     { WM_WINDOWPOSCHANGED, sent|wparam|optional, SWP_NOACTIVATE|SWP_NOMOVE|SWP_NOCLIENTMOVE },
241     { WM_SIZE, sent|defwinproc|wparam|optional, SIZE_RESTORED },
242     { WM_NCCALCSIZE, sent|wparam|optional, TRUE },
243     { WM_NCPAINT, sent|optional },
244     { WM_GETTEXT, sent|defwinproc|optional },
245     { WM_ERASEBKGND, sent|optional },
246     { EVENT_OBJECT_LOCATIONCHANGE, winevent_hook|wparam|lparam, 0, 0 },
247     { 0 }
248 };
249
250 /* SetWindowPos(SWP_NOZORDER|SWP_NOACTIVATE|SWP_NOSIZE)
251  * for a visible overlapped window.
252  */
253 static const struct message WmSWP_MoveSeq[] = {
254     { WM_WINDOWPOSCHANGING, sent|wparam, SWP_NOACTIVATE|SWP_NOSIZE },
255     { WM_NCPAINT, sent|optional },
256     { WM_GETTEXT, sent|defwinproc|optional },
257     { WM_ERASEBKGND, sent|optional },
258     { WM_WINDOWPOSCHANGED, sent|wparam, SWP_NOACTIVATE|SWP_NOSIZE|SWP_NOCLIENTSIZE },
259     { WM_MOVE, sent|defwinproc|wparam, 0 },
260     { EVENT_OBJECT_LOCATIONCHANGE, winevent_hook|wparam|lparam, 0, 0 },
261     { 0 }
262 };
263 /* Resize with SetWindowPos(SWP_NOZORDER)
264  * for a visible overlapped window
265  * SWP_NOZORDER is stripped by the logging code
266  */
267 static const struct message WmSWP_ResizeNoZOrder[] = {
268     { WM_WINDOWPOSCHANGING, sent|wparam, /*SWP_NOZORDER|*/SWP_NOACTIVATE },
269     { WM_GETMINMAXINFO, sent|defwinproc },
270     { WM_NCCALCSIZE, sent|wparam|optional, 1 },
271     { WM_NCPAINT, sent|optional },
272     { WM_GETTEXT, sent|defwinproc|optional },
273     { WM_ERASEBKGND, sent|optional },
274     { WM_WINDOWPOSCHANGED, sent|wparam|optional, /*SWP_NOZORDER|*/SWP_NOACTIVATE, 0,
275       SWP_NOMOVE|SWP_NOCLIENTMOVE|SWP_NOSIZE|SWP_NOCLIENTSIZE },
276     { WM_MOVE, sent|defwinproc|optional },
277     { WM_SIZE, sent|defwinproc|optional },
278     { WM_NCCALCSIZE, sent|wparam|optional, 1 }, /* Win9x doesn't send it */
279     { WM_NCPAINT, sent|optional }, /* Win9x doesn't send it */
280     { WM_GETTEXT, sent|defwinproc|optional }, /* Win9x doesn't send it */
281     { WM_ERASEBKGND, sent|optional }, /* Win9x doesn't send it */
282     { EVENT_OBJECT_LOCATIONCHANGE, winevent_hook|wparam|lparam, 0, 0 },
283     { EVENT_OBJECT_LOCATIONCHANGE, winevent_hook|wparam|lparam, 0, 0 },
284     { 0 }
285 };
286
287 /* Switch visible mdi children */
288 static const struct message WmSwitchChild[] = {
289     /* Switch MDI child */
290     { WM_MDIACTIVATE, sent },/* in the MDI client */
291     { WM_WINDOWPOSCHANGING, sent|wparam,SWP_NOSIZE|SWP_NOMOVE },/* in the 1st MDI child */
292     { EVENT_OBJECT_REORDER, winevent_hook|wparam|lparam, OBJID_CLIENT, 0 },
293     { WM_CHILDACTIVATE, sent },/* in the 1st MDI child */
294     /* Deactivate 2nd MDI child */
295     { WM_NCACTIVATE, sent|wparam|defwinproc, 0 }, /* in the 2nd MDI child */
296     { WM_MDIACTIVATE, sent|defwinproc }, /* in the 2nd MDI child */
297     { HCBT_MINMAX, hook|lparam, 0, SW_MAXIMIZE },
298     /* Preparing for maximize and maximize the 1st MDI child */
299     { WM_GETMINMAXINFO, sent|defwinproc }, /* in the 1st MDI child */
300     { WM_WINDOWPOSCHANGING, sent|wparam|defwinproc, SWP_FRAMECHANGED|SWP_STATECHANGED }, /* in the 1st MDI child */
301     { WM_NCCALCSIZE, sent|wparam|defwinproc, 1 }, /* in the 1st MDI child */
302     { WM_CHILDACTIVATE, sent|defwinproc }, /* in the 1st MDI child */
303     { WM_WINDOWPOSCHANGED, sent|wparam|defwinproc, SWP_FRAMECHANGED|SWP_NOMOVE|SWP_NOCLIENTMOVE|SWP_STATECHANGED }, /* in the 1st MDI child */
304     { WM_SIZE, sent|defwinproc|wparam, SIZE_MAXIMIZED }, /* in the 1st MDI child */
305     /* Lock redraw 2nd MDI child */
306     { WM_SETREDRAW, sent|wparam|defwinproc, 0 }, /* in the 2nd MDI child */
307     { HCBT_MINMAX, hook|lparam, 0, SW_NORMALNA },
308     /* Restore 2nd MDI child */
309     { WM_WINDOWPOSCHANGING, sent|wparam|defwinproc, SWP_SHOWWINDOW|SWP_NOACTIVATE|SWP_FRAMECHANGED|SWP_STATECHANGED },/* in the 2nd MDI child */
310     { WM_NCCALCSIZE, sent|wparam|defwinproc, 1 },/* in the 2nd MDI child */
311     { EVENT_OBJECT_SHOW, winevent_hook|wparam|lparam, 0, 0 }, /* in the 2nd MDI child */
312     { WM_WINDOWPOSCHANGED, sent|wparam|defwinproc, SWP_SHOWWINDOW|SWP_NOACTIVATE|SWP_FRAMECHANGED|SWP_NOMOVE|SWP_NOCLIENTMOVE|SWP_STATECHANGED }, /* in the 2nd MDI child */
313     { WM_SIZE, sent|defwinproc|wparam, SIZE_RESTORED }, /* in the 2nd MDI child */
314     { EVENT_OBJECT_LOCATIONCHANGE, winevent_hook|wparam|lparam, 0, 0 }, /* in the 2nd MDI child */
315     /* Redraw 2nd MDI child */
316     { WM_SETREDRAW, sent|wparam|defwinproc, 1 },/* in the 2nd MDI child */
317     /* Redraw MDI frame */
318     { WM_WINDOWPOSCHANGING, sent|wparam, SWP_NOACTIVATE|SWP_FRAMECHANGED|SWP_NOSIZE|SWP_NOMOVE },/* in MDI frame */
319     { WM_NCCALCSIZE, sent|wparam, 1 },/* in MDI frame */
320     { WM_WINDOWPOSCHANGED, sent|wparam, SWP_NOACTIVATE|SWP_FRAMECHANGED|SWP_NOSIZE|SWP_NOMOVE|SWP_NOCLIENTSIZE|SWP_NOCLIENTMOVE}, /* in MDI frame */
321     { EVENT_OBJECT_LOCATIONCHANGE, winevent_hook|wparam|lparam, 0, 0 }, /* in MDI frame */
322     { EVENT_OBJECT_LOCATIONCHANGE, winevent_hook|wparam|lparam, 0, 0 }, /* in the 1st MDI child */
323     { WM_WINDOWPOSCHANGING, sent|wparam|defwinproc, SWP_NOACTIVATE|SWP_NOSIZE|SWP_NOMOVE }, /* in the 1st MDI child */
324     { WM_NCACTIVATE, sent|wparam|defwinproc, 1 }, /* in the 1st MDI child */
325     { HCBT_SETFOCUS, hook },
326     { WM_KILLFOCUS, sent|defwinproc }, /* in the 2nd MDI child */
327     { WM_IME_SETCONTEXT, sent|wparam|defwinproc|optional, 0 },/* in the 1st MDI child */
328     { WM_IME_SETCONTEXT, sent|wparam|optional, 1 }, /* in MDI client */
329     { EVENT_OBJECT_FOCUS, winevent_hook|wparam|lparam, OBJID_CLIENT, 0 },
330     { WM_SETFOCUS, sent },/* in the MDI client */
331     { HCBT_SETFOCUS, hook },
332     { WM_KILLFOCUS, sent },/* in the MDI client */
333     { WM_IME_SETCONTEXT, sent|wparam|optional, 0 }, /* in MDI client */
334     { WM_IME_SETCONTEXT, sent|wparam|defwinproc|optional, 1 }, /* in the 1st MDI child */
335     { EVENT_OBJECT_FOCUS, winevent_hook|wparam|lparam, OBJID_CLIENT, 0 },
336     { WM_SETFOCUS, sent|defwinproc }, /* in the 1st MDI child */
337     { WM_MDIACTIVATE, sent|defwinproc },/* in the 1st MDI child */
338     { WM_WINDOWPOSCHANGED, sent|wparam, SWP_NOSIZE|SWP_NOMOVE|SWP_NOCLIENTSIZE|SWP_NOCLIENTMOVE }, /* in the 1st MDI child */
339     { 0 }
340 };
341
342 /* Switch visible not maximized mdi children */
343 static const struct message WmSwitchNotMaximizedChild[] = {
344     /* Switch not maximized MDI child */
345     { WM_MDIACTIVATE, sent },/* in the MDI client */
346     { WM_WINDOWPOSCHANGING, sent|wparam,SWP_NOSIZE|SWP_NOMOVE },/* in the 2nd MDI child */
347     { EVENT_OBJECT_REORDER, winevent_hook|wparam|lparam, OBJID_CLIENT, 0 },
348     { WM_CHILDACTIVATE, sent },/* in the 2nd MDI child */
349     /* Deactivate 1st MDI child */
350     { WM_NCACTIVATE, sent|wparam|defwinproc, 0 }, /* in the 1st MDI child */
351     { WM_MDIACTIVATE, sent|defwinproc }, /* in the 1st MDI child */
352     /* Activate 2nd MDI child */
353     { WM_WINDOWPOSCHANGING, sent|wparam|defwinproc, SWP_NOSIZE|SWP_NOMOVE|SWP_NOACTIVATE}, /* in the 2nd MDI child */
354     { WM_NCACTIVATE, sent|wparam|defwinproc, 1 }, /* in the 2nd MDI child */
355     { HCBT_SETFOCUS, hook }, /* in the 1st MDI child */
356     { WM_KILLFOCUS, sent|defwinproc }, /* in the 1st MDI child */
357     { WM_IME_SETCONTEXT, sent|defwinproc|optional }, /* in the 1st MDI child */
358     { WM_IME_SETCONTEXT, sent|optional }, /* in the  MDI client */
359     { WM_SETFOCUS, sent, 0 }, /* in the  MDI client */
360     { HCBT_SETFOCUS, hook },
361     { WM_KILLFOCUS, sent }, /* in the  MDI client */
362     { WM_IME_SETCONTEXT, sent|optional }, /* in the  MDI client */
363     { WM_IME_SETCONTEXT, sent|defwinproc|optional  }, /* in the 1st MDI child */
364     { WM_SETFOCUS, sent|defwinproc }, /* in the 2nd MDI child */
365     { WM_MDIACTIVATE, sent|defwinproc }, /* in the 2nd MDI child */
366     { WM_WINDOWPOSCHANGED, sent|wparam, SWP_NOSIZE|SWP_NOMOVE|SWP_NOCLIENTSIZE|SWP_NOCLIENTMOVE}, /* in the 2nd MDI child */
367     { 0 }
368 };
369
370
371 /* SetWindowPos(SWP_NOSIZE|SWP_NOMOVE|SWP_NOACTIVATE|
372                 SWP_NOZORDER|SWP_FRAMECHANGED)
373  * for a visible overlapped window with WS_CLIPCHILDREN style set.
374  */
375 static const struct message WmSWP_FrameChanged_clip[] = {
376     { WM_WINDOWPOSCHANGING, sent|wparam|parent, SWP_NOSIZE|SWP_NOMOVE|SWP_NOACTIVATE|SWP_FRAMECHANGED },
377     { WM_NCCALCSIZE, sent|wparam|parent, 1 },
378     { WM_NCPAINT, sent|parent|optional }, /* wparam != 1 */
379     { WM_GETTEXT, sent|parent|defwinproc|optional },
380     { WM_ERASEBKGND, sent|parent|optional }, /* FIXME: remove optional once Wine is fixed */
381     { WM_NCPAINT, sent }, /* wparam != 1 */
382     { WM_ERASEBKGND, sent },
383     { WM_WINDOWPOSCHANGED, sent|wparam|parent, SWP_NOSIZE|SWP_NOMOVE|SWP_NOACTIVATE|SWP_FRAMECHANGED|SWP_NOCLIENTSIZE|SWP_NOCLIENTMOVE },
384     { EVENT_OBJECT_LOCATIONCHANGE, winevent_hook|wparam|lparam, 0, 0 },
385     { WM_PAINT, sent },
386     { 0 }
387 };
388 /* SetWindowPos(SWP_NOSIZE|SWP_NOMOVE|SWP_DEFERERASE|SWP_NOACTIVATE|
389                 SWP_NOZORDER|SWP_FRAMECHANGED)
390  * for a visible overlapped window.
391  */
392 static const struct message WmSWP_FrameChangedDeferErase[] = {
393     { WM_WINDOWPOSCHANGING, sent|wparam|parent, SWP_NOSIZE|SWP_NOMOVE|SWP_DEFERERASE|SWP_NOACTIVATE|SWP_FRAMECHANGED },
394     { WM_NCCALCSIZE, sent|wparam|parent, 1 },
395     { WM_WINDOWPOSCHANGED, sent|wparam|parent, SWP_NOSIZE|SWP_NOMOVE|SWP_DEFERERASE|SWP_NOACTIVATE|SWP_FRAMECHANGED|SWP_NOCLIENTSIZE|SWP_NOCLIENTMOVE },
396     { EVENT_OBJECT_LOCATIONCHANGE, winevent_hook|wparam|lparam, 0, 0 },
397     { WM_PAINT, sent|parent|optional },
398     { WM_NCPAINT, sent|beginpaint|parent|optional }, /* wparam != 1 */
399     { WM_GETTEXT, sent|beginpaint|parent|defwinproc|optional },
400     { WM_PAINT, sent },
401     { WM_NCPAINT, sent|beginpaint }, /* wparam != 1 */
402     { WM_ERASEBKGND, sent|beginpaint|optional },
403     { 0 }
404 };
405
406 /* SetWindowPos(SWP_NOSIZE|SWP_NOMOVE|SWP_NOACTIVATE|
407                 SWP_NOZORDER|SWP_FRAMECHANGED)
408  * for a visible overlapped window without WS_CLIPCHILDREN style set.
409  */
410 static const struct message WmSWP_FrameChanged_noclip[] = {
411     { WM_WINDOWPOSCHANGING, sent|wparam|parent, SWP_NOSIZE|SWP_NOMOVE|SWP_NOACTIVATE|SWP_FRAMECHANGED },
412     { WM_NCCALCSIZE, sent|wparam|parent, 1 },
413     { WM_NCPAINT, sent|parent|optional }, /* wparam != 1 */
414     { WM_GETTEXT, sent|parent|defwinproc|optional },
415     { WM_ERASEBKGND, sent|parent|optional },
416     { WM_WINDOWPOSCHANGED, sent|wparam|parent, SWP_NOSIZE|SWP_NOMOVE|SWP_NOACTIVATE|SWP_FRAMECHANGED|SWP_NOCLIENTSIZE|SWP_NOCLIENTMOVE },
417     { EVENT_OBJECT_LOCATIONCHANGE, winevent_hook|wparam|lparam, 0, 0 },
418     { WM_PAINT, sent },
419     { WM_NCPAINT, sent|beginpaint }, /* wparam != 1 */
420     { WM_ERASEBKGND, sent|beginpaint|optional },
421     { 0 }
422 };
423
424 /* ShowWindow(SW_SHOW) for a not visible overlapped window */
425 static const struct message WmShowOverlappedSeq[] = {
426     { WM_SHOWWINDOW, sent|wparam, 1 },
427     { WM_NCPAINT, sent|wparam|optional, 1 },
428     { WM_WINDOWPOSCHANGING, sent|wparam, SWP_SHOWWINDOW|SWP_NOSIZE|SWP_NOMOVE },
429     { EVENT_OBJECT_SHOW, winevent_hook|wparam|lparam, 0, 0 },
430     { WM_NCPAINT, sent|wparam|optional, 1 },
431     { WM_GETTEXT, sent|defwinproc|optional },
432     { WM_ERASEBKGND, sent|optional },
433     { HCBT_ACTIVATE, hook },
434     { EVENT_SYSTEM_FOREGROUND, winevent_hook|wparam|lparam, 0, 0 },
435     { WM_QUERYNEWPALETTE, sent|wparam|lparam|optional, 0, 0 },
436     { WM_WINDOWPOSCHANGING, sent|wparam|optional, SWP_NOSIZE|SWP_NOMOVE },
437     { WM_NCPAINT, sent|wparam|optional, 1 },
438     { WM_ACTIVATEAPP, sent|wparam, 1 },
439     { WM_NCACTIVATE, sent|wparam, 1 },
440     { WM_GETTEXT, sent|defwinproc|optional },
441     { WM_ACTIVATE, sent|wparam, 1 },
442     { HCBT_SETFOCUS, hook },
443     { WM_IME_SETCONTEXT, sent|wparam|defwinproc|optional, 1 },
444     { WM_IME_NOTIFY, sent|wparam|defwinproc|optional, 2 },
445     { EVENT_OBJECT_FOCUS, winevent_hook|wparam|lparam, OBJID_CLIENT, 0 },
446     { WM_SETFOCUS, sent|wparam|defwinproc, 0 },
447     { WM_GETTEXT, sent|optional },
448     { WM_NCPAINT, sent|wparam|optional, 1 },
449     { WM_GETTEXT, sent|defwinproc|optional },
450     { WM_ERASEBKGND, sent|optional },
451     /* Win9x adds SWP_NOZORDER below */
452     { WM_WINDOWPOSCHANGED, sent|wparam, SWP_SHOWWINDOW|SWP_NOSIZE|SWP_NOMOVE|SWP_NOCLIENTSIZE|SWP_NOCLIENTMOVE },
453     { WM_NCCALCSIZE, sent|optional },
454     { WM_GETTEXT, sent|optional },
455     { WM_NCPAINT, sent|optional },
456     { WM_ERASEBKGND, sent|optional },
457     { WM_SYNCPAINT, sent|optional },
458 #if 0 /* CreateWindow/ShowWindow(SW_SHOW) also generates WM_SIZE/WM_MOVE
459        * messages. Does that mean that CreateWindow doesn't set initial
460        * window dimensions for overlapped windows?
461        */
462     { WM_SIZE, sent },
463     { WM_MOVE, sent },
464 #endif
465     { WM_PAINT, sent|optional },
466     { WM_NCPAINT, sent|beginpaint|optional },
467     { 0 }
468 };
469 /* ShowWindow(SW_SHOWMAXIMIZED) for a not visible overlapped window */
470 static const struct message WmShowMaxOverlappedSeq[] = {
471     { HCBT_MINMAX, hook|lparam, 0, SW_MAXIMIZE },
472     { WM_GETMINMAXINFO, sent },
473     { WM_WINDOWPOSCHANGING, sent|wparam, SWP_SHOWWINDOW|SWP_FRAMECHANGED|SWP_STATECHANGED },
474     { WM_GETMINMAXINFO, sent|defwinproc },
475     { WM_NCCALCSIZE, sent|wparam, TRUE },
476     { EVENT_OBJECT_SHOW, winevent_hook|wparam|lparam, 0, 0 },
477     { HCBT_ACTIVATE, hook },
478     { EVENT_SYSTEM_FOREGROUND, winevent_hook|wparam|lparam, 0, 0 },
479     { WM_QUERYNEWPALETTE, sent|wparam|lparam|optional, 0, 0 },
480     { WM_WINDOWPOSCHANGING, sent|wparam|optional, SWP_NOSIZE|SWP_NOMOVE },
481     { WM_ACTIVATEAPP, sent|wparam, 1 },
482     { WM_NCACTIVATE, sent|wparam, 1 },
483     { WM_GETTEXT, sent|defwinproc|optional },
484     { WM_ACTIVATE, sent|wparam, 1 },
485     { HCBT_SETFOCUS, hook },
486     { WM_IME_SETCONTEXT, sent|wparam|defwinproc|optional, 1 },
487     { WM_IME_NOTIFY, sent|wparam|defwinproc|optional, 2 },
488     { EVENT_OBJECT_FOCUS, winevent_hook|wparam|lparam, OBJID_CLIENT, 0 },
489     { WM_SETFOCUS, sent|wparam|defwinproc, 0 },
490     { WM_GETTEXT, sent|optional },
491     { WM_NCPAINT, sent|wparam|optional, 1 },
492     { WM_GETTEXT, sent|defwinproc|optional },
493     { WM_ERASEBKGND, sent|optional },
494     /* Win9x adds SWP_NOZORDER below */
495     { WM_WINDOWPOSCHANGED, sent|wparam, SWP_SHOWWINDOW|SWP_FRAMECHANGED|SWP_STATECHANGED },
496     { WM_MOVE, sent|defwinproc },
497     { WM_SIZE, sent|defwinproc|wparam, SIZE_MAXIMIZED },
498     { WM_GETTEXT, sent|optional },
499     { WM_NCCALCSIZE, sent|optional },
500     { WM_NCPAINT, sent|optional },
501     { WM_ERASEBKGND, sent|optional },
502     { EVENT_OBJECT_LOCATIONCHANGE, winevent_hook|wparam|lparam, 0, 0 },
503     { EVENT_OBJECT_LOCATIONCHANGE, winevent_hook|wparam|lparam, 0, 0 },
504     { WM_SYNCPAINT, sent|optional },
505     { WM_GETTITLEBARINFOEX, sent|optional },
506     { WM_PAINT, sent|optional },
507     { WM_NCPAINT, sent|beginpaint|optional },
508     { WM_ERASEBKGND, sent|beginpaint|optional },
509     { 0 }
510 };
511 /* ShowWindow(SW_RESTORE) for a not visible maximized overlapped window */
512 static const struct message WmShowRestoreMaxOverlappedSeq[] = {
513     { HCBT_MINMAX, hook|lparam, 0, SW_RESTORE },
514     { WM_GETTEXT, sent|optional },
515     { WM_WINDOWPOSCHANGING, sent|wparam, SWP_FRAMECHANGED|SWP_STATECHANGED },
516     { WM_GETMINMAXINFO, sent|defwinproc },
517     { WM_NCCALCSIZE, sent|wparam, TRUE },
518     { WM_NCPAINT, sent|optional },
519     { WM_GETTEXT, sent|defwinproc|optional },
520     { WM_ERASEBKGND, sent|optional },
521     { WM_WINDOWPOSCHANGED, sent|wparam, SWP_FRAMECHANGED|SWP_STATECHANGED, 0, SWP_NOCLIENTSIZE|SWP_NOCLIENTMOVE|SWP_NOSIZE|SWP_NOMOVE },
522     { WM_MOVE, sent|defwinproc|optional },
523     { WM_SIZE, sent|defwinproc|wparam, SIZE_RESTORED },
524     { WM_NCCALCSIZE, sent|wparam|optional, TRUE },
525     { WM_NCPAINT, sent|optional },
526     { WM_ERASEBKGND, sent|optional },
527     { WM_PAINT, sent|optional },
528     { WM_GETTITLEBARINFOEX, sent|optional },
529     { WM_NCPAINT, sent|beginpaint|optional },
530     { WM_ERASEBKGND, sent|beginpaint|optional },
531     { 0 }
532 };
533 /* ShowWindow(SW_RESTORE) for a not visible minimized overlapped window */
534 static const struct message WmShowRestoreMinOverlappedSeq[] = {
535     { HCBT_MINMAX, hook|lparam, 0, SW_RESTORE },
536     { WM_QUERYOPEN, sent|optional },
537     { WM_GETTEXT, sent|optional },
538     { WM_WINDOWPOSCHANGING, sent|wparam, SWP_FRAMECHANGED|SWP_STATECHANGED|SWP_NOCOPYBITS },
539     { WM_GETMINMAXINFO, sent|defwinproc },
540     { WM_NCCALCSIZE, sent|wparam, TRUE },
541     { HCBT_ACTIVATE, hook },
542     { WM_QUERYNEWPALETTE, sent|wparam|lparam|optional, 0, 0 },
543     { WM_WINDOWPOSCHANGING, sent|wparam|optional, SWP_NOSIZE|SWP_NOMOVE },
544     { WM_ACTIVATEAPP, sent|wparam, 1 },
545     { WM_NCACTIVATE, sent|wparam, 1 },
546     { WM_GETTEXT, sent|defwinproc|optional },
547     { WM_ACTIVATE, sent|wparam, 1 },
548     { HCBT_SETFOCUS, hook },
549     { WM_IME_SETCONTEXT, sent|wparam|defwinproc|optional, 1 },
550     { WM_IME_NOTIFY, sent|wparam|defwinproc|optional, 2 },
551     { WM_SETFOCUS, sent|wparam|defwinproc, 0 },
552     { WM_GETTEXT, sent|optional },
553     { WM_NCPAINT, sent|wparam|optional, 1 },
554     { WM_GETTEXT, sent|defwinproc|optional },
555     { WM_ERASEBKGND, sent },
556     { WM_WINDOWPOSCHANGED, sent|wparam, SWP_STATECHANGED|SWP_FRAMECHANGED|SWP_NOCOPYBITS },
557     { WM_MOVE, sent|defwinproc },
558     { WM_SIZE, sent|defwinproc|wparam, SIZE_RESTORED },
559     { WM_NCCALCSIZE, sent|wparam|optional, TRUE },
560     { WM_NCPAINT, sent|wparam|optional, 1 },
561     { WM_ERASEBKGND, sent|optional },
562     { WM_ACTIVATE, sent|wparam, 1 },
563     { WM_GETTEXT, sent|optional },
564     { WM_PAINT, sent|optional },
565     { WM_GETTITLEBARINFOEX, sent|optional },
566     { WM_NCPAINT, sent|beginpaint|optional },
567     { WM_ERASEBKGND, sent|beginpaint|optional },
568     { 0 }
569 };
570 /* ShowWindow(SW_SHOWMINIMIZED) for a not visible overlapped window */
571 static const struct message WmShowMinOverlappedSeq[] = {
572     { HCBT_MINMAX, hook|lparam, 0, SW_MINIMIZE },
573     { HCBT_SETFOCUS, hook },
574     { EVENT_OBJECT_FOCUS, winevent_hook|wparam|lparam, OBJID_CLIENT, 0 },
575     { WM_KILLFOCUS, sent },
576     { WM_IME_SETCONTEXT, sent|wparam|optional, 0 },
577     { WM_IME_NOTIFY, sent|wparam|optional|defwinproc, 1 },
578     { WM_GETTEXT, sent|optional },
579     { WM_WINDOWPOSCHANGING, sent|wparam, SWP_NOCOPYBITS|SWP_SHOWWINDOW|SWP_FRAMECHANGED|SWP_NOACTIVATE|SWP_STATECHANGED },
580     { WM_GETMINMAXINFO, sent|defwinproc },
581     { WM_NCCALCSIZE, sent|wparam, TRUE },
582     { EVENT_OBJECT_REORDER, winevent_hook|wparam|lparam, 0, 0 },
583     { WM_NCPAINT, sent|optional },
584     { WM_GETTEXT, sent|defwinproc|optional },
585     { WM_WINDOWPOSCHANGED, sent },
586     { WM_MOVE, sent|defwinproc },
587     { WM_SIZE, sent|defwinproc|wparam|lparam, SIZE_MINIMIZED, 0 },
588     { WM_NCCALCSIZE, sent|optional },
589     { EVENT_OBJECT_LOCATIONCHANGE, winevent_hook|wparam|lparam, 0, 0 },
590     { EVENT_OBJECT_LOCATIONCHANGE, winevent_hook|wparam|lparam, 0, 0 },
591     { EVENT_SYSTEM_MINIMIZESTART, winevent_hook|wparam|lparam, 0, 0 },
592     { WM_NCACTIVATE, sent|wparam, 0 },
593     { WM_GETTEXT, sent|defwinproc|optional },
594     { WM_ACTIVATE, sent },
595     { WM_ACTIVATEAPP, sent|wparam, 0 },
596
597     /* Vista sometimes restores the window right away... */
598     { WM_SYSCOMMAND, sent|optional|wparam, SC_RESTORE },
599     { HCBT_SYSCOMMAND, hook|optional|wparam, SC_RESTORE },
600     { HCBT_MINMAX, hook|optional|lparam, 0, SW_RESTORE },
601     { WM_QUERYOPEN, sent|optional },
602     { WM_WINDOWPOSCHANGING, sent|optional|wparam, SWP_FRAMECHANGED|SWP_STATECHANGED },
603     { WM_GETMINMAXINFO, sent|optional|defwinproc },
604     { WM_NCCALCSIZE, sent|optional|wparam, TRUE },
605     { HCBT_ACTIVATE, hook|optional },
606     { WM_ACTIVATEAPP, sent|optional|wparam, 1 },
607     { WM_NCACTIVATE, sent|optional },
608     { WM_GETTEXT, sent|optional },
609     { WM_ACTIVATE, sent|optional|wparam, 1 },
610     { HCBT_SETFOCUS, hook|optional },
611     { WM_IME_SETCONTEXT, sent|wparam|defwinproc|optional, 1 },
612     { WM_IME_NOTIFY, sent|wparam|defwinproc|optional, 2 },
613     { WM_SETFOCUS, sent|optional },
614     { WM_NCPAINT, sent|optional },
615     { WM_GETTEXT, sent|defwinproc|optional },
616     { WM_ERASEBKGND, sent|optional },
617     { WM_WINDOWPOSCHANGED, sent|optional|wparam, SWP_FRAMECHANGED|SWP_STATECHANGED },
618     { WM_MOVE, sent|defwinproc|optional },
619     { WM_SIZE, sent|defwinproc|optional|wparam, SIZE_RESTORED },
620     { WM_ACTIVATE, sent|optional|wparam, 1 },
621     { WM_SYSCOMMAND, sent|optional|wparam, SC_RESTORE },
622     { HCBT_SYSCOMMAND, hook|optional|wparam, SC_RESTORE },
623
624     { WM_PAINT, sent|optional },
625     { WM_NCPAINT, sent|beginpaint|optional },
626     { WM_ERASEBKGND, sent|beginpaint|optional },
627     { 0 }
628 };
629 /* ShowWindow(SW_HIDE) for a visible overlapped window */
630 static const struct message WmHideOverlappedSeq[] = {
631     { WM_SHOWWINDOW, sent|wparam, 0 },
632     { WM_WINDOWPOSCHANGING, sent|wparam, SWP_HIDEWINDOW|SWP_NOSIZE|SWP_NOMOVE },
633     { EVENT_OBJECT_HIDE, winevent_hook|wparam|lparam, 0, 0 },
634     { WM_WINDOWPOSCHANGED, sent|wparam, SWP_HIDEWINDOW|SWP_NOSIZE|SWP_NOMOVE|SWP_NOCLIENTSIZE|SWP_NOCLIENTMOVE },
635     { WM_SIZE, sent|optional }, /* XP doesn't send it */
636     { WM_MOVE, sent|optional }, /* XP doesn't send it */
637     { WM_NCACTIVATE, sent|wparam|optional, 0 },
638     { WM_ACTIVATE, sent|wparam|optional, 0 },
639     { WM_ACTIVATEAPP, sent|wparam|optional, 0 },
640     { HCBT_SETFOCUS, hook|optional },
641     { WM_KILLFOCUS, sent|wparam, 0 },
642     { WM_IME_SETCONTEXT, sent|wparam|optional, 0 },
643     { WM_IME_NOTIFY, sent|wparam|optional|defwinproc, 1 },
644     { 0 }
645 };
646 /* DestroyWindow for a visible overlapped window */
647 static const struct message WmDestroyOverlappedSeq[] = {
648     { HCBT_DESTROYWND, hook },
649     { 0x0090, sent|optional },
650     { WM_WINDOWPOSCHANGING, sent|wparam, SWP_HIDEWINDOW|SWP_NOACTIVATE|SWP_NOSIZE|SWP_NOMOVE },
651     { EVENT_OBJECT_HIDE, winevent_hook|wparam|lparam, 0, 0 },
652     { 0x0090, sent|optional },
653     { WM_WINDOWPOSCHANGED, sent|wparam, SWP_HIDEWINDOW|SWP_NOACTIVATE|SWP_NOSIZE|SWP_NOMOVE|SWP_NOCLIENTSIZE|SWP_NOCLIENTMOVE },
654     { WM_NCACTIVATE, sent|optional|wparam, 0 },
655     { WM_ACTIVATE, sent|optional },
656     { WM_ACTIVATEAPP, sent|optional|wparam, 0 },
657     { WM_KILLFOCUS, sent|optional|wparam, 0 },
658     { WM_IME_SETCONTEXT, sent|wparam|optional, 0 },
659     { WM_IME_NOTIFY, sent|wparam|optional|defwinproc, 1 },
660     { EVENT_OBJECT_DESTROY, winevent_hook|wparam|lparam, 0, 0 },
661     { WM_DESTROY, sent },
662     { WM_NCDESTROY, sent },
663     { 0 }
664 };
665 /* CreateWindow(WS_MAXIMIZE|WS_VISIBLE) for popup window */
666 static const struct message WmCreateMaxPopupSeq[] = {
667     { HCBT_CREATEWND, hook },
668     { WM_NCCREATE, sent },
669     { WM_NCCALCSIZE, sent|wparam, 0 },
670     { WM_CREATE, sent },
671     { EVENT_OBJECT_CREATE, winevent_hook|wparam|lparam, 0, 0 },
672     { WM_SIZE, sent|wparam, SIZE_RESTORED },
673     { WM_MOVE, sent },
674     { HCBT_MINMAX, hook|lparam, 0, SW_MAXIMIZE },
675     { WM_GETMINMAXINFO, sent },
676     { WM_WINDOWPOSCHANGING, sent|wparam, SWP_NOACTIVATE|SWP_FRAMECHANGED|SWP_STATECHANGED },
677     { WM_NCCALCSIZE, sent|wparam, TRUE },
678     { WM_WINDOWPOSCHANGED, sent|wparam, SWP_NOACTIVATE|SWP_FRAMECHANGED|SWP_NOREDRAW|SWP_STATECHANGED },
679     { WM_MOVE, sent|defwinproc },
680     { WM_SIZE, sent|defwinproc|wparam, SIZE_MAXIMIZED },
681     { EVENT_OBJECT_LOCATIONCHANGE, winevent_hook|wparam|lparam, 0, 0 },
682     { WM_SHOWWINDOW, sent|wparam, 1 },
683     { WM_WINDOWPOSCHANGING, sent|wparam, SWP_SHOWWINDOW|SWP_NOSIZE|SWP_NOMOVE },
684     { EVENT_OBJECT_SHOW, winevent_hook|wparam|lparam, 0, 0 },
685     { HCBT_ACTIVATE, hook },
686     { EVENT_SYSTEM_FOREGROUND, winevent_hook|wparam|lparam, 0, 0 },
687     { WM_QUERYNEWPALETTE, sent|wparam|lparam|optional, 0, 0 },
688     { WM_WINDOWPOSCHANGING, sent|wparam|optional, SWP_NOSIZE|SWP_NOMOVE },
689     { WM_NCPAINT, sent|wparam|optional, 1 },
690     { WM_ERASEBKGND, sent|optional },
691     { WM_WINDOWPOSCHANGED, sent|wparam|optional, SWP_NOCLIENTMOVE|SWP_NOCLIENTSIZE|SWP_NOMOVE|SWP_NOSIZE },
692     { WM_ACTIVATEAPP, sent|wparam, 1 },
693     { WM_NCACTIVATE, sent },
694     { WM_ACTIVATE, sent|wparam, 1 },
695     { HCBT_SETFOCUS, hook },
696     { WM_IME_SETCONTEXT, sent|wparam|defwinproc|optional, 1 },
697     { WM_IME_NOTIFY, sent|wparam|defwinproc|optional, 2 },
698     { EVENT_OBJECT_FOCUS, winevent_hook|wparam|lparam, OBJID_CLIENT, 0 },
699     { WM_SETFOCUS, sent|wparam|defwinproc, 0 },
700     { WM_GETTEXT, sent|optional },
701     { WM_SYNCPAINT, sent|wparam|optional, 4 },
702     { WM_NCPAINT, sent|wparam|optional, 1 },
703     { WM_ERASEBKGND, sent|optional },
704     { WM_NCPAINT, sent|wparam|defwinproc|optional, 1 },
705     { WM_ERASEBKGND, sent|defwinproc|optional },
706     { WM_WINDOWPOSCHANGED, sent|wparam, SWP_NOCLIENTMOVE|SWP_NOCLIENTSIZE|SWP_SHOWWINDOW|SWP_NOMOVE|SWP_NOSIZE },
707     { 0 }
708 };
709 /* CreateWindow(WS_MAXIMIZE) for popup window, not initially visible */
710 static const struct message WmCreateInvisibleMaxPopupSeq[] = {
711     { HCBT_CREATEWND, hook },
712     { WM_NCCREATE, sent },
713     { WM_NCCALCSIZE, sent|wparam, 0 },
714     { WM_CREATE, sent },
715     { EVENT_OBJECT_CREATE, winevent_hook|wparam|lparam, 0, 0 },
716     { WM_SIZE, sent|wparam, SIZE_RESTORED },
717     { WM_MOVE, sent },
718     { HCBT_MINMAX, hook|lparam, 0, SW_MAXIMIZE },
719     { WM_GETMINMAXINFO, sent },
720     { WM_WINDOWPOSCHANGING, sent|wparam, SWP_NOACTIVATE|SWP_FRAMECHANGED|SWP_STATECHANGED  },
721     { WM_NCCALCSIZE, sent|wparam, TRUE },
722     { WM_WINDOWPOSCHANGED, sent|wparam, SWP_NOACTIVATE|SWP_FRAMECHANGED|SWP_NOREDRAW|SWP_STATECHANGED },
723     { WM_MOVE, sent|defwinproc },
724     { WM_SIZE, sent|defwinproc|wparam, SIZE_MAXIMIZED },
725     { EVENT_OBJECT_LOCATIONCHANGE, winevent_hook|wparam|lparam, 0, 0 },
726     { 0 }
727 };
728 /* ShowWindow(SW_SHOWMAXIMIZED) for a resized not visible popup window */
729 static const struct message WmShowMaxPopupResizedSeq[] = {
730     { HCBT_MINMAX, hook|lparam, 0, SW_MAXIMIZE },
731     { WM_GETMINMAXINFO, sent },
732     { WM_WINDOWPOSCHANGING, sent|wparam, SWP_SHOWWINDOW|SWP_FRAMECHANGED },
733     { WM_NCCALCSIZE, sent|wparam, TRUE },
734     { EVENT_OBJECT_SHOW, winevent_hook|wparam|lparam, 0, 0 },
735     { HCBT_ACTIVATE, hook },
736     { EVENT_SYSTEM_FOREGROUND, winevent_hook|wparam|lparam, 0, 0 },
737     { WM_QUERYNEWPALETTE, sent|wparam|lparam|optional, 0, 0 },
738     { WM_WINDOWPOSCHANGING, sent|wparam|optional, SWP_NOSIZE|SWP_NOMOVE },
739     { WM_NCPAINT, sent|wparam|optional, 1 },
740     { WM_ERASEBKGND, sent|optional },
741     { WM_WINDOWPOSCHANGED, sent|wparam|optional, SWP_NOSIZE|SWP_NOMOVE|SWP_NOCLIENTSIZE|SWP_NOCLIENTMOVE },
742     { WM_ACTIVATEAPP, sent|wparam, 1 },
743     { WM_NCACTIVATE, sent },
744     { WM_ACTIVATE, sent|wparam, 1 },
745     { HCBT_SETFOCUS, hook },
746     { WM_IME_SETCONTEXT, sent|wparam|defwinproc|optional, 1 },
747     { WM_IME_NOTIFY, sent|wparam|defwinproc|optional, 2 },
748     { EVENT_OBJECT_FOCUS, winevent_hook|wparam|lparam, OBJID_CLIENT, 0 },
749     { WM_SETFOCUS, sent|wparam|defwinproc, 0 },
750     { WM_GETTEXT, sent|optional },
751     { WM_NCPAINT, sent|wparam|optional, 1 },
752     { WM_ERASEBKGND, sent|optional },
753     { WM_WINDOWPOSCHANGED, sent },
754     /* WinNT4.0 sends WM_MOVE */
755     { WM_MOVE, sent|defwinproc|optional },
756     { WM_SIZE, sent|defwinproc|wparam, SIZE_MAXIMIZED },
757     { EVENT_OBJECT_LOCATIONCHANGE, winevent_hook|wparam|lparam, 0, 0 },
758     { 0 }
759 };
760 /* ShowWindow(SW_SHOWMAXIMIZED) for a not visible popup window */
761 static const struct message WmShowMaxPopupSeq[] = {
762     { HCBT_MINMAX, hook|lparam, 0, SW_MAXIMIZE },
763     { WM_GETMINMAXINFO, sent },
764     { WM_WINDOWPOSCHANGING, sent|wparam, SWP_SHOWWINDOW|SWP_FRAMECHANGED },
765     { WM_NCCALCSIZE, sent|wparam, TRUE },
766     { EVENT_OBJECT_SHOW, winevent_hook|wparam|lparam, 0, 0 },
767     { HCBT_ACTIVATE, hook },
768     { EVENT_SYSTEM_FOREGROUND, winevent_hook|wparam|lparam, 0, 0 },
769     { WM_QUERYNEWPALETTE, sent|wparam|lparam|optional, 0, 0 },
770     { WM_WINDOWPOSCHANGING, sent|wparam|optional, SWP_NOSIZE|SWP_NOMOVE },
771     { WM_ACTIVATEAPP, sent|wparam, 1 },
772     { WM_NCACTIVATE, sent },
773     { WM_ACTIVATE, sent|wparam, 1 },
774     { HCBT_SETFOCUS, hook },
775     { WM_IME_SETCONTEXT, sent|wparam|defwinproc|optional, 1 },
776     { WM_IME_NOTIFY, sent|wparam|defwinproc|optional, 2 },
777     { EVENT_OBJECT_FOCUS, winevent_hook|wparam|lparam, OBJID_CLIENT, 0 },
778     { WM_SETFOCUS, sent|wparam|defwinproc, 0 },
779     { WM_GETTEXT, sent|optional },
780     { WM_SYNCPAINT, sent|wparam|optional, 4 },
781     { WM_NCPAINT, sent|wparam|optional, 1 },
782     { WM_ERASEBKGND, sent|optional },
783     { WM_NCPAINT, sent|wparam|defwinproc|optional, 1 },
784     { WM_ERASEBKGND, sent|defwinproc|optional },
785     { WM_WINDOWPOSCHANGED, sent|wparam, SWP_NOCLIENTSIZE|SWP_NOCLIENTMOVE|SWP_SHOWWINDOW|SWP_FRAMECHANGED|SWP_NOMOVE|SWP_NOSIZE },
786     { EVENT_OBJECT_LOCATIONCHANGE, winevent_hook|wparam|lparam, 0, 0 },
787     { 0 }
788 };
789 /* CreateWindow(WS_VISIBLE) for popup window */
790 static const struct message WmCreatePopupSeq[] = {
791     { HCBT_CREATEWND, hook },
792     { WM_NCCREATE, sent },
793     { WM_NCCALCSIZE, sent|wparam, 0 },
794     { WM_CREATE, sent },
795     { EVENT_OBJECT_CREATE, winevent_hook|wparam|lparam, 0, 0 },
796     { WM_SIZE, sent|wparam, SIZE_RESTORED },
797     { WM_MOVE, sent },
798     { WM_SHOWWINDOW, sent|wparam, 1 },
799     { WM_WINDOWPOSCHANGING, sent|wparam, SWP_SHOWWINDOW|SWP_NOSIZE|SWP_NOMOVE },
800     { EVENT_OBJECT_SHOW, winevent_hook|wparam|lparam, 0, 0 },
801     { HCBT_ACTIVATE, hook },
802     { EVENT_SYSTEM_FOREGROUND, winevent_hook|wparam|lparam, 0, 0 },
803     { WM_QUERYNEWPALETTE, sent|wparam|lparam|optional, 0, 0 },
804     { WM_WINDOWPOSCHANGING, sent|wparam|optional, SWP_NOSIZE|SWP_NOMOVE },
805     { WM_NCPAINT, sent|wparam|optional, 1 },
806     { WM_ERASEBKGND, sent|optional },
807     { WM_WINDOWPOSCHANGED, sent|wparam|optional, SWP_NOSIZE|SWP_NOMOVE|SWP_NOCLIENTSIZE|SWP_NOCLIENTMOVE },
808     { WM_ACTIVATEAPP, sent|wparam, 1 },
809     { WM_NCACTIVATE, sent },
810     { WM_ACTIVATE, sent|wparam, 1 },
811     { HCBT_SETFOCUS, hook },
812     { WM_IME_SETCONTEXT, sent|wparam|defwinproc|optional, 1 },
813     { WM_IME_NOTIFY, sent|wparam|defwinproc|optional, 2 },
814     { EVENT_OBJECT_FOCUS, winevent_hook|wparam|lparam, OBJID_CLIENT, 0 },
815     { WM_SETFOCUS, sent|wparam|defwinproc, 0 },
816     { WM_GETTEXT, sent|optional },
817     { WM_SYNCPAINT, sent|wparam|optional, 4 },
818     { WM_NCPAINT, sent|wparam|optional, 1 },
819     { WM_ERASEBKGND, sent|optional },
820     { WM_WINDOWPOSCHANGED, sent|wparam, SWP_NOCLIENTMOVE|SWP_NOCLIENTSIZE|SWP_SHOWWINDOW|SWP_NOMOVE|SWP_NOSIZE },
821     { 0 }
822 };
823 /* ShowWindow(SW_SHOWMAXIMIZED) for a visible popup window */
824 static const struct message WmShowVisMaxPopupSeq[] = {
825     { HCBT_MINMAX, hook|lparam, 0, SW_MAXIMIZE },
826     { WM_GETMINMAXINFO, sent },
827     { WM_GETTEXT, sent|optional },
828     { WM_WINDOWPOSCHANGING, sent|wparam, SWP_FRAMECHANGED|SWP_STATECHANGED },
829     { WM_GETTEXT, sent|optional },
830     { WM_NCCALCSIZE, sent|wparam, TRUE },
831     { WM_QUERYNEWPALETTE, sent|wparam|lparam|optional, 0, 0 },
832     { WM_NCPAINT, sent|wparam|optional, 1 },
833     { WM_ERASEBKGND, sent|optional },
834     { WM_WINDOWPOSCHANGED, sent|wparam, SWP_FRAMECHANGED|SWP_STATECHANGED },
835     { WM_MOVE, sent|defwinproc },
836     { WM_SIZE, sent|defwinproc|wparam, SIZE_MAXIMIZED },
837     { EVENT_OBJECT_LOCATIONCHANGE, winevent_hook|wparam|lparam, 0, 0 },
838     { 0 }
839 };
840 /* CreateWindow (for a child popup window, not initially visible) */
841 static const struct message WmCreateChildPopupSeq[] = {
842     { HCBT_CREATEWND, hook },
843     { WM_NCCREATE, sent }, 
844     { WM_NCCALCSIZE, sent|wparam, 0 },
845     { WM_CREATE, sent },
846     { EVENT_OBJECT_CREATE, winevent_hook|wparam|lparam, 0, 0 },
847     { WM_SIZE, sent|wparam, SIZE_RESTORED },
848     { WM_MOVE, sent },
849     { 0 }
850 };
851 /* CreateWindow (for a popup window, not initially visible,
852  * which sets WS_VISIBLE in WM_CREATE handler)
853  */
854 static const struct message WmCreateInvisiblePopupSeq[] = {
855     { HCBT_CREATEWND, hook },
856     { WM_NCCREATE, sent }, 
857     { WM_NCCALCSIZE, sent|wparam, 0 },
858     { WM_CREATE, sent },
859     { WM_STYLECHANGING, sent },
860     { WM_STYLECHANGED, sent },
861     { EVENT_OBJECT_CREATE, winevent_hook|wparam|lparam, 0, 0 },
862     { WM_SIZE, sent|wparam, SIZE_RESTORED },
863     { WM_MOVE, sent },
864     { 0 }
865 };
866 /* SetWindowPos(SWP_SHOWWINDOW|SWP_NOSIZE|SWP_NOMOVE|SWP_NOACTIVATE|SWP_NOZORDER)
867  * for a popup window with WS_VISIBLE style set
868  */
869 static const struct message WmShowVisiblePopupSeq_2[] = {
870     { WM_WINDOWPOSCHANGING, sent|wparam, SWP_SHOWWINDOW|SWP_NOACTIVATE|SWP_NOSIZE|SWP_NOMOVE },
871     { 0 }
872 };
873 /* SetWindowPos(SWP_SHOWWINDOW|SWP_NOSIZE|SWP_NOMOVE)
874  * for a popup window with WS_VISIBLE style set
875  */
876 static const struct message WmShowVisiblePopupSeq_3[] = {
877     { WM_WINDOWPOSCHANGING, sent|wparam, SWP_SHOWWINDOW|SWP_NOSIZE|SWP_NOMOVE },
878     { HCBT_ACTIVATE, hook },
879     { EVENT_SYSTEM_FOREGROUND, winevent_hook|wparam|lparam, 0, 0 },
880     { WM_QUERYNEWPALETTE, sent|wparam|lparam|optional, 0, 0 },
881     { WM_WINDOWPOSCHANGING, sent|wparam|optional, SWP_NOSIZE|SWP_NOMOVE },
882     { WM_NCACTIVATE, sent },
883     { WM_ACTIVATE, sent|wparam, 1 },
884     { HCBT_SETFOCUS, hook },
885     { WM_KILLFOCUS, sent|parent },
886     { WM_IME_SETCONTEXT, sent|parent|wparam|optional, 0 },
887     { WM_IME_SETCONTEXT, sent|wparam|defwinproc|optional, 1 },
888     { WM_IME_NOTIFY, sent|wparam|defwinproc|optional, 2 },
889     { EVENT_OBJECT_FOCUS, winevent_hook|wparam|lparam, OBJID_CLIENT, 0 },
890     { WM_SETFOCUS, sent|defwinproc },
891     { WM_GETTEXT, sent|optional },
892     { WM_WINDOWPOSCHANGED, sent|wparam|optional, SWP_NOSIZE|SWP_NOMOVE|SWP_NOCLIENTSIZE|SWP_NOCLIENTMOVE, 0, SWP_SHOWWINDOW },
893     { 0 }
894 };
895 /* CreateWindow (for child window, not initially visible) */
896 static const struct message WmCreateChildSeq[] = {
897     { HCBT_CREATEWND, hook },
898     { WM_NCCREATE, sent }, 
899     /* child is inserted into parent's child list after WM_NCCREATE returns */
900     { WM_NCCALCSIZE, sent|wparam, 0 },
901     { WM_CREATE, sent },
902     { EVENT_OBJECT_CREATE, winevent_hook|wparam|lparam, 0, 0 },
903     { WM_SIZE, sent|wparam, SIZE_RESTORED },
904     { WM_MOVE, sent },
905     { WM_PARENTNOTIFY, sent|parent|wparam, WM_CREATE },
906     { 0 }
907 };
908 /* CreateWindow (for maximized child window, not initially visible) */
909 static const struct message WmCreateMaximizedChildSeq[] = {
910     { HCBT_CREATEWND, hook },
911     { WM_NCCREATE, sent }, 
912     { WM_NCCALCSIZE, sent|wparam, 0 },
913     { WM_CREATE, sent },
914     { EVENT_OBJECT_CREATE, winevent_hook|wparam|lparam, 0, 0 },
915     { WM_SIZE, sent|wparam, SIZE_RESTORED },
916     { WM_MOVE, sent },
917     { HCBT_MINMAX, hook|lparam, 0, SW_MAXIMIZE },
918     { WM_GETMINMAXINFO, sent },
919     { WM_WINDOWPOSCHANGING, sent|wparam, SWP_FRAMECHANGED|SWP_NOACTIVATE|SWP_STATECHANGED },
920     { WM_NCCALCSIZE, sent|wparam, 1 },
921     { WM_WINDOWPOSCHANGED, sent|wparam, SWP_FRAMECHANGED|SWP_NOACTIVATE|SWP_NOMOVE|SWP_NOREDRAW|SWP_NOCLIENTMOVE|SWP_STATECHANGED },
922     { WM_SIZE, sent|defwinproc|wparam, SIZE_MAXIMIZED },
923     { EVENT_OBJECT_LOCATIONCHANGE, winevent_hook|wparam|lparam, 0, 0 },
924     { WM_PARENTNOTIFY, sent|parent|wparam, WM_CREATE },
925     { 0 }
926 };
927 /* CreateWindow (for a child window, initially visible) */
928 static const struct message WmCreateVisibleChildSeq[] = {
929     { HCBT_CREATEWND, hook },
930     { WM_NCCREATE, sent }, 
931     /* child is inserted into parent's child list after WM_NCCREATE returns */
932     { WM_NCCALCSIZE, sent|wparam, 0 },
933     { EVENT_OBJECT_REORDER, winevent_hook|wparam|lparam, 0, 0 },
934     { WM_CREATE, sent },
935     { EVENT_OBJECT_CREATE, winevent_hook|wparam|lparam, 0, 0 },
936     { WM_SIZE, sent|wparam, SIZE_RESTORED },
937     { WM_MOVE, sent },
938     { WM_PARENTNOTIFY, sent|parent|wparam, WM_CREATE },
939     { WM_SHOWWINDOW, sent|wparam, 1 },
940     { WM_WINDOWPOSCHANGING, sent|wparam, SWP_SHOWWINDOW|SWP_NOSIZE|SWP_NOMOVE|SWP_NOACTIVATE },
941     { EVENT_OBJECT_SHOW, winevent_hook|wparam|lparam, 0, 0 },
942     { WM_ERASEBKGND, sent|parent|optional },
943     { WM_WINDOWPOSCHANGED, sent|wparam, SWP_SHOWWINDOW|SWP_NOSIZE|SWP_NOMOVE|SWP_NOACTIVATE|SWP_NOCLIENTSIZE|SWP_NOCLIENTMOVE },
944     { WM_NCCALCSIZE, sent|wparam|optional, 1 }, /* WinXP */
945     { EVENT_OBJECT_LOCATIONCHANGE, winevent_hook|wparam|lparam, 0, 0 },
946     { 0 }
947 };
948 /* ShowWindow(SW_SHOW) for a not visible child window */
949 static const struct message WmShowChildSeq[] = {
950     { WM_SHOWWINDOW, sent|wparam, 1 },
951     { WM_WINDOWPOSCHANGING, sent|wparam, SWP_SHOWWINDOW|SWP_NOACTIVATE|SWP_NOSIZE|SWP_NOMOVE },
952     { EVENT_OBJECT_SHOW, winevent_hook|wparam|lparam, 0, 0 },
953     { WM_ERASEBKGND, sent|parent|optional },
954     { WM_WINDOWPOSCHANGED, sent|wparam, SWP_SHOWWINDOW|SWP_NOACTIVATE|SWP_NOSIZE|SWP_NOMOVE|SWP_NOCLIENTSIZE|SWP_NOCLIENTMOVE },
955     { 0 }
956 };
957 /* ShowWindow(SW_HIDE) for a visible child window */
958 static const struct message WmHideChildSeq[] = {
959     { WM_SHOWWINDOW, sent|wparam, 0 },
960     { WM_WINDOWPOSCHANGING, sent|wparam, SWP_HIDEWINDOW|SWP_NOACTIVATE|SWP_NOSIZE|SWP_NOMOVE },
961     { EVENT_OBJECT_HIDE, winevent_hook|wparam|lparam, 0, 0 },
962     { WM_ERASEBKGND, sent|parent|optional },
963     { WM_WINDOWPOSCHANGED, sent|wparam, SWP_HIDEWINDOW|SWP_NOACTIVATE|SWP_NOSIZE|SWP_NOMOVE|SWP_NOCLIENTSIZE|SWP_NOCLIENTMOVE },
964     { 0 }
965 };
966 /* ShowWindow(SW_HIDE) for a visible child window checking all parent events*/
967 static const struct message WmHideChildSeq2[] = {
968     { WM_SHOWWINDOW, sent|wparam, 0 },
969     { WM_WINDOWPOSCHANGING, sent|wparam, SWP_HIDEWINDOW|SWP_NOACTIVATE|SWP_NOSIZE|SWP_NOMOVE },
970     { EVENT_OBJECT_HIDE, winevent_hook|wparam|lparam, 0, 0 },
971     { WM_ERASEBKGND, sent|parent|optional },
972     { WM_WINDOWPOSCHANGED, sent|wparam, SWP_HIDEWINDOW|SWP_NOACTIVATE|SWP_NOSIZE|SWP_NOMOVE|SWP_NOCLIENTSIZE|SWP_NOCLIENTMOVE },
973     { 0 }
974 };
975 /* SetWindowPos(SWP_SHOWWINDOW|SWP_NOSIZE|SWP_NOMOVE)
976  * for a not visible child window
977  */
978 static const struct message WmShowChildSeq_2[] = {
979     { WM_WINDOWPOSCHANGING, sent|wparam, SWP_SHOWWINDOW|SWP_NOSIZE|SWP_NOMOVE },
980     { EVENT_OBJECT_SHOW, winevent_hook|wparam|lparam, 0, 0 },
981     { WM_CHILDACTIVATE, sent },
982     { WM_WINDOWPOSCHANGED, sent|wparam, SWP_SHOWWINDOW|SWP_NOSIZE|SWP_NOMOVE|SWP_NOCLIENTSIZE|SWP_NOCLIENTMOVE },
983     { 0 }
984 };
985 /* SetWindowPos(SWP_SHOWWINDOW|SWP_NOSIZE|SWP_NOMOVE|SWP_NOACTIVATE)
986  * for a not visible child window
987  */
988 static const struct message WmShowChildSeq_3[] = {
989     { WM_WINDOWPOSCHANGING, sent|wparam, SWP_SHOWWINDOW|SWP_NOSIZE|SWP_NOMOVE|SWP_NOACTIVATE },
990     { EVENT_OBJECT_SHOW, winevent_hook|wparam|lparam, 0, 0 },
991     { WM_WINDOWPOSCHANGED, sent|wparam, SWP_SHOWWINDOW|SWP_NOSIZE|SWP_NOMOVE|SWP_NOACTIVATE|SWP_NOCLIENTSIZE|SWP_NOCLIENTMOVE },
992     { 0 }
993 };
994 /* SetWindowPos(SWP_SHOWWINDOW|SWP_NOSIZE|SWP_NOMOVE)
995  * for a visible child window with a caption
996  */
997 static const struct message WmShowChildSeq_4[] = {
998     { WM_WINDOWPOSCHANGING, sent|wparam, SWP_SHOWWINDOW|SWP_NOSIZE|SWP_NOMOVE },
999     { WM_CHILDACTIVATE, sent },
1000     { 0 }
1001 };
1002 /* ShowWindow(SW_MINIMIZE) for child with invisible parent */
1003 static const struct message WmShowChildInvisibleParentSeq_1[] = {
1004     { HCBT_MINMAX, hook|lparam, 0, SW_MINIMIZE },
1005     { WM_WINDOWPOSCHANGING, sent|wparam, SWP_SHOWWINDOW|SWP_FRAMECHANGED|SWP_NOCOPYBITS|SWP_STATECHANGED, 0, SWP_NOACTIVATE },
1006     { WM_NCCALCSIZE, sent|wparam, 1 },
1007     { EVENT_OBJECT_SHOW, winevent_hook|wparam|lparam, 0, 0 },
1008     { WM_CHILDACTIVATE, sent|optional },
1009     { WM_WINDOWPOSCHANGED, sent|wparam, SWP_SHOWWINDOW|SWP_FRAMECHANGED|SWP_NOREDRAW|SWP_NOCOPYBITS|SWP_STATECHANGED, 0, SWP_NOACTIVATE },
1010     { WM_MOVE, sent|defwinproc },
1011     { WM_SIZE, sent|defwinproc|wparam|lparam, SIZE_MINIMIZED, 0 },
1012     { EVENT_OBJECT_LOCATIONCHANGE, winevent_hook|wparam|lparam, 0, 0 },
1013     { EVENT_SYSTEM_MINIMIZESTART, winevent_hook|wparam|lparam, 0, 0 },
1014     /* FIXME: Wine creates an icon/title window while Windows doesn't */
1015     { WM_PARENTNOTIFY, sent|parent|wparam|optional, WM_CREATE },
1016     { WM_GETTEXT, sent|optional },
1017     { 0 }
1018 };
1019 /* repeated ShowWindow(SW_MINIMIZE) for child with invisible parent */
1020 static const struct message WmShowChildInvisibleParentSeq_1r[] = {
1021     { HCBT_MINMAX, hook|lparam, 0, SW_MINIMIZE },
1022     { 0 }
1023 };
1024 /* ShowWindow(SW_MAXIMIZE) for child with invisible parent */
1025 static const struct message WmShowChildInvisibleParentSeq_2[] = {
1026     { HCBT_MINMAX, hook|lparam, 0, SW_MAXIMIZE },
1027     { WM_GETMINMAXINFO, sent },
1028     { WM_WINDOWPOSCHANGING, sent|wparam, SWP_SHOWWINDOW|SWP_FRAMECHANGED|SWP_STATECHANGED },
1029     { WM_NCCALCSIZE, sent|wparam, 1 },
1030     { EVENT_OBJECT_SHOW, winevent_hook|wparam|lparam, 0, 0 },
1031     { WM_CHILDACTIVATE, sent },
1032     { WM_WINDOWPOSCHANGED, sent|wparam, SWP_SHOWWINDOW|SWP_FRAMECHANGED|SWP_NOMOVE|SWP_NOREDRAW|SWP_NOCLIENTMOVE|SWP_STATECHANGED },
1033     { WM_SIZE, sent|defwinproc|wparam, SIZE_MAXIMIZED },
1034     { EVENT_OBJECT_LOCATIONCHANGE, winevent_hook|wparam|lparam, 0, 0 },
1035     { 0 }
1036 };
1037 /* repeated ShowWindow(SW_MAXIMIZE) for child with invisible parent */
1038 static const struct message WmShowChildInvisibleParentSeq_2r[] = {
1039     { HCBT_MINMAX, hook|lparam, 0, SW_MAXIMIZE },
1040     { 0 }
1041 };
1042 /* ShowWindow(SW_SHOWMINIMIZED) for child with invisible parent */
1043 static const struct message WmShowChildInvisibleParentSeq_3[] = {
1044     { HCBT_MINMAX, hook|lparam, 0, SW_SHOWMINIMIZED },
1045     { WM_WINDOWPOSCHANGING, sent|wparam, SWP_SHOWWINDOW|SWP_FRAMECHANGED|SWP_NOCOPYBITS|SWP_STATECHANGED },
1046     { WM_NCCALCSIZE, sent|wparam, 1 },
1047     { EVENT_OBJECT_SHOW, winevent_hook|wparam|lparam, 0, 0 },
1048     { WM_CHILDACTIVATE, sent },
1049     { WM_WINDOWPOSCHANGED, sent|wparam, SWP_SHOWWINDOW|SWP_FRAMECHANGED|SWP_NOREDRAW|SWP_NOCOPYBITS|SWP_STATECHANGED },
1050     { WM_MOVE, sent|defwinproc },
1051     { WM_SIZE, sent|defwinproc|wparam|lparam, SIZE_MINIMIZED, 0 },
1052     { EVENT_OBJECT_LOCATIONCHANGE, winevent_hook|wparam|lparam, 0, 0 },
1053     { EVENT_SYSTEM_MINIMIZESTART, winevent_hook|wparam|lparam, 0, 0 },
1054     /* FIXME: Wine creates an icon/title window while Windows doesn't */
1055     { WM_PARENTNOTIFY, sent|parent|wparam|optional, WM_CREATE },
1056     { WM_GETTEXT, sent|optional },
1057     { 0 }
1058 };
1059 /* repeated ShowWindow(SW_SHOWMINIMIZED) for child with invisible parent */
1060 static const struct message WmShowChildInvisibleParentSeq_3r[] = {
1061     { HCBT_MINMAX, hook|lparam, 0, SW_SHOWMINIMIZED },
1062     { 0 }
1063 };
1064 /* ShowWindow(SW_SHOWMINNOACTIVE) for child with invisible parent */
1065 static const struct message WmShowChildInvisibleParentSeq_4[] = {
1066     { HCBT_MINMAX, hook|lparam, 0, SW_SHOWMINNOACTIVE },
1067     { WM_WINDOWPOSCHANGING, sent|wparam, SWP_SHOWWINDOW|SWP_FRAMECHANGED|SWP_NOACTIVATE|SWP_NOCOPYBITS|SWP_STATECHANGED },
1068     { WM_NCCALCSIZE, sent|wparam, 1 },
1069     { EVENT_OBJECT_SHOW, winevent_hook|wparam|lparam, 0, 0 },
1070     { WM_WINDOWPOSCHANGED, sent|wparam, SWP_SHOWWINDOW|SWP_FRAMECHANGED|SWP_NOACTIVATE|SWP_NOREDRAW|SWP_NOCOPYBITS|SWP_STATECHANGED },
1071     { WM_MOVE, sent|defwinproc },
1072     { WM_SIZE, sent|defwinproc|wparam|lparam, SIZE_MINIMIZED, 0 },
1073     { EVENT_OBJECT_LOCATIONCHANGE, winevent_hook|wparam|lparam, 0, 0 },
1074     { EVENT_SYSTEM_MINIMIZESTART, winevent_hook|wparam|lparam, 0, 0 },
1075     /* FIXME: Wine creates an icon/title window while Windows doesn't */
1076     { WM_PARENTNOTIFY, sent|parent|wparam|optional, WM_CREATE },
1077     { WM_GETTEXT, sent|optional },
1078     { 0 }
1079 };
1080 /* repeated ShowWindow(SW_SHOWMINNOACTIVE) for child with invisible parent */
1081 static const struct message WmShowChildInvisibleParentSeq_4r[] = {
1082     { HCBT_MINMAX, hook|lparam, 0, SW_SHOWMINNOACTIVE },
1083     { 0 }
1084 };
1085 /* ShowWindow(SW_SHOW) for child with invisible parent */
1086 static const struct message WmShowChildInvisibleParentSeq_5[] = {
1087     { WM_SHOWWINDOW, sent|wparam, 1 },
1088     { 0 }
1089 };
1090 /* ShowWindow(SW_HIDE) for child with invisible parent */
1091 static const struct message WmHideChildInvisibleParentSeq[] = {
1092     { WM_SHOWWINDOW, sent|wparam, 0 },
1093     { 0 }
1094 };
1095 /* SetWindowPos(SWP_SHOWWINDOW) for child with invisible parent */
1096 static const struct message WmShowChildInvisibleParentSeq_6[] = {
1097     { WM_WINDOWPOSCHANGING, sent|wparam, SWP_SHOWWINDOW|SWP_NOSIZE|SWP_NOMOVE|SWP_NOACTIVATE },
1098     { EVENT_OBJECT_SHOW, winevent_hook|wparam|lparam, 0, 0 },
1099     { WM_WINDOWPOSCHANGED, sent|wparam, SWP_SHOWWINDOW|SWP_NOSIZE|SWP_NOMOVE|SWP_NOACTIVATE|SWP_NOREDRAW|SWP_NOCLIENTSIZE|SWP_NOCLIENTMOVE },
1100     { 0 }
1101 };
1102 /* SetWindowPos(SWP_HIDEWINDOW) for child with invisible parent */
1103 static const struct message WmHideChildInvisibleParentSeq_2[] = {
1104     { WM_WINDOWPOSCHANGING, sent|wparam, SWP_HIDEWINDOW|SWP_NOACTIVATE|SWP_NOSIZE|SWP_NOMOVE },
1105     { EVENT_OBJECT_HIDE, winevent_hook|wparam|lparam, 0, 0 },
1106     { WM_WINDOWPOSCHANGED, sent|wparam, SWP_HIDEWINDOW|SWP_NOACTIVATE|SWP_NOSIZE|SWP_NOMOVE|SWP_NOREDRAW|SWP_NOCLIENTSIZE|SWP_NOCLIENTMOVE },
1107     { 0 }
1108 };
1109 /* DestroyWindow for a visible child window */
1110 static const struct message WmDestroyChildSeq[] = {
1111     { HCBT_DESTROYWND, hook },
1112     { 0x0090, sent|optional },
1113     { WM_PARENTNOTIFY, sent|parent|wparam, WM_DESTROY },
1114     { WM_SHOWWINDOW, sent|wparam, 0 },
1115     { WM_WINDOWPOSCHANGING, sent|wparam, SWP_HIDEWINDOW|SWP_NOACTIVATE|SWP_NOSIZE|SWP_NOMOVE },
1116     { EVENT_OBJECT_HIDE, winevent_hook|wparam|lparam, 0, 0 },
1117     { WM_ERASEBKGND, sent|parent|optional },
1118     { WM_WINDOWPOSCHANGED, sent|wparam, SWP_HIDEWINDOW|SWP_NOACTIVATE|SWP_NOSIZE|SWP_NOMOVE|SWP_NOCLIENTSIZE|SWP_NOCLIENTMOVE },
1119     { HCBT_SETFOCUS, hook }, /* set focus to a parent */
1120     { WM_KILLFOCUS, sent },
1121     { WM_IME_SETCONTEXT, sent|wparam|optional, 0 },
1122     { WM_IME_SETCONTEXT, sent|wparam|parent|optional, 1 },
1123     { EVENT_OBJECT_FOCUS, winevent_hook|wparam|lparam, OBJID_CLIENT, 0 },
1124     { WM_SETFOCUS, sent|parent },
1125     { EVENT_OBJECT_DESTROY, winevent_hook|wparam|lparam, 0, 0 },
1126     { WM_DESTROY, sent },
1127     { WM_DESTROY, sent|optional }, /* some other (IME?) window */
1128     { WM_NCDESTROY, sent|optional }, /* some other (IME?) window */
1129     { WM_NCDESTROY, sent },
1130     { 0 }
1131 };
1132 /* visible child window destroyed by thread exit */
1133 static const struct message WmExitThreadSeq[] = {
1134     { WM_NCDESTROY, sent },  /* actually in grandchild */
1135     { WM_PAINT, sent|parent },
1136     { WM_ERASEBKGND, sent|parent|beginpaint },
1137     { 0 }
1138 };
1139 /* DestroyWindow for a visible child window with invisible parent */
1140 static const struct message WmDestroyInvisibleChildSeq[] = {
1141     { HCBT_DESTROYWND, hook },
1142     { 0x0090, sent|optional },
1143     { WM_PARENTNOTIFY, sent|parent|wparam, WM_DESTROY },
1144     { WM_SHOWWINDOW, sent|wparam, 0 },
1145     { EVENT_OBJECT_DESTROY, winevent_hook|wparam|lparam, 0, 0 },
1146     { WM_DESTROY, sent },
1147     { WM_NCDESTROY, sent },
1148     { 0 }
1149 };
1150 /* Moving the mouse in nonclient area */
1151 static const struct message WmMouseMoveInNonClientAreaSeq[] = { /* FIXME: add */
1152     { WM_NCHITTEST, sent },
1153     { WM_SETCURSOR, sent },
1154     { WM_NCMOUSEMOVE, posted },
1155     { 0 }
1156 };
1157 /* Moving the mouse in client area */
1158 static const struct message WmMouseMoveInClientAreaSeq[] = { /* FIXME: add */
1159     { WM_NCHITTEST, sent },
1160     { WM_SETCURSOR, sent },
1161     { WM_MOUSEMOVE, posted },
1162     { 0 }
1163 };
1164 /* Moving by dragging the title bar (after WM_NCHITTEST and WM_SETCURSOR) (outline move) */
1165 static const struct message WmDragTitleBarSeq[] = { /* FIXME: add */
1166     { WM_NCLBUTTONDOWN, sent|wparam, HTCAPTION },
1167     { WM_SYSCOMMAND, sent|defwinproc|wparam, SC_MOVE+2 },
1168     { WM_GETMINMAXINFO, sent|defwinproc },
1169     { WM_ENTERSIZEMOVE, sent|defwinproc },
1170     { WM_WINDOWPOSCHANGING, sent|wparam|defwinproc, 0 },
1171     { WM_WINDOWPOSCHANGED, sent|wparam|defwinproc, 0 },
1172     { WM_MOVE, sent|defwinproc },
1173     { WM_EXITSIZEMOVE, sent|defwinproc },
1174     { 0 }
1175 };
1176 /* Sizing by dragging the thick borders (after WM_NCHITTEST and WM_SETCURSOR) (outline move) */
1177 static const struct message WmDragThickBordersBarSeq[] = { /* FIXME: add */
1178     { WM_NCLBUTTONDOWN, sent|wparam, 0xd },
1179     { WM_SYSCOMMAND, sent|defwinproc|wparam, 0xf004 },
1180     { WM_GETMINMAXINFO, sent|defwinproc },
1181     { WM_ENTERSIZEMOVE, sent|defwinproc },
1182     { WM_SIZING, sent|defwinproc|wparam, 4}, /* one for each mouse movement */
1183     { WM_WINDOWPOSCHANGING, sent|wparam|defwinproc, 0 },
1184     { WM_GETMINMAXINFO, sent|defwinproc },
1185     { WM_NCCALCSIZE, sent|defwinproc|wparam, 1 },
1186     { WM_NCPAINT, sent|defwinproc|wparam, 1 },
1187     { WM_GETTEXT, sent|defwinproc },
1188     { WM_ERASEBKGND, sent|defwinproc },
1189     { WM_WINDOWPOSCHANGED, sent|wparam|defwinproc, 0 },
1190     { WM_MOVE, sent|defwinproc },
1191     { WM_SIZE, sent|defwinproc|wparam, SIZE_RESTORED },
1192     { WM_EXITSIZEMOVE, sent|defwinproc },
1193     { 0 }
1194 };
1195 /* Resizing child window with MoveWindow (32) */
1196 static const struct message WmResizingChildWithMoveWindowSeq[] = {
1197     { WM_WINDOWPOSCHANGING, sent|wparam, SWP_NOACTIVATE },
1198     { WM_NCCALCSIZE, sent|wparam, 1 },
1199     { WM_ERASEBKGND, sent|parent|optional },
1200     { WM_ERASEBKGND, sent|optional },
1201     { WM_WINDOWPOSCHANGED, sent|wparam, SWP_NOACTIVATE },
1202     { WM_MOVE, sent|defwinproc },
1203     { WM_SIZE, sent|defwinproc|wparam, SIZE_RESTORED },
1204     { EVENT_OBJECT_LOCATIONCHANGE, winevent_hook|wparam|lparam, 0, 0 },
1205     { 0 }
1206 };
1207 /* Clicking on inactive button */
1208 static const struct message WmClickInactiveButtonSeq[] = { /* FIXME: add */
1209     { WM_NCHITTEST, sent },
1210     { WM_PARENTNOTIFY, sent|parent|wparam, WM_LBUTTONDOWN },
1211     { WM_MOUSEACTIVATE, sent },
1212     { WM_MOUSEACTIVATE, sent|parent|defwinproc },
1213     { WM_SETCURSOR, sent },
1214     { WM_SETCURSOR, sent|parent|defwinproc },
1215     { WM_LBUTTONDOWN, posted },
1216     { WM_KILLFOCUS, posted|parent },
1217     { WM_SETFOCUS, posted },
1218     { WM_CTLCOLORBTN, posted|parent },
1219     { BM_SETSTATE, posted },
1220     { WM_CTLCOLORBTN, posted|parent },
1221     { WM_LBUTTONUP, posted },
1222     { BM_SETSTATE, posted },
1223     { WM_CTLCOLORBTN, posted|parent },
1224     { WM_COMMAND, posted|parent },
1225     { 0 }
1226 };
1227 /* Reparenting a button (16/32) */
1228 /* The last child (button) reparented gets topmost for its new parent. */
1229 static const struct message WmReparentButtonSeq[] = { /* FIXME: add */
1230     { WM_SHOWWINDOW, sent|wparam, 0 },
1231     { WM_WINDOWPOSCHANGING, sent|wparam, SWP_HIDEWINDOW|SWP_NOACTIVATE|SWP_NOMOVE|SWP_NOSIZE },
1232     { EVENT_OBJECT_HIDE, winevent_hook|wparam|lparam, 0, 0 },
1233     { WM_ERASEBKGND, sent|parent },
1234     { WM_WINDOWPOSCHANGED, sent|wparam, SWP_HIDEWINDOW|SWP_NOACTIVATE|SWP_NOMOVE|SWP_NOSIZE },
1235     { WM_WINDOWPOSCHANGING, sent|wparam, SWP_NOSIZE },
1236     { WM_CHILDACTIVATE, sent },
1237     { WM_WINDOWPOSCHANGED, sent|wparam, SWP_NOSIZE|SWP_NOREDRAW },
1238     { WM_MOVE, sent|defwinproc },
1239     { WM_SHOWWINDOW, sent|wparam, 1 },
1240     { 0 }
1241 };
1242 /* Creation of a custom dialog (32) */
1243 static const struct message WmCreateCustomDialogSeq[] = {
1244     { HCBT_CREATEWND, hook },
1245     { WM_GETMINMAXINFO, sent },
1246     { WM_NCCREATE, sent },
1247     { WM_NCCALCSIZE, sent|wparam, 0 },
1248     { EVENT_OBJECT_REORDER, winevent_hook|wparam|lparam, 0, 0 },
1249     { WM_CREATE, sent },
1250     { EVENT_OBJECT_CREATE, winevent_hook|wparam|lparam, 0, 0 },
1251     { WM_NOTIFYFORMAT, sent|optional },
1252     { WM_QUERYUISTATE, sent|optional },
1253     { WM_WINDOWPOSCHANGING, sent|optional },
1254     { WM_GETMINMAXINFO, sent|optional },
1255     { WM_NCCALCSIZE, sent|optional },
1256     { WM_WINDOWPOSCHANGED, sent|optional },
1257     { WM_SHOWWINDOW, sent|wparam, 1 },
1258     { WM_WINDOWPOSCHANGING, sent|wparam, SWP_SHOWWINDOW|SWP_NOSIZE|SWP_NOMOVE },
1259     { EVENT_OBJECT_SHOW, winevent_hook|wparam|lparam, 0, 0 },
1260     { HCBT_ACTIVATE, hook },
1261     { EVENT_SYSTEM_FOREGROUND, winevent_hook|wparam|lparam, 0, 0 },
1262
1263
1264     { WM_QUERYNEWPALETTE, sent|wparam|lparam|optional, 0, 0 },
1265
1266     { WM_WINDOWPOSCHANGING, sent|wparam|optional, SWP_NOSIZE|SWP_NOMOVE },
1267
1268     { WM_NCACTIVATE, sent },
1269     { WM_GETTEXT, sent|optional|defwinproc },
1270     { WM_GETTEXT, sent|optional|defwinproc },
1271     { WM_GETTEXT, sent|optional|defwinproc },
1272     { EVENT_OBJECT_DEFACTIONCHANGE, winevent_hook|wparam|lparam|optional, OBJID_CLIENT, 0 },
1273     { WM_ACTIVATE, sent|wparam, 1 },
1274     { WM_GETTEXT, sent|optional },
1275     { WM_KILLFOCUS, sent|parent },
1276     { WM_IME_SETCONTEXT, sent|parent|wparam|optional, 0 },
1277     { WM_IME_SETCONTEXT, sent|wparam|optional, 1 },
1278     { WM_IME_NOTIFY, sent|wparam|defwinproc|optional, 2 },
1279     { EVENT_OBJECT_FOCUS, winevent_hook|wparam|lparam, OBJID_CLIENT, 0 },
1280     { WM_SETFOCUS, sent },
1281     { WM_GETDLGCODE, sent|defwinproc|wparam, 0 },
1282     { WM_NCPAINT, sent|wparam, 1 },
1283     { WM_GETTEXT, sent|optional|defwinproc },
1284     { WM_GETTEXT, sent|optional|defwinproc },
1285     { WM_ERASEBKGND, sent },
1286     { WM_CTLCOLORDLG, sent|optional|defwinproc },
1287     { WM_WINDOWPOSCHANGED, sent|wparam, SWP_SHOWWINDOW|SWP_NOSIZE|SWP_NOMOVE|SWP_NOCLIENTSIZE|SWP_NOCLIENTMOVE },
1288     { WM_GETTEXT, sent|optional },
1289     { WM_GETTEXT, sent|optional },
1290     { WM_NCCALCSIZE, sent|optional },
1291     { WM_NCPAINT, sent|optional },
1292     { WM_GETTEXT, sent|optional|defwinproc },
1293     { WM_GETTEXT, sent|optional|defwinproc },
1294     { WM_ERASEBKGND, sent|optional },
1295     { WM_CTLCOLORDLG, sent|optional|defwinproc },
1296     { EVENT_OBJECT_LOCATIONCHANGE, winevent_hook|wparam|lparam, 0, 0 },
1297     { WM_SIZE, sent|wparam, SIZE_RESTORED },
1298     { WM_MOVE, sent },
1299     { 0 }
1300 };
1301 /* Calling EndDialog for a custom dialog (32) */
1302 static const struct message WmEndCustomDialogSeq[] = {
1303     { WM_WINDOWPOSCHANGING, sent|wparam, SWP_HIDEWINDOW|SWP_NOACTIVATE|SWP_NOSIZE|SWP_NOMOVE },
1304     { EVENT_OBJECT_HIDE, winevent_hook|wparam|lparam, 0, 0 },
1305     { WM_WINDOWPOSCHANGED, sent|wparam, SWP_HIDEWINDOW|SWP_NOACTIVATE|SWP_NOSIZE|SWP_NOMOVE|SWP_NOCLIENTSIZE|SWP_NOCLIENTMOVE },
1306     { WM_GETTEXT, sent|optional },
1307     { HCBT_ACTIVATE, hook },
1308     { WM_NCACTIVATE, sent|wparam, 0 },
1309     { WM_GETTEXT, sent|optional|defwinproc },
1310     { WM_GETTEXT, sent|optional|defwinproc },
1311     { WM_ACTIVATE, sent|wparam, 0 },
1312     { EVENT_SYSTEM_FOREGROUND, winevent_hook|wparam|lparam, 0, 0 },
1313     { WM_WINDOWPOSCHANGING, sent|wparam|optional, SWP_NOACTIVATE|SWP_NOSIZE|SWP_NOMOVE },
1314     { WM_WINDOWPOSCHANGED, sent|wparam|optional, SWP_NOACTIVATE|SWP_NOREDRAW|SWP_NOSIZE|SWP_NOMOVE|SWP_NOCLIENTSIZE|SWP_NOCLIENTMOVE },
1315     { WM_GETTEXT, sent|optional|defwinproc },
1316     { WM_GETTEXT, sent|optional|defwinproc },
1317     { HCBT_SETFOCUS, hook },
1318     { WM_KILLFOCUS, sent },
1319     { WM_IME_SETCONTEXT, sent|wparam|optional, 0 },
1320     { WM_IME_SETCONTEXT, sent|parent|wparam|defwinproc|optional, 1 },
1321     { WM_IME_NOTIFY, sent|wparam|optional, 1 },
1322     { EVENT_OBJECT_FOCUS, winevent_hook|wparam|lparam, OBJID_CLIENT, 0 },
1323     { WM_SETFOCUS, sent|parent|defwinproc },
1324     { 0 }
1325 };
1326 /* ShowWindow(SW_SHOW) for a custom dialog (initially invisible) */
1327 static const struct message WmShowCustomDialogSeq[] = {
1328     { WM_SHOWWINDOW, sent|wparam, 1 },
1329     { WM_WINDOWPOSCHANGING, sent|wparam, SWP_SHOWWINDOW|SWP_NOSIZE|SWP_NOMOVE },
1330     { EVENT_OBJECT_SHOW, winevent_hook|wparam|lparam, 0, 0 },
1331     { HCBT_ACTIVATE, hook },
1332     { EVENT_SYSTEM_FOREGROUND, winevent_hook|wparam|lparam, 0, 0 },
1333
1334     { WM_QUERYNEWPALETTE, sent|wparam|lparam|optional, 0, 0 },
1335
1336     { WM_WINDOWPOSCHANGING, sent|wparam|optional, SWP_NOSIZE|SWP_NOMOVE },
1337     { WM_ACTIVATEAPP, sent|wparam|optional, 1 },
1338     { WM_NCACTIVATE, sent },
1339     { WM_ACTIVATE, sent|wparam, 1 },
1340     { WM_GETTEXT, sent|optional },
1341
1342     { WM_KILLFOCUS, sent|parent },
1343     { WM_IME_SETCONTEXT, sent|parent|wparam|optional, 0 },
1344     { WM_IME_SETCONTEXT, sent|wparam|optional, 1 },
1345     { WM_IME_NOTIFY, sent|wparam|defwinproc|optional, 2 },
1346     { EVENT_OBJECT_FOCUS, winevent_hook|wparam|lparam, OBJID_CLIENT, 0 },
1347     { WM_SETFOCUS, sent },
1348     { WM_GETDLGCODE, sent|defwinproc|wparam, 0 },
1349     { WM_NCPAINT, sent|wparam, 1 },
1350     { WM_ERASEBKGND, sent },
1351     { WM_CTLCOLORDLG, sent|defwinproc },
1352     { WM_WINDOWPOSCHANGED, sent|wparam, SWP_SHOWWINDOW|SWP_NOSIZE|SWP_NOMOVE|SWP_NOCLIENTSIZE|SWP_NOCLIENTMOVE },
1353     { 0 }
1354 };
1355 /* Creation and destruction of a modal dialog (32) */
1356 static const struct message WmModalDialogSeq[] = {
1357     { WM_CANCELMODE, sent|parent },
1358     { HCBT_SETFOCUS, hook },
1359     { EVENT_OBJECT_FOCUS, winevent_hook|wparam|lparam, OBJID_CLIENT, 0 },
1360     { WM_KILLFOCUS, sent|parent },
1361     { WM_IME_SETCONTEXT, sent|parent|wparam|optional, 0 },
1362     { EVENT_OBJECT_STATECHANGE, winevent_hook|wparam|lparam, 0, 0 },
1363     { WM_ENABLE, sent|parent|wparam, 0 },
1364     { HCBT_CREATEWND, hook },
1365     { EVENT_OBJECT_REORDER, winevent_hook|wparam|lparam, 0, 0 },
1366     { EVENT_OBJECT_CREATE, winevent_hook|wparam|lparam, 0, 0 },
1367     { WM_SETFONT, sent },
1368     { WM_INITDIALOG, sent },
1369     { WM_CHANGEUISTATE, sent|optional },
1370     { WM_UPDATEUISTATE, sent|optional },
1371     { WM_SHOWWINDOW, sent },
1372     { HCBT_ACTIVATE, hook },
1373     { EVENT_SYSTEM_FOREGROUND, winevent_hook|wparam|lparam, 0, 0 },
1374     { WM_QUERYNEWPALETTE, sent|wparam|lparam|optional, 0, 0 },
1375     { WM_WINDOWPOSCHANGING, sent|wparam, SWP_NOSIZE|SWP_NOMOVE },
1376     { WM_NCACTIVATE, sent },
1377     { WM_GETTEXT, sent|optional },
1378     { WM_ACTIVATE, sent|wparam, 1 },
1379     { WM_WINDOWPOSCHANGING, sent|wparam, SWP_SHOWWINDOW|SWP_NOSIZE|SWP_NOMOVE|SWP_NOACTIVATE },
1380     { EVENT_OBJECT_SHOW, winevent_hook|wparam|lparam, 0, 0 },
1381     { WM_NCPAINT, sent|optional },
1382     { WM_GETTEXT, sent|optional },
1383     { WM_ERASEBKGND, sent|optional },
1384     { WM_CTLCOLORDLG, sent|optional },
1385     { WM_WINDOWPOSCHANGED, sent|wparam, SWP_SHOWWINDOW|SWP_NOSIZE|SWP_NOMOVE|SWP_NOACTIVATE|SWP_NOCLIENTSIZE|SWP_NOCLIENTMOVE },
1386     { WM_GETTEXT, sent|optional },
1387     { WM_NCCALCSIZE, sent|optional },
1388     { WM_NCPAINT, sent|optional },
1389     { WM_GETTEXT, sent|optional },
1390     { WM_ERASEBKGND, sent|optional },
1391     { WM_CTLCOLORDLG, sent|optional },
1392     { EVENT_OBJECT_LOCATIONCHANGE, winevent_hook|wparam|lparam, 0, 0 },
1393     { WM_PAINT, sent|optional },
1394     { WM_CTLCOLORBTN, sent|optional },
1395     { WM_GETTITLEBARINFOEX, sent|optional },
1396     { WM_ENTERIDLE, sent|parent|optional },
1397     { WM_ENTERIDLE, sent|parent|optional },
1398     { WM_ENTERIDLE, sent|parent|optional },
1399     { WM_ENTERIDLE, sent|parent|optional },
1400     { WM_ENTERIDLE, sent|parent|optional },
1401     { WM_ENTERIDLE, sent|parent|optional },
1402     { WM_ENTERIDLE, sent|parent|optional },
1403     { WM_ENTERIDLE, sent|parent|optional },
1404     { WM_ENTERIDLE, sent|parent|optional },
1405     { WM_ENTERIDLE, sent|parent|optional },
1406     { WM_ENTERIDLE, sent|parent|optional },
1407     { WM_ENTERIDLE, sent|parent|optional },
1408     { WM_ENTERIDLE, sent|parent|optional },
1409     { WM_ENTERIDLE, sent|parent|optional },
1410     { WM_ENTERIDLE, sent|parent|optional },
1411     { WM_ENTERIDLE, sent|parent|optional },
1412     { WM_ENTERIDLE, sent|parent|optional },
1413     { WM_ENTERIDLE, sent|parent|optional },
1414     { WM_ENTERIDLE, sent|parent|optional },
1415     { WM_ENTERIDLE, sent|parent|optional },
1416     { WM_TIMER, sent },
1417     { EVENT_OBJECT_STATECHANGE, winevent_hook|wparam|lparam, 0, 0 },
1418     { WM_ENABLE, sent|parent|wparam, 1 },
1419     { WM_WINDOWPOSCHANGING, sent|wparam, SWP_HIDEWINDOW|SWP_NOSIZE|SWP_NOMOVE|SWP_NOACTIVATE },
1420     { EVENT_OBJECT_HIDE, winevent_hook|wparam|lparam, 0, 0 },
1421     { WM_WINDOWPOSCHANGED, sent|wparam, SWP_HIDEWINDOW|SWP_NOSIZE|SWP_NOMOVE|SWP_NOACTIVATE|SWP_NOCLIENTSIZE|SWP_NOCLIENTMOVE },
1422     { WM_GETTEXT, sent|optional },
1423     { HCBT_ACTIVATE, hook },
1424     { WM_NCACTIVATE, sent|wparam, 0 },
1425     { WM_GETTEXT, sent|optional },
1426     { WM_ACTIVATE, sent|wparam, 0 },
1427     { EVENT_SYSTEM_FOREGROUND, winevent_hook|wparam|lparam, 0, 0 },
1428     { WM_WINDOWPOSCHANGING, sent|optional },
1429     { WM_WINDOWPOSCHANGED, sent|optional },
1430     { HCBT_SETFOCUS, hook },
1431     { WM_IME_SETCONTEXT, sent|parent|wparam|defwinproc|optional, 1 },
1432     { EVENT_OBJECT_FOCUS, winevent_hook|wparam|lparam, OBJID_CLIENT, 0 },
1433     { WM_SETFOCUS, sent|parent|defwinproc },
1434     { EVENT_SYSTEM_DIALOGEND, winevent_hook|wparam|lparam, 0, 0 },
1435     { HCBT_DESTROYWND, hook },
1436     { 0x0090, sent|optional },
1437     { EVENT_OBJECT_DESTROY, winevent_hook|wparam|lparam, 0, 0 },
1438     { WM_DESTROY, sent },
1439     { WM_NCDESTROY, sent },
1440     { 0 }
1441 };
1442 /* Creation of a modal dialog that is resized inside WM_INITDIALOG (32) */
1443 static const struct message WmCreateModalDialogResizeSeq[] = { /* FIXME: add */
1444     /* (inside dialog proc, handling WM_INITDIALOG) */
1445     { WM_WINDOWPOSCHANGING, sent|wparam, 0 },
1446     { WM_NCCALCSIZE, sent },
1447     { WM_NCACTIVATE, sent|parent|wparam, 0 },
1448     { WM_GETTEXT, sent|defwinproc },
1449     { WM_ACTIVATE, sent|parent|wparam, 0 },
1450     { WM_WINDOWPOSCHANGING, sent|wparam, 0 },
1451     { WM_WINDOWPOSCHANGING, sent|parent },
1452     { WM_NCACTIVATE, sent|wparam, 1 },
1453     { WM_ACTIVATE, sent|wparam, 1 },
1454     { WM_WINDOWPOSCHANGED, sent|wparam, 0 },
1455     { WM_SIZE, sent|defwinproc|wparam, SIZE_RESTORED },
1456     /* (setting focus) */
1457     { WM_SHOWWINDOW, sent|wparam, 1 },
1458     { WM_WINDOWPOSCHANGING, sent|wparam, 0 },
1459     { WM_NCPAINT, sent },
1460     { WM_GETTEXT, sent|defwinproc },
1461     { WM_ERASEBKGND, sent },
1462     { WM_CTLCOLORDLG, sent|defwinproc },
1463     { WM_WINDOWPOSCHANGED, sent|wparam, 0 },
1464     { WM_PAINT, sent },
1465     /* (bunch of WM_CTLCOLOR* for each control) */
1466     { WM_PAINT, sent|parent },
1467     { WM_ENTERIDLE, sent|parent|wparam, 0 },
1468     { WM_SETCURSOR, sent|parent },
1469     { 0 }
1470 };
1471 /* SetMenu for NonVisible windows with size change*/
1472 static const struct message WmSetMenuNonVisibleSizeChangeSeq[] = {
1473     { WM_WINDOWPOSCHANGING, sent|wparam, SWP_FRAMECHANGED|SWP_NOACTIVATE|SWP_NOSIZE|SWP_NOMOVE },
1474     { WM_NCCALCSIZE, sent|wparam, 1 },
1475     { EVENT_OBJECT_REORDER, winevent_hook|wparam|lparam, 0, 0 },
1476     { WM_WINDOWPOSCHANGED, sent|wparam, SWP_FRAMECHANGED|SWP_NOACTIVATE|SWP_NOSIZE|SWP_NOMOVE|SWP_NOREDRAW },
1477     { WM_MOVE, sent|defwinproc },
1478     { WM_SIZE, sent|defwinproc|wparam, SIZE_RESTORED },
1479     { WM_NCCALCSIZE,sent|wparam|optional, 1 }, /* XP */
1480     { EVENT_OBJECT_LOCATIONCHANGE, winevent_hook|wparam|lparam, 0, 0 },
1481     { EVENT_OBJECT_LOCATIONCHANGE, winevent_hook|wparam|lparam|optional, 0, 0 }, /* XP sends a duplicate */
1482     { WM_GETTEXT, sent|optional },
1483     { WM_NCCALCSIZE, sent|wparam|optional, 1 },
1484     { 0 }
1485 };
1486 /* SetMenu for NonVisible windows with no size change */
1487 static const struct message WmSetMenuNonVisibleNoSizeChangeSeq[] = {
1488     { WM_WINDOWPOSCHANGING, sent|wparam, SWP_FRAMECHANGED|SWP_NOACTIVATE|SWP_NOSIZE|SWP_NOMOVE },
1489     { WM_NCCALCSIZE, sent|wparam, 1 },
1490     { WM_WINDOWPOSCHANGED, sent|wparam, SWP_FRAMECHANGED|SWP_NOACTIVATE|SWP_NOSIZE|SWP_NOMOVE|SWP_NOREDRAW|SWP_NOCLIENTSIZE|SWP_NOCLIENTMOVE },
1491     { EVENT_OBJECT_LOCATIONCHANGE, winevent_hook|wparam|lparam, 0, 0 },
1492     { 0 }
1493 };
1494 /* SetMenu for Visible windows with size change */
1495 static const struct message WmSetMenuVisibleSizeChangeSeq[] = {
1496     { WM_WINDOWPOSCHANGING, sent|wparam, SWP_FRAMECHANGED|SWP_NOACTIVATE|SWP_NOSIZE|SWP_NOMOVE },
1497     { WM_NCCALCSIZE, sent|wparam, 1 },
1498     { 0x0093, sent|defwinproc|optional },
1499     { EVENT_OBJECT_REORDER, winevent_hook|wparam|lparam, 0, 0 },
1500     { WM_NCPAINT, sent|optional }, /* wparam != 1 */
1501     { 0x0093, sent|defwinproc|optional },
1502     { 0x0093, sent|defwinproc|optional },
1503     { 0x0091, sent|defwinproc|optional },
1504     { 0x0092, sent|defwinproc|optional },
1505     { WM_GETTEXT, sent|defwinproc|optional },
1506     { WM_ERASEBKGND, sent|optional },
1507     { WM_ACTIVATE, sent|optional },
1508     { WM_WINDOWPOSCHANGED, sent|wparam, SWP_FRAMECHANGED|SWP_NOACTIVATE|SWP_NOSIZE|SWP_NOMOVE },
1509     { WM_MOVE, sent|defwinproc },
1510     { WM_SIZE, sent|defwinproc|wparam, SIZE_RESTORED },
1511     { 0x0093, sent|optional },
1512     { WM_NCCALCSIZE, sent|wparam|optional, 1 },
1513     { 0x0093, sent|defwinproc|optional },
1514     { WM_NCPAINT, sent|optional }, /* wparam != 1 */
1515     { 0x0093, sent|defwinproc|optional },
1516     { 0x0093, sent|defwinproc|optional },
1517     { 0x0091, sent|defwinproc|optional },
1518     { 0x0092, sent|defwinproc|optional },
1519     { WM_ERASEBKGND, sent|optional },
1520     { EVENT_OBJECT_LOCATIONCHANGE, winevent_hook|wparam|lparam, 0, 0 },
1521     { EVENT_OBJECT_LOCATIONCHANGE, winevent_hook|wparam|lparam|optional, 0, 0 }, /* XP sends a duplicate */
1522     { 0 }
1523 };
1524 /* SetMenu for Visible windows with no size change */
1525 static const struct message WmSetMenuVisibleNoSizeChangeSeq[] = {
1526     { WM_WINDOWPOSCHANGING, sent|wparam, SWP_FRAMECHANGED|SWP_NOACTIVATE|SWP_NOSIZE|SWP_NOMOVE },
1527     { WM_NCCALCSIZE, sent|wparam, 1 },
1528     { WM_NCPAINT, sent|optional }, /* wparam != 1 */
1529     { WM_GETTEXT, sent|defwinproc|optional },
1530     { WM_ERASEBKGND, sent|optional },
1531     { WM_ACTIVATE, sent|optional },
1532     { WM_WINDOWPOSCHANGED, sent|wparam, SWP_FRAMECHANGED|SWP_NOACTIVATE|SWP_NOSIZE|SWP_NOMOVE|SWP_NOCLIENTSIZE|SWP_NOCLIENTMOVE },
1533     { EVENT_OBJECT_LOCATIONCHANGE, winevent_hook|wparam|lparam, 0, 0 },
1534     { 0 }
1535 };
1536 /* DrawMenuBar for a visible window */
1537 static const struct message WmDrawMenuBarSeq[] =
1538 {
1539     { WM_WINDOWPOSCHANGING, sent|wparam, SWP_FRAMECHANGED|SWP_NOACTIVATE|SWP_NOSIZE|SWP_NOMOVE },
1540     { WM_NCCALCSIZE, sent|wparam, 1 },
1541     { 0x0093, sent|defwinproc|optional },
1542     { WM_NCPAINT, sent|optional }, /* wparam != 1 */
1543     { 0x0093, sent|defwinproc|optional },
1544     { 0x0093, sent|defwinproc|optional },
1545     { 0x0091, sent|defwinproc|optional },
1546     { 0x0092, sent|defwinproc|optional },
1547     { WM_GETTEXT, sent|defwinproc|optional },
1548     { WM_ERASEBKGND, sent|optional },
1549     { WM_WINDOWPOSCHANGED, sent|wparam, SWP_FRAMECHANGED|SWP_NOACTIVATE|SWP_NOSIZE|SWP_NOMOVE|SWP_NOCLIENTSIZE|SWP_NOCLIENTMOVE },
1550     { 0x0093, sent|optional },
1551     { EVENT_OBJECT_LOCATIONCHANGE, winevent_hook|wparam|lparam, 0, 0 },
1552     { 0 }
1553 };
1554
1555 static const struct message WmSetRedrawFalseSeq[] =
1556 {
1557     { WM_SETREDRAW, sent|wparam, 0 },
1558     { 0 }
1559 };
1560
1561 static const struct message WmSetRedrawTrueSeq[] =
1562 {
1563     { WM_SETREDRAW, sent|wparam, 1 },
1564     { 0 }
1565 };
1566
1567 static const struct message WmEnableWindowSeq_1[] =
1568 {
1569     { WM_CANCELMODE, sent|wparam|lparam, 0, 0 },
1570     { EVENT_OBJECT_STATECHANGE, winevent_hook|wparam|lparam, 0, 0 },
1571     { HCBT_SETFOCUS, hook|optional },
1572     { WM_KILLFOCUS, sent|optional },
1573     { WM_ENABLE, sent|wparam|lparam, FALSE, 0 },
1574     { 0 }
1575 };
1576
1577 static const struct message WmEnableWindowSeq_2[] =
1578 {
1579     { EVENT_OBJECT_STATECHANGE, winevent_hook|wparam|lparam, 0, 0 },
1580     { WM_ENABLE, sent|wparam|lparam, TRUE, 0 },
1581     { 0 }
1582 };
1583
1584 static const struct message WmGetScrollRangeSeq[] =
1585 {
1586     { SBM_GETRANGE, sent },
1587     { 0 }
1588 };
1589 static const struct message WmGetScrollInfoSeq[] =
1590 {
1591     { SBM_GETSCROLLINFO, sent },
1592     { 0 }
1593 };
1594 static const struct message WmSetScrollRangeSeq[] =
1595 {
1596     /* MSDN claims that Windows sends SBM_SETRANGE message, but win2k SP4
1597        sends SBM_SETSCROLLINFO.
1598      */
1599     { SBM_SETSCROLLINFO, sent },
1600     { 0 }
1601 };
1602 /* SetScrollRange for a window without a non-client area */
1603 static const struct message WmSetScrollRangeHSeq_empty[] =
1604 {
1605     { EVENT_OBJECT_VALUECHANGE, winevent_hook|wparam|lparam, OBJID_HSCROLL, 0 },
1606     { 0 }
1607 };
1608 static const struct message WmSetScrollRangeVSeq_empty[] =
1609 {
1610     { EVENT_OBJECT_VALUECHANGE, winevent_hook|wparam|lparam, OBJID_VSCROLL, 0 },
1611     { 0 }
1612 };
1613 static const struct message WmSetScrollRangeHVSeq[] =
1614 {
1615     { WM_WINDOWPOSCHANGING, sent|wparam, SWP_FRAMECHANGED|SWP_NOACTIVATE|SWP_NOMOVE|SWP_NOSIZE },
1616     { WM_NCCALCSIZE, sent|wparam, 1 },
1617     { WM_GETTEXT, sent|defwinproc|optional },
1618     { WM_ERASEBKGND, sent|optional },
1619     { WM_WINDOWPOSCHANGED, sent|wparam, SWP_FRAMECHANGED|SWP_NOACTIVATE|SWP_NOMOVE|SWP_NOSIZE|SWP_NOCLIENTSIZE|SWP_NOCLIENTMOVE },
1620     { EVENT_OBJECT_LOCATIONCHANGE, winevent_hook|wparam|lparam, 0, 0 },
1621     { EVENT_OBJECT_VALUECHANGE, winevent_hook|lparam|optional, 0/*OBJID_HSCROLL or OBJID_VSCROLL*/, 0 },
1622     { 0 }
1623 };
1624 /* SetScrollRange for a window with a non-client area */
1625 static const struct message WmSetScrollRangeHV_NC_Seq[] =
1626 {
1627     { WM_WINDOWPOSCHANGING, sent|wparam, SWP_FRAMECHANGED|SWP_NOACTIVATE|SWP_NOMOVE|SWP_NOSIZE },
1628     { WM_NCCALCSIZE, sent|wparam, 1 },
1629     { EVENT_OBJECT_REORDER, winevent_hook|wparam|lparam, 0, 0 },
1630     { WM_NCPAINT, sent|optional },
1631     { WM_STYLECHANGING, sent|defwinproc|optional },
1632     { WM_STYLECHANGED, sent|defwinproc|optional },
1633     { WM_STYLECHANGING, sent|defwinproc|optional },
1634     { WM_STYLECHANGED, sent|defwinproc|optional },
1635     { WM_STYLECHANGING, sent|defwinproc|optional },
1636     { WM_STYLECHANGED, sent|defwinproc|optional },
1637     { WM_STYLECHANGING, sent|defwinproc|optional },
1638     { WM_STYLECHANGED, sent|defwinproc|optional },
1639     { WM_GETTEXT, sent|defwinproc|optional },
1640     { WM_GETTEXT, sent|defwinproc|optional },
1641     { WM_ERASEBKGND, sent|optional },
1642     { WM_CTLCOLORDLG, sent|defwinproc|optional }, /* sent to a parent of the dialog */
1643     { WM_WINDOWPOSCHANGED, sent|wparam, SWP_FRAMECHANGED|SWP_NOACTIVATE|SWP_NOMOVE|SWP_NOSIZE|SWP_NOCLIENTMOVE, 0, SWP_NOCLIENTSIZE },
1644     { WM_SIZE, sent|defwinproc|optional },
1645     { EVENT_OBJECT_LOCATIONCHANGE, winevent_hook|wparam|lparam, 0, 0 },
1646     { EVENT_OBJECT_VALUECHANGE, winevent_hook|lparam|optional, 0/*OBJID_HSCROLL or OBJID_VSCROLL*/, 0 },
1647     { WM_GETTEXT, sent|optional },
1648     { WM_GETTEXT, sent|optional },
1649     { WM_GETTEXT, sent|optional },
1650     { WM_GETTEXT, sent|optional },
1651     { 0 }
1652 };
1653 /* test if we receive the right sequence of messages */
1654 /* after calling ShowWindow( SW_SHOWNA) */
1655 static const struct message WmSHOWNAChildInvisParInvis[] = {
1656     { WM_SHOWWINDOW, sent|wparam, 1 },
1657     { 0 }
1658 };
1659 static const struct message WmSHOWNAChildVisParInvis[] = {
1660     { WM_SHOWWINDOW, sent|wparam, 1 },
1661     { 0 }
1662 };
1663 static const struct message WmSHOWNAChildVisParVis[] = {
1664     { WM_SHOWWINDOW, sent|wparam, 1 },
1665     { WM_WINDOWPOSCHANGING, sent|wparam, SWP_SHOWWINDOW|SWP_NOSIZE|SWP_NOMOVE|SWP_NOACTIVATE },
1666     { 0 }
1667 };
1668 static const struct message WmSHOWNAChildInvisParVis[] = {
1669     { WM_SHOWWINDOW, sent|wparam, 1 },
1670     { WM_WINDOWPOSCHANGING, sent|wparam, SWP_SHOWWINDOW|SWP_NOACTIVATE|SWP_NOSIZE|SWP_NOMOVE },
1671     { EVENT_OBJECT_SHOW, winevent_hook|wparam|lparam, 0, 0 },
1672     { WM_ERASEBKGND, sent|optional },
1673     { WM_WINDOWPOSCHANGED, sent|wparam, SWP_SHOWWINDOW|SWP_NOSIZE|SWP_NOMOVE|SWP_NOCLIENTSIZE|SWP_NOACTIVATE|SWP_NOCLIENTMOVE },
1674     { 0 }
1675 };
1676 static const struct message WmSHOWNATopVisible[] = {
1677     { WM_SHOWWINDOW, sent|wparam, 1 },
1678     { WM_WINDOWPOSCHANGING, sent|wparam, SWP_SHOWWINDOW|SWP_NOSIZE|SWP_NOMOVE|SWP_NOACTIVATE },
1679     { WM_NCPAINT, sent|wparam|optional, 1 },
1680     { WM_GETTEXT, sent|defwinproc|optional },
1681     { WM_ERASEBKGND, sent|optional },
1682     { WM_WINDOWPOSCHANGED, sent|optional },
1683     { 0 }
1684 };
1685 static const struct message WmSHOWNATopInvisible[] = {
1686     { WM_NOTIFYFORMAT, sent|optional },
1687     { WM_QUERYUISTATE, sent|optional },
1688     { WM_WINDOWPOSCHANGING, sent|optional },
1689     { WM_GETMINMAXINFO, sent|optional },
1690     { WM_NCCALCSIZE, sent|optional },
1691     { WM_WINDOWPOSCHANGED, sent|optional },
1692     { WM_SHOWWINDOW, sent|wparam, 1 },
1693     { WM_WINDOWPOSCHANGING, sent|wparam, SWP_SHOWWINDOW|SWP_NOACTIVATE|SWP_NOSIZE|SWP_NOMOVE },
1694     { EVENT_OBJECT_SHOW, winevent_hook|wparam|lparam, 0, 0 },
1695     { WM_NCPAINT, sent|wparam|optional, 1 },
1696     { WM_GETTEXT, sent|defwinproc|optional },
1697     { WM_ERASEBKGND, sent|optional },
1698     { WM_WINDOWPOSCHANGED, sent|wparam, SWP_SHOWWINDOW|SWP_NOACTIVATE|SWP_NOSIZE|SWP_NOMOVE|SWP_NOCLIENTSIZE|SWP_NOCLIENTMOVE },
1699     { WM_NCCALCSIZE, sent|wparam|optional, 1 },
1700     { WM_NCPAINT, sent|wparam|optional, 1 },
1701     { WM_ERASEBKGND, sent|optional },
1702     { EVENT_OBJECT_LOCATIONCHANGE, winevent_hook|wparam|lparam, 0, 0 },
1703     { WM_SIZE, sent|wparam, SIZE_RESTORED },
1704     { WM_MOVE, sent },
1705     { 0 }
1706 };
1707
1708 static int after_end_dialog, test_def_id;
1709 static int sequence_cnt, sequence_size;
1710 static struct recvd_message* sequence;
1711 static int log_all_parent_messages;
1712 static int paint_loop_done;
1713
1714 /* user32 functions */
1715 static HWND (WINAPI *pGetAncestor)(HWND,UINT);
1716 static BOOL (WINAPI *pGetMenuInfo)(HMENU,LPCMENUINFO);
1717 static void (WINAPI *pNotifyWinEvent)(DWORD, HWND, LONG, LONG);
1718 static BOOL (WINAPI *pSetMenuInfo)(HMENU,LPCMENUINFO);
1719 static HWINEVENTHOOK (WINAPI *pSetWinEventHook)(DWORD, DWORD, HMODULE, WINEVENTPROC, DWORD, DWORD, DWORD);
1720 static BOOL (WINAPI *pTrackMouseEvent)(TRACKMOUSEEVENT*);
1721 static BOOL (WINAPI *pUnhookWinEvent)(HWINEVENTHOOK);
1722 static BOOL (WINAPI *pGetMonitorInfoA)(HMONITOR,LPMONITORINFO);
1723 static HMONITOR (WINAPI *pMonitorFromPoint)(POINT,DWORD);
1724 /* kernel32 functions */
1725 static BOOL (WINAPI *pGetCPInfoExA)(UINT, DWORD, LPCPINFOEXA);
1726
1727 static void init_procs(void)
1728 {
1729     HMODULE user32 = GetModuleHandleA("user32.dll");
1730     HMODULE kernel32 = GetModuleHandleA("kernel32.dll");
1731
1732 #define GET_PROC(dll, func) \
1733     p ## func = (void*)GetProcAddress(dll, #func); \
1734     if(!p ## func) { \
1735       trace("GetProcAddress(%s) failed\n", #func); \
1736     }
1737
1738     GET_PROC(user32, GetAncestor)
1739     GET_PROC(user32, GetMenuInfo)
1740     GET_PROC(user32, NotifyWinEvent)
1741     GET_PROC(user32, SetMenuInfo)
1742     GET_PROC(user32, SetWinEventHook)
1743     GET_PROC(user32, TrackMouseEvent)
1744     GET_PROC(user32, UnhookWinEvent)
1745     GET_PROC(user32, GetMonitorInfoA)
1746     GET_PROC(user32, MonitorFromPoint)
1747
1748     GET_PROC(kernel32, GetCPInfoExA)
1749
1750 #undef GET_PROC
1751 }
1752
1753 static const char *get_winpos_flags(UINT flags)
1754 {
1755     static char buffer[300];
1756
1757     buffer[0] = 0;
1758 #define DUMP(flag) do { if (flags & flag) { strcat( buffer, "|" #flag ); flags &= ~flag; } } while(0)
1759     DUMP( SWP_SHOWWINDOW );
1760     DUMP( SWP_HIDEWINDOW );
1761     DUMP( SWP_NOACTIVATE );
1762     DUMP( SWP_FRAMECHANGED );
1763     DUMP( SWP_NOCOPYBITS );
1764     DUMP( SWP_NOOWNERZORDER );
1765     DUMP( SWP_NOSENDCHANGING );
1766     DUMP( SWP_DEFERERASE );
1767     DUMP( SWP_ASYNCWINDOWPOS );
1768     DUMP( SWP_NOZORDER );
1769     DUMP( SWP_NOREDRAW );
1770     DUMP( SWP_NOSIZE );
1771     DUMP( SWP_NOMOVE );
1772     DUMP( SWP_NOCLIENTSIZE );
1773     DUMP( SWP_NOCLIENTMOVE );
1774     if (flags) sprintf(buffer + strlen(buffer),"|0x%04x", flags);
1775     return buffer + 1;
1776 #undef DUMP
1777 }
1778
1779 static BOOL ignore_message( UINT message )
1780 {
1781     /* these are always ignored */
1782     return (message >= 0xc000 ||
1783             message == WM_GETICON ||
1784             message == WM_GETOBJECT ||
1785             message == WM_TIMECHANGE ||
1786             message == WM_DISPLAYCHANGE ||
1787             message == WM_DEVICECHANGE ||
1788             message == WM_DWMNCRENDERINGCHANGED);
1789 }
1790
1791
1792 #define add_message(msg) add_message_(__LINE__,msg);
1793 static void add_message_(int line, const struct recvd_message *msg)
1794 {
1795     struct recvd_message *seq;
1796
1797     if (!sequence) 
1798     {
1799         sequence_size = 10;
1800         sequence = HeapAlloc( GetProcessHeap(), 0, sequence_size * sizeof(*sequence) );
1801     }
1802     if (sequence_cnt == sequence_size) 
1803     {
1804         sequence_size *= 2;
1805         sequence = HeapReAlloc( GetProcessHeap(), 0, sequence, sequence_size * sizeof(*sequence) );
1806     }
1807     assert(sequence);
1808
1809     seq = &sequence[sequence_cnt];
1810     seq->hwnd = msg->hwnd;
1811     seq->message = msg->message;
1812     seq->flags = msg->flags;
1813     seq->wParam = msg->wParam;
1814     seq->lParam = msg->lParam;
1815     seq->line   = line;
1816     seq->descr  = msg->descr;
1817     seq->output[0] = 0;
1818
1819     if (msg->descr)
1820     {
1821         if (msg->flags & hook)
1822         {
1823             static const char * const CBT_code_name[10] =
1824             {
1825                 "HCBT_MOVESIZE",
1826                 "HCBT_MINMAX",
1827                 "HCBT_QS",
1828                 "HCBT_CREATEWND",
1829                 "HCBT_DESTROYWND",
1830                 "HCBT_ACTIVATE",
1831                 "HCBT_CLICKSKIPPED",
1832                 "HCBT_KEYSKIPPED",
1833                 "HCBT_SYSCOMMAND",
1834                 "HCBT_SETFOCUS"
1835             };
1836             const char *code_name = (msg->message <= HCBT_SETFOCUS) ? CBT_code_name[msg->message] : "Unknown";
1837
1838             sprintf( seq->output, "%s: hook %d (%s) wp %08lx lp %08lx",
1839                      msg->descr, msg->message, code_name, msg->wParam, msg->lParam );
1840         }
1841         else if (msg->flags & winevent_hook)
1842         {
1843             sprintf( seq->output, "%s: winevent %p %08x %08lx %08lx",
1844                      msg->descr, msg->hwnd, msg->message, msg->wParam, msg->lParam );
1845         }
1846         else
1847         {
1848             switch (msg->message)
1849             {
1850             case WM_WINDOWPOSCHANGING:
1851             case WM_WINDOWPOSCHANGED:
1852             {
1853                 WINDOWPOS *winpos = (WINDOWPOS *)msg->lParam;
1854
1855                 sprintf( seq->output, "%s: %p WM_WINDOWPOS%s wp %08lx lp %08lx after %p x %d y %d cx %d cy %d flags %s",
1856                           msg->descr, msg->hwnd,
1857                           (msg->message == WM_WINDOWPOSCHANGING) ? "CHANGING" : "CHANGED",
1858                           msg->wParam, msg->lParam, winpos->hwndInsertAfter,
1859                           winpos->x, winpos->y, winpos->cx, winpos->cy,
1860                           get_winpos_flags(winpos->flags) );
1861
1862                 /* Log only documented flags, win2k uses 0x1000 and 0x2000
1863                  * in the high word for internal purposes
1864                  */
1865                 seq->wParam = winpos->flags & 0xffff;
1866                 /* We are not interested in the flags that don't match under XP and Win9x */
1867                 seq->wParam &= ~SWP_NOZORDER;
1868                 break;
1869             }
1870
1871             case WM_DRAWITEM:
1872             {
1873                 DRAW_ITEM_STRUCT di;
1874                 DRAWITEMSTRUCT *dis = (DRAWITEMSTRUCT *)msg->lParam;
1875
1876                 sprintf( seq->output, "%s: %p WM_DRAWITEM: type %x, ctl_id %x, item_id %x, action %x, state %x",
1877                          msg->descr, msg->hwnd, dis->CtlType, dis->CtlID,
1878                          dis->itemID, dis->itemAction, dis->itemState);
1879
1880                 di.u.lp = 0;
1881                 di.u.item.type = dis->CtlType;
1882                 di.u.item.ctl_id = dis->CtlID;
1883                 if (dis->CtlType == ODT_LISTBOX ||
1884                     dis->CtlType == ODT_COMBOBOX ||
1885                     dis->CtlType == ODT_MENU)
1886                     di.u.item.item_id = dis->itemID;
1887                 di.u.item.action = dis->itemAction;
1888                 di.u.item.state = dis->itemState;
1889
1890                 seq->lParam = di.u.lp;
1891                 break;
1892             }
1893             default:
1894                 if (msg->message >= 0xc000) return;  /* ignore registered messages */
1895                 sprintf( seq->output, "%s: %p %04x wp %08lx lp %08lx",
1896                          msg->descr, msg->hwnd, msg->message, msg->wParam, msg->lParam );
1897             }
1898             if (msg->flags & (sent|posted|parent|defwinproc|beginpaint))
1899                 sprintf( seq->output + strlen(seq->output), " (flags %x)", msg->flags );
1900         }
1901     }
1902
1903     sequence_cnt++;
1904 }
1905
1906 /* try to make sure pending X events have been processed before continuing */
1907 static void flush_events(void)
1908 {
1909     MSG msg;
1910     int diff = 200;
1911     int min_timeout = 100;
1912     DWORD time = GetTickCount() + diff;
1913
1914     while (diff > 0)
1915     {
1916         if (MsgWaitForMultipleObjects( 0, NULL, FALSE, min_timeout, QS_ALLINPUT ) == WAIT_TIMEOUT) break;
1917         while (PeekMessage( &msg, 0, 0, 0, PM_REMOVE )) DispatchMessage( &msg );
1918         diff = time - GetTickCount();
1919     }
1920 }
1921
1922 static void flush_sequence(void)
1923 {
1924     HeapFree(GetProcessHeap(), 0, sequence);
1925     sequence = 0;
1926     sequence_cnt = sequence_size = 0;
1927 }
1928
1929 static void dump_sequence(const struct message *expected, const char *context, const char *file, int line)
1930 {
1931     const struct recvd_message *actual = sequence;
1932     unsigned int count = 0;
1933
1934     trace_(file, line)("Failed sequence %s:\n", context );
1935     while (expected->message && actual->message)
1936     {
1937         if (actual->output[0])
1938         {
1939             if (expected->flags & hook)
1940             {
1941                 trace_(file, line)( "  %u: expected: hook %04x - actual: %s\n",
1942                                     count, expected->message, actual->output );
1943             }
1944             else if (expected->flags & winevent_hook)
1945             {
1946                 trace_(file, line)( "  %u: expected: winevent %04x - actual: %s\n",
1947                                     count, expected->message, actual->output );
1948             }
1949             else if (expected->flags & kbd_hook)
1950             {
1951                 trace_(file, line)( "  %u: expected: kbd %04x - actual: %s\n",
1952                                     count, expected->message, actual->output );
1953             }
1954             else
1955             {
1956                 trace_(file, line)( "  %u: expected: msg %04x - actual: %s\n",
1957                                     count, expected->message, actual->output );
1958             }
1959         }
1960
1961         if (expected->message == actual->message)
1962         {
1963             if ((expected->flags & defwinproc) != (actual->flags & defwinproc) &&
1964                 (expected->flags & optional))
1965             {
1966                 /* don't match messages if their defwinproc status differs */
1967                 expected++;
1968             }
1969             else
1970             {
1971                 expected++;
1972                 actual++;
1973             }
1974         }
1975         /* silently drop winevent messages if there is no support for them */
1976         else if ((expected->flags & optional) || ((expected->flags & winevent_hook) && !hEvent_hook))
1977             expected++;
1978         else
1979         {
1980             expected++;
1981             actual++;
1982         }
1983         count++;
1984     }
1985
1986     /* optional trailing messages */
1987     while (expected->message && ((expected->flags & optional) ||
1988             ((expected->flags & winevent_hook) && !hEvent_hook)))
1989     {
1990         trace_(file, line)( "  %u: expected: msg %04x - actual: nothing\n", count, expected->message );
1991         expected++;
1992         count++;
1993     }
1994
1995     if (expected->message)
1996     {
1997         trace_(file, line)( "  %u: expected: msg %04x - actual: nothing\n", count, expected->message );
1998         return;
1999     }
2000
2001     while (actual->message && actual->output[0])
2002     {
2003         trace_(file, line)( "  %u: expected: nothing - actual: %s\n", count, actual->output );
2004         actual++;
2005         count++;
2006     }
2007 }
2008
2009 #define ok_sequence( exp, contx, todo) \
2010         ok_sequence_( (exp), (contx), (todo), __FILE__, __LINE__)
2011
2012
2013 static void ok_sequence_(const struct message *expected_list, const char *context, int todo,
2014                          const char *file, int line)
2015 {
2016     static const struct recvd_message end_of_sequence;
2017     const struct message *expected = expected_list;
2018     const struct recvd_message *actual;
2019     int failcount = 0, dump = 0;
2020     unsigned int count = 0;
2021
2022     add_message(&end_of_sequence);
2023
2024     actual = sequence;
2025
2026     while (expected->message && actual->message)
2027     {
2028         if (expected->message == actual->message &&
2029             !((expected->flags ^ actual->flags) & (hook|winevent_hook|kbd_hook)))
2030         {
2031             if (expected->flags & wparam)
2032             {
2033                 if (((expected->wParam ^ actual->wParam) & ~expected->wp_mask) && todo)
2034                 {
2035                     todo_wine {
2036                         failcount ++;
2037                         if (strcmp(winetest_platform, "wine")) dump++;
2038                         ok_( file, line) (FALSE,
2039                             "%s: %u: in msg 0x%04x expecting wParam 0x%lx got 0x%lx\n",
2040                             context, count, expected->message, expected->wParam, actual->wParam);
2041                     }
2042                 }
2043                 else
2044                 {
2045                     ok_( file, line)( ((expected->wParam ^ actual->wParam) & ~expected->wp_mask) == 0,
2046                                      "%s: %u: in msg 0x%04x expecting wParam 0x%lx got 0x%lx\n",
2047                                      context, count, expected->message, expected->wParam, actual->wParam);
2048                     if ((expected->wParam ^ actual->wParam) & ~expected->wp_mask) dump++;
2049                 }
2050
2051             }
2052             if (expected->flags & lparam)
2053             {
2054                 if (((expected->lParam ^ actual->lParam) & ~expected->lp_mask) && todo)
2055                 {
2056                     todo_wine {
2057                         failcount ++;
2058                         if (strcmp(winetest_platform, "wine")) dump++;
2059                         ok_( file, line) (FALSE,
2060                             "%s: %u: in msg 0x%04x expecting lParam 0x%lx got 0x%lx\n",
2061                             context, count, expected->message, expected->lParam, actual->lParam);
2062                     }
2063                 }
2064                 else
2065                 {
2066                     ok_( file, line)(((expected->lParam ^ actual->lParam) & ~expected->lp_mask) == 0,
2067                                      "%s: %u: in msg 0x%04x expecting lParam 0x%lx got 0x%lx\n",
2068                                      context, count, expected->message, expected->lParam, actual->lParam);
2069                     if ((expected->lParam ^ actual->lParam) & ~expected->lp_mask) dump++;
2070                 }
2071             }
2072             if ((expected->flags & optional) &&
2073                 ((expected->flags ^ actual->flags) & (defwinproc|parent)))
2074             {
2075                 /* don't match optional messages if their defwinproc or parent status differs */
2076                 expected++;
2077                 count++;
2078                 continue;
2079             }
2080             if ((expected->flags & defwinproc) != (actual->flags & defwinproc) && todo)
2081             {
2082                     todo_wine {
2083                         failcount ++;
2084                         if (strcmp(winetest_platform, "wine")) dump++;
2085                         ok_( file, line) (FALSE,
2086                             "%s: %u: the msg 0x%04x should %shave been sent by DefWindowProc\n",
2087                             context, count, expected->message, (expected->flags & defwinproc) ? "" : "NOT ");
2088                     }
2089             }
2090             else
2091             {
2092                 ok_( file, line) ((expected->flags & defwinproc) == (actual->flags & defwinproc),
2093                     "%s: %u: the msg 0x%04x should %shave been sent by DefWindowProc\n",
2094                     context, count, expected->message, (expected->flags & defwinproc) ? "" : "NOT ");
2095                 if ((expected->flags & defwinproc) != (actual->flags & defwinproc)) dump++;
2096             }
2097
2098             ok_( file, line) ((expected->flags & beginpaint) == (actual->flags & beginpaint),
2099                 "%s: %u: the msg 0x%04x should %shave been sent by BeginPaint\n",
2100                 context, count, expected->message, (expected->flags & beginpaint) ? "" : "NOT ");
2101             if ((expected->flags & beginpaint) != (actual->flags & beginpaint)) dump++;
2102
2103             ok_( file, line) ((expected->flags & (sent|posted)) == (actual->flags & (sent|posted)),
2104                 "%s: %u: the msg 0x%04x should have been %s\n",
2105                 context, count, expected->message, (expected->flags & posted) ? "posted" : "sent");
2106             if ((expected->flags & (sent|posted)) != (actual->flags & (sent|posted))) dump++;
2107
2108             ok_( file, line) ((expected->flags & parent) == (actual->flags & parent),
2109                 "%s: %u: the msg 0x%04x was expected in %s\n",
2110                 context, count, expected->message, (expected->flags & parent) ? "parent" : "child");
2111             if ((expected->flags & parent) != (actual->flags & parent)) dump++;
2112
2113             ok_( file, line) ((expected->flags & hook) == (actual->flags & hook),
2114                 "%s: %u: the msg 0x%04x should have been sent by a hook\n",
2115                 context, count, expected->message);
2116             if ((expected->flags & hook) != (actual->flags & hook)) dump++;
2117
2118             ok_( file, line) ((expected->flags & winevent_hook) == (actual->flags & winevent_hook),
2119                 "%s: %u: the msg 0x%04x should have been sent by a winevent hook\n",
2120                 context, count, expected->message);
2121             if ((expected->flags & winevent_hook) != (actual->flags & winevent_hook)) dump++;
2122
2123             ok_( file, line) ((expected->flags & kbd_hook) == (actual->flags & kbd_hook),
2124                 "%s: %u: the msg 0x%04x should have been sent by a keyboard hook\n",
2125                 context, count, expected->message);
2126             if ((expected->flags & kbd_hook) != (actual->flags & kbd_hook)) dump++;
2127
2128             expected++;
2129             actual++;
2130         }
2131         /* silently drop hook messages if there is no support for them */
2132         else if ((expected->flags & optional) ||
2133                  ((expected->flags & hook) && !hCBT_hook) ||
2134                  ((expected->flags & winevent_hook) && !hEvent_hook) ||
2135                  ((expected->flags & kbd_hook) && !hKBD_hook))
2136             expected++;
2137         else if (todo)
2138         {
2139             failcount++;
2140             todo_wine {
2141                 if (strcmp(winetest_platform, "wine")) dump++;
2142                 ok_( file, line) (FALSE, "%s: %u: the msg 0x%04x was expected, but got msg 0x%04x instead\n",
2143                                   context, count, expected->message, actual->message);
2144             }
2145             goto done;
2146         }
2147         else
2148         {
2149             ok_( file, line) (FALSE, "%s: %u: the msg 0x%04x was expected, but got msg 0x%04x instead\n",
2150                               context, count, expected->message, actual->message);
2151             dump++;
2152             expected++;
2153             actual++;
2154         }
2155         count++;
2156     }
2157
2158     /* skip all optional trailing messages */
2159     while (expected->message && ((expected->flags & optional) ||
2160                                  ((expected->flags & hook) && !hCBT_hook) ||
2161                                  ((expected->flags & winevent_hook) && !hEvent_hook)))
2162         expected++;
2163
2164     if (todo)
2165     {
2166         todo_wine {
2167             if (expected->message || actual->message) {
2168                 failcount++;
2169                 if (strcmp(winetest_platform, "wine")) dump++;
2170                 ok_( file, line) (FALSE, "%s: %u: the msg sequence is not complete: expected %04x - actual %04x\n",
2171                                   context, count, expected->message, actual->message);
2172             }
2173         }
2174     }
2175     else
2176     {
2177         if (expected->message || actual->message)
2178         {
2179             dump++;
2180             ok_( file, line) (FALSE, "%s: %u: the msg sequence is not complete: expected %04x - actual %04x\n",
2181                               context, count, expected->message, actual->message);
2182         }
2183     }
2184     if( todo && !failcount) /* succeeded yet marked todo */
2185         todo_wine {
2186             if (!strcmp(winetest_platform, "wine")) dump++;
2187             ok_( file, line)( TRUE, "%s: marked \"todo_wine\" but succeeds\n", context);
2188         }
2189
2190 done:
2191     if (dump) dump_sequence(expected_list, context, file, line);
2192     flush_sequence();
2193 }
2194
2195 #define expect(EXPECTED,GOT) ok((GOT)==(EXPECTED), "Expected %d, got %d\n", (EXPECTED), (GOT))
2196
2197 /******************************** MDI test **********************************/
2198
2199 /* CreateWindow for MDI frame window, initially visible */
2200 static const struct message WmCreateMDIframeSeq[] = {
2201     { HCBT_CREATEWND, hook },
2202     { WM_GETMINMAXINFO, sent },
2203     { WM_NCCREATE, sent },
2204     { WM_NCCALCSIZE, sent|wparam, 0 },
2205     { EVENT_OBJECT_REORDER, winevent_hook|wparam|lparam, 0, 0 },
2206     { WM_CREATE, sent },
2207     { EVENT_OBJECT_CREATE, winevent_hook|wparam|lparam, 0, 0 },
2208     { WM_NOTIFYFORMAT, sent|optional },
2209     { WM_QUERYUISTATE, sent|optional },
2210     { WM_WINDOWPOSCHANGING, sent|optional },
2211     { WM_GETMINMAXINFO, sent|optional },
2212     { WM_NCCALCSIZE, sent|optional },
2213     { WM_WINDOWPOSCHANGED, sent|optional },
2214     { WM_SHOWWINDOW, sent|wparam, 1 },
2215     { WM_WINDOWPOSCHANGING, sent|wparam, SWP_SHOWWINDOW|SWP_NOSIZE|SWP_NOMOVE },
2216     { EVENT_OBJECT_SHOW, winevent_hook|wparam|lparam, 0, 0 },
2217     { HCBT_ACTIVATE, hook },
2218     { EVENT_SYSTEM_FOREGROUND, winevent_hook|wparam|lparam, 0, 0 },
2219     { WM_QUERYNEWPALETTE, sent|wparam|lparam|optional, 0, 0 },
2220     { WM_WINDOWPOSCHANGING, sent|wparam|optional, SWP_NOSIZE|SWP_NOMOVE }, /* XP */
2221     { WM_ACTIVATEAPP, sent|wparam|optional, 1 }, /* Win9x doesn't send it */
2222     { WM_NCACTIVATE, sent },
2223     { WM_GETTEXT, sent|defwinproc|optional },
2224     { WM_ACTIVATE, sent|wparam, 1 },
2225     { WM_WINDOWPOSCHANGING, sent|wparam|optional, SWP_NOSIZE|SWP_NOMOVE }, /* Win9x */
2226     { HCBT_SETFOCUS, hook },
2227     { WM_IME_SETCONTEXT, sent|wparam|defwinproc|optional, 1 },
2228     { WM_IME_NOTIFY, sent|wparam|defwinproc|optional, 2 },
2229     { EVENT_OBJECT_FOCUS, winevent_hook|wparam|lparam, OBJID_CLIENT, 0 },
2230     { WM_SETFOCUS, sent|wparam|defwinproc, 0 },
2231     /* Win9x adds SWP_NOZORDER below */
2232     { WM_WINDOWPOSCHANGED, sent|wparam, SWP_SHOWWINDOW|SWP_NOSIZE|SWP_NOMOVE|SWP_NOCLIENTSIZE|SWP_NOCLIENTMOVE },
2233     { WM_NCCALCSIZE, sent|wparam|optional, 1 }, /* XP */
2234     { EVENT_OBJECT_LOCATIONCHANGE, winevent_hook|wparam|lparam, 0, 0 },
2235     { WM_SIZE, sent|wparam, SIZE_RESTORED },
2236     { WM_MOVE, sent },
2237     { 0 }
2238 };
2239 /* DestroyWindow for MDI frame window, initially visible */
2240 static const struct message WmDestroyMDIframeSeq[] = {
2241     { HCBT_DESTROYWND, hook },
2242     { 0x0090, sent|optional },
2243     { WM_WINDOWPOSCHANGING, sent|wparam|optional, SWP_HIDEWINDOW|SWP_NOACTIVATE|SWP_NOSIZE|SWP_NOMOVE },
2244     { EVENT_OBJECT_HIDE, winevent_hook|wparam|lparam, 0, 0 },
2245     { WM_NCACTIVATE, sent|wparam|optional, 0 }, /* Win9x */
2246     { WM_WINDOWPOSCHANGED, sent|wparam|optional, SWP_HIDEWINDOW|SWP_NOACTIVATE|SWP_NOSIZE|SWP_NOMOVE|SWP_NOCLIENTSIZE|SWP_NOCLIENTMOVE },
2247     { WM_NCACTIVATE, sent|wparam|optional, 0 }, /* XP */
2248     { WM_ACTIVATE, sent|wparam|optional, 0 }, /* Win9x */
2249     { WM_ACTIVATEAPP, sent|wparam|optional, 0 }, /* Win9x */
2250     { EVENT_OBJECT_HIDE, winevent_hook|wparam|lparam, OBJID_CARET, 0 },
2251     { EVENT_OBJECT_DESTROY, winevent_hook|wparam|lparam, 0, 0 },
2252     { WM_DESTROY, sent },
2253     { WM_NCDESTROY, sent },
2254     { 0 }
2255 };
2256 /* CreateWindow for MDI client window, initially visible */
2257 static const struct message WmCreateMDIclientSeq[] = {
2258     { HCBT_CREATEWND, hook },
2259     { WM_NCCREATE, sent },
2260     { WM_NCCALCSIZE, sent|wparam, 0 },
2261     { EVENT_OBJECT_REORDER, winevent_hook|wparam|lparam|optional, 0, 0 },
2262     { WM_CREATE, sent },
2263     { EVENT_OBJECT_REORDER, winevent_hook|wparam|lparam|optional, 0, 0 },
2264     { EVENT_OBJECT_CREATE, winevent_hook|wparam|lparam, 0, 0 },
2265     { WM_SIZE, sent|wparam, SIZE_RESTORED },
2266     { WM_MOVE, sent },
2267     { WM_PARENTNOTIFY, sent|wparam, WM_CREATE }, /* in MDI frame */
2268     { WM_SHOWWINDOW, sent|wparam, 1 },
2269     { WM_WINDOWPOSCHANGING, sent|wparam|optional, SWP_SHOWWINDOW|SWP_NOACTIVATE|SWP_NOSIZE|SWP_NOMOVE },
2270     { EVENT_OBJECT_SHOW, winevent_hook|wparam|lparam, 0, 0 },
2271     { WM_WINDOWPOSCHANGED, sent|wparam|optional, SWP_SHOWWINDOW|SWP_NOACTIVATE|SWP_NOSIZE|SWP_NOMOVE|SWP_NOCLIENTSIZE|SWP_NOCLIENTMOVE },
2272     { 0 }
2273 };
2274 /* ShowWindow(SW_SHOW) for MDI client window */
2275 static const struct message WmShowMDIclientSeq[] = {
2276     { WM_SHOWWINDOW, sent|wparam, 1 },
2277     { WM_WINDOWPOSCHANGING, sent|wparam|optional, SWP_SHOWWINDOW|SWP_NOACTIVATE|SWP_NOSIZE|SWP_NOMOVE },
2278     { EVENT_OBJECT_SHOW, winevent_hook|wparam|lparam, 0, 0 },
2279     { WM_WINDOWPOSCHANGED, sent|wparam|optional, SWP_SHOWWINDOW|SWP_NOACTIVATE|SWP_NOSIZE|SWP_NOMOVE|SWP_NOCLIENTSIZE|SWP_NOCLIENTMOVE },
2280     { 0 }
2281 };
2282 /* ShowWindow(SW_HIDE) for MDI client window */
2283 static const struct message WmHideMDIclientSeq[] = {
2284     { WM_SHOWWINDOW, sent|wparam, 0 },
2285     { WM_WINDOWPOSCHANGING, sent|wparam|optional, SWP_HIDEWINDOW|SWP_NOACTIVATE|SWP_NOSIZE|SWP_NOMOVE },
2286     { EVENT_OBJECT_SHOW, winevent_hook|wparam|lparam|optional, 0, 0 }, /* win2000 */
2287     { EVENT_OBJECT_HIDE, winevent_hook|wparam|lparam|optional, 0, 0 }, /* XP */
2288     { WM_WINDOWPOSCHANGED, sent|wparam|optional, SWP_HIDEWINDOW|SWP_NOACTIVATE|SWP_NOSIZE|SWP_NOMOVE|SWP_NOCLIENTSIZE|SWP_NOCLIENTMOVE },
2289     { 0 }
2290 };
2291 /* DestroyWindow for MDI client window, initially visible */
2292 static const struct message WmDestroyMDIclientSeq[] = {
2293     { HCBT_DESTROYWND, hook },
2294     { 0x0090, sent|optional },
2295     { WM_PARENTNOTIFY, sent|wparam, WM_DESTROY }, /* in MDI frame */
2296     { WM_SHOWWINDOW, sent|wparam, 0 },
2297     { WM_WINDOWPOSCHANGING, sent|wparam|optional, SWP_HIDEWINDOW|SWP_NOACTIVATE|SWP_NOSIZE|SWP_NOMOVE },
2298     { EVENT_OBJECT_HIDE, winevent_hook|wparam|lparam, 0, 0 },
2299     { WM_WINDOWPOSCHANGED, sent|wparam|optional, SWP_HIDEWINDOW|SWP_NOACTIVATE|SWP_NOSIZE|SWP_NOMOVE|SWP_NOCLIENTSIZE|SWP_NOCLIENTMOVE },
2300     { EVENT_OBJECT_DESTROY, winevent_hook|wparam|lparam, 0, 0 },
2301     { WM_DESTROY, sent },
2302     { WM_NCDESTROY, sent },
2303     { 0 }
2304 };
2305 /* CreateWindow for MDI child window, initially visible */
2306 static const struct message WmCreateMDIchildVisibleSeq[] = {
2307     { HCBT_CREATEWND, hook },
2308     { WM_NCCREATE, sent }, 
2309     { WM_NCCALCSIZE, sent|wparam, 0 },
2310     { WM_CREATE, sent },
2311     { EVENT_OBJECT_CREATE, winevent_hook|wparam|lparam, 0, 0 },
2312     { WM_SIZE, sent|wparam, SIZE_RESTORED },
2313     { WM_MOVE, sent },
2314     /* Win2k sends wparam set to
2315      * MAKEWPARAM(WM_CREATE, MDI_FIRST_CHILD_ID + nTotalCreated),
2316      * while Win9x doesn't bother to set child window id according to
2317      * CLIENTCREATESTRUCT.idFirstChild
2318      */
2319     { WM_PARENTNOTIFY, sent /*|wparam, WM_CREATE*/ }, /* in MDI client */
2320     { WM_SHOWWINDOW, sent|wparam, 1 },
2321     { WM_WINDOWPOSCHANGING, sent|wparam, SWP_SHOWWINDOW|SWP_NOACTIVATE|SWP_NOSIZE|SWP_NOMOVE },
2322     { EVENT_OBJECT_SHOW, winevent_hook|wparam|lparam, 0, 0 },
2323     { WM_WINDOWPOSCHANGED, sent|wparam, SWP_SHOWWINDOW|SWP_NOACTIVATE|SWP_NOSIZE|SWP_NOMOVE|SWP_NOCLIENTSIZE|SWP_NOCLIENTMOVE },
2324     { WM_MDIREFRESHMENU, sent/*|wparam|lparam, 0, 0*/ },
2325     { WM_WINDOWPOSCHANGING, sent|wparam, SWP_SHOWWINDOW|SWP_NOSIZE|SWP_NOMOVE },
2326     { WM_CHILDACTIVATE, sent|wparam|lparam, 0, 0 },
2327     { WM_WINDOWPOSCHANGING, sent|wparam|defwinproc, SWP_NOACTIVATE|SWP_NOSIZE|SWP_NOMOVE },
2328
2329     /* Win9x: message sequence terminates here. */
2330
2331     { WM_NCACTIVATE, sent|wparam|defwinproc, 1 },
2332     { HCBT_SETFOCUS, hook }, /* in MDI client */
2333     { WM_IME_SETCONTEXT, sent|wparam|optional, 1 }, /* in MDI client */
2334     { WM_IME_NOTIFY, sent|wparam|optional, 2 }, /* in MDI client */
2335     { EVENT_OBJECT_FOCUS, winevent_hook|wparam|lparam, OBJID_CLIENT, 0 },
2336     { WM_SETFOCUS, sent }, /* in MDI client */
2337     { HCBT_SETFOCUS, hook },
2338     { WM_KILLFOCUS, sent }, /* in MDI client */
2339     { WM_IME_SETCONTEXT, sent|wparam|optional, 0 }, /* in MDI client */
2340     { WM_IME_SETCONTEXT, sent|wparam|defwinproc|optional, 1 },
2341     { EVENT_OBJECT_FOCUS, winevent_hook|wparam|lparam, OBJID_CLIENT, 0 },
2342     { WM_SETFOCUS, sent|defwinproc },
2343     { WM_MDIACTIVATE, sent|defwinproc },
2344     { 0 }
2345 };
2346 /* CreateWindow for MDI child window with invisible parent */
2347 static const struct message WmCreateMDIchildInvisibleParentSeq[] = {
2348     { HCBT_CREATEWND, hook },
2349     { WM_GETMINMAXINFO, sent },
2350     { WM_NCCREATE, sent }, 
2351     { WM_NCCALCSIZE, sent|wparam, 0 },
2352     { EVENT_OBJECT_REORDER, winevent_hook|wparam|lparam|optional, 0, 0 },
2353     { WM_CREATE, sent },
2354     { EVENT_OBJECT_CREATE, winevent_hook|wparam|lparam, 0, 0 },
2355     { WM_SIZE, sent|wparam, SIZE_RESTORED },
2356     { WM_MOVE, sent },
2357     { WM_PARENTNOTIFY, sent /*|wparam, WM_CREATE*/ }, /* in MDI client */
2358     { WM_SHOWWINDOW, sent|wparam, 1 },
2359     { WM_MDIREFRESHMENU, sent }, /* in MDI client */
2360     { WM_WINDOWPOSCHANGING, sent|wparam, SWP_SHOWWINDOW|SWP_NOSIZE|SWP_NOMOVE },
2361     { WM_CHILDACTIVATE, sent|wparam|lparam, 0, 0 },
2362     { WM_WINDOWPOSCHANGING, sent|wparam|defwinproc, SWP_NOACTIVATE|SWP_NOSIZE|SWP_NOMOVE },
2363
2364     /* Win9x: message sequence terminates here. */
2365
2366     { WM_NCACTIVATE, sent|wparam|defwinproc, 1 },
2367     { HCBT_SETFOCUS, hook }, /* in MDI client */
2368     { WM_IME_SETCONTEXT, sent|wparam|optional, 1 }, /* in MDI client */
2369     { WM_IME_NOTIFY, sent|wparam|optional, 2 }, /* in MDI client */
2370     { EVENT_OBJECT_FOCUS, winevent_hook|wparam|lparam, OBJID_CLIENT, 0 },
2371     { WM_SETFOCUS, sent }, /* in MDI client */
2372     { HCBT_SETFOCUS, hook },
2373     { WM_KILLFOCUS, sent }, /* in MDI client */
2374     { WM_IME_SETCONTEXT, sent|wparam|optional, 0 }, /* in MDI client */
2375     { WM_IME_SETCONTEXT, sent|wparam|defwinproc|optional, 1 },
2376     { EVENT_OBJECT_FOCUS, winevent_hook|wparam|lparam, OBJID_CLIENT, 0 },
2377     { WM_SETFOCUS, sent|defwinproc },
2378     { WM_MDIACTIVATE, sent|defwinproc },
2379     { 0 }
2380 };
2381 /* DestroyWindow for MDI child window, initially visible */
2382 static const struct message WmDestroyMDIchildVisibleSeq[] = {
2383     { HCBT_DESTROYWND, hook },
2384     /* Win2k sends wparam set to
2385      * MAKEWPARAM(WM_DESTROY, MDI_FIRST_CHILD_ID + nTotalCreated),
2386      * while Win9x doesn't bother to set child window id according to
2387      * CLIENTCREATESTRUCT.idFirstChild
2388      */
2389     { 0x0090, sent|optional },
2390     { WM_PARENTNOTIFY, sent /*|wparam, WM_DESTROY*/ }, /* in MDI client */
2391     { WM_SHOWWINDOW, sent|wparam, 0 },
2392     { WM_WINDOWPOSCHANGING, sent|wparam|optional, SWP_HIDEWINDOW|SWP_NOACTIVATE|SWP_NOSIZE|SWP_NOMOVE },
2393     { EVENT_OBJECT_HIDE, winevent_hook|wparam|lparam, 0, 0 },
2394     { WM_ERASEBKGND, sent|parent|optional },
2395     { WM_WINDOWPOSCHANGED, sent|wparam|optional, SWP_HIDEWINDOW|SWP_NOACTIVATE|SWP_NOSIZE|SWP_NOMOVE|SWP_NOCLIENTSIZE|SWP_NOCLIENTMOVE },
2396
2397     /* { WM_DESTROY, sent }
2398      * Win9x: message sequence terminates here.
2399      */
2400
2401     { HCBT_SETFOCUS, hook }, /* set focus to MDI client */
2402     { WM_KILLFOCUS, sent },
2403     { WM_IME_SETCONTEXT, sent|wparam|optional, 0 },
2404     { WM_IME_SETCONTEXT, sent|wparam|optional, 1 }, /* in MDI client */
2405     { EVENT_OBJECT_FOCUS, winevent_hook|wparam|lparam, OBJID_CLIENT, 0 },
2406     { WM_SETFOCUS, sent }, /* in MDI client */
2407
2408     { HCBT_SETFOCUS, hook }, /* MDI client sets focus back to MDI child */
2409     { WM_KILLFOCUS, sent }, /* in MDI client */
2410     { WM_IME_SETCONTEXT, sent|wparam|optional, 0 }, /* in MDI client */
2411     { WM_IME_SETCONTEXT, sent|wparam|optional, 1 },
2412     { EVENT_OBJECT_FOCUS, winevent_hook|wparam|lparam, OBJID_CLIENT, 0 },
2413     { WM_SETFOCUS, sent }, /* in MDI client */
2414
2415     { EVENT_OBJECT_DESTROY, winevent_hook|wparam|lparam, 0, 0 },
2416
2417     { HCBT_SETFOCUS, hook }, /* set focus to MDI client */
2418     { WM_KILLFOCUS, sent },
2419     { WM_IME_SETCONTEXT, sent|wparam|optional, 0 },
2420     { WM_IME_SETCONTEXT, sent|wparam|optional, 1 }, /* in MDI client */
2421     { EVENT_OBJECT_FOCUS, winevent_hook|wparam|lparam, OBJID_CLIENT, 0 },
2422     { WM_SETFOCUS, sent }, /* in MDI client */
2423
2424     { HCBT_SETFOCUS, hook }, /* MDI client sets focus back to MDI child */
2425     { WM_KILLFOCUS, sent }, /* in MDI client */
2426     { WM_IME_SETCONTEXT, sent|wparam|optional, 0 }, /* in MDI client */
2427     { WM_IME_SETCONTEXT, sent|wparam|optional, 1 },
2428     { EVENT_OBJECT_FOCUS, winevent_hook|wparam|lparam, OBJID_CLIENT, 0 },
2429     { WM_SETFOCUS, sent }, /* in MDI client */
2430
2431     { WM_DESTROY, sent },
2432
2433     { HCBT_SETFOCUS, hook }, /* set focus to MDI client */
2434     { WM_KILLFOCUS, sent },
2435     { WM_IME_SETCONTEXT, sent|wparam|optional, 0 },
2436     { WM_IME_SETCONTEXT, sent|wparam|optional, 1 }, /* in MDI client */
2437     { EVENT_OBJECT_FOCUS, winevent_hook|wparam|lparam, OBJID_CLIENT, 0 },
2438     { WM_SETFOCUS, sent }, /* in MDI client */
2439
2440     { HCBT_SETFOCUS, hook }, /* MDI client sets focus back to MDI child */
2441     { WM_KILLFOCUS, sent }, /* in MDI client */
2442     { WM_IME_SETCONTEXT, sent|wparam|optional, 0 }, /* in MDI client */
2443     { WM_IME_SETCONTEXT, sent|wparam|optional, 1 },
2444     { EVENT_OBJECT_FOCUS, winevent_hook|wparam|lparam, OBJID_CLIENT, 0 },
2445     { WM_SETFOCUS, sent }, /* in MDI client */
2446
2447     { WM_NCDESTROY, sent },
2448     { 0 }
2449 };
2450 /* CreateWindow for MDI child window, initially invisible */
2451 static const struct message WmCreateMDIchildInvisibleSeq[] = {
2452     { HCBT_CREATEWND, hook },
2453     { WM_NCCREATE, sent }, 
2454     { WM_NCCALCSIZE, sent|wparam, 0 },
2455     { WM_CREATE, sent },
2456     { EVENT_OBJECT_CREATE, winevent_hook|wparam|lparam, 0, 0 },
2457     { WM_SIZE, sent|wparam, SIZE_RESTORED },
2458     { WM_MOVE, sent },
2459     /* Win2k sends wparam set to
2460      * MAKEWPARAM(WM_CREATE, MDI_FIRST_CHILD_ID + nTotalCreated),
2461      * while Win9x doesn't bother to set child window id according to
2462      * CLIENTCREATESTRUCT.idFirstChild
2463      */
2464     { WM_PARENTNOTIFY, sent /*|wparam, WM_CREATE*/ }, /* in MDI client */
2465     { 0 }
2466 };
2467 /* DestroyWindow for MDI child window, initially invisible */
2468 static const struct message WmDestroyMDIchildInvisibleSeq[] = {
2469     { HCBT_DESTROYWND, hook },
2470     /* Win2k sends wparam set to
2471      * MAKEWPARAM(WM_DESTROY, MDI_FIRST_CHILD_ID + nTotalCreated),
2472      * while Win9x doesn't bother to set child window id according to
2473      * CLIENTCREATESTRUCT.idFirstChild
2474      */
2475     { 0x0090, sent|optional },
2476     { WM_PARENTNOTIFY, sent /*|wparam, WM_DESTROY*/ }, /* in MDI client */
2477     { EVENT_OBJECT_DESTROY, winevent_hook|wparam|lparam, 0, 0 },
2478     { WM_DESTROY, sent },
2479     { WM_NCDESTROY, sent },
2480     /* FIXME: Wine destroys an icon/title window while Windows doesn't */
2481     { WM_PARENTNOTIFY, sent|wparam|optional, WM_DESTROY }, /* MDI client */
2482     { 0 }
2483 };
2484 /* CreateWindow for the 1st MDI child window, initially visible and maximized */
2485 static const struct message WmCreateMDIchildVisibleMaxSeq1[] = {
2486     { HCBT_CREATEWND, hook },
2487     { WM_NCCREATE, sent }, 
2488     { WM_NCCALCSIZE, sent|wparam, 0 },
2489     { WM_CREATE, sent },
2490     { EVENT_OBJECT_CREATE, winevent_hook|wparam|lparam, 0, 0 },
2491     { WM_SIZE, sent|wparam, SIZE_RESTORED },
2492     { WM_MOVE, sent },
2493     { HCBT_MINMAX, hook|lparam, 0, SW_MAXIMIZE },
2494     { WM_GETMINMAXINFO, sent },
2495     { WM_WINDOWPOSCHANGING, sent|wparam, SWP_FRAMECHANGED|SWP_NOACTIVATE|SWP_STATECHANGED  },
2496     { WM_NCCALCSIZE, sent|wparam, 1 },
2497     { WM_WINDOWPOSCHANGED, sent|wparam, SWP_FRAMECHANGED|SWP_NOACTIVATE|SWP_NOMOVE|SWP_NOREDRAW|SWP_NOCLIENTMOVE|SWP_STATECHANGED },
2498     { WM_SIZE, sent|defwinproc|wparam, SIZE_MAXIMIZED },
2499      /* in MDI frame */
2500     { WM_WINDOWPOSCHANGING, sent|wparam, SWP_FRAMECHANGED|SWP_NOACTIVATE|SWP_NOSIZE|SWP_NOMOVE },
2501     { WM_NCCALCSIZE, sent|wparam, 1 },
2502     { WM_WINDOWPOSCHANGED, sent|wparam, SWP_FRAMECHANGED|SWP_NOACTIVATE|SWP_NOSIZE|SWP_NOMOVE|SWP_NOCLIENTSIZE|SWP_NOCLIENTMOVE },
2503     { EVENT_OBJECT_LOCATIONCHANGE, winevent_hook|wparam|lparam, 0, 0 }, /* MDI frame */
2504     { EVENT_OBJECT_LOCATIONCHANGE, winevent_hook|wparam|lparam, 0, 0 }, /* MDI child */
2505     /* Win2k sends wparam set to
2506      * MAKEWPARAM(WM_CREATE, MDI_FIRST_CHILD_ID + nTotalCreated),
2507      * while Win9x doesn't bother to set child window id according to
2508      * CLIENTCREATESTRUCT.idFirstChild
2509      */
2510     { WM_PARENTNOTIFY, sent /*|wparam, WM_CREATE*/ }, /* in MDI client */
2511     { WM_SHOWWINDOW, sent|wparam, 1 },
2512     { WM_WINDOWPOSCHANGING, sent|wparam, SWP_SHOWWINDOW|SWP_NOACTIVATE|SWP_NOSIZE|SWP_NOMOVE },
2513     { EVENT_OBJECT_SHOW, winevent_hook|wparam|lparam, 0, 0 },
2514     { WM_WINDOWPOSCHANGED, sent|wparam, SWP_SHOWWINDOW|SWP_NOACTIVATE|SWP_NOSIZE|SWP_NOMOVE|SWP_NOCLIENTSIZE|SWP_NOCLIENTMOVE },
2515     { WM_MDIREFRESHMENU, sent/*|wparam|lparam, 0, 0*/ },
2516     { WM_WINDOWPOSCHANGING, sent|wparam, SWP_SHOWWINDOW|SWP_NOSIZE|SWP_NOMOVE },
2517     { WM_CHILDACTIVATE, sent|wparam|lparam, 0, 0 },
2518     { WM_WINDOWPOSCHANGING, sent|wparam|defwinproc|optional, SWP_NOACTIVATE|SWP_NOSIZE|SWP_NOMOVE, 0, SWP_FRAMECHANGED },
2519
2520     /* Win9x: message sequence terminates here. */
2521
2522     { WM_NCACTIVATE, sent|wparam|defwinproc|optional, 1 },
2523     { HCBT_SETFOCUS, hook|optional }, /* in MDI client */
2524     { WM_IME_SETCONTEXT, sent|wparam|optional, 1 }, /* in MDI client */
2525     { WM_IME_NOTIFY, sent|wparam|optional, 2 }, /* in MDI client */
2526     { EVENT_OBJECT_FOCUS, winevent_hook|wparam|lparam, OBJID_CLIENT, 0 },
2527     { WM_SETFOCUS, sent|optional }, /* in MDI client */
2528     { HCBT_SETFOCUS, hook|optional },
2529     { WM_KILLFOCUS, sent|optional }, /* in MDI client */
2530     { WM_IME_SETCONTEXT, sent|wparam|optional, 0 }, /* in MDI client */
2531     { WM_IME_SETCONTEXT, sent|wparam|defwinproc|optional, 1 },
2532     { EVENT_OBJECT_FOCUS, winevent_hook|wparam|lparam, OBJID_CLIENT, 0 },
2533     { WM_SETFOCUS, sent|defwinproc|optional },
2534     { WM_MDIACTIVATE, sent|defwinproc|optional },
2535      /* in MDI frame */
2536     { WM_WINDOWPOSCHANGING, sent|wparam, SWP_FRAMECHANGED|SWP_NOACTIVATE|SWP_NOSIZE|SWP_NOMOVE },
2537     { WM_NCCALCSIZE, sent|wparam, 1 },
2538     { WM_WINDOWPOSCHANGED, sent|wparam, SWP_FRAMECHANGED|SWP_NOACTIVATE|SWP_NOSIZE|SWP_NOMOVE|SWP_NOCLIENTSIZE|SWP_NOCLIENTMOVE },
2539     { EVENT_OBJECT_LOCATIONCHANGE, winevent_hook|wparam|lparam, 0, 0 }, /* MDI frame */
2540     { 0 }
2541 };
2542 /* CreateWindow for the 2nd MDI child window, initially visible and maximized */
2543 static const struct message WmCreateMDIchildVisibleMaxSeq2[] = {
2544     /* restore the 1st MDI child */
2545     { WM_SETREDRAW, sent|wparam, 0 },
2546     { HCBT_MINMAX, hook|lparam, 0, SW_SHOWNORMAL },
2547     { WM_WINDOWPOSCHANGING, sent|wparam, SWP_FRAMECHANGED|SWP_STATECHANGED },
2548     { WM_NCCALCSIZE, sent|wparam, 1 },
2549     { WM_CHILDACTIVATE, sent|wparam|lparam, 0, 0 },
2550     { WM_WINDOWPOSCHANGED, sent|wparam, SWP_FRAMECHANGED|SWP_NOMOVE|SWP_NOREDRAW|SWP_NOCLIENTMOVE|SWP_STATECHANGED },
2551     { WM_SIZE, sent|defwinproc|wparam, SIZE_RESTORED },
2552      /* in MDI frame */
2553     { WM_WINDOWPOSCHANGING, sent|wparam, SWP_FRAMECHANGED|SWP_NOACTIVATE|SWP_NOSIZE|SWP_NOMOVE },
2554     { WM_NCCALCSIZE, sent|wparam, 1 },
2555     { WM_WINDOWPOSCHANGED, sent|wparam, SWP_FRAMECHANGED|SWP_NOACTIVATE|SWP_NOSIZE|SWP_NOMOVE|SWP_NOCLIENTSIZE|SWP_NOCLIENTMOVE },
2556     { EVENT_OBJECT_LOCATIONCHANGE, winevent_hook|wparam|lparam, 0, 0 }, /* MDI frame */
2557     { EVENT_OBJECT_LOCATIONCHANGE, winevent_hook|wparam|lparam, 0, 0 }, /* MDI child */
2558     { WM_SETREDRAW, sent|wparam, 1 }, /* in the 1st MDI child */
2559     /* create the 2nd MDI child */
2560     { HCBT_CREATEWND, hook },
2561     { WM_NCCREATE, sent }, 
2562     { WM_NCCALCSIZE, sent|wparam, 0 },
2563     { WM_CREATE, sent },
2564     { EVENT_OBJECT_CREATE, winevent_hook|wparam|lparam, 0, 0 },
2565     { WM_SIZE, sent|wparam, SIZE_RESTORED },
2566     { WM_MOVE, sent },
2567     { HCBT_MINMAX, hook|lparam, 0, SW_MAXIMIZE },
2568     { WM_GETMINMAXINFO, sent },
2569     { WM_WINDOWPOSCHANGING, sent|wparam, SWP_FRAMECHANGED|SWP_NOACTIVATE|SWP_STATECHANGED },
2570     { WM_NCCALCSIZE, sent|wparam, 1 },
2571     { EVENT_OBJECT_REORDER, winevent_hook|wparam|lparam, OBJID_CLIENT, 0 },
2572     { WM_WINDOWPOSCHANGED, sent|wparam, SWP_FRAMECHANGED|SWP_NOACTIVATE|SWP_NOMOVE|SWP_NOREDRAW|SWP_NOCLIENTMOVE|SWP_STATECHANGED },
2573     { WM_SIZE, sent|defwinproc|wparam, SIZE_MAXIMIZED },
2574      /* in MDI frame */
2575     { WM_WINDOWPOSCHANGING, sent|wparam, SWP_FRAMECHANGED|SWP_NOACTIVATE|SWP_NOSIZE|SWP_NOMOVE },
2576     { WM_NCCALCSIZE, sent|wparam, 1 },
2577     { WM_WINDOWPOSCHANGED, sent|wparam, SWP_FRAMECHANGED|SWP_NOACTIVATE|SWP_NOSIZE|SWP_NOMOVE|SWP_NOCLIENTSIZE|SWP_NOCLIENTMOVE },
2578     { EVENT_OBJECT_LOCATIONCHANGE, winevent_hook|wparam|lparam, 0, 0 }, /* MDI frame */
2579     { EVENT_OBJECT_LOCATIONCHANGE, winevent_hook|wparam|lparam, 0, 0 }, /* MDI child */
2580     /* Win2k sends wparam set to
2581      * MAKEWPARAM(WM_CREATE, MDI_FIRST_CHILD_ID + nTotalCreated),
2582      * while Win9x doesn't bother to set child window id according to
2583      * CLIENTCREATESTRUCT.idFirstChild
2584      */
2585     { WM_PARENTNOTIFY, sent /*|wparam, WM_CREATE*/ }, /* in MDI client */
2586     { WM_SHOWWINDOW, sent|wparam, 1 },
2587     { WM_WINDOWPOSCHANGING, sent|wparam, SWP_SHOWWINDOW|SWP_NOACTIVATE|SWP_NOSIZE|SWP_NOMOVE },
2588     { EVENT_OBJECT_SHOW, winevent_hook|wparam|lparam, 0, 0 },
2589     { WM_WINDOWPOSCHANGED, sent|wparam, SWP_SHOWWINDOW|SWP_NOACTIVATE|SWP_NOSIZE|SWP_NOMOVE|SWP_NOCLIENTSIZE|SWP_NOCLIENTMOVE },
2590     { WM_MDIREFRESHMENU, sent/*|wparam|lparam, 0, 0*/ },
2591     { WM_WINDOWPOSCHANGING, sent|wparam, SWP_SHOWWINDOW|SWP_NOSIZE|SWP_NOMOVE },
2592     { WM_CHILDACTIVATE, sent|wparam|lparam, 0, 0 },
2593
2594     { WM_NCACTIVATE, sent|wparam|defwinproc, 0 }, /* in the 1st MDI child */
2595     { WM_MDIACTIVATE, sent|defwinproc }, /* in the 1st MDI child */
2596
2597     { WM_WINDOWPOSCHANGING, sent|wparam|defwinproc, SWP_NOACTIVATE|SWP_NOSIZE|SWP_NOMOVE },
2598
2599     /* Win9x: message sequence terminates here. */
2600
2601     { WM_NCACTIVATE, sent|wparam|defwinproc, 1 },
2602     { HCBT_SETFOCUS, hook },
2603     { WM_KILLFOCUS, sent|defwinproc|optional }, /* in the 1st MDI child */
2604     { WM_IME_SETCONTEXT, sent|wparam|defwinproc|optional, 0 }, /* in the 1st MDI child */
2605     { WM_IME_SETCONTEXT, sent|wparam|optional, 1 }, /* in MDI client */
2606     { EVENT_OBJECT_FOCUS, winevent_hook|wparam|lparam, OBJID_CLIENT, 0 },
2607     { WM_SETFOCUS, sent }, /* in MDI client */
2608     { HCBT_SETFOCUS, hook },
2609     { WM_KILLFOCUS, sent }, /* in MDI client */
2610     { WM_IME_SETCONTEXT, sent|wparam|optional, 0 }, /* in MDI client */
2611     { WM_IME_SETCONTEXT, sent|wparam|defwinproc|optional, 1 },
2612     { EVENT_OBJECT_FOCUS, winevent_hook|wparam|lparam, OBJID_CLIENT, 0 },
2613     { WM_SETFOCUS, sent|defwinproc },
2614
2615     { WM_MDIACTIVATE, sent|defwinproc },
2616      /* in MDI frame */
2617     { WM_WINDOWPOSCHANGING, sent|wparam, SWP_FRAMECHANGED|SWP_NOACTIVATE|SWP_NOSIZE|SWP_NOMOVE },
2618     { WM_NCCALCSIZE, sent|wparam, 1 },
2619     { WM_WINDOWPOSCHANGED, sent|wparam, SWP_FRAMECHANGED|SWP_NOACTIVATE|SWP_NOSIZE|SWP_NOMOVE|SWP_NOCLIENTSIZE|SWP_NOCLIENTMOVE },
2620     { EVENT_OBJECT_LOCATIONCHANGE, winevent_hook|wparam|lparam, 0, 0 }, /* MDI frame */
2621     { 0 }
2622 };
2623 /* WM_MDICREATE MDI child window, initially visible and maximized */
2624 static const struct message WmCreateMDIchildVisibleMaxSeq3[] = {
2625     { WM_MDICREATE, sent },
2626     { HCBT_CREATEWND, hook },
2627     { WM_NCCREATE, sent }, 
2628     { WM_NCCALCSIZE, sent|wparam, 0 },
2629     { WM_CREATE, sent },
2630     { EVENT_OBJECT_CREATE, winevent_hook|wparam|lparam, 0, 0 },
2631     { WM_SIZE, sent|wparam, SIZE_RESTORED },
2632     { WM_MOVE, sent },
2633     { HCBT_MINMAX, hook|lparam, 0, SW_MAXIMIZE },
2634     { WM_GETMINMAXINFO, sent },
2635     { WM_WINDOWPOSCHANGING, sent|wparam, SWP_FRAMECHANGED|SWP_NOACTIVATE|SWP_STATECHANGED },
2636     { WM_NCCALCSIZE, sent|wparam, 1 },
2637     { WM_WINDOWPOSCHANGED, sent|wparam, SWP_FRAMECHANGED|SWP_NOACTIVATE|SWP_NOMOVE|SWP_NOREDRAW|SWP_NOCLIENTMOVE|SWP_STATECHANGED },
2638     { WM_SIZE, sent|defwinproc|wparam, SIZE_MAXIMIZED },
2639
2640      /* in MDI frame */
2641     { WM_WINDOWPOSCHANGING, sent|wparam, SWP_FRAMECHANGED|SWP_NOACTIVATE|SWP_NOSIZE|SWP_NOMOVE },
2642     { WM_NCCALCSIZE, sent|wparam, 1 },
2643     { WM_WINDOWPOSCHANGED, sent|wparam, SWP_FRAMECHANGED|SWP_NOACTIVATE|SWP_NOSIZE|SWP_NOMOVE|SWP_NOCLIENTSIZE|SWP_NOCLIENTMOVE },
2644     { EVENT_OBJECT_LOCATIONCHANGE, winevent_hook|wparam|lparam, 0, 0 }, /* MDI frame */
2645     { EVENT_OBJECT_LOCATIONCHANGE, winevent_hook|wparam|lparam, 0, 0 }, /* MDI child */
2646
2647     /* Win2k sends wparam set to
2648      * MAKEWPARAM(WM_CREATE, MDI_FIRST_CHILD_ID + nTotalCreated),
2649      * while Win9x doesn't bother to set child window id according to
2650      * CLIENTCREATESTRUCT.idFirstChild
2651      */
2652     { WM_PARENTNOTIFY, sent /*|wparam, WM_CREATE*/ }, /* in MDI client */
2653     { WM_SHOWWINDOW, sent|wparam, 1 },
2654     { WM_WINDOWPOSCHANGING, sent|wparam, SWP_SHOWWINDOW|SWP_NOACTIVATE|SWP_NOSIZE|SWP_NOMOVE },
2655
2656     { EVENT_OBJECT_SHOW, winevent_hook|wparam|lparam, 0, 0 },
2657
2658     { WM_WINDOWPOSCHANGED, sent|wparam, SWP_SHOWWINDOW|SWP_NOACTIVATE|SWP_NOSIZE|SWP_NOMOVE|SWP_NOCLIENTSIZE|SWP_NOCLIENTMOVE },
2659     { WM_MDIREFRESHMENU, sent/*|wparam|lparam, 0, 0*/ },
2660     { WM_WINDOWPOSCHANGING, sent|wparam, SWP_SHOWWINDOW|SWP_NOSIZE|SWP_NOMOVE },
2661
2662     { WM_CHILDACTIVATE, sent|wparam|lparam, 0, 0 },
2663     { WM_WINDOWPOSCHANGING, sent|wparam|defwinproc, SWP_NOACTIVATE|SWP_NOSIZE|SWP_NOMOVE },
2664
2665     /* Win9x: message sequence terminates here. */
2666
2667     { WM_NCACTIVATE, sent|wparam|defwinproc, 1 },
2668     { WM_SETFOCUS, sent|optional }, /* in MDI client */
2669     { HCBT_SETFOCUS, hook }, /* in MDI client */
2670     { WM_IME_SETCONTEXT, sent|wparam|optional, 1 }, /* in MDI client */
2671     { WM_IME_NOTIFY, sent|wparam|optional, 2 },
2672     { EVENT_OBJECT_FOCUS, winevent_hook|wparam|lparam|optional, OBJID_CLIENT, 0 },
2673     { WM_SETFOCUS, sent|optional }, /* in MDI client */
2674     { HCBT_SETFOCUS, hook|optional },
2675     { WM_KILLFOCUS, sent }, /* in MDI client */
2676     { WM_IME_SETCONTEXT, sent|wparam|optional, 0 }, /* in MDI client */
2677     { WM_IME_SETCONTEXT, sent|wparam|defwinproc|optional, 1 },
2678     { EVENT_OBJECT_FOCUS, winevent_hook|wparam|lparam, OBJID_CLIENT, 0 },
2679     { WM_SETFOCUS, sent|defwinproc },
2680
2681     { WM_MDIACTIVATE, sent|defwinproc },
2682
2683      /* in MDI child */
2684     { WM_WINDOWPOSCHANGING, sent|wparam, SWP_FRAMECHANGED|SWP_NOACTIVATE|SWP_NOSIZE|SWP_NOMOVE },
2685     { WM_NCCALCSIZE, sent|wparam, 1 },
2686     { WM_WINDOWPOSCHANGED, sent|wparam, SWP_FRAMECHANGED|SWP_NOACTIVATE|SWP_NOSIZE|SWP_NOMOVE|SWP_NOCLIENTSIZE|SWP_NOCLIENTMOVE },
2687     { EVENT_OBJECT_LOCATIONCHANGE, winevent_hook|wparam|lparam, 0, 0 },
2688
2689      /* in MDI frame */
2690     { WM_WINDOWPOSCHANGING, sent|wparam, SWP_FRAMECHANGED|SWP_NOACTIVATE|SWP_NOSIZE|SWP_NOMOVE },
2691     { WM_NCCALCSIZE, sent|wparam, 1 },
2692     { 0x0093, sent|defwinproc|optional },
2693     { 0x0093, sent|defwinproc|optional },
2694     { 0x0093, sent|defwinproc|optional },
2695     { WM_WINDOWPOSCHANGED, sent|wparam, SWP_FRAMECHANGED|SWP_NOACTIVATE|SWP_NOSIZE|SWP_NOMOVE },
2696     { WM_MOVE, sent|defwinproc },
2697     { WM_SIZE, sent|defwinproc|wparam, SIZE_RESTORED },
2698
2699      /* in MDI client */
2700     { WM_WINDOWPOSCHANGING, sent|wparam, SWP_NOACTIVATE },
2701     { WM_NCCALCSIZE, sent|wparam, 1 },
2702     { WM_WINDOWPOSCHANGED, sent|wparam, SWP_NOACTIVATE|SWP_NOMOVE|SWP_NOCLIENTMOVE },
2703     { WM_SIZE, sent|wparam, SIZE_RESTORED },
2704
2705      /* in MDI child */
2706     { WM_WINDOWPOSCHANGING, sent|wparam, SWP_NOACTIVATE },
2707     { WM_NCCALCSIZE, sent|wparam, 1 },
2708     { 0x0093, sent|optional },
2709     { WM_WINDOWPOSCHANGED, sent|wparam, SWP_NOACTIVATE|SWP_NOMOVE|SWP_NOCLIENTMOVE },
2710     { WM_SIZE, sent|defwinproc|wparam, SIZE_MAXIMIZED },
2711
2712     { 0x0093, sent|optional },
2713     { EVENT_OBJECT_LOCATIONCHANGE, winevent_hook|wparam|lparam, 0, 0 }, /* MDI child */
2714     { EVENT_OBJECT_LOCATIONCHANGE, winevent_hook|wparam|lparam, 0, 0 }, /* MDI client */
2715     { WM_NCCALCSIZE, sent|wparam|optional, 1 }, /* XP sends it to MDI frame */
2716     { 0x0093, sent|defwinproc|optional },
2717     { 0x0093, sent|defwinproc|optional },
2718     { 0x0093, sent|defwinproc|optional },
2719     { EVENT_OBJECT_LOCATIONCHANGE, winevent_hook|wparam|lparam, 0, 0 }, /* MDI frame */
2720     { EVENT_OBJECT_LOCATIONCHANGE, winevent_hook|wparam|lparam, 0, 0 }, /* XP sends a duplicate */
2721
2722     { 0 }
2723 };
2724 /* CreateWindow for the 1st MDI child window, initially invisible and maximized */
2725 static const struct message WmCreateMDIchildInvisibleMaxSeq4[] = {
2726     { HCBT_CREATEWND, hook },
2727     { WM_GETMINMAXINFO, sent },
2728     { WM_NCCREATE, sent }, 
2729     { WM_NCCALCSIZE, sent|wparam, 0 },
2730     { EVENT_OBJECT_REORDER, winevent_hook|wparam|lparam, 0, 0 },
2731     { WM_CREATE, sent },
2732     { EVENT_OBJECT_CREATE, winevent_hook|wparam|lparam, 0, 0 },
2733     { WM_SIZE, sent|wparam, SIZE_RESTORED },
2734     { WM_WINDOWPOSCHANGING, sent|wparam|optional, SWP_FRAMECHANGED|SWP_NOACTIVATE|SWP_NOSIZE|SWP_NOMOVE, 0, SWP_NOZORDER }, /* MDI frame */
2735     { WM_NCCALCSIZE, sent|wparam|optional, 1 }, /* MDI frame */
2736     { WM_WINDOWPOSCHANGED, sent|wparam|optional, SWP_FRAMECHANGED|SWP_NOACTIVATE|SWP_NOSIZE|SWP_NOMOVE|SWP_NOCLIENTSIZE|SWP_NOCLIENTMOVE, 0, SWP_NOZORDER }, /* MDI frame */
2737     { WM_MOVE, sent },
2738     { HCBT_MINMAX, hook|lparam, 0, SW_MAXIMIZE },
2739     { WM_GETMINMAXINFO, sent },
2740     { WM_WINDOWPOSCHANGING, sent|wparam, SWP_FRAMECHANGED|SWP_NOACTIVATE|SWP_STATECHANGED },
2741     { WM_GETMINMAXINFO, sent|defwinproc },
2742     { WM_NCCALCSIZE, sent|wparam, 1 },
2743     { WM_WINDOWPOSCHANGED, sent|wparam, SWP_FRAMECHANGED|SWP_NOACTIVATE|SWP_NOREDRAW|SWP_STATECHANGED },
2744     { WM_MOVE, sent|defwinproc },
2745     { WM_SIZE, sent|defwinproc|wparam, SIZE_MAXIMIZED },
2746      /* in MDI frame */
2747     { WM_WINDOWPOSCHANGING, sent|wparam, SWP_FRAMECHANGED|SWP_NOACTIVATE|SWP_NOSIZE|SWP_NOMOVE },
2748     { WM_NCCALCSIZE, sent|wparam, 1 },
2749     { WM_WINDOWPOSCHANGED, sent|wparam, SWP_FRAMECHANGED|SWP_NOACTIVATE|SWP_NOSIZE|SWP_NOMOVE|SWP_NOCLIENTSIZE|SWP_NOCLIENTMOVE },
2750     { EVENT_OBJECT_LOCATIONCHANGE, winevent_hook|wparam|lparam, 0, 0 }, /* MDI frame */
2751     { WM_NCCALCSIZE, sent|wparam|optional, 1 }, /* MDI child */
2752     { EVENT_OBJECT_LOCATIONCHANGE, winevent_hook|wparam|lparam, 0, 0 }, /* MDI child */
2753     { EVENT_OBJECT_LOCATIONCHANGE, winevent_hook|wparam|lparam, 0, 0 }, /* MDI child */
2754     /* Win2k sends wparam set to
2755      * MAKEWPARAM(WM_CREATE, MDI_FIRST_CHILD_ID + nTotalCreated),
2756      * while Win9x doesn't bother to set child window id according to
2757      * CLIENTCREATESTRUCT.idFirstChild
2758      */
2759     { WM_PARENTNOTIFY, sent /*|wparam, WM_CREATE*/ }, /* in MDI client */
2760     { 0 }
2761 };
2762 /* WM_SYSCOMMAND/SC_CLOSE for the 2nd MDI child window, initially visible and maximized */
2763 static const struct message WmDestroyMDIchildVisibleMaxSeq2[] = {
2764     { WM_SYSCOMMAND, sent|wparam, SC_CLOSE },
2765     { HCBT_SYSCOMMAND, hook },
2766     { WM_CLOSE, sent|defwinproc },
2767     { WM_MDIDESTROY, sent }, /* in MDI client */
2768
2769     /* bring the 1st MDI child to top */
2770     { WM_WINDOWPOSCHANGING, sent|wparam|defwinproc, SWP_NOSIZE|SWP_NOMOVE }, /* in the 1st MDI child */
2771     { WM_WINDOWPOSCHANGING, sent|wparam|defwinproc, SWP_NOACTIVATE|SWP_NOSIZE|SWP_NOMOVE }, /* in the 2nd MDI child */
2772
2773     { EVENT_OBJECT_REORDER, winevent_hook|wparam|lparam, OBJID_CLIENT, 0 },
2774
2775     { WM_CHILDACTIVATE, sent|defwinproc|wparam|lparam, 0, 0 }, /* in the 1st MDI child */
2776     { WM_NCACTIVATE, sent|wparam|defwinproc, 0 }, /* in the 1st MDI child */
2777     { WM_MDIACTIVATE, sent|defwinproc }, /* in the 1st MDI child */
2778
2779     /* maximize the 1st MDI child */
2780     { HCBT_MINMAX, hook|lparam, 0, SW_MAXIMIZE },
2781     { WM_GETMINMAXINFO, sent|defwinproc },
2782     { WM_WINDOWPOSCHANGING, sent|wparam|defwinproc, SWP_FRAMECHANGED|SWP_STATECHANGED },
2783     { WM_NCCALCSIZE, sent|defwinproc|wparam, 1 },
2784     { WM_CHILDACTIVATE, sent|defwinproc|wparam|lparam, 0, 0 },
2785     { WM_WINDOWPOSCHANGED, sent|wparam|defwinproc, SWP_FRAMECHANGED|SWP_NOMOVE|SWP_NOREDRAW|SWP_NOCLIENTMOVE|SWP_STATECHANGED },
2786     { WM_SIZE, sent|defwinproc|wparam, SIZE_MAXIMIZED },
2787
2788     /* restore the 2nd MDI child */
2789     { WM_SETREDRAW, sent|defwinproc|wparam, 0 },
2790     { HCBT_MINMAX, hook|lparam, 0, SW_NORMALNA },
2791     { WM_WINDOWPOSCHANGING, sent|wparam|defwinproc, SWP_NOACTIVATE|SWP_FRAMECHANGED|SWP_SHOWWINDOW|SWP_STATECHANGED },
2792     { WM_NCCALCSIZE, sent|defwinproc|wparam, 1 },
2793
2794     { EVENT_OBJECT_SHOW, winevent_hook|wparam|lparam, 0, 0 },
2795
2796     { WM_WINDOWPOSCHANGED, sent|wparam|defwinproc, SWP_NOACTIVATE|SWP_FRAMECHANGED|SWP_SHOWWINDOW|SWP_NOMOVE|SWP_NOREDRAW|SWP_NOCLIENTMOVE|SWP_STATECHANGED },
2797     { WM_SIZE, sent|defwinproc|wparam, SIZE_RESTORED },
2798
2799     { EVENT_OBJECT_LOCATIONCHANGE, winevent_hook|wparam|lparam, 0, 0 }, /* MDI frame */
2800
2801     { WM_SETREDRAW, sent|defwinproc|wparam, 1 },
2802      /* in MDI frame */
2803     { WM_WINDOWPOSCHANGING, sent|wparam, SWP_FRAMECHANGED|SWP_NOACTIVATE|SWP_NOSIZE|SWP_NOMOVE },
2804     { WM_NCCALCSIZE, sent|wparam, 1 },
2805     { WM_WINDOWPOSCHANGED, sent|wparam, SWP_FRAMECHANGED|SWP_NOACTIVATE|SWP_NOSIZE|SWP_NOMOVE|SWP_NOCLIENTSIZE|SWP_NOCLIENTMOVE },
2806     { EVENT_OBJECT_LOCATIONCHANGE, winevent_hook|wparam|lparam, 0, 0 }, /* MDI frame */
2807     { EVENT_OBJECT_LOCATIONCHANGE, winevent_hook|wparam|lparam, 0, 0 }, /* MDI child */
2808
2809     /* bring the 1st MDI child to top */
2810     { WM_WINDOWPOSCHANGING, sent|wparam|defwinproc, SWP_NOACTIVATE|SWP_NOSIZE|SWP_NOMOVE },
2811     { WM_NCACTIVATE, sent|wparam|defwinproc, 1 },
2812     { HCBT_SETFOCUS, hook },
2813     { WM_KILLFOCUS, sent|defwinproc },
2814     { WM_IME_SETCONTEXT, sent|wparam|defwinproc|optional, 0 },
2815     { WM_IME_SETCONTEXT, sent|wparam|optional, 1 }, /* in MDI client */
2816     { EVENT_OBJECT_FOCUS, winevent_hook|wparam|lparam, OBJID_CLIENT, 0 },
2817     { WM_SETFOCUS, sent }, /* in MDI client */
2818     { HCBT_SETFOCUS, hook },
2819     { WM_KILLFOCUS, sent }, /* in MDI client */
2820     { WM_IME_SETCONTEXT, sent|wparam|optional, 0 }, /* in MDI client */
2821     { WM_IME_SETCONTEXT, sent|wparam|defwinproc|optional, 1 },
2822     { EVENT_OBJECT_FOCUS, winevent_hook|wparam|lparam, OBJID_CLIENT, 0 },
2823     { WM_SETFOCUS, sent|defwinproc },
2824     { WM_MDIACTIVATE, sent|defwinproc },
2825     { WM_WINDOWPOSCHANGED, sent|wparam|defwinproc, SWP_NOSIZE|SWP_NOMOVE|SWP_NOREDRAW|SWP_NOCLIENTSIZE|SWP_NOCLIENTMOVE },
2826
2827     /* apparently ShowWindow(SW_SHOW) on an MDI client */
2828     { WM_SHOWWINDOW, sent|wparam, 1 },
2829     { WM_WINDOWPOSCHANGING, sent|wparam, SWP_SHOWWINDOW|SWP_NOACTIVATE|SWP_NOSIZE|SWP_NOMOVE },
2830     { EVENT_OBJECT_SHOW, winevent_hook|wparam|lparam, 0, 0 },
2831     { WM_WINDOWPOSCHANGED, sent|wparam, SWP_SHOWWINDOW|SWP_NOACTIVATE|SWP_NOSIZE|SWP_NOMOVE|SWP_NOCLIENTSIZE|SWP_NOCLIENTMOVE },
2832     { WM_MDIREFRESHMENU, sent },
2833
2834     { HCBT_DESTROYWND, hook },
2835     /* Win2k sends wparam set to
2836      * MAKEWPARAM(WM_DESTROY, MDI_FIRST_CHILD_ID + nTotalCreated),
2837      * while Win9x doesn't bother to set child window id according to
2838      * CLIENTCREATESTRUCT.idFirstChild
2839      */
2840     { 0x0090, sent|defwinproc|optional },
2841     { WM_PARENTNOTIFY, sent /*|wparam, WM_DESTROY*/ }, /* in MDI client */
2842     { WM_SHOWWINDOW, sent|defwinproc|wparam, 0 },
2843     { WM_WINDOWPOSCHANGING, sent|wparam|defwinproc, SWP_HIDEWINDOW|SWP_NOACTIVATE|SWP_NOSIZE|SWP_NOMOVE },
2844     { EVENT_OBJECT_HIDE, winevent_hook|wparam|lparam, 0, 0 },
2845     { WM_ERASEBKGND, sent|parent|optional },
2846     { WM_WINDOWPOSCHANGED, sent|wparam|defwinproc, SWP_HIDEWINDOW|SWP_NOACTIVATE|SWP_NOSIZE|SWP_NOMOVE|SWP_NOCLIENTSIZE|SWP_NOCLIENTMOVE },
2847
2848     { EVENT_OBJECT_DESTROY, winevent_hook|wparam|lparam, 0, 0 },
2849     { WM_DESTROY, sent|defwinproc },
2850     { WM_NCDESTROY, sent|defwinproc },
2851     { 0 }
2852 };
2853 /* WM_MDIDESTROY for the single MDI child window, initially visible and maximized */
2854 static const struct message WmDestroyMDIchildVisibleMaxSeq1[] = {
2855     { WM_MDIDESTROY, sent }, /* in MDI client */
2856     { WM_SHOWWINDOW, sent|wparam, 0 },
2857     { WM_WINDOWPOSCHANGING, sent|wparam, SWP_HIDEWINDOW|SWP_NOACTIVATE|SWP_NOSIZE|SWP_NOMOVE },
2858     { EVENT_OBJECT_HIDE, winevent_hook|wparam|lparam, 0, 0 },
2859     { WM_ERASEBKGND, sent|parent|optional },
2860     { WM_WINDOWPOSCHANGED, sent|wparam, SWP_HIDEWINDOW|SWP_NOACTIVATE|SWP_NOSIZE|SWP_NOMOVE|SWP_NOCLIENTSIZE|SWP_NOCLIENTMOVE },
2861
2862     { HCBT_SETFOCUS, hook },
2863     { WM_KILLFOCUS, sent },
2864     { WM_IME_SETCONTEXT, sent|wparam|optional, 0 },
2865     { WM_IME_SETCONTEXT, sent|wparam|optional, 1 }, /* in MDI client */
2866     { EVENT_OBJECT_FOCUS, winevent_hook|wparam|lparam, OBJID_CLIENT, 0 },
2867     { WM_SETFOCUS, sent }, /* in MDI client */
2868     { HCBT_SETFOCUS, hook },
2869     { WM_KILLFOCUS, sent }, /* in MDI client */
2870     { WM_IME_SETCONTEXT, sent|wparam|optional, 0 }, /* in MDI client */
2871     { WM_IME_SETCONTEXT, sent|wparam|optional, 1 },
2872     { EVENT_OBJECT_FOCUS, winevent_hook|wparam|lparam, OBJID_CLIENT, 0 },
2873     { WM_SETFOCUS, sent },
2874
2875      /* in MDI child */
2876     { WM_WINDOWPOSCHANGING, sent|wparam, SWP_FRAMECHANGED|SWP_NOACTIVATE|SWP_NOSIZE|SWP_NOMOVE },
2877     { WM_NCCALCSIZE, sent|wparam, 1 },
2878     { WM_WINDOWPOSCHANGED, sent|wparam, SWP_FRAMECHANGED|SWP_NOACTIVATE|SWP_NOSIZE|SWP_NOMOVE|SWP_NOREDRAW|SWP_NOCLIENTSIZE|SWP_NOCLIENTMOVE },
2879     { EVENT_OBJECT_LOCATIONCHANGE, winevent_hook|wparam|lparam, 0, 0 }, /* MDI child */
2880
2881      /* in MDI frame */
2882     { WM_WINDOWPOSCHANGING, sent|wparam, SWP_FRAMECHANGED|SWP_NOACTIVATE|SWP_NOSIZE|SWP_NOMOVE },
2883     { WM_NCCALCSIZE, sent|wparam, 1 },
2884     { 0x0093, sent|defwinproc|optional },
2885     { 0x0093, sent|defwinproc|optional },
2886     { WM_WINDOWPOSCHANGED, sent|wparam, SWP_FRAMECHANGED|SWP_NOACTIVATE|SWP_NOSIZE|SWP_NOMOVE },
2887     { WM_MOVE, sent|defwinproc },
2888     { WM_SIZE, sent|defwinproc|wparam, SIZE_RESTORED },
2889
2890      /* in MDI client */
2891     { WM_WINDOWPOSCHANGING, sent|wparam, SWP_NOACTIVATE },
2892     { WM_NCCALCSIZE, sent|wparam, 1 },
2893     { WM_WINDOWPOSCHANGED, sent|wparam, SWP_NOACTIVATE|SWP_NOMOVE|SWP_NOCLIENTMOVE },
2894     { WM_SIZE, sent|wparam, SIZE_RESTORED },
2895
2896      /* in MDI child */
2897     { WM_WINDOWPOSCHANGING, sent|wparam, SWP_NOACTIVATE },
2898     { WM_NCCALCSIZE, sent|wparam, 1 },
2899     { WM_WINDOWPOSCHANGED, sent|wparam, SWP_NOACTIVATE|SWP_NOMOVE|SWP_NOREDRAW|SWP_NOCLIENTMOVE },
2900     { WM_SIZE, sent|defwinproc|wparam, SIZE_MAXIMIZED },
2901
2902      /* in MDI child */
2903     { WM_WINDOWPOSCHANGING, sent|wparam|defwinproc, SWP_FRAMECHANGED|SWP_NOACTIVATE|SWP_NOSIZE|SWP_NOMOVE },
2904     { WM_NCCALCSIZE, sent|wparam|defwinproc, 1 },
2905     { WM_WINDOWPOSCHANGED, sent|wparam|defwinproc, SWP_FRAMECHANGED|SWP_NOACTIVATE|SWP_NOSIZE|SWP_NOMOVE|SWP_NOREDRAW|SWP_NOCLIENTSIZE|SWP_NOCLIENTMOVE },
2906     { EVENT_OBJECT_LOCATIONCHANGE, winevent_hook|wparam|lparam, 0, 0 }, /* MDI child */
2907
2908      /* in MDI frame */
2909     { WM_WINDOWPOSCHANGING, sent|wparam|defwinproc, SWP_FRAMECHANGED|SWP_NOACTIVATE|SWP_NOSIZE|SWP_NOMOVE },
2910     { WM_NCCALCSIZE, sent|wparam|defwinproc, 1 },
2911     { 0x0093, sent|defwinproc|optional },
2912     { 0x0093, sent|defwinproc|optional },
2913     { 0x0093, sent|defwinproc|optional },
2914     { WM_WINDOWPOSCHANGED, sent|wparam|defwinproc, SWP_FRAMECHANGED|SWP_NOACTIVATE|SWP_NOSIZE|SWP_NOMOVE },
2915     { WM_MOVE, sent|defwinproc },
2916     { WM_SIZE, sent|defwinproc|wparam, SIZE_RESTORED },
2917
2918      /* in MDI client */
2919     { WM_WINDOWPOSCHANGING, sent|wparam, SWP_NOACTIVATE },
2920     { WM_NCCALCSIZE, sent|wparam, 1 },
2921     { WM_WINDOWPOSCHANGED, sent|wparam, SWP_NOACTIVATE|SWP_NOMOVE|SWP_NOCLIENTMOVE },
2922     { WM_SIZE, sent|wparam, SIZE_RESTORED },
2923
2924      /* in MDI child */
2925     { WM_WINDOWPOSCHANGING, sent|wparam|defwinproc, SWP_NOACTIVATE },
2926     { WM_NCCALCSIZE, sent|wparam|defwinproc, 1 },
2927     { WM_WINDOWPOSCHANGED, sent|wparam|defwinproc, SWP_NOACTIVATE|SWP_NOMOVE|SWP_NOREDRAW|SWP_NOCLIENTMOVE },
2928     { WM_SIZE, sent|defwinproc|wparam, SIZE_MAXIMIZED },
2929     { EVENT_OBJECT_LOCATIONCHANGE, winevent_hook|wparam|lparam, 0, 0 }, /* MDI child */
2930     { EVENT_OBJECT_LOCATIONCHANGE, winevent_hook|wparam|lparam, 0, 0 }, /* MDI client */
2931
2932     { 0x0093, sent|defwinproc|optional },
2933     { WM_NCCALCSIZE, sent|wparam|defwinproc|optional, 1 }, /* XP sends it to MDI frame */
2934     { 0x0093, sent|defwinproc|optional },
2935     { 0x0093, sent|defwinproc|optional },
2936     { 0x0093, sent|defwinproc|optional },
2937     { 0x0093, sent|optional },
2938
2939     { EVENT_OBJECT_LOCATIONCHANGE, winevent_hook|wparam|lparam, 0, 0 }, /* MDI frame */
2940     { EVENT_OBJECT_LOCATIONCHANGE, winevent_hook|wparam|lparam, 0, 0 }, /* MDI child */
2941     { EVENT_OBJECT_LOCATIONCHANGE, winevent_hook|wparam|lparam, 0, 0 }, /* MDI client */
2942     { EVENT_OBJECT_LOCATIONCHANGE, winevent_hook|wparam|lparam, 0, 0 }, /* MDI frame */
2943     { EVENT_OBJECT_LOCATIONCHANGE, winevent_hook|wparam|lparam, 0, 0 }, /* XP sends a duplicate */
2944
2945      /* in MDI frame */
2946     { WM_WINDOWPOSCHANGING, sent|wparam|optional, SWP_FRAMECHANGED|SWP_NOACTIVATE|SWP_NOSIZE|SWP_NOMOVE },
2947     { WM_NCCALCSIZE, sent|wparam|optional, 1 },
2948     { 0x0093, sent|defwinproc|optional },
2949     { 0x0093, sent|defwinproc|optional },
2950     { 0x0093, sent|defwinproc|optional },
2951     { WM_WINDOWPOSCHANGED, sent|wparam|optional, SWP_FRAMECHANGED|SWP_NOACTIVATE|SWP_NOSIZE|SWP_NOMOVE|SWP_NOCLIENTSIZE|SWP_NOCLIENTMOVE },
2952     { EVENT_OBJECT_LOCATIONCHANGE, winevent_hook|wparam|lparam, 0, 0 }, /* MDI frame */
2953     { 0x0093, sent|optional },
2954
2955     { WM_NCACTIVATE, sent|wparam, 0 },
2956     { WM_MDIACTIVATE, sent },
2957
2958     { HCBT_MINMAX, hook|lparam, 0, SW_SHOWNORMAL },
2959     { WM_WINDOWPOSCHANGING, sent|wparam, SWP_FRAMECHANGED|SWP_SHOWWINDOW|SWP_STATECHANGED },
2960     { WM_NCCALCSIZE, sent|wparam, 1 },
2961
2962     { EVENT_OBJECT_SHOW, winevent_hook|wparam|lparam, 0, 0 },
2963
2964     { WM_CHILDACTIVATE, sent|wparam|lparam, 0, 0 },
2965     { WM_WINDOWPOSCHANGED, sent|wparam, SWP_FRAMECHANGED|SWP_SHOWWINDOW|SWP_NOMOVE|SWP_NOCLIENTMOVE|SWP_STATECHANGED },
2966     { WM_SIZE, sent|defwinproc|wparam, SIZE_RESTORED },
2967
2968      /* in MDI child */
2969     { WM_WINDOWPOSCHANGING, sent|wparam|defwinproc, SWP_FRAMECHANGED|SWP_NOACTIVATE|SWP_NOSIZE|SWP_NOMOVE },
2970     { WM_NCCALCSIZE, sent|wparam|defwinproc, 1 },
2971     { WM_WINDOWPOSCHANGED, sent|wparam|defwinproc, SWP_FRAMECHANGED|SWP_NOACTIVATE|SWP_NOSIZE|SWP_NOMOVE|SWP_NOCLIENTSIZE|SWP_NOCLIENTMOVE },
2972     { EVENT_OBJECT_LOCATIONCHANGE, winevent_hook|wparam|lparam, 0, 0 }, /* MDI child */
2973
2974      /* in MDI frame */
2975     { WM_WINDOWPOSCHANGING, sent|wparam, SWP_FRAMECHANGED|SWP_NOACTIVATE|SWP_NOSIZE|SWP_NOMOVE },
2976     { WM_NCCALCSIZE, sent|wparam, 1 },
2977     { WM_WINDOWPOSCHANGED, sent|wparam, SWP_FRAMECHANGED|SWP_NOACTIVATE|SWP_NOSIZE|SWP_NOMOVE },
2978     { WM_MOVE, sent|defwinproc },
2979     { WM_SIZE, sent|defwinproc|wparam, SIZE_RESTORED },
2980
2981      /* in MDI client */
2982     { WM_WINDOWPOSCHANGING, sent|wparam, SWP_NOACTIVATE },
2983     { WM_NCCALCSIZE, sent|wparam, 1 },
2984     { WM_WINDOWPOSCHANGED, sent|wparam, SWP_NOACTIVATE|SWP_NOMOVE|SWP_NOCLIENTMOVE },
2985     { WM_SIZE, sent|wparam, SIZE_RESTORED },
2986     { EVENT_OBJECT_LOCATIONCHANGE, winevent_hook|wparam|lparam, 0, 0 }, /* MDI child */
2987     { WM_NCCALCSIZE, sent|wparam|optional, 1 }, /* XP */
2988     { EVENT_OBJECT_LOCATIONCHANGE, winevent_hook|wparam|lparam, 0, 0 }, /* MDI client */
2989     { EVENT_OBJECT_LOCATIONCHANGE, winevent_hook|wparam|lparam, 0, 0 }, /* MDI frame */
2990     { EVENT_OBJECT_LOCATIONCHANGE, winevent_hook|wparam|lparam, 0, 0 }, /* XP sends a duplicate */
2991
2992     { HCBT_SETFOCUS, hook },
2993     { WM_KILLFOCUS, sent },
2994     { WM_IME_SETCONTEXT, sent|wparam|optional, 0 },
2995     { WM_IME_SETCONTEXT, sent|wparam|optional, 1 }, /* in MDI client */
2996     { EVENT_OBJECT_FOCUS, winevent_hook|wparam|lparam, OBJID_CLIENT, 0 },
2997     { WM_SETFOCUS, sent }, /* in MDI client */
2998
2999     { WM_MDIREFRESHMENU, sent }, /* in MDI client */
3000
3001     { HCBT_DESTROYWND, hook },
3002     /* Win2k sends wparam set to
3003      * MAKEWPARAM(WM_DESTROY, MDI_FIRST_CHILD_ID + nTotalCreated),
3004      * while Win9x doesn't bother to set child window id according to
3005      * CLIENTCREATESTRUCT.idFirstChild
3006      */
3007     { 0x0090, sent|optional },
3008     { WM_PARENTNOTIFY, sent /*|wparam, WM_DESTROY*/ }, /* in MDI client */
3009
3010     { WM_SHOWWINDOW, sent|wparam, 0 },
3011     { WM_WINDOWPOSCHANGING, sent|wparam|optional, SWP_HIDEWINDOW|SWP_NOACTIVATE|SWP_NOSIZE|SWP_NOMOVE },
3012     { EVENT_OBJECT_HIDE, winevent_hook|wparam|lparam, 0, 0 },
3013     { WM_ERASEBKGND, sent|parent|optional },
3014     { WM_WINDOWPOSCHANGED, sent|wparam|optional, SWP_HIDEWINDOW|SWP_NOACTIVATE|SWP_NOSIZE|SWP_NOMOVE|SWP_NOCLIENTSIZE|SWP_NOCLIENTMOVE },
3015
3016     { EVENT_OBJECT_DESTROY, winevent_hook|wparam|lparam, 0, 0 },
3017     { WM_DESTROY, sent },
3018     { WM_NCDESTROY, sent },
3019     { 0 }
3020 };
3021 /* ShowWindow(SW_MAXIMIZE) for a not visible MDI child window */
3022 static const struct message WmMaximizeMDIchildInvisibleSeq[] = {
3023     { HCBT_MINMAX, hook|lparam, 0, SW_MAXIMIZE },
3024     { WM_GETMINMAXINFO, sent },
3025     { WM_WINDOWPOSCHANGING, sent|wparam, SWP_SHOWWINDOW|SWP_FRAMECHANGED|SWP_STATECHANGED },
3026     { WM_NCCALCSIZE, sent|wparam, 1 },
3027     { EVENT_OBJECT_SHOW, winevent_hook|wparam|lparam, 0, 0 },
3028     { WM_CHILDACTIVATE, sent|wparam|lparam, 0, 0 },
3029
3030     { WM_WINDOWPOSCHANGING, sent|wparam|optional|defwinproc, SWP_NOACTIVATE|SWP_NOSIZE|SWP_NOMOVE },
3031     { WM_NCACTIVATE, sent|wparam|optional|defwinproc, 1 },
3032     { HCBT_SETFOCUS, hook|optional },
3033     { WM_IME_SETCONTEXT, sent|wparam|optional, 1 }, /* in MDI client */
3034     { EVENT_OBJECT_FOCUS, winevent_hook|wparam|lparam, OBJID_CLIENT, 0 },
3035     { WM_SETFOCUS, sent|optional }, /* in MDI client */
3036     { HCBT_SETFOCUS, hook|optional },
3037     { WM_KILLFOCUS, sent|optional }, /* in MDI client */
3038     { WM_IME_SETCONTEXT, sent|wparam|optional, 0 }, /* in MDI client */
3039     { WM_IME_SETCONTEXT, sent|wparam|defwinproc|optional, 1 },
3040     { EVENT_OBJECT_FOCUS, winevent_hook|wparam|lparam, OBJID_CLIENT, 0 },
3041     { WM_SETFOCUS, sent|optional|defwinproc },
3042     { WM_MDIACTIVATE, sent|optional|defwinproc },
3043     { WM_WINDOWPOSCHANGED, sent|wparam, SWP_SHOWWINDOW|SWP_FRAMECHANGED|SWP_NOMOVE|SWP_NOCLIENTMOVE|SWP_STATECHANGED },
3044     { WM_SIZE, sent|defwinproc|wparam, SIZE_MAXIMIZED },
3045      /* in MDI frame */
3046     { WM_WINDOWPOSCHANGING, sent|wparam, SWP_FRAMECHANGED|SWP_NOACTIVATE|SWP_NOSIZE|SWP_NOMOVE },
3047     { WM_NCCALCSIZE, sent|wparam, 1 },
3048     { WM_WINDOWPOSCHANGED, sent|wparam, SWP_FRAMECHANGED|SWP_NOACTIVATE|SWP_NOSIZE|SWP_NOMOVE|SWP_NOCLIENTSIZE|SWP_NOCLIENTMOVE },
3049     { EVENT_OBJECT_LOCATIONCHANGE, winevent_hook|wparam|lparam, 0, 0 }, /* MDI frame */
3050     { EVENT_OBJECT_LOCATIONCHANGE, winevent_hook|wparam|lparam, 0, 0 }, /* MDI child */
3051     { 0 }
3052 };
3053 /* ShowWindow(SW_MAXIMIZE) for a not visible maximized MDI child window */
3054 static const struct message WmMaximizeMDIchildInvisibleSeq2[] = {
3055     { HCBT_MINMAX, hook|lparam, 0, SW_MAXIMIZE },
3056     { WM_GETMINMAXINFO, sent },
3057     { WM_WINDOWPOSCHANGING, sent|wparam, SWP_SHOWWINDOW|SWP_FRAMECHANGED },
3058     { WM_GETMINMAXINFO, sent|defwinproc },
3059     { WM_NCCALCSIZE, sent|wparam, 1 },
3060     { EVENT_OBJECT_SHOW, winevent_hook|wparam|lparam, 0, 0 },
3061     { WM_CHILDACTIVATE, sent|wparam|lparam, 0, 0 },
3062
3063     { WM_WINDOWPOSCHANGING, sent|wparam|defwinproc|optional, SWP_NOACTIVATE|SWP_NOSIZE|SWP_NOMOVE },
3064     { WM_NCACTIVATE, sent|wparam|defwinproc|optional, 1 },
3065     { HCBT_SETFOCUS, hook|optional },
3066     { WM_IME_SETCONTEXT, sent|wparam|optional, 1 }, /* in MDI client */
3067     { EVENT_OBJECT_FOCUS, winevent_hook|wparam|lparam, OBJID_CLIENT, 0 },
3068     { WM_SETFOCUS, sent|optional }, /* in MDI client */
3069     { HCBT_SETFOCUS, hook|optional },
3070     { WM_KILLFOCUS, sent|optional }, /* in MDI client */
3071     { WM_IME_SETCONTEXT, sent|wparam|optional, 0 }, /* in MDI client */
3072     { WM_IME_SETCONTEXT, sent|wparam|defwinproc|optional, 1 },
3073     { EVENT_OBJECT_FOCUS, winevent_hook|wparam|lparam, OBJID_CLIENT, 0 },
3074     { WM_SETFOCUS, sent|defwinproc|optional },
3075     { WM_MDIACTIVATE, sent|defwinproc|optional },
3076     { WM_WINDOWPOSCHANGED, sent|wparam, SWP_SHOWWINDOW|SWP_FRAMECHANGED|SWP_NOSIZE|SWP_NOMOVE|SWP_NOCLIENTSIZE|SWP_NOCLIENTMOVE },
3077     { EVENT_OBJECT_LOCATIONCHANGE, winevent_hook|wparam|lparam, 0, 0 }, /* MDI child */
3078     { 0 }
3079 };
3080 /* WM_MDIMAXIMIZE for an MDI child window with invisible parent */
3081 static const struct message WmMaximizeMDIchildInvisibleParentSeq[] = {
3082     { WM_MDIMAXIMIZE, sent }, /* in MDI client */
3083     { HCBT_MINMAX, hook|lparam, 0, SW_MAXIMIZE },
3084     { WM_GETMINMAXINFO, sent },
3085     { WM_WINDOWPOSCHANGING, sent|wparam, SWP_FRAMECHANGED|SWP_STATECHANGED },
3086     { WM_GETMINMAXINFO, sent|defwinproc },
3087     { WM_NCCALCSIZE, sent|wparam, 1 },
3088     { EVENT_OBJECT_SHOW, winevent_hook|wparam|lparam|optional, 0, 0 }, /* XP doesn't send it */
3089     { WM_CHILDACTIVATE, sent|wparam|lparam, 0, 0 },
3090     { WM_WINDOWPOSCHANGED, sent|wparam, SWP_FRAMECHANGED|SWP_NOREDRAW|SWP_STATECHANGED },
3091     { WM_MOVE, sent|defwinproc },
3092     { WM_SIZE, sent|defwinproc|wparam, SIZE_MAXIMIZED },
3093
3094     { WM_WINDOWPOSCHANGING, sent|wparam|defwinproc, SWP_FRAMECHANGED|SWP_NOACTIVATE|SWP_NOSIZE|SWP_NOMOVE },
3095     { WM_NCCALCSIZE, sent|wparam|defwinproc, 1 },
3096     { WM_WINDOWPOSCHANGED, sent|wparam|defwinproc, SWP_FRAMECHANGED|SWP_NOACTIVATE|SWP_NOSIZE|SWP_NOMOVE|SWP_NOREDRAW|SWP_NOCLIENTSIZE|SWP_NOCLIENTMOVE },
3097     { WM_NCCALCSIZE, sent|wparam|defwinproc|optional, 1 },
3098     { EVENT_OBJECT_LOCATIONCHANGE, winevent_hook|wparam|lparam|optional, 0, 0 }, /* MDI child XP */
3099     { EVENT_OBJECT_LOCATIONCHANGE, winevent_hook|wparam|lparam|optional, 0, 0 }, /* MDI client XP */
3100      /* in MDI frame */
3101     { WM_WINDOWPOSCHANGING, sent|wparam, SWP_FRAMECHANGED|SWP_NOACTIVATE|SWP_NOSIZE|SWP_NOMOVE },
3102     { WM_NCCALCSIZE, sent|wparam, 1 },
3103     { 0x0093, sent|defwinproc|optional },
3104     { 0x0094, sent|defwinproc|optional },
3105     { 0x0094, sent|defwinproc|optional },
3106     { 0x0094, sent|defwinproc|optional },
3107     { 0x0094, sent|defwinproc|optional },
3108     { 0x0093, sent|defwinproc|optional },
3109     { 0x0093, sent|defwinproc|optional },
3110     { 0x0091, sent|defwinproc|optional },
3111     { 0x0092, sent|defwinproc|optional },
3112     { 0x0092, sent|defwinproc|optional },
3113     { 0x0092, sent|defwinproc|optional },
3114     { 0x0092, sent|defwinproc|optional },
3115     { WM_WINDOWPOSCHANGED, sent|wparam, SWP_FRAMECHANGED|SWP_NOACTIVATE|SWP_NOSIZE|SWP_NOMOVE },
3116     { WM_MOVE, sent|defwinproc },
3117     { WM_SIZE, sent|defwinproc|wparam, SIZE_RESTORED },
3118     { EVENT_OBJECT_LOCATIONCHANGE, winevent_hook|wparam|lparam|optional, 0, 0 }, /* MDI frame win2000 */
3119      /* in MDI client */
3120     { WM_WINDOWPOSCHANGING, sent|wparam, SWP_NOACTIVATE },
3121     { WM_NCCALCSIZE, sent|wparam, 1 },
3122     { WM_WINDOWPOSCHANGED, sent|wparam, SWP_NOACTIVATE|SWP_NOMOVE|SWP_NOREDRAW|SWP_NOCLIENTMOVE },
3123     { WM_SIZE, sent|wparam, SIZE_RESTORED },
3124      /* in MDI child */
3125     { WM_WINDOWPOSCHANGING, sent|wparam|defwinproc, SWP_NOACTIVATE },
3126     { WM_GETMINMAXINFO, sent|defwinproc },
3127     { WM_NCCALCSIZE, sent|wparam|defwinproc, 1 },
3128     { WM_WINDOWPOSCHANGED, sent|wparam|defwinproc, SWP_NOACTIVATE|SWP_NOMOVE|SWP_NOREDRAW|SWP_NOCLIENTMOVE },
3129     { WM_SIZE, sent|defwinproc|wparam, SIZE_MAXIMIZED },
3130     { EVENT_OBJECT_LOCATIONCHANGE, winevent_hook|wparam|lparam|optional, 0, 0 }, /* MDI child win2000 */
3131     { WM_NCCALCSIZE, sent|wparam|defwinproc|optional, 1 },
3132     { EVENT_OBJECT_LOCATIONCHANGE, winevent_hook|wparam|lparam|optional, 0, 0 }, /* MDI child XP */
3133     { EVENT_OBJECT_LOCATIONCHANGE, winevent_hook|wparam|lparam|optional, 0, 0 }, /* MDI child XP */
3134     { EVENT_OBJECT_LOCATIONCHANGE, winevent_hook|wparam|lparam|optional, 0, 0 }, /* MDI client XP */
3135      /* in MDI frame */
3136     { 0x0093, sent|optional },
3137     { WM_NCCALCSIZE, sent|wparam|optional, 1 },
3138     { 0x0093, sent|defwinproc|optional },
3139     { 0x0093, sent|defwinproc|optional },
3140     { 0x0093, sent|defwinproc|optional },
3141     { 0x0091, sent|defwinproc|optional },
3142     { 0x0092, sent|defwinproc|optional },
3143     { 0x0092, sent|defwinproc|optional },
3144     { 0x0092, sent|defwinproc|optional },
3145     { 0x0092, sent|defwinproc|optional },
3146     { EVENT_OBJECT_LOCATIONCHANGE, winevent_hook|wparam|lparam|optional, 0, 0 }, /* MDI frame XP */
3147     { EVENT_OBJECT_LOCATIONCHANGE, winevent_hook|wparam|lparam|optional, 0, 0 }, /* MDI frame XP */
3148     { EVENT_OBJECT_LOCATIONCHANGE, winevent_hook|wparam|lparam|optional, 0, 0 }, /* MDI child XP */
3149     { 0 }
3150 };
3151 /* ShowWindow(SW_MAXIMIZE) for a visible MDI child window */
3152 static const struct message WmMaximizeMDIchildVisibleSeq[] = {
3153     { HCBT_MINMAX, hook|lparam, 0, SW_MAXIMIZE },
3154     { WM_GETMINMAXINFO, sent },
3155     { WM_WINDOWPOSCHANGING, sent|wparam, SWP_FRAMECHANGED|SWP_STATECHANGED },
3156     { WM_NCCALCSIZE, sent|wparam, 1 },
3157     { WM_CHILDACTIVATE, sent|wparam|lparam, 0, 0 },
3158     { WM_WINDOWPOSCHANGED, sent|wparam, SWP_FRAMECHANGED|SWP_NOMOVE|SWP_NOCLIENTMOVE|SWP_STATECHANGED },
3159     { WM_SIZE, sent|defwinproc|wparam, SIZE_MAXIMIZED },
3160      /* in MDI frame */
3161     { WM_WINDOWPOSCHANGING, sent|wparam, SWP_FRAMECHANGED|SWP_NOACTIVATE|SWP_NOSIZE|SWP_NOMOVE },
3162     { WM_NCCALCSIZE, sent|wparam, 1 },
3163     { WM_WINDOWPOSCHANGED, sent|wparam, SWP_FRAMECHANGED|SWP_NOACTIVATE|SWP_NOSIZE|SWP_NOMOVE|SWP_NOCLIENTSIZE|SWP_NOCLIENTMOVE },
3164     { EVENT_OBJECT_LOCATIONCHANGE, winevent_hook|wparam|lparam, 0, 0 }, /* MDI frame */
3165     { EVENT_OBJECT_LOCATIONCHANGE, winevent_hook|wparam|lparam, 0, 0 }, /* MDI child */
3166     { 0 }
3167 };
3168 /* ShowWindow(SW_RESTORE) for a visible maximized MDI child window */
3169 static const struct message WmRestoreMDIchildVisibleSeq[] = {
3170     { HCBT_MINMAX, hook|lparam, 0, SW_RESTORE },
3171     { WM_WINDOWPOSCHANGING, sent|wparam, SWP_FRAMECHANGED|SWP_STATECHANGED },
3172     { WM_NCCALCSIZE, sent|wparam, 1 },
3173     { WM_CHILDACTIVATE, sent|wparam|lparam, 0, 0 },
3174     { WM_WINDOWPOSCHANGED, sent|wparam, SWP_FRAMECHANGED|SWP_NOMOVE|SWP_NOCLIENTMOVE|SWP_STATECHANGED },
3175     { WM_SIZE, sent|defwinproc|wparam, SIZE_RESTORED },
3176      /* in MDI frame */
3177     { WM_WINDOWPOSCHANGING, sent|wparam, SWP_FRAMECHANGED|SWP_NOACTIVATE|SWP_NOSIZE|SWP_NOMOVE },
3178     { WM_NCCALCSIZE, sent|wparam, 1 },
3179     { WM_WINDOWPOSCHANGED, sent|wparam, SWP_FRAMECHANGED|SWP_NOACTIVATE|SWP_NOSIZE|SWP_NOMOVE|SWP_NOCLIENTSIZE|SWP_NOCLIENTMOVE },
3180     { EVENT_OBJECT_LOCATIONCHANGE, winevent_hook|wparam|lparam, 0, 0 }, /* MDI frame */
3181     { EVENT_OBJECT_LOCATIONCHANGE, winevent_hook|wparam|lparam, 0, 0 }, /* MDI child */
3182     { 0 }
3183 };
3184 /* ShowWindow(SW_RESTORE) for a visible minimized MDI child window */
3185 static const struct message WmRestoreMDIchildVisibleSeq_2[] = {
3186     { HCBT_MINMAX, hook|lparam, 0, SW_RESTORE },
3187     { WM_QUERYOPEN, sent|wparam|lparam, 0, 0 },
3188     { WM_WINDOWPOSCHANGING, sent|wparam, SWP_FRAMECHANGED|SWP_NOCOPYBITS|SWP_STATECHANGED },
3189     { WM_NCCALCSIZE, sent|wparam, 1 },
3190     { WM_CHILDACTIVATE, sent|wparam|lparam, 0, 0 },
3191     { WM_WINDOWPOSCHANGED, sent|wparam, SWP_FRAMECHANGED|SWP_NOCOPYBITS|SWP_STATECHANGED },
3192     { WM_MOVE, sent|defwinproc },
3193     { WM_SIZE, sent|defwinproc|wparam, SIZE_RESTORED },
3194     { EVENT_OBJECT_LOCATIONCHANGE, winevent_hook|wparam|lparam, 0, 0 }, /* MDI child */
3195     { EVENT_SYSTEM_MINIMIZEEND, winevent_hook|wparam|lparam, 0, 0 }, /* MDI child */
3196     { HCBT_SETFOCUS, hook },
3197     { WM_IME_SETCONTEXT, sent|wparam|optional, 1 },
3198     { WM_IME_NOTIFY, sent|wparam|defwinproc|optional, 2 },
3199     { EVENT_OBJECT_FOCUS, winevent_hook|wparam|lparam, OBJID_CLIENT, 0 },
3200     { WM_SETFOCUS, sent },
3201     { 0 }
3202 };
3203 /* ShowWindow(SW_MINIMIZE) for a visible restored MDI child window */
3204 static const struct message WmMinimizeMDIchildVisibleSeq[] = {
3205     { HCBT_MINMAX, hook|lparam, 0, SW_MINIMIZE },
3206     { WM_WINDOWPOSCHANGING, sent|wparam, SWP_SHOWWINDOW|SWP_FRAMECHANGED|SWP_NOACTIVATE|SWP_NOCOPYBITS|SWP_STATECHANGED },
3207     { WM_NCCALCSIZE, sent|wparam, 1 },
3208     { WM_WINDOWPOSCHANGED, sent|wparam, SWP_FRAMECHANGED|SWP_NOACTIVATE|SWP_NOCOPYBITS|SWP_STATECHANGED },
3209     { WM_MOVE, sent|defwinproc },
3210     { WM_SIZE, sent|defwinproc|wparam|lparam, SIZE_MINIMIZED, 0 },
3211     { WM_CHILDACTIVATE, sent|wparam|lparam|defwinproc, 0, 0 },
3212     { EVENT_OBJECT_LOCATIONCHANGE, winevent_hook|wparam|lparam, 0, 0 }, /* MDI child */
3213     { EVENT_SYSTEM_MINIMIZESTART, winevent_hook|wparam|lparam, 0, 0 }, /* MDI child */
3214     /* FIXME: Wine creates an icon/title window while Windows doesn't */
3215     { WM_PARENTNOTIFY, sent|parent|wparam|optional, WM_CREATE }, /* MDI client */
3216     { 0 }
3217 };
3218 /* ShowWindow(SW_RESTORE) for a not visible MDI child window */
3219 static const struct message WmRestoreMDIchildInisibleSeq[] = {
3220     { HCBT_MINMAX, hook|lparam, 0, SW_RESTORE },
3221     { WM_WINDOWPOSCHANGING, sent|wparam, SWP_SHOWWINDOW|SWP_FRAMECHANGED|SWP_STATECHANGED  },
3222     { WM_NCCALCSIZE, sent|wparam, 1 },
3223     { EVENT_OBJECT_SHOW, winevent_hook|wparam|lparam, 0, 0 },
3224     { WM_CHILDACTIVATE, sent|wparam|lparam, 0, 0 },
3225     { WM_WINDOWPOSCHANGED, sent|wparam, SWP_SHOWWINDOW|SWP_FRAMECHANGED|SWP_NOMOVE|SWP_NOCLIENTMOVE|SWP_STATECHANGED },
3226     { WM_SIZE, sent|defwinproc|wparam, SIZE_RESTORED },
3227      /* in MDI frame */
3228     { WM_WINDOWPOSCHANGING, sent|wparam, SWP_FRAMECHANGED|SWP_NOACTIVATE|SWP_NOSIZE|SWP_NOMOVE },
3229     { WM_NCCALCSIZE, sent|wparam, 1 },
3230     { WM_WINDOWPOSCHANGED, sent|wparam, SWP_FRAMECHANGED|SWP_NOACTIVATE|SWP_NOSIZE|SWP_NOMOVE|SWP_NOCLIENTSIZE|SWP_NOCLIENTMOVE },
3231     { EVENT_OBJECT_LOCATIONCHANGE, winevent_hook|wparam|lparam, 0, 0 }, /* MDI frame */
3232     { EVENT_OBJECT_LOCATIONCHANGE, winevent_hook|wparam|lparam, 0, 0 }, /* MDI child */
3233     { 0 }
3234 };
3235
3236 static HWND mdi_client;
3237 static WNDPROC old_mdi_client_proc;
3238
3239 static LRESULT WINAPI mdi_client_hook_proc(HWND hwnd, UINT message, WPARAM wParam, LPARAM lParam)
3240 {
3241     struct recvd_message msg;
3242
3243     /* do not log painting messages */
3244     if (message != WM_PAINT &&
3245         message != WM_NCPAINT &&
3246         message != WM_SYNCPAINT &&
3247         message != WM_ERASEBKGND &&
3248         message != WM_NCHITTEST &&
3249         message != WM_GETTEXT &&
3250         message != WM_MDIGETACTIVE &&
3251         !ignore_message( message ))
3252     {
3253         msg.hwnd = hwnd;
3254         msg.message = message;
3255         msg.flags = sent|wparam|lparam;
3256         msg.wParam = wParam;
3257         msg.lParam = lParam;
3258         msg.descr = "mdi client";
3259         add_message(&msg);
3260     }
3261
3262     return CallWindowProcA(old_mdi_client_proc, hwnd, message, wParam, lParam);
3263 }
3264
3265 static LRESULT WINAPI mdi_child_wnd_proc(HWND hwnd, UINT message, WPARAM wParam, LPARAM lParam)
3266 {
3267     static LONG defwndproc_counter = 0;
3268     LRESULT ret;
3269     struct recvd_message msg;
3270
3271     /* do not log painting messages */
3272     if (message != WM_PAINT &&
3273         message != WM_NCPAINT &&
3274         message != WM_SYNCPAINT &&
3275         message != WM_ERASEBKGND &&
3276         message != WM_NCHITTEST &&
3277         message != WM_GETTEXT &&
3278         !ignore_message( message ))
3279     {
3280         switch (message)
3281         {
3282             case WM_MDIACTIVATE:
3283             {
3284                 HWND active, client = GetParent(hwnd);
3285
3286                 active = (HWND)SendMessageA(client, WM_MDIGETACTIVE, 0, 0);
3287
3288                 if (hwnd == (HWND)lParam) /* if we are being activated */
3289                     ok (active == (HWND)lParam, "new active %p != active %p\n", (HWND)lParam, active);
3290                 else
3291                     ok (active == (HWND)wParam, "old active %p != active %p\n", (HWND)wParam, active);
3292                 break;
3293             }
3294         }
3295
3296         msg.hwnd = hwnd;
3297         msg.message = message;
3298         msg.flags = sent|wparam|lparam;
3299         if (defwndproc_counter) msg.flags |= defwinproc;
3300         msg.wParam = wParam;
3301         msg.lParam = lParam;
3302         msg.descr = "mdi child";
3303         add_message(&msg);
3304     }
3305
3306     defwndproc_counter++;
3307     ret = DefMDIChildProcA(hwnd, message, wParam, lParam);
3308     defwndproc_counter--;
3309
3310     return ret;
3311 }
3312
3313 static LRESULT WINAPI mdi_frame_wnd_proc(HWND hwnd, UINT message, WPARAM wParam, LPARAM lParam)
3314 {
3315     static LONG defwndproc_counter = 0;
3316     LRESULT ret;
3317     struct recvd_message msg;
3318
3319     /* do not log painting messages */
3320     if (message != WM_PAINT &&
3321         message != WM_NCPAINT &&
3322         message != WM_SYNCPAINT &&
3323         message != WM_ERASEBKGND &&
3324         message != WM_NCHITTEST &&
3325         message != WM_GETTEXT &&
3326         !ignore_message( message ))
3327     {
3328         msg.hwnd = hwnd;
3329         msg.message = message;
3330         msg.flags = sent|wparam|lparam;
3331         if (defwndproc_counter) msg.flags |= defwinproc;
3332         msg.wParam = wParam;
3333         msg.lParam = lParam;
3334         msg.descr = "mdi frame";
3335         add_message(&msg);
3336     }
3337
3338     defwndproc_counter++;
3339     ret = DefFrameProcA(hwnd, mdi_client, message, wParam, lParam);
3340     defwndproc_counter--;
3341
3342     return ret;
3343 }
3344
3345 static BOOL mdi_RegisterWindowClasses(void)
3346 {
3347     WNDCLASSA cls;
3348
3349     cls.style = 0;
3350     cls.lpfnWndProc = mdi_frame_wnd_proc;
3351     cls.cbClsExtra = 0;
3352     cls.cbWndExtra = 0;
3353     cls.hInstance = GetModuleHandleA(0);
3354     cls.hIcon = 0;
3355     cls.hCursor = LoadCursorA(0, IDC_ARROW);
3356     cls.hbrBackground = GetStockObject(WHITE_BRUSH);
3357     cls.lpszMenuName = NULL;
3358     cls.lpszClassName = "MDI_frame_class";
3359     if (!RegisterClassA(&cls)) return FALSE;
3360
3361     cls.lpfnWndProc = mdi_child_wnd_proc;
3362     cls.lpszClassName = "MDI_child_class";
3363     if (!RegisterClassA(&cls)) return FALSE;
3364
3365     if (!GetClassInfoA(0, "MDIClient", &cls)) assert(0);
3366     old_mdi_client_proc = cls.lpfnWndProc;
3367     cls.hInstance = GetModuleHandleA(0);
3368     cls.lpfnWndProc = mdi_client_hook_proc;
3369     cls.lpszClassName = "MDI_client_class";
3370     if (!RegisterClassA(&cls)) assert(0);
3371
3372     return TRUE;
3373 }
3374
3375 static void test_mdi_messages(void)
3376 {
3377     MDICREATESTRUCTA mdi_cs;
3378     CLIENTCREATESTRUCT client_cs;
3379     HWND mdi_frame, mdi_child, mdi_child2, active_child;
3380     BOOL zoomed;
3381     RECT rc;
3382     HMENU hMenu = CreateMenu();
3383
3384     if (!mdi_RegisterWindowClasses()) assert(0);
3385
3386     flush_sequence();
3387
3388     trace("creating MDI frame window\n");
3389     mdi_frame = CreateWindowExA(0, "MDI_frame_class", "MDI frame window",
3390                                 WS_CAPTION | WS_SYSMENU | WS_MINIMIZEBOX |
3391                                 WS_MAXIMIZEBOX | WS_VISIBLE,
3392                                 100, 100, CW_USEDEFAULT, CW_USEDEFAULT,
3393                                 GetDesktopWindow(), hMenu,
3394                                 GetModuleHandleA(0), NULL);
3395     assert(mdi_frame);
3396     ok_sequence(WmCreateMDIframeSeq, "Create MDI frame window", FALSE);
3397
3398     ok(GetActiveWindow() == mdi_frame, "wrong active window %p\n", GetActiveWindow());
3399     ok(GetFocus() == mdi_frame, "wrong focus window %p\n", GetFocus());
3400
3401     trace("creating MDI client window\n");
3402     GetClientRect(mdi_frame, &rc);
3403     client_cs.hWindowMenu = 0;
3404     client_cs.idFirstChild = MDI_FIRST_CHILD_ID;
3405     mdi_client = CreateWindowExA(0, "MDI_client_class",
3406                                  NULL,
3407                                  WS_CHILD | WS_VISIBLE | MDIS_ALLCHILDSTYLES,
3408                                  rc.left, rc.top, rc.right - rc.left, rc.bottom - rc.top,
3409                                  mdi_frame, 0, GetModuleHandleA(0), &client_cs);
3410     assert(mdi_client);
3411     ok_sequence(WmCreateMDIclientSeq, "Create visible MDI client window", FALSE);
3412
3413     ok(GetActiveWindow() == mdi_frame, "wrong active window %p\n", GetActiveWindow());
3414     ok(GetFocus() == mdi_frame, "input focus should be on MDI frame not on %p\n", GetFocus());
3415
3416     active_child = (HWND)SendMessageA(mdi_client, WM_MDIGETACTIVE, 0, (LPARAM)&zoomed);
3417     ok(!active_child, "wrong active MDI child %p\n", active_child);
3418     ok(!zoomed, "wrong zoomed state %d\n", zoomed);
3419
3420     SetFocus(0);
3421     flush_sequence();
3422
3423     trace("creating invisible MDI child window\n");
3424     mdi_child = CreateWindowExA(WS_EX_MDICHILD, "MDI_child_class", "MDI child",
3425                                 WS_CHILD,
3426                                 0, 0, CW_USEDEFAULT, CW_USEDEFAULT,
3427                                 mdi_client, 0, GetModuleHandleA(0), NULL);
3428     assert(mdi_child);
3429
3430     flush_sequence();
3431     ShowWindow(mdi_child, SW_SHOWNORMAL);
3432     ok_sequence(WmShowChildSeq, "ShowWindow(SW_SHOWNORMAL) MDI child window", FALSE);
3433
3434     ok(GetWindowLongA(mdi_child, GWL_STYLE) & WS_VISIBLE, "MDI child should be visible\n");
3435     ok(IsWindowVisible(mdi_child), "MDI child should be visible\n");
3436
3437     ok(GetActiveWindow() == mdi_frame, "wrong active window %p\n", GetActiveWindow());
3438     ok(GetFocus() == 0, "wrong focus window %p\n", GetFocus());
3439
3440     active_child = (HWND)SendMessageA(mdi_client, WM_MDIGETACTIVE, 0, (LPARAM)&zoomed);
3441     ok(!active_child, "wrong active MDI child %p\n", active_child);
3442     ok(!zoomed, "wrong zoomed state %d\n", zoomed);
3443
3444     ShowWindow(mdi_child, SW_HIDE);
3445     ok_sequence(WmHideChildSeq, "ShowWindow(SW_HIDE) MDI child window", FALSE);
3446     flush_sequence();
3447
3448     ShowWindow(mdi_child, SW_SHOW);
3449     ok_sequence(WmShowChildSeq, "ShowWindow(SW_SHOW) MDI child window", FALSE);
3450
3451     ok(GetWindowLongA(mdi_child, GWL_STYLE) & WS_VISIBLE, "MDI child should be visible\n");
3452     ok(IsWindowVisible(mdi_child), "MDI child should be visible\n");
3453
3454     ok(GetActiveWindow() == mdi_frame, "wrong active window %p\n", GetActiveWindow());
3455     ok(GetFocus() == 0, "wrong focus window %p\n", GetFocus());
3456
3457     active_child = (HWND)SendMessageA(mdi_client, WM_MDIGETACTIVE, 0, (LPARAM)&zoomed);
3458     ok(!active_child, "wrong active MDI child %p\n", active_child);
3459     ok(!zoomed, "wrong zoomed state %d\n", zoomed);
3460
3461     DestroyWindow(mdi_child);
3462     flush_sequence();
3463
3464     trace("creating visible MDI child window\n");
3465     mdi_child = CreateWindowExA(WS_EX_MDICHILD, "MDI_child_class", "MDI child",
3466                                 WS_CHILD | WS_VISIBLE,
3467                                 0, 0, CW_USEDEFAULT, CW_USEDEFAULT,
3468                                 mdi_client, 0, GetModuleHandleA(0), NULL);
3469     assert(mdi_child);
3470     ok_sequence(WmCreateMDIchildVisibleSeq, "Create visible MDI child window", FALSE);
3471
3472     ok(GetWindowLongA(mdi_child, GWL_STYLE) & WS_VISIBLE, "MDI child should be visible\n");
3473     ok(IsWindowVisible(mdi_child), "MDI child should be visible\n");
3474
3475     ok(GetActiveWindow() == mdi_frame, "wrong active window %p\n", GetActiveWindow());
3476     ok(GetFocus() == mdi_child, "wrong focus window %p\n", GetFocus());
3477
3478     active_child = (HWND)SendMessageA(mdi_client, WM_MDIGETACTIVE, 0, (LPARAM)&zoomed);
3479     ok(active_child == mdi_child, "wrong active MDI child %p\n", active_child);
3480     ok(!zoomed, "wrong zoomed state %d\n", zoomed);
3481     flush_sequence();
3482
3483     DestroyWindow(mdi_child);
3484     ok_sequence(WmDestroyMDIchildVisibleSeq, "Destroy visible MDI child window", TRUE);
3485
3486     ok(GetActiveWindow() == mdi_frame, "wrong active window %p\n", GetActiveWindow());
3487     ok(GetFocus() == 0, "wrong focus window %p\n", GetFocus());
3488
3489     /* Win2k: MDI client still returns a just destroyed child as active
3490      * Win9x: MDI client returns 0
3491      */
3492     active_child = (HWND)SendMessageA(mdi_client, WM_MDIGETACTIVE, 0, (LPARAM)&zoomed);
3493     ok(active_child == mdi_child || /* win2k */
3494        !active_child, /* win9x */
3495        "wrong active MDI child %p\n", active_child);
3496     ok(!zoomed, "wrong zoomed state %d\n", zoomed);
3497
3498     flush_sequence();
3499
3500     trace("creating invisible MDI child window\n");
3501     mdi_child2 = CreateWindowExA(WS_EX_MDICHILD, "MDI_child_class", "MDI child",
3502                                 WS_CHILD,
3503                                 0, 0, CW_USEDEFAULT, CW_USEDEFAULT,
3504                                 mdi_client, 0, GetModuleHandleA(0), NULL);
3505     assert(mdi_child2);
3506     ok_sequence(WmCreateMDIchildInvisibleSeq, "Create invisible MDI child window", FALSE);
3507
3508     ok(!(GetWindowLongA(mdi_child2, GWL_STYLE) & WS_VISIBLE), "MDI child should not be visible\n");
3509     ok(!IsWindowVisible(mdi_child2), "MDI child should not be visible\n");
3510
3511     ok(GetActiveWindow() == mdi_frame, "wrong active window %p\n", GetActiveWindow());
3512     ok(GetFocus() == 0, "wrong focus window %p\n", GetFocus());
3513
3514     /* Win2k: MDI client still returns a just destroyed child as active
3515      * Win9x: MDI client returns mdi_child2
3516      */
3517     active_child = (HWND)SendMessageA(mdi_client, WM_MDIGETACTIVE, 0, (LPARAM)&zoomed);
3518     ok(active_child == mdi_child || /* win2k */
3519        active_child == mdi_child2, /* win9x */
3520        "wrong active MDI child %p\n", active_child);
3521     ok(!zoomed, "wrong zoomed state %d\n", zoomed);
3522     flush_sequence();
3523
3524     ShowWindow(mdi_child2, SW_MAXIMIZE);
3525     ok_sequence(WmMaximizeMDIchildInvisibleSeq, "ShowWindow(SW_MAXIMIZE):invisible MDI child", FALSE);
3526
3527     ok(GetWindowLongA(mdi_child2, GWL_STYLE) & WS_VISIBLE, "MDI child should be visible\n");
3528     ok(IsWindowVisible(mdi_child2), "MDI child should be visible\n");
3529
3530     active_child = (HWND)SendMessageA(mdi_client, WM_MDIGETACTIVE, 0, (LPARAM)&zoomed);
3531     ok(active_child == mdi_child2, "wrong active MDI child %p\n", active_child);
3532     ok(zoomed, "wrong zoomed state %d\n", zoomed);
3533     flush_sequence();
3534
3535     ok(GetActiveWindow() == mdi_frame, "wrong active window %p\n", GetActiveWindow());
3536     ok(GetFocus() == mdi_child2 || /* win2k */
3537        GetFocus() == 0, /* win9x */
3538        "wrong focus window %p\n", GetFocus());
3539
3540     SetFocus(0);
3541     flush_sequence();
3542
3543     ShowWindow(mdi_child2, SW_HIDE);
3544     ok_sequence(WmHideChildSeq, "ShowWindow(SW_HIDE):MDI child", FALSE);
3545
3546     ShowWindow(mdi_child2, SW_RESTORE);
3547     ok_sequence(WmRestoreMDIchildInisibleSeq, "ShowWindow(SW_RESTORE):invisible MDI child", FALSE);
3548     flush_sequence();
3549
3550     ok(GetWindowLongA(mdi_child2, GWL_STYLE) & WS_VISIBLE, "MDI child should be visible\n");
3551     ok(IsWindowVisible(mdi_child2), "MDI child should be visible\n");
3552
3553     active_child = (HWND)SendMessageA(mdi_client, WM_MDIGETACTIVE, 0, (LPARAM)&zoomed);
3554     ok(active_child == mdi_child2, "wrong active MDI child %p\n", active_child);
3555     ok(!zoomed, "wrong zoomed state %d\n", zoomed);
3556     flush_sequence();
3557
3558     SetFocus(0);
3559     flush_sequence();
3560
3561     ShowWindow(mdi_child2, SW_HIDE);
3562     ok_sequence(WmHideChildSeq, "ShowWindow(SW_HIDE):MDI child", FALSE);
3563
3564     ShowWindow(mdi_child2, SW_SHOW);
3565     ok_sequence(WmShowChildSeq, "ShowWindow(SW_SHOW):MDI child", FALSE);
3566
3567     ok(GetActiveWindow() == mdi_frame, "wrong active window %p\n", GetActiveWindow());
3568     ok(GetFocus() == 0, "wrong focus window %p\n", GetFocus());
3569
3570     ShowWindow(mdi_child2, SW_MAXIMIZE);
3571     ok_sequence(WmMaximizeMDIchildVisibleSeq, "ShowWindow(SW_MAXIMIZE):MDI child", FALSE);
3572
3573     ok(GetActiveWindow() == mdi_frame, "wrong active window %p\n", GetActiveWindow());
3574     ok(GetFocus() == 0, "wrong focus window %p\n", GetFocus());
3575
3576     ShowWindow(mdi_child2, SW_RESTORE);
3577     ok_sequence(WmRestoreMDIchildVisibleSeq, "ShowWindow(SW_RESTORE):maximized MDI child", FALSE);
3578
3579     ok(GetActiveWindow() == mdi_frame, "wrong active window %p\n", GetActiveWindow());
3580     ok(GetFocus() == 0, "wrong focus window %p\n", GetFocus());
3581
3582     ShowWindow(mdi_child2, SW_MINIMIZE);
3583     ok_sequence(WmMinimizeMDIchildVisibleSeq, "ShowWindow(SW_MINIMIZE):MDI child", TRUE);
3584
3585     ok(GetActiveWindow() == mdi_frame, "wrong active window %p\n", GetActiveWindow());
3586     ok(GetFocus() == 0, "wrong focus window %p\n", GetFocus());
3587
3588     active_child = (HWND)SendMessageA(mdi_client, WM_MDIGETACTIVE, 0, (LPARAM)&zoomed);
3589     ok(active_child == mdi_child2, "wrong active MDI child %p\n", active_child);
3590     ok(!zoomed, "wrong zoomed state %d\n", zoomed);
3591     flush_sequence();
3592
3593     ShowWindow(mdi_child2, SW_RESTORE);
3594     ok_sequence(WmRestoreMDIchildVisibleSeq_2, "ShowWindow(SW_RESTORE):minimized MDI child", FALSE);
3595
3596     ok(GetActiveWindow() == mdi_frame, "wrong active window %p\n", GetActiveWindow());
3597     ok(GetFocus() == mdi_child2, "wrong focus window %p\n", GetFocus());
3598
3599     active_child = (HWND)SendMessageA(mdi_client, WM_MDIGETACTIVE, 0, (LPARAM)&zoomed);
3600     ok(active_child == mdi_child2, "wrong active MDI child %p\n", active_child);
3601     ok(!zoomed, "wrong zoomed state %d\n", zoomed);
3602     flush_sequence();
3603
3604     SetFocus(0);
3605     flush_sequence();
3606
3607     ShowWindow(mdi_child2, SW_HIDE);
3608     ok_sequence(WmHideChildSeq, "ShowWindow(SW_HIDE):MDI child", FALSE);
3609
3610     ok(GetActiveWindow() == mdi_frame, "wrong active window %p\n", GetActiveWindow());
3611     ok(GetFocus() == 0, "wrong focus window %p\n", GetFocus());
3612
3613     DestroyWindow(mdi_child2);
3614     ok_sequence(WmDestroyMDIchildInvisibleSeq, "Destroy invisible MDI child window", FALSE);
3615
3616     ok(GetActiveWindow() == mdi_frame, "wrong active window %p\n", GetActiveWindow());
3617     ok(GetFocus() == 0, "wrong focus window %p\n", GetFocus());
3618
3619     /* test for maximized MDI children */
3620     trace("creating maximized visible MDI child window 1\n");
3621     mdi_child = CreateWindowExA(WS_EX_MDICHILD, "MDI_child_class", "MDI child",
3622                                 WS_CHILD | WS_VISIBLE | WS_MAXIMIZEBOX | WS_MAXIMIZE,
3623                                 0, 0, CW_USEDEFAULT, CW_USEDEFAULT,
3624                                 mdi_client, 0, GetModuleHandleA(0), NULL);
3625     assert(mdi_child);
3626     ok_sequence(WmCreateMDIchildVisibleMaxSeq1, "Create maximized visible 1st MDI child window", TRUE);
3627     ok(IsZoomed(mdi_child), "1st MDI child should be maximized\n");
3628
3629     ok(GetActiveWindow() == mdi_frame, "wrong active window %p\n", GetActiveWindow());
3630     ok(GetFocus() == mdi_child || /* win2k */
3631        GetFocus() == 0, /* win9x */
3632        "wrong focus window %p\n", GetFocus());
3633
3634     active_child = (HWND)SendMessageA(mdi_client, WM_MDIGETACTIVE, 0, (LPARAM)&zoomed);
3635     ok(active_child == mdi_child, "wrong active MDI child %p\n", active_child);
3636     ok(zoomed, "wrong zoomed state %d\n", zoomed);
3637     flush_sequence();
3638
3639     trace("creating maximized visible MDI child window 2\n");
3640     mdi_child2 = CreateWindowExA(WS_EX_MDICHILD, "MDI_child_class", "MDI child",
3641                                 WS_CHILD | WS_VISIBLE | WS_MAXIMIZEBOX | WS_MAXIMIZE,
3642                                 0, 0, CW_USEDEFAULT, CW_USEDEFAULT,
3643                                 mdi_client, 0, GetModuleHandleA(0), NULL);
3644     assert(mdi_child2);
3645     ok_sequence(WmCreateMDIchildVisibleMaxSeq2, "Create maximized visible 2nd MDI child 2 window", TRUE);
3646     ok(IsZoomed(mdi_child2), "2nd MDI child should be maximized\n");
3647     ok(!IsZoomed(mdi_child), "1st MDI child should NOT be maximized\n");
3648
3649     ok(GetActiveWindow() == mdi_frame, "wrong active window %p\n", GetActiveWindow());
3650     ok(GetFocus() == mdi_child2, "wrong focus window %p\n", GetFocus());
3651
3652     active_child = (HWND)SendMessageA(mdi_client, WM_MDIGETACTIVE, 0, (LPARAM)&zoomed);
3653     ok(active_child == mdi_child2, "wrong active MDI child %p\n", active_child);
3654     ok(zoomed, "wrong zoomed state %d\n", zoomed);
3655     flush_sequence();
3656
3657     trace("destroying maximized visible MDI child window 2\n");
3658     DestroyWindow(mdi_child2);
3659     ok_sequence(WmDestroyMDIchildVisibleSeq, "Destroy visible MDI child window", TRUE);
3660
3661     ok(!IsZoomed(mdi_child), "1st MDI child should NOT be maximized\n");
3662
3663     ok(GetActiveWindow() == mdi_frame, "wrong active window %p\n", GetActiveWindow());
3664     ok(GetFocus() == 0, "wrong focus window %p\n", GetFocus());
3665
3666     /* Win2k: MDI client still returns a just destroyed child as active
3667      * Win9x: MDI client returns 0
3668      */
3669     active_child = (HWND)SendMessageA(mdi_client, WM_MDIGETACTIVE, 0, (LPARAM)&zoomed);
3670     ok(active_child == mdi_child2 || /* win2k */
3671        !active_child, /* win9x */
3672        "wrong active MDI child %p\n", active_child);
3673     flush_sequence();
3674
3675     ShowWindow(mdi_child, SW_MAXIMIZE);
3676     ok(IsZoomed(mdi_child), "1st MDI child should be maximized\n");
3677     flush_sequence();
3678
3679     ok(GetActiveWindow() == mdi_frame, "wrong active window %p\n", GetActiveWindow());
3680     ok(GetFocus() == mdi_child, "wrong focus window %p\n", GetFocus());
3681
3682     trace("re-creating maximized visible MDI child window 2\n");
3683     mdi_child2 = CreateWindowExA(WS_EX_MDICHILD, "MDI_child_class", "MDI child",
3684                                 WS_CHILD | WS_VISIBLE | WS_MAXIMIZEBOX | WS_MAXIMIZE,
3685                                 0, 0, CW_USEDEFAULT, CW_USEDEFAULT,
3686                                 mdi_client, 0, GetModuleHandleA(0), NULL);
3687     assert(mdi_child2);
3688     ok_sequence(WmCreateMDIchildVisibleMaxSeq2, "Create maximized visible 2nd MDI child 2 window", TRUE);
3689     ok(IsZoomed(mdi_child2), "2nd MDI child should be maximized\n");
3690     ok(!IsZoomed(mdi_child), "1st MDI child should NOT be maximized\n");
3691
3692     ok(GetActiveWindow() == mdi_frame, "wrong active window %p\n", GetActiveWindow());
3693     ok(GetFocus() == mdi_child2, "wrong focus window %p\n", GetFocus());
3694
3695     active_child = (HWND)SendMessageA(mdi_client, WM_MDIGETACTIVE, 0, (LPARAM)&zoomed);
3696     ok(active_child == mdi_child2, "wrong active MDI child %p\n", active_child);
3697     ok(zoomed, "wrong zoomed state %d\n", zoomed);
3698     flush_sequence();
3699
3700     SendMessageA(mdi_child2, WM_SYSCOMMAND, SC_CLOSE, 0);
3701     ok_sequence(WmDestroyMDIchildVisibleMaxSeq2, "WM_SYSCOMMAND/SC_CLOSE on a visible maximized MDI child window", TRUE);
3702     ok(!IsWindow(mdi_child2), "MDI child 2 should be destroyed\n");
3703
3704     ok(IsZoomed(mdi_child), "1st MDI child should be maximized\n");
3705     ok(GetActiveWindow() == mdi_frame, "wrong active window %p\n", GetActiveWindow());
3706     ok(GetFocus() == mdi_child, "wrong focus window %p\n", GetFocus());
3707
3708     active_child = (HWND)SendMessageA(mdi_client, WM_MDIGETACTIVE, 0, (LPARAM)&zoomed);
3709     ok(active_child == mdi_child, "wrong active MDI child %p\n", active_child);
3710     ok(zoomed, "wrong zoomed state %d\n", zoomed);
3711     flush_sequence();
3712
3713     DestroyWindow(mdi_child);
3714     ok_sequence(WmDestroyMDIchildVisibleSeq, "Destroy visible MDI child window", TRUE);
3715
3716     ok(GetActiveWindow() == mdi_frame, "wrong active window %p\n", GetActiveWindow());
3717     ok(GetFocus() == 0, "wrong focus window %p\n", GetFocus());
3718
3719     /* Win2k: MDI client still returns a just destroyed child as active
3720      * Win9x: MDI client returns 0
3721      */
3722     active_child = (HWND)SendMessageA(mdi_client, WM_MDIGETACTIVE, 0, (LPARAM)&zoomed);
3723     ok(active_child == mdi_child || /* win2k */
3724        !active_child, /* win9x */
3725        "wrong active MDI child %p\n", active_child);
3726     flush_sequence();
3727
3728     trace("creating maximized invisible MDI child window\n");
3729     mdi_child2 = CreateWindowExA(WS_EX_MDICHILD, "MDI_child_class", "MDI child",
3730                                 WS_CHILD | WS_MAXIMIZE | WS_CAPTION | WS_THICKFRAME,
3731                                 0, 0, CW_USEDEFAULT, CW_USEDEFAULT,
3732                                 mdi_client, 0, GetModuleHandleA(0), NULL);
3733     assert(mdi_child2);
3734     ok_sequence(WmCreateMDIchildInvisibleMaxSeq4, "Create maximized invisible MDI child window", FALSE);
3735     ok(IsZoomed(mdi_child2), "MDI child should be maximized\n");
3736     ok(!(GetWindowLongA(mdi_child2, GWL_STYLE) & WS_VISIBLE), "MDI child should be not visible\n");
3737     ok(!IsWindowVisible(mdi_child2), "MDI child should be not visible\n");
3738
3739     /* Win2k: MDI client still returns a just destroyed child as active
3740      * Win9x: MDI client returns 0
3741      */
3742     active_child = (HWND)SendMessageA(mdi_client, WM_MDIGETACTIVE, 0, (LPARAM)&zoomed);
3743     ok(active_child == mdi_child || /* win2k */
3744        !active_child || active_child == mdi_child2, /* win9x */
3745        "wrong active MDI child %p\n", active_child);
3746     flush_sequence();
3747
3748     trace("call ShowWindow(mdi_child, SW_MAXIMIZE)\n");
3749     ShowWindow(mdi_child2, SW_MAXIMIZE);
3750     ok_sequence(WmMaximizeMDIchildInvisibleSeq2, "ShowWindow(SW_MAXIMIZE):invisible maximized MDI child", FALSE);
3751     ok(IsZoomed(mdi_child2), "MDI child should be maximized\n");
3752     ok(GetWindowLongA(mdi_child2, GWL_STYLE) & WS_VISIBLE, "MDI child should be visible\n");
3753     ok(IsWindowVisible(mdi_child2), "MDI child should be visible\n");
3754
3755     active_child = (HWND)SendMessageA(mdi_client, WM_MDIGETACTIVE, 0, (LPARAM)&zoomed);
3756     ok(active_child == mdi_child2, "wrong active MDI child %p\n", active_child);
3757     ok(zoomed, "wrong zoomed state %d\n", zoomed);
3758     flush_sequence();
3759
3760     SendMessageA(mdi_client, WM_MDIDESTROY, (WPARAM)mdi_child2, 0);
3761     flush_sequence();
3762
3763     /* end of test for maximized MDI children */
3764     SetFocus(0);
3765     flush_sequence();
3766     trace("creating maximized visible MDI child window 1(Switch test)\n");
3767     mdi_child = CreateWindowExA(WS_EX_MDICHILD, "MDI_child_class", "MDI child",
3768                                 WS_CHILD | WS_VISIBLE | WS_MAXIMIZEBOX | WS_MAXIMIZE,
3769                                 0, 0, CW_USEDEFAULT, CW_USEDEFAULT,
3770                                 mdi_client, 0, GetModuleHandleA(0), NULL);
3771     assert(mdi_child);
3772     ok_sequence(WmCreateMDIchildVisibleMaxSeq1, "Create maximized visible 1st MDI child window(Switch test)", TRUE);
3773     ok(IsZoomed(mdi_child), "1st MDI child should be maximized(Switch test)\n");
3774
3775     ok(GetActiveWindow() == mdi_frame, "wrong active window %p(Switch test)\n", GetActiveWindow());
3776     ok(GetFocus() == mdi_child || /* win2k */
3777        GetFocus() == 0, /* win9x */
3778        "wrong focus window %p(Switch test)\n", GetFocus());
3779
3780     active_child = (HWND)SendMessageA(mdi_client, WM_MDIGETACTIVE, 0, (LPARAM)&zoomed);
3781     ok(active_child == mdi_child, "wrong active MDI child %p(Switch test)\n", active_child);
3782     ok(zoomed, "wrong zoomed state %d(Switch test)\n", zoomed);
3783     flush_sequence();
3784
3785     trace("creating maximized visible MDI child window 2(Switch test)\n");
3786     mdi_child2 = CreateWindowExA(WS_EX_MDICHILD, "MDI_child_class", "MDI child",
3787                                 WS_CHILD | WS_VISIBLE | WS_MAXIMIZEBOX | WS_MAXIMIZE,
3788                                 0, 0, CW_USEDEFAULT, CW_USEDEFAULT,
3789                                 mdi_client, 0, GetModuleHandleA(0), NULL);
3790     assert(mdi_child2);
3791     ok_sequence(WmCreateMDIchildVisibleMaxSeq2, "Create maximized visible 2nd MDI child window (Switch test)", TRUE);
3792
3793     ok(IsZoomed(mdi_child2), "2nd MDI child should be maximized(Switch test)\n");
3794     ok(!IsZoomed(mdi_child), "1st MDI child should NOT be maximized(Switch test)\n");
3795
3796     ok(GetActiveWindow() == mdi_frame, "wrong active window %p(Switch test)\n", GetActiveWindow());
3797     ok(GetFocus() == mdi_child2, "wrong focus window %p(Switch test)\n", GetFocus());
3798
3799     active_child = (HWND)SendMessageA(mdi_client, WM_MDIGETACTIVE, 0, (LPARAM)&zoomed);
3800     ok(active_child == mdi_child2, "wrong active MDI child %p(Switch test)\n", active_child);
3801     ok(zoomed, "wrong zoomed state %d(Switch test)\n", zoomed);
3802     flush_sequence();
3803
3804     trace("Switch child window.\n");
3805     SendMessageA(mdi_client, WM_MDIACTIVATE, (WPARAM)mdi_child, 0);
3806     ok_sequence(WmSwitchChild, "Child did not switch correctly", TRUE);
3807     trace("end of test for switch maximized MDI children\n");
3808     flush_sequence();
3809
3810     /* Prepare for switching test of not maximized MDI children  */
3811     ShowWindow( mdi_child, SW_NORMAL );
3812     ok(!IsZoomed(mdi_child), "wrong zoomed state for %p(Switch test)\n", mdi_child);
3813     ok(!IsZoomed(mdi_child2), "wrong zoomed state for %p(Switch test)\n", mdi_child2);
3814     active_child = (HWND)SendMessageA(mdi_client, WM_MDIGETACTIVE, 0, 0);
3815     ok(active_child == mdi_child, "wrong active MDI child %p(Switch test)\n", active_child);
3816     flush_sequence();
3817
3818     SendMessageA(mdi_client, WM_MDIACTIVATE, (WPARAM)mdi_child2, 0);
3819     ok_sequence(WmSwitchNotMaximizedChild, "Not maximized child did not switch correctly", FALSE);
3820     trace("end of test for switch not maximized MDI children\n");
3821     flush_sequence();
3822
3823     SendMessageA(mdi_client, WM_MDIDESTROY, (WPARAM)mdi_child, 0);
3824     flush_sequence();
3825
3826     SendMessageA(mdi_client, WM_MDIDESTROY, (WPARAM)mdi_child2, 0);
3827     flush_sequence();
3828
3829     SetFocus(0);
3830     flush_sequence();
3831     /* end of tests for switch maximized/not maximized MDI children */
3832
3833     mdi_cs.szClass = "MDI_child_Class";
3834     mdi_cs.szTitle = "MDI child";
3835     mdi_cs.hOwner = GetModuleHandleA(0);
3836     mdi_cs.x = 0;
3837     mdi_cs.y = 0;
3838     mdi_cs.cx = CW_USEDEFAULT;
3839     mdi_cs.cy = CW_USEDEFAULT;
3840     mdi_cs.style = WS_CHILD | WS_SYSMENU | WS_VISIBLE | WS_MAXIMIZEBOX | WS_MAXIMIZE;
3841     mdi_cs.lParam = 0;
3842     mdi_child = (HWND)SendMessageA(mdi_client, WM_MDICREATE, 0, (LPARAM)&mdi_cs);
3843     ok(mdi_child != 0, "MDI child creation failed\n");
3844     ok_sequence(WmCreateMDIchildVisibleMaxSeq3, "WM_MDICREATE for maximized visible MDI child window", TRUE);
3845
3846     ok(GetMenuItemID(hMenu, GetMenuItemCount(hMenu) - 1) == SC_CLOSE, "SC_CLOSE menu item not found\n");
3847
3848     active_child = (HWND)SendMessageA(mdi_client, WM_MDIGETACTIVE, 0, (LPARAM)&zoomed);
3849     ok(active_child == mdi_child, "wrong active MDI child %p\n", active_child);
3850
3851     ok(IsZoomed(mdi_child), "MDI child should be maximized\n");
3852     ok(GetActiveWindow() == mdi_frame, "wrong active window %p\n", GetActiveWindow());
3853     ok(GetFocus() == mdi_child, "wrong focus window %p\n", GetFocus());
3854
3855     active_child = (HWND)SendMessageA(mdi_client, WM_MDIGETACTIVE, 0, (LPARAM)&zoomed);
3856     ok(active_child == mdi_child, "wrong active MDI child %p\n", active_child);
3857     ok(zoomed, "wrong zoomed state %d\n", zoomed);
3858     flush_sequence();
3859
3860     SendMessageA(mdi_client, WM_MDIDESTROY, (WPARAM)mdi_child, 0);
3861     ok_sequence(WmDestroyMDIchildVisibleMaxSeq1, "Destroy visible maximized MDI child window", TRUE);
3862
3863     ok(!IsWindow(mdi_child), "MDI child should be destroyed\n");
3864     active_child = (HWND)SendMessageA(mdi_client, WM_MDIGETACTIVE, 0, (LPARAM)&zoomed);
3865     ok(!active_child, "wrong active MDI child %p\n", active_child);
3866
3867     SetFocus(0);
3868     flush_sequence();
3869
3870     DestroyWindow(mdi_client);
3871     ok_sequence(WmDestroyMDIclientSeq, "Destroy MDI client window", FALSE);
3872
3873     /* test maximization of MDI child with invisible parent */
3874     client_cs.hWindowMenu = 0;
3875     mdi_client = CreateWindow("MDI_client_class",
3876                                  NULL,
3877                                  WS_CHILD | WS_CLIPCHILDREN | WS_VSCROLL | WS_HSCROLL | WS_VISIBLE,
3878                                  0, 0, 660, 430,
3879                                  mdi_frame, 0, GetModuleHandleA(0), &client_cs);
3880     ok_sequence(WmCreateMDIclientSeq, "Create MDI client window", FALSE);
3881
3882     ShowWindow(mdi_client, SW_HIDE);
3883     ok_sequence(WmHideMDIclientSeq, "Hide MDI client window", FALSE);
3884
3885     mdi_child = CreateWindowExA(WS_EX_MDICHILD, "MDI_child_class", "MDI child",
3886                                 WS_CHILD | WS_CLIPCHILDREN | WS_VSCROLL | WS_HSCROLL,
3887                                 0, 0, 650, 440,
3888                                 mdi_client, 0, GetModuleHandleA(0), NULL);
3889     ok_sequence(WmCreateMDIchildInvisibleParentSeq, "Create MDI child window with invisible parent", FALSE);
3890
3891     SendMessage(mdi_client, WM_MDIMAXIMIZE, (WPARAM) mdi_child, 0);
3892     ok_sequence(WmMaximizeMDIchildInvisibleParentSeq, "Maximize MDI child window with invisible parent", TRUE);
3893     zoomed = IsZoomed(mdi_child);
3894     ok(zoomed, "wrong zoomed state %d\n", zoomed);
3895     
3896     ShowWindow(mdi_client, SW_SHOW);
3897     ok_sequence(WmShowMDIclientSeq, "Show MDI client window", FALSE);
3898
3899     DestroyWindow(mdi_child);
3900     ok_sequence(WmDestroyMDIchildVisibleSeq, "Destroy visible maximized MDI child window", TRUE);
3901
3902     /* end of test for maximization of MDI child with invisible parent */
3903
3904     DestroyWindow(mdi_client);
3905     ok_sequence(WmDestroyMDIclientSeq, "Destroy MDI client window", FALSE);
3906
3907     DestroyWindow(mdi_frame);
3908     ok_sequence(WmDestroyMDIframeSeq, "Destroy MDI frame window", FALSE);
3909 }
3910 /************************* End of MDI test **********************************/
3911
3912 static void test_WM_SETREDRAW(HWND hwnd)
3913 {
3914     DWORD style = GetWindowLongA(hwnd, GWL_STYLE);
3915
3916     flush_events();
3917     flush_sequence();
3918
3919     SendMessageA(hwnd, WM_SETREDRAW, FALSE, 0);
3920     ok_sequence(WmSetRedrawFalseSeq, "SetRedraw:FALSE", FALSE);
3921
3922     ok(!(GetWindowLongA(hwnd, GWL_STYLE) & WS_VISIBLE), "WS_VISIBLE should NOT be set\n");
3923     ok(!IsWindowVisible(hwnd), "IsWindowVisible() should return FALSE\n");
3924
3925     flush_sequence();
3926     SendMessageA(hwnd, WM_SETREDRAW, TRUE, 0);
3927     ok_sequence(WmSetRedrawTrueSeq, "SetRedraw:TRUE", FALSE);
3928
3929     ok(GetWindowLongA(hwnd, GWL_STYLE) & WS_VISIBLE, "WS_VISIBLE should be set\n");
3930     ok(IsWindowVisible(hwnd), "IsWindowVisible() should return TRUE\n");
3931
3932     /* restore original WS_VISIBLE state */
3933     SetWindowLongA(hwnd, GWL_STYLE, style);
3934
3935     flush_events();
3936     flush_sequence();
3937 }
3938
3939 static INT_PTR CALLBACK TestModalDlgProcA(HWND hwnd, UINT message, WPARAM wParam, LPARAM lParam)
3940 {
3941     struct recvd_message msg;
3942
3943     if (ignore_message( message )) return 0;
3944
3945     switch (message)
3946     {
3947         /* ignore */
3948         case WM_MOUSEMOVE:
3949         case WM_NCMOUSEMOVE:
3950         case WM_NCMOUSELEAVE:
3951         case WM_SETCURSOR:
3952             return 0;
3953         case WM_NCHITTEST:
3954             return HTCLIENT;
3955     }
3956
3957     msg.hwnd = hwnd;
3958     msg.message = message;
3959     msg.flags = sent|wparam|lparam;
3960     msg.wParam = wParam;
3961     msg.lParam = lParam;
3962     msg.descr = "dialog";
3963     add_message(&msg);
3964
3965     if (message == WM_INITDIALOG) SetTimer( hwnd, 1, 100, NULL );
3966     if (message == WM_TIMER) EndDialog( hwnd, 0 );
3967     return 0;
3968 }
3969
3970 static void test_hv_scroll_1(HWND hwnd, INT ctl, DWORD clear, DWORD set, INT min, INT max)
3971 {
3972     DWORD style, exstyle;
3973     INT xmin, xmax;
3974     BOOL ret;
3975
3976     exstyle = GetWindowLongA(hwnd, GWL_EXSTYLE);
3977     style = GetWindowLongA(hwnd, GWL_STYLE);
3978     /* do not be confused by WS_DLGFRAME set */
3979     if ((style & WS_CAPTION) == WS_CAPTION) style &= ~WS_CAPTION;
3980
3981     if (clear) ok(style & clear, "style %08x should be set\n", clear);
3982     if (set) ok(!(style & set), "style %08x should not be set\n", set);
3983
3984     ret = SetScrollRange(hwnd, ctl, min, max, FALSE);
3985     ok( ret, "SetScrollRange(%d) error %d\n", ctl, GetLastError());
3986     if ((style & (WS_DLGFRAME | WS_BORDER | WS_THICKFRAME)) || (exstyle & WS_EX_DLGMODALFRAME))
3987         ok_sequence(WmSetScrollRangeHV_NC_Seq, "SetScrollRange(SB_HORZ/SB_VERT) NC", FALSE);
3988     else
3989         ok_sequence(WmSetScrollRangeHVSeq, "SetScrollRange(SB_HORZ/SB_VERT)", FALSE);
3990
3991     style = GetWindowLongA(hwnd, GWL_STYLE);
3992     if (set) ok(style & set, "style %08x should be set\n", set);
3993     if (clear) ok(!(style & clear), "style %08x should not be set\n", clear);
3994
3995     /* a subsequent call should do nothing */
3996     ret = SetScrollRange(hwnd, ctl, min, max, FALSE);
3997     ok( ret, "SetScrollRange(%d) error %d\n", ctl, GetLastError());
3998     ok_sequence(WmEmptySeq, "SetScrollRange(SB_HORZ/SB_VERT) empty sequence", FALSE);
3999
4000     xmin = 0xdeadbeef;
4001     xmax = 0xdeadbeef;
4002     ret = GetScrollRange(hwnd, ctl, &xmin, &xmax);
4003     ok( ret, "GetScrollRange(%d) error %d\n", ctl, GetLastError());
4004     ok_sequence(WmEmptySeq, "GetScrollRange(SB_HORZ/SB_VERT) empty sequence", FALSE);
4005     ok(xmin == min, "unexpected min scroll value %d\n", xmin);
4006     ok(xmax == max, "unexpected max scroll value %d\n", xmax);
4007 }
4008
4009 static void test_hv_scroll_2(HWND hwnd, INT ctl, DWORD clear, DWORD set, INT min, INT max)
4010 {
4011     DWORD style, exstyle;
4012     SCROLLINFO si;
4013     BOOL ret;
4014
4015     exstyle = GetWindowLongA(hwnd, GWL_EXSTYLE);
4016     style = GetWindowLongA(hwnd, GWL_STYLE);
4017     /* do not be confused by WS_DLGFRAME set */
4018     if ((style & WS_CAPTION) == WS_CAPTION) style &= ~WS_CAPTION;
4019
4020     if (clear) ok(style & clear, "style %08x should be set\n", clear);
4021     if (set) ok(!(style & set), "style %08x should not be set\n", set);
4022
4023     si.cbSize = sizeof(si);
4024     si.fMask = SIF_RANGE;
4025     si.nMin = min;
4026     si.nMax = max;
4027     SetScrollInfo(hwnd, ctl, &si, TRUE);
4028     if ((style & (WS_DLGFRAME | WS_BORDER | WS_THICKFRAME)) || (exstyle & WS_EX_DLGMODALFRAME))
4029         ok_sequence(WmSetScrollRangeHV_NC_Seq, "SetScrollInfo(SB_HORZ/SB_VERT) NC", FALSE);
4030     else
4031         ok_sequence(WmSetScrollRangeHVSeq, "SetScrollInfo(SB_HORZ/SB_VERT)", FALSE);
4032
4033     style = GetWindowLongA(hwnd, GWL_STYLE);
4034     if (set) ok(style & set, "style %08x should be set\n", set);
4035     if (clear) ok(!(style & clear), "style %08x should not be set\n", clear);
4036
4037     /* a subsequent call should do nothing */
4038     SetScrollInfo(hwnd, ctl, &si, TRUE);
4039     if (style & WS_HSCROLL)
4040         ok_sequence(WmSetScrollRangeHSeq_empty, "SetScrollInfo(SB_HORZ/SB_VERT) empty sequence", FALSE);
4041     else if (style & WS_VSCROLL)
4042         ok_sequence(WmSetScrollRangeVSeq_empty, "SetScrollInfo(SB_HORZ/SB_VERT) empty sequence", FALSE);
4043     else
4044         ok_sequence(WmEmptySeq, "SetScrollInfo(SB_HORZ/SB_VERT) empty sequence", FALSE);
4045
4046     si.fMask = SIF_PAGE;
4047     si.nPage = 5;
4048     SetScrollInfo(hwnd, ctl, &si, FALSE);
4049     ok_sequence(WmEmptySeq, "SetScrollInfo(SB_HORZ/SB_VERT) empty sequence", FALSE);
4050
4051     si.fMask = SIF_POS;
4052     si.nPos = max - 1;
4053     SetScrollInfo(hwnd, ctl, &si, FALSE);
4054     ok_sequence(WmEmptySeq, "SetScrollInfo(SB_HORZ/SB_VERT) empty sequence", FALSE);
4055
4056     si.fMask = SIF_RANGE;
4057     si.nMin = 0xdeadbeef;
4058     si.nMax = 0xdeadbeef;
4059     ret = GetScrollInfo(hwnd, ctl, &si);
4060     ok( ret, "GetScrollInfo error %d\n", GetLastError());
4061     ok_sequence(WmEmptySeq, "GetScrollRange(SB_HORZ/SB_VERT) empty sequence", FALSE);
4062     ok(si.nMin == min, "unexpected min scroll value %d\n", si.nMin);
4063     ok(si.nMax == max, "unexpected max scroll value %d\n", si.nMax);
4064 }
4065
4066 /* Win9x sends WM_USER+xxx while and NT versions send SBM_xxx messages */
4067 static void test_scroll_messages(HWND hwnd)
4068 {
4069     SCROLLINFO si;
4070     INT min, max;
4071     BOOL ret;
4072
4073     flush_events();
4074     flush_sequence();
4075
4076     min = 0xdeadbeef;
4077     max = 0xdeadbeef;
4078     ret = GetScrollRange(hwnd, SB_CTL, &min, &max);
4079     ok( ret, "GetScrollRange error %d\n", GetLastError());
4080     if (sequence->message != WmGetScrollRangeSeq[0].message)
4081         trace("GetScrollRange(SB_CTL) generated unknown message %04x\n", sequence->message);
4082     /* values of min and max are undefined */
4083     flush_sequence();
4084
4085     ret = SetScrollRange(hwnd, SB_CTL, 10, 150, FALSE);
4086     ok( ret, "SetScrollRange error %d\n", GetLastError());
4087     if (sequence->message != WmSetScrollRangeSeq[0].message)
4088         trace("SetScrollRange(SB_CTL) generated unknown message %04x\n", sequence->message);
4089     flush_sequence();
4090
4091     min = 0xdeadbeef;
4092     max = 0xdeadbeef;
4093     ret = GetScrollRange(hwnd, SB_CTL, &min, &max);
4094     ok( ret, "GetScrollRange error %d\n", GetLastError());
4095     if (sequence->message != WmGetScrollRangeSeq[0].message)
4096         trace("GetScrollRange(SB_CTL) generated unknown message %04x\n", sequence->message);
4097     /* values of min and max are undefined */
4098     flush_sequence();
4099
4100     si.cbSize = sizeof(si);
4101     si.fMask = SIF_RANGE;
4102     si.nMin = 20;
4103     si.nMax = 160;
4104     SetScrollInfo(hwnd, SB_CTL, &si, FALSE);
4105     if (sequence->message != WmSetScrollRangeSeq[0].message)
4106         trace("SetScrollInfo(SB_CTL) generated unknown message %04x\n", sequence->message);
4107     flush_sequence();
4108
4109     si.fMask = SIF_PAGE;
4110     si.nPage = 10;
4111     SetScrollInfo(hwnd, SB_CTL, &si, FALSE);
4112     if (sequence->message != WmSetScrollRangeSeq[0].message)
4113         trace("SetScrollInfo(SB_CTL) generated unknown message %04x\n", sequence->message);
4114     flush_sequence();
4115
4116     si.fMask = SIF_POS;
4117     si.nPos = 20;
4118     SetScrollInfo(hwnd, SB_CTL, &si, FALSE);
4119     if (sequence->message != WmSetScrollRangeSeq[0].message)
4120         trace("SetScrollInfo(SB_CTL) generated unknown message %04x\n", sequence->message);
4121     flush_sequence();
4122
4123     si.fMask = SIF_RANGE;
4124     si.nMin = 0xdeadbeef;
4125     si.nMax = 0xdeadbeef;
4126     ret = GetScrollInfo(hwnd, SB_CTL, &si);
4127     ok( ret, "GetScrollInfo error %d\n", GetLastError());
4128     if (sequence->message != WmGetScrollInfoSeq[0].message)
4129         trace("GetScrollInfo(SB_CTL) generated unknown message %04x\n", sequence->message);
4130     /* values of min and max are undefined */
4131     flush_sequence();
4132
4133     /* set WS_HSCROLL */
4134     test_hv_scroll_1(hwnd, SB_HORZ, 0, WS_HSCROLL, 10, 150);
4135     /* clear WS_HSCROLL */
4136     test_hv_scroll_1(hwnd, SB_HORZ, WS_HSCROLL, 0, 0, 0);
4137
4138     /* set WS_HSCROLL */
4139     test_hv_scroll_2(hwnd, SB_HORZ, 0, WS_HSCROLL, 10, 150);
4140     /* clear WS_HSCROLL */
4141     test_hv_scroll_2(hwnd, SB_HORZ, WS_HSCROLL, 0, 0, 0);
4142
4143     /* set WS_VSCROLL */
4144     test_hv_scroll_1(hwnd, SB_VERT, 0, WS_VSCROLL, 10, 150);
4145     /* clear WS_VSCROLL */
4146     test_hv_scroll_1(hwnd, SB_VERT, WS_VSCROLL, 0, 0, 0);
4147
4148     /* set WS_VSCROLL */
4149     test_hv_scroll_2(hwnd, SB_VERT, 0, WS_VSCROLL, 10, 150);
4150     /* clear WS_VSCROLL */
4151     test_hv_scroll_2(hwnd, SB_VERT, WS_VSCROLL, 0, 0, 0);
4152 }
4153
4154 static void test_showwindow(void)
4155 {
4156     HWND hwnd, hchild;
4157     RECT rc;
4158
4159     hwnd = CreateWindowExA(0, "TestWindowClass", "Test overlapped", WS_OVERLAPPEDWINDOW,
4160                            100, 100, 200, 200, 0, 0, 0, NULL);
4161     ok (hwnd != 0, "Failed to create overlapped window\n");
4162     hchild = CreateWindowExA(0, "TestWindowClass", "Test child", WS_CHILD,
4163                              0, 0, 10, 10, hwnd, 0, 0, NULL);
4164     ok (hchild != 0, "Failed to create child\n");
4165     flush_sequence();
4166
4167     /* ShowWindow( SW_SHOWNA) for invisible top level window */
4168     trace("calling ShowWindow( SW_SHOWNA) for invisible top level window\n");
4169     ok( ShowWindow(hwnd, SW_SHOWNA) == FALSE, "ShowWindow: window was visible\n" );
4170     ok_sequence(WmSHOWNATopInvisible, "ShowWindow(SW_SHOWNA) on invisible top level window", FALSE);
4171
4172     /* ShowWindow( SW_SHOWNA) for now visible top level window */
4173     trace("calling ShowWindow( SW_SHOWNA) for now visible top level window\n");
4174     ok( ShowWindow(hwnd, SW_SHOWNA) != FALSE, "ShowWindow: window was invisible\n" );
4175     ok_sequence(WmSHOWNATopVisible, "ShowWindow(SW_SHOWNA) on visible top level window", FALSE);
4176     /* back to invisible */
4177     ShowWindow(hchild, SW_HIDE);
4178     ShowWindow(hwnd, SW_HIDE);
4179     flush_sequence();
4180     /* ShowWindow(SW_SHOWNA) with child and parent invisible */ 
4181     trace("calling ShowWindow( SW_SHOWNA) for invisible child with invisible parent\n");
4182     ok( ShowWindow(hchild, SW_SHOWNA) == FALSE, "ShowWindow: window was visible\n" );
4183     ok_sequence(WmSHOWNAChildInvisParInvis, "ShowWindow(SW_SHOWNA) invisible child and parent", FALSE);
4184     /* ShowWindow(SW_SHOWNA) with child visible and parent invisible */ 
4185     ok( ShowWindow(hchild, SW_SHOW) != FALSE, "ShowWindow: window was invisible\n" );
4186     flush_sequence();
4187     trace("calling ShowWindow( SW_SHOWNA) for the visible child and invisible parent\n");
4188     ok( ShowWindow(hchild, SW_SHOWNA) != FALSE, "ShowWindow: window was invisible\n" );
4189     ok_sequence(WmSHOWNAChildVisParInvis, "ShowWindow(SW_SHOWNA) visible child and invisible parent", FALSE);
4190     /* ShowWindow(SW_SHOWNA) with child visible and parent visible */
4191     ShowWindow( hwnd, SW_SHOW);
4192     flush_sequence();
4193     trace("calling ShowWindow( SW_SHOWNA) for the visible child and parent\n");
4194     ok( ShowWindow(hchild, SW_SHOWNA) != FALSE, "ShowWindow: window was invisible\n" );
4195     ok_sequence(WmSHOWNAChildVisParVis, "ShowWindow(SW_SHOWNA) for the visible child and parent", FALSE);
4196
4197     /* ShowWindow(SW_SHOWNA) with child invisible and parent visible */
4198     ShowWindow( hchild, SW_HIDE);
4199     flush_sequence();
4200     trace("calling ShowWindow( SW_SHOWNA) for the invisible child and visible parent\n");
4201     ok( ShowWindow(hchild, SW_SHOWNA) == FALSE, "ShowWindow: window was visible\n" );
4202     ok_sequence(WmSHOWNAChildInvisParVis, "ShowWindow(SW_SHOWNA) for the invisible child and visible parent", FALSE);
4203
4204     SetCapture(hchild);
4205     ok(GetCapture() == hchild, "wrong capture window %p\n", GetCapture());
4206     DestroyWindow(hchild);
4207     ok(!GetCapture(), "wrong capture window %p\n", GetCapture());
4208
4209     DestroyWindow(hwnd);
4210     flush_sequence();
4211
4212     /* Popup windows */
4213     /* Test 1:
4214      * 1. Create invisible maximized popup window.
4215      * 2. Move and resize it.
4216      * 3. Show it maximized.
4217      */
4218     trace("calling CreateWindowExA( WS_MAXIMIZE ) for invisible maximized popup window\n");
4219     hwnd = CreateWindowExA(0, "TestWindowClass", "Test popup", WS_POPUP | WS_MAXIMIZE,
4220                            100, 100, 200, 200, 0, 0, 0, NULL);
4221     ok (hwnd != 0, "Failed to create popup window\n");
4222     ok(IsZoomed(hwnd), "window should be maximized\n");
4223     ok_sequence(WmCreateInvisibleMaxPopupSeq, "CreateWindow(WS_MAXIMIZED):popup", FALSE);
4224
4225     GetWindowRect(hwnd, &rc);
4226     ok( rc.right-rc.left == GetSystemMetrics(SM_CXSCREEN) &&
4227         rc.bottom-rc.top == GetSystemMetrics(SM_CYSCREEN),
4228         "Invalid maximized size before ShowWindow (%d,%d)-(%d,%d)\n",
4229         rc.left, rc.top, rc.right, rc.bottom);
4230     /* Reset window's size & position */
4231     SetWindowPos(hwnd, 0, 10, 10, 200, 200, SWP_NOZORDER | SWP_NOACTIVATE);
4232     ok(IsZoomed(hwnd), "window should be maximized\n");
4233     flush_sequence();
4234
4235     trace("calling ShowWindow( SW_SHOWMAXIMIZE ) for invisible maximized popup window\n");
4236     ShowWindow(hwnd, SW_SHOWMAXIMIZED);
4237     ok(IsZoomed(hwnd), "window should be maximized\n");
4238     ok_sequence(WmShowMaxPopupResizedSeq, "ShowWindow(SW_SHOWMAXIMIZED):invisible maximized and resized popup", FALSE);
4239
4240     GetWindowRect(hwnd, &rc);
4241     ok( rc.right-rc.left == GetSystemMetrics(SM_CXSCREEN) &&
4242         rc.bottom-rc.top == GetSystemMetrics(SM_CYSCREEN),
4243         "Invalid maximized size after ShowWindow (%d,%d)-(%d,%d)\n",
4244         rc.left, rc.top, rc.right, rc.bottom);
4245     DestroyWindow(hwnd);
4246     flush_sequence();
4247
4248     /* Test 2:
4249      * 1. Create invisible maximized popup window.
4250      * 2. Show it maximized.
4251      */
4252     trace("calling CreateWindowExA( WS_MAXIMIZE ) for invisible maximized popup window\n");
4253     hwnd = CreateWindowExA(0, "TestWindowClass", "Test popup", WS_POPUP | WS_MAXIMIZE,
4254                            100, 100, 200, 200, 0, 0, 0, NULL);
4255     ok (hwnd != 0, "Failed to create popup window\n");
4256     ok(IsZoomed(hwnd), "window should be maximized\n");
4257     ok_sequence(WmCreateInvisibleMaxPopupSeq, "CreateWindow(WS_MAXIMIZED):popup", FALSE);
4258
4259     trace("calling ShowWindow( SW_SHOWMAXIMIZE ) for invisible maximized popup window\n");
4260     ShowWindow(hwnd, SW_SHOWMAXIMIZED);
4261     ok(IsZoomed(hwnd), "window should be maximized\n");
4262     ok_sequence(WmShowMaxPopupSeq, "ShowWindow(SW_SHOWMAXIMIZED):invisible maximized popup", FALSE);
4263     DestroyWindow(hwnd);
4264     flush_sequence();
4265
4266     /* Test 3:
4267      * 1. Create visible maximized popup window.
4268      */
4269     trace("calling CreateWindowExA( WS_MAXIMIZE ) for maximized popup window\n");
4270     hwnd = CreateWindowExA(0, "TestWindowClass", "Test popup", WS_POPUP | WS_MAXIMIZE | WS_VISIBLE,
4271                            100, 100, 200, 200, 0, 0, 0, NULL);
4272     ok (hwnd != 0, "Failed to create popup window\n");
4273     ok(IsZoomed(hwnd), "window should be maximized\n");
4274     ok_sequence(WmCreateMaxPopupSeq, "CreateWindow(WS_MAXIMIZED):popup", FALSE);
4275     DestroyWindow(hwnd);
4276     flush_sequence();
4277
4278     /* Test 4:
4279      * 1. Create visible popup window.
4280      * 2. Maximize it.
4281      */
4282     trace("calling CreateWindowExA( WS_VISIBLE ) for popup window\n");
4283     hwnd = CreateWindowExA(0, "TestWindowClass", "Test popup", WS_POPUP | WS_VISIBLE,
4284                            100, 100, 200, 200, 0, 0, 0, NULL);
4285     ok (hwnd != 0, "Failed to create popup window\n");
4286     ok(!IsZoomed(hwnd), "window should NOT be maximized\n");
4287     ok_sequence(WmCreatePopupSeq, "CreateWindow(WS_VISIBLE):popup", FALSE);
4288
4289     trace("calling ShowWindow( SW_SHOWMAXIMIZE ) for visible popup window\n");
4290     ShowWindow(hwnd, SW_SHOWMAXIMIZED);
4291     ok(IsZoomed(hwnd), "window should be maximized\n");
4292     ok_sequence(WmShowVisMaxPopupSeq, "ShowWindow(SW_SHOWMAXIMIZED):popup", FALSE);
4293     DestroyWindow(hwnd);
4294     flush_sequence();
4295 }
4296
4297 static void test_sys_menu(void)
4298 {
4299     HWND hwnd;
4300     HMENU hmenu;
4301     UINT state;
4302
4303     hwnd = CreateWindowExA(0, "TestWindowClass", NULL, WS_OVERLAPPEDWINDOW,
4304                            100, 100, 200, 200, 0, 0, 0, NULL);
4305     ok (hwnd != 0, "Failed to create overlapped window\n");
4306
4307     flush_sequence();
4308
4309     /* test existing window without CS_NOCLOSE style */
4310     hmenu = GetSystemMenu(hwnd, FALSE);
4311     ok(hmenu != 0, "GetSystemMenu error %d\n", GetLastError());
4312
4313     state = GetMenuState(hmenu, SC_CLOSE, MF_BYCOMMAND);
4314     ok(state != 0xffffffff, "wrong SC_CLOSE state %x\n", state);
4315     ok(!(state & (MF_DISABLED | MF_GRAYED)), "wrong SC_CLOSE state %x\n", state);
4316
4317     EnableMenuItem(hmenu, SC_CLOSE, MF_BYCOMMAND | MF_GRAYED);
4318     ok_sequence(WmEmptySeq, "WmEnableMenuItem", FALSE);
4319
4320     state = GetMenuState(hmenu, SC_CLOSE, MF_BYCOMMAND);
4321     ok(state != 0xffffffff, "wrong SC_CLOSE state %x\n", state);
4322     ok((state & (MF_DISABLED | MF_GRAYED)) == MF_GRAYED, "wrong SC_CLOSE state %x\n", state);
4323
4324     EnableMenuItem(hmenu, SC_CLOSE, 0);
4325     ok_sequence(WmEmptySeq, "WmEnableMenuItem", FALSE);
4326
4327     state = GetMenuState(hmenu, SC_CLOSE, MF_BYCOMMAND);
4328     ok(state != 0xffffffff, "wrong SC_CLOSE state %x\n", state);
4329     ok(!(state & (MF_DISABLED | MF_GRAYED)), "wrong SC_CLOSE state %x\n", state);
4330
4331     /* test whether removing WS_SYSMENU destroys a system menu */
4332     SetWindowLongW(hwnd, GWL_STYLE, WS_POPUP);
4333     SetWindowPos(hwnd, 0, 0, 0, 0, 0, SWP_NOZORDER|SWP_NOACTIVATE|SWP_NOSIZE|SWP_NOMOVE|SWP_FRAMECHANGED);
4334     flush_sequence();
4335     hmenu = GetSystemMenu(hwnd, FALSE);
4336     ok(hmenu != 0, "GetSystemMenu error %d\n", GetLastError());
4337
4338     DestroyWindow(hwnd);
4339
4340     /* test new window with CS_NOCLOSE style */
4341     hwnd = CreateWindowExA(0, "NoCloseWindowClass", NULL, WS_OVERLAPPEDWINDOW,
4342                            100, 100, 200, 200, 0, 0, 0, NULL);
4343     ok (hwnd != 0, "Failed to create overlapped window\n");
4344
4345     hmenu = GetSystemMenu(hwnd, FALSE);
4346     ok(hmenu != 0, "GetSystemMenu error %d\n", GetLastError());
4347
4348     state = GetMenuState(hmenu, SC_CLOSE, MF_BYCOMMAND);
4349     ok(state == 0xffffffff, "wrong SC_CLOSE state %x\n", state);
4350
4351     DestroyWindow(hwnd);
4352
4353     /* test new window without WS_SYSMENU style */
4354     hwnd = CreateWindowExA(0, "NoCloseWindowClass", NULL, WS_OVERLAPPEDWINDOW & ~WS_SYSMENU,
4355                            100, 100, 200, 200, 0, 0, 0, NULL);
4356     ok(hwnd != 0, "Failed to create overlapped window\n");
4357
4358     hmenu = GetSystemMenu(hwnd, FALSE);
4359     ok(!hmenu, "GetSystemMenu error %d\n", GetLastError());
4360
4361     DestroyWindow(hwnd);
4362 }
4363
4364 /* For shown WS_OVERLAPPEDWINDOW */
4365 static const struct message WmSetIcon_1[] = {
4366     { WM_SETICON, sent },
4367     { 0x00AE, sent|defwinproc|optional }, /* XP */
4368     { WM_GETTEXT, sent|defwinproc|optional },
4369     { WM_GETTEXT, sent|defwinproc|optional }, /* XP sends a duplicate */
4370     { 0 }
4371 };
4372
4373 /* For WS_POPUP and hidden WS_OVERLAPPEDWINDOW */
4374 static const struct message WmSetIcon_2[] = {
4375     { WM_SETICON, sent },
4376     { 0 }
4377 };
4378
4379 /* Sending undocumented 0x3B message with wparam = 0x8000000b */
4380 static const struct message WmInitEndSession[] = {
4381     { 0x003B, sent },
4382     { WM_QUERYENDSESSION, sent|defwinproc|wparam|lparam, 0, ENDSESSION_LOGOFF },
4383     { 0 }
4384 };
4385
4386 /* Sending undocumented 0x3B message with wparam = 0x0000000b */
4387 static const struct message WmInitEndSession_2[] = {
4388     { 0x003B, sent },
4389     { WM_QUERYENDSESSION, sent|defwinproc|wparam|lparam, 0, 0 },
4390     { 0 }
4391 };
4392
4393 /* Sending undocumented 0x3B message with wparam = 0x80000008 */
4394 static const struct message WmInitEndSession_3[] = {
4395     { 0x003B, sent },
4396     { WM_ENDSESSION, sent|defwinproc|wparam|lparam, 0, ENDSESSION_LOGOFF },
4397     { 0 }
4398 };
4399
4400 /* Sending undocumented 0x3B message with wparam = 0x00000008 */
4401 static const struct message WmInitEndSession_4[] = {
4402     { 0x003B, sent },
4403     { WM_ENDSESSION, sent|defwinproc|wparam|lparam, 0, 0 },
4404     { 0 }
4405 };
4406
4407 /* Sending undocumented 0x3B message with wparam = 0x80000001 */
4408 static const struct message WmInitEndSession_5[] = {
4409     { 0x003B, sent },
4410     { WM_ENDSESSION, sent|defwinproc/*|wparam*/|lparam, 1, ENDSESSION_LOGOFF },
4411     { 0 }
4412 };
4413
4414 static const struct message WmOptionalPaint[] = {
4415     { WM_PAINT, sent|optional },
4416     { WM_NCPAINT, sent|beginpaint|optional },
4417     { WM_GETTEXT, sent|beginpaint|defwinproc|optional },
4418     { WM_ERASEBKGND, sent|beginpaint|optional },
4419     { 0 }
4420 };
4421
4422 static const struct message WmZOrder[] = {
4423     { WM_WINDOWPOSCHANGING, sent|wparam, 0, 0 },
4424     { WM_GETMINMAXINFO, sent|defwinproc|wparam, 0, 0 },
4425     { HCBT_ACTIVATE, hook },
4426     { WM_QUERYNEWPALETTE, sent|wparam|lparam|optional, 0, 0 },
4427     { WM_WINDOWPOSCHANGING, sent|wparam, 3, 0 },
4428     { WM_WINDOWPOSCHANGED, sent|wparam|optional, SWP_NOREDRAW|SWP_NOMOVE|SWP_NOSIZE|SWP_NOCLIENTSIZE|SWP_NOCLIENTMOVE, 0 },
4429     { WM_GETTEXT, sent|optional },
4430     { WM_NCCALCSIZE, sent|wparam|optional, 1 },
4431     { WM_ACTIVATEAPP, sent|wparam, 1, 0 },
4432     { WM_NCACTIVATE, sent|lparam, 1, 0 },
4433     { WM_GETTEXT, sent|defwinproc|optional },
4434     { WM_GETTEXT, sent|defwinproc|optional },
4435     { WM_ACTIVATE, sent|wparam|lparam, 1, 0 },
4436     { HCBT_SETFOCUS, hook },
4437     { WM_IME_SETCONTEXT, sent|wparam|defwinproc|optional, 1 },
4438     { WM_IME_NOTIFY, sent|wparam|defwinproc|optional, 2 },
4439     { WM_SETFOCUS, sent|wparam|defwinproc, 0 },
4440     { WM_WINDOWPOSCHANGED, sent|wparam, SWP_NOSIZE|SWP_NOMOVE|SWP_NOREDRAW|SWP_NOCLIENTSIZE|SWP_NOCLIENTMOVE },
4441     { WM_GETTEXT, sent|optional },
4442     { WM_NCCALCSIZE, sent|optional },
4443     { 0 }
4444 };
4445
4446 static void test_MsgWaitForMultipleObjects(HWND hwnd)
4447 {
4448     DWORD ret;
4449     MSG msg;
4450
4451     ret = MsgWaitForMultipleObjects(0, NULL, FALSE, 0, QS_POSTMESSAGE);
4452     ok(ret == WAIT_TIMEOUT, "MsgWaitForMultipleObjects returned %x\n", ret);
4453
4454     PostMessageA(hwnd, WM_USER, 0, 0);
4455
4456     ret = MsgWaitForMultipleObjects(0, NULL, FALSE, 0, QS_POSTMESSAGE);
4457     ok(ret == WAIT_OBJECT_0, "MsgWaitForMultipleObjects returned %x\n", ret);
4458
4459     ok(PeekMessageA( &msg, 0, 0, 0, PM_REMOVE ), "PeekMessage should succeed\n");
4460     ok(msg.message == WM_USER, "got %04x instead of WM_USER\n", msg.message);
4461
4462     ret = MsgWaitForMultipleObjects(0, NULL, FALSE, 0, QS_POSTMESSAGE);
4463     ok(ret == WAIT_TIMEOUT, "MsgWaitForMultipleObjects returned %x\n", ret);
4464
4465     PostMessageA(hwnd, WM_USER, 0, 0);
4466
4467     ret = MsgWaitForMultipleObjects(0, NULL, FALSE, 0, QS_POSTMESSAGE);
4468     ok(ret == WAIT_OBJECT_0, "MsgWaitForMultipleObjects returned %x\n", ret);
4469
4470     ok(PeekMessageA( &msg, 0, 0, 0, PM_NOREMOVE ), "PeekMessage should succeed\n");
4471     ok(msg.message == WM_USER, "got %04x instead of WM_USER\n", msg.message);
4472
4473     /* shows QS_POSTMESSAGE flag is cleared in the PeekMessage call */
4474     ret = MsgWaitForMultipleObjects(0, NULL, FALSE, 0, QS_POSTMESSAGE);
4475     ok(ret == WAIT_TIMEOUT, "MsgWaitForMultipleObjects returned %x\n", ret);
4476
4477     PostMessageA(hwnd, WM_USER, 0, 0);
4478
4479     /* new incoming message causes it to become signaled again */
4480     ret = MsgWaitForMultipleObjects(0, NULL, FALSE, 0, QS_POSTMESSAGE);
4481     ok(ret == WAIT_OBJECT_0, "MsgWaitForMultipleObjects returned %x\n", ret);
4482
4483     ok(PeekMessageA( &msg, 0, 0, 0, PM_REMOVE ), "PeekMessage should succeed\n");
4484     ok(msg.message == WM_USER, "got %04x instead of WM_USER\n", msg.message);
4485     ok(PeekMessageA( &msg, 0, 0, 0, PM_REMOVE ), "PeekMessage should succeed\n");
4486     ok(msg.message == WM_USER, "got %04x instead of WM_USER\n", msg.message);
4487 }
4488
4489 /* test if we receive the right sequence of messages */
4490 static void test_messages(void)
4491 {
4492     HWND hwnd, hparent, hchild;
4493     HWND hchild2, hbutton;
4494     HMENU hmenu;
4495     MSG msg;
4496     LRESULT res;
4497
4498     flush_sequence();
4499
4500     hwnd = CreateWindowExA(0, "TestWindowClass", "Test overlapped", WS_OVERLAPPEDWINDOW,
4501                            100, 100, 200, 200, 0, 0, 0, NULL);
4502     ok (hwnd != 0, "Failed to create overlapped window\n");
4503     ok_sequence(WmCreateOverlappedSeq, "CreateWindow:overlapped", FALSE);
4504
4505     /* test ShowWindow(SW_HIDE) on a newly created invisible window */
4506     ok( ShowWindow(hwnd, SW_HIDE) == FALSE, "ShowWindow: window was visible\n" );
4507     ok_sequence(WmEmptySeq, "ShowWindow(SW_HIDE):overlapped, invisible", FALSE);
4508
4509     /* test WM_SETREDRAW on a not visible top level window */
4510     test_WM_SETREDRAW(hwnd);
4511
4512     SetWindowPos(hwnd, 0,0,0,0,0, SWP_SHOWWINDOW|SWP_NOSIZE|SWP_NOMOVE);
4513     flush_events();
4514     ok_sequence(WmSWP_ShowOverlappedSeq, "SetWindowPos:SWP_SHOWWINDOW:overlapped", FALSE);
4515     ok(IsWindowVisible(hwnd), "window should be visible at this point\n");
4516
4517     ok(GetActiveWindow() == hwnd, "window should be active\n");
4518     ok(GetFocus() == hwnd, "window should have input focus\n");
4519     ShowWindow(hwnd, SW_HIDE);
4520     flush_events();
4521     ok_sequence(WmHideOverlappedSeq, "ShowWindow(SW_HIDE):overlapped", FALSE);
4522
4523     ShowWindow(hwnd, SW_SHOW);
4524     flush_events();
4525     ok_sequence(WmShowOverlappedSeq, "ShowWindow(SW_SHOW):overlapped", TRUE);
4526
4527     ShowWindow(hwnd, SW_HIDE);
4528     flush_events();
4529     ok_sequence(WmHideOverlappedSeq, "ShowWindow(SW_HIDE):overlapped", FALSE);
4530
4531     ShowWindow(hwnd, SW_SHOWMAXIMIZED);
4532     flush_events();
4533     ok_sequence(WmShowMaxOverlappedSeq, "ShowWindow(SW_SHOWMAXIMIZED):overlapped", TRUE);
4534     flush_sequence();
4535
4536     if (GetWindowLongW( hwnd, GWL_STYLE ) & WS_MAXIMIZE)
4537     {
4538         ShowWindow(hwnd, SW_RESTORE);
4539         flush_events();
4540         ok_sequence(WmShowRestoreMaxOverlappedSeq, "ShowWindow(SW_RESTORE):overlapped", TRUE);
4541         flush_sequence();
4542     }
4543
4544     ShowWindow(hwnd, SW_MINIMIZE);
4545     flush_events();
4546     ok_sequence(WmShowMinOverlappedSeq, "ShowWindow(SW_SHOWMINIMIZED):overlapped", TRUE);
4547     flush_sequence();
4548
4549     if (GetWindowLongW( hwnd, GWL_STYLE ) & WS_MINIMIZE)
4550     {
4551         ShowWindow(hwnd, SW_RESTORE);
4552         flush_events();
4553         ok_sequence(WmShowRestoreMinOverlappedSeq, "ShowWindow(SW_RESTORE):overlapped", TRUE);
4554         flush_sequence();
4555     }
4556
4557     ShowWindow(hwnd, SW_SHOW);
4558     flush_events();
4559     ok_sequence(WmOptionalPaint, "ShowWindow(SW_SHOW):overlapped already visible", FALSE);
4560
4561     SetWindowPos(hwnd, 0,0,0,0,0, SWP_HIDEWINDOW|SWP_NOSIZE|SWP_NOMOVE);
4562     ok_sequence(WmSWP_HideOverlappedSeq, "SetWindowPos:SWP_HIDEWINDOW:overlapped", FALSE);
4563     ok(!IsWindowVisible(hwnd), "window should not be visible at this point\n");
4564     ok(GetActiveWindow() == hwnd, "window should still be active\n");
4565
4566     /* test WM_SETREDRAW on a visible top level window */
4567     ShowWindow(hwnd, SW_SHOW);
4568     flush_events();
4569     test_WM_SETREDRAW(hwnd);
4570
4571     trace("testing scroll APIs on a visible top level window %p\n", hwnd);
4572     test_scroll_messages(hwnd);
4573
4574     /* test resizing and moving */
4575     SetWindowPos( hwnd, 0, 0, 0, 300, 300, SWP_NOMOVE|SWP_NOACTIVATE );
4576     ok_sequence(WmSWP_ResizeSeq, "SetWindowPos:Resize", FALSE );
4577     flush_events();
4578     flush_sequence();
4579     SetWindowPos( hwnd, 0, 200, 200, 0, 0, SWP_NOSIZE|SWP_NOACTIVATE );
4580     ok_sequence(WmSWP_MoveSeq, "SetWindowPos:Move", FALSE );
4581     flush_events();
4582     flush_sequence();
4583     SetWindowPos( hwnd, 0, 200, 200, 250, 250, SWP_NOZORDER|SWP_NOACTIVATE );
4584     ok_sequence(WmSWP_ResizeNoZOrder, "SetWindowPos:WmSWP_ResizeNoZOrder", FALSE );
4585     flush_events();
4586     flush_sequence();
4587
4588     /* popups don't get WM_GETMINMAXINFO */
4589     SetWindowLongW( hwnd, GWL_STYLE, WS_VISIBLE|WS_POPUP );
4590     SetWindowPos( hwnd, 0, 0, 0, 0, 0, SWP_NOZORDER|SWP_NOACTIVATE|SWP_NOSIZE|SWP_NOMOVE|SWP_FRAMECHANGED);
4591     flush_sequence();
4592     SetWindowPos( hwnd, 0, 0, 0, 200, 200, SWP_NOMOVE|SWP_NOACTIVATE );
4593     ok_sequence(WmSWP_ResizePopupSeq, "SetWindowPos:ResizePopup", FALSE );
4594
4595     DestroyWindow(hwnd);
4596     ok_sequence(WmDestroyOverlappedSeq, "DestroyWindow:overlapped", FALSE);
4597
4598     hparent = CreateWindowExA(0, "TestParentClass", "Test parent", WS_OVERLAPPEDWINDOW | WS_VISIBLE,
4599                               100, 100, 200, 200, 0, 0, 0, NULL);
4600     ok (hparent != 0, "Failed to create parent window\n");
4601     flush_sequence();
4602
4603     hchild = CreateWindowExA(0, "TestWindowClass", "Test child", WS_CHILD | WS_MAXIMIZE,
4604                              0, 0, 10, 10, hparent, 0, 0, NULL);
4605     ok (hchild != 0, "Failed to create child window\n");
4606     ok_sequence(WmCreateMaximizedChildSeq, "CreateWindow:maximized child", FALSE);
4607     DestroyWindow(hchild);
4608     flush_sequence();
4609
4610     /* visible child window with a caption */
4611     hchild = CreateWindowExA(0, "TestWindowClass", "Test child",
4612                              WS_CHILD | WS_VISIBLE | WS_CAPTION,
4613                              0, 0, 10, 10, hparent, 0, 0, NULL);
4614     ok (hchild != 0, "Failed to create child window\n");
4615     ok_sequence(WmCreateVisibleChildSeq, "CreateWindow:visible child", FALSE);
4616
4617     trace("testing scroll APIs on a visible child window %p\n", hchild);
4618     test_scroll_messages(hchild);
4619
4620     SetWindowPos(hchild, 0,0,0,0,0, SWP_SHOWWINDOW|SWP_NOSIZE|SWP_NOMOVE);
4621     ok_sequence(WmShowChildSeq_4, "SetWindowPos(SWP_SHOWWINDOW):child with a caption", FALSE);
4622
4623     DestroyWindow(hchild);
4624     flush_sequence();
4625
4626     hchild = CreateWindowExA(0, "TestWindowClass", "Test child", WS_CHILD,
4627                              0, 0, 10, 10, hparent, 0, 0, NULL);
4628     ok (hchild != 0, "Failed to create child window\n");
4629     ok_sequence(WmCreateChildSeq, "CreateWindow:child", FALSE);
4630     
4631     hchild2 = CreateWindowExA(0, "SimpleWindowClass", "Test child2", WS_CHILD,
4632                                100, 100, 50, 50, hparent, 0, 0, NULL);
4633     ok (hchild2 != 0, "Failed to create child2 window\n");
4634     flush_sequence();
4635
4636     hbutton = CreateWindowExA(0, "TestWindowClass", "Test button", WS_CHILD,
4637                               0, 100, 50, 50, hchild, 0, 0, NULL);
4638     ok (hbutton != 0, "Failed to create button window\n");
4639
4640     /* test WM_SETREDRAW on a not visible child window */
4641     test_WM_SETREDRAW(hchild);
4642
4643     ShowWindow(hchild, SW_SHOW);
4644     ok_sequence(WmShowChildSeq, "ShowWindow(SW_SHOW):child", FALSE);
4645
4646     /* check parent messages too */
4647     log_all_parent_messages++;
4648     ShowWindow(hchild, SW_HIDE);
4649     ok_sequence(WmHideChildSeq2, "ShowWindow(SW_HIDE):child", FALSE);
4650     log_all_parent_messages--;
4651
4652     ShowWindow(hchild, SW_SHOW);
4653     ok_sequence(WmShowChildSeq, "ShowWindow(SW_SHOW):child", FALSE);
4654
4655     ShowWindow(hchild, SW_HIDE);
4656     ok_sequence(WmHideChildSeq, "ShowWindow(SW_HIDE):child", FALSE);
4657
4658     ShowWindow(hchild, SW_SHOW);
4659     ok_sequence(WmShowChildSeq, "ShowWindow(SW_SHOW):child", FALSE);
4660
4661     /* test WM_SETREDRAW on a visible child window */
4662     test_WM_SETREDRAW(hchild);
4663
4664     log_all_parent_messages++;
4665     MoveWindow(hchild, 10, 10, 20, 20, TRUE);
4666     ok_sequence(WmResizingChildWithMoveWindowSeq, "MoveWindow:child", FALSE);
4667     log_all_parent_messages--;
4668
4669     ShowWindow(hchild, SW_HIDE);
4670     flush_sequence();
4671     SetWindowPos(hchild, 0,0,0,0,0, SWP_SHOWWINDOW|SWP_NOSIZE|SWP_NOMOVE);
4672     ok_sequence(WmShowChildSeq_2, "SetWindowPos:show_child_2", FALSE);
4673
4674     ShowWindow(hchild, SW_HIDE);
4675     flush_sequence();
4676     SetWindowPos(hchild, 0,0,0,0,0, SWP_SHOWWINDOW|SWP_NOSIZE|SWP_NOMOVE|SWP_NOACTIVATE);
4677     ok_sequence(WmShowChildSeq_3, "SetWindowPos:show_child_3", FALSE);
4678
4679     /* DestroyWindow sequence below expects that a child has focus */
4680     SetFocus(hchild);
4681     flush_sequence();
4682
4683     DestroyWindow(hchild);
4684     ok_sequence(WmDestroyChildSeq, "DestroyWindow:child", FALSE);
4685     DestroyWindow(hchild2);
4686     DestroyWindow(hbutton);
4687
4688     flush_sequence();
4689     hchild = CreateWindowExA(0, "TestWindowClass", "Test Child Popup", WS_CHILD | WS_POPUP,
4690                              0, 0, 100, 100, hparent, 0, 0, NULL);
4691     ok (hchild != 0, "Failed to create child popup window\n");
4692     ok_sequence(WmCreateChildPopupSeq, "CreateWindow:child_popup", FALSE);
4693     DestroyWindow(hchild);
4694
4695     /* test what happens to a window which sets WS_VISIBLE in WM_CREATE */
4696     flush_sequence();
4697     hchild = CreateWindowExA(0, "TestPopupClass", "Test Popup", WS_POPUP,
4698                              0, 0, 100, 100, hparent, 0, 0, NULL);
4699     ok (hchild != 0, "Failed to create popup window\n");
4700     ok_sequence(WmCreateInvisiblePopupSeq, "CreateWindow:invisible_popup", FALSE);
4701     ok(GetWindowLongA(hchild, GWL_STYLE) & WS_VISIBLE, "WS_VISIBLE should be set\n");
4702     ok(IsWindowVisible(hchild), "IsWindowVisible() should return TRUE\n");
4703     flush_sequence();
4704     ShowWindow(hchild, SW_SHOW);
4705     ok_sequence(WmEmptySeq, "ShowWindow:show_visible_popup", FALSE);
4706     flush_sequence();
4707     SetWindowPos(hchild, 0,0,0,0,0, SWP_SHOWWINDOW|SWP_NOSIZE|SWP_NOMOVE|SWP_NOACTIVATE|SWP_NOZORDER);
4708     ok_sequence(WmShowVisiblePopupSeq_2, "SetWindowPos:show_visible_popup_2", FALSE);
4709     flush_sequence();
4710     SetWindowPos(hchild, 0,0,0,0,0, SWP_SHOWWINDOW|SWP_NOSIZE|SWP_NOMOVE);
4711     ok_sequence(WmShowVisiblePopupSeq_3, "SetWindowPos:show_visible_popup_3", FALSE);
4712     DestroyWindow(hchild);
4713
4714     /* this time add WS_VISIBLE for CreateWindowEx, but this fact actually
4715      * changes nothing in message sequences.
4716      */
4717     flush_sequence();
4718     hchild = CreateWindowExA(0, "TestPopupClass", "Test Popup", WS_POPUP | WS_VISIBLE,
4719                              0, 0, 100, 100, hparent, 0, 0, NULL);
4720     ok (hchild != 0, "Failed to create popup window\n");
4721     ok_sequence(WmCreateInvisiblePopupSeq, "CreateWindow:invisible_popup", FALSE);
4722     ok(GetWindowLongA(hchild, GWL_STYLE) & WS_VISIBLE, "WS_VISIBLE should be set\n");
4723     ok(IsWindowVisible(hchild), "IsWindowVisible() should return TRUE\n");
4724     flush_sequence();
4725     ShowWindow(hchild, SW_SHOW);
4726     ok_sequence(WmEmptySeq, "ShowWindow:show_visible_popup", FALSE);
4727     flush_sequence();
4728     SetWindowPos(hchild, 0,0,0,0,0, SWP_SHOWWINDOW|SWP_NOSIZE|SWP_NOMOVE|SWP_NOACTIVATE|SWP_NOZORDER);
4729     ok_sequence(WmShowVisiblePopupSeq_2, "SetWindowPos:show_visible_popup_2", FALSE);
4730     DestroyWindow(hchild);
4731
4732     flush_sequence();
4733     hwnd = CreateWindowExA(WS_EX_DLGMODALFRAME, "TestDialogClass", NULL, WS_VISIBLE|WS_CAPTION|WS_SYSMENU|WS_DLGFRAME,
4734                            0, 0, 100, 100, hparent, 0, 0, NULL);
4735     ok(hwnd != 0, "Failed to create custom dialog window\n");
4736     ok_sequence(WmCreateCustomDialogSeq, "CreateCustomDialog", TRUE);
4737
4738     if(0) {
4739     trace("testing scroll APIs on a visible dialog %p\n", hwnd);
4740     test_scroll_messages(hwnd);
4741     }
4742
4743     flush_sequence();
4744
4745     test_def_id = 1;
4746     SendMessage(hwnd, WM_NULL, 0, 0);
4747
4748     flush_sequence();
4749     after_end_dialog = 1;
4750     EndDialog( hwnd, 0 );
4751     ok_sequence(WmEndCustomDialogSeq, "EndCustomDialog", FALSE);
4752
4753     DestroyWindow(hwnd);
4754     after_end_dialog = 0;
4755     test_def_id = 0;
4756
4757     hwnd = CreateWindowExA(0, "TestDialogClass", NULL, WS_POPUP,
4758                            0, 0, 100, 100, 0, 0, GetModuleHandleA(0), NULL);
4759     ok(hwnd != 0, "Failed to create custom dialog window\n");
4760     flush_sequence();
4761     trace("call ShowWindow(%p, SW_SHOW)\n", hwnd);
4762     ShowWindow(hwnd, SW_SHOW);
4763     ok_sequence(WmShowCustomDialogSeq, "ShowCustomDialog", TRUE);
4764     DestroyWindow(hwnd);
4765
4766     flush_sequence();
4767     DialogBoxA( 0, "TEST_DIALOG", hparent, TestModalDlgProcA );
4768     ok_sequence(WmModalDialogSeq, "ModalDialog", TRUE);
4769
4770     DestroyWindow(hparent);
4771     flush_sequence();
4772
4773     /* Message sequence for SetMenu */
4774     ok(!DrawMenuBar(hwnd), "DrawMenuBar should return FALSE for a window without a menu\n");
4775     ok_sequence(WmEmptySeq, "DrawMenuBar for a window without a menu", FALSE);
4776
4777     hmenu = CreateMenu();
4778     ok (hmenu != 0, "Failed to create menu\n");
4779     ok (InsertMenuA(hmenu, -1, MF_BYPOSITION, 0x1000, "foo"), "InsertMenu failed\n");
4780     hwnd = CreateWindowExA(0, "TestWindowClass", "Test overlapped", WS_OVERLAPPEDWINDOW,
4781                            100, 100, 200, 200, 0, hmenu, 0, NULL);
4782     ok_sequence(WmCreateOverlappedSeq, "CreateWindow:overlapped", FALSE);
4783     ok (SetMenu(hwnd, 0), "SetMenu\n");
4784     ok_sequence(WmSetMenuNonVisibleSizeChangeSeq, "SetMenu:NonVisibleSizeChange", FALSE);
4785     ok (SetMenu(hwnd, 0), "SetMenu\n");
4786     ok_sequence(WmSetMenuNonVisibleNoSizeChangeSeq, "SetMenu:NonVisibleNoSizeChange", FALSE);
4787     ShowWindow(hwnd, SW_SHOW);
4788     UpdateWindow( hwnd );
4789     flush_events();
4790     flush_sequence();
4791     ok (SetMenu(hwnd, 0), "SetMenu\n");
4792     ok_sequence(WmSetMenuVisibleNoSizeChangeSeq, "SetMenu:VisibleNoSizeChange", FALSE);
4793     ok (SetMenu(hwnd, hmenu), "SetMenu\n");
4794     ok_sequence(WmSetMenuVisibleSizeChangeSeq, "SetMenu:VisibleSizeChange", FALSE);
4795
4796     UpdateWindow( hwnd );
4797     flush_events();
4798     flush_sequence();
4799     ok(DrawMenuBar(hwnd), "DrawMenuBar\n");
4800     flush_events();
4801     ok_sequence(WmDrawMenuBarSeq, "DrawMenuBar", FALSE);
4802
4803     DestroyWindow(hwnd);
4804     flush_sequence();
4805
4806     /* Message sequence for EnableWindow */
4807     hparent = CreateWindowExA(0, "TestWindowClass", "Test parent", WS_OVERLAPPEDWINDOW | WS_VISIBLE,
4808                               100, 100, 200, 200, 0, 0, 0, NULL);
4809     ok (hparent != 0, "Failed to create parent window\n");
4810     hchild = CreateWindowExA(0, "TestWindowClass", "Test child", WS_CHILD | WS_VISIBLE,
4811                              0, 0, 10, 10, hparent, 0, 0, NULL);
4812     ok (hchild != 0, "Failed to create child window\n");
4813
4814     SetFocus(hchild);
4815     flush_events();
4816     flush_sequence();
4817
4818     EnableWindow(hparent, FALSE);
4819     ok_sequence(WmEnableWindowSeq_1, "EnableWindow(FALSE)", FALSE);
4820
4821     EnableWindow(hparent, TRUE);
4822     ok_sequence(WmEnableWindowSeq_2, "EnableWindow(TRUE)", FALSE);
4823
4824     flush_events();
4825     flush_sequence();
4826
4827     test_MsgWaitForMultipleObjects(hparent);
4828
4829     /* the following test causes an exception in user.exe under win9x */
4830     if (!PostMessageW( hparent, WM_USER, 0, 0 ))
4831     {
4832         DestroyWindow(hparent);
4833         flush_sequence();
4834         return;
4835     }
4836     PostMessageW( hparent, WM_USER+1, 0, 0 );
4837     /* PeekMessage(NULL) fails, but still removes the message */
4838     SetLastError(0xdeadbeef);
4839     ok( !PeekMessageW( NULL, 0, 0, 0, PM_REMOVE ), "PeekMessage(NULL) should fail\n" );
4840     ok( GetLastError() == ERROR_NOACCESS || /* Win2k */
4841         GetLastError() == 0xdeadbeef, /* NT4 */
4842         "last error is %d\n", GetLastError() );
4843     ok( PeekMessageW( &msg, 0, 0, 0, PM_REMOVE ), "PeekMessage should succeed\n" );
4844     ok( msg.message == WM_USER+1, "got %x instead of WM_USER+1\n", msg.message );
4845
4846     DestroyWindow(hchild);
4847     DestroyWindow(hparent);
4848     flush_sequence();
4849
4850     /* Message sequences for WM_SETICON */
4851     trace("testing WM_SETICON\n");
4852     hwnd = CreateWindowExA(0, "TestWindowClass", NULL, WS_OVERLAPPEDWINDOW,
4853                            CW_USEDEFAULT, CW_USEDEFAULT, 300, 300, 0,
4854                            NULL, NULL, 0);
4855     ShowWindow(hwnd, SW_SHOW);
4856     UpdateWindow(hwnd);
4857     flush_events();
4858     flush_sequence();
4859     SendMessage(hwnd, WM_SETICON, ICON_SMALL, (LPARAM)LoadIcon(0, IDI_APPLICATION));
4860     ok_sequence(WmSetIcon_1, "WM_SETICON for shown window with caption", FALSE);
4861
4862     ShowWindow(hwnd, SW_HIDE);
4863     flush_events();
4864     flush_sequence();
4865     SendMessage(hwnd, WM_SETICON, ICON_SMALL, (LPARAM)LoadIcon(0, IDI_APPLICATION));
4866     ok_sequence(WmSetIcon_2, "WM_SETICON for hidden window with caption", FALSE);
4867     DestroyWindow(hwnd);
4868     flush_sequence();
4869
4870     hwnd = CreateWindowExA(0, "TestPopupClass", NULL, WS_POPUP,
4871                            CW_USEDEFAULT, CW_USEDEFAULT, 300, 300, 0,
4872                            NULL, NULL, 0);
4873     ShowWindow(hwnd, SW_SHOW);
4874     UpdateWindow(hwnd);
4875     flush_events();
4876     flush_sequence();
4877     SendMessage(hwnd, WM_SETICON, ICON_SMALL, (LPARAM)LoadIcon(0, IDI_APPLICATION));
4878     ok_sequence(WmSetIcon_2, "WM_SETICON for shown window without caption", FALSE);
4879
4880     ShowWindow(hwnd, SW_HIDE);
4881     flush_events();
4882     flush_sequence();
4883     SendMessage(hwnd, WM_SETICON, ICON_SMALL, (LPARAM)LoadIcon(0, IDI_APPLICATION));
4884     ok_sequence(WmSetIcon_2, "WM_SETICON for hidden window without caption", FALSE);
4885
4886     flush_sequence();
4887     res = SendMessage(hwnd, 0x3B, 0x8000000b, 0);
4888     if (!res)
4889     {
4890         todo_wine win_skip( "Message 0x3b not supported\n" );
4891         goto done;
4892     }
4893     ok_sequence(WmInitEndSession, "Handling of undocumented 0x3B message by DefWindowProc wparam=0x8000000b", TRUE);
4894     ok(res == 1, "SendMessage(hwnd, 0x3B, 0x8000000b, 0) should have returned 1 instead of %ld\n", res);
4895     res = SendMessage(hwnd, 0x3B, 0x0000000b, 0);
4896     ok_sequence(WmInitEndSession_2, "Handling of undocumented 0x3B message by DefWindowProc wparam=0x0000000b", TRUE);
4897     ok(res == 1, "SendMessage(hwnd, 0x3B, 0x0000000b, 0) should have returned 1 instead of %ld\n", res);
4898     res = SendMessage(hwnd, 0x3B, 0x0000000f, 0);
4899     ok_sequence(WmInitEndSession_2, "Handling of undocumented 0x3B message by DefWindowProc wparam=0x0000000f", TRUE);
4900     ok(res == 1, "SendMessage(hwnd, 0x3B, 0x0000000f, 0) should have returned 1 instead of %ld\n", res);
4901
4902     flush_sequence();
4903     res = SendMessage(hwnd, 0x3B, 0x80000008, 0);
4904     ok_sequence(WmInitEndSession_3, "Handling of undocumented 0x3B message by DefWindowProc wparam=0x80000008", TRUE);
4905     ok(res == 2, "SendMessage(hwnd, 0x3B, 0x80000008, 0) should have returned 2 instead of %ld\n", res);
4906     res = SendMessage(hwnd, 0x3B, 0x00000008, 0);
4907     ok_sequence(WmInitEndSession_4, "Handling of undocumented 0x3B message by DefWindowProc wparam=0x00000008", TRUE);
4908     ok(res == 2, "SendMessage(hwnd, 0x3B, 0x00000008, 0) should have returned 2 instead of %ld\n", res);
4909
4910     res = SendMessage(hwnd, 0x3B, 0x80000004, 0);
4911     ok_sequence(WmInitEndSession_3, "Handling of undocumented 0x3B message by DefWindowProc wparam=0x80000004", TRUE);
4912     ok(res == 2, "SendMessage(hwnd, 0x3B, 0x80000004, 0) should have returned 2 instead of %ld\n", res);
4913
4914     res = SendMessage(hwnd, 0x3B, 0x80000001, 0);
4915     ok_sequence(WmInitEndSession_5, "Handling of undocumented 0x3B message by DefWindowProc wparam=0x80000001", TRUE);
4916     ok(res == 2, "SendMessage(hwnd, 0x3B, 0x80000001, 0) should have returned 2 instead of %ld\n", res);
4917
4918 done:
4919     DestroyWindow(hwnd);
4920     flush_sequence();
4921 }
4922
4923 static void test_setwindowpos(void)
4924 {
4925     HWND hwnd;
4926     RECT rc;
4927     LRESULT res;
4928     const INT winX = 100;
4929     const INT winY = 100;
4930     const INT sysX = GetSystemMetrics(SM_CXMINTRACK);
4931
4932     hwnd = CreateWindowExA(0, "TestWindowClass", NULL, 0,
4933                            0, 0, winX, winY, 0,
4934                            NULL, NULL, 0);
4935
4936     GetWindowRect(hwnd, &rc);
4937     expect(sysX, rc.right);
4938     expect(winY, rc.bottom);
4939
4940     flush_events();
4941     flush_sequence();
4942     res = SetWindowPos(hwnd, HWND_TOPMOST, 0, 0, winX, winY, 0);
4943     ok_sequence(WmZOrder, "Z-Order", TRUE);
4944     ok(res == TRUE, "SetWindowPos expected TRUE, got %ld\n", res);
4945
4946     GetWindowRect(hwnd, &rc);
4947     expect(sysX, rc.right);
4948     expect(winY, rc.bottom);
4949     DestroyWindow(hwnd);
4950 }
4951
4952 static void invisible_parent_tests(void)
4953 {
4954     HWND hparent, hchild;
4955
4956     hparent = CreateWindowExA(0, "TestParentClass", "Test parent", WS_OVERLAPPEDWINDOW,
4957                               100, 100, 200, 200, 0, 0, 0, NULL);
4958     ok (hparent != 0, "Failed to create parent window\n");
4959     flush_sequence();
4960
4961     /* test showing child with hidden parent */
4962
4963     hchild = CreateWindowExA(0, "TestWindowClass", "Test child", WS_CHILD,
4964                              0, 0, 10, 10, hparent, 0, 0, NULL);
4965     ok (hchild != 0, "Failed to create child window\n");
4966     ok_sequence(WmCreateChildSeq, "CreateWindow:child", FALSE);
4967
4968     ShowWindow( hchild, SW_MINIMIZE );
4969     ok_sequence(WmShowChildInvisibleParentSeq_1, "ShowWindow(SW_MINIMIZE) child with invisible parent", FALSE);
4970     ok(GetWindowLongA(hchild, GWL_STYLE) & WS_VISIBLE, "WS_VISIBLE should be set\n");
4971     ok(!IsWindowVisible(hchild), "IsWindowVisible() should return FALSE\n");
4972
4973     /* repeat */
4974     flush_events();
4975     flush_sequence();
4976     ShowWindow( hchild, SW_MINIMIZE );
4977     ok_sequence(WmShowChildInvisibleParentSeq_1r, "ShowWindow(SW_MINIMIZE) child with invisible parent", FALSE);
4978
4979     DestroyWindow(hchild);
4980     hchild = CreateWindowExA(0, "TestWindowClass", "Test child", WS_CHILD,
4981                              0, 0, 10, 10, hparent, 0, 0, NULL);
4982     flush_sequence();
4983
4984     ShowWindow( hchild, SW_MAXIMIZE );
4985     ok_sequence(WmShowChildInvisibleParentSeq_2, "ShowWindow(SW_MAXIMIZE) child with invisible parent", FALSE);
4986     ok(GetWindowLongA(hchild, GWL_STYLE) & WS_VISIBLE, "WS_VISIBLE should be set\n");
4987     ok(!IsWindowVisible(hchild), "IsWindowVisible() should return FALSE\n");
4988
4989     /* repeat */
4990     flush_events();
4991     flush_sequence();
4992     ShowWindow( hchild, SW_MAXIMIZE );
4993     ok_sequence(WmShowChildInvisibleParentSeq_2r, "ShowWindow(SW_MAXIMIZE) child with invisible parent", FALSE);
4994
4995     DestroyWindow(hchild);
4996     hchild = CreateWindowExA(0, "TestWindowClass", "Test child", WS_CHILD,
4997                              0, 0, 10, 10, hparent, 0, 0, NULL);
4998     flush_sequence();
4999
5000     ShowWindow( hchild, SW_RESTORE );
5001     ok_sequence(WmShowChildInvisibleParentSeq_5, "ShowWindow(SW_RESTORE) child with invisible parent", FALSE);
5002     ok(GetWindowLongA(hchild, GWL_STYLE) & WS_VISIBLE, "WS_VISIBLE should be set\n");
5003     ok(!IsWindowVisible(hchild), "IsWindowVisible() should return FALSE\n");
5004
5005     DestroyWindow(hchild);
5006     hchild = CreateWindowExA(0, "TestWindowClass", "Test child", WS_CHILD,
5007                              0, 0, 10, 10, hparent, 0, 0, NULL);
5008     flush_sequence();
5009
5010     ShowWindow( hchild, SW_SHOWMINIMIZED );
5011     ok_sequence(WmShowChildInvisibleParentSeq_3, "ShowWindow(SW_SHOWMINIMIZED) child with invisible parent", FALSE);
5012     ok(GetWindowLongA(hchild, GWL_STYLE) & WS_VISIBLE, "WS_VISIBLE should be set\n");
5013     ok(!IsWindowVisible(hchild), "IsWindowVisible() should return FALSE\n");
5014
5015     /* repeat */
5016     flush_events();
5017     flush_sequence();
5018     ShowWindow( hchild, SW_SHOWMINIMIZED );
5019     ok_sequence(WmShowChildInvisibleParentSeq_3r, "ShowWindow(SW_SHOWMINIMIZED) child with invisible parent", FALSE);
5020
5021     DestroyWindow(hchild);
5022     hchild = CreateWindowExA(0, "TestWindowClass", "Test child", WS_CHILD,
5023                              0, 0, 10, 10, hparent, 0, 0, NULL);
5024     flush_sequence();
5025
5026     /* same as ShowWindow( hchild, SW_MAXIMIZE ); */
5027     ShowWindow( hchild, SW_SHOWMAXIMIZED );
5028     ok_sequence(WmShowChildInvisibleParentSeq_2, "ShowWindow(SW_SHOWMAXIMIZED) child with invisible parent", FALSE);
5029     ok(GetWindowLongA(hchild, GWL_STYLE) & WS_VISIBLE, "WS_VISIBLE should be set\n");
5030     ok(!IsWindowVisible(hchild), "IsWindowVisible() should return FALSE\n");
5031
5032     DestroyWindow(hchild);
5033     hchild = CreateWindowExA(0, "TestWindowClass", "Test child", WS_CHILD,
5034                              0, 0, 10, 10, hparent, 0, 0, NULL);
5035     flush_sequence();
5036
5037     ShowWindow( hchild, SW_SHOWMINNOACTIVE );
5038     ok_sequence(WmShowChildInvisibleParentSeq_4, "ShowWindow(SW_SHOWMINNOACTIVE) child with invisible parent", FALSE);
5039     ok(GetWindowLongA(hchild, GWL_STYLE) & WS_VISIBLE, "WS_VISIBLE should be set\n");
5040     ok(!IsWindowVisible(hchild), "IsWindowVisible() should return FALSE\n");
5041
5042     /* repeat */
5043     flush_events();
5044     flush_sequence();
5045     ShowWindow( hchild, SW_SHOWMINNOACTIVE );
5046     ok_sequence(WmShowChildInvisibleParentSeq_4r, "ShowWindow(SW_SHOWMINNOACTIVE) child with invisible parent", FALSE);
5047
5048     DestroyWindow(hchild);
5049     hchild = CreateWindowExA(0, "TestWindowClass", "Test child", WS_CHILD,
5050                              0, 0, 10, 10, hparent, 0, 0, NULL);
5051     flush_sequence();
5052
5053     /* FIXME: looks like XP SP2 doesn't know about SW_FORCEMINIMIZE at all */
5054     ShowWindow( hchild, SW_FORCEMINIMIZE );
5055     ok_sequence(WmEmptySeq, "ShowWindow(SW_FORCEMINIMIZE) child with invisible parent", TRUE);
5056 todo_wine {
5057     ok(!(GetWindowLongA(hchild, GWL_STYLE) & WS_VISIBLE), "WS_VISIBLE should be not set\n");
5058 }
5059     ok(!IsWindowVisible(hchild), "IsWindowVisible() should return FALSE\n");
5060
5061     DestroyWindow(hchild);
5062     hchild = CreateWindowExA(0, "TestWindowClass", "Test child", WS_CHILD,
5063                              0, 0, 10, 10, hparent, 0, 0, NULL);
5064     flush_sequence();
5065
5066     ShowWindow( hchild, SW_SHOWNA );
5067     ok_sequence(WmShowChildInvisibleParentSeq_5, "ShowWindow(SW_SHOWNA) child with invisible parent", FALSE);
5068     ok(GetWindowLongA(hchild, GWL_STYLE) & WS_VISIBLE, "WS_VISIBLE should be set\n");
5069     ok(!IsWindowVisible(hchild), "IsWindowVisible() should return FALSE\n");
5070
5071     /* repeat */
5072     flush_events();
5073     flush_sequence();
5074     ShowWindow( hchild, SW_SHOWNA );
5075     ok_sequence(WmShowChildInvisibleParentSeq_5, "ShowWindow(SW_SHOWNA) child with invisible parent", FALSE);
5076
5077     DestroyWindow(hchild);
5078     hchild = CreateWindowExA(0, "TestWindowClass", "Test child", WS_CHILD,
5079                              0, 0, 10, 10, hparent, 0, 0, NULL);
5080     flush_sequence();
5081
5082     ShowWindow( hchild, SW_SHOW );
5083     ok_sequence(WmShowChildInvisibleParentSeq_5, "ShowWindow(SW_SHOW) child with invisible parent", FALSE);
5084     ok(GetWindowLongA(hchild, GWL_STYLE) & WS_VISIBLE, "WS_VISIBLE should be set\n");
5085     ok(!IsWindowVisible(hchild), "IsWindowVisible() should return FALSE\n");
5086
5087     /* repeat */
5088     flush_events();
5089     flush_sequence();
5090     ShowWindow( hchild, SW_SHOW );
5091     ok_sequence(WmEmptySeq, "ShowWindow(SW_SHOW) child with invisible parent", FALSE);
5092
5093     ShowWindow( hchild, SW_HIDE );
5094     ok_sequence(WmHideChildInvisibleParentSeq, "ShowWindow:hide child with invisible parent", FALSE);
5095     ok(!(GetWindowLongA(hchild, GWL_STYLE) & WS_VISIBLE), "WS_VISIBLE should be not set\n");
5096     ok(!IsWindowVisible(hchild), "IsWindowVisible() should return FALSE\n");
5097
5098     SetWindowPos(hchild, 0,0,0,0,0, SWP_SHOWWINDOW|SWP_NOSIZE|SWP_NOMOVE|SWP_NOACTIVATE|SWP_NOZORDER);
5099     ok_sequence(WmShowChildInvisibleParentSeq_6, "SetWindowPos:show child with invisible parent", FALSE);
5100     ok(GetWindowLongA(hchild, GWL_STYLE) & WS_VISIBLE, "WS_VISIBLE should be set\n");
5101     ok(!IsWindowVisible(hchild), "IsWindowVisible() should return FALSE\n");
5102
5103     SetWindowPos(hchild, 0,0,0,0,0, SWP_HIDEWINDOW|SWP_NOSIZE|SWP_NOMOVE|SWP_NOACTIVATE|SWP_NOZORDER);
5104     ok_sequence(WmHideChildInvisibleParentSeq_2, "SetWindowPos:hide child with invisible parent", FALSE);
5105     ok(!(GetWindowLongA(hchild, GWL_STYLE) & WS_VISIBLE), "WS_VISIBLE should not be set\n");
5106     ok(!IsWindowVisible(hchild), "IsWindowVisible() should return FALSE\n");
5107
5108     SetWindowPos(hchild, 0,0,0,0,0, SWP_SHOWWINDOW|SWP_NOSIZE|SWP_NOMOVE|SWP_NOACTIVATE|SWP_NOZORDER);
5109     flush_sequence();
5110     DestroyWindow(hchild);
5111     ok_sequence(WmDestroyInvisibleChildSeq, "DestroyInvisibleChildSeq", FALSE);
5112
5113     DestroyWindow(hparent);
5114     flush_sequence();
5115 }
5116
5117 /****************** button message test *************************/
5118 #define ID_BUTTON 0x000e
5119
5120 static const struct message WmSetFocusButtonSeq[] =
5121 {
5122     { HCBT_SETFOCUS, hook },
5123     { WM_IME_SETCONTEXT, sent|wparam|optional, 1 },
5124     { WM_IME_NOTIFY, sent|wparam|defwinproc|optional, 2 },
5125     { EVENT_OBJECT_FOCUS, winevent_hook|wparam|lparam, OBJID_CLIENT, 0 },
5126     { WM_SETFOCUS, sent|wparam, 0 },
5127     { WM_CTLCOLORBTN, sent|parent },
5128     { WM_COMMAND, sent|wparam|parent, MAKEWPARAM(ID_BUTTON, BN_SETFOCUS) },
5129     { WM_APP, sent|wparam|lparam, 0, 0 },
5130     { 0 }
5131 };
5132 static const struct message WmKillFocusButtonSeq[] =
5133 {
5134     { HCBT_SETFOCUS, hook },
5135     { EVENT_OBJECT_FOCUS, winevent_hook|wparam|lparam, OBJID_CLIENT, 0 },
5136     { WM_KILLFOCUS, sent|wparam, 0 },
5137     { WM_CTLCOLORBTN, sent|parent },
5138     { WM_COMMAND, sent|wparam|parent, MAKEWPARAM(ID_BUTTON, BN_KILLFOCUS) },
5139     { WM_IME_SETCONTEXT, sent|wparam|optional, 0 },
5140     { WM_IME_NOTIFY, sent|wparam|defwinproc|optional, 1 },
5141     { WM_APP, sent|wparam|lparam, 0, 0 },
5142     { WM_PAINT, sent },
5143     { WM_CTLCOLORBTN, sent|parent },
5144     { 0 }
5145 };
5146 static const struct message WmSetFocusStaticSeq[] =
5147 {
5148     { HCBT_SETFOCUS, hook },
5149     { WM_IME_SETCONTEXT, sent|wparam|optional, 1 },
5150     { WM_IME_NOTIFY, sent|wparam|defwinproc|optional, 2 },
5151     { EVENT_OBJECT_FOCUS, winevent_hook|wparam|lparam, OBJID_CLIENT, 0 },
5152     { WM_SETFOCUS, sent|wparam, 0 },
5153     { WM_CTLCOLORSTATIC, sent|parent },
5154     { WM_COMMAND, sent|wparam|parent, MAKEWPARAM(ID_BUTTON, BN_SETFOCUS) },
5155     { WM_COMMAND, sent|wparam|parent|optional, MAKEWPARAM(ID_BUTTON, BN_CLICKED) }, /* radio button */
5156     { WM_APP, sent|wparam|lparam, 0, 0 },
5157     { 0 }
5158 };
5159 static const struct message WmKillFocusStaticSeq[] =
5160 {
5161     { HCBT_SETFOCUS, hook },
5162     { EVENT_OBJECT_FOCUS, winevent_hook|wparam|lparam, OBJID_CLIENT, 0 },
5163     { WM_KILLFOCUS, sent|wparam, 0 },
5164     { WM_CTLCOLORSTATIC, sent|parent },
5165     { WM_COMMAND, sent|wparam|parent, MAKEWPARAM(ID_BUTTON, BN_KILLFOCUS) },
5166     { WM_IME_SETCONTEXT, sent|wparam|optional, 0 },
5167     { WM_IME_NOTIFY, sent|wparam|defwinproc|optional, 1 },
5168     { WM_APP, sent|wparam|lparam, 0, 0 },
5169     { WM_PAINT, sent },
5170     { WM_CTLCOLORSTATIC, sent|parent },
5171     { 0 }
5172 };
5173 static const struct message WmSetFocusOwnerdrawSeq[] =
5174 {
5175     { HCBT_SETFOCUS, hook },
5176     { WM_IME_SETCONTEXT, sent|wparam|optional, 1 },
5177     { WM_IME_NOTIFY, sent|wparam|defwinproc|optional, 2 },
5178     { EVENT_OBJECT_FOCUS, winevent_hook|wparam|lparam, OBJID_CLIENT, 0 },
5179     { WM_SETFOCUS, sent|wparam, 0 },
5180     { WM_CTLCOLORBTN, sent|parent },
5181     { WM_DRAWITEM, sent|wparam|lparam|parent, ID_BUTTON, 0x001040e4 },
5182     { WM_COMMAND, sent|wparam|parent, MAKEWPARAM(ID_BUTTON, BN_SETFOCUS) },
5183     { WM_APP, sent|wparam|lparam, 0, 0 },
5184     { 0 }
5185 };
5186 static const struct message WmKillFocusOwnerdrawSeq[] =
5187 {
5188     { HCBT_SETFOCUS, hook },
5189     { EVENT_OBJECT_FOCUS, winevent_hook|wparam|lparam, OBJID_CLIENT, 0 },
5190     { WM_KILLFOCUS, sent|wparam, 0 },
5191     { WM_CTLCOLORBTN, sent|parent },
5192     { WM_DRAWITEM, sent|wparam|lparam|parent, ID_BUTTON, 0x000040e4 },
5193     { WM_COMMAND, sent|wparam|parent, MAKEWPARAM(ID_BUTTON, BN_KILLFOCUS) },
5194     { WM_IME_SETCONTEXT, sent|wparam|optional, 0 },
5195     { WM_IME_NOTIFY, sent|wparam|defwinproc|optional, 1 },
5196     { WM_APP, sent|wparam|lparam, 0, 0 },
5197     { WM_PAINT, sent },
5198     { WM_CTLCOLORBTN, sent|parent },
5199     { WM_DRAWITEM, sent|wparam|lparam|parent, ID_BUTTON, 0x000010e4 },
5200     { 0 }
5201 };
5202 static const struct message WmLButtonDownSeq[] =
5203 {
5204     { WM_LBUTTONDOWN, sent|wparam|lparam, 0, 0 },
5205     { EVENT_SYSTEM_CAPTURESTART, winevent_hook|wparam|lparam, 0, 0 },
5206     { HCBT_SETFOCUS, hook },
5207     { WM_IME_SETCONTEXT, sent|wparam|defwinproc|optional, 1 },
5208     { WM_IME_NOTIFY, sent|wparam|defwinproc|optional, 2 },
5209     { EVENT_OBJECT_FOCUS, winevent_hook|wparam|lparam, OBJID_CLIENT, 0 },
5210     { WM_SETFOCUS, sent|wparam|defwinproc, 0 },
5211     { WM_CTLCOLORBTN, sent|defwinproc },
5212     { BM_SETSTATE, sent|wparam|defwinproc, TRUE },
5213     { WM_CTLCOLORBTN, sent|defwinproc },
5214     { EVENT_OBJECT_STATECHANGE, winevent_hook|wparam|lparam, OBJID_CLIENT, 0 },
5215     { 0 }
5216 };
5217 static const struct message WmLButtonUpSeq[] =
5218 {
5219     { WM_LBUTTONUP, sent|wparam|lparam, 0, 0 },
5220     { BM_SETSTATE, sent|wparam|defwinproc, FALSE },
5221     { WM_CTLCOLORBTN, sent|defwinproc },
5222     { EVENT_OBJECT_STATECHANGE, winevent_hook|wparam|lparam, OBJID_CLIENT, 0 },
5223     { EVENT_SYSTEM_CAPTUREEND, winevent_hook|wparam|lparam, 0, 0 },
5224     { WM_CAPTURECHANGED, sent|wparam|defwinproc, 0 },
5225     { 0 }
5226 };
5227 static const struct message WmSetFontButtonSeq[] =
5228 {
5229     { WM_SETFONT, sent },
5230     { WM_PAINT, sent },
5231     { WM_ERASEBKGND, sent|defwinproc|optional },
5232     { WM_CTLCOLORBTN, sent|defwinproc },
5233     { 0 }
5234 };
5235 static const struct message WmSetStyleButtonSeq[] =
5236 {
5237     { BM_SETSTYLE, sent },
5238     { WM_APP, sent|wparam|lparam, 0, 0 },
5239     { WM_PAINT, sent },
5240     { WM_NCPAINT, sent|defwinproc|optional }, /* FIXME: Wine sends it */
5241     { WM_ERASEBKGND, sent|defwinproc|optional }, /* Win9x doesn't send it */
5242     { WM_CTLCOLORBTN, sent|parent },
5243     { 0 }
5244 };
5245 static const struct message WmSetStyleStaticSeq[] =
5246 {
5247     { BM_SETSTYLE, sent },
5248     { WM_APP, sent|wparam|lparam, 0, 0 },
5249     { WM_PAINT, sent },
5250     { WM_NCPAINT, sent|defwinproc|optional }, /* FIXME: Wine sends it */
5251     { WM_ERASEBKGND, sent|defwinproc|optional }, /* Win9x doesn't send it */
5252     { WM_CTLCOLORSTATIC, sent|parent },
5253     { 0 }
5254 };
5255 static const struct message WmSetStyleUserSeq[] =
5256 {
5257     { BM_SETSTYLE, sent },
5258     { WM_APP, sent|wparam|lparam, 0, 0 },
5259     { WM_PAINT, sent },
5260     { WM_NCPAINT, sent|defwinproc|optional }, /* FIXME: Wine sends it */
5261     { WM_ERASEBKGND, sent|defwinproc|optional }, /* Win9x doesn't send it */
5262     { WM_CTLCOLORBTN, sent|parent },
5263     { WM_COMMAND, sent|wparam|parent, MAKEWPARAM(ID_BUTTON, BN_PAINT) },
5264     { 0 }
5265 };
5266 static const struct message WmSetStyleOwnerdrawSeq[] =
5267 {
5268     { BM_SETSTYLE, sent },
5269     { WM_APP, sent|wparam|lparam, 0, 0 },
5270     { WM_PAINT, sent },
5271     { WM_NCPAINT, sent|optional }, /* FIXME: Wine sends it */
5272     { WM_ERASEBKGND, sent|defwinproc|optional }, /* Win9x doesn't send it */
5273     { WM_CTLCOLORBTN, sent|parent },
5274     { WM_CTLCOLORBTN, sent|parent|optional }, /* Win9x doesn't send it */
5275     { WM_DRAWITEM, sent|wparam|lparam|parent, ID_BUTTON, 0x000010e4 },
5276     { 0 }
5277 };
5278 static const struct message WmSetStateButtonSeq[] =
5279 {
5280     { BM_SETSTATE, sent },
5281     { WM_CTLCOLORBTN, sent|parent },
5282     { WM_APP, sent|wparam|lparam, 0, 0 },
5283     { 0 }
5284 };
5285 static const struct message WmSetStateStaticSeq[] =
5286 {
5287     { BM_SETSTATE, sent },
5288     { WM_CTLCOLORSTATIC, sent|parent },
5289     { WM_APP, sent|wparam|lparam, 0, 0 },
5290     { 0 }
5291 };
5292 static const struct message WmSetStateUserSeq[] =
5293 {
5294     { BM_SETSTATE, sent },
5295     { WM_CTLCOLORBTN, sent|parent },
5296     { WM_COMMAND, sent|wparam|parent, MAKEWPARAM(ID_BUTTON, BN_HILITE) },
5297     { WM_APP, sent|wparam|lparam, 0, 0 },
5298     { 0 }
5299 };
5300 static const struct message WmSetStateOwnerdrawSeq[] =
5301 {
5302     { BM_SETSTATE, sent },
5303     { WM_CTLCOLORBTN, sent|parent },
5304     { WM_DRAWITEM, sent|wparam|lparam|parent, ID_BUTTON, 0x000120e4 },
5305     { WM_APP, sent|wparam|lparam, 0, 0 },
5306     { 0 }
5307 };
5308 static const struct message WmClearStateButtonSeq[] =
5309 {
5310     { BM_SETSTATE, sent },
5311     { WM_CTLCOLORBTN, sent|parent },
5312     { WM_COMMAND, sent|wparam|parent, MAKEWPARAM(ID_BUTTON, BN_UNHILITE) },
5313     { WM_APP, sent|wparam|lparam, 0, 0 },
5314     { 0 }
5315 };
5316 static const struct message WmClearStateOwnerdrawSeq[] =
5317 {
5318     { BM_SETSTATE, sent },
5319     { WM_CTLCOLORBTN, sent|parent },
5320     { WM_DRAWITEM, sent|wparam|lparam|parent, ID_BUTTON, 0x000020e4 },
5321     { WM_APP, sent|wparam|lparam, 0, 0 },
5322     { 0 }
5323 };
5324 static const struct message WmSetCheckIgnoredSeq[] =
5325 {
5326     { BM_SETCHECK, sent },
5327     { WM_APP, sent|wparam|lparam, 0, 0 },
5328     { 0 }
5329 };
5330 static const struct message WmSetCheckStaticSeq[] =
5331 {
5332     { BM_SETCHECK, sent },
5333     { WM_CTLCOLORSTATIC, sent|parent },
5334     { WM_APP, sent|wparam|lparam, 0, 0 },
5335     { 0 }
5336 };
5337
5338 static WNDPROC old_button_proc;
5339
5340 static LRESULT CALLBACK button_hook_proc(HWND hwnd, UINT message, WPARAM wParam, LPARAM lParam)
5341 {
5342     static LONG defwndproc_counter = 0;
5343     LRESULT ret;
5344     struct recvd_message msg;
5345
5346     if (ignore_message( message )) return 0;
5347
5348     switch (message)
5349     {
5350     case WM_SYNCPAINT:
5351         break;
5352     case BM_SETSTATE:
5353         if (GetCapture())
5354             ok(GetCapture() == hwnd, "GetCapture() = %p\n", GetCapture());
5355         /* fall through */
5356     default:
5357         msg.hwnd = hwnd;
5358         msg.message = message;
5359         msg.flags = sent|wparam|lparam;
5360         if (defwndproc_counter) msg.flags |= defwinproc;
5361         msg.wParam = wParam;
5362         msg.lParam = lParam;
5363         msg.descr = "button";
5364         add_message(&msg);
5365     }
5366
5367     defwndproc_counter++;
5368     ret = CallWindowProcA(old_button_proc, hwnd, message, wParam, lParam);
5369     defwndproc_counter--;
5370
5371     return ret;
5372 }
5373
5374 static void subclass_button(void)
5375 {
5376     WNDCLASSA cls;
5377
5378     if (!GetClassInfoA(0, "button", &cls)) assert(0);
5379
5380     old_button_proc = cls.lpfnWndProc;
5381
5382     cls.hInstance = GetModuleHandle(0);
5383     cls.lpfnWndProc = button_hook_proc;
5384     cls.lpszClassName = "my_button_class";
5385     UnregisterClass(cls.lpszClassName, cls.hInstance);
5386     if (!RegisterClassA(&cls)) assert(0);
5387 }
5388
5389 static void test_button_messages(void)
5390 {
5391     static const struct
5392     {
5393         DWORD style;
5394         DWORD dlg_code;
5395         const struct message *setfocus;
5396         const struct message *killfocus;
5397         const struct message *setstyle;
5398         const struct message *setstate;
5399         const struct message *clearstate;
5400         const struct message *setcheck;
5401     } button[] = {
5402         { BS_PUSHBUTTON, DLGC_BUTTON | DLGC_UNDEFPUSHBUTTON,
5403           WmSetFocusButtonSeq, WmKillFocusButtonSeq, WmSetStyleButtonSeq,
5404           WmSetStateButtonSeq, WmSetStateButtonSeq, WmSetCheckIgnoredSeq },
5405         { BS_DEFPUSHBUTTON, DLGC_BUTTON | DLGC_DEFPUSHBUTTON,
5406           WmSetFocusButtonSeq, WmKillFocusButtonSeq, WmSetStyleButtonSeq,
5407           WmSetStateButtonSeq, WmSetStateButtonSeq, WmSetCheckIgnoredSeq },
5408         { BS_CHECKBOX, DLGC_BUTTON,
5409           WmSetFocusStaticSeq, WmKillFocusStaticSeq, WmSetStyleStaticSeq,
5410           WmSetStateStaticSeq, WmSetStateStaticSeq, WmSetCheckStaticSeq },
5411         { BS_AUTOCHECKBOX, DLGC_BUTTON,
5412           WmSetFocusStaticSeq, WmKillFocusStaticSeq, WmSetStyleStaticSeq,
5413           WmSetStateStaticSeq, WmSetStateStaticSeq, WmSetCheckStaticSeq },
5414         { BS_RADIOBUTTON, DLGC_BUTTON | DLGC_RADIOBUTTON,
5415           WmSetFocusStaticSeq, WmKillFocusStaticSeq, WmSetStyleStaticSeq,
5416           WmSetStateStaticSeq, WmSetStateStaticSeq, WmSetCheckStaticSeq },
5417         { BS_3STATE, DLGC_BUTTON,
5418           WmSetFocusStaticSeq, WmKillFocusStaticSeq, WmSetStyleStaticSeq,
5419           WmSetStateStaticSeq, WmSetStateStaticSeq, WmSetCheckStaticSeq },
5420         { BS_AUTO3STATE, DLGC_BUTTON,
5421           WmSetFocusStaticSeq, WmKillFocusStaticSeq, WmSetStyleStaticSeq,
5422           WmSetStateStaticSeq, WmSetStateStaticSeq, WmSetCheckStaticSeq },
5423         { BS_GROUPBOX, DLGC_STATIC,
5424           WmSetFocusStaticSeq, WmKillFocusStaticSeq, WmSetStyleStaticSeq,
5425           WmSetStateStaticSeq, WmSetStateStaticSeq, WmSetCheckIgnoredSeq },
5426         { BS_USERBUTTON, DLGC_BUTTON | DLGC_UNDEFPUSHBUTTON,
5427           WmSetFocusButtonSeq, WmKillFocusButtonSeq, WmSetStyleUserSeq,
5428           WmSetStateUserSeq, WmClearStateButtonSeq, WmSetCheckIgnoredSeq },
5429         { BS_AUTORADIOBUTTON, DLGC_BUTTON | DLGC_RADIOBUTTON,
5430           WmSetFocusStaticSeq, WmKillFocusStaticSeq, WmSetStyleStaticSeq,
5431           WmSetStateStaticSeq, WmSetStateStaticSeq, WmSetCheckStaticSeq },
5432         { BS_OWNERDRAW, DLGC_BUTTON,
5433           WmSetFocusOwnerdrawSeq, WmKillFocusOwnerdrawSeq, WmSetStyleOwnerdrawSeq,
5434           WmSetStateOwnerdrawSeq, WmClearStateOwnerdrawSeq, WmSetCheckIgnoredSeq },
5435     };
5436     unsigned int i;
5437     HWND hwnd, parent;
5438     DWORD dlg_code;
5439     HFONT zfont;
5440
5441     /* selection with VK_SPACE should capture button window */
5442     hwnd = CreateWindowExA(0, "button", "test", BS_CHECKBOX | WS_VISIBLE | WS_POPUP,
5443                            0, 0, 50, 14, 0, 0, 0, NULL);
5444     ok(hwnd != 0, "Failed to create button window\n");
5445     ReleaseCapture();
5446     SetFocus(hwnd);
5447     SendMessageA(hwnd, WM_KEYDOWN, VK_SPACE, 0);
5448     ok(GetCapture() == hwnd, "Should be captured on VK_SPACE WM_KEYDOWN\n");
5449     SendMessageA(hwnd, WM_KEYUP, VK_SPACE, 0);
5450     DestroyWindow(hwnd);
5451
5452     subclass_button();
5453
5454     parent = CreateWindowExA(0, "TestParentClass", "Test parent", WS_OVERLAPPEDWINDOW | WS_VISIBLE,
5455                              100, 100, 200, 200, 0, 0, 0, NULL);
5456     ok(parent != 0, "Failed to create parent window\n");
5457
5458     for (i = 0; i < sizeof(button)/sizeof(button[0]); i++)
5459     {
5460         MSG msg;
5461         DWORD style, state;
5462
5463         trace("button style %08x\n", button[i].style);
5464
5465         hwnd = CreateWindowExA(0, "my_button_class", "test", button[i].style | WS_CHILD | BS_NOTIFY,
5466                                0, 0, 50, 14, parent, (HMENU)ID_BUTTON, 0, NULL);
5467         ok(hwnd != 0, "Failed to create button window\n");
5468
5469         style = GetWindowLongA(hwnd, GWL_STYLE);
5470         style &= ~(WS_CHILD | BS_NOTIFY);
5471         /* XP turns a BS_USERBUTTON into BS_PUSHBUTTON */
5472         if (button[i].style == BS_USERBUTTON)
5473             ok(style == BS_PUSHBUTTON, "expected style BS_PUSHBUTTON got %x\n", style);
5474         else
5475             ok(style == button[i].style, "expected style %x got %x\n", button[i].style, style);
5476
5477         dlg_code = SendMessageA(hwnd, WM_GETDLGCODE, 0, 0);
5478         ok(dlg_code == button[i].dlg_code, "%u: wrong dlg_code %08x\n", i, dlg_code);
5479
5480         ShowWindow(hwnd, SW_SHOW);
5481         UpdateWindow(hwnd);
5482         SetFocus(0);
5483         flush_events();
5484         SetFocus(0);
5485         flush_sequence();
5486
5487         log_all_parent_messages++;
5488
5489         ok(GetFocus() == 0, "expected focus 0, got %p\n", GetFocus());
5490         SetFocus(hwnd);
5491         SendMessage(hwnd, WM_APP, 0, 0); /* place a separator mark here */
5492         while (PeekMessage(&msg, 0, 0, 0, PM_REMOVE)) DispatchMessage(&msg);
5493         ok_sequence(button[i].setfocus, "SetFocus(hwnd) on a button", FALSE);
5494
5495         SetFocus(0);
5496         SendMessage(hwnd, WM_APP, 0, 0); /* place a separator mark here */
5497         while (PeekMessage(&msg, 0, 0, 0, PM_REMOVE)) DispatchMessage(&msg);
5498         ok_sequence(button[i].killfocus, "SetFocus(0) on a button", FALSE);
5499
5500         ok(GetFocus() == 0, "expected focus 0, got %p\n", GetFocus());
5501
5502         SendMessage(hwnd, BM_SETSTYLE, button[i].style | BS_BOTTOM, TRUE);
5503         SendMessage(hwnd, WM_APP, 0, 0); /* place a separator mark here */
5504         while (PeekMessage(&msg, 0, 0, 0, PM_REMOVE)) DispatchMessage(&msg);
5505         ok_sequence(button[i].setstyle, "BM_SETSTYLE on a button", FALSE);
5506
5507         style = GetWindowLongA(hwnd, GWL_STYLE);
5508         style &= ~(WS_VISIBLE | WS_CHILD | BS_NOTIFY);
5509         /* XP doesn't turn a BS_USERBUTTON into BS_PUSHBUTTON here! */
5510         ok(style == button[i].style, "expected style %04x got %04x\n", button[i].style, style);
5511
5512         state = SendMessage(hwnd, BM_GETSTATE, 0, 0);
5513         ok(state == 0, "expected state 0, got %04x\n", state);
5514
5515         flush_sequence();
5516
5517         SendMessage(hwnd, BM_SETSTATE, TRUE, 0);
5518         SendMessage(hwnd, WM_APP, 0, 0); /* place a separator mark here */
5519         while (PeekMessage(&msg, 0, 0, 0, PM_REMOVE)) DispatchMessage(&msg);
5520         ok_sequence(button[i].setstate, "BM_SETSTATE/TRUE on a button", FALSE);
5521
5522         state = SendMessage(hwnd, BM_GETSTATE, 0, 0);
5523         ok(state == 0x0004, "expected state 0x0004, got %04x\n", state);
5524
5525         style = GetWindowLongA(hwnd, GWL_STYLE);
5526         style &= ~(WS_CHILD | BS_NOTIFY | WS_VISIBLE);
5527         ok(style == button[i].style, "expected style %04x got %04x\n", button[i].style, style);
5528
5529         flush_sequence();
5530
5531         SendMessage(hwnd, BM_SETSTATE, FALSE, 0);
5532         SendMessage(hwnd, WM_APP, 0, 0); /* place a separator mark here */
5533         while (PeekMessage(&msg, 0, 0, 0, PM_REMOVE)) DispatchMessage(&msg);
5534         ok_sequence(button[i].clearstate, "BM_SETSTATE/FALSE on a button", FALSE);
5535
5536         state = SendMessage(hwnd, BM_GETSTATE, 0, 0);
5537         ok(state == 0, "expected state 0, got %04x\n", state);
5538
5539         style = GetWindowLongA(hwnd, GWL_STYLE);
5540         style &= ~(WS_CHILD | BS_NOTIFY | WS_VISIBLE);
5541         ok(style == button[i].style, "expected style %04x got %04x\n", button[i].style, style);
5542
5543         state = SendMessage(hwnd, BM_GETCHECK, 0, 0);
5544         ok(state == BST_UNCHECKED, "expected BST_UNCHECKED, got %04x\n", state);
5545
5546         flush_sequence();
5547
5548         SendMessage(hwnd, BM_SETCHECK, BST_UNCHECKED, 0);
5549         SendMessage(hwnd, WM_APP, 0, 0); /* place a separator mark here */
5550         while (PeekMessage(&msg, 0, 0, 0, PM_REMOVE)) DispatchMessage(&msg);
5551         ok_sequence(WmSetCheckIgnoredSeq, "BM_SETCHECK on a button", FALSE);
5552
5553         state = SendMessage(hwnd, BM_GETCHECK, 0, 0);
5554         ok(state == BST_UNCHECKED, "expected BST_UNCHECKED, got %04x\n", state);
5555
5556         style = GetWindowLongA(hwnd, GWL_STYLE);
5557         style &= ~(WS_CHILD | BS_NOTIFY | WS_VISIBLE);
5558         ok(style == button[i].style, "expected style %04x got %04x\n", button[i].style, style);
5559
5560         flush_sequence();
5561
5562         SendMessage(hwnd, BM_SETCHECK, BST_CHECKED, 0);
5563         SendMessage(hwnd, WM_APP, 0, 0); /* place a separator mark here */
5564         while (PeekMessage(&msg, 0, 0, 0, PM_REMOVE)) DispatchMessage(&msg);
5565         ok_sequence(button[i].setcheck, "BM_SETCHECK on a button", FALSE);
5566
5567         state = SendMessage(hwnd, BM_GETCHECK, 0, 0);
5568         if (button[i].style == BS_PUSHBUTTON ||
5569             button[i].style == BS_DEFPUSHBUTTON ||
5570             button[i].style == BS_GROUPBOX ||
5571             button[i].style == BS_USERBUTTON ||
5572             button[i].style == BS_OWNERDRAW)
5573             ok(state == BST_UNCHECKED, "expected check 0, got %04x\n", state);
5574         else
5575             ok(state == BST_CHECKED, "expected check 1, got %04x\n", state);
5576
5577         style = GetWindowLongA(hwnd, GWL_STYLE);
5578         style &= ~(WS_CHILD | BS_NOTIFY | WS_VISIBLE);
5579         if (button[i].style == BS_RADIOBUTTON ||
5580             button[i].style == BS_AUTORADIOBUTTON)
5581             ok(style == (button[i].style | WS_TABSTOP), "expected style %04x | WS_TABSTOP got %04x\n", button[i].style, style);
5582         else
5583             ok(style == button[i].style, "expected style %04x got %04x\n", button[i].style, style);
5584
5585         log_all_parent_messages--;
5586
5587         DestroyWindow(hwnd);
5588     }
5589
5590     DestroyWindow(parent);
5591
5592     hwnd = CreateWindowExA(0, "my_button_class", "test", BS_PUSHBUTTON | WS_POPUP | WS_VISIBLE,
5593                            0, 0, 50, 14, 0, 0, 0, NULL);
5594     ok(hwnd != 0, "Failed to create button window\n");
5595
5596     SetForegroundWindow(hwnd);
5597     flush_events();
5598
5599     SetActiveWindow(hwnd);
5600     SetFocus(0);
5601     flush_sequence();
5602
5603     SendMessageA(hwnd, WM_LBUTTONDOWN, 0, 0);
5604     ok_sequence(WmLButtonDownSeq, "WM_LBUTTONDOWN on a button", FALSE);
5605
5606     SendMessageA(hwnd, WM_LBUTTONUP, 0, 0);
5607     ok_sequence(WmLButtonUpSeq, "WM_LBUTTONUP on a button", FALSE);
5608
5609     flush_sequence();
5610     zfont = GetStockObject(SYSTEM_FONT);
5611     SendMessageA(hwnd, WM_SETFONT, (WPARAM)zfont, TRUE);
5612     UpdateWindow(hwnd);
5613     ok_sequence(WmSetFontButtonSeq, "WM_SETFONT on a button", FALSE);
5614
5615     DestroyWindow(hwnd);
5616 }
5617
5618 /****************** static message test *************************/
5619 static const struct message WmSetFontStaticSeq[] =
5620 {
5621     { WM_SETFONT, sent },
5622     { WM_PAINT, sent|defwinproc|optional },
5623     { WM_ERASEBKGND, sent|defwinproc|optional },
5624     { WM_CTLCOLORSTATIC, sent|defwinproc|optional },
5625     { 0 }
5626 };
5627
5628 static WNDPROC old_static_proc;
5629
5630 static LRESULT CALLBACK static_hook_proc(HWND hwnd, UINT message, WPARAM wParam, LPARAM lParam)
5631 {
5632     static LONG defwndproc_counter = 0;
5633     LRESULT ret;
5634     struct recvd_message msg;
5635
5636     if (ignore_message( message )) return 0;
5637
5638     msg.hwnd = hwnd;
5639     msg.message = message;
5640     msg.flags = sent|wparam|lparam;
5641     if (defwndproc_counter) msg.flags |= defwinproc;
5642     msg.wParam = wParam;
5643     msg.lParam = lParam;
5644     msg.descr = "static";
5645     add_message(&msg);
5646
5647     defwndproc_counter++;
5648     ret = CallWindowProcA(old_static_proc, hwnd, message, wParam, lParam);
5649     defwndproc_counter--;
5650
5651     return ret;
5652 }
5653
5654 static void subclass_static(void)
5655 {
5656     WNDCLASSA cls;
5657
5658     if (!GetClassInfoA(0, "static", &cls)) assert(0);
5659
5660     old_static_proc = cls.lpfnWndProc;
5661
5662     cls.hInstance = GetModuleHandle(0);
5663     cls.lpfnWndProc = static_hook_proc;
5664     cls.lpszClassName = "my_static_class";
5665     UnregisterClass(cls.lpszClassName, cls.hInstance);
5666     if (!RegisterClassA(&cls)) assert(0);
5667 }
5668
5669 static void test_static_messages(void)
5670 {
5671     /* FIXME: make as comprehensive as the button message test */
5672     static const struct
5673     {
5674         DWORD style;
5675         DWORD dlg_code;
5676         const struct message *setfont;
5677     } static_ctrl[] = {
5678         { SS_LEFT, DLGC_STATIC,
5679           WmSetFontStaticSeq }
5680     };
5681     unsigned int i;
5682     HWND hwnd;
5683     DWORD dlg_code;
5684
5685     subclass_static();
5686
5687     for (i = 0; i < sizeof(static_ctrl)/sizeof(static_ctrl[0]); i++)
5688     {
5689         hwnd = CreateWindowExA(0, "my_static_class", "test", static_ctrl[i].style | WS_POPUP,
5690                                0, 0, 50, 14, 0, 0, 0, NULL);
5691         ok(hwnd != 0, "Failed to create static window\n");
5692
5693         dlg_code = SendMessageA(hwnd, WM_GETDLGCODE, 0, 0);
5694         ok(dlg_code == static_ctrl[i].dlg_code, "%u: wrong dlg_code %08x\n", i, dlg_code);
5695
5696         ShowWindow(hwnd, SW_SHOW);
5697         UpdateWindow(hwnd);
5698         SetFocus(0);
5699         flush_sequence();
5700
5701         trace("static style %08x\n", static_ctrl[i].style);
5702         SendMessage(hwnd, WM_SETFONT, (WPARAM)GetStockObject(DEFAULT_GUI_FONT), TRUE);
5703         ok_sequence(static_ctrl[i].setfont, "WM_SETFONT on a static", FALSE);
5704
5705         DestroyWindow(hwnd);
5706     }
5707 }
5708
5709 /****************** ComboBox message test *************************/
5710 #define ID_COMBOBOX 0x000f
5711
5712 static const struct message WmKeyDownComboSeq[] =
5713 {
5714     { WM_KEYDOWN, sent|wparam|lparam, VK_DOWN, 0 },
5715     { WM_COMMAND, sent|wparam|defwinproc, MAKEWPARAM(1000, LBN_SELCHANGE) },
5716     { WM_COMMAND, sent|wparam|parent, MAKEWPARAM(ID_COMBOBOX, CBN_SELENDOK) },
5717     { WM_COMMAND, sent|wparam|parent, MAKEWPARAM(ID_COMBOBOX, CBN_SELCHANGE) },
5718     { WM_CTLCOLOREDIT, sent|parent },
5719     { WM_KEYUP, sent|wparam|lparam, VK_DOWN, 0 },
5720     { 0 }
5721 };
5722
5723 static WNDPROC old_combobox_proc;
5724
5725 static LRESULT CALLBACK combobox_hook_proc(HWND hwnd, UINT message, WPARAM wParam, LPARAM lParam)
5726 {
5727     static LONG defwndproc_counter = 0;
5728     LRESULT ret;
5729     struct recvd_message msg;
5730
5731     /* do not log painting messages */
5732     if (message != WM_PAINT &&
5733         message != WM_NCPAINT &&
5734         message != WM_SYNCPAINT &&
5735         message != WM_ERASEBKGND &&
5736         message != WM_NCHITTEST &&
5737         message != WM_GETTEXT &&
5738         !ignore_message( message ))
5739     {
5740         msg.hwnd = hwnd;
5741         msg.message = message;
5742         msg.flags = sent|wparam|lparam;
5743         if (defwndproc_counter) msg.flags |= defwinproc;
5744         msg.wParam = wParam;
5745         msg.lParam = lParam;
5746         msg.descr = "combo";
5747         add_message(&msg);
5748     }
5749
5750     defwndproc_counter++;
5751     ret = CallWindowProcA(old_combobox_proc, hwnd, message, wParam, lParam);
5752     defwndproc_counter--;
5753
5754     return ret;
5755 }
5756
5757 static void subclass_combobox(void)
5758 {
5759     WNDCLASSA cls;
5760
5761     if (!GetClassInfoA(0, "ComboBox", &cls)) assert(0);
5762
5763     old_combobox_proc = cls.lpfnWndProc;
5764
5765     cls.hInstance = GetModuleHandle(0);
5766     cls.lpfnWndProc = combobox_hook_proc;
5767     cls.lpszClassName = "my_combobox_class";
5768     UnregisterClass(cls.lpszClassName, cls.hInstance);
5769     if (!RegisterClassA(&cls)) assert(0);
5770 }
5771
5772 static void test_combobox_messages(void)
5773 {
5774     HWND parent, combo;
5775     LRESULT ret;
5776
5777     subclass_combobox();
5778
5779     parent = CreateWindowExA(0, "TestParentClass", "Test parent", WS_OVERLAPPEDWINDOW | WS_VISIBLE,
5780                              100, 100, 200, 200, 0, 0, 0, NULL);
5781     ok(parent != 0, "Failed to create parent window\n");
5782     flush_sequence();
5783
5784     combo = CreateWindowEx(0, "my_combobox_class", "test", WS_CHILD | WS_VISIBLE | CBS_DROPDOWNLIST | CBS_HASSTRINGS,
5785                            0, 0, 100, 150, parent, (HMENU)ID_COMBOBOX, 0, NULL);
5786     ok(combo != 0, "Failed to create combobox window\n");
5787
5788     UpdateWindow(combo);
5789
5790     ret = SendMessage(combo, WM_GETDLGCODE, 0, 0);
5791     ok(ret == (DLGC_WANTCHARS | DLGC_WANTARROWS), "wrong dlg_code %08lx\n", ret);
5792
5793     ret = SendMessage(combo, CB_ADDSTRING, 0, (LPARAM)"item 0");
5794     ok(ret == 0, "expected 0, got %ld\n", ret);
5795     ret = SendMessage(combo, CB_ADDSTRING, 0, (LPARAM)"item 1");
5796     ok(ret == 1, "expected 1, got %ld\n", ret);
5797     ret = SendMessage(combo, CB_ADDSTRING, 0, (LPARAM)"item 2");
5798     ok(ret == 2, "expected 2, got %ld\n", ret);
5799
5800     SendMessage(combo, CB_SETCURSEL, 0, 0);
5801     SetFocus(combo);
5802     flush_sequence();
5803
5804     log_all_parent_messages++;
5805     SendMessage(combo, WM_KEYDOWN, VK_DOWN, 0);
5806     SendMessage(combo, WM_KEYUP, VK_DOWN, 0);
5807     log_all_parent_messages--;
5808     ok_sequence(WmKeyDownComboSeq, "WM_KEYDOWN/VK_DOWN on a ComboBox", FALSE);
5809
5810     DestroyWindow(combo);
5811     DestroyWindow(parent);
5812 }
5813
5814 /****************** WM_IME_KEYDOWN message test *******************/
5815
5816 static const struct message WmImeKeydownMsgSeq_0[] =
5817 {
5818     { WM_IME_KEYDOWN, wparam, VK_RETURN },
5819     { WM_CHAR, wparam, 'A' },
5820     { 0 }
5821 };
5822
5823 static const struct message WmImeKeydownMsgSeq_1[] =
5824 {
5825     { WM_KEYDOWN, optional|wparam, VK_RETURN },
5826     { WM_CHAR,    optional|wparam, VK_RETURN },
5827     { 0 }
5828 };
5829
5830 static LRESULT WINAPI wmime_keydown_procA(HWND hwnd, UINT message, WPARAM wParam, LPARAM lParam)
5831 {
5832     struct recvd_message msg;
5833
5834     msg.hwnd = hwnd;
5835     msg.message = message;
5836     msg.flags = wparam|lparam;
5837     msg.wParam = wParam;
5838     msg.lParam = lParam;
5839     msg.descr = "wmime_keydown";
5840     add_message(&msg);
5841
5842     return DefWindowProcA(hwnd, message, wParam, lParam);
5843 }
5844
5845 static void register_wmime_keydown_class(void)
5846 {
5847     WNDCLASSA cls;
5848
5849     ZeroMemory(&cls, sizeof(WNDCLASSA));
5850     cls.lpfnWndProc = wmime_keydown_procA;
5851     cls.hInstance = GetModuleHandleA(0);
5852     cls.lpszClassName = "wmime_keydown_class";
5853     if (!RegisterClassA(&cls)) assert(0);
5854 }
5855
5856 static void test_wmime_keydown_message(void)
5857 {
5858     HWND hwnd;
5859     MSG msg;
5860
5861     trace("Message sequences by WM_IME_KEYDOWN\n");
5862
5863     register_wmime_keydown_class();
5864     hwnd = CreateWindowExA(0, "wmime_keydown_class", NULL, WS_OVERLAPPEDWINDOW,
5865                            CW_USEDEFAULT, CW_USEDEFAULT, 300, 300, 0,
5866                            NULL, NULL, 0);
5867     flush_events();
5868     flush_sequence();
5869
5870     SendMessage(hwnd, WM_IME_KEYDOWN, VK_RETURN, 0x1c0001);
5871     SendMessage(hwnd, WM_CHAR, 'A', 1);
5872     ok_sequence(WmImeKeydownMsgSeq_0, "WM_IME_KEYDOWN 0", FALSE);
5873
5874     while ( PeekMessage(&msg, 0, 0, 0, PM_REMOVE) )
5875     {
5876         TranslateMessage(&msg);
5877         DispatchMessage(&msg);
5878     }
5879     ok_sequence(WmImeKeydownMsgSeq_1, "WM_IME_KEYDOWN 1", FALSE);
5880
5881     DestroyWindow(hwnd);
5882 }
5883
5884 /************* painting message test ********************/
5885
5886 void dump_region(HRGN hrgn)
5887 {
5888     DWORD i, size;
5889     RGNDATA *data = NULL;
5890     RECT *rect;
5891
5892     if (!hrgn)
5893     {
5894         printf( "null region\n" );
5895         return;
5896     }
5897     if (!(size = GetRegionData( hrgn, 0, NULL ))) return;
5898     if (!(data = HeapAlloc( GetProcessHeap(), 0, size ))) return;
5899     GetRegionData( hrgn, size, data );
5900     printf("%d rects:", data->rdh.nCount );
5901     for (i = 0, rect = (RECT *)data->Buffer; i < data->rdh.nCount; i++, rect++)
5902         printf( " (%d,%d)-(%d,%d)", rect->left, rect->top, rect->right, rect->bottom );
5903     printf("\n");
5904     HeapFree( GetProcessHeap(), 0, data );
5905 }
5906
5907 static void check_update_rgn( HWND hwnd, HRGN hrgn )
5908 {
5909     INT ret;
5910     RECT r1, r2;
5911     HRGN tmp = CreateRectRgn( 0, 0, 0, 0 );
5912     HRGN update = CreateRectRgn( 0, 0, 0, 0 );
5913
5914     ret = GetUpdateRgn( hwnd, update, FALSE );
5915     ok( ret != ERROR, "GetUpdateRgn failed\n" );
5916     if (ret == NULLREGION)
5917     {
5918         ok( !hrgn, "Update region shouldn't be empty\n" );
5919     }
5920     else
5921     {
5922         if (CombineRgn( tmp, hrgn, update, RGN_XOR ) != NULLREGION)
5923         {
5924             ok( 0, "Regions are different\n" );
5925             if (winetest_debug > 0)
5926             {
5927                 printf( "Update region: " );
5928                 dump_region( update );
5929                 printf( "Wanted region: " );
5930                 dump_region( hrgn );
5931             }
5932         }
5933     }
5934     GetRgnBox( update, &r1 );
5935     GetUpdateRect( hwnd, &r2, FALSE );
5936     ok( r1.left == r2.left && r1.top == r2.top && r1.right == r2.right && r1.bottom == r2.bottom,
5937         "Rectangles are different: %d,%d-%d,%d / %d,%d-%d,%d\n",
5938         r1.left, r1.top, r1.right, r1.bottom, r2.left, r2.top, r2.right, r2.bottom );
5939
5940     DeleteObject( tmp );
5941     DeleteObject( update );
5942 }
5943
5944 static const struct message WmInvalidateRgn[] = {
5945     { WM_NCPAINT, sent },
5946     { WM_GETTEXT, sent|defwinproc|optional },
5947     { 0 }
5948 };
5949
5950 static const struct message WmGetUpdateRect[] = {
5951     { WM_NCPAINT, sent },
5952     { WM_GETTEXT, sent|defwinproc|optional },
5953     { WM_PAINT, sent },
5954     { 0 }
5955 };
5956
5957 static const struct message WmInvalidateFull[] = {
5958     { WM_NCPAINT, sent|wparam, 1 },
5959     { WM_GETTEXT, sent|defwinproc|optional },
5960     { 0 }
5961 };
5962
5963 static const struct message WmInvalidateErase[] = {
5964     { WM_NCPAINT, sent|wparam, 1 },
5965     { WM_GETTEXT, sent|defwinproc|optional },
5966     { WM_ERASEBKGND, sent },
5967     { 0 }
5968 };
5969
5970 static const struct message WmInvalidatePaint[] = {
5971     { WM_PAINT, sent },
5972     { WM_NCPAINT, sent|wparam|beginpaint, 1 },
5973     { WM_GETTEXT, sent|beginpaint|defwinproc|optional },
5974     { 0 }
5975 };
5976
5977 static const struct message WmInvalidateErasePaint[] = {
5978     { WM_PAINT, sent },
5979     { WM_NCPAINT, sent|wparam|beginpaint, 1 },
5980     { WM_GETTEXT, sent|beginpaint|defwinproc|optional },
5981     { WM_ERASEBKGND, sent|beginpaint|optional },
5982     { 0 }
5983 };
5984
5985 static const struct message WmInvalidateErasePaint2[] = {
5986     { WM_PAINT, sent },
5987     { WM_NCPAINT, sent|beginpaint },
5988     { WM_GETTEXT, sent|beginpaint|defwinproc|optional },
5989     { WM_ERASEBKGND, sent|beginpaint|optional },
5990     { 0 }
5991 };
5992
5993 static const struct message WmErase[] = {
5994     { WM_ERASEBKGND, sent },
5995     { 0 }
5996 };
5997
5998 static const struct message WmPaint[] = {
5999     { WM_PAINT, sent },
6000     { 0 }
6001 };
6002
6003 static const struct message WmParentOnlyPaint[] = {
6004     { WM_PAINT, sent|parent },
6005     { 0 }
6006 };
6007
6008 static const struct message WmInvalidateParent[] = {
6009     { WM_NCPAINT, sent|parent },
6010     { WM_GETTEXT, sent|defwinproc|parent|optional },
6011     { WM_ERASEBKGND, sent|parent },
6012     { 0 }
6013 };
6014
6015 static const struct message WmInvalidateParentChild[] = {
6016     { WM_NCPAINT, sent|parent },
6017     { WM_GETTEXT, sent|defwinproc|parent|optional },
6018     { WM_ERASEBKGND, sent|parent },
6019     { WM_NCPAINT, sent },
6020     { WM_GETTEXT, sent|defwinproc|optional },
6021     { WM_ERASEBKGND, sent },
6022     { 0 }
6023 };
6024
6025 static const struct message WmInvalidateParentChild2[] = {
6026     { WM_ERASEBKGND, sent|parent },
6027     { WM_NCPAINT, sent },
6028     { WM_GETTEXT, sent|defwinproc|optional },
6029     { WM_ERASEBKGND, sent },
6030     { 0 }
6031 };
6032
6033 static const struct message WmParentPaint[] = {
6034     { WM_PAINT, sent|parent },
6035     { WM_PAINT, sent },
6036     { 0 }
6037 };
6038
6039 static const struct message WmParentPaintNc[] = {
6040     { WM_PAINT, sent|parent },
6041     { WM_PAINT, sent },
6042     { WM_NCPAINT, sent|beginpaint },
6043     { WM_GETTEXT, sent|beginpaint|defwinproc|optional },
6044     { WM_ERASEBKGND, sent|beginpaint|optional },
6045     { 0 }
6046 };
6047
6048 static const struct message WmChildPaintNc[] = {
6049     { WM_PAINT, sent },
6050     { WM_NCPAINT, sent|beginpaint },
6051     { WM_GETTEXT, sent|beginpaint|defwinproc|optional },
6052     { WM_ERASEBKGND, sent|beginpaint|optional },
6053     { 0 }
6054 };
6055
6056 static const struct message WmParentErasePaint[] = {
6057     { WM_PAINT, sent|parent },
6058     { WM_NCPAINT, sent|parent|beginpaint },
6059     { WM_GETTEXT, sent|parent|beginpaint|defwinproc|optional },
6060     { WM_ERASEBKGND, sent|parent|beginpaint|optional },
6061     { WM_PAINT, sent },
6062     { WM_NCPAINT, sent|beginpaint },
6063     { WM_GETTEXT, sent|beginpaint|defwinproc|optional },
6064     { WM_ERASEBKGND, sent|beginpaint|optional },
6065     { 0 }
6066 };
6067
6068 static const struct message WmParentOnlyNcPaint[] = {
6069     { WM_PAINT, sent|parent },
6070     { WM_NCPAINT, sent|parent|beginpaint },
6071     { WM_GETTEXT, sent|parent|beginpaint|defwinproc|optional },
6072     { 0 }
6073 };
6074
6075 static const struct message WmSetParentStyle[] = {
6076     { WM_STYLECHANGING, sent|parent },
6077     { WM_STYLECHANGED, sent|parent },
6078     { 0 }
6079 };
6080
6081 static void test_paint_messages(void)
6082 {
6083     BOOL ret;
6084     RECT rect;
6085     POINT pt;
6086     MSG msg;
6087     HWND hparent, hchild;
6088     HRGN hrgn = CreateRectRgn( 0, 0, 0, 0 );
6089     HRGN hrgn2 = CreateRectRgn( 0, 0, 0, 0 );
6090     HWND hwnd = CreateWindowExA(0, "TestWindowClass", "Test overlapped", WS_OVERLAPPEDWINDOW,
6091                                 100, 100, 200, 200, 0, 0, 0, NULL);
6092     ok (hwnd != 0, "Failed to create overlapped window\n");
6093
6094     ShowWindow( hwnd, SW_SHOW );
6095     UpdateWindow( hwnd );
6096     flush_events();
6097     flush_sequence();
6098
6099     check_update_rgn( hwnd, 0 );
6100     SetRectRgn( hrgn, 10, 10, 20, 20 );
6101     ret = RedrawWindow( hwnd, NULL, hrgn, RDW_INVALIDATE );
6102     ok(ret, "RedrawWindow returned %d instead of TRUE\n", ret);
6103     check_update_rgn( hwnd, hrgn );
6104     SetRectRgn( hrgn2, 20, 20, 30, 30 );
6105     ret = RedrawWindow( hwnd, NULL, hrgn2, RDW_INVALIDATE );
6106     ok(ret, "RedrawWindow returned %d instead of TRUE\n", ret);
6107     CombineRgn( hrgn, hrgn, hrgn2, RGN_OR );
6108     check_update_rgn( hwnd, hrgn );
6109     /* validate everything */
6110     ret = RedrawWindow( hwnd, NULL, NULL, RDW_VALIDATE );
6111     ok(ret, "RedrawWindow returned %d instead of TRUE\n", ret);
6112     check_update_rgn( hwnd, 0 );
6113
6114     /* test empty region */
6115     SetRectRgn( hrgn, 10, 10, 10, 15 );
6116     ret = RedrawWindow( hwnd, NULL, hrgn, RDW_INVALIDATE );
6117     ok(ret, "RedrawWindow returned %d instead of TRUE\n", ret);
6118     check_update_rgn( hwnd, 0 );
6119     /* test empty rect */
6120     SetRect( &rect, 10, 10, 10, 15 );
6121     ret = RedrawWindow( hwnd, &rect, NULL, RDW_INVALIDATE );
6122     ok(ret, "RedrawWindow returned %d instead of TRUE\n", ret);
6123     check_update_rgn( hwnd, 0 );
6124
6125     /* flush pending messages */
6126     flush_events();
6127     flush_sequence();
6128
6129     GetClientRect( hwnd, &rect );
6130     SetRectRgn( hrgn, 0, 0, rect.right - rect.left, rect.bottom - rect.top );
6131     /* MSDN: if hwnd parameter is NULL, InvalidateRect invalidates and redraws
6132      * all windows and sends WM_ERASEBKGND and WM_NCPAINT.
6133      */
6134     trace("testing InvalidateRect(0, NULL, FALSE)\n");
6135     SetRectEmpty( &rect );
6136     ok(InvalidateRect(0, &rect, FALSE), "InvalidateRect(0, &rc, FALSE) should fail\n");
6137     check_update_rgn( hwnd, hrgn );
6138     ok_sequence( WmInvalidateErase, "InvalidateErase", FALSE );
6139     flush_events();
6140     ok_sequence( WmPaint, "Paint", FALSE );
6141     RedrawWindow( hwnd, NULL, NULL, RDW_VALIDATE );
6142     check_update_rgn( hwnd, 0 );
6143
6144     /* MSDN: if hwnd parameter is NULL, ValidateRect invalidates and redraws
6145      * all windows and sends WM_ERASEBKGND and WM_NCPAINT.
6146      */
6147     trace("testing ValidateRect(0, NULL)\n");
6148     SetRectEmpty( &rect );
6149     if (ValidateRect(0, &rect))  /* not supported on Win9x */
6150     {
6151         check_update_rgn( hwnd, hrgn );
6152         ok_sequence( WmInvalidateErase, "InvalidateErase", FALSE );
6153         flush_events();
6154         ok_sequence( WmPaint, "Paint", FALSE );
6155         RedrawWindow( hwnd, NULL, NULL, RDW_VALIDATE );
6156         check_update_rgn( hwnd, 0 );
6157     }
6158
6159     trace("testing InvalidateRgn(0, NULL, FALSE)\n");
6160     SetLastError(0xdeadbeef);
6161     ok(!InvalidateRgn(0, NULL, FALSE), "InvalidateRgn(0, NULL, FALSE) should fail\n");
6162     ok(GetLastError() == ERROR_INVALID_WINDOW_HANDLE || GetLastError() == 0xdeadbeef,
6163        "wrong error code %d\n", GetLastError());
6164     check_update_rgn( hwnd, 0 );
6165     flush_events();
6166     ok_sequence( WmEmptySeq, "WmEmptySeq", FALSE );
6167
6168     trace("testing ValidateRgn(0, NULL)\n");
6169     SetLastError(0xdeadbeef);
6170     ok(!ValidateRgn(0, NULL), "ValidateRgn(0, NULL) should fail\n");
6171     ok(GetLastError() == ERROR_INVALID_WINDOW_HANDLE ||
6172        broken( GetLastError() == 0xdeadbeef ) /* win9x */,
6173        "wrong error code %d\n", GetLastError());
6174     check_update_rgn( hwnd, 0 );
6175     flush_events();
6176     ok_sequence( WmEmptySeq, "WmEmptySeq", FALSE );
6177
6178     trace("testing UpdateWindow(NULL)\n");
6179     SetLastError(0xdeadbeef);
6180     ok(!UpdateWindow(NULL), "UpdateWindow(NULL) should fail\n");
6181     ok(GetLastError() == ERROR_INVALID_WINDOW_HANDLE ||
6182        broken( GetLastError() == 0xdeadbeef ) /* win9x */,
6183        "wrong error code %d\n", GetLastError());
6184     check_update_rgn( hwnd, 0 );
6185     flush_events();
6186     ok_sequence( WmEmptySeq, "WmEmptySeq", FALSE );
6187
6188     /* now with frame */
6189     SetRectRgn( hrgn, -5, -5, 20, 20 );
6190
6191     /* flush pending messages */
6192     flush_events();
6193     flush_sequence();
6194     RedrawWindow( hwnd, NULL, hrgn, RDW_INVALIDATE | RDW_FRAME );
6195     ok_sequence( WmEmptySeq, "EmptySeq", FALSE );
6196
6197     SetRectRgn( hrgn, 0, 0, 20, 20 );  /* GetUpdateRgn clips to client area */
6198     check_update_rgn( hwnd, hrgn );
6199
6200     flush_sequence();
6201     RedrawWindow( hwnd, NULL, hrgn, RDW_INVALIDATE | RDW_FRAME | RDW_ERASENOW );
6202     ok_sequence( WmInvalidateRgn, "InvalidateRgn", FALSE );
6203
6204     flush_sequence();
6205     RedrawWindow( hwnd, NULL, NULL, RDW_INVALIDATE | RDW_FRAME | RDW_ERASENOW );
6206     ok_sequence( WmInvalidateFull, "InvalidateFull", FALSE );
6207
6208     GetClientRect( hwnd, &rect );
6209     SetRectRgn( hrgn, rect.left, rect.top, rect.right, rect.bottom );
6210     check_update_rgn( hwnd, hrgn );
6211
6212     flush_sequence();
6213     RedrawWindow( hwnd, NULL, NULL, RDW_INVALIDATE | RDW_FRAME | RDW_ERASE | RDW_ERASENOW );
6214     ok_sequence( WmInvalidateErase, "InvalidateErase", FALSE );
6215
6216     flush_sequence();
6217     RedrawWindow( hwnd, NULL, NULL, RDW_INVALIDATE | RDW_FRAME | RDW_ERASENOW | RDW_UPDATENOW );
6218     ok_sequence( WmInvalidatePaint, "InvalidatePaint", FALSE );
6219     check_update_rgn( hwnd, 0 );
6220
6221     flush_sequence();
6222     RedrawWindow( hwnd, NULL, NULL, RDW_INVALIDATE | RDW_FRAME | RDW_ERASE | RDW_UPDATENOW );
6223     ok_sequence( WmInvalidateErasePaint, "InvalidateErasePaint", FALSE );
6224     check_update_rgn( hwnd, 0 );
6225
6226     flush_sequence();
6227     SetRectRgn( hrgn, 0, 0, 100, 100 );
6228     RedrawWindow( hwnd, NULL, hrgn, RDW_INVALIDATE );
6229     SetRectRgn( hrgn, 0, 0, 50, 100 );
6230     RedrawWindow( hwnd, NULL, hrgn, RDW_VALIDATE );
6231     SetRectRgn( hrgn, 50, 0, 100, 100 );
6232     check_update_rgn( hwnd, hrgn );
6233     RedrawWindow( hwnd, NULL, hrgn, RDW_VALIDATE | RDW_ERASENOW );
6234     ok_sequence( WmEmptySeq, "EmptySeq", FALSE );  /* must not generate messages, everything is valid */
6235     check_update_rgn( hwnd, 0 );
6236
6237     flush_sequence();
6238     SetRectRgn( hrgn, 0, 0, 100, 100 );
6239     RedrawWindow( hwnd, NULL, hrgn, RDW_INVALIDATE | RDW_ERASE );
6240     SetRectRgn( hrgn, 0, 0, 100, 50 );
6241     RedrawWindow( hwnd, NULL, hrgn, RDW_VALIDATE | RDW_ERASENOW );
6242     ok_sequence( WmErase, "Erase", FALSE );
6243     SetRectRgn( hrgn, 0, 50, 100, 100 );
6244     check_update_rgn( hwnd, hrgn );
6245
6246     flush_sequence();
6247     SetRectRgn( hrgn, 0, 0, 100, 100 );
6248     RedrawWindow( hwnd, NULL, hrgn, RDW_INVALIDATE | RDW_ERASE );
6249     SetRectRgn( hrgn, 0, 0, 50, 50 );
6250     RedrawWindow( hwnd, NULL, hrgn, RDW_VALIDATE | RDW_NOERASE | RDW_UPDATENOW );
6251     ok_sequence( WmPaint, "Paint", FALSE );
6252
6253     flush_sequence();
6254     SetRectRgn( hrgn, -4, -4, -2, -2 );
6255     RedrawWindow( hwnd, NULL, hrgn, RDW_INVALIDATE | RDW_FRAME );
6256     SetRectRgn( hrgn, -200, -200, -198, -198 );
6257     RedrawWindow( hwnd, NULL, hrgn, RDW_VALIDATE | RDW_NOFRAME | RDW_ERASENOW );
6258     ok_sequence( WmEmptySeq, "EmptySeq", FALSE );
6259
6260     flush_sequence();
6261     SetRectRgn( hrgn, -4, -4, -2, -2 );
6262     RedrawWindow( hwnd, NULL, hrgn, RDW_INVALIDATE | RDW_FRAME );
6263     SetRectRgn( hrgn, -4, -4, -3, -3 );
6264     RedrawWindow( hwnd, NULL, hrgn, RDW_VALIDATE | RDW_NOFRAME );
6265     SetRectRgn( hrgn, 0, 0, 1, 1 );
6266     RedrawWindow( hwnd, NULL, hrgn, RDW_INVALIDATE | RDW_UPDATENOW );
6267     ok_sequence( WmPaint, "Paint", FALSE );
6268
6269     flush_sequence();
6270     SetRectRgn( hrgn, -4, -4, -1, -1 );
6271     RedrawWindow( hwnd, NULL, hrgn, RDW_INVALIDATE | RDW_FRAME );
6272     RedrawWindow( hwnd, NULL, 0, RDW_ERASENOW );
6273     /* make sure no WM_PAINT was generated */
6274     flush_events();
6275     ok_sequence( WmInvalidateRgn, "InvalidateRgn", FALSE );
6276
6277     flush_sequence();
6278     SetRectRgn( hrgn, -4, -4, -1, -1 );
6279     RedrawWindow( hwnd, NULL, hrgn, RDW_INVALIDATE | RDW_FRAME );
6280     while (PeekMessage( &msg, 0, 0, 0, PM_REMOVE ))
6281     {
6282         if (msg.hwnd == hwnd && msg.message == WM_PAINT)
6283         {
6284             /* GetUpdateRgn must return empty region since only nonclient area is invalidated */
6285             INT ret = GetUpdateRgn( hwnd, hrgn, FALSE );
6286             ok( ret == NULLREGION, "Invalid GetUpdateRgn result %d\n", ret );
6287             ret = GetUpdateRect( hwnd, &rect, FALSE );
6288             ok( ret, "Invalid GetUpdateRect result %d\n", ret );
6289             /* this will send WM_NCPAINT and validate the non client area */
6290             ret = GetUpdateRect( hwnd, &rect, TRUE );
6291             ok( !ret, "Invalid GetUpdateRect result %d\n", ret );
6292         }
6293         DispatchMessage( &msg );
6294     }
6295     ok_sequence( WmGetUpdateRect, "GetUpdateRect", FALSE );
6296
6297     DestroyWindow( hwnd );
6298
6299     /* now test with a child window */
6300
6301     hparent = CreateWindowExA(0, "TestParentClass", "Test parent", WS_OVERLAPPEDWINDOW,
6302                               100, 100, 200, 200, 0, 0, 0, NULL);
6303     ok (hparent != 0, "Failed to create parent window\n");
6304
6305     hchild = CreateWindowExA(0, "TestWindowClass", "Test child", WS_CHILD | WS_VISIBLE | WS_BORDER,
6306                            10, 10, 100, 100, hparent, 0, 0, NULL);
6307     ok (hchild != 0, "Failed to create child window\n");
6308
6309     ShowWindow( hparent, SW_SHOW );
6310     UpdateWindow( hparent );
6311     UpdateWindow( hchild );
6312     flush_events();
6313     flush_sequence();
6314     log_all_parent_messages++;
6315
6316     SetRect( &rect, 0, 0, 50, 50 );
6317     RedrawWindow( hparent, &rect, 0, RDW_INVALIDATE | RDW_ERASE | RDW_FRAME );
6318     RedrawWindow( hparent, NULL, 0, RDW_ERASENOW | RDW_ALLCHILDREN );
6319     ok_sequence( WmInvalidateParentChild, "InvalidateParentChild", FALSE );
6320
6321     RedrawWindow( hparent, &rect, 0, RDW_INVALIDATE | RDW_ERASE | RDW_FRAME );
6322     pt.x = pt.y = 0;
6323     MapWindowPoints( hchild, hparent, &pt, 1 );
6324     SetRectRgn( hrgn, 0, 0, 50 - pt.x, 50 - pt.y );
6325     check_update_rgn( hchild, hrgn );
6326     SetRectRgn( hrgn, 0, 0, 50, 50 );
6327     check_update_rgn( hparent, hrgn );
6328     RedrawWindow( hparent, NULL, 0, RDW_ERASENOW );
6329     ok_sequence( WmInvalidateParent, "InvalidateParent", FALSE );
6330     RedrawWindow( hchild, NULL, 0, RDW_ERASENOW );
6331     ok_sequence( WmEmptySeq, "EraseNow child", FALSE );
6332
6333     flush_events();
6334     ok_sequence( WmParentPaintNc, "WmParentPaintNc", FALSE );
6335
6336     RedrawWindow( hparent, &rect, 0, RDW_INVALIDATE | RDW_ERASE | RDW_FRAME | RDW_ALLCHILDREN );
6337     RedrawWindow( hparent, NULL, 0, RDW_ERASENOW );
6338     ok_sequence( WmInvalidateParent, "InvalidateParent2", FALSE );
6339     RedrawWindow( hchild, NULL, 0, RDW_ERASENOW );
6340     ok_sequence( WmEmptySeq, "EraseNow child", FALSE );
6341
6342     RedrawWindow( hparent, &rect, 0, RDW_INVALIDATE | RDW_ERASE );
6343     RedrawWindow( hparent, NULL, 0, RDW_ERASENOW | RDW_ALLCHILDREN );
6344     ok_sequence( WmInvalidateParentChild2, "InvalidateParentChild2", FALSE );
6345
6346     SetWindowLong( hparent, GWL_STYLE, GetWindowLong(hparent,GWL_STYLE) | WS_CLIPCHILDREN );
6347     flush_sequence();
6348     RedrawWindow( hparent, &rect, 0, RDW_INVALIDATE | RDW_ERASE | RDW_FRAME | RDW_ALLCHILDREN );
6349     RedrawWindow( hparent, NULL, 0, RDW_ERASENOW );
6350     ok_sequence( WmInvalidateParentChild, "InvalidateParentChild3", FALSE );
6351
6352     /* flush all paint messages */
6353     flush_events();
6354     flush_sequence();
6355
6356     /* RDW_UPDATENOW on child with WS_CLIPCHILDREN doesn't change corresponding parent area */
6357     RedrawWindow( hparent, &rect, 0, RDW_INVALIDATE | RDW_ERASE | RDW_FRAME | RDW_ALLCHILDREN );
6358     SetRectRgn( hrgn, 0, 0, 50, 50 );
6359     check_update_rgn( hparent, hrgn );
6360     RedrawWindow( hchild, NULL, 0, RDW_UPDATENOW );
6361     ok_sequence( WmInvalidateErasePaint2, "WmInvalidateErasePaint2", FALSE );
6362     SetRectRgn( hrgn, 0, 0, 50, 50 );
6363     check_update_rgn( hparent, hrgn );
6364
6365     /* flush all paint messages */
6366     flush_events();
6367     SetWindowLong( hparent, GWL_STYLE, GetWindowLong(hparent,GWL_STYLE) & ~WS_CLIPCHILDREN );
6368     flush_sequence();
6369
6370     /* RDW_UPDATENOW on child without WS_CLIPCHILDREN will validate corresponding parent area */
6371     RedrawWindow( hparent, &rect, 0, RDW_INVALIDATE | RDW_ERASE | RDW_FRAME );
6372     SetRectRgn( hrgn, 0, 0, 50, 50 );
6373     check_update_rgn( hparent, hrgn );
6374     RedrawWindow( hchild, NULL, 0, RDW_UPDATENOW );
6375     ok_sequence( WmInvalidateErasePaint2, "WmInvalidateErasePaint2", FALSE );
6376     SetRectRgn( hrgn2, 10, 10, 50, 50 );
6377     CombineRgn( hrgn, hrgn, hrgn2, RGN_DIFF );
6378     check_update_rgn( hparent, hrgn );
6379     /* flush all paint messages */
6380     flush_events();
6381     flush_sequence();
6382
6383     /* same as above but parent gets completely validated */
6384     SetRect( &rect, 20, 20, 30, 30 );
6385     RedrawWindow( hparent, &rect, 0, RDW_INVALIDATE | RDW_ERASE | RDW_FRAME );
6386     SetRectRgn( hrgn, 20, 20, 30, 30 );
6387     check_update_rgn( hparent, hrgn );
6388     RedrawWindow( hchild, NULL, 0, RDW_UPDATENOW );
6389     ok_sequence( WmInvalidateErasePaint2, "WmInvalidateErasePaint2", FALSE );
6390     check_update_rgn( hparent, 0 );  /* no update region */
6391     flush_events();
6392     ok_sequence( WmEmptySeq, "WmEmpty", FALSE );  /* and no paint messages */
6393
6394     /* make sure RDW_VALIDATE on child doesn't have the same effect */
6395     flush_sequence();
6396     RedrawWindow( hparent, &rect, 0, RDW_INVALIDATE | RDW_ERASE | RDW_FRAME );
6397     SetRectRgn( hrgn, 20, 20, 30, 30 );
6398     check_update_rgn( hparent, hrgn );
6399     RedrawWindow( hchild, NULL, 0, RDW_VALIDATE | RDW_NOERASE );
6400     SetRectRgn( hrgn, 20, 20, 30, 30 );
6401     check_update_rgn( hparent, hrgn );
6402
6403     /* same as above but normal WM_PAINT doesn't validate parent */
6404     flush_sequence();
6405     SetRect( &rect, 20, 20, 30, 30 );
6406     RedrawWindow( hparent, &rect, 0, RDW_INVALIDATE | RDW_ERASE | RDW_FRAME );
6407     SetRectRgn( hrgn, 20, 20, 30, 30 );
6408     check_update_rgn( hparent, hrgn );
6409     /* no WM_PAINT in child while parent still pending */
6410     while (PeekMessage( &msg, hchild, 0, 0, PM_REMOVE )) DispatchMessage( &msg );
6411     ok_sequence( WmEmptySeq, "No WM_PAINT", FALSE );
6412     while (PeekMessage( &msg, hparent, 0, 0, PM_REMOVE )) DispatchMessage( &msg );
6413     ok_sequence( WmParentErasePaint, "WmParentErasePaint", FALSE );
6414
6415     flush_sequence();
6416     RedrawWindow( hparent, &rect, 0, RDW_INVALIDATE | RDW_ERASE | RDW_FRAME );
6417     /* no WM_PAINT in child while parent still pending */
6418     while (PeekMessage( &msg, hchild, 0, 0, PM_REMOVE )) DispatchMessage( &msg );
6419     ok_sequence( WmEmptySeq, "No WM_PAINT", FALSE );
6420     RedrawWindow( hparent, &rect, 0, RDW_VALIDATE | RDW_NOERASE | RDW_NOCHILDREN );
6421     /* now that parent is valid child should get WM_PAINT */
6422     while (PeekMessage( &msg, hchild, 0, 0, PM_REMOVE )) DispatchMessage( &msg );
6423     ok_sequence( WmInvalidateErasePaint2, "WmInvalidateErasePaint2", FALSE );
6424     while (PeekMessage( &msg, 0, 0, 0, PM_REMOVE )) DispatchMessage( &msg );
6425     ok_sequence( WmEmptySeq, "No other message", FALSE );
6426
6427     /* same thing with WS_CLIPCHILDREN in parent */
6428     flush_sequence();
6429     SetWindowLong( hparent, GWL_STYLE, GetWindowLong(hparent,GWL_STYLE) | WS_CLIPCHILDREN );
6430     ok_sequence( WmSetParentStyle, "WmSetParentStyle", FALSE );
6431     /* changing style invalidates non client area, but we need to invalidate something else to see it */
6432     RedrawWindow( hparent, &rect, 0, RDW_UPDATENOW );
6433     ok_sequence( WmEmptySeq, "No message", FALSE );
6434     RedrawWindow( hparent, &rect, 0, RDW_INVALIDATE | RDW_UPDATENOW );
6435     ok_sequence( WmParentOnlyNcPaint, "WmParentOnlyNcPaint", FALSE );
6436
6437     flush_sequence();
6438     RedrawWindow( hparent, &rect, 0, RDW_INVALIDATE | RDW_ERASE | RDW_ALLCHILDREN );
6439     SetRectRgn( hrgn, 20, 20, 30, 30 );
6440     check_update_rgn( hparent, hrgn );
6441     /* no WM_PAINT in child while parent still pending */
6442     while (PeekMessage( &msg, hchild, 0, 0, PM_REMOVE )) DispatchMessage( &msg );
6443     ok_sequence( WmEmptySeq, "No WM_PAINT", FALSE );
6444     /* WM_PAINT in parent first */
6445     while (PeekMessage( &msg, 0, 0, 0, PM_REMOVE )) DispatchMessage( &msg );
6446     ok_sequence( WmParentPaintNc, "WmParentPaintNc2", FALSE );
6447
6448     /* no RDW_ERASE in parent still causes RDW_ERASE and RDW_FRAME in child */
6449     flush_sequence();
6450     SetRect( &rect, 0, 0, 30, 30 );
6451     RedrawWindow( hparent, &rect, 0, RDW_INVALIDATE | RDW_ALLCHILDREN );
6452     SetRectRgn( hrgn, 0, 0, 30, 30 );
6453     check_update_rgn( hparent, hrgn );
6454     flush_events();
6455     ok_sequence( WmParentPaintNc, "WmParentPaintNc3", FALSE );
6456
6457     /* validate doesn't cause RDW_NOERASE or RDW_NOFRAME in child */
6458     flush_sequence();
6459     SetRect( &rect, -10, 0, 30, 30 );
6460     RedrawWindow( hchild, &rect, 0, RDW_INVALIDATE | RDW_FRAME | RDW_ERASE );
6461     SetRect( &rect, 0, 0, 20, 20 );
6462     RedrawWindow( hparent, &rect, 0, RDW_VALIDATE | RDW_ALLCHILDREN );
6463     RedrawWindow( hparent, NULL, 0, RDW_UPDATENOW );
6464     ok_sequence( WmChildPaintNc, "WmChildPaintNc", FALSE );
6465
6466     /* validate doesn't cause RDW_NOERASE or RDW_NOFRAME in child */
6467     flush_sequence();
6468     SetRect( &rect, -10, 0, 30, 30 );
6469     RedrawWindow( hchild, &rect, 0, RDW_INVALIDATE | RDW_FRAME | RDW_ERASE );
6470     SetRect( &rect, 0, 0, 100, 100 );
6471     RedrawWindow( hparent, &rect, 0, RDW_VALIDATE | RDW_ALLCHILDREN );
6472     RedrawWindow( hparent, NULL, 0, RDW_UPDATENOW );
6473     ok_sequence( WmEmptySeq, "WmChildPaintNc2", FALSE );
6474     RedrawWindow( hparent, NULL, 0, RDW_ERASENOW );
6475     ok_sequence( WmEmptySeq, "WmChildPaintNc3", FALSE );
6476
6477     /* test RDW_INTERNALPAINT behavior */
6478
6479     flush_sequence();
6480     RedrawWindow( hparent, NULL, 0, RDW_INTERNALPAINT | RDW_NOCHILDREN );
6481     flush_events();
6482     ok_sequence( WmParentOnlyPaint, "WmParentOnlyPaint", FALSE );
6483
6484     RedrawWindow( hparent, NULL, 0, RDW_INTERNALPAINT | RDW_ALLCHILDREN );
6485     flush_events();
6486     ok_sequence( WmParentPaint, "WmParentPaint", FALSE );
6487
6488     RedrawWindow( hparent, NULL, 0, RDW_INTERNALPAINT );
6489     flush_events();
6490     ok_sequence( WmParentOnlyPaint, "WmParentOnlyPaint", FALSE );
6491
6492     assert( GetWindowLong(hparent, GWL_STYLE) & WS_CLIPCHILDREN );
6493     UpdateWindow( hparent );
6494     flush_events();
6495     flush_sequence();
6496     trace("testing SWP_FRAMECHANGED on parent with WS_CLIPCHILDREN\n");
6497     RedrawWindow( hchild, NULL, 0, RDW_INVALIDATE | RDW_ERASE | RDW_FRAME );
6498     SetWindowPos( hparent, 0, 0, 0, 0, 0, SWP_NOSIZE | SWP_NOMOVE |
6499                   SWP_NOACTIVATE | SWP_NOZORDER | SWP_FRAMECHANGED );
6500     flush_events();
6501     ok_sequence(WmSWP_FrameChanged_clip, "SetWindowPos:FrameChanged_clip", FALSE );
6502
6503     UpdateWindow( hparent );
6504     flush_events();
6505     flush_sequence();
6506     trace("testing SWP_FRAMECHANGED|SWP_DEFERERASE on parent with WS_CLIPCHILDREN\n");
6507     RedrawWindow( hchild, NULL, 0, RDW_INVALIDATE | RDW_ERASE | RDW_FRAME );
6508     SetWindowPos( hparent, 0, 0, 0, 0, 0, SWP_NOSIZE | SWP_NOMOVE | SWP_DEFERERASE |
6509                   SWP_NOACTIVATE | SWP_NOZORDER | SWP_FRAMECHANGED );
6510     flush_events();
6511     ok_sequence(WmSWP_FrameChangedDeferErase, "SetWindowPos:FrameChangedDeferErase", FALSE );
6512
6513     SetWindowLong( hparent, GWL_STYLE, GetWindowLong(hparent,GWL_STYLE) & ~WS_CLIPCHILDREN );
6514     ok_sequence( WmSetParentStyle, "WmSetParentStyle", FALSE );
6515     RedrawWindow( hparent, NULL, 0, RDW_INTERNALPAINT );
6516     flush_events();
6517     ok_sequence( WmParentPaint, "WmParentPaint", FALSE );
6518
6519     assert( !(GetWindowLong(hparent, GWL_STYLE) & WS_CLIPCHILDREN) );
6520     UpdateWindow( hparent );
6521     flush_events();
6522     flush_sequence();
6523     trace("testing SWP_FRAMECHANGED on parent without WS_CLIPCHILDREN\n");
6524     RedrawWindow( hchild, NULL, 0, RDW_INVALIDATE | RDW_ERASE | RDW_FRAME );
6525     SetWindowPos( hparent, 0, 0, 0, 0, 0, SWP_NOSIZE | SWP_NOMOVE |
6526                   SWP_NOACTIVATE | SWP_NOZORDER | SWP_FRAMECHANGED );
6527     flush_events();
6528     ok_sequence(WmSWP_FrameChanged_noclip, "SetWindowPos:FrameChanged_noclip", FALSE );
6529
6530     UpdateWindow( hparent );
6531     flush_events();
6532     flush_sequence();
6533     trace("testing SWP_FRAMECHANGED|SWP_DEFERERASE on parent without WS_CLIPCHILDREN\n");
6534     RedrawWindow( hchild, NULL, 0, RDW_INVALIDATE | RDW_ERASE | RDW_FRAME );
6535     SetWindowPos( hparent, 0, 0, 0, 0, 0, SWP_NOSIZE | SWP_NOMOVE | SWP_DEFERERASE |
6536                   SWP_NOACTIVATE | SWP_NOZORDER | SWP_FRAMECHANGED );
6537     flush_events();
6538     ok_sequence(WmSWP_FrameChangedDeferErase, "SetWindowPos:FrameChangedDeferErase", FALSE );
6539
6540     ok(GetWindowLong( hparent, GWL_STYLE ) & WS_VISIBLE, "parent should be visible\n");
6541     ok(GetWindowLong( hchild, GWL_STYLE ) & WS_VISIBLE, "child should be visible\n");
6542
6543     UpdateWindow( hparent );
6544     flush_events();
6545     flush_sequence();
6546     trace("testing SetWindowPos(-10000, -10000) on child\n");
6547     SetWindowPos( hchild, 0, -10000, -10000, 0, 0, SWP_NOSIZE | SWP_NOACTIVATE | SWP_NOZORDER );
6548     check_update_rgn( hchild, 0 );
6549     flush_events();
6550
6551 #if 0 /* this one doesn't pass under Wine yet */
6552     UpdateWindow( hparent );
6553     flush_events();
6554     flush_sequence();
6555     trace("testing ShowWindow(SW_MINIMIZE) on child\n");
6556     ShowWindow( hchild, SW_MINIMIZE );
6557     check_update_rgn( hchild, 0 );
6558     flush_events();
6559 #endif
6560
6561     UpdateWindow( hparent );
6562     flush_events();
6563     flush_sequence();
6564     trace("testing SetWindowPos(-10000, -10000) on parent\n");
6565     SetWindowPos( hparent, 0, -10000, -10000, 0, 0, SWP_NOSIZE | SWP_NOACTIVATE | SWP_NOZORDER );
6566     check_update_rgn( hparent, 0 );
6567     flush_events();
6568
6569     log_all_parent_messages--;
6570     DestroyWindow( hparent );
6571     ok(!IsWindow(hchild), "child must be destroyed with its parent\n");
6572
6573     /* tests for moving windows off-screen (needs simple WS_POPUP windows) */
6574
6575     hparent = CreateWindowExA(0, "TestParentClass", "Test parent", WS_POPUP | WS_VISIBLE,
6576                               100, 100, 200, 200, 0, 0, 0, NULL);
6577     ok (hparent != 0, "Failed to create parent window\n");
6578
6579     hchild = CreateWindowExA(0, "TestWindowClass", "Test child", WS_CHILD | WS_VISIBLE,
6580                            10, 10, 100, 100, hparent, 0, 0, NULL);
6581     ok (hchild != 0, "Failed to create child window\n");
6582
6583     ShowWindow( hparent, SW_SHOW );
6584     UpdateWindow( hparent );
6585     UpdateWindow( hchild );
6586     flush_events();
6587     flush_sequence();
6588
6589     /* moving child outside of parent boundaries changes update region */
6590     SetRect( &rect, 0, 0, 40, 40 );
6591     RedrawWindow( hchild, &rect, 0, RDW_INVALIDATE | RDW_ERASE );
6592     SetRectRgn( hrgn, 0, 0, 40, 40 );
6593     check_update_rgn( hchild, hrgn );
6594     MoveWindow( hchild, -10, 10, 100, 100, FALSE );
6595     SetRectRgn( hrgn, 10, 0, 40, 40 );
6596     check_update_rgn( hchild, hrgn );
6597     MoveWindow( hchild, -10, -10, 100, 100, FALSE );
6598     SetRectRgn( hrgn, 10, 10, 40, 40 );
6599     check_update_rgn( hchild, hrgn );
6600
6601     /* moving parent off-screen does too */
6602     SetRect( &rect, 0, 0, 100, 100 );
6603     RedrawWindow( hparent, &rect, 0, RDW_INVALIDATE | RDW_ERASE | RDW_NOCHILDREN );
6604     SetRectRgn( hrgn, 0, 0, 100, 100 );
6605     check_update_rgn( hparent, hrgn );
6606     SetRectRgn( hrgn, 10, 10, 40, 40 );
6607     check_update_rgn( hchild, hrgn );
6608     MoveWindow( hparent, -20, -20, 200, 200, FALSE );
6609     SetRectRgn( hrgn, 20, 20, 100, 100 );
6610     check_update_rgn( hparent, hrgn );
6611     SetRectRgn( hrgn, 30, 30, 40, 40 );
6612     check_update_rgn( hchild, hrgn );
6613
6614     /* invalidated region is cropped by the parent rects */
6615     SetRect( &rect, 0, 0, 50, 50 );
6616     RedrawWindow( hchild, &rect, 0, RDW_INVALIDATE | RDW_ERASE );
6617     SetRectRgn( hrgn, 30, 30, 50, 50 );
6618     check_update_rgn( hchild, hrgn );
6619
6620     DestroyWindow( hparent );
6621     ok(!IsWindow(hchild), "child must be destroyed with its parent\n");
6622     flush_sequence();
6623
6624     DeleteObject( hrgn );
6625     DeleteObject( hrgn2 );
6626 }
6627
6628 struct wnd_event
6629 {
6630     HWND hwnd;
6631     HANDLE grand_child;
6632     HANDLE start_event;
6633     HANDLE stop_event;
6634 };
6635
6636 static DWORD WINAPI thread_proc(void *param)
6637 {
6638     MSG msg;
6639     struct wnd_event *wnd_event = param;
6640
6641     wnd_event->hwnd = CreateWindowExA(0, "TestWindowClass", "window caption text", WS_OVERLAPPEDWINDOW,
6642                                       100, 100, 200, 200, 0, 0, 0, NULL);
6643     ok(wnd_event->hwnd != 0, "Failed to create overlapped window\n");
6644
6645     SetEvent(wnd_event->start_event);
6646
6647     while (GetMessage(&msg, 0, 0, 0))
6648     {
6649         TranslateMessage(&msg);
6650         DispatchMessage(&msg);
6651     }
6652
6653     ok(IsWindow(wnd_event->hwnd), "window should still exist\n");
6654
6655     return 0;
6656 }
6657
6658 static DWORD CALLBACK create_grand_child_thread( void *param )
6659 {
6660     struct wnd_event *wnd_event = param;
6661     HWND hchild;
6662     MSG msg;
6663
6664     hchild = CreateWindowExA(0, "TestWindowClass", "Test child",
6665                              WS_CHILD | WS_VISIBLE, 0, 0, 10, 10, wnd_event->hwnd, 0, 0, NULL);
6666     ok (hchild != 0, "Failed to create child window\n");
6667     flush_events();
6668     flush_sequence();
6669     SetEvent( wnd_event->start_event );
6670
6671     for (;;)
6672     {
6673         MsgWaitForMultipleObjects(0, NULL, FALSE, 1000, QS_ALLINPUT);
6674         if (!IsWindow( hchild )) break;  /* will be destroyed when parent thread exits */
6675         while (PeekMessageA(&msg, 0, 0, 0, PM_REMOVE)) DispatchMessage(&msg);
6676     }
6677     return 0;
6678 }
6679
6680 static DWORD CALLBACK create_child_thread( void *param )
6681 {
6682     struct wnd_event *wnd_event = param;
6683     struct wnd_event child_event;
6684     DWORD ret, tid;
6685     MSG msg;
6686
6687     child_event.hwnd = CreateWindowExA(0, "TestWindowClass", "Test child",
6688                              WS_CHILD | WS_VISIBLE, 0, 0, 10, 10, wnd_event->hwnd, 0, 0, NULL);
6689     ok (child_event.hwnd != 0, "Failed to create child window\n");
6690     SetFocus( child_event.hwnd );
6691     flush_events();
6692     flush_sequence();
6693     child_event.start_event = wnd_event->start_event;
6694     wnd_event->grand_child = CreateThread(NULL, 0, create_grand_child_thread, &child_event, 0, &tid);
6695     for (;;)
6696     {
6697         DWORD ret = MsgWaitForMultipleObjects(1, &child_event.start_event, FALSE, 1000, QS_SENDMESSAGE);
6698         if (ret != 1) break;
6699         while (PeekMessageA(&msg, 0, 0, 0, PM_REMOVE)) DispatchMessage(&msg);
6700     }
6701     ret = WaitForSingleObject( wnd_event->stop_event, 5000 );
6702     ok( !ret, "WaitForSingleObject failed %x\n", ret );
6703     return 0;
6704 }
6705
6706 static void test_interthread_messages(void)
6707 {
6708     HANDLE hThread;
6709     DWORD tid;
6710     WNDPROC proc;
6711     MSG msg;
6712     char buf[256];
6713     int len, expected_len;
6714     struct wnd_event wnd_event;
6715     BOOL ret;
6716
6717     wnd_event.start_event = CreateEventW(NULL, 0, 0, NULL);
6718     if (!wnd_event.start_event)
6719     {
6720         win_skip("skipping interthread message test under win9x\n");
6721         return;
6722     }
6723
6724     hThread = CreateThread(NULL, 0, thread_proc, &wnd_event, 0, &tid);
6725     ok(hThread != NULL, "CreateThread failed, error %d\n", GetLastError());
6726
6727     ok(WaitForSingleObject(wnd_event.start_event, INFINITE) == WAIT_OBJECT_0, "WaitForSingleObject failed\n");
6728
6729     CloseHandle(wnd_event.start_event);
6730
6731     SetLastError(0xdeadbeef);
6732     ok(!DestroyWindow(wnd_event.hwnd), "DestroyWindow succeeded\n");
6733     ok(GetLastError() == ERROR_ACCESS_DENIED || GetLastError() == 0xdeadbeef,
6734        "wrong error code %d\n", GetLastError());
6735
6736     proc = (WNDPROC)GetWindowLongPtrA(wnd_event.hwnd, GWLP_WNDPROC);
6737     ok(proc != NULL, "GetWindowLongPtrA(GWLP_WNDPROC) error %d\n", GetLastError());
6738
6739     expected_len = lstrlenA("window caption text");
6740     memset(buf, 0, sizeof(buf));
6741     SetLastError(0xdeadbeef);
6742     len = CallWindowProcA(proc, wnd_event.hwnd, WM_GETTEXT, sizeof(buf), (LPARAM)buf);
6743     ok(len == expected_len, "CallWindowProcA(WM_GETTEXT) error %d, len %d, expected len %d\n", GetLastError(), len, expected_len);
6744     ok(!lstrcmpA(buf, "window caption text"), "window text mismatch\n");
6745
6746     msg.hwnd = wnd_event.hwnd;
6747     msg.message = WM_GETTEXT;
6748     msg.wParam = sizeof(buf);
6749     msg.lParam = (LPARAM)buf;
6750     memset(buf, 0, sizeof(buf));
6751     SetLastError(0xdeadbeef);
6752     len = DispatchMessageA(&msg);
6753     ok((!len && GetLastError() == ERROR_MESSAGE_SYNC_ONLY) || broken(len), /* nt4 */
6754        "DispatchMessageA(WM_GETTEXT) succeeded on another thread window: ret %d, error %d\n", len, GetLastError());
6755
6756     /* the following test causes an exception in user.exe under win9x */
6757     msg.hwnd = wnd_event.hwnd;
6758     msg.message = WM_TIMER;
6759     msg.wParam = 0;
6760     msg.lParam = GetWindowLongPtrA(wnd_event.hwnd, GWLP_WNDPROC);
6761     SetLastError(0xdeadbeef);
6762     len = DispatchMessageA(&msg);
6763     ok(!len && GetLastError() == 0xdeadbeef,
6764        "DispatchMessageA(WM_TIMER) failed on another thread window: ret %d, error %d\n", len, GetLastError());
6765
6766     ret = PostMessageA(wnd_event.hwnd, WM_QUIT, 0, 0);
6767     ok( ret, "PostMessageA(WM_QUIT) error %d\n", GetLastError());
6768
6769     ok(WaitForSingleObject(hThread, INFINITE) == WAIT_OBJECT_0, "WaitForSingleObject failed\n");
6770     CloseHandle(hThread);
6771
6772     ok(!IsWindow(wnd_event.hwnd), "window should be destroyed on thread exit\n");
6773
6774     wnd_event.hwnd = CreateWindowExA(0, "TestParentClass", "Test parent", WS_OVERLAPPEDWINDOW | WS_VISIBLE,
6775                               100, 100, 200, 200, 0, 0, 0, NULL);
6776     ok (wnd_event.hwnd != 0, "Failed to create parent window\n");
6777     flush_sequence();
6778     log_all_parent_messages++;
6779     wnd_event.start_event = CreateEventA( NULL, TRUE, FALSE, NULL );
6780     wnd_event.stop_event = CreateEventA( NULL, TRUE, FALSE, NULL );
6781     hThread = CreateThread( NULL, 0, create_child_thread, &wnd_event, 0, &tid );
6782     for (;;)
6783     {
6784         ret = MsgWaitForMultipleObjects(1, &wnd_event.start_event, FALSE, 1000, QS_SENDMESSAGE);
6785         if (ret != 1) break;
6786         while (PeekMessageA(&msg, 0, 0, 0, PM_REMOVE)) DispatchMessage(&msg);
6787     }
6788     ok( !ret, "MsgWaitForMultipleObjects failed %x\n", ret );
6789     /* now wait for the thread without processing messages; this shouldn't deadlock */
6790     SetEvent( wnd_event.stop_event );
6791     ret = WaitForSingleObject( hThread, 5000 );
6792     ok( !ret, "WaitForSingleObject failed %x\n", ret );
6793     CloseHandle( hThread );
6794
6795     ret = WaitForSingleObject( wnd_event.grand_child, 5000 );
6796     ok( !ret, "WaitForSingleObject failed %x\n", ret );
6797     CloseHandle( wnd_event.grand_child );
6798
6799     CloseHandle( wnd_event.start_event );
6800     CloseHandle( wnd_event.stop_event );
6801     flush_events();
6802     ok_sequence(WmExitThreadSeq, "destroy child on thread exit", FALSE);
6803     log_all_parent_messages--;
6804     DestroyWindow( wnd_event.hwnd );
6805 }
6806
6807
6808 static const struct message WmVkN[] = {
6809     { HCBT_KEYSKIPPED, hook|wparam|lparam|optional, 'N', 1 }, /* XP */
6810     { WM_KEYDOWN, wparam|lparam, 'N', 1 },
6811     { WM_KEYDOWN, sent|wparam|lparam, 'N', 1 },
6812     { WM_CHAR, wparam|lparam, 'n', 1 },
6813     { WM_COMMAND, sent|wparam|lparam, MAKEWPARAM(1002,1), 0 },
6814     { HCBT_KEYSKIPPED, hook|wparam|lparam|optional, 'N', 0xc0000001 }, /* XP */
6815     { WM_KEYUP, wparam|lparam, 'N', 0xc0000001 },
6816     { WM_KEYUP, sent|wparam|lparam, 'N', 0xc0000001 },
6817     { 0 }
6818 };
6819 static const struct message WmShiftVkN[] = {
6820     { HCBT_KEYSKIPPED, hook|wparam|lparam|optional, VK_SHIFT, 1 }, /* XP */
6821     { WM_KEYDOWN, wparam|lparam, VK_SHIFT, 1 },
6822     { WM_KEYDOWN, sent|wparam|lparam, VK_SHIFT, 1 },
6823     { HCBT_KEYSKIPPED, hook|wparam|lparam|optional, 'N', 1 }, /* XP */
6824     { WM_KEYDOWN, wparam|lparam, 'N', 1 },
6825     { WM_KEYDOWN, sent|wparam|lparam, 'N', 1 },
6826     { WM_CHAR, wparam|lparam, 'N', 1 },
6827     { WM_COMMAND, sent|wparam|lparam, MAKEWPARAM(1001,1), 0 },
6828     { HCBT_KEYSKIPPED, hook|wparam|lparam|optional, 'N', 0xc0000001 }, /* XP */
6829     { WM_KEYUP, wparam|lparam, 'N', 0xc0000001 },
6830     { WM_KEYUP, sent|wparam|lparam, 'N', 0xc0000001 },
6831     { HCBT_KEYSKIPPED, hook|wparam|lparam|optional, VK_SHIFT, 0xc0000001 }, /* XP */
6832     { WM_KEYUP, wparam|lparam, VK_SHIFT, 0xc0000001 },
6833     { WM_KEYUP, sent|wparam|lparam, VK_SHIFT, 0xc0000001 },
6834     { 0 }
6835 };
6836 static const struct message WmCtrlVkN[] = {
6837     { HCBT_KEYSKIPPED, hook|wparam|lparam|optional, VK_CONTROL, 1 }, /* XP */
6838     { WM_KEYDOWN, wparam|lparam, VK_CONTROL, 1 },
6839     { WM_KEYDOWN, sent|wparam|lparam, VK_CONTROL, 1 },
6840     { HCBT_KEYSKIPPED, hook|wparam|lparam|optional, 'N', 1 }, /* XP */
6841     { WM_KEYDOWN, wparam|lparam, 'N', 1 },
6842     { WM_KEYDOWN, sent|wparam|lparam, 'N', 1 },
6843     { WM_CHAR, wparam|lparam, 0x000e, 1 },
6844     { WM_COMMAND, sent|wparam|lparam, MAKEWPARAM(1000,1), 0 },
6845     { HCBT_KEYSKIPPED, hook|wparam|lparam|optional, 'N', 0xc0000001 }, /* XP */
6846     { WM_KEYUP, wparam|lparam, 'N', 0xc0000001 },
6847     { WM_KEYUP, sent|wparam|lparam, 'N', 0xc0000001 },
6848     { HCBT_KEYSKIPPED, hook|wparam|lparam|optional, VK_CONTROL, 0xc0000001 }, /* XP */
6849     { WM_KEYUP, wparam|lparam, VK_CONTROL, 0xc0000001 },
6850     { WM_KEYUP, sent|wparam|lparam, VK_CONTROL, 0xc0000001 },
6851     { 0 }
6852 };
6853 static const struct message WmCtrlVkN_2[] = {
6854     { HCBT_KEYSKIPPED, hook|wparam|lparam|optional, VK_CONTROL, 1 }, /* XP */
6855     { WM_KEYDOWN, wparam|lparam, VK_CONTROL, 1 },
6856     { WM_KEYDOWN, sent|wparam|lparam, VK_CONTROL, 1 },
6857     { HCBT_KEYSKIPPED, hook|wparam|lparam|optional, 'N', 1 }, /* XP */
6858     { WM_KEYDOWN, wparam|lparam, 'N', 1 },
6859     { WM_COMMAND, sent|wparam|lparam, MAKEWPARAM(1000,1), 0 },
6860     { HCBT_KEYSKIPPED, hook|wparam|lparam|optional, 'N', 0xc0000001 }, /* XP */
6861     { WM_KEYUP, wparam|lparam, 'N', 0xc0000001 },
6862     { WM_KEYUP, sent|wparam|lparam, 'N', 0xc0000001 },
6863     { HCBT_KEYSKIPPED, hook|wparam|lparam|optional, VK_CONTROL, 0xc0000001 }, /* XP */
6864     { WM_KEYUP, wparam|lparam, VK_CONTROL, 0xc0000001 },
6865     { WM_KEYUP, sent|wparam|lparam, VK_CONTROL, 0xc0000001 },
6866     { 0 }
6867 };
6868 static const struct message WmAltVkN[] = {
6869     { HCBT_KEYSKIPPED, hook|wparam|lparam|optional, VK_MENU, 0x20000001 }, /* XP */
6870     { WM_SYSKEYDOWN, wparam|lparam, VK_MENU, 0x20000001 },
6871     { WM_SYSKEYDOWN, sent|wparam|lparam, VK_MENU, 0x20000001 },
6872     { HCBT_KEYSKIPPED, hook|wparam|lparam|optional, 'N', 0x20000001 }, /* XP */
6873     { WM_SYSKEYDOWN, wparam|lparam, 'N', 0x20000001 },
6874     { WM_SYSKEYDOWN, sent|wparam|lparam, 'N', 0x20000001 },
6875     { WM_SYSCHAR, wparam|lparam, 'n', 0x20000001 },
6876     { WM_SYSCHAR, sent|wparam|lparam, 'n', 0x20000001 },
6877     { WM_SYSCOMMAND, sent|defwinproc|wparam|lparam, SC_KEYMENU, 'n' },
6878     { HCBT_SYSCOMMAND, hook },
6879     { WM_ENTERMENULOOP, sent|defwinproc|wparam|lparam, 0, 0 },
6880     { EVENT_SYSTEM_CAPTURESTART, winevent_hook|wparam|lparam, 0, 0 },
6881     { 0x00AE, sent|defwinproc|optional }, /* XP */
6882     { WM_GETTEXT, sent|defwinproc|optional }, /* XP */
6883     { WM_INITMENU, sent|defwinproc },
6884     { EVENT_SYSTEM_MENUSTART, winevent_hook|wparam|lparam, OBJID_SYSMENU, 0 },
6885     { WM_MENUCHAR, sent|defwinproc|wparam, MAKEWPARAM('n',MF_SYSMENU) },
6886     { EVENT_SYSTEM_CAPTUREEND, winevent_hook|wparam|lparam, 0, 0 },
6887     { WM_CAPTURECHANGED, sent|defwinproc },
6888     { WM_MENUSELECT, sent|defwinproc|wparam, MAKEWPARAM(0,0xffff) },
6889     { EVENT_SYSTEM_MENUEND, winevent_hook|wparam|lparam, OBJID_SYSMENU, 0 },
6890     { WM_EXITMENULOOP, sent|defwinproc },
6891     { WM_MENUSELECT, sent|defwinproc|wparam|optional, MAKEWPARAM(0,0xffff) }, /* Win95 bug */
6892     { WM_EXITMENULOOP, sent|defwinproc|optional }, /* Win95 bug */
6893     { HCBT_KEYSKIPPED, hook|wparam|lparam|optional, 'N', 0xe0000001 }, /* XP */
6894     { WM_SYSKEYUP, wparam|lparam, 'N', 0xe0000001 },
6895     { WM_SYSKEYUP, sent|wparam|lparam, 'N', 0xe0000001 },
6896     { HCBT_KEYSKIPPED, hook|wparam|lparam|optional, VK_MENU, 0xc0000001 }, /* XP */
6897     { WM_KEYUP, wparam|lparam, VK_MENU, 0xc0000001 },
6898     { WM_KEYUP, sent|wparam|lparam, VK_MENU, 0xc0000001 },
6899     { 0 }
6900 };
6901 static const struct message WmAltVkN_2[] = {
6902     { HCBT_KEYSKIPPED, hook|wparam|lparam|optional, VK_MENU, 0x20000001 }, /* XP */
6903     { WM_SYSKEYDOWN, wparam|lparam, VK_MENU, 0x20000001 },
6904     { WM_SYSKEYDOWN, sent|wparam|lparam, VK_MENU, 0x20000001 },
6905     { HCBT_KEYSKIPPED, hook|wparam|lparam|optional, 'N', 0x20000001 }, /* XP */
6906     { WM_SYSKEYDOWN, wparam|lparam, 'N', 0x20000001 },
6907     { WM_COMMAND, sent|wparam|lparam, MAKEWPARAM(1003,1), 0 },
6908     { HCBT_KEYSKIPPED, hook|wparam|lparam|optional, 'N', 0xe0000001 }, /* XP */
6909     { WM_SYSKEYUP, wparam|lparam, 'N', 0xe0000001 },
6910     { WM_SYSKEYUP, sent|wparam|lparam, 'N', 0xe0000001 },
6911     { HCBT_KEYSKIPPED, hook|wparam|lparam|optional, VK_MENU, 0xc0000001 }, /* XP */
6912     { WM_KEYUP, wparam|lparam, VK_MENU, 0xc0000001 },
6913     { WM_KEYUP, sent|wparam|lparam, VK_MENU, 0xc0000001 },
6914     { 0 }
6915 };
6916 static const struct message WmCtrlAltVkN[] = {
6917     { HCBT_KEYSKIPPED, hook|wparam|lparam|optional, VK_CONTROL, 1 }, /* XP */
6918     { WM_KEYDOWN, wparam|lparam, VK_CONTROL, 1 },
6919     { WM_KEYDOWN, sent|wparam|lparam, VK_CONTROL, 1 },
6920     { HCBT_KEYSKIPPED, hook|wparam|lparam|optional, VK_MENU, 0x20000001 }, /* XP */
6921     { WM_KEYDOWN, wparam|lparam, VK_MENU, 0x20000001 },
6922     { WM_KEYDOWN, sent|wparam|lparam, VK_MENU, 0x20000001 },
6923     { HCBT_KEYSKIPPED, hook|wparam|lparam|optional, 'N', 0x20000001 }, /* XP */
6924     { WM_KEYDOWN, wparam|lparam, 'N', 0x20000001 },
6925     { WM_KEYDOWN, sent|wparam|lparam, 'N', 0x20000001 },
6926     { WM_CHAR, optional },
6927     { WM_CHAR, sent|optional },
6928     { HCBT_KEYSKIPPED, hook|wparam|lparam|optional, 'N', 0xe0000001 }, /* XP */
6929     { WM_KEYUP, wparam|lparam, 'N', 0xe0000001 },
6930     { WM_KEYUP, sent|wparam|lparam, 'N', 0xe0000001 },
6931     { HCBT_KEYSKIPPED, hook|wparam|lparam|optional, VK_MENU, 0xc0000001 }, /* XP */
6932     { WM_KEYUP, wparam|lparam, VK_MENU, 0xc0000001 },
6933     { WM_KEYUP, sent|wparam|lparam, VK_MENU, 0xc0000001 },
6934     { HCBT_KEYSKIPPED, hook|wparam|lparam|optional, VK_CONTROL, 0xc0000001 }, /* XP */
6935     { WM_KEYUP, wparam|lparam, VK_CONTROL, 0xc0000001 },
6936     { WM_KEYUP, sent|wparam|lparam, VK_CONTROL, 0xc0000001 },
6937     { 0 }
6938 };
6939 static const struct message WmCtrlShiftVkN[] = {
6940     { HCBT_KEYSKIPPED, hook|wparam|lparam|optional, VK_CONTROL, 1 }, /* XP */
6941     { WM_KEYDOWN, wparam|lparam, VK_CONTROL, 1 },
6942     { WM_KEYDOWN, sent|wparam|lparam, VK_CONTROL, 1 },
6943     { HCBT_KEYSKIPPED, hook|wparam|lparam|optional, VK_SHIFT, 1 }, /* XP */
6944     { WM_KEYDOWN, wparam|lparam, VK_SHIFT, 1 },
6945     { WM_KEYDOWN, sent|wparam|lparam, VK_SHIFT, 1 },
6946     { HCBT_KEYSKIPPED, hook|wparam|lparam|optional, 'N', 1 }, /* XP */
6947     { WM_KEYDOWN, wparam|lparam, 'N', 1 },
6948     { WM_COMMAND, sent|wparam|lparam, MAKEWPARAM(1004,1), 0 },
6949     { HCBT_KEYSKIPPED, hook|wparam|lparam|optional, 'N', 0xc0000001 }, /* XP */
6950     { WM_KEYUP, wparam|lparam, 'N', 0xc0000001 },
6951     { WM_KEYUP, sent|wparam|lparam, 'N', 0xc0000001 },
6952     { HCBT_KEYSKIPPED, hook|wparam|lparam|optional, VK_SHIFT, 0xc0000001 }, /* XP */
6953     { WM_KEYUP, wparam|lparam, VK_SHIFT, 0xc0000001 },
6954     { WM_KEYUP, sent|wparam|lparam, VK_SHIFT, 0xc0000001 },
6955     { HCBT_KEYSKIPPED, hook|wparam|lparam|optional, VK_CONTROL, 0xc0000001 }, /* XP */
6956     { WM_KEYUP, wparam|lparam, VK_CONTROL, 0xc0000001 },
6957     { WM_KEYUP, sent|wparam|lparam, VK_CONTROL, 0xc0000001 },
6958     { 0 }
6959 };
6960 static const struct message WmCtrlAltShiftVkN[] = {
6961     { HCBT_KEYSKIPPED, hook|wparam|lparam|optional, VK_CONTROL, 1 }, /* XP */
6962     { WM_KEYDOWN, wparam|lparam, VK_CONTROL, 1 },
6963     { WM_KEYDOWN, sent|wparam|lparam, VK_CONTROL, 1 },
6964     { HCBT_KEYSKIPPED, hook|wparam|lparam|optional, VK_MENU, 0x20000001 }, /* XP */
6965     { WM_KEYDOWN, wparam|lparam, VK_MENU, 0x20000001 },
6966     { WM_KEYDOWN, sent|wparam|lparam, VK_MENU, 0x20000001 },
6967     { HCBT_KEYSKIPPED, hook|wparam|lparam|optional, VK_SHIFT, 0x20000001 }, /* XP */
6968     { WM_KEYDOWN, wparam|lparam, VK_SHIFT, 0x20000001 },
6969     { WM_KEYDOWN, sent|wparam|lparam, VK_SHIFT, 0x20000001 },
6970     { HCBT_KEYSKIPPED, hook|wparam|lparam|optional, 'N', 0x20000001 }, /* XP */
6971     { WM_KEYDOWN, wparam|lparam, 'N', 0x20000001 },
6972     { WM_COMMAND, sent|wparam|lparam, MAKEWPARAM(1005,1), 0 },
6973     { HCBT_KEYSKIPPED, hook|wparam|lparam|optional, 'N', 0xe0000001 }, /* XP */
6974     { WM_KEYUP, wparam|lparam, 'N', 0xe0000001 },
6975     { WM_KEYUP, sent|wparam|lparam, 'N', 0xe0000001 },
6976     { HCBT_KEYSKIPPED, hook|wparam|lparam|optional, VK_SHIFT, 0xe0000001 }, /* XP */
6977     { WM_KEYUP, wparam|lparam, VK_SHIFT, 0xe0000001 },
6978     { WM_KEYUP, sent|wparam|lparam, VK_SHIFT, 0xe0000001 },
6979     { HCBT_KEYSKIPPED, hook|wparam|lparam|optional, VK_MENU, 0xc0000001 }, /* XP */
6980     { WM_KEYUP, wparam|lparam, VK_MENU, 0xc0000001 },
6981     { WM_KEYUP, sent|wparam|lparam, VK_MENU, 0xc0000001 },
6982     { HCBT_KEYSKIPPED, hook|wparam|lparam|optional, VK_CONTROL, 0xc0000001 }, /* XP */
6983     { WM_KEYUP, wparam|lparam, VK_CONTROL, 0xc0000001 },
6984     { WM_KEYUP, sent|wparam|lparam, VK_CONTROL, 0xc0000001 },
6985     { 0 }
6986 };
6987 static const struct message WmAltPressRelease[] = {
6988     { HCBT_KEYSKIPPED, hook|wparam|lparam|optional, VK_MENU, 0x20000001 }, /* XP */
6989     { WM_SYSKEYDOWN, wparam|lparam, VK_MENU, 0x20000001 },
6990     { WM_SYSKEYDOWN, sent|wparam|lparam, VK_MENU, 0x20000001 },
6991     { HCBT_KEYSKIPPED, hook|wparam|lparam|optional, VK_MENU, 0xc0000001 }, /* XP */
6992     { WM_SYSKEYUP, wparam|lparam, VK_MENU, 0xc0000001 },
6993     { WM_SYSKEYUP, sent|wparam|lparam, VK_MENU, 0xc0000001 },
6994     { WM_SYSCOMMAND, sent|defwinproc|wparam|lparam, SC_KEYMENU, 0 },
6995     { HCBT_SYSCOMMAND, hook },
6996     { WM_ENTERMENULOOP, sent|defwinproc|wparam|lparam, 0, 0 },
6997     { EVENT_SYSTEM_CAPTURESTART, winevent_hook|wparam|lparam, 0, 0 },
6998     { WM_INITMENU, sent|defwinproc },
6999     { EVENT_SYSTEM_MENUSTART, winevent_hook|wparam|lparam, OBJID_SYSMENU, 0 },
7000     { WM_MENUSELECT, sent|defwinproc|wparam, MAKEWPARAM(0,MF_SYSMENU|MF_POPUP|MF_HILITE) },
7001     { EVENT_OBJECT_FOCUS, winevent_hook|wparam|lparam, OBJID_SYSMENU, 1 },
7002
7003     { HCBT_KEYSKIPPED, hook|wparam|lparam|optional, VK_MENU, 0x30000001 }, /* XP */
7004
7005     { EVENT_OBJECT_FOCUS, winevent_hook|wparam|lparam, OBJID_SYSMENU, 0 },
7006     { EVENT_SYSTEM_CAPTUREEND, winevent_hook|wparam|lparam, 0, 0, },
7007     { WM_CAPTURECHANGED, sent|defwinproc },
7008     { WM_MENUSELECT, sent|defwinproc|wparam|optional, MAKEWPARAM(0,0xffff) },
7009     { EVENT_SYSTEM_MENUEND, winevent_hook|wparam|lparam, OBJID_SYSMENU, 0 },
7010     { WM_EXITMENULOOP, sent|defwinproc },
7011     { HCBT_KEYSKIPPED, hook|wparam|lparam|optional, VK_MENU, 0xc0000001 }, /* XP */
7012     { WM_SYSKEYUP, wparam|lparam, VK_MENU, 0xc0000001 },
7013     { WM_SYSKEYUP, sent|wparam|lparam, VK_MENU, 0xc0000001 },
7014     { 0 }
7015 };
7016 static const struct message WmShiftMouseButton[] = {
7017     { HCBT_KEYSKIPPED, hook|wparam|lparam|optional, VK_SHIFT, 1 }, /* XP */
7018     { WM_KEYDOWN, wparam|lparam, VK_SHIFT, 1 },
7019     { WM_KEYDOWN, sent|wparam|lparam, VK_SHIFT, 1 },
7020     { WM_MOUSEMOVE, wparam|optional, 0, 0 },
7021     { WM_MOUSEMOVE, sent|wparam|optional, 0, 0 },
7022     { WM_LBUTTONDOWN, wparam, MK_LBUTTON|MK_SHIFT, 0 },
7023     { WM_LBUTTONDOWN, sent|wparam, MK_LBUTTON|MK_SHIFT, 0 },
7024     { WM_LBUTTONUP, wparam, MK_SHIFT, 0 },
7025     { WM_LBUTTONUP, sent|wparam, MK_SHIFT, 0 },
7026     { HCBT_KEYSKIPPED, hook|wparam|lparam|optional, VK_SHIFT, 0xc0000001 }, /* XP */
7027     { WM_KEYUP, wparam|lparam, VK_SHIFT, 0xc0000001 },
7028     { WM_KEYUP, sent|wparam|lparam, VK_SHIFT, 0xc0000001 },
7029     { 0 }
7030 };
7031 static const struct message WmF1Seq[] = {
7032     { HCBT_KEYSKIPPED, hook|wparam|lparam|optional, VK_F1, 1 }, /* XP */
7033     { WM_KEYDOWN, wparam|lparam, VK_F1, 1 },
7034     { WM_KEYDOWN, sent|wparam|lparam, VK_F1, 0x00000001 },
7035     { WM_KEYF1, wparam|lparam, 0, 0 },
7036     { WM_KEYF1, sent|wparam|lparam, 0, 0 },
7037     { WM_HELP, sent|defwinproc },
7038     { HCBT_KEYSKIPPED, hook|wparam|lparam|optional, VK_F1, 0xc0000001 }, /* XP */
7039     { WM_KEYUP, wparam|lparam, VK_F1, 0xc0000001 },
7040     { WM_KEYUP, sent|wparam|lparam, VK_F1, 0xc0000001 },
7041     { 0 }
7042 };
7043 static const struct message WmVkAppsSeq[] = {
7044     { HCBT_KEYSKIPPED, hook|wparam|lparam|optional, VK_APPS, 1 }, /* XP */
7045     { WM_KEYDOWN, wparam|lparam, VK_APPS, 1 },
7046     { WM_KEYDOWN, sent|wparam|lparam, VK_APPS, 0x00000001 },
7047     { HCBT_KEYSKIPPED, hook|wparam|lparam|optional, VK_APPS, 0xc0000001 }, /* XP */
7048     { WM_KEYUP, wparam|lparam, VK_APPS, 0xc0000001 },
7049     { WM_KEYUP, sent|wparam|lparam, VK_APPS, 0xc0000001 },
7050     { WM_CONTEXTMENU, lparam, /*hwnd*/0, -1 },
7051     { WM_CONTEXTMENU, sent|lparam, /*hwnd*/0, -1 },
7052     { 0 }
7053 };
7054 static const struct message WmVkF10Seq[] = {
7055     { HCBT_KEYSKIPPED, hook|wparam|lparam|optional, VK_F10, 1 }, /* XP */
7056     { WM_SYSKEYDOWN, wparam|lparam, VK_F10, 1 },
7057     { WM_SYSKEYDOWN, sent|wparam|lparam, VK_F10, 0x00000001 },
7058     { HCBT_KEYSKIPPED, hook|wparam|lparam|optional, VK_F10, 0xc0000001 }, /* XP */
7059     { WM_SYSKEYUP, wparam|lparam, VK_F10, 0xc0000001 },
7060     { WM_SYSKEYUP, sent|wparam|lparam, VK_F10, 0xc0000001 },
7061     { WM_SYSCOMMAND, sent|defwinproc|wparam, SC_KEYMENU },
7062     { HCBT_SYSCOMMAND, hook },
7063     { WM_ENTERMENULOOP, sent|defwinproc|wparam|lparam, 0, 0 },
7064     { EVENT_SYSTEM_CAPTURESTART, winevent_hook|wparam|lparam, 0, 0 },
7065     { WM_INITMENU, sent|defwinproc },
7066     { EVENT_SYSTEM_MENUSTART, winevent_hook|wparam|lparam, OBJID_SYSMENU, 0 },
7067     { WM_MENUSELECT, sent|defwinproc|wparam, MAKEWPARAM(0,MF_SYSMENU|MF_POPUP|MF_HILITE) },
7068     { EVENT_OBJECT_FOCUS, winevent_hook|wparam|lparam, OBJID_SYSMENU, 1 },
7069
7070     { HCBT_KEYSKIPPED, hook|wparam|lparam|optional, VK_F10, 0x10000001 }, /* XP */
7071
7072     { HCBT_KEYSKIPPED, hook|wparam|lparam|optional, VK_F10, 1 }, /* XP */
7073     { EVENT_OBJECT_FOCUS, winevent_hook|wparam|lparam, OBJID_SYSMENU, 0 },
7074     { EVENT_SYSTEM_CAPTUREEND, winevent_hook|wparam|lparam, 0, 0, },
7075     { WM_CAPTURECHANGED, sent|defwinproc },
7076     { WM_MENUSELECT, sent|defwinproc|wparam|optional, MAKEWPARAM(0,0xffff) },
7077     { EVENT_SYSTEM_MENUEND, winevent_hook|wparam|lparam, OBJID_SYSMENU, 0 },
7078     { WM_EXITMENULOOP, sent|defwinproc },
7079     { HCBT_KEYSKIPPED, hook|wparam|lparam|optional, VK_F10, 0xc0000001 }, /* XP */
7080     { WM_SYSKEYUP, wparam|lparam, VK_F10, 0xc0000001 },
7081     { WM_SYSKEYUP, sent|wparam|lparam, VK_F10, 0xc0000001 },
7082     { 0 }
7083 };
7084 static const struct message WmShiftF10Seq[] = {
7085     { HCBT_KEYSKIPPED, hook|wparam|lparam|optional, VK_SHIFT, 1 }, /* XP */
7086     { WM_KEYDOWN, wparam|lparam, VK_SHIFT, 1 },
7087     { WM_KEYDOWN, sent|wparam|lparam, VK_SHIFT, 0x00000001 },
7088     { HCBT_KEYSKIPPED, hook|wparam|lparam|optional, VK_F10, 1 }, /* XP */
7089     { WM_SYSKEYDOWN, wparam|lparam, VK_F10, 1 },
7090     { WM_SYSKEYDOWN, sent|wparam|lparam, VK_F10, 0x00000001 },
7091     { WM_CONTEXTMENU, sent|defwinproc|lparam, /*hwnd*/0, -1 },
7092     { HCBT_KEYSKIPPED, hook|wparam|lparam|optional, VK_F10, 0xc0000001 }, /* XP */
7093     { WM_SYSKEYUP, wparam|lparam, VK_F10, 0xc0000001 },
7094     { WM_SYSKEYUP, sent|wparam|lparam, VK_F10, 0xc0000001 },
7095     { WM_SYSCOMMAND, sent|defwinproc|wparam, SC_KEYMENU },
7096     { HCBT_SYSCOMMAND, hook },
7097     { WM_ENTERMENULOOP, sent|defwinproc|wparam|lparam, 0, 0 },
7098     { WM_INITMENU, sent|defwinproc },
7099     { WM_MENUSELECT, sent|defwinproc|wparam, MAKEWPARAM(0,MF_SYSMENU|MF_POPUP|MF_HILITE) },
7100     { HCBT_KEYSKIPPED, hook|wparam|lparam|optional, VK_SHIFT, 0xd0000001 }, /* XP */
7101     { HCBT_KEYSKIPPED, hook|wparam|lparam|optional, VK_ESCAPE, 0x10000001 }, /* XP */
7102     { WM_CAPTURECHANGED, sent|defwinproc|wparam|lparam, 0, 0 },
7103     { WM_MENUSELECT, sent|defwinproc|wparam|lparam, 0xffff0000, 0 },
7104     { WM_EXITMENULOOP, sent|defwinproc|wparam|lparam, 0, 0 },
7105     { HCBT_KEYSKIPPED, hook|wparam|lparam|optional, VK_ESCAPE, 0xc0000001 }, /* XP */
7106     { WM_KEYUP, wparam|lparam, VK_ESCAPE, 0xc0000001 },
7107     { WM_KEYUP, sent|wparam|lparam, VK_ESCAPE, 0xc0000001 },
7108     { 0 }
7109 };
7110
7111 static void pump_msg_loop(HWND hwnd, HACCEL hAccel)
7112 {
7113     MSG msg;
7114
7115     while (PeekMessageA(&msg, 0, 0, 0, PM_REMOVE))
7116     {
7117         struct recvd_message log_msg;
7118
7119         /* ignore some unwanted messages */
7120         if (msg.message == WM_MOUSEMOVE ||
7121             msg.message == WM_TIMER ||
7122             ignore_message( msg.message ))
7123             continue;
7124
7125         log_msg.hwnd = msg.hwnd;
7126         log_msg.message = msg.message;
7127         log_msg.flags = wparam|lparam;
7128         log_msg.wParam = msg.wParam;
7129         log_msg.lParam = msg.lParam;
7130         log_msg.descr = "accel";
7131         add_message(&log_msg);
7132
7133         if (!hAccel || !TranslateAccelerator(hwnd, hAccel, &msg))
7134         {
7135             TranslateMessage(&msg);
7136             DispatchMessage(&msg);
7137         }
7138     }
7139 }
7140
7141 static void test_accelerators(void)
7142 {
7143     RECT rc;
7144     POINT pt;
7145     SHORT state;
7146     HACCEL hAccel;
7147     HWND hwnd = CreateWindowExA(0, "TestWindowClass", NULL, WS_OVERLAPPEDWINDOW | WS_VISIBLE,
7148                                 100, 100, 200, 200, 0, 0, 0, NULL);
7149     BOOL ret;
7150
7151     assert(hwnd != 0);
7152     UpdateWindow(hwnd);
7153     flush_events();
7154     flush_sequence();
7155
7156     SetFocus(hwnd);
7157     ok(GetFocus() == hwnd, "wrong focus window %p\n", GetFocus());
7158
7159     state = GetKeyState(VK_SHIFT);
7160     ok(!(state & 0x8000), "wrong Shift state %04x\n", state);
7161     state = GetKeyState(VK_CAPITAL);
7162     ok(state == 0, "wrong CapsLock state %04x\n", state);
7163
7164     hAccel = LoadAccelerators(GetModuleHandleA(0), MAKEINTRESOURCE(1));
7165     assert(hAccel != 0);
7166
7167     flush_events();
7168     pump_msg_loop(hwnd, 0);
7169     flush_sequence();
7170
7171     trace("testing VK_N press/release\n");
7172     flush_sequence();
7173     keybd_event('N', 0, 0, 0);
7174     keybd_event('N', 0, KEYEVENTF_KEYUP, 0);
7175     pump_msg_loop(hwnd, hAccel);
7176     if (!sequence_cnt)  /* we didn't get any message */
7177     {
7178         skip( "queuing key events not supported\n" );
7179         goto done;
7180     }
7181     ok_sequence(WmVkN, "VK_N press/release", FALSE);
7182
7183     trace("testing Shift+VK_N press/release\n");
7184     flush_sequence();
7185     keybd_event(VK_SHIFT, 0, 0, 0);
7186     keybd_event('N', 0, 0, 0);
7187     keybd_event('N', 0, KEYEVENTF_KEYUP, 0);
7188     keybd_event(VK_SHIFT, 0, KEYEVENTF_KEYUP, 0);
7189     pump_msg_loop(hwnd, hAccel);
7190     ok_sequence(WmShiftVkN, "Shift+VK_N press/release", FALSE);
7191
7192     trace("testing Ctrl+VK_N press/release\n");
7193     flush_sequence();
7194     keybd_event(VK_CONTROL, 0, 0, 0);
7195     keybd_event('N', 0, 0, 0);
7196     keybd_event('N', 0, KEYEVENTF_KEYUP, 0);
7197     keybd_event(VK_CONTROL, 0, KEYEVENTF_KEYUP, 0);
7198     pump_msg_loop(hwnd, hAccel);
7199     ok_sequence(WmCtrlVkN, "Ctrl+VK_N press/release", FALSE);
7200
7201     trace("testing Alt+VK_N press/release\n");
7202     flush_sequence();
7203     keybd_event(VK_MENU, 0, 0, 0);
7204     keybd_event('N', 0, 0, 0);
7205     keybd_event('N', 0, KEYEVENTF_KEYUP, 0);
7206     keybd_event(VK_MENU, 0, KEYEVENTF_KEYUP, 0);
7207     pump_msg_loop(hwnd, hAccel);
7208     ok_sequence(WmAltVkN, "Alt+VK_N press/release", FALSE);
7209
7210     trace("testing Ctrl+Alt+VK_N press/release 1\n");
7211     flush_sequence();
7212     keybd_event(VK_CONTROL, 0, 0, 0);
7213     keybd_event(VK_MENU, 0, 0, 0);
7214     keybd_event('N', 0, 0, 0);
7215     keybd_event('N', 0, KEYEVENTF_KEYUP, 0);
7216     keybd_event(VK_MENU, 0, KEYEVENTF_KEYUP, 0);
7217     keybd_event(VK_CONTROL, 0, KEYEVENTF_KEYUP, 0);
7218     pump_msg_loop(hwnd, hAccel);
7219     ok_sequence(WmCtrlAltVkN, "Ctrl+Alt+VK_N press/release 1", FALSE);
7220
7221     ret = DestroyAcceleratorTable(hAccel);
7222     ok( ret, "DestroyAcceleratorTable error %d\n", GetLastError());
7223
7224     hAccel = LoadAccelerators(GetModuleHandleA(0), MAKEINTRESOURCE(2));
7225     assert(hAccel != 0);
7226
7227     trace("testing VK_N press/release\n");
7228     flush_sequence();
7229     keybd_event('N', 0, 0, 0);
7230     keybd_event('N', 0, KEYEVENTF_KEYUP, 0);
7231     pump_msg_loop(hwnd, hAccel);
7232     ok_sequence(WmVkN, "VK_N press/release", FALSE);
7233
7234     trace("testing Shift+VK_N press/release\n");
7235     flush_sequence();
7236     keybd_event(VK_SHIFT, 0, 0, 0);
7237     keybd_event('N', 0, 0, 0);
7238     keybd_event('N', 0, KEYEVENTF_KEYUP, 0);
7239     keybd_event(VK_SHIFT, 0, KEYEVENTF_KEYUP, 0);
7240     pump_msg_loop(hwnd, hAccel);
7241     ok_sequence(WmShiftVkN, "Shift+VK_N press/release", FALSE);
7242
7243     trace("testing Ctrl+VK_N press/release 2\n");
7244     flush_sequence();
7245     keybd_event(VK_CONTROL, 0, 0, 0);
7246     keybd_event('N', 0, 0, 0);
7247     keybd_event('N', 0, KEYEVENTF_KEYUP, 0);
7248     keybd_event(VK_CONTROL, 0, KEYEVENTF_KEYUP, 0);
7249     pump_msg_loop(hwnd, hAccel);
7250     ok_sequence(WmCtrlVkN_2, "Ctrl+VK_N press/release 2", FALSE);
7251
7252     trace("testing Alt+VK_N press/release 2\n");
7253     flush_sequence();
7254     keybd_event(VK_MENU, 0, 0, 0);
7255     keybd_event('N', 0, 0, 0);
7256     keybd_event('N', 0, KEYEVENTF_KEYUP, 0);
7257     keybd_event(VK_MENU, 0, KEYEVENTF_KEYUP, 0);
7258     pump_msg_loop(hwnd, hAccel);
7259     ok_sequence(WmAltVkN_2, "Alt+VK_N press/release 2", FALSE);
7260
7261     trace("testing Ctrl+Alt+VK_N press/release 2\n");
7262     flush_sequence();
7263     keybd_event(VK_CONTROL, 0, 0, 0);
7264     keybd_event(VK_MENU, 0, 0, 0);
7265     keybd_event('N', 0, 0, 0);
7266     keybd_event('N', 0, KEYEVENTF_KEYUP, 0);
7267     keybd_event(VK_MENU, 0, KEYEVENTF_KEYUP, 0);
7268     keybd_event(VK_CONTROL, 0, KEYEVENTF_KEYUP, 0);
7269     pump_msg_loop(hwnd, hAccel);
7270     ok_sequence(WmCtrlAltVkN, "Ctrl+Alt+VK_N press/release 2", FALSE);
7271
7272     trace("testing Ctrl+Shift+VK_N press/release\n");
7273     flush_sequence();
7274     keybd_event(VK_CONTROL, 0, 0, 0);
7275     keybd_event(VK_SHIFT, 0, 0, 0);
7276     keybd_event('N', 0, 0, 0);
7277     keybd_event('N', 0, KEYEVENTF_KEYUP, 0);
7278     keybd_event(VK_SHIFT, 0, KEYEVENTF_KEYUP, 0);
7279     keybd_event(VK_CONTROL, 0, KEYEVENTF_KEYUP, 0);
7280     pump_msg_loop(hwnd, hAccel);
7281     ok_sequence(WmCtrlShiftVkN, "Ctrl+Shift+VK_N press/release", FALSE);
7282
7283     trace("testing Ctrl+Alt+Shift+VK_N press/release\n");
7284     flush_sequence();
7285     keybd_event(VK_CONTROL, 0, 0, 0);
7286     keybd_event(VK_MENU, 0, 0, 0);
7287     keybd_event(VK_SHIFT, 0, 0, 0);
7288     keybd_event('N', 0, 0, 0);
7289     keybd_event('N', 0, KEYEVENTF_KEYUP, 0);
7290     keybd_event(VK_SHIFT, 0, KEYEVENTF_KEYUP, 0);
7291     keybd_event(VK_MENU, 0, KEYEVENTF_KEYUP, 0);
7292     keybd_event(VK_CONTROL, 0, KEYEVENTF_KEYUP, 0);
7293     pump_msg_loop(hwnd, hAccel);
7294     ok_sequence(WmCtrlAltShiftVkN, "Ctrl+Alt+Shift+VK_N press/release", FALSE);
7295
7296     ret = DestroyAcceleratorTable(hAccel);
7297     ok( ret, "DestroyAcceleratorTable error %d\n", GetLastError());
7298     hAccel = 0;
7299
7300     trace("testing Alt press/release\n");
7301     flush_sequence();
7302     keybd_event(VK_MENU, 0, 0, 0);
7303     keybd_event(VK_MENU, 0, KEYEVENTF_KEYUP, 0);
7304     keybd_event(VK_MENU, 0, 0, 0);
7305     keybd_event(VK_MENU, 0, KEYEVENTF_KEYUP, 0);
7306     pump_msg_loop(hwnd, 0);
7307     /* this test doesn't pass in Wine for managed windows */
7308     ok_sequence(WmAltPressRelease, "Alt press/release", TRUE);
7309
7310     trace("testing VK_F1 press/release\n");
7311     keybd_event(VK_F1, 0, 0, 0);
7312     keybd_event(VK_F1, 0, KEYEVENTF_KEYUP, 0);
7313     pump_msg_loop(hwnd, 0);
7314     ok_sequence(WmF1Seq, "F1 press/release", FALSE);
7315
7316     trace("testing VK_APPS press/release\n");
7317     keybd_event(VK_APPS, 0, 0, 0);
7318     keybd_event(VK_APPS, 0, KEYEVENTF_KEYUP, 0);
7319     pump_msg_loop(hwnd, 0);
7320     ok_sequence(WmVkAppsSeq, "VK_APPS press/release", FALSE);
7321
7322     trace("testing VK_F10 press/release\n");
7323     keybd_event(VK_F10, 0, 0, 0);
7324     keybd_event(VK_F10, 0, KEYEVENTF_KEYUP, 0);
7325     keybd_event(VK_F10, 0, 0, 0);
7326     keybd_event(VK_F10, 0, KEYEVENTF_KEYUP, 0);
7327     pump_msg_loop(hwnd, 0);
7328     ok_sequence(WmVkF10Seq, "VK_F10 press/release", TRUE);
7329
7330     trace("testing SHIFT+F10 press/release\n");
7331     keybd_event(VK_SHIFT, 0, 0, 0);
7332     keybd_event(VK_F10, 0, 0, 0);
7333     keybd_event(VK_F10, 0, KEYEVENTF_KEYUP, 0);
7334     keybd_event(VK_SHIFT, 0, KEYEVENTF_KEYUP, 0);
7335     keybd_event(VK_ESCAPE, 0, 0, 0);
7336     keybd_event(VK_ESCAPE, 0, KEYEVENTF_KEYUP, 0);
7337     pump_msg_loop(hwnd, 0);
7338     ok_sequence(WmShiftF10Seq, "SHIFT+F10 press/release", TRUE);
7339
7340     trace("testing Shift+MouseButton press/release\n");
7341     /* first, move mouse pointer inside of the window client area */
7342     GetClientRect(hwnd, &rc);
7343     MapWindowPoints(hwnd, 0, (LPPOINT)&rc, 2);
7344     rc.left += (rc.right - rc.left)/2;
7345     rc.top += (rc.bottom - rc.top)/2;
7346     SetCursorPos(rc.left, rc.top);
7347     SetActiveWindow(hwnd);
7348
7349     flush_events();
7350     flush_sequence();
7351     GetCursorPos(&pt);
7352     if (pt.x == rc.left && pt.y == rc.top)
7353     {
7354         int i;
7355         keybd_event(VK_SHIFT, 0, 0, 0);
7356         mouse_event(MOUSEEVENTF_LEFTDOWN, 0, 0, 0, 0);
7357         mouse_event(MOUSEEVENTF_LEFTUP, 0, 0, 0, 0);
7358         keybd_event(VK_SHIFT, 0, KEYEVENTF_KEYUP, 0);
7359         pump_msg_loop(hwnd, 0);
7360         for (i = 0; i < sequence_cnt; i++) if (sequence[i].message == WM_LBUTTONDOWN) break;
7361         if (i < sequence_cnt)
7362             ok_sequence(WmShiftMouseButton, "Shift+MouseButton press/release", FALSE);
7363         else
7364             skip( "Shift+MouseButton event didn't get to the window\n" );
7365     }
7366
7367 done:
7368     if (hAccel) DestroyAcceleratorTable(hAccel);
7369     DestroyWindow(hwnd);
7370 }
7371
7372 /************* window procedures ********************/
7373
7374 static LRESULT MsgCheckProc (BOOL unicode, HWND hwnd, UINT message, 
7375                              WPARAM wParam, LPARAM lParam)
7376 {
7377     static LONG defwndproc_counter = 0;
7378     static LONG beginpaint_counter = 0;
7379     LRESULT ret;
7380     struct recvd_message msg;
7381
7382     if (ignore_message( message )) return 0;
7383
7384     switch (message)
7385     {
7386         case WM_ENABLE:
7387         {
7388             LONG style = GetWindowLongA(hwnd, GWL_STYLE);
7389             ok((BOOL)wParam == !(style & WS_DISABLED),
7390                 "wrong WS_DISABLED state: %ld != %d\n", wParam, !(style & WS_DISABLED));
7391             break;
7392         }
7393
7394         case WM_CAPTURECHANGED:
7395             if (test_DestroyWindow_flag)
7396             {
7397                 DWORD style = GetWindowLongA(hwnd, GWL_STYLE);
7398                 if (style & WS_CHILD)
7399                     lParam = GetWindowLongPtrA(hwnd, GWLP_ID);
7400                 else if (style & WS_POPUP)
7401                     lParam = WND_POPUP_ID;
7402                 else
7403                     lParam = WND_PARENT_ID;
7404             }
7405             break;
7406
7407         case WM_NCDESTROY:
7408         {
7409             HWND capture;
7410
7411             ok(!GetWindow(hwnd, GW_CHILD), "children should be unlinked at this point\n");
7412             capture = GetCapture();
7413             if (capture)
7414             {
7415                 ok(capture == hwnd, "capture should NOT be released at this point (capture %p)\n", capture);
7416                 trace("current capture %p, releasing...\n", capture);
7417                 ReleaseCapture();
7418             }
7419         }
7420         /* fall through */
7421         case WM_DESTROY:
7422             if (pGetAncestor)
7423                 ok(pGetAncestor(hwnd, GA_PARENT) != 0, "parent should NOT be unlinked at this point\n");
7424             if (test_DestroyWindow_flag)
7425             {
7426                 DWORD style = GetWindowLongA(hwnd, GWL_STYLE);
7427                 if (style & WS_CHILD)
7428                     lParam = GetWindowLongPtrA(hwnd, GWLP_ID);
7429                 else if (style & WS_POPUP)
7430                     lParam = WND_POPUP_ID;
7431                 else
7432                     lParam = WND_PARENT_ID;
7433             }
7434             break;
7435
7436         /* test_accelerators() depends on this */
7437         case WM_NCHITTEST:
7438             return HTCLIENT;
7439
7440         /* ignore */
7441         case WM_MOUSEMOVE:
7442         case WM_MOUSEACTIVATE:
7443         case WM_NCMOUSEMOVE:
7444         case WM_SETCURSOR:
7445         case WM_IME_SELECT:
7446             return 0;
7447     }
7448
7449     msg.hwnd = hwnd;
7450     msg.message = message;
7451     msg.flags = sent|wparam|lparam;
7452     if (defwndproc_counter) msg.flags |= defwinproc;
7453     if (beginpaint_counter) msg.flags |= beginpaint;
7454     msg.wParam = wParam;
7455     msg.lParam = lParam;
7456     msg.descr = "MsgCheckProc";
7457     add_message(&msg);
7458
7459     if (message == WM_GETMINMAXINFO && (GetWindowLongA(hwnd, GWL_STYLE) & WS_CHILD))
7460     {
7461         HWND parent = GetParent(hwnd);
7462         RECT rc;
7463         MINMAXINFO *minmax = (MINMAXINFO *)lParam;
7464
7465         GetClientRect(parent, &rc);
7466         trace("parent %p client size = (%d x %d)\n", parent, rc.right, rc.bottom);
7467         trace("Reserved=%d,%d MaxSize=%d,%d MaxPos=%d,%d MinTrack=%d,%d MaxTrack=%d,%d\n",
7468               minmax->ptReserved.x, minmax->ptReserved.y,
7469               minmax->ptMaxSize.x, minmax->ptMaxSize.y,
7470               minmax->ptMaxPosition.x, minmax->ptMaxPosition.y,
7471               minmax->ptMinTrackSize.x, minmax->ptMinTrackSize.y,
7472               minmax->ptMaxTrackSize.x, minmax->ptMaxTrackSize.y);
7473
7474         ok(minmax->ptMaxSize.x == rc.right, "default width of maximized child %d != %d\n",
7475            minmax->ptMaxSize.x, rc.right);
7476         ok(minmax->ptMaxSize.y == rc.bottom, "default height of maximized child %d != %d\n",
7477            minmax->ptMaxSize.y, rc.bottom);
7478     }
7479
7480     if (message == WM_PAINT)
7481     {
7482         PAINTSTRUCT ps;
7483         beginpaint_counter++;
7484         BeginPaint( hwnd, &ps );
7485         beginpaint_counter--;
7486         EndPaint( hwnd, &ps );
7487         return 0;
7488     }
7489
7490     defwndproc_counter++;
7491     ret = unicode ? DefWindowProcW(hwnd, message, wParam, lParam) 
7492                   : DefWindowProcA(hwnd, message, wParam, lParam);
7493     defwndproc_counter--;
7494
7495     return ret;
7496 }
7497
7498 static LRESULT WINAPI MsgCheckProcA(HWND hwnd, UINT message, WPARAM wParam, LPARAM lParam)
7499 {
7500     return MsgCheckProc (FALSE, hwnd, message, wParam, lParam);
7501 }
7502
7503 static LRESULT WINAPI MsgCheckProcW(HWND hwnd, UINT message, WPARAM wParam, LPARAM lParam)
7504 {
7505     return MsgCheckProc (TRUE, hwnd, message, wParam, lParam);
7506 }
7507
7508 static LRESULT WINAPI PopupMsgCheckProcA(HWND hwnd, UINT message, WPARAM wParam, LPARAM lParam)
7509 {
7510     static LONG defwndproc_counter = 0;
7511     LRESULT ret;
7512     struct recvd_message msg;
7513
7514     if (ignore_message( message )) return 0;
7515
7516     switch (message)
7517     {
7518     case WM_QUERYENDSESSION:
7519     case WM_ENDSESSION:
7520         lParam &= ~0x01;  /* Vista adds a 0x01 flag */
7521         break;
7522     }
7523
7524     msg.hwnd = hwnd;
7525     msg.message = message;
7526     msg.flags = sent|wparam|lparam;
7527     if (defwndproc_counter) msg.flags |= defwinproc;
7528     msg.wParam = wParam;
7529     msg.lParam = lParam;
7530     msg.descr = "popup";
7531     add_message(&msg);
7532
7533     if (message == WM_CREATE)
7534     {
7535         DWORD style = GetWindowLongA(hwnd, GWL_STYLE) | WS_VISIBLE;
7536         SetWindowLongA(hwnd, GWL_STYLE, style);
7537     }
7538
7539     defwndproc_counter++;
7540     ret = DefWindowProcA(hwnd, message, wParam, lParam);
7541     defwndproc_counter--;
7542
7543     return ret;
7544 }
7545
7546 static LRESULT WINAPI ParentMsgCheckProcA(HWND hwnd, UINT message, WPARAM wParam, LPARAM lParam)
7547 {
7548     static LONG defwndproc_counter = 0;
7549     static LONG beginpaint_counter = 0;
7550     LRESULT ret;
7551     struct recvd_message msg;
7552
7553     if (ignore_message( message )) return 0;
7554
7555     if (log_all_parent_messages ||
7556         message == WM_PARENTNOTIFY || message == WM_CANCELMODE ||
7557         message == WM_SETFOCUS || message == WM_KILLFOCUS ||
7558         message == WM_ENABLE || message == WM_ENTERIDLE ||
7559         message == WM_DRAWITEM || message == WM_COMMAND ||
7560         message == WM_IME_SETCONTEXT)
7561     {
7562         switch (message)
7563         {
7564             /* ignore */
7565             case WM_NCHITTEST:
7566                 return HTCLIENT;
7567             case WM_SETCURSOR:
7568             case WM_MOUSEMOVE:
7569             case WM_NCMOUSEMOVE:
7570                 return 0;
7571
7572             case WM_ERASEBKGND:
7573             {
7574                 RECT rc;
7575                 INT ret = GetClipBox((HDC)wParam, &rc);
7576
7577                 trace("WM_ERASEBKGND: GetClipBox()=%d, (%d,%d-%d,%d)\n",
7578                        ret, rc.left, rc.top, rc.right, rc.bottom);
7579                 break;
7580             }
7581         }
7582
7583         msg.hwnd = hwnd;
7584         msg.message = message;
7585         msg.flags = sent|parent|wparam|lparam;
7586         if (defwndproc_counter) msg.flags |= defwinproc;
7587         if (beginpaint_counter) msg.flags |= beginpaint;
7588         msg.wParam = wParam;
7589         msg.lParam = lParam;
7590         msg.descr = "parent";
7591         add_message(&msg);
7592     }
7593
7594     if (message == WM_PAINT)
7595     {
7596         PAINTSTRUCT ps;
7597         beginpaint_counter++;
7598         BeginPaint( hwnd, &ps );
7599         beginpaint_counter--;
7600         EndPaint( hwnd, &ps );
7601         return 0;
7602     }
7603
7604     defwndproc_counter++;
7605     ret = DefWindowProcA(hwnd, message, wParam, lParam);
7606     defwndproc_counter--;
7607
7608     return ret;
7609 }
7610
7611 static INT_PTR CALLBACK StopQuitMsgCheckProcA(HWND hwnd, UINT message, WPARAM wp, LPARAM lp)
7612 {
7613     if (message == WM_CREATE)
7614         PostMessage(hwnd, WM_CLOSE, 0, 0);
7615     else if (message == WM_CLOSE)
7616     {
7617         /* Only the first WM_QUIT will survive the window destruction */
7618         PostMessage(hwnd, WM_USER, 0x1234, 0x5678);
7619         PostMessage(hwnd, WM_QUIT, 0x1234, 0x5678);
7620         PostMessage(hwnd, WM_QUIT, 0x4321, 0x8765);
7621     }
7622
7623     return DefWindowProcA(hwnd, message, wp, lp);
7624 }
7625
7626 static LRESULT WINAPI TestDlgProcA(HWND hwnd, UINT message, WPARAM wParam, LPARAM lParam)
7627 {
7628     static LONG defwndproc_counter = 0;
7629     LRESULT ret;
7630     struct recvd_message msg;
7631
7632     if (ignore_message( message )) return 0;
7633
7634     if (test_def_id)
7635     {
7636         DefDlgProcA(hwnd, DM_SETDEFID, 1, 0);
7637         ret = DefDlgProcA(hwnd, DM_GETDEFID, 0, 0);
7638         if (after_end_dialog)
7639             ok( ret == 0, "DM_GETDEFID should return 0 after EndDialog, got %lx\n", ret );
7640         else
7641             ok(HIWORD(ret) == DC_HASDEFID, "DM_GETDEFID should return DC_HASDEFID, got %lx\n", ret);
7642     }
7643
7644     msg.hwnd = hwnd;
7645     msg.message = message;
7646     msg.flags = sent|wparam|lparam;
7647     if (defwndproc_counter) msg.flags |= defwinproc;
7648     msg.wParam = wParam;
7649     msg.lParam = lParam;
7650     msg.descr = "dialog";
7651     add_message(&msg);
7652
7653     defwndproc_counter++;
7654     ret = DefDlgProcA(hwnd, message, wParam, lParam);
7655     defwndproc_counter--;
7656
7657     return ret;
7658 }
7659
7660 static LRESULT WINAPI ShowWindowProcA(HWND hwnd, UINT message, WPARAM wParam, LPARAM lParam)
7661 {
7662     static LONG defwndproc_counter = 0;
7663     LRESULT ret;
7664     struct recvd_message msg;
7665
7666     /* log only specific messages we are interested in */
7667     switch (message)
7668     {
7669 #if 0 /* probably log these as well */
7670     case WM_ACTIVATE:
7671     case WM_SETFOCUS:
7672     case WM_KILLFOCUS:
7673 #endif
7674     case WM_SHOWWINDOW:
7675     case WM_SIZE:
7676     case WM_MOVE:
7677     case WM_GETMINMAXINFO:
7678     case WM_WINDOWPOSCHANGING:
7679     case WM_WINDOWPOSCHANGED:
7680         break;
7681
7682     default: /* ignore */
7683         /*trace("showwindow: %p, %04x, %08x, %08lx\n", hwnd, message, wParam, lParam);*/
7684         return DefWindowProcA(hwnd, message, wParam, lParam);
7685     }
7686
7687     msg.hwnd = hwnd;
7688     msg.message = message;
7689     msg.flags = sent|wparam|lparam;
7690     if (defwndproc_counter) msg.flags |= defwinproc;
7691     msg.wParam = wParam;
7692     msg.lParam = lParam;
7693     msg.descr = "show";
7694     add_message(&msg);
7695
7696     defwndproc_counter++;
7697     ret = DefWindowProcA(hwnd, message, wParam, lParam);
7698     defwndproc_counter--;
7699
7700     return ret;
7701 }
7702
7703 static LRESULT WINAPI PaintLoopProcA(HWND hWnd, UINT msg, WPARAM wParam, LPARAM lParam)
7704 {
7705     switch (msg)
7706     {
7707         case WM_CREATE: return 0;
7708         case WM_PAINT:
7709         {
7710             MSG msg2;
7711             static int i = 0;
7712
7713             if (i < 256)
7714             {
7715                 i++;
7716                 if (PeekMessageA(&msg2, 0, 0, 0, 1))
7717                 {
7718                     TranslateMessage(&msg2);
7719                     DispatchMessage(&msg2);
7720                 }
7721                 i--;
7722             }
7723             else ok(broken(1), "infinite loop\n");
7724             if ( i == 0)
7725                 paint_loop_done = 1;
7726             return DefWindowProcA(hWnd,msg,wParam,lParam);
7727         }
7728     }
7729     return DefWindowProcA(hWnd,msg,wParam,lParam);
7730 }
7731
7732 static LRESULT WINAPI HotkeyMsgCheckProcA(HWND hwnd, UINT message, WPARAM wParam, LPARAM lParam)
7733 {
7734     static LONG defwndproc_counter = 0;
7735     LRESULT ret;
7736     struct recvd_message msg;
7737     DWORD queue_status;
7738
7739     if (ignore_message( message )) return 0;
7740
7741     if ((message >= WM_KEYFIRST && message <= WM_KEYLAST) ||
7742         message == WM_HOTKEY || message >= WM_APP)
7743     {
7744         msg.hwnd = hwnd;
7745         msg.message = message;
7746         msg.flags = sent|wparam|lparam;
7747         if (defwndproc_counter) msg.flags |= defwinproc;
7748         msg.wParam = wParam;
7749         msg.lParam = lParam;
7750         msg.descr = "HotkeyMsgCheckProcA";
7751         add_message(&msg);
7752     }
7753
7754     defwndproc_counter++;
7755     ret = DefWindowProcA(hwnd, message, wParam, lParam);
7756     defwndproc_counter--;
7757
7758     if (message == WM_APP)
7759     {
7760         queue_status = GetQueueStatus(QS_HOTKEY);
7761         ok((queue_status & (QS_HOTKEY << 16)) == QS_HOTKEY << 16, "expected QS_HOTKEY << 16 set, got %x\n", queue_status);
7762         queue_status = GetQueueStatus(QS_POSTMESSAGE);
7763         ok((queue_status & (QS_POSTMESSAGE << 16)) == QS_POSTMESSAGE << 16, "expected QS_POSTMESSAGE << 16 set, got %x\n", queue_status);
7764         PostMessageA(hwnd, WM_APP+1, 0, 0);
7765     }
7766     else if (message == WM_APP+1)
7767     {
7768         queue_status = GetQueueStatus(QS_HOTKEY);
7769         ok((queue_status & (QS_HOTKEY << 16)) == 0, "expected QS_HOTKEY << 16 cleared, got %x\n", queue_status);
7770     }
7771
7772     return ret;
7773 }
7774
7775 static BOOL RegisterWindowClasses(void)
7776 {
7777     WNDCLASSA cls;
7778     WNDCLASSW clsW;
7779
7780     cls.style = 0;
7781     cls.lpfnWndProc = MsgCheckProcA;
7782     cls.cbClsExtra = 0;
7783     cls.cbWndExtra = 0;
7784     cls.hInstance = GetModuleHandleA(0);
7785     cls.hIcon = 0;
7786     cls.hCursor = LoadCursorA(0, IDC_ARROW);
7787     cls.hbrBackground = GetStockObject(WHITE_BRUSH);
7788     cls.lpszMenuName = NULL;
7789     cls.lpszClassName = "TestWindowClass";
7790     if(!RegisterClassA(&cls)) return FALSE;
7791
7792     cls.lpfnWndProc = HotkeyMsgCheckProcA;
7793     cls.lpszClassName = "HotkeyWindowClass";
7794     if(!RegisterClassA(&cls)) return FALSE;
7795
7796     cls.lpfnWndProc = ShowWindowProcA;
7797     cls.lpszClassName = "ShowWindowClass";
7798     if(!RegisterClassA(&cls)) return FALSE;
7799
7800     cls.lpfnWndProc = PopupMsgCheckProcA;
7801     cls.lpszClassName = "TestPopupClass";
7802     if(!RegisterClassA(&cls)) return FALSE;
7803
7804     cls.lpfnWndProc = ParentMsgCheckProcA;
7805     cls.lpszClassName = "TestParentClass";
7806     if(!RegisterClassA(&cls)) return FALSE;
7807
7808     cls.lpfnWndProc = StopQuitMsgCheckProcA;
7809     cls.lpszClassName = "StopQuitClass";
7810     if(!RegisterClassA(&cls)) return FALSE;
7811
7812     cls.lpfnWndProc = DefWindowProcA;
7813     cls.lpszClassName = "SimpleWindowClass";
7814     if(!RegisterClassA(&cls)) return FALSE;
7815
7816     cls.lpfnWndProc = PaintLoopProcA;
7817     cls.lpszClassName = "PaintLoopWindowClass";
7818     if(!RegisterClassA(&cls)) return FALSE;
7819
7820     cls.style = CS_NOCLOSE;
7821     cls.lpszClassName = "NoCloseWindowClass";
7822     if(!RegisterClassA(&cls)) return FALSE;
7823
7824     ok(GetClassInfoA(0, "#32770", &cls), "GetClassInfo failed\n");
7825     cls.style = 0;
7826     cls.hInstance = GetModuleHandleA(0);
7827     cls.hbrBackground = 0;
7828     cls.lpfnWndProc = TestDlgProcA;
7829     cls.lpszClassName = "TestDialogClass";
7830     if(!RegisterClassA(&cls)) return FALSE;
7831
7832     clsW.style = 0;
7833     clsW.lpfnWndProc = MsgCheckProcW;
7834     clsW.cbClsExtra = 0;
7835     clsW.cbWndExtra = 0;
7836     clsW.hInstance = GetModuleHandleW(0);
7837     clsW.hIcon = 0;
7838     clsW.hCursor = LoadCursorW(0, (LPWSTR)IDC_ARROW);
7839     clsW.hbrBackground = GetStockObject(WHITE_BRUSH);
7840     clsW.lpszMenuName = NULL;
7841     clsW.lpszClassName = testWindowClassW;
7842     RegisterClassW(&clsW);  /* ignore error, this fails on Win9x */
7843
7844     return TRUE;
7845 }
7846
7847 static BOOL is_our_logged_class(HWND hwnd)
7848 {
7849     char buf[256];
7850
7851     if (GetClassNameA(hwnd, buf, sizeof(buf)))
7852     {
7853         if (!lstrcmpiA(buf, "TestWindowClass") ||
7854             !lstrcmpiA(buf, "ShowWindowClass") ||
7855             !lstrcmpiA(buf, "TestParentClass") ||
7856             !lstrcmpiA(buf, "TestPopupClass") ||
7857             !lstrcmpiA(buf, "SimpleWindowClass") ||
7858             !lstrcmpiA(buf, "TestDialogClass") ||
7859             !lstrcmpiA(buf, "MDI_frame_class") ||
7860             !lstrcmpiA(buf, "MDI_client_class") ||
7861             !lstrcmpiA(buf, "MDI_child_class") ||
7862             !lstrcmpiA(buf, "my_button_class") ||
7863             !lstrcmpiA(buf, "my_edit_class") ||
7864             !lstrcmpiA(buf, "static") ||
7865             !lstrcmpiA(buf, "ListBox") ||
7866             !lstrcmpiA(buf, "ComboBox") ||
7867             !lstrcmpiA(buf, "MyDialogClass") ||
7868             !lstrcmpiA(buf, "#32770") ||
7869             !lstrcmpiA(buf, "#32768"))
7870         return TRUE;
7871     }
7872     return FALSE;
7873 }
7874
7875 static LRESULT CALLBACK cbt_hook_proc(int nCode, WPARAM wParam, LPARAM lParam) 
7876
7877     HWND hwnd;
7878
7879     ok(cbt_hook_thread_id == GetCurrentThreadId(), "we didn't ask for events from other threads\n");
7880
7881     if (nCode == HCBT_CLICKSKIPPED)
7882     {
7883         /* ignore this event, XP sends it a lot when switching focus between windows */
7884         return CallNextHookEx(hCBT_hook, nCode, wParam, lParam);
7885     }
7886
7887     if (nCode == HCBT_SYSCOMMAND || nCode == HCBT_KEYSKIPPED)
7888     {
7889         struct recvd_message msg;
7890
7891         msg.hwnd = 0;
7892         msg.message = nCode;
7893         msg.flags = hook|wparam|lparam;
7894         msg.wParam = wParam;
7895         msg.lParam = lParam;
7896         msg.descr = "CBT";
7897         add_message(&msg);
7898
7899         return CallNextHookEx(hCBT_hook, nCode, wParam, lParam);
7900     }
7901
7902     if (nCode == HCBT_DESTROYWND)
7903     {
7904         if (test_DestroyWindow_flag)
7905         {
7906             DWORD style = GetWindowLongA((HWND)wParam, GWL_STYLE);
7907             if (style & WS_CHILD)
7908                 lParam = GetWindowLongPtrA((HWND)wParam, GWLP_ID);
7909             else if (style & WS_POPUP)
7910                 lParam = WND_POPUP_ID;
7911             else
7912                 lParam = WND_PARENT_ID;
7913         }
7914     }
7915
7916     /* Log also SetFocus(0) calls */
7917     hwnd = wParam ? (HWND)wParam : (HWND)lParam;
7918
7919     if (is_our_logged_class(hwnd))
7920     {
7921         struct recvd_message msg;
7922
7923         msg.hwnd = hwnd;
7924         msg.message = nCode;
7925         msg.flags = hook|wparam|lparam;
7926         msg.wParam = wParam;
7927         msg.lParam = lParam;
7928         msg.descr = "CBT";
7929         add_message(&msg);
7930     }
7931     return CallNextHookEx(hCBT_hook, nCode, wParam, lParam);
7932 }
7933
7934 static void CALLBACK win_event_proc(HWINEVENTHOOK hevent,
7935                                     DWORD event,
7936                                     HWND hwnd,
7937                                     LONG object_id,
7938                                     LONG child_id,
7939                                     DWORD thread_id,
7940                                     DWORD event_time)
7941 {
7942     ok(thread_id == GetCurrentThreadId(), "we didn't ask for events from other threads\n");
7943
7944     /* ignore mouse cursor events */
7945     if (object_id == OBJID_CURSOR) return;
7946
7947     if (!hwnd || is_our_logged_class(hwnd))
7948     {
7949         struct recvd_message msg;
7950
7951         msg.hwnd = hwnd;
7952         msg.message = event;
7953         msg.flags = winevent_hook|wparam|lparam;
7954         msg.wParam = object_id;
7955         msg.lParam = child_id;
7956         msg.descr = "WEH";
7957         add_message(&msg);
7958     }
7959 }
7960
7961 static const WCHAR wszUnicode[] = {'U','n','i','c','o','d','e',0};
7962 static const WCHAR wszAnsi[] = {'U',0};
7963
7964 static LRESULT CALLBACK MsgConversionProcW(HWND hwnd, UINT uMsg, WPARAM wParam, LPARAM lParam)
7965 {
7966     switch (uMsg)
7967     {
7968     case CB_FINDSTRINGEXACT:
7969         trace("String: %p\n", (LPCWSTR)lParam);
7970         if (!lstrcmpW((LPCWSTR)lParam, wszUnicode))
7971             return 1;
7972         if (!lstrcmpW((LPCWSTR)lParam, wszAnsi))
7973             return 0;
7974         return -1;
7975     }
7976     return DefWindowProcW(hwnd, uMsg, wParam, lParam);
7977 }
7978
7979 static const struct message WmGetTextLengthAfromW[] = {
7980     { WM_GETTEXTLENGTH, sent },
7981     { WM_GETTEXT, sent|optional },
7982     { 0 }
7983 };
7984
7985 static const WCHAR dummy_window_text[] = {'d','u','m','m','y',' ','t','e','x','t',0};
7986
7987 /* dummy window proc for WM_GETTEXTLENGTH test */
7988 static LRESULT CALLBACK get_text_len_proc( HWND hwnd, UINT msg, WPARAM wp, LPARAM lp )
7989 {
7990     switch(msg)
7991     {
7992     case WM_GETTEXTLENGTH:
7993         return lstrlenW(dummy_window_text) + 37;  /* some random length */
7994     case WM_GETTEXT:
7995         lstrcpynW( (LPWSTR)lp, dummy_window_text, wp );
7996         return lstrlenW( (LPWSTR)lp );
7997     default:
7998         return DefWindowProcW( hwnd, msg, wp, lp );
7999     }
8000 }
8001
8002 static void test_message_conversion(void)
8003 {
8004     static const WCHAR wszMsgConversionClass[] =
8005         {'M','s','g','C','o','n','v','e','r','s','i','o','n','C','l','a','s','s',0};
8006     WNDCLASSW cls;
8007     LRESULT lRes;
8008     HWND hwnd;
8009     WNDPROC wndproc, newproc;
8010     BOOL ret;
8011
8012     cls.style = 0;
8013     cls.lpfnWndProc = MsgConversionProcW;
8014     cls.cbClsExtra = 0;
8015     cls.cbWndExtra = 0;
8016     cls.hInstance = GetModuleHandleW(NULL);
8017     cls.hIcon = NULL;
8018     cls.hCursor = LoadCursorW(NULL, (LPWSTR)IDC_ARROW);
8019     cls.hbrBackground = (HBRUSH)(COLOR_BTNFACE+1);
8020     cls.lpszMenuName = NULL;
8021     cls.lpszClassName = wszMsgConversionClass;
8022     /* this call will fail on Win9x, but that doesn't matter as this test is
8023      * meaningless on those platforms */
8024     if(!RegisterClassW(&cls)) return;
8025
8026     hwnd = CreateWindowExW(0, wszMsgConversionClass, NULL, WS_OVERLAPPED,
8027                            100, 100, 200, 200, 0, 0, 0, NULL);
8028     ok(hwnd != NULL, "Window creation failed\n");
8029
8030     /* {W, A} -> A */
8031
8032     wndproc = (WNDPROC)GetWindowLongPtrA(hwnd, GWLP_WNDPROC);
8033     lRes = CallWindowProcA(wndproc, hwnd, CB_FINDSTRINGEXACT, 0, (LPARAM)wszUnicode);
8034     ok(lRes == 0, "String should have been converted\n");
8035     lRes = CallWindowProcW(wndproc, hwnd, CB_FINDSTRINGEXACT, 0, (LPARAM)wszUnicode);
8036     ok(lRes == 1, "String shouldn't have been converted\n");
8037
8038     /* {W, A} -> W */
8039
8040     wndproc = (WNDPROC)GetWindowLongPtrW(hwnd, GWLP_WNDPROC);
8041     lRes = CallWindowProcA(wndproc, hwnd, CB_FINDSTRINGEXACT, 0, (LPARAM)wszUnicode);
8042     ok(lRes == 1, "String shouldn't have been converted\n");
8043     lRes = CallWindowProcW(wndproc, hwnd, CB_FINDSTRINGEXACT, 0, (LPARAM)wszUnicode);
8044     ok(lRes == 1, "String shouldn't have been converted\n");
8045
8046     /* Synchronous messages */
8047
8048     lRes = SendMessageA(hwnd, CB_FINDSTRINGEXACT, 0, (LPARAM)wszUnicode);
8049     ok(lRes == 0, "String should have been converted\n");
8050     lRes = SendMessageW(hwnd, CB_FINDSTRINGEXACT, 0, (LPARAM)wszUnicode);
8051     ok(lRes == 1, "String shouldn't have been converted\n");
8052
8053     /* Asynchronous messages */
8054
8055     SetLastError(0);
8056     lRes = PostMessageA(hwnd, CB_FINDSTRINGEXACT, 0, (LPARAM)wszUnicode);
8057     ok(lRes == 0 && (GetLastError() == ERROR_MESSAGE_SYNC_ONLY || GetLastError() == ERROR_INVALID_PARAMETER),
8058         "PostMessage on sync only message returned %ld, last error %d\n", lRes, GetLastError());
8059     SetLastError(0);
8060     lRes = PostMessageW(hwnd, CB_FINDSTRINGEXACT, 0, (LPARAM)wszUnicode);
8061     ok(lRes == 0 && (GetLastError() == ERROR_MESSAGE_SYNC_ONLY || GetLastError() == ERROR_INVALID_PARAMETER),
8062         "PostMessage on sync only message returned %ld, last error %d\n", lRes, GetLastError());
8063     SetLastError(0);
8064     lRes = PostThreadMessageA(GetCurrentThreadId(), CB_FINDSTRINGEXACT, 0, (LPARAM)wszUnicode);
8065     ok(lRes == 0 && (GetLastError() == ERROR_MESSAGE_SYNC_ONLY || GetLastError() == ERROR_INVALID_PARAMETER),
8066         "PosThreadtMessage on sync only message returned %ld, last error %d\n", lRes, GetLastError());
8067     SetLastError(0);
8068     lRes = PostThreadMessageW(GetCurrentThreadId(), CB_FINDSTRINGEXACT, 0, (LPARAM)wszUnicode);
8069     ok(lRes == 0 && (GetLastError() == ERROR_MESSAGE_SYNC_ONLY || GetLastError() == ERROR_INVALID_PARAMETER),
8070         "PosThreadtMessage on sync only message returned %ld, last error %d\n", lRes, GetLastError());
8071     SetLastError(0);
8072     lRes = SendNotifyMessageA(hwnd, CB_FINDSTRINGEXACT, 0, (LPARAM)wszUnicode);
8073     ok(lRes == 0 && (GetLastError() == ERROR_MESSAGE_SYNC_ONLY || GetLastError() == ERROR_INVALID_PARAMETER),
8074         "SendNotifyMessage on sync only message returned %ld, last error %d\n", lRes, GetLastError());
8075     SetLastError(0);
8076     lRes = SendNotifyMessageW(hwnd, CB_FINDSTRINGEXACT, 0, (LPARAM)wszUnicode);
8077     ok(lRes == 0 && (GetLastError() == ERROR_MESSAGE_SYNC_ONLY || GetLastError() == ERROR_INVALID_PARAMETER),
8078         "SendNotifyMessage on sync only message returned %ld, last error %d\n", lRes, GetLastError());
8079     SetLastError(0);
8080     lRes = SendMessageCallbackA(hwnd, CB_FINDSTRINGEXACT, 0, (LPARAM)wszUnicode, NULL, 0);
8081     ok(lRes == 0 && (GetLastError() == ERROR_MESSAGE_SYNC_ONLY || GetLastError() == ERROR_INVALID_PARAMETER),
8082         "SendMessageCallback on sync only message returned %ld, last error %d\n", lRes, GetLastError());
8083     SetLastError(0);
8084     lRes = SendMessageCallbackW(hwnd, CB_FINDSTRINGEXACT, 0, (LPARAM)wszUnicode, NULL, 0);
8085     ok(lRes == 0 && (GetLastError() == ERROR_MESSAGE_SYNC_ONLY || GetLastError() == ERROR_INVALID_PARAMETER),
8086         "SendMessageCallback on sync only message returned %ld, last error %d\n", lRes, GetLastError());
8087
8088     /* Check WM_GETTEXTLENGTH A->W behaviour, whether WM_GETTEXT is also sent or not */
8089
8090     hwnd = CreateWindowW (testWindowClassW, wszUnicode,
8091                           WS_OVERLAPPEDWINDOW,
8092                           100, 100, 200, 200, 0, 0, 0, NULL);
8093     assert(hwnd);
8094     flush_sequence();
8095     lRes = SendMessageA (hwnd, WM_GETTEXTLENGTH, 0, 0);
8096     ok_sequence(WmGetTextLengthAfromW, "ANSI WM_GETTEXTLENGTH to Unicode window", FALSE);
8097     ok( lRes == WideCharToMultiByte( CP_ACP, 0, wszUnicode, lstrlenW(wszUnicode), NULL, 0, NULL, NULL ),
8098         "got bad length %ld\n", lRes );
8099
8100     flush_sequence();
8101     lRes = CallWindowProcA( (WNDPROC)GetWindowLongPtrA( hwnd, GWLP_WNDPROC ),
8102                             hwnd, WM_GETTEXTLENGTH, 0, 0);
8103     ok_sequence(WmGetTextLengthAfromW, "ANSI WM_GETTEXTLENGTH to Unicode window", FALSE);
8104     ok( lRes == WideCharToMultiByte( CP_ACP, 0, wszUnicode, lstrlenW(wszUnicode), NULL, 0, NULL, NULL ),
8105         "got bad length %ld\n", lRes );
8106
8107     wndproc = (WNDPROC)SetWindowLongPtrW( hwnd, GWLP_WNDPROC, (LONG_PTR)get_text_len_proc );
8108     newproc = (WNDPROC)GetWindowLongPtrA( hwnd, GWLP_WNDPROC );
8109     lRes = CallWindowProcA( newproc, hwnd, WM_GETTEXTLENGTH, 0, 0 );
8110     ok( lRes == WideCharToMultiByte( CP_ACP, 0, dummy_window_text, lstrlenW(dummy_window_text),
8111                                      NULL, 0, NULL, NULL ) ||
8112         broken(lRes == lstrlenW(dummy_window_text) + 37),
8113         "got bad length %ld\n", lRes );
8114
8115     SetWindowLongPtrW( hwnd, GWLP_WNDPROC, (LONG_PTR)wndproc );  /* restore old wnd proc */
8116     lRes = CallWindowProcA( newproc, hwnd, WM_GETTEXTLENGTH, 0, 0 );
8117     ok( lRes == WideCharToMultiByte( CP_ACP, 0, dummy_window_text, lstrlenW(dummy_window_text),
8118                                      NULL, 0, NULL, NULL ) ||
8119         broken(lRes == lstrlenW(dummy_window_text) + 37),
8120         "got bad length %ld\n", lRes );
8121
8122     ret = DestroyWindow(hwnd);
8123     ok( ret, "DestroyWindow() error %d\n", GetLastError());
8124 }
8125
8126 struct timer_info
8127 {
8128     HWND hWnd;
8129     HANDLE handles[2];
8130     DWORD id;
8131 };
8132
8133 static VOID CALLBACK tfunc(HWND hwnd, UINT uMsg, UINT_PTR id, DWORD dwTime)
8134 {
8135 }
8136
8137 static VOID CALLBACK tfunc_crash(HWND hwnd, UINT uMsg, UINT_PTR id, DWORD dwTime)
8138 {
8139     /* Crash on purpose */
8140     *(volatile int *)0 = 2;
8141 }
8142
8143 #define TIMER_ID  0x19
8144
8145 static DWORD WINAPI timer_thread_proc(LPVOID x)
8146 {
8147     struct timer_info *info = x;
8148     DWORD r;
8149
8150     r = KillTimer(info->hWnd, 0x19);
8151     ok(r,"KillTimer failed in thread\n");
8152     r = SetTimer(info->hWnd,TIMER_ID,10000,tfunc);
8153     ok(r,"SetTimer failed in thread\n");
8154     ok(r==TIMER_ID,"SetTimer id different\n");
8155     r = SetEvent(info->handles[0]);
8156     ok(r,"SetEvent failed in thread\n");
8157     return 0;
8158 }
8159
8160 static void test_timers(void)
8161 {
8162     struct timer_info info;
8163     DWORD id;
8164     MSG msg;
8165
8166     info.hWnd = CreateWindow ("TestWindowClass", NULL,
8167        WS_OVERLAPPEDWINDOW ,
8168        CW_USEDEFAULT, CW_USEDEFAULT, 300, 300, 0,
8169        NULL, NULL, 0);
8170
8171     info.id = SetTimer(info.hWnd,TIMER_ID,10000,tfunc);
8172     ok(info.id, "SetTimer failed\n");
8173     ok(info.id==TIMER_ID, "SetTimer timer ID different\n");
8174     info.handles[0] = CreateEvent(NULL,0,0,NULL);
8175     info.handles[1] = CreateThread(NULL,0,timer_thread_proc,&info,0,&id);
8176
8177     WaitForMultipleObjects(2, info.handles, FALSE, INFINITE);
8178
8179     WaitForSingleObject(info.handles[1], INFINITE);
8180
8181     CloseHandle(info.handles[0]);
8182     CloseHandle(info.handles[1]);
8183
8184     ok( KillTimer(info.hWnd, TIMER_ID), "KillTimer failed\n");
8185
8186     ok(DestroyWindow(info.hWnd), "failed to destroy window\n");
8187
8188     /* Test timer callback with crash */
8189     SetLastError(0xdeadbeef);
8190     info.hWnd = CreateWindowW(testWindowClassW, NULL,
8191                               WS_OVERLAPPEDWINDOW ,
8192                               CW_USEDEFAULT, CW_USEDEFAULT, 300, 300, 0,
8193                               NULL, NULL, 0);
8194     if ((!info.hWnd && GetLastError() == ERROR_CALL_NOT_IMPLEMENTED) || /* Win9x/Me */
8195         (!pGetMenuInfo)) /* Win95/NT4 */
8196     {
8197         win_skip("Test would crash on Win9x/WinMe/NT4\n");
8198         DestroyWindow(info.hWnd);
8199         return;
8200     }
8201     info.id = SetTimer(info.hWnd, TIMER_ID, 0, tfunc_crash);
8202     ok(info.id, "SetTimer failed\n");
8203     Sleep(150);
8204     while (PeekMessage(&msg, NULL, 0, 0, PM_REMOVE)) DispatchMessage(&msg);
8205
8206     ok(DestroyWindow(info.hWnd), "failed to destroy window\n");
8207 }
8208
8209 static int count = 0;
8210 static VOID CALLBACK callback_count(
8211     HWND hwnd,
8212     UINT uMsg,
8213     UINT_PTR idEvent,
8214     DWORD dwTime
8215 )
8216 {
8217     count++;
8218 }
8219
8220 static void test_timers_no_wnd(void)
8221 {
8222     UINT_PTR id, id2;
8223     MSG msg;
8224
8225     count = 0;
8226     id = SetTimer(NULL, 0, 100, callback_count);
8227     ok(id != 0, "did not get id from SetTimer.\n");
8228     id2 = SetTimer(NULL, id, 200, callback_count);
8229     ok(id2 == id, "did not get same id from SetTimer when replacing (%li expected %li).\n", id2, id);
8230     Sleep(150);
8231     while (PeekMessage(&msg, NULL, 0, 0, PM_REMOVE)) DispatchMessage(&msg);
8232     ok(count == 0, "did not get zero count as expected (%i).\n", count);
8233     Sleep(150);
8234     while (PeekMessage(&msg, NULL, 0, 0, PM_REMOVE)) DispatchMessage(&msg);
8235     ok(count == 1, "did not get one count as expected (%i).\n", count);
8236     KillTimer(NULL, id);
8237     Sleep(250);
8238     while (PeekMessage(&msg, NULL, 0, 0, PM_REMOVE)) DispatchMessage(&msg);
8239     ok(count == 1, "killing replaced timer did not work (%i).\n", count);
8240 }
8241
8242 /* Various win events with arbitrary parameters */
8243 static const struct message WmWinEventsSeq[] = {
8244     { EVENT_SYSTEM_SOUND, winevent_hook|wparam|lparam, OBJID_WINDOW, 0 },
8245     { EVENT_SYSTEM_ALERT, winevent_hook|wparam|lparam, OBJID_SYSMENU, 1 },
8246     { EVENT_SYSTEM_FOREGROUND, winevent_hook|wparam|lparam, OBJID_TITLEBAR, 2 },
8247     { EVENT_SYSTEM_MENUSTART, winevent_hook|wparam|lparam, OBJID_MENU, 3 },
8248     { EVENT_SYSTEM_MENUEND, winevent_hook|wparam|lparam, OBJID_CLIENT, 4 },
8249     { EVENT_SYSTEM_MENUPOPUPSTART, winevent_hook|wparam|lparam, OBJID_VSCROLL, 5 },
8250     { EVENT_SYSTEM_MENUPOPUPEND, winevent_hook|wparam|lparam, OBJID_HSCROLL, 6 },
8251     { EVENT_SYSTEM_CAPTURESTART, winevent_hook|wparam|lparam, OBJID_SIZEGRIP, 7 },
8252     { EVENT_SYSTEM_CAPTUREEND, winevent_hook|wparam|lparam, OBJID_CARET, 8 },
8253     /* our win event hook ignores OBJID_CURSOR events */
8254     /*{ EVENT_SYSTEM_MOVESIZESTART, winevent_hook|wparam|lparam, OBJID_CURSOR, 9 },*/
8255     { EVENT_SYSTEM_MOVESIZEEND, winevent_hook|wparam|lparam, OBJID_ALERT, 10 },
8256     { EVENT_SYSTEM_CONTEXTHELPSTART, winevent_hook|wparam|lparam, OBJID_SOUND, 11 },
8257     { EVENT_SYSTEM_CONTEXTHELPEND, winevent_hook|wparam|lparam, OBJID_QUERYCLASSNAMEIDX, 12 },
8258     { EVENT_SYSTEM_DRAGDROPSTART, winevent_hook|wparam|lparam, OBJID_NATIVEOM, 13 },
8259     { EVENT_SYSTEM_DRAGDROPEND, winevent_hook|wparam|lparam, OBJID_WINDOW, 0 },
8260     { EVENT_SYSTEM_DIALOGSTART, winevent_hook|wparam|lparam, OBJID_SYSMENU, 1 },
8261     { EVENT_SYSTEM_DIALOGEND, winevent_hook|wparam|lparam, OBJID_TITLEBAR, 2 },
8262     { EVENT_SYSTEM_SCROLLINGSTART, winevent_hook|wparam|lparam, OBJID_MENU, 3 },
8263     { EVENT_SYSTEM_SCROLLINGEND, winevent_hook|wparam|lparam, OBJID_CLIENT, 4 },
8264     { EVENT_SYSTEM_SWITCHSTART, winevent_hook|wparam|lparam, OBJID_VSCROLL, 5 },
8265     { EVENT_SYSTEM_SWITCHEND, winevent_hook|wparam|lparam, OBJID_HSCROLL, 6 },
8266     { EVENT_SYSTEM_MINIMIZESTART, winevent_hook|wparam|lparam, OBJID_SIZEGRIP, 7 },
8267     { EVENT_SYSTEM_MINIMIZEEND, winevent_hook|wparam|lparam, OBJID_CARET, 8 },
8268     { 0 }
8269 };
8270 static const struct message WmWinEventCaretSeq[] = {
8271     { EVENT_OBJECT_CREATE, winevent_hook|wparam|lparam, OBJID_CARET, 0 }, /* hook 1 */
8272     { EVENT_OBJECT_SHOW, winevent_hook|wparam|lparam, OBJID_CARET, 0 }, /* hook 1 */
8273     { EVENT_OBJECT_SHOW, winevent_hook|wparam|lparam, OBJID_CARET, 0 }, /* hook 2 */
8274     { EVENT_OBJECT_NAMECHANGE, winevent_hook|wparam|lparam, OBJID_CARET, 0 }, /* hook 1 */
8275     { 0 }
8276 };
8277 static const struct message WmWinEventCaretSeq_2[] = {
8278     { EVENT_OBJECT_CREATE, winevent_hook|wparam|lparam, OBJID_CARET, 0 }, /* hook 1/2 */
8279     { EVENT_OBJECT_SHOW, winevent_hook|wparam|lparam, OBJID_CARET, 0 }, /* hook 1/2 */
8280     { EVENT_OBJECT_NAMECHANGE, winevent_hook|wparam|lparam, OBJID_CARET, 0 }, /* hook 1/2 */
8281     { 0 }
8282 };
8283 static const struct message WmWinEventAlertSeq[] = {
8284     { EVENT_OBJECT_LOCATIONCHANGE, winevent_hook|wparam|lparam, OBJID_ALERT, 0 },
8285     { 0 }
8286 };
8287 static const struct message WmWinEventAlertSeq_2[] = {
8288     /* create window in the thread proc */
8289     { EVENT_OBJECT_CREATE, winevent_hook|wparam|lparam, OBJID_WINDOW, 2 },
8290     /* our test event */
8291     { EVENT_OBJECT_LOCATIONCHANGE, winevent_hook|wparam|lparam, OBJID_ALERT, 2 },
8292     { 0 }
8293 };
8294 static const struct message WmGlobalHookSeq_1[] = {
8295     /* create window in the thread proc */
8296     { HCBT_CREATEWND, hook|lparam, 0, 2 },
8297     /* our test events */
8298     { HCBT_SYSCOMMAND, hook|wparam|lparam, SC_PREVWINDOW, 2 },
8299     { HCBT_SYSCOMMAND, hook|wparam|lparam, SC_NEXTWINDOW, 2 },
8300     { 0 }
8301 };
8302 static const struct message WmGlobalHookSeq_2[] = {
8303     { HCBT_SYSCOMMAND, hook|wparam|lparam, SC_NEXTWINDOW, 0 }, /* old local hook */
8304     { HCBT_SYSCOMMAND, hook|wparam|lparam, SC_NEXTWINDOW, 2 }, /* new global hook */
8305     { HCBT_SYSCOMMAND, hook|wparam|lparam, SC_PREVWINDOW, 0 }, /* old local hook */
8306     { HCBT_SYSCOMMAND, hook|wparam|lparam, SC_PREVWINDOW, 2 }, /* new global hook */
8307     { 0 }
8308 };
8309
8310 static const struct message WmMouseLLHookSeq[] = {
8311     { WM_MOUSEMOVE, hook },
8312     { WM_LBUTTONUP, hook },
8313     { WM_MOUSEMOVE, hook },
8314     { 0 }
8315 };
8316
8317 static void CALLBACK win_event_global_hook_proc(HWINEVENTHOOK hevent,
8318                                          DWORD event,
8319                                          HWND hwnd,
8320                                          LONG object_id,
8321                                          LONG child_id,
8322                                          DWORD thread_id,
8323                                          DWORD event_time)
8324 {
8325     char buf[256];
8326
8327     if (GetClassNameA(hwnd, buf, sizeof(buf)))
8328     {
8329         if (!lstrcmpiA(buf, "TestWindowClass") ||
8330             !lstrcmpiA(buf, "static"))
8331         {
8332             struct recvd_message msg;
8333
8334             msg.hwnd = hwnd;
8335             msg.message = event;
8336             msg.flags = winevent_hook|wparam|lparam;
8337             msg.wParam = object_id;
8338             msg.lParam = (thread_id == GetCurrentThreadId()) ? child_id : (child_id + 2);
8339             msg.descr = "WEH_2";
8340             add_message(&msg);
8341         }
8342     }
8343 }
8344
8345 static HHOOK hCBT_global_hook;
8346 static DWORD cbt_global_hook_thread_id;
8347
8348 static LRESULT CALLBACK cbt_global_hook_proc(int nCode, WPARAM wParam, LPARAM lParam) 
8349
8350     HWND hwnd;
8351     char buf[256];
8352
8353     if (nCode == HCBT_SYSCOMMAND)
8354     {
8355         struct recvd_message msg;
8356
8357         msg.hwnd = 0;
8358         msg.message = nCode;
8359         msg.flags = hook|wparam|lparam;
8360         msg.wParam = wParam;
8361         msg.lParam = (cbt_global_hook_thread_id == GetCurrentThreadId()) ? 1 : 2;
8362         msg.descr = "CBT_2";
8363         add_message(&msg);
8364
8365         return CallNextHookEx(hCBT_global_hook, nCode, wParam, lParam);
8366     }
8367     /* WH_MOUSE_LL hook */
8368     if (nCode == HC_ACTION)
8369     {
8370         MSLLHOOKSTRUCT *mhll = (MSLLHOOKSTRUCT *)lParam;
8371
8372         /* we can't test for real mouse events */
8373         if (mhll->flags & LLMHF_INJECTED)
8374         {
8375             struct recvd_message msg;
8376
8377             memset (&msg, 0, sizeof (msg));
8378             msg.message = wParam;
8379             msg.flags = hook;
8380             msg.descr = "CBT_2";
8381             add_message(&msg);
8382         }
8383         return CallNextHookEx(hCBT_global_hook, nCode, wParam, lParam);
8384     }
8385
8386     /* Log also SetFocus(0) calls */
8387     hwnd = wParam ? (HWND)wParam : (HWND)lParam;
8388
8389     if (GetClassNameA(hwnd, buf, sizeof(buf)))
8390     {
8391         if (!lstrcmpiA(buf, "TestWindowClass") ||
8392             !lstrcmpiA(buf, "static"))
8393         {
8394             struct recvd_message msg;
8395
8396             msg.hwnd = hwnd;
8397             msg.message = nCode;
8398             msg.flags = hook|wparam|lparam;
8399             msg.wParam = wParam;
8400             msg.lParam = (cbt_global_hook_thread_id == GetCurrentThreadId()) ? 1 : 2;
8401             msg.descr = "CBT_2";
8402             add_message(&msg);
8403         }
8404     }
8405     return CallNextHookEx(hCBT_global_hook, nCode, wParam, lParam);
8406 }
8407
8408 static DWORD WINAPI win_event_global_thread_proc(void *param)
8409 {
8410     HWND hwnd;
8411     MSG msg;
8412     HANDLE hevent = *(HANDLE *)param;
8413
8414     assert(pNotifyWinEvent);
8415
8416     hwnd = CreateWindowExA(0, "static", NULL, WS_POPUP, 0,0,0,0,0,0,0, NULL);
8417     assert(hwnd);
8418     trace("created thread window %p\n", hwnd);
8419
8420     *(HWND *)param = hwnd;
8421
8422     flush_sequence();
8423     /* this event should be received only by our new hook proc,
8424      * an old one does not expect an event from another thread.
8425      */
8426     pNotifyWinEvent(EVENT_OBJECT_LOCATIONCHANGE, hwnd, OBJID_ALERT, 0);
8427     SetEvent(hevent);
8428
8429     while (GetMessage(&msg, 0, 0, 0))
8430     {
8431         TranslateMessage(&msg);
8432         DispatchMessage(&msg);
8433     }
8434     return 0;
8435 }
8436
8437 static DWORD WINAPI cbt_global_hook_thread_proc(void *param)
8438 {
8439     HWND hwnd;
8440     MSG msg;
8441     HANDLE hevent = *(HANDLE *)param;
8442
8443     flush_sequence();
8444     /* these events should be received only by our new hook proc,
8445      * an old one does not expect an event from another thread.
8446      */
8447
8448     hwnd = CreateWindowExA(0, "static", NULL, WS_POPUP, 0,0,0,0,0,0,0, NULL);
8449     assert(hwnd);
8450     trace("created thread window %p\n", hwnd);
8451
8452     *(HWND *)param = hwnd;
8453
8454     /* Windows doesn't like when a thread plays games with the focus,
8455        that leads to all kinds of misbehaviours and failures to activate
8456        a window. So, better keep next lines commented out.
8457     SetFocus(0);
8458     SetFocus(hwnd);*/
8459
8460     DefWindowProcA(hwnd, WM_SYSCOMMAND, SC_PREVWINDOW, 0);
8461     DefWindowProcA(hwnd, WM_SYSCOMMAND, SC_NEXTWINDOW, 0);
8462
8463     SetEvent(hevent);
8464
8465     while (GetMessage(&msg, 0, 0, 0))
8466     {
8467         TranslateMessage(&msg);
8468         DispatchMessage(&msg);
8469     }
8470     return 0;
8471 }
8472
8473 static DWORD WINAPI mouse_ll_global_thread_proc(void *param)
8474 {
8475     HWND hwnd;
8476     MSG msg;
8477     HANDLE hevent = *(HANDLE *)param;
8478
8479     hwnd = CreateWindowExA(0, "static", NULL, WS_POPUP, 0,0,0,0,0,0,0, NULL);
8480     assert(hwnd);
8481     trace("created thread window %p\n", hwnd);
8482
8483     *(HWND *)param = hwnd;
8484
8485     flush_sequence();
8486
8487     /* Windows doesn't like when a thread plays games with the focus,
8488      * that leads to all kinds of misbehaviours and failures to activate
8489      * a window. So, better don't generate a mouse click message below.
8490      */
8491     mouse_event(MOUSEEVENTF_MOVE, -1, 0, 0, 0);
8492     mouse_event(MOUSEEVENTF_LEFTUP, 0, 0, 0, 0);
8493     mouse_event(MOUSEEVENTF_MOVE, 1, 0, 0, 0);
8494
8495     SetEvent(hevent);
8496     while (GetMessage(&msg, 0, 0, 0))
8497     {
8498         TranslateMessage(&msg);
8499         DispatchMessage(&msg);
8500     }
8501     return 0;
8502 }
8503
8504 static void test_winevents(void)
8505 {
8506     BOOL ret;
8507     MSG msg;
8508     HWND hwnd, hwnd2;
8509     UINT i;
8510     HANDLE hthread, hevent;
8511     DWORD tid;
8512     HWINEVENTHOOK hhook;
8513     const struct message *events = WmWinEventsSeq;
8514
8515     hwnd = CreateWindowExA(0, "TestWindowClass", NULL,
8516                            WS_OVERLAPPEDWINDOW,
8517                            CW_USEDEFAULT, CW_USEDEFAULT, 300, 300, 0,
8518                            NULL, NULL, 0);
8519     assert(hwnd);
8520
8521     /****** start of global hook test *************/
8522     hCBT_global_hook = SetWindowsHookExA(WH_CBT, cbt_global_hook_proc, GetModuleHandleA(0), 0);
8523     if (!hCBT_global_hook)
8524     {
8525         ok(DestroyWindow(hwnd), "failed to destroy window\n");
8526         skip( "cannot set global hook\n" );
8527         return;
8528     }
8529
8530     hevent = CreateEventA(NULL, 0, 0, NULL);
8531     assert(hevent);
8532     hwnd2 = hevent;
8533
8534     hthread = CreateThread(NULL, 0, cbt_global_hook_thread_proc, &hwnd2, 0, &tid);
8535     ok(hthread != NULL, "CreateThread failed, error %d\n", GetLastError());
8536
8537     ok(WaitForSingleObject(hevent, INFINITE) == WAIT_OBJECT_0, "WaitForSingleObject failed\n");
8538
8539     ok_sequence(WmGlobalHookSeq_1, "global hook 1", FALSE);
8540
8541     flush_sequence();
8542     /* this one should be received only by old hook proc */
8543     DefWindowProcA(hwnd, WM_SYSCOMMAND, SC_NEXTWINDOW, 0);
8544     /* this one should be received only by old hook proc */
8545     DefWindowProcA(hwnd, WM_SYSCOMMAND, SC_PREVWINDOW, 0);
8546
8547     ok_sequence(WmGlobalHookSeq_2, "global hook 2", FALSE);
8548
8549     ret = UnhookWindowsHookEx(hCBT_global_hook);
8550     ok( ret, "UnhookWindowsHookEx error %d\n", GetLastError());
8551
8552     PostThreadMessageA(tid, WM_QUIT, 0, 0);
8553     ok(WaitForSingleObject(hthread, INFINITE) == WAIT_OBJECT_0, "WaitForSingleObject failed\n");
8554     CloseHandle(hthread);
8555     CloseHandle(hevent);
8556     ok(!IsWindow(hwnd2), "window should be destroyed on thread exit\n");
8557     /****** end of global hook test *************/
8558
8559     if (!pSetWinEventHook || !pNotifyWinEvent || !pUnhookWinEvent)
8560     {
8561         ok(DestroyWindow(hwnd), "failed to destroy window\n");
8562         return;
8563     }
8564
8565     flush_sequence();
8566
8567     if (0)
8568     {
8569     /* this test doesn't pass under Win9x */
8570     /* win2k ignores events with hwnd == 0 */
8571     SetLastError(0xdeadbeef);
8572     pNotifyWinEvent(events[0].message, 0, events[0].wParam, events[0].lParam);
8573     ok(GetLastError() == ERROR_INVALID_WINDOW_HANDLE || /* Win2k */
8574        GetLastError() == 0xdeadbeef, /* Win9x */
8575        "unexpected error %d\n", GetLastError());
8576     ok_sequence(WmEmptySeq, "empty notify winevents", FALSE);
8577     }
8578
8579     for (i = 0; i < sizeof(WmWinEventsSeq)/sizeof(WmWinEventsSeq[0]); i++)
8580         pNotifyWinEvent(events[i].message, hwnd, events[i].wParam, events[i].lParam);
8581
8582     ok_sequence(WmWinEventsSeq, "notify winevents", FALSE);
8583
8584     /****** start of event filtering test *************/
8585     hhook = pSetWinEventHook(
8586         EVENT_OBJECT_SHOW, /* 0x8002 */
8587         EVENT_OBJECT_LOCATIONCHANGE, /* 0x800B */
8588         GetModuleHandleA(0), win_event_global_hook_proc,
8589         GetCurrentProcessId(), 0,
8590         WINEVENT_INCONTEXT);
8591     ok(hhook != 0, "SetWinEventHook error %d\n", GetLastError());
8592
8593     hevent = CreateEventA(NULL, 0, 0, NULL);
8594     assert(hevent);
8595     hwnd2 = hevent;
8596
8597     hthread = CreateThread(NULL, 0, win_event_global_thread_proc, &hwnd2, 0, &tid);
8598     ok(hthread != NULL, "CreateThread failed, error %d\n", GetLastError());
8599
8600     ok(WaitForSingleObject(hevent, INFINITE) == WAIT_OBJECT_0, "WaitForSingleObject failed\n");
8601
8602     ok_sequence(WmWinEventAlertSeq, "alert winevent", FALSE);
8603
8604     flush_sequence();
8605     /* this one should be received only by old hook proc */
8606     pNotifyWinEvent(EVENT_OBJECT_CREATE, hwnd, OBJID_CARET, 0); /* 0x8000 */
8607     pNotifyWinEvent(EVENT_OBJECT_SHOW, hwnd, OBJID_CARET, 0); /* 0x8002 */
8608     /* this one should be received only by old hook proc */
8609     pNotifyWinEvent(EVENT_OBJECT_NAMECHANGE, hwnd, OBJID_CARET, 0); /* 0x800C */
8610
8611     ok_sequence(WmWinEventCaretSeq, "caret winevent", FALSE);
8612
8613     ret = pUnhookWinEvent(hhook);
8614     ok( ret, "UnhookWinEvent error %d\n", GetLastError());
8615
8616     PostThreadMessageA(tid, WM_QUIT, 0, 0);
8617     ok(WaitForSingleObject(hthread, INFINITE) == WAIT_OBJECT_0, "WaitForSingleObject failed\n");
8618     CloseHandle(hthread);
8619     CloseHandle(hevent);
8620     ok(!IsWindow(hwnd2), "window should be destroyed on thread exit\n");
8621     /****** end of event filtering test *************/
8622
8623     /****** start of out of context event test *************/
8624     hhook = pSetWinEventHook(EVENT_MIN, EVENT_MAX, 0,
8625         win_event_global_hook_proc, GetCurrentProcessId(), 0,
8626         WINEVENT_OUTOFCONTEXT);
8627     ok(hhook != 0, "SetWinEventHook error %d\n", GetLastError());
8628
8629     hevent = CreateEventA(NULL, 0, 0, NULL);
8630     assert(hevent);
8631     hwnd2 = hevent;
8632
8633     flush_sequence();
8634
8635     hthread = CreateThread(NULL, 0, win_event_global_thread_proc, &hwnd2, 0, &tid);
8636     ok(hthread != NULL, "CreateThread failed, error %d\n", GetLastError());
8637
8638     ok(WaitForSingleObject(hevent, INFINITE) == WAIT_OBJECT_0, "WaitForSingleObject failed\n");
8639
8640     ok_sequence(WmEmptySeq, "empty notify winevents", FALSE);
8641     /* process pending winevent messages */
8642     ok(!PeekMessageA(&msg, 0, 0, 0, PM_NOREMOVE), "msg queue should be empty\n");
8643     ok_sequence(WmWinEventAlertSeq_2, "alert winevent for out of context proc", FALSE);
8644
8645     flush_sequence();
8646     /* this one should be received only by old hook proc */
8647     pNotifyWinEvent(EVENT_OBJECT_CREATE, hwnd, OBJID_CARET, 0); /* 0x8000 */
8648     pNotifyWinEvent(EVENT_OBJECT_SHOW, hwnd, OBJID_CARET, 0); /* 0x8002 */
8649     /* this one should be received only by old hook proc */
8650     pNotifyWinEvent(EVENT_OBJECT_NAMECHANGE, hwnd, OBJID_CARET, 0); /* 0x800C */
8651
8652     ok_sequence(WmWinEventCaretSeq_2, "caret winevent for incontext proc", FALSE);
8653     /* process pending winevent messages */
8654     ok(!PeekMessageA(&msg, 0, 0, 0, PM_NOREMOVE), "msg queue should be empty\n");
8655     ok_sequence(WmWinEventCaretSeq_2, "caret winevent for out of context proc", FALSE);
8656
8657     ret = pUnhookWinEvent(hhook);
8658     ok( ret, "UnhookWinEvent error %d\n", GetLastError());
8659
8660     PostThreadMessageA(tid, WM_QUIT, 0, 0);
8661     ok(WaitForSingleObject(hthread, INFINITE) == WAIT_OBJECT_0, "WaitForSingleObject failed\n");
8662     CloseHandle(hthread);
8663     CloseHandle(hevent);
8664     ok(!IsWindow(hwnd2), "window should be destroyed on thread exit\n");
8665     /****** end of out of context event test *************/
8666
8667     /****** start of MOUSE_LL hook test *************/
8668     hCBT_global_hook = SetWindowsHookExA(WH_MOUSE_LL, cbt_global_hook_proc, GetModuleHandleA(0), 0);
8669     /* WH_MOUSE_LL is not supported on Win9x platforms */
8670     if (!hCBT_global_hook)
8671     {
8672         win_skip("Skipping WH_MOUSE_LL test on this platform\n");
8673         goto skip_mouse_ll_hook_test;
8674     }
8675
8676     hevent = CreateEventA(NULL, 0, 0, NULL);
8677     assert(hevent);
8678     hwnd2 = hevent;
8679
8680     hthread = CreateThread(NULL, 0, mouse_ll_global_thread_proc, &hwnd2, 0, &tid);
8681     ok(hthread != NULL, "CreateThread failed, error %d\n", GetLastError());
8682
8683     while (WaitForSingleObject(hevent, 100) == WAIT_TIMEOUT)
8684         while (PeekMessage( &msg, 0, 0, 0, PM_REMOVE )) DispatchMessage( &msg );
8685
8686     ok_sequence(WmMouseLLHookSeq, "MOUSE_LL hook other thread", FALSE);
8687     flush_sequence();
8688
8689     mouse_event(MOUSEEVENTF_MOVE, -1, 0, 0, 0);
8690     mouse_event(MOUSEEVENTF_LEFTUP, 0, 0, 0, 0);
8691     mouse_event(MOUSEEVENTF_MOVE, 1, 0, 0, 0);
8692
8693     ok_sequence(WmMouseLLHookSeq, "MOUSE_LL hook same thread", FALSE);
8694
8695     ret = UnhookWindowsHookEx(hCBT_global_hook);
8696     ok( ret, "UnhookWindowsHookEx error %d\n", GetLastError());
8697
8698     PostThreadMessageA(tid, WM_QUIT, 0, 0);
8699     ok(WaitForSingleObject(hthread, INFINITE) == WAIT_OBJECT_0, "WaitForSingleObject failed\n");
8700     CloseHandle(hthread);
8701     CloseHandle(hevent);
8702     ok(!IsWindow(hwnd2), "window should be destroyed on thread exit\n");
8703     /****** end of MOUSE_LL hook test *************/
8704 skip_mouse_ll_hook_test:
8705
8706     ok(DestroyWindow(hwnd), "failed to destroy window\n");
8707 }
8708
8709 static void test_set_hook(void)
8710 {
8711     BOOL ret;
8712     HHOOK hhook;
8713     HWINEVENTHOOK hwinevent_hook;
8714
8715     hhook = SetWindowsHookExA(WH_CBT, cbt_hook_proc, GetModuleHandleA(0), GetCurrentThreadId());
8716     ok(hhook != 0, "local hook does not require hModule set to 0\n");
8717     UnhookWindowsHookEx(hhook);
8718
8719     if (0)
8720     {
8721     /* this test doesn't pass under Win9x: BUG! */
8722     SetLastError(0xdeadbeef);
8723     hhook = SetWindowsHookExA(WH_CBT, cbt_hook_proc, 0, 0);
8724     ok(!hhook, "global hook requires hModule != 0\n");
8725     ok(GetLastError() == ERROR_HOOK_NEEDS_HMOD, "unexpected error %d\n", GetLastError());
8726     }
8727
8728     SetLastError(0xdeadbeef);
8729     hhook = SetWindowsHookExA(WH_CBT, 0, GetModuleHandleA(0), GetCurrentThreadId());
8730     ok(!hhook, "SetWinEventHook with invalid proc should fail\n");
8731     ok(GetLastError() == ERROR_INVALID_FILTER_PROC || /* Win2k */
8732        GetLastError() == 0xdeadbeef, /* Win9x */
8733        "unexpected error %d\n", GetLastError());
8734
8735     SetLastError(0xdeadbeef);
8736     ok(!UnhookWindowsHookEx((HHOOK)0xdeadbeef), "UnhookWindowsHookEx succeeded\n");
8737     ok(GetLastError() == ERROR_INVALID_HOOK_HANDLE || /* Win2k */
8738        GetLastError() == 0xdeadbeef, /* Win9x */
8739        "unexpected error %d\n", GetLastError());
8740
8741     if (!pSetWinEventHook || !pUnhookWinEvent) return;
8742
8743     /* even process local incontext hooks require hmodule */
8744     SetLastError(0xdeadbeef);
8745     hwinevent_hook = pSetWinEventHook(EVENT_MIN, EVENT_MAX, 0, win_event_proc,
8746         GetCurrentProcessId(), 0, WINEVENT_INCONTEXT);
8747     ok(!hwinevent_hook, "WINEVENT_INCONTEXT requires hModule != 0\n");
8748     ok(GetLastError() == ERROR_HOOK_NEEDS_HMOD || /* Win2k */
8749        GetLastError() == 0xdeadbeef, /* Win9x */
8750        "unexpected error %d\n", GetLastError());
8751
8752     /* even thread local incontext hooks require hmodule */
8753     SetLastError(0xdeadbeef);
8754     hwinevent_hook = pSetWinEventHook(EVENT_MIN, EVENT_MAX, 0, win_event_proc,
8755         GetCurrentProcessId(), GetCurrentThreadId(), WINEVENT_INCONTEXT);
8756     ok(!hwinevent_hook, "WINEVENT_INCONTEXT requires hModule != 0\n");
8757     ok(GetLastError() == ERROR_HOOK_NEEDS_HMOD || /* Win2k */
8758        GetLastError() == 0xdeadbeef, /* Win9x */
8759        "unexpected error %d\n", GetLastError());
8760
8761     if (0)
8762     {
8763     /* these 3 tests don't pass under Win9x */
8764     SetLastError(0xdeadbeef);
8765     hwinevent_hook = pSetWinEventHook(1, 0, 0, win_event_proc,
8766         GetCurrentProcessId(), 0, WINEVENT_OUTOFCONTEXT);
8767     ok(!hwinevent_hook, "SetWinEventHook with invalid event range should fail\n");
8768     ok(GetLastError() == ERROR_INVALID_HOOK_FILTER, "unexpected error %d\n", GetLastError());
8769
8770     SetLastError(0xdeadbeef);
8771     hwinevent_hook = pSetWinEventHook(-1, 1, 0, win_event_proc,
8772         GetCurrentProcessId(), 0, WINEVENT_OUTOFCONTEXT);
8773     ok(!hwinevent_hook, "SetWinEventHook with invalid event range should fail\n");
8774     ok(GetLastError() == ERROR_INVALID_HOOK_FILTER, "unexpected error %d\n", GetLastError());
8775
8776     SetLastError(0xdeadbeef);
8777     hwinevent_hook = pSetWinEventHook(EVENT_MIN, EVENT_MAX, 0, win_event_proc,
8778         0, 0xdeadbeef, WINEVENT_OUTOFCONTEXT);
8779     ok(!hwinevent_hook, "SetWinEventHook with invalid tid should fail\n");
8780     ok(GetLastError() == ERROR_INVALID_THREAD_ID, "unexpected error %d\n", GetLastError());
8781     }
8782
8783     SetLastError(0xdeadbeef);
8784     hwinevent_hook = pSetWinEventHook(0, 0, 0, win_event_proc,
8785         GetCurrentProcessId(), 0, WINEVENT_OUTOFCONTEXT);
8786     ok(hwinevent_hook != 0, "SetWinEventHook error %d\n", GetLastError());
8787     ok(GetLastError() == 0xdeadbeef, "unexpected error %d\n", GetLastError());
8788     ret = pUnhookWinEvent(hwinevent_hook);
8789     ok( ret, "UnhookWinEvent error %d\n", GetLastError());
8790
8791 todo_wine {
8792     /* This call succeeds under win2k SP4, but fails under Wine.
8793        Does win2k test/use passed process id? */
8794     SetLastError(0xdeadbeef);
8795     hwinevent_hook = pSetWinEventHook(EVENT_MIN, EVENT_MAX, 0, win_event_proc,
8796         0xdeadbeef, 0, WINEVENT_OUTOFCONTEXT);
8797     ok(hwinevent_hook != 0, "SetWinEventHook error %d\n", GetLastError());
8798     ok(GetLastError() == 0xdeadbeef, "unexpected error %d\n", GetLastError());
8799     ret = pUnhookWinEvent(hwinevent_hook);
8800     ok( ret, "UnhookWinEvent error %d\n", GetLastError());
8801 }
8802
8803     SetLastError(0xdeadbeef);
8804     ok(!pUnhookWinEvent((HWINEVENTHOOK)0xdeadbeef), "UnhookWinEvent succeeded\n");
8805     ok(GetLastError() == ERROR_INVALID_HANDLE || /* Win2k */
8806         GetLastError() == 0xdeadbeef, /* Win9x */
8807         "unexpected error %d\n", GetLastError());
8808 }
8809
8810 static const struct message ScrollWindowPaint1[] = {
8811     { WM_PAINT, sent },
8812     { WM_ERASEBKGND, sent|beginpaint },
8813     { WM_GETTEXTLENGTH, sent|optional },
8814     { WM_PAINT, sent|optional },
8815     { WM_NCPAINT, sent|beginpaint|optional },
8816     { WM_GETTEXT, sent|beginpaint|optional },
8817     { WM_GETTEXT, sent|beginpaint|optional },
8818     { WM_GETTEXT, sent|beginpaint|optional },
8819     { WM_GETTEXT, sent|beginpaint|defwinproc|optional },
8820     { WM_ERASEBKGND, sent|beginpaint|optional },
8821     { 0 }
8822 };
8823
8824 static const struct message ScrollWindowPaint2[] = {
8825     { WM_PAINT, sent },
8826     { 0 }
8827 };
8828
8829 static void test_scrollwindowex(void)
8830 {
8831     HWND hwnd, hchild;
8832     RECT rect={0,0,130,130};
8833
8834     hwnd = CreateWindowExA(0, "TestWindowClass", "Test Scroll",
8835             WS_VISIBLE|WS_OVERLAPPEDWINDOW,
8836             100, 100, 200, 200, 0, 0, 0, NULL);
8837     ok (hwnd != 0, "Failed to create overlapped window\n");
8838     hchild = CreateWindowExA(0, "TestWindowClass", "Test child", 
8839             WS_VISIBLE|WS_CAPTION|WS_CHILD,
8840             10, 10, 150, 150, hwnd, 0, 0, NULL);
8841     ok (hchild != 0, "Failed to create child\n");
8842     UpdateWindow(hwnd);
8843     flush_events();
8844     flush_sequence();
8845
8846     /* scroll without the child window */
8847     trace("start scroll\n");
8848     ScrollWindowEx( hwnd, 10, 10, &rect, NULL, NULL, NULL,
8849             SW_ERASE|SW_INVALIDATE);
8850     ok_sequence(WmEmptySeq, "ScrollWindowEx", 0);
8851     trace("end scroll\n");
8852     flush_sequence();
8853     flush_events();
8854     ok_sequence(ScrollWindowPaint1, "ScrollWindowEx", 0);
8855     flush_events();
8856     flush_sequence();
8857
8858     /* Now without the SW_ERASE flag */
8859     trace("start scroll\n");
8860     ScrollWindowEx( hwnd, 10, 10, &rect, NULL, NULL, NULL, SW_INVALIDATE);
8861     ok_sequence(WmEmptySeq, "ScrollWindowEx", 0);
8862     trace("end scroll\n");
8863     flush_sequence();
8864     flush_events();
8865     ok_sequence(ScrollWindowPaint2, "ScrollWindowEx", 0);
8866     flush_events();
8867     flush_sequence();
8868
8869     /* now scroll the child window as well */
8870     trace("start scroll\n");
8871     ScrollWindowEx( hwnd, 10, 10, &rect, NULL, NULL, NULL,
8872             SW_SCROLLCHILDREN|SW_ERASE|SW_INVALIDATE);
8873     /* wine sends WM_POSCHANGING, WM_POSCHANGED messages */
8874     /* windows sometimes a WM_MOVE */
8875     ok_sequence(WmEmptySeq, "ScrollWindowEx", TRUE);
8876     trace("end scroll\n");
8877     flush_sequence();
8878     flush_events();
8879     ok_sequence(ScrollWindowPaint1, "ScrollWindowEx", 0);
8880     flush_events();
8881     flush_sequence();
8882
8883     /* now scroll with ScrollWindow() */
8884     trace("start scroll with ScrollWindow\n");
8885     ScrollWindow( hwnd, 5, 5, NULL, NULL);
8886     trace("end scroll\n");
8887     flush_sequence();
8888     flush_events();
8889     ok_sequence(ScrollWindowPaint1, "ScrollWindow", 0);
8890
8891     ok(DestroyWindow(hchild), "failed to destroy window\n");
8892     ok(DestroyWindow(hwnd), "failed to destroy window\n");
8893     flush_sequence();
8894 }
8895
8896 static const struct message destroy_window_with_children[] = {
8897     { EVENT_SYSTEM_CAPTURESTART, winevent_hook|wparam|lparam, 0, 0 }, /* popup */
8898     { HCBT_DESTROYWND, hook|lparam, 0, WND_PARENT_ID }, /* parent */
8899     { 0x0090, sent|optional },
8900     { HCBT_DESTROYWND, hook|lparam, 0, WND_POPUP_ID }, /* popup */
8901     { 0x0090, sent|optional },
8902     { EVENT_OBJECT_DESTROY, winevent_hook|wparam|lparam, 0, 0 }, /* popup */
8903     { WM_DESTROY, sent|wparam|lparam, 0, WND_POPUP_ID }, /* popup */
8904     { WM_CAPTURECHANGED, sent|wparam|lparam, 0, WND_POPUP_ID }, /* popup */
8905     { WM_NCDESTROY, sent|wparam|lparam, 0, WND_POPUP_ID }, /* popup */
8906     { EVENT_OBJECT_DESTROY, winevent_hook|wparam|lparam, 0, 0 }, /* parent */
8907     { WM_DESTROY, sent|wparam|lparam, 0, WND_PARENT_ID }, /* parent */
8908     { WM_DESTROY, sent|wparam|lparam, 0, WND_CHILD_ID + 2 }, /* child2 */
8909     { WM_DESTROY, sent|wparam|lparam, 0, WND_CHILD_ID + 1 }, /* child1 */
8910     { WM_DESTROY, sent|wparam|lparam, 0, WND_CHILD_ID + 3 }, /* child3 */
8911     { WM_NCDESTROY, sent|wparam|lparam, 0, WND_CHILD_ID + 2 }, /* child2 */
8912     { WM_NCDESTROY, sent|wparam|lparam, 0, WND_CHILD_ID + 3 }, /* child3 */
8913     { WM_NCDESTROY, sent|wparam|lparam, 0, WND_CHILD_ID + 1 }, /* child1 */
8914     { WM_NCDESTROY, sent|wparam|lparam, 0, WND_PARENT_ID }, /* parent */
8915     { 0 }
8916 };
8917
8918 static void test_DestroyWindow(void)
8919 {
8920     BOOL ret;
8921     HWND parent, child1, child2, child3, child4, test;
8922     UINT_PTR child_id = WND_CHILD_ID + 1;
8923
8924     parent = CreateWindowExA(0, "TestWindowClass", NULL, WS_OVERLAPPEDWINDOW,
8925                              100, 100, 200, 200, 0, 0, 0, NULL);
8926     assert(parent != 0);
8927     child1 = CreateWindowExA(0, "TestWindowClass", NULL, WS_CHILD,
8928                              0, 0, 50, 50, parent, (HMENU)child_id++, 0, NULL);
8929     assert(child1 != 0);
8930     child2 = CreateWindowExA(0, "TestWindowClass", NULL, WS_CHILD,
8931                              0, 0, 50, 50, GetDesktopWindow(), (HMENU)child_id++, 0, NULL);
8932     assert(child2 != 0);
8933     child3 = CreateWindowExA(0, "TestWindowClass", NULL, WS_CHILD,
8934                              0, 0, 50, 50, child1, (HMENU)child_id++, 0, NULL);
8935     assert(child3 != 0);
8936     child4 = CreateWindowExA(0, "TestWindowClass", NULL, WS_POPUP,
8937                              0, 0, 50, 50, parent, 0, 0, NULL);
8938     assert(child4 != 0);
8939
8940     /* test owner/parent of child2 */
8941     test = GetParent(child2);
8942     ok(test == GetDesktopWindow(), "wrong parent %p\n", test);
8943     ok(!IsChild(parent, child2), "wrong parent/child %p/%p\n", parent, child2);
8944     if(pGetAncestor) {
8945         test = pGetAncestor(child2, GA_PARENT);
8946         ok(test == GetDesktopWindow(), "wrong parent %p\n", test);
8947     }
8948     test = GetWindow(child2, GW_OWNER);
8949     ok(!test, "wrong owner %p\n", test);
8950
8951     test = SetParent(child2, parent);
8952     ok(test == GetDesktopWindow(), "wrong old parent %p\n", test);
8953
8954     /* test owner/parent of the parent */
8955     test = GetParent(parent);
8956     ok(!test, "wrong parent %p\n", test);
8957     ok(!IsChild(GetDesktopWindow(), parent), "wrong parent/child %p/%p\n", GetDesktopWindow(), parent);
8958     if(pGetAncestor) {
8959         test = pGetAncestor(parent, GA_PARENT);
8960         ok(test == GetDesktopWindow(), "wrong parent %p\n", test);
8961     }
8962     test = GetWindow(parent, GW_OWNER);
8963     ok(!test, "wrong owner %p\n", test);
8964
8965     /* test owner/parent of child1 */
8966     test = GetParent(child1);
8967     ok(test == parent, "wrong parent %p\n", test);
8968     ok(IsChild(parent, child1), "wrong parent/child %p/%p\n", parent, child1);
8969     if(pGetAncestor) {
8970         test = pGetAncestor(child1, GA_PARENT);
8971         ok(test == parent, "wrong parent %p\n", test);
8972     }
8973     test = GetWindow(child1, GW_OWNER);
8974     ok(!test, "wrong owner %p\n", test);
8975
8976     /* test owner/parent of child2 */
8977     test = GetParent(child2);
8978     ok(test == parent, "wrong parent %p\n", test);
8979     ok(IsChild(parent, child2), "wrong parent/child %p/%p\n", parent, child2);
8980     if(pGetAncestor) {
8981         test = pGetAncestor(child2, GA_PARENT);
8982         ok(test == parent, "wrong parent %p\n", test);
8983     }
8984     test = GetWindow(child2, GW_OWNER);
8985     ok(!test, "wrong owner %p\n", test);
8986
8987     /* test owner/parent of child3 */
8988     test = GetParent(child3);
8989     ok(test == child1, "wrong parent %p\n", test);
8990     ok(IsChild(parent, child3), "wrong parent/child %p/%p\n", parent, child3);
8991     if(pGetAncestor) {
8992         test = pGetAncestor(child3, GA_PARENT);
8993         ok(test == child1, "wrong parent %p\n", test);
8994     }
8995     test = GetWindow(child3, GW_OWNER);
8996     ok(!test, "wrong owner %p\n", test);
8997
8998     /* test owner/parent of child4 */
8999     test = GetParent(child4);
9000     ok(test == parent, "wrong parent %p\n", test);
9001     ok(!IsChild(parent, child4), "wrong parent/child %p/%p\n", parent, child4);
9002     if(pGetAncestor) {
9003         test = pGetAncestor(child4, GA_PARENT);
9004         ok(test == GetDesktopWindow(), "wrong parent %p\n", test);
9005     }
9006     test = GetWindow(child4, GW_OWNER);
9007     ok(test == parent, "wrong owner %p\n", test);
9008
9009     flush_sequence();
9010
9011     trace("parent %p, child1 %p, child2 %p, child3 %p, child4 %p\n",
9012            parent, child1, child2, child3, child4);
9013
9014     SetCapture(child4);
9015     test = GetCapture();
9016     ok(test == child4, "wrong capture window %p\n", test);
9017
9018     test_DestroyWindow_flag = TRUE;
9019     ret = DestroyWindow(parent);
9020     ok( ret, "DestroyWindow() error %d\n", GetLastError());
9021     test_DestroyWindow_flag = FALSE;
9022     ok_sequence(destroy_window_with_children, "destroy window with children", 0);
9023
9024     ok(!IsWindow(parent), "parent still exists\n");
9025     ok(!IsWindow(child1), "child1 still exists\n");
9026     ok(!IsWindow(child2), "child2 still exists\n");
9027     ok(!IsWindow(child3), "child3 still exists\n");
9028     ok(!IsWindow(child4), "child4 still exists\n");
9029
9030     test = GetCapture();
9031     ok(!test, "wrong capture window %p\n", test);
9032 }
9033
9034
9035 static const struct message WmDispatchPaint[] = {
9036     { WM_NCPAINT, sent },
9037     { WM_GETTEXT, sent|defwinproc|optional },
9038     { WM_GETTEXT, sent|defwinproc|optional },
9039     { WM_ERASEBKGND, sent },
9040     { 0 }
9041 };
9042
9043 static LRESULT WINAPI DispatchMessageCheckProc(HWND hwnd, UINT message, WPARAM wParam, LPARAM lParam)
9044 {
9045     if (message == WM_PAINT) return 0;
9046     return MsgCheckProcA( hwnd, message, wParam, lParam );
9047 }
9048
9049 static void test_DispatchMessage(void)
9050 {
9051     RECT rect;
9052     MSG msg;
9053     int count;
9054     HWND hwnd = CreateWindowA( "TestWindowClass", NULL, WS_OVERLAPPEDWINDOW,
9055                                100, 100, 200, 200, 0, 0, 0, NULL);
9056     ShowWindow( hwnd, SW_SHOW );
9057     UpdateWindow( hwnd );
9058     flush_events();
9059     flush_sequence();
9060     SetWindowLongPtrA( hwnd, GWLP_WNDPROC, (LONG_PTR)DispatchMessageCheckProc );
9061
9062     SetRect( &rect, -5, -5, 5, 5 );
9063     RedrawWindow( hwnd, &rect, 0, RDW_INVALIDATE|RDW_ERASE|RDW_FRAME );
9064     count = 0;
9065     while (PeekMessage( &msg, 0, 0, 0, PM_REMOVE ))
9066     {
9067         if (msg.message != WM_PAINT) DispatchMessage( &msg );
9068         else
9069         {
9070             flush_sequence();
9071             DispatchMessage( &msg );
9072             /* DispatchMessage will send WM_NCPAINT if non client area is still invalid after WM_PAINT */
9073             if (!count) ok_sequence( WmDispatchPaint, "WmDispatchPaint", FALSE );
9074             else ok_sequence( WmEmptySeq, "WmEmpty", FALSE );
9075             if (++count > 10) break;
9076         }
9077     }
9078     ok( msg.message == WM_PAINT && count > 10, "WM_PAINT messages stopped\n" );
9079
9080     trace("now without DispatchMessage\n");
9081     flush_sequence();
9082     RedrawWindow( hwnd, &rect, 0, RDW_INVALIDATE|RDW_ERASE|RDW_FRAME );
9083     count = 0;
9084     while (PeekMessage( &msg, 0, 0, 0, PM_REMOVE ))
9085     {
9086         if (msg.message != WM_PAINT) DispatchMessage( &msg );
9087         else
9088         {
9089             HRGN hrgn = CreateRectRgn( 0, 0, 0, 0 );
9090             flush_sequence();
9091             /* this will send WM_NCCPAINT just like DispatchMessage does */
9092             GetUpdateRgn( hwnd, hrgn, TRUE );
9093             ok_sequence( WmDispatchPaint, "WmDispatchPaint", FALSE );
9094             DeleteObject( hrgn );
9095             GetClientRect( hwnd, &rect );
9096             ValidateRect( hwnd, &rect );  /* this will stop WM_PAINTs */
9097             ok( !count, "Got multiple WM_PAINTs\n" );
9098             if (++count > 10) break;
9099         }
9100     }
9101     DestroyWindow(hwnd);
9102 }
9103
9104
9105 static const struct message WmUser[] = {
9106     { WM_USER, sent },
9107     { 0 }
9108 };
9109
9110 struct sendmsg_info
9111 {
9112     HWND  hwnd;
9113     DWORD timeout;
9114     DWORD ret;
9115 };
9116
9117 static DWORD CALLBACK send_msg_thread( LPVOID arg )
9118 {
9119     struct sendmsg_info *info = arg;
9120     SetLastError( 0xdeadbeef );
9121     info->ret = SendMessageTimeoutA( info->hwnd, WM_USER, 0, 0, 0, info->timeout, NULL );
9122     if (!info->ret) ok( GetLastError() == ERROR_TIMEOUT ||
9123                         broken(GetLastError() == 0),  /* win9x */
9124                         "unexpected error %d\n", GetLastError());
9125     return 0;
9126 }
9127
9128 static void wait_for_thread( HANDLE thread )
9129 {
9130     while (MsgWaitForMultipleObjects(1, &thread, FALSE, INFINITE, QS_SENDMESSAGE) != WAIT_OBJECT_0)
9131     {
9132         MSG msg;
9133         while (PeekMessageA( &msg, 0, 0, 0, PM_REMOVE )) DispatchMessage(&msg);
9134     }
9135 }
9136
9137 static LRESULT WINAPI send_msg_delay_proc(HWND hwnd, UINT message, WPARAM wParam, LPARAM lParam)
9138 {
9139     if (message == WM_USER) Sleep(200);
9140     return MsgCheckProcA( hwnd, message, wParam, lParam );
9141 }
9142
9143 static void test_SendMessageTimeout(void)
9144 {
9145     HANDLE thread;
9146     struct sendmsg_info info;
9147     DWORD tid;
9148     BOOL is_win9x;
9149
9150     info.hwnd = CreateWindowA( "TestWindowClass", NULL, WS_OVERLAPPEDWINDOW,
9151                                100, 100, 200, 200, 0, 0, 0, NULL);
9152     flush_events();
9153     flush_sequence();
9154
9155     info.timeout = 1000;
9156     info.ret = 0xdeadbeef;
9157     thread = CreateThread( NULL, 0, send_msg_thread, &info, 0, &tid );
9158     wait_for_thread( thread );
9159     CloseHandle( thread );
9160     ok( info.ret == 1, "SendMessageTimeout failed\n" );
9161     ok_sequence( WmUser, "WmUser", FALSE );
9162
9163     info.timeout = 1;
9164     info.ret = 0xdeadbeef;
9165     thread = CreateThread( NULL, 0, send_msg_thread, &info, 0, &tid );
9166     Sleep(100);  /* SendMessageTimeout should time out here */
9167     wait_for_thread( thread );
9168     CloseHandle( thread );
9169     ok( info.ret == 0, "SendMessageTimeout succeeded\n" );
9170     ok_sequence( WmEmptySeq, "WmEmptySeq", FALSE );
9171
9172     /* 0 means infinite timeout (but not on win9x) */
9173     info.timeout = 0;
9174     info.ret = 0xdeadbeef;
9175     thread = CreateThread( NULL, 0, send_msg_thread, &info, 0, &tid );
9176     Sleep(100);
9177     wait_for_thread( thread );
9178     CloseHandle( thread );
9179     is_win9x = !info.ret;
9180     if (is_win9x) ok_sequence( WmEmptySeq, "WmEmptySeq", FALSE );
9181     else ok_sequence( WmUser, "WmUser", FALSE );
9182
9183     /* timeout is treated as signed despite the prototype (but not on win9x) */
9184     info.timeout = 0x7fffffff;
9185     info.ret = 0xdeadbeef;
9186     thread = CreateThread( NULL, 0, send_msg_thread, &info, 0, &tid );
9187     Sleep(100);
9188     wait_for_thread( thread );
9189     CloseHandle( thread );
9190     ok( info.ret == 1, "SendMessageTimeout failed\n" );
9191     ok_sequence( WmUser, "WmUser", FALSE );
9192
9193     info.timeout = 0x80000000;
9194     info.ret = 0xdeadbeef;
9195     thread = CreateThread( NULL, 0, send_msg_thread, &info, 0, &tid );
9196     Sleep(100);
9197     wait_for_thread( thread );
9198     CloseHandle( thread );
9199     if (is_win9x)
9200     {
9201         ok( info.ret == 1, "SendMessageTimeout failed\n" );
9202         ok_sequence( WmUser, "WmUser", FALSE );
9203     }
9204     else
9205     {
9206         ok( info.ret == 0, "SendMessageTimeout succeeded\n" );
9207         ok_sequence( WmEmptySeq, "WmEmptySeq", FALSE );
9208     }
9209
9210     /* now check for timeout during message processing */
9211     SetWindowLongPtrA( info.hwnd, GWLP_WNDPROC, (LONG_PTR)send_msg_delay_proc );
9212     info.timeout = 100;
9213     info.ret = 0xdeadbeef;
9214     thread = CreateThread( NULL, 0, send_msg_thread, &info, 0, &tid );
9215     wait_for_thread( thread );
9216     CloseHandle( thread );
9217     /* we should time out but still get the message */
9218     ok( info.ret == 0, "SendMessageTimeout failed\n" );
9219     ok_sequence( WmUser, "WmUser", FALSE );
9220
9221     DestroyWindow( info.hwnd );
9222 }
9223
9224
9225 /****************** edit message test *************************/
9226 #define ID_EDIT 0x1234
9227 static const struct message sl_edit_setfocus[] =
9228 {
9229     { HCBT_SETFOCUS, hook },
9230     { WM_IME_SETCONTEXT, sent|wparam|optional, 1 },
9231     { WM_IME_NOTIFY, sent|wparam|defwinproc|optional, 2 },
9232     { EVENT_OBJECT_FOCUS, winevent_hook|wparam|lparam, OBJID_CLIENT, 0 },
9233     { WM_SETFOCUS, sent|wparam, 0 },
9234     { WM_IME_NOTIFY, sent|wparam|defwinproc|optional, 10 },
9235     { WM_IME_NOTIFY, sent|wparam|defwinproc|optional, 15 },
9236     { WM_CTLCOLOREDIT, sent|parent },
9237     { WM_IME_NOTIFY, sent|wparam|defwinproc|optional, 11 },
9238     { EVENT_OBJECT_CREATE, winevent_hook|wparam|lparam, OBJID_CARET, 0 },
9239     { EVENT_OBJECT_SHOW, winevent_hook|wparam|lparam, OBJID_CARET, 0 },
9240     { WM_COMMAND, sent|parent|wparam, MAKEWPARAM(ID_EDIT, EN_SETFOCUS) },
9241     { 0 }
9242 };
9243 static const struct message ml_edit_setfocus[] =
9244 {
9245     { HCBT_SETFOCUS, hook },
9246     { WM_IME_SETCONTEXT, sent|wparam|optional, 1 },
9247     { WM_IME_NOTIFY, sent|wparam|defwinproc|optional, 2 },
9248     { EVENT_OBJECT_FOCUS, winevent_hook|wparam|lparam, OBJID_CLIENT, 0 },
9249     { WM_SETFOCUS, sent|wparam, 0 },
9250     { WM_IME_NOTIFY, sent|wparam|defwinproc|optional, 10 },
9251     { WM_IME_NOTIFY, sent|wparam|defwinproc|optional, 11 },
9252     { EVENT_OBJECT_CREATE, winevent_hook|wparam|lparam, OBJID_CARET, 0 },
9253     { EVENT_OBJECT_SHOW, winevent_hook|wparam|lparam, OBJID_CARET, 0 },
9254     { WM_COMMAND, sent|parent|wparam, MAKEWPARAM(ID_EDIT, EN_SETFOCUS) },
9255     { 0 }
9256 };
9257 static const struct message sl_edit_killfocus[] =
9258 {
9259     { HCBT_SETFOCUS, hook },
9260     { EVENT_OBJECT_FOCUS, winevent_hook|wparam|lparam, OBJID_CLIENT, 0 },
9261     { WM_KILLFOCUS, sent|wparam, 0 },
9262     { EVENT_OBJECT_HIDE, winevent_hook|wparam|lparam, OBJID_CARET, 0 },
9263     { EVENT_OBJECT_DESTROY, winevent_hook|wparam|lparam, OBJID_CARET, 0 },
9264     { WM_COMMAND, sent|parent|wparam, MAKEWPARAM(ID_EDIT, EN_KILLFOCUS) },
9265     { WM_IME_SETCONTEXT, sent|wparam|optional, 0 },
9266     { WM_IME_NOTIFY, sent|wparam|defwinproc|optional, 1 },
9267     { 0 }
9268 };
9269 static const struct message sl_edit_lbutton_dblclk[] =
9270 {
9271     { WM_LBUTTONDBLCLK, sent },
9272     { EVENT_SYSTEM_CAPTURESTART, winevent_hook|wparam|lparam, 0, 0 },
9273     { 0 }
9274 };
9275 static const struct message sl_edit_lbutton_down[] =
9276 {
9277     { WM_LBUTTONDOWN, sent|wparam|lparam, 0, 0 },
9278     { HCBT_SETFOCUS, hook },
9279     { WM_IME_SETCONTEXT, sent|wparam|defwinproc|optional, 1 },
9280     { WM_IME_NOTIFY, sent|wparam|defwinproc|optional, 2 },
9281     { EVENT_OBJECT_FOCUS, winevent_hook|wparam|lparam, OBJID_CLIENT, 0 },
9282     { WM_SETFOCUS, sent|wparam|defwinproc, 0 },
9283     { WM_IME_NOTIFY, sent|wparam|defwinproc|optional, 10 },
9284     { WM_CTLCOLOREDIT, sent|parent },
9285     { WM_IME_NOTIFY, sent|wparam|defwinproc|optional, 11 },
9286     { EVENT_OBJECT_CREATE, winevent_hook|wparam|lparam, OBJID_CARET, 0 },
9287     { EVENT_OBJECT_LOCATIONCHANGE, winevent_hook|wparam|lparam, OBJID_CARET, 0 },
9288     { EVENT_OBJECT_SHOW, winevent_hook|wparam|lparam, OBJID_CARET, 0 },
9289     { WM_COMMAND, sent|parent|wparam, MAKEWPARAM(ID_EDIT, EN_SETFOCUS) },
9290     { EVENT_SYSTEM_CAPTURESTART, winevent_hook|wparam|lparam, 0, 0 },
9291     { EVENT_OBJECT_HIDE, winevent_hook|wparam|lparam, OBJID_CARET, 0 },
9292     { WM_CTLCOLOREDIT, sent|parent|optional },
9293     { WM_IME_NOTIFY, sent|wparam|defwinproc|optional, 11 },
9294     { EVENT_OBJECT_LOCATIONCHANGE, winevent_hook|wparam|lparam, OBJID_CARET, 0 },
9295     { EVENT_OBJECT_SHOW, winevent_hook|wparam|lparam, OBJID_CARET, 0 },
9296     { 0 }
9297 };
9298 static const struct message ml_edit_lbutton_down[] =
9299 {
9300     { WM_LBUTTONDOWN, sent|wparam|lparam, 0, 0 },
9301     { EVENT_SYSTEM_CAPTURESTART, winevent_hook|wparam|lparam, 0, 0 },
9302     { HCBT_SETFOCUS, hook },
9303     { WM_IME_SETCONTEXT, sent|wparam|defwinproc|optional, 1 },
9304     { WM_IME_NOTIFY, sent|wparam|defwinproc|optional, 2 },
9305     { EVENT_OBJECT_FOCUS, winevent_hook|wparam|lparam, OBJID_CLIENT, 0 },
9306     { WM_SETFOCUS, sent|wparam|defwinproc, 0 },
9307     { WM_IME_NOTIFY, sent|wparam|defwinproc|optional, 10 },
9308     { WM_IME_NOTIFY, sent|wparam|defwinproc|optional, 11 },
9309     { EVENT_OBJECT_CREATE, winevent_hook|wparam|lparam, OBJID_CARET, 0 },
9310     { EVENT_OBJECT_SHOW, winevent_hook|wparam|lparam, OBJID_CARET, 0 },
9311     { WM_COMMAND, sent|parent|wparam, MAKEWPARAM(ID_EDIT, EN_SETFOCUS) },
9312     { 0 }
9313 };
9314 static const struct message sl_edit_lbutton_up[] =
9315 {
9316     { WM_LBUTTONUP, sent|wparam|lparam, 0, 0 },
9317     { EVENT_OBJECT_HIDE, winevent_hook|wparam|lparam, OBJID_CARET, 0 },
9318     { EVENT_SYSTEM_CAPTUREEND, winevent_hook|wparam|lparam, 0, 0 },
9319     { WM_CAPTURECHANGED, sent|defwinproc },
9320     { EVENT_OBJECT_SHOW, winevent_hook|wparam|lparam, OBJID_CARET, 0 },
9321     { 0 }
9322 };
9323 static const struct message ml_edit_lbutton_up[] =
9324 {
9325     { WM_LBUTTONUP, sent|wparam|lparam, 0, 0 },
9326     { EVENT_SYSTEM_CAPTUREEND, winevent_hook|wparam|lparam, 0, 0 },
9327     { WM_CAPTURECHANGED, sent|defwinproc },
9328     { 0 }
9329 };
9330
9331 static WNDPROC old_edit_proc;
9332
9333 static LRESULT CALLBACK edit_hook_proc(HWND hwnd, UINT message, WPARAM wParam, LPARAM lParam)
9334 {
9335     static LONG defwndproc_counter = 0;
9336     LRESULT ret;
9337     struct recvd_message msg;
9338
9339     if (ignore_message( message )) return 0;
9340
9341     msg.hwnd = hwnd;
9342     msg.message = message;
9343     msg.flags = sent|wparam|lparam;
9344     if (defwndproc_counter) msg.flags |= defwinproc;
9345     msg.wParam = wParam;
9346     msg.lParam = lParam;
9347     msg.descr = "edit";
9348     add_message(&msg);
9349
9350     defwndproc_counter++;
9351     ret = CallWindowProcA(old_edit_proc, hwnd, message, wParam, lParam);
9352     defwndproc_counter--;
9353
9354     return ret;
9355 }
9356
9357 static void subclass_edit(void)
9358 {
9359     WNDCLASSA cls;
9360
9361     if (!GetClassInfoA(0, "edit", &cls)) assert(0);
9362
9363     old_edit_proc = cls.lpfnWndProc;
9364
9365     cls.hInstance = GetModuleHandle(0);
9366     cls.lpfnWndProc = edit_hook_proc;
9367     cls.lpszClassName = "my_edit_class";
9368     UnregisterClass(cls.lpszClassName, cls.hInstance);
9369     if (!RegisterClassA(&cls)) assert(0);
9370 }
9371
9372 static void test_edit_messages(void)
9373 {
9374     HWND hwnd, parent;
9375     DWORD dlg_code;
9376
9377     subclass_edit();
9378     log_all_parent_messages++;
9379
9380     parent = CreateWindowExA(0, "TestParentClass", "Test parent", WS_OVERLAPPEDWINDOW | WS_VISIBLE,
9381                              100, 100, 200, 200, 0, 0, 0, NULL);
9382     ok (parent != 0, "Failed to create parent window\n");
9383
9384     /* test single line edit */
9385     hwnd = CreateWindowExA(0, "my_edit_class", "test", WS_CHILD,
9386                            0, 0, 80, 20, parent, (HMENU)ID_EDIT, 0, NULL);
9387     ok(hwnd != 0, "Failed to create edit window\n");
9388
9389     dlg_code = SendMessageA(hwnd, WM_GETDLGCODE, 0, 0);
9390     ok(dlg_code == (DLGC_WANTCHARS|DLGC_HASSETSEL|DLGC_WANTARROWS), "wrong dlg_code %08x\n", dlg_code);
9391
9392     ShowWindow(hwnd, SW_SHOW);
9393     UpdateWindow(hwnd);
9394     SetFocus(0);
9395     flush_sequence();
9396
9397     SetFocus(hwnd);
9398     ok_sequence(sl_edit_setfocus, "SetFocus(hwnd) on an edit", FALSE);
9399
9400     SetFocus(0);
9401     ok_sequence(sl_edit_killfocus, "SetFocus(0) on an edit", FALSE);
9402
9403     SetFocus(0);
9404     ReleaseCapture();
9405     flush_sequence();
9406
9407     SendMessageA(hwnd, WM_LBUTTONDBLCLK, 0, 0);
9408     ok_sequence(sl_edit_lbutton_dblclk, "WM_LBUTTONDBLCLK on an edit", FALSE);
9409
9410     SetFocus(0);
9411     ReleaseCapture();
9412     flush_sequence();
9413
9414     SendMessageA(hwnd, WM_LBUTTONDOWN, 0, 0);
9415     ok_sequence(sl_edit_lbutton_down, "WM_LBUTTONDOWN on an edit", FALSE);
9416
9417     SendMessageA(hwnd, WM_LBUTTONUP, 0, 0);
9418     ok_sequence(sl_edit_lbutton_up, "WM_LBUTTONUP on an edit", FALSE);
9419
9420     DestroyWindow(hwnd);
9421
9422     /* test multiline edit */
9423     hwnd = CreateWindowExA(0, "my_edit_class", "test", WS_CHILD | ES_MULTILINE,
9424                            0, 0, 80, 20, parent, (HMENU)ID_EDIT, 0, NULL);
9425     ok(hwnd != 0, "Failed to create edit window\n");
9426
9427     dlg_code = SendMessageA(hwnd, WM_GETDLGCODE, 0, 0);
9428     ok(dlg_code == (DLGC_WANTCHARS|DLGC_HASSETSEL|DLGC_WANTARROWS|DLGC_WANTALLKEYS),
9429        "wrong dlg_code %08x\n", dlg_code);
9430
9431     ShowWindow(hwnd, SW_SHOW);
9432     UpdateWindow(hwnd);
9433     SetFocus(0);
9434     flush_sequence();
9435
9436     SetFocus(hwnd);
9437     ok_sequence(ml_edit_setfocus, "SetFocus(hwnd) on multiline edit", FALSE);
9438
9439     SetFocus(0);
9440     ok_sequence(sl_edit_killfocus, "SetFocus(0) on multiline edit", FALSE);
9441
9442     SetFocus(0);
9443     ReleaseCapture();
9444     flush_sequence();
9445
9446     SendMessageA(hwnd, WM_LBUTTONDBLCLK, 0, 0);
9447     ok_sequence(sl_edit_lbutton_dblclk, "WM_LBUTTONDBLCLK on multiline edit", FALSE);
9448
9449     SetFocus(0);
9450     ReleaseCapture();
9451     flush_sequence();
9452
9453     SendMessageA(hwnd, WM_LBUTTONDOWN, 0, 0);
9454     ok_sequence(ml_edit_lbutton_down, "WM_LBUTTONDOWN on multiline edit", FALSE);
9455
9456     SendMessageA(hwnd, WM_LBUTTONUP, 0, 0);
9457     ok_sequence(ml_edit_lbutton_up, "WM_LBUTTONUP on multiline edit", FALSE);
9458
9459     DestroyWindow(hwnd);
9460     DestroyWindow(parent);
9461
9462     log_all_parent_messages--;
9463 }
9464
9465 /**************************** End of Edit test ******************************/
9466
9467 static const struct message WmKeyDownSkippedSeq[] =
9468 {
9469     { HCBT_KEYSKIPPED, hook|wparam|lparam|optional, 'N', 1 }, /* XP */
9470     { 0 }
9471 };
9472 static const struct message WmKeyDownWasDownSkippedSeq[] =
9473 {
9474     { HCBT_KEYSKIPPED, hook|wparam|lparam|optional, 'N', 0x40000001 }, /* XP */
9475     { 0 }
9476 };
9477 static const struct message WmKeyUpSkippedSeq[] =
9478 {
9479     { HCBT_KEYSKIPPED, hook|wparam|lparam|optional, 'N', 0xc0000001 }, /* XP */
9480     { 0 }
9481 };
9482 static const struct message WmUserKeyUpSkippedSeq[] =
9483 {
9484     { WM_USER, sent },
9485     { HCBT_KEYSKIPPED, hook|wparam|lparam|optional, 'N', 0xc0000001 }, /* XP */
9486     { 0 }
9487 };
9488
9489 #define EV_STOP 0
9490 #define EV_SENDMSG 1
9491 #define EV_ACK 2
9492
9493 struct peekmsg_info
9494 {
9495     HWND  hwnd;
9496     HANDLE hevent[3]; /* 0 - start/stop, 1 - SendMessage, 2 - ack */
9497 };
9498
9499 static DWORD CALLBACK send_msg_thread_2(void *param)
9500 {
9501     DWORD ret;
9502     struct peekmsg_info *info = param;
9503
9504     trace("thread: looping\n");
9505     SetEvent(info->hevent[EV_ACK]);
9506
9507     while (1)
9508     {
9509         ret = WaitForMultipleObjects(2, info->hevent, FALSE, INFINITE);
9510
9511         switch (ret)
9512         {
9513         case WAIT_OBJECT_0 + EV_STOP:
9514             trace("thread: exiting\n");
9515             return 0;
9516
9517         case WAIT_OBJECT_0 + EV_SENDMSG:
9518             trace("thread: sending message\n");
9519             ret = SendNotifyMessageA(info->hwnd, WM_USER, 0, 0);
9520             ok(ret, "SendNotifyMessageA failed error %u\n", GetLastError());
9521             SetEvent(info->hevent[EV_ACK]);
9522             break;
9523
9524         default:
9525             trace("unexpected return: %04x\n", ret);
9526             assert(0);
9527             break;
9528         }
9529     }
9530     return 0;
9531 }
9532
9533 static void test_PeekMessage(void)
9534 {
9535     MSG msg;
9536     HANDLE hthread;
9537     DWORD tid, qstatus;
9538     UINT qs_all_input = QS_ALLINPUT;
9539     UINT qs_input = QS_INPUT;
9540     BOOL ret;
9541     struct peekmsg_info info;
9542
9543     info.hwnd = CreateWindowA("TestWindowClass", NULL, WS_OVERLAPPEDWINDOW,
9544                               100, 100, 200, 200, 0, 0, 0, NULL);
9545     assert(info.hwnd);
9546     ShowWindow(info.hwnd, SW_SHOW);
9547     UpdateWindow(info.hwnd);
9548     SetFocus(info.hwnd);
9549
9550     info.hevent[EV_STOP] = CreateEventA(NULL, 0, 0, NULL);
9551     info.hevent[EV_SENDMSG] = CreateEventA(NULL, 0, 0, NULL);
9552     info.hevent[EV_ACK] = CreateEventA(NULL, 0, 0, NULL);
9553
9554     hthread = CreateThread(NULL, 0, send_msg_thread_2, &info, 0, &tid);
9555     WaitForSingleObject(info.hevent[EV_ACK], 10000);
9556
9557     flush_events();
9558     flush_sequence();
9559
9560     SetLastError(0xdeadbeef);
9561     qstatus = GetQueueStatus(qs_all_input);
9562     if (GetLastError() == ERROR_INVALID_FLAGS)
9563     {
9564         trace("QS_RAWINPUT not supported on this platform\n");
9565         qs_all_input &= ~QS_RAWINPUT;
9566         qs_input &= ~QS_RAWINPUT;
9567     }
9568     if (qstatus & QS_POSTMESSAGE)
9569     {
9570         while (PeekMessage(&msg, 0, 0, 0, PM_REMOVE)) /* nothing */ ;
9571         qstatus = GetQueueStatus(qs_all_input);
9572     }
9573     ok(qstatus == 0, "wrong qstatus %08x\n", qstatus);
9574
9575     trace("signalling to send message\n");
9576     SetEvent(info.hevent[EV_SENDMSG]);
9577     WaitForSingleObject(info.hevent[EV_ACK], INFINITE);
9578
9579     /* pass invalid QS_xxxx flags */
9580     SetLastError(0xdeadbeef);
9581     qstatus = GetQueueStatus(0xffffffff);
9582     ok(qstatus == 0 || broken(qstatus)  /* win9x */, "GetQueueStatus should fail: %08x\n", qstatus);
9583     if (!qstatus)
9584     {
9585         ok(GetLastError() == ERROR_INVALID_FLAGS, "wrong error %d\n", GetLastError());
9586         qstatus = GetQueueStatus(qs_all_input);
9587     }
9588     qstatus &= ~MAKELONG( 0x4000, 0x4000 );  /* sometimes set on Win95 */
9589     ok(qstatus == MAKELONG(QS_SENDMESSAGE, QS_SENDMESSAGE),
9590        "wrong qstatus %08x\n", qstatus);
9591
9592     msg.message = 0;
9593     ret = PeekMessageA(&msg, 0, 0, 0, PM_REMOVE);
9594     ok(!ret,
9595        "PeekMessageA should have returned FALSE instead of msg %04x\n",
9596         msg.message);
9597     ok_sequence(WmUser, "WmUser", FALSE);
9598
9599     qstatus = GetQueueStatus(qs_all_input);
9600     ok(qstatus == 0, "wrong qstatus %08x\n", qstatus);
9601
9602     keybd_event('N', 0, 0, 0);
9603     keybd_event('N', 0, KEYEVENTF_KEYUP, 0);
9604     qstatus = GetQueueStatus(qs_all_input);
9605     if (!(qstatus & MAKELONG(QS_KEY, QS_KEY)))
9606     {
9607         skip( "queuing key events not supported\n" );
9608         goto done;
9609     }
9610     ok(qstatus == MAKELONG(QS_KEY, QS_KEY) ||
9611        /* keybd_event seems to trigger a sent message on NT4 */
9612        qstatus == MAKELONG(QS_KEY|QS_SENDMESSAGE, QS_KEY|QS_SENDMESSAGE),
9613        "wrong qstatus %08x\n", qstatus);
9614
9615     PostMessageA(info.hwnd, WM_CHAR, 'z', 0);
9616     qstatus = GetQueueStatus(qs_all_input);
9617     ok(qstatus == MAKELONG(QS_POSTMESSAGE, QS_POSTMESSAGE|QS_KEY) ||
9618        qstatus == MAKELONG(QS_POSTMESSAGE, QS_POSTMESSAGE|QS_KEY|QS_SENDMESSAGE),
9619        "wrong qstatus %08x\n", qstatus);
9620
9621     InvalidateRect(info.hwnd, NULL, FALSE);
9622     qstatus = GetQueueStatus(qs_all_input);
9623     ok(qstatus == MAKELONG(QS_PAINT, QS_PAINT|QS_POSTMESSAGE|QS_KEY) ||
9624        qstatus == MAKELONG(QS_PAINT, QS_PAINT|QS_POSTMESSAGE|QS_KEY|QS_SENDMESSAGE),
9625        "wrong qstatus %08x\n", qstatus);
9626
9627     trace("signalling to send message\n");
9628     SetEvent(info.hevent[EV_SENDMSG]);
9629     WaitForSingleObject(info.hevent[EV_ACK], INFINITE);
9630
9631     qstatus = GetQueueStatus(qs_all_input);
9632     ok(qstatus == MAKELONG(QS_SENDMESSAGE, QS_SENDMESSAGE|QS_PAINT|QS_POSTMESSAGE|QS_KEY),
9633        "wrong qstatus %08x\n", qstatus);
9634
9635     msg.message = 0;
9636     ret = PeekMessageA(&msg, 0, 0, 0, PM_REMOVE | (qs_input << 16));
9637     if (ret && msg.message == WM_CHAR)
9638     {
9639         win_skip( "PM_QS_* flags not supported in PeekMessage\n" );
9640         goto done;
9641     }
9642     ok(!ret,
9643        "PeekMessageA should have returned FALSE instead of msg %04x\n",
9644         msg.message);
9645     if (!sequence_cnt)  /* nt4 doesn't fetch anything with PM_QS_* flags */
9646     {
9647         win_skip( "PM_QS_* flags not supported in PeekMessage\n" );
9648         goto done;
9649     }
9650     ok_sequence(WmUser, "WmUser", FALSE);
9651
9652     qstatus = GetQueueStatus(qs_all_input);
9653     ok(qstatus == MAKELONG(0, QS_PAINT|QS_POSTMESSAGE|QS_KEY),
9654        "wrong qstatus %08x\n", qstatus);
9655
9656     trace("signalling to send message\n");
9657     SetEvent(info.hevent[EV_SENDMSG]);
9658     WaitForSingleObject(info.hevent[EV_ACK], INFINITE);
9659
9660     qstatus = GetQueueStatus(qs_all_input);
9661     ok(qstatus == MAKELONG(QS_SENDMESSAGE, QS_SENDMESSAGE|QS_PAINT|QS_POSTMESSAGE|QS_KEY),
9662        "wrong qstatus %08x\n", qstatus);
9663
9664     msg.message = 0;
9665     ret = PeekMessageA(&msg, 0, 0, 0, PM_REMOVE | PM_QS_POSTMESSAGE );
9666     ok(!ret,
9667        "PeekMessageA should have returned FALSE instead of msg %04x\n",
9668         msg.message);
9669     ok_sequence(WmUser, "WmUser", FALSE);
9670
9671     qstatus = GetQueueStatus(qs_all_input);
9672     ok(qstatus == MAKELONG(0, QS_PAINT|QS_POSTMESSAGE|QS_KEY),
9673        "wrong qstatus %08x\n", qstatus);
9674
9675     msg.message = 0;
9676     ret = PeekMessageA(&msg, 0, 0, 0, PM_REMOVE | PM_QS_POSTMESSAGE);
9677     ok(ret && msg.message == WM_CHAR && msg.wParam == 'z',
9678        "got %d and %04x wParam %08lx instead of TRUE and WM_CHAR wParam 'z'\n",
9679        ret, msg.message, msg.wParam);
9680     ok_sequence(WmEmptySeq, "WmEmptySeq", FALSE);
9681
9682     qstatus = GetQueueStatus(qs_all_input);
9683     ok(qstatus == MAKELONG(0, QS_PAINT|QS_KEY),
9684        "wrong qstatus %08x\n", qstatus);
9685
9686     msg.message = 0;
9687     ret = PeekMessageA(&msg, 0, 0, 0, PM_REMOVE | PM_QS_POSTMESSAGE);
9688     ok(!ret,
9689        "PeekMessageA should have returned FALSE instead of msg %04x\n",
9690         msg.message);
9691     ok_sequence(WmEmptySeq, "WmEmptySeq", FALSE);
9692
9693     qstatus = GetQueueStatus(qs_all_input);
9694     ok(qstatus == MAKELONG(0, QS_PAINT|QS_KEY),
9695        "wrong qstatus %08x\n", qstatus);
9696
9697     msg.message = 0;
9698     ret = PeekMessageA(&msg, 0, 0, 0, PM_REMOVE | PM_QS_PAINT);
9699     ok(ret && msg.message == WM_PAINT,
9700        "got %d and %04x instead of TRUE and WM_PAINT\n", ret, msg.message);
9701     DispatchMessageA(&msg);
9702     ok_sequence(WmPaint, "WmPaint", FALSE);
9703
9704     qstatus = GetQueueStatus(qs_all_input);
9705     ok(qstatus == MAKELONG(0, QS_KEY),
9706        "wrong qstatus %08x\n", qstatus);
9707
9708     msg.message = 0;
9709     ret = PeekMessageA(&msg, 0, 0, 0, PM_REMOVE | PM_QS_PAINT);
9710     ok(!ret,
9711        "PeekMessageA should have returned FALSE instead of msg %04x\n",
9712         msg.message);
9713     ok_sequence(WmEmptySeq, "WmEmptySeq", FALSE);
9714
9715     qstatus = GetQueueStatus(qs_all_input);
9716     ok(qstatus == MAKELONG(0, QS_KEY),
9717        "wrong qstatus %08x\n", qstatus);
9718
9719     trace("signalling to send message\n");
9720     SetEvent(info.hevent[EV_SENDMSG]);
9721     WaitForSingleObject(info.hevent[EV_ACK], INFINITE);
9722
9723     qstatus = GetQueueStatus(qs_all_input);
9724     ok(qstatus == MAKELONG(QS_SENDMESSAGE, QS_SENDMESSAGE|QS_KEY),
9725        "wrong qstatus %08x\n", qstatus);
9726
9727     PostMessageA(info.hwnd, WM_CHAR, 'z', 0);
9728
9729     qstatus = GetQueueStatus(qs_all_input);
9730     ok(qstatus == MAKELONG(QS_POSTMESSAGE, QS_SENDMESSAGE|QS_POSTMESSAGE|QS_KEY),
9731        "wrong qstatus %08x\n", qstatus);
9732
9733     msg.message = 0;
9734     ret = PeekMessageA(&msg, 0, WM_CHAR, WM_CHAR, PM_REMOVE);
9735     ok(ret && msg.message == WM_CHAR && msg.wParam == 'z',
9736        "got %d and %04x wParam %08lx instead of TRUE and WM_CHAR wParam 'z'\n",
9737        ret, msg.message, msg.wParam);
9738     ok_sequence(WmUser, "WmUser", FALSE);
9739
9740     qstatus = GetQueueStatus(qs_all_input);
9741     ok(qstatus == MAKELONG(0, QS_KEY),
9742        "wrong qstatus %08x\n", qstatus);
9743
9744     msg.message = 0;
9745     ret = PeekMessageA(&msg, 0, WM_CHAR, WM_CHAR, PM_REMOVE);
9746     ok(!ret,
9747        "PeekMessageA should have returned FALSE instead of msg %04x\n",
9748         msg.message);
9749     ok_sequence(WmEmptySeq, "WmEmptySeq", FALSE);
9750
9751     qstatus = GetQueueStatus(qs_all_input);
9752     ok(qstatus == MAKELONG(0, QS_KEY),
9753        "wrong qstatus %08x\n", qstatus);
9754
9755     PostMessageA(info.hwnd, WM_CHAR, 'z', 0);
9756
9757     qstatus = GetQueueStatus(qs_all_input);
9758     ok(qstatus == MAKELONG(QS_POSTMESSAGE, QS_POSTMESSAGE|QS_KEY),
9759        "wrong qstatus %08x\n", qstatus);
9760
9761     trace("signalling to send message\n");
9762     SetEvent(info.hevent[EV_SENDMSG]);
9763     WaitForSingleObject(info.hevent[EV_ACK], INFINITE);
9764
9765     qstatus = GetQueueStatus(qs_all_input);
9766     ok(qstatus == MAKELONG(QS_SENDMESSAGE, QS_SENDMESSAGE|QS_POSTMESSAGE|QS_KEY),
9767        "wrong qstatus %08x\n", qstatus);
9768
9769     msg.message = 0;
9770     ret = PeekMessageA(&msg, 0, 0, 0, PM_REMOVE | (QS_KEY << 16));
9771     ok(!ret,
9772        "PeekMessageA should have returned FALSE instead of msg %04x\n",
9773         msg.message);
9774     ok_sequence(WmUser, "WmUser", FALSE);
9775
9776     qstatus = GetQueueStatus(qs_all_input);
9777     ok(qstatus == MAKELONG(0, QS_POSTMESSAGE|QS_KEY),
9778        "wrong qstatus %08x\n", qstatus);
9779
9780     msg.message = 0;
9781     if (qs_all_input & QS_RAWINPUT) /* use QS_RAWINPUT only if supported */
9782         ret = PeekMessageA(&msg, 0, 0, 0, PM_REMOVE | (QS_RAWINPUT << 16));
9783     else /* workaround for a missing QS_RAWINPUT support */
9784         ret = PeekMessageA(&msg, 0, WM_KEYDOWN, WM_KEYDOWN, PM_REMOVE);
9785     ok(ret && msg.message == WM_KEYDOWN && msg.wParam == 'N',
9786        "got %d and %04x wParam %08lx instead of TRUE and WM_KEYDOWN wParam 'N'\n",
9787        ret, msg.message, msg.wParam);
9788     ok_sequence(WmKeyDownSkippedSeq, "WmKeyDownSkippedSeq", FALSE);
9789
9790     qstatus = GetQueueStatus(qs_all_input);
9791     ok(qstatus == MAKELONG(0, QS_POSTMESSAGE|QS_KEY),
9792        "wrong qstatus %08x\n", qstatus);
9793
9794     msg.message = 0;
9795     if (qs_all_input & QS_RAWINPUT) /* use QS_RAWINPUT only if supported */
9796         ret = PeekMessageA(&msg, 0, 0, 0, PM_REMOVE | (QS_RAWINPUT << 16));
9797     else /* workaround for a missing QS_RAWINPUT support */
9798         ret = PeekMessageA(&msg, 0, WM_KEYUP, WM_KEYUP, PM_REMOVE);
9799     ok(ret && msg.message == WM_KEYUP && msg.wParam == 'N',
9800        "got %d and %04x wParam %08lx instead of TRUE and WM_KEYUP wParam 'N'\n",
9801        ret, msg.message, msg.wParam);
9802     ok_sequence(WmKeyUpSkippedSeq, "WmKeyUpSkippedSeq", FALSE);
9803
9804     qstatus = GetQueueStatus(qs_all_input);
9805     ok(qstatus == MAKELONG(0, QS_POSTMESSAGE),
9806        "wrong qstatus %08x\n", qstatus);
9807
9808     msg.message = 0;
9809     ret = PeekMessageA(&msg, 0, 0, 0, PM_REMOVE | PM_QS_SENDMESSAGE);
9810     ok(!ret,
9811        "PeekMessageA should have returned FALSE instead of msg %04x\n",
9812         msg.message);
9813     ok_sequence(WmEmptySeq, "WmEmptySeq", FALSE);
9814
9815     qstatus = GetQueueStatus(qs_all_input);
9816     ok(qstatus == MAKELONG(0, QS_POSTMESSAGE),
9817        "wrong qstatus %08x\n", qstatus);
9818
9819     msg.message = 0;
9820     ret = PeekMessageA(&msg, 0, 0, 0, PM_REMOVE);
9821     ok(ret && msg.message == WM_CHAR && msg.wParam == 'z',
9822        "got %d and %04x wParam %08lx instead of TRUE and WM_CHAR wParam 'z'\n",
9823        ret, msg.message, msg.wParam);
9824     ok_sequence(WmEmptySeq, "WmEmptySeq", FALSE);
9825
9826     qstatus = GetQueueStatus(qs_all_input);
9827     ok(qstatus == 0,
9828        "wrong qstatus %08x\n", qstatus);
9829
9830     msg.message = 0;
9831     ret = PeekMessageA(&msg, 0, 0, 0, PM_REMOVE);
9832     ok(!ret,
9833        "PeekMessageA should have returned FALSE instead of msg %04x\n",
9834         msg.message);
9835     ok_sequence(WmEmptySeq, "WmEmptySeq", FALSE);
9836
9837     qstatus = GetQueueStatus(qs_all_input);
9838     ok(qstatus == 0,
9839        "wrong qstatus %08x\n", qstatus);
9840
9841     /* test whether presence of the quit flag in the queue affects
9842      * the queue state
9843      */
9844     PostQuitMessage(0x1234abcd);
9845
9846     qstatus = GetQueueStatus(qs_all_input);
9847     ok(qstatus == MAKELONG(QS_POSTMESSAGE, QS_POSTMESSAGE),
9848        "wrong qstatus %08x\n", qstatus);
9849
9850     PostMessageA(info.hwnd, WM_USER, 0, 0);
9851
9852     qstatus = GetQueueStatus(qs_all_input);
9853     ok(qstatus == MAKELONG(QS_POSTMESSAGE, QS_POSTMESSAGE),
9854        "wrong qstatus %08x\n", qstatus);
9855
9856     msg.message = 0;
9857     ret = PeekMessageA(&msg, 0, 0, 0, PM_REMOVE);
9858     ok(ret && msg.message == WM_USER,
9859        "got %d and %04x instead of TRUE and WM_USER\n", ret, msg.message);
9860     ok_sequence(WmEmptySeq, "WmEmptySeq", FALSE);
9861
9862     qstatus = GetQueueStatus(qs_all_input);
9863     ok(qstatus == MAKELONG(0, QS_POSTMESSAGE),
9864        "wrong qstatus %08x\n", qstatus);
9865
9866     msg.message = 0;
9867     ret = PeekMessageA(&msg, 0, 0, 0, PM_REMOVE);
9868     ok(ret && msg.message == WM_QUIT,
9869        "got %d and %04x instead of TRUE and WM_QUIT\n", ret, msg.message);
9870     ok(msg.wParam == 0x1234abcd, "got wParam %08lx instead of 0x1234abcd\n", msg.wParam);
9871     ok(msg.lParam == 0, "got lParam %08lx instead of 0\n", msg.lParam);
9872     ok_sequence(WmEmptySeq, "WmEmptySeq", FALSE);
9873
9874     qstatus = GetQueueStatus(qs_all_input);
9875 todo_wine {
9876     ok(qstatus == MAKELONG(0, QS_POSTMESSAGE),
9877        "wrong qstatus %08x\n", qstatus);
9878 }
9879
9880     msg.message = 0;
9881     ret = PeekMessageA(&msg, 0, 0, 0, PM_REMOVE);
9882     ok(!ret,
9883        "PeekMessageA should have returned FALSE instead of msg %04x\n",
9884         msg.message);
9885     ok_sequence(WmEmptySeq, "WmEmptySeq", FALSE);
9886
9887     qstatus = GetQueueStatus(qs_all_input);
9888     ok(qstatus == 0,
9889        "wrong qstatus %08x\n", qstatus);
9890
9891     /* some GetMessage tests */
9892
9893     keybd_event('N', 0, 0, 0);
9894     qstatus = GetQueueStatus(qs_all_input);
9895     ok(qstatus == MAKELONG(QS_KEY, QS_KEY), "wrong qstatus %08x\n", qstatus);
9896
9897     PostMessageA(info.hwnd, WM_CHAR, 'z', 0);
9898     qstatus = GetQueueStatus(qs_all_input);
9899     ok(qstatus == MAKELONG(QS_POSTMESSAGE, QS_POSTMESSAGE|QS_KEY), "wrong qstatus %08x\n", qstatus);
9900
9901     if (qstatus)
9902     {
9903         ret = GetMessageA( &msg, 0, 0, 0 );
9904         ok(ret && msg.message == WM_CHAR && msg.wParam == 'z',
9905            "got %d and %04x wParam %08lx instead of TRUE and WM_CHAR wParam 'z'\n",
9906            ret, msg.message, msg.wParam);
9907         qstatus = GetQueueStatus(qs_all_input);
9908         ok(qstatus == MAKELONG(0, QS_KEY), "wrong qstatus %08x\n", qstatus);
9909     }
9910
9911     if (qstatus)
9912     {
9913         ret = GetMessageA( &msg, 0, 0, 0 );
9914         ok(ret && msg.message == WM_KEYDOWN && msg.wParam == 'N',
9915            "got %d and %04x wParam %08lx instead of TRUE and WM_KEYDOWN wParam 'N'\n",
9916            ret, msg.message, msg.wParam);
9917         ok_sequence(WmKeyDownSkippedSeq, "WmKeyDownSkippedSeq", FALSE);
9918         qstatus = GetQueueStatus(qs_all_input);
9919         ok(qstatus == 0, "wrong qstatus %08x\n", qstatus);
9920     }
9921
9922     keybd_event('N', 0, 0, 0);
9923     qstatus = GetQueueStatus(qs_all_input);
9924     ok(qstatus == MAKELONG(QS_KEY, QS_KEY), "wrong qstatus %08x\n", qstatus);
9925
9926     PostMessageA(info.hwnd, WM_CHAR, 'z', 0);
9927     qstatus = GetQueueStatus(qs_all_input);
9928     ok(qstatus == MAKELONG(QS_POSTMESSAGE, QS_POSTMESSAGE|QS_KEY), "wrong qstatus %08x\n", qstatus);
9929
9930     if (qstatus & (QS_KEY << 16))
9931     {
9932         ret = GetMessageA( &msg, 0, WM_KEYDOWN, WM_KEYUP );
9933         ok(ret && msg.message == WM_KEYDOWN && msg.wParam == 'N',
9934            "got %d and %04x wParam %08lx instead of TRUE and WM_KEYDOWN wParam 'N'\n",
9935            ret, msg.message, msg.wParam);
9936         ok_sequence(WmKeyDownWasDownSkippedSeq, "WmKeyDownWasDownSkippedSeq", FALSE);
9937         qstatus = GetQueueStatus(qs_all_input);
9938         ok(qstatus == MAKELONG(0, QS_POSTMESSAGE), "wrong qstatus %08x\n", qstatus);
9939     }
9940
9941     if (qstatus)
9942     {
9943         ret = GetMessageA( &msg, 0, WM_CHAR, WM_CHAR );
9944         ok(ret && msg.message == WM_CHAR && msg.wParam == 'z',
9945            "got %d and %04x wParam %08lx instead of TRUE and WM_CHAR wParam 'z'\n",
9946            ret, msg.message, msg.wParam);
9947         qstatus = GetQueueStatus(qs_all_input);
9948         ok(qstatus == 0, "wrong qstatus %08x\n", qstatus);
9949     }
9950
9951     keybd_event('N', 0, KEYEVENTF_KEYUP, 0);
9952     qstatus = GetQueueStatus(qs_all_input);
9953     ok(qstatus == MAKELONG(QS_KEY, QS_KEY), "wrong qstatus %08x\n", qstatus);
9954
9955     PostMessageA(info.hwnd, WM_CHAR, 'z', 0);
9956     qstatus = GetQueueStatus(qs_all_input);
9957     ok(qstatus == MAKELONG(QS_POSTMESSAGE, QS_POSTMESSAGE|QS_KEY), "wrong qstatus %08x\n", qstatus);
9958
9959     trace("signalling to send message\n");
9960     SetEvent(info.hevent[EV_SENDMSG]);
9961     WaitForSingleObject(info.hevent[EV_ACK], INFINITE);
9962     qstatus = GetQueueStatus(qs_all_input);
9963     ok(qstatus == MAKELONG(QS_SENDMESSAGE, QS_SENDMESSAGE|QS_POSTMESSAGE|QS_KEY),
9964        "wrong qstatus %08x\n", qstatus);
9965
9966     if (qstatus & (QS_KEY << 16))
9967     {
9968         ret = GetMessageA( &msg, 0, WM_KEYDOWN, WM_KEYUP );
9969         ok(ret && msg.message == WM_KEYUP && msg.wParam == 'N',
9970            "got %d and %04x wParam %08lx instead of TRUE and WM_KEYDOWN wParam 'N'\n",
9971            ret, msg.message, msg.wParam);
9972         ok_sequence(WmUserKeyUpSkippedSeq, "WmUserKeyUpSkippedSeq", FALSE);
9973         qstatus = GetQueueStatus(qs_all_input);
9974         ok(qstatus == MAKELONG(0, QS_POSTMESSAGE), "wrong qstatus %08x\n", qstatus);
9975     }
9976
9977     if (qstatus)
9978     {
9979         ret = GetMessageA( &msg, 0, WM_CHAR, WM_CHAR );
9980         ok(ret && msg.message == WM_CHAR && msg.wParam == 'z',
9981            "got %d and %04x wParam %08lx instead of TRUE and WM_CHAR wParam 'z'\n",
9982            ret, msg.message, msg.wParam);
9983         qstatus = GetQueueStatus(qs_all_input);
9984         ok(qstatus == 0, "wrong qstatus %08x\n", qstatus);
9985     }
9986 done:
9987     trace("signalling to exit\n");
9988     SetEvent(info.hevent[EV_STOP]);
9989
9990     WaitForSingleObject(hthread, INFINITE);
9991
9992     CloseHandle(hthread);
9993     CloseHandle(info.hevent[0]);
9994     CloseHandle(info.hevent[1]);
9995     CloseHandle(info.hevent[2]);
9996
9997     DestroyWindow(info.hwnd);
9998 }
9999
10000 static void wait_move_event(HWND hwnd, int x, int y)
10001 {
10002     MSG msg;
10003     DWORD time;
10004     BOOL  ret;
10005     int go = 0;
10006
10007     time = GetTickCount();
10008     while (GetTickCount() - time < 200 && !go) {
10009         ret = PeekMessageA(&msg, hwnd, WM_MOUSEMOVE, WM_MOUSEMOVE, PM_NOREMOVE);
10010         go  = ret && msg.pt.x > x && msg.pt.y > y;
10011         if (!ret) MsgWaitForMultipleObjects( 0, NULL, FALSE, GetTickCount() - time, QS_ALLINPUT );
10012     }
10013 }
10014
10015 #define STEP 5
10016 static void test_PeekMessage2(void)
10017 {
10018     HWND hwnd;
10019     BOOL ret;
10020     MSG msg;
10021     UINT message;
10022     DWORD time1, time2, time3;
10023     int x1, y1, x2, y2, x3, y3;
10024     POINT pos;
10025
10026     time1 = time2 = time3 = 0;
10027     x1 = y1 = x2 = y2 = x3 = y3 = 0;
10028
10029     /* Initialise window and make sure it is ready for events */
10030     hwnd = CreateWindow("TestWindowClass", "PeekMessage2", WS_OVERLAPPEDWINDOW,
10031                         10, 10, 800, 800, NULL, NULL, NULL, NULL);
10032     assert(hwnd);
10033     trace("Window for test_PeekMessage2 %p\n", hwnd);
10034     ShowWindow(hwnd, SW_SHOW);
10035     UpdateWindow(hwnd);
10036     SetFocus(hwnd);
10037     GetCursorPos(&pos);
10038     SetCursorPos(100, 100);
10039     mouse_event(MOUSEEVENTF_MOVE, -STEP, -STEP, 0, 0);
10040     flush_events();
10041
10042     /* Do initial mousemove, wait until we can see it
10043        and then do our test peek with PM_NOREMOVE. */
10044     mouse_event(MOUSEEVENTF_MOVE, STEP, STEP, 0, 0);
10045     wait_move_event(hwnd, 100-STEP, 100-STEP);
10046
10047     ret = PeekMessageA(&msg, hwnd, WM_MOUSEMOVE, WM_MOUSEMOVE, PM_NOREMOVE);
10048     if (!ret)
10049     {
10050         skip( "queuing mouse events not supported\n" );
10051         goto done;
10052     }
10053     else
10054     {
10055         trace("1st move event: %04x %x %d %d\n", msg.message, msg.time, msg.pt.x, msg.pt.y);
10056         message = msg.message;
10057         time1 = msg.time;
10058         x1 = msg.pt.x;
10059         y1 = msg.pt.y;
10060         ok(message == WM_MOUSEMOVE, "message not WM_MOUSEMOVE, %04x instead\n", message);
10061     }
10062
10063     /* Allow time to advance a bit, and then simulate the user moving their
10064      * mouse around. After that we peek again with PM_NOREMOVE.
10065      * Although the previous mousemove message was never removed, the
10066      * mousemove we now peek should reflect the recent mouse movements
10067      * because the input queue will merge the move events. */
10068     Sleep(100);
10069     mouse_event(MOUSEEVENTF_MOVE, STEP, STEP, 0, 0);
10070     wait_move_event(hwnd, x1, y1);
10071
10072     ret = PeekMessageA(&msg, hwnd, WM_MOUSEMOVE, WM_MOUSEMOVE, PM_NOREMOVE);
10073     ok(ret, "no message available\n");
10074     if (ret) {
10075         trace("2nd move event: %04x %x %d %d\n", msg.message, msg.time, msg.pt.x, msg.pt.y);
10076         message = msg.message;
10077         time2 = msg.time;
10078         x2 = msg.pt.x;
10079         y2 = msg.pt.y;
10080         ok(message == WM_MOUSEMOVE, "message not WM_MOUSEMOVE, %04x instead\n", message);
10081         ok(time2 > time1, "message time not advanced: %x %x\n", time1, time2);
10082         ok(x2 != x1 && y2 != y1, "coords not changed: (%d %d) (%d %d)\n", x1, y1, x2, y2);
10083     }
10084
10085     /* Have another go, to drive the point home */
10086     Sleep(100);
10087     mouse_event(MOUSEEVENTF_MOVE, STEP, STEP, 0, 0);
10088     wait_move_event(hwnd, x2, y2);
10089
10090     ret = PeekMessageA(&msg, hwnd, WM_MOUSEMOVE, WM_MOUSEMOVE, PM_NOREMOVE);
10091     ok(ret, "no message available\n");
10092     if (ret) {
10093         trace("3rd move event: %04x %x %d %d\n", msg.message, msg.time, msg.pt.x, msg.pt.y);
10094         message = msg.message;
10095         time3 = msg.time;
10096         x3 = msg.pt.x;
10097         y3 = msg.pt.y;
10098         ok(message == WM_MOUSEMOVE, "message not WM_MOUSEMOVE, %04x instead\n", message);
10099         ok(time3 > time2, "message time not advanced: %x %x\n", time2, time3);
10100         ok(x3 != x2 && y3 != y2, "coords not changed: (%d %d) (%d %d)\n", x2, y2, x3, y3);
10101     }
10102
10103 done:
10104     DestroyWindow(hwnd);
10105     SetCursorPos(pos.x, pos.y);
10106     flush_events();
10107 }
10108
10109 static INT_PTR CALLBACK wm_quit_dlg_proc(HWND hwnd, UINT message, WPARAM wp, LPARAM lp)
10110 {
10111     struct recvd_message msg;
10112
10113     if (ignore_message( message )) return 0;
10114
10115     msg.hwnd = hwnd;
10116     msg.message = message;
10117     msg.flags = sent|wparam|lparam;
10118     msg.wParam = wp;
10119     msg.lParam = lp;
10120     msg.descr = "dialog";
10121     add_message(&msg);
10122
10123     switch (message)
10124     {
10125     case WM_INITDIALOG:
10126         PostMessage(hwnd, WM_QUIT, 0x1234, 0x5678);
10127         PostMessage(hwnd, WM_USER, 0xdead, 0xbeef);
10128         return 0;
10129
10130     case WM_GETDLGCODE:
10131         return 0;
10132
10133     case WM_USER:
10134         EndDialog(hwnd, 0);
10135         break;
10136     }
10137
10138     return 1;
10139 }
10140
10141 static const struct message WmQuitDialogSeq[] = {
10142     { HCBT_CREATEWND, hook },
10143     { WM_SETFONT, sent },
10144     { WM_INITDIALOG, sent },
10145     { WM_CHANGEUISTATE, sent|optional },
10146     { HCBT_DESTROYWND, hook },
10147     { 0x0090, sent|optional }, /* Vista */
10148     { WM_DESTROY, sent },
10149     { WM_NCDESTROY, sent },
10150     { 0 }
10151 };
10152
10153 static const struct message WmStopQuitSeq[] = {
10154     { WM_DWMNCRENDERINGCHANGED, posted|optional },
10155     { WM_CLOSE, posted },
10156     { WM_QUIT, posted|wparam|lparam, 0x1234, 0 },
10157     { 0 }
10158 };
10159
10160 static void test_quit_message(void)
10161 {
10162     MSG msg;
10163     BOOL ret;
10164
10165     /* test using PostQuitMessage */
10166     flush_events();
10167     PostQuitMessage(0xbeef);
10168
10169     ret = PeekMessage(&msg, NULL, 0, 0, PM_NOREMOVE);
10170     ok(ret, "PeekMessage failed with error %d\n", GetLastError());
10171     ok(msg.message == WM_QUIT, "Received message 0x%04x instead of WM_QUIT\n", msg.message);
10172     ok(msg.wParam == 0xbeef, "wParam was 0x%lx instead of 0xbeef\n", msg.wParam);
10173
10174     ret = PostThreadMessage(GetCurrentThreadId(), WM_USER, 0, 0);
10175     ok(ret, "PostMessage failed with error %d\n", GetLastError());
10176
10177     ret = GetMessage(&msg, NULL, 0, 0);
10178     ok(ret > 0, "GetMessage failed with error %d\n", GetLastError());
10179     ok(msg.message == WM_USER, "Received message 0x%04x instead of WM_USER\n", msg.message);
10180
10181     /* note: WM_QUIT message received after WM_USER message */
10182     ret = GetMessage(&msg, NULL, 0, 0);
10183     ok(!ret, "GetMessage return %d with error %d instead of FALSE\n", ret, GetLastError());
10184     ok(msg.message == WM_QUIT, "Received message 0x%04x instead of WM_QUIT\n", msg.message);
10185     ok(msg.wParam == 0xbeef, "wParam was 0x%lx instead of 0xbeef\n", msg.wParam);
10186
10187     ret = PeekMessage(&msg, NULL, 0, 0, PM_REMOVE);
10188     ok( !ret || msg.message != WM_QUIT, "Received WM_QUIT again\n" );
10189
10190     /* now test with PostThreadMessage - different behaviour! */
10191     PostThreadMessage(GetCurrentThreadId(), WM_QUIT, 0xdead, 0);
10192
10193     ret = PeekMessage(&msg, NULL, 0, 0, PM_NOREMOVE);
10194     ok(ret, "PeekMessage failed with error %d\n", GetLastError());
10195     ok(msg.message == WM_QUIT, "Received message 0x%04x instead of WM_QUIT\n", msg.message);
10196     ok(msg.wParam == 0xdead, "wParam was 0x%lx instead of 0xdead\n", msg.wParam);
10197
10198     ret = PostThreadMessage(GetCurrentThreadId(), WM_USER, 0, 0);
10199     ok(ret, "PostMessage failed with error %d\n", GetLastError());
10200
10201     /* note: we receive the WM_QUIT message first this time */
10202     ret = GetMessage(&msg, NULL, 0, 0);
10203     ok(!ret, "GetMessage return %d with error %d instead of FALSE\n", ret, GetLastError());
10204     ok(msg.message == WM_QUIT, "Received message 0x%04x instead of WM_QUIT\n", msg.message);
10205     ok(msg.wParam == 0xdead, "wParam was 0x%lx instead of 0xdead\n", msg.wParam);
10206
10207     ret = GetMessage(&msg, NULL, 0, 0);
10208     ok(ret > 0, "GetMessage failed with error %d\n", GetLastError());
10209     ok(msg.message == WM_USER, "Received message 0x%04x instead of WM_USER\n", msg.message);
10210
10211     flush_events();
10212     flush_sequence();
10213     ret = DialogBoxParam(GetModuleHandle(0), "TEST_EMPTY_DIALOG", 0, wm_quit_dlg_proc, 0);
10214     ok(ret == 1, "expected 1, got %d\n", ret);
10215     ok_sequence(WmQuitDialogSeq, "WmQuitDialogSeq", FALSE);
10216     memset(&msg, 0xab, sizeof(msg));
10217     ret = PeekMessage(&msg, NULL, 0, 0, PM_REMOVE);
10218     ok(ret, "PeekMessage failed\n");
10219     ok(msg.message == WM_QUIT, "Received message 0x%04x instead of WM_QUIT\n", msg.message);
10220     ok(msg.wParam == 0x1234, "wParam was 0x%lx instead of 0x1234\n", msg.wParam);
10221     ok(msg.lParam == 0, "lParam was 0x%lx instead of 0\n", msg.lParam);
10222
10223     /* Check what happens to a WM_QUIT message posted to a window that gets
10224      * destroyed.
10225      */
10226     CreateWindowExA(0, "StopQuitClass", "Stop Quit Test", WS_OVERLAPPEDWINDOW,
10227                     0, 0, 100, 100, NULL, NULL, NULL, NULL);
10228     flush_sequence();
10229     while (PeekMessage(&msg, 0, 0, 0, PM_REMOVE))
10230     {
10231         struct recvd_message rmsg;
10232         rmsg.hwnd = msg.hwnd;
10233         rmsg.message = msg.message;
10234         rmsg.flags = posted|wparam|lparam;
10235         rmsg.wParam = msg.wParam;
10236         rmsg.lParam = msg.lParam;
10237         rmsg.descr = "stop/quit";
10238         if (msg.message == WM_QUIT)
10239             /* The hwnd can only be checked here */
10240             ok(!msg.hwnd, "The WM_QUIT hwnd was %p instead of NULL\n", msg.hwnd);
10241         add_message(&rmsg);
10242         DispatchMessage(&msg);
10243     }
10244     ok_sequence(WmStopQuitSeq, "WmStopQuitSeq", FALSE);
10245 }
10246
10247 static const struct message WmMouseHoverSeq[] = {
10248     { WM_MOUSEACTIVATE, sent|optional },  /* we can get those when moving the mouse in focus-follow-mouse mode under X11 */
10249     { WM_MOUSEACTIVATE, sent|optional },
10250     { WM_TIMER, sent|optional }, /* XP sends it */
10251     { WM_SYSTIMER, sent },
10252     { WM_MOUSEHOVER, sent|wparam, 0 },
10253     { 0 }
10254 };
10255
10256 static void pump_msg_loop_timeout(DWORD timeout, BOOL inject_mouse_move)
10257 {
10258     MSG msg;
10259     DWORD start_ticks, end_ticks;
10260
10261     start_ticks = GetTickCount();
10262     /* add some deviation (50%) to cover not expected delays */
10263     start_ticks += timeout / 2;
10264
10265     do
10266     {
10267         while (PeekMessage(&msg, 0, 0, 0, PM_REMOVE))
10268         {
10269             /* Timer proc messages are not dispatched to the window proc,
10270              * and therefore not logged.
10271              */
10272             if ((msg.message == WM_TIMER || msg.message == WM_SYSTIMER) && msg.hwnd)
10273             {
10274                 struct recvd_message s_msg;
10275
10276                 s_msg.hwnd = msg.hwnd;
10277                 s_msg.message = msg.message;
10278                 s_msg.flags = sent|wparam|lparam;
10279                 s_msg.wParam = msg.wParam;
10280                 s_msg.lParam = msg.lParam;
10281                 s_msg.descr = "msg_loop";
10282                 add_message(&s_msg);
10283             }
10284             DispatchMessage(&msg);
10285         }
10286
10287         end_ticks = GetTickCount();
10288
10289         /* inject WM_MOUSEMOVE to see how it changes tracking */
10290         if (inject_mouse_move && start_ticks + timeout / 2 >= end_ticks)
10291         {
10292             mouse_event(MOUSEEVENTF_MOVE, -1, 0, 0, 0);
10293             mouse_event(MOUSEEVENTF_MOVE, 1, 0, 0, 0);
10294
10295             inject_mouse_move = FALSE;
10296         }
10297     } while (start_ticks + timeout >= end_ticks);
10298 }
10299
10300 static void test_TrackMouseEvent(void)
10301 {
10302     TRACKMOUSEEVENT tme;
10303     BOOL ret;
10304     HWND hwnd, hchild;
10305     RECT rc_parent, rc_child;
10306     UINT default_hover_time, hover_width = 0, hover_height = 0;
10307
10308 #define track_hover(track_hwnd, track_hover_time) \
10309     tme.cbSize = sizeof(tme); \
10310     tme.dwFlags = TME_HOVER; \
10311     tme.hwndTrack = track_hwnd; \
10312     tme.dwHoverTime = track_hover_time; \
10313     SetLastError(0xdeadbeef); \
10314     ret = pTrackMouseEvent(&tme); \
10315     ok(ret, "TrackMouseEvent(TME_HOVER) error %d\n", GetLastError())
10316
10317 #define track_query(expected_track_flags, expected_track_hwnd, expected_hover_time) \
10318     tme.cbSize = sizeof(tme); \
10319     tme.dwFlags = TME_QUERY; \
10320     tme.hwndTrack = (HWND)0xdeadbeef; \
10321     tme.dwHoverTime = 0xdeadbeef; \
10322     SetLastError(0xdeadbeef); \
10323     ret = pTrackMouseEvent(&tme); \
10324     ok(ret, "TrackMouseEvent(TME_QUERY) error %d\n", GetLastError());\
10325     ok(tme.cbSize == sizeof(tme), "wrong tme.cbSize %u\n", tme.cbSize); \
10326     ok(tme.dwFlags == (expected_track_flags), \
10327        "wrong tme.dwFlags %08x, expected %08x\n", tme.dwFlags, (expected_track_flags)); \
10328     ok(tme.hwndTrack == (expected_track_hwnd), \
10329        "wrong tme.hwndTrack %p, expected %p\n", tme.hwndTrack, (expected_track_hwnd)); \
10330     ok(tme.dwHoverTime == (expected_hover_time), \
10331        "wrong tme.dwHoverTime %u, expected %u\n", tme.dwHoverTime, (expected_hover_time))
10332
10333 #define track_hover_cancel(track_hwnd) \
10334     tme.cbSize = sizeof(tme); \
10335     tme.dwFlags = TME_HOVER | TME_CANCEL; \
10336     tme.hwndTrack = track_hwnd; \
10337     tme.dwHoverTime = 0xdeadbeef; \
10338     SetLastError(0xdeadbeef); \
10339     ret = pTrackMouseEvent(&tme); \
10340     ok(ret, "TrackMouseEvent(TME_HOVER | TME_CANCEL) error %d\n", GetLastError())
10341
10342     default_hover_time = 0xdeadbeef;
10343     SetLastError(0xdeadbeef);
10344     ret = SystemParametersInfo(SPI_GETMOUSEHOVERTIME, 0, &default_hover_time, 0);
10345     ok(ret || broken(GetLastError() == 0xdeadbeef),  /* win9x */
10346        "SystemParametersInfo(SPI_GETMOUSEHOVERTIME) error %u\n", GetLastError());
10347     if (!ret) default_hover_time = 400;
10348     trace("SPI_GETMOUSEHOVERTIME returned %u ms\n", default_hover_time);
10349
10350     SetLastError(0xdeadbeef);
10351     ret = SystemParametersInfo(SPI_GETMOUSEHOVERWIDTH, 0, &hover_width, 0);
10352     ok(ret || broken(GetLastError() == 0xdeadbeef),  /* win9x */
10353        "SystemParametersInfo(SPI_GETMOUSEHOVERWIDTH) error %u\n", GetLastError());
10354     if (!ret) hover_width = 4;
10355     SetLastError(0xdeadbeef);
10356     ret = SystemParametersInfo(SPI_GETMOUSEHOVERHEIGHT, 0, &hover_height, 0);
10357     ok(ret || broken(GetLastError() == 0xdeadbeef),  /* win9x */
10358        "SystemParametersInfo(SPI_GETMOUSEHOVERHEIGHT) error %u\n", GetLastError());
10359     if (!ret) hover_height = 4;
10360     trace("hover rect is %u x %d\n", hover_width, hover_height);
10361
10362     hwnd = CreateWindowEx(0, "TestWindowClass", NULL,
10363                           WS_OVERLAPPEDWINDOW | WS_VISIBLE,
10364                           CW_USEDEFAULT, CW_USEDEFAULT, 300, 300, 0,
10365                           NULL, NULL, 0);
10366     assert(hwnd);
10367
10368     hchild = CreateWindowEx(0, "TestWindowClass", NULL,
10369                           WS_CHILD | WS_BORDER | WS_VISIBLE,
10370                           50, 50, 200, 200, hwnd,
10371                           NULL, NULL, 0);
10372     assert(hchild);
10373
10374     SetWindowPos( hwnd, HWND_TOPMOST, 0, 0, 0, 0, SWP_NOSIZE|SWP_NOMOVE );
10375     flush_events();
10376     flush_sequence();
10377
10378     tme.cbSize = 0;
10379     tme.dwFlags = TME_QUERY;
10380     tme.hwndTrack = (HWND)0xdeadbeef;
10381     tme.dwHoverTime = 0xdeadbeef;
10382     SetLastError(0xdeadbeef);
10383     ret = pTrackMouseEvent(&tme);
10384     ok(!ret, "TrackMouseEvent should fail\n");
10385     ok(GetLastError() == ERROR_INVALID_PARAMETER || broken(GetLastError() == 0xdeadbeef),
10386        "not expected error %u\n", GetLastError());
10387
10388     tme.cbSize = sizeof(tme);
10389     tme.dwFlags = TME_HOVER;
10390     tme.hwndTrack = (HWND)0xdeadbeef;
10391     tme.dwHoverTime = 0xdeadbeef;
10392     SetLastError(0xdeadbeef);
10393     ret = pTrackMouseEvent(&tme);
10394     ok(!ret, "TrackMouseEvent should fail\n");
10395     ok(GetLastError() == ERROR_INVALID_WINDOW_HANDLE || broken(GetLastError() == 0xdeadbeef),
10396        "not expected error %u\n", GetLastError());
10397
10398     tme.cbSize = sizeof(tme);
10399     tme.dwFlags = TME_HOVER | TME_CANCEL;
10400     tme.hwndTrack = (HWND)0xdeadbeef;
10401     tme.dwHoverTime = 0xdeadbeef;
10402     SetLastError(0xdeadbeef);
10403     ret = pTrackMouseEvent(&tme);
10404     ok(!ret, "TrackMouseEvent should fail\n");
10405     ok(GetLastError() == ERROR_INVALID_WINDOW_HANDLE || broken(GetLastError() == 0xdeadbeef),
10406        "not expected error %u\n", GetLastError());
10407
10408     GetWindowRect(hwnd, &rc_parent);
10409     GetWindowRect(hchild, &rc_child);
10410     SetCursorPos(rc_child.left - 10, rc_child.top - 10);
10411
10412     /* Process messages so that the system updates its internal current
10413      * window and hittest, otherwise TrackMouseEvent calls don't have any
10414      * effect.
10415      */
10416     flush_events();
10417     flush_sequence();
10418
10419     track_query(0, NULL, 0);
10420     track_hover(hchild, 0);
10421     track_query(0, NULL, 0);
10422
10423     flush_events();
10424     flush_sequence();
10425
10426     track_hover(hwnd, 0);
10427     tme.cbSize = sizeof(tme);
10428     tme.dwFlags = TME_QUERY;
10429     tme.hwndTrack = (HWND)0xdeadbeef;
10430     tme.dwHoverTime = 0xdeadbeef;
10431     SetLastError(0xdeadbeef);
10432     ret = pTrackMouseEvent(&tme);
10433     ok(ret, "TrackMouseEvent(TME_QUERY) error %d\n", GetLastError());
10434     ok(tme.cbSize == sizeof(tme), "wrong tme.cbSize %u\n", tme.cbSize);
10435     if (!tme.dwFlags)
10436     {
10437         skip( "Cursor not inside window, skipping TrackMouseEvent tests\n" );
10438         DestroyWindow( hwnd );
10439         return;
10440     }
10441     ok(tme.dwFlags == TME_HOVER, "wrong tme.dwFlags %08x, expected TME_HOVER\n", tme.dwFlags);
10442     ok(tme.hwndTrack == hwnd, "wrong tme.hwndTrack %p, expected %p\n", tme.hwndTrack, hwnd);
10443     ok(tme.dwHoverTime == default_hover_time, "wrong tme.dwHoverTime %u, expected %u\n",
10444        tme.dwHoverTime, default_hover_time);
10445
10446     pump_msg_loop_timeout(default_hover_time, FALSE);
10447     ok_sequence(WmMouseHoverSeq, "WmMouseHoverSeq", FALSE);
10448
10449     track_query(0, NULL, 0);
10450
10451     track_hover(hwnd, HOVER_DEFAULT);
10452     track_query(TME_HOVER, hwnd, default_hover_time);
10453
10454     Sleep(default_hover_time / 2);
10455     mouse_event(MOUSEEVENTF_MOVE, -1, 0, 0, 0);
10456     mouse_event(MOUSEEVENTF_MOVE, 1, 0, 0, 0);
10457
10458     track_query(TME_HOVER, hwnd, default_hover_time);
10459
10460     pump_msg_loop_timeout(default_hover_time, FALSE);
10461     ok_sequence(WmMouseHoverSeq, "WmMouseHoverSeq", FALSE);
10462
10463     track_query(0, NULL, 0);
10464
10465     track_hover(hwnd, HOVER_DEFAULT);
10466     track_query(TME_HOVER, hwnd, default_hover_time);
10467
10468     pump_msg_loop_timeout(default_hover_time, TRUE);
10469     ok_sequence(WmMouseHoverSeq, "WmMouseHoverSeq", FALSE);
10470
10471     track_query(0, NULL, 0);
10472
10473     track_hover(hwnd, HOVER_DEFAULT);
10474     track_query(TME_HOVER, hwnd, default_hover_time);
10475     track_hover_cancel(hwnd);
10476
10477     DestroyWindow(hwnd);
10478
10479 #undef track_hover
10480 #undef track_query
10481 #undef track_hover_cancel
10482 }
10483
10484
10485 static const struct message WmSetWindowRgn[] = {
10486     { WM_WINDOWPOSCHANGING, sent|wparam, SWP_NOCLIENTSIZE|SWP_NOCLIENTMOVE|SWP_NOACTIVATE|SWP_FRAMECHANGED|SWP_NOSIZE|SWP_NOMOVE },
10487     { WM_NCCALCSIZE, sent|wparam, 1 },
10488     { WM_NCPAINT, sent|optional }, /* wparam != 1 */
10489     { WM_GETTEXT, sent|defwinproc|optional },
10490     { WM_ERASEBKGND, sent|optional },
10491     { WM_WINDOWPOSCHANGED, sent|wparam, SWP_NOCLIENTSIZE|SWP_NOCLIENTMOVE|SWP_NOACTIVATE|SWP_FRAMECHANGED|SWP_NOSIZE|SWP_NOMOVE },
10492     { EVENT_OBJECT_LOCATIONCHANGE, winevent_hook|wparam|lparam, 0, 0 },
10493     { 0 }
10494 };
10495
10496 static const struct message WmSetWindowRgn_no_redraw[] = {
10497     { WM_WINDOWPOSCHANGING, sent|wparam, SWP_NOCLIENTSIZE|SWP_NOCLIENTMOVE|SWP_NOACTIVATE|SWP_FRAMECHANGED|SWP_NOSIZE|SWP_NOMOVE|SWP_NOREDRAW },
10498     { WM_NCCALCSIZE, sent|wparam, 1 },
10499     { WM_WINDOWPOSCHANGED, sent|wparam, SWP_NOCLIENTSIZE|SWP_NOCLIENTMOVE|SWP_NOACTIVATE|SWP_FRAMECHANGED|SWP_NOSIZE|SWP_NOMOVE|SWP_NOREDRAW },
10500     { EVENT_OBJECT_LOCATIONCHANGE, winevent_hook|wparam|lparam, 0, 0 },
10501     { 0 }
10502 };
10503
10504 static const struct message WmSetWindowRgn_clear[] = {
10505     { WM_WINDOWPOSCHANGING, sent/*|wparam*/, SWP_NOACTIVATE|SWP_FRAMECHANGED|SWP_NOSIZE|SWP_NOMOVE/*|SWP_NOCLIENTSIZE|SWP_NOCLIENTMOVE only on some Windows versions */ },
10506     { WM_NCCALCSIZE, sent|wparam, 1 },
10507     { WM_NCPAINT, sent|optional },
10508     { WM_GETTEXT, sent|defwinproc|optional },
10509     { WM_ERASEBKGND, sent|optional }, /* FIXME: remove optional once Wine is fixed */
10510     { WM_WINDOWPOSCHANGED, sent|wparam, SWP_NOCLIENTSIZE|SWP_NOCLIENTMOVE|SWP_NOACTIVATE|SWP_FRAMECHANGED|SWP_NOSIZE|SWP_NOMOVE },
10511     { WM_NCCALCSIZE, sent|wparam|optional, 1 },
10512     { WM_NCPAINT, sent|optional },
10513     { WM_GETTEXT, sent|defwinproc|optional },
10514     { WM_ERASEBKGND, sent|optional },
10515     { WM_WINDOWPOSCHANGING, sent|optional },
10516     { WM_NCCALCSIZE, sent|optional|wparam, 1 },
10517     { WM_NCPAINT, sent|optional },
10518     { WM_GETTEXT, sent|defwinproc|optional },
10519     { WM_ERASEBKGND, sent|optional },
10520     { WM_WINDOWPOSCHANGED, sent|optional|wparam, SWP_NOCLIENTSIZE|SWP_NOCLIENTMOVE|SWP_NOACTIVATE|SWP_FRAMECHANGED|SWP_NOSIZE|SWP_NOMOVE },
10521     { WM_NCCALCSIZE, sent|optional|wparam, 1 },
10522     { WM_NCPAINT, sent|optional },
10523     { WM_GETTEXT, sent|defwinproc|optional },
10524     { WM_ERASEBKGND, sent|optional },
10525     { EVENT_OBJECT_LOCATIONCHANGE, winevent_hook|wparam|lparam, 0, 0 },
10526     { EVENT_OBJECT_LOCATIONCHANGE, winevent_hook|wparam|lparam, 0, 0 },
10527     { 0 }
10528 };
10529
10530 static void test_SetWindowRgn(void)
10531 {
10532     HRGN hrgn;
10533     HWND hwnd = CreateWindowExA(0, "TestWindowClass", "Test overlapped", WS_OVERLAPPEDWINDOW,
10534                                 100, 100, 200, 200, 0, 0, 0, NULL);
10535     ok( hwnd != 0, "Failed to create overlapped window\n" );
10536
10537     ShowWindow( hwnd, SW_SHOW );
10538     UpdateWindow( hwnd );
10539     flush_events();
10540     flush_sequence();
10541
10542     trace("testing SetWindowRgn\n");
10543     hrgn = CreateRectRgn( 0, 0, 150, 150 );
10544     SetWindowRgn( hwnd, hrgn, TRUE );
10545     ok_sequence( WmSetWindowRgn, "WmSetWindowRgn", FALSE );
10546
10547     hrgn = CreateRectRgn( 30, 30, 160, 160 );
10548     SetWindowRgn( hwnd, hrgn, FALSE );
10549     ok_sequence( WmSetWindowRgn_no_redraw, "WmSetWindowRgn_no_redraw", FALSE );
10550
10551     hrgn = CreateRectRgn( 0, 0, 180, 180 );
10552     SetWindowRgn( hwnd, hrgn, TRUE );
10553     ok_sequence( WmSetWindowRgn, "WmSetWindowRgn2", FALSE );
10554
10555     SetWindowRgn( hwnd, 0, TRUE );
10556     ok_sequence( WmSetWindowRgn_clear, "WmSetWindowRgn_clear", FALSE );
10557
10558     DestroyWindow( hwnd );
10559 }
10560
10561 /*************************** ShowWindow() test ******************************/
10562 static const struct message WmShowNormal[] = {
10563     { WM_SHOWWINDOW, sent|wparam, 1 },
10564     { WM_WINDOWPOSCHANGING, sent|wparam, SWP_SHOWWINDOW|SWP_NOSIZE|SWP_NOMOVE },
10565     { HCBT_ACTIVATE, hook },
10566     { WM_WINDOWPOSCHANGING, sent|wparam|optional, SWP_NOSIZE|SWP_NOMOVE }, /* win2003 doesn't send it */
10567     { HCBT_SETFOCUS, hook },
10568     { WM_WINDOWPOSCHANGED, sent|wparam, SWP_SHOWWINDOW|SWP_NOSIZE|SWP_NOMOVE|SWP_NOCLIENTSIZE|SWP_NOCLIENTMOVE },
10569     { 0 }
10570 };
10571 static const struct message WmShow[] = {
10572     { WM_SHOWWINDOW, sent|wparam, 1 },
10573     { WM_WINDOWPOSCHANGING, sent|wparam, SWP_SHOWWINDOW|SWP_NOSIZE|SWP_NOMOVE },
10574     { HCBT_ACTIVATE, hook|optional }, /* win2000 doesn't send it */
10575     { WM_WINDOWPOSCHANGING, sent|wparam|optional, SWP_NOSIZE|SWP_NOMOVE }, /* win2000 doesn't send it */
10576     { HCBT_SETFOCUS, hook|optional }, /* win2000 doesn't send it */
10577     { WM_WINDOWPOSCHANGED, sent|wparam, SWP_SHOWWINDOW|SWP_NOSIZE|SWP_NOMOVE|SWP_NOCLIENTSIZE|SWP_NOCLIENTMOVE },
10578     { 0 }
10579 };
10580 static const struct message WmShowNoActivate_1[] = {
10581     { HCBT_MINMAX, hook|lparam, 0, SW_SHOWNOACTIVATE },
10582     { WM_WINDOWPOSCHANGING, sent|wparam, SWP_SHOWWINDOW|SWP_NOACTIVATE|SWP_FRAMECHANGED|SWP_STATECHANGED, 0, SWP_NOCLIENTSIZE|SWP_NOCLIENTMOVE|SWP_NOSIZE|SWP_NOMOVE },
10583     { WM_WINDOWPOSCHANGED, sent|wparam, SWP_SHOWWINDOW|SWP_NOACTIVATE|SWP_FRAMECHANGED|SWP_STATECHANGED, 0, SWP_NOCLIENTSIZE|SWP_NOCLIENTMOVE|SWP_NOSIZE|SWP_NOMOVE },
10584     { WM_MOVE, sent|defwinproc|optional },
10585     { WM_SIZE, sent|wparam|defwinproc, SIZE_RESTORED },
10586     { 0 }
10587 };
10588 static const struct message WmShowNoActivate_2[] = {
10589     { HCBT_MINMAX, hook|lparam, 0, SW_SHOWNOACTIVATE },
10590     { WM_WINDOWPOSCHANGING, sent|wparam, SWP_SHOWWINDOW|SWP_NOACTIVATE|SWP_FRAMECHANGED|SWP_NOCOPYBITS|SWP_STATECHANGED },
10591     { WM_WINDOWPOSCHANGED, sent|wparam, SWP_SHOWWINDOW|SWP_NOACTIVATE|SWP_FRAMECHANGED|SWP_NOCOPYBITS|SWP_STATECHANGED },
10592     { WM_MOVE, sent|defwinproc },
10593     { WM_SIZE, sent|wparam|defwinproc, SIZE_RESTORED },
10594     { HCBT_SETFOCUS, hook|optional },
10595     { HCBT_ACTIVATE, hook|optional }, /* win2003 doesn't send it */
10596     { WM_WINDOWPOSCHANGING, sent|wparam|optional, SWP_NOSIZE|SWP_NOMOVE }, /* win2003 doesn't send it */
10597     { WM_WINDOWPOSCHANGED, sent|wparam|optional, SWP_NOSIZE|SWP_NOMOVE|SWP_NOCLIENTSIZE|SWP_NOCLIENTMOVE },
10598     { HCBT_SETFOCUS, hook|optional }, /* win2003 doesn't send it */
10599     { 0 }
10600 };
10601 static const struct message WmShowNA_1[] = {
10602     { WM_SHOWWINDOW, sent|wparam, 1 },
10603     { WM_WINDOWPOSCHANGING, sent|wparam, SWP_SHOWWINDOW|SWP_NOACTIVATE|SWP_NOSIZE|SWP_NOMOVE },
10604     { WM_WINDOWPOSCHANGED, sent|wparam, SWP_SHOWWINDOW|SWP_NOACTIVATE|SWP_NOSIZE|SWP_NOMOVE|SWP_NOCLIENTSIZE|SWP_NOCLIENTMOVE },
10605     { 0 }
10606 };
10607 static const struct message WmShowNA_2[] = {
10608     { WM_SHOWWINDOW, sent|wparam, 1 },
10609     { WM_WINDOWPOSCHANGING, sent|wparam, SWP_SHOWWINDOW|SWP_NOACTIVATE|SWP_NOSIZE|SWP_NOMOVE },
10610     { WM_WINDOWPOSCHANGED, sent|wparam|optional, SWP_NOACTIVATE|SWP_NOSIZE|SWP_NOMOVE|SWP_NOCLIENTSIZE|SWP_NOCLIENTMOVE },
10611     { 0 }
10612 };
10613 static const struct message WmRestore_1[] = {
10614     { HCBT_MINMAX, hook|lparam, 0, SW_RESTORE },
10615     { WM_WINDOWPOSCHANGING, sent|wparam, SWP_SHOWWINDOW|SWP_FRAMECHANGED|SWP_NOCOPYBITS|SWP_STATECHANGED },
10616     { HCBT_ACTIVATE, hook|optional }, /* win2000 doesn't send it */
10617     { WM_WINDOWPOSCHANGING, sent|wparam|optional, SWP_NOSIZE|SWP_NOMOVE }, /* win2000 doesn't send it */
10618     { HCBT_SETFOCUS, hook|optional }, /* win2000 doesn't send it */
10619     { WM_WINDOWPOSCHANGED, sent|wparam, SWP_SHOWWINDOW|SWP_FRAMECHANGED|SWP_NOCOPYBITS|SWP_STATECHANGED },
10620     { WM_MOVE, sent|defwinproc },
10621     { WM_SIZE, sent|wparam|defwinproc, SIZE_RESTORED },
10622     { HCBT_SETFOCUS, hook|optional }, /* win2000 sends it */
10623     { 0 }
10624 };
10625 static const struct message WmRestore_2[] = {
10626     { WM_SHOWWINDOW, sent|wparam, 1 },
10627     { WM_WINDOWPOSCHANGING, sent|wparam, SWP_SHOWWINDOW|SWP_NOSIZE|SWP_NOMOVE },
10628     { HCBT_ACTIVATE, hook|optional }, /* win2000 doesn't send it */
10629     { WM_WINDOWPOSCHANGING, sent|wparam|optional, SWP_NOSIZE|SWP_NOMOVE }, /* win2000 doesn't send it */
10630     { HCBT_SETFOCUS, hook|optional }, /* win2000 doesn't send it */
10631     { WM_WINDOWPOSCHANGED, sent|wparam, SWP_SHOWWINDOW|SWP_NOSIZE|SWP_NOMOVE|SWP_NOCLIENTSIZE|SWP_NOCLIENTMOVE },
10632     { 0 }
10633 };
10634 static const struct message WmRestore_3[] = {
10635     { HCBT_MINMAX, hook|lparam, 0, SW_RESTORE },
10636     { WM_GETMINMAXINFO, sent },
10637     { WM_WINDOWPOSCHANGING, sent|wparam, SWP_FRAMECHANGED|SWP_NOCOPYBITS|SWP_STATECHANGED },
10638     { HCBT_ACTIVATE, hook|optional }, /* win2003 doesn't send it */
10639     { WM_WINDOWPOSCHANGING, sent|wparam|optional, SWP_NOSIZE|SWP_NOMOVE }, /* win2003 doesn't send it */
10640     { HCBT_SETFOCUS, hook|optional }, /* win2003 doesn't send it */
10641     { WM_WINDOWPOSCHANGED, sent|wparam, SWP_FRAMECHANGED|SWP_NOCOPYBITS|SWP_STATECHANGED },
10642     { WM_MOVE, sent|defwinproc },
10643     { WM_SIZE, sent|wparam|defwinproc, SIZE_MAXIMIZED },
10644     { HCBT_SETFOCUS, hook|optional }, /* win2003 sends it */
10645     { 0 }
10646 };
10647 static const struct message WmRestore_4[] = {
10648     { HCBT_MINMAX, hook|lparam|optional, 0, SW_RESTORE },
10649     { WM_WINDOWPOSCHANGING, sent|wparam|optional, SWP_FRAMECHANGED|SWP_STATECHANGED, 0, SWP_NOCLIENTSIZE|SWP_NOCLIENTMOVE|SWP_NOSIZE|SWP_NOMOVE },
10650     { WM_WINDOWPOSCHANGED, sent|wparam|optional, SWP_FRAMECHANGED|SWP_STATECHANGED, 0, SWP_NOCLIENTSIZE|SWP_NOCLIENTMOVE|SWP_NOSIZE|SWP_NOMOVE },
10651     { WM_MOVE, sent|defwinproc|optional },
10652     { WM_SIZE, sent|wparam|defwinproc|optional, SIZE_RESTORED },
10653     { 0 }
10654 };
10655 static const struct message WmRestore_5[] = {
10656     { HCBT_MINMAX, hook|lparam|optional, 0, SW_SHOWNORMAL },
10657     { WM_WINDOWPOSCHANGING, sent|wparam|optional, SWP_FRAMECHANGED|SWP_STATECHANGED, 0, SWP_NOCLIENTSIZE|SWP_NOCLIENTMOVE|SWP_NOSIZE|SWP_NOMOVE },
10658     { HCBT_ACTIVATE, hook|optional },
10659     { HCBT_SETFOCUS, hook|optional },
10660     { WM_WINDOWPOSCHANGED, sent|wparam|optional, SWP_FRAMECHANGED|SWP_STATECHANGED, 0, SWP_NOCLIENTSIZE|SWP_NOCLIENTMOVE|SWP_NOSIZE|SWP_NOMOVE },
10661     { WM_MOVE, sent|defwinproc|optional },
10662     { WM_SIZE, sent|wparam|defwinproc|optional, SIZE_RESTORED },
10663     { 0 }
10664 };
10665 static const struct message WmHide_1[] = {
10666     { WM_SHOWWINDOW, sent|wparam, 0 },
10667     { WM_WINDOWPOSCHANGING, sent|wparam, SWP_HIDEWINDOW|SWP_NOSIZE|SWP_NOMOVE, 0, SWP_NOACTIVATE },
10668     { WM_WINDOWPOSCHANGED, sent|wparam, SWP_HIDEWINDOW|SWP_NOSIZE|SWP_NOMOVE|SWP_NOCLIENTSIZE|SWP_NOCLIENTMOVE, 0, SWP_NOACTIVATE },
10669     { HCBT_ACTIVATE, hook|optional },
10670     { HCBT_SETFOCUS, hook|optional }, /* win2000 sends it */
10671     { 0 }
10672 };
10673 static const struct message WmHide_2[] = {
10674     { WM_SHOWWINDOW, sent|wparam, 0 },
10675     { WM_WINDOWPOSCHANGING, sent /*|wparam, SWP_HIDEWINDOW|SWP_NOACTIVATE|SWP_NOSIZE|SWP_NOMOVE*/ }, /* win2000 doesn't add SWP_NOACTIVATE */
10676     { WM_WINDOWPOSCHANGED, sent /*|wparam, SWP_HIDEWINDOW|SWP_NOACTIVATE|SWP_NOSIZE|SWP_NOMOVE|SWP_NOCLIENTSIZE|SWP_NOCLIENTMOVE*/ }, /* win2000 doesn't add SWP_NOACTIVATE */
10677     { HCBT_ACTIVATE, hook|optional },
10678     { 0 }
10679 };
10680 static const struct message WmHide_3[] = {
10681     { WM_SHOWWINDOW, sent|wparam, 0 },
10682     { WM_WINDOWPOSCHANGING, sent|wparam, SWP_HIDEWINDOW|SWP_NOSIZE|SWP_NOMOVE },
10683     { WM_WINDOWPOSCHANGED, sent|wparam, SWP_HIDEWINDOW|SWP_NOSIZE|SWP_NOMOVE|SWP_NOCLIENTSIZE|SWP_NOCLIENTMOVE },
10684     { HCBT_SETFOCUS, hook|optional },
10685     { EVENT_OBJECT_FOCUS, winevent_hook|wparam|lparam, OBJID_CLIENT, 0 },
10686     { 0 }
10687 };
10688 static const struct message WmShowMinimized_1[] = {
10689     { HCBT_MINMAX, hook|lparam, 0, SW_SHOWMINIMIZED },
10690     { WM_WINDOWPOSCHANGING, sent|wparam, SWP_SHOWWINDOW|SWP_FRAMECHANGED|SWP_NOCOPYBITS|SWP_STATECHANGED },
10691     { HCBT_ACTIVATE, hook|optional }, /* win2000 doesn't send it */
10692     { WM_WINDOWPOSCHANGING, sent|wparam|optional, SWP_NOSIZE|SWP_NOMOVE }, /* win2000 doesn't send it */
10693     { WM_WINDOWPOSCHANGED, sent|wparam, SWP_SHOWWINDOW|SWP_FRAMECHANGED|SWP_NOCOPYBITS|SWP_STATECHANGED },
10694     { WM_MOVE, sent|defwinproc },
10695     { WM_SIZE, sent|wparam|lparam|defwinproc, SIZE_MINIMIZED, 0 },
10696     { 0 }
10697 };
10698 static const struct message WmMinimize_1[] = {
10699     { HCBT_MINMAX, hook|lparam, 0, SW_MINIMIZE },
10700     { HCBT_SETFOCUS, hook|optional }, /* win2000 doesn't send it */
10701     { EVENT_OBJECT_FOCUS, winevent_hook|wparam|lparam, OBJID_CLIENT, 0 },
10702     { WM_WINDOWPOSCHANGING, sent|wparam, SWP_SHOWWINDOW|SWP_NOACTIVATE|SWP_FRAMECHANGED|SWP_NOCOPYBITS|SWP_STATECHANGED },
10703     { WM_WINDOWPOSCHANGED, sent|wparam, SWP_NOACTIVATE|SWP_FRAMECHANGED|SWP_NOCOPYBITS|SWP_STATECHANGED },
10704     { WM_MOVE, sent|defwinproc },
10705     { WM_SIZE, sent|wparam|lparam|defwinproc, SIZE_MINIMIZED, 0 },
10706     { 0 }
10707 };
10708 static const struct message WmMinimize_2[] = {
10709     { HCBT_MINMAX, hook|lparam, 0, SW_MINIMIZE },
10710     { HCBT_SETFOCUS, hook|optional },
10711     { WM_WINDOWPOSCHANGING, sent|wparam, SWP_SHOWWINDOW|SWP_NOACTIVATE|SWP_FRAMECHANGED|SWP_NOCOPYBITS|SWP_STATECHANGED },
10712     { WM_WINDOWPOSCHANGED, sent|wparam, SWP_NOACTIVATE|SWP_FRAMECHANGED|SWP_NOCOPYBITS|SWP_STATECHANGED },
10713     { WM_MOVE, sent|defwinproc },
10714     { WM_SIZE, sent|wparam|lparam|defwinproc, SIZE_MINIMIZED, 0 },
10715     { 0 }
10716 };
10717 static const struct message WmMinimize_3[] = {
10718     { HCBT_MINMAX, hook|lparam, 0, SW_MINIMIZE },
10719     { WM_WINDOWPOSCHANGING, sent|wparam, SWP_SHOWWINDOW|SWP_NOACTIVATE|SWP_FRAMECHANGED|SWP_NOCOPYBITS|SWP_STATECHANGED },
10720     { WM_WINDOWPOSCHANGED, sent|wparam, SWP_SHOWWINDOW|SWP_NOACTIVATE|SWP_FRAMECHANGED|SWP_NOCOPYBITS|SWP_STATECHANGED },
10721     { WM_MOVE, sent|defwinproc },
10722     { WM_SIZE, sent|wparam|lparam|defwinproc, SIZE_MINIMIZED, 0 },
10723     { 0 }
10724 };
10725 static const struct message WmShowMinNoActivate[] = {
10726     { HCBT_MINMAX, hook|lparam, 0, SW_SHOWMINNOACTIVE },
10727     { WM_WINDOWPOSCHANGING, sent },
10728     { WM_WINDOWPOSCHANGED, sent },
10729     { WM_MOVE, sent|defwinproc|optional },
10730     { WM_SIZE, sent|wparam|lparam|defwinproc|optional, SIZE_MINIMIZED, 0 },
10731     { 0 }
10732 };
10733 static const struct message WmMinMax_1[] = {
10734     { HCBT_MINMAX, hook|lparam, 0, SW_SHOWMINIMIZED },
10735     { 0 }
10736 };
10737 static const struct message WmMinMax_2[] = {
10738     { HCBT_MINMAX, hook|lparam, 0, SW_SHOWMAXIMIZED },
10739     { WM_GETMINMAXINFO, sent|optional },
10740     { WM_WINDOWPOSCHANGING, sent|wparam|optional, SWP_FRAMECHANGED|SWP_STATECHANGED },
10741     { HCBT_ACTIVATE, hook|optional },
10742     { WM_WINDOWPOSCHANGING, sent|wparam|optional, SWP_NOSIZE|SWP_NOMOVE },
10743     { HCBT_SETFOCUS, hook|optional },
10744     { WM_WINDOWPOSCHANGED, sent|wparam|optional, SWP_FRAMECHANGED|SWP_STATECHANGED, 0, SWP_NOSIZE|SWP_NOMOVE|SWP_NOCLIENTSIZE|SWP_NOCLIENTMOVE },
10745     { WM_MOVE, sent|defwinproc|optional },
10746     { WM_SIZE, sent|wparam|defwinproc|optional, SIZE_MAXIMIZED },
10747     { HCBT_SETFOCUS, hook|optional },
10748     { 0 }
10749 };
10750 static const struct message WmMinMax_3[] = {
10751     { HCBT_MINMAX, hook|lparam, 0, SW_MINIMIZE },
10752     { HCBT_SETFOCUS, hook|optional },
10753     { WM_WINDOWPOSCHANGING, sent|wparam|optional, SWP_SHOWWINDOW|SWP_NOACTIVATE|SWP_FRAMECHANGED|SWP_NOCOPYBITS|SWP_STATECHANGED },
10754     { WM_WINDOWPOSCHANGED, sent|wparam|optional, SWP_NOACTIVATE|SWP_FRAMECHANGED|SWP_NOCOPYBITS|SWP_STATECHANGED },
10755     { WM_MOVE, sent|defwinproc|optional },
10756     { WM_SIZE, sent|wparam|lparam|defwinproc|optional, SIZE_MINIMIZED, 0 },
10757     { 0 }
10758 };
10759 static const struct message WmMinMax_4[] = {
10760     { HCBT_MINMAX, hook|lparam, 0, SW_SHOWMINNOACTIVE },
10761     { 0 }
10762 };
10763 static const struct message WmShowMaximized_1[] = {
10764     { HCBT_MINMAX, hook|lparam, 0, SW_SHOWMAXIMIZED },
10765     { WM_GETMINMAXINFO, sent },
10766     { WM_WINDOWPOSCHANGING, sent|wparam, SWP_SHOWWINDOW|SWP_FRAMECHANGED|SWP_NOCOPYBITS|SWP_STATECHANGED },
10767     { HCBT_ACTIVATE, hook|optional }, /* win2000 doesn't send it */
10768     { WM_WINDOWPOSCHANGING, sent|wparam|optional, SWP_NOSIZE|SWP_NOMOVE }, /* win2000 doesn't send it */
10769     { HCBT_SETFOCUS, hook|optional }, /* win2000 doesn't send it */
10770     { WM_WINDOWPOSCHANGED, sent|wparam, SWP_SHOWWINDOW|SWP_FRAMECHANGED|SWP_NOCOPYBITS|SWP_STATECHANGED },
10771     { WM_MOVE, sent|defwinproc },
10772     { WM_SIZE, sent|wparam|defwinproc, SIZE_MAXIMIZED },
10773     { HCBT_SETFOCUS, hook|optional }, /* win2003 sends it */
10774     { 0 }
10775 };
10776 static const struct message WmShowMaximized_2[] = {
10777     { HCBT_MINMAX, hook|lparam, 0, SW_SHOWMAXIMIZED },
10778     { WM_GETMINMAXINFO, sent },
10779     { WM_WINDOWPOSCHANGING, sent|optional },
10780     { HCBT_ACTIVATE, hook|optional },
10781     { WM_WINDOWPOSCHANGED, sent|optional },
10782     { WM_MOVE, sent|optional }, /* Win9x doesn't send it */
10783     { WM_SIZE, sent|wparam|optional, SIZE_MAXIMIZED }, /* Win9x doesn't send it */
10784     { WM_WINDOWPOSCHANGING, sent|optional },
10785     { HCBT_SETFOCUS, hook|optional },
10786     { WM_WINDOWPOSCHANGED, sent|wparam|optional, SWP_FRAMECHANGED|SWP_NOCOPYBITS|SWP_STATECHANGED },
10787     { WM_MOVE, sent|defwinproc },
10788     { WM_SIZE, sent|wparam|defwinproc, SIZE_MAXIMIZED },
10789     { HCBT_SETFOCUS, hook|optional },
10790     { 0 }
10791 };
10792 static const struct message WmShowMaximized_3[] = {
10793     { HCBT_MINMAX, hook|lparam, 0, SW_SHOWMAXIMIZED },
10794     { WM_GETMINMAXINFO, sent|optional },
10795     { WM_WINDOWPOSCHANGING, sent|wparam, SWP_FRAMECHANGED|SWP_STATECHANGED, 0, SWP_NOCLIENTSIZE|SWP_NOCLIENTMOVE|SWP_NOSIZE|SWP_NOMOVE },
10796     { HCBT_ACTIVATE, hook|optional }, /* win2000 doesn't send it */
10797     { WM_WINDOWPOSCHANGING, sent|wparam|optional, SWP_NOSIZE|SWP_NOMOVE }, /* win2000 doesn't send it */
10798     { HCBT_SETFOCUS, hook|optional }, /* win2000 doesn't send it */
10799     { WM_WINDOWPOSCHANGED, sent|wparam, SWP_FRAMECHANGED|SWP_STATECHANGED, 0, SWP_NOCLIENTSIZE|SWP_NOCLIENTMOVE|SWP_NOSIZE|SWP_NOMOVE },
10800     { WM_MOVE, sent|defwinproc|optional },
10801     { WM_SIZE, sent|wparam|defwinproc, SIZE_MAXIMIZED },
10802     { 0 }
10803 };
10804
10805 static void test_ShowWindow(void)
10806 {
10807     /* ShowWindow commands in random order */
10808     static const struct
10809     {
10810         INT cmd; /* ShowWindow command */
10811         LPARAM ret; /* ShowWindow return value */
10812         DWORD style; /* window style after the command */
10813         const struct message *msg; /* message sequence the command produces */
10814         INT wp_cmd, wp_flags; /* window placement after the command */
10815         POINT wp_min, wp_max; /* window placement after the command */
10816         BOOL todo_msg; /* message sequence doesn't match what Wine does */
10817     } sw[] =
10818     {
10819 /*  1 */ { SW_SHOWNORMAL, FALSE, WS_VISIBLE, WmShowNormal,
10820            SW_SHOWNORMAL, 0, {-1,-1}, {-1,-1}, FALSE },
10821 /*  2 */ { SW_SHOWNORMAL, TRUE, WS_VISIBLE, WmEmptySeq,
10822            SW_SHOWNORMAL, 0, {-1,-1}, {-1,-1}, FALSE },
10823 /*  3 */ { SW_HIDE, TRUE, 0, WmHide_1,
10824            SW_SHOWNORMAL, 0, {-1,-1}, {-1,-1}, FALSE },
10825 /*  4 */ { SW_HIDE, FALSE, 0, WmEmptySeq,
10826            SW_SHOWNORMAL, 0, {-1,-1}, {-1,-1}, FALSE },
10827 /*  5 */ { SW_SHOWMINIMIZED, FALSE, WS_VISIBLE|WS_MINIMIZE, WmShowMinimized_1,
10828            SW_SHOWMINIMIZED, 0, {-32000,-32000}, {-1,-1}, FALSE },
10829 /*  6 */ { SW_SHOWMINIMIZED, TRUE, WS_VISIBLE|WS_MINIMIZE, WmMinMax_1,
10830            SW_SHOWMINIMIZED, 0, {-32000,-32000}, {-1,-1}, FALSE },
10831 /*  7 */ { SW_HIDE, TRUE, WS_MINIMIZE, WmHide_1,
10832            SW_SHOWMINIMIZED, 0, {-32000,-32000}, {-1,-1}, FALSE },
10833 /*  8 */ { SW_HIDE, FALSE, WS_MINIMIZE, WmEmptySeq,
10834            SW_SHOWMINIMIZED, 0, {-32000,-32000}, {-1,-1}, FALSE },
10835 /*  9 */ { SW_SHOWMAXIMIZED, FALSE, WS_VISIBLE|WS_MAXIMIZE, WmShowMaximized_1,
10836            SW_SHOWMAXIMIZED, WPF_RESTORETOMAXIMIZED, {-32000,-32000}, {-1,-1}, FALSE },
10837 /* 10 */ { SW_SHOWMAXIMIZED, TRUE, WS_VISIBLE|WS_MAXIMIZE, WmMinMax_2,
10838            SW_SHOWMAXIMIZED, WPF_RESTORETOMAXIMIZED, {-32000,-32000}, {-1,-1}, FALSE },
10839 /* 11 */ { SW_HIDE, TRUE, WS_MAXIMIZE, WmHide_1,
10840            SW_SHOWMAXIMIZED, WPF_RESTORETOMAXIMIZED, {-32000,-32000}, {-1,-1}, FALSE },
10841 /* 12 */ { SW_HIDE, FALSE, WS_MAXIMIZE, WmEmptySeq,
10842            SW_SHOWMAXIMIZED, WPF_RESTORETOMAXIMIZED, {-32000,-32000}, {-1,-1}, FALSE },
10843 /* 13 */ { SW_SHOWNOACTIVATE, FALSE, WS_VISIBLE, WmShowNoActivate_1,
10844            SW_SHOWNORMAL, 0, {-32000,-32000}, {-1,-1}, FALSE },
10845 /* 14 */ { SW_SHOWNOACTIVATE, TRUE, WS_VISIBLE, WmEmptySeq,
10846            SW_SHOWNORMAL, 0, {-32000,-32000}, {-1,-1}, FALSE },
10847 /* 15 */ { SW_HIDE, TRUE, 0, WmHide_2,
10848            SW_SHOWNORMAL, 0, {-32000,-32000}, {-1,-1}, FALSE },
10849 /* 16 */ { SW_HIDE, FALSE, 0, WmEmptySeq,
10850            SW_SHOWNORMAL, 0, {-32000,-32000}, {-1,-1}, FALSE },
10851 /* 17 */ { SW_SHOW, FALSE, WS_VISIBLE, WmShow,
10852            SW_SHOWNORMAL, 0, {-32000,-32000}, {-1,-1}, FALSE },
10853 /* 18 */ { SW_SHOW, TRUE, WS_VISIBLE, WmEmptySeq,
10854            SW_SHOWNORMAL, 0, {-32000,-32000}, {-1,-1}, FALSE },
10855 /* 19 */ { SW_MINIMIZE, TRUE, WS_VISIBLE|WS_MINIMIZE, WmMinimize_1,
10856            SW_SHOWMINIMIZED, 0, {-32000,-32000}, {-1,-1}, FALSE },
10857 /* 20 */ { SW_MINIMIZE, TRUE, WS_VISIBLE|WS_MINIMIZE, WmMinMax_3,
10858            SW_SHOWMINIMIZED, 0, {-32000,-32000}, {-1,-1}, FALSE },
10859 /* 21 */ { SW_HIDE, TRUE, WS_MINIMIZE, WmHide_2,
10860            SW_SHOWMINIMIZED, 0, {-32000,-32000}, {-1,-1}, FALSE },
10861 /* 22 */ { SW_SHOWMINNOACTIVE, FALSE, WS_VISIBLE|WS_MINIMIZE, WmShowMinNoActivate,
10862            SW_SHOWMINIMIZED, 0, {-32000,-32000}, {-1,-1}, TRUE },
10863 /* 23 */ { SW_SHOWMINNOACTIVE, TRUE, WS_VISIBLE|WS_MINIMIZE, WmMinMax_4,
10864            SW_SHOWMINIMIZED, 0, {-32000,-32000}, {-1,-1}, FALSE },
10865 /* 24 */ { SW_HIDE, TRUE, WS_MINIMIZE, WmHide_2,
10866            SW_SHOWMINIMIZED, 0, {-32000,-32000}, {-1,-1}, FALSE },
10867 /* 25 */ { SW_HIDE, FALSE, WS_MINIMIZE, WmEmptySeq,
10868            SW_SHOWMINIMIZED, 0, {-32000,-32000}, {-1,-1}, FALSE },
10869 /* 26 */ { SW_SHOWNA, FALSE, WS_VISIBLE|WS_MINIMIZE, WmShowNA_1,
10870            SW_SHOWMINIMIZED, 0, {-32000,-32000}, {-1,-1}, FALSE },
10871 /* 27 */ { SW_SHOWNA, TRUE, WS_VISIBLE|WS_MINIMIZE, WmShowNA_2,
10872            SW_SHOWMINIMIZED, 0, {-32000,-32000}, {-1,-1}, FALSE },
10873 /* 28 */ { SW_HIDE, TRUE, WS_MINIMIZE, WmHide_2,
10874            SW_SHOWMINIMIZED, 0, {-32000,-32000}, {-1,-1}, FALSE },
10875 /* 29 */ { SW_HIDE, FALSE, WS_MINIMIZE, WmEmptySeq,
10876            SW_SHOWMINIMIZED, 0, {-32000,-32000}, {-1,-1}, FALSE },
10877 /* 30 */ { SW_RESTORE, FALSE, WS_VISIBLE, WmRestore_1,
10878            SW_SHOWNORMAL, 0, {-32000,-32000}, {-1,-1}, FALSE },
10879 /* 31 */ { SW_RESTORE, TRUE, WS_VISIBLE, WmEmptySeq,
10880            SW_SHOWNORMAL, 0, {-32000,-32000}, {-1,-1}, FALSE },
10881 /* 32 */ { SW_HIDE, TRUE, 0, WmHide_3,
10882            SW_SHOWNORMAL, 0, {-32000,-32000}, {-1,-1}, FALSE },
10883 /* 33 */ { SW_HIDE, FALSE, 0, WmEmptySeq,
10884            SW_SHOWNORMAL, 0, {-32000,-32000}, {-1,-1}, FALSE },
10885 /* 34 */ { SW_NORMALNA, FALSE, 0, WmEmptySeq, /* what does this mean?! */
10886            SW_SHOWNORMAL, 0, {-32000,-32000}, {-1,-1}, FALSE },
10887 /* 35 */ { SW_NORMALNA, FALSE, 0, WmEmptySeq,
10888            SW_SHOWNORMAL, 0, {-32000,-32000}, {-1,-1}, FALSE },
10889 /* 36 */ { SW_HIDE, FALSE, 0, WmEmptySeq,
10890            SW_SHOWNORMAL, 0, {-32000,-32000}, {-1,-1}, FALSE },
10891 /* 37 */ { SW_RESTORE, FALSE, WS_VISIBLE, WmRestore_2,
10892            SW_SHOWNORMAL, 0, {-32000,-32000}, {-1,-1}, FALSE },
10893 /* 38 */ { SW_RESTORE, TRUE, WS_VISIBLE, WmEmptySeq,
10894            SW_SHOWNORMAL, 0, {-32000,-32000}, {-1,-1}, FALSE },
10895 /* 39 */ { SW_SHOWNOACTIVATE, TRUE, WS_VISIBLE, WmEmptySeq,
10896            SW_SHOWNORMAL, 0, {-32000,-32000}, {-1,-1}, FALSE },
10897 /* 40 */ { SW_MINIMIZE, TRUE, WS_VISIBLE|WS_MINIMIZE, WmMinimize_2,
10898            SW_SHOWMINIMIZED, 0, {-32000,-32000}, {-1,-1}, FALSE },
10899 /* 41 */ { SW_MINIMIZE, TRUE, WS_VISIBLE|WS_MINIMIZE, WmMinMax_3,
10900            SW_SHOWMINIMIZED, 0, {-32000,-32000}, {-1,-1}, FALSE },
10901 /* 42 */ { SW_SHOWMAXIMIZED, TRUE, WS_VISIBLE|WS_MAXIMIZE, WmShowMaximized_2,
10902            SW_SHOWMAXIMIZED, WPF_RESTORETOMAXIMIZED, {-32000,-32000}, {-1,-1}, FALSE },
10903 /* 43 */ { SW_SHOWMAXIMIZED, TRUE, WS_VISIBLE|WS_MAXIMIZE, WmMinMax_2,
10904            SW_SHOWMAXIMIZED, WPF_RESTORETOMAXIMIZED, {-32000,-32000}, {-1,-1}, FALSE },
10905 /* 44 */ { SW_MINIMIZE, TRUE, WS_VISIBLE|WS_MINIMIZE, WmMinimize_1,
10906            SW_SHOWMINIMIZED, WPF_RESTORETOMAXIMIZED, {-32000,-32000}, {-1,-1}, FALSE },
10907 /* 45 */ { SW_MINIMIZE, TRUE, WS_VISIBLE|WS_MINIMIZE, WmMinMax_3,
10908            SW_SHOWMINIMIZED, WPF_RESTORETOMAXIMIZED, {-32000,-32000}, {-1,-1}, FALSE },
10909 /* 46 */ { SW_RESTORE, TRUE, WS_VISIBLE|WS_MAXIMIZE, WmRestore_3,
10910            SW_SHOWMAXIMIZED, WPF_RESTORETOMAXIMIZED, {-32000,-32000}, {-1,-1}, FALSE },
10911 /* 47 */ { SW_RESTORE, TRUE, WS_VISIBLE, WmRestore_4,
10912            SW_SHOWNORMAL, WPF_RESTORETOMAXIMIZED, {-32000,-32000}, {-1,-1}, FALSE },
10913 /* 48 */ { SW_SHOWMAXIMIZED, TRUE, WS_VISIBLE|WS_MAXIMIZE, WmShowMaximized_3,
10914            SW_SHOWMAXIMIZED, WPF_RESTORETOMAXIMIZED, {-32000,-32000}, {-1,-1}, FALSE },
10915 /* 49 */ { SW_SHOW, TRUE, WS_VISIBLE|WS_MAXIMIZE, WmEmptySeq,
10916            SW_SHOWMAXIMIZED, WPF_RESTORETOMAXIMIZED, {-32000,-32000}, {-1,-1}, FALSE },
10917 /* 50 */ { SW_SHOWNORMAL, TRUE, WS_VISIBLE, WmRestore_5,
10918            SW_SHOWNORMAL, WPF_RESTORETOMAXIMIZED, {-32000,-32000}, {-1,-1}, FALSE },
10919 /* 51 */ { SW_SHOWNORMAL, TRUE, WS_VISIBLE, WmRestore_5,
10920            SW_SHOWNORMAL, WPF_RESTORETOMAXIMIZED, {-32000,-32000}, {-1,-1}, FALSE },
10921 /* 52 */ { SW_HIDE, TRUE, 0, WmHide_1,
10922            SW_SHOWNORMAL, WPF_RESTORETOMAXIMIZED, {-32000,-32000}, {-1,-1}, FALSE },
10923 /* 53 */ { SW_HIDE, FALSE, 0, WmEmptySeq,
10924            SW_SHOWNORMAL, WPF_RESTORETOMAXIMIZED, {-32000,-32000}, {-1,-1}, FALSE },
10925 /* 54 */ { SW_MINIMIZE, FALSE, WS_VISIBLE|WS_MINIMIZE, WmMinimize_3,
10926            SW_SHOWMINIMIZED, 0, {-32000,-32000}, {-1,-1}, FALSE },
10927 /* 55 */ { SW_HIDE, TRUE, WS_MINIMIZE, WmHide_2,
10928            SW_SHOWMINIMIZED, 0, {-32000,-32000}, {-1,-1}, FALSE },
10929 /* 56 */ { SW_SHOWNOACTIVATE, FALSE, WS_VISIBLE, WmShowNoActivate_2,
10930            SW_SHOWNORMAL, 0, {-32000,-32000}, {-1,-1}, FALSE },
10931 /* 57 */ { SW_SHOW, TRUE, WS_VISIBLE, WmEmptySeq,
10932            SW_SHOWNORMAL, 0, {-32000,-32000}, {-1,-1}, FALSE }
10933     };
10934     HWND hwnd;
10935     DWORD style;
10936     LPARAM ret;
10937     INT i;
10938     WINDOWPLACEMENT wp;
10939     RECT win_rc, work_rc = {0, 0, 0, 0};
10940
10941 #define WS_BASE (WS_CAPTION|WS_SYSMENU|WS_MINIMIZEBOX|WS_MAXIMIZEBOX|WS_POPUP|WS_CLIPSIBLINGS)
10942     hwnd = CreateWindowEx(0, "ShowWindowClass", NULL, WS_BASE,
10943                           120, 120, 90, 90,
10944                           0, 0, 0, NULL);
10945     assert(hwnd);
10946
10947     style = GetWindowLong(hwnd, GWL_STYLE) & ~WS_BASE;
10948     ok(style == 0, "expected style 0, got %08x\n", style);
10949
10950     flush_events();
10951     flush_sequence();
10952
10953     if (pGetMonitorInfoA && pMonitorFromPoint)
10954     {
10955         HMONITOR hmon;
10956         MONITORINFO mi;
10957         POINT pt = {0, 0};
10958
10959         SetLastError(0xdeadbeef);
10960         hmon = pMonitorFromPoint(pt, MONITOR_DEFAULTTOPRIMARY);
10961         ok(hmon != 0, "MonitorFromPoint error %u\n", GetLastError());
10962
10963         mi.cbSize = sizeof(mi);
10964         SetLastError(0xdeadbeef);
10965         ret = pGetMonitorInfoA(hmon, &mi);
10966         ok(ret, "GetMonitorInfo error %u\n", GetLastError());
10967         trace("monitor (%d,%d-%d,%d), work (%d,%d-%d,%d)\n",
10968             mi.rcMonitor.left, mi.rcMonitor.top, mi.rcMonitor.right, mi.rcMonitor.bottom,
10969             mi.rcWork.left, mi.rcWork.top, mi.rcWork.right, mi.rcWork.bottom);
10970         work_rc = mi.rcWork;
10971     }
10972
10973     GetWindowRect(hwnd, &win_rc);
10974     OffsetRect(&win_rc, -work_rc.left, -work_rc.top);
10975
10976     wp.length = sizeof(wp);
10977     SetLastError(0xdeadbeaf);
10978     ret = GetWindowPlacement(hwnd, &wp);
10979     ok(ret, "GetWindowPlacement error %u\n", GetLastError());
10980     ok(wp.flags == 0, "expected 0, got %#x\n", wp.flags);
10981     ok(wp.showCmd == SW_SHOWNORMAL, "expected SW_SHOWNORMAL, got %d\n", wp.showCmd);
10982     ok(wp.ptMinPosition.x == -1 && wp.ptMinPosition.y == -1,
10983        "expected -1,-1 got %d,%d\n", wp.ptMinPosition.x, wp.ptMinPosition.y);
10984     ok(wp.ptMaxPosition.x == -1 && wp.ptMaxPosition.y == -1,
10985        "expected -1,-1 got %d,%d\n", wp.ptMaxPosition.x, wp.ptMaxPosition.y);
10986     if (work_rc.left || work_rc.top) todo_wine /* FIXME: remove once Wine is fixed */
10987     ok(EqualRect(&win_rc, &wp.rcNormalPosition),
10988        "expected %d,%d-%d,%d got %d,%d-%d,%d\n",
10989         win_rc.left, win_rc.top, win_rc.right, win_rc.bottom,
10990         wp.rcNormalPosition.left, wp.rcNormalPosition.top,
10991         wp.rcNormalPosition.right, wp.rcNormalPosition.bottom);
10992     else
10993     ok(EqualRect(&win_rc, &wp.rcNormalPosition),
10994        "expected %d,%d-%d,%d got %d,%d-%d,%d\n",
10995         win_rc.left, win_rc.top, win_rc.right, win_rc.bottom,
10996         wp.rcNormalPosition.left, wp.rcNormalPosition.top,
10997         wp.rcNormalPosition.right, wp.rcNormalPosition.bottom);
10998
10999     for (i = 0; i < sizeof(sw)/sizeof(sw[0]); i++)
11000     {
11001         static const char * const sw_cmd_name[13] =
11002         {
11003             "SW_HIDE", "SW_SHOWNORMAL", "SW_SHOWMINIMIZED", "SW_SHOWMAXIMIZED",
11004             "SW_SHOWNOACTIVATE", "SW_SHOW", "SW_MINIMIZE", "SW_SHOWMINNOACTIVE",
11005             "SW_SHOWNA", "SW_RESTORE", "SW_SHOWDEFAULT", "SW_FORCEMINIMIZE",
11006             "SW_NORMALNA" /* 0xCC */
11007         };
11008         char comment[64];
11009         INT idx; /* index into the above array of names */
11010
11011         idx = (sw[i].cmd == SW_NORMALNA) ? 12 : sw[i].cmd;
11012
11013         style = GetWindowLong(hwnd, GWL_STYLE);
11014         trace("%d: sending %s, current window style %08x\n", i+1, sw_cmd_name[idx], style);
11015         ret = ShowWindow(hwnd, sw[i].cmd);
11016         ok(!ret == !sw[i].ret, "%d: cmd %s: expected ret %lu, got %lu\n", i+1, sw_cmd_name[idx], sw[i].ret, ret);
11017         style = GetWindowLong(hwnd, GWL_STYLE) & ~WS_BASE;
11018         ok(style == sw[i].style, "%d: expected style %08x, got %08x\n", i+1, sw[i].style, style);
11019
11020         sprintf(comment, "%d: ShowWindow(%s)", i+1, sw_cmd_name[idx]);
11021         ok_sequence(sw[i].msg, comment, sw[i].todo_msg);
11022
11023         wp.length = sizeof(wp);
11024         SetLastError(0xdeadbeaf);
11025         ret = GetWindowPlacement(hwnd, &wp);
11026         ok(ret, "GetWindowPlacement error %u\n", GetLastError());
11027         ok(wp.flags == sw[i].wp_flags, "expected %#x, got %#x\n", sw[i].wp_flags, wp.flags);
11028         ok(wp.showCmd == sw[i].wp_cmd, "expected %d, got %d\n", sw[i].wp_cmd, wp.showCmd);
11029
11030         /* NT moves the minimized window to -32000,-32000, win9x to 3000,3000 */
11031         if ((wp.ptMinPosition.x + work_rc.left == -32000 && wp.ptMinPosition.y + work_rc.top == -32000) ||
11032             (wp.ptMinPosition.x + work_rc.left == 3000 && wp.ptMinPosition.y + work_rc.top == 3000))
11033         {
11034             ok((wp.ptMinPosition.x + work_rc.left == sw[i].wp_min.x && wp.ptMinPosition.y + work_rc.top == sw[i].wp_min.y) ||
11035                (wp.ptMinPosition.x + work_rc.left == 3000 && wp.ptMinPosition.y + work_rc.top == 3000),
11036                "expected %d,%d got %d,%d\n", sw[i].wp_min.x, sw[i].wp_min.y, wp.ptMinPosition.x, wp.ptMinPosition.y);
11037         }
11038         else
11039         {
11040             if (wp.ptMinPosition.x != sw[i].wp_min.x || wp.ptMinPosition.y != sw[i].wp_min.y)
11041             todo_wine
11042             ok(wp.ptMinPosition.x == sw[i].wp_min.x && wp.ptMinPosition.y == sw[i].wp_min.y,
11043                "expected %d,%d got %d,%d\n", sw[i].wp_min.x, sw[i].wp_min.y, wp.ptMinPosition.x, wp.ptMinPosition.y);
11044             else
11045             ok(wp.ptMinPosition.x == sw[i].wp_min.x && wp.ptMinPosition.y == sw[i].wp_min.y,
11046                "expected %d,%d got %d,%d\n", sw[i].wp_min.x, sw[i].wp_min.y, wp.ptMinPosition.x, wp.ptMinPosition.y);
11047         }
11048
11049         if (wp.ptMaxPosition.x != sw[i].wp_max.x || wp.ptMaxPosition.y != sw[i].wp_max.y)
11050         todo_wine
11051         ok(wp.ptMaxPosition.x == sw[i].wp_max.x && wp.ptMaxPosition.y == sw[i].wp_max.y,
11052            "expected %d,%d got %d,%d\n", sw[i].wp_max.x, sw[i].wp_max.y, wp.ptMaxPosition.x, wp.ptMaxPosition.y);
11053         else
11054         ok(wp.ptMaxPosition.x == sw[i].wp_max.x && wp.ptMaxPosition.y == sw[i].wp_max.y,
11055            "expected %d,%d got %d,%d\n", sw[i].wp_max.x, sw[i].wp_max.y, wp.ptMaxPosition.x, wp.ptMaxPosition.y);
11056
11057 if (0) /* FIXME: Wine behaves completely different here */
11058         ok(EqualRect(&win_rc, &wp.rcNormalPosition),
11059            "expected %d,%d-%d,%d got %d,%d-%d,%d\n",
11060             win_rc.left, win_rc.top, win_rc.right, win_rc.bottom,
11061             wp.rcNormalPosition.left, wp.rcNormalPosition.top,
11062             wp.rcNormalPosition.right, wp.rcNormalPosition.bottom);
11063
11064         flush_events();
11065         flush_sequence();
11066     }
11067
11068     DestroyWindow(hwnd);
11069 }
11070
11071 static INT_PTR WINAPI test_dlg_proc(HWND hwnd, UINT message, WPARAM wParam, LPARAM lParam)
11072 {
11073     struct recvd_message msg;
11074
11075     if (ignore_message( message )) return 0;
11076
11077     msg.hwnd = hwnd;
11078     msg.message = message;
11079     msg.flags = sent|wparam|lparam;
11080     msg.wParam = wParam;
11081     msg.lParam = lParam;
11082     msg.descr = "dialog";
11083     add_message(&msg);
11084
11085     /* calling DefDlgProc leads to a recursion under XP */
11086
11087     switch (message)
11088     {
11089     case WM_INITDIALOG:
11090     case WM_GETDLGCODE:
11091         return 0;
11092     }
11093     return 1;
11094 }
11095
11096 static const struct message WmDefDlgSetFocus_1[] = {
11097     { WM_GETDLGCODE, sent|wparam|lparam, 0, 0 },
11098     { WM_GETTEXTLENGTH, sent|wparam|lparam|optional, 0, 0 }, /* XP */
11099     { WM_GETTEXT, sent|wparam|optional, 6 }, /* XP */
11100     { WM_GETTEXT, sent|wparam|optional, 12 }, /* XP */
11101     { EM_SETSEL, sent|wparam, 0 }, /* XP sets lparam to text length, Win9x to -2 */
11102     { HCBT_SETFOCUS, hook },
11103     { WM_IME_SETCONTEXT, sent|wparam|optional, 1 },
11104     { WM_IME_NOTIFY, sent|wparam|defwinproc|optional, 2 },
11105     { EVENT_OBJECT_FOCUS, winevent_hook|wparam|lparam, OBJID_CLIENT, 0 },
11106     { WM_SETFOCUS, sent|wparam, 0 },
11107     { WM_IME_NOTIFY, sent|wparam|defwinproc|optional, 10 },
11108     { WM_CTLCOLOREDIT, sent },
11109     { WM_IME_NOTIFY, sent|wparam|defwinproc|optional, 11 },
11110     { EVENT_OBJECT_CREATE, winevent_hook|wparam|lparam, OBJID_CARET, 0 },
11111     { EVENT_OBJECT_LOCATIONCHANGE, winevent_hook|wparam|lparam, OBJID_CARET, 0 },
11112     { EVENT_OBJECT_SHOW, winevent_hook|wparam|lparam, OBJID_CARET, 0 },
11113     { WM_COMMAND, sent|wparam, MAKEWPARAM(1, EN_SETFOCUS) },
11114     { 0 }
11115 };
11116 static const struct message WmDefDlgSetFocus_2[] = {
11117     { WM_GETDLGCODE, sent|wparam|lparam, 0, 0 },
11118     { WM_GETTEXTLENGTH, sent|wparam|lparam|optional, 0, 0 }, /* XP */
11119     { WM_GETTEXT, sent|wparam|optional, 6 }, /* XP */
11120     { WM_GETTEXT, sent|wparam|optional, 12 }, /* XP */
11121     { EM_SETSEL, sent|wparam, 0 }, /* XP sets lparam to text length, Win9x to -2 */
11122     { EVENT_OBJECT_HIDE, winevent_hook|wparam|lparam, OBJID_CARET, 0 },
11123     { WM_CTLCOLOREDIT, sent|optional }, /* XP */
11124     { EVENT_OBJECT_SHOW, winevent_hook|wparam|lparam, OBJID_CARET, 0 },
11125     { 0 }
11126 };
11127 /* Creation of a dialog */
11128 static const struct message WmCreateDialogParamSeq_1[] = {
11129     { HCBT_CREATEWND, hook },
11130     { WM_NCCREATE, sent },
11131     { WM_NCCALCSIZE, sent|wparam, 0 },
11132     { WM_CREATE, sent },
11133     { EVENT_OBJECT_CREATE, winevent_hook|wparam|lparam, 0, 0 },
11134     { WM_SIZE, sent|wparam, SIZE_RESTORED },
11135     { WM_MOVE, sent },
11136     { WM_SETFONT, sent },
11137     { WM_INITDIALOG, sent },
11138     { WM_CHANGEUISTATE, sent|optional },
11139     { 0 }
11140 };
11141 /* Creation of a dialog */
11142 static const struct message WmCreateDialogParamSeq_2[] = {
11143     { HCBT_CREATEWND, hook },
11144     { WM_NCCREATE, sent },
11145     { WM_NCCALCSIZE, sent|wparam, 0 },
11146     { WM_CREATE, sent },
11147     { EVENT_OBJECT_CREATE, winevent_hook|wparam|lparam, 0, 0 },
11148     { WM_SIZE, sent|wparam, SIZE_RESTORED },
11149     { WM_MOVE, sent },
11150     { WM_CHANGEUISTATE, sent|optional },
11151     { 0 }
11152 };
11153
11154 static void test_dialog_messages(void)
11155 {
11156     WNDCLASS cls;
11157     HWND hdlg, hedit1, hedit2, hfocus;
11158     LRESULT ret;
11159
11160 #define set_selection(hctl, start, end) \
11161     ret = SendMessage(hctl, EM_SETSEL, start, end); \
11162     ok(ret == 1, "EM_SETSEL returned %ld\n", ret);
11163
11164 #define check_selection(hctl, start, end) \
11165     ret = SendMessage(hctl, EM_GETSEL, 0, 0); \
11166     ok(ret == MAKELRESULT(start, end), "wrong selection (%d - %d)\n", LOWORD(ret), HIWORD(ret));
11167
11168     subclass_edit();
11169
11170     hdlg = CreateWindowEx(WS_EX_DLGMODALFRAME, "TestDialogClass", NULL,
11171                           WS_VISIBLE|WS_CAPTION|WS_SYSMENU|WS_DLGFRAME,
11172                           0, 0, 100, 100, 0, 0, 0, NULL);
11173     ok(hdlg != 0, "Failed to create custom dialog window\n");
11174
11175     hedit1 = CreateWindowEx(0, "my_edit_class", NULL,
11176                            WS_CHILD|WS_BORDER|WS_VISIBLE|WS_TABSTOP,
11177                            0, 0, 80, 20, hdlg, (HMENU)1, 0, NULL);
11178     ok(hedit1 != 0, "Failed to create edit control\n");
11179     hedit2 = CreateWindowEx(0, "my_edit_class", NULL,
11180                            WS_CHILD|WS_BORDER|WS_VISIBLE|WS_TABSTOP,
11181                            0, 40, 80, 20, hdlg, (HMENU)2, 0, NULL);
11182     ok(hedit2 != 0, "Failed to create edit control\n");
11183
11184     SendMessage(hedit1, WM_SETTEXT, 0, (LPARAM)"hello");
11185     SendMessage(hedit2, WM_SETTEXT, 0, (LPARAM)"bye");
11186
11187     hfocus = GetFocus();
11188     ok(hfocus == hdlg, "wrong focus %p\n", hfocus);
11189
11190     SetFocus(hedit2);
11191     hfocus = GetFocus();
11192     ok(hfocus == hedit2, "wrong focus %p\n", hfocus);
11193
11194     check_selection(hedit1, 0, 0);
11195     check_selection(hedit2, 0, 0);
11196
11197     set_selection(hedit2, 0, -1);
11198     check_selection(hedit2, 0, 3);
11199
11200     SetFocus(0);
11201     hfocus = GetFocus();
11202     ok(hfocus == 0, "wrong focus %p\n", hfocus);
11203
11204     flush_sequence();
11205     ret = DefDlgProc(hdlg, WM_SETFOCUS, 0, 0);
11206     ok(ret == 0, "WM_SETFOCUS returned %ld\n", ret);
11207     ok_sequence(WmDefDlgSetFocus_1, "DefDlgProc(WM_SETFOCUS) 1", FALSE);
11208
11209     hfocus = GetFocus();
11210     ok(hfocus == hedit1, "wrong focus %p\n", hfocus);
11211
11212     check_selection(hedit1, 0, 5);
11213     check_selection(hedit2, 0, 3);
11214
11215     flush_sequence();
11216     ret = DefDlgProc(hdlg, WM_SETFOCUS, 0, 0);
11217     ok(ret == 0, "WM_SETFOCUS returned %ld\n", ret);
11218     ok_sequence(WmDefDlgSetFocus_2, "DefDlgProc(WM_SETFOCUS) 2", FALSE);
11219
11220     hfocus = GetFocus();
11221     ok(hfocus == hedit1, "wrong focus %p\n", hfocus);
11222
11223     check_selection(hedit1, 0, 5);
11224     check_selection(hedit2, 0, 3);
11225
11226     EndDialog(hdlg, 0);
11227     DestroyWindow(hedit1);
11228     DestroyWindow(hedit2);
11229     DestroyWindow(hdlg);
11230     flush_sequence();
11231
11232 #undef set_selection
11233 #undef check_selection
11234
11235     ok(GetClassInfo(0, "#32770", &cls), "GetClassInfo failed\n");
11236     cls.lpszClassName = "MyDialogClass";
11237     cls.hInstance = GetModuleHandle(0);
11238     /* need a cast since a dlgproc is used as a wndproc */
11239     cls.lpfnWndProc = (WNDPROC)test_dlg_proc;
11240     if (!RegisterClass(&cls)) assert(0);
11241
11242     hdlg = CreateDialogParam(0, "CLASS_TEST_DIALOG_2", 0, test_dlg_proc, 0);
11243     ok(IsWindow(hdlg), "CreateDialogParam failed\n");
11244     ok_sequence(WmCreateDialogParamSeq_1, "CreateDialogParam_1", FALSE);
11245     EndDialog(hdlg, 0);
11246     DestroyWindow(hdlg);
11247     flush_sequence();
11248
11249     hdlg = CreateDialogParam(0, "CLASS_TEST_DIALOG_2", 0, NULL, 0);
11250     ok(IsWindow(hdlg), "CreateDialogParam failed\n");
11251     ok_sequence(WmCreateDialogParamSeq_2, "CreateDialogParam_2", FALSE);
11252     EndDialog(hdlg, 0);
11253     DestroyWindow(hdlg);
11254     flush_sequence();
11255
11256     UnregisterClass(cls.lpszClassName, cls.hInstance);
11257 }
11258
11259 static void test_nullCallback(void)
11260 {
11261     HWND hwnd;
11262
11263     hwnd = CreateWindowExA(0, "TestWindowClass", "Test overlapped", WS_OVERLAPPEDWINDOW,
11264                            100, 100, 200, 200, 0, 0, 0, NULL);
11265     ok (hwnd != 0, "Failed to create overlapped window\n");
11266
11267     SendMessageCallbackA(hwnd,WM_NULL,0,0,NULL,0);
11268     flush_events();
11269     DestroyWindow(hwnd);
11270 }
11271
11272 /* SetActiveWindow( 0 ) hwnd visible */
11273 static const struct message SetActiveWindowSeq0[] =
11274 {
11275     { HCBT_ACTIVATE, hook|optional },
11276     { WM_NCACTIVATE, sent|wparam, 0 },
11277     { WM_GETTEXT, sent|defwinproc|optional },
11278     { WM_ACTIVATE, sent|wparam, 0 },
11279     { WM_ACTIVATEAPP, sent|wparam|optional, 0 },
11280     { WM_ACTIVATEAPP, sent|wparam|optional, 0 },
11281     { WM_QUERYNEWPALETTE, sent|wparam|lparam|optional, 0, 0 },
11282     { WM_KILLFOCUS, sent|optional },
11283     { WM_WINDOWPOSCHANGING, sent|wparam|optional, SWP_NOSIZE|SWP_NOMOVE|SWP_NOACTIVATE },
11284     { WM_WINDOWPOSCHANGING, sent|wparam|optional, SWP_NOSIZE|SWP_NOMOVE },
11285     { WM_NCACTIVATE, sent|wparam|optional, 1 },
11286     { WM_GETTEXT, sent|defwinproc|optional },
11287     { WM_ACTIVATE, sent|wparam|optional, 1 },
11288     { HCBT_SETFOCUS, hook|optional },
11289     { WM_KILLFOCUS, sent|defwinproc|optional },
11290     { WM_IME_SETCONTEXT, sent|defwinproc|optional },
11291     { WM_IME_SETCONTEXT, sent|defwinproc|optional },
11292     { WM_IME_SETCONTEXT, sent|optional },
11293     { WM_IME_SETCONTEXT, sent|optional },
11294     { WM_IME_NOTIFY, sent|wparam|defwinproc|optional, 1 },
11295     { WM_IME_NOTIFY, sent|wparam|defwinproc|optional, 2 },
11296     { WM_SETFOCUS, sent|defwinproc|optional },
11297     { WM_GETTEXT, sent|optional },
11298     { 0 }
11299 };
11300 /* SetActiveWindow( hwnd ) hwnd visible */
11301 static const struct message SetActiveWindowSeq1[] =
11302 {
11303     { WM_QUERYNEWPALETTE, sent|wparam|lparam|optional, 0, 0 },
11304     { 0 }
11305 };
11306 /* SetActiveWindow( popup ) hwnd visible, popup visible */
11307 static const struct message SetActiveWindowSeq2[] =
11308 {
11309     { HCBT_ACTIVATE, hook },
11310     { WM_NCACTIVATE, sent|wparam, 0 },
11311     { WM_GETTEXT, sent|defwinproc|optional },
11312     { WM_ACTIVATE, sent|wparam, 0 },
11313     { WM_QUERYNEWPALETTE, sent|wparam|lparam|optional, 0, 0 },
11314     { WM_WINDOWPOSCHANGING, sent|wparam|optional, SWP_NOSIZE|SWP_NOMOVE },
11315     { WM_WINDOWPOSCHANGING, sent|wparam|optional, SWP_NOSIZE|SWP_NOMOVE|SWP_NOACTIVATE },
11316     { WM_NCPAINT, sent|optional },
11317     { WM_GETTEXT, sent|defwinproc|optional },
11318     { WM_ERASEBKGND, sent|optional },
11319     { WM_WINDOWPOSCHANGED, sent|wparam|optional, SWP_NOSIZE|SWP_NOMOVE|SWP_NOCLIENTSIZE|SWP_NOCLIENTMOVE },
11320     { WM_NCACTIVATE, sent|wparam, 1 },
11321     { WM_GETTEXT, sent|defwinproc|optional },
11322     { WM_ACTIVATE, sent|wparam, 1 },
11323     { HCBT_SETFOCUS, hook },
11324     { WM_KILLFOCUS, sent|defwinproc },
11325     { WM_IME_SETCONTEXT, sent|wparam|defwinproc|optional, 0 },
11326     { WM_IME_SETCONTEXT, sent|wparam|defwinproc|optional, 1 },
11327     { WM_IME_NOTIFY, sent|wparam|defwinproc|optional, 1 },
11328     { WM_IME_NOTIFY, sent|wparam|defwinproc|optional, 2 },
11329     { WM_SETFOCUS, sent|defwinproc },
11330     { WM_GETTEXT, sent|optional },
11331     { 0 }
11332 };
11333
11334 /* SetActiveWindow( hwnd ) hwnd not visible */
11335 static const struct message SetActiveWindowSeq3[] =
11336 {
11337     { HCBT_ACTIVATE, hook },
11338     { WM_QUERYNEWPALETTE, sent|wparam|lparam|optional, 0, 0 },
11339     { WM_WINDOWPOSCHANGING, sent|wparam, SWP_NOSIZE|SWP_NOMOVE|SWP_NOACTIVATE },
11340     { WM_WINDOWPOSCHANGING, sent|wparam, SWP_NOSIZE|SWP_NOMOVE },
11341     { WM_WINDOWPOSCHANGED, sent|wparam|optional, SWP_NOSIZE|SWP_NOMOVE|SWP_NOREDRAW|SWP_NOACTIVATE|SWP_NOCLIENTSIZE|SWP_NOCLIENTMOVE },
11342     { WM_WINDOWPOSCHANGED, sent|wparam|optional, SWP_NOSIZE|SWP_NOMOVE|SWP_NOREDRAW|SWP_NOCLIENTSIZE|SWP_NOCLIENTMOVE },
11343     { WM_ACTIVATEAPP, sent|wparam, 1 },
11344     { WM_ACTIVATEAPP, sent|wparam, 1 },
11345     { WM_NCACTIVATE, sent|wparam, 1 },
11346     { WM_ACTIVATE, sent|wparam, 1 },
11347     { HCBT_SETFOCUS, hook },
11348     { WM_IME_SETCONTEXT, sent|wparam|defwinproc|optional, 1 },
11349     { WM_IME_NOTIFY, sent|wparam|defwinproc|optional, 2 },
11350     { WM_SETFOCUS, sent|defwinproc },
11351     { 0 }
11352 };
11353 /* SetActiveWindow( popup ) hwnd not visible, popup not visible */
11354 static const struct message SetActiveWindowSeq4[] =
11355 {
11356     { HCBT_ACTIVATE, hook },
11357     { WM_NCACTIVATE, sent|wparam, 0 },
11358     { WM_GETTEXT, sent|defwinproc|optional },
11359     { WM_ACTIVATE, sent|wparam, 0 },
11360     { WM_QUERYNEWPALETTE, sent|wparam|lparam|optional, 0, 0 },
11361     { WM_WINDOWPOSCHANGING, sent|wparam, SWP_NOSIZE|SWP_NOMOVE },
11362     { WM_WINDOWPOSCHANGING, sent|wparam, SWP_NOSIZE|SWP_NOMOVE|SWP_NOACTIVATE },
11363     { WM_WINDOWPOSCHANGED, sent|wparam|optional, SWP_NOSIZE|SWP_NOMOVE|SWP_NOREDRAW|SWP_NOCLIENTSIZE|SWP_NOCLIENTMOVE },
11364     { WM_WINDOWPOSCHANGED, sent|wparam|optional, SWP_NOSIZE|SWP_NOMOVE|SWP_NOACTIVATE|SWP_NOREDRAW|SWP_NOCLIENTSIZE|SWP_NOCLIENTMOVE },
11365     { WM_NCACTIVATE, sent|wparam, 1 },
11366     { WM_GETTEXT, sent|defwinproc|optional },
11367     { WM_ACTIVATE, sent|wparam, 1 },
11368     { HCBT_SETFOCUS, hook },
11369     { WM_KILLFOCUS, sent|defwinproc },
11370     { WM_IME_SETCONTEXT, sent|wparam|defwinproc|optional, 0 },
11371     { WM_IME_SETCONTEXT, sent|wparam|defwinproc|optional, 1 },
11372     { WM_IME_NOTIFY, sent|wparam|defwinproc|optional, 1 },
11373     { WM_IME_NOTIFY, sent|wparam|defwinproc|optional, 2 },
11374     { WM_SETFOCUS, sent|defwinproc },
11375     { 0 }
11376 };
11377
11378
11379 static void test_SetActiveWindow(void)
11380 {
11381     HWND hwnd, popup, ret;
11382
11383     hwnd = CreateWindowExA(0, "TestWindowClass", "Test SetActiveWindow",
11384                            WS_OVERLAPPEDWINDOW | WS_VISIBLE,
11385                            100, 100, 200, 200, 0, 0, 0, NULL);
11386
11387     popup = CreateWindowExA(0, "TestWindowClass", "Test SetActiveWindow",
11388                            WS_OVERLAPPEDWINDOW | WS_VISIBLE | WS_POPUP,
11389                            100, 100, 200, 200, hwnd, 0, 0, NULL);
11390
11391     ok(hwnd != 0, "Failed to create overlapped window\n");
11392     ok(popup != 0, "Failed to create popup window\n");
11393     SetForegroundWindow( popup );
11394     flush_sequence();
11395
11396     trace("SetActiveWindow(0)\n");
11397     ret = SetActiveWindow(0);
11398     ok( ret == popup, "Failed to SetActiveWindow(0)\n");
11399     ok_sequence(SetActiveWindowSeq0, "SetActiveWindow(0)", FALSE);
11400     flush_sequence();
11401
11402     trace("SetActiveWindow(hwnd), hwnd visible\n");
11403     ret = SetActiveWindow(hwnd);
11404     if (ret == hwnd) ok_sequence(SetActiveWindowSeq1, "SetActiveWindow(hwnd), hwnd visible", TRUE);
11405     flush_sequence();
11406
11407     trace("SetActiveWindow(popup), hwnd visible, popup visible\n");
11408     ret = SetActiveWindow(popup);
11409     ok( ret == hwnd, "Failed to SetActiveWindow(popup), popup visible\n");
11410     ok_sequence(SetActiveWindowSeq2, "SetActiveWindow(popup), hwnd visible, popup visible", FALSE);
11411     flush_sequence();
11412
11413     ShowWindow(hwnd, SW_HIDE);
11414     ShowWindow(popup, SW_HIDE);
11415     flush_sequence();
11416
11417     trace("SetActiveWindow(hwnd), hwnd not visible\n");
11418     ret = SetActiveWindow(hwnd);
11419     ok( ret == NULL, "SetActiveWindow(hwnd), hwnd not visible, previous is %p\n", ret );
11420     ok_sequence(SetActiveWindowSeq3, "SetActiveWindow(hwnd), hwnd not visible", TRUE);
11421     flush_sequence();
11422
11423     trace("SetActiveWindow(popup), hwnd not visible, popup not visible\n");
11424     ret = SetActiveWindow(popup);
11425     ok( ret == hwnd, "Failed to SetActiveWindow(popup)\n");
11426     ok_sequence(SetActiveWindowSeq4, "SetActiveWindow(popup), hwnd not visible, popup not visible", TRUE);
11427     flush_sequence();
11428
11429     trace("done\n");
11430
11431     DestroyWindow(hwnd);
11432 }
11433
11434 static const struct message SetForegroundWindowSeq[] =
11435 {
11436     { WM_NCACTIVATE, sent|wparam, 0 },
11437     { WM_GETTEXT, sent|defwinproc|optional },
11438     { WM_ACTIVATE, sent|wparam, 0 },
11439     { WM_ACTIVATEAPP, sent|wparam, 0 },
11440     { WM_KILLFOCUS, sent },
11441     { WM_IME_SETCONTEXT, sent|wparam|optional, 0 },
11442     { WM_IME_NOTIFY, sent|wparam|optional|defwinproc, 1 },
11443     { 0 }
11444 };
11445
11446 static void test_SetForegroundWindow(void)
11447 {
11448     HWND hwnd;
11449
11450     hwnd = CreateWindowExA(0, "TestWindowClass", "Test SetForegroundWindow",
11451                            WS_OVERLAPPEDWINDOW | WS_VISIBLE,
11452                            100, 100, 200, 200, 0, 0, 0, NULL);
11453     ok (hwnd != 0, "Failed to create overlapped window\n");
11454     SetForegroundWindow( hwnd );
11455     flush_sequence();
11456
11457     trace("SetForegroundWindow( 0 )\n");
11458     SetForegroundWindow( 0 );
11459     ok_sequence(WmEmptySeq, "SetForegroundWindow( 0 ) away from foreground top level window", FALSE);
11460     trace("SetForegroundWindow( GetDesktopWindow() )\n");
11461     SetForegroundWindow( GetDesktopWindow() );
11462     ok_sequence(SetForegroundWindowSeq, "SetForegroundWindow( desktop ) away from "
11463                                         "foreground top level window", FALSE);
11464     trace("done\n");
11465
11466     DestroyWindow(hwnd);
11467 }
11468
11469 static void test_dbcs_wm_char(void)
11470 {
11471     BYTE dbch[2];
11472     WCHAR wch, bad_wch;
11473     HWND hwnd, hwnd2;
11474     MSG msg;
11475     DWORD time;
11476     POINT pt;
11477     DWORD_PTR res;
11478     CPINFOEXA cpinfo;
11479     UINT i, j, k;
11480     struct message wmCharSeq[2];
11481
11482     if (!pGetCPInfoExA)
11483     {
11484         win_skip("GetCPInfoExA is not available\n");
11485         return;
11486     }
11487
11488     pGetCPInfoExA( CP_ACP, 0, &cpinfo );
11489     if (cpinfo.MaxCharSize != 2)
11490     {
11491         skip( "Skipping DBCS WM_CHAR test in SBCS codepage '%s'\n", cpinfo.CodePageName );
11492         return;
11493     }
11494
11495     dbch[0] = dbch[1] = 0;
11496     wch = 0;
11497     bad_wch = cpinfo.UnicodeDefaultChar;
11498     for (i = 0; !wch && i < MAX_LEADBYTES && cpinfo.LeadByte[i]; i += 2)
11499         for (j = cpinfo.LeadByte[i]; !wch && j <= cpinfo.LeadByte[i+1]; j++)
11500             for (k = 128; k <= 255; k++)
11501             {
11502                 char str[2];
11503                 WCHAR wstr[2];
11504                 str[0] = j;
11505                 str[1] = k;
11506                 if (MultiByteToWideChar( CP_ACP, 0, str, 2, wstr, 2 ) == 1 &&
11507                     WideCharToMultiByte( CP_ACP, 0, wstr, 1, str, 2, NULL, NULL ) == 2 &&
11508                     (BYTE)str[0] == j && (BYTE)str[1] == k &&
11509                     HIBYTE(wstr[0]) && HIBYTE(wstr[0]) != 0xff)
11510                 {
11511                     dbch[0] = j;
11512                     dbch[1] = k;
11513                     wch = wstr[0];
11514                     break;
11515                 }
11516             }
11517
11518     if (!wch)
11519     {
11520         skip( "Skipping DBCS WM_CHAR test, no appropriate char found\n" );
11521         return;
11522     }
11523     trace( "using dbcs char %02x,%02x wchar %04x bad wchar %04x codepage '%s'\n",
11524            dbch[0], dbch[1], wch, bad_wch, cpinfo.CodePageName );
11525
11526     hwnd = CreateWindowExW(0, testWindowClassW, NULL,
11527                            WS_OVERLAPPEDWINDOW, 100, 100, 200, 200, 0, 0, 0, NULL);
11528     hwnd2 = CreateWindowExW(0, testWindowClassW, NULL,
11529                            WS_OVERLAPPEDWINDOW, 100, 100, 200, 200, 0, 0, 0, NULL);
11530     ok (hwnd != 0, "Failed to create overlapped window\n");
11531     ok (hwnd2 != 0, "Failed to create overlapped window\n");
11532     flush_sequence();
11533
11534     memset( wmCharSeq, 0, sizeof(wmCharSeq) );
11535     wmCharSeq[0].message = WM_CHAR;
11536     wmCharSeq[0].flags = sent|wparam;
11537     wmCharSeq[0].wParam = wch;
11538
11539     /* posted message */
11540     PostMessageA( hwnd, WM_CHAR, dbch[0], 0 );
11541     ok( !PeekMessageW( &msg, hwnd, 0, 0, PM_REMOVE ), "got message %x\n", msg.message );
11542     PostMessageA( hwnd, WM_CHAR, dbch[1], 0 );
11543     ok( PeekMessageW( &msg, hwnd, 0, 0, PM_REMOVE ), "no message\n" );
11544     ok( msg.message == WM_CHAR, "unexpected message %x\n", msg.message );
11545     ok( msg.wParam == wch, "bad wparam %lx/%x\n", msg.wParam, wch );
11546     ok( !PeekMessageW( &msg, hwnd, 0, 0, PM_REMOVE ), "got message %x\n", msg.message );
11547
11548     /* posted thread message */
11549     PostThreadMessageA( GetCurrentThreadId(), WM_CHAR, dbch[0], 0 );
11550     ok( !PeekMessageW( &msg, hwnd, 0, 0, PM_REMOVE ), "got message %x\n", msg.message );
11551     PostMessageA( hwnd, WM_CHAR, dbch[1], 0 );
11552     ok( PeekMessageW( &msg, hwnd, 0, 0, PM_REMOVE ), "no message\n" );
11553     ok( msg.message == WM_CHAR, "unexpected message %x\n", msg.message );
11554     ok( msg.wParam == wch, "bad wparam %lx/%x\n", msg.wParam, wch );
11555     ok( !PeekMessageW( &msg, hwnd, 0, 0, PM_REMOVE ), "got message %x\n", msg.message );
11556
11557     /* sent message */
11558     flush_sequence();
11559     SendMessageA( hwnd, WM_CHAR, dbch[0], 0 );
11560     ok_sequence( WmEmptySeq, "no messages", FALSE );
11561     SendMessageA( hwnd, WM_CHAR, dbch[1], 0 );
11562     ok_sequence( wmCharSeq, "Unicode WM_CHAR", FALSE );
11563     ok( !PeekMessageW( &msg, hwnd, 0, 0, PM_REMOVE ), "got message %x\n", msg.message );
11564
11565     /* sent message with timeout */
11566     flush_sequence();
11567     SendMessageTimeoutA( hwnd, WM_CHAR, dbch[0], 0, SMTO_NORMAL, 0, &res );
11568     ok_sequence( WmEmptySeq, "no messages", FALSE );
11569     SendMessageTimeoutA( hwnd, WM_CHAR, dbch[1], 0, SMTO_NORMAL, 0, &res );
11570     ok_sequence( wmCharSeq, "Unicode WM_CHAR", FALSE );
11571     ok( !PeekMessageW( &msg, hwnd, 0, 0, PM_REMOVE ), "got message %x\n", msg.message );
11572
11573     /* sent message with timeout and callback */
11574     flush_sequence();
11575     SendMessageTimeoutA( hwnd, WM_CHAR, dbch[0], 0, SMTO_NORMAL, 0, &res );
11576     ok_sequence( WmEmptySeq, "no messages", FALSE );
11577     SendMessageCallbackA( hwnd, WM_CHAR, dbch[1], 0, NULL, 0 );
11578     ok_sequence( wmCharSeq, "Unicode WM_CHAR", FALSE );
11579     ok( !PeekMessageW( &msg, hwnd, 0, 0, PM_REMOVE ), "got message %x\n", msg.message );
11580
11581     /* sent message with callback */
11582     flush_sequence();
11583     SendNotifyMessageA( hwnd, WM_CHAR, dbch[0], 0 );
11584     ok_sequence( WmEmptySeq, "no messages", FALSE );
11585     SendMessageCallbackA( hwnd, WM_CHAR, dbch[1], 0, NULL, 0 );
11586     ok_sequence( wmCharSeq, "Unicode WM_CHAR", FALSE );
11587     ok( !PeekMessageW( &msg, hwnd, 0, 0, PM_REMOVE ), "got message %x\n", msg.message );
11588
11589     /* direct window proc call */
11590     flush_sequence();
11591     CallWindowProcA( (WNDPROC)GetWindowLongPtrA( hwnd, GWLP_WNDPROC ), hwnd, WM_CHAR, dbch[0], 0 );
11592     ok_sequence( WmEmptySeq, "no messages", FALSE );
11593     CallWindowProcA( (WNDPROC)GetWindowLongPtrA( hwnd, GWLP_WNDPROC ), hwnd, WM_CHAR, dbch[1], 0 );
11594     ok_sequence( wmCharSeq, "Unicode WM_CHAR", FALSE );
11595
11596     /* dispatch message */
11597     msg.hwnd = hwnd;
11598     msg.message = WM_CHAR;
11599     msg.wParam = dbch[0];
11600     msg.lParam = 0;
11601     DispatchMessageA( &msg );
11602     ok_sequence( WmEmptySeq, "no messages", FALSE );
11603     msg.wParam = dbch[1];
11604     DispatchMessageA( &msg );
11605     ok_sequence( wmCharSeq, "Unicode WM_CHAR", FALSE );
11606
11607     /* window handle is irrelevant */
11608     flush_sequence();
11609     SendMessageA( hwnd2, WM_CHAR, dbch[0], 0 );
11610     ok_sequence( WmEmptySeq, "no messages", FALSE );
11611     SendMessageA( hwnd, WM_CHAR, dbch[1], 0 );
11612     ok_sequence( wmCharSeq, "Unicode WM_CHAR", FALSE );
11613     ok( !PeekMessageW( &msg, hwnd, 0, 0, PM_REMOVE ), "got message %x\n", msg.message );
11614
11615     /* interleaved post and send */
11616     flush_sequence();
11617     PostMessageA( hwnd2, WM_CHAR, dbch[0], 0 );
11618     SendMessageA( hwnd2, WM_CHAR, dbch[0], 0 );
11619     ok_sequence( WmEmptySeq, "no messages", FALSE );
11620     ok( !PeekMessageW( &msg, hwnd, 0, 0, PM_REMOVE ), "got message %x\n", msg.message );
11621     PostMessageA( hwnd, WM_CHAR, dbch[1], 0 );
11622     ok_sequence( WmEmptySeq, "no messages", FALSE );
11623     ok( PeekMessageW( &msg, hwnd, 0, 0, PM_REMOVE ), "no message\n" );
11624     ok( msg.message == WM_CHAR, "unexpected message %x\n", msg.message );
11625     ok( msg.wParam == wch, "bad wparam %lx/%x\n", msg.wParam, wch );
11626     ok( !PeekMessageW( &msg, hwnd, 0, 0, PM_REMOVE ), "got message %x\n", msg.message );
11627     SendMessageA( hwnd, WM_CHAR, dbch[1], 0 );
11628     ok_sequence( wmCharSeq, "Unicode WM_CHAR", FALSE );
11629     ok( !PeekMessageW( &msg, hwnd, 0, 0, PM_REMOVE ), "got message %x\n", msg.message );
11630
11631     /* interleaved sent message and winproc */
11632     flush_sequence();
11633     SendMessageA( hwnd, WM_CHAR, dbch[0], 0 );
11634     CallWindowProcA( (WNDPROC)GetWindowLongPtrA( hwnd, GWLP_WNDPROC ), hwnd, WM_CHAR, dbch[0], 0 );
11635     ok_sequence( WmEmptySeq, "no messages", FALSE );
11636     SendMessageA( hwnd, WM_CHAR, dbch[1], 0 );
11637     ok_sequence( wmCharSeq, "Unicode WM_CHAR", FALSE );
11638     CallWindowProcA( (WNDPROC)GetWindowLongPtrA( hwnd, GWLP_WNDPROC ), hwnd, WM_CHAR, dbch[1], 0 );
11639     ok_sequence( wmCharSeq, "Unicode WM_CHAR", FALSE );
11640
11641     /* interleaved winproc and dispatch */
11642     msg.hwnd = hwnd;
11643     msg.message = WM_CHAR;
11644     msg.wParam = dbch[0];
11645     msg.lParam = 0;
11646     CallWindowProcA( (WNDPROC)GetWindowLongPtrA( hwnd, GWLP_WNDPROC ), hwnd, WM_CHAR, dbch[0], 0 );
11647     DispatchMessageA( &msg );
11648     ok_sequence( WmEmptySeq, "no messages", FALSE );
11649     msg.wParam = dbch[1];
11650     DispatchMessageA( &msg );
11651     ok_sequence( wmCharSeq, "Unicode WM_CHAR", FALSE );
11652     CallWindowProcA( (WNDPROC)GetWindowLongPtrA( hwnd, GWLP_WNDPROC ), hwnd, WM_CHAR, dbch[1], 0 );
11653     ok_sequence( wmCharSeq, "Unicode WM_CHAR", FALSE );
11654
11655     /* interleaved sends */
11656     flush_sequence();
11657     SendMessageA( hwnd, WM_CHAR, dbch[0], 0 );
11658     SendMessageCallbackA( hwnd, WM_CHAR, dbch[0], 0, NULL, 0 );
11659     ok_sequence( WmEmptySeq, "no messages", FALSE );
11660     SendMessageTimeoutA( hwnd, WM_CHAR, dbch[1], 0, SMTO_NORMAL, 0, &res );
11661     ok_sequence( wmCharSeq, "Unicode WM_CHAR", FALSE );
11662     SendMessageA( hwnd, WM_CHAR, dbch[1], 0 );
11663     ok_sequence( wmCharSeq, "Unicode WM_CHAR", FALSE );
11664
11665     /* dbcs WM_CHAR */
11666     flush_sequence();
11667     SendMessageA( hwnd2, WM_CHAR, (dbch[1] << 8) | dbch[0], 0 );
11668     ok_sequence( wmCharSeq, "Unicode WM_CHAR", FALSE );
11669     ok( !PeekMessageW( &msg, hwnd, 0, 0, PM_REMOVE ), "got message %x\n", msg.message );
11670
11671     /* other char messages are not magic */
11672     PostMessageA( hwnd, WM_SYSCHAR, dbch[0], 0 );
11673     ok( PeekMessageW( &msg, hwnd, 0, 0, PM_REMOVE ), "no message\n" );
11674     ok( msg.message == WM_SYSCHAR, "unexpected message %x\n", msg.message );
11675     ok( msg.wParam == bad_wch, "bad wparam %lx/%x\n", msg.wParam, bad_wch );
11676     ok( !PeekMessageW( &msg, hwnd, 0, 0, PM_REMOVE ), "got message %x\n", msg.message );
11677     PostMessageA( hwnd, WM_DEADCHAR, dbch[0], 0 );
11678     ok( PeekMessageW( &msg, hwnd, 0, 0, PM_REMOVE ), "no message\n" );
11679     ok( msg.message == WM_DEADCHAR, "unexpected message %x\n", msg.message );
11680     ok( msg.wParam == bad_wch, "bad wparam %lx/%x\n", msg.wParam, bad_wch );
11681     ok( !PeekMessageW( &msg, hwnd, 0, 0, PM_REMOVE ), "got message %x\n", msg.message );
11682
11683     /* test retrieving messages */
11684
11685     PostMessageW( hwnd, WM_CHAR, wch, 0 );
11686     ok( PeekMessageA( &msg, hwnd, 0, 0, PM_REMOVE ), "no message\n" );
11687     ok( msg.hwnd == hwnd, "unexpected hwnd %p\n", msg.hwnd );
11688     ok( msg.message == WM_CHAR, "unexpected message %x\n", msg.message );
11689     ok( msg.wParam == dbch[0], "bad wparam %lx/%x\n", msg.wParam, dbch[0] );
11690     ok( PeekMessageA( &msg, hwnd, 0, 0, PM_REMOVE ), "no message\n" );
11691     ok( msg.hwnd == hwnd, "unexpected hwnd %p\n", msg.hwnd );
11692     ok( msg.message == WM_CHAR, "unexpected message %x\n", msg.message );
11693     ok( msg.wParam == dbch[1], "bad wparam %lx/%x\n", msg.wParam, dbch[0] );
11694     ok( !PeekMessageA( &msg, hwnd, 0, 0, PM_REMOVE ), "got message %x\n", msg.message );
11695
11696     /* message filters */
11697     PostMessageW( hwnd, WM_CHAR, wch, 0 );
11698     ok( PeekMessageA( &msg, hwnd, 0, 0, PM_REMOVE ), "no message\n" );
11699     ok( msg.hwnd == hwnd, "unexpected hwnd %p\n", msg.hwnd );
11700     ok( msg.message == WM_CHAR, "unexpected message %x\n", msg.message );
11701     ok( msg.wParam == dbch[0], "bad wparam %lx/%x\n", msg.wParam, dbch[0] );
11702     /* message id is filtered, hwnd is not */
11703     ok( !PeekMessageA( &msg, hwnd, WM_MOUSEFIRST, WM_MOUSELAST, PM_REMOVE ), "no message\n" );
11704     ok( PeekMessageA( &msg, hwnd2, 0, 0, PM_REMOVE ), "no message\n" );
11705     ok( msg.hwnd == hwnd, "unexpected hwnd %p\n", msg.hwnd );
11706     ok( msg.message == WM_CHAR, "unexpected message %x\n", msg.message );
11707     ok( msg.wParam == dbch[1], "bad wparam %lx/%x\n", msg.wParam, dbch[0] );
11708     ok( !PeekMessageA( &msg, hwnd, 0, 0, PM_REMOVE ), "got message %x\n", msg.message );
11709
11710     /* mixing GetMessage and PostMessage */
11711     PostMessageW( hwnd, WM_CHAR, wch, 0xbeef );
11712     ok( GetMessageA( &msg, hwnd, 0, 0 ), "no message\n" );
11713     ok( msg.hwnd == hwnd, "unexpected hwnd %p\n", msg.hwnd );
11714     ok( msg.message == WM_CHAR, "unexpected message %x\n", msg.message );
11715     ok( msg.wParam == dbch[0], "bad wparam %lx/%x\n", msg.wParam, dbch[0] );
11716     ok( msg.lParam == 0xbeef, "bad lparam %lx\n", msg.lParam );
11717     time = msg.time;
11718     pt = msg.pt;
11719     ok( time - GetTickCount() <= 100, "bad time %x\n", msg.time );
11720     ok( PeekMessageA( &msg, 0, 0, 0, PM_REMOVE ), "no message\n" );
11721     ok( msg.hwnd == hwnd, "unexpected hwnd %p\n", msg.hwnd );
11722     ok( msg.message == WM_CHAR, "unexpected message %x\n", msg.message );
11723     ok( msg.wParam == dbch[1], "bad wparam %lx/%x\n", msg.wParam, dbch[0] );
11724     ok( msg.lParam == 0xbeef, "bad lparam %lx\n", msg.lParam );
11725     ok( msg.time == time, "bad time %x/%x\n", msg.time, time );
11726     ok( msg.pt.x == pt.x && msg.pt.y == pt.y, "bad point %u,%u/%u,%u\n", msg.pt.x, msg.pt.y, pt.x, pt.y );
11727     ok( !PeekMessageA( &msg, hwnd, 0, 0, PM_REMOVE ), "got message %x\n", msg.message );
11728
11729     /* without PM_REMOVE */
11730     PostMessageW( hwnd, WM_CHAR, wch, 0 );
11731     ok( PeekMessageA( &msg, 0, 0, 0, PM_NOREMOVE ), "no message\n" );
11732     ok( msg.hwnd == hwnd, "unexpected hwnd %p\n", msg.hwnd );
11733     ok( msg.message == WM_CHAR, "unexpected message %x\n", msg.message );
11734     ok( msg.wParam == dbch[0], "bad wparam %lx/%x\n", msg.wParam, dbch[0] );
11735     ok( PeekMessageA( &msg, 0, 0, 0, PM_REMOVE ), "no message\n" );
11736     ok( msg.hwnd == hwnd, "unexpected hwnd %p\n", msg.hwnd );
11737     ok( msg.message == WM_CHAR, "unexpected message %x\n", msg.message );
11738     ok( msg.wParam == dbch[0], "bad wparam %lx/%x\n", msg.wParam, dbch[0] );
11739     ok( PeekMessageA( &msg, 0, 0, 0, PM_NOREMOVE ), "no message\n" );
11740     ok( msg.hwnd == hwnd, "unexpected hwnd %p\n", msg.hwnd );
11741     ok( msg.message == WM_CHAR, "unexpected message %x\n", msg.message );
11742     ok( msg.wParam == dbch[1], "bad wparam %lx/%x\n", msg.wParam, dbch[0] );
11743     ok( PeekMessageA( &msg, 0, 0, 0, PM_REMOVE ), "no message\n" );
11744     ok( msg.hwnd == hwnd, "unexpected hwnd %p\n", msg.hwnd );
11745     ok( msg.message == WM_CHAR, "unexpected message %x\n", msg.message );
11746     ok( msg.wParam == dbch[1], "bad wparam %lx/%x\n", msg.wParam, dbch[0] );
11747     ok( !PeekMessageA( &msg, hwnd, 0, 0, PM_REMOVE ), "got message %x\n", msg.message );
11748
11749     DestroyWindow(hwnd);
11750     DestroyWindow(hwnd2);
11751 }
11752
11753 #define ID_LISTBOX 0x000f
11754
11755 static const struct message wm_lb_setcursel_0[] =
11756 {
11757     { LB_SETCURSEL, sent|wparam|lparam, 0, 0 },
11758     { WM_CTLCOLORLISTBOX, sent|parent },
11759     { WM_DRAWITEM, sent|wparam|lparam|parent, ID_LISTBOX, 0x000120f2 },
11760     { EVENT_OBJECT_FOCUS, winevent_hook|wparam|lparam, OBJID_CLIENT, 1 },
11761     { EVENT_OBJECT_SELECTION, winevent_hook|wparam|lparam, OBJID_CLIENT, 1 },
11762     { 0 }
11763 };
11764 static const struct message wm_lb_setcursel_1[] =
11765 {
11766     { LB_SETCURSEL, sent|wparam|lparam, 1, 0 },
11767     { WM_CTLCOLORLISTBOX, sent|parent },
11768     { WM_DRAWITEM, sent|wparam|lparam|parent, ID_LISTBOX, 0x000020f2 },
11769     { WM_CTLCOLORLISTBOX, sent|parent },
11770     { WM_DRAWITEM, sent|wparam|lparam|parent, ID_LISTBOX, 0x000121f2 },
11771     { EVENT_OBJECT_FOCUS, winevent_hook|wparam|lparam, OBJID_CLIENT, 2 },
11772     { EVENT_OBJECT_SELECTION, winevent_hook|wparam|lparam, OBJID_CLIENT, 2 },
11773     { 0 }
11774 };
11775 static const struct message wm_lb_setcursel_2[] =
11776 {
11777     { LB_SETCURSEL, sent|wparam|lparam, 2, 0 },
11778     { WM_CTLCOLORLISTBOX, sent|parent },
11779     { WM_DRAWITEM, sent|wparam|lparam|parent, ID_LISTBOX, 0x000021f2 },
11780     { WM_CTLCOLORLISTBOX, sent|parent },
11781     { WM_DRAWITEM, sent|wparam|lparam|parent, ID_LISTBOX, 0x000122f2 },
11782     { EVENT_OBJECT_FOCUS, winevent_hook|wparam|lparam, OBJID_CLIENT, 3 },
11783     { EVENT_OBJECT_SELECTION, winevent_hook|wparam|lparam, OBJID_CLIENT, 3 },
11784     { 0 }
11785 };
11786 static const struct message wm_lb_click_0[] =
11787 {
11788     { WM_LBUTTONDOWN, sent|wparam|lparam, 0, MAKELPARAM(1,1) },
11789     { HCBT_SETFOCUS, hook },
11790     { WM_KILLFOCUS, sent|parent },
11791     { WM_IME_SETCONTEXT, sent|wparam|optional|parent, 0 },
11792     { WM_IME_SETCONTEXT, sent|wparam|defwinproc|optional, 1 },
11793     { EVENT_OBJECT_FOCUS, winevent_hook|wparam|lparam, OBJID_CLIENT, 0 },
11794     { WM_SETFOCUS, sent|defwinproc },
11795
11796     { WM_DRAWITEM, sent|wparam|lparam|parent, ID_LISTBOX, 0x001142f2 },
11797     { WM_COMMAND, sent|wparam|parent, MAKEWPARAM(ID_LISTBOX, LBN_SETFOCUS) },
11798     { EVENT_OBJECT_FOCUS, winevent_hook|wparam|lparam, OBJID_CLIENT, 3 },
11799     { WM_LBTRACKPOINT, sent|wparam|lparam|parent, 0, MAKELPARAM(1,1) },
11800     { EVENT_SYSTEM_CAPTURESTART, winevent_hook|wparam|lparam, 0, 0 },
11801
11802     { WM_DRAWITEM, sent|wparam|lparam|parent, ID_LISTBOX, 0x000142f2 },
11803     { WM_CTLCOLORLISTBOX, sent|parent },
11804     { WM_DRAWITEM, sent|wparam|lparam|parent, ID_LISTBOX, 0x000022f2 },
11805     { WM_CTLCOLORLISTBOX, sent|parent },
11806     { WM_DRAWITEM, sent|wparam|lparam|parent, ID_LISTBOX, 0x000120f2 },
11807     { WM_DRAWITEM, sent|wparam|lparam|parent, ID_LISTBOX, 0x001140f2 },
11808
11809     { EVENT_OBJECT_FOCUS, winevent_hook|wparam|lparam, OBJID_CLIENT, 1 },
11810     { EVENT_OBJECT_SELECTION, winevent_hook|wparam|lparam, OBJID_CLIENT, 1 },
11811
11812     { WM_LBUTTONUP, sent|wparam|lparam, 0, 0 },
11813     { EVENT_SYSTEM_CAPTUREEND, winevent_hook|wparam|lparam, 0, 0 },
11814     { WM_CAPTURECHANGED, sent|wparam|lparam|defwinproc, 0, 0 },
11815     { WM_COMMAND, sent|wparam|parent, MAKEWPARAM(ID_LISTBOX, LBN_SELCHANGE) },
11816     { 0 }
11817 };
11818 static const struct message wm_lb_deletestring[] =
11819 {
11820     { LB_DELETESTRING, sent|wparam|lparam, 0, 0 },
11821     { WM_DELETEITEM, sent|wparam|parent|optional, ID_LISTBOX, 0 },
11822     { WM_DRAWITEM, sent|wparam|parent|optional, ID_LISTBOX },
11823     { WM_DRAWITEM, sent|wparam|parent|optional, ID_LISTBOX },
11824     { 0 }
11825 };
11826 static const struct message wm_lb_deletestring_reset[] =
11827 {
11828     { LB_DELETESTRING, sent|wparam|lparam, 0, 0 },
11829     { LB_RESETCONTENT, sent|wparam|lparam|defwinproc|optional, 0, 0 },
11830     { WM_DELETEITEM, sent|wparam|parent|optional, ID_LISTBOX, 0 },
11831     { WM_DRAWITEM, sent|wparam|parent|optional, ID_LISTBOX },
11832     { WM_DRAWITEM, sent|wparam|parent|optional, ID_LISTBOX },
11833     { 0 }
11834 };
11835
11836 #define check_lb_state(a1, a2, a3, a4, a5) check_lb_state_dbg(a1, a2, a3, a4, a5, __LINE__)
11837
11838 static LRESULT (WINAPI *listbox_orig_proc)(HWND, UINT, WPARAM, LPARAM);
11839
11840 static LRESULT WINAPI listbox_hook_proc(HWND hwnd, UINT message, WPARAM wp, LPARAM lp)
11841 {
11842     static LONG defwndproc_counter = 0;
11843     LRESULT ret;
11844     struct recvd_message msg;
11845
11846     /* do not log painting messages */
11847     if (message != WM_PAINT &&
11848         message != WM_NCPAINT &&
11849         message != WM_SYNCPAINT &&
11850         message != WM_ERASEBKGND &&
11851         message != WM_NCHITTEST &&
11852         message != WM_GETTEXT &&
11853         !ignore_message( message ))
11854     {
11855         msg.hwnd = hwnd;
11856         msg.message = message;
11857         msg.flags = sent|wparam|lparam;
11858         if (defwndproc_counter) msg.flags |= defwinproc;
11859         msg.wParam = wp;
11860         msg.lParam = lp;
11861         msg.descr = "listbox";
11862         add_message(&msg);
11863     }
11864
11865     defwndproc_counter++;
11866     ret = CallWindowProcA(listbox_orig_proc, hwnd, message, wp, lp);
11867     defwndproc_counter--;
11868
11869     return ret;
11870 }
11871
11872 static void check_lb_state_dbg(HWND listbox, int count, int cur_sel,
11873                                int caret_index, int top_index, int line)
11874 {
11875     LRESULT ret;
11876
11877     /* calling an orig proc helps to avoid unnecessary message logging */
11878     ret = CallWindowProcA(listbox_orig_proc, listbox, LB_GETCOUNT, 0, 0);
11879     ok_(__FILE__, line)(ret == count, "expected count %d, got %ld\n", count, ret);
11880     ret = CallWindowProcA(listbox_orig_proc, listbox, LB_GETCURSEL, 0, 0);
11881     ok_(__FILE__, line)(ret == cur_sel, "expected cur sel %d, got %ld\n", cur_sel, ret);
11882     ret = CallWindowProcA(listbox_orig_proc, listbox, LB_GETCARETINDEX, 0, 0);
11883     ok_(__FILE__, line)(ret == caret_index ||
11884                         broken(cur_sel == -1 && caret_index == 0 && ret == -1),  /* nt4 */
11885                         "expected caret index %d, got %ld\n", caret_index, ret);
11886     ret = CallWindowProcA(listbox_orig_proc, listbox, LB_GETTOPINDEX, 0, 0);
11887     ok_(__FILE__, line)(ret == top_index, "expected top index %d, got %ld\n", top_index, ret);
11888 }
11889
11890 static void test_listbox_messages(void)
11891 {
11892     HWND parent, listbox;
11893     LRESULT ret;
11894
11895     parent = CreateWindowExA(0, "TestParentClass", NULL, WS_OVERLAPPEDWINDOW  | WS_VISIBLE,
11896                              100, 100, 200, 200, 0, 0, 0, NULL);
11897     listbox = CreateWindowExA(WS_EX_NOPARENTNOTIFY, "ListBox", NULL,
11898                               WS_CHILD | LBS_NOTIFY | LBS_OWNERDRAWVARIABLE | LBS_HASSTRINGS | WS_VISIBLE,
11899                               10, 10, 80, 80, parent, (HMENU)ID_LISTBOX, 0, NULL);
11900     listbox_orig_proc = (WNDPROC)SetWindowLongPtrA(listbox, GWLP_WNDPROC, (ULONG_PTR)listbox_hook_proc);
11901
11902     check_lb_state(listbox, 0, LB_ERR, 0, 0);
11903
11904     ret = SendMessage(listbox, LB_ADDSTRING, 0, (LPARAM)"item 0");
11905     ok(ret == 0, "expected 0, got %ld\n", ret);
11906     ret = SendMessage(listbox, LB_ADDSTRING, 0, (LPARAM)"item 1");
11907     ok(ret == 1, "expected 1, got %ld\n", ret);
11908     ret = SendMessage(listbox, LB_ADDSTRING, 0, (LPARAM)"item 2");
11909     ok(ret == 2, "expected 2, got %ld\n", ret);
11910
11911     check_lb_state(listbox, 3, LB_ERR, 0, 0);
11912
11913     flush_sequence();
11914
11915     log_all_parent_messages++;
11916
11917     trace("selecting item 0\n");
11918     ret = SendMessage(listbox, LB_SETCURSEL, 0, 0);
11919     ok(ret == 0, "expected 0, got %ld\n", ret);
11920     ok_sequence(wm_lb_setcursel_0, "LB_SETCURSEL 0", FALSE );
11921     check_lb_state(listbox, 3, 0, 0, 0);
11922     flush_sequence();
11923
11924     trace("selecting item 1\n");
11925     ret = SendMessage(listbox, LB_SETCURSEL, 1, 0);
11926     ok(ret == 1, "expected 1, got %ld\n", ret);
11927     ok_sequence(wm_lb_setcursel_1, "LB_SETCURSEL 1", FALSE );
11928     check_lb_state(listbox, 3, 1, 1, 0);
11929
11930     trace("selecting item 2\n");
11931     ret = SendMessage(listbox, LB_SETCURSEL, 2, 0);
11932     ok(ret == 2, "expected 2, got %ld\n", ret);
11933     ok_sequence(wm_lb_setcursel_2, "LB_SETCURSEL 2", FALSE );
11934     check_lb_state(listbox, 3, 2, 2, 0);
11935
11936     trace("clicking on item 0\n");
11937     ret = SendMessage(listbox, WM_LBUTTONDOWN, 0, MAKELPARAM(1, 1));
11938     ok(ret == LB_OKAY, "expected LB_OKAY, got %ld\n", ret);
11939     ret = SendMessage(listbox, WM_LBUTTONUP, 0, 0);
11940     ok(ret == LB_OKAY, "expected LB_OKAY, got %ld\n", ret);
11941     ok_sequence(wm_lb_click_0, "WM_LBUTTONDOWN 0", FALSE );
11942     check_lb_state(listbox, 3, 0, 0, 0);
11943     flush_sequence();
11944
11945     trace("deleting item 0\n");
11946     ret = SendMessage(listbox, LB_DELETESTRING, 0, 0);
11947     ok(ret == 2, "expected 2, got %ld\n", ret);
11948     ok_sequence(wm_lb_deletestring, "LB_DELETESTRING 0", FALSE );
11949     check_lb_state(listbox, 2, -1, 0, 0);
11950     flush_sequence();
11951
11952     trace("deleting item 0\n");
11953     ret = SendMessage(listbox, LB_DELETESTRING, 0, 0);
11954     ok(ret == 1, "expected 1, got %ld\n", ret);
11955     ok_sequence(wm_lb_deletestring, "LB_DELETESTRING 0", FALSE );
11956     check_lb_state(listbox, 1, -1, 0, 0);
11957     flush_sequence();
11958
11959     trace("deleting item 0\n");
11960     ret = SendMessage(listbox, LB_DELETESTRING, 0, 0);
11961     ok(ret == 0, "expected 0, got %ld\n", ret);
11962     ok_sequence(wm_lb_deletestring_reset, "LB_DELETESTRING 0", FALSE );
11963     check_lb_state(listbox, 0, -1, 0, 0);
11964     flush_sequence();
11965
11966     trace("deleting item 0\n");
11967     ret = SendMessage(listbox, LB_DELETESTRING, 0, 0);
11968     ok(ret == LB_ERR, "expected LB_ERR, got %ld\n", ret);
11969     check_lb_state(listbox, 0, -1, 0, 0);
11970     flush_sequence();
11971
11972     log_all_parent_messages--;
11973
11974     DestroyWindow(listbox);
11975     DestroyWindow(parent);
11976 }
11977
11978 /*************************** Menu test ******************************/
11979 static const struct message wm_popup_menu_1[] =
11980 {
11981     { HCBT_KEYSKIPPED, hook|wparam|lparam|optional, VK_MENU, 0x20000001 },
11982     { WM_SYSKEYDOWN, sent|wparam|lparam, VK_MENU, 0x20000001 },
11983     { HCBT_KEYSKIPPED, hook|wparam|lparam|optional, 'E', 0x20000001 },
11984     { WM_SYSKEYDOWN, sent|wparam|lparam, 'E', 0x20000001 },
11985     { WM_SYSCHAR, sent|wparam|lparam, 'e', 0x20000001 },
11986     { HCBT_SYSCOMMAND, hook|wparam|lparam, SC_KEYMENU, 'e' },
11987     { WM_ENTERMENULOOP, sent|wparam|lparam, 0, 0 },
11988     { WM_INITMENU, sent|lparam, 0, 0 },
11989     { WM_MENUSELECT, sent|wparam, MAKEWPARAM(1,MF_HILITE|MF_POPUP) },
11990     { WM_INITMENUPOPUP, sent|lparam, 0, 1 },
11991     { HCBT_CREATEWND, hook|optional }, /* Win9x doesn't create a window */
11992     { WM_MENUSELECT, sent|wparam, MAKEWPARAM(200,MF_HILITE) },
11993     { HCBT_KEYSKIPPED, hook|wparam|lparam|optional, 'E', 0xf0000001 },
11994     { HCBT_KEYSKIPPED, hook|wparam|lparam|optional, VK_MENU, 0xd0000001 },
11995     { HCBT_KEYSKIPPED, hook|wparam|lparam|optional, VK_RETURN, 0x10000001, 0, 0x40000000 },
11996     { HCBT_DESTROYWND, hook|optional }, /* Win9x doesn't create a window */
11997     { WM_UNINITMENUPOPUP, sent|lparam, 0, 0 },
11998     { WM_MENUSELECT, sent|wparam|lparam, MAKEWPARAM(0,0xffff), 0 },
11999     { WM_EXITMENULOOP, sent|wparam|lparam, 0, 0 },
12000     { WM_MENUCOMMAND, sent }, /* |wparam, 200 - Win9x */
12001     { HCBT_KEYSKIPPED, hook|wparam|lparam|optional, VK_RETURN, 0xc0000001 },
12002     { WM_KEYUP, sent|wparam|lparam, VK_RETURN, 0xc0000001 },
12003     { 0 }
12004 };
12005 static const struct message wm_popup_menu_2[] =
12006 {
12007     { HCBT_KEYSKIPPED, hook|wparam|lparam|optional, VK_MENU, 0x20000001 },
12008     { WM_SYSKEYDOWN, sent|wparam|lparam, VK_MENU, 0x20000001 },
12009     { HCBT_KEYSKIPPED, hook|wparam|lparam|optional, 'F', 0x20000001 },
12010     { WM_SYSKEYDOWN, sent|wparam|lparam, 'F', 0x20000001 },
12011     { WM_SYSCHAR, sent|wparam|lparam, 'f', 0x20000001 },
12012     { HCBT_SYSCOMMAND, hook|wparam|lparam, SC_KEYMENU, 'f' },
12013     { WM_ENTERMENULOOP, sent|wparam|lparam, 0, 0 },
12014     { WM_INITMENU, sent|lparam, 0, 0 },
12015     { WM_MENUSELECT, sent|wparam, MAKEWPARAM(0,MF_HILITE|MF_POPUP) },
12016     { WM_INITMENUPOPUP, sent|lparam, 0, 0 },
12017     { WM_MENUSELECT, sent|wparam|optional, MAKEWPARAM(0,MF_HILITE|MF_POPUP) }, /* Win9x */
12018     { WM_INITMENUPOPUP, sent|lparam|optional, 0, 0 }, /* Win9x */
12019     { HCBT_CREATEWND, hook },
12020     { WM_MENUSELECT, sent }, /*|wparam, MAKEWPARAM(0,MF_HILITE|MF_POPUP) - XP
12021                                |wparam, MAKEWPARAM(100,MF_HILITE) - Win9x */
12022     { HCBT_KEYSKIPPED, hook|wparam|lparam|optional, 'F', 0xf0000001 },
12023     { HCBT_KEYSKIPPED, hook|wparam|lparam|optional, VK_MENU, 0xd0000001 },
12024     { HCBT_KEYSKIPPED, hook|wparam|lparam|optional, VK_RIGHT, 0x10000001 },
12025     { WM_INITMENUPOPUP, sent|lparam|optional, 0, 0 }, /* Win9x doesn't send it */
12026     { HCBT_CREATEWND, hook|optional }, /* Win9x doesn't send it */
12027     { WM_MENUSELECT, sent|wparam|optional, MAKEWPARAM(100,MF_HILITE) },
12028     { HCBT_KEYSKIPPED, hook|wparam|lparam|optional, VK_RIGHT, 0xd0000001 },
12029     { HCBT_KEYSKIPPED, hook|wparam|lparam|optional, VK_RETURN, 0x10000001 },
12030     { HCBT_DESTROYWND, hook },
12031     { WM_UNINITMENUPOPUP, sent|lparam, 0, 0 },
12032     { HCBT_DESTROYWND, hook|optional }, /* Win9x doesn't send it */
12033     { WM_UNINITMENUPOPUP, sent|lparam, 0, 0 },
12034     { WM_MENUSELECT, sent|wparam|lparam, MAKEWPARAM(0,0xffff), 0 },
12035     { WM_EXITMENULOOP, sent|wparam|lparam, 0, 0 },
12036     { WM_MENUCOMMAND, sent }, /* |wparam, 100 - Win9x */
12037     { HCBT_KEYSKIPPED, hook|wparam|lparam|optional, VK_RETURN, 0xc0000001 },
12038     { WM_KEYUP, sent|wparam|lparam, VK_RETURN, 0xc0000001 },
12039     { 0 }
12040 };
12041 static const struct message wm_popup_menu_3[] =
12042 {
12043     { HCBT_KEYSKIPPED, hook|wparam|lparam|optional, VK_MENU, 0x20000001 },
12044     { WM_SYSKEYDOWN, sent|wparam|lparam, VK_MENU, 0x20000001 },
12045     { HCBT_KEYSKIPPED, hook|wparam|lparam|optional, 'F', 0x20000001 },
12046     { WM_SYSKEYDOWN, sent|wparam|lparam, 'F', 0x20000001 },
12047     { WM_SYSCHAR, sent|wparam|lparam, 'f', 0x20000001 },
12048     { HCBT_SYSCOMMAND, hook|wparam|lparam, SC_KEYMENU, 'f' },
12049     { WM_ENTERMENULOOP, sent|wparam|lparam, 0, 0 },
12050     { WM_INITMENU, sent|lparam, 0, 0 },
12051     { WM_MENUSELECT, sent|wparam, MAKEWPARAM(0,MF_HILITE|MF_POPUP) },
12052     { WM_INITMENUPOPUP, sent|lparam, 0, 0 },
12053     { WM_MENUSELECT, sent|wparam|optional, MAKEWPARAM(0,MF_HILITE|MF_POPUP) }, /* Win9x */
12054     { WM_INITMENUPOPUP, sent|lparam|optional, 0, 0 }, /* Win9x */
12055     { HCBT_CREATEWND, hook },
12056     { WM_MENUSELECT, sent }, /*|wparam, MAKEWPARAM(0,MF_HILITE|MF_POPUP) - XP
12057                                |wparam, MAKEWPARAM(100,MF_HILITE) - Win9x */
12058     { HCBT_KEYSKIPPED, hook|wparam|lparam|optional, 'F', 0xf0000001 },
12059     { HCBT_KEYSKIPPED, hook|wparam|lparam|optional, VK_MENU, 0xd0000001 },
12060     { HCBT_KEYSKIPPED, hook|wparam|lparam|optional, VK_RIGHT, 0x10000001 },
12061     { WM_INITMENUPOPUP, sent|lparam|optional, 0, 0 }, /* Win9x doesn't send it */
12062     { HCBT_CREATEWND, hook|optional }, /* Win9x doesn't send it */
12063     { WM_MENUSELECT, sent|wparam|optional, MAKEWPARAM(100,MF_HILITE) },
12064     { HCBT_KEYSKIPPED, hook|wparam|lparam|optional, VK_RIGHT, 0xd0000001 },
12065     { HCBT_KEYSKIPPED, hook|wparam|lparam|optional, VK_RETURN, 0x10000001 },
12066     { HCBT_DESTROYWND, hook },
12067     { WM_UNINITMENUPOPUP, sent|lparam, 0, 0 },
12068     { HCBT_DESTROYWND, hook|optional }, /* Win9x doesn't send it */
12069     { WM_UNINITMENUPOPUP, sent|lparam, 0, 0 },
12070     { WM_MENUSELECT, sent|wparam|lparam, MAKEWPARAM(0,0xffff), 0 },
12071     { WM_EXITMENULOOP, sent|wparam|lparam, 0, 0 },
12072     { WM_COMMAND, sent|wparam|lparam, 100, 0 },
12073     { HCBT_KEYSKIPPED, hook|wparam|lparam|optional, VK_RETURN, 0xc0000001 },
12074     { WM_KEYUP, sent|wparam|lparam, VK_RETURN, 0xc0000001 },
12075     { 0 }
12076 };
12077
12078 static const struct message wm_single_menu_item[] =
12079 {
12080     { HCBT_KEYSKIPPED, hook|wparam|lparam|optional, VK_MENU, 0x20000001 },
12081     { WM_SYSKEYDOWN, sent|wparam|lparam, VK_MENU, 0x20000001 },
12082     { HCBT_KEYSKIPPED, hook|wparam|lparam|optional, 'Q', 0x20000001 },
12083     { WM_SYSKEYDOWN, sent|wparam|lparam, 'Q', 0x20000001 },
12084     { WM_SYSCHAR, sent|wparam|lparam, 'q', 0x20000001 },
12085     { HCBT_SYSCOMMAND, hook|wparam|lparam, SC_KEYMENU, 'q' },
12086     { WM_ENTERMENULOOP, sent|wparam|lparam, 0, 0 },
12087     { WM_INITMENU, sent|lparam, 0, 0 },
12088     { WM_MENUSELECT, sent|wparam|optional, MAKEWPARAM(300,MF_HILITE) },
12089     { WM_MENUSELECT, sent|wparam|lparam, MAKEWPARAM(0,0xffff), 0 },
12090     { WM_EXITMENULOOP, sent|wparam|lparam, 0, 0 },
12091     { WM_MENUCOMMAND, sent },
12092     { HCBT_KEYSKIPPED, hook|wparam|lparam|optional, 'Q', 0xe0000001 },
12093     { WM_SYSKEYUP, sent|wparam|lparam, 'Q', 0xe0000001 },
12094     { HCBT_KEYSKIPPED, hook|wparam|lparam|optional, VK_MENU, 0xc0000001 },
12095     { WM_KEYUP, sent|wparam|lparam, VK_MENU, 0xc0000001 },
12096
12097     { HCBT_KEYSKIPPED, hook|wparam|lparam|optional, VK_ESCAPE, 1 },
12098     { WM_KEYDOWN, sent|wparam|lparam, VK_ESCAPE, 1 },
12099     { WM_CHAR,  sent|wparam|lparam, VK_ESCAPE, 0x00000001 },
12100     { HCBT_KEYSKIPPED, hook|wparam|lparam|optional, VK_ESCAPE, 0xc0000001 },
12101     { WM_KEYUP, sent|wparam|lparam, VK_ESCAPE, 0xc0000001 },
12102     { 0 }
12103 };
12104
12105 static LRESULT WINAPI parent_menu_proc(HWND hwnd, UINT message, WPARAM wp, LPARAM lp)
12106 {
12107     if (message == WM_ENTERIDLE ||
12108         message == WM_INITMENU ||
12109         message == WM_INITMENUPOPUP ||
12110         message == WM_MENUSELECT ||
12111         message == WM_PARENTNOTIFY ||
12112         message == WM_ENTERMENULOOP ||
12113         message == WM_EXITMENULOOP ||
12114         message == WM_UNINITMENUPOPUP ||
12115         message == WM_KEYDOWN ||
12116         message == WM_KEYUP ||
12117         message == WM_CHAR ||
12118         message == WM_SYSKEYDOWN ||
12119         message == WM_SYSKEYUP ||
12120         message == WM_SYSCHAR ||
12121         message == WM_COMMAND ||
12122         message == WM_MENUCOMMAND)
12123     {
12124         struct recvd_message msg;
12125
12126         msg.hwnd = hwnd;
12127         msg.message = message;
12128         msg.flags = sent|wparam|lparam;
12129         msg.wParam = wp;
12130         msg.lParam = lp;
12131         msg.descr = "parent_menu_proc";
12132         add_message(&msg);
12133     }
12134
12135     return DefWindowProcA(hwnd, message, wp, lp);
12136 }
12137
12138 static void set_menu_style(HMENU hmenu, DWORD style)
12139 {
12140     MENUINFO mi;
12141     BOOL ret;
12142
12143     mi.cbSize = sizeof(mi);
12144     mi.fMask = MIM_STYLE;
12145     mi.dwStyle = style;
12146     SetLastError(0xdeadbeef);
12147     ret = pSetMenuInfo(hmenu, &mi);
12148     ok(ret, "SetMenuInfo error %u\n", GetLastError());
12149 }
12150
12151 static DWORD get_menu_style(HMENU hmenu)
12152 {
12153     MENUINFO mi;
12154     BOOL ret;
12155
12156     mi.cbSize = sizeof(mi);
12157     mi.fMask = MIM_STYLE;
12158     mi.dwStyle = 0;
12159     SetLastError(0xdeadbeef);
12160     ret = pGetMenuInfo(hmenu, &mi);
12161     ok(ret, "GetMenuInfo error %u\n", GetLastError());
12162
12163     return mi.dwStyle;
12164 }
12165
12166 static void test_menu_messages(void)
12167 {
12168     MSG msg;
12169     WNDCLASSA cls;
12170     HMENU hmenu, hmenu_popup;
12171     HWND hwnd;
12172     DWORD style;
12173
12174     if (!pGetMenuInfo || !pSetMenuInfo)
12175     {
12176         win_skip("GetMenuInfo and/or SetMenuInfo are not available\n");
12177         return;
12178     }
12179     cls.style = 0;
12180     cls.lpfnWndProc = parent_menu_proc;
12181     cls.cbClsExtra = 0;
12182     cls.cbWndExtra = 0;
12183     cls.hInstance = GetModuleHandleA(0);
12184     cls.hIcon = 0;
12185     cls.hCursor = LoadCursorA(0, IDC_ARROW);
12186     cls.hbrBackground = GetStockObject(WHITE_BRUSH);
12187     cls.lpszMenuName = NULL;
12188     cls.lpszClassName = "TestMenuClass";
12189     UnregisterClass(cls.lpszClassName, cls.hInstance);
12190     if (!RegisterClassA(&cls)) assert(0);
12191
12192     SetLastError(0xdeadbeef);
12193     hwnd = CreateWindowExA(0, "TestMenuClass", NULL, WS_OVERLAPPEDWINDOW | WS_VISIBLE,
12194                            100, 100, 200, 200, 0, 0, 0, NULL);
12195     ok(hwnd != 0, "LoadMenuA error %u\n", GetLastError());
12196
12197     SetLastError(0xdeadbeef);
12198     hmenu = LoadMenuA(GetModuleHandle(0), MAKEINTRESOURCE(1));
12199     ok(hmenu != 0, "LoadMenuA error %u\n", GetLastError());
12200
12201     SetMenu(hwnd, hmenu);
12202     SetForegroundWindow( hwnd );
12203
12204     set_menu_style(hmenu, MNS_NOTIFYBYPOS);
12205     style = get_menu_style(hmenu);
12206     ok(style == MNS_NOTIFYBYPOS, "expected MNS_NOTIFYBYPOS, got %u\n", style);
12207
12208     hmenu_popup = GetSubMenu(hmenu, 0);
12209     ok(hmenu_popup != 0, "GetSubMenu returned 0 for submenu 0\n");
12210     style = get_menu_style(hmenu_popup);
12211     ok(style == 0, "expected 0, got %u\n", style);
12212
12213     hmenu_popup = GetSubMenu(hmenu_popup, 0);
12214     ok(hmenu_popup != 0, "GetSubMenu returned 0 for submenu 0\n");
12215     style = get_menu_style(hmenu_popup);
12216     ok(style == 0, "expected 0, got %u\n", style);
12217
12218     /* Alt+E, Enter */
12219     trace("testing a popup menu command\n");
12220     flush_sequence();
12221     keybd_event(VK_MENU, 0, 0, 0);
12222     keybd_event('E', 0, 0, 0);
12223     keybd_event('E', 0, KEYEVENTF_KEYUP, 0);
12224     keybd_event(VK_MENU, 0, KEYEVENTF_KEYUP, 0);
12225     keybd_event(VK_RETURN, 0, 0, 0);
12226     keybd_event(VK_RETURN, 0, KEYEVENTF_KEYUP, 0);
12227     while (PeekMessage(&msg, 0, 0, 0, PM_REMOVE))
12228     {
12229         TranslateMessage(&msg);
12230         DispatchMessage(&msg);
12231     }
12232     if (!sequence_cnt)  /* we didn't get any message */
12233     {
12234         skip( "queuing key events not supported\n" );
12235         goto done;
12236     }
12237     /* win98 queues only a WM_KEYUP and doesn't start menu tracking */
12238     if (sequence[0].message == WM_KEYUP && sequence[0].wParam == VK_MENU)
12239     {
12240         win_skip( "menu tracking through VK_MENU not supported\n" );
12241         goto done;
12242     }
12243     ok_sequence(wm_popup_menu_1, "popup menu command", FALSE);
12244
12245     /* Alt+F, Right, Enter */
12246     trace("testing submenu of a popup menu command\n");
12247     flush_sequence();
12248     keybd_event(VK_MENU, 0, 0, 0);
12249     keybd_event('F', 0, 0, 0);
12250     keybd_event('F', 0, KEYEVENTF_KEYUP, 0);
12251     keybd_event(VK_MENU, 0, KEYEVENTF_KEYUP, 0);
12252     keybd_event(VK_RIGHT, 0, 0, 0);
12253     keybd_event(VK_RIGHT, 0, KEYEVENTF_KEYUP, 0);
12254     keybd_event(VK_RETURN, 0, 0, 0);
12255     keybd_event(VK_RETURN, 0, KEYEVENTF_KEYUP, 0);
12256     while (PeekMessage(&msg, 0, 0, 0, PM_REMOVE))
12257     {
12258         TranslateMessage(&msg);
12259         DispatchMessage(&msg);
12260     }
12261     ok_sequence(wm_popup_menu_2, "submenu of a popup menu command", FALSE);
12262
12263     trace("testing single menu item command\n");
12264     flush_sequence();
12265     keybd_event(VK_MENU, 0, 0, 0);
12266     keybd_event('Q', 0, 0, 0);
12267     keybd_event('Q', 0, KEYEVENTF_KEYUP, 0);
12268     keybd_event(VK_MENU, 0, KEYEVENTF_KEYUP, 0);
12269     keybd_event(VK_ESCAPE, 0, 0, 0);
12270     keybd_event(VK_ESCAPE, 0, KEYEVENTF_KEYUP, 0);
12271     while (PeekMessage(&msg, 0, 0, 0, PM_REMOVE))
12272     {
12273         TranslateMessage(&msg);
12274         DispatchMessage(&msg);
12275     }
12276     ok_sequence(wm_single_menu_item, "single menu item command", FALSE);
12277
12278     set_menu_style(hmenu, 0);
12279     style = get_menu_style(hmenu);
12280     ok(style == 0, "expected 0, got %u\n", style);
12281
12282     hmenu_popup = GetSubMenu(hmenu, 0);
12283     ok(hmenu_popup != 0, "GetSubMenu returned 0 for submenu 0\n");
12284     set_menu_style(hmenu_popup, MNS_NOTIFYBYPOS);
12285     style = get_menu_style(hmenu_popup);
12286     ok(style == MNS_NOTIFYBYPOS, "expected MNS_NOTIFYBYPOS, got %u\n", style);
12287
12288     hmenu_popup = GetSubMenu(hmenu_popup, 0);
12289     ok(hmenu_popup != 0, "GetSubMenu returned 0 for submenu 0\n");
12290     style = get_menu_style(hmenu_popup);
12291     ok(style == 0, "expected 0, got %u\n", style);
12292
12293     /* Alt+F, Right, Enter */
12294     trace("testing submenu of a popup menu command\n");
12295     flush_sequence();
12296     keybd_event(VK_MENU, 0, 0, 0);
12297     keybd_event('F', 0, 0, 0);
12298     keybd_event('F', 0, KEYEVENTF_KEYUP, 0);
12299     keybd_event(VK_MENU, 0, KEYEVENTF_KEYUP, 0);
12300     keybd_event(VK_RIGHT, 0, 0, 0);
12301     keybd_event(VK_RIGHT, 0, KEYEVENTF_KEYUP, 0);
12302     keybd_event(VK_RETURN, 0, 0, 0);
12303     keybd_event(VK_RETURN, 0, KEYEVENTF_KEYUP, 0);
12304     while (PeekMessage(&msg, 0, 0, 0, PM_REMOVE))
12305     {
12306         TranslateMessage(&msg);
12307         DispatchMessage(&msg);
12308     }
12309     ok_sequence(wm_popup_menu_3, "submenu of a popup menu command", FALSE);
12310
12311 done:
12312     DestroyWindow(hwnd);
12313     DestroyMenu(hmenu);
12314 }
12315
12316
12317 static void test_paintingloop(void)
12318 {
12319     HWND hwnd;
12320
12321     paint_loop_done = 0;
12322     hwnd = CreateWindowExA(0x0,"PaintLoopWindowClass",
12323                                "PaintLoopWindowClass",WS_OVERLAPPEDWINDOW,
12324                                 100, 100, 100, 100, 0, 0, 0, NULL );
12325     ok(hwnd != 0, "PaintLoop window error %u\n", GetLastError());
12326     ShowWindow(hwnd,SW_NORMAL);
12327     SetFocus(hwnd);
12328
12329     while (!paint_loop_done)
12330     {
12331         MSG msg;
12332         if (PeekMessageA(&msg, 0, 0, 0, 1))
12333         {
12334             TranslateMessage(&msg);
12335             DispatchMessage(&msg);
12336         }
12337     }
12338     DestroyWindow(hwnd);
12339 }
12340
12341 static void test_defwinproc(void)
12342 {
12343     HWND hwnd;
12344     MSG msg;
12345     int gotwmquit = FALSE;
12346     hwnd = CreateWindowExA(0, "static", "test_defwndproc", WS_POPUP, 0,0,0,0,0,0,0, NULL);
12347     assert(hwnd);
12348     DefWindowProcA( hwnd, WM_ENDSESSION, 1, 0);
12349     while (PeekMessageA( &msg, 0, 0, 0, PM_REMOVE )) {
12350         if( msg.message == WM_QUIT) gotwmquit = TRUE;
12351         DispatchMessageA( &msg );
12352     }
12353     ok( gotwmquit == FALSE, "Unexpected WM_QUIT message!\n");
12354     DestroyWindow( hwnd);
12355 }
12356
12357 #define clear_clipboard(hwnd)  clear_clipboard_(__LINE__, (hwnd))
12358 static void clear_clipboard_(int line, HWND hWnd)
12359 {
12360     BOOL succ;
12361     succ = OpenClipboard(hWnd);
12362     ok_(__FILE__, line)(succ, "OpenClipboard failed, err=%u\n", GetLastError());
12363     succ = EmptyClipboard();
12364     ok_(__FILE__, line)(succ, "EmptyClipboard failed, err=%u\n", GetLastError());
12365     succ = CloseClipboard();
12366     ok_(__FILE__, line)(succ, "CloseClipboard failed, err=%u\n", GetLastError());
12367 }
12368
12369 #define expect_HWND(expected, got) expect_HWND_(__LINE__, (expected), (got))
12370 static void expect_HWND_(int line, HWND expected, HWND got)
12371 {
12372     ok_(__FILE__, line)(got==expected, "Expected %p, got %p\n", expected, got);
12373 }
12374
12375 static WNDPROC pOldViewerProc;
12376
12377 static LRESULT CALLBACK recursive_viewer_proc(HWND hWnd, UINT message, WPARAM wParam, LPARAM lParam)
12378 {
12379     static BOOL recursion_guard;
12380
12381     if (message == WM_DRAWCLIPBOARD && !recursion_guard)
12382     {
12383         recursion_guard = TRUE;
12384         clear_clipboard(hWnd);
12385         recursion_guard = FALSE;
12386     }
12387     return CallWindowProcA(pOldViewerProc, hWnd, message, wParam, lParam);
12388 }
12389
12390 static void test_clipboard_viewers(void)
12391 {
12392     static struct message wm_change_cb_chain[] =
12393     {
12394         { WM_CHANGECBCHAIN, sent|wparam|lparam, 0, 0 },
12395         { 0 }
12396     };
12397     static const struct message wm_clipboard_destroyed[] =
12398     {
12399         { WM_DESTROYCLIPBOARD, sent|wparam|lparam, 0, 0 },
12400         { 0 }
12401     };
12402     static struct message wm_clipboard_changed[] =
12403     {
12404         { WM_DRAWCLIPBOARD, sent|wparam|lparam, 0, 0 },
12405         { 0 }
12406     };
12407     static struct message wm_clipboard_changed_and_owned[] =
12408     {
12409         { WM_DESTROYCLIPBOARD, sent|wparam|lparam, 0, 0 },
12410         { WM_DRAWCLIPBOARD, sent|wparam|lparam, 0, 0 },
12411         { 0 }
12412     };
12413
12414     HINSTANCE hInst = GetModuleHandleA(NULL);
12415     HWND hWnd1, hWnd2, hWnd3;
12416     HWND hOrigViewer;
12417     HWND hRet;
12418
12419     hWnd1 = CreateWindowExA(0, "TestWindowClass", "Clipboard viewer test wnd 1",
12420         WS_CAPTION | WS_SYSMENU | WS_MINIMIZEBOX | WS_MAXIMIZEBOX,
12421         CW_USEDEFAULT, CW_USEDEFAULT, CW_USEDEFAULT, CW_USEDEFAULT,
12422         GetDesktopWindow(), NULL, hInst, NULL);
12423     hWnd2 = CreateWindowExA(0, "SimpleWindowClass", "Clipboard viewer test wnd 2",
12424         WS_CAPTION | WS_SYSMENU | WS_MINIMIZEBOX | WS_MAXIMIZEBOX,
12425         CW_USEDEFAULT, CW_USEDEFAULT, CW_USEDEFAULT, CW_USEDEFAULT,
12426         GetDesktopWindow(), NULL, hInst, NULL);
12427     hWnd3 = CreateWindowExA(0, "SimpleWindowClass", "Clipboard viewer test wnd 3",
12428         WS_CAPTION | WS_SYSMENU | WS_MINIMIZEBOX | WS_MAXIMIZEBOX,
12429         CW_USEDEFAULT, CW_USEDEFAULT, CW_USEDEFAULT, CW_USEDEFAULT,
12430         GetDesktopWindow(), NULL, hInst, NULL);
12431     trace("clipbd viewers: hWnd1=%p, hWnd2=%p, hWnd3=%p\n", hWnd1, hWnd2, hWnd3);
12432     assert(hWnd1 && hWnd2 && hWnd3);
12433
12434     flush_sequence();
12435
12436     /* Test getting the clipboard viewer and setting the viewer to NULL. */
12437     hOrigViewer = GetClipboardViewer();
12438     hRet = SetClipboardViewer(NULL);
12439     ok_sequence(WmEmptySeq, "set viewer to NULL", FALSE);
12440     expect_HWND(hOrigViewer, hRet);
12441     expect_HWND(NULL, GetClipboardViewer());
12442
12443     /* Test registering hWnd1 as a viewer. */
12444     hRet = SetClipboardViewer(hWnd1);
12445     wm_clipboard_changed[0].wParam = (WPARAM) GetClipboardOwner();
12446     ok_sequence(wm_clipboard_changed, "set viewer NULL->1", FALSE);
12447     expect_HWND(NULL, hRet);
12448     expect_HWND(hWnd1, GetClipboardViewer());
12449
12450     /* Test that changing the clipboard actually refreshes the registered viewer. */
12451     clear_clipboard(hWnd1);
12452     wm_clipboard_changed[0].wParam = (WPARAM) GetClipboardOwner();
12453     ok_sequence(wm_clipboard_changed, "clear clipbd (viewer=owner=1)", FALSE);
12454
12455     /* Again, but with different owner. */
12456     clear_clipboard(hWnd2);
12457     wm_clipboard_changed_and_owned[1].wParam = (WPARAM) GetClipboardOwner();
12458     ok_sequence(wm_clipboard_changed_and_owned, "clear clipbd (viewer=1, owner=2)", FALSE);
12459
12460     /* Test re-registering same window. */
12461     hRet = SetClipboardViewer(hWnd1);
12462     wm_clipboard_changed[0].wParam = (WPARAM) GetClipboardOwner();
12463     ok_sequence(wm_clipboard_changed, "set viewer 1->1", FALSE);
12464     expect_HWND(hWnd1, hRet);
12465     expect_HWND(hWnd1, GetClipboardViewer());
12466
12467     /* Test ChangeClipboardChain. */
12468     ChangeClipboardChain(hWnd2, hWnd3);
12469     wm_change_cb_chain[0].wParam = (WPARAM) hWnd2;
12470     wm_change_cb_chain[0].lParam = (LPARAM) hWnd3;
12471     ok_sequence(wm_change_cb_chain, "change chain (viewer=1, remove=2, next=3)", FALSE);
12472     expect_HWND(hWnd1, GetClipboardViewer());
12473
12474     ChangeClipboardChain(hWnd2, NULL);
12475     wm_change_cb_chain[0].wParam = (WPARAM) hWnd2;
12476     wm_change_cb_chain[0].lParam = 0;
12477     ok_sequence(wm_change_cb_chain, "change chain (viewer=1, remove=2, next=NULL)", FALSE);
12478     expect_HWND(hWnd1, GetClipboardViewer());
12479
12480     ChangeClipboardChain(NULL, hWnd2);
12481     ok_sequence(WmEmptySeq, "change chain (viewer=1, remove=NULL, next=2)", TRUE);
12482     expect_HWND(hWnd1, GetClipboardViewer());
12483
12484     /* Actually change clipboard viewer with ChangeClipboardChain. */
12485     ChangeClipboardChain(hWnd1, hWnd2);
12486     ok_sequence(WmEmptySeq, "change chain (viewer=remove=1, next=2)", FALSE);
12487     expect_HWND(hWnd2, GetClipboardViewer());
12488
12489     /* Test that no refresh messages are sent when viewer has unregistered. */
12490     clear_clipboard(hWnd2);
12491     ok_sequence(WmEmptySeq, "clear clipd (viewer=2, owner=1)", FALSE);
12492
12493     /* Register hWnd1 again. */
12494     ChangeClipboardChain(hWnd2, hWnd1);
12495     ok_sequence(WmEmptySeq, "change chain (viewer=remove=2, next=1)", FALSE);
12496     expect_HWND(hWnd1, GetClipboardViewer());
12497
12498     /* Subclass hWnd1 so that when it receives a WM_DRAWCLIPBOARD message, it
12499      * changes the clipboard. When this happens, the system shouldn't send
12500      * another WM_DRAWCLIPBOARD (as this could cause an infinite loop).
12501      */
12502     pOldViewerProc = (WNDPROC) SetWindowLongPtrA(hWnd1, GWLP_WNDPROC, (LONG_PTR) recursive_viewer_proc);
12503     clear_clipboard(hWnd2);
12504     /* The clipboard owner is changed in recursive_viewer_proc: */
12505     wm_clipboard_changed[0].wParam = (WPARAM) hWnd2;
12506     ok_sequence(wm_clipboard_changed, "recursive clear clipbd (viewer=1, owner=2)", TRUE);
12507
12508     /* Test unregistering. */
12509     ChangeClipboardChain(hWnd1, NULL);
12510     ok_sequence(WmEmptySeq, "change chain (viewer=remove=1, next=NULL)", FALSE);
12511     expect_HWND(NULL, GetClipboardViewer());
12512
12513     clear_clipboard(hWnd1);
12514     ok_sequence(wm_clipboard_destroyed, "clear clipbd (no viewer, owner=1)", FALSE);
12515
12516     DestroyWindow(hWnd1);
12517     DestroyWindow(hWnd2);
12518     DestroyWindow(hWnd3);
12519     SetClipboardViewer(hOrigViewer);
12520 }
12521
12522 static void test_PostMessage(void)
12523 {
12524     static const struct
12525     {
12526         HWND hwnd;
12527         BOOL ret;
12528     } data[] =
12529     {
12530         { HWND_TOP /* 0 */, TRUE },
12531         { HWND_BROADCAST, TRUE },
12532         { HWND_BOTTOM, TRUE },
12533         { HWND_TOPMOST, TRUE },
12534         { HWND_NOTOPMOST, FALSE },
12535         { HWND_MESSAGE, FALSE },
12536         { (HWND)0xdeadbeef, FALSE }
12537     };
12538     int i;
12539     HWND hwnd;
12540     BOOL ret;
12541     MSG msg;
12542     static const WCHAR staticW[] = {'s','t','a','t','i','c',0};
12543
12544     SetLastError(0xdeadbeef);
12545     hwnd = CreateWindowExW(0, staticW, NULL, WS_POPUP, 0,0,0,0,0,0,0, NULL);
12546     if (!hwnd && GetLastError() == ERROR_CALL_NOT_IMPLEMENTED)
12547     {
12548         win_skip("Skipping some PostMessage tests on Win9x/WinMe\n");
12549         return;
12550     }
12551     assert(hwnd);
12552
12553     flush_events();
12554
12555     PostMessage(hwnd, WM_USER+1, 0x1234, 0x5678);
12556     PostMessage(0, WM_USER+2, 0x5678, 0x1234);
12557
12558     for (i = 0; i < sizeof(data)/sizeof(data[0]); i++)
12559     {
12560         memset(&msg, 0xab, sizeof(msg));
12561         ret = PeekMessageA(&msg, data[i].hwnd, 0, 0, PM_NOREMOVE);
12562         ok(ret == data[i].ret, "%d: hwnd %p expected %d, got %d\n", i, data[i].hwnd, data[i].ret, ret);
12563         if (data[i].ret)
12564         {
12565             if (data[i].hwnd)
12566                 ok(ret && msg.hwnd == 0 && msg.message == WM_USER+2 &&
12567                    msg.wParam == 0x5678 && msg.lParam == 0x1234,
12568                    "%d: got ret %d hwnd %p msg %04x wParam %08lx lParam %08lx instead of TRUE/0/WM_USER+2/0x5678/0x1234\n",
12569                    i, ret, msg.hwnd, msg.message, msg.wParam, msg.lParam);
12570             else
12571                 ok(ret && msg.hwnd == hwnd && msg.message == WM_USER+1 &&
12572                    msg.wParam == 0x1234 && msg.lParam == 0x5678,
12573                    "%d: got ret %d hwnd %p msg %04x wParam %08lx lParam %08lx instead of TRUE/%p/WM_USER+1/0x1234/0x5678\n",
12574                    i, ret, msg.hwnd, msg.message, msg.wParam, msg.lParam, msg.hwnd);
12575         }
12576     }
12577
12578     DestroyWindow(hwnd);
12579     flush_events();
12580 }
12581
12582 static const struct
12583 {
12584     DWORD exp, broken;
12585     BOOL todo;
12586 } wait_idle_expect[] =
12587 {
12588 /* 0 */  { WAIT_TIMEOUT, WAIT_TIMEOUT, FALSE },
12589          { WAIT_TIMEOUT, 0,            FALSE },
12590          { WAIT_TIMEOUT, 0,            FALSE },
12591          { WAIT_TIMEOUT, WAIT_TIMEOUT, FALSE },
12592          { WAIT_TIMEOUT, WAIT_TIMEOUT, FALSE },
12593 /* 5 */  { WAIT_TIMEOUT, 0,            FALSE },
12594          { WAIT_TIMEOUT, 0,            FALSE },
12595          { WAIT_TIMEOUT, WAIT_TIMEOUT, FALSE },
12596          { 0,            0,            FALSE },
12597          { 0,            0,            FALSE },
12598 /* 10 */ { 0,            0,            FALSE },
12599          { 0,            0,            FALSE },
12600          { 0,            WAIT_TIMEOUT, FALSE },
12601          { 0,            0,            FALSE },
12602          { 0,            0,            FALSE },
12603 /* 15 */ { 0,            0,            FALSE },
12604          { WAIT_TIMEOUT, 0,            FALSE },
12605          { WAIT_TIMEOUT, 0,            FALSE },
12606          { WAIT_TIMEOUT, 0,            FALSE },
12607          { WAIT_TIMEOUT, 0,            FALSE },
12608 /* 20 */ { WAIT_TIMEOUT, 0,            FALSE },
12609 };
12610
12611 static DWORD CALLBACK do_wait_idle_child_thread( void *arg )
12612 {
12613     MSG msg;
12614
12615     PeekMessage( &msg, 0, 0, 0, PM_NOREMOVE );
12616     Sleep( 200 );
12617     MsgWaitForMultipleObjects( 0, NULL, FALSE, 100, QS_ALLINPUT );
12618     return 0;
12619 }
12620
12621 static void do_wait_idle_child( int arg )
12622 {
12623     WNDCLASS cls;
12624     MSG msg;
12625     HWND hwnd = 0;
12626     HANDLE thread;
12627     DWORD id;
12628     HANDLE start_event = OpenEventA( EVENT_ALL_ACCESS, FALSE, "test_WaitForInputIdle_start" );
12629     HANDLE end_event = OpenEventA( EVENT_ALL_ACCESS, FALSE, "test_WaitForInputIdle_end" );
12630
12631     memset( &cls, 0, sizeof(cls) );
12632     cls.lpfnWndProc   = DefWindowProc;
12633     cls.hbrBackground = (HBRUSH)(COLOR_WINDOW + 1);
12634     cls.hCursor       = LoadCursor(0, IDC_ARROW);
12635     cls.lpszClassName = "TestClass";
12636     RegisterClass( &cls );
12637
12638     PeekMessage( &msg, 0, 0, 0, PM_NOREMOVE );  /* create the msg queue */
12639
12640     ok( start_event != 0, "failed to create start event, error %u\n", GetLastError() );
12641     ok( end_event != 0, "failed to create end event, error %u\n", GetLastError() );
12642
12643     switch (arg)
12644     {
12645     case 0:
12646         SetEvent( start_event );
12647         break;
12648     case 1:
12649         SetEvent( start_event );
12650         Sleep( 200 );
12651         PeekMessage( &msg, 0, 0, 0, PM_REMOVE );
12652         break;
12653     case 2:
12654         SetEvent( start_event );
12655         Sleep( 200 );
12656         PeekMessage( &msg, 0, 0, 0, PM_NOREMOVE );
12657         PostThreadMessage( GetCurrentThreadId(), WM_COMMAND, 0x1234, 0xabcd );
12658         PeekMessage( &msg, 0, 0, 0, PM_REMOVE );
12659         break;
12660     case 3:
12661         SetEvent( start_event );
12662         Sleep( 200 );
12663         SendMessage( HWND_BROADCAST, WM_WININICHANGE, 0, 0 );
12664         break;
12665     case 4:
12666         SetEvent( start_event );
12667         Sleep( 200 );
12668         hwnd = CreateWindowExA(0, "TestClass", NULL, WS_POPUP|WS_VISIBLE, 0, 0, 10, 10, 0, 0, 0, NULL);
12669         while (PeekMessage( &msg, 0, 0, 0, PM_REMOVE|PM_NOYIELD )) DispatchMessage( &msg );
12670         break;
12671     case 5:
12672         SetEvent( start_event );
12673         Sleep( 200 );
12674         hwnd = CreateWindowExA(0, "TestClass", NULL, WS_POPUP|WS_VISIBLE, 0, 0, 10, 10, 0, 0, 0, NULL);
12675         while (PeekMessage( &msg, 0, 0, 0, PM_REMOVE )) DispatchMessage( &msg );
12676         break;
12677     case 6:
12678         SetEvent( start_event );
12679         Sleep( 200 );
12680         hwnd = CreateWindowExA(0, "TestClass", NULL, WS_POPUP|WS_VISIBLE, 0, 0, 10, 10, 0, 0, 0, NULL);
12681         while (PeekMessage( &msg, 0, 0, 0, PM_NOREMOVE ))
12682         {
12683             GetMessage( &msg, 0, 0, 0 );
12684             DispatchMessage( &msg );
12685         }
12686         break;
12687     case 7:
12688         SetEvent( start_event );
12689         Sleep( 200 );
12690         hwnd = CreateWindowExA(0, "TestClass", NULL, WS_POPUP|WS_VISIBLE, 0, 0, 10, 10, 0, 0, 0, NULL);
12691         SetTimer( hwnd, 3, 1, NULL );
12692         Sleep( 200 );
12693         while (PeekMessage( &msg, 0, 0, 0, PM_REMOVE|PM_NOYIELD )) DispatchMessage( &msg );
12694         break;
12695     case 8:
12696         SetEvent( start_event );
12697         Sleep( 200 );
12698         PeekMessage( &msg, 0, 0, 0, PM_NOREMOVE );
12699         MsgWaitForMultipleObjects( 0, NULL, FALSE, 100, QS_ALLINPUT );
12700         break;
12701     case 9:
12702         SetEvent( start_event );
12703         Sleep( 200 );
12704         hwnd = CreateWindowExA(0, "TestClass", NULL, WS_POPUP|WS_VISIBLE, 0, 0, 10, 10, 0, 0, 0, NULL);
12705         while (PeekMessage( &msg, 0, 0, 0, PM_REMOVE )) DispatchMessage( &msg );
12706         for (;;) GetMessage( &msg, 0, 0, 0 );
12707         break;
12708     case 10:
12709         SetEvent( start_event );
12710         Sleep( 200 );
12711         hwnd = CreateWindowExA(0, "TestClass", NULL, WS_POPUP|WS_VISIBLE, 0, 0, 10, 10, 0, 0, 0, NULL);
12712         SetTimer( hwnd, 3, 1, NULL );
12713         Sleep( 200 );
12714         while (PeekMessage( &msg, 0, 0, 0, PM_REMOVE )) DispatchMessage( &msg );
12715         break;
12716     case 11:
12717         SetEvent( start_event );
12718         Sleep( 200 );
12719         return;  /* exiting the process makes WaitForInputIdle return success too */
12720     case 12:
12721         PeekMessage( &msg, 0, 0, 0, PM_NOREMOVE );
12722         Sleep( 200 );
12723         MsgWaitForMultipleObjects( 0, NULL, FALSE, 100, QS_ALLINPUT );
12724         SetEvent( start_event );
12725         break;
12726     case 13:
12727         SetEvent( start_event );
12728         PeekMessage( &msg, 0, 0, 0, PM_NOREMOVE );
12729         Sleep( 200 );
12730         thread = CreateThread( NULL, 0, do_wait_idle_child_thread, NULL, 0, &id );
12731         WaitForSingleObject( thread, 10000 );
12732         CloseHandle( thread );
12733         break;
12734     case 14:
12735         SetEvent( start_event );
12736         Sleep( 200 );
12737         PeekMessage( &msg, HWND_TOPMOST, 0, 0, PM_NOREMOVE );
12738         break;
12739     case 15:
12740         SetEvent( start_event );
12741         Sleep( 200 );
12742         PeekMessage( &msg, HWND_BROADCAST, 0, 0, PM_NOREMOVE );
12743         break;
12744     case 16:
12745         SetEvent( start_event );
12746         Sleep( 200 );
12747         PeekMessage( &msg, HWND_BOTTOM, 0, 0, PM_NOREMOVE );
12748         break;
12749     case 17:
12750         SetEvent( start_event );
12751         Sleep( 200 );
12752         PeekMessage( &msg, (HWND)0xdeadbeef, 0, 0, PM_NOREMOVE );
12753         break;
12754     case 18:
12755         SetEvent( start_event );
12756         Sleep( 200 );
12757         PeekMessage( &msg, HWND_NOTOPMOST, 0, 0, PM_NOREMOVE );
12758         break;
12759     case 19:
12760         SetEvent( start_event );
12761         Sleep( 200 );
12762         PeekMessage( &msg, HWND_MESSAGE, 0, 0, PM_NOREMOVE );
12763         break;
12764     case 20:
12765         SetEvent( start_event );
12766         Sleep( 200 );
12767         PeekMessage( &msg, GetDesktopWindow(), 0, 0, PM_NOREMOVE );
12768         break;
12769     }
12770     WaitForSingleObject( end_event, 2000 );
12771     CloseHandle( start_event );
12772     CloseHandle( end_event );
12773     if (hwnd) DestroyWindow( hwnd );
12774 }
12775
12776 static LRESULT CALLBACK wait_idle_proc( HWND hwnd, UINT msg, WPARAM wp, LPARAM lp )
12777 {
12778     if (msg == WM_WININICHANGE) Sleep( 200 );  /* make sure the child waits */
12779     return DefWindowProcA( hwnd, msg, wp, lp );
12780 }
12781
12782 static DWORD CALLBACK wait_idle_thread( void *arg )
12783 {
12784     WNDCLASS cls;
12785     MSG msg;
12786     HWND hwnd;
12787
12788     memset( &cls, 0, sizeof(cls) );
12789     cls.lpfnWndProc   = wait_idle_proc;
12790     cls.hbrBackground = (HBRUSH)(COLOR_WINDOW + 1);
12791     cls.hCursor       = LoadCursor(0, IDC_ARROW);
12792     cls.lpszClassName = "TestClass";
12793     RegisterClass( &cls );
12794
12795     hwnd = CreateWindowExA(0, "TestClass", NULL, WS_POPUP, 0, 0, 10, 10, 0, 0, 0, NULL);
12796     while (GetMessage( &msg, 0, 0, 0 )) DispatchMessage( &msg );
12797     DestroyWindow(hwnd);
12798     return 0;
12799 }
12800
12801 static void test_WaitForInputIdle( char *argv0 )
12802 {
12803     char path[MAX_PATH];
12804     PROCESS_INFORMATION pi;
12805     STARTUPINFOA startup;
12806     BOOL ret;
12807     HANDLE start_event, end_event, thread;
12808     unsigned int i;
12809     DWORD id;
12810     const IMAGE_DOS_HEADER *dos = (const IMAGE_DOS_HEADER *)GetModuleHandleA(0);
12811     const IMAGE_NT_HEADERS *nt = (const IMAGE_NT_HEADERS *)((const char *)dos + dos->e_lfanew);
12812     BOOL console_app = (nt->OptionalHeader.Subsystem != IMAGE_SUBSYSTEM_WINDOWS_GUI);
12813
12814     if (console_app)  /* build the test with -mwindows for better coverage */
12815         trace( "not built as a GUI app, WaitForInputIdle may not be fully tested\n" );
12816
12817     start_event = CreateEventA(NULL, 0, 0, "test_WaitForInputIdle_start");
12818     end_event = CreateEventA(NULL, 0, 0, "test_WaitForInputIdle_end");
12819     ok(start_event != 0, "failed to create start event, error %u\n", GetLastError());
12820     ok(end_event != 0, "failed to create end event, error %u\n", GetLastError());
12821
12822     memset( &startup, 0, sizeof(startup) );
12823     startup.cb = sizeof(startup);
12824     startup.dwFlags = STARTF_USESHOWWINDOW;
12825     startup.wShowWindow = SW_SHOWNORMAL;
12826
12827     thread = CreateThread( NULL, 0, wait_idle_thread, NULL, 0, &id );
12828
12829     for (i = 0; i < sizeof(wait_idle_expect)/sizeof(wait_idle_expect[0]); i++)
12830     {
12831         ResetEvent( start_event );
12832         ResetEvent( end_event );
12833         sprintf( path, "%s msg %u", argv0, i );
12834         ret = CreateProcessA( NULL, path, NULL, NULL, TRUE, 0, NULL, NULL, &startup, &pi );
12835         ok( ret, "CreateProcess '%s' failed err %u.\n", path, GetLastError() );
12836         if (ret)
12837         {
12838             ret = WaitForSingleObject( start_event, 5000 );
12839             ok( ret == WAIT_OBJECT_0, "%u: WaitForSingleObject failed\n", i );
12840             if (ret == WAIT_OBJECT_0)
12841             {
12842                 ret = WaitForInputIdle( pi.hProcess, 1000 );
12843                 if (ret == WAIT_FAILED)
12844                     ok( console_app ||
12845                         ret == wait_idle_expect[i].exp ||
12846                         broken(ret == wait_idle_expect[i].broken),
12847                         "%u: WaitForInputIdle error %08x expected %08x\n",
12848                         i, ret, wait_idle_expect[i].exp );
12849                 else if (wait_idle_expect[i].todo)
12850                     todo_wine
12851                     ok( ret == wait_idle_expect[i].exp || broken(ret == wait_idle_expect[i].broken),
12852                         "%u: WaitForInputIdle error %08x expected %08x\n",
12853                         i, ret, wait_idle_expect[i].exp );
12854                 else
12855                     ok( ret == wait_idle_expect[i].exp || broken(ret == wait_idle_expect[i].broken),
12856                         "%u: WaitForInputIdle error %08x expected %08x\n",
12857                         i, ret, wait_idle_expect[i].exp );
12858                 SetEvent( end_event );
12859                 WaitForSingleObject( pi.hProcess, 1000 );  /* give it a chance to exit on its own */
12860             }
12861             TerminateProcess( pi.hProcess, 0 );  /* just in case */
12862             winetest_wait_child_process( pi.hProcess );
12863             ret = WaitForInputIdle( pi.hProcess, 100 );
12864             ok( ret == WAIT_FAILED, "%u: WaitForInputIdle after exit error %08x\n", i, ret );
12865             CloseHandle( pi.hProcess );
12866             CloseHandle( pi.hThread );
12867         }
12868     }
12869     CloseHandle( start_event );
12870     PostThreadMessage( id, WM_QUIT, 0, 0 );
12871     WaitForSingleObject( thread, 10000 );
12872     CloseHandle( thread );
12873 }
12874
12875 static const struct message WmSetParentSeq_1[] = {
12876     { WM_SHOWWINDOW, sent|wparam, 0 },
12877     { EVENT_OBJECT_PARENTCHANGE, winevent_hook|wparam|lparam, 0, 0 },
12878     { WM_WINDOWPOSCHANGING, sent|wparam, SWP_NOSIZE },
12879     { WM_CHILDACTIVATE, sent },
12880     { WM_WINDOWPOSCHANGED, sent|wparam, SWP_NOSIZE|SWP_NOREDRAW|SWP_NOCLIENTSIZE },
12881     { WM_MOVE, sent|defwinproc|wparam, 0 },
12882     { EVENT_OBJECT_LOCATIONCHANGE, winevent_hook|wparam|lparam, 0, 0 },
12883     { WM_SHOWWINDOW, sent|wparam, 1 },
12884     { 0 }
12885 };
12886
12887 static const struct message WmSetParentSeq_2[] = {
12888     { WM_SHOWWINDOW, sent|wparam, 0 },
12889     { WM_WINDOWPOSCHANGING, sent|wparam, SWP_HIDEWINDOW|SWP_NOSIZE|SWP_NOMOVE },
12890     { EVENT_OBJECT_HIDE, winevent_hook|wparam|lparam, 0, 0 },
12891     { WM_WINDOWPOSCHANGED, sent|wparam, SWP_HIDEWINDOW|SWP_NOSIZE|SWP_NOMOVE|SWP_NOCLIENTSIZE|SWP_NOCLIENTMOVE },
12892     { HCBT_SETFOCUS, hook|optional },
12893     { WM_NCACTIVATE, sent|wparam|optional, 0 },
12894     { WM_ACTIVATE, sent|wparam|optional, 0 },
12895     { WM_ACTIVATEAPP, sent|wparam|optional, 0 },
12896     { WM_KILLFOCUS, sent|wparam, 0 },
12897     { EVENT_OBJECT_PARENTCHANGE, winevent_hook|wparam|lparam, 0, 0 },
12898     { WM_WINDOWPOSCHANGING, sent|wparam, SWP_NOSIZE },
12899     { HCBT_ACTIVATE, hook|optional },
12900     { EVENT_SYSTEM_FOREGROUND, winevent_hook|wparam|lparam, 0, 0 },
12901     { WM_WINDOWPOSCHANGING, sent|wparam|optional, SWP_NOSIZE|SWP_NOMOVE },
12902     { WM_NCACTIVATE, sent|wparam|optional, 1 },
12903     { WM_ACTIVATE, sent|wparam|optional, 1 },
12904     { HCBT_SETFOCUS, hook|optional },
12905     { EVENT_OBJECT_FOCUS, winevent_hook|wparam|lparam, OBJID_CLIENT, 0 },
12906     { WM_SETFOCUS, sent|optional|defwinproc },
12907     { WM_WINDOWPOSCHANGED, sent|wparam, SWP_NOREDRAW|SWP_NOSIZE|SWP_NOCLIENTSIZE },
12908     { WM_MOVE, sent|defwinproc|wparam, 0 },
12909     { EVENT_OBJECT_LOCATIONCHANGE, winevent_hook|wparam|lparam, 0, 0 },
12910     { WM_SHOWWINDOW, sent|wparam, 1 },
12911     { WM_WINDOWPOSCHANGING, sent|wparam, SWP_SHOWWINDOW|SWP_NOSIZE|SWP_NOMOVE },
12912     { EVENT_OBJECT_SHOW, winevent_hook|wparam|lparam, 0, 0 },
12913     { WM_WINDOWPOSCHANGED, sent|wparam, SWP_SHOWWINDOW|SWP_NOSIZE|SWP_NOMOVE|SWP_NOCLIENTSIZE|SWP_NOCLIENTMOVE },
12914     { 0 }
12915 };
12916
12917
12918 static void test_SetParent(void)
12919 {
12920     HWND parent1, parent2, child, popup;
12921     RECT rc, rc_old;
12922
12923     parent1 = CreateWindowEx(0, "TestParentClass", NULL, WS_OVERLAPPEDWINDOW,
12924                             100, 100, 200, 200, 0, 0, 0, NULL);
12925     ok(parent1 != 0, "Failed to create parent1 window\n");
12926
12927     parent2 = CreateWindowEx(0, "TestParentClass", NULL, WS_OVERLAPPEDWINDOW,
12928                             400, 100, 200, 200, 0, 0, 0, NULL);
12929     ok(parent2 != 0, "Failed to create parent2 window\n");
12930
12931     /* WS_CHILD window */
12932     child = CreateWindowEx(0, "TestWindowClass", NULL, WS_CHILD | WS_VISIBLE,
12933                            10, 10, 150, 150, parent1, 0, 0, NULL);
12934     ok(child != 0, "Failed to create child window\n");
12935
12936     GetWindowRect(parent1, &rc);
12937     trace("parent1 (%d,%d)-(%d,%d)\n", rc.left, rc.top, rc.right, rc.bottom);
12938     GetWindowRect(child, &rc_old);
12939     MapWindowPoints(0, parent1, (POINT *)&rc_old, 2);
12940     trace("child (%d,%d)-(%d,%d)\n", rc_old.left, rc_old.top, rc_old.right, rc_old.bottom);
12941
12942     flush_sequence();
12943
12944     SetParent(child, parent2);
12945     flush_events();
12946     ok_sequence(WmSetParentSeq_1, "SetParent() visible WS_CHILD", TRUE);
12947
12948     ok(GetWindowLongA(child, GWL_STYLE) & WS_VISIBLE, "WS_VISIBLE should be set\n");
12949     ok(!IsWindowVisible(child), "IsWindowVisible() should return FALSE\n");
12950
12951     GetWindowRect(parent2, &rc);
12952     trace("parent2 (%d,%d)-(%d,%d)\n", rc.left, rc.top, rc.right, rc.bottom);
12953     GetWindowRect(child, &rc);
12954     MapWindowPoints(0, parent2, (POINT *)&rc, 2);
12955     trace("child (%d,%d)-(%d,%d)\n", rc.left, rc.top, rc.right, rc.bottom);
12956
12957     ok(EqualRect(&rc_old, &rc), "rects do not match (%d,%d-%d,%d) / (%d,%d-%d,%d)\n",
12958        rc_old.left, rc_old.top, rc_old.right, rc_old.bottom,
12959        rc.left, rc.top, rc.right, rc.bottom );
12960
12961     /* WS_POPUP window */
12962     popup = CreateWindowEx(0, "TestWindowClass", NULL, WS_POPUP | WS_VISIBLE,
12963                            20, 20, 100, 100, 0, 0, 0, NULL);
12964     ok(popup != 0, "Failed to create popup window\n");
12965
12966     GetWindowRect(popup, &rc_old);
12967     trace("popup (%d,%d)-(%d,%d)\n", rc_old.left, rc_old.top, rc_old.right, rc_old.bottom);
12968
12969     flush_sequence();
12970
12971     SetParent(popup, child);
12972     flush_events();
12973     ok_sequence(WmSetParentSeq_2, "SetParent() visible WS_POPUP", TRUE);
12974
12975     ok(GetWindowLongA(popup, GWL_STYLE) & WS_VISIBLE, "WS_VISIBLE should be set\n");
12976     ok(!IsWindowVisible(popup), "IsWindowVisible() should return FALSE\n");
12977
12978     GetWindowRect(child, &rc);
12979     trace("parent2 (%d,%d)-(%d,%d)\n", rc.left, rc.top, rc.right, rc.bottom);
12980     GetWindowRect(popup, &rc);
12981     MapWindowPoints(0, child, (POINT *)&rc, 2);
12982     trace("popup (%d,%d)-(%d,%d)\n", rc.left, rc.top, rc.right, rc.bottom);
12983
12984     ok(EqualRect(&rc_old, &rc), "rects do not match (%d,%d-%d,%d) / (%d,%d-%d,%d)\n",
12985        rc_old.left, rc_old.top, rc_old.right, rc_old.bottom,
12986        rc.left, rc.top, rc.right, rc.bottom );
12987
12988     DestroyWindow(popup);
12989     DestroyWindow(child);
12990     DestroyWindow(parent1);
12991     DestroyWindow(parent2);
12992
12993     flush_sequence();
12994 }
12995
12996 static const struct message WmKeyReleaseOnly[] = {
12997     { HCBT_KEYSKIPPED, hook|wparam|lparam|optional, 0x41, 0x80000001 },
12998     { WM_KEYUP, sent|wparam|lparam, 0x41, 0x80000001 },
12999     { 0 }
13000 };
13001 static const struct message WmKeyPressNormal[] = {
13002     { HCBT_KEYSKIPPED, hook|wparam|lparam|optional, 0x41, 0x1 },
13003     { WM_KEYDOWN, sent|wparam|lparam, 0x41, 0x1 },
13004     { 0 }
13005 };
13006 static const struct message WmKeyPressRepeat[] = {
13007     { HCBT_KEYSKIPPED, hook|wparam|lparam|optional, 0x41, 0x40000001 },
13008     { WM_KEYDOWN, sent|wparam|lparam, 0x41, 0x40000001 },
13009     { 0 }
13010 };
13011 static const struct message WmKeyReleaseNormal[] = {
13012     { HCBT_KEYSKIPPED, hook|wparam|lparam|optional, 0x41, 0xc0000001 },
13013     { WM_KEYUP, sent|wparam|lparam, 0x41, 0xc0000001 },
13014     { 0 }
13015 };
13016
13017 static void test_keyflags(void)
13018 {
13019     HWND test_window;
13020     SHORT key_state;
13021     BYTE keyboard_state[256];
13022     MSG msg;
13023
13024     test_window = CreateWindowEx(0, "TestWindowClass", NULL, WS_OVERLAPPEDWINDOW | WS_VISIBLE,
13025                            100, 100, 200, 200, 0, 0, 0, NULL);
13026
13027     flush_events();
13028     flush_sequence();
13029
13030     /* keyup without a keydown */
13031     keybd_event(0x41, 0, KEYEVENTF_KEYUP, 0);
13032     while (PeekMessage(&msg, NULL, WM_KEYFIRST, WM_KEYLAST, PM_REMOVE))
13033         DispatchMessage(&msg);
13034     ok_sequence(WmKeyReleaseOnly, "key release only", TRUE);
13035
13036     key_state = GetAsyncKeyState(0x41);
13037     ok((key_state & 0x8000) == 0, "unexpected key state %x\n", key_state);
13038
13039     key_state = GetKeyState(0x41);
13040     ok((key_state & 0x8000) == 0, "unexpected key state %x\n", key_state);
13041
13042     /* keydown */
13043     keybd_event(0x41, 0, 0, 0);
13044     while (PeekMessage(&msg, NULL, WM_KEYFIRST, WM_KEYLAST, PM_REMOVE))
13045         DispatchMessage(&msg);
13046     ok_sequence(WmKeyPressNormal, "key press only", FALSE);
13047
13048     key_state = GetAsyncKeyState(0x41);
13049     ok((key_state & 0x8000) == 0x8000, "unexpected key state %x\n", key_state);
13050
13051     key_state = GetKeyState(0x41);
13052     ok((key_state & 0x8000) == 0x8000, "unexpected key state %x\n", key_state);
13053
13054     /* keydown repeat */
13055     keybd_event(0x41, 0, 0, 0);
13056     while (PeekMessage(&msg, NULL, WM_KEYFIRST, WM_KEYLAST, PM_REMOVE))
13057         DispatchMessage(&msg);
13058     ok_sequence(WmKeyPressRepeat, "key press repeat", FALSE);
13059
13060     key_state = GetAsyncKeyState(0x41);
13061     ok((key_state & 0x8000) == 0x8000, "unexpected key state %x\n", key_state);
13062
13063     key_state = GetKeyState(0x41);
13064     ok((key_state & 0x8000) == 0x8000, "unexpected key state %x\n", key_state);
13065
13066     /* keyup */
13067     keybd_event(0x41, 0, KEYEVENTF_KEYUP, 0);
13068     while (PeekMessage(&msg, NULL, WM_KEYFIRST, WM_KEYLAST, PM_REMOVE))
13069         DispatchMessage(&msg);
13070     ok_sequence(WmKeyReleaseNormal, "key release repeat", FALSE);
13071
13072     key_state = GetAsyncKeyState(0x41);
13073     ok((key_state & 0x8000) == 0, "unexpected key state %x\n", key_state);
13074
13075     key_state = GetKeyState(0x41);
13076     ok((key_state & 0x8000) == 0, "unexpected key state %x\n", key_state);
13077
13078     /* set the key state in this thread */
13079     GetKeyboardState(keyboard_state);
13080     keyboard_state[0x41] = 0x80;
13081     SetKeyboardState(keyboard_state);
13082
13083     key_state = GetAsyncKeyState(0x41);
13084     ok((key_state & 0x8000) == 0, "unexpected key state %x\n", key_state);
13085
13086     /* keydown */
13087     keybd_event(0x41, 0, 0, 0);
13088     while (PeekMessage(&msg, NULL, WM_KEYFIRST, WM_KEYLAST, PM_REMOVE))
13089         DispatchMessage(&msg);
13090     ok_sequence(WmKeyPressRepeat, "key press after setkeyboardstate", TRUE);
13091
13092     key_state = GetAsyncKeyState(0x41);
13093     ok((key_state & 0x8000) == 0x8000, "unexpected key state %x\n", key_state);
13094
13095     key_state = GetKeyState(0x41);
13096     ok((key_state & 0x8000) == 0x8000, "unexpected key state %x\n", key_state);
13097
13098     /* clear the key state in this thread */
13099     GetKeyboardState(keyboard_state);
13100     keyboard_state[0x41] = 0;
13101     SetKeyboardState(keyboard_state);
13102
13103     key_state = GetAsyncKeyState(0x41);
13104     ok((key_state & 0x8000) == 0x8000, "unexpected key state %x\n", key_state);
13105
13106     /* keyup */
13107     keybd_event(0x41, 0, KEYEVENTF_KEYUP, 0);
13108     while (PeekMessage(&msg, NULL, WM_KEYFIRST, WM_KEYLAST, PM_REMOVE))
13109         DispatchMessage(&msg);
13110     ok_sequence(WmKeyReleaseOnly, "key release after setkeyboardstate", TRUE);
13111
13112     key_state = GetAsyncKeyState(0x41);
13113     ok((key_state & 0x8000) == 0, "unexpected key state %x\n", key_state);
13114
13115     key_state = GetKeyState(0x41);
13116     ok((key_state & 0x8000) == 0, "unexpected key state %x\n", key_state);
13117
13118     DestroyWindow(test_window);
13119     flush_sequence();
13120 }
13121
13122 static const struct message WmHotkeyPressLWIN[] = {
13123     { WM_KEYDOWN, kbd_hook|wparam|lparam, VK_LWIN, LLKHF_INJECTED },
13124     { HCBT_KEYSKIPPED, hook|wparam|lparam|optional, VK_LWIN, 1 },
13125     { WM_KEYDOWN, sent|wparam|lparam, VK_LWIN, 1 },
13126     { 0 }
13127 };
13128 static const struct message WmHotkeyPress[] = {
13129     { WM_KEYDOWN, kbd_hook|lparam, 0, LLKHF_INJECTED },
13130     { WM_HOTKEY, sent|wparam, 5 },
13131     { 0 }
13132 };
13133 static const struct message WmHotkeyRelease[] = {
13134     { WM_KEYUP, kbd_hook|lparam, 0, LLKHF_INJECTED|LLKHF_UP },
13135     { HCBT_KEYSKIPPED, hook|lparam|optional, 0, 0x80000001 },
13136     { WM_KEYUP, sent|lparam, 0, 0x80000001 },
13137     { 0 }
13138 };
13139 static const struct message WmHotkeyReleaseLWIN[] = {
13140     { WM_KEYUP, kbd_hook|wparam|lparam, VK_LWIN, LLKHF_INJECTED|LLKHF_UP },
13141     { HCBT_KEYSKIPPED, hook|wparam|lparam|optional, VK_LWIN, 0xc0000001 },
13142     { WM_KEYUP, sent|wparam|lparam, VK_LWIN, 0xc0000001 },
13143     { 0 }
13144 };
13145 static const struct message WmHotkeyCombined[] = {
13146     { WM_KEYDOWN, kbd_hook|wparam|lparam, VK_LWIN, LLKHF_INJECTED },
13147     { WM_KEYDOWN, kbd_hook|lparam, 0, LLKHF_INJECTED },
13148     { WM_KEYUP, kbd_hook|lparam, 0, LLKHF_INJECTED|LLKHF_UP },
13149     { WM_KEYUP, kbd_hook|wparam|lparam, VK_LWIN, LLKHF_INJECTED|LLKHF_UP },
13150     { WM_APP, sent, 0, 0 },
13151     { WM_HOTKEY, sent|wparam, 5 },
13152     { WM_APP+1, sent, 0, 0 },
13153     { HCBT_KEYSKIPPED, hook|wparam|lparam|optional, VK_LWIN, 1 },
13154     { WM_KEYDOWN, sent|wparam|lparam, VK_LWIN, 1 },
13155     { HCBT_KEYSKIPPED, hook|optional, 0, 0x80000001 },
13156     { WM_KEYUP, sent, 0, 0x80000001 }, /* lparam not checked so the sequence isn't a todo */
13157     { HCBT_KEYSKIPPED, hook|wparam|lparam|optional, VK_LWIN, 0xc0000001 },
13158     { WM_KEYUP, sent|wparam|lparam, VK_LWIN, 0xc0000001 },
13159     { 0 }
13160 };
13161 static const struct message WmHotkeyPrevious[] = {
13162     { WM_KEYDOWN, kbd_hook|wparam|lparam, VK_LWIN, LLKHF_INJECTED },
13163     { WM_KEYDOWN, kbd_hook|lparam, 0, LLKHF_INJECTED },
13164     { WM_KEYUP, kbd_hook|lparam, 0, LLKHF_INJECTED|LLKHF_UP },
13165     { WM_KEYUP, kbd_hook|wparam|lparam, VK_LWIN, LLKHF_INJECTED|LLKHF_UP },
13166     { HCBT_KEYSKIPPED, hook|wparam|lparam|optional, VK_LWIN, 1 },
13167     { WM_KEYDOWN, sent|wparam|lparam, VK_LWIN, 1 },
13168     { HCBT_KEYSKIPPED, hook|lparam|optional, 0, 1 },
13169     { WM_KEYDOWN, sent|lparam, 0, 1 },
13170     { HCBT_KEYSKIPPED, hook|optional|lparam, 0, 0xc0000001 },
13171     { WM_KEYUP, sent|lparam, 0, 0xc0000001 },
13172     { HCBT_KEYSKIPPED, hook|wparam|lparam|optional, VK_LWIN, 0xc0000001 },
13173     { WM_KEYUP, sent|wparam|lparam, VK_LWIN, 0xc0000001 },
13174     { 0 }
13175 };
13176 static const struct message WmHotkeyNew[] = {
13177     { WM_KEYDOWN, kbd_hook|lparam, 0, LLKHF_INJECTED },
13178     { WM_KEYUP, kbd_hook|lparam, 0, LLKHF_INJECTED|LLKHF_UP },
13179     { WM_HOTKEY, sent|wparam, 5 },
13180     { HCBT_KEYSKIPPED, hook|optional, 0, 0x80000001 },
13181     { WM_KEYUP, sent, 0, 0x80000001 }, /* lparam not checked so the sequence isn't a todo */
13182     { 0 }
13183 };
13184
13185 static int hotkey_letter;
13186
13187 static LRESULT CALLBACK KeyboardHookProc(int nCode, WPARAM wParam, LPARAM lParam)
13188 {
13189     struct recvd_message msg;
13190
13191     if (nCode == HC_ACTION)
13192     {
13193         KBDLLHOOKSTRUCT *kdbhookstruct = (KBDLLHOOKSTRUCT*)lParam;
13194
13195         msg.hwnd = 0;
13196         msg.message = wParam;
13197         msg.flags = kbd_hook|wparam|lparam;
13198         msg.wParam = kdbhookstruct->vkCode;
13199         msg.lParam = kdbhookstruct->flags;
13200         msg.descr = "KeyboardHookProc";
13201         add_message(&msg);
13202
13203         if (wParam == WM_KEYUP || wParam == WM_KEYDOWN)
13204         {
13205             ok(kdbhookstruct->vkCode == VK_LWIN || kdbhookstruct->vkCode == hotkey_letter,
13206                "unexpected keycode %x\n", kdbhookstruct->vkCode);
13207        }
13208     }
13209
13210     return CallNextHookEx(hKBD_hook, nCode, wParam, lParam);
13211 }
13212
13213 static void test_hotkey(void)
13214 {
13215     HWND test_window, taskbar_window;
13216     BOOL ret;
13217     MSG msg;
13218     DWORD queue_status;
13219     SHORT key_state;
13220
13221     SetLastError(0xdeadbeef);
13222     ret = UnregisterHotKey(NULL, 0);
13223     ok(ret == FALSE, "expected FALSE, got %i\n", ret);
13224     ok(GetLastError() == ERROR_HOTKEY_NOT_REGISTERED || broken(GetLastError() == 0xdeadbeef),
13225        "unexpected error %d\n", GetLastError());
13226
13227     if (ret == TRUE)
13228     {
13229         skip("hotkeys not supported\n");
13230         return;
13231     }
13232
13233     test_window = CreateWindowEx(0, "HotkeyWindowClass", NULL, WS_OVERLAPPEDWINDOW | WS_VISIBLE,
13234                            100, 100, 200, 200, 0, 0, 0, NULL);
13235
13236     flush_sequence();
13237
13238     SetLastError(0xdeadbeef);
13239     ret = UnregisterHotKey(test_window, 0);
13240     ok(ret == FALSE, "expected FALSE, got %i\n", ret);
13241     ok(GetLastError() == ERROR_HOTKEY_NOT_REGISTERED || broken(GetLastError() == 0xdeadbeef),
13242        "unexpected error %d\n", GetLastError());
13243
13244     /* Search for a Windows Key + letter combination that hasn't been registered */
13245     for (hotkey_letter = 0x41; hotkey_letter <= 0x51; hotkey_letter ++)
13246     {
13247         SetLastError(0xdeadbeef);
13248         ret = RegisterHotKey(test_window, 5, MOD_WIN, hotkey_letter);
13249
13250         if (ret == TRUE)
13251         {
13252             break;
13253         }
13254         else
13255         {
13256             ok(GetLastError() == ERROR_HOTKEY_ALREADY_REGISTERED || broken(GetLastError() == 0xdeadbeef),
13257                "unexpected error %d\n", GetLastError());
13258         }
13259     }
13260
13261     if (hotkey_letter == 0x52)
13262     {
13263         ok(0, "Couldn't find any free Windows Key + letter combination\n");
13264         goto end;
13265     }
13266
13267     hKBD_hook = SetWindowsHookEx(WH_KEYBOARD_LL, KeyboardHookProc, GetModuleHandle(NULL), 0);
13268     if (!hKBD_hook) win_skip("WH_KEYBOARD_LL is not supported\n");
13269
13270     /* Same key combination, different id */
13271     SetLastError(0xdeadbeef);
13272     ret = RegisterHotKey(test_window, 4, MOD_WIN, hotkey_letter);
13273     ok(ret == FALSE, "expected FALSE, got %i\n", ret);
13274     ok(GetLastError() == ERROR_HOTKEY_ALREADY_REGISTERED || broken(GetLastError() == 0xdeadbeef),
13275        "unexpected error %d\n", GetLastError());
13276
13277     /* Same key combination, different window */
13278     SetLastError(0xdeadbeef);
13279     ret = RegisterHotKey(NULL, 5, MOD_WIN, hotkey_letter);
13280     ok(ret == FALSE, "expected FALSE, got %i\n", ret);
13281     ok(GetLastError() == ERROR_HOTKEY_ALREADY_REGISTERED || broken(GetLastError() == 0xdeadbeef),
13282        "unexpected error %d\n", GetLastError());
13283
13284     /* Register the same hotkey twice */
13285     SetLastError(0xdeadbeef);
13286     ret = RegisterHotKey(test_window, 5, MOD_WIN, hotkey_letter);
13287     ok(ret == FALSE, "expected FALSE, got %i\n", ret);
13288     ok(GetLastError() == ERROR_HOTKEY_ALREADY_REGISTERED || broken(GetLastError() == 0xdeadbeef),
13289        "unexpected error %d\n", GetLastError());
13290
13291     /* Window on another thread */
13292     taskbar_window = FindWindowA("Shell_TrayWnd", NULL);
13293     if (!taskbar_window)
13294     {
13295         skip("no taskbar?\n");
13296     }
13297     else
13298     {
13299         SetLastError(0xdeadbeef);
13300         ret = RegisterHotKey(taskbar_window, 5, 0, hotkey_letter);
13301         ok(ret == FALSE, "expected FALSE, got %i\n", ret);
13302         ok(GetLastError() == ERROR_WINDOW_OF_OTHER_THREAD || broken(GetLastError() == 0xdeadbeef),
13303            "unexpected error %d\n", GetLastError());
13304     }
13305
13306     /* Inject the appropriate key sequence */
13307     keybd_event(VK_LWIN, 0, 0, 0);
13308     while (PeekMessage(&msg, NULL, 0, 0, PM_REMOVE))
13309         DispatchMessage(&msg);
13310     ok_sequence(WmHotkeyPressLWIN, "window hotkey press LWIN", FALSE);
13311
13312     keybd_event(hotkey_letter, 0, 0, 0);
13313     queue_status = GetQueueStatus(QS_HOTKEY);
13314     ok((queue_status & (QS_HOTKEY << 16)) == QS_HOTKEY << 16, "expected QS_HOTKEY << 16 set, got %x\n", queue_status);
13315     while (PeekMessage(&msg, NULL, 0, 0, PM_REMOVE))
13316     {
13317         if (msg.message == WM_HOTKEY)
13318         {
13319             ok(msg.hwnd == test_window, "unexpected hwnd %p\n", msg.hwnd);
13320             ok(msg.lParam == MAKELPARAM(MOD_WIN, hotkey_letter), "unexpected WM_HOTKEY lparam %lx\n", msg.lParam);
13321         }
13322         DispatchMessage(&msg);
13323     }
13324     ok_sequence(WmHotkeyPress, "window hotkey press", FALSE);
13325
13326     queue_status = GetQueueStatus(QS_HOTKEY);
13327     ok((queue_status & (QS_HOTKEY << 16)) == 0, "expected QS_HOTKEY << 16 cleared, got %x\n", queue_status);
13328
13329     key_state = GetAsyncKeyState(hotkey_letter);
13330     ok((key_state & 0x8000) == 0x8000, "unexpected key state %x\n", key_state);
13331
13332     keybd_event(hotkey_letter, 0, KEYEVENTF_KEYUP, 0);
13333     while (PeekMessage(&msg, NULL, 0, 0, PM_REMOVE))
13334         DispatchMessage(&msg);
13335     ok_sequence(WmHotkeyRelease, "window hotkey release", TRUE);
13336
13337     keybd_event(VK_LWIN, 0, KEYEVENTF_KEYUP, 0);
13338     while (PeekMessage(&msg, NULL, 0, 0, PM_REMOVE))
13339         DispatchMessage(&msg);
13340     ok_sequence(WmHotkeyReleaseLWIN, "window hotkey release LWIN", FALSE);
13341
13342     /* normal posted WM_HOTKEY messages set QS_HOTKEY */
13343     PostMessage(test_window, WM_HOTKEY, 0, 0);
13344     queue_status = GetQueueStatus(QS_HOTKEY);
13345     ok((queue_status & (QS_HOTKEY << 16)) == QS_HOTKEY << 16, "expected QS_HOTKEY << 16 set, got %x\n", queue_status);
13346     queue_status = GetQueueStatus(QS_POSTMESSAGE);
13347     ok((queue_status & (QS_POSTMESSAGE << 16)) == QS_POSTMESSAGE << 16, "expected QS_POSTMESSAGE << 16 set, got %x\n", queue_status);
13348     while (PeekMessage(&msg, NULL, 0, 0, PM_REMOVE))
13349         DispatchMessage(&msg);
13350     flush_sequence();
13351
13352     /* Send and process all messages at once */
13353     PostMessage(test_window, WM_APP, 0, 0);
13354     keybd_event(VK_LWIN, 0, 0, 0);
13355     keybd_event(hotkey_letter, 0, 0, 0);
13356     keybd_event(hotkey_letter, 0, KEYEVENTF_KEYUP, 0);
13357     keybd_event(VK_LWIN, 0, KEYEVENTF_KEYUP, 0);
13358
13359     while (PeekMessage(&msg, NULL, 0, 0, PM_REMOVE))
13360     {
13361         if (msg.message == WM_HOTKEY)
13362         {
13363             ok(msg.hwnd == test_window, "unexpected hwnd %p\n", msg.hwnd);
13364             ok(msg.lParam == MAKELPARAM(MOD_WIN, hotkey_letter), "unexpected WM_HOTKEY lparam %lx\n", msg.lParam);
13365         }
13366         DispatchMessage(&msg);
13367     }
13368     ok_sequence(WmHotkeyCombined, "window hotkey combined", FALSE);
13369
13370     /* Register same hwnd/id with different key combination */
13371     ret = RegisterHotKey(test_window, 5, 0, hotkey_letter);
13372     ok(ret == TRUE, "expected TRUE, got %i, err=%d\n", ret, GetLastError());
13373
13374     /* Previous key combination does not work */
13375     keybd_event(VK_LWIN, 0, 0, 0);
13376     keybd_event(hotkey_letter, 0, 0, 0);
13377     keybd_event(hotkey_letter, 0, KEYEVENTF_KEYUP, 0);
13378     keybd_event(VK_LWIN, 0, KEYEVENTF_KEYUP, 0);
13379
13380     while (PeekMessage(&msg, NULL, 0, 0, PM_REMOVE))
13381         DispatchMessage(&msg);
13382     ok_sequence(WmHotkeyPrevious, "window hotkey previous", FALSE);
13383
13384     /* New key combination works */
13385     keybd_event(hotkey_letter, 0, 0, 0);
13386     keybd_event(hotkey_letter, 0, KEYEVENTF_KEYUP, 0);
13387
13388     while (PeekMessage(&msg, NULL, 0, 0, PM_REMOVE))
13389     {
13390         if (msg.message == WM_HOTKEY)
13391         {
13392             ok(msg.hwnd == test_window, "unexpected hwnd %p\n", msg.hwnd);
13393             ok(msg.lParam == MAKELPARAM(0, hotkey_letter), "unexpected WM_HOTKEY lparam %lx\n", msg.lParam);
13394         }
13395         DispatchMessage(&msg);
13396     }
13397     ok_sequence(WmHotkeyNew, "window hotkey new", FALSE);
13398
13399     /* Unregister hotkey properly */
13400     ret = UnregisterHotKey(test_window, 5);
13401     ok(ret == TRUE, "expected TRUE, got %i, err=%d\n", ret, GetLastError());
13402
13403     /* Unregister hotkey again */
13404     SetLastError(0xdeadbeef);
13405     ret = UnregisterHotKey(test_window, 5);
13406     ok(ret == FALSE, "expected FALSE, got %i\n", ret);
13407     ok(GetLastError() == ERROR_HOTKEY_NOT_REGISTERED || broken(GetLastError() == 0xdeadbeef),
13408        "unexpected error %d\n", GetLastError());
13409
13410     /* Register thread hotkey */
13411     ret = RegisterHotKey(NULL, 5, MOD_WIN, hotkey_letter);
13412     ok(ret == TRUE, "expected TRUE, got %i, err=%d\n", ret, GetLastError());
13413
13414     /* Inject the appropriate key sequence */
13415     keybd_event(VK_LWIN, 0, 0, 0);
13416     while (PeekMessage(&msg, NULL, 0, 0, PM_REMOVE))
13417     {
13418         ok(msg.hwnd != NULL, "unexpected thread message %x\n", msg.message);
13419         DispatchMessage(&msg);
13420     }
13421     ok_sequence(WmHotkeyPressLWIN, "thread hotkey press LWIN", FALSE);
13422
13423     keybd_event(hotkey_letter, 0, 0, 0);
13424     while (PeekMessage(&msg, NULL, 0, 0, PM_REMOVE))
13425     {
13426         if (msg.message == WM_HOTKEY)
13427         {
13428             struct recvd_message message;
13429             ok(msg.hwnd == NULL, "unexpected hwnd %p\n", msg.hwnd);
13430             ok(msg.lParam == MAKELPARAM(MOD_WIN, hotkey_letter), "unexpected WM_HOTKEY lparam %lx\n", msg.lParam);
13431             message.message = msg.message;
13432             message.flags = sent|wparam|lparam;
13433             message.wParam = msg.wParam;
13434             message.lParam = msg.lParam;
13435             message.descr = "test_hotkey thread message";
13436             add_message(&message);
13437         }
13438         else
13439             ok(msg.hwnd != NULL, "unexpected thread message %x\n", msg.message);
13440         DispatchMessage(&msg);
13441     }
13442     ok_sequence(WmHotkeyPress, "thread hotkey press", FALSE);
13443
13444     keybd_event(hotkey_letter, 0, KEYEVENTF_KEYUP, 0);
13445     while (PeekMessage(&msg, NULL, 0, 0, PM_REMOVE))
13446     {
13447         ok(msg.hwnd != NULL, "unexpected thread message %x\n", msg.message);
13448         DispatchMessage(&msg);
13449     }
13450     ok_sequence(WmHotkeyRelease, "thread hotkey release", TRUE);
13451
13452     keybd_event(VK_LWIN, 0, KEYEVENTF_KEYUP, 0);
13453     while (PeekMessage(&msg, NULL, 0, 0, PM_REMOVE))
13454     {
13455         ok(msg.hwnd != NULL, "unexpected thread message %x\n", msg.message);
13456         DispatchMessage(&msg);
13457     }
13458     ok_sequence(WmHotkeyReleaseLWIN, "thread hotkey release LWIN", FALSE);
13459
13460     /* Unregister thread hotkey */
13461     ret = UnregisterHotKey(NULL, 5);
13462     ok(ret == TRUE, "expected TRUE, got %i, err=%d\n", ret, GetLastError());
13463
13464     if (hKBD_hook) UnhookWindowsHookEx(hKBD_hook);
13465     hKBD_hook = NULL;
13466
13467 end:
13468     UnregisterHotKey(NULL, 5);
13469     UnregisterHotKey(test_window, 5);
13470     DestroyWindow(test_window);
13471     flush_sequence();
13472 }
13473
13474 START_TEST(msg)
13475 {
13476     char **test_argv;
13477     BOOL ret;
13478     BOOL (WINAPI *pIsWinEventHookInstalled)(DWORD)= 0;/*GetProcAddress(user32, "IsWinEventHookInstalled");*/
13479     HMODULE hModuleImm32;
13480     BOOL (WINAPI *pImmDisableIME)(DWORD);
13481
13482     int argc = winetest_get_mainargs( &test_argv );
13483     if (argc >= 3)
13484     {
13485         unsigned int arg;
13486         /* Child process. */
13487         sscanf (test_argv[2], "%d", (unsigned int *) &arg);
13488         do_wait_idle_child( arg );
13489         return;
13490     }
13491
13492     init_procs();
13493
13494     hModuleImm32 = LoadLibrary("imm32.dll");
13495     if (hModuleImm32) {
13496         pImmDisableIME = (void *)GetProcAddress(hModuleImm32, "ImmDisableIME");
13497         if (pImmDisableIME)
13498             pImmDisableIME(0);
13499     }
13500     pImmDisableIME = NULL;
13501     FreeLibrary(hModuleImm32);
13502
13503     if (!RegisterWindowClasses()) assert(0);
13504
13505     if (pSetWinEventHook)
13506     {
13507         hEvent_hook = pSetWinEventHook(EVENT_MIN, EVENT_MAX,
13508                                        GetModuleHandleA(0), win_event_proc,
13509                                        0, GetCurrentThreadId(),
13510                                        WINEVENT_INCONTEXT);
13511         if (pIsWinEventHookInstalled && hEvent_hook)
13512         {
13513             UINT event;
13514             for (event = EVENT_MIN; event <= EVENT_MAX; event++)
13515                 ok(pIsWinEventHookInstalled(event), "IsWinEventHookInstalled(%u) failed\n", event);
13516         }
13517     }
13518     if (!hEvent_hook) win_skip( "no win event hook support\n" );
13519
13520     cbt_hook_thread_id = GetCurrentThreadId();
13521     hCBT_hook = SetWindowsHookExA(WH_CBT, cbt_hook_proc, 0, GetCurrentThreadId());
13522     if (!hCBT_hook) win_skip( "cannot set global hook, will skip hook tests\n" );
13523
13524     test_winevents();
13525
13526     /* Fix message sequences before removing 4 lines below */
13527 #if 1
13528     if (pUnhookWinEvent && hEvent_hook)
13529     {
13530         ret = pUnhookWinEvent(hEvent_hook);
13531         ok( ret, "UnhookWinEvent error %d\n", GetLastError());
13532         pUnhookWinEvent = 0;
13533     }
13534     hEvent_hook = 0;
13535 #endif
13536
13537     test_SetParent();
13538     test_PostMessage();
13539     test_ShowWindow();
13540     test_PeekMessage();
13541     test_PeekMessage2();
13542     test_WaitForInputIdle( test_argv[0] );
13543     test_scrollwindowex();
13544     test_messages();
13545     test_setwindowpos();
13546     test_showwindow();
13547     invisible_parent_tests();
13548     test_mdi_messages();
13549     test_button_messages();
13550     test_static_messages();
13551     test_listbox_messages();
13552     test_combobox_messages();
13553     test_wmime_keydown_message();
13554     test_paint_messages();
13555     test_interthread_messages();
13556     test_message_conversion();
13557     test_accelerators();
13558     test_timers();
13559     test_timers_no_wnd();
13560     if (hCBT_hook) test_set_hook();
13561     test_DestroyWindow();
13562     test_DispatchMessage();
13563     test_SendMessageTimeout();
13564     test_edit_messages();
13565     test_quit_message();
13566     test_SetActiveWindow();
13567
13568     if (!pTrackMouseEvent)
13569         win_skip("TrackMouseEvent is not available\n");
13570     else
13571         test_TrackMouseEvent();
13572
13573     test_SetWindowRgn();
13574     test_sys_menu();
13575     test_dialog_messages();
13576     test_nullCallback();
13577     test_dbcs_wm_char();
13578     test_menu_messages();
13579     test_paintingloop();
13580     test_defwinproc();
13581     test_clipboard_viewers();
13582     test_keyflags();
13583     test_hotkey();
13584     /* keep it the last test, under Windows it tends to break the tests
13585      * which rely on active/foreground windows being correct.
13586      */
13587     test_SetForegroundWindow();
13588
13589     UnhookWindowsHookEx(hCBT_hook);
13590     if (pUnhookWinEvent && hEvent_hook)
13591     {
13592         ret = pUnhookWinEvent(hEvent_hook);
13593         ok( ret, "UnhookWinEvent error %d\n", GetLastError());
13594         SetLastError(0xdeadbeef);
13595         ok(!pUnhookWinEvent(hEvent_hook), "UnhookWinEvent succeeded\n");
13596         ok(GetLastError() == ERROR_INVALID_HANDLE || /* Win2k */
13597            GetLastError() == 0xdeadbeef, /* Win9x */
13598            "unexpected error %d\n", GetLastError());
13599     }
13600 }