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(WidgetsBookCtrl
*book
, wxImageList
*imaglist
);
77 virtual ~CheckBoxWidgetsPage(){};
79 virtual wxControl
*GetWidget() const { return m_checkbox
; }
80 virtual void RecreateWidget() { CreateCheckbox(); }
84 void OnCheckBox(wxCommandEvent
& event
);
86 void OnStyleChange(wxCommandEvent
& event
);
87 void OnButtonReset(wxCommandEvent
& event
);
88 void OnButtonChangeLabel(wxCommandEvent
& event
);
90 void OnButtonCheck(wxCommandEvent
&) { m_checkbox
->SetValue(true); }
91 void OnButtonUncheck(wxCommandEvent
&) { m_checkbox
->SetValue(false); }
92 void OnButtonPartCheck(wxCommandEvent
&)
94 m_checkbox
->Set3StateValue(wxCHK_UNDETERMINED
);
97 void Is3State(wxUpdateUIEvent
& event
)
99 event
.Enable( m_checkbox
->Is3State() );
103 // reset the wxCheckBox parameters
106 // (re)create the wxCheckBox
107 void CreateCheckbox();
112 // the contols to choose the checkbox style
113 wxCheckBox
*m_chkRight
;
114 wxRadioBox
*m_radioKind
;
116 // the checkbox itself and the sizer it is in
117 wxCheckBox
*m_checkbox
;
118 wxSizer
*m_sizerCheckbox
;
120 // the text entries for command parameters
121 wxTextCtrl
*m_textLabel
;
124 DECLARE_EVENT_TABLE()
125 DECLARE_WIDGETS_PAGE(CheckBoxWidgetsPage
)
128 // ----------------------------------------------------------------------------
130 // ----------------------------------------------------------------------------
132 BEGIN_EVENT_TABLE(CheckBoxWidgetsPage
, WidgetsPage
)
133 EVT_CHECKBOX(CheckboxPage_Checkbox
, CheckBoxWidgetsPage::OnCheckBox
)
135 EVT_BUTTON(CheckboxPage_Reset
, CheckBoxWidgetsPage::OnButtonReset
)
136 EVT_BUTTON(CheckboxPage_ChangeLabel
, CheckBoxWidgetsPage::OnButtonChangeLabel
)
137 EVT_BUTTON(CheckboxPage_Check
, CheckBoxWidgetsPage::OnButtonCheck
)
138 EVT_BUTTON(CheckboxPage_Uncheck
, CheckBoxWidgetsPage::OnButtonUncheck
)
139 EVT_BUTTON(CheckboxPage_PartCheck
, CheckBoxWidgetsPage::OnButtonPartCheck
)
141 EVT_UPDATE_UI(CheckboxPage_PartCheck
, CheckBoxWidgetsPage::Is3State
)
143 EVT_RADIOBOX(wxID_ANY
, CheckBoxWidgetsPage::OnStyleChange
)
144 EVT_CHECKBOX(CheckboxPage_ChkRight
, CheckBoxWidgetsPage::OnStyleChange
)
147 // ============================================================================
149 // ============================================================================
151 IMPLEMENT_WIDGETS_PAGE(CheckBoxWidgetsPage
, wxT("CheckBox"),
152 (int)wxPlatform(GENERIC_CTRLS
).If(wxMSW
,NATIVE_CTRLS
)
155 CheckBoxWidgetsPage::CheckBoxWidgetsPage(WidgetsBookCtrl
*book
,
156 wxImageList
*imaglist
)
159 imaglist
->Add(wxBitmap(checkbox_xpm
));
161 wxSizer
*sizerTop
= new wxBoxSizer(wxHORIZONTAL
);
164 wxStaticBox
*box
= new wxStaticBox(this, wxID_ANY
, _T("&Set style"));
166 wxSizer
*sizerLeft
= new wxStaticBoxSizer(box
, wxVERTICAL
);
168 m_chkRight
= CreateCheckBoxAndAddToSizer
171 _T("&Right aligned"),
172 CheckboxPage_ChkRight
175 sizerLeft
->Add(5, 5, 0, wxGROW
| wxALL
, 5); // spacer
177 static const wxString kinds
[] =
179 _T("usual &2-state checkbox"),
180 _T("&3rd state settable by program"),
181 _T("&user-settable 3rd state"),
184 m_radioKind
= new wxRadioBox(this, wxID_ANY
, _T("&Kind"),
185 wxDefaultPosition
, wxDefaultSize
,
186 WXSIZEOF(kinds
), kinds
,
188 sizerLeft
->Add(m_radioKind
, 0, wxGROW
| wxALL
, 5);
189 wxButton
*btn
= new wxButton(this, CheckboxPage_Reset
, _T("&Reset"));
190 sizerLeft
->Add(btn
, 0, wxALIGN_CENTRE_HORIZONTAL
| wxALL
, 15);
193 wxStaticBox
*box2
= new wxStaticBox(this, wxID_ANY
, _T("&Operations"));
194 wxSizer
*sizerMiddle
= new wxStaticBoxSizer(box2
, wxVERTICAL
);
196 sizerMiddle
->Add(CreateSizerWithTextAndButton(CheckboxPage_ChangeLabel
,
200 0, wxALL
| wxGROW
, 5);
201 sizerMiddle
->Add(new wxButton(this, CheckboxPage_Check
, _T("&Check it")),
202 0, wxALL
| wxGROW
, 5);
203 sizerMiddle
->Add(new wxButton(this, CheckboxPage_Uncheck
, _T("&Uncheck it")),
204 0, wxALL
| wxGROW
, 5);
205 sizerMiddle
->Add(new wxButton(this, CheckboxPage_PartCheck
,
206 _T("Put in &3rd state")),
207 0, wxALL
| wxGROW
, 5);
210 wxSizer
*sizerRight
= new wxBoxSizer(wxHORIZONTAL
);
211 m_checkbox
= new wxCheckBox(this, CheckboxPage_Checkbox
, _T("&Check me!"));
212 sizerRight
->Add(0, 0, 1, wxCENTRE
);
213 sizerRight
->Add(m_checkbox
, 1, wxCENTRE
);
214 sizerRight
->Add(0, 0, 1, wxCENTRE
);
215 sizerRight
->SetMinSize(150, 0);
216 m_sizerCheckbox
= sizerRight
; // save it to modify it later
218 // the 3 panes panes compose the window
219 sizerTop
->Add(sizerLeft
, 0, wxGROW
| (wxALL
& ~wxLEFT
), 10);
220 sizerTop
->Add(sizerMiddle
, 1, wxGROW
| wxALL
, 10);
221 sizerTop
->Add(sizerRight
, 1, wxGROW
| (wxALL
& ~wxRIGHT
), 10);
223 // final initializations
231 void CheckBoxWidgetsPage::Reset()
233 m_chkRight
->SetValue(false);
234 m_radioKind
->SetSelection(CheckboxKind_2State
);
237 void CheckBoxWidgetsPage::CreateCheckbox()
239 wxString label
= m_checkbox
->GetLabel();
241 size_t count
= m_sizerCheckbox
->GetChildren().GetCount();
242 for ( size_t n
= 0; n
< count
; n
++ )
244 m_sizerCheckbox
->Remove(0);
249 int flags
= ms_defaultFlags
;
250 if ( m_chkRight
->IsChecked() )
251 flags
|= wxALIGN_RIGHT
;
253 switch ( m_radioKind
->GetSelection() )
256 wxFAIL_MSG(_T("unexpected radiobox selection"));
259 case CheckboxKind_2State
:
260 flags
|= wxCHK_2STATE
;
263 case CheckboxKind_3StateUser
:
264 flags
|= wxCHK_ALLOW_3RD_STATE_FOR_USER
;
267 case CheckboxKind_3State
:
268 flags
|= wxCHK_3STATE
;
272 m_checkbox
= new wxCheckBox(this, CheckboxPage_Checkbox
, label
,
273 wxDefaultPosition
, wxDefaultSize
,
276 m_sizerCheckbox
->Add(0, 0, 1, wxCENTRE
);
277 m_sizerCheckbox
->Add(m_checkbox
, 1, wxCENTRE
);
278 m_sizerCheckbox
->Add(0, 0, 1, wxCENTRE
);
279 m_sizerCheckbox
->Layout();
282 // ----------------------------------------------------------------------------
284 // ----------------------------------------------------------------------------
286 void CheckBoxWidgetsPage::OnButtonReset(wxCommandEvent
& WXUNUSED(event
))
293 void CheckBoxWidgetsPage::OnStyleChange(wxCommandEvent
& WXUNUSED(event
))
298 void CheckBoxWidgetsPage::OnButtonChangeLabel(wxCommandEvent
& WXUNUSED(event
))
300 m_checkbox
->SetLabel(m_textLabel
->GetValue());
303 void CheckBoxWidgetsPage::OnCheckBox(wxCommandEvent
& event
)
305 wxLogMessage(_T("Test checkbox %schecked (value = %d)."),
306 event
.IsChecked() ? _T("") : _T("un"),
307 (int)m_checkbox
->Get3StateValue());