X-Git-Url: https://git.saurik.com/wxWidgets.git/blobdiff_plain/bf956fac0e89eab569c41a9f121e728958767de7..5b99d5d8eae1091046d3fd475c48fa43073e03b3:/samples/calendar/calendar.cpp?ds=inline diff --git a/samples/calendar/calendar.cpp b/samples/calendar/calendar.cpp index 42714c2345..586a955659 100644 --- a/samples/calendar/calendar.cpp +++ b/samples/calendar/calendar.cpp @@ -102,6 +102,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; @@ -159,6 +161,8 @@ public: event.Enable(m_panel->IsUsingGeneric()); } + void OnCalRClick(wxMouseEvent& event); + private: MyPanel *m_panel; wxTextCtrl *m_logWindow; @@ -265,7 +269,9 @@ BEGIN_EVENT_TABLE(MyFrame, wxFrame) EVT_UPDATE_UI(Calendar_Cal_SeqMonth, MyFrame::OnUpdateUIGenericOnly) +#ifdef __WXGTK20__ EVT_UPDATE_UI(Calendar_Cal_Monday, MyFrame::OnUpdateUIGenericOnly) +#endif EVT_UPDATE_UI(Calendar_Cal_Holidays, MyFrame::OnUpdateUIGenericOnly) EVT_UPDATE_UI(Calendar_Cal_Special, MyFrame::OnUpdateUIGenericOnly) EVT_UPDATE_UI(Calendar_Cal_SurroundWeeks, MyFrame::OnUpdateUIGenericOnly) @@ -506,6 +512,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) @@ -575,13 +623,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) ); @@ -596,9 +639,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) @@ -623,24 +672,36 @@ void MyPanel::OnCalendarWeekDayClick(wxCalendarEvent& event) wxDateTime::GetWeekDayName(event.GetWeekDay()).c_str()); } -void MyPanel::RecreateCalendar(long style) +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, + 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;