]> git.saurik.com Git - wxWidgets.git/blobdiff - samples/calendar/calendar.cpp
Don't use a destroy event handler to disconnect the native control's delegate as...
[wxWidgets.git] / samples / calendar / calendar.cpp
index ffa07b257c12f86cbae90f072082a8a248afae79..a6c6007c51f0ae800aaead0ff99a736936fe0728 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);
 
@@ -97,6 +98,7 @@ public:
     }
 
     void HighlightSpecial(bool on);
+    void LimitDateRange(bool on);
 
     wxDateTime GetDate() const { return m_calendar->GetDate(); }
     void SetDate(const wxDateTime& dt) { m_calendar->SetDate(dt); }
@@ -146,7 +148,7 @@ public:
     void OnCalSpecial(wxCommandEvent& event);
 
     void OnCalAllowMonth(wxCommandEvent& event);
-
+    void OnCalLimitDates(wxCommandEvent& event);
     void OnCalSeqMonth(wxCommandEvent& event);
     void OnCalShowSurroundingWeeks(wxCommandEvent& event);
     void OnCalShowWeekNumbers(wxCommandEvent& event);
@@ -211,6 +213,7 @@ enum
     Calendar_Cal_Holidays,
     Calendar_Cal_Special,
     Calendar_Cal_Month,
+    Calendar_Cal_LimitDates,
     Calendar_Cal_SeqMonth,
     Calendar_Cal_SurroundWeeks,
     Calendar_Cal_WeekNumbers,
@@ -260,6 +263,8 @@ BEGIN_EVENT_TABLE(MyFrame, wxFrame)
 
     EVT_MENU(Calendar_Cal_Month, MyFrame::OnCalAllowMonth)
 
+    EVT_MENU(Calendar_Cal_LimitDates, MyFrame::OnCalLimitDates)
+
     EVT_MENU(Calendar_Cal_SeqMonth, MyFrame::OnCalSeqMonth)
     EVT_MENU(Calendar_Cal_SurroundWeeks, MyFrame::OnCalShowSurroundingWeeks)
     EVT_MENU(Calendar_Cal_WeekNumbers, MyFrame::OnCalShowWeekNumbers)
@@ -275,6 +280,7 @@ BEGIN_EVENT_TABLE(MyFrame, wxFrame)
 #ifdef __WXGTK20__
     EVT_UPDATE_UI(Calendar_Cal_Monday, MyFrame::OnUpdateUIGenericOnly)
     EVT_UPDATE_UI(Calendar_Cal_Holidays, MyFrame::OnUpdateUIGenericOnly)
+    EVT_UPDATE_UI(Calendar_Cal_LimitDates, MyFrame::OnUpdateUIGenericOnly)
 #endif
     EVT_UPDATE_UI(Calendar_Cal_Special, MyFrame::OnUpdateUIGenericOnly)
     EVT_UPDATE_UI(Calendar_Cal_SurroundWeeks, MyFrame::OnUpdateUIGenericOnly)
@@ -285,6 +291,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
@@ -317,11 +324,11 @@ bool MyApp::OnInit()
         return false;
 
     // Create the main application window
-    MyFrame *frame = new MyFrame(_T("Calendar wxWidgets sample")
+    MyFrame *frame = new MyFrame(wxT("Calendar wxWidgets sample")
 #ifndef __WXWINCE__
                                  ,wxPoint(50, 50), wxSize(450, 340)
 #endif
-                                                                );
+                                 );
 
     frame->Show(true);
 
@@ -344,11 +351,11 @@ MyFrame::MyFrame(const wxString& title, const wxPoint& pos, const wxSize& size)
 
     // create a menu bar
     wxMenu *menuFile = new wxMenu;
-    menuFile->Append(Calendar_File_About, _T("&About...\tCtrl-A"), _T("Show about dialog"));
+    menuFile->Append(Calendar_File_About, wxT("&About...\tCtrl-A"), wxT("Show about dialog"));
     menuFile->AppendSeparator();
-    menuFile->Append(Calendar_File_ClearLog, _T("&Clear log\tCtrl-L"));
+    menuFile->Append(Calendar_File_ClearLog, wxT("&Clear log\tCtrl-L"));
     menuFile->AppendSeparator();
-    menuFile->Append(Calendar_File_Quit, _T("E&xit\tAlt-X"), _T("Quit this program"));
+    menuFile->Append(Calendar_File_Quit, wxT("E&xit\tAlt-X"), wxT("Quit this program"));
 
     wxMenu *menuCal = new wxMenu;
 #ifdef wxHAS_NATIVE_CALENDARCTRL
@@ -357,67 +364,70 @@ MyFrame::MyFrame(const wxString& title, const wxPoint& pos, const wxSize& size)
     menuCal->AppendSeparator();
 #endif // wxHAS_NATIVE_CALENDARCTRL
     menuCal->Append(Calendar_Cal_Monday,
-                    _T("Monday &first weekday\tCtrl-F"),
-                    _T("Toggle between Mon and Sun as the first week day"),
+                    wxT("Monday &first weekday\tCtrl-F"),
+                    wxT("Toggle between Mon and Sun as the first week day"),
                     true);
-    menuCal->Append(Calendar_Cal_Holidays, _T("Show &holidays\tCtrl-H"),
-                    _T("Toggle highlighting the holidays"),
+    menuCal->Append(Calendar_Cal_Holidays, wxT("Show &holidays\tCtrl-H"),
+                    wxT("Toggle highlighting the holidays"),
                     true);
-    menuCal->Append(Calendar_Cal_Special, _T("Highlight &special dates\tCtrl-S"),
-                    _T("Test custom highlighting"),
+    menuCal->Append(Calendar_Cal_Special, wxT("Highlight &special dates\tCtrl-S"),
+                    wxT("Test custom highlighting"),
                     true);
     menuCal->Append(Calendar_Cal_SurroundWeeks,
-                    _T("Show s&urrounding weeks\tCtrl-W"),
-                    _T("Show the neighbouring weeks in the prev/next month"),
+                    wxT("Show s&urrounding weeks\tCtrl-W"),
+                    wxT("Show the neighbouring weeks in the prev/next month"),
                     true);
     menuCal->Append(Calendar_Cal_WeekNumbers,
-                    _T("Show &week numbers"),
-                    _T("Toggle week numbers"),
+                    wxT("Show &week numbers"),
+                    wxT("Toggle week numbers"),
                     true);
     menuCal->AppendSeparator();
     menuCal->Append(Calendar_Cal_SeqMonth,
-                    _T("Toggle month selector st&yle\tCtrl-Y"),
-                    _T("Use another style for the calendar controls"),
+                    wxT("Toggle month selector st&yle\tCtrl-Y"),
+                    wxT("Use another style for the calendar controls"),
                     true);
-    menuCal->Append(Calendar_Cal_Month, _T("&Month can be changed\tCtrl-M"),
-                    _T("Allow changing the month in the calendar"),
+    menuCal->Append(Calendar_Cal_Month, wxT("&Month can be changed\tCtrl-M"),
+                    wxT("Allow changing the month in the calendar"),
                     true);
+    menuCal->AppendCheckItem(Calendar_Cal_LimitDates, wxT("Toggle date ra&nge\tCtrl-N"),
+                    wxT("Limit the valid dates"));
     menuCal->AppendSeparator();
-    menuCal->Append(Calendar_Cal_SetDate, _T("Call &SetDate(2005-12-24)"), _T("Set date to 2005-12-24."));
-    menuCal->Append(Calendar_Cal_Today, _T("Call &Today()"), _T("Set to the current date."));
+    menuCal->Append(Calendar_Cal_SetDate, wxT("Call &SetDate(2005-12-24)"), wxT("Set date to 2005-12-24."));
+    menuCal->Append(Calendar_Cal_Today, wxT("Call &Today()"), wxT("Set to the current date."));
     menuCal->Append(Calendar_Cal_BeginDST, "Call SetDate(GetBeginDST())");
     menuCal->AppendSeparator();
-    menuCal->AppendCheckItem(Calendar_Cal_Resizable, _T("Make &resizable\tCtrl-R"));
+    menuCal->AppendCheckItem(Calendar_Cal_Resizable, wxT("Make &resizable\tCtrl-R"));
 
 #if wxUSE_DATEPICKCTRL
     wxMenu *menuDate = new wxMenu;
     menuDate->AppendCheckItem(Calendar_DatePicker_ShowCentury,
-                              _T("Al&ways show century"));
+                              wxT("Al&ways show century"));
     menuDate->AppendCheckItem(Calendar_DatePicker_DropDown,
-                              _T("Use &drop down control"));
+                              wxT("Use &drop down control"));
     menuDate->AppendCheckItem(Calendar_DatePicker_AllowNone,
-                              _T("Allow &no date"));
+                              wxT("Allow &no date"));
     menuDate->AppendCheckItem(Calendar_DatePicker_StartWithNone,
-                              _T("Start &with no date"));
+                              wxT("Start &with no date"));
 #if wxUSE_DATEPICKCTRL_GENERIC
     menuDate->AppendCheckItem(Calendar_DatePicker_Generic,
-                              _T("Use &generic version of the control"));
+                              wxT("Use &generic version of the control"));
 #endif // wxUSE_DATEPICKCTRL_GENERIC
     menuDate->AppendSeparator();
-    menuDate->Append(Calendar_DatePicker_AskDate, _T("&Choose date...\tCtrl-D"), _T("Show dialog with wxDatePickerCtrl"));
+    menuDate->Append(Calendar_DatePicker_AskDate, wxT("&Choose date...\tCtrl-D"), wxT("Show dialog with wxDatePickerCtrl"));
 #endif // wxUSE_DATEPICKCTRL
 
     // now append the freshly created menu to the menu bar...
     wxMenuBar *menuBar = new wxMenuBar;
-    menuBar->Append(menuFile, _T("&File"));
-    menuBar->Append(menuCal, _T("&Calendar"));
+    menuBar->Append(menuFile, wxT("&File"));
+    menuBar->Append(menuCal, wxT("&Calendar"));
 #if wxUSE_DATEPICKCTRL
-    menuBar->Append(menuDate, _T("&Date picker"));
+    menuBar->Append(menuDate, wxT("&Date picker"));
 #endif // wxUSE_DATEPICKCTRL
 
     menuBar->Check(Calendar_Cal_Monday, true);
     menuBar->Check(Calendar_Cal_Holidays, true);
     menuBar->Check(Calendar_Cal_Month, true);
+    menuBar->Check(Calendar_Cal_LimitDates, false);
 
 #if wxUSE_DATEPICKCTRL
     menuBar->Check(Calendar_DatePicker_ShowCentury, true);
@@ -446,8 +456,8 @@ void MyFrame::OnQuit(wxCommandEvent& WXUNUSED(event))
 
 void MyFrame::OnAbout(wxCommandEvent& WXUNUSED(event))
 {
-    wxMessageBox(_T("wxCalendarCtrl sample\n(c) 2000--2008 Vadim Zeitlin"),
-                 _T("About Calendar"), wxOK | wxICON_INFORMATION, this);
+    wxMessageBox(wxT("wxCalendarCtrl sample\n(c) 2000--2008 Vadim Zeitlin"),
+                 wxT("About Calendar"), wxOK | wxICON_INFORMATION, this);
 }
 
 void MyFrame::OnClearLog(wxCommandEvent& WXUNUSED(event))
@@ -470,6 +480,11 @@ void MyFrame::OnCalSpecial(wxCommandEvent& event)
     m_panel->HighlightSpecial(GetMenuBar()->IsChecked(event.GetId()));
 }
 
+void MyFrame::OnCalLimitDates(wxCommandEvent& event)
+{
+    m_panel->LimitDateRange(GetMenuBar()->IsChecked(event.GetId()));
+}
+
 void MyFrame::OnCalAllowMonth(wxCommandEvent& event)
 {
     m_panel->GetCal()->EnableMonthChange(event.IsChecked());
@@ -602,16 +617,16 @@ void MyFrame::OnAskDate(wxCommandEvent& WXUNUSED(event))
             if ( dt.GetDay() == today.GetDay() &&
                     dt.GetMonth() == today.GetMonth() )
             {
-                wxMessageBox(_T("Happy birthday!"), _T("Calendar Sample"));
+                wxMessageBox(wxT("Happy birthday!"), wxT("Calendar Sample"));
             }
 
             m_panel->SetDate(dt);
 
-            wxLogStatus(_T("Changed the date to your input"));
+            wxLogStatus(wxT("Changed the date to your input"));
         }
         else
         {
-            wxLogStatus(_T("No date entered"));
+            wxLogStatus(wxT("No date entered"));
         }
     }
 }
@@ -684,6 +699,11 @@ void MyPanel::OnCalendarWeekDayClick(wxCalendarEvent& event)
                  wxDateTime::GetWeekDayName(event.GetWeekDay()).c_str());
 }
 
+void MyPanel::OnCalendarWeekClick(wxCalendarEvent& event)
+{
+    wxLogMessage(wxT("Clicked on week %d"), event.GetDate().GetWeekOfYear());
+}
+
 wxCalendarCtrlBase *MyPanel::DoCreateCalendar(const wxDateTime& dt, long style)
 {
     wxCalendarCtrlBase *calendar;
@@ -705,7 +725,7 @@ wxCalendarCtrlBase *MyPanel::DoCreateCalendar(const wxDateTime& dt, long style)
     calendar->Connect(wxEVT_RIGHT_DOWN,
                       wxMouseEventHandler(MyFrame::OnCalRClick),
                       NULL,
-                      wxGetTopLevelParent(this));
+                      ( MyFrame * )wxGetTopLevelParent(this));
 
     return calendar;
 }
@@ -729,7 +749,7 @@ 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
@@ -766,6 +786,52 @@ void MyPanel::HighlightSpecial(bool on)
     m_calendar->Refresh();
 }
 
+// Toggle a restricted date range to the six months centered on today's date.
+void MyPanel::LimitDateRange(bool on)
+{
+    if ( on )
+    {
+        // limit the choice of date to 3 months around today
+        const wxDateSpan diff = wxDateSpan::Months(3);
+        const wxDateTime today = wxDateTime::Today();
+
+        // Set the restricted date range.
+        if ( m_calendar->SetDateRange(today - diff, today + diff) )
+        {
+            wxLogStatus("Date range limited to 3 months around today.");
+            wxDateTime firstValidDate;
+            wxDateTime lastValidDate;
+            if ( m_calendar->GetDateRange(&firstValidDate, &lastValidDate) )
+            {
+                wxLogMessage("First valid date: %s, last valid date: %s",
+                             firstValidDate.FormatISODate(),
+                             lastValidDate.FormatISODate());
+            }
+            else
+            {
+                wxLogWarning("Failed to get back the valid dates range.");
+            }
+        }
+        else
+        {
+            wxLogWarning("Date range not supported.");
+        }
+    }
+    else // off
+    {
+        // Remove the date restrictions.
+        if ( m_calendar->SetDateRange() )
+        {
+            wxLogStatus("Date choice is unlimited now.");
+        }
+        else
+        {
+            wxLogWarning("Date range not supported.");
+        }
+    }
+
+    m_calendar->Refresh();
+}
 
 // ----------------------------------------------------------------------------
 // MyDialog
@@ -774,7 +840,7 @@ void MyPanel::HighlightSpecial(bool on)
 #if wxUSE_DATEPICKCTRL
 
 MyDialog::MyDialog(wxWindow *parent, const wxDateTime& dt, int dtpStyle)
-        : wxDialog(parent, wxID_ANY, wxString(_T("Calendar: Choose a date")))
+        : wxDialog(parent, wxID_ANY, wxString(wxT("Calendar: Choose a date")))
 {
     wxStdDialogButtonSizer *sizerBtns = new wxStdDialogButtonSizer;
     sizerBtns->AddButton(new wxButton(this, wxID_OK));
@@ -782,7 +848,7 @@ MyDialog::MyDialog(wxWindow *parent, const wxDateTime& dt, int dtpStyle)
     sizerBtns->Realize();
 
     wxSizer *sizerText = new wxBoxSizer(wxHORIZONTAL);
-    sizerText->Add(new wxStaticText(this, wxID_ANY, _T("Date in ISO format: ")),
+    sizerText->Add(new wxStaticText(this, wxID_ANY, wxT("Date in ISO format: ")),
                     wxSizerFlags().Border().Align(wxALIGN_CENTRE_VERTICAL));
     m_text = new wxTextCtrl(this, wxID_ANY);
     sizerText->Add(m_text, wxSizerFlags().
@@ -792,7 +858,7 @@ MyDialog::MyDialog(wxWindow *parent, const wxDateTime& dt, int dtpStyle)
     sizerTop->Add(new wxStaticText
                       (
                         this, wxID_ANY,
-                        _T("Enter your birthday date (not before 20th century):")
+                        wxT("Enter your birthday date (not before 20th century):")
                       ),
                     wxSizerFlags().Border());