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