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/bitmap.h"
32 #include "wx/button.h"
33 #include "wx/checkbox.h"
34 #include "wx/radiobox.h"
35 #include "wx/statbox.h"
36 #include "wx/textctrl.h"
39 #include "wx/spinbutt.h"
40 #include "wx/spinctrl.h"
46 #include "icons/spinbtn.xpm"
48 // ----------------------------------------------------------------------------
50 // ----------------------------------------------------------------------------
55 SpinBtnPage_Reset
= 100,
58 SpinBtnPage_SetMinAndMax
,
59 SpinBtnPage_CurValueText
,
60 SpinBtnPage_ValueText
,
67 // ----------------------------------------------------------------------------
69 // ----------------------------------------------------------------------------
71 class SpinBtnWidgetsPage
: public WidgetsPage
74 SpinBtnWidgetsPage(wxNotebook
*notebook
, wxImageList
*imaglist
);
75 virtual ~SpinBtnWidgetsPage();
79 void OnButtonReset(wxCommandEvent
& event
);
80 void OnButtonClear(wxCommandEvent
& event
);
81 void OnButtonSetValue(wxCommandEvent
& event
);
82 void OnButtonSetMinAndMax(wxCommandEvent
& event
);
84 void OnCheckOrRadioBox(wxCommandEvent
& event
);
86 void OnSpinBtn(wxSpinEvent
& event
);
87 void OnSpinBtnUp(wxSpinEvent
& event
);
88 void OnSpinBtnDown(wxSpinEvent
& event
);
89 void OnSpinCtrl(wxSpinEvent
& event
);
91 void OnUpdateUIValueButton(wxUpdateUIEvent
& event
);
92 void OnUpdateUIMinMaxButton(wxUpdateUIEvent
& event
);
94 void OnUpdateUIResetButton(wxUpdateUIEvent
& event
);
96 void OnUpdateUICurValueText(wxUpdateUIEvent
& event
);
98 // reset the spinbtn parameters
101 // (re)create the spinbtn
104 // is this spinbtn value in range?
105 bool IsValidValue(int val
) const
106 { return (val
>= m_min
) && (val
<= m_max
); }
114 // the check/radio boxes for styles
115 wxCheckBox
*m_chkVert
,
118 // the spinbtn and the spinctrl and the sizer containing them
119 wxSpinButton
*m_spinbtn
;
120 wxSpinCtrl
*m_spinctrl
;
122 wxSizer
*m_sizerSpin
;
124 // the text entries for set value/range
125 wxTextCtrl
*m_textValue
,
130 DECLARE_EVENT_TABLE()
131 DECLARE_WIDGETS_PAGE(SpinBtnWidgetsPage
)
134 // ----------------------------------------------------------------------------
136 // ----------------------------------------------------------------------------
138 BEGIN_EVENT_TABLE(SpinBtnWidgetsPage
, WidgetsPage
)
139 EVT_BUTTON(SpinBtnPage_Reset
, SpinBtnWidgetsPage::OnButtonReset
)
140 EVT_BUTTON(SpinBtnPage_SetValue
, SpinBtnWidgetsPage::OnButtonSetValue
)
141 EVT_BUTTON(SpinBtnPage_SetMinAndMax
, SpinBtnWidgetsPage::OnButtonSetMinAndMax
)
143 EVT_UPDATE_UI(SpinBtnPage_SetValue
, SpinBtnWidgetsPage::OnUpdateUIValueButton
)
144 EVT_UPDATE_UI(SpinBtnPage_SetMinAndMax
, SpinBtnWidgetsPage::OnUpdateUIMinMaxButton
)
146 EVT_UPDATE_UI(SpinBtnPage_Reset
, SpinBtnWidgetsPage::OnUpdateUIResetButton
)
148 EVT_UPDATE_UI(SpinBtnPage_CurValueText
, SpinBtnWidgetsPage::OnUpdateUICurValueText
)
150 EVT_SPIN(SpinBtnPage_SpinBtn
, SpinBtnWidgetsPage::OnSpinBtn
)
151 EVT_SPIN_UP(SpinBtnPage_SpinBtn
, SpinBtnWidgetsPage::OnSpinBtnUp
)
152 EVT_SPIN_DOWN(SpinBtnPage_SpinBtn
, SpinBtnWidgetsPage::OnSpinBtnDown
)
153 EVT_SPINCTRL(SpinBtnPage_SpinCtrl
, SpinBtnWidgetsPage::OnSpinCtrl
)
155 EVT_CHECKBOX(wxID_ANY
, SpinBtnWidgetsPage::OnCheckOrRadioBox
)
156 EVT_RADIOBOX(wxID_ANY
, SpinBtnWidgetsPage::OnCheckOrRadioBox
)
159 // ============================================================================
161 // ============================================================================
163 IMPLEMENT_WIDGETS_PAGE(SpinBtnWidgetsPage
, _T("Spin"));
165 SpinBtnWidgetsPage::SpinBtnWidgetsPage(wxNotebook
*notebook
,
166 wxImageList
*imaglist
)
167 : WidgetsPage(notebook
)
176 imaglist
->Add(wxBitmap(spinbtn_xpm
));
183 m_chkWrap
= (wxCheckBox
*)NULL
;
185 m_spinbtn
= (wxSpinButton
*)NULL
;
186 m_sizerSpin
= (wxSizer
*)NULL
;
188 wxSizer
*sizerTop
= new wxBoxSizer(wxHORIZONTAL
);
191 wxStaticBox
*box
= new wxStaticBox(this, wxID_ANY
, _T("&Set style"));
192 wxSizer
*sizerLeft
= new wxStaticBoxSizer(box
, wxVERTICAL
);
194 m_chkVert
= CreateCheckBoxAndAddToSizer(sizerLeft
, _T("&Vertical"));
195 m_chkWrap
= CreateCheckBoxAndAddToSizer(sizerLeft
, _T("&Wrap"));
197 sizerLeft
->Add(5, 5, 0, wxGROW
| wxALL
, 5); // spacer
199 wxButton
*btn
= new wxButton(this, SpinBtnPage_Reset
, _T("&Reset"));
200 sizerLeft
->Add(btn
, 0, wxALIGN_CENTRE_HORIZONTAL
| wxALL
, 15);
203 wxStaticBox
*box2
= new wxStaticBox(this, wxID_ANY
,
204 _T("&Change spinbtn value"));
206 wxSizer
*sizerMiddle
= new wxStaticBoxSizer(box2
, wxVERTICAL
);
209 wxSizer
*sizerRow
= CreateSizerWithTextAndLabel(_T("Current value"),
210 SpinBtnPage_CurValueText
,
212 text
->SetEditable(false);
214 sizerMiddle
->Add(sizerRow
, 0, wxALL
| wxGROW
, 5);
216 sizerRow
= CreateSizerWithTextAndButton(SpinBtnPage_SetValue
,
218 SpinBtnPage_ValueText
,
220 sizerMiddle
->Add(sizerRow
, 0, wxALL
| wxGROW
, 5);
222 sizerRow
= CreateSizerWithTextAndButton(SpinBtnPage_SetMinAndMax
,
227 m_textMax
= new wxTextCtrl(this, SpinBtnPage_MaxText
, wxEmptyString
);
228 sizerRow
->Add(m_textMax
, 1, wxLEFT
| wxALIGN_CENTRE_VERTICAL
, 5);
230 m_textMin
->SetValue( wxString::Format(_T("%d"), m_min
) );
231 m_textMax
->SetValue( wxString::Format(_T("%d"), m_max
) );
233 sizerMiddle
->Add(sizerRow
, 0, wxALL
| wxGROW
, 5);
236 wxSizer
*sizerRight
= new wxBoxSizer(wxVERTICAL
);
237 sizerRight
->SetMinSize(150, 0);
238 m_sizerSpin
= sizerRight
; // save it to modify it later
243 // the 3 panes panes compose the window
244 sizerTop
->Add(sizerLeft
, 0, wxGROW
| (wxALL
& ~wxLEFT
), 10);
245 sizerTop
->Add(sizerMiddle
, 0, wxGROW
| wxALL
, 10);
246 sizerTop
->Add(sizerRight
, 1, wxGROW
| (wxALL
& ~wxRIGHT
), 10);
248 // final initializations
254 SpinBtnWidgetsPage::~SpinBtnWidgetsPage()
258 // ----------------------------------------------------------------------------
260 // ----------------------------------------------------------------------------
262 void SpinBtnWidgetsPage::Reset()
264 m_chkVert
->SetValue(true);
265 m_chkWrap
->SetValue(false);
268 void SpinBtnWidgetsPage::CreateSpin()
272 bool isVert
= m_chkVert
->GetValue();
274 flags
|= wxSP_VERTICAL
;
276 flags
|= wxSP_HORIZONTAL
;
278 if ( m_chkWrap
->GetValue() )
284 int valOld
= m_spinbtn
->GetValue();
285 if ( !IsValidValue(valOld
) )
290 m_sizerSpin
->Detach( m_spinbtn
);
291 m_sizerSpin
->Detach( m_spinctrl
);
293 // there are 3 spacers left
294 m_sizerSpin
->Remove( 0 );
295 m_sizerSpin
->Remove( 0 );
296 m_sizerSpin
->Remove( 0 );
302 m_spinbtn
= new wxSpinButton(this, SpinBtnPage_SpinBtn
,
303 wxDefaultPosition
, wxDefaultSize
,
306 m_spinbtn
->SetValue(val
);
307 m_spinbtn
->SetRange(m_min
, m_max
);
309 m_spinctrl
= new wxSpinCtrl(this, SpinBtnPage_SpinCtrl
,
310 wxString::Format(_T("%d"), val
),
311 wxDefaultPosition
, wxDefaultSize
,
315 m_sizerSpin
->Add(0, 0, 1);
316 m_sizerSpin
->Add(m_spinbtn
, 0, wxALIGN_CENTRE
| wxALL
, 5);
317 m_sizerSpin
->Add(0, 0, 1);
318 m_sizerSpin
->Add(m_spinctrl
, 0, wxALIGN_CENTRE
| wxALL
, 5);
319 m_sizerSpin
->Add(0, 0, 1);
321 m_sizerSpin
->Layout();
324 // ----------------------------------------------------------------------------
326 // ----------------------------------------------------------------------------
328 void SpinBtnWidgetsPage::OnButtonReset(wxCommandEvent
& WXUNUSED(event
))
335 void SpinBtnWidgetsPage::OnButtonSetMinAndMax(wxCommandEvent
& WXUNUSED(event
))
338 maxNew
= 0; // init to suppress compiler warning
339 if ( !m_textMin
->GetValue().ToLong(&minNew
) ||
340 !m_textMax
->GetValue().ToLong(&maxNew
) ||
343 wxLogWarning(_T("Invalid min/max values for the spinbtn."));
351 m_spinbtn
->SetRange(minNew
, maxNew
);
352 m_spinctrl
->SetRange(minNew
, maxNew
);
355 void SpinBtnWidgetsPage::OnButtonSetValue(wxCommandEvent
& WXUNUSED(event
))
358 if ( !m_textValue
->GetValue().ToLong(&val
) || !IsValidValue(val
) )
360 wxLogWarning(_T("Invalid spinbtn value."));
365 m_spinbtn
->SetValue(val
);
366 m_spinctrl
->SetValue(val
);
369 void SpinBtnWidgetsPage::OnUpdateUIValueButton(wxUpdateUIEvent
& event
)
372 event
.Enable( m_textValue
->GetValue().ToLong(&val
) && IsValidValue(val
) );
375 void SpinBtnWidgetsPage::OnUpdateUIMinMaxButton(wxUpdateUIEvent
& event
)
378 event
.Enable( m_textMin
->GetValue().ToLong(&mn
) &&
379 m_textMax
->GetValue().ToLong(&mx
) &&
383 void SpinBtnWidgetsPage::OnUpdateUIResetButton(wxUpdateUIEvent
& event
)
385 event
.Enable( !m_chkVert
->GetValue() || m_chkWrap
->GetValue() );
388 void SpinBtnWidgetsPage::OnCheckOrRadioBox(wxCommandEvent
& WXUNUSED(event
))
393 void SpinBtnWidgetsPage::OnUpdateUICurValueText(wxUpdateUIEvent
& event
)
395 event
.SetText( wxString::Format(_T("%d"), m_spinbtn
->GetValue()));
398 void SpinBtnWidgetsPage::OnSpinBtn(wxSpinEvent
& event
)
400 int value
= event
.GetInt();
402 wxASSERT_MSG( value
== m_spinbtn
->GetValue(),
403 _T("spinbtn value should be the same") );
405 wxLogMessage(_T("Spin button value changed, now %d"), value
);
408 void SpinBtnWidgetsPage::OnSpinBtnUp(wxSpinEvent
& event
)
410 wxLogMessage( _T("Spin button value incremented, will be %ld (was %d)"),
411 event
.GetInt(), m_spinbtn
->GetValue() );
414 void SpinBtnWidgetsPage::OnSpinBtnDown(wxSpinEvent
& event
)
416 wxLogMessage( _T("Spin button value decremented, will be %ld (was %d)"),
417 event
.GetInt(), m_spinbtn
->GetValue() );
420 void SpinBtnWidgetsPage::OnSpinCtrl(wxSpinEvent
& event
)
424 int value
= event
.GetInt();
426 wxASSERT_MSG( value
== m_spinctrl
->GetValue(),
427 _T("spinctrl value should be the same") );
429 wxLogMessage(_T("Spin control value changed, now %d"), value
);