]> git.saurik.com Git - wxWidgets.git/blame - samples/widgets/slider.cpp
Use #if wxUSE_GAUGE instead of #if 1
[wxWidgets.git] / samples / widgets / slider.cpp
CommitLineData
32b8ec41
VZ
1/////////////////////////////////////////////////////////////////////////////
2// Program: wxWindows Widgets Sample
3// Name: slider.cpp
4// Purpose: Part of the widgets sample showing wxSlider
5// Author: Vadim Zeitlin
6// Created: 16.04.01
7// Id: $Id$
8// Copyright: (c) 2001 Vadim Zeitlin
9// License: wxWindows license
10/////////////////////////////////////////////////////////////////////////////
11
12// ============================================================================
13// declarations
14// ============================================================================
15
16// ----------------------------------------------------------------------------
17// headers
18// ----------------------------------------------------------------------------
19
20// for compilers that support precompilation, includes "wx/wx.h".
21#include "wx/wxprec.h"
22
23#ifdef __BORLANDC__
24 #pragma hdrstop
25#endif
26
27// for all others, include the necessary headers
28#ifndef WX_PRECOMP
29 #include "wx/log.h"
30
3bb70c40 31 #include "wx/bitmap.h"
32b8ec41
VZ
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"
38#endif
39
53b98211
JS
40#if wxUSE_TOOLTIPS
41 #include "wx/tooltip.h"
42#endif
43
32b8ec41
VZ
44#include "wx/sizer.h"
45
46#include "widgets.h"
32b8ec41
VZ
47#include "icons/slider.xpm"
48
49// ----------------------------------------------------------------------------
50// constants
51// ----------------------------------------------------------------------------
52
53// control ids
54enum
55{
d79b005a 56 SliderPage_Reset = wxID_HIGHEST,
32b8ec41
VZ
57 SliderPage_Clear,
58 SliderPage_SetValue,
59 SliderPage_SetMinAndMax,
60 SliderPage_SetTickFreq,
53b98211 61 SliderPage_SetThumbLen,
32b8ec41
VZ
62 SliderPage_CurValueText,
63 SliderPage_ValueText,
64 SliderPage_MinText,
65 SliderPage_MaxText,
66 SliderPage_TickFreqText,
53b98211
JS
67 SliderPage_ThumbLenText,
68 SliderPage_RadioSides,
69 SliderPage_BothSides,
32b8ec41
VZ
70 SliderPage_Slider
71};
72
53b98211
JS
73// sides radiobox values
74enum
75{
76 StaticSides_Top,
77 StaticSides_Bottom,
78 StaticSides_Left,
79 StaticSides_Right
80};
81
32b8ec41
VZ
82// ----------------------------------------------------------------------------
83// SliderWidgetsPage
84// ----------------------------------------------------------------------------
85
86class SliderWidgetsPage : public WidgetsPage
87{
88public:
89 SliderWidgetsPage(wxNotebook *notebook, wxImageList *imaglist);
90 virtual ~SliderWidgetsPage();
91
92protected:
93 // event handlers
94 void OnButtonReset(wxCommandEvent& event);
95 void OnButtonClear(wxCommandEvent& event);
96 void OnButtonSetValue(wxCommandEvent& event);
97 void OnButtonSetMinAndMax(wxCommandEvent& event);
98 void OnButtonSetTickFreq(wxCommandEvent& event);
53b98211 99 void OnButtonSetThumbLen(wxCommandEvent& event);
32b8ec41
VZ
100
101 void OnCheckOrRadioBox(wxCommandEvent& event);
102
d79b005a 103 void OnSlider(wxScrollEvent& event);
32b8ec41 104
32b8ec41
VZ
105 void OnUpdateUIValueButton(wxUpdateUIEvent& event);
106 void OnUpdateUIMinMaxButton(wxUpdateUIEvent& event);
107 void OnUpdateUITickFreq(wxUpdateUIEvent& event);
53b98211
JS
108 void OnUpdateUIThumbLen(wxUpdateUIEvent& event);
109 void OnUpdateUIRadioSides(wxUpdateUIEvent& event);
110 void OnUpdateUIBothSides(wxUpdateUIEvent& event);
32b8ec41
VZ
111
112 void OnUpdateUIResetButton(wxUpdateUIEvent& event);
113
114 void OnUpdateUICurValueText(wxUpdateUIEvent& event);
115
116 // reset the slider parameters
117 void Reset();
118
119 // (re)create the slider
120 void CreateSlider();
121
122 // set the tick frequency from the text field value
123 void DoSetTickFreq();
124
53b98211
JS
125 // set the thumb len from the text field value
126 void DoSetThumbLen();
127
32b8ec41
VZ
128 // is this slider value in range?
129 bool IsValidValue(int val) const
130 { return (val >= m_min) && (val <= m_max); }
131
132 // the slider range
133 int m_min, m_max;
134
135 // the controls
136 // ------------
137
138 // the check/radio boxes for styles
139 wxCheckBox *m_chkLabels,
32b8ec41 140 *m_chkVert,
53b98211
JS
141 *m_chkTicks,
142 *m_chkBothSides;
143
144 wxRadioBox *m_radioSides;
32b8ec41
VZ
145
146 // the slider itself and the sizer it is in
147 wxSlider *m_slider;
148 wxSizer *m_sizerSlider;
149
150 // the text entries for set value/range
151 wxTextCtrl *m_textValue,
152 *m_textMin,
153 *m_textMax,
53b98211
JS
154 *m_textTickFreq,
155 *m_textThumbLen;
32b8ec41
VZ
156
157private:
5e173f35
GD
158 DECLARE_EVENT_TABLE()
159 DECLARE_WIDGETS_PAGE(SliderWidgetsPage)
32b8ec41
VZ
160};
161
162// ----------------------------------------------------------------------------
163// event tables
164// ----------------------------------------------------------------------------
165
166BEGIN_EVENT_TABLE(SliderWidgetsPage, WidgetsPage)
167 EVT_BUTTON(SliderPage_Reset, SliderWidgetsPage::OnButtonReset)
168 EVT_BUTTON(SliderPage_SetValue, SliderWidgetsPage::OnButtonSetValue)
169 EVT_BUTTON(SliderPage_SetMinAndMax, SliderWidgetsPage::OnButtonSetMinAndMax)
170 EVT_BUTTON(SliderPage_SetTickFreq, SliderWidgetsPage::OnButtonSetTickFreq)
53b98211 171 EVT_BUTTON(SliderPage_SetThumbLen, SliderWidgetsPage::OnButtonSetThumbLen)
32b8ec41
VZ
172
173 EVT_UPDATE_UI(SliderPage_SetValue, SliderWidgetsPage::OnUpdateUIValueButton)
174 EVT_UPDATE_UI(SliderPage_SetMinAndMax, SliderWidgetsPage::OnUpdateUIMinMaxButton)
175 EVT_UPDATE_UI(SliderPage_SetTickFreq, SliderWidgetsPage::OnUpdateUITickFreq)
53b98211 176 EVT_UPDATE_UI(SliderPage_SetThumbLen, SliderWidgetsPage::OnUpdateUIThumbLen)
32b8ec41 177 EVT_UPDATE_UI(SliderPage_TickFreqText, SliderWidgetsPage::OnUpdateUITickFreq)
53b98211
JS
178 EVT_UPDATE_UI(SliderPage_RadioSides, SliderWidgetsPage::OnUpdateUIRadioSides)
179 EVT_UPDATE_UI(SliderPage_BothSides, SliderWidgetsPage::OnUpdateUIBothSides)
32b8ec41
VZ
180
181 EVT_UPDATE_UI(SliderPage_Reset, SliderWidgetsPage::OnUpdateUIResetButton)
182
183 EVT_UPDATE_UI(SliderPage_CurValueText, SliderWidgetsPage::OnUpdateUICurValueText)
184
d79b005a 185 EVT_COMMAND_SCROLL(SliderPage_Slider, SliderWidgetsPage::OnSlider)
32b8ec41
VZ
186
187 EVT_CHECKBOX(-1, SliderWidgetsPage::OnCheckOrRadioBox)
188 EVT_RADIOBOX(-1, SliderWidgetsPage::OnCheckOrRadioBox)
189END_EVENT_TABLE()
190
191// ============================================================================
192// implementation
193// ============================================================================
194
195IMPLEMENT_WIDGETS_PAGE(SliderWidgetsPage, _T("Slider"));
196
197SliderWidgetsPage::SliderWidgetsPage(wxNotebook *notebook,
198 wxImageList *imaglist)
199 : WidgetsPage(notebook)
200{
201 imaglist->Add(wxBitmap(slider_xpm));
202
203 // init everything
204 m_min = 0;
205 m_max = 100;
206
207 m_chkVert =
208 m_chkTicks =
209 m_chkLabels =
53b98211
JS
210 m_chkBothSides = (wxCheckBox *)NULL;
211
212 m_radioSides = (wxRadioBox *)NULL;
32b8ec41
VZ
213
214 m_slider = (wxSlider *)NULL;
215 m_sizerSlider = (wxSizer *)NULL;
216
217 wxSizer *sizerTop = new wxBoxSizer(wxHORIZONTAL);
218
219 // left pane
220 wxStaticBox *box = new wxStaticBox(this, -1, _T("&Set style"));
221 wxSizer *sizerLeft = new wxStaticBoxSizer(box, wxVERTICAL);
222
223 m_chkVert = CreateCheckBoxAndAddToSizer(sizerLeft, _T("&Vertical"));
224 m_chkTicks = CreateCheckBoxAndAddToSizer(sizerLeft, _T("Show &ticks"));
225 m_chkLabels = CreateCheckBoxAndAddToSizer(sizerLeft, _T("Show &labels"));
53b98211
JS
226 static const wxString sides[] =
227 {
228 _T("top"),
229 _T("bottom"),
230 _T("left"),
231 _T("right"),
232 };
233 m_radioSides = new wxRadioBox(this, SliderPage_RadioSides, _T("&Ticks/Labels"),
234 wxDefaultPosition, wxDefaultSize,
235 WXSIZEOF(sides), sides,
236 1, wxRA_SPECIFY_COLS);
237 sizerLeft->Add(m_radioSides, 0, wxGROW | wxALL, 5);
238 m_chkBothSides = CreateCheckBoxAndAddToSizer
239 (sizerLeft, _T("&Both sides"), SliderPage_BothSides);
240#if wxUSE_TOOLTIPS
241 m_chkBothSides->SetToolTip( _T("\"Both sides\" is only supported \nin Win95 and Universal") );
242#endif // wxUSE_TOOLTIPS
32b8ec41
VZ
243
244 sizerLeft->Add(5, 5, 0, wxGROW | wxALL, 5); // spacer
245
246 wxButton *btn = new wxButton(this, SliderPage_Reset, _T("&Reset"));
247 sizerLeft->Add(btn, 0, wxALIGN_CENTRE_HORIZONTAL | wxALL, 15);
248
249 // middle pane
250 wxStaticBox *box2 = new wxStaticBox(this, -1, _T("&Change slider value"));
251 wxSizer *sizerMiddle = new wxStaticBoxSizer(box2, wxVERTICAL);
252
253 wxTextCtrl *text;
254 wxSizer *sizerRow = CreateSizerWithTextAndLabel(_T("Current value"),
255 SliderPage_CurValueText,
256 &text);
257 text->SetEditable(FALSE);
258
259 sizerMiddle->Add(sizerRow, 0, wxALL | wxGROW, 5);
260
261 sizerRow = CreateSizerWithTextAndButton(SliderPage_SetValue,
262 _T("Set &value"),
263 SliderPage_ValueText,
264 &m_textValue);
265 sizerMiddle->Add(sizerRow, 0, wxALL | wxGROW, 5);
266
267 sizerRow = CreateSizerWithTextAndButton(SliderPage_SetMinAndMax,
268 _T("&Min and max"),
269 SliderPage_MinText,
270 &m_textMin);
271
272 m_textMax = new wxTextCtrl(this, SliderPage_MaxText, _T(""));
273 sizerRow->Add(m_textMax, 1, wxLEFT | wxALIGN_CENTRE_VERTICAL, 5);
274
aec18ff7
MB
275 m_textMin->SetValue( wxString::Format(_T("%d"), m_min) );
276 m_textMax->SetValue( wxString::Format(_T("%d"), m_max) );
32b8ec41
VZ
277
278 sizerMiddle->Add(sizerRow, 0, wxALL | wxGROW, 5);
279
280 sizerRow = CreateSizerWithTextAndButton(SliderPage_SetTickFreq,
281 _T("Tick &frequency"),
282 SliderPage_TickFreqText,
283 &m_textTickFreq);
284
285 m_textTickFreq->SetValue(_T("10"));
286
287 sizerMiddle->Add(sizerRow, 0, wxALL | wxGROW, 5);
288
53b98211
JS
289 sizerRow = CreateSizerWithTextAndButton(SliderPage_SetThumbLen,
290 _T("Thumb &lenght"),
291 SliderPage_ThumbLenText,
292 &m_textThumbLen);
293
294 sizerMiddle->Add(sizerRow, 0, wxALL | wxGROW, 5);
295
32b8ec41
VZ
296 // right pane
297 wxSizer *sizerRight = new wxBoxSizer(wxHORIZONTAL);
53b98211 298 sizerRight->SetMinSize(150, 40);
32b8ec41
VZ
299 m_sizerSlider = sizerRight; // save it to modify it later
300
301 Reset();
302 CreateSlider();
303
304 // the 3 panes panes compose the window
305 sizerTop->Add(sizerLeft, 0, wxGROW | (wxALL & ~wxLEFT), 10);
7b127900 306 sizerTop->Add(sizerMiddle, 0, wxGROW | wxALL, 10);
32b8ec41
VZ
307 sizerTop->Add(sizerRight, 1, wxGROW | (wxALL & ~wxRIGHT), 10);
308
309 // final initializations
310 SetAutoLayout(TRUE);
311 SetSizer(sizerTop);
312
313 sizerTop->Fit(this);
314}
315
316SliderWidgetsPage::~SliderWidgetsPage()
317{
318}
319
320// ----------------------------------------------------------------------------
321// operations
322// ----------------------------------------------------------------------------
323
324void SliderWidgetsPage::Reset()
325{
32b8ec41 326 m_chkVert->SetValue(FALSE);
53b98211
JS
327 m_chkTicks->SetValue(TRUE);
328 m_chkLabels->SetValue(TRUE);
329 m_chkBothSides->SetValue(FALSE);
330
331 m_radioSides->SetSelection(StaticSides_Top);
32b8ec41
VZ
332}
333
334void SliderWidgetsPage::CreateSlider()
335{
336 int flags = 0;
337
338 bool isVert = m_chkVert->GetValue();
339 if ( isVert )
340 flags |= wxSL_VERTICAL;
341 else
342 flags |= wxSL_HORIZONTAL;
343
344 if ( m_chkLabels->GetValue() )
345 {
346 flags |= wxSL_LABELS;
32b8ec41
VZ
347 }
348
349 if ( m_chkTicks->GetValue() )
350 {
351 flags |= wxSL_AUTOTICKS;
352 }
353
53b98211
JS
354 switch ( m_radioSides->GetSelection() )
355 {
356 case StaticSides_Top:
357 flags |= wxSL_TOP;
358 break;
359 case StaticSides_Left:
360 flags |= wxSL_LEFT;
361 break;
362 case StaticSides_Bottom:
363 flags |= wxSL_BOTTOM;
364 break;
365 case StaticSides_Right:
366 flags |= wxSL_RIGHT;
367 break;
368 default:
369 wxFAIL_MSG(_T("unexpected radiobox selection"));
370 // fall through
371 }
372
373 if ( m_chkBothSides->GetValue() )
374 {
375 flags |= wxSL_BOTH;
376 }
377
32b8ec41
VZ
378 int val = m_min;
379 if ( m_slider )
380 {
381 int valOld = m_slider->GetValue();
382 if ( !IsValidValue(valOld) )
383 {
384 val = valOld;
385 }
386
12a3f227 387 m_sizerSlider->Detach( m_slider );
32b8ec41
VZ
388
389 if ( m_sizerSlider->GetChildren().GetCount() )
390 {
391 // we have 2 spacers, remove them too
9dd96c0f
VZ
392 m_sizerSlider->Remove( 0 );
393 m_sizerSlider->Remove( 0 );
32b8ec41
VZ
394 }
395
396 delete m_slider;
397 }
398
399 m_slider = new wxSlider(this, SliderPage_Slider,
400 val, m_min, m_max,
401 wxDefaultPosition, wxDefaultSize,
402 flags);
403
404 if ( isVert )
405 {
406 m_sizerSlider->Add(0, 0, 1);
407 m_sizerSlider->Add(m_slider, 0, wxGROW | wxALL, 5);
408 m_sizerSlider->Add(0, 0, 1);
409 }
410 else
411 {
412 m_sizerSlider->Add(m_slider, 1, wxCENTRE | wxALL, 5);
413 }
414
415 if ( m_chkTicks->GetValue() )
416 {
417 DoSetTickFreq();
418 }
419
420 m_sizerSlider->Layout();
421}
422
423void SliderWidgetsPage::DoSetTickFreq()
424{
425 long freq;
426 if ( !m_textTickFreq->GetValue().ToLong(&freq) )
427 {
428 wxLogWarning(_T("Invalid slider tick frequency"));
429
430 return;
431 }
432
433 m_slider->SetTickFreq(freq, 0 /* unused */);
434}
435
53b98211
JS
436void SliderWidgetsPage::DoSetThumbLen()
437{
438 long len;
439 if ( !m_textThumbLen->GetValue().ToLong(&len) )
440 {
441 wxLogWarning(_T("Invalid slider thumb lenght"));
442
443 return;
444 }
445
446 m_slider->SetThumbLength(len);
447}
448
32b8ec41
VZ
449// ----------------------------------------------------------------------------
450// event handlers
451// ----------------------------------------------------------------------------
452
453void SliderWidgetsPage::OnButtonReset(wxCommandEvent& WXUNUSED(event))
454{
455 Reset();
456
457 CreateSlider();
458}
459
460void SliderWidgetsPage::OnButtonSetTickFreq(wxCommandEvent& WXUNUSED(event))
461{
462 DoSetTickFreq();
463}
464
53b98211
JS
465void SliderWidgetsPage::OnButtonSetThumbLen(wxCommandEvent& WXUNUSED(event))
466{
467 DoSetThumbLen();
468}
469
32b8ec41
VZ
470void SliderWidgetsPage::OnButtonSetMinAndMax(wxCommandEvent& WXUNUSED(event))
471{
472 long minNew,
473 maxNew = 0; // init to suppress compiler warning
474 if ( !m_textMin->GetValue().ToLong(&minNew) ||
475 !m_textMax->GetValue().ToLong(&maxNew) ||
476 minNew >= maxNew )
477 {
478 wxLogWarning(_T("Invalid min/max values for the slider."));
479
480 return;
481 }
482
483 m_min = minNew;
484 m_max = maxNew;
485
486 m_slider->SetRange(minNew, maxNew);
487}
488
489void SliderWidgetsPage::OnButtonSetValue(wxCommandEvent& WXUNUSED(event))
490{
491 long val;
492 if ( !m_textValue->GetValue().ToLong(&val) || !IsValidValue(val) )
493 {
494 wxLogWarning(_T("Invalid slider value."));
495
496 return;
497 }
498
499 m_slider->SetValue(val);
500}
501
502void SliderWidgetsPage::OnUpdateUIValueButton(wxUpdateUIEvent& event)
503{
504 long val;
505 event.Enable( m_textValue->GetValue().ToLong(&val) && IsValidValue(val) );
506}
507
508void SliderWidgetsPage::OnUpdateUITickFreq(wxUpdateUIEvent& event)
509{
510 long freq;
511 event.Enable( m_chkTicks->GetValue() &&
512 m_textTickFreq->GetValue().ToLong(&freq) &&
513 (freq > 0) && (freq <= m_max - m_min) );
514}
515
53b98211
JS
516void SliderWidgetsPage::OnUpdateUIThumbLen(wxUpdateUIEvent& event)
517{
518 long val;
519 event.Enable( m_textThumbLen->GetValue().ToLong(&val));
520}
521
32b8ec41
VZ
522void SliderWidgetsPage::OnUpdateUIMinMaxButton(wxUpdateUIEvent& event)
523{
524 long mn, mx;
525 event.Enable( m_textMin->GetValue().ToLong(&mn) &&
526 m_textMax->GetValue().ToLong(&mx) &&
527 mn < mx);
528}
529
530void SliderWidgetsPage::OnUpdateUIResetButton(wxUpdateUIEvent& event)
531{
532 event.Enable( m_chkVert->GetValue() ||
53b98211 533 !m_chkTicks->GetValue() ||
32b8ec41 534 !m_chkLabels->GetValue() ||
53b98211 535 m_chkBothSides->GetValue() );
32b8ec41
VZ
536}
537
538void SliderWidgetsPage::OnCheckOrRadioBox(wxCommandEvent& event)
539{
540 CreateSlider();
541}
542
543void SliderWidgetsPage::OnUpdateUICurValueText(wxUpdateUIEvent& event)
544{
aec18ff7 545 event.SetText( wxString::Format(_T("%d"), m_slider->GetValue()) );
32b8ec41
VZ
546}
547
53b98211
JS
548void SliderWidgetsPage::OnUpdateUIRadioSides(wxUpdateUIEvent& event)
549{
550 event.Enable( m_chkLabels->GetValue() || m_chkTicks->GetValue() );
551}
552
553void SliderWidgetsPage::OnUpdateUIBothSides(wxUpdateUIEvent& event)
32b8ec41 554{
53b98211
JS
555#if defined(__WIN95__) || defined(__WXUNIVERSAL__)
556 event.Enable( m_chkTicks->GetValue() );
557#else
558 event.Enable( FALSE );
559#endif // defined(__WIN95__) || defined(__WXUNIVERSAL__)
32b8ec41
VZ
560}
561
d79b005a 562void SliderWidgetsPage::OnSlider(wxScrollEvent& event)
32b8ec41 563{
2b5f62a0
VZ
564 wxASSERT_MSG( event.GetInt() == m_slider->GetValue(),
565 wxT("slider value should be the same") );
32b8ec41 566
d79b005a
VZ
567 wxEventType eventType = event.GetEventType();
568
569 /*
570 This array takes the EXACT order of the declarations in
571 include/wx/event.h
572 (section "wxScrollBar and wxSlider event identifiers")
573 */
574 static const wxChar *eventNames[] =
575 {
576 wxT("wxEVT_SCROLL_TOP"),
577 wxT("wxEVT_SCROLL_BOTTOM"),
578 wxT("wxEVT_SCROLL_LINEUP"),
579 wxT("wxEVT_SCROLL_LINEDOWN"),
580 wxT("wxEVT_SCROLL_PAGEUP"),
581 wxT("wxEVT_SCROLL_PAGEDOWN"),
582 wxT("wxEVT_SCROLL_THUMBTRACK"),
583 wxT("wxEVT_SCROLL_THUMBRELEASE"),
584 wxT("wxEVT_SCROLL_ENDSCROLL")
585 };
586
587 int index = eventType - wxEVT_SCROLL_TOP;
588
589 /*
590 If this assert is triggered, there is an unknown slider event which
591 should be added to the above eventNames array.
592 */
8b1d8f36 593 wxASSERT_MSG(index >= 0 && (size_t)index < WXSIZEOF(eventNames),
d79b005a
VZ
594 wxT("Unknown slider event") );
595
596
597 static int s_numSliderEvents = 0;
598
599 wxLogMessage(wxT("Slider event #%d: %s (pos = %d)"),
580d78b3 600 s_numSliderEvents++,
d79b005a
VZ
601 eventNames[index],
602 event.GetPosition());
32b8ec41
VZ
603}
604