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