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
)
211 : WidgetsPage(book
, imaglist
, slider_xpm
)
220 m_chkBothSides
= (wxCheckBox
*)NULL
;
222 m_radioSides
= (wxRadioBox
*)NULL
;
224 m_slider
= (wxSlider
*)NULL
;
225 m_sizerSlider
= (wxSizer
*)NULL
;
227 wxSizer
*sizerTop
= new wxBoxSizer(wxHORIZONTAL
);
230 wxStaticBox
*box
= new wxStaticBox(this, wxID_ANY
, _T("&Set style"));
231 wxSizer
*sizerLeft
= new wxStaticBoxSizer(box
, wxVERTICAL
);
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
[] =
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
);
251 m_chkBothSides
->SetToolTip( _T("\"Both sides\" is only supported \nin Win95 and Universal") );
252 #endif // wxUSE_TOOLTIPS
254 sizerLeft
->Add(5, 5, 0, wxGROW
| wxALL
, 5); // spacer
256 wxButton
*btn
= new wxButton(this, SliderPage_Reset
, _T("&Reset"));
257 sizerLeft
->Add(btn
, 0, wxALIGN_CENTRE_HORIZONTAL
| wxALL
, 15);
260 wxStaticBox
*box2
= new wxStaticBox(this, wxID_ANY
, _T("&Change slider value"));
261 wxSizer
*sizerMiddle
= new wxStaticBoxSizer(box2
, wxVERTICAL
);
264 wxSizer
*sizerRow
= CreateSizerWithTextAndLabel(_T("Current value"),
265 SliderPage_CurValueText
,
267 text
->SetEditable(false);
269 sizerMiddle
->Add(sizerRow
, 0, wxALL
| wxGROW
, 5);
271 sizerRow
= CreateSizerWithTextAndButton(SliderPage_SetValue
,
273 SliderPage_ValueText
,
275 sizerMiddle
->Add(sizerRow
, 0, wxALL
| wxGROW
, 5);
277 sizerRow
= CreateSizerWithTextAndButton(SliderPage_SetMinAndMax
,
282 m_textMax
= new wxTextCtrl(this, SliderPage_MaxText
, wxEmptyString
);
283 sizerRow
->Add(m_textMax
, 1, wxLEFT
| wxALIGN_CENTRE_VERTICAL
, 5);
285 m_textMin
->SetValue( wxString::Format(_T("%d"), m_min
) );
286 m_textMax
->SetValue( wxString::Format(_T("%d"), m_max
) );
288 sizerMiddle
->Add(sizerRow
, 0, wxALL
| wxGROW
, 5);
290 sizerRow
= CreateSizerWithTextAndButton(SliderPage_SetTickFreq
,
291 _T("Tick &frequency"),
292 SliderPage_TickFreqText
,
295 m_textTickFreq
->SetValue(_T("10"));
297 sizerMiddle
->Add(sizerRow
, 0, wxALL
| wxGROW
, 5);
299 sizerRow
= CreateSizerWithTextAndButton(SliderPage_SetThumbLen
,
301 SliderPage_ThumbLenText
,
304 sizerMiddle
->Add(sizerRow
, 0, wxALL
| wxGROW
, 5);
307 wxSizer
*sizerRight
= new wxBoxSizer(wxHORIZONTAL
);
308 sizerRight
->SetMinSize(150, 40);
309 m_sizerSlider
= sizerRight
; // save it to modify it later
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);
319 // final initializations
325 // ----------------------------------------------------------------------------
327 // ----------------------------------------------------------------------------
329 void SliderWidgetsPage::Reset()
331 m_chkInverse
->SetValue(false);
332 m_chkTicks
->SetValue(true);
333 m_chkLabels
->SetValue(true);
334 m_chkBothSides
->SetValue(false);
336 m_radioSides
->SetSelection(SliderTicks_Top
);
339 void SliderWidgetsPage::CreateSlider()
341 int flags
= ms_defaultFlags
;
343 if ( m_chkInverse
->GetValue() )
345 flags
|= wxSL_INVERSE
;
348 if ( m_chkLabels
->GetValue() )
350 flags
|= wxSL_LABELS
;
353 if ( m_chkTicks
->GetValue() )
355 flags
|= wxSL_AUTOTICKS
;
358 switch ( m_radioSides
->GetSelection() )
360 case SliderTicks_Top
:
364 case SliderTicks_Left
:
368 case SliderTicks_Bottom
:
369 flags
|= wxSL_BOTTOM
;
372 case SliderTicks_Right
:
377 wxFAIL_MSG(_T("unexpected radiobox selection"));
381 if ( m_chkBothSides
->GetValue() )
389 int valOld
= m_slider
->GetValue();
390 if ( !IsValidValue(valOld
) )
395 m_sizerSlider
->Detach( m_slider
);
397 if ( m_sizerSlider
->GetChildren().GetCount() )
399 // we have 2 spacers, remove them too
400 m_sizerSlider
->Remove( 0 );
401 m_sizerSlider
->Remove( 0 );
407 m_slider
= new wxSlider(this, SliderPage_Slider
,
409 wxDefaultPosition
, wxDefaultSize
,
412 if ( m_slider
->HasFlag(wxSL_VERTICAL
) )
414 m_sizerSlider
->Add(0, 0, 1);
415 m_sizerSlider
->Add(m_slider
, 0, wxGROW
| wxALL
, 5);
416 m_sizerSlider
->Add(0, 0, 1);
420 m_sizerSlider
->Add(m_slider
, 1, wxCENTRE
| wxALL
, 5);
423 if ( m_chkTicks
->GetValue() )
428 m_sizerSlider
->Layout();
431 void SliderWidgetsPage::DoSetTickFreq()
434 if ( !m_textTickFreq
->GetValue().ToLong(&freq
) )
436 wxLogWarning(_T("Invalid slider tick frequency"));
441 m_slider
->SetTickFreq(freq
, 0 /* unused */);
444 void SliderWidgetsPage::DoSetThumbLen()
447 if ( !m_textThumbLen
->GetValue().ToLong(&len
) )
449 wxLogWarning(_T("Invalid slider thumb length"));
454 m_slider
->SetThumbLength(len
);
457 // ----------------------------------------------------------------------------
459 // ----------------------------------------------------------------------------
461 void SliderWidgetsPage::OnButtonReset(wxCommandEvent
& WXUNUSED(event
))
468 void SliderWidgetsPage::OnButtonSetTickFreq(wxCommandEvent
& WXUNUSED(event
))
473 void SliderWidgetsPage::OnButtonSetThumbLen(wxCommandEvent
& WXUNUSED(event
))
478 void SliderWidgetsPage::OnButtonSetMinAndMax(wxCommandEvent
& WXUNUSED(event
))
481 maxNew
= 0; // init to suppress compiler warning
482 if ( !m_textMin
->GetValue().ToLong(&minNew
) ||
483 !m_textMax
->GetValue().ToLong(&maxNew
) ||
486 wxLogWarning(_T("Invalid min/max values for the slider."));
494 m_slider
->SetRange(minNew
, maxNew
);
496 if ( m_slider
->GetMin() != m_min
||
497 m_slider
->GetMax() != m_max
)
499 wxLogWarning(_T("Invalid range in slider."));
503 void SliderWidgetsPage::OnButtonSetValue(wxCommandEvent
& WXUNUSED(event
))
506 if ( !m_textValue
->GetValue().ToLong(&val
) || !IsValidValue(val
) )
508 wxLogWarning(_T("Invalid slider value."));
513 m_slider
->SetValue(val
);
516 void SliderWidgetsPage::OnUpdateUIValueButton(wxUpdateUIEvent
& event
)
519 event
.Enable( m_textValue
->GetValue().ToLong(&val
) && IsValidValue(val
) );
522 void SliderWidgetsPage::OnUpdateUITickFreq(wxUpdateUIEvent
& event
)
525 event
.Enable( m_chkTicks
->GetValue() &&
526 m_textTickFreq
->GetValue().ToLong(&freq
) &&
527 (freq
> 0) && (freq
<= m_max
- m_min
) );
530 void SliderWidgetsPage::OnUpdateUIThumbLen(wxUpdateUIEvent
& event
)
533 event
.Enable( m_textThumbLen
->GetValue().ToLong(&val
));
536 void SliderWidgetsPage::OnUpdateUIMinMaxButton(wxUpdateUIEvent
& event
)
539 event
.Enable( m_textMin
->GetValue().ToLong(&mn
) &&
540 m_textMax
->GetValue().ToLong(&mx
) &&
544 void SliderWidgetsPage::OnUpdateUIResetButton(wxUpdateUIEvent
& event
)
546 event
.Enable( m_chkInverse
->GetValue() ||
547 !m_chkTicks
->GetValue() ||
548 !m_chkLabels
->GetValue() ||
549 m_chkBothSides
->GetValue() ||
550 m_radioSides
->GetSelection() != SliderTicks_Top
);
553 void SliderWidgetsPage::OnCheckOrRadioBox(wxCommandEvent
& WXUNUSED(event
))
558 void SliderWidgetsPage::OnUpdateUICurValueText(wxUpdateUIEvent
& event
)
560 event
.SetText( wxString::Format(_T("%d"), m_slider
->GetValue()) );
563 void SliderWidgetsPage::OnUpdateUIRadioSides(wxUpdateUIEvent
& event
)
565 event
.Enable( m_chkLabels
->GetValue() || m_chkTicks
->GetValue() );
568 void SliderWidgetsPage::OnUpdateUIBothSides(wxUpdateUIEvent
& event
)
570 #if defined(__WXMSW__) || defined(__WXUNIVERSAL__)
571 event
.Enable( m_chkTicks
->GetValue() );
573 event
.Enable( false );
574 #endif // defined(__WXMSW__) || defined(__WXUNIVERSAL__)
577 void SliderWidgetsPage::OnSlider(wxScrollEvent
& event
)
579 wxASSERT_MSG( event
.GetInt() == m_slider
->GetValue(),
580 wxT("slider value should be the same") );
582 wxEventType eventType
= event
.GetEventType();
585 This array takes the EXACT order of the declarations in
587 (section "wxScrollBar and wxSlider event identifiers")
589 static const wxChar
*eventNames
[] =
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")
602 int index
= eventType
- wxEVT_SCROLL_TOP
;
605 If this assert is triggered, there is an unknown slider event which
606 should be added to the above eventNames array.
608 wxASSERT_MSG(index
>= 0 && (size_t)index
< WXSIZEOF(eventNames
),
609 wxT("Unknown slider event") );
612 static int s_numSliderEvents
= 0;
614 wxLogMessage(wxT("Slider event #%d: %s (pos = %d, int value = %d)"),
621 #endif // wxUSE_SLIDER