- guint year, month, day;
- gtk_calendar_get_date(GTK_CALENDAR(m_widget), &year, &month, &day);
- return wxDateTime(day, (wxDateTime::Month) month, year);
+ guint year, monthGTK, day;
+ gtk_calendar_get_date(GTK_CALENDAR(m_widget), &year, &monthGTK, &day);
+
+ // GTK may return an invalid date, this happens at least when switching the
+ // month (or the year in case of February in a leap year) and the new month
+ // has fewer days than the currently selected one making the currently
+ // selected day invalid, e.g. just choosing May 31 and going back a month
+ // results in the date being (non existent) April 31 when we're called from
+ // gtk_prev_month_callback(). We need to manually work around this to avoid
+ // asserts from wxDateTime ctor.
+ const wxDateTime::Month month = static_cast<wxDateTime::Month>(monthGTK);
+ const guint dayMax = wxDateTime::GetNumberOfDays(month, year);
+ if ( day > dayMax )
+ day = dayMax;
+
+ return wxDateTime(day, month, year);