]>
Commit | Line | Data |
---|---|---|
32816f9a WS |
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" | |
261357eb | 37 | #include "wx/textctrl.h" |
32816f9a WS |
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 | protected: | |
68 | // event handlers | |
69 | void OnButtonReset(wxCommandEvent& event); | |
70 | void OnButtonChangeLabel(wxCommandEvent& event); | |
71 | ||
72 | // reset the toggle parameters | |
73 | void Reset(); | |
74 | ||
75 | // (re)create the toggle | |
76 | void CreateToggle(); | |
77 | ||
78 | // the controls | |
79 | // ------------ | |
80 | ||
81 | // the checkbox itself and the sizer it is in | |
82 | wxToggleButton *m_toggle; | |
83 | wxSizer *m_sizerToggle; | |
84 | ||
85 | // the text entries for command parameters | |
86 | wxTextCtrl *m_textLabel; | |
87 | ||
88 | private: | |
89 | DECLARE_EVENT_TABLE() | |
90 | DECLARE_WIDGETS_PAGE(ToggleWidgetsPage) | |
91 | }; | |
92 | ||
93 | // ---------------------------------------------------------------------------- | |
94 | // event tables | |
95 | // ---------------------------------------------------------------------------- | |
96 | ||
97 | BEGIN_EVENT_TABLE(ToggleWidgetsPage, WidgetsPage) | |
98 | EVT_BUTTON(TogglePage_Reset, ToggleWidgetsPage::OnButtonReset) | |
99 | EVT_BUTTON(TogglePage_ChangeLabel, ToggleWidgetsPage::OnButtonChangeLabel) | |
100 | END_EVENT_TABLE() | |
101 | ||
102 | // ============================================================================ | |
103 | // implementation | |
104 | // ============================================================================ | |
105 | ||
106 | #if defined(__WXUNIVERSAL__) | |
107 | #define FAMILY_CTRLS UNIVERSAL_CTRLS | |
108 | #else | |
109 | #define FAMILY_CTRLS NATIVE_CTRLS | |
110 | #endif | |
111 | ||
112 | IMPLEMENT_WIDGETS_PAGE(ToggleWidgetsPage, wxT("ToggleButton"), | |
113 | FAMILY_CTRLS | |
114 | ); | |
115 | ||
116 | ToggleWidgetsPage::ToggleWidgetsPage(WidgetsBookCtrl *book, | |
117 | wxImageList *imaglist) | |
261357eb | 118 | :WidgetsPage(book, imaglist, toggle_xpm) |
32816f9a | 119 | { |
32816f9a WS |
120 | wxSizer *sizerTop = new wxBoxSizer(wxHORIZONTAL); |
121 | ||
122 | // left pane | |
123 | // wxStaticBox *box = new wxStaticBox(this, wxID_ANY, wxT("Styles")); | |
124 | ||
125 | // wxSizer *sizerLeft = new wxStaticBoxSizer(box, wxVERTICAL); | |
126 | ||
127 | // middle pane | |
128 | wxStaticBox *box2 = new wxStaticBox(this, wxID_ANY, _T("&Operations")); | |
129 | wxSizer *sizerMiddle = new wxStaticBoxSizer(box2, wxVERTICAL); | |
130 | ||
131 | wxSizer *sizerRow = CreateSizerWithTextAndButton(TogglePage_ChangeLabel, | |
132 | _T("Change label"), | |
133 | wxID_ANY, | |
134 | &m_textLabel); | |
135 | m_textLabel->SetValue(_T("&Toggle me!")); | |
136 | ||
137 | sizerMiddle->Add(sizerRow, 0, wxALL | wxGROW, 5); | |
138 | ||
139 | // right pane | |
140 | wxSizer *sizerRight = new wxBoxSizer(wxHORIZONTAL); | |
141 | ||
142 | m_toggle = new wxToggleButton(this, TogglePage_Picker, wxT("Toggle Button")); | |
143 | ||
144 | sizerRight->Add(0, 0, 1, wxCENTRE); | |
145 | sizerRight->Add(m_toggle, 1, wxCENTRE); | |
146 | sizerRight->Add(0, 0, 1, wxCENTRE); | |
147 | sizerRight->SetMinSize(150, 0); | |
148 | m_sizerToggle = sizerRight; // save it to modify it later | |
149 | ||
150 | // the 3 panes panes compose the window | |
151 | // sizerTop->Add(sizerLeft, 0, (wxALL & ~wxLEFT), 10); | |
152 | sizerTop->Add(sizerMiddle, 1, wxGROW | wxALL, 10); | |
153 | sizerTop->Add(sizerRight, 1, wxGROW | (wxALL & ~wxRIGHT), 10); | |
154 | ||
155 | // final initializations | |
156 | Reset(); | |
157 | ||
158 | SetSizer(sizerTop); | |
159 | ||
160 | sizerTop->Fit(this); | |
161 | } | |
162 | ||
163 | void ToggleWidgetsPage::Reset() | |
164 | { | |
165 | m_toggle->SetValue(false); | |
166 | } | |
167 | ||
168 | void ToggleWidgetsPage::CreateToggle() | |
169 | { | |
170 | const bool value = m_toggle->GetValue(); | |
171 | ||
172 | size_t count = m_sizerToggle->GetChildren().GetCount(); | |
173 | for ( size_t n = 0; n < count; n++ ) | |
174 | { | |
175 | m_sizerToggle->Remove(0); | |
176 | } | |
177 | ||
178 | delete m_toggle; | |
179 | ||
180 | m_toggle = new wxToggleButton(this, TogglePage_Picker, wxT("Toggle Button")); | |
181 | ||
182 | m_toggle->SetValue(value); | |
183 | ||
184 | m_sizerToggle->Add(0, 0, 1, wxCENTRE); | |
185 | m_sizerToggle->Add(m_toggle, 1, wxCENTRE); | |
186 | m_sizerToggle->Add(0, 0, 1, wxCENTRE); | |
187 | m_sizerToggle->Layout(); | |
188 | } | |
189 | ||
190 | // ---------------------------------------------------------------------------- | |
191 | // event handlers | |
192 | // ---------------------------------------------------------------------------- | |
193 | ||
194 | void ToggleWidgetsPage::OnButtonReset(wxCommandEvent& WXUNUSED(event)) | |
195 | { | |
196 | Reset(); | |
197 | ||
198 | CreateToggle(); | |
199 | } | |
200 | ||
201 | void ToggleWidgetsPage::OnButtonChangeLabel(wxCommandEvent& WXUNUSED(event)) | |
202 | { | |
203 | m_toggle->SetLabel(m_textLabel->GetValue()); | |
204 | } | |
205 | ||
206 | #endif // wxUSE_TOGGLEBTN |