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