]>
git.saurik.com Git - wxWidgets.git/blob - samples/calendar/calendar.cpp
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 (this file is usually all you
33 // need because it includes almost all "standard" wxWindows headers
39 #include "wx/calctrl.h"
41 // ----------------------------------------------------------------------------
43 // ----------------------------------------------------------------------------
45 // Define a new application type, each program should derive a class from wxApp
46 class MyApp
: public wxApp
49 // override base class virtuals
50 // ----------------------------
52 // this one is called on application startup and is a good place for the app
53 // initialization (doing it here and not in the ctor allows to have an error
54 // return: if OnInit() returns false, the application terminates)
55 virtual bool OnInit();
58 class MyPanel
: public wxPanel
61 MyPanel(wxFrame
*frame
);
63 void OnCalendar(wxCalendarEvent
& event
);
64 void OnCalendarWeekDayClick(wxCalendarEvent
& event
);
65 void OnCalendarChange(wxCalendarEvent
& event
);
67 void StartWithMonday(bool on
);
68 void ShowHolidays(bool on
);
69 void HighlightSpecial(bool on
);
72 wxCalendarCtrl
*m_calendar
;
78 // Define a new frame type: this is going to be our main frame
79 class MyFrame
: public wxFrame
83 MyFrame(const wxString
& title
, const wxPoint
& pos
, const wxSize
& size
);
85 // event handlers (these functions should _not_ be virtual)
86 void OnQuit(wxCommandEvent
& event
);
87 void OnAbout(wxCommandEvent
& event
);
89 void OnCalMonday(wxCommandEvent
& event
);
90 void OnCalHolidays(wxCommandEvent
& event
);
91 void OnCalSpecial(wxCommandEvent
& event
);
96 // any class wishing to process wxWindows events must use this macro
100 // ----------------------------------------------------------------------------
102 // ----------------------------------------------------------------------------
104 // IDs for the controls and the menu commands
108 Calendar_File_About
= 100,
110 Calendar_Cal_Monday
= 200,
111 Calendar_Cal_Holidays
,
112 Calendar_Cal_Special
,
113 Calendar_CalCtrl
= 1000,
116 // ----------------------------------------------------------------------------
117 // event tables and other macros for wxWindows
118 // ----------------------------------------------------------------------------
120 // the event tables connect the wxWindows events with the functions (event
121 // handlers) which process them. It can be also done at run-time, but for the
122 // simple menu events like this the static method is much simpler.
123 BEGIN_EVENT_TABLE(MyFrame
, wxFrame
)
124 EVT_MENU(Calendar_File_Quit
, MyFrame::OnQuit
)
125 EVT_MENU(Calendar_File_About
, MyFrame::OnAbout
)
127 EVT_MENU(Calendar_Cal_Monday
, MyFrame::OnCalMonday
)
128 EVT_MENU(Calendar_Cal_Holidays
, MyFrame::OnCalHolidays
)
129 EVT_MENU(Calendar_Cal_Special
, MyFrame::OnCalSpecial
)
132 BEGIN_EVENT_TABLE(MyPanel
, wxPanel
)
133 EVT_CALENDAR (Calendar_CalCtrl
, MyPanel::OnCalendar
)
134 EVT_CALENDAR_SEL_CHANGED(Calendar_CalCtrl
, MyPanel::OnCalendarChange
)
135 EVT_CALENDAR_WEEKDAY_CLICKED(Calendar_CalCtrl
, MyPanel::OnCalendarWeekDayClick
)
138 // Create a new application object: this macro will allow wxWindows to create
139 // the application object during program execution (it's better than using a
140 // static object for many reasons) and also declares the accessor function
141 // wxGetApp() which will return the reference of the right type (i.e. MyApp and
145 // ============================================================================
147 // ============================================================================
149 // ----------------------------------------------------------------------------
150 // the application class
151 // ----------------------------------------------------------------------------
153 // `Main program' equivalent: the program execution "starts" here
156 // Create the main application window
157 MyFrame
*frame
= new MyFrame("Minimal wxWindows App",
158 wxPoint(50, 50), wxSize(450, 340));
160 // Show it and tell the application that it's our main window
161 // @@@ what does it do exactly, in fact? is it necessary here?
165 // success: wxApp::OnRun() will be called which will enter the main message
166 // loop and the application will run. If we returned FALSE here, the
167 // application would exit immediately.
171 // ----------------------------------------------------------------------------
173 // ----------------------------------------------------------------------------
176 MyFrame::MyFrame(const wxString
& title
, const wxPoint
& pos
, const wxSize
& size
)
177 : wxFrame((wxFrame
*)NULL
, -1, title
, pos
, size
)
180 wxMenu
*menuFile
= new wxMenu
;
182 menuFile
->Append(Calendar_File_About
, "&About...\tCtrl-A", "Show about dialog");
183 menuFile
->AppendSeparator();
184 menuFile
->Append(Calendar_File_Quit
, "E&xit\tAlt-X", "Quit this program");
186 wxMenu
*menuCal
= new wxMenu
;
187 menuCal
->Append(Calendar_Cal_Monday
,
188 "&Monday first weekday\tCtrl-M",
189 "Toggle between Mon and Sun as the first week day",
191 menuCal
->Append(Calendar_Cal_Holidays
, "Show &holidays\tCtrl-H",
192 "Toggle highlighting the holidays",
194 menuCal
->Append(Calendar_Cal_Special
, "Highlight &special dates\tCtrl-S",
195 "Test custom highlighting",
198 // now append the freshly created menu to the menu bar...
199 wxMenuBar
*menuBar
= new wxMenuBar
;
200 menuBar
->Append(menuFile
, "&File");
201 menuBar
->Append(menuCal
, "&Calendar");
203 menuBar
->Check(Calendar_Cal_Monday
, TRUE
);
204 menuBar
->Check(Calendar_Cal_Holidays
, TRUE
);
206 // ... and attach this menu bar to the frame
209 m_panel
= new MyPanel(this);
212 // create a status bar just for fun (by default with 1 pane only)
214 SetStatusText("Welcome to wxWindows!");
215 #endif // wxUSE_STATUSBAR
218 void MyFrame::OnQuit(wxCommandEvent
& WXUNUSED(event
))
220 // TRUE is to force the frame to close
224 void MyFrame::OnAbout(wxCommandEvent
& WXUNUSED(event
))
226 wxMessageBox(_T("wxCalendarCtrl sample\n© 2000 Vadim Zeitlin"),
227 _T("About Calendar"), wxOK
| wxICON_INFORMATION
, this);
230 void MyFrame::OnCalMonday(wxCommandEvent
& event
)
232 m_panel
->StartWithMonday(GetMenuBar()->IsChecked(event
.GetInt()) != 0);
235 void MyFrame::OnCalHolidays(wxCommandEvent
& event
)
237 m_panel
->ShowHolidays(GetMenuBar()->IsChecked(event
.GetInt()) != 0);
240 void MyFrame::OnCalSpecial(wxCommandEvent
& event
)
242 m_panel
->HighlightSpecial(GetMenuBar()->IsChecked(event
.GetInt()) != 0);
245 // ----------------------------------------------------------------------------
247 // ----------------------------------------------------------------------------
249 MyPanel::MyPanel(wxFrame
*frame
)
255 date
.Printf("Selected date: %s",
256 wxDateTime::Today().FormatISODate().c_str());
257 m_date
= new wxStaticText(this, -1, date
);
258 m_calendar
= new wxCalendarCtrl(this, Calendar_CalCtrl
,
262 wxCAL_MONDAY_FIRST
| wxCAL_SHOW_HOLIDAYS
);
264 wxLayoutConstraints
*c
= new wxLayoutConstraints
;
265 c
->left
.SameAs(this, wxLeft
, 10);
266 c
->centreY
.SameAs(m_calendar
, wxCentreY
);
270 m_date
->SetConstraints(c
);
272 c
= new wxLayoutConstraints
;
273 c
->left
.SameAs(m_date
, wxRight
, 10);
274 c
->top
.SameAs(this, wxTop
, 10);
278 m_calendar
->SetConstraints(c
);
281 void MyPanel::OnCalendar(wxCalendarEvent
& event
)
283 wxLogMessage("Selected %s from calendar",
284 event
.GetDate().FormatISODate().c_str());
287 void MyPanel::OnCalendarChange(wxCalendarEvent
& event
)
290 s
.Printf("Selected date: %s", event
.GetDate().FormatISODate().c_str());
295 void MyPanel::OnCalendarWeekDayClick(wxCalendarEvent
& event
)
297 wxLogMessage("Clicked on %s",
298 wxDateTime::GetWeekDayName(event
.GetWeekDay()).c_str());
301 void MyPanel::StartWithMonday(bool on
)
303 long style
= m_calendar
->GetWindowStyle();
305 style
|= wxCAL_MONDAY_FIRST
;
307 style
&= ~wxCAL_MONDAY_FIRST
;
309 m_calendar
->SetWindowStyle(style
);
311 m_calendar
->Refresh();
314 void MyPanel::ShowHolidays(bool on
)
316 m_calendar
->EnableHolidayDisplay(on
);
319 void MyPanel::HighlightSpecial(bool on
)
324 *attrRedCircle
= new wxCalendarDateAttr(wxCAL_BORDER_ROUND
, *wxRED
),
325 *attrGreenSquare
= new wxCalendarDateAttr(wxCAL_BORDER_SQUARE
, *wxGREEN
),
326 *attrHeaderLike
= new wxCalendarDateAttr(*wxBLUE
, *wxLIGHT_GREY
);
328 m_calendar
->SetAttr(17, attrRedCircle
);
329 m_calendar
->SetAttr(29, attrGreenSquare
);
330 m_calendar
->SetAttr(13, attrHeaderLike
);
334 m_calendar
->ResetAttr(17);
335 m_calendar
->ResetAttr(29);
336 m_calendar
->ResetAttr(13);
339 m_calendar
->Refresh();