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