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 #include "../sample.xpm"
56 #ifdef wxHAS_NATIVE_CALENDARCTRL
57 #include "wx/generic/calctrlg.h"
60 // ----------------------------------------------------------------------------
62 // ----------------------------------------------------------------------------
64 // Define a new application type, each program should derive a class from wxApp
65 class MyApp
: public wxApp
68 // override base class virtuals
69 // ----------------------------
71 // this one is called on application startup and is a good place for the app
72 // initialization (doing it here and not in the ctor allows to have an error
73 // return: if OnInit() returns false, the application terminates)
74 virtual bool OnInit();
77 class MyPanel
: public wxPanel
80 MyPanel(wxWindow
*parent
);
82 void OnCalendar(wxCalendarEvent
& event
);
83 void OnCalendarWeekDayClick(wxCalendarEvent
& event
);
84 void OnCalendarWeekClick(wxCalendarEvent
& event
);
85 void OnCalendarChange(wxCalendarEvent
& event
);
86 void OnCalMonthChange(wxCalendarEvent
& event
);
88 wxCalendarCtrlBase
*GetCal() const { return m_calendar
; }
90 // turn on/off the specified style bit on the calendar control
91 void ToggleCalStyle(bool on
, int style
);
93 bool IsUsingGeneric() const { return m_usingGeneric
; }
94 void ToggleUseGeneric()
96 m_usingGeneric
= !m_usingGeneric
;
97 RecreateCalendar(m_calendar
->GetWindowStyle());
100 void HighlightSpecial(bool on
);
101 void LimitDateRange(bool on
);
103 wxDateTime
GetDate() const { return m_calendar
->GetDate(); }
104 void SetDate(const wxDateTime
& dt
) { m_calendar
->SetDate(dt
); }
107 wxCalendarCtrlBase
*DoCreateCalendar(const wxDateTime
& dt
, long style
);
109 void RecreateCalendar(long style
);
111 wxCalendarCtrlBase
*m_calendar
;
112 wxStaticText
*m_date
;
118 DECLARE_EVENT_TABLE()
121 // Define a new frame type: this is going to be our main frame
122 class MyFrame
: public wxFrame
126 MyFrame(const wxString
& title
, const wxPoint
& pos
= wxDefaultPosition
, const wxSize
& size
= wxDefaultSize
);
128 // event handlers (these functions should _not_ be virtual)
129 void OnAbout(wxCommandEvent
& event
);
130 void OnClearLog(wxCommandEvent
& event
);
131 void OnQuit(wxCommandEvent
& event
);
133 #if wxUSE_DATEPICKCTRL
134 void OnAskDate(wxCommandEvent
& event
);
136 void OnUpdateUIStartWithNone(wxUpdateUIEvent
& event
);
137 #endif // wxUSE_DATEPICKCTRL
139 #ifdef wxHAS_NATIVE_CALENDARCTRL
140 void OnCalGeneric(wxCommandEvent
& WXUNUSED(event
))
142 m_panel
->ToggleUseGeneric();
144 #endif // wxHAS_NATIVE_CALENDARCTRL
146 void OnCalMonday(wxCommandEvent
& event
);
147 void OnCalHolidays(wxCommandEvent
& event
);
148 void OnCalSpecial(wxCommandEvent
& event
);
150 void OnCalAllowMonth(wxCommandEvent
& event
);
151 void OnCalLimitDates(wxCommandEvent
& event
);
152 void OnCalSeqMonth(wxCommandEvent
& event
);
153 void OnCalShowSurroundingWeeks(wxCommandEvent
& event
);
154 void OnCalShowWeekNumbers(wxCommandEvent
& event
);
156 void OnSetDate(wxCommandEvent
& event
);
157 void OnToday(wxCommandEvent
& event
);
158 void OnBeginDST(wxCommandEvent
& event
);
160 void OnCalToggleResizable(wxCommandEvent
& event
);
162 void OnUpdateUIGenericOnly(wxUpdateUIEvent
& event
)
164 event
.Enable(m_panel
->IsUsingGeneric());
167 void OnCalRClick(wxMouseEvent
& event
);
171 wxTextCtrl
*m_logWindow
;
173 // any class wishing to process wxWidgets events must use this macro
174 DECLARE_EVENT_TABLE()
177 #if wxUSE_DATEPICKCTRL
179 // Define a simple modal dialog which asks the user for a date
180 class MyDialog
: public wxDialog
183 MyDialog(wxWindow
*parent
, const wxDateTime
& dt
, int dtpStyle
);
185 wxDateTime
GetDate() const { return m_datePicker
->GetValue(); }
188 void OnDateChange(wxDateEvent
& event
);
191 wxDatePickerCtrlBase
*m_datePicker
;
195 DECLARE_EVENT_TABLE()
198 #endif // wxUSE_DATEPICKCTRL
200 // ----------------------------------------------------------------------------
202 // ----------------------------------------------------------------------------
204 // IDs for the controls and the menu commands
208 Calendar_File_About
= wxID_ABOUT
,
209 Calendar_File_ClearLog
= wxID_CLEAR
,
210 Calendar_File_Quit
= wxID_EXIT
,
211 Calendar_Cal_Generic
= 200,
213 Calendar_Cal_Holidays
,
214 Calendar_Cal_Special
,
216 Calendar_Cal_LimitDates
,
217 Calendar_Cal_SeqMonth
,
218 Calendar_Cal_SurroundWeeks
,
219 Calendar_Cal_WeekNumbers
,
220 Calendar_Cal_SetDate
,
222 Calendar_Cal_BeginDST
,
223 Calendar_Cal_Resizable
,
224 #if wxUSE_DATEPICKCTRL
225 Calendar_DatePicker_AskDate
= 300,
226 Calendar_DatePicker_ShowCentury
,
227 Calendar_DatePicker_DropDown
,
228 Calendar_DatePicker_AllowNone
,
229 Calendar_DatePicker_StartWithNone
,
230 #if wxUSE_DATEPICKCTRL_GENERIC
231 Calendar_DatePicker_Generic
,
232 #endif // wxUSE_DATEPICKCTRL_GENERIC
233 #endif // wxUSE_DATEPICKCTRL
234 Calendar_CalCtrl
= 1000
237 // ----------------------------------------------------------------------------
238 // event tables and other macros for wxWidgets
239 // ----------------------------------------------------------------------------
241 // the event tables connect the wxWidgets events with the functions (event
242 // handlers) which process them. It can be also done at run-time, but for the
243 // simple menu events like this the static method is much simpler.
244 BEGIN_EVENT_TABLE(MyFrame
, wxFrame
)
245 EVT_MENU(Calendar_File_About
, MyFrame::OnAbout
)
246 EVT_MENU(Calendar_File_ClearLog
, MyFrame::OnClearLog
)
247 EVT_MENU(Calendar_File_Quit
, MyFrame::OnQuit
)
249 #if wxUSE_DATEPICKCTRL
250 EVT_MENU(Calendar_DatePicker_AskDate
, MyFrame::OnAskDate
)
252 EVT_UPDATE_UI(Calendar_DatePicker_StartWithNone
,
253 MyFrame::OnUpdateUIStartWithNone
)
254 #endif // wxUSE_DATEPICKCTRL
256 #ifdef wxHAS_NATIVE_CALENDARCTRL
257 EVT_MENU(Calendar_Cal_Generic
, MyFrame::OnCalGeneric
)
258 #endif // wxHAS_NATIVE_CALENDARCTRL
260 EVT_MENU(Calendar_Cal_Monday
, MyFrame::OnCalMonday
)
261 EVT_MENU(Calendar_Cal_Holidays
, MyFrame::OnCalHolidays
)
262 EVT_MENU(Calendar_Cal_Special
, MyFrame::OnCalSpecial
)
264 EVT_MENU(Calendar_Cal_Month
, MyFrame::OnCalAllowMonth
)
266 EVT_MENU(Calendar_Cal_LimitDates
, MyFrame::OnCalLimitDates
)
268 EVT_MENU(Calendar_Cal_SeqMonth
, MyFrame::OnCalSeqMonth
)
269 EVT_MENU(Calendar_Cal_SurroundWeeks
, MyFrame::OnCalShowSurroundingWeeks
)
270 EVT_MENU(Calendar_Cal_WeekNumbers
, MyFrame::OnCalShowWeekNumbers
)
272 EVT_MENU(Calendar_Cal_SetDate
, MyFrame::OnSetDate
)
273 EVT_MENU(Calendar_Cal_Today
, MyFrame::OnToday
)
274 EVT_MENU(Calendar_Cal_BeginDST
, MyFrame::OnBeginDST
)
276 EVT_MENU(Calendar_Cal_Resizable
, MyFrame::OnCalToggleResizable
)
279 EVT_UPDATE_UI(Calendar_Cal_SeqMonth
, MyFrame::OnUpdateUIGenericOnly
)
281 EVT_UPDATE_UI(Calendar_Cal_Monday
, MyFrame::OnUpdateUIGenericOnly
)
282 EVT_UPDATE_UI(Calendar_Cal_Holidays
, MyFrame::OnUpdateUIGenericOnly
)
283 EVT_UPDATE_UI(Calendar_Cal_LimitDates
, MyFrame::OnUpdateUIGenericOnly
)
285 EVT_UPDATE_UI(Calendar_Cal_Special
, MyFrame::OnUpdateUIGenericOnly
)
286 EVT_UPDATE_UI(Calendar_Cal_SurroundWeeks
, MyFrame::OnUpdateUIGenericOnly
)
289 BEGIN_EVENT_TABLE(MyPanel
, wxPanel
)
290 EVT_CALENDAR(Calendar_CalCtrl
, MyPanel::OnCalendar
)
291 EVT_CALENDAR_PAGE_CHANGED(Calendar_CalCtrl
, MyPanel::OnCalMonthChange
)
292 EVT_CALENDAR_SEL_CHANGED(Calendar_CalCtrl
, MyPanel::OnCalendarChange
)
293 EVT_CALENDAR_WEEKDAY_CLICKED(Calendar_CalCtrl
, MyPanel::OnCalendarWeekDayClick
)
294 EVT_CALENDAR_WEEK_CLICKED(Calendar_CalCtrl
, MyPanel::OnCalendarWeekClick
)
297 #if wxUSE_DATEPICKCTRL
299 BEGIN_EVENT_TABLE(MyDialog
, wxDialog
)
300 EVT_DATE_CHANGED(wxID_ANY
, MyDialog::OnDateChange
)
303 #endif // wxUSE_DATEPICKCTRL
305 // Create a new application object: this macro will allow wxWidgets to create
306 // the application object during program execution (it's better than using a
307 // static object for many reasons) and also declares the accessor function
308 // wxGetApp() which will return the reference of the right type (i.e. MyApp and
312 // ============================================================================
314 // ============================================================================
316 // ----------------------------------------------------------------------------
317 // the application class
318 // ----------------------------------------------------------------------------
320 // `Main program' equivalent: the program execution "starts" here
323 if ( !wxApp::OnInit() )
326 // Create the main application window
327 MyFrame
*frame
= new MyFrame(wxT("Calendar wxWidgets sample")
329 ,wxPoint(50, 50), wxSize(450, 340)
335 // success: wxApp::OnRun() will be called which will enter the main message
336 // loop and the application will run. If we returned false here, the
337 // application would exit immediately.
341 // ----------------------------------------------------------------------------
343 // ----------------------------------------------------------------------------
346 MyFrame::MyFrame(const wxString
& title
, const wxPoint
& pos
, const wxSize
& size
)
347 : wxFrame((wxFrame
*)NULL
, wxID_ANY
, title
, pos
, size
)
349 // set the frame icon
350 SetIcon(wxIcon(sample_xpm
));
353 wxMenu
*menuFile
= new wxMenu
;
354 menuFile
->Append(Calendar_File_About
, wxT("&About...\tCtrl-A"), wxT("Show about dialog"));
355 menuFile
->AppendSeparator();
356 menuFile
->Append(Calendar_File_ClearLog
, wxT("&Clear log\tCtrl-L"));
357 menuFile
->AppendSeparator();
358 menuFile
->Append(Calendar_File_Quit
, wxT("E&xit\tAlt-X"), wxT("Quit this program"));
360 wxMenu
*menuCal
= new wxMenu
;
361 #ifdef wxHAS_NATIVE_CALENDARCTRL
362 menuCal
->AppendCheckItem(Calendar_Cal_Generic
, "Use &generic version\tCtrl-G",
363 "Toggle between native and generic control");
364 menuCal
->AppendSeparator();
365 #endif // wxHAS_NATIVE_CALENDARCTRL
366 menuCal
->Append(Calendar_Cal_Monday
,
367 wxT("Monday &first weekday\tCtrl-F"),
368 wxT("Toggle between Mon and Sun as the first week day"),
370 menuCal
->Append(Calendar_Cal_Holidays
, wxT("Show &holidays\tCtrl-H"),
371 wxT("Toggle highlighting the holidays"),
373 menuCal
->Append(Calendar_Cal_Special
, wxT("Highlight &special dates\tCtrl-S"),
374 wxT("Test custom highlighting"),
376 menuCal
->Append(Calendar_Cal_SurroundWeeks
,
377 wxT("Show s&urrounding weeks\tCtrl-W"),
378 wxT("Show the neighbouring weeks in the prev/next month"),
380 menuCal
->Append(Calendar_Cal_WeekNumbers
,
381 wxT("Show &week numbers"),
382 wxT("Toggle week numbers"),
384 menuCal
->AppendSeparator();
385 menuCal
->Append(Calendar_Cal_SeqMonth
,
386 wxT("Toggle month selector st&yle\tCtrl-Y"),
387 wxT("Use another style for the calendar controls"),
389 menuCal
->Append(Calendar_Cal_Month
, wxT("&Month can be changed\tCtrl-M"),
390 wxT("Allow changing the month in the calendar"),
392 menuCal
->AppendCheckItem(Calendar_Cal_LimitDates
, wxT("Toggle date ra&nge\tCtrl-N"),
393 wxT("Limit the valid dates"));
394 menuCal
->AppendSeparator();
395 menuCal
->Append(Calendar_Cal_SetDate
, wxT("Call &SetDate(2005-12-24)"), wxT("Set date to 2005-12-24."));
396 menuCal
->Append(Calendar_Cal_Today
, wxT("Call &Today()"), wxT("Set to the current date."));
397 menuCal
->Append(Calendar_Cal_BeginDST
, "Call SetDate(GetBeginDST())");
398 menuCal
->AppendSeparator();
399 menuCal
->AppendCheckItem(Calendar_Cal_Resizable
, wxT("Make &resizable\tCtrl-R"));
401 #if wxUSE_DATEPICKCTRL
402 wxMenu
*menuDate
= new wxMenu
;
403 menuDate
->AppendCheckItem(Calendar_DatePicker_ShowCentury
,
404 wxT("Al&ways show century"));
405 menuDate
->AppendCheckItem(Calendar_DatePicker_DropDown
,
406 wxT("Use &drop down control"));
407 menuDate
->AppendCheckItem(Calendar_DatePicker_AllowNone
,
408 wxT("Allow &no date"));
409 menuDate
->AppendCheckItem(Calendar_DatePicker_StartWithNone
,
410 wxT("Start &with no date"));
411 #if wxUSE_DATEPICKCTRL_GENERIC
412 menuDate
->AppendCheckItem(Calendar_DatePicker_Generic
,
413 wxT("Use &generic version of the control"));
414 #endif // wxUSE_DATEPICKCTRL_GENERIC
415 menuDate
->AppendSeparator();
416 menuDate
->Append(Calendar_DatePicker_AskDate
, wxT("&Choose date...\tCtrl-D"), wxT("Show dialog with wxDatePickerCtrl"));
417 #endif // wxUSE_DATEPICKCTRL
419 // now append the freshly created menu to the menu bar...
420 wxMenuBar
*menuBar
= new wxMenuBar
;
421 menuBar
->Append(menuFile
, wxT("&File"));
422 menuBar
->Append(menuCal
, wxT("&Calendar"));
423 #if wxUSE_DATEPICKCTRL
424 menuBar
->Append(menuDate
, wxT("&Date picker"));
425 #endif // wxUSE_DATEPICKCTRL
427 menuBar
->Check(Calendar_Cal_Monday
, true);
428 menuBar
->Check(Calendar_Cal_Holidays
, true);
429 menuBar
->Check(Calendar_Cal_Month
, true);
430 menuBar
->Check(Calendar_Cal_LimitDates
, false);
432 #if wxUSE_DATEPICKCTRL
433 menuBar
->Check(Calendar_DatePicker_ShowCentury
, true);
434 #endif // wxUSE_DATEPICKCTRL
436 // ... and attach this menu bar to the frame
439 wxSplitterWindow
*splitter
= new wxSplitterWindow(this, wxID_ANY
,
440 wxDefaultPosition
, wxDefaultSize
,
442 m_panel
= new MyPanel(splitter
);
443 m_logWindow
= new wxTextCtrl(splitter
, wxID_ANY
, wxEmptyString
,
444 wxDefaultPosition
, wxDefaultSize
,
445 wxTE_READONLY
| wxTE_MULTILINE
);
446 splitter
->SplitHorizontally(m_panel
, m_logWindow
);
447 splitter
->SetMinimumPaneSize(20);
448 wxLog::SetActiveTarget(new wxLogTextCtrl(m_logWindow
));
451 void MyFrame::OnQuit(wxCommandEvent
& WXUNUSED(event
))
453 // true is to force the frame to close
457 void MyFrame::OnAbout(wxCommandEvent
& WXUNUSED(event
))
459 wxMessageBox(wxT("wxCalendarCtrl sample\n(c) 2000--2008 Vadim Zeitlin"),
460 wxT("About Calendar"), wxOK
| wxICON_INFORMATION
, this);
463 void MyFrame::OnClearLog(wxCommandEvent
& WXUNUSED(event
))
465 m_logWindow
->Clear();
468 void MyFrame::OnCalMonday(wxCommandEvent
& event
)
470 m_panel
->ToggleCalStyle(event
.IsChecked(), wxCAL_MONDAY_FIRST
);
473 void MyFrame::OnCalHolidays(wxCommandEvent
& event
)
475 m_panel
->GetCal()->EnableHolidayDisplay(event
.IsChecked());
478 void MyFrame::OnCalSpecial(wxCommandEvent
& event
)
480 m_panel
->HighlightSpecial(GetMenuBar()->IsChecked(event
.GetId()));
483 void MyFrame::OnCalLimitDates(wxCommandEvent
& event
)
485 m_panel
->LimitDateRange(GetMenuBar()->IsChecked(event
.GetId()));
488 void MyFrame::OnCalAllowMonth(wxCommandEvent
& event
)
490 m_panel
->GetCal()->EnableMonthChange(event
.IsChecked());
493 void MyFrame::OnCalSeqMonth(wxCommandEvent
& event
)
495 m_panel
->ToggleCalStyle(event
.IsChecked(),
496 wxCAL_SEQUENTIAL_MONTH_SELECTION
);
499 void MyFrame::OnCalShowSurroundingWeeks(wxCommandEvent
& event
)
501 m_panel
->ToggleCalStyle(event
.IsChecked(), wxCAL_SHOW_SURROUNDING_WEEKS
);
504 void MyFrame::OnCalShowWeekNumbers(wxCommandEvent
& event
)
506 m_panel
->ToggleCalStyle(event
.IsChecked(), wxCAL_SHOW_WEEK_NUMBERS
);
509 void MyFrame::OnSetDate(wxCommandEvent
&WXUNUSED(event
))
511 m_panel
->SetDate(wxDateTime(24, wxDateTime::Dec
, 2005, 22, 00, 00));
514 void MyFrame::OnToday(wxCommandEvent
&WXUNUSED(event
))
516 m_panel
->SetDate(wxDateTime::Today());
519 void MyFrame::OnBeginDST(wxCommandEvent
&WXUNUSED(event
))
521 m_panel
->SetDate(wxDateTime::GetBeginDST(m_panel
->GetDate().GetYear()));
524 void MyFrame::OnCalToggleResizable(wxCommandEvent
& event
)
526 wxSizer
* const sizer
= m_panel
->GetSizer();
527 wxSizerItem
* const item
= sizer
->GetItem(m_panel
->GetCal());
528 if ( event
.IsChecked() )
530 item
->SetProportion(1);
531 item
->SetFlag(wxEXPAND
);
533 else // not resizable
535 item
->SetProportion(0);
536 item
->SetFlag(wxALIGN_CENTER
);
542 void MyFrame::OnCalRClick(wxMouseEvent
& event
)
545 wxDateTime::WeekDay wd
;
547 const wxPoint pt
= event
.GetPosition();
548 wxString msg
= wxString::Format("Point (%d, %d) is ", pt
.x
, pt
.y
);
550 switch ( m_panel
->GetCal()->HitTest(pt
, &dt
, &wd
) )
553 wxFAIL_MSG( "unexpected" );
556 case wxCAL_HITTEST_NOWHERE
:
560 case wxCAL_HITTEST_HEADER
:
561 msg
+= wxString::Format("over %s", wxDateTime::GetWeekDayName(wd
));
564 case wxCAL_HITTEST_DAY
:
565 msg
+= wxString::Format("over %s", dt
.FormatISODate());
568 case wxCAL_HITTEST_INCMONTH
:
569 msg
+= "over next month button";
572 case wxCAL_HITTEST_DECMONTH
:
573 msg
+= "over previous month button";
576 case wxCAL_HITTEST_SURROUNDING_WEEK
:
577 msg
+= "over a day from another month";
581 wxLogMessage("%s", msg
);
584 #if wxUSE_DATEPICKCTRL
586 void MyFrame::OnUpdateUIStartWithNone(wxUpdateUIEvent
& event
)
588 // it only makes sense to start with invalid date if we can have no date
589 event
.Enable( GetMenuBar()->IsChecked(Calendar_DatePicker_AllowNone
) );
592 void MyFrame::OnAskDate(wxCommandEvent
& WXUNUSED(event
))
594 wxDateTime dt
= m_panel
->GetCal()->GetDate();
596 int style
= wxDP_DEFAULT
;
597 if ( GetMenuBar()->IsChecked(Calendar_DatePicker_ShowCentury
) )
598 style
|= wxDP_SHOWCENTURY
;
599 if ( GetMenuBar()->IsChecked(Calendar_DatePicker_DropDown
) )
600 style
|= wxDP_DROPDOWN
;
601 if ( GetMenuBar()->IsChecked(Calendar_DatePicker_AllowNone
) )
603 style
|= wxDP_ALLOWNONE
;
605 if ( GetMenuBar()->IsChecked(Calendar_DatePicker_StartWithNone
) )
606 dt
= wxDefaultDateTime
;
609 MyDialog
dlg(this, dt
, style
);
610 if ( dlg
.ShowModal() == wxID_OK
)
615 const wxDateTime today
= wxDateTime::Today();
617 if ( dt
.GetDay() == today
.GetDay() &&
618 dt
.GetMonth() == today
.GetMonth() )
620 wxMessageBox(wxT("Happy birthday!"), wxT("Calendar Sample"));
623 m_panel
->SetDate(dt
);
625 wxLogStatus(wxT("Changed the date to your input"));
629 wxLogStatus(wxT("No date entered"));
634 #endif // wxUSE_DATEPICKCTRL
636 // ----------------------------------------------------------------------------
638 // ----------------------------------------------------------------------------
640 MyPanel::MyPanel(wxWindow
*parent
)
641 : wxPanel(parent
, wxID_ANY
)
643 #ifdef wxHAS_NATIVE_CALENDARCTRL
644 m_usingGeneric
= false;
646 m_usingGeneric
= true;
650 date
.Printf(wxT("Selected date: %s"),
651 wxDateTime::Today().FormatISODate().c_str());
652 m_date
= new wxStaticText(this, wxID_ANY
, date
);
653 m_calendar
= DoCreateCalendar(wxDefaultDateTime
,
654 wxCAL_MONDAY_FIRST
| wxCAL_SHOW_HOLIDAYS
);
656 // adjust to vertical/horizontal display, check mostly dedicated to WinCE
657 bool horizontal
= ( wxSystemSettings::GetMetric(wxSYS_SCREEN_X
) > wxSystemSettings::GetMetric(wxSYS_SCREEN_Y
) );
658 m_sizer
= new wxBoxSizer( horizontal
? wxHORIZONTAL
: wxVERTICAL
);
660 m_sizer
->Add(m_date
, 0, wxALIGN_CENTER
| wxALL
, 10 );
661 m_sizer
->Add(m_calendar
, 0, wxALIGN_CENTER
| wxALIGN_LEFT
);
664 m_sizer
->SetSizeHints( this );
667 void MyPanel::OnCalendar(wxCalendarEvent
& event
)
669 // clicking the same date twice unmarks it (convenient for testing)
670 static wxDateTime s_dateLast
;
671 const bool mark
= !s_dateLast
.IsValid() || event
.GetDate() != s_dateLast
;
673 s_dateLast
= event
.GetDate();
675 m_calendar
->Mark(event
.GetDate().GetDay(), mark
);
676 wxLogMessage(wxT("Selected (and %smarked) %s from calendar."),
677 mark
? "" : "un", s_dateLast
.FormatISODate().c_str());
680 void MyPanel::OnCalendarChange(wxCalendarEvent
& event
)
683 s
.Printf(wxT("Selected date: %s"), event
.GetDate().FormatISODate().c_str());
689 void MyPanel::OnCalMonthChange(wxCalendarEvent
& event
)
691 wxLogStatus(wxT("Calendar month changed to %s %d"),
692 wxDateTime::GetMonthName(event
.GetDate().GetMonth()),
693 event
.GetDate().GetYear());
696 void MyPanel::OnCalendarWeekDayClick(wxCalendarEvent
& event
)
698 wxLogMessage(wxT("Clicked on %s"),
699 wxDateTime::GetWeekDayName(event
.GetWeekDay()).c_str());
702 void MyPanel::OnCalendarWeekClick(wxCalendarEvent
& event
)
704 wxLogMessage(wxT("Clicked on week %d"), event
.GetDate().GetWeekOfYear());
707 wxCalendarCtrlBase
*MyPanel::DoCreateCalendar(const wxDateTime
& dt
, long style
)
709 wxCalendarCtrlBase
*calendar
;
710 #ifdef wxHAS_NATIVE_CALENDARCTRL
711 if ( m_usingGeneric
)
712 calendar
= new wxGenericCalendarCtrl(this, Calendar_CalCtrl
,
718 #endif // wxHAS_NATIVE_CALENDARCTRL
719 calendar
= new wxCalendarCtrl(this, Calendar_CalCtrl
,
725 calendar
->Connect(wxEVT_RIGHT_DOWN
,
726 wxMouseEventHandler(MyFrame::OnCalRClick
),
728 ( MyFrame
* )wxGetTopLevelParent(this));
733 void MyPanel::RecreateCalendar(long style
)
735 wxCalendarCtrlBase
*calendar
= DoCreateCalendar(m_calendar
->GetDate(), style
);
737 m_sizer
->Replace(m_calendar
, calendar
);
739 m_calendar
= calendar
;
744 void MyPanel::ToggleCalStyle(bool on
, int flag
)
746 long style
= m_calendar
->GetWindowStyle();
752 if ( flag
== wxCAL_SEQUENTIAL_MONTH_SELECTION
753 || flag
== wxCAL_SHOW_WEEK_NUMBERS
)
755 // changing this style requires recreating the control
756 RecreateCalendar(style
);
758 else // just changing the style is enough
760 m_calendar
->SetWindowStyle(style
);
762 m_calendar
->Refresh();
766 void MyPanel::HighlightSpecial(bool on
)
771 *attrRedCircle
= new wxCalendarDateAttr(wxCAL_BORDER_ROUND
, *wxRED
),
772 *attrGreenSquare
= new wxCalendarDateAttr(wxCAL_BORDER_SQUARE
, *wxGREEN
),
773 *attrHeaderLike
= new wxCalendarDateAttr(*wxBLUE
, *wxLIGHT_GREY
);
775 m_calendar
->SetAttr(17, attrRedCircle
);
776 m_calendar
->SetAttr(29, attrGreenSquare
);
777 m_calendar
->SetAttr(13, attrHeaderLike
);
781 m_calendar
->ResetAttr(17);
782 m_calendar
->ResetAttr(29);
783 m_calendar
->ResetAttr(13);
786 m_calendar
->Refresh();
789 // Toggle a restricted date range to the six months centered on today's date.
790 void MyPanel::LimitDateRange(bool on
)
794 // limit the choice of date to 3 months around today
795 const wxDateSpan diff
= wxDateSpan::Months(3);
796 const wxDateTime today
= wxDateTime::Today();
798 // Set the restricted date range.
799 if ( m_calendar
->SetDateRange(today
- diff
, today
+ diff
) )
801 wxLogStatus("Date range limited to 3 months around today.");
802 wxDateTime firstValidDate
;
803 wxDateTime lastValidDate
;
804 if ( m_calendar
->GetDateRange(&firstValidDate
, &lastValidDate
) )
806 wxLogMessage("First valid date: %s, last valid date: %s",
807 firstValidDate
.FormatISODate(),
808 lastValidDate
.FormatISODate());
812 wxLogWarning("Failed to get back the valid dates range.");
817 wxLogWarning("Date range not supported.");
822 // Remove the date restrictions.
823 if ( m_calendar
->SetDateRange() )
825 wxLogStatus("Date choice is unlimited now.");
829 wxLogWarning("Date range not supported.");
833 m_calendar
->Refresh();
836 // ----------------------------------------------------------------------------
838 // ----------------------------------------------------------------------------
840 #if wxUSE_DATEPICKCTRL
842 MyDialog::MyDialog(wxWindow
*parent
, const wxDateTime
& dt
, int dtpStyle
)
843 : wxDialog(parent
, wxID_ANY
, wxString(wxT("Calendar: Choose a date")))
845 wxStdDialogButtonSizer
*sizerBtns
= new wxStdDialogButtonSizer
;
846 sizerBtns
->AddButton(new wxButton(this, wxID_OK
));
847 sizerBtns
->AddButton(new wxButton(this, wxID_CANCEL
));
848 sizerBtns
->Realize();
850 wxSizer
*sizerText
= new wxBoxSizer(wxHORIZONTAL
);
851 sizerText
->Add(new wxStaticText(this, wxID_ANY
, wxT("Date in ISO format: ")),
852 wxSizerFlags().Border().Align(wxALIGN_CENTRE_VERTICAL
));
853 m_text
= new wxTextCtrl(this, wxID_ANY
);
854 sizerText
->Add(m_text
, wxSizerFlags().
855 Expand().Border().Align(wxALIGN_CENTRE_VERTICAL
));
857 wxSizer
*sizerTop
= new wxBoxSizer(wxVERTICAL
);
858 sizerTop
->Add(new wxStaticText
861 wxT("Enter your birthday date (not before 20th century):")
863 wxSizerFlags().Border());
865 #if wxUSE_DATEPICKCTRL_GENERIC
866 wxFrame
*frame
= (wxFrame
*)wxGetTopLevelParent(parent
);
867 if ( frame
&& frame
->GetMenuBar()->IsChecked(Calendar_DatePicker_Generic
) )
868 m_datePicker
= new wxDatePickerCtrlGeneric(this, wxID_ANY
, dt
,
873 #endif // wxUSE_DATEPICKCTRL_GENERIC
874 m_datePicker
= new wxDatePickerCtrl(this, wxID_ANY
, dt
,
875 wxDefaultPosition
, wxDefaultSize
,
877 m_datePicker
->SetRange(wxDateTime(1, wxDateTime::Jan
, 1900),
879 sizerTop
->Add(m_datePicker
, wxSizerFlags().Expand().Border());
881 sizerTop
->AddStretchSpacer(1);
882 sizerTop
->Add(sizerText
);
884 sizerTop
->Add(sizerBtns
, wxSizerFlags().Centre().Border());
886 SetSizerAndFit(sizerTop
);
890 void MyDialog::OnDateChange(wxDateEvent
& event
)
892 const wxDateTime dt
= event
.GetDate();
894 m_text
->SetValue(dt
.FormatISODate());
896 m_text
->SetValue(wxEmptyString
);
899 #endif // wxUSE_DATEPICKCTRL