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