]> git.saurik.com Git - wxWidgets.git/blame - samples/widgets/datepick.cpp
Remove unnecessary wxHTML dependency from the notebook sample.
[wxWidgets.git] / samples / widgets / datepick.cpp
CommitLineData
f2fdc4d5
WS
1/////////////////////////////////////////////////////////////////////////////
2// Program: wxWidgets Widgets Sample
3// Name: datepick.cpp
4// Purpose: Part of the widgets sample showing date picker
5// Author: Dimitri Schoolwerth, Vadim Zeitlin
6// Created: 27 Sep 2003
f2fdc4d5 7// Copyright: (c) 2003 wxWindows team
526954c5 8// Licence: wxWindows licence
f2fdc4d5
WS
9/////////////////////////////////////////////////////////////////////////////
10
11// ============================================================================
12// declarations
13// ============================================================================
14
15// ----------------------------------------------------------------------------
16// headers
17// ----------------------------------------------------------------------------
18
19// for compilers that support precompilation, includes "wx/wx.h".
20#include "wx/wxprec.h"
21
22#ifdef __BORLANDC__
23 #pragma hdrstop
24#endif
25
26#if wxUSE_DATEPICKCTRL
27
28// for all others, include the necessary headers
29#ifndef WX_PRECOMP
30 #include "wx/app.h"
31 #include "wx/log.h"
32
33 #include "wx/bitmap.h"
34 #include "wx/button.h"
35 #include "wx/checkbox.h"
36 #include "wx/radiobox.h"
37 #include "wx/statbox.h"
38 #include "wx/textctrl.h"
39
40 #include "wx/sizer.h"
41#endif
42
43#include "wx/datectrl.h"
54450d09 44#include "wx/dateevt.h"
f2fdc4d5
WS
45
46#include "widgets.h"
47
48#include "icons/datepick.xpm"
49
50// ----------------------------------------------------------------------------
51// constants
52// ----------------------------------------------------------------------------
53
54// control ids
55enum
56{
57 DatePickerPage_Reset = wxID_HIGHEST,
f2fdc4d5 58 DatePickerPage_Set,
54450d09 59 DatePickerPage_SetRange,
f2fdc4d5
WS
60 DatePickerPage_Picker
61};
62
63// ----------------------------------------------------------------------------
64// CheckBoxWidgetsPage
65// ----------------------------------------------------------------------------
66
67class DatePickerWidgetsPage : public WidgetsPage
68{
69public:
70 DatePickerWidgetsPage(WidgetsBookCtrl *book, wxImageList *imaglist);
71 virtual ~DatePickerWidgetsPage(){};
72
73 virtual wxControl *GetWidget() const { return m_datePicker; }
74 virtual void RecreateWidget() { CreateDatePicker(); }
75
453535a7
WS
76 // lazy creation of the content
77 virtual void CreateContent();
78
f2fdc4d5
WS
79protected:
80 // event handlers
54450d09 81 void OnDateChanged(wxDateEvent& event);
f2fdc4d5 82
54450d09
VZ
83 void OnButtonSet(wxCommandEvent& event);
84 void OnButtonSetRange(wxCommandEvent& event);
f2fdc4d5
WS
85 void OnButtonReset(wxCommandEvent& event);
86
87 // reset the date picker parameters
88 void Reset();
89
90 // (re)create the date picker
91 void CreateDatePicker();
92
93 // the controls
94 // ------------
95
96 // the checkbox itself and the sizer it is in
97 wxDatePickerCtrl *m_datePicker;
98 wxSizer *m_sizerDatePicker;
99
54450d09
VZ
100 wxTextCtrl *m_textCur;
101 wxTextCtrl *m_textMin;
102 wxTextCtrl *m_textMax;
103
104 wxRadioBox* m_radioKind;
105 wxCheckBox* m_chkStyleCentury;
106 wxCheckBox* m_chkStyleAllowNone;
f2fdc4d5
WS
107
108 // the text entries for command parameters
109 wxTextCtrl *m_textLabel;
110
111private:
112 DECLARE_EVENT_TABLE()
113 DECLARE_WIDGETS_PAGE(DatePickerWidgetsPage)
114};
115
116// ----------------------------------------------------------------------------
117// event tables
118// ----------------------------------------------------------------------------
119
120BEGIN_EVENT_TABLE(DatePickerWidgetsPage, WidgetsPage)
121 EVT_BUTTON(DatePickerPage_Reset, DatePickerWidgetsPage::OnButtonReset)
122 EVT_BUTTON(DatePickerPage_Set, DatePickerWidgetsPage::OnButtonSet)
54450d09
VZ
123 EVT_BUTTON(DatePickerPage_SetRange, DatePickerWidgetsPage::OnButtonSetRange)
124
125 EVT_DATE_CHANGED(wxID_ANY, DatePickerWidgetsPage::OnDateChanged)
f2fdc4d5
WS
126END_EVENT_TABLE()
127
128// ============================================================================
129// implementation
130// ============================================================================
131
f0fa4312
WS
132#if defined(__WXMSW__)
133 #define FAMILY_CTRLS NATIVE_CTRLS
134#else
135 #define FAMILY_CTRLS GENERIC_CTRLS
136#endif
137
f2fdc4d5 138IMPLEMENT_WIDGETS_PAGE(DatePickerWidgetsPage, wxT("DatePicker"),
f0fa4312 139 FAMILY_CTRLS | PICKER_CTRLS
f2fdc4d5
WS
140 );
141
142DatePickerWidgetsPage::DatePickerWidgetsPage(WidgetsBookCtrl *book,
143 wxImageList *imaglist)
261357eb 144 :WidgetsPage(book, imaglist, datepick_xpm)
453535a7
WS
145{
146}
147
148void DatePickerWidgetsPage::CreateContent()
f2fdc4d5 149{
f2fdc4d5
WS
150 wxSizer *sizerTop = new wxBoxSizer(wxHORIZONTAL);
151
54450d09
VZ
152 // left pane: style
153 wxSizer* const sizerLeft = new wxBoxSizer(wxVERTICAL);
154
155 static const wxString kinds[] = { "&Default", "&Spin", "Drop do&wn" };
156 m_radioKind = new wxRadioBox(this, wxID_ANY, "&Kind",
157 wxDefaultPosition, wxDefaultSize,
158 WXSIZEOF(kinds), kinds,
159 1, wxRA_SPECIFY_COLS);
160 sizerLeft->Add(m_radioKind, wxSizerFlags().Expand().Border());
161
162 wxSizer* const sizerStyle = new wxStaticBoxSizer(wxVERTICAL, this, "&Style");
163 m_chkStyleCentury = CreateCheckBoxAndAddToSizer(sizerStyle, "Show &century");
164 m_chkStyleAllowNone = CreateCheckBoxAndAddToSizer(sizerStyle, "Allow &no value");
165
166 sizerLeft->Add(sizerStyle, wxSizerFlags().Expand().Border());
167
168 sizerLeft->Add(new wxButton(this, DatePickerPage_Reset, "&Recreate"),
169 wxSizerFlags().Centre().Border());
170
171
172 // middle pane: operations
173 wxSizer* const sizerMiddle = new wxBoxSizer(wxVERTICAL);
174 sizerMiddle->Add(CreateSizerWithTextAndButton
175 (
176 DatePickerPage_Set,
177 "&Set date",
178 wxID_ANY,
179 &m_textCur
180 ),
181 wxSizerFlags().Expand().Border());
182
183 m_textCur->SetMinSize(wxSize(GetTextExtent(" 9999-99-99 ").x, -1));
184
185 sizerMiddle->AddSpacer(10);
186
187 sizerMiddle->Add(CreateSizerWithTextAndLabel
188 (
189 "&Min date",
190 wxID_ANY,
191 &m_textMin
192 ),
193 wxSizerFlags().Expand().Border());
194 sizerMiddle->Add(CreateSizerWithTextAndLabel
195 (
196 "Ma&x date",
197 wxID_ANY,
198 &m_textMax
199 ),
200 wxSizerFlags().Expand().Border());
201 sizerMiddle->Add(new wxButton(this, DatePickerPage_SetRange, "Set &range"),
202 wxSizerFlags().Centre().Border());
203
204
205 // right pane: control itself
f2fdc4d5
WS
206 wxSizer *sizerRight = new wxBoxSizer(wxHORIZONTAL);
207
208 m_datePicker = new wxDatePickerCtrl(this, DatePickerPage_Picker);
209
210 sizerRight->Add(0, 0, 1, wxCENTRE);
211 sizerRight->Add(m_datePicker, 1, wxCENTRE);
212 sizerRight->Add(0, 0, 1, wxCENTRE);
f2fdc4d5
WS
213 m_sizerDatePicker = sizerRight; // save it to modify it later
214
215 // the 3 panes panes compose the window
216 sizerTop->Add(sizerLeft, 0, (wxALL & ~wxLEFT), 10);
54450d09 217 sizerTop->Add(sizerMiddle, 0, (wxTOP | wxBOTTOM), 10);
f2fdc4d5
WS
218 sizerTop->Add(sizerRight, 1, wxGROW | (wxALL & ~wxRIGHT), 10);
219
220 // final initializations
54450d09 221 m_chkStyleCentury->SetValue(true);
f2fdc4d5
WS
222 Reset();
223
224 SetSizer(sizerTop);
f2fdc4d5
WS
225}
226
227void DatePickerWidgetsPage::Reset()
228{
229 const wxDateTime today = wxDateTime::Today();
230
231 m_datePicker->SetValue(today);
54450d09 232 m_textCur->SetValue(today.FormatISODate());
f2fdc4d5
WS
233}
234
235void DatePickerWidgetsPage::CreateDatePicker()
236{
237 const wxDateTime value = m_datePicker->GetValue();
238
239 size_t count = m_sizerDatePicker->GetChildren().GetCount();
240 for ( size_t n = 0; n < count; n++ )
241 {
242 m_sizerDatePicker->Remove(0);
243 }
244
245 delete m_datePicker;
246
54450d09
VZ
247 long style = 0;
248 switch ( m_radioKind->GetSelection() )
249 {
250 case 0:
251 style = wxDP_DEFAULT;
252 break;
253
254 case 1:
255 style = wxDP_SPIN;
256 break;
257
258 case 2:
259 style = wxDP_DROPDOWN;
260 break;
261 }
262
263 if ( m_chkStyleCentury->GetValue() )
264 style |= wxDP_SHOWCENTURY;
265 if ( m_chkStyleAllowNone->GetValue() )
266 style |= wxDP_ALLOWNONE;
267
268 m_datePicker = new wxDatePickerCtrl(this, DatePickerPage_Picker, value,
269 wxDefaultPosition, wxDefaultSize,
270 style);
f2fdc4d5
WS
271
272 m_sizerDatePicker->Add(0, 0, 1, wxCENTRE);
273 m_sizerDatePicker->Add(m_datePicker, 1, wxCENTRE);
274 m_sizerDatePicker->Add(0, 0, 1, wxCENTRE);
275 m_sizerDatePicker->Layout();
276}
277
278// ----------------------------------------------------------------------------
279// event handlers
280// ----------------------------------------------------------------------------
281
282void DatePickerWidgetsPage::OnButtonReset(wxCommandEvent& WXUNUSED(event))
283{
284 Reset();
285
286 CreateDatePicker();
287}
288
54450d09 289static bool GetDateFromTextControl(wxDateTime& dt, const wxTextCtrl* text)
f2fdc4d5 290{
54450d09
VZ
291 const wxString& value = text->GetValue();
292 if ( !value.empty() )
95c336d6 293 {
54450d09
VZ
294 wxString::const_iterator end;
295 if ( !dt.ParseDate(value, &end) || end != value.end() )
95c336d6 296 {
54450d09
VZ
297 wxLogError("Invalid date \"%s\"");
298 return false;
95c336d6
WS
299 }
300 }
54450d09
VZ
301
302 return true;
303}
304
305void DatePickerWidgetsPage::OnButtonSet(wxCommandEvent& WXUNUSED(event))
306{
307 wxDateTime dt;
308 if ( GetDateFromTextControl(dt, m_textCur) )
309 m_datePicker->SetValue(dt);
310}
311
312void DatePickerWidgetsPage::OnButtonSetRange(wxCommandEvent& WXUNUSED(event))
313{
314 wxDateTime dt1, dt2;
315 if ( !GetDateFromTextControl(dt1, m_textMin) ||
316 !GetDateFromTextControl(dt2, m_textMax) )
317 return;
318
319 m_datePicker->SetRange(dt1, dt2);
320
321 if ( !m_datePicker->GetRange(&dt1, &dt2) )
322 {
323 wxLogMessage("No range set");
324 }
95c336d6
WS
325 else
326 {
54450d09
VZ
327 m_textMin->SetValue(dt1.IsValid() ? dt1.FormatISODate() : wxString());
328 m_textMax->SetValue(dt2.IsValid() ? dt2.FormatISODate() : wxString());
329
330 wxLogMessage("Date picker range updated");
95c336d6 331 }
f2fdc4d5
WS
332}
333
54450d09
VZ
334void DatePickerWidgetsPage::OnDateChanged(wxDateEvent& event)
335{
336 wxLogMessage("Date changed, now is %s (control value is %s).",
337 event.GetDate().FormatISOCombined(),
338 m_datePicker->GetValue().FormatISOCombined());
339}
340
f2fdc4d5 341#endif // wxUSE_DATEPICKCTRL