1 /////////////////////////////////////////////////////////////////////////////
2 // Program: wxWidgets Widgets Sample
4 // Purpose: Part of the widgets sample showing time picker
5 // Author: Vadim Zeitlin
7 // Copyright: (c) 2011 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_TIMEPICKCTRL
28 // for all others, include the necessary headers
34 #include "wx/button.h"
35 #include "wx/textctrl.h"
40 #include "wx/timectrl.h"
41 #include "wx/dateevt.h"
45 #include "icons/timepick.xpm"
47 // ----------------------------------------------------------------------------
49 // ----------------------------------------------------------------------------
54 TimePickerPage_Reset
= wxID_HIGHEST
,
59 // ----------------------------------------------------------------------------
60 // CheckBoxWidgetsPage
61 // ----------------------------------------------------------------------------
63 class TimePickerWidgetsPage
: public WidgetsPage
66 TimePickerWidgetsPage(WidgetsBookCtrl
*book
, wxImageList
*imaglist
);
67 virtual ~TimePickerWidgetsPage(){};
69 virtual wxControl
*GetWidget() const { return m_timePicker
; }
70 virtual void RecreateWidget() { CreateTimePicker(); }
72 // lazy creation of the content
73 virtual void CreateContent();
77 void OnTimeChanged(wxDateEvent
& event
);
79 void OnButtonSet(wxCommandEvent
& event
);
80 void OnButtonReset(wxCommandEvent
& event
);
82 // reset the time picker parameters
85 // (re)create the time picker
86 void CreateTimePicker();
91 // the checkbox itself and the sizer it is in
92 wxTimePickerCtrl
*m_timePicker
;
93 wxSizer
*m_sizerTimePicker
;
95 wxTextCtrl
*m_textCur
;
99 DECLARE_WIDGETS_PAGE(TimePickerWidgetsPage
)
102 // ----------------------------------------------------------------------------
104 // ----------------------------------------------------------------------------
106 BEGIN_EVENT_TABLE(TimePickerWidgetsPage
, WidgetsPage
)
107 EVT_BUTTON(TimePickerPage_Reset
, TimePickerWidgetsPage::OnButtonReset
)
108 EVT_BUTTON(TimePickerPage_Set
, TimePickerWidgetsPage::OnButtonSet
)
110 EVT_TIME_CHANGED(wxID_ANY
, TimePickerWidgetsPage::OnTimeChanged
)
113 // ============================================================================
115 // ============================================================================
117 #if defined(__WXMSW__)
118 #define FAMILY_CTRLS NATIVE_CTRLS
120 #define FAMILY_CTRLS GENERIC_CTRLS
123 IMPLEMENT_WIDGETS_PAGE(TimePickerWidgetsPage
, wxT("TimePicker"),
124 FAMILY_CTRLS
| PICKER_CTRLS
127 TimePickerWidgetsPage::TimePickerWidgetsPage(WidgetsBookCtrl
*book
,
128 wxImageList
*imaglist
)
129 : WidgetsPage(book
, imaglist
, timepick_xpm
)
133 void TimePickerWidgetsPage::CreateContent()
135 wxSizer
*sizerTop
= new wxBoxSizer(wxHORIZONTAL
);
138 wxSizer
* const sizerLeft
= new wxBoxSizer(wxVERTICAL
);
140 sizerLeft
->Add(new wxButton(this, TimePickerPage_Reset
, "&Reset"),
141 wxSizerFlags().Centre().Border());
144 // middle pane: operations
145 wxSizer
* const sizerMiddle
= new wxBoxSizer(wxVERTICAL
);
146 sizerMiddle
->Add(CreateSizerWithTextAndButton
153 wxSizerFlags().Expand().Border());
155 m_textCur
->SetMinSize(wxSize(GetTextExtent(" 99:99:99 ").x
, -1));
158 // right pane: control itself
159 wxSizer
*sizerRight
= new wxBoxSizer(wxHORIZONTAL
);
161 m_timePicker
= new wxTimePickerCtrl(this, TimePickerPage_Picker
);
163 sizerRight
->Add(0, 0, 1, wxCENTRE
);
164 sizerRight
->Add(m_timePicker
, 1, wxCENTRE
);
165 sizerRight
->Add(0, 0, 1, wxCENTRE
);
166 m_sizerTimePicker
= sizerRight
; // save it to modify it later
168 // the 3 panes panes compose the window
169 sizerTop
->Add(sizerLeft
, 0, (wxALL
& ~wxLEFT
), 10);
170 sizerTop
->Add(sizerMiddle
, 0, (wxTOP
| wxBOTTOM
), 10);
171 sizerTop
->Add(sizerRight
, 1, wxGROW
| (wxALL
& ~wxRIGHT
), 10);
173 // final initializations
179 void TimePickerWidgetsPage::Reset()
181 const wxDateTime today
= wxDateTime::Today();
183 m_timePicker
->SetValue(today
);
184 m_textCur
->SetValue(today
.FormatISOTime());
187 void TimePickerWidgetsPage::CreateTimePicker()
189 const wxDateTime value
= m_timePicker
->GetValue();
191 size_t count
= m_sizerTimePicker
->GetChildren().GetCount();
192 for ( size_t n
= 0; n
< count
; n
++ )
194 m_sizerTimePicker
->Remove(0);
199 m_timePicker
= new wxTimePickerCtrl(this, TimePickerPage_Picker
, value
);
201 m_sizerTimePicker
->Add(0, 0, 1, wxCENTRE
);
202 m_sizerTimePicker
->Add(m_timePicker
, 1, wxCENTRE
);
203 m_sizerTimePicker
->Add(0, 0, 1, wxCENTRE
);
204 m_sizerTimePicker
->Layout();
207 // ----------------------------------------------------------------------------
209 // ----------------------------------------------------------------------------
211 void TimePickerWidgetsPage::OnButtonReset(wxCommandEvent
& WXUNUSED(event
))
218 void TimePickerWidgetsPage::OnButtonSet(wxCommandEvent
& WXUNUSED(event
))
221 if ( wxSscanf(m_textCur
->GetValue(), "%d:%d:%d", &h
, &m
, &s
) != 3 )
223 wxLogError("Invalid time, please use HH:MM:SS format.");
227 m_timePicker
->SetTime(h
, m
, s
);
230 void TimePickerWidgetsPage::OnTimeChanged(wxDateEvent
& event
)
233 m_timePicker
->GetTime(&h
, &m
, &s
);
235 wxLogMessage("Time changed, now is %s (control value is %02d:%02d:%02d).",
236 event
.GetDate().FormatISOTime(), h
, m
, s
);
239 #endif // wxUSE_TIMEPICKCTRL