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