wbemprox: Support overriding the CIM to VARIANT type mapping for method parameters.
[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 static BOOL (WINAPI *pUpdateLayeredWindow)(HWND,HDC,POINT*,SIZE*,HDC,POINT*,COLORREF,BLENDFUNCTION*,DWORD);
1725 /* kernel32 functions */
1726 static BOOL (WINAPI *pGetCPInfoExA)(UINT, DWORD, LPCPINFOEXA);
1727
1728 static void init_procs(void)
1729 {
1730     HMODULE user32 = GetModuleHandleA("user32.dll");
1731     HMODULE kernel32 = GetModuleHandleA("kernel32.dll");
1732
1733 #define GET_PROC(dll, func) \
1734     p ## func = (void*)GetProcAddress(dll, #func); \
1735     if(!p ## func) { \
1736       trace("GetProcAddress(%s) failed\n", #func); \
1737     }
1738
1739     GET_PROC(user32, GetAncestor)
1740     GET_PROC(user32, GetMenuInfo)
1741     GET_PROC(user32, NotifyWinEvent)
1742     GET_PROC(user32, SetMenuInfo)
1743     GET_PROC(user32, SetWinEventHook)
1744     GET_PROC(user32, TrackMouseEvent)
1745     GET_PROC(user32, UnhookWinEvent)
1746     GET_PROC(user32, GetMonitorInfoA)
1747     GET_PROC(user32, MonitorFromPoint)
1748     GET_PROC(user32, UpdateLayeredWindow)
1749
1750     GET_PROC(kernel32, GetCPInfoExA)
1751
1752 #undef GET_PROC
1753 }
1754
1755 static const char *get_winpos_flags(UINT flags)
1756 {
1757     static char buffer[300];
1758
1759     buffer[0] = 0;
1760 #define DUMP(flag) do { if (flags & flag) { strcat( buffer, "|" #flag ); flags &= ~flag; } } while(0)
1761     DUMP( SWP_SHOWWINDOW );
1762     DUMP( SWP_HIDEWINDOW );
1763     DUMP( SWP_NOACTIVATE );
1764     DUMP( SWP_FRAMECHANGED );
1765     DUMP( SWP_NOCOPYBITS );
1766     DUMP( SWP_NOOWNERZORDER );
1767     DUMP( SWP_NOSENDCHANGING );
1768     DUMP( SWP_DEFERERASE );
1769     DUMP( SWP_ASYNCWINDOWPOS );
1770     DUMP( SWP_NOZORDER );
1771     DUMP( SWP_NOREDRAW );
1772     DUMP( SWP_NOSIZE );
1773     DUMP( SWP_NOMOVE );
1774     DUMP( SWP_NOCLIENTSIZE );
1775     DUMP( SWP_NOCLIENTMOVE );
1776     if (flags) sprintf(buffer + strlen(buffer),"|0x%04x", flags);
1777     return buffer + 1;
1778 #undef DUMP
1779 }
1780
1781 static BOOL ignore_message( UINT message )
1782 {
1783     /* these are always ignored */
1784     return (message >= 0xc000 ||
1785             message == WM_GETICON ||
1786             message == WM_GETOBJECT ||
1787             message == WM_TIMECHANGE ||
1788             message == WM_DISPLAYCHANGE ||
1789             message == WM_DEVICECHANGE ||
1790             message == WM_DWMNCRENDERINGCHANGED);
1791 }
1792
1793
1794 #define add_message(msg) add_message_(__LINE__,msg);
1795 static void add_message_(int line, const struct recvd_message *msg)
1796 {
1797     struct recvd_message *seq;
1798
1799     if (!sequence) 
1800     {
1801         sequence_size = 10;
1802         sequence = HeapAlloc( GetProcessHeap(), 0, sequence_size * sizeof(*sequence) );
1803     }
1804     if (sequence_cnt == sequence_size) 
1805     {
1806         sequence_size *= 2;
1807         sequence = HeapReAlloc( GetProcessHeap(), 0, sequence, sequence_size * sizeof(*sequence) );
1808     }
1809     assert(sequence);
1810
1811     seq = &sequence[sequence_cnt];
1812     seq->hwnd = msg->hwnd;
1813     seq->message = msg->message;
1814     seq->flags = msg->flags;
1815     seq->wParam = msg->wParam;
1816     seq->lParam = msg->lParam;
1817     seq->line   = line;
1818     seq->descr  = msg->descr;
1819     seq->output[0] = 0;
1820
1821     if (msg->descr)
1822     {
1823         if (msg->flags & hook)
1824         {
1825             static const char * const CBT_code_name[10] =
1826             {
1827                 "HCBT_MOVESIZE",
1828                 "HCBT_MINMAX",
1829                 "HCBT_QS",
1830                 "HCBT_CREATEWND",
1831                 "HCBT_DESTROYWND",
1832                 "HCBT_ACTIVATE",
1833                 "HCBT_CLICKSKIPPED",
1834                 "HCBT_KEYSKIPPED",
1835                 "HCBT_SYSCOMMAND",
1836                 "HCBT_SETFOCUS"
1837             };
1838             const char *code_name = (msg->message <= HCBT_SETFOCUS) ? CBT_code_name[msg->message] : "Unknown";
1839
1840             sprintf( seq->output, "%s: hook %d (%s) wp %08lx lp %08lx",
1841                      msg->descr, msg->message, code_name, msg->wParam, msg->lParam );
1842         }
1843         else if (msg->flags & winevent_hook)
1844         {
1845             sprintf( seq->output, "%s: winevent %p %08x %08lx %08lx",
1846                      msg->descr, msg->hwnd, msg->message, msg->wParam, msg->lParam );
1847         }
1848         else
1849         {
1850             switch (msg->message)
1851             {
1852             case WM_WINDOWPOSCHANGING:
1853             case WM_WINDOWPOSCHANGED:
1854             {
1855                 WINDOWPOS *winpos = (WINDOWPOS *)msg->lParam;
1856
1857                 sprintf( seq->output, "%s: %p WM_WINDOWPOS%s wp %08lx lp %08lx after %p x %d y %d cx %d cy %d flags %s",
1858                           msg->descr, msg->hwnd,
1859                           (msg->message == WM_WINDOWPOSCHANGING) ? "CHANGING" : "CHANGED",
1860                           msg->wParam, msg->lParam, winpos->hwndInsertAfter,
1861                           winpos->x, winpos->y, winpos->cx, winpos->cy,
1862                           get_winpos_flags(winpos->flags) );
1863
1864                 /* Log only documented flags, win2k uses 0x1000 and 0x2000
1865                  * in the high word for internal purposes
1866                  */
1867                 seq->wParam = winpos->flags & 0xffff;
1868                 /* We are not interested in the flags that don't match under XP and Win9x */
1869                 seq->wParam &= ~SWP_NOZORDER;
1870                 break;
1871             }
1872
1873             case WM_DRAWITEM:
1874             {
1875                 DRAW_ITEM_STRUCT di;
1876                 DRAWITEMSTRUCT *dis = (DRAWITEMSTRUCT *)msg->lParam;
1877
1878                 sprintf( seq->output, "%s: %p WM_DRAWITEM: type %x, ctl_id %x, item_id %x, action %x, state %x",
1879                          msg->descr, msg->hwnd, dis->CtlType, dis->CtlID,
1880                          dis->itemID, dis->itemAction, dis->itemState);
1881
1882                 di.u.lp = 0;
1883                 di.u.item.type = dis->CtlType;
1884                 di.u.item.ctl_id = dis->CtlID;
1885                 if (dis->CtlType == ODT_LISTBOX ||
1886                     dis->CtlType == ODT_COMBOBOX ||
1887                     dis->CtlType == ODT_MENU)
1888                     di.u.item.item_id = dis->itemID;
1889                 di.u.item.action = dis->itemAction;
1890                 di.u.item.state = dis->itemState;
1891
1892                 seq->lParam = di.u.lp;
1893                 break;
1894             }
1895             default:
1896                 if (msg->message >= 0xc000) return;  /* ignore registered messages */
1897                 sprintf( seq->output, "%s: %p %04x wp %08lx lp %08lx",
1898                          msg->descr, msg->hwnd, msg->message, msg->wParam, msg->lParam );
1899             }
1900             if (msg->flags & (sent|posted|parent|defwinproc|beginpaint))
1901                 sprintf( seq->output + strlen(seq->output), " (flags %x)", msg->flags );
1902         }
1903     }
1904
1905     sequence_cnt++;
1906 }
1907
1908 /* try to make sure pending X events have been processed before continuing */
1909 static void flush_events(void)
1910 {
1911     MSG msg;
1912     int diff = 200;
1913     int min_timeout = 100;
1914     DWORD time = GetTickCount() + diff;
1915
1916     while (diff > 0)
1917     {
1918         if (MsgWaitForMultipleObjects( 0, NULL, FALSE, min_timeout, QS_ALLINPUT ) == WAIT_TIMEOUT) break;
1919         while (PeekMessage( &msg, 0, 0, 0, PM_REMOVE )) DispatchMessage( &msg );
1920         diff = time - GetTickCount();
1921     }
1922 }
1923
1924 static void flush_sequence(void)
1925 {
1926     HeapFree(GetProcessHeap(), 0, sequence);
1927     sequence = 0;
1928     sequence_cnt = sequence_size = 0;
1929 }
1930
1931 static void dump_sequence(const struct message *expected, const char *context, const char *file, int line)
1932 {
1933     const struct recvd_message *actual = sequence;
1934     unsigned int count = 0;
1935
1936     trace_(file, line)("Failed sequence %s:\n", context );
1937     while (expected->message && actual->message)
1938     {
1939         if (actual->output[0])
1940         {
1941             if (expected->flags & hook)
1942             {
1943                 trace_(file, line)( "  %u: expected: hook %04x - actual: %s\n",
1944                                     count, expected->message, actual->output );
1945             }
1946             else if (expected->flags & winevent_hook)
1947             {
1948                 trace_(file, line)( "  %u: expected: winevent %04x - actual: %s\n",
1949                                     count, expected->message, actual->output );
1950             }
1951             else if (expected->flags & kbd_hook)
1952             {
1953                 trace_(file, line)( "  %u: expected: kbd %04x - actual: %s\n",
1954                                     count, expected->message, actual->output );
1955             }
1956             else
1957             {
1958                 trace_(file, line)( "  %u: expected: msg %04x - actual: %s\n",
1959                                     count, expected->message, actual->output );
1960             }
1961         }
1962
1963         if (expected->message == actual->message)
1964         {
1965             if ((expected->flags & defwinproc) != (actual->flags & defwinproc) &&
1966                 (expected->flags & optional))
1967             {
1968                 /* don't match messages if their defwinproc status differs */
1969                 expected++;
1970             }
1971             else
1972             {
1973                 expected++;
1974                 actual++;
1975             }
1976         }
1977         /* silently drop winevent messages if there is no support for them */
1978         else if ((expected->flags & optional) || ((expected->flags & winevent_hook) && !hEvent_hook))
1979             expected++;
1980         else
1981         {
1982             expected++;
1983             actual++;
1984         }
1985         count++;
1986     }
1987
1988     /* optional trailing messages */
1989     while (expected->message && ((expected->flags & optional) ||
1990             ((expected->flags & winevent_hook) && !hEvent_hook)))
1991     {
1992         trace_(file, line)( "  %u: expected: msg %04x - actual: nothing\n", count, expected->message );
1993         expected++;
1994         count++;
1995     }
1996
1997     if (expected->message)
1998     {
1999         trace_(file, line)( "  %u: expected: msg %04x - actual: nothing\n", count, expected->message );
2000         return;
2001     }
2002
2003     while (actual->message && actual->output[0])
2004     {
2005         trace_(file, line)( "  %u: expected: nothing - actual: %s\n", count, actual->output );
2006         actual++;
2007         count++;
2008     }
2009 }
2010
2011 #define ok_sequence( exp, contx, todo) \
2012         ok_sequence_( (exp), (contx), (todo), __FILE__, __LINE__)
2013
2014
2015 static void ok_sequence_(const struct message *expected_list, const char *context, int todo,
2016                          const char *file, int line)
2017 {
2018     static const struct recvd_message end_of_sequence;
2019     const struct message *expected = expected_list;
2020     const struct recvd_message *actual;
2021     int failcount = 0, dump = 0;
2022     unsigned int count = 0;
2023
2024     add_message(&end_of_sequence);
2025
2026     actual = sequence;
2027
2028     while (expected->message && actual->message)
2029     {
2030         if (expected->message == actual->message &&
2031             !((expected->flags ^ actual->flags) & (hook|winevent_hook|kbd_hook)))
2032         {
2033             if (expected->flags & wparam)
2034             {
2035                 if (((expected->wParam ^ actual->wParam) & ~expected->wp_mask) && todo)
2036                 {
2037                     todo_wine {
2038                         failcount ++;
2039                         if (strcmp(winetest_platform, "wine")) dump++;
2040                         ok_( file, line) (FALSE,
2041                             "%s: %u: in msg 0x%04x expecting wParam 0x%lx got 0x%lx\n",
2042                             context, count, expected->message, expected->wParam, actual->wParam);
2043                     }
2044                 }
2045                 else
2046                 {
2047                     ok_( file, line)( ((expected->wParam ^ actual->wParam) & ~expected->wp_mask) == 0,
2048                                      "%s: %u: in msg 0x%04x expecting wParam 0x%lx got 0x%lx\n",
2049                                      context, count, expected->message, expected->wParam, actual->wParam);
2050                     if ((expected->wParam ^ actual->wParam) & ~expected->wp_mask) dump++;
2051                 }
2052
2053             }
2054             if (expected->flags & lparam)
2055             {
2056                 if (((expected->lParam ^ actual->lParam) & ~expected->lp_mask) && todo)
2057                 {
2058                     todo_wine {
2059                         failcount ++;
2060                         if (strcmp(winetest_platform, "wine")) dump++;
2061                         ok_( file, line) (FALSE,
2062                             "%s: %u: in msg 0x%04x expecting lParam 0x%lx got 0x%lx\n",
2063                             context, count, expected->message, expected->lParam, actual->lParam);
2064                     }
2065                 }
2066                 else
2067                 {
2068                     ok_( file, line)(((expected->lParam ^ actual->lParam) & ~expected->lp_mask) == 0,
2069                                      "%s: %u: in msg 0x%04x expecting lParam 0x%lx got 0x%lx\n",
2070                                      context, count, expected->message, expected->lParam, actual->lParam);
2071                     if ((expected->lParam ^ actual->lParam) & ~expected->lp_mask) dump++;
2072                 }
2073             }
2074             if ((expected->flags & optional) &&
2075                 ((expected->flags ^ actual->flags) & (defwinproc|parent)))
2076             {
2077                 /* don't match optional messages if their defwinproc or parent status differs */
2078                 expected++;
2079                 count++;
2080                 continue;
2081             }
2082             if ((expected->flags & defwinproc) != (actual->flags & defwinproc) && todo)
2083             {
2084                     todo_wine {
2085                         failcount ++;
2086                         if (strcmp(winetest_platform, "wine")) dump++;
2087                         ok_( file, line) (FALSE,
2088                             "%s: %u: the msg 0x%04x should %shave been sent by DefWindowProc\n",
2089                             context, count, expected->message, (expected->flags & defwinproc) ? "" : "NOT ");
2090                     }
2091             }
2092             else
2093             {
2094                 ok_( file, line) ((expected->flags & defwinproc) == (actual->flags & defwinproc),
2095                     "%s: %u: the msg 0x%04x should %shave been sent by DefWindowProc\n",
2096                     context, count, expected->message, (expected->flags & defwinproc) ? "" : "NOT ");
2097                 if ((expected->flags & defwinproc) != (actual->flags & defwinproc)) dump++;
2098             }
2099
2100             ok_( file, line) ((expected->flags & beginpaint) == (actual->flags & beginpaint),
2101                 "%s: %u: the msg 0x%04x should %shave been sent by BeginPaint\n",
2102                 context, count, expected->message, (expected->flags & beginpaint) ? "" : "NOT ");
2103             if ((expected->flags & beginpaint) != (actual->flags & beginpaint)) dump++;
2104
2105             ok_( file, line) ((expected->flags & (sent|posted)) == (actual->flags & (sent|posted)),
2106                 "%s: %u: the msg 0x%04x should have been %s\n",
2107                 context, count, expected->message, (expected->flags & posted) ? "posted" : "sent");
2108             if ((expected->flags & (sent|posted)) != (actual->flags & (sent|posted))) dump++;
2109
2110             ok_( file, line) ((expected->flags & parent) == (actual->flags & parent),
2111                 "%s: %u: the msg 0x%04x was expected in %s\n",
2112                 context, count, expected->message, (expected->flags & parent) ? "parent" : "child");
2113             if ((expected->flags & parent) != (actual->flags & parent)) dump++;
2114
2115             ok_( file, line) ((expected->flags & hook) == (actual->flags & hook),
2116                 "%s: %u: the msg 0x%04x should have been sent by a hook\n",
2117                 context, count, expected->message);
2118             if ((expected->flags & hook) != (actual->flags & hook)) dump++;
2119
2120             ok_( file, line) ((expected->flags & winevent_hook) == (actual->flags & winevent_hook),
2121                 "%s: %u: the msg 0x%04x should have been sent by a winevent hook\n",
2122                 context, count, expected->message);
2123             if ((expected->flags & winevent_hook) != (actual->flags & winevent_hook)) dump++;
2124
2125             ok_( file, line) ((expected->flags & kbd_hook) == (actual->flags & kbd_hook),
2126                 "%s: %u: the msg 0x%04x should have been sent by a keyboard hook\n",
2127                 context, count, expected->message);
2128             if ((expected->flags & kbd_hook) != (actual->flags & kbd_hook)) dump++;
2129
2130             expected++;
2131             actual++;
2132         }
2133         /* silently drop hook messages if there is no support for them */
2134         else if ((expected->flags & optional) ||
2135                  ((expected->flags & hook) && !hCBT_hook) ||
2136                  ((expected->flags & winevent_hook) && !hEvent_hook) ||
2137                  ((expected->flags & kbd_hook) && !hKBD_hook))
2138             expected++;
2139         else if (todo)
2140         {
2141             failcount++;
2142             todo_wine {
2143                 if (strcmp(winetest_platform, "wine")) dump++;
2144                 ok_( file, line) (FALSE, "%s: %u: the msg 0x%04x was expected, but got msg 0x%04x instead\n",
2145                                   context, count, expected->message, actual->message);
2146             }
2147             goto done;
2148         }
2149         else
2150         {
2151             ok_( file, line) (FALSE, "%s: %u: the msg 0x%04x was expected, but got msg 0x%04x instead\n",
2152                               context, count, expected->message, actual->message);
2153             dump++;
2154             expected++;
2155             actual++;
2156         }
2157         count++;
2158     }
2159
2160     /* skip all optional trailing messages */
2161     while (expected->message && ((expected->flags & optional) ||
2162                                  ((expected->flags & hook) && !hCBT_hook) ||
2163                                  ((expected->flags & winevent_hook) && !hEvent_hook)))
2164         expected++;
2165
2166     if (todo)
2167     {
2168         todo_wine {
2169             if (expected->message || actual->message) {
2170                 failcount++;
2171                 if (strcmp(winetest_platform, "wine")) dump++;
2172                 ok_( file, line) (FALSE, "%s: %u: the msg sequence is not complete: expected %04x - actual %04x\n",
2173                                   context, count, expected->message, actual->message);
2174             }
2175         }
2176     }
2177     else
2178     {
2179         if (expected->message || actual->message)
2180         {
2181             dump++;
2182             ok_( file, line) (FALSE, "%s: %u: the msg sequence is not complete: expected %04x - actual %04x\n",
2183                               context, count, expected->message, actual->message);
2184         }
2185     }
2186     if( todo && !failcount) /* succeeded yet marked todo */
2187         todo_wine {
2188             if (!strcmp(winetest_platform, "wine")) dump++;
2189             ok_( file, line)( TRUE, "%s: marked \"todo_wine\" but succeeds\n", context);
2190         }
2191
2192 done:
2193     if (dump) dump_sequence(expected_list, context, file, line);
2194     flush_sequence();
2195 }
2196
2197 #define expect(EXPECTED,GOT) ok((GOT)==(EXPECTED), "Expected %d, got %d\n", (EXPECTED), (GOT))
2198
2199 /******************************** MDI test **********************************/
2200
2201 /* CreateWindow for MDI frame window, initially visible */
2202 static const struct message WmCreateMDIframeSeq[] = {
2203     { HCBT_CREATEWND, hook },
2204     { WM_GETMINMAXINFO, sent },
2205     { WM_NCCREATE, sent },
2206     { WM_NCCALCSIZE, sent|wparam, 0 },
2207     { EVENT_OBJECT_REORDER, winevent_hook|wparam|lparam, 0, 0 },
2208     { WM_CREATE, sent },
2209     { EVENT_OBJECT_CREATE, winevent_hook|wparam|lparam, 0, 0 },
2210     { WM_NOTIFYFORMAT, sent|optional },
2211     { WM_QUERYUISTATE, sent|optional },
2212     { WM_WINDOWPOSCHANGING, sent|optional },
2213     { WM_GETMINMAXINFO, sent|optional },
2214     { WM_NCCALCSIZE, sent|optional },
2215     { WM_WINDOWPOSCHANGED, sent|optional },
2216     { WM_SHOWWINDOW, sent|wparam, 1 },
2217     { WM_WINDOWPOSCHANGING, sent|wparam, SWP_SHOWWINDOW|SWP_NOSIZE|SWP_NOMOVE },
2218     { EVENT_OBJECT_SHOW, winevent_hook|wparam|lparam, 0, 0 },
2219     { HCBT_ACTIVATE, hook },
2220     { EVENT_SYSTEM_FOREGROUND, winevent_hook|wparam|lparam, 0, 0 },
2221     { WM_QUERYNEWPALETTE, sent|wparam|lparam|optional, 0, 0 },
2222     { WM_WINDOWPOSCHANGING, sent|wparam|optional, SWP_NOSIZE|SWP_NOMOVE }, /* XP */
2223     { WM_ACTIVATEAPP, sent|wparam|optional, 1 }, /* Win9x doesn't send it */
2224     { WM_NCACTIVATE, sent },
2225     { WM_GETTEXT, sent|defwinproc|optional },
2226     { WM_ACTIVATE, sent|wparam, 1 },
2227     { WM_WINDOWPOSCHANGING, sent|wparam|optional, SWP_NOSIZE|SWP_NOMOVE }, /* Win9x */
2228     { HCBT_SETFOCUS, hook },
2229     { WM_IME_SETCONTEXT, sent|wparam|defwinproc|optional, 1 },
2230     { WM_IME_NOTIFY, sent|wparam|defwinproc|optional, 2 },
2231     { EVENT_OBJECT_FOCUS, winevent_hook|wparam|lparam, OBJID_CLIENT, 0 },
2232     { WM_SETFOCUS, sent|wparam|defwinproc, 0 },
2233     /* Win9x adds SWP_NOZORDER below */
2234     { WM_WINDOWPOSCHANGED, sent|wparam, SWP_SHOWWINDOW|SWP_NOSIZE|SWP_NOMOVE|SWP_NOCLIENTSIZE|SWP_NOCLIENTMOVE },
2235     { WM_NCCALCSIZE, sent|wparam|optional, 1 }, /* XP */
2236     { EVENT_OBJECT_LOCATIONCHANGE, winevent_hook|wparam|lparam, 0, 0 },
2237     { WM_SIZE, sent|wparam, SIZE_RESTORED },
2238     { WM_MOVE, sent },
2239     { 0 }
2240 };
2241 /* DestroyWindow for MDI frame window, initially visible */
2242 static const struct message WmDestroyMDIframeSeq[] = {
2243     { HCBT_DESTROYWND, hook },
2244     { 0x0090, sent|optional },
2245     { WM_WINDOWPOSCHANGING, sent|wparam|optional, SWP_HIDEWINDOW|SWP_NOACTIVATE|SWP_NOSIZE|SWP_NOMOVE },
2246     { EVENT_OBJECT_HIDE, winevent_hook|wparam|lparam, 0, 0 },
2247     { WM_NCACTIVATE, sent|wparam|optional, 0 }, /* Win9x */
2248     { WM_WINDOWPOSCHANGED, sent|wparam|optional, SWP_HIDEWINDOW|SWP_NOACTIVATE|SWP_NOSIZE|SWP_NOMOVE|SWP_NOCLIENTSIZE|SWP_NOCLIENTMOVE },
2249     { WM_NCACTIVATE, sent|wparam|optional, 0 }, /* XP */
2250     { WM_ACTIVATE, sent|wparam|optional, 0 }, /* Win9x */
2251     { WM_ACTIVATEAPP, sent|wparam|optional, 0 }, /* Win9x */
2252     { EVENT_OBJECT_HIDE, winevent_hook|wparam|lparam, OBJID_CARET, 0 },
2253     { EVENT_OBJECT_DESTROY, winevent_hook|wparam|lparam, 0, 0 },
2254     { WM_DESTROY, sent },
2255     { WM_NCDESTROY, sent },
2256     { 0 }
2257 };
2258 /* CreateWindow for MDI client window, initially visible */
2259 static const struct message WmCreateMDIclientSeq[] = {
2260     { HCBT_CREATEWND, hook },
2261     { WM_NCCREATE, sent },
2262     { WM_NCCALCSIZE, sent|wparam, 0 },
2263     { EVENT_OBJECT_REORDER, winevent_hook|wparam|lparam|optional, 0, 0 },
2264     { WM_CREATE, sent },
2265     { EVENT_OBJECT_REORDER, winevent_hook|wparam|lparam|optional, 0, 0 },
2266     { EVENT_OBJECT_CREATE, winevent_hook|wparam|lparam, 0, 0 },
2267     { WM_SIZE, sent|wparam, SIZE_RESTORED },
2268     { WM_MOVE, sent },
2269     { WM_PARENTNOTIFY, sent|wparam, WM_CREATE }, /* in MDI frame */
2270     { WM_SHOWWINDOW, sent|wparam, 1 },
2271     { WM_WINDOWPOSCHANGING, sent|wparam|optional, SWP_SHOWWINDOW|SWP_NOACTIVATE|SWP_NOSIZE|SWP_NOMOVE },
2272     { EVENT_OBJECT_SHOW, winevent_hook|wparam|lparam, 0, 0 },
2273     { WM_WINDOWPOSCHANGED, sent|wparam|optional, SWP_SHOWWINDOW|SWP_NOACTIVATE|SWP_NOSIZE|SWP_NOMOVE|SWP_NOCLIENTSIZE|SWP_NOCLIENTMOVE },
2274     { 0 }
2275 };
2276 /* ShowWindow(SW_SHOW) for MDI client window */
2277 static const struct message WmShowMDIclientSeq[] = {
2278     { WM_SHOWWINDOW, sent|wparam, 1 },
2279     { WM_WINDOWPOSCHANGING, sent|wparam|optional, SWP_SHOWWINDOW|SWP_NOACTIVATE|SWP_NOSIZE|SWP_NOMOVE },
2280     { EVENT_OBJECT_SHOW, winevent_hook|wparam|lparam, 0, 0 },
2281     { WM_WINDOWPOSCHANGED, sent|wparam|optional, SWP_SHOWWINDOW|SWP_NOACTIVATE|SWP_NOSIZE|SWP_NOMOVE|SWP_NOCLIENTSIZE|SWP_NOCLIENTMOVE },
2282     { 0 }
2283 };
2284 /* ShowWindow(SW_HIDE) for MDI client window */
2285 static const struct message WmHideMDIclientSeq[] = {
2286     { WM_SHOWWINDOW, sent|wparam, 0 },
2287     { WM_WINDOWPOSCHANGING, sent|wparam|optional, SWP_HIDEWINDOW|SWP_NOACTIVATE|SWP_NOSIZE|SWP_NOMOVE },
2288     { EVENT_OBJECT_SHOW, winevent_hook|wparam|lparam|optional, 0, 0 }, /* win2000 */
2289     { EVENT_OBJECT_HIDE, winevent_hook|wparam|lparam|optional, 0, 0 }, /* XP */
2290     { WM_WINDOWPOSCHANGED, sent|wparam|optional, SWP_HIDEWINDOW|SWP_NOACTIVATE|SWP_NOSIZE|SWP_NOMOVE|SWP_NOCLIENTSIZE|SWP_NOCLIENTMOVE },
2291     { 0 }
2292 };
2293 /* DestroyWindow for MDI client window, initially visible */
2294 static const struct message WmDestroyMDIclientSeq[] = {
2295     { HCBT_DESTROYWND, hook },
2296     { 0x0090, sent|optional },
2297     { WM_PARENTNOTIFY, sent|wparam, WM_DESTROY }, /* in MDI frame */
2298     { WM_SHOWWINDOW, sent|wparam, 0 },
2299     { WM_WINDOWPOSCHANGING, sent|wparam|optional, SWP_HIDEWINDOW|SWP_NOACTIVATE|SWP_NOSIZE|SWP_NOMOVE },
2300     { EVENT_OBJECT_HIDE, winevent_hook|wparam|lparam, 0, 0 },
2301     { WM_WINDOWPOSCHANGED, sent|wparam|optional, SWP_HIDEWINDOW|SWP_NOACTIVATE|SWP_NOSIZE|SWP_NOMOVE|SWP_NOCLIENTSIZE|SWP_NOCLIENTMOVE },
2302     { EVENT_OBJECT_DESTROY, winevent_hook|wparam|lparam, 0, 0 },
2303     { WM_DESTROY, sent },
2304     { WM_NCDESTROY, sent },
2305     { 0 }
2306 };
2307 /* CreateWindow for MDI child window, initially visible */
2308 static const struct message WmCreateMDIchildVisibleSeq[] = {
2309     { HCBT_CREATEWND, hook },
2310     { WM_NCCREATE, sent }, 
2311     { WM_NCCALCSIZE, sent|wparam, 0 },
2312     { WM_CREATE, sent },
2313     { EVENT_OBJECT_CREATE, winevent_hook|wparam|lparam, 0, 0 },
2314     { WM_SIZE, sent|wparam, SIZE_RESTORED },
2315     { WM_MOVE, sent },
2316     /* Win2k sends wparam set to
2317      * MAKEWPARAM(WM_CREATE, MDI_FIRST_CHILD_ID + nTotalCreated),
2318      * while Win9x doesn't bother to set child window id according to
2319      * CLIENTCREATESTRUCT.idFirstChild
2320      */
2321     { WM_PARENTNOTIFY, sent /*|wparam, WM_CREATE*/ }, /* in MDI client */
2322     { WM_SHOWWINDOW, sent|wparam, 1 },
2323     { WM_WINDOWPOSCHANGING, sent|wparam, SWP_SHOWWINDOW|SWP_NOACTIVATE|SWP_NOSIZE|SWP_NOMOVE },
2324     { EVENT_OBJECT_SHOW, winevent_hook|wparam|lparam, 0, 0 },
2325     { WM_WINDOWPOSCHANGED, sent|wparam, SWP_SHOWWINDOW|SWP_NOACTIVATE|SWP_NOSIZE|SWP_NOMOVE|SWP_NOCLIENTSIZE|SWP_NOCLIENTMOVE },
2326     { WM_MDIREFRESHMENU, sent/*|wparam|lparam, 0, 0*/ },
2327     { WM_WINDOWPOSCHANGING, sent|wparam, SWP_SHOWWINDOW|SWP_NOSIZE|SWP_NOMOVE },
2328     { WM_CHILDACTIVATE, sent|wparam|lparam, 0, 0 },
2329     { WM_WINDOWPOSCHANGING, sent|wparam|defwinproc, SWP_NOACTIVATE|SWP_NOSIZE|SWP_NOMOVE },
2330
2331     /* Win9x: message sequence terminates here. */
2332
2333     { WM_NCACTIVATE, sent|wparam|defwinproc, 1 },
2334     { HCBT_SETFOCUS, hook }, /* in MDI client */
2335     { WM_IME_SETCONTEXT, sent|wparam|optional, 1 }, /* in MDI client */
2336     { WM_IME_NOTIFY, sent|wparam|optional, 2 }, /* in MDI client */
2337     { EVENT_OBJECT_FOCUS, winevent_hook|wparam|lparam, OBJID_CLIENT, 0 },
2338     { WM_SETFOCUS, sent }, /* in MDI client */
2339     { HCBT_SETFOCUS, hook },
2340     { WM_KILLFOCUS, sent }, /* in MDI client */
2341     { WM_IME_SETCONTEXT, sent|wparam|optional, 0 }, /* in MDI client */
2342     { WM_IME_SETCONTEXT, sent|wparam|defwinproc|optional, 1 },
2343     { EVENT_OBJECT_FOCUS, winevent_hook|wparam|lparam, OBJID_CLIENT, 0 },
2344     { WM_SETFOCUS, sent|defwinproc },
2345     { WM_MDIACTIVATE, sent|defwinproc },
2346     { 0 }
2347 };
2348 /* CreateWindow for MDI child window with invisible parent */
2349 static const struct message WmCreateMDIchildInvisibleParentSeq[] = {
2350     { HCBT_CREATEWND, hook },
2351     { WM_GETMINMAXINFO, sent },
2352     { WM_NCCREATE, sent }, 
2353     { WM_NCCALCSIZE, sent|wparam, 0 },
2354     { EVENT_OBJECT_REORDER, winevent_hook|wparam|lparam|optional, 0, 0 },
2355     { WM_CREATE, sent },
2356     { EVENT_OBJECT_CREATE, winevent_hook|wparam|lparam, 0, 0 },
2357     { WM_SIZE, sent|wparam, SIZE_RESTORED },
2358     { WM_MOVE, sent },
2359     { WM_PARENTNOTIFY, sent /*|wparam, WM_CREATE*/ }, /* in MDI client */
2360     { WM_SHOWWINDOW, sent|wparam, 1 },
2361     { WM_MDIREFRESHMENU, sent }, /* in MDI client */
2362     { WM_WINDOWPOSCHANGING, sent|wparam, SWP_SHOWWINDOW|SWP_NOSIZE|SWP_NOMOVE },
2363     { WM_CHILDACTIVATE, sent|wparam|lparam, 0, 0 },
2364     { WM_WINDOWPOSCHANGING, sent|wparam|defwinproc, SWP_NOACTIVATE|SWP_NOSIZE|SWP_NOMOVE },
2365
2366     /* Win9x: message sequence terminates here. */
2367
2368     { WM_NCACTIVATE, sent|wparam|defwinproc, 1 },
2369     { HCBT_SETFOCUS, hook }, /* in MDI client */
2370     { WM_IME_SETCONTEXT, sent|wparam|optional, 1 }, /* in MDI client */
2371     { WM_IME_NOTIFY, sent|wparam|optional, 2 }, /* in MDI client */
2372     { EVENT_OBJECT_FOCUS, winevent_hook|wparam|lparam, OBJID_CLIENT, 0 },
2373     { WM_SETFOCUS, sent }, /* in MDI client */
2374     { HCBT_SETFOCUS, hook },
2375     { WM_KILLFOCUS, sent }, /* in MDI client */
2376     { WM_IME_SETCONTEXT, sent|wparam|optional, 0 }, /* in MDI client */
2377     { WM_IME_SETCONTEXT, sent|wparam|defwinproc|optional, 1 },
2378     { EVENT_OBJECT_FOCUS, winevent_hook|wparam|lparam, OBJID_CLIENT, 0 },
2379     { WM_SETFOCUS, sent|defwinproc },
2380     { WM_MDIACTIVATE, sent|defwinproc },
2381     { 0 }
2382 };
2383 /* DestroyWindow for MDI child window, initially visible */
2384 static const struct message WmDestroyMDIchildVisibleSeq[] = {
2385     { HCBT_DESTROYWND, hook },
2386     /* Win2k sends wparam set to
2387      * MAKEWPARAM(WM_DESTROY, MDI_FIRST_CHILD_ID + nTotalCreated),
2388      * while Win9x doesn't bother to set child window id according to
2389      * CLIENTCREATESTRUCT.idFirstChild
2390      */
2391     { 0x0090, sent|optional },
2392     { WM_PARENTNOTIFY, sent /*|wparam, WM_DESTROY*/ }, /* in MDI client */
2393     { WM_SHOWWINDOW, sent|wparam, 0 },
2394     { WM_WINDOWPOSCHANGING, sent|wparam|optional, SWP_HIDEWINDOW|SWP_NOACTIVATE|SWP_NOSIZE|SWP_NOMOVE },
2395     { EVENT_OBJECT_HIDE, winevent_hook|wparam|lparam, 0, 0 },
2396     { WM_ERASEBKGND, sent|parent|optional },
2397     { WM_WINDOWPOSCHANGED, sent|wparam|optional, SWP_HIDEWINDOW|SWP_NOACTIVATE|SWP_NOSIZE|SWP_NOMOVE|SWP_NOCLIENTSIZE|SWP_NOCLIENTMOVE },
2398
2399     /* { WM_DESTROY, sent }
2400      * Win9x: message sequence terminates here.
2401      */
2402
2403     { HCBT_SETFOCUS, hook }, /* set focus to MDI client */
2404     { WM_KILLFOCUS, sent },
2405     { WM_IME_SETCONTEXT, sent|wparam|optional, 0 },
2406     { WM_IME_SETCONTEXT, sent|wparam|optional, 1 }, /* in MDI client */
2407     { EVENT_OBJECT_FOCUS, winevent_hook|wparam|lparam, OBJID_CLIENT, 0 },
2408     { WM_SETFOCUS, sent }, /* in MDI client */
2409
2410     { HCBT_SETFOCUS, hook }, /* MDI client sets focus back to MDI child */
2411     { WM_KILLFOCUS, sent }, /* in MDI client */
2412     { WM_IME_SETCONTEXT, sent|wparam|optional, 0 }, /* in MDI client */
2413     { WM_IME_SETCONTEXT, sent|wparam|optional, 1 },
2414     { EVENT_OBJECT_FOCUS, winevent_hook|wparam|lparam, OBJID_CLIENT, 0 },
2415     { WM_SETFOCUS, sent }, /* in MDI client */
2416
2417     { EVENT_OBJECT_DESTROY, winevent_hook|wparam|lparam, 0, 0 },
2418
2419     { HCBT_SETFOCUS, hook }, /* set focus to MDI client */
2420     { WM_KILLFOCUS, sent },
2421     { WM_IME_SETCONTEXT, sent|wparam|optional, 0 },
2422     { WM_IME_SETCONTEXT, sent|wparam|optional, 1 }, /* in MDI client */
2423     { EVENT_OBJECT_FOCUS, winevent_hook|wparam|lparam, OBJID_CLIENT, 0 },
2424     { WM_SETFOCUS, sent }, /* in MDI client */
2425
2426     { HCBT_SETFOCUS, hook }, /* MDI client sets focus back to MDI child */
2427     { WM_KILLFOCUS, sent }, /* in MDI client */
2428     { WM_IME_SETCONTEXT, sent|wparam|optional, 0 }, /* in MDI client */
2429     { WM_IME_SETCONTEXT, sent|wparam|optional, 1 },
2430     { EVENT_OBJECT_FOCUS, winevent_hook|wparam|lparam, OBJID_CLIENT, 0 },
2431     { WM_SETFOCUS, sent }, /* in MDI client */
2432
2433     { WM_DESTROY, sent },
2434
2435     { HCBT_SETFOCUS, hook }, /* set focus to MDI client */
2436     { WM_KILLFOCUS, sent },
2437     { WM_IME_SETCONTEXT, sent|wparam|optional, 0 },
2438     { WM_IME_SETCONTEXT, sent|wparam|optional, 1 }, /* in MDI client */
2439     { EVENT_OBJECT_FOCUS, winevent_hook|wparam|lparam, OBJID_CLIENT, 0 },
2440     { WM_SETFOCUS, sent }, /* in MDI client */
2441
2442     { HCBT_SETFOCUS, hook }, /* MDI client sets focus back to MDI child */
2443     { WM_KILLFOCUS, sent }, /* in MDI client */
2444     { WM_IME_SETCONTEXT, sent|wparam|optional, 0 }, /* in MDI client */
2445     { WM_IME_SETCONTEXT, sent|wparam|optional, 1 },
2446     { EVENT_OBJECT_FOCUS, winevent_hook|wparam|lparam, OBJID_CLIENT, 0 },
2447     { WM_SETFOCUS, sent }, /* in MDI client */
2448
2449     { WM_NCDESTROY, sent },
2450     { 0 }
2451 };
2452 /* CreateWindow for MDI child window, initially invisible */
2453 static const struct message WmCreateMDIchildInvisibleSeq[] = {
2454     { HCBT_CREATEWND, hook },
2455     { WM_NCCREATE, sent }, 
2456     { WM_NCCALCSIZE, sent|wparam, 0 },
2457     { WM_CREATE, sent },
2458     { EVENT_OBJECT_CREATE, winevent_hook|wparam|lparam, 0, 0 },
2459     { WM_SIZE, sent|wparam, SIZE_RESTORED },
2460     { WM_MOVE, sent },
2461     /* Win2k sends wparam set to
2462      * MAKEWPARAM(WM_CREATE, MDI_FIRST_CHILD_ID + nTotalCreated),
2463      * while Win9x doesn't bother to set child window id according to
2464      * CLIENTCREATESTRUCT.idFirstChild
2465      */
2466     { WM_PARENTNOTIFY, sent /*|wparam, WM_CREATE*/ }, /* in MDI client */
2467     { 0 }
2468 };
2469 /* DestroyWindow for MDI child window, initially invisible */
2470 static const struct message WmDestroyMDIchildInvisibleSeq[] = {
2471     { HCBT_DESTROYWND, hook },
2472     /* Win2k sends wparam set to
2473      * MAKEWPARAM(WM_DESTROY, MDI_FIRST_CHILD_ID + nTotalCreated),
2474      * while Win9x doesn't bother to set child window id according to
2475      * CLIENTCREATESTRUCT.idFirstChild
2476      */
2477     { 0x0090, sent|optional },
2478     { WM_PARENTNOTIFY, sent /*|wparam, WM_DESTROY*/ }, /* in MDI client */
2479     { EVENT_OBJECT_DESTROY, winevent_hook|wparam|lparam, 0, 0 },
2480     { WM_DESTROY, sent },
2481     { WM_NCDESTROY, sent },
2482     /* FIXME: Wine destroys an icon/title window while Windows doesn't */
2483     { WM_PARENTNOTIFY, sent|wparam|optional, WM_DESTROY }, /* MDI client */
2484     { 0 }
2485 };
2486 /* CreateWindow for the 1st MDI child window, initially visible and maximized */
2487 static const struct message WmCreateMDIchildVisibleMaxSeq1[] = {
2488     { HCBT_CREATEWND, hook },
2489     { WM_NCCREATE, sent }, 
2490     { WM_NCCALCSIZE, sent|wparam, 0 },
2491     { WM_CREATE, sent },
2492     { EVENT_OBJECT_CREATE, winevent_hook|wparam|lparam, 0, 0 },
2493     { WM_SIZE, sent|wparam, SIZE_RESTORED },
2494     { WM_MOVE, sent },
2495     { HCBT_MINMAX, hook|lparam, 0, SW_MAXIMIZE },
2496     { WM_GETMINMAXINFO, sent },
2497     { WM_WINDOWPOSCHANGING, sent|wparam, SWP_FRAMECHANGED|SWP_NOACTIVATE|SWP_STATECHANGED  },
2498     { WM_NCCALCSIZE, sent|wparam, 1 },
2499     { WM_WINDOWPOSCHANGED, sent|wparam, SWP_FRAMECHANGED|SWP_NOACTIVATE|SWP_NOMOVE|SWP_NOREDRAW|SWP_NOCLIENTMOVE|SWP_STATECHANGED },
2500     { WM_SIZE, sent|defwinproc|wparam, SIZE_MAXIMIZED },
2501      /* in MDI frame */
2502     { WM_WINDOWPOSCHANGING, sent|wparam, SWP_FRAMECHANGED|SWP_NOACTIVATE|SWP_NOSIZE|SWP_NOMOVE },
2503     { WM_NCCALCSIZE, sent|wparam, 1 },
2504     { WM_WINDOWPOSCHANGED, sent|wparam, SWP_FRAMECHANGED|SWP_NOACTIVATE|SWP_NOSIZE|SWP_NOMOVE|SWP_NOCLIENTSIZE|SWP_NOCLIENTMOVE },
2505     { EVENT_OBJECT_LOCATIONCHANGE, winevent_hook|wparam|lparam, 0, 0 }, /* MDI frame */
2506     { EVENT_OBJECT_LOCATIONCHANGE, winevent_hook|wparam|lparam, 0, 0 }, /* MDI child */
2507     /* Win2k sends wparam set to
2508      * MAKEWPARAM(WM_CREATE, MDI_FIRST_CHILD_ID + nTotalCreated),
2509      * while Win9x doesn't bother to set child window id according to
2510      * CLIENTCREATESTRUCT.idFirstChild
2511      */
2512     { WM_PARENTNOTIFY, sent /*|wparam, WM_CREATE*/ }, /* in MDI client */
2513     { WM_SHOWWINDOW, sent|wparam, 1 },
2514     { WM_WINDOWPOSCHANGING, sent|wparam, SWP_SHOWWINDOW|SWP_NOACTIVATE|SWP_NOSIZE|SWP_NOMOVE },
2515     { EVENT_OBJECT_SHOW, winevent_hook|wparam|lparam, 0, 0 },
2516     { WM_WINDOWPOSCHANGED, sent|wparam, SWP_SHOWWINDOW|SWP_NOACTIVATE|SWP_NOSIZE|SWP_NOMOVE|SWP_NOCLIENTSIZE|SWP_NOCLIENTMOVE },
2517     { WM_MDIREFRESHMENU, sent/*|wparam|lparam, 0, 0*/ },
2518     { WM_WINDOWPOSCHANGING, sent|wparam, SWP_SHOWWINDOW|SWP_NOSIZE|SWP_NOMOVE },
2519     { WM_CHILDACTIVATE, sent|wparam|lparam, 0, 0 },
2520     { WM_WINDOWPOSCHANGING, sent|wparam|defwinproc|optional, SWP_NOACTIVATE|SWP_NOSIZE|SWP_NOMOVE, 0, SWP_FRAMECHANGED },
2521
2522     /* Win9x: message sequence terminates here. */
2523
2524     { WM_NCACTIVATE, sent|wparam|defwinproc|optional, 1 },
2525     { HCBT_SETFOCUS, hook|optional }, /* in MDI client */
2526     { WM_IME_SETCONTEXT, sent|wparam|optional, 1 }, /* in MDI client */
2527     { WM_IME_NOTIFY, sent|wparam|optional, 2 }, /* in MDI client */
2528     { EVENT_OBJECT_FOCUS, winevent_hook|wparam|lparam, OBJID_CLIENT, 0 },
2529     { WM_SETFOCUS, sent|optional }, /* in MDI client */
2530     { HCBT_SETFOCUS, hook|optional },
2531     { WM_KILLFOCUS, sent|optional }, /* in MDI client */
2532     { WM_IME_SETCONTEXT, sent|wparam|optional, 0 }, /* in MDI client */
2533     { WM_IME_SETCONTEXT, sent|wparam|defwinproc|optional, 1 },
2534     { EVENT_OBJECT_FOCUS, winevent_hook|wparam|lparam, OBJID_CLIENT, 0 },
2535     { WM_SETFOCUS, sent|defwinproc|optional },
2536     { WM_MDIACTIVATE, sent|defwinproc|optional },
2537      /* in MDI frame */
2538     { WM_WINDOWPOSCHANGING, sent|wparam, SWP_FRAMECHANGED|SWP_NOACTIVATE|SWP_NOSIZE|SWP_NOMOVE },
2539     { WM_NCCALCSIZE, sent|wparam, 1 },
2540     { WM_WINDOWPOSCHANGED, sent|wparam, SWP_FRAMECHANGED|SWP_NOACTIVATE|SWP_NOSIZE|SWP_NOMOVE|SWP_NOCLIENTSIZE|SWP_NOCLIENTMOVE },
2541     { EVENT_OBJECT_LOCATIONCHANGE, winevent_hook|wparam|lparam, 0, 0 }, /* MDI frame */
2542     { 0 }
2543 };
2544 /* CreateWindow for the 2nd MDI child window, initially visible and maximized */
2545 static const struct message WmCreateMDIchildVisibleMaxSeq2[] = {
2546     /* restore the 1st MDI child */
2547     { WM_SETREDRAW, sent|wparam, 0 },
2548     { HCBT_MINMAX, hook|lparam, 0, SW_SHOWNORMAL },
2549     { WM_WINDOWPOSCHANGING, sent|wparam, SWP_FRAMECHANGED|SWP_STATECHANGED },
2550     { WM_NCCALCSIZE, sent|wparam, 1 },
2551     { WM_CHILDACTIVATE, sent|wparam|lparam, 0, 0 },
2552     { WM_WINDOWPOSCHANGED, sent|wparam, SWP_FRAMECHANGED|SWP_NOMOVE|SWP_NOREDRAW|SWP_NOCLIENTMOVE|SWP_STATECHANGED },
2553     { WM_SIZE, sent|defwinproc|wparam, SIZE_RESTORED },
2554      /* in MDI frame */
2555     { WM_WINDOWPOSCHANGING, sent|wparam, SWP_FRAMECHANGED|SWP_NOACTIVATE|SWP_NOSIZE|SWP_NOMOVE },
2556     { WM_NCCALCSIZE, sent|wparam, 1 },
2557     { WM_WINDOWPOSCHANGED, sent|wparam, SWP_FRAMECHANGED|SWP_NOACTIVATE|SWP_NOSIZE|SWP_NOMOVE|SWP_NOCLIENTSIZE|SWP_NOCLIENTMOVE },
2558     { EVENT_OBJECT_LOCATIONCHANGE, winevent_hook|wparam|lparam, 0, 0 }, /* MDI frame */
2559     { EVENT_OBJECT_LOCATIONCHANGE, winevent_hook|wparam|lparam, 0, 0 }, /* MDI child */
2560     { WM_SETREDRAW, sent|wparam, 1 }, /* in the 1st MDI child */
2561     /* create the 2nd MDI child */
2562     { HCBT_CREATEWND, hook },
2563     { WM_NCCREATE, sent }, 
2564     { WM_NCCALCSIZE, sent|wparam, 0 },
2565     { WM_CREATE, sent },
2566     { EVENT_OBJECT_CREATE, winevent_hook|wparam|lparam, 0, 0 },
2567     { WM_SIZE, sent|wparam, SIZE_RESTORED },
2568     { WM_MOVE, sent },
2569     { HCBT_MINMAX, hook|lparam, 0, SW_MAXIMIZE },
2570     { WM_GETMINMAXINFO, sent },
2571     { WM_WINDOWPOSCHANGING, sent|wparam, SWP_FRAMECHANGED|SWP_NOACTIVATE|SWP_STATECHANGED },
2572     { WM_NCCALCSIZE, sent|wparam, 1 },
2573     { EVENT_OBJECT_REORDER, winevent_hook|wparam|lparam, OBJID_CLIENT, 0 },
2574     { WM_WINDOWPOSCHANGED, sent|wparam, SWP_FRAMECHANGED|SWP_NOACTIVATE|SWP_NOMOVE|SWP_NOREDRAW|SWP_NOCLIENTMOVE|SWP_STATECHANGED },
2575     { WM_SIZE, sent|defwinproc|wparam, SIZE_MAXIMIZED },
2576      /* in MDI frame */
2577     { WM_WINDOWPOSCHANGING, sent|wparam, SWP_FRAMECHANGED|SWP_NOACTIVATE|SWP_NOSIZE|SWP_NOMOVE },
2578     { WM_NCCALCSIZE, sent|wparam, 1 },
2579     { WM_WINDOWPOSCHANGED, sent|wparam, SWP_FRAMECHANGED|SWP_NOACTIVATE|SWP_NOSIZE|SWP_NOMOVE|SWP_NOCLIENTSIZE|SWP_NOCLIENTMOVE },
2580     { EVENT_OBJECT_LOCATIONCHANGE, winevent_hook|wparam|lparam, 0, 0 }, /* MDI frame */
2581     { EVENT_OBJECT_LOCATIONCHANGE, winevent_hook|wparam|lparam, 0, 0 }, /* MDI child */
2582     /* Win2k sends wparam set to
2583      * MAKEWPARAM(WM_CREATE, MDI_FIRST_CHILD_ID + nTotalCreated),
2584      * while Win9x doesn't bother to set child window id according to
2585      * CLIENTCREATESTRUCT.idFirstChild
2586      */
2587     { WM_PARENTNOTIFY, sent /*|wparam, WM_CREATE*/ }, /* in MDI client */
2588     { WM_SHOWWINDOW, sent|wparam, 1 },
2589     { WM_WINDOWPOSCHANGING, sent|wparam, SWP_SHOWWINDOW|SWP_NOACTIVATE|SWP_NOSIZE|SWP_NOMOVE },
2590     { EVENT_OBJECT_SHOW, winevent_hook|wparam|lparam, 0, 0 },
2591     { WM_WINDOWPOSCHANGED, sent|wparam, SWP_SHOWWINDOW|SWP_NOACTIVATE|SWP_NOSIZE|SWP_NOMOVE|SWP_NOCLIENTSIZE|SWP_NOCLIENTMOVE },
2592     { WM_MDIREFRESHMENU, sent/*|wparam|lparam, 0, 0*/ },
2593     { WM_WINDOWPOSCHANGING, sent|wparam, SWP_SHOWWINDOW|SWP_NOSIZE|SWP_NOMOVE },
2594     { WM_CHILDACTIVATE, sent|wparam|lparam, 0, 0 },
2595
2596     { WM_NCACTIVATE, sent|wparam|defwinproc, 0 }, /* in the 1st MDI child */
2597     { WM_MDIACTIVATE, sent|defwinproc }, /* in the 1st MDI child */
2598
2599     { WM_WINDOWPOSCHANGING, sent|wparam|defwinproc, SWP_NOACTIVATE|SWP_NOSIZE|SWP_NOMOVE },
2600
2601     /* Win9x: message sequence terminates here. */
2602
2603     { WM_NCACTIVATE, sent|wparam|defwinproc, 1 },
2604     { HCBT_SETFOCUS, hook },
2605     { WM_KILLFOCUS, sent|defwinproc|optional }, /* in the 1st MDI child */
2606     { WM_IME_SETCONTEXT, sent|wparam|defwinproc|optional, 0 }, /* in the 1st MDI child */
2607     { WM_IME_SETCONTEXT, sent|wparam|optional, 1 }, /* in MDI client */
2608     { EVENT_OBJECT_FOCUS, winevent_hook|wparam|lparam, OBJID_CLIENT, 0 },
2609     { WM_SETFOCUS, sent }, /* in MDI client */
2610     { HCBT_SETFOCUS, hook },
2611     { WM_KILLFOCUS, sent }, /* in MDI client */
2612     { WM_IME_SETCONTEXT, sent|wparam|optional, 0 }, /* in MDI client */
2613     { WM_IME_SETCONTEXT, sent|wparam|defwinproc|optional, 1 },
2614     { EVENT_OBJECT_FOCUS, winevent_hook|wparam|lparam, OBJID_CLIENT, 0 },
2615     { WM_SETFOCUS, sent|defwinproc },
2616
2617     { WM_MDIACTIVATE, sent|defwinproc },
2618      /* in MDI frame */
2619     { WM_WINDOWPOSCHANGING, sent|wparam, SWP_FRAMECHANGED|SWP_NOACTIVATE|SWP_NOSIZE|SWP_NOMOVE },
2620     { WM_NCCALCSIZE, sent|wparam, 1 },
2621     { WM_WINDOWPOSCHANGED, sent|wparam, SWP_FRAMECHANGED|SWP_NOACTIVATE|SWP_NOSIZE|SWP_NOMOVE|SWP_NOCLIENTSIZE|SWP_NOCLIENTMOVE },
2622     { EVENT_OBJECT_LOCATIONCHANGE, winevent_hook|wparam|lparam, 0, 0 }, /* MDI frame */
2623     { 0 }
2624 };
2625 /* WM_MDICREATE MDI child window, initially visible and maximized */
2626 static const struct message WmCreateMDIchildVisibleMaxSeq3[] = {
2627     { WM_MDICREATE, sent },
2628     { HCBT_CREATEWND, hook },
2629     { WM_NCCREATE, sent }, 
2630     { WM_NCCALCSIZE, sent|wparam, 0 },
2631     { WM_CREATE, sent },
2632     { EVENT_OBJECT_CREATE, winevent_hook|wparam|lparam, 0, 0 },
2633     { WM_SIZE, sent|wparam, SIZE_RESTORED },
2634     { WM_MOVE, sent },
2635     { HCBT_MINMAX, hook|lparam, 0, SW_MAXIMIZE },
2636     { WM_GETMINMAXINFO, sent },
2637     { WM_WINDOWPOSCHANGING, sent|wparam, SWP_FRAMECHANGED|SWP_NOACTIVATE|SWP_STATECHANGED },
2638     { WM_NCCALCSIZE, sent|wparam, 1 },
2639     { WM_WINDOWPOSCHANGED, sent|wparam, SWP_FRAMECHANGED|SWP_NOACTIVATE|SWP_NOMOVE|SWP_NOREDRAW|SWP_NOCLIENTMOVE|SWP_STATECHANGED },
2640     { WM_SIZE, sent|defwinproc|wparam, SIZE_MAXIMIZED },
2641
2642      /* in MDI frame */
2643     { WM_WINDOWPOSCHANGING, sent|wparam, SWP_FRAMECHANGED|SWP_NOACTIVATE|SWP_NOSIZE|SWP_NOMOVE },
2644     { WM_NCCALCSIZE, sent|wparam, 1 },
2645     { WM_WINDOWPOSCHANGED, sent|wparam, SWP_FRAMECHANGED|SWP_NOACTIVATE|SWP_NOSIZE|SWP_NOMOVE|SWP_NOCLIENTSIZE|SWP_NOCLIENTMOVE },
2646     { EVENT_OBJECT_LOCATIONCHANGE, winevent_hook|wparam|lparam, 0, 0 }, /* MDI frame */
2647     { EVENT_OBJECT_LOCATIONCHANGE, winevent_hook|wparam|lparam, 0, 0 }, /* MDI child */
2648
2649     /* Win2k sends wparam set to
2650      * MAKEWPARAM(WM_CREATE, MDI_FIRST_CHILD_ID + nTotalCreated),
2651      * while Win9x doesn't bother to set child window id according to
2652      * CLIENTCREATESTRUCT.idFirstChild
2653      */
2654     { WM_PARENTNOTIFY, sent /*|wparam, WM_CREATE*/ }, /* in MDI client */
2655     { WM_SHOWWINDOW, sent|wparam, 1 },
2656     { WM_WINDOWPOSCHANGING, sent|wparam, SWP_SHOWWINDOW|SWP_NOACTIVATE|SWP_NOSIZE|SWP_NOMOVE },
2657
2658     { EVENT_OBJECT_SHOW, winevent_hook|wparam|lparam, 0, 0 },
2659
2660     { WM_WINDOWPOSCHANGED, sent|wparam, SWP_SHOWWINDOW|SWP_NOACTIVATE|SWP_NOSIZE|SWP_NOMOVE|SWP_NOCLIENTSIZE|SWP_NOCLIENTMOVE },
2661     { WM_MDIREFRESHMENU, sent/*|wparam|lparam, 0, 0*/ },
2662     { WM_WINDOWPOSCHANGING, sent|wparam, SWP_SHOWWINDOW|SWP_NOSIZE|SWP_NOMOVE },
2663
2664     { WM_CHILDACTIVATE, sent|wparam|lparam, 0, 0 },
2665     { WM_WINDOWPOSCHANGING, sent|wparam|defwinproc, SWP_NOACTIVATE|SWP_NOSIZE|SWP_NOMOVE },
2666
2667     /* Win9x: message sequence terminates here. */
2668
2669     { WM_NCACTIVATE, sent|wparam|defwinproc, 1 },
2670     { WM_SETFOCUS, sent|optional }, /* in MDI client */
2671     { HCBT_SETFOCUS, hook }, /* in MDI client */
2672     { WM_IME_SETCONTEXT, sent|wparam|optional, 1 }, /* in MDI client */
2673     { WM_IME_NOTIFY, sent|wparam|optional, 2 },
2674     { EVENT_OBJECT_FOCUS, winevent_hook|wparam|lparam|optional, OBJID_CLIENT, 0 },
2675     { WM_SETFOCUS, sent|optional }, /* in MDI client */
2676     { HCBT_SETFOCUS, hook|optional },
2677     { WM_KILLFOCUS, sent }, /* in MDI client */
2678     { WM_IME_SETCONTEXT, sent|wparam|optional, 0 }, /* in MDI client */
2679     { WM_IME_SETCONTEXT, sent|wparam|defwinproc|optional, 1 },
2680     { EVENT_OBJECT_FOCUS, winevent_hook|wparam|lparam, OBJID_CLIENT, 0 },
2681     { WM_SETFOCUS, sent|defwinproc },
2682
2683     { WM_MDIACTIVATE, sent|defwinproc },
2684
2685      /* in MDI child */
2686     { WM_WINDOWPOSCHANGING, sent|wparam, SWP_FRAMECHANGED|SWP_NOACTIVATE|SWP_NOSIZE|SWP_NOMOVE },
2687     { WM_NCCALCSIZE, sent|wparam, 1 },
2688     { WM_WINDOWPOSCHANGED, sent|wparam, SWP_FRAMECHANGED|SWP_NOACTIVATE|SWP_NOSIZE|SWP_NOMOVE|SWP_NOCLIENTSIZE|SWP_NOCLIENTMOVE },
2689     { EVENT_OBJECT_LOCATIONCHANGE, winevent_hook|wparam|lparam, 0, 0 },
2690
2691      /* in MDI frame */
2692     { WM_WINDOWPOSCHANGING, sent|wparam, SWP_FRAMECHANGED|SWP_NOACTIVATE|SWP_NOSIZE|SWP_NOMOVE },
2693     { WM_NCCALCSIZE, sent|wparam, 1 },
2694     { 0x0093, sent|defwinproc|optional },
2695     { 0x0093, sent|defwinproc|optional },
2696     { 0x0093, sent|defwinproc|optional },
2697     { WM_WINDOWPOSCHANGED, sent|wparam, SWP_FRAMECHANGED|SWP_NOACTIVATE|SWP_NOSIZE|SWP_NOMOVE },
2698     { WM_MOVE, sent|defwinproc },
2699     { WM_SIZE, sent|defwinproc|wparam, SIZE_RESTORED },
2700
2701      /* in MDI client */
2702     { WM_WINDOWPOSCHANGING, sent|wparam, SWP_NOACTIVATE },
2703     { WM_NCCALCSIZE, sent|wparam, 1 },
2704     { WM_WINDOWPOSCHANGED, sent|wparam, SWP_NOACTIVATE|SWP_NOMOVE|SWP_NOCLIENTMOVE },
2705     { WM_SIZE, sent|wparam, SIZE_RESTORED },
2706
2707      /* in MDI child */
2708     { WM_WINDOWPOSCHANGING, sent|wparam, SWP_NOACTIVATE },
2709     { WM_NCCALCSIZE, sent|wparam, 1 },
2710     { 0x0093, sent|optional },
2711     { WM_WINDOWPOSCHANGED, sent|wparam, SWP_NOACTIVATE|SWP_NOMOVE|SWP_NOCLIENTMOVE },
2712     { WM_SIZE, sent|defwinproc|wparam, SIZE_MAXIMIZED },
2713
2714     { 0x0093, sent|optional },
2715     { EVENT_OBJECT_LOCATIONCHANGE, winevent_hook|wparam|lparam, 0, 0 }, /* MDI child */
2716     { EVENT_OBJECT_LOCATIONCHANGE, winevent_hook|wparam|lparam, 0, 0 }, /* MDI client */
2717     { WM_NCCALCSIZE, sent|wparam|optional, 1 }, /* XP sends it to MDI frame */
2718     { 0x0093, sent|defwinproc|optional },
2719     { 0x0093, sent|defwinproc|optional },
2720     { 0x0093, sent|defwinproc|optional },
2721     { EVENT_OBJECT_LOCATIONCHANGE, winevent_hook|wparam|lparam, 0, 0 }, /* MDI frame */
2722     { EVENT_OBJECT_LOCATIONCHANGE, winevent_hook|wparam|lparam, 0, 0 }, /* XP sends a duplicate */
2723
2724     { 0 }
2725 };
2726 /* CreateWindow for the 1st MDI child window, initially invisible and maximized */
2727 static const struct message WmCreateMDIchildInvisibleMaxSeq4[] = {
2728     { HCBT_CREATEWND, hook },
2729     { WM_GETMINMAXINFO, sent },
2730     { WM_NCCREATE, sent }, 
2731     { WM_NCCALCSIZE, sent|wparam, 0 },
2732     { EVENT_OBJECT_REORDER, winevent_hook|wparam|lparam, 0, 0 },
2733     { WM_CREATE, sent },
2734     { EVENT_OBJECT_CREATE, winevent_hook|wparam|lparam, 0, 0 },
2735     { WM_SIZE, sent|wparam, SIZE_RESTORED },
2736     { WM_WINDOWPOSCHANGING, sent|wparam|optional, SWP_FRAMECHANGED|SWP_NOACTIVATE|SWP_NOSIZE|SWP_NOMOVE, 0, SWP_NOZORDER }, /* MDI frame */
2737     { WM_NCCALCSIZE, sent|wparam|optional, 1 }, /* MDI frame */
2738     { WM_WINDOWPOSCHANGED, sent|wparam|optional, SWP_FRAMECHANGED|SWP_NOACTIVATE|SWP_NOSIZE|SWP_NOMOVE|SWP_NOCLIENTSIZE|SWP_NOCLIENTMOVE, 0, SWP_NOZORDER }, /* MDI frame */
2739     { WM_MOVE, sent },
2740     { HCBT_MINMAX, hook|lparam, 0, SW_MAXIMIZE },
2741     { WM_GETMINMAXINFO, sent },
2742     { WM_WINDOWPOSCHANGING, sent|wparam, SWP_FRAMECHANGED|SWP_NOACTIVATE|SWP_STATECHANGED },
2743     { WM_GETMINMAXINFO, sent|defwinproc },
2744     { WM_NCCALCSIZE, sent|wparam, 1 },
2745     { WM_WINDOWPOSCHANGED, sent|wparam, SWP_FRAMECHANGED|SWP_NOACTIVATE|SWP_NOREDRAW|SWP_STATECHANGED },
2746     { WM_MOVE, sent|defwinproc },
2747     { WM_SIZE, sent|defwinproc|wparam, SIZE_MAXIMIZED },
2748      /* in MDI frame */
2749     { WM_WINDOWPOSCHANGING, sent|wparam, SWP_FRAMECHANGED|SWP_NOACTIVATE|SWP_NOSIZE|SWP_NOMOVE },
2750     { WM_NCCALCSIZE, sent|wparam, 1 },
2751     { WM_WINDOWPOSCHANGED, sent|wparam, SWP_FRAMECHANGED|SWP_NOACTIVATE|SWP_NOSIZE|SWP_NOMOVE|SWP_NOCLIENTSIZE|SWP_NOCLIENTMOVE },
2752     { EVENT_OBJECT_LOCATIONCHANGE, winevent_hook|wparam|lparam, 0, 0 }, /* MDI frame */
2753     { WM_NCCALCSIZE, sent|wparam|optional, 1 }, /* MDI child */
2754     { EVENT_OBJECT_LOCATIONCHANGE, winevent_hook|wparam|lparam, 0, 0 }, /* MDI child */
2755     { EVENT_OBJECT_LOCATIONCHANGE, winevent_hook|wparam|lparam, 0, 0 }, /* MDI child */
2756     /* Win2k sends wparam set to
2757      * MAKEWPARAM(WM_CREATE, MDI_FIRST_CHILD_ID + nTotalCreated),
2758      * while Win9x doesn't bother to set child window id according to
2759      * CLIENTCREATESTRUCT.idFirstChild
2760      */
2761     { WM_PARENTNOTIFY, sent /*|wparam, WM_CREATE*/ }, /* in MDI client */
2762     { 0 }
2763 };
2764 /* WM_SYSCOMMAND/SC_CLOSE for the 2nd MDI child window, initially visible and maximized */
2765 static const struct message WmDestroyMDIchildVisibleMaxSeq2[] = {
2766     { WM_SYSCOMMAND, sent|wparam, SC_CLOSE },
2767     { HCBT_SYSCOMMAND, hook },
2768     { WM_CLOSE, sent|defwinproc },
2769     { WM_MDIDESTROY, sent }, /* in MDI client */
2770
2771     /* bring the 1st MDI child to top */
2772     { WM_WINDOWPOSCHANGING, sent|wparam|defwinproc, SWP_NOSIZE|SWP_NOMOVE }, /* in the 1st MDI child */
2773     { WM_WINDOWPOSCHANGING, sent|wparam|defwinproc, SWP_NOACTIVATE|SWP_NOSIZE|SWP_NOMOVE }, /* in the 2nd MDI child */
2774
2775     { EVENT_OBJECT_REORDER, winevent_hook|wparam|lparam, OBJID_CLIENT, 0 },
2776
2777     { WM_CHILDACTIVATE, sent|defwinproc|wparam|lparam, 0, 0 }, /* in the 1st MDI child */
2778     { WM_NCACTIVATE, sent|wparam|defwinproc, 0 }, /* in the 1st MDI child */
2779     { WM_MDIACTIVATE, sent|defwinproc }, /* in the 1st MDI child */
2780
2781     /* maximize the 1st MDI child */
2782     { HCBT_MINMAX, hook|lparam, 0, SW_MAXIMIZE },
2783     { WM_GETMINMAXINFO, sent|defwinproc },
2784     { WM_WINDOWPOSCHANGING, sent|wparam|defwinproc, SWP_FRAMECHANGED|SWP_STATECHANGED },
2785     { WM_NCCALCSIZE, sent|defwinproc|wparam, 1 },
2786     { WM_CHILDACTIVATE, sent|defwinproc|wparam|lparam, 0, 0 },
2787     { WM_WINDOWPOSCHANGED, sent|wparam|defwinproc, SWP_FRAMECHANGED|SWP_NOMOVE|SWP_NOREDRAW|SWP_NOCLIENTMOVE|SWP_STATECHANGED },
2788     { WM_SIZE, sent|defwinproc|wparam, SIZE_MAXIMIZED },
2789
2790     /* restore the 2nd MDI child */
2791     { WM_SETREDRAW, sent|defwinproc|wparam, 0 },
2792     { HCBT_MINMAX, hook|lparam, 0, SW_NORMALNA },
2793     { WM_WINDOWPOSCHANGING, sent|wparam|defwinproc, SWP_NOACTIVATE|SWP_FRAMECHANGED|SWP_SHOWWINDOW|SWP_STATECHANGED },
2794     { WM_NCCALCSIZE, sent|defwinproc|wparam, 1 },
2795
2796     { EVENT_OBJECT_SHOW, winevent_hook|wparam|lparam, 0, 0 },
2797
2798     { WM_WINDOWPOSCHANGED, sent|wparam|defwinproc, SWP_NOACTIVATE|SWP_FRAMECHANGED|SWP_SHOWWINDOW|SWP_NOMOVE|SWP_NOREDRAW|SWP_NOCLIENTMOVE|SWP_STATECHANGED },
2799     { WM_SIZE, sent|defwinproc|wparam, SIZE_RESTORED },
2800
2801     { EVENT_OBJECT_LOCATIONCHANGE, winevent_hook|wparam|lparam, 0, 0 }, /* MDI frame */
2802
2803     { WM_SETREDRAW, sent|defwinproc|wparam, 1 },
2804      /* in MDI frame */
2805     { WM_WINDOWPOSCHANGING, sent|wparam, SWP_FRAMECHANGED|SWP_NOACTIVATE|SWP_NOSIZE|SWP_NOMOVE },
2806     { WM_NCCALCSIZE, sent|wparam, 1 },
2807     { WM_WINDOWPOSCHANGED, sent|wparam, SWP_FRAMECHANGED|SWP_NOACTIVATE|SWP_NOSIZE|SWP_NOMOVE|SWP_NOCLIENTSIZE|SWP_NOCLIENTMOVE },
2808     { EVENT_OBJECT_LOCATIONCHANGE, winevent_hook|wparam|lparam, 0, 0 }, /* MDI frame */
2809     { EVENT_OBJECT_LOCATIONCHANGE, winevent_hook|wparam|lparam, 0, 0 }, /* MDI child */
2810
2811     /* bring the 1st MDI child to top */
2812     { WM_WINDOWPOSCHANGING, sent|wparam|defwinproc, SWP_NOACTIVATE|SWP_NOSIZE|SWP_NOMOVE },
2813     { WM_NCACTIVATE, sent|wparam|defwinproc, 1 },
2814     { HCBT_SETFOCUS, hook },
2815     { WM_KILLFOCUS, sent|defwinproc },
2816     { WM_IME_SETCONTEXT, sent|wparam|defwinproc|optional, 0 },
2817     { WM_IME_SETCONTEXT, sent|wparam|optional, 1 }, /* in MDI client */
2818     { EVENT_OBJECT_FOCUS, winevent_hook|wparam|lparam, OBJID_CLIENT, 0 },
2819     { WM_SETFOCUS, sent }, /* in MDI client */
2820     { HCBT_SETFOCUS, hook },
2821     { WM_KILLFOCUS, sent }, /* in MDI client */
2822     { WM_IME_SETCONTEXT, sent|wparam|optional, 0 }, /* in MDI client */
2823     { WM_IME_SETCONTEXT, sent|wparam|defwinproc|optional, 1 },
2824     { EVENT_OBJECT_FOCUS, winevent_hook|wparam|lparam, OBJID_CLIENT, 0 },
2825     { WM_SETFOCUS, sent|defwinproc },
2826     { WM_MDIACTIVATE, sent|defwinproc },
2827     { WM_WINDOWPOSCHANGED, sent|wparam|defwinproc, SWP_NOSIZE|SWP_NOMOVE|SWP_NOREDRAW|SWP_NOCLIENTSIZE|SWP_NOCLIENTMOVE },
2828
2829     /* apparently ShowWindow(SW_SHOW) on an MDI client */
2830     { WM_SHOWWINDOW, sent|wparam, 1 },
2831     { WM_WINDOWPOSCHANGING, sent|wparam, SWP_SHOWWINDOW|SWP_NOACTIVATE|SWP_NOSIZE|SWP_NOMOVE },
2832     { EVENT_OBJECT_SHOW, winevent_hook|wparam|lparam, 0, 0 },
2833     { WM_WINDOWPOSCHANGED, sent|wparam, SWP_SHOWWINDOW|SWP_NOACTIVATE|SWP_NOSIZE|SWP_NOMOVE|SWP_NOCLIENTSIZE|SWP_NOCLIENTMOVE },
2834     { WM_MDIREFRESHMENU, sent },
2835
2836     { HCBT_DESTROYWND, hook },
2837     /* Win2k sends wparam set to
2838      * MAKEWPARAM(WM_DESTROY, MDI_FIRST_CHILD_ID + nTotalCreated),
2839      * while Win9x doesn't bother to set child window id according to
2840      * CLIENTCREATESTRUCT.idFirstChild
2841      */
2842     { 0x0090, sent|defwinproc|optional },
2843     { WM_PARENTNOTIFY, sent /*|wparam, WM_DESTROY*/ }, /* in MDI client */
2844     { WM_SHOWWINDOW, sent|defwinproc|wparam, 0 },
2845     { WM_WINDOWPOSCHANGING, sent|wparam|defwinproc, SWP_HIDEWINDOW|SWP_NOACTIVATE|SWP_NOSIZE|SWP_NOMOVE },
2846     { EVENT_OBJECT_HIDE, winevent_hook|wparam|lparam, 0, 0 },
2847     { WM_ERASEBKGND, sent|parent|optional },
2848     { WM_WINDOWPOSCHANGED, sent|wparam|defwinproc, SWP_HIDEWINDOW|SWP_NOACTIVATE|SWP_NOSIZE|SWP_NOMOVE|SWP_NOCLIENTSIZE|SWP_NOCLIENTMOVE },
2849
2850     { EVENT_OBJECT_DESTROY, winevent_hook|wparam|lparam, 0, 0 },
2851     { WM_DESTROY, sent|defwinproc },
2852     { WM_NCDESTROY, sent|defwinproc },
2853     { 0 }
2854 };
2855 /* WM_MDIDESTROY for the single MDI child window, initially visible and maximized */
2856 static const struct message WmDestroyMDIchildVisibleMaxSeq1[] = {
2857     { WM_MDIDESTROY, sent }, /* in MDI client */
2858     { WM_SHOWWINDOW, sent|wparam, 0 },
2859     { WM_WINDOWPOSCHANGING, sent|wparam, SWP_HIDEWINDOW|SWP_NOACTIVATE|SWP_NOSIZE|SWP_NOMOVE },
2860     { EVENT_OBJECT_HIDE, winevent_hook|wparam|lparam, 0, 0 },
2861     { WM_ERASEBKGND, sent|parent|optional },
2862     { WM_WINDOWPOSCHANGED, sent|wparam, SWP_HIDEWINDOW|SWP_NOACTIVATE|SWP_NOSIZE|SWP_NOMOVE|SWP_NOCLIENTSIZE|SWP_NOCLIENTMOVE },
2863
2864     { HCBT_SETFOCUS, hook },
2865     { WM_KILLFOCUS, sent },
2866     { WM_IME_SETCONTEXT, sent|wparam|optional, 0 },
2867     { WM_IME_SETCONTEXT, sent|wparam|optional, 1 }, /* in MDI client */
2868     { EVENT_OBJECT_FOCUS, winevent_hook|wparam|lparam, OBJID_CLIENT, 0 },
2869     { WM_SETFOCUS, sent }, /* in MDI client */
2870     { HCBT_SETFOCUS, hook },
2871     { WM_KILLFOCUS, sent }, /* in MDI client */
2872     { WM_IME_SETCONTEXT, sent|wparam|optional, 0 }, /* in MDI client */
2873     { WM_IME_SETCONTEXT, sent|wparam|optional, 1 },
2874     { EVENT_OBJECT_FOCUS, winevent_hook|wparam|lparam, OBJID_CLIENT, 0 },
2875     { WM_SETFOCUS, sent },
2876
2877      /* in MDI child */
2878     { WM_WINDOWPOSCHANGING, sent|wparam, SWP_FRAMECHANGED|SWP_NOACTIVATE|SWP_NOSIZE|SWP_NOMOVE },
2879     { WM_NCCALCSIZE, sent|wparam, 1 },
2880     { WM_WINDOWPOSCHANGED, sent|wparam, SWP_FRAMECHANGED|SWP_NOACTIVATE|SWP_NOSIZE|SWP_NOMOVE|SWP_NOREDRAW|SWP_NOCLIENTSIZE|SWP_NOCLIENTMOVE },
2881     { EVENT_OBJECT_LOCATIONCHANGE, winevent_hook|wparam|lparam, 0, 0 }, /* MDI child */
2882
2883      /* in MDI frame */
2884     { WM_WINDOWPOSCHANGING, sent|wparam, SWP_FRAMECHANGED|SWP_NOACTIVATE|SWP_NOSIZE|SWP_NOMOVE },
2885     { WM_NCCALCSIZE, sent|wparam, 1 },
2886     { 0x0093, sent|defwinproc|optional },
2887     { 0x0093, sent|defwinproc|optional },
2888     { WM_WINDOWPOSCHANGED, sent|wparam, SWP_FRAMECHANGED|SWP_NOACTIVATE|SWP_NOSIZE|SWP_NOMOVE },
2889     { WM_MOVE, sent|defwinproc },
2890     { WM_SIZE, sent|defwinproc|wparam, SIZE_RESTORED },
2891
2892      /* in MDI client */
2893     { WM_WINDOWPOSCHANGING, sent|wparam, SWP_NOACTIVATE },
2894     { WM_NCCALCSIZE, sent|wparam, 1 },
2895     { WM_WINDOWPOSCHANGED, sent|wparam, SWP_NOACTIVATE|SWP_NOMOVE|SWP_NOCLIENTMOVE },
2896     { WM_SIZE, sent|wparam, SIZE_RESTORED },
2897
2898      /* in MDI child */
2899     { WM_WINDOWPOSCHANGING, sent|wparam, SWP_NOACTIVATE },
2900     { WM_NCCALCSIZE, sent|wparam, 1 },
2901     { WM_WINDOWPOSCHANGED, sent|wparam, SWP_NOACTIVATE|SWP_NOMOVE|SWP_NOREDRAW|SWP_NOCLIENTMOVE },
2902     { WM_SIZE, sent|defwinproc|wparam, SIZE_MAXIMIZED },
2903
2904      /* in MDI child */
2905     { WM_WINDOWPOSCHANGING, sent|wparam|defwinproc, SWP_FRAMECHANGED|SWP_NOACTIVATE|SWP_NOSIZE|SWP_NOMOVE },
2906     { WM_NCCALCSIZE, sent|wparam|defwinproc, 1 },
2907     { WM_WINDOWPOSCHANGED, sent|wparam|defwinproc, SWP_FRAMECHANGED|SWP_NOACTIVATE|SWP_NOSIZE|SWP_NOMOVE|SWP_NOREDRAW|SWP_NOCLIENTSIZE|SWP_NOCLIENTMOVE },
2908     { EVENT_OBJECT_LOCATIONCHANGE, winevent_hook|wparam|lparam, 0, 0 }, /* MDI child */
2909
2910      /* in MDI frame */
2911     { WM_WINDOWPOSCHANGING, sent|wparam|defwinproc, SWP_FRAMECHANGED|SWP_NOACTIVATE|SWP_NOSIZE|SWP_NOMOVE },
2912     { WM_NCCALCSIZE, sent|wparam|defwinproc, 1 },
2913     { 0x0093, sent|defwinproc|optional },
2914     { 0x0093, sent|defwinproc|optional },
2915     { 0x0093, sent|defwinproc|optional },
2916     { WM_WINDOWPOSCHANGED, sent|wparam|defwinproc, SWP_FRAMECHANGED|SWP_NOACTIVATE|SWP_NOSIZE|SWP_NOMOVE },
2917     { WM_MOVE, sent|defwinproc },
2918     { WM_SIZE, sent|defwinproc|wparam, SIZE_RESTORED },
2919
2920      /* in MDI client */
2921     { WM_WINDOWPOSCHANGING, sent|wparam, SWP_NOACTIVATE },
2922     { WM_NCCALCSIZE, sent|wparam, 1 },
2923     { WM_WINDOWPOSCHANGED, sent|wparam, SWP_NOACTIVATE|SWP_NOMOVE|SWP_NOCLIENTMOVE },
2924     { WM_SIZE, sent|wparam, SIZE_RESTORED },
2925
2926      /* in MDI child */
2927     { WM_WINDOWPOSCHANGING, sent|wparam|defwinproc, SWP_NOACTIVATE },
2928     { WM_NCCALCSIZE, sent|wparam|defwinproc, 1 },
2929     { WM_WINDOWPOSCHANGED, sent|wparam|defwinproc, SWP_NOACTIVATE|SWP_NOMOVE|SWP_NOREDRAW|SWP_NOCLIENTMOVE },
2930     { WM_SIZE, sent|defwinproc|wparam, SIZE_MAXIMIZED },
2931     { EVENT_OBJECT_LOCATIONCHANGE, winevent_hook|wparam|lparam, 0, 0 }, /* MDI child */
2932     { EVENT_OBJECT_LOCATIONCHANGE, winevent_hook|wparam|lparam, 0, 0 }, /* MDI client */
2933
2934     { 0x0093, sent|defwinproc|optional },
2935     { WM_NCCALCSIZE, sent|wparam|defwinproc|optional, 1 }, /* XP sends it to MDI frame */
2936     { 0x0093, sent|defwinproc|optional },
2937     { 0x0093, sent|defwinproc|optional },
2938     { 0x0093, sent|defwinproc|optional },
2939     { 0x0093, sent|optional },
2940
2941     { EVENT_OBJECT_LOCATIONCHANGE, winevent_hook|wparam|lparam, 0, 0 }, /* MDI frame */
2942     { EVENT_OBJECT_LOCATIONCHANGE, winevent_hook|wparam|lparam, 0, 0 }, /* MDI child */
2943     { EVENT_OBJECT_LOCATIONCHANGE, winevent_hook|wparam|lparam, 0, 0 }, /* MDI client */
2944     { EVENT_OBJECT_LOCATIONCHANGE, winevent_hook|wparam|lparam, 0, 0 }, /* MDI frame */
2945     { EVENT_OBJECT_LOCATIONCHANGE, winevent_hook|wparam|lparam, 0, 0 }, /* XP sends a duplicate */
2946
2947      /* in MDI frame */
2948     { WM_WINDOWPOSCHANGING, sent|wparam|optional, SWP_FRAMECHANGED|SWP_NOACTIVATE|SWP_NOSIZE|SWP_NOMOVE },
2949     { WM_NCCALCSIZE, sent|wparam|optional, 1 },
2950     { 0x0093, sent|defwinproc|optional },
2951     { 0x0093, sent|defwinproc|optional },
2952     { 0x0093, sent|defwinproc|optional },
2953     { WM_WINDOWPOSCHANGED, sent|wparam|optional, SWP_FRAMECHANGED|SWP_NOACTIVATE|SWP_NOSIZE|SWP_NOMOVE|SWP_NOCLIENTSIZE|SWP_NOCLIENTMOVE },
2954     { EVENT_OBJECT_LOCATIONCHANGE, winevent_hook|wparam|lparam, 0, 0 }, /* MDI frame */
2955     { 0x0093, sent|optional },
2956
2957     { WM_NCACTIVATE, sent|wparam, 0 },
2958     { WM_MDIACTIVATE, sent },
2959
2960     { HCBT_MINMAX, hook|lparam, 0, SW_SHOWNORMAL },
2961     { WM_WINDOWPOSCHANGING, sent|wparam, SWP_FRAMECHANGED|SWP_SHOWWINDOW|SWP_STATECHANGED },
2962     { WM_NCCALCSIZE, sent|wparam, 1 },
2963
2964     { EVENT_OBJECT_SHOW, winevent_hook|wparam|lparam, 0, 0 },
2965
2966     { WM_CHILDACTIVATE, sent|wparam|lparam, 0, 0 },
2967     { WM_WINDOWPOSCHANGED, sent|wparam, SWP_FRAMECHANGED|SWP_SHOWWINDOW|SWP_NOMOVE|SWP_NOCLIENTMOVE|SWP_STATECHANGED },
2968     { WM_SIZE, sent|defwinproc|wparam, SIZE_RESTORED },
2969
2970      /* in MDI child */
2971     { WM_WINDOWPOSCHANGING, sent|wparam|defwinproc, SWP_FRAMECHANGED|SWP_NOACTIVATE|SWP_NOSIZE|SWP_NOMOVE },
2972     { WM_NCCALCSIZE, sent|wparam|defwinproc, 1 },
2973     { WM_WINDOWPOSCHANGED, sent|wparam|defwinproc, SWP_FRAMECHANGED|SWP_NOACTIVATE|SWP_NOSIZE|SWP_NOMOVE|SWP_NOCLIENTSIZE|SWP_NOCLIENTMOVE },
2974     { EVENT_OBJECT_LOCATIONCHANGE, winevent_hook|wparam|lparam, 0, 0 }, /* MDI child */
2975
2976      /* in MDI frame */
2977     { WM_WINDOWPOSCHANGING, sent|wparam, SWP_FRAMECHANGED|SWP_NOACTIVATE|SWP_NOSIZE|SWP_NOMOVE },
2978     { WM_NCCALCSIZE, sent|wparam, 1 },
2979     { WM_WINDOWPOSCHANGED, sent|wparam, SWP_FRAMECHANGED|SWP_NOACTIVATE|SWP_NOSIZE|SWP_NOMOVE },
2980     { WM_MOVE, sent|defwinproc },
2981     { WM_SIZE, sent|defwinproc|wparam, SIZE_RESTORED },
2982
2983      /* in MDI client */
2984     { WM_WINDOWPOSCHANGING, sent|wparam, SWP_NOACTIVATE },
2985     { WM_NCCALCSIZE, sent|wparam, 1 },
2986     { WM_WINDOWPOSCHANGED, sent|wparam, SWP_NOACTIVATE|SWP_NOMOVE|SWP_NOCLIENTMOVE },
2987     { WM_SIZE, sent|wparam, SIZE_RESTORED },
2988     { EVENT_OBJECT_LOCATIONCHANGE, winevent_hook|wparam|lparam, 0, 0 }, /* MDI child */
2989     { WM_NCCALCSIZE, sent|wparam|optional, 1 }, /* XP */
2990     { EVENT_OBJECT_LOCATIONCHANGE, winevent_hook|wparam|lparam, 0, 0 }, /* MDI client */
2991     { EVENT_OBJECT_LOCATIONCHANGE, winevent_hook|wparam|lparam, 0, 0 }, /* MDI frame */
2992     { EVENT_OBJECT_LOCATIONCHANGE, winevent_hook|wparam|lparam, 0, 0 }, /* XP sends a duplicate */
2993
2994     { HCBT_SETFOCUS, hook },
2995     { WM_KILLFOCUS, sent },
2996     { WM_IME_SETCONTEXT, sent|wparam|optional, 0 },
2997     { WM_IME_SETCONTEXT, sent|wparam|optional, 1 }, /* in MDI client */
2998     { EVENT_OBJECT_FOCUS, winevent_hook|wparam|lparam, OBJID_CLIENT, 0 },
2999     { WM_SETFOCUS, sent }, /* in MDI client */
3000
3001     { WM_MDIREFRESHMENU, sent }, /* in MDI client */
3002
3003     { HCBT_DESTROYWND, hook },
3004     /* Win2k sends wparam set to
3005      * MAKEWPARAM(WM_DESTROY, MDI_FIRST_CHILD_ID + nTotalCreated),
3006      * while Win9x doesn't bother to set child window id according to
3007      * CLIENTCREATESTRUCT.idFirstChild
3008      */
3009     { 0x0090, sent|optional },
3010     { WM_PARENTNOTIFY, sent /*|wparam, WM_DESTROY*/ }, /* in MDI client */
3011
3012     { WM_SHOWWINDOW, sent|wparam, 0 },
3013     { WM_WINDOWPOSCHANGING, sent|wparam|optional, SWP_HIDEWINDOW|SWP_NOACTIVATE|SWP_NOSIZE|SWP_NOMOVE },
3014     { EVENT_OBJECT_HIDE, winevent_hook|wparam|lparam, 0, 0 },
3015     { WM_ERASEBKGND, sent|parent|optional },
3016     { WM_WINDOWPOSCHANGED, sent|wparam|optional, SWP_HIDEWINDOW|SWP_NOACTIVATE|SWP_NOSIZE|SWP_NOMOVE|SWP_NOCLIENTSIZE|SWP_NOCLIENTMOVE },
3017
3018     { EVENT_OBJECT_DESTROY, winevent_hook|wparam|lparam, 0, 0 },
3019     { WM_DESTROY, sent },
3020     { WM_NCDESTROY, sent },
3021     { 0 }
3022 };
3023 /* ShowWindow(SW_MAXIMIZE) for a not visible MDI child window */
3024 static const struct message WmMaximizeMDIchildInvisibleSeq[] = {
3025     { HCBT_MINMAX, hook|lparam, 0, SW_MAXIMIZE },
3026     { WM_GETMINMAXINFO, sent },
3027     { WM_WINDOWPOSCHANGING, sent|wparam, SWP_SHOWWINDOW|SWP_FRAMECHANGED|SWP_STATECHANGED },
3028     { WM_NCCALCSIZE, sent|wparam, 1 },
3029     { EVENT_OBJECT_SHOW, winevent_hook|wparam|lparam, 0, 0 },
3030     { WM_CHILDACTIVATE, sent|wparam|lparam, 0, 0 },
3031
3032     { WM_WINDOWPOSCHANGING, sent|wparam|optional|defwinproc, SWP_NOACTIVATE|SWP_NOSIZE|SWP_NOMOVE },
3033     { WM_NCACTIVATE, sent|wparam|optional|defwinproc, 1 },
3034     { HCBT_SETFOCUS, hook|optional },
3035     { WM_IME_SETCONTEXT, sent|wparam|optional, 1 }, /* in MDI client */
3036     { EVENT_OBJECT_FOCUS, winevent_hook|wparam|lparam, OBJID_CLIENT, 0 },
3037     { WM_SETFOCUS, sent|optional }, /* in MDI client */
3038     { HCBT_SETFOCUS, hook|optional },
3039     { WM_KILLFOCUS, sent|optional }, /* in MDI client */
3040     { WM_IME_SETCONTEXT, sent|wparam|optional, 0 }, /* in MDI client */
3041     { WM_IME_SETCONTEXT, sent|wparam|defwinproc|optional, 1 },
3042     { EVENT_OBJECT_FOCUS, winevent_hook|wparam|lparam, OBJID_CLIENT, 0 },
3043     { WM_SETFOCUS, sent|optional|defwinproc },
3044     { WM_MDIACTIVATE, sent|optional|defwinproc },
3045     { WM_WINDOWPOSCHANGED, sent|wparam, SWP_SHOWWINDOW|SWP_FRAMECHANGED|SWP_NOMOVE|SWP_NOCLIENTMOVE|SWP_STATECHANGED },
3046     { WM_SIZE, sent|defwinproc|wparam, SIZE_MAXIMIZED },
3047      /* in MDI frame */
3048     { WM_WINDOWPOSCHANGING, sent|wparam, SWP_FRAMECHANGED|SWP_NOACTIVATE|SWP_NOSIZE|SWP_NOMOVE },
3049     { WM_NCCALCSIZE, sent|wparam, 1 },
3050     { WM_WINDOWPOSCHANGED, sent|wparam, SWP_FRAMECHANGED|SWP_NOACTIVATE|SWP_NOSIZE|SWP_NOMOVE|SWP_NOCLIENTSIZE|SWP_NOCLIENTMOVE },
3051     { EVENT_OBJECT_LOCATIONCHANGE, winevent_hook|wparam|lparam, 0, 0 }, /* MDI frame */
3052     { EVENT_OBJECT_LOCATIONCHANGE, winevent_hook|wparam|lparam, 0, 0 }, /* MDI child */
3053     { 0 }
3054 };
3055 /* ShowWindow(SW_MAXIMIZE) for a not visible maximized MDI child window */
3056 static const struct message WmMaximizeMDIchildInvisibleSeq2[] = {
3057     { HCBT_MINMAX, hook|lparam, 0, SW_MAXIMIZE },
3058     { WM_GETMINMAXINFO, sent },
3059     { WM_WINDOWPOSCHANGING, sent|wparam, SWP_SHOWWINDOW|SWP_FRAMECHANGED },
3060     { WM_GETMINMAXINFO, sent|defwinproc },
3061     { WM_NCCALCSIZE, sent|wparam, 1 },
3062     { EVENT_OBJECT_SHOW, winevent_hook|wparam|lparam, 0, 0 },
3063     { WM_CHILDACTIVATE, sent|wparam|lparam, 0, 0 },
3064
3065     { WM_WINDOWPOSCHANGING, sent|wparam|defwinproc|optional, SWP_NOACTIVATE|SWP_NOSIZE|SWP_NOMOVE },
3066     { WM_NCACTIVATE, sent|wparam|defwinproc|optional, 1 },
3067     { HCBT_SETFOCUS, hook|optional },
3068     { WM_IME_SETCONTEXT, sent|wparam|optional, 1 }, /* in MDI client */
3069     { EVENT_OBJECT_FOCUS, winevent_hook|wparam|lparam, OBJID_CLIENT, 0 },
3070     { WM_SETFOCUS, sent|optional }, /* in MDI client */
3071     { HCBT_SETFOCUS, hook|optional },
3072     { WM_KILLFOCUS, sent|optional }, /* in MDI client */
3073     { WM_IME_SETCONTEXT, sent|wparam|optional, 0 }, /* in MDI client */
3074     { WM_IME_SETCONTEXT, sent|wparam|defwinproc|optional, 1 },
3075     { EVENT_OBJECT_FOCUS, winevent_hook|wparam|lparam, OBJID_CLIENT, 0 },
3076     { WM_SETFOCUS, sent|defwinproc|optional },
3077     { WM_MDIACTIVATE, sent|defwinproc|optional },
3078     { WM_WINDOWPOSCHANGED, sent|wparam, SWP_SHOWWINDOW|SWP_FRAMECHANGED|SWP_NOSIZE|SWP_NOMOVE|SWP_NOCLIENTSIZE|SWP_NOCLIENTMOVE },
3079     { EVENT_OBJECT_LOCATIONCHANGE, winevent_hook|wparam|lparam, 0, 0 }, /* MDI child */
3080     { 0 }
3081 };
3082 /* WM_MDIMAXIMIZE for an MDI child window with invisible parent */
3083 static const struct message WmMaximizeMDIchildInvisibleParentSeq[] = {
3084     { WM_MDIMAXIMIZE, sent }, /* in MDI client */
3085     { HCBT_MINMAX, hook|lparam, 0, SW_MAXIMIZE },
3086     { WM_GETMINMAXINFO, sent },
3087     { WM_WINDOWPOSCHANGING, sent|wparam, SWP_FRAMECHANGED|SWP_STATECHANGED },
3088     { WM_GETMINMAXINFO, sent|defwinproc },
3089     { WM_NCCALCSIZE, sent|wparam, 1 },
3090     { EVENT_OBJECT_SHOW, winevent_hook|wparam|lparam|optional, 0, 0 }, /* XP doesn't send it */
3091     { WM_CHILDACTIVATE, sent|wparam|lparam, 0, 0 },
3092     { WM_WINDOWPOSCHANGED, sent|wparam, SWP_FRAMECHANGED|SWP_NOREDRAW|SWP_STATECHANGED },
3093     { WM_MOVE, sent|defwinproc },
3094     { WM_SIZE, sent|defwinproc|wparam, SIZE_MAXIMIZED },
3095
3096     { WM_WINDOWPOSCHANGING, sent|wparam|defwinproc, SWP_FRAMECHANGED|SWP_NOACTIVATE|SWP_NOSIZE|SWP_NOMOVE },
3097     { WM_NCCALCSIZE, sent|wparam|defwinproc, 1 },
3098     { WM_WINDOWPOSCHANGED, sent|wparam|defwinproc, SWP_FRAMECHANGED|SWP_NOACTIVATE|SWP_NOSIZE|SWP_NOMOVE|SWP_NOREDRAW|SWP_NOCLIENTSIZE|SWP_NOCLIENTMOVE },
3099     { WM_NCCALCSIZE, sent|wparam|defwinproc|optional, 1 },
3100     { EVENT_OBJECT_LOCATIONCHANGE, winevent_hook|wparam|lparam|optional, 0, 0 }, /* MDI child XP */
3101     { EVENT_OBJECT_LOCATIONCHANGE, winevent_hook|wparam|lparam|optional, 0, 0 }, /* MDI client XP */
3102      /* in MDI frame */
3103     { WM_WINDOWPOSCHANGING, sent|wparam, SWP_FRAMECHANGED|SWP_NOACTIVATE|SWP_NOSIZE|SWP_NOMOVE },
3104     { WM_NCCALCSIZE, sent|wparam, 1 },
3105     { 0x0093, sent|defwinproc|optional },
3106     { 0x0094, sent|defwinproc|optional },
3107     { 0x0094, sent|defwinproc|optional },
3108     { 0x0094, sent|defwinproc|optional },
3109     { 0x0094, sent|defwinproc|optional },
3110     { 0x0093, sent|defwinproc|optional },
3111     { 0x0093, sent|defwinproc|optional },
3112     { 0x0091, sent|defwinproc|optional },
3113     { 0x0092, sent|defwinproc|optional },
3114     { 0x0092, sent|defwinproc|optional },
3115     { 0x0092, sent|defwinproc|optional },
3116     { 0x0092, sent|defwinproc|optional },
3117     { WM_WINDOWPOSCHANGED, sent|wparam, SWP_FRAMECHANGED|SWP_NOACTIVATE|SWP_NOSIZE|SWP_NOMOVE },
3118     { WM_MOVE, sent|defwinproc },
3119     { WM_SIZE, sent|defwinproc|wparam, SIZE_RESTORED },
3120     { EVENT_OBJECT_LOCATIONCHANGE, winevent_hook|wparam|lparam|optional, 0, 0 }, /* MDI frame win2000 */
3121      /* in MDI client */
3122     { WM_WINDOWPOSCHANGING, sent|wparam, SWP_NOACTIVATE },
3123     { WM_NCCALCSIZE, sent|wparam, 1 },
3124     { WM_WINDOWPOSCHANGED, sent|wparam, SWP_NOACTIVATE|SWP_NOMOVE|SWP_NOREDRAW|SWP_NOCLIENTMOVE },
3125     { WM_SIZE, sent|wparam, SIZE_RESTORED },
3126      /* in MDI child */
3127     { WM_WINDOWPOSCHANGING, sent|wparam|defwinproc, SWP_NOACTIVATE },
3128     { WM_GETMINMAXINFO, sent|defwinproc },
3129     { WM_NCCALCSIZE, sent|wparam|defwinproc, 1 },
3130     { WM_WINDOWPOSCHANGED, sent|wparam|defwinproc, SWP_NOACTIVATE|SWP_NOMOVE|SWP_NOREDRAW|SWP_NOCLIENTMOVE },
3131     { WM_SIZE, sent|defwinproc|wparam, SIZE_MAXIMIZED },
3132     { EVENT_OBJECT_LOCATIONCHANGE, winevent_hook|wparam|lparam|optional, 0, 0 }, /* MDI child win2000 */
3133     { WM_NCCALCSIZE, sent|wparam|defwinproc|optional, 1 },
3134     { EVENT_OBJECT_LOCATIONCHANGE, winevent_hook|wparam|lparam|optional, 0, 0 }, /* MDI child XP */
3135     { EVENT_OBJECT_LOCATIONCHANGE, winevent_hook|wparam|lparam|optional, 0, 0 }, /* MDI child XP */
3136     { EVENT_OBJECT_LOCATIONCHANGE, winevent_hook|wparam|lparam|optional, 0, 0 }, /* MDI client XP */
3137      /* in MDI frame */
3138     { 0x0093, sent|optional },
3139     { WM_NCCALCSIZE, sent|wparam|optional, 1 },
3140     { 0x0093, sent|defwinproc|optional },
3141     { 0x0093, sent|defwinproc|optional },
3142     { 0x0093, sent|defwinproc|optional },
3143     { 0x0091, sent|defwinproc|optional },
3144     { 0x0092, sent|defwinproc|optional },
3145     { 0x0092, sent|defwinproc|optional },
3146     { 0x0092, sent|defwinproc|optional },
3147     { 0x0092, sent|defwinproc|optional },
3148     { EVENT_OBJECT_LOCATIONCHANGE, winevent_hook|wparam|lparam|optional, 0, 0 }, /* MDI frame XP */
3149     { EVENT_OBJECT_LOCATIONCHANGE, winevent_hook|wparam|lparam|optional, 0, 0 }, /* MDI frame XP */
3150     { EVENT_OBJECT_LOCATIONCHANGE, winevent_hook|wparam|lparam|optional, 0, 0 }, /* MDI child XP */
3151     { 0 }
3152 };
3153 /* ShowWindow(SW_MAXIMIZE) for a visible MDI child window */
3154 static const struct message WmMaximizeMDIchildVisibleSeq[] = {
3155     { HCBT_MINMAX, hook|lparam, 0, SW_MAXIMIZE },
3156     { WM_GETMINMAXINFO, sent },
3157     { WM_WINDOWPOSCHANGING, sent|wparam, SWP_FRAMECHANGED|SWP_STATECHANGED },
3158     { WM_NCCALCSIZE, sent|wparam, 1 },
3159     { WM_CHILDACTIVATE, sent|wparam|lparam, 0, 0 },
3160     { WM_WINDOWPOSCHANGED, sent|wparam, SWP_FRAMECHANGED|SWP_NOMOVE|SWP_NOCLIENTMOVE|SWP_STATECHANGED },
3161     { WM_SIZE, sent|defwinproc|wparam, SIZE_MAXIMIZED },
3162      /* in MDI frame */
3163     { WM_WINDOWPOSCHANGING, sent|wparam, SWP_FRAMECHANGED|SWP_NOACTIVATE|SWP_NOSIZE|SWP_NOMOVE },
3164     { WM_NCCALCSIZE, sent|wparam, 1 },
3165     { WM_WINDOWPOSCHANGED, sent|wparam, SWP_FRAMECHANGED|SWP_NOACTIVATE|SWP_NOSIZE|SWP_NOMOVE|SWP_NOCLIENTSIZE|SWP_NOCLIENTMOVE },
3166     { EVENT_OBJECT_LOCATIONCHANGE, winevent_hook|wparam|lparam, 0, 0 }, /* MDI frame */
3167     { EVENT_OBJECT_LOCATIONCHANGE, winevent_hook|wparam|lparam, 0, 0 }, /* MDI child */
3168     { 0 }
3169 };
3170 /* ShowWindow(SW_RESTORE) for a visible maximized MDI child window */
3171 static const struct message WmRestoreMDIchildVisibleSeq[] = {
3172     { HCBT_MINMAX, hook|lparam, 0, SW_RESTORE },
3173     { WM_WINDOWPOSCHANGING, sent|wparam, SWP_FRAMECHANGED|SWP_STATECHANGED },
3174     { WM_NCCALCSIZE, sent|wparam, 1 },
3175     { WM_CHILDACTIVATE, sent|wparam|lparam, 0, 0 },
3176     { WM_WINDOWPOSCHANGED, sent|wparam, SWP_FRAMECHANGED|SWP_NOMOVE|SWP_NOCLIENTMOVE|SWP_STATECHANGED },
3177     { WM_SIZE, sent|defwinproc|wparam, SIZE_RESTORED },
3178      /* in MDI frame */
3179     { WM_WINDOWPOSCHANGING, sent|wparam, SWP_FRAMECHANGED|SWP_NOACTIVATE|SWP_NOSIZE|SWP_NOMOVE },
3180     { WM_NCCALCSIZE, sent|wparam, 1 },
3181     { WM_WINDOWPOSCHANGED, sent|wparam, SWP_FRAMECHANGED|SWP_NOACTIVATE|SWP_NOSIZE|SWP_NOMOVE|SWP_NOCLIENTSIZE|SWP_NOCLIENTMOVE },
3182     { EVENT_OBJECT_LOCATIONCHANGE, winevent_hook|wparam|lparam, 0, 0 }, /* MDI frame */
3183     { EVENT_OBJECT_LOCATIONCHANGE, winevent_hook|wparam|lparam, 0, 0 }, /* MDI child */
3184     { 0 }
3185 };
3186 /* ShowWindow(SW_RESTORE) for a visible minimized MDI child window */
3187 static const struct message WmRestoreMDIchildVisibleSeq_2[] = {
3188     { HCBT_MINMAX, hook|lparam, 0, SW_RESTORE },
3189     { WM_QUERYOPEN, sent|wparam|lparam, 0, 0 },
3190     { WM_WINDOWPOSCHANGING, sent|wparam, SWP_FRAMECHANGED|SWP_NOCOPYBITS|SWP_STATECHANGED },
3191     { WM_NCCALCSIZE, sent|wparam, 1 },
3192     { WM_CHILDACTIVATE, sent|wparam|lparam, 0, 0 },
3193     { WM_WINDOWPOSCHANGED, sent|wparam, SWP_FRAMECHANGED|SWP_NOCOPYBITS|SWP_STATECHANGED },
3194     { WM_MOVE, sent|defwinproc },
3195     { WM_SIZE, sent|defwinproc|wparam, SIZE_RESTORED },
3196     { EVENT_OBJECT_LOCATIONCHANGE, winevent_hook|wparam|lparam, 0, 0 }, /* MDI child */
3197     { EVENT_SYSTEM_MINIMIZEEND, winevent_hook|wparam|lparam, 0, 0 }, /* MDI child */
3198     { HCBT_SETFOCUS, hook },
3199     { WM_IME_SETCONTEXT, sent|wparam|optional, 1 },
3200     { WM_IME_NOTIFY, sent|wparam|defwinproc|optional, 2 },
3201     { EVENT_OBJECT_FOCUS, winevent_hook|wparam|lparam, OBJID_CLIENT, 0 },
3202     { WM_SETFOCUS, sent },
3203     { 0 }
3204 };
3205 /* ShowWindow(SW_MINIMIZE) for a visible restored MDI child window */
3206 static const struct message WmMinimizeMDIchildVisibleSeq[] = {
3207     { HCBT_MINMAX, hook|lparam, 0, SW_MINIMIZE },
3208     { WM_WINDOWPOSCHANGING, sent|wparam, SWP_SHOWWINDOW|SWP_FRAMECHANGED|SWP_NOACTIVATE|SWP_NOCOPYBITS|SWP_STATECHANGED },
3209     { WM_NCCALCSIZE, sent|wparam, 1 },
3210     { WM_WINDOWPOSCHANGED, sent|wparam, SWP_FRAMECHANGED|SWP_NOACTIVATE|SWP_NOCOPYBITS|SWP_STATECHANGED },
3211     { WM_MOVE, sent|defwinproc },
3212     { WM_SIZE, sent|defwinproc|wparam|lparam, SIZE_MINIMIZED, 0 },
3213     { WM_CHILDACTIVATE, sent|wparam|lparam|defwinproc, 0, 0 },
3214     { EVENT_OBJECT_LOCATIONCHANGE, winevent_hook|wparam|lparam, 0, 0 }, /* MDI child */
3215     { EVENT_SYSTEM_MINIMIZESTART, winevent_hook|wparam|lparam, 0, 0 }, /* MDI child */
3216     /* FIXME: Wine creates an icon/title window while Windows doesn't */
3217     { WM_PARENTNOTIFY, sent|parent|wparam|optional, WM_CREATE }, /* MDI client */
3218     { 0 }
3219 };
3220 /* ShowWindow(SW_RESTORE) for a not visible MDI child window */
3221 static const struct message WmRestoreMDIchildInisibleSeq[] = {
3222     { HCBT_MINMAX, hook|lparam, 0, SW_RESTORE },
3223     { WM_WINDOWPOSCHANGING, sent|wparam, SWP_SHOWWINDOW|SWP_FRAMECHANGED|SWP_STATECHANGED  },
3224     { WM_NCCALCSIZE, sent|wparam, 1 },
3225     { EVENT_OBJECT_SHOW, winevent_hook|wparam|lparam, 0, 0 },
3226     { WM_CHILDACTIVATE, sent|wparam|lparam, 0, 0 },
3227     { WM_WINDOWPOSCHANGED, sent|wparam, SWP_SHOWWINDOW|SWP_FRAMECHANGED|SWP_NOMOVE|SWP_NOCLIENTMOVE|SWP_STATECHANGED },
3228     { WM_SIZE, sent|defwinproc|wparam, SIZE_RESTORED },
3229      /* in MDI frame */
3230     { WM_WINDOWPOSCHANGING, sent|wparam, SWP_FRAMECHANGED|SWP_NOACTIVATE|SWP_NOSIZE|SWP_NOMOVE },
3231     { WM_NCCALCSIZE, sent|wparam, 1 },
3232     { WM_WINDOWPOSCHANGED, sent|wparam, SWP_FRAMECHANGED|SWP_NOACTIVATE|SWP_NOSIZE|SWP_NOMOVE|SWP_NOCLIENTSIZE|SWP_NOCLIENTMOVE },
3233     { EVENT_OBJECT_LOCATIONCHANGE, winevent_hook|wparam|lparam, 0, 0 }, /* MDI frame */
3234     { EVENT_OBJECT_LOCATIONCHANGE, winevent_hook|wparam|lparam, 0, 0 }, /* MDI child */
3235     { 0 }
3236 };
3237
3238 static HWND mdi_client;
3239 static WNDPROC old_mdi_client_proc;
3240
3241 static LRESULT WINAPI mdi_client_hook_proc(HWND hwnd, UINT message, WPARAM wParam, LPARAM lParam)
3242 {
3243     struct recvd_message msg;
3244
3245     /* do not log painting messages */
3246     if (message != WM_PAINT &&
3247         message != WM_NCPAINT &&
3248         message != WM_SYNCPAINT &&
3249         message != WM_ERASEBKGND &&
3250         message != WM_NCHITTEST &&
3251         message != WM_GETTEXT &&
3252         message != WM_MDIGETACTIVE &&
3253         !ignore_message( message ))
3254     {
3255         msg.hwnd = hwnd;
3256         msg.message = message;
3257         msg.flags = sent|wparam|lparam;
3258         msg.wParam = wParam;
3259         msg.lParam = lParam;
3260         msg.descr = "mdi client";
3261         add_message(&msg);
3262     }
3263
3264     return CallWindowProcA(old_mdi_client_proc, hwnd, message, wParam, lParam);
3265 }
3266
3267 static LRESULT WINAPI mdi_child_wnd_proc(HWND hwnd, UINT message, WPARAM wParam, LPARAM lParam)
3268 {
3269     static LONG defwndproc_counter = 0;
3270     LRESULT ret;
3271     struct recvd_message msg;
3272
3273     /* do not log painting messages */
3274     if (message != WM_PAINT &&
3275         message != WM_NCPAINT &&
3276         message != WM_SYNCPAINT &&
3277         message != WM_ERASEBKGND &&
3278         message != WM_NCHITTEST &&
3279         message != WM_GETTEXT &&
3280         !ignore_message( message ))
3281     {
3282         switch (message)
3283         {
3284             case WM_MDIACTIVATE:
3285             {
3286                 HWND active, client = GetParent(hwnd);
3287
3288                 active = (HWND)SendMessageA(client, WM_MDIGETACTIVE, 0, 0);
3289
3290                 if (hwnd == (HWND)lParam) /* if we are being activated */
3291                     ok (active == (HWND)lParam, "new active %p != active %p\n", (HWND)lParam, active);
3292                 else
3293                     ok (active == (HWND)wParam, "old active %p != active %p\n", (HWND)wParam, active);
3294                 break;
3295             }
3296         }
3297
3298         msg.hwnd = hwnd;
3299         msg.message = message;
3300         msg.flags = sent|wparam|lparam;
3301         if (defwndproc_counter) msg.flags |= defwinproc;
3302         msg.wParam = wParam;
3303         msg.lParam = lParam;
3304         msg.descr = "mdi child";
3305         add_message(&msg);
3306     }
3307
3308     defwndproc_counter++;
3309     ret = DefMDIChildProcA(hwnd, message, wParam, lParam);
3310     defwndproc_counter--;
3311
3312     return ret;
3313 }
3314
3315 static LRESULT WINAPI mdi_frame_wnd_proc(HWND hwnd, UINT message, WPARAM wParam, LPARAM lParam)
3316 {
3317     static LONG defwndproc_counter = 0;
3318     LRESULT ret;
3319     struct recvd_message msg;
3320
3321     /* do not log painting messages */
3322     if (message != WM_PAINT &&
3323         message != WM_NCPAINT &&
3324         message != WM_SYNCPAINT &&
3325         message != WM_ERASEBKGND &&
3326         message != WM_NCHITTEST &&
3327         message != WM_GETTEXT &&
3328         !ignore_message( message ))
3329     {
3330         msg.hwnd = hwnd;
3331         msg.message = message;
3332         msg.flags = sent|wparam|lparam;
3333         if (defwndproc_counter) msg.flags |= defwinproc;
3334         msg.wParam = wParam;
3335         msg.lParam = lParam;
3336         msg.descr = "mdi frame";
3337         add_message(&msg);
3338     }
3339
3340     defwndproc_counter++;
3341     ret = DefFrameProcA(hwnd, mdi_client, message, wParam, lParam);
3342     defwndproc_counter--;
3343
3344     return ret;
3345 }
3346
3347 static BOOL mdi_RegisterWindowClasses(void)
3348 {
3349     WNDCLASSA cls;
3350
3351     cls.style = 0;
3352     cls.lpfnWndProc = mdi_frame_wnd_proc;
3353     cls.cbClsExtra = 0;
3354     cls.cbWndExtra = 0;
3355     cls.hInstance = GetModuleHandleA(0);
3356     cls.hIcon = 0;
3357     cls.hCursor = LoadCursorA(0, IDC_ARROW);
3358     cls.hbrBackground = GetStockObject(WHITE_BRUSH);
3359     cls.lpszMenuName = NULL;
3360     cls.lpszClassName = "MDI_frame_class";
3361     if (!RegisterClassA(&cls)) return FALSE;
3362
3363     cls.lpfnWndProc = mdi_child_wnd_proc;
3364     cls.lpszClassName = "MDI_child_class";
3365     if (!RegisterClassA(&cls)) return FALSE;
3366
3367     if (!GetClassInfoA(0, "MDIClient", &cls)) assert(0);
3368     old_mdi_client_proc = cls.lpfnWndProc;
3369     cls.hInstance = GetModuleHandleA(0);
3370     cls.lpfnWndProc = mdi_client_hook_proc;
3371     cls.lpszClassName = "MDI_client_class";
3372     if (!RegisterClassA(&cls)) assert(0);
3373
3374     return TRUE;
3375 }
3376
3377 static void test_mdi_messages(void)
3378 {
3379     MDICREATESTRUCTA mdi_cs;
3380     CLIENTCREATESTRUCT client_cs;
3381     HWND mdi_frame, mdi_child, mdi_child2, active_child;
3382     BOOL zoomed;
3383     RECT rc;
3384     HMENU hMenu = CreateMenu();
3385
3386     if (!mdi_RegisterWindowClasses()) assert(0);
3387
3388     flush_sequence();
3389
3390     trace("creating MDI frame window\n");
3391     mdi_frame = CreateWindowExA(0, "MDI_frame_class", "MDI frame window",
3392                                 WS_CAPTION | WS_SYSMENU | WS_MINIMIZEBOX |
3393                                 WS_MAXIMIZEBOX | WS_VISIBLE,
3394                                 100, 100, CW_USEDEFAULT, CW_USEDEFAULT,
3395                                 GetDesktopWindow(), hMenu,
3396                                 GetModuleHandleA(0), NULL);
3397     assert(mdi_frame);
3398     ok_sequence(WmCreateMDIframeSeq, "Create MDI frame window", FALSE);
3399
3400     ok(GetActiveWindow() == mdi_frame, "wrong active window %p\n", GetActiveWindow());
3401     ok(GetFocus() == mdi_frame, "wrong focus window %p\n", GetFocus());
3402
3403     trace("creating MDI client window\n");
3404     GetClientRect(mdi_frame, &rc);
3405     client_cs.hWindowMenu = 0;
3406     client_cs.idFirstChild = MDI_FIRST_CHILD_ID;
3407     mdi_client = CreateWindowExA(0, "MDI_client_class",
3408                                  NULL,
3409                                  WS_CHILD | WS_VISIBLE | MDIS_ALLCHILDSTYLES,
3410                                  rc.left, rc.top, rc.right - rc.left, rc.bottom - rc.top,
3411                                  mdi_frame, 0, GetModuleHandleA(0), &client_cs);
3412     assert(mdi_client);
3413     ok_sequence(WmCreateMDIclientSeq, "Create visible MDI client window", FALSE);
3414
3415     ok(GetActiveWindow() == mdi_frame, "wrong active window %p\n", GetActiveWindow());
3416     ok(GetFocus() == mdi_frame, "input focus should be on MDI frame not on %p\n", GetFocus());
3417
3418     active_child = (HWND)SendMessageA(mdi_client, WM_MDIGETACTIVE, 0, (LPARAM)&zoomed);
3419     ok(!active_child, "wrong active MDI child %p\n", active_child);
3420     ok(!zoomed, "wrong zoomed state %d\n", zoomed);
3421
3422     SetFocus(0);
3423     flush_sequence();
3424
3425     trace("creating invisible MDI child window\n");
3426     mdi_child = CreateWindowExA(WS_EX_MDICHILD, "MDI_child_class", "MDI child",
3427                                 WS_CHILD,
3428                                 0, 0, CW_USEDEFAULT, CW_USEDEFAULT,
3429                                 mdi_client, 0, GetModuleHandleA(0), NULL);
3430     assert(mdi_child);
3431
3432     flush_sequence();
3433     ShowWindow(mdi_child, SW_SHOWNORMAL);
3434     ok_sequence(WmShowChildSeq, "ShowWindow(SW_SHOWNORMAL) MDI child window", FALSE);
3435
3436     ok(GetWindowLongA(mdi_child, GWL_STYLE) & WS_VISIBLE, "MDI child should be visible\n");
3437     ok(IsWindowVisible(mdi_child), "MDI child should be visible\n");
3438
3439     ok(GetActiveWindow() == mdi_frame, "wrong active window %p\n", GetActiveWindow());
3440     ok(GetFocus() == 0, "wrong focus window %p\n", GetFocus());
3441
3442     active_child = (HWND)SendMessageA(mdi_client, WM_MDIGETACTIVE, 0, (LPARAM)&zoomed);
3443     ok(!active_child, "wrong active MDI child %p\n", active_child);
3444     ok(!zoomed, "wrong zoomed state %d\n", zoomed);
3445
3446     ShowWindow(mdi_child, SW_HIDE);
3447     ok_sequence(WmHideChildSeq, "ShowWindow(SW_HIDE) MDI child window", FALSE);
3448     flush_sequence();
3449
3450     ShowWindow(mdi_child, SW_SHOW);
3451     ok_sequence(WmShowChildSeq, "ShowWindow(SW_SHOW) MDI child window", FALSE);
3452
3453     ok(GetWindowLongA(mdi_child, GWL_STYLE) & WS_VISIBLE, "MDI child should be visible\n");
3454     ok(IsWindowVisible(mdi_child), "MDI child should be visible\n");
3455
3456     ok(GetActiveWindow() == mdi_frame, "wrong active window %p\n", GetActiveWindow());
3457     ok(GetFocus() == 0, "wrong focus window %p\n", GetFocus());
3458
3459     active_child = (HWND)SendMessageA(mdi_client, WM_MDIGETACTIVE, 0, (LPARAM)&zoomed);
3460     ok(!active_child, "wrong active MDI child %p\n", active_child);
3461     ok(!zoomed, "wrong zoomed state %d\n", zoomed);
3462
3463     DestroyWindow(mdi_child);
3464     flush_sequence();
3465
3466     trace("creating visible MDI child window\n");
3467     mdi_child = CreateWindowExA(WS_EX_MDICHILD, "MDI_child_class", "MDI child",
3468                                 WS_CHILD | WS_VISIBLE,
3469                                 0, 0, CW_USEDEFAULT, CW_USEDEFAULT,
3470                                 mdi_client, 0, GetModuleHandleA(0), NULL);
3471     assert(mdi_child);
3472     ok_sequence(WmCreateMDIchildVisibleSeq, "Create visible MDI child window", FALSE);
3473
3474     ok(GetWindowLongA(mdi_child, GWL_STYLE) & WS_VISIBLE, "MDI child should be visible\n");
3475     ok(IsWindowVisible(mdi_child), "MDI child should be visible\n");
3476
3477     ok(GetActiveWindow() == mdi_frame, "wrong active window %p\n", GetActiveWindow());
3478     ok(GetFocus() == mdi_child, "wrong focus window %p\n", GetFocus());
3479
3480     active_child = (HWND)SendMessageA(mdi_client, WM_MDIGETACTIVE, 0, (LPARAM)&zoomed);
3481     ok(active_child == mdi_child, "wrong active MDI child %p\n", active_child);
3482     ok(!zoomed, "wrong zoomed state %d\n", zoomed);
3483     flush_sequence();
3484
3485     DestroyWindow(mdi_child);
3486     ok_sequence(WmDestroyMDIchildVisibleSeq, "Destroy visible MDI child window", TRUE);
3487
3488     ok(GetActiveWindow() == mdi_frame, "wrong active window %p\n", GetActiveWindow());
3489     ok(GetFocus() == 0, "wrong focus window %p\n", GetFocus());
3490
3491     /* Win2k: MDI client still returns a just destroyed child as active
3492      * Win9x: MDI client returns 0
3493      */
3494     active_child = (HWND)SendMessageA(mdi_client, WM_MDIGETACTIVE, 0, (LPARAM)&zoomed);
3495     ok(active_child == mdi_child || /* win2k */
3496        !active_child, /* win9x */
3497        "wrong active MDI child %p\n", active_child);
3498     ok(!zoomed, "wrong zoomed state %d\n", zoomed);
3499
3500     flush_sequence();
3501
3502     trace("creating invisible MDI child window\n");
3503     mdi_child2 = CreateWindowExA(WS_EX_MDICHILD, "MDI_child_class", "MDI child",
3504                                 WS_CHILD,
3505                                 0, 0, CW_USEDEFAULT, CW_USEDEFAULT,
3506                                 mdi_client, 0, GetModuleHandleA(0), NULL);
3507     assert(mdi_child2);
3508     ok_sequence(WmCreateMDIchildInvisibleSeq, "Create invisible MDI child window", FALSE);
3509
3510     ok(!(GetWindowLongA(mdi_child2, GWL_STYLE) & WS_VISIBLE), "MDI child should not be visible\n");
3511     ok(!IsWindowVisible(mdi_child2), "MDI child should not be visible\n");
3512
3513     ok(GetActiveWindow() == mdi_frame, "wrong active window %p\n", GetActiveWindow());
3514     ok(GetFocus() == 0, "wrong focus window %p\n", GetFocus());
3515
3516     /* Win2k: MDI client still returns a just destroyed child as active
3517      * Win9x: MDI client returns mdi_child2
3518      */
3519     active_child = (HWND)SendMessageA(mdi_client, WM_MDIGETACTIVE, 0, (LPARAM)&zoomed);
3520     ok(active_child == mdi_child || /* win2k */
3521        active_child == mdi_child2, /* win9x */
3522        "wrong active MDI child %p\n", active_child);
3523     ok(!zoomed, "wrong zoomed state %d\n", zoomed);
3524     flush_sequence();
3525
3526     ShowWindow(mdi_child2, SW_MAXIMIZE);
3527     ok_sequence(WmMaximizeMDIchildInvisibleSeq, "ShowWindow(SW_MAXIMIZE):invisible MDI child", FALSE);
3528
3529     ok(GetWindowLongA(mdi_child2, GWL_STYLE) & WS_VISIBLE, "MDI child should be visible\n");
3530     ok(IsWindowVisible(mdi_child2), "MDI child should be visible\n");
3531
3532     active_child = (HWND)SendMessageA(mdi_client, WM_MDIGETACTIVE, 0, (LPARAM)&zoomed);
3533     ok(active_child == mdi_child2, "wrong active MDI child %p\n", active_child);
3534     ok(zoomed, "wrong zoomed state %d\n", zoomed);
3535     flush_sequence();
3536
3537     ok(GetActiveWindow() == mdi_frame, "wrong active window %p\n", GetActiveWindow());
3538     ok(GetFocus() == mdi_child2 || /* win2k */
3539        GetFocus() == 0, /* win9x */
3540        "wrong focus window %p\n", GetFocus());
3541
3542     SetFocus(0);
3543     flush_sequence();
3544
3545     ShowWindow(mdi_child2, SW_HIDE);
3546     ok_sequence(WmHideChildSeq, "ShowWindow(SW_HIDE):MDI child", FALSE);
3547
3548     ShowWindow(mdi_child2, SW_RESTORE);
3549     ok_sequence(WmRestoreMDIchildInisibleSeq, "ShowWindow(SW_RESTORE):invisible MDI child", FALSE);
3550     flush_sequence();
3551
3552     ok(GetWindowLongA(mdi_child2, GWL_STYLE) & WS_VISIBLE, "MDI child should be visible\n");
3553     ok(IsWindowVisible(mdi_child2), "MDI child should be visible\n");
3554
3555     active_child = (HWND)SendMessageA(mdi_client, WM_MDIGETACTIVE, 0, (LPARAM)&zoomed);
3556     ok(active_child == mdi_child2, "wrong active MDI child %p\n", active_child);
3557     ok(!zoomed, "wrong zoomed state %d\n", zoomed);
3558     flush_sequence();
3559
3560     SetFocus(0);
3561     flush_sequence();
3562
3563     ShowWindow(mdi_child2, SW_HIDE);
3564     ok_sequence(WmHideChildSeq, "ShowWindow(SW_HIDE):MDI child", FALSE);
3565
3566     ShowWindow(mdi_child2, SW_SHOW);
3567     ok_sequence(WmShowChildSeq, "ShowWindow(SW_SHOW):MDI child", FALSE);
3568
3569     ok(GetActiveWindow() == mdi_frame, "wrong active window %p\n", GetActiveWindow());
3570     ok(GetFocus() == 0, "wrong focus window %p\n", GetFocus());
3571
3572     ShowWindow(mdi_child2, SW_MAXIMIZE);
3573     ok_sequence(WmMaximizeMDIchildVisibleSeq, "ShowWindow(SW_MAXIMIZE):MDI child", FALSE);
3574
3575     ok(GetActiveWindow() == mdi_frame, "wrong active window %p\n", GetActiveWindow());
3576     ok(GetFocus() == 0, "wrong focus window %p\n", GetFocus());
3577
3578     ShowWindow(mdi_child2, SW_RESTORE);
3579     ok_sequence(WmRestoreMDIchildVisibleSeq, "ShowWindow(SW_RESTORE):maximized MDI child", FALSE);
3580
3581     ok(GetActiveWindow() == mdi_frame, "wrong active window %p\n", GetActiveWindow());
3582     ok(GetFocus() == 0, "wrong focus window %p\n", GetFocus());
3583
3584     ShowWindow(mdi_child2, SW_MINIMIZE);
3585     ok_sequence(WmMinimizeMDIchildVisibleSeq, "ShowWindow(SW_MINIMIZE):MDI child", TRUE);
3586
3587     ok(GetActiveWindow() == mdi_frame, "wrong active window %p\n", GetActiveWindow());
3588     ok(GetFocus() == 0, "wrong focus window %p\n", GetFocus());
3589
3590     active_child = (HWND)SendMessageA(mdi_client, WM_MDIGETACTIVE, 0, (LPARAM)&zoomed);
3591     ok(active_child == mdi_child2, "wrong active MDI child %p\n", active_child);
3592     ok(!zoomed, "wrong zoomed state %d\n", zoomed);
3593     flush_sequence();
3594
3595     ShowWindow(mdi_child2, SW_RESTORE);
3596     ok_sequence(WmRestoreMDIchildVisibleSeq_2, "ShowWindow(SW_RESTORE):minimized MDI child", FALSE);
3597
3598     ok(GetActiveWindow() == mdi_frame, "wrong active window %p\n", GetActiveWindow());
3599     ok(GetFocus() == mdi_child2, "wrong focus window %p\n", GetFocus());
3600
3601     active_child = (HWND)SendMessageA(mdi_client, WM_MDIGETACTIVE, 0, (LPARAM)&zoomed);
3602     ok(active_child == mdi_child2, "wrong active MDI child %p\n", active_child);
3603     ok(!zoomed, "wrong zoomed state %d\n", zoomed);
3604     flush_sequence();
3605
3606     SetFocus(0);
3607     flush_sequence();
3608
3609     ShowWindow(mdi_child2, SW_HIDE);
3610     ok_sequence(WmHideChildSeq, "ShowWindow(SW_HIDE):MDI child", FALSE);
3611
3612     ok(GetActiveWindow() == mdi_frame, "wrong active window %p\n", GetActiveWindow());
3613     ok(GetFocus() == 0, "wrong focus window %p\n", GetFocus());
3614
3615     DestroyWindow(mdi_child2);
3616     ok_sequence(WmDestroyMDIchildInvisibleSeq, "Destroy invisible MDI child window", FALSE);
3617
3618     ok(GetActiveWindow() == mdi_frame, "wrong active window %p\n", GetActiveWindow());
3619     ok(GetFocus() == 0, "wrong focus window %p\n", GetFocus());
3620
3621     /* test for maximized MDI children */
3622     trace("creating maximized visible MDI child window 1\n");
3623     mdi_child = CreateWindowExA(WS_EX_MDICHILD, "MDI_child_class", "MDI child",
3624                                 WS_CHILD | WS_VISIBLE | WS_MAXIMIZEBOX | WS_MAXIMIZE,
3625                                 0, 0, CW_USEDEFAULT, CW_USEDEFAULT,
3626                                 mdi_client, 0, GetModuleHandleA(0), NULL);
3627     assert(mdi_child);
3628     ok_sequence(WmCreateMDIchildVisibleMaxSeq1, "Create maximized visible 1st MDI child window", TRUE);
3629     ok(IsZoomed(mdi_child), "1st MDI child should be maximized\n");
3630
3631     ok(GetActiveWindow() == mdi_frame, "wrong active window %p\n", GetActiveWindow());
3632     ok(GetFocus() == mdi_child || /* win2k */
3633        GetFocus() == 0, /* win9x */
3634        "wrong focus window %p\n", GetFocus());
3635
3636     active_child = (HWND)SendMessageA(mdi_client, WM_MDIGETACTIVE, 0, (LPARAM)&zoomed);
3637     ok(active_child == mdi_child, "wrong active MDI child %p\n", active_child);
3638     ok(zoomed, "wrong zoomed state %d\n", zoomed);
3639     flush_sequence();
3640
3641     trace("creating maximized visible MDI child window 2\n");
3642     mdi_child2 = CreateWindowExA(WS_EX_MDICHILD, "MDI_child_class", "MDI child",
3643                                 WS_CHILD | WS_VISIBLE | WS_MAXIMIZEBOX | WS_MAXIMIZE,
3644                                 0, 0, CW_USEDEFAULT, CW_USEDEFAULT,
3645                                 mdi_client, 0, GetModuleHandleA(0), NULL);
3646     assert(mdi_child2);
3647     ok_sequence(WmCreateMDIchildVisibleMaxSeq2, "Create maximized visible 2nd MDI child 2 window", TRUE);
3648     ok(IsZoomed(mdi_child2), "2nd MDI child should be maximized\n");
3649     ok(!IsZoomed(mdi_child), "1st MDI child should NOT be maximized\n");
3650
3651     ok(GetActiveWindow() == mdi_frame, "wrong active window %p\n", GetActiveWindow());
3652     ok(GetFocus() == mdi_child2, "wrong focus window %p\n", GetFocus());
3653
3654     active_child = (HWND)SendMessageA(mdi_client, WM_MDIGETACTIVE, 0, (LPARAM)&zoomed);
3655     ok(active_child == mdi_child2, "wrong active MDI child %p\n", active_child);
3656     ok(zoomed, "wrong zoomed state %d\n", zoomed);
3657     flush_sequence();
3658
3659     trace("destroying maximized visible MDI child window 2\n");
3660     DestroyWindow(mdi_child2);
3661     ok_sequence(WmDestroyMDIchildVisibleSeq, "Destroy visible MDI child window", TRUE);
3662
3663     ok(!IsZoomed(mdi_child), "1st MDI child should NOT be maximized\n");
3664
3665     ok(GetActiveWindow() == mdi_frame, "wrong active window %p\n", GetActiveWindow());
3666     ok(GetFocus() == 0, "wrong focus window %p\n", GetFocus());
3667
3668     /* Win2k: MDI client still returns a just destroyed child as active
3669      * Win9x: MDI client returns 0
3670      */
3671     active_child = (HWND)SendMessageA(mdi_client, WM_MDIGETACTIVE, 0, (LPARAM)&zoomed);
3672     ok(active_child == mdi_child2 || /* win2k */
3673        !active_child, /* win9x */
3674        "wrong active MDI child %p\n", active_child);
3675     flush_sequence();
3676
3677     ShowWindow(mdi_child, SW_MAXIMIZE);
3678     ok(IsZoomed(mdi_child), "1st MDI child should be maximized\n");
3679     flush_sequence();
3680
3681     ok(GetActiveWindow() == mdi_frame, "wrong active window %p\n", GetActiveWindow());
3682     ok(GetFocus() == mdi_child, "wrong focus window %p\n", GetFocus());
3683
3684     trace("re-creating maximized visible MDI child window 2\n");
3685     mdi_child2 = CreateWindowExA(WS_EX_MDICHILD, "MDI_child_class", "MDI child",
3686                                 WS_CHILD | WS_VISIBLE | WS_MAXIMIZEBOX | WS_MAXIMIZE,
3687                                 0, 0, CW_USEDEFAULT, CW_USEDEFAULT,
3688                                 mdi_client, 0, GetModuleHandleA(0), NULL);
3689     assert(mdi_child2);
3690     ok_sequence(WmCreateMDIchildVisibleMaxSeq2, "Create maximized visible 2nd MDI child 2 window", TRUE);
3691     ok(IsZoomed(mdi_child2), "2nd MDI child should be maximized\n");
3692     ok(!IsZoomed(mdi_child), "1st MDI child should NOT be maximized\n");
3693
3694     ok(GetActiveWindow() == mdi_frame, "wrong active window %p\n", GetActiveWindow());
3695     ok(GetFocus() == mdi_child2, "wrong focus window %p\n", GetFocus());
3696
3697     active_child = (HWND)SendMessageA(mdi_client, WM_MDIGETACTIVE, 0, (LPARAM)&zoomed);
3698     ok(active_child == mdi_child2, "wrong active MDI child %p\n", active_child);
3699     ok(zoomed, "wrong zoomed state %d\n", zoomed);
3700     flush_sequence();
3701
3702     SendMessageA(mdi_child2, WM_SYSCOMMAND, SC_CLOSE, 0);
3703     ok_sequence(WmDestroyMDIchildVisibleMaxSeq2, "WM_SYSCOMMAND/SC_CLOSE on a visible maximized MDI child window", TRUE);
3704     ok(!IsWindow(mdi_child2), "MDI child 2 should be destroyed\n");
3705
3706     ok(IsZoomed(mdi_child), "1st MDI child should be maximized\n");
3707     ok(GetActiveWindow() == mdi_frame, "wrong active window %p\n", GetActiveWindow());
3708     ok(GetFocus() == mdi_child, "wrong focus window %p\n", GetFocus());
3709
3710     active_child = (HWND)SendMessageA(mdi_client, WM_MDIGETACTIVE, 0, (LPARAM)&zoomed);
3711     ok(active_child == mdi_child, "wrong active MDI child %p\n", active_child);
3712     ok(zoomed, "wrong zoomed state %d\n", zoomed);
3713     flush_sequence();
3714
3715     DestroyWindow(mdi_child);
3716     ok_sequence(WmDestroyMDIchildVisibleSeq, "Destroy visible MDI child window", TRUE);
3717
3718     ok(GetActiveWindow() == mdi_frame, "wrong active window %p\n", GetActiveWindow());
3719     ok(GetFocus() == 0, "wrong focus window %p\n", GetFocus());
3720
3721     /* Win2k: MDI client still returns a just destroyed child as active
3722      * Win9x: MDI client returns 0
3723      */
3724     active_child = (HWND)SendMessageA(mdi_client, WM_MDIGETACTIVE, 0, (LPARAM)&zoomed);
3725     ok(active_child == mdi_child || /* win2k */
3726        !active_child, /* win9x */
3727        "wrong active MDI child %p\n", active_child);
3728     flush_sequence();
3729
3730     trace("creating maximized invisible MDI child window\n");
3731     mdi_child2 = CreateWindowExA(WS_EX_MDICHILD, "MDI_child_class", "MDI child",
3732                                 WS_CHILD | WS_MAXIMIZE | WS_CAPTION | WS_THICKFRAME,
3733                                 0, 0, CW_USEDEFAULT, CW_USEDEFAULT,
3734                                 mdi_client, 0, GetModuleHandleA(0), NULL);
3735     assert(mdi_child2);
3736     ok_sequence(WmCreateMDIchildInvisibleMaxSeq4, "Create maximized invisible MDI child window", FALSE);
3737     ok(IsZoomed(mdi_child2), "MDI child should be maximized\n");
3738     ok(!(GetWindowLongA(mdi_child2, GWL_STYLE) & WS_VISIBLE), "MDI child should be not visible\n");
3739     ok(!IsWindowVisible(mdi_child2), "MDI child should be not visible\n");
3740
3741     /* Win2k: MDI client still returns a just destroyed child as active
3742      * Win9x: MDI client returns 0
3743      */
3744     active_child = (HWND)SendMessageA(mdi_client, WM_MDIGETACTIVE, 0, (LPARAM)&zoomed);
3745     ok(active_child == mdi_child || /* win2k */
3746        !active_child || active_child == mdi_child2, /* win9x */
3747        "wrong active MDI child %p\n", active_child);
3748     flush_sequence();
3749
3750     trace("call ShowWindow(mdi_child, SW_MAXIMIZE)\n");
3751     ShowWindow(mdi_child2, SW_MAXIMIZE);
3752     ok_sequence(WmMaximizeMDIchildInvisibleSeq2, "ShowWindow(SW_MAXIMIZE):invisible maximized MDI child", FALSE);
3753     ok(IsZoomed(mdi_child2), "MDI child should be maximized\n");
3754     ok(GetWindowLongA(mdi_child2, GWL_STYLE) & WS_VISIBLE, "MDI child should be visible\n");
3755     ok(IsWindowVisible(mdi_child2), "MDI child should be visible\n");
3756
3757     active_child = (HWND)SendMessageA(mdi_client, WM_MDIGETACTIVE, 0, (LPARAM)&zoomed);
3758     ok(active_child == mdi_child2, "wrong active MDI child %p\n", active_child);
3759     ok(zoomed, "wrong zoomed state %d\n", zoomed);
3760     flush_sequence();
3761
3762     SendMessageA(mdi_client, WM_MDIDESTROY, (WPARAM)mdi_child2, 0);
3763     flush_sequence();
3764
3765     /* end of test for maximized MDI children */
3766     SetFocus(0);
3767     flush_sequence();
3768     trace("creating maximized visible MDI child window 1(Switch test)\n");
3769     mdi_child = CreateWindowExA(WS_EX_MDICHILD, "MDI_child_class", "MDI child",
3770                                 WS_CHILD | WS_VISIBLE | WS_MAXIMIZEBOX | WS_MAXIMIZE,
3771                                 0, 0, CW_USEDEFAULT, CW_USEDEFAULT,
3772                                 mdi_client, 0, GetModuleHandleA(0), NULL);
3773     assert(mdi_child);
3774     ok_sequence(WmCreateMDIchildVisibleMaxSeq1, "Create maximized visible 1st MDI child window(Switch test)", TRUE);
3775     ok(IsZoomed(mdi_child), "1st MDI child should be maximized(Switch test)\n");
3776
3777     ok(GetActiveWindow() == mdi_frame, "wrong active window %p(Switch test)\n", GetActiveWindow());
3778     ok(GetFocus() == mdi_child || /* win2k */
3779        GetFocus() == 0, /* win9x */
3780        "wrong focus window %p(Switch test)\n", GetFocus());
3781
3782     active_child = (HWND)SendMessageA(mdi_client, WM_MDIGETACTIVE, 0, (LPARAM)&zoomed);
3783     ok(active_child == mdi_child, "wrong active MDI child %p(Switch test)\n", active_child);
3784     ok(zoomed, "wrong zoomed state %d(Switch test)\n", zoomed);
3785     flush_sequence();
3786
3787     trace("creating maximized visible MDI child window 2(Switch test)\n");
3788     mdi_child2 = CreateWindowExA(WS_EX_MDICHILD, "MDI_child_class", "MDI child",
3789                                 WS_CHILD | WS_VISIBLE | WS_MAXIMIZEBOX | WS_MAXIMIZE,
3790                                 0, 0, CW_USEDEFAULT, CW_USEDEFAULT,
3791                                 mdi_client, 0, GetModuleHandleA(0), NULL);
3792     assert(mdi_child2);
3793     ok_sequence(WmCreateMDIchildVisibleMaxSeq2, "Create maximized visible 2nd MDI child window (Switch test)", TRUE);
3794
3795     ok(IsZoomed(mdi_child2), "2nd MDI child should be maximized(Switch test)\n");
3796     ok(!IsZoomed(mdi_child), "1st MDI child should NOT be maximized(Switch test)\n");
3797
3798     ok(GetActiveWindow() == mdi_frame, "wrong active window %p(Switch test)\n", GetActiveWindow());
3799     ok(GetFocus() == mdi_child2, "wrong focus window %p(Switch test)\n", GetFocus());
3800
3801     active_child = (HWND)SendMessageA(mdi_client, WM_MDIGETACTIVE, 0, (LPARAM)&zoomed);
3802     ok(active_child == mdi_child2, "wrong active MDI child %p(Switch test)\n", active_child);
3803     ok(zoomed, "wrong zoomed state %d(Switch test)\n", zoomed);
3804     flush_sequence();
3805
3806     trace("Switch child window.\n");
3807     SendMessageA(mdi_client, WM_MDIACTIVATE, (WPARAM)mdi_child, 0);
3808     ok_sequence(WmSwitchChild, "Child did not switch correctly", TRUE);
3809     trace("end of test for switch maximized MDI children\n");
3810     flush_sequence();
3811
3812     /* Prepare for switching test of not maximized MDI children  */
3813     ShowWindow( mdi_child, SW_NORMAL );
3814     ok(!IsZoomed(mdi_child), "wrong zoomed state for %p(Switch test)\n", mdi_child);
3815     ok(!IsZoomed(mdi_child2), "wrong zoomed state for %p(Switch test)\n", mdi_child2);
3816     active_child = (HWND)SendMessageA(mdi_client, WM_MDIGETACTIVE, 0, 0);
3817     ok(active_child == mdi_child, "wrong active MDI child %p(Switch test)\n", active_child);
3818     flush_sequence();
3819
3820     SendMessageA(mdi_client, WM_MDIACTIVATE, (WPARAM)mdi_child2, 0);
3821     ok_sequence(WmSwitchNotMaximizedChild, "Not maximized child did not switch correctly", FALSE);
3822     trace("end of test for switch not maximized MDI children\n");
3823     flush_sequence();
3824
3825     SendMessageA(mdi_client, WM_MDIDESTROY, (WPARAM)mdi_child, 0);
3826     flush_sequence();
3827
3828     SendMessageA(mdi_client, WM_MDIDESTROY, (WPARAM)mdi_child2, 0);
3829     flush_sequence();
3830
3831     SetFocus(0);
3832     flush_sequence();
3833     /* end of tests for switch maximized/not maximized MDI children */
3834
3835     mdi_cs.szClass = "MDI_child_Class";
3836     mdi_cs.szTitle = "MDI child";
3837     mdi_cs.hOwner = GetModuleHandleA(0);
3838     mdi_cs.x = 0;
3839     mdi_cs.y = 0;
3840     mdi_cs.cx = CW_USEDEFAULT;
3841     mdi_cs.cy = CW_USEDEFAULT;
3842     mdi_cs.style = WS_CHILD | WS_SYSMENU | WS_VISIBLE | WS_MAXIMIZEBOX | WS_MAXIMIZE;
3843     mdi_cs.lParam = 0;
3844     mdi_child = (HWND)SendMessageA(mdi_client, WM_MDICREATE, 0, (LPARAM)&mdi_cs);
3845     ok(mdi_child != 0, "MDI child creation failed\n");
3846     ok_sequence(WmCreateMDIchildVisibleMaxSeq3, "WM_MDICREATE for maximized visible MDI child window", TRUE);
3847
3848     ok(GetMenuItemID(hMenu, GetMenuItemCount(hMenu) - 1) == SC_CLOSE, "SC_CLOSE menu item not found\n");
3849
3850     active_child = (HWND)SendMessageA(mdi_client, WM_MDIGETACTIVE, 0, (LPARAM)&zoomed);
3851     ok(active_child == mdi_child, "wrong active MDI child %p\n", active_child);
3852
3853     ok(IsZoomed(mdi_child), "MDI child should be maximized\n");
3854     ok(GetActiveWindow() == mdi_frame, "wrong active window %p\n", GetActiveWindow());
3855     ok(GetFocus() == mdi_child, "wrong focus window %p\n", GetFocus());
3856
3857     active_child = (HWND)SendMessageA(mdi_client, WM_MDIGETACTIVE, 0, (LPARAM)&zoomed);
3858     ok(active_child == mdi_child, "wrong active MDI child %p\n", active_child);
3859     ok(zoomed, "wrong zoomed state %d\n", zoomed);
3860     flush_sequence();
3861
3862     SendMessageA(mdi_client, WM_MDIDESTROY, (WPARAM)mdi_child, 0);
3863     ok_sequence(WmDestroyMDIchildVisibleMaxSeq1, "Destroy visible maximized MDI child window", TRUE);
3864
3865     ok(!IsWindow(mdi_child), "MDI child should be destroyed\n");
3866     active_child = (HWND)SendMessageA(mdi_client, WM_MDIGETACTIVE, 0, (LPARAM)&zoomed);
3867     ok(!active_child, "wrong active MDI child %p\n", active_child);
3868
3869     SetFocus(0);
3870     flush_sequence();
3871
3872     DestroyWindow(mdi_client);
3873     ok_sequence(WmDestroyMDIclientSeq, "Destroy MDI client window", FALSE);
3874
3875     /* test maximization of MDI child with invisible parent */
3876     client_cs.hWindowMenu = 0;
3877     mdi_client = CreateWindow("MDI_client_class",
3878                                  NULL,
3879                                  WS_CHILD | WS_CLIPCHILDREN | WS_VSCROLL | WS_HSCROLL | WS_VISIBLE,
3880                                  0, 0, 660, 430,
3881                                  mdi_frame, 0, GetModuleHandleA(0), &client_cs);
3882     ok_sequence(WmCreateMDIclientSeq, "Create MDI client window", FALSE);
3883
3884     ShowWindow(mdi_client, SW_HIDE);
3885     ok_sequence(WmHideMDIclientSeq, "Hide MDI client window", FALSE);
3886
3887     mdi_child = CreateWindowExA(WS_EX_MDICHILD, "MDI_child_class", "MDI child",
3888                                 WS_CHILD | WS_CLIPCHILDREN | WS_VSCROLL | WS_HSCROLL,
3889                                 0, 0, 650, 440,
3890                                 mdi_client, 0, GetModuleHandleA(0), NULL);
3891     ok_sequence(WmCreateMDIchildInvisibleParentSeq, "Create MDI child window with invisible parent", FALSE);
3892
3893     SendMessage(mdi_client, WM_MDIMAXIMIZE, (WPARAM) mdi_child, 0);
3894     ok_sequence(WmMaximizeMDIchildInvisibleParentSeq, "Maximize MDI child window with invisible parent", TRUE);
3895     zoomed = IsZoomed(mdi_child);
3896     ok(zoomed, "wrong zoomed state %d\n", zoomed);
3897     
3898     ShowWindow(mdi_client, SW_SHOW);
3899     ok_sequence(WmShowMDIclientSeq, "Show MDI client window", FALSE);
3900
3901     DestroyWindow(mdi_child);
3902     ok_sequence(WmDestroyMDIchildVisibleSeq, "Destroy visible maximized MDI child window", TRUE);
3903
3904     /* end of test for maximization of MDI child with invisible parent */
3905
3906     DestroyWindow(mdi_client);
3907     ok_sequence(WmDestroyMDIclientSeq, "Destroy MDI client window", FALSE);
3908
3909     DestroyWindow(mdi_frame);
3910     ok_sequence(WmDestroyMDIframeSeq, "Destroy MDI frame window", FALSE);
3911 }
3912 /************************* End of MDI test **********************************/
3913
3914 static void test_WM_SETREDRAW(HWND hwnd)
3915 {
3916     DWORD style = GetWindowLongA(hwnd, GWL_STYLE);
3917
3918     flush_events();
3919     flush_sequence();
3920
3921     SendMessageA(hwnd, WM_SETREDRAW, FALSE, 0);
3922     ok_sequence(WmSetRedrawFalseSeq, "SetRedraw:FALSE", FALSE);
3923
3924     ok(!(GetWindowLongA(hwnd, GWL_STYLE) & WS_VISIBLE), "WS_VISIBLE should NOT be set\n");
3925     ok(!IsWindowVisible(hwnd), "IsWindowVisible() should return FALSE\n");
3926
3927     flush_sequence();
3928     SendMessageA(hwnd, WM_SETREDRAW, TRUE, 0);
3929     ok_sequence(WmSetRedrawTrueSeq, "SetRedraw:TRUE", FALSE);
3930
3931     ok(GetWindowLongA(hwnd, GWL_STYLE) & WS_VISIBLE, "WS_VISIBLE should be set\n");
3932     ok(IsWindowVisible(hwnd), "IsWindowVisible() should return TRUE\n");
3933
3934     /* restore original WS_VISIBLE state */
3935     SetWindowLongA(hwnd, GWL_STYLE, style);
3936
3937     flush_events();
3938     flush_sequence();
3939 }
3940
3941 static INT_PTR CALLBACK TestModalDlgProcA(HWND hwnd, UINT message, WPARAM wParam, LPARAM lParam)
3942 {
3943     struct recvd_message msg;
3944
3945     if (ignore_message( message )) return 0;
3946
3947     switch (message)
3948     {
3949         /* ignore */
3950         case WM_MOUSEMOVE:
3951         case WM_NCMOUSEMOVE:
3952         case WM_NCMOUSELEAVE:
3953         case WM_SETCURSOR:
3954             return 0;
3955         case WM_NCHITTEST:
3956             return HTCLIENT;
3957     }
3958
3959     msg.hwnd = hwnd;
3960     msg.message = message;
3961     msg.flags = sent|wparam|lparam;
3962     msg.wParam = wParam;
3963     msg.lParam = lParam;
3964     msg.descr = "dialog";
3965     add_message(&msg);
3966
3967     if (message == WM_INITDIALOG) SetTimer( hwnd, 1, 100, NULL );
3968     if (message == WM_TIMER) EndDialog( hwnd, 0 );
3969     return 0;
3970 }
3971
3972 static void test_hv_scroll_1(HWND hwnd, INT ctl, DWORD clear, DWORD set, INT min, INT max)
3973 {
3974     DWORD style, exstyle;
3975     INT xmin, xmax;
3976     BOOL ret;
3977
3978     exstyle = GetWindowLongA(hwnd, GWL_EXSTYLE);
3979     style = GetWindowLongA(hwnd, GWL_STYLE);
3980     /* do not be confused by WS_DLGFRAME set */
3981     if ((style & WS_CAPTION) == WS_CAPTION) style &= ~WS_CAPTION;
3982
3983     if (clear) ok(style & clear, "style %08x should be set\n", clear);
3984     if (set) ok(!(style & set), "style %08x should not be set\n", set);
3985
3986     ret = SetScrollRange(hwnd, ctl, min, max, FALSE);
3987     ok( ret, "SetScrollRange(%d) error %d\n", ctl, GetLastError());
3988     if ((style & (WS_DLGFRAME | WS_BORDER | WS_THICKFRAME)) || (exstyle & WS_EX_DLGMODALFRAME))
3989         ok_sequence(WmSetScrollRangeHV_NC_Seq, "SetScrollRange(SB_HORZ/SB_VERT) NC", FALSE);
3990     else
3991         ok_sequence(WmSetScrollRangeHVSeq, "SetScrollRange(SB_HORZ/SB_VERT)", FALSE);
3992
3993     style = GetWindowLongA(hwnd, GWL_STYLE);
3994     if (set) ok(style & set, "style %08x should be set\n", set);
3995     if (clear) ok(!(style & clear), "style %08x should not be set\n", clear);
3996
3997     /* a subsequent call should do nothing */
3998     ret = SetScrollRange(hwnd, ctl, min, max, FALSE);
3999     ok( ret, "SetScrollRange(%d) error %d\n", ctl, GetLastError());
4000     ok_sequence(WmEmptySeq, "SetScrollRange(SB_HORZ/SB_VERT) empty sequence", FALSE);
4001
4002     xmin = 0xdeadbeef;
4003     xmax = 0xdeadbeef;
4004     ret = GetScrollRange(hwnd, ctl, &xmin, &xmax);
4005     ok( ret, "GetScrollRange(%d) error %d\n", ctl, GetLastError());
4006     ok_sequence(WmEmptySeq, "GetScrollRange(SB_HORZ/SB_VERT) empty sequence", FALSE);
4007     ok(xmin == min, "unexpected min scroll value %d\n", xmin);
4008     ok(xmax == max, "unexpected max scroll value %d\n", xmax);
4009 }
4010
4011 static void test_hv_scroll_2(HWND hwnd, INT ctl, DWORD clear, DWORD set, INT min, INT max)
4012 {
4013     DWORD style, exstyle;
4014     SCROLLINFO si;
4015     BOOL ret;
4016
4017     exstyle = GetWindowLongA(hwnd, GWL_EXSTYLE);
4018     style = GetWindowLongA(hwnd, GWL_STYLE);
4019     /* do not be confused by WS_DLGFRAME set */
4020     if ((style & WS_CAPTION) == WS_CAPTION) style &= ~WS_CAPTION;
4021
4022     if (clear) ok(style & clear, "style %08x should be set\n", clear);
4023     if (set) ok(!(style & set), "style %08x should not be set\n", set);
4024
4025     si.cbSize = sizeof(si);
4026     si.fMask = SIF_RANGE;
4027     si.nMin = min;
4028     si.nMax = max;
4029     SetScrollInfo(hwnd, ctl, &si, TRUE);
4030     if ((style & (WS_DLGFRAME | WS_BORDER | WS_THICKFRAME)) || (exstyle & WS_EX_DLGMODALFRAME))
4031         ok_sequence(WmSetScrollRangeHV_NC_Seq, "SetScrollInfo(SB_HORZ/SB_VERT) NC", FALSE);
4032     else
4033         ok_sequence(WmSetScrollRangeHVSeq, "SetScrollInfo(SB_HORZ/SB_VERT)", FALSE);
4034
4035     style = GetWindowLongA(hwnd, GWL_STYLE);
4036     if (set) ok(style & set, "style %08x should be set\n", set);
4037     if (clear) ok(!(style & clear), "style %08x should not be set\n", clear);
4038
4039     /* a subsequent call should do nothing */
4040     SetScrollInfo(hwnd, ctl, &si, TRUE);
4041     if (style & WS_HSCROLL)
4042         ok_sequence(WmSetScrollRangeHSeq_empty, "SetScrollInfo(SB_HORZ/SB_VERT) empty sequence", FALSE);
4043     else if (style & WS_VSCROLL)
4044         ok_sequence(WmSetScrollRangeVSeq_empty, "SetScrollInfo(SB_HORZ/SB_VERT) empty sequence", FALSE);
4045     else
4046         ok_sequence(WmEmptySeq, "SetScrollInfo(SB_HORZ/SB_VERT) empty sequence", FALSE);
4047
4048     si.fMask = SIF_PAGE;
4049     si.nPage = 5;
4050     SetScrollInfo(hwnd, ctl, &si, FALSE);
4051     ok_sequence(WmEmptySeq, "SetScrollInfo(SB_HORZ/SB_VERT) empty sequence", FALSE);
4052
4053     si.fMask = SIF_POS;
4054     si.nPos = max - 1;
4055     SetScrollInfo(hwnd, ctl, &si, FALSE);
4056     ok_sequence(WmEmptySeq, "SetScrollInfo(SB_HORZ/SB_VERT) empty sequence", FALSE);
4057
4058     si.fMask = SIF_RANGE;
4059     si.nMin = 0xdeadbeef;
4060     si.nMax = 0xdeadbeef;
4061     ret = GetScrollInfo(hwnd, ctl, &si);
4062     ok( ret, "GetScrollInfo error %d\n", GetLastError());
4063     ok_sequence(WmEmptySeq, "GetScrollRange(SB_HORZ/SB_VERT) empty sequence", FALSE);
4064     ok(si.nMin == min, "unexpected min scroll value %d\n", si.nMin);
4065     ok(si.nMax == max, "unexpected max scroll value %d\n", si.nMax);
4066 }
4067
4068 /* Win9x sends WM_USER+xxx while and NT versions send SBM_xxx messages */
4069 static void test_scroll_messages(HWND hwnd)
4070 {
4071     SCROLLINFO si;
4072     INT min, max;
4073     BOOL ret;
4074
4075     flush_events();
4076     flush_sequence();
4077
4078     min = 0xdeadbeef;
4079     max = 0xdeadbeef;
4080     ret = GetScrollRange(hwnd, SB_CTL, &min, &max);
4081     ok( ret, "GetScrollRange error %d\n", GetLastError());
4082     if (sequence->message != WmGetScrollRangeSeq[0].message)
4083         trace("GetScrollRange(SB_CTL) generated unknown message %04x\n", sequence->message);
4084     /* values of min and max are undefined */
4085     flush_sequence();
4086
4087     ret = SetScrollRange(hwnd, SB_CTL, 10, 150, FALSE);
4088     ok( ret, "SetScrollRange error %d\n", GetLastError());
4089     if (sequence->message != WmSetScrollRangeSeq[0].message)
4090         trace("SetScrollRange(SB_CTL) generated unknown message %04x\n", sequence->message);
4091     flush_sequence();
4092
4093     min = 0xdeadbeef;
4094     max = 0xdeadbeef;
4095     ret = GetScrollRange(hwnd, SB_CTL, &min, &max);
4096     ok( ret, "GetScrollRange error %d\n", GetLastError());
4097     if (sequence->message != WmGetScrollRangeSeq[0].message)
4098         trace("GetScrollRange(SB_CTL) generated unknown message %04x\n", sequence->message);
4099     /* values of min and max are undefined */
4100     flush_sequence();
4101
4102     si.cbSize = sizeof(si);
4103     si.fMask = SIF_RANGE;
4104     si.nMin = 20;
4105     si.nMax = 160;
4106     SetScrollInfo(hwnd, SB_CTL, &si, FALSE);
4107     if (sequence->message != WmSetScrollRangeSeq[0].message)
4108         trace("SetScrollInfo(SB_CTL) generated unknown message %04x\n", sequence->message);
4109     flush_sequence();
4110
4111     si.fMask = SIF_PAGE;
4112     si.nPage = 10;
4113     SetScrollInfo(hwnd, SB_CTL, &si, FALSE);
4114     if (sequence->message != WmSetScrollRangeSeq[0].message)
4115         trace("SetScrollInfo(SB_CTL) generated unknown message %04x\n", sequence->message);
4116     flush_sequence();
4117
4118     si.fMask = SIF_POS;
4119     si.nPos = 20;
4120     SetScrollInfo(hwnd, SB_CTL, &si, FALSE);
4121     if (sequence->message != WmSetScrollRangeSeq[0].message)
4122         trace("SetScrollInfo(SB_CTL) generated unknown message %04x\n", sequence->message);
4123     flush_sequence();
4124
4125     si.fMask = SIF_RANGE;
4126     si.nMin = 0xdeadbeef;
4127     si.nMax = 0xdeadbeef;
4128     ret = GetScrollInfo(hwnd, SB_CTL, &si);
4129     ok( ret, "GetScrollInfo error %d\n", GetLastError());
4130     if (sequence->message != WmGetScrollInfoSeq[0].message)
4131         trace("GetScrollInfo(SB_CTL) generated unknown message %04x\n", sequence->message);
4132     /* values of min and max are undefined */
4133     flush_sequence();
4134
4135     /* set WS_HSCROLL */
4136     test_hv_scroll_1(hwnd, SB_HORZ, 0, WS_HSCROLL, 10, 150);
4137     /* clear WS_HSCROLL */
4138     test_hv_scroll_1(hwnd, SB_HORZ, WS_HSCROLL, 0, 0, 0);
4139
4140     /* set WS_HSCROLL */
4141     test_hv_scroll_2(hwnd, SB_HORZ, 0, WS_HSCROLL, 10, 150);
4142     /* clear WS_HSCROLL */
4143     test_hv_scroll_2(hwnd, SB_HORZ, WS_HSCROLL, 0, 0, 0);
4144
4145     /* set WS_VSCROLL */
4146     test_hv_scroll_1(hwnd, SB_VERT, 0, WS_VSCROLL, 10, 150);
4147     /* clear WS_VSCROLL */
4148     test_hv_scroll_1(hwnd, SB_VERT, WS_VSCROLL, 0, 0, 0);
4149
4150     /* set WS_VSCROLL */
4151     test_hv_scroll_2(hwnd, SB_VERT, 0, WS_VSCROLL, 10, 150);
4152     /* clear WS_VSCROLL */
4153     test_hv_scroll_2(hwnd, SB_VERT, WS_VSCROLL, 0, 0, 0);
4154 }
4155
4156 static void test_showwindow(void)
4157 {
4158     HWND hwnd, hchild;
4159     RECT rc;
4160
4161     hwnd = CreateWindowExA(0, "TestWindowClass", "Test overlapped", WS_OVERLAPPEDWINDOW,
4162                            100, 100, 200, 200, 0, 0, 0, NULL);
4163     ok (hwnd != 0, "Failed to create overlapped window\n");
4164     hchild = CreateWindowExA(0, "TestWindowClass", "Test child", WS_CHILD,
4165                              0, 0, 10, 10, hwnd, 0, 0, NULL);
4166     ok (hchild != 0, "Failed to create child\n");
4167     flush_sequence();
4168
4169     /* ShowWindow( SW_SHOWNA) for invisible top level window */
4170     trace("calling ShowWindow( SW_SHOWNA) for invisible top level window\n");
4171     ok( ShowWindow(hwnd, SW_SHOWNA) == FALSE, "ShowWindow: window was visible\n" );
4172     ok_sequence(WmSHOWNATopInvisible, "ShowWindow(SW_SHOWNA) on invisible top level window", FALSE);
4173
4174     /* ShowWindow( SW_SHOWNA) for now visible top level window */
4175     trace("calling ShowWindow( SW_SHOWNA) for now visible top level window\n");
4176     ok( ShowWindow(hwnd, SW_SHOWNA) != FALSE, "ShowWindow: window was invisible\n" );
4177     ok_sequence(WmSHOWNATopVisible, "ShowWindow(SW_SHOWNA) on visible top level window", FALSE);
4178     /* back to invisible */
4179     ShowWindow(hchild, SW_HIDE);
4180     ShowWindow(hwnd, SW_HIDE);
4181     flush_sequence();
4182     /* ShowWindow(SW_SHOWNA) with child and parent invisible */ 
4183     trace("calling ShowWindow( SW_SHOWNA) for invisible child with invisible parent\n");
4184     ok( ShowWindow(hchild, SW_SHOWNA) == FALSE, "ShowWindow: window was visible\n" );
4185     ok_sequence(WmSHOWNAChildInvisParInvis, "ShowWindow(SW_SHOWNA) invisible child and parent", FALSE);
4186     /* ShowWindow(SW_SHOWNA) with child visible and parent invisible */ 
4187     ok( ShowWindow(hchild, SW_SHOW) != FALSE, "ShowWindow: window was invisible\n" );
4188     flush_sequence();
4189     trace("calling ShowWindow( SW_SHOWNA) for the visible child and invisible parent\n");
4190     ok( ShowWindow(hchild, SW_SHOWNA) != FALSE, "ShowWindow: window was invisible\n" );
4191     ok_sequence(WmSHOWNAChildVisParInvis, "ShowWindow(SW_SHOWNA) visible child and invisible parent", FALSE);
4192     /* ShowWindow(SW_SHOWNA) with child visible and parent visible */
4193     ShowWindow( hwnd, SW_SHOW);
4194     flush_sequence();
4195     trace("calling ShowWindow( SW_SHOWNA) for the visible child and parent\n");
4196     ok( ShowWindow(hchild, SW_SHOWNA) != FALSE, "ShowWindow: window was invisible\n" );
4197     ok_sequence(WmSHOWNAChildVisParVis, "ShowWindow(SW_SHOWNA) for the visible child and parent", FALSE);
4198
4199     /* ShowWindow(SW_SHOWNA) with child invisible and parent visible */
4200     ShowWindow( hchild, SW_HIDE);
4201     flush_sequence();
4202     trace("calling ShowWindow( SW_SHOWNA) for the invisible child and visible parent\n");
4203     ok( ShowWindow(hchild, SW_SHOWNA) == FALSE, "ShowWindow: window was visible\n" );
4204     ok_sequence(WmSHOWNAChildInvisParVis, "ShowWindow(SW_SHOWNA) for the invisible child and visible parent", FALSE);
4205
4206     SetCapture(hchild);
4207     ok(GetCapture() == hchild, "wrong capture window %p\n", GetCapture());
4208     DestroyWindow(hchild);
4209     ok(!GetCapture(), "wrong capture window %p\n", GetCapture());
4210
4211     DestroyWindow(hwnd);
4212     flush_sequence();
4213
4214     /* Popup windows */
4215     /* Test 1:
4216      * 1. Create invisible maximized popup window.
4217      * 2. Move and resize it.
4218      * 3. Show it maximized.
4219      */
4220     trace("calling CreateWindowExA( WS_MAXIMIZE ) for invisible maximized popup window\n");
4221     hwnd = CreateWindowExA(0, "TestWindowClass", "Test popup", WS_POPUP | WS_MAXIMIZE,
4222                            100, 100, 200, 200, 0, 0, 0, NULL);
4223     ok (hwnd != 0, "Failed to create popup window\n");
4224     ok(IsZoomed(hwnd), "window should be maximized\n");
4225     ok_sequence(WmCreateInvisibleMaxPopupSeq, "CreateWindow(WS_MAXIMIZED):popup", FALSE);
4226
4227     GetWindowRect(hwnd, &rc);
4228     ok( rc.right-rc.left == GetSystemMetrics(SM_CXSCREEN) &&
4229         rc.bottom-rc.top == GetSystemMetrics(SM_CYSCREEN),
4230         "Invalid maximized size before ShowWindow (%d,%d)-(%d,%d)\n",
4231         rc.left, rc.top, rc.right, rc.bottom);
4232     /* Reset window's size & position */
4233     SetWindowPos(hwnd, 0, 10, 10, 200, 200, SWP_NOZORDER | SWP_NOACTIVATE);
4234     ok(IsZoomed(hwnd), "window should be maximized\n");
4235     flush_sequence();
4236
4237     trace("calling ShowWindow( SW_SHOWMAXIMIZE ) for invisible maximized popup window\n");
4238     ShowWindow(hwnd, SW_SHOWMAXIMIZED);
4239     ok(IsZoomed(hwnd), "window should be maximized\n");
4240     ok_sequence(WmShowMaxPopupResizedSeq, "ShowWindow(SW_SHOWMAXIMIZED):invisible maximized and resized popup", FALSE);
4241
4242     GetWindowRect(hwnd, &rc);
4243     ok( rc.right-rc.left == GetSystemMetrics(SM_CXSCREEN) &&
4244         rc.bottom-rc.top == GetSystemMetrics(SM_CYSCREEN),
4245         "Invalid maximized size after ShowWindow (%d,%d)-(%d,%d)\n",
4246         rc.left, rc.top, rc.right, rc.bottom);
4247     DestroyWindow(hwnd);
4248     flush_sequence();
4249
4250     /* Test 2:
4251      * 1. Create invisible maximized popup window.
4252      * 2. Show it maximized.
4253      */
4254     trace("calling CreateWindowExA( WS_MAXIMIZE ) for invisible maximized popup window\n");
4255     hwnd = CreateWindowExA(0, "TestWindowClass", "Test popup", WS_POPUP | WS_MAXIMIZE,
4256                            100, 100, 200, 200, 0, 0, 0, NULL);
4257     ok (hwnd != 0, "Failed to create popup window\n");
4258     ok(IsZoomed(hwnd), "window should be maximized\n");
4259     ok_sequence(WmCreateInvisibleMaxPopupSeq, "CreateWindow(WS_MAXIMIZED):popup", FALSE);
4260
4261     trace("calling ShowWindow( SW_SHOWMAXIMIZE ) for invisible maximized popup window\n");
4262     ShowWindow(hwnd, SW_SHOWMAXIMIZED);
4263     ok(IsZoomed(hwnd), "window should be maximized\n");
4264     ok_sequence(WmShowMaxPopupSeq, "ShowWindow(SW_SHOWMAXIMIZED):invisible maximized popup", FALSE);
4265     DestroyWindow(hwnd);
4266     flush_sequence();
4267
4268     /* Test 3:
4269      * 1. Create visible maximized popup window.
4270      */
4271     trace("calling CreateWindowExA( WS_MAXIMIZE ) for maximized popup window\n");
4272     hwnd = CreateWindowExA(0, "TestWindowClass", "Test popup", WS_POPUP | WS_MAXIMIZE | WS_VISIBLE,
4273                            100, 100, 200, 200, 0, 0, 0, NULL);
4274     ok (hwnd != 0, "Failed to create popup window\n");
4275     ok(IsZoomed(hwnd), "window should be maximized\n");
4276     ok_sequence(WmCreateMaxPopupSeq, "CreateWindow(WS_MAXIMIZED):popup", FALSE);
4277     DestroyWindow(hwnd);
4278     flush_sequence();
4279
4280     /* Test 4:
4281      * 1. Create visible popup window.
4282      * 2. Maximize it.
4283      */
4284     trace("calling CreateWindowExA( WS_VISIBLE ) for popup window\n");
4285     hwnd = CreateWindowExA(0, "TestWindowClass", "Test popup", WS_POPUP | WS_VISIBLE,
4286                            100, 100, 200, 200, 0, 0, 0, NULL);
4287     ok (hwnd != 0, "Failed to create popup window\n");
4288     ok(!IsZoomed(hwnd), "window should NOT be maximized\n");
4289     ok_sequence(WmCreatePopupSeq, "CreateWindow(WS_VISIBLE):popup", FALSE);
4290
4291     trace("calling ShowWindow( SW_SHOWMAXIMIZE ) for visible popup window\n");
4292     ShowWindow(hwnd, SW_SHOWMAXIMIZED);
4293     ok(IsZoomed(hwnd), "window should be maximized\n");
4294     ok_sequence(WmShowVisMaxPopupSeq, "ShowWindow(SW_SHOWMAXIMIZED):popup", FALSE);
4295     DestroyWindow(hwnd);
4296     flush_sequence();
4297 }
4298
4299 static void test_sys_menu(void)
4300 {
4301     HWND hwnd;
4302     HMENU hmenu;
4303     UINT state;
4304
4305     hwnd = CreateWindowExA(0, "TestWindowClass", NULL, WS_OVERLAPPEDWINDOW,
4306                            100, 100, 200, 200, 0, 0, 0, NULL);
4307     ok (hwnd != 0, "Failed to create overlapped window\n");
4308
4309     flush_sequence();
4310
4311     /* test existing window without CS_NOCLOSE style */
4312     hmenu = GetSystemMenu(hwnd, FALSE);
4313     ok(hmenu != 0, "GetSystemMenu error %d\n", GetLastError());
4314
4315     state = GetMenuState(hmenu, SC_CLOSE, MF_BYCOMMAND);
4316     ok(state != 0xffffffff, "wrong SC_CLOSE state %x\n", state);
4317     ok(!(state & (MF_DISABLED | MF_GRAYED)), "wrong SC_CLOSE state %x\n", state);
4318
4319     EnableMenuItem(hmenu, SC_CLOSE, MF_BYCOMMAND | MF_GRAYED);
4320     ok_sequence(WmEmptySeq, "WmEnableMenuItem", FALSE);
4321
4322     state = GetMenuState(hmenu, SC_CLOSE, MF_BYCOMMAND);
4323     ok(state != 0xffffffff, "wrong SC_CLOSE state %x\n", state);
4324     ok((state & (MF_DISABLED | MF_GRAYED)) == MF_GRAYED, "wrong SC_CLOSE state %x\n", state);
4325
4326     EnableMenuItem(hmenu, SC_CLOSE, 0);
4327     ok_sequence(WmEmptySeq, "WmEnableMenuItem", FALSE);
4328
4329     state = GetMenuState(hmenu, SC_CLOSE, MF_BYCOMMAND);
4330     ok(state != 0xffffffff, "wrong SC_CLOSE state %x\n", state);
4331     ok(!(state & (MF_DISABLED | MF_GRAYED)), "wrong SC_CLOSE state %x\n", state);
4332
4333     /* test whether removing WS_SYSMENU destroys a system menu */
4334     SetWindowLongW(hwnd, GWL_STYLE, WS_POPUP);
4335     SetWindowPos(hwnd, 0, 0, 0, 0, 0, SWP_NOZORDER|SWP_NOACTIVATE|SWP_NOSIZE|SWP_NOMOVE|SWP_FRAMECHANGED);
4336     flush_sequence();
4337     hmenu = GetSystemMenu(hwnd, FALSE);
4338     ok(hmenu != 0, "GetSystemMenu error %d\n", GetLastError());
4339
4340     DestroyWindow(hwnd);
4341
4342     /* test new window with CS_NOCLOSE style */
4343     hwnd = CreateWindowExA(0, "NoCloseWindowClass", NULL, WS_OVERLAPPEDWINDOW,
4344                            100, 100, 200, 200, 0, 0, 0, NULL);
4345     ok (hwnd != 0, "Failed to create overlapped window\n");
4346
4347     hmenu = GetSystemMenu(hwnd, FALSE);
4348     ok(hmenu != 0, "GetSystemMenu error %d\n", GetLastError());
4349
4350     state = GetMenuState(hmenu, SC_CLOSE, MF_BYCOMMAND);
4351     ok(state == 0xffffffff, "wrong SC_CLOSE state %x\n", state);
4352
4353     DestroyWindow(hwnd);
4354
4355     /* test new window without WS_SYSMENU style */
4356     hwnd = CreateWindowExA(0, "NoCloseWindowClass", NULL, WS_OVERLAPPEDWINDOW & ~WS_SYSMENU,
4357                            100, 100, 200, 200, 0, 0, 0, NULL);
4358     ok(hwnd != 0, "Failed to create overlapped window\n");
4359
4360     hmenu = GetSystemMenu(hwnd, FALSE);
4361     ok(!hmenu, "GetSystemMenu error %d\n", GetLastError());
4362
4363     DestroyWindow(hwnd);
4364 }
4365
4366 /* For shown WS_OVERLAPPEDWINDOW */
4367 static const struct message WmSetIcon_1[] = {
4368     { WM_SETICON, sent },
4369     { 0x00AE, sent|defwinproc|optional }, /* XP */
4370     { WM_GETTEXT, sent|defwinproc|optional },
4371     { WM_GETTEXT, sent|defwinproc|optional }, /* XP sends a duplicate */
4372     { 0 }
4373 };
4374
4375 /* For WS_POPUP and hidden WS_OVERLAPPEDWINDOW */
4376 static const struct message WmSetIcon_2[] = {
4377     { WM_SETICON, sent },
4378     { 0 }
4379 };
4380
4381 /* Sending undocumented 0x3B message with wparam = 0x8000000b */
4382 static const struct message WmInitEndSession[] = {
4383     { 0x003B, sent },
4384     { WM_QUERYENDSESSION, sent|defwinproc|wparam|lparam, 0, ENDSESSION_LOGOFF },
4385     { 0 }
4386 };
4387
4388 /* Sending undocumented 0x3B message with wparam = 0x0000000b */
4389 static const struct message WmInitEndSession_2[] = {
4390     { 0x003B, sent },
4391     { WM_QUERYENDSESSION, sent|defwinproc|wparam|lparam, 0, 0 },
4392     { 0 }
4393 };
4394
4395 /* Sending undocumented 0x3B message with wparam = 0x80000008 */
4396 static const struct message WmInitEndSession_3[] = {
4397     { 0x003B, sent },
4398     { WM_ENDSESSION, sent|defwinproc|wparam|lparam, 0, ENDSESSION_LOGOFF },
4399     { 0 }
4400 };
4401
4402 /* Sending undocumented 0x3B message with wparam = 0x00000008 */
4403 static const struct message WmInitEndSession_4[] = {
4404     { 0x003B, sent },
4405     { WM_ENDSESSION, sent|defwinproc|wparam|lparam, 0, 0 },
4406     { 0 }
4407 };
4408
4409 /* Sending undocumented 0x3B message with wparam = 0x80000001 */
4410 static const struct message WmInitEndSession_5[] = {
4411     { 0x003B, sent },
4412     { WM_ENDSESSION, sent|defwinproc/*|wparam*/|lparam, 1, ENDSESSION_LOGOFF },
4413     { 0 }
4414 };
4415
4416 static const struct message WmOptionalPaint[] = {
4417     { WM_PAINT, sent|optional },
4418     { WM_NCPAINT, sent|beginpaint|optional },
4419     { WM_GETTEXT, sent|beginpaint|defwinproc|optional },
4420     { WM_ERASEBKGND, sent|beginpaint|optional },
4421     { 0 }
4422 };
4423
4424 static const struct message WmZOrder[] = {
4425     { WM_WINDOWPOSCHANGING, sent|wparam, 0, 0 },
4426     { WM_GETMINMAXINFO, sent|defwinproc|wparam, 0, 0 },
4427     { HCBT_ACTIVATE, hook },
4428     { WM_QUERYNEWPALETTE, sent|wparam|lparam|optional, 0, 0 },
4429     { WM_WINDOWPOSCHANGING, sent|wparam, 3, 0 },
4430     { WM_WINDOWPOSCHANGED, sent|wparam|optional, SWP_NOREDRAW|SWP_NOMOVE|SWP_NOSIZE|SWP_NOCLIENTSIZE|SWP_NOCLIENTMOVE, 0 },
4431     { WM_GETTEXT, sent|optional },
4432     { WM_NCCALCSIZE, sent|wparam|optional, 1 },
4433     { WM_ACTIVATEAPP, sent|wparam, 1, 0 },
4434     { WM_NCACTIVATE, sent|lparam, 1, 0 },
4435     { WM_GETTEXT, sent|defwinproc|optional },
4436     { WM_GETTEXT, sent|defwinproc|optional },
4437     { WM_ACTIVATE, sent|wparam|lparam, 1, 0 },
4438     { HCBT_SETFOCUS, hook },
4439     { WM_IME_SETCONTEXT, sent|wparam|defwinproc|optional, 1 },
4440     { WM_IME_NOTIFY, sent|wparam|defwinproc|optional, 2 },
4441     { WM_SETFOCUS, sent|wparam|defwinproc, 0 },
4442     { WM_WINDOWPOSCHANGED, sent|wparam, SWP_NOSIZE|SWP_NOMOVE|SWP_NOREDRAW|SWP_NOCLIENTSIZE|SWP_NOCLIENTMOVE },
4443     { WM_GETTEXT, sent|optional },
4444     { WM_NCCALCSIZE, sent|optional },
4445     { 0 }
4446 };
4447
4448 static void test_MsgWaitForMultipleObjects(HWND hwnd)
4449 {
4450     DWORD ret;
4451     MSG msg;
4452
4453     ret = MsgWaitForMultipleObjects(0, NULL, FALSE, 0, QS_POSTMESSAGE);
4454     ok(ret == WAIT_TIMEOUT, "MsgWaitForMultipleObjects returned %x\n", ret);
4455
4456     PostMessageA(hwnd, WM_USER, 0, 0);
4457
4458     ret = MsgWaitForMultipleObjects(0, NULL, FALSE, 0, QS_POSTMESSAGE);
4459     ok(ret == WAIT_OBJECT_0, "MsgWaitForMultipleObjects returned %x\n", ret);
4460
4461     ok(PeekMessageA( &msg, 0, 0, 0, PM_REMOVE ), "PeekMessage should succeed\n");
4462     ok(msg.message == WM_USER, "got %04x instead of WM_USER\n", msg.message);
4463
4464     ret = MsgWaitForMultipleObjects(0, NULL, FALSE, 0, QS_POSTMESSAGE);
4465     ok(ret == WAIT_TIMEOUT, "MsgWaitForMultipleObjects returned %x\n", ret);
4466
4467     PostMessageA(hwnd, WM_USER, 0, 0);
4468
4469     ret = MsgWaitForMultipleObjects(0, NULL, FALSE, 0, QS_POSTMESSAGE);
4470     ok(ret == WAIT_OBJECT_0, "MsgWaitForMultipleObjects returned %x\n", ret);
4471
4472     ok(PeekMessageA( &msg, 0, 0, 0, PM_NOREMOVE ), "PeekMessage should succeed\n");
4473     ok(msg.message == WM_USER, "got %04x instead of WM_USER\n", msg.message);
4474
4475     /* shows QS_POSTMESSAGE flag is cleared in the PeekMessage call */
4476     ret = MsgWaitForMultipleObjects(0, NULL, FALSE, 0, QS_POSTMESSAGE);
4477     ok(ret == WAIT_TIMEOUT, "MsgWaitForMultipleObjects returned %x\n", ret);
4478
4479     PostMessageA(hwnd, WM_USER, 0, 0);
4480
4481     /* new incoming message causes it to become signaled again */
4482     ret = MsgWaitForMultipleObjects(0, NULL, FALSE, 0, QS_POSTMESSAGE);
4483     ok(ret == WAIT_OBJECT_0, "MsgWaitForMultipleObjects returned %x\n", ret);
4484
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     ok(PeekMessageA( &msg, 0, 0, 0, PM_REMOVE ), "PeekMessage should succeed\n");
4488     ok(msg.message == WM_USER, "got %04x instead of WM_USER\n", msg.message);
4489 }
4490
4491 /* test if we receive the right sequence of messages */
4492 static void test_messages(void)
4493 {
4494     HWND hwnd, hparent, hchild;
4495     HWND hchild2, hbutton;
4496     HMENU hmenu;
4497     MSG msg;
4498     LRESULT res;
4499
4500     flush_sequence();
4501
4502     hwnd = CreateWindowExA(0, "TestWindowClass", "Test overlapped", WS_OVERLAPPEDWINDOW,
4503                            100, 100, 200, 200, 0, 0, 0, NULL);
4504     ok (hwnd != 0, "Failed to create overlapped window\n");
4505     ok_sequence(WmCreateOverlappedSeq, "CreateWindow:overlapped", FALSE);
4506
4507     /* test ShowWindow(SW_HIDE) on a newly created invisible window */
4508     ok( ShowWindow(hwnd, SW_HIDE) == FALSE, "ShowWindow: window was visible\n" );
4509     ok_sequence(WmEmptySeq, "ShowWindow(SW_HIDE):overlapped, invisible", FALSE);
4510
4511     /* test WM_SETREDRAW on a not visible top level window */
4512     test_WM_SETREDRAW(hwnd);
4513
4514     SetWindowPos(hwnd, 0,0,0,0,0, SWP_SHOWWINDOW|SWP_NOSIZE|SWP_NOMOVE);
4515     flush_events();
4516     ok_sequence(WmSWP_ShowOverlappedSeq, "SetWindowPos:SWP_SHOWWINDOW:overlapped", FALSE);
4517     ok(IsWindowVisible(hwnd), "window should be visible at this point\n");
4518
4519     ok(GetActiveWindow() == hwnd, "window should be active\n");
4520     ok(GetFocus() == hwnd, "window should have input focus\n");
4521     ShowWindow(hwnd, SW_HIDE);
4522     flush_events();
4523     ok_sequence(WmHideOverlappedSeq, "ShowWindow(SW_HIDE):overlapped", FALSE);
4524
4525     ShowWindow(hwnd, SW_SHOW);
4526     flush_events();
4527     ok_sequence(WmShowOverlappedSeq, "ShowWindow(SW_SHOW):overlapped", TRUE);
4528
4529     ShowWindow(hwnd, SW_HIDE);
4530     flush_events();
4531     ok_sequence(WmHideOverlappedSeq, "ShowWindow(SW_HIDE):overlapped", FALSE);
4532
4533     ShowWindow(hwnd, SW_SHOWMAXIMIZED);
4534     flush_events();
4535     ok_sequence(WmShowMaxOverlappedSeq, "ShowWindow(SW_SHOWMAXIMIZED):overlapped", TRUE);
4536     flush_sequence();
4537
4538     if (GetWindowLongW( hwnd, GWL_STYLE ) & WS_MAXIMIZE)
4539     {
4540         ShowWindow(hwnd, SW_RESTORE);
4541         flush_events();
4542         ok_sequence(WmShowRestoreMaxOverlappedSeq, "ShowWindow(SW_RESTORE):overlapped", TRUE);
4543         flush_sequence();
4544     }
4545
4546     ShowWindow(hwnd, SW_MINIMIZE);
4547     flush_events();
4548     ok_sequence(WmShowMinOverlappedSeq, "ShowWindow(SW_SHOWMINIMIZED):overlapped", TRUE);
4549     flush_sequence();
4550
4551     if (GetWindowLongW( hwnd, GWL_STYLE ) & WS_MINIMIZE)
4552     {
4553         ShowWindow(hwnd, SW_RESTORE);
4554         flush_events();
4555         ok_sequence(WmShowRestoreMinOverlappedSeq, "ShowWindow(SW_RESTORE):overlapped", TRUE);
4556         flush_sequence();
4557     }
4558
4559     ShowWindow(hwnd, SW_SHOW);
4560     flush_events();
4561     ok_sequence(WmOptionalPaint, "ShowWindow(SW_SHOW):overlapped already visible", FALSE);
4562
4563     SetWindowPos(hwnd, 0,0,0,0,0, SWP_HIDEWINDOW|SWP_NOSIZE|SWP_NOMOVE);
4564     ok_sequence(WmSWP_HideOverlappedSeq, "SetWindowPos:SWP_HIDEWINDOW:overlapped", FALSE);
4565     ok(!IsWindowVisible(hwnd), "window should not be visible at this point\n");
4566     ok(GetActiveWindow() == hwnd, "window should still be active\n");
4567
4568     /* test WM_SETREDRAW on a visible top level window */
4569     ShowWindow(hwnd, SW_SHOW);
4570     flush_events();
4571     test_WM_SETREDRAW(hwnd);
4572
4573     trace("testing scroll APIs on a visible top level window %p\n", hwnd);
4574     test_scroll_messages(hwnd);
4575
4576     /* test resizing and moving */
4577     SetWindowPos( hwnd, 0, 0, 0, 300, 300, SWP_NOMOVE|SWP_NOACTIVATE );
4578     ok_sequence(WmSWP_ResizeSeq, "SetWindowPos:Resize", FALSE );
4579     flush_events();
4580     flush_sequence();
4581     SetWindowPos( hwnd, 0, 200, 200, 0, 0, SWP_NOSIZE|SWP_NOACTIVATE );
4582     ok_sequence(WmSWP_MoveSeq, "SetWindowPos:Move", FALSE );
4583     flush_events();
4584     flush_sequence();
4585     SetWindowPos( hwnd, 0, 200, 200, 250, 250, SWP_NOZORDER|SWP_NOACTIVATE );
4586     ok_sequence(WmSWP_ResizeNoZOrder, "SetWindowPos:WmSWP_ResizeNoZOrder", FALSE );
4587     flush_events();
4588     flush_sequence();
4589
4590     /* popups don't get WM_GETMINMAXINFO */
4591     SetWindowLongW( hwnd, GWL_STYLE, WS_VISIBLE|WS_POPUP );
4592     SetWindowPos( hwnd, 0, 0, 0, 0, 0, SWP_NOZORDER|SWP_NOACTIVATE|SWP_NOSIZE|SWP_NOMOVE|SWP_FRAMECHANGED);
4593     flush_sequence();
4594     SetWindowPos( hwnd, 0, 0, 0, 200, 200, SWP_NOMOVE|SWP_NOACTIVATE );
4595     ok_sequence(WmSWP_ResizePopupSeq, "SetWindowPos:ResizePopup", FALSE );
4596
4597     DestroyWindow(hwnd);
4598     ok_sequence(WmDestroyOverlappedSeq, "DestroyWindow:overlapped", FALSE);
4599
4600     hparent = CreateWindowExA(0, "TestParentClass", "Test parent", WS_OVERLAPPEDWINDOW | WS_VISIBLE,
4601                               100, 100, 200, 200, 0, 0, 0, NULL);
4602     ok (hparent != 0, "Failed to create parent window\n");
4603     flush_sequence();
4604
4605     hchild = CreateWindowExA(0, "TestWindowClass", "Test child", WS_CHILD | WS_MAXIMIZE,
4606                              0, 0, 10, 10, hparent, 0, 0, NULL);
4607     ok (hchild != 0, "Failed to create child window\n");
4608     ok_sequence(WmCreateMaximizedChildSeq, "CreateWindow:maximized child", FALSE);
4609     DestroyWindow(hchild);
4610     flush_sequence();
4611
4612     /* visible child window with a caption */
4613     hchild = CreateWindowExA(0, "TestWindowClass", "Test child",
4614                              WS_CHILD | WS_VISIBLE | WS_CAPTION,
4615                              0, 0, 10, 10, hparent, 0, 0, NULL);
4616     ok (hchild != 0, "Failed to create child window\n");
4617     ok_sequence(WmCreateVisibleChildSeq, "CreateWindow:visible child", FALSE);
4618
4619     trace("testing scroll APIs on a visible child window %p\n", hchild);
4620     test_scroll_messages(hchild);
4621
4622     SetWindowPos(hchild, 0,0,0,0,0, SWP_SHOWWINDOW|SWP_NOSIZE|SWP_NOMOVE);
4623     ok_sequence(WmShowChildSeq_4, "SetWindowPos(SWP_SHOWWINDOW):child with a caption", FALSE);
4624
4625     DestroyWindow(hchild);
4626     flush_sequence();
4627
4628     hchild = CreateWindowExA(0, "TestWindowClass", "Test child", WS_CHILD,
4629                              0, 0, 10, 10, hparent, 0, 0, NULL);
4630     ok (hchild != 0, "Failed to create child window\n");
4631     ok_sequence(WmCreateChildSeq, "CreateWindow:child", FALSE);
4632     
4633     hchild2 = CreateWindowExA(0, "SimpleWindowClass", "Test child2", WS_CHILD,
4634                                100, 100, 50, 50, hparent, 0, 0, NULL);
4635     ok (hchild2 != 0, "Failed to create child2 window\n");
4636     flush_sequence();
4637
4638     hbutton = CreateWindowExA(0, "TestWindowClass", "Test button", WS_CHILD,
4639                               0, 100, 50, 50, hchild, 0, 0, NULL);
4640     ok (hbutton != 0, "Failed to create button window\n");
4641
4642     /* test WM_SETREDRAW on a not visible child window */
4643     test_WM_SETREDRAW(hchild);
4644
4645     ShowWindow(hchild, SW_SHOW);
4646     ok_sequence(WmShowChildSeq, "ShowWindow(SW_SHOW):child", FALSE);
4647
4648     /* check parent messages too */
4649     log_all_parent_messages++;
4650     ShowWindow(hchild, SW_HIDE);
4651     ok_sequence(WmHideChildSeq2, "ShowWindow(SW_HIDE):child", FALSE);
4652     log_all_parent_messages--;
4653
4654     ShowWindow(hchild, SW_SHOW);
4655     ok_sequence(WmShowChildSeq, "ShowWindow(SW_SHOW):child", FALSE);
4656
4657     ShowWindow(hchild, SW_HIDE);
4658     ok_sequence(WmHideChildSeq, "ShowWindow(SW_HIDE):child", FALSE);
4659
4660     ShowWindow(hchild, SW_SHOW);
4661     ok_sequence(WmShowChildSeq, "ShowWindow(SW_SHOW):child", FALSE);
4662
4663     /* test WM_SETREDRAW on a visible child window */
4664     test_WM_SETREDRAW(hchild);
4665
4666     log_all_parent_messages++;
4667     MoveWindow(hchild, 10, 10, 20, 20, TRUE);
4668     ok_sequence(WmResizingChildWithMoveWindowSeq, "MoveWindow:child", FALSE);
4669     log_all_parent_messages--;
4670
4671     ShowWindow(hchild, SW_HIDE);
4672     flush_sequence();
4673     SetWindowPos(hchild, 0,0,0,0,0, SWP_SHOWWINDOW|SWP_NOSIZE|SWP_NOMOVE);
4674     ok_sequence(WmShowChildSeq_2, "SetWindowPos:show_child_2", FALSE);
4675
4676     ShowWindow(hchild, SW_HIDE);
4677     flush_sequence();
4678     SetWindowPos(hchild, 0,0,0,0,0, SWP_SHOWWINDOW|SWP_NOSIZE|SWP_NOMOVE|SWP_NOACTIVATE);
4679     ok_sequence(WmShowChildSeq_3, "SetWindowPos:show_child_3", FALSE);
4680
4681     /* DestroyWindow sequence below expects that a child has focus */
4682     SetFocus(hchild);
4683     flush_sequence();
4684
4685     DestroyWindow(hchild);
4686     ok_sequence(WmDestroyChildSeq, "DestroyWindow:child", FALSE);
4687     DestroyWindow(hchild2);
4688     DestroyWindow(hbutton);
4689
4690     flush_sequence();
4691     hchild = CreateWindowExA(0, "TestWindowClass", "Test Child Popup", WS_CHILD | WS_POPUP,
4692                              0, 0, 100, 100, hparent, 0, 0, NULL);
4693     ok (hchild != 0, "Failed to create child popup window\n");
4694     ok_sequence(WmCreateChildPopupSeq, "CreateWindow:child_popup", FALSE);
4695     DestroyWindow(hchild);
4696
4697     /* test what happens to a window which sets WS_VISIBLE in WM_CREATE */
4698     flush_sequence();
4699     hchild = CreateWindowExA(0, "TestPopupClass", "Test Popup", WS_POPUP,
4700                              0, 0, 100, 100, hparent, 0, 0, NULL);
4701     ok (hchild != 0, "Failed to create popup window\n");
4702     ok_sequence(WmCreateInvisiblePopupSeq, "CreateWindow:invisible_popup", FALSE);
4703     ok(GetWindowLongA(hchild, GWL_STYLE) & WS_VISIBLE, "WS_VISIBLE should be set\n");
4704     ok(IsWindowVisible(hchild), "IsWindowVisible() should return TRUE\n");
4705     flush_sequence();
4706     ShowWindow(hchild, SW_SHOW);
4707     ok_sequence(WmEmptySeq, "ShowWindow:show_visible_popup", FALSE);
4708     flush_sequence();
4709     SetWindowPos(hchild, 0,0,0,0,0, SWP_SHOWWINDOW|SWP_NOSIZE|SWP_NOMOVE|SWP_NOACTIVATE|SWP_NOZORDER);
4710     ok_sequence(WmShowVisiblePopupSeq_2, "SetWindowPos:show_visible_popup_2", FALSE);
4711     flush_sequence();
4712     SetWindowPos(hchild, 0,0,0,0,0, SWP_SHOWWINDOW|SWP_NOSIZE|SWP_NOMOVE);
4713     ok_sequence(WmShowVisiblePopupSeq_3, "SetWindowPos:show_visible_popup_3", FALSE);
4714     DestroyWindow(hchild);
4715
4716     /* this time add WS_VISIBLE for CreateWindowEx, but this fact actually
4717      * changes nothing in message sequences.
4718      */
4719     flush_sequence();
4720     hchild = CreateWindowExA(0, "TestPopupClass", "Test Popup", WS_POPUP | WS_VISIBLE,
4721                              0, 0, 100, 100, hparent, 0, 0, NULL);
4722     ok (hchild != 0, "Failed to create popup window\n");
4723     ok_sequence(WmCreateInvisiblePopupSeq, "CreateWindow:invisible_popup", FALSE);
4724     ok(GetWindowLongA(hchild, GWL_STYLE) & WS_VISIBLE, "WS_VISIBLE should be set\n");
4725     ok(IsWindowVisible(hchild), "IsWindowVisible() should return TRUE\n");
4726     flush_sequence();
4727     ShowWindow(hchild, SW_SHOW);
4728     ok_sequence(WmEmptySeq, "ShowWindow:show_visible_popup", FALSE);
4729     flush_sequence();
4730     SetWindowPos(hchild, 0,0,0,0,0, SWP_SHOWWINDOW|SWP_NOSIZE|SWP_NOMOVE|SWP_NOACTIVATE|SWP_NOZORDER);
4731     ok_sequence(WmShowVisiblePopupSeq_2, "SetWindowPos:show_visible_popup_2", FALSE);
4732     DestroyWindow(hchild);
4733
4734     flush_sequence();
4735     hwnd = CreateWindowExA(WS_EX_DLGMODALFRAME, "TestDialogClass", NULL, WS_VISIBLE|WS_CAPTION|WS_SYSMENU|WS_DLGFRAME,
4736                            0, 0, 100, 100, hparent, 0, 0, NULL);
4737     ok(hwnd != 0, "Failed to create custom dialog window\n");
4738     ok_sequence(WmCreateCustomDialogSeq, "CreateCustomDialog", TRUE);
4739
4740     if(0) {
4741     trace("testing scroll APIs on a visible dialog %p\n", hwnd);
4742     test_scroll_messages(hwnd);
4743     }
4744
4745     flush_sequence();
4746
4747     test_def_id = 1;
4748     SendMessage(hwnd, WM_NULL, 0, 0);
4749
4750     flush_sequence();
4751     after_end_dialog = 1;
4752     EndDialog( hwnd, 0 );
4753     ok_sequence(WmEndCustomDialogSeq, "EndCustomDialog", FALSE);
4754
4755     DestroyWindow(hwnd);
4756     after_end_dialog = 0;
4757     test_def_id = 0;
4758
4759     hwnd = CreateWindowExA(0, "TestDialogClass", NULL, WS_POPUP,
4760                            0, 0, 100, 100, 0, 0, GetModuleHandleA(0), NULL);
4761     ok(hwnd != 0, "Failed to create custom dialog window\n");
4762     flush_sequence();
4763     trace("call ShowWindow(%p, SW_SHOW)\n", hwnd);
4764     ShowWindow(hwnd, SW_SHOW);
4765     ok_sequence(WmShowCustomDialogSeq, "ShowCustomDialog", TRUE);
4766     DestroyWindow(hwnd);
4767
4768     flush_sequence();
4769     DialogBoxA( 0, "TEST_DIALOG", hparent, TestModalDlgProcA );
4770     ok_sequence(WmModalDialogSeq, "ModalDialog", TRUE);
4771
4772     DestroyWindow(hparent);
4773     flush_sequence();
4774
4775     /* Message sequence for SetMenu */
4776     ok(!DrawMenuBar(hwnd), "DrawMenuBar should return FALSE for a window without a menu\n");
4777     ok_sequence(WmEmptySeq, "DrawMenuBar for a window without a menu", FALSE);
4778
4779     hmenu = CreateMenu();
4780     ok (hmenu != 0, "Failed to create menu\n");
4781     ok (InsertMenuA(hmenu, -1, MF_BYPOSITION, 0x1000, "foo"), "InsertMenu failed\n");
4782     hwnd = CreateWindowExA(0, "TestWindowClass", "Test overlapped", WS_OVERLAPPEDWINDOW,
4783                            100, 100, 200, 200, 0, hmenu, 0, NULL);
4784     ok_sequence(WmCreateOverlappedSeq, "CreateWindow:overlapped", FALSE);
4785     ok (SetMenu(hwnd, 0), "SetMenu\n");
4786     ok_sequence(WmSetMenuNonVisibleSizeChangeSeq, "SetMenu:NonVisibleSizeChange", FALSE);
4787     ok (SetMenu(hwnd, 0), "SetMenu\n");
4788     ok_sequence(WmSetMenuNonVisibleNoSizeChangeSeq, "SetMenu:NonVisibleNoSizeChange", FALSE);
4789     ShowWindow(hwnd, SW_SHOW);
4790     UpdateWindow( hwnd );
4791     flush_events();
4792     flush_sequence();
4793     ok (SetMenu(hwnd, 0), "SetMenu\n");
4794     ok_sequence(WmSetMenuVisibleNoSizeChangeSeq, "SetMenu:VisibleNoSizeChange", FALSE);
4795     ok (SetMenu(hwnd, hmenu), "SetMenu\n");
4796     ok_sequence(WmSetMenuVisibleSizeChangeSeq, "SetMenu:VisibleSizeChange", FALSE);
4797
4798     UpdateWindow( hwnd );
4799     flush_events();
4800     flush_sequence();
4801     ok(DrawMenuBar(hwnd), "DrawMenuBar\n");
4802     flush_events();
4803     ok_sequence(WmDrawMenuBarSeq, "DrawMenuBar", FALSE);
4804
4805     DestroyWindow(hwnd);
4806     flush_sequence();
4807
4808     /* Message sequence for EnableWindow */
4809     hparent = CreateWindowExA(0, "TestWindowClass", "Test parent", WS_OVERLAPPEDWINDOW | WS_VISIBLE,
4810                               100, 100, 200, 200, 0, 0, 0, NULL);
4811     ok (hparent != 0, "Failed to create parent window\n");
4812     hchild = CreateWindowExA(0, "TestWindowClass", "Test child", WS_CHILD | WS_VISIBLE,
4813                              0, 0, 10, 10, hparent, 0, 0, NULL);
4814     ok (hchild != 0, "Failed to create child window\n");
4815
4816     SetFocus(hchild);
4817     flush_events();
4818     flush_sequence();
4819
4820     EnableWindow(hparent, FALSE);
4821     ok_sequence(WmEnableWindowSeq_1, "EnableWindow(FALSE)", FALSE);
4822
4823     EnableWindow(hparent, TRUE);
4824     ok_sequence(WmEnableWindowSeq_2, "EnableWindow(TRUE)", FALSE);
4825
4826     flush_events();
4827     flush_sequence();
4828
4829     test_MsgWaitForMultipleObjects(hparent);
4830
4831     /* the following test causes an exception in user.exe under win9x */
4832     if (!PostMessageW( hparent, WM_USER, 0, 0 ))
4833     {
4834         DestroyWindow(hparent);
4835         flush_sequence();
4836         return;
4837     }
4838     PostMessageW( hparent, WM_USER+1, 0, 0 );
4839     /* PeekMessage(NULL) fails, but still removes the message */
4840     SetLastError(0xdeadbeef);
4841     ok( !PeekMessageW( NULL, 0, 0, 0, PM_REMOVE ), "PeekMessage(NULL) should fail\n" );
4842     ok( GetLastError() == ERROR_NOACCESS || /* Win2k */
4843         GetLastError() == 0xdeadbeef, /* NT4 */
4844         "last error is %d\n", GetLastError() );
4845     ok( PeekMessageW( &msg, 0, 0, 0, PM_REMOVE ), "PeekMessage should succeed\n" );
4846     ok( msg.message == WM_USER+1, "got %x instead of WM_USER+1\n", msg.message );
4847
4848     DestroyWindow(hchild);
4849     DestroyWindow(hparent);
4850     flush_sequence();
4851
4852     /* Message sequences for WM_SETICON */
4853     trace("testing WM_SETICON\n");
4854     hwnd = CreateWindowExA(0, "TestWindowClass", NULL, WS_OVERLAPPEDWINDOW,
4855                            CW_USEDEFAULT, CW_USEDEFAULT, 300, 300, 0,
4856                            NULL, NULL, 0);
4857     ShowWindow(hwnd, SW_SHOW);
4858     UpdateWindow(hwnd);
4859     flush_events();
4860     flush_sequence();
4861     SendMessage(hwnd, WM_SETICON, ICON_SMALL, (LPARAM)LoadIcon(0, IDI_APPLICATION));
4862     ok_sequence(WmSetIcon_1, "WM_SETICON for shown window with caption", FALSE);
4863
4864     ShowWindow(hwnd, SW_HIDE);
4865     flush_events();
4866     flush_sequence();
4867     SendMessage(hwnd, WM_SETICON, ICON_SMALL, (LPARAM)LoadIcon(0, IDI_APPLICATION));
4868     ok_sequence(WmSetIcon_2, "WM_SETICON for hidden window with caption", FALSE);
4869     DestroyWindow(hwnd);
4870     flush_sequence();
4871
4872     hwnd = CreateWindowExA(0, "TestPopupClass", NULL, WS_POPUP,
4873                            CW_USEDEFAULT, CW_USEDEFAULT, 300, 300, 0,
4874                            NULL, NULL, 0);
4875     ShowWindow(hwnd, SW_SHOW);
4876     UpdateWindow(hwnd);
4877     flush_events();
4878     flush_sequence();
4879     SendMessage(hwnd, WM_SETICON, ICON_SMALL, (LPARAM)LoadIcon(0, IDI_APPLICATION));
4880     ok_sequence(WmSetIcon_2, "WM_SETICON for shown window without caption", FALSE);
4881
4882     ShowWindow(hwnd, SW_HIDE);
4883     flush_events();
4884     flush_sequence();
4885     SendMessage(hwnd, WM_SETICON, ICON_SMALL, (LPARAM)LoadIcon(0, IDI_APPLICATION));
4886     ok_sequence(WmSetIcon_2, "WM_SETICON for hidden window without caption", FALSE);
4887
4888     flush_sequence();
4889     res = SendMessage(hwnd, 0x3B, 0x8000000b, 0);
4890     if (!res)
4891     {
4892         todo_wine win_skip( "Message 0x3b not supported\n" );
4893         goto done;
4894     }
4895     ok_sequence(WmInitEndSession, "Handling of undocumented 0x3B message by DefWindowProc wparam=0x8000000b", TRUE);
4896     ok(res == 1, "SendMessage(hwnd, 0x3B, 0x8000000b, 0) should have returned 1 instead of %ld\n", res);
4897     res = SendMessage(hwnd, 0x3B, 0x0000000b, 0);
4898     ok_sequence(WmInitEndSession_2, "Handling of undocumented 0x3B message by DefWindowProc wparam=0x0000000b", TRUE);
4899     ok(res == 1, "SendMessage(hwnd, 0x3B, 0x0000000b, 0) should have returned 1 instead of %ld\n", res);
4900     res = SendMessage(hwnd, 0x3B, 0x0000000f, 0);
4901     ok_sequence(WmInitEndSession_2, "Handling of undocumented 0x3B message by DefWindowProc wparam=0x0000000f", TRUE);
4902     ok(res == 1, "SendMessage(hwnd, 0x3B, 0x0000000f, 0) should have returned 1 instead of %ld\n", res);
4903
4904     flush_sequence();
4905     res = SendMessage(hwnd, 0x3B, 0x80000008, 0);
4906     ok_sequence(WmInitEndSession_3, "Handling of undocumented 0x3B message by DefWindowProc wparam=0x80000008", TRUE);
4907     ok(res == 2, "SendMessage(hwnd, 0x3B, 0x80000008, 0) should have returned 2 instead of %ld\n", res);
4908     res = SendMessage(hwnd, 0x3B, 0x00000008, 0);
4909     ok_sequence(WmInitEndSession_4, "Handling of undocumented 0x3B message by DefWindowProc wparam=0x00000008", TRUE);
4910     ok(res == 2, "SendMessage(hwnd, 0x3B, 0x00000008, 0) should have returned 2 instead of %ld\n", res);
4911
4912     res = SendMessage(hwnd, 0x3B, 0x80000004, 0);
4913     ok_sequence(WmInitEndSession_3, "Handling of undocumented 0x3B message by DefWindowProc wparam=0x80000004", TRUE);
4914     ok(res == 2, "SendMessage(hwnd, 0x3B, 0x80000004, 0) should have returned 2 instead of %ld\n", res);
4915
4916     res = SendMessage(hwnd, 0x3B, 0x80000001, 0);
4917     ok_sequence(WmInitEndSession_5, "Handling of undocumented 0x3B message by DefWindowProc wparam=0x80000001", TRUE);
4918     ok(res == 2, "SendMessage(hwnd, 0x3B, 0x80000001, 0) should have returned 2 instead of %ld\n", res);
4919
4920 done:
4921     DestroyWindow(hwnd);
4922     flush_sequence();
4923 }
4924
4925 static void test_setwindowpos(void)
4926 {
4927     HWND hwnd;
4928     RECT rc;
4929     LRESULT res;
4930     const INT winX = 100;
4931     const INT winY = 100;
4932     const INT sysX = GetSystemMetrics(SM_CXMINTRACK);
4933
4934     hwnd = CreateWindowExA(0, "TestWindowClass", NULL, 0,
4935                            0, 0, winX, winY, 0,
4936                            NULL, NULL, 0);
4937
4938     GetWindowRect(hwnd, &rc);
4939     expect(sysX, rc.right);
4940     expect(winY, rc.bottom);
4941
4942     flush_events();
4943     flush_sequence();
4944     res = SetWindowPos(hwnd, HWND_TOPMOST, 0, 0, winX, winY, 0);
4945     ok_sequence(WmZOrder, "Z-Order", TRUE);
4946     ok(res == TRUE, "SetWindowPos expected TRUE, got %ld\n", res);
4947
4948     GetWindowRect(hwnd, &rc);
4949     expect(sysX, rc.right);
4950     expect(winY, rc.bottom);
4951     DestroyWindow(hwnd);
4952 }
4953
4954 static void invisible_parent_tests(void)
4955 {
4956     HWND hparent, hchild;
4957
4958     hparent = CreateWindowExA(0, "TestParentClass", "Test parent", WS_OVERLAPPEDWINDOW,
4959                               100, 100, 200, 200, 0, 0, 0, NULL);
4960     ok (hparent != 0, "Failed to create parent window\n");
4961     flush_sequence();
4962
4963     /* test showing child with hidden parent */
4964
4965     hchild = CreateWindowExA(0, "TestWindowClass", "Test child", WS_CHILD,
4966                              0, 0, 10, 10, hparent, 0, 0, NULL);
4967     ok (hchild != 0, "Failed to create child window\n");
4968     ok_sequence(WmCreateChildSeq, "CreateWindow:child", FALSE);
4969
4970     ShowWindow( hchild, SW_MINIMIZE );
4971     ok_sequence(WmShowChildInvisibleParentSeq_1, "ShowWindow(SW_MINIMIZE) child with invisible parent", FALSE);
4972     ok(GetWindowLongA(hchild, GWL_STYLE) & WS_VISIBLE, "WS_VISIBLE should be set\n");
4973     ok(!IsWindowVisible(hchild), "IsWindowVisible() should return FALSE\n");
4974
4975     /* repeat */
4976     flush_events();
4977     flush_sequence();
4978     ShowWindow( hchild, SW_MINIMIZE );
4979     ok_sequence(WmShowChildInvisibleParentSeq_1r, "ShowWindow(SW_MINIMIZE) child with invisible parent", FALSE);
4980
4981     DestroyWindow(hchild);
4982     hchild = CreateWindowExA(0, "TestWindowClass", "Test child", WS_CHILD,
4983                              0, 0, 10, 10, hparent, 0, 0, NULL);
4984     flush_sequence();
4985
4986     ShowWindow( hchild, SW_MAXIMIZE );
4987     ok_sequence(WmShowChildInvisibleParentSeq_2, "ShowWindow(SW_MAXIMIZE) child with invisible parent", FALSE);
4988     ok(GetWindowLongA(hchild, GWL_STYLE) & WS_VISIBLE, "WS_VISIBLE should be set\n");
4989     ok(!IsWindowVisible(hchild), "IsWindowVisible() should return FALSE\n");
4990
4991     /* repeat */
4992     flush_events();
4993     flush_sequence();
4994     ShowWindow( hchild, SW_MAXIMIZE );
4995     ok_sequence(WmShowChildInvisibleParentSeq_2r, "ShowWindow(SW_MAXIMIZE) child with invisible parent", FALSE);
4996
4997     DestroyWindow(hchild);
4998     hchild = CreateWindowExA(0, "TestWindowClass", "Test child", WS_CHILD,
4999                              0, 0, 10, 10, hparent, 0, 0, NULL);
5000     flush_sequence();
5001
5002     ShowWindow( hchild, SW_RESTORE );
5003     ok_sequence(WmShowChildInvisibleParentSeq_5, "ShowWindow(SW_RESTORE) child with invisible parent", FALSE);
5004     ok(GetWindowLongA(hchild, GWL_STYLE) & WS_VISIBLE, "WS_VISIBLE should be set\n");
5005     ok(!IsWindowVisible(hchild), "IsWindowVisible() should return FALSE\n");
5006
5007     DestroyWindow(hchild);
5008     hchild = CreateWindowExA(0, "TestWindowClass", "Test child", WS_CHILD,
5009                              0, 0, 10, 10, hparent, 0, 0, NULL);
5010     flush_sequence();
5011
5012     ShowWindow( hchild, SW_SHOWMINIMIZED );
5013     ok_sequence(WmShowChildInvisibleParentSeq_3, "ShowWindow(SW_SHOWMINIMIZED) child with invisible parent", FALSE);
5014     ok(GetWindowLongA(hchild, GWL_STYLE) & WS_VISIBLE, "WS_VISIBLE should be set\n");
5015     ok(!IsWindowVisible(hchild), "IsWindowVisible() should return FALSE\n");
5016
5017     /* repeat */
5018     flush_events();
5019     flush_sequence();
5020     ShowWindow( hchild, SW_SHOWMINIMIZED );
5021     ok_sequence(WmShowChildInvisibleParentSeq_3r, "ShowWindow(SW_SHOWMINIMIZED) child with invisible parent", FALSE);
5022
5023     DestroyWindow(hchild);
5024     hchild = CreateWindowExA(0, "TestWindowClass", "Test child", WS_CHILD,
5025                              0, 0, 10, 10, hparent, 0, 0, NULL);
5026     flush_sequence();
5027
5028     /* same as ShowWindow( hchild, SW_MAXIMIZE ); */
5029     ShowWindow( hchild, SW_SHOWMAXIMIZED );
5030     ok_sequence(WmShowChildInvisibleParentSeq_2, "ShowWindow(SW_SHOWMAXIMIZED) child with invisible parent", FALSE);
5031     ok(GetWindowLongA(hchild, GWL_STYLE) & WS_VISIBLE, "WS_VISIBLE should be set\n");
5032     ok(!IsWindowVisible(hchild), "IsWindowVisible() should return FALSE\n");
5033
5034     DestroyWindow(hchild);
5035     hchild = CreateWindowExA(0, "TestWindowClass", "Test child", WS_CHILD,
5036                              0, 0, 10, 10, hparent, 0, 0, NULL);
5037     flush_sequence();
5038
5039     ShowWindow( hchild, SW_SHOWMINNOACTIVE );
5040     ok_sequence(WmShowChildInvisibleParentSeq_4, "ShowWindow(SW_SHOWMINNOACTIVE) child with invisible parent", FALSE);
5041     ok(GetWindowLongA(hchild, GWL_STYLE) & WS_VISIBLE, "WS_VISIBLE should be set\n");
5042     ok(!IsWindowVisible(hchild), "IsWindowVisible() should return FALSE\n");
5043
5044     /* repeat */
5045     flush_events();
5046     flush_sequence();
5047     ShowWindow( hchild, SW_SHOWMINNOACTIVE );
5048     ok_sequence(WmShowChildInvisibleParentSeq_4r, "ShowWindow(SW_SHOWMINNOACTIVE) child with invisible parent", FALSE);
5049
5050     DestroyWindow(hchild);
5051     hchild = CreateWindowExA(0, "TestWindowClass", "Test child", WS_CHILD,
5052                              0, 0, 10, 10, hparent, 0, 0, NULL);
5053     flush_sequence();
5054
5055     /* FIXME: looks like XP SP2 doesn't know about SW_FORCEMINIMIZE at all */
5056     ShowWindow( hchild, SW_FORCEMINIMIZE );
5057     ok_sequence(WmEmptySeq, "ShowWindow(SW_FORCEMINIMIZE) child with invisible parent", TRUE);
5058 todo_wine {
5059     ok(!(GetWindowLongA(hchild, GWL_STYLE) & WS_VISIBLE), "WS_VISIBLE should be not set\n");
5060 }
5061     ok(!IsWindowVisible(hchild), "IsWindowVisible() should return FALSE\n");
5062
5063     DestroyWindow(hchild);
5064     hchild = CreateWindowExA(0, "TestWindowClass", "Test child", WS_CHILD,
5065                              0, 0, 10, 10, hparent, 0, 0, NULL);
5066     flush_sequence();
5067
5068     ShowWindow( hchild, SW_SHOWNA );
5069     ok_sequence(WmShowChildInvisibleParentSeq_5, "ShowWindow(SW_SHOWNA) child with invisible parent", FALSE);
5070     ok(GetWindowLongA(hchild, GWL_STYLE) & WS_VISIBLE, "WS_VISIBLE should be set\n");
5071     ok(!IsWindowVisible(hchild), "IsWindowVisible() should return FALSE\n");
5072
5073     /* repeat */
5074     flush_events();
5075     flush_sequence();
5076     ShowWindow( hchild, SW_SHOWNA );
5077     ok_sequence(WmShowChildInvisibleParentSeq_5, "ShowWindow(SW_SHOWNA) child with invisible parent", FALSE);
5078
5079     DestroyWindow(hchild);
5080     hchild = CreateWindowExA(0, "TestWindowClass", "Test child", WS_CHILD,
5081                              0, 0, 10, 10, hparent, 0, 0, NULL);
5082     flush_sequence();
5083
5084     ShowWindow( hchild, SW_SHOW );
5085     ok_sequence(WmShowChildInvisibleParentSeq_5, "ShowWindow(SW_SHOW) child with invisible parent", FALSE);
5086     ok(GetWindowLongA(hchild, GWL_STYLE) & WS_VISIBLE, "WS_VISIBLE should be set\n");
5087     ok(!IsWindowVisible(hchild), "IsWindowVisible() should return FALSE\n");
5088
5089     /* repeat */
5090     flush_events();
5091     flush_sequence();
5092     ShowWindow( hchild, SW_SHOW );
5093     ok_sequence(WmEmptySeq, "ShowWindow(SW_SHOW) child with invisible parent", FALSE);
5094
5095     ShowWindow( hchild, SW_HIDE );
5096     ok_sequence(WmHideChildInvisibleParentSeq, "ShowWindow:hide child with invisible parent", FALSE);
5097     ok(!(GetWindowLongA(hchild, GWL_STYLE) & WS_VISIBLE), "WS_VISIBLE should be not set\n");
5098     ok(!IsWindowVisible(hchild), "IsWindowVisible() should return FALSE\n");
5099
5100     SetWindowPos(hchild, 0,0,0,0,0, SWP_SHOWWINDOW|SWP_NOSIZE|SWP_NOMOVE|SWP_NOACTIVATE|SWP_NOZORDER);
5101     ok_sequence(WmShowChildInvisibleParentSeq_6, "SetWindowPos:show child with invisible parent", FALSE);
5102     ok(GetWindowLongA(hchild, GWL_STYLE) & WS_VISIBLE, "WS_VISIBLE should be set\n");
5103     ok(!IsWindowVisible(hchild), "IsWindowVisible() should return FALSE\n");
5104
5105     SetWindowPos(hchild, 0,0,0,0,0, SWP_HIDEWINDOW|SWP_NOSIZE|SWP_NOMOVE|SWP_NOACTIVATE|SWP_NOZORDER);
5106     ok_sequence(WmHideChildInvisibleParentSeq_2, "SetWindowPos:hide child with invisible parent", FALSE);
5107     ok(!(GetWindowLongA(hchild, GWL_STYLE) & WS_VISIBLE), "WS_VISIBLE should not be set\n");
5108     ok(!IsWindowVisible(hchild), "IsWindowVisible() should return FALSE\n");
5109
5110     SetWindowPos(hchild, 0,0,0,0,0, SWP_SHOWWINDOW|SWP_NOSIZE|SWP_NOMOVE|SWP_NOACTIVATE|SWP_NOZORDER);
5111     flush_sequence();
5112     DestroyWindow(hchild);
5113     ok_sequence(WmDestroyInvisibleChildSeq, "DestroyInvisibleChildSeq", FALSE);
5114
5115     DestroyWindow(hparent);
5116     flush_sequence();
5117 }
5118
5119 /****************** button message test *************************/
5120 #define ID_BUTTON 0x000e
5121
5122 static const struct message WmSetFocusButtonSeq[] =
5123 {
5124     { HCBT_SETFOCUS, hook },
5125     { WM_IME_SETCONTEXT, sent|wparam|optional, 1 },
5126     { WM_IME_NOTIFY, sent|wparam|defwinproc|optional, 2 },
5127     { EVENT_OBJECT_FOCUS, winevent_hook|wparam|lparam, OBJID_CLIENT, 0 },
5128     { WM_SETFOCUS, sent|wparam, 0 },
5129     { WM_CTLCOLORBTN, sent|parent },
5130     { WM_COMMAND, sent|wparam|parent, MAKEWPARAM(ID_BUTTON, BN_SETFOCUS) },
5131     { WM_APP, sent|wparam|lparam, 0, 0 },
5132     { 0 }
5133 };
5134 static const struct message WmKillFocusButtonSeq[] =
5135 {
5136     { HCBT_SETFOCUS, hook },
5137     { EVENT_OBJECT_FOCUS, winevent_hook|wparam|lparam, OBJID_CLIENT, 0 },
5138     { WM_KILLFOCUS, sent|wparam, 0 },
5139     { WM_CTLCOLORBTN, sent|parent },
5140     { WM_COMMAND, sent|wparam|parent, MAKEWPARAM(ID_BUTTON, BN_KILLFOCUS) },
5141     { WM_IME_SETCONTEXT, sent|wparam|optional, 0 },
5142     { WM_IME_NOTIFY, sent|wparam|defwinproc|optional, 1 },
5143     { WM_APP, sent|wparam|lparam, 0, 0 },
5144     { WM_PAINT, sent },
5145     { WM_CTLCOLORBTN, sent|parent },
5146     { 0 }
5147 };
5148 static const struct message WmSetFocusStaticSeq[] =
5149 {
5150     { HCBT_SETFOCUS, hook },
5151     { WM_IME_SETCONTEXT, sent|wparam|optional, 1 },
5152     { WM_IME_NOTIFY, sent|wparam|defwinproc|optional, 2 },
5153     { EVENT_OBJECT_FOCUS, winevent_hook|wparam|lparam, OBJID_CLIENT, 0 },
5154     { WM_SETFOCUS, sent|wparam, 0 },
5155     { WM_CTLCOLORSTATIC, sent|parent },
5156     { WM_COMMAND, sent|wparam|parent, MAKEWPARAM(ID_BUTTON, BN_SETFOCUS) },
5157     { WM_COMMAND, sent|wparam|parent|optional, MAKEWPARAM(ID_BUTTON, BN_CLICKED) }, /* radio button */
5158     { WM_APP, sent|wparam|lparam, 0, 0 },
5159     { 0 }
5160 };
5161 static const struct message WmKillFocusStaticSeq[] =
5162 {
5163     { HCBT_SETFOCUS, hook },
5164     { EVENT_OBJECT_FOCUS, winevent_hook|wparam|lparam, OBJID_CLIENT, 0 },
5165     { WM_KILLFOCUS, sent|wparam, 0 },
5166     { WM_CTLCOLORSTATIC, sent|parent },
5167     { WM_COMMAND, sent|wparam|parent, MAKEWPARAM(ID_BUTTON, BN_KILLFOCUS) },
5168     { WM_IME_SETCONTEXT, sent|wparam|optional, 0 },
5169     { WM_IME_NOTIFY, sent|wparam|defwinproc|optional, 1 },
5170     { WM_APP, sent|wparam|lparam, 0, 0 },
5171     { WM_PAINT, sent },
5172     { WM_CTLCOLORSTATIC, sent|parent },
5173     { 0 }
5174 };
5175 static const struct message WmSetFocusOwnerdrawSeq[] =
5176 {
5177     { HCBT_SETFOCUS, hook },
5178     { WM_IME_SETCONTEXT, sent|wparam|optional, 1 },
5179     { WM_IME_NOTIFY, sent|wparam|defwinproc|optional, 2 },
5180     { EVENT_OBJECT_FOCUS, winevent_hook|wparam|lparam, OBJID_CLIENT, 0 },
5181     { WM_SETFOCUS, sent|wparam, 0 },
5182     { WM_CTLCOLORBTN, sent|parent },
5183     { WM_DRAWITEM, sent|wparam|lparam|parent, ID_BUTTON, 0x001040e4 },
5184     { WM_COMMAND, sent|wparam|parent, MAKEWPARAM(ID_BUTTON, BN_SETFOCUS) },
5185     { WM_APP, sent|wparam|lparam, 0, 0 },
5186     { 0 }
5187 };
5188 static const struct message WmKillFocusOwnerdrawSeq[] =
5189 {
5190     { HCBT_SETFOCUS, hook },
5191     { EVENT_OBJECT_FOCUS, winevent_hook|wparam|lparam, OBJID_CLIENT, 0 },
5192     { WM_KILLFOCUS, sent|wparam, 0 },
5193     { WM_CTLCOLORBTN, sent|parent },
5194     { WM_DRAWITEM, sent|wparam|lparam|parent, ID_BUTTON, 0x000040e4 },
5195     { WM_COMMAND, sent|wparam|parent, MAKEWPARAM(ID_BUTTON, BN_KILLFOCUS) },
5196     { WM_IME_SETCONTEXT, sent|wparam|optional, 0 },
5197     { WM_IME_NOTIFY, sent|wparam|defwinproc|optional, 1 },
5198     { WM_APP, sent|wparam|lparam, 0, 0 },
5199     { WM_PAINT, sent },
5200     { WM_CTLCOLORBTN, sent|parent },
5201     { WM_DRAWITEM, sent|wparam|lparam|parent, ID_BUTTON, 0x000010e4 },
5202     { 0 }
5203 };
5204 static const struct message WmLButtonDownSeq[] =
5205 {
5206     { WM_LBUTTONDOWN, sent|wparam|lparam, 0, 0 },
5207     { EVENT_SYSTEM_CAPTURESTART, winevent_hook|wparam|lparam, 0, 0 },
5208     { HCBT_SETFOCUS, hook },
5209     { WM_IME_SETCONTEXT, sent|wparam|defwinproc|optional, 1 },
5210     { WM_IME_NOTIFY, sent|wparam|defwinproc|optional, 2 },
5211     { EVENT_OBJECT_FOCUS, winevent_hook|wparam|lparam, OBJID_CLIENT, 0 },
5212     { WM_SETFOCUS, sent|wparam|defwinproc, 0 },
5213     { WM_CTLCOLORBTN, sent|defwinproc },
5214     { BM_SETSTATE, sent|wparam|defwinproc, TRUE },
5215     { WM_CTLCOLORBTN, sent|defwinproc },
5216     { EVENT_OBJECT_STATECHANGE, winevent_hook|wparam|lparam, OBJID_CLIENT, 0 },
5217     { 0 }
5218 };
5219 static const struct message WmLButtonUpSeq[] =
5220 {
5221     { WM_LBUTTONUP, sent|wparam|lparam, 0, 0 },
5222     { BM_SETSTATE, sent|wparam|defwinproc, FALSE },
5223     { WM_CTLCOLORBTN, sent|defwinproc },
5224     { EVENT_OBJECT_STATECHANGE, winevent_hook|wparam|lparam, OBJID_CLIENT, 0 },
5225     { EVENT_SYSTEM_CAPTUREEND, winevent_hook|wparam|lparam, 0, 0 },
5226     { WM_CAPTURECHANGED, sent|wparam|defwinproc, 0 },
5227     { 0 }
5228 };
5229 static const struct message WmSetFontButtonSeq[] =
5230 {
5231     { WM_SETFONT, sent },
5232     { WM_PAINT, sent },
5233     { WM_ERASEBKGND, sent|defwinproc|optional },
5234     { WM_CTLCOLORBTN, sent|defwinproc },
5235     { 0 }
5236 };
5237 static const struct message WmSetStyleButtonSeq[] =
5238 {
5239     { BM_SETSTYLE, sent },
5240     { WM_APP, sent|wparam|lparam, 0, 0 },
5241     { WM_PAINT, sent },
5242     { WM_NCPAINT, sent|defwinproc|optional }, /* FIXME: Wine sends it */
5243     { WM_ERASEBKGND, sent|defwinproc|optional }, /* Win9x doesn't send it */
5244     { WM_CTLCOLORBTN, sent|parent },
5245     { 0 }
5246 };
5247 static const struct message WmSetStyleStaticSeq[] =
5248 {
5249     { BM_SETSTYLE, sent },
5250     { WM_APP, sent|wparam|lparam, 0, 0 },
5251     { WM_PAINT, sent },
5252     { WM_NCPAINT, sent|defwinproc|optional }, /* FIXME: Wine sends it */
5253     { WM_ERASEBKGND, sent|defwinproc|optional }, /* Win9x doesn't send it */
5254     { WM_CTLCOLORSTATIC, sent|parent },
5255     { 0 }
5256 };
5257 static const struct message WmSetStyleUserSeq[] =
5258 {
5259     { BM_SETSTYLE, sent },
5260     { WM_APP, sent|wparam|lparam, 0, 0 },
5261     { WM_PAINT, sent },
5262     { WM_NCPAINT, sent|defwinproc|optional }, /* FIXME: Wine sends it */
5263     { WM_ERASEBKGND, sent|defwinproc|optional }, /* Win9x doesn't send it */
5264     { WM_CTLCOLORBTN, sent|parent },
5265     { WM_COMMAND, sent|wparam|parent, MAKEWPARAM(ID_BUTTON, BN_PAINT) },
5266     { 0 }
5267 };
5268 static const struct message WmSetStyleOwnerdrawSeq[] =
5269 {
5270     { BM_SETSTYLE, sent },
5271     { WM_APP, sent|wparam|lparam, 0, 0 },
5272     { WM_PAINT, sent },
5273     { WM_NCPAINT, sent|optional }, /* FIXME: Wine sends it */
5274     { WM_ERASEBKGND, sent|defwinproc|optional }, /* Win9x doesn't send it */
5275     { WM_CTLCOLORBTN, sent|parent },
5276     { WM_CTLCOLORBTN, sent|parent|optional }, /* Win9x doesn't send it */
5277     { WM_DRAWITEM, sent|wparam|lparam|parent, ID_BUTTON, 0x000010e4 },
5278     { 0 }
5279 };
5280 static const struct message WmSetStateButtonSeq[] =
5281 {
5282     { BM_SETSTATE, sent },
5283     { WM_CTLCOLORBTN, sent|parent },
5284     { WM_APP, sent|wparam|lparam, 0, 0 },
5285     { 0 }
5286 };
5287 static const struct message WmSetStateStaticSeq[] =
5288 {
5289     { BM_SETSTATE, sent },
5290     { WM_CTLCOLORSTATIC, sent|parent },
5291     { WM_APP, sent|wparam|lparam, 0, 0 },
5292     { 0 }
5293 };
5294 static const struct message WmSetStateUserSeq[] =
5295 {
5296     { BM_SETSTATE, sent },
5297     { WM_CTLCOLORBTN, sent|parent },
5298     { WM_COMMAND, sent|wparam|parent, MAKEWPARAM(ID_BUTTON, BN_HILITE) },
5299     { WM_APP, sent|wparam|lparam, 0, 0 },
5300     { 0 }
5301 };
5302 static const struct message WmSetStateOwnerdrawSeq[] =
5303 {
5304     { BM_SETSTATE, sent },
5305     { WM_CTLCOLORBTN, sent|parent },
5306     { WM_DRAWITEM, sent|wparam|lparam|parent, ID_BUTTON, 0x000120e4 },
5307     { WM_APP, sent|wparam|lparam, 0, 0 },
5308     { 0 }
5309 };
5310 static const struct message WmClearStateButtonSeq[] =
5311 {
5312     { BM_SETSTATE, sent },
5313     { WM_CTLCOLORBTN, sent|parent },
5314     { WM_COMMAND, sent|wparam|parent, MAKEWPARAM(ID_BUTTON, BN_UNHILITE) },
5315     { WM_APP, sent|wparam|lparam, 0, 0 },
5316     { 0 }
5317 };
5318 static const struct message WmClearStateOwnerdrawSeq[] =
5319 {
5320     { BM_SETSTATE, sent },
5321     { WM_CTLCOLORBTN, sent|parent },
5322     { WM_DRAWITEM, sent|wparam|lparam|parent, ID_BUTTON, 0x000020e4 },
5323     { WM_APP, sent|wparam|lparam, 0, 0 },
5324     { 0 }
5325 };
5326 static const struct message WmSetCheckIgnoredSeq[] =
5327 {
5328     { BM_SETCHECK, sent },
5329     { WM_APP, sent|wparam|lparam, 0, 0 },
5330     { 0 }
5331 };
5332 static const struct message WmSetCheckStaticSeq[] =
5333 {
5334     { BM_SETCHECK, sent },
5335     { WM_CTLCOLORSTATIC, sent|parent },
5336     { WM_APP, sent|wparam|lparam, 0, 0 },
5337     { 0 }
5338 };
5339
5340 static WNDPROC old_button_proc;
5341
5342 static LRESULT CALLBACK button_hook_proc(HWND hwnd, UINT message, WPARAM wParam, LPARAM lParam)
5343 {
5344     static LONG defwndproc_counter = 0;
5345     LRESULT ret;
5346     struct recvd_message msg;
5347
5348     if (ignore_message( message )) return 0;
5349
5350     switch (message)
5351     {
5352     case WM_SYNCPAINT:
5353         break;
5354     case BM_SETSTATE:
5355         if (GetCapture())
5356             ok(GetCapture() == hwnd, "GetCapture() = %p\n", GetCapture());
5357         /* fall through */
5358     default:
5359         msg.hwnd = hwnd;
5360         msg.message = message;
5361         msg.flags = sent|wparam|lparam;
5362         if (defwndproc_counter) msg.flags |= defwinproc;
5363         msg.wParam = wParam;
5364         msg.lParam = lParam;
5365         msg.descr = "button";
5366         add_message(&msg);
5367     }
5368
5369     defwndproc_counter++;
5370     ret = CallWindowProcA(old_button_proc, hwnd, message, wParam, lParam);
5371     defwndproc_counter--;
5372
5373     return ret;
5374 }
5375
5376 static void subclass_button(void)
5377 {
5378     WNDCLASSA cls;
5379
5380     if (!GetClassInfoA(0, "button", &cls)) assert(0);
5381
5382     old_button_proc = cls.lpfnWndProc;
5383
5384     cls.hInstance = GetModuleHandle(0);
5385     cls.lpfnWndProc = button_hook_proc;
5386     cls.lpszClassName = "my_button_class";
5387     UnregisterClass(cls.lpszClassName, cls.hInstance);
5388     if (!RegisterClassA(&cls)) assert(0);
5389 }
5390
5391 static void test_button_messages(void)
5392 {
5393     static const struct
5394     {
5395         DWORD style;
5396         DWORD dlg_code;
5397         const struct message *setfocus;
5398         const struct message *killfocus;
5399         const struct message *setstyle;
5400         const struct message *setstate;
5401         const struct message *clearstate;
5402         const struct message *setcheck;
5403     } button[] = {
5404         { BS_PUSHBUTTON, DLGC_BUTTON | DLGC_UNDEFPUSHBUTTON,
5405           WmSetFocusButtonSeq, WmKillFocusButtonSeq, WmSetStyleButtonSeq,
5406           WmSetStateButtonSeq, WmSetStateButtonSeq, WmSetCheckIgnoredSeq },
5407         { BS_DEFPUSHBUTTON, DLGC_BUTTON | DLGC_DEFPUSHBUTTON,
5408           WmSetFocusButtonSeq, WmKillFocusButtonSeq, WmSetStyleButtonSeq,
5409           WmSetStateButtonSeq, WmSetStateButtonSeq, WmSetCheckIgnoredSeq },
5410         { BS_CHECKBOX, DLGC_BUTTON,
5411           WmSetFocusStaticSeq, WmKillFocusStaticSeq, WmSetStyleStaticSeq,
5412           WmSetStateStaticSeq, WmSetStateStaticSeq, WmSetCheckStaticSeq },
5413         { BS_AUTOCHECKBOX, DLGC_BUTTON,
5414           WmSetFocusStaticSeq, WmKillFocusStaticSeq, WmSetStyleStaticSeq,
5415           WmSetStateStaticSeq, WmSetStateStaticSeq, WmSetCheckStaticSeq },
5416         { BS_RADIOBUTTON, DLGC_BUTTON | DLGC_RADIOBUTTON,
5417           WmSetFocusStaticSeq, WmKillFocusStaticSeq, WmSetStyleStaticSeq,
5418           WmSetStateStaticSeq, WmSetStateStaticSeq, WmSetCheckStaticSeq },
5419         { BS_3STATE, DLGC_BUTTON,
5420           WmSetFocusStaticSeq, WmKillFocusStaticSeq, WmSetStyleStaticSeq,
5421           WmSetStateStaticSeq, WmSetStateStaticSeq, WmSetCheckStaticSeq },
5422         { BS_AUTO3STATE, DLGC_BUTTON,
5423           WmSetFocusStaticSeq, WmKillFocusStaticSeq, WmSetStyleStaticSeq,
5424           WmSetStateStaticSeq, WmSetStateStaticSeq, WmSetCheckStaticSeq },
5425         { BS_GROUPBOX, DLGC_STATIC,
5426           WmSetFocusStaticSeq, WmKillFocusStaticSeq, WmSetStyleStaticSeq,
5427           WmSetStateStaticSeq, WmSetStateStaticSeq, WmSetCheckIgnoredSeq },
5428         { BS_USERBUTTON, DLGC_BUTTON | DLGC_UNDEFPUSHBUTTON,
5429           WmSetFocusButtonSeq, WmKillFocusButtonSeq, WmSetStyleUserSeq,
5430           WmSetStateUserSeq, WmClearStateButtonSeq, WmSetCheckIgnoredSeq },
5431         { BS_AUTORADIOBUTTON, DLGC_BUTTON | DLGC_RADIOBUTTON,
5432           WmSetFocusStaticSeq, WmKillFocusStaticSeq, WmSetStyleStaticSeq,
5433           WmSetStateStaticSeq, WmSetStateStaticSeq, WmSetCheckStaticSeq },
5434         { BS_OWNERDRAW, DLGC_BUTTON,
5435           WmSetFocusOwnerdrawSeq, WmKillFocusOwnerdrawSeq, WmSetStyleOwnerdrawSeq,
5436           WmSetStateOwnerdrawSeq, WmClearStateOwnerdrawSeq, WmSetCheckIgnoredSeq },
5437     };
5438     unsigned int i;
5439     HWND hwnd, parent;
5440     DWORD dlg_code;
5441     HFONT zfont;
5442
5443     /* selection with VK_SPACE should capture button window */
5444     hwnd = CreateWindowExA(0, "button", "test", BS_CHECKBOX | WS_VISIBLE | WS_POPUP,
5445                            0, 0, 50, 14, 0, 0, 0, NULL);
5446     ok(hwnd != 0, "Failed to create button window\n");
5447     ReleaseCapture();
5448     SetFocus(hwnd);
5449     SendMessageA(hwnd, WM_KEYDOWN, VK_SPACE, 0);
5450     ok(GetCapture() == hwnd, "Should be captured on VK_SPACE WM_KEYDOWN\n");
5451     SendMessageA(hwnd, WM_KEYUP, VK_SPACE, 0);
5452     DestroyWindow(hwnd);
5453
5454     subclass_button();
5455
5456     parent = CreateWindowExA(0, "TestParentClass", "Test parent", WS_OVERLAPPEDWINDOW | WS_VISIBLE,
5457                              100, 100, 200, 200, 0, 0, 0, NULL);
5458     ok(parent != 0, "Failed to create parent window\n");
5459
5460     for (i = 0; i < sizeof(button)/sizeof(button[0]); i++)
5461     {
5462         MSG msg;
5463         DWORD style, state;
5464
5465         trace("button style %08x\n", button[i].style);
5466
5467         hwnd = CreateWindowExA(0, "my_button_class", "test", button[i].style | WS_CHILD | BS_NOTIFY,
5468                                0, 0, 50, 14, parent, (HMENU)ID_BUTTON, 0, NULL);
5469         ok(hwnd != 0, "Failed to create button window\n");
5470
5471         style = GetWindowLongA(hwnd, GWL_STYLE);
5472         style &= ~(WS_CHILD | BS_NOTIFY);
5473         /* XP turns a BS_USERBUTTON into BS_PUSHBUTTON */
5474         if (button[i].style == BS_USERBUTTON)
5475             ok(style == BS_PUSHBUTTON, "expected style BS_PUSHBUTTON got %x\n", style);
5476         else
5477             ok(style == button[i].style, "expected style %x got %x\n", button[i].style, style);
5478
5479         dlg_code = SendMessageA(hwnd, WM_GETDLGCODE, 0, 0);
5480         ok(dlg_code == button[i].dlg_code, "%u: wrong dlg_code %08x\n", i, dlg_code);
5481
5482         ShowWindow(hwnd, SW_SHOW);
5483         UpdateWindow(hwnd);
5484         SetFocus(0);
5485         flush_events();
5486         SetFocus(0);
5487         flush_sequence();
5488
5489         log_all_parent_messages++;
5490
5491         ok(GetFocus() == 0, "expected focus 0, got %p\n", GetFocus());
5492         SetFocus(hwnd);
5493         SendMessage(hwnd, WM_APP, 0, 0); /* place a separator mark here */
5494         while (PeekMessage(&msg, 0, 0, 0, PM_REMOVE)) DispatchMessage(&msg);
5495         ok_sequence(button[i].setfocus, "SetFocus(hwnd) on a button", FALSE);
5496
5497         SetFocus(0);
5498         SendMessage(hwnd, WM_APP, 0, 0); /* place a separator mark here */
5499         while (PeekMessage(&msg, 0, 0, 0, PM_REMOVE)) DispatchMessage(&msg);
5500         ok_sequence(button[i].killfocus, "SetFocus(0) on a button", FALSE);
5501
5502         ok(GetFocus() == 0, "expected focus 0, got %p\n", GetFocus());
5503
5504         SendMessage(hwnd, BM_SETSTYLE, button[i].style | BS_BOTTOM, TRUE);
5505         SendMessage(hwnd, WM_APP, 0, 0); /* place a separator mark here */
5506         while (PeekMessage(&msg, 0, 0, 0, PM_REMOVE)) DispatchMessage(&msg);
5507         ok_sequence(button[i].setstyle, "BM_SETSTYLE on a button", FALSE);
5508
5509         style = GetWindowLongA(hwnd, GWL_STYLE);
5510         style &= ~(WS_VISIBLE | WS_CHILD | BS_NOTIFY);
5511         /* XP doesn't turn a BS_USERBUTTON into BS_PUSHBUTTON here! */
5512         ok(style == button[i].style, "expected style %04x got %04x\n", button[i].style, style);
5513
5514         state = SendMessage(hwnd, BM_GETSTATE, 0, 0);
5515         ok(state == 0, "expected state 0, got %04x\n", state);
5516
5517         flush_sequence();
5518
5519         SendMessage(hwnd, BM_SETSTATE, TRUE, 0);
5520         SendMessage(hwnd, WM_APP, 0, 0); /* place a separator mark here */
5521         while (PeekMessage(&msg, 0, 0, 0, PM_REMOVE)) DispatchMessage(&msg);
5522         ok_sequence(button[i].setstate, "BM_SETSTATE/TRUE on a button", FALSE);
5523
5524         state = SendMessage(hwnd, BM_GETSTATE, 0, 0);
5525         ok(state == 0x0004, "expected state 0x0004, got %04x\n", state);
5526
5527         style = GetWindowLongA(hwnd, GWL_STYLE);
5528         style &= ~(WS_CHILD | BS_NOTIFY | WS_VISIBLE);
5529         ok(style == button[i].style, "expected style %04x got %04x\n", button[i].style, style);
5530
5531         flush_sequence();
5532
5533         SendMessage(hwnd, BM_SETSTATE, FALSE, 0);
5534         SendMessage(hwnd, WM_APP, 0, 0); /* place a separator mark here */
5535         while (PeekMessage(&msg, 0, 0, 0, PM_REMOVE)) DispatchMessage(&msg);
5536         ok_sequence(button[i].clearstate, "BM_SETSTATE/FALSE on a button", FALSE);
5537
5538         state = SendMessage(hwnd, BM_GETSTATE, 0, 0);
5539         ok(state == 0, "expected state 0, got %04x\n", state);
5540
5541         style = GetWindowLongA(hwnd, GWL_STYLE);
5542         style &= ~(WS_CHILD | BS_NOTIFY | WS_VISIBLE);
5543         ok(style == button[i].style, "expected style %04x got %04x\n", button[i].style, style);
5544
5545         state = SendMessage(hwnd, BM_GETCHECK, 0, 0);
5546         ok(state == BST_UNCHECKED, "expected BST_UNCHECKED, got %04x\n", state);
5547
5548         flush_sequence();
5549
5550         SendMessage(hwnd, BM_SETCHECK, BST_UNCHECKED, 0);
5551         SendMessage(hwnd, WM_APP, 0, 0); /* place a separator mark here */
5552         while (PeekMessage(&msg, 0, 0, 0, PM_REMOVE)) DispatchMessage(&msg);
5553         ok_sequence(WmSetCheckIgnoredSeq, "BM_SETCHECK on a button", FALSE);
5554
5555         state = SendMessage(hwnd, BM_GETCHECK, 0, 0);
5556         ok(state == BST_UNCHECKED, "expected BST_UNCHECKED, got %04x\n", state);
5557
5558         style = GetWindowLongA(hwnd, GWL_STYLE);
5559         style &= ~(WS_CHILD | BS_NOTIFY | WS_VISIBLE);
5560         ok(style == button[i].style, "expected style %04x got %04x\n", button[i].style, style);
5561
5562         flush_sequence();
5563
5564         SendMessage(hwnd, BM_SETCHECK, BST_CHECKED, 0);
5565         SendMessage(hwnd, WM_APP, 0, 0); /* place a separator mark here */
5566         while (PeekMessage(&msg, 0, 0, 0, PM_REMOVE)) DispatchMessage(&msg);
5567         ok_sequence(button[i].setcheck, "BM_SETCHECK on a button", FALSE);
5568
5569         state = SendMessage(hwnd, BM_GETCHECK, 0, 0);
5570         if (button[i].style == BS_PUSHBUTTON ||
5571             button[i].style == BS_DEFPUSHBUTTON ||
5572             button[i].style == BS_GROUPBOX ||
5573             button[i].style == BS_USERBUTTON ||
5574             button[i].style == BS_OWNERDRAW)
5575             ok(state == BST_UNCHECKED, "expected check 0, got %04x\n", state);
5576         else
5577             ok(state == BST_CHECKED, "expected check 1, got %04x\n", state);
5578
5579         style = GetWindowLongA(hwnd, GWL_STYLE);
5580         style &= ~(WS_CHILD | BS_NOTIFY | WS_VISIBLE);
5581         if (button[i].style == BS_RADIOBUTTON ||
5582             button[i].style == BS_AUTORADIOBUTTON)
5583             ok(style == (button[i].style | WS_TABSTOP), "expected style %04x | WS_TABSTOP got %04x\n", button[i].style, style);
5584         else
5585             ok(style == button[i].style, "expected style %04x got %04x\n", button[i].style, style);
5586
5587         log_all_parent_messages--;
5588
5589         DestroyWindow(hwnd);
5590     }
5591
5592     DestroyWindow(parent);
5593
5594     hwnd = CreateWindowExA(0, "my_button_class", "test", BS_PUSHBUTTON | WS_POPUP | WS_VISIBLE,
5595                            0, 0, 50, 14, 0, 0, 0, NULL);
5596     ok(hwnd != 0, "Failed to create button window\n");
5597
5598     SetForegroundWindow(hwnd);
5599     flush_events();
5600
5601     SetActiveWindow(hwnd);
5602     SetFocus(0);
5603     flush_sequence();
5604
5605     SendMessageA(hwnd, WM_LBUTTONDOWN, 0, 0);
5606     ok_sequence(WmLButtonDownSeq, "WM_LBUTTONDOWN on a button", FALSE);
5607
5608     SendMessageA(hwnd, WM_LBUTTONUP, 0, 0);
5609     ok_sequence(WmLButtonUpSeq, "WM_LBUTTONUP on a button", FALSE);
5610
5611     flush_sequence();
5612     zfont = GetStockObject(SYSTEM_FONT);
5613     SendMessageA(hwnd, WM_SETFONT, (WPARAM)zfont, TRUE);
5614     UpdateWindow(hwnd);
5615     ok_sequence(WmSetFontButtonSeq, "WM_SETFONT on a button", FALSE);
5616
5617     DestroyWindow(hwnd);
5618 }
5619
5620 /****************** static message test *************************/
5621 static const struct message WmSetFontStaticSeq[] =
5622 {
5623     { WM_SETFONT, sent },
5624     { WM_PAINT, sent|defwinproc|optional },
5625     { WM_ERASEBKGND, sent|defwinproc|optional },
5626     { WM_CTLCOLORSTATIC, sent|defwinproc|optional },
5627     { 0 }
5628 };
5629
5630 static WNDPROC old_static_proc;
5631
5632 static LRESULT CALLBACK static_hook_proc(HWND hwnd, UINT message, WPARAM wParam, LPARAM lParam)
5633 {
5634     static LONG defwndproc_counter = 0;
5635     LRESULT ret;
5636     struct recvd_message msg;
5637
5638     if (ignore_message( message )) return 0;
5639
5640     msg.hwnd = hwnd;
5641     msg.message = message;
5642     msg.flags = sent|wparam|lparam;
5643     if (defwndproc_counter) msg.flags |= defwinproc;
5644     msg.wParam = wParam;
5645     msg.lParam = lParam;
5646     msg.descr = "static";
5647     add_message(&msg);
5648
5649     defwndproc_counter++;
5650     ret = CallWindowProcA(old_static_proc, hwnd, message, wParam, lParam);
5651     defwndproc_counter--;
5652
5653     return ret;
5654 }
5655
5656 static void subclass_static(void)
5657 {
5658     WNDCLASSA cls;
5659
5660     if (!GetClassInfoA(0, "static", &cls)) assert(0);
5661
5662     old_static_proc = cls.lpfnWndProc;
5663
5664     cls.hInstance = GetModuleHandle(0);
5665     cls.lpfnWndProc = static_hook_proc;
5666     cls.lpszClassName = "my_static_class";
5667     UnregisterClass(cls.lpszClassName, cls.hInstance);
5668     if (!RegisterClassA(&cls)) assert(0);
5669 }
5670
5671 static void test_static_messages(void)
5672 {
5673     /* FIXME: make as comprehensive as the button message test */
5674     static const struct
5675     {
5676         DWORD style;
5677         DWORD dlg_code;
5678         const struct message *setfont;
5679     } static_ctrl[] = {
5680         { SS_LEFT, DLGC_STATIC,
5681           WmSetFontStaticSeq }
5682     };
5683     unsigned int i;
5684     HWND hwnd;
5685     DWORD dlg_code;
5686
5687     subclass_static();
5688
5689     for (i = 0; i < sizeof(static_ctrl)/sizeof(static_ctrl[0]); i++)
5690     {
5691         hwnd = CreateWindowExA(0, "my_static_class", "test", static_ctrl[i].style | WS_POPUP,
5692                                0, 0, 50, 14, 0, 0, 0, NULL);
5693         ok(hwnd != 0, "Failed to create static window\n");
5694
5695         dlg_code = SendMessageA(hwnd, WM_GETDLGCODE, 0, 0);
5696         ok(dlg_code == static_ctrl[i].dlg_code, "%u: wrong dlg_code %08x\n", i, dlg_code);
5697
5698         ShowWindow(hwnd, SW_SHOW);
5699         UpdateWindow(hwnd);
5700         SetFocus(0);
5701         flush_sequence();
5702
5703         trace("static style %08x\n", static_ctrl[i].style);
5704         SendMessage(hwnd, WM_SETFONT, (WPARAM)GetStockObject(DEFAULT_GUI_FONT), TRUE);
5705         ok_sequence(static_ctrl[i].setfont, "WM_SETFONT on a static", FALSE);
5706
5707         DestroyWindow(hwnd);
5708     }
5709 }
5710
5711 /****************** ComboBox message test *************************/
5712 #define ID_COMBOBOX 0x000f
5713
5714 static const struct message WmKeyDownComboSeq[] =
5715 {
5716     { WM_KEYDOWN, sent|wparam|lparam, VK_DOWN, 0 },
5717     { WM_COMMAND, sent|wparam|defwinproc, MAKEWPARAM(1000, LBN_SELCHANGE) },
5718     { WM_COMMAND, sent|wparam|parent, MAKEWPARAM(ID_COMBOBOX, CBN_SELENDOK) },
5719     { WM_COMMAND, sent|wparam|parent, MAKEWPARAM(ID_COMBOBOX, CBN_SELCHANGE) },
5720     { WM_CTLCOLOREDIT, sent|parent },
5721     { WM_KEYUP, sent|wparam|lparam, VK_DOWN, 0 },
5722     { 0 }
5723 };
5724
5725 static const struct message WmSetPosComboSeq[] =
5726 {
5727     { WM_WINDOWPOSCHANGING, sent },
5728     { WM_NCCALCSIZE, sent|wparam, TRUE },
5729     { WM_CHILDACTIVATE, sent },
5730     { WM_WINDOWPOSCHANGED, sent },
5731     { WM_MOVE, sent|defwinproc },
5732     { WM_SIZE, sent|defwinproc|wparam, SIZE_RESTORED },
5733     { WM_WINDOWPOSCHANGING, sent|defwinproc },
5734     { WM_NCCALCSIZE, sent|defwinproc|wparam, TRUE },
5735     { WM_WINDOWPOSCHANGED, sent|defwinproc },
5736     { WM_SIZE, sent|defwinproc|wparam, SIZE_RESTORED },
5737     { 0 }
5738 };
5739
5740 static WNDPROC old_combobox_proc;
5741
5742 static LRESULT CALLBACK combobox_hook_proc(HWND hwnd, UINT message, WPARAM wParam, LPARAM lParam)
5743 {
5744     static LONG defwndproc_counter = 0;
5745     LRESULT ret;
5746     struct recvd_message msg;
5747
5748     /* do not log painting messages */
5749     if (message != WM_PAINT &&
5750         message != WM_NCPAINT &&
5751         message != WM_SYNCPAINT &&
5752         message != WM_ERASEBKGND &&
5753         message != WM_NCHITTEST &&
5754         message != WM_GETTEXT &&
5755         !ignore_message( message ))
5756     {
5757         msg.hwnd = hwnd;
5758         msg.message = message;
5759         msg.flags = sent|wparam|lparam;
5760         if (defwndproc_counter) msg.flags |= defwinproc;
5761         msg.wParam = wParam;
5762         msg.lParam = lParam;
5763         msg.descr = "combo";
5764         add_message(&msg);
5765     }
5766
5767     defwndproc_counter++;
5768     ret = CallWindowProcA(old_combobox_proc, hwnd, message, wParam, lParam);
5769     defwndproc_counter--;
5770
5771     return ret;
5772 }
5773
5774 static void subclass_combobox(void)
5775 {
5776     WNDCLASSA cls;
5777
5778     if (!GetClassInfoA(0, "ComboBox", &cls)) assert(0);
5779
5780     old_combobox_proc = cls.lpfnWndProc;
5781
5782     cls.hInstance = GetModuleHandle(0);
5783     cls.lpfnWndProc = combobox_hook_proc;
5784     cls.lpszClassName = "my_combobox_class";
5785     UnregisterClass(cls.lpszClassName, cls.hInstance);
5786     if (!RegisterClassA(&cls)) assert(0);
5787 }
5788
5789 static void test_combobox_messages(void)
5790 {
5791     HWND parent, combo;
5792     LRESULT ret;
5793
5794     subclass_combobox();
5795
5796     parent = CreateWindowExA(0, "TestParentClass", "Test parent", WS_OVERLAPPEDWINDOW | WS_VISIBLE,
5797                              100, 100, 200, 200, 0, 0, 0, NULL);
5798     ok(parent != 0, "Failed to create parent window\n");
5799     flush_sequence();
5800
5801     combo = CreateWindowEx(0, "my_combobox_class", "test", WS_CHILD | WS_VISIBLE | CBS_DROPDOWNLIST | CBS_HASSTRINGS,
5802                            0, 0, 100, 150, parent, (HMENU)ID_COMBOBOX, 0, NULL);
5803     ok(combo != 0, "Failed to create combobox window\n");
5804
5805     UpdateWindow(combo);
5806
5807     ret = SendMessage(combo, WM_GETDLGCODE, 0, 0);
5808     ok(ret == (DLGC_WANTCHARS | DLGC_WANTARROWS), "wrong dlg_code %08lx\n", ret);
5809
5810     ret = SendMessage(combo, CB_ADDSTRING, 0, (LPARAM)"item 0");
5811     ok(ret == 0, "expected 0, got %ld\n", ret);
5812     ret = SendMessage(combo, CB_ADDSTRING, 0, (LPARAM)"item 1");
5813     ok(ret == 1, "expected 1, got %ld\n", ret);
5814     ret = SendMessage(combo, CB_ADDSTRING, 0, (LPARAM)"item 2");
5815     ok(ret == 2, "expected 2, got %ld\n", ret);
5816
5817     SendMessage(combo, CB_SETCURSEL, 0, 0);
5818     SetFocus(combo);
5819     flush_sequence();
5820
5821     log_all_parent_messages++;
5822     SendMessage(combo, WM_KEYDOWN, VK_DOWN, 0);
5823     SendMessage(combo, WM_KEYUP, VK_DOWN, 0);
5824     log_all_parent_messages--;
5825     ok_sequence(WmKeyDownComboSeq, "WM_KEYDOWN/VK_DOWN on a ComboBox", FALSE);
5826
5827     flush_sequence();
5828     SetWindowPos(combo, 0, 10, 10, 120, 130, SWP_NOZORDER);
5829     ok_sequence(WmSetPosComboSeq, "repositioning messages on a ComboBox", FALSE);
5830
5831     DestroyWindow(combo);
5832     DestroyWindow(parent);
5833 }
5834
5835 /****************** WM_IME_KEYDOWN message test *******************/
5836
5837 static const struct message WmImeKeydownMsgSeq_0[] =
5838 {
5839     { WM_IME_KEYDOWN, wparam, VK_RETURN },
5840     { WM_CHAR, wparam, 'A' },
5841     { 0 }
5842 };
5843
5844 static const struct message WmImeKeydownMsgSeq_1[] =
5845 {
5846     { WM_KEYDOWN, optional|wparam, VK_RETURN },
5847     { WM_CHAR,    optional|wparam, VK_RETURN },
5848     { 0 }
5849 };
5850
5851 static LRESULT WINAPI wmime_keydown_procA(HWND hwnd, UINT message, WPARAM wParam, LPARAM lParam)
5852 {
5853     struct recvd_message msg;
5854
5855     msg.hwnd = hwnd;
5856     msg.message = message;
5857     msg.flags = wparam|lparam;
5858     msg.wParam = wParam;
5859     msg.lParam = lParam;
5860     msg.descr = "wmime_keydown";
5861     add_message(&msg);
5862
5863     return DefWindowProcA(hwnd, message, wParam, lParam);
5864 }
5865
5866 static void register_wmime_keydown_class(void)
5867 {
5868     WNDCLASSA cls;
5869
5870     ZeroMemory(&cls, sizeof(WNDCLASSA));
5871     cls.lpfnWndProc = wmime_keydown_procA;
5872     cls.hInstance = GetModuleHandleA(0);
5873     cls.lpszClassName = "wmime_keydown_class";
5874     if (!RegisterClassA(&cls)) assert(0);
5875 }
5876
5877 static void test_wmime_keydown_message(void)
5878 {
5879     HWND hwnd;
5880     MSG msg;
5881
5882     trace("Message sequences by WM_IME_KEYDOWN\n");
5883
5884     register_wmime_keydown_class();
5885     hwnd = CreateWindowExA(0, "wmime_keydown_class", NULL, WS_OVERLAPPEDWINDOW,
5886                            CW_USEDEFAULT, CW_USEDEFAULT, 300, 300, 0,
5887                            NULL, NULL, 0);
5888     flush_events();
5889     flush_sequence();
5890
5891     SendMessage(hwnd, WM_IME_KEYDOWN, VK_RETURN, 0x1c0001);
5892     SendMessage(hwnd, WM_CHAR, 'A', 1);
5893     ok_sequence(WmImeKeydownMsgSeq_0, "WM_IME_KEYDOWN 0", FALSE);
5894
5895     while ( PeekMessage(&msg, 0, 0, 0, PM_REMOVE) )
5896     {
5897         TranslateMessage(&msg);
5898         DispatchMessage(&msg);
5899     }
5900     ok_sequence(WmImeKeydownMsgSeq_1, "WM_IME_KEYDOWN 1", FALSE);
5901
5902     DestroyWindow(hwnd);
5903 }
5904
5905 /************* painting message test ********************/
5906
5907 void dump_region(HRGN hrgn)
5908 {
5909     DWORD i, size;
5910     RGNDATA *data = NULL;
5911     RECT *rect;
5912
5913     if (!hrgn)
5914     {
5915         printf( "null region\n" );
5916         return;
5917     }
5918     if (!(size = GetRegionData( hrgn, 0, NULL ))) return;
5919     if (!(data = HeapAlloc( GetProcessHeap(), 0, size ))) return;
5920     GetRegionData( hrgn, size, data );
5921     printf("%d rects:", data->rdh.nCount );
5922     for (i = 0, rect = (RECT *)data->Buffer; i < data->rdh.nCount; i++, rect++)
5923         printf( " (%d,%d)-(%d,%d)", rect->left, rect->top, rect->right, rect->bottom );
5924     printf("\n");
5925     HeapFree( GetProcessHeap(), 0, data );
5926 }
5927
5928 static void check_update_rgn( HWND hwnd, HRGN hrgn )
5929 {
5930     INT ret;
5931     RECT r1, r2;
5932     HRGN tmp = CreateRectRgn( 0, 0, 0, 0 );
5933     HRGN update = CreateRectRgn( 0, 0, 0, 0 );
5934
5935     ret = GetUpdateRgn( hwnd, update, FALSE );
5936     ok( ret != ERROR, "GetUpdateRgn failed\n" );
5937     if (ret == NULLREGION)
5938     {
5939         ok( !hrgn, "Update region shouldn't be empty\n" );
5940     }
5941     else
5942     {
5943         if (CombineRgn( tmp, hrgn, update, RGN_XOR ) != NULLREGION)
5944         {
5945             ok( 0, "Regions are different\n" );
5946             if (winetest_debug > 0)
5947             {
5948                 printf( "Update region: " );
5949                 dump_region( update );
5950                 printf( "Wanted region: " );
5951                 dump_region( hrgn );
5952             }
5953         }
5954     }
5955     GetRgnBox( update, &r1 );
5956     GetUpdateRect( hwnd, &r2, FALSE );
5957     ok( r1.left == r2.left && r1.top == r2.top && r1.right == r2.right && r1.bottom == r2.bottom,
5958         "Rectangles are different: %d,%d-%d,%d / %d,%d-%d,%d\n",
5959         r1.left, r1.top, r1.right, r1.bottom, r2.left, r2.top, r2.right, r2.bottom );
5960
5961     DeleteObject( tmp );
5962     DeleteObject( update );
5963 }
5964
5965 static const struct message WmInvalidateRgn[] = {
5966     { WM_NCPAINT, sent },
5967     { WM_GETTEXT, sent|defwinproc|optional },
5968     { 0 }
5969 };
5970
5971 static const struct message WmGetUpdateRect[] = {
5972     { WM_NCPAINT, sent },
5973     { WM_GETTEXT, sent|defwinproc|optional },
5974     { WM_PAINT, sent },
5975     { 0 }
5976 };
5977
5978 static const struct message WmInvalidateFull[] = {
5979     { WM_NCPAINT, sent|wparam, 1 },
5980     { WM_GETTEXT, sent|defwinproc|optional },
5981     { 0 }
5982 };
5983
5984 static const struct message WmInvalidateErase[] = {
5985     { WM_NCPAINT, sent|wparam, 1 },
5986     { WM_GETTEXT, sent|defwinproc|optional },
5987     { WM_ERASEBKGND, sent },
5988     { 0 }
5989 };
5990
5991 static const struct message WmInvalidatePaint[] = {
5992     { WM_PAINT, sent },
5993     { WM_NCPAINT, sent|wparam|beginpaint, 1 },
5994     { WM_GETTEXT, sent|beginpaint|defwinproc|optional },
5995     { 0 }
5996 };
5997
5998 static const struct message WmInvalidateErasePaint[] = {
5999     { WM_PAINT, sent },
6000     { WM_NCPAINT, sent|wparam|beginpaint, 1 },
6001     { WM_GETTEXT, sent|beginpaint|defwinproc|optional },
6002     { WM_ERASEBKGND, sent|beginpaint|optional },
6003     { 0 }
6004 };
6005
6006 static const struct message WmInvalidateErasePaint2[] = {
6007     { WM_PAINT, sent },
6008     { WM_NCPAINT, sent|beginpaint },
6009     { WM_GETTEXT, sent|beginpaint|defwinproc|optional },
6010     { WM_ERASEBKGND, sent|beginpaint|optional },
6011     { 0 }
6012 };
6013
6014 static const struct message WmErase[] = {
6015     { WM_ERASEBKGND, sent },
6016     { 0 }
6017 };
6018
6019 static const struct message WmPaint[] = {
6020     { WM_PAINT, sent },
6021     { 0 }
6022 };
6023
6024 static const struct message WmParentOnlyPaint[] = {
6025     { WM_PAINT, sent|parent },
6026     { 0 }
6027 };
6028
6029 static const struct message WmInvalidateParent[] = {
6030     { WM_NCPAINT, sent|parent },
6031     { WM_GETTEXT, sent|defwinproc|parent|optional },
6032     { WM_ERASEBKGND, sent|parent },
6033     { 0 }
6034 };
6035
6036 static const struct message WmInvalidateParentChild[] = {
6037     { WM_NCPAINT, sent|parent },
6038     { WM_GETTEXT, sent|defwinproc|parent|optional },
6039     { WM_ERASEBKGND, sent|parent },
6040     { WM_NCPAINT, sent },
6041     { WM_GETTEXT, sent|defwinproc|optional },
6042     { WM_ERASEBKGND, sent },
6043     { 0 }
6044 };
6045
6046 static const struct message WmInvalidateParentChild2[] = {
6047     { WM_ERASEBKGND, sent|parent },
6048     { WM_NCPAINT, sent },
6049     { WM_GETTEXT, sent|defwinproc|optional },
6050     { WM_ERASEBKGND, sent },
6051     { 0 }
6052 };
6053
6054 static const struct message WmParentPaint[] = {
6055     { WM_PAINT, sent|parent },
6056     { WM_PAINT, sent },
6057     { 0 }
6058 };
6059
6060 static const struct message WmParentPaintNc[] = {
6061     { WM_PAINT, sent|parent },
6062     { WM_PAINT, sent },
6063     { WM_NCPAINT, sent|beginpaint },
6064     { WM_GETTEXT, sent|beginpaint|defwinproc|optional },
6065     { WM_ERASEBKGND, sent|beginpaint|optional },
6066     { 0 }
6067 };
6068
6069 static const struct message WmChildPaintNc[] = {
6070     { WM_PAINT, sent },
6071     { WM_NCPAINT, sent|beginpaint },
6072     { WM_GETTEXT, sent|beginpaint|defwinproc|optional },
6073     { WM_ERASEBKGND, sent|beginpaint|optional },
6074     { 0 }
6075 };
6076
6077 static const struct message WmParentErasePaint[] = {
6078     { WM_PAINT, sent|parent },
6079     { WM_NCPAINT, sent|parent|beginpaint },
6080     { WM_GETTEXT, sent|parent|beginpaint|defwinproc|optional },
6081     { WM_ERASEBKGND, sent|parent|beginpaint|optional },
6082     { WM_PAINT, sent },
6083     { WM_NCPAINT, sent|beginpaint },
6084     { WM_GETTEXT, sent|beginpaint|defwinproc|optional },
6085     { WM_ERASEBKGND, sent|beginpaint|optional },
6086     { 0 }
6087 };
6088
6089 static const struct message WmParentOnlyNcPaint[] = {
6090     { WM_PAINT, sent|parent },
6091     { WM_NCPAINT, sent|parent|beginpaint },
6092     { WM_GETTEXT, sent|parent|beginpaint|defwinproc|optional },
6093     { 0 }
6094 };
6095
6096 static const struct message WmSetParentStyle[] = {
6097     { WM_STYLECHANGING, sent|parent },
6098     { WM_STYLECHANGED, sent|parent },
6099     { 0 }
6100 };
6101
6102 static void test_paint_messages(void)
6103 {
6104     BOOL ret;
6105     RECT rect;
6106     POINT pt;
6107     MSG msg;
6108     HWND hparent, hchild;
6109     HRGN hrgn = CreateRectRgn( 0, 0, 0, 0 );
6110     HRGN hrgn2 = CreateRectRgn( 0, 0, 0, 0 );
6111     HWND hwnd = CreateWindowExA(0, "TestWindowClass", "Test overlapped", WS_OVERLAPPEDWINDOW,
6112                                 100, 100, 200, 200, 0, 0, 0, NULL);
6113     ok (hwnd != 0, "Failed to create overlapped window\n");
6114
6115     ShowWindow( hwnd, SW_SHOW );
6116     UpdateWindow( hwnd );
6117     flush_events();
6118     flush_sequence();
6119
6120     check_update_rgn( hwnd, 0 );
6121     SetRectRgn( hrgn, 10, 10, 20, 20 );
6122     ret = RedrawWindow( hwnd, NULL, hrgn, RDW_INVALIDATE );
6123     ok(ret, "RedrawWindow returned %d instead of TRUE\n", ret);
6124     check_update_rgn( hwnd, hrgn );
6125     SetRectRgn( hrgn2, 20, 20, 30, 30 );
6126     ret = RedrawWindow( hwnd, NULL, hrgn2, RDW_INVALIDATE );
6127     ok(ret, "RedrawWindow returned %d instead of TRUE\n", ret);
6128     CombineRgn( hrgn, hrgn, hrgn2, RGN_OR );
6129     check_update_rgn( hwnd, hrgn );
6130     /* validate everything */
6131     ret = RedrawWindow( hwnd, NULL, NULL, RDW_VALIDATE );
6132     ok(ret, "RedrawWindow returned %d instead of TRUE\n", ret);
6133     check_update_rgn( hwnd, 0 );
6134
6135     /* test empty region */
6136     SetRectRgn( hrgn, 10, 10, 10, 15 );
6137     ret = RedrawWindow( hwnd, NULL, hrgn, RDW_INVALIDATE );
6138     ok(ret, "RedrawWindow returned %d instead of TRUE\n", ret);
6139     check_update_rgn( hwnd, 0 );
6140     /* test empty rect */
6141     SetRect( &rect, 10, 10, 10, 15 );
6142     ret = RedrawWindow( hwnd, &rect, NULL, RDW_INVALIDATE );
6143     ok(ret, "RedrawWindow returned %d instead of TRUE\n", ret);
6144     check_update_rgn( hwnd, 0 );
6145
6146     /* flush pending messages */
6147     flush_events();
6148     flush_sequence();
6149
6150     GetClientRect( hwnd, &rect );
6151     SetRectRgn( hrgn, 0, 0, rect.right - rect.left, rect.bottom - rect.top );
6152     /* MSDN: if hwnd parameter is NULL, InvalidateRect invalidates and redraws
6153      * all windows and sends WM_ERASEBKGND and WM_NCPAINT.
6154      */
6155     trace("testing InvalidateRect(0, NULL, FALSE)\n");
6156     SetRectEmpty( &rect );
6157     ok(InvalidateRect(0, &rect, FALSE), "InvalidateRect(0, &rc, FALSE) should fail\n");
6158     check_update_rgn( hwnd, hrgn );
6159     ok_sequence( WmInvalidateErase, "InvalidateErase", FALSE );
6160     flush_events();
6161     ok_sequence( WmPaint, "Paint", FALSE );
6162     RedrawWindow( hwnd, NULL, NULL, RDW_VALIDATE );
6163     check_update_rgn( hwnd, 0 );
6164
6165     /* MSDN: if hwnd parameter is NULL, ValidateRect invalidates and redraws
6166      * all windows and sends WM_ERASEBKGND and WM_NCPAINT.
6167      */
6168     trace("testing ValidateRect(0, NULL)\n");
6169     SetRectEmpty( &rect );
6170     if (ValidateRect(0, &rect))  /* not supported on Win9x */
6171     {
6172         check_update_rgn( hwnd, hrgn );
6173         ok_sequence( WmInvalidateErase, "InvalidateErase", FALSE );
6174         flush_events();
6175         ok_sequence( WmPaint, "Paint", FALSE );
6176         RedrawWindow( hwnd, NULL, NULL, RDW_VALIDATE );
6177         check_update_rgn( hwnd, 0 );
6178     }
6179
6180     trace("testing InvalidateRgn(0, NULL, FALSE)\n");
6181     SetLastError(0xdeadbeef);
6182     ok(!InvalidateRgn(0, NULL, FALSE), "InvalidateRgn(0, NULL, FALSE) should fail\n");
6183     ok(GetLastError() == ERROR_INVALID_WINDOW_HANDLE || GetLastError() == 0xdeadbeef,
6184        "wrong error code %d\n", GetLastError());
6185     check_update_rgn( hwnd, 0 );
6186     flush_events();
6187     ok_sequence( WmEmptySeq, "WmEmptySeq", FALSE );
6188
6189     trace("testing ValidateRgn(0, NULL)\n");
6190     SetLastError(0xdeadbeef);
6191     ok(!ValidateRgn(0, NULL), "ValidateRgn(0, NULL) should fail\n");
6192     ok(GetLastError() == ERROR_INVALID_WINDOW_HANDLE ||
6193        broken( GetLastError() == 0xdeadbeef ) /* win9x */,
6194        "wrong error code %d\n", GetLastError());
6195     check_update_rgn( hwnd, 0 );
6196     flush_events();
6197     ok_sequence( WmEmptySeq, "WmEmptySeq", FALSE );
6198
6199     trace("testing UpdateWindow(NULL)\n");
6200     SetLastError(0xdeadbeef);
6201     ok(!UpdateWindow(NULL), "UpdateWindow(NULL) should fail\n");
6202     ok(GetLastError() == ERROR_INVALID_WINDOW_HANDLE ||
6203        broken( GetLastError() == 0xdeadbeef ) /* win9x */,
6204        "wrong error code %d\n", GetLastError());
6205     check_update_rgn( hwnd, 0 );
6206     flush_events();
6207     ok_sequence( WmEmptySeq, "WmEmptySeq", FALSE );
6208
6209     /* now with frame */
6210     SetRectRgn( hrgn, -5, -5, 20, 20 );
6211
6212     /* flush pending messages */
6213     flush_events();
6214     flush_sequence();
6215     RedrawWindow( hwnd, NULL, hrgn, RDW_INVALIDATE | RDW_FRAME );
6216     ok_sequence( WmEmptySeq, "EmptySeq", FALSE );
6217
6218     SetRectRgn( hrgn, 0, 0, 20, 20 );  /* GetUpdateRgn clips to client area */
6219     check_update_rgn( hwnd, hrgn );
6220
6221     flush_sequence();
6222     RedrawWindow( hwnd, NULL, hrgn, RDW_INVALIDATE | RDW_FRAME | RDW_ERASENOW );
6223     ok_sequence( WmInvalidateRgn, "InvalidateRgn", FALSE );
6224
6225     flush_sequence();
6226     RedrawWindow( hwnd, NULL, NULL, RDW_INVALIDATE | RDW_FRAME | RDW_ERASENOW );
6227     ok_sequence( WmInvalidateFull, "InvalidateFull", FALSE );
6228
6229     GetClientRect( hwnd, &rect );
6230     SetRectRgn( hrgn, rect.left, rect.top, rect.right, rect.bottom );
6231     check_update_rgn( hwnd, hrgn );
6232
6233     flush_sequence();
6234     RedrawWindow( hwnd, NULL, NULL, RDW_INVALIDATE | RDW_FRAME | RDW_ERASE | RDW_ERASENOW );
6235     ok_sequence( WmInvalidateErase, "InvalidateErase", FALSE );
6236
6237     flush_sequence();
6238     RedrawWindow( hwnd, NULL, NULL, RDW_INVALIDATE | RDW_FRAME | RDW_ERASENOW | RDW_UPDATENOW );
6239     ok_sequence( WmInvalidatePaint, "InvalidatePaint", FALSE );
6240     check_update_rgn( hwnd, 0 );
6241
6242     flush_sequence();
6243     RedrawWindow( hwnd, NULL, NULL, RDW_INVALIDATE | RDW_FRAME | RDW_ERASE | RDW_UPDATENOW );
6244     ok_sequence( WmInvalidateErasePaint, "InvalidateErasePaint", FALSE );
6245     check_update_rgn( hwnd, 0 );
6246
6247     flush_sequence();
6248     SetRectRgn( hrgn, 0, 0, 100, 100 );
6249     RedrawWindow( hwnd, NULL, hrgn, RDW_INVALIDATE );
6250     SetRectRgn( hrgn, 0, 0, 50, 100 );
6251     RedrawWindow( hwnd, NULL, hrgn, RDW_VALIDATE );
6252     SetRectRgn( hrgn, 50, 0, 100, 100 );
6253     check_update_rgn( hwnd, hrgn );
6254     RedrawWindow( hwnd, NULL, hrgn, RDW_VALIDATE | RDW_ERASENOW );
6255     ok_sequence( WmEmptySeq, "EmptySeq", FALSE );  /* must not generate messages, everything is valid */
6256     check_update_rgn( hwnd, 0 );
6257
6258     flush_sequence();
6259     SetRectRgn( hrgn, 0, 0, 100, 100 );
6260     RedrawWindow( hwnd, NULL, hrgn, RDW_INVALIDATE | RDW_ERASE );
6261     SetRectRgn( hrgn, 0, 0, 100, 50 );
6262     RedrawWindow( hwnd, NULL, hrgn, RDW_VALIDATE | RDW_ERASENOW );
6263     ok_sequence( WmErase, "Erase", FALSE );
6264     SetRectRgn( hrgn, 0, 50, 100, 100 );
6265     check_update_rgn( hwnd, hrgn );
6266
6267     flush_sequence();
6268     SetRectRgn( hrgn, 0, 0, 100, 100 );
6269     RedrawWindow( hwnd, NULL, hrgn, RDW_INVALIDATE | RDW_ERASE );
6270     SetRectRgn( hrgn, 0, 0, 50, 50 );
6271     RedrawWindow( hwnd, NULL, hrgn, RDW_VALIDATE | RDW_NOERASE | RDW_UPDATENOW );
6272     ok_sequence( WmPaint, "Paint", FALSE );
6273
6274     flush_sequence();
6275     SetRectRgn( hrgn, -4, -4, -2, -2 );
6276     RedrawWindow( hwnd, NULL, hrgn, RDW_INVALIDATE | RDW_FRAME );
6277     SetRectRgn( hrgn, -200, -200, -198, -198 );
6278     RedrawWindow( hwnd, NULL, hrgn, RDW_VALIDATE | RDW_NOFRAME | RDW_ERASENOW );
6279     ok_sequence( WmEmptySeq, "EmptySeq", FALSE );
6280
6281     flush_sequence();
6282     SetRectRgn( hrgn, -4, -4, -2, -2 );
6283     RedrawWindow( hwnd, NULL, hrgn, RDW_INVALIDATE | RDW_FRAME );
6284     SetRectRgn( hrgn, -4, -4, -3, -3 );
6285     RedrawWindow( hwnd, NULL, hrgn, RDW_VALIDATE | RDW_NOFRAME );
6286     SetRectRgn( hrgn, 0, 0, 1, 1 );
6287     RedrawWindow( hwnd, NULL, hrgn, RDW_INVALIDATE | RDW_UPDATENOW );
6288     ok_sequence( WmPaint, "Paint", FALSE );
6289
6290     flush_sequence();
6291     SetRectRgn( hrgn, -4, -4, -1, -1 );
6292     RedrawWindow( hwnd, NULL, hrgn, RDW_INVALIDATE | RDW_FRAME );
6293     RedrawWindow( hwnd, NULL, 0, RDW_ERASENOW );
6294     /* make sure no WM_PAINT was generated */
6295     flush_events();
6296     ok_sequence( WmInvalidateRgn, "InvalidateRgn", FALSE );
6297
6298     flush_sequence();
6299     SetRectRgn( hrgn, -4, -4, -1, -1 );
6300     RedrawWindow( hwnd, NULL, hrgn, RDW_INVALIDATE | RDW_FRAME );
6301     while (PeekMessage( &msg, 0, 0, 0, PM_REMOVE ))
6302     {
6303         if (msg.hwnd == hwnd && msg.message == WM_PAINT)
6304         {
6305             /* GetUpdateRgn must return empty region since only nonclient area is invalidated */
6306             INT ret = GetUpdateRgn( hwnd, hrgn, FALSE );
6307             ok( ret == NULLREGION, "Invalid GetUpdateRgn result %d\n", ret );
6308             ret = GetUpdateRect( hwnd, &rect, FALSE );
6309             ok( ret, "Invalid GetUpdateRect result %d\n", ret );
6310             /* this will send WM_NCPAINT and validate the non client area */
6311             ret = GetUpdateRect( hwnd, &rect, TRUE );
6312             ok( !ret, "Invalid GetUpdateRect result %d\n", ret );
6313         }
6314         DispatchMessage( &msg );
6315     }
6316     ok_sequence( WmGetUpdateRect, "GetUpdateRect", FALSE );
6317
6318     DestroyWindow( hwnd );
6319
6320     /* now test with a child window */
6321
6322     hparent = CreateWindowExA(0, "TestParentClass", "Test parent", WS_OVERLAPPEDWINDOW,
6323                               100, 100, 200, 200, 0, 0, 0, NULL);
6324     ok (hparent != 0, "Failed to create parent window\n");
6325
6326     hchild = CreateWindowExA(0, "TestWindowClass", "Test child", WS_CHILD | WS_VISIBLE | WS_BORDER,
6327                            10, 10, 100, 100, hparent, 0, 0, NULL);
6328     ok (hchild != 0, "Failed to create child window\n");
6329
6330     ShowWindow( hparent, SW_SHOW );
6331     UpdateWindow( hparent );
6332     UpdateWindow( hchild );
6333     flush_events();
6334     flush_sequence();
6335     log_all_parent_messages++;
6336
6337     SetRect( &rect, 0, 0, 50, 50 );
6338     RedrawWindow( hparent, &rect, 0, RDW_INVALIDATE | RDW_ERASE | RDW_FRAME );
6339     RedrawWindow( hparent, NULL, 0, RDW_ERASENOW | RDW_ALLCHILDREN );
6340     ok_sequence( WmInvalidateParentChild, "InvalidateParentChild", FALSE );
6341
6342     RedrawWindow( hparent, &rect, 0, RDW_INVALIDATE | RDW_ERASE | RDW_FRAME );
6343     pt.x = pt.y = 0;
6344     MapWindowPoints( hchild, hparent, &pt, 1 );
6345     SetRectRgn( hrgn, 0, 0, 50 - pt.x, 50 - pt.y );
6346     check_update_rgn( hchild, hrgn );
6347     SetRectRgn( hrgn, 0, 0, 50, 50 );
6348     check_update_rgn( hparent, hrgn );
6349     RedrawWindow( hparent, NULL, 0, RDW_ERASENOW );
6350     ok_sequence( WmInvalidateParent, "InvalidateParent", FALSE );
6351     RedrawWindow( hchild, NULL, 0, RDW_ERASENOW );
6352     ok_sequence( WmEmptySeq, "EraseNow child", FALSE );
6353
6354     flush_events();
6355     ok_sequence( WmParentPaintNc, "WmParentPaintNc", FALSE );
6356
6357     RedrawWindow( hparent, &rect, 0, RDW_INVALIDATE | RDW_ERASE | RDW_FRAME | RDW_ALLCHILDREN );
6358     RedrawWindow( hparent, NULL, 0, RDW_ERASENOW );
6359     ok_sequence( WmInvalidateParent, "InvalidateParent2", FALSE );
6360     RedrawWindow( hchild, NULL, 0, RDW_ERASENOW );
6361     ok_sequence( WmEmptySeq, "EraseNow child", FALSE );
6362
6363     RedrawWindow( hparent, &rect, 0, RDW_INVALIDATE | RDW_ERASE );
6364     RedrawWindow( hparent, NULL, 0, RDW_ERASENOW | RDW_ALLCHILDREN );
6365     ok_sequence( WmInvalidateParentChild2, "InvalidateParentChild2", FALSE );
6366
6367     SetWindowLong( hparent, GWL_STYLE, GetWindowLong(hparent,GWL_STYLE) | WS_CLIPCHILDREN );
6368     flush_sequence();
6369     RedrawWindow( hparent, &rect, 0, RDW_INVALIDATE | RDW_ERASE | RDW_FRAME | RDW_ALLCHILDREN );
6370     RedrawWindow( hparent, NULL, 0, RDW_ERASENOW );
6371     ok_sequence( WmInvalidateParentChild, "InvalidateParentChild3", FALSE );
6372
6373     /* flush all paint messages */
6374     flush_events();
6375     flush_sequence();
6376
6377     /* RDW_UPDATENOW on child with WS_CLIPCHILDREN doesn't change corresponding parent area */
6378     RedrawWindow( hparent, &rect, 0, RDW_INVALIDATE | RDW_ERASE | RDW_FRAME | RDW_ALLCHILDREN );
6379     SetRectRgn( hrgn, 0, 0, 50, 50 );
6380     check_update_rgn( hparent, hrgn );
6381     RedrawWindow( hchild, NULL, 0, RDW_UPDATENOW );
6382     ok_sequence( WmInvalidateErasePaint2, "WmInvalidateErasePaint2", FALSE );
6383     SetRectRgn( hrgn, 0, 0, 50, 50 );
6384     check_update_rgn( hparent, hrgn );
6385
6386     /* flush all paint messages */
6387     flush_events();
6388     SetWindowLong( hparent, GWL_STYLE, GetWindowLong(hparent,GWL_STYLE) & ~WS_CLIPCHILDREN );
6389     flush_sequence();
6390
6391     /* RDW_UPDATENOW on child without WS_CLIPCHILDREN will validate corresponding parent area */
6392     RedrawWindow( hparent, &rect, 0, RDW_INVALIDATE | RDW_ERASE | RDW_FRAME );
6393     SetRectRgn( hrgn, 0, 0, 50, 50 );
6394     check_update_rgn( hparent, hrgn );
6395     RedrawWindow( hchild, NULL, 0, RDW_UPDATENOW );
6396     ok_sequence( WmInvalidateErasePaint2, "WmInvalidateErasePaint2", FALSE );
6397     SetRectRgn( hrgn2, 10, 10, 50, 50 );
6398     CombineRgn( hrgn, hrgn, hrgn2, RGN_DIFF );
6399     check_update_rgn( hparent, hrgn );
6400     /* flush all paint messages */
6401     flush_events();
6402     flush_sequence();
6403
6404     /* same as above but parent gets completely validated */
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     RedrawWindow( hchild, NULL, 0, RDW_UPDATENOW );
6410     ok_sequence( WmInvalidateErasePaint2, "WmInvalidateErasePaint2", FALSE );
6411     check_update_rgn( hparent, 0 );  /* no update region */
6412     flush_events();
6413     ok_sequence( WmEmptySeq, "WmEmpty", FALSE );  /* and no paint messages */
6414
6415     /* make sure RDW_VALIDATE on child doesn't have the same effect */
6416     flush_sequence();
6417     RedrawWindow( hparent, &rect, 0, RDW_INVALIDATE | RDW_ERASE | RDW_FRAME );
6418     SetRectRgn( hrgn, 20, 20, 30, 30 );
6419     check_update_rgn( hparent, hrgn );
6420     RedrawWindow( hchild, NULL, 0, RDW_VALIDATE | RDW_NOERASE );
6421     SetRectRgn( hrgn, 20, 20, 30, 30 );
6422     check_update_rgn( hparent, hrgn );
6423
6424     /* same as above but normal WM_PAINT doesn't validate parent */
6425     flush_sequence();
6426     SetRect( &rect, 20, 20, 30, 30 );
6427     RedrawWindow( hparent, &rect, 0, RDW_INVALIDATE | RDW_ERASE | RDW_FRAME );
6428     SetRectRgn( hrgn, 20, 20, 30, 30 );
6429     check_update_rgn( hparent, hrgn );
6430     /* no WM_PAINT in child while parent still pending */
6431     while (PeekMessage( &msg, hchild, 0, 0, PM_REMOVE )) DispatchMessage( &msg );
6432     ok_sequence( WmEmptySeq, "No WM_PAINT", FALSE );
6433     while (PeekMessage( &msg, hparent, 0, 0, PM_REMOVE )) DispatchMessage( &msg );
6434     ok_sequence( WmParentErasePaint, "WmParentErasePaint", FALSE );
6435
6436     flush_sequence();
6437     RedrawWindow( hparent, &rect, 0, RDW_INVALIDATE | RDW_ERASE | RDW_FRAME );
6438     /* no WM_PAINT in child while parent still pending */
6439     while (PeekMessage( &msg, hchild, 0, 0, PM_REMOVE )) DispatchMessage( &msg );
6440     ok_sequence( WmEmptySeq, "No WM_PAINT", FALSE );
6441     RedrawWindow( hparent, &rect, 0, RDW_VALIDATE | RDW_NOERASE | RDW_NOCHILDREN );
6442     /* now that parent is valid child should get WM_PAINT */
6443     while (PeekMessage( &msg, hchild, 0, 0, PM_REMOVE )) DispatchMessage( &msg );
6444     ok_sequence( WmInvalidateErasePaint2, "WmInvalidateErasePaint2", FALSE );
6445     while (PeekMessage( &msg, 0, 0, 0, PM_REMOVE )) DispatchMessage( &msg );
6446     ok_sequence( WmEmptySeq, "No other message", FALSE );
6447
6448     /* same thing with WS_CLIPCHILDREN in parent */
6449     flush_sequence();
6450     SetWindowLong( hparent, GWL_STYLE, GetWindowLong(hparent,GWL_STYLE) | WS_CLIPCHILDREN );
6451     ok_sequence( WmSetParentStyle, "WmSetParentStyle", FALSE );
6452     /* changing style invalidates non client area, but we need to invalidate something else to see it */
6453     RedrawWindow( hparent, &rect, 0, RDW_UPDATENOW );
6454     ok_sequence( WmEmptySeq, "No message", FALSE );
6455     RedrawWindow( hparent, &rect, 0, RDW_INVALIDATE | RDW_UPDATENOW );
6456     ok_sequence( WmParentOnlyNcPaint, "WmParentOnlyNcPaint", FALSE );
6457
6458     flush_sequence();
6459     RedrawWindow( hparent, &rect, 0, RDW_INVALIDATE | RDW_ERASE | RDW_ALLCHILDREN );
6460     SetRectRgn( hrgn, 20, 20, 30, 30 );
6461     check_update_rgn( hparent, hrgn );
6462     /* no WM_PAINT in child while parent still pending */
6463     while (PeekMessage( &msg, hchild, 0, 0, PM_REMOVE )) DispatchMessage( &msg );
6464     ok_sequence( WmEmptySeq, "No WM_PAINT", FALSE );
6465     /* WM_PAINT in parent first */
6466     while (PeekMessage( &msg, 0, 0, 0, PM_REMOVE )) DispatchMessage( &msg );
6467     ok_sequence( WmParentPaintNc, "WmParentPaintNc2", FALSE );
6468
6469     /* no RDW_ERASE in parent still causes RDW_ERASE and RDW_FRAME in child */
6470     flush_sequence();
6471     SetRect( &rect, 0, 0, 30, 30 );
6472     RedrawWindow( hparent, &rect, 0, RDW_INVALIDATE | RDW_ALLCHILDREN );
6473     SetRectRgn( hrgn, 0, 0, 30, 30 );
6474     check_update_rgn( hparent, hrgn );
6475     flush_events();
6476     ok_sequence( WmParentPaintNc, "WmParentPaintNc3", FALSE );
6477
6478     /* validate doesn't cause RDW_NOERASE or RDW_NOFRAME in child */
6479     flush_sequence();
6480     SetRect( &rect, -10, 0, 30, 30 );
6481     RedrawWindow( hchild, &rect, 0, RDW_INVALIDATE | RDW_FRAME | RDW_ERASE );
6482     SetRect( &rect, 0, 0, 20, 20 );
6483     RedrawWindow( hparent, &rect, 0, RDW_VALIDATE | RDW_ALLCHILDREN );
6484     RedrawWindow( hparent, NULL, 0, RDW_UPDATENOW );
6485     ok_sequence( WmChildPaintNc, "WmChildPaintNc", FALSE );
6486
6487     /* validate doesn't cause RDW_NOERASE or RDW_NOFRAME in child */
6488     flush_sequence();
6489     SetRect( &rect, -10, 0, 30, 30 );
6490     RedrawWindow( hchild, &rect, 0, RDW_INVALIDATE | RDW_FRAME | RDW_ERASE );
6491     SetRect( &rect, 0, 0, 100, 100 );
6492     RedrawWindow( hparent, &rect, 0, RDW_VALIDATE | RDW_ALLCHILDREN );
6493     RedrawWindow( hparent, NULL, 0, RDW_UPDATENOW );
6494     ok_sequence( WmEmptySeq, "WmChildPaintNc2", FALSE );
6495     RedrawWindow( hparent, NULL, 0, RDW_ERASENOW );
6496     ok_sequence( WmEmptySeq, "WmChildPaintNc3", FALSE );
6497
6498     /* test RDW_INTERNALPAINT behavior */
6499
6500     flush_sequence();
6501     RedrawWindow( hparent, NULL, 0, RDW_INTERNALPAINT | RDW_NOCHILDREN );
6502     flush_events();
6503     ok_sequence( WmParentOnlyPaint, "WmParentOnlyPaint", FALSE );
6504
6505     RedrawWindow( hparent, NULL, 0, RDW_INTERNALPAINT | RDW_ALLCHILDREN );
6506     flush_events();
6507     ok_sequence( WmParentPaint, "WmParentPaint", FALSE );
6508
6509     RedrawWindow( hparent, NULL, 0, RDW_INTERNALPAINT );
6510     flush_events();
6511     ok_sequence( WmParentOnlyPaint, "WmParentOnlyPaint", FALSE );
6512
6513     assert( GetWindowLong(hparent, GWL_STYLE) & WS_CLIPCHILDREN );
6514     UpdateWindow( hparent );
6515     flush_events();
6516     flush_sequence();
6517     trace("testing SWP_FRAMECHANGED on parent with WS_CLIPCHILDREN\n");
6518     RedrawWindow( hchild, NULL, 0, RDW_INVALIDATE | RDW_ERASE | RDW_FRAME );
6519     SetWindowPos( hparent, 0, 0, 0, 0, 0, SWP_NOSIZE | SWP_NOMOVE |
6520                   SWP_NOACTIVATE | SWP_NOZORDER | SWP_FRAMECHANGED );
6521     flush_events();
6522     ok_sequence(WmSWP_FrameChanged_clip, "SetWindowPos:FrameChanged_clip", FALSE );
6523
6524     UpdateWindow( hparent );
6525     flush_events();
6526     flush_sequence();
6527     trace("testing SWP_FRAMECHANGED|SWP_DEFERERASE on parent with WS_CLIPCHILDREN\n");
6528     RedrawWindow( hchild, NULL, 0, RDW_INVALIDATE | RDW_ERASE | RDW_FRAME );
6529     SetWindowPos( hparent, 0, 0, 0, 0, 0, SWP_NOSIZE | SWP_NOMOVE | SWP_DEFERERASE |
6530                   SWP_NOACTIVATE | SWP_NOZORDER | SWP_FRAMECHANGED );
6531     flush_events();
6532     ok_sequence(WmSWP_FrameChangedDeferErase, "SetWindowPos:FrameChangedDeferErase", FALSE );
6533
6534     SetWindowLong( hparent, GWL_STYLE, GetWindowLong(hparent,GWL_STYLE) & ~WS_CLIPCHILDREN );
6535     ok_sequence( WmSetParentStyle, "WmSetParentStyle", FALSE );
6536     RedrawWindow( hparent, NULL, 0, RDW_INTERNALPAINT );
6537     flush_events();
6538     ok_sequence( WmParentPaint, "WmParentPaint", FALSE );
6539
6540     assert( !(GetWindowLong(hparent, GWL_STYLE) & WS_CLIPCHILDREN) );
6541     UpdateWindow( hparent );
6542     flush_events();
6543     flush_sequence();
6544     trace("testing SWP_FRAMECHANGED on parent without WS_CLIPCHILDREN\n");
6545     RedrawWindow( hchild, NULL, 0, RDW_INVALIDATE | RDW_ERASE | RDW_FRAME );
6546     SetWindowPos( hparent, 0, 0, 0, 0, 0, SWP_NOSIZE | SWP_NOMOVE |
6547                   SWP_NOACTIVATE | SWP_NOZORDER | SWP_FRAMECHANGED );
6548     flush_events();
6549     ok_sequence(WmSWP_FrameChanged_noclip, "SetWindowPos:FrameChanged_noclip", FALSE );
6550
6551     UpdateWindow( hparent );
6552     flush_events();
6553     flush_sequence();
6554     trace("testing SWP_FRAMECHANGED|SWP_DEFERERASE on parent without WS_CLIPCHILDREN\n");
6555     RedrawWindow( hchild, NULL, 0, RDW_INVALIDATE | RDW_ERASE | RDW_FRAME );
6556     SetWindowPos( hparent, 0, 0, 0, 0, 0, SWP_NOSIZE | SWP_NOMOVE | SWP_DEFERERASE |
6557                   SWP_NOACTIVATE | SWP_NOZORDER | SWP_FRAMECHANGED );
6558     flush_events();
6559     ok_sequence(WmSWP_FrameChangedDeferErase, "SetWindowPos:FrameChangedDeferErase", FALSE );
6560
6561     ok(GetWindowLong( hparent, GWL_STYLE ) & WS_VISIBLE, "parent should be visible\n");
6562     ok(GetWindowLong( hchild, GWL_STYLE ) & WS_VISIBLE, "child should be visible\n");
6563
6564     UpdateWindow( hparent );
6565     flush_events();
6566     flush_sequence();
6567     trace("testing SetWindowPos(-10000, -10000) on child\n");
6568     SetWindowPos( hchild, 0, -10000, -10000, 0, 0, SWP_NOSIZE | SWP_NOACTIVATE | SWP_NOZORDER );
6569     check_update_rgn( hchild, 0 );
6570     flush_events();
6571
6572 #if 0 /* this one doesn't pass under Wine yet */
6573     UpdateWindow( hparent );
6574     flush_events();
6575     flush_sequence();
6576     trace("testing ShowWindow(SW_MINIMIZE) on child\n");
6577     ShowWindow( hchild, SW_MINIMIZE );
6578     check_update_rgn( hchild, 0 );
6579     flush_events();
6580 #endif
6581
6582     UpdateWindow( hparent );
6583     flush_events();
6584     flush_sequence();
6585     trace("testing SetWindowPos(-10000, -10000) on parent\n");
6586     SetWindowPos( hparent, 0, -10000, -10000, 0, 0, SWP_NOSIZE | SWP_NOACTIVATE | SWP_NOZORDER );
6587     check_update_rgn( hparent, 0 );
6588     flush_events();
6589
6590     log_all_parent_messages--;
6591     DestroyWindow( hparent );
6592     ok(!IsWindow(hchild), "child must be destroyed with its parent\n");
6593
6594     /* tests for moving windows off-screen (needs simple WS_POPUP windows) */
6595
6596     hparent = CreateWindowExA(0, "TestParentClass", "Test parent", WS_POPUP | WS_VISIBLE,
6597                               100, 100, 200, 200, 0, 0, 0, NULL);
6598     ok (hparent != 0, "Failed to create parent window\n");
6599
6600     hchild = CreateWindowExA(0, "TestWindowClass", "Test child", WS_CHILD | WS_VISIBLE,
6601                            10, 10, 100, 100, hparent, 0, 0, NULL);
6602     ok (hchild != 0, "Failed to create child window\n");
6603
6604     ShowWindow( hparent, SW_SHOW );
6605     UpdateWindow( hparent );
6606     UpdateWindow( hchild );
6607     flush_events();
6608     flush_sequence();
6609
6610     /* moving child outside of parent boundaries changes update region */
6611     SetRect( &rect, 0, 0, 40, 40 );
6612     RedrawWindow( hchild, &rect, 0, RDW_INVALIDATE | RDW_ERASE );
6613     SetRectRgn( hrgn, 0, 0, 40, 40 );
6614     check_update_rgn( hchild, hrgn );
6615     MoveWindow( hchild, -10, 10, 100, 100, FALSE );
6616     SetRectRgn( hrgn, 10, 0, 40, 40 );
6617     check_update_rgn( hchild, hrgn );
6618     MoveWindow( hchild, -10, -10, 100, 100, FALSE );
6619     SetRectRgn( hrgn, 10, 10, 40, 40 );
6620     check_update_rgn( hchild, hrgn );
6621
6622     /* moving parent off-screen does too */
6623     SetRect( &rect, 0, 0, 100, 100 );
6624     RedrawWindow( hparent, &rect, 0, RDW_INVALIDATE | RDW_ERASE | RDW_NOCHILDREN );
6625     SetRectRgn( hrgn, 0, 0, 100, 100 );
6626     check_update_rgn( hparent, hrgn );
6627     SetRectRgn( hrgn, 10, 10, 40, 40 );
6628     check_update_rgn( hchild, hrgn );
6629     MoveWindow( hparent, -20, -20, 200, 200, FALSE );
6630     SetRectRgn( hrgn, 20, 20, 100, 100 );
6631     check_update_rgn( hparent, hrgn );
6632     SetRectRgn( hrgn, 30, 30, 40, 40 );
6633     check_update_rgn( hchild, hrgn );
6634
6635     /* invalidated region is cropped by the parent rects */
6636     SetRect( &rect, 0, 0, 50, 50 );
6637     RedrawWindow( hchild, &rect, 0, RDW_INVALIDATE | RDW_ERASE );
6638     SetRectRgn( hrgn, 30, 30, 50, 50 );
6639     check_update_rgn( hchild, hrgn );
6640
6641     DestroyWindow( hparent );
6642     ok(!IsWindow(hchild), "child must be destroyed with its parent\n");
6643     flush_sequence();
6644
6645     DeleteObject( hrgn );
6646     DeleteObject( hrgn2 );
6647 }
6648
6649 struct wnd_event
6650 {
6651     HWND hwnd;
6652     HANDLE grand_child;
6653     HANDLE start_event;
6654     HANDLE stop_event;
6655 };
6656
6657 static DWORD WINAPI thread_proc(void *param)
6658 {
6659     MSG msg;
6660     struct wnd_event *wnd_event = param;
6661
6662     wnd_event->hwnd = CreateWindowExA(0, "TestWindowClass", "window caption text", WS_OVERLAPPEDWINDOW,
6663                                       100, 100, 200, 200, 0, 0, 0, NULL);
6664     ok(wnd_event->hwnd != 0, "Failed to create overlapped window\n");
6665
6666     SetEvent(wnd_event->start_event);
6667
6668     while (GetMessage(&msg, 0, 0, 0))
6669     {
6670         TranslateMessage(&msg);
6671         DispatchMessage(&msg);
6672     }
6673
6674     ok(IsWindow(wnd_event->hwnd), "window should still exist\n");
6675
6676     return 0;
6677 }
6678
6679 static DWORD CALLBACK create_grand_child_thread( void *param )
6680 {
6681     struct wnd_event *wnd_event = param;
6682     HWND hchild;
6683     MSG msg;
6684
6685     hchild = CreateWindowExA(0, "TestWindowClass", "Test child",
6686                              WS_CHILD | WS_VISIBLE, 0, 0, 10, 10, wnd_event->hwnd, 0, 0, NULL);
6687     ok (hchild != 0, "Failed to create child window\n");
6688     flush_events();
6689     flush_sequence();
6690     SetEvent( wnd_event->start_event );
6691
6692     for (;;)
6693     {
6694         MsgWaitForMultipleObjects(0, NULL, FALSE, 1000, QS_ALLINPUT);
6695         if (!IsWindow( hchild )) break;  /* will be destroyed when parent thread exits */
6696         while (PeekMessageA(&msg, 0, 0, 0, PM_REMOVE)) DispatchMessage(&msg);
6697     }
6698     return 0;
6699 }
6700
6701 static DWORD CALLBACK create_child_thread( void *param )
6702 {
6703     struct wnd_event *wnd_event = param;
6704     struct wnd_event child_event;
6705     DWORD ret, tid;
6706     MSG msg;
6707
6708     child_event.hwnd = CreateWindowExA(0, "TestWindowClass", "Test child",
6709                              WS_CHILD | WS_VISIBLE, 0, 0, 10, 10, wnd_event->hwnd, 0, 0, NULL);
6710     ok (child_event.hwnd != 0, "Failed to create child window\n");
6711     SetFocus( child_event.hwnd );
6712     flush_events();
6713     flush_sequence();
6714     child_event.start_event = wnd_event->start_event;
6715     wnd_event->grand_child = CreateThread(NULL, 0, create_grand_child_thread, &child_event, 0, &tid);
6716     for (;;)
6717     {
6718         DWORD ret = MsgWaitForMultipleObjects(1, &child_event.start_event, FALSE, 1000, QS_SENDMESSAGE);
6719         if (ret != 1) break;
6720         while (PeekMessageA(&msg, 0, 0, 0, PM_REMOVE)) DispatchMessage(&msg);
6721     }
6722     ret = WaitForSingleObject( wnd_event->stop_event, 5000 );
6723     ok( !ret, "WaitForSingleObject failed %x\n", ret );
6724     return 0;
6725 }
6726
6727 static void test_interthread_messages(void)
6728 {
6729     HANDLE hThread;
6730     DWORD tid;
6731     WNDPROC proc;
6732     MSG msg;
6733     char buf[256];
6734     int len, expected_len;
6735     struct wnd_event wnd_event;
6736     BOOL ret;
6737
6738     wnd_event.start_event = CreateEventW(NULL, 0, 0, NULL);
6739     if (!wnd_event.start_event)
6740     {
6741         win_skip("skipping interthread message test under win9x\n");
6742         return;
6743     }
6744
6745     hThread = CreateThread(NULL, 0, thread_proc, &wnd_event, 0, &tid);
6746     ok(hThread != NULL, "CreateThread failed, error %d\n", GetLastError());
6747
6748     ok(WaitForSingleObject(wnd_event.start_event, INFINITE) == WAIT_OBJECT_0, "WaitForSingleObject failed\n");
6749
6750     CloseHandle(wnd_event.start_event);
6751
6752     SetLastError(0xdeadbeef);
6753     ok(!DestroyWindow(wnd_event.hwnd), "DestroyWindow succeeded\n");
6754     ok(GetLastError() == ERROR_ACCESS_DENIED || GetLastError() == 0xdeadbeef,
6755        "wrong error code %d\n", GetLastError());
6756
6757     proc = (WNDPROC)GetWindowLongPtrA(wnd_event.hwnd, GWLP_WNDPROC);
6758     ok(proc != NULL, "GetWindowLongPtrA(GWLP_WNDPROC) error %d\n", GetLastError());
6759
6760     expected_len = lstrlenA("window caption text");
6761     memset(buf, 0, sizeof(buf));
6762     SetLastError(0xdeadbeef);
6763     len = CallWindowProcA(proc, wnd_event.hwnd, WM_GETTEXT, sizeof(buf), (LPARAM)buf);
6764     ok(len == expected_len, "CallWindowProcA(WM_GETTEXT) error %d, len %d, expected len %d\n", GetLastError(), len, expected_len);
6765     ok(!lstrcmpA(buf, "window caption text"), "window text mismatch\n");
6766
6767     msg.hwnd = wnd_event.hwnd;
6768     msg.message = WM_GETTEXT;
6769     msg.wParam = sizeof(buf);
6770     msg.lParam = (LPARAM)buf;
6771     memset(buf, 0, sizeof(buf));
6772     SetLastError(0xdeadbeef);
6773     len = DispatchMessageA(&msg);
6774     ok((!len && GetLastError() == ERROR_MESSAGE_SYNC_ONLY) || broken(len), /* nt4 */
6775        "DispatchMessageA(WM_GETTEXT) succeeded on another thread window: ret %d, error %d\n", len, GetLastError());
6776
6777     /* the following test causes an exception in user.exe under win9x */
6778     msg.hwnd = wnd_event.hwnd;
6779     msg.message = WM_TIMER;
6780     msg.wParam = 0;
6781     msg.lParam = GetWindowLongPtrA(wnd_event.hwnd, GWLP_WNDPROC);
6782     SetLastError(0xdeadbeef);
6783     len = DispatchMessageA(&msg);
6784     ok(!len && GetLastError() == 0xdeadbeef,
6785        "DispatchMessageA(WM_TIMER) failed on another thread window: ret %d, error %d\n", len, GetLastError());
6786
6787     ret = PostMessageA(wnd_event.hwnd, WM_QUIT, 0, 0);
6788     ok( ret, "PostMessageA(WM_QUIT) error %d\n", GetLastError());
6789
6790     ok(WaitForSingleObject(hThread, INFINITE) == WAIT_OBJECT_0, "WaitForSingleObject failed\n");
6791     CloseHandle(hThread);
6792
6793     ok(!IsWindow(wnd_event.hwnd), "window should be destroyed on thread exit\n");
6794
6795     wnd_event.hwnd = CreateWindowExA(0, "TestParentClass", "Test parent", WS_OVERLAPPEDWINDOW | WS_VISIBLE,
6796                               100, 100, 200, 200, 0, 0, 0, NULL);
6797     ok (wnd_event.hwnd != 0, "Failed to create parent window\n");
6798     flush_sequence();
6799     log_all_parent_messages++;
6800     wnd_event.start_event = CreateEventA( NULL, TRUE, FALSE, NULL );
6801     wnd_event.stop_event = CreateEventA( NULL, TRUE, FALSE, NULL );
6802     hThread = CreateThread( NULL, 0, create_child_thread, &wnd_event, 0, &tid );
6803     for (;;)
6804     {
6805         ret = MsgWaitForMultipleObjects(1, &wnd_event.start_event, FALSE, 1000, QS_SENDMESSAGE);
6806         if (ret != 1) break;
6807         while (PeekMessageA(&msg, 0, 0, 0, PM_REMOVE)) DispatchMessage(&msg);
6808     }
6809     ok( !ret, "MsgWaitForMultipleObjects failed %x\n", ret );
6810     /* now wait for the thread without processing messages; this shouldn't deadlock */
6811     SetEvent( wnd_event.stop_event );
6812     ret = WaitForSingleObject( hThread, 5000 );
6813     ok( !ret, "WaitForSingleObject failed %x\n", ret );
6814     CloseHandle( hThread );
6815
6816     ret = WaitForSingleObject( wnd_event.grand_child, 5000 );
6817     ok( !ret, "WaitForSingleObject failed %x\n", ret );
6818     CloseHandle( wnd_event.grand_child );
6819
6820     CloseHandle( wnd_event.start_event );
6821     CloseHandle( wnd_event.stop_event );
6822     flush_events();
6823     ok_sequence(WmExitThreadSeq, "destroy child on thread exit", FALSE);
6824     log_all_parent_messages--;
6825     DestroyWindow( wnd_event.hwnd );
6826 }
6827
6828
6829 static const struct message WmVkN[] = {
6830     { HCBT_KEYSKIPPED, hook|wparam|lparam|optional, 'N', 1 }, /* XP */
6831     { WM_KEYDOWN, wparam|lparam, 'N', 1 },
6832     { WM_KEYDOWN, sent|wparam|lparam, 'N', 1 },
6833     { WM_CHAR, wparam|lparam, 'n', 1 },
6834     { WM_COMMAND, sent|wparam|lparam, MAKEWPARAM(1002,1), 0 },
6835     { HCBT_KEYSKIPPED, hook|wparam|lparam|optional, 'N', 0xc0000001 }, /* XP */
6836     { WM_KEYUP, wparam|lparam, 'N', 0xc0000001 },
6837     { WM_KEYUP, sent|wparam|lparam, 'N', 0xc0000001 },
6838     { 0 }
6839 };
6840 static const struct message WmShiftVkN[] = {
6841     { HCBT_KEYSKIPPED, hook|wparam|lparam|optional, VK_SHIFT, 1 }, /* XP */
6842     { WM_KEYDOWN, wparam|lparam, VK_SHIFT, 1 },
6843     { WM_KEYDOWN, sent|wparam|lparam, VK_SHIFT, 1 },
6844     { HCBT_KEYSKIPPED, hook|wparam|lparam|optional, 'N', 1 }, /* XP */
6845     { WM_KEYDOWN, wparam|lparam, 'N', 1 },
6846     { WM_KEYDOWN, sent|wparam|lparam, 'N', 1 },
6847     { WM_CHAR, wparam|lparam, 'N', 1 },
6848     { WM_COMMAND, sent|wparam|lparam, MAKEWPARAM(1001,1), 0 },
6849     { HCBT_KEYSKIPPED, hook|wparam|lparam|optional, 'N', 0xc0000001 }, /* XP */
6850     { WM_KEYUP, wparam|lparam, 'N', 0xc0000001 },
6851     { WM_KEYUP, sent|wparam|lparam, 'N', 0xc0000001 },
6852     { HCBT_KEYSKIPPED, hook|wparam|lparam|optional, VK_SHIFT, 0xc0000001 }, /* XP */
6853     { WM_KEYUP, wparam|lparam, VK_SHIFT, 0xc0000001 },
6854     { WM_KEYUP, sent|wparam|lparam, VK_SHIFT, 0xc0000001 },
6855     { 0 }
6856 };
6857 static const struct message WmCtrlVkN[] = {
6858     { HCBT_KEYSKIPPED, hook|wparam|lparam|optional, VK_CONTROL, 1 }, /* XP */
6859     { WM_KEYDOWN, wparam|lparam, VK_CONTROL, 1 },
6860     { WM_KEYDOWN, sent|wparam|lparam, VK_CONTROL, 1 },
6861     { HCBT_KEYSKIPPED, hook|wparam|lparam|optional, 'N', 1 }, /* XP */
6862     { WM_KEYDOWN, wparam|lparam, 'N', 1 },
6863     { WM_KEYDOWN, sent|wparam|lparam, 'N', 1 },
6864     { WM_CHAR, wparam|lparam, 0x000e, 1 },
6865     { WM_COMMAND, sent|wparam|lparam, MAKEWPARAM(1000,1), 0 },
6866     { HCBT_KEYSKIPPED, hook|wparam|lparam|optional, 'N', 0xc0000001 }, /* XP */
6867     { WM_KEYUP, wparam|lparam, 'N', 0xc0000001 },
6868     { WM_KEYUP, sent|wparam|lparam, 'N', 0xc0000001 },
6869     { HCBT_KEYSKIPPED, hook|wparam|lparam|optional, VK_CONTROL, 0xc0000001 }, /* XP */
6870     { WM_KEYUP, wparam|lparam, VK_CONTROL, 0xc0000001 },
6871     { WM_KEYUP, sent|wparam|lparam, VK_CONTROL, 0xc0000001 },
6872     { 0 }
6873 };
6874 static const struct message WmCtrlVkN_2[] = {
6875     { HCBT_KEYSKIPPED, hook|wparam|lparam|optional, VK_CONTROL, 1 }, /* XP */
6876     { WM_KEYDOWN, wparam|lparam, VK_CONTROL, 1 },
6877     { WM_KEYDOWN, sent|wparam|lparam, VK_CONTROL, 1 },
6878     { HCBT_KEYSKIPPED, hook|wparam|lparam|optional, 'N', 1 }, /* XP */
6879     { WM_KEYDOWN, wparam|lparam, 'N', 1 },
6880     { WM_COMMAND, sent|wparam|lparam, MAKEWPARAM(1000,1), 0 },
6881     { HCBT_KEYSKIPPED, hook|wparam|lparam|optional, 'N', 0xc0000001 }, /* XP */
6882     { WM_KEYUP, wparam|lparam, 'N', 0xc0000001 },
6883     { WM_KEYUP, sent|wparam|lparam, 'N', 0xc0000001 },
6884     { HCBT_KEYSKIPPED, hook|wparam|lparam|optional, VK_CONTROL, 0xc0000001 }, /* XP */
6885     { WM_KEYUP, wparam|lparam, VK_CONTROL, 0xc0000001 },
6886     { WM_KEYUP, sent|wparam|lparam, VK_CONTROL, 0xc0000001 },
6887     { 0 }
6888 };
6889 static const struct message WmAltVkN[] = {
6890     { HCBT_KEYSKIPPED, hook|wparam|lparam|optional, VK_MENU, 0x20000001 }, /* XP */
6891     { WM_SYSKEYDOWN, wparam|lparam, VK_MENU, 0x20000001 },
6892     { WM_SYSKEYDOWN, sent|wparam|lparam, VK_MENU, 0x20000001 },
6893     { HCBT_KEYSKIPPED, hook|wparam|lparam|optional, 'N', 0x20000001 }, /* XP */
6894     { WM_SYSKEYDOWN, wparam|lparam, 'N', 0x20000001 },
6895     { WM_SYSKEYDOWN, sent|wparam|lparam, 'N', 0x20000001 },
6896     { WM_SYSCHAR, wparam|lparam, 'n', 0x20000001 },
6897     { WM_SYSCHAR, sent|wparam|lparam, 'n', 0x20000001 },
6898     { WM_SYSCOMMAND, sent|defwinproc|wparam|lparam, SC_KEYMENU, 'n' },
6899     { HCBT_SYSCOMMAND, hook },
6900     { WM_ENTERMENULOOP, sent|defwinproc|wparam|lparam, 0, 0 },
6901     { EVENT_SYSTEM_CAPTURESTART, winevent_hook|wparam|lparam, 0, 0 },
6902     { 0x00AE, sent|defwinproc|optional }, /* XP */
6903     { WM_GETTEXT, sent|defwinproc|optional }, /* XP */
6904     { WM_INITMENU, sent|defwinproc },
6905     { EVENT_SYSTEM_MENUSTART, winevent_hook|wparam|lparam, OBJID_SYSMENU, 0 },
6906     { WM_MENUCHAR, sent|defwinproc|wparam, MAKEWPARAM('n',MF_SYSMENU) },
6907     { EVENT_SYSTEM_CAPTUREEND, winevent_hook|wparam|lparam, 0, 0 },
6908     { WM_CAPTURECHANGED, sent|defwinproc },
6909     { WM_MENUSELECT, sent|defwinproc|wparam, MAKEWPARAM(0,0xffff) },
6910     { EVENT_SYSTEM_MENUEND, winevent_hook|wparam|lparam, OBJID_SYSMENU, 0 },
6911     { WM_EXITMENULOOP, sent|defwinproc },
6912     { WM_MENUSELECT, sent|defwinproc|wparam|optional, MAKEWPARAM(0,0xffff) }, /* Win95 bug */
6913     { WM_EXITMENULOOP, sent|defwinproc|optional }, /* Win95 bug */
6914     { HCBT_KEYSKIPPED, hook|wparam|lparam|optional, 'N', 0xe0000001 }, /* XP */
6915     { WM_SYSKEYUP, wparam|lparam, 'N', 0xe0000001 },
6916     { WM_SYSKEYUP, sent|wparam|lparam, 'N', 0xe0000001 },
6917     { HCBT_KEYSKIPPED, hook|wparam|lparam|optional, VK_MENU, 0xc0000001 }, /* XP */
6918     { WM_KEYUP, wparam|lparam, VK_MENU, 0xc0000001 },
6919     { WM_KEYUP, sent|wparam|lparam, VK_MENU, 0xc0000001 },
6920     { 0 }
6921 };
6922 static const struct message WmAltVkN_2[] = {
6923     { HCBT_KEYSKIPPED, hook|wparam|lparam|optional, VK_MENU, 0x20000001 }, /* XP */
6924     { WM_SYSKEYDOWN, wparam|lparam, VK_MENU, 0x20000001 },
6925     { WM_SYSKEYDOWN, sent|wparam|lparam, VK_MENU, 0x20000001 },
6926     { HCBT_KEYSKIPPED, hook|wparam|lparam|optional, 'N', 0x20000001 }, /* XP */
6927     { WM_SYSKEYDOWN, wparam|lparam, 'N', 0x20000001 },
6928     { WM_COMMAND, sent|wparam|lparam, MAKEWPARAM(1003,1), 0 },
6929     { HCBT_KEYSKIPPED, hook|wparam|lparam|optional, 'N', 0xe0000001 }, /* XP */
6930     { WM_SYSKEYUP, wparam|lparam, 'N', 0xe0000001 },
6931     { WM_SYSKEYUP, sent|wparam|lparam, 'N', 0xe0000001 },
6932     { HCBT_KEYSKIPPED, hook|wparam|lparam|optional, VK_MENU, 0xc0000001 }, /* XP */
6933     { WM_KEYUP, wparam|lparam, VK_MENU, 0xc0000001 },
6934     { WM_KEYUP, sent|wparam|lparam, VK_MENU, 0xc0000001 },
6935     { 0 }
6936 };
6937 static const struct message WmCtrlAltVkN[] = {
6938     { HCBT_KEYSKIPPED, hook|wparam|lparam|optional, VK_CONTROL, 1 }, /* XP */
6939     { WM_KEYDOWN, wparam|lparam, VK_CONTROL, 1 },
6940     { WM_KEYDOWN, sent|wparam|lparam, VK_CONTROL, 1 },
6941     { HCBT_KEYSKIPPED, hook|wparam|lparam|optional, VK_MENU, 0x20000001 }, /* XP */
6942     { WM_KEYDOWN, wparam|lparam, VK_MENU, 0x20000001 },
6943     { WM_KEYDOWN, sent|wparam|lparam, VK_MENU, 0x20000001 },
6944     { HCBT_KEYSKIPPED, hook|wparam|lparam|optional, 'N', 0x20000001 }, /* XP */
6945     { WM_KEYDOWN, wparam|lparam, 'N', 0x20000001 },
6946     { WM_KEYDOWN, sent|wparam|lparam, 'N', 0x20000001 },
6947     { WM_CHAR, optional },
6948     { WM_CHAR, sent|optional },
6949     { HCBT_KEYSKIPPED, hook|wparam|lparam|optional, 'N', 0xe0000001 }, /* XP */
6950     { WM_KEYUP, wparam|lparam, 'N', 0xe0000001 },
6951     { WM_KEYUP, sent|wparam|lparam, 'N', 0xe0000001 },
6952     { HCBT_KEYSKIPPED, hook|wparam|lparam|optional, VK_MENU, 0xc0000001 }, /* XP */
6953     { WM_KEYUP, wparam|lparam, VK_MENU, 0xc0000001 },
6954     { WM_KEYUP, sent|wparam|lparam, VK_MENU, 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 WmCtrlShiftVkN[] = {
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_SHIFT, 1 }, /* XP */
6965     { WM_KEYDOWN, wparam|lparam, VK_SHIFT, 1 },
6966     { WM_KEYDOWN, sent|wparam|lparam, VK_SHIFT, 1 },
6967     { HCBT_KEYSKIPPED, hook|wparam|lparam|optional, 'N', 1 }, /* XP */
6968     { WM_KEYDOWN, wparam|lparam, 'N', 1 },
6969     { WM_COMMAND, sent|wparam|lparam, MAKEWPARAM(1004,1), 0 },
6970     { HCBT_KEYSKIPPED, hook|wparam|lparam|optional, 'N', 0xc0000001 }, /* XP */
6971     { WM_KEYUP, wparam|lparam, 'N', 0xc0000001 },
6972     { WM_KEYUP, sent|wparam|lparam, 'N', 0xc0000001 },
6973     { HCBT_KEYSKIPPED, hook|wparam|lparam|optional, VK_SHIFT, 0xc0000001 }, /* XP */
6974     { WM_KEYUP, wparam|lparam, VK_SHIFT, 0xc0000001 },
6975     { WM_KEYUP, sent|wparam|lparam, VK_SHIFT, 0xc0000001 },
6976     { HCBT_KEYSKIPPED, hook|wparam|lparam|optional, VK_CONTROL, 0xc0000001 }, /* XP */
6977     { WM_KEYUP, wparam|lparam, VK_CONTROL, 0xc0000001 },
6978     { WM_KEYUP, sent|wparam|lparam, VK_CONTROL, 0xc0000001 },
6979     { 0 }
6980 };
6981 static const struct message WmCtrlAltShiftVkN[] = {
6982     { HCBT_KEYSKIPPED, hook|wparam|lparam|optional, VK_CONTROL, 1 }, /* XP */
6983     { WM_KEYDOWN, wparam|lparam, VK_CONTROL, 1 },
6984     { WM_KEYDOWN, sent|wparam|lparam, VK_CONTROL, 1 },
6985     { HCBT_KEYSKIPPED, hook|wparam|lparam|optional, VK_MENU, 0x20000001 }, /* XP */
6986     { WM_KEYDOWN, wparam|lparam, VK_MENU, 0x20000001 },
6987     { WM_KEYDOWN, sent|wparam|lparam, VK_MENU, 0x20000001 },
6988     { HCBT_KEYSKIPPED, hook|wparam|lparam|optional, VK_SHIFT, 0x20000001 }, /* XP */
6989     { WM_KEYDOWN, wparam|lparam, VK_SHIFT, 0x20000001 },
6990     { WM_KEYDOWN, sent|wparam|lparam, VK_SHIFT, 0x20000001 },
6991     { HCBT_KEYSKIPPED, hook|wparam|lparam|optional, 'N', 0x20000001 }, /* XP */
6992     { WM_KEYDOWN, wparam|lparam, 'N', 0x20000001 },
6993     { WM_COMMAND, sent|wparam|lparam, MAKEWPARAM(1005,1), 0 },
6994     { HCBT_KEYSKIPPED, hook|wparam|lparam|optional, 'N', 0xe0000001 }, /* XP */
6995     { WM_KEYUP, wparam|lparam, 'N', 0xe0000001 },
6996     { WM_KEYUP, sent|wparam|lparam, 'N', 0xe0000001 },
6997     { HCBT_KEYSKIPPED, hook|wparam|lparam|optional, VK_SHIFT, 0xe0000001 }, /* XP */
6998     { WM_KEYUP, wparam|lparam, VK_SHIFT, 0xe0000001 },
6999     { WM_KEYUP, sent|wparam|lparam, VK_SHIFT, 0xe0000001 },
7000     { HCBT_KEYSKIPPED, hook|wparam|lparam|optional, VK_MENU, 0xc0000001 }, /* XP */
7001     { WM_KEYUP, wparam|lparam, VK_MENU, 0xc0000001 },
7002     { WM_KEYUP, sent|wparam|lparam, VK_MENU, 0xc0000001 },
7003     { HCBT_KEYSKIPPED, hook|wparam|lparam|optional, VK_CONTROL, 0xc0000001 }, /* XP */
7004     { WM_KEYUP, wparam|lparam, VK_CONTROL, 0xc0000001 },
7005     { WM_KEYUP, sent|wparam|lparam, VK_CONTROL, 0xc0000001 },
7006     { 0 }
7007 };
7008 static const struct message WmAltPressRelease[] = {
7009     { HCBT_KEYSKIPPED, hook|wparam|lparam|optional, VK_MENU, 0x20000001 }, /* XP */
7010     { WM_SYSKEYDOWN, wparam|lparam, VK_MENU, 0x20000001 },
7011     { WM_SYSKEYDOWN, sent|wparam|lparam, VK_MENU, 0x20000001 },
7012     { HCBT_KEYSKIPPED, hook|wparam|lparam|optional, VK_MENU, 0xc0000001 }, /* XP */
7013     { WM_SYSKEYUP, wparam|lparam, VK_MENU, 0xc0000001 },
7014     { WM_SYSKEYUP, sent|wparam|lparam, VK_MENU, 0xc0000001 },
7015     { WM_SYSCOMMAND, sent|defwinproc|wparam|lparam, SC_KEYMENU, 0 },
7016     { HCBT_SYSCOMMAND, hook },
7017     { WM_ENTERMENULOOP, sent|defwinproc|wparam|lparam, 0, 0 },
7018     { EVENT_SYSTEM_CAPTURESTART, winevent_hook|wparam|lparam, 0, 0 },
7019     { WM_INITMENU, sent|defwinproc },
7020     { EVENT_SYSTEM_MENUSTART, winevent_hook|wparam|lparam, OBJID_SYSMENU, 0 },
7021     { WM_MENUSELECT, sent|defwinproc|wparam, MAKEWPARAM(0,MF_SYSMENU|MF_POPUP|MF_HILITE) },
7022     { EVENT_OBJECT_FOCUS, winevent_hook|wparam|lparam, OBJID_SYSMENU, 1 },
7023
7024     { HCBT_KEYSKIPPED, hook|wparam|lparam|optional, VK_MENU, 0x30000001 }, /* XP */
7025
7026     { EVENT_OBJECT_FOCUS, winevent_hook|wparam|lparam, OBJID_SYSMENU, 0 },
7027     { EVENT_SYSTEM_CAPTUREEND, winevent_hook|wparam|lparam, 0, 0, },
7028     { WM_CAPTURECHANGED, sent|defwinproc },
7029     { WM_MENUSELECT, sent|defwinproc|wparam|optional, MAKEWPARAM(0,0xffff) },
7030     { EVENT_SYSTEM_MENUEND, winevent_hook|wparam|lparam, OBJID_SYSMENU, 0 },
7031     { WM_EXITMENULOOP, sent|defwinproc },
7032     { HCBT_KEYSKIPPED, hook|wparam|lparam|optional, VK_MENU, 0xc0000001 }, /* XP */
7033     { WM_SYSKEYUP, wparam|lparam, VK_MENU, 0xc0000001 },
7034     { WM_SYSKEYUP, sent|wparam|lparam, VK_MENU, 0xc0000001 },
7035     { 0 }
7036 };
7037 static const struct message WmShiftMouseButton[] = {
7038     { HCBT_KEYSKIPPED, hook|wparam|lparam|optional, VK_SHIFT, 1 }, /* XP */
7039     { WM_KEYDOWN, wparam|lparam, VK_SHIFT, 1 },
7040     { WM_KEYDOWN, sent|wparam|lparam, VK_SHIFT, 1 },
7041     { WM_MOUSEMOVE, wparam|optional, 0, 0 },
7042     { WM_MOUSEMOVE, sent|wparam|optional, 0, 0 },
7043     { WM_LBUTTONDOWN, wparam, MK_LBUTTON|MK_SHIFT, 0 },
7044     { WM_LBUTTONDOWN, sent|wparam, MK_LBUTTON|MK_SHIFT, 0 },
7045     { WM_LBUTTONUP, wparam, MK_SHIFT, 0 },
7046     { WM_LBUTTONUP, sent|wparam, MK_SHIFT, 0 },
7047     { HCBT_KEYSKIPPED, hook|wparam|lparam|optional, VK_SHIFT, 0xc0000001 }, /* XP */
7048     { WM_KEYUP, wparam|lparam, VK_SHIFT, 0xc0000001 },
7049     { WM_KEYUP, sent|wparam|lparam, VK_SHIFT, 0xc0000001 },
7050     { 0 }
7051 };
7052 static const struct message WmF1Seq[] = {
7053     { HCBT_KEYSKIPPED, hook|wparam|lparam|optional, VK_F1, 1 }, /* XP */
7054     { WM_KEYDOWN, wparam|lparam, VK_F1, 1 },
7055     { WM_KEYDOWN, sent|wparam|lparam, VK_F1, 0x00000001 },
7056     { WM_KEYF1, wparam|lparam, 0, 0 },
7057     { WM_KEYF1, sent|wparam|lparam, 0, 0 },
7058     { WM_HELP, sent|defwinproc },
7059     { HCBT_KEYSKIPPED, hook|wparam|lparam|optional, VK_F1, 0xc0000001 }, /* XP */
7060     { WM_KEYUP, wparam|lparam, VK_F1, 0xc0000001 },
7061     { WM_KEYUP, sent|wparam|lparam, VK_F1, 0xc0000001 },
7062     { 0 }
7063 };
7064 static const struct message WmVkAppsSeq[] = {
7065     { HCBT_KEYSKIPPED, hook|wparam|lparam|optional, VK_APPS, 1 }, /* XP */
7066     { WM_KEYDOWN, wparam|lparam, VK_APPS, 1 },
7067     { WM_KEYDOWN, sent|wparam|lparam, VK_APPS, 0x00000001 },
7068     { HCBT_KEYSKIPPED, hook|wparam|lparam|optional, VK_APPS, 0xc0000001 }, /* XP */
7069     { WM_KEYUP, wparam|lparam, VK_APPS, 0xc0000001 },
7070     { WM_KEYUP, sent|wparam|lparam, VK_APPS, 0xc0000001 },
7071     { WM_CONTEXTMENU, lparam, /*hwnd*/0, -1 },
7072     { WM_CONTEXTMENU, sent|lparam, /*hwnd*/0, -1 },
7073     { 0 }
7074 };
7075 static const struct message WmVkF10Seq[] = {
7076     { HCBT_KEYSKIPPED, hook|wparam|lparam|optional, VK_F10, 1 }, /* XP */
7077     { WM_SYSKEYDOWN, wparam|lparam, VK_F10, 1 },
7078     { WM_SYSKEYDOWN, sent|wparam|lparam, VK_F10, 0x00000001 },
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     { WM_SYSCOMMAND, sent|defwinproc|wparam, SC_KEYMENU },
7083     { HCBT_SYSCOMMAND, hook },
7084     { WM_ENTERMENULOOP, sent|defwinproc|wparam|lparam, 0, 0 },
7085     { EVENT_SYSTEM_CAPTURESTART, winevent_hook|wparam|lparam, 0, 0 },
7086     { WM_INITMENU, sent|defwinproc },
7087     { EVENT_SYSTEM_MENUSTART, winevent_hook|wparam|lparam, OBJID_SYSMENU, 0 },
7088     { WM_MENUSELECT, sent|defwinproc|wparam, MAKEWPARAM(0,MF_SYSMENU|MF_POPUP|MF_HILITE) },
7089     { EVENT_OBJECT_FOCUS, winevent_hook|wparam|lparam, OBJID_SYSMENU, 1 },
7090
7091     { HCBT_KEYSKIPPED, hook|wparam|lparam|optional, VK_F10, 0x10000001 }, /* XP */
7092
7093     { HCBT_KEYSKIPPED, hook|wparam|lparam|optional, VK_F10, 1 }, /* XP */
7094     { EVENT_OBJECT_FOCUS, winevent_hook|wparam|lparam, OBJID_SYSMENU, 0 },
7095     { EVENT_SYSTEM_CAPTUREEND, winevent_hook|wparam|lparam, 0, 0, },
7096     { WM_CAPTURECHANGED, sent|defwinproc },
7097     { WM_MENUSELECT, sent|defwinproc|wparam|optional, MAKEWPARAM(0,0xffff) },
7098     { EVENT_SYSTEM_MENUEND, winevent_hook|wparam|lparam, OBJID_SYSMENU, 0 },
7099     { WM_EXITMENULOOP, sent|defwinproc },
7100     { HCBT_KEYSKIPPED, hook|wparam|lparam|optional, VK_F10, 0xc0000001 }, /* XP */
7101     { WM_SYSKEYUP, wparam|lparam, VK_F10, 0xc0000001 },
7102     { WM_SYSKEYUP, sent|wparam|lparam, VK_F10, 0xc0000001 },
7103     { 0 }
7104 };
7105 static const struct message WmShiftF10Seq[] = {
7106     { HCBT_KEYSKIPPED, hook|wparam|lparam|optional, VK_SHIFT, 1 }, /* XP */
7107     { WM_KEYDOWN, wparam|lparam, VK_SHIFT, 1 },
7108     { WM_KEYDOWN, sent|wparam|lparam, VK_SHIFT, 0x00000001 },
7109     { HCBT_KEYSKIPPED, hook|wparam|lparam|optional, VK_F10, 1 }, /* XP */
7110     { WM_SYSKEYDOWN, wparam|lparam, VK_F10, 1 },
7111     { WM_SYSKEYDOWN, sent|wparam|lparam, VK_F10, 0x00000001 },
7112     { WM_CONTEXTMENU, sent|defwinproc|lparam, /*hwnd*/0, -1 },
7113     { HCBT_KEYSKIPPED, hook|wparam|lparam|optional, VK_F10, 0xc0000001 }, /* XP */
7114     { WM_SYSKEYUP, wparam|lparam, VK_F10, 0xc0000001 },
7115     { WM_SYSKEYUP, sent|wparam|lparam, VK_F10, 0xc0000001 },
7116     { WM_SYSCOMMAND, sent|defwinproc|wparam, SC_KEYMENU },
7117     { HCBT_SYSCOMMAND, hook },
7118     { WM_ENTERMENULOOP, sent|defwinproc|wparam|lparam, 0, 0 },
7119     { WM_INITMENU, sent|defwinproc },
7120     { WM_MENUSELECT, sent|defwinproc|wparam, MAKEWPARAM(0,MF_SYSMENU|MF_POPUP|MF_HILITE) },
7121     { HCBT_KEYSKIPPED, hook|wparam|lparam|optional, VK_SHIFT, 0xd0000001 }, /* XP */
7122     { HCBT_KEYSKIPPED, hook|wparam|lparam|optional, VK_ESCAPE, 0x10000001 }, /* XP */
7123     { WM_CAPTURECHANGED, sent|defwinproc|wparam|lparam, 0, 0 },
7124     { WM_MENUSELECT, sent|defwinproc|wparam|lparam, 0xffff0000, 0 },
7125     { WM_EXITMENULOOP, sent|defwinproc|wparam|lparam, 0, 0 },
7126     { HCBT_KEYSKIPPED, hook|wparam|lparam|optional, VK_ESCAPE, 0xc0000001 }, /* XP */
7127     { WM_KEYUP, wparam|lparam, VK_ESCAPE, 0xc0000001 },
7128     { WM_KEYUP, sent|wparam|lparam, VK_ESCAPE, 0xc0000001 },
7129     { 0 }
7130 };
7131
7132 static void pump_msg_loop(HWND hwnd, HACCEL hAccel)
7133 {
7134     MSG msg;
7135
7136     while (PeekMessageA(&msg, 0, 0, 0, PM_REMOVE))
7137     {
7138         struct recvd_message log_msg;
7139
7140         /* ignore some unwanted messages */
7141         if (msg.message == WM_MOUSEMOVE ||
7142             msg.message == WM_TIMER ||
7143             ignore_message( msg.message ))
7144             continue;
7145
7146         log_msg.hwnd = msg.hwnd;
7147         log_msg.message = msg.message;
7148         log_msg.flags = wparam|lparam;
7149         log_msg.wParam = msg.wParam;
7150         log_msg.lParam = msg.lParam;
7151         log_msg.descr = "accel";
7152         add_message(&log_msg);
7153
7154         if (!hAccel || !TranslateAccelerator(hwnd, hAccel, &msg))
7155         {
7156             TranslateMessage(&msg);
7157             DispatchMessage(&msg);
7158         }
7159     }
7160 }
7161
7162 static void test_accelerators(void)
7163 {
7164     RECT rc;
7165     POINT pt;
7166     SHORT state;
7167     HACCEL hAccel;
7168     HWND hwnd = CreateWindowExA(0, "TestWindowClass", NULL, WS_OVERLAPPEDWINDOW | WS_VISIBLE,
7169                                 100, 100, 200, 200, 0, 0, 0, NULL);
7170     BOOL ret;
7171
7172     assert(hwnd != 0);
7173     UpdateWindow(hwnd);
7174     flush_events();
7175     flush_sequence();
7176
7177     SetFocus(hwnd);
7178     ok(GetFocus() == hwnd, "wrong focus window %p\n", GetFocus());
7179
7180     state = GetKeyState(VK_SHIFT);
7181     ok(!(state & 0x8000), "wrong Shift state %04x\n", state);
7182     state = GetKeyState(VK_CAPITAL);
7183     ok(state == 0, "wrong CapsLock state %04x\n", state);
7184
7185     hAccel = LoadAccelerators(GetModuleHandleA(0), MAKEINTRESOURCE(1));
7186     assert(hAccel != 0);
7187
7188     flush_events();
7189     pump_msg_loop(hwnd, 0);
7190     flush_sequence();
7191
7192     trace("testing VK_N press/release\n");
7193     flush_sequence();
7194     keybd_event('N', 0, 0, 0);
7195     keybd_event('N', 0, KEYEVENTF_KEYUP, 0);
7196     pump_msg_loop(hwnd, hAccel);
7197     if (!sequence_cnt)  /* we didn't get any message */
7198     {
7199         skip( "queuing key events not supported\n" );
7200         goto done;
7201     }
7202     ok_sequence(WmVkN, "VK_N press/release", FALSE);
7203
7204     trace("testing Shift+VK_N press/release\n");
7205     flush_sequence();
7206     keybd_event(VK_SHIFT, 0, 0, 0);
7207     keybd_event('N', 0, 0, 0);
7208     keybd_event('N', 0, KEYEVENTF_KEYUP, 0);
7209     keybd_event(VK_SHIFT, 0, KEYEVENTF_KEYUP, 0);
7210     pump_msg_loop(hwnd, hAccel);
7211     ok_sequence(WmShiftVkN, "Shift+VK_N press/release", FALSE);
7212
7213     trace("testing Ctrl+VK_N press/release\n");
7214     flush_sequence();
7215     keybd_event(VK_CONTROL, 0, 0, 0);
7216     keybd_event('N', 0, 0, 0);
7217     keybd_event('N', 0, KEYEVENTF_KEYUP, 0);
7218     keybd_event(VK_CONTROL, 0, KEYEVENTF_KEYUP, 0);
7219     pump_msg_loop(hwnd, hAccel);
7220     ok_sequence(WmCtrlVkN, "Ctrl+VK_N press/release", FALSE);
7221
7222     trace("testing Alt+VK_N press/release\n");
7223     flush_sequence();
7224     keybd_event(VK_MENU, 0, 0, 0);
7225     keybd_event('N', 0, 0, 0);
7226     keybd_event('N', 0, KEYEVENTF_KEYUP, 0);
7227     keybd_event(VK_MENU, 0, KEYEVENTF_KEYUP, 0);
7228     pump_msg_loop(hwnd, hAccel);
7229     ok_sequence(WmAltVkN, "Alt+VK_N press/release", FALSE);
7230
7231     trace("testing Ctrl+Alt+VK_N press/release 1\n");
7232     flush_sequence();
7233     keybd_event(VK_CONTROL, 0, 0, 0);
7234     keybd_event(VK_MENU, 0, 0, 0);
7235     keybd_event('N', 0, 0, 0);
7236     keybd_event('N', 0, KEYEVENTF_KEYUP, 0);
7237     keybd_event(VK_MENU, 0, KEYEVENTF_KEYUP, 0);
7238     keybd_event(VK_CONTROL, 0, KEYEVENTF_KEYUP, 0);
7239     pump_msg_loop(hwnd, hAccel);
7240     ok_sequence(WmCtrlAltVkN, "Ctrl+Alt+VK_N press/release 1", FALSE);
7241
7242     ret = DestroyAcceleratorTable(hAccel);
7243     ok( ret, "DestroyAcceleratorTable error %d\n", GetLastError());
7244
7245     hAccel = LoadAccelerators(GetModuleHandleA(0), MAKEINTRESOURCE(2));
7246     assert(hAccel != 0);
7247
7248     trace("testing VK_N press/release\n");
7249     flush_sequence();
7250     keybd_event('N', 0, 0, 0);
7251     keybd_event('N', 0, KEYEVENTF_KEYUP, 0);
7252     pump_msg_loop(hwnd, hAccel);
7253     ok_sequence(WmVkN, "VK_N press/release", FALSE);
7254
7255     trace("testing Shift+VK_N press/release\n");
7256     flush_sequence();
7257     keybd_event(VK_SHIFT, 0, 0, 0);
7258     keybd_event('N', 0, 0, 0);
7259     keybd_event('N', 0, KEYEVENTF_KEYUP, 0);
7260     keybd_event(VK_SHIFT, 0, KEYEVENTF_KEYUP, 0);
7261     pump_msg_loop(hwnd, hAccel);
7262     ok_sequence(WmShiftVkN, "Shift+VK_N press/release", FALSE);
7263
7264     trace("testing Ctrl+VK_N press/release 2\n");
7265     flush_sequence();
7266     keybd_event(VK_CONTROL, 0, 0, 0);
7267     keybd_event('N', 0, 0, 0);
7268     keybd_event('N', 0, KEYEVENTF_KEYUP, 0);
7269     keybd_event(VK_CONTROL, 0, KEYEVENTF_KEYUP, 0);
7270     pump_msg_loop(hwnd, hAccel);
7271     ok_sequence(WmCtrlVkN_2, "Ctrl+VK_N press/release 2", FALSE);
7272
7273     trace("testing Alt+VK_N press/release 2\n");
7274     flush_sequence();
7275     keybd_event(VK_MENU, 0, 0, 0);
7276     keybd_event('N', 0, 0, 0);
7277     keybd_event('N', 0, KEYEVENTF_KEYUP, 0);
7278     keybd_event(VK_MENU, 0, KEYEVENTF_KEYUP, 0);
7279     pump_msg_loop(hwnd, hAccel);
7280     ok_sequence(WmAltVkN_2, "Alt+VK_N press/release 2", FALSE);
7281
7282     trace("testing Ctrl+Alt+VK_N press/release 2\n");
7283     flush_sequence();
7284     keybd_event(VK_CONTROL, 0, 0, 0);
7285     keybd_event(VK_MENU, 0, 0, 0);
7286     keybd_event('N', 0, 0, 0);
7287     keybd_event('N', 0, KEYEVENTF_KEYUP, 0);
7288     keybd_event(VK_MENU, 0, KEYEVENTF_KEYUP, 0);
7289     keybd_event(VK_CONTROL, 0, KEYEVENTF_KEYUP, 0);
7290     pump_msg_loop(hwnd, hAccel);
7291     ok_sequence(WmCtrlAltVkN, "Ctrl+Alt+VK_N press/release 2", FALSE);
7292
7293     trace("testing Ctrl+Shift+VK_N press/release\n");
7294     flush_sequence();
7295     keybd_event(VK_CONTROL, 0, 0, 0);
7296     keybd_event(VK_SHIFT, 0, 0, 0);
7297     keybd_event('N', 0, 0, 0);
7298     keybd_event('N', 0, KEYEVENTF_KEYUP, 0);
7299     keybd_event(VK_SHIFT, 0, KEYEVENTF_KEYUP, 0);
7300     keybd_event(VK_CONTROL, 0, KEYEVENTF_KEYUP, 0);
7301     pump_msg_loop(hwnd, hAccel);
7302     ok_sequence(WmCtrlShiftVkN, "Ctrl+Shift+VK_N press/release", FALSE);
7303
7304     trace("testing Ctrl+Alt+Shift+VK_N press/release\n");
7305     flush_sequence();
7306     keybd_event(VK_CONTROL, 0, 0, 0);
7307     keybd_event(VK_MENU, 0, 0, 0);
7308     keybd_event(VK_SHIFT, 0, 0, 0);
7309     keybd_event('N', 0, 0, 0);
7310     keybd_event('N', 0, KEYEVENTF_KEYUP, 0);
7311     keybd_event(VK_SHIFT, 0, KEYEVENTF_KEYUP, 0);
7312     keybd_event(VK_MENU, 0, KEYEVENTF_KEYUP, 0);
7313     keybd_event(VK_CONTROL, 0, KEYEVENTF_KEYUP, 0);
7314     pump_msg_loop(hwnd, hAccel);
7315     ok_sequence(WmCtrlAltShiftVkN, "Ctrl+Alt+Shift+VK_N press/release", FALSE);
7316
7317     ret = DestroyAcceleratorTable(hAccel);
7318     ok( ret, "DestroyAcceleratorTable error %d\n", GetLastError());
7319     hAccel = 0;
7320
7321     trace("testing Alt press/release\n");
7322     flush_sequence();
7323     keybd_event(VK_MENU, 0, 0, 0);
7324     keybd_event(VK_MENU, 0, KEYEVENTF_KEYUP, 0);
7325     keybd_event(VK_MENU, 0, 0, 0);
7326     keybd_event(VK_MENU, 0, KEYEVENTF_KEYUP, 0);
7327     pump_msg_loop(hwnd, 0);
7328     /* this test doesn't pass in Wine for managed windows */
7329     ok_sequence(WmAltPressRelease, "Alt press/release", TRUE);
7330
7331     trace("testing VK_F1 press/release\n");
7332     keybd_event(VK_F1, 0, 0, 0);
7333     keybd_event(VK_F1, 0, KEYEVENTF_KEYUP, 0);
7334     pump_msg_loop(hwnd, 0);
7335     ok_sequence(WmF1Seq, "F1 press/release", FALSE);
7336
7337     trace("testing VK_APPS press/release\n");
7338     keybd_event(VK_APPS, 0, 0, 0);
7339     keybd_event(VK_APPS, 0, KEYEVENTF_KEYUP, 0);
7340     pump_msg_loop(hwnd, 0);
7341     ok_sequence(WmVkAppsSeq, "VK_APPS press/release", FALSE);
7342
7343     trace("testing VK_F10 press/release\n");
7344     keybd_event(VK_F10, 0, 0, 0);
7345     keybd_event(VK_F10, 0, KEYEVENTF_KEYUP, 0);
7346     keybd_event(VK_F10, 0, 0, 0);
7347     keybd_event(VK_F10, 0, KEYEVENTF_KEYUP, 0);
7348     pump_msg_loop(hwnd, 0);
7349     ok_sequence(WmVkF10Seq, "VK_F10 press/release", TRUE);
7350
7351     trace("testing SHIFT+F10 press/release\n");
7352     keybd_event(VK_SHIFT, 0, 0, 0);
7353     keybd_event(VK_F10, 0, 0, 0);
7354     keybd_event(VK_F10, 0, KEYEVENTF_KEYUP, 0);
7355     keybd_event(VK_SHIFT, 0, KEYEVENTF_KEYUP, 0);
7356     keybd_event(VK_ESCAPE, 0, 0, 0);
7357     keybd_event(VK_ESCAPE, 0, KEYEVENTF_KEYUP, 0);
7358     pump_msg_loop(hwnd, 0);
7359     ok_sequence(WmShiftF10Seq, "SHIFT+F10 press/release", TRUE);
7360
7361     trace("testing Shift+MouseButton press/release\n");
7362     /* first, move mouse pointer inside of the window client area */
7363     GetClientRect(hwnd, &rc);
7364     MapWindowPoints(hwnd, 0, (LPPOINT)&rc, 2);
7365     rc.left += (rc.right - rc.left)/2;
7366     rc.top += (rc.bottom - rc.top)/2;
7367     SetCursorPos(rc.left, rc.top);
7368     SetActiveWindow(hwnd);
7369
7370     flush_events();
7371     flush_sequence();
7372     GetCursorPos(&pt);
7373     if (pt.x == rc.left && pt.y == rc.top)
7374     {
7375         int i;
7376         keybd_event(VK_SHIFT, 0, 0, 0);
7377         mouse_event(MOUSEEVENTF_LEFTDOWN, 0, 0, 0, 0);
7378         mouse_event(MOUSEEVENTF_LEFTUP, 0, 0, 0, 0);
7379         keybd_event(VK_SHIFT, 0, KEYEVENTF_KEYUP, 0);
7380         pump_msg_loop(hwnd, 0);
7381         for (i = 0; i < sequence_cnt; i++) if (sequence[i].message == WM_LBUTTONDOWN) break;
7382         if (i < sequence_cnt)
7383             ok_sequence(WmShiftMouseButton, "Shift+MouseButton press/release", FALSE);
7384         else
7385             skip( "Shift+MouseButton event didn't get to the window\n" );
7386     }
7387
7388 done:
7389     if (hAccel) DestroyAcceleratorTable(hAccel);
7390     DestroyWindow(hwnd);
7391 }
7392
7393 /************* window procedures ********************/
7394
7395 static LRESULT MsgCheckProc (BOOL unicode, HWND hwnd, UINT message, 
7396                              WPARAM wParam, LPARAM lParam)
7397 {
7398     static LONG defwndproc_counter = 0;
7399     static LONG beginpaint_counter = 0;
7400     LRESULT ret;
7401     struct recvd_message msg;
7402
7403     if (ignore_message( message )) return 0;
7404
7405     switch (message)
7406     {
7407         case WM_ENABLE:
7408         {
7409             LONG style = GetWindowLongA(hwnd, GWL_STYLE);
7410             ok((BOOL)wParam == !(style & WS_DISABLED),
7411                 "wrong WS_DISABLED state: %ld != %d\n", wParam, !(style & WS_DISABLED));
7412             break;
7413         }
7414
7415         case WM_CAPTURECHANGED:
7416             if (test_DestroyWindow_flag)
7417             {
7418                 DWORD style = GetWindowLongA(hwnd, GWL_STYLE);
7419                 if (style & WS_CHILD)
7420                     lParam = GetWindowLongPtrA(hwnd, GWLP_ID);
7421                 else if (style & WS_POPUP)
7422                     lParam = WND_POPUP_ID;
7423                 else
7424                     lParam = WND_PARENT_ID;
7425             }
7426             break;
7427
7428         case WM_NCDESTROY:
7429         {
7430             HWND capture;
7431
7432             ok(!GetWindow(hwnd, GW_CHILD), "children should be unlinked at this point\n");
7433             capture = GetCapture();
7434             if (capture)
7435             {
7436                 ok(capture == hwnd, "capture should NOT be released at this point (capture %p)\n", capture);
7437                 trace("current capture %p, releasing...\n", capture);
7438                 ReleaseCapture();
7439             }
7440         }
7441         /* fall through */
7442         case WM_DESTROY:
7443             if (pGetAncestor)
7444                 ok(pGetAncestor(hwnd, GA_PARENT) != 0, "parent should NOT be unlinked at this point\n");
7445             if (test_DestroyWindow_flag)
7446             {
7447                 DWORD style = GetWindowLongA(hwnd, GWL_STYLE);
7448                 if (style & WS_CHILD)
7449                     lParam = GetWindowLongPtrA(hwnd, GWLP_ID);
7450                 else if (style & WS_POPUP)
7451                     lParam = WND_POPUP_ID;
7452                 else
7453                     lParam = WND_PARENT_ID;
7454             }
7455             break;
7456
7457         /* test_accelerators() depends on this */
7458         case WM_NCHITTEST:
7459             return HTCLIENT;
7460
7461         /* ignore */
7462         case WM_MOUSEMOVE:
7463         case WM_MOUSEACTIVATE:
7464         case WM_NCMOUSEMOVE:
7465         case WM_SETCURSOR:
7466         case WM_IME_SELECT:
7467             return 0;
7468     }
7469
7470     msg.hwnd = hwnd;
7471     msg.message = message;
7472     msg.flags = sent|wparam|lparam;
7473     if (defwndproc_counter) msg.flags |= defwinproc;
7474     if (beginpaint_counter) msg.flags |= beginpaint;
7475     msg.wParam = wParam;
7476     msg.lParam = lParam;
7477     msg.descr = "MsgCheckProc";
7478     add_message(&msg);
7479
7480     if (message == WM_GETMINMAXINFO && (GetWindowLongA(hwnd, GWL_STYLE) & WS_CHILD))
7481     {
7482         HWND parent = GetParent(hwnd);
7483         RECT rc;
7484         MINMAXINFO *minmax = (MINMAXINFO *)lParam;
7485
7486         GetClientRect(parent, &rc);
7487         trace("parent %p client size = (%d x %d)\n", parent, rc.right, rc.bottom);
7488         trace("Reserved=%d,%d MaxSize=%d,%d MaxPos=%d,%d MinTrack=%d,%d MaxTrack=%d,%d\n",
7489               minmax->ptReserved.x, minmax->ptReserved.y,
7490               minmax->ptMaxSize.x, minmax->ptMaxSize.y,
7491               minmax->ptMaxPosition.x, minmax->ptMaxPosition.y,
7492               minmax->ptMinTrackSize.x, minmax->ptMinTrackSize.y,
7493               minmax->ptMaxTrackSize.x, minmax->ptMaxTrackSize.y);
7494
7495         ok(minmax->ptMaxSize.x == rc.right, "default width of maximized child %d != %d\n",
7496            minmax->ptMaxSize.x, rc.right);
7497         ok(minmax->ptMaxSize.y == rc.bottom, "default height of maximized child %d != %d\n",
7498            minmax->ptMaxSize.y, rc.bottom);
7499     }
7500
7501     if (message == WM_PAINT)
7502     {
7503         PAINTSTRUCT ps;
7504         beginpaint_counter++;
7505         BeginPaint( hwnd, &ps );
7506         beginpaint_counter--;
7507         EndPaint( hwnd, &ps );
7508         return 0;
7509     }
7510
7511     defwndproc_counter++;
7512     ret = unicode ? DefWindowProcW(hwnd, message, wParam, lParam) 
7513                   : DefWindowProcA(hwnd, message, wParam, lParam);
7514     defwndproc_counter--;
7515
7516     return ret;
7517 }
7518
7519 static LRESULT WINAPI MsgCheckProcA(HWND hwnd, UINT message, WPARAM wParam, LPARAM lParam)
7520 {
7521     return MsgCheckProc (FALSE, hwnd, message, wParam, lParam);
7522 }
7523
7524 static LRESULT WINAPI MsgCheckProcW(HWND hwnd, UINT message, WPARAM wParam, LPARAM lParam)
7525 {
7526     return MsgCheckProc (TRUE, hwnd, message, wParam, lParam);
7527 }
7528
7529 static LRESULT WINAPI PopupMsgCheckProcA(HWND hwnd, UINT message, WPARAM wParam, LPARAM lParam)
7530 {
7531     static LONG defwndproc_counter = 0;
7532     LRESULT ret;
7533     struct recvd_message msg;
7534
7535     if (ignore_message( message )) return 0;
7536
7537     switch (message)
7538     {
7539     case WM_QUERYENDSESSION:
7540     case WM_ENDSESSION:
7541         lParam &= ~0x01;  /* Vista adds a 0x01 flag */
7542         break;
7543     }
7544
7545     msg.hwnd = hwnd;
7546     msg.message = message;
7547     msg.flags = sent|wparam|lparam;
7548     if (defwndproc_counter) msg.flags |= defwinproc;
7549     msg.wParam = wParam;
7550     msg.lParam = lParam;
7551     msg.descr = "popup";
7552     add_message(&msg);
7553
7554     if (message == WM_CREATE)
7555     {
7556         DWORD style = GetWindowLongA(hwnd, GWL_STYLE) | WS_VISIBLE;
7557         SetWindowLongA(hwnd, GWL_STYLE, style);
7558     }
7559
7560     defwndproc_counter++;
7561     ret = DefWindowProcA(hwnd, message, wParam, lParam);
7562     defwndproc_counter--;
7563
7564     return ret;
7565 }
7566
7567 static LRESULT WINAPI ParentMsgCheckProcA(HWND hwnd, UINT message, WPARAM wParam, LPARAM lParam)
7568 {
7569     static LONG defwndproc_counter = 0;
7570     static LONG beginpaint_counter = 0;
7571     LRESULT ret;
7572     struct recvd_message msg;
7573
7574     if (ignore_message( message )) return 0;
7575
7576     if (log_all_parent_messages ||
7577         message == WM_PARENTNOTIFY || message == WM_CANCELMODE ||
7578         message == WM_SETFOCUS || message == WM_KILLFOCUS ||
7579         message == WM_ENABLE || message == WM_ENTERIDLE ||
7580         message == WM_DRAWITEM || message == WM_COMMAND ||
7581         message == WM_IME_SETCONTEXT)
7582     {
7583         switch (message)
7584         {
7585             /* ignore */
7586             case WM_NCHITTEST:
7587                 return HTCLIENT;
7588             case WM_SETCURSOR:
7589             case WM_MOUSEMOVE:
7590             case WM_NCMOUSEMOVE:
7591                 return 0;
7592
7593             case WM_ERASEBKGND:
7594             {
7595                 RECT rc;
7596                 INT ret = GetClipBox((HDC)wParam, &rc);
7597
7598                 trace("WM_ERASEBKGND: GetClipBox()=%d, (%d,%d-%d,%d)\n",
7599                        ret, rc.left, rc.top, rc.right, rc.bottom);
7600                 break;
7601             }
7602         }
7603
7604         msg.hwnd = hwnd;
7605         msg.message = message;
7606         msg.flags = sent|parent|wparam|lparam;
7607         if (defwndproc_counter) msg.flags |= defwinproc;
7608         if (beginpaint_counter) msg.flags |= beginpaint;
7609         msg.wParam = wParam;
7610         msg.lParam = lParam;
7611         msg.descr = "parent";
7612         add_message(&msg);
7613     }
7614
7615     if (message == WM_PAINT)
7616     {
7617         PAINTSTRUCT ps;
7618         beginpaint_counter++;
7619         BeginPaint( hwnd, &ps );
7620         beginpaint_counter--;
7621         EndPaint( hwnd, &ps );
7622         return 0;
7623     }
7624
7625     defwndproc_counter++;
7626     ret = DefWindowProcA(hwnd, message, wParam, lParam);
7627     defwndproc_counter--;
7628
7629     return ret;
7630 }
7631
7632 static INT_PTR CALLBACK StopQuitMsgCheckProcA(HWND hwnd, UINT message, WPARAM wp, LPARAM lp)
7633 {
7634     if (message == WM_CREATE)
7635         PostMessage(hwnd, WM_CLOSE, 0, 0);
7636     else if (message == WM_CLOSE)
7637     {
7638         /* Only the first WM_QUIT will survive the window destruction */
7639         PostMessage(hwnd, WM_USER, 0x1234, 0x5678);
7640         PostMessage(hwnd, WM_QUIT, 0x1234, 0x5678);
7641         PostMessage(hwnd, WM_QUIT, 0x4321, 0x8765);
7642     }
7643
7644     return DefWindowProcA(hwnd, message, wp, lp);
7645 }
7646
7647 static LRESULT WINAPI TestDlgProcA(HWND hwnd, UINT message, WPARAM wParam, LPARAM lParam)
7648 {
7649     static LONG defwndproc_counter = 0;
7650     LRESULT ret;
7651     struct recvd_message msg;
7652
7653     if (ignore_message( message )) return 0;
7654
7655     if (test_def_id)
7656     {
7657         DefDlgProcA(hwnd, DM_SETDEFID, 1, 0);
7658         ret = DefDlgProcA(hwnd, DM_GETDEFID, 0, 0);
7659         if (after_end_dialog)
7660             ok( ret == 0, "DM_GETDEFID should return 0 after EndDialog, got %lx\n", ret );
7661         else
7662             ok(HIWORD(ret) == DC_HASDEFID, "DM_GETDEFID should return DC_HASDEFID, got %lx\n", ret);
7663     }
7664
7665     msg.hwnd = hwnd;
7666     msg.message = message;
7667     msg.flags = sent|wparam|lparam;
7668     if (defwndproc_counter) msg.flags |= defwinproc;
7669     msg.wParam = wParam;
7670     msg.lParam = lParam;
7671     msg.descr = "dialog";
7672     add_message(&msg);
7673
7674     defwndproc_counter++;
7675     ret = DefDlgProcA(hwnd, message, wParam, lParam);
7676     defwndproc_counter--;
7677
7678     return ret;
7679 }
7680
7681 static LRESULT WINAPI ShowWindowProcA(HWND hwnd, UINT message, WPARAM wParam, LPARAM lParam)
7682 {
7683     static LONG defwndproc_counter = 0;
7684     LRESULT ret;
7685     struct recvd_message msg;
7686
7687     /* log only specific messages we are interested in */
7688     switch (message)
7689     {
7690 #if 0 /* probably log these as well */
7691     case WM_ACTIVATE:
7692     case WM_SETFOCUS:
7693     case WM_KILLFOCUS:
7694 #endif
7695     case WM_SHOWWINDOW:
7696     case WM_SIZE:
7697     case WM_MOVE:
7698     case WM_GETMINMAXINFO:
7699     case WM_WINDOWPOSCHANGING:
7700     case WM_WINDOWPOSCHANGED:
7701         break;
7702
7703     default: /* ignore */
7704         /*trace("showwindow: %p, %04x, %08x, %08lx\n", hwnd, message, wParam, lParam);*/
7705         return DefWindowProcA(hwnd, message, wParam, lParam);
7706     }
7707
7708     msg.hwnd = hwnd;
7709     msg.message = message;
7710     msg.flags = sent|wparam|lparam;
7711     if (defwndproc_counter) msg.flags |= defwinproc;
7712     msg.wParam = wParam;
7713     msg.lParam = lParam;
7714     msg.descr = "show";
7715     add_message(&msg);
7716
7717     defwndproc_counter++;
7718     ret = DefWindowProcA(hwnd, message, wParam, lParam);
7719     defwndproc_counter--;
7720
7721     return ret;
7722 }
7723
7724 static LRESULT WINAPI PaintLoopProcA(HWND hWnd, UINT msg, WPARAM wParam, LPARAM lParam)
7725 {
7726     switch (msg)
7727     {
7728         case WM_CREATE: return 0;
7729         case WM_PAINT:
7730         {
7731             MSG msg2;
7732             static int i = 0;
7733
7734             if (i < 256)
7735             {
7736                 i++;
7737                 if (PeekMessageA(&msg2, 0, 0, 0, 1))
7738                 {
7739                     TranslateMessage(&msg2);
7740                     DispatchMessage(&msg2);
7741                 }
7742                 i--;
7743             }
7744             else ok(broken(1), "infinite loop\n");
7745             if ( i == 0)
7746                 paint_loop_done = 1;
7747             return DefWindowProcA(hWnd,msg,wParam,lParam);
7748         }
7749     }
7750     return DefWindowProcA(hWnd,msg,wParam,lParam);
7751 }
7752
7753 static LRESULT WINAPI HotkeyMsgCheckProcA(HWND hwnd, UINT message, WPARAM wParam, LPARAM lParam)
7754 {
7755     static LONG defwndproc_counter = 0;
7756     LRESULT ret;
7757     struct recvd_message msg;
7758     DWORD queue_status;
7759
7760     if (ignore_message( message )) return 0;
7761
7762     if ((message >= WM_KEYFIRST && message <= WM_KEYLAST) ||
7763         message == WM_HOTKEY || message >= WM_APP)
7764     {
7765         msg.hwnd = hwnd;
7766         msg.message = message;
7767         msg.flags = sent|wparam|lparam;
7768         if (defwndproc_counter) msg.flags |= defwinproc;
7769         msg.wParam = wParam;
7770         msg.lParam = lParam;
7771         msg.descr = "HotkeyMsgCheckProcA";
7772         add_message(&msg);
7773     }
7774
7775     defwndproc_counter++;
7776     ret = DefWindowProcA(hwnd, message, wParam, lParam);
7777     defwndproc_counter--;
7778
7779     if (message == WM_APP)
7780     {
7781         queue_status = GetQueueStatus(QS_HOTKEY);
7782         ok((queue_status & (QS_HOTKEY << 16)) == QS_HOTKEY << 16, "expected QS_HOTKEY << 16 set, got %x\n", queue_status);
7783         queue_status = GetQueueStatus(QS_POSTMESSAGE);
7784         ok((queue_status & (QS_POSTMESSAGE << 16)) == QS_POSTMESSAGE << 16, "expected QS_POSTMESSAGE << 16 set, got %x\n", queue_status);
7785         PostMessageA(hwnd, WM_APP+1, 0, 0);
7786     }
7787     else if (message == WM_APP+1)
7788     {
7789         queue_status = GetQueueStatus(QS_HOTKEY);
7790         ok((queue_status & (QS_HOTKEY << 16)) == 0, "expected QS_HOTKEY << 16 cleared, got %x\n", queue_status);
7791     }
7792
7793     return ret;
7794 }
7795
7796 static BOOL RegisterWindowClasses(void)
7797 {
7798     WNDCLASSA cls;
7799     WNDCLASSW clsW;
7800
7801     cls.style = 0;
7802     cls.lpfnWndProc = MsgCheckProcA;
7803     cls.cbClsExtra = 0;
7804     cls.cbWndExtra = 0;
7805     cls.hInstance = GetModuleHandleA(0);
7806     cls.hIcon = 0;
7807     cls.hCursor = LoadCursorA(0, IDC_ARROW);
7808     cls.hbrBackground = GetStockObject(WHITE_BRUSH);
7809     cls.lpszMenuName = NULL;
7810     cls.lpszClassName = "TestWindowClass";
7811     if(!RegisterClassA(&cls)) return FALSE;
7812
7813     cls.lpfnWndProc = HotkeyMsgCheckProcA;
7814     cls.lpszClassName = "HotkeyWindowClass";
7815     if(!RegisterClassA(&cls)) return FALSE;
7816
7817     cls.lpfnWndProc = ShowWindowProcA;
7818     cls.lpszClassName = "ShowWindowClass";
7819     if(!RegisterClassA(&cls)) return FALSE;
7820
7821     cls.lpfnWndProc = PopupMsgCheckProcA;
7822     cls.lpszClassName = "TestPopupClass";
7823     if(!RegisterClassA(&cls)) return FALSE;
7824
7825     cls.lpfnWndProc = ParentMsgCheckProcA;
7826     cls.lpszClassName = "TestParentClass";
7827     if(!RegisterClassA(&cls)) return FALSE;
7828
7829     cls.lpfnWndProc = StopQuitMsgCheckProcA;
7830     cls.lpszClassName = "StopQuitClass";
7831     if(!RegisterClassA(&cls)) return FALSE;
7832
7833     cls.lpfnWndProc = DefWindowProcA;
7834     cls.lpszClassName = "SimpleWindowClass";
7835     if(!RegisterClassA(&cls)) return FALSE;
7836
7837     cls.lpfnWndProc = PaintLoopProcA;
7838     cls.lpszClassName = "PaintLoopWindowClass";
7839     if(!RegisterClassA(&cls)) return FALSE;
7840
7841     cls.style = CS_NOCLOSE;
7842     cls.lpszClassName = "NoCloseWindowClass";
7843     if(!RegisterClassA(&cls)) return FALSE;
7844
7845     ok(GetClassInfoA(0, "#32770", &cls), "GetClassInfo failed\n");
7846     cls.style = 0;
7847     cls.hInstance = GetModuleHandleA(0);
7848     cls.hbrBackground = 0;
7849     cls.lpfnWndProc = TestDlgProcA;
7850     cls.lpszClassName = "TestDialogClass";
7851     if(!RegisterClassA(&cls)) return FALSE;
7852
7853     clsW.style = 0;
7854     clsW.lpfnWndProc = MsgCheckProcW;
7855     clsW.cbClsExtra = 0;
7856     clsW.cbWndExtra = 0;
7857     clsW.hInstance = GetModuleHandleW(0);
7858     clsW.hIcon = 0;
7859     clsW.hCursor = LoadCursorW(0, (LPWSTR)IDC_ARROW);
7860     clsW.hbrBackground = GetStockObject(WHITE_BRUSH);
7861     clsW.lpszMenuName = NULL;
7862     clsW.lpszClassName = testWindowClassW;
7863     RegisterClassW(&clsW);  /* ignore error, this fails on Win9x */
7864
7865     return TRUE;
7866 }
7867
7868 static BOOL is_our_logged_class(HWND hwnd)
7869 {
7870     char buf[256];
7871
7872     if (GetClassNameA(hwnd, buf, sizeof(buf)))
7873     {
7874         if (!lstrcmpiA(buf, "TestWindowClass") ||
7875             !lstrcmpiA(buf, "ShowWindowClass") ||
7876             !lstrcmpiA(buf, "TestParentClass") ||
7877             !lstrcmpiA(buf, "TestPopupClass") ||
7878             !lstrcmpiA(buf, "SimpleWindowClass") ||
7879             !lstrcmpiA(buf, "TestDialogClass") ||
7880             !lstrcmpiA(buf, "MDI_frame_class") ||
7881             !lstrcmpiA(buf, "MDI_client_class") ||
7882             !lstrcmpiA(buf, "MDI_child_class") ||
7883             !lstrcmpiA(buf, "my_button_class") ||
7884             !lstrcmpiA(buf, "my_edit_class") ||
7885             !lstrcmpiA(buf, "static") ||
7886             !lstrcmpiA(buf, "ListBox") ||
7887             !lstrcmpiA(buf, "ComboBox") ||
7888             !lstrcmpiA(buf, "MyDialogClass") ||
7889             !lstrcmpiA(buf, "#32770") ||
7890             !lstrcmpiA(buf, "#32768"))
7891         return TRUE;
7892     }
7893     return FALSE;
7894 }
7895
7896 static LRESULT CALLBACK cbt_hook_proc(int nCode, WPARAM wParam, LPARAM lParam) 
7897
7898     HWND hwnd;
7899
7900     ok(cbt_hook_thread_id == GetCurrentThreadId(), "we didn't ask for events from other threads\n");
7901
7902     if (nCode == HCBT_CLICKSKIPPED)
7903     {
7904         /* ignore this event, XP sends it a lot when switching focus between windows */
7905         return CallNextHookEx(hCBT_hook, nCode, wParam, lParam);
7906     }
7907
7908     if (nCode == HCBT_SYSCOMMAND || nCode == HCBT_KEYSKIPPED)
7909     {
7910         struct recvd_message msg;
7911
7912         msg.hwnd = 0;
7913         msg.message = nCode;
7914         msg.flags = hook|wparam|lparam;
7915         msg.wParam = wParam;
7916         msg.lParam = lParam;
7917         msg.descr = "CBT";
7918         add_message(&msg);
7919
7920         return CallNextHookEx(hCBT_hook, nCode, wParam, lParam);
7921     }
7922
7923     if (nCode == HCBT_DESTROYWND)
7924     {
7925         if (test_DestroyWindow_flag)
7926         {
7927             DWORD style = GetWindowLongA((HWND)wParam, GWL_STYLE);
7928             if (style & WS_CHILD)
7929                 lParam = GetWindowLongPtrA((HWND)wParam, GWLP_ID);
7930             else if (style & WS_POPUP)
7931                 lParam = WND_POPUP_ID;
7932             else
7933                 lParam = WND_PARENT_ID;
7934         }
7935     }
7936
7937     /* Log also SetFocus(0) calls */
7938     hwnd = wParam ? (HWND)wParam : (HWND)lParam;
7939
7940     if (is_our_logged_class(hwnd))
7941     {
7942         struct recvd_message msg;
7943
7944         msg.hwnd = hwnd;
7945         msg.message = nCode;
7946         msg.flags = hook|wparam|lparam;
7947         msg.wParam = wParam;
7948         msg.lParam = lParam;
7949         msg.descr = "CBT";
7950         add_message(&msg);
7951     }
7952     return CallNextHookEx(hCBT_hook, nCode, wParam, lParam);
7953 }
7954
7955 static void CALLBACK win_event_proc(HWINEVENTHOOK hevent,
7956                                     DWORD event,
7957                                     HWND hwnd,
7958                                     LONG object_id,
7959                                     LONG child_id,
7960                                     DWORD thread_id,
7961                                     DWORD event_time)
7962 {
7963     ok(thread_id == GetCurrentThreadId(), "we didn't ask for events from other threads\n");
7964
7965     /* ignore mouse cursor events */
7966     if (object_id == OBJID_CURSOR) return;
7967
7968     if (!hwnd || is_our_logged_class(hwnd))
7969     {
7970         struct recvd_message msg;
7971
7972         msg.hwnd = hwnd;
7973         msg.message = event;
7974         msg.flags = winevent_hook|wparam|lparam;
7975         msg.wParam = object_id;
7976         msg.lParam = child_id;
7977         msg.descr = "WEH";
7978         add_message(&msg);
7979     }
7980 }
7981
7982 static const WCHAR wszUnicode[] = {'U','n','i','c','o','d','e',0};
7983 static const WCHAR wszAnsi[] = {'U',0};
7984
7985 static LRESULT CALLBACK MsgConversionProcW(HWND hwnd, UINT uMsg, WPARAM wParam, LPARAM lParam)
7986 {
7987     switch (uMsg)
7988     {
7989     case CB_FINDSTRINGEXACT:
7990         trace("String: %p\n", (LPCWSTR)lParam);
7991         if (!lstrcmpW((LPCWSTR)lParam, wszUnicode))
7992             return 1;
7993         if (!lstrcmpW((LPCWSTR)lParam, wszAnsi))
7994             return 0;
7995         return -1;
7996     }
7997     return DefWindowProcW(hwnd, uMsg, wParam, lParam);
7998 }
7999
8000 static const struct message WmGetTextLengthAfromW[] = {
8001     { WM_GETTEXTLENGTH, sent },
8002     { WM_GETTEXT, sent|optional },
8003     { 0 }
8004 };
8005
8006 static const WCHAR dummy_window_text[] = {'d','u','m','m','y',' ','t','e','x','t',0};
8007
8008 /* dummy window proc for WM_GETTEXTLENGTH test */
8009 static LRESULT CALLBACK get_text_len_proc( HWND hwnd, UINT msg, WPARAM wp, LPARAM lp )
8010 {
8011     switch(msg)
8012     {
8013     case WM_GETTEXTLENGTH:
8014         return lstrlenW(dummy_window_text) + 37;  /* some random length */
8015     case WM_GETTEXT:
8016         lstrcpynW( (LPWSTR)lp, dummy_window_text, wp );
8017         return lstrlenW( (LPWSTR)lp );
8018     default:
8019         return DefWindowProcW( hwnd, msg, wp, lp );
8020     }
8021 }
8022
8023 static void test_message_conversion(void)
8024 {
8025     static const WCHAR wszMsgConversionClass[] =
8026         {'M','s','g','C','o','n','v','e','r','s','i','o','n','C','l','a','s','s',0};
8027     WNDCLASSW cls;
8028     LRESULT lRes;
8029     HWND hwnd;
8030     WNDPROC wndproc, newproc;
8031     BOOL ret;
8032
8033     cls.style = 0;
8034     cls.lpfnWndProc = MsgConversionProcW;
8035     cls.cbClsExtra = 0;
8036     cls.cbWndExtra = 0;
8037     cls.hInstance = GetModuleHandleW(NULL);
8038     cls.hIcon = NULL;
8039     cls.hCursor = LoadCursorW(NULL, (LPWSTR)IDC_ARROW);
8040     cls.hbrBackground = (HBRUSH)(COLOR_BTNFACE+1);
8041     cls.lpszMenuName = NULL;
8042     cls.lpszClassName = wszMsgConversionClass;
8043     /* this call will fail on Win9x, but that doesn't matter as this test is
8044      * meaningless on those platforms */
8045     if(!RegisterClassW(&cls)) return;
8046
8047     hwnd = CreateWindowExW(0, wszMsgConversionClass, NULL, WS_OVERLAPPED,
8048                            100, 100, 200, 200, 0, 0, 0, NULL);
8049     ok(hwnd != NULL, "Window creation failed\n");
8050
8051     /* {W, A} -> A */
8052
8053     wndproc = (WNDPROC)GetWindowLongPtrA(hwnd, GWLP_WNDPROC);
8054     lRes = CallWindowProcA(wndproc, hwnd, CB_FINDSTRINGEXACT, 0, (LPARAM)wszUnicode);
8055     ok(lRes == 0, "String should have been converted\n");
8056     lRes = CallWindowProcW(wndproc, hwnd, CB_FINDSTRINGEXACT, 0, (LPARAM)wszUnicode);
8057     ok(lRes == 1, "String shouldn't have been converted\n");
8058
8059     /* {W, A} -> W */
8060
8061     wndproc = (WNDPROC)GetWindowLongPtrW(hwnd, GWLP_WNDPROC);
8062     lRes = CallWindowProcA(wndproc, hwnd, CB_FINDSTRINGEXACT, 0, (LPARAM)wszUnicode);
8063     ok(lRes == 1, "String shouldn't have been converted\n");
8064     lRes = CallWindowProcW(wndproc, hwnd, CB_FINDSTRINGEXACT, 0, (LPARAM)wszUnicode);
8065     ok(lRes == 1, "String shouldn't have been converted\n");
8066
8067     /* Synchronous messages */
8068
8069     lRes = SendMessageA(hwnd, CB_FINDSTRINGEXACT, 0, (LPARAM)wszUnicode);
8070     ok(lRes == 0, "String should have been converted\n");
8071     lRes = SendMessageW(hwnd, CB_FINDSTRINGEXACT, 0, (LPARAM)wszUnicode);
8072     ok(lRes == 1, "String shouldn't have been converted\n");
8073
8074     /* Asynchronous messages */
8075
8076     SetLastError(0);
8077     lRes = PostMessageA(hwnd, CB_FINDSTRINGEXACT, 0, (LPARAM)wszUnicode);
8078     ok(lRes == 0 && (GetLastError() == ERROR_MESSAGE_SYNC_ONLY || GetLastError() == ERROR_INVALID_PARAMETER),
8079         "PostMessage on sync only message returned %ld, last error %d\n", lRes, GetLastError());
8080     SetLastError(0);
8081     lRes = PostMessageW(hwnd, CB_FINDSTRINGEXACT, 0, (LPARAM)wszUnicode);
8082     ok(lRes == 0 && (GetLastError() == ERROR_MESSAGE_SYNC_ONLY || GetLastError() == ERROR_INVALID_PARAMETER),
8083         "PostMessage on sync only message returned %ld, last error %d\n", lRes, GetLastError());
8084     SetLastError(0);
8085     lRes = PostThreadMessageA(GetCurrentThreadId(), CB_FINDSTRINGEXACT, 0, (LPARAM)wszUnicode);
8086     ok(lRes == 0 && (GetLastError() == ERROR_MESSAGE_SYNC_ONLY || GetLastError() == ERROR_INVALID_PARAMETER),
8087         "PosThreadtMessage on sync only message returned %ld, last error %d\n", lRes, GetLastError());
8088     SetLastError(0);
8089     lRes = PostThreadMessageW(GetCurrentThreadId(), CB_FINDSTRINGEXACT, 0, (LPARAM)wszUnicode);
8090     ok(lRes == 0 && (GetLastError() == ERROR_MESSAGE_SYNC_ONLY || GetLastError() == ERROR_INVALID_PARAMETER),
8091         "PosThreadtMessage on sync only message returned %ld, last error %d\n", lRes, GetLastError());
8092     SetLastError(0);
8093     lRes = SendNotifyMessageA(hwnd, CB_FINDSTRINGEXACT, 0, (LPARAM)wszUnicode);
8094     ok(lRes == 0 && (GetLastError() == ERROR_MESSAGE_SYNC_ONLY || GetLastError() == ERROR_INVALID_PARAMETER),
8095         "SendNotifyMessage on sync only message returned %ld, last error %d\n", lRes, GetLastError());
8096     SetLastError(0);
8097     lRes = SendNotifyMessageW(hwnd, CB_FINDSTRINGEXACT, 0, (LPARAM)wszUnicode);
8098     ok(lRes == 0 && (GetLastError() == ERROR_MESSAGE_SYNC_ONLY || GetLastError() == ERROR_INVALID_PARAMETER),
8099         "SendNotifyMessage on sync only message returned %ld, last error %d\n", lRes, GetLastError());
8100     SetLastError(0);
8101     lRes = SendMessageCallbackA(hwnd, CB_FINDSTRINGEXACT, 0, (LPARAM)wszUnicode, NULL, 0);
8102     ok(lRes == 0 && (GetLastError() == ERROR_MESSAGE_SYNC_ONLY || GetLastError() == ERROR_INVALID_PARAMETER),
8103         "SendMessageCallback on sync only message returned %ld, last error %d\n", lRes, GetLastError());
8104     SetLastError(0);
8105     lRes = SendMessageCallbackW(hwnd, CB_FINDSTRINGEXACT, 0, (LPARAM)wszUnicode, NULL, 0);
8106     ok(lRes == 0 && (GetLastError() == ERROR_MESSAGE_SYNC_ONLY || GetLastError() == ERROR_INVALID_PARAMETER),
8107         "SendMessageCallback on sync only message returned %ld, last error %d\n", lRes, GetLastError());
8108
8109     /* Check WM_GETTEXTLENGTH A->W behaviour, whether WM_GETTEXT is also sent or not */
8110
8111     hwnd = CreateWindowW (testWindowClassW, wszUnicode,
8112                           WS_OVERLAPPEDWINDOW,
8113                           100, 100, 200, 200, 0, 0, 0, NULL);
8114     assert(hwnd);
8115     flush_sequence();
8116     lRes = SendMessageA (hwnd, WM_GETTEXTLENGTH, 0, 0);
8117     ok_sequence(WmGetTextLengthAfromW, "ANSI WM_GETTEXTLENGTH to Unicode window", FALSE);
8118     ok( lRes == WideCharToMultiByte( CP_ACP, 0, wszUnicode, lstrlenW(wszUnicode), NULL, 0, NULL, NULL ),
8119         "got bad length %ld\n", lRes );
8120
8121     flush_sequence();
8122     lRes = CallWindowProcA( (WNDPROC)GetWindowLongPtrA( hwnd, GWLP_WNDPROC ),
8123                             hwnd, WM_GETTEXTLENGTH, 0, 0);
8124     ok_sequence(WmGetTextLengthAfromW, "ANSI WM_GETTEXTLENGTH to Unicode window", FALSE);
8125     ok( lRes == WideCharToMultiByte( CP_ACP, 0, wszUnicode, lstrlenW(wszUnicode), NULL, 0, NULL, NULL ),
8126         "got bad length %ld\n", lRes );
8127
8128     wndproc = (WNDPROC)SetWindowLongPtrW( hwnd, GWLP_WNDPROC, (LONG_PTR)get_text_len_proc );
8129     newproc = (WNDPROC)GetWindowLongPtrA( hwnd, GWLP_WNDPROC );
8130     lRes = CallWindowProcA( newproc, hwnd, WM_GETTEXTLENGTH, 0, 0 );
8131     ok( lRes == WideCharToMultiByte( CP_ACP, 0, dummy_window_text, lstrlenW(dummy_window_text),
8132                                      NULL, 0, NULL, NULL ) ||
8133         broken(lRes == lstrlenW(dummy_window_text) + 37),
8134         "got bad length %ld\n", lRes );
8135
8136     SetWindowLongPtrW( hwnd, GWLP_WNDPROC, (LONG_PTR)wndproc );  /* restore old wnd proc */
8137     lRes = CallWindowProcA( newproc, hwnd, WM_GETTEXTLENGTH, 0, 0 );
8138     ok( lRes == WideCharToMultiByte( CP_ACP, 0, dummy_window_text, lstrlenW(dummy_window_text),
8139                                      NULL, 0, NULL, NULL ) ||
8140         broken(lRes == lstrlenW(dummy_window_text) + 37),
8141         "got bad length %ld\n", lRes );
8142
8143     ret = DestroyWindow(hwnd);
8144     ok( ret, "DestroyWindow() error %d\n", GetLastError());
8145 }
8146
8147 struct timer_info
8148 {
8149     HWND hWnd;
8150     HANDLE handles[2];
8151     DWORD id;
8152 };
8153
8154 static VOID CALLBACK tfunc(HWND hwnd, UINT uMsg, UINT_PTR id, DWORD dwTime)
8155 {
8156 }
8157
8158 #define TIMER_ID  0x19
8159
8160 static DWORD WINAPI timer_thread_proc(LPVOID x)
8161 {
8162     struct timer_info *info = x;
8163     DWORD r;
8164
8165     r = KillTimer(info->hWnd, 0x19);
8166     ok(r,"KillTimer failed in thread\n");
8167     r = SetTimer(info->hWnd,TIMER_ID,10000,tfunc);
8168     ok(r,"SetTimer failed in thread\n");
8169     ok(r==TIMER_ID,"SetTimer id different\n");
8170     r = SetEvent(info->handles[0]);
8171     ok(r,"SetEvent failed in thread\n");
8172     return 0;
8173 }
8174
8175 static void test_timers(void)
8176 {
8177     struct timer_info info;
8178     DWORD id;
8179
8180     info.hWnd = CreateWindow ("TestWindowClass", NULL,
8181        WS_OVERLAPPEDWINDOW ,
8182        CW_USEDEFAULT, CW_USEDEFAULT, 300, 300, 0,
8183        NULL, NULL, 0);
8184
8185     info.id = SetTimer(info.hWnd,TIMER_ID,10000,tfunc);
8186     ok(info.id, "SetTimer failed\n");
8187     ok(info.id==TIMER_ID, "SetTimer timer ID different\n");
8188     info.handles[0] = CreateEvent(NULL,0,0,NULL);
8189     info.handles[1] = CreateThread(NULL,0,timer_thread_proc,&info,0,&id);
8190
8191     WaitForMultipleObjects(2, info.handles, FALSE, INFINITE);
8192
8193     WaitForSingleObject(info.handles[1], INFINITE);
8194
8195     CloseHandle(info.handles[0]);
8196     CloseHandle(info.handles[1]);
8197
8198     ok( KillTimer(info.hWnd, TIMER_ID), "KillTimer failed\n");
8199
8200     ok(DestroyWindow(info.hWnd), "failed to destroy window\n");
8201 }
8202
8203 static int count = 0;
8204 static VOID CALLBACK callback_count(
8205     HWND hwnd,
8206     UINT uMsg,
8207     UINT_PTR idEvent,
8208     DWORD dwTime
8209 )
8210 {
8211     count++;
8212 }
8213
8214 static void test_timers_no_wnd(void)
8215 {
8216     UINT_PTR id, id2;
8217     MSG msg;
8218
8219     count = 0;
8220     id = SetTimer(NULL, 0, 100, callback_count);
8221     ok(id != 0, "did not get id from SetTimer.\n");
8222     id2 = SetTimer(NULL, id, 200, callback_count);
8223     ok(id2 == id, "did not get same id from SetTimer when replacing (%li expected %li).\n", id2, id);
8224     Sleep(150);
8225     while (PeekMessage(&msg, NULL, 0, 0, PM_REMOVE)) DispatchMessage(&msg);
8226     ok(count == 0, "did not get zero count as expected (%i).\n", count);
8227     Sleep(150);
8228     while (PeekMessage(&msg, NULL, 0, 0, PM_REMOVE)) DispatchMessage(&msg);
8229     ok(count == 1, "did not get one count as expected (%i).\n", count);
8230     KillTimer(NULL, id);
8231     Sleep(250);
8232     while (PeekMessage(&msg, NULL, 0, 0, PM_REMOVE)) DispatchMessage(&msg);
8233     ok(count == 1, "killing replaced timer did not work (%i).\n", count);
8234 }
8235
8236 /* Various win events with arbitrary parameters */
8237 static const struct message WmWinEventsSeq[] = {
8238     { EVENT_SYSTEM_SOUND, winevent_hook|wparam|lparam, OBJID_WINDOW, 0 },
8239     { EVENT_SYSTEM_ALERT, winevent_hook|wparam|lparam, OBJID_SYSMENU, 1 },
8240     { EVENT_SYSTEM_FOREGROUND, winevent_hook|wparam|lparam, OBJID_TITLEBAR, 2 },
8241     { EVENT_SYSTEM_MENUSTART, winevent_hook|wparam|lparam, OBJID_MENU, 3 },
8242     { EVENT_SYSTEM_MENUEND, winevent_hook|wparam|lparam, OBJID_CLIENT, 4 },
8243     { EVENT_SYSTEM_MENUPOPUPSTART, winevent_hook|wparam|lparam, OBJID_VSCROLL, 5 },
8244     { EVENT_SYSTEM_MENUPOPUPEND, winevent_hook|wparam|lparam, OBJID_HSCROLL, 6 },
8245     { EVENT_SYSTEM_CAPTURESTART, winevent_hook|wparam|lparam, OBJID_SIZEGRIP, 7 },
8246     { EVENT_SYSTEM_CAPTUREEND, winevent_hook|wparam|lparam, OBJID_CARET, 8 },
8247     /* our win event hook ignores OBJID_CURSOR events */
8248     /*{ EVENT_SYSTEM_MOVESIZESTART, winevent_hook|wparam|lparam, OBJID_CURSOR, 9 },*/
8249     { EVENT_SYSTEM_MOVESIZEEND, winevent_hook|wparam|lparam, OBJID_ALERT, 10 },
8250     { EVENT_SYSTEM_CONTEXTHELPSTART, winevent_hook|wparam|lparam, OBJID_SOUND, 11 },
8251     { EVENT_SYSTEM_CONTEXTHELPEND, winevent_hook|wparam|lparam, OBJID_QUERYCLASSNAMEIDX, 12 },
8252     { EVENT_SYSTEM_DRAGDROPSTART, winevent_hook|wparam|lparam, OBJID_NATIVEOM, 13 },
8253     { EVENT_SYSTEM_DRAGDROPEND, winevent_hook|wparam|lparam, OBJID_WINDOW, 0 },
8254     { EVENT_SYSTEM_DIALOGSTART, winevent_hook|wparam|lparam, OBJID_SYSMENU, 1 },
8255     { EVENT_SYSTEM_DIALOGEND, winevent_hook|wparam|lparam, OBJID_TITLEBAR, 2 },
8256     { EVENT_SYSTEM_SCROLLINGSTART, winevent_hook|wparam|lparam, OBJID_MENU, 3 },
8257     { EVENT_SYSTEM_SCROLLINGEND, winevent_hook|wparam|lparam, OBJID_CLIENT, 4 },
8258     { EVENT_SYSTEM_SWITCHSTART, winevent_hook|wparam|lparam, OBJID_VSCROLL, 5 },
8259     { EVENT_SYSTEM_SWITCHEND, winevent_hook|wparam|lparam, OBJID_HSCROLL, 6 },
8260     { EVENT_SYSTEM_MINIMIZESTART, winevent_hook|wparam|lparam, OBJID_SIZEGRIP, 7 },
8261     { EVENT_SYSTEM_MINIMIZEEND, winevent_hook|wparam|lparam, OBJID_CARET, 8 },
8262     { 0 }
8263 };
8264 static const struct message WmWinEventCaretSeq[] = {
8265     { EVENT_OBJECT_CREATE, winevent_hook|wparam|lparam, OBJID_CARET, 0 }, /* hook 1 */
8266     { EVENT_OBJECT_SHOW, winevent_hook|wparam|lparam, OBJID_CARET, 0 }, /* hook 1 */
8267     { EVENT_OBJECT_SHOW, winevent_hook|wparam|lparam, OBJID_CARET, 0 }, /* hook 2 */
8268     { EVENT_OBJECT_NAMECHANGE, winevent_hook|wparam|lparam, OBJID_CARET, 0 }, /* hook 1 */
8269     { 0 }
8270 };
8271 static const struct message WmWinEventCaretSeq_2[] = {
8272     { EVENT_OBJECT_CREATE, winevent_hook|wparam|lparam, OBJID_CARET, 0 }, /* hook 1/2 */
8273     { EVENT_OBJECT_SHOW, winevent_hook|wparam|lparam, OBJID_CARET, 0 }, /* hook 1/2 */
8274     { EVENT_OBJECT_NAMECHANGE, winevent_hook|wparam|lparam, OBJID_CARET, 0 }, /* hook 1/2 */
8275     { 0 }
8276 };
8277 static const struct message WmWinEventAlertSeq[] = {
8278     { EVENT_OBJECT_LOCATIONCHANGE, winevent_hook|wparam|lparam, OBJID_ALERT, 0 },
8279     { 0 }
8280 };
8281 static const struct message WmWinEventAlertSeq_2[] = {
8282     /* create window in the thread proc */
8283     { EVENT_OBJECT_CREATE, winevent_hook|wparam|lparam, OBJID_WINDOW, 2 },
8284     /* our test event */
8285     { EVENT_OBJECT_LOCATIONCHANGE, winevent_hook|wparam|lparam, OBJID_ALERT, 2 },
8286     { 0 }
8287 };
8288 static const struct message WmGlobalHookSeq_1[] = {
8289     /* create window in the thread proc */
8290     { HCBT_CREATEWND, hook|lparam, 0, 2 },
8291     /* our test events */
8292     { HCBT_SYSCOMMAND, hook|wparam|lparam, SC_PREVWINDOW, 2 },
8293     { HCBT_SYSCOMMAND, hook|wparam|lparam, SC_NEXTWINDOW, 2 },
8294     { 0 }
8295 };
8296 static const struct message WmGlobalHookSeq_2[] = {
8297     { HCBT_SYSCOMMAND, hook|wparam|lparam, SC_NEXTWINDOW, 0 }, /* old local hook */
8298     { HCBT_SYSCOMMAND, hook|wparam|lparam, SC_NEXTWINDOW, 2 }, /* new global hook */
8299     { HCBT_SYSCOMMAND, hook|wparam|lparam, SC_PREVWINDOW, 0 }, /* old local hook */
8300     { HCBT_SYSCOMMAND, hook|wparam|lparam, SC_PREVWINDOW, 2 }, /* new global hook */
8301     { 0 }
8302 };
8303
8304 static const struct message WmMouseLLHookSeq[] = {
8305     { WM_MOUSEMOVE, hook },
8306     { WM_LBUTTONUP, hook },
8307     { WM_MOUSEMOVE, hook },
8308     { 0 }
8309 };
8310
8311 static void CALLBACK win_event_global_hook_proc(HWINEVENTHOOK hevent,
8312                                          DWORD event,
8313                                          HWND hwnd,
8314                                          LONG object_id,
8315                                          LONG child_id,
8316                                          DWORD thread_id,
8317                                          DWORD event_time)
8318 {
8319     char buf[256];
8320
8321     if (GetClassNameA(hwnd, buf, sizeof(buf)))
8322     {
8323         if (!lstrcmpiA(buf, "TestWindowClass") ||
8324             !lstrcmpiA(buf, "static"))
8325         {
8326             struct recvd_message msg;
8327
8328             msg.hwnd = hwnd;
8329             msg.message = event;
8330             msg.flags = winevent_hook|wparam|lparam;
8331             msg.wParam = object_id;
8332             msg.lParam = (thread_id == GetCurrentThreadId()) ? child_id : (child_id + 2);
8333             msg.descr = "WEH_2";
8334             add_message(&msg);
8335         }
8336     }
8337 }
8338
8339 static HHOOK hCBT_global_hook;
8340 static DWORD cbt_global_hook_thread_id;
8341
8342 static LRESULT CALLBACK cbt_global_hook_proc(int nCode, WPARAM wParam, LPARAM lParam) 
8343
8344     HWND hwnd;
8345     char buf[256];
8346
8347     if (nCode == HCBT_SYSCOMMAND)
8348     {
8349         struct recvd_message msg;
8350
8351         msg.hwnd = 0;
8352         msg.message = nCode;
8353         msg.flags = hook|wparam|lparam;
8354         msg.wParam = wParam;
8355         msg.lParam = (cbt_global_hook_thread_id == GetCurrentThreadId()) ? 1 : 2;
8356         msg.descr = "CBT_2";
8357         add_message(&msg);
8358
8359         return CallNextHookEx(hCBT_global_hook, nCode, wParam, lParam);
8360     }
8361     /* WH_MOUSE_LL hook */
8362     if (nCode == HC_ACTION)
8363     {
8364         MSLLHOOKSTRUCT *mhll = (MSLLHOOKSTRUCT *)lParam;
8365
8366         /* we can't test for real mouse events */
8367         if (mhll->flags & LLMHF_INJECTED)
8368         {
8369             struct recvd_message msg;
8370
8371             memset (&msg, 0, sizeof (msg));
8372             msg.message = wParam;
8373             msg.flags = hook;
8374             msg.descr = "CBT_2";
8375             add_message(&msg);
8376         }
8377         return CallNextHookEx(hCBT_global_hook, nCode, wParam, lParam);
8378     }
8379
8380     /* Log also SetFocus(0) calls */
8381     hwnd = wParam ? (HWND)wParam : (HWND)lParam;
8382
8383     if (GetClassNameA(hwnd, buf, sizeof(buf)))
8384     {
8385         if (!lstrcmpiA(buf, "TestWindowClass") ||
8386             !lstrcmpiA(buf, "static"))
8387         {
8388             struct recvd_message msg;
8389
8390             msg.hwnd = hwnd;
8391             msg.message = nCode;
8392             msg.flags = hook|wparam|lparam;
8393             msg.wParam = wParam;
8394             msg.lParam = (cbt_global_hook_thread_id == GetCurrentThreadId()) ? 1 : 2;
8395             msg.descr = "CBT_2";
8396             add_message(&msg);
8397         }
8398     }
8399     return CallNextHookEx(hCBT_global_hook, nCode, wParam, lParam);
8400 }
8401
8402 static DWORD WINAPI win_event_global_thread_proc(void *param)
8403 {
8404     HWND hwnd;
8405     MSG msg;
8406     HANDLE hevent = *(HANDLE *)param;
8407
8408     assert(pNotifyWinEvent);
8409
8410     hwnd = CreateWindowExA(0, "static", NULL, WS_POPUP, 0,0,0,0,0,0,0, NULL);
8411     assert(hwnd);
8412     trace("created thread window %p\n", hwnd);
8413
8414     *(HWND *)param = hwnd;
8415
8416     flush_sequence();
8417     /* this event should be received only by our new hook proc,
8418      * an old one does not expect an event from another thread.
8419      */
8420     pNotifyWinEvent(EVENT_OBJECT_LOCATIONCHANGE, hwnd, OBJID_ALERT, 0);
8421     SetEvent(hevent);
8422
8423     while (GetMessage(&msg, 0, 0, 0))
8424     {
8425         TranslateMessage(&msg);
8426         DispatchMessage(&msg);
8427     }
8428     return 0;
8429 }
8430
8431 static DWORD WINAPI cbt_global_hook_thread_proc(void *param)
8432 {
8433     HWND hwnd;
8434     MSG msg;
8435     HANDLE hevent = *(HANDLE *)param;
8436
8437     flush_sequence();
8438     /* these events should be received only by our new hook proc,
8439      * an old one does not expect an event from another thread.
8440      */
8441
8442     hwnd = CreateWindowExA(0, "static", NULL, WS_POPUP, 0,0,0,0,0,0,0, NULL);
8443     assert(hwnd);
8444     trace("created thread window %p\n", hwnd);
8445
8446     *(HWND *)param = hwnd;
8447
8448     /* Windows doesn't like when a thread plays games with the focus,
8449        that leads to all kinds of misbehaviours and failures to activate
8450        a window. So, better keep next lines commented out.
8451     SetFocus(0);
8452     SetFocus(hwnd);*/
8453
8454     DefWindowProcA(hwnd, WM_SYSCOMMAND, SC_PREVWINDOW, 0);
8455     DefWindowProcA(hwnd, WM_SYSCOMMAND, SC_NEXTWINDOW, 0);
8456
8457     SetEvent(hevent);
8458
8459     while (GetMessage(&msg, 0, 0, 0))
8460     {
8461         TranslateMessage(&msg);
8462         DispatchMessage(&msg);
8463     }
8464     return 0;
8465 }
8466
8467 static DWORD WINAPI mouse_ll_global_thread_proc(void *param)
8468 {
8469     HWND hwnd;
8470     MSG msg;
8471     HANDLE hevent = *(HANDLE *)param;
8472
8473     hwnd = CreateWindowExA(0, "static", NULL, WS_POPUP, 0,0,0,0,0,0,0, NULL);
8474     assert(hwnd);
8475     trace("created thread window %p\n", hwnd);
8476
8477     *(HWND *)param = hwnd;
8478
8479     flush_sequence();
8480
8481     /* Windows doesn't like when a thread plays games with the focus,
8482      * that leads to all kinds of misbehaviours and failures to activate
8483      * a window. So, better don't generate a mouse click message below.
8484      */
8485     mouse_event(MOUSEEVENTF_MOVE, -1, 0, 0, 0);
8486     mouse_event(MOUSEEVENTF_LEFTUP, 0, 0, 0, 0);
8487     mouse_event(MOUSEEVENTF_MOVE, 1, 0, 0, 0);
8488
8489     SetEvent(hevent);
8490     while (GetMessage(&msg, 0, 0, 0))
8491     {
8492         TranslateMessage(&msg);
8493         DispatchMessage(&msg);
8494     }
8495     return 0;
8496 }
8497
8498 static void test_winevents(void)
8499 {
8500     BOOL ret;
8501     MSG msg;
8502     HWND hwnd, hwnd2;
8503     UINT i;
8504     HANDLE hthread, hevent;
8505     DWORD tid;
8506     HWINEVENTHOOK hhook;
8507     const struct message *events = WmWinEventsSeq;
8508
8509     hwnd = CreateWindowExA(0, "TestWindowClass", NULL,
8510                            WS_OVERLAPPEDWINDOW,
8511                            CW_USEDEFAULT, CW_USEDEFAULT, 300, 300, 0,
8512                            NULL, NULL, 0);
8513     assert(hwnd);
8514
8515     /****** start of global hook test *************/
8516     hCBT_global_hook = SetWindowsHookExA(WH_CBT, cbt_global_hook_proc, GetModuleHandleA(0), 0);
8517     if (!hCBT_global_hook)
8518     {
8519         ok(DestroyWindow(hwnd), "failed to destroy window\n");
8520         skip( "cannot set global hook\n" );
8521         return;
8522     }
8523
8524     hevent = CreateEventA(NULL, 0, 0, NULL);
8525     assert(hevent);
8526     hwnd2 = hevent;
8527
8528     hthread = CreateThread(NULL, 0, cbt_global_hook_thread_proc, &hwnd2, 0, &tid);
8529     ok(hthread != NULL, "CreateThread failed, error %d\n", GetLastError());
8530
8531     ok(WaitForSingleObject(hevent, INFINITE) == WAIT_OBJECT_0, "WaitForSingleObject failed\n");
8532
8533     ok_sequence(WmGlobalHookSeq_1, "global hook 1", FALSE);
8534
8535     flush_sequence();
8536     /* this one should be received only by old hook proc */
8537     DefWindowProcA(hwnd, WM_SYSCOMMAND, SC_NEXTWINDOW, 0);
8538     /* this one should be received only by old hook proc */
8539     DefWindowProcA(hwnd, WM_SYSCOMMAND, SC_PREVWINDOW, 0);
8540
8541     ok_sequence(WmGlobalHookSeq_2, "global hook 2", FALSE);
8542
8543     ret = UnhookWindowsHookEx(hCBT_global_hook);
8544     ok( ret, "UnhookWindowsHookEx error %d\n", GetLastError());
8545
8546     PostThreadMessageA(tid, WM_QUIT, 0, 0);
8547     ok(WaitForSingleObject(hthread, INFINITE) == WAIT_OBJECT_0, "WaitForSingleObject failed\n");
8548     CloseHandle(hthread);
8549     CloseHandle(hevent);
8550     ok(!IsWindow(hwnd2), "window should be destroyed on thread exit\n");
8551     /****** end of global hook test *************/
8552
8553     if (!pSetWinEventHook || !pNotifyWinEvent || !pUnhookWinEvent)
8554     {
8555         ok(DestroyWindow(hwnd), "failed to destroy window\n");
8556         return;
8557     }
8558
8559     flush_sequence();
8560
8561     if (0)
8562     {
8563     /* this test doesn't pass under Win9x */
8564     /* win2k ignores events with hwnd == 0 */
8565     SetLastError(0xdeadbeef);
8566     pNotifyWinEvent(events[0].message, 0, events[0].wParam, events[0].lParam);
8567     ok(GetLastError() == ERROR_INVALID_WINDOW_HANDLE || /* Win2k */
8568        GetLastError() == 0xdeadbeef, /* Win9x */
8569        "unexpected error %d\n", GetLastError());
8570     ok_sequence(WmEmptySeq, "empty notify winevents", FALSE);
8571     }
8572
8573     for (i = 0; i < sizeof(WmWinEventsSeq)/sizeof(WmWinEventsSeq[0]); i++)
8574         pNotifyWinEvent(events[i].message, hwnd, events[i].wParam, events[i].lParam);
8575
8576     ok_sequence(WmWinEventsSeq, "notify winevents", FALSE);
8577
8578     /****** start of event filtering test *************/
8579     hhook = pSetWinEventHook(
8580         EVENT_OBJECT_SHOW, /* 0x8002 */
8581         EVENT_OBJECT_LOCATIONCHANGE, /* 0x800B */
8582         GetModuleHandleA(0), win_event_global_hook_proc,
8583         GetCurrentProcessId(), 0,
8584         WINEVENT_INCONTEXT);
8585     ok(hhook != 0, "SetWinEventHook error %d\n", GetLastError());
8586
8587     hevent = CreateEventA(NULL, 0, 0, NULL);
8588     assert(hevent);
8589     hwnd2 = hevent;
8590
8591     hthread = CreateThread(NULL, 0, win_event_global_thread_proc, &hwnd2, 0, &tid);
8592     ok(hthread != NULL, "CreateThread failed, error %d\n", GetLastError());
8593
8594     ok(WaitForSingleObject(hevent, INFINITE) == WAIT_OBJECT_0, "WaitForSingleObject failed\n");
8595
8596     ok_sequence(WmWinEventAlertSeq, "alert winevent", FALSE);
8597
8598     flush_sequence();
8599     /* this one should be received only by old hook proc */
8600     pNotifyWinEvent(EVENT_OBJECT_CREATE, hwnd, OBJID_CARET, 0); /* 0x8000 */
8601     pNotifyWinEvent(EVENT_OBJECT_SHOW, hwnd, OBJID_CARET, 0); /* 0x8002 */
8602     /* this one should be received only by old hook proc */
8603     pNotifyWinEvent(EVENT_OBJECT_NAMECHANGE, hwnd, OBJID_CARET, 0); /* 0x800C */
8604
8605     ok_sequence(WmWinEventCaretSeq, "caret winevent", FALSE);
8606
8607     ret = pUnhookWinEvent(hhook);
8608     ok( ret, "UnhookWinEvent error %d\n", GetLastError());
8609
8610     PostThreadMessageA(tid, WM_QUIT, 0, 0);
8611     ok(WaitForSingleObject(hthread, INFINITE) == WAIT_OBJECT_0, "WaitForSingleObject failed\n");
8612     CloseHandle(hthread);
8613     CloseHandle(hevent);
8614     ok(!IsWindow(hwnd2), "window should be destroyed on thread exit\n");
8615     /****** end of event filtering test *************/
8616
8617     /****** start of out of context event test *************/
8618     hhook = pSetWinEventHook(EVENT_MIN, EVENT_MAX, 0,
8619         win_event_global_hook_proc, GetCurrentProcessId(), 0,
8620         WINEVENT_OUTOFCONTEXT);
8621     ok(hhook != 0, "SetWinEventHook error %d\n", GetLastError());
8622
8623     hevent = CreateEventA(NULL, 0, 0, NULL);
8624     assert(hevent);
8625     hwnd2 = hevent;
8626
8627     flush_sequence();
8628
8629     hthread = CreateThread(NULL, 0, win_event_global_thread_proc, &hwnd2, 0, &tid);
8630     ok(hthread != NULL, "CreateThread failed, error %d\n", GetLastError());
8631
8632     ok(WaitForSingleObject(hevent, INFINITE) == WAIT_OBJECT_0, "WaitForSingleObject failed\n");
8633
8634     ok_sequence(WmEmptySeq, "empty notify winevents", FALSE);
8635     /* process pending winevent messages */
8636     ok(!PeekMessageA(&msg, 0, 0, 0, PM_NOREMOVE), "msg queue should be empty\n");
8637     ok_sequence(WmWinEventAlertSeq_2, "alert winevent for out of context proc", FALSE);
8638
8639     flush_sequence();
8640     /* this one should be received only by old hook proc */
8641     pNotifyWinEvent(EVENT_OBJECT_CREATE, hwnd, OBJID_CARET, 0); /* 0x8000 */
8642     pNotifyWinEvent(EVENT_OBJECT_SHOW, hwnd, OBJID_CARET, 0); /* 0x8002 */
8643     /* this one should be received only by old hook proc */
8644     pNotifyWinEvent(EVENT_OBJECT_NAMECHANGE, hwnd, OBJID_CARET, 0); /* 0x800C */
8645
8646     ok_sequence(WmWinEventCaretSeq_2, "caret winevent for incontext proc", FALSE);
8647     /* process pending winevent messages */
8648     ok(!PeekMessageA(&msg, 0, 0, 0, PM_NOREMOVE), "msg queue should be empty\n");
8649     ok_sequence(WmWinEventCaretSeq_2, "caret winevent for out of context proc", FALSE);
8650
8651     ret = pUnhookWinEvent(hhook);
8652     ok( ret, "UnhookWinEvent error %d\n", GetLastError());
8653
8654     PostThreadMessageA(tid, WM_QUIT, 0, 0);
8655     ok(WaitForSingleObject(hthread, INFINITE) == WAIT_OBJECT_0, "WaitForSingleObject failed\n");
8656     CloseHandle(hthread);
8657     CloseHandle(hevent);
8658     ok(!IsWindow(hwnd2), "window should be destroyed on thread exit\n");
8659     /****** end of out of context event test *************/
8660
8661     /****** start of MOUSE_LL hook test *************/
8662     hCBT_global_hook = SetWindowsHookExA(WH_MOUSE_LL, cbt_global_hook_proc, GetModuleHandleA(0), 0);
8663     /* WH_MOUSE_LL is not supported on Win9x platforms */
8664     if (!hCBT_global_hook)
8665     {
8666         win_skip("Skipping WH_MOUSE_LL test on this platform\n");
8667         goto skip_mouse_ll_hook_test;
8668     }
8669
8670     hevent = CreateEventA(NULL, 0, 0, NULL);
8671     assert(hevent);
8672     hwnd2 = hevent;
8673
8674     hthread = CreateThread(NULL, 0, mouse_ll_global_thread_proc, &hwnd2, 0, &tid);
8675     ok(hthread != NULL, "CreateThread failed, error %d\n", GetLastError());
8676
8677     while (WaitForSingleObject(hevent, 100) == WAIT_TIMEOUT)
8678         while (PeekMessage( &msg, 0, 0, 0, PM_REMOVE )) DispatchMessage( &msg );
8679
8680     ok_sequence(WmMouseLLHookSeq, "MOUSE_LL hook other thread", FALSE);
8681     flush_sequence();
8682
8683     mouse_event(MOUSEEVENTF_MOVE, -1, 0, 0, 0);
8684     mouse_event(MOUSEEVENTF_LEFTUP, 0, 0, 0, 0);
8685     mouse_event(MOUSEEVENTF_MOVE, 1, 0, 0, 0);
8686
8687     ok_sequence(WmMouseLLHookSeq, "MOUSE_LL hook same thread", FALSE);
8688
8689     ret = UnhookWindowsHookEx(hCBT_global_hook);
8690     ok( ret, "UnhookWindowsHookEx error %d\n", GetLastError());
8691
8692     PostThreadMessageA(tid, WM_QUIT, 0, 0);
8693     ok(WaitForSingleObject(hthread, INFINITE) == WAIT_OBJECT_0, "WaitForSingleObject failed\n");
8694     CloseHandle(hthread);
8695     CloseHandle(hevent);
8696     ok(!IsWindow(hwnd2), "window should be destroyed on thread exit\n");
8697     /****** end of MOUSE_LL hook test *************/
8698 skip_mouse_ll_hook_test:
8699
8700     ok(DestroyWindow(hwnd), "failed to destroy window\n");
8701 }
8702
8703 static void test_set_hook(void)
8704 {
8705     BOOL ret;
8706     HHOOK hhook;
8707     HWINEVENTHOOK hwinevent_hook;
8708
8709     hhook = SetWindowsHookExA(WH_CBT, cbt_hook_proc, GetModuleHandleA(0), GetCurrentThreadId());
8710     ok(hhook != 0, "local hook does not require hModule set to 0\n");
8711     UnhookWindowsHookEx(hhook);
8712
8713     if (0)
8714     {
8715     /* this test doesn't pass under Win9x: BUG! */
8716     SetLastError(0xdeadbeef);
8717     hhook = SetWindowsHookExA(WH_CBT, cbt_hook_proc, 0, 0);
8718     ok(!hhook, "global hook requires hModule != 0\n");
8719     ok(GetLastError() == ERROR_HOOK_NEEDS_HMOD, "unexpected error %d\n", GetLastError());
8720     }
8721
8722     SetLastError(0xdeadbeef);
8723     hhook = SetWindowsHookExA(WH_CBT, 0, GetModuleHandleA(0), GetCurrentThreadId());
8724     ok(!hhook, "SetWinEventHook with invalid proc should fail\n");
8725     ok(GetLastError() == ERROR_INVALID_FILTER_PROC || /* Win2k */
8726        GetLastError() == 0xdeadbeef, /* Win9x */
8727        "unexpected error %d\n", GetLastError());
8728
8729     SetLastError(0xdeadbeef);
8730     ok(!UnhookWindowsHookEx((HHOOK)0xdeadbeef), "UnhookWindowsHookEx succeeded\n");
8731     ok(GetLastError() == ERROR_INVALID_HOOK_HANDLE || /* Win2k */
8732        GetLastError() == 0xdeadbeef, /* Win9x */
8733        "unexpected error %d\n", GetLastError());
8734
8735     if (!pSetWinEventHook || !pUnhookWinEvent) return;
8736
8737     /* even process local incontext hooks require hmodule */
8738     SetLastError(0xdeadbeef);
8739     hwinevent_hook = pSetWinEventHook(EVENT_MIN, EVENT_MAX, 0, win_event_proc,
8740         GetCurrentProcessId(), 0, WINEVENT_INCONTEXT);
8741     ok(!hwinevent_hook, "WINEVENT_INCONTEXT requires hModule != 0\n");
8742     ok(GetLastError() == ERROR_HOOK_NEEDS_HMOD || /* Win2k */
8743        GetLastError() == 0xdeadbeef, /* Win9x */
8744        "unexpected error %d\n", GetLastError());
8745
8746     /* even thread local incontext hooks require hmodule */
8747     SetLastError(0xdeadbeef);
8748     hwinevent_hook = pSetWinEventHook(EVENT_MIN, EVENT_MAX, 0, win_event_proc,
8749         GetCurrentProcessId(), GetCurrentThreadId(), WINEVENT_INCONTEXT);
8750     ok(!hwinevent_hook, "WINEVENT_INCONTEXT requires hModule != 0\n");
8751     ok(GetLastError() == ERROR_HOOK_NEEDS_HMOD || /* Win2k */
8752        GetLastError() == 0xdeadbeef, /* Win9x */
8753        "unexpected error %d\n", GetLastError());
8754
8755     if (0)
8756     {
8757     /* these 3 tests don't pass under Win9x */
8758     SetLastError(0xdeadbeef);
8759     hwinevent_hook = pSetWinEventHook(1, 0, 0, win_event_proc,
8760         GetCurrentProcessId(), 0, WINEVENT_OUTOFCONTEXT);
8761     ok(!hwinevent_hook, "SetWinEventHook with invalid event range should fail\n");
8762     ok(GetLastError() == ERROR_INVALID_HOOK_FILTER, "unexpected error %d\n", GetLastError());
8763
8764     SetLastError(0xdeadbeef);
8765     hwinevent_hook = pSetWinEventHook(-1, 1, 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(EVENT_MIN, EVENT_MAX, 0, win_event_proc,
8772         0, 0xdeadbeef, WINEVENT_OUTOFCONTEXT);
8773     ok(!hwinevent_hook, "SetWinEventHook with invalid tid should fail\n");
8774     ok(GetLastError() == ERROR_INVALID_THREAD_ID, "unexpected error %d\n", GetLastError());
8775     }
8776
8777     SetLastError(0xdeadbeef);
8778     hwinevent_hook = pSetWinEventHook(0, 0, 0, win_event_proc,
8779         GetCurrentProcessId(), 0, WINEVENT_OUTOFCONTEXT);
8780     ok(hwinevent_hook != 0, "SetWinEventHook error %d\n", GetLastError());
8781     ok(GetLastError() == 0xdeadbeef, "unexpected error %d\n", GetLastError());
8782     ret = pUnhookWinEvent(hwinevent_hook);
8783     ok( ret, "UnhookWinEvent error %d\n", GetLastError());
8784
8785 todo_wine {
8786     /* This call succeeds under win2k SP4, but fails under Wine.
8787        Does win2k test/use passed process id? */
8788     SetLastError(0xdeadbeef);
8789     hwinevent_hook = pSetWinEventHook(EVENT_MIN, EVENT_MAX, 0, win_event_proc,
8790         0xdeadbeef, 0, WINEVENT_OUTOFCONTEXT);
8791     ok(hwinevent_hook != 0, "SetWinEventHook error %d\n", GetLastError());
8792     ok(GetLastError() == 0xdeadbeef, "unexpected error %d\n", GetLastError());
8793     ret = pUnhookWinEvent(hwinevent_hook);
8794     ok( ret, "UnhookWinEvent error %d\n", GetLastError());
8795 }
8796
8797     SetLastError(0xdeadbeef);
8798     ok(!pUnhookWinEvent((HWINEVENTHOOK)0xdeadbeef), "UnhookWinEvent succeeded\n");
8799     ok(GetLastError() == ERROR_INVALID_HANDLE || /* Win2k */
8800         GetLastError() == 0xdeadbeef, /* Win9x */
8801         "unexpected error %d\n", GetLastError());
8802 }
8803
8804 static const struct message ScrollWindowPaint1[] = {
8805     { WM_PAINT, sent },
8806     { WM_ERASEBKGND, sent|beginpaint },
8807     { WM_GETTEXTLENGTH, sent|optional },
8808     { WM_PAINT, sent|optional },
8809     { WM_NCPAINT, sent|beginpaint|optional },
8810     { WM_GETTEXT, sent|beginpaint|optional },
8811     { WM_GETTEXT, sent|beginpaint|optional },
8812     { WM_GETTEXT, sent|beginpaint|optional },
8813     { WM_GETTEXT, sent|beginpaint|defwinproc|optional },
8814     { WM_ERASEBKGND, sent|beginpaint|optional },
8815     { 0 }
8816 };
8817
8818 static const struct message ScrollWindowPaint2[] = {
8819     { WM_PAINT, sent },
8820     { 0 }
8821 };
8822
8823 static void test_scrollwindowex(void)
8824 {
8825     HWND hwnd, hchild;
8826     RECT rect={0,0,130,130};
8827
8828     hwnd = CreateWindowExA(0, "TestWindowClass", "Test Scroll",
8829             WS_VISIBLE|WS_OVERLAPPEDWINDOW,
8830             100, 100, 200, 200, 0, 0, 0, NULL);
8831     ok (hwnd != 0, "Failed to create overlapped window\n");
8832     hchild = CreateWindowExA(0, "TestWindowClass", "Test child", 
8833             WS_VISIBLE|WS_CAPTION|WS_CHILD,
8834             10, 10, 150, 150, hwnd, 0, 0, NULL);
8835     ok (hchild != 0, "Failed to create child\n");
8836     UpdateWindow(hwnd);
8837     flush_events();
8838     flush_sequence();
8839
8840     /* scroll without the child window */
8841     trace("start scroll\n");
8842     ScrollWindowEx( hwnd, 10, 10, &rect, NULL, NULL, NULL,
8843             SW_ERASE|SW_INVALIDATE);
8844     ok_sequence(WmEmptySeq, "ScrollWindowEx", 0);
8845     trace("end scroll\n");
8846     flush_sequence();
8847     flush_events();
8848     ok_sequence(ScrollWindowPaint1, "ScrollWindowEx", 0);
8849     flush_events();
8850     flush_sequence();
8851
8852     /* Now without the SW_ERASE flag */
8853     trace("start scroll\n");
8854     ScrollWindowEx( hwnd, 10, 10, &rect, NULL, NULL, NULL, SW_INVALIDATE);
8855     ok_sequence(WmEmptySeq, "ScrollWindowEx", 0);
8856     trace("end scroll\n");
8857     flush_sequence();
8858     flush_events();
8859     ok_sequence(ScrollWindowPaint2, "ScrollWindowEx", 0);
8860     flush_events();
8861     flush_sequence();
8862
8863     /* now scroll the child window as well */
8864     trace("start scroll\n");
8865     ScrollWindowEx( hwnd, 10, 10, &rect, NULL, NULL, NULL,
8866             SW_SCROLLCHILDREN|SW_ERASE|SW_INVALIDATE);
8867     /* wine sends WM_POSCHANGING, WM_POSCHANGED messages */
8868     /* windows sometimes a WM_MOVE */
8869     ok_sequence(WmEmptySeq, "ScrollWindowEx", TRUE);
8870     trace("end scroll\n");
8871     flush_sequence();
8872     flush_events();
8873     ok_sequence(ScrollWindowPaint1, "ScrollWindowEx", 0);
8874     flush_events();
8875     flush_sequence();
8876
8877     /* now scroll with ScrollWindow() */
8878     trace("start scroll with ScrollWindow\n");
8879     ScrollWindow( hwnd, 5, 5, NULL, NULL);
8880     trace("end scroll\n");
8881     flush_sequence();
8882     flush_events();
8883     ok_sequence(ScrollWindowPaint1, "ScrollWindow", 0);
8884
8885     ok(DestroyWindow(hchild), "failed to destroy window\n");
8886     ok(DestroyWindow(hwnd), "failed to destroy window\n");
8887     flush_sequence();
8888 }
8889
8890 static const struct message destroy_window_with_children[] = {
8891     { EVENT_SYSTEM_CAPTURESTART, winevent_hook|wparam|lparam, 0, 0 }, /* popup */
8892     { HCBT_DESTROYWND, hook|lparam, 0, WND_PARENT_ID }, /* parent */
8893     { 0x0090, sent|optional },
8894     { HCBT_DESTROYWND, hook|lparam, 0, WND_POPUP_ID }, /* popup */
8895     { 0x0090, sent|optional },
8896     { EVENT_OBJECT_DESTROY, winevent_hook|wparam|lparam, 0, 0 }, /* popup */
8897     { WM_DESTROY, sent|wparam|lparam, 0, WND_POPUP_ID }, /* popup */
8898     { WM_CAPTURECHANGED, sent|wparam|lparam, 0, WND_POPUP_ID }, /* popup */
8899     { WM_NCDESTROY, sent|wparam|lparam, 0, WND_POPUP_ID }, /* popup */
8900     { EVENT_OBJECT_DESTROY, winevent_hook|wparam|lparam, 0, 0 }, /* parent */
8901     { WM_DESTROY, sent|wparam|lparam, 0, WND_PARENT_ID }, /* parent */
8902     { WM_DESTROY, sent|wparam|lparam, 0, WND_CHILD_ID + 2 }, /* child2 */
8903     { WM_DESTROY, sent|wparam|lparam, 0, WND_CHILD_ID + 1 }, /* child1 */
8904     { WM_DESTROY, sent|wparam|lparam, 0, WND_CHILD_ID + 3 }, /* child3 */
8905     { WM_NCDESTROY, sent|wparam|lparam, 0, WND_CHILD_ID + 2 }, /* child2 */
8906     { WM_NCDESTROY, sent|wparam|lparam, 0, WND_CHILD_ID + 3 }, /* child3 */
8907     { WM_NCDESTROY, sent|wparam|lparam, 0, WND_CHILD_ID + 1 }, /* child1 */
8908     { WM_NCDESTROY, sent|wparam|lparam, 0, WND_PARENT_ID }, /* parent */
8909     { 0 }
8910 };
8911
8912 static void test_DestroyWindow(void)
8913 {
8914     BOOL ret;
8915     HWND parent, child1, child2, child3, child4, test;
8916     UINT_PTR child_id = WND_CHILD_ID + 1;
8917
8918     parent = CreateWindowExA(0, "TestWindowClass", NULL, WS_OVERLAPPEDWINDOW,
8919                              100, 100, 200, 200, 0, 0, 0, NULL);
8920     assert(parent != 0);
8921     child1 = CreateWindowExA(0, "TestWindowClass", NULL, WS_CHILD,
8922                              0, 0, 50, 50, parent, (HMENU)child_id++, 0, NULL);
8923     assert(child1 != 0);
8924     child2 = CreateWindowExA(0, "TestWindowClass", NULL, WS_CHILD,
8925                              0, 0, 50, 50, GetDesktopWindow(), (HMENU)child_id++, 0, NULL);
8926     assert(child2 != 0);
8927     child3 = CreateWindowExA(0, "TestWindowClass", NULL, WS_CHILD,
8928                              0, 0, 50, 50, child1, (HMENU)child_id++, 0, NULL);
8929     assert(child3 != 0);
8930     child4 = CreateWindowExA(0, "TestWindowClass", NULL, WS_POPUP,
8931                              0, 0, 50, 50, parent, 0, 0, NULL);
8932     assert(child4 != 0);
8933
8934     /* test owner/parent of child2 */
8935     test = GetParent(child2);
8936     ok(test == GetDesktopWindow(), "wrong parent %p\n", test);
8937     ok(!IsChild(parent, child2), "wrong parent/child %p/%p\n", parent, child2);
8938     if(pGetAncestor) {
8939         test = pGetAncestor(child2, GA_PARENT);
8940         ok(test == GetDesktopWindow(), "wrong parent %p\n", test);
8941     }
8942     test = GetWindow(child2, GW_OWNER);
8943     ok(!test, "wrong owner %p\n", test);
8944
8945     test = SetParent(child2, parent);
8946     ok(test == GetDesktopWindow(), "wrong old parent %p\n", test);
8947
8948     /* test owner/parent of the parent */
8949     test = GetParent(parent);
8950     ok(!test, "wrong parent %p\n", test);
8951     ok(!IsChild(GetDesktopWindow(), parent), "wrong parent/child %p/%p\n", GetDesktopWindow(), parent);
8952     if(pGetAncestor) {
8953         test = pGetAncestor(parent, GA_PARENT);
8954         ok(test == GetDesktopWindow(), "wrong parent %p\n", test);
8955     }
8956     test = GetWindow(parent, GW_OWNER);
8957     ok(!test, "wrong owner %p\n", test);
8958
8959     /* test owner/parent of child1 */
8960     test = GetParent(child1);
8961     ok(test == parent, "wrong parent %p\n", test);
8962     ok(IsChild(parent, child1), "wrong parent/child %p/%p\n", parent, child1);
8963     if(pGetAncestor) {
8964         test = pGetAncestor(child1, GA_PARENT);
8965         ok(test == parent, "wrong parent %p\n", test);
8966     }
8967     test = GetWindow(child1, GW_OWNER);
8968     ok(!test, "wrong owner %p\n", test);
8969
8970     /* test owner/parent of child2 */
8971     test = GetParent(child2);
8972     ok(test == parent, "wrong parent %p\n", test);
8973     ok(IsChild(parent, child2), "wrong parent/child %p/%p\n", parent, child2);
8974     if(pGetAncestor) {
8975         test = pGetAncestor(child2, GA_PARENT);
8976         ok(test == parent, "wrong parent %p\n", test);
8977     }
8978     test = GetWindow(child2, GW_OWNER);
8979     ok(!test, "wrong owner %p\n", test);
8980
8981     /* test owner/parent of child3 */
8982     test = GetParent(child3);
8983     ok(test == child1, "wrong parent %p\n", test);
8984     ok(IsChild(parent, child3), "wrong parent/child %p/%p\n", parent, child3);
8985     if(pGetAncestor) {
8986         test = pGetAncestor(child3, GA_PARENT);
8987         ok(test == child1, "wrong parent %p\n", test);
8988     }
8989     test = GetWindow(child3, GW_OWNER);
8990     ok(!test, "wrong owner %p\n", test);
8991
8992     /* test owner/parent of child4 */
8993     test = GetParent(child4);
8994     ok(test == parent, "wrong parent %p\n", test);
8995     ok(!IsChild(parent, child4), "wrong parent/child %p/%p\n", parent, child4);
8996     if(pGetAncestor) {
8997         test = pGetAncestor(child4, GA_PARENT);
8998         ok(test == GetDesktopWindow(), "wrong parent %p\n", test);
8999     }
9000     test = GetWindow(child4, GW_OWNER);
9001     ok(test == parent, "wrong owner %p\n", test);
9002
9003     flush_sequence();
9004
9005     trace("parent %p, child1 %p, child2 %p, child3 %p, child4 %p\n",
9006            parent, child1, child2, child3, child4);
9007
9008     SetCapture(child4);
9009     test = GetCapture();
9010     ok(test == child4, "wrong capture window %p\n", test);
9011
9012     test_DestroyWindow_flag = TRUE;
9013     ret = DestroyWindow(parent);
9014     ok( ret, "DestroyWindow() error %d\n", GetLastError());
9015     test_DestroyWindow_flag = FALSE;
9016     ok_sequence(destroy_window_with_children, "destroy window with children", 0);
9017
9018     ok(!IsWindow(parent), "parent still exists\n");
9019     ok(!IsWindow(child1), "child1 still exists\n");
9020     ok(!IsWindow(child2), "child2 still exists\n");
9021     ok(!IsWindow(child3), "child3 still exists\n");
9022     ok(!IsWindow(child4), "child4 still exists\n");
9023
9024     test = GetCapture();
9025     ok(!test, "wrong capture window %p\n", test);
9026 }
9027
9028
9029 static const struct message WmDispatchPaint[] = {
9030     { WM_NCPAINT, sent },
9031     { WM_GETTEXT, sent|defwinproc|optional },
9032     { WM_GETTEXT, sent|defwinproc|optional },
9033     { WM_ERASEBKGND, sent },
9034     { 0 }
9035 };
9036
9037 static LRESULT WINAPI DispatchMessageCheckProc(HWND hwnd, UINT message, WPARAM wParam, LPARAM lParam)
9038 {
9039     if (message == WM_PAINT) return 0;
9040     return MsgCheckProcA( hwnd, message, wParam, lParam );
9041 }
9042
9043 static void test_DispatchMessage(void)
9044 {
9045     RECT rect;
9046     MSG msg;
9047     int count;
9048     HWND hwnd = CreateWindowA( "TestWindowClass", NULL, WS_OVERLAPPEDWINDOW,
9049                                100, 100, 200, 200, 0, 0, 0, NULL);
9050     ShowWindow( hwnd, SW_SHOW );
9051     UpdateWindow( hwnd );
9052     flush_events();
9053     flush_sequence();
9054     SetWindowLongPtrA( hwnd, GWLP_WNDPROC, (LONG_PTR)DispatchMessageCheckProc );
9055
9056     SetRect( &rect, -5, -5, 5, 5 );
9057     RedrawWindow( hwnd, &rect, 0, RDW_INVALIDATE|RDW_ERASE|RDW_FRAME );
9058     count = 0;
9059     while (PeekMessage( &msg, 0, 0, 0, PM_REMOVE ))
9060     {
9061         if (msg.message != WM_PAINT) DispatchMessage( &msg );
9062         else
9063         {
9064             flush_sequence();
9065             DispatchMessage( &msg );
9066             /* DispatchMessage will send WM_NCPAINT if non client area is still invalid after WM_PAINT */
9067             if (!count) ok_sequence( WmDispatchPaint, "WmDispatchPaint", FALSE );
9068             else ok_sequence( WmEmptySeq, "WmEmpty", FALSE );
9069             if (++count > 10) break;
9070         }
9071     }
9072     ok( msg.message == WM_PAINT && count > 10, "WM_PAINT messages stopped\n" );
9073
9074     trace("now without DispatchMessage\n");
9075     flush_sequence();
9076     RedrawWindow( hwnd, &rect, 0, RDW_INVALIDATE|RDW_ERASE|RDW_FRAME );
9077     count = 0;
9078     while (PeekMessage( &msg, 0, 0, 0, PM_REMOVE ))
9079     {
9080         if (msg.message != WM_PAINT) DispatchMessage( &msg );
9081         else
9082         {
9083             HRGN hrgn = CreateRectRgn( 0, 0, 0, 0 );
9084             flush_sequence();
9085             /* this will send WM_NCCPAINT just like DispatchMessage does */
9086             GetUpdateRgn( hwnd, hrgn, TRUE );
9087             ok_sequence( WmDispatchPaint, "WmDispatchPaint", FALSE );
9088             DeleteObject( hrgn );
9089             GetClientRect( hwnd, &rect );
9090             ValidateRect( hwnd, &rect );  /* this will stop WM_PAINTs */
9091             ok( !count, "Got multiple WM_PAINTs\n" );
9092             if (++count > 10) break;
9093         }
9094     }
9095     DestroyWindow(hwnd);
9096 }
9097
9098
9099 static const struct message WmUser[] = {
9100     { WM_USER, sent },
9101     { 0 }
9102 };
9103
9104 struct sendmsg_info
9105 {
9106     HWND  hwnd;
9107     DWORD timeout;
9108     DWORD ret;
9109 };
9110
9111 static DWORD CALLBACK send_msg_thread( LPVOID arg )
9112 {
9113     struct sendmsg_info *info = arg;
9114     SetLastError( 0xdeadbeef );
9115     info->ret = SendMessageTimeoutA( info->hwnd, WM_USER, 0, 0, 0, info->timeout, NULL );
9116     if (!info->ret) ok( GetLastError() == ERROR_TIMEOUT ||
9117                         broken(GetLastError() == 0),  /* win9x */
9118                         "unexpected error %d\n", GetLastError());
9119     return 0;
9120 }
9121
9122 static void wait_for_thread( HANDLE thread )
9123 {
9124     while (MsgWaitForMultipleObjects(1, &thread, FALSE, INFINITE, QS_SENDMESSAGE) != WAIT_OBJECT_0)
9125     {
9126         MSG msg;
9127         while (PeekMessageA( &msg, 0, 0, 0, PM_REMOVE )) DispatchMessage(&msg);
9128     }
9129 }
9130
9131 static LRESULT WINAPI send_msg_delay_proc(HWND hwnd, UINT message, WPARAM wParam, LPARAM lParam)
9132 {
9133     if (message == WM_USER) Sleep(200);
9134     return MsgCheckProcA( hwnd, message, wParam, lParam );
9135 }
9136
9137 static void test_SendMessageTimeout(void)
9138 {
9139     HANDLE thread;
9140     struct sendmsg_info info;
9141     DWORD tid;
9142     BOOL is_win9x;
9143
9144     info.hwnd = CreateWindowA( "TestWindowClass", NULL, WS_OVERLAPPEDWINDOW,
9145                                100, 100, 200, 200, 0, 0, 0, NULL);
9146     flush_events();
9147     flush_sequence();
9148
9149     info.timeout = 1000;
9150     info.ret = 0xdeadbeef;
9151     thread = CreateThread( NULL, 0, send_msg_thread, &info, 0, &tid );
9152     wait_for_thread( thread );
9153     CloseHandle( thread );
9154     ok( info.ret == 1, "SendMessageTimeout failed\n" );
9155     ok_sequence( WmUser, "WmUser", FALSE );
9156
9157     info.timeout = 1;
9158     info.ret = 0xdeadbeef;
9159     thread = CreateThread( NULL, 0, send_msg_thread, &info, 0, &tid );
9160     Sleep(100);  /* SendMessageTimeout should time out here */
9161     wait_for_thread( thread );
9162     CloseHandle( thread );
9163     ok( info.ret == 0, "SendMessageTimeout succeeded\n" );
9164     ok_sequence( WmEmptySeq, "WmEmptySeq", FALSE );
9165
9166     /* 0 means infinite timeout (but not on win9x) */
9167     info.timeout = 0;
9168     info.ret = 0xdeadbeef;
9169     thread = CreateThread( NULL, 0, send_msg_thread, &info, 0, &tid );
9170     Sleep(100);
9171     wait_for_thread( thread );
9172     CloseHandle( thread );
9173     is_win9x = !info.ret;
9174     if (is_win9x) ok_sequence( WmEmptySeq, "WmEmptySeq", FALSE );
9175     else ok_sequence( WmUser, "WmUser", FALSE );
9176
9177     /* timeout is treated as signed despite the prototype (but not on win9x) */
9178     info.timeout = 0x7fffffff;
9179     info.ret = 0xdeadbeef;
9180     thread = CreateThread( NULL, 0, send_msg_thread, &info, 0, &tid );
9181     Sleep(100);
9182     wait_for_thread( thread );
9183     CloseHandle( thread );
9184     ok( info.ret == 1, "SendMessageTimeout failed\n" );
9185     ok_sequence( WmUser, "WmUser", FALSE );
9186
9187     info.timeout = 0x80000000;
9188     info.ret = 0xdeadbeef;
9189     thread = CreateThread( NULL, 0, send_msg_thread, &info, 0, &tid );
9190     Sleep(100);
9191     wait_for_thread( thread );
9192     CloseHandle( thread );
9193     if (is_win9x)
9194     {
9195         ok( info.ret == 1, "SendMessageTimeout failed\n" );
9196         ok_sequence( WmUser, "WmUser", FALSE );
9197     }
9198     else
9199     {
9200         ok( info.ret == 0, "SendMessageTimeout succeeded\n" );
9201         ok_sequence( WmEmptySeq, "WmEmptySeq", FALSE );
9202     }
9203
9204     /* now check for timeout during message processing */
9205     SetWindowLongPtrA( info.hwnd, GWLP_WNDPROC, (LONG_PTR)send_msg_delay_proc );
9206     info.timeout = 100;
9207     info.ret = 0xdeadbeef;
9208     thread = CreateThread( NULL, 0, send_msg_thread, &info, 0, &tid );
9209     wait_for_thread( thread );
9210     CloseHandle( thread );
9211     /* we should time out but still get the message */
9212     ok( info.ret == 0, "SendMessageTimeout failed\n" );
9213     ok_sequence( WmUser, "WmUser", FALSE );
9214
9215     DestroyWindow( info.hwnd );
9216 }
9217
9218
9219 /****************** edit message test *************************/
9220 #define ID_EDIT 0x1234
9221 static const struct message sl_edit_setfocus[] =
9222 {
9223     { HCBT_SETFOCUS, hook },
9224     { WM_IME_SETCONTEXT, sent|wparam|optional, 1 },
9225     { WM_IME_NOTIFY, sent|wparam|defwinproc|optional, 2 },
9226     { EVENT_OBJECT_FOCUS, winevent_hook|wparam|lparam, OBJID_CLIENT, 0 },
9227     { WM_SETFOCUS, sent|wparam, 0 },
9228     { WM_IME_NOTIFY, sent|wparam|defwinproc|optional, 10 },
9229     { WM_IME_NOTIFY, sent|wparam|defwinproc|optional, 15 },
9230     { WM_CTLCOLOREDIT, sent|parent },
9231     { WM_IME_NOTIFY, sent|wparam|defwinproc|optional, 11 },
9232     { EVENT_OBJECT_CREATE, winevent_hook|wparam|lparam, OBJID_CARET, 0 },
9233     { EVENT_OBJECT_SHOW, winevent_hook|wparam|lparam, OBJID_CARET, 0 },
9234     { WM_COMMAND, sent|parent|wparam, MAKEWPARAM(ID_EDIT, EN_SETFOCUS) },
9235     { 0 }
9236 };
9237 static const struct message ml_edit_setfocus[] =
9238 {
9239     { HCBT_SETFOCUS, hook },
9240     { WM_IME_SETCONTEXT, sent|wparam|optional, 1 },
9241     { WM_IME_NOTIFY, sent|wparam|defwinproc|optional, 2 },
9242     { EVENT_OBJECT_FOCUS, winevent_hook|wparam|lparam, OBJID_CLIENT, 0 },
9243     { WM_SETFOCUS, sent|wparam, 0 },
9244     { WM_IME_NOTIFY, sent|wparam|defwinproc|optional, 10 },
9245     { WM_IME_NOTIFY, sent|wparam|defwinproc|optional, 11 },
9246     { EVENT_OBJECT_CREATE, winevent_hook|wparam|lparam, OBJID_CARET, 0 },
9247     { EVENT_OBJECT_SHOW, winevent_hook|wparam|lparam, OBJID_CARET, 0 },
9248     { WM_COMMAND, sent|parent|wparam, MAKEWPARAM(ID_EDIT, EN_SETFOCUS) },
9249     { 0 }
9250 };
9251 static const struct message sl_edit_killfocus[] =
9252 {
9253     { HCBT_SETFOCUS, hook },
9254     { EVENT_OBJECT_FOCUS, winevent_hook|wparam|lparam, OBJID_CLIENT, 0 },
9255     { WM_KILLFOCUS, sent|wparam, 0 },
9256     { EVENT_OBJECT_HIDE, winevent_hook|wparam|lparam, OBJID_CARET, 0 },
9257     { EVENT_OBJECT_DESTROY, winevent_hook|wparam|lparam, OBJID_CARET, 0 },
9258     { WM_COMMAND, sent|parent|wparam, MAKEWPARAM(ID_EDIT, EN_KILLFOCUS) },
9259     { WM_IME_SETCONTEXT, sent|wparam|optional, 0 },
9260     { WM_IME_NOTIFY, sent|wparam|defwinproc|optional, 1 },
9261     { 0 }
9262 };
9263 static const struct message sl_edit_lbutton_dblclk[] =
9264 {
9265     { WM_LBUTTONDBLCLK, sent },
9266     { EVENT_SYSTEM_CAPTURESTART, winevent_hook|wparam|lparam, 0, 0 },
9267     { 0 }
9268 };
9269 static const struct message sl_edit_lbutton_down[] =
9270 {
9271     { WM_LBUTTONDOWN, sent|wparam|lparam, 0, 0 },
9272     { HCBT_SETFOCUS, hook },
9273     { WM_IME_SETCONTEXT, sent|wparam|defwinproc|optional, 1 },
9274     { WM_IME_NOTIFY, sent|wparam|defwinproc|optional, 2 },
9275     { EVENT_OBJECT_FOCUS, winevent_hook|wparam|lparam, OBJID_CLIENT, 0 },
9276     { WM_SETFOCUS, sent|wparam|defwinproc, 0 },
9277     { WM_IME_NOTIFY, sent|wparam|defwinproc|optional, 10 },
9278     { WM_CTLCOLOREDIT, sent|parent },
9279     { WM_IME_NOTIFY, sent|wparam|defwinproc|optional, 11 },
9280     { EVENT_OBJECT_CREATE, winevent_hook|wparam|lparam, OBJID_CARET, 0 },
9281     { EVENT_OBJECT_LOCATIONCHANGE, winevent_hook|wparam|lparam, OBJID_CARET, 0 },
9282     { EVENT_OBJECT_SHOW, winevent_hook|wparam|lparam, OBJID_CARET, 0 },
9283     { WM_COMMAND, sent|parent|wparam, MAKEWPARAM(ID_EDIT, EN_SETFOCUS) },
9284     { EVENT_SYSTEM_CAPTURESTART, winevent_hook|wparam|lparam, 0, 0 },
9285     { EVENT_OBJECT_HIDE, winevent_hook|wparam|lparam, OBJID_CARET, 0 },
9286     { WM_CTLCOLOREDIT, sent|parent|optional },
9287     { WM_IME_NOTIFY, sent|wparam|defwinproc|optional, 11 },
9288     { EVENT_OBJECT_LOCATIONCHANGE, winevent_hook|wparam|lparam, OBJID_CARET, 0 },
9289     { EVENT_OBJECT_SHOW, winevent_hook|wparam|lparam, OBJID_CARET, 0 },
9290     { 0 }
9291 };
9292 static const struct message ml_edit_lbutton_down[] =
9293 {
9294     { WM_LBUTTONDOWN, sent|wparam|lparam, 0, 0 },
9295     { EVENT_SYSTEM_CAPTURESTART, winevent_hook|wparam|lparam, 0, 0 },
9296     { HCBT_SETFOCUS, hook },
9297     { WM_IME_SETCONTEXT, sent|wparam|defwinproc|optional, 1 },
9298     { WM_IME_NOTIFY, sent|wparam|defwinproc|optional, 2 },
9299     { EVENT_OBJECT_FOCUS, winevent_hook|wparam|lparam, OBJID_CLIENT, 0 },
9300     { WM_SETFOCUS, sent|wparam|defwinproc, 0 },
9301     { WM_IME_NOTIFY, sent|wparam|defwinproc|optional, 10 },
9302     { WM_IME_NOTIFY, sent|wparam|defwinproc|optional, 11 },
9303     { EVENT_OBJECT_CREATE, winevent_hook|wparam|lparam, OBJID_CARET, 0 },
9304     { EVENT_OBJECT_SHOW, winevent_hook|wparam|lparam, OBJID_CARET, 0 },
9305     { WM_COMMAND, sent|parent|wparam, MAKEWPARAM(ID_EDIT, EN_SETFOCUS) },
9306     { 0 }
9307 };
9308 static const struct message sl_edit_lbutton_up[] =
9309 {
9310     { WM_LBUTTONUP, sent|wparam|lparam, 0, 0 },
9311     { EVENT_OBJECT_HIDE, winevent_hook|wparam|lparam, OBJID_CARET, 0 },
9312     { EVENT_SYSTEM_CAPTUREEND, winevent_hook|wparam|lparam, 0, 0 },
9313     { WM_CAPTURECHANGED, sent|defwinproc },
9314     { EVENT_OBJECT_SHOW, winevent_hook|wparam|lparam, OBJID_CARET, 0 },
9315     { 0 }
9316 };
9317 static const struct message ml_edit_lbutton_up[] =
9318 {
9319     { WM_LBUTTONUP, sent|wparam|lparam, 0, 0 },
9320     { EVENT_SYSTEM_CAPTUREEND, winevent_hook|wparam|lparam, 0, 0 },
9321     { WM_CAPTURECHANGED, sent|defwinproc },
9322     { 0 }
9323 };
9324
9325 static WNDPROC old_edit_proc;
9326
9327 static LRESULT CALLBACK edit_hook_proc(HWND hwnd, UINT message, WPARAM wParam, LPARAM lParam)
9328 {
9329     static LONG defwndproc_counter = 0;
9330     LRESULT ret;
9331     struct recvd_message msg;
9332
9333     if (ignore_message( message )) return 0;
9334
9335     msg.hwnd = hwnd;
9336     msg.message = message;
9337     msg.flags = sent|wparam|lparam;
9338     if (defwndproc_counter) msg.flags |= defwinproc;
9339     msg.wParam = wParam;
9340     msg.lParam = lParam;
9341     msg.descr = "edit";
9342     add_message(&msg);
9343
9344     defwndproc_counter++;
9345     ret = CallWindowProcA(old_edit_proc, hwnd, message, wParam, lParam);
9346     defwndproc_counter--;
9347
9348     return ret;
9349 }
9350
9351 static void subclass_edit(void)
9352 {
9353     WNDCLASSA cls;
9354
9355     if (!GetClassInfoA(0, "edit", &cls)) assert(0);
9356
9357     old_edit_proc = cls.lpfnWndProc;
9358
9359     cls.hInstance = GetModuleHandle(0);
9360     cls.lpfnWndProc = edit_hook_proc;
9361     cls.lpszClassName = "my_edit_class";
9362     UnregisterClass(cls.lpszClassName, cls.hInstance);
9363     if (!RegisterClassA(&cls)) assert(0);
9364 }
9365
9366 static void test_edit_messages(void)
9367 {
9368     HWND hwnd, parent;
9369     DWORD dlg_code;
9370
9371     subclass_edit();
9372     log_all_parent_messages++;
9373
9374     parent = CreateWindowExA(0, "TestParentClass", "Test parent", WS_OVERLAPPEDWINDOW | WS_VISIBLE,
9375                              100, 100, 200, 200, 0, 0, 0, NULL);
9376     ok (parent != 0, "Failed to create parent window\n");
9377
9378     /* test single line edit */
9379     hwnd = CreateWindowExA(0, "my_edit_class", "test", WS_CHILD,
9380                            0, 0, 80, 20, parent, (HMENU)ID_EDIT, 0, NULL);
9381     ok(hwnd != 0, "Failed to create edit window\n");
9382
9383     dlg_code = SendMessageA(hwnd, WM_GETDLGCODE, 0, 0);
9384     ok(dlg_code == (DLGC_WANTCHARS|DLGC_HASSETSEL|DLGC_WANTARROWS), "wrong dlg_code %08x\n", dlg_code);
9385
9386     ShowWindow(hwnd, SW_SHOW);
9387     UpdateWindow(hwnd);
9388     SetFocus(0);
9389     flush_sequence();
9390
9391     SetFocus(hwnd);
9392     ok_sequence(sl_edit_setfocus, "SetFocus(hwnd) on an edit", FALSE);
9393
9394     SetFocus(0);
9395     ok_sequence(sl_edit_killfocus, "SetFocus(0) on an edit", FALSE);
9396
9397     SetFocus(0);
9398     ReleaseCapture();
9399     flush_sequence();
9400
9401     SendMessageA(hwnd, WM_LBUTTONDBLCLK, 0, 0);
9402     ok_sequence(sl_edit_lbutton_dblclk, "WM_LBUTTONDBLCLK on an edit", FALSE);
9403
9404     SetFocus(0);
9405     ReleaseCapture();
9406     flush_sequence();
9407
9408     SendMessageA(hwnd, WM_LBUTTONDOWN, 0, 0);
9409     ok_sequence(sl_edit_lbutton_down, "WM_LBUTTONDOWN on an edit", FALSE);
9410
9411     SendMessageA(hwnd, WM_LBUTTONUP, 0, 0);
9412     ok_sequence(sl_edit_lbutton_up, "WM_LBUTTONUP on an edit", FALSE);
9413
9414     DestroyWindow(hwnd);
9415
9416     /* test multiline edit */
9417     hwnd = CreateWindowExA(0, "my_edit_class", "test", WS_CHILD | ES_MULTILINE,
9418                            0, 0, 80, 20, parent, (HMENU)ID_EDIT, 0, NULL);
9419     ok(hwnd != 0, "Failed to create edit window\n");
9420
9421     dlg_code = SendMessageA(hwnd, WM_GETDLGCODE, 0, 0);
9422     ok(dlg_code == (DLGC_WANTCHARS|DLGC_HASSETSEL|DLGC_WANTARROWS|DLGC_WANTALLKEYS),
9423        "wrong dlg_code %08x\n", dlg_code);
9424
9425     ShowWindow(hwnd, SW_SHOW);
9426     UpdateWindow(hwnd);
9427     SetFocus(0);
9428     flush_sequence();
9429
9430     SetFocus(hwnd);
9431     ok_sequence(ml_edit_setfocus, "SetFocus(hwnd) on multiline edit", FALSE);
9432
9433     SetFocus(0);
9434     ok_sequence(sl_edit_killfocus, "SetFocus(0) on multiline edit", FALSE);
9435
9436     SetFocus(0);
9437     ReleaseCapture();
9438     flush_sequence();
9439
9440     SendMessageA(hwnd, WM_LBUTTONDBLCLK, 0, 0);
9441     ok_sequence(sl_edit_lbutton_dblclk, "WM_LBUTTONDBLCLK on multiline edit", FALSE);
9442
9443     SetFocus(0);
9444     ReleaseCapture();
9445     flush_sequence();
9446
9447     SendMessageA(hwnd, WM_LBUTTONDOWN, 0, 0);
9448     ok_sequence(ml_edit_lbutton_down, "WM_LBUTTONDOWN on multiline edit", FALSE);
9449
9450     SendMessageA(hwnd, WM_LBUTTONUP, 0, 0);
9451     ok_sequence(ml_edit_lbutton_up, "WM_LBUTTONUP on multiline edit", FALSE);
9452
9453     DestroyWindow(hwnd);
9454     DestroyWindow(parent);
9455
9456     log_all_parent_messages--;
9457 }
9458
9459 /**************************** End of Edit test ******************************/
9460
9461 static const struct message WmKeyDownSkippedSeq[] =
9462 {
9463     { HCBT_KEYSKIPPED, hook|wparam|lparam|optional, 'N', 1 }, /* XP */
9464     { 0 }
9465 };
9466 static const struct message WmKeyDownWasDownSkippedSeq[] =
9467 {
9468     { HCBT_KEYSKIPPED, hook|wparam|lparam|optional, 'N', 0x40000001 }, /* XP */
9469     { 0 }
9470 };
9471 static const struct message WmKeyUpSkippedSeq[] =
9472 {
9473     { HCBT_KEYSKIPPED, hook|wparam|lparam|optional, 'N', 0xc0000001 }, /* XP */
9474     { 0 }
9475 };
9476 static const struct message WmUserKeyUpSkippedSeq[] =
9477 {
9478     { WM_USER, sent },
9479     { HCBT_KEYSKIPPED, hook|wparam|lparam|optional, 'N', 0xc0000001 }, /* XP */
9480     { 0 }
9481 };
9482
9483 #define EV_STOP 0
9484 #define EV_SENDMSG 1
9485 #define EV_ACK 2
9486
9487 struct peekmsg_info
9488 {
9489     HWND  hwnd;
9490     HANDLE hevent[3]; /* 0 - start/stop, 1 - SendMessage, 2 - ack */
9491 };
9492
9493 static DWORD CALLBACK send_msg_thread_2(void *param)
9494 {
9495     DWORD ret;
9496     struct peekmsg_info *info = param;
9497
9498     trace("thread: looping\n");
9499     SetEvent(info->hevent[EV_ACK]);
9500
9501     while (1)
9502     {
9503         ret = WaitForMultipleObjects(2, info->hevent, FALSE, INFINITE);
9504
9505         switch (ret)
9506         {
9507         case WAIT_OBJECT_0 + EV_STOP:
9508             trace("thread: exiting\n");
9509             return 0;
9510
9511         case WAIT_OBJECT_0 + EV_SENDMSG:
9512             trace("thread: sending message\n");
9513             ret = SendNotifyMessageA(info->hwnd, WM_USER, 0, 0);
9514             ok(ret, "SendNotifyMessageA failed error %u\n", GetLastError());
9515             SetEvent(info->hevent[EV_ACK]);
9516             break;
9517
9518         default:
9519             trace("unexpected return: %04x\n", ret);
9520             assert(0);
9521             break;
9522         }
9523     }
9524     return 0;
9525 }
9526
9527 static void test_PeekMessage(void)
9528 {
9529     MSG msg;
9530     HANDLE hthread;
9531     DWORD tid, qstatus;
9532     UINT qs_all_input = QS_ALLINPUT;
9533     UINT qs_input = QS_INPUT;
9534     BOOL ret;
9535     struct peekmsg_info info;
9536
9537     info.hwnd = CreateWindowA("TestWindowClass", NULL, WS_OVERLAPPEDWINDOW,
9538                               100, 100, 200, 200, 0, 0, 0, NULL);
9539     assert(info.hwnd);
9540     ShowWindow(info.hwnd, SW_SHOW);
9541     UpdateWindow(info.hwnd);
9542     SetFocus(info.hwnd);
9543
9544     info.hevent[EV_STOP] = CreateEventA(NULL, 0, 0, NULL);
9545     info.hevent[EV_SENDMSG] = CreateEventA(NULL, 0, 0, NULL);
9546     info.hevent[EV_ACK] = CreateEventA(NULL, 0, 0, NULL);
9547
9548     hthread = CreateThread(NULL, 0, send_msg_thread_2, &info, 0, &tid);
9549     WaitForSingleObject(info.hevent[EV_ACK], 10000);
9550
9551     flush_events();
9552     flush_sequence();
9553
9554     SetLastError(0xdeadbeef);
9555     qstatus = GetQueueStatus(qs_all_input);
9556     if (GetLastError() == ERROR_INVALID_FLAGS)
9557     {
9558         trace("QS_RAWINPUT not supported on this platform\n");
9559         qs_all_input &= ~QS_RAWINPUT;
9560         qs_input &= ~QS_RAWINPUT;
9561     }
9562     if (qstatus & QS_POSTMESSAGE)
9563     {
9564         while (PeekMessage(&msg, 0, 0, 0, PM_REMOVE)) /* nothing */ ;
9565         qstatus = GetQueueStatus(qs_all_input);
9566     }
9567     ok(qstatus == 0, "wrong qstatus %08x\n", qstatus);
9568
9569     trace("signalling to send message\n");
9570     SetEvent(info.hevent[EV_SENDMSG]);
9571     WaitForSingleObject(info.hevent[EV_ACK], INFINITE);
9572
9573     /* pass invalid QS_xxxx flags */
9574     SetLastError(0xdeadbeef);
9575     qstatus = GetQueueStatus(0xffffffff);
9576     ok(qstatus == 0 || broken(qstatus)  /* win9x */, "GetQueueStatus should fail: %08x\n", qstatus);
9577     if (!qstatus)
9578     {
9579         ok(GetLastError() == ERROR_INVALID_FLAGS, "wrong error %d\n", GetLastError());
9580         qstatus = GetQueueStatus(qs_all_input);
9581     }
9582     qstatus &= ~MAKELONG( 0x4000, 0x4000 );  /* sometimes set on Win95 */
9583     ok(qstatus == MAKELONG(QS_SENDMESSAGE, QS_SENDMESSAGE),
9584        "wrong qstatus %08x\n", qstatus);
9585
9586     msg.message = 0;
9587     ret = PeekMessageA(&msg, 0, 0, 0, PM_REMOVE);
9588     ok(!ret,
9589        "PeekMessageA should have returned FALSE instead of msg %04x\n",
9590         msg.message);
9591     ok_sequence(WmUser, "WmUser", FALSE);
9592
9593     qstatus = GetQueueStatus(qs_all_input);
9594     ok(qstatus == 0, "wrong qstatus %08x\n", qstatus);
9595
9596     keybd_event('N', 0, 0, 0);
9597     keybd_event('N', 0, KEYEVENTF_KEYUP, 0);
9598     qstatus = GetQueueStatus(qs_all_input);
9599     if (!(qstatus & MAKELONG(QS_KEY, QS_KEY)))
9600     {
9601         skip( "queuing key events not supported\n" );
9602         goto done;
9603     }
9604     ok(qstatus == MAKELONG(QS_KEY, QS_KEY) ||
9605        /* keybd_event seems to trigger a sent message on NT4 */
9606        qstatus == MAKELONG(QS_KEY|QS_SENDMESSAGE, QS_KEY|QS_SENDMESSAGE),
9607        "wrong qstatus %08x\n", qstatus);
9608
9609     PostMessageA(info.hwnd, WM_CHAR, 'z', 0);
9610     qstatus = GetQueueStatus(qs_all_input);
9611     ok(qstatus == MAKELONG(QS_POSTMESSAGE, QS_POSTMESSAGE|QS_KEY) ||
9612        qstatus == MAKELONG(QS_POSTMESSAGE, QS_POSTMESSAGE|QS_KEY|QS_SENDMESSAGE),
9613        "wrong qstatus %08x\n", qstatus);
9614
9615     InvalidateRect(info.hwnd, NULL, FALSE);
9616     qstatus = GetQueueStatus(qs_all_input);
9617     ok(qstatus == MAKELONG(QS_PAINT, QS_PAINT|QS_POSTMESSAGE|QS_KEY) ||
9618        qstatus == MAKELONG(QS_PAINT, QS_PAINT|QS_POSTMESSAGE|QS_KEY|QS_SENDMESSAGE),
9619        "wrong qstatus %08x\n", qstatus);
9620
9621     trace("signalling to send message\n");
9622     SetEvent(info.hevent[EV_SENDMSG]);
9623     WaitForSingleObject(info.hevent[EV_ACK], INFINITE);
9624
9625     qstatus = GetQueueStatus(qs_all_input);
9626     ok(qstatus == MAKELONG(QS_SENDMESSAGE, QS_SENDMESSAGE|QS_PAINT|QS_POSTMESSAGE|QS_KEY),
9627        "wrong qstatus %08x\n", qstatus);
9628
9629     msg.message = 0;
9630     ret = PeekMessageA(&msg, 0, 0, 0, PM_REMOVE | (qs_input << 16));
9631     if (ret && msg.message == WM_CHAR)
9632     {
9633         win_skip( "PM_QS_* flags not supported in PeekMessage\n" );
9634         goto done;
9635     }
9636     ok(!ret,
9637        "PeekMessageA should have returned FALSE instead of msg %04x\n",
9638         msg.message);
9639     if (!sequence_cnt)  /* nt4 doesn't fetch anything with PM_QS_* flags */
9640     {
9641         win_skip( "PM_QS_* flags not supported in PeekMessage\n" );
9642         goto done;
9643     }
9644     ok_sequence(WmUser, "WmUser", FALSE);
9645
9646     qstatus = GetQueueStatus(qs_all_input);
9647     ok(qstatus == MAKELONG(0, QS_PAINT|QS_POSTMESSAGE|QS_KEY),
9648        "wrong qstatus %08x\n", qstatus);
9649
9650     trace("signalling to send message\n");
9651     SetEvent(info.hevent[EV_SENDMSG]);
9652     WaitForSingleObject(info.hevent[EV_ACK], INFINITE);
9653
9654     qstatus = GetQueueStatus(qs_all_input);
9655     ok(qstatus == MAKELONG(QS_SENDMESSAGE, QS_SENDMESSAGE|QS_PAINT|QS_POSTMESSAGE|QS_KEY),
9656        "wrong qstatus %08x\n", qstatus);
9657
9658     msg.message = 0;
9659     ret = PeekMessageA(&msg, 0, 0, 0, PM_REMOVE | PM_QS_POSTMESSAGE );
9660     ok(!ret,
9661        "PeekMessageA should have returned FALSE instead of msg %04x\n",
9662         msg.message);
9663     ok_sequence(WmUser, "WmUser", FALSE);
9664
9665     qstatus = GetQueueStatus(qs_all_input);
9666     ok(qstatus == MAKELONG(0, QS_PAINT|QS_POSTMESSAGE|QS_KEY),
9667        "wrong qstatus %08x\n", qstatus);
9668
9669     msg.message = 0;
9670     ret = PeekMessageA(&msg, 0, 0, 0, PM_REMOVE | PM_QS_POSTMESSAGE);
9671     ok(ret && msg.message == WM_CHAR && msg.wParam == 'z',
9672        "got %d and %04x wParam %08lx instead of TRUE and WM_CHAR wParam 'z'\n",
9673        ret, msg.message, msg.wParam);
9674     ok_sequence(WmEmptySeq, "WmEmptySeq", FALSE);
9675
9676     qstatus = GetQueueStatus(qs_all_input);
9677     ok(qstatus == MAKELONG(0, QS_PAINT|QS_KEY),
9678        "wrong qstatus %08x\n", qstatus);
9679
9680     msg.message = 0;
9681     ret = PeekMessageA(&msg, 0, 0, 0, PM_REMOVE | PM_QS_POSTMESSAGE);
9682     ok(!ret,
9683        "PeekMessageA should have returned FALSE instead of msg %04x\n",
9684         msg.message);
9685     ok_sequence(WmEmptySeq, "WmEmptySeq", FALSE);
9686
9687     qstatus = GetQueueStatus(qs_all_input);
9688     ok(qstatus == MAKELONG(0, QS_PAINT|QS_KEY),
9689        "wrong qstatus %08x\n", qstatus);
9690
9691     msg.message = 0;
9692     ret = PeekMessageA(&msg, 0, 0, 0, PM_REMOVE | PM_QS_PAINT);
9693     ok(ret && msg.message == WM_PAINT,
9694        "got %d and %04x instead of TRUE and WM_PAINT\n", ret, msg.message);
9695     DispatchMessageA(&msg);
9696     ok_sequence(WmPaint, "WmPaint", FALSE);
9697
9698     qstatus = GetQueueStatus(qs_all_input);
9699     ok(qstatus == MAKELONG(0, QS_KEY),
9700        "wrong qstatus %08x\n", qstatus);
9701
9702     msg.message = 0;
9703     ret = PeekMessageA(&msg, 0, 0, 0, PM_REMOVE | PM_QS_PAINT);
9704     ok(!ret,
9705        "PeekMessageA should have returned FALSE instead of msg %04x\n",
9706         msg.message);
9707     ok_sequence(WmEmptySeq, "WmEmptySeq", FALSE);
9708
9709     qstatus = GetQueueStatus(qs_all_input);
9710     ok(qstatus == MAKELONG(0, QS_KEY),
9711        "wrong qstatus %08x\n", qstatus);
9712
9713     trace("signalling to send message\n");
9714     SetEvent(info.hevent[EV_SENDMSG]);
9715     WaitForSingleObject(info.hevent[EV_ACK], INFINITE);
9716
9717     qstatus = GetQueueStatus(qs_all_input);
9718     ok(qstatus == MAKELONG(QS_SENDMESSAGE, QS_SENDMESSAGE|QS_KEY),
9719        "wrong qstatus %08x\n", qstatus);
9720
9721     PostMessageA(info.hwnd, WM_CHAR, 'z', 0);
9722
9723     qstatus = GetQueueStatus(qs_all_input);
9724     ok(qstatus == MAKELONG(QS_POSTMESSAGE, QS_SENDMESSAGE|QS_POSTMESSAGE|QS_KEY),
9725        "wrong qstatus %08x\n", qstatus);
9726
9727     msg.message = 0;
9728     ret = PeekMessageA(&msg, 0, WM_CHAR, WM_CHAR, PM_REMOVE);
9729     ok(ret && msg.message == WM_CHAR && msg.wParam == 'z',
9730        "got %d and %04x wParam %08lx instead of TRUE and WM_CHAR wParam 'z'\n",
9731        ret, msg.message, msg.wParam);
9732     ok_sequence(WmUser, "WmUser", FALSE);
9733
9734     qstatus = GetQueueStatus(qs_all_input);
9735     ok(qstatus == MAKELONG(0, QS_KEY),
9736        "wrong qstatus %08x\n", qstatus);
9737
9738     msg.message = 0;
9739     ret = PeekMessageA(&msg, 0, WM_CHAR, WM_CHAR, PM_REMOVE);
9740     ok(!ret,
9741        "PeekMessageA should have returned FALSE instead of msg %04x\n",
9742         msg.message);
9743     ok_sequence(WmEmptySeq, "WmEmptySeq", FALSE);
9744
9745     qstatus = GetQueueStatus(qs_all_input);
9746     ok(qstatus == MAKELONG(0, QS_KEY),
9747        "wrong qstatus %08x\n", qstatus);
9748
9749     PostMessageA(info.hwnd, WM_CHAR, 'z', 0);
9750
9751     qstatus = GetQueueStatus(qs_all_input);
9752     ok(qstatus == MAKELONG(QS_POSTMESSAGE, QS_POSTMESSAGE|QS_KEY),
9753        "wrong qstatus %08x\n", qstatus);
9754
9755     trace("signalling to send message\n");
9756     SetEvent(info.hevent[EV_SENDMSG]);
9757     WaitForSingleObject(info.hevent[EV_ACK], INFINITE);
9758
9759     qstatus = GetQueueStatus(qs_all_input);
9760     ok(qstatus == MAKELONG(QS_SENDMESSAGE, QS_SENDMESSAGE|QS_POSTMESSAGE|QS_KEY),
9761        "wrong qstatus %08x\n", qstatus);
9762
9763     msg.message = 0;
9764     ret = PeekMessageA(&msg, 0, 0, 0, PM_REMOVE | (QS_KEY << 16));
9765     ok(!ret,
9766        "PeekMessageA should have returned FALSE instead of msg %04x\n",
9767         msg.message);
9768     ok_sequence(WmUser, "WmUser", FALSE);
9769
9770     qstatus = GetQueueStatus(qs_all_input);
9771     ok(qstatus == MAKELONG(0, QS_POSTMESSAGE|QS_KEY),
9772        "wrong qstatus %08x\n", qstatus);
9773
9774     msg.message = 0;
9775     if (qs_all_input & QS_RAWINPUT) /* use QS_RAWINPUT only if supported */
9776         ret = PeekMessageA(&msg, 0, 0, 0, PM_REMOVE | (QS_RAWINPUT << 16));
9777     else /* workaround for a missing QS_RAWINPUT support */
9778         ret = PeekMessageA(&msg, 0, WM_KEYDOWN, WM_KEYDOWN, PM_REMOVE);
9779     ok(ret && msg.message == WM_KEYDOWN && msg.wParam == 'N',
9780        "got %d and %04x wParam %08lx instead of TRUE and WM_KEYDOWN wParam 'N'\n",
9781        ret, msg.message, msg.wParam);
9782     ok_sequence(WmKeyDownSkippedSeq, "WmKeyDownSkippedSeq", FALSE);
9783
9784     qstatus = GetQueueStatus(qs_all_input);
9785     ok(qstatus == MAKELONG(0, QS_POSTMESSAGE|QS_KEY),
9786        "wrong qstatus %08x\n", qstatus);
9787
9788     msg.message = 0;
9789     if (qs_all_input & QS_RAWINPUT) /* use QS_RAWINPUT only if supported */
9790         ret = PeekMessageA(&msg, 0, 0, 0, PM_REMOVE | (QS_RAWINPUT << 16));
9791     else /* workaround for a missing QS_RAWINPUT support */
9792         ret = PeekMessageA(&msg, 0, WM_KEYUP, WM_KEYUP, PM_REMOVE);
9793     ok(ret && msg.message == WM_KEYUP && msg.wParam == 'N',
9794        "got %d and %04x wParam %08lx instead of TRUE and WM_KEYUP wParam 'N'\n",
9795        ret, msg.message, msg.wParam);
9796     ok_sequence(WmKeyUpSkippedSeq, "WmKeyUpSkippedSeq", FALSE);
9797
9798     qstatus = GetQueueStatus(qs_all_input);
9799     ok(qstatus == MAKELONG(0, QS_POSTMESSAGE),
9800        "wrong qstatus %08x\n", qstatus);
9801
9802     msg.message = 0;
9803     ret = PeekMessageA(&msg, 0, 0, 0, PM_REMOVE | PM_QS_SENDMESSAGE);
9804     ok(!ret,
9805        "PeekMessageA should have returned FALSE instead of msg %04x\n",
9806         msg.message);
9807     ok_sequence(WmEmptySeq, "WmEmptySeq", FALSE);
9808
9809     qstatus = GetQueueStatus(qs_all_input);
9810     ok(qstatus == MAKELONG(0, QS_POSTMESSAGE),
9811        "wrong qstatus %08x\n", qstatus);
9812
9813     msg.message = 0;
9814     ret = PeekMessageA(&msg, 0, 0, 0, PM_REMOVE);
9815     ok(ret && msg.message == WM_CHAR && msg.wParam == 'z',
9816        "got %d and %04x wParam %08lx instead of TRUE and WM_CHAR wParam 'z'\n",
9817        ret, msg.message, msg.wParam);
9818     ok_sequence(WmEmptySeq, "WmEmptySeq", FALSE);
9819
9820     qstatus = GetQueueStatus(qs_all_input);
9821     ok(qstatus == 0,
9822        "wrong qstatus %08x\n", qstatus);
9823
9824     msg.message = 0;
9825     ret = PeekMessageA(&msg, 0, 0, 0, PM_REMOVE);
9826     ok(!ret,
9827        "PeekMessageA should have returned FALSE instead of msg %04x\n",
9828         msg.message);
9829     ok_sequence(WmEmptySeq, "WmEmptySeq", FALSE);
9830
9831     qstatus = GetQueueStatus(qs_all_input);
9832     ok(qstatus == 0,
9833        "wrong qstatus %08x\n", qstatus);
9834
9835     /* test whether presence of the quit flag in the queue affects
9836      * the queue state
9837      */
9838     PostQuitMessage(0x1234abcd);
9839
9840     qstatus = GetQueueStatus(qs_all_input);
9841     ok(qstatus == MAKELONG(QS_POSTMESSAGE, QS_POSTMESSAGE),
9842        "wrong qstatus %08x\n", qstatus);
9843
9844     PostMessageA(info.hwnd, WM_USER, 0, 0);
9845
9846     qstatus = GetQueueStatus(qs_all_input);
9847     ok(qstatus == MAKELONG(QS_POSTMESSAGE, QS_POSTMESSAGE),
9848        "wrong qstatus %08x\n", qstatus);
9849
9850     msg.message = 0;
9851     ret = PeekMessageA(&msg, 0, 0, 0, PM_REMOVE);
9852     ok(ret && msg.message == WM_USER,
9853        "got %d and %04x instead of TRUE and WM_USER\n", ret, msg.message);
9854     ok_sequence(WmEmptySeq, "WmEmptySeq", FALSE);
9855
9856     qstatus = GetQueueStatus(qs_all_input);
9857     ok(qstatus == MAKELONG(0, QS_POSTMESSAGE),
9858        "wrong qstatus %08x\n", qstatus);
9859
9860     msg.message = 0;
9861     ret = PeekMessageA(&msg, 0, 0, 0, PM_REMOVE);
9862     ok(ret && msg.message == WM_QUIT,
9863        "got %d and %04x instead of TRUE and WM_QUIT\n", ret, msg.message);
9864     ok(msg.wParam == 0x1234abcd, "got wParam %08lx instead of 0x1234abcd\n", msg.wParam);
9865     ok(msg.lParam == 0, "got lParam %08lx instead of 0\n", msg.lParam);
9866     ok_sequence(WmEmptySeq, "WmEmptySeq", FALSE);
9867
9868     qstatus = GetQueueStatus(qs_all_input);
9869 todo_wine {
9870     ok(qstatus == MAKELONG(0, QS_POSTMESSAGE),
9871        "wrong qstatus %08x\n", qstatus);
9872 }
9873
9874     msg.message = 0;
9875     ret = PeekMessageA(&msg, 0, 0, 0, PM_REMOVE);
9876     ok(!ret,
9877        "PeekMessageA should have returned FALSE instead of msg %04x\n",
9878         msg.message);
9879     ok_sequence(WmEmptySeq, "WmEmptySeq", FALSE);
9880
9881     qstatus = GetQueueStatus(qs_all_input);
9882     ok(qstatus == 0,
9883        "wrong qstatus %08x\n", qstatus);
9884
9885     /* some GetMessage tests */
9886
9887     keybd_event('N', 0, 0, 0);
9888     qstatus = GetQueueStatus(qs_all_input);
9889     ok(qstatus == MAKELONG(QS_KEY, QS_KEY), "wrong qstatus %08x\n", qstatus);
9890
9891     PostMessageA(info.hwnd, WM_CHAR, 'z', 0);
9892     qstatus = GetQueueStatus(qs_all_input);
9893     ok(qstatus == MAKELONG(QS_POSTMESSAGE, QS_POSTMESSAGE|QS_KEY), "wrong qstatus %08x\n", qstatus);
9894
9895     if (qstatus)
9896     {
9897         ret = GetMessageA( &msg, 0, 0, 0 );
9898         ok(ret && msg.message == WM_CHAR && msg.wParam == 'z',
9899            "got %d and %04x wParam %08lx instead of TRUE and WM_CHAR wParam 'z'\n",
9900            ret, msg.message, msg.wParam);
9901         qstatus = GetQueueStatus(qs_all_input);
9902         ok(qstatus == MAKELONG(0, QS_KEY), "wrong qstatus %08x\n", qstatus);
9903     }
9904
9905     if (qstatus)
9906     {
9907         ret = GetMessageA( &msg, 0, 0, 0 );
9908         ok(ret && msg.message == WM_KEYDOWN && msg.wParam == 'N',
9909            "got %d and %04x wParam %08lx instead of TRUE and WM_KEYDOWN wParam 'N'\n",
9910            ret, msg.message, msg.wParam);
9911         ok_sequence(WmKeyDownSkippedSeq, "WmKeyDownSkippedSeq", FALSE);
9912         qstatus = GetQueueStatus(qs_all_input);
9913         ok(qstatus == 0, "wrong qstatus %08x\n", qstatus);
9914     }
9915
9916     keybd_event('N', 0, 0, 0);
9917     qstatus = GetQueueStatus(qs_all_input);
9918     ok(qstatus == MAKELONG(QS_KEY, QS_KEY), "wrong qstatus %08x\n", qstatus);
9919
9920     PostMessageA(info.hwnd, WM_CHAR, 'z', 0);
9921     qstatus = GetQueueStatus(qs_all_input);
9922     ok(qstatus == MAKELONG(QS_POSTMESSAGE, QS_POSTMESSAGE|QS_KEY), "wrong qstatus %08x\n", qstatus);
9923
9924     if (qstatus & (QS_KEY << 16))
9925     {
9926         ret = GetMessageA( &msg, 0, WM_KEYDOWN, WM_KEYUP );
9927         ok(ret && msg.message == WM_KEYDOWN && msg.wParam == 'N',
9928            "got %d and %04x wParam %08lx instead of TRUE and WM_KEYDOWN wParam 'N'\n",
9929            ret, msg.message, msg.wParam);
9930         ok_sequence(WmKeyDownWasDownSkippedSeq, "WmKeyDownWasDownSkippedSeq", FALSE);
9931         qstatus = GetQueueStatus(qs_all_input);
9932         ok(qstatus == MAKELONG(0, QS_POSTMESSAGE), "wrong qstatus %08x\n", qstatus);
9933     }
9934
9935     if (qstatus)
9936     {
9937         ret = GetMessageA( &msg, 0, WM_CHAR, WM_CHAR );
9938         ok(ret && msg.message == WM_CHAR && msg.wParam == 'z',
9939            "got %d and %04x wParam %08lx instead of TRUE and WM_CHAR wParam 'z'\n",
9940            ret, msg.message, msg.wParam);
9941         qstatus = GetQueueStatus(qs_all_input);
9942         ok(qstatus == 0, "wrong qstatus %08x\n", qstatus);
9943     }
9944
9945     keybd_event('N', 0, KEYEVENTF_KEYUP, 0);
9946     qstatus = GetQueueStatus(qs_all_input);
9947     ok(qstatus == MAKELONG(QS_KEY, QS_KEY), "wrong qstatus %08x\n", qstatus);
9948
9949     PostMessageA(info.hwnd, WM_CHAR, 'z', 0);
9950     qstatus = GetQueueStatus(qs_all_input);
9951     ok(qstatus == MAKELONG(QS_POSTMESSAGE, QS_POSTMESSAGE|QS_KEY), "wrong qstatus %08x\n", qstatus);
9952
9953     trace("signalling to send message\n");
9954     SetEvent(info.hevent[EV_SENDMSG]);
9955     WaitForSingleObject(info.hevent[EV_ACK], INFINITE);
9956     qstatus = GetQueueStatus(qs_all_input);
9957     ok(qstatus == MAKELONG(QS_SENDMESSAGE, QS_SENDMESSAGE|QS_POSTMESSAGE|QS_KEY),
9958        "wrong qstatus %08x\n", qstatus);
9959
9960     if (qstatus & (QS_KEY << 16))
9961     {
9962         ret = GetMessageA( &msg, 0, WM_KEYDOWN, WM_KEYUP );
9963         ok(ret && msg.message == WM_KEYUP && msg.wParam == 'N',
9964            "got %d and %04x wParam %08lx instead of TRUE and WM_KEYDOWN wParam 'N'\n",
9965            ret, msg.message, msg.wParam);
9966         ok_sequence(WmUserKeyUpSkippedSeq, "WmUserKeyUpSkippedSeq", FALSE);
9967         qstatus = GetQueueStatus(qs_all_input);
9968         ok(qstatus == MAKELONG(0, QS_POSTMESSAGE), "wrong qstatus %08x\n", qstatus);
9969     }
9970
9971     if (qstatus)
9972     {
9973         ret = GetMessageA( &msg, 0, WM_CHAR, WM_CHAR );
9974         ok(ret && msg.message == WM_CHAR && msg.wParam == 'z',
9975            "got %d and %04x wParam %08lx instead of TRUE and WM_CHAR wParam 'z'\n",
9976            ret, msg.message, msg.wParam);
9977         qstatus = GetQueueStatus(qs_all_input);
9978         ok(qstatus == 0, "wrong qstatus %08x\n", qstatus);
9979     }
9980 done:
9981     trace("signalling to exit\n");
9982     SetEvent(info.hevent[EV_STOP]);
9983
9984     WaitForSingleObject(hthread, INFINITE);
9985
9986     CloseHandle(hthread);
9987     CloseHandle(info.hevent[0]);
9988     CloseHandle(info.hevent[1]);
9989     CloseHandle(info.hevent[2]);
9990
9991     DestroyWindow(info.hwnd);
9992 }
9993
9994 static void wait_move_event(HWND hwnd, int x, int y)
9995 {
9996     MSG msg;
9997     DWORD time;
9998     BOOL  ret;
9999     int go = 0;
10000
10001     time = GetTickCount();
10002     while (GetTickCount() - time < 200 && !go) {
10003         ret = PeekMessageA(&msg, hwnd, WM_MOUSEMOVE, WM_MOUSEMOVE, PM_NOREMOVE);
10004         go  = ret && msg.pt.x > x && msg.pt.y > y;
10005         if (!ret) MsgWaitForMultipleObjects( 0, NULL, FALSE, GetTickCount() - time, QS_ALLINPUT );
10006     }
10007 }
10008
10009 #define STEP 5
10010 static void test_PeekMessage2(void)
10011 {
10012     HWND hwnd;
10013     BOOL ret;
10014     MSG msg;
10015     UINT message;
10016     DWORD time1, time2, time3;
10017     int x1, y1, x2, y2, x3, y3;
10018     POINT pos;
10019
10020     time1 = time2 = time3 = 0;
10021     x1 = y1 = x2 = y2 = x3 = y3 = 0;
10022
10023     /* Initialise window and make sure it is ready for events */
10024     hwnd = CreateWindow("TestWindowClass", "PeekMessage2", WS_OVERLAPPEDWINDOW,
10025                         10, 10, 800, 800, NULL, NULL, NULL, NULL);
10026     assert(hwnd);
10027     trace("Window for test_PeekMessage2 %p\n", hwnd);
10028     ShowWindow(hwnd, SW_SHOW);
10029     UpdateWindow(hwnd);
10030     SetFocus(hwnd);
10031     GetCursorPos(&pos);
10032     SetCursorPos(100, 100);
10033     mouse_event(MOUSEEVENTF_MOVE, -STEP, -STEP, 0, 0);
10034     flush_events();
10035
10036     /* Do initial mousemove, wait until we can see it
10037        and then do our test peek with PM_NOREMOVE. */
10038     mouse_event(MOUSEEVENTF_MOVE, STEP, STEP, 0, 0);
10039     wait_move_event(hwnd, 100-STEP, 100-STEP);
10040
10041     ret = PeekMessageA(&msg, hwnd, WM_MOUSEMOVE, WM_MOUSEMOVE, PM_NOREMOVE);
10042     if (!ret)
10043     {
10044         skip( "queuing mouse events not supported\n" );
10045         goto done;
10046     }
10047     else
10048     {
10049         trace("1st move event: %04x %x %d %d\n", msg.message, msg.time, msg.pt.x, msg.pt.y);
10050         message = msg.message;
10051         time1 = msg.time;
10052         x1 = msg.pt.x;
10053         y1 = msg.pt.y;
10054         ok(message == WM_MOUSEMOVE, "message not WM_MOUSEMOVE, %04x instead\n", message);
10055     }
10056
10057     /* Allow time to advance a bit, and then simulate the user moving their
10058      * mouse around. After that we peek again with PM_NOREMOVE.
10059      * Although the previous mousemove message was never removed, the
10060      * mousemove we now peek should reflect the recent mouse movements
10061      * because the input queue will merge the move events. */
10062     Sleep(100);
10063     mouse_event(MOUSEEVENTF_MOVE, STEP, STEP, 0, 0);
10064     wait_move_event(hwnd, x1, y1);
10065
10066     ret = PeekMessageA(&msg, hwnd, WM_MOUSEMOVE, WM_MOUSEMOVE, PM_NOREMOVE);
10067     ok(ret, "no message available\n");
10068     if (ret) {
10069         trace("2nd move event: %04x %x %d %d\n", msg.message, msg.time, msg.pt.x, msg.pt.y);
10070         message = msg.message;
10071         time2 = msg.time;
10072         x2 = msg.pt.x;
10073         y2 = msg.pt.y;
10074         ok(message == WM_MOUSEMOVE, "message not WM_MOUSEMOVE, %04x instead\n", message);
10075         ok(time2 > time1, "message time not advanced: %x %x\n", time1, time2);
10076         ok(x2 != x1 && y2 != y1, "coords not changed: (%d %d) (%d %d)\n", x1, y1, x2, y2);
10077     }
10078
10079     /* Have another go, to drive the point home */
10080     Sleep(100);
10081     mouse_event(MOUSEEVENTF_MOVE, STEP, STEP, 0, 0);
10082     wait_move_event(hwnd, x2, y2);
10083
10084     ret = PeekMessageA(&msg, hwnd, WM_MOUSEMOVE, WM_MOUSEMOVE, PM_NOREMOVE);
10085     ok(ret, "no message available\n");
10086     if (ret) {
10087         trace("3rd move event: %04x %x %d %d\n", msg.message, msg.time, msg.pt.x, msg.pt.y);
10088         message = msg.message;
10089         time3 = msg.time;
10090         x3 = msg.pt.x;
10091         y3 = msg.pt.y;
10092         ok(message == WM_MOUSEMOVE, "message not WM_MOUSEMOVE, %04x instead\n", message);
10093         ok(time3 > time2, "message time not advanced: %x %x\n", time2, time3);
10094         ok(x3 != x2 && y3 != y2, "coords not changed: (%d %d) (%d %d)\n", x2, y2, x3, y3);
10095     }
10096
10097 done:
10098     DestroyWindow(hwnd);
10099     SetCursorPos(pos.x, pos.y);
10100     flush_events();
10101 }
10102
10103 static INT_PTR CALLBACK wm_quit_dlg_proc(HWND hwnd, UINT message, WPARAM wp, LPARAM lp)
10104 {
10105     struct recvd_message msg;
10106
10107     if (ignore_message( message )) return 0;
10108
10109     msg.hwnd = hwnd;
10110     msg.message = message;
10111     msg.flags = sent|wparam|lparam;
10112     msg.wParam = wp;
10113     msg.lParam = lp;
10114     msg.descr = "dialog";
10115     add_message(&msg);
10116
10117     switch (message)
10118     {
10119     case WM_INITDIALOG:
10120         PostMessage(hwnd, WM_QUIT, 0x1234, 0x5678);
10121         PostMessage(hwnd, WM_USER, 0xdead, 0xbeef);
10122         return 0;
10123
10124     case WM_GETDLGCODE:
10125         return 0;
10126
10127     case WM_USER:
10128         EndDialog(hwnd, 0);
10129         break;
10130     }
10131
10132     return 1;
10133 }
10134
10135 static const struct message WmQuitDialogSeq[] = {
10136     { HCBT_CREATEWND, hook },
10137     { WM_SETFONT, sent },
10138     { WM_INITDIALOG, sent },
10139     { WM_CHANGEUISTATE, sent|optional },
10140     { HCBT_DESTROYWND, hook },
10141     { 0x0090, sent|optional }, /* Vista */
10142     { WM_DESTROY, sent },
10143     { WM_NCDESTROY, sent },
10144     { 0 }
10145 };
10146
10147 static const struct message WmStopQuitSeq[] = {
10148     { WM_DWMNCRENDERINGCHANGED, posted|optional },
10149     { WM_CLOSE, posted },
10150     { WM_QUIT, posted|wparam|lparam, 0x1234, 0 },
10151     { 0 }
10152 };
10153
10154 static void test_quit_message(void)
10155 {
10156     MSG msg;
10157     BOOL ret;
10158
10159     /* test using PostQuitMessage */
10160     flush_events();
10161     PostQuitMessage(0xbeef);
10162
10163     ret = PeekMessage(&msg, NULL, 0, 0, PM_NOREMOVE);
10164     ok(ret, "PeekMessage failed with error %d\n", GetLastError());
10165     ok(msg.message == WM_QUIT, "Received message 0x%04x instead of WM_QUIT\n", msg.message);
10166     ok(msg.wParam == 0xbeef, "wParam was 0x%lx instead of 0xbeef\n", msg.wParam);
10167
10168     ret = PostThreadMessage(GetCurrentThreadId(), WM_USER, 0, 0);
10169     ok(ret, "PostMessage failed with error %d\n", GetLastError());
10170
10171     ret = GetMessage(&msg, NULL, 0, 0);
10172     ok(ret > 0, "GetMessage failed with error %d\n", GetLastError());
10173     ok(msg.message == WM_USER, "Received message 0x%04x instead of WM_USER\n", msg.message);
10174
10175     /* note: WM_QUIT message received after WM_USER message */
10176     ret = GetMessage(&msg, NULL, 0, 0);
10177     ok(!ret, "GetMessage return %d with error %d instead of FALSE\n", ret, GetLastError());
10178     ok(msg.message == WM_QUIT, "Received message 0x%04x instead of WM_QUIT\n", msg.message);
10179     ok(msg.wParam == 0xbeef, "wParam was 0x%lx instead of 0xbeef\n", msg.wParam);
10180
10181     ret = PeekMessage(&msg, NULL, 0, 0, PM_REMOVE);
10182     ok( !ret || msg.message != WM_QUIT, "Received WM_QUIT again\n" );
10183
10184     /* now test with PostThreadMessage - different behaviour! */
10185     PostThreadMessage(GetCurrentThreadId(), WM_QUIT, 0xdead, 0);
10186
10187     ret = PeekMessage(&msg, NULL, 0, 0, PM_NOREMOVE);
10188     ok(ret, "PeekMessage failed with error %d\n", GetLastError());
10189     ok(msg.message == WM_QUIT, "Received message 0x%04x instead of WM_QUIT\n", msg.message);
10190     ok(msg.wParam == 0xdead, "wParam was 0x%lx instead of 0xdead\n", msg.wParam);
10191
10192     ret = PostThreadMessage(GetCurrentThreadId(), WM_USER, 0, 0);
10193     ok(ret, "PostMessage failed with error %d\n", GetLastError());
10194
10195     /* note: we receive the WM_QUIT message first this time */
10196     ret = GetMessage(&msg, NULL, 0, 0);
10197     ok(!ret, "GetMessage return %d with error %d instead of FALSE\n", ret, GetLastError());
10198     ok(msg.message == WM_QUIT, "Received message 0x%04x instead of WM_QUIT\n", msg.message);
10199     ok(msg.wParam == 0xdead, "wParam was 0x%lx instead of 0xdead\n", msg.wParam);
10200
10201     ret = GetMessage(&msg, NULL, 0, 0);
10202     ok(ret > 0, "GetMessage failed with error %d\n", GetLastError());
10203     ok(msg.message == WM_USER, "Received message 0x%04x instead of WM_USER\n", msg.message);
10204
10205     flush_events();
10206     flush_sequence();
10207     ret = DialogBoxParam(GetModuleHandle(0), "TEST_EMPTY_DIALOG", 0, wm_quit_dlg_proc, 0);
10208     ok(ret == 1, "expected 1, got %d\n", ret);
10209     ok_sequence(WmQuitDialogSeq, "WmQuitDialogSeq", FALSE);
10210     memset(&msg, 0xab, sizeof(msg));
10211     ret = PeekMessage(&msg, NULL, 0, 0, PM_REMOVE);
10212     ok(ret, "PeekMessage failed\n");
10213     ok(msg.message == WM_QUIT, "Received message 0x%04x instead of WM_QUIT\n", msg.message);
10214     ok(msg.wParam == 0x1234, "wParam was 0x%lx instead of 0x1234\n", msg.wParam);
10215     ok(msg.lParam == 0, "lParam was 0x%lx instead of 0\n", msg.lParam);
10216
10217     /* Check what happens to a WM_QUIT message posted to a window that gets
10218      * destroyed.
10219      */
10220     CreateWindowExA(0, "StopQuitClass", "Stop Quit Test", WS_OVERLAPPEDWINDOW,
10221                     0, 0, 100, 100, NULL, NULL, NULL, NULL);
10222     flush_sequence();
10223     while (PeekMessage(&msg, 0, 0, 0, PM_REMOVE))
10224     {
10225         struct recvd_message rmsg;
10226         rmsg.hwnd = msg.hwnd;
10227         rmsg.message = msg.message;
10228         rmsg.flags = posted|wparam|lparam;
10229         rmsg.wParam = msg.wParam;
10230         rmsg.lParam = msg.lParam;
10231         rmsg.descr = "stop/quit";
10232         if (msg.message == WM_QUIT)
10233             /* The hwnd can only be checked here */
10234             ok(!msg.hwnd, "The WM_QUIT hwnd was %p instead of NULL\n", msg.hwnd);
10235         add_message(&rmsg);
10236         DispatchMessage(&msg);
10237     }
10238     ok_sequence(WmStopQuitSeq, "WmStopQuitSeq", FALSE);
10239 }
10240
10241 static const struct message WmMouseHoverSeq[] = {
10242     { WM_MOUSEACTIVATE, sent|optional },  /* we can get those when moving the mouse in focus-follow-mouse mode under X11 */
10243     { WM_MOUSEACTIVATE, sent|optional },
10244     { WM_TIMER, sent|optional }, /* XP sends it */
10245     { WM_SYSTIMER, sent },
10246     { WM_MOUSEHOVER, sent|wparam, 0 },
10247     { 0 }
10248 };
10249
10250 static void pump_msg_loop_timeout(DWORD timeout, BOOL inject_mouse_move)
10251 {
10252     MSG msg;
10253     DWORD start_ticks, end_ticks;
10254
10255     start_ticks = GetTickCount();
10256     /* add some deviation (50%) to cover not expected delays */
10257     start_ticks += timeout / 2;
10258
10259     do
10260     {
10261         while (PeekMessage(&msg, 0, 0, 0, PM_REMOVE))
10262         {
10263             /* Timer proc messages are not dispatched to the window proc,
10264              * and therefore not logged.
10265              */
10266             if ((msg.message == WM_TIMER || msg.message == WM_SYSTIMER) && msg.hwnd)
10267             {
10268                 struct recvd_message s_msg;
10269
10270                 s_msg.hwnd = msg.hwnd;
10271                 s_msg.message = msg.message;
10272                 s_msg.flags = sent|wparam|lparam;
10273                 s_msg.wParam = msg.wParam;
10274                 s_msg.lParam = msg.lParam;
10275                 s_msg.descr = "msg_loop";
10276                 add_message(&s_msg);
10277             }
10278             DispatchMessage(&msg);
10279         }
10280
10281         end_ticks = GetTickCount();
10282
10283         /* inject WM_MOUSEMOVE to see how it changes tracking */
10284         if (inject_mouse_move && start_ticks + timeout / 2 >= end_ticks)
10285         {
10286             mouse_event(MOUSEEVENTF_MOVE, -1, 0, 0, 0);
10287             mouse_event(MOUSEEVENTF_MOVE, 1, 0, 0, 0);
10288
10289             inject_mouse_move = FALSE;
10290         }
10291     } while (start_ticks + timeout >= end_ticks);
10292 }
10293
10294 static void test_TrackMouseEvent(void)
10295 {
10296     TRACKMOUSEEVENT tme;
10297     BOOL ret;
10298     HWND hwnd, hchild;
10299     RECT rc_parent, rc_child;
10300     UINT default_hover_time, hover_width = 0, hover_height = 0;
10301
10302 #define track_hover(track_hwnd, track_hover_time) \
10303     tme.cbSize = sizeof(tme); \
10304     tme.dwFlags = TME_HOVER; \
10305     tme.hwndTrack = track_hwnd; \
10306     tme.dwHoverTime = track_hover_time; \
10307     SetLastError(0xdeadbeef); \
10308     ret = pTrackMouseEvent(&tme); \
10309     ok(ret, "TrackMouseEvent(TME_HOVER) error %d\n", GetLastError())
10310
10311 #define track_query(expected_track_flags, expected_track_hwnd, expected_hover_time) \
10312     tme.cbSize = sizeof(tme); \
10313     tme.dwFlags = TME_QUERY; \
10314     tme.hwndTrack = (HWND)0xdeadbeef; \
10315     tme.dwHoverTime = 0xdeadbeef; \
10316     SetLastError(0xdeadbeef); \
10317     ret = pTrackMouseEvent(&tme); \
10318     ok(ret, "TrackMouseEvent(TME_QUERY) error %d\n", GetLastError());\
10319     ok(tme.cbSize == sizeof(tme), "wrong tme.cbSize %u\n", tme.cbSize); \
10320     ok(tme.dwFlags == (expected_track_flags), \
10321        "wrong tme.dwFlags %08x, expected %08x\n", tme.dwFlags, (expected_track_flags)); \
10322     ok(tme.hwndTrack == (expected_track_hwnd), \
10323        "wrong tme.hwndTrack %p, expected %p\n", tme.hwndTrack, (expected_track_hwnd)); \
10324     ok(tme.dwHoverTime == (expected_hover_time), \
10325        "wrong tme.dwHoverTime %u, expected %u\n", tme.dwHoverTime, (expected_hover_time))
10326
10327 #define track_hover_cancel(track_hwnd) \
10328     tme.cbSize = sizeof(tme); \
10329     tme.dwFlags = TME_HOVER | TME_CANCEL; \
10330     tme.hwndTrack = track_hwnd; \
10331     tme.dwHoverTime = 0xdeadbeef; \
10332     SetLastError(0xdeadbeef); \
10333     ret = pTrackMouseEvent(&tme); \
10334     ok(ret, "TrackMouseEvent(TME_HOVER | TME_CANCEL) error %d\n", GetLastError())
10335
10336     default_hover_time = 0xdeadbeef;
10337     SetLastError(0xdeadbeef);
10338     ret = SystemParametersInfo(SPI_GETMOUSEHOVERTIME, 0, &default_hover_time, 0);
10339     ok(ret || broken(GetLastError() == 0xdeadbeef),  /* win9x */
10340        "SystemParametersInfo(SPI_GETMOUSEHOVERTIME) error %u\n", GetLastError());
10341     if (!ret) default_hover_time = 400;
10342     trace("SPI_GETMOUSEHOVERTIME returned %u ms\n", default_hover_time);
10343
10344     SetLastError(0xdeadbeef);
10345     ret = SystemParametersInfo(SPI_GETMOUSEHOVERWIDTH, 0, &hover_width, 0);
10346     ok(ret || broken(GetLastError() == 0xdeadbeef),  /* win9x */
10347        "SystemParametersInfo(SPI_GETMOUSEHOVERWIDTH) error %u\n", GetLastError());
10348     if (!ret) hover_width = 4;
10349     SetLastError(0xdeadbeef);
10350     ret = SystemParametersInfo(SPI_GETMOUSEHOVERHEIGHT, 0, &hover_height, 0);
10351     ok(ret || broken(GetLastError() == 0xdeadbeef),  /* win9x */
10352        "SystemParametersInfo(SPI_GETMOUSEHOVERHEIGHT) error %u\n", GetLastError());
10353     if (!ret) hover_height = 4;
10354     trace("hover rect is %u x %d\n", hover_width, hover_height);
10355
10356     hwnd = CreateWindowEx(0, "TestWindowClass", NULL,
10357                           WS_OVERLAPPEDWINDOW | WS_VISIBLE,
10358                           CW_USEDEFAULT, CW_USEDEFAULT, 300, 300, 0,
10359                           NULL, NULL, 0);
10360     assert(hwnd);
10361
10362     hchild = CreateWindowEx(0, "TestWindowClass", NULL,
10363                           WS_CHILD | WS_BORDER | WS_VISIBLE,
10364                           50, 50, 200, 200, hwnd,
10365                           NULL, NULL, 0);
10366     assert(hchild);
10367
10368     SetWindowPos( hwnd, HWND_TOPMOST, 0, 0, 0, 0, SWP_NOSIZE|SWP_NOMOVE );
10369     flush_events();
10370     flush_sequence();
10371
10372     tme.cbSize = 0;
10373     tme.dwFlags = TME_QUERY;
10374     tme.hwndTrack = (HWND)0xdeadbeef;
10375     tme.dwHoverTime = 0xdeadbeef;
10376     SetLastError(0xdeadbeef);
10377     ret = pTrackMouseEvent(&tme);
10378     ok(!ret, "TrackMouseEvent should fail\n");
10379     ok(GetLastError() == ERROR_INVALID_PARAMETER || broken(GetLastError() == 0xdeadbeef),
10380        "not expected error %u\n", GetLastError());
10381
10382     tme.cbSize = sizeof(tme);
10383     tme.dwFlags = TME_HOVER;
10384     tme.hwndTrack = (HWND)0xdeadbeef;
10385     tme.dwHoverTime = 0xdeadbeef;
10386     SetLastError(0xdeadbeef);
10387     ret = pTrackMouseEvent(&tme);
10388     ok(!ret, "TrackMouseEvent should fail\n");
10389     ok(GetLastError() == ERROR_INVALID_WINDOW_HANDLE || broken(GetLastError() == 0xdeadbeef),
10390        "not expected error %u\n", GetLastError());
10391
10392     tme.cbSize = sizeof(tme);
10393     tme.dwFlags = TME_HOVER | TME_CANCEL;
10394     tme.hwndTrack = (HWND)0xdeadbeef;
10395     tme.dwHoverTime = 0xdeadbeef;
10396     SetLastError(0xdeadbeef);
10397     ret = pTrackMouseEvent(&tme);
10398     ok(!ret, "TrackMouseEvent should fail\n");
10399     ok(GetLastError() == ERROR_INVALID_WINDOW_HANDLE || broken(GetLastError() == 0xdeadbeef),
10400        "not expected error %u\n", GetLastError());
10401
10402     GetWindowRect(hwnd, &rc_parent);
10403     GetWindowRect(hchild, &rc_child);
10404     SetCursorPos(rc_child.left - 10, rc_child.top - 10);
10405
10406     /* Process messages so that the system updates its internal current
10407      * window and hittest, otherwise TrackMouseEvent calls don't have any
10408      * effect.
10409      */
10410     flush_events();
10411     flush_sequence();
10412
10413     track_query(0, NULL, 0);
10414     track_hover(hchild, 0);
10415     track_query(0, NULL, 0);
10416
10417     flush_events();
10418     flush_sequence();
10419
10420     track_hover(hwnd, 0);
10421     tme.cbSize = sizeof(tme);
10422     tme.dwFlags = TME_QUERY;
10423     tme.hwndTrack = (HWND)0xdeadbeef;
10424     tme.dwHoverTime = 0xdeadbeef;
10425     SetLastError(0xdeadbeef);
10426     ret = pTrackMouseEvent(&tme);
10427     ok(ret, "TrackMouseEvent(TME_QUERY) error %d\n", GetLastError());
10428     ok(tme.cbSize == sizeof(tme), "wrong tme.cbSize %u\n", tme.cbSize);
10429     if (!tme.dwFlags)
10430     {
10431         skip( "Cursor not inside window, skipping TrackMouseEvent tests\n" );
10432         DestroyWindow( hwnd );
10433         return;
10434     }
10435     ok(tme.dwFlags == TME_HOVER, "wrong tme.dwFlags %08x, expected TME_HOVER\n", tme.dwFlags);
10436     ok(tme.hwndTrack == hwnd, "wrong tme.hwndTrack %p, expected %p\n", tme.hwndTrack, hwnd);
10437     ok(tme.dwHoverTime == default_hover_time, "wrong tme.dwHoverTime %u, expected %u\n",
10438        tme.dwHoverTime, default_hover_time);
10439
10440     pump_msg_loop_timeout(default_hover_time, FALSE);
10441     ok_sequence(WmMouseHoverSeq, "WmMouseHoverSeq", FALSE);
10442
10443     track_query(0, NULL, 0);
10444
10445     track_hover(hwnd, HOVER_DEFAULT);
10446     track_query(TME_HOVER, hwnd, default_hover_time);
10447
10448     Sleep(default_hover_time / 2);
10449     mouse_event(MOUSEEVENTF_MOVE, -1, 0, 0, 0);
10450     mouse_event(MOUSEEVENTF_MOVE, 1, 0, 0, 0);
10451
10452     track_query(TME_HOVER, hwnd, default_hover_time);
10453
10454     pump_msg_loop_timeout(default_hover_time, FALSE);
10455     ok_sequence(WmMouseHoverSeq, "WmMouseHoverSeq", FALSE);
10456
10457     track_query(0, NULL, 0);
10458
10459     track_hover(hwnd, HOVER_DEFAULT);
10460     track_query(TME_HOVER, hwnd, default_hover_time);
10461
10462     pump_msg_loop_timeout(default_hover_time, TRUE);
10463     ok_sequence(WmMouseHoverSeq, "WmMouseHoverSeq", FALSE);
10464
10465     track_query(0, NULL, 0);
10466
10467     track_hover(hwnd, HOVER_DEFAULT);
10468     track_query(TME_HOVER, hwnd, default_hover_time);
10469     track_hover_cancel(hwnd);
10470
10471     DestroyWindow(hwnd);
10472
10473 #undef track_hover
10474 #undef track_query
10475 #undef track_hover_cancel
10476 }
10477
10478
10479 static const struct message WmSetWindowRgn[] = {
10480     { WM_WINDOWPOSCHANGING, sent|wparam, SWP_NOCLIENTSIZE|SWP_NOCLIENTMOVE|SWP_NOACTIVATE|SWP_FRAMECHANGED|SWP_NOSIZE|SWP_NOMOVE },
10481     { WM_NCCALCSIZE, sent|wparam, 1 },
10482     { WM_NCPAINT, sent|optional }, /* wparam != 1 */
10483     { WM_GETTEXT, sent|defwinproc|optional },
10484     { WM_ERASEBKGND, sent|optional },
10485     { WM_WINDOWPOSCHANGED, sent|wparam, SWP_NOCLIENTSIZE|SWP_NOCLIENTMOVE|SWP_NOACTIVATE|SWP_FRAMECHANGED|SWP_NOSIZE|SWP_NOMOVE },
10486     { EVENT_OBJECT_LOCATIONCHANGE, winevent_hook|wparam|lparam, 0, 0 },
10487     { 0 }
10488 };
10489
10490 static const struct message WmSetWindowRgn_no_redraw[] = {
10491     { WM_WINDOWPOSCHANGING, sent|wparam, SWP_NOCLIENTSIZE|SWP_NOCLIENTMOVE|SWP_NOACTIVATE|SWP_FRAMECHANGED|SWP_NOSIZE|SWP_NOMOVE|SWP_NOREDRAW },
10492     { WM_NCCALCSIZE, sent|wparam, 1 },
10493     { WM_WINDOWPOSCHANGED, sent|wparam, SWP_NOCLIENTSIZE|SWP_NOCLIENTMOVE|SWP_NOACTIVATE|SWP_FRAMECHANGED|SWP_NOSIZE|SWP_NOMOVE|SWP_NOREDRAW },
10494     { EVENT_OBJECT_LOCATIONCHANGE, winevent_hook|wparam|lparam, 0, 0 },
10495     { 0 }
10496 };
10497
10498 static const struct message WmSetWindowRgn_clear[] = {
10499     { WM_WINDOWPOSCHANGING, sent/*|wparam*/, SWP_NOACTIVATE|SWP_FRAMECHANGED|SWP_NOSIZE|SWP_NOMOVE/*|SWP_NOCLIENTSIZE|SWP_NOCLIENTMOVE only on some Windows versions */ },
10500     { WM_NCCALCSIZE, sent|wparam, 1 },
10501     { WM_NCPAINT, sent|optional },
10502     { WM_GETTEXT, sent|defwinproc|optional },
10503     { WM_ERASEBKGND, sent|optional }, /* FIXME: remove optional once Wine is fixed */
10504     { WM_WINDOWPOSCHANGED, sent|wparam, SWP_NOCLIENTSIZE|SWP_NOCLIENTMOVE|SWP_NOACTIVATE|SWP_FRAMECHANGED|SWP_NOSIZE|SWP_NOMOVE },
10505     { WM_NCCALCSIZE, sent|wparam|optional, 1 },
10506     { WM_NCPAINT, sent|optional },
10507     { WM_GETTEXT, sent|defwinproc|optional },
10508     { WM_ERASEBKGND, sent|optional },
10509     { WM_WINDOWPOSCHANGING, sent|optional },
10510     { WM_NCCALCSIZE, sent|optional|wparam, 1 },
10511     { WM_NCPAINT, sent|optional },
10512     { WM_GETTEXT, sent|defwinproc|optional },
10513     { WM_ERASEBKGND, sent|optional },
10514     { WM_WINDOWPOSCHANGED, sent|optional|wparam, SWP_NOCLIENTSIZE|SWP_NOCLIENTMOVE|SWP_NOACTIVATE|SWP_FRAMECHANGED|SWP_NOSIZE|SWP_NOMOVE },
10515     { WM_NCCALCSIZE, sent|optional|wparam, 1 },
10516     { WM_NCPAINT, sent|optional },
10517     { WM_GETTEXT, sent|defwinproc|optional },
10518     { WM_ERASEBKGND, sent|optional },
10519     { EVENT_OBJECT_LOCATIONCHANGE, winevent_hook|wparam|lparam, 0, 0 },
10520     { EVENT_OBJECT_LOCATIONCHANGE, winevent_hook|wparam|lparam, 0, 0 },
10521     { 0 }
10522 };
10523
10524 static void test_SetWindowRgn(void)
10525 {
10526     HRGN hrgn;
10527     HWND hwnd = CreateWindowExA(0, "TestWindowClass", "Test overlapped", WS_OVERLAPPEDWINDOW,
10528                                 100, 100, 200, 200, 0, 0, 0, NULL);
10529     ok( hwnd != 0, "Failed to create overlapped window\n" );
10530
10531     ShowWindow( hwnd, SW_SHOW );
10532     UpdateWindow( hwnd );
10533     flush_events();
10534     flush_sequence();
10535
10536     trace("testing SetWindowRgn\n");
10537     hrgn = CreateRectRgn( 0, 0, 150, 150 );
10538     SetWindowRgn( hwnd, hrgn, TRUE );
10539     ok_sequence( WmSetWindowRgn, "WmSetWindowRgn", FALSE );
10540
10541     hrgn = CreateRectRgn( 30, 30, 160, 160 );
10542     SetWindowRgn( hwnd, hrgn, FALSE );
10543     ok_sequence( WmSetWindowRgn_no_redraw, "WmSetWindowRgn_no_redraw", FALSE );
10544
10545     hrgn = CreateRectRgn( 0, 0, 180, 180 );
10546     SetWindowRgn( hwnd, hrgn, TRUE );
10547     ok_sequence( WmSetWindowRgn, "WmSetWindowRgn2", FALSE );
10548
10549     SetWindowRgn( hwnd, 0, TRUE );
10550     ok_sequence( WmSetWindowRgn_clear, "WmSetWindowRgn_clear", FALSE );
10551
10552     DestroyWindow( hwnd );
10553 }
10554
10555 /*************************** ShowWindow() test ******************************/
10556 static const struct message WmShowNormal[] = {
10557     { WM_SHOWWINDOW, sent|wparam, 1 },
10558     { WM_WINDOWPOSCHANGING, sent|wparam, SWP_SHOWWINDOW|SWP_NOSIZE|SWP_NOMOVE },
10559     { HCBT_ACTIVATE, hook },
10560     { WM_WINDOWPOSCHANGING, sent|wparam|optional, SWP_NOSIZE|SWP_NOMOVE }, /* win2003 doesn't send it */
10561     { HCBT_SETFOCUS, hook },
10562     { WM_WINDOWPOSCHANGED, sent|wparam, SWP_SHOWWINDOW|SWP_NOSIZE|SWP_NOMOVE|SWP_NOCLIENTSIZE|SWP_NOCLIENTMOVE },
10563     { 0 }
10564 };
10565 static const struct message WmShow[] = {
10566     { WM_SHOWWINDOW, sent|wparam, 1 },
10567     { WM_WINDOWPOSCHANGING, sent|wparam, SWP_SHOWWINDOW|SWP_NOSIZE|SWP_NOMOVE },
10568     { HCBT_ACTIVATE, hook|optional }, /* win2000 doesn't send it */
10569     { WM_WINDOWPOSCHANGING, sent|wparam|optional, SWP_NOSIZE|SWP_NOMOVE }, /* win2000 doesn't send it */
10570     { HCBT_SETFOCUS, hook|optional }, /* win2000 doesn't send it */
10571     { WM_WINDOWPOSCHANGED, sent|wparam, SWP_SHOWWINDOW|SWP_NOSIZE|SWP_NOMOVE|SWP_NOCLIENTSIZE|SWP_NOCLIENTMOVE },
10572     { 0 }
10573 };
10574 static const struct message WmShowNoActivate_1[] = {
10575     { HCBT_MINMAX, hook|lparam, 0, SW_SHOWNOACTIVATE },
10576     { WM_WINDOWPOSCHANGING, sent|wparam, SWP_SHOWWINDOW|SWP_NOACTIVATE|SWP_FRAMECHANGED|SWP_STATECHANGED, 0, SWP_NOCLIENTSIZE|SWP_NOCLIENTMOVE|SWP_NOSIZE|SWP_NOMOVE },
10577     { WM_WINDOWPOSCHANGED, sent|wparam, SWP_SHOWWINDOW|SWP_NOACTIVATE|SWP_FRAMECHANGED|SWP_STATECHANGED, 0, SWP_NOCLIENTSIZE|SWP_NOCLIENTMOVE|SWP_NOSIZE|SWP_NOMOVE },
10578     { WM_MOVE, sent|defwinproc|optional },
10579     { WM_SIZE, sent|wparam|defwinproc, SIZE_RESTORED },
10580     { 0 }
10581 };
10582 static const struct message WmShowNoActivate_2[] = {
10583     { HCBT_MINMAX, hook|lparam, 0, SW_SHOWNOACTIVATE },
10584     { WM_WINDOWPOSCHANGING, sent|wparam, SWP_SHOWWINDOW|SWP_NOACTIVATE|SWP_FRAMECHANGED|SWP_NOCOPYBITS|SWP_STATECHANGED },
10585     { WM_WINDOWPOSCHANGED, sent|wparam, SWP_SHOWWINDOW|SWP_NOACTIVATE|SWP_FRAMECHANGED|SWP_NOCOPYBITS|SWP_STATECHANGED },
10586     { WM_MOVE, sent|defwinproc },
10587     { WM_SIZE, sent|wparam|defwinproc, SIZE_RESTORED },
10588     { HCBT_SETFOCUS, hook|optional },
10589     { HCBT_ACTIVATE, hook|optional }, /* win2003 doesn't send it */
10590     { WM_WINDOWPOSCHANGING, sent|wparam|optional, SWP_NOSIZE|SWP_NOMOVE }, /* win2003 doesn't send it */
10591     { WM_WINDOWPOSCHANGED, sent|wparam|optional, SWP_NOSIZE|SWP_NOMOVE|SWP_NOCLIENTSIZE|SWP_NOCLIENTMOVE },
10592     { HCBT_SETFOCUS, hook|optional }, /* win2003 doesn't send it */
10593     { 0 }
10594 };
10595 static const struct message WmShowNA_1[] = {
10596     { WM_SHOWWINDOW, sent|wparam, 1 },
10597     { WM_WINDOWPOSCHANGING, sent|wparam, SWP_SHOWWINDOW|SWP_NOACTIVATE|SWP_NOSIZE|SWP_NOMOVE },
10598     { WM_WINDOWPOSCHANGED, sent|wparam, SWP_SHOWWINDOW|SWP_NOACTIVATE|SWP_NOSIZE|SWP_NOMOVE|SWP_NOCLIENTSIZE|SWP_NOCLIENTMOVE },
10599     { 0 }
10600 };
10601 static const struct message WmShowNA_2[] = {
10602     { WM_SHOWWINDOW, sent|wparam, 1 },
10603     { WM_WINDOWPOSCHANGING, sent|wparam, SWP_SHOWWINDOW|SWP_NOACTIVATE|SWP_NOSIZE|SWP_NOMOVE },
10604     { WM_WINDOWPOSCHANGED, sent|wparam|optional, SWP_NOACTIVATE|SWP_NOSIZE|SWP_NOMOVE|SWP_NOCLIENTSIZE|SWP_NOCLIENTMOVE },
10605     { 0 }
10606 };
10607 static const struct message WmRestore_1[] = {
10608     { HCBT_MINMAX, hook|lparam, 0, SW_RESTORE },
10609     { WM_WINDOWPOSCHANGING, sent|wparam, SWP_SHOWWINDOW|SWP_FRAMECHANGED|SWP_NOCOPYBITS|SWP_STATECHANGED },
10610     { HCBT_ACTIVATE, hook|optional }, /* win2000 doesn't send it */
10611     { WM_WINDOWPOSCHANGING, sent|wparam|optional, SWP_NOSIZE|SWP_NOMOVE }, /* win2000 doesn't send it */
10612     { HCBT_SETFOCUS, hook|optional }, /* win2000 doesn't send it */
10613     { WM_WINDOWPOSCHANGED, sent|wparam, SWP_SHOWWINDOW|SWP_FRAMECHANGED|SWP_NOCOPYBITS|SWP_STATECHANGED },
10614     { WM_MOVE, sent|defwinproc },
10615     { WM_SIZE, sent|wparam|defwinproc, SIZE_RESTORED },
10616     { HCBT_SETFOCUS, hook|optional }, /* win2000 sends it */
10617     { 0 }
10618 };
10619 static const struct message WmRestore_2[] = {
10620     { WM_SHOWWINDOW, sent|wparam, 1 },
10621     { WM_WINDOWPOSCHANGING, sent|wparam, SWP_SHOWWINDOW|SWP_NOSIZE|SWP_NOMOVE },
10622     { HCBT_ACTIVATE, hook|optional }, /* win2000 doesn't send it */
10623     { WM_WINDOWPOSCHANGING, sent|wparam|optional, SWP_NOSIZE|SWP_NOMOVE }, /* win2000 doesn't send it */
10624     { HCBT_SETFOCUS, hook|optional }, /* win2000 doesn't send it */
10625     { WM_WINDOWPOSCHANGED, sent|wparam, SWP_SHOWWINDOW|SWP_NOSIZE|SWP_NOMOVE|SWP_NOCLIENTSIZE|SWP_NOCLIENTMOVE },
10626     { 0 }
10627 };
10628 static const struct message WmRestore_3[] = {
10629     { HCBT_MINMAX, hook|lparam, 0, SW_RESTORE },
10630     { WM_GETMINMAXINFO, sent },
10631     { WM_WINDOWPOSCHANGING, sent|wparam, SWP_FRAMECHANGED|SWP_NOCOPYBITS|SWP_STATECHANGED },
10632     { HCBT_ACTIVATE, hook|optional }, /* win2003 doesn't send it */
10633     { WM_WINDOWPOSCHANGING, sent|wparam|optional, SWP_NOSIZE|SWP_NOMOVE }, /* win2003 doesn't send it */
10634     { HCBT_SETFOCUS, hook|optional }, /* win2003 doesn't send it */
10635     { WM_WINDOWPOSCHANGED, sent|wparam, SWP_FRAMECHANGED|SWP_NOCOPYBITS|SWP_STATECHANGED },
10636     { WM_MOVE, sent|defwinproc },
10637     { WM_SIZE, sent|wparam|defwinproc, SIZE_MAXIMIZED },
10638     { HCBT_SETFOCUS, hook|optional }, /* win2003 sends it */
10639     { 0 }
10640 };
10641 static const struct message WmRestore_4[] = {
10642     { HCBT_MINMAX, hook|lparam|optional, 0, SW_RESTORE },
10643     { WM_WINDOWPOSCHANGING, sent|wparam|optional, SWP_FRAMECHANGED|SWP_STATECHANGED, 0, SWP_NOCLIENTSIZE|SWP_NOCLIENTMOVE|SWP_NOSIZE|SWP_NOMOVE },
10644     { WM_WINDOWPOSCHANGED, sent|wparam|optional, SWP_FRAMECHANGED|SWP_STATECHANGED, 0, SWP_NOCLIENTSIZE|SWP_NOCLIENTMOVE|SWP_NOSIZE|SWP_NOMOVE },
10645     { WM_MOVE, sent|defwinproc|optional },
10646     { WM_SIZE, sent|wparam|defwinproc|optional, SIZE_RESTORED },
10647     { 0 }
10648 };
10649 static const struct message WmRestore_5[] = {
10650     { HCBT_MINMAX, hook|lparam|optional, 0, SW_SHOWNORMAL },
10651     { WM_WINDOWPOSCHANGING, sent|wparam|optional, SWP_FRAMECHANGED|SWP_STATECHANGED, 0, SWP_NOCLIENTSIZE|SWP_NOCLIENTMOVE|SWP_NOSIZE|SWP_NOMOVE },
10652     { HCBT_ACTIVATE, hook|optional },
10653     { HCBT_SETFOCUS, hook|optional },
10654     { WM_WINDOWPOSCHANGED, sent|wparam|optional, SWP_FRAMECHANGED|SWP_STATECHANGED, 0, SWP_NOCLIENTSIZE|SWP_NOCLIENTMOVE|SWP_NOSIZE|SWP_NOMOVE },
10655     { WM_MOVE, sent|defwinproc|optional },
10656     { WM_SIZE, sent|wparam|defwinproc|optional, SIZE_RESTORED },
10657     { 0 }
10658 };
10659 static const struct message WmHide_1[] = {
10660     { WM_SHOWWINDOW, sent|wparam, 0 },
10661     { WM_WINDOWPOSCHANGING, sent|wparam, SWP_HIDEWINDOW|SWP_NOSIZE|SWP_NOMOVE, 0, SWP_NOACTIVATE },
10662     { WM_WINDOWPOSCHANGED, sent|wparam, SWP_HIDEWINDOW|SWP_NOSIZE|SWP_NOMOVE|SWP_NOCLIENTSIZE|SWP_NOCLIENTMOVE, 0, SWP_NOACTIVATE },
10663     { HCBT_ACTIVATE, hook|optional },
10664     { HCBT_SETFOCUS, hook|optional }, /* win2000 sends it */
10665     { 0 }
10666 };
10667 static const struct message WmHide_2[] = {
10668     { WM_SHOWWINDOW, sent|wparam, 0 },
10669     { WM_WINDOWPOSCHANGING, sent /*|wparam, SWP_HIDEWINDOW|SWP_NOACTIVATE|SWP_NOSIZE|SWP_NOMOVE*/ }, /* win2000 doesn't add SWP_NOACTIVATE */
10670     { WM_WINDOWPOSCHANGED, sent /*|wparam, SWP_HIDEWINDOW|SWP_NOACTIVATE|SWP_NOSIZE|SWP_NOMOVE|SWP_NOCLIENTSIZE|SWP_NOCLIENTMOVE*/ }, /* win2000 doesn't add SWP_NOACTIVATE */
10671     { HCBT_ACTIVATE, hook|optional },
10672     { 0 }
10673 };
10674 static const struct message WmHide_3[] = {
10675     { WM_SHOWWINDOW, sent|wparam, 0 },
10676     { WM_WINDOWPOSCHANGING, sent|wparam, SWP_HIDEWINDOW|SWP_NOSIZE|SWP_NOMOVE },
10677     { WM_WINDOWPOSCHANGED, sent|wparam, SWP_HIDEWINDOW|SWP_NOSIZE|SWP_NOMOVE|SWP_NOCLIENTSIZE|SWP_NOCLIENTMOVE },
10678     { HCBT_SETFOCUS, hook|optional },
10679     { EVENT_OBJECT_FOCUS, winevent_hook|wparam|lparam, OBJID_CLIENT, 0 },
10680     { 0 }
10681 };
10682 static const struct message WmShowMinimized_1[] = {
10683     { HCBT_MINMAX, hook|lparam, 0, SW_SHOWMINIMIZED },
10684     { WM_WINDOWPOSCHANGING, sent|wparam, SWP_SHOWWINDOW|SWP_FRAMECHANGED|SWP_NOCOPYBITS|SWP_STATECHANGED },
10685     { HCBT_ACTIVATE, hook|optional }, /* win2000 doesn't send it */
10686     { WM_WINDOWPOSCHANGING, sent|wparam|optional, SWP_NOSIZE|SWP_NOMOVE }, /* win2000 doesn't send it */
10687     { WM_WINDOWPOSCHANGED, sent|wparam, SWP_SHOWWINDOW|SWP_FRAMECHANGED|SWP_NOCOPYBITS|SWP_STATECHANGED },
10688     { WM_MOVE, sent|defwinproc },
10689     { WM_SIZE, sent|wparam|lparam|defwinproc, SIZE_MINIMIZED, 0 },
10690     { 0 }
10691 };
10692 static const struct message WmMinimize_1[] = {
10693     { HCBT_MINMAX, hook|lparam, 0, SW_MINIMIZE },
10694     { HCBT_SETFOCUS, hook|optional }, /* win2000 doesn't send it */
10695     { EVENT_OBJECT_FOCUS, winevent_hook|wparam|lparam, OBJID_CLIENT, 0 },
10696     { WM_WINDOWPOSCHANGING, sent|wparam, SWP_SHOWWINDOW|SWP_NOACTIVATE|SWP_FRAMECHANGED|SWP_NOCOPYBITS|SWP_STATECHANGED },
10697     { WM_WINDOWPOSCHANGED, sent|wparam, SWP_NOACTIVATE|SWP_FRAMECHANGED|SWP_NOCOPYBITS|SWP_STATECHANGED },
10698     { WM_MOVE, sent|defwinproc },
10699     { WM_SIZE, sent|wparam|lparam|defwinproc, SIZE_MINIMIZED, 0 },
10700     { 0 }
10701 };
10702 static const struct message WmMinimize_2[] = {
10703     { HCBT_MINMAX, hook|lparam, 0, SW_MINIMIZE },
10704     { HCBT_SETFOCUS, hook|optional },
10705     { WM_WINDOWPOSCHANGING, sent|wparam, SWP_SHOWWINDOW|SWP_NOACTIVATE|SWP_FRAMECHANGED|SWP_NOCOPYBITS|SWP_STATECHANGED },
10706     { WM_WINDOWPOSCHANGED, sent|wparam, SWP_NOACTIVATE|SWP_FRAMECHANGED|SWP_NOCOPYBITS|SWP_STATECHANGED },
10707     { WM_MOVE, sent|defwinproc },
10708     { WM_SIZE, sent|wparam|lparam|defwinproc, SIZE_MINIMIZED, 0 },
10709     { 0 }
10710 };
10711 static const struct message WmMinimize_3[] = {
10712     { HCBT_MINMAX, hook|lparam, 0, SW_MINIMIZE },
10713     { WM_WINDOWPOSCHANGING, sent|wparam, SWP_SHOWWINDOW|SWP_NOACTIVATE|SWP_FRAMECHANGED|SWP_NOCOPYBITS|SWP_STATECHANGED },
10714     { WM_WINDOWPOSCHANGED, sent|wparam, SWP_SHOWWINDOW|SWP_NOACTIVATE|SWP_FRAMECHANGED|SWP_NOCOPYBITS|SWP_STATECHANGED },
10715     { WM_MOVE, sent|defwinproc },
10716     { WM_SIZE, sent|wparam|lparam|defwinproc, SIZE_MINIMIZED, 0 },
10717     { 0 }
10718 };
10719 static const struct message WmShowMinNoActivate[] = {
10720     { HCBT_MINMAX, hook|lparam, 0, SW_SHOWMINNOACTIVE },
10721     { WM_WINDOWPOSCHANGING, sent },
10722     { WM_WINDOWPOSCHANGED, sent },
10723     { WM_MOVE, sent|defwinproc|optional },
10724     { WM_SIZE, sent|wparam|lparam|defwinproc|optional, SIZE_MINIMIZED, 0 },
10725     { 0 }
10726 };
10727 static const struct message WmMinMax_1[] = {
10728     { HCBT_MINMAX, hook|lparam, 0, SW_SHOWMINIMIZED },
10729     { 0 }
10730 };
10731 static const struct message WmMinMax_2[] = {
10732     { HCBT_MINMAX, hook|lparam, 0, SW_SHOWMAXIMIZED },
10733     { WM_GETMINMAXINFO, sent|optional },
10734     { WM_WINDOWPOSCHANGING, sent|wparam|optional, SWP_FRAMECHANGED|SWP_STATECHANGED },
10735     { HCBT_ACTIVATE, hook|optional },
10736     { WM_WINDOWPOSCHANGING, sent|wparam|optional, SWP_NOSIZE|SWP_NOMOVE },
10737     { HCBT_SETFOCUS, hook|optional },
10738     { WM_WINDOWPOSCHANGED, sent|wparam|optional, SWP_FRAMECHANGED|SWP_STATECHANGED, 0, SWP_NOSIZE|SWP_NOMOVE|SWP_NOCLIENTSIZE|SWP_NOCLIENTMOVE },
10739     { WM_MOVE, sent|defwinproc|optional },
10740     { WM_SIZE, sent|wparam|defwinproc|optional, SIZE_MAXIMIZED },
10741     { HCBT_SETFOCUS, hook|optional },
10742     { 0 }
10743 };
10744 static const struct message WmMinMax_3[] = {
10745     { HCBT_MINMAX, hook|lparam, 0, SW_MINIMIZE },
10746     { HCBT_SETFOCUS, hook|optional },
10747     { WM_WINDOWPOSCHANGING, sent|wparam|optional, SWP_SHOWWINDOW|SWP_NOACTIVATE|SWP_FRAMECHANGED|SWP_NOCOPYBITS|SWP_STATECHANGED },
10748     { WM_WINDOWPOSCHANGED, sent|wparam|optional, SWP_NOACTIVATE|SWP_FRAMECHANGED|SWP_NOCOPYBITS|SWP_STATECHANGED },
10749     { WM_MOVE, sent|defwinproc|optional },
10750     { WM_SIZE, sent|wparam|lparam|defwinproc|optional, SIZE_MINIMIZED, 0 },
10751     { 0 }
10752 };
10753 static const struct message WmMinMax_4[] = {
10754     { HCBT_MINMAX, hook|lparam, 0, SW_SHOWMINNOACTIVE },
10755     { 0 }
10756 };
10757 static const struct message WmShowMaximized_1[] = {
10758     { HCBT_MINMAX, hook|lparam, 0, SW_SHOWMAXIMIZED },
10759     { WM_GETMINMAXINFO, sent },
10760     { WM_WINDOWPOSCHANGING, sent|wparam, SWP_SHOWWINDOW|SWP_FRAMECHANGED|SWP_NOCOPYBITS|SWP_STATECHANGED },
10761     { HCBT_ACTIVATE, hook|optional }, /* win2000 doesn't send it */
10762     { WM_WINDOWPOSCHANGING, sent|wparam|optional, SWP_NOSIZE|SWP_NOMOVE }, /* win2000 doesn't send it */
10763     { HCBT_SETFOCUS, hook|optional }, /* win2000 doesn't send it */
10764     { WM_WINDOWPOSCHANGED, sent|wparam, SWP_SHOWWINDOW|SWP_FRAMECHANGED|SWP_NOCOPYBITS|SWP_STATECHANGED },
10765     { WM_MOVE, sent|defwinproc },
10766     { WM_SIZE, sent|wparam|defwinproc, SIZE_MAXIMIZED },
10767     { HCBT_SETFOCUS, hook|optional }, /* win2003 sends it */
10768     { 0 }
10769 };
10770 static const struct message WmShowMaximized_2[] = {
10771     { HCBT_MINMAX, hook|lparam, 0, SW_SHOWMAXIMIZED },
10772     { WM_GETMINMAXINFO, sent },
10773     { WM_WINDOWPOSCHANGING, sent|optional },
10774     { HCBT_ACTIVATE, hook|optional },
10775     { WM_WINDOWPOSCHANGED, sent|optional },
10776     { WM_MOVE, sent|optional }, /* Win9x doesn't send it */
10777     { WM_SIZE, sent|wparam|optional, SIZE_MAXIMIZED }, /* Win9x doesn't send it */
10778     { WM_WINDOWPOSCHANGING, sent|optional },
10779     { HCBT_SETFOCUS, hook|optional },
10780     { WM_WINDOWPOSCHANGED, sent|wparam|optional, SWP_FRAMECHANGED|SWP_NOCOPYBITS|SWP_STATECHANGED },
10781     { WM_MOVE, sent|defwinproc },
10782     { WM_SIZE, sent|wparam|defwinproc, SIZE_MAXIMIZED },
10783     { HCBT_SETFOCUS, hook|optional },
10784     { 0 }
10785 };
10786 static const struct message WmShowMaximized_3[] = {
10787     { HCBT_MINMAX, hook|lparam, 0, SW_SHOWMAXIMIZED },
10788     { WM_GETMINMAXINFO, sent|optional },
10789     { WM_WINDOWPOSCHANGING, sent|wparam, SWP_FRAMECHANGED|SWP_STATECHANGED, 0, SWP_NOCLIENTSIZE|SWP_NOCLIENTMOVE|SWP_NOSIZE|SWP_NOMOVE },
10790     { HCBT_ACTIVATE, hook|optional }, /* win2000 doesn't send it */
10791     { WM_WINDOWPOSCHANGING, sent|wparam|optional, SWP_NOSIZE|SWP_NOMOVE }, /* win2000 doesn't send it */
10792     { HCBT_SETFOCUS, hook|optional }, /* win2000 doesn't send it */
10793     { WM_WINDOWPOSCHANGED, sent|wparam, SWP_FRAMECHANGED|SWP_STATECHANGED, 0, SWP_NOCLIENTSIZE|SWP_NOCLIENTMOVE|SWP_NOSIZE|SWP_NOMOVE },
10794     { WM_MOVE, sent|defwinproc|optional },
10795     { WM_SIZE, sent|wparam|defwinproc, SIZE_MAXIMIZED },
10796     { 0 }
10797 };
10798
10799 static void test_ShowWindow(void)
10800 {
10801     /* ShowWindow commands in random order */
10802     static const struct
10803     {
10804         INT cmd; /* ShowWindow command */
10805         LPARAM ret; /* ShowWindow return value */
10806         DWORD style; /* window style after the command */
10807         const struct message *msg; /* message sequence the command produces */
10808         INT wp_cmd, wp_flags; /* window placement after the command */
10809         POINT wp_min, wp_max; /* window placement after the command */
10810         BOOL todo_msg; /* message sequence doesn't match what Wine does */
10811     } sw[] =
10812     {
10813 /*  1 */ { SW_SHOWNORMAL, FALSE, WS_VISIBLE, WmShowNormal,
10814            SW_SHOWNORMAL, 0, {-1,-1}, {-1,-1}, FALSE },
10815 /*  2 */ { SW_SHOWNORMAL, TRUE, WS_VISIBLE, WmEmptySeq,
10816            SW_SHOWNORMAL, 0, {-1,-1}, {-1,-1}, FALSE },
10817 /*  3 */ { SW_HIDE, TRUE, 0, WmHide_1,
10818            SW_SHOWNORMAL, 0, {-1,-1}, {-1,-1}, FALSE },
10819 /*  4 */ { SW_HIDE, FALSE, 0, WmEmptySeq,
10820            SW_SHOWNORMAL, 0, {-1,-1}, {-1,-1}, FALSE },
10821 /*  5 */ { SW_SHOWMINIMIZED, FALSE, WS_VISIBLE|WS_MINIMIZE, WmShowMinimized_1,
10822            SW_SHOWMINIMIZED, 0, {-32000,-32000}, {-1,-1}, FALSE },
10823 /*  6 */ { SW_SHOWMINIMIZED, TRUE, WS_VISIBLE|WS_MINIMIZE, WmMinMax_1,
10824            SW_SHOWMINIMIZED, 0, {-32000,-32000}, {-1,-1}, FALSE },
10825 /*  7 */ { SW_HIDE, TRUE, WS_MINIMIZE, WmHide_1,
10826            SW_SHOWMINIMIZED, 0, {-32000,-32000}, {-1,-1}, FALSE },
10827 /*  8 */ { SW_HIDE, FALSE, WS_MINIMIZE, WmEmptySeq,
10828            SW_SHOWMINIMIZED, 0, {-32000,-32000}, {-1,-1}, FALSE },
10829 /*  9 */ { SW_SHOWMAXIMIZED, FALSE, WS_VISIBLE|WS_MAXIMIZE, WmShowMaximized_1,
10830            SW_SHOWMAXIMIZED, WPF_RESTORETOMAXIMIZED, {-32000,-32000}, {-1,-1}, FALSE },
10831 /* 10 */ { SW_SHOWMAXIMIZED, TRUE, WS_VISIBLE|WS_MAXIMIZE, WmMinMax_2,
10832            SW_SHOWMAXIMIZED, WPF_RESTORETOMAXIMIZED, {-32000,-32000}, {-1,-1}, FALSE },
10833 /* 11 */ { SW_HIDE, TRUE, WS_MAXIMIZE, WmHide_1,
10834            SW_SHOWMAXIMIZED, WPF_RESTORETOMAXIMIZED, {-32000,-32000}, {-1,-1}, FALSE },
10835 /* 12 */ { SW_HIDE, FALSE, WS_MAXIMIZE, WmEmptySeq,
10836            SW_SHOWMAXIMIZED, WPF_RESTORETOMAXIMIZED, {-32000,-32000}, {-1,-1}, FALSE },
10837 /* 13 */ { SW_SHOWNOACTIVATE, FALSE, WS_VISIBLE, WmShowNoActivate_1,
10838            SW_SHOWNORMAL, 0, {-32000,-32000}, {-1,-1}, FALSE },
10839 /* 14 */ { SW_SHOWNOACTIVATE, TRUE, WS_VISIBLE, WmEmptySeq,
10840            SW_SHOWNORMAL, 0, {-32000,-32000}, {-1,-1}, FALSE },
10841 /* 15 */ { SW_HIDE, TRUE, 0, WmHide_2,
10842            SW_SHOWNORMAL, 0, {-32000,-32000}, {-1,-1}, FALSE },
10843 /* 16 */ { SW_HIDE, FALSE, 0, WmEmptySeq,
10844            SW_SHOWNORMAL, 0, {-32000,-32000}, {-1,-1}, FALSE },
10845 /* 17 */ { SW_SHOW, FALSE, WS_VISIBLE, WmShow,
10846            SW_SHOWNORMAL, 0, {-32000,-32000}, {-1,-1}, FALSE },
10847 /* 18 */ { SW_SHOW, TRUE, WS_VISIBLE, WmEmptySeq,
10848            SW_SHOWNORMAL, 0, {-32000,-32000}, {-1,-1}, FALSE },
10849 /* 19 */ { SW_MINIMIZE, TRUE, WS_VISIBLE|WS_MINIMIZE, WmMinimize_1,
10850            SW_SHOWMINIMIZED, 0, {-32000,-32000}, {-1,-1}, FALSE },
10851 /* 20 */ { SW_MINIMIZE, TRUE, WS_VISIBLE|WS_MINIMIZE, WmMinMax_3,
10852            SW_SHOWMINIMIZED, 0, {-32000,-32000}, {-1,-1}, FALSE },
10853 /* 21 */ { SW_HIDE, TRUE, WS_MINIMIZE, WmHide_2,
10854            SW_SHOWMINIMIZED, 0, {-32000,-32000}, {-1,-1}, FALSE },
10855 /* 22 */ { SW_SHOWMINNOACTIVE, FALSE, WS_VISIBLE|WS_MINIMIZE, WmShowMinNoActivate,
10856            SW_SHOWMINIMIZED, 0, {-32000,-32000}, {-1,-1}, TRUE },
10857 /* 23 */ { SW_SHOWMINNOACTIVE, TRUE, WS_VISIBLE|WS_MINIMIZE, WmMinMax_4,
10858            SW_SHOWMINIMIZED, 0, {-32000,-32000}, {-1,-1}, FALSE },
10859 /* 24 */ { SW_HIDE, TRUE, WS_MINIMIZE, WmHide_2,
10860            SW_SHOWMINIMIZED, 0, {-32000,-32000}, {-1,-1}, FALSE },
10861 /* 25 */ { SW_HIDE, FALSE, WS_MINIMIZE, WmEmptySeq,
10862            SW_SHOWMINIMIZED, 0, {-32000,-32000}, {-1,-1}, FALSE },
10863 /* 26 */ { SW_SHOWNA, FALSE, WS_VISIBLE|WS_MINIMIZE, WmShowNA_1,
10864            SW_SHOWMINIMIZED, 0, {-32000,-32000}, {-1,-1}, FALSE },
10865 /* 27 */ { SW_SHOWNA, TRUE, WS_VISIBLE|WS_MINIMIZE, WmShowNA_2,
10866            SW_SHOWMINIMIZED, 0, {-32000,-32000}, {-1,-1}, FALSE },
10867 /* 28 */ { SW_HIDE, TRUE, WS_MINIMIZE, WmHide_2,
10868            SW_SHOWMINIMIZED, 0, {-32000,-32000}, {-1,-1}, FALSE },
10869 /* 29 */ { SW_HIDE, FALSE, WS_MINIMIZE, WmEmptySeq,
10870            SW_SHOWMINIMIZED, 0, {-32000,-32000}, {-1,-1}, FALSE },
10871 /* 30 */ { SW_RESTORE, FALSE, WS_VISIBLE, WmRestore_1,
10872            SW_SHOWNORMAL, 0, {-32000,-32000}, {-1,-1}, FALSE },
10873 /* 31 */ { SW_RESTORE, TRUE, WS_VISIBLE, WmEmptySeq,
10874            SW_SHOWNORMAL, 0, {-32000,-32000}, {-1,-1}, FALSE },
10875 /* 32 */ { SW_HIDE, TRUE, 0, WmHide_3,
10876            SW_SHOWNORMAL, 0, {-32000,-32000}, {-1,-1}, FALSE },
10877 /* 33 */ { SW_HIDE, FALSE, 0, WmEmptySeq,
10878            SW_SHOWNORMAL, 0, {-32000,-32000}, {-1,-1}, FALSE },
10879 /* 34 */ { SW_NORMALNA, FALSE, 0, WmEmptySeq, /* what does this mean?! */
10880            SW_SHOWNORMAL, 0, {-32000,-32000}, {-1,-1}, FALSE },
10881 /* 35 */ { SW_NORMALNA, FALSE, 0, WmEmptySeq,
10882            SW_SHOWNORMAL, 0, {-32000,-32000}, {-1,-1}, FALSE },
10883 /* 36 */ { SW_HIDE, FALSE, 0, WmEmptySeq,
10884            SW_SHOWNORMAL, 0, {-32000,-32000}, {-1,-1}, FALSE },
10885 /* 37 */ { SW_RESTORE, FALSE, WS_VISIBLE, WmRestore_2,
10886            SW_SHOWNORMAL, 0, {-32000,-32000}, {-1,-1}, FALSE },
10887 /* 38 */ { SW_RESTORE, TRUE, WS_VISIBLE, WmEmptySeq,
10888            SW_SHOWNORMAL, 0, {-32000,-32000}, {-1,-1}, FALSE },
10889 /* 39 */ { SW_SHOWNOACTIVATE, TRUE, WS_VISIBLE, WmEmptySeq,
10890            SW_SHOWNORMAL, 0, {-32000,-32000}, {-1,-1}, FALSE },
10891 /* 40 */ { SW_MINIMIZE, TRUE, WS_VISIBLE|WS_MINIMIZE, WmMinimize_2,
10892            SW_SHOWMINIMIZED, 0, {-32000,-32000}, {-1,-1}, FALSE },
10893 /* 41 */ { SW_MINIMIZE, TRUE, WS_VISIBLE|WS_MINIMIZE, WmMinMax_3,
10894            SW_SHOWMINIMIZED, 0, {-32000,-32000}, {-1,-1}, FALSE },
10895 /* 42 */ { SW_SHOWMAXIMIZED, TRUE, WS_VISIBLE|WS_MAXIMIZE, WmShowMaximized_2,
10896            SW_SHOWMAXIMIZED, WPF_RESTORETOMAXIMIZED, {-32000,-32000}, {-1,-1}, FALSE },
10897 /* 43 */ { SW_SHOWMAXIMIZED, TRUE, WS_VISIBLE|WS_MAXIMIZE, WmMinMax_2,
10898            SW_SHOWMAXIMIZED, WPF_RESTORETOMAXIMIZED, {-32000,-32000}, {-1,-1}, FALSE },
10899 /* 44 */ { SW_MINIMIZE, TRUE, WS_VISIBLE|WS_MINIMIZE, WmMinimize_1,
10900            SW_SHOWMINIMIZED, WPF_RESTORETOMAXIMIZED, {-32000,-32000}, {-1,-1}, FALSE },
10901 /* 45 */ { SW_MINIMIZE, TRUE, WS_VISIBLE|WS_MINIMIZE, WmMinMax_3,
10902            SW_SHOWMINIMIZED, WPF_RESTORETOMAXIMIZED, {-32000,-32000}, {-1,-1}, FALSE },
10903 /* 46 */ { SW_RESTORE, TRUE, WS_VISIBLE|WS_MAXIMIZE, WmRestore_3,
10904            SW_SHOWMAXIMIZED, WPF_RESTORETOMAXIMIZED, {-32000,-32000}, {-1,-1}, FALSE },
10905 /* 47 */ { SW_RESTORE, TRUE, WS_VISIBLE, WmRestore_4,
10906            SW_SHOWNORMAL, WPF_RESTORETOMAXIMIZED, {-32000,-32000}, {-1,-1}, FALSE },
10907 /* 48 */ { SW_SHOWMAXIMIZED, TRUE, WS_VISIBLE|WS_MAXIMIZE, WmShowMaximized_3,
10908            SW_SHOWMAXIMIZED, WPF_RESTORETOMAXIMIZED, {-32000,-32000}, {-1,-1}, FALSE },
10909 /* 49 */ { SW_SHOW, TRUE, WS_VISIBLE|WS_MAXIMIZE, WmEmptySeq,
10910            SW_SHOWMAXIMIZED, WPF_RESTORETOMAXIMIZED, {-32000,-32000}, {-1,-1}, FALSE },
10911 /* 50 */ { SW_SHOWNORMAL, TRUE, WS_VISIBLE, WmRestore_5,
10912            SW_SHOWNORMAL, WPF_RESTORETOMAXIMIZED, {-32000,-32000}, {-1,-1}, FALSE },
10913 /* 51 */ { SW_SHOWNORMAL, TRUE, WS_VISIBLE, WmRestore_5,
10914            SW_SHOWNORMAL, WPF_RESTORETOMAXIMIZED, {-32000,-32000}, {-1,-1}, FALSE },
10915 /* 52 */ { SW_HIDE, TRUE, 0, WmHide_1,
10916            SW_SHOWNORMAL, WPF_RESTORETOMAXIMIZED, {-32000,-32000}, {-1,-1}, FALSE },
10917 /* 53 */ { SW_HIDE, FALSE, 0, WmEmptySeq,
10918            SW_SHOWNORMAL, WPF_RESTORETOMAXIMIZED, {-32000,-32000}, {-1,-1}, FALSE },
10919 /* 54 */ { SW_MINIMIZE, FALSE, WS_VISIBLE|WS_MINIMIZE, WmMinimize_3,
10920            SW_SHOWMINIMIZED, 0, {-32000,-32000}, {-1,-1}, FALSE },
10921 /* 55 */ { SW_HIDE, TRUE, WS_MINIMIZE, WmHide_2,
10922            SW_SHOWMINIMIZED, 0, {-32000,-32000}, {-1,-1}, FALSE },
10923 /* 56 */ { SW_SHOWNOACTIVATE, FALSE, WS_VISIBLE, WmShowNoActivate_2,
10924            SW_SHOWNORMAL, 0, {-32000,-32000}, {-1,-1}, FALSE },
10925 /* 57 */ { SW_SHOW, TRUE, WS_VISIBLE, WmEmptySeq,
10926            SW_SHOWNORMAL, 0, {-32000,-32000}, {-1,-1}, FALSE }
10927     };
10928     HWND hwnd;
10929     DWORD style;
10930     LPARAM ret;
10931     INT i;
10932     WINDOWPLACEMENT wp;
10933     RECT win_rc, work_rc = {0, 0, 0, 0};
10934
10935 #define WS_BASE (WS_CAPTION|WS_SYSMENU|WS_MINIMIZEBOX|WS_MAXIMIZEBOX|WS_POPUP|WS_CLIPSIBLINGS)
10936     hwnd = CreateWindowEx(0, "ShowWindowClass", NULL, WS_BASE,
10937                           120, 120, 90, 90,
10938                           0, 0, 0, NULL);
10939     assert(hwnd);
10940
10941     style = GetWindowLong(hwnd, GWL_STYLE) & ~WS_BASE;
10942     ok(style == 0, "expected style 0, got %08x\n", style);
10943
10944     flush_events();
10945     flush_sequence();
10946
10947     if (pGetMonitorInfoA && pMonitorFromPoint)
10948     {
10949         HMONITOR hmon;
10950         MONITORINFO mi;
10951         POINT pt = {0, 0};
10952
10953         SetLastError(0xdeadbeef);
10954         hmon = pMonitorFromPoint(pt, MONITOR_DEFAULTTOPRIMARY);
10955         ok(hmon != 0, "MonitorFromPoint error %u\n", GetLastError());
10956
10957         mi.cbSize = sizeof(mi);
10958         SetLastError(0xdeadbeef);
10959         ret = pGetMonitorInfoA(hmon, &mi);
10960         ok(ret, "GetMonitorInfo error %u\n", GetLastError());
10961         trace("monitor (%d,%d-%d,%d), work (%d,%d-%d,%d)\n",
10962             mi.rcMonitor.left, mi.rcMonitor.top, mi.rcMonitor.right, mi.rcMonitor.bottom,
10963             mi.rcWork.left, mi.rcWork.top, mi.rcWork.right, mi.rcWork.bottom);
10964         work_rc = mi.rcWork;
10965     }
10966
10967     GetWindowRect(hwnd, &win_rc);
10968     OffsetRect(&win_rc, -work_rc.left, -work_rc.top);
10969
10970     wp.length = sizeof(wp);
10971     SetLastError(0xdeadbeaf);
10972     ret = GetWindowPlacement(hwnd, &wp);
10973     ok(ret, "GetWindowPlacement error %u\n", GetLastError());
10974     ok(wp.flags == 0, "expected 0, got %#x\n", wp.flags);
10975     ok(wp.showCmd == SW_SHOWNORMAL, "expected SW_SHOWNORMAL, got %d\n", wp.showCmd);
10976     ok(wp.ptMinPosition.x == -1 && wp.ptMinPosition.y == -1,
10977        "expected -1,-1 got %d,%d\n", wp.ptMinPosition.x, wp.ptMinPosition.y);
10978     ok(wp.ptMaxPosition.x == -1 && wp.ptMaxPosition.y == -1,
10979        "expected -1,-1 got %d,%d\n", wp.ptMaxPosition.x, wp.ptMaxPosition.y);
10980     if (work_rc.left || work_rc.top) todo_wine /* FIXME: remove once Wine is fixed */
10981     ok(EqualRect(&win_rc, &wp.rcNormalPosition),
10982        "expected %d,%d-%d,%d got %d,%d-%d,%d\n",
10983         win_rc.left, win_rc.top, win_rc.right, win_rc.bottom,
10984         wp.rcNormalPosition.left, wp.rcNormalPosition.top,
10985         wp.rcNormalPosition.right, wp.rcNormalPosition.bottom);
10986     else
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
10993     for (i = 0; i < sizeof(sw)/sizeof(sw[0]); i++)
10994     {
10995         static const char * const sw_cmd_name[13] =
10996         {
10997             "SW_HIDE", "SW_SHOWNORMAL", "SW_SHOWMINIMIZED", "SW_SHOWMAXIMIZED",
10998             "SW_SHOWNOACTIVATE", "SW_SHOW", "SW_MINIMIZE", "SW_SHOWMINNOACTIVE",
10999             "SW_SHOWNA", "SW_RESTORE", "SW_SHOWDEFAULT", "SW_FORCEMINIMIZE",
11000             "SW_NORMALNA" /* 0xCC */
11001         };
11002         char comment[64];
11003         INT idx; /* index into the above array of names */
11004
11005         idx = (sw[i].cmd == SW_NORMALNA) ? 12 : sw[i].cmd;
11006
11007         style = GetWindowLong(hwnd, GWL_STYLE);
11008         trace("%d: sending %s, current window style %08x\n", i+1, sw_cmd_name[idx], style);
11009         ret = ShowWindow(hwnd, sw[i].cmd);
11010         ok(!ret == !sw[i].ret, "%d: cmd %s: expected ret %lu, got %lu\n", i+1, sw_cmd_name[idx], sw[i].ret, ret);
11011         style = GetWindowLong(hwnd, GWL_STYLE) & ~WS_BASE;
11012         ok(style == sw[i].style, "%d: expected style %08x, got %08x\n", i+1, sw[i].style, style);
11013
11014         sprintf(comment, "%d: ShowWindow(%s)", i+1, sw_cmd_name[idx]);
11015         ok_sequence(sw[i].msg, comment, sw[i].todo_msg);
11016
11017         wp.length = sizeof(wp);
11018         SetLastError(0xdeadbeaf);
11019         ret = GetWindowPlacement(hwnd, &wp);
11020         ok(ret, "GetWindowPlacement error %u\n", GetLastError());
11021         ok(wp.flags == sw[i].wp_flags, "expected %#x, got %#x\n", sw[i].wp_flags, wp.flags);
11022         ok(wp.showCmd == sw[i].wp_cmd, "expected %d, got %d\n", sw[i].wp_cmd, wp.showCmd);
11023
11024         /* NT moves the minimized window to -32000,-32000, win9x to 3000,3000 */
11025         if ((wp.ptMinPosition.x + work_rc.left == -32000 && wp.ptMinPosition.y + work_rc.top == -32000) ||
11026             (wp.ptMinPosition.x + work_rc.left == 3000 && wp.ptMinPosition.y + work_rc.top == 3000))
11027         {
11028             ok((wp.ptMinPosition.x + work_rc.left == sw[i].wp_min.x && wp.ptMinPosition.y + work_rc.top == sw[i].wp_min.y) ||
11029                (wp.ptMinPosition.x + work_rc.left == 3000 && wp.ptMinPosition.y + work_rc.top == 3000),
11030                "expected %d,%d got %d,%d\n", sw[i].wp_min.x, sw[i].wp_min.y, wp.ptMinPosition.x, wp.ptMinPosition.y);
11031         }
11032         else
11033         {
11034             if (wp.ptMinPosition.x != sw[i].wp_min.x || wp.ptMinPosition.y != sw[i].wp_min.y)
11035             todo_wine
11036             ok(wp.ptMinPosition.x == sw[i].wp_min.x && wp.ptMinPosition.y == sw[i].wp_min.y,
11037                "expected %d,%d got %d,%d\n", sw[i].wp_min.x, sw[i].wp_min.y, wp.ptMinPosition.x, wp.ptMinPosition.y);
11038             else
11039             ok(wp.ptMinPosition.x == sw[i].wp_min.x && wp.ptMinPosition.y == sw[i].wp_min.y,
11040                "expected %d,%d got %d,%d\n", sw[i].wp_min.x, sw[i].wp_min.y, wp.ptMinPosition.x, wp.ptMinPosition.y);
11041         }
11042
11043         if (wp.ptMaxPosition.x != sw[i].wp_max.x || wp.ptMaxPosition.y != sw[i].wp_max.y)
11044         todo_wine
11045         ok(wp.ptMaxPosition.x == sw[i].wp_max.x && wp.ptMaxPosition.y == sw[i].wp_max.y,
11046            "expected %d,%d got %d,%d\n", sw[i].wp_max.x, sw[i].wp_max.y, wp.ptMaxPosition.x, wp.ptMaxPosition.y);
11047         else
11048         ok(wp.ptMaxPosition.x == sw[i].wp_max.x && wp.ptMaxPosition.y == sw[i].wp_max.y,
11049            "expected %d,%d got %d,%d\n", sw[i].wp_max.x, sw[i].wp_max.y, wp.ptMaxPosition.x, wp.ptMaxPosition.y);
11050
11051 if (0) /* FIXME: Wine behaves completely different here */
11052         ok(EqualRect(&win_rc, &wp.rcNormalPosition),
11053            "expected %d,%d-%d,%d got %d,%d-%d,%d\n",
11054             win_rc.left, win_rc.top, win_rc.right, win_rc.bottom,
11055             wp.rcNormalPosition.left, wp.rcNormalPosition.top,
11056             wp.rcNormalPosition.right, wp.rcNormalPosition.bottom);
11057
11058         flush_events();
11059         flush_sequence();
11060     }
11061
11062     DestroyWindow(hwnd);
11063 }
11064
11065 static INT_PTR WINAPI test_dlg_proc(HWND hwnd, UINT message, WPARAM wParam, LPARAM lParam)
11066 {
11067     struct recvd_message msg;
11068
11069     if (ignore_message( message )) return 0;
11070
11071     msg.hwnd = hwnd;
11072     msg.message = message;
11073     msg.flags = sent|wparam|lparam;
11074     msg.wParam = wParam;
11075     msg.lParam = lParam;
11076     msg.descr = "dialog";
11077     add_message(&msg);
11078
11079     /* calling DefDlgProc leads to a recursion under XP */
11080
11081     switch (message)
11082     {
11083     case WM_INITDIALOG:
11084     case WM_GETDLGCODE:
11085         return 0;
11086     }
11087     return 1;
11088 }
11089
11090 static const struct message WmDefDlgSetFocus_1[] = {
11091     { WM_GETDLGCODE, sent|wparam|lparam, 0, 0 },
11092     { WM_GETTEXTLENGTH, sent|wparam|lparam|optional, 0, 0 }, /* XP */
11093     { WM_GETTEXT, sent|wparam|optional, 6 }, /* XP */
11094     { WM_GETTEXT, sent|wparam|optional, 12 }, /* XP */
11095     { EM_SETSEL, sent|wparam, 0 }, /* XP sets lparam to text length, Win9x to -2 */
11096     { HCBT_SETFOCUS, hook },
11097     { WM_IME_SETCONTEXT, sent|wparam|optional, 1 },
11098     { WM_IME_NOTIFY, sent|wparam|defwinproc|optional, 2 },
11099     { EVENT_OBJECT_FOCUS, winevent_hook|wparam|lparam, OBJID_CLIENT, 0 },
11100     { WM_SETFOCUS, sent|wparam, 0 },
11101     { WM_IME_NOTIFY, sent|wparam|defwinproc|optional, 10 },
11102     { WM_CTLCOLOREDIT, sent },
11103     { WM_IME_NOTIFY, sent|wparam|defwinproc|optional, 11 },
11104     { EVENT_OBJECT_CREATE, winevent_hook|wparam|lparam, OBJID_CARET, 0 },
11105     { EVENT_OBJECT_LOCATIONCHANGE, winevent_hook|wparam|lparam, OBJID_CARET, 0 },
11106     { EVENT_OBJECT_SHOW, winevent_hook|wparam|lparam, OBJID_CARET, 0 },
11107     { WM_COMMAND, sent|wparam, MAKEWPARAM(1, EN_SETFOCUS) },
11108     { 0 }
11109 };
11110 static const struct message WmDefDlgSetFocus_2[] = {
11111     { WM_GETDLGCODE, sent|wparam|lparam, 0, 0 },
11112     { WM_GETTEXTLENGTH, sent|wparam|lparam|optional, 0, 0 }, /* XP */
11113     { WM_GETTEXT, sent|wparam|optional, 6 }, /* XP */
11114     { WM_GETTEXT, sent|wparam|optional, 12 }, /* XP */
11115     { EM_SETSEL, sent|wparam, 0 }, /* XP sets lparam to text length, Win9x to -2 */
11116     { EVENT_OBJECT_HIDE, winevent_hook|wparam|lparam, OBJID_CARET, 0 },
11117     { WM_CTLCOLOREDIT, sent|optional }, /* XP */
11118     { EVENT_OBJECT_SHOW, winevent_hook|wparam|lparam, OBJID_CARET, 0 },
11119     { 0 }
11120 };
11121 /* Creation of a dialog */
11122 static const struct message WmCreateDialogParamSeq_1[] = {
11123     { HCBT_CREATEWND, hook },
11124     { WM_NCCREATE, sent },
11125     { WM_NCCALCSIZE, sent|wparam, 0 },
11126     { WM_CREATE, sent },
11127     { EVENT_OBJECT_CREATE, winevent_hook|wparam|lparam, 0, 0 },
11128     { WM_SIZE, sent|wparam, SIZE_RESTORED },
11129     { WM_MOVE, sent },
11130     { WM_SETFONT, sent },
11131     { WM_INITDIALOG, sent },
11132     { WM_CHANGEUISTATE, sent|optional },
11133     { 0 }
11134 };
11135 /* Creation of a dialog */
11136 static const struct message WmCreateDialogParamSeq_2[] = {
11137     { HCBT_CREATEWND, hook },
11138     { WM_NCCREATE, sent },
11139     { WM_NCCALCSIZE, sent|wparam, 0 },
11140     { WM_CREATE, sent },
11141     { EVENT_OBJECT_CREATE, winevent_hook|wparam|lparam, 0, 0 },
11142     { WM_SIZE, sent|wparam, SIZE_RESTORED },
11143     { WM_MOVE, sent },
11144     { WM_CHANGEUISTATE, sent|optional },
11145     { 0 }
11146 };
11147
11148 static void test_dialog_messages(void)
11149 {
11150     WNDCLASS cls;
11151     HWND hdlg, hedit1, hedit2, hfocus;
11152     LRESULT ret;
11153
11154 #define set_selection(hctl, start, end) \
11155     ret = SendMessage(hctl, EM_SETSEL, start, end); \
11156     ok(ret == 1, "EM_SETSEL returned %ld\n", ret);
11157
11158 #define check_selection(hctl, start, end) \
11159     ret = SendMessage(hctl, EM_GETSEL, 0, 0); \
11160     ok(ret == MAKELRESULT(start, end), "wrong selection (%d - %d)\n", LOWORD(ret), HIWORD(ret));
11161
11162     subclass_edit();
11163
11164     hdlg = CreateWindowEx(WS_EX_DLGMODALFRAME, "TestDialogClass", NULL,
11165                           WS_VISIBLE|WS_CAPTION|WS_SYSMENU|WS_DLGFRAME,
11166                           0, 0, 100, 100, 0, 0, 0, NULL);
11167     ok(hdlg != 0, "Failed to create custom dialog window\n");
11168
11169     hedit1 = CreateWindowEx(0, "my_edit_class", NULL,
11170                            WS_CHILD|WS_BORDER|WS_VISIBLE|WS_TABSTOP,
11171                            0, 0, 80, 20, hdlg, (HMENU)1, 0, NULL);
11172     ok(hedit1 != 0, "Failed to create edit control\n");
11173     hedit2 = CreateWindowEx(0, "my_edit_class", NULL,
11174                            WS_CHILD|WS_BORDER|WS_VISIBLE|WS_TABSTOP,
11175                            0, 40, 80, 20, hdlg, (HMENU)2, 0, NULL);
11176     ok(hedit2 != 0, "Failed to create edit control\n");
11177
11178     SendMessage(hedit1, WM_SETTEXT, 0, (LPARAM)"hello");
11179     SendMessage(hedit2, WM_SETTEXT, 0, (LPARAM)"bye");
11180
11181     hfocus = GetFocus();
11182     ok(hfocus == hdlg, "wrong focus %p\n", hfocus);
11183
11184     SetFocus(hedit2);
11185     hfocus = GetFocus();
11186     ok(hfocus == hedit2, "wrong focus %p\n", hfocus);
11187
11188     check_selection(hedit1, 0, 0);
11189     check_selection(hedit2, 0, 0);
11190
11191     set_selection(hedit2, 0, -1);
11192     check_selection(hedit2, 0, 3);
11193
11194     SetFocus(0);
11195     hfocus = GetFocus();
11196     ok(hfocus == 0, "wrong focus %p\n", hfocus);
11197
11198     flush_sequence();
11199     ret = DefDlgProc(hdlg, WM_SETFOCUS, 0, 0);
11200     ok(ret == 0, "WM_SETFOCUS returned %ld\n", ret);
11201     ok_sequence(WmDefDlgSetFocus_1, "DefDlgProc(WM_SETFOCUS) 1", FALSE);
11202
11203     hfocus = GetFocus();
11204     ok(hfocus == hedit1, "wrong focus %p\n", hfocus);
11205
11206     check_selection(hedit1, 0, 5);
11207     check_selection(hedit2, 0, 3);
11208
11209     flush_sequence();
11210     ret = DefDlgProc(hdlg, WM_SETFOCUS, 0, 0);
11211     ok(ret == 0, "WM_SETFOCUS returned %ld\n", ret);
11212     ok_sequence(WmDefDlgSetFocus_2, "DefDlgProc(WM_SETFOCUS) 2", FALSE);
11213
11214     hfocus = GetFocus();
11215     ok(hfocus == hedit1, "wrong focus %p\n", hfocus);
11216
11217     check_selection(hedit1, 0, 5);
11218     check_selection(hedit2, 0, 3);
11219
11220     EndDialog(hdlg, 0);
11221     DestroyWindow(hedit1);
11222     DestroyWindow(hedit2);
11223     DestroyWindow(hdlg);
11224     flush_sequence();
11225
11226 #undef set_selection
11227 #undef check_selection
11228
11229     ok(GetClassInfo(0, "#32770", &cls), "GetClassInfo failed\n");
11230     cls.lpszClassName = "MyDialogClass";
11231     cls.hInstance = GetModuleHandle(0);
11232     /* need a cast since a dlgproc is used as a wndproc */
11233     cls.lpfnWndProc = test_dlg_proc;
11234     if (!RegisterClass(&cls)) assert(0);
11235
11236     hdlg = CreateDialogParam(0, "CLASS_TEST_DIALOG_2", 0, test_dlg_proc, 0);
11237     ok(IsWindow(hdlg), "CreateDialogParam failed\n");
11238     ok_sequence(WmCreateDialogParamSeq_1, "CreateDialogParam_1", FALSE);
11239     EndDialog(hdlg, 0);
11240     DestroyWindow(hdlg);
11241     flush_sequence();
11242
11243     hdlg = CreateDialogParam(0, "CLASS_TEST_DIALOG_2", 0, NULL, 0);
11244     ok(IsWindow(hdlg), "CreateDialogParam failed\n");
11245     ok_sequence(WmCreateDialogParamSeq_2, "CreateDialogParam_2", FALSE);
11246     EndDialog(hdlg, 0);
11247     DestroyWindow(hdlg);
11248     flush_sequence();
11249
11250     UnregisterClass(cls.lpszClassName, cls.hInstance);
11251 }
11252
11253 static void test_EndDialog(void)
11254 {
11255     HWND hparent, hother, hactive, hdlg;
11256     WNDCLASS cls;
11257
11258     hparent = CreateWindowExA(0, "TestParentClass", "Test parent",
11259                               WS_OVERLAPPEDWINDOW | WS_VISIBLE | WS_DISABLED,
11260                               100, 100, 200, 200, 0, 0, 0, NULL);
11261     ok (hparent != 0, "Failed to create parent window\n");
11262
11263     hother = CreateWindowExA(0, "TestParentClass", "Test parent 2",
11264                               WS_OVERLAPPEDWINDOW | WS_VISIBLE,
11265                               100, 100, 200, 200, 0, 0, 0, NULL);
11266     ok (hother != 0, "Failed to create parent window\n");
11267
11268     ok(GetClassInfo(0, "#32770", &cls), "GetClassInfo failed\n");
11269     cls.lpszClassName = "MyDialogClass";
11270     cls.hInstance = GetModuleHandle(0);
11271     cls.lpfnWndProc = test_dlg_proc;
11272     if (!RegisterClass(&cls)) assert(0);
11273
11274     flush_sequence();
11275     SetForegroundWindow(hother);
11276     hactive = GetForegroundWindow();
11277     ok(hother == hactive, "Wrong window has focus (%p != %p)\n", hother, hactive);
11278
11279     /* create a dialog where the parent is disabled, this parent should still
11280        receive the focus when the dialog exits (even though "normally" a
11281        disabled window should not receive the focus) */
11282     hdlg = CreateDialogParam(0, "CLASS_TEST_DIALOG_2", hparent, test_dlg_proc, 0);
11283     ok(IsWindow(hdlg), "CreateDialogParam failed\n");
11284     SetForegroundWindow(hdlg);
11285     hactive = GetForegroundWindow();
11286     ok(hdlg == hactive, "Wrong window has focus (%p != %p)\n", hdlg, hactive);
11287     EndDialog(hdlg, 0);
11288     hactive = GetForegroundWindow();
11289     ok(hparent == hactive, "Wrong window has focus (parent != active) (active: %p, parent: %p, dlg: %p, other: %p)\n", hactive, hparent, hdlg, hother);
11290     DestroyWindow(hdlg);
11291     flush_sequence();
11292
11293     DestroyWindow( hother );
11294     DestroyWindow( hparent );
11295     UnregisterClass(cls.lpszClassName, cls.hInstance);
11296 }
11297
11298 static void test_nullCallback(void)
11299 {
11300     HWND hwnd;
11301
11302     hwnd = CreateWindowExA(0, "TestWindowClass", "Test overlapped", WS_OVERLAPPEDWINDOW,
11303                            100, 100, 200, 200, 0, 0, 0, NULL);
11304     ok (hwnd != 0, "Failed to create overlapped window\n");
11305
11306     SendMessageCallbackA(hwnd,WM_NULL,0,0,NULL,0);
11307     flush_events();
11308     DestroyWindow(hwnd);
11309 }
11310
11311 /* SetActiveWindow( 0 ) hwnd visible */
11312 static const struct message SetActiveWindowSeq0[] =
11313 {
11314     { HCBT_ACTIVATE, hook|optional },
11315     { WM_NCACTIVATE, sent|wparam, 0 },
11316     { WM_GETTEXT, sent|defwinproc|optional },
11317     { WM_ACTIVATE, sent|wparam, 0 },
11318     { WM_ACTIVATEAPP, sent|wparam|optional, 0 },
11319     { WM_ACTIVATEAPP, sent|wparam|optional, 0 },
11320     { WM_QUERYNEWPALETTE, sent|wparam|lparam|optional, 0, 0 },
11321     { WM_KILLFOCUS, sent|optional },
11322     { WM_WINDOWPOSCHANGING, sent|wparam|optional, SWP_NOSIZE|SWP_NOMOVE|SWP_NOACTIVATE },
11323     { WM_WINDOWPOSCHANGING, sent|wparam|optional, SWP_NOSIZE|SWP_NOMOVE },
11324     { WM_NCACTIVATE, sent|wparam|optional, 1 },
11325     { WM_GETTEXT, sent|defwinproc|optional },
11326     { WM_ACTIVATE, sent|wparam|optional, 1 },
11327     { HCBT_SETFOCUS, hook|optional },
11328     { WM_KILLFOCUS, sent|defwinproc|optional },
11329     { WM_IME_SETCONTEXT, sent|defwinproc|optional },
11330     { WM_IME_SETCONTEXT, sent|defwinproc|optional },
11331     { WM_IME_SETCONTEXT, sent|optional },
11332     { WM_IME_SETCONTEXT, sent|optional },
11333     { WM_IME_NOTIFY, sent|wparam|defwinproc|optional, 1 },
11334     { WM_IME_NOTIFY, sent|wparam|defwinproc|optional, 2 },
11335     { WM_SETFOCUS, sent|defwinproc|optional },
11336     { WM_GETTEXT, sent|optional },
11337     { 0 }
11338 };
11339 /* SetActiveWindow( hwnd ) hwnd visible */
11340 static const struct message SetActiveWindowSeq1[] =
11341 {
11342     { WM_QUERYNEWPALETTE, sent|wparam|lparam|optional, 0, 0 },
11343     { 0 }
11344 };
11345 /* SetActiveWindow( popup ) hwnd visible, popup visible */
11346 static const struct message SetActiveWindowSeq2[] =
11347 {
11348     { HCBT_ACTIVATE, hook },
11349     { WM_NCACTIVATE, sent|wparam, 0 },
11350     { WM_GETTEXT, sent|defwinproc|optional },
11351     { WM_ACTIVATE, sent|wparam, 0 },
11352     { WM_QUERYNEWPALETTE, sent|wparam|lparam|optional, 0, 0 },
11353     { WM_WINDOWPOSCHANGING, sent|wparam|optional, SWP_NOSIZE|SWP_NOMOVE },
11354     { WM_WINDOWPOSCHANGING, sent|wparam|optional, SWP_NOSIZE|SWP_NOMOVE|SWP_NOACTIVATE },
11355     { WM_NCPAINT, sent|optional },
11356     { WM_GETTEXT, sent|defwinproc|optional },
11357     { WM_ERASEBKGND, sent|optional },
11358     { WM_WINDOWPOSCHANGED, sent|wparam|optional, SWP_NOSIZE|SWP_NOMOVE|SWP_NOCLIENTSIZE|SWP_NOCLIENTMOVE },
11359     { WM_NCACTIVATE, sent|wparam, 1 },
11360     { WM_GETTEXT, sent|defwinproc|optional },
11361     { WM_ACTIVATE, sent|wparam, 1 },
11362     { HCBT_SETFOCUS, hook },
11363     { WM_KILLFOCUS, sent|defwinproc },
11364     { WM_IME_SETCONTEXT, sent|wparam|defwinproc|optional, 0 },
11365     { WM_IME_SETCONTEXT, sent|wparam|defwinproc|optional, 1 },
11366     { WM_IME_NOTIFY, sent|wparam|defwinproc|optional, 1 },
11367     { WM_IME_NOTIFY, sent|wparam|defwinproc|optional, 2 },
11368     { WM_SETFOCUS, sent|defwinproc },
11369     { WM_GETTEXT, sent|optional },
11370     { 0 }
11371 };
11372
11373 /* SetActiveWindow( hwnd ) hwnd not visible */
11374 static const struct message SetActiveWindowSeq3[] =
11375 {
11376     { HCBT_ACTIVATE, hook },
11377     { WM_QUERYNEWPALETTE, sent|wparam|lparam|optional, 0, 0 },
11378     { WM_WINDOWPOSCHANGING, sent|wparam, SWP_NOSIZE|SWP_NOMOVE|SWP_NOACTIVATE },
11379     { WM_WINDOWPOSCHANGING, sent|wparam, SWP_NOSIZE|SWP_NOMOVE },
11380     { WM_WINDOWPOSCHANGED, sent|wparam|optional, SWP_NOSIZE|SWP_NOMOVE|SWP_NOREDRAW|SWP_NOACTIVATE|SWP_NOCLIENTSIZE|SWP_NOCLIENTMOVE },
11381     { WM_WINDOWPOSCHANGED, sent|wparam|optional, SWP_NOSIZE|SWP_NOMOVE|SWP_NOREDRAW|SWP_NOCLIENTSIZE|SWP_NOCLIENTMOVE },
11382     { WM_ACTIVATEAPP, sent|wparam, 1 },
11383     { WM_ACTIVATEAPP, sent|wparam, 1 },
11384     { WM_NCACTIVATE, sent|wparam, 1 },
11385     { WM_ACTIVATE, sent|wparam, 1 },
11386     { HCBT_SETFOCUS, hook },
11387     { WM_IME_SETCONTEXT, sent|wparam|defwinproc|optional, 1 },
11388     { WM_IME_NOTIFY, sent|wparam|defwinproc|optional, 2 },
11389     { WM_SETFOCUS, sent|defwinproc },
11390     { 0 }
11391 };
11392 /* SetActiveWindow( popup ) hwnd not visible, popup not visible */
11393 static const struct message SetActiveWindowSeq4[] =
11394 {
11395     { HCBT_ACTIVATE, hook },
11396     { WM_NCACTIVATE, sent|wparam, 0 },
11397     { WM_GETTEXT, sent|defwinproc|optional },
11398     { WM_ACTIVATE, sent|wparam, 0 },
11399     { WM_QUERYNEWPALETTE, sent|wparam|lparam|optional, 0, 0 },
11400     { WM_WINDOWPOSCHANGING, sent|wparam, SWP_NOSIZE|SWP_NOMOVE },
11401     { WM_WINDOWPOSCHANGING, sent|wparam, SWP_NOSIZE|SWP_NOMOVE|SWP_NOACTIVATE },
11402     { WM_WINDOWPOSCHANGED, sent|wparam|optional, SWP_NOSIZE|SWP_NOMOVE|SWP_NOREDRAW|SWP_NOCLIENTSIZE|SWP_NOCLIENTMOVE },
11403     { WM_WINDOWPOSCHANGED, sent|wparam|optional, SWP_NOSIZE|SWP_NOMOVE|SWP_NOACTIVATE|SWP_NOREDRAW|SWP_NOCLIENTSIZE|SWP_NOCLIENTMOVE },
11404     { WM_NCACTIVATE, sent|wparam, 1 },
11405     { WM_GETTEXT, sent|defwinproc|optional },
11406     { WM_ACTIVATE, sent|wparam, 1 },
11407     { HCBT_SETFOCUS, hook },
11408     { WM_KILLFOCUS, sent|defwinproc },
11409     { WM_IME_SETCONTEXT, sent|wparam|defwinproc|optional, 0 },
11410     { WM_IME_SETCONTEXT, sent|wparam|defwinproc|optional, 1 },
11411     { WM_IME_NOTIFY, sent|wparam|defwinproc|optional, 1 },
11412     { WM_IME_NOTIFY, sent|wparam|defwinproc|optional, 2 },
11413     { WM_SETFOCUS, sent|defwinproc },
11414     { 0 }
11415 };
11416
11417
11418 static void test_SetActiveWindow(void)
11419 {
11420     HWND hwnd, popup, ret;
11421
11422     hwnd = CreateWindowExA(0, "TestWindowClass", "Test SetActiveWindow",
11423                            WS_OVERLAPPEDWINDOW | WS_VISIBLE,
11424                            100, 100, 200, 200, 0, 0, 0, NULL);
11425
11426     popup = CreateWindowExA(0, "TestWindowClass", "Test SetActiveWindow",
11427                            WS_OVERLAPPEDWINDOW | WS_VISIBLE | WS_POPUP,
11428                            100, 100, 200, 200, hwnd, 0, 0, NULL);
11429
11430     ok(hwnd != 0, "Failed to create overlapped window\n");
11431     ok(popup != 0, "Failed to create popup window\n");
11432     SetForegroundWindow( popup );
11433     flush_sequence();
11434
11435     trace("SetActiveWindow(0)\n");
11436     ret = SetActiveWindow(0);
11437     ok( ret == popup, "Failed to SetActiveWindow(0)\n");
11438     ok_sequence(SetActiveWindowSeq0, "SetActiveWindow(0)", FALSE);
11439     flush_sequence();
11440
11441     trace("SetActiveWindow(hwnd), hwnd visible\n");
11442     ret = SetActiveWindow(hwnd);
11443     if (ret == hwnd) ok_sequence(SetActiveWindowSeq1, "SetActiveWindow(hwnd), hwnd visible", TRUE);
11444     flush_sequence();
11445
11446     trace("SetActiveWindow(popup), hwnd visible, popup visible\n");
11447     ret = SetActiveWindow(popup);
11448     ok( ret == hwnd, "Failed to SetActiveWindow(popup), popup visible\n");
11449     ok_sequence(SetActiveWindowSeq2, "SetActiveWindow(popup), hwnd visible, popup visible", FALSE);
11450     flush_sequence();
11451
11452     ShowWindow(hwnd, SW_HIDE);
11453     ShowWindow(popup, SW_HIDE);
11454     flush_sequence();
11455
11456     trace("SetActiveWindow(hwnd), hwnd not visible\n");
11457     ret = SetActiveWindow(hwnd);
11458     ok( ret == NULL, "SetActiveWindow(hwnd), hwnd not visible, previous is %p\n", ret );
11459     ok_sequence(SetActiveWindowSeq3, "SetActiveWindow(hwnd), hwnd not visible", TRUE);
11460     flush_sequence();
11461
11462     trace("SetActiveWindow(popup), hwnd not visible, popup not visible\n");
11463     ret = SetActiveWindow(popup);
11464     ok( ret == hwnd, "Failed to SetActiveWindow(popup)\n");
11465     ok_sequence(SetActiveWindowSeq4, "SetActiveWindow(popup), hwnd not visible, popup not visible", TRUE);
11466     flush_sequence();
11467
11468     trace("done\n");
11469
11470     DestroyWindow(hwnd);
11471 }
11472
11473 static const struct message SetForegroundWindowSeq[] =
11474 {
11475     { WM_NCACTIVATE, sent|wparam, 0 },
11476     { WM_GETTEXT, sent|defwinproc|optional },
11477     { WM_ACTIVATE, sent|wparam, 0 },
11478     { WM_ACTIVATEAPP, sent|wparam, 0 },
11479     { WM_KILLFOCUS, sent },
11480     { WM_IME_SETCONTEXT, sent|wparam|optional, 0 },
11481     { WM_IME_NOTIFY, sent|wparam|optional|defwinproc, 1 },
11482     { 0 }
11483 };
11484
11485 static void test_SetForegroundWindow(void)
11486 {
11487     HWND hwnd;
11488
11489     hwnd = CreateWindowExA(0, "TestWindowClass", "Test SetForegroundWindow",
11490                            WS_OVERLAPPEDWINDOW | WS_VISIBLE,
11491                            100, 100, 200, 200, 0, 0, 0, NULL);
11492     ok (hwnd != 0, "Failed to create overlapped window\n");
11493     SetForegroundWindow( hwnd );
11494     flush_sequence();
11495
11496     trace("SetForegroundWindow( 0 )\n");
11497     SetForegroundWindow( 0 );
11498     ok_sequence(WmEmptySeq, "SetForegroundWindow( 0 ) away from foreground top level window", FALSE);
11499     trace("SetForegroundWindow( GetDesktopWindow() )\n");
11500     SetForegroundWindow( GetDesktopWindow() );
11501     ok_sequence(SetForegroundWindowSeq, "SetForegroundWindow( desktop ) away from "
11502                                         "foreground top level window", FALSE);
11503     trace("done\n");
11504
11505     DestroyWindow(hwnd);
11506 }
11507
11508 static void test_dbcs_wm_char(void)
11509 {
11510     BYTE dbch[2];
11511     WCHAR wch, bad_wch;
11512     HWND hwnd, hwnd2;
11513     MSG msg;
11514     DWORD time;
11515     POINT pt;
11516     DWORD_PTR res;
11517     CPINFOEXA cpinfo;
11518     UINT i, j, k;
11519     struct message wmCharSeq[2];
11520     BOOL ret;
11521
11522     if (!pGetCPInfoExA)
11523     {
11524         win_skip("GetCPInfoExA is not available\n");
11525         return;
11526     }
11527
11528     pGetCPInfoExA( CP_ACP, 0, &cpinfo );
11529     if (cpinfo.MaxCharSize != 2)
11530     {
11531         skip( "Skipping DBCS WM_CHAR test in SBCS codepage '%s'\n", cpinfo.CodePageName );
11532         return;
11533     }
11534
11535     dbch[0] = dbch[1] = 0;
11536     wch = 0;
11537     bad_wch = cpinfo.UnicodeDefaultChar;
11538     for (i = 0; !wch && i < MAX_LEADBYTES && cpinfo.LeadByte[i]; i += 2)
11539         for (j = cpinfo.LeadByte[i]; !wch && j <= cpinfo.LeadByte[i+1]; j++)
11540             for (k = 128; k <= 255; k++)
11541             {
11542                 char str[2];
11543                 WCHAR wstr[2];
11544                 str[0] = j;
11545                 str[1] = k;
11546                 if (MultiByteToWideChar( CP_ACP, 0, str, 2, wstr, 2 ) == 1 &&
11547                     WideCharToMultiByte( CP_ACP, 0, wstr, 1, str, 2, NULL, NULL ) == 2 &&
11548                     (BYTE)str[0] == j && (BYTE)str[1] == k &&
11549                     HIBYTE(wstr[0]) && HIBYTE(wstr[0]) != 0xff)
11550                 {
11551                     dbch[0] = j;
11552                     dbch[1] = k;
11553                     wch = wstr[0];
11554                     break;
11555                 }
11556             }
11557
11558     if (!wch)
11559     {
11560         skip( "Skipping DBCS WM_CHAR test, no appropriate char found\n" );
11561         return;
11562     }
11563     trace( "using dbcs char %02x,%02x wchar %04x bad wchar %04x codepage '%s'\n",
11564            dbch[0], dbch[1], wch, bad_wch, cpinfo.CodePageName );
11565
11566     hwnd = CreateWindowExW(0, testWindowClassW, NULL,
11567                            WS_OVERLAPPEDWINDOW, 100, 100, 200, 200, 0, 0, 0, NULL);
11568     hwnd2 = CreateWindowExW(0, testWindowClassW, NULL,
11569                            WS_OVERLAPPEDWINDOW, 100, 100, 200, 200, 0, 0, 0, NULL);
11570     ok (hwnd != 0, "Failed to create overlapped window\n");
11571     ok (hwnd2 != 0, "Failed to create overlapped window\n");
11572     flush_sequence();
11573
11574     memset( wmCharSeq, 0, sizeof(wmCharSeq) );
11575     wmCharSeq[0].message = WM_CHAR;
11576     wmCharSeq[0].flags = sent|wparam;
11577     wmCharSeq[0].wParam = wch;
11578
11579     /* posted message */
11580     PostMessageA( hwnd, WM_CHAR, dbch[0], 0 );
11581     ret = PeekMessageW( &msg, hwnd, 0, 0, PM_REMOVE );
11582     ok( !ret, "got message %x\n", msg.message );
11583     PostMessageA( hwnd, WM_CHAR, dbch[1], 0 );
11584     ret = PeekMessageW( &msg, hwnd, 0, 0, PM_REMOVE );
11585     ok( ret, "no message\n" );
11586     ok( msg.message == WM_CHAR, "unexpected message %x\n", msg.message );
11587     ok( msg.wParam == wch, "bad wparam %lx/%x\n", msg.wParam, wch );
11588     ret = PeekMessageW( &msg, hwnd, 0, 0, PM_REMOVE );
11589     ok( !ret, "got message %x\n", msg.message );
11590
11591     /* posted thread message */
11592     PostThreadMessageA( GetCurrentThreadId(), WM_CHAR, dbch[0], 0 );
11593     ret = PeekMessageW( &msg, hwnd, 0, 0, PM_REMOVE );
11594     ok( !ret, "got message %x\n", msg.message );
11595     PostMessageA( hwnd, WM_CHAR, dbch[1], 0 );
11596     ret = PeekMessageW( &msg, hwnd, 0, 0, PM_REMOVE );
11597     ok( ret, "no message\n" );
11598     ok( msg.message == WM_CHAR, "unexpected message %x\n", msg.message );
11599     ok( msg.wParam == wch, "bad wparam %lx/%x\n", msg.wParam, wch );
11600     ret = PeekMessageW( &msg, hwnd, 0, 0, PM_REMOVE );
11601     ok( !ret, "got message %x\n", msg.message );
11602
11603     /* sent message */
11604     flush_sequence();
11605     SendMessageA( hwnd, WM_CHAR, dbch[0], 0 );
11606     ok_sequence( WmEmptySeq, "no messages", FALSE );
11607     SendMessageA( hwnd, WM_CHAR, dbch[1], 0 );
11608     ok_sequence( wmCharSeq, "Unicode WM_CHAR", FALSE );
11609     ret = PeekMessageW( &msg, hwnd, 0, 0, PM_REMOVE );
11610     ok( !ret, "got message %x\n", msg.message );
11611
11612     /* sent message with timeout */
11613     flush_sequence();
11614     SendMessageTimeoutA( hwnd, WM_CHAR, dbch[0], 0, SMTO_NORMAL, 0, &res );
11615     ok_sequence( WmEmptySeq, "no messages", FALSE );
11616     SendMessageTimeoutA( hwnd, WM_CHAR, dbch[1], 0, SMTO_NORMAL, 0, &res );
11617     ok_sequence( wmCharSeq, "Unicode WM_CHAR", FALSE );
11618     ret = PeekMessageW( &msg, hwnd, 0, 0, PM_REMOVE );
11619     ok( !ret, "got message %x\n", msg.message );
11620
11621     /* sent message with timeout and callback */
11622     flush_sequence();
11623     SendMessageTimeoutA( hwnd, WM_CHAR, dbch[0], 0, SMTO_NORMAL, 0, &res );
11624     ok_sequence( WmEmptySeq, "no messages", FALSE );
11625     SendMessageCallbackA( hwnd, WM_CHAR, dbch[1], 0, NULL, 0 );
11626     ok_sequence( wmCharSeq, "Unicode WM_CHAR", FALSE );
11627     ret = PeekMessageW( &msg, hwnd, 0, 0, PM_REMOVE );
11628     ok( !ret, "got message %x\n", msg.message );
11629
11630     /* sent message with callback */
11631     flush_sequence();
11632     SendNotifyMessageA( hwnd, WM_CHAR, dbch[0], 0 );
11633     ok_sequence( WmEmptySeq, "no messages", FALSE );
11634     SendMessageCallbackA( hwnd, WM_CHAR, dbch[1], 0, NULL, 0 );
11635     ok_sequence( wmCharSeq, "Unicode WM_CHAR", FALSE );
11636     ret = PeekMessageW( &msg, hwnd, 0, 0, PM_REMOVE );
11637     ok( !ret, "got message %x\n", msg.message );
11638
11639     /* direct window proc call */
11640     flush_sequence();
11641     CallWindowProcA( (WNDPROC)GetWindowLongPtrA( hwnd, GWLP_WNDPROC ), hwnd, WM_CHAR, dbch[0], 0 );
11642     ok_sequence( WmEmptySeq, "no messages", FALSE );
11643     CallWindowProcA( (WNDPROC)GetWindowLongPtrA( hwnd, GWLP_WNDPROC ), hwnd, WM_CHAR, dbch[1], 0 );
11644     ok_sequence( wmCharSeq, "Unicode WM_CHAR", FALSE );
11645
11646     /* dispatch message */
11647     msg.hwnd = hwnd;
11648     msg.message = WM_CHAR;
11649     msg.wParam = dbch[0];
11650     msg.lParam = 0;
11651     DispatchMessageA( &msg );
11652     ok_sequence( WmEmptySeq, "no messages", FALSE );
11653     msg.wParam = dbch[1];
11654     DispatchMessageA( &msg );
11655     ok_sequence( wmCharSeq, "Unicode WM_CHAR", FALSE );
11656
11657     /* window handle is irrelevant */
11658     flush_sequence();
11659     SendMessageA( hwnd2, WM_CHAR, dbch[0], 0 );
11660     ok_sequence( WmEmptySeq, "no messages", FALSE );
11661     SendMessageA( hwnd, WM_CHAR, dbch[1], 0 );
11662     ok_sequence( wmCharSeq, "Unicode WM_CHAR", FALSE );
11663     ret = PeekMessageW( &msg, hwnd, 0, 0, PM_REMOVE );
11664     ok( !ret, "got message %x\n", msg.message );
11665
11666     /* interleaved post and send */
11667     flush_sequence();
11668     PostMessageA( hwnd2, WM_CHAR, dbch[0], 0 );
11669     SendMessageA( hwnd2, WM_CHAR, dbch[0], 0 );
11670     ok_sequence( WmEmptySeq, "no messages", FALSE );
11671     ret = PeekMessageW( &msg, hwnd, 0, 0, PM_REMOVE );
11672     ok( !ret, "got message %x\n", msg.message );
11673     PostMessageA( hwnd, WM_CHAR, dbch[1], 0 );
11674     ok_sequence( WmEmptySeq, "no messages", FALSE );
11675     ret = PeekMessageW( &msg, hwnd, 0, 0, PM_REMOVE );
11676     ok( ret, "no message\n" );
11677     ok( msg.message == WM_CHAR, "unexpected message %x\n", msg.message );
11678     ok( msg.wParam == wch, "bad wparam %lx/%x\n", msg.wParam, wch );
11679     ret = PeekMessageW( &msg, hwnd, 0, 0, PM_REMOVE );
11680     ok( !ret, "got message %x\n", msg.message );
11681     SendMessageA( hwnd, WM_CHAR, dbch[1], 0 );
11682     ok_sequence( wmCharSeq, "Unicode WM_CHAR", FALSE );
11683     ret = PeekMessageW( &msg, hwnd, 0, 0, PM_REMOVE );
11684     ok( !ret, "got message %x\n", msg.message );
11685
11686     /* interleaved sent message and winproc */
11687     flush_sequence();
11688     SendMessageA( hwnd, WM_CHAR, dbch[0], 0 );
11689     CallWindowProcA( (WNDPROC)GetWindowLongPtrA( hwnd, GWLP_WNDPROC ), hwnd, WM_CHAR, dbch[0], 0 );
11690     ok_sequence( WmEmptySeq, "no messages", FALSE );
11691     SendMessageA( hwnd, WM_CHAR, dbch[1], 0 );
11692     ok_sequence( wmCharSeq, "Unicode WM_CHAR", FALSE );
11693     CallWindowProcA( (WNDPROC)GetWindowLongPtrA( hwnd, GWLP_WNDPROC ), hwnd, WM_CHAR, dbch[1], 0 );
11694     ok_sequence( wmCharSeq, "Unicode WM_CHAR", FALSE );
11695
11696     /* interleaved winproc and dispatch */
11697     msg.hwnd = hwnd;
11698     msg.message = WM_CHAR;
11699     msg.wParam = dbch[0];
11700     msg.lParam = 0;
11701     CallWindowProcA( (WNDPROC)GetWindowLongPtrA( hwnd, GWLP_WNDPROC ), hwnd, WM_CHAR, dbch[0], 0 );
11702     DispatchMessageA( &msg );
11703     ok_sequence( WmEmptySeq, "no messages", FALSE );
11704     msg.wParam = dbch[1];
11705     DispatchMessageA( &msg );
11706     ok_sequence( wmCharSeq, "Unicode WM_CHAR", FALSE );
11707     CallWindowProcA( (WNDPROC)GetWindowLongPtrA( hwnd, GWLP_WNDPROC ), hwnd, WM_CHAR, dbch[1], 0 );
11708     ok_sequence( wmCharSeq, "Unicode WM_CHAR", FALSE );
11709
11710     /* interleaved sends */
11711     flush_sequence();
11712     SendMessageA( hwnd, WM_CHAR, dbch[0], 0 );
11713     SendMessageCallbackA( hwnd, WM_CHAR, dbch[0], 0, NULL, 0 );
11714     ok_sequence( WmEmptySeq, "no messages", FALSE );
11715     SendMessageTimeoutA( hwnd, WM_CHAR, dbch[1], 0, SMTO_NORMAL, 0, &res );
11716     ok_sequence( wmCharSeq, "Unicode WM_CHAR", FALSE );
11717     SendMessageA( hwnd, WM_CHAR, dbch[1], 0 );
11718     ok_sequence( wmCharSeq, "Unicode WM_CHAR", FALSE );
11719
11720     /* dbcs WM_CHAR */
11721     flush_sequence();
11722     SendMessageA( hwnd2, WM_CHAR, (dbch[1] << 8) | dbch[0], 0 );
11723     ok_sequence( wmCharSeq, "Unicode WM_CHAR", FALSE );
11724     ret = PeekMessageW( &msg, hwnd, 0, 0, PM_REMOVE );
11725     ok( !ret, "got message %x\n", msg.message );
11726
11727     /* other char messages are not magic */
11728     PostMessageA( hwnd, WM_SYSCHAR, dbch[0], 0 );
11729     ret = PeekMessageW( &msg, hwnd, 0, 0, PM_REMOVE );
11730     ok( ret, "no message\n" );
11731     ok( msg.message == WM_SYSCHAR, "unexpected message %x\n", msg.message );
11732     ok( msg.wParam == bad_wch, "bad wparam %lx/%x\n", msg.wParam, bad_wch );
11733     ret = PeekMessageW( &msg, hwnd, 0, 0, PM_REMOVE );
11734     ok( !ret, "got message %x\n", msg.message );
11735     PostMessageA( hwnd, WM_DEADCHAR, dbch[0], 0 );
11736     ret = PeekMessageW( &msg, hwnd, 0, 0, PM_REMOVE );
11737     ok( ret, "no message\n" );
11738     ok( msg.message == WM_DEADCHAR, "unexpected message %x\n", msg.message );
11739     ok( msg.wParam == bad_wch, "bad wparam %lx/%x\n", msg.wParam, bad_wch );
11740     ret = PeekMessageW( &msg, hwnd, 0, 0, PM_REMOVE );
11741     ok( !ret, "got message %x\n", msg.message );
11742
11743     /* test retrieving messages */
11744
11745     PostMessageW( hwnd, WM_CHAR, wch, 0 );
11746     ret = PeekMessageA( &msg, hwnd, 0, 0, PM_REMOVE );
11747     ok( ret, "no message\n" );
11748     ok( msg.hwnd == hwnd, "unexpected hwnd %p\n", msg.hwnd );
11749     ok( msg.message == WM_CHAR, "unexpected message %x\n", msg.message );
11750     ok( msg.wParam == dbch[0], "bad wparam %lx/%x\n", msg.wParam, dbch[0] );
11751     ret = PeekMessageA( &msg, hwnd, 0, 0, PM_REMOVE );
11752     ok( ret, "no message\n" );
11753     ok( msg.hwnd == hwnd, "unexpected hwnd %p\n", msg.hwnd );
11754     ok( msg.message == WM_CHAR, "unexpected message %x\n", msg.message );
11755     ok( msg.wParam == dbch[1], "bad wparam %lx/%x\n", msg.wParam, dbch[0] );
11756     ret = PeekMessageA( &msg, hwnd, 0, 0, PM_REMOVE );
11757     ok( !ret, "got message %x\n", msg.message );
11758
11759     /* message filters */
11760     PostMessageW( hwnd, WM_CHAR, wch, 0 );
11761     ret = PeekMessageA( &msg, hwnd, 0, 0, PM_REMOVE );
11762     ok( ret, "no message\n" );
11763     ok( msg.hwnd == hwnd, "unexpected hwnd %p\n", msg.hwnd );
11764     ok( msg.message == WM_CHAR, "unexpected message %x\n", msg.message );
11765     ok( msg.wParam == dbch[0], "bad wparam %lx/%x\n", msg.wParam, dbch[0] );
11766     /* message id is filtered, hwnd is not */
11767     ret = PeekMessageA( &msg, hwnd, WM_MOUSEFIRST, WM_MOUSELAST, PM_REMOVE );
11768     ok( !ret, "no message\n" );
11769     ret = PeekMessageA( &msg, hwnd2, 0, 0, PM_REMOVE );
11770     ok( ret, "no message\n" );
11771     ok( msg.hwnd == hwnd, "unexpected hwnd %p\n", msg.hwnd );
11772     ok( msg.message == WM_CHAR, "unexpected message %x\n", msg.message );
11773     ok( msg.wParam == dbch[1], "bad wparam %lx/%x\n", msg.wParam, dbch[0] );
11774     ret = PeekMessageA( &msg, hwnd, 0, 0, PM_REMOVE );
11775     ok( !ret, "got message %x\n", msg.message );
11776
11777     /* mixing GetMessage and PostMessage */
11778     PostMessageW( hwnd, WM_CHAR, wch, 0xbeef );
11779     ok( GetMessageA( &msg, hwnd, 0, 0 ), "no message\n" );
11780     ok( msg.hwnd == hwnd, "unexpected hwnd %p\n", msg.hwnd );
11781     ok( msg.message == WM_CHAR, "unexpected message %x\n", msg.message );
11782     ok( msg.wParam == dbch[0], "bad wparam %lx/%x\n", msg.wParam, dbch[0] );
11783     ok( msg.lParam == 0xbeef, "bad lparam %lx\n", msg.lParam );
11784     time = msg.time;
11785     pt = msg.pt;
11786     ok( time - GetTickCount() <= 100, "bad time %x\n", msg.time );
11787     ret = PeekMessageA( &msg, 0, 0, 0, PM_REMOVE );
11788     ok( ret, "no message\n" );
11789     ok( msg.hwnd == hwnd, "unexpected hwnd %p\n", msg.hwnd );
11790     ok( msg.message == WM_CHAR, "unexpected message %x\n", msg.message );
11791     ok( msg.wParam == dbch[1], "bad wparam %lx/%x\n", msg.wParam, dbch[0] );
11792     ok( msg.lParam == 0xbeef, "bad lparam %lx\n", msg.lParam );
11793     ok( msg.time == time, "bad time %x/%x\n", msg.time, time );
11794     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 );
11795     ret = PeekMessageA( &msg, hwnd, 0, 0, PM_REMOVE );
11796     ok( !ret, "got message %x\n", msg.message );
11797
11798     /* without PM_REMOVE */
11799     PostMessageW( hwnd, WM_CHAR, wch, 0 );
11800     ret = PeekMessageA( &msg, 0, 0, 0, PM_NOREMOVE );
11801     ok( ret, "no message\n" );
11802     ok( msg.hwnd == hwnd, "unexpected hwnd %p\n", msg.hwnd );
11803     ok( msg.message == WM_CHAR, "unexpected message %x\n", msg.message );
11804     ok( msg.wParam == dbch[0], "bad wparam %lx/%x\n", msg.wParam, dbch[0] );
11805     ret = PeekMessageA( &msg, 0, 0, 0, PM_REMOVE );
11806     ok( ret, "no message\n" );
11807     ok( msg.hwnd == hwnd, "unexpected hwnd %p\n", msg.hwnd );
11808     ok( msg.message == WM_CHAR, "unexpected message %x\n", msg.message );
11809     ok( msg.wParam == dbch[0], "bad wparam %lx/%x\n", msg.wParam, dbch[0] );
11810     ret = PeekMessageA( &msg, 0, 0, 0, PM_NOREMOVE );
11811     ok( ret, "no message\n" );
11812     ok( msg.hwnd == hwnd, "unexpected hwnd %p\n", msg.hwnd );
11813     ok( msg.message == WM_CHAR, "unexpected message %x\n", msg.message );
11814     ok( msg.wParam == dbch[1], "bad wparam %lx/%x\n", msg.wParam, dbch[0] );
11815     ret = PeekMessageA( &msg, 0, 0, 0, PM_REMOVE );
11816     ok( ret, "no message\n" );
11817     ok( msg.hwnd == hwnd, "unexpected hwnd %p\n", msg.hwnd );
11818     ok( msg.message == WM_CHAR, "unexpected message %x\n", msg.message );
11819     ok( msg.wParam == dbch[1], "bad wparam %lx/%x\n", msg.wParam, dbch[0] );
11820     ret = PeekMessageA( &msg, hwnd, 0, 0, PM_REMOVE );
11821     ok( !ret, "got message %x\n", msg.message );
11822
11823     DestroyWindow(hwnd);
11824     DestroyWindow(hwnd2);
11825 }
11826
11827 #define ID_LISTBOX 0x000f
11828
11829 static const struct message wm_lb_setcursel_0[] =
11830 {
11831     { LB_SETCURSEL, sent|wparam|lparam, 0, 0 },
11832     { WM_CTLCOLORLISTBOX, sent|parent },
11833     { WM_DRAWITEM, sent|wparam|lparam|parent, ID_LISTBOX, 0x000120f2 },
11834     { EVENT_OBJECT_FOCUS, winevent_hook|wparam|lparam, OBJID_CLIENT, 1 },
11835     { EVENT_OBJECT_SELECTION, winevent_hook|wparam|lparam, OBJID_CLIENT, 1 },
11836     { 0 }
11837 };
11838 static const struct message wm_lb_setcursel_1[] =
11839 {
11840     { LB_SETCURSEL, sent|wparam|lparam, 1, 0 },
11841     { WM_CTLCOLORLISTBOX, sent|parent },
11842     { WM_DRAWITEM, sent|wparam|lparam|parent, ID_LISTBOX, 0x000020f2 },
11843     { WM_CTLCOLORLISTBOX, sent|parent },
11844     { WM_DRAWITEM, sent|wparam|lparam|parent, ID_LISTBOX, 0x000121f2 },
11845     { EVENT_OBJECT_FOCUS, winevent_hook|wparam|lparam, OBJID_CLIENT, 2 },
11846     { EVENT_OBJECT_SELECTION, winevent_hook|wparam|lparam, OBJID_CLIENT, 2 },
11847     { 0 }
11848 };
11849 static const struct message wm_lb_setcursel_2[] =
11850 {
11851     { LB_SETCURSEL, sent|wparam|lparam, 2, 0 },
11852     { WM_CTLCOLORLISTBOX, sent|parent },
11853     { WM_DRAWITEM, sent|wparam|lparam|parent, ID_LISTBOX, 0x000021f2 },
11854     { WM_CTLCOLORLISTBOX, sent|parent },
11855     { WM_DRAWITEM, sent|wparam|lparam|parent, ID_LISTBOX, 0x000122f2 },
11856     { EVENT_OBJECT_FOCUS, winevent_hook|wparam|lparam, OBJID_CLIENT, 3 },
11857     { EVENT_OBJECT_SELECTION, winevent_hook|wparam|lparam, OBJID_CLIENT, 3 },
11858     { 0 }
11859 };
11860 static const struct message wm_lb_click_0[] =
11861 {
11862     { WM_LBUTTONDOWN, sent|wparam|lparam, 0, MAKELPARAM(1,1) },
11863     { HCBT_SETFOCUS, hook },
11864     { WM_KILLFOCUS, sent|parent },
11865     { WM_IME_SETCONTEXT, sent|wparam|optional|parent, 0 },
11866     { WM_IME_SETCONTEXT, sent|wparam|defwinproc|optional, 1 },
11867     { EVENT_OBJECT_FOCUS, winevent_hook|wparam|lparam, OBJID_CLIENT, 0 },
11868     { WM_SETFOCUS, sent|defwinproc },
11869
11870     { WM_DRAWITEM, sent|wparam|lparam|parent, ID_LISTBOX, 0x001142f2 },
11871     { WM_COMMAND, sent|wparam|parent, MAKEWPARAM(ID_LISTBOX, LBN_SETFOCUS) },
11872     { EVENT_OBJECT_FOCUS, winevent_hook|wparam|lparam, OBJID_CLIENT, 3 },
11873     { WM_LBTRACKPOINT, sent|wparam|lparam|parent, 0, MAKELPARAM(1,1) },
11874     { EVENT_SYSTEM_CAPTURESTART, winevent_hook|wparam|lparam, 0, 0 },
11875
11876     { WM_DRAWITEM, sent|wparam|lparam|parent, ID_LISTBOX, 0x000142f2 },
11877     { WM_CTLCOLORLISTBOX, sent|parent },
11878     { WM_DRAWITEM, sent|wparam|lparam|parent, ID_LISTBOX, 0x000022f2 },
11879     { WM_CTLCOLORLISTBOX, sent|parent },
11880     { WM_DRAWITEM, sent|wparam|lparam|parent, ID_LISTBOX, 0x000120f2 },
11881     { WM_DRAWITEM, sent|wparam|lparam|parent, ID_LISTBOX, 0x001140f2 },
11882
11883     { EVENT_OBJECT_FOCUS, winevent_hook|wparam|lparam, OBJID_CLIENT, 1 },
11884     { EVENT_OBJECT_SELECTION, winevent_hook|wparam|lparam, OBJID_CLIENT, 1 },
11885
11886     { WM_LBUTTONUP, sent|wparam|lparam, 0, 0 },
11887     { EVENT_SYSTEM_CAPTUREEND, winevent_hook|wparam|lparam, 0, 0 },
11888     { WM_CAPTURECHANGED, sent|wparam|lparam|defwinproc, 0, 0 },
11889     { WM_COMMAND, sent|wparam|parent, MAKEWPARAM(ID_LISTBOX, LBN_SELCHANGE) },
11890     { 0 }
11891 };
11892 static const struct message wm_lb_deletestring[] =
11893 {
11894     { LB_DELETESTRING, sent|wparam|lparam, 0, 0 },
11895     { WM_DELETEITEM, sent|wparam|parent|optional, ID_LISTBOX, 0 },
11896     { WM_DRAWITEM, sent|wparam|parent|optional, ID_LISTBOX },
11897     { WM_DRAWITEM, sent|wparam|parent|optional, ID_LISTBOX },
11898     { 0 }
11899 };
11900 static const struct message wm_lb_deletestring_reset[] =
11901 {
11902     { LB_DELETESTRING, sent|wparam|lparam, 0, 0 },
11903     { LB_RESETCONTENT, sent|wparam|lparam|defwinproc|optional, 0, 0 },
11904     { WM_DELETEITEM, sent|wparam|parent|optional, ID_LISTBOX, 0 },
11905     { WM_DRAWITEM, sent|wparam|parent|optional, ID_LISTBOX },
11906     { WM_DRAWITEM, sent|wparam|parent|optional, ID_LISTBOX },
11907     { 0 }
11908 };
11909
11910 #define check_lb_state(a1, a2, a3, a4, a5) check_lb_state_dbg(a1, a2, a3, a4, a5, __LINE__)
11911
11912 static LRESULT (WINAPI *listbox_orig_proc)(HWND, UINT, WPARAM, LPARAM);
11913
11914 static LRESULT WINAPI listbox_hook_proc(HWND hwnd, UINT message, WPARAM wp, LPARAM lp)
11915 {
11916     static LONG defwndproc_counter = 0;
11917     LRESULT ret;
11918     struct recvd_message msg;
11919
11920     /* do not log painting messages */
11921     if (message != WM_PAINT &&
11922         message != WM_NCPAINT &&
11923         message != WM_SYNCPAINT &&
11924         message != WM_ERASEBKGND &&
11925         message != WM_NCHITTEST &&
11926         message != WM_GETTEXT &&
11927         !ignore_message( message ))
11928     {
11929         msg.hwnd = hwnd;
11930         msg.message = message;
11931         msg.flags = sent|wparam|lparam;
11932         if (defwndproc_counter) msg.flags |= defwinproc;
11933         msg.wParam = wp;
11934         msg.lParam = lp;
11935         msg.descr = "listbox";
11936         add_message(&msg);
11937     }
11938
11939     defwndproc_counter++;
11940     ret = CallWindowProcA(listbox_orig_proc, hwnd, message, wp, lp);
11941     defwndproc_counter--;
11942
11943     return ret;
11944 }
11945
11946 static void check_lb_state_dbg(HWND listbox, int count, int cur_sel,
11947                                int caret_index, int top_index, int line)
11948 {
11949     LRESULT ret;
11950
11951     /* calling an orig proc helps to avoid unnecessary message logging */
11952     ret = CallWindowProcA(listbox_orig_proc, listbox, LB_GETCOUNT, 0, 0);
11953     ok_(__FILE__, line)(ret == count, "expected count %d, got %ld\n", count, ret);
11954     ret = CallWindowProcA(listbox_orig_proc, listbox, LB_GETCURSEL, 0, 0);
11955     ok_(__FILE__, line)(ret == cur_sel, "expected cur sel %d, got %ld\n", cur_sel, ret);
11956     ret = CallWindowProcA(listbox_orig_proc, listbox, LB_GETCARETINDEX, 0, 0);
11957     ok_(__FILE__, line)(ret == caret_index ||
11958                         broken(cur_sel == -1 && caret_index == 0 && ret == -1),  /* nt4 */
11959                         "expected caret index %d, got %ld\n", caret_index, ret);
11960     ret = CallWindowProcA(listbox_orig_proc, listbox, LB_GETTOPINDEX, 0, 0);
11961     ok_(__FILE__, line)(ret == top_index, "expected top index %d, got %ld\n", top_index, ret);
11962 }
11963
11964 static void test_listbox_messages(void)
11965 {
11966     HWND parent, listbox;
11967     LRESULT ret;
11968
11969     parent = CreateWindowExA(0, "TestParentClass", NULL, WS_OVERLAPPEDWINDOW  | WS_VISIBLE,
11970                              100, 100, 200, 200, 0, 0, 0, NULL);
11971     listbox = CreateWindowExA(WS_EX_NOPARENTNOTIFY, "ListBox", NULL,
11972                               WS_CHILD | LBS_NOTIFY | LBS_OWNERDRAWVARIABLE | LBS_HASSTRINGS | WS_VISIBLE,
11973                               10, 10, 80, 80, parent, (HMENU)ID_LISTBOX, 0, NULL);
11974     listbox_orig_proc = (WNDPROC)SetWindowLongPtrA(listbox, GWLP_WNDPROC, (ULONG_PTR)listbox_hook_proc);
11975
11976     check_lb_state(listbox, 0, LB_ERR, 0, 0);
11977
11978     ret = SendMessage(listbox, LB_ADDSTRING, 0, (LPARAM)"item 0");
11979     ok(ret == 0, "expected 0, got %ld\n", ret);
11980     ret = SendMessage(listbox, LB_ADDSTRING, 0, (LPARAM)"item 1");
11981     ok(ret == 1, "expected 1, got %ld\n", ret);
11982     ret = SendMessage(listbox, LB_ADDSTRING, 0, (LPARAM)"item 2");
11983     ok(ret == 2, "expected 2, got %ld\n", ret);
11984
11985     check_lb_state(listbox, 3, LB_ERR, 0, 0);
11986
11987     flush_sequence();
11988
11989     log_all_parent_messages++;
11990
11991     trace("selecting item 0\n");
11992     ret = SendMessage(listbox, LB_SETCURSEL, 0, 0);
11993     ok(ret == 0, "expected 0, got %ld\n", ret);
11994     ok_sequence(wm_lb_setcursel_0, "LB_SETCURSEL 0", FALSE );
11995     check_lb_state(listbox, 3, 0, 0, 0);
11996     flush_sequence();
11997
11998     trace("selecting item 1\n");
11999     ret = SendMessage(listbox, LB_SETCURSEL, 1, 0);
12000     ok(ret == 1, "expected 1, got %ld\n", ret);
12001     ok_sequence(wm_lb_setcursel_1, "LB_SETCURSEL 1", FALSE );
12002     check_lb_state(listbox, 3, 1, 1, 0);
12003
12004     trace("selecting item 2\n");
12005     ret = SendMessage(listbox, LB_SETCURSEL, 2, 0);
12006     ok(ret == 2, "expected 2, got %ld\n", ret);
12007     ok_sequence(wm_lb_setcursel_2, "LB_SETCURSEL 2", FALSE );
12008     check_lb_state(listbox, 3, 2, 2, 0);
12009
12010     trace("clicking on item 0\n");
12011     ret = SendMessage(listbox, WM_LBUTTONDOWN, 0, MAKELPARAM(1, 1));
12012     ok(ret == LB_OKAY, "expected LB_OKAY, got %ld\n", ret);
12013     ret = SendMessage(listbox, WM_LBUTTONUP, 0, 0);
12014     ok(ret == LB_OKAY, "expected LB_OKAY, got %ld\n", ret);
12015     ok_sequence(wm_lb_click_0, "WM_LBUTTONDOWN 0", FALSE );
12016     check_lb_state(listbox, 3, 0, 0, 0);
12017     flush_sequence();
12018
12019     trace("deleting item 0\n");
12020     ret = SendMessage(listbox, LB_DELETESTRING, 0, 0);
12021     ok(ret == 2, "expected 2, got %ld\n", ret);
12022     ok_sequence(wm_lb_deletestring, "LB_DELETESTRING 0", FALSE );
12023     check_lb_state(listbox, 2, -1, 0, 0);
12024     flush_sequence();
12025
12026     trace("deleting item 0\n");
12027     ret = SendMessage(listbox, LB_DELETESTRING, 0, 0);
12028     ok(ret == 1, "expected 1, got %ld\n", ret);
12029     ok_sequence(wm_lb_deletestring, "LB_DELETESTRING 0", FALSE );
12030     check_lb_state(listbox, 1, -1, 0, 0);
12031     flush_sequence();
12032
12033     trace("deleting item 0\n");
12034     ret = SendMessage(listbox, LB_DELETESTRING, 0, 0);
12035     ok(ret == 0, "expected 0, got %ld\n", ret);
12036     ok_sequence(wm_lb_deletestring_reset, "LB_DELETESTRING 0", FALSE );
12037     check_lb_state(listbox, 0, -1, 0, 0);
12038     flush_sequence();
12039
12040     trace("deleting item 0\n");
12041     ret = SendMessage(listbox, LB_DELETESTRING, 0, 0);
12042     ok(ret == LB_ERR, "expected LB_ERR, got %ld\n", ret);
12043     check_lb_state(listbox, 0, -1, 0, 0);
12044     flush_sequence();
12045
12046     log_all_parent_messages--;
12047
12048     DestroyWindow(listbox);
12049     DestroyWindow(parent);
12050 }
12051
12052 /*************************** Menu test ******************************/
12053 static const struct message wm_popup_menu_1[] =
12054 {
12055     { HCBT_KEYSKIPPED, hook|wparam|lparam|optional, VK_MENU, 0x20000001 },
12056     { WM_SYSKEYDOWN, sent|wparam|lparam, VK_MENU, 0x20000001 },
12057     { HCBT_KEYSKIPPED, hook|wparam|lparam|optional, 'E', 0x20000001 },
12058     { WM_SYSKEYDOWN, sent|wparam|lparam, 'E', 0x20000001 },
12059     { WM_SYSCHAR, sent|wparam|lparam, 'e', 0x20000001 },
12060     { HCBT_SYSCOMMAND, hook|wparam|lparam, SC_KEYMENU, 'e' },
12061     { WM_ENTERMENULOOP, sent|wparam|lparam, 0, 0 },
12062     { WM_INITMENU, sent|lparam, 0, 0 },
12063     { WM_MENUSELECT, sent|wparam, MAKEWPARAM(1,MF_HILITE|MF_POPUP) },
12064     { WM_INITMENUPOPUP, sent|lparam, 0, 1 },
12065     { HCBT_CREATEWND, hook|optional }, /* Win9x doesn't create a window */
12066     { WM_MENUSELECT, sent|wparam, MAKEWPARAM(200,MF_HILITE) },
12067     { HCBT_KEYSKIPPED, hook|wparam|lparam|optional, 'E', 0xf0000001 },
12068     { HCBT_KEYSKIPPED, hook|wparam|lparam|optional, VK_MENU, 0xd0000001 },
12069     { HCBT_KEYSKIPPED, hook|wparam|lparam|optional, VK_RETURN, 0x10000001, 0, 0x40000000 },
12070     { HCBT_DESTROYWND, hook|optional }, /* Win9x doesn't create a window */
12071     { WM_UNINITMENUPOPUP, sent|lparam, 0, 0 },
12072     { WM_MENUSELECT, sent|wparam|lparam, MAKEWPARAM(0,0xffff), 0 },
12073     { WM_EXITMENULOOP, sent|wparam|lparam, 0, 0 },
12074     { WM_MENUCOMMAND, sent }, /* |wparam, 200 - Win9x */
12075     { HCBT_KEYSKIPPED, hook|wparam|lparam|optional, VK_RETURN, 0xc0000001 },
12076     { WM_KEYUP, sent|wparam|lparam, VK_RETURN, 0xc0000001 },
12077     { 0 }
12078 };
12079 static const struct message wm_popup_menu_2[] =
12080 {
12081     { HCBT_KEYSKIPPED, hook|wparam|lparam|optional, VK_MENU, 0x20000001 },
12082     { WM_SYSKEYDOWN, sent|wparam|lparam, VK_MENU, 0x20000001 },
12083     { HCBT_KEYSKIPPED, hook|wparam|lparam|optional, 'F', 0x20000001 },
12084     { WM_SYSKEYDOWN, sent|wparam|lparam, 'F', 0x20000001 },
12085     { WM_SYSCHAR, sent|wparam|lparam, 'f', 0x20000001 },
12086     { HCBT_SYSCOMMAND, hook|wparam|lparam, SC_KEYMENU, 'f' },
12087     { WM_ENTERMENULOOP, sent|wparam|lparam, 0, 0 },
12088     { WM_INITMENU, sent|lparam, 0, 0 },
12089     { WM_MENUSELECT, sent|wparam, MAKEWPARAM(0,MF_HILITE|MF_POPUP) },
12090     { WM_INITMENUPOPUP, sent|lparam, 0, 0 },
12091     { WM_MENUSELECT, sent|wparam|optional, MAKEWPARAM(0,MF_HILITE|MF_POPUP) }, /* Win9x */
12092     { WM_INITMENUPOPUP, sent|lparam|optional, 0, 0 }, /* Win9x */
12093     { HCBT_CREATEWND, hook },
12094     { WM_MENUSELECT, sent }, /*|wparam, MAKEWPARAM(0,MF_HILITE|MF_POPUP) - XP
12095                                |wparam, MAKEWPARAM(100,MF_HILITE) - Win9x */
12096     { HCBT_KEYSKIPPED, hook|wparam|lparam|optional, 'F', 0xf0000001 },
12097     { HCBT_KEYSKIPPED, hook|wparam|lparam|optional, VK_MENU, 0xd0000001 },
12098     { HCBT_KEYSKIPPED, hook|wparam|lparam|optional, VK_RIGHT, 0x10000001 },
12099     { WM_INITMENUPOPUP, sent|lparam|optional, 0, 0 }, /* Win9x doesn't send it */
12100     { HCBT_CREATEWND, hook|optional }, /* Win9x doesn't send it */
12101     { WM_MENUSELECT, sent|wparam|optional, MAKEWPARAM(100,MF_HILITE) },
12102     { HCBT_KEYSKIPPED, hook|wparam|lparam|optional, VK_RIGHT, 0xd0000001 },
12103     { HCBT_KEYSKIPPED, hook|wparam|lparam|optional, VK_RETURN, 0x10000001 },
12104     { HCBT_DESTROYWND, hook },
12105     { WM_UNINITMENUPOPUP, sent|lparam, 0, 0 },
12106     { HCBT_DESTROYWND, hook|optional }, /* Win9x doesn't send it */
12107     { WM_UNINITMENUPOPUP, sent|lparam, 0, 0 },
12108     { WM_MENUSELECT, sent|wparam|lparam, MAKEWPARAM(0,0xffff), 0 },
12109     { WM_EXITMENULOOP, sent|wparam|lparam, 0, 0 },
12110     { WM_MENUCOMMAND, sent }, /* |wparam, 100 - Win9x */
12111     { HCBT_KEYSKIPPED, hook|wparam|lparam|optional, VK_RETURN, 0xc0000001 },
12112     { WM_KEYUP, sent|wparam|lparam, VK_RETURN, 0xc0000001 },
12113     { 0 }
12114 };
12115 static const struct message wm_popup_menu_3[] =
12116 {
12117     { HCBT_KEYSKIPPED, hook|wparam|lparam|optional, VK_MENU, 0x20000001 },
12118     { WM_SYSKEYDOWN, sent|wparam|lparam, VK_MENU, 0x20000001 },
12119     { HCBT_KEYSKIPPED, hook|wparam|lparam|optional, 'F', 0x20000001 },
12120     { WM_SYSKEYDOWN, sent|wparam|lparam, 'F', 0x20000001 },
12121     { WM_SYSCHAR, sent|wparam|lparam, 'f', 0x20000001 },
12122     { HCBT_SYSCOMMAND, hook|wparam|lparam, SC_KEYMENU, 'f' },
12123     { WM_ENTERMENULOOP, sent|wparam|lparam, 0, 0 },
12124     { WM_INITMENU, sent|lparam, 0, 0 },
12125     { WM_MENUSELECT, sent|wparam, MAKEWPARAM(0,MF_HILITE|MF_POPUP) },
12126     { WM_INITMENUPOPUP, sent|lparam, 0, 0 },
12127     { WM_MENUSELECT, sent|wparam|optional, MAKEWPARAM(0,MF_HILITE|MF_POPUP) }, /* Win9x */
12128     { WM_INITMENUPOPUP, sent|lparam|optional, 0, 0 }, /* Win9x */
12129     { HCBT_CREATEWND, hook },
12130     { WM_MENUSELECT, sent }, /*|wparam, MAKEWPARAM(0,MF_HILITE|MF_POPUP) - XP
12131                                |wparam, MAKEWPARAM(100,MF_HILITE) - Win9x */
12132     { HCBT_KEYSKIPPED, hook|wparam|lparam|optional, 'F', 0xf0000001 },
12133     { HCBT_KEYSKIPPED, hook|wparam|lparam|optional, VK_MENU, 0xd0000001 },
12134     { HCBT_KEYSKIPPED, hook|wparam|lparam|optional, VK_RIGHT, 0x10000001 },
12135     { WM_INITMENUPOPUP, sent|lparam|optional, 0, 0 }, /* Win9x doesn't send it */
12136     { HCBT_CREATEWND, hook|optional }, /* Win9x doesn't send it */
12137     { WM_MENUSELECT, sent|wparam|optional, MAKEWPARAM(100,MF_HILITE) },
12138     { HCBT_KEYSKIPPED, hook|wparam|lparam|optional, VK_RIGHT, 0xd0000001 },
12139     { HCBT_KEYSKIPPED, hook|wparam|lparam|optional, VK_RETURN, 0x10000001 },
12140     { HCBT_DESTROYWND, hook },
12141     { WM_UNINITMENUPOPUP, sent|lparam, 0, 0 },
12142     { HCBT_DESTROYWND, hook|optional }, /* Win9x doesn't send it */
12143     { WM_UNINITMENUPOPUP, sent|lparam, 0, 0 },
12144     { WM_MENUSELECT, sent|wparam|lparam, MAKEWPARAM(0,0xffff), 0 },
12145     { WM_EXITMENULOOP, sent|wparam|lparam, 0, 0 },
12146     { WM_COMMAND, sent|wparam|lparam, 100, 0 },
12147     { HCBT_KEYSKIPPED, hook|wparam|lparam|optional, VK_RETURN, 0xc0000001 },
12148     { WM_KEYUP, sent|wparam|lparam, VK_RETURN, 0xc0000001 },
12149     { 0 }
12150 };
12151
12152 static const struct message wm_single_menu_item[] =
12153 {
12154     { HCBT_KEYSKIPPED, hook|wparam|lparam|optional, VK_MENU, 0x20000001 },
12155     { WM_SYSKEYDOWN, sent|wparam|lparam, VK_MENU, 0x20000001 },
12156     { HCBT_KEYSKIPPED, hook|wparam|lparam|optional, 'Q', 0x20000001 },
12157     { WM_SYSKEYDOWN, sent|wparam|lparam, 'Q', 0x20000001 },
12158     { WM_SYSCHAR, sent|wparam|lparam, 'q', 0x20000001 },
12159     { HCBT_SYSCOMMAND, hook|wparam|lparam, SC_KEYMENU, 'q' },
12160     { WM_ENTERMENULOOP, sent|wparam|lparam, 0, 0 },
12161     { WM_INITMENU, sent|lparam, 0, 0 },
12162     { WM_MENUSELECT, sent|wparam|optional, MAKEWPARAM(300,MF_HILITE) },
12163     { WM_MENUSELECT, sent|wparam|lparam, MAKEWPARAM(0,0xffff), 0 },
12164     { WM_EXITMENULOOP, sent|wparam|lparam, 0, 0 },
12165     { WM_MENUCOMMAND, sent },
12166     { HCBT_KEYSKIPPED, hook|wparam|lparam|optional, 'Q', 0xe0000001 },
12167     { WM_SYSKEYUP, sent|wparam|lparam, 'Q', 0xe0000001 },
12168     { HCBT_KEYSKIPPED, hook|wparam|lparam|optional, VK_MENU, 0xc0000001 },
12169     { WM_KEYUP, sent|wparam|lparam, VK_MENU, 0xc0000001 },
12170
12171     { HCBT_KEYSKIPPED, hook|wparam|lparam|optional, VK_ESCAPE, 1 },
12172     { WM_KEYDOWN, sent|wparam|lparam, VK_ESCAPE, 1 },
12173     { WM_CHAR,  sent|wparam|lparam, VK_ESCAPE, 0x00000001 },
12174     { HCBT_KEYSKIPPED, hook|wparam|lparam|optional, VK_ESCAPE, 0xc0000001 },
12175     { WM_KEYUP, sent|wparam|lparam, VK_ESCAPE, 0xc0000001 },
12176     { 0 }
12177 };
12178
12179 static LRESULT WINAPI parent_menu_proc(HWND hwnd, UINT message, WPARAM wp, LPARAM lp)
12180 {
12181     if (message == WM_ENTERIDLE ||
12182         message == WM_INITMENU ||
12183         message == WM_INITMENUPOPUP ||
12184         message == WM_MENUSELECT ||
12185         message == WM_PARENTNOTIFY ||
12186         message == WM_ENTERMENULOOP ||
12187         message == WM_EXITMENULOOP ||
12188         message == WM_UNINITMENUPOPUP ||
12189         message == WM_KEYDOWN ||
12190         message == WM_KEYUP ||
12191         message == WM_CHAR ||
12192         message == WM_SYSKEYDOWN ||
12193         message == WM_SYSKEYUP ||
12194         message == WM_SYSCHAR ||
12195         message == WM_COMMAND ||
12196         message == WM_MENUCOMMAND)
12197     {
12198         struct recvd_message msg;
12199
12200         msg.hwnd = hwnd;
12201         msg.message = message;
12202         msg.flags = sent|wparam|lparam;
12203         msg.wParam = wp;
12204         msg.lParam = lp;
12205         msg.descr = "parent_menu_proc";
12206         add_message(&msg);
12207     }
12208
12209     return DefWindowProcA(hwnd, message, wp, lp);
12210 }
12211
12212 static void set_menu_style(HMENU hmenu, DWORD style)
12213 {
12214     MENUINFO mi;
12215     BOOL ret;
12216
12217     mi.cbSize = sizeof(mi);
12218     mi.fMask = MIM_STYLE;
12219     mi.dwStyle = style;
12220     SetLastError(0xdeadbeef);
12221     ret = pSetMenuInfo(hmenu, &mi);
12222     ok(ret, "SetMenuInfo error %u\n", GetLastError());
12223 }
12224
12225 static DWORD get_menu_style(HMENU hmenu)
12226 {
12227     MENUINFO mi;
12228     BOOL ret;
12229
12230     mi.cbSize = sizeof(mi);
12231     mi.fMask = MIM_STYLE;
12232     mi.dwStyle = 0;
12233     SetLastError(0xdeadbeef);
12234     ret = pGetMenuInfo(hmenu, &mi);
12235     ok(ret, "GetMenuInfo error %u\n", GetLastError());
12236
12237     return mi.dwStyle;
12238 }
12239
12240 static void test_menu_messages(void)
12241 {
12242     MSG msg;
12243     WNDCLASSA cls;
12244     HMENU hmenu, hmenu_popup;
12245     HWND hwnd;
12246     DWORD style;
12247
12248     if (!pGetMenuInfo || !pSetMenuInfo)
12249     {
12250         win_skip("GetMenuInfo and/or SetMenuInfo are not available\n");
12251         return;
12252     }
12253     cls.style = 0;
12254     cls.lpfnWndProc = parent_menu_proc;
12255     cls.cbClsExtra = 0;
12256     cls.cbWndExtra = 0;
12257     cls.hInstance = GetModuleHandleA(0);
12258     cls.hIcon = 0;
12259     cls.hCursor = LoadCursorA(0, IDC_ARROW);
12260     cls.hbrBackground = GetStockObject(WHITE_BRUSH);
12261     cls.lpszMenuName = NULL;
12262     cls.lpszClassName = "TestMenuClass";
12263     UnregisterClass(cls.lpszClassName, cls.hInstance);
12264     if (!RegisterClassA(&cls)) assert(0);
12265
12266     SetLastError(0xdeadbeef);
12267     hwnd = CreateWindowExA(0, "TestMenuClass", NULL, WS_OVERLAPPEDWINDOW | WS_VISIBLE,
12268                            100, 100, 200, 200, 0, 0, 0, NULL);
12269     ok(hwnd != 0, "LoadMenuA error %u\n", GetLastError());
12270
12271     SetLastError(0xdeadbeef);
12272     hmenu = LoadMenuA(GetModuleHandle(0), MAKEINTRESOURCE(1));
12273     ok(hmenu != 0, "LoadMenuA error %u\n", GetLastError());
12274
12275     SetMenu(hwnd, hmenu);
12276     SetForegroundWindow( hwnd );
12277
12278     set_menu_style(hmenu, MNS_NOTIFYBYPOS);
12279     style = get_menu_style(hmenu);
12280     ok(style == MNS_NOTIFYBYPOS, "expected MNS_NOTIFYBYPOS, got %u\n", style);
12281
12282     hmenu_popup = GetSubMenu(hmenu, 0);
12283     ok(hmenu_popup != 0, "GetSubMenu returned 0 for submenu 0\n");
12284     style = get_menu_style(hmenu_popup);
12285     ok(style == 0, "expected 0, got %u\n", style);
12286
12287     hmenu_popup = GetSubMenu(hmenu_popup, 0);
12288     ok(hmenu_popup != 0, "GetSubMenu returned 0 for submenu 0\n");
12289     style = get_menu_style(hmenu_popup);
12290     ok(style == 0, "expected 0, got %u\n", style);
12291
12292     /* Alt+E, Enter */
12293     trace("testing a popup menu command\n");
12294     flush_sequence();
12295     keybd_event(VK_MENU, 0, 0, 0);
12296     keybd_event('E', 0, 0, 0);
12297     keybd_event('E', 0, KEYEVENTF_KEYUP, 0);
12298     keybd_event(VK_MENU, 0, KEYEVENTF_KEYUP, 0);
12299     keybd_event(VK_RETURN, 0, 0, 0);
12300     keybd_event(VK_RETURN, 0, KEYEVENTF_KEYUP, 0);
12301     while (PeekMessage(&msg, 0, 0, 0, PM_REMOVE))
12302     {
12303         TranslateMessage(&msg);
12304         DispatchMessage(&msg);
12305     }
12306     if (!sequence_cnt)  /* we didn't get any message */
12307     {
12308         skip( "queuing key events not supported\n" );
12309         goto done;
12310     }
12311     /* win98 queues only a WM_KEYUP and doesn't start menu tracking */
12312     if (sequence[0].message == WM_KEYUP && sequence[0].wParam == VK_MENU)
12313     {
12314         win_skip( "menu tracking through VK_MENU not supported\n" );
12315         goto done;
12316     }
12317     ok_sequence(wm_popup_menu_1, "popup menu command", FALSE);
12318
12319     /* Alt+F, Right, Enter */
12320     trace("testing submenu of a popup menu command\n");
12321     flush_sequence();
12322     keybd_event(VK_MENU, 0, 0, 0);
12323     keybd_event('F', 0, 0, 0);
12324     keybd_event('F', 0, KEYEVENTF_KEYUP, 0);
12325     keybd_event(VK_MENU, 0, KEYEVENTF_KEYUP, 0);
12326     keybd_event(VK_RIGHT, 0, 0, 0);
12327     keybd_event(VK_RIGHT, 0, KEYEVENTF_KEYUP, 0);
12328     keybd_event(VK_RETURN, 0, 0, 0);
12329     keybd_event(VK_RETURN, 0, KEYEVENTF_KEYUP, 0);
12330     while (PeekMessage(&msg, 0, 0, 0, PM_REMOVE))
12331     {
12332         TranslateMessage(&msg);
12333         DispatchMessage(&msg);
12334     }
12335     ok_sequence(wm_popup_menu_2, "submenu of a popup menu command", FALSE);
12336
12337     trace("testing single menu item command\n");
12338     flush_sequence();
12339     keybd_event(VK_MENU, 0, 0, 0);
12340     keybd_event('Q', 0, 0, 0);
12341     keybd_event('Q', 0, KEYEVENTF_KEYUP, 0);
12342     keybd_event(VK_MENU, 0, KEYEVENTF_KEYUP, 0);
12343     keybd_event(VK_ESCAPE, 0, 0, 0);
12344     keybd_event(VK_ESCAPE, 0, KEYEVENTF_KEYUP, 0);
12345     while (PeekMessage(&msg, 0, 0, 0, PM_REMOVE))
12346     {
12347         TranslateMessage(&msg);
12348         DispatchMessage(&msg);
12349     }
12350     ok_sequence(wm_single_menu_item, "single menu item command", FALSE);
12351
12352     set_menu_style(hmenu, 0);
12353     style = get_menu_style(hmenu);
12354     ok(style == 0, "expected 0, got %u\n", style);
12355
12356     hmenu_popup = GetSubMenu(hmenu, 0);
12357     ok(hmenu_popup != 0, "GetSubMenu returned 0 for submenu 0\n");
12358     set_menu_style(hmenu_popup, MNS_NOTIFYBYPOS);
12359     style = get_menu_style(hmenu_popup);
12360     ok(style == MNS_NOTIFYBYPOS, "expected MNS_NOTIFYBYPOS, got %u\n", style);
12361
12362     hmenu_popup = GetSubMenu(hmenu_popup, 0);
12363     ok(hmenu_popup != 0, "GetSubMenu returned 0 for submenu 0\n");
12364     style = get_menu_style(hmenu_popup);
12365     ok(style == 0, "expected 0, got %u\n", style);
12366
12367     /* Alt+F, Right, Enter */
12368     trace("testing submenu of a popup menu command\n");
12369     flush_sequence();
12370     keybd_event(VK_MENU, 0, 0, 0);
12371     keybd_event('F', 0, 0, 0);
12372     keybd_event('F', 0, KEYEVENTF_KEYUP, 0);
12373     keybd_event(VK_MENU, 0, KEYEVENTF_KEYUP, 0);
12374     keybd_event(VK_RIGHT, 0, 0, 0);
12375     keybd_event(VK_RIGHT, 0, KEYEVENTF_KEYUP, 0);
12376     keybd_event(VK_RETURN, 0, 0, 0);
12377     keybd_event(VK_RETURN, 0, KEYEVENTF_KEYUP, 0);
12378     while (PeekMessage(&msg, 0, 0, 0, PM_REMOVE))
12379     {
12380         TranslateMessage(&msg);
12381         DispatchMessage(&msg);
12382     }
12383     ok_sequence(wm_popup_menu_3, "submenu of a popup menu command", FALSE);
12384
12385 done:
12386     DestroyWindow(hwnd);
12387     DestroyMenu(hmenu);
12388 }
12389
12390
12391 static void test_paintingloop(void)
12392 {
12393     HWND hwnd;
12394
12395     paint_loop_done = 0;
12396     hwnd = CreateWindowExA(0x0,"PaintLoopWindowClass",
12397                                "PaintLoopWindowClass",WS_OVERLAPPEDWINDOW,
12398                                 100, 100, 100, 100, 0, 0, 0, NULL );
12399     ok(hwnd != 0, "PaintLoop window error %u\n", GetLastError());
12400     ShowWindow(hwnd,SW_NORMAL);
12401     SetFocus(hwnd);
12402
12403     while (!paint_loop_done)
12404     {
12405         MSG msg;
12406         if (PeekMessageA(&msg, 0, 0, 0, 1))
12407         {
12408             TranslateMessage(&msg);
12409             DispatchMessage(&msg);
12410         }
12411     }
12412     DestroyWindow(hwnd);
12413 }
12414
12415 static void test_defwinproc(void)
12416 {
12417     HWND hwnd;
12418     MSG msg;
12419     int gotwmquit = FALSE;
12420     hwnd = CreateWindowExA(0, "static", "test_defwndproc", WS_POPUP, 0,0,0,0,0,0,0, NULL);
12421     assert(hwnd);
12422     DefWindowProcA( hwnd, WM_ENDSESSION, 1, 0);
12423     while (PeekMessageA( &msg, 0, 0, 0, PM_REMOVE )) {
12424         if( msg.message == WM_QUIT) gotwmquit = TRUE;
12425         DispatchMessageA( &msg );
12426     }
12427     ok( gotwmquit == FALSE, "Unexpected WM_QUIT message!\n");
12428     DestroyWindow( hwnd);
12429 }
12430
12431 #define clear_clipboard(hwnd)  clear_clipboard_(__LINE__, (hwnd))
12432 static void clear_clipboard_(int line, HWND hWnd)
12433 {
12434     BOOL succ;
12435     succ = OpenClipboard(hWnd);
12436     ok_(__FILE__, line)(succ, "OpenClipboard failed, err=%u\n", GetLastError());
12437     succ = EmptyClipboard();
12438     ok_(__FILE__, line)(succ, "EmptyClipboard failed, err=%u\n", GetLastError());
12439     succ = CloseClipboard();
12440     ok_(__FILE__, line)(succ, "CloseClipboard failed, err=%u\n", GetLastError());
12441 }
12442
12443 #define expect_HWND(expected, got) expect_HWND_(__LINE__, (expected), (got))
12444 static void expect_HWND_(int line, HWND expected, HWND got)
12445 {
12446     ok_(__FILE__, line)(got==expected, "Expected %p, got %p\n", expected, got);
12447 }
12448
12449 static WNDPROC pOldViewerProc;
12450
12451 static LRESULT CALLBACK recursive_viewer_proc(HWND hWnd, UINT message, WPARAM wParam, LPARAM lParam)
12452 {
12453     static BOOL recursion_guard;
12454
12455     if (message == WM_DRAWCLIPBOARD && !recursion_guard)
12456     {
12457         recursion_guard = TRUE;
12458         clear_clipboard(hWnd);
12459         recursion_guard = FALSE;
12460     }
12461     return CallWindowProcA(pOldViewerProc, hWnd, message, wParam, lParam);
12462 }
12463
12464 static void test_clipboard_viewers(void)
12465 {
12466     static struct message wm_change_cb_chain[] =
12467     {
12468         { WM_CHANGECBCHAIN, sent|wparam|lparam, 0, 0 },
12469         { 0 }
12470     };
12471     static const struct message wm_clipboard_destroyed[] =
12472     {
12473         { WM_DESTROYCLIPBOARD, sent|wparam|lparam, 0, 0 },
12474         { 0 }
12475     };
12476     static struct message wm_clipboard_changed[] =
12477     {
12478         { WM_DRAWCLIPBOARD, sent|wparam|lparam, 0, 0 },
12479         { 0 }
12480     };
12481     static struct message wm_clipboard_changed_and_owned[] =
12482     {
12483         { WM_DESTROYCLIPBOARD, sent|wparam|lparam, 0, 0 },
12484         { WM_DRAWCLIPBOARD, sent|wparam|lparam, 0, 0 },
12485         { 0 }
12486     };
12487
12488     HINSTANCE hInst = GetModuleHandleA(NULL);
12489     HWND hWnd1, hWnd2, hWnd3;
12490     HWND hOrigViewer;
12491     HWND hRet;
12492
12493     hWnd1 = CreateWindowExA(0, "TestWindowClass", "Clipboard viewer test wnd 1",
12494         WS_CAPTION | WS_SYSMENU | WS_MINIMIZEBOX | WS_MAXIMIZEBOX,
12495         CW_USEDEFAULT, CW_USEDEFAULT, CW_USEDEFAULT, CW_USEDEFAULT,
12496         GetDesktopWindow(), NULL, hInst, NULL);
12497     hWnd2 = CreateWindowExA(0, "SimpleWindowClass", "Clipboard viewer test wnd 2",
12498         WS_CAPTION | WS_SYSMENU | WS_MINIMIZEBOX | WS_MAXIMIZEBOX,
12499         CW_USEDEFAULT, CW_USEDEFAULT, CW_USEDEFAULT, CW_USEDEFAULT,
12500         GetDesktopWindow(), NULL, hInst, NULL);
12501     hWnd3 = CreateWindowExA(0, "SimpleWindowClass", "Clipboard viewer test wnd 3",
12502         WS_CAPTION | WS_SYSMENU | WS_MINIMIZEBOX | WS_MAXIMIZEBOX,
12503         CW_USEDEFAULT, CW_USEDEFAULT, CW_USEDEFAULT, CW_USEDEFAULT,
12504         GetDesktopWindow(), NULL, hInst, NULL);
12505     trace("clipbd viewers: hWnd1=%p, hWnd2=%p, hWnd3=%p\n", hWnd1, hWnd2, hWnd3);
12506     assert(hWnd1 && hWnd2 && hWnd3);
12507
12508     flush_sequence();
12509
12510     /* Test getting the clipboard viewer and setting the viewer to NULL. */
12511     hOrigViewer = GetClipboardViewer();
12512     hRet = SetClipboardViewer(NULL);
12513     ok_sequence(WmEmptySeq, "set viewer to NULL", FALSE);
12514     expect_HWND(hOrigViewer, hRet);
12515     expect_HWND(NULL, GetClipboardViewer());
12516
12517     /* Test registering hWnd1 as a viewer. */
12518     hRet = SetClipboardViewer(hWnd1);
12519     wm_clipboard_changed[0].wParam = (WPARAM) GetClipboardOwner();
12520     ok_sequence(wm_clipboard_changed, "set viewer NULL->1", FALSE);
12521     expect_HWND(NULL, hRet);
12522     expect_HWND(hWnd1, GetClipboardViewer());
12523
12524     /* Test that changing the clipboard actually refreshes the registered viewer. */
12525     clear_clipboard(hWnd1);
12526     wm_clipboard_changed[0].wParam = (WPARAM) GetClipboardOwner();
12527     ok_sequence(wm_clipboard_changed, "clear clipbd (viewer=owner=1)", FALSE);
12528
12529     /* Again, but with different owner. */
12530     clear_clipboard(hWnd2);
12531     wm_clipboard_changed_and_owned[1].wParam = (WPARAM) GetClipboardOwner();
12532     ok_sequence(wm_clipboard_changed_and_owned, "clear clipbd (viewer=1, owner=2)", FALSE);
12533
12534     /* Test re-registering same window. */
12535     hRet = SetClipboardViewer(hWnd1);
12536     wm_clipboard_changed[0].wParam = (WPARAM) GetClipboardOwner();
12537     ok_sequence(wm_clipboard_changed, "set viewer 1->1", FALSE);
12538     expect_HWND(hWnd1, hRet);
12539     expect_HWND(hWnd1, GetClipboardViewer());
12540
12541     /* Test ChangeClipboardChain. */
12542     ChangeClipboardChain(hWnd2, hWnd3);
12543     wm_change_cb_chain[0].wParam = (WPARAM) hWnd2;
12544     wm_change_cb_chain[0].lParam = (LPARAM) hWnd3;
12545     ok_sequence(wm_change_cb_chain, "change chain (viewer=1, remove=2, next=3)", FALSE);
12546     expect_HWND(hWnd1, GetClipboardViewer());
12547
12548     ChangeClipboardChain(hWnd2, NULL);
12549     wm_change_cb_chain[0].wParam = (WPARAM) hWnd2;
12550     wm_change_cb_chain[0].lParam = 0;
12551     ok_sequence(wm_change_cb_chain, "change chain (viewer=1, remove=2, next=NULL)", FALSE);
12552     expect_HWND(hWnd1, GetClipboardViewer());
12553
12554     ChangeClipboardChain(NULL, hWnd2);
12555     ok_sequence(WmEmptySeq, "change chain (viewer=1, remove=NULL, next=2)", TRUE);
12556     expect_HWND(hWnd1, GetClipboardViewer());
12557
12558     /* Actually change clipboard viewer with ChangeClipboardChain. */
12559     ChangeClipboardChain(hWnd1, hWnd2);
12560     ok_sequence(WmEmptySeq, "change chain (viewer=remove=1, next=2)", FALSE);
12561     expect_HWND(hWnd2, GetClipboardViewer());
12562
12563     /* Test that no refresh messages are sent when viewer has unregistered. */
12564     clear_clipboard(hWnd2);
12565     ok_sequence(WmEmptySeq, "clear clipd (viewer=2, owner=1)", FALSE);
12566
12567     /* Register hWnd1 again. */
12568     ChangeClipboardChain(hWnd2, hWnd1);
12569     ok_sequence(WmEmptySeq, "change chain (viewer=remove=2, next=1)", FALSE);
12570     expect_HWND(hWnd1, GetClipboardViewer());
12571
12572     /* Subclass hWnd1 so that when it receives a WM_DRAWCLIPBOARD message, it
12573      * changes the clipboard. When this happens, the system shouldn't send
12574      * another WM_DRAWCLIPBOARD (as this could cause an infinite loop).
12575      */
12576     pOldViewerProc = (WNDPROC) SetWindowLongPtrA(hWnd1, GWLP_WNDPROC, (LONG_PTR) recursive_viewer_proc);
12577     clear_clipboard(hWnd2);
12578     /* The clipboard owner is changed in recursive_viewer_proc: */
12579     wm_clipboard_changed[0].wParam = (WPARAM) hWnd2;
12580     ok_sequence(wm_clipboard_changed, "recursive clear clipbd (viewer=1, owner=2)", TRUE);
12581
12582     /* Test unregistering. */
12583     ChangeClipboardChain(hWnd1, NULL);
12584     ok_sequence(WmEmptySeq, "change chain (viewer=remove=1, next=NULL)", FALSE);
12585     expect_HWND(NULL, GetClipboardViewer());
12586
12587     clear_clipboard(hWnd1);
12588     ok_sequence(wm_clipboard_destroyed, "clear clipbd (no viewer, owner=1)", FALSE);
12589
12590     DestroyWindow(hWnd1);
12591     DestroyWindow(hWnd2);
12592     DestroyWindow(hWnd3);
12593     SetClipboardViewer(hOrigViewer);
12594 }
12595
12596 static void test_PostMessage(void)
12597 {
12598     static const struct
12599     {
12600         HWND hwnd;
12601         BOOL ret;
12602     } data[] =
12603     {
12604         { HWND_TOP /* 0 */, TRUE },
12605         { HWND_BROADCAST, TRUE },
12606         { HWND_BOTTOM, TRUE },
12607         { HWND_TOPMOST, TRUE },
12608         { HWND_NOTOPMOST, FALSE },
12609         { HWND_MESSAGE, FALSE },
12610         { (HWND)0xdeadbeef, FALSE }
12611     };
12612     int i;
12613     HWND hwnd;
12614     BOOL ret;
12615     MSG msg;
12616     static const WCHAR staticW[] = {'s','t','a','t','i','c',0};
12617
12618     SetLastError(0xdeadbeef);
12619     hwnd = CreateWindowExW(0, staticW, NULL, WS_POPUP, 0,0,0,0,0,0,0, NULL);
12620     if (!hwnd && GetLastError() == ERROR_CALL_NOT_IMPLEMENTED)
12621     {
12622         win_skip("Skipping some PostMessage tests on Win9x/WinMe\n");
12623         return;
12624     }
12625     assert(hwnd);
12626
12627     flush_events();
12628
12629     PostMessage(hwnd, WM_USER+1, 0x1234, 0x5678);
12630     PostMessage(0, WM_USER+2, 0x5678, 0x1234);
12631
12632     for (i = 0; i < sizeof(data)/sizeof(data[0]); i++)
12633     {
12634         memset(&msg, 0xab, sizeof(msg));
12635         ret = PeekMessageA(&msg, data[i].hwnd, 0, 0, PM_NOREMOVE);
12636         ok(ret == data[i].ret, "%d: hwnd %p expected %d, got %d\n", i, data[i].hwnd, data[i].ret, ret);
12637         if (data[i].ret)
12638         {
12639             if (data[i].hwnd)
12640                 ok(ret && msg.hwnd == 0 && msg.message == WM_USER+2 &&
12641                    msg.wParam == 0x5678 && msg.lParam == 0x1234,
12642                    "%d: got ret %d hwnd %p msg %04x wParam %08lx lParam %08lx instead of TRUE/0/WM_USER+2/0x5678/0x1234\n",
12643                    i, ret, msg.hwnd, msg.message, msg.wParam, msg.lParam);
12644             else
12645                 ok(ret && msg.hwnd == hwnd && msg.message == WM_USER+1 &&
12646                    msg.wParam == 0x1234 && msg.lParam == 0x5678,
12647                    "%d: got ret %d hwnd %p msg %04x wParam %08lx lParam %08lx instead of TRUE/%p/WM_USER+1/0x1234/0x5678\n",
12648                    i, ret, msg.hwnd, msg.message, msg.wParam, msg.lParam, msg.hwnd);
12649         }
12650     }
12651
12652     DestroyWindow(hwnd);
12653     flush_events();
12654 }
12655
12656 static const struct
12657 {
12658     DWORD exp, broken;
12659     BOOL todo;
12660 } wait_idle_expect[] =
12661 {
12662 /* 0 */  { WAIT_TIMEOUT, WAIT_TIMEOUT, FALSE },
12663          { WAIT_TIMEOUT, 0,            FALSE },
12664          { WAIT_TIMEOUT, 0,            FALSE },
12665          { WAIT_TIMEOUT, WAIT_TIMEOUT, FALSE },
12666          { WAIT_TIMEOUT, WAIT_TIMEOUT, FALSE },
12667 /* 5 */  { WAIT_TIMEOUT, 0,            FALSE },
12668          { WAIT_TIMEOUT, 0,            FALSE },
12669          { WAIT_TIMEOUT, WAIT_TIMEOUT, FALSE },
12670          { 0,            0,            FALSE },
12671          { 0,            0,            FALSE },
12672 /* 10 */ { 0,            0,            FALSE },
12673          { 0,            0,            FALSE },
12674          { 0,            WAIT_TIMEOUT, FALSE },
12675          { 0,            0,            FALSE },
12676          { 0,            0,            FALSE },
12677 /* 15 */ { 0,            0,            FALSE },
12678          { WAIT_TIMEOUT, 0,            FALSE },
12679          { WAIT_TIMEOUT, 0,            FALSE },
12680          { WAIT_TIMEOUT, 0,            FALSE },
12681          { WAIT_TIMEOUT, 0,            FALSE },
12682 /* 20 */ { WAIT_TIMEOUT, 0,            FALSE },
12683 };
12684
12685 static DWORD CALLBACK do_wait_idle_child_thread( void *arg )
12686 {
12687     MSG msg;
12688
12689     PeekMessage( &msg, 0, 0, 0, PM_NOREMOVE );
12690     Sleep( 200 );
12691     MsgWaitForMultipleObjects( 0, NULL, FALSE, 100, QS_ALLINPUT );
12692     return 0;
12693 }
12694
12695 static void do_wait_idle_child( int arg )
12696 {
12697     WNDCLASS cls;
12698     MSG msg;
12699     HWND hwnd = 0;
12700     HANDLE thread;
12701     DWORD id;
12702     HANDLE start_event = OpenEventA( EVENT_ALL_ACCESS, FALSE, "test_WaitForInputIdle_start" );
12703     HANDLE end_event = OpenEventA( EVENT_ALL_ACCESS, FALSE, "test_WaitForInputIdle_end" );
12704
12705     memset( &cls, 0, sizeof(cls) );
12706     cls.lpfnWndProc   = DefWindowProc;
12707     cls.hbrBackground = (HBRUSH)(COLOR_WINDOW + 1);
12708     cls.hCursor       = LoadCursor(0, IDC_ARROW);
12709     cls.lpszClassName = "TestClass";
12710     RegisterClass( &cls );
12711
12712     PeekMessage( &msg, 0, 0, 0, PM_NOREMOVE );  /* create the msg queue */
12713
12714     ok( start_event != 0, "failed to create start event, error %u\n", GetLastError() );
12715     ok( end_event != 0, "failed to create end event, error %u\n", GetLastError() );
12716
12717     switch (arg)
12718     {
12719     case 0:
12720         SetEvent( start_event );
12721         break;
12722     case 1:
12723         SetEvent( start_event );
12724         Sleep( 200 );
12725         PeekMessage( &msg, 0, 0, 0, PM_REMOVE );
12726         break;
12727     case 2:
12728         SetEvent( start_event );
12729         Sleep( 200 );
12730         PeekMessage( &msg, 0, 0, 0, PM_NOREMOVE );
12731         PostThreadMessage( GetCurrentThreadId(), WM_COMMAND, 0x1234, 0xabcd );
12732         PeekMessage( &msg, 0, 0, 0, PM_REMOVE );
12733         break;
12734     case 3:
12735         SetEvent( start_event );
12736         Sleep( 200 );
12737         SendMessage( HWND_BROADCAST, WM_WININICHANGE, 0, 0 );
12738         break;
12739     case 4:
12740         SetEvent( start_event );
12741         Sleep( 200 );
12742         hwnd = CreateWindowExA(0, "TestClass", NULL, WS_POPUP|WS_VISIBLE, 0, 0, 10, 10, 0, 0, 0, NULL);
12743         while (PeekMessage( &msg, 0, 0, 0, PM_REMOVE|PM_NOYIELD )) DispatchMessage( &msg );
12744         break;
12745     case 5:
12746         SetEvent( start_event );
12747         Sleep( 200 );
12748         hwnd = CreateWindowExA(0, "TestClass", NULL, WS_POPUP|WS_VISIBLE, 0, 0, 10, 10, 0, 0, 0, NULL);
12749         while (PeekMessage( &msg, 0, 0, 0, PM_REMOVE )) DispatchMessage( &msg );
12750         break;
12751     case 6:
12752         SetEvent( start_event );
12753         Sleep( 200 );
12754         hwnd = CreateWindowExA(0, "TestClass", NULL, WS_POPUP|WS_VISIBLE, 0, 0, 10, 10, 0, 0, 0, NULL);
12755         while (PeekMessage( &msg, 0, 0, 0, PM_NOREMOVE ))
12756         {
12757             GetMessage( &msg, 0, 0, 0 );
12758             DispatchMessage( &msg );
12759         }
12760         break;
12761     case 7:
12762         SetEvent( start_event );
12763         Sleep( 200 );
12764         hwnd = CreateWindowExA(0, "TestClass", NULL, WS_POPUP|WS_VISIBLE, 0, 0, 10, 10, 0, 0, 0, NULL);
12765         SetTimer( hwnd, 3, 1, NULL );
12766         Sleep( 200 );
12767         while (PeekMessage( &msg, 0, 0, 0, PM_REMOVE|PM_NOYIELD )) DispatchMessage( &msg );
12768         break;
12769     case 8:
12770         SetEvent( start_event );
12771         Sleep( 200 );
12772         PeekMessage( &msg, 0, 0, 0, PM_NOREMOVE );
12773         MsgWaitForMultipleObjects( 0, NULL, FALSE, 100, QS_ALLINPUT );
12774         break;
12775     case 9:
12776         SetEvent( start_event );
12777         Sleep( 200 );
12778         hwnd = CreateWindowExA(0, "TestClass", NULL, WS_POPUP|WS_VISIBLE, 0, 0, 10, 10, 0, 0, 0, NULL);
12779         while (PeekMessage( &msg, 0, 0, 0, PM_REMOVE )) DispatchMessage( &msg );
12780         for (;;) GetMessage( &msg, 0, 0, 0 );
12781         break;
12782     case 10:
12783         SetEvent( start_event );
12784         Sleep( 200 );
12785         hwnd = CreateWindowExA(0, "TestClass", NULL, WS_POPUP|WS_VISIBLE, 0, 0, 10, 10, 0, 0, 0, NULL);
12786         SetTimer( hwnd, 3, 1, NULL );
12787         Sleep( 200 );
12788         while (PeekMessage( &msg, 0, 0, 0, PM_REMOVE )) DispatchMessage( &msg );
12789         break;
12790     case 11:
12791         SetEvent( start_event );
12792         Sleep( 200 );
12793         return;  /* exiting the process makes WaitForInputIdle return success too */
12794     case 12:
12795         PeekMessage( &msg, 0, 0, 0, PM_NOREMOVE );
12796         Sleep( 200 );
12797         MsgWaitForMultipleObjects( 0, NULL, FALSE, 100, QS_ALLINPUT );
12798         SetEvent( start_event );
12799         break;
12800     case 13:
12801         SetEvent( start_event );
12802         PeekMessage( &msg, 0, 0, 0, PM_NOREMOVE );
12803         Sleep( 200 );
12804         thread = CreateThread( NULL, 0, do_wait_idle_child_thread, NULL, 0, &id );
12805         WaitForSingleObject( thread, 10000 );
12806         CloseHandle( thread );
12807         break;
12808     case 14:
12809         SetEvent( start_event );
12810         Sleep( 200 );
12811         PeekMessage( &msg, HWND_TOPMOST, 0, 0, PM_NOREMOVE );
12812         break;
12813     case 15:
12814         SetEvent( start_event );
12815         Sleep( 200 );
12816         PeekMessage( &msg, HWND_BROADCAST, 0, 0, PM_NOREMOVE );
12817         break;
12818     case 16:
12819         SetEvent( start_event );
12820         Sleep( 200 );
12821         PeekMessage( &msg, HWND_BOTTOM, 0, 0, PM_NOREMOVE );
12822         break;
12823     case 17:
12824         SetEvent( start_event );
12825         Sleep( 200 );
12826         PeekMessage( &msg, (HWND)0xdeadbeef, 0, 0, PM_NOREMOVE );
12827         break;
12828     case 18:
12829         SetEvent( start_event );
12830         Sleep( 200 );
12831         PeekMessage( &msg, HWND_NOTOPMOST, 0, 0, PM_NOREMOVE );
12832         break;
12833     case 19:
12834         SetEvent( start_event );
12835         Sleep( 200 );
12836         PeekMessage( &msg, HWND_MESSAGE, 0, 0, PM_NOREMOVE );
12837         break;
12838     case 20:
12839         SetEvent( start_event );
12840         Sleep( 200 );
12841         PeekMessage( &msg, GetDesktopWindow(), 0, 0, PM_NOREMOVE );
12842         break;
12843     }
12844     WaitForSingleObject( end_event, 2000 );
12845     CloseHandle( start_event );
12846     CloseHandle( end_event );
12847     if (hwnd) DestroyWindow( hwnd );
12848 }
12849
12850 static LRESULT CALLBACK wait_idle_proc( HWND hwnd, UINT msg, WPARAM wp, LPARAM lp )
12851 {
12852     if (msg == WM_WININICHANGE) Sleep( 200 );  /* make sure the child waits */
12853     return DefWindowProcA( hwnd, msg, wp, lp );
12854 }
12855
12856 static DWORD CALLBACK wait_idle_thread( void *arg )
12857 {
12858     WNDCLASS cls;
12859     MSG msg;
12860     HWND hwnd;
12861
12862     memset( &cls, 0, sizeof(cls) );
12863     cls.lpfnWndProc   = wait_idle_proc;
12864     cls.hbrBackground = (HBRUSH)(COLOR_WINDOW + 1);
12865     cls.hCursor       = LoadCursor(0, IDC_ARROW);
12866     cls.lpszClassName = "TestClass";
12867     RegisterClass( &cls );
12868
12869     hwnd = CreateWindowExA(0, "TestClass", NULL, WS_POPUP, 0, 0, 10, 10, 0, 0, 0, NULL);
12870     while (GetMessage( &msg, 0, 0, 0 )) DispatchMessage( &msg );
12871     DestroyWindow(hwnd);
12872     return 0;
12873 }
12874
12875 static void test_WaitForInputIdle( char *argv0 )
12876 {
12877     char path[MAX_PATH];
12878     PROCESS_INFORMATION pi;
12879     STARTUPINFOA startup;
12880     BOOL ret;
12881     HANDLE start_event, end_event, thread;
12882     unsigned int i;
12883     DWORD id;
12884     const IMAGE_DOS_HEADER *dos = (const IMAGE_DOS_HEADER *)GetModuleHandleA(0);
12885     const IMAGE_NT_HEADERS *nt = (const IMAGE_NT_HEADERS *)((const char *)dos + dos->e_lfanew);
12886     BOOL console_app = (nt->OptionalHeader.Subsystem != IMAGE_SUBSYSTEM_WINDOWS_GUI);
12887
12888     if (console_app)  /* build the test with -mwindows for better coverage */
12889         trace( "not built as a GUI app, WaitForInputIdle may not be fully tested\n" );
12890
12891     start_event = CreateEventA(NULL, 0, 0, "test_WaitForInputIdle_start");
12892     end_event = CreateEventA(NULL, 0, 0, "test_WaitForInputIdle_end");
12893     ok(start_event != 0, "failed to create start event, error %u\n", GetLastError());
12894     ok(end_event != 0, "failed to create end event, error %u\n", GetLastError());
12895
12896     memset( &startup, 0, sizeof(startup) );
12897     startup.cb = sizeof(startup);
12898     startup.dwFlags = STARTF_USESHOWWINDOW;
12899     startup.wShowWindow = SW_SHOWNORMAL;
12900
12901     thread = CreateThread( NULL, 0, wait_idle_thread, NULL, 0, &id );
12902
12903     for (i = 0; i < sizeof(wait_idle_expect)/sizeof(wait_idle_expect[0]); i++)
12904     {
12905         ResetEvent( start_event );
12906         ResetEvent( end_event );
12907         sprintf( path, "%s msg %u", argv0, i );
12908         ret = CreateProcessA( NULL, path, NULL, NULL, TRUE, 0, NULL, NULL, &startup, &pi );
12909         ok( ret, "CreateProcess '%s' failed err %u.\n", path, GetLastError() );
12910         if (ret)
12911         {
12912             ret = WaitForSingleObject( start_event, 5000 );
12913             ok( ret == WAIT_OBJECT_0, "%u: WaitForSingleObject failed\n", i );
12914             if (ret == WAIT_OBJECT_0)
12915             {
12916                 ret = WaitForInputIdle( pi.hProcess, 1000 );
12917                 if (ret == WAIT_FAILED)
12918                     ok( console_app ||
12919                         ret == wait_idle_expect[i].exp ||
12920                         broken(ret == wait_idle_expect[i].broken),
12921                         "%u: WaitForInputIdle error %08x expected %08x\n",
12922                         i, ret, wait_idle_expect[i].exp );
12923                 else if (wait_idle_expect[i].todo)
12924                     todo_wine
12925                     ok( ret == wait_idle_expect[i].exp || broken(ret == wait_idle_expect[i].broken),
12926                         "%u: WaitForInputIdle error %08x expected %08x\n",
12927                         i, ret, wait_idle_expect[i].exp );
12928                 else
12929                     ok( ret == wait_idle_expect[i].exp || broken(ret == wait_idle_expect[i].broken),
12930                         "%u: WaitForInputIdle error %08x expected %08x\n",
12931                         i, ret, wait_idle_expect[i].exp );
12932                 SetEvent( end_event );
12933                 WaitForSingleObject( pi.hProcess, 1000 );  /* give it a chance to exit on its own */
12934             }
12935             TerminateProcess( pi.hProcess, 0 );  /* just in case */
12936             winetest_wait_child_process( pi.hProcess );
12937             ret = WaitForInputIdle( pi.hProcess, 100 );
12938             ok( ret == WAIT_FAILED, "%u: WaitForInputIdle after exit error %08x\n", i, ret );
12939             CloseHandle( pi.hProcess );
12940             CloseHandle( pi.hThread );
12941         }
12942     }
12943     CloseHandle( start_event );
12944     PostThreadMessage( id, WM_QUIT, 0, 0 );
12945     WaitForSingleObject( thread, 10000 );
12946     CloseHandle( thread );
12947 }
12948
12949 static const struct message WmSetParentSeq_1[] = {
12950     { WM_SHOWWINDOW, sent|wparam, 0 },
12951     { EVENT_OBJECT_PARENTCHANGE, winevent_hook|wparam|lparam, 0, 0 },
12952     { WM_WINDOWPOSCHANGING, sent|wparam, SWP_NOSIZE },
12953     { WM_CHILDACTIVATE, sent },
12954     { WM_WINDOWPOSCHANGED, sent|wparam, SWP_NOSIZE|SWP_NOREDRAW|SWP_NOCLIENTSIZE },
12955     { WM_MOVE, sent|defwinproc|wparam, 0 },
12956     { EVENT_OBJECT_LOCATIONCHANGE, winevent_hook|wparam|lparam, 0, 0 },
12957     { WM_SHOWWINDOW, sent|wparam, 1 },
12958     { 0 }
12959 };
12960
12961 static const struct message WmSetParentSeq_2[] = {
12962     { WM_SHOWWINDOW, sent|wparam, 0 },
12963     { WM_WINDOWPOSCHANGING, sent|wparam, SWP_HIDEWINDOW|SWP_NOSIZE|SWP_NOMOVE },
12964     { EVENT_OBJECT_HIDE, winevent_hook|wparam|lparam, 0, 0 },
12965     { WM_WINDOWPOSCHANGED, sent|wparam, SWP_HIDEWINDOW|SWP_NOSIZE|SWP_NOMOVE|SWP_NOCLIENTSIZE|SWP_NOCLIENTMOVE },
12966     { HCBT_SETFOCUS, hook|optional },
12967     { WM_NCACTIVATE, sent|wparam|optional, 0 },
12968     { WM_ACTIVATE, sent|wparam|optional, 0 },
12969     { WM_ACTIVATEAPP, sent|wparam|optional, 0 },
12970     { WM_KILLFOCUS, sent|wparam, 0 },
12971     { EVENT_OBJECT_PARENTCHANGE, winevent_hook|wparam|lparam, 0, 0 },
12972     { WM_WINDOWPOSCHANGING, sent|wparam, SWP_NOSIZE },
12973     { HCBT_ACTIVATE, hook|optional },
12974     { EVENT_SYSTEM_FOREGROUND, winevent_hook|wparam|lparam, 0, 0 },
12975     { WM_WINDOWPOSCHANGING, sent|wparam|optional, SWP_NOSIZE|SWP_NOMOVE },
12976     { WM_NCACTIVATE, sent|wparam|optional, 1 },
12977     { WM_ACTIVATE, sent|wparam|optional, 1 },
12978     { HCBT_SETFOCUS, hook|optional },
12979     { EVENT_OBJECT_FOCUS, winevent_hook|wparam|lparam, OBJID_CLIENT, 0 },
12980     { WM_SETFOCUS, sent|optional|defwinproc },
12981     { WM_WINDOWPOSCHANGED, sent|wparam, SWP_NOREDRAW|SWP_NOSIZE|SWP_NOCLIENTSIZE },
12982     { WM_MOVE, sent|defwinproc|wparam, 0 },
12983     { EVENT_OBJECT_LOCATIONCHANGE, winevent_hook|wparam|lparam, 0, 0 },
12984     { WM_SHOWWINDOW, sent|wparam, 1 },
12985     { WM_WINDOWPOSCHANGING, sent|wparam, SWP_SHOWWINDOW|SWP_NOSIZE|SWP_NOMOVE },
12986     { EVENT_OBJECT_SHOW, winevent_hook|wparam|lparam, 0, 0 },
12987     { WM_WINDOWPOSCHANGED, sent|wparam, SWP_SHOWWINDOW|SWP_NOSIZE|SWP_NOMOVE|SWP_NOCLIENTSIZE|SWP_NOCLIENTMOVE },
12988     { 0 }
12989 };
12990
12991
12992 static void test_SetParent(void)
12993 {
12994     HWND parent1, parent2, child, popup;
12995     RECT rc, rc_old;
12996
12997     parent1 = CreateWindowEx(0, "TestParentClass", NULL, WS_OVERLAPPEDWINDOW,
12998                             100, 100, 200, 200, 0, 0, 0, NULL);
12999     ok(parent1 != 0, "Failed to create parent1 window\n");
13000
13001     parent2 = CreateWindowEx(0, "TestParentClass", NULL, WS_OVERLAPPEDWINDOW,
13002                             400, 100, 200, 200, 0, 0, 0, NULL);
13003     ok(parent2 != 0, "Failed to create parent2 window\n");
13004
13005     /* WS_CHILD window */
13006     child = CreateWindowEx(0, "TestWindowClass", NULL, WS_CHILD | WS_VISIBLE,
13007                            10, 10, 150, 150, parent1, 0, 0, NULL);
13008     ok(child != 0, "Failed to create child window\n");
13009
13010     GetWindowRect(parent1, &rc);
13011     trace("parent1 (%d,%d)-(%d,%d)\n", rc.left, rc.top, rc.right, rc.bottom);
13012     GetWindowRect(child, &rc_old);
13013     MapWindowPoints(0, parent1, (POINT *)&rc_old, 2);
13014     trace("child (%d,%d)-(%d,%d)\n", rc_old.left, rc_old.top, rc_old.right, rc_old.bottom);
13015
13016     flush_sequence();
13017
13018     SetParent(child, parent2);
13019     flush_events();
13020     ok_sequence(WmSetParentSeq_1, "SetParent() visible WS_CHILD", TRUE);
13021
13022     ok(GetWindowLongA(child, GWL_STYLE) & WS_VISIBLE, "WS_VISIBLE should be set\n");
13023     ok(!IsWindowVisible(child), "IsWindowVisible() should return FALSE\n");
13024
13025     GetWindowRect(parent2, &rc);
13026     trace("parent2 (%d,%d)-(%d,%d)\n", rc.left, rc.top, rc.right, rc.bottom);
13027     GetWindowRect(child, &rc);
13028     MapWindowPoints(0, parent2, (POINT *)&rc, 2);
13029     trace("child (%d,%d)-(%d,%d)\n", rc.left, rc.top, rc.right, rc.bottom);
13030
13031     ok(EqualRect(&rc_old, &rc), "rects do not match (%d,%d-%d,%d) / (%d,%d-%d,%d)\n",
13032        rc_old.left, rc_old.top, rc_old.right, rc_old.bottom,
13033        rc.left, rc.top, rc.right, rc.bottom );
13034
13035     /* WS_POPUP window */
13036     popup = CreateWindowEx(0, "TestWindowClass", NULL, WS_POPUP | WS_VISIBLE,
13037                            20, 20, 100, 100, 0, 0, 0, NULL);
13038     ok(popup != 0, "Failed to create popup window\n");
13039
13040     GetWindowRect(popup, &rc_old);
13041     trace("popup (%d,%d)-(%d,%d)\n", rc_old.left, rc_old.top, rc_old.right, rc_old.bottom);
13042
13043     flush_sequence();
13044
13045     SetParent(popup, child);
13046     flush_events();
13047     ok_sequence(WmSetParentSeq_2, "SetParent() visible WS_POPUP", TRUE);
13048
13049     ok(GetWindowLongA(popup, GWL_STYLE) & WS_VISIBLE, "WS_VISIBLE should be set\n");
13050     ok(!IsWindowVisible(popup), "IsWindowVisible() should return FALSE\n");
13051
13052     GetWindowRect(child, &rc);
13053     trace("parent2 (%d,%d)-(%d,%d)\n", rc.left, rc.top, rc.right, rc.bottom);
13054     GetWindowRect(popup, &rc);
13055     MapWindowPoints(0, child, (POINT *)&rc, 2);
13056     trace("popup (%d,%d)-(%d,%d)\n", rc.left, rc.top, rc.right, rc.bottom);
13057
13058     ok(EqualRect(&rc_old, &rc), "rects do not match (%d,%d-%d,%d) / (%d,%d-%d,%d)\n",
13059        rc_old.left, rc_old.top, rc_old.right, rc_old.bottom,
13060        rc.left, rc.top, rc.right, rc.bottom );
13061
13062     DestroyWindow(popup);
13063     DestroyWindow(child);
13064     DestroyWindow(parent1);
13065     DestroyWindow(parent2);
13066
13067     flush_sequence();
13068 }
13069
13070 static const struct message WmKeyReleaseOnly[] = {
13071     { HCBT_KEYSKIPPED, hook|wparam|lparam|optional, 0x41, 0x80000001 },
13072     { WM_KEYUP, sent|wparam|lparam, 0x41, 0x80000001 },
13073     { 0 }
13074 };
13075 static const struct message WmKeyPressNormal[] = {
13076     { HCBT_KEYSKIPPED, hook|wparam|lparam|optional, 0x41, 0x1 },
13077     { WM_KEYDOWN, sent|wparam|lparam, 0x41, 0x1 },
13078     { 0 }
13079 };
13080 static const struct message WmKeyPressRepeat[] = {
13081     { HCBT_KEYSKIPPED, hook|wparam|lparam|optional, 0x41, 0x40000001 },
13082     { WM_KEYDOWN, sent|wparam|lparam, 0x41, 0x40000001 },
13083     { 0 }
13084 };
13085 static const struct message WmKeyReleaseNormal[] = {
13086     { HCBT_KEYSKIPPED, hook|wparam|lparam|optional, 0x41, 0xc0000001 },
13087     { WM_KEYUP, sent|wparam|lparam, 0x41, 0xc0000001 },
13088     { 0 }
13089 };
13090
13091 static void test_keyflags(void)
13092 {
13093     HWND test_window;
13094     SHORT key_state;
13095     BYTE keyboard_state[256];
13096     MSG msg;
13097
13098     test_window = CreateWindowEx(0, "TestWindowClass", NULL, WS_OVERLAPPEDWINDOW | WS_VISIBLE,
13099                            100, 100, 200, 200, 0, 0, 0, NULL);
13100
13101     flush_events();
13102     flush_sequence();
13103
13104     /* keyup without a keydown */
13105     keybd_event(0x41, 0, KEYEVENTF_KEYUP, 0);
13106     while (PeekMessage(&msg, NULL, WM_KEYFIRST, WM_KEYLAST, PM_REMOVE))
13107         DispatchMessage(&msg);
13108     ok_sequence(WmKeyReleaseOnly, "key release only", TRUE);
13109
13110     key_state = GetAsyncKeyState(0x41);
13111     ok((key_state & 0x8000) == 0, "unexpected key state %x\n", key_state);
13112
13113     key_state = GetKeyState(0x41);
13114     ok((key_state & 0x8000) == 0, "unexpected key state %x\n", key_state);
13115
13116     /* keydown */
13117     keybd_event(0x41, 0, 0, 0);
13118     while (PeekMessage(&msg, NULL, WM_KEYFIRST, WM_KEYLAST, PM_REMOVE))
13119         DispatchMessage(&msg);
13120     ok_sequence(WmKeyPressNormal, "key press only", FALSE);
13121
13122     key_state = GetAsyncKeyState(0x41);
13123     ok((key_state & 0x8000) == 0x8000, "unexpected key state %x\n", key_state);
13124
13125     key_state = GetKeyState(0x41);
13126     ok((key_state & 0x8000) == 0x8000, "unexpected key state %x\n", key_state);
13127
13128     /* keydown repeat */
13129     keybd_event(0x41, 0, 0, 0);
13130     while (PeekMessage(&msg, NULL, WM_KEYFIRST, WM_KEYLAST, PM_REMOVE))
13131         DispatchMessage(&msg);
13132     ok_sequence(WmKeyPressRepeat, "key press repeat", FALSE);
13133
13134     key_state = GetAsyncKeyState(0x41);
13135     ok((key_state & 0x8000) == 0x8000, "unexpected key state %x\n", key_state);
13136
13137     key_state = GetKeyState(0x41);
13138     ok((key_state & 0x8000) == 0x8000, "unexpected key state %x\n", key_state);
13139
13140     /* keyup */
13141     keybd_event(0x41, 0, KEYEVENTF_KEYUP, 0);
13142     while (PeekMessage(&msg, NULL, WM_KEYFIRST, WM_KEYLAST, PM_REMOVE))
13143         DispatchMessage(&msg);
13144     ok_sequence(WmKeyReleaseNormal, "key release repeat", FALSE);
13145
13146     key_state = GetAsyncKeyState(0x41);
13147     ok((key_state & 0x8000) == 0, "unexpected key state %x\n", key_state);
13148
13149     key_state = GetKeyState(0x41);
13150     ok((key_state & 0x8000) == 0, "unexpected key state %x\n", key_state);
13151
13152     /* set the key state in this thread */
13153     GetKeyboardState(keyboard_state);
13154     keyboard_state[0x41] = 0x80;
13155     SetKeyboardState(keyboard_state);
13156
13157     key_state = GetAsyncKeyState(0x41);
13158     ok((key_state & 0x8000) == 0, "unexpected key state %x\n", key_state);
13159
13160     /* keydown */
13161     keybd_event(0x41, 0, 0, 0);
13162     while (PeekMessage(&msg, NULL, WM_KEYFIRST, WM_KEYLAST, PM_REMOVE))
13163         DispatchMessage(&msg);
13164     ok_sequence(WmKeyPressRepeat, "key press after setkeyboardstate", TRUE);
13165
13166     key_state = GetAsyncKeyState(0x41);
13167     ok((key_state & 0x8000) == 0x8000, "unexpected key state %x\n", key_state);
13168
13169     key_state = GetKeyState(0x41);
13170     ok((key_state & 0x8000) == 0x8000, "unexpected key state %x\n", key_state);
13171
13172     /* clear the key state in this thread */
13173     GetKeyboardState(keyboard_state);
13174     keyboard_state[0x41] = 0;
13175     SetKeyboardState(keyboard_state);
13176
13177     key_state = GetAsyncKeyState(0x41);
13178     ok((key_state & 0x8000) == 0x8000, "unexpected key state %x\n", key_state);
13179
13180     /* keyup */
13181     keybd_event(0x41, 0, KEYEVENTF_KEYUP, 0);
13182     while (PeekMessage(&msg, NULL, WM_KEYFIRST, WM_KEYLAST, PM_REMOVE))
13183         DispatchMessage(&msg);
13184     ok_sequence(WmKeyReleaseOnly, "key release after setkeyboardstate", TRUE);
13185
13186     key_state = GetAsyncKeyState(0x41);
13187     ok((key_state & 0x8000) == 0, "unexpected key state %x\n", key_state);
13188
13189     key_state = GetKeyState(0x41);
13190     ok((key_state & 0x8000) == 0, "unexpected key state %x\n", key_state);
13191
13192     DestroyWindow(test_window);
13193     flush_sequence();
13194 }
13195
13196 static const struct message WmHotkeyPressLWIN[] = {
13197     { WM_KEYDOWN, kbd_hook|wparam|lparam, VK_LWIN, LLKHF_INJECTED },
13198     { HCBT_KEYSKIPPED, hook|wparam|lparam|optional, VK_LWIN, 1 },
13199     { WM_KEYDOWN, sent|wparam|lparam, VK_LWIN, 1 },
13200     { 0 }
13201 };
13202 static const struct message WmHotkeyPress[] = {
13203     { WM_KEYDOWN, kbd_hook|lparam, 0, LLKHF_INJECTED },
13204     { WM_HOTKEY, sent|wparam, 5 },
13205     { 0 }
13206 };
13207 static const struct message WmHotkeyRelease[] = {
13208     { WM_KEYUP, kbd_hook|lparam, 0, LLKHF_INJECTED|LLKHF_UP },
13209     { HCBT_KEYSKIPPED, hook|lparam|optional, 0, 0x80000001 },
13210     { WM_KEYUP, sent|lparam, 0, 0x80000001 },
13211     { 0 }
13212 };
13213 static const struct message WmHotkeyReleaseLWIN[] = {
13214     { WM_KEYUP, kbd_hook|wparam|lparam, VK_LWIN, LLKHF_INJECTED|LLKHF_UP },
13215     { HCBT_KEYSKIPPED, hook|wparam|lparam|optional, VK_LWIN, 0xc0000001 },
13216     { WM_KEYUP, sent|wparam|lparam, VK_LWIN, 0xc0000001 },
13217     { 0 }
13218 };
13219 static const struct message WmHotkeyCombined[] = {
13220     { WM_KEYDOWN, kbd_hook|wparam|lparam, VK_LWIN, LLKHF_INJECTED },
13221     { WM_KEYDOWN, kbd_hook|lparam, 0, LLKHF_INJECTED },
13222     { WM_KEYUP, kbd_hook|lparam, 0, LLKHF_INJECTED|LLKHF_UP },
13223     { WM_KEYUP, kbd_hook|wparam|lparam, VK_LWIN, LLKHF_INJECTED|LLKHF_UP },
13224     { WM_APP, sent, 0, 0 },
13225     { WM_HOTKEY, sent|wparam, 5 },
13226     { WM_APP+1, sent, 0, 0 },
13227     { HCBT_KEYSKIPPED, hook|wparam|lparam|optional, VK_LWIN, 1 },
13228     { WM_KEYDOWN, sent|wparam|lparam, VK_LWIN, 1 },
13229     { HCBT_KEYSKIPPED, hook|optional, 0, 0x80000001 },
13230     { WM_KEYUP, sent, 0, 0x80000001 }, /* lparam not checked so the sequence isn't a todo */
13231     { HCBT_KEYSKIPPED, hook|wparam|lparam|optional, VK_LWIN, 0xc0000001 },
13232     { WM_KEYUP, sent|wparam|lparam, VK_LWIN, 0xc0000001 },
13233     { 0 }
13234 };
13235 static const struct message WmHotkeyPrevious[] = {
13236     { WM_KEYDOWN, kbd_hook|wparam|lparam, VK_LWIN, LLKHF_INJECTED },
13237     { WM_KEYDOWN, kbd_hook|lparam, 0, LLKHF_INJECTED },
13238     { WM_KEYUP, kbd_hook|lparam, 0, LLKHF_INJECTED|LLKHF_UP },
13239     { WM_KEYUP, kbd_hook|wparam|lparam, VK_LWIN, LLKHF_INJECTED|LLKHF_UP },
13240     { HCBT_KEYSKIPPED, hook|wparam|lparam|optional, VK_LWIN, 1 },
13241     { WM_KEYDOWN, sent|wparam|lparam, VK_LWIN, 1 },
13242     { HCBT_KEYSKIPPED, hook|lparam|optional, 0, 1 },
13243     { WM_KEYDOWN, sent|lparam, 0, 1 },
13244     { HCBT_KEYSKIPPED, hook|optional|lparam, 0, 0xc0000001 },
13245     { WM_KEYUP, sent|lparam, 0, 0xc0000001 },
13246     { HCBT_KEYSKIPPED, hook|wparam|lparam|optional, VK_LWIN, 0xc0000001 },
13247     { WM_KEYUP, sent|wparam|lparam, VK_LWIN, 0xc0000001 },
13248     { 0 }
13249 };
13250 static const struct message WmHotkeyNew[] = {
13251     { WM_KEYDOWN, kbd_hook|lparam, 0, LLKHF_INJECTED },
13252     { WM_KEYUP, kbd_hook|lparam, 0, LLKHF_INJECTED|LLKHF_UP },
13253     { WM_HOTKEY, sent|wparam, 5 },
13254     { HCBT_KEYSKIPPED, hook|optional, 0, 0x80000001 },
13255     { WM_KEYUP, sent, 0, 0x80000001 }, /* lparam not checked so the sequence isn't a todo */
13256     { 0 }
13257 };
13258
13259 static int hotkey_letter;
13260
13261 static LRESULT CALLBACK KeyboardHookProc(int nCode, WPARAM wParam, LPARAM lParam)
13262 {
13263     struct recvd_message msg;
13264
13265     if (nCode == HC_ACTION)
13266     {
13267         KBDLLHOOKSTRUCT *kdbhookstruct = (KBDLLHOOKSTRUCT*)lParam;
13268
13269         msg.hwnd = 0;
13270         msg.message = wParam;
13271         msg.flags = kbd_hook|wparam|lparam;
13272         msg.wParam = kdbhookstruct->vkCode;
13273         msg.lParam = kdbhookstruct->flags;
13274         msg.descr = "KeyboardHookProc";
13275         add_message(&msg);
13276
13277         if (wParam == WM_KEYUP || wParam == WM_KEYDOWN)
13278         {
13279             ok(kdbhookstruct->vkCode == VK_LWIN || kdbhookstruct->vkCode == hotkey_letter,
13280                "unexpected keycode %x\n", kdbhookstruct->vkCode);
13281        }
13282     }
13283
13284     return CallNextHookEx(hKBD_hook, nCode, wParam, lParam);
13285 }
13286
13287 static void test_hotkey(void)
13288 {
13289     HWND test_window, taskbar_window;
13290     BOOL ret;
13291     MSG msg;
13292     DWORD queue_status;
13293     SHORT key_state;
13294
13295     SetLastError(0xdeadbeef);
13296     ret = UnregisterHotKey(NULL, 0);
13297     ok(ret == FALSE, "expected FALSE, got %i\n", ret);
13298     ok(GetLastError() == ERROR_HOTKEY_NOT_REGISTERED || broken(GetLastError() == 0xdeadbeef),
13299        "unexpected error %d\n", GetLastError());
13300
13301     if (ret == TRUE)
13302     {
13303         skip("hotkeys not supported\n");
13304         return;
13305     }
13306
13307     test_window = CreateWindowEx(0, "HotkeyWindowClass", NULL, WS_OVERLAPPEDWINDOW | WS_VISIBLE,
13308                            100, 100, 200, 200, 0, 0, 0, NULL);
13309
13310     flush_sequence();
13311
13312     SetLastError(0xdeadbeef);
13313     ret = UnregisterHotKey(test_window, 0);
13314     ok(ret == FALSE, "expected FALSE, got %i\n", ret);
13315     ok(GetLastError() == ERROR_HOTKEY_NOT_REGISTERED || broken(GetLastError() == 0xdeadbeef),
13316        "unexpected error %d\n", GetLastError());
13317
13318     /* Search for a Windows Key + letter combination that hasn't been registered */
13319     for (hotkey_letter = 0x41; hotkey_letter <= 0x51; hotkey_letter ++)
13320     {
13321         SetLastError(0xdeadbeef);
13322         ret = RegisterHotKey(test_window, 5, MOD_WIN, hotkey_letter);
13323
13324         if (ret == TRUE)
13325         {
13326             break;
13327         }
13328         else
13329         {
13330             ok(GetLastError() == ERROR_HOTKEY_ALREADY_REGISTERED || broken(GetLastError() == 0xdeadbeef),
13331                "unexpected error %d\n", GetLastError());
13332         }
13333     }
13334
13335     if (hotkey_letter == 0x52)
13336     {
13337         ok(0, "Couldn't find any free Windows Key + letter combination\n");
13338         goto end;
13339     }
13340
13341     hKBD_hook = SetWindowsHookEx(WH_KEYBOARD_LL, KeyboardHookProc, GetModuleHandle(NULL), 0);
13342     if (!hKBD_hook) win_skip("WH_KEYBOARD_LL is not supported\n");
13343
13344     /* Same key combination, different id */
13345     SetLastError(0xdeadbeef);
13346     ret = RegisterHotKey(test_window, 4, MOD_WIN, hotkey_letter);
13347     ok(ret == FALSE, "expected FALSE, got %i\n", ret);
13348     ok(GetLastError() == ERROR_HOTKEY_ALREADY_REGISTERED || broken(GetLastError() == 0xdeadbeef),
13349        "unexpected error %d\n", GetLastError());
13350
13351     /* Same key combination, different window */
13352     SetLastError(0xdeadbeef);
13353     ret = RegisterHotKey(NULL, 5, MOD_WIN, hotkey_letter);
13354     ok(ret == FALSE, "expected FALSE, got %i\n", ret);
13355     ok(GetLastError() == ERROR_HOTKEY_ALREADY_REGISTERED || broken(GetLastError() == 0xdeadbeef),
13356        "unexpected error %d\n", GetLastError());
13357
13358     /* Register the same hotkey twice */
13359     SetLastError(0xdeadbeef);
13360     ret = RegisterHotKey(test_window, 5, MOD_WIN, hotkey_letter);
13361     ok(ret == FALSE, "expected FALSE, got %i\n", ret);
13362     ok(GetLastError() == ERROR_HOTKEY_ALREADY_REGISTERED || broken(GetLastError() == 0xdeadbeef),
13363        "unexpected error %d\n", GetLastError());
13364
13365     /* Window on another thread */
13366     taskbar_window = FindWindowA("Shell_TrayWnd", NULL);
13367     if (!taskbar_window)
13368     {
13369         skip("no taskbar?\n");
13370     }
13371     else
13372     {
13373         SetLastError(0xdeadbeef);
13374         ret = RegisterHotKey(taskbar_window, 5, 0, hotkey_letter);
13375         ok(ret == FALSE, "expected FALSE, got %i\n", ret);
13376         ok(GetLastError() == ERROR_WINDOW_OF_OTHER_THREAD || broken(GetLastError() == 0xdeadbeef),
13377            "unexpected error %d\n", GetLastError());
13378     }
13379
13380     /* Inject the appropriate key sequence */
13381     keybd_event(VK_LWIN, 0, 0, 0);
13382     while (PeekMessage(&msg, NULL, 0, 0, PM_REMOVE))
13383         DispatchMessage(&msg);
13384     ok_sequence(WmHotkeyPressLWIN, "window hotkey press LWIN", FALSE);
13385
13386     keybd_event(hotkey_letter, 0, 0, 0);
13387     queue_status = GetQueueStatus(QS_HOTKEY);
13388     ok((queue_status & (QS_HOTKEY << 16)) == QS_HOTKEY << 16, "expected QS_HOTKEY << 16 set, got %x\n", queue_status);
13389     while (PeekMessage(&msg, NULL, 0, 0, PM_REMOVE))
13390     {
13391         if (msg.message == WM_HOTKEY)
13392         {
13393             ok(msg.hwnd == test_window, "unexpected hwnd %p\n", msg.hwnd);
13394             ok(msg.lParam == MAKELPARAM(MOD_WIN, hotkey_letter), "unexpected WM_HOTKEY lparam %lx\n", msg.lParam);
13395         }
13396         DispatchMessage(&msg);
13397     }
13398     ok_sequence(WmHotkeyPress, "window hotkey press", FALSE);
13399
13400     queue_status = GetQueueStatus(QS_HOTKEY);
13401     ok((queue_status & (QS_HOTKEY << 16)) == 0, "expected QS_HOTKEY << 16 cleared, got %x\n", queue_status);
13402
13403     key_state = GetAsyncKeyState(hotkey_letter);
13404     ok((key_state & 0x8000) == 0x8000, "unexpected key state %x\n", key_state);
13405
13406     keybd_event(hotkey_letter, 0, KEYEVENTF_KEYUP, 0);
13407     while (PeekMessage(&msg, NULL, 0, 0, PM_REMOVE))
13408         DispatchMessage(&msg);
13409     ok_sequence(WmHotkeyRelease, "window hotkey release", TRUE);
13410
13411     keybd_event(VK_LWIN, 0, KEYEVENTF_KEYUP, 0);
13412     while (PeekMessage(&msg, NULL, 0, 0, PM_REMOVE))
13413         DispatchMessage(&msg);
13414     ok_sequence(WmHotkeyReleaseLWIN, "window hotkey release LWIN", FALSE);
13415
13416     /* normal posted WM_HOTKEY messages set QS_HOTKEY */
13417     PostMessage(test_window, WM_HOTKEY, 0, 0);
13418     queue_status = GetQueueStatus(QS_HOTKEY);
13419     ok((queue_status & (QS_HOTKEY << 16)) == QS_HOTKEY << 16, "expected QS_HOTKEY << 16 set, got %x\n", queue_status);
13420     queue_status = GetQueueStatus(QS_POSTMESSAGE);
13421     ok((queue_status & (QS_POSTMESSAGE << 16)) == QS_POSTMESSAGE << 16, "expected QS_POSTMESSAGE << 16 set, got %x\n", queue_status);
13422     while (PeekMessage(&msg, NULL, 0, 0, PM_REMOVE))
13423         DispatchMessage(&msg);
13424     flush_sequence();
13425
13426     /* Send and process all messages at once */
13427     PostMessage(test_window, WM_APP, 0, 0);
13428     keybd_event(VK_LWIN, 0, 0, 0);
13429     keybd_event(hotkey_letter, 0, 0, 0);
13430     keybd_event(hotkey_letter, 0, KEYEVENTF_KEYUP, 0);
13431     keybd_event(VK_LWIN, 0, KEYEVENTF_KEYUP, 0);
13432
13433     while (PeekMessage(&msg, NULL, 0, 0, PM_REMOVE))
13434     {
13435         if (msg.message == WM_HOTKEY)
13436         {
13437             ok(msg.hwnd == test_window, "unexpected hwnd %p\n", msg.hwnd);
13438             ok(msg.lParam == MAKELPARAM(MOD_WIN, hotkey_letter), "unexpected WM_HOTKEY lparam %lx\n", msg.lParam);
13439         }
13440         DispatchMessage(&msg);
13441     }
13442     ok_sequence(WmHotkeyCombined, "window hotkey combined", FALSE);
13443
13444     /* Register same hwnd/id with different key combination */
13445     ret = RegisterHotKey(test_window, 5, 0, hotkey_letter);
13446     ok(ret == TRUE, "expected TRUE, got %i, err=%d\n", ret, GetLastError());
13447
13448     /* Previous key combination does not work */
13449     keybd_event(VK_LWIN, 0, 0, 0);
13450     keybd_event(hotkey_letter, 0, 0, 0);
13451     keybd_event(hotkey_letter, 0, KEYEVENTF_KEYUP, 0);
13452     keybd_event(VK_LWIN, 0, KEYEVENTF_KEYUP, 0);
13453
13454     while (PeekMessage(&msg, NULL, 0, 0, PM_REMOVE))
13455         DispatchMessage(&msg);
13456     ok_sequence(WmHotkeyPrevious, "window hotkey previous", FALSE);
13457
13458     /* New key combination works */
13459     keybd_event(hotkey_letter, 0, 0, 0);
13460     keybd_event(hotkey_letter, 0, KEYEVENTF_KEYUP, 0);
13461
13462     while (PeekMessage(&msg, NULL, 0, 0, PM_REMOVE))
13463     {
13464         if (msg.message == WM_HOTKEY)
13465         {
13466             ok(msg.hwnd == test_window, "unexpected hwnd %p\n", msg.hwnd);
13467             ok(msg.lParam == MAKELPARAM(0, hotkey_letter), "unexpected WM_HOTKEY lparam %lx\n", msg.lParam);
13468         }
13469         DispatchMessage(&msg);
13470     }
13471     ok_sequence(WmHotkeyNew, "window hotkey new", FALSE);
13472
13473     /* Unregister hotkey properly */
13474     ret = UnregisterHotKey(test_window, 5);
13475     ok(ret == TRUE, "expected TRUE, got %i, err=%d\n", ret, GetLastError());
13476
13477     /* Unregister hotkey again */
13478     SetLastError(0xdeadbeef);
13479     ret = UnregisterHotKey(test_window, 5);
13480     ok(ret == FALSE, "expected FALSE, got %i\n", ret);
13481     ok(GetLastError() == ERROR_HOTKEY_NOT_REGISTERED || broken(GetLastError() == 0xdeadbeef),
13482        "unexpected error %d\n", GetLastError());
13483
13484     /* Register thread hotkey */
13485     ret = RegisterHotKey(NULL, 5, MOD_WIN, hotkey_letter);
13486     ok(ret == TRUE, "expected TRUE, got %i, err=%d\n", ret, GetLastError());
13487
13488     /* Inject the appropriate key sequence */
13489     keybd_event(VK_LWIN, 0, 0, 0);
13490     while (PeekMessage(&msg, NULL, 0, 0, PM_REMOVE))
13491     {
13492         ok(msg.hwnd != NULL, "unexpected thread message %x\n", msg.message);
13493         DispatchMessage(&msg);
13494     }
13495     ok_sequence(WmHotkeyPressLWIN, "thread hotkey press LWIN", FALSE);
13496
13497     keybd_event(hotkey_letter, 0, 0, 0);
13498     while (PeekMessage(&msg, NULL, 0, 0, PM_REMOVE))
13499     {
13500         if (msg.message == WM_HOTKEY)
13501         {
13502             struct recvd_message message;
13503             ok(msg.hwnd == NULL, "unexpected hwnd %p\n", msg.hwnd);
13504             ok(msg.lParam == MAKELPARAM(MOD_WIN, hotkey_letter), "unexpected WM_HOTKEY lparam %lx\n", msg.lParam);
13505             message.message = msg.message;
13506             message.flags = sent|wparam|lparam;
13507             message.wParam = msg.wParam;
13508             message.lParam = msg.lParam;
13509             message.descr = "test_hotkey thread message";
13510             add_message(&message);
13511         }
13512         else
13513             ok(msg.hwnd != NULL, "unexpected thread message %x\n", msg.message);
13514         DispatchMessage(&msg);
13515     }
13516     ok_sequence(WmHotkeyPress, "thread hotkey press", FALSE);
13517
13518     keybd_event(hotkey_letter, 0, KEYEVENTF_KEYUP, 0);
13519     while (PeekMessage(&msg, NULL, 0, 0, PM_REMOVE))
13520     {
13521         ok(msg.hwnd != NULL, "unexpected thread message %x\n", msg.message);
13522         DispatchMessage(&msg);
13523     }
13524     ok_sequence(WmHotkeyRelease, "thread hotkey release", TRUE);
13525
13526     keybd_event(VK_LWIN, 0, KEYEVENTF_KEYUP, 0);
13527     while (PeekMessage(&msg, NULL, 0, 0, PM_REMOVE))
13528     {
13529         ok(msg.hwnd != NULL, "unexpected thread message %x\n", msg.message);
13530         DispatchMessage(&msg);
13531     }
13532     ok_sequence(WmHotkeyReleaseLWIN, "thread hotkey release LWIN", FALSE);
13533
13534     /* Unregister thread hotkey */
13535     ret = UnregisterHotKey(NULL, 5);
13536     ok(ret == TRUE, "expected TRUE, got %i, err=%d\n", ret, GetLastError());
13537
13538     if (hKBD_hook) UnhookWindowsHookEx(hKBD_hook);
13539     hKBD_hook = NULL;
13540
13541 end:
13542     UnregisterHotKey(NULL, 5);
13543     UnregisterHotKey(test_window, 5);
13544     DestroyWindow(test_window);
13545     flush_sequence();
13546 }
13547
13548
13549 static const struct message WmSetFocus_1[] = {
13550     { HCBT_SETFOCUS, hook }, /* child */
13551     { HCBT_ACTIVATE, hook }, /* parent */
13552     { WM_QUERYNEWPALETTE, sent|wparam|lparam|parent|optional, 0, 0 },
13553     { WM_WINDOWPOSCHANGING, sent|parent, 0, SWP_NOSIZE|SWP_NOMOVE },
13554     { WM_ACTIVATEAPP, sent|wparam|parent, 1 },
13555     { WM_NCACTIVATE, sent|parent },
13556     { WM_GETTEXT, sent|defwinproc|parent|optional },
13557     { WM_GETTEXT, sent|defwinproc|parent|optional },
13558     { WM_ACTIVATE, sent|wparam|parent, 1 },
13559     { HCBT_SETFOCUS, hook }, /* parent */
13560     { WM_SETFOCUS, sent|defwinproc|parent },
13561     { WM_KILLFOCUS, sent|parent },
13562     { WM_SETFOCUS, sent },
13563     { 0 }
13564 };
13565 static const struct message WmSetFocus_2[] = {
13566     { HCBT_SETFOCUS, hook }, /* parent */
13567     { WM_KILLFOCUS, sent },
13568     { WM_SETFOCUS, sent|parent },
13569     { 0 }
13570 };
13571 static const struct message WmSetFocus_3[] = {
13572     { HCBT_SETFOCUS, hook }, /* child */
13573     { 0 }
13574 };
13575 static const struct message WmSetFocus_4[] = {
13576     { 0 }
13577 };
13578
13579 static void test_SetFocus(void)
13580 {
13581     HWND parent, old_parent, child, old_focus, old_active;
13582     MSG msg;
13583     struct wnd_event wnd_event;
13584     HANDLE hthread;
13585     DWORD ret, tid;
13586
13587     wnd_event.start_event = CreateEvent(NULL, 0, 0, NULL);
13588     ok(wnd_event.start_event != 0, "CreateEvent error %d\n", GetLastError());
13589     hthread = CreateThread(NULL, 0, thread_proc, &wnd_event, 0, &tid);
13590     ok(hthread != 0, "CreateThread error %d\n", GetLastError());
13591     ret = WaitForSingleObject(wnd_event.start_event, INFINITE);
13592     ok(ret == WAIT_OBJECT_0, "WaitForSingleObject failed\n");
13593     CloseHandle(wnd_event.start_event);
13594
13595     parent = CreateWindowEx(0, "TestParentClass", NULL, WS_OVERLAPPEDWINDOW,
13596                             0, 0, 0, 0, 0, 0, 0, NULL);
13597     ok(parent != 0, "failed to create parent window\n");
13598     child = CreateWindowEx(0, "TestWindowClass", NULL, WS_CHILD,
13599                            0, 0, 0, 0, parent, 0, 0, NULL);
13600     ok(child != 0, "failed to create child window\n");
13601
13602     trace("parent %p, child %p, thread window %p\n", parent, child, wnd_event.hwnd);
13603
13604     SetFocus(0);
13605     SetActiveWindow(0);
13606
13607     flush_events();
13608     flush_sequence();
13609
13610     ok(GetActiveWindow() == 0, "expected active 0, got %p\n", GetActiveWindow());
13611     ok(GetFocus() == 0, "expected focus 0, got %p\n", GetFocus());
13612
13613     log_all_parent_messages++;
13614
13615     old_focus = SetFocus(child);
13616     while (PeekMessage(&msg, NULL, 0, 0, PM_REMOVE)) DispatchMessage(&msg);
13617     ok_sequence(WmSetFocus_1, "SetFocus on a child window", TRUE);
13618     ok(old_focus == parent, "expected old focus %p, got %p\n", parent, old_focus);
13619     ok(GetActiveWindow() == parent, "expected active %p, got %p\n", parent, GetActiveWindow());
13620     ok(GetFocus() == child, "expected focus %p, got %p\n", child, GetFocus());
13621
13622     old_focus = SetFocus(parent);
13623     while (PeekMessage(&msg, NULL, 0, 0, PM_REMOVE)) DispatchMessage(&msg);
13624     ok_sequence(WmSetFocus_2, "SetFocus on a parent window", FALSE);
13625     ok(old_focus == child, "expected old focus %p, got %p\n", child, old_focus);
13626     ok(GetActiveWindow() == parent, "expected active %p, got %p\n", parent, GetActiveWindow());
13627     ok(GetFocus() == parent, "expected focus %p, got %p\n", parent, GetFocus());
13628
13629     SetLastError(0xdeadbeef);
13630     old_focus = SetFocus((HWND)0xdeadbeef);
13631     ok(GetLastError() == ERROR_INVALID_WINDOW_HANDLE || broken(GetLastError() == 0xdeadbeef),
13632        "expected ERROR_INVALID_WINDOW_HANDLE, got %d\n", GetLastError());
13633     while (PeekMessage(&msg, NULL, 0, 0, PM_REMOVE)) DispatchMessage(&msg);
13634     ok_sequence(WmEmptySeq, "SetFocus on an invalid window", FALSE);
13635     ok(old_focus == 0, "expected old focus 0, got %p\n", old_focus);
13636     ok(GetActiveWindow() == parent, "expected active %p, got %p\n", parent, GetActiveWindow());
13637     ok(GetFocus() == parent, "expected focus %p, got %p\n", parent, GetFocus());
13638
13639     SetLastError(0xdeadbeef);
13640     old_focus = SetFocus(GetDesktopWindow());
13641     ok(GetLastError() == ERROR_ACCESS_DENIED /* Vista+ */ ||
13642        broken(GetLastError() == 0xdeadbeef), "expected ERROR_ACCESS_DENIED, got %d\n", GetLastError());
13643     while (PeekMessage(&msg, NULL, 0, 0, PM_REMOVE)) DispatchMessage(&msg);
13644     ok_sequence(WmEmptySeq, "SetFocus on a desktop window", TRUE);
13645     ok(old_focus == 0, "expected old focus 0, got %p\n", old_focus);
13646     ok(GetActiveWindow() == parent, "expected active %p, got %p\n", parent, GetActiveWindow());
13647     ok(GetFocus() == parent, "expected focus %p, got %p\n", parent, GetFocus());
13648
13649     SetLastError(0xdeadbeef);
13650     old_focus = SetFocus(wnd_event.hwnd);
13651     ok(GetLastError() == ERROR_ACCESS_DENIED /* Vista+ */ ||
13652        broken(GetLastError() == 0xdeadbeef), "expected ERROR_ACCESS_DENIED, got %d\n", GetLastError());
13653     while (PeekMessage(&msg, NULL, 0, 0, PM_REMOVE)) DispatchMessage(&msg);
13654     ok_sequence(WmEmptySeq, "SetFocus on another thread window", TRUE);
13655     ok(old_focus == 0, "expected old focus 0, got %p\n", old_focus);
13656     ok(GetActiveWindow() == parent, "expected active %p, got %p\n", parent, GetActiveWindow());
13657     ok(GetFocus() == parent, "expected focus %p, got %p\n", parent, GetFocus());
13658
13659     SetLastError(0xdeadbeef);
13660     old_active = SetActiveWindow((HWND)0xdeadbeef);
13661     ok(GetLastError() == ERROR_INVALID_WINDOW_HANDLE || broken(GetLastError() == 0xdeadbeef),
13662        "expected ERROR_INVALID_WINDOW_HANDLE, got %d\n", GetLastError());
13663     while (PeekMessage(&msg, NULL, 0, 0, PM_REMOVE)) DispatchMessage(&msg);
13664     ok_sequence(WmEmptySeq, "SetActiveWindow on an invalid window", FALSE);
13665     ok(old_active == 0, "expected old focus 0, got %p\n", old_active);
13666     ok(GetActiveWindow() == parent, "expected active %p, got %p\n", parent, GetActiveWindow());
13667     ok(GetFocus() == parent, "expected focus %p, got %p\n", parent, GetFocus());
13668
13669     SetLastError(0xdeadbeef);
13670     old_active = SetActiveWindow(GetDesktopWindow());
13671 todo_wine
13672     ok(GetLastError() == 0xdeadbeef, "expected 0xdeadbeef, got %d\n", GetLastError());
13673     while (PeekMessage(&msg, NULL, 0, 0, PM_REMOVE)) DispatchMessage(&msg);
13674     ok_sequence(WmEmptySeq, "SetActiveWindow on a desktop window", TRUE);
13675     ok(old_active == 0, "expected old focus 0, got %p\n", old_focus);
13676     ok(GetActiveWindow() == parent, "expected active %p, got %p\n", parent, GetActiveWindow());
13677     ok(GetFocus() == parent, "expected focus %p, got %p\n", parent, GetFocus());
13678
13679     SetLastError(0xdeadbeef);
13680     old_active = SetActiveWindow(wnd_event.hwnd);
13681 todo_wine
13682     ok(GetLastError() == 0xdeadbeef, "expected 0xdeadbeef, got %d\n", GetLastError());
13683     while (PeekMessage(&msg, NULL, 0, 0, PM_REMOVE)) DispatchMessage(&msg);
13684     ok_sequence(WmEmptySeq, "SetActiveWindow on another thread window", TRUE);
13685     ok(old_active == 0, "expected old focus 0, got %p\n", old_active);
13686     ok(GetActiveWindow() == parent, "expected active %p, got %p\n", parent, GetActiveWindow());
13687     ok(GetFocus() == parent, "expected focus %p, got %p\n", parent, GetFocus());
13688
13689     SetLastError(0xdeadbeef);
13690     ret = AttachThreadInput(GetCurrentThreadId(), tid, TRUE);
13691     ok(ret, "AttachThreadInput error %d\n", GetLastError());
13692
13693 todo_wine {
13694     ok(GetActiveWindow() == parent, "expected active %p, got %p\n", parent, GetActiveWindow());
13695     ok(GetFocus() == parent, "expected focus %p, got %p\n", parent, GetFocus());
13696 }
13697     flush_events();
13698     flush_sequence();
13699
13700     old_focus = SetFocus(wnd_event.hwnd);
13701     while (PeekMessage(&msg, NULL, 0, 0, PM_REMOVE)) DispatchMessage(&msg);
13702     ok(old_focus == wnd_event.hwnd, "expected old focus %p, got %p\n", wnd_event.hwnd, old_focus);
13703     ok(GetActiveWindow() == wnd_event.hwnd, "expected active %p, got %p\n", wnd_event.hwnd, GetActiveWindow());
13704     ok(GetFocus() == wnd_event.hwnd, "expected focus %p, got %p\n", wnd_event.hwnd, GetFocus());
13705
13706     old_focus = SetFocus(parent);
13707     while (PeekMessage(&msg, NULL, 0, 0, PM_REMOVE)) DispatchMessage(&msg);
13708     ok(old_focus == parent, "expected old focus %p, got %p\n", parent, old_focus);
13709     ok(GetActiveWindow() == parent, "expected active %p, got %p\n", parent, GetActiveWindow());
13710     ok(GetFocus() == parent, "expected focus %p, got %p\n", parent, GetFocus());
13711
13712     flush_events();
13713     flush_sequence();
13714
13715     old_active = SetActiveWindow(wnd_event.hwnd);
13716     while (PeekMessage(&msg, NULL, 0, 0, PM_REMOVE)) DispatchMessage(&msg);
13717     ok(old_active == parent, "expected old focus %p, got %p\n", parent, old_active);
13718     ok(GetActiveWindow() == wnd_event.hwnd, "expected active %p, got %p\n", wnd_event.hwnd, GetActiveWindow());
13719     ok(GetFocus() == wnd_event.hwnd, "expected focus %p, got %p\n", wnd_event.hwnd, GetFocus());
13720
13721     SetLastError(0xdeadbeef);
13722     ret = AttachThreadInput(GetCurrentThreadId(), tid, FALSE);
13723     ok(ret, "AttachThreadInput error %d\n", GetLastError());
13724
13725     ok(GetActiveWindow() == 0, "expected active 0, got %p\n", GetActiveWindow());
13726     ok(GetFocus() == 0, "expected focus 0, got %p\n", GetFocus());
13727
13728     old_parent = SetParent(child, GetDesktopWindow());
13729     ok(old_parent == parent, "expected old parent %p, got %p\n", parent, old_parent);
13730
13731     ok(GetActiveWindow() == 0, "expected active 0, got %p\n", GetActiveWindow());
13732     ok(GetFocus() == 0, "expected focus 0, got %p\n", GetFocus());
13733
13734     old_focus = SetFocus(parent);
13735     while (PeekMessage(&msg, NULL, 0, 0, PM_REMOVE)) DispatchMessage(&msg);
13736     ok(old_focus == parent, "expected old focus %p, got %p\n", parent, old_focus);
13737     ok(GetActiveWindow() == parent, "expected active %p, got %p\n", parent, GetActiveWindow());
13738     ok(GetFocus() == parent, "expected focus %p, got %p\n", parent, GetFocus());
13739
13740     flush_events();
13741     flush_sequence();
13742
13743     SetLastError(0xdeadbeef);
13744     old_focus = SetFocus(child);
13745 todo_wine
13746     ok(GetLastError() == ERROR_INVALID_PARAMETER /* Vista+ */ ||
13747        broken(GetLastError() == 0) /* XP */ ||
13748        broken(GetLastError() == 0xdeadbeef), "expected ERROR_INVALID_PARAMETER, got %d\n", GetLastError());
13749     while (PeekMessage(&msg, NULL, 0, 0, PM_REMOVE)) DispatchMessage(&msg);
13750     ok_sequence(WmSetFocus_3, "SetFocus on a child window", TRUE);
13751     ok(old_focus == 0, "expected old focus 0, got %p\n", old_focus);
13752     ok(GetActiveWindow() == parent, "expected active %p, got %p\n", parent, GetActiveWindow());
13753     ok(GetFocus() == parent, "expected focus %p, got %p\n", parent, GetFocus());
13754
13755     SetLastError(0xdeadbeef);
13756     old_active = SetActiveWindow(child);
13757     ok(GetLastError() == 0xdeadbeef, "expected 0xdeadbeef, got %d\n", GetLastError());
13758     while (PeekMessage(&msg, NULL, 0, 0, PM_REMOVE)) DispatchMessage(&msg);
13759     ok_sequence(WmEmptySeq, "SetActiveWindow on a child window", FALSE);
13760     ok(old_active == parent, "expected old active %p, got %p\n", parent, old_active);
13761     ok(GetActiveWindow() == parent, "expected active %p, got %p\n", parent, GetActiveWindow());
13762     ok(GetFocus() == parent, "expected focus %p, got %p\n", parent, GetFocus());
13763
13764     log_all_parent_messages--;
13765
13766     DestroyWindow(child);
13767     DestroyWindow(parent);
13768
13769     ret = PostMessage(wnd_event.hwnd, WM_QUIT, 0, 0);
13770     ok(ret, "PostMessage(WM_QUIT) error %d\n", GetLastError());
13771     ret = WaitForSingleObject(hthread, INFINITE);
13772     ok(ret == WAIT_OBJECT_0, "WaitForSingleObject failed\n");
13773     CloseHandle(hthread);
13774 }
13775
13776 static const struct message WmSetLayeredStyle[] = {
13777     { WM_STYLECHANGING, sent },
13778     { WM_STYLECHANGED, sent },
13779     { WM_GETTEXT, sent|defwinproc|optional },
13780     { 0 }
13781 };
13782
13783 static const struct message WmSetLayeredStyle2[] = {
13784     { WM_STYLECHANGING, sent },
13785     { WM_STYLECHANGED, sent },
13786     { WM_WINDOWPOSCHANGING, sent|optional|wparam|defwinproc, SWP_FRAMECHANGED|SWP_NOACTIVATE|SWP_NOSIZE|SWP_NOMOVE|SWP_NOCLIENTSIZE|SWP_NOCLIENTMOVE },
13787     { WM_NCCALCSIZE, sent|optional|wparam|defwinproc, 1 },
13788     { WM_WINDOWPOSCHANGED, sent|optional|wparam|defwinproc, SWP_FRAMECHANGED|SWP_NOACTIVATE|SWP_NOSIZE|SWP_NOMOVE },
13789     { WM_MOVE, sent|optional|defwinproc|wparam, 0 },
13790     { WM_SIZE, sent|optional|defwinproc|wparam, SIZE_RESTORED },
13791     { 0 }
13792 };
13793
13794 struct layered_window_info
13795 {
13796     HWND   hwnd;
13797     HDC    hdc;
13798     SIZE   size;
13799     HANDLE event;
13800     BOOL   ret;
13801 };
13802
13803 static DWORD CALLBACK update_layered_proc( void *param )
13804 {
13805     struct layered_window_info *info = param;
13806     POINT src = { 0, 0 };
13807
13808     info->ret = pUpdateLayeredWindow( info->hwnd, 0, NULL, &info->size,
13809                                       info->hdc, &src, 0, NULL, ULW_OPAQUE );
13810     ok( info->ret, "failed\n");
13811     SetEvent( info->event );
13812     return 0;
13813 }
13814
13815 static void test_layered_window(void)
13816 {
13817     HWND hwnd;
13818     HDC hdc;
13819     HBITMAP bmp;
13820     BOOL ret;
13821     SIZE size;
13822     POINT pos, src;
13823     RECT rect, client;
13824     HANDLE thread;
13825     DWORD tid;
13826     struct layered_window_info info;
13827
13828     if (!pUpdateLayeredWindow)
13829     {
13830         win_skip( "UpdateLayeredWindow not supported\n" );
13831         return;
13832     }
13833
13834     hdc = CreateCompatibleDC( 0 );
13835     bmp = CreateCompatibleBitmap( hdc, 300, 300 );
13836     SelectObject( hdc, bmp );
13837
13838     hwnd = CreateWindowExA(0, "TestWindowClass", NULL, WS_CAPTION | WS_THICKFRAME | WS_SYSMENU,
13839                            100, 100, 300, 300, 0, 0, 0, NULL);
13840     ok( hwnd != 0, "failed to create window\n" );
13841     ShowWindow( hwnd, SW_SHOWNORMAL );
13842     UpdateWindow( hwnd );
13843     flush_events();
13844     flush_sequence();
13845
13846     GetWindowRect( hwnd, &rect );
13847     GetClientRect( hwnd, &client );
13848     ok( client.right < rect.right - rect.left, "wrong client area\n" );
13849     ok( client.bottom < rect.bottom - rect.top, "wrong client area\n" );
13850
13851     src.x = src.y = 0;
13852     pos.x = pos.y = 300;
13853     size.cx = size.cy = 250;
13854     ret = pUpdateLayeredWindow( hwnd, 0, &pos, &size, hdc, &src, 0, NULL, ULW_OPAQUE );
13855     ok( !ret, "UpdateLayeredWindow should fail on non-layered window\n" );
13856     ok( GetLastError() == ERROR_INVALID_PARAMETER, "wrong error %u\n", GetLastError() );
13857     SetWindowLong( hwnd, GWL_EXSTYLE, GetWindowLong(hwnd, GWL_EXSTYLE) | WS_EX_LAYERED );
13858     ok_sequence( WmSetLayeredStyle, "WmSetLayeredStyle", FALSE );
13859
13860     ret = pUpdateLayeredWindow( hwnd, 0, &pos, &size, hdc, &src, 0, NULL, ULW_OPAQUE );
13861     ok( ret, "UpdateLayeredWindow failed err %u\n", GetLastError() );
13862     ok_sequence( WmEmptySeq, "UpdateLayeredWindow", FALSE );
13863     GetWindowRect( hwnd, &rect );
13864     ok( rect.left == 300 && rect.top == 300 && rect.right == 550 && rect.bottom == 550,
13865         "wrong window rect %d,%d,%d,%d\n", rect.left, rect.top, rect.right, rect.bottom );
13866     GetClientRect( hwnd, &rect );
13867     ok( rect.right == client.right - 50 && rect.bottom == client.bottom - 50,
13868         "wrong client rect %d,%d,%d,%d\n", rect.left, rect.top, rect.right, rect.bottom );
13869
13870     size.cx = 150;
13871     pos.y = 200;
13872     ret = pUpdateLayeredWindow( hwnd, 0, &pos, &size, hdc, &src, 0, NULL, ULW_OPAQUE );
13873     ok( ret, "UpdateLayeredWindow failed err %u\n", GetLastError() );
13874     ok_sequence( WmEmptySeq, "UpdateLayeredWindow", FALSE );
13875     GetWindowRect( hwnd, &rect );
13876     ok( rect.left == 300 && rect.top == 200 && rect.right == 450 && rect.bottom == 450,
13877         "wrong window rect %d,%d,%d,%d\n", rect.left, rect.top, rect.right, rect.bottom );
13878     GetClientRect( hwnd, &rect );
13879     ok( rect.right == client.right - 150 && rect.bottom == client.bottom - 50,
13880         "wrong client rect %d,%d,%d,%d\n", rect.left, rect.top, rect.right, rect.bottom );
13881
13882     SetWindowLong( hwnd, GWL_STYLE,
13883                    GetWindowLong(hwnd, GWL_STYLE) & ~(WS_CAPTION | WS_THICKFRAME | WS_SYSMENU) );
13884     ok_sequence( WmSetLayeredStyle2, "WmSetLayeredStyle2", FALSE );
13885
13886     size.cx = 200;
13887     pos.x = 200;
13888     ret = pUpdateLayeredWindow( hwnd, 0, &pos, &size, hdc, &src, 0, NULL, ULW_OPAQUE );
13889     ok( ret, "UpdateLayeredWindow failed err %u\n", GetLastError() );
13890     ok_sequence( WmEmptySeq, "UpdateLayeredWindow", FALSE );
13891     GetWindowRect( hwnd, &rect );
13892     ok( rect.left == 200 && rect.top == 200 && rect.right == 400 && rect.bottom == 450,
13893         "wrong window rect %d,%d,%d,%d\n", rect.left, rect.top, rect.right, rect.bottom );
13894     GetClientRect( hwnd, &rect );
13895     ok( (rect.right == 200 && rect.bottom == 250) ||
13896         broken(rect.right == client.right - 100 && rect.bottom == client.bottom - 50),
13897         "wrong client rect %d,%d,%d,%d\n", rect.left, rect.top, rect.right, rect.bottom );
13898
13899     size.cx = 0;
13900     ret = pUpdateLayeredWindow( hwnd, 0, &pos, &size, hdc, &src, 0, NULL, ULW_OPAQUE );
13901     ok( !ret, "UpdateLayeredWindow should fail on non-layered window\n" );
13902     ok( GetLastError() == ERROR_INVALID_PARAMETER || broken(ERROR_MR_MID_NOT_FOUND) /* win7 */,
13903         "wrong error %u\n", GetLastError() );
13904     size.cx = 1;
13905     size.cy = -1;
13906     ret = pUpdateLayeredWindow( hwnd, 0, &pos, &size, hdc, &src, 0, NULL, ULW_OPAQUE );
13907     ok( !ret, "UpdateLayeredWindow should fail on non-layered window\n" );
13908     ok( GetLastError() == ERROR_INVALID_PARAMETER, "wrong error %u\n", GetLastError() );
13909
13910     SetWindowLong( hwnd, GWL_EXSTYLE, GetWindowLong(hwnd, GWL_EXSTYLE) & ~WS_EX_LAYERED );
13911     ok_sequence( WmSetLayeredStyle, "WmSetLayeredStyle", FALSE );
13912     GetWindowRect( hwnd, &rect );
13913     ok( rect.left == 200 && rect.top == 200 && rect.right == 400 && rect.bottom == 450,
13914         "wrong window rect %d,%d,%d,%d\n", rect.left, rect.top, rect.right, rect.bottom );
13915     GetClientRect( hwnd, &rect );
13916     ok( (rect.right == 200 && rect.bottom == 250) ||
13917         broken(rect.right == client.right - 100 && rect.bottom == client.bottom - 50),
13918         "wrong client rect %d,%d,%d,%d\n", rect.left, rect.top, rect.right, rect.bottom );
13919
13920     SetWindowLong( hwnd, GWL_EXSTYLE, GetWindowLong(hwnd, GWL_EXSTYLE) | WS_EX_LAYERED );
13921     info.hwnd = hwnd;
13922     info.hdc = hdc;
13923     info.size.cx = 250;
13924     info.size.cy = 300;
13925     info.event = CreateEventA( NULL, TRUE, FALSE, NULL );
13926     info.ret = FALSE;
13927     thread = CreateThread( NULL, 0, update_layered_proc, &info, 0, &tid );
13928     ok( WaitForSingleObject( info.event, 1000 ) == 0, "wait failed\n" );
13929     ok( info.ret, "UpdateLayeredWindow failed in other thread\n" );
13930     WaitForSingleObject( thread, 1000 );
13931     CloseHandle( thread );
13932     GetWindowRect( hwnd, &rect );
13933     ok( rect.left == 200 && rect.top == 200 && rect.right == 450 && rect.bottom == 500,
13934         "wrong window rect %d,%d,%d,%d\n", rect.left, rect.top, rect.right, rect.bottom );
13935     GetClientRect( hwnd, &rect );
13936     ok( (rect.right == 250 && rect.bottom == 300) ||
13937         broken(rect.right == client.right - 50 && rect.bottom == client.bottom),
13938         "wrong client rect %d,%d,%d,%d\n", rect.left, rect.top, rect.right, rect.bottom );
13939
13940     DestroyWindow( hwnd );
13941     DeleteDC( hdc );
13942     DeleteObject( bmp );
13943 }
13944
13945 START_TEST(msg)
13946 {
13947     char **test_argv;
13948     BOOL ret;
13949     BOOL (WINAPI *pIsWinEventHookInstalled)(DWORD)= 0;/*GetProcAddress(user32, "IsWinEventHookInstalled");*/
13950     HMODULE hModuleImm32;
13951     BOOL (WINAPI *pImmDisableIME)(DWORD);
13952
13953     int argc = winetest_get_mainargs( &test_argv );
13954     if (argc >= 3)
13955     {
13956         unsigned int arg;
13957         /* Child process. */
13958         sscanf (test_argv[2], "%d", (unsigned int *) &arg);
13959         do_wait_idle_child( arg );
13960         return;
13961     }
13962
13963     init_procs();
13964
13965     hModuleImm32 = LoadLibrary("imm32.dll");
13966     if (hModuleImm32) {
13967         pImmDisableIME = (void *)GetProcAddress(hModuleImm32, "ImmDisableIME");
13968         if (pImmDisableIME)
13969             pImmDisableIME(0);
13970     }
13971     pImmDisableIME = NULL;
13972     FreeLibrary(hModuleImm32);
13973
13974     if (!RegisterWindowClasses()) assert(0);
13975
13976     if (pSetWinEventHook)
13977     {
13978         hEvent_hook = pSetWinEventHook(EVENT_MIN, EVENT_MAX,
13979                                        GetModuleHandleA(0), win_event_proc,
13980                                        0, GetCurrentThreadId(),
13981                                        WINEVENT_INCONTEXT);
13982         if (pIsWinEventHookInstalled && hEvent_hook)
13983         {
13984             UINT event;
13985             for (event = EVENT_MIN; event <= EVENT_MAX; event++)
13986                 ok(pIsWinEventHookInstalled(event), "IsWinEventHookInstalled(%u) failed\n", event);
13987         }
13988     }
13989     if (!hEvent_hook) win_skip( "no win event hook support\n" );
13990
13991     cbt_hook_thread_id = GetCurrentThreadId();
13992     hCBT_hook = SetWindowsHookExA(WH_CBT, cbt_hook_proc, 0, GetCurrentThreadId());
13993     if (!hCBT_hook) win_skip( "cannot set global hook, will skip hook tests\n" );
13994
13995     test_winevents();
13996
13997     /* Fix message sequences before removing 4 lines below */
13998 #if 1
13999     if (pUnhookWinEvent && hEvent_hook)
14000     {
14001         ret = pUnhookWinEvent(hEvent_hook);
14002         ok( ret, "UnhookWinEvent error %d\n", GetLastError());
14003         pUnhookWinEvent = 0;
14004     }
14005     hEvent_hook = 0;
14006 #endif
14007
14008     test_SetFocus();
14009     test_SetParent();
14010     test_PostMessage();
14011     test_ShowWindow();
14012     test_PeekMessage();
14013     test_PeekMessage2();
14014     test_WaitForInputIdle( test_argv[0] );
14015     test_scrollwindowex();
14016     test_messages();
14017     test_setwindowpos();
14018     test_showwindow();
14019     invisible_parent_tests();
14020     test_mdi_messages();
14021     test_button_messages();
14022     test_static_messages();
14023     test_listbox_messages();
14024     test_combobox_messages();
14025     test_wmime_keydown_message();
14026     test_paint_messages();
14027     test_interthread_messages();
14028     test_message_conversion();
14029     test_accelerators();
14030     test_timers();
14031     test_timers_no_wnd();
14032     if (hCBT_hook) test_set_hook();
14033     test_DestroyWindow();
14034     test_DispatchMessage();
14035     test_SendMessageTimeout();
14036     test_edit_messages();
14037     test_quit_message();
14038     test_SetActiveWindow();
14039
14040     if (!pTrackMouseEvent)
14041         win_skip("TrackMouseEvent is not available\n");
14042     else
14043         test_TrackMouseEvent();
14044
14045     test_SetWindowRgn();
14046     test_sys_menu();
14047     test_dialog_messages();
14048     test_EndDialog();
14049     test_nullCallback();
14050     test_dbcs_wm_char();
14051     test_menu_messages();
14052     test_paintingloop();
14053     test_defwinproc();
14054     test_clipboard_viewers();
14055     test_keyflags();
14056     test_hotkey();
14057     test_layered_window();
14058     /* keep it the last test, under Windows it tends to break the tests
14059      * which rely on active/foreground windows being correct.
14060      */
14061     test_SetForegroundWindow();
14062
14063     UnhookWindowsHookEx(hCBT_hook);
14064     if (pUnhookWinEvent && hEvent_hook)
14065     {
14066         ret = pUnhookWinEvent(hEvent_hook);
14067         ok( ret, "UnhookWinEvent error %d\n", GetLastError());
14068         SetLastError(0xdeadbeef);
14069         ok(!pUnhookWinEvent(hEvent_hook), "UnhookWinEvent succeeded\n");
14070         ok(GetLastError() == ERROR_INVALID_HANDLE || /* Win2k */
14071            GetLastError() == 0xdeadbeef, /* Win9x */
14072            "unexpected error %d\n", GetLastError());
14073     }
14074 }