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