1 /////////////////////////////////////////////////////////////////////////////
2 // Program: wxWidgets Widgets Sample
4 // Purpose: Part of the widgets sample showing wxSpinButton
5 // Author: Vadim Zeitlin
8 // Copyright: (c) 2001 Vadim Zeitlin
9 // Licence: wxWindows licence
10 /////////////////////////////////////////////////////////////////////////////
12 // ============================================================================
14 // ============================================================================
16 // ----------------------------------------------------------------------------
18 // ----------------------------------------------------------------------------
20 // for compilers that support precompilation, includes "wx/wx.h".
21 #include "wx/wxprec.h"
29 // for all others, include the necessary headers
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"
41 #include "wx/spinbutt.h"
42 #include "wx/spinctrl.h"
45 #include "wx/stattext.h"
48 #include "icons/spinbtn.xpm"
50 // ----------------------------------------------------------------------------
52 // ----------------------------------------------------------------------------
57 SpinBtnPage_Reset
= wxID_HIGHEST
,
60 SpinBtnPage_SetMinAndMax
,
62 SpinBtnPage_CurValueText
,
63 SpinBtnPage_ValueText
,
69 SpinBtnPage_SpinCtrlDouble
72 // alignment radiobox values
80 // ----------------------------------------------------------------------------
82 // ----------------------------------------------------------------------------
84 class SpinBtnWidgetsPage
: public WidgetsPage
87 SpinBtnWidgetsPage(WidgetsBookCtrl
*book
, wxImageList
*imaglist
);
88 virtual ~SpinBtnWidgetsPage(){};
90 virtual wxControl
*GetWidget() const { return m_spinbtn
; }
91 virtual Widgets
GetWidgets() const
93 Widgets
widgets(WidgetsPage::GetWidgets());
94 widgets
.push_back(m_spinctrl
);
95 widgets
.push_back(m_spinctrldbl
);
99 virtual void RecreateWidget() { CreateSpin(); }
101 // lazy creation of the content
102 virtual void CreateContent();
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
);
112 void OnCheckOrRadioBox(wxCommandEvent
& event
);
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
);
122 void OnUpdateUIValueButton(wxUpdateUIEvent
& event
);
123 void OnUpdateUIMinMaxButton(wxUpdateUIEvent
& event
);
124 void OnUpdateUIBaseButton(wxUpdateUIEvent
& event
);
126 void OnUpdateUIResetButton(wxUpdateUIEvent
& event
);
128 void OnUpdateUICurValueText(wxUpdateUIEvent
& event
);
130 // reset the spinbtn parameters
133 // (re)create the spinbtn
136 // is this spinbtn value in range?
137 bool IsValidValue(int val
) const
138 { return (val
>= m_min
) && (val
<= m_max
); }
149 // the check/radio boxes for styles
150 wxCheckBox
*m_chkVert
,
154 wxRadioBox
*m_radioAlign
;
156 // the spinbtn and the spinctrl and the sizer containing them
157 wxSpinButton
*m_spinbtn
;
158 wxSpinCtrl
*m_spinctrl
;
159 wxSpinCtrlDouble
*m_spinctrldbl
;
161 wxSizer
*m_sizerSpin
;
163 // the text entries for set value/range
164 wxTextCtrl
*m_textValue
,
170 DECLARE_EVENT_TABLE()
171 DECLARE_WIDGETS_PAGE(SpinBtnWidgetsPage
)
174 // ----------------------------------------------------------------------------
176 // ----------------------------------------------------------------------------
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
)
184 EVT_UPDATE_UI(SpinBtnPage_SetValue
, SpinBtnWidgetsPage::OnUpdateUIValueButton
)
185 EVT_UPDATE_UI(SpinBtnPage_SetMinAndMax
, SpinBtnWidgetsPage::OnUpdateUIMinMaxButton
)
186 EVT_UPDATE_UI(SpinBtnPage_SetBase
, SpinBtnWidgetsPage::OnUpdateUIBaseButton
)
188 EVT_UPDATE_UI(SpinBtnPage_Reset
, SpinBtnWidgetsPage::OnUpdateUIResetButton
)
190 EVT_UPDATE_UI(SpinBtnPage_CurValueText
, SpinBtnWidgetsPage::OnUpdateUICurValueText
)
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 EVT_TEXT_ENTER(SpinBtnPage_SpinCtrlDouble
, SpinBtnWidgetsPage::OnSpinTextEnter
)
202 EVT_CHECKBOX(wxID_ANY
, SpinBtnWidgetsPage::OnCheckOrRadioBox
)
203 EVT_RADIOBOX(wxID_ANY
, SpinBtnWidgetsPage::OnCheckOrRadioBox
)
206 // ============================================================================
208 // ============================================================================
210 #if defined(__WXUNIVERSAL__)
211 #define FAMILY_CTRLS UNIVERSAL_CTRLS
213 #define FAMILY_CTRLS NATIVE_CTRLS
216 IMPLEMENT_WIDGETS_PAGE(SpinBtnWidgetsPage
, wxT("Spin"),
217 FAMILY_CTRLS
| EDITABLE_CTRLS
220 SpinBtnWidgetsPage::SpinBtnWidgetsPage(WidgetsBookCtrl
*book
,
221 wxImageList
*imaglist
)
222 : WidgetsPage(book
, imaglist
, spinbtn_xpm
)
225 m_chkArrowKeys
= NULL
;
227 m_chkProcessEnter
= NULL
;
231 m_spinctrldbl
= NULL
;
245 void SpinBtnWidgetsPage::CreateContent()
247 wxSizer
*sizerTop
= new wxBoxSizer(wxHORIZONTAL
);
250 wxStaticBox
*box
= new wxStaticBox(this, wxID_ANY
, wxT("&Set style"));
251 wxSizer
*sizerLeft
= new wxStaticBoxSizer(box
, wxVERTICAL
);
253 m_chkVert
= CreateCheckBoxAndAddToSizer(sizerLeft
, wxT("&Vertical"));
254 m_chkArrowKeys
= CreateCheckBoxAndAddToSizer(sizerLeft
, wxT("&Arrow Keys"));
255 m_chkWrap
= CreateCheckBoxAndAddToSizer(sizerLeft
, wxT("&Wrap"));
256 m_chkProcessEnter
= CreateCheckBoxAndAddToSizer(sizerLeft
,
257 wxT("Process &Enter"));
259 sizerLeft
->Add(5, 5, 0, wxGROW
| wxALL
, 5); // spacer
261 static const wxString halign
[] =
268 m_radioAlign
= new wxRadioBox(this, wxID_ANY
, wxT("&Text alignment"),
269 wxDefaultPosition
, wxDefaultSize
,
270 WXSIZEOF(halign
), halign
, 1);
272 sizerLeft
->Add(m_radioAlign
, 0, wxGROW
| wxALL
, 5);
274 sizerLeft
->Add(5, 5, 0, wxGROW
| wxALL
, 5); // spacer
276 wxButton
*btn
= new wxButton(this, SpinBtnPage_Reset
, wxT("&Reset"));
277 sizerLeft
->Add(btn
, 0, wxALIGN_CENTRE_HORIZONTAL
| wxALL
, 15);
280 wxStaticBox
*box2
= new wxStaticBox(this, wxID_ANY
,
281 wxT("&Change spinbtn value"));
283 wxSizer
*sizerMiddle
= new wxStaticBoxSizer(box2
, wxVERTICAL
);
286 wxSizer
*sizerRow
= CreateSizerWithTextAndLabel(wxT("Current value"),
287 SpinBtnPage_CurValueText
,
289 text
->SetEditable(false);
291 sizerMiddle
->Add(sizerRow
, 0, wxALL
| wxGROW
, 5);
293 sizerRow
= CreateSizerWithTextAndButton(SpinBtnPage_SetValue
,
295 SpinBtnPage_ValueText
,
297 sizerMiddle
->Add(sizerRow
, 0, wxALL
| wxGROW
, 5);
299 sizerRow
= CreateSizerWithTextAndButton(SpinBtnPage_SetMinAndMax
,
304 m_textMax
= new wxTextCtrl(this, SpinBtnPage_MaxText
, wxEmptyString
);
305 sizerRow
->Add(m_textMax
, 1, wxLEFT
| wxALIGN_CENTRE_VERTICAL
, 5);
307 m_textMin
->SetValue( wxString::Format(wxT("%d"), m_min
) );
308 m_textMax
->SetValue( wxString::Format(wxT("%d"), m_max
) );
310 sizerMiddle
->Add(sizerRow
, 0, wxALL
| wxGROW
, 5);
312 sizerRow
= CreateSizerWithTextAndButton(SpinBtnPage_SetBase
,
314 SpinBtnPage_BaseText
,
316 m_textBase
->SetValue("10");
317 sizerMiddle
->Add(sizerRow
, 0, wxALL
| wxGROW
, 5);
320 wxSizer
*sizerRight
= new wxBoxSizer(wxVERTICAL
);
321 sizerRight
->SetMinSize(150, 0);
322 m_sizerSpin
= sizerRight
; // save it to modify it later
327 // the 3 panes panes compose the window
328 sizerTop
->Add(sizerLeft
, 0, wxGROW
| (wxALL
& ~wxLEFT
), 10);
329 sizerTop
->Add(sizerMiddle
, 0, wxGROW
| wxALL
, 10);
330 sizerTop
->Add(sizerRight
, 1, wxGROW
| (wxALL
& ~wxRIGHT
), 10);
332 // final initializations
336 // ----------------------------------------------------------------------------
338 // ----------------------------------------------------------------------------
340 void SpinBtnWidgetsPage::Reset()
342 m_chkVert
->SetValue(true);
343 m_chkArrowKeys
->SetValue(true);
344 m_chkWrap
->SetValue(false);
345 m_chkProcessEnter
->SetValue(false);
346 m_radioAlign
->SetSelection(Align_Right
);
349 void SpinBtnWidgetsPage::CreateSpin()
351 int flags
= ms_defaultFlags
;
353 bool isVert
= m_chkVert
->GetValue();
355 flags
|= wxSP_VERTICAL
;
357 flags
|= wxSP_HORIZONTAL
;
359 if ( m_chkArrowKeys
->GetValue() )
360 flags
|= wxSP_ARROW_KEYS
;
362 if ( m_chkWrap
->GetValue() )
365 if ( m_chkProcessEnter
->GetValue() )
366 flags
|= wxTE_PROCESS_ENTER
;
369 switch ( m_radioAlign
->GetSelection() )
372 wxFAIL_MSG(wxT("unexpected radiobox selection"));
376 textFlags
|= wxALIGN_LEFT
; // no-op
380 textFlags
|= wxALIGN_CENTRE_HORIZONTAL
;
384 textFlags
|= wxALIGN_RIGHT
;
391 int valOld
= m_spinbtn
->GetValue();
392 if ( !IsValidValue(valOld
) )
397 m_sizerSpin
->Clear(true /* delete windows */);
400 m_spinbtn
= new wxSpinButton(this, SpinBtnPage_SpinBtn
,
401 wxDefaultPosition
, wxDefaultSize
,
404 m_spinbtn
->SetValue(val
);
405 m_spinbtn
->SetRange(m_min
, m_max
);
407 m_spinctrl
= new wxSpinCtrl(this, SpinBtnPage_SpinCtrl
,
408 wxString::Format(wxT("%d"), val
),
409 wxDefaultPosition
, wxDefaultSize
,
413 m_spinctrldbl
= new wxSpinCtrlDouble(this, SpinBtnPage_SpinCtrlDouble
,
414 wxString::Format(wxT("%d"), val
),
415 wxDefaultPosition
, wxDefaultSize
,
417 m_min
, m_max
, val
, 0.1);
419 // Add spacers, labels and spin controls to the sizer.
420 m_sizerSpin
->Add(0, 0, 1);
421 m_sizerSpin
->Add(new wxStaticText(this, wxID_ANY
, wxT("wxSpinButton")),
422 0, wxALIGN_CENTRE
| wxALL
, 5);
423 m_sizerSpin
->Add(m_spinbtn
, 0, wxALIGN_CENTRE
| wxALL
, 5);
424 m_sizerSpin
->Add(0, 0, 1);
425 m_sizerSpin
->Add(new wxStaticText(this, wxID_ANY
, wxT("wxSpinCtrl")),
426 0, wxALIGN_CENTRE
| wxALL
, 5);
427 m_sizerSpin
->Add(m_spinctrl
, 0, wxALIGN_CENTRE
| wxALL
, 5);
428 m_sizerSpin
->Add(0, 0, 1);
429 m_sizerSpin
->Add(new wxStaticText(this, wxID_ANY
, wxT("wxSpinCtrlDouble")),
430 0, wxALIGN_CENTRE
| wxALL
, 5);
431 m_sizerSpin
->Add(m_spinctrldbl
, 0, wxALIGN_CENTRE
| wxALL
, 5);
432 m_sizerSpin
->Add(0, 0, 1);
434 m_sizerSpin
->Layout();
437 // ----------------------------------------------------------------------------
439 // ----------------------------------------------------------------------------
441 void SpinBtnWidgetsPage::OnButtonReset(wxCommandEvent
& WXUNUSED(event
))
448 void SpinBtnWidgetsPage::OnButtonSetMinAndMax(wxCommandEvent
& WXUNUSED(event
))
451 maxNew
= 0; // init to suppress compiler warning
452 if ( !m_textMin
->GetValue().ToLong(&minNew
) ||
453 !m_textMax
->GetValue().ToLong(&maxNew
) ||
456 wxLogWarning(wxT("Invalid min/max values for the spinbtn."));
463 wxString
smax('9', m_textMax
->GetValue().length());
465 size
= m_spinctrl
->GetSizeFromTextSize(m_spinctrl
->GetTextExtent(smax
));
467 m_spinctrl
->SetMinSize(size
);
468 m_spinctrl
->SetSize(size
);
471 size
= m_spinctrldbl
->GetSizeFromTextSize(
472 m_spinctrldbl
->GetTextExtent(smax
)
474 m_spinctrldbl
->SetMinSize(size
);
475 m_spinctrldbl
->SetSize(size
);
477 m_spinbtn
->SetRange(minNew
, maxNew
);
478 m_spinctrl
->SetRange(minNew
, maxNew
);
479 m_spinctrldbl
->SetRange(minNew
, maxNew
);
482 void SpinBtnWidgetsPage::OnButtonSetBase(wxCommandEvent
& WXUNUSED(event
))
485 if ( !m_textBase
->GetValue().ToULong(&base
) || !base
)
487 wxLogWarning("Invalid base value.");
492 if ( !m_spinctrl
->SetBase(m_base
) )
494 wxLogWarning("Setting base %d failed.", m_base
);
498 void SpinBtnWidgetsPage::OnButtonSetValue(wxCommandEvent
& WXUNUSED(event
))
501 if ( !m_textValue
->GetValue().ToLong(&val
) || !IsValidValue(val
) )
503 wxLogWarning(wxT("Invalid spinbtn value."));
508 m_spinbtn
->SetValue(val
);
509 m_spinctrl
->SetValue(val
);
510 m_spinctrldbl
->SetValue(val
);
513 void SpinBtnWidgetsPage::OnUpdateUIValueButton(wxUpdateUIEvent
& event
)
516 event
.Enable( m_textValue
->GetValue().ToLong(&val
) && IsValidValue(val
) );
519 void SpinBtnWidgetsPage::OnUpdateUIMinMaxButton(wxUpdateUIEvent
& event
)
522 event
.Enable( m_textMin
->GetValue().ToLong(&mn
) &&
523 m_textMax
->GetValue().ToLong(&mx
) &&
527 void SpinBtnWidgetsPage::OnUpdateUIBaseButton(wxUpdateUIEvent
& event
)
530 event
.Enable( m_textBase
->GetValue().ToULong(&base
) && base
);
533 void SpinBtnWidgetsPage::OnUpdateUIResetButton(wxUpdateUIEvent
& event
)
535 event
.Enable( !m_chkVert
->GetValue() ||
536 m_chkWrap
->GetValue() ||
537 m_chkProcessEnter
->GetValue() );
540 void SpinBtnWidgetsPage::OnCheckOrRadioBox(wxCommandEvent
& WXUNUSED(event
))
545 void SpinBtnWidgetsPage::OnUpdateUICurValueText(wxUpdateUIEvent
& event
)
547 event
.SetText( wxString::Format(wxT("%d"), m_spinbtn
->GetValue()));
550 void SpinBtnWidgetsPage::OnSpinBtn(wxSpinEvent
& event
)
552 int value
= event
.GetInt();
554 wxASSERT_MSG( value
== m_spinbtn
->GetValue(),
555 wxT("spinbtn value should be the same") );
557 wxLogMessage(wxT("Spin button value changed, now %d"), value
);
560 void SpinBtnWidgetsPage::OnSpinBtnUp(wxSpinEvent
& event
)
562 wxLogMessage( wxT("Spin button value incremented, will be %d (was %d)"),
563 event
.GetInt(), m_spinbtn
->GetValue() );
566 void SpinBtnWidgetsPage::OnSpinBtnDown(wxSpinEvent
& event
)
568 wxLogMessage( wxT("Spin button value decremented, will be %d (was %d)"),
569 event
.GetInt(), m_spinbtn
->GetValue() );
572 void SpinBtnWidgetsPage::OnSpinCtrl(wxSpinEvent
& event
)
574 int value
= event
.GetInt();
576 wxASSERT_MSG( value
== m_spinctrl
->GetValue(),
577 wxT("spinctrl value should be the same") );
579 wxLogMessage(wxT("Spin control value changed, now %d"), value
);
582 void SpinBtnWidgetsPage::OnSpinCtrlDouble(wxSpinDoubleEvent
& event
)
584 double value
= event
.GetValue();
586 wxLogMessage(wxT("Spin control value changed, now %g"), value
);
589 void SpinBtnWidgetsPage::OnSpinText(wxCommandEvent
& event
)
591 wxLogMessage(wxT("Text changed in spin control, now \"%s\""),
592 event
.GetString().c_str());
595 void SpinBtnWidgetsPage::OnSpinTextEnter(wxCommandEvent
& event
)
597 wxLogMessage("\"Enter\" pressed in spin control, text is \"%s\"",
601 #endif // wxUSE_SPINBTN