1 /////////////////////////////////////////////////////////////////////////////
3 // Purpose: wxCalendarCtrl sample
4 // Author: Vadim Zeitlin
7 // Copyright: (c) Vadim Zeitlin
8 // Licence: wxWindows licence
9 /////////////////////////////////////////////////////////////////////////////
11 // ============================================================================
13 // ============================================================================
15 // ----------------------------------------------------------------------------
17 // ----------------------------------------------------------------------------
19 // For compilers that support precompilation, includes "wx/wx.h".
20 #include "wx/wxprec.h"
26 // for all others, include the necessary headers
32 #include "wx/stattext.h"
34 #include "wx/layout.h"
35 #include "wx/msgdlg.h"
37 #include "wx/button.h"
39 #include "wx/textctrl.h"
40 #include "wx/settings.h"
43 #include "wx/calctrl.h"
44 #include "wx/splitter.h"
46 #if wxUSE_DATEPICKCTRL
47 #include "wx/datectrl.h"
48 #if wxUSE_DATEPICKCTRL_GENERIC
49 #include "wx/generic/datectrl.h"
50 #endif // wxUSE_DATEPICKCTRL_GENERIC
51 #endif // wxUSE_DATEPICKCTRL
53 #if wxUSE_TIMEPICKCTRL
54 #include "wx/timectrl.h"
55 #if wxUSE_TIMEPICKCTRL_GENERIC
56 #include "wx/generic/timectrl.h"
57 #endif // wxUSE_TIMEPICKCTRL_GENERIC
58 #endif // wxUSE_TIMEPICKCTRL
60 #include "../sample.xpm"
62 #ifdef wxHAS_NATIVE_CALENDARCTRL
63 #include "wx/generic/calctrlg.h"
66 // ----------------------------------------------------------------------------
68 // ----------------------------------------------------------------------------
70 // Define a new application type, each program should derive a class from wxApp
71 class MyApp
: public wxApp
74 // override base class virtuals
75 // ----------------------------
77 // this one is called on application startup and is a good place for the app
78 // initialization (doing it here and not in the ctor allows to have an error
79 // return: if OnInit() returns false, the application terminates)
80 virtual bool OnInit();
83 class MyPanel
: public wxPanel
86 MyPanel(wxWindow
*parent
);
88 void OnCalendar(wxCalendarEvent
& event
);
89 void OnCalendarWeekDayClick(wxCalendarEvent
& event
);
90 void OnCalendarWeekClick(wxCalendarEvent
& event
);
91 void OnCalendarChange(wxCalendarEvent
& event
);
92 void OnCalMonthChange(wxCalendarEvent
& event
);
94 wxCalendarCtrlBase
*GetCal() const { return m_calendar
; }
96 // turn on/off the specified style bit on the calendar control
97 void ToggleCalStyle(bool on
, int style
);
99 bool IsUsingGeneric() const { return m_usingGeneric
; }
100 void ToggleUseGeneric()
102 m_usingGeneric
= !m_usingGeneric
;
103 RecreateCalendar(m_calendar
->GetWindowStyle());
106 void HighlightSpecial(bool on
);
107 void LimitDateRange(bool on
);
109 wxDateTime
GetDate() const { return m_calendar
->GetDate(); }
110 void SetDate(const wxDateTime
& dt
) { m_calendar
->SetDate(dt
); }
113 wxCalendarCtrlBase
*DoCreateCalendar(const wxDateTime
& dt
, long style
);
115 void RecreateCalendar(long style
);
117 wxCalendarCtrlBase
*m_calendar
;
118 wxStaticText
*m_date
;
124 DECLARE_EVENT_TABLE()
127 // Define a new frame type: this is going to be our main frame
128 class MyFrame
: public wxFrame
132 MyFrame(const wxString
& title
, const wxPoint
& pos
= wxDefaultPosition
, const wxSize
& size
= wxDefaultSize
);
134 // event handlers (these functions should _not_ be virtual)
135 void OnAbout(wxCommandEvent
& event
);
136 void OnClearLog(wxCommandEvent
& event
);
137 void OnQuit(wxCommandEvent
& event
);
139 #if wxUSE_DATEPICKCTRL
140 void OnAskDate(wxCommandEvent
& event
);
142 void OnUpdateUIStartWithNone(wxUpdateUIEvent
& event
);
143 #endif // wxUSE_DATEPICKCTRL
145 #if wxUSE_TIMEPICKCTRL
146 void OnAskTime(wxCommandEvent
& event
);
147 #endif // wxUSE_TIMEPICKCTRL
149 #ifdef wxHAS_NATIVE_CALENDARCTRL
150 void OnCalGeneric(wxCommandEvent
& WXUNUSED(event
))
152 m_panel
->ToggleUseGeneric();
154 #endif // wxHAS_NATIVE_CALENDARCTRL
156 void OnCalMonday(wxCommandEvent
& event
);
157 void OnCalHolidays(wxCommandEvent
& event
);
158 void OnCalSpecial(wxCommandEvent
& event
);
160 void OnCalAllowMonth(wxCommandEvent
& event
);
161 void OnCalLimitDates(wxCommandEvent
& event
);
162 void OnCalSeqMonth(wxCommandEvent
& event
);
163 void OnCalShowSurroundingWeeks(wxCommandEvent
& event
);
164 void OnCalShowWeekNumbers(wxCommandEvent
& event
);
166 void OnSetDate(wxCommandEvent
& event
);
167 void OnToday(wxCommandEvent
& event
);
168 void OnBeginDST(wxCommandEvent
& event
);
170 void OnCalToggleResizable(wxCommandEvent
& event
);
172 void OnUpdateUIGenericOnly(wxUpdateUIEvent
& event
)
174 event
.Enable(m_panel
->IsUsingGeneric());
177 void OnCalRClick(wxMouseEvent
& event
);
181 wxTextCtrl
*m_logWindow
;
183 // any class wishing to process wxWidgets events must use this macro
184 DECLARE_EVENT_TABLE()
187 #if wxUSE_DATEPICKCTRL
189 // Define a simple modal dialog which asks the user for a date
190 class MyDateDialog
: public wxDialog
193 MyDateDialog(wxWindow
*parent
, const wxDateTime
& dt
, int dtpStyle
);
195 wxDateTime
GetDate() const { return m_datePicker
->GetValue(); }
198 void OnDateChange(wxDateEvent
& event
);
201 wxDatePickerCtrlBase
*m_datePicker
;
202 wxStaticText
*m_dateText
;
205 DECLARE_EVENT_TABLE()
208 #endif // wxUSE_DATEPICKCTRL
210 #if wxUSE_TIMEPICKCTRL
212 // Another simple dialog, this one asking for time.
213 class MyTimeDialog
: public wxDialog
216 MyTimeDialog(wxWindow
* parent
);
218 wxDateTime
GetTime() const { return m_timePicker
->GetValue(); }
221 void OnTimeChange(wxDateEvent
& event
);
223 wxTimePickerCtrlBase
* m_timePicker
;
224 wxStaticText
* m_timeText
;
226 wxDECLARE_EVENT_TABLE();
229 #endif // wxUSE_TIMEPICKCTRL
231 // ----------------------------------------------------------------------------
233 // ----------------------------------------------------------------------------
235 // IDs for the controls and the menu commands
239 Calendar_File_About
= wxID_ABOUT
,
240 Calendar_File_ClearLog
= wxID_CLEAR
,
241 Calendar_File_Quit
= wxID_EXIT
,
242 Calendar_Cal_Generic
= 200,
244 Calendar_Cal_Holidays
,
245 Calendar_Cal_Special
,
247 Calendar_Cal_LimitDates
,
248 Calendar_Cal_SeqMonth
,
249 Calendar_Cal_SurroundWeeks
,
250 Calendar_Cal_WeekNumbers
,
251 Calendar_Cal_SetDate
,
253 Calendar_Cal_BeginDST
,
254 Calendar_Cal_Resizable
,
255 #if wxUSE_DATEPICKCTRL
256 Calendar_DatePicker_AskDate
= 300,
257 Calendar_DatePicker_ShowCentury
,
258 Calendar_DatePicker_DropDown
,
259 Calendar_DatePicker_AllowNone
,
260 Calendar_DatePicker_StartWithNone
,
261 #if wxUSE_DATEPICKCTRL_GENERIC
262 Calendar_DatePicker_Generic
,
263 #endif // wxUSE_DATEPICKCTRL_GENERIC
264 #endif // wxUSE_DATEPICKCTRL
265 #if wxUSE_TIMEPICKCTRL
266 Calendar_TimePicker_AskTime
= 400,
267 #if wxUSE_TIMEPICKCTRL_GENERIC
268 Calendar_TimePicker_Generic
,
269 #endif // wxUSE_TIMEPICKCTRL_GENERIC
270 #endif // wxUSE_TIMEPICKCTRL
271 Calendar_CalCtrl
= 1000
274 // ----------------------------------------------------------------------------
275 // event tables and other macros for wxWidgets
276 // ----------------------------------------------------------------------------
278 // the event tables connect the wxWidgets events with the functions (event
279 // handlers) which process them. It can be also done at run-time, but for the
280 // simple menu events like this the static method is much simpler.
281 BEGIN_EVENT_TABLE(MyFrame
, wxFrame
)
282 EVT_MENU(Calendar_File_About
, MyFrame::OnAbout
)
283 EVT_MENU(Calendar_File_ClearLog
, MyFrame::OnClearLog
)
284 EVT_MENU(Calendar_File_Quit
, MyFrame::OnQuit
)
286 #if wxUSE_DATEPICKCTRL
287 EVT_MENU(Calendar_DatePicker_AskDate
, MyFrame::OnAskDate
)
289 EVT_UPDATE_UI(Calendar_DatePicker_StartWithNone
,
290 MyFrame::OnUpdateUIStartWithNone
)
291 #endif // wxUSE_DATEPICKCTRL
293 #if wxUSE_TIMEPICKCTRL
294 EVT_MENU(Calendar_TimePicker_AskTime
, MyFrame::OnAskTime
)
295 #endif // wxUSE_TIMEPICKCTRL
297 #ifdef wxHAS_NATIVE_CALENDARCTRL
298 EVT_MENU(Calendar_Cal_Generic
, MyFrame::OnCalGeneric
)
299 #endif // wxHAS_NATIVE_CALENDARCTRL
301 EVT_MENU(Calendar_Cal_Monday
, MyFrame::OnCalMonday
)
302 EVT_MENU(Calendar_Cal_Holidays
, MyFrame::OnCalHolidays
)
303 EVT_MENU(Calendar_Cal_Special
, MyFrame::OnCalSpecial
)
305 EVT_MENU(Calendar_Cal_Month
, MyFrame::OnCalAllowMonth
)
307 EVT_MENU(Calendar_Cal_LimitDates
, MyFrame::OnCalLimitDates
)
309 EVT_MENU(Calendar_Cal_SeqMonth
, MyFrame::OnCalSeqMonth
)
310 EVT_MENU(Calendar_Cal_SurroundWeeks
, MyFrame::OnCalShowSurroundingWeeks
)
311 EVT_MENU(Calendar_Cal_WeekNumbers
, MyFrame::OnCalShowWeekNumbers
)
313 EVT_MENU(Calendar_Cal_SetDate
, MyFrame::OnSetDate
)
314 EVT_MENU(Calendar_Cal_Today
, MyFrame::OnToday
)
315 EVT_MENU(Calendar_Cal_BeginDST
, MyFrame::OnBeginDST
)
317 EVT_MENU(Calendar_Cal_Resizable
, MyFrame::OnCalToggleResizable
)
320 EVT_UPDATE_UI(Calendar_Cal_SeqMonth
, MyFrame::OnUpdateUIGenericOnly
)
322 EVT_UPDATE_UI(Calendar_Cal_Monday
, MyFrame::OnUpdateUIGenericOnly
)
323 EVT_UPDATE_UI(Calendar_Cal_Holidays
, MyFrame::OnUpdateUIGenericOnly
)
325 EVT_UPDATE_UI(Calendar_Cal_Special
, MyFrame::OnUpdateUIGenericOnly
)
326 EVT_UPDATE_UI(Calendar_Cal_SurroundWeeks
, MyFrame::OnUpdateUIGenericOnly
)
329 BEGIN_EVENT_TABLE(MyPanel
, wxPanel
)
330 EVT_CALENDAR(Calendar_CalCtrl
, MyPanel::OnCalendar
)
331 EVT_CALENDAR_PAGE_CHANGED(Calendar_CalCtrl
, MyPanel::OnCalMonthChange
)
332 EVT_CALENDAR_SEL_CHANGED(Calendar_CalCtrl
, MyPanel::OnCalendarChange
)
333 EVT_CALENDAR_WEEKDAY_CLICKED(Calendar_CalCtrl
, MyPanel::OnCalendarWeekDayClick
)
334 EVT_CALENDAR_WEEK_CLICKED(Calendar_CalCtrl
, MyPanel::OnCalendarWeekClick
)
337 // Create a new application object: this macro will allow wxWidgets to create
338 // the application object during program execution (it's better than using a
339 // static object for many reasons) and also declares the accessor function
340 // wxGetApp() which will return the reference of the right type (i.e. MyApp and
344 // ============================================================================
346 // ============================================================================
348 // ----------------------------------------------------------------------------
349 // the application class
350 // ----------------------------------------------------------------------------
352 // `Main program' equivalent: the program execution "starts" here
355 if ( !wxApp::OnInit() )
358 // Create the main application window
359 MyFrame
*frame
= new MyFrame(wxT("Calendar wxWidgets sample")
361 ,wxPoint(50, 50), wxSize(450, 340)
367 // success: wxApp::OnRun() will be called which will enter the main message
368 // loop and the application will run. If we returned false here, the
369 // application would exit immediately.
373 // ----------------------------------------------------------------------------
375 // ----------------------------------------------------------------------------
378 MyFrame::MyFrame(const wxString
& title
, const wxPoint
& pos
, const wxSize
& size
)
379 : wxFrame((wxFrame
*)NULL
, wxID_ANY
, title
, pos
, size
)
381 // set the frame icon
382 SetIcon(wxICON(sample
));
385 wxMenuBar
*menuBar
= new wxMenuBar
;
387 wxMenu
*menuFile
= new wxMenu
;
388 menuFile
->Append(Calendar_File_About
, wxT("&About\tCtrl-A"), wxT("Show about dialog"));
389 menuFile
->AppendSeparator();
390 menuFile
->Append(Calendar_File_ClearLog
, wxT("&Clear log\tCtrl-L"));
391 menuFile
->AppendSeparator();
392 menuFile
->Append(Calendar_File_Quit
, wxT("E&xit\tAlt-X"), wxT("Quit this program"));
393 menuBar
->Append(menuFile
, wxT("&File"));
395 wxMenu
*menuCal
= new wxMenu
;
396 #ifdef wxHAS_NATIVE_CALENDARCTRL
397 menuCal
->AppendCheckItem(Calendar_Cal_Generic
, "Use &generic version\tCtrl-G",
398 "Toggle between native and generic control");
399 menuCal
->AppendSeparator();
400 #endif // wxHAS_NATIVE_CALENDARCTRL
401 menuCal
->Append(Calendar_Cal_Monday
,
402 wxT("Monday &first weekday\tCtrl-F"),
403 wxT("Toggle between Mon and Sun as the first week day"),
405 menuCal
->Append(Calendar_Cal_Holidays
, wxT("Show &holidays\tCtrl-H"),
406 wxT("Toggle highlighting the holidays"),
408 menuCal
->Append(Calendar_Cal_Special
, wxT("Highlight &special dates\tCtrl-S"),
409 wxT("Test custom highlighting"),
411 menuCal
->Append(Calendar_Cal_SurroundWeeks
,
412 wxT("Show s&urrounding weeks\tCtrl-W"),
413 wxT("Show the neighbouring weeks in the prev/next month"),
415 menuCal
->Append(Calendar_Cal_WeekNumbers
,
416 wxT("Show &week numbers"),
417 wxT("Toggle week numbers"),
419 menuCal
->AppendSeparator();
420 menuCal
->Append(Calendar_Cal_SeqMonth
,
421 wxT("Toggle month selector st&yle\tCtrl-Y"),
422 wxT("Use another style for the calendar controls"),
424 menuCal
->Append(Calendar_Cal_Month
, wxT("&Month can be changed\tCtrl-M"),
425 wxT("Allow changing the month in the calendar"),
427 menuCal
->AppendCheckItem(Calendar_Cal_LimitDates
, wxT("Toggle date ra&nge\tCtrl-N"),
428 wxT("Limit the valid dates"));
429 menuCal
->AppendSeparator();
430 menuCal
->Append(Calendar_Cal_SetDate
, wxT("Call &SetDate(2005-12-24)"), wxT("Set date to 2005-12-24."));
431 menuCal
->Append(Calendar_Cal_Today
, wxT("Call &Today()"), wxT("Set to the current date."));
432 menuCal
->Append(Calendar_Cal_BeginDST
, "Call SetDate(GetBeginDST())");
433 menuCal
->AppendSeparator();
434 menuCal
->AppendCheckItem(Calendar_Cal_Resizable
, wxT("Make &resizable\tCtrl-R"));
435 menuBar
->Append(menuCal
, wxT("&Calendar"));
437 #if wxUSE_DATEPICKCTRL
438 wxMenu
*menuDate
= new wxMenu
;
439 menuDate
->AppendCheckItem(Calendar_DatePicker_ShowCentury
,
440 wxT("Al&ways show century"));
441 menuDate
->AppendCheckItem(Calendar_DatePicker_DropDown
,
442 wxT("Use &drop down control"));
443 menuDate
->AppendCheckItem(Calendar_DatePicker_AllowNone
,
444 wxT("Allow &no date"));
445 menuDate
->AppendCheckItem(Calendar_DatePicker_StartWithNone
,
446 wxT("Start &with no date"));
447 #if wxUSE_DATEPICKCTRL_GENERIC
448 menuDate
->AppendCheckItem(Calendar_DatePicker_Generic
,
449 wxT("Use &generic version of the control"));
450 #endif // wxUSE_DATEPICKCTRL_GENERIC
451 menuDate
->AppendSeparator();
452 menuDate
->Append(Calendar_DatePicker_AskDate
, wxT("&Choose date...\tCtrl-D"), wxT("Show dialog with wxDatePickerCtrl"));
453 menuBar
->Append(menuDate
, wxT("&Date picker"));
454 #endif // wxUSE_DATEPICKCTRL
456 #if wxUSE_TIMEPICKCTRL
457 wxMenu
*menuTime
= new wxMenu
;
458 #if wxUSE_TIMEPICKCTRL_GENERIC
459 menuTime
->AppendCheckItem(Calendar_TimePicker_Generic
,
460 wxT("Use &generic version of the control"));
461 menuTime
->AppendSeparator();
462 #endif // wxUSE_TIMEPICKCTRL_GENERIC
463 menuTime
->Append(Calendar_TimePicker_AskTime
, wxT("&Choose time...\tCtrl-T"), wxT("Show dialog with wxTimePickerCtrl"));
464 menuBar
->Append(menuTime
, wxT("&Time picker"));
465 #endif // wxUSE_TIMEPICKCTRL
467 menuBar
->Check(Calendar_Cal_Monday
, true);
468 menuBar
->Check(Calendar_Cal_Holidays
, true);
469 menuBar
->Check(Calendar_Cal_Month
, true);
470 menuBar
->Check(Calendar_Cal_LimitDates
, false);
472 #if wxUSE_DATEPICKCTRL
473 menuBar
->Check(Calendar_DatePicker_ShowCentury
, true);
474 #endif // wxUSE_DATEPICKCTRL
476 // ... and attach this menu bar to the frame
479 wxSplitterWindow
*splitter
= new wxSplitterWindow(this, wxID_ANY
,
480 wxDefaultPosition
, wxDefaultSize
,
482 m_panel
= new MyPanel(splitter
);
483 m_logWindow
= new wxTextCtrl(splitter
, wxID_ANY
, wxEmptyString
,
484 wxDefaultPosition
, wxDefaultSize
,
485 wxTE_READONLY
| wxTE_MULTILINE
);
486 splitter
->SplitHorizontally(m_panel
, m_logWindow
);
487 splitter
->SetMinimumPaneSize(20);
488 wxLog::SetActiveTarget(new wxLogTextCtrl(m_logWindow
));
491 void MyFrame::OnQuit(wxCommandEvent
& WXUNUSED(event
))
493 // true is to force the frame to close
497 void MyFrame::OnAbout(wxCommandEvent
& WXUNUSED(event
))
499 wxMessageBox(wxT("wxCalendarCtrl sample\n(c) 2000--2008 Vadim Zeitlin"),
500 wxT("About Calendar"), wxOK
| wxICON_INFORMATION
, this);
503 void MyFrame::OnClearLog(wxCommandEvent
& WXUNUSED(event
))
505 m_logWindow
->Clear();
508 void MyFrame::OnCalMonday(wxCommandEvent
& event
)
510 m_panel
->ToggleCalStyle(event
.IsChecked(), wxCAL_MONDAY_FIRST
);
513 void MyFrame::OnCalHolidays(wxCommandEvent
& event
)
515 m_panel
->GetCal()->EnableHolidayDisplay(event
.IsChecked());
518 void MyFrame::OnCalSpecial(wxCommandEvent
& event
)
520 m_panel
->HighlightSpecial(GetMenuBar()->IsChecked(event
.GetId()));
523 void MyFrame::OnCalLimitDates(wxCommandEvent
& event
)
525 m_panel
->LimitDateRange(GetMenuBar()->IsChecked(event
.GetId()));
528 void MyFrame::OnCalAllowMonth(wxCommandEvent
& event
)
530 m_panel
->GetCal()->EnableMonthChange(event
.IsChecked());
533 void MyFrame::OnCalSeqMonth(wxCommandEvent
& event
)
535 m_panel
->ToggleCalStyle(event
.IsChecked(),
536 wxCAL_SEQUENTIAL_MONTH_SELECTION
);
539 void MyFrame::OnCalShowSurroundingWeeks(wxCommandEvent
& event
)
541 m_panel
->ToggleCalStyle(event
.IsChecked(), wxCAL_SHOW_SURROUNDING_WEEKS
);
544 void MyFrame::OnCalShowWeekNumbers(wxCommandEvent
& event
)
546 m_panel
->ToggleCalStyle(event
.IsChecked(), wxCAL_SHOW_WEEK_NUMBERS
);
549 void MyFrame::OnSetDate(wxCommandEvent
&WXUNUSED(event
))
551 m_panel
->SetDate(wxDateTime(24, wxDateTime::Dec
, 2005, 22, 00, 00));
554 void MyFrame::OnToday(wxCommandEvent
&WXUNUSED(event
))
556 m_panel
->SetDate(wxDateTime::Today());
559 void MyFrame::OnBeginDST(wxCommandEvent
&WXUNUSED(event
))
561 m_panel
->SetDate(wxDateTime::GetBeginDST(m_panel
->GetDate().GetYear()));
564 void MyFrame::OnCalToggleResizable(wxCommandEvent
& event
)
566 wxSizer
* const sizer
= m_panel
->GetSizer();
567 wxSizerItem
* const item
= sizer
->GetItem(m_panel
->GetCal());
568 if ( event
.IsChecked() )
570 item
->SetProportion(1);
571 item
->SetFlag(wxEXPAND
);
573 else // not resizable
575 item
->SetProportion(0);
576 item
->SetFlag(wxALIGN_CENTER
);
582 void MyFrame::OnCalRClick(wxMouseEvent
& event
)
585 wxDateTime::WeekDay wd
;
587 const wxPoint pt
= event
.GetPosition();
588 wxString msg
= wxString::Format("Point (%d, %d) is ", pt
.x
, pt
.y
);
590 switch ( m_panel
->GetCal()->HitTest(pt
, &dt
, &wd
) )
593 wxFAIL_MSG( "unexpected" );
596 case wxCAL_HITTEST_NOWHERE
:
600 case wxCAL_HITTEST_HEADER
:
601 msg
+= wxString::Format("over %s", wxDateTime::GetWeekDayName(wd
));
604 case wxCAL_HITTEST_DAY
:
605 msg
+= wxString::Format("over %s", dt
.FormatISODate());
608 case wxCAL_HITTEST_INCMONTH
:
609 msg
+= "over next month button";
612 case wxCAL_HITTEST_DECMONTH
:
613 msg
+= "over previous month button";
616 case wxCAL_HITTEST_SURROUNDING_WEEK
:
617 msg
+= "over a day from another month";
621 wxLogMessage("%s", msg
);
624 #if wxUSE_DATEPICKCTRL
626 void MyFrame::OnUpdateUIStartWithNone(wxUpdateUIEvent
& event
)
628 // it only makes sense to start with invalid date if we can have no date
629 event
.Enable( GetMenuBar()->IsChecked(Calendar_DatePicker_AllowNone
) );
632 void MyFrame::OnAskDate(wxCommandEvent
& WXUNUSED(event
))
634 wxDateTime dt
= m_panel
->GetCal()->GetDate();
636 int style
= wxDP_DEFAULT
;
637 if ( GetMenuBar()->IsChecked(Calendar_DatePicker_ShowCentury
) )
638 style
|= wxDP_SHOWCENTURY
;
639 if ( GetMenuBar()->IsChecked(Calendar_DatePicker_DropDown
) )
640 style
|= wxDP_DROPDOWN
;
641 if ( GetMenuBar()->IsChecked(Calendar_DatePicker_AllowNone
) )
643 style
|= wxDP_ALLOWNONE
;
645 if ( GetMenuBar()->IsChecked(Calendar_DatePicker_StartWithNone
) )
646 dt
= wxDefaultDateTime
;
649 MyDateDialog
dlg(this, dt
, style
);
650 if ( dlg
.ShowModal() == wxID_OK
)
655 const wxDateTime today
= wxDateTime::Today();
657 if ( dt
.GetDay() == today
.GetDay() &&
658 dt
.GetMonth() == today
.GetMonth() )
660 wxMessageBox(wxT("Happy birthday!"), wxT("Calendar Sample"));
663 m_panel
->SetDate(dt
);
665 wxLogStatus(wxT("Changed the date to your input"));
669 wxLogStatus(wxT("No date entered"));
674 #endif // wxUSE_DATEPICKCTRL
676 #if wxUSE_TIMEPICKCTRL
678 void MyFrame::OnAskTime(wxCommandEvent
& WXUNUSED(event
))
680 MyTimeDialog
dlg(this);
681 if ( dlg
.ShowModal() == wxID_OK
)
683 wxLogMessage("You entered %s", dlg
.GetTime().FormatISOTime());
687 #endif // wxUSE_TIMEPICKCTRL
689 // ----------------------------------------------------------------------------
691 // ----------------------------------------------------------------------------
693 MyPanel::MyPanel(wxWindow
*parent
)
694 : wxPanel(parent
, wxID_ANY
)
696 #ifdef wxHAS_NATIVE_CALENDARCTRL
697 m_usingGeneric
= false;
699 m_usingGeneric
= true;
703 date
.Printf(wxT("Selected date: %s"),
704 wxDateTime::Today().FormatISODate().c_str());
705 m_date
= new wxStaticText(this, wxID_ANY
, date
);
706 m_calendar
= DoCreateCalendar(wxDefaultDateTime
,
707 wxCAL_MONDAY_FIRST
| wxCAL_SHOW_HOLIDAYS
);
709 // adjust to vertical/horizontal display, check mostly dedicated to WinCE
710 bool horizontal
= ( wxSystemSettings::GetMetric(wxSYS_SCREEN_X
) > wxSystemSettings::GetMetric(wxSYS_SCREEN_Y
) );
711 m_sizer
= new wxBoxSizer( horizontal
? wxHORIZONTAL
: wxVERTICAL
);
713 m_sizer
->Add(m_date
, 0, wxALIGN_CENTER
| wxALL
, 10 );
714 m_sizer
->Add(m_calendar
, 0, wxALIGN_CENTER
| wxALIGN_LEFT
);
717 m_sizer
->SetSizeHints( this );
720 void MyPanel::OnCalendar(wxCalendarEvent
& event
)
722 // clicking the same date twice unmarks it (convenient for testing)
723 static wxDateTime s_dateLast
;
724 const bool mark
= !s_dateLast
.IsValid() || event
.GetDate() != s_dateLast
;
726 s_dateLast
= event
.GetDate();
728 m_calendar
->Mark(event
.GetDate().GetDay(), mark
);
729 wxLogMessage(wxT("Selected (and %smarked) %s from calendar."),
730 mark
? "" : "un", s_dateLast
.FormatISODate().c_str());
733 void MyPanel::OnCalendarChange(wxCalendarEvent
& event
)
736 s
.Printf(wxT("Selected date: %s"), event
.GetDate().FormatISODate().c_str());
742 void MyPanel::OnCalMonthChange(wxCalendarEvent
& event
)
744 wxLogStatus(wxT("Calendar month changed to %s %d"),
745 wxDateTime::GetMonthName(event
.GetDate().GetMonth()),
746 event
.GetDate().GetYear());
749 void MyPanel::OnCalendarWeekDayClick(wxCalendarEvent
& event
)
751 wxLogMessage(wxT("Clicked on %s"),
752 wxDateTime::GetWeekDayName(event
.GetWeekDay()).c_str());
755 void MyPanel::OnCalendarWeekClick(wxCalendarEvent
& event
)
757 wxLogMessage(wxT("Clicked on week %d"), event
.GetDate().GetWeekOfYear());
760 wxCalendarCtrlBase
*MyPanel::DoCreateCalendar(const wxDateTime
& dt
, long style
)
762 wxCalendarCtrlBase
*calendar
;
763 #ifdef wxHAS_NATIVE_CALENDARCTRL
764 if ( m_usingGeneric
)
765 calendar
= new wxGenericCalendarCtrl(this, Calendar_CalCtrl
,
771 #endif // wxHAS_NATIVE_CALENDARCTRL
772 calendar
= new wxCalendarCtrl(this, Calendar_CalCtrl
,
778 calendar
->Connect(wxEVT_RIGHT_DOWN
,
779 wxMouseEventHandler(MyFrame::OnCalRClick
),
781 ( MyFrame
* )wxGetTopLevelParent(this));
786 void MyPanel::RecreateCalendar(long style
)
788 wxCalendarCtrlBase
*calendar
= DoCreateCalendar(m_calendar
->GetDate(), style
);
790 m_sizer
->Replace(m_calendar
, calendar
);
792 m_calendar
= calendar
;
797 void MyPanel::ToggleCalStyle(bool on
, int flag
)
799 long style
= m_calendar
->GetWindowStyle();
805 if ( flag
== wxCAL_SEQUENTIAL_MONTH_SELECTION
806 || flag
== wxCAL_SHOW_WEEK_NUMBERS
)
808 // changing this style requires recreating the control
809 RecreateCalendar(style
);
811 else // just changing the style is enough
813 m_calendar
->SetWindowStyle(style
);
815 m_calendar
->Refresh();
819 void MyPanel::HighlightSpecial(bool on
)
824 *attrRedCircle
= new wxCalendarDateAttr(wxCAL_BORDER_ROUND
, *wxRED
),
825 *attrGreenSquare
= new wxCalendarDateAttr(wxCAL_BORDER_SQUARE
, *wxGREEN
),
826 *attrHeaderLike
= new wxCalendarDateAttr(*wxBLUE
, *wxLIGHT_GREY
);
828 m_calendar
->SetAttr(17, attrRedCircle
);
829 m_calendar
->SetAttr(29, attrGreenSquare
);
830 m_calendar
->SetAttr(13, attrHeaderLike
);
834 m_calendar
->ResetAttr(17);
835 m_calendar
->ResetAttr(29);
836 m_calendar
->ResetAttr(13);
839 m_calendar
->Refresh();
842 // Toggle a restricted date range to the six months centered on today's date.
843 void MyPanel::LimitDateRange(bool on
)
847 // limit the choice of date to 3 months around today
848 const wxDateSpan diff
= wxDateSpan::Months(3);
849 const wxDateTime today
= wxDateTime::Today();
851 // Set the restricted date range.
852 if ( m_calendar
->SetDateRange(today
- diff
, today
+ diff
) )
854 wxLogStatus("Date range limited to 3 months around today.");
855 wxDateTime firstValidDate
;
856 wxDateTime lastValidDate
;
857 if ( m_calendar
->GetDateRange(&firstValidDate
, &lastValidDate
) )
859 wxLogMessage("First valid date: %s, last valid date: %s",
860 firstValidDate
.FormatISODate(),
861 lastValidDate
.FormatISODate());
865 wxLogWarning("Failed to get back the valid dates range.");
870 wxLogWarning("Date range not supported.");
875 // Remove the date restrictions.
876 if ( m_calendar
->SetDateRange() )
878 wxLogStatus("Date choice is unlimited now.");
882 wxLogWarning("Date range not supported.");
886 m_calendar
->Refresh();
889 // ----------------------------------------------------------------------------
891 // ----------------------------------------------------------------------------
893 #if wxUSE_DATEPICKCTRL
895 BEGIN_EVENT_TABLE(MyDateDialog
, wxDialog
)
896 EVT_DATE_CHANGED(wxID_ANY
, MyDateDialog::OnDateChange
)
899 MyDateDialog::MyDateDialog(wxWindow
*parent
, const wxDateTime
& dt
, int dtpStyle
)
900 : wxDialog(parent
, wxID_ANY
, wxString(wxT("Calendar: Choose a date")))
902 #if wxUSE_DATEPICKCTRL_GENERIC
903 wxFrame
*frame
= (wxFrame
*)wxGetTopLevelParent(parent
);
904 if ( frame
&& frame
->GetMenuBar()->IsChecked(Calendar_DatePicker_Generic
) )
905 m_datePicker
= new wxDatePickerCtrlGeneric(this, wxID_ANY
, dt
,
910 #endif // wxUSE_DATEPICKCTRL_GENERIC
911 m_datePicker
= new wxDatePickerCtrl(this, wxID_ANY
, dt
,
912 wxDefaultPosition
, wxDefaultSize
,
914 m_datePicker
->SetRange(wxDateTime(1, wxDateTime::Jan
, 1900),
916 m_dateText
= new wxStaticText(this, wxID_ANY
,
917 dt
.IsValid() ? dt
.FormatISODate()
920 const wxSizerFlags flags
= wxSizerFlags().Centre().Border();
921 wxFlexGridSizer
* const sizerMain
= new wxFlexGridSizer(2);
922 sizerMain
->Add(new wxStaticText(this, wxID_ANY
, "Enter &date:"), flags
);
923 sizerMain
->Add(m_datePicker
, flags
);
925 sizerMain
->Add(new wxStaticText(this, wxID_ANY
, "Date in ISO format:"),
927 sizerMain
->Add(m_dateText
, flags
);
929 wxSizer
*sizerTop
= new wxBoxSizer(wxVERTICAL
);
930 sizerTop
->Add(sizerMain
, flags
);
931 sizerTop
->Add(CreateStdDialogButtonSizer(wxOK
| wxCANCEL
), flags
);
933 SetSizerAndFit(sizerTop
);
936 void MyDateDialog::OnDateChange(wxDateEvent
& event
)
938 const wxDateTime dt
= event
.GetDate();
940 m_dateText
->SetLabel(dt
.FormatISODate());
942 m_dateText
->SetLabel(wxEmptyString
);
945 #endif // wxUSE_DATEPICKCTRL
947 // ----------------------------------------------------------------------------
949 // ----------------------------------------------------------------------------
951 #if wxUSE_TIMEPICKCTRL
953 BEGIN_EVENT_TABLE(MyTimeDialog
, wxDialog
)
954 EVT_TIME_CHANGED(wxID_ANY
, MyTimeDialog::OnTimeChange
)
957 MyTimeDialog::MyTimeDialog(wxWindow
*parent
)
958 : wxDialog(parent
, wxID_ANY
, wxString(wxT("Calendar: Choose time")))
960 #if wxUSE_TIMEPICKCTRL_GENERIC
961 wxFrame
*frame
= (wxFrame
*)wxGetTopLevelParent(parent
);
962 if ( frame
&& frame
->GetMenuBar()->IsChecked(Calendar_TimePicker_Generic
) )
963 m_timePicker
= new wxTimePickerCtrlGeneric(this, wxID_ANY
);
965 #endif // wxUSE_TIMEPICKCTRL_GENERIC
966 m_timePicker
= new wxTimePickerCtrl(this, wxID_ANY
);
967 m_timeText
= new wxStaticText(this, wxID_ANY
,
968 m_timePicker
->GetValue().FormatISOTime());
970 const wxSizerFlags flags
= wxSizerFlags().Centre().Border();
971 wxFlexGridSizer
* const sizerMain
= new wxFlexGridSizer(2);
972 sizerMain
->Add(new wxStaticText(this, wxID_ANY
, "Enter &time:"), flags
);
973 sizerMain
->Add(m_timePicker
, flags
);
975 sizerMain
->Add(new wxStaticText(this, wxID_ANY
, "Time in ISO format:"),
977 sizerMain
->Add(m_timeText
, flags
);
979 wxSizer
* sizerTop
= new wxBoxSizer(wxVERTICAL
);
980 sizerTop
->Add(sizerMain
, flags
);
981 sizerTop
->Add(CreateStdDialogButtonSizer(wxOK
| wxCANCEL
), flags
);
983 SetSizerAndFit(sizerTop
);
986 void MyTimeDialog::OnTimeChange(wxDateEvent
& event
)
988 m_timeText
->SetLabel(event
.GetDate().FormatISOTime());
991 #endif // wxUSE_TIMEPICKCTRL