]> git.saurik.com Git - wxWidgets.git/blobdiff - src/generic/calctrlg.cpp
Override wxTextEntry::GetSelection(long *from, long *to) in wxOwnerDrawnComboBox...
[wxWidgets.git] / src / generic / calctrlg.cpp
index 2c5a3403765a72f26bb89a3b20bcf3fe116dde63..367de68c36fbf6ebdff513a8c9e9471a8b94d8e4 100644 (file)
@@ -96,13 +96,14 @@ wxBEGIN_FLAGS( wxCalendarCtrlStyle )
     wxFLAGS_MEMBER(wxCAL_NO_MONTH_CHANGE)
     wxFLAGS_MEMBER(wxCAL_SEQUENTIAL_MONTH_SELECTION)
     wxFLAGS_MEMBER(wxCAL_SHOW_SURROUNDING_WEEKS)
     wxFLAGS_MEMBER(wxCAL_NO_MONTH_CHANGE)
     wxFLAGS_MEMBER(wxCAL_SEQUENTIAL_MONTH_SELECTION)
     wxFLAGS_MEMBER(wxCAL_SHOW_SURROUNDING_WEEKS)
+    wxFLAGS_MEMBER(wxCAL_SHOW_WEEK_NUMBERS)
 
 wxEND_FLAGS( wxCalendarCtrlStyle )
 
 IMPLEMENT_DYNAMIC_CLASS_XTI(wxGenericCalendarCtrl, wxControl,"wx/calctrl.h")
 
 wxBEGIN_PROPERTIES_TABLE(wxGenericCalendarCtrl)
 
 wxEND_FLAGS( wxCalendarCtrlStyle )
 
 IMPLEMENT_DYNAMIC_CLASS_XTI(wxGenericCalendarCtrl, wxControl,"wx/calctrl.h")
 
 wxBEGIN_PROPERTIES_TABLE(wxGenericCalendarCtrl)
-    wxEVENT_RANGE_PROPERTY( Updated , wxEVT_CALENDAR_SEL_CHANGED , wxEVT_CALENDAR_WEEKDAY_CLICKED , wxCalendarEvent )
+    wxEVENT_RANGE_PROPERTY( Updated , wxEVT_CALENDAR_SEL_CHANGED , wxEVT_CALENDAR_WEEK_CLICKED , wxCalendarEvent )
     wxHIDE_PROPERTY( Children )
     wxPROPERTY( Date,wxDateTime, SetDate , GetDate, , 0 /*flags*/ , wxT("Helpstring") , wxT("group"))
     wxPROPERTY_FLAGS( WindowStyle , wxCalendarCtrlStyle , long , SetWindowStyleFlag , GetWindowStyleFlag , , 0 /*flags*/ , wxT("Helpstring") , wxT("group")) // style
     wxHIDE_PROPERTY( Children )
     wxPROPERTY( Date,wxDateTime, SetDate , GetDate, , 0 /*flags*/ , wxT("Helpstring") , wxT("group"))
     wxPROPERTY_FLAGS( WindowStyle , wxCalendarCtrlStyle , long , SetWindowStyleFlag , GetWindowStyleFlag , , 0 /*flags*/ , wxT("Helpstring") , wxT("group")) // style
@@ -190,7 +191,8 @@ void wxGenericCalendarCtrl::Init()
     m_userChangedYear = false;
 
     m_widthCol =
     m_userChangedYear = false;
 
     m_widthCol =
-    m_heightRow = 0;
+    m_heightRow =
+    m_calendarWeekWidth = 0;
 
     wxDateTime::WeekDay wd;
     for ( wd = wxDateTime::Sun; wd < wxDateTime::Inv_WeekDay; wxNextWDay(wd) )
 
     wxDateTime::WeekDay wd;
     for ( wd = wxDateTime::Sun; wd < wxDateTime::Inv_WeekDay; wxNextWDay(wd) )
@@ -246,12 +248,11 @@ bool wxGenericCalendarCtrl::Create(wxWindow *parent,
     if ( !HasFlag(wxCAL_SEQUENTIAL_MONTH_SELECTION) )
     {
         CreateYearSpinCtrl();
     if ( !HasFlag(wxCAL_SEQUENTIAL_MONTH_SELECTION) )
     {
         CreateYearSpinCtrl();
-        m_staticYear = new wxStaticText(GetParent(), wxID_ANY, m_date.Format(_T("%Y")),
+        m_staticYear = new wxStaticText(GetParent(), wxID_ANY, m_date.Format(wxT("%Y")),
                                         wxDefaultPosition, wxDefaultSize,
                                         wxALIGN_CENTRE);
                                         wxDefaultPosition, wxDefaultSize,
                                         wxALIGN_CENTRE);
-
         CreateMonthComboBox();
         CreateMonthComboBox();
-        m_staticMonth = new wxStaticText(GetParent(), wxID_ANY, m_date.Format(_T("%B")),
+        m_staticMonth = new wxStaticText(GetParent(), wxID_ANY, m_date.Format(wxT("%B")),
                                          wxDefaultPosition, wxDefaultSize,
                                          wxALIGN_CENTRE);
     }
                                          wxDefaultPosition, wxDefaultSize,
                                          wxALIGN_CENTRE);
     }
@@ -295,7 +296,7 @@ void wxGenericCalendarCtrl::SetWindowStyleFlag(long style)
     // created/shown/hidden accordingly
     wxASSERT_MSG( (style & wxCAL_SEQUENTIAL_MONTH_SELECTION) ==
                     (m_windowStyle & wxCAL_SEQUENTIAL_MONTH_SELECTION),
     // created/shown/hidden accordingly
     wxASSERT_MSG( (style & wxCAL_SEQUENTIAL_MONTH_SELECTION) ==
                     (m_windowStyle & wxCAL_SEQUENTIAL_MONTH_SELECTION),
-                  _T("wxCAL_SEQUENTIAL_MONTH_SELECTION can't be changed after creation") );
+                  wxT("wxCAL_SEQUENTIAL_MONTH_SELECTION can't be changed after creation") );
 
     wxControl::SetWindowStyleFlag(style);
 }
 
     wxControl::SetWindowStyleFlag(style);
 }
@@ -334,18 +335,21 @@ void wxGenericCalendarCtrl::CreateMonthComboBox()
 void wxGenericCalendarCtrl::CreateYearSpinCtrl()
 {
     m_spinYear = new wxSpinCtrl(GetParent(), wxID_ANY,
 void wxGenericCalendarCtrl::CreateYearSpinCtrl()
 {
     m_spinYear = new wxSpinCtrl(GetParent(), wxID_ANY,
-                                GetDate().Format(_T("%Y")),
+                                GetDate().Format(wxT("%Y")),
                                 wxDefaultPosition,
                                 wxDefaultSize,
                                 wxSP_ARROW_KEYS | wxCLIP_SIBLINGS,
                                 -4300, 10000, GetDate().GetYear());
                                 wxDefaultPosition,
                                 wxDefaultSize,
                                 wxSP_ARROW_KEYS | wxCLIP_SIBLINGS,
                                 -4300, 10000, GetDate().GetYear());
+#ifdef __WXMAC__
+    m_spinYear->SetSize( 90, -1 );
+#endif
 
     m_spinYear->Connect(m_spinYear->GetId(), wxEVT_COMMAND_TEXT_UPDATED,
                         wxCommandEventHandler(wxGenericCalendarCtrl::OnYearTextChange),
                         NULL, this);
 
     m_spinYear->Connect(m_spinYear->GetId(), wxEVT_COMMAND_SPINCTRL_UPDATED,
 
     m_spinYear->Connect(m_spinYear->GetId(), wxEVT_COMMAND_TEXT_UPDATED,
                         wxCommandEventHandler(wxGenericCalendarCtrl::OnYearTextChange),
                         NULL, this);
 
     m_spinYear->Connect(m_spinYear->GetId(), wxEVT_COMMAND_SPINCTRL_UPDATED,
-                        wxCommandEventHandler(wxGenericCalendarCtrl::OnYearChange),
+                        wxSpinEventHandler(wxGenericCalendarCtrl::OnYearChange),
                         NULL, this);
 }
 
                         NULL, this);
 }
 
@@ -516,7 +520,7 @@ bool wxGenericCalendarCtrl::SetDate(const wxDateTime& date)
                     if ( AllowYearChange() )
                     {
                         if ( !m_userChangedYear )
                     if ( AllowYearChange() )
                     {
                         if ( !m_userChangedYear )
-                            m_spinYear->SetValue(m_date.Format(_T("%Y")));
+                            m_spinYear->SetValue(m_date.Format(wxT("%Y")));
                     }
                 }
 
                     }
                 }
 
@@ -644,8 +648,7 @@ wxDateTime wxGenericCalendarCtrl::GetStartDate() const
     wxDateTime date = wxDateTime(1, tm.mon, tm.year);
 
     // rewind back
     wxDateTime date = wxDateTime(1, tm.mon, tm.year);
 
     // rewind back
-    date.SetToPrevWeekDay(GetWindowStyle() & wxCAL_MONDAY_FIRST
-                          ? wxDateTime::Mon : wxDateTime::Sun);
+    date.SetToPrevWeekDay(GetWeekStart());
 
     if ( GetWindowStyle() & wxCAL_SHOW_SURROUNDING_WEEKS )
     {
 
     if ( GetWindowStyle() & wxCAL_SHOW_SURROUNDING_WEEKS )
     {
@@ -678,69 +681,26 @@ bool wxGenericCalendarCtrl::IsDateInRange(const wxDateTime& date) const
         && ( ( m_highdate.IsValid() ) ? ( date <= m_highdate ) : true ) );
 }
 
         && ( ( m_highdate.IsValid() ) ? ( date <= m_highdate ) : true ) );
 }
 
-bool wxGenericCalendarCtrl::ChangeYear(wxDateTime* target) const
+bool wxGenericCalendarCtrl::AdjustDateToRange(wxDateTime *date) const
 {
 {
-    bool retval = false;
-
-    if ( !(IsDateInRange(*target)) )
+    if ( m_lowdate.IsValid() && *date < m_lowdate )
     {
     {
-        if ( target->GetYear() < m_date.GetYear() )
-        {
-            if ( target->GetYear() >= GetLowerDateLimit().GetYear() )
-            {
-                *target = GetLowerDateLimit();
-                retval = true;
-            }
-            else
-            {
-                *target = m_date;
-            }
-        }
-        else
-        {
-            if ( target->GetYear() <= GetUpperDateLimit().GetYear() )
-            {
-                *target = GetUpperDateLimit();
-                retval = true;
-            }
-            else
-            {
-                *target = m_date;
-            }
-        }
-    }
-    else
-    {
-        retval = true;
+        *date = m_lowdate;
+        return true;
     }
 
     }
 
-    return retval;
-}
-
-bool wxGenericCalendarCtrl::ChangeMonth(wxDateTime* target) const
-{
-    bool retval = true;
-
-    if ( !(IsDateInRange(*target)) )
+    if ( m_highdate.IsValid() && *date > m_highdate )
     {
     {
-        retval = false;
-
-        if ( target->GetMonth() < m_date.GetMonth() )
-        {
-            *target = GetLowerDateLimit();
-        }
-        else
-        {
-            *target = GetUpperDateLimit();
-        }
+        *date = m_highdate;
+        return true;
     }
 
     }
 
-    return retval;
+    return false;
 }
 
 size_t wxGenericCalendarCtrl::GetWeek(const wxDateTime& date) const
 {
 }
 
 size_t wxGenericCalendarCtrl::GetWeek(const wxDateTime& date) const
 {
-    size_t retval = date.GetWeekOfMonth(GetWindowStyle() & wxCAL_MONDAY_FIRST
+    size_t retval = date.GetWeekOfMonth(HasFlag(wxCAL_MONDAY_FIRST)
                                    ? wxDateTime::Monday_First
                                    : wxDateTime::Sunday_First);
 
                                    ? wxDateTime::Monday_First
                                    : wxDateTime::Sunday_First);
 
@@ -752,8 +712,7 @@ size_t wxGenericCalendarCtrl::GetWeek(const wxDateTime& date) const
         wxDateTime datetest = wxDateTime(1, tm.mon, tm.year);
 
         // rewind back
         wxDateTime datetest = wxDateTime(1, tm.mon, tm.year);
 
         // rewind back
-        datetest.SetToPrevWeekDay(GetWindowStyle() & wxCAL_MONDAY_FIRST
-                              ? wxDateTime::Mon : wxDateTime::Sun);
+        datetest.SetToPrevWeekDay(GetWeekStart());
 
         if ( datetest.GetDay() == 1 )
         {
 
         if ( datetest.GetDay() == 1 )
         {
@@ -770,56 +729,49 @@ size_t wxGenericCalendarCtrl::GetWeek(const wxDateTime& date) const
 
 // this is a composite control and it must arrange its parts each time its
 // size or position changes: the combobox and spinctrl are along the top of
 
 // this is a composite control and it must arrange its parts each time its
 // size or position changes: the combobox and spinctrl are along the top of
-// the available area and the calendar takes up therest of the space
+// the available area and the calendar takes up the rest of the space
 
 // the static controls are supposed to be always smaller than combo/spin so we
 // always use the latter for size calculations and position the static to take
 // the same space
 
 // the constants used for the layout
 
 // the static controls are supposed to be always smaller than combo/spin so we
 // always use the latter for size calculations and position the static to take
 // the same space
 
 // the constants used for the layout
-#define VERT_MARGIN     5           // distance between combo and calendar
-#ifdef __WXMAC__
+#define VERT_MARGIN    5           // distance between combo and calendar
 #define HORZ_MARGIN    5           //                            spin
 #define HORZ_MARGIN    5           //                            spin
-#else
-#define HORZ_MARGIN    15           //                            spin
-#endif
+
 wxSize wxGenericCalendarCtrl::DoGetBestSize() const
 {
     // calc the size of the calendar
 wxSize wxGenericCalendarCtrl::DoGetBestSize() const
 {
     // calc the size of the calendar
-    wx_const_cast(wxGenericCalendarCtrl *, this)->RecalcGeometry();
+    const_cast<wxGenericCalendarCtrl *>(this)->RecalcGeometry();
 
 
-    wxCoord width = 7*m_widthCol,
+    wxCoord width = 7*m_widthCol + m_calendarWeekWidth,
             height = 7*m_heightRow + m_rowOffset + VERT_MARGIN;
 
     if ( !HasFlag(wxCAL_SEQUENTIAL_MONTH_SELECTION) )
     {
             height = 7*m_heightRow + m_rowOffset + VERT_MARGIN;
 
     if ( !HasFlag(wxCAL_SEQUENTIAL_MONTH_SELECTION) )
     {
-        // the combobox doesn't report its height correctly (it returns the
-        // height including the drop down list) so don't use it
-        height += m_spinYear->GetBestSize().y;
+        const wxSize bestSizeCombo = m_comboMonth->GetBestSize();
 
 
+        height += wxMax(bestSizeCombo.y, m_spinYear->GetBestSize().y)
+                    + VERT_MARGIN;
+#ifdef __WXMAC__
+        // the spin control get clipped otherwise
+        width += 25;
+#endif
 
 
-        wxCoord w2 = m_comboMonth->GetBestSize().x + HORZ_MARGIN + GetCharWidth()*6;
-        if (width < w2)
+        wxCoord w2 = bestSizeCombo.x + HORZ_MARGIN + GetCharWidth()*6;
+        if ( width < w2 )
             width = w2;
     }
 
             width = w2;
     }
 
+    wxSize best(width, height);
     if ( !HasFlag(wxBORDER_NONE) )
     {
     if ( !HasFlag(wxBORDER_NONE) )
     {
-        // the border would clip the last line otherwise
-        height += 6;
-        width += 4;
+        best += GetWindowBorderSize();
     }
 
     }
 
-    wxSize best(width, height);
     CacheBestSize(best);
     CacheBestSize(best);
-    return best;
-}
 
 
-void wxGenericCalendarCtrl::DoSetSize(int x, int y,
-                               int width, int height,
-                               int sizeFlags)
-{
-    wxControl::DoSetSize(x, y, width, height, sizeFlags);
+    return best;
 }
 
 void wxGenericCalendarCtrl::DoMoveWindow(int x, int y, int width, int height)
 }
 
 void wxGenericCalendarCtrl::DoMoveWindow(int x, int y, int width, int height)
@@ -832,26 +784,21 @@ void wxGenericCalendarCtrl::DoMoveWindow(int x, int y, int width, int height)
         wxSize sizeStatic = m_staticMonth->GetSize();
         wxSize sizeSpin = m_spinYear->GetSize();
 
         wxSize sizeStatic = m_staticMonth->GetSize();
         wxSize sizeSpin = m_spinYear->GetSize();
 
-        // wxMSW sometimes reports the wrong combo height,
-        // so on this platform we'll use the spin control
-        // height instead.
-#ifdef __WXMSW__
-        int maxHeight = sizeSpin.y;
-        int requiredSpinHeight = -1;
+        int maxHeight = wxMax(sizeSpin.y, sizeCombo.y);
+        int dy = (maxHeight - sizeStatic.y) / 2;
+#ifdef __WXMAC__
+        m_comboMonth->Move(x, y + (maxHeight - sizeCombo.y)/2 + 2); // FIXME, something is reporting the wrong size..
 #else
 #else
-        int maxHeight = sizeCombo.y;
-        int requiredSpinHeight = sizeCombo.y;
+        m_comboMonth->Move(x, y + (maxHeight - sizeCombo.y)/2);
 #endif
 #endif
-        int dy = (maxHeight - sizeStatic.y) / 2;
-        m_comboMonth->Move(x, y);
         m_staticMonth->SetSize(x, y + dy, sizeCombo.x, -1, sizeStatic.y);
 
         int xDiff = sizeCombo.x + HORZ_MARGIN;
 
         m_staticMonth->SetSize(x, y + dy, sizeCombo.x, -1, sizeStatic.y);
 
         int xDiff = sizeCombo.x + HORZ_MARGIN;
 
-        m_spinYear->SetSize(x + xDiff, y, width - xDiff, requiredSpinHeight);
+        m_spinYear->SetSize(x + xDiff, y + (maxHeight - sizeSpin.y)/2, width - xDiff, maxHeight);
         m_staticYear->SetSize(x + xDiff, y + dy, width - xDiff, sizeStatic.y);
 
         m_staticYear->SetSize(x + xDiff, y + dy, width - xDiff, sizeStatic.y);
 
-        yDiff = wxMax(sizeSpin.y, maxHeight) + VERT_MARGIN;
+        yDiff = maxHeight + VERT_MARGIN;
     }
     else // no controls on the top
     {
     }
     else // no controls on the top
     {
@@ -861,33 +808,21 @@ void wxGenericCalendarCtrl::DoMoveWindow(int x, int y, int width, int height)
     wxControl::DoMoveWindow(x, y + yDiff, width, height - yDiff);
 }
 
     wxControl::DoMoveWindow(x, y + yDiff, width, height - yDiff);
 }
 
-void wxGenericCalendarCtrl::DoGetPosition(int *x, int *y) const
-{
-    wxControl::DoGetPosition(x, y);
-#ifndef __WXPM__
-    if ( !HasFlag(wxCAL_SEQUENTIAL_MONTH_SELECTION) && GetMonthControl() )
-    {
-        // our real top corner is not in this position
-        if ( y )
-        {
-            *y -= GetMonthControl()->GetSize().y + VERT_MARGIN;
-        }
-    }
-#endif
-}
-
 void wxGenericCalendarCtrl::DoGetSize(int *width, int *height) const
 {
 void wxGenericCalendarCtrl::DoGetSize(int *width, int *height) const
 {
-    wxControl::DoGetSize(width, height);
-#ifndef __WXPM__
-    if ( !HasFlag(wxCAL_SEQUENTIAL_MONTH_SELECTION) )
+#ifdef __WXMAC__
+    wxControl::DoGetSize( width, height );
+
+    if ( !HasFlag(wxCAL_SEQUENTIAL_MONTH_SELECTION) && m_staticMonth && height )
     {
     {
-        // our real height is bigger
-        if ( height && GetMonthControl())
-        {
-            *height += GetMonthControl()->GetSize().y + VERT_MARGIN;
-        }
+        wxSize sizeCombo = m_comboMonth->GetEffectiveMinSize();
+        wxSize sizeSpin = m_spinYear->GetSize();
+
+        int maxHeight = wxMax(sizeSpin.y, sizeCombo.y);
+        *height += maxHeight + VERT_MARGIN;
     }
     }
+#else
+    wxControl::DoGetSize( width, height );
 #endif
 }
 
 #endif
 }
 
@@ -922,6 +857,9 @@ void wxGenericCalendarCtrl::RecalcGeometry()
         }
     }
 
         }
     }
 
+    m_calendarWeekWidth = HasFlag( wxCAL_SHOW_WEEK_NUMBERS )
+        ? dc.GetTextExtent( wxString::Format( wxT( "%d" ), 42 )).GetWidth() + 4 : 0;
+
     // leave some margins
     m_widthCol += 2;
     m_heightRow += 2;
     // leave some margins
     m_widthCol += 2;
     m_heightRow += 2;
@@ -948,7 +886,7 @@ void wxGenericCalendarCtrl::OnPaint(wxPaintEvent& WXUNUSED(event))
 #endif
 
     wxCoord y = 0;
 #endif
 
     wxCoord y = 0;
-    wxCoord x0 = wxMax( (GetSize().x - m_widthCol*7) /2 , 0 );
+    wxCoord x0 = m_calendarWeekWidth;
 
     if ( HasFlag(wxCAL_SEQUENTIAL_MONTH_SELECTION) )
     {
 
     if ( HasFlag(wxCAL_SEQUENTIAL_MONTH_SELECTION) )
     {
@@ -1032,7 +970,7 @@ void wxGenericCalendarCtrl::OnPaint(wxPaintEvent& WXUNUSED(event))
         dc.SetPen(wxPen(m_colHeaderBg, 1, wxPENSTYLE_SOLID));
         dc.DrawRectangle(0, y, GetClientSize().x, m_heightRow);
 
         dc.SetPen(wxPen(m_colHeaderBg, 1, wxPENSTYLE_SOLID));
         dc.DrawRectangle(0, y, GetClientSize().x, m_heightRow);
 
-        bool startOnMonday = (GetWindowStyle() & wxCAL_MONDAY_FIRST) != 0;
+        bool startOnMonday = HasFlag(wxCAL_MONDAY_FIRST);
         for ( int wd = 0; wd < 7; wd++ )
         {
             size_t n;
         for ( int wd = 0; wd < 7; wd++ )
         {
             size_t n;
@@ -1051,6 +989,24 @@ void wxGenericCalendarCtrl::OnPaint(wxPaintEvent& WXUNUSED(event))
     //dc.SetFont(*wxNORMAL_FONT);
 
     y += m_heightRow;
     //dc.SetFont(*wxNORMAL_FONT);
 
     y += m_heightRow;
+
+    // draw column with calendar week nr
+    if ( HasFlag( wxCAL_SHOW_WEEK_NUMBERS ) && IsExposed( 0, y, m_calendarWeekWidth, m_heightRow * 6 ))
+    {
+        dc.SetBackgroundMode(wxTRANSPARENT);
+        dc.SetBrush(wxBrush(m_colHeaderBg, wxSOLID));
+        dc.SetPen(wxPen(m_colHeaderBg, 1, wxSOLID));
+        dc.DrawRectangle( 0, y, m_calendarWeekWidth, m_heightRow * 6 );
+        wxDateTime date = GetStartDate();
+        for ( size_t i = 0; i < 6; ++i )
+        {
+            const int weekNr = date.GetWeekOfYear();
+            wxString text = wxString::Format( wxT( "%d" ), weekNr );
+            dc.DrawText( text, m_calendarWeekWidth - dc.GetTextExtent( text ).GetWidth() - 2, y + m_heightRow * i );
+            date += wxDateSpan::Week();
+        }
+    }
+
     wxDateTime date = GetStartDate();
 
 #if DEBUG_PAINT
     wxDateTime date = GetStartDate();
 
 #if DEBUG_PAINT
@@ -1080,9 +1036,9 @@ void wxGenericCalendarCtrl::OnPaint(wxPaintEvent& WXUNUSED(event))
             {
                 // don't use wxDate::Format() which prepends 0s
                 unsigned int day = date.GetDay();
             {
                 // don't use wxDate::Format() which prepends 0s
                 unsigned int day = date.GetDay();
-                wxString dayStr = wxString::Format(_T("%u"), day);
+                wxString dayStr = wxString::Format(wxT("%u"), day);
                 wxCoord width;
                 wxCoord width;
-                dc.GetTextExtent(dayStr, &width, (wxCoord *)NULL);
+                dc.GetTextExtent(dayStr, &width, NULL);
 
                 bool changedColours = false,
                      changedFont = false;
 
                 bool changedColours = false,
                      changedFont = false;
@@ -1175,7 +1131,7 @@ void wxGenericCalendarCtrl::OnPaint(wxPaintEvent& WXUNUSED(event))
                             break;
 
                         default:
                             break;
 
                         default:
-                            wxFAIL_MSG(_T("unknown border type"));
+                            wxFAIL_MSG(wxT("unknown border type"));
                     }
                 }
 
                     }
                 }
 
@@ -1233,9 +1189,9 @@ void wxGenericCalendarCtrl::RefreshDate(const wxDateTime& date)
     wxRect rect;
 
     // always refresh the whole row at once because our OnPaint() will draw
     wxRect rect;
 
     // always refresh the whole row at once because our OnPaint() will draw
-    // the whole row anyhow - and this allows the small optimisation in
+    // the whole row anyhow - and this allows the small optimization in
     // OnClick() below to work
     // OnClick() below to work
-    rect.x = wxMax( (GetSize().x - m_widthCol*7) /2 , 0 );
+    rect.x = m_calendarWeekWidth;
 
     rect.y = (m_heightRow * GetWeek(date)) + m_rowOffset;
 
 
     rect.y = (m_heightRow * GetWeek(date)) + m_rowOffset;
 
@@ -1301,7 +1257,7 @@ void wxGenericCalendarCtrl::HighlightRange(wxPaintDC* pDC, const wxDateTime& fro
             {
                 int numpoints;
                 wxPoint corners[8]; // potentially 8 corners in polygon
             {
                 int numpoints;
                 wxPoint corners[8]; // potentially 8 corners in polygon
-                wxCoord x0 = wxMax( (GetSize().x - m_widthCol*7) /2 , 0 );
+                wxCoord x0 = m_calendarWeekWidth;
 
                 if ( fw == tw )
                 {
 
                 if ( fw == tw )
                 {
@@ -1361,7 +1317,7 @@ bool wxGenericCalendarCtrl::GetDateCoord(const wxDateTime& date, int *day, int *
 
     if ( IsDateShown(date) )
     {
 
     if ( IsDateShown(date) )
     {
-        bool startOnMonday = ( GetWindowStyle() & wxCAL_MONDAY_FIRST ) != 0;
+        bool startOnMonday = HasFlag(wxCAL_MONDAY_FIRST);
 
         // Find day
         *day = date.GetWeekDay();
 
         // Find day
         *day = date.GetWeekDay();
@@ -1474,7 +1430,14 @@ void wxGenericCalendarCtrl::OnClick(wxMouseEvent& event)
                 // GenerateAllChangeEvents() here, we know which event to send
                 GenerateEvent(wxEVT_CALENDAR_DAY_CHANGED);
             }
                 // GenerateAllChangeEvents() here, we know which event to send
                 GenerateEvent(wxEVT_CALENDAR_DAY_CHANGED);
             }
-            break;
+        break;
+
+        case wxCAL_HITTEST_WEEK:
+        {
+            wxCalendarEvent send( this, date, wxEVT_CALENDAR_WEEK_CLICKED );
+            HandleWindowEvent( send );
+        }
+        break;
 
         case wxCAL_HITTEST_HEADER:
             {
 
         case wxCAL_HITTEST_HEADER:
             {
@@ -1492,7 +1455,7 @@ void wxGenericCalendarCtrl::OnClick(wxMouseEvent& event)
             break;
 
         default:
             break;
 
         default:
-            wxFAIL_MSG(_T("unknown hittest code"));
+            wxFAIL_MSG(wxT("unknown hittest code"));
             // fall through
 
         case wxCAL_HITTEST_NOWHERE:
             // fall through
 
         case wxCAL_HITTEST_NOWHERE:
@@ -1512,7 +1475,7 @@ wxCalendarHitTestResult wxGenericCalendarCtrl::HitTest(const wxPoint& pos,
     RecalcGeometry();
 
     // the position where the calendar really begins
     RecalcGeometry();
 
     // the position where the calendar really begins
-    wxCoord x0 = wxMax((GetSize().x - m_widthCol*7)/2, 0);
+    wxCoord x0 = m_calendarWeekWidth;
 
     if ( HasFlag(wxCAL_SEQUENTIAL_MONTH_SELECTION) )
     {
 
     if ( HasFlag(wxCAL_SEQUENTIAL_MONTH_SELECTION) )
     {
@@ -1553,18 +1516,36 @@ wxCalendarHitTestResult wxGenericCalendarCtrl::HitTest(const wxPoint& pos,
 
             return wxCAL_HITTEST_INCMONTH;
         }
 
             return wxCAL_HITTEST_INCMONTH;
         }
+    }
 
 
+    if ( pos.x - x0 < 0 )
+    {
+        if ( pos.x >= 0 && pos.y > m_rowOffset + m_heightRow && pos.y <= m_rowOffset + m_heightRow * 7 )
+        {
+            if ( date )
+            {
+                *date = GetStartDate();
+                *date += wxDateSpan::Week() * (( pos.y - m_rowOffset ) / m_heightRow - 1 );
+            }
+            if ( wd )
+                *wd = GetWeekStart();
+            return wxCAL_HITTEST_WEEK;
+        }
+        else    // early exit -> the rest of the function checks for clicks on days
+            return wxCAL_HITTEST_NOWHERE;
     }
 
     // header: week days
     int wday = (pos.x - x0) / m_widthCol;
     }
 
     // header: week days
     int wday = (pos.x - x0) / m_widthCol;
-    if ( pos.y < (m_heightRow + m_rowOffset) )
+    if ( wday > 6 )
+        return wxCAL_HITTEST_NOWHERE;
+    if ( pos.y < (m_heightRow + m_rowOffset))
     {
         if ( pos.y > m_rowOffset )
         {
             if ( wd )
             {
     {
         if ( pos.y > m_rowOffset )
         {
             if ( wd )
             {
-                if ( GetWindowStyle() & wxCAL_MONDAY_FIRST )
+                if ( HasFlag(wxCAL_MONDAY_FIRST) )
                 {
                     wday = wday == 6 ? 0 : wday + 1;
                 }
                 {
                     wday = wday == 6 ? 0 : wday + 1;
                 }
@@ -1623,13 +1604,18 @@ void wxGenericCalendarCtrl::OnMonthChange(wxCommandEvent& event)
         tm.mday = wxDateTime::GetNumberOfDays(mon, tm.year);
     }
 
         tm.mday = wxDateTime::GetNumberOfDays(mon, tm.year);
     }
 
-    wxDateTime target = wxDateTime(tm.mday, mon, tm.year);
+    wxDateTime dt(tm.mday, mon, tm.year);
+    if ( AdjustDateToRange(&dt) )
+    {
+        // The date must have been changed to ensure it's in valid range,
+        // reflect this in the month choice control.
+        m_comboMonth->SetSelection(dt.GetMonth());
+    }
 
 
-    ChangeMonth(&target);
-    SetDateAndNotify(target);
+    SetDateAndNotify(dt);
 }
 
 }
 
-void wxGenericCalendarCtrl::OnYearChange(wxCommandEvent& event)
+void wxGenericCalendarCtrl::HandleYearChange(wxCommandEvent& event)
 {
     int year = (int)event.GetInt();
     if ( year == INT_MIN )
 {
     int year = (int)event.GetInt();
     if ( year == INT_MIN )
@@ -1645,24 +1631,26 @@ void wxGenericCalendarCtrl::OnYearChange(wxCommandEvent& event)
         tm.mday = wxDateTime::GetNumberOfDays(tm.mon, year);
     }
 
         tm.mday = wxDateTime::GetNumberOfDays(tm.mon, year);
     }
 
-    wxDateTime target = wxDateTime(tm.mday, tm.mon, year);
-
-    if ( ChangeYear(&target) )
-    {
-        SetDateAndNotify(target);
-    }
-    else
+    wxDateTime dt(tm.mday, tm.mon, year);
+    if ( AdjustDateToRange(&dt) )
     {
     {
-        // In this case we don't want to change the date. That would put us
-        // inside the same year but a strange number of months forward/back..
-        m_spinYear->SetValue(target.GetYear());
+        // As above, if the date was changed to keep it in valid range, its
+        // possibly changed year must be shown in the GUI.
+        m_spinYear->SetValue(dt.GetYear());
     }
     }
+
+    SetDateAndNotify(dt);
+}
+
+void wxGenericCalendarCtrl::OnYearChange(wxSpinEvent& event)
+{
+    HandleYearChange( event );
 }
 
 void wxGenericCalendarCtrl::OnYearTextChange(wxCommandEvent& event)
 {
     SetUserChangedYear();
 }
 
 void wxGenericCalendarCtrl::OnYearTextChange(wxCommandEvent& event)
 {
     SetUserChangedYear();
-    OnYearChange(event);
+    HandleYearChange(event);
 }
 
 // Responds to colour changes, and passes event on to children.
 }
 
 // Responds to colour changes, and passes event on to children.
@@ -1685,49 +1673,31 @@ void wxGenericCalendarCtrl::OnSysColourChanged(wxSysColourChangedEvent& event)
 
 void wxGenericCalendarCtrl::OnChar(wxKeyEvent& event)
 {
 
 void wxGenericCalendarCtrl::OnChar(wxKeyEvent& event)
 {
-    wxDateTime target;
     switch ( event.GetKeyCode() )
     {
     switch ( event.GetKeyCode() )
     {
-        case _T('+'):
+        case wxT('+'):
         case WXK_ADD:
         case WXK_ADD:
-            target = m_date + wxDateSpan::Year();
-            if ( ChangeYear(&target) )
-            {
-                SetDateAndNotify(target);
-            }
+            SetDateAndNotify(m_date + wxDateSpan::Year());
             break;
 
             break;
 
-        case _T('-'):
+        case wxT('-'):
         case WXK_SUBTRACT:
         case WXK_SUBTRACT:
-            target = m_date - wxDateSpan::Year();
-            if ( ChangeYear(&target) )
-            {
-                SetDateAndNotify(target);
-            }
+            SetDateAndNotify(m_date - wxDateSpan::Year());
             break;
 
         case WXK_PAGEUP:
             break;
 
         case WXK_PAGEUP:
-            target = m_date - wxDateSpan::Month();
-            ChangeMonth(&target);
-            SetDateAndNotify(target); // always
+            SetDateAndNotify(m_date - wxDateSpan::Month());
             break;
 
         case WXK_PAGEDOWN:
             break;
 
         case WXK_PAGEDOWN:
-            target = m_date + wxDateSpan::Month();
-            ChangeMonth(&target);
-            SetDateAndNotify(target); // always
+            SetDateAndNotify(m_date + wxDateSpan::Month());
             break;
 
         case WXK_RIGHT:
             if ( event.ControlDown() )
             {
             break;
 
         case WXK_RIGHT:
             if ( event.ControlDown() )
             {
-                target = wxDateTime(m_date).SetToNextWeekDay(
-                                 GetWindowStyle() & wxCAL_MONDAY_FIRST
-                                 ? wxDateTime::Sun : wxDateTime::Sat);
-                if ( !IsDateInRange(target) )
-                {
-                    target = GetUpperDateLimit();
-                }
+                wxDateTime target = m_date.SetToNextWeekDay(GetWeekEnd());
+                AdjustDateToRange(&target);
                 SetDateAndNotify(target);
             }
             else
                 SetDateAndNotify(target);
             }
             else
@@ -1737,13 +1707,8 @@ void wxGenericCalendarCtrl::OnChar(wxKeyEvent& event)
         case WXK_LEFT:
             if ( event.ControlDown() )
             {
         case WXK_LEFT:
             if ( event.ControlDown() )
             {
-                target = wxDateTime(m_date).SetToPrevWeekDay(
-                                 GetWindowStyle() & wxCAL_MONDAY_FIRST
-                                 ? wxDateTime::Mon : wxDateTime::Sun);
-                if ( !IsDateInRange(target) )
-                {
-                    target = GetLowerDateLimit();
-                }
+                wxDateTime target = m_date.SetToPrevWeekDay(GetWeekStart());
+                AdjustDateToRange(&target);
                 SetDateAndNotify(target);
             }
             else
                 SetDateAndNotify(target);
             }
             else
@@ -1782,48 +1747,9 @@ void wxGenericCalendarCtrl::OnChar(wxKeyEvent& event)
 // holidays handling
 // ----------------------------------------------------------------------------
 
 // holidays handling
 // ----------------------------------------------------------------------------
 
-void wxGenericCalendarCtrl::EnableHolidayDisplay(bool display)
-{
-    long style = GetWindowStyle();
-    if ( display )
-        style |= wxCAL_SHOW_HOLIDAYS;
-    else
-        style &= ~wxCAL_SHOW_HOLIDAYS;
-
-    SetWindowStyle(style);
-
-    if ( display )
-        SetHolidayAttrs();
-    else
-        ResetHolidayAttrs();
-
-    Refresh();
-}
-
-void wxGenericCalendarCtrl::SetHolidayAttrs()
-{
-    if ( GetWindowStyle() & wxCAL_SHOW_HOLIDAYS )
-    {
-        ResetHolidayAttrs();
-
-        wxDateTime::Tm tm = m_date.GetTm();
-        wxDateTime dtStart(1, tm.mon, tm.year),
-                   dtEnd = dtStart.GetLastMonthDay();
-
-        wxDateTimeArray hol;
-        wxDateTimeHolidayAuthority::GetHolidaysInRange(dtStart, dtEnd, hol);
-
-        size_t count = hol.GetCount();
-        for ( size_t n = 0; n < count; n++ )
-        {
-            SetHoliday(hol[n].GetDay());
-        }
-    }
-}
-
 void wxGenericCalendarCtrl::SetHoliday(size_t day)
 {
 void wxGenericCalendarCtrl::SetHoliday(size_t day)
 {
-    wxCHECK_RET( day > 0 && day < 32, _T("invalid day in SetHoliday") );
+    wxCHECK_RET( day > 0 && day < 32, wxT("invalid day in SetHoliday") );
 
     wxCalendarDateAttr *attr = GetAttr(day);
     if ( !attr )
 
     wxCalendarDateAttr *attr = GetAttr(day);
     if ( !attr )
@@ -1850,7 +1776,7 @@ void wxGenericCalendarCtrl::ResetHolidayAttrs()
 
 void wxGenericCalendarCtrl::Mark(size_t day, bool mark)
 {
 
 void wxGenericCalendarCtrl::Mark(size_t day, bool mark)
 {
-    wxCHECK_RET( day > 0 && day < 32, _T("invalid day in Mark") );
+    wxCHECK_RET( day > 0 && day < 32, wxT("invalid day in Mark") );
 
     const wxCalendarDateAttr& m = wxCalendarDateAttr::GetMark();
     if (mark) {
 
     const wxCalendarDateAttr& m = wxCalendarDateAttr::GetMark();
     if (mark) {