1 /////////////////////////////////////////////////////////////////////////////
3 // Purpose: wxCalendarCtrl sample
4 // Author: Vadim Zeitlin
8 // Copyright: (c) Vadim Zeitlin
9 // Licence: wxWindows licence
10 /////////////////////////////////////////////////////////////////////////////
12 // ============================================================================
14 // ============================================================================
16 // ----------------------------------------------------------------------------
18 // ----------------------------------------------------------------------------
21 #pragma implementation "calendar.cpp"
22 #pragma interface "calendar.cpp"
25 // For compilers that support precompilation, includes "wx/wx.h".
26 #include "wx/wxprec.h"
32 // for all others, include the necessary headers
37 #include "wx/stattext.h"
39 #include "wx/layout.h"
40 #include "wx/msgdlg.h"
43 #include "wx/calctrl.h"
45 // ----------------------------------------------------------------------------
47 // ----------------------------------------------------------------------------
49 // Define a new application type, each program should derive a class from wxApp
50 class MyApp
: public wxApp
53 // override base class virtuals
54 // ----------------------------
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();
62 class MyPanel
: public wxPanel
65 MyPanel(wxFrame
*frame
);
67 void OnCalendar(wxCalendarEvent
& event
);
68 void OnCalendarWeekDayClick(wxCalendarEvent
& event
);
69 void OnCalendarChange(wxCalendarEvent
& event
);
71 void StartWithMonday(bool on
);
72 void ShowHolidays(bool on
);
73 void HighlightSpecial(bool on
);
76 wxCalendarCtrl
*m_calendar
;
82 // Define a new frame type: this is going to be our main frame
83 class MyFrame
: public wxFrame
87 MyFrame(const wxString
& title
, const wxPoint
& pos
, const wxSize
& size
);
89 // event handlers (these functions should _not_ be virtual)
90 void OnQuit(wxCommandEvent
& event
);
91 void OnAbout(wxCommandEvent
& event
);
93 void OnCalMonday(wxCommandEvent
& event
);
94 void OnCalHolidays(wxCommandEvent
& event
);
95 void OnCalSpecial(wxCommandEvent
& event
);
100 // any class wishing to process wxWindows events must use this macro
101 DECLARE_EVENT_TABLE()
104 // ----------------------------------------------------------------------------
106 // ----------------------------------------------------------------------------
108 // IDs for the controls and the menu commands
112 Calendar_File_About
= 100,
114 Calendar_Cal_Monday
= 200,
115 Calendar_Cal_Holidays
,
116 Calendar_Cal_Special
,
117 Calendar_CalCtrl
= 1000,
120 // ----------------------------------------------------------------------------
121 // event tables and other macros for wxWindows
122 // ----------------------------------------------------------------------------
124 // the event tables connect the wxWindows events with the functions (event
125 // handlers) which process them. It can be also done at run-time, but for the
126 // simple menu events like this the static method is much simpler.
127 BEGIN_EVENT_TABLE(MyFrame
, wxFrame
)
128 EVT_MENU(Calendar_File_Quit
, MyFrame
::OnQuit
)
129 EVT_MENU(Calendar_File_About
, MyFrame
::OnAbout
)
131 EVT_MENU(Calendar_Cal_Monday
, MyFrame
::OnCalMonday
)
132 EVT_MENU(Calendar_Cal_Holidays
, MyFrame
::OnCalHolidays
)
133 EVT_MENU(Calendar_Cal_Special
, MyFrame
::OnCalSpecial
)
136 BEGIN_EVENT_TABLE(MyPanel
, wxPanel
)
137 EVT_CALENDAR (Calendar_CalCtrl
, MyPanel
::OnCalendar
)
138 EVT_CALENDAR_SEL_CHANGED(Calendar_CalCtrl
, MyPanel
::OnCalendarChange
)
139 EVT_CALENDAR_WEEKDAY_CLICKED(Calendar_CalCtrl
, MyPanel
::OnCalendarWeekDayClick
)
142 // Create a new application object: this macro will allow wxWindows to create
143 // the application object during program execution (it's better than using a
144 // static object for many reasons) and also declares the accessor function
145 // wxGetApp() which will return the reference of the right type (i.e. MyApp and
149 // ============================================================================
151 // ============================================================================
153 // ----------------------------------------------------------------------------
154 // the application class
155 // ----------------------------------------------------------------------------
157 // `Main program' equivalent: the program execution "starts" here
160 // Create the main application window
161 MyFrame
*frame
= new MyFrame("Minimal wxWindows App",
162 wxPoint(50, 50), wxSize(450, 340));
164 // Show it and tell the application that it's our main window
165 // @@@ what does it do exactly, in fact? is it necessary here?
169 // success: wxApp::OnRun() will be called which will enter the main message
170 // loop and the application will run. If we returned FALSE here, the
171 // application would exit immediately.
175 // ----------------------------------------------------------------------------
177 // ----------------------------------------------------------------------------
180 MyFrame
::MyFrame(const wxString
& title
, const wxPoint
& pos
, const wxSize
& size
)
181 : wxFrame((wxFrame
*)NULL
, -1, title
, pos
, size
)
184 wxMenu
*menuFile
= new wxMenu
;
186 menuFile
->Append(Calendar_File_About
, "&About...\tCtrl-A", "Show about dialog");
187 menuFile
->AppendSeparator();
188 menuFile
->Append(Calendar_File_Quit
, "E&xit\tAlt-X", "Quit this program");
190 wxMenu
*menuCal
= new wxMenu
;
191 menuCal
->Append(Calendar_Cal_Monday
,
192 "&Monday first weekday\tCtrl-M",
193 "Toggle between Mon and Sun as the first week day",
195 menuCal
->Append(Calendar_Cal_Holidays
, "Show &holidays\tCtrl-H",
196 "Toggle highlighting the holidays",
198 menuCal
->Append(Calendar_Cal_Special
, "Highlight &special dates\tCtrl-S",
199 "Test custom highlighting",
202 // now append the freshly created menu to the menu bar...
203 wxMenuBar
*menuBar
= new wxMenuBar
;
204 menuBar
->Append(menuFile
, "&File");
205 menuBar
->Append(menuCal
, "&Calendar");
207 menuBar
->Check(Calendar_Cal_Monday
, TRUE
);
208 menuBar
->Check(Calendar_Cal_Holidays
, TRUE
);
210 // ... and attach this menu bar to the frame
213 m_panel
= new MyPanel(this);
216 // create a status bar just for fun (by default with 1 pane only)
218 SetStatusText("Welcome to wxWindows!");
219 #endif // wxUSE_STATUSBAR
222 void MyFrame
::OnQuit(wxCommandEvent
& WXUNUSED(event
))
224 // TRUE is to force the frame to close
228 void MyFrame
::OnAbout(wxCommandEvent
& WXUNUSED(event
))
230 wxMessageBox(_T("wxCalendarCtrl sample\n© 2000 Vadim Zeitlin"),
231 _T("About Calendar"), wxOK
| wxICON_INFORMATION
, this);
234 void MyFrame
::OnCalMonday(wxCommandEvent
& event
)
236 m_panel
->StartWithMonday(GetMenuBar()->IsChecked(event
.GetInt()) != 0);
239 void MyFrame
::OnCalHolidays(wxCommandEvent
& event
)
241 m_panel
->ShowHolidays(GetMenuBar()->IsChecked(event
.GetInt()) != 0);
244 void MyFrame
::OnCalSpecial(wxCommandEvent
& event
)
246 m_panel
->HighlightSpecial(GetMenuBar()->IsChecked(event
.GetInt()) != 0);
249 // ----------------------------------------------------------------------------
251 // ----------------------------------------------------------------------------
253 MyPanel
::MyPanel(wxFrame
*frame
)
259 date
.Printf("Selected date: %s",
260 wxDateTime
::Today().FormatISODate().c_str());
261 m_date
= new wxStaticText(this, -1, date
);
262 m_calendar
= new wxCalendarCtrl(this, Calendar_CalCtrl
,
266 wxCAL_MONDAY_FIRST
| wxCAL_SHOW_HOLIDAYS
);
268 wxLayoutConstraints
*c
= new wxLayoutConstraints
;
269 c
->left
.SameAs(this, wxLeft
, 10);
270 c
->centreY
.SameAs(m_calendar
, wxCentreY
);
274 m_date
->SetConstraints(c
);
276 c
= new wxLayoutConstraints
;
277 c
->left
.SameAs(m_date
, wxRight
, 10);
278 c
->top
.SameAs(this, wxTop
, 10);
282 m_calendar
->SetConstraints(c
);
285 void MyPanel
::OnCalendar(wxCalendarEvent
& event
)
287 wxLogMessage("Selected %s from calendar",
288 event
.GetDate().FormatISODate().c_str());
291 void MyPanel
::OnCalendarChange(wxCalendarEvent
& event
)
294 s
.Printf("Selected date: %s", event
.GetDate().FormatISODate().c_str());
299 void MyPanel
::OnCalendarWeekDayClick(wxCalendarEvent
& event
)
301 wxLogMessage("Clicked on %s",
302 wxDateTime
::GetWeekDayName(event
.GetWeekDay()).c_str());
305 void MyPanel
::StartWithMonday(bool on
)
307 long style
= m_calendar
->GetWindowStyle();
309 style
|= wxCAL_MONDAY_FIRST
;
311 style
&= ~wxCAL_MONDAY_FIRST
;
313 m_calendar
->SetWindowStyle(style
);
315 m_calendar
->Refresh();
318 void MyPanel
::ShowHolidays(bool on
)
320 m_calendar
->EnableHolidayDisplay(on
);
323 void MyPanel
::HighlightSpecial(bool on
)
328 *attrRedCircle
= new wxCalendarDateAttr(wxCAL_BORDER_ROUND
, *wxRED
),
329 *attrGreenSquare
= new wxCalendarDateAttr(wxCAL_BORDER_SQUARE
, *wxGREEN
),
330 *attrHeaderLike
= new wxCalendarDateAttr(*wxBLUE
, *wxLIGHT_GREY
);
332 m_calendar
->SetAttr(17, attrRedCircle
);
333 m_calendar
->SetAttr(29, attrGreenSquare
);
334 m_calendar
->SetAttr(13, attrHeaderLike
);
338 m_calendar
->ResetAttr(17);
339 m_calendar
->ResetAttr(29);
340 m_calendar
->ResetAttr(13);
343 m_calendar
->Refresh();