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