]> git.saurik.com Git - wxWidgets.git/commitdiff
added support for several new events in wxCalendarCtrl: clicking on week
authorVadim Zeitlin <vadim@wxwidgets.org>
Thu, 30 Dec 1999 18:21:42 +0000 (18:21 +0000)
committerVadim Zeitlin <vadim@wxwidgets.org>
Thu, 30 Dec 1999 18:21:42 +0000 (18:21 +0000)
days in the header, double clicking

git-svn-id: https://svn.wxwidgets.org/svn/wx/wxWidgets/trunk@5153 c3d73ce0-8a6f-49c7-b76d-6d57e0e08775

include/wx/calctrl.h
include/wx/event.h
include/wx/generic/calctrl.h
samples/controls/controls.cpp
src/generic/calctrl.cpp

index 1017e4e18236d49d1e002fc5f593f7ae463fbe18..3fe059bea3dddc375407881b89a80f6cd8050b92 100644 (file)
 #ifndef _WX_CALCTRL_H
 #define _WX_CALCTRL_H
 
+// ----------------------------------------------------------------------------
+// constants
+// ----------------------------------------------------------------------------
+
+// return values for the HitTest() method
+enum wxCalendarHitTestResult
+{
+    wxCAL_HITTEST_NOWHERE,      // outside of anything
+    wxCAL_HITTEST_HEADER,       // on the header (weekdays)
+    wxCAL_HITTEST_DAY           // on a day in the calendar
+};
+
+// ----------------------------------------------------------------------------
+// wxCalendarCtrl
+// ----------------------------------------------------------------------------
+
 // so far we only have a generic version, so keep it simple
 #include "wx/generic/calctrl.h"
 
 
 class WXDLLEXPORT wxCalendarEvent : public wxCommandEvent
 {
+friend class wxCalendarCtrl;
 public:
-    wxCalendarEvent() { }
+    wxCalendarEvent() { Init(); }
     wxCalendarEvent(wxCalendarCtrl *cal, wxEventType type);
 
     const wxDateTime& GetDate() const { return m_date; }
+    wxDateTime::WeekDay GetWeekDay() const { return m_wday; }
+
+protected:
+    void Init();
 
 private:
     wxDateTime m_date;
+    wxDateTime::WeekDay m_wday;
 };
 
-#define EVT_CALENDAR(id, fn) { wxEVT_CALENDAR_SEL_CHANGED, id, -1, (wxObjectEventFunction) (wxEventFunction) (wxCommandEventFunction) & fn, (wxObject *) NULL },
+#define EVT_CALENDAR(id, fn) { wxEVT_CALENDAR_DOUBLECLICKED, id, -1, (wxObjectEventFunction) (wxEventFunction) (wxCommandEventFunction) & fn, (wxObject *) NULL },
+#define EVT_CALENDAR_SEL_CHANGED(id, fn) { wxEVT_CALENDAR_SEL_CHANGED, id, -1, (wxObjectEventFunction) (wxEventFunction) (wxCommandEventFunction) & fn, (wxObject *) NULL },
 #define EVT_CALENDAR_DAY(id, fn) { wxEVT_CALENDAR_DAY_CHANGED, id, -1, (wxObjectEventFunction) (wxEventFunction) (wxCommandEventFunction) & fn, (wxObject *) NULL },
 #define EVT_CALENDAR_MONTH(id, fn) { wxEVT_CALENDAR_MONTH_CHANGED, id, -1, (wxObjectEventFunction) (wxEventFunction) (wxCommandEventFunction) & fn, (wxObject *) NULL },
 #define EVT_CALENDAR_YEAR(id, fn) { wxEVT_CALENDAR_YEAR_CHANGED, id, -1, (wxObjectEventFunction) (wxEventFunction) (wxCommandEventFunction) & fn, (wxObject *) NULL },
+#define EVT_CALENDAR_WEEKDAY_CLICKED(id, fn) { wxEVT_CALENDAR_WEEKDAY_CLICKED, id, -1, (wxObjectEventFunction) (wxEventFunction) (wxCommandEventFunction) & fn, (wxObject *) NULL },
 
 #endif // _WX_CALCTRL_H
index d125990a06acc7fbef98738aa7d65212ee9de8cd..1d97b842f8a8872460d729150196e3bb58a12b17 100644 (file)
@@ -255,8 +255,10 @@ const wxEventType wxEVT_CALENDAR_SEL_CHANGED = wxEVT_FIRST + 950;
 const wxEventType wxEVT_CALENDAR_DAY_CHANGED = wxEVT_FIRST + 951;
 const wxEventType wxEVT_CALENDAR_MONTH_CHANGED = wxEVT_FIRST + 952;
 const wxEventType wxEVT_CALENDAR_YEAR_CHANGED = wxEVT_FIRST + 953;
+const wxEventType wxEVT_CALENDAR_DOUBLECLICKED = wxEVT_FIRST + 954;
+const wxEventType wxEVT_CALENDAR_WEEKDAY_CLICKED = wxEVT_FIRST + 955;
 
-const wxEventType wxEVT_USER_FIRST =                        wxEVT_FIRST + 2000;
+const wxEventType wxEVT_USER_FIRST = wxEVT_FIRST + 2000;
 
 /* Compatibility */
 
index c685deeb4846a4348b94faf05c767909fc5e52b7..a3c4617964a1bfd82012334792c41f821a819f9c 100644 (file)
@@ -30,6 +30,9 @@
 
 class WXDLLEXPORT wxCalendarCtrl : public wxControl
 {
+friend class wxMonthComboBox;
+friend class wxYearSpinCtrl;
+
 public:
     // construction
     wxCalendarCtrl() { Init(); }
@@ -62,9 +65,12 @@ public:
     void SetDate(const wxDateTime& date);
     const wxDateTime& GetDate() const { return m_date; }
 
-    // returns TRUE if the given point is on a day and fills date with its
-    // value
-    bool HitTest(const wxPoint& pos, wxDateTime *date);
+    // returns one of wxCAL_HITTEST_XXX constants and fills either date or wd
+    // with the corresponding value (none for NOWHERE, the date for DAY and wd
+    // for HEADER)
+    wxCalendarHitTestResult HitTest(const wxPoint& pos,
+                                    wxDateTime *date = NULL,
+                                    wxDateTime::WeekDay *wd = NULL);
 
     // implementation only from now on
     // -------------------------------
@@ -73,17 +79,18 @@ public:
     virtual bool Enable(bool enable = TRUE);
     virtual bool Show(bool show = TRUE);
 
+private:
+    // common part of all ctors
+    void Init();
+
     // event handlers
     void OnPaint(wxPaintEvent& event);
     void OnClick(wxMouseEvent& event);
+    void OnDClick(wxMouseEvent& event);
     void OnChar(wxKeyEvent& event);
     void OnMonthChange(wxCommandEvent& event);
     void OnYearChange(wxSpinEvent& event);
 
-private:
-    // common part of all ctors
-    void Init();
-
     // override some base class virtuals
     virtual wxSize DoGetBestSize() const;
     virtual void DoGetPosition(int *x, int *y) const;
@@ -112,8 +119,9 @@ private:
     // change the date inside the same month/year
     void ChangeDay(const wxDateTime& date);
 
-    // generate a calendar event
-    void GenerateEvent(wxEventType type);
+    // generate the given calendar event and a "selection changed" one if
+    // selChanged is TRUE
+    void GenerateEvent(wxEventType type, bool selChanged = TRUE);
 
     // the subcontrols
     wxComboBox *m_comboMonth;
index aedd9ae761a05a86d217bd3e1ea490a9c0084eb1..ac99563156aad51e459c620da8e02765713b9cbd 100644 (file)
@@ -113,6 +113,8 @@ public:
     void OnEnableAll(wxCommandEvent& event);
     void OnChangeColour(wxCommandEvent& event);
 
+    void OnCalendar(wxCalendarEvent& event);
+    void OnCalendarWeekDayClick(wxCalendarEvent& event);
     void OnCalendarChange(wxCalendarEvent& event);
 
     wxListBox     *m_listbox,
@@ -380,7 +382,11 @@ EVT_SPINCTRL  (ID_SPINCTRL,             MyPanel::OnSpinCtrl)
 #endif // wxUSE_SPINCTRL
 EVT_BUTTON    (ID_BUTTON_LABEL,         MyPanel::OnUpdateLabel)
 EVT_CHECKBOX  (ID_CHANGE_COLOUR,        MyPanel::OnChangeColour)
-EVT_CALENDAR  (ID_CALENDAR,             MyPanel::OnCalendarChange)
+
+EVT_CALENDAR            (ID_CALENDAR,   MyPanel::OnCalendar)
+EVT_CALENDAR_SEL_CHANGED(ID_CALENDAR,   MyPanel::OnCalendarChange)
+EVT_CALENDAR_WEEKDAY_CLICKED(ID_CALENDAR, MyPanel::OnCalendarWeekDayClick)
+
 END_EVENT_TABLE()
 
 MyPanel::MyPanel( wxFrame *frame, int x, int y, int w, int h )
@@ -758,6 +764,12 @@ void MyPanel::OnPageChanged( wxNotebookEvent &event )
     *m_text << "Notebook selection is " << event.GetSelection() << "\n";
 }
 
+void MyPanel::OnCalendar(wxCalendarEvent& event)
+{
+    *m_text << "Selected " << event.GetDate().FormatISODate() <<
+               " from calendar\n";
+}
+
 void MyPanel::OnCalendarChange(wxCalendarEvent& event)
 {
     wxString s;
@@ -766,6 +778,13 @@ void MyPanel::OnCalendarChange(wxCalendarEvent& event)
     m_date->SetLabel(s);
 }
 
+void MyPanel::OnCalendarWeekDayClick(wxCalendarEvent& event)
+{
+    *m_text << "Clicked on "
+            << wxDateTime::GetWeekDayName(event.GetWeekDay())
+            << "\n";
+}
+
 void MyPanel::OnChangeColour(wxCommandEvent& WXUNUSED(event))
 {
     static wxColour s_colOld;
index a3f7cbd455fef20c53b1665073eabec5f01c3810..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)
@@ -551,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);
+    }
+}
+
+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);
+            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;
+            }
+
+            *wd = (wxDateTime::WeekDay)wday;
+        }
 
-    y -= m_heightRow;
-    int week = y / m_heightRow,
-        wday = pos.x / m_widthCol;
+        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;
+    }
 }
 
 // ----------------------------------------------------------------------------
@@ -697,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)