diff --git a/include/wx/calctrl.h b/include/wx/calctrl.h index de8881187e..02f50a14cd 100644 --- a/include/wx/calctrl.h +++ b/include/wx/calctrl.h @@ -27,7 +27,7 @@ enum { // show Sunday as the first day of the week (default) - wxCAL_SUNDAY_FIRST = 0x0000, + wxCAL_SUNDAY_FIRST = 0x0080, // show Monday as the first day of the week wxCAL_MONDAY_FIRST = 0x0001, @@ -332,6 +332,9 @@ protected: // called by EnableHolidayDisplay() virtual void RefreshHolidays() { } + + // does the week start on monday based on flags and OS settings? + bool WeekStartsOnMonday() const; }; // ---------------------------------------------------------------------------- diff --git a/include/wx/generic/calctrlg.h b/include/wx/generic/calctrlg.h index 0c2b48b413..0299b38a3c 100644 --- a/include/wx/generic/calctrlg.h +++ b/include/wx/generic/calctrlg.h @@ -202,13 +202,13 @@ private: // get the first/last days of the week corresponding to the current style wxDateTime::WeekDay GetWeekStart() const { - return HasFlag(wxCAL_MONDAY_FIRST) ? wxDateTime::Mon + return WeekStartsOnMonday() ? wxDateTime::Mon : wxDateTime::Sun; } wxDateTime::WeekDay GetWeekEnd() const { - return HasFlag(wxCAL_MONDAY_FIRST) ? wxDateTime::Sun + return WeekStartsOnMonday() ? wxDateTime::Sun : wxDateTime::Sat; } diff --git a/interface/wx/calctrl.h b/interface/wx/calctrl.h index 688c7fdf91..40bd51493d 100644 --- a/interface/wx/calctrl.h +++ b/interface/wx/calctrl.h @@ -8,7 +8,7 @@ enum { // show Sunday as the first day of the week (default) - wxCAL_SUNDAY_FIRST = 0x0000, + wxCAL_SUNDAY_FIRST = 0x0080, // show Monday as the first day of the week wxCAL_MONDAY_FIRST = 0x0001, @@ -254,6 +254,11 @@ enum wxCalendarHitTestResult month is changed, so you will often want to update them in @c EVT_CALENDAR_PAGE_CHANGED event handler. + If neither the @c wxCAL_SUNDAY_FIRST or @c wxCAL_MONDAY_FIRST style is given, + the first day of the week is determined from operating system's settings, + if possible. The native wxGTK calendar chooses the first weekday based on + locale, and these styles have no effect on it. + @beginStyleTable @style{wxCAL_SUNDAY_FIRST} Show Sunday as the first day in the week (not in wxGTK) diff --git a/src/common/calctrlcmn.cpp b/src/common/calctrlcmn.cpp index 5ab2542251..751b8b7458 100644 --- a/src/common/calctrlcmn.cpp +++ b/src/common/calctrlcmn.cpp @@ -195,5 +195,26 @@ bool wxCalendarCtrlBase::SetHolidayAttrs() return true; } +bool wxCalendarCtrlBase::WeekStartsOnMonday() const +{ + if ( HasFlag(wxCAL_MONDAY_FIRST) ) + { + return true; + } + else if ( HasFlag(wxCAL_SUNDAY_FIRST) ) + { + return false; + } + else + { + // Neither flag was explicitly given, let's make a best guess + // based on locale and/or OS settings. + + wxDateTime::WeekDay firstDay; + wxDateTime::GetFirstWeekDay(&firstDay); + return firstDay == wxDateTime::Mon; + } +} + #endif // wxUSE_CALENDARCTRL diff --git a/src/generic/calctrlg.cpp b/src/generic/calctrlg.cpp index d30e835f34..a3b396221f 100644 --- a/src/generic/calctrlg.cpp +++ b/src/generic/calctrlg.cpp @@ -245,6 +245,10 @@ void wxGenericCalendarCtrl::SetWindowStyleFlag(long style) (m_windowStyle & wxCAL_SEQUENTIAL_MONTH_SELECTION), wxT("wxCAL_SEQUENTIAL_MONTH_SELECTION can't be changed after creation") ); + wxASSERT_MSG( !((style & wxCAL_SUNDAY_FIRST) && + (style & wxCAL_MONDAY_FIRST)), + "wxCAL_SUNDAY_FIRST and wxCAL_MONDAY_FIRST cannot be both used" ); + wxControl::SetWindowStyleFlag(style); } @@ -644,7 +648,7 @@ bool wxGenericCalendarCtrl::AdjustDateToRange(wxDateTime *date) const size_t wxGenericCalendarCtrl::GetWeek(const wxDateTime& date) const { - size_t retval = date.GetWeekOfMonth(HasFlag(wxCAL_MONDAY_FIRST) + size_t retval = date.GetWeekOfMonth(WeekStartsOnMonday() ? wxDateTime::Monday_First : wxDateTime::Sunday_First); @@ -891,7 +895,7 @@ void wxGenericCalendarCtrl::OnPaint(wxPaintEvent& WXUNUSED(event)) dc.SetPen(wxPen(m_colHeaderBg, 1, wxPENSTYLE_SOLID)); dc.DrawRectangle(0, y, GetClientSize().x, m_heightRow); - bool startOnMonday = HasFlag(wxCAL_MONDAY_FIRST); + bool startOnMonday = WeekStartsOnMonday(); for ( int wd = 0; wd < 7; wd++ ) { size_t n; @@ -1238,7 +1242,7 @@ bool wxGenericCalendarCtrl::GetDateCoord(const wxDateTime& date, int *day, int * if ( IsDateShown(date) ) { - bool startOnMonday = HasFlag(wxCAL_MONDAY_FIRST); + bool startOnMonday = WeekStartsOnMonday(); // Find day *day = date.GetWeekDay(); @@ -1479,7 +1483,7 @@ wxCalendarHitTestResult wxGenericCalendarCtrl::HitTest(const wxPoint& pos, { if ( wd ) { - if ( HasFlag(wxCAL_MONDAY_FIRST) ) + if ( WeekStartsOnMonday() ) { wday = wday == 6 ? 0 : wday + 1; } diff --git a/src/msw/calctrl.cpp b/src/msw/calctrl.cpp index 2567cc59d0..86b320bd02 100644 --- a/src/msw/calctrl.cpp +++ b/src/msw/calctrl.cpp @@ -152,11 +152,11 @@ WXDWORD wxCalendarCtrl::MSWGetStyle(long style, WXDWORD *exstyle) const void wxCalendarCtrl::SetWindowStyleFlag(long style) { - const bool hadMondayFirst = HasFlag(wxCAL_MONDAY_FIRST); + const bool hadMondayFirst = WeekStartsOnMonday(); wxCalendarCtrlBase::SetWindowStyleFlag(style); - if ( HasFlag(wxCAL_MONDAY_FIRST) != hadMondayFirst ) + if ( WeekStartsOnMonday() != hadMondayFirst ) UpdateFirstDayOfWeek(); } @@ -427,7 +427,7 @@ void wxCalendarCtrl::UpdateMarks() void wxCalendarCtrl::UpdateFirstDayOfWeek() { MonthCal_SetFirstDayOfWeek(GetHwnd(), - HasFlag(wxCAL_MONDAY_FIRST) ? MonthCal_Monday + WeekStartsOnMonday() ? MonthCal_Monday : MonthCal_Sunday); } diff --git a/src/qt/calctrl.cpp b/src/qt/calctrl.cpp index d2fbd8fcbd..c83474c179 100644 --- a/src/qt/calctrl.cpp +++ b/src/qt/calctrl.cpp @@ -107,9 +107,9 @@ void wxCalendarCtrl::UpdateStyle() if ( !m_qtCalendar ) return; - if ( m_windowStyle & wxCAL_MONDAY_FIRST ) + if ( WeekStartsOnMOnday() ) m_qtCalendar->setFirstDayOfWeek(Qt::Monday); - else // wxCAL_SUNDAY_FIRST + else m_qtCalendar->setFirstDayOfWeek(Qt::Sunday); if ( m_windowStyle & wxCAL_SHOW_WEEK_NUMBERS )