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(wxNotebook
*notebook
, wxImageList
*imaglist
);
77 virtual ~CheckBoxWidgetsPage(){};
81 void OnCheckBox(wxCommandEvent
& event
);
83 void OnStyleChange(wxCommandEvent
& event
);
84 void OnButtonReset(wxCommandEvent
& event
);
85 void OnButtonChangeLabel(wxCommandEvent
& event
);
87 void OnButtonCheck(wxCommandEvent
&) { m_checkbox
->SetValue(true); }
88 void OnButtonUncheck(wxCommandEvent
&) { m_checkbox
->SetValue(false); }
89 void OnButtonPartCheck(wxCommandEvent
&)
91 m_checkbox
->Set3StateValue(wxCHK_UNDETERMINED
);
94 void Is3State(wxUpdateUIEvent
& event
)
96 event
.Enable( m_checkbox
->Is3State() );
100 // reset the wxCheckBox parameters
103 // (re)create the wxCheckBox
104 void CreateCheckbox();
109 // the contols to choose the checkbox style
110 wxCheckBox
*m_chkRight
;
111 wxRadioBox
*m_radioKind
;
113 // the checkbox itself and the sizer it is in
114 wxCheckBox
*m_checkbox
;
115 wxSizer
*m_sizerCheckbox
;
117 // the text entries for command parameters
118 wxTextCtrl
*m_textLabel
;
121 DECLARE_EVENT_TABLE()
122 DECLARE_WIDGETS_PAGE(CheckBoxWidgetsPage
)
125 // ----------------------------------------------------------------------------
127 // ----------------------------------------------------------------------------
129 BEGIN_EVENT_TABLE(CheckBoxWidgetsPage
, WidgetsPage
)
130 EVT_CHECKBOX(CheckboxPage_Checkbox
, CheckBoxWidgetsPage::OnCheckBox
)
132 EVT_BUTTON(CheckboxPage_Reset
, CheckBoxWidgetsPage::OnButtonReset
)
133 EVT_BUTTON(CheckboxPage_ChangeLabel
, CheckBoxWidgetsPage::OnButtonChangeLabel
)
134 EVT_BUTTON(CheckboxPage_Check
, CheckBoxWidgetsPage::OnButtonCheck
)
135 EVT_BUTTON(CheckboxPage_Uncheck
, CheckBoxWidgetsPage::OnButtonUncheck
)
136 EVT_BUTTON(CheckboxPage_PartCheck
, CheckBoxWidgetsPage::OnButtonPartCheck
)
138 EVT_UPDATE_UI(CheckboxPage_PartCheck
, CheckBoxWidgetsPage::Is3State
)
140 EVT_RADIOBOX(wxID_ANY
, CheckBoxWidgetsPage::OnStyleChange
)
141 EVT_CHECKBOX(CheckboxPage_ChkRight
, CheckBoxWidgetsPage::OnStyleChange
)
144 // ============================================================================
146 // ============================================================================
148 IMPLEMENT_WIDGETS_PAGE(CheckBoxWidgetsPage
, wxT("CheckBox"));
150 CheckBoxWidgetsPage::CheckBoxWidgetsPage(wxNotebook
*notebook
,
151 wxImageList
*imaglist
)
152 : WidgetsPage(notebook
)
154 imaglist
->Add(wxBitmap(checkbox_xpm
));
156 wxSizer
*sizerTop
= new wxBoxSizer(wxHORIZONTAL
);
159 wxStaticBox
*box
= new wxStaticBox(this, wxID_ANY
, _T("&Set style"));
161 wxSizer
*sizerLeft
= new wxStaticBoxSizer(box
, wxVERTICAL
);
163 m_chkRight
= CreateCheckBoxAndAddToSizer
166 _T("&Right aligned"),
167 CheckboxPage_ChkRight
170 sizerLeft
->Add(5, 5, 0, wxGROW
| wxALL
, 5); // spacer
172 static const wxString kinds
[] =
174 _T("usual &2-state checkbox"),
175 _T("&3rd state settable by program"),
176 _T("&user-settable 3rd state"),
179 m_radioKind
= new wxRadioBox(this, wxID_ANY
, _T("&Kind"),
180 wxDefaultPosition
, wxDefaultSize
,
181 WXSIZEOF(kinds
), kinds
,
183 sizerLeft
->Add(m_radioKind
, 0, wxGROW
| wxALL
, 5);
184 wxButton
*btn
= new wxButton(this, CheckboxPage_Reset
, _T("&Reset"));
185 sizerLeft
->Add(btn
, 0, wxALIGN_CENTRE_HORIZONTAL
| wxALL
, 15);
188 wxStaticBox
*box2
= new wxStaticBox(this, wxID_ANY
, _T("&Operations"));
189 wxSizer
*sizerMiddle
= new wxStaticBoxSizer(box2
, wxVERTICAL
);
191 sizerMiddle
->Add(CreateSizerWithTextAndButton(CheckboxPage_ChangeLabel
,
195 0, wxALL
| wxGROW
, 5);
196 sizerMiddle
->Add(new wxButton(this, CheckboxPage_Check
, _T("&Check it")),
197 0, wxALL
| wxGROW
, 5);
198 sizerMiddle
->Add(new wxButton(this, CheckboxPage_Uncheck
, _T("&Uncheck it")),
199 0, wxALL
| wxGROW
, 5);
200 sizerMiddle
->Add(new wxButton(this, CheckboxPage_PartCheck
,
201 _T("Put in &3rd state")),
202 0, wxALL
| wxGROW
, 5);
205 wxSizer
*sizerRight
= new wxBoxSizer(wxHORIZONTAL
);
206 m_checkbox
= new wxCheckBox(this, CheckboxPage_Checkbox
, _T("&Check me!"));
207 sizerRight
->Add(0, 0, 1, wxCENTRE
);
208 sizerRight
->Add(m_checkbox
, 1, wxCENTRE
);
209 sizerRight
->Add(0, 0, 1, wxCENTRE
);
210 sizerRight
->SetMinSize(150, 0);
211 m_sizerCheckbox
= sizerRight
; // save it to modify it later
213 // the 3 panes panes compose the window
214 sizerTop
->Add(sizerLeft
, 0, wxGROW
| (wxALL
& ~wxLEFT
), 10);
215 sizerTop
->Add(sizerMiddle
, 1, wxGROW
| wxALL
, 10);
216 sizerTop
->Add(sizerRight
, 1, wxGROW
| (wxALL
& ~wxRIGHT
), 10);
218 // final initializations
226 void CheckBoxWidgetsPage::Reset()
228 m_chkRight
->SetValue(false);
229 m_radioKind
->SetSelection(CheckboxKind_2State
);
232 void CheckBoxWidgetsPage::CreateCheckbox()
234 wxString label
= m_checkbox
->GetLabel();
236 size_t count
= m_sizerCheckbox
->GetChildren().GetCount();
237 for ( size_t n
= 0; n
< count
; n
++ )
239 m_sizerCheckbox
->Remove(0);
245 if ( m_chkRight
->IsChecked() )
246 flags
|= wxALIGN_RIGHT
;
248 switch ( m_radioKind
->GetSelection() )
251 wxFAIL_MSG(_T("unexpected radiobox selection"));
254 case CheckboxKind_2State
:
255 flags
|= wxCHK_2STATE
;
258 case CheckboxKind_3StateUser
:
259 flags
|= wxCHK_ALLOW_3RD_STATE_FOR_USER
;
262 case CheckboxKind_3State
:
263 flags
|= wxCHK_3STATE
;
267 m_checkbox
= new wxCheckBox(this, CheckboxPage_Checkbox
, label
,
268 wxDefaultPosition
, wxDefaultSize
,
271 m_sizerCheckbox
->Add(0, 0, 1, wxCENTRE
);
272 m_sizerCheckbox
->Add(m_checkbox
, 1, wxCENTRE
);
273 m_sizerCheckbox
->Add(0, 0, 1, wxCENTRE
);
274 m_sizerCheckbox
->Layout();
277 // ----------------------------------------------------------------------------
279 // ----------------------------------------------------------------------------
281 void CheckBoxWidgetsPage::OnButtonReset(wxCommandEvent
& WXUNUSED(event
))
288 void CheckBoxWidgetsPage::OnStyleChange(wxCommandEvent
& WXUNUSED(event
))
293 void CheckBoxWidgetsPage::OnButtonChangeLabel(wxCommandEvent
& WXUNUSED(event
))
295 m_checkbox
->SetLabel(m_textLabel
->GetValue());
298 void CheckBoxWidgetsPage::OnCheckBox(wxCommandEvent
& event
)
300 wxLogMessage(_T("Test checkbox %schecked (value = %d)."),
301 event
.IsChecked() ? _T("") : _T("un"),
302 (int)m_checkbox
->Get3StateValue());