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