]>
Commit | Line | Data |
---|---|---|
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 | |
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); | |
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 |
93 | private: |
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 | |
102 | class MyFrame : public wxFrame | |
103 | { | |
104 | public: | |
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 |
135 | private: |
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 | |
145 | class MyDialog : public wxDialog | |
146 | { | |
147 | public: | |
d1fd3c26 | 148 | MyDialog(wxWindow *parent, const wxDateTime& dt, int dtpStyle); |
feb72429 VZ |
149 | |
150 | wxDateTime GetDate() const { return m_datePicker->GetValue(); } | |
151 | ||
152 | private: | |
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 | |
170 | enum | |
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. | |
205 | BEGIN_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 |
233 | END_EVENT_TABLE() |
234 | ||
235 | BEGIN_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) | |
241 | END_EVENT_TABLE() | |
242 | ||
ac78ab7b | 243 | #if wxUSE_DATEPICKCTRL |
feb72429 VZ |
244 | |
245 | BEGIN_EVENT_TABLE(MyDialog, wxDialog) | |
246 | EVT_DATE_CHANGED(wxID_ANY, MyDialog::OnDateChange) | |
247 | END_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) | |
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 | |
f9cc9706 WS |
270 | MyFrame *frame = new MyFrame(_T("Calendar wxWidgets sample") |
271 | #ifndef __WXWINCE__ | |
272 | ,wxPoint(50, 50), wxSize(450, 340) | |
273 | #endif | |
274 | ); | |
74a533f7 | 275 | |
9230b621 | 276 | frame->Show(true); |
74a533f7 VZ |
277 | |
278 | // success: wxApp::OnRun() will be called which will enter the main message | |
5014bb3a | 279 | // loop and the application will run. If we returned false here, the |
74a533f7 | 280 | // application would exit immediately. |
9230b621 | 281 | return true; |
74a533f7 VZ |
282 | } |
283 | ||
284 | // ---------------------------------------------------------------------------- | |
285 | // main frame | |
286 | // ---------------------------------------------------------------------------- | |
287 | ||
288 | // frame constructor | |
289 | MyFrame::MyFrame(const wxString& title, const wxPoint& pos, const wxSize& size) | |
9230b621 | 290 | : wxFrame((wxFrame *)NULL, wxID_ANY, title, pos, size) |
74a533f7 | 291 | { |
3200f37d | 292 | // set the frame icon |
b58a81c4 | 293 | SetIcon(wxIcon(sample_xpm)); |
3200f37d | 294 | |
74a533f7 VZ |
295 | // create a menu bar |
296 | wxMenu *menuFile = new wxMenu; | |
9f84eccd | 297 | menuFile->Append(Calendar_File_About, _T("&About...\tCtrl-A"), _T("Show about dialog")); |
74a533f7 | 298 | menuFile->AppendSeparator(); |
9f84eccd | 299 | menuFile->Append(Calendar_File_Quit, _T("E&xit\tAlt-X"), _T("Quit this program")); |
74a533f7 VZ |
300 | |
301 | wxMenu *menuCal = new wxMenu; | |
302 | menuCal->Append(Calendar_Cal_Monday, | |
9f84eccd MB |
303 | _T("Monday &first weekday\tCtrl-F"), |
304 | _T("Toggle between Mon and Sun as the first week day"), | |
9230b621 | 305 | true); |
9f84eccd MB |
306 | menuCal->Append(Calendar_Cal_Holidays, _T("Show &holidays\tCtrl-H"), |
307 | _T("Toggle highlighting the holidays"), | |
9230b621 | 308 | true); |
9f84eccd MB |
309 | menuCal->Append(Calendar_Cal_Special, _T("Highlight &special dates\tCtrl-S"), |
310 | _T("Test custom highlighting"), | |
9230b621 | 311 | true); |
37df1f33 | 312 | menuCal->Append(Calendar_Cal_SurroundWeeks, |
9f84eccd MB |
313 | _T("Show s&urrounding weeks\tCtrl-W"), |
314 | _T("Show the neighbouring weeks in the prev/next month"), | |
9230b621 | 315 | true); |
bc385ba9 | 316 | menuCal->AppendSeparator(); |
37df1f33 | 317 | menuCal->Append(Calendar_Cal_SeqMonth, |
9f84eccd MB |
318 | _T("To&ggle month selector style\tCtrl-G"), |
319 | _T("Use another style for the calendar controls"), | |
9230b621 | 320 | true); |
9f84eccd MB |
321 | menuCal->Append(Calendar_Cal_Month, _T("&Month can be changed\tCtrl-M"), |
322 | _T("Allow changing the month in the calendar"), | |
9230b621 | 323 | true); |
9f84eccd MB |
324 | menuCal->Append(Calendar_Cal_Year, _T("&Year can be changed\tCtrl-Y"), |
325 | _T("Allow changing the year in the calendar"), | |
9230b621 | 326 | true); |
605dfd91 | 327 | menuCal->AppendSeparator(); |
404e855d VZ |
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.")); | |
5f324bb6 VZ |
330 | menuCal->AppendSeparator(); |
331 | menuCal->AppendCheckItem(Calendar_Cal_Resizable, _T("Make &resizable\tCtrl-R")); | |
74a533f7 | 332 | |
d1fd3c26 VZ |
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")); | |
3200f37d VZ |
339 | menuDate->AppendCheckItem(Calendar_DatePicker_AllowNone, |
340 | _T("Allow &no date")); | |
404e855d VZ |
341 | menuDate->AppendCheckItem(Calendar_DatePicker_StartWithNone, |
342 | _T("Start &with no date")); | |
7ff2dfba VZ |
343 | #if wxUSE_DATEPICKCTRL_GENERIC |
344 | menuDate->AppendCheckItem(Calendar_DatePicker_Generic, | |
345 | _T("Use &generic version of the control")); | |
346 | #endif // wxUSE_DATEPICKCTRL_GENERIC | |
d1fd3c26 VZ |
347 | menuDate->AppendSeparator(); |
348 | menuDate->Append(Calendar_DatePicker_AskDate, _T("&Choose date...\tCtrl-D"), _T("Show dialog with wxDatePickerCtrl")); | |
349 | #endif // wxUSE_DATEPICKCTRL | |
350 | ||
74a533f7 VZ |
351 | // now append the freshly created menu to the menu bar... |
352 | wxMenuBar *menuBar = new wxMenuBar; | |
9f84eccd MB |
353 | menuBar->Append(menuFile, _T("&File")); |
354 | menuBar->Append(menuCal, _T("&Calendar")); | |
d1fd3c26 VZ |
355 | #if wxUSE_DATEPICKCTRL |
356 | menuBar->Append(menuDate, _T("&Date picker")); | |
357 | #endif // wxUSE_DATEPICKCTRL | |
74a533f7 | 358 | |
9230b621 VS |
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); | |
74a533f7 | 363 | |
d1fd3c26 VZ |
364 | #if wxUSE_DATEPICKCTRL |
365 | menuBar->Check(Calendar_DatePicker_ShowCentury, true); | |
366 | #endif // wxUSE_DATEPICKCTRL | |
367 | ||
74a533f7 VZ |
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) | |
9898fcda | 375 | CreateStatusBar(2); |
be5a51fb | 376 | SetStatusText(_T("Welcome to wxWidgets!")); |
74a533f7 VZ |
377 | #endif // wxUSE_STATUSBAR |
378 | } | |
379 | ||
380 | void MyFrame::OnQuit(wxCommandEvent& WXUNUSED(event)) | |
381 | { | |
5014bb3a | 382 | // true is to force the frame to close |
9230b621 | 383 | Close(true); |
74a533f7 VZ |
384 | } |
385 | ||
386 | void MyFrame::OnAbout(wxCommandEvent& WXUNUSED(event)) | |
387 | { | |
749bfe9a | 388 | wxMessageBox(_T("wxCalendarCtrl sample\n(c) 2000 Vadim Zeitlin"), |
74a533f7 VZ |
389 | _T("About Calendar"), wxOK | wxICON_INFORMATION, this); |
390 | } | |
391 | ||
392 | void MyFrame::OnCalMonday(wxCommandEvent& event) | |
393 | { | |
37df1f33 VZ |
394 | bool enable = GetMenuBar()->IsChecked(event.GetId()); |
395 | ||
396 | m_panel->ToggleCalStyle(enable, wxCAL_MONDAY_FIRST); | |
74a533f7 VZ |
397 | } |
398 | ||
399 | void MyFrame::OnCalHolidays(wxCommandEvent& event) | |
400 | { | |
89a0a322 | 401 | bool enable = GetMenuBar()->IsChecked(event.GetId()); |
37df1f33 | 402 | |
bc385ba9 | 403 | m_panel->GetCal()->EnableHolidayDisplay(enable); |
74a533f7 VZ |
404 | } |
405 | ||
406 | void MyFrame::OnCalSpecial(wxCommandEvent& event) | |
407 | { | |
89a0a322 | 408 | m_panel->HighlightSpecial(GetMenuBar()->IsChecked(event.GetId())); |
bc385ba9 VZ |
409 | } |
410 | ||
411 | void MyFrame::OnCalAllowMonth(wxCommandEvent& event) | |
412 | { | |
89a0a322 | 413 | bool allow = GetMenuBar()->IsChecked(event.GetId()); |
bc385ba9 VZ |
414 | |
415 | m_panel->GetCal()->EnableMonthChange(allow); | |
416 | } | |
417 | ||
418 | void MyFrame::OnCalAllowYear(wxCommandEvent& event) | |
419 | { | |
89a0a322 | 420 | bool allow = GetMenuBar()->IsChecked(event.GetId()); |
bc385ba9 VZ |
421 | |
422 | m_panel->GetCal()->EnableYearChange(allow); | |
423 | } | |
424 | ||
37df1f33 VZ |
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 | ||
bc385ba9 VZ |
439 | void MyFrame::OnAllowYearUpdate(wxUpdateUIEvent& event) |
440 | { | |
441 | event.Enable( GetMenuBar()->IsChecked(Calendar_Cal_Month)); | |
74a533f7 VZ |
442 | } |
443 | ||
87728739 | 444 | void MyFrame::OnSetDate(wxCommandEvent &WXUNUSED(event)) |
605dfd91 JS |
445 | { |
446 | m_panel->SetDate(); | |
447 | } | |
448 | ||
87728739 | 449 | void MyFrame::OnToday(wxCommandEvent &WXUNUSED(event)) |
605dfd91 JS |
450 | { |
451 | m_panel->Today(); | |
452 | } | |
453 | ||
5f324bb6 VZ |
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 | ||
ac78ab7b | 472 | #if wxUSE_DATEPICKCTRL |
feb72429 | 473 | |
404e855d VZ |
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 | ||
feb72429 VZ |
480 | void MyFrame::OnAskDate(wxCommandEvent& WXUNUSED(event)) |
481 | { | |
404e855d VZ |
482 | wxDateTime dt = m_panel->GetCal()->GetDate(); |
483 | ||
d1fd3c26 VZ |
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; | |
3200f37d | 489 | if ( GetMenuBar()->IsChecked(Calendar_DatePicker_AllowNone) ) |
404e855d | 490 | { |
3200f37d | 491 | style |= wxDP_ALLOWNONE; |
d1fd3c26 | 492 | |
404e855d VZ |
493 | if ( GetMenuBar()->IsChecked(Calendar_DatePicker_StartWithNone) ) |
494 | dt = wxDefaultDateTime; | |
495 | } | |
496 | ||
497 | MyDialog dlg(this, dt, style); | |
feb72429 VZ |
498 | if ( dlg.ShowModal() == wxID_OK ) |
499 | { | |
404e855d | 500 | dt = dlg.GetDate(); |
3200f37d | 501 | if ( dt.IsValid() ) |
feb72429 | 502 | { |
3200f37d | 503 | const wxDateTime today = wxDateTime::Today(); |
feb72429 | 504 | |
3200f37d VZ |
505 | if ( dt.GetDay() == today.GetDay() && |
506 | dt.GetMonth() == today.GetMonth() ) | |
507 | { | |
508 | wxMessageBox(_T("Happy birthday!"), _T("Calendar Sample")); | |
509 | } | |
feb72429 | 510 | |
3200f37d VZ |
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 | } | |
feb72429 VZ |
519 | } |
520 | } | |
521 | ||
ac78ab7b | 522 | #endif // wxUSE_DATEPICKCTRL |
feb72429 | 523 | |
74a533f7 VZ |
524 | // ---------------------------------------------------------------------------- |
525 | // MyPanel | |
526 | // ---------------------------------------------------------------------------- | |
527 | ||
528 | MyPanel::MyPanel(wxFrame *frame) | |
9230b621 | 529 | : wxPanel(frame, wxID_ANY) |
74a533f7 | 530 | { |
4e6bceff | 531 | wxString date; |
4693b20c | 532 | date.Printf(wxT("Selected date: %s"), |
74a533f7 | 533 | wxDateTime::Today().FormatISODate().c_str()); |
9230b621 | 534 | m_date = new wxStaticText(this, wxID_ANY, date); |
74a533f7 VZ |
535 | m_calendar = new wxCalendarCtrl(this, Calendar_CalCtrl, |
536 | wxDefaultDateTime, | |
537 | wxDefaultPosition, | |
538 | wxDefaultSize, | |
bc385ba9 VZ |
539 | wxCAL_MONDAY_FIRST | |
540 | wxCAL_SHOW_HOLIDAYS | | |
541 | wxRAISED_BORDER); | |
74a533f7 | 542 | |
f9cc9706 WS |
543 | // adjust to vertical/horizontal display, check mostly dedicated to WinCE |
544 | bool horizontal = ( wxSystemSettings::GetMetric(wxSYS_SCREEN_X) > wxSystemSettings::GetMetric(wxSYS_SCREEN_Y) ); | |
2522d529 | 545 | m_sizer = new wxBoxSizer( horizontal ? wxHORIZONTAL : wxVERTICAL ); |
4e6bceff | 546 | |
9230b621 VS |
547 | m_sizer->Add(m_date, 0, wxALIGN_CENTER | wxALL, 10 ); |
548 | m_sizer->Add(m_calendar, 0, wxALIGN_CENTER | wxALIGN_LEFT); | |
4e6bceff | 549 | |
9230b621 VS |
550 | SetSizer( m_sizer ); |
551 | m_sizer->SetSizeHints( this ); | |
74a533f7 VZ |
552 | } |
553 | ||
554 | void MyPanel::OnCalendar(wxCalendarEvent& event) | |
555 | { | |
4693b20c | 556 | wxLogMessage(wxT("Selected %s from calendar"), |
74a533f7 VZ |
557 | event.GetDate().FormatISODate().c_str()); |
558 | } | |
559 | ||
560 | void MyPanel::OnCalendarChange(wxCalendarEvent& event) | |
561 | { | |
562 | wxString s; | |
4693b20c | 563 | s.Printf(wxT("Selected date: %s"), event.GetDate().FormatISODate().c_str()); |
74a533f7 VZ |
564 | |
565 | m_date->SetLabel(s); | |
566 | } | |
567 | ||
0de868d9 VZ |
568 | void MyPanel::OnCalMonthChange(wxCalendarEvent& WXUNUSED(event)) |
569 | { | |
4693b20c | 570 | wxLogStatus(wxT("Calendar month changed")); |
0de868d9 VZ |
571 | } |
572 | ||
573 | void MyPanel::OnCalYearChange(wxCalendarEvent& WXUNUSED(event)) | |
574 | { | |
4693b20c | 575 | wxLogStatus(wxT("Calendar year changed")); |
0de868d9 VZ |
576 | } |
577 | ||
74a533f7 VZ |
578 | void MyPanel::OnCalendarWeekDayClick(wxCalendarEvent& event) |
579 | { | |
4693b20c | 580 | wxLogMessage(wxT("Clicked on %s"), |
74a533f7 VZ |
581 | wxDateTime::GetWeekDayName(event.GetWeekDay()).c_str()); |
582 | } | |
583 | ||
37df1f33 | 584 | void MyPanel::ToggleCalStyle(bool on, int flag) |
74a533f7 VZ |
585 | { |
586 | long style = m_calendar->GetWindowStyle(); | |
587 | if ( on ) | |
37df1f33 | 588 | style |= flag; |
74a533f7 | 589 | else |
37df1f33 | 590 | style &= ~flag; |
74a533f7 | 591 | |
2522d529 VZ |
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); | |
74a533f7 | 609 | |
2522d529 VZ |
610 | m_calendar->Refresh(); |
611 | } | |
74a533f7 VZ |
612 | } |
613 | ||
74a533f7 VZ |
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 | } | |
605dfd91 JS |
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 | } | |
feb72429 VZ |
647 | |
648 | // ---------------------------------------------------------------------------- | |
649 | // MyDialog | |
650 | // ---------------------------------------------------------------------------- | |
651 | ||
ac78ab7b | 652 | #if wxUSE_DATEPICKCTRL |
feb72429 | 653 | |
d1fd3c26 | 654 | MyDialog::MyDialog(wxWindow *parent, const wxDateTime& dt, int dtpStyle) |
a7c58211 | 655 | : wxDialog(parent, wxID_ANY, wxString(_T("Calendar: Choose a date"))) |
feb72429 VZ |
656 | { |
657 | wxStdDialogButtonSizer *sizerBtns = new wxStdDialogButtonSizer; | |
658 | sizerBtns->AddButton(new wxButton(this, wxID_OK)); | |
659 | sizerBtns->AddButton(new wxButton(this, wxID_CANCEL)); | |
718903fe | 660 | sizerBtns->Realize(); |
feb72429 VZ |
661 | |
662 | wxSizer *sizerText = new wxBoxSizer(wxHORIZONTAL); | |
a7c58211 | 663 | sizerText->Add(new wxStaticText(this, wxID_ANY, _T("Date in ISO format: ")), |
b7e542be | 664 | wxSizerFlags().Border().Align(wxALIGN_CENTRE_VERTICAL)); |
a7c58211 | 665 | m_text = new wxTextCtrl(this, wxID_ANY); |
b7e542be VZ |
666 | sizerText->Add(m_text, wxSizerFlags(). |
667 | Expand().Border().Align(wxALIGN_CENTRE_VERTICAL)); | |
feb72429 VZ |
668 | |
669 | wxSizer *sizerTop = new wxBoxSizer(wxVERTICAL); | |
670 | sizerTop->Add(new wxStaticText | |
671 | ( | |
a7c58211 | 672 | this, wxID_ANY, |
feb72429 VZ |
673 | _T("Enter your birthday date (not before 20th century):") |
674 | ), | |
675 | wxSizerFlags().Border()); | |
676 | ||
7ff2dfba VZ |
677 | #if wxUSE_DATEPICKCTRL_GENERIC |
678 | wxFrame *frame = (wxFrame *)wxGetTopLevelParent(parent); | |
679 | if ( frame && frame->GetMenuBar()->IsChecked(Calendar_DatePicker_Generic) ) | |
a7c58211 | 680 | m_datePicker = new wxDatePickerCtrlGeneric(this, wxID_ANY, dt, |
7ff2dfba VZ |
681 | wxDefaultPosition, |
682 | wxDefaultSize, | |
683 | dtpStyle); | |
684 | else | |
685 | #endif // wxUSE_DATEPICKCTRL_GENERIC | |
a7c58211 | 686 | m_datePicker = new wxDatePickerCtrl(this, wxID_ANY, dt, |
d1fd3c26 VZ |
687 | wxDefaultPosition, wxDefaultSize, |
688 | dtpStyle); | |
feb72429 VZ |
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 | { | |
b7e542be | 704 | const wxDateTime dt = event.GetDate(); |
404e855d | 705 | if ( dt.IsValid() ) |
a7c58211 WS |
706 | m_text->SetValue(dt.FormatISODate()); |
707 | else | |
708 | m_text->SetValue(wxEmptyString); | |
feb72429 VZ |
709 | } |
710 | ||
ac78ab7b | 711 | #endif // wxUSE_DATEPICKCTRL |