1 /////////////////////////////////////////////////////////////////////////////
2 // Program: wxWidgets Widgets Sample
4 // Purpose: Part of the widgets sample showing date picker
5 // Author: Dimitri Schoolwerth, Vadim Zeitlin
6 // Created: 27 Sep 2003
8 // Copyright: (c) 2003 wxWindows team
9 // Licence: wxWindows licence
10 /////////////////////////////////////////////////////////////////////////////
12 // ============================================================================
14 // ============================================================================
16 // ----------------------------------------------------------------------------
18 // ----------------------------------------------------------------------------
20 // for compilers that support precompilation, includes "wx/wx.h".
21 #include "wx/wxprec.h"
27 #if wxUSE_DATEPICKCTRL
29 // for all others, include the necessary headers
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"
44 #include "wx/datectrl.h"
45 #include "wx/dateevt.h"
49 #include "icons/datepick.xpm"
51 // ----------------------------------------------------------------------------
53 // ----------------------------------------------------------------------------
58 DatePickerPage_Reset
= wxID_HIGHEST
,
60 DatePickerPage_SetRange
,
64 // ----------------------------------------------------------------------------
65 // CheckBoxWidgetsPage
66 // ----------------------------------------------------------------------------
68 class DatePickerWidgetsPage
: public WidgetsPage
71 DatePickerWidgetsPage(WidgetsBookCtrl
*book
, wxImageList
*imaglist
);
72 virtual ~DatePickerWidgetsPage(){};
74 virtual wxControl
*GetWidget() const { return m_datePicker
; }
75 virtual void RecreateWidget() { CreateDatePicker(); }
77 // lazy creation of the content
78 virtual void CreateContent();
82 void OnDateChanged(wxDateEvent
& event
);
84 void OnButtonSet(wxCommandEvent
& event
);
85 void OnButtonSetRange(wxCommandEvent
& event
);
86 void OnButtonReset(wxCommandEvent
& event
);
88 // reset the date picker parameters
91 // (re)create the date picker
92 void CreateDatePicker();
97 // the checkbox itself and the sizer it is in
98 wxDatePickerCtrl
*m_datePicker
;
99 wxSizer
*m_sizerDatePicker
;
101 wxTextCtrl
*m_textCur
;
102 wxTextCtrl
*m_textMin
;
103 wxTextCtrl
*m_textMax
;
105 wxRadioBox
* m_radioKind
;
106 wxCheckBox
* m_chkStyleCentury
;
107 wxCheckBox
* m_chkStyleAllowNone
;
109 // the text entries for command parameters
110 wxTextCtrl
*m_textLabel
;
113 DECLARE_EVENT_TABLE()
114 DECLARE_WIDGETS_PAGE(DatePickerWidgetsPage
)
117 // ----------------------------------------------------------------------------
119 // ----------------------------------------------------------------------------
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
)
126 EVT_DATE_CHANGED(wxID_ANY
, DatePickerWidgetsPage::OnDateChanged
)
129 // ============================================================================
131 // ============================================================================
133 #if defined(__WXMSW__)
134 #define FAMILY_CTRLS NATIVE_CTRLS
136 #define FAMILY_CTRLS GENERIC_CTRLS
139 IMPLEMENT_WIDGETS_PAGE(DatePickerWidgetsPage
, wxT("DatePicker"),
140 FAMILY_CTRLS
| PICKER_CTRLS
143 DatePickerWidgetsPage::DatePickerWidgetsPage(WidgetsBookCtrl
*book
,
144 wxImageList
*imaglist
)
145 :WidgetsPage(book
, imaglist
, datepick_xpm
)
149 void DatePickerWidgetsPage::CreateContent()
151 wxSizer
*sizerTop
= new wxBoxSizer(wxHORIZONTAL
);
154 wxSizer
* const sizerLeft
= new wxBoxSizer(wxVERTICAL
);
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());
163 wxSizer
* const sizerStyle
= new wxStaticBoxSizer(wxVERTICAL
, this, "&Style");
164 m_chkStyleCentury
= CreateCheckBoxAndAddToSizer(sizerStyle
, "Show ¢ury");
165 m_chkStyleAllowNone
= CreateCheckBoxAndAddToSizer(sizerStyle
, "Allow &no value");
167 sizerLeft
->Add(sizerStyle
, wxSizerFlags().Expand().Border());
169 sizerLeft
->Add(new wxButton(this, DatePickerPage_Reset
, "&Recreate"),
170 wxSizerFlags().Centre().Border());
173 // middle pane: operations
174 wxSizer
* const sizerMiddle
= new wxBoxSizer(wxVERTICAL
);
175 sizerMiddle
->Add(CreateSizerWithTextAndButton
182 wxSizerFlags().Expand().Border());
184 m_textCur
->SetMinSize(wxSize(GetTextExtent(" 9999-99-99 ").x
, -1));
186 sizerMiddle
->AddSpacer(10);
188 sizerMiddle
->Add(CreateSizerWithTextAndLabel
194 wxSizerFlags().Expand().Border());
195 sizerMiddle
->Add(CreateSizerWithTextAndLabel
201 wxSizerFlags().Expand().Border());
202 sizerMiddle
->Add(new wxButton(this, DatePickerPage_SetRange
, "Set &range"),
203 wxSizerFlags().Centre().Border());
206 // right pane: control itself
207 wxSizer
*sizerRight
= new wxBoxSizer(wxHORIZONTAL
);
209 m_datePicker
= new wxDatePickerCtrl(this, DatePickerPage_Picker
);
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
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);
221 // final initializations
222 m_chkStyleCentury
->SetValue(true);
228 void DatePickerWidgetsPage::Reset()
230 const wxDateTime today
= wxDateTime::Today();
232 m_datePicker
->SetValue(today
);
233 m_textCur
->SetValue(today
.FormatISODate());
236 void DatePickerWidgetsPage::CreateDatePicker()
238 const wxDateTime value
= m_datePicker
->GetValue();
240 size_t count
= m_sizerDatePicker
->GetChildren().GetCount();
241 for ( size_t n
= 0; n
< count
; n
++ )
243 m_sizerDatePicker
->Remove(0);
249 switch ( m_radioKind
->GetSelection() )
252 style
= wxDP_DEFAULT
;
260 style
= wxDP_DROPDOWN
;
264 if ( m_chkStyleCentury
->GetValue() )
265 style
|= wxDP_SHOWCENTURY
;
266 if ( m_chkStyleAllowNone
->GetValue() )
267 style
|= wxDP_ALLOWNONE
;
269 m_datePicker
= new wxDatePickerCtrl(this, DatePickerPage_Picker
, value
,
270 wxDefaultPosition
, wxDefaultSize
,
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();
279 // ----------------------------------------------------------------------------
281 // ----------------------------------------------------------------------------
283 void DatePickerWidgetsPage::OnButtonReset(wxCommandEvent
& WXUNUSED(event
))
290 static bool GetDateFromTextControl(wxDateTime
& dt
, const wxTextCtrl
* text
)
292 const wxString
& value
= text
->GetValue();
293 if ( !value
.empty() )
295 wxString::const_iterator end
;
296 if ( !dt
.ParseDate(value
, &end
) || end
!= value
.end() )
298 wxLogError("Invalid date \"%s\"");
306 void DatePickerWidgetsPage::OnButtonSet(wxCommandEvent
& WXUNUSED(event
))
309 if ( GetDateFromTextControl(dt
, m_textCur
) )
310 m_datePicker
->SetValue(dt
);
313 void DatePickerWidgetsPage::OnButtonSetRange(wxCommandEvent
& WXUNUSED(event
))
316 if ( !GetDateFromTextControl(dt1
, m_textMin
) ||
317 !GetDateFromTextControl(dt2
, m_textMax
) )
320 m_datePicker
->SetRange(dt1
, dt2
);
322 if ( !m_datePicker
->GetRange(&dt1
, &dt2
) )
324 wxLogMessage("No range set");
328 m_textMin
->SetValue(dt1
.IsValid() ? dt1
.FormatISODate() : wxString());
329 m_textMax
->SetValue(dt2
.IsValid() ? dt2
.FormatISODate() : wxString());
331 wxLogMessage("Date picker range updated");
335 void DatePickerWidgetsPage::OnDateChanged(wxDateEvent
& event
)
337 wxLogMessage("Date changed, now is %s (control value is %s).",
338 event
.GetDate().FormatISOCombined(),
339 m_datePicker
->GetValue().FormatISOCombined());
342 #endif // wxUSE_DATEPICKCTRL