]> git.saurik.com Git - wxWidgets.git/blame - samples/widgets/spinbtn.cpp
use WX_STATVFS_T instead of trying to guess statvfs() argument
[wxWidgets.git] / samples / widgets / spinbtn.cpp
CommitLineData
32b8ec41
VZ
1/////////////////////////////////////////////////////////////////////////////
2// Program: wxWindows Widgets Sample
3// Name: spinbtn.cpp
4// Purpose: Part of the widgets sample showing wxSpinButton
5// Author: Vadim Zeitlin
6// Created: 16.04.01
7// Id: $Id$
8// Copyright: (c) 2001 Vadim Zeitlin
9// License: wxWindows license
10/////////////////////////////////////////////////////////////////////////////
11
12// ============================================================================
13// declarations
14// ============================================================================
15
16// ----------------------------------------------------------------------------
17// headers
18// ----------------------------------------------------------------------------
19
20// for compilers that support precompilation, includes "wx/wx.h".
21#include "wx/wxprec.h"
22
23#ifdef __BORLANDC__
24 #pragma hdrstop
25#endif
26
27// for all others, include the necessary headers
28#ifndef WX_PRECOMP
29 #include "wx/log.h"
30
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"
36#endif
37
38#include "wx/spinbutt.h"
39#include "wx/spinctrl.h"
40
41#include "wx/sizer.h"
42
43#include "widgets.h"
7b127900 44#if 1
32b8ec41
VZ
45#include "icons/spinbtn.xpm"
46
47// ----------------------------------------------------------------------------
48// constants
49// ----------------------------------------------------------------------------
50
51// control ids
52enum
53{
54 SpinBtnPage_Reset = 100,
55 SpinBtnPage_Clear,
56 SpinBtnPage_SetValue,
57 SpinBtnPage_SetMinAndMax,
58 SpinBtnPage_CurValueText,
59 SpinBtnPage_ValueText,
60 SpinBtnPage_MinText,
61 SpinBtnPage_MaxText,
62 SpinBtnPage_SpinBtn,
63 SpinBtnPage_SpinCtrl
64};
65
66// ----------------------------------------------------------------------------
67// SpinBtnWidgetsPage
68// ----------------------------------------------------------------------------
69
70class SpinBtnWidgetsPage : public WidgetsPage
71{
72public:
73 SpinBtnWidgetsPage(wxNotebook *notebook, wxImageList *imaglist);
74 virtual ~SpinBtnWidgetsPage();
75
76protected:
77 // event handlers
78 void OnButtonReset(wxCommandEvent& event);
79 void OnButtonClear(wxCommandEvent& event);
80 void OnButtonSetValue(wxCommandEvent& event);
81 void OnButtonSetMinAndMax(wxCommandEvent& event);
82
83 void OnCheckOrRadioBox(wxCommandEvent& event);
84
85 void OnSpinBtn(wxCommandEvent& event);
86 void OnSpinBtnUp(wxCommandEvent& event);
87 void OnSpinBtnDown(wxCommandEvent& event);
88 void OnSpinCtrl(wxCommandEvent& event);
89
90 void OnUpdateUIValueButton(wxUpdateUIEvent& event);
91 void OnUpdateUIMinMaxButton(wxUpdateUIEvent& event);
92
93 void OnUpdateUIResetButton(wxUpdateUIEvent& event);
94
95 void OnUpdateUICurValueText(wxUpdateUIEvent& event);
96
97 // reset the spinbtn parameters
98 void Reset();
99
100 // (re)create the spinbtn
101 void CreateSpin();
102
103 // is this spinbtn value in range?
104 bool IsValidValue(int val) const
105 { return (val >= m_min) && (val <= m_max); }
106
107 // the spinbtn range
108 int m_min, m_max;
109
110 // the controls
111 // ------------
112
113 // the check/radio boxes for styles
114 wxCheckBox *m_chkVert,
115 *m_chkWrap;
116
117 // the spinbtn and the spinctrl and the sizer containing them
118 wxSpinButton *m_spinbtn;
119 wxSpinCtrl *m_spinctrl;
120
121 wxSizer *m_sizerSpin;
122
123 // the text entries for set value/range
124 wxTextCtrl *m_textValue,
125 *m_textMin,
126 *m_textMax;
127
128private:
5e173f35
GD
129 DECLARE_EVENT_TABLE()
130 DECLARE_WIDGETS_PAGE(SpinBtnWidgetsPage)
32b8ec41
VZ
131};
132
133// ----------------------------------------------------------------------------
134// event tables
135// ----------------------------------------------------------------------------
136
137BEGIN_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)
141
142 EVT_UPDATE_UI(SpinBtnPage_SetValue, SpinBtnWidgetsPage::OnUpdateUIValueButton)
143 EVT_UPDATE_UI(SpinBtnPage_SetMinAndMax, SpinBtnWidgetsPage::OnUpdateUIMinMaxButton)
144
145 EVT_UPDATE_UI(SpinBtnPage_Reset, SpinBtnWidgetsPage::OnUpdateUIResetButton)
146
147 EVT_UPDATE_UI(SpinBtnPage_CurValueText, SpinBtnWidgetsPage::OnUpdateUICurValueText)
148
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)
153
154 EVT_CHECKBOX(-1, SpinBtnWidgetsPage::OnCheckOrRadioBox)
155 EVT_RADIOBOX(-1, SpinBtnWidgetsPage::OnCheckOrRadioBox)
156END_EVENT_TABLE()
157
158// ============================================================================
159// implementation
160// ============================================================================
161
162IMPLEMENT_WIDGETS_PAGE(SpinBtnWidgetsPage, _T("Spin"));
163
164SpinBtnWidgetsPage::SpinBtnWidgetsPage(wxNotebook *notebook,
165 wxImageList *imaglist)
166 : WidgetsPage(notebook)
167{
59b9edf0
JS
168 m_chkVert = NULL;
169 m_chkWrap = NULL;
170 m_spinbtn = NULL;
171 m_spinctrl = NULL;
172 m_textValue = NULL;
173 m_textMin = NULL;
174 m_textMax = NULL;
32b8ec41
VZ
175 imaglist->Add(wxBitmap(spinbtn_xpm));
176
177 // init everything
178 m_min = 0;
179 m_max = 10;
180
181 m_chkVert =
182 m_chkWrap = (wxCheckBox *)NULL;
183
184 m_spinbtn = (wxSpinButton *)NULL;
185 m_sizerSpin = (wxSizer *)NULL;
186
187 wxSizer *sizerTop = new wxBoxSizer(wxHORIZONTAL);
188
189 // left pane
190 wxStaticBox *box = new wxStaticBox(this, -1, _T("&Set style"));
191 wxSizer *sizerLeft = new wxStaticBoxSizer(box, wxVERTICAL);
192
193 m_chkVert = CreateCheckBoxAndAddToSizer(sizerLeft, _T("&Vertical"));
194 m_chkWrap = CreateCheckBoxAndAddToSizer(sizerLeft, _T("&Wrap"));
195
196 sizerLeft->Add(5, 5, 0, wxGROW | wxALL, 5); // spacer
197
198 wxButton *btn = new wxButton(this, SpinBtnPage_Reset, _T("&Reset"));
199 sizerLeft->Add(btn, 0, wxALIGN_CENTRE_HORIZONTAL | wxALL, 15);
200
201 // middle pane
202 wxStaticBox *box2 = new wxStaticBox(this, -1, _T("&Change spinbtn value"));
203 wxSizer *sizerMiddle = new wxStaticBoxSizer(box2, wxVERTICAL);
204
205 wxTextCtrl *text;
206 wxSizer *sizerRow = CreateSizerWithTextAndLabel(_T("Current value"),
207 SpinBtnPage_CurValueText,
208 &text);
209 text->SetEditable(FALSE);
210
211 sizerMiddle->Add(sizerRow, 0, wxALL | wxGROW, 5);
212
213 sizerRow = CreateSizerWithTextAndButton(SpinBtnPage_SetValue,
214 _T("Set &value"),
215 SpinBtnPage_ValueText,
216 &m_textValue);
217 sizerMiddle->Add(sizerRow, 0, wxALL | wxGROW, 5);
218
219 sizerRow = CreateSizerWithTextAndButton(SpinBtnPage_SetMinAndMax,
220 _T("&Min and max"),
221 SpinBtnPage_MinText,
222 &m_textMin);
223
224 m_textMax = new wxTextCtrl(this, SpinBtnPage_MaxText, _T(""));
225 sizerRow->Add(m_textMax, 1, wxLEFT | wxALIGN_CENTRE_VERTICAL, 5);
226
aec18ff7
MB
227 m_textMin->SetValue( wxString::Format(_T("%d"), m_min) );
228 m_textMax->SetValue( wxString::Format(_T("%d"), m_max) );
32b8ec41
VZ
229
230 sizerMiddle->Add(sizerRow, 0, wxALL | wxGROW, 5);
231
232 // right pane
233 wxSizer *sizerRight = new wxBoxSizer(wxVERTICAL);
7b127900 234 sizerRight->SetMinSize(150, 0);
32b8ec41
VZ
235 m_sizerSpin = sizerRight; // save it to modify it later
236
237 Reset();
238 CreateSpin();
239
240 // the 3 panes panes compose the window
241 sizerTop->Add(sizerLeft, 0, wxGROW | (wxALL & ~wxLEFT), 10);
7b127900 242 sizerTop->Add(sizerMiddle, 0, wxGROW | wxALL, 10);
32b8ec41
VZ
243 sizerTop->Add(sizerRight, 1, wxGROW | (wxALL & ~wxRIGHT), 10);
244
245 // final initializations
246 SetAutoLayout(TRUE);
247 SetSizer(sizerTop);
248
249 sizerTop->Fit(this);
250}
251
252SpinBtnWidgetsPage::~SpinBtnWidgetsPage()
253{
254}
255
256// ----------------------------------------------------------------------------
257// operations
258// ----------------------------------------------------------------------------
259
260void SpinBtnWidgetsPage::Reset()
261{
262 m_chkVert->SetValue(TRUE);
263 m_chkWrap->SetValue(FALSE);
264}
265
266void SpinBtnWidgetsPage::CreateSpin()
267{
268 int flags = 0;
269
270 bool isVert = m_chkVert->GetValue();
271 if ( isVert )
272 flags |= wxSP_VERTICAL;
273 else
274 flags |= wxSP_HORIZONTAL;
275
276 if ( m_chkWrap->GetValue() )
277 flags |= wxSP_WRAP;
278
279 int val = m_min;
280 if ( m_spinbtn )
281 {
282 int valOld = m_spinbtn->GetValue();
283 if ( !IsValidValue(valOld) )
284 {
285 val = valOld;
286 }
287
12a3f227
RL
288 m_sizerSpin->Detach( m_spinbtn );
289 m_sizerSpin->Detach( m_spinctrl );
32b8ec41
VZ
290
291 // there are 3 spacers left
12a3f227
RL
292 m_sizerSpin->Remove( 0u );
293 m_sizerSpin->Remove( 0u );
294 m_sizerSpin->Remove( 0u );
32b8ec41
VZ
295
296 delete m_spinbtn;
297 delete m_spinctrl;
298 }
299
300 m_spinbtn = new wxSpinButton(this, SpinBtnPage_SpinBtn,
301 wxDefaultPosition, wxDefaultSize,
302 flags);
303
304 m_spinbtn->SetValue(val);
305 m_spinbtn->SetRange(m_min, m_max);
306
307 m_spinctrl = new wxSpinCtrl(this, SpinBtnPage_SpinCtrl,
308 wxString::Format(_T("%d"), val),
309 wxDefaultPosition, wxDefaultSize,
310 flags,
311 m_min, m_max, val);
312
313 m_sizerSpin->Add(0, 0, 1);
314 m_sizerSpin->Add(m_spinbtn, 0, wxALIGN_CENTRE | wxALL, 5);
315 m_sizerSpin->Add(0, 0, 1);
316 m_sizerSpin->Add(m_spinctrl, 0, wxALIGN_CENTRE | wxALL, 5);
317 m_sizerSpin->Add(0, 0, 1);
318
319 m_sizerSpin->Layout();
320}
321
322// ----------------------------------------------------------------------------
323// event handlers
324// ----------------------------------------------------------------------------
325
326void SpinBtnWidgetsPage::OnButtonReset(wxCommandEvent& WXUNUSED(event))
327{
328 Reset();
329
330 CreateSpin();
331}
332
333void SpinBtnWidgetsPage::OnButtonSetMinAndMax(wxCommandEvent& WXUNUSED(event))
334{
335 long minNew,
336 maxNew = 0; // init to suppress compiler warning
337 if ( !m_textMin->GetValue().ToLong(&minNew) ||
338 !m_textMax->GetValue().ToLong(&maxNew) ||
339 minNew >= maxNew )
340 {
341 wxLogWarning(_T("Invalid min/max values for the spinbtn."));
342
343 return;
344 }
345
346 m_min = minNew;
347 m_max = maxNew;
348
349 m_spinbtn->SetRange(minNew, maxNew);
350 m_spinctrl->SetRange(minNew, maxNew);
351}
352
353void SpinBtnWidgetsPage::OnButtonSetValue(wxCommandEvent& WXUNUSED(event))
354{
355 long val;
356 if ( !m_textValue->GetValue().ToLong(&val) || !IsValidValue(val) )
357 {
358 wxLogWarning(_T("Invalid spinbtn value."));
359
360 return;
361 }
362
363 m_spinbtn->SetValue(val);
364 m_spinctrl->SetValue(val);
365}
366
367void SpinBtnWidgetsPage::OnUpdateUIValueButton(wxUpdateUIEvent& event)
368{
369 long val;
370 event.Enable( m_textValue->GetValue().ToLong(&val) && IsValidValue(val) );
371}
372
373void SpinBtnWidgetsPage::OnUpdateUIMinMaxButton(wxUpdateUIEvent& event)
374{
375 long mn, mx;
376 event.Enable( m_textMin->GetValue().ToLong(&mn) &&
377 m_textMax->GetValue().ToLong(&mx) &&
378 mn < mx);
379}
380
381void SpinBtnWidgetsPage::OnUpdateUIResetButton(wxUpdateUIEvent& event)
382{
383 event.Enable( !m_chkVert->GetValue() || m_chkWrap->GetValue() );
384}
385
386void SpinBtnWidgetsPage::OnCheckOrRadioBox(wxCommandEvent& event)
387{
388 CreateSpin();
389}
390
391void SpinBtnWidgetsPage::OnUpdateUICurValueText(wxUpdateUIEvent& event)
392{
393 event.SetText( wxString::Format(_T("%d"), m_spinbtn->GetValue()));
394}
395
396void SpinBtnWidgetsPage::OnSpinBtn(wxCommandEvent& event)
397{
398 int value = event.GetInt();
399
400 wxASSERT_MSG( value == m_spinbtn->GetValue(),
401 _T("spinbtn value should be the same") );
402
403 wxLogMessage(_T("Spin button value changed, now %d"), value);
404}
405
406void SpinBtnWidgetsPage::OnSpinBtnUp(wxCommandEvent& event)
407{
aec18ff7
MB
408 wxLogMessage( _T("Spin button value incremented, will be %ld (was %d)"),
409 event.GetInt(), m_spinbtn->GetValue() );
32b8ec41
VZ
410}
411
412void SpinBtnWidgetsPage::OnSpinBtnDown(wxCommandEvent& event)
413{
aec18ff7
MB
414 wxLogMessage( _T("Spin button value decremented, will be %ld (was %d)"),
415 event.GetInt(), m_spinbtn->GetValue() );
32b8ec41
VZ
416}
417
418void SpinBtnWidgetsPage::OnSpinCtrl(wxCommandEvent& event)
419{
59b9edf0
JS
420 if (!m_spinctrl)
421 return;
32b8ec41
VZ
422 int value = event.GetInt();
423
424 wxASSERT_MSG( value == m_spinctrl->GetValue(),
425 _T("spinctrl value should be the same") );
426
427 wxLogMessage(_T("Spin control value changed, now %d"), value);
428}
59b9edf0 429#endif