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"
27 // for all others, include the necessary headers
31 #include "wx/bitmap.h"
32 #include "wx/button.h"
33 #include "wx/checkbox.h"
34 #include "wx/radiobox.h"
35 #include "wx/slider.h"
36 #include "wx/statbox.h"
37 #include "wx/textctrl.h"
41 #include "wx/tooltip.h"
48 #include "icons/slider.xpm"
50 // ----------------------------------------------------------------------------
52 // ----------------------------------------------------------------------------
57 SliderPage_Reset
= wxID_HIGHEST
,
60 SliderPage_SetMinAndMax
,
61 SliderPage_SetTickFreq
,
62 SliderPage_SetThumbLen
,
63 SliderPage_CurValueText
,
67 SliderPage_TickFreqText
,
68 SliderPage_ThumbLenText
,
69 SliderPage_RadioSides
,
74 // sides radiobox values
83 // ----------------------------------------------------------------------------
85 // ----------------------------------------------------------------------------
87 class SliderWidgetsPage
: public WidgetsPage
90 SliderWidgetsPage(wxNotebook
*notebook
, wxImageList
*imaglist
);
91 virtual ~SliderWidgetsPage(){};
93 virtual wxControl
*GetWidget() const { return m_slider
; }
97 void OnButtonReset(wxCommandEvent
& event
);
98 void OnButtonClear(wxCommandEvent
& event
);
99 void OnButtonSetValue(wxCommandEvent
& event
);
100 void OnButtonSetMinAndMax(wxCommandEvent
& event
);
101 void OnButtonSetTickFreq(wxCommandEvent
& event
);
102 void OnButtonSetThumbLen(wxCommandEvent
& event
);
104 void OnCheckOrRadioBox(wxCommandEvent
& event
);
106 void OnSlider(wxScrollEvent
& event
);
108 void OnUpdateUIValueButton(wxUpdateUIEvent
& event
);
109 void OnUpdateUIMinMaxButton(wxUpdateUIEvent
& event
);
110 void OnUpdateUITickFreq(wxUpdateUIEvent
& event
);
111 void OnUpdateUIThumbLen(wxUpdateUIEvent
& event
);
112 void OnUpdateUIRadioSides(wxUpdateUIEvent
& event
);
113 void OnUpdateUIBothSides(wxUpdateUIEvent
& event
);
115 void OnUpdateUIResetButton(wxUpdateUIEvent
& event
);
117 void OnUpdateUICurValueText(wxUpdateUIEvent
& event
);
119 // reset the slider parameters
122 // (re)create the slider
125 // set the tick frequency from the text field value
126 void DoSetTickFreq();
128 // set the thumb len from the text field value
129 void DoSetThumbLen();
131 // is this slider value in range?
132 bool IsValidValue(int val
) const
133 { return (val
>= m_min
) && (val
<= m_max
); }
141 // the check/radio boxes for styles
142 wxCheckBox
*m_chkLabels
,
147 wxRadioBox
*m_radioSides
;
149 // the slider itself and the sizer it is in
151 wxSizer
*m_sizerSlider
;
153 // the text entries for set value/range
154 wxTextCtrl
*m_textValue
,
161 DECLARE_EVENT_TABLE()
162 DECLARE_WIDGETS_PAGE(SliderWidgetsPage
)
165 // ----------------------------------------------------------------------------
167 // ----------------------------------------------------------------------------
169 BEGIN_EVENT_TABLE(SliderWidgetsPage
, WidgetsPage
)
170 EVT_BUTTON(SliderPage_Reset
, SliderWidgetsPage::OnButtonReset
)
171 EVT_BUTTON(SliderPage_SetValue
, SliderWidgetsPage::OnButtonSetValue
)
172 EVT_BUTTON(SliderPage_SetMinAndMax
, SliderWidgetsPage::OnButtonSetMinAndMax
)
173 EVT_BUTTON(SliderPage_SetTickFreq
, SliderWidgetsPage::OnButtonSetTickFreq
)
174 EVT_BUTTON(SliderPage_SetThumbLen
, SliderWidgetsPage::OnButtonSetThumbLen
)
176 EVT_UPDATE_UI(SliderPage_SetValue
, SliderWidgetsPage::OnUpdateUIValueButton
)
177 EVT_UPDATE_UI(SliderPage_SetMinAndMax
, SliderWidgetsPage::OnUpdateUIMinMaxButton
)
178 EVT_UPDATE_UI(SliderPage_SetTickFreq
, SliderWidgetsPage::OnUpdateUITickFreq
)
179 EVT_UPDATE_UI(SliderPage_SetThumbLen
, SliderWidgetsPage::OnUpdateUIThumbLen
)
180 EVT_UPDATE_UI(SliderPage_TickFreqText
, SliderWidgetsPage::OnUpdateUITickFreq
)
181 EVT_UPDATE_UI(SliderPage_RadioSides
, SliderWidgetsPage::OnUpdateUIRadioSides
)
182 EVT_UPDATE_UI(SliderPage_BothSides
, SliderWidgetsPage::OnUpdateUIBothSides
)
184 EVT_UPDATE_UI(SliderPage_Reset
, SliderWidgetsPage::OnUpdateUIResetButton
)
186 EVT_UPDATE_UI(SliderPage_CurValueText
, SliderWidgetsPage::OnUpdateUICurValueText
)
188 EVT_COMMAND_SCROLL(SliderPage_Slider
, SliderWidgetsPage::OnSlider
)
190 EVT_CHECKBOX(wxID_ANY
, SliderWidgetsPage::OnCheckOrRadioBox
)
191 EVT_RADIOBOX(wxID_ANY
, SliderWidgetsPage::OnCheckOrRadioBox
)
194 // ============================================================================
196 // ============================================================================
198 IMPLEMENT_WIDGETS_PAGE(SliderWidgetsPage
, _T("Slider"));
200 SliderWidgetsPage::SliderWidgetsPage(wxNotebook
*notebook
,
201 wxImageList
*imaglist
)
202 : WidgetsPage(notebook
)
204 imaglist
->Add(wxBitmap(slider_xpm
));
213 m_chkBothSides
= (wxCheckBox
*)NULL
;
215 m_radioSides
= (wxRadioBox
*)NULL
;
217 m_slider
= (wxSlider
*)NULL
;
218 m_sizerSlider
= (wxSizer
*)NULL
;
220 wxSizer
*sizerTop
= new wxBoxSizer(wxHORIZONTAL
);
223 wxStaticBox
*box
= new wxStaticBox(this, wxID_ANY
, _T("&Set style"));
224 wxSizer
*sizerLeft
= new wxStaticBoxSizer(box
, wxVERTICAL
);
226 m_chkVert
= CreateCheckBoxAndAddToSizer(sizerLeft
, _T("&Vertical"));
227 m_chkTicks
= CreateCheckBoxAndAddToSizer(sizerLeft
, _T("Show &ticks"));
228 m_chkLabels
= CreateCheckBoxAndAddToSizer(sizerLeft
, _T("Show &labels"));
229 static const wxString sides
[] =
236 m_radioSides
= new wxRadioBox(this, SliderPage_RadioSides
, _T("&Ticks/Labels"),
237 wxDefaultPosition
, wxDefaultSize
,
238 WXSIZEOF(sides
), sides
,
239 1, wxRA_SPECIFY_COLS
);
240 sizerLeft
->Add(m_radioSides
, 0, wxGROW
| wxALL
, 5);
241 m_chkBothSides
= CreateCheckBoxAndAddToSizer
242 (sizerLeft
, _T("&Both sides"), SliderPage_BothSides
);
244 m_chkBothSides
->SetToolTip( _T("\"Both sides\" is only supported \nin Win95 and Universal") );
245 #endif // wxUSE_TOOLTIPS
247 sizerLeft
->Add(5, 5, 0, wxGROW
| wxALL
, 5); // spacer
249 wxButton
*btn
= new wxButton(this, SliderPage_Reset
, _T("&Reset"));
250 sizerLeft
->Add(btn
, 0, wxALIGN_CENTRE_HORIZONTAL
| wxALL
, 15);
253 wxStaticBox
*box2
= new wxStaticBox(this, wxID_ANY
, _T("&Change slider value"));
254 wxSizer
*sizerMiddle
= new wxStaticBoxSizer(box2
, wxVERTICAL
);
257 wxSizer
*sizerRow
= CreateSizerWithTextAndLabel(_T("Current value"),
258 SliderPage_CurValueText
,
260 text
->SetEditable(false);
262 sizerMiddle
->Add(sizerRow
, 0, wxALL
| wxGROW
, 5);
264 sizerRow
= CreateSizerWithTextAndButton(SliderPage_SetValue
,
266 SliderPage_ValueText
,
268 sizerMiddle
->Add(sizerRow
, 0, wxALL
| wxGROW
, 5);
270 sizerRow
= CreateSizerWithTextAndButton(SliderPage_SetMinAndMax
,
275 m_textMax
= new wxTextCtrl(this, SliderPage_MaxText
, wxEmptyString
);
276 sizerRow
->Add(m_textMax
, 1, wxLEFT
| wxALIGN_CENTRE_VERTICAL
, 5);
278 m_textMin
->SetValue( wxString::Format(_T("%d"), m_min
) );
279 m_textMax
->SetValue( wxString::Format(_T("%d"), m_max
) );
281 sizerMiddle
->Add(sizerRow
, 0, wxALL
| wxGROW
, 5);
283 sizerRow
= CreateSizerWithTextAndButton(SliderPage_SetTickFreq
,
284 _T("Tick &frequency"),
285 SliderPage_TickFreqText
,
288 m_textTickFreq
->SetValue(_T("10"));
290 sizerMiddle
->Add(sizerRow
, 0, wxALL
| wxGROW
, 5);
292 sizerRow
= CreateSizerWithTextAndButton(SliderPage_SetThumbLen
,
294 SliderPage_ThumbLenText
,
297 sizerMiddle
->Add(sizerRow
, 0, wxALL
| wxGROW
, 5);
300 wxSizer
*sizerRight
= new wxBoxSizer(wxHORIZONTAL
);
301 sizerRight
->SetMinSize(150, 40);
302 m_sizerSlider
= sizerRight
; // save it to modify it later
307 // the 3 panes panes compose the window
308 sizerTop
->Add(sizerLeft
, 0, wxGROW
| (wxALL
& ~wxLEFT
), 10);
309 sizerTop
->Add(sizerMiddle
, 0, wxGROW
| wxALL
, 10);
310 sizerTop
->Add(sizerRight
, 1, wxGROW
| (wxALL
& ~wxRIGHT
), 10);
312 // final initializations
318 // ----------------------------------------------------------------------------
320 // ----------------------------------------------------------------------------
322 void SliderWidgetsPage::Reset()
324 m_chkVert
->SetValue(false);
325 m_chkTicks
->SetValue(true);
326 m_chkLabels
->SetValue(true);
327 m_chkBothSides
->SetValue(false);
329 m_radioSides
->SetSelection(StaticSides_Top
);
332 void SliderWidgetsPage::CreateSlider()
336 bool isVert
= m_chkVert
->GetValue();
338 flags
|= wxSL_VERTICAL
;
340 flags
|= wxSL_HORIZONTAL
;
342 if ( m_chkLabels
->GetValue() )
344 flags
|= wxSL_LABELS
;
347 if ( m_chkTicks
->GetValue() )
349 flags
|= wxSL_AUTOTICKS
;
352 switch ( m_radioSides
->GetSelection() )
354 case StaticSides_Top
:
357 case StaticSides_Left
:
360 case StaticSides_Bottom
:
361 flags
|= wxSL_BOTTOM
;
363 case StaticSides_Right
:
367 wxFAIL_MSG(_T("unexpected radiobox selection"));
371 if ( m_chkBothSides
->GetValue() )
379 int valOld
= m_slider
->GetValue();
380 if ( !IsValidValue(valOld
) )
385 m_sizerSlider
->Detach( m_slider
);
387 if ( m_sizerSlider
->GetChildren().GetCount() )
389 // we have 2 spacers, remove them too
390 m_sizerSlider
->Remove( 0 );
391 m_sizerSlider
->Remove( 0 );
397 m_slider
= new wxSlider(this, SliderPage_Slider
,
399 wxDefaultPosition
, wxDefaultSize
,
404 m_sizerSlider
->Add(0, 0, 1);
405 m_sizerSlider
->Add(m_slider
, 0, wxGROW
| wxALL
, 5);
406 m_sizerSlider
->Add(0, 0, 1);
410 m_sizerSlider
->Add(m_slider
, 1, wxCENTRE
| wxALL
, 5);
413 if ( m_chkTicks
->GetValue() )
418 m_sizerSlider
->Layout();
421 void SliderWidgetsPage::DoSetTickFreq()
424 if ( !m_textTickFreq
->GetValue().ToLong(&freq
) )
426 wxLogWarning(_T("Invalid slider tick frequency"));
431 m_slider
->SetTickFreq(freq
, 0 /* unused */);
434 void SliderWidgetsPage::DoSetThumbLen()
437 if ( !m_textThumbLen
->GetValue().ToLong(&len
) )
439 wxLogWarning(_T("Invalid slider thumb length"));
444 m_slider
->SetThumbLength(len
);
447 // ----------------------------------------------------------------------------
449 // ----------------------------------------------------------------------------
451 void SliderWidgetsPage::OnButtonReset(wxCommandEvent
& WXUNUSED(event
))
458 void SliderWidgetsPage::OnButtonSetTickFreq(wxCommandEvent
& WXUNUSED(event
))
463 void SliderWidgetsPage::OnButtonSetThumbLen(wxCommandEvent
& WXUNUSED(event
))
468 void SliderWidgetsPage::OnButtonSetMinAndMax(wxCommandEvent
& WXUNUSED(event
))
471 maxNew
= 0; // init to suppress compiler warning
472 if ( !m_textMin
->GetValue().ToLong(&minNew
) ||
473 !m_textMax
->GetValue().ToLong(&maxNew
) ||
476 wxLogWarning(_T("Invalid min/max values for the slider."));
484 m_slider
->SetRange(minNew
, maxNew
);
487 void SliderWidgetsPage::OnButtonSetValue(wxCommandEvent
& WXUNUSED(event
))
490 if ( !m_textValue
->GetValue().ToLong(&val
) || !IsValidValue(val
) )
492 wxLogWarning(_T("Invalid slider value."));
497 m_slider
->SetValue(val
);
500 void SliderWidgetsPage::OnUpdateUIValueButton(wxUpdateUIEvent
& event
)
503 event
.Enable( m_textValue
->GetValue().ToLong(&val
) && IsValidValue(val
) );
506 void SliderWidgetsPage::OnUpdateUITickFreq(wxUpdateUIEvent
& event
)
509 event
.Enable( m_chkTicks
->GetValue() &&
510 m_textTickFreq
->GetValue().ToLong(&freq
) &&
511 (freq
> 0) && (freq
<= m_max
- m_min
) );
514 void SliderWidgetsPage::OnUpdateUIThumbLen(wxUpdateUIEvent
& event
)
517 event
.Enable( m_textThumbLen
->GetValue().ToLong(&val
));
520 void SliderWidgetsPage::OnUpdateUIMinMaxButton(wxUpdateUIEvent
& event
)
523 event
.Enable( m_textMin
->GetValue().ToLong(&mn
) &&
524 m_textMax
->GetValue().ToLong(&mx
) &&
528 void SliderWidgetsPage::OnUpdateUIResetButton(wxUpdateUIEvent
& event
)
530 event
.Enable( m_chkVert
->GetValue() ||
531 !m_chkTicks
->GetValue() ||
532 !m_chkLabels
->GetValue() ||
533 m_chkBothSides
->GetValue() );
536 void SliderWidgetsPage::OnCheckOrRadioBox(wxCommandEvent
& WXUNUSED(event
))
541 void SliderWidgetsPage::OnUpdateUICurValueText(wxUpdateUIEvent
& event
)
543 event
.SetText( wxString::Format(_T("%d"), m_slider
->GetValue()) );
546 void SliderWidgetsPage::OnUpdateUIRadioSides(wxUpdateUIEvent
& event
)
548 event
.Enable( m_chkLabels
->GetValue() || m_chkTicks
->GetValue() );
551 void SliderWidgetsPage::OnUpdateUIBothSides(wxUpdateUIEvent
& event
)
553 #if defined(__WIN95__) || defined(__WXUNIVERSAL__)
554 event
.Enable( m_chkTicks
->GetValue() );
556 event
.Enable( false );
557 #endif // defined(__WIN95__) || defined(__WXUNIVERSAL__)
560 void SliderWidgetsPage::OnSlider(wxScrollEvent
& event
)
562 wxASSERT_MSG( event
.GetInt() == m_slider
->GetValue(),
563 wxT("slider value should be the same") );
565 wxEventType eventType
= event
.GetEventType();
568 This array takes the EXACT order of the declarations in
570 (section "wxScrollBar and wxSlider event identifiers")
572 static const wxChar
*eventNames
[] =
574 wxT("wxEVT_SCROLL_TOP"),
575 wxT("wxEVT_SCROLL_BOTTOM"),
576 wxT("wxEVT_SCROLL_LINEUP"),
577 wxT("wxEVT_SCROLL_LINEDOWN"),
578 wxT("wxEVT_SCROLL_PAGEUP"),
579 wxT("wxEVT_SCROLL_PAGEDOWN"),
580 wxT("wxEVT_SCROLL_THUMBTRACK"),
581 wxT("wxEVT_SCROLL_THUMBRELEASE"),
582 wxT("wxEVT_SCROLL_ENDSCROLL")
585 int index
= eventType
- wxEVT_SCROLL_TOP
;
588 If this assert is triggered, there is an unknown slider event which
589 should be added to the above eventNames array.
591 wxASSERT_MSG(index
>= 0 && (size_t)index
< WXSIZEOF(eventNames
),
592 wxT("Unknown slider event") );
595 static int s_numSliderEvents
= 0;
597 wxLogMessage(wxT("Slider event #%d: %s (pos = %d)"),
600 event
.GetPosition());