Automatically determine the first weekday for a calendar control

If neither wxCAL_SUNDAY_FIRST or wxCAL_MONDAY_FIRST was given, use
wxDateTime::GetFirstWeekDay() to automatically determine the preferred
day.

This changes the earlier default behaviour, which was to use Sunday
if not otherwise specified. However, the wxGTK native calendar control
ignored this behaviour anyway.
This commit is contained in:
Lauri Nurmi 2017-07-09 17:56:16 +03:00
parent 378851a283
commit 2f2700b2dd
7 changed files with 46 additions and 13 deletions

View File

@ -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;
};
// ----------------------------------------------------------------------------

View File

@ -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;
}

View File

@ -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)

View File

@ -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

View File

@ -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;
}

View File

@ -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);
}

View File

@ -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 )