1 /////////////////////////////////////////////////////////////////////////////
2 // Program: wxWidgets Widgets Sample
4 // Purpose: Part of the widgets sample showing wxSlider
5 // Author: Vadim Zeitlin
8 // Copyright: (c) 2001 Vadim Zeitlin
9 // License: wxWindows license
10 /////////////////////////////////////////////////////////////////////////////
12 // ============================================================================
14 // ============================================================================
16 // ----------------------------------------------------------------------------
18 // ----------------------------------------------------------------------------
20 // for compilers that support precompilation, includes "wx/wx.h".
21 #include "wx/wxprec.h"
29 // for all others, include the necessary headers
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"
43 #include "wx/tooltip.h"
50 #include "icons/slider.xpm"
52 // ----------------------------------------------------------------------------
54 // ----------------------------------------------------------------------------
59 SliderPage_Reset
= wxID_HIGHEST
,
62 SliderPage_SetMinAndMax
,
63 SliderPage_SetTickFreq
,
64 SliderPage_SetThumbLen
,
65 SliderPage_CurValueText
,
69 SliderPage_TickFreqText
,
70 SliderPage_ThumbLenText
,
71 SliderPage_RadioSides
,
76 // sides radiobox values
85 // ----------------------------------------------------------------------------
87 // ----------------------------------------------------------------------------
89 class SliderWidgetsPage
: public WidgetsPage
92 SliderWidgetsPage(WidgetsBookCtrl
*book
, wxImageList
*imaglist
);
93 virtual ~SliderWidgetsPage(){};
95 virtual wxControl
*GetWidget() const { return m_slider
; }
96 virtual void RecreateWidget() { CreateSlider(); }
98 // lazy creation of the content
99 virtual void CreateContent();
103 void OnButtonReset(wxCommandEvent
& event
);
104 void OnButtonClear(wxCommandEvent
& event
);
105 void OnButtonSetValue(wxCommandEvent
& event
);
106 void OnButtonSetMinAndMax(wxCommandEvent
& event
);
107 void OnButtonSetTickFreq(wxCommandEvent
& event
);
108 void OnButtonSetThumbLen(wxCommandEvent
& event
);
110 void OnCheckOrRadioBox(wxCommandEvent
& event
);
112 void OnSlider(wxScrollEvent
& event
);
114 void OnUpdateUIValueButton(wxUpdateUIEvent
& event
);
115 void OnUpdateUIMinMaxButton(wxUpdateUIEvent
& event
);
116 void OnUpdateUITickFreq(wxUpdateUIEvent
& event
);
117 void OnUpdateUIThumbLen(wxUpdateUIEvent
& event
);
118 void OnUpdateUIRadioSides(wxUpdateUIEvent
& event
);
119 void OnUpdateUIBothSides(wxUpdateUIEvent
& event
);
121 void OnUpdateUIResetButton(wxUpdateUIEvent
& event
);
123 void OnUpdateUICurValueText(wxUpdateUIEvent
& event
);
125 // reset the slider parameters
128 // (re)create the slider
131 // set the tick frequency from the text field value
132 void DoSetTickFreq();
134 // set the thumb len from the text field value
135 void DoSetThumbLen();
137 // is this slider value in range?
138 bool IsValidValue(int val
) const
139 { return (val
>= m_min
) && (val
<= m_max
); }
147 // the check/radio boxes for styles
148 wxCheckBox
*m_chkLabels
,
153 wxRadioBox
*m_radioSides
;
155 // the slider itself and the sizer it is in
157 wxSizer
*m_sizerSlider
;
159 // the text entries for set value/range
160 wxTextCtrl
*m_textValue
,
167 DECLARE_EVENT_TABLE()
168 DECLARE_WIDGETS_PAGE(SliderWidgetsPage
)
171 // ----------------------------------------------------------------------------
173 // ----------------------------------------------------------------------------
175 BEGIN_EVENT_TABLE(SliderWidgetsPage
, WidgetsPage
)
176 EVT_BUTTON(SliderPage_Reset
, SliderWidgetsPage::OnButtonReset
)
177 EVT_BUTTON(SliderPage_SetValue
, SliderWidgetsPage::OnButtonSetValue
)
178 EVT_BUTTON(SliderPage_SetMinAndMax
, SliderWidgetsPage::OnButtonSetMinAndMax
)
179 EVT_BUTTON(SliderPage_SetTickFreq
, SliderWidgetsPage::OnButtonSetTickFreq
)
180 EVT_BUTTON(SliderPage_SetThumbLen
, SliderWidgetsPage::OnButtonSetThumbLen
)
182 EVT_UPDATE_UI(SliderPage_SetValue
, SliderWidgetsPage::OnUpdateUIValueButton
)
183 EVT_UPDATE_UI(SliderPage_SetMinAndMax
, SliderWidgetsPage::OnUpdateUIMinMaxButton
)
184 EVT_UPDATE_UI(SliderPage_SetTickFreq
, SliderWidgetsPage::OnUpdateUITickFreq
)
185 EVT_UPDATE_UI(SliderPage_SetThumbLen
, SliderWidgetsPage::OnUpdateUIThumbLen
)
186 EVT_UPDATE_UI(SliderPage_TickFreqText
, SliderWidgetsPage::OnUpdateUITickFreq
)
187 EVT_UPDATE_UI(SliderPage_RadioSides
, SliderWidgetsPage::OnUpdateUIRadioSides
)
188 EVT_UPDATE_UI(SliderPage_BothSides
, SliderWidgetsPage::OnUpdateUIBothSides
)
190 EVT_UPDATE_UI(SliderPage_Reset
, SliderWidgetsPage::OnUpdateUIResetButton
)
192 EVT_UPDATE_UI(SliderPage_CurValueText
, SliderWidgetsPage::OnUpdateUICurValueText
)
194 EVT_COMMAND_SCROLL(SliderPage_Slider
, SliderWidgetsPage::OnSlider
)
196 EVT_CHECKBOX(wxID_ANY
, SliderWidgetsPage::OnCheckOrRadioBox
)
197 EVT_RADIOBOX(wxID_ANY
, SliderWidgetsPage::OnCheckOrRadioBox
)
200 // ============================================================================
202 // ============================================================================
204 #if defined(__WXUNIVERSAL__)
205 #define FAMILY_CTRLS UNIVERSAL_CTRLS
207 #define FAMILY_CTRLS NATIVE_CTRLS
210 IMPLEMENT_WIDGETS_PAGE(SliderWidgetsPage
, _T("Slider"), FAMILY_CTRLS
);
212 SliderWidgetsPage::SliderWidgetsPage(WidgetsBookCtrl
*book
,
213 wxImageList
*imaglist
)
214 : WidgetsPage(book
, imaglist
, slider_xpm
)
223 m_chkBothSides
= (wxCheckBox
*)NULL
;
225 m_radioSides
= (wxRadioBox
*)NULL
;
227 m_slider
= (wxSlider
*)NULL
;
228 m_sizerSlider
= (wxSizer
*)NULL
;
231 void SliderWidgetsPage::CreateContent()
233 wxSizer
*sizerTop
= new wxBoxSizer(wxHORIZONTAL
);
236 wxStaticBox
*box
= new wxStaticBox(this, wxID_ANY
, _T("&Set style"));
237 wxSizer
*sizerLeft
= new wxStaticBoxSizer(box
, wxVERTICAL
);
239 m_chkInverse
= CreateCheckBoxAndAddToSizer(sizerLeft
, _T("&Inverse"));
240 m_chkTicks
= CreateCheckBoxAndAddToSizer(sizerLeft
, _T("Show &ticks"));
241 m_chkLabels
= CreateCheckBoxAndAddToSizer(sizerLeft
, _T("Show &labels"));
242 static const wxString sides
[] =
249 m_radioSides
= new wxRadioBox(this, SliderPage_RadioSides
, _T("&Ticks/Labels"),
250 wxDefaultPosition
, wxDefaultSize
,
251 WXSIZEOF(sides
), sides
,
252 1, wxRA_SPECIFY_COLS
);
253 sizerLeft
->Add(m_radioSides
, 0, wxGROW
| wxALL
, 5);
254 m_chkBothSides
= CreateCheckBoxAndAddToSizer
255 (sizerLeft
, _T("&Both sides"), SliderPage_BothSides
);
257 m_chkBothSides
->SetToolTip( _T("\"Both sides\" is only supported \nin Win95 and Universal") );
258 #endif // wxUSE_TOOLTIPS
260 sizerLeft
->Add(5, 5, 0, wxGROW
| wxALL
, 5); // spacer
262 wxButton
*btn
= new wxButton(this, SliderPage_Reset
, _T("&Reset"));
263 sizerLeft
->Add(btn
, 0, wxALIGN_CENTRE_HORIZONTAL
| wxALL
, 15);
266 wxStaticBox
*box2
= new wxStaticBox(this, wxID_ANY
, _T("&Change slider value"));
267 wxSizer
*sizerMiddle
= new wxStaticBoxSizer(box2
, wxVERTICAL
);
270 wxSizer
*sizerRow
= CreateSizerWithTextAndLabel(_T("Current value"),
271 SliderPage_CurValueText
,
273 text
->SetEditable(false);
275 sizerMiddle
->Add(sizerRow
, 0, wxALL
| wxGROW
, 5);
277 sizerRow
= CreateSizerWithTextAndButton(SliderPage_SetValue
,
279 SliderPage_ValueText
,
281 sizerMiddle
->Add(sizerRow
, 0, wxALL
| wxGROW
, 5);
283 sizerRow
= CreateSizerWithTextAndButton(SliderPage_SetMinAndMax
,
288 m_textMax
= new wxTextCtrl(this, SliderPage_MaxText
, wxEmptyString
);
289 sizerRow
->Add(m_textMax
, 1, wxLEFT
| wxALIGN_CENTRE_VERTICAL
, 5);
291 m_textMin
->SetValue( wxString::Format(_T("%d"), m_min
) );
292 m_textMax
->SetValue( wxString::Format(_T("%d"), m_max
) );
294 sizerMiddle
->Add(sizerRow
, 0, wxALL
| wxGROW
, 5);
296 sizerRow
= CreateSizerWithTextAndButton(SliderPage_SetTickFreq
,
297 _T("Tick &frequency"),
298 SliderPage_TickFreqText
,
301 m_textTickFreq
->SetValue(_T("10"));
303 sizerMiddle
->Add(sizerRow
, 0, wxALL
| wxGROW
, 5);
305 sizerRow
= CreateSizerWithTextAndButton(SliderPage_SetThumbLen
,
307 SliderPage_ThumbLenText
,
310 sizerMiddle
->Add(sizerRow
, 0, wxALL
| wxGROW
, 5);
313 wxSizer
*sizerRight
= new wxBoxSizer(wxHORIZONTAL
);
314 sizerRight
->SetMinSize(150, 40);
315 m_sizerSlider
= sizerRight
; // save it to modify it later
320 // the 3 panes panes compose the window
321 sizerTop
->Add(sizerLeft
, 0, wxGROW
| (wxALL
& ~wxLEFT
), 10);
322 sizerTop
->Add(sizerMiddle
, 0, wxGROW
| wxALL
, 10);
323 sizerTop
->Add(sizerRight
, 1, wxGROW
| (wxALL
& ~wxRIGHT
), 10);
325 // final initializations
331 // ----------------------------------------------------------------------------
333 // ----------------------------------------------------------------------------
335 void SliderWidgetsPage::Reset()
337 m_chkInverse
->SetValue(false);
338 m_chkTicks
->SetValue(true);
339 m_chkLabels
->SetValue(true);
340 m_chkBothSides
->SetValue(false);
342 m_radioSides
->SetSelection(SliderTicks_Top
);
345 void SliderWidgetsPage::CreateSlider()
347 int flags
= ms_defaultFlags
;
349 if ( m_chkInverse
->GetValue() )
351 flags
|= wxSL_INVERSE
;
354 if ( m_chkLabels
->GetValue() )
356 flags
|= wxSL_LABELS
;
359 if ( m_chkTicks
->GetValue() )
361 flags
|= wxSL_AUTOTICKS
;
364 switch ( m_radioSides
->GetSelection() )
366 case SliderTicks_Top
:
370 case SliderTicks_Left
:
374 case SliderTicks_Bottom
:
375 flags
|= wxSL_BOTTOM
;
378 case SliderTicks_Right
:
383 wxFAIL_MSG(_T("unexpected radiobox selection"));
387 if ( m_chkBothSides
->GetValue() )
395 int valOld
= m_slider
->GetValue();
396 if ( !IsValidValue(valOld
) )
401 m_sizerSlider
->Detach( m_slider
);
403 if ( m_sizerSlider
->GetChildren().GetCount() )
405 // we have 2 spacers, remove them too
406 m_sizerSlider
->Remove( 0 );
407 m_sizerSlider
->Remove( 0 );
413 m_slider
= new wxSlider(this, SliderPage_Slider
,
415 wxDefaultPosition
, wxDefaultSize
,
418 if ( m_slider
->HasFlag(wxSL_VERTICAL
) )
420 m_sizerSlider
->Add(0, 0, 1);
421 m_sizerSlider
->Add(m_slider
, 0, wxGROW
| wxALL
, 5);
422 m_sizerSlider
->Add(0, 0, 1);
426 m_sizerSlider
->Add(m_slider
, 1, wxCENTRE
| wxALL
, 5);
429 if ( m_chkTicks
->GetValue() )
434 m_sizerSlider
->Layout();
437 void SliderWidgetsPage::DoSetTickFreq()
440 if ( !m_textTickFreq
->GetValue().ToLong(&freq
) )
442 wxLogWarning(_T("Invalid slider tick frequency"));
447 m_slider
->SetTickFreq(freq
, 0 /* unused */);
450 void SliderWidgetsPage::DoSetThumbLen()
453 if ( !m_textThumbLen
->GetValue().ToLong(&len
) )
455 wxLogWarning(_T("Invalid slider thumb length"));
460 m_slider
->SetThumbLength(len
);
463 // ----------------------------------------------------------------------------
465 // ----------------------------------------------------------------------------
467 void SliderWidgetsPage::OnButtonReset(wxCommandEvent
& WXUNUSED(event
))
474 void SliderWidgetsPage::OnButtonSetTickFreq(wxCommandEvent
& WXUNUSED(event
))
479 void SliderWidgetsPage::OnButtonSetThumbLen(wxCommandEvent
& WXUNUSED(event
))
484 void SliderWidgetsPage::OnButtonSetMinAndMax(wxCommandEvent
& WXUNUSED(event
))
487 maxNew
= 0; // init to suppress compiler warning
488 if ( !m_textMin
->GetValue().ToLong(&minNew
) ||
489 !m_textMax
->GetValue().ToLong(&maxNew
) ||
492 wxLogWarning(_T("Invalid min/max values for the slider."));
500 m_slider
->SetRange(minNew
, maxNew
);
502 if ( m_slider
->GetMin() != m_min
||
503 m_slider
->GetMax() != m_max
)
505 wxLogWarning(_T("Invalid range in slider."));
509 void SliderWidgetsPage::OnButtonSetValue(wxCommandEvent
& WXUNUSED(event
))
512 if ( !m_textValue
->GetValue().ToLong(&val
) || !IsValidValue(val
) )
514 wxLogWarning(_T("Invalid slider value."));
519 m_slider
->SetValue(val
);
522 void SliderWidgetsPage::OnUpdateUIValueButton(wxUpdateUIEvent
& event
)
525 event
.Enable( m_textValue
->GetValue().ToLong(&val
) && IsValidValue(val
) );
528 void SliderWidgetsPage::OnUpdateUITickFreq(wxUpdateUIEvent
& event
)
531 event
.Enable( m_chkTicks
->GetValue() &&
532 m_textTickFreq
->GetValue().ToLong(&freq
) &&
533 (freq
> 0) && (freq
<= m_max
- m_min
) );
536 void SliderWidgetsPage::OnUpdateUIThumbLen(wxUpdateUIEvent
& event
)
539 event
.Enable( m_textThumbLen
->GetValue().ToLong(&val
));
542 void SliderWidgetsPage::OnUpdateUIMinMaxButton(wxUpdateUIEvent
& event
)
545 event
.Enable( m_textMin
->GetValue().ToLong(&mn
) &&
546 m_textMax
->GetValue().ToLong(&mx
) &&
550 void SliderWidgetsPage::OnUpdateUIResetButton(wxUpdateUIEvent
& event
)
552 event
.Enable( m_chkInverse
->GetValue() ||
553 !m_chkTicks
->GetValue() ||
554 !m_chkLabels
->GetValue() ||
555 m_chkBothSides
->GetValue() ||
556 m_radioSides
->GetSelection() != SliderTicks_Top
);
559 void SliderWidgetsPage::OnCheckOrRadioBox(wxCommandEvent
& WXUNUSED(event
))
564 void SliderWidgetsPage::OnUpdateUICurValueText(wxUpdateUIEvent
& event
)
566 event
.SetText( wxString::Format(_T("%d"), m_slider
->GetValue()) );
569 void SliderWidgetsPage::OnUpdateUIRadioSides(wxUpdateUIEvent
& event
)
571 event
.Enable( m_chkLabels
->GetValue() || m_chkTicks
->GetValue() );
574 void SliderWidgetsPage::OnUpdateUIBothSides(wxUpdateUIEvent
& event
)
576 #if defined(__WXMSW__) || defined(__WXUNIVERSAL__)
577 event
.Enable( m_chkTicks
->GetValue() );
579 event
.Enable( false );
580 #endif // defined(__WXMSW__) || defined(__WXUNIVERSAL__)
583 void SliderWidgetsPage::OnSlider(wxScrollEvent
& event
)
585 wxASSERT_MSG( event
.GetInt() == m_slider
->GetValue(),
586 wxT("slider value should be the same") );
588 wxEventType eventType
= event
.GetEventType();
591 This array takes the EXACT order of the declarations in
593 (section "wxScrollBar and wxSlider event identifiers")
595 static const wxChar
*eventNames
[] =
597 wxT("wxEVT_SCROLL_TOP"),
598 wxT("wxEVT_SCROLL_BOTTOM"),
599 wxT("wxEVT_SCROLL_LINEUP"),
600 wxT("wxEVT_SCROLL_LINEDOWN"),
601 wxT("wxEVT_SCROLL_PAGEUP"),
602 wxT("wxEVT_SCROLL_PAGEDOWN"),
603 wxT("wxEVT_SCROLL_THUMBTRACK"),
604 wxT("wxEVT_SCROLL_THUMBRELEASE"),
605 wxT("wxEVT_SCROLL_CHANGED")
608 int index
= eventType
- wxEVT_SCROLL_TOP
;
611 If this assert is triggered, there is an unknown slider event which
612 should be added to the above eventNames array.
614 wxASSERT_MSG(index
>= 0 && (size_t)index
< WXSIZEOF(eventNames
),
615 wxT("Unknown slider event") );
618 static int s_numSliderEvents
= 0;
620 wxLogMessage(wxT("Slider event #%d: %s (pos = %d, int value = %d)"),
627 #endif // wxUSE_SLIDER