>From 171ecdf20a0f6cb76db01fd04b2316d2542dfe8f Mon Sep 17 00:00:00 2001
From: Nikolay Sivov <nsivov@codeweavers.com>
Date: Fri, 23 Sep 2011 10:27:14 +0400
Subject: [PATCH 1/5] Fix MCM_GETMONTHRANGE with regard to multiple calendars and parameter handling

---
 dlls/comctl32/monthcal.c       |   65 ++++++++++++++++++++++-----------------
 dlls/comctl32/tests/monthcal.c |   12 +++++--
 2 files changed, 45 insertions(+), 32 deletions(-)

diff --git a/dlls/comctl32/monthcal.c b/dlls/comctl32/monthcal.c
index 5f8a36c..a6602f9 100644
--- a/dlls/comctl32/monthcal.c
+++ b/dlls/comctl32/monthcal.c
@@ -1330,42 +1330,51 @@ MONTHCAL_SetFirstDayOfWeek(MONTHCAL_INFO *infoPtr, INT day)
 static LRESULT
 MONTHCAL_GetMonthRange(const MONTHCAL_INFO *infoPtr, DWORD flag, SYSTEMTIME *st)
 {
+  INT range;
+
   TRACE("flag=%d, st=%p\n", flag, st);
 
-  if(st)
+  switch (flag) {
+  case GMR_VISIBLE:
   {
-    switch (flag) {
-    case GMR_VISIBLE:
-    {
-        st[0] = infoPtr->calendars[0].month;
-        st[1] = infoPtr->calendars[MONTHCAL_GetCalCount(infoPtr)-1].month;
+      if (st)
+      {
+          st[0] = infoPtr->calendars[0].month;
+          st[1] = infoPtr->calendars[MONTHCAL_GetCalCount(infoPtr)-1].month;
 
-        if (st[0].wMonth == min_allowed_date.wMonth &&
-            st[0].wYear  == min_allowed_date.wYear)
-        {
-            st[0].wDay = min_allowed_date.wDay;
-        }
-        else
-            st[0].wDay = 1;
-        MONTHCAL_CalculateDayOfWeek(&st[0], TRUE);
+          if (st[0].wMonth == min_allowed_date.wMonth &&
+              st[0].wYear  == min_allowed_date.wYear)
+          {
+              st[0].wDay = min_allowed_date.wDay;
+          }
+          else
+              st[0].wDay = 1;
+          MONTHCAL_CalculateDayOfWeek(&st[0], TRUE);
 
-        st[1].wDay = MONTHCAL_MonthLength(st[1].wMonth, st[1].wYear);
-        MONTHCAL_CalculateDayOfWeek(&st[1], TRUE);
+          st[1].wDay = MONTHCAL_MonthLength(st[1].wMonth, st[1].wYear);
+          MONTHCAL_CalculateDayOfWeek(&st[1], TRUE);
+      }
 
-        return MONTHCAL_GetCalCount(infoPtr);
-    }
-    case GMR_DAYSTATE:
-    {
-        MONTHCAL_GetMinDate(infoPtr, &st[0]);
-        MONTHCAL_GetMaxDate(infoPtr, &st[1]);
-        break;
-    }
-    default:
-        WARN("Unknown flag value, got %d\n", flag);
-    }
+      range = MONTHCAL_GetCalCount(infoPtr);
+      break;
+  }
+  case GMR_DAYSTATE:
+  {
+      if (st)
+      {
+          MONTHCAL_GetMinDate(infoPtr, &st[0]);
+          MONTHCAL_GetMaxDate(infoPtr, &st[1]);
+      }
+      /* include two partially visible months */
+      range = MONTHCAL_GetCalCount(infoPtr) + 2;
+      break;
+  }
+  default:
+      WARN("Unknown flag value, got %d\n", flag);
+      range = 0;
   }
 
-  return infoPtr->monthRange;
+  return range;
 }
 
 
diff --git a/dlls/comctl32/tests/monthcal.c b/dlls/comctl32/tests/monthcal.c
index ccf8881..766732b 100644
--- a/dlls/comctl32/tests/monthcal.c
+++ b/dlls/comctl32/tests/monthcal.c
@@ -1317,7 +1317,6 @@ static void test_monthrange(void)
 
     res = SendMessage(hwnd, MCM_GETMONTHRANGE, GMR_VISIBLE, (LPARAM)st_visible);
     expect(2, res);
-
     expect(2000, st_visible[0].wYear);
     expect(11, st_visible[0].wMonth);
     expect(1, st_visible[0].wDay);
@@ -1326,9 +1325,7 @@ static void test_monthrange(void)
     expect(31, st_visible[1].wDay);
 
     res = SendMessage(hwnd, MCM_GETMONTHRANGE, GMR_DAYSTATE, (LPARAM)st_daystate);
-    todo_wine {
-        expect(4, res);
-    }
+    expect(4, res);
     expect(2000, st_daystate[0].wYear);
     expect(10, st_daystate[0].wMonth);
     expect(29, st_daystate[0].wDay);
@@ -1338,6 +1335,13 @@ static void test_monthrange(void)
 
     ok_sequence(sequences, MONTHCAL_SEQ_INDEX, monthcal_monthrange_seq, "monthcal monthrange", FALSE);
 
+    /* with null date array parameter */
+    res = SendMessage(hwnd, MCM_GETMONTHRANGE, GMR_VISIBLE, 0);
+    expect(2, res);
+
+    res = SendMessage(hwnd, MCM_GETMONTHRANGE, GMR_DAYSTATE, 0);
+    expect(4, res);
+
     /* resize control to display single Calendar */
     MoveWindow(hwnd, 0, 0, r.right, r.bottom, FALSE);
 
-- 
1.5.6.5


