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