]> git.saurik.com Git - wxWidgets.git/blob - samples/widgets/toggle.cpp
wxToggleButton page added to widgets sample.
[wxWidgets.git] / samples / widgets / toggle.cpp
1 /////////////////////////////////////////////////////////////////////////////
2 // Program: wxWidgets Widgets Sample
3 // Name: toggle.cpp
4 // Purpose: Part of the widgets sample showing toggle control
5 // Author: Dimitri Schoolwerth, Vadim Zeitlin
6 // Created: 27 Sep 2003
7 // Id: $Id$
8 // Copyright: (c) 2006 Wlodzmierz Skiba
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 #if wxUSE_TOGGLEBTN
28
29 #include "wx/tglbtn.h"
30
31 #include "widgets.h"
32
33 // for all others, include the necessary headers
34 #ifndef WX_PRECOMP
35 #include "wx/sizer.h"
36 #include "wx/statbox.h"
37 #endif
38
39 #include "icons/toggle.xpm"
40
41 // ----------------------------------------------------------------------------
42 // constants
43 // ----------------------------------------------------------------------------
44
45 // control ids
46 enum
47 {
48 TogglePage_Reset = wxID_HIGHEST,
49 TogglePage_ChangeLabel,
50 TogglePage_Picker
51 };
52
53 // ----------------------------------------------------------------------------
54 // CheckBoxWidgetsPage
55 // ----------------------------------------------------------------------------
56
57 class ToggleWidgetsPage : public WidgetsPage
58 {
59 public:
60 ToggleWidgetsPage(WidgetsBookCtrl *book, wxImageList *imaglist);
61 virtual ~ToggleWidgetsPage(){};
62
63 virtual wxControl *GetWidget() const { return m_toggle; }
64 virtual void RecreateWidget() { CreateToggle(); }
65
66 protected:
67 // event handlers
68 void OnButtonReset(wxCommandEvent& event);
69 void OnButtonChangeLabel(wxCommandEvent& event);
70
71 // reset the toggle parameters
72 void Reset();
73
74 // (re)create the toggle
75 void CreateToggle();
76
77 // the controls
78 // ------------
79
80 // the checkbox itself and the sizer it is in
81 wxToggleButton *m_toggle;
82 wxSizer *m_sizerToggle;
83
84 // the text entries for command parameters
85 wxTextCtrl *m_textLabel;
86
87 private:
88 DECLARE_EVENT_TABLE()
89 DECLARE_WIDGETS_PAGE(ToggleWidgetsPage)
90 };
91
92 // ----------------------------------------------------------------------------
93 // event tables
94 // ----------------------------------------------------------------------------
95
96 BEGIN_EVENT_TABLE(ToggleWidgetsPage, WidgetsPage)
97 EVT_BUTTON(TogglePage_Reset, ToggleWidgetsPage::OnButtonReset)
98 EVT_BUTTON(TogglePage_ChangeLabel, ToggleWidgetsPage::OnButtonChangeLabel)
99 END_EVENT_TABLE()
100
101 // ============================================================================
102 // implementation
103 // ============================================================================
104
105 #if defined(__WXUNIVERSAL__)
106 #define FAMILY_CTRLS UNIVERSAL_CTRLS
107 #else
108 #define FAMILY_CTRLS NATIVE_CTRLS
109 #endif
110
111 IMPLEMENT_WIDGETS_PAGE(ToggleWidgetsPage, wxT("ToggleButton"),
112 FAMILY_CTRLS
113 );
114
115 ToggleWidgetsPage::ToggleWidgetsPage(WidgetsBookCtrl *book,
116 wxImageList *imaglist)
117 :WidgetsPage(book)
118 {
119 imaglist->Add(wxBitmap(toggle_xpm));
120
121 wxSizer *sizerTop = new wxBoxSizer(wxHORIZONTAL);
122
123 // left pane
124 // wxStaticBox *box = new wxStaticBox(this, wxID_ANY, wxT("Styles"));
125
126 // wxSizer *sizerLeft = new wxStaticBoxSizer(box, wxVERTICAL);
127
128 // middle pane
129 wxStaticBox *box2 = new wxStaticBox(this, wxID_ANY, _T("&Operations"));
130 wxSizer *sizerMiddle = new wxStaticBoxSizer(box2, wxVERTICAL);
131
132 wxSizer *sizerRow = CreateSizerWithTextAndButton(TogglePage_ChangeLabel,
133 _T("Change label"),
134 wxID_ANY,
135 &m_textLabel);
136 m_textLabel->SetValue(_T("&Toggle me!"));
137
138 sizerMiddle->Add(sizerRow, 0, wxALL | wxGROW, 5);
139
140 // right pane
141 wxSizer *sizerRight = new wxBoxSizer(wxHORIZONTAL);
142
143 m_toggle = new wxToggleButton(this, TogglePage_Picker, wxT("Toggle Button"));
144
145 sizerRight->Add(0, 0, 1, wxCENTRE);
146 sizerRight->Add(m_toggle, 1, wxCENTRE);
147 sizerRight->Add(0, 0, 1, wxCENTRE);
148 sizerRight->SetMinSize(150, 0);
149 m_sizerToggle = sizerRight; // save it to modify it later
150
151 // the 3 panes panes compose the window
152 // sizerTop->Add(sizerLeft, 0, (wxALL & ~wxLEFT), 10);
153 sizerTop->Add(sizerMiddle, 1, wxGROW | wxALL, 10);
154 sizerTop->Add(sizerRight, 1, wxGROW | (wxALL & ~wxRIGHT), 10);
155
156 // final initializations
157 Reset();
158
159 SetSizer(sizerTop);
160
161 sizerTop->Fit(this);
162 }
163
164 void ToggleWidgetsPage::Reset()
165 {
166 m_toggle->SetValue(false);
167 }
168
169 void ToggleWidgetsPage::CreateToggle()
170 {
171 const bool value = m_toggle->GetValue();
172
173 size_t count = m_sizerToggle->GetChildren().GetCount();
174 for ( size_t n = 0; n < count; n++ )
175 {
176 m_sizerToggle->Remove(0);
177 }
178
179 delete m_toggle;
180
181 m_toggle = new wxToggleButton(this, TogglePage_Picker, wxT("Toggle Button"));
182
183 m_toggle->SetValue(value);
184
185 m_sizerToggle->Add(0, 0, 1, wxCENTRE);
186 m_sizerToggle->Add(m_toggle, 1, wxCENTRE);
187 m_sizerToggle->Add(0, 0, 1, wxCENTRE);
188 m_sizerToggle->Layout();
189 }
190
191 // ----------------------------------------------------------------------------
192 // event handlers
193 // ----------------------------------------------------------------------------
194
195 void ToggleWidgetsPage::OnButtonReset(wxCommandEvent& WXUNUSED(event))
196 {
197 Reset();
198
199 CreateToggle();
200 }
201
202 void ToggleWidgetsPage::OnButtonChangeLabel(wxCommandEvent& WXUNUSED(event))
203 {
204 m_toggle->SetLabel(m_textLabel->GetValue());
205 }
206
207 #endif // wxUSE_TOGGLEBTN