+// 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();
+}
+
+// ----------------------------------------------------------------------------
+// MyDateDialog
+// ----------------------------------------------------------------------------
+
+#if wxUSE_DATEPICKCTRL
+
+BEGIN_EVENT_TABLE(MyDateDialog, wxDialog)
+ EVT_DATE_CHANGED(wxID_ANY, MyDateDialog::OnDateChange)
+END_EVENT_TABLE()
+
+MyDateDialog::MyDateDialog(wxWindow *parent, const wxDateTime& dt, int dtpStyle)
+ : wxDialog(parent, wxID_ANY, wxString(wxT("Calendar: Choose a date")))
+{
+#if wxUSE_DATEPICKCTRL_GENERIC
+ wxFrame *frame = (wxFrame *)wxGetTopLevelParent(parent);
+ if ( frame && frame->GetMenuBar()->IsChecked(Calendar_DatePicker_Generic) )
+ m_datePicker = new wxDatePickerCtrlGeneric(this, wxID_ANY, dt,
+ wxDefaultPosition,
+ wxDefaultSize,
+ dtpStyle);
+ else
+#endif // wxUSE_DATEPICKCTRL_GENERIC
+ m_datePicker = new wxDatePickerCtrl(this, wxID_ANY, dt,
+ wxDefaultPosition, wxDefaultSize,
+ dtpStyle);
+ m_datePicker->SetRange(wxDateTime(1, wxDateTime::Jan, 1900),
+ wxDefaultDateTime);
+ m_dateText = new wxStaticText(this, wxID_ANY, wxString());
+
+ const wxSizerFlags flags = wxSizerFlags().Centre().Border();
+ wxFlexGridSizer* const sizerMain = new wxFlexGridSizer(2);
+ sizerMain->Add(new wxStaticText(this, wxID_ANY, "Enter &date:"), flags);
+ sizerMain->Add(m_datePicker, flags);
+
+ sizerMain->Add(new wxStaticText(this, wxID_ANY, "Date in ISO format:"),
+ flags);
+ sizerMain->Add(m_dateText, flags);
+
+ wxStdDialogButtonSizer *sizerBtns = new wxStdDialogButtonSizer;
+ sizerBtns->AddButton(new wxButton(this, wxID_OK));
+ sizerBtns->AddButton(new wxButton(this, wxID_CANCEL));
+ sizerBtns->Realize();
+
+ wxSizer *sizerTop = new wxBoxSizer(wxVERTICAL);
+ sizerTop->Add(sizerMain, flags);
+ sizerTop->Add(sizerBtns, flags);
+
+ SetSizerAndFit(sizerTop);
+}
+
+void MyDateDialog::OnDateChange(wxDateEvent& event)
+{
+ const wxDateTime dt = event.GetDate();
+ if ( dt.IsValid() )
+ m_dateText->SetLabel(dt.FormatISODate());
+ else
+ m_dateText->SetLabel(wxEmptyString);
+}
+
+#endif // wxUSE_DATEPICKCTRL
+
+// ----------------------------------------------------------------------------
+// MyTimeDialog
+// ----------------------------------------------------------------------------
+
+#if wxUSE_TIMEPICKCTRL
+
+BEGIN_EVENT_TABLE(MyTimeDialog, wxDialog)
+ EVT_TIME_CHANGED(wxID_ANY, MyTimeDialog::OnTimeChange)
+END_EVENT_TABLE()
+
+MyTimeDialog::MyTimeDialog(wxWindow *parent)
+ : wxDialog(parent, wxID_ANY, wxString(wxT("Calendar: Choose time")))
+{
+#if wxUSE_TIMEPICKCTRL_GENERIC
+ wxFrame *frame = (wxFrame *)wxGetTopLevelParent(parent);
+ if ( frame && frame->GetMenuBar()->IsChecked(Calendar_TimePicker_Generic) )
+ m_timePicker = new wxTimePickerCtrlGeneric(this, wxID_ANY);
+ else
+#endif // wxUSE_TIMEPICKCTRL_GENERIC
+ m_timePicker = new wxTimePickerCtrl(this, wxID_ANY);
+ m_timeText = new wxStaticText(this, wxID_ANY, wxString());
+
+ const wxSizerFlags flags = wxSizerFlags().Centre().Border();
+ wxFlexGridSizer* const sizerMain = new wxFlexGridSizer(2);
+ sizerMain->Add(new wxStaticText(this, wxID_ANY, "Enter &time:"), flags);
+ sizerMain->Add(m_timePicker, flags);
+
+ sizerMain->Add(new wxStaticText(this, wxID_ANY, "Time in ISO format:"),
+ flags);
+ sizerMain->Add(m_timeText, flags);
+
+ wxStdDialogButtonSizer* sizerBtns = new wxStdDialogButtonSizer;
+ sizerBtns->AddButton(new wxButton(this, wxID_OK));
+ sizerBtns->AddButton(new wxButton(this, wxID_CANCEL));
+ sizerBtns->Realize();
+
+ wxSizer* sizerTop = new wxBoxSizer(wxVERTICAL);
+ sizerTop->Add(sizerMain, flags);
+ sizerTop->Add(sizerBtns, flags);
+
+ SetSizerAndFit(sizerTop);
+}
+
+void MyTimeDialog::OnTimeChange(wxDateEvent& event)
+{
+ m_timeText->SetLabel(event.GetDate().FormatISOTime());
+}
+
+#endif // wxUSE_TIMEPICKCTRL