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(wxBookCtrlBase
*book
, wxImageList
*imaglist
);
93 virtual ~SliderWidgetsPage(){};
95 virtual wxControl
*GetWidget() const { return m_slider
; }
96 virtual void RecreateWidget() { CreateSlider(); }
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
);
107 void OnCheckOrRadioBox(wxCommandEvent
& event
);
109 void OnSlider(wxScrollEvent
& event
);
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
);
118 void OnUpdateUIResetButton(wxUpdateUIEvent
& event
);
120 void OnUpdateUICurValueText(wxUpdateUIEvent
& event
);
122 // reset the slider parameters
125 // (re)create the slider
128 // set the tick frequency from the text field value
129 void DoSetTickFreq();
131 // set the thumb len from the text field value
132 void DoSetThumbLen();
134 // is this slider value in range?
135 bool IsValidValue(int val
) const
136 { return (val
>= m_min
) && (val
<= m_max
); }
144 // the check/radio boxes for styles
145 wxCheckBox
*m_chkLabels
,
150 wxRadioBox
*m_radioSides
;
152 // the slider itself and the sizer it is in
154 wxSizer
*m_sizerSlider
;
156 // the text entries for set value/range
157 wxTextCtrl
*m_textValue
,
164 DECLARE_EVENT_TABLE()
165 DECLARE_WIDGETS_PAGE(SliderWidgetsPage
)
168 // ----------------------------------------------------------------------------
170 // ----------------------------------------------------------------------------
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
)
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
)
187 EVT_UPDATE_UI(SliderPage_Reset
, SliderWidgetsPage::OnUpdateUIResetButton
)
189 EVT_UPDATE_UI(SliderPage_CurValueText
, SliderWidgetsPage::OnUpdateUICurValueText
)
191 EVT_COMMAND_SCROLL(SliderPage_Slider
, SliderWidgetsPage::OnSlider
)
193 EVT_CHECKBOX(wxID_ANY
, SliderWidgetsPage::OnCheckOrRadioBox
)
194 EVT_RADIOBOX(wxID_ANY
, SliderWidgetsPage::OnCheckOrRadioBox
)
197 // ============================================================================
199 // ============================================================================
201 IMPLEMENT_WIDGETS_PAGE(SliderWidgetsPage
, _T("Slider"));
203 SliderWidgetsPage::SliderWidgetsPage(wxBookCtrlBase
*book
,
204 wxImageList
*imaglist
)
207 imaglist
->Add(wxBitmap(slider_xpm
));
216 m_chkBothSides
= (wxCheckBox
*)NULL
;
218 m_radioSides
= (wxRadioBox
*)NULL
;
220 m_slider
= (wxSlider
*)NULL
;
221 m_sizerSlider
= (wxSizer
*)NULL
;
223 wxSizer
*sizerTop
= new wxBoxSizer(wxHORIZONTAL
);
226 wxStaticBox
*box
= new wxStaticBox(this, wxID_ANY
, _T("&Set style"));
227 wxSizer
*sizerLeft
= new wxStaticBoxSizer(box
, wxVERTICAL
);
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
[] =
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
);
247 m_chkBothSides
->SetToolTip( _T("\"Both sides\" is only supported \nin Win95 and Universal") );
248 #endif // wxUSE_TOOLTIPS
250 sizerLeft
->Add(5, 5, 0, wxGROW
| wxALL
, 5); // spacer
252 wxButton
*btn
= new wxButton(this, SliderPage_Reset
, _T("&Reset"));
253 sizerLeft
->Add(btn
, 0, wxALIGN_CENTRE_HORIZONTAL
| wxALL
, 15);
256 wxStaticBox
*box2
= new wxStaticBox(this, wxID_ANY
, _T("&Change slider value"));
257 wxSizer
*sizerMiddle
= new wxStaticBoxSizer(box2
, wxVERTICAL
);
260 wxSizer
*sizerRow
= CreateSizerWithTextAndLabel(_T("Current value"),
261 SliderPage_CurValueText
,
263 text
->SetEditable(false);
265 sizerMiddle
->Add(sizerRow
, 0, wxALL
| wxGROW
, 5);
267 sizerRow
= CreateSizerWithTextAndButton(SliderPage_SetValue
,
269 SliderPage_ValueText
,
271 sizerMiddle
->Add(sizerRow
, 0, wxALL
| wxGROW
, 5);
273 sizerRow
= CreateSizerWithTextAndButton(SliderPage_SetMinAndMax
,
278 m_textMax
= new wxTextCtrl(this, SliderPage_MaxText
, wxEmptyString
);
279 sizerRow
->Add(m_textMax
, 1, wxLEFT
| wxALIGN_CENTRE_VERTICAL
, 5);
281 m_textMin
->SetValue( wxString::Format(_T("%d"), m_min
) );
282 m_textMax
->SetValue( wxString::Format(_T("%d"), m_max
) );
284 sizerMiddle
->Add(sizerRow
, 0, wxALL
| wxGROW
, 5);
286 sizerRow
= CreateSizerWithTextAndButton(SliderPage_SetTickFreq
,
287 _T("Tick &frequency"),
288 SliderPage_TickFreqText
,
291 m_textTickFreq
->SetValue(_T("10"));
293 sizerMiddle
->Add(sizerRow
, 0, wxALL
| wxGROW
, 5);
295 sizerRow
= CreateSizerWithTextAndButton(SliderPage_SetThumbLen
,
297 SliderPage_ThumbLenText
,
300 sizerMiddle
->Add(sizerRow
, 0, wxALL
| wxGROW
, 5);
303 wxSizer
*sizerRight
= new wxBoxSizer(wxHORIZONTAL
);
304 sizerRight
->SetMinSize(150, 40);
305 m_sizerSlider
= sizerRight
; // save it to modify it later
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);
315 // final initializations
321 // ----------------------------------------------------------------------------
323 // ----------------------------------------------------------------------------
325 void SliderWidgetsPage::Reset()
327 m_chkInverse
->SetValue(false);
328 m_chkTicks
->SetValue(true);
329 m_chkLabels
->SetValue(true);
330 m_chkBothSides
->SetValue(false);
332 m_radioSides
->SetSelection(SliderTicks_Top
);
335 void SliderWidgetsPage::CreateSlider()
337 int flags
= ms_defaultFlags
;
339 if ( m_chkInverse
->GetValue() )
341 flags
|= wxSL_INVERSE
;
344 if ( m_chkLabels
->GetValue() )
346 flags
|= wxSL_LABELS
;
349 if ( m_chkTicks
->GetValue() )
351 flags
|= wxSL_AUTOTICKS
;
354 switch ( m_radioSides
->GetSelection() )
356 case SliderTicks_Top
:
360 case SliderTicks_Left
:
364 case SliderTicks_Bottom
:
365 flags
|= wxSL_BOTTOM
;
368 case SliderTicks_Right
:
373 wxFAIL_MSG(_T("unexpected radiobox selection"));
377 if ( m_chkBothSides
->GetValue() )
385 int valOld
= m_slider
->GetValue();
386 if ( !IsValidValue(valOld
) )
391 m_sizerSlider
->Detach( m_slider
);
393 if ( m_sizerSlider
->GetChildren().GetCount() )
395 // we have 2 spacers, remove them too
396 m_sizerSlider
->Remove( 0 );
397 m_sizerSlider
->Remove( 0 );
403 m_slider
= new wxSlider(this, SliderPage_Slider
,
405 wxDefaultPosition
, wxDefaultSize
,
408 if ( m_slider
->HasFlag(wxSL_VERTICAL
) )
410 m_sizerSlider
->Add(0, 0, 1);
411 m_sizerSlider
->Add(m_slider
, 0, wxGROW
| wxALL
, 5);
412 m_sizerSlider
->Add(0, 0, 1);
416 m_sizerSlider
->Add(m_slider
, 1, wxCENTRE
| wxALL
, 5);
419 if ( m_chkTicks
->GetValue() )
424 m_sizerSlider
->Layout();
427 void SliderWidgetsPage::DoSetTickFreq()
430 if ( !m_textTickFreq
->GetValue().ToLong(&freq
) )
432 wxLogWarning(_T("Invalid slider tick frequency"));
437 m_slider
->SetTickFreq(freq
, 0 /* unused */);
440 void SliderWidgetsPage::DoSetThumbLen()
443 if ( !m_textThumbLen
->GetValue().ToLong(&len
) )
445 wxLogWarning(_T("Invalid slider thumb length"));
450 m_slider
->SetThumbLength(len
);
453 // ----------------------------------------------------------------------------
455 // ----------------------------------------------------------------------------
457 void SliderWidgetsPage::OnButtonReset(wxCommandEvent
& WXUNUSED(event
))
464 void SliderWidgetsPage::OnButtonSetTickFreq(wxCommandEvent
& WXUNUSED(event
))
469 void SliderWidgetsPage::OnButtonSetThumbLen(wxCommandEvent
& WXUNUSED(event
))
474 void SliderWidgetsPage::OnButtonSetMinAndMax(wxCommandEvent
& WXUNUSED(event
))
477 maxNew
= 0; // init to suppress compiler warning
478 if ( !m_textMin
->GetValue().ToLong(&minNew
) ||
479 !m_textMax
->GetValue().ToLong(&maxNew
) ||
482 wxLogWarning(_T("Invalid min/max values for the slider."));
490 m_slider
->SetRange(minNew
, maxNew
);
492 if ( m_slider
->GetMin() != m_min
||
493 m_slider
->GetMax() != m_max
)
495 wxLogWarning(_T("Invalid range in slider."));
499 void SliderWidgetsPage::OnButtonSetValue(wxCommandEvent
& WXUNUSED(event
))
502 if ( !m_textValue
->GetValue().ToLong(&val
) || !IsValidValue(val
) )
504 wxLogWarning(_T("Invalid slider value."));
509 m_slider
->SetValue(val
);
512 void SliderWidgetsPage::OnUpdateUIValueButton(wxUpdateUIEvent
& event
)
515 event
.Enable( m_textValue
->GetValue().ToLong(&val
) && IsValidValue(val
) );
518 void SliderWidgetsPage::OnUpdateUITickFreq(wxUpdateUIEvent
& event
)
521 event
.Enable( m_chkTicks
->GetValue() &&
522 m_textTickFreq
->GetValue().ToLong(&freq
) &&
523 (freq
> 0) && (freq
<= m_max
- m_min
) );
526 void SliderWidgetsPage::OnUpdateUIThumbLen(wxUpdateUIEvent
& event
)
529 event
.Enable( m_textThumbLen
->GetValue().ToLong(&val
));
532 void SliderWidgetsPage::OnUpdateUIMinMaxButton(wxUpdateUIEvent
& event
)
535 event
.Enable( m_textMin
->GetValue().ToLong(&mn
) &&
536 m_textMax
->GetValue().ToLong(&mx
) &&
540 void SliderWidgetsPage::OnUpdateUIResetButton(wxUpdateUIEvent
& event
)
542 event
.Enable( m_chkInverse
->GetValue() ||
543 !m_chkTicks
->GetValue() ||
544 !m_chkLabels
->GetValue() ||
545 m_chkBothSides
->GetValue() ||
546 m_radioSides
->GetSelection() != SliderTicks_Top
);
549 void SliderWidgetsPage::OnCheckOrRadioBox(wxCommandEvent
& WXUNUSED(event
))
554 void SliderWidgetsPage::OnUpdateUICurValueText(wxUpdateUIEvent
& event
)
556 event
.SetText( wxString::Format(_T("%d"), m_slider
->GetValue()) );
559 void SliderWidgetsPage::OnUpdateUIRadioSides(wxUpdateUIEvent
& event
)
561 event
.Enable( m_chkLabels
->GetValue() || m_chkTicks
->GetValue() );
564 void SliderWidgetsPage::OnUpdateUIBothSides(wxUpdateUIEvent
& event
)
566 #if defined(__WXMSW__) || defined(__WXUNIVERSAL__)
567 event
.Enable( m_chkTicks
->GetValue() );
569 event
.Enable( false );
570 #endif // defined(__WXMSW__) || defined(__WXUNIVERSAL__)
573 void SliderWidgetsPage::OnSlider(wxScrollEvent
& event
)
575 wxASSERT_MSG( event
.GetInt() == m_slider
->GetValue(),
576 wxT("slider value should be the same") );
578 wxEventType eventType
= event
.GetEventType();
581 This array takes the EXACT order of the declarations in
583 (section "wxScrollBar and wxSlider event identifiers")
585 static const wxChar
*eventNames
[] =
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")
598 int index
= eventType
- wxEVT_SCROLL_TOP
;
601 If this assert is triggered, there is an unknown slider event which
602 should be added to the above eventNames array.
604 wxASSERT_MSG(index
>= 0 && (size_t)index
< WXSIZEOF(eventNames
),
605 wxT("Unknown slider event") );
608 static int s_numSliderEvents
= 0;
610 wxLogMessage(wxT("Slider event #%d: %s (pos = %d, int value = %d)"),
617 #endif // wxUSE_SLIDER