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