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