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