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