1 /////////////////////////////////////////////////////////////////////////////
2 // Program:     wxWidgets Widgets Sample
3 // Name:        timepick.cpp
4 // Purpose:     Part of the widgets sample showing time picker
5 // Author:      Vadim Zeitlin
6 // Created:     2011-12-20
7 // Copyright:   (c) 2011 wxWindows team
8 // Licence:     wxWindows licence
9 /////////////////////////////////////////////////////////////////////////////
10 
11 // ============================================================================
12 // declarations
13 // ============================================================================
14 
15 // ----------------------------------------------------------------------------
16 // headers
17 // ----------------------------------------------------------------------------
18 
19 // for compilers that support precompilation, includes "wx/wx.h".
20 #include "wx/wxprec.h"
21 
22 
23 #if wxUSE_TIMEPICKCTRL
24 
25 // for all others, include the necessary headers
26 #ifndef WX_PRECOMP
27     #include "wx/crt.h"
28     #include "wx/app.h"
29     #include "wx/log.h"
30 
31     #include "wx/button.h"
32     #include "wx/textctrl.h"
33 
34     #include "wx/sizer.h"
35 #endif
36 
37 #include "wx/timectrl.h"
38 #include "wx/dateevt.h"
39 
40 #include "widgets.h"
41 
42 #include "icons/timepick.xpm"
43 
44 // ----------------------------------------------------------------------------
45 // constants
46 // ----------------------------------------------------------------------------
47 
48 // control ids
49 enum
50 {
51     TimePickerPage_Reset = wxID_HIGHEST,
52     TimePickerPage_Set,
53     TimePickerPage_Picker
54 };
55 
56 // ----------------------------------------------------------------------------
57 // CheckBoxWidgetsPage
58 // ----------------------------------------------------------------------------
59 
60 class TimePickerWidgetsPage : public WidgetsPage
61 {
62 public:
63     TimePickerWidgetsPage(WidgetsBookCtrl *book, wxImageList *imaglist);
64 
GetWidget() const65     virtual wxWindow *GetWidget() const wxOVERRIDE { return m_timePicker; }
RecreateWidget()66     virtual void RecreateWidget() wxOVERRIDE { CreateTimePicker(); }
67 
68     // lazy creation of the content
69     virtual void CreateContent() wxOVERRIDE;
70 
71 protected:
72     // event handlers
73     void OnTimeChanged(wxDateEvent& event);
74 
75     void OnButtonSet(wxCommandEvent& event);
76     void OnButtonReset(wxCommandEvent& event);
77 
78     // reset the time picker parameters
79     void Reset();
80 
81     // (re)create the time picker
82     void CreateTimePicker();
83 
84     // the controls
85     // ------------
86 
87     // the checkbox itself and the sizer it is in
88     wxTimePickerCtrl *m_timePicker;
89     wxSizer *m_sizerTimePicker;
90 
91     wxTextCtrl *m_textCur;
92 
93 private:
94     wxDECLARE_EVENT_TABLE();
95     DECLARE_WIDGETS_PAGE(TimePickerWidgetsPage)
96 };
97 
98 // ----------------------------------------------------------------------------
99 // event tables
100 // ----------------------------------------------------------------------------
101 
102 wxBEGIN_EVENT_TABLE(TimePickerWidgetsPage, WidgetsPage)
103     EVT_BUTTON(TimePickerPage_Reset, TimePickerWidgetsPage::OnButtonReset)
104     EVT_BUTTON(TimePickerPage_Set, TimePickerWidgetsPage::OnButtonSet)
105 
106     EVT_TIME_CHANGED(wxID_ANY, TimePickerWidgetsPage::OnTimeChanged)
107 wxEND_EVENT_TABLE()
108 
109 // ============================================================================
110 // implementation
111 // ============================================================================
112 
113 #if defined(__WXMSW__)
114     #define FAMILY_CTRLS NATIVE_CTRLS
115 #else
116     #define FAMILY_CTRLS GENERIC_CTRLS
117 #endif
118 
119 IMPLEMENT_WIDGETS_PAGE(TimePickerWidgetsPage, "TimePicker",
120                        FAMILY_CTRLS | PICKER_CTRLS
121                        );
122 
TimePickerWidgetsPage(WidgetsBookCtrl * book,wxImageList * imaglist)123 TimePickerWidgetsPage::TimePickerWidgetsPage(WidgetsBookCtrl *book,
124                                          wxImageList *imaglist)
125                      : WidgetsPage(book, imaglist, timepick_xpm)
126 {
127 }
128 
CreateContent()129 void TimePickerWidgetsPage::CreateContent()
130 {
131     wxSizer *sizerTop = new wxBoxSizer(wxHORIZONTAL);
132 
133     // left pane
134     wxSizer* const sizerLeft = new wxBoxSizer(wxVERTICAL);
135 
136     sizerLeft->Add(new wxButton(this, TimePickerPage_Reset, "&Reset"),
137                    wxSizerFlags().Centre().Border());
138 
139 
140     // middle pane: operations
141     wxSizer* const sizerMiddle = new wxBoxSizer(wxVERTICAL);
142     sizerMiddle->Add(CreateSizerWithTextAndButton
143                      (
144                         TimePickerPage_Set,
145                         "&Set time",
146                         wxID_ANY,
147                         &m_textCur
148                      ),
149                      wxSizerFlags().Expand().Border());
150 
151     m_textCur->SetMinSize(wxSize(GetTextExtent("  99:99:99  ").x, -1));
152 
153 
154     // right pane: control itself
155     wxSizer *sizerRight = new wxBoxSizer(wxHORIZONTAL);
156 
157     m_timePicker = new wxTimePickerCtrl(this, TimePickerPage_Picker);
158 
159     sizerRight->Add(0, 0, 1, wxCENTRE);
160     sizerRight->Add(m_timePicker, 1, wxCENTRE);
161     sizerRight->Add(0, 0, 1, wxCENTRE);
162     m_sizerTimePicker = sizerRight; // save it to modify it later
163 
164     // the 3 panes panes compose the window
165     sizerTop->Add(sizerLeft, 0, (wxALL & ~wxLEFT), 10);
166     sizerTop->Add(sizerMiddle, 0, (wxTOP | wxBOTTOM), 10);
167     sizerTop->Add(sizerRight, 1, wxGROW | (wxALL & ~wxRIGHT), 10);
168 
169     // final initializations
170     Reset();
171 
172     SetSizer(sizerTop);
173 }
174 
Reset()175 void TimePickerWidgetsPage::Reset()
176 {
177     const wxDateTime today = wxDateTime::Today();
178 
179     m_timePicker->SetValue(today);
180     m_textCur->SetValue(today.FormatISOTime());
181 }
182 
CreateTimePicker()183 void TimePickerWidgetsPage::CreateTimePicker()
184 {
185     const wxDateTime value = m_timePicker->GetValue();
186 
187     size_t count = m_sizerTimePicker->GetChildren().GetCount();
188     for ( size_t n = 0; n < count; n++ )
189     {
190         m_sizerTimePicker->Remove(0);
191     }
192 
193     delete m_timePicker;
194 
195     long style = GetAttrs().m_defaultFlags;
196 
197     m_timePicker = new wxTimePickerCtrl(this, TimePickerPage_Picker, value,
198                                         wxDefaultPosition, wxDefaultSize,
199                                         style);
200 
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();
205 }
206 
207 // ----------------------------------------------------------------------------
208 // event handlers
209 // ----------------------------------------------------------------------------
210 
OnButtonReset(wxCommandEvent & WXUNUSED (event))211 void TimePickerWidgetsPage::OnButtonReset(wxCommandEvent& WXUNUSED(event))
212 {
213     Reset();
214 
215     CreateTimePicker();
216 }
217 
OnButtonSet(wxCommandEvent & WXUNUSED (event))218 void TimePickerWidgetsPage::OnButtonSet(wxCommandEvent& WXUNUSED(event))
219 {
220     int h, m, s;
221     if ( wxSscanf(m_textCur->GetValue(), "%d:%d:%d", &h, &m, &s) != 3 )
222     {
223         wxLogError("Invalid time, please use HH:MM:SS format.");
224         return;
225     }
226 
227     m_timePicker->SetTime(h, m, s);
228 }
229 
OnTimeChanged(wxDateEvent & event)230 void TimePickerWidgetsPage::OnTimeChanged(wxDateEvent& event)
231 {
232     int h, m, s;
233     m_timePicker->GetTime(&h, &m, &s);
234 
235     wxLogMessage("Time changed, now is %s (control value is %02d:%02d:%02d).",
236                  event.GetDate().FormatISOTime(), h, m, s);
237 }
238 
239 #endif // wxUSE_TIMEPICKCTRL
240