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