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"
40 #include "wx/textctrl.h"
42 #include "wx/calctrl.h"
44 #if wxUSE_DATEPICKCTRL
45 #include "wx/datectrl.h"
46 #if wxUSE_DATEPICKCTRL_GENERIC
47 #include "wx/generic/datectrl.h"
48 #endif // wxUSE_DATEPICKCTRL_GENERIC
49 #endif // wxUSE_DATEPICKCTRL
51 // the application icon (under Windows and OS/2 it is in resources and even
52 // though we could still include the XPM here it would be unused)
53 #if !defined(__WXMSW__) && !defined(__WXPM__)
54 #include "../sample.xpm"
57 // ----------------------------------------------------------------------------
59 // ----------------------------------------------------------------------------
61 // Define a new application type, each program should derive a class from wxApp
62 class MyApp
: public wxApp
65 // override base class virtuals
66 // ----------------------------
68 // this one is called on application startup and is a good place for the app
69 // initialization (doing it here and not in the ctor allows to have an error
70 // return: if OnInit() returns false, the application terminates)
71 virtual bool OnInit();
74 class MyPanel
: public wxPanel
77 MyPanel(wxFrame
*frame
);
79 void OnCalendar(wxCalendarEvent
& event
);
80 void OnCalendarWeekDayClick(wxCalendarEvent
& event
);
81 void OnCalendarChange(wxCalendarEvent
& event
);
82 void OnCalMonthChange(wxCalendarEvent
& event
);
83 void OnCalYearChange(wxCalendarEvent
& event
);
85 wxCalendarCtrl
*GetCal() const { return m_calendar
; }
87 // turn on/off the specified style bit on the calendar control
88 void ToggleCalStyle(bool on
, int style
);
90 void HighlightSpecial(bool on
);
96 wxCalendarCtrl
*m_calendar
;
102 // Define a new frame type: this is going to be our main frame
103 class MyFrame
: public wxFrame
107 MyFrame(const wxString
& title
, const wxPoint
& pos
, const wxSize
& size
);
109 // event handlers (these functions should _not_ be virtual)
110 void OnQuit(wxCommandEvent
& event
);
111 void OnAbout(wxCommandEvent
& event
);
113 #if wxUSE_DATEPICKCTRL
114 void OnAskDate(wxCommandEvent
& 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 #if wxUSE_DATEPICKCTRL_GENERIC
187 Calendar_DatePicker_Generic
,
188 #endif // wxUSE_DATEPICKCTRL_GENERIC
189 #endif // wxUSE_DATEPICKCTRL
190 Calendar_CalCtrl
= 1000
193 // ----------------------------------------------------------------------------
194 // event tables and other macros for wxWidgets
195 // ----------------------------------------------------------------------------
197 // the event tables connect the wxWidgets events with the functions (event
198 // handlers) which process them. It can be also done at run-time, but for the
199 // simple menu events like this the static method is much simpler.
200 BEGIN_EVENT_TABLE(MyFrame
, wxFrame
)
201 EVT_MENU(Calendar_File_Quit
, MyFrame::OnQuit
)
202 EVT_MENU(Calendar_File_About
, MyFrame::OnAbout
)
204 #if wxUSE_DATEPICKCTRL
205 EVT_MENU(Calendar_DatePicker_AskDate
, MyFrame::OnAskDate
)
206 #endif // wxUSE_DATEPICKCTRL
208 EVT_MENU(Calendar_Cal_Monday
, MyFrame::OnCalMonday
)
209 EVT_MENU(Calendar_Cal_Holidays
, MyFrame::OnCalHolidays
)
210 EVT_MENU(Calendar_Cal_Special
, MyFrame::OnCalSpecial
)
212 EVT_MENU(Calendar_Cal_Month
, MyFrame::OnCalAllowMonth
)
213 EVT_MENU(Calendar_Cal_Year
, MyFrame::OnCalAllowYear
)
215 EVT_MENU(Calendar_Cal_SeqMonth
, MyFrame::OnCalSeqMonth
)
216 EVT_MENU(Calendar_Cal_SurroundWeeks
, MyFrame::OnCalShowSurroundingWeeks
)
218 EVT_MENU(Calendar_Cal_SetDate
, MyFrame::OnSetDate
)
219 EVT_MENU(Calendar_Cal_Today
, MyFrame::OnToday
)
222 EVT_UPDATE_UI(Calendar_Cal_Year
, MyFrame::OnAllowYearUpdate
)
225 BEGIN_EVENT_TABLE(MyPanel
, wxPanel
)
226 EVT_CALENDAR (Calendar_CalCtrl
, MyPanel::OnCalendar
)
227 EVT_CALENDAR_MONTH (Calendar_CalCtrl
, MyPanel::OnCalMonthChange
)
228 EVT_CALENDAR_YEAR (Calendar_CalCtrl
, MyPanel::OnCalYearChange
)
229 EVT_CALENDAR_SEL_CHANGED(Calendar_CalCtrl
, MyPanel::OnCalendarChange
)
230 EVT_CALENDAR_WEEKDAY_CLICKED(Calendar_CalCtrl
, MyPanel::OnCalendarWeekDayClick
)
233 #if wxUSE_DATEPICKCTRL
235 BEGIN_EVENT_TABLE(MyDialog
, wxDialog
)
236 EVT_DATE_CHANGED(wxID_ANY
, MyDialog::OnDateChange
)
239 #endif // wxUSE_DATEPICKCTRL
241 // Create a new application object: this macro will allow wxWidgets to create
242 // the application object during program execution (it's better than using a
243 // static object for many reasons) and also declares the accessor function
244 // wxGetApp() which will return the reference of the right type (i.e. MyApp and
248 // ============================================================================
250 // ============================================================================
252 // ----------------------------------------------------------------------------
253 // the application class
254 // ----------------------------------------------------------------------------
256 // `Main program' equivalent: the program execution "starts" here
259 // Create the main application window
260 MyFrame
*frame
= new MyFrame(_T("Calendar wxWidgets sample"),
261 wxPoint(50, 50), wxSize(450, 340));
265 // success: wxApp::OnRun() will be called which will enter the main message
266 // loop and the application will run. If we returned false here, the
267 // application would exit immediately.
271 // ----------------------------------------------------------------------------
273 // ----------------------------------------------------------------------------
276 MyFrame::MyFrame(const wxString
& title
, const wxPoint
& pos
, const wxSize
& size
)
277 : wxFrame((wxFrame
*)NULL
, wxID_ANY
, title
, pos
, size
)
279 // set the frame icon
280 SetIcon(wxICON(sample
));
283 wxMenu
*menuFile
= new wxMenu
;
284 menuFile
->Append(Calendar_File_About
, _T("&About...\tCtrl-A"), _T("Show about dialog"));
285 menuFile
->AppendSeparator();
286 menuFile
->Append(Calendar_File_Quit
, _T("E&xit\tAlt-X"), _T("Quit this program"));
288 wxMenu
*menuCal
= new wxMenu
;
289 menuCal
->Append(Calendar_Cal_Monday
,
290 _T("Monday &first weekday\tCtrl-F"),
291 _T("Toggle between Mon and Sun as the first week day"),
293 menuCal
->Append(Calendar_Cal_Holidays
, _T("Show &holidays\tCtrl-H"),
294 _T("Toggle highlighting the holidays"),
296 menuCal
->Append(Calendar_Cal_Special
, _T("Highlight &special dates\tCtrl-S"),
297 _T("Test custom highlighting"),
299 menuCal
->Append(Calendar_Cal_SurroundWeeks
,
300 _T("Show s&urrounding weeks\tCtrl-W"),
301 _T("Show the neighbouring weeks in the prev/next month"),
303 menuCal
->AppendSeparator();
304 menuCal
->Append(Calendar_Cal_SeqMonth
,
305 _T("To&ggle month selector style\tCtrl-G"),
306 _T("Use another style for the calendar controls"),
308 menuCal
->Append(Calendar_Cal_Month
, _T("&Month can be changed\tCtrl-M"),
309 _T("Allow changing the month in the calendar"),
311 menuCal
->Append(Calendar_Cal_Year
, _T("&Year can be changed\tCtrl-Y"),
312 _T("Allow changing the year in the calendar"),
314 menuCal
->AppendSeparator();
315 menuCal
->Append(Calendar_Cal_SetDate
, _T("SetDate()"), _T("Set date to 2005-12-24."));
316 menuCal
->Append(Calendar_Cal_Today
, _T("Today()"), _T("Set the current date."));
318 #if wxUSE_DATEPICKCTRL
319 wxMenu
*menuDate
= new wxMenu
;
320 menuDate
->AppendCheckItem(Calendar_DatePicker_ShowCentury
,
321 _T("Al&ways show century"));
322 menuDate
->AppendCheckItem(Calendar_DatePicker_DropDown
,
323 _T("Use &drop down control"));
324 menuDate
->AppendCheckItem(Calendar_DatePicker_AllowNone
,
325 _T("Allow &no date"));
326 #if wxUSE_DATEPICKCTRL_GENERIC
327 menuDate
->AppendCheckItem(Calendar_DatePicker_Generic
,
328 _T("Use &generic version of the control"));
329 #endif // wxUSE_DATEPICKCTRL_GENERIC
330 menuDate
->AppendSeparator();
331 menuDate
->Append(Calendar_DatePicker_AskDate
, _T("&Choose date...\tCtrl-D"), _T("Show dialog with wxDatePickerCtrl"));
332 #endif // wxUSE_DATEPICKCTRL
334 // now append the freshly created menu to the menu bar...
335 wxMenuBar
*menuBar
= new wxMenuBar
;
336 menuBar
->Append(menuFile
, _T("&File"));
337 menuBar
->Append(menuCal
, _T("&Calendar"));
338 #if wxUSE_DATEPICKCTRL
339 menuBar
->Append(menuDate
, _T("&Date picker"));
340 #endif // wxUSE_DATEPICKCTRL
342 menuBar
->Check(Calendar_Cal_Monday
, true);
343 menuBar
->Check(Calendar_Cal_Holidays
, true);
344 menuBar
->Check(Calendar_Cal_Month
, true);
345 menuBar
->Check(Calendar_Cal_Year
, true);
347 #if wxUSE_DATEPICKCTRL
348 menuBar
->Check(Calendar_DatePicker_ShowCentury
, true);
349 #endif // wxUSE_DATEPICKCTRL
351 // ... and attach this menu bar to the frame
354 m_panel
= new MyPanel(this);
357 // create a status bar just for fun (by default with 1 pane only)
359 SetStatusText(_T("Welcome to wxWidgets!"));
360 #endif // wxUSE_STATUSBAR
363 void MyFrame::OnQuit(wxCommandEvent
& WXUNUSED(event
))
365 // true is to force the frame to close
369 void MyFrame::OnAbout(wxCommandEvent
& WXUNUSED(event
))
371 wxMessageBox(_T("wxCalendarCtrl sample\n(c) 2000 Vadim Zeitlin"),
372 _T("About Calendar"), wxOK
| wxICON_INFORMATION
, this);
375 void MyFrame::OnCalMonday(wxCommandEvent
& event
)
377 bool enable
= GetMenuBar()->IsChecked(event
.GetId());
379 m_panel
->ToggleCalStyle(enable
, wxCAL_MONDAY_FIRST
);
382 void MyFrame::OnCalHolidays(wxCommandEvent
& event
)
384 bool enable
= GetMenuBar()->IsChecked(event
.GetId());
386 m_panel
->GetCal()->EnableHolidayDisplay(enable
);
389 void MyFrame::OnCalSpecial(wxCommandEvent
& event
)
391 m_panel
->HighlightSpecial(GetMenuBar()->IsChecked(event
.GetId()));
394 void MyFrame::OnCalAllowMonth(wxCommandEvent
& event
)
396 bool allow
= GetMenuBar()->IsChecked(event
.GetId());
398 m_panel
->GetCal()->EnableMonthChange(allow
);
401 void MyFrame::OnCalAllowYear(wxCommandEvent
& event
)
403 bool allow
= GetMenuBar()->IsChecked(event
.GetId());
405 m_panel
->GetCal()->EnableYearChange(allow
);
408 void MyFrame::OnCalSeqMonth(wxCommandEvent
& event
)
410 bool allow
= GetMenuBar()->IsChecked(event
.GetId());
412 m_panel
->ToggleCalStyle(allow
, wxCAL_SEQUENTIAL_MONTH_SELECTION
);
415 void MyFrame::OnCalShowSurroundingWeeks(wxCommandEvent
& event
)
417 bool allow
= GetMenuBar()->IsChecked(event
.GetId());
419 m_panel
->ToggleCalStyle(allow
, wxCAL_SHOW_SURROUNDING_WEEKS
);
422 void MyFrame::OnAllowYearUpdate(wxUpdateUIEvent
& event
)
424 event
.Enable( GetMenuBar()->IsChecked(Calendar_Cal_Month
));
427 void MyFrame::OnSetDate(wxCommandEvent
&WXUNUSED(event
))
432 void MyFrame::OnToday(wxCommandEvent
&WXUNUSED(event
))
437 #if wxUSE_DATEPICKCTRL
439 void MyFrame::OnAskDate(wxCommandEvent
& WXUNUSED(event
))
441 int style
= wxDP_DEFAULT
;
442 if ( GetMenuBar()->IsChecked(Calendar_DatePicker_ShowCentury
) )
443 style
|= wxDP_SHOWCENTURY
;
444 if ( GetMenuBar()->IsChecked(Calendar_DatePicker_DropDown
) )
445 style
|= wxDP_DROPDOWN
;
446 if ( GetMenuBar()->IsChecked(Calendar_DatePicker_AllowNone
) )
447 style
|= wxDP_ALLOWNONE
;
449 MyDialog
dlg(this, m_panel
->GetCal()->GetDate(), style
);
450 if ( dlg
.ShowModal() == wxID_OK
)
452 const wxDateTime dt
= dlg
.GetDate();
455 const wxDateTime today
= wxDateTime::Today();
457 if ( dt
.GetDay() == today
.GetDay() &&
458 dt
.GetMonth() == today
.GetMonth() )
460 wxMessageBox(_T("Happy birthday!"), _T("Calendar Sample"));
463 m_panel
->GetCal()->SetDate(dt
);
465 wxLogStatus(_T("Changed the date to your input"));
469 wxLogStatus(_T("No date entered"));
474 #endif // wxUSE_DATEPICKCTRL
476 // ----------------------------------------------------------------------------
478 // ----------------------------------------------------------------------------
480 MyPanel::MyPanel(wxFrame
*frame
)
481 : wxPanel(frame
, wxID_ANY
)
484 date
.Printf(wxT("Selected date: %s"),
485 wxDateTime::Today().FormatISODate().c_str());
486 m_date
= new wxStaticText(this, wxID_ANY
, date
);
487 m_calendar
= new wxCalendarCtrl(this, Calendar_CalCtrl
,
492 wxCAL_SHOW_HOLIDAYS
|
495 wxBoxSizer
*m_sizer
= new wxBoxSizer( wxHORIZONTAL
);
497 m_sizer
->Add(m_date
, 0, wxALIGN_CENTER
| wxALL
, 10 );
498 m_sizer
->Add(m_calendar
, 0, wxALIGN_CENTER
| wxALIGN_LEFT
);
501 m_sizer
->SetSizeHints( this );
504 void MyPanel::OnCalendar(wxCalendarEvent
& event
)
506 wxLogMessage(wxT("Selected %s from calendar"),
507 event
.GetDate().FormatISODate().c_str());
510 void MyPanel::OnCalendarChange(wxCalendarEvent
& event
)
513 s
.Printf(wxT("Selected date: %s"), event
.GetDate().FormatISODate().c_str());
518 void MyPanel::OnCalMonthChange(wxCalendarEvent
& WXUNUSED(event
))
520 wxLogStatus(wxT("Calendar month changed"));
523 void MyPanel::OnCalYearChange(wxCalendarEvent
& WXUNUSED(event
))
525 wxLogStatus(wxT("Calendar year changed"));
528 void MyPanel::OnCalendarWeekDayClick(wxCalendarEvent
& event
)
530 wxLogMessage(wxT("Clicked on %s"),
531 wxDateTime::GetWeekDayName(event
.GetWeekDay()).c_str());
534 void MyPanel::ToggleCalStyle(bool on
, int flag
)
536 long style
= m_calendar
->GetWindowStyle();
542 m_calendar
->SetWindowStyle(style
);
544 m_calendar
->Refresh();
547 void MyPanel::HighlightSpecial(bool on
)
552 *attrRedCircle
= new wxCalendarDateAttr(wxCAL_BORDER_ROUND
, *wxRED
),
553 *attrGreenSquare
= new wxCalendarDateAttr(wxCAL_BORDER_SQUARE
, *wxGREEN
),
554 *attrHeaderLike
= new wxCalendarDateAttr(*wxBLUE
, *wxLIGHT_GREY
);
556 m_calendar
->SetAttr(17, attrRedCircle
);
557 m_calendar
->SetAttr(29, attrGreenSquare
);
558 m_calendar
->SetAttr(13, attrHeaderLike
);
562 m_calendar
->ResetAttr(17);
563 m_calendar
->ResetAttr(29);
564 m_calendar
->ResetAttr(13);
567 m_calendar
->Refresh();
570 void MyPanel::SetDate()
572 wxDateTime
date(24, wxDateTime::Dec
, 2005, 23, 59, 59);
573 m_calendar
->SetDate(date
);
576 void MyPanel::Today()
578 m_calendar
->SetDate(wxDateTime::Today());
581 // ----------------------------------------------------------------------------
583 // ----------------------------------------------------------------------------
585 #if wxUSE_DATEPICKCTRL
587 MyDialog::MyDialog(wxWindow
*parent
, const wxDateTime
& dt
, int dtpStyle
)
588 : wxDialog(parent
, wxID_ANY
, wxString(_T("Calendar: Choose a date")))
590 wxStdDialogButtonSizer
*sizerBtns
= new wxStdDialogButtonSizer
;
591 sizerBtns
->AddButton(new wxButton(this, wxID_OK
));
592 sizerBtns
->AddButton(new wxButton(this, wxID_CANCEL
));
593 sizerBtns
->Realize();
595 wxSizer
*sizerText
= new wxBoxSizer(wxHORIZONTAL
);
596 sizerText
->Add(new wxStaticText(this, wxID_ANY
, _T("Date in ISO format: ")),
597 wxSizerFlags().Border().Align(wxALIGN_CENTRE_VERTICAL
));
598 m_text
= new wxTextCtrl(this, wxID_ANY
);
599 sizerText
->Add(m_text
, wxSizerFlags().
600 Expand().Border().Align(wxALIGN_CENTRE_VERTICAL
));
602 wxSizer
*sizerTop
= new wxBoxSizer(wxVERTICAL
);
603 sizerTop
->Add(new wxStaticText
606 _T("Enter your birthday date (not before 20th century):")
608 wxSizerFlags().Border());
610 #if wxUSE_DATEPICKCTRL_GENERIC
611 wxFrame
*frame
= (wxFrame
*)wxGetTopLevelParent(parent
);
612 if ( frame
&& frame
->GetMenuBar()->IsChecked(Calendar_DatePicker_Generic
) )
613 m_datePicker
= new wxDatePickerCtrlGeneric(this, wxID_ANY
, dt
,
618 #endif // wxUSE_DATEPICKCTRL_GENERIC
619 m_datePicker
= new wxDatePickerCtrl(this, wxID_ANY
, dt
,
620 wxDefaultPosition
, wxDefaultSize
,
622 m_datePicker
->SetRange(wxDateTime(1, wxDateTime::Jan
, 1900),
624 sizerTop
->Add(m_datePicker
, wxSizerFlags().Expand().Border());
626 sizerTop
->AddStretchSpacer(1);
627 sizerTop
->Add(sizerText
);
629 sizerTop
->Add(sizerBtns
, wxSizerFlags().Centre().Border());
631 SetSizerAndFit(sizerTop
);
635 void MyDialog::OnDateChange(wxDateEvent
& event
)
637 const wxDateTime dt
= event
.GetDate();
639 m_text
->SetValue(dt
.FormatISODate());
641 m_text
->SetValue(wxEmptyString
);
644 #endif // wxUSE_DATEPICKCTRL