1 /////////////////////////////////////////////////////////////////////////////
2 // Program: wxWidgets Widgets Sample
4 // Purpose: Part of the widgets sample showing wxSpinButton
5 // Author: Vadim Zeitlin
7 // Copyright: (c) 2001 Vadim Zeitlin
8 // Licence: wxWindows licence
9 /////////////////////////////////////////////////////////////////////////////
11 // ============================================================================
13 // ============================================================================
15 // ----------------------------------------------------------------------------
17 // ----------------------------------------------------------------------------
19 // for compilers that support precompilation, includes "wx/wx.h".
20 #include "wx/wxprec.h"
28 // for all others, include the necessary headers
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"
40 #include "wx/spinbutt.h"
41 #include "wx/spinctrl.h"
44 #include "wx/stattext.h"
47 #include "icons/spinbtn.xpm"
49 // ----------------------------------------------------------------------------
51 // ----------------------------------------------------------------------------
56 SpinBtnPage_Reset
= wxID_HIGHEST
,
59 SpinBtnPage_SetMinAndMax
,
61 SpinBtnPage_CurValueText
,
62 SpinBtnPage_ValueText
,
68 SpinBtnPage_SpinCtrlDouble
71 // alignment radiobox values
79 // ----------------------------------------------------------------------------
81 // ----------------------------------------------------------------------------
83 class SpinBtnWidgetsPage
: public WidgetsPage
86 SpinBtnWidgetsPage(WidgetsBookCtrl
*book
, wxImageList
*imaglist
);
87 virtual ~SpinBtnWidgetsPage(){};
89 virtual wxControl
*GetWidget() const { return m_spinbtn
; }
90 virtual Widgets
GetWidgets() const
92 Widgets
widgets(WidgetsPage::GetWidgets());
93 widgets
.push_back(m_spinctrl
);
94 widgets
.push_back(m_spinctrldbl
);
98 virtual void RecreateWidget() { CreateSpin(); }
100 // lazy creation of the content
101 virtual void CreateContent();
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
);
111 void OnCheckOrRadioBox(wxCommandEvent
& event
);
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
);
121 void OnUpdateUIValueButton(wxUpdateUIEvent
& event
);
122 void OnUpdateUIMinMaxButton(wxUpdateUIEvent
& event
);
123 void OnUpdateUIBaseButton(wxUpdateUIEvent
& event
);
125 void OnUpdateUIResetButton(wxUpdateUIEvent
& event
);
127 void OnUpdateUICurValueText(wxUpdateUIEvent
& event
);
129 // reset the spinbtn parameters
132 // (re)create the spinbtn
135 // is this spinbtn value in range?
136 bool IsValidValue(int val
) const
137 { return (val
>= m_min
) && (val
<= m_max
); }
148 // the check/radio boxes for styles
149 wxCheckBox
*m_chkVert
,
153 wxRadioBox
*m_radioAlign
;
155 // the spinbtn and the spinctrl and the sizer containing them
156 wxSpinButton
*m_spinbtn
;
157 wxSpinCtrl
*m_spinctrl
;
158 wxSpinCtrlDouble
*m_spinctrldbl
;
160 wxSizer
*m_sizerSpin
;
162 // the text entries for set value/range
163 wxTextCtrl
*m_textValue
,
169 DECLARE_EVENT_TABLE()
170 DECLARE_WIDGETS_PAGE(SpinBtnWidgetsPage
)
173 // ----------------------------------------------------------------------------
175 // ----------------------------------------------------------------------------
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
)
183 EVT_UPDATE_UI(SpinBtnPage_SetValue
, SpinBtnWidgetsPage::OnUpdateUIValueButton
)
184 EVT_UPDATE_UI(SpinBtnPage_SetMinAndMax
, SpinBtnWidgetsPage::OnUpdateUIMinMaxButton
)
185 EVT_UPDATE_UI(SpinBtnPage_SetBase
, SpinBtnWidgetsPage::OnUpdateUIBaseButton
)
187 EVT_UPDATE_UI(SpinBtnPage_Reset
, SpinBtnWidgetsPage::OnUpdateUIResetButton
)
189 EVT_UPDATE_UI(SpinBtnPage_CurValueText
, SpinBtnWidgetsPage::OnUpdateUICurValueText
)
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
)
201 EVT_CHECKBOX(wxID_ANY
, SpinBtnWidgetsPage::OnCheckOrRadioBox
)
202 EVT_RADIOBOX(wxID_ANY
, SpinBtnWidgetsPage::OnCheckOrRadioBox
)
205 // ============================================================================
207 // ============================================================================
209 #if defined(__WXUNIVERSAL__)
210 #define FAMILY_CTRLS UNIVERSAL_CTRLS
212 #define FAMILY_CTRLS NATIVE_CTRLS
215 IMPLEMENT_WIDGETS_PAGE(SpinBtnWidgetsPage
, wxT("Spin"),
216 FAMILY_CTRLS
| EDITABLE_CTRLS
219 SpinBtnWidgetsPage::SpinBtnWidgetsPage(WidgetsBookCtrl
*book
,
220 wxImageList
*imaglist
)
221 : WidgetsPage(book
, imaglist
, spinbtn_xpm
)
224 m_chkArrowKeys
= NULL
;
226 m_chkProcessEnter
= NULL
;
230 m_spinctrldbl
= NULL
;
244 void SpinBtnWidgetsPage::CreateContent()
246 wxSizer
*sizerTop
= new wxBoxSizer(wxHORIZONTAL
);
249 wxStaticBox
*box
= new wxStaticBox(this, wxID_ANY
, wxT("&Set style"));
250 wxSizer
*sizerLeft
= new wxStaticBoxSizer(box
, wxVERTICAL
);
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"));
258 sizerLeft
->Add(5, 5, 0, wxGROW
| wxALL
, 5); // spacer
260 static const wxString halign
[] =
267 m_radioAlign
= new wxRadioBox(this, wxID_ANY
, wxT("&Text alignment"),
268 wxDefaultPosition
, wxDefaultSize
,
269 WXSIZEOF(halign
), halign
, 1);
271 sizerLeft
->Add(m_radioAlign
, 0, wxGROW
| wxALL
, 5);
273 sizerLeft
->Add(5, 5, 0, wxGROW
| wxALL
, 5); // spacer
275 wxButton
*btn
= new wxButton(this, SpinBtnPage_Reset
, wxT("&Reset"));
276 sizerLeft
->Add(btn
, 0, wxALIGN_CENTRE_HORIZONTAL
| wxALL
, 15);
279 wxStaticBox
*box2
= new wxStaticBox(this, wxID_ANY
,
280 wxT("&Change spinbtn value"));
282 wxSizer
*sizerMiddle
= new wxStaticBoxSizer(box2
, wxVERTICAL
);
285 wxSizer
*sizerRow
= CreateSizerWithTextAndLabel(wxT("Current value"),
286 SpinBtnPage_CurValueText
,
288 text
->SetEditable(false);
290 sizerMiddle
->Add(sizerRow
, 0, wxALL
| wxGROW
, 5);
292 sizerRow
= CreateSizerWithTextAndButton(SpinBtnPage_SetValue
,
294 SpinBtnPage_ValueText
,
296 sizerMiddle
->Add(sizerRow
, 0, wxALL
| wxGROW
, 5);
298 sizerRow
= CreateSizerWithTextAndButton(SpinBtnPage_SetMinAndMax
,
303 m_textMax
= new wxTextCtrl(this, SpinBtnPage_MaxText
, wxEmptyString
);
304 sizerRow
->Add(m_textMax
, 1, wxLEFT
| wxALIGN_CENTRE_VERTICAL
, 5);
306 m_textMin
->SetValue( wxString::Format(wxT("%d"), m_min
) );
307 m_textMax
->SetValue( wxString::Format(wxT("%d"), m_max
) );
309 sizerMiddle
->Add(sizerRow
, 0, wxALL
| wxGROW
, 5);
311 sizerRow
= CreateSizerWithTextAndButton(SpinBtnPage_SetBase
,
313 SpinBtnPage_BaseText
,
315 m_textBase
->SetValue("10");
316 sizerMiddle
->Add(sizerRow
, 0, wxALL
| wxGROW
, 5);
319 wxSizer
*sizerRight
= new wxBoxSizer(wxVERTICAL
);
320 sizerRight
->SetMinSize(150, 0);
321 m_sizerSpin
= sizerRight
; // save it to modify it later
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);
331 // final initializations
335 // ----------------------------------------------------------------------------
337 // ----------------------------------------------------------------------------
339 void SpinBtnWidgetsPage::Reset()
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
);
348 void SpinBtnWidgetsPage::CreateSpin()
350 int flags
= ms_defaultFlags
;
352 bool isVert
= m_chkVert
->GetValue();
354 flags
|= wxSP_VERTICAL
;
356 flags
|= wxSP_HORIZONTAL
;
358 if ( m_chkArrowKeys
->GetValue() )
359 flags
|= wxSP_ARROW_KEYS
;
361 if ( m_chkWrap
->GetValue() )
364 if ( m_chkProcessEnter
->GetValue() )
365 flags
|= wxTE_PROCESS_ENTER
;
368 switch ( m_radioAlign
->GetSelection() )
371 wxFAIL_MSG(wxT("unexpected radiobox selection"));
375 textFlags
|= wxALIGN_LEFT
; // no-op
379 textFlags
|= wxALIGN_CENTRE_HORIZONTAL
;
383 textFlags
|= wxALIGN_RIGHT
;
390 int valOld
= m_spinbtn
->GetValue();
391 if ( !IsValidValue(valOld
) )
396 m_sizerSpin
->Clear(true /* delete windows */);
399 m_spinbtn
= new wxSpinButton(this, SpinBtnPage_SpinBtn
,
400 wxDefaultPosition
, wxDefaultSize
,
403 m_spinbtn
->SetValue(val
);
404 m_spinbtn
->SetRange(m_min
, m_max
);
406 m_spinctrl
= new wxSpinCtrl(this, SpinBtnPage_SpinCtrl
,
407 wxString::Format(wxT("%d"), val
),
408 wxDefaultPosition
, wxDefaultSize
,
412 m_spinctrldbl
= new wxSpinCtrlDouble(this, SpinBtnPage_SpinCtrlDouble
,
413 wxString::Format(wxT("%d"), val
),
414 wxDefaultPosition
, wxDefaultSize
,
416 m_min
, m_max
, val
, 0.1);
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);
433 m_sizerSpin
->Layout();
436 // ----------------------------------------------------------------------------
438 // ----------------------------------------------------------------------------
440 void SpinBtnWidgetsPage::OnButtonReset(wxCommandEvent
& WXUNUSED(event
))
447 void SpinBtnWidgetsPage::OnButtonSetMinAndMax(wxCommandEvent
& WXUNUSED(event
))
450 maxNew
= 0; // init to suppress compiler warning
451 if ( !m_textMin
->GetValue().ToLong(&minNew
) ||
452 !m_textMax
->GetValue().ToLong(&maxNew
) ||
455 wxLogWarning(wxT("Invalid min/max values for the spinbtn."));
462 wxString
smax('9', m_textMax
->GetValue().length());
464 size
= m_spinctrl
->GetSizeFromTextSize(m_spinctrl
->GetTextExtent(smax
));
466 m_spinctrl
->SetMinSize(size
);
467 m_spinctrl
->SetSize(size
);
470 size
= m_spinctrldbl
->GetSizeFromTextSize(
471 m_spinctrldbl
->GetTextExtent(smax
)
473 m_spinctrldbl
->SetMinSize(size
);
474 m_spinctrldbl
->SetSize(size
);
476 m_spinbtn
->SetRange(minNew
, maxNew
);
477 m_spinctrl
->SetRange(minNew
, maxNew
);
478 m_spinctrldbl
->SetRange(minNew
, maxNew
);
481 void SpinBtnWidgetsPage::OnButtonSetBase(wxCommandEvent
& WXUNUSED(event
))
484 if ( !m_textBase
->GetValue().ToULong(&base
) || !base
)
486 wxLogWarning("Invalid base value.");
491 if ( !m_spinctrl
->SetBase(m_base
) )
493 wxLogWarning("Setting base %d failed.", m_base
);
497 void SpinBtnWidgetsPage::OnButtonSetValue(wxCommandEvent
& WXUNUSED(event
))
500 if ( !m_textValue
->GetValue().ToLong(&val
) || !IsValidValue(val
) )
502 wxLogWarning(wxT("Invalid spinbtn value."));
507 m_spinbtn
->SetValue(val
);
508 m_spinctrl
->SetValue(val
);
509 m_spinctrldbl
->SetValue(val
);
512 void SpinBtnWidgetsPage::OnUpdateUIValueButton(wxUpdateUIEvent
& event
)
515 event
.Enable( m_textValue
->GetValue().ToLong(&val
) && IsValidValue(val
) );
518 void SpinBtnWidgetsPage::OnUpdateUIMinMaxButton(wxUpdateUIEvent
& event
)
521 event
.Enable( m_textMin
->GetValue().ToLong(&mn
) &&
522 m_textMax
->GetValue().ToLong(&mx
) &&
526 void SpinBtnWidgetsPage::OnUpdateUIBaseButton(wxUpdateUIEvent
& event
)
529 event
.Enable( m_textBase
->GetValue().ToULong(&base
) && base
);
532 void SpinBtnWidgetsPage::OnUpdateUIResetButton(wxUpdateUIEvent
& event
)
534 event
.Enable( !m_chkVert
->GetValue() ||
535 m_chkWrap
->GetValue() ||
536 m_chkProcessEnter
->GetValue() );
539 void SpinBtnWidgetsPage::OnCheckOrRadioBox(wxCommandEvent
& WXUNUSED(event
))
544 void SpinBtnWidgetsPage::OnUpdateUICurValueText(wxUpdateUIEvent
& event
)
546 event
.SetText( wxString::Format(wxT("%d"), m_spinbtn
->GetValue()));
549 void SpinBtnWidgetsPage::OnSpinBtn(wxSpinEvent
& event
)
551 int value
= event
.GetInt();
553 wxASSERT_MSG( value
== m_spinbtn
->GetValue(),
554 wxT("spinbtn value should be the same") );
556 wxLogMessage(wxT("Spin button value changed, now %d"), value
);
559 void SpinBtnWidgetsPage::OnSpinBtnUp(wxSpinEvent
& event
)
561 wxLogMessage( wxT("Spin button value incremented, will be %d (was %d)"),
562 event
.GetInt(), m_spinbtn
->GetValue() );
565 void SpinBtnWidgetsPage::OnSpinBtnDown(wxSpinEvent
& event
)
567 wxLogMessage( wxT("Spin button value decremented, will be %d (was %d)"),
568 event
.GetInt(), m_spinbtn
->GetValue() );
571 void SpinBtnWidgetsPage::OnSpinCtrl(wxSpinEvent
& event
)
573 int value
= event
.GetInt();
575 wxASSERT_MSG( value
== m_spinctrl
->GetValue(),
576 wxT("spinctrl value should be the same") );
578 wxLogMessage(wxT("Spin control value changed, now %d"), value
);
581 void SpinBtnWidgetsPage::OnSpinCtrlDouble(wxSpinDoubleEvent
& event
)
583 double value
= event
.GetValue();
585 wxLogMessage(wxT("Spin control value changed, now %g"), value
);
588 void SpinBtnWidgetsPage::OnSpinText(wxCommandEvent
& event
)
590 wxLogMessage(wxT("Text changed in spin control, now \"%s\""),
591 event
.GetString().c_str());
594 void SpinBtnWidgetsPage::OnSpinTextEnter(wxCommandEvent
& event
)
596 wxLogMessage("\"Enter\" pressed in spin control, text is \"%s\"",
600 #endif // wxUSE_SPINBTN