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