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 // Licence: wxWindows licence
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 controls 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
, wxT("&Set style"));
175 wxSizer
*sizerLeft
= new wxStaticBoxSizer(box
, wxVERTICAL
);
177 m_chkRight
= CreateCheckBoxAndAddToSizer
180 wxT("&Right aligned"),
181 CheckboxPage_ChkRight
184 sizerLeft
->Add(5, 5, 0, wxGROW
| wxALL
, 5); // spacer
186 static const wxString kinds
[] =
188 wxT("usual &2-state checkbox"),
189 wxT("&3rd state settable by program"),
190 wxT("&user-settable 3rd state"),
193 m_radioKind
= new wxRadioBox(this, wxID_ANY
, wxT("&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
, wxT("&Reset"));
199 sizerLeft
->Add(btn
, 0, wxALIGN_CENTRE_HORIZONTAL
| wxALL
, 15);
202 wxStaticBox
*box2
= new wxStaticBox(this, wxID_ANY
, wxT("&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
, wxT("&Check it")),
211 0, wxALL
| wxGROW
, 5);
212 sizerMiddle
->Add(new wxButton(this, CheckboxPage_Uncheck
, wxT("&Uncheck it")),
213 0, wxALL
| wxGROW
, 5);
214 sizerMiddle
->Add(new wxButton(this, CheckboxPage_PartCheck
,
215 wxT("Put in &3rd state")),
216 0, wxALL
| wxGROW
, 5);
219 wxSizer
*sizerRight
= new wxBoxSizer(wxHORIZONTAL
);
220 m_checkbox
= new wxCheckBox(this, CheckboxPage_Checkbox
, wxT("&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
238 void CheckBoxWidgetsPage::Reset()
240 m_chkRight
->SetValue(false);
241 m_radioKind
->SetSelection(CheckboxKind_2State
);
244 void CheckBoxWidgetsPage::CreateCheckbox()
246 wxString label
= m_checkbox
->GetLabel();
248 size_t count
= m_sizerCheckbox
->GetChildren().GetCount();
249 for ( size_t n
= 0; n
< count
; n
++ )
251 m_sizerCheckbox
->Remove(0);
256 int flags
= ms_defaultFlags
;
257 if ( m_chkRight
->IsChecked() )
258 flags
|= wxALIGN_RIGHT
;
260 switch ( m_radioKind
->GetSelection() )
263 wxFAIL_MSG(wxT("unexpected radiobox selection"));
266 case CheckboxKind_2State
:
267 flags
|= wxCHK_2STATE
;
270 case CheckboxKind_3StateUser
:
271 flags
|= wxCHK_ALLOW_3RD_STATE_FOR_USER
;
274 case CheckboxKind_3State
:
275 flags
|= wxCHK_3STATE
;
279 m_checkbox
= new wxCheckBox(this, CheckboxPage_Checkbox
, label
,
280 wxDefaultPosition
, wxDefaultSize
,
283 m_sizerCheckbox
->Add(0, 0, 1, wxCENTRE
);
284 m_sizerCheckbox
->Add(m_checkbox
, 1, wxCENTRE
);
285 m_sizerCheckbox
->Add(0, 0, 1, wxCENTRE
);
286 m_sizerCheckbox
->Layout();
289 // ----------------------------------------------------------------------------
291 // ----------------------------------------------------------------------------
293 void CheckBoxWidgetsPage::OnButtonReset(wxCommandEvent
& WXUNUSED(event
))
300 void CheckBoxWidgetsPage::OnStyleChange(wxCommandEvent
& WXUNUSED(event
))
305 void CheckBoxWidgetsPage::OnButtonChangeLabel(wxCommandEvent
& WXUNUSED(event
))
307 m_checkbox
->SetLabel(m_textLabel
->GetValue());
310 void CheckBoxWidgetsPage::OnCheckBox(wxCommandEvent
& event
)
312 wxLogMessage(wxT("Test checkbox %schecked (value = %d)."),
313 event
.IsChecked() ? wxT("") : wxT("un"),
314 (int)m_checkbox
->Get3StateValue());