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