]> git.saurik.com Git - wxWidgets.git/blobdiff - src/generic/calctrl.cpp
wxLocale now uses wxEncodingConverter (must be explicitly enabled)
[wxWidgets.git] / src / generic / calctrl.cpp
index ed540c9dd43c6af015c1f6af992b826924ac95b5..78b6c57f49f50dc1d8fe34f4fe96170d1c37ec7b 100644 (file)
@@ -78,6 +78,7 @@ BEGIN_EVENT_TABLE(wxCalendarCtrl, wxControl)
     EVT_CHAR(wxCalendarCtrl::OnChar)
 
     EVT_LEFT_DOWN(wxCalendarCtrl::OnClick)
+    EVT_LEFT_DCLICK(wxCalendarCtrl::OnDClick)
 END_EVENT_TABLE()
 
 BEGIN_EVENT_TABLE(wxMonthComboBox, wxComboBox)
@@ -147,15 +148,15 @@ void wxCalendarCtrl::Init()
     }
 }
 
-bool wxCalendarCtrl::Create(wxWindow *parent,
-                            wxWindowID id,
+bool wxCalendarCtrl::Create(wxWindow * WXUNUSED(parent),
+                            wxWindowID WXUNUSED(id),
                             const wxDateTime& date,
-                            const wxPoint& pos,
+                            const wxPoint& WXUNUSED(pos),
                             const wxSize& size,
                             long style,
-                            const wxString& name)
+                            const wxString& WXUNUSED(name))
 {
-    SetWindowStyle(style | (wxBORDER | wxWANTS_CHARS));
+    SetWindowStyle(style | (wxRAISED_BORDER | wxWANTS_CHARS));
 
     m_date = date.IsValid() ? date : wxDateTime::Today();
 
@@ -296,14 +297,13 @@ wxDateTime wxCalendarCtrl::GetStartDate() const
     wxDateTime::Tm tm = m_date.GetTm();
 
     wxDateTime date = wxDateTime(1, tm.mon, tm.year);
-    if ( date.GetWeekDay() != wxDateTime::Sun )
-    {
-        date.SetToPrevWeekDay(wxDateTime::Sun);
 
-        // be sure to do it or it might gain 1 hour if DST changed in between
-        date.ResetTime();
-    }
-    //else: we already have it
+    // rewind back
+    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;
 }
@@ -315,7 +315,9 @@ bool wxCalendarCtrl::IsDateShown(const wxDateTime& date) const
 
 size_t wxCalendarCtrl::GetWeek(const wxDateTime& date) const
 {
-    return date.GetWeekOfMonth(wxDateTime::Sunday_First);
+    return date.GetWeekOfMonth(GetWindowStyle() & wxCAL_MONDAY_FIRST
+                               ? wxDateTime::Monday_First
+                               : wxDateTime::Sunday_First);
 }
 
 // ----------------------------------------------------------------------------
@@ -421,12 +423,10 @@ void wxCalendarCtrl::RecalcGeometry()
 // drawing
 // ----------------------------------------------------------------------------
 
-void wxCalendarCtrl::OnPaint(wxPaintEvent& event)
+void wxCalendarCtrl::OnPaint(wxPaintEvent& WXUNUSED(event))
 {
     wxPaintDC dc(this);
 
-    wxDateTime::WeekDay wd;
-
     dc.SetFont(m_font);
 
     RecalcGeometry();
@@ -449,9 +449,17 @@ void wxCalendarCtrl::OnPaint(wxPaintEvent& event)
         dc.SetBackgroundMode(wxTRANSPARENT);
         dc.SetPen(*wxLIGHT_GREY_PEN);
         dc.DrawRectangle(0, 0, 7*m_widthCol, m_heightRow);
-        for ( wd = wxDateTime::Sun; wd < wxDateTime::Inv_WeekDay; wxNextWDay(wd) )
+
+        bool startOnMonday = (GetWindowStyle() & wxCAL_MONDAY_FIRST) != 0;
+        for ( size_t wd = 0; wd < 7; wd++ )
         {
-            dc.DrawText(m_weekdays[wd], wd*m_widthCol + 1, 0);
+            size_t n;
+            if ( startOnMonday )
+                n = wd == 6 ? 0 : wd + 1;
+            else
+                n = wd;
+
+            dc.DrawText(m_weekdays[n], wd*m_widthCol + 1, 0);
         }
     }
 
@@ -478,11 +486,11 @@ void wxCalendarCtrl::OnPaint(wxPaintEvent& event)
             continue;
         }
 
-#if DEBUG_PAINT        
+#if DEBUG_PAINT
         printf("painting week %d at y = %d\n", nWeek, y);
 #endif
 
-        for ( wd = wxDateTime::Sun; wd < wxDateTime::Inv_WeekDay; wxNextWDay(wd) )
+        for ( size_t wd = 0; wd < 7; wd++ )
         {
             if ( IsDateShown(date) )
             {
@@ -544,44 +552,91 @@ void wxCalendarCtrl::RefreshDate(const wxDateTime& date)
 // 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
     {
-        ChangeDay(date);
+        GenerateEvent(wxEVT_CALENDAR_DOUBLECLICKED, FALSE);
+    }
+}
 
-        GenerateEvent(wxEVT_CALENDAR_DAY_CHANGED);
+void wxCalendarCtrl::OnClick(wxMouseEvent& event)
+{
+    wxDateTime date;
+    wxDateTime::WeekDay wday;
+    switch ( HitTest(event.GetPosition(), &date, &wday) )
+    {
+        case wxCAL_HITTEST_DAY:
+            ChangeDay(date);
+
+            GenerateEvent(wxEVT_CALENDAR_DAY_CHANGED);
+            break;
+
+        case wxCAL_HITTEST_HEADER:
+            {
+                wxCalendarEvent event(this, wxEVT_CALENDAR_WEEKDAY_CLICKED);
+                event.m_wday = wday;
+                (void)GetEventHandler()->ProcessEvent(event);
+            }
+            break;
+
+        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();
 
+    int wday = pos.x / m_widthCol;
+
     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 )
-        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;
+    }
 }
 
 // ----------------------------------------------------------------------------
@@ -607,7 +662,7 @@ void wxCalendarCtrl::OnYearChange(wxSpinEvent& event)
 {
     wxDateTime::Tm tm = m_date.GetTm();
 
-    int year = event.GetInt();
+    int year = (int)event.GetInt();
     if ( tm.mday > wxDateTime::GetNumberOfDays(tm.mon, year) )
     {
         tm.mday = wxDateTime::GetNumberOfDays(tm.mon, year);
@@ -645,11 +700,21 @@ void wxCalendarCtrl::OnChar(wxKeyEvent& event)
             break;
 
         case WXK_RIGHT:
-            SetDateAndNotify(m_date + wxDateSpan::Day());
+            if ( event.ControlDown() )
+                SetDateAndNotify(wxDateTime(m_date).SetToNextWeekDay(
+                                 GetWindowStyle() & wxCAL_MONDAY_FIRST
+                                 ? wxDateTime::Sun : wxDateTime::Sat));
+            else
+                SetDateAndNotify(m_date + wxDateSpan::Day());
             break;
 
         case WXK_LEFT:
-            SetDateAndNotify(m_date - wxDateSpan::Day());
+            if ( event.ControlDown() )
+                SetDateAndNotify(wxDateTime(m_date).SetToPrevWeekDay(
+                                 GetWindowStyle() & wxCAL_MONDAY_FIRST
+                                 ? wxDateTime::Mon : wxDateTime::Sun));
+            else
+                SetDateAndNotify(m_date - wxDateSpan::Day());
             break;
 
         case WXK_UP:
@@ -661,7 +726,14 @@ void wxCalendarCtrl::OnChar(wxKeyEvent& event)
             break;
 
         case WXK_HOME:
-            SetDateAndNotify(wxDateTime::Today());
+            if ( event.ControlDown() )
+                SetDateAndNotify(wxDateTime::Today());
+            else
+                SetDateAndNotify(wxDateTime(1, m_date.GetMonth(), m_date.GetYear()));
+            break;
+
+        case WXK_END:
+            SetDateAndNotify(wxDateTime(m_date).SetToLastMonthDay());
             break;
 
         default:
@@ -673,15 +745,24 @@ void wxCalendarCtrl::OnChar(wxKeyEvent& event)
 // wxCalendarEvent
 // ----------------------------------------------------------------------------
 
-void wxCalendarCtrl::GenerateEvent(wxEventType type)
+void wxCalendarCtrl::GenerateEvent(wxEventType type, bool selChanged)
 {
     // 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);
-
     (void)GetEventHandler()->ProcessEvent(event);
-    (void)GetEventHandler()->ProcessEvent(event2);
+
+    if ( selChanged )
+    {
+        wxCalendarEvent event2(this, wxEVT_CALENDAR_SEL_CHANGED);
+
+        (void)GetEventHandler()->ProcessEvent(event2);
+    }
+}
+
+void wxCalendarEvent::Init()
+{
+    m_wday = wxDateTime::Inv_WeekDay;
 }
 
 wxCalendarEvent::wxCalendarEvent(wxCalendarCtrl *cal, wxEventType type)