]> git.saurik.com Git - wxWidgets.git/blob - samples/widgets/datepick.cpp
Make storing non-trivial data in wxThreadSpecificInfo possible.
[wxWidgets.git] / samples / widgets / datepick.cpp
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 // Copyright: (c) 2003 wxWindows team
8 // Licence: wxWindows licence
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"
44 #include "wx/dateevt.h"
45
46 #include "widgets.h"
47
48 #include "icons/datepick.xpm"
49
50 // ----------------------------------------------------------------------------
51 // constants
52 // ----------------------------------------------------------------------------
53
54 // control ids
55 enum
56 {
57 DatePickerPage_Reset = wxID_HIGHEST,
58 DatePickerPage_Set,
59 DatePickerPage_SetRange,
60 DatePickerPage_Picker
61 };
62
63 // ----------------------------------------------------------------------------
64 // CheckBoxWidgetsPage
65 // ----------------------------------------------------------------------------
66
67 class DatePickerWidgetsPage : public WidgetsPage
68 {
69 public:
70 DatePickerWidgetsPage(WidgetsBookCtrl *book, wxImageList *imaglist);
71 virtual ~DatePickerWidgetsPage(){};
72
73 virtual wxControl *GetWidget() const { return m_datePicker; }
74 virtual void RecreateWidget() { CreateDatePicker(); }
75
76 // lazy creation of the content
77 virtual void CreateContent();
78
79 protected:
80 // event handlers
81 void OnDateChanged(wxDateEvent& event);
82
83 void OnButtonSet(wxCommandEvent& event);
84 void OnButtonSetRange(wxCommandEvent& event);
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
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;
107
108 // the text entries for command parameters
109 wxTextCtrl *m_textLabel;
110
111 private:
112 DECLARE_EVENT_TABLE()
113 DECLARE_WIDGETS_PAGE(DatePickerWidgetsPage)
114 };
115
116 // ----------------------------------------------------------------------------
117 // event tables
118 // ----------------------------------------------------------------------------
119
120 BEGIN_EVENT_TABLE(DatePickerWidgetsPage, WidgetsPage)
121 EVT_BUTTON(DatePickerPage_Reset, DatePickerWidgetsPage::OnButtonReset)
122 EVT_BUTTON(DatePickerPage_Set, DatePickerWidgetsPage::OnButtonSet)
123 EVT_BUTTON(DatePickerPage_SetRange, DatePickerWidgetsPage::OnButtonSetRange)
124
125 EVT_DATE_CHANGED(wxID_ANY, DatePickerWidgetsPage::OnDateChanged)
126 END_EVENT_TABLE()
127
128 // ============================================================================
129 // implementation
130 // ============================================================================
131
132 #if defined(__WXMSW__)
133 #define FAMILY_CTRLS NATIVE_CTRLS
134 #else
135 #define FAMILY_CTRLS GENERIC_CTRLS
136 #endif
137
138 IMPLEMENT_WIDGETS_PAGE(DatePickerWidgetsPage, wxT("DatePicker"),
139 FAMILY_CTRLS | PICKER_CTRLS
140 );
141
142 DatePickerWidgetsPage::DatePickerWidgetsPage(WidgetsBookCtrl *book,
143 wxImageList *imaglist)
144 :WidgetsPage(book, imaglist, datepick_xpm)
145 {
146 }
147
148 void DatePickerWidgetsPage::CreateContent()
149 {
150 wxSizer *sizerTop = new wxBoxSizer(wxHORIZONTAL);
151
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
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);
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);
217 sizerTop->Add(sizerMiddle, 0, (wxTOP | wxBOTTOM), 10);
218 sizerTop->Add(sizerRight, 1, wxGROW | (wxALL & ~wxRIGHT), 10);
219
220 // final initializations
221 m_chkStyleCentury->SetValue(true);
222 Reset();
223
224 SetSizer(sizerTop);
225 }
226
227 void DatePickerWidgetsPage::Reset()
228 {
229 const wxDateTime today = wxDateTime::Today();
230
231 m_datePicker->SetValue(today);
232 m_textCur->SetValue(today.FormatISODate());
233 }
234
235 void 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
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);
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
282 void DatePickerWidgetsPage::OnButtonReset(wxCommandEvent& WXUNUSED(event))
283 {
284 Reset();
285
286 CreateDatePicker();
287 }
288
289 static bool GetDateFromTextControl(wxDateTime& dt, const wxTextCtrl* text)
290 {
291 const wxString& value = text->GetValue();
292 if ( !value.empty() )
293 {
294 wxString::const_iterator end;
295 if ( !dt.ParseDate(value, &end) || end != value.end() )
296 {
297 wxLogError("Invalid date \"%s\"");
298 return false;
299 }
300 }
301
302 return true;
303 }
304
305 void DatePickerWidgetsPage::OnButtonSet(wxCommandEvent& WXUNUSED(event))
306 {
307 wxDateTime dt;
308 if ( GetDateFromTextControl(dt, m_textCur) )
309 m_datePicker->SetValue(dt);
310 }
311
312 void 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 }
325 else
326 {
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");
331 }
332 }
333
334 void 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
341 #endif // wxUSE_DATEPICKCTRL