crypt32: Add additional path for Solaris 11 Express.
[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 SendMessage(hdex, HDM_INSERTITEMA, 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 = SendMessage(hdex, HDM_SETITEMA, 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 = SendMessage(hdex, HDM_SETITEMA, 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 SendMessage(hdex, HDM_DELETEITEM, idx, 0);
290 }
291
292 static LONG getItemCount(HWND hdex)
293 {
294     return 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 SendMessage(hdex, HDM_GETITEMA, 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 static LRESULT WINAPI header_subclass_proc(HWND hwnd, UINT message, WPARAM wParam, LPARAM lParam)
400 {
401     WNDPROC oldproc = (WNDPROC)GetWindowLongPtrA(hwnd, GWLP_USERDATA);
402     static LONG defwndproc_counter = 0;
403     LRESULT ret;
404     struct message msg;
405
406     trace("header: %p, %04x, %08lx, %08lx\n", hwnd, message, wParam, lParam);
407     msg.message = message;
408     msg.flags = sent|wparam|lparam;
409     if (defwndproc_counter) msg.flags |= defwinproc;
410     msg.wParam = wParam;
411     msg.lParam = lParam;
412     add_message(sequences, HEADER_SEQ_INDEX, &msg);
413
414     defwndproc_counter++;
415     ret = CallWindowProcA(oldproc, hwnd, message, wParam, lParam);
416     defwndproc_counter--;
417
418     return ret;
419 }
420
421 static LRESULT WINAPI parent_wnd_proc(HWND hwnd, UINT message, WPARAM wParam, LPARAM lParam)
422 {
423     static LONG defwndproc_counter = 0;
424     LRESULT ret;
425     struct message msg;
426
427     /* do not log painting messages */
428     if (message != WM_PAINT &&
429         message != WM_ERASEBKGND &&
430         message != WM_NCPAINT &&
431         message != WM_NCHITTEST &&
432         message != WM_GETTEXT &&
433         message != WM_GETICON &&
434         message != WM_DEVICECHANGE)
435
436     {
437         trace("parent: %p, %04x, %08lx, %08lx\n", hwnd, message, wParam, lParam);
438
439         msg.message = message;
440         msg.flags = sent|wparam|lparam;
441         if (defwndproc_counter) msg.flags |= defwinproc;
442         msg.wParam = wParam;
443         msg.lParam = lParam;
444         add_message(sequences, PARENT_SEQ_INDEX, &msg);
445    }
446
447     defwndproc_counter++;
448     ret = DefWindowProcA(hwnd, message, wParam, lParam);
449     defwndproc_counter--;
450
451     return ret;
452 }
453
454 static BOOL register_parent_wnd_class(void)
455 {
456     WNDCLASSA cls;
457
458     cls.style = 0;
459     cls.lpfnWndProc = parent_wnd_proc;
460     cls.cbClsExtra = 0;
461     cls.cbWndExtra = 0;
462     cls.hInstance = GetModuleHandleA(NULL);
463     cls.hIcon = 0;
464     cls.hCursor = LoadCursorA(0, IDC_ARROW);
465     cls.hbrBackground = GetStockObject(WHITE_BRUSH);
466     cls.lpszMenuName = NULL;
467     cls.lpszClassName = "Header test parent class";
468     return RegisterClassA(&cls);
469 }
470
471 static HWND create_custom_parent_window(void)
472 {
473     if (!register_parent_wnd_class())
474         return NULL;
475
476     return CreateWindowExA(0, "Header test parent class", "Header Message Sequence Testing",
477                            WS_OVERLAPPEDWINDOW, CW_USEDEFAULT, CW_USEDEFAULT,
478                            672+2*GetSystemMetrics(SM_CXSIZEFRAME),
479                            226+GetSystemMetrics(SM_CYCAPTION)+2*GetSystemMetrics(SM_CYSIZEFRAME),
480                            NULL, NULL, GetModuleHandleA(NULL), 0);
481 }
482
483 static HWND create_custom_header_control(HWND hParent, BOOL preloadHeaderItems)
484 {
485     WNDPROC oldproc;
486     HWND childHandle;
487     HDLAYOUT hlayout;
488     RECT rectwin;
489     WINDOWPOS winpos;
490     int retVal;
491     int loopcnt;
492     static char firstHeaderItem[] = "Name";
493     static char secondHeaderItem[] = "Size";
494     static char *items[] = {secondHeaderItem, firstHeaderItem};
495     HDITEM hdItem;
496     hdItem.mask = HDI_TEXT | HDI_WIDTH | HDI_FORMAT;
497     hdItem.fmt = HDF_LEFT;
498     hdItem.cxy = 80;
499     hdItem.cchTextMax = 260;
500
501
502     flush_sequences(sequences, NUM_MSG_SEQUENCES);
503
504     childHandle = CreateWindowEx(0, WC_HEADER, NULL,
505                            WS_CHILD|WS_BORDER|WS_VISIBLE|HDS_BUTTONS|HDS_HORZ,
506                            0, 0, 0, 0,
507                            hParent, NULL, NULL, NULL);
508     assert(childHandle);
509     if (preloadHeaderItems)
510     {
511          for ( loopcnt = 0 ; loopcnt < 2 ; loopcnt++ )
512          {
513              hdItem.pszText = items[loopcnt];
514              retVal = SendMessage(childHandle, HDM_INSERTITEM, loopcnt, (LPARAM) &hdItem);
515              ok(retVal == loopcnt, "Adding item %d failed with return value %d\n", ( loopcnt + 1 ), retVal);
516           }
517     }
518
519     if (winetest_interactive)
520        ShowWindow (hParent, SW_SHOW);
521
522     GetClientRect(hParent,&rectwin);
523     hlayout.prc = &rectwin;
524     hlayout.pwpos = &winpos;
525     SendMessageA(childHandle,HDM_LAYOUT,0,(LPARAM) &hlayout);
526     SetWindowPos(childHandle, winpos.hwndInsertAfter, winpos.x, winpos.y,
527                  winpos.cx, winpos.cy, 0);
528
529     oldproc = (WNDPROC)SetWindowLongPtrA(childHandle, GWLP_WNDPROC,
530                                          (LONG_PTR)header_subclass_proc);
531     SetWindowLongPtrA(childHandle, GWLP_USERDATA, (LONG_PTR)oldproc);
532     return childHandle;
533 }
534
535 static void check_auto_format(void)
536 {
537     HDITEMA hdiCreate;
538     HDITEMA hdiRead;
539     static CHAR text[] = "Test";
540     ZeroMemory(&hdiCreate, sizeof(HDITEMA));
541
542     /* Windows implicitly sets some format bits in INSERTITEM */
543
544     /* HDF_STRING is automatically set and cleared for no text */
545     hdiCreate.mask = HDI_TEXT|HDI_WIDTH|HDI_FORMAT;
546     hdiCreate.pszText = text;
547     hdiCreate.cxy = 100;
548     hdiCreate.fmt=HDF_CENTER;
549     addReadDelItem(hWndHeader, &hdiCreate, HDI_FORMAT, &hdiRead);
550     ok(hdiRead.fmt == (HDF_STRING|HDF_CENTER), "HDF_STRING not set automatically (fmt=%x)\n", hdiRead.fmt);
551
552     hdiCreate.mask = HDI_WIDTH|HDI_FORMAT;
553     hdiCreate.pszText = text;
554     hdiCreate.fmt = HDF_CENTER|HDF_STRING;
555     addReadDelItem(hWndHeader, &hdiCreate, HDI_FORMAT, &hdiRead);
556     ok(hdiRead.fmt == (HDF_CENTER), "HDF_STRING should be automatically cleared (fmt=%x)\n", hdiRead.fmt);
557
558     /* HDF_BITMAP is automatically set and cleared for a NULL bitmap or no bitmap */
559     hdiCreate.mask = HDI_BITMAP|HDI_WIDTH|HDI_FORMAT;
560     hdiCreate.hbm = CreateBitmap(16, 16, 1, 8, NULL);
561     hdiCreate.fmt = HDF_CENTER;
562     addReadDelItem(hWndHeader, &hdiCreate, HDI_FORMAT, &hdiRead);
563     ok(hdiRead.fmt == (HDF_BITMAP|HDF_CENTER), "HDF_BITMAP not set automatically (fmt=%x)\n", hdiRead.fmt);
564     DeleteObject(hdiCreate.hbm);
565
566     hdiCreate.hbm = NULL;
567     hdiCreate.fmt = HDF_CENTER|HDF_BITMAP;
568     addReadDelItem(hWndHeader, &hdiCreate, HDI_FORMAT, &hdiRead);
569     ok(hdiRead.fmt == HDF_CENTER, "HDF_BITMAP not cleared automatically for NULL bitmap (fmt=%x)\n", hdiRead.fmt);
570
571     hdiCreate.mask = HDI_WIDTH|HDI_FORMAT;
572     hdiCreate.fmt = HDF_CENTER|HDF_BITMAP;
573     addReadDelItem(hWndHeader, &hdiCreate, HDI_FORMAT, &hdiRead);
574     ok(hdiRead.fmt == HDF_CENTER, "HDF_BITMAP not cleared automatically for no bitmap (fmt=%x)\n", hdiRead.fmt);
575
576     /* HDF_IMAGE is automatically set but not cleared */
577     hdiCreate.mask = HDI_IMAGE|HDI_WIDTH|HDI_FORMAT;
578     hdiCreate.iImage = 17;
579     addReadDelItem(hWndHeader, &hdiCreate, HDI_FORMAT, &hdiRead);
580     ok(hdiRead.fmt == (HDF_IMAGE|HDF_CENTER), "HDF_IMAGE not set automatically (fmt=%x)\n", hdiRead.fmt);
581
582     hdiCreate.mask = HDI_WIDTH|HDI_FORMAT;
583     hdiCreate.fmt = HDF_CENTER|HDF_IMAGE;
584     hdiCreate.iImage = 0;
585     addReadDelItem(hWndHeader, &hdiCreate, HDI_FORMAT, &hdiRead);
586     ok(hdiRead.fmt == (HDF_CENTER|HDF_IMAGE), "HDF_IMAGE shouldn't be cleared automatically (fmt=%x)\n", hdiRead.fmt);
587 }
588
589 static void check_auto_fields(void)
590 {
591     HDITEMA hdiCreate;
592     HDITEMA hdiRead;
593     static CHAR text[] = "Test";
594     LONG res;
595
596     /* Windows stores the format, width, lparam even if they are not in the item's mask */
597     ZeroMemory(&hdiCreate, sizeof(HDITEMA));
598     hdiCreate.mask = HDI_TEXT;
599     hdiCreate.cxy = 100;
600     hdiCreate.pszText = text;
601     addReadDelItem(hWndHeader, &hdiCreate, HDI_WIDTH, &hdiRead);
602     TEST_GET_ITEMCOUNT(6);
603     ok(hdiRead.cxy == hdiCreate.cxy, "cxy should be automatically set\n");
604
605     ZeroMemory(&hdiCreate, sizeof(HDITEMA));
606     hdiCreate.mask = HDI_TEXT;
607     hdiCreate.pszText = text;
608     hdiCreate.lParam = 0x12345678;
609     addReadDelItem(hWndHeader, &hdiCreate, HDI_LPARAM, &hdiRead);
610     TEST_GET_ITEMCOUNT(6);
611     ok(hdiRead.lParam == hdiCreate.lParam, "lParam should be automatically set\n");
612
613     ZeroMemory(&hdiCreate, sizeof(HDITEMA));
614     hdiCreate.mask = HDI_TEXT;
615     hdiCreate.pszText = text;
616     hdiCreate.fmt = HDF_STRING|HDF_CENTER;
617     addReadDelItem(hWndHeader, &hdiCreate, HDI_FORMAT, &hdiRead);
618     TEST_GET_ITEMCOUNT(6);
619     ok(hdiRead.fmt == hdiCreate.fmt, "fmt should be automatically set\n");
620
621     /* others fields are not set */
622     ZeroMemory(&hdiCreate, sizeof(HDITEMA));
623     hdiCreate.mask = HDI_TEXT;
624     hdiCreate.pszText = text;
625     hdiCreate.hbm = CreateBitmap(16, 16, 1, 8, NULL);
626     addReadDelItem(hWndHeader, &hdiCreate, HDI_BITMAP, &hdiRead);
627     TEST_GET_ITEMCOUNT(6);
628     ok(hdiRead.hbm == NULL, "hbm should not be automatically set\n");
629     DeleteObject(hdiCreate.hbm);
630
631     ZeroMemory(&hdiCreate, sizeof(HDITEMA));
632     hdiCreate.mask = HDI_IMAGE;
633     hdiCreate.iImage = 17;
634     hdiCreate.pszText = text;
635     addReadDelItem(hWndHeader, &hdiCreate, HDI_TEXT, &hdiRead);
636     TEST_GET_ITEMCOUNT(6);
637     ok(hdiRead.pszText==NULL, "pszText shouldn't be automatically set\n");
638
639     /* field from comctl >4.0 not tested as the system probably won't touch them */
640 }
641
642 static void check_mask(void)
643 {
644     HDITEMA hdi;
645     static CHAR text[] = "ABC";
646     LRESULT ret;
647
648     /* don't create items if the mask is zero */
649     ZeroMemory(&hdi, sizeof(hdi));
650     hdi.mask = 0;
651     hdi.cxy = 200;
652     hdi.pszText = text;
653     hdi.fmt = 0;
654     hdi.iOrder = 0;
655     hdi.lParam = 17;
656     hdi.cchTextMax = 260;
657     ret = SendMessage(hWndHeader, HDM_INSERTITEM, 0, (LPARAM)&hdi);
658     ok(ret == -1, "Creating an item with a zero mask should have failed\n");
659     if (ret != -1) SendMessage(hWndHeader, HDM_DELETEITEM, 0, 0);
660
661     /* with a non-zero mask creation will succeed */
662     ZeroMemory(&hdi, sizeof(hdi));
663     hdi.mask = HDI_LPARAM;
664     ret = SendMessage(hWndHeader, HDM_INSERTITEM, 0, (LPARAM)&hdi);
665     ok(ret != -1, "Adding item with non-zero mask failed\n");
666     if (ret != -1)
667         SendMessage(hWndHeader, HDM_DELETEITEM, 0, 0);
668
669     /* in SETITEM if the mask contains a unknown bit, it is ignored */
670     ZeroMemory(&hdi, sizeof(hdi));
671     hdi.mask = 0x08000000 | HDI_LPARAM | HDI_IMAGE;
672     hdi.lParam = 133;
673     hdi.iImage = 17;
674     ret = SendMessage(hWndHeader, HDM_INSERTITEM, 0, (LPARAM)&hdi);
675     ok(ret != -1, "Adding item failed\n");
676
677     if (ret != -1)
678     {
679         /* check result */
680         ZeroMemory(&hdi, sizeof(hdi));
681         hdi.mask = HDI_LPARAM | HDI_IMAGE;
682         SendMessage(hWndHeader, HDM_GETITEM, 0, (LPARAM)&hdi);
683         ok(hdi.lParam == 133, "comctl32 4.0 field not set\n");
684         ok(hdi.iImage == 17, "comctl32 >4.0 field not set\n");
685
686         /* but in GETITEM if an unknown bit is set, comctl32 uses only version 4.0 fields */
687         ZeroMemory(&hdi, sizeof(hdi));
688         hdi.mask = 0x08000000 | HDI_LPARAM | HDI_IMAGE;
689         SendMessage(hWndHeader, HDM_GETITEM, 0, (LPARAM)&hdi);
690         ok(hdi.lParam == 133, "comctl32 4.0 field not read\n");
691         ok(hdi.iImage == 0, "comctl32 >4.0 field shouldn't be read\n");
692
693         SendMessage(hWndHeader, HDM_DELETEITEM, 0, 0);
694     }
695 }
696
697 static void test_header_control (void)
698 {
699     LONG res;
700     static char buffer[MAX_CHARS];
701     int i;
702
703     hWndHeader = create_header_control ();
704
705     for (i = 3; i >= 0; i--)
706     {
707         TEST_GET_ITEMCOUNT(3-i);
708         res = addItem(hWndHeader, 0, str_items[i]);
709         ok(res == 0, "Adding simple item failed (%d)\n", res);
710     }
711
712     TEST_GET_ITEMCOUNT(4);
713     res = addItem(hWndHeader, 99, str_items[i+1]);
714     ok(res != -1, "Adding Out of Range item should fail with -1 got (%d)\n", res);
715     TEST_GET_ITEMCOUNT(5);
716     res = addItem(hWndHeader, 5, str_items[i+1]);
717     ok(res != -1, "Adding Out of Range item should fail with -1 got (%d)\n", res);
718     TEST_GET_ITEMCOUNT(6);
719
720     for (i = 0; i < 4; i++) { TEST_GET_ITEM(i,i); TEST_GET_ITEMCOUNT(6); }
721
722     res=getItem(hWndHeader, 99, buffer);
723     ok(res == 0, "Getting Out of Range item should fail with 0 (%d), got %s\n", res,buffer);
724     res=getItem(hWndHeader, 5, buffer);
725     ok(res == 1, "Getting Out of Range item should fail with 1 (%d), got %s\n", res,buffer);
726     res=getItem(hWndHeader, -2, buffer);
727     ok(res == 0, "Getting Out of Range item should fail with 0 (%d), got %s\n", res,buffer);
728
729     if (winetest_interactive)
730     {
731         UpdateWindow(hHeaderParentWnd);
732         UpdateWindow(hWndHeader);
733     }
734
735     TEST_GET_ITEMCOUNT(6);
736     res=setItem(hWndHeader, 99, str_items[5], FALSE);
737     ok(res == 0, "Setting Out of Range item should fail with 0 (%d)\n", res);
738     res=setItem(hWndHeader, 5, str_items[5], TRUE);
739     ok(res == 1, "Setting Out of Range item should fail with 1 (%d)\n", res);
740     res=setItem(hWndHeader, -2, str_items[5], FALSE);
741     ok(res == 0, "Setting Out of Range item should fail with 0 (%d)\n", res);
742     TEST_GET_ITEMCOUNT(6);
743
744     for (i = 0; i < 4; i++)
745     {
746         res = setItem(hWndHeader, i, str_items[4], TRUE);
747         ok(res != 0, "Setting %d item failed (%d)\n", i+1, res);
748         TEST_GET_ITEM(i, 4);
749         TEST_GET_ITEMCOUNT(6);
750     }
751
752     SendMessageA(hWndHeader, HDM_SETUNICODEFORMAT, TRUE, 0);
753     setItemUnicodeNotify(hWndHeader, 3, pszUniTestA, pszUniTestW);
754     SendMessageA(hWndHeader, WM_NOTIFYFORMAT, (WPARAM)hHeaderParentWnd, NF_REQUERY);
755     setItem(hWndHeader, 3, str_items[4], TRUE);
756     
757     dont_expect_notify(HDN_GETDISPINFOA);
758     dont_expect_notify(HDN_GETDISPINFOW);
759     addItem(hWndHeader, 0, LPSTR_TEXTCALLBACKA);
760     setItem(hWndHeader, 0, str_items[4], TRUE);
761     /* unexpected notifies cleared by notifies_received in setItem */
762     dont_expect_notify(HDN_GETDISPINFOA);
763     dont_expect_notify(HDN_GETDISPINFOW);
764     setItem(hWndHeader, 0, LPSTR_TEXTCALLBACKA, TRUE);
765     /* unexpected notifies cleared by notifies_received in setItem */
766     delItem(hWndHeader, 0);
767
768     check_auto_format();
769     TEST_GET_ITEMCOUNT(6);
770     check_auto_fields();
771     TEST_GET_ITEMCOUNT(6);
772     check_mask();
773     TEST_GET_ITEMCOUNT(6);
774
775     res = delItem(hWndHeader, 5);
776     ok(res == 1, "Deleting Out of Range item should fail with 1 (%d)\n", res);
777     res = delItem(hWndHeader, -2);
778     ok(res == 0, "Deleting Out of Range item should fail with 0 (%d)\n", res);
779     TEST_GET_ITEMCOUNT(5);
780
781     res = delItem(hWndHeader, 3);
782     ok(res != 0, "Deleting using out of range index failed (%d)\n", res);
783     TEST_GET_ITEMCOUNT(4);
784     res = delItem(hWndHeader, 0);
785     ok(res != 0, "Deleting using out of range index failed (%d)\n", res);
786     TEST_GET_ITEMCOUNT(3);
787     res = delItem(hWndHeader, 0);
788     ok(res != 0, "Deleting using out of range index failed (%d)\n", res);
789     TEST_GET_ITEMCOUNT(2);
790     res = delItem(hWndHeader, 0);
791     ok(res != 0, "Deleting using out of range index failed (%d)\n", res);
792     TEST_GET_ITEMCOUNT(1);
793
794     DestroyWindow(hWndHeader);
795 }
796
797 static void test_hdm_getitemrect(HWND hParent)
798 {
799
800     HWND hChild;
801     RECT rect;
802     int retVal;
803
804     flush_sequences(sequences, NUM_MSG_SEQUENCES);
805     hChild = create_custom_header_control(hParent, TRUE);
806     ok_sequence(sequences, PARENT_SEQ_INDEX, add_header_to_parent_seq,
807                                     "adder header control to parent", FALSE);
808
809     retVal = SendMessage(hChild, HDM_GETITEMRECT, 1, (LPARAM) &rect);
810     ok(retVal == TRUE, "Getting item rect should TRUE, got %d\n", retVal);
811     /* check bounding rectangle information of 2nd header item */
812     expect(80, rect.left);
813     expect(0, rect.top);
814     expect(160, rect.right);
815     expect(g_customheight, rect.bottom);
816
817     retVal = SendMessage(hChild, HDM_GETITEMRECT, 0, (LPARAM) &rect);
818
819     ok(retVal == TRUE, "Getting item rect should TRUE, got %d\n", retVal);
820     /* check bounding rectangle information of 1st header item */
821     expect(0, rect.left);
822     expect(0, rect.top);
823
824     expect(80, rect.right);
825     expect(g_customheight, rect.bottom);
826
827     retVal = SendMessage(hChild, HDM_GETITEMRECT, 10, (LPARAM) &rect);
828     ok(retVal == 0, "Getting rect of nonexistent item should return 0, got %d\n", retVal);
829
830     ok_sequence(sequences, HEADER_SEQ_INDEX, getItemRect_seq, "getItemRect sequence testing", FALSE);
831     DestroyWindow(hChild);
832 }
833
834 static void test_hdm_layout(HWND hParent)
835 {
836     HWND hChild;
837     int retVal;
838     RECT rect;
839     HDLAYOUT hdLayout;
840     WINDOWPOS windowPos;
841     hdLayout.prc = &rect;
842     hdLayout.pwpos = &windowPos;
843
844     flush_sequences(sequences, NUM_MSG_SEQUENCES);
845     hChild = create_custom_header_control(hParent, TRUE);
846     ok_sequence(sequences, PARENT_SEQ_INDEX, add_header_to_parent_seq,
847                                     "adder header control to parent", FALSE);
848
849     flush_sequences(sequences, NUM_MSG_SEQUENCES);
850     retVal = SendMessage(hChild, HDM_LAYOUT, 0, (LPARAM) &hdLayout);
851     expect(TRUE, retVal);
852
853     ok_sequence(sequences, HEADER_SEQ_INDEX, layout_seq, "layout sequence testing", FALSE);
854
855     DestroyWindow(hChild);
856 }
857
858 static void test_hdm_ordertoindex(HWND hParent)
859 {
860     HWND hChild;
861     int retVal;
862
863     flush_sequences(sequences, NUM_MSG_SEQUENCES);
864     hChild = create_custom_header_control(hParent, TRUE);
865     ok_sequence(sequences, PARENT_SEQ_INDEX, add_header_to_parent_seq,
866                                     "adder header control to parent", FALSE);
867
868     flush_sequences(sequences, NUM_MSG_SEQUENCES);
869     retVal = SendMessage(hChild, HDM_ORDERTOINDEX, 1, 0);
870     expect(1, retVal);
871
872     ok_sequence(sequences, HEADER_SEQ_INDEX, orderToIndex_seq, "orderToIndex sequence testing", FALSE);
873     DestroyWindow(hChild);
874 }
875
876 static void test_hdm_hittest(HWND hParent)
877 {
878     HWND hChild;
879     int retVal;
880     POINT pt;
881     HDHITTESTINFO hdHitTestInfo;
882     const int firstItemRightBoundary = 80;
883     const int secondItemRightBoundary = 160;
884     const int bottomBoundary = g_customheight;
885
886     pt.x = firstItemRightBoundary - 1;
887     pt.y = bottomBoundary - 1;
888     hdHitTestInfo.pt = pt;
889
890
891     flush_sequences(sequences, NUM_MSG_SEQUENCES);
892     hChild = create_custom_header_control(hParent, TRUE);
893     ok_sequence(sequences, PARENT_SEQ_INDEX, add_header_to_parent_seq,
894                                     "adder header control to parent", FALSE);
895
896     flush_sequences(sequences, NUM_MSG_SEQUENCES);
897     retVal = SendMessage(hChild, HDM_HITTEST, 0, (LPARAM) &hdHitTestInfo);
898     expect(0, retVal);
899     expect(0, hdHitTestInfo.iItem);
900     expect(HHT_ONDIVIDER, hdHitTestInfo.flags);
901
902     pt.x = secondItemRightBoundary - 1;
903     pt.y = bottomBoundary - 1;
904     hdHitTestInfo.pt = pt;
905     retVal = SendMessage(hChild, HDM_HITTEST, 1, (LPARAM) &hdHitTestInfo);
906     expect(1, retVal);
907     expect(1, hdHitTestInfo.iItem);
908     expect(HHT_ONDIVIDER, hdHitTestInfo.flags);
909
910     pt.x = secondItemRightBoundary;
911     pt.y = bottomBoundary + 1;
912     hdHitTestInfo.pt = pt;
913     retVal = SendMessage(hChild, HDM_HITTEST, 0, (LPARAM) &hdHitTestInfo);
914     expect(-1, retVal);
915     expect(-1, hdHitTestInfo.iItem);
916     expect(HHT_BELOW, hdHitTestInfo.flags);
917
918     ok_sequence(sequences, HEADER_SEQ_INDEX, hittest_seq, "hittest sequence testing", FALSE);
919
920     DestroyWindow(hChild);
921 }
922
923 static void test_hdm_sethotdivider(HWND hParent)
924 {
925     HWND hChild;
926     int retVal;
927     /*  low word: x coordinate = 5
928      *  high word:  y coordinate = 5
929      */
930
931     flush_sequences(sequences, NUM_MSG_SEQUENCES);
932     hChild = create_custom_header_control(hParent, TRUE);
933     ok_sequence(sequences, PARENT_SEQ_INDEX, add_header_to_parent_seq,
934                                     "adder header control to parent", FALSE);
935
936     flush_sequences(sequences, NUM_MSG_SEQUENCES);
937     retVal = SendMessage(hChild, HDM_SETHOTDIVIDER, TRUE, MAKELPARAM(5, 5));
938     expect(0, retVal);
939
940     retVal = SendMessage(hChild, HDM_SETHOTDIVIDER, FALSE, 100);
941     expect(100, retVal);
942     retVal = SendMessage(hChild, HDM_SETHOTDIVIDER, FALSE, 1);
943     expect(1, retVal);
944     if (winetest_interactive)
945        ok_sequence(sequences, HEADER_SEQ_INDEX, setHotDivider_seq_interactive,
946                    "setHotDivider sequence testing", TRUE);
947     else
948        ok_sequence(sequences, HEADER_SEQ_INDEX, setHotDivider_seq_noninteractive,
949                    "setHotDivider sequence testing", FALSE);
950
951     DestroyWindow(hChild);
952 }
953
954 static void test_hdm_imageMessages(HWND hParent)
955 {
956     HIMAGELIST hImageList = ImageList_Create (4, 4, 0, 1, 0);
957     HIMAGELIST hIml;
958     HWND hChild;
959
960     flush_sequences(sequences, NUM_MSG_SEQUENCES);
961     hChild = create_custom_header_control(hParent, TRUE);
962     ok_sequence(sequences, PARENT_SEQ_INDEX, add_header_to_parent_seq,
963                                     "adder header control to parent", FALSE);
964
965     flush_sequences(sequences, NUM_MSG_SEQUENCES);
966
967     hIml = (HIMAGELIST) SendMessage(hChild, HDM_SETIMAGELIST, 0, (LPARAM) hImageList);
968     ok(hIml == NULL, "Expected NULL, got %p\n", hIml);
969
970     hIml = (HIMAGELIST) SendMessage(hChild, HDM_GETIMAGELIST, 0, 0);
971     ok(hIml != NULL, "Expected non-NULL handle, got %p\n", hIml);
972
973     hIml = (HIMAGELIST) SendMessage(hChild, HDM_CREATEDRAGIMAGE, 0, 0);
974     ok(hIml != NULL, "Expected non-NULL handle, got %p\n", hIml);
975     ImageList_Destroy(hIml);
976
977     ok_sequence(sequences, HEADER_SEQ_INDEX, imageMessages_seq, "imageMessages sequence testing", FALSE);
978
979     DestroyWindow(hChild);
980 }
981
982 static void test_hdm_filterMessages(HWND hParent)
983 {
984     HWND hChild;
985     int retVal, timeout;
986
987     flush_sequences(sequences, NUM_MSG_SEQUENCES);
988     hChild = create_custom_header_control(hParent, TRUE);
989     assert(hChild);
990     ok_sequence(sequences, PARENT_SEQ_INDEX, add_header_to_parent_seq,
991                                     "adder header control to parent", FALSE);
992
993     timeout = SendMessage(hChild, HDM_SETFILTERCHANGETIMEOUT, 1, 100);
994     SendMessage(hChild, HDM_SETFILTERCHANGETIMEOUT, 1, timeout);
995
996     flush_sequences(sequences, NUM_MSG_SEQUENCES);
997
998     /* msdn incorrectly states that return value
999      * is the index of the filter control being
1000      * modified. The sendMessage here should
1001      * return previous filter timeout value
1002      */
1003
1004     retVal = SendMessage(hChild, HDM_SETFILTERCHANGETIMEOUT, 1, 100);
1005     expect(timeout, retVal);
1006
1007     todo_wine
1008     {
1009         retVal = SendMessage(hChild, HDM_CLEARFILTER, 0, 1);
1010         if (retVal == 0)
1011             win_skip("HDM_CLEARFILTER needs 5.80\n");
1012         else
1013             expect(1, retVal);
1014
1015         retVal = SendMessage(hChild, HDM_EDITFILTER, 1, 0);
1016         if (retVal == 0)
1017             win_skip("HDM_EDITFILTER needs 5.80\n");
1018         else
1019             expect(1, retVal);
1020     }
1021     if (winetest_interactive)
1022          ok_sequence(sequences, HEADER_SEQ_INDEX, filterMessages_seq_interactive,
1023                      "filterMessages sequence testing", TRUE);
1024     else
1025          ok_sequence(sequences, HEADER_SEQ_INDEX, filterMessages_seq_noninteractive,
1026                      "filterMessages sequence testing", FALSE);
1027     DestroyWindow(hChild);
1028
1029 }
1030
1031 static void test_hdm_unicodeformatMessages(HWND hParent)
1032 {
1033     HWND hChild;
1034     int retVal;
1035
1036     flush_sequences(sequences, NUM_MSG_SEQUENCES);
1037     hChild = create_custom_header_control(hParent, TRUE);
1038     ok_sequence(sequences, PARENT_SEQ_INDEX, add_header_to_parent_seq,
1039                                     "adder header control to parent", FALSE);
1040
1041     flush_sequences(sequences, NUM_MSG_SEQUENCES);
1042     retVal = SendMessage(hChild, HDM_SETUNICODEFORMAT, TRUE, 0);
1043     expect(0, retVal);
1044     retVal = SendMessage(hChild, HDM_GETUNICODEFORMAT, 0, 0);
1045     expect(1, retVal);
1046
1047     ok_sequence(sequences, HEADER_SEQ_INDEX, unicodeformatMessages_seq,
1048                      "unicodeformatMessages sequence testing", FALSE);
1049     DestroyWindow(hChild);
1050 }
1051
1052 static void test_hdm_bitmapmarginMessages(HWND hParent)
1053 {
1054     HWND hChild;
1055     int retVal;
1056
1057     flush_sequences(sequences, NUM_MSG_SEQUENCES);
1058     hChild = create_custom_header_control(hParent, TRUE);
1059     ok_sequence(sequences, PARENT_SEQ_INDEX, add_header_to_parent_seq,
1060                                     "adder header control to parent", FALSE);
1061
1062     flush_sequences(sequences, NUM_MSG_SEQUENCES);
1063     retVal = SendMessage(hChild, HDM_GETBITMAPMARGIN, 0, 0);
1064     if (retVal == 0)
1065         win_skip("HDM_GETBITMAPMARGIN needs 5.80\n");
1066     else
1067         expect(6, retVal);
1068
1069     ok_sequence(sequences, HEADER_SEQ_INDEX, bitmapmarginMessages_seq,
1070                       "bitmapmarginMessages sequence testing", FALSE);
1071     DestroyWindow(hChild);
1072 }
1073
1074 static void test_hdm_index_messages(HWND hParent)
1075 {
1076     HWND hChild;
1077     int retVal;
1078     int loopcnt;
1079     int strcmpResult;
1080     int iSize;
1081     static const int lpiarray[2] = {1, 0};
1082     static int lpiarrayReceived[2];
1083     static char firstHeaderItem[] = "Name";
1084     static char secondHeaderItem[] = "Size";
1085     static char thirdHeaderItem[] = "Type";
1086     static char fourthHeaderItem[] = "Date Modified";
1087     static char *items[] = {firstHeaderItem, secondHeaderItem, thirdHeaderItem, fourthHeaderItem};
1088     RECT rect;
1089     HDITEM hdItem;
1090     hdItem.mask = HDI_TEXT | HDI_WIDTH | HDI_FORMAT;
1091     hdItem.fmt = HDF_LEFT;
1092     hdItem.cxy = 80;
1093     hdItem.cchTextMax = 260;
1094
1095     flush_sequences(sequences, NUM_MSG_SEQUENCES);
1096     hChild = create_custom_header_control(hParent, FALSE);
1097     if (winetest_interactive)
1098          ok_sequence(sequences, PARENT_SEQ_INDEX, add_header_to_parent_seq_interactive,
1099                                               "adder header control to parent", TRUE);
1100     else
1101          ok_sequence(sequences, PARENT_SEQ_INDEX, add_header_to_parent_seq,
1102                                      "adder header control to parent", FALSE);
1103     flush_sequences(sequences, NUM_MSG_SEQUENCES);
1104     for ( loopcnt = 0 ; loopcnt < 4 ; loopcnt++ )
1105     {
1106       hdItem.pszText = items[loopcnt];
1107       retVal = SendMessage(hChild, HDM_INSERTITEM, loopcnt, (LPARAM) &hdItem);
1108       ok(retVal == loopcnt, "Adding item %d failed with return value %d\n", ( loopcnt + 1 ), retVal);
1109     }
1110     ok_sequence(sequences, HEADER_SEQ_INDEX, insertItem_seq, "insertItem sequence testing", FALSE);
1111
1112     flush_sequences(sequences, NUM_MSG_SEQUENCES);
1113
1114     retVal = SendMessage(hChild, HDM_DELETEITEM, 3, (LPARAM) &hdItem);
1115     ok(retVal == TRUE, "Deleting item 3 should return TRUE, got %d\n", retVal);
1116     retVal = SendMessage(hChild, HDM_GETITEMCOUNT, 0, 0);
1117     ok(retVal == 3, "Getting item count should return 3, got %d\n", retVal);
1118
1119     retVal = SendMessage(hChild, HDM_DELETEITEM, 3, (LPARAM) &hdItem);
1120     ok(retVal == FALSE, "Deleting already-deleted item should return FALSE, got %d\n", retVal);
1121     retVal = SendMessage(hChild, HDM_GETITEMCOUNT, 0, 0);
1122     ok(retVal == 3, "Getting item count should return 3, got %d\n", retVal);
1123
1124     retVal = SendMessage(hChild, HDM_DELETEITEM, 2, (LPARAM) &hdItem);
1125     ok(retVal == TRUE, "Deleting item 2 should return TRUE, got %d\n", retVal);
1126     retVal = SendMessage(hChild, HDM_GETITEMCOUNT, 0, 0);
1127     ok(retVal == 2, "Getting item count should return 2, got %d\n", retVal);
1128
1129     ok_sequence(sequences, HEADER_SEQ_INDEX, deleteItem_getItemCount_seq,
1130                          "deleteItem_getItemCount sequence testing", FALSE);
1131
1132     flush_sequences(sequences, NUM_MSG_SEQUENCES);
1133
1134     retVal = SendMessage(hChild, HDM_GETITEM, 3, (LPARAM) &hdItem);
1135     ok(retVal == FALSE, "Getting already-deleted item should return FALSE, got %d\n", retVal);
1136
1137     retVal = SendMessage(hChild, HDM_GETITEM, 0, (LPARAM) &hdItem);
1138     ok(retVal == TRUE, "Getting the 1st header item should return TRUE, got %d\n", retVal);
1139
1140     ok_sequence(sequences, HEADER_SEQ_INDEX, getItem_seq, "getItem sequence testing", FALSE);
1141
1142     /* check if the item is the right one */
1143     strcmpResult =  strcmp(hdItem.pszText, firstHeaderItem);
1144     expect(0, strcmpResult);
1145     expect(80, hdItem.cxy);
1146
1147     iSize = SendMessage(hChild, HDM_GETITEMCOUNT, 0, 0);
1148
1149     /* item should be updated just after accepting new array */
1150     ShowWindow(hChild, SW_HIDE);
1151     retVal = SendMessage(hChild, HDM_SETORDERARRAY, iSize, (LPARAM) lpiarray);
1152     expect(TRUE, retVal);
1153     rect.left = 0;
1154     retVal = SendMessage(hChild, HDM_GETITEMRECT, 0, (LPARAM) &rect);
1155     expect(TRUE, retVal);
1156     ok(rect.left != 0, "Expected updated rectangle\n");
1157
1158     flush_sequences(sequences, NUM_MSG_SEQUENCES);
1159
1160     retVal = SendMessage(hChild, HDM_SETORDERARRAY, iSize, (LPARAM) lpiarray);
1161     ok(retVal == TRUE, "Setting header items order should return TRUE, got %d\n", retVal);
1162
1163     retVal = SendMessage(hChild, HDM_GETORDERARRAY, iSize, (LPARAM) lpiarrayReceived);
1164     ok(retVal == TRUE, "Getting header items order should return TRUE, got %d\n", retVal);
1165
1166     ok_sequence(sequences, HEADER_SEQ_INDEX, orderArray_seq, "set_get_orderArray sequence testing", FALSE);
1167
1168     /* check if the array order is set correctly and the size of the array is correct. */
1169     expect(2, iSize);
1170     expect(lpiarray[0], lpiarrayReceived[0]);
1171     expect(lpiarray[1], lpiarrayReceived[1]);
1172
1173     hdItem.mask = HDI_FORMAT;
1174     hdItem.fmt = HDF_CENTER | HDF_STRING;
1175
1176     flush_sequences(sequences, NUM_MSG_SEQUENCES);
1177
1178     retVal = SendMessage(hChild, HDM_SETITEM, 0, (LPARAM) &hdItem);
1179     ok(retVal == TRUE, "Aligning 1st header item to center should return TRUE, got %d\n", retVal);
1180     hdItem.fmt = HDF_RIGHT | HDF_STRING;
1181     retVal = SendMessage(hChild, HDM_SETITEM, 1, (LPARAM) &hdItem);
1182     ok(retVal == TRUE, "Aligning 2nd header item to right should return TRUE, got %d\n", retVal);
1183
1184     ok_sequence(sequences, HEADER_SEQ_INDEX, setItem_seq, "setItem sequence testing", FALSE);
1185     DestroyWindow(hChild);
1186 }
1187
1188 static void test_hdf_fixedwidth(HWND hParent)
1189 {
1190     HWND hChild;
1191     HDITEM hdItem;
1192     DWORD ret;
1193     RECT rect;
1194     HDHITTESTINFO ht;
1195
1196     hChild = create_custom_header_control(hParent, FALSE);
1197
1198     hdItem.mask = HDI_WIDTH | HDI_FORMAT;
1199     hdItem.fmt = HDF_FIXEDWIDTH;
1200     hdItem.cxy = 80;
1201
1202     ret = SendMessage(hChild, HDM_INSERTITEM, 0, (LPARAM)&hdItem);
1203     expect(0, ret);
1204
1205     /* try to change width */
1206     rect.right = rect.bottom = 0;
1207     SendMessage(hChild, HDM_GETITEMRECT, 0, (LPARAM)&rect);
1208     ok(rect.right  != 0, "Expected not zero width\n");
1209     ok(rect.bottom != 0, "Expected not zero height\n");
1210
1211     SendMessage(hChild, WM_LBUTTONDOWN, 0, MAKELPARAM(rect.right, rect.bottom / 2));
1212     SendMessage(hChild, WM_MOUSEMOVE, 0, MAKELPARAM(rect.right + 20, rect.bottom / 2));
1213     SendMessage(hChild, WM_LBUTTONUP, 0, MAKELPARAM(rect.right + 20, rect.bottom / 2));
1214
1215     SendMessage(hChild, HDM_GETITEMRECT, 0, (LPARAM)&rect);
1216
1217     if (hdItem.cxy != rect.right)
1218     {
1219         win_skip("HDF_FIXEDWIDTH format not supported\n");
1220         DestroyWindow(hChild);
1221         return;
1222     }
1223
1224     /* try to adjust with message */
1225     hdItem.mask = HDI_WIDTH;
1226     hdItem.cxy = 90;
1227
1228     ret = SendMessage(hChild, HDM_SETITEM, 0, (LPARAM)&hdItem);
1229     expect(TRUE, ret);
1230
1231     rect.right = 0;
1232     SendMessage(hChild, HDM_GETITEMRECT, 0, (LPARAM)&rect);
1233     expect(90, rect.right);
1234
1235     /* hittesting doesn't report ondivider flag for HDF_FIXEDWIDTH */
1236     ht.pt.x = rect.right - 1;
1237     ht.pt.y = rect.bottom / 2;
1238     SendMessage(hChild, HDM_HITTEST, 0, (LPARAM)&ht);
1239     expect(HHT_ONHEADER, ht.flags);
1240
1241     /* try to adjust with message */
1242     hdItem.mask = HDI_FORMAT;
1243     hdItem.fmt  = 0;
1244
1245     ret = SendMessage(hChild, HDM_SETITEM, 0, (LPARAM)&hdItem);
1246     expect(TRUE, ret);
1247
1248     ht.pt.x = 90;
1249     ht.pt.y = rect.bottom / 2;
1250     SendMessage(hChild, HDM_HITTEST, 0, (LPARAM)&ht);
1251     expect(HHT_ONDIVIDER, ht.flags);
1252
1253     DestroyWindow(hChild);
1254 }
1255
1256 static void test_hds_nosizing(HWND hParent)
1257 {
1258     HWND hChild;
1259     HDITEM hdItem;
1260     DWORD ret;
1261     RECT rect;
1262     HDHITTESTINFO ht;
1263
1264     hChild = create_custom_header_control(hParent, FALSE);
1265
1266     memset(&hdItem, 0, sizeof(hdItem));
1267     hdItem.mask = HDI_WIDTH;
1268     hdItem.cxy = 80;
1269
1270     ret = SendMessage(hChild, HDM_INSERTITEM, 0, (LPARAM)&hdItem);
1271     expect(0, ret);
1272
1273     /* HDS_NOSIZING only blocks hittesting */
1274     ret = GetWindowLong(hChild, GWL_STYLE);
1275     SetWindowLong(hChild, GWL_STYLE, ret | HDS_NOSIZING);
1276
1277     /* try to change width with mouse gestures */
1278     rect.right = rect.bottom = 0;
1279     SendMessage(hChild, HDM_GETITEMRECT, 0, (LPARAM)&rect);
1280     ok(rect.right  != 0, "Expected not zero width\n");
1281     ok(rect.bottom != 0, "Expected not zero height\n");
1282
1283     SendMessage(hChild, WM_LBUTTONDOWN, 0, MAKELPARAM(rect.right, rect.bottom / 2));
1284     SendMessage(hChild, WM_MOUSEMOVE, 0, MAKELPARAM(rect.right + 20, rect.bottom / 2));
1285     SendMessage(hChild, WM_LBUTTONUP, 0, MAKELPARAM(rect.right + 20, rect.bottom / 2));
1286
1287     SendMessage(hChild, HDM_GETITEMRECT, 0, (LPARAM)&rect);
1288
1289     if (hdItem.cxy != rect.right)
1290     {
1291         win_skip("HDS_NOSIZING style not supported\n");
1292         DestroyWindow(hChild);
1293         return;
1294     }
1295
1296     /* this style doesn't set HDF_FIXEDWIDTH for items */
1297     hdItem.mask = HDI_FORMAT;
1298     ret = SendMessage(hChild, HDM_GETITEM, 0, (LPARAM)&hdItem);
1299     expect(TRUE, ret);
1300     ok(!(hdItem.fmt & HDF_FIXEDWIDTH), "Unexpected HDF_FIXEDWIDTH\n");
1301
1302     /* try to adjust with message */
1303     hdItem.mask = HDI_WIDTH;
1304     hdItem.cxy = 90;
1305
1306     ret = SendMessage(hChild, HDM_SETITEM, 0, (LPARAM)&hdItem);
1307     expect(TRUE, ret);
1308
1309     rect.right = 0;
1310     SendMessage(hChild, HDM_GETITEMRECT, 0, (LPARAM)&rect);
1311     expect(90, rect.right);
1312
1313     /* hittesting doesn't report ondivider flags for HDS_NOSIZING */
1314     ht.pt.x = rect.right - 1;
1315     ht.pt.y = rect.bottom / 2;
1316     SendMessage(hChild, HDM_HITTEST, 0, (LPARAM)&ht);
1317     expect(HHT_ONHEADER, ht.flags);
1318
1319     /* try to adjust with message */
1320     ret = GetWindowLong(hChild, GWL_STYLE);
1321     SetWindowLong(hChild, GWL_STYLE, ret & ~HDS_NOSIZING);
1322
1323     ht.pt.x = 90;
1324     ht.pt.y = rect.bottom / 2;
1325     SendMessage(hChild, HDM_HITTEST, 0, (LPARAM)&ht);
1326     expect(HHT_ONDIVIDER, ht.flags);
1327
1328     DestroyWindow(hChild);
1329 }
1330
1331 #define TEST_NMCUSTOMDRAW(draw_stage, item_spec, lparam, _left, _top, _right, _bottom) \
1332     ok(nm->dwDrawStage == draw_stage, "Invalid dwDrawStage %d vs %d\n", draw_stage, nm->dwDrawStage); \
1333     if (item_spec != -1) \
1334         ok(nm->dwItemSpec == item_spec, "Invalid dwItemSpec %d vs %ld\n", item_spec, nm->dwItemSpec); \
1335     ok(nm->lItemlParam == lparam, "Invalid lItemlParam %d vs %ld\n", lparam, nm->lItemlParam); \
1336     ok((nm->rc.top == _top && nm->rc.bottom == _bottom && nm->rc.left == _left && nm->rc.right == _right) || \
1337         broken(draw_stage != CDDS_ITEMPREPAINT), /* comctl32 < 5.80 */ \
1338         "Invalid rect (%d,%d) (%d,%d) vs (%d,%d) (%d,%d)\n", _left, _top, _right, _bottom, \
1339         nm->rc.left, nm->rc.top, nm->rc.right, nm->rc.bottom);
1340
1341 static LRESULT customdraw_1(int n, NMCUSTOMDRAW *nm)
1342 {
1343     if (nm == NULL) {  /* test ended */
1344         ok(n==1, "NM_CUSTOMDRAW messages: %d, expected: 1\n", n);
1345         return 0;
1346     }
1347
1348     switch (n)
1349     {
1350     case 0:
1351         /* don't test dwItemSpec - it's 0 no comctl5 but 1308756 on comctl6 */
1352         TEST_NMCUSTOMDRAW(CDDS_PREPAINT, -1, 0, 0, 0, 670, g_customheight);
1353         return 0;
1354     }
1355
1356     ok(FALSE, "To many custom draw messages (n=%d, nm->dwDrawStage=%d)\n", n, nm->dwDrawStage);
1357     return -1;
1358 }
1359
1360 static LRESULT customdraw_2(int n, NMCUSTOMDRAW *nm)
1361 {
1362     if (nm == NULL) {  /* test ended */
1363         ok(n==4, "NM_CUSTOMDRAW messages: %d, expected: 4\n", n);
1364         return 0;
1365     }
1366
1367     switch (n)
1368     {
1369     case 0:
1370         TEST_NMCUSTOMDRAW(CDDS_PREPAINT, -1, 0, 0, 0, 670, g_customheight);
1371         return CDRF_NOTIFYITEMDRAW;
1372     case 1:
1373         TEST_NMCUSTOMDRAW(CDDS_ITEMPREPAINT, 0, 0, 0, 0, 50, g_customheight);
1374         return 0;
1375     case 2:
1376         TEST_NMCUSTOMDRAW(CDDS_ITEMPREPAINT, 1, 5, 50, 0, 150, g_customheight);
1377         return 0;
1378     case 3:
1379         TEST_NMCUSTOMDRAW(CDDS_ITEMPREPAINT, 2, 10, 150, 0, 300, g_customheight);
1380         return 0;
1381     }
1382
1383     ok(FALSE, "To many custom draw messages (n=%d, nm->dwDrawStage=%d)\n", n, nm->dwDrawStage);
1384     return 0;
1385 }
1386
1387 static LRESULT customdraw_3(int n, NMCUSTOMDRAW *nm)
1388 {
1389     if (nm == NULL) {  /* test ended */
1390         ok(n==5, "NM_CUSTOMDRAW messages: %d, expected: 5\n", n);
1391         return 0;
1392     }
1393
1394     switch (n)
1395     {
1396     case 0:
1397         TEST_NMCUSTOMDRAW(CDDS_PREPAINT, -1, 0, 0, 0, 670, g_customheight);
1398         return CDRF_NOTIFYITEMDRAW|CDRF_NOTIFYPOSTERASE|CDRF_NOTIFYPOSTPAINT|CDRF_SKIPDEFAULT;
1399     case 1:
1400         TEST_NMCUSTOMDRAW(CDDS_ITEMPREPAINT, 0, 0, 0, 0, 50, g_customheight);
1401         return 0;
1402     case 2:
1403         TEST_NMCUSTOMDRAW(CDDS_ITEMPREPAINT, 1, 5, 50, 0, 150, g_customheight);
1404         return 0;
1405     case 3:
1406         TEST_NMCUSTOMDRAW(CDDS_ITEMPREPAINT, 2, 10, 150, 0, 300, g_customheight);
1407         return 0;
1408     case 4:
1409         TEST_NMCUSTOMDRAW(CDDS_POSTPAINT, -1, 0, 0, 0, 670, g_customheight);
1410         return 0;
1411     }
1412
1413     ok(FALSE, "To many custom draw messages (n=%d, nm->dwDrawStage=%d)\n", n, nm->dwDrawStage);
1414     return 0;
1415 }
1416
1417
1418 static LRESULT customdraw_4(int n, NMCUSTOMDRAW *nm)
1419 {
1420     if (nm == NULL) {  /* test ended */
1421         ok(n==4, "NM_CUSTOMDRAW messages: %d, expected: 4\n", n);
1422         return 0;
1423     }
1424
1425     switch (n)
1426     {
1427     case 0:
1428         TEST_NMCUSTOMDRAW(CDDS_PREPAINT, -1, 0, 0, 0, 670, g_customheight);
1429         return CDRF_NOTIFYITEMDRAW|CDRF_NOTIFYPOSTPAINT;
1430     case 1:
1431         TEST_NMCUSTOMDRAW(CDDS_ITEMPREPAINT, 0, 0, 0, 0, 50, g_customheight);
1432         return 0;
1433     case 2:
1434         TEST_NMCUSTOMDRAW(CDDS_ITEMPREPAINT, 2, 10, 150, 0, 300, g_customheight);
1435         return 0;
1436     case 3:
1437         TEST_NMCUSTOMDRAW(CDDS_POSTPAINT, -1, 0, 0, 0, 670, g_customheight);
1438         return 0;
1439     }
1440
1441     ok(FALSE, "To many custom draw messages (n=%d, nm->dwDrawStage=%d)\n", n, nm->dwDrawStage);
1442     return 0;
1443 }
1444
1445 static void run_customdraw_scenario(CUSTOMDRAWPROC proc)
1446 {
1447     g_CustomDrawProc = proc;
1448     g_CustomDrawCount = 0;
1449     InvalidateRect(hWndHeader, NULL, TRUE);
1450     UpdateWindow(hWndHeader);
1451     proc(g_CustomDrawCount, NULL);
1452     g_CustomDrawProc = NULL;
1453 }
1454
1455 static void test_customdraw(void)
1456 {
1457     int i;
1458     HDITEM item;
1459     RECT rect;
1460     CHAR name[] = "Test";
1461     hWndHeader = create_header_control();
1462     GetClientRect(hWndHeader, &rect);
1463     ok(rect.right - rect.left == 670 && rect.bottom - rect.top == g_customheight,
1464         "Tests will fail as header size is %dx%d instead of 670x%d\n",
1465         rect.right - rect.left, rect.bottom - rect.top, g_customheight);
1466
1467     for (i = 0; i < 3; i++)
1468     {
1469         ZeroMemory(&item, sizeof(item));
1470         item.mask = HDI_TEXT|HDI_WIDTH;
1471         item.cxy = 50*(i+1);
1472         item.pszText = name;
1473         item.lParam = i*5;
1474         SendMessage(hWndHeader, HDM_INSERTITEM, i, (LPARAM)&item);
1475     }
1476
1477     run_customdraw_scenario(customdraw_1);
1478     run_customdraw_scenario(customdraw_2);
1479     run_customdraw_scenario(customdraw_3);
1480
1481     ZeroMemory(&item, sizeof(item));
1482     item.mask = HDI_FORMAT;
1483     item.fmt = HDF_OWNERDRAW;
1484     SendMessage(hWndHeader, HDM_SETITEM, 1, (LPARAM)&item);
1485     g_DrawItem.CtlID = 0;
1486     g_DrawItem.CtlType = ODT_HEADER;
1487     g_DrawItem.hwndItem = hWndHeader;
1488     g_DrawItem.itemID = 1;
1489     g_DrawItem.itemState = 0;
1490     SendMessage(hWndHeader, HDM_GETITEMRECT, 1, (LPARAM)&g_DrawItem.rcItem);
1491     run_customdraw_scenario(customdraw_4);
1492     ok(g_DrawItemReceived, "WM_DRAWITEM not received\n");
1493     DestroyWindow(hWndHeader);
1494     hWndHeader = NULL;
1495     g_DrawItem.CtlType = 0;
1496     g_DrawItemReceived = FALSE;
1497 }
1498
1499 static void check_order(const int expected_id[], const int expected_order[],
1500                         int count, const char *type)
1501 {
1502     int i;
1503     HDITEMA hdi;
1504
1505     ok(getItemCount(hWndHeader) == count, "Invalid item count in order tests\n");
1506     for (i = 0; i < count; i++)
1507     {
1508         hdi.mask = HDI_LPARAM|HDI_ORDER;
1509         SendMessage(hWndHeader, HDM_GETITEMA, i, (LPARAM)&hdi);
1510         ok(hdi.lParam == expected_id[i],
1511             "Invalid item ids after '%s'- item %d has lParam %d\n", type, i, (int)hdi.lParam);
1512         ok(hdi.iOrder == expected_order[i],
1513             "Invalid item order after '%s'- item %d has iOrder %d\n", type, i, hdi.iOrder);
1514     }
1515 }
1516
1517 static void test_header_order (void)
1518 {
1519     const int rand1[] = {0, 1, 1, 0, 4};
1520     const int rand2[] = {4, 5, 6, 7, 4};
1521     const int rand3[] = {5, 5, 1, 6, 1};
1522     const int rand4[] = {1, 5, 2, 7, 6, 1, 4, 2, 3, 2};
1523     const int rand5[] = {7, 8, 5, 6, 7, 2, 1, 9, 10, 10};
1524     const int rand6[] = {2, 8, 3, 4, 0};
1525
1526     const int ids1[] = {3, 0, 2, 1, 4};
1527     const int ord1[] = {0, 1, 2, 3, 4};
1528     const int ids2[] = {3, 9, 7, 0, 2, 1, 4, 8, 6, 5};
1529     const int ord2[] = {0, 4, 7, 1, 2, 3, 9, 8, 6, 5};
1530     const int ord3[] = {0, 3, 9, 2, 1, 8, 7, 6, 5, 4};
1531     const int ids4[] = {9, 0, 1, 8, 6};
1532     const int ord4[] = {1, 0, 4, 3, 2};
1533
1534     char buffer[20];
1535     HDITEMA hdi;
1536     int i;
1537
1538     hWndHeader = create_header_control();
1539
1540     ZeroMemory(&hdi, sizeof(HDITEMA));
1541     hdi.mask = HDI_TEXT | HDI_LPARAM;
1542     hdi.pszText = buffer;
1543     strcpy(buffer, "test");
1544
1545     for (i = 0; i < 5; i++)
1546     {
1547         hdi.lParam = i;
1548         SendMessage(hWndHeader, HDM_INSERTITEMA, rand1[i], (LPARAM)&hdi);
1549         rand();
1550     }
1551     check_order(ids1, ord1, 5, "insert without iOrder");
1552
1553     hdi.mask |= HDI_ORDER;
1554     for (i = 0; i < 5; i++)
1555     {
1556         hdi.lParam = i + 5;
1557         hdi.iOrder = rand2[i];
1558         SendMessage(hWndHeader, HDM_INSERTITEMA, rand3[i], (LPARAM)&hdi);
1559         rand(); rand();
1560     }
1561     check_order(ids2, ord2, 10, "insert with order");
1562
1563     hdi.mask = HDI_ORDER;
1564     for (i=0; i<10; i++)
1565     {
1566         hdi.iOrder = rand5[i];
1567         SendMessage(hWndHeader, HDM_SETITEMA, rand4[i], (LPARAM)&hdi);
1568         rand(); rand();
1569     }
1570     check_order(ids2, ord3, 10, "setitems changing order");
1571
1572     for (i=0; i<5; i++)
1573         SendMessage(hWndHeader, HDM_DELETEITEM, rand6[i], 0);
1574     check_order(ids4, ord4, 5, "deleteitem");
1575
1576     DestroyWindow(hWndHeader);
1577 }
1578
1579 static LRESULT CALLBACK HeaderTestWndProc(HWND hWnd, UINT msg, WPARAM wParam, LPARAM lParam)
1580 {
1581     DRAWITEMSTRUCT *di;
1582     switch(msg) {
1583
1584     case WM_NOTIFY:
1585     {
1586         NMHEADERA *hdr = (NMHEADER *)lParam;
1587         EXPECTEDNOTIFY *expected;
1588         int i;
1589
1590         if (hdr->hdr.code == NM_CUSTOMDRAW)
1591             if (g_CustomDrawProc)
1592                 return g_CustomDrawProc(g_CustomDrawCount++, (NMCUSTOMDRAW*)hdr);
1593
1594         for (i=0; i<nUnexpectedNotify; i++)
1595             ok(hdr->hdr.code != unexpectedNotify[i], "Received invalid notify %d\n", hdr->hdr.code);
1596         
1597         if (nReceivedNotify >= nExpectedNotify || hdr->hdr.hwndFrom != hWndHeader )
1598             break;
1599
1600         expected = &expectedNotify[nReceivedNotify];
1601         if (hdr->hdr.code != expected->iCode)
1602             break;
1603         
1604         nReceivedNotify++;
1605         compare_items(hdr->hdr.code, &expected->hdItem, hdr->pitem, expected->fUnicode);
1606         break;
1607     }
1608
1609     case WM_DRAWITEM:
1610         di = (DRAWITEMSTRUCT *)lParam;
1611         ok(g_DrawItem.CtlType != 0, "Unexpected WM_DRAWITEM\n");
1612         if (g_DrawItem.CtlType == 0) return 0;
1613         g_DrawItemReceived = TRUE;
1614         compare(di->CtlType,   g_DrawItem.CtlType, "%d");
1615         compare(di->CtlID,     g_DrawItem.CtlID, "%d");
1616         compare(di->hwndItem,  g_DrawItem.hwndItem, "%p");
1617         compare(di->itemID,    g_DrawItem.itemID, "%d");
1618         compare(di->itemState, g_DrawItem.itemState, "%d");
1619         compare(di->rcItem.left,   g_DrawItem.rcItem.left, "%d");
1620         compare(di->rcItem.top,    g_DrawItem.rcItem.top, "%d");
1621         compare(di->rcItem.right,  g_DrawItem.rcItem.right, "%d");
1622         compare(di->rcItem.bottom, g_DrawItem.rcItem.bottom, "%d");
1623         break;
1624
1625     case WM_DESTROY:
1626         PostQuitMessage(0);
1627         break;
1628   
1629     default:
1630         return DefWindowProcA(hWnd, msg, wParam, lParam);
1631     }
1632     
1633     return 0L;
1634 }
1635
1636 static int init(void)
1637 {
1638     HMODULE hComctl32;
1639     BOOL (WINAPI *pInitCommonControlsEx)(const INITCOMMONCONTROLSEX*);
1640     WNDCLASSA wc;
1641     INITCOMMONCONTROLSEX iccex;
1642     TEXTMETRICA tm;
1643     HFONT hOldFont;
1644     HDC hdc;
1645
1646     hComctl32 = GetModuleHandleA("comctl32.dll");
1647     pInitCommonControlsEx = (void*)GetProcAddress(hComctl32, "InitCommonControlsEx");
1648     if (!pInitCommonControlsEx)
1649     {
1650         skip("InitCommonControlsEx() is missing. Skipping the tests\n");
1651         return 0;
1652     }
1653
1654     iccex.dwSize = sizeof(iccex);
1655     iccex.dwICC  = ICC_USEREX_CLASSES;
1656     pInitCommonControlsEx(&iccex);
1657
1658     wc.style = CS_HREDRAW | CS_VREDRAW;
1659     wc.cbClsExtra = 0;
1660     wc.cbWndExtra = 0;
1661     wc.hInstance = GetModuleHandleA(NULL);
1662     wc.hIcon = NULL;
1663     wc.hCursor = LoadCursorA(NULL, IDC_ARROW);
1664     wc.hbrBackground = GetSysColorBrush(COLOR_WINDOW);
1665     wc.lpszMenuName = NULL;
1666     wc.lpszClassName = "HeaderTestClass";
1667     wc.lpfnWndProc = HeaderTestWndProc;
1668     RegisterClassA(&wc);
1669
1670     /* The height of the header control depends on the height of the system font.
1671        The height of the system font is dpi dependent */
1672     hdc = GetDC(0);
1673     hOldFont = SelectObject(hdc, GetStockObject(SYSTEM_FONT));
1674     GetTextMetricsA(hdc, &tm);
1675     /* 2 dot extra space are needed for the border */
1676     g_customheight = tm.tmHeight + 2;
1677     trace("customdraw height: %d (dpi: %d)\n", g_customheight, GetDeviceCaps(hdc, LOGPIXELSY));
1678     SelectObject(hdc, hOldFont);
1679     ReleaseDC(0, hdc);
1680
1681     hHeaderParentWnd = CreateWindowExA(0, "HeaderTestClass", "Header test", WS_OVERLAPPEDWINDOW, 
1682       CW_USEDEFAULT, CW_USEDEFAULT, 672+2*GetSystemMetrics(SM_CXSIZEFRAME),
1683       226+GetSystemMetrics(SM_CYCAPTION)+2*GetSystemMetrics(SM_CYSIZEFRAME),
1684       NULL, NULL, GetModuleHandleA(NULL), 0);
1685     assert(hHeaderParentWnd != NULL);
1686     ShowWindow(hHeaderParentWnd, SW_SHOW);
1687     return 1;
1688 }
1689
1690 /* maximum 8 items allowed */
1691 static void check_orderarray(HWND hwnd, DWORD start, DWORD set, DWORD expected,
1692                              int todo, int line)
1693 {
1694     int count, i;
1695     INT order[8];
1696     DWORD ret, array = 0;
1697
1698     count = SendMessage(hwnd, HDM_GETITEMCOUNT, 0, 0);
1699
1700     /* initial order */
1701     for(i = 1; i<=count; i++)
1702         order[i-1] = start>>(4*(count-i)) & 0xf;
1703
1704     ret = SendMessage(hwnd, HDM_SETORDERARRAY, count, (LPARAM)order);
1705     ok_(__FILE__, line)(ret, "Expected HDM_SETORDERARAY to succeed, got %d\n", ret);
1706
1707     /* new order */
1708     for(i = 1; i<=count; i++)
1709         order[i-1] = set>>(4*(count-i)) & 0xf;
1710     ret = SendMessage(hwnd, HDM_SETORDERARRAY, count, (LPARAM)order);
1711     ok_(__FILE__, line)(ret, "Expected HDM_SETORDERARAY to succeed, got %d\n", ret);
1712
1713     /* check actual order */
1714     ret = SendMessage(hwnd, HDM_GETORDERARRAY, count, (LPARAM)order);
1715     ok_(__FILE__, line)(ret, "Expected HDM_GETORDERARAY to succeed, got %d\n", ret);
1716     for(i = 1; i<=count; i++)
1717         array |= order[i-1]<<(4*(count-i));
1718
1719     if (todo) {
1720     todo_wine
1721         ok_(__FILE__, line)(array == expected, "Expected %x, got %x\n", expected, array);
1722     }
1723     else
1724         ok_(__FILE__, line)(array == expected, "Expected %x, got %x\n", expected, array);
1725 }
1726
1727 static void test_hdm_orderarray(void)
1728 {
1729     HWND hwnd;
1730     INT order[5];
1731     DWORD ret;
1732
1733     hwnd = create_header_control();
1734
1735     /* three items */
1736     addItem(hwnd, 0, NULL);
1737     addItem(hwnd, 1, NULL);
1738     addItem(hwnd, 2, NULL);
1739
1740     ret = SendMessage(hwnd, HDM_GETORDERARRAY, 3, (LPARAM)order);
1741     if (!ret)
1742     {
1743         win_skip("HDM_GETORDERARRAY not implemented.\n");
1744         DestroyWindow(hwnd);
1745         return;
1746     }
1747
1748     expect(0, order[0]);
1749     expect(1, order[1]);
1750     expect(2, order[2]);
1751
1752 if (0)
1753 {
1754     /* null pointer, crashes native */
1755     ret = SendMessage(hwnd, HDM_SETORDERARRAY, 3, 0);
1756     expect(FALSE, ret);
1757 }
1758     /* count out of limits */
1759     ret = SendMessage(hwnd, HDM_SETORDERARRAY, 5, (LPARAM)order);
1760     expect(FALSE, ret);
1761     /* count out of limits */
1762     ret = SendMessage(hwnd, HDM_SETORDERARRAY, 2, (LPARAM)order);
1763     expect(FALSE, ret);
1764
1765     /* try with out of range item index */
1766     /* (0,1,2)->(1,0,3) => (1,0,2) */
1767     check_orderarray(hwnd, 0x120, 0x103, 0x102, FALSE, __LINE__);
1768     /* (1,0,2)->(3,0,1) => (0,2,1) */
1769     check_orderarray(hwnd, 0x102, 0x301, 0x021, TRUE, __LINE__);
1770     /* (0,2,1)->(2,3,1) => (2,0,1) */
1771     check_orderarray(hwnd, 0x021, 0x231, 0x201, FALSE, __LINE__);
1772
1773     /* (0,1,2)->(0,2,2) => (0,1,2) */
1774     check_orderarray(hwnd, 0x012, 0x022, 0x012, FALSE, __LINE__);
1775
1776     addItem(hwnd, 3, NULL);
1777
1778     /* (0,1,2,3)->(0,1,2,2) => (0,1,3,2) */
1779     check_orderarray(hwnd, 0x0123, 0x0122, 0x0132, FALSE, __LINE__);
1780     /* (0,1,2,3)->(0,1,3,3) => (0,1,2,3) */
1781     check_orderarray(hwnd, 0x0123, 0x0133, 0x0123, FALSE, __LINE__);
1782     /* (0,1,2,3)->(0,4,2,3) => (0,1,2,3) */
1783     check_orderarray(hwnd, 0x0123, 0x0423, 0x0123, FALSE, __LINE__);
1784     /* (0,1,2,3)->(4,0,1,2) => (0,1,3,2) */
1785     check_orderarray(hwnd, 0x0123, 0x4012, 0x0132, TRUE, __LINE__);
1786     /* (0,1,3,2)->(4,0,1,4) => (0,3,1,2) */
1787     check_orderarray(hwnd, 0x0132, 0x4014, 0x0312, TRUE, __LINE__);
1788     /* (0,1,2,3)->(4,1,0,2) => (1,0,3,2) */
1789     check_orderarray(hwnd, 0x0123, 0x4102, 0x1032, TRUE, __LINE__);
1790     /* (0,1,2,3)->(0,1,4,2) => (0,1,2,3) */
1791     check_orderarray(hwnd, 0x0123, 0x0142, 0x0132, FALSE, __LINE__);
1792     /* (0,1,2,3)->(4,4,4,4) => (0,1,2,3) */
1793     check_orderarray(hwnd, 0x0123, 0x4444, 0x0123, FALSE, __LINE__);
1794     /* (0,1,2,3)->(4,4,1,2) => (0,1,3,2) */
1795     check_orderarray(hwnd, 0x0123, 0x4412, 0x0132, TRUE, __LINE__);
1796     /* (0,1,2,3)->(4,4,4,1) => (0,2,3,1) */
1797     check_orderarray(hwnd, 0x0123, 0x4441, 0x0231, TRUE, __LINE__);
1798     /* (0,1,2,3)->(1,4,4,4) => (1,0,2,3) */
1799     check_orderarray(hwnd, 0x0123, 0x1444, 0x1023, FALSE, __LINE__);
1800     /* (0,1,2,3)->(4,2,4,1) => (0,2,3,1) */
1801     check_orderarray(hwnd, 0x0123, 0x4241, 0x0231, FALSE, __LINE__);
1802     /* (0,1,2,3)->(4,2,0,1) => (2,0,3,1) */
1803     check_orderarray(hwnd, 0x0123, 0x4201, 0x2031, TRUE, __LINE__);
1804     /* (3,2,1,0)->(4,2,0,1) => (3,2,0,1) */
1805     check_orderarray(hwnd, 0x3210, 0x4201, 0x3201, FALSE, __LINE__);
1806
1807     DestroyWindow(hwnd);
1808 }
1809
1810 START_TEST(header)
1811 {
1812     HWND parent_hwnd;
1813     ULONG_PTR ctx_cookie;
1814     HANDLE hCtx;
1815     HWND hwnd;
1816
1817     if (!init())
1818         return;
1819
1820     test_header_control();
1821     test_header_order();
1822     test_hdm_orderarray();
1823     test_customdraw();
1824
1825     DestroyWindow(hHeaderParentWnd);
1826
1827     init_msg_sequences(sequences, NUM_MSG_SEQUENCES);
1828     parent_hwnd = create_custom_parent_window();
1829     ok_sequence(sequences, PARENT_SEQ_INDEX, create_parent_wnd_seq, "create parent windows", FALSE);
1830
1831     test_hdm_index_messages(parent_hwnd);
1832     test_hdm_getitemrect(parent_hwnd);
1833     test_hdm_hittest(parent_hwnd);
1834     test_hdm_layout(parent_hwnd);
1835     test_hdm_ordertoindex(parent_hwnd);
1836     test_hdm_sethotdivider(parent_hwnd);
1837     test_hdm_imageMessages(parent_hwnd);
1838     test_hdm_filterMessages(parent_hwnd);
1839     test_hdm_unicodeformatMessages(parent_hwnd);
1840     test_hdm_bitmapmarginMessages(parent_hwnd);
1841
1842     if (!load_v6_module(&ctx_cookie, &hCtx))
1843     {
1844         DestroyWindow(parent_hwnd);
1845         return;
1846     }
1847
1848     /* this is a XP SP3 failure workaround */
1849     hwnd = CreateWindowExA(0, WC_HEADER, NULL,
1850                            WS_CHILD|WS_BORDER|WS_VISIBLE|HDS_BUTTONS|HDS_HORZ,
1851                            0, 0, 100, 100,
1852                            parent_hwnd, NULL, GetModuleHandleA(NULL), NULL);
1853
1854     if (!IsWindow(hwnd))
1855     {
1856         win_skip("FIXME: failed to create Header window.\n");
1857         unload_v6_module(ctx_cookie, hCtx);
1858         DestroyWindow(parent_hwnd);
1859         return;
1860     }
1861     else
1862         DestroyWindow(hwnd);
1863
1864     /* comctl32 version 6 tests start here */
1865     test_hdf_fixedwidth(parent_hwnd);
1866     test_hds_nosizing(parent_hwnd);
1867
1868     unload_v6_module(ctx_cookie, hCtx);
1869
1870     DestroyWindow(parent_hwnd);
1871 }