]> git.saurik.com Git - wxWidgets.git/blobdiff - src/generic/calctrl.cpp
Corrected wxScrolledWindow's OnSize behaviour.
[wxWidgets.git] / src / generic / calctrl.cpp
index a3f7cbd455fef20c53b1665073eabec5f01c3810..04facbffb19ee93513f206be31730f78277fe602 100644 (file)
     #include "wx/dcclient.h"
     #include "wx/settings.h"
     #include "wx/brush.h"
     #include "wx/dcclient.h"
     #include "wx/settings.h"
     #include "wx/brush.h"
+    #include "wx/combobox.h"
+    #include "wx/stattext.h"
 #endif //WX_PRECOMP
 
 #endif //WX_PRECOMP
 
+#if wxUSE_CALENDARCTRL
+
 #include "wx/calctrl.h"
 
 #define DEBUG_PAINT 0
 #include "wx/calctrl.h"
 
 #define DEBUG_PAINT 0
@@ -78,6 +82,7 @@ BEGIN_EVENT_TABLE(wxCalendarCtrl, wxControl)
     EVT_CHAR(wxCalendarCtrl::OnChar)
 
     EVT_LEFT_DOWN(wxCalendarCtrl::OnClick)
     EVT_CHAR(wxCalendarCtrl::OnChar)
 
     EVT_LEFT_DOWN(wxCalendarCtrl::OnClick)
+    EVT_LEFT_DCLICK(wxCalendarCtrl::OnDClick)
 END_EVENT_TABLE()
 
 BEGIN_EVENT_TABLE(wxMonthComboBox, wxComboBox)
 END_EVENT_TABLE()
 
 BEGIN_EVENT_TABLE(wxMonthComboBox, wxComboBox)
@@ -89,6 +94,18 @@ BEGIN_EVENT_TABLE(wxYearSpinCtrl, wxSpinCtrl)
 END_EVENT_TABLE()
 
 IMPLEMENT_DYNAMIC_CLASS(wxCalendarCtrl, wxControl)
 END_EVENT_TABLE()
 
 IMPLEMENT_DYNAMIC_CLASS(wxCalendarCtrl, wxControl)
+IMPLEMENT_DYNAMIC_CLASS(wxCalendarEvent, wxCommandEvent)
+
+// ----------------------------------------------------------------------------
+// events
+// ----------------------------------------------------------------------------
+
+DEFINE_EVENT_TYPE(wxEVT_CALENDAR_SEL_CHANGED)
+DEFINE_EVENT_TYPE(wxEVT_CALENDAR_DAY_CHANGED)
+DEFINE_EVENT_TYPE(wxEVT_CALENDAR_MONTH_CHANGED)
+DEFINE_EVENT_TYPE(wxEVT_CALENDAR_YEAR_CHANGED)
+DEFINE_EVENT_TYPE(wxEVT_CALENDAR_DOUBLECLICKED)
+DEFINE_EVENT_TYPE(wxEVT_CALENDAR_WEEKDAY_CLICKED)
 
 // ============================================================================
 // implementation
 
 // ============================================================================
 // implementation
@@ -115,6 +132,7 @@ wxMonthComboBox::wxMonthComboBox(wxCalendarCtrl *cal)
     }
 
     SetSelection(m_cal->GetDate().GetMonth());
     }
 
     SetSelection(m_cal->GetDate().GetMonth());
+    SetSize(-1, -1, -1, -1, wxSIZE_AUTO_WIDTH|wxSIZE_AUTO_HEIGHT);
 }
 
 wxYearSpinCtrl::wxYearSpinCtrl(wxCalendarCtrl *cal)
 }
 
 wxYearSpinCtrl::wxYearSpinCtrl(wxCalendarCtrl *cal)
@@ -145,6 +163,21 @@ void wxCalendarCtrl::Init()
     {
         m_weekdays[wd] = wxDateTime::GetWeekDayName(wd, wxDateTime::Name_Abbr);
     }
     {
         m_weekdays[wd] = wxDateTime::GetWeekDayName(wd, wxDateTime::Name_Abbr);
     }
+
+    for ( size_t n = 0; n < WXSIZEOF(m_attrs); n++ )
+    {
+        m_attrs[n] = NULL;
+    }
+
+    wxSystemSettings ss;
+    m_colHighlightFg = ss.GetSystemColour(wxSYS_COLOUR_HIGHLIGHTTEXT);
+    m_colHighlightBg = ss.GetSystemColour(wxSYS_COLOUR_HIGHLIGHT);
+
+    m_colHolidayFg = *wxRED;
+    // don't set m_colHolidayBg - by default, same as our bg colour
+
+    m_colHeaderFg = *wxBLUE;
+    m_colHeaderBg = *wxLIGHT_GREY;
 }
 
 bool wxCalendarCtrl::Create(wxWindow * WXUNUSED(parent),
 }
 
 bool wxCalendarCtrl::Create(wxWindow * WXUNUSED(parent),
@@ -155,12 +188,22 @@ bool wxCalendarCtrl::Create(wxWindow * WXUNUSED(parent),
                             long style,
                             const wxString& WXUNUSED(name))
 {
                             long style,
                             const wxString& WXUNUSED(name))
 {
-    SetWindowStyle(style | (wxRAISED_BORDER | wxWANTS_CHARS));
+    // needed to get the arrow keys normally used for the dialog navigation
+    SetWindowStyle(style | wxWANTS_CHARS);
 
     m_date = date.IsValid() ? date : wxDateTime::Today();
 
 
     m_date = date.IsValid() ? date : wxDateTime::Today();
 
-    m_comboMonth = new wxMonthComboBox(this);
     m_spinYear = new wxYearSpinCtrl(this);
     m_spinYear = new wxYearSpinCtrl(this);
+    m_staticYear = new wxStaticText(GetParent(), -1, m_date.Format(_T("%Y")),
+                                    wxDefaultPosition, wxDefaultSize,
+                                    wxALIGN_CENTRE);
+
+    m_comboMonth = new wxMonthComboBox(this);
+    m_staticMonth = new wxStaticText(GetParent(), -1, m_date.Format(_T("%B")),
+                                     wxDefaultPosition, wxDefaultSize,
+                                     wxALIGN_CENTRE);
+
+    ShowCurrentControls();
 
     wxSize sizeReal;
     if ( size.x == -1 || size.y == -1 )
 
     wxSize sizeReal;
     if ( size.x == -1 || size.y == -1 )
@@ -181,15 +224,17 @@ bool wxCalendarCtrl::Create(wxWindow * WXUNUSED(parent),
     SetBackgroundColour(*wxWHITE);
     SetFont(*wxSWISS_FONT);
 
     SetBackgroundColour(*wxWHITE);
     SetFont(*wxSWISS_FONT);
 
+    SetHolidayAttrs();
+
     return TRUE;
 }
 
 wxCalendarCtrl::~wxCalendarCtrl()
 {
     return TRUE;
 }
 
 wxCalendarCtrl::~wxCalendarCtrl()
 {
-#if 0
-    m_comboMonth->PopEventHandler();
-    m_spinYear->PopEventHandler();
-#endif // 0
+    for ( size_t n = 0; n < WXSIZEOF(m_attrs); n++ )
+    {
+        delete m_attrs[n];
+    }
 }
 
 // ----------------------------------------------------------------------------
 }
 
 // ----------------------------------------------------------------------------
@@ -203,8 +248,8 @@ bool wxCalendarCtrl::Show(bool show)
         return FALSE;
     }
 
         return FALSE;
     }
 
-    m_comboMonth->Show(show);
-    m_spinYear->Show(show);
+    GetMonthControl()->Show(show);
+    GetYearControl()->Show(show);
 
     return TRUE;
 }
 
     return TRUE;
 }
@@ -216,32 +261,118 @@ bool wxCalendarCtrl::Enable(bool enable)
         return FALSE;
     }
 
         return FALSE;
     }
 
-    m_comboMonth->Enable(enable);
-    m_spinYear->Enable(enable);
+    GetMonthControl()->Enable(enable);
+    GetYearControl()->Enable(enable);
 
     return TRUE;
 }
 
 
     return TRUE;
 }
 
+// ----------------------------------------------------------------------------
+// enable/disable month/year controls
+// ----------------------------------------------------------------------------
+
+void wxCalendarCtrl::ShowCurrentControls()
+{
+    if ( AllowMonthChange() )
+    {
+        m_comboMonth->Show();
+        m_staticMonth->Hide();
+
+        if ( AllowYearChange() )
+        {
+            m_spinYear->Show();
+            m_staticYear->Hide();
+
+            // skip the rest
+            return;
+        }
+    }
+    else
+    {
+        m_comboMonth->Hide();
+        m_staticMonth->Show();
+    }
+
+    // year change not allowed here
+    m_spinYear->Hide();
+    m_staticYear->Show();
+}
+
+wxControl *wxCalendarCtrl::GetMonthControl() const
+{
+    return AllowMonthChange() ? (wxControl *)m_comboMonth : (wxControl *)m_staticMonth;
+}
+
+wxControl *wxCalendarCtrl::GetYearControl() const
+{
+    return AllowYearChange() ? (wxControl *)m_spinYear : (wxControl *)m_staticYear;
+}
+
+void wxCalendarCtrl::EnableYearChange(bool enable)
+{
+    if ( enable != AllowYearChange() )
+    {
+        long style = GetWindowStyle();
+        if ( enable )
+            style &= ~wxCAL_NO_YEAR_CHANGE;
+        else
+            style |= wxCAL_NO_YEAR_CHANGE;
+        SetWindowStyle(style);
+
+        ShowCurrentControls();
+    }
+}
+
+void wxCalendarCtrl::EnableMonthChange(bool enable)
+{
+    if ( enable != AllowMonthChange() )
+    {
+        long style = GetWindowStyle();
+        if ( enable )
+            style &= ~wxCAL_NO_MONTH_CHANGE;
+        else
+            style |= wxCAL_NO_MONTH_CHANGE;
+        SetWindowStyle(style);
+
+        ShowCurrentControls();
+    }
+}
+
 // ----------------------------------------------------------------------------
 // changing date
 // ----------------------------------------------------------------------------
 
 void wxCalendarCtrl::SetDate(const wxDateTime& date)
 {
 // ----------------------------------------------------------------------------
 // changing date
 // ----------------------------------------------------------------------------
 
 void wxCalendarCtrl::SetDate(const wxDateTime& date)
 {
-    if ( m_date.GetMonth() == date.GetMonth() &&
-         m_date.GetYear() == date.GetYear() )
+    bool sameMonth = m_date.GetMonth() == date.GetMonth(),
+         sameYear = m_date.GetYear() == date.GetYear();
+
+    if ( sameMonth && sameYear )
     {
         // just change the day
         ChangeDay(date);
     }
     else
     {
     {
         // just change the day
         ChangeDay(date);
     }
     else
     {
+        if ( !AllowMonthChange() || (!AllowYearChange() && !sameYear) )
+        {
+            // forbidden
+            return;
+        }
+
         // change everything
         m_date = date;
 
         // update the controls
         m_comboMonth->SetSelection(m_date.GetMonth());
         // change everything
         m_date = date;
 
         // update the controls
         m_comboMonth->SetSelection(m_date.GetMonth());
-        m_spinYear->SetValue(m_date.Format(_T("%Y")));
+
+        if ( AllowYearChange() )
+        {
+            m_spinYear->SetValue(m_date.Format(_T("%Y")));
+        }
+
+        // as the month changed, holidays did too
+        SetHolidayAttrs();
 
         // update the calendar
         Refresh();
 
         // update the calendar
         Refresh();
@@ -284,7 +415,7 @@ void wxCalendarCtrl::SetDateAndNotify(const wxDateTime& date)
 
     SetDate(date);
 
 
     SetDate(date);
 
-    GenerateEvent(type);
+    GenerateEvents(type, wxEVT_CALENDAR_SEL_CHANGED);
 }
 
 // ----------------------------------------------------------------------------
 }
 
 // ----------------------------------------------------------------------------
@@ -301,9 +432,6 @@ wxDateTime wxCalendarCtrl::GetStartDate() const
     date.SetToPrevWeekDay(GetWindowStyle() & wxCAL_MONDAY_FIRST
                           ? wxDateTime::Mon : wxDateTime::Sun);
 
     date.SetToPrevWeekDay(GetWindowStyle() & wxCAL_MONDAY_FIRST
                           ? wxDateTime::Mon : wxDateTime::Sun);
 
-    // be sure to do it or it might gain 1 hour if DST changed in between
-    date.ResetTime();
-
     return date;
 }
 
     return date;
 }
 
@@ -327,6 +455,10 @@ size_t wxCalendarCtrl::GetWeek(const wxDateTime& date) const
 // 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
 
 // 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 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
 #define HORZ_MARGIN    15           //                            spin
 // the constants used for the layout
 #define VERT_MARGIN     5           // distance between combo and calendar
 #define HORZ_MARGIN    15           //                            spin
@@ -339,10 +471,15 @@ wxSize wxCalendarCtrl::DoGetBestSize() const
     wxCoord width = 7*m_widthCol,
             height = 7*m_heightRow;
 
     wxCoord width = 7*m_widthCol,
             height = 7*m_heightRow;
 
-    wxSize sizeCombo = m_comboMonth->GetBestSize(),
-           sizeSpin = m_spinYear->GetBestSize();
+    // the combobox doesn't report its height correctly (it returns the
+    // height including the drop down list) so don't use it
+    height += VERT_MARGIN + m_spinYear->GetBestSize().y;
 
 
-    height += VERT_MARGIN + wxMax(sizeCombo.y, sizeSpin.y);
+    if ( GetWindowStyle() & (wxRAISED_BORDER | wxSUNKEN_BORDER) )
+    {
+        // the border would clip the last line otherwise
+        height += 6;
+    }
 
     return wxSize(width, height);
 }
 
     return wxSize(width, height);
 }
@@ -357,10 +494,15 @@ void wxCalendarCtrl::DoSetSize(int x, int y,
 void wxCalendarCtrl::DoMoveWindow(int x, int y, int width, int height)
 {
     wxSize sizeCombo = m_comboMonth->GetSize();
 void wxCalendarCtrl::DoMoveWindow(int x, int y, int width, int height)
 {
     wxSize sizeCombo = m_comboMonth->GetSize();
+    wxSize sizeStatic = m_staticMonth->GetSize();
+
+    int dy = (sizeCombo.y - sizeStatic.y) / 2;
     m_comboMonth->Move(x, y);
     m_comboMonth->Move(x, y);
+    m_staticMonth->SetSize(x, y + dy, sizeCombo.x, sizeStatic.y);
 
     int xDiff = sizeCombo.x + HORZ_MARGIN;
     m_spinYear->SetSize(x + xDiff, y, width - xDiff, sizeCombo.y);
 
     int xDiff = sizeCombo.x + HORZ_MARGIN;
     m_spinYear->SetSize(x + xDiff, y, width - xDiff, sizeCombo.y);
+    m_staticYear->SetSize(x + xDiff, y + dy, width - xDiff, sizeStatic.y);
 
     wxSize sizeSpin = m_spinYear->GetSize();
     int yDiff = wxMax(sizeSpin.y, sizeCombo.y) + VERT_MARGIN;
 
     wxSize sizeSpin = m_spinYear->GetSize();
     int yDiff = wxMax(sizeSpin.y, sizeCombo.y) + VERT_MARGIN;
@@ -375,7 +517,7 @@ void wxCalendarCtrl::DoGetPosition(int *x, int *y) const
     // our real top corner is not in this position
     if ( y )
     {
     // our real top corner is not in this position
     if ( y )
     {
-        *y -= m_comboMonth->GetSize().y + VERT_MARGIN;
+        *y -= GetMonthControl()->GetSize().y + VERT_MARGIN;
     }
 }
 
     }
 }
 
@@ -386,7 +528,7 @@ void wxCalendarCtrl::DoGetSize(int *width, int *height) const
     // our real height is bigger
     if ( height )
     {
     // our real height is bigger
     if ( height )
     {
-        *height += m_comboMonth->GetSize().y + VERT_MARGIN;
+        *height += GetMonthControl()->GetSize().y + VERT_MARGIN;
     }
 }
 
     }
 }
 
@@ -431,7 +573,7 @@ void wxCalendarCtrl::OnPaint(wxPaintEvent& WXUNUSED(event))
     RecalcGeometry();
 
 #if DEBUG_PAINT
     RecalcGeometry();
 
 #if DEBUG_PAINT
-    printf("--- starting to paint, selection: %s, week %u\n",
+    wxLogDebug("--- starting to paint, selection: %s, week %u\n",
            m_date.Format("%a %d-%m-%Y %H:%M:%S").c_str(),
            GetWeek(m_date));
 #endif
            m_date.Format("%a %d-%m-%Y %H:%M:%S").c_str(),
            GetWeek(m_date));
 #endif
@@ -440,13 +582,13 @@ void wxCalendarCtrl::OnPaint(wxPaintEvent& WXUNUSED(event))
     if ( IsExposed(0, 0, 7*m_widthCol, m_heightRow) )
     {
 #if DEBUG_PAINT
     if ( IsExposed(0, 0, 7*m_widthCol, m_heightRow) )
     {
 #if DEBUG_PAINT
-        puts("painting the header");
+        wxLogDebug("painting the header");
 #endif
 
 #endif
 
-        dc.SetTextForeground(*wxBLUE);
-        dc.SetBrush(wxBrush(*wxLIGHT_GREY, wxSOLID));
         dc.SetBackgroundMode(wxTRANSPARENT);
         dc.SetBackgroundMode(wxTRANSPARENT);
-        dc.SetPen(*wxLIGHT_GREY_PEN);
+        dc.SetTextForeground(m_colHeaderFg);
+        dc.SetBrush(wxBrush(m_colHeaderBg, wxSOLID));
+        dc.SetPen(wxPen(m_colHeaderBg, 1, wxSOLID));
         dc.DrawRectangle(0, 0, 7*m_widthCol, m_heightRow);
 
         bool startOnMonday = (GetWindowStyle() & wxCAL_MONDAY_FIRST) != 0;
         dc.DrawRectangle(0, 0, 7*m_widthCol, m_heightRow);
 
         bool startOnMonday = (GetWindowStyle() & wxCAL_MONDAY_FIRST) != 0;
@@ -470,7 +612,7 @@ void wxCalendarCtrl::OnPaint(wxPaintEvent& WXUNUSED(event))
 
     wxDateTime date = GetStartDate();
 #if DEBUG_PAINT
 
     wxDateTime date = GetStartDate();
 #if DEBUG_PAINT
-    printf("starting calendar from %s\n",
+    wxLogDebug("starting calendar from %s\n",
             date.Format("%a %d-%m-%Y %H:%M:%S").c_str());
 #endif
 
             date.Format("%a %d-%m-%Y %H:%M:%S").c_str());
 #endif
 
@@ -486,7 +628,7 @@ void wxCalendarCtrl::OnPaint(wxPaintEvent& WXUNUSED(event))
         }
 
 #if DEBUG_PAINT
         }
 
 #if DEBUG_PAINT
-        printf("painting week %d at y = %d\n", nWeek, y);
+        wxLogDebug("painting week %d at y = %d\n", nWeek, y);
 #endif
 
         for ( size_t wd = 0; wd < 7; wd++ )
 #endif
 
         for ( size_t wd = 0; wd < 7; wd++ )
@@ -494,24 +636,104 @@ void wxCalendarCtrl::OnPaint(wxPaintEvent& WXUNUSED(event))
             if ( IsDateShown(date) )
             {
                 // don't use wxDate::Format() which prepends 0s
             if ( IsDateShown(date) )
             {
                 // don't use wxDate::Format() which prepends 0s
-                wxString day = wxString::Format(_T("%u"), date.GetDay());
+                unsigned int day = date.GetDay();
+                wxString dayStr = wxString::Format(_T("%u"), day);
                 wxCoord width;
                 wxCoord width;
-                dc.GetTextExtent(day, &width, (wxCoord *)NULL);
+                dc.GetTextExtent(dayStr, &width, (wxCoord *)NULL);
+
+                bool changedColours = FALSE,
+                     changedFont = FALSE;
+
+                wxCalendarDateAttr *attr = m_attrs[day - 1];
 
 
-                bool isSel = m_date == date;
+                bool isSel = date.IsSameDate(m_date);
                 if ( isSel )
                 {
                 if ( isSel )
                 {
-                    dc.SetTextForeground(wxSystemSettings::GetSystemColour(wxSYS_COLOUR_HIGHLIGHTTEXT));
-                    dc.SetTextBackground(wxSystemSettings::GetSystemColour(wxSYS_COLOUR_HIGHLIGHT));
+                    dc.SetTextForeground(m_colHighlightFg);
+                    dc.SetTextBackground(m_colHighlightBg);
+
+                    changedColours = TRUE;
+                }
+                else if ( attr )
+                {
+                    wxColour colFg, colBg;
+
+                    if ( attr->IsHoliday() )
+                    {
+                        colFg = m_colHolidayFg;
+                        colBg = m_colHolidayBg;
+                    }
+                    else
+                    {
+                        colFg = attr->GetTextColour();
+                        colBg = attr->GetBackgroundColour();
+                    }
+
+                    if ( colFg.Ok() )
+                    {
+                        dc.SetTextForeground(colFg);
+                        changedColours = TRUE;
+                    }
+
+                    if ( colBg.Ok() )
+                    {
+                        dc.SetTextBackground(colBg);
+                        changedColours = TRUE;
+                    }
+
+                    if ( attr->HasFont() )
+                    {
+                        dc.SetFont(attr->GetFont());
+                        changedFont = TRUE;
+                    }
                 }
 
                 }
 
-                dc.DrawText(day, wd*m_widthCol + (m_widthCol - width) / 2, y);
+                wxCoord x = wd*m_widthCol + (m_widthCol - width) / 2;
+                dc.DrawText(dayStr, x, y + 1);
 
 
-                if ( isSel )
+                if ( !isSel && attr && attr->HasBorder() )
+                {
+                    wxColour colBorder;
+                    if ( attr->HasBorderColour() )
+                    {
+                        colBorder = attr->GetBorderColour();
+                    }
+                    else
+                    {
+                        colBorder = m_foregroundColour;
+                    }
+
+                    wxPen pen(colBorder, 1, wxSOLID);
+                    dc.SetPen(pen);
+                    dc.SetBrush(*wxTRANSPARENT_BRUSH);
+
+                    switch ( attr->GetBorder() )
+                    {
+                        case wxCAL_BORDER_SQUARE:
+                            dc.DrawRectangle(x - 2, y,
+                                             width + 4, m_heightRow);
+                            break;
+
+                        case wxCAL_BORDER_ROUND:
+                            dc.DrawEllipse(x - 2, y,
+                                           width + 4, m_heightRow);
+                            break;
+
+                        default:
+                            wxFAIL_MSG(_T("unknown border type"));
+                    }
+                }
+
+                if ( changedColours )
                 {
                     dc.SetTextForeground(m_foregroundColour);
                     dc.SetTextBackground(m_backgroundColour);
                 }
                 {
                     dc.SetTextForeground(m_foregroundColour);
                     dc.SetTextBackground(m_backgroundColour);
                 }
+
+                if ( changedFont )
+                {
+                    dc.SetFont(m_font);
+                }
             }
             //else: just don't draw it
 
             }
             //else: just don't draw it
 
@@ -519,7 +741,7 @@ void wxCalendarCtrl::OnPaint(wxPaintEvent& WXUNUSED(event))
         }
     }
 #if DEBUG_PAINT
         }
     }
 #if DEBUG_PAINT
-    puts("+++ finished painting");
+    wxLogDebug("+++ finished painting");
 #endif
 }
 
 #endif
 }
 
@@ -537,8 +759,17 @@ void wxCalendarCtrl::RefreshDate(const wxDateTime& date)
     rect.width = 7*m_widthCol;
     rect.height = m_heightRow;
 
     rect.width = 7*m_widthCol;
     rect.height = m_heightRow;
 
+#ifdef __WXMSW__
+    // VZ: for some reason, the selected date seems to occupy more space under
+    //     MSW - this is probably some bug in the font size calculations, but I
+    //     don't know where exactly. This fix is ugly and leads to more
+    //     refreshes than really needed, but without it the selected days
+    //     leaves even more ugly underscores on screen.
+    rect.Inflate(0, 1);
+#endif // MSW
+
 #if DEBUG_PAINT
 #if DEBUG_PAINT
-    printf("*** refreshing week %d at (%d, %d)-(%d, %d)\n",
+    wxLogDebug("*** refreshing week %d at (%d, %d)-(%d, %d)\n",
            GetWeek(date),
            rect.x, rect.y,
            rect.x + rect.width, rect.y + rect.height);
            GetWeek(date),
            rect.x, rect.y,
            rect.x + rect.width, rect.y + rect.height);
@@ -551,44 +782,92 @@ void wxCalendarCtrl::RefreshDate(const wxDateTime& date)
 // mouse handling
 // ----------------------------------------------------------------------------
 
 // mouse handling
 // ----------------------------------------------------------------------------
 
-void wxCalendarCtrl::OnClick(wxMouseEvent& event)
+void wxCalendarCtrl::OnDClick(wxMouseEvent& event)
 {
 {
-    RecalcGeometry();
-
-    wxDateTime date;
-    if ( !HitTest(event.GetPosition(), &date) )
+    if ( HitTest(event.GetPosition()) != wxCAL_HITTEST_DAY )
     {
         event.Skip();
     }
     else
     {
     {
         event.Skip();
     }
     else
     {
-        ChangeDay(date);
+        GenerateEvent(wxEVT_CALENDAR_DOUBLECLICKED);
+    }
+}
+
+void wxCalendarCtrl::OnClick(wxMouseEvent& event)
+{
+    wxDateTime date;
+    wxDateTime::WeekDay wday;
+    switch ( HitTest(event.GetPosition(), &date, &wday) )
+    {
+        case wxCAL_HITTEST_DAY:
+            ChangeDay(date);
+
+            GenerateEvents(wxEVT_CALENDAR_DAY_CHANGED,
+                           wxEVT_CALENDAR_SEL_CHANGED);
+            break;
+
+        case wxCAL_HITTEST_HEADER:
+            {
+                wxCalendarEvent event(this, wxEVT_CALENDAR_WEEKDAY_CLICKED);
+                event.m_wday = wday;
+                (void)GetEventHandler()->ProcessEvent(event);
+            }
+            break;
 
 
-        GenerateEvent(wxEVT_CALENDAR_DAY_CHANGED);
+        default:
+            wxFAIL_MSG(_T("unknown hittest code"));
+            // fall through
+
+        case wxCAL_HITTEST_NOWHERE:
+            event.Skip();
+            break;
     }
 }
 
     }
 }
 
-bool wxCalendarCtrl::HitTest(const wxPoint& pos, wxDateTime *date)
+wxCalendarHitTestResult wxCalendarCtrl::HitTest(const wxPoint& pos,
+                                                wxDateTime *date,
+                                                wxDateTime::WeekDay *wd)
 {
     RecalcGeometry();
 
 {
     RecalcGeometry();
 
+    int wday = pos.x / m_widthCol;
+
     wxCoord y = pos.y;
     if ( y < m_heightRow )
     wxCoord y = pos.y;
     if ( y < m_heightRow )
-        return FALSE;
+    {
+        if ( wd )
+        {
+            if ( GetWindowStyle() & wxCAL_MONDAY_FIRST )
+            {
+                wday = wday == 6 ? 0 : wday + 1;
+            }
 
 
-    y -= m_heightRow;
-    int week = y / m_heightRow,
-        wday = pos.x / m_widthCol;
+            *wd = (wxDateTime::WeekDay)wday;
+        }
+
+        return wxCAL_HITTEST_HEADER;
+    }
 
 
+    int week = (y - m_heightRow) / m_heightRow;
     if ( week >= 6 || wday >= 7 )
     if ( week >= 6 || wday >= 7 )
-        return FALSE;
+    {
+        return wxCAL_HITTEST_NOWHERE;
+    }
 
 
-    wxCHECK_MSG( date, FALSE, _T("bad pointer in wxCalendarCtrl::HitTest") );
+    wxDateTime dt = GetStartDate() + wxDateSpan::Days(7*week + wday);
 
 
-    *date = GetStartDate();
-    *date += wxDateSpan::Days(7*week + wday);
+    if ( IsDateShown(dt) )
+    {
+        if ( date )
+            *date = dt;
 
 
-    return IsDateShown(*date);
+        return wxCAL_HITTEST_DAY;
+    }
+    else
+    {
+        return wxCAL_HITTEST_NOWHERE;
+    }
 }
 
 // ----------------------------------------------------------------------------
 }
 
 // ----------------------------------------------------------------------------
@@ -605,9 +884,7 @@ void wxCalendarCtrl::OnMonthChange(wxCommandEvent& event)
         tm.mday = wxDateTime::GetNumberOfDays(mon, tm.year);
     }
 
         tm.mday = wxDateTime::GetNumberOfDays(mon, tm.year);
     }
 
-    SetDate(wxDateTime(tm.mday, mon, tm.year));
-
-    GenerateEvent(wxEVT_CALENDAR_MONTH_CHANGED);
+    SetDateAndNotify(wxDateTime(tm.mday, mon, tm.year));
 }
 
 void wxCalendarCtrl::OnYearChange(wxSpinEvent& event)
 }
 
 void wxCalendarCtrl::OnYearChange(wxSpinEvent& event)
@@ -620,9 +897,7 @@ void wxCalendarCtrl::OnYearChange(wxSpinEvent& event)
         tm.mday = wxDateTime::GetNumberOfDays(tm.mon, year);
     }
 
         tm.mday = wxDateTime::GetNumberOfDays(tm.mon, year);
     }
 
-    SetDate(wxDateTime(tm.mday, tm.mon, year));
-
-    GenerateEvent(wxEVT_CALENDAR_YEAR_CHANGED);
+    SetDateAndNotify(wxDateTime(tm.mday, tm.mon, year));
 }
 
 // ----------------------------------------------------------------------------
 }
 
 // ----------------------------------------------------------------------------
@@ -688,24 +963,92 @@ void wxCalendarCtrl::OnChar(wxKeyEvent& event)
             SetDateAndNotify(wxDateTime(m_date).SetToLastMonthDay());
             break;
 
             SetDateAndNotify(wxDateTime(m_date).SetToLastMonthDay());
             break;
 
+        case WXK_RETURN:
+            GenerateEvent(wxEVT_CALENDAR_DOUBLECLICKED);
+            break;
+
         default:
             event.Skip();
     }
 }
 
 // ----------------------------------------------------------------------------
         default:
             event.Skip();
     }
 }
 
 // ----------------------------------------------------------------------------
-// wxCalendarEvent
+// holidays handling
 // ----------------------------------------------------------------------------
 
 // ----------------------------------------------------------------------------
 
-void wxCalendarCtrl::GenerateEvent(wxEventType type)
+void wxCalendarCtrl::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 wxCalendarCtrl::SetHolidayAttrs()
 {
 {
-    // we're called for a change in some particular date field but we always
-    // also generate a generic "changed" event
-    wxCalendarEvent event(this, type);
-    wxCalendarEvent event2(this, wxEVT_CALENDAR_SEL_CHANGED);
+    if ( GetWindowStyle() & wxCAL_SHOW_HOLIDAYS )
+    {
+        ResetHolidayAttrs();
 
 
-    (void)GetEventHandler()->ProcessEvent(event);
-    (void)GetEventHandler()->ProcessEvent(event2);
+        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 wxCalendarCtrl::SetHoliday(size_t day)
+{
+    wxCHECK_RET( day > 0 && day < 32, _T("invalid day in SetHoliday") );
+
+    wxCalendarDateAttr *attr = GetAttr(day);
+    if ( !attr )
+    {
+        attr = new wxCalendarDateAttr;
+    }
+
+    attr->SetHoliday(TRUE);
+
+    // can't use SetAttr() because it would delete this pointer
+    m_attrs[day - 1] = attr;
+}
+
+void wxCalendarCtrl::ResetHolidayAttrs()
+{
+    for ( size_t day = 0; day < 31; day++ )
+    {
+        if ( m_attrs[day] )
+        {
+            m_attrs[day]->SetHoliday(FALSE);
+        }
+    }
+}
+
+// ----------------------------------------------------------------------------
+// wxCalendarEvent
+// ----------------------------------------------------------------------------
+
+void wxCalendarEvent::Init()
+{
+    m_wday = wxDateTime::Inv_WeekDay;
 }
 
 wxCalendarEvent::wxCalendarEvent(wxCalendarCtrl *cal, wxEventType type)
 }
 
 wxCalendarEvent::wxCalendarEvent(wxCalendarCtrl *cal, wxEventType type)
@@ -713,3 +1056,6 @@ wxCalendarEvent::wxCalendarEvent(wxCalendarCtrl *cal, wxEventType type)
 {
     m_date = cal->GetDate();
 }
 {
     m_date = cal->GetDate();
 }
+
+#endif // wxUSE_CALENDARCTRL
+