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