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