]>
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 | { | |
53 | CheckboxPage_Reset = 100, | |
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: | |
76 | CheckBoxWidgetsPage(wxNotebook *notebook, wxImageList *imaglist); | |
77 | virtual ~CheckBoxWidgetsPage(); | |
78 | ||
79 | protected: | |
80 | // event handlers | |
81 | void OnCheckBox(wxCommandEvent& event); | |
82 | ||
34ad2e8f VZ |
83 | void OnStyleChange(wxCommandEvent& event); |
84 | void OnButtonReset(wxCommandEvent& event); | |
85 | void OnButtonChangeLabel(wxCommandEvent& event); | |
86 | ||
87 | void OnButtonCheck(wxCommandEvent&) { m_checkbox->SetValue(true); } | |
88 | void OnButtonUncheck(wxCommandEvent&) { m_checkbox->SetValue(false); } | |
89 | void OnButtonPartCheck(wxCommandEvent&) | |
90 | { | |
91 | m_checkbox->Set3StateValue(wxCHK_UNDETERMINED); | |
92 | } | |
93 | ||
94 | void Is3State(wxUpdateUIEvent& event) | |
95 | { | |
96 | event.Enable( m_checkbox->Is3State() ); | |
97 | } | |
98 | ||
99 | ||
100 | // reset the wxCheckBox parameters | |
101 | void Reset(); | |
102 | ||
103 | // (re)create the wxCheckBox | |
104 | void CreateCheckbox(); | |
8941fa88 VZ |
105 | |
106 | // the controls | |
107 | // ------------ | |
108 | ||
34ad2e8f VZ |
109 | // the contols to choose the checkbox style |
110 | wxCheckBox *m_chkRight; | |
111 | wxRadioBox *m_radioKind; | |
8941fa88 | 112 | |
34ad2e8f VZ |
113 | // the checkbox itself and the sizer it is in |
114 | wxCheckBox *m_checkbox; | |
115 | wxSizer *m_sizerCheckbox; | |
116 | ||
117 | // the text entries for command parameters | |
118 | wxTextCtrl *m_textLabel; | |
8941fa88 VZ |
119 | |
120 | private: | |
121 | DECLARE_EVENT_TABLE() | |
122 | DECLARE_WIDGETS_PAGE(CheckBoxWidgetsPage) | |
123 | }; | |
124 | ||
125 | // ---------------------------------------------------------------------------- | |
126 | // event tables | |
127 | // ---------------------------------------------------------------------------- | |
128 | ||
129 | BEGIN_EVENT_TABLE(CheckBoxWidgetsPage, WidgetsPage) | |
34ad2e8f VZ |
130 | EVT_CHECKBOX(CheckboxPage_Checkbox, CheckBoxWidgetsPage::OnCheckBox) |
131 | ||
132 | EVT_BUTTON(CheckboxPage_Reset, CheckBoxWidgetsPage::OnButtonReset) | |
133 | EVT_BUTTON(CheckboxPage_ChangeLabel, CheckBoxWidgetsPage::OnButtonChangeLabel) | |
134 | EVT_BUTTON(CheckboxPage_Check, CheckBoxWidgetsPage::OnButtonCheck) | |
135 | EVT_BUTTON(CheckboxPage_Uncheck, CheckBoxWidgetsPage::OnButtonUncheck) | |
136 | EVT_BUTTON(CheckboxPage_PartCheck, CheckBoxWidgetsPage::OnButtonPartCheck) | |
137 | ||
138 | EVT_UPDATE_UI(CheckboxPage_PartCheck, CheckBoxWidgetsPage::Is3State) | |
139 | ||
140 | EVT_RADIOBOX(wxID_ANY, CheckBoxWidgetsPage::OnStyleChange) | |
141 | EVT_CHECKBOX(CheckboxPage_ChkRight, CheckBoxWidgetsPage::OnStyleChange) | |
8941fa88 VZ |
142 | END_EVENT_TABLE() |
143 | ||
144 | // ============================================================================ | |
145 | // implementation | |
146 | // ============================================================================ | |
147 | ||
148 | IMPLEMENT_WIDGETS_PAGE(CheckBoxWidgetsPage, wxT("CheckBox")); | |
149 | ||
150 | CheckBoxWidgetsPage::CheckBoxWidgetsPage(wxNotebook *notebook, | |
151 | wxImageList *imaglist) | |
152 | : WidgetsPage(notebook) | |
153 | { | |
154 | imaglist->Add(wxBitmap(checkbox_xpm)); | |
155 | ||
34ad2e8f | 156 | wxSizer *sizerTop = new wxBoxSizer(wxHORIZONTAL); |
8941fa88 | 157 | |
34ad2e8f VZ |
158 | // left pane |
159 | wxStaticBox *box = new wxStaticBox(this, wxID_ANY, _T("&Set style")); | |
160 | ||
161 | wxSizer *sizerLeft = new wxStaticBoxSizer(box, wxVERTICAL); | |
162 | ||
163 | m_chkRight = CreateCheckBoxAndAddToSizer | |
164 | ( | |
165 | sizerLeft, | |
166 | _T("&Right aligned"), | |
167 | CheckboxPage_ChkRight | |
168 | ); | |
169 | ||
170 | sizerLeft->Add(5, 5, 0, wxGROW | wxALL, 5); // spacer | |
171 | ||
172 | static const wxString kinds[] = | |
173 | { | |
174 | _T("usual &2-state checkbox"), | |
175 | _T("&3rd state settable by program"), | |
176 | _T("&user-settable 3rd state"), | |
177 | }; | |
8941fa88 | 178 | |
34ad2e8f VZ |
179 | m_radioKind = new wxRadioBox(this, wxID_ANY, _T("&Kind"), |
180 | wxDefaultPosition, wxDefaultSize, | |
181 | WXSIZEOF(kinds), kinds, | |
182 | 1); | |
183 | sizerLeft->Add(m_radioKind, 0, wxGROW | wxALL, 5); | |
184 | wxButton *btn = new wxButton(this, CheckboxPage_Reset, _T("&Reset")); | |
185 | sizerLeft->Add(btn, 0, wxALIGN_CENTRE_HORIZONTAL | wxALL, 15); | |
186 | ||
187 | // middle pane | |
188 | wxStaticBox *box2 = new wxStaticBox(this, wxID_ANY, _T("&Operations")); | |
189 | wxSizer *sizerMiddle = new wxStaticBoxSizer(box2, wxVERTICAL); | |
190 | ||
191 | sizerMiddle->Add(CreateSizerWithTextAndButton(CheckboxPage_ChangeLabel, | |
192 | _T("Change label"), | |
193 | wxID_ANY, | |
194 | &m_textLabel), | |
195 | 0, wxALL | wxGROW, 5); | |
196 | sizerMiddle->Add(new wxButton(this, CheckboxPage_Check, _T("&Check it")), | |
197 | 0, wxALL | wxGROW, 5); | |
198 | sizerMiddle->Add(new wxButton(this, CheckboxPage_Uncheck, _T("&Uncheck it")), | |
199 | 0, wxALL | wxGROW, 5); | |
200 | sizerMiddle->Add(new wxButton(this, CheckboxPage_PartCheck, | |
201 | _T("Put in &3rd state")), | |
202 | 0, wxALL | wxGROW, 5); | |
203 | ||
204 | // right pane | |
205 | wxSizer *sizerRight = new wxBoxSizer(wxHORIZONTAL); | |
206 | m_checkbox = new wxCheckBox(this, CheckboxPage_Checkbox, _T("&Check me!")); | |
207 | sizerRight->Add(0, 0, 1, wxCENTRE); | |
208 | sizerRight->Add(m_checkbox, 1, wxCENTRE); | |
209 | sizerRight->Add(0, 0, 1, wxCENTRE); | |
210 | sizerRight->SetMinSize(150, 0); | |
211 | m_sizerCheckbox = sizerRight; // save it to modify it later | |
212 | ||
213 | // the 3 panes panes compose the window | |
214 | sizerTop->Add(sizerLeft, 0, wxGROW | (wxALL & ~wxLEFT), 10); | |
215 | sizerTop->Add(sizerMiddle, 1, wxGROW | wxALL, 10); | |
216 | sizerTop->Add(sizerRight, 1, wxGROW | (wxALL & ~wxRIGHT), 10); | |
217 | ||
218 | // final initializations | |
219 | Reset(); | |
8941fa88 VZ |
220 | |
221 | SetSizer(sizerTop); | |
222 | ||
223 | sizerTop->Fit(this); | |
224 | } | |
225 | ||
226 | CheckBoxWidgetsPage::~CheckBoxWidgetsPage() | |
227 | { | |
228 | } | |
229 | ||
34ad2e8f VZ |
230 | void CheckBoxWidgetsPage::Reset() |
231 | { | |
232 | m_chkRight->SetValue(false); | |
233 | m_radioKind->SetSelection(CheckboxKind_2State); | |
234 | } | |
235 | ||
236 | void CheckBoxWidgetsPage::CreateCheckbox() | |
237 | { | |
238 | wxString label = m_checkbox->GetLabel(); | |
239 | ||
240 | size_t count = m_sizerCheckbox->GetChildren().GetCount(); | |
241 | for ( size_t n = 0; n < count; n++ ) | |
242 | { | |
243 | m_sizerCheckbox->Remove(0); | |
244 | } | |
245 | ||
246 | delete m_checkbox; | |
247 | ||
248 | int flags = 0; | |
249 | if ( m_chkRight->IsChecked() ) | |
250 | flags |= wxALIGN_RIGHT; | |
251 | ||
252 | switch ( m_radioKind->GetSelection() ) | |
253 | { | |
254 | default: | |
255 | wxFAIL_MSG(_T("unexpected radiobox selection")); | |
256 | // fall through | |
257 | ||
258 | case CheckboxKind_2State: | |
259 | flags |= wxCHK_2STATE; | |
260 | break; | |
261 | ||
262 | case CheckboxKind_3StateUser: | |
263 | flags |= wxCHK_ALLOW_3RD_STATE_FOR_USER; | |
264 | // fall through | |
265 | ||
266 | case CheckboxKind_3State: | |
267 | flags |= wxCHK_3STATE; | |
268 | break; | |
269 | } | |
270 | ||
271 | m_checkbox = new wxCheckBox(this, CheckboxPage_Checkbox, label, | |
272 | wxDefaultPosition, wxDefaultSize, | |
273 | flags); | |
274 | ||
275 | m_sizerCheckbox->Add(0, 0, 1, wxCENTRE); | |
276 | m_sizerCheckbox->Add(m_checkbox, 1, wxCENTRE); | |
277 | m_sizerCheckbox->Add(0, 0, 1, wxCENTRE); | |
278 | m_sizerCheckbox->Layout(); | |
279 | } | |
280 | ||
8941fa88 VZ |
281 | // ---------------------------------------------------------------------------- |
282 | // event handlers | |
283 | // ---------------------------------------------------------------------------- | |
284 | ||
34ad2e8f | 285 | void CheckBoxWidgetsPage::OnButtonReset(wxCommandEvent& WXUNUSED(event)) |
8941fa88 | 286 | { |
34ad2e8f | 287 | Reset(); |
8941fa88 | 288 | |
34ad2e8f VZ |
289 | CreateCheckbox(); |
290 | } | |
8941fa88 | 291 | |
34ad2e8f VZ |
292 | void CheckBoxWidgetsPage::OnStyleChange(wxCommandEvent& WXUNUSED(event)) |
293 | { | |
294 | CreateCheckbox(); | |
8941fa88 VZ |
295 | } |
296 | ||
34ad2e8f | 297 | void CheckBoxWidgetsPage::OnButtonChangeLabel(wxCommandEvent& WXUNUSED(event)) |
8941fa88 | 298 | { |
34ad2e8f | 299 | m_checkbox->SetLabel(m_textLabel->GetValue()); |
8941fa88 | 300 | } |
34ad2e8f VZ |
301 | |
302 | void CheckBoxWidgetsPage::OnCheckBox(wxCommandEvent& event) | |
303 | { | |
304 | wxLogMessage(_T("Test checkbox %schecked (value = %d)."), | |
305 | event.IsChecked() ? _T("") : _T("un"), | |
306 | (int)m_checkbox->Get3StateValue()); | |
307 | } | |
308 |