choice.cpp 14 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398399400401402403404405406407408409410411412413414415416417418419420421422423424425426427428429430431432433434435436437438439440441442443444445446447448449
  1. /////////////////////////////////////////////////////////////////////////////
  2. // Program: wxWidgets Widgets Sample
  3. // Name: choice.cpp
  4. // Purpose: Part of the widgets sample showing wxChoice
  5. // Created: 23.07.07
  6. // Licence: wxWindows licence
  7. /////////////////////////////////////////////////////////////////////////////
  8. // ============================================================================
  9. // declarations
  10. // ============================================================================
  11. // ----------------------------------------------------------------------------
  12. // headers
  13. // ----------------------------------------------------------------------------
  14. // for compilers that support precompilation, includes "wx/wx.h".
  15. #include "wx/wxprec.h"
  16. #ifdef __BORLANDC__
  17. #pragma hdrstop
  18. #endif
  19. #if wxUSE_CHOICE
  20. // for all others, include the necessary headers
  21. #ifndef WX_PRECOMP
  22. #include "wx/log.h"
  23. #include "wx/bitmap.h"
  24. #include "wx/button.h"
  25. #include "wx/checkbox.h"
  26. #include "wx/choice.h"
  27. #include "wx/combobox.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 "wx/checklst.h"
  34. #include "itemcontainer.h"
  35. #include "widgets.h"
  36. #include "icons/choice.xpm"
  37. // ----------------------------------------------------------------------------
  38. // constants
  39. // ----------------------------------------------------------------------------
  40. // control ids
  41. enum
  42. {
  43. ChoicePage_Reset = wxID_HIGHEST,
  44. ChoicePage_Add,
  45. ChoicePage_AddText,
  46. ChoicePage_AddSeveral,
  47. ChoicePage_AddMany,
  48. ChoicePage_Clear,
  49. ChoicePage_Change,
  50. ChoicePage_ChangeText,
  51. ChoicePage_Delete,
  52. ChoicePage_DeleteText,
  53. ChoicePage_DeleteSel,
  54. ChoicePage_Choice,
  55. ChoicePage_ContainerTests
  56. };
  57. // ----------------------------------------------------------------------------
  58. // ChoiceWidgetsPage
  59. // ----------------------------------------------------------------------------
  60. class ChoiceWidgetsPage : public ItemContainerWidgetsPage
  61. {
  62. public:
  63. ChoiceWidgetsPage(WidgetsBookCtrl *book, wxImageList *imaglist);
  64. virtual wxControl *GetWidget() const { return m_choice; }
  65. virtual wxItemContainer* GetContainer() const { return m_choice; }
  66. virtual void RecreateWidget() { CreateChoice(); }
  67. // lazy creation of the content
  68. virtual void CreateContent();
  69. protected:
  70. // event handlers
  71. void OnButtonReset(wxCommandEvent& event);
  72. void OnButtonChange(wxCommandEvent& event);
  73. void OnButtonDelete(wxCommandEvent& event);
  74. void OnButtonDeleteSel(wxCommandEvent& event);
  75. void OnButtonClear(wxCommandEvent& event);
  76. void OnButtonAdd(wxCommandEvent& event);
  77. void OnButtonAddSeveral(wxCommandEvent& event);
  78. void OnButtonAddMany(wxCommandEvent& event);
  79. void OnChoice(wxCommandEvent& event);
  80. void OnCheckOrRadioBox(wxCommandEvent& event);
  81. void OnUpdateUIAddSeveral(wxUpdateUIEvent& event);
  82. void OnUpdateUIClearButton(wxUpdateUIEvent& event);
  83. void OnUpdateUIDeleteButton(wxUpdateUIEvent& event);
  84. void OnUpdateUIDeleteSelButton(wxUpdateUIEvent& event);
  85. void OnUpdateUIResetButton(wxUpdateUIEvent& event);
  86. // reset the choice parameters
  87. void Reset();
  88. // (re)create the choice
  89. void CreateChoice();
  90. // should it be sorted?
  91. bool m_sorted;
  92. // the controls
  93. // ------------
  94. // the checkboxes
  95. wxCheckBox *m_chkSort;
  96. // the choice itself and the sizer it is in
  97. #ifdef __WXWINCE__
  98. wxChoiceBase
  99. #else
  100. wxChoice
  101. #endif
  102. *m_choice;
  103. wxSizer *m_sizerChoice;
  104. // the text entries for "Add/change string" and "Delete" buttons
  105. wxTextCtrl *m_textAdd,
  106. *m_textChange,
  107. *m_textDelete;
  108. private:
  109. wxDECLARE_EVENT_TABLE();
  110. DECLARE_WIDGETS_PAGE(ChoiceWidgetsPage)
  111. };
  112. // ----------------------------------------------------------------------------
  113. // event tables
  114. // ----------------------------------------------------------------------------
  115. wxBEGIN_EVENT_TABLE(ChoiceWidgetsPage, WidgetsPage)
  116. EVT_BUTTON(ChoicePage_Reset, ChoiceWidgetsPage::OnButtonReset)
  117. EVT_BUTTON(ChoicePage_Change, ChoiceWidgetsPage::OnButtonChange)
  118. EVT_BUTTON(ChoicePage_Delete, ChoiceWidgetsPage::OnButtonDelete)
  119. EVT_BUTTON(ChoicePage_DeleteSel, ChoiceWidgetsPage::OnButtonDeleteSel)
  120. EVT_BUTTON(ChoicePage_Clear, ChoiceWidgetsPage::OnButtonClear)
  121. EVT_BUTTON(ChoicePage_Add, ChoiceWidgetsPage::OnButtonAdd)
  122. EVT_BUTTON(ChoicePage_AddSeveral, ChoiceWidgetsPage::OnButtonAddSeveral)
  123. EVT_BUTTON(ChoicePage_AddMany, ChoiceWidgetsPage::OnButtonAddMany)
  124. EVT_BUTTON(ChoicePage_ContainerTests, ItemContainerWidgetsPage::OnButtonTestItemContainer)
  125. EVT_TEXT_ENTER(ChoicePage_AddText, ChoiceWidgetsPage::OnButtonAdd)
  126. EVT_TEXT_ENTER(ChoicePage_DeleteText, ChoiceWidgetsPage::OnButtonDelete)
  127. EVT_UPDATE_UI(ChoicePage_Reset, ChoiceWidgetsPage::OnUpdateUIResetButton)
  128. EVT_UPDATE_UI(ChoicePage_AddSeveral, ChoiceWidgetsPage::OnUpdateUIAddSeveral)
  129. EVT_UPDATE_UI(ChoicePage_Clear, ChoiceWidgetsPage::OnUpdateUIClearButton)
  130. EVT_UPDATE_UI(ChoicePage_DeleteText, ChoiceWidgetsPage::OnUpdateUIClearButton)
  131. EVT_UPDATE_UI(ChoicePage_Delete, ChoiceWidgetsPage::OnUpdateUIDeleteButton)
  132. EVT_UPDATE_UI(ChoicePage_Change, ChoiceWidgetsPage::OnUpdateUIDeleteSelButton)
  133. EVT_UPDATE_UI(ChoicePage_ChangeText, ChoiceWidgetsPage::OnUpdateUIDeleteSelButton)
  134. EVT_UPDATE_UI(ChoicePage_DeleteSel, ChoiceWidgetsPage::OnUpdateUIDeleteSelButton)
  135. EVT_CHOICE(ChoicePage_Choice, ChoiceWidgetsPage::OnChoice)
  136. EVT_CHECKBOX(wxID_ANY, ChoiceWidgetsPage::OnCheckOrRadioBox)
  137. EVT_RADIOBOX(wxID_ANY, ChoiceWidgetsPage::OnCheckOrRadioBox)
  138. wxEND_EVENT_TABLE()
  139. // ============================================================================
  140. // implementation
  141. // ============================================================================
  142. #if defined(__WXUNIVERSAL__)
  143. #define FAMILY_CTRLS UNIVERSAL_CTRLS
  144. #else
  145. #define FAMILY_CTRLS NATIVE_CTRLS
  146. #endif
  147. IMPLEMENT_WIDGETS_PAGE(ChoiceWidgetsPage, wxT("Choice"),
  148. FAMILY_CTRLS | WITH_ITEMS_CTRLS
  149. );
  150. ChoiceWidgetsPage::ChoiceWidgetsPage(WidgetsBookCtrl *book,
  151. wxImageList *imaglist)
  152. : ItemContainerWidgetsPage(book, imaglist, choice_xpm)
  153. {
  154. // init everything
  155. m_chkSort = (wxCheckBox *)NULL;
  156. m_choice = NULL;
  157. m_sizerChoice = (wxSizer *)NULL;
  158. }
  159. void ChoiceWidgetsPage::CreateContent()
  160. {
  161. /*
  162. What we create here is a frame having 3 panes: style pane is the
  163. leftmost one, in the middle the pane with buttons allowing to perform
  164. miscellaneous choice operations and the pane containing the choice
  165. itself to the right
  166. */
  167. wxSizer *sizerTop = new wxBoxSizer(wxHORIZONTAL);
  168. // left pane
  169. wxStaticBox *box = new wxStaticBox(this, wxID_ANY,
  170. wxT("&Set choice parameters"));
  171. wxSizer *sizerLeft = new wxStaticBoxSizer(box, wxVERTICAL);
  172. static const wxString modes[] =
  173. {
  174. wxT("single"),
  175. wxT("extended"),
  176. wxT("multiple"),
  177. };
  178. m_chkSort = CreateCheckBoxAndAddToSizer(sizerLeft, wxT("&Sort items"));
  179. wxButton *btn = new wxButton(this, ChoicePage_Reset, wxT("&Reset"));
  180. sizerLeft->Add(btn, 0, wxALIGN_CENTRE_HORIZONTAL | wxALL, 15);
  181. // middle pane
  182. wxStaticBox *box2 = new wxStaticBox(this, wxID_ANY,
  183. wxT("&Change choice contents"));
  184. wxSizer *sizerMiddle = new wxStaticBoxSizer(box2, wxVERTICAL);
  185. wxSizer *sizerRow = new wxBoxSizer(wxHORIZONTAL);
  186. btn = new wxButton(this, ChoicePage_Add, wxT("&Add this string"));
  187. m_textAdd = new wxTextCtrl(this, ChoicePage_AddText, wxT("test item 0"));
  188. sizerRow->Add(btn, 0, wxRIGHT, 5);
  189. sizerRow->Add(m_textAdd, 1, wxLEFT, 5);
  190. sizerMiddle->Add(sizerRow, 0, wxALL | wxGROW, 5);
  191. btn = new wxButton(this, ChoicePage_AddSeveral, wxT("&Insert a few strings"));
  192. sizerMiddle->Add(btn, 0, wxALL | wxGROW, 5);
  193. btn = new wxButton(this, ChoicePage_AddMany, wxT("Add &many strings"));
  194. sizerMiddle->Add(btn, 0, wxALL | wxGROW, 5);
  195. sizerRow = new wxBoxSizer(wxHORIZONTAL);
  196. btn = new wxButton(this, ChoicePage_Change, wxT("C&hange current"));
  197. m_textChange = new wxTextCtrl(this, ChoicePage_ChangeText, wxEmptyString);
  198. sizerRow->Add(btn, 0, wxRIGHT, 5);
  199. sizerRow->Add(m_textChange, 1, wxLEFT, 5);
  200. sizerMiddle->Add(sizerRow, 0, wxALL | wxGROW, 5);
  201. sizerRow = new wxBoxSizer(wxHORIZONTAL);
  202. btn = new wxButton(this, ChoicePage_Delete, wxT("&Delete this item"));
  203. m_textDelete = new wxTextCtrl(this, ChoicePage_DeleteText, wxEmptyString);
  204. sizerRow->Add(btn, 0, wxRIGHT, 5);
  205. sizerRow->Add(m_textDelete, 1, wxLEFT, 5);
  206. sizerMiddle->Add(sizerRow, 0, wxALL | wxGROW, 5);
  207. btn = new wxButton(this, ChoicePage_DeleteSel, wxT("Delete &selection"));
  208. sizerMiddle->Add(btn, 0, wxALL | wxGROW, 5);
  209. btn = new wxButton(this, ChoicePage_Clear, wxT("&Clear"));
  210. sizerMiddle->Add(btn, 0, wxALL | wxGROW, 5);
  211. btn = new wxButton(this, ChoicePage_ContainerTests, wxT("Run &tests"));
  212. sizerMiddle->Add(btn, 0, wxALL | wxGROW, 5);
  213. // right pane
  214. wxSizer *sizerRight = new wxBoxSizer(wxVERTICAL);
  215. m_choice = new wxChoice(this, ChoicePage_Choice);
  216. sizerRight->Add(m_choice, 0, wxALL | wxGROW, 5);
  217. sizerRight->SetMinSize(150, 0);
  218. m_sizerChoice = sizerRight; // save it to modify it later
  219. // the 3 panes panes compose the window
  220. sizerTop->Add(sizerLeft, 0, wxGROW | (wxALL & ~wxLEFT), 10);
  221. sizerTop->Add(sizerMiddle, 1, wxGROW | wxALL, 10);
  222. sizerTop->Add(sizerRight, 1, wxGROW | (wxALL & ~wxRIGHT), 10);
  223. // final initializations
  224. Reset();
  225. SetSizer(sizerTop);
  226. }
  227. // ----------------------------------------------------------------------------
  228. // operations
  229. // ----------------------------------------------------------------------------
  230. void ChoiceWidgetsPage::Reset()
  231. {
  232. m_chkSort->SetValue(false);
  233. }
  234. void ChoiceWidgetsPage::CreateChoice()
  235. {
  236. int flags = ms_defaultFlags;
  237. if ( m_chkSort->GetValue() )
  238. flags |= wxCB_SORT;
  239. wxArrayString items;
  240. if ( m_choice )
  241. {
  242. int count = m_choice->GetCount();
  243. for ( int n = 0; n < count; n++ )
  244. {
  245. items.Add(m_choice->GetString(n));
  246. }
  247. m_sizerChoice->Detach( m_choice );
  248. delete m_choice;
  249. }
  250. m_choice = new wxChoice(this, ChoicePage_Choice,
  251. wxDefaultPosition, wxDefaultSize,
  252. 0, NULL,
  253. flags);
  254. m_choice->Set(items);
  255. m_sizerChoice->Add(m_choice, 1, wxGROW | wxALL, 5);
  256. m_sizerChoice->Layout();
  257. }
  258. // ----------------------------------------------------------------------------
  259. // event handlers
  260. // ----------------------------------------------------------------------------
  261. void ChoiceWidgetsPage::OnButtonReset(wxCommandEvent& WXUNUSED(event))
  262. {
  263. Reset();
  264. CreateChoice();
  265. }
  266. void ChoiceWidgetsPage::OnButtonChange(wxCommandEvent& WXUNUSED(event))
  267. {
  268. int selection = m_choice->GetSelection();
  269. if(selection != wxNOT_FOUND)
  270. {
  271. m_choice->SetString(selection, m_textChange->GetValue());
  272. }
  273. }
  274. void ChoiceWidgetsPage::OnButtonDelete(wxCommandEvent& WXUNUSED(event))
  275. {
  276. unsigned long n;
  277. if ( !m_textDelete->GetValue().ToULong(&n) ||
  278. (n >= (unsigned)m_choice->GetCount()) )
  279. {
  280. return;
  281. }
  282. m_choice->Delete(n);
  283. }
  284. void ChoiceWidgetsPage::OnButtonDeleteSel(wxCommandEvent& WXUNUSED(event))
  285. {
  286. int selection = m_choice->GetSelection();
  287. if(selection != wxNOT_FOUND)
  288. {
  289. m_choice->Delete(selection);
  290. }
  291. }
  292. void ChoiceWidgetsPage::OnButtonClear(wxCommandEvent& WXUNUSED(event))
  293. {
  294. m_choice->Clear();
  295. }
  296. void ChoiceWidgetsPage::OnButtonAdd(wxCommandEvent& WXUNUSED(event))
  297. {
  298. static unsigned int s_item = 0;
  299. wxString s = m_textAdd->GetValue();
  300. if ( !m_textAdd->IsModified() )
  301. {
  302. // update the default string
  303. m_textAdd->SetValue(wxString::Format(wxT("test item %u"), ++s_item));
  304. }
  305. m_choice->Append(s);
  306. }
  307. void ChoiceWidgetsPage::OnButtonAddMany(wxCommandEvent& WXUNUSED(event))
  308. {
  309. // "many" means 1000 here
  310. wxArrayString strings;
  311. for ( unsigned int n = 0; n < 1000; n++ )
  312. {
  313. strings.Add(wxString::Format(wxT("item #%u"), n));
  314. }
  315. m_choice->Append(strings);
  316. }
  317. void ChoiceWidgetsPage::OnButtonAddSeveral(wxCommandEvent& WXUNUSED(event))
  318. {
  319. wxArrayString items;
  320. items.Add(wxT("First"));
  321. items.Add(wxT("another one"));
  322. items.Add(wxT("and the last (very very very very very very very very very very long) one"));
  323. m_choice->Insert(items, 0);
  324. }
  325. void ChoiceWidgetsPage::OnUpdateUIResetButton(wxUpdateUIEvent& event)
  326. {
  327. event.Enable( m_chkSort->GetValue() );
  328. }
  329. void ChoiceWidgetsPage::OnUpdateUIDeleteButton(wxUpdateUIEvent& event)
  330. {
  331. unsigned long n;
  332. event.Enable(m_textDelete->GetValue().ToULong(&n) &&
  333. (n < (unsigned)m_choice->GetCount()));
  334. }
  335. void ChoiceWidgetsPage::OnUpdateUIDeleteSelButton(wxUpdateUIEvent& event)
  336. {
  337. wxArrayInt selections;
  338. event.Enable(m_choice->GetSelection() != wxNOT_FOUND);
  339. }
  340. void ChoiceWidgetsPage::OnUpdateUIClearButton(wxUpdateUIEvent& event)
  341. {
  342. event.Enable(m_choice->GetCount() != 0);
  343. }
  344. void ChoiceWidgetsPage::OnUpdateUIAddSeveral(wxUpdateUIEvent& event)
  345. {
  346. event.Enable(!m_choice->HasFlag(wxCB_SORT));
  347. }
  348. void ChoiceWidgetsPage::OnChoice(wxCommandEvent& event)
  349. {
  350. long sel = event.GetSelection();
  351. m_textDelete->SetValue(wxString::Format(wxT("%ld"), sel));
  352. wxLogMessage(wxT("Choice item %ld selected"), sel);
  353. }
  354. void ChoiceWidgetsPage::OnCheckOrRadioBox(wxCommandEvent& WXUNUSED(event))
  355. {
  356. CreateChoice();
  357. }
  358. #endif // wxUSE_CHOICE