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"
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();
95 void OnButtonReset(wxCommandEvent
& event
);
96 void OnButtonClear(wxCommandEvent
& event
);
97 void OnButtonSetValue(wxCommandEvent
& event
);
98 void OnButtonSetMinAndMax(wxCommandEvent
& event
);
99 void OnButtonSetTickFreq(wxCommandEvent
& event
);
100 void OnButtonSetThumbLen(wxCommandEvent
& event
);
102 void OnCheckOrRadioBox(wxCommandEvent
& event
);
104 void OnSlider(wxScrollEvent
& event
);
106 void OnUpdateUIValueButton(wxUpdateUIEvent
& event
);
107 void OnUpdateUIMinMaxButton(wxUpdateUIEvent
& event
);
108 void OnUpdateUITickFreq(wxUpdateUIEvent
& event
);
109 void OnUpdateUIThumbLen(wxUpdateUIEvent
& event
);
110 void OnUpdateUIRadioSides(wxUpdateUIEvent
& event
);
111 void OnUpdateUIBothSides(wxUpdateUIEvent
& event
);
113 void OnUpdateUIResetButton(wxUpdateUIEvent
& event
);
115 void OnUpdateUICurValueText(wxUpdateUIEvent
& event
);
117 // reset the slider parameters
120 // (re)create the slider
123 // set the tick frequency from the text field value
124 void DoSetTickFreq();
126 // set the thumb len from the text field value
127 void DoSetThumbLen();
129 // is this slider value in range?
130 bool IsValidValue(int val
) const
131 { return (val
>= m_min
) && (val
<= m_max
); }
139 // the check/radio boxes for styles
140 wxCheckBox
*m_chkLabels
,
145 wxRadioBox
*m_radioSides
;
147 // the slider itself and the sizer it is in
149 wxSizer
*m_sizerSlider
;
151 // the text entries for set value/range
152 wxTextCtrl
*m_textValue
,
159 DECLARE_EVENT_TABLE()
160 DECLARE_WIDGETS_PAGE(SliderWidgetsPage
)
163 // ----------------------------------------------------------------------------
165 // ----------------------------------------------------------------------------
167 BEGIN_EVENT_TABLE(SliderWidgetsPage
, WidgetsPage
)
168 EVT_BUTTON(SliderPage_Reset
, SliderWidgetsPage::OnButtonReset
)
169 EVT_BUTTON(SliderPage_SetValue
, SliderWidgetsPage::OnButtonSetValue
)
170 EVT_BUTTON(SliderPage_SetMinAndMax
, SliderWidgetsPage::OnButtonSetMinAndMax
)
171 EVT_BUTTON(SliderPage_SetTickFreq
, SliderWidgetsPage::OnButtonSetTickFreq
)
172 EVT_BUTTON(SliderPage_SetThumbLen
, SliderWidgetsPage::OnButtonSetThumbLen
)
174 EVT_UPDATE_UI(SliderPage_SetValue
, SliderWidgetsPage::OnUpdateUIValueButton
)
175 EVT_UPDATE_UI(SliderPage_SetMinAndMax
, SliderWidgetsPage::OnUpdateUIMinMaxButton
)
176 EVT_UPDATE_UI(SliderPage_SetTickFreq
, SliderWidgetsPage::OnUpdateUITickFreq
)
177 EVT_UPDATE_UI(SliderPage_SetThumbLen
, SliderWidgetsPage::OnUpdateUIThumbLen
)
178 EVT_UPDATE_UI(SliderPage_TickFreqText
, SliderWidgetsPage::OnUpdateUITickFreq
)
179 EVT_UPDATE_UI(SliderPage_RadioSides
, SliderWidgetsPage::OnUpdateUIRadioSides
)
180 EVT_UPDATE_UI(SliderPage_BothSides
, SliderWidgetsPage::OnUpdateUIBothSides
)
182 EVT_UPDATE_UI(SliderPage_Reset
, SliderWidgetsPage::OnUpdateUIResetButton
)
184 EVT_UPDATE_UI(SliderPage_CurValueText
, SliderWidgetsPage::OnUpdateUICurValueText
)
186 EVT_COMMAND_SCROLL(SliderPage_Slider
, SliderWidgetsPage::OnSlider
)
188 EVT_CHECKBOX(-1, SliderWidgetsPage::OnCheckOrRadioBox
)
189 EVT_RADIOBOX(-1, SliderWidgetsPage::OnCheckOrRadioBox
)
192 // ============================================================================
194 // ============================================================================
196 IMPLEMENT_WIDGETS_PAGE(SliderWidgetsPage
, _T("Slider"));
198 SliderWidgetsPage::SliderWidgetsPage(wxNotebook
*notebook
,
199 wxImageList
*imaglist
)
200 : WidgetsPage(notebook
)
202 imaglist
->Add(wxBitmap(slider_xpm
));
211 m_chkBothSides
= (wxCheckBox
*)NULL
;
213 m_radioSides
= (wxRadioBox
*)NULL
;
215 m_slider
= (wxSlider
*)NULL
;
216 m_sizerSlider
= (wxSizer
*)NULL
;
218 wxSizer
*sizerTop
= new wxBoxSizer(wxHORIZONTAL
);
221 wxStaticBox
*box
= new wxStaticBox(this, -1, _T("&Set style"));
222 wxSizer
*sizerLeft
= new wxStaticBoxSizer(box
, wxVERTICAL
);
224 m_chkVert
= CreateCheckBoxAndAddToSizer(sizerLeft
, _T("&Vertical"));
225 m_chkTicks
= CreateCheckBoxAndAddToSizer(sizerLeft
, _T("Show &ticks"));
226 m_chkLabels
= CreateCheckBoxAndAddToSizer(sizerLeft
, _T("Show &labels"));
227 static const wxString sides
[] =
234 m_radioSides
= new wxRadioBox(this, SliderPage_RadioSides
, _T("&Ticks/Labels"),
235 wxDefaultPosition
, wxDefaultSize
,
236 WXSIZEOF(sides
), sides
,
237 1, wxRA_SPECIFY_COLS
);
238 sizerLeft
->Add(m_radioSides
, 0, wxGROW
| wxALL
, 5);
239 m_chkBothSides
= CreateCheckBoxAndAddToSizer
240 (sizerLeft
, _T("&Both sides"), SliderPage_BothSides
);
242 m_chkBothSides
->SetToolTip( _T("\"Both sides\" is only supported \nin Win95 and Universal") );
243 #endif // wxUSE_TOOLTIPS
245 sizerLeft
->Add(5, 5, 0, wxGROW
| wxALL
, 5); // spacer
247 wxButton
*btn
= new wxButton(this, SliderPage_Reset
, _T("&Reset"));
248 sizerLeft
->Add(btn
, 0, wxALIGN_CENTRE_HORIZONTAL
| wxALL
, 15);
251 wxStaticBox
*box2
= new wxStaticBox(this, -1, _T("&Change slider value"));
252 wxSizer
*sizerMiddle
= new wxStaticBoxSizer(box2
, wxVERTICAL
);
255 wxSizer
*sizerRow
= CreateSizerWithTextAndLabel(_T("Current value"),
256 SliderPage_CurValueText
,
258 text
->SetEditable(FALSE
);
260 sizerMiddle
->Add(sizerRow
, 0, wxALL
| wxGROW
, 5);
262 sizerRow
= CreateSizerWithTextAndButton(SliderPage_SetValue
,
264 SliderPage_ValueText
,
266 sizerMiddle
->Add(sizerRow
, 0, wxALL
| wxGROW
, 5);
268 sizerRow
= CreateSizerWithTextAndButton(SliderPage_SetMinAndMax
,
273 m_textMax
= new wxTextCtrl(this, SliderPage_MaxText
, _T(""));
274 sizerRow
->Add(m_textMax
, 1, wxLEFT
| wxALIGN_CENTRE_VERTICAL
, 5);
276 m_textMin
->SetValue( wxString::Format(_T("%d"), m_min
) );
277 m_textMax
->SetValue( wxString::Format(_T("%d"), m_max
) );
279 sizerMiddle
->Add(sizerRow
, 0, wxALL
| wxGROW
, 5);
281 sizerRow
= CreateSizerWithTextAndButton(SliderPage_SetTickFreq
,
282 _T("Tick &frequency"),
283 SliderPage_TickFreqText
,
286 m_textTickFreq
->SetValue(_T("10"));
288 sizerMiddle
->Add(sizerRow
, 0, wxALL
| wxGROW
, 5);
290 sizerRow
= CreateSizerWithTextAndButton(SliderPage_SetThumbLen
,
292 SliderPage_ThumbLenText
,
295 sizerMiddle
->Add(sizerRow
, 0, wxALL
| wxGROW
, 5);
298 wxSizer
*sizerRight
= new wxBoxSizer(wxHORIZONTAL
);
299 sizerRight
->SetMinSize(150, 40);
300 m_sizerSlider
= sizerRight
; // save it to modify it later
305 // the 3 panes panes compose the window
306 sizerTop
->Add(sizerLeft
, 0, wxGROW
| (wxALL
& ~wxLEFT
), 10);
307 sizerTop
->Add(sizerMiddle
, 0, wxGROW
| wxALL
, 10);
308 sizerTop
->Add(sizerRight
, 1, wxGROW
| (wxALL
& ~wxRIGHT
), 10);
310 // final initializations
317 SliderWidgetsPage::~SliderWidgetsPage()
321 // ----------------------------------------------------------------------------
323 // ----------------------------------------------------------------------------
325 void SliderWidgetsPage::Reset()
327 m_chkVert
->SetValue(FALSE
);
328 m_chkTicks
->SetValue(TRUE
);
329 m_chkLabels
->SetValue(TRUE
);
330 m_chkBothSides
->SetValue(FALSE
);
332 m_radioSides
->SetSelection(StaticSides_Top
);
335 void SliderWidgetsPage::CreateSlider()
339 bool isVert
= m_chkVert
->GetValue();
341 flags
|= wxSL_VERTICAL
;
343 flags
|= wxSL_HORIZONTAL
;
345 if ( m_chkLabels
->GetValue() )
347 flags
|= wxSL_LABELS
;
350 if ( m_chkTicks
->GetValue() )
352 flags
|= wxSL_AUTOTICKS
;
355 switch ( m_radioSides
->GetSelection() )
357 case StaticSides_Top
:
360 case StaticSides_Left
:
363 case StaticSides_Bottom
:
364 flags
|= wxSL_BOTTOM
;
366 case StaticSides_Right
:
370 wxFAIL_MSG(_T("unexpected radiobox selection"));
374 if ( m_chkBothSides
->GetValue() )
382 int valOld
= m_slider
->GetValue();
383 if ( !IsValidValue(valOld
) )
388 m_sizerSlider
->Detach( m_slider
);
390 if ( m_sizerSlider
->GetChildren().GetCount() )
392 // we have 2 spacers, remove them too
393 m_sizerSlider
->Remove( 0 );
394 m_sizerSlider
->Remove( 0 );
400 m_slider
= new wxSlider(this, SliderPage_Slider
,
402 wxDefaultPosition
, wxDefaultSize
,
407 m_sizerSlider
->Add(0, 0, 1);
408 m_sizerSlider
->Add(m_slider
, 0, wxGROW
| wxALL
, 5);
409 m_sizerSlider
->Add(0, 0, 1);
413 m_sizerSlider
->Add(m_slider
, 1, wxCENTRE
| wxALL
, 5);
416 if ( m_chkTicks
->GetValue() )
421 m_sizerSlider
->Layout();
424 void SliderWidgetsPage::DoSetTickFreq()
427 if ( !m_textTickFreq
->GetValue().ToLong(&freq
) )
429 wxLogWarning(_T("Invalid slider tick frequency"));
434 m_slider
->SetTickFreq(freq
, 0 /* unused */);
437 void SliderWidgetsPage::DoSetThumbLen()
440 if ( !m_textThumbLen
->GetValue().ToLong(&len
) )
442 wxLogWarning(_T("Invalid slider thumb lenght"));
447 m_slider
->SetThumbLength(len
);
450 // ----------------------------------------------------------------------------
452 // ----------------------------------------------------------------------------
454 void SliderWidgetsPage::OnButtonReset(wxCommandEvent
& WXUNUSED(event
))
461 void SliderWidgetsPage::OnButtonSetTickFreq(wxCommandEvent
& WXUNUSED(event
))
466 void SliderWidgetsPage::OnButtonSetThumbLen(wxCommandEvent
& WXUNUSED(event
))
471 void SliderWidgetsPage::OnButtonSetMinAndMax(wxCommandEvent
& WXUNUSED(event
))
474 maxNew
= 0; // init to suppress compiler warning
475 if ( !m_textMin
->GetValue().ToLong(&minNew
) ||
476 !m_textMax
->GetValue().ToLong(&maxNew
) ||
479 wxLogWarning(_T("Invalid min/max values for the slider."));
487 m_slider
->SetRange(minNew
, maxNew
);
490 void SliderWidgetsPage::OnButtonSetValue(wxCommandEvent
& WXUNUSED(event
))
493 if ( !m_textValue
->GetValue().ToLong(&val
) || !IsValidValue(val
) )
495 wxLogWarning(_T("Invalid slider value."));
500 m_slider
->SetValue(val
);
503 void SliderWidgetsPage::OnUpdateUIValueButton(wxUpdateUIEvent
& event
)
506 event
.Enable( m_textValue
->GetValue().ToLong(&val
) && IsValidValue(val
) );
509 void SliderWidgetsPage::OnUpdateUITickFreq(wxUpdateUIEvent
& event
)
512 event
.Enable( m_chkTicks
->GetValue() &&
513 m_textTickFreq
->GetValue().ToLong(&freq
) &&
514 (freq
> 0) && (freq
<= m_max
- m_min
) );
517 void SliderWidgetsPage::OnUpdateUIThumbLen(wxUpdateUIEvent
& event
)
520 event
.Enable( m_textThumbLen
->GetValue().ToLong(&val
));
523 void SliderWidgetsPage::OnUpdateUIMinMaxButton(wxUpdateUIEvent
& event
)
526 event
.Enable( m_textMin
->GetValue().ToLong(&mn
) &&
527 m_textMax
->GetValue().ToLong(&mx
) &&
531 void SliderWidgetsPage::OnUpdateUIResetButton(wxUpdateUIEvent
& event
)
533 event
.Enable( m_chkVert
->GetValue() ||
534 !m_chkTicks
->GetValue() ||
535 !m_chkLabels
->GetValue() ||
536 m_chkBothSides
->GetValue() );
539 void SliderWidgetsPage::OnCheckOrRadioBox(wxCommandEvent
& event
)
544 void SliderWidgetsPage::OnUpdateUICurValueText(wxUpdateUIEvent
& event
)
546 event
.SetText( wxString::Format(_T("%d"), m_slider
->GetValue()) );
549 void SliderWidgetsPage::OnUpdateUIRadioSides(wxUpdateUIEvent
& event
)
551 event
.Enable( m_chkLabels
->GetValue() || m_chkTicks
->GetValue() );
554 void SliderWidgetsPage::OnUpdateUIBothSides(wxUpdateUIEvent
& event
)
556 #if defined(__WIN95__) || defined(__WXUNIVERSAL__)
557 event
.Enable( m_chkTicks
->GetValue() );
559 event
.Enable( FALSE
);
560 #endif // defined(__WIN95__) || defined(__WXUNIVERSAL__)
563 void SliderWidgetsPage::OnSlider(wxScrollEvent
& event
)
565 wxASSERT_MSG( event
.GetInt() == m_slider
->GetValue(),
566 wxT("slider value should be the same") );
568 wxEventType eventType
= event
.GetEventType();
571 This array takes the EXACT order of the declarations in
573 (section "wxScrollBar and wxSlider event identifiers")
575 static const wxChar
*eventNames
[] =
577 wxT("wxEVT_SCROLL_TOP"),
578 wxT("wxEVT_SCROLL_BOTTOM"),
579 wxT("wxEVT_SCROLL_LINEUP"),
580 wxT("wxEVT_SCROLL_LINEDOWN"),
581 wxT("wxEVT_SCROLL_PAGEUP"),
582 wxT("wxEVT_SCROLL_PAGEDOWN"),
583 wxT("wxEVT_SCROLL_THUMBTRACK"),
584 wxT("wxEVT_SCROLL_THUMBRELEASE"),
585 wxT("wxEVT_SCROLL_ENDSCROLL")
588 int index
= eventType
- wxEVT_SCROLL_TOP
;
591 If this assert is triggered, there is an unknown slider event which
592 should be added to the above eventNames array.
594 wxASSERT_MSG(index
>= 0 && (size_t)index
< WXSIZEOF(eventNames
),
595 wxT("Unknown slider event") );
598 static int s_numSliderEvents
= 0;
600 wxLogMessage(wxT("Slider event #%d: %s (pos = %d)"),
603 event
.GetPosition());