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 // License: wxWindows license
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
,
61 SpinBtnPage_CurValueText
,
62 SpinBtnPage_ValueText
,
67 SpinBtnPage_SpinCtrlDouble
70 // alignment radiobox values
78 // ----------------------------------------------------------------------------
80 // ----------------------------------------------------------------------------
82 class SpinBtnWidgetsPage
: public WidgetsPage
85 SpinBtnWidgetsPage(WidgetsBookCtrl
*book
, wxImageList
*imaglist
);
86 virtual ~SpinBtnWidgetsPage(){};
88 virtual wxControl
*GetWidget() const { return m_spinbtn
; }
89 virtual Widgets
GetWidgets() const
91 Widgets
widgets(WidgetsPage::GetWidgets());
92 widgets
.push_back(m_spinctrl
);
93 widgets
.push_back(m_spinctrldbl
);
97 virtual void RecreateWidget() { CreateSpin(); }
99 // lazy creation of the content
100 virtual void CreateContent();
104 void OnButtonReset(wxCommandEvent
& event
);
105 void OnButtonClear(wxCommandEvent
& event
);
106 void OnButtonSetValue(wxCommandEvent
& event
);
107 void OnButtonSetMinAndMax(wxCommandEvent
& event
);
109 void OnCheckOrRadioBox(wxCommandEvent
& event
);
111 void OnSpinBtn(wxSpinEvent
& event
);
112 void OnSpinBtnUp(wxSpinEvent
& event
);
113 void OnSpinBtnDown(wxSpinEvent
& event
);
114 void OnSpinCtrl(wxSpinEvent
& event
);
115 void OnSpinCtrlDouble(wxSpinDoubleEvent
& event
);
116 void OnSpinText(wxCommandEvent
& event
);
117 void OnSpinTextEnter(wxCommandEvent
& event
);
119 void OnUpdateUIValueButton(wxUpdateUIEvent
& event
);
120 void OnUpdateUIMinMaxButton(wxUpdateUIEvent
& event
);
122 void OnUpdateUIResetButton(wxUpdateUIEvent
& event
);
124 void OnUpdateUICurValueText(wxUpdateUIEvent
& event
);
126 // reset the spinbtn parameters
129 // (re)create the spinbtn
132 // is this spinbtn value in range?
133 bool IsValidValue(int val
) const
134 { return (val
>= m_min
) && (val
<= m_max
); }
142 // the check/radio boxes for styles
143 wxCheckBox
*m_chkVert
,
147 wxRadioBox
*m_radioAlign
;
149 // the spinbtn and the spinctrl and the sizer containing them
150 wxSpinButton
*m_spinbtn
;
151 wxSpinCtrl
*m_spinctrl
;
152 wxSpinCtrlDouble
*m_spinctrldbl
;
154 wxSizer
*m_sizerSpin
;
156 // the text entries for set value/range
157 wxTextCtrl
*m_textValue
,
162 DECLARE_EVENT_TABLE()
163 DECLARE_WIDGETS_PAGE(SpinBtnWidgetsPage
)
166 // ----------------------------------------------------------------------------
168 // ----------------------------------------------------------------------------
170 BEGIN_EVENT_TABLE(SpinBtnWidgetsPage
, WidgetsPage
)
171 EVT_BUTTON(SpinBtnPage_Reset
, SpinBtnWidgetsPage::OnButtonReset
)
172 EVT_BUTTON(SpinBtnPage_SetValue
, SpinBtnWidgetsPage::OnButtonSetValue
)
173 EVT_BUTTON(SpinBtnPage_SetMinAndMax
, SpinBtnWidgetsPage::OnButtonSetMinAndMax
)
175 EVT_UPDATE_UI(SpinBtnPage_SetValue
, SpinBtnWidgetsPage::OnUpdateUIValueButton
)
176 EVT_UPDATE_UI(SpinBtnPage_SetMinAndMax
, SpinBtnWidgetsPage::OnUpdateUIMinMaxButton
)
178 EVT_UPDATE_UI(SpinBtnPage_Reset
, SpinBtnWidgetsPage::OnUpdateUIResetButton
)
180 EVT_UPDATE_UI(SpinBtnPage_CurValueText
, SpinBtnWidgetsPage::OnUpdateUICurValueText
)
182 EVT_SPIN(SpinBtnPage_SpinBtn
, SpinBtnWidgetsPage::OnSpinBtn
)
183 EVT_SPIN_UP(SpinBtnPage_SpinBtn
, SpinBtnWidgetsPage::OnSpinBtnUp
)
184 EVT_SPIN_DOWN(SpinBtnPage_SpinBtn
, SpinBtnWidgetsPage::OnSpinBtnDown
)
185 EVT_SPINCTRL(SpinBtnPage_SpinCtrl
, SpinBtnWidgetsPage::OnSpinCtrl
)
186 EVT_SPINCTRLDOUBLE(SpinBtnPage_SpinCtrlDouble
, SpinBtnWidgetsPage::OnSpinCtrlDouble
)
187 EVT_TEXT(SpinBtnPage_SpinCtrl
, SpinBtnWidgetsPage::OnSpinText
)
188 EVT_TEXT_ENTER(SpinBtnPage_SpinCtrl
, SpinBtnWidgetsPage::OnSpinTextEnter
)
189 EVT_TEXT(SpinBtnPage_SpinCtrlDouble
, SpinBtnWidgetsPage::OnSpinText
)
191 EVT_CHECKBOX(wxID_ANY
, SpinBtnWidgetsPage::OnCheckOrRadioBox
)
192 EVT_RADIOBOX(wxID_ANY
, SpinBtnWidgetsPage::OnCheckOrRadioBox
)
195 // ============================================================================
197 // ============================================================================
199 #if defined(__WXUNIVERSAL__)
200 #define FAMILY_CTRLS UNIVERSAL_CTRLS
202 #define FAMILY_CTRLS NATIVE_CTRLS
205 IMPLEMENT_WIDGETS_PAGE(SpinBtnWidgetsPage
, wxT("Spin"),
206 FAMILY_CTRLS
| EDITABLE_CTRLS
209 SpinBtnWidgetsPage::SpinBtnWidgetsPage(WidgetsBookCtrl
*book
,
210 wxImageList
*imaglist
)
211 : WidgetsPage(book
, imaglist
, spinbtn_xpm
)
214 m_chkArrowKeys
= NULL
;
216 m_chkProcessEnter
= NULL
;
220 m_spinctrldbl
= NULL
;
231 void SpinBtnWidgetsPage::CreateContent()
233 wxSizer
*sizerTop
= new wxBoxSizer(wxHORIZONTAL
);
236 wxStaticBox
*box
= new wxStaticBox(this, wxID_ANY
, wxT("&Set style"));
237 wxSizer
*sizerLeft
= new wxStaticBoxSizer(box
, wxVERTICAL
);
239 m_chkVert
= CreateCheckBoxAndAddToSizer(sizerLeft
, wxT("&Vertical"));
240 m_chkArrowKeys
= CreateCheckBoxAndAddToSizer(sizerLeft
, wxT("&Arrow Keys"));
241 m_chkWrap
= CreateCheckBoxAndAddToSizer(sizerLeft
, wxT("&Wrap"));
242 m_chkProcessEnter
= CreateCheckBoxAndAddToSizer(sizerLeft
,
243 wxT("Process &Enter"));
245 sizerLeft
->Add(5, 5, 0, wxGROW
| wxALL
, 5); // spacer
247 static const wxString halign
[] =
254 m_radioAlign
= new wxRadioBox(this, wxID_ANY
, wxT("&Text alignment"),
255 wxDefaultPosition
, wxDefaultSize
,
256 WXSIZEOF(halign
), halign
, 1);
258 sizerLeft
->Add(m_radioAlign
, 0, wxGROW
| wxALL
, 5);
260 sizerLeft
->Add(5, 5, 0, wxGROW
| wxALL
, 5); // spacer
262 wxButton
*btn
= new wxButton(this, SpinBtnPage_Reset
, wxT("&Reset"));
263 sizerLeft
->Add(btn
, 0, wxALIGN_CENTRE_HORIZONTAL
| wxALL
, 15);
266 wxStaticBox
*box2
= new wxStaticBox(this, wxID_ANY
,
267 wxT("&Change spinbtn value"));
269 wxSizer
*sizerMiddle
= new wxStaticBoxSizer(box2
, wxVERTICAL
);
272 wxSizer
*sizerRow
= CreateSizerWithTextAndLabel(wxT("Current value"),
273 SpinBtnPage_CurValueText
,
275 text
->SetEditable(false);
277 sizerMiddle
->Add(sizerRow
, 0, wxALL
| wxGROW
, 5);
279 sizerRow
= CreateSizerWithTextAndButton(SpinBtnPage_SetValue
,
281 SpinBtnPage_ValueText
,
283 sizerMiddle
->Add(sizerRow
, 0, wxALL
| wxGROW
, 5);
285 sizerRow
= CreateSizerWithTextAndButton(SpinBtnPage_SetMinAndMax
,
290 m_textMax
= new wxTextCtrl(this, SpinBtnPage_MaxText
, wxEmptyString
);
291 sizerRow
->Add(m_textMax
, 1, wxLEFT
| wxALIGN_CENTRE_VERTICAL
, 5);
293 m_textMin
->SetValue( wxString::Format(wxT("%d"), m_min
) );
294 m_textMax
->SetValue( wxString::Format(wxT("%d"), m_max
) );
296 sizerMiddle
->Add(sizerRow
, 0, wxALL
| wxGROW
, 5);
299 wxSizer
*sizerRight
= new wxBoxSizer(wxVERTICAL
);
300 sizerRight
->SetMinSize(150, 0);
301 m_sizerSpin
= sizerRight
; // save it to modify it later
306 // the 3 panes panes compose the window
307 sizerTop
->Add(sizerLeft
, 0, wxGROW
| (wxALL
& ~wxLEFT
), 10);
308 sizerTop
->Add(sizerMiddle
, 0, wxGROW
| wxALL
, 10);
309 sizerTop
->Add(sizerRight
, 1, wxGROW
| (wxALL
& ~wxRIGHT
), 10);
311 // final initializations
315 // ----------------------------------------------------------------------------
317 // ----------------------------------------------------------------------------
319 void SpinBtnWidgetsPage::Reset()
321 m_chkVert
->SetValue(true);
322 m_chkArrowKeys
->SetValue(true);
323 m_chkWrap
->SetValue(false);
324 m_chkProcessEnter
->SetValue(false);
325 m_radioAlign
->SetSelection(Align_Right
);
328 void SpinBtnWidgetsPage::CreateSpin()
330 int flags
= ms_defaultFlags
;
332 bool isVert
= m_chkVert
->GetValue();
334 flags
|= wxSP_VERTICAL
;
336 flags
|= wxSP_HORIZONTAL
;
338 if ( m_chkArrowKeys
->GetValue() )
339 flags
|= wxSP_ARROW_KEYS
;
341 if ( m_chkWrap
->GetValue() )
344 if ( m_chkProcessEnter
->GetValue() )
345 flags
|= wxTE_PROCESS_ENTER
;
348 switch ( m_radioAlign
->GetSelection() )
351 wxFAIL_MSG(wxT("unexpected radiobox selection"));
355 textFlags
|= wxALIGN_LEFT
; // no-op
359 textFlags
|= wxALIGN_CENTRE_HORIZONTAL
;
363 textFlags
|= wxALIGN_RIGHT
;
370 int valOld
= m_spinbtn
->GetValue();
371 if ( !IsValidValue(valOld
) )
376 m_sizerSpin
->Clear(true /* delete windows */);
379 m_spinbtn
= new wxSpinButton(this, SpinBtnPage_SpinBtn
,
380 wxDefaultPosition
, wxDefaultSize
,
383 m_spinbtn
->SetValue(val
);
384 m_spinbtn
->SetRange(m_min
, m_max
);
386 m_spinctrl
= new wxSpinCtrl(this, SpinBtnPage_SpinCtrl
,
387 wxString::Format(wxT("%d"), val
),
388 wxDefaultPosition
, wxDefaultSize
,
392 m_spinctrldbl
= new wxSpinCtrlDouble(this, SpinBtnPage_SpinCtrlDouble
,
393 wxString::Format(wxT("%d"), val
),
394 wxDefaultPosition
, wxDefaultSize
,
396 m_min
, m_max
, val
, 0.1);
398 // Add spacers, labels and spin controls to the sizer.
399 m_sizerSpin
->Add(0, 0, 1);
400 m_sizerSpin
->Add(new wxStaticText(this, wxID_ANY
, wxT("wxSpinButton")),
401 0, wxALIGN_CENTRE
| wxALL
, 5);
402 m_sizerSpin
->Add(m_spinbtn
, 0, wxALIGN_CENTRE
| wxALL
, 5);
403 m_sizerSpin
->Add(0, 0, 1);
404 m_sizerSpin
->Add(new wxStaticText(this, wxID_ANY
, wxT("wxSpinCtrl")),
405 0, wxALIGN_CENTRE
| wxALL
, 5);
406 m_sizerSpin
->Add(m_spinctrl
, 0, wxALIGN_CENTRE
| wxALL
, 5);
407 m_sizerSpin
->Add(0, 0, 1);
408 m_sizerSpin
->Add(new wxStaticText(this, wxID_ANY
, wxT("wxSpinCtrlDouble")),
409 0, wxALIGN_CENTRE
| wxALL
, 5);
410 m_sizerSpin
->Add(m_spinctrldbl
, 0, wxALIGN_CENTRE
| wxALL
, 5);
411 m_sizerSpin
->Add(0, 0, 1);
413 m_sizerSpin
->Layout();
416 // ----------------------------------------------------------------------------
418 // ----------------------------------------------------------------------------
420 void SpinBtnWidgetsPage::OnButtonReset(wxCommandEvent
& WXUNUSED(event
))
427 void SpinBtnWidgetsPage::OnButtonSetMinAndMax(wxCommandEvent
& WXUNUSED(event
))
430 maxNew
= 0; // init to suppress compiler warning
431 if ( !m_textMin
->GetValue().ToLong(&minNew
) ||
432 !m_textMax
->GetValue().ToLong(&maxNew
) ||
435 wxLogWarning(wxT("Invalid min/max values for the spinbtn."));
443 m_spinbtn
->SetRange(minNew
, maxNew
);
444 m_spinctrl
->SetRange(minNew
, maxNew
);
445 m_spinctrldbl
->SetRange(minNew
, maxNew
);
448 void SpinBtnWidgetsPage::OnButtonSetValue(wxCommandEvent
& WXUNUSED(event
))
451 if ( !m_textValue
->GetValue().ToLong(&val
) || !IsValidValue(val
) )
453 wxLogWarning(wxT("Invalid spinbtn value."));
458 m_spinbtn
->SetValue(val
);
459 m_spinctrl
->SetValue(val
);
460 m_spinctrldbl
->SetValue(val
);
463 void SpinBtnWidgetsPage::OnUpdateUIValueButton(wxUpdateUIEvent
& event
)
466 event
.Enable( m_textValue
->GetValue().ToLong(&val
) && IsValidValue(val
) );
469 void SpinBtnWidgetsPage::OnUpdateUIMinMaxButton(wxUpdateUIEvent
& event
)
472 event
.Enable( m_textMin
->GetValue().ToLong(&mn
) &&
473 m_textMax
->GetValue().ToLong(&mx
) &&
477 void SpinBtnWidgetsPage::OnUpdateUIResetButton(wxUpdateUIEvent
& event
)
479 event
.Enable( !m_chkVert
->GetValue() ||
480 m_chkWrap
->GetValue() ||
481 m_chkProcessEnter
->GetValue() );
484 void SpinBtnWidgetsPage::OnCheckOrRadioBox(wxCommandEvent
& WXUNUSED(event
))
489 void SpinBtnWidgetsPage::OnUpdateUICurValueText(wxUpdateUIEvent
& event
)
491 event
.SetText( wxString::Format(wxT("%d"), m_spinbtn
->GetValue()));
494 void SpinBtnWidgetsPage::OnSpinBtn(wxSpinEvent
& event
)
496 int value
= event
.GetInt();
498 wxASSERT_MSG( value
== m_spinbtn
->GetValue(),
499 wxT("spinbtn value should be the same") );
501 wxLogMessage(wxT("Spin button value changed, now %d"), value
);
504 void SpinBtnWidgetsPage::OnSpinBtnUp(wxSpinEvent
& event
)
506 wxLogMessage( wxT("Spin button value incremented, will be %d (was %d)"),
507 event
.GetInt(), m_spinbtn
->GetValue() );
510 void SpinBtnWidgetsPage::OnSpinBtnDown(wxSpinEvent
& event
)
512 wxLogMessage( wxT("Spin button value decremented, will be %d (was %d)"),
513 event
.GetInt(), m_spinbtn
->GetValue() );
516 void SpinBtnWidgetsPage::OnSpinCtrl(wxSpinEvent
& event
)
518 int value
= event
.GetInt();
520 wxASSERT_MSG( value
== m_spinctrl
->GetValue(),
521 wxT("spinctrl value should be the same") );
523 wxLogMessage(wxT("Spin control value changed, now %d"), value
);
526 void SpinBtnWidgetsPage::OnSpinCtrlDouble(wxSpinDoubleEvent
& event
)
528 double value
= event
.GetValue();
530 wxLogMessage(wxT("Spin control value changed, now %g"), value
);
533 void SpinBtnWidgetsPage::OnSpinText(wxCommandEvent
& event
)
535 wxLogMessage(wxT("Text changed in spin control, now \"%s\""),
536 event
.GetString().c_str());
539 void SpinBtnWidgetsPage::OnSpinTextEnter(wxCommandEvent
& event
)
541 wxLogMessage("\"Enter\" pressed in spin control, text is \"%s\"",
545 #endif // wxUSE_SPINBTN