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