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