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