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