]> git.saurik.com Git - wxWidgets.git/blame - samples/widgets/slider.cpp
reverted my previous change to WINVER - it is already defined elsewhere
[wxWidgets.git] / samples / widgets / slider.cpp
CommitLineData
32b8ec41
VZ
1/////////////////////////////////////////////////////////////////////////////
2// Program: wxWindows Widgets Sample
3// Name: slider.cpp
4// Purpose: Part of the widgets sample showing wxSlider
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/button.h"
32 #include "wx/checkbox.h"
33 #include "wx/radiobox.h"
34 #include "wx/slider.h"
35 #include "wx/statbox.h"
36 #include "wx/textctrl.h"
37#endif
38
39#include "wx/sizer.h"
40
41#include "widgets.h"
42
43#include "icons/slider.xpm"
44
45// ----------------------------------------------------------------------------
46// constants
47// ----------------------------------------------------------------------------
48
49// control ids
50enum
51{
52 SliderPage_Reset = 100,
53 SliderPage_Clear,
54 SliderPage_SetValue,
55 SliderPage_SetMinAndMax,
56 SliderPage_SetTickFreq,
57 SliderPage_CurValueText,
58 SliderPage_ValueText,
59 SliderPage_MinText,
60 SliderPage_MaxText,
61 SliderPage_TickFreqText,
62 SliderPage_OtherSide,
63 SliderPage_Slider
64};
65
66// ----------------------------------------------------------------------------
67// SliderWidgetsPage
68// ----------------------------------------------------------------------------
69
70class SliderWidgetsPage : public WidgetsPage
71{
72public:
73 SliderWidgetsPage(wxNotebook *notebook, wxImageList *imaglist);
74 virtual ~SliderWidgetsPage();
75
76protected:
77 // event handlers
78 void OnButtonReset(wxCommandEvent& event);
79 void OnButtonClear(wxCommandEvent& event);
80 void OnButtonSetValue(wxCommandEvent& event);
81 void OnButtonSetMinAndMax(wxCommandEvent& event);
82 void OnButtonSetTickFreq(wxCommandEvent& event);
83
84 void OnCheckOrRadioBox(wxCommandEvent& event);
85
86 void OnSlider(wxCommandEvent& event);
87
88 void OnUpdateUIOtherSide(wxUpdateUIEvent& event);
89 void OnUpdateUIValueButton(wxUpdateUIEvent& event);
90 void OnUpdateUIMinMaxButton(wxUpdateUIEvent& event);
91 void OnUpdateUITickFreq(wxUpdateUIEvent& event);
92
93 void OnUpdateUIResetButton(wxUpdateUIEvent& event);
94
95 void OnUpdateUICurValueText(wxUpdateUIEvent& event);
96
97 // reset the slider parameters
98 void Reset();
99
100 // (re)create the slider
101 void CreateSlider();
102
103 // set the tick frequency from the text field value
104 void DoSetTickFreq();
105
106 // is this slider value in range?
107 bool IsValidValue(int val) const
108 { return (val >= m_min) && (val <= m_max); }
109
110 // the slider range
111 int m_min, m_max;
112
113 // the controls
114 // ------------
115
116 // the check/radio boxes for styles
117 wxCheckBox *m_chkLabels,
118 *m_chkOtherSide,
119 *m_chkVert,
120 *m_chkTicks;
121
122 // the slider itself and the sizer it is in
123 wxSlider *m_slider;
124 wxSizer *m_sizerSlider;
125
126 // the text entries for set value/range
127 wxTextCtrl *m_textValue,
128 *m_textMin,
129 *m_textMax,
130 *m_textTickFreq;
131
132private:
133 DECLARE_EVENT_TABLE();
134 DECLARE_WIDGETS_PAGE(SliderWidgetsPage);
135};
136
137// ----------------------------------------------------------------------------
138// event tables
139// ----------------------------------------------------------------------------
140
141BEGIN_EVENT_TABLE(SliderWidgetsPage, WidgetsPage)
142 EVT_BUTTON(SliderPage_Reset, SliderWidgetsPage::OnButtonReset)
143 EVT_BUTTON(SliderPage_SetValue, SliderWidgetsPage::OnButtonSetValue)
144 EVT_BUTTON(SliderPage_SetMinAndMax, SliderWidgetsPage::OnButtonSetMinAndMax)
145 EVT_BUTTON(SliderPage_SetTickFreq, SliderWidgetsPage::OnButtonSetTickFreq)
146
147 EVT_UPDATE_UI(SliderPage_OtherSide, SliderWidgetsPage::OnUpdateUIOtherSide)
148
149 EVT_UPDATE_UI(SliderPage_SetValue, SliderWidgetsPage::OnUpdateUIValueButton)
150 EVT_UPDATE_UI(SliderPage_SetMinAndMax, SliderWidgetsPage::OnUpdateUIMinMaxButton)
151 EVT_UPDATE_UI(SliderPage_SetTickFreq, SliderWidgetsPage::OnUpdateUITickFreq)
152 EVT_UPDATE_UI(SliderPage_TickFreqText, SliderWidgetsPage::OnUpdateUITickFreq)
153
154 EVT_UPDATE_UI(SliderPage_Reset, SliderWidgetsPage::OnUpdateUIResetButton)
155
156 EVT_UPDATE_UI(SliderPage_CurValueText, SliderWidgetsPage::OnUpdateUICurValueText)
157
158 EVT_SLIDER(SliderPage_Slider, SliderWidgetsPage::OnSlider)
159
160 EVT_CHECKBOX(-1, SliderWidgetsPage::OnCheckOrRadioBox)
161 EVT_RADIOBOX(-1, SliderWidgetsPage::OnCheckOrRadioBox)
162END_EVENT_TABLE()
163
164// ============================================================================
165// implementation
166// ============================================================================
167
168IMPLEMENT_WIDGETS_PAGE(SliderWidgetsPage, _T("Slider"));
169
170SliderWidgetsPage::SliderWidgetsPage(wxNotebook *notebook,
171 wxImageList *imaglist)
172 : WidgetsPage(notebook)
173{
174 imaglist->Add(wxBitmap(slider_xpm));
175
176 // init everything
177 m_min = 0;
178 m_max = 100;
179
180 m_chkVert =
181 m_chkTicks =
182 m_chkLabels =
183 m_chkOtherSide = (wxCheckBox *)NULL;
184
185 m_slider = (wxSlider *)NULL;
186 m_sizerSlider = (wxSizer *)NULL;
187
188 wxSizer *sizerTop = new wxBoxSizer(wxHORIZONTAL);
189
190 // left pane
191 wxStaticBox *box = new wxStaticBox(this, -1, _T("&Set style"));
192 wxSizer *sizerLeft = new wxStaticBoxSizer(box, wxVERTICAL);
193
194 m_chkVert = CreateCheckBoxAndAddToSizer(sizerLeft, _T("&Vertical"));
195 m_chkTicks = CreateCheckBoxAndAddToSizer(sizerLeft, _T("Show &ticks"));
196 m_chkLabels = CreateCheckBoxAndAddToSizer(sizerLeft, _T("Show &labels"));
197 m_chkOtherSide = CreateCheckBoxAndAddToSizer
198 (
199 sizerLeft,
200 _T("On &other side"),
201 SliderPage_OtherSide
202 );
203
204 sizerLeft->Add(5, 5, 0, wxGROW | wxALL, 5); // spacer
205
206 wxButton *btn = new wxButton(this, SliderPage_Reset, _T("&Reset"));
207 sizerLeft->Add(btn, 0, wxALIGN_CENTRE_HORIZONTAL | wxALL, 15);
208
209 // middle pane
210 wxStaticBox *box2 = new wxStaticBox(this, -1, _T("&Change slider value"));
211 wxSizer *sizerMiddle = new wxStaticBoxSizer(box2, wxVERTICAL);
212
213 wxTextCtrl *text;
214 wxSizer *sizerRow = CreateSizerWithTextAndLabel(_T("Current value"),
215 SliderPage_CurValueText,
216 &text);
217 text->SetEditable(FALSE);
218
219 sizerMiddle->Add(sizerRow, 0, wxALL | wxGROW, 5);
220
221 sizerRow = CreateSizerWithTextAndButton(SliderPage_SetValue,
222 _T("Set &value"),
223 SliderPage_ValueText,
224 &m_textValue);
225 sizerMiddle->Add(sizerRow, 0, wxALL | wxGROW, 5);
226
227 sizerRow = CreateSizerWithTextAndButton(SliderPage_SetMinAndMax,
228 _T("&Min and max"),
229 SliderPage_MinText,
230 &m_textMin);
231
232 m_textMax = new wxTextCtrl(this, SliderPage_MaxText, _T(""));
233 sizerRow->Add(m_textMax, 1, wxLEFT | wxALIGN_CENTRE_VERTICAL, 5);
234
235 m_textMin->SetValue(wxString::Format(_T("%lu"), m_min));
236 m_textMax->SetValue(wxString::Format(_T("%lu"), m_max));
237
238 sizerMiddle->Add(sizerRow, 0, wxALL | wxGROW, 5);
239
240 sizerRow = CreateSizerWithTextAndButton(SliderPage_SetTickFreq,
241 _T("Tick &frequency"),
242 SliderPage_TickFreqText,
243 &m_textTickFreq);
244
245 m_textTickFreq->SetValue(_T("10"));
246
247 sizerMiddle->Add(sizerRow, 0, wxALL | wxGROW, 5);
248
249 // right pane
250 wxSizer *sizerRight = new wxBoxSizer(wxHORIZONTAL);
251 sizerRight->SetMinSize(250, 0);
252 m_sizerSlider = sizerRight; // save it to modify it later
253
254 Reset();
255 CreateSlider();
256
257 // the 3 panes panes compose the window
258 sizerTop->Add(sizerLeft, 0, wxGROW | (wxALL & ~wxLEFT), 10);
259 sizerTop->Add(sizerMiddle, 1, wxGROW | wxALL, 10);
260 sizerTop->Add(sizerRight, 1, wxGROW | (wxALL & ~wxRIGHT), 10);
261
262 // final initializations
263 SetAutoLayout(TRUE);
264 SetSizer(sizerTop);
265
266 sizerTop->Fit(this);
267}
268
269SliderWidgetsPage::~SliderWidgetsPage()
270{
271}
272
273// ----------------------------------------------------------------------------
274// operations
275// ----------------------------------------------------------------------------
276
277void SliderWidgetsPage::Reset()
278{
279 m_chkLabels->SetValue(TRUE);
280 m_chkTicks->SetValue(FALSE);
281 m_chkVert->SetValue(FALSE);
282 m_chkOtherSide->SetValue(FALSE);
283}
284
285void SliderWidgetsPage::CreateSlider()
286{
287 int flags = 0;
288
289 bool isVert = m_chkVert->GetValue();
290 if ( isVert )
291 flags |= wxSL_VERTICAL;
292 else
293 flags |= wxSL_HORIZONTAL;
294
295 if ( m_chkLabels->GetValue() )
296 {
297 flags |= wxSL_LABELS;
298
299 if ( m_chkOtherSide->GetValue() )
300 flags |= isVert ? wxSL_RIGHT : wxSL_BOTTOM;
301 else
302 flags |= isVert ? wxSL_LEFT : wxSL_TOP;
303 }
304
305 if ( m_chkTicks->GetValue() )
306 {
307 flags |= wxSL_AUTOTICKS;
308 }
309
310 int val = m_min;
311 if ( m_slider )
312 {
313 int valOld = m_slider->GetValue();
314 if ( !IsValidValue(valOld) )
315 {
316 val = valOld;
317 }
318
319 m_sizerSlider->Remove(m_slider);
320
321 if ( m_sizerSlider->GetChildren().GetCount() )
322 {
323 // we have 2 spacers, remove them too
324 m_sizerSlider->Remove((int)0);
325 m_sizerSlider->Remove((int)0);
326 }
327
328 delete m_slider;
329 }
330
331 m_slider = new wxSlider(this, SliderPage_Slider,
332 val, m_min, m_max,
333 wxDefaultPosition, wxDefaultSize,
334 flags);
335
336 if ( isVert )
337 {
338 m_sizerSlider->Add(0, 0, 1);
339 m_sizerSlider->Add(m_slider, 0, wxGROW | wxALL, 5);
340 m_sizerSlider->Add(0, 0, 1);
341 }
342 else
343 {
344 m_sizerSlider->Add(m_slider, 1, wxCENTRE | wxALL, 5);
345 }
346
347 if ( m_chkTicks->GetValue() )
348 {
349 DoSetTickFreq();
350 }
351
352 m_sizerSlider->Layout();
353}
354
355void SliderWidgetsPage::DoSetTickFreq()
356{
357 long freq;
358 if ( !m_textTickFreq->GetValue().ToLong(&freq) )
359 {
360 wxLogWarning(_T("Invalid slider tick frequency"));
361
362 return;
363 }
364
365 m_slider->SetTickFreq(freq, 0 /* unused */);
366}
367
368// ----------------------------------------------------------------------------
369// event handlers
370// ----------------------------------------------------------------------------
371
372void SliderWidgetsPage::OnButtonReset(wxCommandEvent& WXUNUSED(event))
373{
374 Reset();
375
376 CreateSlider();
377}
378
379void SliderWidgetsPage::OnButtonSetTickFreq(wxCommandEvent& WXUNUSED(event))
380{
381 DoSetTickFreq();
382}
383
384void SliderWidgetsPage::OnButtonSetMinAndMax(wxCommandEvent& WXUNUSED(event))
385{
386 long minNew,
387 maxNew = 0; // init to suppress compiler warning
388 if ( !m_textMin->GetValue().ToLong(&minNew) ||
389 !m_textMax->GetValue().ToLong(&maxNew) ||
390 minNew >= maxNew )
391 {
392 wxLogWarning(_T("Invalid min/max values for the slider."));
393
394 return;
395 }
396
397 m_min = minNew;
398 m_max = maxNew;
399
400 m_slider->SetRange(minNew, maxNew);
401}
402
403void SliderWidgetsPage::OnButtonSetValue(wxCommandEvent& WXUNUSED(event))
404{
405 long val;
406 if ( !m_textValue->GetValue().ToLong(&val) || !IsValidValue(val) )
407 {
408 wxLogWarning(_T("Invalid slider value."));
409
410 return;
411 }
412
413 m_slider->SetValue(val);
414}
415
416void SliderWidgetsPage::OnUpdateUIValueButton(wxUpdateUIEvent& event)
417{
418 long val;
419 event.Enable( m_textValue->GetValue().ToLong(&val) && IsValidValue(val) );
420}
421
422void SliderWidgetsPage::OnUpdateUITickFreq(wxUpdateUIEvent& event)
423{
424 long freq;
425 event.Enable( m_chkTicks->GetValue() &&
426 m_textTickFreq->GetValue().ToLong(&freq) &&
427 (freq > 0) && (freq <= m_max - m_min) );
428}
429
430void SliderWidgetsPage::OnUpdateUIMinMaxButton(wxUpdateUIEvent& event)
431{
432 long mn, mx;
433 event.Enable( m_textMin->GetValue().ToLong(&mn) &&
434 m_textMax->GetValue().ToLong(&mx) &&
435 mn < mx);
436}
437
438void SliderWidgetsPage::OnUpdateUIResetButton(wxUpdateUIEvent& event)
439{
440 event.Enable( m_chkVert->GetValue() ||
441 !m_chkLabels->GetValue() ||
442 m_chkOtherSide->GetValue() ||
443 m_chkTicks->GetValue() );
444}
445
446void SliderWidgetsPage::OnCheckOrRadioBox(wxCommandEvent& event)
447{
448 CreateSlider();
449}
450
451void SliderWidgetsPage::OnUpdateUICurValueText(wxUpdateUIEvent& event)
452{
453 event.SetText( wxString::Format(_T("%d"), m_slider->GetValue()));
454}
455
456void SliderWidgetsPage::OnUpdateUIOtherSide(wxUpdateUIEvent& event)
457{
458 event.Enable( m_chkLabels->GetValue() );
459}
460
461void SliderWidgetsPage::OnSlider(wxCommandEvent& event)
462{
463 int value = event.GetInt();
464
465 wxASSERT_MSG( value == m_slider->GetValue(),
466 _T("slider value should be the same") );
467
468 wxLogMessage(_T("Slider value changed, now %d"), value);
469}
470