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