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 #include "../sample.xpm"
53 // ----------------------------------------------------------------------------
55 // ----------------------------------------------------------------------------
57 // Define a new application type, each program should derive a class from wxApp
58 class MyApp
: public wxApp
61 // override base class virtuals
62 // ----------------------------
64 // this one is called on application startup and is a good place for the app
65 // initialization (doing it here and not in the ctor allows to have an error
66 // return: if OnInit() returns false, the application terminates)
67 virtual bool OnInit();
70 class MyPanel
: public wxPanel
73 MyPanel(wxFrame
*frame
);
75 void OnCalendar(wxCalendarEvent
& event
);
76 void OnCalendarWeekDayClick(wxCalendarEvent
& event
);
77 void OnCalendarChange(wxCalendarEvent
& event
);
78 void OnCalMonthChange(wxCalendarEvent
& event
);
79 void OnCalYearChange(wxCalendarEvent
& event
);
81 wxCalendarCtrl
*GetCal() const { return m_calendar
; }
83 // turn on/off the specified style bit on the calendar control
84 void ToggleCalStyle(bool on
, int style
);
86 void HighlightSpecial(bool on
);
92 wxCalendarCtrl
*m_calendar
;
98 // Define a new frame type: this is going to be our main frame
99 class MyFrame
: public wxFrame
103 MyFrame(const wxString
& title
, const wxPoint
& pos
, const wxSize
& size
);
105 // event handlers (these functions should _not_ be virtual)
106 void OnQuit(wxCommandEvent
& event
);
107 void OnAbout(wxCommandEvent
& event
);
109 #if wxUSE_DATEPICKCTRL
110 void OnAskDate(wxCommandEvent
& event
);
111 #endif // wxUSE_DATEPICKCTRL
113 void OnCalMonday(wxCommandEvent
& event
);
114 void OnCalHolidays(wxCommandEvent
& event
);
115 void OnCalSpecial(wxCommandEvent
& event
);
117 void OnCalAllowMonth(wxCommandEvent
& event
);
118 void OnCalAllowYear(wxCommandEvent
& event
);
120 void OnCalSeqMonth(wxCommandEvent
& event
);
121 void OnCalShowSurroundingWeeks(wxCommandEvent
& event
);
123 void OnSetDate(wxCommandEvent
& event
);
124 void OnToday(wxCommandEvent
& event
);
126 void OnAllowYearUpdate(wxUpdateUIEvent
& event
);
131 // any class wishing to process wxWidgets events must use this macro
132 DECLARE_EVENT_TABLE()
135 #if wxUSE_DATEPICKCTRL
137 // Define a simple modal dialog which asks the user for a date
138 class MyDialog
: public wxDialog
141 MyDialog(wxWindow
*parent
, const wxDateTime
& dt
, int dtpStyle
);
143 wxDateTime
GetDate() const { return m_datePicker
->GetValue(); }
146 void OnDateChange(wxDateEvent
& event
);
149 wxDatePickerCtrlBase
*m_datePicker
;
153 DECLARE_EVENT_TABLE()
156 #endif // wxUSE_DATEPICKCTRL
158 // ----------------------------------------------------------------------------
160 // ----------------------------------------------------------------------------
162 // IDs for the controls and the menu commands
166 Calendar_File_About
= wxID_ABOUT
,
167 Calendar_File_Quit
= wxID_EXIT
,
168 Calendar_Cal_Monday
= 200,
169 Calendar_Cal_Holidays
,
170 Calendar_Cal_Special
,
173 Calendar_Cal_SeqMonth
,
174 Calendar_Cal_SurroundWeeks
,
175 Calendar_Cal_SetDate
,
177 #if wxUSE_DATEPICKCTRL
178 Calendar_DatePicker_AskDate
= 300,
179 Calendar_DatePicker_ShowCentury
,
180 Calendar_DatePicker_DropDown
,
181 Calendar_DatePicker_AllowNone
,
182 #if wxUSE_DATEPICKCTRL_GENERIC
183 Calendar_DatePicker_Generic
,
184 #endif // wxUSE_DATEPICKCTRL_GENERIC
185 #endif // wxUSE_DATEPICKCTRL
186 Calendar_CalCtrl
= 1000
189 // ----------------------------------------------------------------------------
190 // event tables and other macros for wxWidgets
191 // ----------------------------------------------------------------------------
193 // the event tables connect the wxWidgets events with the functions (event
194 // handlers) which process them. It can be also done at run-time, but for the
195 // simple menu events like this the static method is much simpler.
196 BEGIN_EVENT_TABLE(MyFrame
, wxFrame
)
197 EVT_MENU(Calendar_File_Quit
, MyFrame::OnQuit
)
198 EVT_MENU(Calendar_File_About
, MyFrame::OnAbout
)
200 #if wxUSE_DATEPICKCTRL
201 EVT_MENU(Calendar_DatePicker_AskDate
, MyFrame::OnAskDate
)
202 #endif // wxUSE_DATEPICKCTRL
204 EVT_MENU(Calendar_Cal_Monday
, MyFrame::OnCalMonday
)
205 EVT_MENU(Calendar_Cal_Holidays
, MyFrame::OnCalHolidays
)
206 EVT_MENU(Calendar_Cal_Special
, MyFrame::OnCalSpecial
)
208 EVT_MENU(Calendar_Cal_Month
, MyFrame::OnCalAllowMonth
)
209 EVT_MENU(Calendar_Cal_Year
, MyFrame::OnCalAllowYear
)
211 EVT_MENU(Calendar_Cal_SeqMonth
, MyFrame::OnCalSeqMonth
)
212 EVT_MENU(Calendar_Cal_SurroundWeeks
, MyFrame::OnCalShowSurroundingWeeks
)
214 EVT_MENU(Calendar_Cal_SetDate
, MyFrame::OnSetDate
)
215 EVT_MENU(Calendar_Cal_Today
, MyFrame::OnToday
)
218 EVT_UPDATE_UI(Calendar_Cal_Year
, MyFrame::OnAllowYearUpdate
)
221 BEGIN_EVENT_TABLE(MyPanel
, wxPanel
)
222 EVT_CALENDAR (Calendar_CalCtrl
, MyPanel::OnCalendar
)
223 EVT_CALENDAR_MONTH (Calendar_CalCtrl
, MyPanel::OnCalMonthChange
)
224 EVT_CALENDAR_YEAR (Calendar_CalCtrl
, MyPanel::OnCalYearChange
)
225 EVT_CALENDAR_SEL_CHANGED(Calendar_CalCtrl
, MyPanel::OnCalendarChange
)
226 EVT_CALENDAR_WEEKDAY_CLICKED(Calendar_CalCtrl
, MyPanel::OnCalendarWeekDayClick
)
229 #if wxUSE_DATEPICKCTRL
231 BEGIN_EVENT_TABLE(MyDialog
, wxDialog
)
232 EVT_DATE_CHANGED(wxID_ANY
, MyDialog::OnDateChange
)
235 #endif // wxUSE_DATEPICKCTRL
237 // Create a new application object: this macro will allow wxWidgets to create
238 // the application object during program execution (it's better than using a
239 // static object for many reasons) and also declares the accessor function
240 // wxGetApp() which will return the reference of the right type (i.e. MyApp and
244 // ============================================================================
246 // ============================================================================
248 // ----------------------------------------------------------------------------
249 // the application class
250 // ----------------------------------------------------------------------------
252 // `Main program' equivalent: the program execution "starts" here
255 // Create the main application window
256 MyFrame
*frame
= new MyFrame(_T("Calendar wxWidgets sample"),
257 wxPoint(50, 50), wxSize(450, 340));
261 // success: wxApp::OnRun() will be called which will enter the main message
262 // loop and the application will run. If we returned false here, the
263 // application would exit immediately.
267 // ----------------------------------------------------------------------------
269 // ----------------------------------------------------------------------------
272 MyFrame::MyFrame(const wxString
& title
, const wxPoint
& pos
, const wxSize
& size
)
273 : wxFrame((wxFrame
*)NULL
, wxID_ANY
, title
, pos
, size
)
275 // set the frame icon
279 wxMenu
*menuFile
= new wxMenu
;
280 menuFile
->Append(Calendar_File_About
, _T("&About...\tCtrl-A"), _T("Show about dialog"));
281 menuFile
->AppendSeparator();
282 menuFile
->Append(Calendar_File_Quit
, _T("E&xit\tAlt-X"), _T("Quit this program"));
284 wxMenu
*menuCal
= new wxMenu
;
285 menuCal
->Append(Calendar_Cal_Monday
,
286 _T("Monday &first weekday\tCtrl-F"),
287 _T("Toggle between Mon and Sun as the first week day"),
289 menuCal
->Append(Calendar_Cal_Holidays
, _T("Show &holidays\tCtrl-H"),
290 _T("Toggle highlighting the holidays"),
292 menuCal
->Append(Calendar_Cal_Special
, _T("Highlight &special dates\tCtrl-S"),
293 _T("Test custom highlighting"),
295 menuCal
->Append(Calendar_Cal_SurroundWeeks
,
296 _T("Show s&urrounding weeks\tCtrl-W"),
297 _T("Show the neighbouring weeks in the prev/next month"),
299 menuCal
->AppendSeparator();
300 menuCal
->Append(Calendar_Cal_SeqMonth
,
301 _T("To&ggle month selector style\tCtrl-G"),
302 _T("Use another style for the calendar controls"),
304 menuCal
->Append(Calendar_Cal_Month
, _T("&Month can be changed\tCtrl-M"),
305 _T("Allow changing the month in the calendar"),
307 menuCal
->Append(Calendar_Cal_Year
, _T("&Year can be changed\tCtrl-Y"),
308 _T("Allow changing the year in the calendar"),
310 menuCal
->AppendSeparator();
311 menuCal
->Append(Calendar_Cal_SetDate
, _T("SetDate()"), _T("Set date to 2005-12-24."));
312 menuCal
->Append(Calendar_Cal_Today
, _T("Today()"), _T("Set the current date."));
314 #if wxUSE_DATEPICKCTRL
315 wxMenu
*menuDate
= new wxMenu
;
316 menuDate
->AppendCheckItem(Calendar_DatePicker_ShowCentury
,
317 _T("Al&ways show century"));
318 menuDate
->AppendCheckItem(Calendar_DatePicker_DropDown
,
319 _T("Use &drop down control"));
320 menuDate
->AppendCheckItem(Calendar_DatePicker_AllowNone
,
321 _T("Allow &no date"));
322 #if wxUSE_DATEPICKCTRL_GENERIC
323 menuDate
->AppendCheckItem(Calendar_DatePicker_Generic
,
324 _T("Use &generic version of the control"));
325 #endif // wxUSE_DATEPICKCTRL_GENERIC
326 menuDate
->AppendSeparator();
327 menuDate
->Append(Calendar_DatePicker_AskDate
, _T("&Choose date...\tCtrl-D"), _T("Show dialog with wxDatePickerCtrl"));
328 #endif // wxUSE_DATEPICKCTRL
330 // now append the freshly created menu to the menu bar...
331 wxMenuBar
*menuBar
= new wxMenuBar
;
332 menuBar
->Append(menuFile
, _T("&File"));
333 menuBar
->Append(menuCal
, _T("&Calendar"));
334 #if wxUSE_DATEPICKCTRL
335 menuBar
->Append(menuDate
, _T("&Date picker"));
336 #endif // wxUSE_DATEPICKCTRL
338 menuBar
->Check(Calendar_Cal_Monday
, true);
339 menuBar
->Check(Calendar_Cal_Holidays
, true);
340 menuBar
->Check(Calendar_Cal_Month
, true);
341 menuBar
->Check(Calendar_Cal_Year
, true);
343 #if wxUSE_DATEPICKCTRL
344 menuBar
->Check(Calendar_DatePicker_ShowCentury
, true);
345 #endif // wxUSE_DATEPICKCTRL
347 // ... and attach this menu bar to the frame
350 m_panel
= new MyPanel(this);
353 // create a status bar just for fun (by default with 1 pane only)
355 SetStatusText(_T("Welcome to wxWidgets!"));
356 #endif // wxUSE_STATUSBAR
359 void MyFrame::OnQuit(wxCommandEvent
& WXUNUSED(event
))
361 // true is to force the frame to close
365 void MyFrame::OnAbout(wxCommandEvent
& WXUNUSED(event
))
367 wxMessageBox(_T("wxCalendarCtrl sample\n(c) 2000 Vadim Zeitlin"),
368 _T("About Calendar"), wxOK
| wxICON_INFORMATION
, this);
371 void MyFrame::OnCalMonday(wxCommandEvent
& event
)
373 bool enable
= GetMenuBar()->IsChecked(event
.GetId());
375 m_panel
->ToggleCalStyle(enable
, wxCAL_MONDAY_FIRST
);
378 void MyFrame::OnCalHolidays(wxCommandEvent
& event
)
380 bool enable
= GetMenuBar()->IsChecked(event
.GetId());
382 m_panel
->GetCal()->EnableHolidayDisplay(enable
);
385 void MyFrame::OnCalSpecial(wxCommandEvent
& event
)
387 m_panel
->HighlightSpecial(GetMenuBar()->IsChecked(event
.GetId()));
390 void MyFrame::OnCalAllowMonth(wxCommandEvent
& event
)
392 bool allow
= GetMenuBar()->IsChecked(event
.GetId());
394 m_panel
->GetCal()->EnableMonthChange(allow
);
397 void MyFrame::OnCalAllowYear(wxCommandEvent
& event
)
399 bool allow
= GetMenuBar()->IsChecked(event
.GetId());
401 m_panel
->GetCal()->EnableYearChange(allow
);
404 void MyFrame::OnCalSeqMonth(wxCommandEvent
& event
)
406 bool allow
= GetMenuBar()->IsChecked(event
.GetId());
408 m_panel
->ToggleCalStyle(allow
, wxCAL_SEQUENTIAL_MONTH_SELECTION
);
411 void MyFrame::OnCalShowSurroundingWeeks(wxCommandEvent
& event
)
413 bool allow
= GetMenuBar()->IsChecked(event
.GetId());
415 m_panel
->ToggleCalStyle(allow
, wxCAL_SHOW_SURROUNDING_WEEKS
);
418 void MyFrame::OnAllowYearUpdate(wxUpdateUIEvent
& event
)
420 event
.Enable( GetMenuBar()->IsChecked(Calendar_Cal_Month
));
423 void MyFrame::OnSetDate(wxCommandEvent
&WXUNUSED(event
))
428 void MyFrame::OnToday(wxCommandEvent
&WXUNUSED(event
))
433 #if wxUSE_DATEPICKCTRL
435 void MyFrame::OnAskDate(wxCommandEvent
& WXUNUSED(event
))
437 int style
= wxDP_DEFAULT
;
438 if ( GetMenuBar()->IsChecked(Calendar_DatePicker_ShowCentury
) )
439 style
|= wxDP_SHOWCENTURY
;
440 if ( GetMenuBar()->IsChecked(Calendar_DatePicker_DropDown
) )
441 style
|= wxDP_DROPDOWN
;
442 if ( GetMenuBar()->IsChecked(Calendar_DatePicker_AllowNone
) )
443 style
|= wxDP_ALLOWNONE
;
445 MyDialog
dlg(this, m_panel
->GetCal()->GetDate(), style
);
446 if ( dlg
.ShowModal() == wxID_OK
)
448 const wxDateTime dt
= dlg
.GetDate();
451 const wxDateTime today
= wxDateTime::Today();
453 if ( dt
.GetDay() == today
.GetDay() &&
454 dt
.GetMonth() == today
.GetMonth() )
456 wxMessageBox(_T("Happy birthday!"), _T("Calendar Sample"));
459 m_panel
->GetCal()->SetDate(dt
);
461 wxLogStatus(_T("Changed the date to your input"));
465 wxLogStatus(_T("No date entered"));
470 #endif // wxUSE_DATEPICKCTRL
472 // ----------------------------------------------------------------------------
474 // ----------------------------------------------------------------------------
476 MyPanel::MyPanel(wxFrame
*frame
)
477 : wxPanel(frame
, wxID_ANY
)
480 date
.Printf(wxT("Selected date: %s"),
481 wxDateTime::Today().FormatISODate().c_str());
482 m_date
= new wxStaticText(this, wxID_ANY
, date
);
483 m_calendar
= new wxCalendarCtrl(this, Calendar_CalCtrl
,
488 wxCAL_SHOW_HOLIDAYS
|
491 wxBoxSizer
*m_sizer
= new wxBoxSizer( wxHORIZONTAL
);
493 m_sizer
->Add(m_date
, 0, wxALIGN_CENTER
| wxALL
, 10 );
494 m_sizer
->Add(m_calendar
, 0, wxALIGN_CENTER
| wxALIGN_LEFT
);
497 m_sizer
->SetSizeHints( this );
500 void MyPanel::OnCalendar(wxCalendarEvent
& event
)
502 wxLogMessage(wxT("Selected %s from calendar"),
503 event
.GetDate().FormatISODate().c_str());
506 void MyPanel::OnCalendarChange(wxCalendarEvent
& event
)
509 s
.Printf(wxT("Selected date: %s"), event
.GetDate().FormatISODate().c_str());
514 void MyPanel::OnCalMonthChange(wxCalendarEvent
& WXUNUSED(event
))
516 wxLogStatus(wxT("Calendar month changed"));
519 void MyPanel::OnCalYearChange(wxCalendarEvent
& WXUNUSED(event
))
521 wxLogStatus(wxT("Calendar year changed"));
524 void MyPanel::OnCalendarWeekDayClick(wxCalendarEvent
& event
)
526 wxLogMessage(wxT("Clicked on %s"),
527 wxDateTime::GetWeekDayName(event
.GetWeekDay()).c_str());
530 void MyPanel::ToggleCalStyle(bool on
, int flag
)
532 long style
= m_calendar
->GetWindowStyle();
538 m_calendar
->SetWindowStyle(style
);
540 m_calendar
->Refresh();
543 void MyPanel::HighlightSpecial(bool on
)
548 *attrRedCircle
= new wxCalendarDateAttr(wxCAL_BORDER_ROUND
, *wxRED
),
549 *attrGreenSquare
= new wxCalendarDateAttr(wxCAL_BORDER_SQUARE
, *wxGREEN
),
550 *attrHeaderLike
= new wxCalendarDateAttr(*wxBLUE
, *wxLIGHT_GREY
);
552 m_calendar
->SetAttr(17, attrRedCircle
);
553 m_calendar
->SetAttr(29, attrGreenSquare
);
554 m_calendar
->SetAttr(13, attrHeaderLike
);
558 m_calendar
->ResetAttr(17);
559 m_calendar
->ResetAttr(29);
560 m_calendar
->ResetAttr(13);
563 m_calendar
->Refresh();
566 void MyPanel::SetDate()
568 wxDateTime
date(24, wxDateTime::Dec
, 2005, 23, 59, 59);
569 m_calendar
->SetDate(date
);
572 void MyPanel::Today()
574 m_calendar
->SetDate(wxDateTime::Today());
577 // ----------------------------------------------------------------------------
579 // ----------------------------------------------------------------------------
581 #if wxUSE_DATEPICKCTRL
583 MyDialog::MyDialog(wxWindow
*parent
, const wxDateTime
& dt
, int dtpStyle
)
584 : wxDialog(parent
, wxID_ANY
, wxString(_T("Calendar: Choose a date")))
586 wxStdDialogButtonSizer
*sizerBtns
= new wxStdDialogButtonSizer
;
587 sizerBtns
->AddButton(new wxButton(this, wxID_OK
));
588 sizerBtns
->AddButton(new wxButton(this, wxID_CANCEL
));
589 sizerBtns
->Realize();
591 wxSizer
*sizerText
= new wxBoxSizer(wxHORIZONTAL
);
592 sizerText
->Add(new wxStaticText(this, wxID_ANY
, _T("Date in ISO format: ")),
593 wxSizerFlags().Border().Align(wxALIGN_CENTRE_VERTICAL
));
594 m_text
= new wxTextCtrl(this, wxID_ANY
);
595 sizerText
->Add(m_text
, wxSizerFlags().
596 Expand().Border().Align(wxALIGN_CENTRE_VERTICAL
));
598 wxSizer
*sizerTop
= new wxBoxSizer(wxVERTICAL
);
599 sizerTop
->Add(new wxStaticText
602 _T("Enter your birthday date (not before 20th century):")
604 wxSizerFlags().Border());
606 #if wxUSE_DATEPICKCTRL_GENERIC
607 wxFrame
*frame
= (wxFrame
*)wxGetTopLevelParent(parent
);
608 if ( frame
&& frame
->GetMenuBar()->IsChecked(Calendar_DatePicker_Generic
) )
609 m_datePicker
= new wxDatePickerCtrlGeneric(this, wxID_ANY
, dt
,
614 #endif // wxUSE_DATEPICKCTRL_GENERIC
615 m_datePicker
= new wxDatePickerCtrl(this, wxID_ANY
, dt
,
616 wxDefaultPosition
, wxDefaultSize
,
618 m_datePicker
->SetRange(wxDateTime(1, wxDateTime::Jan
, 1900),
620 sizerTop
->Add(m_datePicker
, wxSizerFlags().Expand().Border());
622 sizerTop
->AddStretchSpacer(1);
623 sizerTop
->Add(sizerText
);
625 sizerTop
->Add(sizerBtns
, wxSizerFlags().Centre().Border());
627 SetSizerAndFit(sizerTop
);
631 void MyDialog::OnDateChange(wxDateEvent
& event
)
633 const wxDateTime dt
= event
.GetDate();
635 m_text
->SetValue(dt
.FormatISODate());
637 m_text
->SetValue(wxEmptyString
);
640 #endif // wxUSE_DATEPICKCTRL