1 /////////////////////////////////////////////////////////////////////////////
2 // Program: wxWidgets Widgets Sample
4 // Purpose: Part of the widgets sample showing time picker
5 // Author: Vadim Zeitlin
8 // Copyright: (c) 2011 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_TIMEPICKCTRL
29 // for all others, include the necessary headers
35 #include "wx/button.h"
36 #include "wx/textctrl.h"
41 #include "wx/timectrl.h"
42 #include "wx/dateevt.h"
46 #include "icons/timepick.xpm"
48 // ----------------------------------------------------------------------------
50 // ----------------------------------------------------------------------------
55 TimePickerPage_Reset
= wxID_HIGHEST
,
60 // ----------------------------------------------------------------------------
61 // CheckBoxWidgetsPage
62 // ----------------------------------------------------------------------------
64 class TimePickerWidgetsPage
: public WidgetsPage
67 TimePickerWidgetsPage(WidgetsBookCtrl
*book
, wxImageList
*imaglist
);
68 virtual ~TimePickerWidgetsPage(){};
70 virtual wxControl
*GetWidget() const { return m_timePicker
; }
71 virtual void RecreateWidget() { CreateTimePicker(); }
73 // lazy creation of the content
74 virtual void CreateContent();
78 void OnTimeChanged(wxDateEvent
& event
);
80 void OnButtonSet(wxCommandEvent
& event
);
81 void OnButtonReset(wxCommandEvent
& event
);
83 // reset the time picker parameters
86 // (re)create the time picker
87 void CreateTimePicker();
92 // the checkbox itself and the sizer it is in
93 wxTimePickerCtrl
*m_timePicker
;
94 wxSizer
*m_sizerTimePicker
;
96 wxTextCtrl
*m_textCur
;
100 DECLARE_WIDGETS_PAGE(TimePickerWidgetsPage
)
103 // ----------------------------------------------------------------------------
105 // ----------------------------------------------------------------------------
107 BEGIN_EVENT_TABLE(TimePickerWidgetsPage
, WidgetsPage
)
108 EVT_BUTTON(TimePickerPage_Reset
, TimePickerWidgetsPage::OnButtonReset
)
109 EVT_BUTTON(TimePickerPage_Set
, TimePickerWidgetsPage::OnButtonSet
)
111 EVT_TIME_CHANGED(wxID_ANY
, TimePickerWidgetsPage::OnTimeChanged
)
114 // ============================================================================
116 // ============================================================================
118 #if defined(__WXMSW__)
119 #define FAMILY_CTRLS NATIVE_CTRLS
121 #define FAMILY_CTRLS GENERIC_CTRLS
124 IMPLEMENT_WIDGETS_PAGE(TimePickerWidgetsPage
, wxT("TimePicker"),
125 FAMILY_CTRLS
| PICKER_CTRLS
128 TimePickerWidgetsPage::TimePickerWidgetsPage(WidgetsBookCtrl
*book
,
129 wxImageList
*imaglist
)
130 : WidgetsPage(book
, imaglist
, timepick_xpm
)
134 void TimePickerWidgetsPage::CreateContent()
136 wxSizer
*sizerTop
= new wxBoxSizer(wxHORIZONTAL
);
139 wxSizer
* const sizerLeft
= new wxBoxSizer(wxVERTICAL
);
141 sizerLeft
->Add(new wxButton(this, TimePickerPage_Reset
, "&Reset"),
142 wxSizerFlags().Centre().Border());
145 // middle pane: operations
146 wxSizer
* const sizerMiddle
= new wxBoxSizer(wxVERTICAL
);
147 sizerMiddle
->Add(CreateSizerWithTextAndButton
154 wxSizerFlags().Expand().Border());
156 m_textCur
->SetMinSize(wxSize(GetTextExtent(" 99:99:99 ").x
, -1));
159 // right pane: control itself
160 wxSizer
*sizerRight
= new wxBoxSizer(wxHORIZONTAL
);
162 m_timePicker
= new wxTimePickerCtrl(this, TimePickerPage_Picker
);
164 sizerRight
->Add(0, 0, 1, wxCENTRE
);
165 sizerRight
->Add(m_timePicker
, 1, wxCENTRE
);
166 sizerRight
->Add(0, 0, 1, wxCENTRE
);
167 m_sizerTimePicker
= sizerRight
; // save it to modify it later
169 // the 3 panes panes compose the window
170 sizerTop
->Add(sizerLeft
, 0, (wxALL
& ~wxLEFT
), 10);
171 sizerTop
->Add(sizerMiddle
, 0, (wxTOP
| wxBOTTOM
), 10);
172 sizerTop
->Add(sizerRight
, 1, wxGROW
| (wxALL
& ~wxRIGHT
), 10);
174 // final initializations
180 void TimePickerWidgetsPage::Reset()
182 const wxDateTime today
= wxDateTime::Today();
184 m_timePicker
->SetValue(today
);
185 m_textCur
->SetValue(today
.FormatISOTime());
188 void TimePickerWidgetsPage::CreateTimePicker()
190 const wxDateTime value
= m_timePicker
->GetValue();
192 size_t count
= m_sizerTimePicker
->GetChildren().GetCount();
193 for ( size_t n
= 0; n
< count
; n
++ )
195 m_sizerTimePicker
->Remove(0);
200 m_timePicker
= new wxTimePickerCtrl(this, TimePickerPage_Picker
, value
);
202 m_sizerTimePicker
->Add(0, 0, 1, wxCENTRE
);
203 m_sizerTimePicker
->Add(m_timePicker
, 1, wxCENTRE
);
204 m_sizerTimePicker
->Add(0, 0, 1, wxCENTRE
);
205 m_sizerTimePicker
->Layout();
208 // ----------------------------------------------------------------------------
210 // ----------------------------------------------------------------------------
212 void TimePickerWidgetsPage::OnButtonReset(wxCommandEvent
& WXUNUSED(event
))
219 void TimePickerWidgetsPage::OnButtonSet(wxCommandEvent
& WXUNUSED(event
))
222 if ( wxSscanf(m_textCur
->GetValue(), "%d:%d:%d", &h
, &m
, &s
) != 3 )
224 wxLogError("Invalid time, please use HH:MM:SS format.");
228 m_timePicker
->SetTime(h
, m
, s
);
231 void TimePickerWidgetsPage::OnTimeChanged(wxDateEvent
& event
)
234 m_timePicker
->GetTime(&h
, &m
, &s
);
236 wxLogMessage("Time changed, now is %s (control value is %02d:%02d:%02d).",
237 event
.GetDate().FormatISOTime(), h
, m
, s
);
240 #endif // wxUSE_TIMEPICKCTRL