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/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"
47 #include "icons/slider.xpm"
49 // ----------------------------------------------------------------------------
51 // ----------------------------------------------------------------------------
56 SliderPage_Reset
= wxID_HIGHEST
,
59 SliderPage_SetMinAndMax
,
60 SliderPage_SetTickFreq
,
61 SliderPage_SetThumbLen
,
62 SliderPage_CurValueText
,
66 SliderPage_TickFreqText
,
67 SliderPage_ThumbLenText
,
68 SliderPage_RadioSides
,
73 // sides radiobox values
82 // ----------------------------------------------------------------------------
84 // ----------------------------------------------------------------------------
86 class SliderWidgetsPage
: public WidgetsPage
89 SliderWidgetsPage(wxNotebook
*notebook
, wxImageList
*imaglist
);
90 virtual ~SliderWidgetsPage();
94 void OnButtonReset(wxCommandEvent
& event
);
95 void OnButtonClear(wxCommandEvent
& event
);
96 void OnButtonSetValue(wxCommandEvent
& event
);
97 void OnButtonSetMinAndMax(wxCommandEvent
& event
);
98 void OnButtonSetTickFreq(wxCommandEvent
& event
);
99 void OnButtonSetThumbLen(wxCommandEvent
& event
);
101 void OnCheckOrRadioBox(wxCommandEvent
& event
);
103 void OnSlider(wxScrollEvent
& event
);
105 void OnUpdateUIValueButton(wxUpdateUIEvent
& event
);
106 void OnUpdateUIMinMaxButton(wxUpdateUIEvent
& event
);
107 void OnUpdateUITickFreq(wxUpdateUIEvent
& event
);
108 void OnUpdateUIThumbLen(wxUpdateUIEvent
& event
);
109 void OnUpdateUIRadioSides(wxUpdateUIEvent
& event
);
110 void OnUpdateUIBothSides(wxUpdateUIEvent
& event
);
112 void OnUpdateUIResetButton(wxUpdateUIEvent
& event
);
114 void OnUpdateUICurValueText(wxUpdateUIEvent
& event
);
116 // reset the slider parameters
119 // (re)create the slider
122 // set the tick frequency from the text field value
123 void DoSetTickFreq();
125 // set the thumb len from the text field value
126 void DoSetThumbLen();
128 // is this slider value in range?
129 bool IsValidValue(int val
) const
130 { return (val
>= m_min
) && (val
<= m_max
); }
138 // the check/radio boxes for styles
139 wxCheckBox
*m_chkLabels
,
144 wxRadioBox
*m_radioSides
;
146 // the slider itself and the sizer it is in
148 wxSizer
*m_sizerSlider
;
150 // the text entries for set value/range
151 wxTextCtrl
*m_textValue
,
158 DECLARE_EVENT_TABLE()
159 DECLARE_WIDGETS_PAGE(SliderWidgetsPage
)
162 // ----------------------------------------------------------------------------
164 // ----------------------------------------------------------------------------
166 BEGIN_EVENT_TABLE(SliderWidgetsPage
, WidgetsPage
)
167 EVT_BUTTON(SliderPage_Reset
, SliderWidgetsPage::OnButtonReset
)
168 EVT_BUTTON(SliderPage_SetValue
, SliderWidgetsPage::OnButtonSetValue
)
169 EVT_BUTTON(SliderPage_SetMinAndMax
, SliderWidgetsPage::OnButtonSetMinAndMax
)
170 EVT_BUTTON(SliderPage_SetTickFreq
, SliderWidgetsPage::OnButtonSetTickFreq
)
171 EVT_BUTTON(SliderPage_SetThumbLen
, SliderWidgetsPage::OnButtonSetThumbLen
)
173 EVT_UPDATE_UI(SliderPage_SetValue
, SliderWidgetsPage::OnUpdateUIValueButton
)
174 EVT_UPDATE_UI(SliderPage_SetMinAndMax
, SliderWidgetsPage::OnUpdateUIMinMaxButton
)
175 EVT_UPDATE_UI(SliderPage_SetTickFreq
, SliderWidgetsPage::OnUpdateUITickFreq
)
176 EVT_UPDATE_UI(SliderPage_SetThumbLen
, SliderWidgetsPage::OnUpdateUIThumbLen
)
177 EVT_UPDATE_UI(SliderPage_TickFreqText
, SliderWidgetsPage::OnUpdateUITickFreq
)
178 EVT_UPDATE_UI(SliderPage_RadioSides
, SliderWidgetsPage::OnUpdateUIRadioSides
)
179 EVT_UPDATE_UI(SliderPage_BothSides
, SliderWidgetsPage::OnUpdateUIBothSides
)
181 EVT_UPDATE_UI(SliderPage_Reset
, SliderWidgetsPage::OnUpdateUIResetButton
)
183 EVT_UPDATE_UI(SliderPage_CurValueText
, SliderWidgetsPage::OnUpdateUICurValueText
)
185 EVT_COMMAND_SCROLL(SliderPage_Slider
, SliderWidgetsPage::OnSlider
)
187 EVT_CHECKBOX(-1, SliderWidgetsPage::OnCheckOrRadioBox
)
188 EVT_RADIOBOX(-1, SliderWidgetsPage::OnCheckOrRadioBox
)
191 // ============================================================================
193 // ============================================================================
195 IMPLEMENT_WIDGETS_PAGE(SliderWidgetsPage
, _T("Slider"));
197 SliderWidgetsPage::SliderWidgetsPage(wxNotebook
*notebook
,
198 wxImageList
*imaglist
)
199 : WidgetsPage(notebook
)
201 imaglist
->Add(wxBitmap(slider_xpm
));
210 m_chkBothSides
= (wxCheckBox
*)NULL
;
212 m_radioSides
= (wxRadioBox
*)NULL
;
214 m_slider
= (wxSlider
*)NULL
;
215 m_sizerSlider
= (wxSizer
*)NULL
;
217 wxSizer
*sizerTop
= new wxBoxSizer(wxHORIZONTAL
);
220 wxStaticBox
*box
= new wxStaticBox(this, -1, _T("&Set style"));
221 wxSizer
*sizerLeft
= new wxStaticBoxSizer(box
, wxVERTICAL
);
223 m_chkVert
= CreateCheckBoxAndAddToSizer(sizerLeft
, _T("&Vertical"));
224 m_chkTicks
= CreateCheckBoxAndAddToSizer(sizerLeft
, _T("Show &ticks"));
225 m_chkLabels
= CreateCheckBoxAndAddToSizer(sizerLeft
, _T("Show &labels"));
226 static const wxString sides
[] =
233 m_radioSides
= new wxRadioBox(this, SliderPage_RadioSides
, _T("&Ticks/Labels"),
234 wxDefaultPosition
, wxDefaultSize
,
235 WXSIZEOF(sides
), sides
,
236 1, wxRA_SPECIFY_COLS
);
237 sizerLeft
->Add(m_radioSides
, 0, wxGROW
| wxALL
, 5);
238 m_chkBothSides
= CreateCheckBoxAndAddToSizer
239 (sizerLeft
, _T("&Both sides"), SliderPage_BothSides
);
241 m_chkBothSides
->SetToolTip( _T("\"Both sides\" is only supported \nin Win95 and Universal") );
242 #endif // wxUSE_TOOLTIPS
244 sizerLeft
->Add(5, 5, 0, wxGROW
| wxALL
, 5); // spacer
246 wxButton
*btn
= new wxButton(this, SliderPage_Reset
, _T("&Reset"));
247 sizerLeft
->Add(btn
, 0, wxALIGN_CENTRE_HORIZONTAL
| wxALL
, 15);
250 wxStaticBox
*box2
= new wxStaticBox(this, -1, _T("&Change slider value"));
251 wxSizer
*sizerMiddle
= new wxStaticBoxSizer(box2
, wxVERTICAL
);
254 wxSizer
*sizerRow
= CreateSizerWithTextAndLabel(_T("Current value"),
255 SliderPage_CurValueText
,
257 text
->SetEditable(FALSE
);
259 sizerMiddle
->Add(sizerRow
, 0, wxALL
| wxGROW
, 5);
261 sizerRow
= CreateSizerWithTextAndButton(SliderPage_SetValue
,
263 SliderPage_ValueText
,
265 sizerMiddle
->Add(sizerRow
, 0, wxALL
| wxGROW
, 5);
267 sizerRow
= CreateSizerWithTextAndButton(SliderPage_SetMinAndMax
,
272 m_textMax
= new wxTextCtrl(this, SliderPage_MaxText
, _T(""));
273 sizerRow
->Add(m_textMax
, 1, wxLEFT
| wxALIGN_CENTRE_VERTICAL
, 5);
275 m_textMin
->SetValue( wxString::Format(_T("%d"), m_min
) );
276 m_textMax
->SetValue( wxString::Format(_T("%d"), m_max
) );
278 sizerMiddle
->Add(sizerRow
, 0, wxALL
| wxGROW
, 5);
280 sizerRow
= CreateSizerWithTextAndButton(SliderPage_SetTickFreq
,
281 _T("Tick &frequency"),
282 SliderPage_TickFreqText
,
285 m_textTickFreq
->SetValue(_T("10"));
287 sizerMiddle
->Add(sizerRow
, 0, wxALL
| wxGROW
, 5);
289 sizerRow
= CreateSizerWithTextAndButton(SliderPage_SetThumbLen
,
291 SliderPage_ThumbLenText
,
294 sizerMiddle
->Add(sizerRow
, 0, wxALL
| wxGROW
, 5);
297 wxSizer
*sizerRight
= new wxBoxSizer(wxHORIZONTAL
);
298 sizerRight
->SetMinSize(150, 40);
299 m_sizerSlider
= sizerRight
; // save it to modify it later
304 // the 3 panes panes compose the window
305 sizerTop
->Add(sizerLeft
, 0, wxGROW
| (wxALL
& ~wxLEFT
), 10);
306 sizerTop
->Add(sizerMiddle
, 0, wxGROW
| wxALL
, 10);
307 sizerTop
->Add(sizerRight
, 1, wxGROW
| (wxALL
& ~wxRIGHT
), 10);
309 // final initializations
316 SliderWidgetsPage::~SliderWidgetsPage()
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 lenght"));
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
& 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());