1 /////////////////////////////////////////////////////////////////////////////
2 // Program: wxWindows 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/button.h"
32 #include "wx/checkbox.h"
33 #include "wx/radiobox.h"
34 #include "wx/slider.h"
35 #include "wx/statbox.h"
36 #include "wx/textctrl.h"
40 #include "wx/tooltip.h"
46 #include "icons/slider.xpm"
48 // ----------------------------------------------------------------------------
50 // ----------------------------------------------------------------------------
55 SliderPage_Reset
= wxID_HIGHEST
,
58 SliderPage_SetMinAndMax
,
59 SliderPage_SetTickFreq
,
60 SliderPage_SetThumbLen
,
61 SliderPage_CurValueText
,
65 SliderPage_TickFreqText
,
66 SliderPage_ThumbLenText
,
67 SliderPage_RadioSides
,
72 // sides radiobox values
81 // ----------------------------------------------------------------------------
83 // ----------------------------------------------------------------------------
85 class SliderWidgetsPage
: public WidgetsPage
88 SliderWidgetsPage(wxNotebook
*notebook
, wxImageList
*imaglist
);
89 virtual ~SliderWidgetsPage();
93 void OnButtonReset(wxCommandEvent
& event
);
94 void OnButtonClear(wxCommandEvent
& event
);
95 void OnButtonSetValue(wxCommandEvent
& event
);
96 void OnButtonSetMinAndMax(wxCommandEvent
& event
);
97 void OnButtonSetTickFreq(wxCommandEvent
& event
);
98 void OnButtonSetThumbLen(wxCommandEvent
& event
);
100 void OnCheckOrRadioBox(wxCommandEvent
& event
);
102 void OnSlider(wxScrollEvent
& event
);
104 void OnUpdateUIValueButton(wxUpdateUIEvent
& event
);
105 void OnUpdateUIMinMaxButton(wxUpdateUIEvent
& event
);
106 void OnUpdateUITickFreq(wxUpdateUIEvent
& event
);
107 void OnUpdateUIThumbLen(wxUpdateUIEvent
& event
);
108 void OnUpdateUIRadioSides(wxUpdateUIEvent
& event
);
109 void OnUpdateUIBothSides(wxUpdateUIEvent
& event
);
111 void OnUpdateUIResetButton(wxUpdateUIEvent
& event
);
113 void OnUpdateUICurValueText(wxUpdateUIEvent
& event
);
115 // reset the slider parameters
118 // (re)create the slider
121 // set the tick frequency from the text field value
122 void DoSetTickFreq();
124 // set the thumb len from the text field value
125 void DoSetThumbLen();
127 // is this slider value in range?
128 bool IsValidValue(int val
) const
129 { return (val
>= m_min
) && (val
<= m_max
); }
137 // the check/radio boxes for styles
138 wxCheckBox
*m_chkLabels
,
143 wxRadioBox
*m_radioSides
;
145 // the slider itself and the sizer it is in
147 wxSizer
*m_sizerSlider
;
149 // the text entries for set value/range
150 wxTextCtrl
*m_textValue
,
157 DECLARE_EVENT_TABLE()
158 DECLARE_WIDGETS_PAGE(SliderWidgetsPage
)
161 // ----------------------------------------------------------------------------
163 // ----------------------------------------------------------------------------
165 BEGIN_EVENT_TABLE(SliderWidgetsPage
, WidgetsPage
)
166 EVT_BUTTON(SliderPage_Reset
, SliderWidgetsPage::OnButtonReset
)
167 EVT_BUTTON(SliderPage_SetValue
, SliderWidgetsPage::OnButtonSetValue
)
168 EVT_BUTTON(SliderPage_SetMinAndMax
, SliderWidgetsPage::OnButtonSetMinAndMax
)
169 EVT_BUTTON(SliderPage_SetTickFreq
, SliderWidgetsPage::OnButtonSetTickFreq
)
170 EVT_BUTTON(SliderPage_SetThumbLen
, SliderWidgetsPage::OnButtonSetThumbLen
)
172 EVT_UPDATE_UI(SliderPage_SetValue
, SliderWidgetsPage::OnUpdateUIValueButton
)
173 EVT_UPDATE_UI(SliderPage_SetMinAndMax
, SliderWidgetsPage::OnUpdateUIMinMaxButton
)
174 EVT_UPDATE_UI(SliderPage_SetTickFreq
, SliderWidgetsPage::OnUpdateUITickFreq
)
175 EVT_UPDATE_UI(SliderPage_SetThumbLen
, SliderWidgetsPage::OnUpdateUIThumbLen
)
176 EVT_UPDATE_UI(SliderPage_TickFreqText
, SliderWidgetsPage::OnUpdateUITickFreq
)
177 EVT_UPDATE_UI(SliderPage_RadioSides
, SliderWidgetsPage::OnUpdateUIRadioSides
)
178 EVT_UPDATE_UI(SliderPage_BothSides
, SliderWidgetsPage::OnUpdateUIBothSides
)
180 EVT_UPDATE_UI(SliderPage_Reset
, SliderWidgetsPage::OnUpdateUIResetButton
)
182 EVT_UPDATE_UI(SliderPage_CurValueText
, SliderWidgetsPage::OnUpdateUICurValueText
)
184 EVT_COMMAND_SCROLL(SliderPage_Slider
, SliderWidgetsPage::OnSlider
)
186 EVT_CHECKBOX(-1, SliderWidgetsPage::OnCheckOrRadioBox
)
187 EVT_RADIOBOX(-1, SliderWidgetsPage::OnCheckOrRadioBox
)
190 // ============================================================================
192 // ============================================================================
194 IMPLEMENT_WIDGETS_PAGE(SliderWidgetsPage
, _T("Slider"));
196 SliderWidgetsPage::SliderWidgetsPage(wxNotebook
*notebook
,
197 wxImageList
*imaglist
)
198 : WidgetsPage(notebook
)
200 imaglist
->Add(wxBitmap(slider_xpm
));
209 m_chkBothSides
= (wxCheckBox
*)NULL
;
211 m_radioSides
= (wxRadioBox
*)NULL
;
213 m_slider
= (wxSlider
*)NULL
;
214 m_sizerSlider
= (wxSizer
*)NULL
;
216 wxSizer
*sizerTop
= new wxBoxSizer(wxHORIZONTAL
);
219 wxStaticBox
*box
= new wxStaticBox(this, -1, _T("&Set style"));
220 wxSizer
*sizerLeft
= new wxStaticBoxSizer(box
, wxVERTICAL
);
222 m_chkVert
= CreateCheckBoxAndAddToSizer(sizerLeft
, _T("&Vertical"));
223 m_chkTicks
= CreateCheckBoxAndAddToSizer(sizerLeft
, _T("Show &ticks"));
224 m_chkLabels
= CreateCheckBoxAndAddToSizer(sizerLeft
, _T("Show &labels"));
225 static const wxString sides
[] =
232 m_radioSides
= new wxRadioBox(this, SliderPage_RadioSides
, _T("&Ticks/Labels"),
233 wxDefaultPosition
, wxDefaultSize
,
234 WXSIZEOF(sides
), sides
,
235 1, wxRA_SPECIFY_COLS
);
236 sizerLeft
->Add(m_radioSides
, 0, wxGROW
| wxALL
, 5);
237 m_chkBothSides
= CreateCheckBoxAndAddToSizer
238 (sizerLeft
, _T("&Both sides"), SliderPage_BothSides
);
240 m_chkBothSides
->SetToolTip( _T("\"Both sides\" is only supported \nin Win95 and Universal") );
241 #endif // wxUSE_TOOLTIPS
243 sizerLeft
->Add(5, 5, 0, wxGROW
| wxALL
, 5); // spacer
245 wxButton
*btn
= new wxButton(this, SliderPage_Reset
, _T("&Reset"));
246 sizerLeft
->Add(btn
, 0, wxALIGN_CENTRE_HORIZONTAL
| wxALL
, 15);
249 wxStaticBox
*box2
= new wxStaticBox(this, -1, _T("&Change slider value"));
250 wxSizer
*sizerMiddle
= new wxStaticBoxSizer(box2
, wxVERTICAL
);
253 wxSizer
*sizerRow
= CreateSizerWithTextAndLabel(_T("Current value"),
254 SliderPage_CurValueText
,
256 text
->SetEditable(FALSE
);
258 sizerMiddle
->Add(sizerRow
, 0, wxALL
| wxGROW
, 5);
260 sizerRow
= CreateSizerWithTextAndButton(SliderPage_SetValue
,
262 SliderPage_ValueText
,
264 sizerMiddle
->Add(sizerRow
, 0, wxALL
| wxGROW
, 5);
266 sizerRow
= CreateSizerWithTextAndButton(SliderPage_SetMinAndMax
,
271 m_textMax
= new wxTextCtrl(this, SliderPage_MaxText
, _T(""));
272 sizerRow
->Add(m_textMax
, 1, wxLEFT
| wxALIGN_CENTRE_VERTICAL
, 5);
274 m_textMin
->SetValue( wxString::Format(_T("%d"), m_min
) );
275 m_textMax
->SetValue( wxString::Format(_T("%d"), m_max
) );
277 sizerMiddle
->Add(sizerRow
, 0, wxALL
| wxGROW
, 5);
279 sizerRow
= CreateSizerWithTextAndButton(SliderPage_SetTickFreq
,
280 _T("Tick &frequency"),
281 SliderPage_TickFreqText
,
284 m_textTickFreq
->SetValue(_T("10"));
286 sizerMiddle
->Add(sizerRow
, 0, wxALL
| wxGROW
, 5);
288 sizerRow
= CreateSizerWithTextAndButton(SliderPage_SetThumbLen
,
290 SliderPage_ThumbLenText
,
293 sizerMiddle
->Add(sizerRow
, 0, wxALL
| wxGROW
, 5);
296 wxSizer
*sizerRight
= new wxBoxSizer(wxHORIZONTAL
);
297 sizerRight
->SetMinSize(150, 40);
298 m_sizerSlider
= sizerRight
; // save it to modify it later
303 // the 3 panes panes compose the window
304 sizerTop
->Add(sizerLeft
, 0, wxGROW
| (wxALL
& ~wxLEFT
), 10);
305 sizerTop
->Add(sizerMiddle
, 0, wxGROW
| wxALL
, 10);
306 sizerTop
->Add(sizerRight
, 1, wxGROW
| (wxALL
& ~wxRIGHT
), 10);
308 // final initializations
315 SliderWidgetsPage::~SliderWidgetsPage()
319 // ----------------------------------------------------------------------------
321 // ----------------------------------------------------------------------------
323 void SliderWidgetsPage::Reset()
325 m_chkVert
->SetValue(FALSE
);
326 m_chkTicks
->SetValue(TRUE
);
327 m_chkLabels
->SetValue(TRUE
);
328 m_chkBothSides
->SetValue(FALSE
);
330 m_radioSides
->SetSelection(StaticSides_Top
);
333 void SliderWidgetsPage::CreateSlider()
337 bool isVert
= m_chkVert
->GetValue();
339 flags
|= wxSL_VERTICAL
;
341 flags
|= wxSL_HORIZONTAL
;
343 if ( m_chkLabels
->GetValue() )
345 flags
|= wxSL_LABELS
;
348 if ( m_chkTicks
->GetValue() )
350 flags
|= wxSL_AUTOTICKS
;
353 switch ( m_radioSides
->GetSelection() )
355 case StaticSides_Top
:
358 case StaticSides_Left
:
361 case StaticSides_Bottom
:
362 flags
|= wxSL_BOTTOM
;
364 case StaticSides_Right
:
368 wxFAIL_MSG(_T("unexpected radiobox selection"));
372 if ( m_chkBothSides
->GetValue() )
380 int valOld
= m_slider
->GetValue();
381 if ( !IsValidValue(valOld
) )
386 m_sizerSlider
->Detach( m_slider
);
388 if ( m_sizerSlider
->GetChildren().GetCount() )
390 // we have 2 spacers, remove them too
391 m_sizerSlider
->Remove( 0 );
392 m_sizerSlider
->Remove( 0 );
398 m_slider
= new wxSlider(this, SliderPage_Slider
,
400 wxDefaultPosition
, wxDefaultSize
,
405 m_sizerSlider
->Add(0, 0, 1);
406 m_sizerSlider
->Add(m_slider
, 0, wxGROW
| wxALL
, 5);
407 m_sizerSlider
->Add(0, 0, 1);
411 m_sizerSlider
->Add(m_slider
, 1, wxCENTRE
| wxALL
, 5);
414 if ( m_chkTicks
->GetValue() )
419 m_sizerSlider
->Layout();
422 void SliderWidgetsPage::DoSetTickFreq()
425 if ( !m_textTickFreq
->GetValue().ToLong(&freq
) )
427 wxLogWarning(_T("Invalid slider tick frequency"));
432 m_slider
->SetTickFreq(freq
, 0 /* unused */);
435 void SliderWidgetsPage::DoSetThumbLen()
438 if ( !m_textThumbLen
->GetValue().ToLong(&len
) )
440 wxLogWarning(_T("Invalid slider thumb lenght"));
445 m_slider
->SetThumbLength(len
);
448 // ----------------------------------------------------------------------------
450 // ----------------------------------------------------------------------------
452 void SliderWidgetsPage::OnButtonReset(wxCommandEvent
& WXUNUSED(event
))
459 void SliderWidgetsPage::OnButtonSetTickFreq(wxCommandEvent
& WXUNUSED(event
))
464 void SliderWidgetsPage::OnButtonSetThumbLen(wxCommandEvent
& WXUNUSED(event
))
469 void SliderWidgetsPage::OnButtonSetMinAndMax(wxCommandEvent
& WXUNUSED(event
))
472 maxNew
= 0; // init to suppress compiler warning
473 if ( !m_textMin
->GetValue().ToLong(&minNew
) ||
474 !m_textMax
->GetValue().ToLong(&maxNew
) ||
477 wxLogWarning(_T("Invalid min/max values for the slider."));
485 m_slider
->SetRange(minNew
, maxNew
);
488 void SliderWidgetsPage::OnButtonSetValue(wxCommandEvent
& WXUNUSED(event
))
491 if ( !m_textValue
->GetValue().ToLong(&val
) || !IsValidValue(val
) )
493 wxLogWarning(_T("Invalid slider value."));
498 m_slider
->SetValue(val
);
501 void SliderWidgetsPage::OnUpdateUIValueButton(wxUpdateUIEvent
& event
)
504 event
.Enable( m_textValue
->GetValue().ToLong(&val
) && IsValidValue(val
) );
507 void SliderWidgetsPage::OnUpdateUITickFreq(wxUpdateUIEvent
& event
)
510 event
.Enable( m_chkTicks
->GetValue() &&
511 m_textTickFreq
->GetValue().ToLong(&freq
) &&
512 (freq
> 0) && (freq
<= m_max
- m_min
) );
515 void SliderWidgetsPage::OnUpdateUIThumbLen(wxUpdateUIEvent
& event
)
518 event
.Enable( m_textThumbLen
->GetValue().ToLong(&val
));
521 void SliderWidgetsPage::OnUpdateUIMinMaxButton(wxUpdateUIEvent
& event
)
524 event
.Enable( m_textMin
->GetValue().ToLong(&mn
) &&
525 m_textMax
->GetValue().ToLong(&mx
) &&
529 void SliderWidgetsPage::OnUpdateUIResetButton(wxUpdateUIEvent
& event
)
531 event
.Enable( m_chkVert
->GetValue() ||
532 !m_chkTicks
->GetValue() ||
533 !m_chkLabels
->GetValue() ||
534 m_chkBothSides
->GetValue() );
537 void SliderWidgetsPage::OnCheckOrRadioBox(wxCommandEvent
& event
)
542 void SliderWidgetsPage::OnUpdateUICurValueText(wxUpdateUIEvent
& event
)
544 event
.SetText( wxString::Format(_T("%d"), m_slider
->GetValue()) );
547 void SliderWidgetsPage::OnUpdateUIRadioSides(wxUpdateUIEvent
& event
)
549 event
.Enable( m_chkLabels
->GetValue() || m_chkTicks
->GetValue() );
552 void SliderWidgetsPage::OnUpdateUIBothSides(wxUpdateUIEvent
& event
)
554 #if defined(__WIN95__) || defined(__WXUNIVERSAL__)
555 event
.Enable( m_chkTicks
->GetValue() );
557 event
.Enable( FALSE
);
558 #endif // defined(__WIN95__) || defined(__WXUNIVERSAL__)
561 void SliderWidgetsPage::OnSlider(wxScrollEvent
& event
)
563 wxASSERT_MSG( event
.GetInt() == m_slider
->GetValue(),
564 wxT("slider value should be the same") );
566 wxEventType eventType
= event
.GetEventType();
569 This array takes the EXACT order of the declarations in
571 (section "wxScrollBar and wxSlider event identifiers")
573 static const wxChar
*eventNames
[] =
575 wxT("wxEVT_SCROLL_TOP"),
576 wxT("wxEVT_SCROLL_BOTTOM"),
577 wxT("wxEVT_SCROLL_LINEUP"),
578 wxT("wxEVT_SCROLL_LINEDOWN"),
579 wxT("wxEVT_SCROLL_PAGEUP"),
580 wxT("wxEVT_SCROLL_PAGEDOWN"),
581 wxT("wxEVT_SCROLL_THUMBTRACK"),
582 wxT("wxEVT_SCROLL_THUMBRELEASE"),
583 wxT("wxEVT_SCROLL_ENDSCROLL")
586 int index
= eventType
- wxEVT_SCROLL_TOP
;
589 If this assert is triggered, there is an unknown slider event which
590 should be added to the above eventNames array.
592 wxASSERT_MSG(index
>= 0 && (size_t)index
< WXSIZEOF(eventNames
),
593 wxT("Unknown slider event") );
596 static int s_numSliderEvents
= 0;
598 wxLogMessage(wxT("Slider event #%d: %s (pos = %d)"),
601 event
.GetPosition());