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_SetLineSize
,
64 SliderPage_SetPageSize
,
65 SliderPage_SetTickFreq
,
66 SliderPage_SetThumbLen
,
67 SliderPage_CurValueText
,
71 SliderPage_LineSizeText
,
72 SliderPage_PageSizeText
,
73 SliderPage_TickFreqText
,
74 SliderPage_ThumbLenText
,
75 SliderPage_RadioSides
,
80 // sides radiobox values
90 // ----------------------------------------------------------------------------
92 // ----------------------------------------------------------------------------
94 class SliderWidgetsPage
: public WidgetsPage
97 SliderWidgetsPage(WidgetsBookCtrl
*book
, wxImageList
*imaglist
);
98 virtual ~SliderWidgetsPage(){};
100 virtual wxControl
*GetWidget() const { return m_slider
; }
101 virtual void RecreateWidget() { CreateSlider(); }
103 // lazy creation of the content
104 virtual void CreateContent();
108 void OnButtonReset(wxCommandEvent
& event
);
109 void OnButtonClear(wxCommandEvent
& event
);
110 void OnButtonSetValue(wxCommandEvent
& event
);
111 void OnButtonSetMinAndMax(wxCommandEvent
& event
);
112 void OnButtonSetLineSize(wxCommandEvent
& event
);
113 void OnButtonSetPageSize(wxCommandEvent
& event
);
114 void OnButtonSetTickFreq(wxCommandEvent
& event
);
115 void OnButtonSetThumbLen(wxCommandEvent
& event
);
117 void OnCheckOrRadioBox(wxCommandEvent
& event
);
119 void OnSlider(wxScrollEvent
& event
);
121 void OnUpdateUIValueButton(wxUpdateUIEvent
& event
);
122 void OnUpdateUIMinMaxButton(wxUpdateUIEvent
& event
);
123 void OnUpdateUILineSize(wxUpdateUIEvent
& event
);
124 void OnUpdateUIPageSize(wxUpdateUIEvent
& event
);
125 void OnUpdateUITickFreq(wxUpdateUIEvent
& event
);
126 void OnUpdateUIThumbLen(wxUpdateUIEvent
& event
);
127 void OnUpdateUIRadioSides(wxUpdateUIEvent
& event
);
128 void OnUpdateUIBothSides(wxUpdateUIEvent
& event
);
130 void OnUpdateUIResetButton(wxUpdateUIEvent
& event
);
132 void OnUpdateUICurValueText(wxUpdateUIEvent
& event
);
134 // reset the slider parameters
137 // (re)create the slider
140 // set the line size from the text field value
141 void DoSetLineSize();
143 // set the page size from the text field value
144 void DoSetPageSize();
146 // set the tick frequency from the text field value
147 void DoSetTickFreq();
149 // set the thumb len from the text field value
150 void DoSetThumbLen();
152 // is this slider value in range?
153 bool IsValidValue(int val
) const
154 { return (val
>= m_min
) && (val
<= m_max
); }
162 // the check/radio boxes for styles
163 wxCheckBox
*m_chkMinMaxLabels
,
169 wxRadioBox
*m_radioSides
;
171 // the slider itself and the sizer it is in
173 wxSizer
*m_sizerSlider
;
175 // the text entries for set value/range
176 wxTextCtrl
*m_textValue
,
185 DECLARE_EVENT_TABLE()
186 DECLARE_WIDGETS_PAGE(SliderWidgetsPage
)
189 // ----------------------------------------------------------------------------
191 // ----------------------------------------------------------------------------
193 BEGIN_EVENT_TABLE(SliderWidgetsPage
, WidgetsPage
)
194 EVT_BUTTON(SliderPage_Reset
, SliderWidgetsPage::OnButtonReset
)
195 EVT_BUTTON(SliderPage_SetValue
, SliderWidgetsPage::OnButtonSetValue
)
196 EVT_BUTTON(SliderPage_SetMinAndMax
, SliderWidgetsPage::OnButtonSetMinAndMax
)
197 EVT_BUTTON(SliderPage_SetLineSize
, SliderWidgetsPage::OnButtonSetLineSize
)
198 EVT_BUTTON(SliderPage_SetPageSize
, SliderWidgetsPage::OnButtonSetPageSize
)
199 EVT_BUTTON(SliderPage_SetTickFreq
, SliderWidgetsPage::OnButtonSetTickFreq
)
200 EVT_BUTTON(SliderPage_SetThumbLen
, SliderWidgetsPage::OnButtonSetThumbLen
)
202 EVT_UPDATE_UI(SliderPage_SetValue
, SliderWidgetsPage::OnUpdateUIValueButton
)
203 EVT_UPDATE_UI(SliderPage_SetMinAndMax
, SliderWidgetsPage::OnUpdateUIMinMaxButton
)
204 EVT_UPDATE_UI(SliderPage_SetLineSize
, SliderWidgetsPage::OnUpdateUILineSize
)
205 EVT_UPDATE_UI(SliderPage_SetPageSize
, SliderWidgetsPage::OnUpdateUIPageSize
)
206 EVT_UPDATE_UI(SliderPage_SetTickFreq
, SliderWidgetsPage::OnUpdateUITickFreq
)
207 EVT_UPDATE_UI(SliderPage_SetThumbLen
, SliderWidgetsPage::OnUpdateUIThumbLen
)
208 EVT_UPDATE_UI(SliderPage_RadioSides
, SliderWidgetsPage::OnUpdateUIRadioSides
)
209 EVT_UPDATE_UI(SliderPage_BothSides
, SliderWidgetsPage::OnUpdateUIBothSides
)
211 EVT_UPDATE_UI(SliderPage_Reset
, SliderWidgetsPage::OnUpdateUIResetButton
)
213 EVT_UPDATE_UI(SliderPage_CurValueText
, SliderWidgetsPage::OnUpdateUICurValueText
)
215 EVT_COMMAND_SCROLL(SliderPage_Slider
, SliderWidgetsPage::OnSlider
)
217 EVT_CHECKBOX(wxID_ANY
, SliderWidgetsPage::OnCheckOrRadioBox
)
218 EVT_RADIOBOX(wxID_ANY
, SliderWidgetsPage::OnCheckOrRadioBox
)
221 // ============================================================================
223 // ============================================================================
225 #if defined(__WXUNIVERSAL__)
226 #define FAMILY_CTRLS UNIVERSAL_CTRLS
228 #define FAMILY_CTRLS NATIVE_CTRLS
231 IMPLEMENT_WIDGETS_PAGE(SliderWidgetsPage
, wxT("Slider"), FAMILY_CTRLS
);
233 SliderWidgetsPage::SliderWidgetsPage(WidgetsBookCtrl
*book
,
234 wxImageList
*imaglist
)
235 : WidgetsPage(book
, imaglist
, slider_xpm
)
245 m_chkBothSides
= (wxCheckBox
*)NULL
;
247 m_radioSides
= (wxRadioBox
*)NULL
;
249 m_slider
= (wxSlider
*)NULL
;
250 m_sizerSlider
= (wxSizer
*)NULL
;
253 void SliderWidgetsPage::CreateContent()
255 wxSizer
*sizerTop
= new wxBoxSizer(wxHORIZONTAL
);
258 wxStaticBox
*box
= new wxStaticBox(this, wxID_ANY
, wxT("&Set style"));
259 wxSizer
*sizerLeft
= new wxStaticBoxSizer(box
, wxVERTICAL
);
261 m_chkInverse
= CreateCheckBoxAndAddToSizer(sizerLeft
, wxT("&Inverse"));
262 m_chkTicks
= CreateCheckBoxAndAddToSizer(sizerLeft
, wxT("Show &ticks"));
263 m_chkMinMaxLabels
= CreateCheckBoxAndAddToSizer(sizerLeft
, wxT("Show min/max &labels"));
264 m_chkValueLabel
= CreateCheckBoxAndAddToSizer(sizerLeft
, wxT("Show &value label"));
265 static const wxString sides
[] =
273 m_radioSides
= new wxRadioBox(this, SliderPage_RadioSides
, wxT("&Label position"),
274 wxDefaultPosition
, wxDefaultSize
,
275 WXSIZEOF(sides
), sides
,
276 1, wxRA_SPECIFY_COLS
);
277 sizerLeft
->Add(m_radioSides
, 0, wxGROW
| wxALL
, 5);
278 m_chkBothSides
= CreateCheckBoxAndAddToSizer
279 (sizerLeft
, wxT("&Both sides"), SliderPage_BothSides
);
281 m_chkBothSides
->SetToolTip( wxT("\"Both sides\" is only supported \nin Win95 and Universal") );
282 #endif // wxUSE_TOOLTIPS
284 sizerLeft
->Add(5, 5, 0, wxGROW
| wxALL
, 5); // spacer
286 wxButton
*btn
= new wxButton(this, SliderPage_Reset
, wxT("&Reset"));
287 sizerLeft
->Add(btn
, 0, wxALIGN_CENTRE_HORIZONTAL
| wxALL
, 15);
290 wxStaticBox
*box2
= new wxStaticBox(this, wxID_ANY
, wxT("&Change slider value"));
291 wxSizer
*sizerMiddle
= new wxStaticBoxSizer(box2
, wxVERTICAL
);
294 wxSizer
*sizerRow
= CreateSizerWithTextAndLabel(wxT("Current value"),
295 SliderPage_CurValueText
,
297 text
->SetEditable(false);
299 sizerMiddle
->Add(sizerRow
, 0, wxALL
| wxGROW
, 5);
301 sizerRow
= CreateSizerWithTextAndButton(SliderPage_SetValue
,
303 SliderPage_ValueText
,
305 sizerMiddle
->Add(sizerRow
, 0, wxALL
| wxGROW
, 5);
307 sizerRow
= CreateSizerWithTextAndButton(SliderPage_SetMinAndMax
,
312 m_textMax
= new wxTextCtrl(this, SliderPage_MaxText
, wxEmptyString
);
313 sizerRow
->Add(m_textMax
, 1, wxLEFT
| wxALIGN_CENTRE_VERTICAL
, 5);
315 m_textMin
->SetValue( wxString::Format(wxT("%d"), m_min
) );
316 m_textMax
->SetValue( wxString::Format(wxT("%d"), m_max
) );
318 sizerMiddle
->Add(sizerRow
, 0, wxALL
| wxGROW
, 5);
320 sizerRow
= CreateSizerWithTextAndButton(SliderPage_SetLineSize
,
322 SliderPage_LineSizeText
,
325 sizerMiddle
->Add(sizerRow
, 0, wxALL
| wxGROW
, 5);
327 sizerRow
= CreateSizerWithTextAndButton(SliderPage_SetPageSize
,
329 SliderPage_PageSizeText
,
332 sizerMiddle
->Add(sizerRow
, 0, wxALL
| wxGROW
, 5);
334 sizerRow
= CreateSizerWithTextAndButton(SliderPage_SetTickFreq
,
335 wxT("Tick &frequency"),
336 SliderPage_TickFreqText
,
339 m_textTickFreq
->SetValue(wxT("10"));
341 sizerMiddle
->Add(sizerRow
, 0, wxALL
| wxGROW
, 5);
343 sizerRow
= CreateSizerWithTextAndButton(SliderPage_SetThumbLen
,
344 wxT("Thumb &length"),
345 SliderPage_ThumbLenText
,
348 sizerMiddle
->Add(sizerRow
, 0, wxALL
| wxGROW
, 5);
351 wxSizer
*sizerRight
= new wxBoxSizer(wxHORIZONTAL
);
352 sizerRight
->SetMinSize(150, 40);
353 m_sizerSlider
= sizerRight
; // save it to modify it later
358 m_textLineSize
->SetValue(wxString::Format(wxT("%d"), m_slider
->GetLineSize()));
359 m_textPageSize
->SetValue(wxString::Format(wxT("%d"), m_slider
->GetPageSize()));
361 // the 3 panes panes compose the window
362 sizerTop
->Add(sizerLeft
, 0, wxGROW
| (wxALL
& ~wxLEFT
), 10);
363 sizerTop
->Add(sizerMiddle
, 0, wxGROW
| wxALL
, 10);
364 sizerTop
->Add(sizerRight
, 1, wxGROW
| (wxALL
& ~wxRIGHT
), 10);
366 // final initializations
370 // ----------------------------------------------------------------------------
372 // ----------------------------------------------------------------------------
374 void SliderWidgetsPage::Reset()
376 m_chkInverse
->SetValue(false);
377 m_chkTicks
->SetValue(true);
378 m_chkValueLabel
->SetValue(true);
379 m_chkMinMaxLabels
->SetValue(true);
380 m_chkBothSides
->SetValue(false);
382 m_radioSides
->SetSelection(SliderTicks_None
);
385 void SliderWidgetsPage::CreateSlider()
387 int flags
= ms_defaultFlags
;
389 if ( m_chkInverse
->GetValue() )
391 flags
|= wxSL_INVERSE
;
394 if ( m_chkMinMaxLabels
->GetValue() )
396 flags
|= wxSL_MIN_MAX_LABELS
;
399 if ( m_chkValueLabel
->GetValue() )
401 flags
|= wxSL_VALUE_LABEL
;
404 if ( m_chkTicks
->GetValue() )
406 flags
|= wxSL_AUTOTICKS
;
409 // notice that the style names refer to the _ticks_ positions while we want
410 // to allow the user to select the label(s) positions and the labels are on
411 // the opposite side from the ticks, hence the apparent reversal below
412 switch ( m_radioSides
->GetSelection() )
414 case SliderTicks_None
:
417 case SliderTicks_Top
:
418 flags
|= wxSL_BOTTOM
;
421 case SliderTicks_Left
:
422 flags
|= wxSL_RIGHT
| wxSL_VERTICAL
;
425 case SliderTicks_Bottom
:
429 case SliderTicks_Right
:
430 flags
|= wxSL_LEFT
| wxSL_VERTICAL
;
434 wxFAIL_MSG(wxT("unexpected radiobox selection"));
438 if ( m_chkBothSides
->GetValue() )
446 int valOld
= m_slider
->GetValue();
447 if ( !IsValidValue(valOld
) )
452 m_sizerSlider
->Detach( m_slider
);
454 if ( m_sizerSlider
->GetChildren().GetCount() )
456 // we have 2 spacers, remove them too
457 m_sizerSlider
->Remove( 0 );
458 m_sizerSlider
->Remove( 0 );
464 m_slider
= new wxSlider(this, SliderPage_Slider
,
466 wxDefaultPosition
, wxDefaultSize
,
469 if ( m_slider
->HasFlag(wxSL_VERTICAL
) )
471 m_sizerSlider
->Add(0, 0, 1);
472 m_sizerSlider
->Add(m_slider
, 0, wxGROW
| wxALL
, 5);
473 m_sizerSlider
->Add(0, 0, 1);
477 m_sizerSlider
->Add(m_slider
, 1, wxCENTRE
| wxALL
, 5);
480 if ( m_chkTicks
->GetValue() )
485 m_sizerSlider
->Layout();
488 void SliderWidgetsPage::DoSetLineSize()
491 if ( !m_textLineSize
->GetValue().ToLong(&lineSize
) )
493 wxLogWarning(wxT("Invalid slider line size"));
498 m_slider
->SetLineSize(lineSize
);
500 if ( m_slider
->GetLineSize() != lineSize
)
502 wxLogWarning(wxT("Invalid line size in slider."));
506 void SliderWidgetsPage::DoSetPageSize()
509 if ( !m_textPageSize
->GetValue().ToLong(&pageSize
) )
511 wxLogWarning(wxT("Invalid slider page size"));
516 m_slider
->SetPageSize(pageSize
);
518 if ( m_slider
->GetPageSize() != pageSize
)
520 wxLogWarning(wxT("Invalid page size in slider."));
524 void SliderWidgetsPage::DoSetTickFreq()
527 if ( !m_textTickFreq
->GetValue().ToLong(&freq
) )
529 wxLogWarning(wxT("Invalid slider tick frequency"));
534 m_slider
->SetTickFreq(freq
, 0 /* unused */);
537 void SliderWidgetsPage::DoSetThumbLen()
540 if ( !m_textThumbLen
->GetValue().ToLong(&len
) )
542 wxLogWarning(wxT("Invalid slider thumb length"));
547 m_slider
->SetThumbLength(len
);
550 // ----------------------------------------------------------------------------
552 // ----------------------------------------------------------------------------
554 void SliderWidgetsPage::OnButtonReset(wxCommandEvent
& WXUNUSED(event
))
561 void SliderWidgetsPage::OnButtonSetLineSize(wxCommandEvent
& WXUNUSED(event
))
566 void SliderWidgetsPage::OnButtonSetPageSize(wxCommandEvent
& WXUNUSED(event
))
571 void SliderWidgetsPage::OnButtonSetTickFreq(wxCommandEvent
& WXUNUSED(event
))
576 void SliderWidgetsPage::OnButtonSetThumbLen(wxCommandEvent
& WXUNUSED(event
))
581 void SliderWidgetsPage::OnButtonSetMinAndMax(wxCommandEvent
& WXUNUSED(event
))
584 maxNew
= 0; // init to suppress compiler warning
585 if ( !m_textMin
->GetValue().ToLong(&minNew
) ||
586 !m_textMax
->GetValue().ToLong(&maxNew
) ||
589 wxLogWarning(wxT("Invalid min/max values for the slider."));
597 m_slider
->SetRange(minNew
, maxNew
);
599 if ( m_slider
->GetMin() != m_min
||
600 m_slider
->GetMax() != m_max
)
602 wxLogWarning(wxT("Invalid range in slider."));
606 void SliderWidgetsPage::OnButtonSetValue(wxCommandEvent
& WXUNUSED(event
))
609 if ( !m_textValue
->GetValue().ToLong(&val
) || !IsValidValue(val
) )
611 wxLogWarning(wxT("Invalid slider value."));
616 m_slider
->SetValue(val
);
619 void SliderWidgetsPage::OnUpdateUIValueButton(wxUpdateUIEvent
& event
)
622 event
.Enable( m_textValue
->GetValue().ToLong(&val
) && IsValidValue(val
) );
625 void SliderWidgetsPage::OnUpdateUILineSize(wxUpdateUIEvent
& event
)
628 event
.Enable( m_textLineSize
->GetValue().ToLong(&lineSize
) &&
629 (lineSize
> 0) && (lineSize
<= m_max
- m_min
) );
632 void SliderWidgetsPage::OnUpdateUIPageSize(wxUpdateUIEvent
& event
)
635 event
.Enable( m_textPageSize
->GetValue().ToLong(&pageSize
) &&
636 (pageSize
> 0) && (pageSize
<= m_max
- m_min
) );
639 void SliderWidgetsPage::OnUpdateUITickFreq(wxUpdateUIEvent
& event
)
642 event
.Enable( m_chkTicks
->GetValue() &&
643 m_textTickFreq
->GetValue().ToLong(&freq
) &&
644 (freq
> 0) && (freq
<= m_max
- m_min
) );
647 void SliderWidgetsPage::OnUpdateUIThumbLen(wxUpdateUIEvent
& event
)
650 event
.Enable( m_textThumbLen
->GetValue().ToLong(&val
));
653 void SliderWidgetsPage::OnUpdateUIMinMaxButton(wxUpdateUIEvent
& event
)
656 event
.Enable( m_textMin
->GetValue().ToLong(&mn
) &&
657 m_textMax
->GetValue().ToLong(&mx
) &&
661 void SliderWidgetsPage::OnUpdateUIResetButton(wxUpdateUIEvent
& event
)
663 event
.Enable( m_chkInverse
->GetValue() ||
664 !m_chkTicks
->GetValue() ||
665 !m_chkValueLabel
->GetValue() ||
666 !m_chkMinMaxLabels
->GetValue() ||
667 m_chkBothSides
->GetValue() ||
668 m_radioSides
->GetSelection() != SliderTicks_None
);
671 void SliderWidgetsPage::OnCheckOrRadioBox(wxCommandEvent
& WXUNUSED(event
))
676 void SliderWidgetsPage::OnUpdateUICurValueText(wxUpdateUIEvent
& event
)
678 event
.SetText( wxString::Format(wxT("%d"), m_slider
->GetValue()) );
681 void SliderWidgetsPage::OnUpdateUIRadioSides(wxUpdateUIEvent
& event
)
683 event
.Enable( m_chkValueLabel
->GetValue() || m_chkTicks
->GetValue() );
686 void SliderWidgetsPage::OnUpdateUIBothSides(wxUpdateUIEvent
& event
)
688 #if defined(__WXMSW__) || defined(__WXUNIVERSAL__)
689 event
.Enable( m_chkTicks
->GetValue() );
691 event
.Enable( false );
692 #endif // defined(__WXMSW__) || defined(__WXUNIVERSAL__)
695 void SliderWidgetsPage::OnSlider(wxScrollEvent
& event
)
697 wxASSERT_MSG( event
.GetInt() == m_slider
->GetValue(),
698 wxT("slider value should be the same") );
700 wxEventType eventType
= event
.GetEventType();
703 This array takes the EXACT order of the declarations in
705 (section "wxScrollBar and wxSlider event identifiers")
707 static const wxChar
*eventNames
[] =
709 wxT("wxEVT_SCROLL_TOP"),
710 wxT("wxEVT_SCROLL_BOTTOM"),
711 wxT("wxEVT_SCROLL_LINEUP"),
712 wxT("wxEVT_SCROLL_LINEDOWN"),
713 wxT("wxEVT_SCROLL_PAGEUP"),
714 wxT("wxEVT_SCROLL_PAGEDOWN"),
715 wxT("wxEVT_SCROLL_THUMBTRACK"),
716 wxT("wxEVT_SCROLL_THUMBRELEASE"),
717 wxT("wxEVT_SCROLL_CHANGED")
720 int index
= eventType
- wxEVT_SCROLL_TOP
;
723 If this assert is triggered, there is an unknown slider event which
724 should be added to the above eventNames array.
726 wxASSERT_MSG(index
>= 0 && (size_t)index
< WXSIZEOF(eventNames
),
727 wxT("Unknown slider event") );
730 static int s_numSliderEvents
= 0;
732 wxLogMessage(wxT("Slider event #%d: %s (pos = %d, int value = %d)"),
739 #endif // wxUSE_SLIDER