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