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