fltlib: Add a stub dll.
[wine] / dlls / comctl32 / tests / header.c
1 /* Unit test suite for header control.
2  *
3  * Copyright 2005 Vijay Kiran Kamuju
4  * Copyright 2007 Shanren Zhou
5  *
6  * This library is free software; you can redistribute it and/or
7  * modify it under the terms of the GNU Lesser General Public
8  * License as published by the Free Software Foundation; either
9  * version 2.1 of the License, or (at your option) any later version.
10  *
11  * This library is distributed in the hope that it will be useful,
12  * but WITHOUT ANY WARRANTY; without even the implied warranty of
13  * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the GNU
14  * Lesser General Public License for more details.
15  *
16  * You should have received a copy of the GNU Lesser General Public
17  * License along with this library; if not, write to the Free Software
18  * Foundation, Inc., 51 Franklin St, Fifth Floor, Boston, MA 02110-1301, USA
19  */
20
21
22 #include <windows.h>
23 #include <commctrl.h>
24 #include <assert.h>
25
26 #include "wine/test.h"
27 #include "v6util.h"
28 #include "msg.h"
29
30 typedef struct tagEXPECTEDNOTIFY
31 {
32     INT iCode;
33     BOOL fUnicode;
34     HDITEMA hdItem;
35 } EXPECTEDNOTIFY;
36
37 typedef LRESULT (*CUSTOMDRAWPROC)(int n, NMCUSTOMDRAW *nm);
38
39 static CUSTOMDRAWPROC g_CustomDrawProc;
40 static int g_CustomDrawCount;
41 static DRAWITEMSTRUCT g_DrawItem;
42 static BOOL g_DrawItemReceived;
43 static DWORD g_customheight;
44
45 static EXPECTEDNOTIFY expectedNotify[10];
46 static INT nExpectedNotify = 0;
47 static INT nReceivedNotify = 0;
48 static INT unexpectedNotify[10];
49 static INT nUnexpectedNotify = 0;
50
51 static HWND hHeaderParentWnd;
52 static HWND hWndHeader;
53 #define MAX_CHARS 100
54
55 #define compare(val, exp, fmt)  ok((val) == (exp), #val " value: " fmt ", expected: " fmt "\n", (val), (exp))
56
57 #define expect(expected, got) ok(expected == got, "expected %d, got %d\n", expected,got)
58
59 #define NUM_MSG_SEQUENCES    2
60 #define PARENT_SEQ_INDEX     0
61 #define HEADER_SEQ_INDEX     1
62
63 static struct msg_sequence *sequences[NUM_MSG_SEQUENCES];
64
65 static const struct message create_parent_wnd_seq[] = {
66     { WM_GETMINMAXINFO, sent },
67     { WM_NCCREATE, sent },
68     { WM_NCCALCSIZE, sent|wparam, 0 },
69     { WM_CREATE, sent },
70     { 0 }
71 };
72
73 static const struct message add_header_to_parent_seq_interactive[] = {
74     { WM_NOTIFYFORMAT, sent|lparam, 0, NF_QUERY },
75     { WM_QUERYUISTATE, sent },
76     { WM_PARENTNOTIFY, sent|wparam, 1 },
77     { WM_SHOWWINDOW, sent|wparam, 1 },
78     { WM_WINDOWPOSCHANGING, sent|wparam, 0 },
79     { WM_WINDOWPOSCHANGING, sent|wparam, 0 },
80     { WM_ACTIVATEAPP, sent|wparam, 1 },
81     { WM_NCACTIVATE, sent|wparam, 1 },
82     { WM_ACTIVATE, sent|wparam, 1 },
83     { WM_IME_SETCONTEXT, sent|defwinproc|wparam, 1 },
84     { WM_IME_NOTIFY, sent|defwinproc|wparam, 2 },
85     { WM_SETFOCUS, sent|defwinproc|wparam, 0 },
86     { WM_WINDOWPOSCHANGED, sent|wparam, 0 },
87     { WM_SIZE, sent|wparam, 0 },
88     { WM_MOVE, sent|wparam, 0 },
89     { 0 }
90 };
91
92 static const struct message add_header_to_parent_seq[] = {
93     { WM_NOTIFYFORMAT, sent|lparam, 0, NF_QUERY },
94     { WM_QUERYUISTATE, sent|optional },
95     { WM_PARENTNOTIFY, sent },
96     { 0 }
97 };
98
99 static const struct message insertItem_seq[] = {
100     { HDM_INSERTITEM, sent|wparam, 0 },
101     { HDM_INSERTITEM, sent|wparam, 1 },
102     { HDM_INSERTITEM, sent|wparam, 2 },
103     { HDM_INSERTITEM, sent|wparam, 3 },
104     { 0 }
105 };
106
107 static const struct message getItem_seq[] = {
108     { HDM_GETITEM, sent|wparam, 3 },
109     { HDM_GETITEM, sent|wparam, 0 },
110     { 0 }
111 };
112
113
114 static const struct message deleteItem_getItemCount_seq[] = {
115     { HDM_DELETEITEM, sent|wparam, 3 },
116     { HDM_GETITEMCOUNT, sent },
117     { HDM_DELETEITEM, sent|wparam, 3 },
118     { HDM_GETITEMCOUNT, sent },
119     { HDM_DELETEITEM, sent|wparam, 2 },
120     { HDM_GETITEMCOUNT, sent },
121     { 0 }
122 };
123
124 static const struct message orderArray_seq[] = {
125     { HDM_SETORDERARRAY, sent|wparam, 2 },
126     { HDM_GETORDERARRAY, sent|wparam, 2 },
127     { 0 }
128 };
129
130 static const struct message setItem_seq[] = {
131     { HDM_SETITEM, sent|wparam, 0 },
132     { HDM_SETITEM, sent|wparam, 1 },
133     { 0 }
134 };
135
136 static const struct message getItemRect_seq[] = {
137     { HDM_GETITEMRECT, sent|wparam, 1 },
138     { HDM_GETITEMRECT, sent|wparam, 0 },
139     { HDM_GETITEMRECT, sent|wparam, 10 },
140     { 0 }
141 };
142
143 static const struct message layout_seq[] = {
144     { HDM_LAYOUT, sent },
145     { 0 }
146 };
147
148 static const struct message orderToIndex_seq[] = {
149     { HDM_ORDERTOINDEX, sent|wparam, 1 },
150     { 0 }
151 };
152
153 static const struct message hittest_seq[] = {
154     { HDM_HITTEST, sent },
155     { HDM_HITTEST, sent },
156     { HDM_HITTEST, sent },
157     { 0 }
158 };
159
160 static const struct message setHotDivider_seq_interactive[] = {
161     { HDM_SETHOTDIVIDER, sent|wparam, TRUE },
162     { WM_PAINT, sent|defwinproc},
163     { WM_NCPAINT, sent|defwinproc},
164     { WM_ERASEBKGND, sent|defwinproc},
165     { HDM_SETHOTDIVIDER, sent|wparam|lparam, FALSE, 100 },
166     { WM_PAINT, sent|defwinproc},
167     { HDM_SETHOTDIVIDER, sent|wparam|lparam, FALSE, 1},
168     { WM_PAINT, sent|defwinproc},
169     { 0 }
170 };
171
172 static const struct message setHotDivider_seq_noninteractive[] = {
173     { HDM_SETHOTDIVIDER, sent|wparam, TRUE },
174     { HDM_SETHOTDIVIDER, sent|wparam|lparam, FALSE, 100 },
175     { HDM_SETHOTDIVIDER, sent|wparam|lparam, FALSE, 1},
176     { 0 }
177 };
178
179 static const struct message imageMessages_seq[] = {
180     { HDM_SETIMAGELIST, sent },
181     { HDM_GETIMAGELIST, sent },
182     { HDM_CREATEDRAGIMAGE, sent },
183     { 0 }
184 };
185
186 static const struct message filterMessages_seq_interactive[] = {
187     { HDM_SETFILTERCHANGETIMEOUT, sent|wparam|lparam, 1, 100 },
188     { HDM_CLEARFILTER, sent|wparam|lparam, 0, 1 },
189     { HDM_EDITFILTER,  sent|wparam|lparam, 1, 0 },
190     { WM_PARENTNOTIFY, sent|wparam|defwinproc, WM_CREATE },
191     { WM_CTLCOLOREDIT, sent|defwinproc },
192     { WM_COMMAND, sent|defwinproc },
193     { 0 }
194 };
195
196 static const struct message filterMessages_seq_noninteractive[] = {
197     { HDM_SETFILTERCHANGETIMEOUT, sent|wparam|lparam, 1, 100 },
198     { HDM_CLEARFILTER, sent|wparam|lparam, 0, 1 },
199     { HDM_EDITFILTER,  sent|wparam|lparam, 1, 0 },
200     { WM_PARENTNOTIFY, sent|wparam|defwinproc|optional, WM_CREATE },
201     { WM_COMMAND, sent|defwinproc|optional },
202     { 0 }
203 };
204
205 static const struct message unicodeformatMessages_seq[] = {
206     { HDM_SETUNICODEFORMAT, sent|wparam, TRUE },
207     { HDM_GETUNICODEFORMAT, sent },
208     { 0 }
209 };
210
211 static const struct message bitmapmarginMessages_seq[] = {
212     { HDM_GETBITMAPMARGIN, sent },
213     { 0 }
214 };
215
216
217 static void expect_notify(INT iCode, BOOL fUnicode, HDITEMA *lpItem)
218 {
219     assert(nExpectedNotify < 10);
220     expectedNotify[nExpectedNotify].iCode = iCode;
221     expectedNotify[nExpectedNotify].fUnicode = fUnicode;
222     expectedNotify[nExpectedNotify].hdItem = *lpItem;
223     nExpectedNotify++;
224 }
225
226 static void dont_expect_notify(INT iCode)
227 {
228     assert(nUnexpectedNotify < 10);
229     unexpectedNotify[nUnexpectedNotify++] = iCode;
230 }
231
232 static BOOL notifies_received(void)
233 {
234     BOOL fRet = (nExpectedNotify == nReceivedNotify);
235     nExpectedNotify = nReceivedNotify = 0;
236     nUnexpectedNotify = 0;
237     return fRet;
238 }
239
240 static LONG addItem(HWND hdex, int idx, LPSTR text)
241 {
242     HDITEMA hdItem;
243     hdItem.mask       = HDI_TEXT | HDI_WIDTH;
244     hdItem.cxy        = 100;
245     hdItem.pszText    = text;
246     hdItem.cchTextMax = 0;
247     return (LONG)SendMessage(hdex, HDM_INSERTITEMA, (WPARAM)idx, (LPARAM)&hdItem);
248 }
249
250 static LONG setItem(HWND hdex, int idx, LPSTR text, BOOL fCheckNotifies)
251 {
252     LONG ret;
253     HDITEMA hdexItem;
254     hdexItem.mask       = HDI_TEXT;
255     hdexItem.pszText    = text;
256     hdexItem.cchTextMax = 0;
257     if (fCheckNotifies)
258     {
259         expect_notify(HDN_ITEMCHANGINGA, FALSE, &hdexItem);
260         expect_notify(HDN_ITEMCHANGEDA, FALSE, &hdexItem);
261     }
262     ret = (LONG)SendMessage(hdex, HDM_SETITEMA, (WPARAM)idx, (LPARAM)&hdexItem);
263     if (fCheckNotifies)
264         ok(notifies_received(), "setItem(): not all expected notifies were received\n");
265     return ret;
266 }
267
268 static LONG setItemUnicodeNotify(HWND hdex, int idx, LPSTR text, LPWSTR wText)
269 {
270     LONG ret;
271     HDITEMA hdexItem;
272     HDITEMW hdexNotify;
273     hdexItem.mask       = HDI_TEXT;
274     hdexItem.pszText    = text;
275     hdexItem.cchTextMax = 0;
276     
277     hdexNotify.mask    = HDI_TEXT;
278     hdexNotify.pszText = wText;
279     
280     expect_notify(HDN_ITEMCHANGINGW, TRUE, (HDITEMA*)&hdexNotify);
281     expect_notify(HDN_ITEMCHANGEDW, TRUE, (HDITEMA*)&hdexNotify);
282     ret = (LONG)SendMessage(hdex, HDM_SETITEMA, (WPARAM)idx, (LPARAM)&hdexItem);
283     ok(notifies_received(), "setItemUnicodeNotify(): not all expected notifies were received\n");
284     return ret;
285 }
286
287 static LONG delItem(HWND hdex, int idx)
288 {
289     return (LONG)SendMessage(hdex, HDM_DELETEITEM, (WPARAM)idx, 0);
290 }
291
292 static LONG getItemCount(HWND hdex)
293 {
294     return (LONG)SendMessage(hdex, HDM_GETITEMCOUNT, 0, 0);
295 }
296
297 static LONG getItem(HWND hdex, int idx, LPSTR textBuffer)
298 {
299     HDITEMA hdItem;
300     hdItem.mask         = HDI_TEXT;
301     hdItem.pszText      = textBuffer;
302     hdItem.cchTextMax   = MAX_CHARS;
303     return (LONG)SendMessage(hdex, HDM_GETITEMA, (WPARAM)idx, (LPARAM)&hdItem);
304 }
305
306 static void addReadDelItem(HWND hdex, HDITEMA *phdiCreate, int maskRead, HDITEMA *phdiRead)
307 {
308     ok(SendMessage(hdex, HDM_INSERTITEMA, 0, (LPARAM)phdiCreate)!=-1, "Adding item failed\n");
309     ZeroMemory(phdiRead, sizeof(HDITEMA));
310     phdiRead->mask = maskRead;
311     ok(SendMessage(hdex, HDM_GETITEMA, 0, (LPARAM)phdiRead)!=0, "Getting item data failed\n");
312     ok(SendMessage(hdex, HDM_DELETEITEM, 0, 0)!=0, "Deleting item failed\n");
313 }
314
315 static HWND create_header_control (void)
316 {
317     HWND handle;
318     HDLAYOUT hlayout;
319     RECT rectwin;
320     WINDOWPOS winpos;
321
322     handle = CreateWindowEx(0, WC_HEADER, NULL,
323                             WS_CHILD|WS_BORDER|WS_VISIBLE|HDS_BUTTONS|HDS_HORZ,
324                             0, 0, 0, 0,
325                             hHeaderParentWnd, NULL, NULL, NULL);
326     assert(handle);
327
328     if (winetest_interactive)
329         ShowWindow (hHeaderParentWnd, SW_SHOW);
330
331     GetClientRect(hHeaderParentWnd,&rectwin);
332     hlayout.prc = &rectwin;
333     hlayout.pwpos = &winpos;
334     SendMessageA(handle,HDM_LAYOUT,0,(LPARAM) &hlayout);
335     SetWindowPos(handle, winpos.hwndInsertAfter, winpos.x, winpos.y, 
336                  winpos.cx, winpos.cy, 0);
337
338     return handle;
339 }
340
341 static void compare_items(INT iCode, HDITEMA *hdi1, HDITEMA *hdi2, BOOL fUnicode)
342 {
343     ok(hdi1->mask == hdi2->mask, "Notify %d mask mismatch (%08x != %08x)\n", iCode, hdi1->mask, hdi2->mask);
344     if (hdi1->mask & HDI_WIDTH)
345     {
346         ok(hdi1->cxy == hdi2->cxy, "Notify %d cxy mismatch (%08x != %08x)\n", iCode, hdi1->cxy, hdi2->cxy);
347     }
348     if (hdi1->mask & HDI_TEXT)
349     {
350         if (hdi1->pszText == LPSTR_TEXTCALLBACKA)
351         {
352             ok(hdi1->pszText == LPSTR_TEXTCALLBACKA, "Notify %d - only one item is LPSTR_TEXTCALLBACK\n", iCode);
353         }
354         else
355         if (fUnicode)
356         {
357             char buf1[260];
358             char buf2[260];
359             WideCharToMultiByte(CP_ACP, 0, (LPCWSTR)hdi1->pszText, -1, buf1, 260, NULL, NULL);
360             WideCharToMultiByte(CP_ACP, 0, (LPCWSTR)hdi2->pszText, -1, buf2, 260, NULL, NULL);
361             ok(lstrcmpW((LPWSTR)hdi1->pszText, (LPWSTR)hdi2->pszText)==0,
362                 "Notify %d text mismatch (L\"%s\" vs L\"%s\")\n",
363                     iCode, buf1, buf2);
364         }
365         else
366         {
367             ok(strcmp(hdi1->pszText, hdi2->pszText)==0,
368                 "Notify %d text mismatch (\"%s\" vs \"%s\")\n",
369                     iCode, hdi1->pszText, hdi2->pszText);
370             }
371     }
372 }
373
374 static char pszFirstItem[]      = "First Item";
375 static char pszSecondItem[]     = "Second Item";
376 static char pszThirdItem[]      = "Third Item";
377 static char pszFourthItem[]     = "Fourth Item";
378 static char pszReplaceItem[]    = "Replace Item";
379 static char pszOutOfRangeItem[] = "Out Of Range Item";
380
381 static char *str_items[] =
382     {pszFirstItem, pszSecondItem, pszThirdItem, pszFourthItem, pszReplaceItem, pszOutOfRangeItem};
383     
384 static char pszUniTestA[]  = "TST";
385 static WCHAR pszUniTestW[] = {'T','S','T',0};
386
387
388 #define TEST_GET_ITEM(i,c)\
389 {   res = getItem(hWndHeader, i, buffer);\
390     ok(res != 0, "Getting item[%d] using valid index failed unexpectedly (%d)\n", i, res);\
391     ok(strcmp(str_items[c], buffer) == 0, "Getting item[%d] returned \"%s\" expecting \"%s\"\n", i, buffer, str_items[c]);\
392 }
393
394 #define TEST_GET_ITEMCOUNT(i)\
395 {   res = getItemCount(hWndHeader);\
396     ok(res == i, "Got Item Count as %d\n", res);\
397 }
398
399 struct subclass_info
400 {
401     WNDPROC oldproc;
402 };
403
404 static LRESULT WINAPI header_subclass_proc(HWND hwnd, UINT message, WPARAM wParam, LPARAM lParam)
405 {
406     struct subclass_info *info = (struct subclass_info *)GetWindowLongPtrA(hwnd, GWLP_USERDATA);
407     static LONG defwndproc_counter = 0;
408     LRESULT ret;
409     struct message msg;
410
411     trace("header: %p, %04x, %08lx, %08lx\n", hwnd, message, wParam, lParam);
412     msg.message = message;
413     msg.flags = sent|wparam|lparam;
414     if (defwndproc_counter) msg.flags |= defwinproc;
415     msg.wParam = wParam;
416     msg.lParam = lParam;
417     add_message(sequences, HEADER_SEQ_INDEX, &msg);
418
419     defwndproc_counter++;
420     ret = CallWindowProcA(info->oldproc, hwnd, message, wParam, lParam);
421     defwndproc_counter--;
422
423     return ret;
424 }
425
426 static LRESULT WINAPI parent_wnd_proc(HWND hwnd, UINT message, WPARAM wParam, LPARAM lParam)
427 {
428     static LONG defwndproc_counter = 0;
429     LRESULT ret;
430     struct message msg;
431
432     /* do not log painting messages */
433     if (message != WM_PAINT &&
434         message != WM_ERASEBKGND &&
435         message != WM_NCPAINT &&
436         message != WM_NCHITTEST &&
437         message != WM_GETTEXT &&
438         message != WM_GETICON &&
439         message != WM_DEVICECHANGE)
440
441     {
442         trace("parent: %p, %04x, %08lx, %08lx\n", hwnd, message, wParam, lParam);
443
444         msg.message = message;
445         msg.flags = sent|wparam|lparam;
446         if (defwndproc_counter) msg.flags |= defwinproc;
447         msg.wParam = wParam;
448         msg.lParam = lParam;
449         add_message(sequences, PARENT_SEQ_INDEX, &msg);
450    }
451
452     defwndproc_counter++;
453     ret = DefWindowProcA(hwnd, message, wParam, lParam);
454     defwndproc_counter--;
455
456     return ret;
457 }
458
459 static BOOL register_parent_wnd_class(void)
460 {
461     WNDCLASSA cls;
462
463     cls.style = 0;
464     cls.lpfnWndProc = parent_wnd_proc;
465     cls.cbClsExtra = 0;
466     cls.cbWndExtra = 0;
467     cls.hInstance = GetModuleHandleA(NULL);
468     cls.hIcon = 0;
469     cls.hCursor = LoadCursorA(0, IDC_ARROW);
470     cls.hbrBackground = GetStockObject(WHITE_BRUSH);
471     cls.lpszMenuName = NULL;
472     cls.lpszClassName = "Header test parent class";
473     return RegisterClassA(&cls);
474 }
475
476 static HWND create_custom_parent_window(void)
477 {
478     if (!register_parent_wnd_class())
479         return NULL;
480
481     return CreateWindowExA(0, "Header test parent class", "Header Message Sequence Testing",
482                            WS_OVERLAPPEDWINDOW, CW_USEDEFAULT, CW_USEDEFAULT,
483                            672+2*GetSystemMetrics(SM_CXSIZEFRAME),
484                            226+GetSystemMetrics(SM_CYCAPTION)+2*GetSystemMetrics(SM_CYSIZEFRAME),
485                            NULL, NULL, GetModuleHandleA(NULL), 0);
486 }
487
488 static HWND create_custom_header_control(HWND hParent, BOOL preloadHeaderItems)
489 {
490     struct subclass_info *info;
491     HWND childHandle;
492     HDLAYOUT hlayout;
493     RECT rectwin;
494     WINDOWPOS winpos;
495     int retVal;
496     int loopcnt;
497     static char firstHeaderItem[] = "Name";
498     static char secondHeaderItem[] = "Size";
499     static char *items[] = {secondHeaderItem, firstHeaderItem};
500     HDITEM hdItem;
501     hdItem.mask = HDI_TEXT | HDI_WIDTH | HDI_FORMAT;
502     hdItem.fmt = HDF_LEFT;
503     hdItem.cxy = 80;
504     hdItem.cchTextMax = 260;
505
506
507     flush_sequences(sequences, NUM_MSG_SEQUENCES);
508     info = HeapAlloc(GetProcessHeap(), 0, sizeof(struct subclass_info));
509     if (!info)
510          return NULL;
511
512     childHandle = CreateWindowEx(0, WC_HEADER, NULL,
513                            WS_CHILD|WS_BORDER|WS_VISIBLE|HDS_BUTTONS|HDS_HORZ,
514                            0, 0, 0, 0,
515                            hParent, NULL, NULL, NULL);
516     assert(childHandle);
517     if (preloadHeaderItems)
518     {
519          for ( loopcnt = 0 ; loopcnt < 2 ; loopcnt++ )
520          {
521              hdItem.pszText = items[loopcnt];
522              retVal = SendMessage(childHandle, HDM_INSERTITEM, loopcnt, (LPARAM) &hdItem);
523              ok(retVal == loopcnt, "Adding item %d failed with return value %d\n", ( loopcnt + 1 ), retVal);
524           }
525     }
526
527     if (winetest_interactive)
528        ShowWindow (hParent, SW_SHOW);
529
530     GetClientRect(hParent,&rectwin);
531     hlayout.prc = &rectwin;
532     hlayout.pwpos = &winpos;
533     SendMessageA(childHandle,HDM_LAYOUT,0,(LPARAM) &hlayout);
534     SetWindowPos(childHandle, winpos.hwndInsertAfter, winpos.x, winpos.y,
535                  winpos.cx, winpos.cy, 0);
536
537     info->oldproc = (WNDPROC)SetWindowLongPtrA(childHandle, GWLP_WNDPROC,
538                                                (LONG_PTR)header_subclass_proc);
539     SetWindowLongPtrA(childHandle, GWLP_USERDATA, (LONG_PTR)info);
540     return childHandle;
541 }
542
543 static void check_auto_format(void)
544 {
545     HDITEMA hdiCreate;
546     HDITEMA hdiRead;
547     static CHAR text[] = "Test";
548     ZeroMemory(&hdiCreate, sizeof(HDITEMA));
549
550     /* Windows implicitly sets some format bits in INSERTITEM */
551
552     /* HDF_STRING is automatically set and cleared for no text */
553     hdiCreate.mask = HDI_TEXT|HDI_WIDTH|HDI_FORMAT;
554     hdiCreate.pszText = text;
555     hdiCreate.cxy = 100;
556     hdiCreate.fmt=HDF_CENTER;
557     addReadDelItem(hWndHeader, &hdiCreate, HDI_FORMAT, &hdiRead);
558     ok(hdiRead.fmt == (HDF_STRING|HDF_CENTER), "HDF_STRING not set automatically (fmt=%x)\n", hdiRead.fmt);
559
560     hdiCreate.mask = HDI_WIDTH|HDI_FORMAT;
561     hdiCreate.pszText = text;
562     hdiCreate.fmt = HDF_CENTER|HDF_STRING;
563     addReadDelItem(hWndHeader, &hdiCreate, HDI_FORMAT, &hdiRead);
564     ok(hdiRead.fmt == (HDF_CENTER), "HDF_STRING should be automatically cleared (fmt=%x)\n", hdiRead.fmt);
565
566     /* HDF_BITMAP is automatically set and cleared for a NULL bitmap or no bitmap */
567     hdiCreate.mask = HDI_BITMAP|HDI_WIDTH|HDI_FORMAT;
568     hdiCreate.hbm = CreateBitmap(16, 16, 1, 8, NULL);
569     hdiCreate.fmt = HDF_CENTER;
570     addReadDelItem(hWndHeader, &hdiCreate, HDI_FORMAT, &hdiRead);
571     ok(hdiRead.fmt == (HDF_BITMAP|HDF_CENTER), "HDF_BITMAP not set automatically (fmt=%x)\n", hdiRead.fmt);
572     DeleteObject(hdiCreate.hbm);
573
574     hdiCreate.hbm = NULL;
575     hdiCreate.fmt = HDF_CENTER|HDF_BITMAP;
576     addReadDelItem(hWndHeader, &hdiCreate, HDI_FORMAT, &hdiRead);
577     ok(hdiRead.fmt == HDF_CENTER, "HDF_BITMAP not cleared automatically for NULL bitmap (fmt=%x)\n", hdiRead.fmt);
578
579     hdiCreate.mask = HDI_WIDTH|HDI_FORMAT;
580     hdiCreate.fmt = HDF_CENTER|HDF_BITMAP;
581     addReadDelItem(hWndHeader, &hdiCreate, HDI_FORMAT, &hdiRead);
582     ok(hdiRead.fmt == HDF_CENTER, "HDF_BITMAP not cleared automatically for no bitmap (fmt=%x)\n", hdiRead.fmt);
583
584     /* HDF_IMAGE is automatically set but not cleared */
585     hdiCreate.mask = HDI_IMAGE|HDI_WIDTH|HDI_FORMAT;
586     hdiCreate.iImage = 17;
587     addReadDelItem(hWndHeader, &hdiCreate, HDI_FORMAT, &hdiRead);
588     ok(hdiRead.fmt == (HDF_IMAGE|HDF_CENTER), "HDF_IMAGE not set automatically (fmt=%x)\n", hdiRead.fmt);
589
590     hdiCreate.mask = HDI_WIDTH|HDI_FORMAT;
591     hdiCreate.fmt = HDF_CENTER|HDF_IMAGE;
592     hdiCreate.iImage = 0;
593     addReadDelItem(hWndHeader, &hdiCreate, HDI_FORMAT, &hdiRead);
594     ok(hdiRead.fmt == (HDF_CENTER|HDF_IMAGE), "HDF_IMAGE shouldn't be cleared automatically (fmt=%x)\n", hdiRead.fmt);
595 }
596
597 static void check_auto_fields(void)
598 {
599     HDITEMA hdiCreate;
600     HDITEMA hdiRead;
601     static CHAR text[] = "Test";
602     LONG res;
603
604     /* Windows stores the format, width, lparam even if they are not in the item's mask */
605     ZeroMemory(&hdiCreate, sizeof(HDITEMA));
606     hdiCreate.mask = HDI_TEXT;
607     hdiCreate.cxy = 100;
608     hdiCreate.pszText = text;
609     addReadDelItem(hWndHeader, &hdiCreate, HDI_WIDTH, &hdiRead);
610     TEST_GET_ITEMCOUNT(6);
611     ok(hdiRead.cxy == hdiCreate.cxy, "cxy should be automatically set\n");
612
613     ZeroMemory(&hdiCreate, sizeof(HDITEMA));
614     hdiCreate.mask = HDI_TEXT;
615     hdiCreate.pszText = text;
616     hdiCreate.lParam = 0x12345678;
617     addReadDelItem(hWndHeader, &hdiCreate, HDI_LPARAM, &hdiRead);
618     TEST_GET_ITEMCOUNT(6);
619     ok(hdiRead.lParam == hdiCreate.lParam, "lParam should be automatically set\n");
620
621     ZeroMemory(&hdiCreate, sizeof(HDITEMA));
622     hdiCreate.mask = HDI_TEXT;
623     hdiCreate.pszText = text;
624     hdiCreate.fmt = HDF_STRING|HDF_CENTER;
625     addReadDelItem(hWndHeader, &hdiCreate, HDI_FORMAT, &hdiRead);
626     TEST_GET_ITEMCOUNT(6);
627     ok(hdiRead.fmt == hdiCreate.fmt, "fmt should be automatically set\n");
628
629     /* others fields are not set */
630     ZeroMemory(&hdiCreate, sizeof(HDITEMA));
631     hdiCreate.mask = HDI_TEXT;
632     hdiCreate.pszText = text;
633     hdiCreate.hbm = CreateBitmap(16, 16, 1, 8, NULL);
634     addReadDelItem(hWndHeader, &hdiCreate, HDI_BITMAP, &hdiRead);
635     TEST_GET_ITEMCOUNT(6);
636     ok(hdiRead.hbm == NULL, "hbm should not be automatically set\n");
637     DeleteObject(hdiCreate.hbm);
638
639     ZeroMemory(&hdiCreate, sizeof(HDITEMA));
640     hdiCreate.mask = HDI_IMAGE;
641     hdiCreate.iImage = 17;
642     hdiCreate.pszText = text;
643     addReadDelItem(hWndHeader, &hdiCreate, HDI_TEXT, &hdiRead);
644     TEST_GET_ITEMCOUNT(6);
645     ok(hdiRead.pszText==NULL, "pszText shouldn't be automatically set\n");
646
647     /* field from comctl >4.0 not tested as the system probably won't touch them */
648 }
649
650 static void check_mask(void)
651 {
652     HDITEMA hdi;
653     static CHAR text[] = "ABC";
654     LRESULT ret;
655
656     /* don't create items if the mask is zero */
657     ZeroMemory(&hdi, sizeof(hdi));
658     hdi.mask = 0;
659     hdi.cxy = 200;
660     hdi.pszText = text;
661     hdi.fmt = 0;
662     hdi.iOrder = 0;
663     hdi.lParam = 17;
664     hdi.cchTextMax = 260;
665     ret = SendMessage(hWndHeader, HDM_INSERTITEM, 0, (LPARAM)&hdi);
666     ok(ret == -1, "Creating an item with a zero mask should have failed\n");
667     if (ret != -1) SendMessage(hWndHeader, HDM_DELETEITEM, 0, 0);
668
669     /* with a non-zero mask creation will succeed */
670     ZeroMemory(&hdi, sizeof(hdi));
671     hdi.mask = HDI_LPARAM;
672     ret = SendMessage(hWndHeader, HDM_INSERTITEM, 0, (LPARAM)&hdi);
673     ok(ret != -1, "Adding item with non-zero mask failed\n");
674     if (ret != -1)
675         SendMessage(hWndHeader, HDM_DELETEITEM, 0, 0);
676
677     /* in SETITEM if the mask contains a unknown bit, it is ignored */
678     ZeroMemory(&hdi, sizeof(hdi));
679     hdi.mask = 0x08000000 | HDI_LPARAM | HDI_IMAGE;
680     hdi.lParam = 133;
681     hdi.iImage = 17;
682     ret = SendMessage(hWndHeader, HDM_INSERTITEM, 0, (LPARAM)&hdi);
683     ok(ret != -1, "Adding item failed\n");
684
685     if (ret != -1)
686     {
687         /* check result */
688         ZeroMemory(&hdi, sizeof(hdi));
689         hdi.mask = HDI_LPARAM | HDI_IMAGE;
690         SendMessage(hWndHeader, HDM_GETITEM, 0, (LPARAM)&hdi);
691         ok(hdi.lParam == 133, "comctl32 4.0 field not set\n");
692         ok(hdi.iImage == 17, "comctl32 >4.0 field not set\n");
693
694         /* but in GETITEM if an unknown bit is set, comctl32 uses only version 4.0 fields */
695         ZeroMemory(&hdi, sizeof(hdi));
696         hdi.mask = 0x08000000 | HDI_LPARAM | HDI_IMAGE;
697         SendMessage(hWndHeader, HDM_GETITEM, 0, (LPARAM)&hdi);
698         ok(hdi.lParam == 133, "comctl32 4.0 field not read\n");
699         ok(hdi.iImage == 0, "comctl32 >4.0 field shouldn't be read\n");
700
701         SendMessage(hWndHeader, HDM_DELETEITEM, 0, 0);
702     }
703 }
704
705 static void test_header_control (void)
706 {
707     LONG res;
708     static char buffer[MAX_CHARS];
709     int i;
710
711     hWndHeader = create_header_control ();
712
713     for (i = 3; i >= 0; i--)
714     {
715         TEST_GET_ITEMCOUNT(3-i);
716         res = addItem(hWndHeader, 0, str_items[i]);
717         ok(res == 0, "Adding simple item failed (%d)\n", res);
718     }
719
720     TEST_GET_ITEMCOUNT(4);
721     res = addItem(hWndHeader, 99, str_items[i+1]);
722     ok(res != -1, "Adding Out of Range item should fail with -1 got (%d)\n", res);
723     TEST_GET_ITEMCOUNT(5);
724     res = addItem(hWndHeader, 5, str_items[i+1]);
725     ok(res != -1, "Adding Out of Range item should fail with -1 got (%d)\n", res);
726     TEST_GET_ITEMCOUNT(6);
727
728     for (i = 0; i < 4; i++) { TEST_GET_ITEM(i,i); TEST_GET_ITEMCOUNT(6); }
729
730     res=getItem(hWndHeader, 99, buffer);
731     ok(res == 0, "Getting Out of Range item should fail with 0 (%d), got %s\n", res,buffer);
732     res=getItem(hWndHeader, 5, buffer);
733     ok(res == 1, "Getting Out of Range item should fail with 1 (%d), got %s\n", res,buffer);
734     res=getItem(hWndHeader, -2, buffer);
735     ok(res == 0, "Getting Out of Range item should fail with 0 (%d), got %s\n", res,buffer);
736
737     if (winetest_interactive)
738     {
739         UpdateWindow(hHeaderParentWnd);
740         UpdateWindow(hWndHeader);
741     }
742
743     TEST_GET_ITEMCOUNT(6);
744     res=setItem(hWndHeader, 99, str_items[5], FALSE);
745     ok(res == 0, "Setting Out of Range item should fail with 0 (%d)\n", res);
746     res=setItem(hWndHeader, 5, str_items[5], TRUE);
747     ok(res == 1, "Setting Out of Range item should fail with 1 (%d)\n", res);
748     res=setItem(hWndHeader, -2, str_items[5], FALSE);
749     ok(res == 0, "Setting Out of Range item should fail with 0 (%d)\n", res);
750     TEST_GET_ITEMCOUNT(6);
751
752     for (i = 0; i < 4; i++)
753     {
754         res = setItem(hWndHeader, i, str_items[4], TRUE);
755         ok(res != 0, "Setting %d item failed (%d)\n", i+1, res);
756         TEST_GET_ITEM(i, 4);
757         TEST_GET_ITEMCOUNT(6);
758     }
759     
760     SendMessageA(hWndHeader, HDM_SETUNICODEFORMAT, (WPARAM)TRUE, 0);
761     setItemUnicodeNotify(hWndHeader, 3, pszUniTestA, pszUniTestW);
762     SendMessageA(hWndHeader, WM_NOTIFYFORMAT, (WPARAM)hHeaderParentWnd, (LPARAM)NF_REQUERY);
763     setItem(hWndHeader, 3, str_items[4], TRUE);
764     
765     dont_expect_notify(HDN_GETDISPINFOA);
766     dont_expect_notify(HDN_GETDISPINFOW);
767     addItem(hWndHeader, 0, LPSTR_TEXTCALLBACKA);
768     setItem(hWndHeader, 0, str_items[4], TRUE);
769     /* unexpected notifies cleared by notifies_received in setItem */
770     dont_expect_notify(HDN_GETDISPINFOA);
771     dont_expect_notify(HDN_GETDISPINFOW);
772     setItem(hWndHeader, 0, LPSTR_TEXTCALLBACKA, TRUE);
773     /* unexpected notifies cleared by notifies_received in setItem */
774     delItem(hWndHeader, 0);
775
776     check_auto_format();
777     TEST_GET_ITEMCOUNT(6);
778     check_auto_fields();
779     TEST_GET_ITEMCOUNT(6);
780     check_mask();
781     TEST_GET_ITEMCOUNT(6);
782
783     res = delItem(hWndHeader, 5);
784     ok(res == 1, "Deleting Out of Range item should fail with 1 (%d)\n", res);
785     res = delItem(hWndHeader, -2);
786     ok(res == 0, "Deleting Out of Range item should fail with 0 (%d)\n", res);
787     TEST_GET_ITEMCOUNT(5);
788
789     res = delItem(hWndHeader, 3);
790     ok(res != 0, "Deleting using out of range index failed (%d)\n", res);
791     TEST_GET_ITEMCOUNT(4);
792     res = delItem(hWndHeader, 0);
793     ok(res != 0, "Deleting using out of range index failed (%d)\n", res);
794     TEST_GET_ITEMCOUNT(3);
795     res = delItem(hWndHeader, 0);
796     ok(res != 0, "Deleting using out of range index failed (%d)\n", res);
797     TEST_GET_ITEMCOUNT(2);
798     res = delItem(hWndHeader, 0);
799     ok(res != 0, "Deleting using out of range index failed (%d)\n", res);
800     TEST_GET_ITEMCOUNT(1);
801
802     DestroyWindow(hWndHeader);
803 }
804
805 static void test_hdm_getitemrect(HWND hParent)
806 {
807
808     HWND hChild;
809     RECT rect;
810     int retVal;
811
812     flush_sequences(sequences, NUM_MSG_SEQUENCES);
813     hChild = create_custom_header_control(hParent, TRUE);
814     ok_sequence(sequences, PARENT_SEQ_INDEX, add_header_to_parent_seq,
815                                     "adder header control to parent", FALSE);
816
817     retVal = SendMessage(hChild, HDM_GETITEMRECT, 1, (LPARAM) &rect);
818     ok(retVal == TRUE, "Getting item rect should TRUE, got %d\n", retVal);
819     /* check bounding rectangle information of 2nd header item */
820     expect(80, rect.left);
821     expect(0, rect.top);
822     expect(160, rect.right);
823     expect(g_customheight, rect.bottom);
824
825     retVal = SendMessage(hChild, HDM_GETITEMRECT, 0, (LPARAM) &rect);
826
827     ok(retVal == TRUE, "Getting item rect should TRUE, got %d\n", retVal);
828     /* check bounding rectangle information of 1st header item */
829     expect(0, rect.left);
830     expect(0, rect.top);
831
832     expect(80, rect.right);
833     expect(g_customheight, rect.bottom);
834
835     retVal = SendMessage(hChild, HDM_GETITEMRECT, 10, (LPARAM) &rect);
836     ok(retVal == 0, "Getting rect of nonexistent item should return 0, got %d\n", retVal);
837
838     ok_sequence(sequences, HEADER_SEQ_INDEX, getItemRect_seq, "getItemRect sequence testing", FALSE);
839     DestroyWindow(hChild);
840 }
841
842 static void test_hdm_layout(HWND hParent)
843 {
844     HWND hChild;
845     int retVal;
846     RECT rect;
847     HDLAYOUT hdLayout;
848     WINDOWPOS windowPos;
849     hdLayout.prc = &rect;
850     hdLayout.pwpos = &windowPos;
851
852     flush_sequences(sequences, NUM_MSG_SEQUENCES);
853     hChild = create_custom_header_control(hParent, TRUE);
854     ok_sequence(sequences, PARENT_SEQ_INDEX, add_header_to_parent_seq,
855                                     "adder header control to parent", FALSE);
856
857     flush_sequences(sequences, NUM_MSG_SEQUENCES);
858     retVal = SendMessage(hChild, HDM_LAYOUT, 0, (LPARAM) &hdLayout);
859     expect(TRUE, retVal);
860
861     ok_sequence(sequences, HEADER_SEQ_INDEX, layout_seq, "layout sequence testing", FALSE);
862
863     DestroyWindow(hChild);
864 }
865
866 static void test_hdm_ordertoindex(HWND hParent)
867 {
868     HWND hChild;
869     int retVal;
870
871     flush_sequences(sequences, NUM_MSG_SEQUENCES);
872     hChild = create_custom_header_control(hParent, TRUE);
873     ok_sequence(sequences, PARENT_SEQ_INDEX, add_header_to_parent_seq,
874                                     "adder header control to parent", FALSE);
875
876     flush_sequences(sequences, NUM_MSG_SEQUENCES);
877     retVal = SendMessage(hChild, HDM_ORDERTOINDEX, 1, 0);
878     expect(1, retVal);
879
880     ok_sequence(sequences, HEADER_SEQ_INDEX, orderToIndex_seq, "orderToIndex sequence testing", FALSE);
881     DestroyWindow(hChild);
882 }
883
884 static void test_hdm_hittest(HWND hParent)
885 {
886     HWND hChild;
887     int retVal;
888     POINT pt;
889     HDHITTESTINFO hdHitTestInfo;
890     const int firstItemRightBoundary = 80;
891     const int secondItemRightBoundary = 160;
892     const int bottomBoundary = g_customheight;
893
894     pt.x = firstItemRightBoundary - 1;
895     pt.y = bottomBoundary - 1;
896     hdHitTestInfo.pt = pt;
897
898
899     flush_sequences(sequences, NUM_MSG_SEQUENCES);
900     hChild = create_custom_header_control(hParent, TRUE);
901     ok_sequence(sequences, PARENT_SEQ_INDEX, add_header_to_parent_seq,
902                                     "adder header control to parent", FALSE);
903
904     flush_sequences(sequences, NUM_MSG_SEQUENCES);
905     retVal = SendMessage(hChild, HDM_HITTEST, 0, (LPARAM) &hdHitTestInfo);
906     expect(0, retVal);
907     expect(0, hdHitTestInfo.iItem);
908     expect(HHT_ONDIVIDER, hdHitTestInfo.flags);
909
910     pt.x = secondItemRightBoundary - 1;
911     pt.y = bottomBoundary - 1;
912     hdHitTestInfo.pt = pt;
913     retVal = SendMessage(hChild, HDM_HITTEST, 1, (LPARAM) &hdHitTestInfo);
914     expect(1, retVal);
915     expect(1, hdHitTestInfo.iItem);
916     expect(HHT_ONDIVIDER, hdHitTestInfo.flags);
917
918     pt.x = secondItemRightBoundary;
919     pt.y = bottomBoundary + 1;
920     hdHitTestInfo.pt = pt;
921     retVal = SendMessage(hChild, HDM_HITTEST, 0, (LPARAM) &hdHitTestInfo);
922     expect(-1, retVal);
923     expect(-1, hdHitTestInfo.iItem);
924     expect(HHT_BELOW, hdHitTestInfo.flags);
925
926     ok_sequence(sequences, HEADER_SEQ_INDEX, hittest_seq, "hittest sequence testing", FALSE);
927
928     DestroyWindow(hChild);
929 }
930
931 static void test_hdm_sethotdivider(HWND hParent)
932 {
933     HWND hChild;
934     int retVal;
935     /*  low word: x coordinate = 5
936      *  high word:  y coordinate = 5
937      */
938
939     flush_sequences(sequences, NUM_MSG_SEQUENCES);
940     hChild = create_custom_header_control(hParent, TRUE);
941     ok_sequence(sequences, PARENT_SEQ_INDEX, add_header_to_parent_seq,
942                                     "adder header control to parent", FALSE);
943
944     flush_sequences(sequences, NUM_MSG_SEQUENCES);
945     retVal = SendMessage(hChild, HDM_SETHOTDIVIDER, TRUE, MAKELPARAM(5, 5));
946     expect(0, retVal);
947
948     retVal = SendMessage(hChild, HDM_SETHOTDIVIDER, FALSE, 100);
949     expect(100, retVal);
950     retVal = SendMessage(hChild, HDM_SETHOTDIVIDER, FALSE, 1);
951     expect(1, retVal);
952     if (winetest_interactive)
953        ok_sequence(sequences, HEADER_SEQ_INDEX, setHotDivider_seq_interactive,
954                    "setHotDivider sequence testing", TRUE);
955     else
956        ok_sequence(sequences, HEADER_SEQ_INDEX, setHotDivider_seq_noninteractive,
957                    "setHotDivider sequence testing", FALSE);
958
959     DestroyWindow(hChild);
960 }
961
962 static void test_hdm_imageMessages(HWND hParent)
963 {
964     HIMAGELIST hImageList = ImageList_Create (4, 4, 0, 1, 0);
965     HIMAGELIST hImageListRetVal;
966     HWND hChild;
967
968     flush_sequences(sequences, NUM_MSG_SEQUENCES);
969     hChild = create_custom_header_control(hParent, TRUE);
970     ok_sequence(sequences, PARENT_SEQ_INDEX, add_header_to_parent_seq,
971                                     "adder header control to parent", FALSE);
972
973     flush_sequences(sequences, NUM_MSG_SEQUENCES);
974
975     hImageListRetVal = (HIMAGELIST) SendMessage(hChild, HDM_SETIMAGELIST, 0, (LPARAM) hImageList);
976     ok(hImageListRetVal == NULL, "Expected NULL, got %p\n", hImageListRetVal);
977
978     hImageListRetVal = (HIMAGELIST) SendMessage(hChild, HDM_GETIMAGELIST, 0, 0);
979     ok(hImageListRetVal != NULL, "Expected non-NULL handle, got %p\n", hImageListRetVal);
980
981     hImageListRetVal = (HIMAGELIST) SendMessage(hChild, HDM_CREATEDRAGIMAGE, 0, 0);
982     ok(hImageListRetVal != NULL, "Expected non-NULL handle, got %p\n", hImageListRetVal);
983
984     ok_sequence(sequences, HEADER_SEQ_INDEX, imageMessages_seq, "imageMessages sequence testing", FALSE);
985
986     DestroyWindow(hChild);
987 }
988
989 static void test_hdm_filterMessages(HWND hParent)
990 {
991     HWND hChild;
992     int retVal, timeout;
993
994     flush_sequences(sequences, NUM_MSG_SEQUENCES);
995     hChild = create_custom_header_control(hParent, TRUE);
996     assert(hChild);
997     ok_sequence(sequences, PARENT_SEQ_INDEX, add_header_to_parent_seq,
998                                     "adder header control to parent", FALSE);
999
1000     timeout = SendMessage(hChild, HDM_SETFILTERCHANGETIMEOUT, 1, 100);
1001     SendMessage(hChild, HDM_SETFILTERCHANGETIMEOUT, 1, timeout);
1002
1003     flush_sequences(sequences, NUM_MSG_SEQUENCES);
1004
1005     /* msdn incorrectly states that return value
1006      * is the index of the filter control being
1007      * modified. The sendMessage here should
1008      * return previous filter timeout value
1009      */
1010
1011     retVal = SendMessage(hChild, HDM_SETFILTERCHANGETIMEOUT, 1, 100);
1012     expect(timeout, retVal);
1013
1014     todo_wine
1015     {
1016         retVal = SendMessage(hChild, HDM_CLEARFILTER, 0, 1);
1017         if (retVal == 0)
1018             win_skip("HDM_CLEARFILTER needs 5.80\n");
1019         else
1020             expect(1, retVal);
1021
1022         retVal = SendMessage(hChild, HDM_EDITFILTER, 1, 0);
1023         if (retVal == 0)
1024             win_skip("HDM_EDITFILTER needs 5.80\n");
1025         else
1026             expect(1, retVal);
1027     }
1028     if (winetest_interactive)
1029          ok_sequence(sequences, HEADER_SEQ_INDEX, filterMessages_seq_interactive,
1030                      "filterMessages sequence testing", TRUE);
1031     else
1032          ok_sequence(sequences, HEADER_SEQ_INDEX, filterMessages_seq_noninteractive,
1033                      "filterMessages sequence testing", FALSE);
1034     /* Some Win9x versions don't send a WM_KILLFOCUS.
1035      * Set the focus explicitly to the parent to avoid a crash.
1036      */
1037     SetFocus(hParent);
1038     DestroyWindow(hChild);
1039
1040 }
1041
1042 static void test_hdm_unicodeformatMessages(HWND hParent)
1043 {
1044     HWND hChild;
1045     int retVal;
1046
1047     flush_sequences(sequences, NUM_MSG_SEQUENCES);
1048     hChild = create_custom_header_control(hParent, TRUE);
1049     ok_sequence(sequences, PARENT_SEQ_INDEX, add_header_to_parent_seq,
1050                                     "adder header control to parent", FALSE);
1051
1052     flush_sequences(sequences, NUM_MSG_SEQUENCES);
1053     retVal = SendMessage(hChild, HDM_SETUNICODEFORMAT, TRUE, 0);
1054     expect(0, retVal);
1055     retVal = SendMessage(hChild, HDM_GETUNICODEFORMAT, 0, 0);
1056     expect(1, retVal);
1057
1058     ok_sequence(sequences, HEADER_SEQ_INDEX, unicodeformatMessages_seq,
1059                      "unicodeformatMessages sequence testing", FALSE);
1060     DestroyWindow(hChild);
1061 }
1062
1063 static void test_hdm_bitmapmarginMessages(HWND hParent)
1064 {
1065     HWND hChild;
1066     int retVal;
1067
1068     flush_sequences(sequences, NUM_MSG_SEQUENCES);
1069     hChild = create_custom_header_control(hParent, TRUE);
1070     ok_sequence(sequences, PARENT_SEQ_INDEX, add_header_to_parent_seq,
1071                                     "adder header control to parent", FALSE);
1072
1073     flush_sequences(sequences, NUM_MSG_SEQUENCES);
1074     retVal = SendMessage(hChild, HDM_GETBITMAPMARGIN, 0, 0);
1075     if (retVal == 0)
1076         win_skip("HDM_GETBITMAPMARGIN needs 5.80\n");
1077     else
1078         expect(6, retVal);
1079
1080     ok_sequence(sequences, HEADER_SEQ_INDEX, bitmapmarginMessages_seq,
1081                       "bitmapmarginMessages sequence testing", FALSE);
1082     DestroyWindow(hChild);
1083 }
1084
1085 static void test_hdm_index_messages(HWND hParent)
1086 {
1087     HWND hChild;
1088     int retVal;
1089     int loopcnt;
1090     int strcmpResult;
1091     int iSize;
1092     static const int lpiarray[2] = {1, 0};
1093     static int lpiarrayReceived[2];
1094     static char firstHeaderItem[] = "Name";
1095     static char secondHeaderItem[] = "Size";
1096     static char thirdHeaderItem[] = "Type";
1097     static char fourthHeaderItem[] = "Date Modified";
1098     static char *items[] = {firstHeaderItem, secondHeaderItem, thirdHeaderItem, fourthHeaderItem};
1099     RECT rect;
1100     HDITEM hdItem;
1101     hdItem.mask = HDI_TEXT | HDI_WIDTH | HDI_FORMAT;
1102     hdItem.fmt = HDF_LEFT;
1103     hdItem.cxy = 80;
1104     hdItem.cchTextMax = 260;
1105
1106     flush_sequences(sequences, NUM_MSG_SEQUENCES);
1107     hChild = create_custom_header_control(hParent, FALSE);
1108     if (winetest_interactive)
1109          ok_sequence(sequences, PARENT_SEQ_INDEX, add_header_to_parent_seq_interactive,
1110                                               "adder header control to parent", TRUE);
1111     else
1112          ok_sequence(sequences, PARENT_SEQ_INDEX, add_header_to_parent_seq,
1113                                      "adder header control to parent", FALSE);
1114     flush_sequences(sequences, NUM_MSG_SEQUENCES);
1115     for ( loopcnt = 0 ; loopcnt < 4 ; loopcnt++ )
1116     {
1117       hdItem.pszText = items[loopcnt];
1118       retVal = SendMessage(hChild, HDM_INSERTITEM, loopcnt, (LPARAM) &hdItem);
1119       ok(retVal == loopcnt, "Adding item %d failed with return value %d\n", ( loopcnt + 1 ), retVal);
1120     }
1121     ok_sequence(sequences, HEADER_SEQ_INDEX, insertItem_seq, "insertItem sequence testing", FALSE);
1122
1123     flush_sequences(sequences, NUM_MSG_SEQUENCES);
1124
1125     retVal = SendMessage(hChild, HDM_DELETEITEM, 3, (LPARAM) &hdItem);
1126     ok(retVal == TRUE, "Deleting item 3 should return TRUE, got %d\n", retVal);
1127     retVal = SendMessage(hChild, HDM_GETITEMCOUNT, 0, 0);
1128     ok(retVal == 3, "Getting item count should return 3, got %d\n", retVal);
1129
1130     retVal = SendMessage(hChild, HDM_DELETEITEM, 3, (LPARAM) &hdItem);
1131     ok(retVal == FALSE, "Deleting already-deleted item should return FALSE, got %d\n", retVal);
1132     retVal = SendMessage(hChild, HDM_GETITEMCOUNT, 0, 0);
1133     ok(retVal == 3, "Getting item count should return 3, got %d\n", retVal);
1134
1135     retVal = SendMessage(hChild, HDM_DELETEITEM, 2, (LPARAM) &hdItem);
1136     ok(retVal == TRUE, "Deleting item 2 should return TRUE, got %d\n", retVal);
1137     retVal = SendMessage(hChild, HDM_GETITEMCOUNT, 0, 0);
1138     ok(retVal == 2, "Getting item count should return 2, got %d\n", retVal);
1139
1140     ok_sequence(sequences, HEADER_SEQ_INDEX, deleteItem_getItemCount_seq,
1141                          "deleteItem_getItemCount sequence testing", FALSE);
1142
1143     flush_sequences(sequences, NUM_MSG_SEQUENCES);
1144
1145     retVal = SendMessage(hChild, HDM_GETITEM, 3, (LPARAM) &hdItem);
1146     ok(retVal == FALSE, "Getting already-deleted item should return FALSE, got %d\n", retVal);
1147
1148     retVal = SendMessage(hChild, HDM_GETITEM, 0, (LPARAM) &hdItem);
1149     ok(retVal == TRUE, "Getting the 1st header item should return TRUE, got %d\n", retVal);
1150
1151     ok_sequence(sequences, HEADER_SEQ_INDEX, getItem_seq, "getItem sequence testing", FALSE);
1152
1153     /* check if the item is the right one */
1154     strcmpResult =  strcmp(hdItem.pszText, firstHeaderItem);
1155     expect(0, strcmpResult);
1156     expect(80, hdItem.cxy);
1157
1158     iSize = SendMessage(hChild, HDM_GETITEMCOUNT, 0, 0);
1159
1160     /* item should be updated just after accepting new array */
1161     ShowWindow(hChild, SW_HIDE);
1162     retVal = SendMessage(hChild, HDM_SETORDERARRAY, iSize, (LPARAM) lpiarray);
1163     expect(TRUE, retVal);
1164     rect.left = 0;
1165     retVal = SendMessage(hChild, HDM_GETITEMRECT, 0, (LPARAM) &rect);
1166     expect(TRUE, retVal);
1167     ok(rect.left != 0, "Expected updated rectangle\n");
1168
1169     flush_sequences(sequences, NUM_MSG_SEQUENCES);
1170
1171     retVal = SendMessage(hChild, HDM_SETORDERARRAY, iSize, (LPARAM) lpiarray);
1172     ok(retVal == TRUE, "Setting header items order should return TRUE, got %d\n", retVal);
1173
1174     retVal = SendMessage(hChild, HDM_GETORDERARRAY, iSize, (LPARAM) lpiarrayReceived);
1175     ok(retVal == TRUE, "Getting header items order should return TRUE, got %d\n", retVal);
1176
1177     ok_sequence(sequences, HEADER_SEQ_INDEX, orderArray_seq, "set_get_orderArray sequence testing", FALSE);
1178
1179     /* check if the array order is set correctly and the size of the array is correct. */
1180     expect(2, iSize);
1181     expect(lpiarray[0], lpiarrayReceived[0]);
1182     expect(lpiarray[1], lpiarrayReceived[1]);
1183
1184     hdItem.mask = HDI_FORMAT;
1185     hdItem.fmt = HDF_CENTER | HDF_STRING;
1186
1187     flush_sequences(sequences, NUM_MSG_SEQUENCES);
1188
1189     retVal = SendMessage(hChild, HDM_SETITEM, 0, (LPARAM) &hdItem);
1190     ok(retVal == TRUE, "Aligning 1st header item to center should return TRUE, got %d\n", retVal);
1191     hdItem.fmt = HDF_RIGHT | HDF_STRING;
1192     retVal = SendMessage(hChild, HDM_SETITEM, 1, (LPARAM) &hdItem);
1193     ok(retVal == TRUE, "Aligning 2nd header item to right should return TRUE, got %d\n", retVal);
1194
1195     ok_sequence(sequences, HEADER_SEQ_INDEX, setItem_seq, "setItem sequence testing", FALSE);
1196     DestroyWindow(hChild);
1197 }
1198
1199 static void test_hdf_fixedwidth(HWND hParent)
1200 {
1201     HWND hChild;
1202     HDITEM hdItem;
1203     DWORD ret;
1204     RECT rect;
1205     HDHITTESTINFO ht;
1206
1207     hChild = create_custom_header_control(hParent, FALSE);
1208
1209     hdItem.mask = HDI_WIDTH | HDI_FORMAT;
1210     hdItem.fmt = HDF_FIXEDWIDTH;
1211     hdItem.cxy = 80;
1212
1213     ret = SendMessage(hChild, HDM_INSERTITEM, 0, (LPARAM)&hdItem);
1214     expect(0, ret);
1215
1216     /* try to change width */
1217     rect.right = rect.bottom = 0;
1218     SendMessage(hChild, HDM_GETITEMRECT, 0, (LPARAM)&rect);
1219     ok(rect.right  != 0, "Expected not zero width\n");
1220     ok(rect.bottom != 0, "Expected not zero height\n");
1221
1222     SendMessage(hChild, WM_LBUTTONDOWN, 0, MAKELPARAM(rect.right, rect.bottom / 2));
1223     SendMessage(hChild, WM_MOUSEMOVE, 0, MAKELPARAM(rect.right + 20, rect.bottom / 2));
1224     SendMessage(hChild, WM_LBUTTONUP, 0, MAKELPARAM(rect.right + 20, rect.bottom / 2));
1225
1226     SendMessage(hChild, HDM_GETITEMRECT, 0, (LPARAM)&rect);
1227
1228     if (hdItem.cxy != rect.right)
1229     {
1230         win_skip("HDF_FIXEDWIDTH format not supported\n");
1231         DestroyWindow(hChild);
1232         return;
1233     }
1234
1235     /* try to adjust with message */
1236     hdItem.mask = HDI_WIDTH;
1237     hdItem.cxy = 90;
1238
1239     ret = SendMessage(hChild, HDM_SETITEM, 0, (LPARAM)&hdItem);
1240     expect(TRUE, ret);
1241
1242     rect.right = 0;
1243     SendMessage(hChild, HDM_GETITEMRECT, 0, (LPARAM)&rect);
1244     expect(90, rect.right);
1245
1246     /* hittesting doesn't report ondivider flag for HDF_FIXEDWIDTH */
1247     ht.pt.x = rect.right - 1;
1248     ht.pt.y = rect.bottom / 2;
1249     SendMessage(hChild, HDM_HITTEST, 0, (LPARAM)&ht);
1250     expect(HHT_ONHEADER, ht.flags);
1251
1252     /* try to adjust with message */
1253     hdItem.mask = HDI_FORMAT;
1254     hdItem.fmt  = 0;
1255
1256     ret = SendMessage(hChild, HDM_SETITEM, 0, (LPARAM)&hdItem);
1257     expect(TRUE, ret);
1258
1259     ht.pt.x = 90;
1260     ht.pt.y = rect.bottom / 2;
1261     SendMessage(hChild, HDM_HITTEST, 0, (LPARAM)&ht);
1262     expect(HHT_ONDIVIDER, ht.flags);
1263
1264     DestroyWindow(hChild);
1265 }
1266
1267 static void test_hds_nosizing(HWND hParent)
1268 {
1269     HWND hChild;
1270     HDITEM hdItem;
1271     DWORD ret;
1272     RECT rect;
1273     HDHITTESTINFO ht;
1274
1275     hChild = create_custom_header_control(hParent, FALSE);
1276
1277     hdItem.mask = HDI_WIDTH;
1278     hdItem.cxy = 80;
1279
1280     ret = SendMessage(hChild, HDM_INSERTITEM, 0, (LPARAM)&hdItem);
1281     expect(0, ret);
1282
1283     /* HDS_NOSIZING only blocks hittesting */
1284     ret = GetWindowLong(hChild, GWL_STYLE);
1285     SetWindowLong(hChild, GWL_STYLE, ret | HDS_NOSIZING);
1286
1287     /* try to change width with mouse gestures */
1288     rect.right = rect.bottom = 0;
1289     SendMessage(hChild, HDM_GETITEMRECT, 0, (LPARAM)&rect);
1290     ok(rect.right  != 0, "Expected not zero width\n");
1291     ok(rect.bottom != 0, "Expected not zero height\n");
1292
1293     SendMessage(hChild, WM_LBUTTONDOWN, 0, MAKELPARAM(rect.right, rect.bottom / 2));
1294     SendMessage(hChild, WM_MOUSEMOVE, 0, MAKELPARAM(rect.right + 20, rect.bottom / 2));
1295     SendMessage(hChild, WM_LBUTTONUP, 0, MAKELPARAM(rect.right + 20, rect.bottom / 2));
1296
1297     SendMessage(hChild, HDM_GETITEMRECT, 0, (LPARAM)&rect);
1298
1299     if (hdItem.cxy != rect.right)
1300     {
1301         win_skip("HDS_NOSIZING style not supported\n");
1302         DestroyWindow(hChild);
1303         return;
1304     }
1305
1306     /* this style doesn't set HDF_FIXEDWIDTH for items */
1307     hdItem.mask = HDI_FORMAT;
1308     ret = SendMessage(hChild, HDM_GETITEM, 0, (LPARAM)&hdItem);
1309     expect(TRUE, ret);
1310     ok(!(hdItem.fmt & HDF_FIXEDWIDTH), "Unexpected HDF_FIXEDWIDTH\n");
1311
1312     /* try to adjust with message */
1313     hdItem.mask = HDI_WIDTH;
1314     hdItem.cxy = 90;
1315
1316     ret = SendMessage(hChild, HDM_SETITEM, 0, (LPARAM)&hdItem);
1317     expect(TRUE, ret);
1318
1319     rect.right = 0;
1320     SendMessage(hChild, HDM_GETITEMRECT, 0, (LPARAM)&rect);
1321     expect(90, rect.right);
1322
1323     /* hittesting doesn't report ondivider flags for HDS_NOSIZING */
1324     ht.pt.x = rect.right - 1;
1325     ht.pt.y = rect.bottom / 2;
1326     SendMessage(hChild, HDM_HITTEST, 0, (LPARAM)&ht);
1327     expect(HHT_ONHEADER, ht.flags);
1328
1329     /* try to adjust with message */
1330     ret = GetWindowLong(hChild, GWL_STYLE);
1331     SetWindowLong(hChild, GWL_STYLE, ret & ~HDS_NOSIZING);
1332
1333     ht.pt.x = 90;
1334     ht.pt.y = rect.bottom / 2;
1335     SendMessage(hChild, HDM_HITTEST, 0, (LPARAM)&ht);
1336     expect(HHT_ONDIVIDER, ht.flags);
1337
1338     DestroyWindow(hChild);
1339 }
1340
1341 #define TEST_NMCUSTOMDRAW(draw_stage, item_spec, lparam, _left, _top, _right, _bottom) \
1342     ok(nm->dwDrawStage == draw_stage, "Invalid dwDrawStage %d vs %d\n", draw_stage, nm->dwDrawStage); \
1343     if (item_spec != -1) \
1344         ok(nm->dwItemSpec == item_spec, "Invalid dwItemSpec %d vs %ld\n", item_spec, nm->dwItemSpec); \
1345     ok(nm->lItemlParam == lparam, "Invalid lItemlParam %d vs %ld\n", lparam, nm->lItemlParam); \
1346     ok((nm->rc.top == _top && nm->rc.bottom == _bottom && nm->rc.left == _left && nm->rc.right == _right) || \
1347         broken(draw_stage != CDDS_ITEMPREPAINT), /* comctl32 < 5.80 */ \
1348         "Invalid rect (%d,%d) (%d,%d) vs (%d,%d) (%d,%d)\n", _left, _top, _right, _bottom, \
1349         nm->rc.left, nm->rc.top, nm->rc.right, nm->rc.bottom);
1350
1351 static LRESULT customdraw_1(int n, NMCUSTOMDRAW *nm)
1352 {
1353     if (nm == NULL) {  /* test ended */
1354         ok(n==1, "NM_CUSTOMDRAW messages: %d, expected: 1\n", n);
1355         return 0;
1356     }
1357
1358     switch (n)
1359     {
1360     case 0:
1361         /* don't test dwItemSpec - it's 0 no comctl5 but 1308756 on comctl6 */
1362         TEST_NMCUSTOMDRAW(CDDS_PREPAINT, -1, 0, 0, 0, 670, g_customheight);
1363         return 0;
1364     }
1365
1366     ok(FALSE, "To many custom draw messages (n=%d, nm->dwDrawStage=%d)\n", n, nm->dwDrawStage);
1367     return -1;
1368 }
1369
1370 static LRESULT customdraw_2(int n, NMCUSTOMDRAW *nm)
1371 {
1372     if (nm == NULL) {  /* test ended */
1373         ok(n==4, "NM_CUSTOMDRAW messages: %d, expected: 4\n", n);
1374         return 0;
1375     }
1376
1377     switch (n)
1378     {
1379     case 0:
1380         TEST_NMCUSTOMDRAW(CDDS_PREPAINT, -1, 0, 0, 0, 670, g_customheight);
1381         return CDRF_NOTIFYITEMDRAW;
1382     case 1:
1383         TEST_NMCUSTOMDRAW(CDDS_ITEMPREPAINT, 0, 0, 0, 0, 50, g_customheight);
1384         return 0;
1385     case 2:
1386         TEST_NMCUSTOMDRAW(CDDS_ITEMPREPAINT, 1, 5, 50, 0, 150, g_customheight);
1387         return 0;
1388     case 3:
1389         TEST_NMCUSTOMDRAW(CDDS_ITEMPREPAINT, 2, 10, 150, 0, 300, g_customheight);
1390         return 0;
1391     }
1392
1393     ok(FALSE, "To many custom draw messages (n=%d, nm->dwDrawStage=%d)\n", n, nm->dwDrawStage);
1394     return 0;
1395 }
1396
1397 static LRESULT customdraw_3(int n, NMCUSTOMDRAW *nm)
1398 {
1399     if (nm == NULL) {  /* test ended */
1400         ok(n==5, "NM_CUSTOMDRAW messages: %d, expected: 5\n", n);
1401         return 0;
1402     }
1403
1404     switch (n)
1405     {
1406     case 0:
1407         TEST_NMCUSTOMDRAW(CDDS_PREPAINT, -1, 0, 0, 0, 670, g_customheight);
1408         return CDRF_NOTIFYITEMDRAW|CDRF_NOTIFYPOSTERASE|CDRF_NOTIFYPOSTPAINT|CDRF_SKIPDEFAULT;
1409     case 1:
1410         TEST_NMCUSTOMDRAW(CDDS_ITEMPREPAINT, 0, 0, 0, 0, 50, g_customheight);
1411         return 0;
1412     case 2:
1413         TEST_NMCUSTOMDRAW(CDDS_ITEMPREPAINT, 1, 5, 50, 0, 150, g_customheight);
1414         return 0;
1415     case 3:
1416         TEST_NMCUSTOMDRAW(CDDS_ITEMPREPAINT, 2, 10, 150, 0, 300, g_customheight);
1417         return 0;
1418     case 4:
1419         TEST_NMCUSTOMDRAW(CDDS_POSTPAINT, -1, 0, 0, 0, 670, g_customheight);
1420         return 0;
1421     }
1422
1423     ok(FALSE, "To many custom draw messages (n=%d, nm->dwDrawStage=%d)\n", n, nm->dwDrawStage);
1424     return 0;
1425 }
1426
1427
1428 static LRESULT customdraw_4(int n, NMCUSTOMDRAW *nm)
1429 {
1430     if (nm == NULL) {  /* test ended */
1431         ok(n==4, "NM_CUSTOMDRAW messages: %d, expected: 4\n", n);
1432         return 0;
1433     }
1434
1435     switch (n)
1436     {
1437     case 0:
1438         TEST_NMCUSTOMDRAW(CDDS_PREPAINT, -1, 0, 0, 0, 670, g_customheight);
1439         return CDRF_NOTIFYITEMDRAW|CDRF_NOTIFYPOSTPAINT;
1440     case 1:
1441         TEST_NMCUSTOMDRAW(CDDS_ITEMPREPAINT, 0, 0, 0, 0, 50, g_customheight);
1442         return 0;
1443     case 2:
1444         TEST_NMCUSTOMDRAW(CDDS_ITEMPREPAINT, 2, 10, 150, 0, 300, g_customheight);
1445         return 0;
1446     case 3:
1447         TEST_NMCUSTOMDRAW(CDDS_POSTPAINT, -1, 0, 0, 0, 670, g_customheight);
1448         return 0;
1449     }
1450
1451     ok(FALSE, "To many custom draw messages (n=%d, nm->dwDrawStage=%d)\n", n, nm->dwDrawStage);
1452     return 0;
1453 }
1454
1455 static void run_customdraw_scenario(CUSTOMDRAWPROC proc)
1456 {
1457     g_CustomDrawProc = proc;
1458     g_CustomDrawCount = 0;
1459     InvalidateRect(hWndHeader, NULL, TRUE);
1460     UpdateWindow(hWndHeader);
1461     proc(g_CustomDrawCount, NULL);
1462     g_CustomDrawProc = NULL;
1463 }
1464
1465 static void test_customdraw(void)
1466 {
1467     int i;
1468     HDITEM item;
1469     RECT rect;
1470     CHAR name[] = "Test";
1471     hWndHeader = create_header_control();
1472     GetClientRect(hWndHeader, &rect);
1473     ok(rect.right - rect.left == 670 && rect.bottom - rect.top == g_customheight,
1474         "Tests will fail as header size is %dx%d instead of 670x%d\n",
1475         rect.right - rect.left, rect.bottom - rect.top, g_customheight);
1476
1477     for (i = 0; i < 3; i++)
1478     {
1479         ZeroMemory(&item, sizeof(item));
1480         item.mask = HDI_TEXT|HDI_WIDTH;
1481         item.cxy = 50*(i+1);
1482         item.pszText = name;
1483         item.lParam = i*5;
1484         SendMessage(hWndHeader, HDM_INSERTITEM, i, (LPARAM)&item);
1485     }
1486
1487     run_customdraw_scenario(customdraw_1);
1488     run_customdraw_scenario(customdraw_2);
1489     run_customdraw_scenario(customdraw_3);
1490
1491     ZeroMemory(&item, sizeof(item));
1492     item.mask = HDI_FORMAT;
1493     item.fmt = HDF_OWNERDRAW;
1494     SendMessage(hWndHeader, HDM_SETITEM, 1, (LPARAM)&item);
1495     g_DrawItem.CtlID = 0;
1496     g_DrawItem.CtlType = ODT_HEADER;
1497     g_DrawItem.hwndItem = hWndHeader;
1498     g_DrawItem.itemID = 1;
1499     g_DrawItem.itemState = 0;
1500     SendMessage(hWndHeader, HDM_GETITEMRECT, 1, (LPARAM)&g_DrawItem.rcItem);
1501     run_customdraw_scenario(customdraw_4);
1502     ok(g_DrawItemReceived, "WM_DRAWITEM not received\n");
1503     DestroyWindow(hWndHeader);
1504     hWndHeader = NULL;
1505     g_DrawItem.CtlType = 0;
1506     g_DrawItemReceived = FALSE;
1507 }
1508
1509 static void check_order(const int expected_id[], const int expected_order[],
1510                         int count, const char *type)
1511 {
1512     int i;
1513     HDITEMA hdi;
1514
1515     ok(getItemCount(hWndHeader) == count, "Invalid item count in order tests\n");
1516     for (i = 0; i < count; i++)
1517     {
1518         hdi.mask = HDI_LPARAM|HDI_ORDER;
1519         SendMessage(hWndHeader, HDM_GETITEMA, i, (LPARAM)&hdi);
1520         ok(hdi.lParam == expected_id[i],
1521             "Invalid item ids after '%s'- item %d has lParam %d\n", type, i, (int)hdi.lParam);
1522         ok(hdi.iOrder == expected_order[i],
1523             "Invalid item order after '%s'- item %d has iOrder %d\n", type, i, hdi.iOrder);
1524     }
1525 }
1526
1527 static void test_header_order (void)
1528 {
1529     const int rand1[] = {0, 1, 1, 0, 4};
1530     const int rand2[] = {4, 5, 6, 7, 4};
1531     const int rand3[] = {5, 5, 1, 6, 1};
1532     const int rand4[] = {1, 5, 2, 7, 6, 1, 4, 2, 3, 2};
1533     const int rand5[] = {7, 8, 5, 6, 7, 2, 1, 9, 10, 10};
1534     const int rand6[] = {2, 8, 3, 4, 0};
1535
1536     const int ids1[] = {3, 0, 2, 1, 4};
1537     const int ord1[] = {0, 1, 2, 3, 4};
1538     const int ids2[] = {3, 9, 7, 0, 2, 1, 4, 8, 6, 5};
1539     const int ord2[] = {0, 4, 7, 1, 2, 3, 9, 8, 6, 5};
1540     const int ord3[] = {0, 3, 9, 2, 1, 8, 7, 6, 5, 4};
1541     const int ids4[] = {9, 0, 1, 8, 6};
1542     const int ord4[] = {1, 0, 4, 3, 2};
1543
1544     char buffer[20];
1545     HDITEMA hdi;
1546     int i;
1547
1548     hWndHeader = create_header_control();
1549
1550     ZeroMemory(&hdi, sizeof(HDITEMA));
1551     hdi.mask = HDI_TEXT | HDI_LPARAM;
1552     hdi.pszText = buffer;
1553     strcpy(buffer, "test");
1554
1555     for (i = 0; i < 5; i++)
1556     {
1557         hdi.lParam = i;
1558         SendMessage(hWndHeader, HDM_INSERTITEMA, rand1[i], (LPARAM)&hdi);
1559         rand();
1560     }
1561     check_order(ids1, ord1, 5, "insert without iOrder");
1562
1563     hdi.mask |= HDI_ORDER;
1564     for (i = 0; i < 5; i++)
1565     {
1566         hdi.lParam = i + 5;
1567         hdi.iOrder = rand2[i];
1568         SendMessage(hWndHeader, HDM_INSERTITEMA, rand3[i], (LPARAM)&hdi);
1569         rand(); rand();
1570     }
1571     check_order(ids2, ord2, 10, "insert with order");
1572
1573     hdi.mask = HDI_ORDER;
1574     for (i=0; i<10; i++)
1575     {
1576         hdi.iOrder = rand5[i];
1577         SendMessage(hWndHeader, HDM_SETITEMA, rand4[i], (LPARAM)&hdi);
1578         rand(); rand();
1579     }
1580     check_order(ids2, ord3, 10, "setitems changing order");
1581
1582     for (i=0; i<5; i++)
1583         SendMessage(hWndHeader, HDM_DELETEITEM, rand6[i], 0);
1584     check_order(ids4, ord4, 5, "deleteitem");
1585
1586     DestroyWindow(hWndHeader);
1587 }
1588
1589 static LRESULT CALLBACK HeaderTestWndProc(HWND hWnd, UINT msg, WPARAM wParam, LPARAM lParam)
1590 {
1591     DRAWITEMSTRUCT *di;
1592     switch(msg) {
1593
1594     case WM_NOTIFY:
1595     {
1596         NMHEADERA *hdr = (NMHEADER *)lParam;
1597         EXPECTEDNOTIFY *expected;
1598         int i;
1599
1600         if (hdr->hdr.code == NM_CUSTOMDRAW)
1601             if (g_CustomDrawProc)
1602                 return g_CustomDrawProc(g_CustomDrawCount++, (NMCUSTOMDRAW*)hdr);
1603
1604         for (i=0; i<nUnexpectedNotify; i++)
1605             ok(hdr->hdr.code != unexpectedNotify[i], "Received invalid notify %d\n", hdr->hdr.code);
1606         
1607         if (nReceivedNotify >= nExpectedNotify || hdr->hdr.hwndFrom != hWndHeader )
1608             break;
1609
1610         expected = &expectedNotify[nReceivedNotify];
1611         if (hdr->hdr.code != expected->iCode)
1612             break;
1613         
1614         nReceivedNotify++;
1615         compare_items(hdr->hdr.code, &expected->hdItem, hdr->pitem, expected->fUnicode);
1616         break;
1617     }
1618
1619     case WM_DRAWITEM:
1620         di = (DRAWITEMSTRUCT *)lParam;
1621         ok(g_DrawItem.CtlType != 0, "Unexpected WM_DRAWITEM\n");
1622         if (g_DrawItem.CtlType == 0) return 0;
1623         g_DrawItemReceived = TRUE;
1624         compare(di->CtlType,   g_DrawItem.CtlType, "%d");
1625         compare(di->CtlID,     g_DrawItem.CtlID, "%d");
1626         compare(di->hwndItem,  g_DrawItem.hwndItem, "%p");
1627         compare(di->itemID,    g_DrawItem.itemID, "%d");
1628         compare(di->itemState, g_DrawItem.itemState, "%d");
1629         compare(di->rcItem.left,   g_DrawItem.rcItem.left, "%d");
1630         compare(di->rcItem.top,    g_DrawItem.rcItem.top, "%d");
1631         compare(di->rcItem.right,  g_DrawItem.rcItem.right, "%d");
1632         compare(di->rcItem.bottom, g_DrawItem.rcItem.bottom, "%d");
1633         break;
1634
1635     case WM_DESTROY:
1636         PostQuitMessage(0);
1637         break;
1638   
1639     default:
1640         return DefWindowProcA(hWnd, msg, wParam, lParam);
1641     }
1642     
1643     return 0L;
1644 }
1645
1646 static int init(void)
1647 {
1648     HMODULE hComctl32;
1649     BOOL (WINAPI *pInitCommonControlsEx)(const INITCOMMONCONTROLSEX*);
1650     WNDCLASSA wc;
1651     INITCOMMONCONTROLSEX iccex;
1652     TEXTMETRICA tm;
1653     HFONT hOldFont;
1654     HDC hdc;
1655
1656     hComctl32 = GetModuleHandleA("comctl32.dll");
1657     pInitCommonControlsEx = (void*)GetProcAddress(hComctl32, "InitCommonControlsEx");
1658     if (!pInitCommonControlsEx)
1659     {
1660         skip("InitCommonControlsEx() is missing. Skipping the tests\n");
1661         return 0;
1662     }
1663
1664     iccex.dwSize = sizeof(iccex);
1665     iccex.dwICC  = ICC_USEREX_CLASSES;
1666     pInitCommonControlsEx(&iccex);
1667
1668     wc.style = CS_HREDRAW | CS_VREDRAW;
1669     wc.cbClsExtra = 0;
1670     wc.cbWndExtra = 0;
1671     wc.hInstance = GetModuleHandleA(NULL);
1672     wc.hIcon = NULL;
1673     wc.hCursor = LoadCursorA(NULL, IDC_ARROW);
1674     wc.hbrBackground = GetSysColorBrush(COLOR_WINDOW);
1675     wc.lpszMenuName = NULL;
1676     wc.lpszClassName = "HeaderTestClass";
1677     wc.lpfnWndProc = HeaderTestWndProc;
1678     RegisterClassA(&wc);
1679
1680     /* The height of the header control depends on the height of the system font.
1681        The height of the system font is dpi dependent */
1682     hdc = GetDC(0);
1683     hOldFont = SelectObject(hdc, GetStockObject(SYSTEM_FONT));
1684     GetTextMetricsA(hdc, &tm);
1685     /* 2 dot extra space are needed for the border */
1686     g_customheight = tm.tmHeight + 2;
1687     trace("customdraw height: %d (dpi: %d)\n", g_customheight, GetDeviceCaps(hdc, LOGPIXELSY));
1688     SelectObject(hdc, hOldFont);
1689     ReleaseDC(0, hdc);
1690
1691     hHeaderParentWnd = CreateWindowExA(0, "HeaderTestClass", "Header test", WS_OVERLAPPEDWINDOW, 
1692       CW_USEDEFAULT, CW_USEDEFAULT, 672+2*GetSystemMetrics(SM_CXSIZEFRAME),
1693       226+GetSystemMetrics(SM_CYCAPTION)+2*GetSystemMetrics(SM_CYSIZEFRAME),
1694       NULL, NULL, GetModuleHandleA(NULL), 0);
1695     assert(hHeaderParentWnd != NULL);
1696     ShowWindow(hHeaderParentWnd, SW_SHOW);
1697     return 1;
1698 }
1699
1700 START_TEST(header)
1701 {
1702     HWND parent_hwnd;
1703     ULONG_PTR ctx_cookie;
1704     HWND hwnd;
1705
1706     if (!init())
1707         return;
1708
1709     test_header_control();
1710     test_header_order();
1711     test_customdraw();
1712
1713     DestroyWindow(hHeaderParentWnd);
1714
1715     init_msg_sequences(sequences, NUM_MSG_SEQUENCES);
1716     parent_hwnd = create_custom_parent_window();
1717     ok_sequence(sequences, PARENT_SEQ_INDEX, create_parent_wnd_seq, "create parent windows", FALSE);
1718
1719     test_hdm_index_messages(parent_hwnd);
1720     test_hdm_getitemrect(parent_hwnd);
1721     test_hdm_hittest(parent_hwnd);
1722     test_hdm_layout(parent_hwnd);
1723     test_hdm_ordertoindex(parent_hwnd);
1724     test_hdm_sethotdivider(parent_hwnd);
1725     test_hdm_imageMessages(parent_hwnd);
1726     test_hdm_filterMessages(parent_hwnd);
1727     test_hdm_unicodeformatMessages(parent_hwnd);
1728     test_hdm_bitmapmarginMessages(parent_hwnd);
1729
1730     if (!load_v6_module(&ctx_cookie))
1731     {
1732         DestroyWindow(parent_hwnd);
1733         return;
1734     }
1735
1736     /* this is a XP SP3 failure workaround */
1737     hwnd = CreateWindowExA(0, WC_HEADER, NULL,
1738                            WS_CHILD|WS_BORDER|WS_VISIBLE|HDS_BUTTONS|HDS_HORZ,
1739                            0, 0, 100, 100,
1740                            parent_hwnd, NULL, GetModuleHandleA(NULL), NULL);
1741
1742     if (!IsWindow(hwnd))
1743     {
1744         win_skip("FIXME: failed to create Header window.\n");
1745         unload_v6_module(ctx_cookie);
1746         DestroyWindow(parent_hwnd);
1747         return;
1748     }
1749     else
1750         DestroyWindow(hwnd);
1751
1752     /* comctl32 version 6 tests start here */
1753     test_hdf_fixedwidth(parent_hwnd);
1754     test_hds_nosizing(parent_hwnd);
1755
1756     unload_v6_module(ctx_cookie);
1757
1758     DestroyWindow(parent_hwnd);
1759 }