added 3-state checkboxes (patch 813790)
[wxWidgets.git] / samples / widgets / checkbox.cpp
1 /////////////////////////////////////////////////////////////////////////////
2 // Program: wxWindows Widgets Sample
3 // Name: checkbox.cpp
4 // Purpose: Part of the widgets sample showing wxCheckBox
5 // Author: Dimitri Schoolwerth
6 // Created: 27 Sep 2003
7 // Id: $Id$
8 // Copyright: (c) 2003 wxWindows team
9 // License: wxWindows license
10 /////////////////////////////////////////////////////////////////////////////
11
12 // ============================================================================
13 // declarations
14 // ============================================================================
15
16 // ----------------------------------------------------------------------------
17 // headers
18 // ----------------------------------------------------------------------------
19
20 // for compilers that support precompilation, includes "wx/wx.h".
21 #include "wx/wxprec.h"
22
23 #ifdef __BORLANDC__
24 #pragma hdrstop
25 #endif
26
27 // for all others, include the necessary headers
28 #ifndef WX_PRECOMP
29 #include "wx/app.h"
30 #include "wx/log.h"
31
32 #include "wx/button.h"
33 #include "wx/checkbox.h"
34
35 #include "wx/sizer.h"
36
37 #endif
38
39 #include "widgets.h"
40
41 #include "icons/checkbox.xpm"
42
43 // ----------------------------------------------------------------------------
44 // CheckBoxWidgetsPage
45 // ----------------------------------------------------------------------------
46
47 class CheckBoxWidgetsPage : public WidgetsPage
48 {
49 public:
50 CheckBoxWidgetsPage(wxNotebook *notebook, wxImageList *imaglist);
51 virtual ~CheckBoxWidgetsPage();
52
53 protected:
54 // event handlers
55 void OnCheckBox(wxCommandEvent& event);
56
57 void OnButton(wxCommandEvent& event);
58
59 // the controls
60 // ------------
61
62 wxCheckBox *m_chk2States,
63 *m_chk3States,
64 *m_chk3StatesAllows3rdStateForUser;
65
66 wxButton *m_button;
67
68 private:
69 DECLARE_EVENT_TABLE()
70 DECLARE_WIDGETS_PAGE(CheckBoxWidgetsPage)
71 };
72
73 // ----------------------------------------------------------------------------
74 // event tables
75 // ----------------------------------------------------------------------------
76
77 BEGIN_EVENT_TABLE(CheckBoxWidgetsPage, WidgetsPage)
78 EVT_CHECKBOX(wxID_ANY, CheckBoxWidgetsPage::OnCheckBox)
79 EVT_BUTTON(wxID_ANY, CheckBoxWidgetsPage::OnButton)
80 END_EVENT_TABLE()
81
82 // ============================================================================
83 // implementation
84 // ============================================================================
85
86 IMPLEMENT_WIDGETS_PAGE(CheckBoxWidgetsPage, wxT("CheckBox"));
87
88 CheckBoxWidgetsPage::CheckBoxWidgetsPage(wxNotebook *notebook,
89 wxImageList *imaglist)
90 : WidgetsPage(notebook)
91 {
92 imaglist->Add(wxBitmap(checkbox_xpm));
93
94 m_chk2States = new wxCheckBox( this, wxID_ANY,
95 wxT("I'm a standard 2-state checkbox") );
96 m_chk3States = new wxCheckBox( this, wxID_ANY,
97 wxT("I'm a 3-state checkbox that disallows setting the undetermined")
98 wxT(" state by the user" ),
99 wxDefaultPosition, wxDefaultSize, wxCHK_3STATE);
100 m_button = new wxButton( this, wxID_ANY, wxT("&Programmatically set this")
101 wxT(" checkbox to undetermined state") );
102
103 m_chk3StatesAllows3rdStateForUser = new wxCheckBox(this, wxID_ANY,
104 wxT("I'm a 3-state checkbox that allows setting the 3rd state by the user"),
105 wxDefaultPosition, wxDefaultSize, wxCHK_3STATE
106 | wxCHK_ALLOW_3RD_STATE_FOR_USER);
107
108 wxSizer *sizerTop = new wxBoxSizer(wxVERTICAL);
109
110 sizerTop->Add(0, 0, 1, wxEXPAND);
111 sizerTop->Add(m_chk2States, 0, wxEXPAND);
112 sizerTop->Add(0, 0, 1, wxEXPAND);
113 wxSizer *sizerCheckBoxAndButton = new wxBoxSizer(wxHORIZONTAL);
114 {
115 wxSizer *szr = sizerCheckBoxAndButton;
116 szr->Add(m_chk3States, 0, wxEXPAND);
117 szr->Add(0, 0, 1, wxEXPAND);
118 szr->Add(m_button, 0, wxEXPAND);
119
120 sizerTop->Add(szr, 0, wxEXPAND);
121 }
122
123 sizerTop->Add(0, 0, 1, wxEXPAND);
124 sizerTop->Add(m_chk3StatesAllows3rdStateForUser, 0, wxEXPAND);
125 sizerTop->Add(0, 0, 1, wxEXPAND);
126
127 SetSizer(sizerTop);
128
129 sizerTop->Fit(this);
130 }
131
132 CheckBoxWidgetsPage::~CheckBoxWidgetsPage()
133 {
134 }
135
136 // ----------------------------------------------------------------------------
137 // event handlers
138 // ----------------------------------------------------------------------------
139
140 void CheckBoxWidgetsPage::OnCheckBox(wxCommandEvent& event)
141 {
142 static const wxString stateNames[] =
143 {
144 wxT("unchecked"),
145 wxT("checked"),
146 wxT("undetermined/mixed"),
147 };
148 wxCheckBoxState state = (wxCheckBoxState) event.GetInt();
149
150 wxCHECK_RET( (state >= 0) && (state < WXSIZEOF(stateNames)),
151 "event.GetInt() returned an invalid wxCheckBoxState" );
152
153 wxLogMessage(wxT("Checkbox now set to state: %s"),
154 stateNames[state].c_str());
155 }
156
157 void CheckBoxWidgetsPage::OnButton(wxCommandEvent& WXUNUSED(event))
158 {
159 m_chk3States->Set3StateValue(wxCHK_UNDETERMINED);
160 }