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