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