Continuation of 'widgets' sample improvements.
[wxWidgets.git] / samples / widgets / spinbtn.cpp
1 /////////////////////////////////////////////////////////////////////////////
2 // Program: wxWidgets 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 #if wxUSE_SPINBTN
28
29 // for all others, include the necessary headers
30 #ifndef WX_PRECOMP
31 #include "wx/log.h"
32
33 #include "wx/bitmap.h"
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"
47
48 #include "icons/spinbtn.xpm"
49
50 // ----------------------------------------------------------------------------
51 // constants
52 // ----------------------------------------------------------------------------
53
54 // control ids
55 enum
56 {
57 SpinBtnPage_Reset = wxID_HIGHEST,
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
73 class SpinBtnWidgetsPage : public WidgetsPage
74 {
75 public:
76 SpinBtnWidgetsPage(WidgetsBookCtrl *book, wxImageList *imaglist);
77 virtual ~SpinBtnWidgetsPage(){};
78
79 virtual wxControl *GetWidget() const { return m_spinbtn; }
80 virtual wxControl *GetWidget2() const { return m_spinctrl; }
81 virtual void RecreateWidget() { CreateSpin(); }
82
83 protected:
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
92 void OnSpinBtn(wxSpinEvent& event);
93 void OnSpinBtnUp(wxSpinEvent& event);
94 void OnSpinBtnDown(wxSpinEvent& event);
95 void OnSpinCtrl(wxSpinEvent& event);
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
135 private:
136 DECLARE_EVENT_TABLE()
137 DECLARE_WIDGETS_PAGE(SpinBtnWidgetsPage)
138 };
139
140 // ----------------------------------------------------------------------------
141 // event tables
142 // ----------------------------------------------------------------------------
143
144 BEGIN_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
161 EVT_CHECKBOX(wxID_ANY, SpinBtnWidgetsPage::OnCheckOrRadioBox)
162 EVT_RADIOBOX(wxID_ANY, SpinBtnWidgetsPage::OnCheckOrRadioBox)
163 END_EVENT_TABLE()
164
165 // ============================================================================
166 // implementation
167 // ============================================================================
168
169 #if defined(__WXUNIVERSAL__)
170 #define FAMILY_CTRLS UNIVERSAL_CTRLS
171 #else
172 #define FAMILY_CTRLS NATIVE_CTRLS
173 #endif
174
175 IMPLEMENT_WIDGETS_PAGE(SpinBtnWidgetsPage, _T("Spin"),
176 FAMILY_CTRLS | EDITABLE_CTRLS
177 );
178
179 SpinBtnWidgetsPage::SpinBtnWidgetsPage(WidgetsBookCtrl *book,
180 wxImageList *imaglist)
181 : WidgetsPage(book)
182 {
183 m_chkVert = NULL;
184 m_chkWrap = NULL;
185 m_spinbtn = NULL;
186 m_spinctrl = NULL;
187 m_textValue = NULL;
188 m_textMin = NULL;
189 m_textMax = NULL;
190 imaglist->Add(wxBitmap(spinbtn_xpm));
191
192 // init everything
193 m_min = 0;
194 m_max = 10;
195
196 m_chkVert =
197 m_chkWrap = (wxCheckBox *)NULL;
198
199 m_spinbtn = (wxSpinButton *)NULL;
200 m_sizerSpin = (wxSizer *)NULL;
201
202 wxSizer *sizerTop = new wxBoxSizer(wxHORIZONTAL);
203
204 // left pane
205 wxStaticBox *box = new wxStaticBox(this, wxID_ANY, _T("&Set style"));
206 wxSizer *sizerLeft = new wxStaticBoxSizer(box, wxVERTICAL);
207
208 m_chkVert = CreateCheckBoxAndAddToSizer(sizerLeft, _T("&Vertical"));
209 m_chkWrap = CreateCheckBoxAndAddToSizer(sizerLeft, _T("&Wrap"));
210
211 sizerLeft->Add(5, 5, 0, wxGROW | wxALL, 5); // spacer
212
213 wxButton *btn = new wxButton(this, SpinBtnPage_Reset, _T("&Reset"));
214 sizerLeft->Add(btn, 0, wxALIGN_CENTRE_HORIZONTAL | wxALL, 15);
215
216 // middle pane
217 wxStaticBox *box2 = new wxStaticBox(this, wxID_ANY,
218 _T("&Change spinbtn value"));
219
220 wxSizer *sizerMiddle = new wxStaticBoxSizer(box2, wxVERTICAL);
221
222 wxTextCtrl *text;
223 wxSizer *sizerRow = CreateSizerWithTextAndLabel(_T("Current value"),
224 SpinBtnPage_CurValueText,
225 &text);
226 text->SetEditable(false);
227
228 sizerMiddle->Add(sizerRow, 0, wxALL | wxGROW, 5);
229
230 sizerRow = CreateSizerWithTextAndButton(SpinBtnPage_SetValue,
231 _T("Set &value"),
232 SpinBtnPage_ValueText,
233 &m_textValue);
234 sizerMiddle->Add(sizerRow, 0, wxALL | wxGROW, 5);
235
236 sizerRow = CreateSizerWithTextAndButton(SpinBtnPage_SetMinAndMax,
237 _T("&Min and max"),
238 SpinBtnPage_MinText,
239 &m_textMin);
240
241 m_textMax = new wxTextCtrl(this, SpinBtnPage_MaxText, wxEmptyString);
242 sizerRow->Add(m_textMax, 1, wxLEFT | wxALIGN_CENTRE_VERTICAL, 5);
243
244 m_textMin->SetValue( wxString::Format(_T("%d"), m_min) );
245 m_textMax->SetValue( wxString::Format(_T("%d"), m_max) );
246
247 sizerMiddle->Add(sizerRow, 0, wxALL | wxGROW, 5);
248
249 // right pane
250 wxSizer *sizerRight = new wxBoxSizer(wxVERTICAL);
251 sizerRight->SetMinSize(150, 0);
252 m_sizerSpin = sizerRight; // save it to modify it later
253
254 Reset();
255 CreateSpin();
256
257 // the 3 panes panes compose the window
258 sizerTop->Add(sizerLeft, 0, wxGROW | (wxALL & ~wxLEFT), 10);
259 sizerTop->Add(sizerMiddle, 0, wxGROW | wxALL, 10);
260 sizerTop->Add(sizerRight, 1, wxGROW | (wxALL & ~wxRIGHT), 10);
261
262 // final initializations
263 SetSizer(sizerTop);
264
265 sizerTop->Fit(this);
266 }
267
268 // ----------------------------------------------------------------------------
269 // operations
270 // ----------------------------------------------------------------------------
271
272 void SpinBtnWidgetsPage::Reset()
273 {
274 m_chkVert->SetValue(true);
275 m_chkWrap->SetValue(false);
276 }
277
278 void SpinBtnWidgetsPage::CreateSpin()
279 {
280 int flags = ms_defaultFlags;
281
282 bool isVert = m_chkVert->GetValue();
283 if ( isVert )
284 flags |= wxSP_VERTICAL;
285 else
286 flags |= wxSP_HORIZONTAL;
287
288 if ( m_chkWrap->GetValue() )
289 flags |= wxSP_WRAP;
290
291 int val = m_min;
292 if ( m_spinbtn )
293 {
294 int valOld = m_spinbtn->GetValue();
295 if ( !IsValidValue(valOld) )
296 {
297 val = valOld;
298 }
299
300 m_sizerSpin->Detach( m_spinbtn );
301 m_sizerSpin->Detach( m_spinctrl );
302
303 // there are 3 spacers left
304 m_sizerSpin->Remove( 0 );
305 m_sizerSpin->Remove( 0 );
306 m_sizerSpin->Remove( 0 );
307
308 delete m_spinbtn;
309 delete m_spinctrl;
310 }
311
312 m_spinbtn = new wxSpinButton(this, SpinBtnPage_SpinBtn,
313 wxDefaultPosition, wxDefaultSize,
314 flags);
315
316 m_spinbtn->SetValue(val);
317 m_spinbtn->SetRange(m_min, m_max);
318
319 m_spinctrl = new wxSpinCtrl(this, SpinBtnPage_SpinCtrl,
320 wxString::Format(_T("%d"), val),
321 wxDefaultPosition, wxDefaultSize,
322 flags,
323 m_min, m_max, val);
324
325 m_sizerSpin->Add(0, 0, 1);
326 m_sizerSpin->Add(m_spinbtn, 0, wxALIGN_CENTRE | wxALL, 5);
327 m_sizerSpin->Add(0, 0, 1);
328 m_sizerSpin->Add(m_spinctrl, 0, wxALIGN_CENTRE | wxALL, 5);
329 m_sizerSpin->Add(0, 0, 1);
330
331 m_sizerSpin->Layout();
332 }
333
334 // ----------------------------------------------------------------------------
335 // event handlers
336 // ----------------------------------------------------------------------------
337
338 void SpinBtnWidgetsPage::OnButtonReset(wxCommandEvent& WXUNUSED(event))
339 {
340 Reset();
341
342 CreateSpin();
343 }
344
345 void SpinBtnWidgetsPage::OnButtonSetMinAndMax(wxCommandEvent& WXUNUSED(event))
346 {
347 long minNew,
348 maxNew = 0; // init to suppress compiler warning
349 if ( !m_textMin->GetValue().ToLong(&minNew) ||
350 !m_textMax->GetValue().ToLong(&maxNew) ||
351 minNew >= maxNew )
352 {
353 wxLogWarning(_T("Invalid min/max values for the spinbtn."));
354
355 return;
356 }
357
358 m_min = minNew;
359 m_max = maxNew;
360
361 m_spinbtn->SetRange(minNew, maxNew);
362 m_spinctrl->SetRange(minNew, maxNew);
363 }
364
365 void SpinBtnWidgetsPage::OnButtonSetValue(wxCommandEvent& WXUNUSED(event))
366 {
367 long val;
368 if ( !m_textValue->GetValue().ToLong(&val) || !IsValidValue(val) )
369 {
370 wxLogWarning(_T("Invalid spinbtn value."));
371
372 return;
373 }
374
375 m_spinbtn->SetValue(val);
376 m_spinctrl->SetValue(val);
377 }
378
379 void SpinBtnWidgetsPage::OnUpdateUIValueButton(wxUpdateUIEvent& event)
380 {
381 long val;
382 event.Enable( m_textValue->GetValue().ToLong(&val) && IsValidValue(val) );
383 }
384
385 void SpinBtnWidgetsPage::OnUpdateUIMinMaxButton(wxUpdateUIEvent& event)
386 {
387 long mn, mx;
388 event.Enable( m_textMin->GetValue().ToLong(&mn) &&
389 m_textMax->GetValue().ToLong(&mx) &&
390 mn < mx);
391 }
392
393 void SpinBtnWidgetsPage::OnUpdateUIResetButton(wxUpdateUIEvent& event)
394 {
395 event.Enable( !m_chkVert->GetValue() || m_chkWrap->GetValue() );
396 }
397
398 void SpinBtnWidgetsPage::OnCheckOrRadioBox(wxCommandEvent& WXUNUSED(event))
399 {
400 CreateSpin();
401 }
402
403 void SpinBtnWidgetsPage::OnUpdateUICurValueText(wxUpdateUIEvent& event)
404 {
405 event.SetText( wxString::Format(_T("%d"), m_spinbtn->GetValue()));
406 }
407
408 void SpinBtnWidgetsPage::OnSpinBtn(wxSpinEvent& event)
409 {
410 int value = event.GetInt();
411
412 wxASSERT_MSG( value == m_spinbtn->GetValue(),
413 _T("spinbtn value should be the same") );
414
415 wxLogMessage(_T("Spin button value changed, now %d"), value);
416 }
417
418 void SpinBtnWidgetsPage::OnSpinBtnUp(wxSpinEvent& event)
419 {
420 wxLogMessage( _T("Spin button value incremented, will be %d (was %d)"),
421 event.GetInt(), m_spinbtn->GetValue() );
422 }
423
424 void SpinBtnWidgetsPage::OnSpinBtnDown(wxSpinEvent& event)
425 {
426 wxLogMessage( _T("Spin button value decremented, will be %d (was %d)"),
427 event.GetInt(), m_spinbtn->GetValue() );
428 }
429
430 void SpinBtnWidgetsPage::OnSpinCtrl(wxSpinEvent& event)
431 {
432 if (!m_spinctrl)
433 return;
434 int value = event.GetInt();
435
436 wxASSERT_MSG( value == m_spinctrl->GetValue(),
437 _T("spinctrl value should be the same") );
438
439 wxLogMessage(_T("Spin control value changed, now %d"), value);
440 }
441 #endif
442 // wxUSE_SPINBTN