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