Globally use "wxWindows licence" consistently.
[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 // Licence: wxWindows licence
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, wxT("&Operations"));
136 wxSizer *sizerMiddle = new wxStaticBoxSizer(box2, wxVERTICAL);
137
138 wxSizer *sizerRow = CreateSizerWithTextAndButton(TogglePage_ChangeLabel,
139 wxT("Change label"),
140 wxID_ANY,
141 &m_textLabel);
142 m_textLabel->SetValue(wxT("&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
168 void ToggleWidgetsPage::Reset()
169 {
170 m_toggle->SetValue(false);
171 }
172
173 void ToggleWidgetsPage::CreateToggle()
174 {
175 const bool value = m_toggle->GetValue();
176
177 size_t count = m_sizerToggle->GetChildren().GetCount();
178 for ( size_t n = 0; n < count; n++ )
179 {
180 m_sizerToggle->Remove(0);
181 }
182
183 delete m_toggle;
184
185 m_toggle = new wxToggleButton(this, TogglePage_Picker, wxT("Toggle Button"));
186
187 m_toggle->SetValue(value);
188
189 m_sizerToggle->Add(0, 0, 1, wxCENTRE);
190 m_sizerToggle->Add(m_toggle, 1, wxCENTRE);
191 m_sizerToggle->Add(0, 0, 1, wxCENTRE);
192 m_sizerToggle->Layout();
193 }
194
195 // ----------------------------------------------------------------------------
196 // event handlers
197 // ----------------------------------------------------------------------------
198
199 void ToggleWidgetsPage::OnButtonReset(wxCommandEvent& WXUNUSED(event))
200 {
201 Reset();
202
203 CreateToggle();
204 }
205
206 void ToggleWidgetsPage::OnButtonChangeLabel(wxCommandEvent& WXUNUSED(event))
207 {
208 m_toggle->SetLabel(m_textLabel->GetValue());
209 }
210
211 #endif // wxUSE_TOGGLEBTN