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"
41 #include "icons/checkbox.xpm"
43 // ----------------------------------------------------------------------------
45 // ----------------------------------------------------------------------------
50 CheckboxPage_Reset
= 100,
51 CheckboxPage_ChangeLabel
,
54 CheckboxPage_PartCheck
,
55 CheckboxPage_ChkRight
,
63 CheckboxKind_3StateUser
66 // ----------------------------------------------------------------------------
67 // CheckBoxWidgetsPage
68 // ----------------------------------------------------------------------------
70 class CheckBoxWidgetsPage
: public WidgetsPage
73 CheckBoxWidgetsPage(wxNotebook
*notebook
, wxImageList
*imaglist
);
74 virtual ~CheckBoxWidgetsPage();
78 void OnCheckBox(wxCommandEvent
& event
);
80 void OnStyleChange(wxCommandEvent
& event
);
81 void OnButtonReset(wxCommandEvent
& event
);
82 void OnButtonChangeLabel(wxCommandEvent
& event
);
84 void OnButtonCheck(wxCommandEvent
&) { m_checkbox
->SetValue(true); }
85 void OnButtonUncheck(wxCommandEvent
&) { m_checkbox
->SetValue(false); }
86 void OnButtonPartCheck(wxCommandEvent
&)
88 m_checkbox
->Set3StateValue(wxCHK_UNDETERMINED
);
91 void Is3State(wxUpdateUIEvent
& event
)
93 event
.Enable( m_checkbox
->Is3State() );
97 // reset the wxCheckBox parameters
100 // (re)create the wxCheckBox
101 void CreateCheckbox();
106 // the contols to choose the checkbox style
107 wxCheckBox
*m_chkRight
;
108 wxRadioBox
*m_radioKind
;
110 // the checkbox itself and the sizer it is in
111 wxCheckBox
*m_checkbox
;
112 wxSizer
*m_sizerCheckbox
;
114 // the text entries for command parameters
115 wxTextCtrl
*m_textLabel
;
118 DECLARE_EVENT_TABLE()
119 DECLARE_WIDGETS_PAGE(CheckBoxWidgetsPage
)
122 // ----------------------------------------------------------------------------
124 // ----------------------------------------------------------------------------
126 BEGIN_EVENT_TABLE(CheckBoxWidgetsPage
, WidgetsPage
)
127 EVT_CHECKBOX(CheckboxPage_Checkbox
, CheckBoxWidgetsPage::OnCheckBox
)
129 EVT_BUTTON(CheckboxPage_Reset
, CheckBoxWidgetsPage::OnButtonReset
)
130 EVT_BUTTON(CheckboxPage_ChangeLabel
, CheckBoxWidgetsPage::OnButtonChangeLabel
)
131 EVT_BUTTON(CheckboxPage_Check
, CheckBoxWidgetsPage::OnButtonCheck
)
132 EVT_BUTTON(CheckboxPage_Uncheck
, CheckBoxWidgetsPage::OnButtonUncheck
)
133 EVT_BUTTON(CheckboxPage_PartCheck
, CheckBoxWidgetsPage::OnButtonPartCheck
)
135 EVT_UPDATE_UI(CheckboxPage_PartCheck
, CheckBoxWidgetsPage::Is3State
)
137 EVT_RADIOBOX(wxID_ANY
, CheckBoxWidgetsPage::OnStyleChange
)
138 EVT_CHECKBOX(CheckboxPage_ChkRight
, CheckBoxWidgetsPage::OnStyleChange
)
141 // ============================================================================
143 // ============================================================================
145 IMPLEMENT_WIDGETS_PAGE(CheckBoxWidgetsPage
, wxT("CheckBox"));
147 CheckBoxWidgetsPage::CheckBoxWidgetsPage(wxNotebook
*notebook
,
148 wxImageList
*imaglist
)
149 : WidgetsPage(notebook
)
151 imaglist
->Add(wxBitmap(checkbox_xpm
));
153 wxSizer
*sizerTop
= new wxBoxSizer(wxHORIZONTAL
);
156 wxStaticBox
*box
= new wxStaticBox(this, wxID_ANY
, _T("&Set style"));
158 wxSizer
*sizerLeft
= new wxStaticBoxSizer(box
, wxVERTICAL
);
160 m_chkRight
= CreateCheckBoxAndAddToSizer
163 _T("&Right aligned"),
164 CheckboxPage_ChkRight
167 sizerLeft
->Add(5, 5, 0, wxGROW
| wxALL
, 5); // spacer
169 static const wxString kinds
[] =
171 _T("usual &2-state checkbox"),
172 _T("&3rd state settable by program"),
173 _T("&user-settable 3rd state"),
176 m_radioKind
= new wxRadioBox(this, wxID_ANY
, _T("&Kind"),
177 wxDefaultPosition
, wxDefaultSize
,
178 WXSIZEOF(kinds
), kinds
,
180 sizerLeft
->Add(m_radioKind
, 0, wxGROW
| wxALL
, 5);
181 wxButton
*btn
= new wxButton(this, CheckboxPage_Reset
, _T("&Reset"));
182 sizerLeft
->Add(btn
, 0, wxALIGN_CENTRE_HORIZONTAL
| wxALL
, 15);
185 wxStaticBox
*box2
= new wxStaticBox(this, wxID_ANY
, _T("&Operations"));
186 wxSizer
*sizerMiddle
= new wxStaticBoxSizer(box2
, wxVERTICAL
);
188 sizerMiddle
->Add(CreateSizerWithTextAndButton(CheckboxPage_ChangeLabel
,
192 0, wxALL
| wxGROW
, 5);
193 sizerMiddle
->Add(new wxButton(this, CheckboxPage_Check
, _T("&Check it")),
194 0, wxALL
| wxGROW
, 5);
195 sizerMiddle
->Add(new wxButton(this, CheckboxPage_Uncheck
, _T("&Uncheck it")),
196 0, wxALL
| wxGROW
, 5);
197 sizerMiddle
->Add(new wxButton(this, CheckboxPage_PartCheck
,
198 _T("Put in &3rd state")),
199 0, wxALL
| wxGROW
, 5);
202 wxSizer
*sizerRight
= new wxBoxSizer(wxHORIZONTAL
);
203 m_checkbox
= new wxCheckBox(this, CheckboxPage_Checkbox
, _T("&Check me!"));
204 sizerRight
->Add(0, 0, 1, wxCENTRE
);
205 sizerRight
->Add(m_checkbox
, 1, wxCENTRE
);
206 sizerRight
->Add(0, 0, 1, wxCENTRE
);
207 sizerRight
->SetMinSize(150, 0);
208 m_sizerCheckbox
= sizerRight
; // save it to modify it later
210 // the 3 panes panes compose the window
211 sizerTop
->Add(sizerLeft
, 0, wxGROW
| (wxALL
& ~wxLEFT
), 10);
212 sizerTop
->Add(sizerMiddle
, 1, wxGROW
| wxALL
, 10);
213 sizerTop
->Add(sizerRight
, 1, wxGROW
| (wxALL
& ~wxRIGHT
), 10);
215 // final initializations
223 CheckBoxWidgetsPage::~CheckBoxWidgetsPage()
227 void CheckBoxWidgetsPage::Reset()
229 m_chkRight
->SetValue(false);
230 m_radioKind
->SetSelection(CheckboxKind_2State
);
233 void CheckBoxWidgetsPage::CreateCheckbox()
235 wxString label
= m_checkbox
->GetLabel();
237 size_t count
= m_sizerCheckbox
->GetChildren().GetCount();
238 for ( size_t n
= 0; n
< count
; n
++ )
240 m_sizerCheckbox
->Remove(0);
246 if ( m_chkRight
->IsChecked() )
247 flags
|= wxALIGN_RIGHT
;
249 switch ( m_radioKind
->GetSelection() )
252 wxFAIL_MSG(_T("unexpected radiobox selection"));
255 case CheckboxKind_2State
:
256 flags
|= wxCHK_2STATE
;
259 case CheckboxKind_3StateUser
:
260 flags
|= wxCHK_ALLOW_3RD_STATE_FOR_USER
;
263 case CheckboxKind_3State
:
264 flags
|= wxCHK_3STATE
;
268 m_checkbox
= new wxCheckBox(this, CheckboxPage_Checkbox
, label
,
269 wxDefaultPosition
, wxDefaultSize
,
272 m_sizerCheckbox
->Add(0, 0, 1, wxCENTRE
);
273 m_sizerCheckbox
->Add(m_checkbox
, 1, wxCENTRE
);
274 m_sizerCheckbox
->Add(0, 0, 1, wxCENTRE
);
275 m_sizerCheckbox
->Layout();
278 // ----------------------------------------------------------------------------
280 // ----------------------------------------------------------------------------
282 void CheckBoxWidgetsPage::OnButtonReset(wxCommandEvent
& WXUNUSED(event
))
289 void CheckBoxWidgetsPage::OnStyleChange(wxCommandEvent
& WXUNUSED(event
))
294 void CheckBoxWidgetsPage::OnButtonChangeLabel(wxCommandEvent
& WXUNUSED(event
))
296 m_checkbox
->SetLabel(m_textLabel
->GetValue());
299 void CheckBoxWidgetsPage::OnCheckBox(wxCommandEvent
& event
)
301 wxLogMessage(_T("Test checkbox %schecked (value = %d)."),
302 event
.IsChecked() ? _T("") : _T("un"),
303 (int)m_checkbox
->Get3StateValue());