]> git.saurik.com Git - wxWidgets.git/blame - samples/widgets/button.cpp
Added version test
[wxWidgets.git] / samples / widgets / button.cpp
CommitLineData
32b8ec41
VZ
1/////////////////////////////////////////////////////////////////////////////
2// Program: wxWindows Widgets Sample
3// Name: button.cpp
4// Purpose: Part of the widgets sample showing wxButton
5// Author: Vadim Zeitlin
6// Created: 10.04.01
7// Id: $Id$
8// Copyright: (c) 2001 Vadim Zeitlin
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
3379ed37 29 #include "wx/app.h"
32b8ec41
VZ
30 #include "wx/log.h"
31
32 #include "wx/button.h"
33 #include "wx/checkbox.h"
34 #include "wx/radiobox.h"
35 #include "wx/statbox.h"
36 #include "wx/textctrl.h"
37#endif
38
ff8ebfaf 39#include "wx/artprov.h"
32b8ec41
VZ
40#include "wx/sizer.h"
41
42#include "widgets.h"
ff8ebfaf 43
32b8ec41
VZ
44#include "icons/button.xpm"
45
46// ----------------------------------------------------------------------------
47// constants
48// ----------------------------------------------------------------------------
49
50// control ids
51enum
52{
53 ButtonPage_Reset = 100,
54 ButtonPage_ChangeLabel,
55 ButtonPage_Button
56};
57
58// radio boxes
59enum
60{
61 ButtonHAlign_Left,
62 ButtonHAlign_Centre,
63 ButtonHAlign_Right
64};
65
66enum
67{
68 ButtonVAlign_Top,
69 ButtonVAlign_Centre,
70 ButtonVAlign_Bottom
71};
72
73// ----------------------------------------------------------------------------
74// ButtonWidgetsPage
75// ----------------------------------------------------------------------------
76
77class ButtonWidgetsPage : public WidgetsPage
78{
79public:
80 ButtonWidgetsPage(wxNotebook *notebook, wxImageList *imaglist);
81 virtual ~ButtonWidgetsPage();
82
83protected:
84 // event handlers
85 void OnCheckOrRadioBox(wxCommandEvent& event);
86
87 void OnButton(wxCommandEvent& event);
88 void OnButtonReset(wxCommandEvent& event);
89 void OnButtonChangeLabel(wxCommandEvent& event);
90
91 // reset the wxButton parameters
92 void Reset();
93
94 // (re)create the wxButton
95 void CreateButton();
96
97 // the controls
98 // ------------
99
100 // the check/radio boxes for styles
101 wxCheckBox *m_chkImage,
102 *m_chkFit,
103 *m_chkDefault;
104
105 wxRadioBox *m_radioHAlign,
106 *m_radioVAlign;
107
108 // the gauge itself and the sizer it is in
109 wxButton *m_button;
110 wxSizer *m_sizerButton;
111
112 // the text entries for command parameters
113 wxTextCtrl *m_textLabel;
114
115private:
5e173f35
GD
116 DECLARE_EVENT_TABLE()
117 DECLARE_WIDGETS_PAGE(ButtonWidgetsPage)
32b8ec41
VZ
118};
119
120// ----------------------------------------------------------------------------
121// event tables
122// ----------------------------------------------------------------------------
123
124BEGIN_EVENT_TABLE(ButtonWidgetsPage, WidgetsPage)
125 EVT_BUTTON(ButtonPage_Button, ButtonWidgetsPage::OnButton)
126
127 EVT_BUTTON(ButtonPage_Reset, ButtonWidgetsPage::OnButtonReset)
128 EVT_BUTTON(ButtonPage_ChangeLabel, ButtonWidgetsPage::OnButtonChangeLabel)
129
206d3a16
JS
130 EVT_CHECKBOX(wxID_ANY, ButtonWidgetsPage::OnCheckOrRadioBox)
131 EVT_RADIOBOX(wxID_ANY, ButtonWidgetsPage::OnCheckOrRadioBox)
32b8ec41
VZ
132END_EVENT_TABLE()
133
134// ============================================================================
135// implementation
136// ============================================================================
137
138IMPLEMENT_WIDGETS_PAGE(ButtonWidgetsPage, _T("Button"));
139
140ButtonWidgetsPage::ButtonWidgetsPage(wxNotebook *notebook,
141 wxImageList *imaglist)
142 : WidgetsPage(notebook)
143{
144 imaglist->Add(wxBitmap(button_xpm));
145
146 // init everything
147 m_chkImage =
148 m_chkFit =
149 m_chkDefault = (wxCheckBox *)NULL;
150
151 m_radioHAlign =
152 m_radioVAlign = (wxRadioBox *)NULL;
153
154 m_textLabel = (wxTextCtrl *)NULL;
155
156 m_button = (wxButton *)NULL;
157 m_sizerButton = (wxSizer *)NULL;
158
159 wxSizer *sizerTop = new wxBoxSizer(wxHORIZONTAL);
160
161 // left pane
206d3a16 162 wxStaticBox *box = new wxStaticBox(this, wxID_ANY, _T("&Set style"));
32b8ec41
VZ
163
164 wxSizer *sizerLeft = new wxStaticBoxSizer(box, wxVERTICAL);
165
166 m_chkImage = CreateCheckBoxAndAddToSizer(sizerLeft, _T("With &image"));
167 m_chkFit = CreateCheckBoxAndAddToSizer(sizerLeft, _T("&Fit exactly"));
168 m_chkDefault = CreateCheckBoxAndAddToSizer(sizerLeft, _T("&Default"));
169
170#ifndef __WXUNIVERSAL__
171 // only wxUniv currently supoprts buttons with images
172 m_chkImage->Disable();
173#endif // !wxUniv
174
175 sizerLeft->Add(5, 5, 0, wxGROW | wxALL, 5); // spacer
176
177 // should be in sync with enums Button[HV]Align!
178 static const wxString halign[] =
179 {
180 _T("left"),
181 _T("centre"),
182 _T("right"),
183 };
184
185 static const wxString valign[] =
186 {
187 _T("top"),
188 _T("centre"),
189 _T("bottom"),
190 };
191
206d3a16 192 m_radioHAlign = new wxRadioBox(this, wxID_ANY, _T("&Horz alignment"),
32b8ec41
VZ
193 wxDefaultPosition, wxDefaultSize,
194 WXSIZEOF(halign), halign);
206d3a16 195 m_radioVAlign = new wxRadioBox(this, wxID_ANY, _T("&Vert alignment"),
32b8ec41
VZ
196 wxDefaultPosition, wxDefaultSize,
197 WXSIZEOF(valign), valign);
198
199 sizerLeft->Add(m_radioHAlign, 0, wxGROW | wxALL, 5);
200 sizerLeft->Add(m_radioVAlign, 0, wxGROW | wxALL, 5);
201
202 sizerLeft->Add(5, 5, 0, wxGROW | wxALL, 5); // spacer
203
204 wxButton *btn = new wxButton(this, ButtonPage_Reset, _T("&Reset"));
205 sizerLeft->Add(btn, 0, wxALIGN_CENTRE_HORIZONTAL | wxALL, 15);
206
207 // middle pane
206d3a16 208 wxStaticBox *box2 = new wxStaticBox(this, wxID_ANY, _T("&Operations"));
32b8ec41
VZ
209 wxSizer *sizerMiddle = new wxStaticBoxSizer(box2, wxVERTICAL);
210
211 wxSizer *sizerRow = CreateSizerWithTextAndButton(ButtonPage_ChangeLabel,
212 _T("Change label"),
206d3a16 213 wxID_ANY,
32b8ec41
VZ
214 &m_textLabel);
215
216 sizerMiddle->Add(sizerRow, 0, wxALL | wxGROW, 5);
217
218 // right pane
219 wxSizer *sizerRight = new wxBoxSizer(wxHORIZONTAL);
220 m_button = new wxButton(this, ButtonPage_Button, _T("&Press me!"));
221 sizerRight->Add(0, 0, 1, wxCENTRE);
222 sizerRight->Add(m_button, 1, wxCENTRE);
223 sizerRight->Add(0, 0, 1, wxCENTRE);
7b127900 224 sizerRight->SetMinSize(150, 0);
32b8ec41
VZ
225 m_sizerButton = sizerRight; // save it to modify it later
226
227 // the 3 panes panes compose the window
228 sizerTop->Add(sizerLeft, 0, wxGROW | (wxALL & ~wxLEFT), 10);
229 sizerTop->Add(sizerMiddle, 1, wxGROW | wxALL, 10);
230 sizerTop->Add(sizerRight, 1, wxGROW | (wxALL & ~wxRIGHT), 10);
231
232 // final initializations
233 Reset();
234
32b8ec41
VZ
235 SetSizer(sizerTop);
236
237 sizerTop->Fit(this);
238}
239
240ButtonWidgetsPage::~ButtonWidgetsPage()
241{
242}
243
244// ----------------------------------------------------------------------------
245// operations
246// ----------------------------------------------------------------------------
247
248void ButtonWidgetsPage::Reset()
249{
206d3a16
JS
250 m_chkFit->SetValue(true);
251 m_chkImage->SetValue(false);
252 m_chkDefault->SetValue(false);
32b8ec41
VZ
253
254 m_radioHAlign->SetSelection(ButtonHAlign_Centre);
255 m_radioVAlign->SetSelection(ButtonVAlign_Centre);
256}
257
258void ButtonWidgetsPage::CreateButton()
259{
260 wxString label;
261 if ( m_button )
262 {
263 label = m_button->GetLabel();
264
265 size_t count = m_sizerButton->GetChildren().GetCount();
266 for ( size_t n = 0; n < count; n++ )
267 {
9dd96c0f 268 m_sizerButton->Remove( 0 );
32b8ec41
VZ
269 }
270
271 delete m_button;
272 }
273 else
274 {
275 label = _T("&Press me!");
276 }
277
278 int flags = 0;
279 switch ( m_radioHAlign->GetSelection() )
280 {
281 case ButtonHAlign_Left:
c0495687 282 flags |= wxBU_LEFT;
32b8ec41
VZ
283 break;
284
285 default:
286 wxFAIL_MSG(_T("unexpected radiobox selection"));
287 // fall through
288
289 case ButtonHAlign_Centre:
32b8ec41
VZ
290 break;
291
292 case ButtonHAlign_Right:
c0495687 293 flags |= wxBU_RIGHT;
32b8ec41
VZ
294 break;
295 }
296
297 switch ( m_radioVAlign->GetSelection() )
298 {
299 case ButtonVAlign_Top:
c0495687 300 flags |= wxBU_TOP;
32b8ec41
VZ
301 break;
302
303 default:
304 wxFAIL_MSG(_T("unexpected radiobox selection"));
305 // fall through
306
307 case ButtonVAlign_Centre:
308 flags |= wxALIGN_CENTRE_VERTICAL;
309 break;
310
311 case ButtonVAlign_Bottom:
c0495687 312 flags |= wxBU_BOTTOM;
32b8ec41
VZ
313 break;
314 }
315
316 m_button = new wxButton(this, ButtonPage_Button, label,
317 wxDefaultPosition, wxDefaultSize,
318 flags);
319
320#ifdef __WXUNIVERSAL__
321 if ( m_chkImage->GetValue() )
322 {
ff8ebfaf 323 m_button->SetImageLabel(wxArtProvider::GetIcon(wxART_INFORMATION));
32b8ec41
VZ
324 }
325#endif // wxUniv
326
327 if ( m_chkDefault->GetValue() )
328 {
329 m_button->SetDefault();
330 }
331
332 if ( m_chkFit->GetValue() )
333 {
334 m_sizerButton->Add(0, 0, 1, wxCENTRE);
335 m_sizerButton->Add(m_button, 1, wxCENTRE);
336 m_sizerButton->Add(0, 0, 1, wxCENTRE);
337 }
338 else
339 {
340 m_sizerButton->Add(m_button, 1, wxGROW | wxALL, 5);
341 }
342
343 m_sizerButton->Layout();
344}
345
346// ----------------------------------------------------------------------------
347// event handlers
348// ----------------------------------------------------------------------------
349
350void ButtonWidgetsPage::OnButtonReset(wxCommandEvent& WXUNUSED(event))
351{
352 Reset();
353
354 CreateButton();
355}
356
c02e5a31 357void ButtonWidgetsPage::OnCheckOrRadioBox(wxCommandEvent& WXUNUSED(event))
32b8ec41
VZ
358{
359 CreateButton();
360}
361
362void ButtonWidgetsPage::OnButtonChangeLabel(wxCommandEvent& WXUNUSED(event))
363{
364 m_button->SetLabel(m_textLabel->GetValue());
365}
366
c02e5a31 367void ButtonWidgetsPage::OnButton(wxCommandEvent& WXUNUSED(event))
32b8ec41
VZ
368{
369 wxLogMessage(_T("Test button clicked."));
370}
371