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