]>
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 | ||
81 | private: | |
82 | wxCalendarCtrl *m_calendar; | |
83 | wxStaticText *m_date; | |
84 | ||
85 | DECLARE_EVENT_TABLE() | |
86 | }; | |
87 | ||
88 | // Define a new frame type: this is going to be our main frame | |
89 | class MyFrame : public wxFrame | |
90 | { | |
91 | public: | |
92 | // ctor(s) | |
93 | MyFrame(const wxString& title, const wxPoint& pos, const wxSize& size); | |
94 | ||
95 | // event handlers (these functions should _not_ be virtual) | |
96 | void OnQuit(wxCommandEvent& event); | |
97 | void OnAbout(wxCommandEvent& event); | |
98 | ||
99 | void OnCalMonday(wxCommandEvent& event); | |
100 | void OnCalHolidays(wxCommandEvent& event); | |
101 | void OnCalSpecial(wxCommandEvent& event); | |
102 | ||
bc385ba9 VZ |
103 | void OnCalAllowMonth(wxCommandEvent& event); |
104 | void OnCalAllowYear(wxCommandEvent& event); | |
105 | ||
37df1f33 VZ |
106 | void OnCalSeqMonth(wxCommandEvent& event); |
107 | void OnCalShowSurroundingWeeks(wxCommandEvent& event); | |
108 | ||
bc385ba9 VZ |
109 | void OnAllowYearUpdate(wxUpdateUIEvent& event); |
110 | ||
74a533f7 VZ |
111 | private: |
112 | MyPanel *m_panel; | |
113 | ||
114 | // any class wishing to process wxWindows events must use this macro | |
115 | DECLARE_EVENT_TABLE() | |
116 | }; | |
117 | ||
118 | // ---------------------------------------------------------------------------- | |
119 | // constants | |
120 | // ---------------------------------------------------------------------------- | |
121 | ||
122 | // IDs for the controls and the menu commands | |
123 | enum | |
124 | { | |
125 | // menu items | |
126 | Calendar_File_About = 100, | |
127 | Calendar_File_Quit, | |
128 | Calendar_Cal_Monday = 200, | |
129 | Calendar_Cal_Holidays, | |
130 | Calendar_Cal_Special, | |
bc385ba9 VZ |
131 | Calendar_Cal_Month, |
132 | Calendar_Cal_Year, | |
37df1f33 VZ |
133 | Calendar_Cal_SeqMonth, |
134 | Calendar_Cal_SurroundWeeks, | |
e9561b3b | 135 | Calendar_CalCtrl = 1000 |
74a533f7 VZ |
136 | }; |
137 | ||
138 | // ---------------------------------------------------------------------------- | |
139 | // event tables and other macros for wxWindows | |
140 | // ---------------------------------------------------------------------------- | |
141 | ||
142 | // the event tables connect the wxWindows events with the functions (event | |
143 | // handlers) which process them. It can be also done at run-time, but for the | |
144 | // simple menu events like this the static method is much simpler. | |
145 | BEGIN_EVENT_TABLE(MyFrame, wxFrame) | |
146 | EVT_MENU(Calendar_File_Quit, MyFrame::OnQuit) | |
147 | EVT_MENU(Calendar_File_About, MyFrame::OnAbout) | |
148 | ||
149 | EVT_MENU(Calendar_Cal_Monday, MyFrame::OnCalMonday) | |
150 | EVT_MENU(Calendar_Cal_Holidays, MyFrame::OnCalHolidays) | |
151 | EVT_MENU(Calendar_Cal_Special, MyFrame::OnCalSpecial) | |
bc385ba9 VZ |
152 | |
153 | EVT_MENU(Calendar_Cal_Month, MyFrame::OnCalAllowMonth) | |
154 | EVT_MENU(Calendar_Cal_Year, MyFrame::OnCalAllowYear) | |
155 | ||
37df1f33 VZ |
156 | EVT_MENU(Calendar_Cal_SeqMonth, MyFrame::OnCalSeqMonth) |
157 | EVT_MENU(Calendar_Cal_SurroundWeeks, MyFrame::OnCalShowSurroundingWeeks) | |
158 | ||
bc385ba9 | 159 | EVT_UPDATE_UI(Calendar_Cal_Year, MyFrame::OnAllowYearUpdate) |
74a533f7 VZ |
160 | END_EVENT_TABLE() |
161 | ||
162 | BEGIN_EVENT_TABLE(MyPanel, wxPanel) | |
163 | EVT_CALENDAR (Calendar_CalCtrl, MyPanel::OnCalendar) | |
0de868d9 VZ |
164 | EVT_CALENDAR_MONTH (Calendar_CalCtrl, MyPanel::OnCalMonthChange) |
165 | EVT_CALENDAR_YEAR (Calendar_CalCtrl, MyPanel::OnCalYearChange) | |
74a533f7 VZ |
166 | EVT_CALENDAR_SEL_CHANGED(Calendar_CalCtrl, MyPanel::OnCalendarChange) |
167 | EVT_CALENDAR_WEEKDAY_CLICKED(Calendar_CalCtrl, MyPanel::OnCalendarWeekDayClick) | |
168 | END_EVENT_TABLE() | |
169 | ||
170 | // Create a new application object: this macro will allow wxWindows to create | |
171 | // the application object during program execution (it's better than using a | |
172 | // static object for many reasons) and also declares the accessor function | |
173 | // wxGetApp() which will return the reference of the right type (i.e. MyApp and | |
174 | // not wxApp) | |
175 | IMPLEMENT_APP(MyApp) | |
176 | ||
177 | // ============================================================================ | |
178 | // implementation | |
179 | // ============================================================================ | |
180 | ||
181 | // ---------------------------------------------------------------------------- | |
182 | // the application class | |
183 | // ---------------------------------------------------------------------------- | |
184 | ||
185 | // `Main program' equivalent: the program execution "starts" here | |
186 | bool MyApp::OnInit() | |
187 | { | |
188 | // Create the main application window | |
9f84eccd | 189 | MyFrame *frame = new MyFrame(_T("Calendar wxWindows sample"), |
4e6bceff | 190 | wxPoint(50, 50), wxSize(450, 340)); |
74a533f7 | 191 | |
74a533f7 | 192 | frame->Show(TRUE); |
74a533f7 VZ |
193 | |
194 | // success: wxApp::OnRun() will be called which will enter the main message | |
195 | // loop and the application will run. If we returned FALSE here, the | |
196 | // application would exit immediately. | |
197 | return TRUE; | |
198 | } | |
199 | ||
200 | // ---------------------------------------------------------------------------- | |
201 | // main frame | |
202 | // ---------------------------------------------------------------------------- | |
203 | ||
204 | // frame constructor | |
205 | MyFrame::MyFrame(const wxString& title, const wxPoint& pos, const wxSize& size) | |
206 | : wxFrame((wxFrame *)NULL, -1, title, pos, size) | |
207 | { | |
208 | // create a menu bar | |
209 | wxMenu *menuFile = new wxMenu; | |
210 | ||
9f84eccd | 211 | menuFile->Append(Calendar_File_About, _T("&About...\tCtrl-A"), _T("Show about dialog")); |
74a533f7 | 212 | menuFile->AppendSeparator(); |
9f84eccd | 213 | menuFile->Append(Calendar_File_Quit, _T("E&xit\tAlt-X"), _T("Quit this program")); |
74a533f7 VZ |
214 | |
215 | wxMenu *menuCal = new wxMenu; | |
216 | menuCal->Append(Calendar_Cal_Monday, | |
9f84eccd MB |
217 | _T("Monday &first weekday\tCtrl-F"), |
218 | _T("Toggle between Mon and Sun as the first week day"), | |
74a533f7 | 219 | TRUE); |
9f84eccd MB |
220 | menuCal->Append(Calendar_Cal_Holidays, _T("Show &holidays\tCtrl-H"), |
221 | _T("Toggle highlighting the holidays"), | |
74a533f7 | 222 | TRUE); |
9f84eccd MB |
223 | menuCal->Append(Calendar_Cal_Special, _T("Highlight &special dates\tCtrl-S"), |
224 | _T("Test custom highlighting"), | |
74a533f7 | 225 | TRUE); |
37df1f33 | 226 | menuCal->Append(Calendar_Cal_SurroundWeeks, |
9f84eccd MB |
227 | _T("Show s&urrounding weeks\tCtrl-W"), |
228 | _T("Show the neighbouring weeks in the prev/next month"), | |
37df1f33 | 229 | TRUE); |
bc385ba9 | 230 | menuCal->AppendSeparator(); |
37df1f33 | 231 | menuCal->Append(Calendar_Cal_SeqMonth, |
9f84eccd MB |
232 | _T("To&ggle month selector style\tCtrl-G"), |
233 | _T("Use another style for the calendar controls"), | |
37df1f33 | 234 | TRUE); |
9f84eccd MB |
235 | menuCal->Append(Calendar_Cal_Month, _T("&Month can be changed\tCtrl-M"), |
236 | _T("Allow changing the month in the calendar"), | |
bc385ba9 | 237 | TRUE); |
9f84eccd MB |
238 | menuCal->Append(Calendar_Cal_Year, _T("&Year can be changed\tCtrl-Y"), |
239 | _T("Allow changing the year in the calendar"), | |
bc385ba9 | 240 | TRUE); |
74a533f7 VZ |
241 | |
242 | // now append the freshly created menu to the menu bar... | |
243 | wxMenuBar *menuBar = new wxMenuBar; | |
9f84eccd MB |
244 | menuBar->Append(menuFile, _T("&File")); |
245 | menuBar->Append(menuCal, _T("&Calendar")); | |
74a533f7 VZ |
246 | |
247 | menuBar->Check(Calendar_Cal_Monday, TRUE); | |
248 | menuBar->Check(Calendar_Cal_Holidays, TRUE); | |
bc385ba9 VZ |
249 | menuBar->Check(Calendar_Cal_Month, TRUE); |
250 | menuBar->Check(Calendar_Cal_Year, TRUE); | |
74a533f7 VZ |
251 | |
252 | // ... and attach this menu bar to the frame | |
253 | SetMenuBar(menuBar); | |
254 | ||
255 | m_panel = new MyPanel(this); | |
256 | ||
257 | #if wxUSE_STATUSBAR | |
258 | // create a status bar just for fun (by default with 1 pane only) | |
9898fcda | 259 | CreateStatusBar(2); |
9f84eccd | 260 | SetStatusText(_T("Welcome to wxWindows!")); |
74a533f7 VZ |
261 | #endif // wxUSE_STATUSBAR |
262 | } | |
263 | ||
264 | void MyFrame::OnQuit(wxCommandEvent& WXUNUSED(event)) | |
265 | { | |
266 | // TRUE is to force the frame to close | |
267 | Close(TRUE); | |
268 | } | |
269 | ||
270 | void MyFrame::OnAbout(wxCommandEvent& WXUNUSED(event)) | |
271 | { | |
272 |