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