1 /////////////////////////////////////////////////////////////////////////////
3 // Purpose: wxCalendarCtrl sample
4 // Author: Vadim Zeitlin
8 // Copyright: (c) Vadim Zeitlin
9 // Licence: wxWindows licence
10 /////////////////////////////////////////////////////////////////////////////
12 // ============================================================================
14 // ============================================================================
16 // ----------------------------------------------------------------------------
18 // ----------------------------------------------------------------------------
20 #if defined(__GNUG__) && !defined(__APPLE__)
21 #pragma implementation "calendar.cpp"
22 #pragma interface "calendar.cpp"
25 // For compilers that support precompilation, includes "wx/wx.h".
26 #include "wx/wxprec.h"
32 // for all others, include the necessary headers
38 #include "wx/stattext.h"
40 #include "wx/layout.h"
41 #include "wx/msgdlg.h"
45 #include "wx/textctrl.h"
47 #include "wx/calctrl.h"
48 #include "wx/datectrl.h"
50 // ----------------------------------------------------------------------------
52 // ----------------------------------------------------------------------------
54 // Define a new application type, each program should derive a class from wxApp
55 class MyApp
: public wxApp
58 // override base class virtuals
59 // ----------------------------
61 // this one is called on application startup and is a good place for the app
62 // initialization (doing it here and not in the ctor allows to have an error
63 // return: if OnInit() returns false, the application terminates)
64 virtual bool OnInit();
67 class MyPanel
: public wxPanel
70 MyPanel(wxFrame
*frame
);
72 void OnCalendar(wxCalendarEvent
& event
);
73 void OnCalendarWeekDayClick(wxCalendarEvent
& event
);
74 void OnCalendarChange(wxCalendarEvent
& event
);
75 void OnCalMonthChange(wxCalendarEvent
& event
);
76 void OnCalYearChange(wxCalendarEvent
& event
);
78 wxCalendarCtrl
*GetCal() const { return m_calendar
; }
80 // turn on/off the specified style bit on the calendar control
81 void ToggleCalStyle(bool on
, int style
);
83 void HighlightSpecial(bool on
);
89 wxCalendarCtrl
*m_calendar
;
95 // Define a new frame type: this is going to be our main frame
96 class MyFrame
: public wxFrame
100 MyFrame(const wxString
& title
, const wxPoint
& pos
, const wxSize
& size
);
102 // event handlers (these functions should _not_ be virtual)
103 void OnQuit(wxCommandEvent
& event
);
104 void OnAbout(wxCommandEvent
& event
);
106 #if wxUSE_DATEPICKCTRL
107 void OnAskDate(wxCommandEvent
& event
);
108 #endif // wxUSE_DATEPICKCTRL
110 void OnCalMonday(wxCommandEvent
& event
);
111 void OnCalHolidays(wxCommandEvent
& event
);
112 void OnCalSpecial(wxCommandEvent
& event
);
114 void OnCalAllowMonth(wxCommandEvent
& event
);
115 void OnCalAllowYear(wxCommandEvent
& event
);
117 void OnCalSeqMonth(wxCommandEvent
& event
);
118 void OnCalShowSurroundingWeeks(wxCommandEvent
& event
);
120 void OnSetDate(wxCommandEvent
& event
);
121 void OnToday(wxCommandEvent
& event
);
123 void OnAllowYearUpdate(wxUpdateUIEvent
& event
);
128 // any class wishing to process wxWidgets events must use this macro
129 DECLARE_EVENT_TABLE()
132 #if wxUSE_DATEPICKCTRL
134 // Define a simple modal dialog which asks the user for a date
135 class MyDialog
: public wxDialog
138 MyDialog(wxWindow
*parent
, const wxDateTime
& dt
, int dtpStyle
);
140 wxDateTime
GetDate() const { return m_datePicker
->GetValue(); }
143 void OnDateChange(wxDateEvent
& event
);
146 wxDatePickerCtrl
*m_datePicker
;
150 DECLARE_EVENT_TABLE()
153 #endif // wxUSE_DATEPICKCTRL
155 // ----------------------------------------------------------------------------
157 // ----------------------------------------------------------------------------
159 // IDs for the controls and the menu commands
163 Calendar_File_About
= wxID_ABOUT
,
164 Calendar_File_Quit
= wxID_EXIT
,
165 Calendar_Cal_Monday
= 200,
166 Calendar_Cal_Holidays
,
167 Calendar_Cal_Special
,
170 Calendar_Cal_SeqMonth
,
171 Calendar_Cal_SurroundWeeks
,
172 Calendar_Cal_SetDate
,
174 #if wxUSE_DATEPICKCTRL
175 Calendar_DatePicker_AskDate
= 300,
176 Calendar_DatePicker_ShowCentury
,
177 Calendar_DatePicker_DropDown
,
178 #endif // wxUSE_DATEPICKCTRL
179 Calendar_CalCtrl
= 1000
182 // ----------------------------------------------------------------------------
183 // event tables and other macros for wxWidgets
184 // ----------------------------------------------------------------------------
186 // the event tables connect the wxWidgets events with the functions (event
187 // handlers) which process them. It can be also done at run-time, but for the
188 // simple menu events like this the static method is much simpler.
189 BEGIN_EVENT_TABLE(MyFrame
, wxFrame
)
190 EVT_MENU(Calendar_File_Quit
, MyFrame
::OnQuit
)
191 EVT_MENU(Calendar_File_About
, MyFrame
::OnAbout
)
193 #if wxUSE_DATEPICKCTRL
194 EVT_MENU(Calendar_DatePicker_AskDate
, MyFrame
::OnAskDate
)
195 #endif // wxUSE_DATEPICKCTRL
197 EVT_MENU(Calendar_Cal_Monday
, MyFrame
::OnCalMonday
)
198 EVT_MENU(Calendar_Cal_Holidays
, MyFrame
::OnCalHolidays
)
199 EVT_MENU(Calendar_Cal_Special
, MyFrame
::OnCalSpecial
)
201 EVT_MENU(Calendar_Cal_Month
, MyFrame
::OnCalAllowMonth
)
202 EVT_MENU(Calendar_Cal_Year
, MyFrame
::OnCalAllowYear
)
204 EVT_MENU(Calendar_Cal_SeqMonth
, MyFrame
::OnCalSeqMonth
)
205 EVT_MENU(Calendar_Cal_SurroundWeeks
, MyFrame
::OnCalShowSurroundingWeeks
)
207 EVT_MENU(Calendar_Cal_SetDate
, MyFrame
::OnSetDate
)
208 EVT_MENU(Calendar_Cal_Today
, MyFrame
::OnToday
)
211 EVT_UPDATE_UI(Calendar_Cal_Year
, MyFrame
::OnAllowYearUpdate
)
214 BEGIN_EVENT_TABLE(MyPanel
, wxPanel
)
215 EVT_CALENDAR (Calendar_CalCtrl
, MyPanel
::OnCalendar
)
216 EVT_CALENDAR_MONTH (Calendar_CalCtrl
, MyPanel
::OnCalMonthChange
)
217 EVT_CALENDAR_YEAR (Calendar_CalCtrl
, MyPanel
::OnCalYearChange
)
218 EVT_CALENDAR_SEL_CHANGED(Calendar_CalCtrl
, MyPanel
::OnCalendarChange
)
219 EVT_CALENDAR_WEEKDAY_CLICKED(Calendar_CalCtrl
, MyPanel
::OnCalendarWeekDayClick
)
222 #if wxUSE_DATEPICKCTRL
224 BEGIN_EVENT_TABLE(MyDialog
, wxDialog
)
225 EVT_DATE_CHANGED(wxID_ANY
, MyDialog
::OnDateChange
)
228 #endif // wxUSE_DATEPICKCTRL
230 // Create a new application object: this macro will allow wxWidgets to create
231 // the application object during program execution (it's better than using a
232 // static object for many reasons) and also declares the accessor function
233 // wxGetApp() which will return the reference of the right type (i.e. MyApp and
237 // ============================================================================
239 // ============================================================================
241 // ----------------------------------------------------------------------------
242 // the application class
243 // ----------------------------------------------------------------------------
245 // `Main program' equivalent: the program execution "starts" here
248 // Create the main application window
249 MyFrame
*frame
= new MyFrame(_T("Calendar wxWidgets sample"),
250 wxPoint(50, 50), wxSize(450, 340));
254 // success: wxApp::OnRun() will be called which will enter the main message
255 // loop and the application will run. If we returned false here, the
256 // application would exit immediately.
260 // ----------------------------------------------------------------------------
262 // ----------------------------------------------------------------------------
265 MyFrame
::MyFrame(const wxString
& title
, const wxPoint
& pos
, const wxSize
& size
)
266 : wxFrame((wxFrame
*)NULL
, wxID_ANY
, title
, pos
, size
)
269 wxMenu
*menuFile
= new wxMenu
;
270 menuFile
->Append(Calendar_File_About
, _T("&About...\tCtrl-A"), _T("Show about dialog"));
271 menuFile
->AppendSeparator();
272 menuFile
->Append(Calendar_File_Quit
, _T("E&xit\tAlt-X"), _T("Quit this program"));
274 wxMenu
*menuCal
= new wxMenu
;
275 menuCal
->Append(Calendar_Cal_Monday
,
276 _T("Monday &first weekday\tCtrl-F"),
277 _T("Toggle between Mon and Sun as the first week day"),
279 menuCal
->Append(Calendar_Cal_Holidays
, _T("Show &holidays\tCtrl-H"),
280 _T("Toggle highlighting the holidays"),
282 menuCal
->Append(Calendar_Cal_Special
, _T("Highlight &special dates\tCtrl-S"),
283 _T("Test custom highlighting"),
285 menuCal
->Append(Calendar_Cal_SurroundWeeks
,
286 _T("Show s&urrounding weeks\tCtrl-W"),
287 _T("Show the neighbouring weeks in the prev/next month"),
289 menuCal
->AppendSeparator();
290 menuCal
->Append(Calendar_Cal_SeqMonth
,
291 _T("To&ggle month selector style\tCtrl-G"),
292 _T("Use another style for the calendar controls"),
294 menuCal
->Append(Calendar_Cal_Month
, _T("&Month can be changed\tCtrl-M"),
295 _T("Allow changing the month in the calendar"),
297 menuCal
->Append(Calendar_Cal_Year
, _T("&Year can be changed\tCtrl-Y"),
298 _T("Allow changing the year in the calendar"),
300 menuCal
->AppendSeparator();
301 menuCal
->Append(Calendar_Cal_SetDate
, _T("SetDate()"), _T("Set date to 2005-12-24."));
302 menuCal
->Append(Calendar_Cal_Today
, _T("Today()"), _T("Set the current date."));
304 #if wxUSE_DATEPICKCTRL
305 wxMenu
*menuDate
= new wxMenu
;
306 menuDate
->AppendCheckItem(Calendar_DatePicker_ShowCentury
,
307 _T("Al&ways show century"));
308 menuDate
->AppendCheckItem(Calendar_DatePicker_DropDown
,
309 _T("Use &drop down control"));
310 menuDate
->AppendSeparator();
311 menuDate
->Append(Calendar_DatePicker_AskDate
, _T("&Choose date...\tCtrl-D"), _T("Show dialog with wxDatePickerCtrl"));
312 #endif // wxUSE_DATEPICKCTRL
314 // now append the freshly created menu to the menu bar...
315 wxMenuBar
*menuBar
= new wxMenuBar
;
316 menuBar
->Append(menuFile
, _T("&File"));
317 menuBar
->Append(menuCal
, _T("&Calendar"));
318 #if wxUSE_DATEPICKCTRL
319 menuBar
->Append(menuDate
, _T("&Date picker"));
320 #endif // wxUSE_DATEPICKCTRL
322 menuBar
->Check(Calendar_Cal_Monday
, true);
323 menuBar
->Check(Calendar_Cal_Holidays
, true);
324 menuBar
->Check(Calendar_Cal_Month
, true);
325 menuBar
->Check(Calendar_Cal_Year
, true);
327 #if wxUSE_DATEPICKCTRL
328 menuBar
->Check(Calendar_DatePicker_ShowCentury
, true);
329 #endif // wxUSE_DATEPICKCTRL
331 // ... and attach this menu bar to the frame
334 m_panel
= new MyPanel(this);
337 // create a status bar just for fun (by default with 1 pane only)
339 SetStatusText(_T("Welcome to wxWidgets!"));
340 #endif // wxUSE_STATUSBAR
343 void MyFrame
::OnQuit(wxCommandEvent
& WXUNUSED(event
))
345 // true is to force the frame to close
349 void MyFrame
::OnAbout(wxCommandEvent
& WXUNUSED(event
))
351 wxMessageBox(_T("wxCalendarCtrl sample\n(c) 2000 Vadim Zeitlin"),
352 _T("About Calendar"), wxOK
| wxICON_INFORMATION
, this);
355 void MyFrame
::OnCalMonday(wxCommandEvent
& event
)
357 bool enable
= GetMenuBar()->IsChecked(event
.GetId());
359 m_panel
->ToggleCalStyle(enable
, wxCAL_MONDAY_FIRST
);
362 void MyFrame
::OnCalHolidays(wxCommandEvent
& event
)
364 bool enable
= GetMenuBar()->IsChecked(event
.GetId());
366 m_panel
->GetCal()->EnableHolidayDisplay(enable
);
369 void MyFrame
::OnCalSpecial(wxCommandEvent
& event
)
371 m_panel
->HighlightSpecial(GetMenuBar()->IsChecked(event
.GetId()));
374 void MyFrame
::OnCalAllowMonth(wxCommandEvent
& event
)
376 bool allow
= GetMenuBar()->IsChecked(event
.GetId());
378 m_panel
->GetCal()->EnableMonthChange(allow
);
381 void MyFrame
::OnCalAllowYear(wxCommandEvent
& event
)
383 bool allow
= GetMenuBar()->IsChecked(event
.GetId());
385 m_panel
->GetCal()->EnableYearChange(allow
);
388 void MyFrame
::OnCalSeqMonth(wxCommandEvent
& event
)
390 bool allow
= GetMenuBar()->IsChecked(event
.GetId());
392 m_panel
->ToggleCalStyle(allow
, wxCAL_SEQUENTIAL_MONTH_SELECTION
);
395 void MyFrame
::OnCalShowSurroundingWeeks(wxCommandEvent
& event
)
397 bool allow
= GetMenuBar()->IsChecked(event
.GetId());
399 m_panel
->ToggleCalStyle(allow
, wxCAL_SHOW_SURROUNDING_WEEKS
);
402 void MyFrame
::OnAllowYearUpdate(wxUpdateUIEvent
& event
)
404 event
.Enable( GetMenuBar()->IsChecked(Calendar_Cal_Month
));
407 void MyFrame
::OnSetDate(wxCommandEvent
&WXUNUSED(event
))
412 void MyFrame
::OnToday(wxCommandEvent
&WXUNUSED(event
))
417 #if wxUSE_DATEPICKCTRL
419 void MyFrame
::OnAskDate(wxCommandEvent
& WXUNUSED(event
))
421 int style
= wxDP_DEFAULT
;
422 if ( GetMenuBar()->IsChecked(Calendar_DatePicker_ShowCentury
) )
423 style
|= wxDP_SHOWCENTURY
;
424 if ( GetMenuBar()->IsChecked(Calendar_DatePicker_DropDown
) )
425 style
|= wxDP_DROPDOWN
;
427 MyDialog
dlg(this, m_panel
->GetCal()->GetDate(), style
);
428 if ( dlg
.ShowModal() == wxID_OK
)
430 const wxDateTime dt
= dlg
.GetDate(),
431 today
= wxDateTime
::Today();
433 if ( dt
.GetDay() == today
.GetDay() &&
434 dt
.GetMonth() == today
.GetMonth() )
436 wxMessageBox(_T("Happy birthday!"), _T("Calendar Sample"));
439 m_panel
->GetCal()->SetDate(dt
);
441 wxLogStatus(_T("Changed the date to your birthday"));
445 #endif // wxUSE_DATEPICKCTRL
447 // ----------------------------------------------------------------------------
449 // ----------------------------------------------------------------------------
451 MyPanel
::MyPanel(wxFrame
*frame
)
452 : wxPanel(frame
, wxID_ANY
)
455 date
.Printf(wxT("Selected date: %s"),
456 wxDateTime
::Today().FormatISODate().c_str());
457 m_date
= new wxStaticText(this, wxID_ANY
, date
);
458 m_calendar
= new wxCalendarCtrl(this, Calendar_CalCtrl
,
463 wxCAL_SHOW_HOLIDAYS
|
466 wxBoxSizer
*m_sizer
= new wxBoxSizer( wxHORIZONTAL
);
468 m_sizer
->Add(m_date
, 0, wxALIGN_CENTER
| wxALL
, 10 );
469 m_sizer
->Add(m_calendar
, 0, wxALIGN_CENTER
| wxALIGN_LEFT
);
472 m_sizer
->SetSizeHints( this );
475 void MyPanel
::OnCalendar(wxCalendarEvent
& event
)
477 wxLogMessage(wxT("Selected %s from calendar"),
478 event
.GetDate().FormatISODate().c_str());
481 void MyPanel
::OnCalendarChange(wxCalendarEvent
& event
)
484 s
.Printf(wxT("Selected date: %s"), event
.GetDate().FormatISODate().c_str());
489 void MyPanel
::OnCalMonthChange(wxCalendarEvent
& WXUNUSED(event
))
491 wxLogStatus(wxT("Calendar month changed"));
494 void MyPanel
::OnCalYearChange(wxCalendarEvent
& WXUNUSED(event
))
496 wxLogStatus(wxT("Calendar year changed"));
499 void MyPanel
::OnCalendarWeekDayClick(wxCalendarEvent
& event
)
501 wxLogMessage(wxT("Clicked on %s"),
502 wxDateTime
::GetWeekDayName(event
.GetWeekDay()).c_str());
505 void MyPanel
::ToggleCalStyle(bool on
, int flag
)
507 long style
= m_calendar
->GetWindowStyle();
513 m_calendar
->SetWindowStyle(style
);
515 m_calendar
->Refresh();
518 void MyPanel
::HighlightSpecial(bool on
)
523 *attrRedCircle
= new wxCalendarDateAttr(wxCAL_BORDER_ROUND
, *wxRED
),
524 *attrGreenSquare
= new wxCalendarDateAttr(wxCAL_BORDER_SQUARE
, *wxGREEN
),
525 *attrHeaderLike
= new wxCalendarDateAttr(*wxBLUE
, *wxLIGHT_GREY
);
527 m_calendar
->SetAttr(17, attrRedCircle
);
528 m_calendar
->SetAttr(29, attrGreenSquare
);
529 m_calendar
->SetAttr(13, attrHeaderLike
);
533 m_calendar
->ResetAttr(17);
534 m_calendar
->ResetAttr(29);
535 m_calendar
->ResetAttr(13);
538 m_calendar
->Refresh();
541 void MyPanel
::SetDate()
543 wxDateTime
date(24, wxDateTime
::Dec
, 2005, 23, 59, 59);
544 m_calendar
->SetDate(date
);
547 void MyPanel
::Today()
549 m_calendar
->SetDate(wxDateTime
::Today());
552 // ----------------------------------------------------------------------------
554 // ----------------------------------------------------------------------------
556 #if wxUSE_DATEPICKCTRL
558 MyDialog
::MyDialog(wxWindow
*parent
, const wxDateTime
& dt
, int dtpStyle
)
559 : wxDialog(parent
, -1, wxString(_T("Calendar: Choose a date")))
561 wxStdDialogButtonSizer
*sizerBtns
= new wxStdDialogButtonSizer
;
562 sizerBtns
->AddButton(new wxButton(this, wxID_OK
));
563 sizerBtns
->AddButton(new wxButton(this, wxID_CANCEL
));
564 sizerBtns
->Finalise();
566 wxSizer
*sizerText
= new wxBoxSizer(wxHORIZONTAL
);
567 sizerText
->Add(new wxStaticText(this, -1, _T("Date in ISO format: ")),
568 wxSizerFlags().Border());
569 m_text
= new wxTextCtrl(this, -1);
570 sizerText
->Add(m_text
, wxSizerFlags().Expand().Border());
572 wxSizer
*sizerTop
= new wxBoxSizer(wxVERTICAL
);
573 sizerTop
->Add(new wxStaticText
576 _T("Enter your birthday date (not before 20th century):")
578 wxSizerFlags().Border());
580 m_datePicker
= new wxDatePickerCtrl(this, -1, dt
,
581 wxDefaultPosition
, wxDefaultSize
,
583 m_datePicker
->SetRange(wxDateTime(1, wxDateTime
::Jan
, 1900),
585 sizerTop
->Add(m_datePicker
, wxSizerFlags().Expand().Border());
587 sizerTop
->AddStretchSpacer(1);
588 sizerTop
->Add(sizerText
);
590 sizerTop
->Add(sizerBtns
, wxSizerFlags().Centre().Border());
592 SetSizerAndFit(sizerTop
);
596 void MyDialog
::OnDateChange(wxDateEvent
& event
)
598 m_text
->SetValue(event
.GetDate().FormatISODate());
601 #endif // wxUSE_DATEPICKCTRL