]> git.saurik.com Git - wxWidgets.git/blame - samples/calendar/calendar.cpp
focus propagation, fixes #10047
[wxWidgets.git] / samples / calendar / calendar.cpp
CommitLineData
74a533f7
VZ
1/////////////////////////////////////////////////////////////////////////////
2// Name: calendar.cpp
3// Purpose: wxCalendarCtrl sample
4// Author: Vadim Zeitlin
4e6bceff 5// Modified by:
74a533f7
VZ
6// Created: 02.01.00
7// RCS-ID: $Id$
8// Copyright: (c) Vadim Zeitlin
9// Licence: wxWindows licence
10/////////////////////////////////////////////////////////////////////////////
11
12// ============================================================================
13// declarations
14// ============================================================================
15
16// ----------------------------------------------------------------------------
17// headers
18// ----------------------------------------------------------------------------
19
74a533f7
VZ
20// For compilers that support precompilation, includes "wx/wx.h".
21#include "wx/wxprec.h"
22
23#ifdef __BORLANDC__
24 #pragma hdrstop
25#endif
26
273b7ed9 27// for all others, include the necessary headers
74a533f7
VZ
28#ifndef WX_PRECOMP
29 #include "wx/app.h"
788233da 30 #include "wx/log.h"
74a533f7 31 #include "wx/frame.h"
273b7ed9
VZ
32 #include "wx/panel.h"
33 #include "wx/stattext.h"
34 #include "wx/menu.h"
35 #include "wx/layout.h"
36 #include "wx/msgdlg.h"
d9210ab7 37 #include "wx/icon.h"
574d4d1c
PC
38 #include "wx/button.h"
39 #include "wx/sizer.h"
40 #include "wx/textctrl.h"
41 #include "wx/settings.h"
74a533f7
VZ
42#endif
43
44#include "wx/calctrl.h"
628e155d 45#include "wx/splitter.h"
7ff2dfba
VZ
46
47#if wxUSE_DATEPICKCTRL
48 #include "wx/datectrl.h"
49 #if wxUSE_DATEPICKCTRL_GENERIC
50 #include "wx/generic/datectrl.h"
51 #endif // wxUSE_DATEPICKCTRL_GENERIC
52#endif // wxUSE_DATEPICKCTRL
feb72429 53
48263511 54#include "../sample.xpm"
3200f37d 55
628e155d
VZ
56#ifdef wxHAS_NATIVE_CALENDARCTRL
57 #include "wx/generic/calctrlg.h"
58#endif
59
74a533f7
VZ
60// ----------------------------------------------------------------------------
61// private classes
62// ----------------------------------------------------------------------------
63
64// Define a new application type, each program should derive a class from wxApp
65class MyApp : public wxApp
66{
67public:
68 // override base class virtuals
69 // ----------------------------
70
71 // this one is called on application startup and is a good place for the app
72 // initialization (doing it here and not in the ctor allows to have an error
73 // return: if OnInit() returns false, the application terminates)
74 virtual bool OnInit();
75};
76
77class MyPanel : public wxPanel
78{
79public:
628e155d 80 MyPanel(wxWindow *parent);
74a533f7
VZ
81
82 void OnCalendar(wxCalendarEvent& event);
83 void OnCalendarWeekDayClick(wxCalendarEvent& event);
84 void OnCalendarChange(wxCalendarEvent& event);
0de868d9 85 void OnCalMonthChange(wxCalendarEvent& event);
74a533f7 86
628e155d 87 wxCalendarCtrlBase *GetCal() const { return m_calendar; }
bc385ba9 88
37df1f33
VZ
89 // turn on/off the specified style bit on the calendar control
90 void ToggleCalStyle(bool on, int style);
91
628e155d
VZ
92 bool IsUsingGeneric() const { return m_usingGeneric; }
93 void ToggleUseGeneric()
94 {
95 m_usingGeneric = !m_usingGeneric;
96 RecreateCalendar(m_calendar->GetWindowStyle());
97 }
98
74a533f7
VZ
99 void HighlightSpecial(bool on);
100
a3c3a4cd
VZ
101 wxDateTime GetDate() const { return m_calendar->GetDate(); }
102 void SetDate(const wxDateTime& dt) { m_calendar->SetDate(dt); }
a7c58211 103
74a533f7 104private:
ee22a3a2
VZ
105 wxCalendarCtrlBase *DoCreateCalendar(const wxDateTime& dt, long style);
106
628e155d
VZ
107 void RecreateCalendar(long style);
108
109 wxCalendarCtrlBase *m_calendar;
74a533f7 110 wxStaticText *m_date;
2522d529 111 wxSizer *m_sizer;
74a533f7 112
628e155d
VZ
113 bool m_usingGeneric;
114
115
74a533f7
VZ
116 DECLARE_EVENT_TABLE()
117};
118
119// Define a new frame type: this is going to be our main frame
120class MyFrame : public wxFrame
121{
122public:
123 // ctor(s)
f9cc9706 124 MyFrame(const wxString& title, const wxPoint& pos = wxDefaultPosition, const wxSize& size = wxDefaultSize);
74a533f7
VZ
125
126 // event handlers (these functions should _not_ be virtual)
74a533f7 127 void OnAbout(wxCommandEvent& event);
628e155d
VZ
128 void OnClearLog(wxCommandEvent& event);
129 void OnQuit(wxCommandEvent& event);
74a533f7 130
ac78ab7b 131#if wxUSE_DATEPICKCTRL
feb72429 132 void OnAskDate(wxCommandEvent& event);
404e855d
VZ
133
134 void OnUpdateUIStartWithNone(wxUpdateUIEvent& event);
ac78ab7b 135#endif // wxUSE_DATEPICKCTRL
feb72429 136
628e155d
VZ
137#ifdef wxHAS_NATIVE_CALENDARCTRL
138 void OnCalGeneric(wxCommandEvent& WXUNUSED(event))
139 {
140 m_panel->ToggleUseGeneric();
141 }
142#endif // wxHAS_NATIVE_CALENDARCTRL
143
74a533f7
VZ
144 void OnCalMonday(wxCommandEvent& event);
145 void OnCalHolidays(wxCommandEvent& event);
146 void OnCalSpecial(wxCommandEvent& event);
147
bc385ba9 148 void OnCalAllowMonth(wxCommandEvent& event);
bc385ba9 149
37df1f33
VZ
150 void OnCalSeqMonth(wxCommandEvent& event);
151 void OnCalShowSurroundingWeeks(wxCommandEvent& event);
7b0ccb8a 152 void OnCalShowWeekNumbers(wxCommandEvent& event);
37df1f33 153
605dfd91
JS
154 void OnSetDate(wxCommandEvent& event);
155 void OnToday(wxCommandEvent& event);
a3c3a4cd 156 void OnBeginDST(wxCommandEvent& event);
605dfd91 157
5f324bb6
VZ
158 void OnCalToggleResizable(wxCommandEvent& event);
159
628e155d
VZ
160 void OnUpdateUIGenericOnly(wxUpdateUIEvent& event)
161 {
162 event.Enable(m_panel->IsUsingGeneric());
163 }
bc385ba9 164
ee22a3a2
VZ
165 void OnCalRClick(wxMouseEvent& event);
166
74a533f7
VZ
167private:
168 MyPanel *m_panel;
628e155d 169 wxTextCtrl *m_logWindow;
74a533f7 170
be5a51fb 171 // any class wishing to process wxWidgets events must use this macro
74a533f7
VZ
172 DECLARE_EVENT_TABLE()
173};
174
ac78ab7b 175#if wxUSE_DATEPICKCTRL
feb72429
VZ
176
177// Define a simple modal dialog which asks the user for a date
178class MyDialog : public wxDialog
179{
180public:
d1fd3c26 181 MyDialog(wxWindow *parent, const wxDateTime& dt, int dtpStyle);
feb72429
VZ
182
183 wxDateTime GetDate() const { return m_datePicker->GetValue(); }
184
185private:
186 void OnDateChange(wxDateEvent& event);
187
188
7ff2dfba 189 wxDatePickerCtrlBase *m_datePicker;
feb72429
VZ
190 wxTextCtrl *m_text;
191
192
193 DECLARE_EVENT_TABLE()
194};
195
ac78ab7b 196#endif // wxUSE_DATEPICKCTRL
feb72429 197
74a533f7
VZ
198// ----------------------------------------------------------------------------
199// constants
200// ----------------------------------------------------------------------------
201
202// IDs for the controls and the menu commands
203enum
204{
205 // menu items
feb72429 206 Calendar_File_About = wxID_ABOUT,
628e155d 207 Calendar_File_ClearLog = wxID_CLEAR,
feb72429 208 Calendar_File_Quit = wxID_EXIT,
628e155d
VZ
209 Calendar_Cal_Generic = 200,
210 Calendar_Cal_Monday,
74a533f7
VZ
211 Calendar_Cal_Holidays,
212 Calendar_Cal_Special,
bc385ba9 213 Calendar_Cal_Month,
37df1f33
VZ
214 Calendar_Cal_SeqMonth,
215 Calendar_Cal_SurroundWeeks,
7b0ccb8a 216 Calendar_Cal_WeekNumbers,
605dfd91
JS
217 Calendar_Cal_SetDate,
218 Calendar_Cal_Today,
a3c3a4cd 219 Calendar_Cal_BeginDST,
5f324bb6 220 Calendar_Cal_Resizable,
d1fd3c26
VZ
221#if wxUSE_DATEPICKCTRL
222 Calendar_DatePicker_AskDate = 300,
223 Calendar_DatePicker_ShowCentury,
224 Calendar_DatePicker_DropDown,
3200f37d 225 Calendar_DatePicker_AllowNone,
404e855d 226 Calendar_DatePicker_StartWithNone,
7ff2dfba
VZ
227#if wxUSE_DATEPICKCTRL_GENERIC
228 Calendar_DatePicker_Generic,
229#endif // wxUSE_DATEPICKCTRL_GENERIC
d1fd3c26 230#endif // wxUSE_DATEPICKCTRL
e9561b3b 231 Calendar_CalCtrl = 1000
74a533f7
VZ
232};
233
234// ----------------------------------------------------------------------------
be5a51fb 235// event tables and other macros for wxWidgets
74a533f7
VZ
236// ----------------------------------------------------------------------------
237
be5a51fb 238// the event tables connect the wxWidgets events with the functions (event
74a533f7
VZ
239// handlers) which process them. It can be also done at run-time, but for the
240// simple menu events like this the static method is much simpler.
241BEGIN_EVENT_TABLE(MyFrame, wxFrame)
74a533f7 242 EVT_MENU(Calendar_File_About, MyFrame::OnAbout)
628e155d
VZ
243 EVT_MENU(Calendar_File_ClearLog, MyFrame::OnClearLog)
244 EVT_MENU(Calendar_File_Quit, MyFrame::OnQuit)
74a533f7 245
ac78ab7b 246#if wxUSE_DATEPICKCTRL
d1fd3c26 247 EVT_MENU(Calendar_DatePicker_AskDate, MyFrame::OnAskDate)
404e855d
VZ
248
249 EVT_UPDATE_UI(Calendar_DatePicker_StartWithNone,
250 MyFrame::OnUpdateUIStartWithNone)
ac78ab7b 251#endif // wxUSE_DATEPICKCTRL
feb72429 252
628e155d
VZ
253#ifdef wxHAS_NATIVE_CALENDARCTRL
254 EVT_MENU(Calendar_Cal_Generic, MyFrame::OnCalGeneric)
255#endif // wxHAS_NATIVE_CALENDARCTRL
256
74a533f7
VZ
257 EVT_MENU(Calendar_Cal_Monday, MyFrame::OnCalMonday)
258 EVT_MENU(Calendar_Cal_Holidays, MyFrame::OnCalHolidays)
259 EVT_MENU(Calendar_Cal_Special, MyFrame::OnCalSpecial)
bc385ba9
VZ
260
261 EVT_MENU(Calendar_Cal_Month, MyFrame::OnCalAllowMonth)
bc385ba9 262
37df1f33
VZ
263 EVT_MENU(Calendar_Cal_SeqMonth, MyFrame::OnCalSeqMonth)
264 EVT_MENU(Calendar_Cal_SurroundWeeks, MyFrame::OnCalShowSurroundingWeeks)
7b0ccb8a 265 EVT_MENU(Calendar_Cal_WeekNumbers, MyFrame::OnCalShowWeekNumbers)
37df1f33 266
605dfd91
JS
267 EVT_MENU(Calendar_Cal_SetDate, MyFrame::OnSetDate)
268 EVT_MENU(Calendar_Cal_Today, MyFrame::OnToday)
a3c3a4cd 269 EVT_MENU(Calendar_Cal_BeginDST, MyFrame::OnBeginDST)
605dfd91 270
5f324bb6
VZ
271 EVT_MENU(Calendar_Cal_Resizable, MyFrame::OnCalToggleResizable)
272
605dfd91 273
628e155d 274 EVT_UPDATE_UI(Calendar_Cal_SeqMonth, MyFrame::OnUpdateUIGenericOnly)
db0b0942 275#ifdef __WXGTK20__
628e155d
VZ
276 EVT_UPDATE_UI(Calendar_Cal_Monday, MyFrame::OnUpdateUIGenericOnly)
277 EVT_UPDATE_UI(Calendar_Cal_Holidays, MyFrame::OnUpdateUIGenericOnly)
6d9b6716 278#endif
628e155d
VZ
279 EVT_UPDATE_UI(Calendar_Cal_Special, MyFrame::OnUpdateUIGenericOnly)
280 EVT_UPDATE_UI(Calendar_Cal_SurroundWeeks, MyFrame::OnUpdateUIGenericOnly)
74a533f7
VZ
281END_EVENT_TABLE()
282
283BEGIN_EVENT_TABLE(MyPanel, wxPanel)
628e155d
VZ
284 EVT_CALENDAR(Calendar_CalCtrl, MyPanel::OnCalendar)
285 EVT_CALENDAR_PAGE_CHANGED(Calendar_CalCtrl, MyPanel::OnCalMonthChange)
286 EVT_CALENDAR_SEL_CHANGED(Calendar_CalCtrl, MyPanel::OnCalendarChange)
74a533f7
VZ
287 EVT_CALENDAR_WEEKDAY_CLICKED(Calendar_CalCtrl, MyPanel::OnCalendarWeekDayClick)
288END_EVENT_TABLE()
289
ac78ab7b 290#if wxUSE_DATEPICKCTRL
feb72429
VZ
291
292BEGIN_EVENT_TABLE(MyDialog, wxDialog)
293 EVT_DATE_CHANGED(wxID_ANY, MyDialog::OnDateChange)
294END_EVENT_TABLE()
295
ac78ab7b 296#endif // wxUSE_DATEPICKCTRL
feb72429 297
be5a51fb 298// Create a new application object: this macro will allow wxWidgets to create
74a533f7
VZ
299// the application object during program execution (it's better than using a
300// static object for many reasons) and also declares the accessor function
301// wxGetApp() which will return the reference of the right type (i.e. MyApp and
302// not wxApp)
303IMPLEMENT_APP(MyApp)
304
305// ============================================================================
306// implementation
307// ============================================================================
308
309// ----------------------------------------------------------------------------
310// the application class
311// ----------------------------------------------------------------------------
312
313// `Main program' equivalent: the program execution "starts" here
314bool MyApp::OnInit()
315{
45e6e6f8
VZ
316 if ( !wxApp::OnInit() )
317 return false;
318
74a533f7 319 // Create the main application window
f9cc9706
WS
320 MyFrame *frame = new MyFrame(_T("Calendar wxWidgets sample")
321#ifndef __WXWINCE__
322 ,wxPoint(50, 50), wxSize(450, 340)
323#endif
324 );
74a533f7 325
9230b621 326 frame->Show(true);
74a533f7
VZ
327
328 // success: wxApp::OnRun() will be called which will enter the main message
5014bb3a 329 // loop and the application will run. If we returned false here, the
74a533f7 330 // application would exit immediately.
9230b621 331 return true;
74a533f7
VZ
332}
333
334// ----------------------------------------------------------------------------
335// main frame
336// ----------------------------------------------------------------------------
337
338// frame constructor
339MyFrame::MyFrame(const wxString& title, const wxPoint& pos, const wxSize& size)
9230b621 340 : wxFrame((wxFrame *)NULL, wxID_ANY, title, pos, size)
74a533f7 341{
3200f37d 342 // set the frame icon
b58a81c4 343 SetIcon(wxIcon(sample_xpm));
3200f37d 344
74a533f7
VZ
345 // create a menu bar
346 wxMenu *menuFile = new wxMenu;
9f84eccd 347 menuFile->Append(Calendar_File_About, _T("&About...\tCtrl-A"), _T("Show about dialog"));
74a533f7 348 menuFile->AppendSeparator();
628e155d
VZ
349 menuFile->Append(Calendar_File_ClearLog, _T("&Clear log\tCtrl-L"));
350 menuFile->AppendSeparator();
9f84eccd 351 menuFile->Append(Calendar_File_Quit, _T("E&xit\tAlt-X"), _T("Quit this program"));
74a533f7
VZ
352
353 wxMenu *menuCal = new wxMenu;
628e155d
VZ
354#ifdef wxHAS_NATIVE_CALENDARCTRL
355 menuCal->AppendCheckItem(Calendar_Cal_Generic, "Use &generic version\tCtrl-G",
356 "Toggle between native and generic control");
357 menuCal->AppendSeparator();
358#endif // wxHAS_NATIVE_CALENDARCTRL
74a533f7 359 menuCal->Append(Calendar_Cal_Monday,
9f84eccd
MB
360 _T("Monday &first weekday\tCtrl-F"),
361 _T("Toggle between Mon and Sun as the first week day"),
9230b621 362 true);
9f84eccd
MB
363 menuCal->Append(Calendar_Cal_Holidays, _T("Show &holidays\tCtrl-H"),
364 _T("Toggle highlighting the holidays"),
9230b621 365 true);
9f84eccd
MB
366 menuCal->Append(Calendar_Cal_Special, _T("Highlight &special dates\tCtrl-S"),
367 _T("Test custom highlighting"),
9230b621 368 true);
37df1f33 369 menuCal->Append(Calendar_Cal_SurroundWeeks,
9f84eccd
MB
370 _T("Show s&urrounding weeks\tCtrl-W"),
371 _T("Show the neighbouring weeks in the prev/next month"),
9230b621 372 true);
7b0ccb8a
RR
373 menuCal->Append(Calendar_Cal_WeekNumbers,
374 _T("Show &week numbers"),
375 _T("Toggle week numbers"),
376 true);
bc385ba9 377 menuCal->AppendSeparator();
37df1f33 378 menuCal->Append(Calendar_Cal_SeqMonth,
628e155d 379 _T("Toggle month selector st&yle\tCtrl-Y"),
9f84eccd 380 _T("Use another style for the calendar controls"),
9230b621 381 true);
9f84eccd
MB
382 menuCal->Append(Calendar_Cal_Month, _T("&Month can be changed\tCtrl-M"),
383 _T("Allow changing the month in the calendar"),
9230b621 384 true);
605dfd91 385 menuCal->AppendSeparator();
404e855d 386 menuCal->Append(Calendar_Cal_SetDate, _T("Call &SetDate(2005-12-24)"), _T("Set date to 2005-12-24."));
a3c3a4cd
VZ
387 menuCal->Append(Calendar_Cal_Today, _T("Call &Today()"), _T("Set to the current date."));
388 menuCal->Append(Calendar_Cal_BeginDST, "Call SetDate(GetBeginDST())");
5f324bb6
VZ
389 menuCal->AppendSeparator();
390 menuCal->AppendCheckItem(Calendar_Cal_Resizable, _T("Make &resizable\tCtrl-R"));
74a533f7 391
d1fd3c26
VZ
392#if wxUSE_DATEPICKCTRL
393 wxMenu *menuDate = new wxMenu;
394 menuDate->AppendCheckItem(Calendar_DatePicker_ShowCentury,
395 _T("Al&ways show century"));
396 menuDate->AppendCheckItem(Calendar_DatePicker_DropDown,
397 _T("Use &drop down control"));
3200f37d
VZ
398 menuDate->AppendCheckItem(Calendar_DatePicker_AllowNone,
399 _T("Allow &no date"));
404e855d
VZ
400 menuDate->AppendCheckItem(Calendar_DatePicker_StartWithNone,
401 _T("Start &with no date"));
7ff2dfba
VZ
402#if wxUSE_DATEPICKCTRL_GENERIC
403 menuDate->AppendCheckItem(Calendar_DatePicker_Generic,
404 _T("Use &generic version of the control"));
405#endif // wxUSE_DATEPICKCTRL_GENERIC
d1fd3c26
VZ
406 menuDate->AppendSeparator();
407 menuDate->Append(Calendar_DatePicker_AskDate, _T("&Choose date...\tCtrl-D"), _T("Show dialog with wxDatePickerCtrl"));
408#endif // wxUSE_DATEPICKCTRL
409
74a533f7
VZ
410 // now append the freshly created menu to the menu bar...
411 wxMenuBar *menuBar = new wxMenuBar;
9f84eccd
MB
412 menuBar->Append(menuFile, _T("&File"));
413 menuBar->Append(menuCal, _T("&Calendar"));
d1fd3c26
VZ
414#if wxUSE_DATEPICKCTRL
415 menuBar->Append(menuDate, _T("&Date picker"));
416#endif // wxUSE_DATEPICKCTRL
74a533f7 417
9230b621
VS
418 menuBar->Check(Calendar_Cal_Monday, true);
419 menuBar->Check(Calendar_Cal_Holidays, true);
420 menuBar->Check(Calendar_Cal_Month, true);
74a533f7 421
d1fd3c26
VZ
422#if wxUSE_DATEPICKCTRL
423 menuBar->Check(Calendar_DatePicker_ShowCentury, true);
424#endif // wxUSE_DATEPICKCTRL
425
74a533f7
VZ
426 // ... and attach this menu bar to the frame
427 SetMenuBar(menuBar);
428
628e155d
VZ
429 wxSplitterWindow *splitter = new wxSplitterWindow(this, wxID_ANY,
430 wxDefaultPosition, wxDefaultSize,
431 wxSP_NOBORDER);
432 m_panel = new MyPanel(splitter);
433 m_logWindow = new wxTextCtrl(splitter, wxID_ANY, wxEmptyString,
434 wxDefaultPosition, wxDefaultSize,
435 wxTE_READONLY | wxTE_MULTILINE);
436 splitter->SplitHorizontally(m_panel, m_logWindow);
437 splitter->SetMinimumPaneSize(20);
438 wxLog::SetActiveTarget(new wxLogTextCtrl(m_logWindow));
74a533f7
VZ
439}
440
441void MyFrame::OnQuit(wxCommandEvent& WXUNUSED(event))
442{
5014bb3a 443 // true is to force the frame to close
9230b621 444 Close(true);
74a533f7
VZ
445}
446
447void MyFrame::OnAbout(wxCommandEvent& WXUNUSED(event))
448{
628e155d 449 wxMessageBox(_T("wxCalendarCtrl sample\n(c) 2000--2008 Vadim Zeitlin"),
74a533f7
VZ
450 _T("About Calendar"), wxOK | wxICON_INFORMATION, this);
451}
452
628e155d 453void MyFrame::OnClearLog(wxCommandEvent& WXUNUSED(event))
74a533f7 454{
628e155d
VZ
455 m_logWindow->Clear();
456}
37df1f33 457
628e155d
VZ
458void MyFrame::OnCalMonday(wxCommandEvent& event)
459{
460 m_panel->ToggleCalStyle(event.IsChecked(), wxCAL_MONDAY_FIRST);
74a533f7
VZ
461}
462
463void MyFrame::OnCalHolidays(wxCommandEvent& event)
464{
bf956fac 465 m_panel->GetCal()->EnableHolidayDisplay(event.IsChecked());
74a533f7
VZ
466}
467
468void MyFrame::OnCalSpecial(wxCommandEvent& event)
469{
89a0a322 470 m_panel->HighlightSpecial(GetMenuBar()->IsChecked(event.GetId()));
bc385ba9
VZ
471}
472
473void MyFrame::OnCalAllowMonth(wxCommandEvent& event)
474{
628e155d 475 m_panel->GetCal()->EnableMonthChange(event.IsChecked());
bc385ba9
VZ
476}
477
37df1f33
VZ
478void MyFrame::OnCalSeqMonth(wxCommandEvent& event)
479{
628e155d
VZ
480 m_panel->ToggleCalStyle(event.IsChecked(),
481 wxCAL_SEQUENTIAL_MONTH_SELECTION);
37df1f33
VZ
482}
483
484void MyFrame::OnCalShowSurroundingWeeks(wxCommandEvent& event)
485{
628e155d 486 m_panel->ToggleCalStyle(event.IsChecked(), wxCAL_SHOW_SURROUNDING_WEEKS);
74a533f7
VZ
487}
488
7b0ccb8a
RR
489void MyFrame::OnCalShowWeekNumbers(wxCommandEvent& event)
490{
491 m_panel->ToggleCalStyle(event.IsChecked(), wxCAL_SHOW_WEEK_NUMBERS);
492}
493
87728739 494void MyFrame::OnSetDate(wxCommandEvent &WXUNUSED(event))
605dfd91 495{
628e155d 496 m_panel->SetDate(wxDateTime(24, wxDateTime::Dec, 2005, 22, 00, 00));
605dfd91
JS
497}
498
87728739 499void MyFrame::OnToday(wxCommandEvent &WXUNUSED(event))
605dfd91 500{
a3c3a4cd
VZ
501 m_panel->SetDate(wxDateTime::Today());
502}
503
504void MyFrame::OnBeginDST(wxCommandEvent &WXUNUSED(event))
505{
506 m_panel->SetDate(wxDateTime::GetBeginDST(m_panel->GetDate().GetYear()));
605dfd91
JS
507}
508
5f324bb6
VZ
509void MyFrame::OnCalToggleResizable(wxCommandEvent& event)
510{
511 wxSizer * const sizer = m_panel->GetSizer();
512 wxSizerItem * const item = sizer->GetItem(m_panel->GetCal());
513 if ( event.IsChecked() )
514 {
515 item->SetProportion(1);
516 item->SetFlag(wxEXPAND);
517 }
518 else // not resizable
519 {
520 item->SetProportion(0);
521 item->SetFlag(wxALIGN_CENTER);
522 }
523
524 sizer->Layout();
525}
526
ee22a3a2
VZ
527void MyFrame::OnCalRClick(wxMouseEvent& event)
528{
529 wxDateTime dt;
530 wxDateTime::WeekDay wd;
531
532 const wxPoint pt = event.GetPosition();
533 wxString msg = wxString::Format("Point (%d, %d) is ", pt.x, pt.y);
534
535 switch ( m_panel->GetCal()->HitTest(pt, &dt, &wd) )
536 {
537 default:
538 wxFAIL_MSG( "unexpected" );
539 // fall through
540
541 case wxCAL_HITTEST_NOWHERE:
542 msg += "nowhere";
543 break;
544
545 case wxCAL_HITTEST_HEADER:
546 msg += wxString::Format("over %s", wxDateTime::GetWeekDayName(wd));
547 break;
548
549 case wxCAL_HITTEST_DAY:
550 msg += wxString::Format("over %s", dt.FormatISODate());
551 break;
552
553 case wxCAL_HITTEST_INCMONTH:
554 msg += "over next month button";
555 break;
556
557 case wxCAL_HITTEST_DECMONTH:
558 msg += "over previous month button";
559 break;
560
561 case wxCAL_HITTEST_SURROUNDING_WEEK:
562 msg += "over a day from another month";
563 break;
564 }
565
566 wxLogMessage("%s", msg);
567}
568
ac78ab7b 569#if wxUSE_DATEPICKCTRL
feb72429 570
404e855d
VZ
571void MyFrame::OnUpdateUIStartWithNone(wxUpdateUIEvent& event)
572{
573 // it only makes sense to start with invalid date if we can have no date
574 event.Enable( GetMenuBar()->IsChecked(Calendar_DatePicker_AllowNone) );
575}
576
feb72429
VZ
577void MyFrame::OnAskDate(wxCommandEvent& WXUNUSED(event))
578{
404e855d
VZ
579 wxDateTime dt = m_panel->GetCal()->GetDate();
580
d1fd3c26
VZ
581 int style = wxDP_DEFAULT;
582 if ( GetMenuBar()->IsChecked(Calendar_DatePicker_ShowCentury) )
583 style |= wxDP_SHOWCENTURY;
584 if ( GetMenuBar()->IsChecked(Calendar_DatePicker_DropDown) )
585 style |= wxDP_DROPDOWN;
3200f37d 586 if ( GetMenuBar()->IsChecked(Calendar_DatePicker_AllowNone) )
404e855d 587 {
3200f37d 588 style |= wxDP_ALLOWNONE;
d1fd3c26 589
404e855d
VZ
590 if ( GetMenuBar()->IsChecked(Calendar_DatePicker_StartWithNone) )
591 dt = wxDefaultDateTime;
592 }
593
594 MyDialog dlg(this, dt, style);
feb72429
VZ
595 if ( dlg.ShowModal() == wxID_OK )
596 {
404e855d 597 dt = dlg.GetDate();
3200f37d 598 if ( dt.IsValid() )
feb72429 599 {
3200f37d 600 const wxDateTime today = wxDateTime::Today();
feb72429 601
3200f37d
VZ
602 if ( dt.GetDay() == today.GetDay() &&
603 dt.GetMonth() == today.GetMonth() )
604 {
605 wxMessageBox(_T("Happy birthday!"), _T("Calendar Sample"));
606 }
feb72429 607
a3c3a4cd 608 m_panel->SetDate(dt);
3200f37d
VZ
609
610 wxLogStatus(_T("Changed the date to your input"));
611 }
612 else
613 {
614 wxLogStatus(_T("No date entered"));
615 }
feb72429
VZ
616 }
617}
618
ac78ab7b 619#endif // wxUSE_DATEPICKCTRL
feb72429 620
74a533f7
VZ
621// ----------------------------------------------------------------------------
622// MyPanel
623// ----------------------------------------------------------------------------
624
628e155d
VZ
625MyPanel::MyPanel(wxWindow *parent)
626 : wxPanel(parent, wxID_ANY)
74a533f7 627{
628e155d
VZ
628#ifdef wxHAS_NATIVE_CALENDARCTRL
629 m_usingGeneric = false;
630#else
631 m_usingGeneric = true;
632#endif
633
4e6bceff 634 wxString date;
4693b20c 635 date.Printf(wxT("Selected date: %s"),
74a533f7 636 wxDateTime::Today().FormatISODate().c_str());
9230b621 637 m_date = new wxStaticText(this, wxID_ANY, date);
ee22a3a2
VZ
638 m_calendar = DoCreateCalendar(wxDefaultDateTime,
639 wxCAL_MONDAY_FIRST | wxCAL_SHOW_HOLIDAYS);
74a533f7 640
f9cc9706
WS
641 // adjust to vertical/horizontal display, check mostly dedicated to WinCE
642 bool horizontal = ( wxSystemSettings::GetMetric(wxSYS_SCREEN_X) > wxSystemSettings::GetMetric(wxSYS_SCREEN_Y) );
2522d529 643 m_sizer = new wxBoxSizer( horizontal ? wxHORIZONTAL : wxVERTICAL );
4e6bceff 644
9230b621
VS
645 m_sizer->Add(m_date, 0, wxALIGN_CENTER | wxALL, 10 );
646 m_sizer->Add(m_calendar, 0, wxALIGN_CENTER | wxALIGN_LEFT);
4e6bceff 647
9230b621
VS
648 SetSizer( m_sizer );
649 m_sizer->SetSizeHints( this );
74a533f7
VZ
650}
651
652void MyPanel::OnCalendar(wxCalendarEvent& event)
653{
82c6027b
VZ
654 // clicking the same date twice unmarks it (convenient for testing)
655 static wxDateTime s_dateLast;
656 const bool mark = !s_dateLast.IsValid() || event.GetDate() != s_dateLast;
657
658 s_dateLast = event.GetDate();
659
660 m_calendar->Mark(event.GetDate().GetDay(), mark);
661 wxLogMessage(wxT("Selected (and %smarked) %s from calendar."),
662 mark ? "" : "un", s_dateLast.FormatISODate().c_str());
74a533f7
VZ
663}
664
665void MyPanel::OnCalendarChange(wxCalendarEvent& event)
666{
667 wxString s;
4693b20c 668 s.Printf(wxT("Selected date: %s"), event.GetDate().FormatISODate().c_str());
74a533f7
VZ
669
670 m_date->SetLabel(s);
628e155d 671 wxLogStatus(s);
74a533f7
VZ
672}
673
628e155d 674void MyPanel::OnCalMonthChange(wxCalendarEvent& event)
0de868d9 675{
628e155d
VZ
676 wxLogStatus(wxT("Calendar month changed to %s %d"),
677 wxDateTime::GetMonthName(event.GetDate().GetMonth()),
678 event.GetDate().GetYear());
0de868d9
VZ
679}
680
74a533f7
VZ
681void MyPanel::OnCalendarWeekDayClick(wxCalendarEvent& event)
682{
4693b20c 683 wxLogMessage(wxT("Clicked on %s"),
74a533f7
VZ
684 wxDateTime::GetWeekDayName(event.GetWeekDay()).c_str());
685}
686
ee22a3a2 687wxCalendarCtrlBase *MyPanel::DoCreateCalendar(const wxDateTime& dt, long style)
628e155d
VZ
688{
689 wxCalendarCtrlBase *calendar;
690#ifdef wxHAS_NATIVE_CALENDARCTRL
691 if ( m_usingGeneric )
692 calendar = new wxGenericCalendarCtrl(this, Calendar_CalCtrl,
ee22a3a2 693 dt,
628e155d
VZ
694 wxDefaultPosition,
695 wxDefaultSize,
696 style);
697 else
698#endif // wxHAS_NATIVE_CALENDARCTRL
699 calendar = new wxCalendarCtrl(this, Calendar_CalCtrl,
ee22a3a2 700 dt,
628e155d
VZ
701 wxDefaultPosition,
702 wxDefaultSize,
703 style);
704
ee22a3a2
VZ
705 calendar->Connect(wxEVT_RIGHT_DOWN,
706 wxMouseEventHandler(MyFrame::OnCalRClick),
707 NULL,
708 wxGetTopLevelParent(this));
709
710 return calendar;
711}
712
713void MyPanel::RecreateCalendar(long style)
714{
715 wxCalendarCtrlBase *calendar = DoCreateCalendar(m_calendar->GetDate(), style);
716
628e155d
VZ
717 m_sizer->Replace(m_calendar, calendar);
718 delete m_calendar;
719 m_calendar = calendar;
720
721 m_sizer->Layout();
722}
723
37df1f33 724void MyPanel::ToggleCalStyle(bool on, int flag)
74a533f7
VZ
725{
726 long style = m_calendar->GetWindowStyle();
727 if ( on )
37df1f33 728 style |= flag;
74a533f7 729 else
37df1f33 730 style &= ~flag;
74a533f7 731
7b0ccb8a
RR
732 if ( flag == wxCAL_SEQUENTIAL_MONTH_SELECTION
733 || flag == wxCAL_SHOW_WEEK_NUMBERS)
2522d529
VZ
734 {
735 // changing this style requires recreating the control
628e155d 736 RecreateCalendar(style);
2522d529
VZ
737 }
738 else // just changing the style is enough
739 {
740 m_calendar->SetWindowStyle(style);
74a533f7 741
2522d529
VZ
742 m_calendar->Refresh();
743 }
74a533f7
VZ
744}
745
74a533f7
VZ
746void MyPanel::HighlightSpecial(bool on)
747{
748 if ( on )
749 {
750 wxCalendarDateAttr
751 *attrRedCircle = new wxCalendarDateAttr(wxCAL_BORDER_ROUND, *wxRED),
752 *attrGreenSquare = new wxCalendarDateAttr(wxCAL_BORDER_SQUARE, *wxGREEN),
753 *attrHeaderLike = new wxCalendarDateAttr(*wxBLUE, *wxLIGHT_GREY);
754
755 m_calendar->SetAttr(17, attrRedCircle);
756 m_calendar->SetAttr(29, attrGreenSquare);
757 m_calendar->SetAttr(13, attrHeaderLike);
758 }
759 else // off
760 {
761 m_calendar->ResetAttr(17);
762 m_calendar->ResetAttr(29);
763 m_calendar->ResetAttr(13);
764 }
765
766 m_calendar->Refresh();
767}
605dfd91 768
628e155d 769
feb72429
VZ
770// ----------------------------------------------------------------------------
771// MyDialog
772// ----------------------------------------------------------------------------
773
ac78ab7b 774#if wxUSE_DATEPICKCTRL
feb72429 775
d1fd3c26 776MyDialog::MyDialog(wxWindow *parent, const wxDateTime& dt, int dtpStyle)
a7c58211 777 : wxDialog(parent, wxID_ANY, wxString(_T("Calendar: Choose a date")))
feb72429
VZ
778{
779 wxStdDialogButtonSizer *sizerBtns = new wxStdDialogButtonSizer;
780 sizerBtns->AddButton(new wxButton(this, wxID_OK));
781 sizerBtns->AddButton(new wxButton(this, wxID_CANCEL));
718903fe 782 sizerBtns->Realize();
feb72429
VZ
783
784 wxSizer *sizerText = new wxBoxSizer(wxHORIZONTAL);
a7c58211 785 sizerText->Add(new wxStaticText(this, wxID_ANY, _T("Date in ISO format: ")),
b7e542be 786 wxSizerFlags().Border().Align(wxALIGN_CENTRE_VERTICAL));
a7c58211 787 m_text = new wxTextCtrl(this, wxID_ANY);
b7e542be
VZ
788 sizerText->Add(m_text, wxSizerFlags().
789 Expand().Border().Align(wxALIGN_CENTRE_VERTICAL));
feb72429
VZ
790
791 wxSizer *sizerTop = new wxBoxSizer(wxVERTICAL);
792 sizerTop->Add(new wxStaticText
793 (
a7c58211 794 this, wxID_ANY,
feb72429
VZ
795 _T("Enter your birthday date (not before 20th century):")
796 ),
797 wxSizerFlags().Border());
798
7ff2dfba
VZ
799#if wxUSE_DATEPICKCTRL_GENERIC
800 wxFrame *frame = (wxFrame *)wxGetTopLevelParent(parent);
801 if ( frame && frame->GetMenuBar()->IsChecked(Calendar_DatePicker_Generic) )
a7c58211 802 m_datePicker = new wxDatePickerCtrlGeneric(this, wxID_ANY, dt,
7ff2dfba
VZ
803 wxDefaultPosition,
804 wxDefaultSize,
805 dtpStyle);
806 else
807#endif // wxUSE_DATEPICKCTRL_GENERIC
a7c58211 808 m_datePicker = new wxDatePickerCtrl(this, wxID_ANY, dt,
d1fd3c26
VZ
809 wxDefaultPosition, wxDefaultSize,
810 dtpStyle);
feb72429
VZ
811 m_datePicker->SetRange(wxDateTime(1, wxDateTime::Jan, 1900),
812 wxDefaultDateTime);
813 sizerTop->Add(m_datePicker, wxSizerFlags().Expand().Border());
814
815 sizerTop->AddStretchSpacer(1);
816 sizerTop->Add(sizerText);
817
818 sizerTop->Add(sizerBtns, wxSizerFlags().Centre().Border());
819
820 SetSizerAndFit(sizerTop);
821 Layout();
822}
823
824void MyDialog::OnDateChange(wxDateEvent& event)
825{
b7e542be 826 const wxDateTime dt = event.GetDate();
404e855d 827 if ( dt.IsValid() )
a7c58211
WS
828 m_text->SetValue(dt.FormatISODate());
829 else
830 m_text->SetValue(wxEmptyString);
feb72429
VZ
831}
832
ac78ab7b 833#endif // wxUSE_DATEPICKCTRL