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