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
,
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 IMPLEMENT_WIDGETS_PAGE(SliderWidgetsPage
, _T("Slider"));
203 SliderWidgetsPage::SliderWidgetsPage(wxBookCtrl
*book
,
204 wxImageList
*imaglist
)
207 imaglist
->Add(wxBitmap(slider_xpm
));
217 m_chkBothSides
= (wxCheckBox
*)NULL
;
219 m_radioSides
= (wxRadioBox
*)NULL
;
221 m_slider
= (wxSlider
*)NULL
;
222 m_sizerSlider
= (wxSizer
*)NULL
;
224 wxSizer
*sizerTop
= new wxBoxSizer(wxHORIZONTAL
);
227 wxStaticBox
*box
= new wxStaticBox(this, wxID_ANY
, _T("&Set style"));
228 wxSizer
*sizerLeft
= new wxStaticBoxSizer(box
, wxVERTICAL
);
230 m_chkVert
= CreateCheckBoxAndAddToSizer(sizerLeft
, _T("&Vertical"));
231 m_chkInverse
= CreateCheckBoxAndAddToSizer(sizerLeft
, _T("&Inverse"));
232 m_chkTicks
= CreateCheckBoxAndAddToSizer(sizerLeft
, _T("Show &ticks"));
233 m_chkLabels
= CreateCheckBoxAndAddToSizer(sizerLeft
, _T("Show &labels"));
234 static const wxString sides
[] =
241 m_radioSides
= new wxRadioBox(this, SliderPage_RadioSides
, _T("&Ticks/Labels"),
242 wxDefaultPosition
, wxDefaultSize
,
243 WXSIZEOF(sides
), sides
,
244 1, wxRA_SPECIFY_COLS
);
245 sizerLeft
->Add(m_radioSides
, 0, wxGROW
| wxALL
, 5);
246 m_chkBothSides
= CreateCheckBoxAndAddToSizer
247 (sizerLeft
, _T("&Both sides"), SliderPage_BothSides
);
249 m_chkBothSides
->SetToolTip( _T("\"Both sides\" is only supported \nin Win95 and Universal") );
250 #endif // wxUSE_TOOLTIPS
252 sizerLeft
->Add(5, 5, 0, wxGROW
| wxALL
, 5); // spacer
254 wxButton
*btn
= new wxButton(this, SliderPage_Reset
, _T("&Reset"));
255 sizerLeft
->Add(btn
, 0, wxALIGN_CENTRE_HORIZONTAL
| wxALL
, 15);
258 wxStaticBox
*box2
= new wxStaticBox(this, wxID_ANY
, _T("&Change slider value"));
259 wxSizer
*sizerMiddle
= new wxStaticBoxSizer(box2
, wxVERTICAL
);
262 wxSizer
*sizerRow
= CreateSizerWithTextAndLabel(_T("Current value"),
263 SliderPage_CurValueText
,
265 text
->SetEditable(false);
267 sizerMiddle
->Add(sizerRow
, 0, wxALL
| wxGROW
, 5);
269 sizerRow
= CreateSizerWithTextAndButton(SliderPage_SetValue
,
271 SliderPage_ValueText
,
273 sizerMiddle
->Add(sizerRow
, 0, wxALL
| wxGROW
, 5);
275 sizerRow
= CreateSizerWithTextAndButton(SliderPage_SetMinAndMax
,
280 m_textMax
= new wxTextCtrl(this, SliderPage_MaxText
, wxEmptyString
);
281 sizerRow
->Add(m_textMax
, 1, wxLEFT
| wxALIGN_CENTRE_VERTICAL
, 5);
283 m_textMin
->SetValue( wxString::Format(_T("%d"), m_min
) );
284 m_textMax
->SetValue( wxString::Format(_T("%d"), m_max
) );
286 sizerMiddle
->Add(sizerRow
, 0, wxALL
| wxGROW
, 5);
288 sizerRow
= CreateSizerWithTextAndButton(SliderPage_SetTickFreq
,
289 _T("Tick &frequency"),
290 SliderPage_TickFreqText
,
293 m_textTickFreq
->SetValue(_T("10"));
295 sizerMiddle
->Add(sizerRow
, 0, wxALL
| wxGROW
, 5);
297 sizerRow
= CreateSizerWithTextAndButton(SliderPage_SetThumbLen
,
299 SliderPage_ThumbLenText
,
302 sizerMiddle
->Add(sizerRow
, 0, wxALL
| wxGROW
, 5);
305 wxSizer
*sizerRight
= new wxBoxSizer(wxHORIZONTAL
);
306 sizerRight
->SetMinSize(150, 40);
307 m_sizerSlider
= sizerRight
; // save it to modify it later
312 // the 3 panes panes compose the window
313 sizerTop
->Add(sizerLeft
, 0, wxGROW
| (wxALL
& ~wxLEFT
), 10);
314 sizerTop
->Add(sizerMiddle
, 0, wxGROW
| wxALL
, 10);
315 sizerTop
->Add(sizerRight
, 1, wxGROW
| (wxALL
& ~wxRIGHT
), 10);
317 // final initializations
323 // ----------------------------------------------------------------------------
325 // ----------------------------------------------------------------------------
327 void SliderWidgetsPage::Reset()
329 m_chkVert
->SetValue(false);
330 m_chkInverse
->SetValue(false);
331 m_chkTicks
->SetValue(true);
332 m_chkLabels
->SetValue(true);
333 m_chkBothSides
->SetValue(false);
335 m_radioSides
->SetSelection(StaticSides_Top
);
338 void SliderWidgetsPage::CreateSlider()
342 bool isVert
= m_chkVert
->GetValue();
344 flags
|= wxSL_VERTICAL
;
346 flags
|= wxSL_HORIZONTAL
;
348 if ( m_chkInverse
->GetValue() )
350 flags
|= wxSL_INVERSE
;
353 if ( m_chkLabels
->GetValue() )
355 flags
|= wxSL_LABELS
;
358 if ( m_chkTicks
->GetValue() )
360 flags
|= wxSL_AUTOTICKS
;
363 switch ( m_radioSides
->GetSelection() )
365 case StaticSides_Top
:
368 case StaticSides_Left
:
371 case StaticSides_Bottom
:
372 flags
|= wxSL_BOTTOM
;
374 case StaticSides_Right
:
378 wxFAIL_MSG(_T("unexpected radiobox selection"));
382 if ( m_chkBothSides
->GetValue() )
390 int valOld
= m_slider
->GetValue();
391 if ( !IsValidValue(valOld
) )
396 m_sizerSlider
->Detach( m_slider
);
398 if ( m_sizerSlider
->GetChildren().GetCount() )
400 // we have 2 spacers, remove them too
401 m_sizerSlider
->Remove( 0 );
402 m_sizerSlider
->Remove( 0 );
408 m_slider
= new wxSlider(this, SliderPage_Slider
,
410 wxDefaultPosition
, wxDefaultSize
,
415 m_sizerSlider
->Add(0, 0, 1);
416 m_sizerSlider
->Add(m_slider
, 0, wxGROW
| wxALL
, 5);
417 m_sizerSlider
->Add(0, 0, 1);
421 m_sizerSlider
->Add(m_slider
, 1, wxCENTRE
| wxALL
, 5);
424 if ( m_chkTicks
->GetValue() )
429 m_sizerSlider
->Layout();
432 void SliderWidgetsPage::DoSetTickFreq()
435 if ( !m_textTickFreq
->GetValue().ToLong(&freq
) )
437 wxLogWarning(_T("Invalid slider tick frequency"));
442 m_slider
->SetTickFreq(freq
, 0 /* unused */);
445 void SliderWidgetsPage::DoSetThumbLen()
448 if ( !m_textThumbLen
->GetValue().ToLong(&len
) )
450 wxLogWarning(_T("Invalid slider thumb length"));
455 m_slider
->SetThumbLength(len
);
458 // ----------------------------------------------------------------------------
460 // ----------------------------------------------------------------------------
462 void SliderWidgetsPage::OnButtonReset(wxCommandEvent
& WXUNUSED(event
))
469 void SliderWidgetsPage::OnButtonSetTickFreq(wxCommandEvent
& WXUNUSED(event
))
474 void SliderWidgetsPage::OnButtonSetThumbLen(wxCommandEvent
& WXUNUSED(event
))
479 void SliderWidgetsPage::OnButtonSetMinAndMax(wxCommandEvent
& WXUNUSED(event
))
482 maxNew
= 0; // init to suppress compiler warning
483 if ( !m_textMin
->GetValue().ToLong(&minNew
) ||
484 !m_textMax
->GetValue().ToLong(&maxNew
) ||
487 wxLogWarning(_T("Invalid min/max values for the slider."));
495 m_slider
->SetRange(minNew
, maxNew
);
498 void SliderWidgetsPage::OnButtonSetValue(wxCommandEvent
& WXUNUSED(event
))
501 if ( !m_textValue
->GetValue().ToLong(&val
) || !IsValidValue(val
) )
503 wxLogWarning(_T("Invalid slider value."));
508 m_slider
->SetValue(val
);
511 void SliderWidgetsPage::OnUpdateUIValueButton(wxUpdateUIEvent
& event
)
514 event
.Enable( m_textValue
->GetValue().ToLong(&val
) && IsValidValue(val
) );
517 void SliderWidgetsPage::OnUpdateUITickFreq(wxUpdateUIEvent
& event
)
520 event
.Enable( m_chkTicks
->GetValue() &&
521 m_textTickFreq
->GetValue().ToLong(&freq
) &&
522 (freq
> 0) && (freq
<= m_max
- m_min
) );
525 void SliderWidgetsPage::OnUpdateUIThumbLen(wxUpdateUIEvent
& event
)
528 event
.Enable( m_textThumbLen
->GetValue().ToLong(&val
));
531 void SliderWidgetsPage::OnUpdateUIMinMaxButton(wxUpdateUIEvent
& event
)
534 event
.Enable( m_textMin
->GetValue().ToLong(&mn
) &&
535 m_textMax
->GetValue().ToLong(&mx
) &&
539 void SliderWidgetsPage::OnUpdateUIResetButton(wxUpdateUIEvent
& event
)
541 event
.Enable( m_chkVert
->GetValue() ||
542 m_chkInverse
->GetValue() ||
543 !m_chkTicks
->GetValue() ||
544 !m_chkLabels
->GetValue() ||
545 m_chkBothSides
->GetValue() );
548 void SliderWidgetsPage::OnCheckOrRadioBox(wxCommandEvent
& WXUNUSED(event
))
553 void SliderWidgetsPage::OnUpdateUICurValueText(wxUpdateUIEvent
& event
)
555 event
.SetText( wxString::Format(_T("%d"), m_slider
->GetValue()) );
558 void SliderWidgetsPage::OnUpdateUIRadioSides(wxUpdateUIEvent
& event
)
560 event
.Enable( m_chkLabels
->GetValue() || m_chkTicks
->GetValue() );
563 void SliderWidgetsPage::OnUpdateUIBothSides(wxUpdateUIEvent
& event
)
565 #if defined(__WIN95__) || defined(__WXUNIVERSAL__)
566 event
.Enable( m_chkTicks
->GetValue() );
568 event
.Enable( false );
569 #endif // defined(__WIN95__) || defined(__WXUNIVERSAL__)
572 void SliderWidgetsPage::OnSlider(wxScrollEvent
& event
)
574 wxASSERT_MSG( event
.GetInt() == m_slider
->GetValue(),
575 wxT("slider value should be the same") );
577 wxEventType eventType
= event
.GetEventType();
580 This array takes the EXACT order of the declarations in
582 (section "wxScrollBar and wxSlider event identifiers")
584 static const wxChar
*eventNames
[] =
586 wxT("wxEVT_SCROLL_TOP"),
587 wxT("wxEVT_SCROLL_BOTTOM"),
588 wxT("wxEVT_SCROLL_LINEUP"),
589 wxT("wxEVT_SCROLL_LINEDOWN"),
590 wxT("wxEVT_SCROLL_PAGEUP"),
591 wxT("wxEVT_SCROLL_PAGEDOWN"),
592 wxT("wxEVT_SCROLL_THUMBTRACK"),
593 wxT("wxEVT_SCROLL_THUMBRELEASE"),
594 wxT("wxEVT_SCROLL_ENDSCROLL")
597 int index
= eventType
- wxEVT_SCROLL_TOP
;
600 If this assert is triggered, there is an unknown slider event which
601 should be added to the above eventNames array.
603 wxASSERT_MSG(index
>= 0 && (size_t)index
< WXSIZEOF(eventNames
),
604 wxT("Unknown slider event") );
607 static int s_numSliderEvents
= 0;
609 wxLogMessage(wxT("Slider event #%d: %s (pos = %d, int value = %ld)"),
616 #endif // wxUSE_SLIDER