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 wxControl
*GetWidget2() const { return m_spinctrl
; }
90 virtual wxControl
*GetWidget3() const { return m_spinctrldbl
; }
91 virtual void RecreateWidget() { CreateSpin(); }
93 // lazy creation of the content
94 virtual void CreateContent();
98 void OnButtonReset(wxCommandEvent
& event
);
99 void OnButtonClear(wxCommandEvent
& event
);
100 void OnButtonSetValue(wxCommandEvent
& event
);
101 void OnButtonSetMinAndMax(wxCommandEvent
& event
);
103 void OnCheckOrRadioBox(wxCommandEvent
& event
);
105 void OnSpinBtn(wxSpinEvent
& event
);
106 void OnSpinBtnUp(wxSpinEvent
& event
);
107 void OnSpinBtnDown(wxSpinEvent
& event
);
108 void OnSpinCtrl(wxSpinEvent
& event
);
109 void OnSpinCtrlDouble(wxSpinDoubleEvent
& event
);
110 void OnSpinText(wxCommandEvent
& event
);
111 void OnSpinTextEnter(wxCommandEvent
& event
);
113 void OnUpdateUIValueButton(wxUpdateUIEvent
& event
);
114 void OnUpdateUIMinMaxButton(wxUpdateUIEvent
& event
);
116 void OnUpdateUIResetButton(wxUpdateUIEvent
& event
);
118 void OnUpdateUICurValueText(wxUpdateUIEvent
& event
);
120 // reset the spinbtn parameters
123 // (re)create the spinbtn
126 // is this spinbtn value in range?
127 bool IsValidValue(int val
) const
128 { return (val
>= m_min
) && (val
<= m_max
); }
136 // the check/radio boxes for styles
137 wxCheckBox
*m_chkVert
,
141 wxRadioBox
*m_radioAlign
;
143 // the spinbtn and the spinctrl and the sizer containing them
144 wxSpinButton
*m_spinbtn
;
145 wxSpinCtrl
*m_spinctrl
;
146 wxSpinCtrlDouble
*m_spinctrldbl
;
148 wxSizer
*m_sizerSpin
;
150 // the text entries for set value/range
151 wxTextCtrl
*m_textValue
,
156 DECLARE_EVENT_TABLE()
157 DECLARE_WIDGETS_PAGE(SpinBtnWidgetsPage
)
160 // ----------------------------------------------------------------------------
162 // ----------------------------------------------------------------------------
164 BEGIN_EVENT_TABLE(SpinBtnWidgetsPage
, WidgetsPage
)
165 EVT_BUTTON(SpinBtnPage_Reset
, SpinBtnWidgetsPage::OnButtonReset
)
166 EVT_BUTTON(SpinBtnPage_SetValue
, SpinBtnWidgetsPage::OnButtonSetValue
)
167 EVT_BUTTON(SpinBtnPage_SetMinAndMax
, SpinBtnWidgetsPage::OnButtonSetMinAndMax
)
169 EVT_UPDATE_UI(SpinBtnPage_SetValue
, SpinBtnWidgetsPage::OnUpdateUIValueButton
)
170 EVT_UPDATE_UI(SpinBtnPage_SetMinAndMax
, SpinBtnWidgetsPage::OnUpdateUIMinMaxButton
)
172 EVT_UPDATE_UI(SpinBtnPage_Reset
, SpinBtnWidgetsPage::OnUpdateUIResetButton
)
174 EVT_UPDATE_UI(SpinBtnPage_CurValueText
, SpinBtnWidgetsPage::OnUpdateUICurValueText
)
176 EVT_SPIN(SpinBtnPage_SpinBtn
, SpinBtnWidgetsPage::OnSpinBtn
)
177 EVT_SPIN_UP(SpinBtnPage_SpinBtn
, SpinBtnWidgetsPage::OnSpinBtnUp
)
178 EVT_SPIN_DOWN(SpinBtnPage_SpinBtn
, SpinBtnWidgetsPage::OnSpinBtnDown
)
179 EVT_SPINCTRL(SpinBtnPage_SpinCtrl
, SpinBtnWidgetsPage::OnSpinCtrl
)
180 EVT_SPINCTRLDOUBLE(SpinBtnPage_SpinCtrlDouble
, SpinBtnWidgetsPage::OnSpinCtrlDouble
)
181 EVT_TEXT(SpinBtnPage_SpinCtrl
, SpinBtnWidgetsPage::OnSpinText
)
182 EVT_TEXT_ENTER(SpinBtnPage_SpinCtrl
, SpinBtnWidgetsPage::OnSpinTextEnter
)
183 EVT_TEXT(SpinBtnPage_SpinCtrlDouble
, SpinBtnWidgetsPage::OnSpinText
)
185 EVT_CHECKBOX(wxID_ANY
, SpinBtnWidgetsPage::OnCheckOrRadioBox
)
186 EVT_RADIOBOX(wxID_ANY
, SpinBtnWidgetsPage::OnCheckOrRadioBox
)
189 // ============================================================================
191 // ============================================================================
193 #if defined(__WXUNIVERSAL__)
194 #define FAMILY_CTRLS UNIVERSAL_CTRLS
196 #define FAMILY_CTRLS NATIVE_CTRLS
199 IMPLEMENT_WIDGETS_PAGE(SpinBtnWidgetsPage
, wxT("Spin"),
200 FAMILY_CTRLS
| EDITABLE_CTRLS
203 SpinBtnWidgetsPage::SpinBtnWidgetsPage(WidgetsBookCtrl
*book
,
204 wxImageList
*imaglist
)
205 : WidgetsPage(book
, imaglist
, spinbtn_xpm
)
208 m_chkArrowKeys
= NULL
;
210 m_chkProcessEnter
= NULL
;
214 m_spinctrldbl
= NULL
;
225 void SpinBtnWidgetsPage::CreateContent()
227 wxSizer
*sizerTop
= new wxBoxSizer(wxHORIZONTAL
);
230 wxStaticBox
*box
= new wxStaticBox(this, wxID_ANY
, wxT("&Set style"));
231 wxSizer
*sizerLeft
= new wxStaticBoxSizer(box
, wxVERTICAL
);
233 m_chkVert
= CreateCheckBoxAndAddToSizer(sizerLeft
, wxT("&Vertical"));
234 m_chkArrowKeys
= CreateCheckBoxAndAddToSizer(sizerLeft
, wxT("&Arrow Keys"));
235 m_chkWrap
= CreateCheckBoxAndAddToSizer(sizerLeft
, wxT("&Wrap"));
236 m_chkProcessEnter
= CreateCheckBoxAndAddToSizer(sizerLeft
,
237 wxT("Process &Enter"));
239 sizerLeft
->Add(5, 5, 0, wxGROW
| wxALL
, 5); // spacer
241 static const wxString halign
[] =
248 m_radioAlign
= new wxRadioBox(this, wxID_ANY
, wxT("&Text alignment"),
249 wxDefaultPosition
, wxDefaultSize
,
250 WXSIZEOF(halign
), halign
, 1);
252 sizerLeft
->Add(m_radioAlign
, 0, wxGROW
| wxALL
, 5);
254 sizerLeft
->Add(5, 5, 0, wxGROW
| wxALL
, 5); // spacer
256 wxButton
*btn
= new wxButton(this, SpinBtnPage_Reset
, wxT("&Reset"));
257 sizerLeft
->Add(btn
, 0, wxALIGN_CENTRE_HORIZONTAL
| wxALL
, 15);
260 wxStaticBox
*box2
= new wxStaticBox(this, wxID_ANY
,
261 wxT("&Change spinbtn value"));
263 wxSizer
*sizerMiddle
= new wxStaticBoxSizer(box2
, wxVERTICAL
);
266 wxSizer
*sizerRow
= CreateSizerWithTextAndLabel(wxT("Current value"),
267 SpinBtnPage_CurValueText
,
269 text
->SetEditable(false);
271 sizerMiddle
->Add(sizerRow
, 0, wxALL
| wxGROW
, 5);
273 sizerRow
= CreateSizerWithTextAndButton(SpinBtnPage_SetValue
,
275 SpinBtnPage_ValueText
,
277 sizerMiddle
->Add(sizerRow
, 0, wxALL
| wxGROW
, 5);
279 sizerRow
= CreateSizerWithTextAndButton(SpinBtnPage_SetMinAndMax
,
284 m_textMax
= new wxTextCtrl(this, SpinBtnPage_MaxText
, wxEmptyString
);
285 sizerRow
->Add(m_textMax
, 1, wxLEFT
| wxALIGN_CENTRE_VERTICAL
, 5);
287 m_textMin
->SetValue( wxString::Format(wxT("%d"), m_min
) );
288 m_textMax
->SetValue( wxString::Format(wxT("%d"), m_max
) );
290 sizerMiddle
->Add(sizerRow
, 0, wxALL
| wxGROW
, 5);
293 wxSizer
*sizerRight
= new wxBoxSizer(wxVERTICAL
);
294 sizerRight
->SetMinSize(150, 0);
295 m_sizerSpin
= sizerRight
; // save it to modify it later
300 // the 3 panes panes compose the window
301 sizerTop
->Add(sizerLeft
, 0, wxGROW
| (wxALL
& ~wxLEFT
), 10);
302 sizerTop
->Add(sizerMiddle
, 0, wxGROW
| wxALL
, 10);
303 sizerTop
->Add(sizerRight
, 1, wxGROW
| (wxALL
& ~wxRIGHT
), 10);
305 // final initializations
309 // ----------------------------------------------------------------------------
311 // ----------------------------------------------------------------------------
313 void SpinBtnWidgetsPage::Reset()
315 m_chkVert
->SetValue(true);
316 m_chkArrowKeys
->SetValue(true);
317 m_chkWrap
->SetValue(false);
318 m_chkProcessEnter
->SetValue(false);
319 m_radioAlign
->SetSelection(Align_Right
);
322 void SpinBtnWidgetsPage::CreateSpin()
324 int flags
= ms_defaultFlags
;
326 bool isVert
= m_chkVert
->GetValue();
328 flags
|= wxSP_VERTICAL
;
330 flags
|= wxSP_HORIZONTAL
;
332 if ( m_chkArrowKeys
->GetValue() )
333 flags
|= wxSP_ARROW_KEYS
;
335 if ( m_chkWrap
->GetValue() )
338 if ( m_chkProcessEnter
->GetValue() )
339 flags
|= wxTE_PROCESS_ENTER
;
342 switch ( m_radioAlign
->GetSelection() )
345 wxFAIL_MSG(wxT("unexpected radiobox selection"));
349 textFlags
|= wxALIGN_LEFT
; // no-op
353 textFlags
|= wxALIGN_CENTRE_HORIZONTAL
;
357 textFlags
|= wxALIGN_RIGHT
;
364 int valOld
= m_spinbtn
->GetValue();
365 if ( !IsValidValue(valOld
) )
370 m_sizerSpin
->Clear(true /* delete windows */);
373 m_spinbtn
= new wxSpinButton(this, SpinBtnPage_SpinBtn
,
374 wxDefaultPosition
, wxDefaultSize
,
377 m_spinbtn
->SetValue(val
);
378 m_spinbtn
->SetRange(m_min
, m_max
);
380 m_spinctrl
= new wxSpinCtrl(this, SpinBtnPage_SpinCtrl
,
381 wxString::Format(wxT("%d"), val
),
382 wxDefaultPosition
, wxDefaultSize
,
386 m_spinctrldbl
= new wxSpinCtrlDouble(this, SpinBtnPage_SpinCtrlDouble
,
387 wxString::Format(wxT("%d"), val
),
388 wxDefaultPosition
, wxDefaultSize
,
390 m_min
, m_max
, val
, 0.1);
392 // Add spacers, labels and spin controls to the sizer.
393 m_sizerSpin
->Add(0, 0, 1);
394 m_sizerSpin
->Add(new wxStaticText(this, wxID_ANY
, wxT("wxSpinButton")),
395 0, wxALIGN_CENTRE
| wxALL
, 5);
396 m_sizerSpin
->Add(m_spinbtn
, 0, wxALIGN_CENTRE
| wxALL
, 5);
397 m_sizerSpin
->Add(0, 0, 1);
398 m_sizerSpin
->Add(new wxStaticText(this, wxID_ANY
, wxT("wxSpinCtrl")),
399 0, wxALIGN_CENTRE
| wxALL
, 5);
400 m_sizerSpin
->Add(m_spinctrl
, 0, wxALIGN_CENTRE
| wxALL
, 5);
401 m_sizerSpin
->Add(0, 0, 1);
402 m_sizerSpin
->Add(new wxStaticText(this, wxID_ANY
, wxT("wxSpinCtrlDouble")),
403 0, wxALIGN_CENTRE
| wxALL
, 5);
404 m_sizerSpin
->Add(m_spinctrldbl
, 0, wxALIGN_CENTRE
| wxALL
, 5);
405 m_sizerSpin
->Add(0, 0, 1);
407 m_sizerSpin
->Layout();
410 // ----------------------------------------------------------------------------
412 // ----------------------------------------------------------------------------
414 void SpinBtnWidgetsPage::OnButtonReset(wxCommandEvent
& WXUNUSED(event
))
421 void SpinBtnWidgetsPage::OnButtonSetMinAndMax(wxCommandEvent
& WXUNUSED(event
))
424 maxNew
= 0; // init to suppress compiler warning
425 if ( !m_textMin
->GetValue().ToLong(&minNew
) ||
426 !m_textMax
->GetValue().ToLong(&maxNew
) ||
429 wxLogWarning(wxT("Invalid min/max values for the spinbtn."));
437 m_spinbtn
->SetRange(minNew
, maxNew
);
438 m_spinctrl
->SetRange(minNew
, maxNew
);
439 m_spinctrldbl
->SetRange(minNew
, maxNew
);
442 void SpinBtnWidgetsPage::OnButtonSetValue(wxCommandEvent
& WXUNUSED(event
))
445 if ( !m_textValue
->GetValue().ToLong(&val
) || !IsValidValue(val
) )
447 wxLogWarning(wxT("Invalid spinbtn value."));
452 m_spinbtn
->SetValue(val
);
453 m_spinctrl
->SetValue(val
);
454 m_spinctrldbl
->SetValue(val
);
457 void SpinBtnWidgetsPage::OnUpdateUIValueButton(wxUpdateUIEvent
& event
)
460 event
.Enable( m_textValue
->GetValue().ToLong(&val
) && IsValidValue(val
) );
463 void SpinBtnWidgetsPage::OnUpdateUIMinMaxButton(wxUpdateUIEvent
& event
)
466 event
.Enable( m_textMin
->GetValue().ToLong(&mn
) &&
467 m_textMax
->GetValue().ToLong(&mx
) &&
471 void SpinBtnWidgetsPage::OnUpdateUIResetButton(wxUpdateUIEvent
& event
)
473 event
.Enable( !m_chkVert
->GetValue() ||
474 m_chkWrap
->GetValue() ||
475 m_chkProcessEnter
->GetValue() );
478 void SpinBtnWidgetsPage::OnCheckOrRadioBox(wxCommandEvent
& WXUNUSED(event
))
483 void SpinBtnWidgetsPage::OnUpdateUICurValueText(wxUpdateUIEvent
& event
)
485 event
.SetText( wxString::Format(wxT("%d"), m_spinbtn
->GetValue()));
488 void SpinBtnWidgetsPage::OnSpinBtn(wxSpinEvent
& event
)
490 int value
= event
.GetInt();
492 wxASSERT_MSG( value
== m_spinbtn
->GetValue(),
493 wxT("spinbtn value should be the same") );
495 wxLogMessage(wxT("Spin button value changed, now %d"), value
);
498 void SpinBtnWidgetsPage::OnSpinBtnUp(wxSpinEvent
& event
)
500 wxLogMessage( wxT("Spin button value incremented, will be %d (was %d)"),
501 event
.GetInt(), m_spinbtn
->GetValue() );
504 void SpinBtnWidgetsPage::OnSpinBtnDown(wxSpinEvent
& event
)
506 wxLogMessage( wxT("Spin button value decremented, will be %d (was %d)"),
507 event
.GetInt(), m_spinbtn
->GetValue() );
510 void SpinBtnWidgetsPage::OnSpinCtrl(wxSpinEvent
& event
)
512 int value
= event
.GetInt();
514 wxASSERT_MSG( value
== m_spinctrl
->GetValue(),
515 wxT("spinctrl value should be the same") );
517 wxLogMessage(wxT("Spin control value changed, now %d"), value
);
520 void SpinBtnWidgetsPage::OnSpinCtrlDouble(wxSpinDoubleEvent
& event
)
522 double value
= event
.GetValue();
524 wxLogMessage(wxT("Spin control value changed, now %g"), value
);
527 void SpinBtnWidgetsPage::OnSpinText(wxCommandEvent
& event
)
529 wxLogMessage(wxT("Text changed in spin control, now \"%s\""),
530 event
.GetString().c_str());
533 void SpinBtnWidgetsPage::OnSpinTextEnter(wxCommandEvent
& event
)
535 wxLogMessage("\"Enter\" pressed in spin control, text is \"%s\"",
539 #endif // wxUSE_SPINBTN