]> git.saurik.com Git - wxWidgets.git/blobdiff - samples/calendar/calendar.cpp
remove wxNewEventFunctor() overload which allowed calls to "Bind(evt, &WrongClass...
[wxWidgets.git] / samples / calendar / calendar.cpp
index 5241a440a689bd4fb83a8956f024ecd6f9e0a7a3..47d7dbae302346407ddd2333ec8f798e0ab6b71b 100644 (file)
@@ -81,6 +81,7 @@ public:
 
     void OnCalendar(wxCalendarEvent& event);
     void OnCalendarWeekDayClick(wxCalendarEvent& event);
+    void OnCalendarWeekClick(wxCalendarEvent& event);
     void OnCalendarChange(wxCalendarEvent& event);
     void OnCalMonthChange(wxCalendarEvent& event);
 
@@ -102,6 +103,8 @@ public:
     void SetDate(const wxDateTime& dt) { m_calendar->SetDate(dt); }
 
 private:
+    wxCalendarCtrlBase *DoCreateCalendar(const wxDateTime& dt, long style);
+
     void RecreateCalendar(long style);
 
     wxCalendarCtrlBase *m_calendar;
@@ -147,6 +150,7 @@ public:
 
     void OnCalSeqMonth(wxCommandEvent& event);
     void OnCalShowSurroundingWeeks(wxCommandEvent& event);
+    void OnCalShowWeekNumbers(wxCommandEvent& event);
 
     void OnSetDate(wxCommandEvent& event);
     void OnToday(wxCommandEvent& event);
@@ -159,6 +163,8 @@ public:
         event.Enable(m_panel->IsUsingGeneric());
     }
 
+    void OnCalRClick(wxMouseEvent& event);
+
 private:
     MyPanel *m_panel;
     wxTextCtrl *m_logWindow;
@@ -208,6 +214,7 @@ enum
     Calendar_Cal_Month,
     Calendar_Cal_SeqMonth,
     Calendar_Cal_SurroundWeeks,
+    Calendar_Cal_WeekNumbers,
     Calendar_Cal_SetDate,
     Calendar_Cal_Today,
     Calendar_Cal_BeginDST,
@@ -256,6 +263,7 @@ BEGIN_EVENT_TABLE(MyFrame, wxFrame)
 
     EVT_MENU(Calendar_Cal_SeqMonth, MyFrame::OnCalSeqMonth)
     EVT_MENU(Calendar_Cal_SurroundWeeks, MyFrame::OnCalShowSurroundingWeeks)
+    EVT_MENU(Calendar_Cal_WeekNumbers, MyFrame::OnCalShowWeekNumbers)
 
     EVT_MENU(Calendar_Cal_SetDate, MyFrame::OnSetDate)
     EVT_MENU(Calendar_Cal_Today, MyFrame::OnToday)
@@ -265,8 +273,10 @@ BEGIN_EVENT_TABLE(MyFrame, wxFrame)
 
 
     EVT_UPDATE_UI(Calendar_Cal_SeqMonth, MyFrame::OnUpdateUIGenericOnly)
+#ifdef __WXGTK20__
     EVT_UPDATE_UI(Calendar_Cal_Monday, MyFrame::OnUpdateUIGenericOnly)
     EVT_UPDATE_UI(Calendar_Cal_Holidays, MyFrame::OnUpdateUIGenericOnly)
+#endif
     EVT_UPDATE_UI(Calendar_Cal_Special, MyFrame::OnUpdateUIGenericOnly)
     EVT_UPDATE_UI(Calendar_Cal_SurroundWeeks, MyFrame::OnUpdateUIGenericOnly)
 END_EVENT_TABLE()
@@ -276,6 +286,7 @@ BEGIN_EVENT_TABLE(MyPanel, wxPanel)
     EVT_CALENDAR_PAGE_CHANGED(Calendar_CalCtrl, MyPanel::OnCalMonthChange)
     EVT_CALENDAR_SEL_CHANGED(Calendar_CalCtrl, MyPanel::OnCalendarChange)
     EVT_CALENDAR_WEEKDAY_CLICKED(Calendar_CalCtrl, MyPanel::OnCalendarWeekDayClick)
+    EVT_CALENDAR_WEEK_CLICKED(Calendar_CalCtrl,  MyPanel::OnCalendarWeekClick)
 END_EVENT_TABLE()
 
 #if wxUSE_DATEPICKCTRL
@@ -312,7 +323,7 @@ bool MyApp::OnInit()
 #ifndef __WXWINCE__
                                  ,wxPoint(50, 50), wxSize(450, 340)
 #endif
-                                                                );
+                                 );
 
     frame->Show(true);
 
@@ -361,6 +372,10 @@ MyFrame::MyFrame(const wxString& title, const wxPoint& pos, const wxSize& size)
                     _T("Show s&urrounding weeks\tCtrl-W"),
                     _T("Show the neighbouring weeks in the prev/next month"),
                     true);
+    menuCal->Append(Calendar_Cal_WeekNumbers,
+                    _T("Show &week numbers"),
+                    _T("Toggle week numbers"),
+                    true);
     menuCal->AppendSeparator();
     menuCal->Append(Calendar_Cal_SeqMonth,
                     _T("Toggle month selector st&yle\tCtrl-Y"),
@@ -449,13 +464,7 @@ void MyFrame::OnCalMonday(wxCommandEvent& event)
 
 void MyFrame::OnCalHolidays(wxCommandEvent& event)
 {
-    wxCalendarCtrlBase * cal = m_panel->GetCal();
-#ifdef wxHAS_NATIVE_CALENDARCTRL
-    wxStaticCast(cal, wxGenericCalendarCtrl)
-#else
-    cal
-#endif
-    ->EnableHolidayDisplay(event.IsChecked());
+    m_panel->GetCal()->EnableHolidayDisplay(event.IsChecked());
 }
 
 void MyFrame::OnCalSpecial(wxCommandEvent& event)
@@ -479,6 +488,11 @@ void MyFrame::OnCalShowSurroundingWeeks(wxCommandEvent& event)
     m_panel->ToggleCalStyle(event.IsChecked(), wxCAL_SHOW_SURROUNDING_WEEKS);
 }
 
+void MyFrame::OnCalShowWeekNumbers(wxCommandEvent& event)
+{
+    m_panel->ToggleCalStyle(event.IsChecked(), wxCAL_SHOW_WEEK_NUMBERS);
+}
+
 void MyFrame::OnSetDate(wxCommandEvent &WXUNUSED(event))
 {
     m_panel->SetDate(wxDateTime(24, wxDateTime::Dec, 2005, 22, 00, 00));
@@ -512,6 +526,48 @@ void MyFrame::OnCalToggleResizable(wxCommandEvent& event)
     sizer->Layout();
 }
 
+void MyFrame::OnCalRClick(wxMouseEvent& event)
+{
+    wxDateTime dt;
+    wxDateTime::WeekDay wd;
+
+    const wxPoint pt = event.GetPosition();
+    wxString msg = wxString::Format("Point (%d, %d) is ", pt.x, pt.y);
+
+    switch ( m_panel->GetCal()->HitTest(pt, &dt, &wd) )
+    {
+        default:
+            wxFAIL_MSG( "unexpected" );
+            // fall through
+
+        case wxCAL_HITTEST_NOWHERE:
+            msg += "nowhere";
+            break;
+
+        case wxCAL_HITTEST_HEADER:
+            msg += wxString::Format("over %s", wxDateTime::GetWeekDayName(wd));
+            break;
+
+        case wxCAL_HITTEST_DAY:
+            msg += wxString::Format("over %s", dt.FormatISODate());
+            break;
+
+        case wxCAL_HITTEST_INCMONTH:
+            msg += "over next month button";
+            break;
+
+        case wxCAL_HITTEST_DECMONTH:
+            msg += "over previous month button";
+            break;
+
+        case wxCAL_HITTEST_SURROUNDING_WEEK:
+            msg += "over a day from another month";
+            break;
+    }
+
+    wxLogMessage("%s", msg);
+}
+
 #if wxUSE_DATEPICKCTRL
 
 void MyFrame::OnUpdateUIStartWithNone(wxUpdateUIEvent& event)
@@ -581,13 +637,8 @@ MyPanel::MyPanel(wxWindow *parent)
     date.Printf(wxT("Selected date: %s"),
                 wxDateTime::Today().FormatISODate().c_str());
     m_date = new wxStaticText(this, wxID_ANY, date);
-    m_calendar = new wxCalendarCtrl(this, Calendar_CalCtrl,
-                                    wxDefaultDateTime,
-                                    wxDefaultPosition,
-                                    wxDefaultSize,
-                                    wxCAL_MONDAY_FIRST |
-                                    wxCAL_SHOW_HOLIDAYS |
-                                    wxRAISED_BORDER);
+    m_calendar = DoCreateCalendar(wxDefaultDateTime,
+                                  wxCAL_MONDAY_FIRST | wxCAL_SHOW_HOLIDAYS);
 
     // adjust to vertical/horizontal display, check mostly dedicated to WinCE
     bool horizontal = ( wxSystemSettings::GetMetric(wxSYS_SCREEN_X) > wxSystemSettings::GetMetric(wxSYS_SCREEN_Y) );
@@ -602,9 +653,15 @@ MyPanel::MyPanel(wxWindow *parent)
 
 void MyPanel::OnCalendar(wxCalendarEvent& event)
 {
-    m_calendar->Mark(event.GetDate().GetDay(), true);
-    wxLogMessage(wxT("Selected (and marked) %s from calendar."),
-                 event.GetDate().FormatISODate().c_str());
+    // clicking the same date twice unmarks it (convenient for testing)
+    static wxDateTime s_dateLast;
+    const bool mark = !s_dateLast.IsValid() || event.GetDate() != s_dateLast;
+
+    s_dateLast = event.GetDate();
+
+    m_calendar->Mark(event.GetDate().GetDay(), mark);
+    wxLogMessage(wxT("Selected (and %smarked) %s from calendar."),
+                 mark ? "" : "un", s_dateLast.FormatISODate().c_str());
 }
 
 void MyPanel::OnCalendarChange(wxCalendarEvent& event)
@@ -629,24 +686,41 @@ void MyPanel::OnCalendarWeekDayClick(wxCalendarEvent& event)
                  wxDateTime::GetWeekDayName(event.GetWeekDay()).c_str());
 }
 
-void MyPanel::RecreateCalendar(long style)
+void MyPanel::OnCalendarWeekClick(wxCalendarEvent& event)
+{
+    wxLogMessage(wxT("Clicked on week %d"), event.GetDate().GetWeekOfYear());
+}
+
+wxCalendarCtrlBase *MyPanel::DoCreateCalendar(const wxDateTime& dt, long style)
 {
     wxCalendarCtrlBase *calendar;
 #ifdef wxHAS_NATIVE_CALENDARCTRL
     if ( m_usingGeneric )
         calendar = new wxGenericCalendarCtrl(this, Calendar_CalCtrl,
-                                             m_calendar->GetDate(),
+                                             dt,
                                              wxDefaultPosition,
                                              wxDefaultSize,
                                              style);
     else
 #endif // wxHAS_NATIVE_CALENDARCTRL
         calendar = new wxCalendarCtrl(this, Calendar_CalCtrl,
-                                      m_calendar->GetDate(),
+                                      dt,
                                       wxDefaultPosition,
                                       wxDefaultSize,
                                       style);
 
+    calendar->Connect(wxEVT_RIGHT_DOWN,
+                      wxMouseEventHandler(MyFrame::OnCalRClick),
+                      NULL,
+                      ( MyFrame * )wxGetTopLevelParent(this));
+
+    return calendar;
+}
+
+void MyPanel::RecreateCalendar(long style)
+{
+    wxCalendarCtrlBase *calendar = DoCreateCalendar(m_calendar->GetDate(), style);
+
     m_sizer->Replace(m_calendar, calendar);
     delete m_calendar;
     m_calendar = calendar;
@@ -662,7 +736,8 @@ void MyPanel::ToggleCalStyle(bool on, int flag)
     else
         style &= ~flag;
 
-    if ( flag == wxCAL_SEQUENTIAL_MONTH_SELECTION )
+    if ( flag == wxCAL_SEQUENTIAL_MONTH_SELECTION
+        || flag == wxCAL_SHOW_WEEK_NUMBERS)
     {
         // changing this style requires recreating the control
         RecreateCalendar(style);