]> git.saurik.com Git - wxWidgets.git/blob - samples/widgets/timepick.cpp
Don't generate any events from wxSpinCtrl and wxSpinCtrlDouble methods.
[wxWidgets.git] / samples / widgets / timepick.cpp
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 #ifdef __BORLANDC__
23 #pragma hdrstop
24 #endif
25
26 #if wxUSE_TIMEPICKCTRL
27
28 // for all others, include the necessary headers
29 #ifndef WX_PRECOMP
30 #include "wx/crt.h"
31 #include "wx/app.h"
32 #include "wx/log.h"
33
34 #include "wx/button.h"
35 #include "wx/textctrl.h"
36
37 #include "wx/sizer.h"
38 #endif
39
40 #include "wx/timectrl.h"
41 #include "wx/dateevt.h"
42
43 #include "widgets.h"
44
45 #include "icons/timepick.xpm"
46
47 // ----------------------------------------------------------------------------
48 // constants
49 // ----------------------------------------------------------------------------
50
51 // control ids
52 enum
53 {
54 TimePickerPage_Reset = wxID_HIGHEST,
55 TimePickerPage_Set,
56 TimePickerPage_Picker
57 };
58
59 // ----------------------------------------------------------------------------
60 // CheckBoxWidgetsPage
61 // ----------------------------------------------------------------------------
62
63 class TimePickerWidgetsPage : public WidgetsPage
64 {
65 public:
66 TimePickerWidgetsPage(WidgetsBookCtrl *book, wxImageList *imaglist);
67 virtual ~TimePickerWidgetsPage(){};
68
69 virtual wxControl *GetWidget() const { return m_timePicker; }
70 virtual void RecreateWidget() { CreateTimePicker(); }
71
72 // lazy creation of the content
73 virtual void CreateContent();
74
75 protected:
76 // event handlers
77 void OnTimeChanged(wxDateEvent& event);
78
79 void OnButtonSet(wxCommandEvent& event);
80 void OnButtonReset(wxCommandEvent& event);
81
82 // reset the time picker parameters
83 void Reset();
84
85 // (re)create the time picker
86 void CreateTimePicker();
87
88 // the controls
89 // ------------
90
91 // the checkbox itself and the sizer it is in
92 wxTimePickerCtrl *m_timePicker;
93 wxSizer *m_sizerTimePicker;
94
95 wxTextCtrl *m_textCur;
96
97 private:
98 DECLARE_EVENT_TABLE()
99 DECLARE_WIDGETS_PAGE(TimePickerWidgetsPage)
100 };
101
102 // ----------------------------------------------------------------------------
103 // event tables
104 // ----------------------------------------------------------------------------
105
106 BEGIN_EVENT_TABLE(TimePickerWidgetsPage, WidgetsPage)
107 EVT_BUTTON(TimePickerPage_Reset, TimePickerWidgetsPage::OnButtonReset)
108 EVT_BUTTON(TimePickerPage_Set, TimePickerWidgetsPage::OnButtonSet)
109
110 EVT_TIME_CHANGED(wxID_ANY, TimePickerWidgetsPage::OnTimeChanged)
111 END_EVENT_TABLE()
112
113 // ============================================================================
114 // implementation
115 // ============================================================================
116
117 #if defined(__WXMSW__)
118 #define FAMILY_CTRLS NATIVE_CTRLS
119 #else
120 #define FAMILY_CTRLS GENERIC_CTRLS
121 #endif
122
123 IMPLEMENT_WIDGETS_PAGE(TimePickerWidgetsPage, wxT("TimePicker"),
124 FAMILY_CTRLS | PICKER_CTRLS
125 );
126
127 TimePickerWidgetsPage::TimePickerWidgetsPage(WidgetsBookCtrl *book,
128 wxImageList *imaglist)
129 : WidgetsPage(book, imaglist, timepick_xpm)
130 {
131 }
132
133 void TimePickerWidgetsPage::CreateContent()
134 {
135 wxSizer *sizerTop = new wxBoxSizer(wxHORIZONTAL);
136
137 // left pane
138 wxSizer* const sizerLeft = new wxBoxSizer(wxVERTICAL);
139
140 sizerLeft->Add(new wxButton(this, TimePickerPage_Reset, "&Reset"),
141 wxSizerFlags().Centre().Border());
142
143
144 // middle pane: operations
145 wxSizer* const sizerMiddle = new wxBoxSizer(wxVERTICAL);
146 sizerMiddle->Add(CreateSizerWithTextAndButton
147 (
148 TimePickerPage_Set,
149 "&Set time",
150 wxID_ANY,
151 &m_textCur
152 ),
153 wxSizerFlags().Expand().Border());
154
155 m_textCur->SetMinSize(wxSize(GetTextExtent(" 99:99:99 ").x, -1));
156
157
158 // right pane: control itself
159 wxSizer *sizerRight = new wxBoxSizer(wxHORIZONTAL);
160
161 m_timePicker = new wxTimePickerCtrl(this, TimePickerPage_Picker);
162
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
167
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);
172
173 // final initializations
174 Reset();
175
176 SetSizer(sizerTop);
177 }
178
179 void TimePickerWidgetsPage::Reset()
180 {
181 const wxDateTime today = wxDateTime::Today();
182
183 m_timePicker->SetValue(today);
184 m_textCur->SetValue(today.FormatISOTime());
185 }
186
187 void TimePickerWidgetsPage::CreateTimePicker()
188 {
189 const wxDateTime value = m_timePicker->GetValue();
190
191 size_t count = m_sizerTimePicker->GetChildren().GetCount();
192 for ( size_t n = 0; n < count; n++ )
193 {
194 m_sizerTimePicker->Remove(0);
195 }
196
197 delete m_timePicker;
198
199 m_timePicker = new wxTimePickerCtrl(this, TimePickerPage_Picker, value);
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
211 void TimePickerWidgetsPage::OnButtonReset(wxCommandEvent& WXUNUSED(event))
212 {
213 Reset();
214
215 CreateTimePicker();
216 }
217
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
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