Always link with expat in monolithic build.
[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 // Licence: wxWindows licence
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 // alignment radiobox values
71 enum
72 {
73 Align_Left,
74 Align_Centre,
75 Align_Right
76 };
77
78 // ----------------------------------------------------------------------------
79 // SpinBtnWidgetsPage
80 // ----------------------------------------------------------------------------
81
82 class SpinBtnWidgetsPage : public WidgetsPage
83 {
84 public:
85 SpinBtnWidgetsPage(WidgetsBookCtrl *book, wxImageList *imaglist);
86 virtual ~SpinBtnWidgetsPage(){};
87
88 virtual wxControl *GetWidget() const { return m_spinbtn; }
89 virtual Widgets GetWidgets() const
90 {
91 Widgets widgets(WidgetsPage::GetWidgets());
92 widgets.push_back(m_spinctrl);
93 widgets.push_back(m_spinctrldbl);
94 return widgets;
95 }
96
97 virtual void RecreateWidget() { CreateSpin(); }
98
99 // lazy creation of the content
100 virtual void CreateContent();
101
102 protected:
103 // event handlers
104 void OnButtonReset(wxCommandEvent& event);
105 void OnButtonClear(wxCommandEvent& event);
106 void OnButtonSetValue(wxCommandEvent& event);
107 void OnButtonSetMinAndMax(wxCommandEvent& event);
108
109 void OnCheckOrRadioBox(wxCommandEvent& event);
110
111 void OnSpinBtn(wxSpinEvent& event);
112 void OnSpinBtnUp(wxSpinEvent& event);
113 void OnSpinBtnDown(wxSpinEvent& event);
114 void OnSpinCtrl(wxSpinEvent& event);
115 void OnSpinCtrlDouble(wxSpinDoubleEvent& event);
116 void OnSpinText(wxCommandEvent& event);
117 void OnSpinTextEnter(wxCommandEvent& event);
118
119 void OnUpdateUIValueButton(wxUpdateUIEvent& event);
120 void OnUpdateUIMinMaxButton(wxUpdateUIEvent& event);
121
122 void OnUpdateUIResetButton(wxUpdateUIEvent& event);
123
124 void OnUpdateUICurValueText(wxUpdateUIEvent& event);
125
126 // reset the spinbtn parameters
127 void Reset();
128
129 // (re)create the spinbtn
130 void CreateSpin();
131
132 // is this spinbtn value in range?
133 bool IsValidValue(int val) const
134 { return (val >= m_min) && (val <= m_max); }
135
136 // the spinbtn range
137 int m_min, m_max;
138
139 // the controls
140 // ------------
141
142 // the check/radio boxes for styles
143 wxCheckBox *m_chkVert,
144 *m_chkArrowKeys,
145 *m_chkWrap,
146 *m_chkProcessEnter;
147 wxRadioBox *m_radioAlign;
148
149 // the spinbtn and the spinctrl and the sizer containing them
150 wxSpinButton *m_spinbtn;
151 wxSpinCtrl *m_spinctrl;
152 wxSpinCtrlDouble *m_spinctrldbl;
153
154 wxSizer *m_sizerSpin;
155
156 // the text entries for set value/range
157 wxTextCtrl *m_textValue,
158 *m_textMin,
159 *m_textMax;
160
161 private:
162 DECLARE_EVENT_TABLE()
163 DECLARE_WIDGETS_PAGE(SpinBtnWidgetsPage)
164 };
165
166 // ----------------------------------------------------------------------------
167 // event tables
168 // ----------------------------------------------------------------------------
169
170 BEGIN_EVENT_TABLE(SpinBtnWidgetsPage, WidgetsPage)
171 EVT_BUTTON(SpinBtnPage_Reset, SpinBtnWidgetsPage::OnButtonReset)
172 EVT_BUTTON(SpinBtnPage_SetValue, SpinBtnWidgetsPage::OnButtonSetValue)
173 EVT_BUTTON(SpinBtnPage_SetMinAndMax, SpinBtnWidgetsPage::OnButtonSetMinAndMax)
174
175 EVT_UPDATE_UI(SpinBtnPage_SetValue, SpinBtnWidgetsPage::OnUpdateUIValueButton)
176 EVT_UPDATE_UI(SpinBtnPage_SetMinAndMax, SpinBtnWidgetsPage::OnUpdateUIMinMaxButton)
177
178 EVT_UPDATE_UI(SpinBtnPage_Reset, SpinBtnWidgetsPage::OnUpdateUIResetButton)
179
180 EVT_UPDATE_UI(SpinBtnPage_CurValueText, SpinBtnWidgetsPage::OnUpdateUICurValueText)
181
182 EVT_SPIN(SpinBtnPage_SpinBtn, SpinBtnWidgetsPage::OnSpinBtn)
183 EVT_SPIN_UP(SpinBtnPage_SpinBtn, SpinBtnWidgetsPage::OnSpinBtnUp)
184 EVT_SPIN_DOWN(SpinBtnPage_SpinBtn, SpinBtnWidgetsPage::OnSpinBtnDown)
185 EVT_SPINCTRL(SpinBtnPage_SpinCtrl, SpinBtnWidgetsPage::OnSpinCtrl)
186 EVT_SPINCTRLDOUBLE(SpinBtnPage_SpinCtrlDouble, SpinBtnWidgetsPage::OnSpinCtrlDouble)
187 EVT_TEXT(SpinBtnPage_SpinCtrl, SpinBtnWidgetsPage::OnSpinText)
188 EVT_TEXT_ENTER(SpinBtnPage_SpinCtrl, SpinBtnWidgetsPage::OnSpinTextEnter)
189 EVT_TEXT(SpinBtnPage_SpinCtrlDouble, SpinBtnWidgetsPage::OnSpinText)
190
191 EVT_CHECKBOX(wxID_ANY, SpinBtnWidgetsPage::OnCheckOrRadioBox)
192 EVT_RADIOBOX(wxID_ANY, SpinBtnWidgetsPage::OnCheckOrRadioBox)
193 END_EVENT_TABLE()
194
195 // ============================================================================
196 // implementation
197 // ============================================================================
198
199 #if defined(__WXUNIVERSAL__)
200 #define FAMILY_CTRLS UNIVERSAL_CTRLS
201 #else
202 #define FAMILY_CTRLS NATIVE_CTRLS
203 #endif
204
205 IMPLEMENT_WIDGETS_PAGE(SpinBtnWidgetsPage, wxT("Spin"),
206 FAMILY_CTRLS | EDITABLE_CTRLS
207 );
208
209 SpinBtnWidgetsPage::SpinBtnWidgetsPage(WidgetsBookCtrl *book,
210 wxImageList *imaglist)
211 : WidgetsPage(book, imaglist, spinbtn_xpm)
212 {
213 m_chkVert = NULL;
214 m_chkArrowKeys = NULL;
215 m_chkWrap = NULL;
216 m_chkProcessEnter = NULL;
217 m_radioAlign = NULL;
218 m_spinbtn = NULL;
219 m_spinctrl = NULL;
220 m_spinctrldbl = NULL;
221 m_textValue = NULL;
222 m_textMin = NULL;
223 m_textMax = NULL;
224
225 m_min = 0;
226 m_max = 10;
227
228 m_sizerSpin = NULL;
229 }
230
231 void SpinBtnWidgetsPage::CreateContent()
232 {
233 wxSizer *sizerTop = new wxBoxSizer(wxHORIZONTAL);
234
235 // left pane
236 wxStaticBox *box = new wxStaticBox(this, wxID_ANY, wxT("&Set style"));
237 wxSizer *sizerLeft = new wxStaticBoxSizer(box, wxVERTICAL);
238
239 m_chkVert = CreateCheckBoxAndAddToSizer(sizerLeft, wxT("&Vertical"));
240 m_chkArrowKeys = CreateCheckBoxAndAddToSizer(sizerLeft, wxT("&Arrow Keys"));
241 m_chkWrap = CreateCheckBoxAndAddToSizer(sizerLeft, wxT("&Wrap"));
242 m_chkProcessEnter = CreateCheckBoxAndAddToSizer(sizerLeft,
243 wxT("Process &Enter"));
244
245 sizerLeft->Add(5, 5, 0, wxGROW | wxALL, 5); // spacer
246
247 static const wxString halign[] =
248 {
249 wxT("left"),
250 wxT("centre"),
251 wxT("right"),
252 };
253
254 m_radioAlign = new wxRadioBox(this, wxID_ANY, wxT("&Text alignment"),
255 wxDefaultPosition, wxDefaultSize,
256 WXSIZEOF(halign), halign, 1);
257
258 sizerLeft->Add(m_radioAlign, 0, wxGROW | wxALL, 5);
259
260 sizerLeft->Add(5, 5, 0, wxGROW | wxALL, 5); // spacer
261
262 wxButton *btn = new wxButton(this, SpinBtnPage_Reset, wxT("&Reset"));
263 sizerLeft->Add(btn, 0, wxALIGN_CENTRE_HORIZONTAL | wxALL, 15);
264
265 // middle pane
266 wxStaticBox *box2 = new wxStaticBox(this, wxID_ANY,
267 wxT("&Change spinbtn value"));
268
269 wxSizer *sizerMiddle = new wxStaticBoxSizer(box2, wxVERTICAL);
270
271 wxTextCtrl *text;
272 wxSizer *sizerRow = CreateSizerWithTextAndLabel(wxT("Current value"),
273 SpinBtnPage_CurValueText,
274 &text);
275 text->SetEditable(false);
276
277 sizerMiddle->Add(sizerRow, 0, wxALL | wxGROW, 5);
278
279 sizerRow = CreateSizerWithTextAndButton(SpinBtnPage_SetValue,
280 wxT("Set &value"),
281 SpinBtnPage_ValueText,
282 &m_textValue);
283 sizerMiddle->Add(sizerRow, 0, wxALL | wxGROW, 5);
284
285 sizerRow = CreateSizerWithTextAndButton(SpinBtnPage_SetMinAndMax,
286 wxT("&Min and max"),
287 SpinBtnPage_MinText,
288 &m_textMin);
289
290 m_textMax = new wxTextCtrl(this, SpinBtnPage_MaxText, wxEmptyString);
291 sizerRow->Add(m_textMax, 1, wxLEFT | wxALIGN_CENTRE_VERTICAL, 5);
292
293 m_textMin->SetValue( wxString::Format(wxT("%d"), m_min) );
294 m_textMax->SetValue( wxString::Format(wxT("%d"), m_max) );
295
296 sizerMiddle->Add(sizerRow, 0, wxALL | wxGROW, 5);
297
298 // right pane
299 wxSizer *sizerRight = new wxBoxSizer(wxVERTICAL);
300 sizerRight->SetMinSize(150, 0);
301 m_sizerSpin = sizerRight; // save it to modify it later
302
303 Reset();
304 CreateSpin();
305
306 // the 3 panes panes compose the window
307 sizerTop->Add(sizerLeft, 0, wxGROW | (wxALL & ~wxLEFT), 10);
308 sizerTop->Add(sizerMiddle, 0, wxGROW | wxALL, 10);
309 sizerTop->Add(sizerRight, 1, wxGROW | (wxALL & ~wxRIGHT), 10);
310
311 // final initializations
312 SetSizer(sizerTop);
313 }
314
315 // ----------------------------------------------------------------------------
316 // operations
317 // ----------------------------------------------------------------------------
318
319 void SpinBtnWidgetsPage::Reset()
320 {
321 m_chkVert->SetValue(true);
322 m_chkArrowKeys->SetValue(true);
323 m_chkWrap->SetValue(false);
324 m_chkProcessEnter->SetValue(false);
325 m_radioAlign->SetSelection(Align_Right);
326 }
327
328 void SpinBtnWidgetsPage::CreateSpin()
329 {
330 int flags = ms_defaultFlags;
331
332 bool isVert = m_chkVert->GetValue();
333 if ( isVert )
334 flags |= wxSP_VERTICAL;
335 else
336 flags |= wxSP_HORIZONTAL;
337
338 if ( m_chkArrowKeys->GetValue() )
339 flags |= wxSP_ARROW_KEYS;
340
341 if ( m_chkWrap->GetValue() )
342 flags |= wxSP_WRAP;
343
344 if ( m_chkProcessEnter->GetValue() )
345 flags |= wxTE_PROCESS_ENTER;
346
347 int textFlags = 0;
348 switch ( m_radioAlign->GetSelection() )
349 {
350 default:
351 wxFAIL_MSG(wxT("unexpected radiobox selection"));
352 // fall through
353
354 case Align_Left:
355 textFlags |= wxALIGN_LEFT; // no-op
356 break;
357
358 case Align_Centre:
359 textFlags |= wxALIGN_CENTRE_HORIZONTAL;
360 break;
361
362 case Align_Right:
363 textFlags |= wxALIGN_RIGHT;
364 break;
365 }
366
367 int val = m_min;
368 if ( m_spinbtn )
369 {
370 int valOld = m_spinbtn->GetValue();
371 if ( !IsValidValue(valOld) )
372 {
373 val = valOld;
374 }
375
376 m_sizerSpin->Clear(true /* delete windows */);
377 }
378
379 m_spinbtn = new wxSpinButton(this, SpinBtnPage_SpinBtn,
380 wxDefaultPosition, wxDefaultSize,
381 flags);
382
383 m_spinbtn->SetValue(val);
384 m_spinbtn->SetRange(m_min, m_max);
385
386 m_spinctrl = new wxSpinCtrl(this, SpinBtnPage_SpinCtrl,
387 wxString::Format(wxT("%d"), val),
388 wxDefaultPosition, wxDefaultSize,
389 flags | textFlags,
390 m_min, m_max, val);
391
392 m_spinctrldbl = new wxSpinCtrlDouble(this, SpinBtnPage_SpinCtrlDouble,
393 wxString::Format(wxT("%d"), val),
394 wxDefaultPosition, wxDefaultSize,
395 flags | textFlags,
396 m_min, m_max, val, 0.1);
397
398 // Add spacers, labels and spin controls to the sizer.
399 m_sizerSpin->Add(0, 0, 1);
400 m_sizerSpin->Add(new wxStaticText(this, wxID_ANY, wxT("wxSpinButton")),
401 0, wxALIGN_CENTRE | wxALL, 5);
402 m_sizerSpin->Add(m_spinbtn, 0, wxALIGN_CENTRE | wxALL, 5);
403 m_sizerSpin->Add(0, 0, 1);
404 m_sizerSpin->Add(new wxStaticText(this, wxID_ANY, wxT("wxSpinCtrl")),
405 0, wxALIGN_CENTRE | wxALL, 5);
406 m_sizerSpin->Add(m_spinctrl, 0, wxALIGN_CENTRE | wxALL, 5);
407 m_sizerSpin->Add(0, 0, 1);
408 m_sizerSpin->Add(new wxStaticText(this, wxID_ANY, wxT("wxSpinCtrlDouble")),
409 0, wxALIGN_CENTRE | wxALL, 5);
410 m_sizerSpin->Add(m_spinctrldbl, 0, wxALIGN_CENTRE | wxALL, 5);
411 m_sizerSpin->Add(0, 0, 1);
412
413 m_sizerSpin->Layout();
414 }
415
416 // ----------------------------------------------------------------------------
417 // event handlers
418 // ----------------------------------------------------------------------------
419
420 void SpinBtnWidgetsPage::OnButtonReset(wxCommandEvent& WXUNUSED(event))
421 {
422 Reset();
423
424 CreateSpin();
425 }
426
427 void SpinBtnWidgetsPage::OnButtonSetMinAndMax(wxCommandEvent& WXUNUSED(event))
428 {
429 long minNew,
430 maxNew = 0; // init to suppress compiler warning
431 if ( !m_textMin->GetValue().ToLong(&minNew) ||
432 !m_textMax->GetValue().ToLong(&maxNew) ||
433 minNew > maxNew )
434 {
435 wxLogWarning(wxT("Invalid min/max values for the spinbtn."));
436
437 return;
438 }
439
440 m_min = minNew;
441 m_max = maxNew;
442
443 m_spinbtn->SetRange(minNew, maxNew);
444 m_spinctrl->SetRange(minNew, maxNew);
445 m_spinctrldbl->SetRange(minNew, maxNew);
446 }
447
448 void SpinBtnWidgetsPage::OnButtonSetValue(wxCommandEvent& WXUNUSED(event))
449 {
450 long val;
451 if ( !m_textValue->GetValue().ToLong(&val) || !IsValidValue(val) )
452 {
453 wxLogWarning(wxT("Invalid spinbtn value."));
454
455 return;
456 }
457
458 m_spinbtn->SetValue(val);
459 m_spinctrl->SetValue(val);
460 m_spinctrldbl->SetValue(val);
461 }
462
463 void SpinBtnWidgetsPage::OnUpdateUIValueButton(wxUpdateUIEvent& event)
464 {
465 long val;
466 event.Enable( m_textValue->GetValue().ToLong(&val) && IsValidValue(val) );
467 }
468
469 void SpinBtnWidgetsPage::OnUpdateUIMinMaxButton(wxUpdateUIEvent& event)
470 {
471 long mn, mx;
472 event.Enable( m_textMin->GetValue().ToLong(&mn) &&
473 m_textMax->GetValue().ToLong(&mx) &&
474 mn <= mx);
475 }
476
477 void SpinBtnWidgetsPage::OnUpdateUIResetButton(wxUpdateUIEvent& event)
478 {
479 event.Enable( !m_chkVert->GetValue() ||
480 m_chkWrap->GetValue() ||
481 m_chkProcessEnter->GetValue() );
482 }
483
484 void SpinBtnWidgetsPage::OnCheckOrRadioBox(wxCommandEvent& WXUNUSED(event))
485 {
486 CreateSpin();
487 }
488
489 void SpinBtnWidgetsPage::OnUpdateUICurValueText(wxUpdateUIEvent& event)
490 {
491 event.SetText( wxString::Format(wxT("%d"), m_spinbtn->GetValue()));
492 }
493
494 void SpinBtnWidgetsPage::OnSpinBtn(wxSpinEvent& event)
495 {
496 int value = event.GetInt();
497
498 wxASSERT_MSG( value == m_spinbtn->GetValue(),
499 wxT("spinbtn value should be the same") );
500
501 wxLogMessage(wxT("Spin button value changed, now %d"), value);
502 }
503
504 void SpinBtnWidgetsPage::OnSpinBtnUp(wxSpinEvent& event)
505 {
506 wxLogMessage( wxT("Spin button value incremented, will be %d (was %d)"),
507 event.GetInt(), m_spinbtn->GetValue() );
508 }
509
510 void SpinBtnWidgetsPage::OnSpinBtnDown(wxSpinEvent& event)
511 {
512 wxLogMessage( wxT("Spin button value decremented, will be %d (was %d)"),
513 event.GetInt(), m_spinbtn->GetValue() );
514 }
515
516 void SpinBtnWidgetsPage::OnSpinCtrl(wxSpinEvent& event)
517 {
518 int value = event.GetInt();
519
520 wxASSERT_MSG( value == m_spinctrl->GetValue(),
521 wxT("spinctrl value should be the same") );
522
523 wxLogMessage(wxT("Spin control value changed, now %d"), value);
524 }
525
526 void SpinBtnWidgetsPage::OnSpinCtrlDouble(wxSpinDoubleEvent& event)
527 {
528 double value = event.GetValue();
529
530 wxLogMessage(wxT("Spin control value changed, now %g"), value);
531 }
532
533 void SpinBtnWidgetsPage::OnSpinText(wxCommandEvent& event)
534 {
535 wxLogMessage(wxT("Text changed in spin control, now \"%s\""),
536 event.GetString().c_str());
537 }
538
539 void SpinBtnWidgetsPage::OnSpinTextEnter(wxCommandEvent& event)
540 {
541 wxLogMessage("\"Enter\" pressed in spin control, text is \"%s\"",
542 event.GetString());
543 }
544
545 #endif // wxUSE_SPINBTN