jscript: Added SCRIPTITEM_ISVISIBLE flag implementation.
[wine] / dlls / comctl32 / tests / tab.c
1 /* Unit test suite for tab control.
2  *
3  * Copyright 2003 Vitaliy Margolen
4  * Copyright 2007 Hagop Hagopian
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 #include <assert.h>
22 #include <windows.h>
23 #include <commctrl.h>
24 #include <stdio.h>
25
26 #include "wine/test.h"
27 #include "msg.h"
28
29 #define DEFAULT_MIN_TAB_WIDTH 54
30 #define TAB_PADDING_X 6
31 #define EXTRA_ICON_PADDING 3
32 #define MAX_TABLEN 32
33
34 #define NUM_MSG_SEQUENCES  2
35 #define PARENT_SEQ_INDEX   0
36 #define TAB_SEQ_INDEX      1
37
38 #define expect(expected, got) ok ( expected == got, "Expected %d, got %d\n", expected, got)
39 #define expect_str(expected, got)\
40  ok ( strcmp(expected, got) == 0, "Expected '%s', got '%s'\n", expected, got)
41
42 #define TabWidthPadded(padd_x, num) (DEFAULT_MIN_TAB_WIDTH - (TAB_PADDING_X - (padd_x)) * num)
43
44 #define TabCheckSetSize(hwnd, SetWidth, SetHeight, ExpWidth, ExpHeight, Msg)\
45     SendMessage (hwnd, TCM_SETITEMSIZE, 0,\
46         (LPARAM) MAKELPARAM((SetWidth >= 0) ? SetWidth:0, (SetHeight >= 0) ? SetHeight:0));\
47     if (winetest_interactive) RedrawWindow (hwnd, NULL, 0, RDW_UPDATENOW);\
48     CheckSize(hwnd, ExpWidth, ExpHeight, Msg);
49
50 #define CheckSize(hwnd,width,height,msg)\
51     SendMessage (hwnd, TCM_GETITEMRECT, 0, (LPARAM) &rTab);\
52     if ((width  >= 0) && (height < 0))\
53         ok (width  == rTab.right  - rTab.left, "%s: Expected width [%d] got [%d]\n",\
54         msg, (int)width,  rTab.right  - rTab.left);\
55     else if ((height >= 0) && (width  < 0))\
56         ok (height == rTab.bottom - rTab.top,  "%s: Expected height [%d] got [%d]\n",\
57         msg, (int)height, rTab.bottom - rTab.top);\
58     else\
59         ok ((width  == rTab.right  - rTab.left) &&\
60             (height == rTab.bottom - rTab.top ),\
61             "%s: Expected [%d,%d] got [%d,%d]\n", msg, (int)width, (int)height,\
62             rTab.right - rTab.left, rTab.bottom - rTab.top);
63
64 static HFONT hFont = 0;
65
66 static struct msg_sequence *sequences[NUM_MSG_SEQUENCES];
67
68 static const struct message create_parent_wnd_seq[] = {
69     { WM_GETMINMAXINFO, sent },
70     { WM_NCCREATE, sent },
71     { WM_NCCALCSIZE, sent|wparam, 0 },
72     { WM_CREATE, sent },
73     { WM_SHOWWINDOW, sent|wparam, 1 },
74     { WM_WINDOWPOSCHANGING, sent|wparam, 0 },
75     { WM_WINDOWPOSCHANGING, sent|wparam, 0 },
76     { WM_ACTIVATEAPP, sent|wparam, 1 },
77     { WM_NCACTIVATE, sent|wparam, 1 },
78     { WM_ACTIVATE, sent|wparam, 1 },
79     { WM_IME_SETCONTEXT, sent|wparam|defwinproc|optional, 1 },
80     { WM_IME_NOTIFY, sent|defwinproc|optional },
81     { WM_SETFOCUS, sent|wparam|defwinproc, 0 },
82     /* Win9x adds SWP_NOZORDER below */
83     { WM_WINDOWPOSCHANGED, sent},
84     { WM_NCCALCSIZE, sent|wparam|optional, 1 },
85     { WM_SIZE, sent },
86     { WM_MOVE, sent },
87     { 0 }
88 };
89
90 static const struct message add_tab_to_parent[] = {
91     { TCM_INSERTITEMA, sent },
92     { TCM_INSERTITEMA, sent },
93     { WM_NOTIFYFORMAT, sent|defwinproc },
94     { WM_QUERYUISTATE, sent|wparam|lparam|defwinproc|optional, 0, 0 },
95     { WM_PARENTNOTIFY, sent|defwinproc },
96     { TCM_INSERTITEMA, sent },
97     { TCM_INSERTITEMA, sent },
98     { TCM_INSERTITEMA, sent },
99     { 0 }
100 };
101
102 static const struct message add_tab_to_parent_interactive[] = {
103     { TCM_INSERTITEMA, sent },
104     { TCM_INSERTITEMA, sent },
105     { WM_NOTIFYFORMAT, sent|defwinproc },
106     { WM_QUERYUISTATE, sent|wparam|lparam|defwinproc, 0, 0 },
107     { WM_PARENTNOTIFY, sent|defwinproc },
108     { TCM_INSERTITEMA, sent },
109     { TCM_INSERTITEMA, sent },
110     { TCM_INSERTITEMA, sent },
111     { WM_SHOWWINDOW, sent},
112     { WM_WINDOWPOSCHANGING, sent},
113     { WM_WINDOWPOSCHANGING, sent},
114     { WM_NCACTIVATE, sent},
115     { WM_ACTIVATE, sent},
116     { WM_IME_SETCONTEXT, sent|defwinproc|optional},
117     { WM_IME_NOTIFY, sent|defwinproc|optional},
118     { WM_SETFOCUS, sent|defwinproc},
119     { WM_WINDOWPOSCHANGED, sent},
120     { WM_SIZE, sent},
121     { WM_MOVE, sent},
122     { 0 }
123 };
124
125 static const struct message add_tab_control_parent_seq[] = {
126     { WM_NOTIFYFORMAT, sent },
127     { WM_QUERYUISTATE, sent|wparam|lparam|optional, 0, 0 },
128     { 0 }
129 };
130
131 static const struct message add_tab_control_parent_seq_interactive[] = {
132     { WM_NOTIFYFORMAT, sent },
133     { WM_QUERYUISTATE, sent|wparam|lparam, 0, 0 },
134     { WM_WINDOWPOSCHANGING, sent|optional},
135     { WM_NCACTIVATE, sent},
136     { WM_ACTIVATE, sent},
137     { WM_WINDOWPOSCHANGING, sent|optional},
138     { WM_KILLFOCUS, sent},
139     { WM_IME_SETCONTEXT, sent|optional},
140     { WM_IME_NOTIFY, sent|optional},
141     { 0 }
142 };
143
144 static const struct message empty_sequence[] = {
145     { 0 }
146 };
147
148 static const struct message set_min_tab_width_seq[] = {
149     { TCM_SETMINTABWIDTH, sent|wparam, 0 },
150     { TCM_SETMINTABWIDTH, sent|wparam, 0 },
151     { 0 }
152 };
153
154 static const struct message get_item_count_seq[] = {
155     { TCM_GETITEMCOUNT, sent|wparam|lparam, 0, 0 },
156     { 0 }
157 };
158
159 static const struct message get_row_count_seq[] = {
160     { TCM_GETROWCOUNT, sent|wparam|lparam, 0, 0 },
161     { 0 }
162 };
163
164 static const struct message get_item_rect_seq[] = {
165     { TCM_GETITEMRECT, sent },
166     { TCM_GETITEMRECT, sent },
167     { 0 }
168 };
169
170 static const struct message getset_cur_focus_seq[] = {
171     { TCM_SETCURFOCUS, sent|lparam, 0 },
172     { TCM_GETCURFOCUS, sent|wparam|lparam, 0, 0 },
173     { TCM_SETCURFOCUS, sent|lparam, 0 },
174     { TCM_GETCURFOCUS, sent|wparam|lparam, 0, 0 },
175     { TCM_SETCURSEL, sent|lparam, 0 },
176     { TCM_SETCURFOCUS, sent|lparam, 0 },
177     { TCM_GETCURFOCUS, sent|wparam|lparam, 0, 0 },
178     { 0 }
179 };
180
181 static const struct message getset_cur_sel_seq[] = {
182     { TCM_SETCURSEL, sent|lparam, 0 },
183     { TCM_GETCURSEL, sent|wparam|lparam, 0, 0 },
184     { TCM_GETCURFOCUS, sent|wparam|lparam, 0, 0 },
185     { TCM_SETCURSEL, sent|lparam, 0 },
186     { TCM_GETCURSEL, sent|wparam|lparam, 0, 0 },
187     { TCM_SETCURSEL, sent|lparam, 0 },
188     { TCM_SETCURSEL, sent|lparam, 0 },
189     { TCM_GETCURFOCUS, sent|wparam|lparam, 0, 0 },
190     { 0 }
191 };
192
193 static const struct message getset_extended_style_seq[] = {
194     { TCM_GETEXTENDEDSTYLE, sent|wparam|lparam, 0, 0 },
195     { TCM_SETEXTENDEDSTYLE, sent },
196     { TCM_GETEXTENDEDSTYLE, sent|wparam|lparam, 0, 0 },
197     { TCM_SETEXTENDEDSTYLE, sent },
198     { TCM_GETEXTENDEDSTYLE, sent|wparam|lparam, 0, 0 },
199     { 0 }
200 };
201
202 static const struct message getset_unicode_format_seq[] = {
203     { CCM_SETUNICODEFORMAT, sent|lparam, 0 },
204     { CCM_GETUNICODEFORMAT, sent|wparam|lparam, 0, 0 },
205     { CCM_SETUNICODEFORMAT, sent|lparam, 0 },
206     { CCM_GETUNICODEFORMAT, sent|wparam|lparam, 0, 0 },
207     { CCM_SETUNICODEFORMAT, sent|lparam, 0 },
208     { 0 }
209 };
210
211 static const struct message getset_item_seq[] = {
212     { TCM_SETITEMA, sent },
213     { TCM_GETITEMA, sent },
214     { TCM_GETITEMA, sent },
215     { 0 }
216 };
217
218 static const struct message getset_tooltip_seq[] = {
219     { WM_NOTIFYFORMAT, sent|optional },
220     { WM_QUERYUISTATE, sent|wparam|lparam|optional, 0, 0 },
221     { WM_WINDOWPOSCHANGING, sent|wparam, 0 },
222     { WM_NOTIFYFORMAT, sent|optional },
223     { TCM_SETTOOLTIPS, sent|lparam, 0 },
224     { TCM_GETTOOLTIPS, sent|wparam|lparam, 0, 0 },
225     { TCM_SETTOOLTIPS, sent|lparam, 0 },
226     { TCM_GETTOOLTIPS, sent|wparam|lparam, 0, 0 },
227     { 0 }
228 };
229
230 static const struct message getset_tooltip_parent_seq[] = {
231     { WM_WINDOWPOSCHANGING, sent|wparam, 0 },
232     { 0 }
233 };
234
235 static const struct message insert_focus_seq[] = {
236     { TCM_GETITEMCOUNT, sent|wparam|lparam, 0, 0 },
237     { TCM_GETCURFOCUS, sent|wparam|lparam, 0, 0 },
238     { TCM_INSERTITEM, sent|wparam, 1 },
239     { TCM_GETITEMCOUNT, sent|wparam|lparam, 0, 0 },
240     { TCM_GETCURFOCUS, sent|wparam|lparam, 0, 0 },
241     { TCM_INSERTITEM, sent|wparam, 2 },
242     { WM_NOTIFYFORMAT, sent|defwinproc, },
243     { WM_QUERYUISTATE, sent|defwinproc|optional, },
244     { WM_PARENTNOTIFY, sent|defwinproc, },
245     { TCM_GETITEMCOUNT, sent|wparam|lparam, 0, 0 },
246     { TCM_GETCURFOCUS, sent|wparam|lparam, 0, 0 },
247     { TCM_SETCURFOCUS, sent|wparam|lparam, -1, 0 },
248     { TCM_GETCURFOCUS, sent|wparam|lparam, 0, 0 },
249     { TCM_INSERTITEM, sent|wparam, 3 },
250     { TCM_GETCURFOCUS, sent|wparam|lparam, 0, 0 },
251     { 0 }
252 };
253
254 static const struct message delete_focus_seq[] = {
255     { TCM_GETITEMCOUNT, sent|wparam|lparam, 0, 0 },
256     { TCM_GETCURFOCUS, sent|wparam|lparam, 0, 0 },
257     { TCM_DELETEITEM, sent|wparam|lparam, 1, 0 },
258     { TCM_GETITEMCOUNT, sent|wparam|lparam, 0, 0 },
259     { TCM_GETCURFOCUS, sent|wparam|lparam, 0, 0 },
260     { TCM_SETCURFOCUS, sent|wparam|lparam, -1, 0 },
261     { TCM_GETCURFOCUS, sent|wparam|lparam, 0, 0 },
262     { TCM_DELETEITEM, sent|wparam|lparam, 0, 0 },
263     { TCM_GETITEMCOUNT, sent|wparam|lparam, 0, 0 },
264     { TCM_GETCURFOCUS, sent|wparam|lparam, 0, 0 },
265     { 0 }
266 };
267
268
269 static HWND
270 create_tabcontrol (DWORD style, DWORD mask)
271 {
272     HWND handle;
273     TCITEM tcNewTab;
274     static char text1[] = "Tab 1",
275     text2[] = "Wide Tab 2",
276     text3[] = "T 3";
277
278     handle = CreateWindow (
279         WC_TABCONTROLA,
280         "TestTab",
281         WS_CLIPSIBLINGS | WS_CLIPCHILDREN | TCS_FOCUSNEVER | style,
282         10, 10, 300, 100,
283         NULL, NULL, NULL, 0);
284
285     assert (handle);
286
287     SetWindowLong(handle, GWL_STYLE, WS_CLIPSIBLINGS | WS_CLIPCHILDREN | TCS_FOCUSNEVER | style);
288     SendMessage (handle, WM_SETFONT, 0, (LPARAM) hFont);
289
290     tcNewTab.mask = mask;
291     tcNewTab.pszText = text1;
292     tcNewTab.iImage = 0;
293     SendMessage (handle, TCM_INSERTITEM, 0, (LPARAM) &tcNewTab);
294     tcNewTab.pszText = text2;
295     tcNewTab.iImage = 1;
296     SendMessage (handle, TCM_INSERTITEM, 1, (LPARAM) &tcNewTab);
297     tcNewTab.pszText = text3;
298     tcNewTab.iImage = 2;
299     SendMessage (handle, TCM_INSERTITEM, 2, (LPARAM) &tcNewTab);
300
301     if (winetest_interactive)
302     {
303         ShowWindow (handle, SW_SHOW);
304         RedrawWindow (handle, NULL, 0, RDW_UPDATENOW);
305         Sleep (1000);
306     }
307
308     return handle;
309 }
310
311 static LRESULT WINAPI parentWindowProcess(HWND hwnd, UINT message, WPARAM wParam, LPARAM lParam)
312 {
313     static long defwndproc_counter = 0;
314     LRESULT ret;
315     struct message msg;
316
317     /* do not log painting messages */
318     if (message != WM_PAINT &&
319         message != WM_ERASEBKGND &&
320         message != WM_NCPAINT &&
321         message != WM_NCHITTEST &&
322         message != WM_GETTEXT &&
323         message != WM_GETICON &&
324         message != WM_DEVICECHANGE)
325     {
326         trace("parent: %p, %04x, %08lx, %08lx\n", hwnd, message, wParam, lParam);
327
328         msg.message = message;
329         msg.flags = sent|wparam|lparam;
330         if (defwndproc_counter) msg.flags |= defwinproc;
331         msg.wParam = wParam;
332         msg.lParam = lParam;
333         add_message(sequences, PARENT_SEQ_INDEX, &msg);
334     }
335
336     defwndproc_counter++;
337     ret = DefWindowProcA(hwnd, message, wParam, lParam);
338     defwndproc_counter--;
339
340     return ret;
341 }
342
343 static BOOL registerParentWindowClass(void)
344 {
345     WNDCLASSA cls;
346
347     cls.style = 0;
348     cls.lpfnWndProc = parentWindowProcess;
349     cls.cbClsExtra = 0;
350     cls.cbWndExtra = 0;
351     cls.hInstance = GetModuleHandleA(NULL);
352     cls.hIcon = 0;
353     cls.hCursor = LoadCursorA(0, (LPSTR)IDC_ARROW);
354     cls.hbrBackground = GetStockObject(WHITE_BRUSH);
355     cls.lpszMenuName = NULL;
356     cls.lpszClassName = "Tab test parent class";
357     return RegisterClassA(&cls);
358 }
359
360 static HWND createParentWindow(void)
361 {
362     if (!registerParentWindowClass())
363         return NULL;
364
365     return CreateWindowEx(0, "Tab test parent class",
366                           "Tab test parent window",
367                           WS_CAPTION | WS_SYSMENU | WS_MINIMIZEBOX |
368                           WS_MAXIMIZEBOX | WS_VISIBLE,
369                           0, 0, 100, 100,
370                           GetDesktopWindow(), NULL, GetModuleHandleA(NULL), NULL);
371 }
372
373 struct subclass_info
374 {
375     WNDPROC oldproc;
376 };
377
378 static LRESULT WINAPI tabSubclassProcess(HWND hwnd, UINT message, WPARAM wParam, LPARAM lParam)
379 {
380     struct subclass_info *info = (struct subclass_info *)GetWindowLongPtrA(hwnd, GWLP_USERDATA);
381     static long defwndproc_counter = 0;
382     LRESULT ret;
383     struct message msg;
384
385     /* do not log painting messages */
386     if (message != WM_PAINT &&
387         message != WM_ERASEBKGND &&
388         message != WM_NCPAINT &&
389         message != WM_NCHITTEST &&
390         message != WM_GETTEXT &&
391         message != WM_GETICON &&
392         message != WM_DEVICECHANGE)
393     {
394         trace("tab: %p, %04x, %08lx, %08lx\n", hwnd, message, wParam, lParam);
395
396         msg.message = message;
397         msg.flags = sent|wparam|lparam;
398         if (defwndproc_counter) msg.flags |= defwinproc;
399         msg.wParam = wParam;
400         msg.lParam = lParam;
401         add_message(sequences, TAB_SEQ_INDEX, &msg);
402     }
403
404     defwndproc_counter++;
405     ret = CallWindowProcA(info->oldproc, hwnd, message, wParam, lParam);
406     defwndproc_counter--;
407
408     return ret;
409 }
410
411 static HWND createFilledTabControl(HWND parent_wnd, DWORD style, DWORD mask, INT nTabs)
412 {
413     HWND tabHandle;
414     TCITEM tcNewTab;
415     struct subclass_info *info;
416     RECT rect;
417     INT i;
418
419     info = HeapAlloc(GetProcessHeap(), 0, sizeof(struct subclass_info));
420     if (!info)
421         return NULL;
422
423     GetClientRect(parent_wnd, &rect);
424
425     tabHandle = CreateWindow (
426         WC_TABCONTROLA,
427         "TestTab",
428         WS_CLIPSIBLINGS | WS_CLIPCHILDREN | TCS_FOCUSNEVER | style,
429         0, 0, rect.right, rect.bottom,
430         parent_wnd, NULL, NULL, 0);
431
432     assert(tabHandle);
433
434     info->oldproc = (WNDPROC)SetWindowLongPtrA(tabHandle, GWLP_WNDPROC, (LONG_PTR)tabSubclassProcess);
435     SetWindowLongPtrA(tabHandle, GWLP_USERDATA, (LONG_PTR)info);
436
437     tcNewTab.mask = mask;
438
439     for (i = 0; i < nTabs; i++)
440     {
441         char tabName[MAX_TABLEN];
442
443         sprintf(tabName, "Tab %d", i+1);
444         tcNewTab.pszText = tabName;
445         tcNewTab.iImage = i;
446         SendMessage (tabHandle, TCM_INSERTITEM, i, (LPARAM) &tcNewTab);
447     }
448
449     if (winetest_interactive)
450     {
451         ShowWindow (tabHandle, SW_SHOW);
452         RedrawWindow (tabHandle, NULL, 0, RDW_UPDATENOW);
453         Sleep (1000);
454     }
455
456     return tabHandle;
457 }
458
459 static HWND create_tooltip (HWND hTab, char toolTipText[])
460 {
461     HWND hwndTT;
462
463     TOOLINFO ti;
464     LPTSTR lptstr = toolTipText;
465     RECT rect;
466
467     /* Creating a tooltip window*/
468     hwndTT = CreateWindowEx(
469         WS_EX_TOPMOST,
470         TOOLTIPS_CLASS,
471         NULL,
472         WS_POPUP | TTS_NOPREFIX | TTS_ALWAYSTIP,
473         CW_USEDEFAULT, CW_USEDEFAULT, CW_USEDEFAULT, CW_USEDEFAULT,
474         hTab, NULL, 0, NULL);
475
476     SetWindowPos(
477         hwndTT,
478         HWND_TOPMOST,
479         0, 0, 0, 0,
480         SWP_NOMOVE | SWP_NOSIZE | SWP_NOACTIVATE);
481
482     GetClientRect (hTab, &rect);
483
484     /* Initialize members of toolinfo*/
485     ti.cbSize = sizeof(TOOLINFO);
486     ti.uFlags = TTF_SUBCLASS;
487     ti.hwnd = hTab;
488     ti.hinst = 0;
489     ti.uId = 0;
490     ti.lpszText = lptstr;
491
492     ti.rect = rect;
493
494     /* Add toolinfo structure to the tooltip control */
495     SendMessage(hwndTT, TTM_ADDTOOL, 0, (LPARAM) (LPTOOLINFO) &ti);
496
497     return hwndTT;
498 }
499
500 static void test_tab(INT nMinTabWidth)
501 {
502     HWND hwTab;
503     RECT rTab;
504     HIMAGELIST himl = ImageList_Create(21, 21, ILC_COLOR, 3, 4);
505     SIZE size;
506     HDC hdc;
507     HFONT hOldFont;
508     INT i, dpi;
509
510     hwTab = create_tabcontrol(TCS_FIXEDWIDTH, TCIF_TEXT|TCIF_IMAGE);
511     SendMessage(hwTab, TCM_SETMINTABWIDTH, 0, nMinTabWidth);
512
513     hdc = GetDC(hwTab);
514     dpi = GetDeviceCaps(hdc, LOGPIXELSX);
515     hOldFont = SelectObject(hdc, (HFONT)SendMessage(hwTab, WM_GETFONT, 0, 0));
516     GetTextExtentPoint32A(hdc, "Tab 1", strlen("Tab 1"), &size);
517     trace("Tab1 text size: size.cx=%d size.cy=%d\n", size.cx, size.cy);
518     SelectObject(hdc, hOldFont);
519     ReleaseDC(hwTab, hdc);
520
521     trace ("  TCS_FIXEDWIDTH tabs no icon...\n");
522     CheckSize(hwTab, dpi, -1, "default width");
523     TabCheckSetSize(hwTab, 50, 20, 50, 20, "set size");
524     TabCheckSetSize(hwTab, 0, 1, 0, 1, "min size");
525
526     SendMessage(hwTab, TCM_SETIMAGELIST, 0, (LPARAM)himl);
527
528     trace ("  TCS_FIXEDWIDTH tabs with icon...\n");
529     TabCheckSetSize(hwTab, 50, 30, 50, 30, "set size > icon");
530     TabCheckSetSize(hwTab, 20, 20, 25, 20, "set size < icon");
531     TabCheckSetSize(hwTab, 0, 1, 25, 1, "min size");
532
533     DestroyWindow (hwTab);
534
535     hwTab = create_tabcontrol(TCS_FIXEDWIDTH | TCS_BUTTONS, TCIF_TEXT|TCIF_IMAGE);
536     SendMessage(hwTab, TCM_SETMINTABWIDTH, 0, nMinTabWidth);
537
538     hdc = GetDC(hwTab);
539     dpi = GetDeviceCaps(hdc, LOGPIXELSX);
540     ReleaseDC(hwTab, hdc);
541     trace ("  TCS_FIXEDWIDTH buttons no icon...\n");
542     CheckSize(hwTab, dpi, -1, "default width");
543     TabCheckSetSize(hwTab, 20, 20, 20, 20, "set size 1");
544     TabCheckSetSize(hwTab, 10, 50, 10, 50, "set size 2");
545     TabCheckSetSize(hwTab, 0, 1, 0, 1, "min size");
546
547     SendMessage(hwTab, TCM_SETIMAGELIST, 0, (LPARAM)himl);
548
549     trace ("  TCS_FIXEDWIDTH buttons with icon...\n");
550     TabCheckSetSize(hwTab, 50, 30, 50, 30, "set size > icon");
551     TabCheckSetSize(hwTab, 20, 20, 25, 20, "set size < icon");
552     TabCheckSetSize(hwTab, 0, 1, 25, 1, "min size");
553     SendMessage(hwTab, TCM_SETPADDING, 0, MAKELPARAM(4,4));
554     TabCheckSetSize(hwTab, 0, 1, 25, 1, "set padding, min size");
555
556     DestroyWindow (hwTab);
557
558     hwTab = create_tabcontrol(TCS_FIXEDWIDTH | TCS_BOTTOM, TCIF_TEXT|TCIF_IMAGE);
559     SendMessage(hwTab, TCM_SETMINTABWIDTH, 0, nMinTabWidth);
560
561     hdc = GetDC(hwTab);
562     dpi = GetDeviceCaps(hdc, LOGPIXELSX);
563     ReleaseDC(hwTab, hdc);
564     trace ("  TCS_FIXEDWIDTH | TCS_BOTTOM tabs...\n");
565     CheckSize(hwTab, dpi, -1, "no icon, default width");
566
567     TabCheckSetSize(hwTab, 20, 20, 20, 20, "no icon, set size 1");
568     TabCheckSetSize(hwTab, 10, 50, 10, 50, "no icon, set size 2");
569     TabCheckSetSize(hwTab, 0, 1, 0, 1, "no icon, min size");
570
571     SendMessage(hwTab, TCM_SETIMAGELIST, 0, (LPARAM)himl);
572
573     TabCheckSetSize(hwTab, 50, 30, 50, 30, "with icon, set size > icon");
574     TabCheckSetSize(hwTab, 20, 20, 25, 20, "with icon, set size < icon");
575     TabCheckSetSize(hwTab, 0, 1, 25, 1, "with icon, min size");
576     SendMessage(hwTab, TCM_SETPADDING, 0, MAKELPARAM(4,4));
577     TabCheckSetSize(hwTab, 0, 1, 25, 1, "set padding, min size");
578
579     DestroyWindow (hwTab);
580
581     hwTab = create_tabcontrol(0, TCIF_TEXT|TCIF_IMAGE);
582     SendMessage(hwTab, TCM_SETMINTABWIDTH, 0, nMinTabWidth);
583
584     trace ("  non fixed width, with text...\n");
585     CheckSize(hwTab, max(size.cx +TAB_PADDING_X*2, (nMinTabWidth < 0) ? DEFAULT_MIN_TAB_WIDTH : nMinTabWidth), -1,
586               "no icon, default width");
587     for (i=0; i<8; i++)
588     {
589         INT nTabWidth = (nMinTabWidth < 0) ? TabWidthPadded(i, 2) : nMinTabWidth;
590
591         SendMessage(hwTab, TCM_SETIMAGELIST, 0, 0);
592         SendMessage(hwTab, TCM_SETPADDING, 0, MAKELPARAM(i,i));
593
594         TabCheckSetSize(hwTab, 50, 20, max(size.cx + i*2, nTabWidth), 20, "no icon, set size");
595         TabCheckSetSize(hwTab, 0, 1, max(size.cx + i*2, nTabWidth), 1, "no icon, min size");
596
597         SendMessage(hwTab, TCM_SETIMAGELIST, 0, (LPARAM)himl);
598         nTabWidth = (nMinTabWidth < 0) ? TabWidthPadded(i, 3) : nMinTabWidth;
599
600         TabCheckSetSize(hwTab, 50, 30, max(size.cx + 21 + i*3, nTabWidth), 30, "with icon, set size > icon");
601         TabCheckSetSize(hwTab, 20, 20, max(size.cx + 21 + i*3, nTabWidth), 20, "with icon, set size < icon");
602         TabCheckSetSize(hwTab, 0, 1, max(size.cx + 21 + i*3, nTabWidth), 1, "with icon, min size");
603     }
604     DestroyWindow (hwTab);
605
606     hwTab = create_tabcontrol(0, TCIF_IMAGE);
607     SendMessage(hwTab, TCM_SETMINTABWIDTH, 0, nMinTabWidth);
608
609     trace ("  non fixed width, no text...\n");
610     CheckSize(hwTab, (nMinTabWidth < 0) ? DEFAULT_MIN_TAB_WIDTH : nMinTabWidth, -1, "no icon, default width");
611     for (i=0; i<8; i++)
612     {
613         INT nTabWidth = (nMinTabWidth < 0) ? TabWidthPadded(i, 2) : nMinTabWidth;
614
615         SendMessage(hwTab, TCM_SETIMAGELIST, 0, 0);
616         SendMessage(hwTab, TCM_SETPADDING, 0, MAKELPARAM(i,i));
617
618         TabCheckSetSize(hwTab, 50, 20, nTabWidth, 20, "no icon, set size");
619         TabCheckSetSize(hwTab, 0, 1, nTabWidth, 1, "no icon, min size");
620
621         SendMessage(hwTab, TCM_SETIMAGELIST, 0, (LPARAM)himl);
622         if (i > 1 && nMinTabWidth > 0 && nMinTabWidth < DEFAULT_MIN_TAB_WIDTH)
623             nTabWidth += EXTRA_ICON_PADDING *(i-1);
624
625         TabCheckSetSize(hwTab, 50, 30, nTabWidth, 30, "with icon, set size > icon");
626         TabCheckSetSize(hwTab, 20, 20, nTabWidth, 20, "with icon, set size < icon");
627         TabCheckSetSize(hwTab, 0, 1, nTabWidth, 1, "with icon, min size");
628     }
629
630     DestroyWindow (hwTab);
631
632     ImageList_Destroy(himl);
633     DeleteObject(hFont);
634 }
635
636 static void test_getters_setters(HWND parent_wnd, INT nTabs)
637 {
638     HWND hTab;
639     RECT rTab;
640     INT nTabsRetrieved;
641     INT rowCount;
642     INT dpi;
643     HDC hdc;
644
645     ok(parent_wnd != NULL, "no parent window!\n");
646     flush_sequences(sequences, NUM_MSG_SEQUENCES);
647
648     hTab = createFilledTabControl(parent_wnd, TCS_FIXEDWIDTH, TCIF_TEXT|TCIF_IMAGE, nTabs);
649     ok(hTab != NULL, "Failed to create tab control\n");
650
651     if(!winetest_interactive)
652         ok_sequence(sequences, TAB_SEQ_INDEX, add_tab_to_parent,
653                     "Tab sequence, after adding tab control to parent", TRUE);
654     else
655         ok_sequence(sequences, TAB_SEQ_INDEX, add_tab_to_parent_interactive,
656                     "Tab sequence, after adding tab control to parent", TRUE);
657
658     if(!winetest_interactive)
659         ok_sequence(sequences, PARENT_SEQ_INDEX, add_tab_control_parent_seq,
660                     "Parent after sequence, adding tab control to parent", TRUE);
661     else
662         ok_sequence(sequences, PARENT_SEQ_INDEX, add_tab_control_parent_seq_interactive,
663                     "Parent after sequence, adding tab control to parent", TRUE);
664
665     flush_sequences(sequences, NUM_MSG_SEQUENCES);
666     todo_wine{
667         expect(DEFAULT_MIN_TAB_WIDTH, (int)SendMessage(hTab, TCM_SETMINTABWIDTH, 0, -1));
668     }
669     ok_sequence(sequences, TAB_SEQ_INDEX, set_min_tab_width_seq, "Set minTabWidth test sequence", FALSE);
670     ok_sequence(sequences, PARENT_SEQ_INDEX, empty_sequence, "Set minTabWidth test parent sequence", FALSE);
671
672     /* Testing GetItemCount */
673     flush_sequences(sequences, NUM_MSG_SEQUENCES);
674     nTabsRetrieved = SendMessage(hTab, TCM_GETITEMCOUNT, 0, 0);
675     expect(nTabs, nTabsRetrieved);
676     ok_sequence(sequences, TAB_SEQ_INDEX, get_item_count_seq, "Get itemCount test sequence", FALSE);
677     ok_sequence(sequences, PARENT_SEQ_INDEX, empty_sequence, "Getset itemCount test parent sequence", FALSE);
678
679     /* Testing GetRowCount */
680     flush_sequences(sequences, NUM_MSG_SEQUENCES);
681     rowCount = SendMessage(hTab, TCM_GETROWCOUNT, 0, 0);
682     expect(1, rowCount);
683     ok_sequence(sequences, TAB_SEQ_INDEX, get_row_count_seq, "Get rowCount test sequence", FALSE);
684     ok_sequence(sequences, PARENT_SEQ_INDEX, empty_sequence, "Get rowCount test parent sequence", FALSE);
685
686     /* Testing GetItemRect */
687     flush_sequences(sequences, NUM_MSG_SEQUENCES);
688     ok(SendMessage(hTab, TCM_GETITEMRECT, 0, (LPARAM) &rTab), "GetItemRect failed.\n");
689
690     hdc = GetDC(hTab);
691     dpi = GetDeviceCaps(hdc, LOGPIXELSX);
692     ReleaseDC(hTab, hdc);
693     CheckSize(hTab, dpi, -1 , "Default Width");
694     ok_sequence(sequences, TAB_SEQ_INDEX, get_item_rect_seq, "Get itemRect test sequence", FALSE);
695     ok_sequence(sequences, PARENT_SEQ_INDEX, empty_sequence, "Get itemRect test parent sequence", FALSE);
696
697     /* Testing CurFocus */
698     {
699         INT focusIndex;
700
701         flush_sequences(sequences, NUM_MSG_SEQUENCES);
702
703         /* Testing CurFocus with largest appropriate value */
704         SendMessage(hTab, TCM_SETCURFOCUS, nTabs-1, 0);
705         focusIndex = SendMessage(hTab, TCM_GETCURFOCUS, 0, 0);
706             expect(nTabs-1, focusIndex);
707
708         /* Testing CurFocus with negative value */
709         SendMessage(hTab, TCM_SETCURFOCUS, -10, 0);
710         focusIndex = SendMessage(hTab, TCM_GETCURFOCUS, 0, 0);
711             expect(-1, focusIndex);
712
713         /* Testing CurFocus with value larger than number of tabs */
714         focusIndex = SendMessage(hTab, TCM_SETCURSEL, 1, 0);
715         todo_wine{
716             expect(-1, focusIndex);
717         }
718
719         SendMessage(hTab, TCM_SETCURFOCUS, nTabs+1, 0);
720         focusIndex = SendMessage(hTab, TCM_GETCURFOCUS, 0, 0);
721             expect(1, focusIndex);
722
723         ok_sequence(sequences, TAB_SEQ_INDEX, getset_cur_focus_seq, "Getset curFoc test sequence", FALSE);
724     }
725
726     /* Testing CurSel */
727     {
728         INT selectionIndex;
729         INT focusIndex;
730
731         flush_sequences(sequences, NUM_MSG_SEQUENCES);
732
733         /* Testing CurSel with largest appropriate value */
734         selectionIndex = SendMessage(hTab, TCM_SETCURSEL, nTabs-1, 0);
735             expect(1, selectionIndex);
736         selectionIndex = SendMessage(hTab, TCM_GETCURSEL, 0, 0);
737             expect(nTabs-1, selectionIndex);
738
739         /* Focus should switch with selection */
740         focusIndex = SendMessage(hTab, TCM_GETCURFOCUS, 0, 0);
741             expect(nTabs-1, focusIndex);
742
743         /* Testing CurSel with negative value */
744         SendMessage(hTab, TCM_SETCURSEL, -10, 0);
745         selectionIndex = SendMessage(hTab, TCM_GETCURSEL, 0, 0);
746             expect(-1, selectionIndex);
747
748         /* Testing CurSel with value larger than number of tabs */
749         selectionIndex = SendMessage(hTab, TCM_SETCURSEL, 1, 0);
750             expect(-1, selectionIndex);
751
752         selectionIndex = SendMessage(hTab, TCM_SETCURSEL, nTabs+1, 0);
753             expect(-1, selectionIndex);
754         selectionIndex = SendMessage(hTab, TCM_GETCURFOCUS, 0, 0);
755             expect(1, selectionIndex);
756
757         ok_sequence(sequences, TAB_SEQ_INDEX, getset_cur_sel_seq, "Getset curSel test sequence", FALSE);
758         ok_sequence(sequences, PARENT_SEQ_INDEX, empty_sequence, "Getset curSel test parent sequence", FALSE);
759     }
760
761     /* Testing ExtendedStyle */
762     {
763         DWORD prevExtendedStyle;
764         DWORD extendedStyle;
765
766         flush_sequences(sequences, NUM_MSG_SEQUENCES);
767
768         /* Testing Flat Separators */
769         extendedStyle = SendMessage(hTab, TCM_GETEXTENDEDSTYLE, 0, 0);
770         prevExtendedStyle = SendMessage(hTab, TCM_SETEXTENDEDSTYLE, 0, TCS_EX_FLATSEPARATORS);
771             expect(extendedStyle, prevExtendedStyle);
772
773         extendedStyle = SendMessage(hTab, TCM_GETEXTENDEDSTYLE, 0, 0);
774         todo_wine{
775             expect(TCS_EX_FLATSEPARATORS, extendedStyle);
776         }
777
778         /* Testing Register Drop */
779         prevExtendedStyle = SendMessage(hTab, TCM_SETEXTENDEDSTYLE, 0, TCS_EX_REGISTERDROP);
780             expect(extendedStyle, prevExtendedStyle);
781
782         extendedStyle = SendMessage(hTab, TCM_GETEXTENDEDSTYLE, 0, 0);
783         todo_wine{
784             expect(TCS_EX_REGISTERDROP, extendedStyle);
785         }
786
787         ok_sequence(sequences, TAB_SEQ_INDEX, getset_extended_style_seq, "Getset extendedStyle test sequence", FALSE);
788         ok_sequence(sequences, PARENT_SEQ_INDEX, empty_sequence, "Getset extendedStyle test parent sequence", FALSE);
789     }
790
791     /* Testing UnicodeFormat */
792     {
793         INT unicodeFormat;
794
795         flush_sequences(sequences, NUM_MSG_SEQUENCES);
796
797         unicodeFormat = SendMessage(hTab, TCM_SETUNICODEFORMAT, TRUE, 0);
798         todo_wine{
799             expect(0, unicodeFormat);
800         }
801         unicodeFormat = SendMessage(hTab, TCM_GETUNICODEFORMAT, 0, 0);
802             expect(1, unicodeFormat);
803
804         unicodeFormat = SendMessage(hTab, TCM_SETUNICODEFORMAT, FALSE, 0);
805             expect(1, unicodeFormat);
806         unicodeFormat = SendMessage(hTab, TCM_GETUNICODEFORMAT, 0, 0);
807             expect(0, unicodeFormat);
808
809         unicodeFormat = SendMessage(hTab, TCM_SETUNICODEFORMAT, TRUE, 0);
810             expect(0, unicodeFormat);
811
812         ok_sequence(sequences, TAB_SEQ_INDEX, getset_unicode_format_seq, "Getset unicodeFormat test sequence", FALSE);
813         ok_sequence(sequences, PARENT_SEQ_INDEX, empty_sequence, "Getset unicodeFormat test parent sequence", FALSE);
814     }
815
816     /* Testing GetSet Item */
817     {
818         TCITEM tcItem;
819         char szText[32] = "New Label";
820
821         flush_sequences(sequences, NUM_MSG_SEQUENCES);
822
823         tcItem.mask = TCIF_TEXT;
824         tcItem.pszText = &szText[0];
825         tcItem.cchTextMax = sizeof(szText);
826
827         ok ( SendMessage(hTab, TCM_SETITEM, 0, (LPARAM) &tcItem), "Setting new item failed.\n");
828         ok ( SendMessage(hTab, TCM_GETITEM, 0, (LPARAM) &tcItem), "Getting item failed.\n");
829         expect_str("New Label", tcItem.pszText);
830
831         ok ( SendMessage(hTab, TCM_GETITEM, 1, (LPARAM) &tcItem), "Getting item failed.\n");
832         expect_str("Tab 2", tcItem.pszText);
833
834         ok_sequence(sequences, TAB_SEQ_INDEX, getset_item_seq, "Getset item test sequence", FALSE);
835         ok_sequence(sequences, PARENT_SEQ_INDEX, empty_sequence, "Getset item test parent sequence", FALSE);
836     }
837
838     /* Testing GetSet ToolTip */
839     {
840         HWND toolTip;
841         char toolTipText[32] = "ToolTip Text Test";
842
843         flush_sequences(sequences, NUM_MSG_SEQUENCES);
844
845         toolTip = create_tooltip(hTab, toolTipText);
846         SendMessage(hTab, TCM_SETTOOLTIPS, (LPARAM) toolTip, 0);
847         ok (toolTip == (HWND) SendMessage(hTab,TCM_GETTOOLTIPS,0,0), "ToolTip was set incorrectly.\n");
848
849         SendMessage(hTab, TCM_SETTOOLTIPS, 0, 0);
850         ok (NULL  == (HWND) SendMessage(hTab,TCM_GETTOOLTIPS,0,0), "ToolTip was set incorrectly.\n");
851
852         ok_sequence(sequences, TAB_SEQ_INDEX, getset_tooltip_seq, "Getset tooltip test sequence", TRUE);
853         ok_sequence(sequences, PARENT_SEQ_INDEX, getset_tooltip_parent_seq, "Getset tooltip test parent sequence", TRUE);
854     }
855
856     DestroyWindow(hTab);
857 }
858
859 static void test_insert_focus(HWND parent_wnd)
860 {
861     HWND hTab;
862     INT nTabsRetrieved;
863     INT r;
864     TCITEM tcNewTab;
865     DWORD mask = TCIF_TEXT|TCIF_IMAGE;
866     static char tabName[] = "TAB";
867     tcNewTab.mask = mask;
868     tcNewTab.pszText = tabName;
869
870     ok(parent_wnd != NULL, "no parent window!\n");
871
872     hTab = createFilledTabControl(parent_wnd, TCS_FIXEDWIDTH, mask, 0);
873     ok(hTab != NULL, "Failed to create tab control\n");
874
875     flush_sequences(sequences, NUM_MSG_SEQUENCES);
876
877     nTabsRetrieved = SendMessage(hTab, TCM_GETITEMCOUNT, 0, 0);
878     expect(0, nTabsRetrieved);
879
880     r = SendMessage(hTab, TCM_GETCURFOCUS, 0, 0);
881     expect(-1, r);
882
883     tcNewTab.iImage = 1;
884     r = SendMessage(hTab, TCM_INSERTITEM, 1, (LPARAM) &tcNewTab);
885     expect(0, r);
886
887     nTabsRetrieved = SendMessage(hTab, TCM_GETITEMCOUNT, 0, 0);
888     expect(1, nTabsRetrieved);
889
890     r = SendMessage(hTab, TCM_GETCURFOCUS, 0, 0);
891     expect(0, r);
892
893     tcNewTab.iImage = 2;
894     r = SendMessage(hTab, TCM_INSERTITEM, 2, (LPARAM) &tcNewTab);
895     expect(1, r);
896
897     nTabsRetrieved = SendMessage(hTab, TCM_GETITEMCOUNT, 0, 0);
898     expect(2, nTabsRetrieved);
899
900     r = SendMessage(hTab, TCM_GETCURFOCUS, 0, 0);
901     expect(0, r);
902
903     r = SendMessage(hTab, TCM_SETCURFOCUS, -1, 0);
904     expect(0, r);
905
906     r = SendMessage(hTab, TCM_GETCURFOCUS, 0, 0);
907     expect(-1, r);
908
909     tcNewTab.iImage = 3;
910     r = SendMessage(hTab, TCM_INSERTITEM, 3, (LPARAM) &tcNewTab);
911     expect(2, r);
912
913     r = SendMessage(hTab, TCM_GETCURFOCUS, 0, 0);
914     expect(2, r);
915
916     ok_sequence(sequences, TAB_SEQ_INDEX, insert_focus_seq, "insert_focus test sequence", TRUE);
917     ok_sequence(sequences, PARENT_SEQ_INDEX, empty_sequence, "insert_focus parent test sequence", FALSE);
918
919     DestroyWindow(hTab);
920 }
921
922 static void test_delete_focus(HWND parent_wnd)
923 {
924     HWND hTab;
925     INT nTabsRetrieved;
926     INT r;
927
928     ok(parent_wnd != NULL, "no parent window!\n");
929
930     hTab = createFilledTabControl(parent_wnd, TCS_FIXEDWIDTH, TCIF_TEXT|TCIF_IMAGE, 2);
931     ok(hTab != NULL, "Failed to create tab control\n");
932
933     flush_sequences(sequences, NUM_MSG_SEQUENCES);
934
935     nTabsRetrieved = SendMessage(hTab, TCM_GETITEMCOUNT, 0, 0);
936     expect(2, nTabsRetrieved);
937
938     r = SendMessage(hTab, TCM_GETCURFOCUS, 0, 0);
939     expect(0, r);
940
941     r = SendMessage(hTab, TCM_DELETEITEM, 1, 0);
942     expect(1, r);
943
944     nTabsRetrieved = SendMessage(hTab, TCM_GETITEMCOUNT, 0, 0);
945     expect(1, nTabsRetrieved);
946
947     r = SendMessage(hTab, TCM_GETCURFOCUS, 0, 0);
948     expect(0, r);
949
950     r = SendMessage(hTab, TCM_SETCURFOCUS, -1, 0);
951     expect(0, r);
952
953     r = SendMessage(hTab, TCM_GETCURFOCUS, 0, 0);
954     expect(-1, r);
955
956     r = SendMessage(hTab, TCM_DELETEITEM, 0, 0);
957     expect(1, r);
958
959     nTabsRetrieved = SendMessage(hTab, TCM_GETITEMCOUNT, 0, 0);
960     expect(0, nTabsRetrieved);
961
962     r = SendMessage(hTab, TCM_GETCURFOCUS, 0, 0);
963     expect(-1, r);
964
965     ok_sequence(sequences, TAB_SEQ_INDEX, delete_focus_seq, "delete_focus test sequence", FALSE);
966     ok_sequence(sequences, PARENT_SEQ_INDEX, empty_sequence, "delete_focus parent test sequence", FALSE);
967
968     DestroyWindow(hTab);
969 }
970
971 START_TEST(tab)
972 {
973     HWND parent_wnd;
974     LOGFONTA logfont;
975
976     lstrcpyA(logfont.lfFaceName, "Arial");
977     memset(&logfont, 0, sizeof(logfont));
978     logfont.lfHeight = -12;
979     logfont.lfWeight = FW_NORMAL;
980     logfont.lfCharSet = ANSI_CHARSET;
981     hFont = CreateFontIndirectA(&logfont);
982
983     InitCommonControls();
984
985     trace ("Testing with default MinWidth\n");
986     test_tab(-1);
987     trace ("Testing with MinWidth set to -3\n");
988     test_tab(-3);
989     trace ("Testing with MinWidth set to 24\n");
990     test_tab(24);
991     trace ("Testing with MinWidth set to 54\n");
992     test_tab(54);
993     trace ("Testing with MinWidth set to 94\n");
994     test_tab(94);
995
996     init_msg_sequences(sequences, NUM_MSG_SEQUENCES);
997
998     parent_wnd = createParentWindow();
999     ok(parent_wnd != NULL, "Failed to create parent window!\n");
1000
1001     /* Testing getters and setters with 5 tabs */
1002     test_getters_setters(parent_wnd, 5);
1003
1004     test_insert_focus(parent_wnd);
1005     test_delete_focus(parent_wnd);
1006
1007     DestroyWindow(parent_wnd);
1008 }