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