1 /////////////////////////////////////////////////////////////////////////////
2 // Program: wxWidgets Widgets Sample
4 // Purpose: Part of the widgets sample showing wxCheckBox
5 // Author: Dimitri Schoolwerth, Vadim Zeitlin
6 // Created: 27 Sep 2003
8 // Copyright: (c) 2003 wxWindows team
9 // License: wxWindows license
10 /////////////////////////////////////////////////////////////////////////////
12 // ============================================================================
14 // ============================================================================
16 // ----------------------------------------------------------------------------
18 // ----------------------------------------------------------------------------
20 // for compilers that support precompilation, includes "wx/wx.h".
21 #include "wx/wxprec.h"
27 // for all others, include the necessary headers
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"
44 #include "icons/checkbox.xpm"
46 // ----------------------------------------------------------------------------
48 // ----------------------------------------------------------------------------
53 CheckboxPage_Reset
= wxID_HIGHEST
,
54 CheckboxPage_ChangeLabel
,
57 CheckboxPage_PartCheck
,
58 CheckboxPage_ChkRight
,
66 CheckboxKind_3StateUser
69 // ----------------------------------------------------------------------------
70 // CheckBoxWidgetsPage
71 // ----------------------------------------------------------------------------
73 class CheckBoxWidgetsPage
: public WidgetsPage
76 CheckBoxWidgetsPage(WidgetsBookCtrl
*book
, wxImageList
*imaglist
);
77 virtual ~CheckBoxWidgetsPage(){};
79 virtual wxControl
*GetWidget() const { return m_checkbox
; }
80 virtual void RecreateWidget() { CreateCheckbox(); }
82 // lazy creation of the content
83 virtual void CreateContent();
87 void OnCheckBox(wxCommandEvent
& event
);
89 void OnStyleChange(wxCommandEvent
& event
);
90 void OnButtonReset(wxCommandEvent
& event
);
91 void OnButtonChangeLabel(wxCommandEvent
& event
);
93 void OnButtonCheck(wxCommandEvent
&) { m_checkbox
->SetValue(true); }
94 void OnButtonUncheck(wxCommandEvent
&) { m_checkbox
->SetValue(false); }
95 void OnButtonPartCheck(wxCommandEvent
&)
97 m_checkbox
->Set3StateValue(wxCHK_UNDETERMINED
);
100 void Is3State(wxUpdateUIEvent
& event
)
102 event
.Enable( m_checkbox
->Is3State() );
106 // reset the wxCheckBox parameters
109 // (re)create the wxCheckBox
110 void CreateCheckbox();
115 // the contols to choose the checkbox style
116 wxCheckBox
*m_chkRight
;
117 wxRadioBox
*m_radioKind
;
119 // the checkbox itself and the sizer it is in
120 wxCheckBox
*m_checkbox
;
121 wxSizer
*m_sizerCheckbox
;
123 // the text entries for command parameters
124 wxTextCtrl
*m_textLabel
;
127 DECLARE_EVENT_TABLE()
128 DECLARE_WIDGETS_PAGE(CheckBoxWidgetsPage
)
131 // ----------------------------------------------------------------------------
133 // ----------------------------------------------------------------------------
135 BEGIN_EVENT_TABLE(CheckBoxWidgetsPage
, WidgetsPage
)
136 EVT_CHECKBOX(CheckboxPage_Checkbox
, CheckBoxWidgetsPage::OnCheckBox
)
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
)
144 EVT_UPDATE_UI(CheckboxPage_PartCheck
, CheckBoxWidgetsPage::Is3State
)
146 EVT_RADIOBOX(wxID_ANY
, CheckBoxWidgetsPage::OnStyleChange
)
147 EVT_CHECKBOX(CheckboxPage_ChkRight
, CheckBoxWidgetsPage::OnStyleChange
)
150 // ============================================================================
152 // ============================================================================
154 #if defined(__WXUNIVERSAL__)
155 #define FAMILY_CTRLS UNIVERSAL_CTRLS
157 #define FAMILY_CTRLS NATIVE_CTRLS
160 IMPLEMENT_WIDGETS_PAGE(CheckBoxWidgetsPage
, wxT("CheckBox"), FAMILY_CTRLS
);
162 CheckBoxWidgetsPage::CheckBoxWidgetsPage(WidgetsBookCtrl
*book
,
163 wxImageList
*imaglist
)
164 : WidgetsPage(book
, imaglist
, checkbox_xpm
)
168 void CheckBoxWidgetsPage::CreateContent()
170 wxSizer
*sizerTop
= new wxBoxSizer(wxHORIZONTAL
);
173 wxStaticBox
*box
= new wxStaticBox(this, wxID_ANY
, _T("&Set style"));
175 wxSizer
*sizerLeft
= new wxStaticBoxSizer(box
, wxVERTICAL
);
177 m_chkRight
= CreateCheckBoxAndAddToSizer
180 _T("&Right aligned"),
181 CheckboxPage_ChkRight
184 sizerLeft
->Add(5, 5, 0, wxGROW
| wxALL
, 5); // spacer
186 static const wxString kinds
[] =
188 _T("usual &2-state checkbox"),
189 _T("&3rd state settable by program"),
190 _T("&user-settable 3rd state"),
193 m_radioKind
= new wxRadioBox(this, wxID_ANY
, _T("&Kind"),
194 wxDefaultPosition
, wxDefaultSize
,
195 WXSIZEOF(kinds
), kinds
,
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);
202 wxStaticBox
*box2
= new wxStaticBox(this, wxID_ANY
, _T("&Operations"));
203 wxSizer
*sizerMiddle
= new wxStaticBoxSizer(box2
, wxVERTICAL
);
205 sizerMiddle
->Add(CreateSizerWithTextAndButton(CheckboxPage_ChangeLabel
,
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);
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
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);
232 // final initializations
240 void CheckBoxWidgetsPage::Reset()
242 m_chkRight
->SetValue(false);
243 m_radioKind
->SetSelection(CheckboxKind_2State
);
246 void CheckBoxWidgetsPage::CreateCheckbox()
248 wxString label
= m_checkbox
->GetLabel();
250 size_t count
= m_sizerCheckbox
->GetChildren().GetCount();
251 for ( size_t n
= 0; n
< count
; n
++ )
253 m_sizerCheckbox
->Remove(0);
258 int flags
= ms_defaultFlags
;
259 if ( m_chkRight
->IsChecked() )
260 flags
|= wxALIGN_RIGHT
;
262 switch ( m_radioKind
->GetSelection() )
265 wxFAIL_MSG(_T("unexpected radiobox selection"));
268 case CheckboxKind_2State
:
269 flags
|= wxCHK_2STATE
;
272 case CheckboxKind_3StateUser
:
273 flags
|= wxCHK_ALLOW_3RD_STATE_FOR_USER
;
276 case CheckboxKind_3State
:
277 flags
|= wxCHK_3STATE
;
281 m_checkbox
= new wxCheckBox(this, CheckboxPage_Checkbox
, label
,
282 wxDefaultPosition
, wxDefaultSize
,
285 m_sizerCheckbox
->Add(0, 0, 1, wxCENTRE
);
286 m_sizerCheckbox
->Add(m_checkbox
, 1, wxCENTRE
);
287 m_sizerCheckbox
->Add(0, 0, 1, wxCENTRE
);
288 m_sizerCheckbox
->Layout();
291 // ----------------------------------------------------------------------------
293 // ----------------------------------------------------------------------------
295 void CheckBoxWidgetsPage::OnButtonReset(wxCommandEvent
& WXUNUSED(event
))
302 void CheckBoxWidgetsPage::OnStyleChange(wxCommandEvent
& WXUNUSED(event
))
307 void CheckBoxWidgetsPage::OnButtonChangeLabel(wxCommandEvent
& WXUNUSED(event
))
309 m_checkbox
->SetLabel(m_textLabel
->GetValue());
312 void CheckBoxWidgetsPage::OnCheckBox(wxCommandEvent
& event
)
314 wxLogMessage(_T("Test checkbox %schecked (value = %d)."),
315 event
.IsChecked() ? _T("") : _T("un"),
316 (int)m_checkbox
->Get3StateValue());