checkbox.cpp 9.5 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315
  1. /////////////////////////////////////////////////////////////////////////////
  2. // Program: wxWidgets Widgets Sample
  3. // Name: checkbox.cpp
  4. // Purpose: Part of the widgets sample showing wxCheckBox
  5. // Author: Dimitri Schoolwerth, Vadim Zeitlin
  6. // Created: 27 Sep 2003
  7. // Copyright: (c) 2003 wxWindows team
  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. // for all others, include the necessary headers
  22. #ifndef WX_PRECOMP
  23. #include "wx/app.h"
  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. #include "wx/sizer.h"
  32. #endif
  33. #include "widgets.h"
  34. #include "icons/checkbox.xpm"
  35. // ----------------------------------------------------------------------------
  36. // constants
  37. // ----------------------------------------------------------------------------
  38. // control ids
  39. enum
  40. {
  41. CheckboxPage_Reset = wxID_HIGHEST,
  42. CheckboxPage_ChangeLabel,
  43. CheckboxPage_Check,
  44. CheckboxPage_Uncheck,
  45. CheckboxPage_PartCheck,
  46. CheckboxPage_ChkRight,
  47. CheckboxPage_Checkbox
  48. };
  49. enum
  50. {
  51. CheckboxKind_2State,
  52. CheckboxKind_3State,
  53. CheckboxKind_3StateUser
  54. };
  55. // ----------------------------------------------------------------------------
  56. // CheckBoxWidgetsPage
  57. // ----------------------------------------------------------------------------
  58. class CheckBoxWidgetsPage : public WidgetsPage
  59. {
  60. public:
  61. CheckBoxWidgetsPage(WidgetsBookCtrl *book, wxImageList *imaglist);
  62. virtual ~CheckBoxWidgetsPage(){};
  63. virtual wxControl *GetWidget() const { return m_checkbox; }
  64. virtual void RecreateWidget() { CreateCheckbox(); }
  65. // lazy creation of the content
  66. virtual void CreateContent();
  67. protected:
  68. // event handlers
  69. void OnCheckBox(wxCommandEvent& event);
  70. void OnStyleChange(wxCommandEvent& event);
  71. void OnButtonReset(wxCommandEvent& event);
  72. void OnButtonChangeLabel(wxCommandEvent& event);
  73. void OnButtonCheck(wxCommandEvent&) { m_checkbox->SetValue(true); }
  74. void OnButtonUncheck(wxCommandEvent&) { m_checkbox->SetValue(false); }
  75. void OnButtonPartCheck(wxCommandEvent&)
  76. {
  77. m_checkbox->Set3StateValue(wxCHK_UNDETERMINED);
  78. }
  79. void Is3State(wxUpdateUIEvent& event)
  80. {
  81. event.Enable( m_checkbox->Is3State() );
  82. }
  83. // reset the wxCheckBox parameters
  84. void Reset();
  85. // (re)create the wxCheckBox
  86. void CreateCheckbox();
  87. // the controls
  88. // ------------
  89. // the controls to choose the checkbox style
  90. wxCheckBox *m_chkRight;
  91. wxRadioBox *m_radioKind;
  92. // the checkbox itself and the sizer it is in
  93. wxCheckBox *m_checkbox;
  94. wxSizer *m_sizerCheckbox;
  95. // the text entries for command parameters
  96. wxTextCtrl *m_textLabel;
  97. private:
  98. wxDECLARE_EVENT_TABLE();
  99. DECLARE_WIDGETS_PAGE(CheckBoxWidgetsPage)
  100. };
  101. // ----------------------------------------------------------------------------
  102. // event tables
  103. // ----------------------------------------------------------------------------
  104. wxBEGIN_EVENT_TABLE(CheckBoxWidgetsPage, WidgetsPage)
  105. EVT_CHECKBOX(CheckboxPage_Checkbox, CheckBoxWidgetsPage::OnCheckBox)
  106. EVT_BUTTON(CheckboxPage_Reset, CheckBoxWidgetsPage::OnButtonReset)
  107. EVT_BUTTON(CheckboxPage_ChangeLabel, CheckBoxWidgetsPage::OnButtonChangeLabel)
  108. EVT_BUTTON(CheckboxPage_Check, CheckBoxWidgetsPage::OnButtonCheck)
  109. EVT_BUTTON(CheckboxPage_Uncheck, CheckBoxWidgetsPage::OnButtonUncheck)
  110. EVT_BUTTON(CheckboxPage_PartCheck, CheckBoxWidgetsPage::OnButtonPartCheck)
  111. EVT_UPDATE_UI(CheckboxPage_PartCheck, CheckBoxWidgetsPage::Is3State)
  112. EVT_RADIOBOX(wxID_ANY, CheckBoxWidgetsPage::OnStyleChange)
  113. EVT_CHECKBOX(CheckboxPage_ChkRight, CheckBoxWidgetsPage::OnStyleChange)
  114. wxEND_EVENT_TABLE()
  115. // ============================================================================
  116. // implementation
  117. // ============================================================================
  118. #if defined(__WXUNIVERSAL__)
  119. #define FAMILY_CTRLS UNIVERSAL_CTRLS
  120. #else
  121. #define FAMILY_CTRLS NATIVE_CTRLS
  122. #endif
  123. IMPLEMENT_WIDGETS_PAGE(CheckBoxWidgetsPage, wxT("CheckBox"), FAMILY_CTRLS );
  124. CheckBoxWidgetsPage::CheckBoxWidgetsPage(WidgetsBookCtrl *book,
  125. wxImageList *imaglist)
  126. : WidgetsPage(book, imaglist, checkbox_xpm)
  127. {
  128. }
  129. void CheckBoxWidgetsPage::CreateContent()
  130. {
  131. wxSizer *sizerTop = new wxBoxSizer(wxHORIZONTAL);
  132. // left pane
  133. wxStaticBox *box = new wxStaticBox(this, wxID_ANY, wxT("&Set style"));
  134. wxSizer *sizerLeft = new wxStaticBoxSizer(box, wxVERTICAL);
  135. m_chkRight = CreateCheckBoxAndAddToSizer
  136. (
  137. sizerLeft,
  138. wxT("&Right aligned"),
  139. CheckboxPage_ChkRight
  140. );
  141. sizerLeft->Add(5, 5, 0, wxGROW | wxALL, 5); // spacer
  142. static const wxString kinds[] =
  143. {
  144. wxT("usual &2-state checkbox"),
  145. wxT("&3rd state settable by program"),
  146. wxT("&user-settable 3rd state"),
  147. };
  148. m_radioKind = new wxRadioBox(this, wxID_ANY, wxT("&Kind"),
  149. wxDefaultPosition, wxDefaultSize,
  150. WXSIZEOF(kinds), kinds,
  151. 1);
  152. sizerLeft->Add(m_radioKind, 0, wxGROW | wxALL, 5);
  153. wxButton *btn = new wxButton(this, CheckboxPage_Reset, wxT("&Reset"));
  154. sizerLeft->Add(btn, 0, wxALIGN_CENTRE_HORIZONTAL | wxALL, 15);
  155. // middle pane
  156. wxStaticBox *box2 = new wxStaticBox(this, wxID_ANY, wxT("&Operations"));
  157. wxSizer *sizerMiddle = new wxStaticBoxSizer(box2, wxVERTICAL);
  158. sizerMiddle->Add(CreateSizerWithTextAndButton(CheckboxPage_ChangeLabel,
  159. wxT("Change label"),
  160. wxID_ANY,
  161. &m_textLabel),
  162. 0, wxALL | wxGROW, 5);
  163. sizerMiddle->Add(new wxButton(this, CheckboxPage_Check, wxT("&Check it")),
  164. 0, wxALL | wxGROW, 5);
  165. sizerMiddle->Add(new wxButton(this, CheckboxPage_Uncheck, wxT("&Uncheck it")),
  166. 0, wxALL | wxGROW, 5);
  167. sizerMiddle->Add(new wxButton(this, CheckboxPage_PartCheck,
  168. wxT("Put in &3rd state")),
  169. 0, wxALL | wxGROW, 5);
  170. // right pane
  171. wxSizer *sizerRight = new wxBoxSizer(wxHORIZONTAL);
  172. m_checkbox = new wxCheckBox(this, CheckboxPage_Checkbox, wxT("&Check me!"));
  173. sizerRight->Add(0, 0, 1, wxCENTRE);
  174. sizerRight->Add(m_checkbox, 1, wxCENTRE);
  175. sizerRight->Add(0, 0, 1, wxCENTRE);
  176. sizerRight->SetMinSize(150, 0);
  177. m_sizerCheckbox = sizerRight; // save it to modify it later
  178. // the 3 panes panes compose the window
  179. sizerTop->Add(sizerLeft, 0, wxGROW | (wxALL & ~wxLEFT), 10);
  180. sizerTop->Add(sizerMiddle, 1, wxGROW | wxALL, 10);
  181. sizerTop->Add(sizerRight, 1, wxGROW | (wxALL & ~wxRIGHT), 10);
  182. // final initializations
  183. Reset();
  184. SetSizer(sizerTop);
  185. }
  186. void CheckBoxWidgetsPage::Reset()
  187. {
  188. m_chkRight->SetValue(false);
  189. m_radioKind->SetSelection(CheckboxKind_2State);
  190. }
  191. void CheckBoxWidgetsPage::CreateCheckbox()
  192. {
  193. wxString label = m_checkbox->GetLabel();
  194. size_t count = m_sizerCheckbox->GetChildren().GetCount();
  195. for ( size_t n = 0; n < count; n++ )
  196. {
  197. m_sizerCheckbox->Remove(0);
  198. }
  199. delete m_checkbox;
  200. int flags = ms_defaultFlags;
  201. if ( m_chkRight->IsChecked() )
  202. flags |= wxALIGN_RIGHT;
  203. switch ( m_radioKind->GetSelection() )
  204. {
  205. default:
  206. wxFAIL_MSG(wxT("unexpected radiobox selection"));
  207. // fall through
  208. case CheckboxKind_2State:
  209. flags |= wxCHK_2STATE;
  210. break;
  211. case CheckboxKind_3StateUser:
  212. flags |= wxCHK_ALLOW_3RD_STATE_FOR_USER;
  213. // fall through
  214. case CheckboxKind_3State:
  215. flags |= wxCHK_3STATE;
  216. break;
  217. }
  218. m_checkbox = new wxCheckBox(this, CheckboxPage_Checkbox, label,
  219. wxDefaultPosition, wxDefaultSize,
  220. flags);
  221. m_sizerCheckbox->Add(0, 0, 1, wxCENTRE);
  222. m_sizerCheckbox->Add(m_checkbox, 1, wxCENTRE);
  223. m_sizerCheckbox->Add(0, 0, 1, wxCENTRE);
  224. m_sizerCheckbox->Layout();
  225. }
  226. // ----------------------------------------------------------------------------
  227. // event handlers
  228. // ----------------------------------------------------------------------------
  229. void CheckBoxWidgetsPage::OnButtonReset(wxCommandEvent& WXUNUSED(event))
  230. {
  231. Reset();
  232. CreateCheckbox();
  233. }
  234. void CheckBoxWidgetsPage::OnStyleChange(wxCommandEvent& WXUNUSED(event))
  235. {
  236. CreateCheckbox();
  237. }
  238. void CheckBoxWidgetsPage::OnButtonChangeLabel(wxCommandEvent& WXUNUSED(event))
  239. {
  240. m_checkbox->SetLabel(m_textLabel->GetValue());
  241. }
  242. void CheckBoxWidgetsPage::OnCheckBox(wxCommandEvent& event)
  243. {
  244. wxLogMessage(wxT("Test checkbox %schecked (value = %d)."),
  245. event.IsChecked() ? wxT("") : wxT("un"),
  246. (int)m_checkbox->Get3StateValue());
  247. }