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