d3d10core: Fixup HRESULT in a bunch of error cases.
[wine] / dlls / comctl32 / tests / monthcal.c
1 /*
2  * comctl32 month calendar unit tests
3  *
4  * Copyright (C) 2006 Vitaliy Margolen
5  * Copyright (C) 2007 Farshad Agah
6  *
7  * This library is free software; you can redistribute it and/or
8  * modify it under the terms of the GNU Lesser General Public
9  * License as published by the Free Software Foundation; either
10  * version 2.1 of the License, or (at your option) any later version.
11  *
12  * This library is distributed in the hope that it will be useful,
13  * but WITHOUT ANY WARRANTY; without even the implied warranty of
14  * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the GNU
15  * Lesser General Public License for more details.
16  *
17  * You should have received a copy of the GNU Lesser General Public
18  * License along with this library; if not, write to the Free Software
19  * Foundation, Inc., 51 Franklin St, Fifth Floor, Boston, MA 02110-1301, USA
20  */
21
22 #include <stdarg.h>
23
24 #include "windef.h"
25 #include "winbase.h"
26 #include "winuser.h"
27
28 #include "commctrl.h"
29
30 #include "wine/test.h"
31 #include "v6util.h"
32 #include <assert.h>
33 #include <windows.h>
34 #include "msg.h"
35
36 #define expect(expected, got) ok(expected == got, "Expected %d, got %d\n", expected, got);
37 #define expect_hex(expected, got) ok(expected == got, "Expected %x, got %x\n", expected, got);
38 #define expect_d(expected, got) ok(abs((expected) - (got)) <= 2, "Expected %d, got %d\n", expected, got);
39
40 #define NUM_MSG_SEQUENCES   2
41 #define PARENT_SEQ_INDEX    0
42 #define MONTHCAL_SEQ_INDEX  1
43
44 static struct msg_sequence *sequences[NUM_MSG_SEQUENCES];
45
46 static HWND parent_wnd;
47
48 static const struct message create_parent_window_seq[] = {
49     { WM_GETMINMAXINFO, sent },
50     { WM_NCCREATE, sent },
51     { WM_NCCALCSIZE, sent|wparam, 0 },
52     { WM_CREATE, sent },
53     { WM_SHOWWINDOW, sent|wparam, 1 },
54     { WM_WINDOWPOSCHANGING, sent|wparam, 0 },
55     { WM_QUERYNEWPALETTE, sent|optional },
56     { WM_WINDOWPOSCHANGING, sent|wparam|optional, 0 },
57     { WM_WINDOWPOSCHANGED, sent|optional },
58     { WM_ACTIVATEAPP, sent|wparam, 1 },
59     { WM_NCACTIVATE, sent },
60     { WM_ACTIVATE, sent|wparam, 1 },
61     { WM_IME_SETCONTEXT, sent|wparam|defwinproc|optional, 1 },
62     { WM_IME_NOTIFY, sent|defwinproc|optional },
63     { WM_SETFOCUS, sent|wparam|defwinproc, 0 },
64     /* Win9x adds SWP_NOZORDER below */
65     { WM_WINDOWPOSCHANGED, sent, /*|wparam, SWP_SHOWWINDOW|SWP_NOSIZE|SWP_NOMOVE|SWP_NOCLIENTSIZE|SWP_NOCLIENTMOVE*/ },
66     { WM_NCCALCSIZE, sent|wparam|optional, 1 },
67     { WM_SIZE, sent },
68     { WM_MOVE, sent },
69     { 0 }
70 };
71
72 static const struct message create_monthcal_control_seq[] = {
73     { WM_NOTIFYFORMAT, sent|lparam, 0, NF_QUERY },
74     { WM_QUERYUISTATE, sent|optional },
75     { WM_GETFONT, sent },
76     { WM_PARENTNOTIFY, sent|wparam, WM_CREATE},
77     { 0 }
78 };
79
80 static const struct message create_monthcal_multi_sel_style_seq[] = {
81     { WM_NOTIFYFORMAT, sent|lparam, 0, NF_QUERY },
82     { WM_QUERYUISTATE, sent|optional },
83     { WM_GETFONT, sent },
84     { WM_PARENTNOTIFY, sent },
85     { 0 }
86 };
87
88 static const struct message monthcal_curr_date_seq[] = {
89     { MCM_SETCURSEL, sent|wparam, 0},
90     { WM_PAINT, sent|wparam|lparam|defwinproc, 0, 0},
91     { MCM_SETCURSEL, sent|wparam, 0},
92     { MCM_SETCURSEL, sent|wparam, 0},
93     { MCM_GETCURSEL, sent|wparam, 0},
94     { MCM_GETCURSEL, sent|wparam|lparam, 0, 0},
95     { 0 }
96 };
97
98 static const struct message monthcal_first_day_seq[] = {
99     { MCM_GETFIRSTDAYOFWEEK, sent|wparam|lparam, 0, 0},
100
101     { MCM_SETFIRSTDAYOFWEEK, sent|wparam|lparam, 0, -5},
102     { MCM_GETFIRSTDAYOFWEEK, sent|wparam|lparam, 0, 0},
103
104     { MCM_SETFIRSTDAYOFWEEK, sent|wparam|lparam, 0, -4},
105     { MCM_GETFIRSTDAYOFWEEK, sent|wparam|lparam, 0, 0},
106
107     { MCM_SETFIRSTDAYOFWEEK, sent|wparam|lparam, 0, -3},
108     { MCM_GETFIRSTDAYOFWEEK, sent|wparam|lparam, 0, 0},
109
110     { MCM_SETFIRSTDAYOFWEEK, sent|wparam|lparam, 0, -2},
111     { MCM_GETFIRSTDAYOFWEEK, sent|wparam|lparam, 0, 0},
112
113     { MCM_SETFIRSTDAYOFWEEK, sent|wparam|lparam, 0, -1},
114     { MCM_GETFIRSTDAYOFWEEK, sent|wparam|lparam, 0, 0},
115
116     { MCM_SETFIRSTDAYOFWEEK, sent|wparam|lparam, 0, 0},
117     { MCM_GETFIRSTDAYOFWEEK, sent|wparam|lparam, 0, 0},
118
119     { MCM_SETFIRSTDAYOFWEEK, sent|wparam|lparam, 0, 1},
120     { MCM_GETFIRSTDAYOFWEEK, sent|wparam|lparam, 0, 0},
121
122     { MCM_SETFIRSTDAYOFWEEK, sent|wparam|lparam, 0, 2},
123     { MCM_GETFIRSTDAYOFWEEK, sent|wparam|lparam, 0, 0},
124
125     { MCM_SETFIRSTDAYOFWEEK, sent|wparam|lparam, 0, 3},
126     { MCM_GETFIRSTDAYOFWEEK, sent|wparam|lparam, 0, 0},
127
128     { MCM_SETFIRSTDAYOFWEEK, sent|wparam|lparam, 0, 4},
129     { MCM_GETFIRSTDAYOFWEEK, sent|wparam|lparam, 0, 0},
130
131     { MCM_SETFIRSTDAYOFWEEK, sent|wparam|lparam, 0, 5},
132     { MCM_GETFIRSTDAYOFWEEK, sent|wparam|lparam, 0, 0},
133
134     { MCM_SETFIRSTDAYOFWEEK, sent|wparam|lparam, 0, 6},
135     { MCM_GETFIRSTDAYOFWEEK, sent|wparam|lparam, 0, 0},
136
137     { MCM_SETFIRSTDAYOFWEEK, sent|wparam|lparam, 0, 7},
138     { MCM_GETFIRSTDAYOFWEEK, sent|wparam|lparam, 0, 0},
139
140     { MCM_SETFIRSTDAYOFWEEK, sent|wparam|lparam, 0, 8},
141     { MCM_GETFIRSTDAYOFWEEK, sent|wparam|lparam, 0, 0},
142
143     { MCM_SETFIRSTDAYOFWEEK, sent|wparam|lparam, 0, 9},
144     { MCM_GETFIRSTDAYOFWEEK, sent|wparam|lparam, 0, 0},
145
146     { MCM_SETFIRSTDAYOFWEEK, sent|wparam|lparam, 0, 10},
147     { MCM_GETFIRSTDAYOFWEEK, sent|wparam|lparam, 0, 0},
148
149     { MCM_SETFIRSTDAYOFWEEK, sent|wparam|lparam, 0, 11},
150     { MCM_GETFIRSTDAYOFWEEK, sent|wparam|lparam, 0, 0},
151     { 0 }
152 };
153
154 static const struct message monthcal_unicode_seq[] = {
155     { MCM_GETUNICODEFORMAT, sent|wparam|lparam, 0, 0},
156     { MCM_SETUNICODEFORMAT, sent|wparam|lparam, 1, 0},
157     { MCM_GETUNICODEFORMAT, sent|wparam|lparam, 0, 0},
158     { MCM_SETUNICODEFORMAT, sent|wparam|lparam, 0, 0},
159     { MCM_GETUNICODEFORMAT, sent|wparam|lparam, 0, 0},
160     { MCM_SETUNICODEFORMAT, sent|wparam|lparam, 1, 0},
161     { 0 }
162 };
163
164 static const struct message monthcal_hit_test_seq[] = {
165     { MCM_SETCURSEL, sent|wparam, 0},
166     { WM_PAINT, sent|wparam|lparam|defwinproc, 0, 0},
167     { MCM_HITTEST, sent|wparam, 0},
168     { MCM_HITTEST, sent|wparam, 0},
169     { MCM_HITTEST, sent|wparam, 0},
170     { MCM_HITTEST, sent|wparam, 0},
171     { MCM_HITTEST, sent|wparam, 0},
172     { MCM_HITTEST, sent|wparam, 0},
173     { MCM_HITTEST, sent|wparam, 0},
174     { MCM_HITTEST, sent|wparam, 0},
175     { MCM_HITTEST, sent|wparam, 0},
176     { MCM_HITTEST, sent|wparam, 0},
177     { 0 }
178 };
179
180 static const struct message monthcal_todaylink_seq[] = {
181     { MCM_HITTEST, sent|wparam, 0},
182     { MCM_SETTODAY, sent|wparam, 0},
183     { WM_PAINT, sent|wparam|lparam|defwinproc, 0, 0},
184     { MCM_GETTODAY, sent|wparam, 0},
185     { WM_LBUTTONDOWN, sent|wparam, MK_LBUTTON},
186     { WM_CAPTURECHANGED, sent|wparam|lparam|defwinproc, 0, 0},
187     { WM_PAINT, sent|wparam|lparam|defwinproc, 0, 0},
188     { MCM_GETCURSEL, sent|wparam, 0},
189     { 0 }
190 };
191
192 static const struct message monthcal_today_seq[] = {
193     { MCM_SETTODAY, sent|wparam, 0},
194     { WM_PAINT, sent|wparam|lparam|defwinproc, 0, 0},
195     { MCM_GETTODAY, sent|wparam, 0},
196     { MCM_SETTODAY, sent|wparam, 0},
197     { WM_PAINT, sent|wparam|lparam|defwinproc, 0, 0},
198     { MCM_GETTODAY, sent|wparam, 0},
199     { 0 }
200 };
201
202 static const struct message monthcal_scroll_seq[] = {
203     { MCM_SETMONTHDELTA, sent|wparam|lparam, 2, 0},
204     { MCM_SETMONTHDELTA, sent|wparam|lparam, 3, 0},
205     { MCM_GETMONTHDELTA, sent|wparam|lparam, 0, 0},
206     { MCM_SETMONTHDELTA, sent|wparam|lparam, 12, 0},
207     { MCM_GETMONTHDELTA, sent|wparam|lparam, 0, 0},
208     { MCM_SETMONTHDELTA, sent|wparam|lparam, 15, 0},
209     { MCM_GETMONTHDELTA, sent|wparam|lparam, 0, 0},
210     { MCM_SETMONTHDELTA, sent|wparam|lparam, -5, 0},
211     { MCM_GETMONTHDELTA, sent|wparam|lparam, 0, 0},
212     { 0 }
213 };
214
215 static const struct message monthcal_monthrange_seq[] = {
216     { MCM_GETMONTHRANGE, sent|wparam, GMR_VISIBLE},
217     { MCM_GETMONTHRANGE, sent|wparam, GMR_DAYSTATE},
218     { 0 }
219 };
220
221 static const struct message monthcal_max_sel_day_seq[] = {
222     { MCM_SETMAXSELCOUNT, sent|wparam|lparam, 5, 0},
223     { MCM_GETMAXSELCOUNT, sent|wparam|lparam, 0, 0},
224     { MCM_SETMAXSELCOUNT, sent|wparam|lparam, 15, 0},
225     { MCM_GETMAXSELCOUNT, sent|wparam|lparam, 0, 0},
226     { MCM_SETMAXSELCOUNT, sent|wparam|lparam, -1, 0},
227     { MCM_GETMAXSELCOUNT, sent|wparam|lparam, 0, 0},
228     { 0 }
229 };
230
231 /* expected message sequence for parent*/
232 static const struct message destroy_monthcal_parent_msgs_seq[] = {
233     { WM_PARENTNOTIFY, sent|wparam, WM_DESTROY},
234     { 0 }
235 };
236
237 /* expected message sequence for child*/
238 static const struct message destroy_monthcal_child_msgs_seq[] = {
239     { 0x0090, sent|optional }, /* Vista */
240     { WM_SHOWWINDOW, sent|wparam|lparam, 0, 0},
241     { WM_WINDOWPOSCHANGING, sent|wparam, 0},
242     { WM_WINDOWPOSCHANGED, sent|wparam, 0},
243     { WM_DESTROY, sent|wparam|lparam, 0, 0},
244     { WM_NCDESTROY, sent|wparam|lparam, 0, 0},
245     { 0 }
246 };
247
248 static const struct message destroy_monthcal_multi_sel_style_seq[] = {
249     { 0x0090, sent|optional }, /* Vista */
250     { WM_SHOWWINDOW, sent|wparam|lparam, 0, 0},
251     { WM_WINDOWPOSCHANGING, sent|wparam, 0},
252     { WM_WINDOWPOSCHANGED, sent|wparam, 0},
253     { WM_DESTROY, sent|wparam|lparam, 0, 0},
254     { WM_NCDESTROY, sent|wparam|lparam, 0, 0},
255     { 0 }
256 };
257
258 static void test_monthcal(void)
259 {
260     HWND hwnd;
261     SYSTEMTIME st[2], st1[2], today;
262     int res, month_range;
263     DWORD limits;
264
265     hwnd = CreateWindowA(MONTHCAL_CLASSA, "MonthCal", WS_POPUP | WS_VISIBLE, CW_USEDEFAULT,
266                          0, 300, 300, 0, 0, NULL, NULL);
267     ok(hwnd != NULL, "Failed to create MonthCal\n");
268
269     /* test range just after creation */
270     memset(&st, 0xcc, sizeof(st));
271     limits = SendMessage(hwnd, MCM_GETRANGE, 0, (LPARAM)st);
272     ok(limits == 0 ||
273        broken(limits == GDTR_MIN), /* comctl32 <= 4.70 */
274        "No limits should be set (%d)\n", limits);
275     if (limits == GDTR_MIN)
276     {
277         win_skip("comctl32 <= 4.70 is broken\n");
278         DestroyWindow(hwnd);
279         return;
280     }
281
282     ok(0 == st[0].wYear ||
283        broken(1752 == st[0].wYear), /* comctl32 <= 4.72 */
284        "Expected 0, got %d\n", st[0].wYear);
285     ok(0 == st[0].wMonth ||
286        broken(9 == st[0].wMonth), /* comctl32 <= 4.72 */
287        "Expected 0, got %d\n", st[0].wMonth);
288     ok(0 == st[0].wDay ||
289        broken(14 == st[0].wDay), /* comctl32 <= 4.72 */
290        "Expected 0, got %d\n", st[0].wDay);
291     expect(0, st[0].wDayOfWeek);
292     expect(0, st[0].wHour);
293     expect(0, st[0].wMinute);
294     expect(0, st[0].wSecond);
295     expect(0, st[0].wMilliseconds);
296
297     expect(0, st[1].wYear);
298     expect(0, st[1].wMonth);
299     expect(0, st[1].wDay);
300     expect(0, st[1].wDayOfWeek);
301     expect(0, st[1].wHour);
302     expect(0, st[1].wMinute);
303     expect(0, st[1].wSecond);
304     expect(0, st[1].wMilliseconds);
305
306     GetSystemTime(&st[0]);
307     st[1] = st[0];
308
309     SendMessage(hwnd, MCM_GETTODAY, 0, (LPARAM)&today);
310
311     /* Invalid date/time */
312     st[0].wYear  = 2000;
313     /* Time should not matter */
314     st[1].wHour = st[1].wMinute = st[1].wSecond = 70;
315     st[1].wMilliseconds = 1200;
316     ok(SendMessage(hwnd, MCM_SETRANGE, GDTR_MAX, (LPARAM)st), "Failed to set MAX limit\n");
317     /* invalid timestamp is written back with today data and msecs untouched */
318     expect(today.wHour, st[1].wHour);
319     expect(today.wMinute, st[1].wMinute);
320     expect(today.wSecond, st[1].wSecond);
321     expect(1200, st[1].wMilliseconds);
322
323     ok(SendMessage(hwnd, MCM_GETRANGE, 0, (LPARAM)st1) == GDTR_MAX, "No limits should be set\n");
324     ok(st1[0].wYear != 2000, "Lower limit changed\n");
325     /* invalid timestamp should be replaced with today data, except msecs */
326     expect(today.wHour, st1[1].wHour);
327     expect(today.wMinute, st1[1].wMinute);
328     expect(today.wSecond, st1[1].wSecond);
329     expect(1200, st1[1].wMilliseconds);
330
331     /* Invalid date/time with invalid milliseconds only */
332     GetSystemTime(&st[0]);
333     st[1] = st[0];
334     /* Time should not matter */
335     st[1].wMilliseconds = 1200;
336     ok(SendMessage(hwnd, MCM_SETRANGE, GDTR_MAX, (LPARAM)st), "Failed to set MAX limit\n");
337     /* invalid milliseconds field doesn't lead to invalid timestamp */
338     expect(st[0].wHour,   st[1].wHour);
339     expect(st[0].wMinute, st[1].wMinute);
340     expect(st[0].wSecond, st[1].wSecond);
341     expect(1200, st[1].wMilliseconds);
342
343     GetSystemTime(&st[0]);
344
345     st[1].wMonth = 0;
346     ok(!SendMessage(hwnd, MCM_SETRANGE, GDTR_MIN | GDTR_MAX, (LPARAM)st), "Should have failed to set limits\n");
347     ok(SendMessage(hwnd, MCM_GETRANGE, 0, (LPARAM)st1) == GDTR_MAX, "No limits should be set\n");
348     ok(st1[0].wYear != 2000, "Lower limit changed\n");
349     ok(!SendMessage(hwnd, MCM_SETRANGE, GDTR_MAX, (LPARAM)st), "Should have failed to set MAX limit\n");
350     ok(SendMessage(hwnd, MCM_GETRANGE, 0, (LPARAM)st1) == GDTR_MAX, "No limits should be set\n");
351     ok(st1[0].wYear != 2000, "Lower limit changed\n");
352
353     GetSystemTime(&st[0]);
354     st[0].wDay = 20;
355     st[0].wMonth = 5;
356     st[1] = st[0];
357
358     month_range = SendMessage(hwnd, MCM_GETMONTHRANGE, GMR_VISIBLE, (LPARAM)st1);
359     st[1].wMonth--;
360     ok(SendMessage(hwnd, MCM_SETRANGE, GDTR_MIN | GDTR_MAX, (LPARAM)st), "Failed to set both min and max limits\n");
361     res = SendMessage(hwnd, MCM_GETMONTHRANGE, GMR_VISIBLE, (LPARAM)st1);
362     ok(res == month_range, "Invalid month range (%d)\n", res);
363     ok(SendMessage(hwnd, MCM_GETRANGE, 0, (LPARAM)st1) == (GDTR_MIN|GDTR_MAX), "Limits should be set\n");
364
365     st[1].wMonth += 2;
366     ok(SendMessage(hwnd, MCM_SETRANGE, GDTR_MIN | GDTR_MAX, (LPARAM)st), "Failed to set both min and max limits\n");
367     res = SendMessage(hwnd, MCM_GETMONTHRANGE, GMR_VISIBLE, (LPARAM)st1);
368     ok(res == month_range, "Invalid month range (%d)\n", res);
369
370     st[1].wYear --;
371     ok(SendMessage(hwnd, MCM_SETRANGE, GDTR_MIN | GDTR_MAX, (LPARAM)st), "Failed to set both min and max limits\n");
372     st[1].wYear += 1;
373     ok(SendMessage(hwnd, MCM_SETRANGE, GDTR_MIN | GDTR_MAX, (LPARAM)st), "Failed to set both min and max limits\n");
374
375     st[1].wMonth -= 3;
376     ok(SendMessage(hwnd, MCM_SETRANGE, GDTR_MAX, (LPARAM)st), "Failed to set max limit\n");
377     ok(SendMessage(hwnd, MCM_GETRANGE, 0, (LPARAM)st1) == GDTR_MAX, "Only MAX limit should be set\n");
378     st[1].wMonth += 4;
379     ok(SendMessage(hwnd, MCM_SETRANGE, GDTR_MAX, (LPARAM)st), "Failed to set max limit\n");
380     st[1].wYear -= 3;
381     ok(SendMessage(hwnd, MCM_SETRANGE, GDTR_MAX, (LPARAM)st), "Failed to set max limit\n");
382     st[1].wYear += 4;
383     ok(SendMessage(hwnd, MCM_SETRANGE, GDTR_MAX, (LPARAM)st), "Failed to set max limit\n");
384     ok(SendMessage(hwnd, MCM_GETRANGE, 0, (LPARAM)st1) == GDTR_MAX, "Only MAX limit should be set\n");
385
386     /* set both limits, then set max < min */
387     GetSystemTime(&st[0]);
388     st[1] = st[0];
389     st[1].wYear++;
390     ok(SendMessage(hwnd, MCM_SETRANGE, GDTR_MIN|GDTR_MAX, (LPARAM)st), "Failed to set limits\n");
391     ok(SendMessage(hwnd, MCM_GETRANGE, 0, (LPARAM)st1) == (GDTR_MIN|GDTR_MAX), "Min limit expected\n");
392     st[1].wYear -= 2;
393     ok(SendMessage(hwnd, MCM_SETRANGE, GDTR_MAX, (LPARAM)st), "Failed to set limits\n");
394     ok(SendMessage(hwnd, MCM_GETRANGE, 0, (LPARAM)st1) == GDTR_MAX, "Max limit expected\n");
395
396     expect(0, st1[0].wYear);
397     expect(0, st1[0].wMonth);
398     expect(0, st1[0].wDay);
399     expect(0, st1[0].wDayOfWeek);
400     expect(0, st1[0].wHour);
401     expect(0, st1[0].wMinute);
402     expect(0, st1[0].wSecond);
403     expect(0, st1[0].wMilliseconds);
404
405     expect(st[1].wYear,      st1[1].wYear);
406     expect(st[1].wMonth,     st1[1].wMonth);
407     expect(st[1].wDay,       st1[1].wDay);
408     expect(st[1].wDayOfWeek, st1[1].wDayOfWeek);
409     expect(st[1].wHour,      st1[1].wHour);
410     expect(st[1].wMinute,    st1[1].wMinute);
411     expect(st[1].wSecond,    st1[1].wSecond);
412     expect(st[1].wMilliseconds, st1[1].wMilliseconds);
413
414     st[1] = st[0];
415     st[1].wYear++;
416     ok(SendMessage(hwnd, MCM_SETRANGE, GDTR_MIN|GDTR_MAX, (LPARAM)st), "Failed to set limits\n");
417     ok(SendMessage(hwnd, MCM_GETRANGE, 0, (LPARAM)st1) == (GDTR_MIN|GDTR_MAX), "Min limit expected\n");
418     st[0].wYear++; /* start == end now */
419     ok(SendMessage(hwnd, MCM_SETRANGE, GDTR_MIN, (LPARAM)st), "Failed to set limits\n");
420     ok(SendMessage(hwnd, MCM_GETRANGE, 0, (LPARAM)st1) == GDTR_MIN, "Min limit expected\n");
421
422     expect(st[0].wYear,      st1[0].wYear);
423     expect(st[0].wMonth,     st1[0].wMonth);
424     expect(st[0].wDay,       st1[0].wDay);
425     expect(st[0].wDayOfWeek, st1[0].wDayOfWeek);
426     expect(st[0].wHour,      st1[0].wHour);
427     expect(st[0].wMinute,    st1[0].wMinute);
428     expect(st[0].wSecond,    st1[0].wSecond);
429     expect(st[0].wMilliseconds, st1[0].wMilliseconds);
430
431     expect(0, st1[1].wYear);
432     expect(0, st1[1].wMonth);
433     expect(0, st1[1].wDay);
434     expect(0, st1[1].wDayOfWeek);
435     expect(0, st1[1].wHour);
436     expect(0, st1[1].wMinute);
437     expect(0, st1[1].wSecond);
438     expect(0, st1[1].wMilliseconds);
439
440     DestroyWindow(hwnd);
441 }
442
443 static LRESULT WINAPI parent_wnd_proc(HWND hwnd, UINT message, WPARAM wParam, LPARAM lParam)
444 {
445     static LONG defwndproc_counter = 0;
446     LRESULT ret;
447     struct message msg;
448
449     /* log system messages, except for painting */
450     if (message < WM_USER &&
451         message != WM_PAINT &&
452         message != WM_ERASEBKGND &&
453         message != WM_NCPAINT &&
454         message != WM_NCHITTEST &&
455         message != WM_GETTEXT &&
456         message != WM_GETICON &&
457         message != WM_DEVICECHANGE)
458     {
459         trace("parent: %p, %04x, %08lx, %08lx\n", hwnd, message, wParam, lParam);
460
461         msg.message = message;
462         msg.flags = sent|wparam|lparam;
463         if (defwndproc_counter) msg.flags |= defwinproc;
464         msg.wParam = wParam;
465         msg.lParam = lParam;
466         add_message(sequences, PARENT_SEQ_INDEX, &msg);
467     }
468
469     defwndproc_counter++;
470     ret = DefWindowProcA(hwnd, message, wParam, lParam);
471     defwndproc_counter--;
472
473     return ret;
474 }
475
476 static BOOL register_parent_wnd_class(void)
477 {
478     WNDCLASSA cls;
479
480     cls.style = 0;
481     cls.lpfnWndProc = parent_wnd_proc;
482     cls.cbClsExtra = 0;
483     cls.cbWndExtra = 0;
484     cls.hInstance = GetModuleHandleA(NULL);
485     cls.hIcon = 0;
486     cls.hCursor = LoadCursorA(0, IDC_ARROW);
487     cls.hbrBackground = GetStockObject(WHITE_BRUSH);
488     cls.lpszMenuName = NULL;
489     cls.lpszClassName = "Month-Cal test parent class";
490     return RegisterClassA(&cls);
491 }
492
493 static HWND create_parent_window(void)
494 {
495     HWND hwnd;
496
497     InitCommonControls();
498
499     /* flush message sequences, so we can check the new sequence by the end of function */
500     flush_sequences(sequences, NUM_MSG_SEQUENCES);
501
502     if (!register_parent_wnd_class())
503         return NULL;
504
505     hwnd = CreateWindowEx(0, "Month-Cal test parent class",
506                           "Month-Cal test parent window",
507                           WS_CAPTION | WS_SYSMENU | WS_MINIMIZEBOX |
508                           WS_MAXIMIZEBOX | WS_VISIBLE,
509                           0, 0, 500, 500,
510                           GetDesktopWindow(), NULL, GetModuleHandleA(NULL), NULL);
511
512     assert(hwnd);
513
514     /* check for message sequences */
515     ok_sequence(sequences, PARENT_SEQ_INDEX, create_parent_window_seq, "create parent window", FALSE);
516
517     return hwnd;
518 }
519
520 static LRESULT WINAPI monthcal_subclass_proc(HWND hwnd, UINT message, WPARAM wParam, LPARAM lParam)
521 {
522     WNDPROC oldproc = (WNDPROC)GetWindowLongPtrA(hwnd, GWLP_USERDATA);
523     static LONG defwndproc_counter = 0;
524     LRESULT ret;
525     struct message msg;
526
527     msg.message = message;
528     msg.flags = sent|wparam|lparam;
529     if (defwndproc_counter) msg.flags |= defwinproc;
530     msg.wParam = wParam;
531     msg.lParam = lParam;
532     add_message(sequences, MONTHCAL_SEQ_INDEX, &msg);
533
534     /* some debug output for style changing */
535     if ((message == WM_STYLECHANGING ||
536          message == WM_STYLECHANGED) && lParam)
537     {
538         STYLESTRUCT *style = (STYLESTRUCT*)lParam;
539         trace("\told style: 0x%08x, new style: 0x%08x\n", style->styleOld, style->styleNew);
540     }
541
542     defwndproc_counter++;
543     ret = CallWindowProcA(oldproc, hwnd, message, wParam, lParam);
544     defwndproc_counter--;
545
546     return ret;
547 }
548
549 static HWND create_monthcal_control(DWORD style)
550 {
551     WNDPROC oldproc;
552     HWND hwnd;
553
554     hwnd = CreateWindowEx(0,
555                     MONTHCAL_CLASS,
556                     "",
557                     WS_CHILD | WS_BORDER | WS_VISIBLE | style,
558                     0, 0, 300, 400,
559                     parent_wnd, NULL, GetModuleHandleA(NULL), NULL);
560
561     if (!hwnd) return NULL;
562
563     oldproc = (WNDPROC)SetWindowLongPtrA(hwnd, GWLP_WNDPROC,
564                                         (LONG_PTR)monthcal_subclass_proc);
565     SetWindowLongPtrA(hwnd, GWLP_USERDATA, (LONG_PTR)oldproc);
566
567     SendMessage(hwnd, WM_SETFONT, (WPARAM)GetStockObject(SYSTEM_FONT), 0);
568
569     return hwnd;
570 }
571
572
573 /* Setter and Getters Tests */
574
575 static void test_color(void)
576 {
577     COLORREF color, prev;
578     HWND hwnd;
579
580     hwnd = create_monthcal_control(0);
581
582     /* invalid color index */
583     color = SendMessage(hwnd, MCM_GETCOLOR, MCSC_TRAILINGTEXT + 1, 0);
584     expect(~0u, color);
585     prev = SendMessage(hwnd, MCM_SETCOLOR, MCSC_TRAILINGTEXT + 1, RGB(255,255,255));
586     expect(~0u, prev);
587
588     color = SendMessage(hwnd, MCM_GETCOLOR, MCSC_BACKGROUND, 0);
589     prev = SendMessage(hwnd, MCM_SETCOLOR, MCSC_BACKGROUND, RGB(0,0,0));
590     expect(color, prev);
591     color = SendMessage(hwnd, MCM_GETCOLOR, MCSC_BACKGROUND, 0);
592     expect(RGB(0,0,0), color);
593     prev = SendMessage(hwnd, MCM_SETCOLOR, MCSC_BACKGROUND, RGB(255,255,255));
594     expect(color, prev);
595     color = SendMessage(hwnd, MCM_GETCOLOR, MCSC_BACKGROUND, 0);
596     expect(RGB(255,255,255), color);
597
598     color = SendMessage(hwnd, MCM_GETCOLOR, MCSC_MONTHBK, 0);
599     prev = SendMessage(hwnd, MCM_SETCOLOR, MCSC_MONTHBK, RGB(0,0,0));
600     expect(color, prev);
601     color = SendMessage(hwnd, MCM_GETCOLOR, MCSC_MONTHBK, 0);
602     expect(RGB(0,0,0), color);
603     prev = SendMessage(hwnd, MCM_SETCOLOR, MCSC_MONTHBK, RGB(255,255,255));
604     expect(color, prev);
605     color = SendMessage(hwnd, MCM_GETCOLOR, MCSC_MONTHBK, 0);
606     expect(RGB(255,255,255), color);
607
608     color = SendMessage(hwnd, MCM_GETCOLOR, MCSC_TEXT, 0);
609     prev = SendMessage(hwnd, MCM_SETCOLOR, MCSC_TEXT, RGB(0,0,0));
610     expect(color, prev);
611     color = SendMessage(hwnd, MCM_GETCOLOR, MCSC_TEXT, 0);
612     expect(RGB(0,0,0), color);
613     prev = SendMessage(hwnd, MCM_SETCOLOR, MCSC_TEXT, RGB(255,255,255));
614     expect(color, prev);
615     color = SendMessage(hwnd, MCM_GETCOLOR, MCSC_TEXT, 0);
616     expect(RGB(255,255,255), color);
617
618     color = SendMessage(hwnd, MCM_GETCOLOR, MCSC_TITLEBK, 0);
619     prev = SendMessage(hwnd, MCM_SETCOLOR, MCSC_TITLEBK, RGB(0,0,0));
620     expect(color, prev);
621     color = SendMessage(hwnd, MCM_GETCOLOR, MCSC_TITLEBK, 0);
622     expect(RGB(0,0,0), color);
623     prev = SendMessage(hwnd, MCM_SETCOLOR, MCSC_TITLEBK, RGB(255,255,255));
624     expect(color, prev);
625     color = SendMessage(hwnd, MCM_GETCOLOR, MCSC_TITLEBK, 0);
626     expect(RGB(255,255,255), color);
627
628     color = SendMessage(hwnd, MCM_GETCOLOR, MCSC_TITLETEXT, 0);
629     prev = SendMessage(hwnd, MCM_SETCOLOR, MCSC_TITLETEXT, RGB(0,0,0));
630     expect(color, prev);
631     color = SendMessage(hwnd, MCM_GETCOLOR, MCSC_TITLETEXT, 0);
632     expect(RGB(0,0,0), color);
633     prev = SendMessage(hwnd, MCM_SETCOLOR, MCSC_TITLETEXT, RGB(255,255,255));
634     expect(color, prev);
635     color = SendMessage(hwnd, MCM_GETCOLOR, MCSC_TITLETEXT, 0);
636     expect(RGB(255,255,255), color);
637
638     color = SendMessage(hwnd, MCM_GETCOLOR, MCSC_TRAILINGTEXT, 0);
639     prev = SendMessage(hwnd, MCM_SETCOLOR, MCSC_TRAILINGTEXT, RGB(0,0,0));
640     expect(color, prev);
641     color = SendMessage(hwnd, MCM_GETCOLOR, MCSC_TRAILINGTEXT, 0);
642     expect(RGB(0,0,0), color);
643     prev = SendMessage(hwnd, MCM_SETCOLOR, MCSC_TRAILINGTEXT, RGB(255,255,255));
644     expect(color, prev);
645     color = SendMessage(hwnd, MCM_GETCOLOR, MCSC_TRAILINGTEXT, 0);
646     expect(RGB(255,255,255), color);
647
648     DestroyWindow(hwnd);
649 }
650
651 static void test_currdate(void)
652 {
653     SYSTEMTIME st_original, st_new, st_test;
654     int res;
655     HWND hwnd;
656
657     hwnd = create_monthcal_control(0);
658
659     flush_sequences(sequences, NUM_MSG_SEQUENCES);
660
661     /* Setter and Getters for current date selected */
662     st_original.wYear = 2000;
663     st_original.wMonth = 11;
664     st_original.wDay = 28;
665     st_original.wHour = 11;
666     st_original.wMinute = 59;
667     st_original.wSecond = 30;
668     st_original.wMilliseconds = 0;
669     st_original.wDayOfWeek = 0;
670
671     st_new = st_test = st_original;
672
673     /* Should not validate the time */
674     res = SendMessage(hwnd, MCM_SETCURSEL, 0, (LPARAM)&st_test);
675     expect(1,res);
676
677     /* Overflow matters, check for wDay */
678     st_test.wDay += 4;
679     res = SendMessage(hwnd, MCM_SETCURSEL, 0, (LPARAM)&st_test);
680     expect(0,res);
681
682     /* correct wDay before checking for wMonth */
683     st_test.wDay -= 4;
684     expect(st_original.wDay, st_test.wDay);
685
686     /* Overflow matters, check for wMonth */
687     st_test.wMonth += 4;
688     res = SendMessage(hwnd, MCM_SETCURSEL, 0, (LPARAM)&st_test);
689     expect(0,res);
690
691     /* checking if gets the information right, modify st_new */
692     st_new.wYear += 4;
693     st_new.wMonth += 4;
694     st_new.wDay += 4;
695     st_new.wHour += 4;
696     st_new.wMinute += 4;
697     st_new.wSecond += 4;
698
699     res = SendMessage(hwnd, MCM_GETCURSEL, 0, (LPARAM)&st_new);
700     expect(1, res);
701
702     /* st_new change to st_origin, above settings with overflow */
703     /* should not change the current settings */
704     expect(st_original.wYear, st_new.wYear);
705     expect(st_original.wMonth, st_new.wMonth);
706     expect(st_original.wDay, st_new.wDay);
707     ok(st_original.wHour == st_new.wHour ||
708        broken(0 == st_new.wHour), /* comctl32 <= 4.70 */
709        "Expected %d, got %d\n", st_original.wHour, st_new.wHour);
710     ok(st_original.wMinute == st_new.wMinute ||
711        broken(0 == st_new.wMinute), /* comctl32 <= 4.70 */
712        "Expected %d, got %d\n", st_original.wMinute, st_new.wMinute);
713     ok(st_original.wSecond == st_new.wSecond ||
714        broken(0 == st_new.wSecond), /* comctl32 <= 4.70 */
715        "Expected %d, got %d\n", st_original.wSecond, st_new.wSecond);
716
717     /* lparam cannot be NULL */
718     res = SendMessage(hwnd, MCM_GETCURSEL, 0, 0);
719     expect(0, res);
720
721     ok_sequence(sequences, MONTHCAL_SEQ_INDEX, monthcal_curr_date_seq, "monthcal currDate", TRUE);
722
723     /* December, 31, 9999 is the maximum allowed date */
724     memset(&st_new, 0, sizeof(st_new));
725     st_new.wYear = 9999;
726     st_new.wMonth = 12;
727     st_new.wDay = 31;
728     res = SendMessage(hwnd, MCM_SETCURSEL, 0, (LPARAM)&st_new);
729     expect(1, res);
730     memset(&st_test, 0, sizeof(st_test));
731     res = SendMessage(hwnd, MCM_GETCURSEL, 0, (LPARAM)&st_test);
732     expect(1, res);
733     expect(st_new.wYear, st_test.wYear);
734     expect(st_new.wMonth, st_test.wMonth);
735     expect(st_new.wDay, st_test.wDay);
736     expect(st_new.wHour, st_test.wHour);
737     expect(st_new.wMinute, st_test.wMinute);
738     expect(st_new.wSecond, st_test.wSecond);
739     /* try one day later */
740     st_original = st_new;
741     st_new.wYear = 10000;
742     st_new.wMonth = 1;
743     st_new.wDay = 1;
744     res = SendMessage(hwnd, MCM_SETCURSEL, 0, (LPARAM)&st_new);
745     ok(0 == res ||
746        broken(1 == res), /* comctl32 <= 4.72 */
747        "Expected 0, got %d\n", res);
748     if (0 == res)
749     {
750         memset(&st_test, 0, sizeof(st_test));
751         res = SendMessage(hwnd, MCM_GETCURSEL, 0, (LPARAM)&st_test);
752         expect(1, res);
753         expect(st_original.wYear, st_test.wYear);
754         expect(st_original.wMonth, st_test.wMonth);
755         expect(st_original.wDay, st_test.wDay);
756         expect(st_original.wHour, st_test.wHour);
757         expect(st_original.wMinute, st_test.wMinute);
758         expect(st_original.wSecond, st_test.wSecond);
759     }
760
761     /* setting selection equal to current reports success even if out range */
762     memset(&st_new, 0, sizeof(st_new));
763     st_new.wYear = 2009;
764     st_new.wDay  = 5;
765     st_new.wMonth = 10;
766     res = SendMessage(hwnd, MCM_SETCURSEL, 0, (LPARAM)&st_new);
767     expect(1, res);
768     memset(&st_test, 0, sizeof(st_test));
769     st_test.wYear = 2009;
770     st_test.wDay  = 6;
771     st_test.wMonth = 10;
772     res = SendMessage(hwnd, MCM_SETRANGE, GDTR_MIN, (LPARAM)&st_test);
773     expect(1, res);
774     /* set to current again */
775     res = SendMessage(hwnd, MCM_SETCURSEL, 0, (LPARAM)&st_new);
776     expect(1, res);
777
778     /* set with invalid day of week */
779     memset(&st_test, 0, sizeof(st_test));
780     st_test.wYear = 2009;
781     st_test.wDay  = 7;
782     st_test.wMonth = 10;
783     st_test.wDayOfWeek = 100;
784     res = SendMessage(hwnd, MCM_SETCURSEL, 0, (LPARAM)&st_test);
785     expect(1, res);
786
787     memset(&st_test, 0, sizeof(st_test));
788     res = SendMessage(hwnd, MCM_GETCURSEL, 0, (LPARAM)&st_test);
789     expect(1, res);
790     expect(2009, st_test.wYear);
791     expect(7, st_test.wDay);
792     expect(10, st_test.wMonth);
793     expect(3, st_test.wDayOfWeek);
794
795     DestroyWindow(hwnd);
796 }
797
798 static void test_firstDay(void)
799 {
800     int res, fday, i, prev;
801     CHAR b[128], caltype[3];
802     LCID lcid = LOCALE_USER_DEFAULT;
803     HWND hwnd;
804     LRESULT ret;
805
806     SetLastError(0xdeadbeef);
807     ret = GetLocaleInfoA(lcid, LOCALE_ICALENDARTYPE, caltype, 3);
808     if (ret == 0) {
809         skip("Must know local calendar type (%x)\n", GetLastError());
810         return;
811     } else if (atoi(caltype) != CAL_GREGORIAN) {
812         skip("MonthCalendar Control only supports Gregorian calendar (type: %s)\n", caltype);
813         return;
814     }
815
816     hwnd = create_monthcal_control(0);
817
818     flush_sequences(sequences, NUM_MSG_SEQUENCES);
819
820     /* Setter and Getters for first day of week */
821     /* check for locale first day */
822     if(GetLocaleInfoA(lcid, LOCALE_IFIRSTDAYOFWEEK, b, 128)){
823         fday = atoi(b);
824         trace("fday: %d\n", fday);
825         res = SendMessage(hwnd, MCM_GETFIRSTDAYOFWEEK, 0, 0);
826         expect(fday, res);
827         prev = fday;
828
829         /* checking for the values that actually will be stored as */
830         /* current first day when we set a new value */
831         for (i = -5; i < 12; i++){
832             res = SendMessage(hwnd, MCM_SETFIRSTDAYOFWEEK, 0, i);
833             expect(prev, res);
834             res = SendMessage(hwnd, MCM_GETFIRSTDAYOFWEEK, 0, 0);
835             prev = res;
836
837             if (i == -1){
838                 expect(MAKELONG(fday, FALSE), res);
839             }else if (i >= 7){
840                 /* out of range sets max first day of week, locale is ignored */
841                 expect(MAKELONG(6, TRUE), res);
842             }else{
843                 expect(MAKELONG(i, TRUE), res);
844             }
845         }
846
847         ok_sequence(sequences, MONTHCAL_SEQ_INDEX, monthcal_first_day_seq, "monthcal firstDay", FALSE);
848
849     }else{
850         skip("Cannot retrieve first day of the week\n");
851     }
852
853     DestroyWindow(hwnd);
854 }
855
856 static void test_unicode(void)
857 {
858     int res, temp;
859     HWND hwnd;
860
861     hwnd = create_monthcal_control(0);
862
863     flush_sequences(sequences, NUM_MSG_SEQUENCES);
864
865     /* Setter and Getters for Unicode format */
866
867     /* getting the current settings */
868     temp = SendMessage(hwnd, MCM_GETUNICODEFORMAT, 0, 0);
869
870     /* setting to 1, should return previous settings */
871     res = SendMessage(hwnd, MCM_SETUNICODEFORMAT, 1, 0);
872     expect(temp, res);
873
874     /* current setting is 1, so, should return 1 */
875     res = SendMessage(hwnd, MCM_GETUNICODEFORMAT, 0, 0);
876     ok(1 == res ||
877        broken(0 == res), /* comctl32 <= 4.70 */
878        "Expected 1, got %d\n", res);
879
880     /* setting to 0, should return previous settings */
881     res = SendMessage(hwnd, MCM_SETUNICODEFORMAT, 0, 0);
882     ok(1 == res ||
883        broken(0 == res), /* comctl32 <= 4.70 */
884        "Expected 1, got %d\n", res);
885
886     /* current setting is 0, so, it should return 0 */
887     res = SendMessage(hwnd, MCM_GETUNICODEFORMAT, 0, 0);
888     expect(0, res);
889
890     /* should return previous settings */
891     res = SendMessage(hwnd, MCM_SETUNICODEFORMAT, 1, 0);
892     expect(0, res);
893
894     ok_sequence(sequences, MONTHCAL_SEQ_INDEX, monthcal_unicode_seq, "monthcal unicode", FALSE);
895
896     DestroyWindow(hwnd);
897 }
898
899 static void test_hittest(void)
900 {
901     typedef struct hittest_test
902     {
903         UINT ht;
904         int  todo;
905     } hittest_test_t;
906
907     static const hittest_test_t title_hits[] = {
908         /* Start is the same everywhere */
909         { MCHT_TITLE,        0 },
910         { MCHT_TITLEBTNPREV, 0 },
911         /* The middle piece is only tested for presence of items */
912         /* End is the same everywhere */
913         { MCHT_TITLEBTNNEXT, 0 },
914         { MCHT_TITLE,        0 },
915         { MCHT_NOWHERE,      1 }
916     };
917
918     MCHITTESTINFO mchit;
919     UINT res, old_res;
920     SYSTEMTIME st;
921     LONG x;
922     UINT title_index;
923     HWND hwnd;
924     RECT r;
925     char yearmonth[80], *locale_month, *locale_year;
926     int month_count, year_count;
927     BOOL in_the_middle;
928
929     memset(&mchit, 0, sizeof(MCHITTESTINFO));
930
931     hwnd = create_monthcal_control(0);
932
933     /* test with invalid structure size */
934     mchit.cbSize = MCHITTESTINFO_V1_SIZE - 1;
935     mchit.pt.x = 0;
936     mchit.pt.y = 0;
937     res = SendMessage(hwnd, MCM_HITTEST, 0, (LPARAM)&mchit);
938     expect(0, mchit.pt.x);
939     expect(0, mchit.pt.y);
940     expect(~0u, res);
941     expect(0, mchit.uHit);
942     /* test with invalid pointer */
943     res = SendMessage(hwnd, MCM_HITTEST, 0, 0);
944     expect(~0u, res);
945
946     /* resize control to display single Calendar */
947     res = SendMessage(hwnd, MCM_GETMINREQRECT, 0, (LPARAM)&r);
948     if (res == 0)
949     {
950         win_skip("Message MCM_GETMINREQRECT unsupported. Skipping.\n");
951         DestroyWindow(hwnd);
952         return;
953     }
954     MoveWindow(hwnd, 0, 0, r.right, r.bottom, FALSE);
955
956     flush_sequences(sequences, NUM_MSG_SEQUENCES);
957
958     st.wYear = 2007;
959     st.wMonth = 4;
960     st.wDay = 11;
961     st.wHour = 1;
962     st.wMinute = 0;
963     st.wSecond = 0;
964     st.wMilliseconds = 0;
965     st.wDayOfWeek = 0;
966
967     res = SendMessage(hwnd, MCM_SETCURSEL, 0, (LPARAM)&st);
968     expect(1,res);
969
970     /* (0, 0) is the top left of the control - title */
971     mchit.cbSize = MCHITTESTINFO_V1_SIZE;
972     mchit.pt.x = 0;
973     mchit.pt.y = 0;
974     res = SendMessage(hwnd, MCM_HITTEST, 0, (LPARAM)&mchit);
975     expect(0, mchit.pt.x);
976     expect(0, mchit.pt.y);
977     expect(mchit.uHit, res);
978     expect_hex(MCHT_TITLE, res);
979
980     /* bottom right of the control and should not be active */
981     mchit.pt.x = r.right;
982     mchit.pt.y = r.bottom;
983     res = SendMessage(hwnd, MCM_HITTEST, 0, (LPARAM)&mchit);
984     expect(r.right,  mchit.pt.x);
985     expect(r.bottom, mchit.pt.y);
986     expect(mchit.uHit, res);
987     todo_wine expect_hex(MCHT_NOWHERE, res);
988
989     /* completely out of the control, should not be active */
990     mchit.pt.x = 2 * r.right;
991     mchit.pt.y = 2 * r.bottom;
992     res = SendMessage(hwnd, MCM_HITTEST, 0, (LPARAM) & mchit);
993     expect(2 * r.right, mchit.pt.x);
994     expect(2 * r.bottom, mchit.pt.y);
995     expect(mchit.uHit, res);
996     todo_wine expect_hex(MCHT_NOWHERE, res);
997
998     /* in active area - day of the week */
999     mchit.pt.x = r.right / 2;
1000     mchit.pt.y = r.bottom / 2;
1001     res = SendMessage(hwnd, MCM_HITTEST, 0, (LPARAM) & mchit);
1002     expect(r.right / 2, mchit.pt.x);
1003     expect(r.bottom / 2, mchit.pt.y);
1004     expect(mchit.uHit, res);
1005     expect_hex(MCHT_CALENDARDATE, res);
1006
1007     /* in active area - day of the week #2 */
1008     mchit.pt.x = r.right / 14; /* half of first day rect */
1009     mchit.pt.y = r.bottom / 2;
1010     res = SendMessage(hwnd, MCM_HITTEST, 0, (LPARAM) & mchit);
1011     expect(r.right / 14, mchit.pt.x);
1012     expect(r.bottom / 2, mchit.pt.y);
1013     expect(mchit.uHit, res);
1014     expect_hex(MCHT_CALENDARDATE, res);
1015
1016     /* in active area - date from prev month */
1017     mchit.pt.x = r.right / 14; /* half of first day rect */
1018     mchit.pt.y = 6 * r.bottom / 19;
1019     res = SendMessage(hwnd, MCM_HITTEST, 0, (LPARAM) & mchit);
1020     expect(r.right / 14, mchit.pt.x);
1021     expect(6 * r.bottom / 19, mchit.pt.y);
1022     expect(mchit.uHit, res);
1023     expect_hex(MCHT_CALENDARDATEPREV, res);
1024
1025 #if 0
1026     /* (125, 115) is in active area - date from this month */
1027     mchit.pt.x = 125;
1028     mchit.pt.y = 115;
1029     res = SendMessage(hwnd, MCM_HITTEST, 0, (LPARAM) & mchit);
1030     expect(125, mchit.pt.x);
1031     expect(115, mchit.pt.y);
1032     expect(mchit.uHit, res);
1033     expect(MCHT_CALENDARDATE, res);
1034 #endif
1035
1036     /* in active area - date from next month */
1037     mchit.pt.x = 11 * r.right / 14;
1038     mchit.pt.y = 16 * r.bottom / 19;
1039     res = SendMessage(hwnd, MCM_HITTEST, 0, (LPARAM) & mchit);
1040     expect(11 * r.right / 14, mchit.pt.x);
1041     expect(16 * r.bottom / 19, mchit.pt.y);
1042     expect(mchit.uHit, res);
1043     expect_hex(MCHT_CALENDARDATENEXT, res);
1044
1045     /* in active area - today link */
1046     mchit.pt.x = r.right / 14;
1047     mchit.pt.y = 18 * r.bottom / 19;
1048     res = SendMessage(hwnd, MCM_HITTEST, 0, (LPARAM) & mchit);
1049     expect(r.right / 14, mchit.pt.x);
1050     expect(18 * r.bottom / 19, mchit.pt.y);
1051     expect(mchit.uHit, res);
1052     expect_hex(MCHT_TODAYLINK, res);
1053
1054     /* in active area - today link */
1055     mchit.pt.x = r.right / 2;
1056     mchit.pt.y = 18 * r.bottom / 19;
1057     res = SendMessage(hwnd, MCM_HITTEST, 0, (LPARAM) & mchit);
1058     expect(r.right / 2, mchit.pt.x);
1059     expect(18 * r.bottom / 19, mchit.pt.y);
1060     expect(mchit.uHit, res);
1061     expect_hex(MCHT_TODAYLINK, res);
1062
1063     /* in active area - today link */
1064     mchit.pt.x = r.right / 10;
1065     mchit.pt.y = 18 * r.bottom / 19;
1066     res = SendMessage(hwnd, MCM_HITTEST, 0, (LPARAM) & mchit);
1067     expect(r.right / 10, mchit.pt.x);
1068     expect(18 * r.bottom / 19, mchit.pt.y);
1069     expect(mchit.uHit, res);
1070     expect_hex(MCHT_TODAYLINK, res);
1071
1072     ok_sequence(sequences, MONTHCAL_SEQ_INDEX, monthcal_hit_test_seq, "monthcal hit test", TRUE);
1073
1074     /* The horizontal position of title bar elements depends on locale (y pos
1075        is constant), so we sample across a horizontal line and make sure we
1076        find all elements. */
1077
1078     /* Get the format of the title */
1079     GetLocaleInfo(LOCALE_USER_DEFAULT, LOCALE_SYEARMONTH, yearmonth, 80);
1080     /* Find out if we have a month and/or year */
1081     locale_year = strstr(yearmonth, "y");
1082     locale_month = strstr(yearmonth, "M");
1083
1084     mchit.pt.x = 0;
1085     mchit.pt.y = (5/2) * r.bottom / 19;
1086     title_index = 0;
1087     old_res = SendMessage(hwnd, MCM_HITTEST, 0, (LPARAM) & mchit);
1088     expect_hex(title_hits[title_index].ht, old_res);
1089
1090     in_the_middle = FALSE;
1091     month_count = year_count = 0;
1092     for (x = 0; x < r.right; x++){
1093         mchit.pt.x = x;
1094         res = SendMessage(hwnd, MCM_HITTEST, 0, (LPARAM) & mchit);
1095         expect(x, mchit.pt.x);
1096         expect((5/2) * r.bottom / 19, mchit.pt.y);
1097         expect(mchit.uHit, res);
1098         if (res != old_res) {
1099
1100             if (old_res == MCHT_TITLEBTNPREV)
1101                 in_the_middle = TRUE;
1102
1103             if (res == MCHT_TITLEBTNNEXT)
1104                 in_the_middle = FALSE;
1105
1106             if (in_the_middle) {
1107                 if (res == MCHT_TITLEMONTH)
1108                     month_count++;
1109                 else if (res == MCHT_TITLEYEAR)
1110                     year_count++;
1111             } else {
1112                 title_index++;
1113
1114                 if (sizeof(title_hits) / sizeof(title_hits[0]) <= title_index)
1115                     break;
1116
1117                 if (title_hits[title_index].todo) {
1118                     todo_wine
1119                     ok(title_hits[title_index].ht == res, "Expected %x, got %x, pos %d\n",
1120                                                           title_hits[title_index].ht, res, x);
1121                 } else {
1122                     ok(title_hits[title_index].ht == res, "Expected %x, got %x, pos %d\n",
1123                                                           title_hits[title_index].ht, res, x);
1124                 }
1125             }
1126             old_res = res;
1127         }
1128     }
1129
1130     /* There are some limits, even if LOCALE_SYEARMONTH contains rubbish
1131      * or no month/year indicators at all */
1132     if (locale_month)
1133         todo_wine ok(month_count == 1, "Expected 1 month item, got %d\n", month_count);
1134     else
1135         ok(month_count <= 1, "Too many month items: %d\n", month_count);
1136
1137     if (locale_year)
1138         todo_wine ok(year_count == 1, "Expected 1 year item, got %d\n", year_count);
1139     else
1140         ok(year_count <= 1, "Too many year items: %d\n", year_count);
1141
1142     todo_wine ok(month_count + year_count >= 1, "Not enough month and year items\n");
1143
1144     ok(r.right <= x && title_index + 1 == sizeof(title_hits) / sizeof(title_hits[0]),
1145        "Wrong title layout\n");
1146
1147     DestroyWindow(hwnd);
1148 }
1149
1150 static void test_todaylink(void)
1151 {
1152     MCHITTESTINFO mchit;
1153     SYSTEMTIME st_test, st_new;
1154     UINT res;
1155     HWND hwnd;
1156     RECT r;
1157
1158     memset(&mchit, 0, sizeof(MCHITTESTINFO));
1159
1160     hwnd = create_monthcal_control(0);
1161
1162     res = SendMessage(hwnd, MCM_GETMINREQRECT, 0, (LPARAM)&r);
1163     expect(1, res);
1164     MoveWindow(hwnd, 0, 0, r.right, r.bottom, FALSE);
1165
1166     flush_sequences(sequences, NUM_MSG_SEQUENCES);
1167
1168     /* hit active area - today link */
1169     mchit.cbSize = MCHITTESTINFO_V1_SIZE;
1170     mchit.pt.x = r.right / 14;
1171     mchit.pt.y = 18 * r.bottom / 19;
1172     res = SendMessage(hwnd, MCM_HITTEST, 0, (LPARAM) & mchit);
1173     expect(r.right / 14, mchit.pt.x);
1174     expect(18 * r.bottom / 19, mchit.pt.y);
1175     expect(mchit.uHit, res);
1176     expect(MCHT_TODAYLINK, res);
1177
1178     st_test.wDay = 1;
1179     st_test.wMonth = 1;
1180     st_test.wYear = 2005;
1181
1182     res = SendMessage(hwnd, MCM_SETTODAY, 0, (LPARAM)&st_test);
1183     expect(0, res);
1184
1185     memset(&st_new, 0, sizeof(st_new));
1186     res = SendMessage(hwnd, MCM_GETTODAY, 0, (LPARAM)&st_new);
1187     expect(1, res);
1188     expect(1, st_new.wDay);
1189     expect(1, st_new.wMonth);
1190     expect(2005, st_new.wYear);
1191
1192     res = SendMessage(hwnd, WM_LBUTTONDOWN, MK_LBUTTON, MAKELONG(mchit.pt.x, mchit.pt.y));
1193     expect(0, res);
1194
1195     memset(&st_new, 0, sizeof(st_new));
1196     res = SendMessage(hwnd, MCM_GETCURSEL, 0, (LPARAM)&st_new);
1197     expect(1, res);
1198     expect(1, st_new.wDay);
1199     expect(1, st_new.wMonth);
1200     expect(2005, st_new.wYear);
1201
1202     ok_sequence(sequences, MONTHCAL_SEQ_INDEX, monthcal_todaylink_seq, "monthcal hit test", TRUE);
1203
1204     DestroyWindow(hwnd);
1205 }
1206
1207 static void test_today(void)
1208 {
1209     SYSTEMTIME st_test, st_new;
1210     int res;
1211     HWND hwnd;
1212
1213     hwnd = create_monthcal_control(0);
1214
1215     flush_sequences(sequences, NUM_MSG_SEQUENCES);
1216
1217     /* Setter and Getters for "today" information */
1218
1219     /* check for overflow, should be ok */
1220     memset(&st_test, 0, sizeof(st_test));
1221     st_test.wDay = 38;
1222     st_test.wMonth = 38;
1223
1224     st_new.wDay = 27;
1225     st_new.wMonth = 27;
1226
1227     res = SendMessage(hwnd, MCM_SETTODAY, 0, (LPARAM)&st_test);
1228     expect(0, res);
1229
1230     res = SendMessage(hwnd, MCM_GETTODAY, 0, (LPARAM)&st_new);
1231     expect(1, res);
1232
1233     /* st_test should not change */
1234     expect(38, st_test.wDay);
1235     expect(38, st_test.wMonth);
1236
1237     /* st_new should change, overflow does not matter */
1238     expect(38, st_new.wDay);
1239     expect(38, st_new.wMonth);
1240
1241     /* check for zero, should be ok*/
1242     st_test.wDay = 0;
1243     st_test.wMonth = 0;
1244
1245     res = SendMessage(hwnd, MCM_SETTODAY, 0, (LPARAM)&st_test);
1246     expect(0, res);
1247
1248     res = SendMessage(hwnd, MCM_GETTODAY, 0, (LPARAM)&st_new);
1249     expect(1, res);
1250
1251     /* st_test should not change */
1252     expect(0, st_test.wDay);
1253     expect(0, st_test.wMonth);
1254
1255     /* st_new should change to zero*/
1256     expect(0, st_new.wDay);
1257     expect(0, st_new.wMonth);
1258
1259     ok_sequence(sequences, MONTHCAL_SEQ_INDEX, monthcal_today_seq, "monthcal today", TRUE);
1260
1261     DestroyWindow(hwnd);
1262 }
1263
1264 static void test_scroll(void)
1265 {
1266     int res;
1267     HWND hwnd;
1268
1269     hwnd = create_monthcal_control(0);
1270
1271     flush_sequences(sequences, NUM_MSG_SEQUENCES);
1272
1273     /* Setter and Getters for scroll rate */
1274     res = SendMessage(hwnd, MCM_SETMONTHDELTA, 2, 0);
1275     expect(0, res);
1276
1277     res = SendMessage(hwnd, MCM_SETMONTHDELTA, 3, 0);
1278     expect(2, res);
1279     res = SendMessage(hwnd, MCM_GETMONTHDELTA, 0, 0);
1280     expect(3, res);
1281
1282     res = SendMessage(hwnd, MCM_SETMONTHDELTA, 12, 0);
1283     expect(3, res);
1284     res = SendMessage(hwnd, MCM_GETMONTHDELTA, 0, 0);
1285     expect(12, res);
1286
1287     res = SendMessage(hwnd, MCM_SETMONTHDELTA, 15, 0);
1288     expect(12, res);
1289     res = SendMessage(hwnd, MCM_GETMONTHDELTA, 0, 0);
1290     expect(15, res);
1291
1292     res = SendMessage(hwnd, MCM_SETMONTHDELTA, -5, 0);
1293     expect(15, res);
1294     res = SendMessage(hwnd, MCM_GETMONTHDELTA, 0, 0);
1295     expect(-5, res);
1296
1297     ok_sequence(sequences, MONTHCAL_SEQ_INDEX, monthcal_scroll_seq, "monthcal scroll", FALSE);
1298
1299     DestroyWindow(hwnd);
1300 }
1301
1302 static void test_monthrange(void)
1303 {
1304     int res;
1305     SYSTEMTIME st_visible[2], st_daystate[2], st;
1306     HWND hwnd;
1307     RECT r;
1308
1309     hwnd = create_monthcal_control(0);
1310
1311     st_visible[0].wYear = 0;
1312     st_visible[0].wMonth = 0;
1313     st_visible[0].wDay = 0;
1314     st_daystate[1] = st_daystate[0] = st_visible[1] = st_visible[0];
1315
1316     st.wYear = 2000;
1317     st.wMonth = 11;
1318     st.wDay = 28;
1319     st.wHour = 11;
1320     st.wMinute = 59;
1321     st.wSecond = 30;
1322     st.wMilliseconds = 0;
1323     st.wDayOfWeek = 0;
1324
1325     res = SendMessage(hwnd, MCM_SETCURSEL, 0, (LPARAM)&st);
1326     expect(1,res);
1327
1328     /* to be locale independent */
1329     SendMessage(hwnd, MCM_SETFIRSTDAYOFWEEK, 0, (LPARAM)6);
1330
1331     res = SendMessage(hwnd, MCM_GETMINREQRECT, 0, (LPARAM)&r);
1332     expect(TRUE, res);
1333     /* resize control to display two Calendars */
1334     MoveWindow(hwnd, 0, 0, r.right, (5/2)*r.bottom, FALSE);
1335
1336     flush_sequences(sequences, NUM_MSG_SEQUENCES);
1337
1338     res = SendMessage(hwnd, MCM_GETMONTHRANGE, GMR_VISIBLE, (LPARAM)st_visible);
1339     expect(2, res);
1340     expect(2000, st_visible[0].wYear);
1341     expect(11, st_visible[0].wMonth);
1342     expect(1, st_visible[0].wDay);
1343     expect(2000, st_visible[1].wYear);
1344     expect(12, st_visible[1].wMonth);
1345     expect(31, st_visible[1].wDay);
1346
1347     res = SendMessage(hwnd, MCM_GETMONTHRANGE, GMR_DAYSTATE, (LPARAM)st_daystate);
1348     expect(4, res);
1349     expect(2000, st_daystate[0].wYear);
1350     expect(10, st_daystate[0].wMonth);
1351     expect(29, st_daystate[0].wDay);
1352     expect(2001, st_daystate[1].wYear);
1353     expect(1, st_daystate[1].wMonth);
1354     expect(6, st_daystate[1].wDay);
1355
1356     ok_sequence(sequences, MONTHCAL_SEQ_INDEX, monthcal_monthrange_seq, "monthcal monthrange", FALSE);
1357
1358     /* with null date array parameter */
1359     res = SendMessage(hwnd, MCM_GETMONTHRANGE, GMR_VISIBLE, 0);
1360     expect(2, res);
1361
1362     res = SendMessage(hwnd, MCM_GETMONTHRANGE, GMR_DAYSTATE, 0);
1363     expect(4, res);
1364
1365     /* resize control to display single Calendar */
1366     MoveWindow(hwnd, 0, 0, r.right, r.bottom, FALSE);
1367
1368     memset(&st, 0, sizeof(st));
1369     st.wMonth = 9;
1370     st.wYear  = 1752;
1371     st.wDay   = 14;
1372
1373     res = SendMessage(hwnd, MCM_SETCURSEL, 0, (LPARAM)&st);
1374     expect(1, res);
1375
1376     /* September 1752 has 19 days */
1377     res = SendMessage(hwnd, MCM_GETMONTHRANGE, GMR_VISIBLE, (LPARAM)st_visible);
1378     expect(1, res);
1379
1380     expect(1752, st_visible[0].wYear);
1381     expect(9, st_visible[0].wMonth);
1382     ok(14 == st_visible[0].wDay ||
1383        broken(1 == st_visible[0].wDay), /* comctl32 <= 4.72 */
1384        "Expected 14, got %d\n", st_visible[0].wDay);
1385
1386     expect(1752, st_visible[1].wYear);
1387     expect(9, st_visible[1].wMonth);
1388     expect(19, st_visible[1].wDay);
1389
1390     DestroyWindow(hwnd);
1391 }
1392
1393 static void test_maxselday(void)
1394 {
1395     int res;
1396     HWND hwnd;
1397     DWORD style;
1398
1399     hwnd = create_monthcal_control(0);
1400     /* if no style specified default to 1 */
1401     res = SendMessage(hwnd, MCM_GETMAXSELCOUNT, 0, 0);
1402     expect(1, res);
1403     res = SendMessage(hwnd, MCM_SETMAXSELCOUNT, 5, 0);
1404     expect(0, res);
1405     res = SendMessage(hwnd, MCM_GETMAXSELCOUNT, 0, 0);
1406     expect(1, res);
1407
1408     /* try to set style */
1409     style = GetWindowLong(hwnd, GWL_STYLE);
1410     SetWindowLong(hwnd, GWL_STYLE, style | MCS_MULTISELECT);
1411     style = GetWindowLong(hwnd, GWL_STYLE);
1412     ok(!(style & MCS_MULTISELECT), "Expected MCS_MULTISELECT not to be set\n");
1413     DestroyWindow(hwnd);
1414
1415     hwnd = create_monthcal_control(MCS_MULTISELECT);
1416     /* try to remove style */
1417     style = GetWindowLong(hwnd, GWL_STYLE);
1418     SetWindowLong(hwnd, GWL_STYLE, style & ~MCS_MULTISELECT);
1419     style = GetWindowLong(hwnd, GWL_STYLE);
1420     ok(style & MCS_MULTISELECT, "Expected MCS_MULTISELECT to be set\n");
1421     DestroyWindow(hwnd);
1422
1423     hwnd = create_monthcal_control(MCS_MULTISELECT);
1424
1425     /* default width is a week */
1426     res = SendMessage(hwnd, MCM_GETMAXSELCOUNT, 0, 0);
1427     expect(7, res);
1428
1429     flush_sequences(sequences, NUM_MSG_SEQUENCES);
1430
1431     /* Setter and Getters for max selected days */
1432     res = SendMessage(hwnd, MCM_SETMAXSELCOUNT, 5, 0);
1433     expect(1, res);
1434     res = SendMessage(hwnd, MCM_GETMAXSELCOUNT, 0, 0);
1435     expect(5, res);
1436
1437     res = SendMessage(hwnd, MCM_SETMAXSELCOUNT, 15, 0);
1438     expect(1, res);
1439     res = SendMessage(hwnd, MCM_GETMAXSELCOUNT, 0, 0);
1440     expect(15, res);
1441
1442     /* test invalid value */
1443     res = SendMessage(hwnd, MCM_SETMAXSELCOUNT, -1, 0);
1444     expect(0, res);
1445     res = SendMessage(hwnd, MCM_GETMAXSELCOUNT, 0, 0);
1446     expect(15, res);
1447
1448     ok_sequence(sequences, MONTHCAL_SEQ_INDEX, monthcal_max_sel_day_seq, "monthcal MaxSelDay", FALSE);
1449
1450     /* zero value is invalid too */
1451     res = SendMessage(hwnd, MCM_SETMAXSELCOUNT, 0, 0);
1452     expect(0, res);
1453     res = SendMessage(hwnd, MCM_GETMAXSELCOUNT, 0, 0);
1454     expect(15, res);
1455
1456     DestroyWindow(hwnd);
1457 }
1458
1459 static void test_size(void)
1460 {
1461     int res;
1462     RECT r1, r2;
1463     HFONT hFont1, hFont2;
1464     LOGFONTA logfont;
1465     HWND hwnd;
1466
1467     hwnd = create_monthcal_control(0);
1468
1469     lstrcpyA(logfont.lfFaceName, "Arial");
1470     memset(&logfont, 0, sizeof(logfont));
1471     logfont.lfHeight = 12;
1472     hFont1 = CreateFontIndirectA(&logfont);
1473
1474     logfont.lfHeight = 24;
1475     hFont2 = CreateFontIndirectA(&logfont);
1476
1477     /* initialize to a font we can compare against */
1478     SendMessage(hwnd, WM_SETFONT, (WPARAM)hFont1, 0);
1479     res = SendMessage(hwnd, MCM_GETMINREQRECT, 0, (LPARAM)&r1);
1480     ok(res, "SendMessage(MCM_GETMINREQRECT) failed\n");
1481
1482     /* check that setting a larger font results in an larger rect */
1483     SendMessage(hwnd, WM_SETFONT, (WPARAM)hFont2, 0);
1484     res = SendMessage(hwnd, MCM_GETMINREQRECT, 0, (LPARAM)&r2);
1485     ok(res, "SendMessage(MCM_GETMINREQRECT) failed\n");
1486
1487     OffsetRect(&r1, -r1.left, -r1.top);
1488     OffsetRect(&r2, -r2.left, -r2.top);
1489
1490     ok(r1.bottom < r2.bottom, "Failed to get larger rect with larger font\n");
1491
1492     DestroyWindow(hwnd);
1493 }
1494
1495 static void test_create(void)
1496 {
1497     HWND hwnd;
1498
1499     flush_sequences(sequences, NUM_MSG_SEQUENCES);
1500
1501     hwnd = create_monthcal_control(0);
1502     ok_sequence(sequences, PARENT_SEQ_INDEX, create_monthcal_control_seq, "create monthcal control", TRUE);
1503
1504     DestroyWindow(hwnd);
1505
1506     flush_sequences(sequences, NUM_MSG_SEQUENCES);
1507     hwnd = create_monthcal_control(MCS_MULTISELECT);
1508     ok_sequence(sequences, PARENT_SEQ_INDEX, create_monthcal_multi_sel_style_seq, "create monthcal (multi sel style)", TRUE);
1509     DestroyWindow(hwnd);
1510 }
1511
1512 static void test_destroy(void)
1513 {
1514     HWND hwnd;
1515
1516     hwnd = create_monthcal_control(0);
1517     flush_sequences(sequences, NUM_MSG_SEQUENCES);
1518     DestroyWindow(hwnd);
1519     ok_sequence(sequences, PARENT_SEQ_INDEX, destroy_monthcal_parent_msgs_seq, "Destroy monthcal (parent msg)", FALSE);
1520     ok_sequence(sequences, MONTHCAL_SEQ_INDEX, destroy_monthcal_child_msgs_seq, "Destroy monthcal (child msg)", FALSE);
1521
1522     /* MCS_MULTISELECT */
1523     hwnd = create_monthcal_control(MCS_MULTISELECT);
1524     flush_sequences(sequences, NUM_MSG_SEQUENCES);
1525     DestroyWindow(hwnd);
1526     ok_sequence(sequences, MONTHCAL_SEQ_INDEX, destroy_monthcal_multi_sel_style_seq, "Destroy monthcal (multi sel style)", FALSE);
1527 }
1528
1529 static void test_selrange(void)
1530 {
1531     HWND hwnd;
1532     SYSTEMTIME st, range[2], range2[2];
1533     BOOL ret, old_comctl32 = FALSE;
1534
1535     hwnd = create_monthcal_control(MCS_MULTISELECT);
1536
1537     /* just after creation selection should start and end today */
1538     ret = SendMessage(hwnd, MCM_GETTODAY, 0, (LPARAM)&st);
1539     expect(TRUE, ret);
1540
1541     memset(range, 0xcc, sizeof(range));
1542     ret = SendMessage(hwnd, MCM_GETSELRANGE, 0, (LPARAM)range);
1543     expect(TRUE, ret);
1544     expect(st.wYear,      range[0].wYear);
1545     expect(st.wMonth,     range[0].wMonth);
1546     expect(st.wDay,       range[0].wDay);
1547     if (range[0].wDayOfWeek != st.wDayOfWeek)
1548     {
1549         win_skip("comctl32 <= 4.70 doesn't set some values\n");
1550         old_comctl32 = TRUE;
1551     }
1552     else
1553     {
1554         expect(st.wDayOfWeek, range[0].wDayOfWeek);
1555         expect(st.wHour,      range[0].wHour);
1556         expect(st.wMinute,    range[0].wMinute);
1557         expect(st.wSecond,    range[0].wSecond);
1558         expect(st.wMilliseconds, range[0].wMilliseconds);
1559     }
1560
1561     expect(st.wYear,      range[1].wYear);
1562     expect(st.wMonth,     range[1].wMonth);
1563     expect(st.wDay,       range[1].wDay);
1564     if (!old_comctl32)
1565     {
1566         expect(st.wDayOfWeek, range[1].wDayOfWeek);
1567         expect(st.wHour,      range[1].wHour);
1568         expect(st.wMinute,    range[1].wMinute);
1569         expect(st.wSecond,    range[1].wSecond);
1570         expect(st.wMilliseconds, range[1].wMilliseconds);
1571     }
1572
1573     /* bounds are swapped if min > max */
1574     memset(&range[0], 0, sizeof(range[0]));
1575     range[0].wYear  = 2009;
1576     range[0].wMonth = 10;
1577     range[0].wDay   = 5;
1578     range[1] = range[0];
1579     range[1].wDay   = 3;
1580
1581     ret = SendMessage(hwnd, MCM_SETSELRANGE, 0, (LPARAM)range);
1582     expect(TRUE, ret);
1583
1584     ret = SendMessage(hwnd, MCM_GETSELRANGE, 0, (LPARAM)range2);
1585     expect(TRUE, ret);
1586
1587     expect(range[1].wYear,      range2[0].wYear);
1588     expect(range[1].wMonth,     range2[0].wMonth);
1589     expect(range[1].wDay,       range2[0].wDay);
1590     expect(6, range2[0].wDayOfWeek);
1591     expect(range[1].wHour,      range2[0].wHour);
1592     expect(range[1].wMinute,    range2[0].wMinute);
1593     expect(range[1].wSecond,    range2[0].wSecond);
1594     expect(range[1].wMilliseconds, range2[0].wMilliseconds);
1595
1596     expect(range[0].wYear,      range2[1].wYear);
1597     expect(range[0].wMonth,     range2[1].wMonth);
1598     expect(range[0].wDay,       range2[1].wDay);
1599     expect(1, range2[1].wDayOfWeek);
1600     expect(range[0].wHour,      range2[1].wHour);
1601     expect(range[0].wMinute,    range2[1].wMinute);
1602     expect(range[0].wSecond,    range2[1].wSecond);
1603     expect(range[0].wMilliseconds, range2[1].wMilliseconds);
1604
1605     /* try with range larger than maximum configured */
1606     memset(&range[0], 0, sizeof(range[0]));
1607     range[0].wYear  = 2009;
1608     range[0].wMonth = 10;
1609     range[0].wDay   = 1;
1610     range[1] = range[0];
1611
1612     ret = SendMessage(hwnd, MCM_SETSELRANGE, 0, (LPARAM)range);
1613     expect(TRUE, ret);
1614
1615     range[1] = range[0];
1616     /* default max. range is 7 days */
1617     range[1].wDay = 8;
1618
1619     ret = SendMessage(hwnd, MCM_SETSELRANGE, 0, (LPARAM)range);
1620     expect(FALSE, ret);
1621
1622     ret = SendMessage(hwnd, MCM_GETSELRANGE, 0, (LPARAM)range2);
1623     expect(TRUE, ret);
1624
1625     expect(range[0].wYear,  range2[0].wYear);
1626     expect(range[0].wMonth, range2[0].wMonth);
1627     expect(range[0].wDay,   range2[0].wDay);
1628     expect(range[0].wYear,  range2[1].wYear);
1629     expect(range[0].wMonth, range2[1].wMonth);
1630     expect(range[0].wDay,   range2[1].wDay);
1631
1632     DestroyWindow(hwnd);
1633 }
1634
1635 static void test_killfocus(void)
1636 {
1637     HWND hwnd;
1638     DWORD style;
1639
1640     hwnd = create_monthcal_control(0);
1641
1642     /* make parent invisible */
1643     style = GetWindowLong(parent_wnd, GWL_STYLE);
1644     SetWindowLong(parent_wnd, GWL_STYLE, style &~ WS_VISIBLE);
1645
1646     SendMessage(hwnd, WM_KILLFOCUS, (WPARAM)GetDesktopWindow(), 0);
1647
1648     style = GetWindowLong(hwnd, GWL_STYLE);
1649     ok(style & WS_VISIBLE, "Expected WS_VISIBLE to be set\n");
1650
1651     style = GetWindowLong(parent_wnd, GWL_STYLE);
1652     SetWindowLong(parent_wnd, GWL_STYLE, style | WS_VISIBLE);
1653
1654     DestroyWindow(hwnd);
1655 }
1656
1657 static void test_hittest_v6(void)
1658 {
1659     MCHITTESTINFO mchit;
1660     DWORD ret;
1661     HWND hwnd;
1662     RECT r;
1663
1664     hwnd = create_monthcal_control(0);
1665     SendMessage(hwnd, MCM_SETCALENDARBORDER, TRUE, 0);
1666
1667     SendMessage(hwnd, MCM_GETMINREQRECT, 0, (LPARAM)&r);
1668     /* reserving some area around calendar */
1669     MoveWindow(hwnd, 0, 0, r.right * 3 / 2, r.bottom * 3 / 2, FALSE);
1670     mchit.cbSize = sizeof(MCHITTESTINFO);
1671     mchit.pt.x = mchit.pt.y = 0;
1672     mchit.iOffset = -1;
1673     mchit.iRow = -1;
1674     mchit.iCol = -1;
1675     ret = SendMessage(hwnd, MCM_HITTEST, 0, (LPARAM)&mchit);
1676     if (ret == ~0u)
1677     {
1678         win_skip("Only MCHITTESTINFO_V1 supported\n");
1679         DestroyWindow(hwnd);
1680         return;
1681     }
1682     todo_wine expect_hex(MCHT_NOWHERE, ret);
1683     expect(-1, mchit.iOffset);
1684     expect(-1, mchit.iRow);
1685     expect(-1, mchit.iCol);
1686
1687     MoveWindow(hwnd, 0, 0, r.right, r.bottom, FALSE);
1688     mchit.pt.x = r.right / 2;
1689     mchit.pt.y = r.bottom / 2;
1690     mchit.iOffset = -1;
1691     ret = SendMessage(hwnd, MCM_HITTEST, 0, (LPARAM)&mchit);
1692     expect_hex(MCHT_CALENDARDATE, ret);
1693     expect(0, mchit.iOffset);
1694
1695     /* over day area */
1696     mchit.pt.x = r.right / (7*2);
1697     mchit.pt.y = r.bottom / 2;
1698     mchit.iOffset = -1;
1699     mchit.iCol = mchit.iRow = -1;
1700     mchit.uHit = 0;
1701     mchit.rc.left = mchit.rc.right = mchit.rc.top = mchit.rc.bottom = -1;
1702     ret = SendMessage(hwnd, MCM_HITTEST, 0, (LPARAM)&mchit);
1703     expect_hex(MCHT_CALENDARDATE, ret);
1704     expect_hex(MCHT_CALENDARDATE, mchit.uHit);
1705     expect(0, mchit.iOffset);
1706     expect(2, mchit.iRow);
1707     expect(0, mchit.iCol);
1708     /* returned a one day rectangle */
1709     expect_d(r.right / 7, mchit.rc.right - mchit.rc.left);
1710     expect_d(r.bottom / 10, mchit.rc.bottom - mchit.rc.top);
1711
1712     /* title */
1713     mchit.pt.x = 1;
1714     mchit.pt.y = 1;
1715     mchit.iOffset = -1;
1716     mchit.iCol = mchit.iRow = -1;
1717     mchit.uHit = 0;
1718     mchit.rc.left = mchit.rc.right = mchit.rc.top = mchit.rc.bottom = -1;
1719     ret = SendMessage(hwnd, MCM_HITTEST, 0, (LPARAM)&mchit);
1720     expect_hex(MCHT_TITLE, ret);
1721     expect_hex(MCHT_TITLE, mchit.uHit);
1722     expect(0, mchit.iOffset);
1723     expect(-1, mchit.iRow);
1724     expect(-1, mchit.iCol);
1725     expect(0, mchit.rc.left);
1726     expect(0, mchit.rc.top);
1727     expect_d(r.right, mchit.rc.right);
1728     ok(mchit.rc.bottom > 0, "got %d\n", mchit.rc.bottom);
1729
1730     /* between two calendars */
1731     MoveWindow(hwnd, 0, 0, r.right * 5/2, r.bottom, FALSE);
1732     mchit.pt.x = r.right / (5*4);
1733     mchit.pt.y = r.bottom / 2;
1734     mchit.iOffset = -2;
1735     mchit.iCol = mchit.iRow = -2;
1736     mchit.uHit = ~0;
1737     mchit.rc.left = mchit.rc.right = mchit.rc.top = mchit.rc.bottom = -1;
1738     ret = SendMessage(hwnd, MCM_HITTEST, 0, (LPARAM)&mchit);
1739     todo_wine expect_hex(MCHT_NOWHERE, ret);
1740     todo_wine expect_hex(MCHT_NOWHERE, mchit.uHit);
1741     expect(-2, mchit.iOffset);
1742     expect(-2, mchit.iRow);
1743     expect(-2, mchit.iCol);
1744     todo_wine expect(0, mchit.rc.left);
1745     todo_wine expect(0, mchit.rc.top);
1746     todo_wine expect_d(r.right * 5/2, mchit.rc.right);
1747     todo_wine expect_d(r.bottom, mchit.rc.bottom);
1748
1749     DestroyWindow(hwnd);
1750 }
1751
1752 static void test_get_set_border(void)
1753 {
1754     HWND hwnd;
1755     DWORD ret;
1756
1757     hwnd = create_monthcal_control(0);
1758
1759     /* a non-default value */
1760     ret = SendMessage(hwnd, MCM_SETCALENDARBORDER, TRUE, 10);
1761     expect(0, ret);
1762
1763     ret = SendMessage(hwnd, MCM_GETCALENDARBORDER, 0, 0);
1764
1765     if (ret != 10)
1766     {
1767         skip("MCM_GET/SETCALENDARBORDER not supported\n");
1768         DestroyWindow(hwnd);
1769         return;
1770     }
1771
1772     expect(10, ret);
1773
1774     DestroyWindow(hwnd);
1775 }
1776
1777 static void test_MCM_SIZERECTTOMIN(void)
1778 {
1779     HWND hwnd;
1780     DWORD ret;
1781     RECT r, r2;
1782
1783     hwnd = create_monthcal_control(0);
1784
1785     ret = SendMessageA(hwnd, MCM_GETMINREQRECT, 0, (LPARAM)&r2);
1786     if (ret == 0)
1787     {
1788         win_skip("Message MCM_GETMINREQRECT unsupported. Skipping.\n");
1789         DestroyWindow(hwnd);
1790         return;
1791     }
1792
1793     ret = SendMessageA(hwnd, MCM_SIZERECTTOMIN, 0, 0);
1794     ok(ret == 0, "got %d\n", ret);
1795
1796     r.left = r.right = r.top = r.bottom = 0;
1797     ret = SendMessageA(hwnd, MCM_SIZERECTTOMIN, 0, (LPARAM)&r);
1798     if (ret == 0)
1799     {
1800         skip("Message MCM_SIZERECTTOMIN unsupported. Skipping.\n");
1801         DestroyWindow(hwnd);
1802         return;
1803     }
1804     ok(ret == 1, "got %d\n", ret);
1805     ok(r.left == 0 && r.right > 0, "got %d, %d\n", r.left, r.right);
1806
1807     r = r2;
1808     ret = SendMessageA(hwnd, MCM_SIZERECTTOMIN, 0, (LPARAM)&r);
1809     ok(ret == 1, "got %d\n", ret);
1810
1811     r2.right = (r2.right - r2.left) * 3;
1812     r2.bottom = (r2.bottom - r2.top) * 3;
1813     r2.left = r2.top = 0;
1814     ret = SendMessageA(hwnd, MCM_SIZERECTTOMIN, 0, (LPARAM)&r2);
1815     ok(ret == 1, "got %d\n", ret);
1816
1817     DestroyWindow(hwnd);
1818 }
1819
1820 static void test_MCM_GETCALENDARCOUNT(void)
1821 {
1822     HWND hwnd;
1823     DWORD ret;
1824
1825     hwnd = create_monthcal_control(0);
1826
1827     ret = SendMessageA(hwnd, MCM_GETCALENDARCOUNT, 0, 0);
1828     if (ret == 0)
1829     {
1830         win_skip("Message MCM_GETCALENDARCOUNT unsupported. Skipping.\n");
1831         DestroyWindow(hwnd);
1832         return;
1833     }
1834
1835     expect(2, ret);
1836
1837     DestroyWindow(hwnd);
1838 }
1839
1840 static void test_daystate(void)
1841 {
1842     MONTHDAYSTATE state[4];
1843     DWORD ret, style;
1844     HWND hwnd;
1845
1846     /* without MCS_DAYSTATE */
1847     hwnd = create_monthcal_control(0);
1848
1849     ret = SendMessageA(hwnd, MCM_GETMONTHRANGE, GMR_DAYSTATE, 0);
1850     expect(4, ret);
1851
1852     ret = SendMessageA(hwnd, MCM_SETDAYSTATE, 4, (LPARAM)&state);
1853     expect(0, ret);
1854
1855     ret = SendMessageA(hwnd, MCM_SETDAYSTATE, 2, (LPARAM)&state);
1856     expect(0, ret);
1857
1858     ret = SendMessageA(hwnd, MCM_SETDAYSTATE, 0, 0);
1859     expect(0, ret);
1860
1861     /* try to switch on */
1862     SetWindowLongA(hwnd, GWL_STYLE, GetWindowLongA(hwnd, GWL_STYLE) | MCS_DAYSTATE);
1863     style = GetWindowLongA(hwnd, GWL_STYLE);
1864     ok((style & MCS_DAYSTATE) == 0, "got 0x%08x\n", style);
1865
1866     DestroyWindow(hwnd);
1867
1868     /* with MCS_DAYSTATE */
1869     hwnd = create_monthcal_control(MCS_DAYSTATE);
1870
1871     ret = SendMessageA(hwnd, MCM_GETMONTHRANGE, GMR_DAYSTATE, 0);
1872     expect(4, ret);
1873
1874     ret = SendMessageA(hwnd, MCM_SETDAYSTATE, 4, (LPARAM)&state);
1875     expect(1, ret);
1876
1877     ret = SendMessageA(hwnd, MCM_SETDAYSTATE, 2, (LPARAM)&state);
1878     expect(0, ret);
1879
1880     ret = SendMessageA(hwnd, MCM_SETDAYSTATE, 0, 0);
1881     expect(0, ret);
1882
1883     /* try to switch off */
1884     SetWindowLongA(hwnd, GWL_STYLE, GetWindowLongA(hwnd, GWL_STYLE) & ~MCS_DAYSTATE);
1885     style = GetWindowLongA(hwnd, GWL_STYLE);
1886     ok((style & MCS_DAYSTATE) == MCS_DAYSTATE, "got 0x%08x\n", style);
1887
1888     DestroyWindow(hwnd);
1889 }
1890
1891 START_TEST(monthcal)
1892 {
1893     BOOL (WINAPI *pInitCommonControlsEx)(const INITCOMMONCONTROLSEX*);
1894     INITCOMMONCONTROLSEX iccex;
1895     HMODULE hComctl32;
1896     HWND hwnd;
1897
1898     ULONG_PTR ctx_cookie;
1899     HANDLE hCtx;
1900
1901     hComctl32 = GetModuleHandleA("comctl32.dll");
1902     pInitCommonControlsEx = (void*)GetProcAddress(hComctl32, "InitCommonControlsEx");
1903     if (!pInitCommonControlsEx)
1904     {
1905         skip("InitCommonControlsEx() is missing. Skipping the tests\n");
1906         return;
1907     }
1908     iccex.dwSize = sizeof(iccex);
1909     iccex.dwICC  = ICC_DATE_CLASSES;
1910     pInitCommonControlsEx(&iccex);
1911
1912     test_monthcal();
1913
1914     init_msg_sequences(sequences, NUM_MSG_SEQUENCES);
1915
1916     parent_wnd = create_parent_window();
1917
1918     test_create();
1919     test_destroy();
1920     test_color();
1921     test_currdate();
1922     test_firstDay();
1923     test_unicode();
1924     test_today();
1925     test_scroll();
1926     test_monthrange();
1927     test_hittest();
1928     test_todaylink();
1929     test_size();
1930     test_maxselday();
1931     test_selrange();
1932     test_killfocus();
1933     test_daystate();
1934
1935     if (!load_v6_module(&ctx_cookie, &hCtx))
1936     {
1937         DestroyWindow(parent_wnd);
1938         return;
1939     }
1940
1941     /* this is a XP SP3 failure workaround */
1942     hwnd = CreateWindowExA(0, MONTHCAL_CLASSA, "foo",
1943                            WS_CHILD | WS_BORDER | WS_VISIBLE,
1944                            0, 0, 100, 100,
1945                            parent_wnd, NULL, GetModuleHandleA(NULL), NULL);
1946     if (!IsWindow(hwnd))
1947     {
1948         win_skip("FIXME: failed to create Monthcal window.\n");
1949         unload_v6_module(ctx_cookie, hCtx);
1950         DestroyWindow(parent_wnd);
1951         return;
1952     }
1953     else
1954         DestroyWindow(hwnd);
1955
1956     test_hittest_v6();
1957     test_get_set_border();
1958     test_MCM_SIZERECTTOMIN();
1959     test_MCM_GETCALENDARCOUNT();
1960
1961     unload_v6_module(ctx_cookie, hCtx);
1962
1963     DestroyWindow(parent_wnd);
1964 }