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
= 100,
54 CheckboxPage_ChangeLabel
,
57 CheckboxPage_PartCheck
,
58 CheckboxPage_ChkRight
,
66 CheckboxKind_3StateUser
69 // ----------------------------------------------------------------------------
70 // CheckBoxWidgetsPage
71 // ----------------------------------------------------------------------------
73 class CheckBoxWidgetsPage
: public WidgetsPage
76 CheckBoxWidgetsPage(wxBookCtrl
*book
, wxImageList
*imaglist
);
77 virtual ~CheckBoxWidgetsPage(){};
79 virtual wxControl
*GetWidget() const { return m_checkbox
; }
83 void OnCheckBox(wxCommandEvent
& event
);
85 void OnStyleChange(wxCommandEvent
& event
);
86 void OnButtonReset(wxCommandEvent
& event
);
87 void OnButtonChangeLabel(wxCommandEvent
& event
);
89 void OnButtonCheck(wxCommandEvent
&) { m_checkbox
->SetValue(true); }
90 void OnButtonUncheck(wxCommandEvent
&) { m_checkbox
->SetValue(false); }
91 void OnButtonPartCheck(wxCommandEvent
&)
93 m_checkbox
->Set3StateValue(wxCHK_UNDETERMINED
);
96 void Is3State(wxUpdateUIEvent
& event
)
98 event
.Enable( m_checkbox
->Is3State() );
102 // reset the wxCheckBox parameters
105 // (re)create the wxCheckBox
106 void CreateCheckbox();
111 // the contols to choose the checkbox style
112 wxCheckBox
*m_chkRight
;
113 wxRadioBox
*m_radioKind
;
115 // the checkbox itself and the sizer it is in
116 wxCheckBox
*m_checkbox
;
117 wxSizer
*m_sizerCheckbox
;
119 // the text entries for command parameters
120 wxTextCtrl
*m_textLabel
;
123 DECLARE_EVENT_TABLE()
124 DECLARE_WIDGETS_PAGE(CheckBoxWidgetsPage
)
127 // ----------------------------------------------------------------------------
129 // ----------------------------------------------------------------------------
131 BEGIN_EVENT_TABLE(CheckBoxWidgetsPage
, WidgetsPage
)
132 EVT_CHECKBOX(CheckboxPage_Checkbox
, CheckBoxWidgetsPage::OnCheckBox
)
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
)
140 EVT_UPDATE_UI(CheckboxPage_PartCheck
, CheckBoxWidgetsPage::Is3State
)
142 EVT_RADIOBOX(wxID_ANY
, CheckBoxWidgetsPage::OnStyleChange
)
143 EVT_CHECKBOX(CheckboxPage_ChkRight
, CheckBoxWidgetsPage::OnStyleChange
)
146 // ============================================================================
148 // ============================================================================
150 IMPLEMENT_WIDGETS_PAGE(CheckBoxWidgetsPage
, wxT("CheckBox"));
152 CheckBoxWidgetsPage::CheckBoxWidgetsPage(wxBookCtrl
*book
,
153 wxImageList
*imaglist
)
156 imaglist
->Add(wxBitmap(checkbox_xpm
));
158 wxSizer
*sizerTop
= new wxBoxSizer(wxHORIZONTAL
);
161 wxStaticBox
*box
= new wxStaticBox(this, wxID_ANY
, _T("&Set style"));
163 wxSizer
*sizerLeft
= new wxStaticBoxSizer(box
, wxVERTICAL
);
165 m_chkRight
= CreateCheckBoxAndAddToSizer
168 _T("&Right aligned"),
169 CheckboxPage_ChkRight
172 sizerLeft
->Add(5, 5, 0, wxGROW
| wxALL
, 5); // spacer
174 static const wxString kinds
[] =
176 _T("usual &2-state checkbox"),
177 _T("&3rd state settable by program"),
178 _T("&user-settable 3rd state"),
181 m_radioKind
= new wxRadioBox(this, wxID_ANY
, _T("&Kind"),
182 wxDefaultPosition
, wxDefaultSize
,
183 WXSIZEOF(kinds
), kinds
,
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);
190 wxStaticBox
*box2
= new wxStaticBox(this, wxID_ANY
, _T("&Operations"));
191 wxSizer
*sizerMiddle
= new wxStaticBoxSizer(box2
, wxVERTICAL
);
193 sizerMiddle
->Add(CreateSizerWithTextAndButton(CheckboxPage_ChangeLabel
,
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);
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
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);
220 // final initializations
228 void CheckBoxWidgetsPage::Reset()
230 m_chkRight
->SetValue(false);
231 m_radioKind
->SetSelection(CheckboxKind_2State
);
234 void CheckBoxWidgetsPage::CreateCheckbox()
236 wxString label
= m_checkbox
->GetLabel();
238 size_t count
= m_sizerCheckbox
->GetChildren().GetCount();
239 for ( size_t n
= 0; n
< count
; n
++ )
241 m_sizerCheckbox
->Remove(0);
247 if ( m_chkRight
->IsChecked() )
248 flags
|= wxALIGN_RIGHT
;
250 switch ( m_radioKind
->GetSelection() )
253 wxFAIL_MSG(_T("unexpected radiobox selection"));
256 case CheckboxKind_2State
:
257 flags
|= wxCHK_2STATE
;
260 case CheckboxKind_3StateUser
:
261 flags
|= wxCHK_ALLOW_3RD_STATE_FOR_USER
;
264 case CheckboxKind_3State
:
265 flags
|= wxCHK_3STATE
;
269 m_checkbox
= new wxCheckBox(this, CheckboxPage_Checkbox
, label
,
270 wxDefaultPosition
, wxDefaultSize
,
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();
279 // ----------------------------------------------------------------------------
281 // ----------------------------------------------------------------------------
283 void CheckBoxWidgetsPage::OnButtonReset(wxCommandEvent
& WXUNUSED(event
))
290 void CheckBoxWidgetsPage::OnStyleChange(wxCommandEvent
& WXUNUSED(event
))
295 void CheckBoxWidgetsPage::OnButtonChangeLabel(wxCommandEvent
& WXUNUSED(event
))
297 m_checkbox
->SetLabel(m_textLabel
->GetValue());
300 void CheckBoxWidgetsPage::OnCheckBox(wxCommandEvent
& event
)
302 wxLogMessage(_T("Test checkbox %schecked (value = %d)."),
303 event
.IsChecked() ? _T("") : _T("un"),
304 (int)m_checkbox
->Get3StateValue());