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