]>
Commit | Line | Data |
---|---|---|
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 | |
32b8ec41 | 7 | // Copyright: (c) 2001 Vadim Zeitlin |
526954c5 | 8 | // Licence: wxWindows licence |
32b8ec41 VZ |
9 | ///////////////////////////////////////////////////////////////////////////// |
10 | ||
11 | // ============================================================================ | |
12 | // declarations | |
13 | // ============================================================================ | |
14 | ||
15 | // ---------------------------------------------------------------------------- | |
16 | // headers | |
17 | // ---------------------------------------------------------------------------- | |
18 | ||
19 | // for compilers that support precompilation, includes "wx/wx.h". | |
20 | #include "wx/wxprec.h" | |
21 | ||
22 | #ifdef __BORLANDC__ | |
23 | #pragma hdrstop | |
24 | #endif | |
25 | ||
61c083e7 WS |
26 | #if wxUSE_SLIDER |
27 | ||
32b8ec41 VZ |
28 | // for all others, include the necessary headers |
29 | #ifndef WX_PRECOMP | |
30 | #include "wx/log.h" | |
31 | ||
3bb70c40 | 32 | #include "wx/bitmap.h" |
32b8ec41 VZ |
33 | #include "wx/button.h" |
34 | #include "wx/checkbox.h" | |
35 | #include "wx/radiobox.h" | |
36 | #include "wx/slider.h" | |
37 | #include "wx/statbox.h" | |
38 | #include "wx/textctrl.h" | |
39 | #endif | |
40 | ||
53b98211 JS |
41 | #if wxUSE_TOOLTIPS |
42 | #include "wx/tooltip.h" | |
43 | #endif | |
44 | ||
32b8ec41 VZ |
45 | #include "wx/sizer.h" |
46 | ||
47 | #include "widgets.h" | |
61c083e7 | 48 | |
32b8ec41 VZ |
49 | #include "icons/slider.xpm" |
50 | ||
51 | // ---------------------------------------------------------------------------- | |
52 | // constants | |
53 | // ---------------------------------------------------------------------------- | |
54 | ||
55 | // control ids | |
56 | enum | |
57 | { | |
d79b005a | 58 | SliderPage_Reset = wxID_HIGHEST, |
32b8ec41 VZ |
59 | SliderPage_Clear, |
60 | SliderPage_SetValue, | |
61 | SliderPage_SetMinAndMax, | |
d6c1aef8 MR |
62 | SliderPage_SetLineSize, |
63 | SliderPage_SetPageSize, | |
32b8ec41 | 64 | SliderPage_SetTickFreq, |
53b98211 | 65 | SliderPage_SetThumbLen, |
32b8ec41 VZ |
66 | SliderPage_CurValueText, |
67 | SliderPage_ValueText, | |
68 | SliderPage_MinText, | |
69 | SliderPage_MaxText, | |
d6c1aef8 MR |
70 | SliderPage_LineSizeText, |
71 | SliderPage_PageSizeText, | |
32b8ec41 | 72 | SliderPage_TickFreqText, |
53b98211 JS |
73 | SliderPage_ThumbLenText, |
74 | SliderPage_RadioSides, | |
75 | SliderPage_BothSides, | |
32b8ec41 VZ |
76 | SliderPage_Slider |
77 | }; | |
78 | ||
53b98211 JS |
79 | // sides radiobox values |
80 | enum | |
81 | { | |
c0a9fe92 | 82 | SliderTicks_None, |
93103bab VZ |
83 | SliderTicks_Top, |
84 | SliderTicks_Bottom, | |
85 | SliderTicks_Left, | |
86 | SliderTicks_Right | |
53b98211 JS |
87 | }; |
88 | ||
32b8ec41 VZ |
89 | // ---------------------------------------------------------------------------- |
90 | // SliderWidgetsPage | |
91 | // ---------------------------------------------------------------------------- | |
92 | ||
93 | class SliderWidgetsPage : public WidgetsPage | |
94 | { | |
95 | public: | |
f2fdc4d5 | 96 | SliderWidgetsPage(WidgetsBookCtrl *book, wxImageList *imaglist); |
8f6eaec9 | 97 | virtual ~SliderWidgetsPage(){}; |
32b8ec41 | 98 | |
195df7a7 | 99 | virtual wxControl *GetWidget() const { return m_slider; } |
1301e228 | 100 | virtual void RecreateWidget() { CreateSlider(); } |
195df7a7 | 101 | |
453535a7 WS |
102 | // lazy creation of the content |
103 | virtual void CreateContent(); | |
104 | ||
32b8ec41 VZ |
105 | protected: |
106 | // event handlers | |
107 | void OnButtonReset(wxCommandEvent& event); | |
108 | void OnButtonClear(wxCommandEvent& event); | |
109 | void OnButtonSetValue(wxCommandEvent& event); | |
110 | void OnButtonSetMinAndMax(wxCommandEvent& event); | |
d6c1aef8 MR |
111 | void OnButtonSetLineSize(wxCommandEvent& event); |
112 | void OnButtonSetPageSize(wxCommandEvent& event); | |
32b8ec41 | 113 | void OnButtonSetTickFreq(wxCommandEvent& event); |
53b98211 | 114 | void OnButtonSetThumbLen(wxCommandEvent& event); |
32b8ec41 VZ |
115 | |
116 | void OnCheckOrRadioBox(wxCommandEvent& event); | |
117 | ||
d79b005a | 118 | void OnSlider(wxScrollEvent& event); |
32b8ec41 | 119 | |
32b8ec41 VZ |
120 | void OnUpdateUIValueButton(wxUpdateUIEvent& event); |
121 | void OnUpdateUIMinMaxButton(wxUpdateUIEvent& event); | |
d6c1aef8 MR |
122 | void OnUpdateUILineSize(wxUpdateUIEvent& event); |
123 | void OnUpdateUIPageSize(wxUpdateUIEvent& event); | |
32b8ec41 | 124 | void OnUpdateUITickFreq(wxUpdateUIEvent& event); |
53b98211 JS |
125 | void OnUpdateUIThumbLen(wxUpdateUIEvent& event); |
126 | void OnUpdateUIRadioSides(wxUpdateUIEvent& event); | |
127 | void OnUpdateUIBothSides(wxUpdateUIEvent& event); | |
32b8ec41 VZ |
128 | |
129 | void OnUpdateUIResetButton(wxUpdateUIEvent& event); | |
130 | ||
131 | void OnUpdateUICurValueText(wxUpdateUIEvent& event); | |
132 | ||
133 | // reset the slider parameters | |
134 | void Reset(); | |
135 | ||
136 | // (re)create the slider | |
137 | void CreateSlider(); | |
138 | ||
d6c1aef8 MR |
139 | // set the line size from the text field value |
140 | void DoSetLineSize(); | |
141 | ||
142 | // set the page size from the text field value | |
143 | void DoSetPageSize(); | |
144 | ||
32b8ec41 VZ |
145 | // set the tick frequency from the text field value |
146 | void DoSetTickFreq(); | |
147 | ||
53b98211 JS |
148 | // set the thumb len from the text field value |
149 | void DoSetThumbLen(); | |
150 | ||
32b8ec41 VZ |
151 | // is this slider value in range? |
152 | bool IsValidValue(int val) const | |
153 | { return (val >= m_min) && (val <= m_max); } | |
154 | ||
155 | // the slider range | |
156 | int m_min, m_max; | |
157 | ||
158 | // the controls | |
159 | // ------------ | |
160 | ||
161 | // the check/radio boxes for styles | |
7de83494 RR |
162 | wxCheckBox *m_chkMinMaxLabels, |
163 | *m_chkValueLabel, | |
2bcdbe0f | 164 | *m_chkInverse, |
53b98211 JS |
165 | *m_chkTicks, |
166 | *m_chkBothSides; | |
167 | ||
168 | wxRadioBox *m_radioSides; | |
32b8ec41 VZ |
169 | |
170 | // the slider itself and the sizer it is in | |
171 | wxSlider *m_slider; | |
172 | wxSizer *m_sizerSlider; | |
173 | ||
174 | // the text entries for set value/range | |
175 | wxTextCtrl *m_textValue, | |
176 | *m_textMin, | |
177 | *m_textMax, | |
d6c1aef8 MR |
178 | *m_textLineSize, |
179 | *m_textPageSize, | |
53b98211 JS |
180 | *m_textTickFreq, |
181 | *m_textThumbLen; | |
32b8ec41 VZ |
182 | |
183 | private: | |
5e173f35 GD |
184 | DECLARE_EVENT_TABLE() |
185 | DECLARE_WIDGETS_PAGE(SliderWidgetsPage) | |
32b8ec41 VZ |
186 | }; |
187 | ||
188 | // ---------------------------------------------------------------------------- | |
189 | // event tables | |
190 | // ---------------------------------------------------------------------------- | |
191 | ||
192 | BEGIN_EVENT_TABLE(SliderWidgetsPage, WidgetsPage) | |
193 | EVT_BUTTON(SliderPage_Reset, SliderWidgetsPage::OnButtonReset) | |
194 | EVT_BUTTON(SliderPage_SetValue, SliderWidgetsPage::OnButtonSetValue) | |
195 | EVT_BUTTON(SliderPage_SetMinAndMax, SliderWidgetsPage::OnButtonSetMinAndMax) | |
d6c1aef8 MR |
196 | EVT_BUTTON(SliderPage_SetLineSize, SliderWidgetsPage::OnButtonSetLineSize) |
197 | EVT_BUTTON(SliderPage_SetPageSize, SliderWidgetsPage::OnButtonSetPageSize) | |
32b8ec41 | 198 | EVT_BUTTON(SliderPage_SetTickFreq, SliderWidgetsPage::OnButtonSetTickFreq) |
53b98211 | 199 | EVT_BUTTON(SliderPage_SetThumbLen, SliderWidgetsPage::OnButtonSetThumbLen) |
32b8ec41 VZ |
200 | |
201 | EVT_UPDATE_UI(SliderPage_SetValue, SliderWidgetsPage::OnUpdateUIValueButton) | |
202 | EVT_UPDATE_UI(SliderPage_SetMinAndMax, SliderWidgetsPage::OnUpdateUIMinMaxButton) | |
d6c1aef8 MR |
203 | EVT_UPDATE_UI(SliderPage_SetLineSize, SliderWidgetsPage::OnUpdateUILineSize) |
204 | EVT_UPDATE_UI(SliderPage_SetPageSize, SliderWidgetsPage::OnUpdateUIPageSize) | |
32b8ec41 | 205 | EVT_UPDATE_UI(SliderPage_SetTickFreq, SliderWidgetsPage::OnUpdateUITickFreq) |
53b98211 | 206 | EVT_UPDATE_UI(SliderPage_SetThumbLen, SliderWidgetsPage::OnUpdateUIThumbLen) |
53b98211 JS |
207 | EVT_UPDATE_UI(SliderPage_RadioSides, SliderWidgetsPage::OnUpdateUIRadioSides) |
208 | EVT_UPDATE_UI(SliderPage_BothSides, SliderWidgetsPage::OnUpdateUIBothSides) | |
32b8ec41 VZ |
209 | |
210 | EVT_UPDATE_UI(SliderPage_Reset, SliderWidgetsPage::OnUpdateUIResetButton) | |
211 | ||
212 | EVT_UPDATE_UI(SliderPage_CurValueText, SliderWidgetsPage::OnUpdateUICurValueText) | |
213 | ||
d79b005a | 214 | EVT_COMMAND_SCROLL(SliderPage_Slider, SliderWidgetsPage::OnSlider) |
32b8ec41 | 215 | |
206d3a16 JS |
216 | EVT_CHECKBOX(wxID_ANY, SliderWidgetsPage::OnCheckOrRadioBox) |
217 | EVT_RADIOBOX(wxID_ANY, SliderWidgetsPage::OnCheckOrRadioBox) | |
32b8ec41 VZ |
218 | END_EVENT_TABLE() |
219 | ||
220 | // ============================================================================ | |
221 | // implementation | |
222 | // ============================================================================ | |
223 | ||
f0fa4312 WS |
224 | #if defined(__WXUNIVERSAL__) |
225 | #define FAMILY_CTRLS UNIVERSAL_CTRLS | |
226 | #else | |
227 | #define FAMILY_CTRLS NATIVE_CTRLS | |
228 | #endif | |
229 | ||
9a83f860 | 230 | IMPLEMENT_WIDGETS_PAGE(SliderWidgetsPage, wxT("Slider"), FAMILY_CTRLS ); |
32b8ec41 | 231 | |
f2fdc4d5 | 232 | SliderWidgetsPage::SliderWidgetsPage(WidgetsBookCtrl *book, |
61c083e7 | 233 | wxImageList *imaglist) |
261357eb | 234 | : WidgetsPage(book, imaglist, slider_xpm) |
32b8ec41 | 235 | { |
32b8ec41 VZ |
236 | // init everything |
237 | m_min = 0; | |
238 | m_max = 100; | |
239 | ||
dc3155f1 | 240 | m_chkInverse = |
32b8ec41 | 241 | m_chkTicks = |
7de83494 RR |
242 | m_chkMinMaxLabels = |
243 | m_chkValueLabel = | |
53b98211 JS |
244 | m_chkBothSides = (wxCheckBox *)NULL; |
245 | ||
246 | m_radioSides = (wxRadioBox *)NULL; | |
32b8ec41 VZ |
247 | |
248 | m_slider = (wxSlider *)NULL; | |
249 | m_sizerSlider = (wxSizer *)NULL; | |
453535a7 | 250 | } |
32b8ec41 | 251 | |
453535a7 WS |
252 | void SliderWidgetsPage::CreateContent() |
253 | { | |
32b8ec41 VZ |
254 | wxSizer *sizerTop = new wxBoxSizer(wxHORIZONTAL); |
255 | ||
256 | // left pane | |
9a83f860 | 257 | wxStaticBox *box = new wxStaticBox(this, wxID_ANY, wxT("&Set style")); |
32b8ec41 VZ |
258 | wxSizer *sizerLeft = new wxStaticBoxSizer(box, wxVERTICAL); |
259 | ||
9a83f860 VZ |
260 | m_chkInverse = CreateCheckBoxAndAddToSizer(sizerLeft, wxT("&Inverse")); |
261 | m_chkTicks = CreateCheckBoxAndAddToSizer(sizerLeft, wxT("Show &ticks")); | |
7de83494 RR |
262 | m_chkMinMaxLabels = CreateCheckBoxAndAddToSizer(sizerLeft, wxT("Show min/max &labels")); |
263 | m_chkValueLabel = CreateCheckBoxAndAddToSizer(sizerLeft, wxT("Show &value label")); | |
53b98211 JS |
264 | static const wxString sides[] = |
265 | { | |
c0a9fe92 | 266 | wxT("default"), |
9a83f860 VZ |
267 | wxT("top"), |
268 | wxT("bottom"), | |
269 | wxT("left"), | |
270 | wxT("right"), | |
53b98211 | 271 | }; |
c0a9fe92 | 272 | m_radioSides = new wxRadioBox(this, SliderPage_RadioSides, wxT("&Label position"), |
53b98211 JS |
273 | wxDefaultPosition, wxDefaultSize, |
274 | WXSIZEOF(sides), sides, | |
275 | 1, wxRA_SPECIFY_COLS); | |
276 | sizerLeft->Add(m_radioSides, 0, wxGROW | wxALL, 5); | |
277 | m_chkBothSides = CreateCheckBoxAndAddToSizer | |
9a83f860 | 278 | (sizerLeft, wxT("&Both sides"), SliderPage_BothSides); |
53b98211 | 279 | #if wxUSE_TOOLTIPS |
9a83f860 | 280 | m_chkBothSides->SetToolTip( wxT("\"Both sides\" is only supported \nin Win95 and Universal") ); |
53b98211 | 281 | #endif // wxUSE_TOOLTIPS |
32b8ec41 VZ |
282 | |
283 | sizerLeft->Add(5, 5, 0, wxGROW | wxALL, 5); // spacer | |
284 | ||
9a83f860 | 285 | wxButton *btn = new wxButton(this, SliderPage_Reset, wxT("&Reset")); |
32b8ec41 VZ |
286 | sizerLeft->Add(btn, 0, wxALIGN_CENTRE_HORIZONTAL | wxALL, 15); |
287 | ||
288 | // middle pane | |
9a83f860 | 289 | wxStaticBox *box2 = new wxStaticBox(this, wxID_ANY, wxT("&Change slider value")); |
32b8ec41 VZ |
290 | wxSizer *sizerMiddle = new wxStaticBoxSizer(box2, wxVERTICAL); |
291 | ||
292 | wxTextCtrl *text; | |
9a83f860 | 293 | wxSizer *sizerRow = CreateSizerWithTextAndLabel(wxT("Current value"), |
32b8ec41 VZ |
294 | SliderPage_CurValueText, |
295 | &text); | |
206d3a16 | 296 | text->SetEditable(false); |
32b8ec41 VZ |
297 | |
298 | sizerMiddle->Add(sizerRow, 0, wxALL | wxGROW, 5); | |
299 | ||
300 | sizerRow = CreateSizerWithTextAndButton(SliderPage_SetValue, | |
9a83f860 | 301 | wxT("Set &value"), |
32b8ec41 VZ |
302 | SliderPage_ValueText, |
303 | &m_textValue); | |
304 | sizerMiddle->Add(sizerRow, 0, wxALL | wxGROW, 5); | |
305 | ||
306 | sizerRow = CreateSizerWithTextAndButton(SliderPage_SetMinAndMax, | |
9a83f860 | 307 | wxT("&Min and max"), |
32b8ec41 VZ |
308 | SliderPage_MinText, |
309 | &m_textMin); | |
310 | ||
206d3a16 | 311 | m_textMax = new wxTextCtrl(this, SliderPage_MaxText, wxEmptyString); |
32b8ec41 VZ |
312 | sizerRow->Add(m_textMax, 1, wxLEFT | wxALIGN_CENTRE_VERTICAL, 5); |
313 | ||
9a83f860 VZ |
314 | m_textMin->SetValue( wxString::Format(wxT("%d"), m_min) ); |
315 | m_textMax->SetValue( wxString::Format(wxT("%d"), m_max) ); | |
32b8ec41 VZ |
316 | |
317 | sizerMiddle->Add(sizerRow, 0, wxALL | wxGROW, 5); | |
318 | ||
d6c1aef8 | 319 | sizerRow = CreateSizerWithTextAndButton(SliderPage_SetLineSize, |
9a83f860 | 320 | wxT("Li&ne size"), |
d6c1aef8 MR |
321 | SliderPage_LineSizeText, |
322 | &m_textLineSize); | |
323 | ||
324 | sizerMiddle->Add(sizerRow, 0, wxALL | wxGROW, 5); | |
325 | ||
326 | sizerRow = CreateSizerWithTextAndButton(SliderPage_SetPageSize, | |
9a83f860 | 327 | wxT("P&age size"), |
d6c1aef8 MR |
328 | SliderPage_PageSizeText, |
329 | &m_textPageSize); | |
330 | ||
331 | sizerMiddle->Add(sizerRow, 0, wxALL | wxGROW, 5); | |
332 | ||
32b8ec41 | 333 | sizerRow = CreateSizerWithTextAndButton(SliderPage_SetTickFreq, |
9a83f860 | 334 | wxT("Tick &frequency"), |
32b8ec41 VZ |
335 | SliderPage_TickFreqText, |
336 | &m_textTickFreq); | |
337 | ||
9a83f860 | 338 | m_textTickFreq->SetValue(wxT("10")); |
32b8ec41 VZ |
339 | |
340 | sizerMiddle->Add(sizerRow, 0, wxALL | wxGROW, 5); | |
341 | ||
53b98211 | 342 | sizerRow = CreateSizerWithTextAndButton(SliderPage_SetThumbLen, |
9a83f860 | 343 | wxT("Thumb &length"), |
53b98211 JS |
344 | SliderPage_ThumbLenText, |
345 | &m_textThumbLen); | |
346 | ||
347 | sizerMiddle->Add(sizerRow, 0, wxALL | wxGROW, 5); | |
348 | ||
32b8ec41 VZ |
349 | // right pane |
350 | wxSizer *sizerRight = new wxBoxSizer(wxHORIZONTAL); | |
53b98211 | 351 | sizerRight->SetMinSize(150, 40); |
32b8ec41 VZ |
352 | m_sizerSlider = sizerRight; // save it to modify it later |
353 | ||
354 | Reset(); | |
355 | CreateSlider(); | |
356 | ||
9a83f860 VZ |
357 | m_textLineSize->SetValue(wxString::Format(wxT("%d"), m_slider->GetLineSize())); |
358 | m_textPageSize->SetValue(wxString::Format(wxT("%d"), m_slider->GetPageSize())); | |
d6c1aef8 | 359 | |
32b8ec41 VZ |
360 | // the 3 panes panes compose the window |
361 | sizerTop->Add(sizerLeft, 0, wxGROW | (wxALL & ~wxLEFT), 10); | |
7b127900 | 362 | sizerTop->Add(sizerMiddle, 0, wxGROW | wxALL, 10); |
32b8ec41 VZ |
363 | sizerTop->Add(sizerRight, 1, wxGROW | (wxALL & ~wxRIGHT), 10); |
364 | ||
365 | // final initializations | |
32b8ec41 | 366 | SetSizer(sizerTop); |
32b8ec41 VZ |
367 | } |
368 | ||
32b8ec41 VZ |
369 | // ---------------------------------------------------------------------------- |
370 | // operations | |
371 | // ---------------------------------------------------------------------------- | |
372 | ||
373 | void SliderWidgetsPage::Reset() | |
374 | { | |
2bcdbe0f | 375 | m_chkInverse->SetValue(false); |
206d3a16 | 376 | m_chkTicks->SetValue(true); |
7de83494 RR |
377 | m_chkValueLabel->SetValue(true); |
378 | m_chkMinMaxLabels->SetValue(true); | |
206d3a16 | 379 | m_chkBothSides->SetValue(false); |
53b98211 | 380 | |
c0a9fe92 | 381 | m_radioSides->SetSelection(SliderTicks_None); |
32b8ec41 VZ |
382 | } |
383 | ||
384 | void SliderWidgetsPage::CreateSlider() | |
385 | { | |
1301e228 | 386 | int flags = ms_defaultFlags; |
32b8ec41 | 387 | |
2bcdbe0f KH |
388 | if ( m_chkInverse->GetValue() ) |
389 | { | |
390 | flags |= wxSL_INVERSE; | |
391 | } | |
392 | ||
7de83494 | 393 | if ( m_chkMinMaxLabels->GetValue() ) |
32b8ec41 | 394 | { |
7de83494 RR |
395 | flags |= wxSL_MIN_MAX_LABELS; |
396 | } | |
397 | ||
398 | if ( m_chkValueLabel->GetValue() ) | |
399 | { | |
400 | flags |= wxSL_VALUE_LABEL; | |
32b8ec41 VZ |
401 | } |
402 | ||
403 | if ( m_chkTicks->GetValue() ) | |
404 | { | |
405 | flags |= wxSL_AUTOTICKS; | |
406 | } | |
407 | ||
c0a9fe92 VZ |
408 | // notice that the style names refer to the _ticks_ positions while we want |
409 | // to allow the user to select the label(s) positions and the labels are on | |
410 | // the opposite side from the ticks, hence the apparent reversal below | |
53b98211 JS |
411 | switch ( m_radioSides->GetSelection() ) |
412 | { | |
c0a9fe92 VZ |
413 | case SliderTicks_None: |
414 | break; | |
415 | ||
93103bab | 416 | case SliderTicks_Top: |
c0a9fe92 | 417 | flags |= wxSL_BOTTOM; |
53b98211 | 418 | break; |
93103bab VZ |
419 | |
420 | case SliderTicks_Left: | |
c0a9fe92 | 421 | flags |= wxSL_RIGHT | wxSL_VERTICAL; |
53b98211 | 422 | break; |
93103bab VZ |
423 | |
424 | case SliderTicks_Bottom: | |
c0a9fe92 | 425 | flags |= wxSL_TOP; |
53b98211 | 426 | break; |
93103bab | 427 | |
510878aa | 428 | case SliderTicks_Right: |
c0a9fe92 | 429 | flags |= wxSL_LEFT | wxSL_VERTICAL; |
53b98211 | 430 | break; |
93103bab | 431 | |
53b98211 | 432 | default: |
9a83f860 | 433 | wxFAIL_MSG(wxT("unexpected radiobox selection")); |
53b98211 JS |
434 | // fall through |
435 | } | |
436 | ||
437 | if ( m_chkBothSides->GetValue() ) | |
438 | { | |
439 | flags |= wxSL_BOTH; | |
440 | } | |
441 | ||
32b8ec41 VZ |
442 | int val = m_min; |
443 | if ( m_slider ) | |
444 | { | |
445 | int valOld = m_slider->GetValue(); | |
446 | if ( !IsValidValue(valOld) ) | |
447 | { | |
448 | val = valOld; | |
449 | } | |
450 | ||
12a3f227 | 451 | m_sizerSlider->Detach( m_slider ); |
32b8ec41 VZ |
452 | |
453 | if ( m_sizerSlider->GetChildren().GetCount() ) | |
454 | { | |
455 | // we have 2 spacers, remove them too | |
9dd96c0f VZ |
456 | m_sizerSlider->Remove( 0 ); |
457 | m_sizerSlider->Remove( 0 ); | |
32b8ec41 VZ |
458 | } |
459 | ||
460 | delete m_slider; | |
461 | } | |
462 | ||
463 | m_slider = new wxSlider(this, SliderPage_Slider, | |
464 | val, m_min, m_max, | |
465 | wxDefaultPosition, wxDefaultSize, | |
466 | flags); | |
467 | ||
93103bab | 468 | if ( m_slider->HasFlag(wxSL_VERTICAL) ) |
32b8ec41 VZ |
469 | { |
470 | m_sizerSlider->Add(0, 0, 1); | |
471 | m_sizerSlider->Add(m_slider, 0, wxGROW | wxALL, 5); | |
472 | m_sizerSlider->Add(0, 0, 1); | |
473 | } | |
474 | else | |
475 | { | |
476 | m_sizerSlider->Add(m_slider, 1, wxCENTRE | wxALL, 5); | |
477 | } | |
478 | ||
479 | if ( m_chkTicks->GetValue() ) | |
480 | { | |
481 | DoSetTickFreq(); | |
482 | } | |
483 | ||
484 | m_sizerSlider->Layout(); | |
485 | } | |
486 | ||
d6c1aef8 MR |
487 | void SliderWidgetsPage::DoSetLineSize() |
488 | { | |
489 | long lineSize; | |
490 | if ( !m_textLineSize->GetValue().ToLong(&lineSize) ) | |
491 | { | |
9a83f860 | 492 | wxLogWarning(wxT("Invalid slider line size")); |
d6c1aef8 MR |
493 | |
494 | return; | |
495 | } | |
496 | ||
497 | m_slider->SetLineSize(lineSize); | |
498 | ||
499 | if ( m_slider->GetLineSize() != lineSize ) | |
500 | { | |
9a83f860 | 501 | wxLogWarning(wxT("Invalid line size in slider.")); |
d6c1aef8 MR |
502 | } |
503 | } | |
504 | ||
505 | void SliderWidgetsPage::DoSetPageSize() | |
506 | { | |
507 | long pageSize; | |
508 | if ( !m_textPageSize->GetValue().ToLong(&pageSize) ) | |
509 | { | |
9a83f860 | 510 | wxLogWarning(wxT("Invalid slider page size")); |
d6c1aef8 MR |
511 | |
512 | return; | |
513 | } | |
514 | ||
515 | m_slider->SetPageSize(pageSize); | |
516 | ||
517 | if ( m_slider->GetPageSize() != pageSize ) | |
518 | { | |
9a83f860 | 519 | wxLogWarning(wxT("Invalid page size in slider.")); |
d6c1aef8 MR |
520 | } |
521 | } | |
522 | ||
32b8ec41 VZ |
523 | void SliderWidgetsPage::DoSetTickFreq() |
524 | { | |
525 | long freq; | |
526 | if ( !m_textTickFreq->GetValue().ToLong(&freq) ) | |
527 | { | |
9a83f860 | 528 | wxLogWarning(wxT("Invalid slider tick frequency")); |
32b8ec41 VZ |
529 | |
530 | return; | |
531 | } | |
532 | ||
0a12e013 | 533 | m_slider->SetTickFreq(freq); |
32b8ec41 VZ |
534 | } |
535 | ||
53b98211 JS |
536 | void SliderWidgetsPage::DoSetThumbLen() |
537 | { | |
538 | long len; | |
539 | if ( !m_textThumbLen->GetValue().ToLong(&len) ) | |
540 | { | |
9a83f860 | 541 | wxLogWarning(wxT("Invalid slider thumb length")); |
53b98211 JS |
542 | |
543 | return; | |
544 | } | |
545 | ||
546 | m_slider->SetThumbLength(len); | |
547 | } | |
548 | ||
32b8ec41 VZ |
549 | // ---------------------------------------------------------------------------- |
550 | // event handlers | |
551 | // ---------------------------------------------------------------------------- | |
552 | ||
553 | void SliderWidgetsPage::OnButtonReset(wxCommandEvent& WXUNUSED(event)) | |
554 | { | |
555 | Reset(); | |
556 | ||
557 | CreateSlider(); | |
558 | } | |
559 | ||
d6c1aef8 MR |
560 | void SliderWidgetsPage::OnButtonSetLineSize(wxCommandEvent& WXUNUSED(event)) |
561 | { | |
562 | DoSetLineSize(); | |
563 | } | |
564 | ||
565 | void SliderWidgetsPage::OnButtonSetPageSize(wxCommandEvent& WXUNUSED(event)) | |
566 | { | |
567 | DoSetPageSize(); | |
568 | } | |
569 | ||
32b8ec41 VZ |
570 | void SliderWidgetsPage::OnButtonSetTickFreq(wxCommandEvent& WXUNUSED(event)) |
571 | { | |
572 | DoSetTickFreq(); | |
573 | } | |
574 | ||
53b98211 JS |
575 | void SliderWidgetsPage::OnButtonSetThumbLen(wxCommandEvent& WXUNUSED(event)) |
576 | { | |
577 | DoSetThumbLen(); | |
578 | } | |
579 | ||
32b8ec41 VZ |
580 | void SliderWidgetsPage::OnButtonSetMinAndMax(wxCommandEvent& WXUNUSED(event)) |
581 | { | |
582 | long minNew, | |
583 | maxNew = 0; // init to suppress compiler warning | |
584 | if ( !m_textMin->GetValue().ToLong(&minNew) || | |
585 | !m_textMax->GetValue().ToLong(&maxNew) || | |
586 | minNew >= maxNew ) | |
587 | { | |
9a83f860 | 588 | wxLogWarning(wxT("Invalid min/max values for the slider.")); |
32b8ec41 VZ |
589 | |
590 | return; | |
591 | } | |
592 | ||
593 | m_min = minNew; | |
594 | m_max = maxNew; | |
595 | ||
596 | m_slider->SetRange(minNew, maxNew); | |
9690a439 WS |
597 | |
598 | if ( m_slider->GetMin() != m_min || | |
599 | m_slider->GetMax() != m_max ) | |
600 | { | |
9a83f860 | 601 | wxLogWarning(wxT("Invalid range in slider.")); |
9690a439 | 602 | } |
32b8ec41 VZ |
603 | } |
604 | ||
605 | void SliderWidgetsPage::OnButtonSetValue(wxCommandEvent& WXUNUSED(event)) | |
606 | { | |
607 | long val; | |
608 | if ( !m_textValue->GetValue().ToLong(&val) || !IsValidValue(val) ) | |
609 | { | |
9a83f860 | 610 | wxLogWarning(wxT("Invalid slider value.")); |
32b8ec41 VZ |
611 | |
612 | return; | |
613 | } | |
614 | ||
615 | m_slider->SetValue(val); | |
616 | } | |
617 | ||
618 | void SliderWidgetsPage::OnUpdateUIValueButton(wxUpdateUIEvent& event) | |
619 | { | |
620 | long val; | |
621 | event.Enable( m_textValue->GetValue().ToLong(&val) && IsValidValue(val) ); | |
622 | } | |
623 | ||
d6c1aef8 MR |
624 | void SliderWidgetsPage::OnUpdateUILineSize(wxUpdateUIEvent& event) |
625 | { | |
626 | long lineSize; | |
627 | event.Enable( m_textLineSize->GetValue().ToLong(&lineSize) && | |
628 | (lineSize > 0) && (lineSize <= m_max - m_min) ); | |
629 | } | |
630 | ||
631 | void SliderWidgetsPage::OnUpdateUIPageSize(wxUpdateUIEvent& event) | |
632 | { | |
633 | long pageSize; | |
634 | event.Enable( m_textPageSize->GetValue().ToLong(&pageSize) && | |
635 | (pageSize > 0) && (pageSize <= m_max - m_min) ); | |
636 | } | |
637 | ||
32b8ec41 VZ |
638 | void SliderWidgetsPage::OnUpdateUITickFreq(wxUpdateUIEvent& event) |
639 | { | |
640 | long freq; | |
641 | event.Enable( m_chkTicks->GetValue() && | |
642 | m_textTickFreq->GetValue().ToLong(&freq) && | |
643 | (freq > 0) && (freq <= m_max - m_min) ); | |
644 | } | |
645 | ||
53b98211 JS |
646 | void SliderWidgetsPage::OnUpdateUIThumbLen(wxUpdateUIEvent& event) |
647 | { | |
648 | long val; | |
649 | event.Enable( m_textThumbLen->GetValue().ToLong(&val)); | |
650 | } | |
651 | ||
32b8ec41 VZ |
652 | void SliderWidgetsPage::OnUpdateUIMinMaxButton(wxUpdateUIEvent& event) |
653 | { | |
654 | long mn, mx; | |
655 | event.Enable( m_textMin->GetValue().ToLong(&mn) && | |
656 | m_textMax->GetValue().ToLong(&mx) && | |
657 | mn < mx); | |
658 | } | |
659 | ||
660 | void SliderWidgetsPage::OnUpdateUIResetButton(wxUpdateUIEvent& event) | |
661 | { | |
93103bab | 662 | event.Enable( m_chkInverse->GetValue() || |
53b98211 | 663 | !m_chkTicks->GetValue() || |
7de83494 RR |
664 | !m_chkValueLabel->GetValue() || |
665 | !m_chkMinMaxLabels->GetValue() || | |
93103bab | 666 | m_chkBothSides->GetValue() || |
c0a9fe92 | 667 | m_radioSides->GetSelection() != SliderTicks_None ); |
32b8ec41 VZ |
668 | } |
669 | ||
c02e5a31 | 670 | void SliderWidgetsPage::OnCheckOrRadioBox(wxCommandEvent& WXUNUSED(event)) |
32b8ec41 VZ |
671 | { |
672 | CreateSlider(); | |
673 | } | |
674 | ||
675 | void SliderWidgetsPage::OnUpdateUICurValueText(wxUpdateUIEvent& event) | |
676 | { | |
9a83f860 | 677 | event.SetText( wxString::Format(wxT("%d"), m_slider->GetValue()) ); |
32b8ec41 VZ |
678 | } |
679 | ||
53b98211 JS |
680 | void SliderWidgetsPage::OnUpdateUIRadioSides(wxUpdateUIEvent& event) |
681 | { | |
7de83494 | 682 | event.Enable( m_chkValueLabel->GetValue() || m_chkTicks->GetValue() ); |
53b98211 JS |
683 | } |
684 | ||
685 | void SliderWidgetsPage::OnUpdateUIBothSides(wxUpdateUIEvent& event) | |
32b8ec41 | 686 | { |
a71d815b | 687 | #if defined(__WXMSW__) || defined(__WXUNIVERSAL__) |
53b98211 JS |
688 | event.Enable( m_chkTicks->GetValue() ); |
689 | #else | |
206d3a16 | 690 | event.Enable( false ); |
a71d815b | 691 | #endif // defined(__WXMSW__) || defined(__WXUNIVERSAL__) |
32b8ec41 VZ |
692 | } |
693 | ||
d79b005a | 694 | void SliderWidgetsPage::OnSlider(wxScrollEvent& event) |
32b8ec41 | 695 | { |
2b5f62a0 VZ |
696 | wxASSERT_MSG( event.GetInt() == m_slider->GetValue(), |
697 | wxT("slider value should be the same") ); | |
32b8ec41 | 698 | |
d79b005a VZ |
699 | wxEventType eventType = event.GetEventType(); |
700 | ||
701 | /* | |
702 | This array takes the EXACT order of the declarations in | |
703 | include/wx/event.h | |
704 | (section "wxScrollBar and wxSlider event identifiers") | |
705 | */ | |
706 | static const wxChar *eventNames[] = | |
707 | { | |
708 | wxT("wxEVT_SCROLL_TOP"), | |
709 | wxT("wxEVT_SCROLL_BOTTOM"), | |
710 | wxT("wxEVT_SCROLL_LINEUP"), | |
711 | wxT("wxEVT_SCROLL_LINEDOWN"), | |
712 | wxT("wxEVT_SCROLL_PAGEUP"), | |
713 | wxT("wxEVT_SCROLL_PAGEDOWN"), | |
714 | wxT("wxEVT_SCROLL_THUMBTRACK"), | |
715 | wxT("wxEVT_SCROLL_THUMBRELEASE"), | |
cbc85508 | 716 | wxT("wxEVT_SCROLL_CHANGED") |
d79b005a VZ |
717 | }; |
718 | ||
719 | int index = eventType - wxEVT_SCROLL_TOP; | |
720 | ||
721 | /* | |
722 | If this assert is triggered, there is an unknown slider event which | |
723 | should be added to the above eventNames array. | |
724 | */ | |
8b1d8f36 | 725 | wxASSERT_MSG(index >= 0 && (size_t)index < WXSIZEOF(eventNames), |
d79b005a VZ |
726 | wxT("Unknown slider event") ); |
727 | ||
728 | ||
729 | static int s_numSliderEvents = 0; | |
730 | ||
33cf9a19 | 731 | wxLogMessage(wxT("Slider event #%d: %s (pos = %d, int value = %d)"), |
580d78b3 | 732 | s_numSliderEvents++, |
d79b005a | 733 | eventNames[index], |
2bcdbe0f KH |
734 | event.GetPosition(), |
735 | event.GetInt()); | |
32b8ec41 VZ |
736 | } |
737 | ||
61c083e7 | 738 | #endif // wxUSE_SLIDER |