1 /////////////////////////////////////////////////////////////////////////////
3 // Purpose: wxCalendarCtrl sample
4 // Author: Vadim Zeitlin
8 // Copyright: (c) Vadim Zeitlin
9 // Licence: wxWindows licence
10 /////////////////////////////////////////////////////////////////////////////
12 // ============================================================================
14 // ============================================================================
16 // ----------------------------------------------------------------------------
18 // ----------------------------------------------------------------------------
20 // For compilers that support precompilation, includes "wx/wx.h".
21 #include "wx/wxprec.h"
27 // for all others, include the necessary headers
33 #include "wx/stattext.h"
35 #include "wx/layout.h"
36 #include "wx/msgdlg.h"
38 #include "wx/button.h"
40 #include "wx/textctrl.h"
41 #include "wx/settings.h"
44 #include "wx/calctrl.h"
45 #include "wx/splitter.h"
47 #if wxUSE_DATEPICKCTRL
48 #include "wx/datectrl.h"
49 #if wxUSE_DATEPICKCTRL_GENERIC
50 #include "wx/generic/datectrl.h"
51 #endif // wxUSE_DATEPICKCTRL_GENERIC
52 #endif // wxUSE_DATEPICKCTRL
54 #if wxUSE_TIMEPICKCTRL
55 #include "wx/timectrl.h"
56 #if wxUSE_TIMEPICKCTRL_GENERIC
57 #include "wx/generic/timectrl.h"
58 #endif // wxUSE_TIMEPICKCTRL_GENERIC
59 #endif // wxUSE_TIMEPICKCTRL
61 #include "../sample.xpm"
63 #ifdef wxHAS_NATIVE_CALENDARCTRL
64 #include "wx/generic/calctrlg.h"
67 // ----------------------------------------------------------------------------
69 // ----------------------------------------------------------------------------
71 // Define a new application type, each program should derive a class from wxApp
72 class MyApp
: public wxApp
75 // override base class virtuals
76 // ----------------------------
78 // this one is called on application startup and is a good place for the app
79 // initialization (doing it here and not in the ctor allows to have an error
80 // return: if OnInit() returns false, the application terminates)
81 virtual bool OnInit();
84 class MyPanel
: public wxPanel
87 MyPanel(wxWindow
*parent
);
89 void OnCalendar(wxCalendarEvent
& event
);
90 void OnCalendarWeekDayClick(wxCalendarEvent
& event
);
91 void OnCalendarWeekClick(wxCalendarEvent
& event
);
92 void OnCalendarChange(wxCalendarEvent
& event
);
93 void OnCalMonthChange(wxCalendarEvent
& event
);
95 wxCalendarCtrlBase
*GetCal() const { return m_calendar
; }
97 // turn on/off the specified style bit on the calendar control
98 void ToggleCalStyle(bool on
, int style
);
100 bool IsUsingGeneric() const { return m_usingGeneric
; }
101 void ToggleUseGeneric()
103 m_usingGeneric
= !m_usingGeneric
;
104 RecreateCalendar(m_calendar
->GetWindowStyle());
107 void HighlightSpecial(bool on
);
108 void LimitDateRange(bool on
);
110 wxDateTime
GetDate() const { return m_calendar
->GetDate(); }
111 void SetDate(const wxDateTime
& dt
) { m_calendar
->SetDate(dt
); }
114 wxCalendarCtrlBase
*DoCreateCalendar(const wxDateTime
& dt
, long style
);
116 void RecreateCalendar(long style
);
118 wxCalendarCtrlBase
*m_calendar
;
119 wxStaticText
*m_date
;
125 DECLARE_EVENT_TABLE()
128 // Define a new frame type: this is going to be our main frame
129 class MyFrame
: public wxFrame
133 MyFrame(const wxString
& title
, const wxPoint
& pos
= wxDefaultPosition
, const wxSize
& size
= wxDefaultSize
);
135 // event handlers (these functions should _not_ be virtual)
136 void OnAbout(wxCommandEvent
& event
);
137 void OnClearLog(wxCommandEvent
& event
);
138 void OnQuit(wxCommandEvent
& event
);
140 #if wxUSE_DATEPICKCTRL
141 void OnAskDate(wxCommandEvent
& event
);
143 void OnUpdateUIStartWithNone(wxUpdateUIEvent
& event
);
144 #endif // wxUSE_DATEPICKCTRL
146 #if wxUSE_TIMEPICKCTRL
147 void OnAskTime(wxCommandEvent
& event
);
148 #endif // wxUSE_TIMEPICKCTRL
150 #ifdef wxHAS_NATIVE_CALENDARCTRL
151 void OnCalGeneric(wxCommandEvent
& WXUNUSED(event
))
153 m_panel
->ToggleUseGeneric();
155 #endif // wxHAS_NATIVE_CALENDARCTRL
157 void OnCalMonday(wxCommandEvent
& event
);
158 void OnCalHolidays(wxCommandEvent
& event
);
159 void OnCalSpecial(wxCommandEvent
& event
);
161 void OnCalAllowMonth(wxCommandEvent
& event
);
162 void OnCalLimitDates(wxCommandEvent
& event
);
163 void OnCalSeqMonth(wxCommandEvent
& event
);
164 void OnCalShowSurroundingWeeks(wxCommandEvent
& event
);
165 void OnCalShowWeekNumbers(wxCommandEvent
& event
);
167 void OnSetDate(wxCommandEvent
& event
);
168 void OnToday(wxCommandEvent
& event
);
169 void OnBeginDST(wxCommandEvent
& event
);
171 void OnCalToggleResizable(wxCommandEvent
& event
);
173 void OnUpdateUIGenericOnly(wxUpdateUIEvent
& event
)
175 event
.Enable(m_panel
->IsUsingGeneric());
178 void OnCalRClick(wxMouseEvent
& event
);
182 wxTextCtrl
*m_logWindow
;
184 // any class wishing to process wxWidgets events must use this macro
185 DECLARE_EVENT_TABLE()
188 #if wxUSE_DATEPICKCTRL
190 // Define a simple modal dialog which asks the user for a date
191 class MyDateDialog
: public wxDialog
194 MyDateDialog(wxWindow
*parent
, const wxDateTime
& dt
, int dtpStyle
);
196 wxDateTime
GetDate() const { return m_datePicker
->GetValue(); }
199 void OnDateChange(wxDateEvent
& event
);
202 wxDatePickerCtrlBase
*m_datePicker
;
203 wxStaticText
*m_dateText
;
206 DECLARE_EVENT_TABLE()
209 #endif // wxUSE_DATEPICKCTRL
211 #if wxUSE_TIMEPICKCTRL
213 // Another simple dialog, this one asking for time.
214 class MyTimeDialog
: public wxDialog
217 MyTimeDialog(wxWindow
* parent
);
219 wxDateTime
GetTime() const { return m_timePicker
->GetValue(); }
222 void OnTimeChange(wxDateEvent
& event
);
224 wxTimePickerCtrlBase
* m_timePicker
;
225 wxStaticText
* m_timeText
;
227 wxDECLARE_EVENT_TABLE();
230 #endif // wxUSE_TIMEPICKCTRL
232 // ----------------------------------------------------------------------------
234 // ----------------------------------------------------------------------------
236 // IDs for the controls and the menu commands
240 Calendar_File_About
= wxID_ABOUT
,
241 Calendar_File_ClearLog
= wxID_CLEAR
,
242 Calendar_File_Quit
= wxID_EXIT
,
243 Calendar_Cal_Generic
= 200,
245 Calendar_Cal_Holidays
,
246 Calendar_Cal_Special
,
248 Calendar_Cal_LimitDates
,
249 Calendar_Cal_SeqMonth
,
250 Calendar_Cal_SurroundWeeks
,
251 Calendar_Cal_WeekNumbers
,
252 Calendar_Cal_SetDate
,
254 Calendar_Cal_BeginDST
,
255 Calendar_Cal_Resizable
,
256 #if wxUSE_DATEPICKCTRL
257 Calendar_DatePicker_AskDate
= 300,
258 Calendar_DatePicker_ShowCentury
,
259 Calendar_DatePicker_DropDown
,
260 Calendar_DatePicker_AllowNone
,
261 Calendar_DatePicker_StartWithNone
,
262 #if wxUSE_DATEPICKCTRL_GENERIC
263 Calendar_DatePicker_Generic
,
264 #endif // wxUSE_DATEPICKCTRL_GENERIC
265 #endif // wxUSE_DATEPICKCTRL
266 #if wxUSE_TIMEPICKCTRL
267 Calendar_TimePicker_AskTime
= 400,
268 #if wxUSE_TIMEPICKCTRL_GENERIC
269 Calendar_TimePicker_Generic
,
270 #endif // wxUSE_TIMEPICKCTRL_GENERIC
271 #endif // wxUSE_TIMEPICKCTRL
272 Calendar_CalCtrl
= 1000
275 // ----------------------------------------------------------------------------
276 // event tables and other macros for wxWidgets
277 // ----------------------------------------------------------------------------
279 // the event tables connect the wxWidgets events with the functions (event
280 // handlers) which process them. It can be also done at run-time, but for the
281 // simple menu events like this the static method is much simpler.
282 BEGIN_EVENT_TABLE(MyFrame
, wxFrame
)
283 EVT_MENU(Calendar_File_About
, MyFrame::OnAbout
)
284 EVT_MENU(Calendar_File_ClearLog
, MyFrame::OnClearLog
)
285 EVT_MENU(Calendar_File_Quit
, MyFrame::OnQuit
)
287 #if wxUSE_DATEPICKCTRL
288 EVT_MENU(Calendar_DatePicker_AskDate
, MyFrame::OnAskDate
)
290 EVT_UPDATE_UI(Calendar_DatePicker_StartWithNone
,
291 MyFrame::OnUpdateUIStartWithNone
)
292 #endif // wxUSE_DATEPICKCTRL
294 #if wxUSE_TIMEPICKCTRL
295 EVT_MENU(Calendar_TimePicker_AskTime
, MyFrame::OnAskTime
)
296 #endif // wxUSE_TIMEPICKCTRL
298 #ifdef wxHAS_NATIVE_CALENDARCTRL
299 EVT_MENU(Calendar_Cal_Generic
, MyFrame::OnCalGeneric
)
300 #endif // wxHAS_NATIVE_CALENDARCTRL
302 EVT_MENU(Calendar_Cal_Monday
, MyFrame::OnCalMonday
)
303 EVT_MENU(Calendar_Cal_Holidays
, MyFrame::OnCalHolidays
)
304 EVT_MENU(Calendar_Cal_Special
, MyFrame::OnCalSpecial
)
306 EVT_MENU(Calendar_Cal_Month
, MyFrame::OnCalAllowMonth
)
308 EVT_MENU(Calendar_Cal_LimitDates
, MyFrame::OnCalLimitDates
)
310 EVT_MENU(Calendar_Cal_SeqMonth
, MyFrame::OnCalSeqMonth
)
311 EVT_MENU(Calendar_Cal_SurroundWeeks
, MyFrame::OnCalShowSurroundingWeeks
)
312 EVT_MENU(Calendar_Cal_WeekNumbers
, MyFrame::OnCalShowWeekNumbers
)
314 EVT_MENU(Calendar_Cal_SetDate
, MyFrame::OnSetDate
)
315 EVT_MENU(Calendar_Cal_Today
, MyFrame::OnToday
)
316 EVT_MENU(Calendar_Cal_BeginDST
, MyFrame::OnBeginDST
)
318 EVT_MENU(Calendar_Cal_Resizable
, MyFrame::OnCalToggleResizable
)
321 EVT_UPDATE_UI(Calendar_Cal_SeqMonth
, MyFrame::OnUpdateUIGenericOnly
)
323 EVT_UPDATE_UI(Calendar_Cal_Monday
, MyFrame::OnUpdateUIGenericOnly
)
324 EVT_UPDATE_UI(Calendar_Cal_Holidays
, MyFrame::OnUpdateUIGenericOnly
)
326 EVT_UPDATE_UI(Calendar_Cal_Special
, MyFrame::OnUpdateUIGenericOnly
)
327 EVT_UPDATE_UI(Calendar_Cal_SurroundWeeks
, MyFrame::OnUpdateUIGenericOnly
)
330 BEGIN_EVENT_TABLE(MyPanel
, wxPanel
)
331 EVT_CALENDAR(Calendar_CalCtrl
, MyPanel::OnCalendar
)
332 EVT_CALENDAR_PAGE_CHANGED(Calendar_CalCtrl
, MyPanel::OnCalMonthChange
)
333 EVT_CALENDAR_SEL_CHANGED(Calendar_CalCtrl
, MyPanel::OnCalendarChange
)
334 EVT_CALENDAR_WEEKDAY_CLICKED(Calendar_CalCtrl
, MyPanel::OnCalendarWeekDayClick
)
335 EVT_CALENDAR_WEEK_CLICKED(Calendar_CalCtrl
, MyPanel::OnCalendarWeekClick
)
338 // Create a new application object: this macro will allow wxWidgets to create
339 // the application object during program execution (it's better than using a
340 // static object for many reasons) and also declares the accessor function
341 // wxGetApp() which will return the reference of the right type (i.e. MyApp and
345 // ============================================================================
347 // ============================================================================
349 // ----------------------------------------------------------------------------
350 // the application class
351 // ----------------------------------------------------------------------------
353 // `Main program' equivalent: the program execution "starts" here
356 if ( !wxApp::OnInit() )
359 // Create the main application window
360 MyFrame
*frame
= new MyFrame(wxT("Calendar wxWidgets sample")
362 ,wxPoint(50, 50), wxSize(450, 340)
368 // success: wxApp::OnRun() will be called which will enter the main message
369 // loop and the application will run. If we returned false here, the
370 // application would exit immediately.
374 // ----------------------------------------------------------------------------
376 // ----------------------------------------------------------------------------
379 MyFrame::MyFrame(const wxString
& title
, const wxPoint
& pos
, const wxSize
& size
)
380 : wxFrame((wxFrame
*)NULL
, wxID_ANY
, title
, pos
, size
)
382 // set the frame icon
383 SetIcon(wxICON(sample
));
386 wxMenuBar
*menuBar
= new wxMenuBar
;
388 wxMenu
*menuFile
= new wxMenu
;
389 menuFile
->Append(Calendar_File_About
, wxT("&About\tCtrl-A"), wxT("Show about dialog"));
390 menuFile
->AppendSeparator();
391 menuFile
->Append(Calendar_File_ClearLog
, wxT("&Clear log\tCtrl-L"));
392 menuFile
->AppendSeparator();
393 menuFile
->Append(Calendar_File_Quit
, wxT("E&xit\tAlt-X"), wxT("Quit this program"));
394 menuBar
->Append(menuFile
, wxT("&File"));
396 wxMenu
*menuCal
= new wxMenu
;
397 #ifdef wxHAS_NATIVE_CALENDARCTRL
398 menuCal
->AppendCheckItem(Calendar_Cal_Generic
, "Use &generic version\tCtrl-G",
399 "Toggle between native and generic control");
400 menuCal
->AppendSeparator();
401 #endif // wxHAS_NATIVE_CALENDARCTRL
402 menuCal
->Append(Calendar_Cal_Monday
,
403 wxT("Monday &first weekday\tCtrl-F"),
404 wxT("Toggle between Mon and Sun as the first week day"),
406 menuCal
->Append(Calendar_Cal_Holidays
, wxT("Show &holidays\tCtrl-H"),
407 wxT("Toggle highlighting the holidays"),
409 menuCal
->Append(Calendar_Cal_Special
, wxT("Highlight &special dates\tCtrl-S"),
410 wxT("Test custom highlighting"),
412 menuCal
->Append(Calendar_Cal_SurroundWeeks
,
413 wxT("Show s&urrounding weeks\tCtrl-W"),
414 wxT("Show the neighbouring weeks in the prev/next month"),
416 menuCal
->Append(Calendar_Cal_WeekNumbers
,
417 wxT("Show &week numbers"),
418 wxT("Toggle week numbers"),
420 menuCal
->AppendSeparator();
421 menuCal
->Append(Calendar_Cal_SeqMonth
,
422 wxT("Toggle month selector st&yle\tCtrl-Y"),
423 wxT("Use another style for the calendar controls"),
425 menuCal
->Append(Calendar_Cal_Month
, wxT("&Month can be changed\tCtrl-M"),
426 wxT("Allow changing the month in the calendar"),
428 menuCal
->AppendCheckItem(Calendar_Cal_LimitDates
, wxT("Toggle date ra&nge\tCtrl-N"),
429 wxT("Limit the valid dates"));
430 menuCal
->AppendSeparator();
431 menuCal
->Append(Calendar_Cal_SetDate
, wxT("Call &SetDate(2005-12-24)"), wxT("Set date to 2005-12-24."));
432 menuCal
->Append(Calendar_Cal_Today
, wxT("Call &Today()"), wxT("Set to the current date."));
433 menuCal
->Append(Calendar_Cal_BeginDST
, "Call SetDate(GetBeginDST())");
434 menuCal
->AppendSeparator();
435 menuCal
->AppendCheckItem(Calendar_Cal_Resizable
, wxT("Make &resizable\tCtrl-R"));
436 menuBar
->Append(menuCal
, wxT("&Calendar"));
438 #if wxUSE_DATEPICKCTRL
439 wxMenu
*menuDate
= new wxMenu
;
440 menuDate
->AppendCheckItem(Calendar_DatePicker_ShowCentury
,
441 wxT("Al&ways show century"));
442 menuDate
->AppendCheckItem(Calendar_DatePicker_DropDown
,
443 wxT("Use &drop down control"));
444 menuDate
->AppendCheckItem(Calendar_DatePicker_AllowNone
,
445 wxT("Allow &no date"));
446 menuDate
->AppendCheckItem(Calendar_DatePicker_StartWithNone
,
447 wxT("Start &with no date"));
448 #if wxUSE_DATEPICKCTRL_GENERIC
449 menuDate
->AppendCheckItem(Calendar_DatePicker_Generic
,
450 wxT("Use &generic version of the control"));
451 #endif // wxUSE_DATEPICKCTRL_GENERIC
452 menuDate
->AppendSeparator();
453 menuDate
->Append(Calendar_DatePicker_AskDate
, wxT("&Choose date...\tCtrl-D"), wxT("Show dialog with wxDatePickerCtrl"));
454 menuBar
->Append(menuDate
, wxT("&Date picker"));
455 #endif // wxUSE_DATEPICKCTRL
457 #if wxUSE_TIMEPICKCTRL
458 wxMenu
*menuTime
= new wxMenu
;
459 #if wxUSE_TIMEPICKCTRL_GENERIC
460 menuTime
->AppendCheckItem(Calendar_TimePicker_Generic
,
461 wxT("Use &generic version of the control"));
462 menuTime
->AppendSeparator();
463 #endif // wxUSE_TIMEPICKCTRL_GENERIC
464 menuTime
->Append(Calendar_TimePicker_AskTime
, wxT("&Choose time...\tCtrl-T"), wxT("Show dialog with wxTimePickerCtrl"));
465 menuBar
->Append(menuTime
, wxT("&Time picker"));
466 #endif // wxUSE_TIMEPICKCTRL
468 menuBar
->Check(Calendar_Cal_Monday
, true);
469 menuBar
->Check(Calendar_Cal_Holidays
, true);
470 menuBar
->Check(Calendar_Cal_Month
, true);
471 menuBar
->Check(Calendar_Cal_LimitDates
, false);
473 #if wxUSE_DATEPICKCTRL
474 menuBar
->Check(Calendar_DatePicker_ShowCentury
, true);
475 #endif // wxUSE_DATEPICKCTRL
477 // ... and attach this menu bar to the frame
480 wxSplitterWindow
*splitter
= new wxSplitterWindow(this, wxID_ANY
,
481 wxDefaultPosition
, wxDefaultSize
,
483 m_panel
= new MyPanel(splitter
);
484 m_logWindow
= new wxTextCtrl(splitter
, wxID_ANY
, wxEmptyString
,
485 wxDefaultPosition
, wxDefaultSize
,
486 wxTE_READONLY
| wxTE_MULTILINE
);
487 splitter
->SplitHorizontally(m_panel
, m_logWindow
);
488 splitter
->SetMinimumPaneSize(20);
489 wxLog::SetActiveTarget(new wxLogTextCtrl(m_logWindow
));
492 void MyFrame::OnQuit(wxCommandEvent
& WXUNUSED(event
))
494 // true is to force the frame to close
498 void MyFrame::OnAbout(wxCommandEvent
& WXUNUSED(event
))
500 wxMessageBox(wxT("wxCalendarCtrl sample\n(c) 2000--2008 Vadim Zeitlin"),
501 wxT("About Calendar"), wxOK
| wxICON_INFORMATION
, this);
504 void MyFrame::OnClearLog(wxCommandEvent
& WXUNUSED(event
))
506 m_logWindow
->Clear();
509 void MyFrame::OnCalMonday(wxCommandEvent
& event
)
511 m_panel
->ToggleCalStyle(event
.IsChecked(), wxCAL_MONDAY_FIRST
);
514 void MyFrame::OnCalHolidays(wxCommandEvent
& event
)
516 m_panel
->GetCal()->EnableHolidayDisplay(event
.IsChecked());
519 void MyFrame::OnCalSpecial(wxCommandEvent
& event
)
521 m_panel
->HighlightSpecial(GetMenuBar()->IsChecked(event
.GetId()));
524 void MyFrame::OnCalLimitDates(wxCommandEvent
& event
)
526 m_panel
->LimitDateRange(GetMenuBar()->IsChecked(event
.GetId()));
529 void MyFrame::OnCalAllowMonth(wxCommandEvent
& event
)
531 m_panel
->GetCal()->EnableMonthChange(event
.IsChecked());
534 void MyFrame::OnCalSeqMonth(wxCommandEvent
& event
)
536 m_panel
->ToggleCalStyle(event
.IsChecked(),
537 wxCAL_SEQUENTIAL_MONTH_SELECTION
);
540 void MyFrame::OnCalShowSurroundingWeeks(wxCommandEvent
& event
)
542 m_panel
->ToggleCalStyle(event
.IsChecked(), wxCAL_SHOW_SURROUNDING_WEEKS
);
545 void MyFrame::OnCalShowWeekNumbers(wxCommandEvent
& event
)
547 m_panel
->ToggleCalStyle(event
.IsChecked(), wxCAL_SHOW_WEEK_NUMBERS
);
550 void MyFrame::OnSetDate(wxCommandEvent
&WXUNUSED(event
))
552 m_panel
->SetDate(wxDateTime(24, wxDateTime::Dec
, 2005, 22, 00, 00));
555 void MyFrame::OnToday(wxCommandEvent
&WXUNUSED(event
))
557 m_panel
->SetDate(wxDateTime::Today());
560 void MyFrame::OnBeginDST(wxCommandEvent
&WXUNUSED(event
))
562 m_panel
->SetDate(wxDateTime::GetBeginDST(m_panel
->GetDate().GetYear()));
565 void MyFrame::OnCalToggleResizable(wxCommandEvent
& event
)
567 wxSizer
* const sizer
= m_panel
->GetSizer();
568 wxSizerItem
* const item
= sizer
->GetItem(m_panel
->GetCal());
569 if ( event
.IsChecked() )
571 item
->SetProportion(1);
572 item
->SetFlag(wxEXPAND
);
574 else // not resizable
576 item
->SetProportion(0);
577 item
->SetFlag(wxALIGN_CENTER
);
583 void MyFrame::OnCalRClick(wxMouseEvent
& event
)
586 wxDateTime::WeekDay wd
;
588 const wxPoint pt
= event
.GetPosition();
589 wxString msg
= wxString::Format("Point (%d, %d) is ", pt
.x
, pt
.y
);
591 switch ( m_panel
->GetCal()->HitTest(pt
, &dt
, &wd
) )
594 wxFAIL_MSG( "unexpected" );
597 case wxCAL_HITTEST_NOWHERE
:
601 case wxCAL_HITTEST_HEADER
:
602 msg
+= wxString::Format("over %s", wxDateTime::GetWeekDayName(wd
));
605 case wxCAL_HITTEST_DAY
:
606 msg
+= wxString::Format("over %s", dt
.FormatISODate());
609 case wxCAL_HITTEST_INCMONTH
:
610 msg
+= "over next month button";
613 case wxCAL_HITTEST_DECMONTH
:
614 msg
+= "over previous month button";
617 case wxCAL_HITTEST_SURROUNDING_WEEK
:
618 msg
+= "over a day from another month";
622 wxLogMessage("%s", msg
);
625 #if wxUSE_DATEPICKCTRL
627 void MyFrame::OnUpdateUIStartWithNone(wxUpdateUIEvent
& event
)
629 // it only makes sense to start with invalid date if we can have no date
630 event
.Enable( GetMenuBar()->IsChecked(Calendar_DatePicker_AllowNone
) );
633 void MyFrame::OnAskDate(wxCommandEvent
& WXUNUSED(event
))
635 wxDateTime dt
= m_panel
->GetCal()->GetDate();
637 int style
= wxDP_DEFAULT
;
638 if ( GetMenuBar()->IsChecked(Calendar_DatePicker_ShowCentury
) )
639 style
|= wxDP_SHOWCENTURY
;
640 if ( GetMenuBar()->IsChecked(Calendar_DatePicker_DropDown
) )
641 style
|= wxDP_DROPDOWN
;
642 if ( GetMenuBar()->IsChecked(Calendar_DatePicker_AllowNone
) )
644 style
|= wxDP_ALLOWNONE
;
646 if ( GetMenuBar()->IsChecked(Calendar_DatePicker_StartWithNone
) )
647 dt
= wxDefaultDateTime
;
650 MyDateDialog
dlg(this, dt
, style
);
651 if ( dlg
.ShowModal() == wxID_OK
)
656 const wxDateTime today
= wxDateTime::Today();
658 if ( dt
.GetDay() == today
.GetDay() &&
659 dt
.GetMonth() == today
.GetMonth() )
661 wxMessageBox(wxT("Happy birthday!"), wxT("Calendar Sample"));
664 m_panel
->SetDate(dt
);
666 wxLogStatus(wxT("Changed the date to your input"));
670 wxLogStatus(wxT("No date entered"));
675 #endif // wxUSE_DATEPICKCTRL
677 #if wxUSE_TIMEPICKCTRL
679 void MyFrame::OnAskTime(wxCommandEvent
& WXUNUSED(event
))
681 MyTimeDialog
dlg(this);
682 if ( dlg
.ShowModal() == wxID_OK
)
684 wxLogMessage("You entered %s", dlg
.GetTime().FormatISOTime());
688 #endif // wxUSE_TIMEPICKCTRL
690 // ----------------------------------------------------------------------------
692 // ----------------------------------------------------------------------------
694 MyPanel::MyPanel(wxWindow
*parent
)
695 : wxPanel(parent
, wxID_ANY
)
697 #ifdef wxHAS_NATIVE_CALENDARCTRL
698 m_usingGeneric
= false;
700 m_usingGeneric
= true;
704 date
.Printf(wxT("Selected date: %s"),
705 wxDateTime::Today().FormatISODate().c_str());
706 m_date
= new wxStaticText(this, wxID_ANY
, date
);
707 m_calendar
= DoCreateCalendar(wxDefaultDateTime
,
708 wxCAL_MONDAY_FIRST
| wxCAL_SHOW_HOLIDAYS
);
710 // adjust to vertical/horizontal display, check mostly dedicated to WinCE
711 bool horizontal
= ( wxSystemSettings::GetMetric(wxSYS_SCREEN_X
) > wxSystemSettings::GetMetric(wxSYS_SCREEN_Y
) );
712 m_sizer
= new wxBoxSizer( horizontal
? wxHORIZONTAL
: wxVERTICAL
);
714 m_sizer
->Add(m_date
, 0, wxALIGN_CENTER
| wxALL
, 10 );
715 m_sizer
->Add(m_calendar
, 0, wxALIGN_CENTER
| wxALIGN_LEFT
);
718 m_sizer
->SetSizeHints( this );
721 void MyPanel::OnCalendar(wxCalendarEvent
& event
)
723 // clicking the same date twice unmarks it (convenient for testing)
724 static wxDateTime s_dateLast
;
725 const bool mark
= !s_dateLast
.IsValid() || event
.GetDate() != s_dateLast
;
727 s_dateLast
= event
.GetDate();
729 m_calendar
->Mark(event
.GetDate().GetDay(), mark
);
730 wxLogMessage(wxT("Selected (and %smarked) %s from calendar."),
731 mark
? "" : "un", s_dateLast
.FormatISODate().c_str());
734 void MyPanel::OnCalendarChange(wxCalendarEvent
& event
)
737 s
.Printf(wxT("Selected date: %s"), event
.GetDate().FormatISODate().c_str());
743 void MyPanel::OnCalMonthChange(wxCalendarEvent
& event
)
745 wxLogStatus(wxT("Calendar month changed to %s %d"),
746 wxDateTime::GetMonthName(event
.GetDate().GetMonth()),
747 event
.GetDate().GetYear());
750 void MyPanel::OnCalendarWeekDayClick(wxCalendarEvent
& event
)
752 wxLogMessage(wxT("Clicked on %s"),
753 wxDateTime::GetWeekDayName(event
.GetWeekDay()).c_str());
756 void MyPanel::OnCalendarWeekClick(wxCalendarEvent
& event
)
758 wxLogMessage(wxT("Clicked on week %d"), event
.GetDate().GetWeekOfYear());
761 wxCalendarCtrlBase
*MyPanel::DoCreateCalendar(const wxDateTime
& dt
, long style
)
763 wxCalendarCtrlBase
*calendar
;
764 #ifdef wxHAS_NATIVE_CALENDARCTRL
765 if ( m_usingGeneric
)
766 calendar
= new wxGenericCalendarCtrl(this, Calendar_CalCtrl
,
772 #endif // wxHAS_NATIVE_CALENDARCTRL
773 calendar
= new wxCalendarCtrl(this, Calendar_CalCtrl
,
779 calendar
->Connect(wxEVT_RIGHT_DOWN
,
780 wxMouseEventHandler(MyFrame::OnCalRClick
),
782 ( MyFrame
* )wxGetTopLevelParent(this));
787 void MyPanel::RecreateCalendar(long style
)
789 wxCalendarCtrlBase
*calendar
= DoCreateCalendar(m_calendar
->GetDate(), style
);
791 m_sizer
->Replace(m_calendar
, calendar
);
793 m_calendar
= calendar
;
798 void MyPanel::ToggleCalStyle(bool on
, int flag
)
800 long style
= m_calendar
->GetWindowStyle();
806 if ( flag
== wxCAL_SEQUENTIAL_MONTH_SELECTION
807 || flag
== wxCAL_SHOW_WEEK_NUMBERS
)
809 // changing this style requires recreating the control
810 RecreateCalendar(style
);
812 else // just changing the style is enough
814 m_calendar
->SetWindowStyle(style
);
816 m_calendar
->Refresh();
820 void MyPanel::HighlightSpecial(bool on
)
825 *attrRedCircle
= new wxCalendarDateAttr(wxCAL_BORDER_ROUND
, *wxRED
),
826 *attrGreenSquare
= new wxCalendarDateAttr(wxCAL_BORDER_SQUARE
, *wxGREEN
),
827 *attrHeaderLike
= new wxCalendarDateAttr(*wxBLUE
, *wxLIGHT_GREY
);
829 m_calendar
->SetAttr(17, attrRedCircle
);
830 m_calendar
->SetAttr(29, attrGreenSquare
);
831 m_calendar
->SetAttr(13, attrHeaderLike
);
835 m_calendar
->ResetAttr(17);
836 m_calendar
->ResetAttr(29);
837 m_calendar
->ResetAttr(13);
840 m_calendar
->Refresh();
843 // Toggle a restricted date range to the six months centered on today's date.
844 void MyPanel::LimitDateRange(bool on
)
848 // limit the choice of date to 3 months around today
849 const wxDateSpan diff
= wxDateSpan::Months(3);
850 const wxDateTime today
= wxDateTime::Today();
852 // Set the restricted date range.
853 if ( m_calendar
->SetDateRange(today
- diff
, today
+ diff
) )
855 wxLogStatus("Date range limited to 3 months around today.");
856 wxDateTime firstValidDate
;
857 wxDateTime lastValidDate
;
858 if ( m_calendar
->GetDateRange(&firstValidDate
, &lastValidDate
) )
860 wxLogMessage("First valid date: %s, last valid date: %s",
861 firstValidDate
.FormatISODate(),
862 lastValidDate
.FormatISODate());
866 wxLogWarning("Failed to get back the valid dates range.");
871 wxLogWarning("Date range not supported.");
876 // Remove the date restrictions.
877 if ( m_calendar
->SetDateRange() )
879 wxLogStatus("Date choice is unlimited now.");
883 wxLogWarning("Date range not supported.");
887 m_calendar
->Refresh();
890 // ----------------------------------------------------------------------------
892 // ----------------------------------------------------------------------------
894 #if wxUSE_DATEPICKCTRL
896 BEGIN_EVENT_TABLE(MyDateDialog
, wxDialog
)
897 EVT_DATE_CHANGED(wxID_ANY
, MyDateDialog::OnDateChange
)
900 MyDateDialog::MyDateDialog(wxWindow
*parent
, const wxDateTime
& dt
, int dtpStyle
)
901 : wxDialog(parent
, wxID_ANY
, wxString(wxT("Calendar: Choose a date")))
903 #if wxUSE_DATEPICKCTRL_GENERIC
904 wxFrame
*frame
= (wxFrame
*)wxGetTopLevelParent(parent
);
905 if ( frame
&& frame
->GetMenuBar()->IsChecked(Calendar_DatePicker_Generic
) )
906 m_datePicker
= new wxDatePickerCtrlGeneric(this, wxID_ANY
, dt
,
911 #endif // wxUSE_DATEPICKCTRL_GENERIC
912 m_datePicker
= new wxDatePickerCtrl(this, wxID_ANY
, dt
,
913 wxDefaultPosition
, wxDefaultSize
,
915 m_datePicker
->SetRange(wxDateTime(1, wxDateTime::Jan
, 1900),
917 m_dateText
= new wxStaticText(this, wxID_ANY
,
918 dt
.IsValid() ? dt
.FormatISODate()
921 const wxSizerFlags flags
= wxSizerFlags().Centre().Border();
922 wxFlexGridSizer
* const sizerMain
= new wxFlexGridSizer(2);
923 sizerMain
->Add(new wxStaticText(this, wxID_ANY
, "Enter &date:"), flags
);
924 sizerMain
->Add(m_datePicker
, flags
);
926 sizerMain
->Add(new wxStaticText(this, wxID_ANY
, "Date in ISO format:"),
928 sizerMain
->Add(m_dateText
, flags
);
930 wxSizer
*sizerTop
= new wxBoxSizer(wxVERTICAL
);
931 sizerTop
->Add(sizerMain
, flags
);
932 sizerTop
->Add(CreateStdDialogButtonSizer(wxOK
| wxCANCEL
), flags
);
934 SetSizerAndFit(sizerTop
);
937 void MyDateDialog::OnDateChange(wxDateEvent
& event
)
939 const wxDateTime dt
= event
.GetDate();
941 m_dateText
->SetLabel(dt
.FormatISODate());
943 m_dateText
->SetLabel(wxEmptyString
);
946 #endif // wxUSE_DATEPICKCTRL
948 // ----------------------------------------------------------------------------
950 // ----------------------------------------------------------------------------
952 #if wxUSE_TIMEPICKCTRL
954 BEGIN_EVENT_TABLE(MyTimeDialog
, wxDialog
)
955 EVT_TIME_CHANGED(wxID_ANY
, MyTimeDialog::OnTimeChange
)
958 MyTimeDialog::MyTimeDialog(wxWindow
*parent
)
959 : wxDialog(parent
, wxID_ANY
, wxString(wxT("Calendar: Choose time")))
961 #if wxUSE_TIMEPICKCTRL_GENERIC
962 wxFrame
*frame
= (wxFrame
*)wxGetTopLevelParent(parent
);
963 if ( frame
&& frame
->GetMenuBar()->IsChecked(Calendar_TimePicker_Generic
) )
964 m_timePicker
= new wxTimePickerCtrlGeneric(this, wxID_ANY
);
966 #endif // wxUSE_TIMEPICKCTRL_GENERIC
967 m_timePicker
= new wxTimePickerCtrl(this, wxID_ANY
);
968 m_timeText
= new wxStaticText(this, wxID_ANY
,
969 m_timePicker
->GetValue().FormatISOTime());
971 const wxSizerFlags flags
= wxSizerFlags().Centre().Border();
972 wxFlexGridSizer
* const sizerMain
= new wxFlexGridSizer(2);
973 sizerMain
->Add(new wxStaticText(this, wxID_ANY
, "Enter &time:"), flags
);
974 sizerMain
->Add(m_timePicker
, flags
);
976 sizerMain
->Add(new wxStaticText(this, wxID_ANY
, "Time in ISO format:"),
978 sizerMain
->Add(m_timeText
, flags
);
980 wxSizer
* sizerTop
= new wxBoxSizer(wxVERTICAL
);
981 sizerTop
->Add(sizerMain
, flags
);
982 sizerTop
->Add(CreateStdDialogButtonSizer(wxOK
| wxCANCEL
), flags
);
984 SetSizerAndFit(sizerTop
);
987 void MyTimeDialog::OnTimeChange(wxDateEvent
& event
)
989 m_timeText
->SetLabel(event
.GetDate().FormatISOTime());
992 #endif // wxUSE_TIMEPICKCTRL