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
7 // Copyright: (c) 2003 wxWindows team
8 // Licence: wxWindows licence
9 /////////////////////////////////////////////////////////////////////////////
11 // ============================================================================
13 // ============================================================================
15 // ----------------------------------------------------------------------------
17 // ----------------------------------------------------------------------------
19 // for compilers that support precompilation, includes "wx/wx.h".
20 #include "wx/wxprec.h"
26 #if wxUSE_DATEPICKCTRL
28 // for all others, include the necessary headers
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"
43 #include "wx/datectrl.h"
44 #include "wx/dateevt.h"
48 #include "icons/datepick.xpm"
50 // ----------------------------------------------------------------------------
52 // ----------------------------------------------------------------------------
57 DatePickerPage_Reset
= wxID_HIGHEST
,
59 DatePickerPage_SetRange
,
63 // ----------------------------------------------------------------------------
64 // CheckBoxWidgetsPage
65 // ----------------------------------------------------------------------------
67 class DatePickerWidgetsPage
: public WidgetsPage
70 DatePickerWidgetsPage(WidgetsBookCtrl
*book
, wxImageList
*imaglist
);
71 virtual ~DatePickerWidgetsPage(){};
73 virtual wxControl
*GetWidget() const { return m_datePicker
; }
74 virtual void RecreateWidget() { CreateDatePicker(); }
76 // lazy creation of the content
77 virtual void CreateContent();
81 void OnDateChanged(wxDateEvent
& event
);
83 void OnButtonSet(wxCommandEvent
& event
);
84 void OnButtonSetRange(wxCommandEvent
& event
);
85 void OnButtonReset(wxCommandEvent
& event
);
87 // reset the date picker parameters
90 // (re)create the date picker
91 void CreateDatePicker();
96 // the checkbox itself and the sizer it is in
97 wxDatePickerCtrl
*m_datePicker
;
98 wxSizer
*m_sizerDatePicker
;
100 wxTextCtrl
*m_textCur
;
101 wxTextCtrl
*m_textMin
;
102 wxTextCtrl
*m_textMax
;
104 wxRadioBox
* m_radioKind
;
105 wxCheckBox
* m_chkStyleCentury
;
106 wxCheckBox
* m_chkStyleAllowNone
;
108 // the text entries for command parameters
109 wxTextCtrl
*m_textLabel
;
112 DECLARE_EVENT_TABLE()
113 DECLARE_WIDGETS_PAGE(DatePickerWidgetsPage
)
116 // ----------------------------------------------------------------------------
118 // ----------------------------------------------------------------------------
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
)
125 EVT_DATE_CHANGED(wxID_ANY
, DatePickerWidgetsPage::OnDateChanged
)
128 // ============================================================================
130 // ============================================================================
132 #if defined(__WXMSW__)
133 #define FAMILY_CTRLS NATIVE_CTRLS
135 #define FAMILY_CTRLS GENERIC_CTRLS
138 IMPLEMENT_WIDGETS_PAGE(DatePickerWidgetsPage
, wxT("DatePicker"),
139 FAMILY_CTRLS
| PICKER_CTRLS
142 DatePickerWidgetsPage::DatePickerWidgetsPage(WidgetsBookCtrl
*book
,
143 wxImageList
*imaglist
)
144 :WidgetsPage(book
, imaglist
, datepick_xpm
)
148 void DatePickerWidgetsPage::CreateContent()
150 wxSizer
*sizerTop
= new wxBoxSizer(wxHORIZONTAL
);
153 wxSizer
* const sizerLeft
= new wxBoxSizer(wxVERTICAL
);
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());
162 wxSizer
* const sizerStyle
= new wxStaticBoxSizer(wxVERTICAL
, this, "&Style");
163 m_chkStyleCentury
= CreateCheckBoxAndAddToSizer(sizerStyle
, "Show ¢ury");
164 m_chkStyleAllowNone
= CreateCheckBoxAndAddToSizer(sizerStyle
, "Allow &no value");
166 sizerLeft
->Add(sizerStyle
, wxSizerFlags().Expand().Border());
168 sizerLeft
->Add(new wxButton(this, DatePickerPage_Reset
, "&Recreate"),
169 wxSizerFlags().Centre().Border());
172 // middle pane: operations
173 wxSizer
* const sizerMiddle
= new wxBoxSizer(wxVERTICAL
);
174 sizerMiddle
->Add(CreateSizerWithTextAndButton
181 wxSizerFlags().Expand().Border());
183 m_textCur
->SetMinSize(wxSize(GetTextExtent(" 9999-99-99 ").x
, -1));
185 sizerMiddle
->AddSpacer(10);
187 sizerMiddle
->Add(CreateSizerWithTextAndLabel
193 wxSizerFlags().Expand().Border());
194 sizerMiddle
->Add(CreateSizerWithTextAndLabel
200 wxSizerFlags().Expand().Border());
201 sizerMiddle
->Add(new wxButton(this, DatePickerPage_SetRange
, "Set &range"),
202 wxSizerFlags().Centre().Border());
205 // right pane: control itself
206 wxSizer
*sizerRight
= new wxBoxSizer(wxHORIZONTAL
);
208 m_datePicker
= new wxDatePickerCtrl(this, DatePickerPage_Picker
);
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
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);
220 // final initializations
221 m_chkStyleCentury
->SetValue(true);
227 void DatePickerWidgetsPage::Reset()
229 const wxDateTime today
= wxDateTime::Today();
231 m_datePicker
->SetValue(today
);
232 m_textCur
->SetValue(today
.FormatISODate());
235 void DatePickerWidgetsPage::CreateDatePicker()
237 const wxDateTime value
= m_datePicker
->GetValue();
239 size_t count
= m_sizerDatePicker
->GetChildren().GetCount();
240 for ( size_t n
= 0; n
< count
; n
++ )
242 m_sizerDatePicker
->Remove(0);
248 switch ( m_radioKind
->GetSelection() )
251 style
= wxDP_DEFAULT
;
259 style
= wxDP_DROPDOWN
;
263 if ( m_chkStyleCentury
->GetValue() )
264 style
|= wxDP_SHOWCENTURY
;
265 if ( m_chkStyleAllowNone
->GetValue() )
266 style
|= wxDP_ALLOWNONE
;
268 m_datePicker
= new wxDatePickerCtrl(this, DatePickerPage_Picker
, value
,
269 wxDefaultPosition
, wxDefaultSize
,
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();
278 // ----------------------------------------------------------------------------
280 // ----------------------------------------------------------------------------
282 void DatePickerWidgetsPage::OnButtonReset(wxCommandEvent
& WXUNUSED(event
))
289 static bool GetDateFromTextControl(wxDateTime
& dt
, const wxTextCtrl
* text
)
291 const wxString
& value
= text
->GetValue();
292 if ( !value
.empty() )
294 wxString::const_iterator end
;
295 if ( !dt
.ParseDate(value
, &end
) || end
!= value
.end() )
297 wxLogError("Invalid date \"%s\"");
305 void DatePickerWidgetsPage::OnButtonSet(wxCommandEvent
& WXUNUSED(event
))
308 if ( GetDateFromTextControl(dt
, m_textCur
) )
309 m_datePicker
->SetValue(dt
);
312 void DatePickerWidgetsPage::OnButtonSetRange(wxCommandEvent
& WXUNUSED(event
))
315 if ( !GetDateFromTextControl(dt1
, m_textMin
) ||
316 !GetDateFromTextControl(dt2
, m_textMax
) )
319 m_datePicker
->SetRange(dt1
, dt2
);
321 if ( !m_datePicker
->GetRange(&dt1
, &dt2
) )
323 wxLogMessage("No range set");
327 m_textMin
->SetValue(dt1
.IsValid() ? dt1
.FormatISODate() : wxString());
328 m_textMax
->SetValue(dt2
.IsValid() ? dt2
.FormatISODate() : wxString());
330 wxLogMessage("Date picker range updated");
334 void DatePickerWidgetsPage::OnDateChanged(wxDateEvent
& event
)
336 wxLogMessage("Date changed, now is %s (control value is %s).",
337 event
.GetDate().FormatISOCombined(),
338 m_datePicker
->GetValue().FormatISOCombined());
341 #endif // wxUSE_DATEPICKCTRL