]> git.saurik.com Git - wxWidgets.git/blob - samples/widgets/spinbtn.cpp
Don't redraw borders for cells that are hidden - fixes drawing glitch in sample
[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 // Copyright: (c) 2001 Vadim Zeitlin
8 // Licence: wxWindows licence
9 /////////////////////////////////////////////////////////////////////////////
10
11 // ============================================================================
12 // declarations
13 // ============================================================================
14
15 // ----------------------------------------------------------------------------
16 // headers
17 // ----------------------------------------------------------------------------
18
19 // for compilers that support precompilation, includes "wx/wx.h".
20 #include "wx/wxprec.h"
21
22 #ifdef __BORLANDC__
23 #pragma hdrstop
24 #endif
25
26 #if wxUSE_SPINBTN
27
28 // for all others, include the necessary headers
29 #ifndef WX_PRECOMP
30 #include "wx/log.h"
31
32 #include "wx/bitmap.h"
33 #include "wx/button.h"
34 #include "wx/checkbox.h"
35 #include "wx/radiobox.h"
36 #include "wx/statbox.h"
37 #include "wx/textctrl.h"
38 #endif
39
40 #include "wx/spinbutt.h"
41 #include "wx/spinctrl.h"
42
43 #include "wx/sizer.h"
44 #include "wx/stattext.h"
45 #include "widgets.h"
46
47 #include "icons/spinbtn.xpm"
48
49 // ----------------------------------------------------------------------------
50 // constants
51 // ----------------------------------------------------------------------------
52
53 // control ids
54 enum
55 {
56 SpinBtnPage_Reset = wxID_HIGHEST,
57 SpinBtnPage_Clear,
58 SpinBtnPage_SetValue,
59 SpinBtnPage_SetMinAndMax,
60 SpinBtnPage_SetBase,
61 SpinBtnPage_CurValueText,
62 SpinBtnPage_ValueText,
63 SpinBtnPage_MinText,
64 SpinBtnPage_MaxText,
65 SpinBtnPage_BaseText,
66 SpinBtnPage_SpinBtn,
67 SpinBtnPage_SpinCtrl,
68 SpinBtnPage_SpinCtrlDouble
69 };
70
71 // alignment radiobox values
72 enum
73 {
74 Align_Left,
75 Align_Centre,
76 Align_Right
77 };
78
79 // ----------------------------------------------------------------------------
80 // SpinBtnWidgetsPage
81 // ----------------------------------------------------------------------------
82
83 class SpinBtnWidgetsPage : public WidgetsPage
84 {
85 public:
86 SpinBtnWidgetsPage(WidgetsBookCtrl *book, wxImageList *imaglist);
87 virtual ~SpinBtnWidgetsPage(){};
88
89 virtual wxControl *GetWidget() const { return m_spinbtn; }
90 virtual Widgets GetWidgets() const
91 {
92 Widgets widgets(WidgetsPage::GetWidgets());
93 widgets.push_back(m_spinctrl);
94 widgets.push_back(m_spinctrldbl);
95 return widgets;
96 }
97
98 virtual void RecreateWidget() { CreateSpin(); }
99
100 // lazy creation of the content
101 virtual void CreateContent();
102
103 protected:
104 // event handlers
105 void OnButtonReset(wxCommandEvent& event);
106 void OnButtonClear(wxCommandEvent& event);
107 void OnButtonSetValue(wxCommandEvent& event);
108 void OnButtonSetMinAndMax(wxCommandEvent& event);
109 void OnButtonSetBase(wxCommandEvent& event);
110
111 void OnCheckOrRadioBox(wxCommandEvent& event);
112
113 void OnSpinBtn(wxSpinEvent& event);
114 void OnSpinBtnUp(wxSpinEvent& event);
115 void OnSpinBtnDown(wxSpinEvent& event);
116 void OnSpinCtrl(wxSpinEvent& event);
117 void OnSpinCtrlDouble(wxSpinDoubleEvent& event);
118 void OnSpinText(wxCommandEvent& event);
119 void OnSpinTextEnter(wxCommandEvent& event);
120
121 void OnUpdateUIValueButton(wxUpdateUIEvent& event);
122 void OnUpdateUIMinMaxButton(wxUpdateUIEvent& event);
123 void OnUpdateUIBaseButton(wxUpdateUIEvent& event);
124
125 void OnUpdateUIResetButton(wxUpdateUIEvent& event);
126
127 void OnUpdateUICurValueText(wxUpdateUIEvent& event);
128
129 // reset the spinbtn parameters
130 void Reset();
131
132 // (re)create the spinbtn
133 void CreateSpin();
134
135 // is this spinbtn value in range?
136 bool IsValidValue(int val) const
137 { return (val >= m_min) && (val <= m_max); }
138
139 // the spinbtn range
140 int m_min, m_max;
141
142 // and numeric base
143 int m_base;
144
145 // the controls
146 // ------------
147
148 // the check/radio boxes for styles
149 wxCheckBox *m_chkVert,
150 *m_chkArrowKeys,
151 *m_chkWrap,
152 *m_chkProcessEnter;
153 wxRadioBox *m_radioAlign;
154
155 // the spinbtn and the spinctrl and the sizer containing them
156 wxSpinButton *m_spinbtn;
157 wxSpinCtrl *m_spinctrl;
158 wxSpinCtrlDouble *m_spinctrldbl;
159
160 wxSizer *m_sizerSpin;
161
162 // the text entries for set value/range
163 wxTextCtrl *m_textValue,
164 *m_textMin,
165 *m_textMax,
166 *m_textBase;
167
168 private:
169 DECLARE_EVENT_TABLE()
170 DECLARE_WIDGETS_PAGE(SpinBtnWidgetsPage)
171 };
172
173 // ----------------------------------------------------------------------------
174 // event tables
175 // ----------------------------------------------------------------------------
176
177 BEGIN_EVENT_TABLE(SpinBtnWidgetsPage, WidgetsPage)
178 EVT_BUTTON(SpinBtnPage_Reset, SpinBtnWidgetsPage::OnButtonReset)
179 EVT_BUTTON(SpinBtnPage_SetValue, SpinBtnWidgetsPage::OnButtonSetValue)
180 EVT_BUTTON(SpinBtnPage_SetMinAndMax, SpinBtnWidgetsPage::OnButtonSetMinAndMax)
181 EVT_BUTTON(SpinBtnPage_SetBase, SpinBtnWidgetsPage::OnButtonSetBase)
182
183 EVT_UPDATE_UI(SpinBtnPage_SetValue, SpinBtnWidgetsPage::OnUpdateUIValueButton)
184 EVT_UPDATE_UI(SpinBtnPage_SetMinAndMax, SpinBtnWidgetsPage::OnUpdateUIMinMaxButton)
185 EVT_UPDATE_UI(SpinBtnPage_SetBase, SpinBtnWidgetsPage::OnUpdateUIBaseButton)
186
187 EVT_UPDATE_UI(SpinBtnPage_Reset, SpinBtnWidgetsPage::OnUpdateUIResetButton)
188
189 EVT_UPDATE_UI(SpinBtnPage_CurValueText, SpinBtnWidgetsPage::OnUpdateUICurValueText)
190
191 EVT_SPIN(SpinBtnPage_SpinBtn, SpinBtnWidgetsPage::OnSpinBtn)
192 EVT_SPIN_UP(SpinBtnPage_SpinBtn, SpinBtnWidgetsPage::OnSpinBtnUp)
193 EVT_SPIN_DOWN(SpinBtnPage_SpinBtn, SpinBtnWidgetsPage::OnSpinBtnDown)
194 EVT_SPINCTRL(SpinBtnPage_SpinCtrl, SpinBtnWidgetsPage::OnSpinCtrl)
195 EVT_SPINCTRLDOUBLE(SpinBtnPage_SpinCtrlDouble, SpinBtnWidgetsPage::OnSpinCtrlDouble)
196 EVT_TEXT(SpinBtnPage_SpinCtrl, SpinBtnWidgetsPage::OnSpinText)
197 EVT_TEXT_ENTER(SpinBtnPage_SpinCtrl, SpinBtnWidgetsPage::OnSpinTextEnter)
198 EVT_TEXT(SpinBtnPage_SpinCtrlDouble, SpinBtnWidgetsPage::OnSpinText)
199 EVT_TEXT_ENTER(SpinBtnPage_SpinCtrlDouble, SpinBtnWidgetsPage::OnSpinTextEnter)
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 wxString smax('9', m_textMax->GetValue().length());
463 wxSize
464 size = m_spinctrl->GetSizeFromTextSize(m_spinctrl->GetTextExtent(smax));
465
466 m_spinctrl->SetMinSize(size);
467 m_spinctrl->SetSize(size);
468
469 smax += ".0";
470 size = m_spinctrldbl->GetSizeFromTextSize(
471 m_spinctrldbl->GetTextExtent(smax)
472 );
473 m_spinctrldbl->SetMinSize(size);
474 m_spinctrldbl->SetSize(size);
475
476 m_spinbtn->SetRange(minNew, maxNew);
477 m_spinctrl->SetRange(minNew, maxNew);
478 m_spinctrldbl->SetRange(minNew, maxNew);
479 }
480
481 void SpinBtnWidgetsPage::OnButtonSetBase(wxCommandEvent& WXUNUSED(event))
482 {
483 unsigned long base;
484 if ( !m_textBase->GetValue().ToULong(&base) || !base )
485 {
486 wxLogWarning("Invalid base value.");
487 return;
488 }
489
490 m_base = base;
491 if ( !m_spinctrl->SetBase(m_base) )
492 {
493 wxLogWarning("Setting base %d failed.", m_base);
494 }
495 }
496
497 void SpinBtnWidgetsPage::OnButtonSetValue(wxCommandEvent& WXUNUSED(event))
498 {
499 long val;
500 if ( !m_textValue->GetValue().ToLong(&val) || !IsValidValue(val) )
501 {
502 wxLogWarning(wxT("Invalid spinbtn value."));
503
504 return;
505 }
506
507 m_spinbtn->SetValue(val);
508 m_spinctrl->SetValue(val);
509 m_spinctrldbl->SetValue(val);
510 }
511
512 void SpinBtnWidgetsPage::OnUpdateUIValueButton(wxUpdateUIEvent& event)
513 {
514 long val;
515 event.Enable( m_textValue->GetValue().ToLong(&val) && IsValidValue(val) );
516 }
517
518 void SpinBtnWidgetsPage::OnUpdateUIMinMaxButton(wxUpdateUIEvent& event)
519 {
520 long mn, mx;
521 event.Enable( m_textMin->GetValue().ToLong(&mn) &&
522 m_textMax->GetValue().ToLong(&mx) &&
523 mn <= mx);
524 }
525
526 void SpinBtnWidgetsPage::OnUpdateUIBaseButton(wxUpdateUIEvent& event)
527 {
528 unsigned long base;
529 event.Enable( m_textBase->GetValue().ToULong(&base) && base );
530 }
531
532 void SpinBtnWidgetsPage::OnUpdateUIResetButton(wxUpdateUIEvent& event)
533 {
534 event.Enable( !m_chkVert->GetValue() ||
535 m_chkWrap->GetValue() ||
536 m_chkProcessEnter->GetValue() );
537 }
538
539 void SpinBtnWidgetsPage::OnCheckOrRadioBox(wxCommandEvent& WXUNUSED(event))
540 {
541 CreateSpin();
542 }
543
544 void SpinBtnWidgetsPage::OnUpdateUICurValueText(wxUpdateUIEvent& event)
545 {
546 event.SetText( wxString::Format(wxT("%d"), m_spinbtn->GetValue()));
547 }
548
549 void SpinBtnWidgetsPage::OnSpinBtn(wxSpinEvent& event)
550 {
551 int value = event.GetInt();
552
553 wxASSERT_MSG( value == m_spinbtn->GetValue(),
554 wxT("spinbtn value should be the same") );
555
556 wxLogMessage(wxT("Spin button value changed, now %d"), value);
557 }
558
559 void SpinBtnWidgetsPage::OnSpinBtnUp(wxSpinEvent& event)
560 {
561 wxLogMessage( wxT("Spin button value incremented, will be %d (was %d)"),
562 event.GetInt(), m_spinbtn->GetValue() );
563 }
564
565 void SpinBtnWidgetsPage::OnSpinBtnDown(wxSpinEvent& event)
566 {
567 wxLogMessage( wxT("Spin button value decremented, will be %d (was %d)"),
568 event.GetInt(), m_spinbtn->GetValue() );
569 }
570
571 void SpinBtnWidgetsPage::OnSpinCtrl(wxSpinEvent& event)
572 {
573 int value = event.GetInt();
574
575 wxASSERT_MSG( value == m_spinctrl->GetValue(),
576 wxT("spinctrl value should be the same") );
577
578 wxLogMessage(wxT("Spin control value changed, now %d"), value);
579 }
580
581 void SpinBtnWidgetsPage::OnSpinCtrlDouble(wxSpinDoubleEvent& event)
582 {
583 double value = event.GetValue();
584
585 wxLogMessage(wxT("Spin control value changed, now %g"), value);
586 }
587
588 void SpinBtnWidgetsPage::OnSpinText(wxCommandEvent& event)
589 {
590 wxLogMessage(wxT("Text changed in spin control, now \"%s\""),
591 event.GetString().c_str());
592 }
593
594 void SpinBtnWidgetsPage::OnSpinTextEnter(wxCommandEvent& event)
595 {
596 wxLogMessage("\"Enter\" pressed in spin control, text is \"%s\"",
597 event.GetString());
598 }
599
600 #endif // wxUSE_SPINBTN