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
7 // Copyright: (c) 2003 wxWindows team
8 // Licence: wxWindows licence
9 /////////////////////////////////////////////////////////////////////////////
11 // ============================================================================
13 // ============================================================================
15 // ----------------------------------------------------------------------------
17 // ----------------------------------------------------------------------------
19 // for compilers that support precompilation, includes "wx/wx.h".
20 #include "wx/wxprec.h"
26 // for all others, include the necessary headers
31 #include "wx/bitmap.h"
32 #include "wx/button.h"
33 #include "wx/checkbox.h"
34 #include "wx/radiobox.h"
35 #include "wx/statbox.h"
36 #include "wx/textctrl.h"
43 #include "icons/checkbox.xpm"
45 // ----------------------------------------------------------------------------
47 // ----------------------------------------------------------------------------
52 CheckboxPage_Reset
= wxID_HIGHEST
,
53 CheckboxPage_ChangeLabel
,
56 CheckboxPage_PartCheck
,
57 CheckboxPage_ChkRight
,
65 CheckboxKind_3StateUser
68 // ----------------------------------------------------------------------------
69 // CheckBoxWidgetsPage
70 // ----------------------------------------------------------------------------
72 class CheckBoxWidgetsPage
: public WidgetsPage
75 CheckBoxWidgetsPage(WidgetsBookCtrl
*book
, wxImageList
*imaglist
);
76 virtual ~CheckBoxWidgetsPage(){};
78 virtual wxControl
*GetWidget() const { return m_checkbox
; }
79 virtual void RecreateWidget() { CreateCheckbox(); }
81 // lazy creation of the content
82 virtual void CreateContent();
86 void OnCheckBox(wxCommandEvent
& event
);
88 void OnStyleChange(wxCommandEvent
& event
);
89 void OnButtonReset(wxCommandEvent
& event
);
90 void OnButtonChangeLabel(wxCommandEvent
& event
);
92 void OnButtonCheck(wxCommandEvent
&) { m_checkbox
->SetValue(true); }
93 void OnButtonUncheck(wxCommandEvent
&) { m_checkbox
->SetValue(false); }
94 void OnButtonPartCheck(wxCommandEvent
&)
96 m_checkbox
->Set3StateValue(wxCHK_UNDETERMINED
);
99 void Is3State(wxUpdateUIEvent
& event
)
101 event
.Enable( m_checkbox
->Is3State() );
105 // reset the wxCheckBox parameters
108 // (re)create the wxCheckBox
109 void CreateCheckbox();
114 // the controls to choose the checkbox style
115 wxCheckBox
*m_chkRight
;
116 wxRadioBox
*m_radioKind
;
118 // the checkbox itself and the sizer it is in
119 wxCheckBox
*m_checkbox
;
120 wxSizer
*m_sizerCheckbox
;
122 // the text entries for command parameters
123 wxTextCtrl
*m_textLabel
;
126 DECLARE_EVENT_TABLE()
127 DECLARE_WIDGETS_PAGE(CheckBoxWidgetsPage
)
130 // ----------------------------------------------------------------------------
132 // ----------------------------------------------------------------------------
134 BEGIN_EVENT_TABLE(CheckBoxWidgetsPage
, WidgetsPage
)
135 EVT_CHECKBOX(CheckboxPage_Checkbox
, CheckBoxWidgetsPage::OnCheckBox
)
137 EVT_BUTTON(CheckboxPage_Reset
, CheckBoxWidgetsPage::OnButtonReset
)
138 EVT_BUTTON(CheckboxPage_ChangeLabel
, CheckBoxWidgetsPage::OnButtonChangeLabel
)
139 EVT_BUTTON(CheckboxPage_Check
, CheckBoxWidgetsPage::OnButtonCheck
)
140 EVT_BUTTON(CheckboxPage_Uncheck
, CheckBoxWidgetsPage::OnButtonUncheck
)
141 EVT_BUTTON(CheckboxPage_PartCheck
, CheckBoxWidgetsPage::OnButtonPartCheck
)
143 EVT_UPDATE_UI(CheckboxPage_PartCheck
, CheckBoxWidgetsPage::Is3State
)
145 EVT_RADIOBOX(wxID_ANY
, CheckBoxWidgetsPage::OnStyleChange
)
146 EVT_CHECKBOX(CheckboxPage_ChkRight
, CheckBoxWidgetsPage::OnStyleChange
)
149 // ============================================================================
151 // ============================================================================
153 #if defined(__WXUNIVERSAL__)
154 #define FAMILY_CTRLS UNIVERSAL_CTRLS
156 #define FAMILY_CTRLS NATIVE_CTRLS
159 IMPLEMENT_WIDGETS_PAGE(CheckBoxWidgetsPage
, wxT("CheckBox"), FAMILY_CTRLS
);
161 CheckBoxWidgetsPage::CheckBoxWidgetsPage(WidgetsBookCtrl
*book
,
162 wxImageList
*imaglist
)
163 : WidgetsPage(book
, imaglist
, checkbox_xpm
)
167 void CheckBoxWidgetsPage::CreateContent()
169 wxSizer
*sizerTop
= new wxBoxSizer(wxHORIZONTAL
);
172 wxStaticBox
*box
= new wxStaticBox(this, wxID_ANY
, wxT("&Set style"));
174 wxSizer
*sizerLeft
= new wxStaticBoxSizer(box
, wxVERTICAL
);
176 m_chkRight
= CreateCheckBoxAndAddToSizer
179 wxT("&Right aligned"),
180 CheckboxPage_ChkRight
183 sizerLeft
->Add(5, 5, 0, wxGROW
| wxALL
, 5); // spacer
185 static const wxString kinds
[] =
187 wxT("usual &2-state checkbox"),
188 wxT("&3rd state settable by program"),
189 wxT("&user-settable 3rd state"),
192 m_radioKind
= new wxRadioBox(this, wxID_ANY
, wxT("&Kind"),
193 wxDefaultPosition
, wxDefaultSize
,
194 WXSIZEOF(kinds
), kinds
,
196 sizerLeft
->Add(m_radioKind
, 0, wxGROW
| wxALL
, 5);
197 wxButton
*btn
= new wxButton(this, CheckboxPage_Reset
, wxT("&Reset"));
198 sizerLeft
->Add(btn
, 0, wxALIGN_CENTRE_HORIZONTAL
| wxALL
, 15);
201 wxStaticBox
*box2
= new wxStaticBox(this, wxID_ANY
, wxT("&Operations"));
202 wxSizer
*sizerMiddle
= new wxStaticBoxSizer(box2
, wxVERTICAL
);
204 sizerMiddle
->Add(CreateSizerWithTextAndButton(CheckboxPage_ChangeLabel
,
208 0, wxALL
| wxGROW
, 5);
209 sizerMiddle
->Add(new wxButton(this, CheckboxPage_Check
, wxT("&Check it")),
210 0, wxALL
| wxGROW
, 5);
211 sizerMiddle
->Add(new wxButton(this, CheckboxPage_Uncheck
, wxT("&Uncheck it")),
212 0, wxALL
| wxGROW
, 5);
213 sizerMiddle
->Add(new wxButton(this, CheckboxPage_PartCheck
,
214 wxT("Put in &3rd state")),
215 0, wxALL
| wxGROW
, 5);
218 wxSizer
*sizerRight
= new wxBoxSizer(wxHORIZONTAL
);
219 m_checkbox
= new wxCheckBox(this, CheckboxPage_Checkbox
, wxT("&Check me!"));
220 sizerRight
->Add(0, 0, 1, wxCENTRE
);
221 sizerRight
->Add(m_checkbox
, 1, wxCENTRE
);
222 sizerRight
->Add(0, 0, 1, wxCENTRE
);
223 sizerRight
->SetMinSize(150, 0);
224 m_sizerCheckbox
= sizerRight
; // save it to modify it later
226 // the 3 panes panes compose the window
227 sizerTop
->Add(sizerLeft
, 0, wxGROW
| (wxALL
& ~wxLEFT
), 10);
228 sizerTop
->Add(sizerMiddle
, 1, wxGROW
| wxALL
, 10);
229 sizerTop
->Add(sizerRight
, 1, wxGROW
| (wxALL
& ~wxRIGHT
), 10);
231 // final initializations
237 void CheckBoxWidgetsPage::Reset()
239 m_chkRight
->SetValue(false);
240 m_radioKind
->SetSelection(CheckboxKind_2State
);
243 void CheckBoxWidgetsPage::CreateCheckbox()
245 wxString label
= m_checkbox
->GetLabel();
247 size_t count
= m_sizerCheckbox
->GetChildren().GetCount();
248 for ( size_t n
= 0; n
< count
; n
++ )
250 m_sizerCheckbox
->Remove(0);
255 int flags
= ms_defaultFlags
;
256 if ( m_chkRight
->IsChecked() )
257 flags
|= wxALIGN_RIGHT
;
259 switch ( m_radioKind
->GetSelection() )
262 wxFAIL_MSG(wxT("unexpected radiobox selection"));
265 case CheckboxKind_2State
:
266 flags
|= wxCHK_2STATE
;
269 case CheckboxKind_3StateUser
:
270 flags
|= wxCHK_ALLOW_3RD_STATE_FOR_USER
;
273 case CheckboxKind_3State
:
274 flags
|= wxCHK_3STATE
;
278 m_checkbox
= new wxCheckBox(this, CheckboxPage_Checkbox
, label
,
279 wxDefaultPosition
, wxDefaultSize
,
282 m_sizerCheckbox
->Add(0, 0, 1, wxCENTRE
);
283 m_sizerCheckbox
->Add(m_checkbox
, 1, wxCENTRE
);
284 m_sizerCheckbox
->Add(0, 0, 1, wxCENTRE
);
285 m_sizerCheckbox
->Layout();
288 // ----------------------------------------------------------------------------
290 // ----------------------------------------------------------------------------
292 void CheckBoxWidgetsPage::OnButtonReset(wxCommandEvent
& WXUNUSED(event
))
299 void CheckBoxWidgetsPage::OnStyleChange(wxCommandEvent
& WXUNUSED(event
))
304 void CheckBoxWidgetsPage::OnButtonChangeLabel(wxCommandEvent
& WXUNUSED(event
))
306 m_checkbox
->SetLabel(m_textLabel
->GetValue());
309 void CheckBoxWidgetsPage::OnCheckBox(wxCommandEvent
& event
)
311 wxLogMessage(wxT("Test checkbox %schecked (value = %d)."),
312 event
.IsChecked() ? wxT("") : wxT("un"),
313 (int)m_checkbox
->Get3StateValue());