]> git.saurik.com Git - wxWidgets.git/blob - samples/calendar/calendar.cpp
new carbon implementation (unicode support)
[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 #if defined(__GNUG__) && !defined(__APPLE__)
21 #pragma implementation "calendar.cpp"
22 #pragma interface "calendar.cpp"
23 #endif
24
25 // For compilers that support precompilation, includes "wx/wx.h".
26 #include "wx/wxprec.h"
27
28 #ifdef __BORLANDC__
29 #pragma hdrstop
30 #endif
31
32 // for all others, include the necessary headers
33 #ifndef WX_PRECOMP
34 #include "wx/app.h"
35 #include "wx/log.h"
36 #include "wx/frame.h"
37 #include "wx/panel.h"
38 #include "wx/stattext.h"
39 #include "wx/menu.h"
40 #include "wx/layout.h"
41 #include "wx/msgdlg.h"
42 #endif
43
44 #include "wx/calctrl.h"
45
46 // ----------------------------------------------------------------------------
47 // private classes
48 // ----------------------------------------------------------------------------
49
50 // Define a new application type, each program should derive a class from wxApp
51 class MyApp : public wxApp
52 {
53 public:
54 // override base class virtuals
55 // ----------------------------
56
57 // this one is called on application startup and is a good place for the app
58 // initialization (doing it here and not in the ctor allows to have an error
59 // return: if OnInit() returns false, the application terminates)
60 virtual bool OnInit();
61 };
62
63 class MyPanel : public wxPanel
64 {
65 public:
66 MyPanel(wxFrame *frame);
67
68 void OnCalendar(wxCalendarEvent& event);
69 void OnCalendarWeekDayClick(wxCalendarEvent& event);
70 void OnCalendarChange(wxCalendarEvent& event);
71 void OnCalMonthChange(wxCalendarEvent& event);
72 void OnCalYearChange(wxCalendarEvent& event);
73
74 wxCalendarCtrl *GetCal() const { return m_calendar; }
75
76 // turn on/off the specified style bit on the calendar control
77 void ToggleCalStyle(bool on, int style);
78
79 void HighlightSpecial(bool on);
80
81 void SetDate();
82 void Today();
83
84 private:
85 wxCalendarCtrl *m_calendar;
86 wxStaticText *m_date;
87
88 DECLARE_EVENT_TABLE()
89 };
90
91 // Define a new frame type: this is going to be our main frame
92 class MyFrame : public wxFrame
93 {
94 public:
95 // ctor(s)
96 MyFrame(const wxString& title, const wxPoint& pos, const wxSize& size);
97
98 // event handlers (these functions should _not_ be virtual)
99 void OnQuit(wxCommandEvent& event);
100 void OnAbout(wxCommandEvent& event);
101
102 void OnCalMonday(wxCommandEvent& event);
103 void OnCalHolidays(wxCommandEvent& event);
104 void OnCalSpecial(wxCommandEvent& event);
105
106 void OnCalAllowMonth(wxCommandEvent& event);
107 void OnCalAllowYear(wxCommandEvent& event);
108
109 void OnCalSeqMonth(wxCommandEvent& event);
110 void OnCalShowSurroundingWeeks(wxCommandEvent& event);
111
112 void OnSetDate(wxCommandEvent& event);
113 void OnToday(wxCommandEvent& event);
114
115 void OnAllowYearUpdate(wxUpdateUIEvent& event);
116
117 private:
118 MyPanel *m_panel;
119
120 // any class wishing to process wxWindows events must use this macro
121 DECLARE_EVENT_TABLE()
122 };
123
124 // ----------------------------------------------------------------------------
125 // constants
126 // ----------------------------------------------------------------------------
127
128 // IDs for the controls and the menu commands
129 enum
130 {
131 // menu items
132 Calendar_File_About = 100,
133 Calendar_File_Quit,
134 Calendar_Cal_Monday = 200,
135 Calendar_Cal_Holidays,
136 Calendar_Cal_Special,
137 Calendar_Cal_Month,
138 Calendar_Cal_Year,
139 Calendar_Cal_SeqMonth,
140 Calendar_Cal_SurroundWeeks,
141 Calendar_Cal_SetDate,
142 Calendar_Cal_Today,
143 Calendar_CalCtrl = 1000
144 };
145
146 // ----------------------------------------------------------------------------
147 // event tables and other macros for wxWindows
148 // ----------------------------------------------------------------------------
149
150 // the event tables connect the wxWindows events with the functions (event
151 // handlers) which process them. It can be also done at run-time, but for the
152 // simple menu events like this the static method is much simpler.
153 BEGIN_EVENT_TABLE(MyFrame, wxFrame)
154 EVT_MENU(Calendar_File_Quit, MyFrame::OnQuit)
155 EVT_MENU(Calendar_File_About, MyFrame::OnAbout)
156
157 EVT_MENU(Calendar_Cal_Monday, MyFrame::OnCalMonday)
158 EVT_MENU(Calendar_Cal_Holidays, MyFrame::OnCalHolidays)
159 EVT_MENU(Calendar_Cal_Special, MyFrame::OnCalSpecial)
160
161 EVT_MENU(Calendar_Cal_Month, MyFrame::OnCalAllowMonth)
162 EVT_MENU(Calendar_Cal_Year, MyFrame::OnCalAllowYear)
163
164 EVT_MENU(Calendar_Cal_SeqMonth, MyFrame::OnCalSeqMonth)
165 EVT_MENU(Calendar_Cal_SurroundWeeks, MyFrame::OnCalShowSurroundingWeeks)
166
167 EVT_MENU(Calendar_Cal_SetDate, MyFrame::OnSetDate)
168 EVT_MENU(Calendar_Cal_Today, MyFrame::OnToday)
169
170
171 EVT_UPDATE_UI(Calendar_Cal_Year, MyFrame::OnAllowYearUpdate)
172 END_EVENT_TABLE()
173
174 BEGIN_EVENT_TABLE(MyPanel, wxPanel)
175 EVT_CALENDAR (Calendar_CalCtrl, MyPanel::OnCalendar)
176 EVT_CALENDAR_MONTH (Calendar_CalCtrl, MyPanel::OnCalMonthChange)
177 EVT_CALENDAR_YEAR (Calendar_CalCtrl, MyPanel::OnCalYearChange)
178 EVT_CALENDAR_SEL_CHANGED(Calendar_CalCtrl, MyPanel::OnCalendarChange)
179 EVT_CALENDAR_WEEKDAY_CLICKED(Calendar_CalCtrl, MyPanel::OnCalendarWeekDayClick)
180 END_EVENT_TABLE()
181
182 // Create a new application object: this macro will allow wxWindows to create
183 // the application object during program execution (it's better than using a
184 // static object for many reasons) and also declares the accessor function
185 // wxGetApp() which will return the reference of the right type (i.e. MyApp and
186 // not wxApp)
187 IMPLEMENT_APP(MyApp)
188
189 // ============================================================================
190 // implementation
191 // ============================================================================
192
193 // ----------------------------------------------------------------------------
194 // the application class
195 // ----------------------------------------------------------------------------
196
197 // `Main program' equivalent: the program execution "starts" here
198 bool MyApp::OnInit()
199 {
200 // Create the main application window
201 MyFrame *frame = new MyFrame(_T("Calendar wxWindows sample"),
202 wxPoint(50, 50), wxSize(450, 340));
203
204 frame->Show(TRUE);
205
206 // success: wxApp::OnRun() will be called which will enter the main message
207 // loop and the application will run. If we returned FALSE here, the
208 // application would exit immediately.
209 return TRUE;
210 }
211
212 // ----------------------------------------------------------------------------
213 // main frame
214 // ----------------------------------------------------------------------------
215
216 // frame constructor
217 MyFrame::MyFrame(const wxString& title, const wxPoint& pos, const wxSize& size)
218 : wxFrame((wxFrame *)NULL, -1, title, pos, size)
219 {
220 // create a menu bar
221 wxMenu *menuFile = new wxMenu;
222
223 menuFile->Append(Calendar_File_About, _T("&About...\tCtrl-A"), _T("Show about dialog"));
224 menuFile->AppendSeparator();
225 menuFile->Append(Calendar_File_Quit, _T("E&xit\tAlt-X"), _T("Quit this program"));
226
227 wxMenu *menuCal = new wxMenu;
228 menuCal->Append(Calendar_Cal_Monday,
229 _T("Monday &first weekday\tCtrl-F"),
230 _T("Toggle between Mon and Sun as the first week day"),
231 TRUE);
232 menuCal->Append(Calendar_Cal_Holidays, _T("Show &holidays\tCtrl-H"),
233 _T("Toggle highlighting the holidays"),
234 TRUE);
235 menuCal->Append(Calendar_Cal_Special, _T("Highlight &special dates\tCtrl-S"),
236 _T("Test custom highlighting"),
237 TRUE);
238 menuCal->Append(Calendar_Cal_SurroundWeeks,
239 _T("Show s&urrounding weeks\tCtrl-W"),
240 _T("Show the neighbouring weeks in the prev/next month"),
241 TRUE);
242 menuCal->AppendSeparator();
243 menuCal->Append(Calendar_Cal_SeqMonth,
244 _T("To&ggle month selector style\tCtrl-G"),
245 _T("Use another style for the calendar controls"),
246 TRUE);
247 menuCal->Append(Calendar_Cal_Month, _T("&Month can be changed\tCtrl-M"),
248 _T("Allow changing the month in the calendar"),
249 TRUE);
250 menuCal->Append(Calendar_Cal_Year, _T("&Year can be changed\tCtrl-Y"),
251 _T("Allow changing the year in the calendar"),
252 TRUE);
253 menuCal->AppendSeparator();
254 menuCal->Append(Calendar_Cal_SetDate, _T("SetDate()"), _T("Set date to 2005-12-24."));
255 menuCal->Append(Calendar_Cal_Today, _T("Today()"), _T("Set the current date."));
256
257 // now append the freshly created menu to the menu bar...
258 wxMenuBar *menuBar = new wxMenuBar;
259 menuBar->Append(menuFile, _T("&File"));
260 menuBar->Append(menuCal, _T("&Calendar"));
261
262 menuBar->Check(Calendar_Cal_Monday, TRUE);
263 menuBar->Check(Calendar_Cal_Holidays, TRUE);
264 menuBar->Check(Calendar_Cal_Month, TRUE);
265 menuBar->Check(Calendar_Cal_Year, TRUE);
266
267 // ... and attach this menu bar to the frame
268 SetMenuBar(menuBar);
269
270 m_panel = new MyPanel(this);
271
272 #if wxUSE_STATUSBAR
273 // create a status bar just for fun (by default with 1 pane only)
274 CreateStatusBar(2);
275 SetStatusText(_T("Welcome to wxWindows!"));
276 #endif // wxUSE_STATUSBAR
277 }
278
279 void MyFrame::OnQuit(wxCommandEvent& WXUNUSED(event))
280 {
281 // TRUE is to force the frame to close
282 Close(TRUE);
283 }
284
285 void MyFrame::OnAbout(wxCommandEvent& WXUNUSED(event))
286 {
287 wxMessageBox(_T("wxCalendarCtrl sample\n© 2000 Vadim Zeitlin"),
288 _T("About Calendar"), wxOK | wxICON_INFORMATION, this);
289 }
290
291 void MyFrame::OnCalMonday(wxCommandEvent& event)
292 {
293 bool enable = GetMenuBar()->IsChecked(event.GetId());
294
295 m_panel->ToggleCalStyle(enable, wxCAL_MONDAY_FIRST);
296 }
297
298 void MyFrame::OnCalHolidays(wxCommandEvent& event)
299 {
300 bool enable = GetMenuBar()->IsChecked(event.GetId());
301
302 m_panel->GetCal()->EnableHolidayDisplay(enable);
303 }
304
305 void MyFrame::OnCalSpecial(wxCommandEvent& event)
306 {
307 m_panel->HighlightSpecial(GetMenuBar()->IsChecked(event.GetId()));
308 }
309
310 void MyFrame::OnCalAllowMonth(wxCommandEvent& event)
311 {
312 bool allow = GetMenuBar()->IsChecked(event.GetId());
313
314 m_panel->GetCal()->EnableMonthChange(allow);
315 }
316
317 void MyFrame::OnCalAllowYear(wxCommandEvent& event)
318 {
319 bool allow = GetMenuBar()->IsChecked(event.GetId());
320
321 m_panel->GetCal()->EnableYearChange(allow);
322 }
323
324 void MyFrame::OnCalSeqMonth(wxCommandEvent& event)
325 {
326 bool allow = GetMenuBar()->IsChecked(event.GetId());
327
328 m_panel->ToggleCalStyle(allow, wxCAL_SEQUENTIAL_MONTH_SELECTION);
329 }
330
331 void MyFrame::OnCalShowSurroundingWeeks(wxCommandEvent& event)
332 {
333 bool allow = GetMenuBar()->IsChecked(event.GetId());
334
335 m_panel->ToggleCalStyle(allow, wxCAL_SHOW_SURROUNDING_WEEKS);
336 }
337
338 void MyFrame::OnAllowYearUpdate(wxUpdateUIEvent& event)
339 {
340 event.Enable( GetMenuBar()->IsChecked(Calendar_Cal_Month));
341 }
342
343 void MyFrame::OnSetDate(wxCommandEvent &WXUNUSED(event))
344 {
345 m_panel->SetDate();
346 }
347
348 void MyFrame::OnToday(wxCommandEvent &WXUNUSED(event))
349 {
350 m_panel->Today();
351 }
352
353 // ----------------------------------------------------------------------------
354 // MyPanel
355 // ----------------------------------------------------------------------------
356
357 MyPanel::MyPanel(wxFrame *frame)
358 : wxPanel(frame, -1)
359 {
360 SetAutoLayout(TRUE);
361
362 wxString date;
363 date.Printf(wxT("Selected date: %s"),
364 wxDateTime::Today().FormatISODate().c_str());
365 m_date = new wxStaticText(this, -1, date);
366 m_calendar = new wxCalendarCtrl(this, Calendar_CalCtrl,
367 wxDefaultDateTime,
368 wxDefaultPosition,
369 wxDefaultSize,
370 wxCAL_MONDAY_FIRST |
371 wxCAL_SHOW_HOLIDAYS |
372 wxRAISED_BORDER);
373
374 wxLayoutConstraints *c = new wxLayoutConstraints;
375 c->left.SameAs(this, wxLeft, 10);
376 c->centreY.SameAs(this, wxCentreY);
377 c->height.AsIs();
378 c->width.AsIs();
379
380 m_date->SetConstraints(c);
381
382 c = new wxLayoutConstraints;
383 c->left.SameAs(m_date, wxRight, 20);
384 c->centreY.SameAs(this, wxCentreY);
385 c->height.AsIs();
386 c->width.AsIs();
387
388 m_calendar->SetConstraints(c);
389 }
390
391 void MyPanel::OnCalendar(wxCalendarEvent& event)
392 {
393 wxLogMessage(wxT("Selected %s from calendar"),
394 event.GetDate().FormatISODate().c_str());
395 }
396
397 void MyPanel::OnCalendarChange(wxCalendarEvent& event)
398 {
399 wxString s;
400 s.Printf(wxT("Selected date: %s"), event.GetDate().FormatISODate().c_str());
401
402 m_date->SetLabel(s);
403 }
404
405 void MyPanel::OnCalMonthChange(wxCalendarEvent& WXUNUSED(event))
406 {
407 wxLogStatus(wxT("Calendar month changed"));
408 }
409
410 void MyPanel::OnCalYearChange(wxCalendarEvent& WXUNUSED(event))
411 {
412 wxLogStatus(wxT("Calendar year changed"));
413 }
414
415 void MyPanel::OnCalendarWeekDayClick(wxCalendarEvent& event)
416 {
417 wxLogMessage(wxT("Clicked on %s"),
418 wxDateTime::GetWeekDayName(event.GetWeekDay()).c_str());
419 }
420
421 void MyPanel::ToggleCalStyle(bool on, int flag)
422 {
423 long style = m_calendar->GetWindowStyle();
424 if ( on )
425 style |= flag;
426 else
427 style &= ~flag;
428
429 m_calendar->SetWindowStyle(style);
430
431 m_calendar->Refresh();
432 }
433
434 void MyPanel::HighlightSpecial(bool on)
435 {
436 if ( on )
437 {
438 wxCalendarDateAttr
439 *attrRedCircle = new wxCalendarDateAttr(wxCAL_BORDER_ROUND, *wxRED),
440 *attrGreenSquare = new wxCalendarDateAttr(wxCAL_BORDER_SQUARE, *wxGREEN),
441 *attrHeaderLike = new wxCalendarDateAttr(*wxBLUE, *wxLIGHT_GREY);
442
443 m_calendar->SetAttr(17, attrRedCircle);
444 m_calendar->SetAttr(29, attrGreenSquare);
445 m_calendar->SetAttr(13, attrHeaderLike);
446 }
447 else // off
448 {
449 m_calendar->ResetAttr(17);
450 m_calendar->ResetAttr(29);
451 m_calendar->ResetAttr(13);
452 }
453
454 m_calendar->Refresh();
455 }
456
457 void MyPanel::SetDate()
458 {
459 wxDateTime date(24, wxDateTime::Dec, 2005, 23, 59, 59);
460 m_calendar->SetDate(date);
461 }
462
463 void MyPanel::Today()
464 {
465 m_calendar->SetDate(wxDateTime::Today());
466 }