1 /////////////////////////////////////////////////////////////////////////////
2 // Program: wxWindows 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"
27 // for all others, include the necessary headers
31 #include "wx/button.h"
32 #include "wx/checkbox.h"
33 #include "wx/radiobox.h"
34 #include "wx/statbox.h"
35 #include "wx/textctrl.h"
38 #include "wx/spinbutt.h"
39 #include "wx/spinctrl.h"
45 #include "icons/spinbtn.xpm"
47 // ----------------------------------------------------------------------------
49 // ----------------------------------------------------------------------------
54 SpinBtnPage_Reset
= 100,
57 SpinBtnPage_SetMinAndMax
,
58 SpinBtnPage_CurValueText
,
59 SpinBtnPage_ValueText
,
66 // ----------------------------------------------------------------------------
68 // ----------------------------------------------------------------------------
70 class SpinBtnWidgetsPage
: public WidgetsPage
73 SpinBtnWidgetsPage(wxNotebook
*notebook
, wxImageList
*imaglist
);
74 virtual ~SpinBtnWidgetsPage();
78 void OnButtonReset(wxCommandEvent
& event
);
79 void OnButtonClear(wxCommandEvent
& event
);
80 void OnButtonSetValue(wxCommandEvent
& event
);
81 void OnButtonSetMinAndMax(wxCommandEvent
& event
);
83 void OnCheckOrRadioBox(wxCommandEvent
& event
);
85 void OnSpinBtn(wxCommandEvent
& event
);
86 void OnSpinBtnUp(wxCommandEvent
& event
);
87 void OnSpinBtnDown(wxCommandEvent
& event
);
88 void OnSpinCtrl(wxCommandEvent
& event
);
90 void OnUpdateUIValueButton(wxUpdateUIEvent
& event
);
91 void OnUpdateUIMinMaxButton(wxUpdateUIEvent
& event
);
93 void OnUpdateUIResetButton(wxUpdateUIEvent
& event
);
95 void OnUpdateUICurValueText(wxUpdateUIEvent
& event
);
97 // reset the spinbtn parameters
100 // (re)create the spinbtn
103 // is this spinbtn value in range?
104 bool IsValidValue(int val
) const
105 { return (val
>= m_min
) && (val
<= m_max
); }
113 // the check/radio boxes for styles
114 wxCheckBox
*m_chkVert
,
117 // the spinbtn and the spinctrl and the sizer containing them
118 wxSpinButton
*m_spinbtn
;
119 wxSpinCtrl
*m_spinctrl
;
121 wxSizer
*m_sizerSpin
;
123 // the text entries for set value/range
124 wxTextCtrl
*m_textValue
,
129 DECLARE_EVENT_TABLE()
130 DECLARE_WIDGETS_PAGE(SpinBtnWidgetsPage
)
133 // ----------------------------------------------------------------------------
135 // ----------------------------------------------------------------------------
137 BEGIN_EVENT_TABLE(SpinBtnWidgetsPage
, WidgetsPage
)
138 EVT_BUTTON(SpinBtnPage_Reset
, SpinBtnWidgetsPage::OnButtonReset
)
139 EVT_BUTTON(SpinBtnPage_SetValue
, SpinBtnWidgetsPage::OnButtonSetValue
)
140 EVT_BUTTON(SpinBtnPage_SetMinAndMax
, SpinBtnWidgetsPage::OnButtonSetMinAndMax
)
142 EVT_UPDATE_UI(SpinBtnPage_SetValue
, SpinBtnWidgetsPage::OnUpdateUIValueButton
)
143 EVT_UPDATE_UI(SpinBtnPage_SetMinAndMax
, SpinBtnWidgetsPage::OnUpdateUIMinMaxButton
)
145 EVT_UPDATE_UI(SpinBtnPage_Reset
, SpinBtnWidgetsPage::OnUpdateUIResetButton
)
147 EVT_UPDATE_UI(SpinBtnPage_CurValueText
, SpinBtnWidgetsPage::OnUpdateUICurValueText
)
149 EVT_SPIN(SpinBtnPage_SpinBtn
, SpinBtnWidgetsPage::OnSpinBtn
)
150 EVT_SPIN_UP(SpinBtnPage_SpinBtn
, SpinBtnWidgetsPage::OnSpinBtnUp
)
151 EVT_SPIN_DOWN(SpinBtnPage_SpinBtn
, SpinBtnWidgetsPage::OnSpinBtnDown
)
152 EVT_SPINCTRL(SpinBtnPage_SpinCtrl
, SpinBtnWidgetsPage::OnSpinCtrl
)
154 EVT_CHECKBOX(-1, SpinBtnWidgetsPage::OnCheckOrRadioBox
)
155 EVT_RADIOBOX(-1, SpinBtnWidgetsPage::OnCheckOrRadioBox
)
158 // ============================================================================
160 // ============================================================================
162 IMPLEMENT_WIDGETS_PAGE(SpinBtnWidgetsPage
, _T("Spin"));
164 SpinBtnWidgetsPage::SpinBtnWidgetsPage(wxNotebook
*notebook
,
165 wxImageList
*imaglist
)
166 : WidgetsPage(notebook
)
168 imaglist
->Add(wxBitmap(spinbtn_xpm
));
175 m_chkWrap
= (wxCheckBox
*)NULL
;
177 m_spinbtn
= (wxSpinButton
*)NULL
;
178 m_sizerSpin
= (wxSizer
*)NULL
;
180 wxSizer
*sizerTop
= new wxBoxSizer(wxHORIZONTAL
);
183 wxStaticBox
*box
= new wxStaticBox(this, -1, _T("&Set style"));
184 wxSizer
*sizerLeft
= new wxStaticBoxSizer(box
, wxVERTICAL
);
186 m_chkVert
= CreateCheckBoxAndAddToSizer(sizerLeft
, _T("&Vertical"));
187 m_chkWrap
= CreateCheckBoxAndAddToSizer(sizerLeft
, _T("&Wrap"));
189 sizerLeft
->Add(5, 5, 0, wxGROW
| wxALL
, 5); // spacer
191 wxButton
*btn
= new wxButton(this, SpinBtnPage_Reset
, _T("&Reset"));
192 sizerLeft
->Add(btn
, 0, wxALIGN_CENTRE_HORIZONTAL
| wxALL
, 15);
195 wxStaticBox
*box2
= new wxStaticBox(this, -1, _T("&Change spinbtn value"));
196 wxSizer
*sizerMiddle
= new wxStaticBoxSizer(box2
, wxVERTICAL
);
199 wxSizer
*sizerRow
= CreateSizerWithTextAndLabel(_T("Current value"),
200 SpinBtnPage_CurValueText
,
202 text
->SetEditable(FALSE
);
204 sizerMiddle
->Add(sizerRow
, 0, wxALL
| wxGROW
, 5);
206 sizerRow
= CreateSizerWithTextAndButton(SpinBtnPage_SetValue
,
208 SpinBtnPage_ValueText
,
210 sizerMiddle
->Add(sizerRow
, 0, wxALL
| wxGROW
, 5);
212 sizerRow
= CreateSizerWithTextAndButton(SpinBtnPage_SetMinAndMax
,
217 m_textMax
= new wxTextCtrl(this, SpinBtnPage_MaxText
, _T(""));
218 sizerRow
->Add(m_textMax
, 1, wxLEFT
| wxALIGN_CENTRE_VERTICAL
, 5);
220 m_textMin
->SetValue(wxString::Format(_T("%lu"), m_min
));
221 m_textMax
->SetValue(wxString::Format(_T("%lu"), m_max
));
223 sizerMiddle
->Add(sizerRow
, 0, wxALL
| wxGROW
, 5);
226 wxSizer
*sizerRight
= new wxBoxSizer(wxVERTICAL
);
227 sizerRight
->SetMinSize(150, 0);
228 m_sizerSpin
= sizerRight
; // save it to modify it later
233 // the 3 panes panes compose the window
234 sizerTop
->Add(sizerLeft
, 0, wxGROW
| (wxALL
& ~wxLEFT
), 10);
235 sizerTop
->Add(sizerMiddle
, 0, wxGROW
| wxALL
, 10);
236 sizerTop
->Add(sizerRight
, 1, wxGROW
| (wxALL
& ~wxRIGHT
), 10);
238 // final initializations
245 SpinBtnWidgetsPage::~SpinBtnWidgetsPage()
249 // ----------------------------------------------------------------------------
251 // ----------------------------------------------------------------------------
253 void SpinBtnWidgetsPage::Reset()
255 m_chkVert
->SetValue(TRUE
);
256 m_chkWrap
->SetValue(FALSE
);
259 void SpinBtnWidgetsPage::CreateSpin()
263 bool isVert
= m_chkVert
->GetValue();
265 flags
|= wxSP_VERTICAL
;
267 flags
|= wxSP_HORIZONTAL
;
269 if ( m_chkWrap
->GetValue() )
275 int valOld
= m_spinbtn
->GetValue();
276 if ( !IsValidValue(valOld
) )
281 m_sizerSpin
->Remove(m_spinbtn
);
282 m_sizerSpin
->Remove(m_spinctrl
);
284 // there are 3 spacers left
285 m_sizerSpin
->Remove((int)0);
286 m_sizerSpin
->Remove((int)0);
287 m_sizerSpin
->Remove((int)0);
293 m_spinbtn
= new wxSpinButton(this, SpinBtnPage_SpinBtn
,
294 wxDefaultPosition
, wxDefaultSize
,
297 m_spinbtn
->SetValue(val
);
298 m_spinbtn
->SetRange(m_min
, m_max
);
300 m_spinctrl
= new wxSpinCtrl(this, SpinBtnPage_SpinCtrl
,
301 wxString::Format(_T("%d"), val
),
302 wxDefaultPosition
, wxDefaultSize
,
306 m_sizerSpin
->Add(0, 0, 1);
307 m_sizerSpin
->Add(m_spinbtn
, 0, wxALIGN_CENTRE
| wxALL
, 5);
308 m_sizerSpin
->Add(0, 0, 1);
309 m_sizerSpin
->Add(m_spinctrl
, 0, wxALIGN_CENTRE
| wxALL
, 5);
310 m_sizerSpin
->Add(0, 0, 1);
312 m_sizerSpin
->Layout();
315 // ----------------------------------------------------------------------------
317 // ----------------------------------------------------------------------------
319 void SpinBtnWidgetsPage::OnButtonReset(wxCommandEvent
& WXUNUSED(event
))
326 void SpinBtnWidgetsPage::OnButtonSetMinAndMax(wxCommandEvent
& WXUNUSED(event
))
329 maxNew
= 0; // init to suppress compiler warning
330 if ( !m_textMin
->GetValue().ToLong(&minNew
) ||
331 !m_textMax
->GetValue().ToLong(&maxNew
) ||
334 wxLogWarning(_T("Invalid min/max values for the spinbtn."));
342 m_spinbtn
->SetRange(minNew
, maxNew
);
343 m_spinctrl
->SetRange(minNew
, maxNew
);
346 void SpinBtnWidgetsPage::OnButtonSetValue(wxCommandEvent
& WXUNUSED(event
))
349 if ( !m_textValue
->GetValue().ToLong(&val
) || !IsValidValue(val
) )
351 wxLogWarning(_T("Invalid spinbtn value."));
356 m_spinbtn
->SetValue(val
);
357 m_spinctrl
->SetValue(val
);
360 void SpinBtnWidgetsPage::OnUpdateUIValueButton(wxUpdateUIEvent
& event
)
363 event
.Enable( m_textValue
->GetValue().ToLong(&val
) && IsValidValue(val
) );
366 void SpinBtnWidgetsPage::OnUpdateUIMinMaxButton(wxUpdateUIEvent
& event
)
369 event
.Enable( m_textMin
->GetValue().ToLong(&mn
) &&
370 m_textMax
->GetValue().ToLong(&mx
) &&
374 void SpinBtnWidgetsPage::OnUpdateUIResetButton(wxUpdateUIEvent
& event
)
376 event
.Enable( !m_chkVert
->GetValue() || m_chkWrap
->GetValue() );
379 void SpinBtnWidgetsPage::OnCheckOrRadioBox(wxCommandEvent
& event
)
384 void SpinBtnWidgetsPage::OnUpdateUICurValueText(wxUpdateUIEvent
& event
)
386 event
.SetText( wxString::Format(_T("%d"), m_spinbtn
->GetValue()));
389 void SpinBtnWidgetsPage::OnSpinBtn(wxCommandEvent
& event
)
391 int value
= event
.GetInt();
393 wxASSERT_MSG( value
== m_spinbtn
->GetValue(),
394 _T("spinbtn value should be the same") );
396 wxLogMessage(_T("Spin button value changed, now %d"), value
);
399 void SpinBtnWidgetsPage::OnSpinBtnUp(wxCommandEvent
& event
)
401 wxLogMessage(_T("Spin button value incremented, will be %d (was %d)"),
402 event
.GetInt(), m_spinbtn
->GetValue());
405 void SpinBtnWidgetsPage::OnSpinBtnDown(wxCommandEvent
& event
)
407 wxLogMessage(_T("Spin button value decremented, will be %d (was %d)"),
408 event
.GetInt(), m_spinbtn
->GetValue());
411 void SpinBtnWidgetsPage::OnSpinCtrl(wxCommandEvent
& event
)
413 int value
= event
.GetInt();
415 wxASSERT_MSG( value
== m_spinctrl
->GetValue(),
416 _T("spinctrl value should be the same") );
418 wxLogMessage(_T("Spin control value changed, now %d"), value
);