1 /////////////////////////////////////////////////////////////////////////////
2 // Program: wxWidgets Widgets Sample
4 // Purpose: Part of the widgets sample showing wxSlider
5 // Author: Vadim Zeitlin
7 // Copyright: (c) 2001 Vadim Zeitlin
8 // Licence: wxWindows licence
9 /////////////////////////////////////////////////////////////////////////////
11 // ============================================================================
13 // ============================================================================
15 // ----------------------------------------------------------------------------
17 // ----------------------------------------------------------------------------
19 // for compilers that support precompilation, includes "wx/wx.h".
20 #include "wx/wxprec.h"
28 // for all others, include the necessary headers
32 #include "wx/bitmap.h"
33 #include "wx/button.h"
34 #include "wx/checkbox.h"
35 #include "wx/radiobox.h"
36 #include "wx/slider.h"
37 #include "wx/statbox.h"
38 #include "wx/textctrl.h"
42 #include "wx/tooltip.h"
49 #include "icons/slider.xpm"
51 // ----------------------------------------------------------------------------
53 // ----------------------------------------------------------------------------
58 SliderPage_Reset
= wxID_HIGHEST
,
61 SliderPage_SetMinAndMax
,
62 SliderPage_SetLineSize
,
63 SliderPage_SetPageSize
,
64 SliderPage_SetTickFreq
,
65 SliderPage_SetThumbLen
,
66 SliderPage_CurValueText
,
70 SliderPage_LineSizeText
,
71 SliderPage_PageSizeText
,
72 SliderPage_TickFreqText
,
73 SliderPage_ThumbLenText
,
74 SliderPage_RadioSides
,
79 // sides radiobox values
89 // ----------------------------------------------------------------------------
91 // ----------------------------------------------------------------------------
93 class SliderWidgetsPage
: public WidgetsPage
96 SliderWidgetsPage(WidgetsBookCtrl
*book
, wxImageList
*imaglist
);
97 virtual ~SliderWidgetsPage(){};
99 virtual wxControl
*GetWidget() const { return m_slider
; }
100 virtual void RecreateWidget() { CreateSlider(); }
102 // lazy creation of the content
103 virtual void CreateContent();
107 void OnButtonReset(wxCommandEvent
& event
);
108 void OnButtonClear(wxCommandEvent
& event
);
109 void OnButtonSetValue(wxCommandEvent
& event
);
110 void OnButtonSetMinAndMax(wxCommandEvent
& event
);
111 void OnButtonSetLineSize(wxCommandEvent
& event
);
112 void OnButtonSetPageSize(wxCommandEvent
& event
);
113 void OnButtonSetTickFreq(wxCommandEvent
& event
);
114 void OnButtonSetThumbLen(wxCommandEvent
& event
);
116 void OnCheckOrRadioBox(wxCommandEvent
& event
);
118 void OnSlider(wxScrollEvent
& event
);
120 void OnUpdateUIValueButton(wxUpdateUIEvent
& event
);
121 void OnUpdateUIMinMaxButton(wxUpdateUIEvent
& event
);
122 void OnUpdateUILineSize(wxUpdateUIEvent
& event
);
123 void OnUpdateUIPageSize(wxUpdateUIEvent
& event
);
124 void OnUpdateUITickFreq(wxUpdateUIEvent
& event
);
125 void OnUpdateUIThumbLen(wxUpdateUIEvent
& event
);
126 void OnUpdateUIRadioSides(wxUpdateUIEvent
& event
);
127 void OnUpdateUIBothSides(wxUpdateUIEvent
& event
);
129 void OnUpdateUIResetButton(wxUpdateUIEvent
& event
);
131 void OnUpdateUICurValueText(wxUpdateUIEvent
& event
);
133 // reset the slider parameters
136 // (re)create the slider
139 // set the line size from the text field value
140 void DoSetLineSize();
142 // set the page size from the text field value
143 void DoSetPageSize();
145 // set the tick frequency from the text field value
146 void DoSetTickFreq();
148 // set the thumb len from the text field value
149 void DoSetThumbLen();
151 // is this slider value in range?
152 bool IsValidValue(int val
) const
153 { return (val
>= m_min
) && (val
<= m_max
); }
161 // the check/radio boxes for styles
162 wxCheckBox
*m_chkMinMaxLabels
,
168 wxRadioBox
*m_radioSides
;
170 // the slider itself and the sizer it is in
172 wxSizer
*m_sizerSlider
;
174 // the text entries for set value/range
175 wxTextCtrl
*m_textValue
,
184 DECLARE_EVENT_TABLE()
185 DECLARE_WIDGETS_PAGE(SliderWidgetsPage
)
188 // ----------------------------------------------------------------------------
190 // ----------------------------------------------------------------------------
192 BEGIN_EVENT_TABLE(SliderWidgetsPage
, WidgetsPage
)
193 EVT_BUTTON(SliderPage_Reset
, SliderWidgetsPage::OnButtonReset
)
194 EVT_BUTTON(SliderPage_SetValue
, SliderWidgetsPage::OnButtonSetValue
)
195 EVT_BUTTON(SliderPage_SetMinAndMax
, SliderWidgetsPage::OnButtonSetMinAndMax
)
196 EVT_BUTTON(SliderPage_SetLineSize
, SliderWidgetsPage::OnButtonSetLineSize
)
197 EVT_BUTTON(SliderPage_SetPageSize
, SliderWidgetsPage::OnButtonSetPageSize
)
198 EVT_BUTTON(SliderPage_SetTickFreq
, SliderWidgetsPage::OnButtonSetTickFreq
)
199 EVT_BUTTON(SliderPage_SetThumbLen
, SliderWidgetsPage::OnButtonSetThumbLen
)
201 EVT_UPDATE_UI(SliderPage_SetValue
, SliderWidgetsPage::OnUpdateUIValueButton
)
202 EVT_UPDATE_UI(SliderPage_SetMinAndMax
, SliderWidgetsPage::OnUpdateUIMinMaxButton
)
203 EVT_UPDATE_UI(SliderPage_SetLineSize
, SliderWidgetsPage::OnUpdateUILineSize
)
204 EVT_UPDATE_UI(SliderPage_SetPageSize
, SliderWidgetsPage::OnUpdateUIPageSize
)
205 EVT_UPDATE_UI(SliderPage_SetTickFreq
, SliderWidgetsPage::OnUpdateUITickFreq
)
206 EVT_UPDATE_UI(SliderPage_SetThumbLen
, SliderWidgetsPage::OnUpdateUIThumbLen
)
207 EVT_UPDATE_UI(SliderPage_RadioSides
, SliderWidgetsPage::OnUpdateUIRadioSides
)
208 EVT_UPDATE_UI(SliderPage_BothSides
, SliderWidgetsPage::OnUpdateUIBothSides
)
210 EVT_UPDATE_UI(SliderPage_Reset
, SliderWidgetsPage::OnUpdateUIResetButton
)
212 EVT_UPDATE_UI(SliderPage_CurValueText
, SliderWidgetsPage::OnUpdateUICurValueText
)
214 EVT_COMMAND_SCROLL(SliderPage_Slider
, SliderWidgetsPage::OnSlider
)
216 EVT_CHECKBOX(wxID_ANY
, SliderWidgetsPage::OnCheckOrRadioBox
)
217 EVT_RADIOBOX(wxID_ANY
, SliderWidgetsPage::OnCheckOrRadioBox
)
220 // ============================================================================
222 // ============================================================================
224 #if defined(__WXUNIVERSAL__)
225 #define FAMILY_CTRLS UNIVERSAL_CTRLS
227 #define FAMILY_CTRLS NATIVE_CTRLS
230 IMPLEMENT_WIDGETS_PAGE(SliderWidgetsPage
, wxT("Slider"), FAMILY_CTRLS
);
232 SliderWidgetsPage::SliderWidgetsPage(WidgetsBookCtrl
*book
,
233 wxImageList
*imaglist
)
234 : WidgetsPage(book
, imaglist
, slider_xpm
)
244 m_chkBothSides
= (wxCheckBox
*)NULL
;
246 m_radioSides
= (wxRadioBox
*)NULL
;
248 m_slider
= (wxSlider
*)NULL
;
249 m_sizerSlider
= (wxSizer
*)NULL
;
252 void SliderWidgetsPage::CreateContent()
254 wxSizer
*sizerTop
= new wxBoxSizer(wxHORIZONTAL
);
257 wxStaticBox
*box
= new wxStaticBox(this, wxID_ANY
, wxT("&Set style"));
258 wxSizer
*sizerLeft
= new wxStaticBoxSizer(box
, wxVERTICAL
);
260 m_chkInverse
= CreateCheckBoxAndAddToSizer(sizerLeft
, wxT("&Inverse"));
261 m_chkTicks
= CreateCheckBoxAndAddToSizer(sizerLeft
, wxT("Show &ticks"));
262 m_chkMinMaxLabels
= CreateCheckBoxAndAddToSizer(sizerLeft
, wxT("Show min/max &labels"));
263 m_chkValueLabel
= CreateCheckBoxAndAddToSizer(sizerLeft
, wxT("Show &value label"));
264 static const wxString sides
[] =
272 m_radioSides
= new wxRadioBox(this, SliderPage_RadioSides
, wxT("&Label position"),
273 wxDefaultPosition
, wxDefaultSize
,
274 WXSIZEOF(sides
), sides
,
275 1, wxRA_SPECIFY_COLS
);
276 sizerLeft
->Add(m_radioSides
, 0, wxGROW
| wxALL
, 5);
277 m_chkBothSides
= CreateCheckBoxAndAddToSizer
278 (sizerLeft
, wxT("&Both sides"), SliderPage_BothSides
);
280 m_chkBothSides
->SetToolTip( wxT("\"Both sides\" is only supported \nin Win95 and Universal") );
281 #endif // wxUSE_TOOLTIPS
283 sizerLeft
->Add(5, 5, 0, wxGROW
| wxALL
, 5); // spacer
285 wxButton
*btn
= new wxButton(this, SliderPage_Reset
, wxT("&Reset"));
286 sizerLeft
->Add(btn
, 0, wxALIGN_CENTRE_HORIZONTAL
| wxALL
, 15);
289 wxStaticBox
*box2
= new wxStaticBox(this, wxID_ANY
, wxT("&Change slider value"));
290 wxSizer
*sizerMiddle
= new wxStaticBoxSizer(box2
, wxVERTICAL
);
293 wxSizer
*sizerRow
= CreateSizerWithTextAndLabel(wxT("Current value"),
294 SliderPage_CurValueText
,
296 text
->SetEditable(false);
298 sizerMiddle
->Add(sizerRow
, 0, wxALL
| wxGROW
, 5);
300 sizerRow
= CreateSizerWithTextAndButton(SliderPage_SetValue
,
302 SliderPage_ValueText
,
304 sizerMiddle
->Add(sizerRow
, 0, wxALL
| wxGROW
, 5);
306 sizerRow
= CreateSizerWithTextAndButton(SliderPage_SetMinAndMax
,
311 m_textMax
= new wxTextCtrl(this, SliderPage_MaxText
, wxEmptyString
);
312 sizerRow
->Add(m_textMax
, 1, wxLEFT
| wxALIGN_CENTRE_VERTICAL
, 5);
314 m_textMin
->SetValue( wxString::Format(wxT("%d"), m_min
) );
315 m_textMax
->SetValue( wxString::Format(wxT("%d"), m_max
) );
317 sizerMiddle
->Add(sizerRow
, 0, wxALL
| wxGROW
, 5);
319 sizerRow
= CreateSizerWithTextAndButton(SliderPage_SetLineSize
,
321 SliderPage_LineSizeText
,
324 sizerMiddle
->Add(sizerRow
, 0, wxALL
| wxGROW
, 5);
326 sizerRow
= CreateSizerWithTextAndButton(SliderPage_SetPageSize
,
328 SliderPage_PageSizeText
,
331 sizerMiddle
->Add(sizerRow
, 0, wxALL
| wxGROW
, 5);
333 sizerRow
= CreateSizerWithTextAndButton(SliderPage_SetTickFreq
,
334 wxT("Tick &frequency"),
335 SliderPage_TickFreqText
,
338 m_textTickFreq
->SetValue(wxT("10"));
340 sizerMiddle
->Add(sizerRow
, 0, wxALL
| wxGROW
, 5);
342 sizerRow
= CreateSizerWithTextAndButton(SliderPage_SetThumbLen
,
343 wxT("Thumb &length"),
344 SliderPage_ThumbLenText
,
347 sizerMiddle
->Add(sizerRow
, 0, wxALL
| wxGROW
, 5);
350 wxSizer
*sizerRight
= new wxBoxSizer(wxHORIZONTAL
);
351 sizerRight
->SetMinSize(150, 40);
352 m_sizerSlider
= sizerRight
; // save it to modify it later
357 m_textLineSize
->SetValue(wxString::Format(wxT("%d"), m_slider
->GetLineSize()));
358 m_textPageSize
->SetValue(wxString::Format(wxT("%d"), m_slider
->GetPageSize()));
360 // the 3 panes panes compose the window
361 sizerTop
->Add(sizerLeft
, 0, wxGROW
| (wxALL
& ~wxLEFT
), 10);
362 sizerTop
->Add(sizerMiddle
, 0, wxGROW
| wxALL
, 10);
363 sizerTop
->Add(sizerRight
, 1, wxGROW
| (wxALL
& ~wxRIGHT
), 10);
365 // final initializations
369 // ----------------------------------------------------------------------------
371 // ----------------------------------------------------------------------------
373 void SliderWidgetsPage::Reset()
375 m_chkInverse
->SetValue(false);
376 m_chkTicks
->SetValue(true);
377 m_chkValueLabel
->SetValue(true);
378 m_chkMinMaxLabels
->SetValue(true);
379 m_chkBothSides
->SetValue(false);
381 m_radioSides
->SetSelection(SliderTicks_None
);
384 void SliderWidgetsPage::CreateSlider()
386 int flags
= ms_defaultFlags
;
388 if ( m_chkInverse
->GetValue() )
390 flags
|= wxSL_INVERSE
;
393 if ( m_chkMinMaxLabels
->GetValue() )
395 flags
|= wxSL_MIN_MAX_LABELS
;
398 if ( m_chkValueLabel
->GetValue() )
400 flags
|= wxSL_VALUE_LABEL
;
403 if ( m_chkTicks
->GetValue() )
405 flags
|= wxSL_AUTOTICKS
;
408 // notice that the style names refer to the _ticks_ positions while we want
409 // to allow the user to select the label(s) positions and the labels are on
410 // the opposite side from the ticks, hence the apparent reversal below
411 switch ( m_radioSides
->GetSelection() )
413 case SliderTicks_None
:
416 case SliderTicks_Top
:
417 flags
|= wxSL_BOTTOM
;
420 case SliderTicks_Left
:
421 flags
|= wxSL_RIGHT
| wxSL_VERTICAL
;
424 case SliderTicks_Bottom
:
428 case SliderTicks_Right
:
429 flags
|= wxSL_LEFT
| wxSL_VERTICAL
;
433 wxFAIL_MSG(wxT("unexpected radiobox selection"));
437 if ( m_chkBothSides
->GetValue() )
445 int valOld
= m_slider
->GetValue();
446 if ( !IsValidValue(valOld
) )
451 m_sizerSlider
->Detach( m_slider
);
453 if ( m_sizerSlider
->GetChildren().GetCount() )
455 // we have 2 spacers, remove them too
456 m_sizerSlider
->Remove( 0 );
457 m_sizerSlider
->Remove( 0 );
463 m_slider
= new wxSlider(this, SliderPage_Slider
,
465 wxDefaultPosition
, wxDefaultSize
,
468 if ( m_slider
->HasFlag(wxSL_VERTICAL
) )
470 m_sizerSlider
->Add(0, 0, 1);
471 m_sizerSlider
->Add(m_slider
, 0, wxGROW
| wxALL
, 5);
472 m_sizerSlider
->Add(0, 0, 1);
476 m_sizerSlider
->Add(m_slider
, 1, wxCENTRE
| wxALL
, 5);
479 if ( m_chkTicks
->GetValue() )
484 m_sizerSlider
->Layout();
487 void SliderWidgetsPage::DoSetLineSize()
490 if ( !m_textLineSize
->GetValue().ToLong(&lineSize
) )
492 wxLogWarning(wxT("Invalid slider line size"));
497 m_slider
->SetLineSize(lineSize
);
499 if ( m_slider
->GetLineSize() != lineSize
)
501 wxLogWarning(wxT("Invalid line size in slider."));
505 void SliderWidgetsPage::DoSetPageSize()
508 if ( !m_textPageSize
->GetValue().ToLong(&pageSize
) )
510 wxLogWarning(wxT("Invalid slider page size"));
515 m_slider
->SetPageSize(pageSize
);
517 if ( m_slider
->GetPageSize() != pageSize
)
519 wxLogWarning(wxT("Invalid page size in slider."));
523 void SliderWidgetsPage::DoSetTickFreq()
526 if ( !m_textTickFreq
->GetValue().ToLong(&freq
) )
528 wxLogWarning(wxT("Invalid slider tick frequency"));
533 m_slider
->SetTickFreq(freq
);
536 void SliderWidgetsPage::DoSetThumbLen()
539 if ( !m_textThumbLen
->GetValue().ToLong(&len
) )
541 wxLogWarning(wxT("Invalid slider thumb length"));
546 m_slider
->SetThumbLength(len
);
549 // ----------------------------------------------------------------------------
551 // ----------------------------------------------------------------------------
553 void SliderWidgetsPage::OnButtonReset(wxCommandEvent
& WXUNUSED(event
))
560 void SliderWidgetsPage::OnButtonSetLineSize(wxCommandEvent
& WXUNUSED(event
))
565 void SliderWidgetsPage::OnButtonSetPageSize(wxCommandEvent
& WXUNUSED(event
))
570 void SliderWidgetsPage::OnButtonSetTickFreq(wxCommandEvent
& WXUNUSED(event
))
575 void SliderWidgetsPage::OnButtonSetThumbLen(wxCommandEvent
& WXUNUSED(event
))
580 void SliderWidgetsPage::OnButtonSetMinAndMax(wxCommandEvent
& WXUNUSED(event
))
583 maxNew
= 0; // init to suppress compiler warning
584 if ( !m_textMin
->GetValue().ToLong(&minNew
) ||
585 !m_textMax
->GetValue().ToLong(&maxNew
) ||
588 wxLogWarning(wxT("Invalid min/max values for the slider."));
596 m_slider
->SetRange(minNew
, maxNew
);
598 if ( m_slider
->GetMin() != m_min
||
599 m_slider
->GetMax() != m_max
)
601 wxLogWarning(wxT("Invalid range in slider."));
605 void SliderWidgetsPage::OnButtonSetValue(wxCommandEvent
& WXUNUSED(event
))
608 if ( !m_textValue
->GetValue().ToLong(&val
) || !IsValidValue(val
) )
610 wxLogWarning(wxT("Invalid slider value."));
615 m_slider
->SetValue(val
);
618 void SliderWidgetsPage::OnUpdateUIValueButton(wxUpdateUIEvent
& event
)
621 event
.Enable( m_textValue
->GetValue().ToLong(&val
) && IsValidValue(val
) );
624 void SliderWidgetsPage::OnUpdateUILineSize(wxUpdateUIEvent
& event
)
627 event
.Enable( m_textLineSize
->GetValue().ToLong(&lineSize
) &&
628 (lineSize
> 0) && (lineSize
<= m_max
- m_min
) );
631 void SliderWidgetsPage::OnUpdateUIPageSize(wxUpdateUIEvent
& event
)
634 event
.Enable( m_textPageSize
->GetValue().ToLong(&pageSize
) &&
635 (pageSize
> 0) && (pageSize
<= m_max
- m_min
) );
638 void SliderWidgetsPage::OnUpdateUITickFreq(wxUpdateUIEvent
& event
)
641 event
.Enable( m_chkTicks
->GetValue() &&
642 m_textTickFreq
->GetValue().ToLong(&freq
) &&
643 (freq
> 0) && (freq
<= m_max
- m_min
) );
646 void SliderWidgetsPage::OnUpdateUIThumbLen(wxUpdateUIEvent
& event
)
649 event
.Enable( m_textThumbLen
->GetValue().ToLong(&val
));
652 void SliderWidgetsPage::OnUpdateUIMinMaxButton(wxUpdateUIEvent
& event
)
655 event
.Enable( m_textMin
->GetValue().ToLong(&mn
) &&
656 m_textMax
->GetValue().ToLong(&mx
) &&
660 void SliderWidgetsPage::OnUpdateUIResetButton(wxUpdateUIEvent
& event
)
662 event
.Enable( m_chkInverse
->GetValue() ||
663 !m_chkTicks
->GetValue() ||
664 !m_chkValueLabel
->GetValue() ||
665 !m_chkMinMaxLabels
->GetValue() ||
666 m_chkBothSides
->GetValue() ||
667 m_radioSides
->GetSelection() != SliderTicks_None
);
670 void SliderWidgetsPage::OnCheckOrRadioBox(wxCommandEvent
& WXUNUSED(event
))
675 void SliderWidgetsPage::OnUpdateUICurValueText(wxUpdateUIEvent
& event
)
677 event
.SetText( wxString::Format(wxT("%d"), m_slider
->GetValue()) );
680 void SliderWidgetsPage::OnUpdateUIRadioSides(wxUpdateUIEvent
& event
)
682 event
.Enable( m_chkValueLabel
->GetValue() || m_chkTicks
->GetValue() );
685 void SliderWidgetsPage::OnUpdateUIBothSides(wxUpdateUIEvent
& event
)
687 #if defined(__WXMSW__) || defined(__WXUNIVERSAL__)
688 event
.Enable( m_chkTicks
->GetValue() );
690 event
.Enable( false );
691 #endif // defined(__WXMSW__) || defined(__WXUNIVERSAL__)
694 void SliderWidgetsPage::OnSlider(wxScrollEvent
& event
)
696 wxASSERT_MSG( event
.GetInt() == m_slider
->GetValue(),
697 wxT("slider value should be the same") );
699 wxEventType eventType
= event
.GetEventType();
702 This array takes the EXACT order of the declarations in
704 (section "wxScrollBar and wxSlider event identifiers")
706 static const wxChar
*eventNames
[] =
708 wxT("wxEVT_SCROLL_TOP"),
709 wxT("wxEVT_SCROLL_BOTTOM"),
710 wxT("wxEVT_SCROLL_LINEUP"),
711 wxT("wxEVT_SCROLL_LINEDOWN"),
712 wxT("wxEVT_SCROLL_PAGEUP"),
713 wxT("wxEVT_SCROLL_PAGEDOWN"),
714 wxT("wxEVT_SCROLL_THUMBTRACK"),
715 wxT("wxEVT_SCROLL_THUMBRELEASE"),
716 wxT("wxEVT_SCROLL_CHANGED")
719 int index
= eventType
- wxEVT_SCROLL_TOP
;
722 If this assert is triggered, there is an unknown slider event which
723 should be added to the above eventNames array.
725 wxASSERT_MSG(index
>= 0 && (size_t)index
< WXSIZEOF(eventNames
),
726 wxT("Unknown slider event") );
729 static int s_numSliderEvents
= 0;
731 wxLogMessage(wxT("Slider event #%d: %s (pos = %d, int value = %d)"),
738 #endif // wxUSE_SLIDER