slider.cpp 21 KB

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