winealsa: Fix AudioRenderClient Get/ReleaseBuffer protocol.
[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     if (message == WM_NOTIFY)
470     {
471         NMHDR *hdr = (NMHDR*)lParam;
472         switch (hdr->code)
473         {
474           case MCN_GETDAYSTATE:
475           {
476             NMDAYSTATE *nmstate = (NMDAYSTATE*)lParam;
477             static MONTHDAYSTATE months[14] = { 0 };
478
479             ok(nmstate->cDayState > 0, "got %d\n", nmstate->cDayState);
480             ok(nmstate->cDayState <= 14, "got %d\n", nmstate->cDayState);
481             ok(nmstate->prgDayState != NULL, "got %p\n", nmstate->prgDayState);
482
483             nmstate->prgDayState = months;
484
485             return TRUE;
486           }
487           default:
488             break;
489         }
490     }
491
492     defwndproc_counter++;
493     ret = DefWindowProcA(hwnd, message, wParam, lParam);
494     defwndproc_counter--;
495
496     return ret;
497 }
498
499 static BOOL register_parent_wnd_class(void)
500 {
501     WNDCLASSA cls;
502
503     cls.style = 0;
504     cls.lpfnWndProc = parent_wnd_proc;
505     cls.cbClsExtra = 0;
506     cls.cbWndExtra = 0;
507     cls.hInstance = GetModuleHandleA(NULL);
508     cls.hIcon = 0;
509     cls.hCursor = LoadCursorA(0, IDC_ARROW);
510     cls.hbrBackground = GetStockObject(WHITE_BRUSH);
511     cls.lpszMenuName = NULL;
512     cls.lpszClassName = "Month-Cal test parent class";
513     return RegisterClassA(&cls);
514 }
515
516 static HWND create_parent_window(void)
517 {
518     HWND hwnd;
519
520     InitCommonControls();
521
522     /* flush message sequences, so we can check the new sequence by the end of function */
523     flush_sequences(sequences, NUM_MSG_SEQUENCES);
524
525     if (!register_parent_wnd_class())
526         return NULL;
527
528     hwnd = CreateWindowEx(0, "Month-Cal test parent class",
529                           "Month-Cal test parent window",
530                           WS_CAPTION | WS_SYSMENU | WS_MINIMIZEBOX |
531                           WS_MAXIMIZEBOX | WS_VISIBLE,
532                           0, 0, 500, 500,
533                           GetDesktopWindow(), NULL, GetModuleHandleA(NULL), NULL);
534
535     assert(hwnd);
536
537     /* check for message sequences */
538     ok_sequence(sequences, PARENT_SEQ_INDEX, create_parent_window_seq, "create parent window", FALSE);
539
540     return hwnd;
541 }
542
543 static LRESULT WINAPI monthcal_subclass_proc(HWND hwnd, UINT message, WPARAM wParam, LPARAM lParam)
544 {
545     WNDPROC oldproc = (WNDPROC)GetWindowLongPtrA(hwnd, GWLP_USERDATA);
546     static LONG defwndproc_counter = 0;
547     LRESULT ret;
548     struct message msg;
549
550     msg.message = message;
551     msg.flags = sent|wparam|lparam;
552     if (defwndproc_counter) msg.flags |= defwinproc;
553     msg.wParam = wParam;
554     msg.lParam = lParam;
555     add_message(sequences, MONTHCAL_SEQ_INDEX, &msg);
556
557     /* some debug output for style changing */
558     if ((message == WM_STYLECHANGING ||
559          message == WM_STYLECHANGED) && lParam)
560     {
561         STYLESTRUCT *style = (STYLESTRUCT*)lParam;
562         trace("\told style: 0x%08x, new style: 0x%08x\n", style->styleOld, style->styleNew);
563     }
564
565     defwndproc_counter++;
566     ret = CallWindowProcA(oldproc, hwnd, message, wParam, lParam);
567     defwndproc_counter--;
568
569     return ret;
570 }
571
572 static HWND create_monthcal_control(DWORD style)
573 {
574     WNDPROC oldproc;
575     HWND hwnd;
576
577     hwnd = CreateWindowEx(0,
578                     MONTHCAL_CLASS,
579                     "",
580                     WS_CHILD | WS_BORDER | WS_VISIBLE | style,
581                     0, 0, 300, 400,
582                     parent_wnd, NULL, GetModuleHandleA(NULL), NULL);
583
584     if (!hwnd) return NULL;
585
586     oldproc = (WNDPROC)SetWindowLongPtrA(hwnd, GWLP_WNDPROC,
587                                         (LONG_PTR)monthcal_subclass_proc);
588     SetWindowLongPtrA(hwnd, GWLP_USERDATA, (LONG_PTR)oldproc);
589
590     SendMessage(hwnd, WM_SETFONT, (WPARAM)GetStockObject(SYSTEM_FONT), 0);
591
592     return hwnd;
593 }
594
595
596 /* Setter and Getters Tests */
597
598 static void test_color(void)
599 {
600     COLORREF color, prev;
601     HWND hwnd;
602
603     hwnd = create_monthcal_control(0);
604
605     /* invalid color index */
606     color = SendMessage(hwnd, MCM_GETCOLOR, MCSC_TRAILINGTEXT + 1, 0);
607     expect(~0u, color);
608     prev = SendMessage(hwnd, MCM_SETCOLOR, MCSC_TRAILINGTEXT + 1, RGB(255,255,255));
609     expect(~0u, prev);
610
611     color = SendMessage(hwnd, MCM_GETCOLOR, MCSC_BACKGROUND, 0);
612     prev = SendMessage(hwnd, MCM_SETCOLOR, MCSC_BACKGROUND, RGB(0,0,0));
613     expect(color, prev);
614     color = SendMessage(hwnd, MCM_GETCOLOR, MCSC_BACKGROUND, 0);
615     expect(RGB(0,0,0), color);
616     prev = SendMessage(hwnd, MCM_SETCOLOR, MCSC_BACKGROUND, RGB(255,255,255));
617     expect(color, prev);
618     color = SendMessage(hwnd, MCM_GETCOLOR, MCSC_BACKGROUND, 0);
619     expect(RGB(255,255,255), color);
620
621     color = SendMessage(hwnd, MCM_GETCOLOR, MCSC_MONTHBK, 0);
622     prev = SendMessage(hwnd, MCM_SETCOLOR, MCSC_MONTHBK, RGB(0,0,0));
623     expect(color, prev);
624     color = SendMessage(hwnd, MCM_GETCOLOR, MCSC_MONTHBK, 0);
625     expect(RGB(0,0,0), color);
626     prev = SendMessage(hwnd, MCM_SETCOLOR, MCSC_MONTHBK, RGB(255,255,255));
627     expect(color, prev);
628     color = SendMessage(hwnd, MCM_GETCOLOR, MCSC_MONTHBK, 0);
629     expect(RGB(255,255,255), color);
630
631     color = SendMessage(hwnd, MCM_GETCOLOR, MCSC_TEXT, 0);
632     prev = SendMessage(hwnd, MCM_SETCOLOR, MCSC_TEXT, RGB(0,0,0));
633     expect(color, prev);
634     color = SendMessage(hwnd, MCM_GETCOLOR, MCSC_TEXT, 0);
635     expect(RGB(0,0,0), color);
636     prev = SendMessage(hwnd, MCM_SETCOLOR, MCSC_TEXT, RGB(255,255,255));
637     expect(color, prev);
638     color = SendMessage(hwnd, MCM_GETCOLOR, MCSC_TEXT, 0);
639     expect(RGB(255,255,255), color);
640
641     color = SendMessage(hwnd, MCM_GETCOLOR, MCSC_TITLEBK, 0);
642     prev = SendMessage(hwnd, MCM_SETCOLOR, MCSC_TITLEBK, RGB(0,0,0));
643     expect(color, prev);
644     color = SendMessage(hwnd, MCM_GETCOLOR, MCSC_TITLEBK, 0);
645     expect(RGB(0,0,0), color);
646     prev = SendMessage(hwnd, MCM_SETCOLOR, MCSC_TITLEBK, RGB(255,255,255));
647     expect(color, prev);
648     color = SendMessage(hwnd, MCM_GETCOLOR, MCSC_TITLEBK, 0);
649     expect(RGB(255,255,255), color);
650
651     color = SendMessage(hwnd, MCM_GETCOLOR, MCSC_TITLETEXT, 0);
652     prev = SendMessage(hwnd, MCM_SETCOLOR, MCSC_TITLETEXT, RGB(0,0,0));
653     expect(color, prev);
654     color = SendMessage(hwnd, MCM_GETCOLOR, MCSC_TITLETEXT, 0);
655     expect(RGB(0,0,0), color);
656     prev = SendMessage(hwnd, MCM_SETCOLOR, MCSC_TITLETEXT, RGB(255,255,255));
657     expect(color, prev);
658     color = SendMessage(hwnd, MCM_GETCOLOR, MCSC_TITLETEXT, 0);
659     expect(RGB(255,255,255), color);
660
661     color = SendMessage(hwnd, MCM_GETCOLOR, MCSC_TRAILINGTEXT, 0);
662     prev = SendMessage(hwnd, MCM_SETCOLOR, MCSC_TRAILINGTEXT, RGB(0,0,0));
663     expect(color, prev);
664     color = SendMessage(hwnd, MCM_GETCOLOR, MCSC_TRAILINGTEXT, 0);
665     expect(RGB(0,0,0), color);
666     prev = SendMessage(hwnd, MCM_SETCOLOR, MCSC_TRAILINGTEXT, RGB(255,255,255));
667     expect(color, prev);
668     color = SendMessage(hwnd, MCM_GETCOLOR, MCSC_TRAILINGTEXT, 0);
669     expect(RGB(255,255,255), color);
670
671     DestroyWindow(hwnd);
672 }
673
674 static void test_currdate(void)
675 {
676     SYSTEMTIME st_original, st_new, st_test;
677     int res;
678     HWND hwnd;
679
680     hwnd = create_monthcal_control(0);
681
682     flush_sequences(sequences, NUM_MSG_SEQUENCES);
683
684     /* Setter and Getters for current date selected */
685     st_original.wYear = 2000;
686     st_original.wMonth = 11;
687     st_original.wDay = 28;
688     st_original.wHour = 11;
689     st_original.wMinute = 59;
690     st_original.wSecond = 30;
691     st_original.wMilliseconds = 0;
692     st_original.wDayOfWeek = 0;
693
694     st_new = st_test = st_original;
695
696     /* Should not validate the time */
697     res = SendMessage(hwnd, MCM_SETCURSEL, 0, (LPARAM)&st_test);
698     expect(1,res);
699
700     /* Overflow matters, check for wDay */
701     st_test.wDay += 4;
702     res = SendMessage(hwnd, MCM_SETCURSEL, 0, (LPARAM)&st_test);
703     expect(0,res);
704
705     /* correct wDay before checking for wMonth */
706     st_test.wDay -= 4;
707     expect(st_original.wDay, st_test.wDay);
708
709     /* Overflow matters, check for wMonth */
710     st_test.wMonth += 4;
711     res = SendMessage(hwnd, MCM_SETCURSEL, 0, (LPARAM)&st_test);
712     expect(0,res);
713
714     /* checking if gets the information right, modify st_new */
715     st_new.wYear += 4;
716     st_new.wMonth += 4;
717     st_new.wDay += 4;
718     st_new.wHour += 4;
719     st_new.wMinute += 4;
720     st_new.wSecond += 4;
721
722     res = SendMessage(hwnd, MCM_GETCURSEL, 0, (LPARAM)&st_new);
723     expect(1, res);
724
725     /* st_new change to st_origin, above settings with overflow */
726     /* should not change the current settings */
727     expect(st_original.wYear, st_new.wYear);
728     expect(st_original.wMonth, st_new.wMonth);
729     expect(st_original.wDay, st_new.wDay);
730     ok(st_original.wHour == st_new.wHour ||
731        broken(0 == st_new.wHour), /* comctl32 <= 4.70 */
732        "Expected %d, got %d\n", st_original.wHour, st_new.wHour);
733     ok(st_original.wMinute == st_new.wMinute ||
734        broken(0 == st_new.wMinute), /* comctl32 <= 4.70 */
735        "Expected %d, got %d\n", st_original.wMinute, st_new.wMinute);
736     ok(st_original.wSecond == st_new.wSecond ||
737        broken(0 == st_new.wSecond), /* comctl32 <= 4.70 */
738        "Expected %d, got %d\n", st_original.wSecond, st_new.wSecond);
739
740     /* lparam cannot be NULL */
741     res = SendMessage(hwnd, MCM_GETCURSEL, 0, 0);
742     expect(0, res);
743
744     ok_sequence(sequences, MONTHCAL_SEQ_INDEX, monthcal_curr_date_seq, "monthcal currDate", TRUE);
745
746     /* December, 31, 9999 is the maximum allowed date */
747     memset(&st_new, 0, sizeof(st_new));
748     st_new.wYear = 9999;
749     st_new.wMonth = 12;
750     st_new.wDay = 31;
751     res = SendMessage(hwnd, MCM_SETCURSEL, 0, (LPARAM)&st_new);
752     expect(1, res);
753     memset(&st_test, 0, sizeof(st_test));
754     res = SendMessage(hwnd, MCM_GETCURSEL, 0, (LPARAM)&st_test);
755     expect(1, res);
756     expect(st_new.wYear, st_test.wYear);
757     expect(st_new.wMonth, st_test.wMonth);
758     expect(st_new.wDay, st_test.wDay);
759     expect(st_new.wHour, st_test.wHour);
760     expect(st_new.wMinute, st_test.wMinute);
761     expect(st_new.wSecond, st_test.wSecond);
762     /* try one day later */
763     st_original = st_new;
764     st_new.wYear = 10000;
765     st_new.wMonth = 1;
766     st_new.wDay = 1;
767     res = SendMessage(hwnd, MCM_SETCURSEL, 0, (LPARAM)&st_new);
768     ok(0 == res ||
769        broken(1 == res), /* comctl32 <= 4.72 */
770        "Expected 0, got %d\n", res);
771     if (0 == res)
772     {
773         memset(&st_test, 0, sizeof(st_test));
774         res = SendMessage(hwnd, MCM_GETCURSEL, 0, (LPARAM)&st_test);
775         expect(1, res);
776         expect(st_original.wYear, st_test.wYear);
777         expect(st_original.wMonth, st_test.wMonth);
778         expect(st_original.wDay, st_test.wDay);
779         expect(st_original.wHour, st_test.wHour);
780         expect(st_original.wMinute, st_test.wMinute);
781         expect(st_original.wSecond, st_test.wSecond);
782     }
783
784     /* setting selection equal to current reports success even if out range */
785     memset(&st_new, 0, sizeof(st_new));
786     st_new.wYear = 2009;
787     st_new.wDay  = 5;
788     st_new.wMonth = 10;
789     res = SendMessage(hwnd, MCM_SETCURSEL, 0, (LPARAM)&st_new);
790     expect(1, res);
791     memset(&st_test, 0, sizeof(st_test));
792     st_test.wYear = 2009;
793     st_test.wDay  = 6;
794     st_test.wMonth = 10;
795     res = SendMessage(hwnd, MCM_SETRANGE, GDTR_MIN, (LPARAM)&st_test);
796     expect(1, res);
797     /* set to current again */
798     res = SendMessage(hwnd, MCM_SETCURSEL, 0, (LPARAM)&st_new);
799     expect(1, res);
800
801     /* set with invalid day of week */
802     memset(&st_test, 0, sizeof(st_test));
803     st_test.wYear = 2009;
804     st_test.wDay  = 7;
805     st_test.wMonth = 10;
806     st_test.wDayOfWeek = 100;
807     res = SendMessage(hwnd, MCM_SETCURSEL, 0, (LPARAM)&st_test);
808     expect(1, res);
809
810     memset(&st_test, 0, sizeof(st_test));
811     res = SendMessage(hwnd, MCM_GETCURSEL, 0, (LPARAM)&st_test);
812     expect(1, res);
813     expect(2009, st_test.wYear);
814     expect(7, st_test.wDay);
815     expect(10, st_test.wMonth);
816     expect(3, st_test.wDayOfWeek);
817
818     DestroyWindow(hwnd);
819 }
820
821 static void test_firstDay(void)
822 {
823     int res, fday, i, prev;
824     CHAR b[128], caltype[3];
825     LCID lcid = LOCALE_USER_DEFAULT;
826     HWND hwnd;
827     LRESULT ret;
828
829     SetLastError(0xdeadbeef);
830     ret = GetLocaleInfoA(lcid, LOCALE_ICALENDARTYPE, caltype, 3);
831     if (ret == 0) {
832         skip("Must know local calendar type (%x)\n", GetLastError());
833         return;
834     } else if (atoi(caltype) != CAL_GREGORIAN) {
835         skip("MonthCalendar Control only supports Gregorian calendar (type: %s)\n", caltype);
836         return;
837     }
838
839     hwnd = create_monthcal_control(0);
840
841     flush_sequences(sequences, NUM_MSG_SEQUENCES);
842
843     /* Setter and Getters for first day of week */
844     /* check for locale first day */
845     if(GetLocaleInfoA(lcid, LOCALE_IFIRSTDAYOFWEEK, b, 128)){
846         fday = atoi(b);
847         trace("fday: %d\n", fday);
848         res = SendMessage(hwnd, MCM_GETFIRSTDAYOFWEEK, 0, 0);
849         expect(fday, res);
850         prev = fday;
851
852         /* checking for the values that actually will be stored as */
853         /* current first day when we set a new value */
854         for (i = -5; i < 12; i++){
855             res = SendMessage(hwnd, MCM_SETFIRSTDAYOFWEEK, 0, i);
856             expect(prev, res);
857             res = SendMessage(hwnd, MCM_GETFIRSTDAYOFWEEK, 0, 0);
858             prev = res;
859
860             if (i == -1){
861                 expect(MAKELONG(fday, FALSE), res);
862             }else if (i >= 7){
863                 /* out of range sets max first day of week, locale is ignored */
864                 expect(MAKELONG(6, TRUE), res);
865             }else{
866                 expect(MAKELONG(i, TRUE), res);
867             }
868         }
869
870         ok_sequence(sequences, MONTHCAL_SEQ_INDEX, monthcal_first_day_seq, "monthcal firstDay", FALSE);
871
872     }else{
873         skip("Cannot retrieve first day of the week\n");
874     }
875
876     DestroyWindow(hwnd);
877 }
878
879 static void test_unicode(void)
880 {
881     int res, temp;
882     HWND hwnd;
883
884     hwnd = create_monthcal_control(0);
885
886     flush_sequences(sequences, NUM_MSG_SEQUENCES);
887
888     /* Setter and Getters for Unicode format */
889
890     /* getting the current settings */
891     temp = SendMessage(hwnd, MCM_GETUNICODEFORMAT, 0, 0);
892
893     /* setting to 1, should return previous settings */
894     res = SendMessage(hwnd, MCM_SETUNICODEFORMAT, 1, 0);
895     expect(temp, res);
896
897     /* current setting is 1, so, should return 1 */
898     res = SendMessage(hwnd, MCM_GETUNICODEFORMAT, 0, 0);
899     ok(1 == res ||
900        broken(0 == res), /* comctl32 <= 4.70 */
901        "Expected 1, got %d\n", res);
902
903     /* setting to 0, should return previous settings */
904     res = SendMessage(hwnd, MCM_SETUNICODEFORMAT, 0, 0);
905     ok(1 == res ||
906        broken(0 == res), /* comctl32 <= 4.70 */
907        "Expected 1, got %d\n", res);
908
909     /* current setting is 0, so, it should return 0 */
910     res = SendMessage(hwnd, MCM_GETUNICODEFORMAT, 0, 0);
911     expect(0, res);
912
913     /* should return previous settings */
914     res = SendMessage(hwnd, MCM_SETUNICODEFORMAT, 1, 0);
915     expect(0, res);
916
917     ok_sequence(sequences, MONTHCAL_SEQ_INDEX, monthcal_unicode_seq, "monthcal unicode", FALSE);
918
919     DestroyWindow(hwnd);
920 }
921
922 static void test_hittest(void)
923 {
924     typedef struct hittest_test
925     {
926         UINT ht;
927         int  todo;
928     } hittest_test_t;
929
930     static const hittest_test_t title_hits[] = {
931         /* Start is the same everywhere */
932         { MCHT_TITLE,        0 },
933         { MCHT_TITLEBTNPREV, 0 },
934         /* The middle piece is only tested for presence of items */
935         /* End is the same everywhere */
936         { MCHT_TITLEBTNNEXT, 0 },
937         { MCHT_TITLE,        0 },
938         { MCHT_NOWHERE,      1 }
939     };
940
941     MCHITTESTINFO mchit;
942     UINT res, old_res;
943     SYSTEMTIME st;
944     LONG x;
945     UINT title_index;
946     HWND hwnd;
947     RECT r;
948     char yearmonth[80], *locale_month, *locale_year;
949     int month_count, year_count;
950     BOOL in_the_middle;
951
952     memset(&mchit, 0, sizeof(MCHITTESTINFO));
953
954     hwnd = create_monthcal_control(0);
955
956     /* test with invalid structure size */
957     mchit.cbSize = MCHITTESTINFO_V1_SIZE - 1;
958     mchit.pt.x = 0;
959     mchit.pt.y = 0;
960     res = SendMessage(hwnd, MCM_HITTEST, 0, (LPARAM)&mchit);
961     expect(0, mchit.pt.x);
962     expect(0, mchit.pt.y);
963     expect(~0u, res);
964     expect(0, mchit.uHit);
965     /* test with invalid pointer */
966     res = SendMessage(hwnd, MCM_HITTEST, 0, 0);
967     expect(~0u, res);
968
969     /* resize control to display single Calendar */
970     res = SendMessage(hwnd, MCM_GETMINREQRECT, 0, (LPARAM)&r);
971     if (res == 0)
972     {
973         win_skip("Message MCM_GETMINREQRECT unsupported. Skipping.\n");
974         DestroyWindow(hwnd);
975         return;
976     }
977     MoveWindow(hwnd, 0, 0, r.right, r.bottom, FALSE);
978
979     flush_sequences(sequences, NUM_MSG_SEQUENCES);
980
981     st.wYear = 2007;
982     st.wMonth = 4;
983     st.wDay = 11;
984     st.wHour = 1;
985     st.wMinute = 0;
986     st.wSecond = 0;
987     st.wMilliseconds = 0;
988     st.wDayOfWeek = 0;
989
990     res = SendMessage(hwnd, MCM_SETCURSEL, 0, (LPARAM)&st);
991     expect(1,res);
992
993     /* (0, 0) is the top left of the control - title */
994     mchit.cbSize = MCHITTESTINFO_V1_SIZE;
995     mchit.pt.x = 0;
996     mchit.pt.y = 0;
997     res = SendMessage(hwnd, MCM_HITTEST, 0, (LPARAM)&mchit);
998     expect(0, mchit.pt.x);
999     expect(0, mchit.pt.y);
1000     expect(mchit.uHit, res);
1001     expect_hex(MCHT_TITLE, res);
1002
1003     /* bottom right of the control and should not be active */
1004     mchit.pt.x = r.right;
1005     mchit.pt.y = r.bottom;
1006     res = SendMessage(hwnd, MCM_HITTEST, 0, (LPARAM)&mchit);
1007     expect(r.right,  mchit.pt.x);
1008     expect(r.bottom, mchit.pt.y);
1009     expect(mchit.uHit, res);
1010     todo_wine expect_hex(MCHT_NOWHERE, res);
1011
1012     /* completely out of the control, should not be active */
1013     mchit.pt.x = 2 * r.right;
1014     mchit.pt.y = 2 * r.bottom;
1015     res = SendMessage(hwnd, MCM_HITTEST, 0, (LPARAM) & mchit);
1016     expect(2 * r.right, mchit.pt.x);
1017     expect(2 * r.bottom, mchit.pt.y);
1018     expect(mchit.uHit, res);
1019     todo_wine expect_hex(MCHT_NOWHERE, res);
1020
1021     /* in active area - day of the week */
1022     mchit.pt.x = r.right / 2;
1023     mchit.pt.y = r.bottom / 2;
1024     res = SendMessage(hwnd, MCM_HITTEST, 0, (LPARAM) & mchit);
1025     expect(r.right / 2, mchit.pt.x);
1026     expect(r.bottom / 2, mchit.pt.y);
1027     expect(mchit.uHit, res);
1028     expect_hex(MCHT_CALENDARDATE, res);
1029
1030     /* in active area - day of the week #2 */
1031     mchit.pt.x = r.right / 14; /* half of first day rect */
1032     mchit.pt.y = r.bottom / 2;
1033     res = SendMessage(hwnd, MCM_HITTEST, 0, (LPARAM) & mchit);
1034     expect(r.right / 14, mchit.pt.x);
1035     expect(r.bottom / 2, mchit.pt.y);
1036     expect(mchit.uHit, res);
1037     expect_hex(MCHT_CALENDARDATE, res);
1038
1039     /* in active area - date from prev month */
1040     mchit.pt.x = r.right / 14; /* half of first day rect */
1041     mchit.pt.y = 6 * r.bottom / 19;
1042     res = SendMessage(hwnd, MCM_HITTEST, 0, (LPARAM) & mchit);
1043     expect(r.right / 14, mchit.pt.x);
1044     expect(6 * r.bottom / 19, mchit.pt.y);
1045     expect(mchit.uHit, res);
1046     expect_hex(MCHT_CALENDARDATEPREV, res);
1047
1048 #if 0
1049     /* (125, 115) is in active area - date from this month */
1050     mchit.pt.x = 125;
1051     mchit.pt.y = 115;
1052     res = SendMessage(hwnd, MCM_HITTEST, 0, (LPARAM) & mchit);
1053     expect(125, mchit.pt.x);
1054     expect(115, mchit.pt.y);
1055     expect(mchit.uHit, res);
1056     expect(MCHT_CALENDARDATE, res);
1057 #endif
1058
1059     /* in active area - date from next month */
1060     mchit.pt.x = 11 * r.right / 14;
1061     mchit.pt.y = 16 * r.bottom / 19;
1062     res = SendMessage(hwnd, MCM_HITTEST, 0, (LPARAM) & mchit);
1063     expect(11 * r.right / 14, mchit.pt.x);
1064     expect(16 * r.bottom / 19, mchit.pt.y);
1065     expect(mchit.uHit, res);
1066     expect_hex(MCHT_CALENDARDATENEXT, res);
1067
1068     /* in active area - today link */
1069     mchit.pt.x = r.right / 14;
1070     mchit.pt.y = 18 * r.bottom / 19;
1071     res = SendMessage(hwnd, MCM_HITTEST, 0, (LPARAM) & mchit);
1072     expect(r.right / 14, mchit.pt.x);
1073     expect(18 * r.bottom / 19, mchit.pt.y);
1074     expect(mchit.uHit, res);
1075     expect_hex(MCHT_TODAYLINK, res);
1076
1077     /* in active area - today link */
1078     mchit.pt.x = r.right / 2;
1079     mchit.pt.y = 18 * r.bottom / 19;
1080     res = SendMessage(hwnd, MCM_HITTEST, 0, (LPARAM) & mchit);
1081     expect(r.right / 2, mchit.pt.x);
1082     expect(18 * r.bottom / 19, mchit.pt.y);
1083     expect(mchit.uHit, res);
1084     expect_hex(MCHT_TODAYLINK, res);
1085
1086     /* in active area - today link */
1087     mchit.pt.x = r.right / 10;
1088     mchit.pt.y = 18 * r.bottom / 19;
1089     res = SendMessage(hwnd, MCM_HITTEST, 0, (LPARAM) & mchit);
1090     expect(r.right / 10, mchit.pt.x);
1091     expect(18 * r.bottom / 19, mchit.pt.y);
1092     expect(mchit.uHit, res);
1093     expect_hex(MCHT_TODAYLINK, res);
1094
1095     ok_sequence(sequences, MONTHCAL_SEQ_INDEX, monthcal_hit_test_seq, "monthcal hit test", TRUE);
1096
1097     /* The horizontal position of title bar elements depends on locale (y pos
1098        is constant), so we sample across a horizontal line and make sure we
1099        find all elements. */
1100
1101     /* Get the format of the title */
1102     GetLocaleInfo(LOCALE_USER_DEFAULT, LOCALE_SYEARMONTH, yearmonth, 80);
1103     /* Find out if we have a month and/or year */
1104     locale_year = strstr(yearmonth, "y");
1105     locale_month = strstr(yearmonth, "M");
1106
1107     mchit.pt.x = 0;
1108     mchit.pt.y = (5/2) * r.bottom / 19;
1109     title_index = 0;
1110     old_res = SendMessage(hwnd, MCM_HITTEST, 0, (LPARAM) & mchit);
1111     expect_hex(title_hits[title_index].ht, old_res);
1112
1113     in_the_middle = FALSE;
1114     month_count = year_count = 0;
1115     for (x = 0; x < r.right; x++){
1116         mchit.pt.x = x;
1117         res = SendMessage(hwnd, MCM_HITTEST, 0, (LPARAM) & mchit);
1118         expect(x, mchit.pt.x);
1119         expect((5/2) * r.bottom / 19, mchit.pt.y);
1120         expect(mchit.uHit, res);
1121         if (res != old_res) {
1122
1123             if (old_res == MCHT_TITLEBTNPREV)
1124                 in_the_middle = TRUE;
1125
1126             if (res == MCHT_TITLEBTNNEXT)
1127                 in_the_middle = FALSE;
1128
1129             if (in_the_middle) {
1130                 if (res == MCHT_TITLEMONTH)
1131                     month_count++;
1132                 else if (res == MCHT_TITLEYEAR)
1133                     year_count++;
1134             } else {
1135                 title_index++;
1136
1137                 if (sizeof(title_hits) / sizeof(title_hits[0]) <= title_index)
1138                     break;
1139
1140                 if (title_hits[title_index].todo) {
1141                     todo_wine
1142                     ok(title_hits[title_index].ht == res, "Expected %x, got %x, pos %d\n",
1143                                                           title_hits[title_index].ht, res, x);
1144                 } else {
1145                     ok(title_hits[title_index].ht == res, "Expected %x, got %x, pos %d\n",
1146                                                           title_hits[title_index].ht, res, x);
1147                 }
1148             }
1149             old_res = res;
1150         }
1151     }
1152
1153     /* There are some limits, even if LOCALE_SYEARMONTH contains rubbish
1154      * or no month/year indicators at all */
1155     if (locale_month)
1156         todo_wine ok(month_count == 1, "Expected 1 month item, got %d\n", month_count);
1157     else
1158         ok(month_count <= 1, "Too many month items: %d\n", month_count);
1159
1160     if (locale_year)
1161         todo_wine ok(year_count == 1, "Expected 1 year item, got %d\n", year_count);
1162     else
1163         ok(year_count <= 1, "Too many year items: %d\n", year_count);
1164
1165     todo_wine ok(month_count + year_count >= 1, "Not enough month and year items\n");
1166
1167     ok(r.right <= x && title_index + 1 == sizeof(title_hits) / sizeof(title_hits[0]),
1168        "Wrong title layout\n");
1169
1170     DestroyWindow(hwnd);
1171 }
1172
1173 static void test_todaylink(void)
1174 {
1175     MCHITTESTINFO mchit;
1176     SYSTEMTIME st_test, st_new;
1177     UINT res;
1178     HWND hwnd;
1179     RECT r;
1180
1181     memset(&mchit, 0, sizeof(MCHITTESTINFO));
1182
1183     hwnd = create_monthcal_control(0);
1184
1185     res = SendMessage(hwnd, MCM_GETMINREQRECT, 0, (LPARAM)&r);
1186     expect(1, res);
1187     MoveWindow(hwnd, 0, 0, r.right, r.bottom, FALSE);
1188
1189     flush_sequences(sequences, NUM_MSG_SEQUENCES);
1190
1191     /* hit active area - today link */
1192     mchit.cbSize = MCHITTESTINFO_V1_SIZE;
1193     mchit.pt.x = r.right / 14;
1194     mchit.pt.y = 18 * r.bottom / 19;
1195     res = SendMessage(hwnd, MCM_HITTEST, 0, (LPARAM) & mchit);
1196     expect(r.right / 14, mchit.pt.x);
1197     expect(18 * r.bottom / 19, mchit.pt.y);
1198     expect(mchit.uHit, res);
1199     expect(MCHT_TODAYLINK, res);
1200
1201     st_test.wDay = 1;
1202     st_test.wMonth = 1;
1203     st_test.wYear = 2005;
1204
1205     res = SendMessage(hwnd, MCM_SETTODAY, 0, (LPARAM)&st_test);
1206     expect(0, res);
1207
1208     memset(&st_new, 0, sizeof(st_new));
1209     res = SendMessage(hwnd, MCM_GETTODAY, 0, (LPARAM)&st_new);
1210     expect(1, res);
1211     expect(1, st_new.wDay);
1212     expect(1, st_new.wMonth);
1213     expect(2005, st_new.wYear);
1214
1215     res = SendMessage(hwnd, WM_LBUTTONDOWN, MK_LBUTTON, MAKELONG(mchit.pt.x, mchit.pt.y));
1216     expect(0, res);
1217
1218     memset(&st_new, 0, sizeof(st_new));
1219     res = SendMessage(hwnd, MCM_GETCURSEL, 0, (LPARAM)&st_new);
1220     expect(1, res);
1221     expect(1, st_new.wDay);
1222     expect(1, st_new.wMonth);
1223     expect(2005, st_new.wYear);
1224
1225     ok_sequence(sequences, MONTHCAL_SEQ_INDEX, monthcal_todaylink_seq, "monthcal hit test", TRUE);
1226
1227     DestroyWindow(hwnd);
1228 }
1229
1230 static void test_today(void)
1231 {
1232     SYSTEMTIME st_test, st_new;
1233     int res;
1234     HWND hwnd;
1235
1236     hwnd = create_monthcal_control(0);
1237
1238     flush_sequences(sequences, NUM_MSG_SEQUENCES);
1239
1240     /* Setter and Getters for "today" information */
1241
1242     /* check for overflow, should be ok */
1243     memset(&st_test, 0, sizeof(st_test));
1244     st_test.wDay = 38;
1245     st_test.wMonth = 38;
1246
1247     st_new.wDay = 27;
1248     st_new.wMonth = 27;
1249
1250     res = SendMessage(hwnd, MCM_SETTODAY, 0, (LPARAM)&st_test);
1251     expect(0, res);
1252
1253     res = SendMessage(hwnd, MCM_GETTODAY, 0, (LPARAM)&st_new);
1254     expect(1, res);
1255
1256     /* st_test should not change */
1257     expect(38, st_test.wDay);
1258     expect(38, st_test.wMonth);
1259
1260     /* st_new should change, overflow does not matter */
1261     expect(38, st_new.wDay);
1262     expect(38, st_new.wMonth);
1263
1264     /* check for zero, should be ok*/
1265     st_test.wDay = 0;
1266     st_test.wMonth = 0;
1267
1268     res = SendMessage(hwnd, MCM_SETTODAY, 0, (LPARAM)&st_test);
1269     expect(0, res);
1270
1271     res = SendMessage(hwnd, MCM_GETTODAY, 0, (LPARAM)&st_new);
1272     expect(1, res);
1273
1274     /* st_test should not change */
1275     expect(0, st_test.wDay);
1276     expect(0, st_test.wMonth);
1277
1278     /* st_new should change to zero*/
1279     expect(0, st_new.wDay);
1280     expect(0, st_new.wMonth);
1281
1282     ok_sequence(sequences, MONTHCAL_SEQ_INDEX, monthcal_today_seq, "monthcal today", TRUE);
1283
1284     DestroyWindow(hwnd);
1285 }
1286
1287 static void test_scroll(void)
1288 {
1289     int res;
1290     HWND hwnd;
1291
1292     hwnd = create_monthcal_control(0);
1293
1294     flush_sequences(sequences, NUM_MSG_SEQUENCES);
1295
1296     /* Setter and Getters for scroll rate */
1297     res = SendMessage(hwnd, MCM_SETMONTHDELTA, 2, 0);
1298     expect(0, res);
1299
1300     res = SendMessage(hwnd, MCM_SETMONTHDELTA, 3, 0);
1301     expect(2, res);
1302     res = SendMessage(hwnd, MCM_GETMONTHDELTA, 0, 0);
1303     expect(3, res);
1304
1305     res = SendMessage(hwnd, MCM_SETMONTHDELTA, 12, 0);
1306     expect(3, res);
1307     res = SendMessage(hwnd, MCM_GETMONTHDELTA, 0, 0);
1308     expect(12, res);
1309
1310     res = SendMessage(hwnd, MCM_SETMONTHDELTA, 15, 0);
1311     expect(12, res);
1312     res = SendMessage(hwnd, MCM_GETMONTHDELTA, 0, 0);
1313     expect(15, res);
1314
1315     res = SendMessage(hwnd, MCM_SETMONTHDELTA, -5, 0);
1316     expect(15, res);
1317     res = SendMessage(hwnd, MCM_GETMONTHDELTA, 0, 0);
1318     expect(-5, res);
1319
1320     ok_sequence(sequences, MONTHCAL_SEQ_INDEX, monthcal_scroll_seq, "monthcal scroll", FALSE);
1321
1322     DestroyWindow(hwnd);
1323 }
1324
1325 static void test_monthrange(void)
1326 {
1327     int res;
1328     SYSTEMTIME st_visible[2], st_daystate[2], st;
1329     HWND hwnd;
1330     RECT r;
1331
1332     hwnd = create_monthcal_control(0);
1333
1334     st_visible[0].wYear = 0;
1335     st_visible[0].wMonth = 0;
1336     st_visible[0].wDay = 0;
1337     st_daystate[1] = st_daystate[0] = st_visible[1] = st_visible[0];
1338
1339     st.wYear = 2000;
1340     st.wMonth = 11;
1341     st.wDay = 28;
1342     st.wHour = 11;
1343     st.wMinute = 59;
1344     st.wSecond = 30;
1345     st.wMilliseconds = 0;
1346     st.wDayOfWeek = 0;
1347
1348     res = SendMessage(hwnd, MCM_SETCURSEL, 0, (LPARAM)&st);
1349     expect(1,res);
1350
1351     /* to be locale independent */
1352     SendMessage(hwnd, MCM_SETFIRSTDAYOFWEEK, 0, (LPARAM)6);
1353
1354     res = SendMessage(hwnd, MCM_GETMINREQRECT, 0, (LPARAM)&r);
1355     expect(TRUE, res);
1356     /* resize control to display two Calendars */
1357     MoveWindow(hwnd, 0, 0, r.right, (5/2)*r.bottom, FALSE);
1358
1359     flush_sequences(sequences, NUM_MSG_SEQUENCES);
1360
1361     res = SendMessage(hwnd, MCM_GETMONTHRANGE, GMR_VISIBLE, (LPARAM)st_visible);
1362     expect(2, res);
1363     expect(2000, st_visible[0].wYear);
1364     expect(11, st_visible[0].wMonth);
1365     expect(1, st_visible[0].wDay);
1366     expect(2000, st_visible[1].wYear);
1367     expect(12, st_visible[1].wMonth);
1368     expect(31, st_visible[1].wDay);
1369
1370     res = SendMessage(hwnd, MCM_GETMONTHRANGE, GMR_DAYSTATE, (LPARAM)st_daystate);
1371     expect(4, res);
1372     expect(2000, st_daystate[0].wYear);
1373     expect(10, st_daystate[0].wMonth);
1374     expect(29, st_daystate[0].wDay);
1375     expect(2001, st_daystate[1].wYear);
1376     expect(1, st_daystate[1].wMonth);
1377     expect(6, st_daystate[1].wDay);
1378
1379     ok_sequence(sequences, MONTHCAL_SEQ_INDEX, monthcal_monthrange_seq, "monthcal monthrange", FALSE);
1380
1381     /* with null date array parameter */
1382     res = SendMessage(hwnd, MCM_GETMONTHRANGE, GMR_VISIBLE, 0);
1383     expect(2, res);
1384
1385     res = SendMessage(hwnd, MCM_GETMONTHRANGE, GMR_DAYSTATE, 0);
1386     expect(4, res);
1387
1388     /* resize control to display single Calendar */
1389     MoveWindow(hwnd, 0, 0, r.right, r.bottom, FALSE);
1390
1391     memset(&st, 0, sizeof(st));
1392     st.wMonth = 9;
1393     st.wYear  = 1752;
1394     st.wDay   = 14;
1395
1396     res = SendMessage(hwnd, MCM_SETCURSEL, 0, (LPARAM)&st);
1397     expect(1, res);
1398
1399     /* September 1752 has 19 days */
1400     res = SendMessage(hwnd, MCM_GETMONTHRANGE, GMR_VISIBLE, (LPARAM)st_visible);
1401     expect(1, res);
1402
1403     expect(1752, st_visible[0].wYear);
1404     expect(9, st_visible[0].wMonth);
1405     ok(14 == st_visible[0].wDay ||
1406        broken(1 == st_visible[0].wDay), /* comctl32 <= 4.72 */
1407        "Expected 14, got %d\n", st_visible[0].wDay);
1408
1409     expect(1752, st_visible[1].wYear);
1410     expect(9, st_visible[1].wMonth);
1411     expect(19, st_visible[1].wDay);
1412
1413     DestroyWindow(hwnd);
1414 }
1415
1416 static void test_maxselday(void)
1417 {
1418     int res;
1419     HWND hwnd;
1420     DWORD style;
1421
1422     hwnd = create_monthcal_control(0);
1423     /* if no style specified default to 1 */
1424     res = SendMessage(hwnd, MCM_GETMAXSELCOUNT, 0, 0);
1425     expect(1, res);
1426     res = SendMessage(hwnd, MCM_SETMAXSELCOUNT, 5, 0);
1427     expect(0, res);
1428     res = SendMessage(hwnd, MCM_GETMAXSELCOUNT, 0, 0);
1429     expect(1, res);
1430
1431     /* try to set style */
1432     style = GetWindowLong(hwnd, GWL_STYLE);
1433     SetWindowLong(hwnd, GWL_STYLE, style | MCS_MULTISELECT);
1434     style = GetWindowLong(hwnd, GWL_STYLE);
1435     ok(!(style & MCS_MULTISELECT), "Expected MCS_MULTISELECT not to be set\n");
1436     DestroyWindow(hwnd);
1437
1438     hwnd = create_monthcal_control(MCS_MULTISELECT);
1439     /* try to remove style */
1440     style = GetWindowLong(hwnd, GWL_STYLE);
1441     SetWindowLong(hwnd, GWL_STYLE, style & ~MCS_MULTISELECT);
1442     style = GetWindowLong(hwnd, GWL_STYLE);
1443     ok(style & MCS_MULTISELECT, "Expected MCS_MULTISELECT to be set\n");
1444     DestroyWindow(hwnd);
1445
1446     hwnd = create_monthcal_control(MCS_MULTISELECT);
1447
1448     /* default width is a week */
1449     res = SendMessage(hwnd, MCM_GETMAXSELCOUNT, 0, 0);
1450     expect(7, res);
1451
1452     flush_sequences(sequences, NUM_MSG_SEQUENCES);
1453
1454     /* Setter and Getters for max selected days */
1455     res = SendMessage(hwnd, MCM_SETMAXSELCOUNT, 5, 0);
1456     expect(1, res);
1457     res = SendMessage(hwnd, MCM_GETMAXSELCOUNT, 0, 0);
1458     expect(5, res);
1459
1460     res = SendMessage(hwnd, MCM_SETMAXSELCOUNT, 15, 0);
1461     expect(1, res);
1462     res = SendMessage(hwnd, MCM_GETMAXSELCOUNT, 0, 0);
1463     expect(15, res);
1464
1465     /* test invalid value */
1466     res = SendMessage(hwnd, MCM_SETMAXSELCOUNT, -1, 0);
1467     expect(0, res);
1468     res = SendMessage(hwnd, MCM_GETMAXSELCOUNT, 0, 0);
1469     expect(15, res);
1470
1471     ok_sequence(sequences, MONTHCAL_SEQ_INDEX, monthcal_max_sel_day_seq, "monthcal MaxSelDay", FALSE);
1472
1473     /* zero value is invalid too */
1474     res = SendMessage(hwnd, MCM_SETMAXSELCOUNT, 0, 0);
1475     expect(0, res);
1476     res = SendMessage(hwnd, MCM_GETMAXSELCOUNT, 0, 0);
1477     expect(15, res);
1478
1479     DestroyWindow(hwnd);
1480 }
1481
1482 static void test_size(void)
1483 {
1484     int res;
1485     RECT r1, r2;
1486     HFONT hFont1, hFont2;
1487     LOGFONTA logfont;
1488     HWND hwnd;
1489
1490     hwnd = create_monthcal_control(0);
1491
1492     lstrcpyA(logfont.lfFaceName, "Arial");
1493     memset(&logfont, 0, sizeof(logfont));
1494     logfont.lfHeight = 12;
1495     hFont1 = CreateFontIndirectA(&logfont);
1496
1497     logfont.lfHeight = 24;
1498     hFont2 = CreateFontIndirectA(&logfont);
1499
1500     /* initialize to a font we can compare against */
1501     SendMessage(hwnd, WM_SETFONT, (WPARAM)hFont1, 0);
1502     res = SendMessage(hwnd, MCM_GETMINREQRECT, 0, (LPARAM)&r1);
1503     ok(res, "SendMessage(MCM_GETMINREQRECT) failed\n");
1504
1505     /* check that setting a larger font results in an larger rect */
1506     SendMessage(hwnd, WM_SETFONT, (WPARAM)hFont2, 0);
1507     res = SendMessage(hwnd, MCM_GETMINREQRECT, 0, (LPARAM)&r2);
1508     ok(res, "SendMessage(MCM_GETMINREQRECT) failed\n");
1509
1510     OffsetRect(&r1, -r1.left, -r1.top);
1511     OffsetRect(&r2, -r2.left, -r2.top);
1512
1513     ok(r1.bottom < r2.bottom, "Failed to get larger rect with larger font\n");
1514
1515     DestroyWindow(hwnd);
1516 }
1517
1518 static void test_create(void)
1519 {
1520     HWND hwnd;
1521
1522     flush_sequences(sequences, NUM_MSG_SEQUENCES);
1523
1524     hwnd = create_monthcal_control(0);
1525     ok_sequence(sequences, PARENT_SEQ_INDEX, create_monthcal_control_seq, "create monthcal control", TRUE);
1526
1527     DestroyWindow(hwnd);
1528
1529     flush_sequences(sequences, NUM_MSG_SEQUENCES);
1530     hwnd = create_monthcal_control(MCS_MULTISELECT);
1531     ok_sequence(sequences, PARENT_SEQ_INDEX, create_monthcal_multi_sel_style_seq, "create monthcal (multi sel style)", TRUE);
1532     DestroyWindow(hwnd);
1533 }
1534
1535 static void test_destroy(void)
1536 {
1537     HWND hwnd;
1538
1539     hwnd = create_monthcal_control(0);
1540     flush_sequences(sequences, NUM_MSG_SEQUENCES);
1541     DestroyWindow(hwnd);
1542     ok_sequence(sequences, PARENT_SEQ_INDEX, destroy_monthcal_parent_msgs_seq, "Destroy monthcal (parent msg)", FALSE);
1543     ok_sequence(sequences, MONTHCAL_SEQ_INDEX, destroy_monthcal_child_msgs_seq, "Destroy monthcal (child msg)", FALSE);
1544
1545     /* MCS_MULTISELECT */
1546     hwnd = create_monthcal_control(MCS_MULTISELECT);
1547     flush_sequences(sequences, NUM_MSG_SEQUENCES);
1548     DestroyWindow(hwnd);
1549     ok_sequence(sequences, MONTHCAL_SEQ_INDEX, destroy_monthcal_multi_sel_style_seq, "Destroy monthcal (multi sel style)", FALSE);
1550 }
1551
1552 static void test_selrange(void)
1553 {
1554     HWND hwnd;
1555     SYSTEMTIME st, range[2], range2[2];
1556     BOOL ret, old_comctl32 = FALSE;
1557
1558     hwnd = create_monthcal_control(MCS_MULTISELECT);
1559
1560     /* just after creation selection should start and end today */
1561     ret = SendMessage(hwnd, MCM_GETTODAY, 0, (LPARAM)&st);
1562     expect(TRUE, ret);
1563
1564     memset(range, 0xcc, sizeof(range));
1565     ret = SendMessage(hwnd, MCM_GETSELRANGE, 0, (LPARAM)range);
1566     expect(TRUE, ret);
1567     expect(st.wYear,      range[0].wYear);
1568     expect(st.wMonth,     range[0].wMonth);
1569     expect(st.wDay,       range[0].wDay);
1570     if (range[0].wDayOfWeek != st.wDayOfWeek)
1571     {
1572         win_skip("comctl32 <= 4.70 doesn't set some values\n");
1573         old_comctl32 = TRUE;
1574     }
1575     else
1576     {
1577         expect(st.wDayOfWeek, range[0].wDayOfWeek);
1578         expect(st.wHour,      range[0].wHour);
1579         expect(st.wMinute,    range[0].wMinute);
1580         expect(st.wSecond,    range[0].wSecond);
1581         expect(st.wMilliseconds, range[0].wMilliseconds);
1582     }
1583
1584     expect(st.wYear,      range[1].wYear);
1585     expect(st.wMonth,     range[1].wMonth);
1586     expect(st.wDay,       range[1].wDay);
1587     if (!old_comctl32)
1588     {
1589         expect(st.wDayOfWeek, range[1].wDayOfWeek);
1590         expect(st.wHour,      range[1].wHour);
1591         expect(st.wMinute,    range[1].wMinute);
1592         expect(st.wSecond,    range[1].wSecond);
1593         expect(st.wMilliseconds, range[1].wMilliseconds);
1594     }
1595
1596     /* bounds are swapped if min > max */
1597     memset(&range[0], 0, sizeof(range[0]));
1598     range[0].wYear  = 2009;
1599     range[0].wMonth = 10;
1600     range[0].wDay   = 5;
1601     range[1] = range[0];
1602     range[1].wDay   = 3;
1603
1604     ret = SendMessage(hwnd, MCM_SETSELRANGE, 0, (LPARAM)range);
1605     expect(TRUE, ret);
1606
1607     ret = SendMessage(hwnd, MCM_GETSELRANGE, 0, (LPARAM)range2);
1608     expect(TRUE, ret);
1609
1610     expect(range[1].wYear,      range2[0].wYear);
1611     expect(range[1].wMonth,     range2[0].wMonth);
1612     expect(range[1].wDay,       range2[0].wDay);
1613     expect(6, range2[0].wDayOfWeek);
1614     expect(range[1].wHour,      range2[0].wHour);
1615     expect(range[1].wMinute,    range2[0].wMinute);
1616     expect(range[1].wSecond,    range2[0].wSecond);
1617     expect(range[1].wMilliseconds, range2[0].wMilliseconds);
1618
1619     expect(range[0].wYear,      range2[1].wYear);
1620     expect(range[0].wMonth,     range2[1].wMonth);
1621     expect(range[0].wDay,       range2[1].wDay);
1622     expect(1, range2[1].wDayOfWeek);
1623     expect(range[0].wHour,      range2[1].wHour);
1624     expect(range[0].wMinute,    range2[1].wMinute);
1625     expect(range[0].wSecond,    range2[1].wSecond);
1626     expect(range[0].wMilliseconds, range2[1].wMilliseconds);
1627
1628     /* try with range larger than maximum configured */
1629     memset(&range[0], 0, sizeof(range[0]));
1630     range[0].wYear  = 2009;
1631     range[0].wMonth = 10;
1632     range[0].wDay   = 1;
1633     range[1] = range[0];
1634
1635     ret = SendMessage(hwnd, MCM_SETSELRANGE, 0, (LPARAM)range);
1636     expect(TRUE, ret);
1637
1638     range[1] = range[0];
1639     /* default max. range is 7 days */
1640     range[1].wDay = 8;
1641
1642     ret = SendMessage(hwnd, MCM_SETSELRANGE, 0, (LPARAM)range);
1643     expect(FALSE, ret);
1644
1645     ret = SendMessage(hwnd, MCM_GETSELRANGE, 0, (LPARAM)range2);
1646     expect(TRUE, ret);
1647
1648     expect(range[0].wYear,  range2[0].wYear);
1649     expect(range[0].wMonth, range2[0].wMonth);
1650     expect(range[0].wDay,   range2[0].wDay);
1651     expect(range[0].wYear,  range2[1].wYear);
1652     expect(range[0].wMonth, range2[1].wMonth);
1653     expect(range[0].wDay,   range2[1].wDay);
1654
1655     DestroyWindow(hwnd);
1656 }
1657
1658 static void test_killfocus(void)
1659 {
1660     HWND hwnd;
1661     DWORD style;
1662
1663     hwnd = create_monthcal_control(0);
1664
1665     /* make parent invisible */
1666     style = GetWindowLong(parent_wnd, GWL_STYLE);
1667     SetWindowLong(parent_wnd, GWL_STYLE, style &~ WS_VISIBLE);
1668
1669     SendMessage(hwnd, WM_KILLFOCUS, (WPARAM)GetDesktopWindow(), 0);
1670
1671     style = GetWindowLong(hwnd, GWL_STYLE);
1672     ok(style & WS_VISIBLE, "Expected WS_VISIBLE to be set\n");
1673
1674     style = GetWindowLong(parent_wnd, GWL_STYLE);
1675     SetWindowLong(parent_wnd, GWL_STYLE, style | WS_VISIBLE);
1676
1677     DestroyWindow(hwnd);
1678 }
1679
1680 static void test_hittest_v6(void)
1681 {
1682     MCHITTESTINFO mchit;
1683     DWORD ret;
1684     HWND hwnd;
1685     RECT r;
1686
1687     hwnd = create_monthcal_control(0);
1688     SendMessage(hwnd, MCM_SETCALENDARBORDER, TRUE, 0);
1689
1690     SendMessage(hwnd, MCM_GETMINREQRECT, 0, (LPARAM)&r);
1691     /* reserving some area around calendar */
1692     MoveWindow(hwnd, 0, 0, r.right * 3 / 2, r.bottom * 3 / 2, FALSE);
1693     mchit.cbSize = sizeof(MCHITTESTINFO);
1694     mchit.pt.x = mchit.pt.y = 0;
1695     mchit.iOffset = -1;
1696     mchit.iRow = -1;
1697     mchit.iCol = -1;
1698     ret = SendMessage(hwnd, MCM_HITTEST, 0, (LPARAM)&mchit);
1699     if (ret == ~0u)
1700     {
1701         win_skip("Only MCHITTESTINFO_V1 supported\n");
1702         DestroyWindow(hwnd);
1703         return;
1704     }
1705     todo_wine expect_hex(MCHT_NOWHERE, ret);
1706     expect(-1, mchit.iOffset);
1707     expect(-1, mchit.iRow);
1708     expect(-1, mchit.iCol);
1709
1710     MoveWindow(hwnd, 0, 0, r.right, r.bottom, FALSE);
1711     mchit.pt.x = r.right / 2;
1712     mchit.pt.y = r.bottom / 2;
1713     mchit.iOffset = -1;
1714     ret = SendMessage(hwnd, MCM_HITTEST, 0, (LPARAM)&mchit);
1715     expect_hex(MCHT_CALENDARDATE, ret);
1716     expect(0, mchit.iOffset);
1717
1718     /* over day area */
1719     mchit.pt.x = r.right / (7*2);
1720     mchit.pt.y = r.bottom / 2;
1721     mchit.iOffset = -1;
1722     mchit.iCol = mchit.iRow = -1;
1723     mchit.uHit = 0;
1724     mchit.rc.left = mchit.rc.right = mchit.rc.top = mchit.rc.bottom = -1;
1725     ret = SendMessage(hwnd, MCM_HITTEST, 0, (LPARAM)&mchit);
1726     expect_hex(MCHT_CALENDARDATE, ret);
1727     expect_hex(MCHT_CALENDARDATE, mchit.uHit);
1728     expect(0, mchit.iOffset);
1729     expect(2, mchit.iRow);
1730     expect(0, mchit.iCol);
1731     /* returned a one day rectangle */
1732     expect_d(r.right / 7, mchit.rc.right - mchit.rc.left);
1733     expect_d(r.bottom / 10, mchit.rc.bottom - mchit.rc.top);
1734
1735     /* title */
1736     mchit.pt.x = 1;
1737     mchit.pt.y = 1;
1738     mchit.iOffset = -1;
1739     mchit.iCol = mchit.iRow = -1;
1740     mchit.uHit = 0;
1741     mchit.rc.left = mchit.rc.right = mchit.rc.top = mchit.rc.bottom = -1;
1742     ret = SendMessage(hwnd, MCM_HITTEST, 0, (LPARAM)&mchit);
1743     expect_hex(MCHT_TITLE, ret);
1744     expect_hex(MCHT_TITLE, mchit.uHit);
1745     expect(0, mchit.iOffset);
1746     expect(-1, mchit.iRow);
1747     expect(-1, mchit.iCol);
1748     expect(0, mchit.rc.left);
1749     expect(0, mchit.rc.top);
1750     expect_d(r.right, mchit.rc.right);
1751     ok(mchit.rc.bottom > 0, "got %d\n", mchit.rc.bottom);
1752
1753     /* between two calendars */
1754     MoveWindow(hwnd, 0, 0, r.right * 5/2, r.bottom, FALSE);
1755     mchit.pt.x = r.right / (5*4);
1756     mchit.pt.y = r.bottom / 2;
1757     mchit.iOffset = -2;
1758     mchit.iCol = mchit.iRow = -2;
1759     mchit.uHit = ~0;
1760     mchit.rc.left = mchit.rc.right = mchit.rc.top = mchit.rc.bottom = -1;
1761     ret = SendMessage(hwnd, MCM_HITTEST, 0, (LPARAM)&mchit);
1762     todo_wine expect_hex(MCHT_NOWHERE, ret);
1763     todo_wine expect_hex(MCHT_NOWHERE, mchit.uHit);
1764     expect(-2, mchit.iOffset);
1765     expect(-2, mchit.iRow);
1766     expect(-2, mchit.iCol);
1767     todo_wine expect(0, mchit.rc.left);
1768     todo_wine expect(0, mchit.rc.top);
1769     todo_wine expect_d(r.right * 5/2, mchit.rc.right);
1770     todo_wine expect_d(r.bottom, mchit.rc.bottom);
1771
1772     DestroyWindow(hwnd);
1773 }
1774
1775 static void test_get_set_border(void)
1776 {
1777     HWND hwnd;
1778     DWORD ret;
1779
1780     hwnd = create_monthcal_control(0);
1781
1782     /* a non-default value */
1783     ret = SendMessage(hwnd, MCM_SETCALENDARBORDER, TRUE, 10);
1784     expect(0, ret);
1785
1786     ret = SendMessage(hwnd, MCM_GETCALENDARBORDER, 0, 0);
1787
1788     if (ret != 10)
1789     {
1790         skip("MCM_GET/SETCALENDARBORDER not supported\n");
1791         DestroyWindow(hwnd);
1792         return;
1793     }
1794
1795     expect(10, ret);
1796
1797     DestroyWindow(hwnd);
1798 }
1799
1800 static void test_MCM_SIZERECTTOMIN(void)
1801 {
1802     HWND hwnd;
1803     DWORD ret;
1804     RECT r, r2;
1805
1806     hwnd = create_monthcal_control(0);
1807
1808     ret = SendMessageA(hwnd, MCM_GETMINREQRECT, 0, (LPARAM)&r2);
1809     if (ret == 0)
1810     {
1811         win_skip("Message MCM_GETMINREQRECT unsupported. Skipping.\n");
1812         DestroyWindow(hwnd);
1813         return;
1814     }
1815
1816     ret = SendMessageA(hwnd, MCM_SIZERECTTOMIN, 0, 0);
1817     ok(ret == 0, "got %d\n", ret);
1818
1819     r.left = r.right = r.top = r.bottom = 0;
1820     ret = SendMessageA(hwnd, MCM_SIZERECTTOMIN, 0, (LPARAM)&r);
1821     if (ret == 0)
1822     {
1823         skip("Message MCM_SIZERECTTOMIN unsupported. Skipping.\n");
1824         DestroyWindow(hwnd);
1825         return;
1826     }
1827     ok(ret == 1, "got %d\n", ret);
1828     ok(r.left == 0 && r.right > 0, "got %d, %d\n", r.left, r.right);
1829
1830     r = r2;
1831     ret = SendMessageA(hwnd, MCM_SIZERECTTOMIN, 0, (LPARAM)&r);
1832     ok(ret == 1, "got %d\n", ret);
1833
1834     r2.right = (r2.right - r2.left) * 3;
1835     r2.bottom = (r2.bottom - r2.top) * 3;
1836     r2.left = r2.top = 0;
1837     ret = SendMessageA(hwnd, MCM_SIZERECTTOMIN, 0, (LPARAM)&r2);
1838     ok(ret == 1, "got %d\n", ret);
1839
1840     DestroyWindow(hwnd);
1841 }
1842
1843 static void test_MCM_GETCALENDARCOUNT(void)
1844 {
1845     HWND hwnd;
1846     DWORD ret;
1847
1848     hwnd = create_monthcal_control(0);
1849
1850     ret = SendMessageA(hwnd, MCM_GETCALENDARCOUNT, 0, 0);
1851     if (ret == 0)
1852     {
1853         win_skip("Message MCM_GETCALENDARCOUNT unsupported. Skipping.\n");
1854         DestroyWindow(hwnd);
1855         return;
1856     }
1857
1858     expect(2, ret);
1859
1860     DestroyWindow(hwnd);
1861 }
1862
1863 static void test_daystate(void)
1864 {
1865     MONTHDAYSTATE state[4];
1866     DWORD ret, style;
1867     HWND hwnd;
1868     RECT r;
1869
1870     /* without MCS_DAYSTATE */
1871     hwnd = create_monthcal_control(0);
1872
1873     ret = SendMessage(hwnd, MCM_GETMINREQRECT, 0, (LPARAM)&r);
1874     expect(TRUE, ret);
1875
1876     /* resize control to display two Calendars */
1877     MoveWindow(hwnd, 0, 0, r.right, (5/2)*r.bottom, FALSE);
1878
1879     ret = SendMessageA(hwnd, MCM_GETMONTHRANGE, GMR_DAYSTATE, 0);
1880     expect(4, ret);
1881
1882     ret = SendMessageA(hwnd, MCM_SETDAYSTATE, 4, (LPARAM)&state);
1883     expect(0, ret);
1884
1885     ret = SendMessageA(hwnd, MCM_SETDAYSTATE, 2, (LPARAM)&state);
1886     expect(0, ret);
1887
1888     ret = SendMessageA(hwnd, MCM_SETDAYSTATE, 0, 0);
1889     expect(0, ret);
1890
1891     /* try to switch on */
1892     SetWindowLongA(hwnd, GWL_STYLE, GetWindowLongA(hwnd, GWL_STYLE) | MCS_DAYSTATE);
1893     style = GetWindowLongA(hwnd, GWL_STYLE);
1894     ok((style & MCS_DAYSTATE) == 0, "got 0x%08x\n", style);
1895
1896     DestroyWindow(hwnd);
1897
1898     /* with MCS_DAYSTATE */
1899     hwnd = create_monthcal_control(MCS_DAYSTATE);
1900
1901     ret = SendMessageA(hwnd, MCM_GETMONTHRANGE, GMR_DAYSTATE, 0);
1902     expect(4, ret);
1903
1904     ret = SendMessageA(hwnd, MCM_SETDAYSTATE, 4, (LPARAM)&state);
1905     expect(1, ret);
1906
1907     ret = SendMessageA(hwnd, MCM_SETDAYSTATE, 2, (LPARAM)&state);
1908     expect(0, ret);
1909
1910     ret = SendMessageA(hwnd, MCM_SETDAYSTATE, 0, 0);
1911     expect(0, ret);
1912
1913     /* try to switch off */
1914     SetWindowLongA(hwnd, GWL_STYLE, GetWindowLongA(hwnd, GWL_STYLE) & ~MCS_DAYSTATE);
1915     style = GetWindowLongA(hwnd, GWL_STYLE);
1916     ok((style & MCS_DAYSTATE) == MCS_DAYSTATE, "got 0x%08x\n", style);
1917
1918     DestroyWindow(hwnd);
1919 }
1920
1921 START_TEST(monthcal)
1922 {
1923     BOOL (WINAPI *pInitCommonControlsEx)(const INITCOMMONCONTROLSEX*);
1924     INITCOMMONCONTROLSEX iccex;
1925     HMODULE hComctl32;
1926     HWND hwnd;
1927
1928     ULONG_PTR ctx_cookie;
1929     HANDLE hCtx;
1930
1931     hComctl32 = GetModuleHandleA("comctl32.dll");
1932     pInitCommonControlsEx = (void*)GetProcAddress(hComctl32, "InitCommonControlsEx");
1933     if (!pInitCommonControlsEx)
1934     {
1935         skip("InitCommonControlsEx() is missing. Skipping the tests\n");
1936         return;
1937     }
1938     iccex.dwSize = sizeof(iccex);
1939     iccex.dwICC  = ICC_DATE_CLASSES;
1940     pInitCommonControlsEx(&iccex);
1941
1942     test_monthcal();
1943
1944     init_msg_sequences(sequences, NUM_MSG_SEQUENCES);
1945
1946     parent_wnd = create_parent_window();
1947
1948     test_create();
1949     test_destroy();
1950     test_color();
1951     test_currdate();
1952     test_firstDay();
1953     test_unicode();
1954     test_today();
1955     test_scroll();
1956     test_monthrange();
1957     test_hittest();
1958     test_todaylink();
1959     test_size();
1960     test_maxselday();
1961     test_selrange();
1962     test_killfocus();
1963     test_daystate();
1964
1965     if (!load_v6_module(&ctx_cookie, &hCtx))
1966     {
1967         DestroyWindow(parent_wnd);
1968         return;
1969     }
1970
1971     /* this is a XP SP3 failure workaround */
1972     hwnd = CreateWindowExA(0, MONTHCAL_CLASSA, "foo",
1973                            WS_CHILD | WS_BORDER | WS_VISIBLE,
1974                            0, 0, 100, 100,
1975                            parent_wnd, NULL, GetModuleHandleA(NULL), NULL);
1976     if (!IsWindow(hwnd))
1977     {
1978         win_skip("FIXME: failed to create Monthcal window.\n");
1979         unload_v6_module(ctx_cookie, hCtx);
1980         DestroyWindow(parent_wnd);
1981         return;
1982     }
1983     else
1984         DestroyWindow(hwnd);
1985
1986     test_hittest_v6();
1987     test_get_set_border();
1988     test_MCM_SIZERECTTOMIN();
1989     test_MCM_GETCALENDARCOUNT();
1990
1991     unload_v6_module(ctx_cookie, hCtx);
1992
1993     DestroyWindow(parent_wnd);
1994 }