]>
Commit | Line | Data |
---|---|---|
74a533f7 VZ |
1 | ///////////////////////////////////////////////////////////////////////////// |
2 | // Name: calendar.cpp | |
3 | // Purpose: wxCalendarCtrl sample | |
4 | // Author: Vadim Zeitlin | |
4e6bceff | 5 | // Modified by: |
74a533f7 VZ |
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 | ||
788233da | 20 | #if defined(__GNUG__) && !defined(__APPLE__) |
74a533f7 VZ |
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 | ||
273b7ed9 | 32 | // for all others, include the necessary headers |
74a533f7 VZ |
33 | #ifndef WX_PRECOMP |
34 | #include "wx/app.h" | |
788233da | 35 | #include "wx/log.h" |
74a533f7 | 36 | #include "wx/frame.h" |
273b7ed9 VZ |
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" | |
74a533f7 VZ |
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); | |
0de868d9 VZ |
71 | void OnCalMonthChange(wxCalendarEvent& event); |
72 | void OnCalYearChange(wxCalendarEvent& event); | |
74a533f7 | 73 | |
bc385ba9 VZ |
74 | wxCalendarCtrl *GetCal() const { return m_calendar; } |
75 | ||
37df1f33 VZ |
76 | // turn on/off the specified style bit on the calendar control |
77 | void ToggleCalStyle(bool on, int style); | |
78 | ||
74a533f7 VZ |
79 | void HighlightSpecial(bool on); |
80 | ||
605dfd91 JS |
81 | void SetDate(); |
82 | void Today(); | |
83 | ||
74a533f7 VZ |
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 | ||
bc385ba9 VZ |
106 | void OnCalAllowMonth(wxCommandEvent& event); |
107 | void OnCalAllowYear(wxCommandEvent& event); | |
108 | ||
37df1f33 VZ |
109 | void OnCalSeqMonth(wxCommandEvent& event); |
110 | void OnCalShowSurroundingWeeks(wxCommandEvent& event); | |
111 | ||
605dfd91 JS |
112 | void OnSetDate(wxCommandEvent& event); |
113 | void OnToday(wxCommandEvent& event); | |
114 | ||
bc385ba9 VZ |
115 | void OnAllowYearUpdate(wxUpdateUIEvent& event); |
116 | ||
74a533f7 VZ |
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, | |
bc385ba9 VZ |
137 | Calendar_Cal_Month, |
138 | Calendar_Cal_Year, | |
37df1f33 VZ |
139 | Calendar_Cal_SeqMonth, |
140 | Calendar_Cal_SurroundWeeks, | |
605dfd91 JS |
141 | Calendar_Cal_SetDate, |
142 | Calendar_Cal_Today, | |
e9561b3b | 143 | Calendar_CalCtrl = 1000 |
74a533f7 VZ |
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) | |
bc385ba9 VZ |
160 | |
161 | EVT_MENU(Calendar_Cal_Month, MyFrame::OnCalAllowMonth) | |
162 | EVT_MENU(Calendar_Cal_Year, MyFrame::OnCalAllowYear) | |
163 | ||
37df1f33 VZ |
164 | EVT_MENU(Calendar_Cal_SeqMonth, MyFrame::OnCalSeqMonth) |
165 | EVT_MENU(Calendar_Cal_SurroundWeeks, MyFrame::OnCalShowSurroundingWeeks) | |
166 | ||
605dfd91 JS |
167 | EVT_MENU(Calendar_Cal_SetDate, MyFrame::OnSetDate) |
168 | EVT_MENU(Calendar_Cal_Today, MyFrame::OnToday) | |
169 | ||
170 | ||
bc385ba9 | 171 | EVT_UPDATE_UI(Calendar_Cal_Year, MyFrame::OnAllowYearUpdate) |
74a533f7 VZ |
172 | END_EVENT_TABLE() |
173 | ||
174 | BEGIN_EVENT_TABLE(MyPanel, wxPanel) | |
175 | EVT_CALENDAR (Calendar_CalCtrl, MyPanel::OnCalendar) | |
0de868d9 VZ |
176 | EVT_CALENDAR_MONTH (Calendar_CalCtrl, MyPanel::OnCalMonthChange) |
177 | EVT_CALENDAR_YEAR (Calendar_CalCtrl, MyPanel::OnCalYearChange) | |
74a533f7 VZ |
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 | |
9f84eccd | 201 | MyFrame *frame = new MyFrame(_T("Calendar wxWindows sample"), |
4e6bceff | 202 | wxPoint(50, 50), wxSize(450, 340)); |
74a533f7 | 203 | |
74a533f7 | 204 | frame->Show(TRUE); |
74a533f7 VZ |
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 | ||
9f84eccd | 223 | menuFile->Append(Calendar_File_About, _T("&About...\tCtrl-A"), _T("Show about dialog")); |
74a533f7 | 224 | menuFile->AppendSeparator(); |
9f84eccd | 225 | menuFile->Append(Calendar_File_Quit, _T("E&xit\tAlt-X"), _T("Quit this program")); |
74a533f7 VZ |
226 | |
227 | wxMenu *menuCal = new wxMenu; | |
228 | menuCal->Append(Calendar_Cal_Monday, | |
9f84eccd MB |
229 | _T("Monday &first weekday\tCtrl-F"), |
230 | _T("Toggle between Mon and Sun as the first week day"), | |
74a533f7 | 231 | TRUE); |
9f84eccd MB |
232 | menuCal->Append(Calendar_Cal_Holidays, _T("Show &holidays\tCtrl-H"), |
233 | _T("Toggle highlighting the holidays"), | |
74a533f7 | 234 | TRUE); |
9f84eccd MB |
235 | menuCal->Append(Calendar_Cal_Special, _T("Highlight &special dates\tCtrl-S"), |
236 | _T("Test custom highlighting"), | |
74a533f7 | 237 | TRUE); |
37df1f33 | 238 | menuCal->Append(Calendar_Cal_SurroundWeeks, |
9f84eccd MB |
239 | _T("Show s&urrounding weeks\tCtrl-W"), |
240 | _T("Show the neighbouring weeks in the prev/next month"), | |
37df1f33 | 241 | TRUE); |
bc385ba9 | 242 | menuCal->AppendSeparator(); |
37df1f33 | 243 | menuCal->Append(Calendar_Cal_SeqMonth, |
9f84eccd MB |
244 | _T("To&ggle month selector style\tCtrl-G"), |
245 | _T("Use another style for the calendar controls"), | |
37df1f33 | 246 | TRUE); |
9f84eccd MB |
247 | menuCal->Append(Calendar_Cal_Month, _T("&Month can be changed\tCtrl-M"), |
248 | _T("Allow changing the month in the calendar"), | |
bc385ba9 | 249 | TRUE); |
9f84eccd MB |
250 | menuCal->Append(Calendar_Cal_Year, _T("&Year can be changed\tCtrl-Y"), |
251 | _T("Allow changing the year in the calendar"), | |
bc385ba9 | 252 | TRUE); |
605dfd91 | 253 | menuCal->AppendSeparator(); |
529b7f71 JS |
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.")); | |
74a533f7 VZ |
256 | |
257 | // now append the freshly created menu to the menu bar... | |
258 | wxMenuBar *menuBar = new wxMenuBar; | |
9f84eccd MB |
259 | menuBar->Append(menuFile, _T("&File")); |
260 | menuBar->Append(menuCal, _T("&Calendar")); | |
74a533f7 VZ |
261 | |
262 | menuBar->Check(Calendar_Cal_Monday, TRUE); | |
263 | menuBar->Check(Calendar_Cal_Holidays, TRUE); | |
bc385ba9 VZ |
264 | menuBar->Check(Calendar_Cal_Month, TRUE); |
265 | menuBar->Check(Calendar_Cal_Year, TRUE); | |
74a533f7 VZ |
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) | |
9898fcda | 274 | CreateStatusBar(2); |
9f84eccd | 275 | SetStatusText(_T("Welcome to wxWindows!")); |
74a533f7 VZ |
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 |