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