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
89 // ----------------------------------------------------------------------------
91 // ----------------------------------------------------------------------------
93 class SliderWidgetsPage
: public WidgetsPage
96 SliderWidgetsPage(WidgetsBookCtrl
*book
, wxImageList
*imaglist
);
97 virtual ~SliderWidgetsPage(){};
99 virtual wxControl
*GetWidget() const { return m_slider
; }
100 virtual void RecreateWidget() { CreateSlider(); }
102 // lazy creation of the content
103 virtual void CreateContent();
107 void OnButtonReset(wxCommandEvent
& event
);
108 void OnButtonClear(wxCommandEvent
& event
);
109 void OnButtonSetValue(wxCommandEvent
& event
);
110 void OnButtonSetMinAndMax(wxCommandEvent
& event
);
111 void OnButtonSetLineSize(wxCommandEvent
& event
);
112 void OnButtonSetPageSize(wxCommandEvent
& event
);
113 void OnButtonSetTickFreq(wxCommandEvent
& event
);
114 void OnButtonSetThumbLen(wxCommandEvent
& event
);
116 void OnCheckOrRadioBox(wxCommandEvent
& event
);
118 void OnSlider(wxScrollEvent
& event
);
120 void OnUpdateUIValueButton(wxUpdateUIEvent
& event
);
121 void OnUpdateUIMinMaxButton(wxUpdateUIEvent
& event
);
122 void OnUpdateUILineSize(wxUpdateUIEvent
& event
);
123 void OnUpdateUIPageSize(wxUpdateUIEvent
& event
);
124 void OnUpdateUITickFreq(wxUpdateUIEvent
& event
);
125 void OnUpdateUIThumbLen(wxUpdateUIEvent
& event
);
126 void OnUpdateUIRadioSides(wxUpdateUIEvent
& event
);
127 void OnUpdateUIBothSides(wxUpdateUIEvent
& event
);
129 void OnUpdateUIResetButton(wxUpdateUIEvent
& event
);
131 void OnUpdateUICurValueText(wxUpdateUIEvent
& event
);
133 // reset the slider parameters
136 // (re)create the slider
139 // set the line size from the text field value
140 void DoSetLineSize();
142 // set the page size from the text field value
143 void DoSetPageSize();
145 // set the tick frequency from the text field value
146 void DoSetTickFreq();
148 // set the thumb len from the text field value
149 void DoSetThumbLen();
151 // is this slider value in range?
152 bool IsValidValue(int val
) const
153 { return (val
>= m_min
) && (val
<= m_max
); }
161 // the check/radio boxes for styles
162 wxCheckBox
*m_chkLabels
,
167 wxRadioBox
*m_radioSides
;
169 // the slider itself and the sizer it is in
171 wxSizer
*m_sizerSlider
;
173 // the text entries for set value/range
174 wxTextCtrl
*m_textValue
,
183 DECLARE_EVENT_TABLE()
184 DECLARE_WIDGETS_PAGE(SliderWidgetsPage
)
187 // ----------------------------------------------------------------------------
189 // ----------------------------------------------------------------------------
191 BEGIN_EVENT_TABLE(SliderWidgetsPage
, WidgetsPage
)
192 EVT_BUTTON(SliderPage_Reset
, SliderWidgetsPage::OnButtonReset
)
193 EVT_BUTTON(SliderPage_SetValue
, SliderWidgetsPage::OnButtonSetValue
)
194 EVT_BUTTON(SliderPage_SetMinAndMax
, SliderWidgetsPage::OnButtonSetMinAndMax
)
195 EVT_BUTTON(SliderPage_SetLineSize
, SliderWidgetsPage::OnButtonSetLineSize
)
196 EVT_BUTTON(SliderPage_SetPageSize
, SliderWidgetsPage::OnButtonSetPageSize
)
197 EVT_BUTTON(SliderPage_SetTickFreq
, SliderWidgetsPage::OnButtonSetTickFreq
)
198 EVT_BUTTON(SliderPage_SetThumbLen
, SliderWidgetsPage::OnButtonSetThumbLen
)
200 EVT_UPDATE_UI(SliderPage_SetValue
, SliderWidgetsPage::OnUpdateUIValueButton
)
201 EVT_UPDATE_UI(SliderPage_SetMinAndMax
, SliderWidgetsPage::OnUpdateUIMinMaxButton
)
202 EVT_UPDATE_UI(SliderPage_SetLineSize
, SliderWidgetsPage::OnUpdateUILineSize
)
203 EVT_UPDATE_UI(SliderPage_SetPageSize
, SliderWidgetsPage::OnUpdateUIPageSize
)
204 EVT_UPDATE_UI(SliderPage_SetTickFreq
, SliderWidgetsPage::OnUpdateUITickFreq
)
205 EVT_UPDATE_UI(SliderPage_SetThumbLen
, SliderWidgetsPage::OnUpdateUIThumbLen
)
206 EVT_UPDATE_UI(SliderPage_RadioSides
, SliderWidgetsPage::OnUpdateUIRadioSides
)
207 EVT_UPDATE_UI(SliderPage_BothSides
, SliderWidgetsPage::OnUpdateUIBothSides
)
209 EVT_UPDATE_UI(SliderPage_Reset
, SliderWidgetsPage::OnUpdateUIResetButton
)
211 EVT_UPDATE_UI(SliderPage_CurValueText
, SliderWidgetsPage::OnUpdateUICurValueText
)
213 EVT_COMMAND_SCROLL(SliderPage_Slider
, SliderWidgetsPage::OnSlider
)
215 EVT_CHECKBOX(wxID_ANY
, SliderWidgetsPage::OnCheckOrRadioBox
)
216 EVT_RADIOBOX(wxID_ANY
, SliderWidgetsPage::OnCheckOrRadioBox
)
219 // ============================================================================
221 // ============================================================================
223 #if defined(__WXUNIVERSAL__)
224 #define FAMILY_CTRLS UNIVERSAL_CTRLS
226 #define FAMILY_CTRLS NATIVE_CTRLS
229 IMPLEMENT_WIDGETS_PAGE(SliderWidgetsPage
, _T("Slider"), FAMILY_CTRLS
);
231 SliderWidgetsPage::SliderWidgetsPage(WidgetsBookCtrl
*book
,
232 wxImageList
*imaglist
)
233 : WidgetsPage(book
, imaglist
, slider_xpm
)
242 m_chkBothSides
= (wxCheckBox
*)NULL
;
244 m_radioSides
= (wxRadioBox
*)NULL
;
246 m_slider
= (wxSlider
*)NULL
;
247 m_sizerSlider
= (wxSizer
*)NULL
;
250 void SliderWidgetsPage::CreateContent()
252 wxSizer
*sizerTop
= new wxBoxSizer(wxHORIZONTAL
);
255 wxStaticBox
*box
= new wxStaticBox(this, wxID_ANY
, _T("&Set style"));
256 wxSizer
*sizerLeft
= new wxStaticBoxSizer(box
, wxVERTICAL
);
258 m_chkInverse
= CreateCheckBoxAndAddToSizer(sizerLeft
, _T("&Inverse"));
259 m_chkTicks
= CreateCheckBoxAndAddToSizer(sizerLeft
, _T("Show &ticks"));
260 m_chkLabels
= CreateCheckBoxAndAddToSizer(sizerLeft
, _T("Show &labels"));
261 static const wxString sides
[] =
268 m_radioSides
= new wxRadioBox(this, SliderPage_RadioSides
, _T("&Ticks/Labels"),
269 wxDefaultPosition
, wxDefaultSize
,
270 WXSIZEOF(sides
), sides
,
271 1, wxRA_SPECIFY_COLS
);
272 sizerLeft
->Add(m_radioSides
, 0, wxGROW
| wxALL
, 5);
273 m_chkBothSides
= CreateCheckBoxAndAddToSizer
274 (sizerLeft
, _T("&Both sides"), SliderPage_BothSides
);
276 m_chkBothSides
->SetToolTip( _T("\"Both sides\" is only supported \nin Win95 and Universal") );
277 #endif // wxUSE_TOOLTIPS
279 sizerLeft
->Add(5, 5, 0, wxGROW
| wxALL
, 5); // spacer
281 wxButton
*btn
= new wxButton(this, SliderPage_Reset
, _T("&Reset"));
282 sizerLeft
->Add(btn
, 0, wxALIGN_CENTRE_HORIZONTAL
| wxALL
, 15);
285 wxStaticBox
*box2
= new wxStaticBox(this, wxID_ANY
, _T("&Change slider value"));
286 wxSizer
*sizerMiddle
= new wxStaticBoxSizer(box2
, wxVERTICAL
);
289 wxSizer
*sizerRow
= CreateSizerWithTextAndLabel(_T("Current value"),
290 SliderPage_CurValueText
,
292 text
->SetEditable(false);
294 sizerMiddle
->Add(sizerRow
, 0, wxALL
| wxGROW
, 5);
296 sizerRow
= CreateSizerWithTextAndButton(SliderPage_SetValue
,
298 SliderPage_ValueText
,
300 sizerMiddle
->Add(sizerRow
, 0, wxALL
| wxGROW
, 5);
302 sizerRow
= CreateSizerWithTextAndButton(SliderPage_SetMinAndMax
,
307 m_textMax
= new wxTextCtrl(this, SliderPage_MaxText
, wxEmptyString
);
308 sizerRow
->Add(m_textMax
, 1, wxLEFT
| wxALIGN_CENTRE_VERTICAL
, 5);
310 m_textMin
->SetValue( wxString::Format(_T("%d"), m_min
) );
311 m_textMax
->SetValue( wxString::Format(_T("%d"), m_max
) );
313 sizerMiddle
->Add(sizerRow
, 0, wxALL
| wxGROW
, 5);
315 sizerRow
= CreateSizerWithTextAndButton(SliderPage_SetLineSize
,
317 SliderPage_LineSizeText
,
320 sizerMiddle
->Add(sizerRow
, 0, wxALL
| wxGROW
, 5);
322 sizerRow
= CreateSizerWithTextAndButton(SliderPage_SetPageSize
,
324 SliderPage_PageSizeText
,
327 sizerMiddle
->Add(sizerRow
, 0, wxALL
| wxGROW
, 5);
329 sizerRow
= CreateSizerWithTextAndButton(SliderPage_SetTickFreq
,
330 _T("Tick &frequency"),
331 SliderPage_TickFreqText
,
334 m_textTickFreq
->SetValue(_T("10"));
336 sizerMiddle
->Add(sizerRow
, 0, wxALL
| wxGROW
, 5);
338 sizerRow
= CreateSizerWithTextAndButton(SliderPage_SetThumbLen
,
340 SliderPage_ThumbLenText
,
343 sizerMiddle
->Add(sizerRow
, 0, wxALL
| wxGROW
, 5);
346 wxSizer
*sizerRight
= new wxBoxSizer(wxHORIZONTAL
);
347 sizerRight
->SetMinSize(150, 40);
348 m_sizerSlider
= sizerRight
; // save it to modify it later
353 m_textLineSize
->SetValue(wxString::Format(_T("%d"), m_slider
->GetLineSize()));
354 m_textPageSize
->SetValue(wxString::Format(_T("%d"), m_slider
->GetPageSize()));
356 // the 3 panes panes compose the window
357 sizerTop
->Add(sizerLeft
, 0, wxGROW
| (wxALL
& ~wxLEFT
), 10);
358 sizerTop
->Add(sizerMiddle
, 0, wxGROW
| wxALL
, 10);
359 sizerTop
->Add(sizerRight
, 1, wxGROW
| (wxALL
& ~wxRIGHT
), 10);
361 // final initializations
367 // ----------------------------------------------------------------------------
369 // ----------------------------------------------------------------------------
371 void SliderWidgetsPage::Reset()
373 m_chkInverse
->SetValue(false);
374 m_chkTicks
->SetValue(true);
375 m_chkLabels
->SetValue(true);
376 m_chkBothSides
->SetValue(false);
378 m_radioSides
->SetSelection(SliderTicks_Top
);
381 void SliderWidgetsPage::CreateSlider()
383 int flags
= ms_defaultFlags
;
385 if ( m_chkInverse
->GetValue() )
387 flags
|= wxSL_INVERSE
;
390 if ( m_chkLabels
->GetValue() )
392 flags
|= wxSL_LABELS
;
395 if ( m_chkTicks
->GetValue() )
397 flags
|= wxSL_AUTOTICKS
;
400 switch ( m_radioSides
->GetSelection() )
402 case SliderTicks_Top
:
406 case SliderTicks_Left
:
410 case SliderTicks_Bottom
:
411 flags
|= wxSL_BOTTOM
;
414 case SliderTicks_Right
:
419 wxFAIL_MSG(_T("unexpected radiobox selection"));
423 if ( m_chkBothSides
->GetValue() )
431 int valOld
= m_slider
->GetValue();
432 if ( !IsValidValue(valOld
) )
437 m_sizerSlider
->Detach( m_slider
);
439 if ( m_sizerSlider
->GetChildren().GetCount() )
441 // we have 2 spacers, remove them too
442 m_sizerSlider
->Remove( 0 );
443 m_sizerSlider
->Remove( 0 );
449 m_slider
= new wxSlider(this, SliderPage_Slider
,
451 wxDefaultPosition
, wxDefaultSize
,
454 if ( m_slider
->HasFlag(wxSL_VERTICAL
) )
456 m_sizerSlider
->Add(0, 0, 1);
457 m_sizerSlider
->Add(m_slider
, 0, wxGROW
| wxALL
, 5);
458 m_sizerSlider
->Add(0, 0, 1);
462 m_sizerSlider
->Add(m_slider
, 1, wxCENTRE
| wxALL
, 5);
465 if ( m_chkTicks
->GetValue() )
470 m_sizerSlider
->Layout();
473 void SliderWidgetsPage::DoSetLineSize()
476 if ( !m_textLineSize
->GetValue().ToLong(&lineSize
) )
478 wxLogWarning(_T("Invalid slider line size"));
483 m_slider
->SetLineSize(lineSize
);
485 if ( m_slider
->GetLineSize() != lineSize
)
487 wxLogWarning(_T("Invalid line size in slider."));
491 void SliderWidgetsPage::DoSetPageSize()
494 if ( !m_textPageSize
->GetValue().ToLong(&pageSize
) )
496 wxLogWarning(_T("Invalid slider page size"));
501 m_slider
->SetPageSize(pageSize
);
503 if ( m_slider
->GetPageSize() != pageSize
)
505 wxLogWarning(_T("Invalid page size in slider."));
509 void SliderWidgetsPage::DoSetTickFreq()
512 if ( !m_textTickFreq
->GetValue().ToLong(&freq
) )
514 wxLogWarning(_T("Invalid slider tick frequency"));
519 m_slider
->SetTickFreq(freq
, 0 /* unused */);
522 void SliderWidgetsPage::DoSetThumbLen()
525 if ( !m_textThumbLen
->GetValue().ToLong(&len
) )
527 wxLogWarning(_T("Invalid slider thumb length"));
532 m_slider
->SetThumbLength(len
);
535 // ----------------------------------------------------------------------------
537 // ----------------------------------------------------------------------------
539 void SliderWidgetsPage::OnButtonReset(wxCommandEvent
& WXUNUSED(event
))
546 void SliderWidgetsPage::OnButtonSetLineSize(wxCommandEvent
& WXUNUSED(event
))
551 void SliderWidgetsPage::OnButtonSetPageSize(wxCommandEvent
& WXUNUSED(event
))
556 void SliderWidgetsPage::OnButtonSetTickFreq(wxCommandEvent
& WXUNUSED(event
))
561 void SliderWidgetsPage::OnButtonSetThumbLen(wxCommandEvent
& WXUNUSED(event
))
566 void SliderWidgetsPage::OnButtonSetMinAndMax(wxCommandEvent
& WXUNUSED(event
))
569 maxNew
= 0; // init to suppress compiler warning
570 if ( !m_textMin
->GetValue().ToLong(&minNew
) ||
571 !m_textMax
->GetValue().ToLong(&maxNew
) ||
574 wxLogWarning(_T("Invalid min/max values for the slider."));
582 m_slider
->SetRange(minNew
, maxNew
);
584 if ( m_slider
->GetMin() != m_min
||
585 m_slider
->GetMax() != m_max
)
587 wxLogWarning(_T("Invalid range in slider."));
591 void SliderWidgetsPage::OnButtonSetValue(wxCommandEvent
& WXUNUSED(event
))
594 if ( !m_textValue
->GetValue().ToLong(&val
) || !IsValidValue(val
) )
596 wxLogWarning(_T("Invalid slider value."));
601 m_slider
->SetValue(val
);
604 void SliderWidgetsPage::OnUpdateUIValueButton(wxUpdateUIEvent
& event
)
607 event
.Enable( m_textValue
->GetValue().ToLong(&val
) && IsValidValue(val
) );
610 void SliderWidgetsPage::OnUpdateUILineSize(wxUpdateUIEvent
& event
)
613 event
.Enable( m_textLineSize
->GetValue().ToLong(&lineSize
) &&
614 (lineSize
> 0) && (lineSize
<= m_max
- m_min
) );
617 void SliderWidgetsPage::OnUpdateUIPageSize(wxUpdateUIEvent
& event
)
620 event
.Enable( m_textPageSize
->GetValue().ToLong(&pageSize
) &&
621 (pageSize
> 0) && (pageSize
<= m_max
- m_min
) );
624 void SliderWidgetsPage::OnUpdateUITickFreq(wxUpdateUIEvent
& event
)
627 event
.Enable( m_chkTicks
->GetValue() &&
628 m_textTickFreq
->GetValue().ToLong(&freq
) &&
629 (freq
> 0) && (freq
<= m_max
- m_min
) );
632 void SliderWidgetsPage::OnUpdateUIThumbLen(wxUpdateUIEvent
& event
)
635 event
.Enable( m_textThumbLen
->GetValue().ToLong(&val
));
638 void SliderWidgetsPage::OnUpdateUIMinMaxButton(wxUpdateUIEvent
& event
)
641 event
.Enable( m_textMin
->GetValue().ToLong(&mn
) &&
642 m_textMax
->GetValue().ToLong(&mx
) &&
646 void SliderWidgetsPage::OnUpdateUIResetButton(wxUpdateUIEvent
& event
)
648 event
.Enable( m_chkInverse
->GetValue() ||
649 !m_chkTicks
->GetValue() ||
650 !m_chkLabels
->GetValue() ||
651 m_chkBothSides
->GetValue() ||
652 m_radioSides
->GetSelection() != SliderTicks_Top
);
655 void SliderWidgetsPage::OnCheckOrRadioBox(wxCommandEvent
& WXUNUSED(event
))
660 void SliderWidgetsPage::OnUpdateUICurValueText(wxUpdateUIEvent
& event
)
662 event
.SetText( wxString::Format(_T("%d"), m_slider
->GetValue()) );
665 void SliderWidgetsPage::OnUpdateUIRadioSides(wxUpdateUIEvent
& event
)
667 event
.Enable( m_chkLabels
->GetValue() || m_chkTicks
->GetValue() );
670 void SliderWidgetsPage::OnUpdateUIBothSides(wxUpdateUIEvent
& event
)
672 #if defined(__WXMSW__) || defined(__WXUNIVERSAL__)
673 event
.Enable( m_chkTicks
->GetValue() );
675 event
.Enable( false );
676 #endif // defined(__WXMSW__) || defined(__WXUNIVERSAL__)
679 void SliderWidgetsPage::OnSlider(wxScrollEvent
& event
)
681 wxASSERT_MSG( event
.GetInt() == m_slider
->GetValue(),
682 wxT("slider value should be the same") );
684 wxEventType eventType
= event
.GetEventType();
687 This array takes the EXACT order of the declarations in
689 (section "wxScrollBar and wxSlider event identifiers")
691 static const wxChar
*eventNames
[] =
693 wxT("wxEVT_SCROLL_TOP"),
694 wxT("wxEVT_SCROLL_BOTTOM"),
695 wxT("wxEVT_SCROLL_LINEUP"),
696 wxT("wxEVT_SCROLL_LINEDOWN"),
697 wxT("wxEVT_SCROLL_PAGEUP"),
698 wxT("wxEVT_SCROLL_PAGEDOWN"),
699 wxT("wxEVT_SCROLL_THUMBTRACK"),
700 wxT("wxEVT_SCROLL_THUMBRELEASE"),
701 wxT("wxEVT_SCROLL_CHANGED")
704 int index
= eventType
- wxEVT_SCROLL_TOP
;
707 If this assert is triggered, there is an unknown slider event which
708 should be added to the above eventNames array.
710 wxASSERT_MSG(index
>= 0 && (size_t)index
< WXSIZEOF(eventNames
),
711 wxT("Unknown slider event") );
714 static int s_numSliderEvents
= 0;
716 wxLogMessage(wxT("Slider event #%d: %s (pos = %d, int value = %d)"),
723 #endif // wxUSE_SLIDER