radiobox.cpp 16 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398399400401402403404405406407408409410411412413414415416417418419420421422423424425426427428429430431432433434435436437438439440441442443444445446447448449450451452453454455456457458459460461462463464465466467468469470471472473474475476477478479480481482483484485486487488489490491492493494495496497498499500501502503504505506507508509510511512513514515516517518519520521522523524525526527528529530
  1. /////////////////////////////////////////////////////////////////////////////
  2. // Program: wxWidgets Widgets Sample
  3. // Name: radiobox.cpp
  4. // Purpose: Part of the widgets sample showing wxRadioBox
  5. // Author: Vadim Zeitlin
  6. // Created: 15.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_RADIOBOX
  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/statbox.h"
  30. #include "wx/textctrl.h"
  31. #endif
  32. #include "wx/sizer.h"
  33. #include "widgets.h"
  34. #include "icons/radiobox.xpm"
  35. // ----------------------------------------------------------------------------
  36. // constants
  37. // ----------------------------------------------------------------------------
  38. // control ids
  39. enum
  40. {
  41. RadioPage_Reset = wxID_HIGHEST,
  42. RadioPage_Update,
  43. RadioPage_Selection,
  44. RadioPage_Label,
  45. RadioPage_LabelBtn,
  46. RadioPage_EnableItem,
  47. RadioPage_ShowItem,
  48. RadioPage_Radio
  49. };
  50. // layout direction radiobox selections
  51. enum
  52. {
  53. RadioDir_Default,
  54. RadioDir_LtoR,
  55. RadioDir_TtoB
  56. };
  57. // default values for the number of radiobox items
  58. static const unsigned int DEFAULT_NUM_ENTRIES = 12;
  59. static const unsigned int DEFAULT_MAJOR_DIM = 3;
  60. // this item is enabled/disabled shown/hidden by the test checkboxes
  61. static const int TEST_BUTTON = 1;
  62. // ----------------------------------------------------------------------------
  63. // RadioWidgetsPage
  64. // ----------------------------------------------------------------------------
  65. class RadioWidgetsPage : public WidgetsPage
  66. {
  67. public:
  68. RadioWidgetsPage(WidgetsBookCtrl *book, wxImageList *imaglist);
  69. virtual ~RadioWidgetsPage(){};
  70. virtual wxControl *GetWidget() const { return m_radio; }
  71. virtual void RecreateWidget() { CreateRadio(); }
  72. // lazy creation of the content
  73. virtual void CreateContent();
  74. protected:
  75. // event handlers
  76. void OnCheckOrRadioBox(wxCommandEvent& event);
  77. void OnRadioBox(wxCommandEvent& event);
  78. void OnButtonReset(wxCommandEvent& event);
  79. void OnButtonRecreate(wxCommandEvent& event);
  80. void OnButtonSelection(wxCommandEvent& event);
  81. void OnButtonSetLabel(wxCommandEvent& event);
  82. void OnEnableItem(wxCommandEvent& event);
  83. void OnShowItem(wxCommandEvent& event);
  84. void OnUpdateUIReset(wxUpdateUIEvent& event);
  85. void OnUpdateUIUpdate(wxUpdateUIEvent& event);
  86. void OnUpdateUISelection(wxUpdateUIEvent& event);
  87. void OnUpdateUIEnableItem(wxUpdateUIEvent& event);
  88. void OnUpdateUIShowItem(wxUpdateUIEvent& event);
  89. // reset the wxRadioBox parameters
  90. void Reset();
  91. // (re)create the wxRadioBox
  92. void CreateRadio();
  93. // the controls
  94. // ------------
  95. // the check/radio boxes for styles
  96. wxCheckBox *m_chkSpecifyRows;
  97. wxCheckBox *m_chkEnableItem;
  98. wxCheckBox *m_chkShowItem;
  99. wxRadioBox *m_radioDir;
  100. // the gauge itself and the sizer it is in
  101. wxRadioBox *m_radio;
  102. wxSizer *m_sizerRadio;
  103. // the text entries for command parameters
  104. wxTextCtrl *m_textNumBtns,
  105. *m_textMajorDim,
  106. *m_textCurSel,
  107. *m_textSel,
  108. *m_textLabel,
  109. *m_textLabelBtns;
  110. private:
  111. wxDECLARE_EVENT_TABLE();
  112. DECLARE_WIDGETS_PAGE(RadioWidgetsPage)
  113. };
  114. // ----------------------------------------------------------------------------
  115. // event tables
  116. // ----------------------------------------------------------------------------
  117. wxBEGIN_EVENT_TABLE(RadioWidgetsPage, WidgetsPage)
  118. EVT_BUTTON(RadioPage_Reset, RadioWidgetsPage::OnButtonReset)
  119. EVT_BUTTON(RadioPage_Update, RadioWidgetsPage::OnButtonRecreate)
  120. EVT_BUTTON(RadioPage_LabelBtn, RadioWidgetsPage::OnButtonRecreate)
  121. EVT_BUTTON(RadioPage_Selection, RadioWidgetsPage::OnButtonSelection)
  122. EVT_BUTTON(RadioPage_Label, RadioWidgetsPage::OnButtonSetLabel)
  123. EVT_UPDATE_UI(RadioPage_Update, RadioWidgetsPage::OnUpdateUIUpdate)
  124. EVT_UPDATE_UI(RadioPage_Selection, RadioWidgetsPage::OnUpdateUISelection)
  125. EVT_RADIOBOX(RadioPage_Radio, RadioWidgetsPage::OnRadioBox)
  126. EVT_CHECKBOX(RadioPage_EnableItem, RadioWidgetsPage::OnEnableItem)
  127. EVT_CHECKBOX(RadioPage_ShowItem, RadioWidgetsPage::OnShowItem)
  128. EVT_UPDATE_UI(RadioPage_EnableItem, RadioWidgetsPage::OnUpdateUIEnableItem)
  129. EVT_UPDATE_UI(RadioPage_ShowItem, RadioWidgetsPage::OnUpdateUIShowItem)
  130. EVT_CHECKBOX(wxID_ANY, RadioWidgetsPage::OnCheckOrRadioBox)
  131. EVT_RADIOBOX(wxID_ANY, RadioWidgetsPage::OnCheckOrRadioBox)
  132. wxEND_EVENT_TABLE()
  133. // ============================================================================
  134. // implementation
  135. // ============================================================================
  136. #if defined(__WXUNIVERSAL__)
  137. #define FAMILY_CTRLS UNIVERSAL_CTRLS
  138. #else
  139. #define FAMILY_CTRLS NATIVE_CTRLS
  140. #endif
  141. IMPLEMENT_WIDGETS_PAGE(RadioWidgetsPage, wxT("Radio"),
  142. FAMILY_CTRLS | WITH_ITEMS_CTRLS
  143. );
  144. RadioWidgetsPage::RadioWidgetsPage(WidgetsBookCtrl *book,
  145. wxImageList *imaglist)
  146. : WidgetsPage(book, imaglist, radio_xpm)
  147. {
  148. // init everything
  149. m_chkSpecifyRows = (wxCheckBox *)NULL;
  150. m_chkEnableItem = (wxCheckBox *)NULL;
  151. m_chkShowItem = (wxCheckBox *)NULL;
  152. m_textNumBtns =
  153. m_textLabelBtns =
  154. m_textLabel = (wxTextCtrl *)NULL;
  155. m_radio =
  156. m_radioDir = (wxRadioBox *)NULL;
  157. m_sizerRadio = (wxSizer *)NULL;
  158. }
  159. void RadioWidgetsPage::CreateContent()
  160. {
  161. wxSizer *sizerTop = new wxBoxSizer(wxHORIZONTAL);
  162. // left pane
  163. wxStaticBox *box = new wxStaticBox(this, wxID_ANY, wxT("&Set style"));
  164. wxSizer *sizerLeft = new wxStaticBoxSizer(box, wxVERTICAL);
  165. m_chkSpecifyRows = CreateCheckBoxAndAddToSizer
  166. (
  167. sizerLeft,
  168. "Major specifies &rows count"
  169. );
  170. static const wxString layoutDir[] =
  171. {
  172. wxT("default"),
  173. wxT("left to right"),
  174. wxT("top to bottom")
  175. };
  176. m_radioDir = new wxRadioBox(this, wxID_ANY, wxT("Numbering:"),
  177. wxDefaultPosition, wxDefaultSize,
  178. WXSIZEOF(layoutDir), layoutDir,
  179. 1, wxRA_SPECIFY_COLS);
  180. sizerLeft->Add(m_radioDir, 0, wxGROW | wxALL, 5);
  181. // if it's not defined, we can't change the radiobox direction
  182. #ifndef wxRA_LEFTTORIGHT
  183. m_radioDir->Disable();
  184. #endif // wxRA_LEFTTORIGHT
  185. wxSizer *sizerRow;
  186. sizerRow = CreateSizerWithTextAndLabel(wxT("&Major dimension:"),
  187. wxID_ANY,
  188. &m_textMajorDim);
  189. sizerLeft->Add(sizerRow, 0, wxGROW | wxALL, 5);
  190. sizerRow = CreateSizerWithTextAndLabel(wxT("&Number of buttons:"),
  191. wxID_ANY,
  192. &m_textNumBtns);
  193. sizerLeft->Add(sizerRow, 0, wxGROW | wxALL, 5);
  194. wxButton *btn;
  195. btn = new wxButton(this, RadioPage_Update, wxT("&Update"));
  196. sizerLeft->Add(btn, 0, wxALIGN_CENTRE_HORIZONTAL | wxALL, 5);
  197. sizerLeft->Add(5, 5, 0, wxGROW | wxALL, 5); // spacer
  198. btn = new wxButton(this, RadioPage_Reset, wxT("&Reset"));
  199. sizerLeft->Add(btn, 0, wxALIGN_CENTRE_HORIZONTAL | wxALL, 15);
  200. // middle pane
  201. wxStaticBox *box2 = new wxStaticBox(this, wxID_ANY, wxT("&Change parameters"));
  202. wxSizer *sizerMiddle = new wxStaticBoxSizer(box2, wxVERTICAL);
  203. sizerRow = CreateSizerWithTextAndLabel(wxT("Current selection:"),
  204. wxID_ANY,
  205. &m_textCurSel);
  206. sizerMiddle->Add(sizerRow, 0, wxGROW | wxALL, 5);
  207. sizerRow = CreateSizerWithTextAndButton(RadioPage_Selection,
  208. wxT("&Change selection:"),
  209. wxID_ANY,
  210. &m_textSel);
  211. sizerMiddle->Add(sizerRow, 0, wxGROW | wxALL, 5);
  212. sizerRow = CreateSizerWithTextAndButton(RadioPage_Label,
  213. wxT("&Label for box:"),
  214. wxID_ANY,
  215. &m_textLabel);
  216. sizerMiddle->Add(sizerRow, 0, wxGROW | wxALL, 5);
  217. sizerRow = CreateSizerWithTextAndButton(RadioPage_LabelBtn,
  218. wxT("&Label for buttons:"),
  219. wxID_ANY,
  220. &m_textLabelBtns);
  221. sizerMiddle->Add(sizerRow, 0, wxGROW | wxALL, 5);
  222. m_chkEnableItem = CreateCheckBoxAndAddToSizer(sizerMiddle,
  223. wxT("Disable &2nd item"),
  224. RadioPage_EnableItem);
  225. m_chkShowItem = CreateCheckBoxAndAddToSizer(sizerMiddle,
  226. wxT("Hide 2nd &item"),
  227. RadioPage_ShowItem);
  228. // right pane
  229. wxSizer *sizerRight = new wxBoxSizer(wxHORIZONTAL);
  230. sizerRight->SetMinSize(150, 0);
  231. m_sizerRadio = sizerRight; // save it to modify it later
  232. Reset();
  233. CreateRadio();
  234. // the 3 panes panes compose the window
  235. sizerTop->Add(sizerLeft, 0, wxGROW | (wxALL & ~wxLEFT), 10);
  236. sizerTop->Add(sizerMiddle, 1, wxGROW | wxALL, 10);
  237. sizerTop->Add(sizerRight, 0, wxGROW | (wxALL & ~wxRIGHT), 10);
  238. // final initializations
  239. SetSizer(sizerTop);
  240. }
  241. // ----------------------------------------------------------------------------
  242. // operations
  243. // ----------------------------------------------------------------------------
  244. void RadioWidgetsPage::Reset()
  245. {
  246. m_textMajorDim->SetValue(wxString::Format(wxT("%u"), DEFAULT_MAJOR_DIM));
  247. m_textNumBtns->SetValue(wxString::Format(wxT("%u"), DEFAULT_NUM_ENTRIES));
  248. m_textLabel->SetValue(wxT("I'm a radiobox"));
  249. m_textLabelBtns->SetValue(wxT("item"));
  250. m_chkSpecifyRows->SetValue(false);
  251. m_chkEnableItem->SetValue(true);
  252. m_chkShowItem->SetValue(true);
  253. m_radioDir->SetSelection(RadioDir_Default);
  254. }
  255. void RadioWidgetsPage::CreateRadio()
  256. {
  257. int sel;
  258. if ( m_radio )
  259. {
  260. sel = m_radio->GetSelection();
  261. m_sizerRadio->Detach( m_radio );
  262. delete m_radio;
  263. }
  264. else // first time creation, no old selection to preserve
  265. {
  266. sel = -1;
  267. }
  268. unsigned long count;
  269. if ( !m_textNumBtns->GetValue().ToULong(&count) )
  270. {
  271. wxLogWarning(wxT("Should have a valid number for number of items."));
  272. // fall back to default
  273. count = DEFAULT_NUM_ENTRIES;
  274. }
  275. unsigned long majorDim;
  276. if ( !m_textMajorDim->GetValue().ToULong(&majorDim) )
  277. {
  278. wxLogWarning(wxT("Should have a valid major dimension number."));
  279. // fall back to default
  280. majorDim = DEFAULT_MAJOR_DIM;
  281. }
  282. wxString *items = new wxString[count];
  283. wxString labelBtn = m_textLabelBtns->GetValue();
  284. for ( size_t n = 0; n < count; n++ )
  285. {
  286. items[n] = wxString::Format(wxT("%s %lu"),
  287. labelBtn.c_str(), (unsigned long)n + 1);
  288. }
  289. int flags = m_chkSpecifyRows->GetValue() ? wxRA_SPECIFY_ROWS
  290. : wxRA_SPECIFY_COLS;
  291. flags |= ms_defaultFlags;
  292. #ifdef wxRA_LEFTTORIGHT
  293. switch ( m_radioDir->GetSelection() )
  294. {
  295. default:
  296. wxFAIL_MSG( wxT("unexpected wxRadioBox layout direction") );
  297. // fall through
  298. case RadioDir_Default:
  299. break;
  300. case RadioDir_LtoR:
  301. flags |= wxRA_LEFTTORIGHT;
  302. break;
  303. case RadioDir_TtoB:
  304. flags |= wxRA_TOPTOBOTTOM;
  305. break;
  306. }
  307. #endif // wxRA_LEFTTORIGHT
  308. m_radio = new wxRadioBox(this, RadioPage_Radio,
  309. m_textLabel->GetValue(),
  310. wxDefaultPosition, wxDefaultSize,
  311. count, items,
  312. majorDim,
  313. flags);
  314. delete [] items;
  315. if ( sel >= 0 && (size_t)sel < count )
  316. {
  317. m_radio->SetSelection(sel);
  318. }
  319. m_sizerRadio->Add(m_radio, 1, wxGROW);
  320. m_sizerRadio->Layout();
  321. m_chkEnableItem->SetValue(true);
  322. m_chkEnableItem->SetValue(true);
  323. }
  324. // ----------------------------------------------------------------------------
  325. // event handlers
  326. // ----------------------------------------------------------------------------
  327. void RadioWidgetsPage::OnButtonReset(wxCommandEvent& WXUNUSED(event))
  328. {
  329. Reset();
  330. CreateRadio();
  331. }
  332. void RadioWidgetsPage::OnCheckOrRadioBox(wxCommandEvent& WXUNUSED(event))
  333. {
  334. CreateRadio();
  335. }
  336. void RadioWidgetsPage::OnRadioBox(wxCommandEvent& event)
  337. {
  338. int sel = m_radio->GetSelection();
  339. int event_sel = event.GetSelection();
  340. wxUnusedVar(event_sel);
  341. wxLogMessage(wxT("Radiobox selection changed, now %d"), sel);
  342. wxASSERT_MSG( sel == event_sel,
  343. wxT("selection should be the same in event and radiobox") );
  344. m_textCurSel->SetValue(wxString::Format(wxT("%d"), sel));
  345. }
  346. void RadioWidgetsPage::OnButtonRecreate(wxCommandEvent& WXUNUSED(event))
  347. {
  348. CreateRadio();
  349. }
  350. void RadioWidgetsPage::OnButtonSetLabel(wxCommandEvent& WXUNUSED(event))
  351. {
  352. m_radio->wxControl::SetLabel(m_textLabel->GetValue());
  353. }
  354. void RadioWidgetsPage::OnButtonSelection(wxCommandEvent& WXUNUSED(event))
  355. {
  356. unsigned long sel;
  357. if ( !m_textSel->GetValue().ToULong(&sel) ||
  358. (sel >= (size_t)m_radio->GetCount()) )
  359. {
  360. wxLogWarning(wxT("Invalid number specified as new selection."));
  361. }
  362. else
  363. {
  364. m_radio->SetSelection(sel);
  365. }
  366. }
  367. void RadioWidgetsPage::OnEnableItem(wxCommandEvent& event)
  368. {
  369. m_radio->Enable(TEST_BUTTON, event.IsChecked());
  370. }
  371. void RadioWidgetsPage::OnShowItem(wxCommandEvent& event)
  372. {
  373. m_radio->Show(TEST_BUTTON, event.IsChecked());
  374. }
  375. void RadioWidgetsPage::OnUpdateUIUpdate(wxUpdateUIEvent& event)
  376. {
  377. unsigned long n;
  378. event.Enable( m_textNumBtns->GetValue().ToULong(&n) &&
  379. m_textMajorDim->GetValue().ToULong(&n) );
  380. }
  381. void RadioWidgetsPage::OnUpdateUISelection(wxUpdateUIEvent& event)
  382. {
  383. unsigned long n;
  384. event.Enable( m_textSel->GetValue().ToULong(&n) &&
  385. (n < (size_t)m_radio->GetCount()) );
  386. }
  387. void RadioWidgetsPage::OnUpdateUIReset(wxUpdateUIEvent& event)
  388. {
  389. // only enable it if something is not set to default
  390. bool enable = m_chkSpecifyRows->GetValue();
  391. if ( !enable )
  392. {
  393. unsigned long numEntries;
  394. enable = !m_textNumBtns->GetValue().ToULong(&numEntries) ||
  395. numEntries != DEFAULT_NUM_ENTRIES;
  396. if ( !enable )
  397. {
  398. unsigned long majorDim;
  399. enable = !m_textMajorDim->GetValue().ToULong(&majorDim) ||
  400. majorDim != DEFAULT_MAJOR_DIM;
  401. }
  402. }
  403. event.Enable(enable);
  404. }
  405. void RadioWidgetsPage::OnUpdateUIEnableItem(wxUpdateUIEvent& event)
  406. {
  407. event.SetText(m_radio->IsItemEnabled(TEST_BUTTON) ? wxT("Disable &2nd item")
  408. : wxT("Enable &2nd item"));
  409. }
  410. void RadioWidgetsPage::OnUpdateUIShowItem(wxUpdateUIEvent& event)
  411. {
  412. event.SetText(m_radio->IsItemShown(TEST_BUTTON) ? wxT("Hide 2nd &item")
  413. : wxT("Show 2nd &item"));
  414. }
  415. #endif // wxUSE_RADIOBOX