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