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
,
69 // ----------------------------------------------------------------------------
71 // ----------------------------------------------------------------------------
73 class SpinBtnWidgetsPage
: public WidgetsPage
76 SpinBtnWidgetsPage(WidgetsBookCtrl
*book
, wxImageList
*imaglist
);
77 virtual ~SpinBtnWidgetsPage(){};
79 virtual wxControl
*GetWidget() const { return m_spinbtn
; }
80 virtual wxControl
*GetWidget2() const { return m_spinctrl
; }
81 virtual void RecreateWidget() { CreateSpin(); }
83 // lazy creation of the content
84 virtual void CreateContent();
88 void OnButtonReset(wxCommandEvent
& event
);
89 void OnButtonClear(wxCommandEvent
& event
);
90 void OnButtonSetValue(wxCommandEvent
& event
);
91 void OnButtonSetMinAndMax(wxCommandEvent
& event
);
93 void OnCheckOrRadioBox(wxCommandEvent
& event
);
95 void OnSpinBtn(wxSpinEvent
& event
);
96 void OnSpinBtnUp(wxSpinEvent
& event
);
97 void OnSpinBtnDown(wxSpinEvent
& event
);
98 void OnSpinCtrl(wxSpinEvent
& event
);
100 void OnUpdateUIValueButton(wxUpdateUIEvent
& event
);
101 void OnUpdateUIMinMaxButton(wxUpdateUIEvent
& event
);
103 void OnUpdateUIResetButton(wxUpdateUIEvent
& event
);
105 void OnUpdateUICurValueText(wxUpdateUIEvent
& event
);
107 // reset the spinbtn parameters
110 // (re)create the spinbtn
113 // is this spinbtn value in range?
114 bool IsValidValue(int val
) const
115 { return (val
>= m_min
) && (val
<= m_max
); }
123 // the check/radio boxes for styles
124 wxCheckBox
*m_chkVert
,
127 // the spinbtn and the spinctrl and the sizer containing them
128 wxSpinButton
*m_spinbtn
;
129 wxSpinCtrl
*m_spinctrl
;
131 wxSizer
*m_sizerSpin
;
133 // the text entries for set value/range
134 wxTextCtrl
*m_textValue
,
139 DECLARE_EVENT_TABLE()
140 DECLARE_WIDGETS_PAGE(SpinBtnWidgetsPage
)
143 // ----------------------------------------------------------------------------
145 // ----------------------------------------------------------------------------
147 BEGIN_EVENT_TABLE(SpinBtnWidgetsPage
, WidgetsPage
)
148 EVT_BUTTON(SpinBtnPage_Reset
, SpinBtnWidgetsPage::OnButtonReset
)
149 EVT_BUTTON(SpinBtnPage_SetValue
, SpinBtnWidgetsPage::OnButtonSetValue
)
150 EVT_BUTTON(SpinBtnPage_SetMinAndMax
, SpinBtnWidgetsPage::OnButtonSetMinAndMax
)
152 EVT_UPDATE_UI(SpinBtnPage_SetValue
, SpinBtnWidgetsPage::OnUpdateUIValueButton
)
153 EVT_UPDATE_UI(SpinBtnPage_SetMinAndMax
, SpinBtnWidgetsPage::OnUpdateUIMinMaxButton
)
155 EVT_UPDATE_UI(SpinBtnPage_Reset
, SpinBtnWidgetsPage::OnUpdateUIResetButton
)
157 EVT_UPDATE_UI(SpinBtnPage_CurValueText
, SpinBtnWidgetsPage::OnUpdateUICurValueText
)
159 EVT_SPIN(SpinBtnPage_SpinBtn
, SpinBtnWidgetsPage::OnSpinBtn
)
160 EVT_SPIN_UP(SpinBtnPage_SpinBtn
, SpinBtnWidgetsPage::OnSpinBtnUp
)
161 EVT_SPIN_DOWN(SpinBtnPage_SpinBtn
, SpinBtnWidgetsPage::OnSpinBtnDown
)
162 EVT_SPINCTRL(SpinBtnPage_SpinCtrl
, SpinBtnWidgetsPage::OnSpinCtrl
)
164 EVT_CHECKBOX(wxID_ANY
, SpinBtnWidgetsPage::OnCheckOrRadioBox
)
165 EVT_RADIOBOX(wxID_ANY
, SpinBtnWidgetsPage::OnCheckOrRadioBox
)
168 // ============================================================================
170 // ============================================================================
172 #if defined(__WXUNIVERSAL__)
173 #define FAMILY_CTRLS UNIVERSAL_CTRLS
175 #define FAMILY_CTRLS NATIVE_CTRLS
178 IMPLEMENT_WIDGETS_PAGE(SpinBtnWidgetsPage
, _T("Spin"),
179 FAMILY_CTRLS
| EDITABLE_CTRLS
182 SpinBtnWidgetsPage::SpinBtnWidgetsPage(WidgetsBookCtrl
*book
,
183 wxImageList
*imaglist
)
184 : WidgetsPage(book
, imaglist
, spinbtn_xpm
)
199 m_chkWrap
= (wxCheckBox
*)NULL
;
201 m_spinbtn
= (wxSpinButton
*)NULL
;
202 m_sizerSpin
= (wxSizer
*)NULL
;
205 void SpinBtnWidgetsPage::CreateContent()
207 wxSizer
*sizerTop
= new wxBoxSizer(wxHORIZONTAL
);
210 wxStaticBox
*box
= new wxStaticBox(this, wxID_ANY
, _T("&Set style"));
211 wxSizer
*sizerLeft
= new wxStaticBoxSizer(box
, wxVERTICAL
);
213 m_chkVert
= CreateCheckBoxAndAddToSizer(sizerLeft
, _T("&Vertical"));
214 m_chkWrap
= CreateCheckBoxAndAddToSizer(sizerLeft
, _T("&Wrap"));
216 sizerLeft
->Add(5, 5, 0, wxGROW
| wxALL
, 5); // spacer
218 wxButton
*btn
= new wxButton(this, SpinBtnPage_Reset
, _T("&Reset"));
219 sizerLeft
->Add(btn
, 0, wxALIGN_CENTRE_HORIZONTAL
| wxALL
, 15);
222 wxStaticBox
*box2
= new wxStaticBox(this, wxID_ANY
,
223 _T("&Change spinbtn value"));
225 wxSizer
*sizerMiddle
= new wxStaticBoxSizer(box2
, wxVERTICAL
);
228 wxSizer
*sizerRow
= CreateSizerWithTextAndLabel(_T("Current value"),
229 SpinBtnPage_CurValueText
,
231 text
->SetEditable(false);
233 sizerMiddle
->Add(sizerRow
, 0, wxALL
| wxGROW
, 5);
235 sizerRow
= CreateSizerWithTextAndButton(SpinBtnPage_SetValue
,
237 SpinBtnPage_ValueText
,
239 sizerMiddle
->Add(sizerRow
, 0, wxALL
| wxGROW
, 5);
241 sizerRow
= CreateSizerWithTextAndButton(SpinBtnPage_SetMinAndMax
,
246 m_textMax
= new wxTextCtrl(this, SpinBtnPage_MaxText
, wxEmptyString
);
247 sizerRow
->Add(m_textMax
, 1, wxLEFT
| wxALIGN_CENTRE_VERTICAL
, 5);
249 m_textMin
->SetValue( wxString::Format(_T("%d"), m_min
) );
250 m_textMax
->SetValue( wxString::Format(_T("%d"), m_max
) );
252 sizerMiddle
->Add(sizerRow
, 0, wxALL
| wxGROW
, 5);
255 wxSizer
*sizerRight
= new wxBoxSizer(wxVERTICAL
);
256 sizerRight
->SetMinSize(150, 0);
257 m_sizerSpin
= sizerRight
; // save it to modify it later
262 // the 3 panes panes compose the window
263 sizerTop
->Add(sizerLeft
, 0, wxGROW
| (wxALL
& ~wxLEFT
), 10);
264 sizerTop
->Add(sizerMiddle
, 0, wxGROW
| wxALL
, 10);
265 sizerTop
->Add(sizerRight
, 1, wxGROW
| (wxALL
& ~wxRIGHT
), 10);
267 // final initializations
273 // ----------------------------------------------------------------------------
275 // ----------------------------------------------------------------------------
277 void SpinBtnWidgetsPage::Reset()
279 m_chkVert
->SetValue(true);
280 m_chkWrap
->SetValue(false);
283 void SpinBtnWidgetsPage::CreateSpin()
285 int flags
= ms_defaultFlags
;
287 bool isVert
= m_chkVert
->GetValue();
289 flags
|= wxSP_VERTICAL
;
291 flags
|= wxSP_HORIZONTAL
;
293 if ( m_chkWrap
->GetValue() )
299 int valOld
= m_spinbtn
->GetValue();
300 if ( !IsValidValue(valOld
) )
305 m_sizerSpin
->Detach( m_spinbtn
);
306 m_sizerSpin
->Detach( m_spinctrl
);
308 // there are 3 spacers left
309 m_sizerSpin
->Remove( 0 );
310 m_sizerSpin
->Remove( 0 );
311 m_sizerSpin
->Remove( 0 );
317 m_spinbtn
= new wxSpinButton(this, SpinBtnPage_SpinBtn
,
318 wxDefaultPosition
, wxDefaultSize
,
321 m_spinbtn
->SetValue(val
);
322 m_spinbtn
->SetRange(m_min
, m_max
);
324 m_spinctrl
= new wxSpinCtrl(this, SpinBtnPage_SpinCtrl
,
325 wxString::Format(_T("%d"), val
),
326 wxDefaultPosition
, wxDefaultSize
,
330 m_sizerSpin
->Add(0, 0, 1);
331 m_sizerSpin
->Add(m_spinbtn
, 0, wxALIGN_CENTRE
| wxALL
, 5);
332 m_sizerSpin
->Add(0, 0, 1);
333 m_sizerSpin
->Add(m_spinctrl
, 0, wxALIGN_CENTRE
| wxALL
, 5);
334 m_sizerSpin
->Add(0, 0, 1);
336 m_sizerSpin
->Layout();
339 // ----------------------------------------------------------------------------
341 // ----------------------------------------------------------------------------
343 void SpinBtnWidgetsPage::OnButtonReset(wxCommandEvent
& WXUNUSED(event
))
350 void SpinBtnWidgetsPage::OnButtonSetMinAndMax(wxCommandEvent
& WXUNUSED(event
))
353 maxNew
= 0; // init to suppress compiler warning
354 if ( !m_textMin
->GetValue().ToLong(&minNew
) ||
355 !m_textMax
->GetValue().ToLong(&maxNew
) ||
358 wxLogWarning(_T("Invalid min/max values for the spinbtn."));
366 m_spinbtn
->SetRange(minNew
, maxNew
);
367 m_spinctrl
->SetRange(minNew
, maxNew
);
370 void SpinBtnWidgetsPage::OnButtonSetValue(wxCommandEvent
& WXUNUSED(event
))
373 if ( !m_textValue
->GetValue().ToLong(&val
) || !IsValidValue(val
) )
375 wxLogWarning(_T("Invalid spinbtn value."));
380 m_spinbtn
->SetValue(val
);
381 m_spinctrl
->SetValue(val
);
384 void SpinBtnWidgetsPage::OnUpdateUIValueButton(wxUpdateUIEvent
& event
)
387 event
.Enable( m_textValue
->GetValue().ToLong(&val
) && IsValidValue(val
) );
390 void SpinBtnWidgetsPage::OnUpdateUIMinMaxButton(wxUpdateUIEvent
& event
)
393 event
.Enable( m_textMin
->GetValue().ToLong(&mn
) &&
394 m_textMax
->GetValue().ToLong(&mx
) &&
398 void SpinBtnWidgetsPage::OnUpdateUIResetButton(wxUpdateUIEvent
& event
)
400 event
.Enable( !m_chkVert
->GetValue() || m_chkWrap
->GetValue() );
403 void SpinBtnWidgetsPage::OnCheckOrRadioBox(wxCommandEvent
& WXUNUSED(event
))
408 void SpinBtnWidgetsPage::OnUpdateUICurValueText(wxUpdateUIEvent
& event
)
410 event
.SetText( wxString::Format(_T("%d"), m_spinbtn
->GetValue()));
413 void SpinBtnWidgetsPage::OnSpinBtn(wxSpinEvent
& event
)
415 int value
= event
.GetInt();
417 wxASSERT_MSG( value
== m_spinbtn
->GetValue(),
418 _T("spinbtn value should be the same") );
420 wxLogMessage(_T("Spin button value changed, now %d"), value
);
423 void SpinBtnWidgetsPage::OnSpinBtnUp(wxSpinEvent
& event
)
425 wxLogMessage( _T("Spin button value incremented, will be %d (was %d)"),
426 event
.GetInt(), m_spinbtn
->GetValue() );
429 void SpinBtnWidgetsPage::OnSpinBtnDown(wxSpinEvent
& event
)
431 wxLogMessage( _T("Spin button value decremented, will be %d (was %d)"),
432 event
.GetInt(), m_spinbtn
->GetValue() );
435 void SpinBtnWidgetsPage::OnSpinCtrl(wxSpinEvent
& event
)
439 int value
= event
.GetInt();
441 wxASSERT_MSG( value
== m_spinctrl
->GetValue(),
442 _T("spinctrl value should be the same") );
444 wxLogMessage(_T("Spin control value changed, now %d"), value
);