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
);
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 #if wxUSE_DATEPICKCTRL
166 Calendar_File_AskDate
= 100,
167 #endif // wxUSE_DATEPICKCTRL
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 Calendar_CalCtrl
= 1000
180 // ----------------------------------------------------------------------------
181 // event tables and other macros for wxWidgets
182 // ----------------------------------------------------------------------------
184 // the event tables connect the wxWidgets events with the functions (event
185 // handlers) which process them. It can be also done at run-time, but for the
186 // simple menu events like this the static method is much simpler.
187 BEGIN_EVENT_TABLE(MyFrame
, wxFrame
)
188 EVT_MENU(Calendar_File_Quit
, MyFrame
::OnQuit
)
189 EVT_MENU(Calendar_File_About
, MyFrame
::OnAbout
)
191 #if wxUSE_DATEPICKCTRL
192 EVT_MENU(Calendar_File_AskDate
, MyFrame
::OnAskDate
)
193 #endif // wxUSE_DATEPICKCTRL
195 EVT_MENU(Calendar_Cal_Monday
, MyFrame
::OnCalMonday
)
196 EVT_MENU(Calendar_Cal_Holidays
, MyFrame
::OnCalHolidays
)
197 EVT_MENU(Calendar_Cal_Special
, MyFrame
::OnCalSpecial
)
199 EVT_MENU(Calendar_Cal_Month
, MyFrame
::OnCalAllowMonth
)
200 EVT_MENU(Calendar_Cal_Year
, MyFrame
::OnCalAllowYear
)
202 EVT_MENU(Calendar_Cal_SeqMonth
, MyFrame
::OnCalSeqMonth
)
203 EVT_MENU(Calendar_Cal_SurroundWeeks
, MyFrame
::OnCalShowSurroundingWeeks
)
205 EVT_MENU(Calendar_Cal_SetDate
, MyFrame
::OnSetDate
)
206 EVT_MENU(Calendar_Cal_Today
, MyFrame
::OnToday
)
209 EVT_UPDATE_UI(Calendar_Cal_Year
, MyFrame
::OnAllowYearUpdate
)
212 BEGIN_EVENT_TABLE(MyPanel
, wxPanel
)
213 EVT_CALENDAR (Calendar_CalCtrl
, MyPanel
::OnCalendar
)
214 EVT_CALENDAR_MONTH (Calendar_CalCtrl
, MyPanel
::OnCalMonthChange
)
215 EVT_CALENDAR_YEAR (Calendar_CalCtrl
, MyPanel
::OnCalYearChange
)
216 EVT_CALENDAR_SEL_CHANGED(Calendar_CalCtrl
, MyPanel
::OnCalendarChange
)
217 EVT_CALENDAR_WEEKDAY_CLICKED(Calendar_CalCtrl
, MyPanel
::OnCalendarWeekDayClick
)
220 #if wxUSE_DATEPICKCTRL
222 BEGIN_EVENT_TABLE(MyDialog
, wxDialog
)
223 EVT_DATE_CHANGED(wxID_ANY
, MyDialog
::OnDateChange
)
226 #endif // wxUSE_DATEPICKCTRL
228 // Create a new application object: this macro will allow wxWidgets to create
229 // the application object during program execution (it's better than using a
230 // static object for many reasons) and also declares the accessor function
231 // wxGetApp() which will return the reference of the right type (i.e. MyApp and
235 // ============================================================================
237 // ============================================================================
239 // ----------------------------------------------------------------------------
240 // the application class
241 // ----------------------------------------------------------------------------
243 // `Main program' equivalent: the program execution "starts" here
246 // Create the main application window
247 MyFrame
*frame
= new MyFrame(_T("Calendar wxWidgets sample"),
248 wxPoint(50, 50), wxSize(450, 340));
252 // success: wxApp::OnRun() will be called which will enter the main message
253 // loop and the application will run. If we returned false here, the
254 // application would exit immediately.
258 // ----------------------------------------------------------------------------
260 // ----------------------------------------------------------------------------
263 MyFrame
::MyFrame(const wxString
& title
, const wxPoint
& pos
, const wxSize
& size
)
264 : wxFrame((wxFrame
*)NULL
, wxID_ANY
, title
, pos
, size
)
267 wxMenu
*menuFile
= new wxMenu
;
269 #if wxUSE_DATEPICKCTRL
270 menuFile
->Append(Calendar_File_AskDate
, _T("&Choose date...\tCtrl-D"), _T("Show dialog with wxDatePickerCtrl"));
271 menuFile
->AppendSeparator();
272 #endif // wxUSE_DATEPICKCTRL
274 menuFile
->Append(Calendar_File_About
, _T("&About...\tCtrl-A"), _T("Show about dialog"));
275 menuFile
->AppendSeparator();
276 menuFile
->Append(Calendar_File_Quit
, _T("E&xit\tAlt-X"), _T("Quit this program"));
278 wxMenu
*menuCal
= new wxMenu
;
279 menuCal
->Append(Calendar_Cal_Monday
,
280 _T("Monday &first weekday\tCtrl-F"),
281 _T("Toggle between Mon and Sun as the first week day"),
283 menuCal
->Append(Calendar_Cal_Holidays
, _T("Show &holidays\tCtrl-H"),
284 _T("Toggle highlighting the holidays"),
286 menuCal
->Append(Calendar_Cal_Special
, _T("Highlight &special dates\tCtrl-S"),
287 _T("Test custom highlighting"),
289 menuCal
->Append(Calendar_Cal_SurroundWeeks
,
290 _T("Show s&urrounding weeks\tCtrl-W"),
291 _T("Show the neighbouring weeks in the prev/next month"),
293 menuCal
->AppendSeparator();
294 menuCal
->Append(Calendar_Cal_SeqMonth
,
295 _T("To&ggle month selector style\tCtrl-G"),
296 _T("Use another style for the calendar controls"),
298 menuCal
->Append(Calendar_Cal_Month
, _T("&Month can be changed\tCtrl-M"),
299 _T("Allow changing the month in the calendar"),
301 menuCal
->Append(Calendar_Cal_Year
, _T("&Year can be changed\tCtrl-Y"),
302 _T("Allow changing the year in the calendar"),
304 menuCal
->AppendSeparator();
305 menuCal
->Append(Calendar_Cal_SetDate
, _T("SetDate()"), _T("Set date to 2005-12-24."));
306 menuCal
->Append(Calendar_Cal_Today
, _T("Today()"), _T("Set the current date."));
308 // now append the freshly created menu to the menu bar...
309 wxMenuBar
*menuBar
= new wxMenuBar
;
310 menuBar
->Append(menuFile
, _T("&File"));
311 menuBar
->Append(menuCal
, _T("&Calendar"));
313 menuBar
->Check(Calendar_Cal_Monday
, true);
314 menuBar
->Check(Calendar_Cal_Holidays
, true);
315 menuBar
->Check(Calendar_Cal_Month
, true);
316 menuBar
->Check(Calendar_Cal_Year
, true);
318 // ... and attach this menu bar to the frame
321 m_panel
= new MyPanel(this);
324 // create a status bar just for fun (by default with 1 pane only)
326 SetStatusText(_T("Welcome to wxWidgets!"));
327 #endif // wxUSE_STATUSBAR
330 void MyFrame
::OnQuit(wxCommandEvent
& WXUNUSED(event
))
332 // true is to force the frame to close
336 void MyFrame
::OnAbout(wxCommandEvent
& WXUNUSED(event
))
338 wxMessageBox(_T("wxCalendarCtrl sample\n(c) 2000 Vadim Zeitlin"),
339 _T("About Calendar"), wxOK
| wxICON_INFORMATION
, this);
342 void MyFrame
::OnCalMonday(wxCommandEvent
& event
)
344 bool enable
= GetMenuBar()->IsChecked(event
.GetId());
346 m_panel
->ToggleCalStyle(enable
, wxCAL_MONDAY_FIRST
);
349 void MyFrame
::OnCalHolidays(wxCommandEvent
& event
)
351 bool enable
= GetMenuBar()->IsChecked(event
.GetId());
353 m_panel
->GetCal()->EnableHolidayDisplay(enable
);
356 void MyFrame
::OnCalSpecial(wxCommandEvent
& event
)
358 m_panel
->HighlightSpecial(GetMenuBar()->IsChecked(event
.GetId()));
361 void MyFrame
::OnCalAllowMonth(wxCommandEvent
& event
)
363 bool allow
= GetMenuBar()->IsChecked(event
.GetId());
365 m_panel
->GetCal()->EnableMonthChange(allow
);
368 void MyFrame
::OnCalAllowYear(wxCommandEvent
& event
)
370 bool allow
= GetMenuBar()->IsChecked(event
.GetId());
372 m_panel
->GetCal()->EnableYearChange(allow
);
375 void MyFrame
::OnCalSeqMonth(wxCommandEvent
& event
)
377 bool allow
= GetMenuBar()->IsChecked(event
.GetId());
379 m_panel
->ToggleCalStyle(allow
, wxCAL_SEQUENTIAL_MONTH_SELECTION
);
382 void MyFrame
::OnCalShowSurroundingWeeks(wxCommandEvent
& event
)
384 bool allow
= GetMenuBar()->IsChecked(event
.GetId());
386 m_panel
->ToggleCalStyle(allow
, wxCAL_SHOW_SURROUNDING_WEEKS
);
389 void MyFrame
::OnAllowYearUpdate(wxUpdateUIEvent
& event
)
391 event
.Enable( GetMenuBar()->IsChecked(Calendar_Cal_Month
));
394 void MyFrame
::OnSetDate(wxCommandEvent
&WXUNUSED(event
))
399 void MyFrame
::OnToday(wxCommandEvent
&WXUNUSED(event
))
404 #if wxUSE_DATEPICKCTRL
406 void MyFrame
::OnAskDate(wxCommandEvent
& WXUNUSED(event
))
408 MyDialog
dlg(this, m_panel
->GetCal()->GetDate());
409 if ( dlg
.ShowModal() == wxID_OK
)
411 const wxDateTime dt
= dlg
.GetDate(),
412 today
= wxDateTime
::Today();
414 if ( dt
.GetDay() == today
.GetDay() &&
415 dt
.GetMonth() == today
.GetMonth() )
417 wxMessageBox(_T("Happy birthday!"), _T("Calendar Sample"));
420 m_panel
->GetCal()->SetDate(dt
);
422 wxLogStatus(_T("Changed the date to your birthday"));
426 #endif // wxUSE_DATEPICKCTRL
428 // ----------------------------------------------------------------------------
430 // ----------------------------------------------------------------------------
432 MyPanel
::MyPanel(wxFrame
*frame
)
433 : wxPanel(frame
, wxID_ANY
)
436 date
.Printf(wxT("Selected date: %s"),
437 wxDateTime
::Today().FormatISODate().c_str());
438 m_date
= new wxStaticText(this, wxID_ANY
, date
);
439 m_calendar
= new wxCalendarCtrl(this, Calendar_CalCtrl
,
444 wxCAL_SHOW_HOLIDAYS
|
447 wxBoxSizer
*m_sizer
= new wxBoxSizer( wxHORIZONTAL
);
449 m_sizer
->Add(m_date
, 0, wxALIGN_CENTER
| wxALL
, 10 );
450 m_sizer
->Add(m_calendar
, 0, wxALIGN_CENTER
| wxALIGN_LEFT
);
453 m_sizer
->SetSizeHints( this );
456 void MyPanel
::OnCalendar(wxCalendarEvent
& event
)
458 wxLogMessage(wxT("Selected %s from calendar"),
459 event
.GetDate().FormatISODate().c_str());
462 void MyPanel
::OnCalendarChange(wxCalendarEvent
& event
)
465 s
.Printf(wxT("Selected date: %s"), event
.GetDate().FormatISODate().c_str());
470 void MyPanel
::OnCalMonthChange(wxCalendarEvent
& WXUNUSED(event
))
472 wxLogStatus(wxT("Calendar month changed"));
475 void MyPanel
::OnCalYearChange(wxCalendarEvent
& WXUNUSED(event
))
477 wxLogStatus(wxT("Calendar year changed"));
480 void MyPanel
::OnCalendarWeekDayClick(wxCalendarEvent
& event
)
482 wxLogMessage(wxT("Clicked on %s"),
483 wxDateTime
::GetWeekDayName(event
.GetWeekDay()).c_str());
486 void MyPanel
::ToggleCalStyle(bool on
, int flag
)
488 long style
= m_calendar
->GetWindowStyle();
494 m_calendar
->SetWindowStyle(style
);
496 m_calendar
->Refresh();
499 void MyPanel
::HighlightSpecial(bool on
)
504 *attrRedCircle
= new wxCalendarDateAttr(wxCAL_BORDER_ROUND
, *wxRED
),
505 *attrGreenSquare
= new wxCalendarDateAttr(wxCAL_BORDER_SQUARE
, *wxGREEN
),
506 *attrHeaderLike
= new wxCalendarDateAttr(*wxBLUE
, *wxLIGHT_GREY
);
508 m_calendar
->SetAttr(17, attrRedCircle
);
509 m_calendar
->SetAttr(29, attrGreenSquare
);
510 m_calendar
->SetAttr(13, attrHeaderLike
);
514 m_calendar
->ResetAttr(17);
515 m_calendar
->ResetAttr(29);
516 m_calendar
->ResetAttr(13);
519 m_calendar
->Refresh();
522 void MyPanel
::SetDate()
524 wxDateTime
date(24, wxDateTime
::Dec
, 2005, 23, 59, 59);
525 m_calendar
->SetDate(date
);
528 void MyPanel
::Today()
530 m_calendar
->SetDate(wxDateTime
::Today());
533 // ----------------------------------------------------------------------------
535 // ----------------------------------------------------------------------------
537 #if wxUSE_DATEPICKCTRL
539 MyDialog
::MyDialog(wxWindow
*parent
, const wxDateTime
& dt
)
540 : wxDialog(parent
, -1, wxString(_T("Calendar: Choose a date")))
542 wxStdDialogButtonSizer
*sizerBtns
= new wxStdDialogButtonSizer
;
543 sizerBtns
->AddButton(new wxButton(this, wxID_OK
));
544 sizerBtns
->AddButton(new wxButton(this, wxID_CANCEL
));
545 sizerBtns
->Finalise();
547 wxSizer
*sizerText
= new wxBoxSizer(wxHORIZONTAL
);
548 sizerText
->Add(new wxStaticText(this, -1, _T("Date in ISO format: ")),
549 wxSizerFlags().Border());
550 m_text
= new wxTextCtrl(this, -1);
551 sizerText
->Add(m_text
, wxSizerFlags().Expand().Border());
553 wxSizer
*sizerTop
= new wxBoxSizer(wxVERTICAL
);
554 sizerTop
->Add(new wxStaticText
557 _T("Enter your birthday date (not before 20th century):")
559 wxSizerFlags().Border());
561 m_datePicker
= new wxDatePickerCtrl(this, -1, dt
);
562 m_datePicker
->SetRange(wxDateTime(1, wxDateTime
::Jan
, 1900),
564 sizerTop
->Add(m_datePicker
, wxSizerFlags().Expand().Border());
566 sizerTop
->AddStretchSpacer(1);
567 sizerTop
->Add(sizerText
);
569 sizerTop
->Add(sizerBtns
, wxSizerFlags().Centre().Border());
571 SetSizerAndFit(sizerTop
);
575 void MyDialog
::OnDateChange(wxDateEvent
& event
)
577 m_text
->SetValue(event
.GetDate().FormatISODate());
580 #endif // wxUSE_DATEPICKCTRL