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"
43 #include "icons/slider.xpm"
45 // ----------------------------------------------------------------------------
47 // ----------------------------------------------------------------------------
52 SliderPage_Reset
= 100,
55 SliderPage_SetMinAndMax
,
56 SliderPage_SetTickFreq
,
57 SliderPage_CurValueText
,
61 SliderPage_TickFreqText
,
66 // ----------------------------------------------------------------------------
68 // ----------------------------------------------------------------------------
70 class SliderWidgetsPage
: public WidgetsPage
73 SliderWidgetsPage(wxNotebook
*notebook
, wxImageList
*imaglist
);
74 virtual ~SliderWidgetsPage();
78 void OnButtonReset(wxCommandEvent
& event
);
79 void OnButtonClear(wxCommandEvent
& event
);
80 void OnButtonSetValue(wxCommandEvent
& event
);
81 void OnButtonSetMinAndMax(wxCommandEvent
& event
);
82 void OnButtonSetTickFreq(wxCommandEvent
& event
);
84 void OnCheckOrRadioBox(wxCommandEvent
& event
);
86 void OnSlider(wxCommandEvent
& event
);
88 void OnUpdateUIOtherSide(wxUpdateUIEvent
& event
);
89 void OnUpdateUIValueButton(wxUpdateUIEvent
& event
);
90 void OnUpdateUIMinMaxButton(wxUpdateUIEvent
& event
);
91 void OnUpdateUITickFreq(wxUpdateUIEvent
& event
);
93 void OnUpdateUIResetButton(wxUpdateUIEvent
& event
);
95 void OnUpdateUICurValueText(wxUpdateUIEvent
& event
);
97 // reset the slider parameters
100 // (re)create the slider
103 // set the tick frequency from the text field value
104 void DoSetTickFreq();
106 // is this slider value in range?
107 bool IsValidValue(int val
) const
108 { return (val
>= m_min
) && (val
<= m_max
); }
116 // the check/radio boxes for styles
117 wxCheckBox
*m_chkLabels
,
122 // the slider itself and the sizer it is in
124 wxSizer
*m_sizerSlider
;
126 // the text entries for set value/range
127 wxTextCtrl
*m_textValue
,
133 DECLARE_EVENT_TABLE()
134 DECLARE_WIDGETS_PAGE(SliderWidgetsPage
)
137 // ----------------------------------------------------------------------------
139 // ----------------------------------------------------------------------------
141 BEGIN_EVENT_TABLE(SliderWidgetsPage
, WidgetsPage
)
142 EVT_BUTTON(SliderPage_Reset
, SliderWidgetsPage::OnButtonReset
)
143 EVT_BUTTON(SliderPage_SetValue
, SliderWidgetsPage::OnButtonSetValue
)
144 EVT_BUTTON(SliderPage_SetMinAndMax
, SliderWidgetsPage::OnButtonSetMinAndMax
)
145 EVT_BUTTON(SliderPage_SetTickFreq
, SliderWidgetsPage::OnButtonSetTickFreq
)
147 EVT_UPDATE_UI(SliderPage_OtherSide
, SliderWidgetsPage::OnUpdateUIOtherSide
)
149 EVT_UPDATE_UI(SliderPage_SetValue
, SliderWidgetsPage::OnUpdateUIValueButton
)
150 EVT_UPDATE_UI(SliderPage_SetMinAndMax
, SliderWidgetsPage::OnUpdateUIMinMaxButton
)
151 EVT_UPDATE_UI(SliderPage_SetTickFreq
, SliderWidgetsPage::OnUpdateUITickFreq
)
152 EVT_UPDATE_UI(SliderPage_TickFreqText
, SliderWidgetsPage::OnUpdateUITickFreq
)
154 EVT_UPDATE_UI(SliderPage_Reset
, SliderWidgetsPage::OnUpdateUIResetButton
)
156 EVT_UPDATE_UI(SliderPage_CurValueText
, SliderWidgetsPage::OnUpdateUICurValueText
)
158 EVT_SLIDER(SliderPage_Slider
, SliderWidgetsPage::OnSlider
)
160 EVT_CHECKBOX(-1, SliderWidgetsPage::OnCheckOrRadioBox
)
161 EVT_RADIOBOX(-1, SliderWidgetsPage::OnCheckOrRadioBox
)
164 // ============================================================================
166 // ============================================================================
168 IMPLEMENT_WIDGETS_PAGE(SliderWidgetsPage
, _T("Slider"));
170 SliderWidgetsPage::SliderWidgetsPage(wxNotebook
*notebook
,
171 wxImageList
*imaglist
)
172 : WidgetsPage(notebook
)
174 imaglist
->Add(wxBitmap(slider_xpm
));
183 m_chkOtherSide
= (wxCheckBox
*)NULL
;
185 m_slider
= (wxSlider
*)NULL
;
186 m_sizerSlider
= (wxSizer
*)NULL
;
188 wxSizer
*sizerTop
= new wxBoxSizer(wxHORIZONTAL
);
191 wxStaticBox
*box
= new wxStaticBox(this, -1, _T("&Set style"));
192 wxSizer
*sizerLeft
= new wxStaticBoxSizer(box
, wxVERTICAL
);
194 m_chkVert
= CreateCheckBoxAndAddToSizer(sizerLeft
, _T("&Vertical"));
195 m_chkTicks
= CreateCheckBoxAndAddToSizer(sizerLeft
, _T("Show &ticks"));
196 m_chkLabels
= CreateCheckBoxAndAddToSizer(sizerLeft
, _T("Show &labels"));
197 m_chkOtherSide
= CreateCheckBoxAndAddToSizer
200 _T("On &other side"),
204 sizerLeft
->Add(5, 5, 0, wxGROW
| wxALL
, 5); // spacer
206 wxButton
*btn
= new wxButton(this, SliderPage_Reset
, _T("&Reset"));
207 sizerLeft
->Add(btn
, 0, wxALIGN_CENTRE_HORIZONTAL
| wxALL
, 15);
210 wxStaticBox
*box2
= new wxStaticBox(this, -1, _T("&Change slider value"));
211 wxSizer
*sizerMiddle
= new wxStaticBoxSizer(box2
, wxVERTICAL
);
214 wxSizer
*sizerRow
= CreateSizerWithTextAndLabel(_T("Current value"),
215 SliderPage_CurValueText
,
217 text
->SetEditable(FALSE
);
219 sizerMiddle
->Add(sizerRow
, 0, wxALL
| wxGROW
, 5);
221 sizerRow
= CreateSizerWithTextAndButton(SliderPage_SetValue
,
223 SliderPage_ValueText
,
225 sizerMiddle
->Add(sizerRow
, 0, wxALL
| wxGROW
, 5);
227 sizerRow
= CreateSizerWithTextAndButton(SliderPage_SetMinAndMax
,
232 m_textMax
= new wxTextCtrl(this, SliderPage_MaxText
, _T(""));
233 sizerRow
->Add(m_textMax
, 1, wxLEFT
| wxALIGN_CENTRE_VERTICAL
, 5);
235 m_textMin
->SetValue(wxString::Format(_T("%lu"), m_min
));
236 m_textMax
->SetValue(wxString::Format(_T("%lu"), m_max
));
238 sizerMiddle
->Add(sizerRow
, 0, wxALL
| wxGROW
, 5);
240 sizerRow
= CreateSizerWithTextAndButton(SliderPage_SetTickFreq
,
241 _T("Tick &frequency"),
242 SliderPage_TickFreqText
,
245 m_textTickFreq
->SetValue(_T("10"));
247 sizerMiddle
->Add(sizerRow
, 0, wxALL
| wxGROW
, 5);
250 wxSizer
*sizerRight
= new wxBoxSizer(wxHORIZONTAL
);
251 sizerRight
->SetMinSize(150, 0);
252 m_sizerSlider
= sizerRight
; // save it to modify it later
257 // the 3 panes panes compose the window
258 sizerTop
->Add(sizerLeft
, 0, wxGROW
| (wxALL
& ~wxLEFT
), 10);
259 sizerTop
->Add(sizerMiddle
, 0, wxGROW
| wxALL
, 10);
260 sizerTop
->Add(sizerRight
, 1, wxGROW
| (wxALL
& ~wxRIGHT
), 10);
262 // final initializations
269 SliderWidgetsPage::~SliderWidgetsPage()
273 // ----------------------------------------------------------------------------
275 // ----------------------------------------------------------------------------
277 void SliderWidgetsPage::Reset()
279 m_chkLabels
->SetValue(TRUE
);
280 m_chkTicks
->SetValue(FALSE
);
281 m_chkVert
->SetValue(FALSE
);
282 m_chkOtherSide
->SetValue(FALSE
);
285 void SliderWidgetsPage::CreateSlider()
289 bool isVert
= m_chkVert
->GetValue();
291 flags
|= wxSL_VERTICAL
;
293 flags
|= wxSL_HORIZONTAL
;
295 if ( m_chkLabels
->GetValue() )
297 flags
|= wxSL_LABELS
;
299 if ( m_chkOtherSide
->GetValue() )
300 flags
|= isVert
? wxSL_RIGHT
: wxSL_BOTTOM
;
302 flags
|= isVert
? wxSL_LEFT
: wxSL_TOP
;
305 if ( m_chkTicks
->GetValue() )
307 flags
|= wxSL_AUTOTICKS
;
313 int valOld
= m_slider
->GetValue();
314 if ( !IsValidValue(valOld
) )
319 m_sizerSlider
->Remove(m_slider
);
321 if ( m_sizerSlider
->GetChildren().GetCount() )
323 // we have 2 spacers, remove them too
324 m_sizerSlider
->Remove((int)0);
325 m_sizerSlider
->Remove((int)0);
331 m_slider
= new wxSlider(this, SliderPage_Slider
,
333 wxDefaultPosition
, wxDefaultSize
,
338 m_sizerSlider
->Add(0, 0, 1);
339 m_sizerSlider
->Add(m_slider
, 0, wxGROW
| wxALL
, 5);
340 m_sizerSlider
->Add(0, 0, 1);
344 m_sizerSlider
->Add(m_slider
, 1, wxCENTRE
| wxALL
, 5);
347 if ( m_chkTicks
->GetValue() )
352 m_sizerSlider
->Layout();
355 void SliderWidgetsPage::DoSetTickFreq()
358 if ( !m_textTickFreq
->GetValue().ToLong(&freq
) )
360 wxLogWarning(_T("Invalid slider tick frequency"));
365 m_slider
->SetTickFreq(freq
, 0 /* unused */);
368 // ----------------------------------------------------------------------------
370 // ----------------------------------------------------------------------------
372 void SliderWidgetsPage::OnButtonReset(wxCommandEvent
& WXUNUSED(event
))
379 void SliderWidgetsPage::OnButtonSetTickFreq(wxCommandEvent
& WXUNUSED(event
))
384 void SliderWidgetsPage::OnButtonSetMinAndMax(wxCommandEvent
& WXUNUSED(event
))
387 maxNew
= 0; // init to suppress compiler warning
388 if ( !m_textMin
->GetValue().ToLong(&minNew
) ||
389 !m_textMax
->GetValue().ToLong(&maxNew
) ||
392 wxLogWarning(_T("Invalid min/max values for the slider."));
400 m_slider
->SetRange(minNew
, maxNew
);
403 void SliderWidgetsPage::OnButtonSetValue(wxCommandEvent
& WXUNUSED(event
))
406 if ( !m_textValue
->GetValue().ToLong(&val
) || !IsValidValue(val
) )
408 wxLogWarning(_T("Invalid slider value."));
413 m_slider
->SetValue(val
);
416 void SliderWidgetsPage::OnUpdateUIValueButton(wxUpdateUIEvent
& event
)
419 event
.Enable( m_textValue
->GetValue().ToLong(&val
) && IsValidValue(val
) );
422 void SliderWidgetsPage::OnUpdateUITickFreq(wxUpdateUIEvent
& event
)
425 event
.Enable( m_chkTicks
->GetValue() &&
426 m_textTickFreq
->GetValue().ToLong(&freq
) &&
427 (freq
> 0) && (freq
<= m_max
- m_min
) );
430 void SliderWidgetsPage::OnUpdateUIMinMaxButton(wxUpdateUIEvent
& event
)
433 event
.Enable( m_textMin
->GetValue().ToLong(&mn
) &&
434 m_textMax
->GetValue().ToLong(&mx
) &&
438 void SliderWidgetsPage::OnUpdateUIResetButton(wxUpdateUIEvent
& event
)
440 event
.Enable( m_chkVert
->GetValue() ||
441 !m_chkLabels
->GetValue() ||
442 m_chkOtherSide
->GetValue() ||
443 m_chkTicks
->GetValue() );
446 void SliderWidgetsPage::OnCheckOrRadioBox(wxCommandEvent
& event
)
451 void SliderWidgetsPage::OnUpdateUICurValueText(wxUpdateUIEvent
& event
)
453 event
.SetText( wxString::Format(_T("%d"), m_slider
->GetValue()));
456 void SliderWidgetsPage::OnUpdateUIOtherSide(wxUpdateUIEvent
& event
)
458 event
.Enable( m_chkLabels
->GetValue() );
461 void SliderWidgetsPage::OnSlider(wxCommandEvent
& event
)
463 int value
= event
.GetInt();
465 wxASSERT_MSG( value
== m_slider
->GetValue(),
466 _T("slider value should be the same") );
468 wxLogMessage(_T("Slider value changed, now %d"), value
);