comctl32: header: Don't send HDN_GETDISPINFO during HDM_SET/INSERTITEM.
[wine] / dlls / comctl32 / tests / tab.c
1 /* Unit test suite for tab control.
2  *
3  * Copyright 2003 Vitaliy Margolen
4  *
5  * This library is free software; you can redistribute it and/or
6  * modify it under the terms of the GNU Lesser General Public
7  * License as published by the Free Software Foundation; either
8  * version 2.1 of the License, or (at your option) any later version.
9  *
10  * This library is distributed in the hope that it will be useful,
11  * but WITHOUT ANY WARRANTY; without even the implied warranty of
12  * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the GNU
13  * Lesser General Public License for more details.
14  *
15  * You should have received a copy of the GNU Lesser General Public
16  * License along with this library; if not, write to the Free Software
17  * Foundation, Inc., 59 Temple Place, Suite 330, Boston, MA  02111-1307  USA
18  */
19
20 #include <assert.h>
21 #include <windows.h>
22 #include <commctrl.h>
23
24 #include "wine/test.h"
25
26 #define DEFAULT_MIN_TAB_WIDTH 54
27 #define TAB_DEFAULT_WIDTH 96
28 #define TAB_PADDING_X 6
29 #define EXTRA_ICON_PADDING 3
30
31 #define TabWidthPadded(padd_x, num) (DEFAULT_MIN_TAB_WIDTH - (TAB_PADDING_X - (padd_x)) * num)
32
33 #define TabCheckSetSize(hwnd, SetWidth, SetHeight, ExpWidth, ExpHeight, Msg)\
34     SendMessage (hwnd, TCM_SETITEMSIZE, 0,\
35         (LPARAM) MAKELPARAM((SetWidth >= 0) ? SetWidth:0, (SetHeight >= 0) ? SetHeight:0));\
36     if (winetest_interactive) RedrawWindow (hwnd, NULL, 0, RDW_UPDATENOW);\
37     CheckSize(hwnd, ExpWidth, ExpHeight, Msg);
38
39 #define CheckSize(hwnd,width,height,msg)\
40     SendMessage (hwnd, TCM_GETITEMRECT, 0, (LPARAM) &rTab);\
41     if ((width  >= 0) && (height < 0))\
42         ok (width  == rTab.right  - rTab.left, "%s: Expected width [%d] got [%ld]\n",\
43         msg, (int)width,  rTab.right  - rTab.left);\
44     else if ((height >= 0) && (width  < 0))\
45         ok (height == rTab.bottom - rTab.top,  "%s: Expected height [%d] got [%ld]\n",\
46         msg, (int)height, rTab.bottom - rTab.top);\
47     else\
48         ok ((width  == rTab.right  - rTab.left) &&\
49             (height == rTab.bottom - rTab.top ),\
50             "%s: Expected [%d,%d] got [%ld,%ld]\n", msg, (int)width, (int)height,\
51             rTab.right - rTab.left, rTab.bottom - rTab.top);
52
53 static HFONT hFont = 0;
54
55 static HWND
56 create_tabcontrol (DWORD style, DWORD mask)
57 {
58     HWND handle;
59     TCITEM tcNewTab;
60
61     handle = CreateWindow (
62         WC_TABCONTROLA,
63         "TestTab",
64         WS_CLIPSIBLINGS | WS_CLIPCHILDREN | TCS_FOCUSNEVER | style,
65         10, 10, 300, 100,
66         NULL, NULL, NULL, 0);
67
68     assert (handle);
69
70     SetWindowLong(handle, GWL_STYLE, WS_CLIPSIBLINGS | WS_CLIPCHILDREN | TCS_FOCUSNEVER | style);
71     SendMessage (handle, WM_SETFONT, 0, (LPARAM) hFont);
72
73     tcNewTab.mask = mask;
74     tcNewTab.pszText = "Tab 1";
75     tcNewTab.iImage = 0;
76     SendMessage (handle, TCM_INSERTITEM, 0, (LPARAM) &tcNewTab);
77     tcNewTab.pszText = "Wide Tab 2";
78     tcNewTab.iImage = 1;
79     SendMessage (handle, TCM_INSERTITEM, 1, (LPARAM) &tcNewTab);
80     tcNewTab.pszText = "T 3";
81     tcNewTab.iImage = 2;
82     SendMessage (handle, TCM_INSERTITEM, 2, (LPARAM) &tcNewTab);
83
84     if (winetest_interactive)
85     {
86         ShowWindow (handle, SW_SHOW);
87         RedrawWindow (handle, NULL, 0, RDW_UPDATENOW);
88         Sleep (1000);
89     }
90
91     return handle;
92 }
93
94 static void test_tab(INT nMinTabWidth)
95 {
96     HWND hwTab;
97     RECT rTab;
98     HIMAGELIST himl = ImageList_Create(21, 21, ILC_COLOR, 3, 4);
99     SIZE size;
100     HDC hdc;
101     HFONT hOldFont;
102     INT i;
103
104     hwTab = create_tabcontrol(TCS_FIXEDWIDTH, TCIF_TEXT|TCIF_IMAGE);
105     SendMessage(hwTab, TCM_SETMINTABWIDTH, 0, nMinTabWidth);
106
107     hdc = GetDC(hwTab);
108     hOldFont = SelectObject(hdc, (HFONT)SendMessage(hwTab, WM_GETFONT, 0, 0));
109     GetTextExtentPoint32A(hdc, "Tab 1", strlen("Tab 1"), &size);
110     trace("Tab1 text size: size.cx=%ld size.cy=%ld\n", size.cx, size.cy);
111     SelectObject(hdc, hOldFont);
112     ReleaseDC(hwTab, hdc);
113
114     trace ("  TCS_FIXEDWIDTH tabs no icon...\n");
115     CheckSize(hwTab, TAB_DEFAULT_WIDTH, -1, "default width");
116     TabCheckSetSize(hwTab, 50, 20, 50, 20, "set size");
117     TabCheckSetSize(hwTab, 0, 1, 0, 1, "min size");
118
119     SendMessage(hwTab, TCM_SETIMAGELIST, 0, (LPARAM)himl);
120
121     trace ("  TCS_FIXEDWIDTH tabs with icon...\n");
122     TabCheckSetSize(hwTab, 50, 30, 50, 30, "set size > icon");
123     TabCheckSetSize(hwTab, 20, 20, 25, 20, "set size < icon");
124     TabCheckSetSize(hwTab, 0, 1, 25, 1, "min size");
125
126     DestroyWindow (hwTab);
127
128     hwTab = create_tabcontrol(TCS_FIXEDWIDTH | TCS_BUTTONS, TCIF_TEXT|TCIF_IMAGE);
129     SendMessage(hwTab, TCM_SETMINTABWIDTH, 0, nMinTabWidth);
130
131     trace ("  TCS_FIXEDWIDTH buttons no icon...\n");
132     CheckSize(hwTab, TAB_DEFAULT_WIDTH, -1, "default width");
133     TabCheckSetSize(hwTab, 20, 20, 20, 20, "set size 1");
134     TabCheckSetSize(hwTab, 10, 50, 10, 50, "set size 2");
135     TabCheckSetSize(hwTab, 0, 1, 0, 1, "min size");
136
137     SendMessage(hwTab, TCM_SETIMAGELIST, 0, (LPARAM)himl);
138
139     trace ("  TCS_FIXEDWIDTH buttons with icon...\n");
140     TabCheckSetSize(hwTab, 50, 30, 50, 30, "set size > icon");
141     TabCheckSetSize(hwTab, 20, 20, 25, 20, "set size < icon");
142     TabCheckSetSize(hwTab, 0, 1, 25, 1, "min size");
143     SendMessage(hwTab, TCM_SETPADDING, 0, MAKELPARAM(4,4));
144     TabCheckSetSize(hwTab, 0, 1, 25, 1, "set padding, min size");
145
146     DestroyWindow (hwTab);
147
148     hwTab = create_tabcontrol(TCS_FIXEDWIDTH | TCS_BOTTOM, TCIF_TEXT|TCIF_IMAGE);
149     SendMessage(hwTab, TCM_SETMINTABWIDTH, 0, nMinTabWidth);
150
151     trace ("  TCS_FIXEDWIDTH | TCS_BOTTOM tabs...\n");
152     CheckSize(hwTab, TAB_DEFAULT_WIDTH, -1, "no icon, default width");
153
154     TabCheckSetSize(hwTab, 20, 20, 20, 20, "no icon, set size 1");
155     TabCheckSetSize(hwTab, 10, 50, 10, 50, "no icon, set size 2");
156     TabCheckSetSize(hwTab, 0, 1, 0, 1, "no icon, min size");
157
158     SendMessage(hwTab, TCM_SETIMAGELIST, 0, (LPARAM)himl);
159
160     TabCheckSetSize(hwTab, 50, 30, 50, 30, "with icon, set size > icon");
161     TabCheckSetSize(hwTab, 20, 20, 25, 20, "with icon, set size < icon");
162     TabCheckSetSize(hwTab, 0, 1, 25, 1, "with icon, min size");
163     SendMessage(hwTab, TCM_SETPADDING, 0, MAKELPARAM(4,4));
164     TabCheckSetSize(hwTab, 0, 1, 25, 1, "set padding, min size");
165
166     DestroyWindow (hwTab);
167
168     hwTab = create_tabcontrol(0, TCIF_TEXT|TCIF_IMAGE);
169     SendMessage(hwTab, TCM_SETMINTABWIDTH, 0, nMinTabWidth);
170
171     trace ("  non fixed width, with text...\n");
172     CheckSize(hwTab, max(size.cx +TAB_PADDING_X*2, (nMinTabWidth < 0) ? DEFAULT_MIN_TAB_WIDTH : nMinTabWidth), -1,
173               "no icon, default width");
174     for (i=0; i<8; i++)
175     {
176         INT nTabWidth = (nMinTabWidth < 0) ? TabWidthPadded(i, 2) : nMinTabWidth;
177
178         SendMessage(hwTab, TCM_SETIMAGELIST, 0, 0);
179         SendMessage(hwTab, TCM_SETPADDING, 0, MAKELPARAM(i,i));
180
181         TabCheckSetSize(hwTab, 50, 20, max(size.cx + i*2, nTabWidth), 20, "no icon, set size");
182         TabCheckSetSize(hwTab, 0, 1, max(size.cx + i*2, nTabWidth), 1, "no icon, min size");
183
184         SendMessage(hwTab, TCM_SETIMAGELIST, 0, (LPARAM)himl);
185         nTabWidth = (nMinTabWidth < 0) ? TabWidthPadded(i, 3) : nMinTabWidth;
186
187         TabCheckSetSize(hwTab, 50, 30, max(size.cx + 21 + i*3, nTabWidth), 30, "with icon, set size > icon");
188         TabCheckSetSize(hwTab, 20, 20, max(size.cx + 21 + i*3, nTabWidth), 20, "with icon, set size < icon");
189         TabCheckSetSize(hwTab, 0, 1, max(size.cx + 21 + i*3, nTabWidth), 1, "with icon, min size");
190     }
191     DestroyWindow (hwTab);
192
193     hwTab = create_tabcontrol(0, TCIF_IMAGE);
194     SendMessage(hwTab, TCM_SETMINTABWIDTH, 0, nMinTabWidth);
195
196     trace ("  non fixed width, no text...\n");
197     CheckSize(hwTab, (nMinTabWidth < 0) ? DEFAULT_MIN_TAB_WIDTH : nMinTabWidth, -1, "no icon, default width");
198     for (i=0; i<8; i++)
199     {
200         INT nTabWidth = (nMinTabWidth < 0) ? TabWidthPadded(i, 2) : nMinTabWidth;
201
202         SendMessage(hwTab, TCM_SETIMAGELIST, 0, 0);
203         SendMessage(hwTab, TCM_SETPADDING, 0, MAKELPARAM(i,i));
204
205         TabCheckSetSize(hwTab, 50, 20, nTabWidth, 20, "no icon, set size");
206         TabCheckSetSize(hwTab, 0, 1, nTabWidth, 1, "no icon, min size");
207
208         SendMessage(hwTab, TCM_SETIMAGELIST, 0, (LPARAM)himl);
209         if (i > 1 && nMinTabWidth > 0 && nMinTabWidth < DEFAULT_MIN_TAB_WIDTH)
210             nTabWidth += EXTRA_ICON_PADDING *(i-1);
211
212         TabCheckSetSize(hwTab, 50, 30, nTabWidth, 30, "with icon, set size > icon");
213         TabCheckSetSize(hwTab, 20, 20, nTabWidth, 20, "with icon, set size < icon");
214         TabCheckSetSize(hwTab, 0, 1, nTabWidth, 1, "with icon, min size");
215     }
216
217     DestroyWindow (hwTab);
218
219     ImageList_Destroy(himl);
220     DeleteObject(hFont);
221 }
222
223 START_TEST(tab)
224 {
225     LOGFONTA logfont;
226
227     lstrcpyA(logfont.lfFaceName, "Arial");
228     memset(&logfont, 0, sizeof(logfont));
229     logfont.lfHeight = -12;
230     logfont.lfWeight = FW_NORMAL;
231     logfont.lfCharSet = ANSI_CHARSET;
232     hFont = CreateFontIndirectA(&logfont);
233
234     InitCommonControls();
235
236     trace ("Testing with default MinWidth\n");
237     test_tab(-1);
238     trace ("Testing with MinWidth set to -3\n");
239     test_tab(-3);
240     trace ("Testing with MinWidth set to 24\n");
241     test_tab(24);
242     trace ("Testing with MinWidth set to 54\n");
243     test_tab(54);
244     trace ("Testing with MinWidth set to 94\n");
245     test_tab(94);
246 }