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(wxBookCtrl
*book
, wxImageList
*imaglist
);
93 virtual ~SliderWidgetsPage(){};
95 virtual wxControl
*GetWidget() const { return m_slider
; }
99 void OnButtonReset(wxCommandEvent
& event
);
100 void OnButtonClear(wxCommandEvent
& event
);
101 void OnButtonSetValue(wxCommandEvent
& event
);
102 void OnButtonSetMinAndMax(wxCommandEvent
& event
);
103 void OnButtonSetTickFreq(wxCommandEvent
& event
);
104 void OnButtonSetThumbLen(wxCommandEvent
& event
);
106 void OnCheckOrRadioBox(wxCommandEvent
& event
);
108 void OnSlider(wxScrollEvent
& event
);
110 void OnUpdateUIValueButton(wxUpdateUIEvent
& event
);
111 void OnUpdateUIMinMaxButton(wxUpdateUIEvent
& event
);
112 void OnUpdateUITickFreq(wxUpdateUIEvent
& event
);
113 void OnUpdateUIThumbLen(wxUpdateUIEvent
& event
);
114 void OnUpdateUIRadioSides(wxUpdateUIEvent
& event
);
115 void OnUpdateUIBothSides(wxUpdateUIEvent
& event
);
117 void OnUpdateUIResetButton(wxUpdateUIEvent
& event
);
119 void OnUpdateUICurValueText(wxUpdateUIEvent
& event
);
121 // reset the slider parameters
124 // (re)create the slider
127 // set the tick frequency from the text field value
128 void DoSetTickFreq();
130 // set the thumb len from the text field value
131 void DoSetThumbLen();
133 // is this slider value in range?
134 bool IsValidValue(int val
) const
135 { return (val
>= m_min
) && (val
<= m_max
); }
143 // the check/radio boxes for styles
144 wxCheckBox
*m_chkLabels
,
149 wxRadioBox
*m_radioSides
;
151 // the slider itself and the sizer it is in
153 wxSizer
*m_sizerSlider
;
155 // the text entries for set value/range
156 wxTextCtrl
*m_textValue
,
163 DECLARE_EVENT_TABLE()
164 DECLARE_WIDGETS_PAGE(SliderWidgetsPage
)
167 // ----------------------------------------------------------------------------
169 // ----------------------------------------------------------------------------
171 BEGIN_EVENT_TABLE(SliderWidgetsPage
, WidgetsPage
)
172 EVT_BUTTON(SliderPage_Reset
, SliderWidgetsPage::OnButtonReset
)
173 EVT_BUTTON(SliderPage_SetValue
, SliderWidgetsPage::OnButtonSetValue
)
174 EVT_BUTTON(SliderPage_SetMinAndMax
, SliderWidgetsPage::OnButtonSetMinAndMax
)
175 EVT_BUTTON(SliderPage_SetTickFreq
, SliderWidgetsPage::OnButtonSetTickFreq
)
176 EVT_BUTTON(SliderPage_SetThumbLen
, SliderWidgetsPage::OnButtonSetThumbLen
)
178 EVT_UPDATE_UI(SliderPage_SetValue
, SliderWidgetsPage::OnUpdateUIValueButton
)
179 EVT_UPDATE_UI(SliderPage_SetMinAndMax
, SliderWidgetsPage::OnUpdateUIMinMaxButton
)
180 EVT_UPDATE_UI(SliderPage_SetTickFreq
, SliderWidgetsPage::OnUpdateUITickFreq
)
181 EVT_UPDATE_UI(SliderPage_SetThumbLen
, SliderWidgetsPage::OnUpdateUIThumbLen
)
182 EVT_UPDATE_UI(SliderPage_TickFreqText
, SliderWidgetsPage::OnUpdateUITickFreq
)
183 EVT_UPDATE_UI(SliderPage_RadioSides
, SliderWidgetsPage::OnUpdateUIRadioSides
)
184 EVT_UPDATE_UI(SliderPage_BothSides
, SliderWidgetsPage::OnUpdateUIBothSides
)
186 EVT_UPDATE_UI(SliderPage_Reset
, SliderWidgetsPage::OnUpdateUIResetButton
)
188 EVT_UPDATE_UI(SliderPage_CurValueText
, SliderWidgetsPage::OnUpdateUICurValueText
)
190 EVT_COMMAND_SCROLL(SliderPage_Slider
, SliderWidgetsPage::OnSlider
)
192 EVT_CHECKBOX(wxID_ANY
, SliderWidgetsPage::OnCheckOrRadioBox
)
193 EVT_RADIOBOX(wxID_ANY
, SliderWidgetsPage::OnCheckOrRadioBox
)
196 // ============================================================================
198 // ============================================================================
200 IMPLEMENT_WIDGETS_PAGE(SliderWidgetsPage
, _T("Slider"));
202 SliderWidgetsPage::SliderWidgetsPage(wxBookCtrl
*book
,
203 wxImageList
*imaglist
)
206 imaglist
->Add(wxBitmap(slider_xpm
));
215 m_chkBothSides
= (wxCheckBox
*)NULL
;
217 m_radioSides
= (wxRadioBox
*)NULL
;
219 m_slider
= (wxSlider
*)NULL
;
220 m_sizerSlider
= (wxSizer
*)NULL
;
222 wxSizer
*sizerTop
= new wxBoxSizer(wxHORIZONTAL
);
225 wxStaticBox
*box
= new wxStaticBox(this, wxID_ANY
, _T("&Set style"));
226 wxSizer
*sizerLeft
= new wxStaticBoxSizer(box
, wxVERTICAL
);
228 m_chkVert
= CreateCheckBoxAndAddToSizer(sizerLeft
, _T("&Vertical"));
229 m_chkTicks
= CreateCheckBoxAndAddToSizer(sizerLeft
, _T("Show &ticks"));
230 m_chkLabels
= CreateCheckBoxAndAddToSizer(sizerLeft
, _T("Show &labels"));
231 static const wxString sides
[] =
238 m_radioSides
= new wxRadioBox(this, SliderPage_RadioSides
, _T("&Ticks/Labels"),
239 wxDefaultPosition
, wxDefaultSize
,
240 WXSIZEOF(sides
), sides
,
241 1, wxRA_SPECIFY_COLS
);
242 sizerLeft
->Add(m_radioSides
, 0, wxGROW
| wxALL
, 5);
243 m_chkBothSides
= CreateCheckBoxAndAddToSizer
244 (sizerLeft
, _T("&Both sides"), SliderPage_BothSides
);
246 m_chkBothSides
->SetToolTip( _T("\"Both sides\" is only supported \nin Win95 and Universal") );
247 #endif // wxUSE_TOOLTIPS
249 sizerLeft
->Add(5, 5, 0, wxGROW
| wxALL
, 5); // spacer
251 wxButton
*btn
= new wxButton(this, SliderPage_Reset
, _T("&Reset"));
252 sizerLeft
->Add(btn
, 0, wxALIGN_CENTRE_HORIZONTAL
| wxALL
, 15);
255 wxStaticBox
*box2
= new wxStaticBox(this, wxID_ANY
, _T("&Change slider value"));
256 wxSizer
*sizerMiddle
= new wxStaticBoxSizer(box2
, wxVERTICAL
);
259 wxSizer
*sizerRow
= CreateSizerWithTextAndLabel(_T("Current value"),
260 SliderPage_CurValueText
,
262 text
->SetEditable(false);
264 sizerMiddle
->Add(sizerRow
, 0, wxALL
| wxGROW
, 5);
266 sizerRow
= CreateSizerWithTextAndButton(SliderPage_SetValue
,
268 SliderPage_ValueText
,
270 sizerMiddle
->Add(sizerRow
, 0, wxALL
| wxGROW
, 5);
272 sizerRow
= CreateSizerWithTextAndButton(SliderPage_SetMinAndMax
,
277 m_textMax
= new wxTextCtrl(this, SliderPage_MaxText
, wxEmptyString
);
278 sizerRow
->Add(m_textMax
, 1, wxLEFT
| wxALIGN_CENTRE_VERTICAL
, 5);
280 m_textMin
->SetValue( wxString::Format(_T("%d"), m_min
) );
281 m_textMax
->SetValue( wxString::Format(_T("%d"), m_max
) );
283 sizerMiddle
->Add(sizerRow
, 0, wxALL
| wxGROW
, 5);
285 sizerRow
= CreateSizerWithTextAndButton(SliderPage_SetTickFreq
,
286 _T("Tick &frequency"),
287 SliderPage_TickFreqText
,
290 m_textTickFreq
->SetValue(_T("10"));
292 sizerMiddle
->Add(sizerRow
, 0, wxALL
| wxGROW
, 5);
294 sizerRow
= CreateSizerWithTextAndButton(SliderPage_SetThumbLen
,
296 SliderPage_ThumbLenText
,
299 sizerMiddle
->Add(sizerRow
, 0, wxALL
| wxGROW
, 5);
302 wxSizer
*sizerRight
= new wxBoxSizer(wxHORIZONTAL
);
303 sizerRight
->SetMinSize(150, 40);
304 m_sizerSlider
= sizerRight
; // save it to modify it later
309 // the 3 panes panes compose the window
310 sizerTop
->Add(sizerLeft
, 0, wxGROW
| (wxALL
& ~wxLEFT
), 10);
311 sizerTop
->Add(sizerMiddle
, 0, wxGROW
| wxALL
, 10);
312 sizerTop
->Add(sizerRight
, 1, wxGROW
| (wxALL
& ~wxRIGHT
), 10);
314 // final initializations
320 // ----------------------------------------------------------------------------
322 // ----------------------------------------------------------------------------
324 void SliderWidgetsPage::Reset()
326 m_chkVert
->SetValue(false);
327 m_chkTicks
->SetValue(true);
328 m_chkLabels
->SetValue(true);
329 m_chkBothSides
->SetValue(false);
331 m_radioSides
->SetSelection(StaticSides_Top
);
334 void SliderWidgetsPage::CreateSlider()
338 bool isVert
= m_chkVert
->GetValue();
340 flags
|= wxSL_VERTICAL
;
342 flags
|= wxSL_HORIZONTAL
;
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 StaticSides_Top
:
359 case StaticSides_Left
:
362 case StaticSides_Bottom
:
363 flags
|= wxSL_BOTTOM
;
365 case StaticSides_Right
:
369 wxFAIL_MSG(_T("unexpected radiobox selection"));
373 if ( m_chkBothSides
->GetValue() )
381 int valOld
= m_slider
->GetValue();
382 if ( !IsValidValue(valOld
) )
387 m_sizerSlider
->Detach( m_slider
);
389 if ( m_sizerSlider
->GetChildren().GetCount() )
391 // we have 2 spacers, remove them too
392 m_sizerSlider
->Remove( 0 );
393 m_sizerSlider
->Remove( 0 );
399 m_slider
= new wxSlider(this, SliderPage_Slider
,
401 wxDefaultPosition
, wxDefaultSize
,
406 m_sizerSlider
->Add(0, 0, 1);
407 m_sizerSlider
->Add(m_slider
, 0, wxGROW
| wxALL
, 5);
408 m_sizerSlider
->Add(0, 0, 1);
412 m_sizerSlider
->Add(m_slider
, 1, wxCENTRE
| wxALL
, 5);
415 if ( m_chkTicks
->GetValue() )
420 m_sizerSlider
->Layout();
423 void SliderWidgetsPage::DoSetTickFreq()
426 if ( !m_textTickFreq
->GetValue().ToLong(&freq
) )
428 wxLogWarning(_T("Invalid slider tick frequency"));
433 m_slider
->SetTickFreq(freq
, 0 /* unused */);
436 void SliderWidgetsPage::DoSetThumbLen()
439 if ( !m_textThumbLen
->GetValue().ToLong(&len
) )
441 wxLogWarning(_T("Invalid slider thumb length"));
446 m_slider
->SetThumbLength(len
);
449 // ----------------------------------------------------------------------------
451 // ----------------------------------------------------------------------------
453 void SliderWidgetsPage::OnButtonReset(wxCommandEvent
& WXUNUSED(event
))
460 void SliderWidgetsPage::OnButtonSetTickFreq(wxCommandEvent
& WXUNUSED(event
))
465 void SliderWidgetsPage::OnButtonSetThumbLen(wxCommandEvent
& WXUNUSED(event
))
470 void SliderWidgetsPage::OnButtonSetMinAndMax(wxCommandEvent
& WXUNUSED(event
))
473 maxNew
= 0; // init to suppress compiler warning
474 if ( !m_textMin
->GetValue().ToLong(&minNew
) ||
475 !m_textMax
->GetValue().ToLong(&maxNew
) ||
478 wxLogWarning(_T("Invalid min/max values for the slider."));
486 m_slider
->SetRange(minNew
, maxNew
);
489 void SliderWidgetsPage::OnButtonSetValue(wxCommandEvent
& WXUNUSED(event
))
492 if ( !m_textValue
->GetValue().ToLong(&val
) || !IsValidValue(val
) )
494 wxLogWarning(_T("Invalid slider value."));
499 m_slider
->SetValue(val
);
502 void SliderWidgetsPage::OnUpdateUIValueButton(wxUpdateUIEvent
& event
)
505 event
.Enable( m_textValue
->GetValue().ToLong(&val
) && IsValidValue(val
) );
508 void SliderWidgetsPage::OnUpdateUITickFreq(wxUpdateUIEvent
& event
)
511 event
.Enable( m_chkTicks
->GetValue() &&
512 m_textTickFreq
->GetValue().ToLong(&freq
) &&
513 (freq
> 0) && (freq
<= m_max
- m_min
) );
516 void SliderWidgetsPage::OnUpdateUIThumbLen(wxUpdateUIEvent
& event
)
519 event
.Enable( m_textThumbLen
->GetValue().ToLong(&val
));
522 void SliderWidgetsPage::OnUpdateUIMinMaxButton(wxUpdateUIEvent
& event
)
525 event
.Enable( m_textMin
->GetValue().ToLong(&mn
) &&
526 m_textMax
->GetValue().ToLong(&mx
) &&
530 void SliderWidgetsPage::OnUpdateUIResetButton(wxUpdateUIEvent
& event
)
532 event
.Enable( m_chkVert
->GetValue() ||
533 !m_chkTicks
->GetValue() ||
534 !m_chkLabels
->GetValue() ||
535 m_chkBothSides
->GetValue() );
538 void SliderWidgetsPage::OnCheckOrRadioBox(wxCommandEvent
& WXUNUSED(event
))
543 void SliderWidgetsPage::OnUpdateUICurValueText(wxUpdateUIEvent
& event
)
545 event
.SetText( wxString::Format(_T("%d"), m_slider
->GetValue()) );
548 void SliderWidgetsPage::OnUpdateUIRadioSides(wxUpdateUIEvent
& event
)
550 event
.Enable( m_chkLabels
->GetValue() || m_chkTicks
->GetValue() );
553 void SliderWidgetsPage::OnUpdateUIBothSides(wxUpdateUIEvent
& event
)
555 #if defined(__WIN95__) || defined(__WXUNIVERSAL__)
556 event
.Enable( m_chkTicks
->GetValue() );
558 event
.Enable( false );
559 #endif // defined(__WIN95__) || defined(__WXUNIVERSAL__)
562 void SliderWidgetsPage::OnSlider(wxScrollEvent
& event
)
564 wxASSERT_MSG( event
.GetInt() == m_slider
->GetValue(),
565 wxT("slider value should be the same") );
567 wxEventType eventType
= event
.GetEventType();
570 This array takes the EXACT order of the declarations in
572 (section "wxScrollBar and wxSlider event identifiers")
574 static const wxChar
*eventNames
[] =
576 wxT("wxEVT_SCROLL_TOP"),
577 wxT("wxEVT_SCROLL_BOTTOM"),
578 wxT("wxEVT_SCROLL_LINEUP"),
579 wxT("wxEVT_SCROLL_LINEDOWN"),
580 wxT("wxEVT_SCROLL_PAGEUP"),
581 wxT("wxEVT_SCROLL_PAGEDOWN"),
582 wxT("wxEVT_SCROLL_THUMBTRACK"),
583 wxT("wxEVT_SCROLL_THUMBRELEASE"),
584 wxT("wxEVT_SCROLL_ENDSCROLL")
587 int index
= eventType
- wxEVT_SCROLL_TOP
;
590 If this assert is triggered, there is an unknown slider event which
591 should be added to the above eventNames array.
593 wxASSERT_MSG(index
>= 0 && (size_t)index
< WXSIZEOF(eventNames
),
594 wxT("Unknown slider event") );
597 static int s_numSliderEvents
= 0;
599 wxLogMessage(wxT("Slider event #%d: %s (pos = %d)"),
602 event
.GetPosition());
605 #endif // wxUSE_SLIDER