]> git.saurik.com Git - wxWidgets.git/blame - samples/calendar/calendar.cpp
moved some wxMimeTypeCommands methods into .cpp file from header, this generally...
[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"
7ff2dfba
VZ
45
46#if wxUSE_DATEPICKCTRL
47 #include "wx/datectrl.h"
48 #if wxUSE_DATEPICKCTRL_GENERIC
49 #include "wx/generic/datectrl.h"
50 #endif // wxUSE_DATEPICKCTRL_GENERIC
51#endif // wxUSE_DATEPICKCTRL
feb72429 52
48263511 53#include "../sample.xpm"
3200f37d 54
74a533f7
VZ
55// ----------------------------------------------------------------------------
56// private classes
57// ----------------------------------------------------------------------------
58
59// Define a new application type, each program should derive a class from wxApp
60class MyApp : public wxApp
61{
62public:
63 // override base class virtuals
64 // ----------------------------
65
66 // this one is called on application startup and is a good place for the app
67 // initialization (doing it here and not in the ctor allows to have an error
68 // return: if OnInit() returns false, the application terminates)
69 virtual bool OnInit();
70};
71
72class MyPanel : public wxPanel
73{
74public:
75 MyPanel(wxFrame *frame);
76
77 void OnCalendar(wxCalendarEvent& event);
78 void OnCalendarWeekDayClick(wxCalendarEvent& event);
79 void OnCalendarChange(wxCalendarEvent& event);
0de868d9
VZ
80 void OnCalMonthChange(wxCalendarEvent& event);
81 void OnCalYearChange(wxCalendarEvent& event);
74a533f7 82
bc385ba9
VZ
83 wxCalendarCtrl *GetCal() const { return m_calendar; }
84
37df1f33
VZ
85 // turn on/off the specified style bit on the calendar control
86 void ToggleCalStyle(bool on, int style);
87
74a533f7
VZ
88 void HighlightSpecial(bool on);
89
605dfd91
JS
90 void SetDate();
91 void Today();
a7c58211 92
74a533f7
VZ
93private:
94 wxCalendarCtrl *m_calendar;
95 wxStaticText *m_date;
2522d529 96 wxSizer *m_sizer;
74a533f7
VZ
97
98 DECLARE_EVENT_TABLE()
99};
100
101// Define a new frame type: this is going to be our main frame
102class MyFrame : public wxFrame
103{
104public:
105 // ctor(s)
f9cc9706 106 MyFrame(const wxString& title, const wxPoint& pos = wxDefaultPosition, const wxSize& size = wxDefaultSize);
74a533f7
VZ
107
108 // event handlers (these functions should _not_ be virtual)
109 void OnQuit(wxCommandEvent& event);
110 void OnAbout(wxCommandEvent& event);
111
ac78ab7b 112#if wxUSE_DATEPICKCTRL
feb72429 113 void OnAskDate(wxCommandEvent& event);
404e855d
VZ
114
115 void OnUpdateUIStartWithNone(wxUpdateUIEvent& event);
ac78ab7b 116#endif // wxUSE_DATEPICKCTRL
feb72429 117
74a533f7
VZ
118 void OnCalMonday(wxCommandEvent& event);
119 void OnCalHolidays(wxCommandEvent& event);
120 void OnCalSpecial(wxCommandEvent& event);
121
bc385ba9
VZ
122 void OnCalAllowMonth(wxCommandEvent& event);
123 void OnCalAllowYear(wxCommandEvent& event);
124
37df1f33
VZ
125 void OnCalSeqMonth(wxCommandEvent& event);
126 void OnCalShowSurroundingWeeks(wxCommandEvent& event);
127
605dfd91
JS
128 void OnSetDate(wxCommandEvent& event);
129 void OnToday(wxCommandEvent& event);
130
bc385ba9
VZ
131 void OnAllowYearUpdate(wxUpdateUIEvent& event);
132
74a533f7
VZ
133private:
134 MyPanel *m_panel;
135
be5a51fb 136 // any class wishing to process wxWidgets events must use this macro
74a533f7
VZ
137 DECLARE_EVENT_TABLE()
138};
139
ac78ab7b 140#if wxUSE_DATEPICKCTRL
feb72429
VZ
141
142// Define a simple modal dialog which asks the user for a date
143class MyDialog : public wxDialog
144{
145public:
d1fd3c26 146 MyDialog(wxWindow *parent, const wxDateTime& dt, int dtpStyle);
feb72429
VZ
147
148 wxDateTime GetDate() const { return m_datePicker->GetValue(); }
149
150private:
151 void OnDateChange(wxDateEvent& event);
152
153
7ff2dfba 154 wxDatePickerCtrlBase *m_datePicker;
feb72429
VZ
155 wxTextCtrl *m_text;
156
157
158 DECLARE_EVENT_TABLE()
159};
160
ac78ab7b 161#endif // wxUSE_DATEPICKCTRL
feb72429 162
74a533f7
VZ
163// ----------------------------------------------------------------------------
164// constants
165// ----------------------------------------------------------------------------
166
167// IDs for the controls and the menu commands
168enum
169{
170 // menu items
feb72429
VZ
171 Calendar_File_About = wxID_ABOUT,
172 Calendar_File_Quit = wxID_EXIT,
74a533f7
VZ
173 Calendar_Cal_Monday = 200,
174 Calendar_Cal_Holidays,
175 Calendar_Cal_Special,
bc385ba9
VZ
176 Calendar_Cal_Month,
177 Calendar_Cal_Year,
37df1f33
VZ
178 Calendar_Cal_SeqMonth,
179 Calendar_Cal_SurroundWeeks,
605dfd91
JS
180 Calendar_Cal_SetDate,
181 Calendar_Cal_Today,
d1fd3c26
VZ
182#if wxUSE_DATEPICKCTRL
183 Calendar_DatePicker_AskDate = 300,
184 Calendar_DatePicker_ShowCentury,
185 Calendar_DatePicker_DropDown,
3200f37d 186 Calendar_DatePicker_AllowNone,
404e855d 187 Calendar_DatePicker_StartWithNone,
7ff2dfba
VZ
188#if wxUSE_DATEPICKCTRL_GENERIC
189 Calendar_DatePicker_Generic,
190#endif // wxUSE_DATEPICKCTRL_GENERIC
d1fd3c26 191#endif // wxUSE_DATEPICKCTRL
e9561b3b 192 Calendar_CalCtrl = 1000
74a533f7
VZ
193};
194
195// ----------------------------------------------------------------------------
be5a51fb 196// event tables and other macros for wxWidgets
74a533f7
VZ
197// ----------------------------------------------------------------------------
198
be5a51fb 199// the event tables connect the wxWidgets events with the functions (event
74a533f7
VZ
200// handlers) which process them. It can be also done at run-time, but for the
201// simple menu events like this the static method is much simpler.
202BEGIN_EVENT_TABLE(MyFrame, wxFrame)
203 EVT_MENU(Calendar_File_Quit, MyFrame::OnQuit)
204 EVT_MENU(Calendar_File_About, MyFrame::OnAbout)
205
ac78ab7b 206#if wxUSE_DATEPICKCTRL
d1fd3c26 207 EVT_MENU(Calendar_DatePicker_AskDate, MyFrame::OnAskDate)
404e855d
VZ
208
209 EVT_UPDATE_UI(Calendar_DatePicker_StartWithNone,
210 MyFrame::OnUpdateUIStartWithNone)
ac78ab7b 211#endif // wxUSE_DATEPICKCTRL
feb72429 212
74a533f7
VZ
213 EVT_MENU(Calendar_Cal_Monday, MyFrame::OnCalMonday)
214 EVT_MENU(Calendar_Cal_Holidays, MyFrame::OnCalHolidays)
215 EVT_MENU(Calendar_Cal_Special, MyFrame::OnCalSpecial)
bc385ba9
VZ
216
217 EVT_MENU(Calendar_Cal_Month, MyFrame::OnCalAllowMonth)
218 EVT_MENU(Calendar_Cal_Year, MyFrame::OnCalAllowYear)
219
37df1f33
VZ
220 EVT_MENU(Calendar_Cal_SeqMonth, MyFrame::OnCalSeqMonth)
221 EVT_MENU(Calendar_Cal_SurroundWeeks, MyFrame::OnCalShowSurroundingWeeks)
222
605dfd91
JS
223 EVT_MENU(Calendar_Cal_SetDate, MyFrame::OnSetDate)
224 EVT_MENU(Calendar_Cal_Today, MyFrame::OnToday)
225
226
bc385ba9 227 EVT_UPDATE_UI(Calendar_Cal_Year, MyFrame::OnAllowYearUpdate)
74a533f7
VZ
228END_EVENT_TABLE()
229
230BEGIN_EVENT_TABLE(MyPanel, wxPanel)
231 EVT_CALENDAR (Calendar_CalCtrl, MyPanel::OnCalendar)
0de868d9
VZ
232 EVT_CALENDAR_MONTH (Calendar_CalCtrl, MyPanel::OnCalMonthChange)
233 EVT_CALENDAR_YEAR (Calendar_CalCtrl, MyPanel::OnCalYearChange)
74a533f7
VZ
234 EVT_CALENDAR_SEL_CHANGED(Calendar_CalCtrl, MyPanel::OnCalendarChange)
235 EVT_CALENDAR_WEEKDAY_CLICKED(Calendar_CalCtrl, MyPanel::OnCalendarWeekDayClick)
236END_EVENT_TABLE()
237
ac78ab7b 238#if wxUSE_DATEPICKCTRL
feb72429
VZ
239
240BEGIN_EVENT_TABLE(MyDialog, wxDialog)
241 EVT_DATE_CHANGED(wxID_ANY, MyDialog::OnDateChange)
242END_EVENT_TABLE()
243
ac78ab7b 244#endif // wxUSE_DATEPICKCTRL
feb72429 245
be5a51fb 246// Create a new application object: this macro will allow wxWidgets to create
74a533f7
VZ
247// the application object during program execution (it's better than using a
248// static object for many reasons) and also declares the accessor function
249// wxGetApp() which will return the reference of the right type (i.e. MyApp and
250// not wxApp)
251IMPLEMENT_APP(MyApp)
252
253// ============================================================================
254// implementation
255// ============================================================================
256
257// ----------------------------------------------------------------------------
258// the application class
259// ----------------------------------------------------------------------------
260
261// `Main program' equivalent: the program execution "starts" here
262bool MyApp::OnInit()
263{
264 // Create the main application window
f9cc9706
WS
265 MyFrame *frame = new MyFrame(_T("Calendar wxWidgets sample")
266#ifndef __WXWINCE__
267 ,wxPoint(50, 50), wxSize(450, 340)
268#endif
269 );
74a533f7 270
9230b621 271 frame->Show(true);
74a533f7
VZ
272
273 // success: wxApp::OnRun() will be called which will enter the main message
5014bb3a 274 // loop and the application will run. If we returned false here, the
74a533f7 275 // application would exit immediately.
9230b621 276 return true;
74a533f7
VZ
277}
278
279// ----------------------------------------------------------------------------
280// main frame
281// ----------------------------------------------------------------------------
282
283// frame constructor
284MyFrame::MyFrame(const wxString& title, const wxPoint& pos, const wxSize& size)
9230b621 285 : wxFrame((wxFrame *)NULL, wxID_ANY, title, pos, size)
74a533f7 286{
3200f37d 287 // set the frame icon
b58a81c4 288 SetIcon(wxIcon(sample_xpm));
3200f37d 289
74a533f7
VZ
290 // create a menu bar
291 wxMenu *menuFile = new wxMenu;
9f84eccd 292 menuFile->Append(Calendar_File_About, _T("&About...\tCtrl-A"), _T("Show about dialog"));
74a533f7 293 menuFile->AppendSeparator();
9f84eccd 294 menuFile->Append(Calendar_File_Quit, _T("E&xit\tAlt-X"), _T("Quit this program"));
74a533f7
VZ
295
296 wxMenu *menuCal = new wxMenu;
297 menuCal->Append(Calendar_Cal_Monday,
9f84eccd
MB
298 _T("Monday &first weekday\tCtrl-F"),
299 _T("Toggle between Mon and Sun as the first week day"),
9230b621 300 true);
9f84eccd
MB
301 menuCal->Append(Calendar_Cal_Holidays, _T("Show &holidays\tCtrl-H"),
302 _T("Toggle highlighting the holidays"),
9230b621 303 true);
9f84eccd
MB
304 menuCal->Append(Calendar_Cal_Special, _T("Highlight &special dates\tCtrl-S"),
305 _T("Test custom highlighting"),
9230b621 306 true);
37df1f33 307 menuCal->Append(Calendar_Cal_SurroundWeeks,
9f84eccd
MB
308 _T("Show s&urrounding weeks\tCtrl-W"),
309 _T("Show the neighbouring weeks in the prev/next month"),
9230b621 310 true);
bc385ba9 311 menuCal->AppendSeparator();
37df1f33 312 menuCal->Append(Calendar_Cal_SeqMonth,
9f84eccd
MB
313 _T("To&ggle month selector style\tCtrl-G"),
314 _T("Use another style for the calendar controls"),
9230b621 315 true);
9f84eccd
MB
316 menuCal->Append(Calendar_Cal_Month, _T("&Month can be changed\tCtrl-M"),
317 _T("Allow changing the month in the calendar"),
9230b621 318 true);
9f84eccd
MB
319 menuCal->Append(Calendar_Cal_Year, _T("&Year can be changed\tCtrl-Y"),
320 _T("Allow changing the year in the calendar"),
9230b621 321 true);
605dfd91 322 menuCal->AppendSeparator();
404e855d
VZ
323 menuCal->Append(Calendar_Cal_SetDate, _T("Call &SetDate(2005-12-24)"), _T("Set date to 2005-12-24."));
324 menuCal->Append(Calendar_Cal_Today, _T("Call &Today()"), _T("Set the current date."));
74a533f7 325
d1fd3c26
VZ
326#if wxUSE_DATEPICKCTRL
327 wxMenu *menuDate = new wxMenu;
328 menuDate->AppendCheckItem(Calendar_DatePicker_ShowCentury,
329 _T("Al&ways show century"));
330 menuDate->AppendCheckItem(Calendar_DatePicker_DropDown,
331 _T("Use &drop down control"));
3200f37d
VZ
332 menuDate->AppendCheckItem(Calendar_DatePicker_AllowNone,
333 _T("Allow &no date"));
404e855d
VZ
334 menuDate->AppendCheckItem(Calendar_DatePicker_StartWithNone,
335 _T("Start &with no date"));
7ff2dfba
VZ
336#if wxUSE_DATEPICKCTRL_GENERIC
337 menuDate->AppendCheckItem(Calendar_DatePicker_Generic,
338 _T("Use &generic version of the control"));
339#endif // wxUSE_DATEPICKCTRL_GENERIC
d1fd3c26
VZ
340 menuDate->AppendSeparator();
341 menuDate->Append(Calendar_DatePicker_AskDate, _T("&Choose date...\tCtrl-D"), _T("Show dialog with wxDatePickerCtrl"));
342#endif // wxUSE_DATEPICKCTRL
343
74a533f7
VZ
344 // now append the freshly created menu to the menu bar...
345 wxMenuBar *menuBar = new wxMenuBar;
9f84eccd
MB
346 menuBar->Append(menuFile, _T("&File"));
347 menuBar->Append(menuCal, _T("&Calendar"));
d1fd3c26
VZ
348#if wxUSE_DATEPICKCTRL
349 menuBar->Append(menuDate, _T("&Date picker"));
350#endif // wxUSE_DATEPICKCTRL
74a533f7 351
9230b621
VS
352 menuBar->Check(Calendar_Cal_Monday, true);
353 menuBar->Check(Calendar_Cal_Holidays, true);
354 menuBar->Check(Calendar_Cal_Month, true);
355 menuBar->Check(Calendar_Cal_Year, true);
74a533f7 356
d1fd3c26
VZ
357#if wxUSE_DATEPICKCTRL
358 menuBar->Check(Calendar_DatePicker_ShowCentury, true);
359#endif // wxUSE_DATEPICKCTRL
360
74a533f7
VZ
361 // ... and attach this menu bar to the frame
362 SetMenuBar(menuBar);
363
364 m_panel = new MyPanel(this);
365
366#if wxUSE_STATUSBAR
367 // create a status bar just for fun (by default with 1 pane only)
9898fcda 368 CreateStatusBar(2);
be5a51fb 369 SetStatusText(_T("Welcome to wxWidgets!"));
74a533f7
VZ
370#endif // wxUSE_STATUSBAR
371}
372
373void MyFrame::OnQuit(wxCommandEvent& WXUNUSED(event))
374{
5014bb3a 375 // true is to force the frame to close
9230b621 376 Close(true);
74a533f7
VZ
377}
378
379void MyFrame::OnAbout(wxCommandEvent& WXUNUSED(event))
380{
749bfe9a 381 wxMessageBox(_T("wxCalendarCtrl sample\n(c) 2000 Vadim Zeitlin"),
74a533f7
VZ
382 _T("About Calendar"), wxOK | wxICON_INFORMATION, this);
383}
384
385void MyFrame::OnCalMonday(wxCommandEvent& event)
386{
37df1f33
VZ
387 bool enable = GetMenuBar()->IsChecked(event.GetId());
388
389 m_panel->ToggleCalStyle(enable, wxCAL_MONDAY_FIRST);
74a533f7
VZ
390}
391
392void MyFrame::OnCalHolidays(wxCommandEvent& event)
393{
89a0a322 394 bool enable = GetMenuBar()->IsChecked(event.GetId());
37df1f33 395
bc385ba9 396 m_panel->GetCal()->EnableHolidayDisplay(enable);
74a533f7
VZ
397}
398
399void MyFrame::OnCalSpecial(wxCommandEvent& event)
400{
89a0a322 401 m_panel->HighlightSpecial(GetMenuBar()->IsChecked(event.GetId()));
bc385ba9
VZ
402}
403
404void MyFrame::OnCalAllowMonth(wxCommandEvent& event)
405{
89a0a322 406 bool allow = GetMenuBar()->IsChecked(event.GetId());
bc385ba9
VZ
407
408 m_panel->GetCal()->EnableMonthChange(allow);
409}
410
411void MyFrame::OnCalAllowYear(wxCommandEvent& event)
412{
89a0a322 413 bool allow = GetMenuBar()->IsChecked(event.GetId());
bc385ba9
VZ
414
415 m_panel->GetCal()->EnableYearChange(allow);
416}
417
37df1f33
VZ
418void MyFrame::OnCalSeqMonth(wxCommandEvent& event)
419{
420 bool allow = GetMenuBar()->IsChecked(event.GetId());
421
422 m_panel->ToggleCalStyle(allow, wxCAL_SEQUENTIAL_MONTH_SELECTION);
423}
424
425void MyFrame::OnCalShowSurroundingWeeks(wxCommandEvent& event)
426{
427 bool allow = GetMenuBar()->IsChecked(event.GetId());
428
429 m_panel->ToggleCalStyle(allow, wxCAL_SHOW_SURROUNDING_WEEKS);
430}
431
bc385ba9
VZ
432void MyFrame::OnAllowYearUpdate(wxUpdateUIEvent& event)
433{
434 event.Enable( GetMenuBar()->IsChecked(Calendar_Cal_Month));
74a533f7
VZ
435}
436
87728739 437void MyFrame::OnSetDate(wxCommandEvent &WXUNUSED(event))
605dfd91
JS
438{
439 m_panel->SetDate();
440}
441
87728739 442void MyFrame::OnToday(wxCommandEvent &WXUNUSED(event))
605dfd91
JS
443{
444 m_panel->Today();
445}
446
ac78ab7b 447#if wxUSE_DATEPICKCTRL
feb72429 448
404e855d
VZ
449void MyFrame::OnUpdateUIStartWithNone(wxUpdateUIEvent& event)
450{
451 // it only makes sense to start with invalid date if we can have no date
452 event.Enable( GetMenuBar()->IsChecked(Calendar_DatePicker_AllowNone) );
453}
454
feb72429
VZ
455void MyFrame::OnAskDate(wxCommandEvent& WXUNUSED(event))
456{
404e855d
VZ
457 wxDateTime dt = m_panel->GetCal()->GetDate();
458
d1fd3c26
VZ
459 int style = wxDP_DEFAULT;
460 if ( GetMenuBar()->IsChecked(Calendar_DatePicker_ShowCentury) )
461 style |= wxDP_SHOWCENTURY;
462 if ( GetMenuBar()->IsChecked(Calendar_DatePicker_DropDown) )
463 style |= wxDP_DROPDOWN;
3200f37d 464 if ( GetMenuBar()->IsChecked(Calendar_DatePicker_AllowNone) )
404e855d 465 {
3200f37d 466 style |= wxDP_ALLOWNONE;
d1fd3c26 467
404e855d
VZ
468 if ( GetMenuBar()->IsChecked(Calendar_DatePicker_StartWithNone) )
469 dt = wxDefaultDateTime;
470 }
471
472 MyDialog dlg(this, dt, style);
feb72429
VZ
473 if ( dlg.ShowModal() == wxID_OK )
474 {
404e855d 475 dt = dlg.GetDate();
3200f37d 476 if ( dt.IsValid() )
feb72429 477 {
3200f37d 478 const wxDateTime today = wxDateTime::Today();
feb72429 479
3200f37d
VZ
480 if ( dt.GetDay() == today.GetDay() &&
481 dt.GetMonth() == today.GetMonth() )
482 {
483 wxMessageBox(_T("Happy birthday!"), _T("Calendar Sample"));
484 }
feb72429 485
3200f37d
VZ
486 m_panel->GetCal()->SetDate(dt);
487
488 wxLogStatus(_T("Changed the date to your input"));
489 }
490 else
491 {
492 wxLogStatus(_T("No date entered"));
493 }
feb72429
VZ
494 }
495}
496
ac78ab7b 497#endif // wxUSE_DATEPICKCTRL
feb72429 498
74a533f7
VZ
499// ----------------------------------------------------------------------------
500// MyPanel
501// ----------------------------------------------------------------------------
502
503MyPanel::MyPanel(wxFrame *frame)
9230b621 504 : wxPanel(frame, wxID_ANY)
74a533f7 505{
4e6bceff 506 wxString date;
4693b20c 507 date.Printf(wxT("Selected date: %s"),
74a533f7 508 wxDateTime::Today().FormatISODate().c_str());
9230b621 509 m_date = new wxStaticText(this, wxID_ANY, date);
74a533f7
VZ
510 m_calendar = new wxCalendarCtrl(this, Calendar_CalCtrl,
511 wxDefaultDateTime,
512 wxDefaultPosition,
513 wxDefaultSize,
bc385ba9
VZ
514 wxCAL_MONDAY_FIRST |
515 wxCAL_SHOW_HOLIDAYS |
516 wxRAISED_BORDER);
74a533f7 517
f9cc9706
WS
518 // adjust to vertical/horizontal display, check mostly dedicated to WinCE
519 bool horizontal = ( wxSystemSettings::GetMetric(wxSYS_SCREEN_X) > wxSystemSettings::GetMetric(wxSYS_SCREEN_Y) );
2522d529 520 m_sizer = new wxBoxSizer( horizontal ? wxHORIZONTAL : wxVERTICAL );
4e6bceff 521
9230b621
VS
522 m_sizer->Add(m_date, 0, wxALIGN_CENTER | wxALL, 10 );
523 m_sizer->Add(m_calendar, 0, wxALIGN_CENTER | wxALIGN_LEFT);
4e6bceff 524
9230b621
VS
525 SetSizer( m_sizer );
526 m_sizer->SetSizeHints( this );
74a533f7
VZ
527}
528
529void MyPanel::OnCalendar(wxCalendarEvent& event)
530{
4693b20c 531 wxLogMessage(wxT("Selected %s from calendar"),
74a533f7
VZ
532 event.GetDate().FormatISODate().c_str());
533}
534
535void MyPanel::OnCalendarChange(wxCalendarEvent& event)
536{
537 wxString s;
4693b20c 538 s.Printf(wxT("Selected date: %s"), event.GetDate().FormatISODate().c_str());
74a533f7
VZ
539
540 m_date->SetLabel(s);
541}
542
0de868d9
VZ
543void MyPanel::OnCalMonthChange(wxCalendarEvent& WXUNUSED(event))
544{
4693b20c 545 wxLogStatus(wxT("Calendar month changed"));
0de868d9
VZ
546}
547
548void MyPanel::OnCalYearChange(wxCalendarEvent& WXUNUSED(event))
549{
4693b20c 550 wxLogStatus(wxT("Calendar year changed"));
0de868d9
VZ
551}
552
74a533f7
VZ
553void MyPanel::OnCalendarWeekDayClick(wxCalendarEvent& event)
554{
4693b20c 555 wxLogMessage(wxT("Clicked on %s"),
74a533f7
VZ
556 wxDateTime::GetWeekDayName(event.GetWeekDay()).c_str());
557}
558
37df1f33 559void MyPanel::ToggleCalStyle(bool on, int flag)
74a533f7
VZ
560{
561 long style = m_calendar->GetWindowStyle();
562 if ( on )
37df1f33 563 style |= flag;
74a533f7 564 else
37df1f33 565 style &= ~flag;
74a533f7 566
2522d529
VZ
567 if ( flag == wxCAL_SEQUENTIAL_MONTH_SELECTION )
568 {
569 // changing this style requires recreating the control
570 wxCalendarCtrl *calendar = new wxCalendarCtrl(this, Calendar_CalCtrl,
571 m_calendar->GetDate(),
572 wxDefaultPosition,
573 wxDefaultSize,
574 style);
575 m_sizer->Replace(m_calendar, calendar);
576 delete m_calendar;
577 m_calendar = calendar;
578
579 m_sizer->Layout();
580 }
581 else // just changing the style is enough
582 {
583 m_calendar->SetWindowStyle(style);
74a533f7 584
2522d529
VZ
585 m_calendar->Refresh();
586 }
74a533f7
VZ
587}
588
74a533f7
VZ
589void MyPanel::HighlightSpecial(bool on)
590{
591 if ( on )
592 {
593 wxCalendarDateAttr
594 *attrRedCircle = new wxCalendarDateAttr(wxCAL_BORDER_ROUND, *wxRED),
595 *attrGreenSquare = new wxCalendarDateAttr(wxCAL_BORDER_SQUARE, *wxGREEN),
596 *attrHeaderLike = new wxCalendarDateAttr(*wxBLUE, *wxLIGHT_GREY);
597
598 m_calendar->SetAttr(17, attrRedCircle);
599 m_calendar->SetAttr(29, attrGreenSquare);
600 m_calendar->SetAttr(13, attrHeaderLike);
601 }
602 else // off
603 {
604 m_calendar->ResetAttr(17);
605 m_calendar->ResetAttr(29);
606 m_calendar->ResetAttr(13);
607 }
608
609 m_calendar->Refresh();
610}
605dfd91
JS
611
612void MyPanel::SetDate()
613{
614 wxDateTime date(24, wxDateTime::Dec, 2005, 23, 59, 59);
615 m_calendar->SetDate(date);
616}
617
618void MyPanel::Today()
619{
620 m_calendar->SetDate(wxDateTime::Today());
621}
feb72429
VZ
622
623// ----------------------------------------------------------------------------
624// MyDialog
625// ----------------------------------------------------------------------------
626
ac78ab7b 627#if wxUSE_DATEPICKCTRL
feb72429 628
d1fd3c26 629MyDialog::MyDialog(wxWindow *parent, const wxDateTime& dt, int dtpStyle)
a7c58211 630 : wxDialog(parent, wxID_ANY, wxString(_T("Calendar: Choose a date")))
feb72429
VZ
631{
632 wxStdDialogButtonSizer *sizerBtns = new wxStdDialogButtonSizer;
633 sizerBtns->AddButton(new wxButton(this, wxID_OK));
634 sizerBtns->AddButton(new wxButton(this, wxID_CANCEL));
718903fe 635 sizerBtns->Realize();
feb72429
VZ
636
637 wxSizer *sizerText = new wxBoxSizer(wxHORIZONTAL);
a7c58211 638 sizerText->Add(new wxStaticText(this, wxID_ANY, _T("Date in ISO format: ")),
b7e542be 639 wxSizerFlags().Border().Align(wxALIGN_CENTRE_VERTICAL));
a7c58211 640 m_text = new wxTextCtrl(this, wxID_ANY);
b7e542be
VZ
641 sizerText->Add(m_text, wxSizerFlags().
642 Expand().Border().Align(wxALIGN_CENTRE_VERTICAL));
feb72429
VZ
643
644 wxSizer *sizerTop = new wxBoxSizer(wxVERTICAL);
645 sizerTop->Add(new wxStaticText
646 (
a7c58211 647 this, wxID_ANY,
feb72429
VZ
648 _T("Enter your birthday date (not before 20th century):")
649 ),
650 wxSizerFlags().Border());
651
7ff2dfba
VZ
652#if wxUSE_DATEPICKCTRL_GENERIC
653 wxFrame *frame = (wxFrame *)wxGetTopLevelParent(parent);
654 if ( frame && frame->GetMenuBar()->IsChecked(Calendar_DatePicker_Generic) )
a7c58211 655 m_datePicker = new wxDatePickerCtrlGeneric(this, wxID_ANY, dt,
7ff2dfba
VZ
656 wxDefaultPosition,
657 wxDefaultSize,
658 dtpStyle);
659 else
660#endif // wxUSE_DATEPICKCTRL_GENERIC
a7c58211 661 m_datePicker = new wxDatePickerCtrl(this, wxID_ANY, dt,
d1fd3c26
VZ
662 wxDefaultPosition, wxDefaultSize,
663 dtpStyle);
feb72429
VZ
664 m_datePicker->SetRange(wxDateTime(1, wxDateTime::Jan, 1900),
665 wxDefaultDateTime);
666 sizerTop->Add(m_datePicker, wxSizerFlags().Expand().Border());
667
668 sizerTop->AddStretchSpacer(1);
669 sizerTop->Add(sizerText);
670
671 sizerTop->Add(sizerBtns, wxSizerFlags().Centre().Border());
672
673 SetSizerAndFit(sizerTop);
674 Layout();
675}
676
677void MyDialog::OnDateChange(wxDateEvent& event)
678{
b7e542be 679 const wxDateTime dt = event.GetDate();
404e855d 680 if ( dt.IsValid() )
a7c58211
WS
681 m_text->SetValue(dt.FormatISODate());
682 else
683 m_text->SetValue(wxEmptyString);
feb72429
VZ
684}
685
ac78ab7b 686#endif // wxUSE_DATEPICKCTRL