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(); }
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 #if defined(__WXUNIVERSAL__)
202 #define FAMILY_CTRLS UNIVERSAL_CTRLS
204 #define FAMILY_CTRLS NATIVE_CTRLS
207 IMPLEMENT_WIDGETS_PAGE(SliderWidgetsPage
, _T("Slider"), FAMILY_CTRLS
);
209 SliderWidgetsPage::SliderWidgetsPage(WidgetsBookCtrl
*book
,
210 wxImageList
*imaglist
)
213 imaglist
->Add(wxBitmap(slider_xpm
));
222 m_chkBothSides
= (wxCheckBox
*)NULL
;
224 m_radioSides
= (wxRadioBox
*)NULL
;
226 m_slider
= (wxSlider
*)NULL
;
227 m_sizerSlider
= (wxSizer
*)NULL
;
229 wxSizer
*sizerTop
= new wxBoxSizer(wxHORIZONTAL
);
232 wxStaticBox
*box
= new wxStaticBox(this, wxID_ANY
, _T("&Set style"));
233 wxSizer
*sizerLeft
= new wxStaticBoxSizer(box
, wxVERTICAL
);
235 m_chkInverse
= CreateCheckBoxAndAddToSizer(sizerLeft
, _T("&Inverse"));
236 m_chkTicks
= CreateCheckBoxAndAddToSizer(sizerLeft
, _T("Show &ticks"));
237 m_chkLabels
= CreateCheckBoxAndAddToSizer(sizerLeft
, _T("Show &labels"));
238 static const wxString sides
[] =
245 m_radioSides
= new wxRadioBox(this, SliderPage_RadioSides
, _T("&Ticks/Labels"),
246 wxDefaultPosition
, wxDefaultSize
,
247 WXSIZEOF(sides
), sides
,
248 1, wxRA_SPECIFY_COLS
);
249 sizerLeft
->Add(m_radioSides
, 0, wxGROW
| wxALL
, 5);
250 m_chkBothSides
= CreateCheckBoxAndAddToSizer
251 (sizerLeft
, _T("&Both sides"), SliderPage_BothSides
);
253 m_chkBothSides
->SetToolTip( _T("\"Both sides\" is only supported \nin Win95 and Universal") );
254 #endif // wxUSE_TOOLTIPS
256 sizerLeft
->Add(5, 5, 0, wxGROW
| wxALL
, 5); // spacer
258 wxButton
*btn
= new wxButton(this, SliderPage_Reset
, _T("&Reset"));
259 sizerLeft
->Add(btn
, 0, wxALIGN_CENTRE_HORIZONTAL
| wxALL
, 15);
262 wxStaticBox
*box2
= new wxStaticBox(this, wxID_ANY
, _T("&Change slider value"));
263 wxSizer
*sizerMiddle
= new wxStaticBoxSizer(box2
, wxVERTICAL
);
266 wxSizer
*sizerRow
= CreateSizerWithTextAndLabel(_T("Current value"),
267 SliderPage_CurValueText
,
269 text
->SetEditable(false);
271 sizerMiddle
->Add(sizerRow
, 0, wxALL
| wxGROW
, 5);
273 sizerRow
= CreateSizerWithTextAndButton(SliderPage_SetValue
,
275 SliderPage_ValueText
,
277 sizerMiddle
->Add(sizerRow
, 0, wxALL
| wxGROW
, 5);
279 sizerRow
= CreateSizerWithTextAndButton(SliderPage_SetMinAndMax
,
284 m_textMax
= new wxTextCtrl(this, SliderPage_MaxText
, wxEmptyString
);
285 sizerRow
->Add(m_textMax
, 1, wxLEFT
| wxALIGN_CENTRE_VERTICAL
, 5);
287 m_textMin
->SetValue( wxString::Format(_T("%d"), m_min
) );
288 m_textMax
->SetValue( wxString::Format(_T("%d"), m_max
) );
290 sizerMiddle
->Add(sizerRow
, 0, wxALL
| wxGROW
, 5);
292 sizerRow
= CreateSizerWithTextAndButton(SliderPage_SetTickFreq
,
293 _T("Tick &frequency"),
294 SliderPage_TickFreqText
,
297 m_textTickFreq
->SetValue(_T("10"));
299 sizerMiddle
->Add(sizerRow
, 0, wxALL
| wxGROW
, 5);
301 sizerRow
= CreateSizerWithTextAndButton(SliderPage_SetThumbLen
,
303 SliderPage_ThumbLenText
,
306 sizerMiddle
->Add(sizerRow
, 0, wxALL
| wxGROW
, 5);
309 wxSizer
*sizerRight
= new wxBoxSizer(wxHORIZONTAL
);
310 sizerRight
->SetMinSize(150, 40);
311 m_sizerSlider
= sizerRight
; // save it to modify it later
316 // the 3 panes panes compose the window
317 sizerTop
->Add(sizerLeft
, 0, wxGROW
| (wxALL
& ~wxLEFT
), 10);
318 sizerTop
->Add(sizerMiddle
, 0, wxGROW
| wxALL
, 10);
319 sizerTop
->Add(sizerRight
, 1, wxGROW
| (wxALL
& ~wxRIGHT
), 10);
321 // final initializations
327 // ----------------------------------------------------------------------------
329 // ----------------------------------------------------------------------------
331 void SliderWidgetsPage::Reset()
333 m_chkInverse
->SetValue(false);
334 m_chkTicks
->SetValue(true);
335 m_chkLabels
->SetValue(true);
336 m_chkBothSides
->SetValue(false);
338 m_radioSides
->SetSelection(SliderTicks_Top
);
341 void SliderWidgetsPage::CreateSlider()
343 int flags
= ms_defaultFlags
;
345 if ( m_chkInverse
->GetValue() )
347 flags
|= wxSL_INVERSE
;
350 if ( m_chkLabels
->GetValue() )
352 flags
|= wxSL_LABELS
;
355 if ( m_chkTicks
->GetValue() )
357 flags
|= wxSL_AUTOTICKS
;
360 switch ( m_radioSides
->GetSelection() )
362 case SliderTicks_Top
:
366 case SliderTicks_Left
:
370 case SliderTicks_Bottom
:
371 flags
|= wxSL_BOTTOM
;
374 case SliderTicks_Right
:
379 wxFAIL_MSG(_T("unexpected radiobox selection"));
383 if ( m_chkBothSides
->GetValue() )
391 int valOld
= m_slider
->GetValue();
392 if ( !IsValidValue(valOld
) )
397 m_sizerSlider
->Detach( m_slider
);
399 if ( m_sizerSlider
->GetChildren().GetCount() )
401 // we have 2 spacers, remove them too
402 m_sizerSlider
->Remove( 0 );
403 m_sizerSlider
->Remove( 0 );
409 m_slider
= new wxSlider(this, SliderPage_Slider
,
411 wxDefaultPosition
, wxDefaultSize
,
414 if ( m_slider
->HasFlag(wxSL_VERTICAL
) )
416 m_sizerSlider
->Add(0, 0, 1);
417 m_sizerSlider
->Add(m_slider
, 0, wxGROW
| wxALL
, 5);
418 m_sizerSlider
->Add(0, 0, 1);
422 m_sizerSlider
->Add(m_slider
, 1, wxCENTRE
| wxALL
, 5);
425 if ( m_chkTicks
->GetValue() )
430 m_sizerSlider
->Layout();
433 void SliderWidgetsPage::DoSetTickFreq()
436 if ( !m_textTickFreq
->GetValue().ToLong(&freq
) )
438 wxLogWarning(_T("Invalid slider tick frequency"));
443 m_slider
->SetTickFreq(freq
, 0 /* unused */);
446 void SliderWidgetsPage::DoSetThumbLen()
449 if ( !m_textThumbLen
->GetValue().ToLong(&len
) )
451 wxLogWarning(_T("Invalid slider thumb length"));
456 m_slider
->SetThumbLength(len
);
459 // ----------------------------------------------------------------------------
461 // ----------------------------------------------------------------------------
463 void SliderWidgetsPage::OnButtonReset(wxCommandEvent
& WXUNUSED(event
))
470 void SliderWidgetsPage::OnButtonSetTickFreq(wxCommandEvent
& WXUNUSED(event
))
475 void SliderWidgetsPage::OnButtonSetThumbLen(wxCommandEvent
& WXUNUSED(event
))
480 void SliderWidgetsPage::OnButtonSetMinAndMax(wxCommandEvent
& WXUNUSED(event
))
483 maxNew
= 0; // init to suppress compiler warning
484 if ( !m_textMin
->GetValue().ToLong(&minNew
) ||
485 !m_textMax
->GetValue().ToLong(&maxNew
) ||
488 wxLogWarning(_T("Invalid min/max values for the slider."));
496 m_slider
->SetRange(minNew
, maxNew
);
498 if ( m_slider
->GetMin() != m_min
||
499 m_slider
->GetMax() != m_max
)
501 wxLogWarning(_T("Invalid range in slider."));
505 void SliderWidgetsPage::OnButtonSetValue(wxCommandEvent
& WXUNUSED(event
))
508 if ( !m_textValue
->GetValue().ToLong(&val
) || !IsValidValue(val
) )
510 wxLogWarning(_T("Invalid slider value."));
515 m_slider
->SetValue(val
);
518 void SliderWidgetsPage::OnUpdateUIValueButton(wxUpdateUIEvent
& event
)
521 event
.Enable( m_textValue
->GetValue().ToLong(&val
) && IsValidValue(val
) );
524 void SliderWidgetsPage::OnUpdateUITickFreq(wxUpdateUIEvent
& event
)
527 event
.Enable( m_chkTicks
->GetValue() &&
528 m_textTickFreq
->GetValue().ToLong(&freq
) &&
529 (freq
> 0) && (freq
<= m_max
- m_min
) );
532 void SliderWidgetsPage::OnUpdateUIThumbLen(wxUpdateUIEvent
& event
)
535 event
.Enable( m_textThumbLen
->GetValue().ToLong(&val
));
538 void SliderWidgetsPage::OnUpdateUIMinMaxButton(wxUpdateUIEvent
& event
)
541 event
.Enable( m_textMin
->GetValue().ToLong(&mn
) &&
542 m_textMax
->GetValue().ToLong(&mx
) &&
546 void SliderWidgetsPage::OnUpdateUIResetButton(wxUpdateUIEvent
& event
)
548 event
.Enable( m_chkInverse
->GetValue() ||
549 !m_chkTicks
->GetValue() ||
550 !m_chkLabels
->GetValue() ||
551 m_chkBothSides
->GetValue() ||
552 m_radioSides
->GetSelection() != SliderTicks_Top
);
555 void SliderWidgetsPage::OnCheckOrRadioBox(wxCommandEvent
& WXUNUSED(event
))
560 void SliderWidgetsPage::OnUpdateUICurValueText(wxUpdateUIEvent
& event
)
562 event
.SetText( wxString::Format(_T("%d"), m_slider
->GetValue()) );
565 void SliderWidgetsPage::OnUpdateUIRadioSides(wxUpdateUIEvent
& event
)
567 event
.Enable( m_chkLabels
->GetValue() || m_chkTicks
->GetValue() );
570 void SliderWidgetsPage::OnUpdateUIBothSides(wxUpdateUIEvent
& event
)
572 #if defined(__WXMSW__) || defined(__WXUNIVERSAL__)
573 event
.Enable( m_chkTicks
->GetValue() );
575 event
.Enable( false );
576 #endif // defined(__WXMSW__) || defined(__WXUNIVERSAL__)
579 void SliderWidgetsPage::OnSlider(wxScrollEvent
& event
)
581 wxASSERT_MSG( event
.GetInt() == m_slider
->GetValue(),
582 wxT("slider value should be the same") );
584 wxEventType eventType
= event
.GetEventType();
587 This array takes the EXACT order of the declarations in
589 (section "wxScrollBar and wxSlider event identifiers")
591 static const wxChar
*eventNames
[] =
593 wxT("wxEVT_SCROLL_TOP"),
594 wxT("wxEVT_SCROLL_BOTTOM"),
595 wxT("wxEVT_SCROLL_LINEUP"),
596 wxT("wxEVT_SCROLL_LINEDOWN"),
597 wxT("wxEVT_SCROLL_PAGEUP"),
598 wxT("wxEVT_SCROLL_PAGEDOWN"),
599 wxT("wxEVT_SCROLL_THUMBTRACK"),
600 wxT("wxEVT_SCROLL_THUMBRELEASE"),
601 wxT("wxEVT_SCROLL_CHANGED")
604 int index
= eventType
- wxEVT_SCROLL_TOP
;
607 If this assert is triggered, there is an unknown slider event which
608 should be added to the above eventNames array.
610 wxASSERT_MSG(index
>= 0 && (size_t)index
< WXSIZEOF(eventNames
),
611 wxT("Unknown slider event") );
614 static int s_numSliderEvents
= 0;
616 wxLogMessage(wxT("Slider event #%d: %s (pos = %d, int value = %d)"),
623 #endif // wxUSE_SLIDER