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