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(); }
85 void OnButtonReset(wxCommandEvent
& event
);
86 void OnButtonClear(wxCommandEvent
& event
);
87 void OnButtonSetValue(wxCommandEvent
& event
);
88 void OnButtonSetMinAndMax(wxCommandEvent
& event
);
90 void OnCheckOrRadioBox(wxCommandEvent
& event
);
92 void OnSpinBtn(wxSpinEvent
& event
);
93 void OnSpinBtnUp(wxSpinEvent
& event
);
94 void OnSpinBtnDown(wxSpinEvent
& event
);
95 void OnSpinCtrl(wxSpinEvent
& event
);
97 void OnUpdateUIValueButton(wxUpdateUIEvent
& event
);
98 void OnUpdateUIMinMaxButton(wxUpdateUIEvent
& event
);
100 void OnUpdateUIResetButton(wxUpdateUIEvent
& event
);
102 void OnUpdateUICurValueText(wxUpdateUIEvent
& event
);
104 // reset the spinbtn parameters
107 // (re)create the spinbtn
110 // is this spinbtn value in range?
111 bool IsValidValue(int val
) const
112 { return (val
>= m_min
) && (val
<= m_max
); }
120 // the check/radio boxes for styles
121 wxCheckBox
*m_chkVert
,
124 // the spinbtn and the spinctrl and the sizer containing them
125 wxSpinButton
*m_spinbtn
;
126 wxSpinCtrl
*m_spinctrl
;
128 wxSizer
*m_sizerSpin
;
130 // the text entries for set value/range
131 wxTextCtrl
*m_textValue
,
136 DECLARE_EVENT_TABLE()
137 DECLARE_WIDGETS_PAGE(SpinBtnWidgetsPage
)
140 // ----------------------------------------------------------------------------
142 // ----------------------------------------------------------------------------
144 BEGIN_EVENT_TABLE(SpinBtnWidgetsPage
, WidgetsPage
)
145 EVT_BUTTON(SpinBtnPage_Reset
, SpinBtnWidgetsPage::OnButtonReset
)
146 EVT_BUTTON(SpinBtnPage_SetValue
, SpinBtnWidgetsPage::OnButtonSetValue
)
147 EVT_BUTTON(SpinBtnPage_SetMinAndMax
, SpinBtnWidgetsPage::OnButtonSetMinAndMax
)
149 EVT_UPDATE_UI(SpinBtnPage_SetValue
, SpinBtnWidgetsPage::OnUpdateUIValueButton
)
150 EVT_UPDATE_UI(SpinBtnPage_SetMinAndMax
, SpinBtnWidgetsPage::OnUpdateUIMinMaxButton
)
152 EVT_UPDATE_UI(SpinBtnPage_Reset
, SpinBtnWidgetsPage::OnUpdateUIResetButton
)
154 EVT_UPDATE_UI(SpinBtnPage_CurValueText
, SpinBtnWidgetsPage::OnUpdateUICurValueText
)
156 EVT_SPIN(SpinBtnPage_SpinBtn
, SpinBtnWidgetsPage::OnSpinBtn
)
157 EVT_SPIN_UP(SpinBtnPage_SpinBtn
, SpinBtnWidgetsPage::OnSpinBtnUp
)
158 EVT_SPIN_DOWN(SpinBtnPage_SpinBtn
, SpinBtnWidgetsPage::OnSpinBtnDown
)
159 EVT_SPINCTRL(SpinBtnPage_SpinCtrl
, SpinBtnWidgetsPage::OnSpinCtrl
)
161 EVT_CHECKBOX(wxID_ANY
, SpinBtnWidgetsPage::OnCheckOrRadioBox
)
162 EVT_RADIOBOX(wxID_ANY
, SpinBtnWidgetsPage::OnCheckOrRadioBox
)
165 // ============================================================================
167 // ============================================================================
169 #if defined(__WXUNIVERSAL__)
170 #define FAMILY_CTRLS UNIVERSAL_CTRLS
172 #define FAMILY_CTRLS NATIVE_CTRLS
175 IMPLEMENT_WIDGETS_PAGE(SpinBtnWidgetsPage
, _T("Spin"),
176 FAMILY_CTRLS
| EDITABLE_CTRLS
179 SpinBtnWidgetsPage::SpinBtnWidgetsPage(WidgetsBookCtrl
*book
,
180 wxImageList
*imaglist
)
181 : WidgetsPage(book
, imaglist
, spinbtn_xpm
)
196 m_chkWrap
= (wxCheckBox
*)NULL
;
198 m_spinbtn
= (wxSpinButton
*)NULL
;
199 m_sizerSpin
= (wxSizer
*)NULL
;
201 wxSizer
*sizerTop
= new wxBoxSizer(wxHORIZONTAL
);
204 wxStaticBox
*box
= new wxStaticBox(this, wxID_ANY
, _T("&Set style"));
205 wxSizer
*sizerLeft
= new wxStaticBoxSizer(box
, wxVERTICAL
);
207 m_chkVert
= CreateCheckBoxAndAddToSizer(sizerLeft
, _T("&Vertical"));
208 m_chkWrap
= CreateCheckBoxAndAddToSizer(sizerLeft
, _T("&Wrap"));
210 sizerLeft
->Add(5, 5, 0, wxGROW
| wxALL
, 5); // spacer
212 wxButton
*btn
= new wxButton(this, SpinBtnPage_Reset
, _T("&Reset"));
213 sizerLeft
->Add(btn
, 0, wxALIGN_CENTRE_HORIZONTAL
| wxALL
, 15);
216 wxStaticBox
*box2
= new wxStaticBox(this, wxID_ANY
,
217 _T("&Change spinbtn value"));
219 wxSizer
*sizerMiddle
= new wxStaticBoxSizer(box2
, wxVERTICAL
);
222 wxSizer
*sizerRow
= CreateSizerWithTextAndLabel(_T("Current value"),
223 SpinBtnPage_CurValueText
,
225 text
->SetEditable(false);
227 sizerMiddle
->Add(sizerRow
, 0, wxALL
| wxGROW
, 5);
229 sizerRow
= CreateSizerWithTextAndButton(SpinBtnPage_SetValue
,
231 SpinBtnPage_ValueText
,
233 sizerMiddle
->Add(sizerRow
, 0, wxALL
| wxGROW
, 5);
235 sizerRow
= CreateSizerWithTextAndButton(SpinBtnPage_SetMinAndMax
,
240 m_textMax
= new wxTextCtrl(this, SpinBtnPage_MaxText
, wxEmptyString
);
241 sizerRow
->Add(m_textMax
, 1, wxLEFT
| wxALIGN_CENTRE_VERTICAL
, 5);
243 m_textMin
->SetValue( wxString::Format(_T("%d"), m_min
) );
244 m_textMax
->SetValue( wxString::Format(_T("%d"), m_max
) );
246 sizerMiddle
->Add(sizerRow
, 0, wxALL
| wxGROW
, 5);
249 wxSizer
*sizerRight
= new wxBoxSizer(wxVERTICAL
);
250 sizerRight
->SetMinSize(150, 0);
251 m_sizerSpin
= sizerRight
; // save it to modify it later
256 // the 3 panes panes compose the window
257 sizerTop
->Add(sizerLeft
, 0, wxGROW
| (wxALL
& ~wxLEFT
), 10);
258 sizerTop
->Add(sizerMiddle
, 0, wxGROW
| wxALL
, 10);
259 sizerTop
->Add(sizerRight
, 1, wxGROW
| (wxALL
& ~wxRIGHT
), 10);
261 // final initializations
267 // ----------------------------------------------------------------------------
269 // ----------------------------------------------------------------------------
271 void SpinBtnWidgetsPage::Reset()
273 m_chkVert
->SetValue(true);
274 m_chkWrap
->SetValue(false);
277 void SpinBtnWidgetsPage::CreateSpin()
279 int flags
= ms_defaultFlags
;
281 bool isVert
= m_chkVert
->GetValue();
283 flags
|= wxSP_VERTICAL
;
285 flags
|= wxSP_HORIZONTAL
;
287 if ( m_chkWrap
->GetValue() )
293 int valOld
= m_spinbtn
->GetValue();
294 if ( !IsValidValue(valOld
) )
299 m_sizerSpin
->Detach( m_spinbtn
);
300 m_sizerSpin
->Detach( m_spinctrl
);
302 // there are 3 spacers left
303 m_sizerSpin
->Remove( 0 );
304 m_sizerSpin
->Remove( 0 );
305 m_sizerSpin
->Remove( 0 );
311 m_spinbtn
= new wxSpinButton(this, SpinBtnPage_SpinBtn
,
312 wxDefaultPosition
, wxDefaultSize
,
315 m_spinbtn
->SetValue(val
);
316 m_spinbtn
->SetRange(m_min
, m_max
);
318 m_spinctrl
= new wxSpinCtrl(this, SpinBtnPage_SpinCtrl
,
319 wxString::Format(_T("%d"), val
),
320 wxDefaultPosition
, wxDefaultSize
,
324 m_sizerSpin
->Add(0, 0, 1);
325 m_sizerSpin
->Add(m_spinbtn
, 0, wxALIGN_CENTRE
| wxALL
, 5);
326 m_sizerSpin
->Add(0, 0, 1);
327 m_sizerSpin
->Add(m_spinctrl
, 0, wxALIGN_CENTRE
| wxALL
, 5);
328 m_sizerSpin
->Add(0, 0, 1);
330 m_sizerSpin
->Layout();
333 // ----------------------------------------------------------------------------
335 // ----------------------------------------------------------------------------
337 void SpinBtnWidgetsPage::OnButtonReset(wxCommandEvent
& WXUNUSED(event
))
344 void SpinBtnWidgetsPage::OnButtonSetMinAndMax(wxCommandEvent
& WXUNUSED(event
))
347 maxNew
= 0; // init to suppress compiler warning
348 if ( !m_textMin
->GetValue().ToLong(&minNew
) ||
349 !m_textMax
->GetValue().ToLong(&maxNew
) ||
352 wxLogWarning(_T("Invalid min/max values for the spinbtn."));
360 m_spinbtn
->SetRange(minNew
, maxNew
);
361 m_spinctrl
->SetRange(minNew
, maxNew
);
364 void SpinBtnWidgetsPage::OnButtonSetValue(wxCommandEvent
& WXUNUSED(event
))
367 if ( !m_textValue
->GetValue().ToLong(&val
) || !IsValidValue(val
) )
369 wxLogWarning(_T("Invalid spinbtn value."));
374 m_spinbtn
->SetValue(val
);
375 m_spinctrl
->SetValue(val
);
378 void SpinBtnWidgetsPage::OnUpdateUIValueButton(wxUpdateUIEvent
& event
)
381 event
.Enable( m_textValue
->GetValue().ToLong(&val
) && IsValidValue(val
) );
384 void SpinBtnWidgetsPage::OnUpdateUIMinMaxButton(wxUpdateUIEvent
& event
)
387 event
.Enable( m_textMin
->GetValue().ToLong(&mn
) &&
388 m_textMax
->GetValue().ToLong(&mx
) &&
392 void SpinBtnWidgetsPage::OnUpdateUIResetButton(wxUpdateUIEvent
& event
)
394 event
.Enable( !m_chkVert
->GetValue() || m_chkWrap
->GetValue() );
397 void SpinBtnWidgetsPage::OnCheckOrRadioBox(wxCommandEvent
& WXUNUSED(event
))
402 void SpinBtnWidgetsPage::OnUpdateUICurValueText(wxUpdateUIEvent
& event
)
404 event
.SetText( wxString::Format(_T("%d"), m_spinbtn
->GetValue()));
407 void SpinBtnWidgetsPage::OnSpinBtn(wxSpinEvent
& event
)
409 int value
= event
.GetInt();
411 wxASSERT_MSG( value
== m_spinbtn
->GetValue(),
412 _T("spinbtn value should be the same") );
414 wxLogMessage(_T("Spin button value changed, now %d"), value
);
417 void SpinBtnWidgetsPage::OnSpinBtnUp(wxSpinEvent
& event
)
419 wxLogMessage( _T("Spin button value incremented, will be %d (was %d)"),
420 event
.GetInt(), m_spinbtn
->GetValue() );
423 void SpinBtnWidgetsPage::OnSpinBtnDown(wxSpinEvent
& event
)
425 wxLogMessage( _T("Spin button value decremented, will be %d (was %d)"),
426 event
.GetInt(), m_spinbtn
->GetValue() );
429 void SpinBtnWidgetsPage::OnSpinCtrl(wxSpinEvent
& event
)
433 int value
= event
.GetInt();
435 wxASSERT_MSG( value
== m_spinctrl
->GetValue(),
436 _T("spinctrl value should be the same") );
438 wxLogMessage(_T("Spin control value changed, now %d"), value
);