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