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