]>
Commit | Line | Data |
---|---|---|
32b8ec41 | 1 | ///////////////////////////////////////////////////////////////////////////// |
be5a51fb | 2 | // Program: wxWidgets Widgets Sample |
32b8ec41 VZ |
3 | // Name: spinbtn.cpp |
4 | // Purpose: Part of the widgets sample showing wxSpinButton | |
5 | // Author: Vadim Zeitlin | |
6 | // Created: 16.04.01 | |
7 | // Id: $Id$ | |
8 | // Copyright: (c) 2001 Vadim Zeitlin | |
9 | // License: wxWindows license | |
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 | ||
61c083e7 WS |
27 | #if wxUSE_SPINBTN |
28 | ||
32b8ec41 VZ |
29 | // for all others, include the necessary headers |
30 | #ifndef WX_PRECOMP | |
31 | #include "wx/log.h" | |
32 | ||
3bb70c40 | 33 | #include "wx/bitmap.h" |
32b8ec41 VZ |
34 | #include "wx/button.h" |
35 | #include "wx/checkbox.h" | |
36 | #include "wx/radiobox.h" | |
37 | #include "wx/statbox.h" | |
38 | #include "wx/textctrl.h" | |
39 | #endif | |
40 | ||
41 | #include "wx/spinbutt.h" | |
42 | #include "wx/spinctrl.h" | |
43 | ||
44 | #include "wx/sizer.h" | |
45 | ||
46 | #include "widgets.h" | |
61c083e7 | 47 | |
32b8ec41 VZ |
48 | #include "icons/spinbtn.xpm" |
49 | ||
50 | // ---------------------------------------------------------------------------- | |
51 | // constants | |
52 | // ---------------------------------------------------------------------------- | |
53 | ||
54 | // control ids | |
55 | enum | |
56 | { | |
f0fa4312 | 57 | SpinBtnPage_Reset = wxID_HIGHEST, |
32b8ec41 VZ |
58 | SpinBtnPage_Clear, |
59 | SpinBtnPage_SetValue, | |
60 | SpinBtnPage_SetMinAndMax, | |
61 | SpinBtnPage_CurValueText, | |
62 | SpinBtnPage_ValueText, | |
63 | SpinBtnPage_MinText, | |
64 | SpinBtnPage_MaxText, | |
65 | SpinBtnPage_SpinBtn, | |
66 | SpinBtnPage_SpinCtrl | |
67 | }; | |
68 | ||
69 | // ---------------------------------------------------------------------------- | |
70 | // SpinBtnWidgetsPage | |
71 | // ---------------------------------------------------------------------------- | |
72 | ||
73 | class SpinBtnWidgetsPage : public WidgetsPage | |
74 | { | |
75 | public: | |
f2fdc4d5 | 76 | SpinBtnWidgetsPage(WidgetsBookCtrl *book, wxImageList *imaglist); |
8f6eaec9 | 77 | virtual ~SpinBtnWidgetsPage(){}; |
32b8ec41 | 78 | |
195df7a7 | 79 | virtual wxControl *GetWidget() const { return m_spinbtn; } |
1c01dd16 | 80 | virtual wxControl *GetWidget2() const { return m_spinctrl; } |
1301e228 | 81 | virtual void RecreateWidget() { CreateSpin(); } |
195df7a7 | 82 | |
453535a7 WS |
83 | // lazy creation of the content |
84 | virtual void CreateContent(); | |
85 | ||
32b8ec41 VZ |
86 | protected: |
87 | // event handlers | |
88 | void OnButtonReset(wxCommandEvent& event); | |
89 | void OnButtonClear(wxCommandEvent& event); | |
90 | void OnButtonSetValue(wxCommandEvent& event); | |
91 | void OnButtonSetMinAndMax(wxCommandEvent& event); | |
92 | ||
93 | void OnCheckOrRadioBox(wxCommandEvent& event); | |
94 | ||
0a3d26b8 VZ |
95 | void OnSpinBtn(wxSpinEvent& event); |
96 | void OnSpinBtnUp(wxSpinEvent& event); | |
97 | void OnSpinBtnDown(wxSpinEvent& event); | |
98 | void OnSpinCtrl(wxSpinEvent& event); | |
6ceb105f | 99 | void OnSpinText(wxCommandEvent& event); |
32b8ec41 VZ |
100 | |
101 | void OnUpdateUIValueButton(wxUpdateUIEvent& event); | |
102 | void OnUpdateUIMinMaxButton(wxUpdateUIEvent& event); | |
103 | ||
104 | void OnUpdateUIResetButton(wxUpdateUIEvent& event); | |
105 | ||
106 | void OnUpdateUICurValueText(wxUpdateUIEvent& event); | |
107 | ||
108 | // reset the spinbtn parameters | |
109 | void Reset(); | |
110 | ||
111 | // (re)create the spinbtn | |
112 | void CreateSpin(); | |
113 | ||
114 | // is this spinbtn value in range? | |
115 | bool IsValidValue(int val) const | |
116 | { return (val >= m_min) && (val <= m_max); } | |
117 | ||
118 | // the spinbtn range | |
119 | int m_min, m_max; | |
120 | ||
121 | // the controls | |
122 | // ------------ | |
123 | ||
124 | // the check/radio boxes for styles | |
125 | wxCheckBox *m_chkVert, | |
126 | *m_chkWrap; | |
127 | ||
128 | // the spinbtn and the spinctrl and the sizer containing them | |
129 | wxSpinButton *m_spinbtn; | |
130 | wxSpinCtrl *m_spinctrl; | |
131 | ||
132 | wxSizer *m_sizerSpin; | |
133 | ||
134 | // the text entries for set value/range | |
135 | wxTextCtrl *m_textValue, | |
136 | *m_textMin, | |
137 | *m_textMax; | |
138 | ||
139 | private: | |
5e173f35 GD |
140 | DECLARE_EVENT_TABLE() |
141 | DECLARE_WIDGETS_PAGE(SpinBtnWidgetsPage) | |
32b8ec41 VZ |
142 | }; |
143 | ||
144 | // ---------------------------------------------------------------------------- | |
145 | // event tables | |
146 | // ---------------------------------------------------------------------------- | |
147 | ||
148 | BEGIN_EVENT_TABLE(SpinBtnWidgetsPage, WidgetsPage) | |
149 | EVT_BUTTON(SpinBtnPage_Reset, SpinBtnWidgetsPage::OnButtonReset) | |
150 | EVT_BUTTON(SpinBtnPage_SetValue, SpinBtnWidgetsPage::OnButtonSetValue) | |
151 | EVT_BUTTON(SpinBtnPage_SetMinAndMax, SpinBtnWidgetsPage::OnButtonSetMinAndMax) | |
152 | ||
153 | EVT_UPDATE_UI(SpinBtnPage_SetValue, SpinBtnWidgetsPage::OnUpdateUIValueButton) | |
154 | EVT_UPDATE_UI(SpinBtnPage_SetMinAndMax, SpinBtnWidgetsPage::OnUpdateUIMinMaxButton) | |
155 | ||
156 | EVT_UPDATE_UI(SpinBtnPage_Reset, SpinBtnWidgetsPage::OnUpdateUIResetButton) | |
157 | ||
158 | EVT_UPDATE_UI(SpinBtnPage_CurValueText, SpinBtnWidgetsPage::OnUpdateUICurValueText) | |
159 | ||
160 | EVT_SPIN(SpinBtnPage_SpinBtn, SpinBtnWidgetsPage::OnSpinBtn) | |
161 | EVT_SPIN_UP(SpinBtnPage_SpinBtn, SpinBtnWidgetsPage::OnSpinBtnUp) | |
162 | EVT_SPIN_DOWN(SpinBtnPage_SpinBtn, SpinBtnWidgetsPage::OnSpinBtnDown) | |
163 | EVT_SPINCTRL(SpinBtnPage_SpinCtrl, SpinBtnWidgetsPage::OnSpinCtrl) | |
6ceb105f | 164 | EVT_TEXT(SpinBtnPage_SpinCtrl, SpinBtnWidgetsPage::OnSpinText) |
32b8ec41 | 165 | |
206d3a16 JS |
166 | EVT_CHECKBOX(wxID_ANY, SpinBtnWidgetsPage::OnCheckOrRadioBox) |
167 | EVT_RADIOBOX(wxID_ANY, SpinBtnWidgetsPage::OnCheckOrRadioBox) | |
32b8ec41 VZ |
168 | END_EVENT_TABLE() |
169 | ||
170 | // ============================================================================ | |
171 | // implementation | |
172 | // ============================================================================ | |
173 | ||
f0fa4312 WS |
174 | #if defined(__WXUNIVERSAL__) |
175 | #define FAMILY_CTRLS UNIVERSAL_CTRLS | |
176 | #else | |
177 | #define FAMILY_CTRLS NATIVE_CTRLS | |
178 | #endif | |
179 | ||
f2fdc4d5 | 180 | IMPLEMENT_WIDGETS_PAGE(SpinBtnWidgetsPage, _T("Spin"), |
f0fa4312 | 181 | FAMILY_CTRLS | EDITABLE_CTRLS |
f2fdc4d5 | 182 | ); |
32b8ec41 | 183 | |
f2fdc4d5 | 184 | SpinBtnWidgetsPage::SpinBtnWidgetsPage(WidgetsBookCtrl *book, |
32b8ec41 | 185 | wxImageList *imaglist) |
261357eb | 186 | : WidgetsPage(book, imaglist, spinbtn_xpm) |
32b8ec41 | 187 | { |
59b9edf0 JS |
188 | m_chkVert = NULL; |
189 | m_chkWrap = NULL; | |
190 | m_spinbtn = NULL; | |
191 | m_spinctrl = NULL; | |
192 | m_textValue = NULL; | |
193 | m_textMin = NULL; | |
194 | m_textMax = NULL; | |
32b8ec41 VZ |
195 | |
196 | // init everything | |
197 | m_min = 0; | |
198 | m_max = 10; | |
199 | ||
200 | m_chkVert = | |
201 | m_chkWrap = (wxCheckBox *)NULL; | |
202 | ||
203 | m_spinbtn = (wxSpinButton *)NULL; | |
204 | m_sizerSpin = (wxSizer *)NULL; | |
453535a7 | 205 | } |
32b8ec41 | 206 | |
453535a7 WS |
207 | void SpinBtnWidgetsPage::CreateContent() |
208 | { | |
32b8ec41 VZ |
209 | wxSizer *sizerTop = new wxBoxSizer(wxHORIZONTAL); |
210 | ||
211 | // left pane | |
206d3a16 | 212 | wxStaticBox *box = new wxStaticBox(this, wxID_ANY, _T("&Set style")); |
32b8ec41 VZ |
213 | wxSizer *sizerLeft = new wxStaticBoxSizer(box, wxVERTICAL); |
214 | ||
215 | m_chkVert = CreateCheckBoxAndAddToSizer(sizerLeft, _T("&Vertical")); | |
216 | m_chkWrap = CreateCheckBoxAndAddToSizer(sizerLeft, _T("&Wrap")); | |
217 | ||
218 | sizerLeft->Add(5, 5, 0, wxGROW | wxALL, 5); // spacer | |
219 | ||
220 | wxButton *btn = new wxButton(this, SpinBtnPage_Reset, _T("&Reset")); | |
221 | sizerLeft->Add(btn, 0, wxALIGN_CENTRE_HORIZONTAL | wxALL, 15); | |
222 | ||
223 | // middle pane | |
206d3a16 JS |
224 | wxStaticBox *box2 = new wxStaticBox(this, wxID_ANY, |
225 | _T("&Change spinbtn value")); | |
226 | ||
32b8ec41 VZ |
227 | wxSizer *sizerMiddle = new wxStaticBoxSizer(box2, wxVERTICAL); |
228 | ||
229 | wxTextCtrl *text; | |
230 | wxSizer *sizerRow = CreateSizerWithTextAndLabel(_T("Current value"), | |
231 | SpinBtnPage_CurValueText, | |
232 | &text); | |
206d3a16 | 233 | text->SetEditable(false); |
32b8ec41 VZ |
234 | |
235 | sizerMiddle->Add(sizerRow, 0, wxALL | wxGROW, 5); | |
236 | ||
237 | sizerRow = CreateSizerWithTextAndButton(SpinBtnPage_SetValue, | |
238 | _T("Set &value"), | |
239 | SpinBtnPage_ValueText, | |
240 | &m_textValue); | |
241 | sizerMiddle->Add(sizerRow, 0, wxALL | wxGROW, 5); | |
242 | ||
243 | sizerRow = CreateSizerWithTextAndButton(SpinBtnPage_SetMinAndMax, | |
244 | _T("&Min and max"), | |
245 | SpinBtnPage_MinText, | |
246 | &m_textMin); | |
247 | ||
206d3a16 | 248 | m_textMax = new wxTextCtrl(this, SpinBtnPage_MaxText, wxEmptyString); |
32b8ec41 VZ |
249 | sizerRow->Add(m_textMax, 1, wxLEFT | wxALIGN_CENTRE_VERTICAL, 5); |
250 | ||
aec18ff7 MB |
251 | m_textMin->SetValue( wxString::Format(_T("%d"), m_min) ); |
252 | m_textMax->SetValue( wxString::Format(_T("%d"), m_max) ); | |
32b8ec41 VZ |
253 | |
254 | sizerMiddle->Add(sizerRow, 0, wxALL | wxGROW, 5); | |
255 | ||
256 | // right pane | |
257 | wxSizer *sizerRight = new wxBoxSizer(wxVERTICAL); | |
7b127900 | 258 | sizerRight->SetMinSize(150, 0); |
32b8ec41 VZ |
259 | m_sizerSpin = sizerRight; // save it to modify it later |
260 | ||
261 | Reset(); | |
262 | CreateSpin(); | |
263 | ||
264 | // the 3 panes panes compose the window | |
265 | sizerTop->Add(sizerLeft, 0, wxGROW | (wxALL & ~wxLEFT), 10); | |
7b127900 | 266 | sizerTop->Add(sizerMiddle, 0, wxGROW | wxALL, 10); |
32b8ec41 VZ |
267 | sizerTop->Add(sizerRight, 1, wxGROW | (wxALL & ~wxRIGHT), 10); |
268 | ||
269 | // final initializations | |
32b8ec41 | 270 | SetSizer(sizerTop); |
32b8ec41 VZ |
271 | } |
272 | ||
32b8ec41 VZ |
273 | // ---------------------------------------------------------------------------- |
274 | // operations | |
275 | // ---------------------------------------------------------------------------- | |
276 | ||
277 | void SpinBtnWidgetsPage::Reset() | |
278 | { | |
206d3a16 JS |
279 | m_chkVert->SetValue(true); |
280 | m_chkWrap->SetValue(false); | |
32b8ec41 VZ |
281 | } |
282 | ||
283 | void SpinBtnWidgetsPage::CreateSpin() | |
284 | { | |
1301e228 | 285 | int flags = ms_defaultFlags; |
32b8ec41 VZ |
286 | |
287 | bool isVert = m_chkVert->GetValue(); | |
288 | if ( isVert ) | |
289 | flags |= wxSP_VERTICAL; | |
290 | else | |
291 | flags |= wxSP_HORIZONTAL; | |
292 | ||
293 | if ( m_chkWrap->GetValue() ) | |
294 | flags |= wxSP_WRAP; | |
295 | ||
296 | int val = m_min; | |
297 | if ( m_spinbtn ) | |
298 | { | |
299 | int valOld = m_spinbtn->GetValue(); | |
300 | if ( !IsValidValue(valOld) ) | |
301 | { | |
302 | val = valOld; | |
303 | } | |
304 | ||
12a3f227 RL |
305 | m_sizerSpin->Detach( m_spinbtn ); |
306 | m_sizerSpin->Detach( m_spinctrl ); | |
32b8ec41 VZ |
307 | |
308 | // there are 3 spacers left | |
9dd96c0f VZ |
309 | m_sizerSpin->Remove( 0 ); |
310 | m_sizerSpin->Remove( 0 ); | |
311 | m_sizerSpin->Remove( 0 ); | |
32b8ec41 VZ |
312 | |
313 | delete m_spinbtn; | |
314 | delete m_spinctrl; | |
315 | } | |
316 | ||
317 | m_spinbtn = new wxSpinButton(this, SpinBtnPage_SpinBtn, | |
318 | wxDefaultPosition, wxDefaultSize, | |
319 | flags); | |
320 | ||
321 | m_spinbtn->SetValue(val); | |
322 | m_spinbtn->SetRange(m_min, m_max); | |
323 | ||
324 | m_spinctrl = new wxSpinCtrl(this, SpinBtnPage_SpinCtrl, | |
325 | wxString::Format(_T("%d"), val), | |
326 | wxDefaultPosition, wxDefaultSize, | |
327 | flags, | |
328 | m_min, m_max, val); | |
329 | ||
330 | m_sizerSpin->Add(0, 0, 1); | |
331 | m_sizerSpin->Add(m_spinbtn, 0, wxALIGN_CENTRE | wxALL, 5); | |
332 | m_sizerSpin->Add(0, 0, 1); | |
333 | m_sizerSpin->Add(m_spinctrl, 0, wxALIGN_CENTRE | wxALL, 5); | |
334 | m_sizerSpin->Add(0, 0, 1); | |
335 | ||
336 | m_sizerSpin->Layout(); | |
337 | } | |
338 | ||
339 | // ---------------------------------------------------------------------------- | |
340 | // event handlers | |
341 | // ---------------------------------------------------------------------------- | |
342 | ||
343 | void SpinBtnWidgetsPage::OnButtonReset(wxCommandEvent& WXUNUSED(event)) | |
344 | { | |
345 | Reset(); | |
346 | ||
347 | CreateSpin(); | |
348 | } | |
349 | ||
350 | void SpinBtnWidgetsPage::OnButtonSetMinAndMax(wxCommandEvent& WXUNUSED(event)) | |
351 | { | |
352 | long minNew, | |
353 | maxNew = 0; // init to suppress compiler warning | |
354 | if ( !m_textMin->GetValue().ToLong(&minNew) || | |
355 | !m_textMax->GetValue().ToLong(&maxNew) || | |
6dd21271 | 356 | minNew > maxNew ) |
32b8ec41 VZ |
357 | { |
358 | wxLogWarning(_T("Invalid min/max values for the spinbtn.")); | |
359 | ||
360 | return; | |
361 | } | |
362 | ||
363 | m_min = minNew; | |
364 | m_max = maxNew; | |
365 | ||
366 | m_spinbtn->SetRange(minNew, maxNew); | |
367 | m_spinctrl->SetRange(minNew, maxNew); | |
368 | } | |
369 | ||
370 | void SpinBtnWidgetsPage::OnButtonSetValue(wxCommandEvent& WXUNUSED(event)) | |
371 | { | |
372 | long val; | |
373 | if ( !m_textValue->GetValue().ToLong(&val) || !IsValidValue(val) ) | |
374 | { | |
375 | wxLogWarning(_T("Invalid spinbtn value.")); | |
376 | ||
377 | return; | |
378 | } | |
379 | ||
380 | m_spinbtn->SetValue(val); | |
381 | m_spinctrl->SetValue(val); | |
382 | } | |
383 | ||
384 | void SpinBtnWidgetsPage::OnUpdateUIValueButton(wxUpdateUIEvent& event) | |
385 | { | |
386 | long val; | |
387 | event.Enable( m_textValue->GetValue().ToLong(&val) && IsValidValue(val) ); | |
388 | } | |
389 | ||
390 | void SpinBtnWidgetsPage::OnUpdateUIMinMaxButton(wxUpdateUIEvent& event) | |
391 | { | |
392 | long mn, mx; | |
393 | event.Enable( m_textMin->GetValue().ToLong(&mn) && | |
394 | m_textMax->GetValue().ToLong(&mx) && | |
6dd21271 | 395 | mn <= mx); |
32b8ec41 VZ |
396 | } |
397 | ||
398 | void SpinBtnWidgetsPage::OnUpdateUIResetButton(wxUpdateUIEvent& event) | |
399 | { | |
400 | event.Enable( !m_chkVert->GetValue() || m_chkWrap->GetValue() ); | |
401 | } | |
402 | ||
c02e5a31 | 403 | void SpinBtnWidgetsPage::OnCheckOrRadioBox(wxCommandEvent& WXUNUSED(event)) |
32b8ec41 VZ |
404 | { |
405 | CreateSpin(); | |
406 | } | |
407 | ||
408 | void SpinBtnWidgetsPage::OnUpdateUICurValueText(wxUpdateUIEvent& event) | |
409 | { | |
410 | event.SetText( wxString::Format(_T("%d"), m_spinbtn->GetValue())); | |
411 | } | |
412 | ||
0a3d26b8 | 413 | void SpinBtnWidgetsPage::OnSpinBtn(wxSpinEvent& event) |
32b8ec41 VZ |
414 | { |
415 | int value = event.GetInt(); | |
416 | ||
417 | wxASSERT_MSG( value == m_spinbtn->GetValue(), | |
418 | _T("spinbtn value should be the same") ); | |
419 | ||
420 | wxLogMessage(_T("Spin button value changed, now %d"), value); | |
421 | } | |
422 | ||
0a3d26b8 | 423 | void SpinBtnWidgetsPage::OnSpinBtnUp(wxSpinEvent& event) |
32b8ec41 | 424 | { |
33cf9a19 | 425 | wxLogMessage( _T("Spin button value incremented, will be %d (was %d)"), |
aec18ff7 | 426 | event.GetInt(), m_spinbtn->GetValue() ); |
32b8ec41 VZ |
427 | } |
428 | ||
0a3d26b8 | 429 | void SpinBtnWidgetsPage::OnSpinBtnDown(wxSpinEvent& event) |
32b8ec41 | 430 | { |
33cf9a19 | 431 | wxLogMessage( _T("Spin button value decremented, will be %d (was %d)"), |
aec18ff7 | 432 | event.GetInt(), m_spinbtn->GetValue() ); |
32b8ec41 VZ |
433 | } |
434 | ||
0a3d26b8 | 435 | void SpinBtnWidgetsPage::OnSpinCtrl(wxSpinEvent& event) |
32b8ec41 VZ |
436 | { |
437 | int value = event.GetInt(); | |
438 | ||
439 | wxASSERT_MSG( value == m_spinctrl->GetValue(), | |
440 | _T("spinctrl value should be the same") ); | |
441 | ||
442 | wxLogMessage(_T("Spin control value changed, now %d"), value); | |
443 | } | |
6ceb105f VZ |
444 | |
445 | void SpinBtnWidgetsPage::OnSpinText(wxCommandEvent& event) | |
446 | { | |
447 | wxLogMessage(_T("Text changed in spin control, now \"%s\""), | |
448 | event.GetString().c_str()); | |
449 | } | |
450 | ||
451 | #endif // wxUSE_SPINBTN |