From 8ea26eedce3a50c4021bcf8b971e432d93684de2 Mon Sep 17 00:00:00 2001 From: Vadim Zeitlin Date: Tue, 17 May 2011 22:12:35 +0000 Subject: [PATCH] Don't construct invalid wxDateTime in GTK calendar control callbacks. GTK+ may momentarily return invalid date when switching the month in the calendar control. Check for this and adjust the date ourselves to make it valid if necessary in order to avoid the asserts from wxDateTime ctor. Closes #13224. git-svn-id: https://svn.wxwidgets.org/svn/wx/wxWidgets/trunk@67759 c3d73ce0-8a6f-49c7-b76d-6d57e0e08775 --- src/gtk/calctrl.cpp | 19 ++++++++++++++++--- 1 file changed, 16 insertions(+), 3 deletions(-) diff --git a/src/gtk/calctrl.cpp b/src/gtk/calctrl.cpp index 24b1d73347..09104839e0 100644 --- a/src/gtk/calctrl.cpp +++ b/src/gtk/calctrl.cpp @@ -162,9 +162,22 @@ bool wxGtkCalendarCtrl::SetDate(const wxDateTime& date) wxDateTime wxGtkCalendarCtrl::GetDate() const { - 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(monthGTK); + const guint dayMax = wxDateTime::GetNumberOfDays(month, year); + if ( day > dayMax ) + day = dayMax; + + return wxDateTime(day, month, year); } void wxGtkCalendarCtrl::Mark(size_t day, bool mark)