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"
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 // ----------------------------------------------------------------------------
72 // ----------------------------------------------------------------------------
74 class SpinBtnWidgetsPage
: public WidgetsPage
77 SpinBtnWidgetsPage(WidgetsBookCtrl
*book
, wxImageList
*imaglist
);
78 virtual ~SpinBtnWidgetsPage(){};
80 virtual wxControl
*GetWidget() const { return m_spinbtn
; }
81 virtual wxControl
*GetWidget2() const { return m_spinctrl
; }
82 virtual wxControl
*GetWidget3() const { return m_spinctrldbl
; }
83 virtual void RecreateWidget() { CreateSpin(); }
85 // lazy creation of the content
86 virtual void CreateContent();
90 void OnButtonReset(wxCommandEvent
& event
);
91 void OnButtonClear(wxCommandEvent
& event
);
92 void OnButtonSetValue(wxCommandEvent
& event
);
93 void OnButtonSetMinAndMax(wxCommandEvent
& event
);
95 void OnCheckOrRadioBox(wxCommandEvent
& event
);
97 void OnSpinBtn(wxSpinEvent
& event
);
98 void OnSpinBtnUp(wxSpinEvent
& event
);
99 void OnSpinBtnDown(wxSpinEvent
& event
);
100 void OnSpinCtrl(wxSpinEvent
& event
);
101 void OnSpinCtrlDouble(wxSpinDoubleEvent
& event
);
102 void OnSpinText(wxCommandEvent
& event
);
103 void OnSpinTextEnter(wxCommandEvent
& event
);
105 void OnUpdateUIValueButton(wxUpdateUIEvent
& event
);
106 void OnUpdateUIMinMaxButton(wxUpdateUIEvent
& event
);
108 void OnUpdateUIResetButton(wxUpdateUIEvent
& event
);
110 void OnUpdateUICurValueText(wxUpdateUIEvent
& event
);
112 // reset the spinbtn parameters
115 // (re)create the spinbtn
118 // is this spinbtn value in range?
119 bool IsValidValue(int val
) const
120 { return (val
>= m_min
) && (val
<= m_max
); }
128 // the check/radio boxes for styles
129 wxCheckBox
*m_chkVert
,
132 // the spinbtn and the spinctrl and the sizer containing them
133 wxSpinButton
*m_spinbtn
;
134 wxSpinCtrl
*m_spinctrl
;
135 wxSpinCtrlDouble
*m_spinctrldbl
;
137 wxSizer
*m_sizerSpin
;
139 // the text entries for set value/range
140 wxTextCtrl
*m_textValue
,
145 DECLARE_EVENT_TABLE()
146 DECLARE_WIDGETS_PAGE(SpinBtnWidgetsPage
)
149 // ----------------------------------------------------------------------------
151 // ----------------------------------------------------------------------------
153 BEGIN_EVENT_TABLE(SpinBtnWidgetsPage
, WidgetsPage
)
154 EVT_BUTTON(SpinBtnPage_Reset
, SpinBtnWidgetsPage::OnButtonReset
)
155 EVT_BUTTON(SpinBtnPage_SetValue
, SpinBtnWidgetsPage::OnButtonSetValue
)
156 EVT_BUTTON(SpinBtnPage_SetMinAndMax
, SpinBtnWidgetsPage::OnButtonSetMinAndMax
)
158 EVT_UPDATE_UI(SpinBtnPage_SetValue
, SpinBtnWidgetsPage::OnUpdateUIValueButton
)
159 EVT_UPDATE_UI(SpinBtnPage_SetMinAndMax
, SpinBtnWidgetsPage::OnUpdateUIMinMaxButton
)
161 EVT_UPDATE_UI(SpinBtnPage_Reset
, SpinBtnWidgetsPage::OnUpdateUIResetButton
)
163 EVT_UPDATE_UI(SpinBtnPage_CurValueText
, SpinBtnWidgetsPage::OnUpdateUICurValueText
)
165 EVT_SPIN(SpinBtnPage_SpinBtn
, SpinBtnWidgetsPage::OnSpinBtn
)
166 EVT_SPIN_UP(SpinBtnPage_SpinBtn
, SpinBtnWidgetsPage::OnSpinBtnUp
)
167 EVT_SPIN_DOWN(SpinBtnPage_SpinBtn
, SpinBtnWidgetsPage::OnSpinBtnDown
)
168 EVT_SPINCTRL(SpinBtnPage_SpinCtrl
, SpinBtnWidgetsPage::OnSpinCtrl
)
169 EVT_SPINCTRLDOUBLE(SpinBtnPage_SpinCtrlDouble
, SpinBtnWidgetsPage::OnSpinCtrlDouble
)
170 EVT_TEXT(SpinBtnPage_SpinCtrl
, SpinBtnWidgetsPage::OnSpinText
)
171 EVT_TEXT_ENTER(SpinBtnPage_SpinCtrl
, SpinBtnWidgetsPage::OnSpinTextEnter
)
172 EVT_TEXT(SpinBtnPage_SpinCtrlDouble
, SpinBtnWidgetsPage::OnSpinText
)
174 EVT_CHECKBOX(wxID_ANY
, SpinBtnWidgetsPage::OnCheckOrRadioBox
)
175 EVT_RADIOBOX(wxID_ANY
, SpinBtnWidgetsPage::OnCheckOrRadioBox
)
178 // ============================================================================
180 // ============================================================================
182 #if defined(__WXUNIVERSAL__)
183 #define FAMILY_CTRLS UNIVERSAL_CTRLS
185 #define FAMILY_CTRLS NATIVE_CTRLS
188 IMPLEMENT_WIDGETS_PAGE(SpinBtnWidgetsPage
, _T("Spin"),
189 FAMILY_CTRLS
| EDITABLE_CTRLS
192 SpinBtnWidgetsPage::SpinBtnWidgetsPage(WidgetsBookCtrl
*book
,
193 wxImageList
*imaglist
)
194 : WidgetsPage(book
, imaglist
, spinbtn_xpm
)
200 m_spinctrldbl
= NULL
;
210 m_chkWrap
= (wxCheckBox
*)NULL
;
212 m_spinbtn
= (wxSpinButton
*)NULL
;
213 m_sizerSpin
= (wxSizer
*)NULL
;
216 void SpinBtnWidgetsPage::CreateContent()
218 wxSizer
*sizerTop
= new wxBoxSizer(wxHORIZONTAL
);
221 wxStaticBox
*box
= new wxStaticBox(this, wxID_ANY
, _T("&Set style"));
222 wxSizer
*sizerLeft
= new wxStaticBoxSizer(box
, wxVERTICAL
);
224 m_chkVert
= CreateCheckBoxAndAddToSizer(sizerLeft
, _T("&Vertical"));
225 m_chkWrap
= CreateCheckBoxAndAddToSizer(sizerLeft
, _T("&Wrap"));
227 sizerLeft
->Add(5, 5, 0, wxGROW
| wxALL
, 5); // spacer
229 wxButton
*btn
= new wxButton(this, SpinBtnPage_Reset
, _T("&Reset"));
230 sizerLeft
->Add(btn
, 0, wxALIGN_CENTRE_HORIZONTAL
| wxALL
, 15);
233 wxStaticBox
*box2
= new wxStaticBox(this, wxID_ANY
,
234 _T("&Change spinbtn value"));
236 wxSizer
*sizerMiddle
= new wxStaticBoxSizer(box2
, wxVERTICAL
);
239 wxSizer
*sizerRow
= CreateSizerWithTextAndLabel(_T("Current value"),
240 SpinBtnPage_CurValueText
,
242 text
->SetEditable(false);
244 sizerMiddle
->Add(sizerRow
, 0, wxALL
| wxGROW
, 5);
246 sizerRow
= CreateSizerWithTextAndButton(SpinBtnPage_SetValue
,
248 SpinBtnPage_ValueText
,
250 sizerMiddle
->Add(sizerRow
, 0, wxALL
| wxGROW
, 5);
252 sizerRow
= CreateSizerWithTextAndButton(SpinBtnPage_SetMinAndMax
,
257 m_textMax
= new wxTextCtrl(this, SpinBtnPage_MaxText
, wxEmptyString
);
258 sizerRow
->Add(m_textMax
, 1, wxLEFT
| wxALIGN_CENTRE_VERTICAL
, 5);
260 m_textMin
->SetValue( wxString::Format(_T("%d"), m_min
) );
261 m_textMax
->SetValue( wxString::Format(_T("%d"), m_max
) );
263 sizerMiddle
->Add(sizerRow
, 0, wxALL
| wxGROW
, 5);
266 wxSizer
*sizerRight
= new wxBoxSizer(wxVERTICAL
);
267 sizerRight
->SetMinSize(150, 0);
268 m_sizerSpin
= sizerRight
; // save it to modify it later
273 // the 3 panes panes compose the window
274 sizerTop
->Add(sizerLeft
, 0, wxGROW
| (wxALL
& ~wxLEFT
), 10);
275 sizerTop
->Add(sizerMiddle
, 0, wxGROW
| wxALL
, 10);
276 sizerTop
->Add(sizerRight
, 1, wxGROW
| (wxALL
& ~wxRIGHT
), 10);
278 // final initializations
282 // ----------------------------------------------------------------------------
284 // ----------------------------------------------------------------------------
286 void SpinBtnWidgetsPage::Reset()
288 m_chkVert
->SetValue(true);
289 m_chkWrap
->SetValue(false);
292 void SpinBtnWidgetsPage::CreateSpin()
294 int flags
= ms_defaultFlags
;
296 bool isVert
= m_chkVert
->GetValue();
298 flags
|= wxSP_VERTICAL
;
300 flags
|= wxSP_HORIZONTAL
;
302 if ( m_chkWrap
->GetValue() )
308 int valOld
= m_spinbtn
->GetValue();
309 if ( !IsValidValue(valOld
) )
314 m_sizerSpin
->Detach( m_spinbtn
);
315 m_sizerSpin
->Detach( m_spinctrl
);
316 m_sizerSpin
->Detach( m_spinctrldbl
);
318 // there are 4 spacers left
319 m_sizerSpin
->Remove( 0 );
320 m_sizerSpin
->Remove( 0 );
321 m_sizerSpin
->Remove( 0 );
322 m_sizerSpin
->Remove( 0 );
326 delete m_spinctrldbl
;
329 m_spinbtn
= new wxSpinButton(this, SpinBtnPage_SpinBtn
,
330 wxDefaultPosition
, wxDefaultSize
,
333 m_spinbtn
->SetValue(val
);
334 m_spinbtn
->SetRange(m_min
, m_max
);
336 m_spinctrl
= new wxSpinCtrl(this, SpinBtnPage_SpinCtrl
,
337 wxString::Format(_T("%d"), val
),
338 wxDefaultPosition
, wxDefaultSize
,
342 m_spinctrldbl
= new wxSpinCtrlDouble(this, SpinBtnPage_SpinCtrlDouble
,
343 wxString::Format(_T("%d"), val
),
344 wxDefaultPosition
, wxDefaultSize
,
346 m_min
, m_max
, val
, 0.1);
348 m_sizerSpin
->Add(0, 0, 1);
349 m_sizerSpin
->Add(m_spinbtn
, 0, wxALIGN_CENTRE
| wxALL
, 5);
350 m_sizerSpin
->Add(0, 0, 1);
351 m_sizerSpin
->Add(m_spinctrl
, 0, wxALIGN_CENTRE
| wxALL
, 5);
352 m_sizerSpin
->Add(0, 0, 1);
353 m_sizerSpin
->Add(m_spinctrldbl
, 0, wxALIGN_CENTRE
| wxALL
, 5);
354 m_sizerSpin
->Add(0, 0, 1);
356 m_sizerSpin
->Layout();
359 // ----------------------------------------------------------------------------
361 // ----------------------------------------------------------------------------
363 void SpinBtnWidgetsPage::OnButtonReset(wxCommandEvent
& WXUNUSED(event
))
370 void SpinBtnWidgetsPage::OnButtonSetMinAndMax(wxCommandEvent
& WXUNUSED(event
))
373 maxNew
= 0; // init to suppress compiler warning
374 if ( !m_textMin
->GetValue().ToLong(&minNew
) ||
375 !m_textMax
->GetValue().ToLong(&maxNew
) ||
378 wxLogWarning(_T("Invalid min/max values for the spinbtn."));
386 m_spinbtn
->SetRange(minNew
, maxNew
);
387 m_spinctrl
->SetRange(minNew
, maxNew
);
388 m_spinctrldbl
->SetRange(minNew
, maxNew
);
391 void SpinBtnWidgetsPage::OnButtonSetValue(wxCommandEvent
& WXUNUSED(event
))
394 if ( !m_textValue
->GetValue().ToLong(&val
) || !IsValidValue(val
) )
396 wxLogWarning(_T("Invalid spinbtn value."));
401 m_spinbtn
->SetValue(val
);
402 m_spinctrl
->SetValue(val
);
403 m_spinctrldbl
->SetValue(val
);
406 void SpinBtnWidgetsPage::OnUpdateUIValueButton(wxUpdateUIEvent
& event
)
409 event
.Enable( m_textValue
->GetValue().ToLong(&val
) && IsValidValue(val
) );
412 void SpinBtnWidgetsPage::OnUpdateUIMinMaxButton(wxUpdateUIEvent
& event
)
415 event
.Enable( m_textMin
->GetValue().ToLong(&mn
) &&
416 m_textMax
->GetValue().ToLong(&mx
) &&
420 void SpinBtnWidgetsPage::OnUpdateUIResetButton(wxUpdateUIEvent
& event
)
422 event
.Enable( !m_chkVert
->GetValue() || m_chkWrap
->GetValue() );
425 void SpinBtnWidgetsPage::OnCheckOrRadioBox(wxCommandEvent
& WXUNUSED(event
))
430 void SpinBtnWidgetsPage::OnUpdateUICurValueText(wxUpdateUIEvent
& event
)
432 event
.SetText( wxString::Format(_T("%d"), m_spinbtn
->GetValue()));
435 void SpinBtnWidgetsPage::OnSpinBtn(wxSpinEvent
& event
)
437 int value
= event
.GetInt();
439 wxASSERT_MSG( value
== m_spinbtn
->GetValue(),
440 _T("spinbtn value should be the same") );
442 wxLogMessage(_T("Spin button value changed, now %d"), value
);
445 void SpinBtnWidgetsPage::OnSpinBtnUp(wxSpinEvent
& event
)
447 wxLogMessage( _T("Spin button value incremented, will be %d (was %d)"),
448 event
.GetInt(), m_spinbtn
->GetValue() );
451 void SpinBtnWidgetsPage::OnSpinBtnDown(wxSpinEvent
& event
)
453 wxLogMessage( _T("Spin button value decremented, will be %d (was %d)"),
454 event
.GetInt(), m_spinbtn
->GetValue() );
457 void SpinBtnWidgetsPage::OnSpinCtrl(wxSpinEvent
& event
)
459 int value
= event
.GetInt();
461 wxASSERT_MSG( value
== m_spinctrl
->GetValue(),
462 _T("spinctrl value should be the same") );
464 wxLogMessage(_T("Spin control value changed, now %d"), value
);
467 void SpinBtnWidgetsPage::OnSpinCtrlDouble(wxSpinDoubleEvent
& event
)
469 double value
= event
.GetValue();
471 wxLogMessage(_T("Spin control value changed, now %g"), value
);
474 void SpinBtnWidgetsPage::OnSpinText(wxCommandEvent
& event
)
476 wxLogMessage(_T("Text changed in spin control, now \"%s\""),
477 event
.GetString().c_str());
480 void SpinBtnWidgetsPage::OnSpinTextEnter(wxCommandEvent
& event
)
482 wxLogMessage("\"Enter\" pressed in spin control, text is \"%s\"",
486 #endif // wxUSE_SPINBTN