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"
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 #include "../sample.xpm"
55 // ----------------------------------------------------------------------------
57 // ----------------------------------------------------------------------------
59 // Define a new application type, each program should derive a class from wxApp
60 class MyApp
: public wxApp
63 // override base class virtuals
64 // ----------------------------
66 // this one is called on application startup and is a good place for the app
67 // initialization (doing it here and not in the ctor allows to have an error
68 // return: if OnInit() returns false, the application terminates)
69 virtual bool OnInit();
72 class MyPanel
: public wxPanel
75 MyPanel(wxFrame
*frame
);
77 void OnCalendar(wxCalendarEvent
& event
);
78 void OnCalendarWeekDayClick(wxCalendarEvent
& event
);
79 void OnCalendarChange(wxCalendarEvent
& event
);
80 void OnCalMonthChange(wxCalendarEvent
& event
);
81 void OnCalYearChange(wxCalendarEvent
& event
);
83 wxCalendarCtrl
*GetCal() const { return m_calendar
; }
85 // turn on/off the specified style bit on the calendar control
86 void ToggleCalStyle(bool on
, int style
);
88 void HighlightSpecial(bool on
);
94 wxCalendarCtrl
*m_calendar
;
100 // Define a new frame type: this is going to be our main frame
101 class MyFrame
: public wxFrame
105 MyFrame(const wxString
& title
, const wxPoint
& pos
= wxDefaultPosition
, const wxSize
& size
= wxDefaultSize
);
107 // event handlers (these functions should _not_ be virtual)
108 void OnQuit(wxCommandEvent
& event
);
109 void OnAbout(wxCommandEvent
& event
);
111 #if wxUSE_DATEPICKCTRL
112 void OnAskDate(wxCommandEvent
& event
);
114 void OnUpdateUIStartWithNone(wxUpdateUIEvent
& event
);
115 #endif // wxUSE_DATEPICKCTRL
117 void OnCalMonday(wxCommandEvent
& event
);
118 void OnCalHolidays(wxCommandEvent
& event
);
119 void OnCalSpecial(wxCommandEvent
& event
);
121 void OnCalAllowMonth(wxCommandEvent
& event
);
122 void OnCalAllowYear(wxCommandEvent
& event
);
124 void OnCalSeqMonth(wxCommandEvent
& event
);
125 void OnCalShowSurroundingWeeks(wxCommandEvent
& event
);
127 void OnSetDate(wxCommandEvent
& event
);
128 void OnToday(wxCommandEvent
& event
);
130 void OnAllowYearUpdate(wxUpdateUIEvent
& event
);
135 // any class wishing to process wxWidgets events must use this macro
136 DECLARE_EVENT_TABLE()
139 #if wxUSE_DATEPICKCTRL
141 // Define a simple modal dialog which asks the user for a date
142 class MyDialog
: public wxDialog
145 MyDialog(wxWindow
*parent
, const wxDateTime
& dt
, int dtpStyle
);
147 wxDateTime
GetDate() const { return m_datePicker
->GetValue(); }
150 void OnDateChange(wxDateEvent
& event
);
153 wxDatePickerCtrlBase
*m_datePicker
;
157 DECLARE_EVENT_TABLE()
160 #endif // wxUSE_DATEPICKCTRL
162 // ----------------------------------------------------------------------------
164 // ----------------------------------------------------------------------------
166 // IDs for the controls and the menu commands
170 Calendar_File_About
= wxID_ABOUT
,
171 Calendar_File_Quit
= wxID_EXIT
,
172 Calendar_Cal_Monday
= 200,
173 Calendar_Cal_Holidays
,
174 Calendar_Cal_Special
,
177 Calendar_Cal_SeqMonth
,
178 Calendar_Cal_SurroundWeeks
,
179 Calendar_Cal_SetDate
,
181 #if wxUSE_DATEPICKCTRL
182 Calendar_DatePicker_AskDate
= 300,
183 Calendar_DatePicker_ShowCentury
,
184 Calendar_DatePicker_DropDown
,
185 Calendar_DatePicker_AllowNone
,
186 Calendar_DatePicker_StartWithNone
,
187 #if wxUSE_DATEPICKCTRL_GENERIC
188 Calendar_DatePicker_Generic
,
189 #endif // wxUSE_DATEPICKCTRL_GENERIC
190 #endif // wxUSE_DATEPICKCTRL
191 Calendar_CalCtrl
= 1000
194 // ----------------------------------------------------------------------------
195 // event tables and other macros for wxWidgets
196 // ----------------------------------------------------------------------------
198 // the event tables connect the wxWidgets events with the functions (event
199 // handlers) which process them. It can be also done at run-time, but for the
200 // simple menu events like this the static method is much simpler.
201 BEGIN_EVENT_TABLE(MyFrame
, wxFrame
)
202 EVT_MENU(Calendar_File_Quit
, MyFrame::OnQuit
)
203 EVT_MENU(Calendar_File_About
, MyFrame::OnAbout
)
205 #if wxUSE_DATEPICKCTRL
206 EVT_MENU(Calendar_DatePicker_AskDate
, MyFrame::OnAskDate
)
208 EVT_UPDATE_UI(Calendar_DatePicker_StartWithNone
,
209 MyFrame::OnUpdateUIStartWithNone
)
210 #endif // wxUSE_DATEPICKCTRL
212 EVT_MENU(Calendar_Cal_Monday
, MyFrame::OnCalMonday
)
213 EVT_MENU(Calendar_Cal_Holidays
, MyFrame::OnCalHolidays
)
214 EVT_MENU(Calendar_Cal_Special
, MyFrame::OnCalSpecial
)
216 EVT_MENU(Calendar_Cal_Month
, MyFrame::OnCalAllowMonth
)
217 EVT_MENU(Calendar_Cal_Year
, MyFrame::OnCalAllowYear
)
219 EVT_MENU(Calendar_Cal_SeqMonth
, MyFrame::OnCalSeqMonth
)
220 EVT_MENU(Calendar_Cal_SurroundWeeks
, MyFrame::OnCalShowSurroundingWeeks
)
222 EVT_MENU(Calendar_Cal_SetDate
, MyFrame::OnSetDate
)
223 EVT_MENU(Calendar_Cal_Today
, MyFrame::OnToday
)
226 EVT_UPDATE_UI(Calendar_Cal_Year
, MyFrame::OnAllowYearUpdate
)
229 BEGIN_EVENT_TABLE(MyPanel
, wxPanel
)
230 EVT_CALENDAR (Calendar_CalCtrl
, MyPanel::OnCalendar
)
231 EVT_CALENDAR_MONTH (Calendar_CalCtrl
, MyPanel::OnCalMonthChange
)
232 EVT_CALENDAR_YEAR (Calendar_CalCtrl
, MyPanel::OnCalYearChange
)
233 EVT_CALENDAR_SEL_CHANGED(Calendar_CalCtrl
, MyPanel::OnCalendarChange
)
234 EVT_CALENDAR_WEEKDAY_CLICKED(Calendar_CalCtrl
, MyPanel::OnCalendarWeekDayClick
)
237 #if wxUSE_DATEPICKCTRL
239 BEGIN_EVENT_TABLE(MyDialog
, wxDialog
)
240 EVT_DATE_CHANGED(wxID_ANY
, MyDialog::OnDateChange
)
243 #endif // wxUSE_DATEPICKCTRL
245 // Create a new application object: this macro will allow wxWidgets to create
246 // the application object during program execution (it's better than using a
247 // static object for many reasons) and also declares the accessor function
248 // wxGetApp() which will return the reference of the right type (i.e. MyApp and
252 // ============================================================================
254 // ============================================================================
256 // ----------------------------------------------------------------------------
257 // the application class
258 // ----------------------------------------------------------------------------
260 // `Main program' equivalent: the program execution "starts" here
263 // Create the main application window
264 MyFrame
*frame
= new MyFrame(_T("Calendar wxWidgets sample")
266 ,wxPoint(50, 50), wxSize(450, 340)
272 // success: wxApp::OnRun() will be called which will enter the main message
273 // loop and the application will run. If we returned false here, the
274 // application would exit immediately.
278 // ----------------------------------------------------------------------------
280 // ----------------------------------------------------------------------------
283 MyFrame::MyFrame(const wxString
& title
, const wxPoint
& pos
, const wxSize
& size
)
284 : wxFrame((wxFrame
*)NULL
, wxID_ANY
, title
, pos
, size
)
286 // set the frame icon
287 SetIcon(wxIcon(sample_xpm
));
290 wxMenu
*menuFile
= new wxMenu
;
291 menuFile
->Append(Calendar_File_About
, _T("&About...\tCtrl-A"), _T("Show about dialog"));
292 menuFile
->AppendSeparator();
293 menuFile
->Append(Calendar_File_Quit
, _T("E&xit\tAlt-X"), _T("Quit this program"));
295 wxMenu
*menuCal
= new wxMenu
;
296 menuCal
->Append(Calendar_Cal_Monday
,
297 _T("Monday &first weekday\tCtrl-F"),
298 _T("Toggle between Mon and Sun as the first week day"),
300 menuCal
->Append(Calendar_Cal_Holidays
, _T("Show &holidays\tCtrl-H"),
301 _T("Toggle highlighting the holidays"),
303 menuCal
->Append(Calendar_Cal_Special
, _T("Highlight &special dates\tCtrl-S"),
304 _T("Test custom highlighting"),
306 menuCal
->Append(Calendar_Cal_SurroundWeeks
,
307 _T("Show s&urrounding weeks\tCtrl-W"),
308 _T("Show the neighbouring weeks in the prev/next month"),
310 menuCal
->AppendSeparator();
311 menuCal
->Append(Calendar_Cal_SeqMonth
,
312 _T("To&ggle month selector style\tCtrl-G"),
313 _T("Use another style for the calendar controls"),
315 menuCal
->Append(Calendar_Cal_Month
, _T("&Month can be changed\tCtrl-M"),
316 _T("Allow changing the month in the calendar"),
318 menuCal
->Append(Calendar_Cal_Year
, _T("&Year can be changed\tCtrl-Y"),
319 _T("Allow changing the year in the calendar"),
321 menuCal
->AppendSeparator();
322 menuCal
->Append(Calendar_Cal_SetDate
, _T("Call &SetDate(2005-12-24)"), _T("Set date to 2005-12-24."));
323 menuCal
->Append(Calendar_Cal_Today
, _T("Call &Today()"), _T("Set the current date."));
325 #if wxUSE_DATEPICKCTRL
326 wxMenu
*menuDate
= new wxMenu
;
327 menuDate
->AppendCheckItem(Calendar_DatePicker_ShowCentury
,
328 _T("Al&ways show century"));
329 menuDate
->AppendCheckItem(Calendar_DatePicker_DropDown
,
330 _T("Use &drop down control"));
331 menuDate
->AppendCheckItem(Calendar_DatePicker_AllowNone
,
332 _T("Allow &no date"));
333 menuDate
->AppendCheckItem(Calendar_DatePicker_StartWithNone
,
334 _T("Start &with no date"));
335 #if wxUSE_DATEPICKCTRL_GENERIC
336 menuDate
->AppendCheckItem(Calendar_DatePicker_Generic
,
337 _T("Use &generic version of the control"));
338 #endif // wxUSE_DATEPICKCTRL_GENERIC
339 menuDate
->AppendSeparator();
340 menuDate
->Append(Calendar_DatePicker_AskDate
, _T("&Choose date...\tCtrl-D"), _T("Show dialog with wxDatePickerCtrl"));
341 #endif // wxUSE_DATEPICKCTRL
343 // now append the freshly created menu to the menu bar...
344 wxMenuBar
*menuBar
= new wxMenuBar
;
345 menuBar
->Append(menuFile
, _T("&File"));
346 menuBar
->Append(menuCal
, _T("&Calendar"));
347 #if wxUSE_DATEPICKCTRL
348 menuBar
->Append(menuDate
, _T("&Date picker"));
349 #endif // wxUSE_DATEPICKCTRL
351 menuBar
->Check(Calendar_Cal_Monday
, true);
352 menuBar
->Check(Calendar_Cal_Holidays
, true);
353 menuBar
->Check(Calendar_Cal_Month
, true);
354 menuBar
->Check(Calendar_Cal_Year
, true);
356 #if wxUSE_DATEPICKCTRL
357 menuBar
->Check(Calendar_DatePicker_ShowCentury
, true);
358 #endif // wxUSE_DATEPICKCTRL
360 // ... and attach this menu bar to the frame
363 m_panel
= new MyPanel(this);
366 // create a status bar just for fun (by default with 1 pane only)
368 SetStatusText(_T("Welcome to wxWidgets!"));
369 #endif // wxUSE_STATUSBAR
372 void MyFrame::OnQuit(wxCommandEvent
& WXUNUSED(event
))
374 // true is to force the frame to close
378 void MyFrame::OnAbout(wxCommandEvent
& WXUNUSED(event
))
380 wxMessageBox(_T("wxCalendarCtrl sample\n(c) 2000 Vadim Zeitlin"),
381 _T("About Calendar"), wxOK
| wxICON_INFORMATION
, this);
384 void MyFrame::OnCalMonday(wxCommandEvent
& event
)
386 bool enable
= GetMenuBar()->IsChecked(event
.GetId());
388 m_panel
->ToggleCalStyle(enable
, wxCAL_MONDAY_FIRST
);
391 void MyFrame::OnCalHolidays(wxCommandEvent
& event
)
393 bool enable
= GetMenuBar()->IsChecked(event
.GetId());
395 m_panel
->GetCal()->EnableHolidayDisplay(enable
);
398 void MyFrame::OnCalSpecial(wxCommandEvent
& event
)
400 m_panel
->HighlightSpecial(GetMenuBar()->IsChecked(event
.GetId()));
403 void MyFrame::OnCalAllowMonth(wxCommandEvent
& event
)
405 bool allow
= GetMenuBar()->IsChecked(event
.GetId());
407 m_panel
->GetCal()->EnableMonthChange(allow
);
410 void MyFrame::OnCalAllowYear(wxCommandEvent
& event
)
412 bool allow
= GetMenuBar()->IsChecked(event
.GetId());
414 m_panel
->GetCal()->EnableYearChange(allow
);
417 void MyFrame::OnCalSeqMonth(wxCommandEvent
& event
)
419 bool allow
= GetMenuBar()->IsChecked(event
.GetId());
421 m_panel
->ToggleCalStyle(allow
, wxCAL_SEQUENTIAL_MONTH_SELECTION
);
424 void MyFrame::OnCalShowSurroundingWeeks(wxCommandEvent
& event
)
426 bool allow
= GetMenuBar()->IsChecked(event
.GetId());
428 m_panel
->ToggleCalStyle(allow
, wxCAL_SHOW_SURROUNDING_WEEKS
);
431 void MyFrame::OnAllowYearUpdate(wxUpdateUIEvent
& event
)
433 event
.Enable( GetMenuBar()->IsChecked(Calendar_Cal_Month
));
436 void MyFrame::OnSetDate(wxCommandEvent
&WXUNUSED(event
))
441 void MyFrame::OnToday(wxCommandEvent
&WXUNUSED(event
))
446 #if wxUSE_DATEPICKCTRL
448 void MyFrame::OnUpdateUIStartWithNone(wxUpdateUIEvent
& event
)
450 // it only makes sense to start with invalid date if we can have no date
451 event
.Enable( GetMenuBar()->IsChecked(Calendar_DatePicker_AllowNone
) );
454 void MyFrame::OnAskDate(wxCommandEvent
& WXUNUSED(event
))
456 wxDateTime dt
= m_panel
->GetCal()->GetDate();
458 int style
= wxDP_DEFAULT
;
459 if ( GetMenuBar()->IsChecked(Calendar_DatePicker_ShowCentury
) )
460 style
|= wxDP_SHOWCENTURY
;
461 if ( GetMenuBar()->IsChecked(Calendar_DatePicker_DropDown
) )
462 style
|= wxDP_DROPDOWN
;
463 if ( GetMenuBar()->IsChecked(Calendar_DatePicker_AllowNone
) )
465 style
|= wxDP_ALLOWNONE
;
467 if ( GetMenuBar()->IsChecked(Calendar_DatePicker_StartWithNone
) )
468 dt
= wxDefaultDateTime
;
471 MyDialog
dlg(this, dt
, style
);
472 if ( dlg
.ShowModal() == wxID_OK
)
477 const wxDateTime today
= wxDateTime::Today();
479 if ( dt
.GetDay() == today
.GetDay() &&
480 dt
.GetMonth() == today
.GetMonth() )
482 wxMessageBox(_T("Happy birthday!"), _T("Calendar Sample"));
485 m_panel
->GetCal()->SetDate(dt
);
487 wxLogStatus(_T("Changed the date to your input"));
491 wxLogStatus(_T("No date entered"));
496 #endif // wxUSE_DATEPICKCTRL
498 // ----------------------------------------------------------------------------
500 // ----------------------------------------------------------------------------
502 MyPanel::MyPanel(wxFrame
*frame
)
503 : wxPanel(frame
, wxID_ANY
)
506 date
.Printf(wxT("Selected date: %s"),
507 wxDateTime::Today().FormatISODate().c_str());
508 m_date
= new wxStaticText(this, wxID_ANY
, date
);
509 m_calendar
= new wxCalendarCtrl(this, Calendar_CalCtrl
,
514 wxCAL_SHOW_HOLIDAYS
|
517 // adjust to vertical/horizontal display, check mostly dedicated to WinCE
518 bool horizontal
= ( wxSystemSettings::GetMetric(wxSYS_SCREEN_X
) > wxSystemSettings::GetMetric(wxSYS_SCREEN_Y
) );
519 wxBoxSizer
*m_sizer
= new wxBoxSizer( horizontal
? wxHORIZONTAL
: wxVERTICAL
);
521 m_sizer
->Add(m_date
, 0, wxALIGN_CENTER
| wxALL
, 10 );
522 m_sizer
->Add(m_calendar
, 0, wxALIGN_CENTER
| wxALIGN_LEFT
);
525 m_sizer
->SetSizeHints( this );
528 void MyPanel::OnCalendar(wxCalendarEvent
& event
)
530 wxLogMessage(wxT("Selected %s from calendar"),
531 event
.GetDate().FormatISODate().c_str());
534 void MyPanel::OnCalendarChange(wxCalendarEvent
& event
)
537 s
.Printf(wxT("Selected date: %s"), event
.GetDate().FormatISODate().c_str());
542 void MyPanel::OnCalMonthChange(wxCalendarEvent
& WXUNUSED(event
))
544 wxLogStatus(wxT("Calendar month changed"));
547 void MyPanel::OnCalYearChange(wxCalendarEvent
& WXUNUSED(event
))
549 wxLogStatus(wxT("Calendar year changed"));
552 void MyPanel::OnCalendarWeekDayClick(wxCalendarEvent
& event
)
554 wxLogMessage(wxT("Clicked on %s"),
555 wxDateTime::GetWeekDayName(event
.GetWeekDay()).c_str());
558 void MyPanel::ToggleCalStyle(bool on
, int flag
)
560 long style
= m_calendar
->GetWindowStyle();
566 m_calendar
->SetWindowStyle(style
);
568 m_calendar
->Refresh();
571 void MyPanel::HighlightSpecial(bool on
)
576 *attrRedCircle
= new wxCalendarDateAttr(wxCAL_BORDER_ROUND
, *wxRED
),
577 *attrGreenSquare
= new wxCalendarDateAttr(wxCAL_BORDER_SQUARE
, *wxGREEN
),
578 *attrHeaderLike
= new wxCalendarDateAttr(*wxBLUE
, *wxLIGHT_GREY
);
580 m_calendar
->SetAttr(17, attrRedCircle
);
581 m_calendar
->SetAttr(29, attrGreenSquare
);
582 m_calendar
->SetAttr(13, attrHeaderLike
);
586 m_calendar
->ResetAttr(17);
587 m_calendar
->ResetAttr(29);
588 m_calendar
->ResetAttr(13);
591 m_calendar
->Refresh();
594 void MyPanel::SetDate()
596 wxDateTime
date(24, wxDateTime::Dec
, 2005, 23, 59, 59);
597 m_calendar
->SetDate(date
);
600 void MyPanel::Today()
602 m_calendar
->SetDate(wxDateTime::Today());
605 // ----------------------------------------------------------------------------
607 // ----------------------------------------------------------------------------
609 #if wxUSE_DATEPICKCTRL
611 MyDialog::MyDialog(wxWindow
*parent
, const wxDateTime
& dt
, int dtpStyle
)
612 : wxDialog(parent
, wxID_ANY
, wxString(_T("Calendar: Choose a date")))
614 wxStdDialogButtonSizer
*sizerBtns
= new wxStdDialogButtonSizer
;
615 sizerBtns
->AddButton(new wxButton(this, wxID_OK
));
616 sizerBtns
->AddButton(new wxButton(this, wxID_CANCEL
));
617 sizerBtns
->Realize();
619 wxSizer
*sizerText
= new wxBoxSizer(wxHORIZONTAL
);
620 sizerText
->Add(new wxStaticText(this, wxID_ANY
, _T("Date in ISO format: ")),
621 wxSizerFlags().Border().Align(wxALIGN_CENTRE_VERTICAL
));
622 m_text
= new wxTextCtrl(this, wxID_ANY
);
623 sizerText
->Add(m_text
, wxSizerFlags().
624 Expand().Border().Align(wxALIGN_CENTRE_VERTICAL
));
626 wxSizer
*sizerTop
= new wxBoxSizer(wxVERTICAL
);
627 sizerTop
->Add(new wxStaticText
630 _T("Enter your birthday date (not before 20th century):")
632 wxSizerFlags().Border());
634 #if wxUSE_DATEPICKCTRL_GENERIC
635 wxFrame
*frame
= (wxFrame
*)wxGetTopLevelParent(parent
);
636 if ( frame
&& frame
->GetMenuBar()->IsChecked(Calendar_DatePicker_Generic
) )
637 m_datePicker
= new wxDatePickerCtrlGeneric(this, wxID_ANY
, dt
,
642 #endif // wxUSE_DATEPICKCTRL_GENERIC
643 m_datePicker
= new wxDatePickerCtrl(this, wxID_ANY
, dt
,
644 wxDefaultPosition
, wxDefaultSize
,
646 m_datePicker
->SetRange(wxDateTime(1, wxDateTime::Jan
, 1900),
648 sizerTop
->Add(m_datePicker
, wxSizerFlags().Expand().Border());
650 sizerTop
->AddStretchSpacer(1);
651 sizerTop
->Add(sizerText
);
653 sizerTop
->Add(sizerBtns
, wxSizerFlags().Centre().Border());
655 SetSizerAndFit(sizerTop
);
659 void MyDialog::OnDateChange(wxDateEvent
& event
)
661 const wxDateTime dt
= event
.GetDate();
663 m_text
->SetValue(dt
.FormatISODate());
665 m_text
->SetValue(wxEmptyString
);
668 #endif // wxUSE_DATEPICKCTRL