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"
42 #include "icons/slider.xpm"
44 // ----------------------------------------------------------------------------
46 // ----------------------------------------------------------------------------
51 SliderPage_Reset
= wxID_HIGHEST
,
54 SliderPage_SetMinAndMax
,
55 SliderPage_SetTickFreq
,
56 SliderPage_CurValueText
,
60 SliderPage_TickFreqText
,
65 // ----------------------------------------------------------------------------
67 // ----------------------------------------------------------------------------
69 class SliderWidgetsPage
: public WidgetsPage
72 SliderWidgetsPage(wxNotebook
*notebook
, wxImageList
*imaglist
);
73 virtual ~SliderWidgetsPage();
77 void OnButtonReset(wxCommandEvent
& event
);
78 void OnButtonClear(wxCommandEvent
& event
);
79 void OnButtonSetValue(wxCommandEvent
& event
);
80 void OnButtonSetMinAndMax(wxCommandEvent
& event
);
81 void OnButtonSetTickFreq(wxCommandEvent
& event
);
83 void OnCheckOrRadioBox(wxCommandEvent
& event
);
85 void OnSlider(wxScrollEvent
& event
);
87 void OnUpdateUIOtherSide(wxUpdateUIEvent
& event
);
88 void OnUpdateUIValueButton(wxUpdateUIEvent
& event
);
89 void OnUpdateUIMinMaxButton(wxUpdateUIEvent
& event
);
90 void OnUpdateUITickFreq(wxUpdateUIEvent
& event
);
92 void OnUpdateUIResetButton(wxUpdateUIEvent
& event
);
94 void OnUpdateUICurValueText(wxUpdateUIEvent
& event
);
96 // reset the slider parameters
99 // (re)create the slider
102 // set the tick frequency from the text field value
103 void DoSetTickFreq();
105 // is this slider value in range?
106 bool IsValidValue(int val
) const
107 { return (val
>= m_min
) && (val
<= m_max
); }
115 // the check/radio boxes for styles
116 wxCheckBox
*m_chkLabels
,
121 // the slider itself and the sizer it is in
123 wxSizer
*m_sizerSlider
;
125 // the text entries for set value/range
126 wxTextCtrl
*m_textValue
,
132 DECLARE_EVENT_TABLE()
133 DECLARE_WIDGETS_PAGE(SliderWidgetsPage
)
136 // ----------------------------------------------------------------------------
138 // ----------------------------------------------------------------------------
140 BEGIN_EVENT_TABLE(SliderWidgetsPage
, WidgetsPage
)
141 EVT_BUTTON(SliderPage_Reset
, SliderWidgetsPage::OnButtonReset
)
142 EVT_BUTTON(SliderPage_SetValue
, SliderWidgetsPage::OnButtonSetValue
)
143 EVT_BUTTON(SliderPage_SetMinAndMax
, SliderWidgetsPage::OnButtonSetMinAndMax
)
144 EVT_BUTTON(SliderPage_SetTickFreq
, SliderWidgetsPage::OnButtonSetTickFreq
)
146 EVT_UPDATE_UI(SliderPage_OtherSide
, SliderWidgetsPage::OnUpdateUIOtherSide
)
148 EVT_UPDATE_UI(SliderPage_SetValue
, SliderWidgetsPage::OnUpdateUIValueButton
)
149 EVT_UPDATE_UI(SliderPage_SetMinAndMax
, SliderWidgetsPage::OnUpdateUIMinMaxButton
)
150 EVT_UPDATE_UI(SliderPage_SetTickFreq
, SliderWidgetsPage::OnUpdateUITickFreq
)
151 EVT_UPDATE_UI(SliderPage_TickFreqText
, SliderWidgetsPage::OnUpdateUITickFreq
)
153 EVT_UPDATE_UI(SliderPage_Reset
, SliderWidgetsPage::OnUpdateUIResetButton
)
155 EVT_UPDATE_UI(SliderPage_CurValueText
, SliderWidgetsPage::OnUpdateUICurValueText
)
157 EVT_COMMAND_SCROLL(SliderPage_Slider
, SliderWidgetsPage::OnSlider
)
159 EVT_CHECKBOX(-1, SliderWidgetsPage::OnCheckOrRadioBox
)
160 EVT_RADIOBOX(-1, SliderWidgetsPage::OnCheckOrRadioBox
)
163 // ============================================================================
165 // ============================================================================
167 IMPLEMENT_WIDGETS_PAGE(SliderWidgetsPage
, _T("Slider"));
169 SliderWidgetsPage::SliderWidgetsPage(wxNotebook
*notebook
,
170 wxImageList
*imaglist
)
171 : WidgetsPage(notebook
)
173 imaglist
->Add(wxBitmap(slider_xpm
));
182 m_chkOtherSide
= (wxCheckBox
*)NULL
;
184 m_slider
= (wxSlider
*)NULL
;
185 m_sizerSlider
= (wxSizer
*)NULL
;
187 wxSizer
*sizerTop
= new wxBoxSizer(wxHORIZONTAL
);
190 wxStaticBox
*box
= new wxStaticBox(this, -1, _T("&Set style"));
191 wxSizer
*sizerLeft
= new wxStaticBoxSizer(box
, wxVERTICAL
);
193 m_chkVert
= CreateCheckBoxAndAddToSizer(sizerLeft
, _T("&Vertical"));
194 m_chkTicks
= CreateCheckBoxAndAddToSizer(sizerLeft
, _T("Show &ticks"));
195 m_chkLabels
= CreateCheckBoxAndAddToSizer(sizerLeft
, _T("Show &labels"));
196 m_chkOtherSide
= CreateCheckBoxAndAddToSizer
199 _T("On &other side"),
203 sizerLeft
->Add(5, 5, 0, wxGROW
| wxALL
, 5); // spacer
205 wxButton
*btn
= new wxButton(this, SliderPage_Reset
, _T("&Reset"));
206 sizerLeft
->Add(btn
, 0, wxALIGN_CENTRE_HORIZONTAL
| wxALL
, 15);
209 wxStaticBox
*box2
= new wxStaticBox(this, -1, _T("&Change slider value"));
210 wxSizer
*sizerMiddle
= new wxStaticBoxSizer(box2
, wxVERTICAL
);
213 wxSizer
*sizerRow
= CreateSizerWithTextAndLabel(_T("Current value"),
214 SliderPage_CurValueText
,
216 text
->SetEditable(FALSE
);
218 sizerMiddle
->Add(sizerRow
, 0, wxALL
| wxGROW
, 5);
220 sizerRow
= CreateSizerWithTextAndButton(SliderPage_SetValue
,
222 SliderPage_ValueText
,
224 sizerMiddle
->Add(sizerRow
, 0, wxALL
| wxGROW
, 5);
226 sizerRow
= CreateSizerWithTextAndButton(SliderPage_SetMinAndMax
,
231 m_textMax
= new wxTextCtrl(this, SliderPage_MaxText
, _T(""));
232 sizerRow
->Add(m_textMax
, 1, wxLEFT
| wxALIGN_CENTRE_VERTICAL
, 5);
234 m_textMin
->SetValue(wxString::Format(_T("%lu"), m_min
));
235 m_textMax
->SetValue(wxString::Format(_T("%lu"), m_max
));
237 sizerMiddle
->Add(sizerRow
, 0, wxALL
| wxGROW
, 5);
239 sizerRow
= CreateSizerWithTextAndButton(SliderPage_SetTickFreq
,
240 _T("Tick &frequency"),
241 SliderPage_TickFreqText
,
244 m_textTickFreq
->SetValue(_T("10"));
246 sizerMiddle
->Add(sizerRow
, 0, wxALL
| wxGROW
, 5);
249 wxSizer
*sizerRight
= new wxBoxSizer(wxHORIZONTAL
);
250 sizerRight
->SetMinSize(150, 0);
251 m_sizerSlider
= sizerRight
; // save it to modify it later
256 // the 3 panes panes compose the window
257 sizerTop
->Add(sizerLeft
, 0, wxGROW
| (wxALL
& ~wxLEFT
), 10);
258 sizerTop
->Add(sizerMiddle
, 0, wxGROW
| wxALL
, 10);
259 sizerTop
->Add(sizerRight
, 1, wxGROW
| (wxALL
& ~wxRIGHT
), 10);
261 // final initializations
268 SliderWidgetsPage::~SliderWidgetsPage()
272 // ----------------------------------------------------------------------------
274 // ----------------------------------------------------------------------------
276 void SliderWidgetsPage::Reset()
278 m_chkLabels
->SetValue(TRUE
);
279 m_chkTicks
->SetValue(FALSE
);
280 m_chkVert
->SetValue(FALSE
);
281 m_chkOtherSide
->SetValue(FALSE
);
284 void SliderWidgetsPage::CreateSlider()
288 bool isVert
= m_chkVert
->GetValue();
290 flags
|= wxSL_VERTICAL
;
292 flags
|= wxSL_HORIZONTAL
;
294 if ( m_chkLabels
->GetValue() )
296 flags
|= wxSL_LABELS
;
298 if ( m_chkOtherSide
->GetValue() )
299 flags
|= isVert
? wxSL_RIGHT
: wxSL_BOTTOM
;
301 flags
|= isVert
? wxSL_LEFT
: wxSL_TOP
;
304 if ( m_chkTicks
->GetValue() )
306 flags
|= wxSL_AUTOTICKS
;
312 int valOld
= m_slider
->GetValue();
313 if ( !IsValidValue(valOld
) )
318 m_sizerSlider
->Remove(m_slider
);
320 if ( m_sizerSlider
->GetChildren().GetCount() )
322 // we have 2 spacers, remove them too
323 m_sizerSlider
->Remove((int)0);
324 m_sizerSlider
->Remove((int)0);
330 m_slider
= new wxSlider(this, SliderPage_Slider
,
332 wxDefaultPosition
, wxDefaultSize
,
337 m_sizerSlider
->Add(0, 0, 1);
338 m_sizerSlider
->Add(m_slider
, 0, wxGROW
| wxALL
, 5);
339 m_sizerSlider
->Add(0, 0, 1);
343 m_sizerSlider
->Add(m_slider
, 1, wxCENTRE
| wxALL
, 5);
346 if ( m_chkTicks
->GetValue() )
351 m_sizerSlider
->Layout();
354 void SliderWidgetsPage::DoSetTickFreq()
357 if ( !m_textTickFreq
->GetValue().ToLong(&freq
) )
359 wxLogWarning(_T("Invalid slider tick frequency"));
364 m_slider
->SetTickFreq(freq
, 0 /* unused */);
367 // ----------------------------------------------------------------------------
369 // ----------------------------------------------------------------------------
371 void SliderWidgetsPage::OnButtonReset(wxCommandEvent
& WXUNUSED(event
))
378 void SliderWidgetsPage::OnButtonSetTickFreq(wxCommandEvent
& WXUNUSED(event
))
383 void SliderWidgetsPage::OnButtonSetMinAndMax(wxCommandEvent
& WXUNUSED(event
))
386 maxNew
= 0; // init to suppress compiler warning
387 if ( !m_textMin
->GetValue().ToLong(&minNew
) ||
388 !m_textMax
->GetValue().ToLong(&maxNew
) ||
391 wxLogWarning(_T("Invalid min/max values for the slider."));
399 m_slider
->SetRange(minNew
, maxNew
);
402 void SliderWidgetsPage::OnButtonSetValue(wxCommandEvent
& WXUNUSED(event
))
405 if ( !m_textValue
->GetValue().ToLong(&val
) || !IsValidValue(val
) )
407 wxLogWarning(_T("Invalid slider value."));
412 m_slider
->SetValue(val
);
415 void SliderWidgetsPage::OnUpdateUIValueButton(wxUpdateUIEvent
& event
)
418 event
.Enable( m_textValue
->GetValue().ToLong(&val
) && IsValidValue(val
) );
421 void SliderWidgetsPage::OnUpdateUITickFreq(wxUpdateUIEvent
& event
)
424 event
.Enable( m_chkTicks
->GetValue() &&
425 m_textTickFreq
->GetValue().ToLong(&freq
) &&
426 (freq
> 0) && (freq
<= m_max
- m_min
) );
429 void SliderWidgetsPage::OnUpdateUIMinMaxButton(wxUpdateUIEvent
& event
)
432 event
.Enable( m_textMin
->GetValue().ToLong(&mn
) &&
433 m_textMax
->GetValue().ToLong(&mx
) &&
437 void SliderWidgetsPage::OnUpdateUIResetButton(wxUpdateUIEvent
& event
)
439 event
.Enable( m_chkVert
->GetValue() ||
440 !m_chkLabels
->GetValue() ||
441 m_chkOtherSide
->GetValue() ||
442 m_chkTicks
->GetValue() );
445 void SliderWidgetsPage::OnCheckOrRadioBox(wxCommandEvent
& event
)
450 void SliderWidgetsPage::OnUpdateUICurValueText(wxUpdateUIEvent
& event
)
452 event
.SetText( wxString::Format(_T("%d"), m_slider
->GetValue()));
455 void SliderWidgetsPage::OnUpdateUIOtherSide(wxUpdateUIEvent
& event
)
457 event
.Enable( m_chkLabels
->GetValue() );
460 void SliderWidgetsPage::OnSlider(wxScrollEvent
& event
)
462 int value
= event
.GetInt();
464 wxASSERT_MSG( value
== m_slider
->GetValue(),
465 wxT("slider value should be the same") );
467 wxEventType eventType
= event
.GetEventType();
470 This array takes the EXACT order of the declarations in
472 (section "wxScrollBar and wxSlider event identifiers")
474 static const wxChar
*eventNames
[] =
476 wxT("wxEVT_SCROLL_TOP"),
477 wxT("wxEVT_SCROLL_BOTTOM"),
478 wxT("wxEVT_SCROLL_LINEUP"),
479 wxT("wxEVT_SCROLL_LINEDOWN"),
480 wxT("wxEVT_SCROLL_PAGEUP"),
481 wxT("wxEVT_SCROLL_PAGEDOWN"),
482 wxT("wxEVT_SCROLL_THUMBTRACK"),
483 wxT("wxEVT_SCROLL_THUMBRELEASE"),
484 wxT("wxEVT_SCROLL_ENDSCROLL")
487 int index
= eventType
- wxEVT_SCROLL_TOP
;
490 If this assert is triggered, there is an unknown slider event which
491 should be added to the above eventNames array.
493 wxASSERT_MSG(index
>= 0 && index
< WXSIZEOF(eventNames
),
494 wxT("Unknown slider event") );
497 static int s_numSliderEvents
= 0;
499 wxLogMessage(wxT("Slider event #%d: %s (pos = %d)"),
502 event
.GetPosition());