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