]> git.saurik.com Git - wxWidgets.git/blob - samples/widgets/button.cpp
1056653e389a70b9e7e81886ba62ac7efa78c06b
[wxWidgets.git] / samples / widgets / button.cpp
1 /////////////////////////////////////////////////////////////////////////////
2 // Program: wxWidgets 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/bmpbuttn.h"
33 #include "wx/button.h"
34 #include "wx/checkbox.h"
35 #include "wx/radiobox.h"
36 #include "wx/statbox.h"
37 #include "wx/textctrl.h"
38 #endif
39
40 #include "wx/artprov.h"
41 #include "wx/sizer.h"
42 #include "wx/dcmemory.h"
43
44 #include "widgets.h"
45
46 #include "icons/button.xpm"
47
48 // ----------------------------------------------------------------------------
49 // constants
50 // ----------------------------------------------------------------------------
51
52 // control ids
53 enum
54 {
55 ButtonPage_Reset = 100,
56 ButtonPage_ChangeLabel,
57 ButtonPage_Button
58 };
59
60 // radio boxes
61 enum
62 {
63 ButtonHAlign_Left,
64 ButtonHAlign_Centre,
65 ButtonHAlign_Right
66 };
67
68 enum
69 {
70 ButtonVAlign_Top,
71 ButtonVAlign_Centre,
72 ButtonVAlign_Bottom
73 };
74
75 // ----------------------------------------------------------------------------
76 // ButtonWidgetsPage
77 // ----------------------------------------------------------------------------
78
79 class ButtonWidgetsPage : public WidgetsPage
80 {
81 public:
82 ButtonWidgetsPage(WidgetsBookCtrl *book, wxImageList *imaglist);
83 virtual ~ButtonWidgetsPage(){};
84
85 virtual wxControl *GetWidget() const { return m_button; }
86 virtual void RecreateWidget() { CreateButton(); }
87
88 protected:
89 // event handlers
90 void OnCheckOrRadioBox(wxCommandEvent& event);
91
92 void OnButton(wxCommandEvent& event);
93 void OnButtonReset(wxCommandEvent& event);
94 void OnButtonChangeLabel(wxCommandEvent& event);
95
96 // reset the wxButton parameters
97 void Reset();
98
99 // (re)create the wxButton
100 void CreateButton();
101
102 // add m_button to m_sizerButton using current value of m_chkFit
103 void AddButtonToSizer();
104
105 // helper function: create a bitmap for wxBitmapButton
106 wxBitmap CreateBitmap(const wxString& label);
107
108
109 // the controls
110 // ------------
111
112 // the check/radio boxes for styles
113 wxCheckBox *m_chkBitmap,
114 *m_chkImage,
115 *m_chkFit,
116 *m_chkDefault;
117
118 // more checkboxes for wxBitmapButton only
119 wxCheckBox *m_chkUseSelected,
120 *m_chkUseFocused,
121 *m_chkUseHover,
122 *m_chkUseDisabled;
123
124 wxRadioBox *m_radioHAlign,
125 *m_radioVAlign;
126
127 // the button itself and the sizer it is in
128 wxButton *m_button;
129 wxSizer *m_sizerButton;
130
131 // the text entries for command parameters
132 wxTextCtrl *m_textLabel;
133
134 private:
135 DECLARE_EVENT_TABLE()
136 DECLARE_WIDGETS_PAGE(ButtonWidgetsPage)
137 };
138
139 // ----------------------------------------------------------------------------
140 // event tables
141 // ----------------------------------------------------------------------------
142
143 BEGIN_EVENT_TABLE(ButtonWidgetsPage, WidgetsPage)
144 EVT_BUTTON(ButtonPage_Button, ButtonWidgetsPage::OnButton)
145
146 EVT_BUTTON(ButtonPage_Reset, ButtonWidgetsPage::OnButtonReset)
147 EVT_BUTTON(ButtonPage_ChangeLabel, ButtonWidgetsPage::OnButtonChangeLabel)
148
149 EVT_CHECKBOX(wxID_ANY, ButtonWidgetsPage::OnCheckOrRadioBox)
150 EVT_RADIOBOX(wxID_ANY, ButtonWidgetsPage::OnCheckOrRadioBox)
151 END_EVENT_TABLE()
152
153 // ============================================================================
154 // implementation
155 // ============================================================================
156
157 IMPLEMENT_WIDGETS_PAGE(ButtonWidgetsPage, _T("Button"),
158 (int)wxPlatform(GENERIC_CTRLS).If(wxMSW,NATIVE_CTRLS)
159 );
160
161 ButtonWidgetsPage::ButtonWidgetsPage(WidgetsBookCtrl *book,
162 wxImageList *imaglist)
163 : WidgetsPage(book)
164 {
165 imaglist->Add(wxBitmap(button_xpm));
166
167 // init everything
168 m_chkBitmap =
169 m_chkImage =
170 m_chkFit =
171 m_chkDefault =
172 m_chkUseSelected =
173 m_chkUseFocused =
174 m_chkUseHover =
175 m_chkUseDisabled = (wxCheckBox *)NULL;
176
177 m_radioHAlign =
178 m_radioVAlign = (wxRadioBox *)NULL;
179
180 m_textLabel = (wxTextCtrl *)NULL;
181
182 m_button = (wxButton *)NULL;
183 m_sizerButton = (wxSizer *)NULL;
184
185 wxSizer *sizerTop = new wxBoxSizer(wxHORIZONTAL);
186
187 // left pane
188 wxStaticBox *box = new wxStaticBox(this, wxID_ANY, _T("&Set style"));
189
190 wxSizer *sizerLeft = new wxStaticBoxSizer(box, wxVERTICAL);
191
192 m_chkBitmap = CreateCheckBoxAndAddToSizer(sizerLeft, _T("&Bitmap button"));
193 m_chkImage = CreateCheckBoxAndAddToSizer(sizerLeft, _T("With &image"));
194 m_chkFit = CreateCheckBoxAndAddToSizer(sizerLeft, _T("&Fit exactly"));
195 m_chkDefault = CreateCheckBoxAndAddToSizer(sizerLeft, _T("&Default"));
196
197 #ifndef __WXUNIVERSAL__
198 // only wxUniv currently supports buttons with images
199 m_chkImage->Disable();
200 #endif // !wxUniv
201
202 sizerLeft->AddSpacer(5);
203
204 wxSizer *sizerUseLabels =
205 new wxStaticBoxSizer(wxVERTICAL, this, _T("&Use the following labels?"));
206 m_chkUseSelected = CreateCheckBoxAndAddToSizer(sizerUseLabels, _T("&Pushed"));
207 m_chkUseFocused = CreateCheckBoxAndAddToSizer(sizerUseLabels, _T("&Focused"));
208 m_chkUseHover = CreateCheckBoxAndAddToSizer(sizerUseLabels, _T("&Hover"));
209 m_chkUseDisabled = CreateCheckBoxAndAddToSizer(sizerUseLabels, _T("&Disabled"));
210 sizerLeft->Add(sizerUseLabels, wxSizerFlags().Expand().Border());
211
212 sizerLeft->AddSpacer(15);
213
214 // should be in sync with enums Button[HV]Align!
215 static const wxString halign[] =
216 {
217 _T("left"),
218 _T("centre"),
219 _T("right"),
220 };
221
222 static const wxString valign[] =
223 {
224 _T("top"),
225 _T("centre"),
226 _T("bottom"),
227 };
228
229 m_radioHAlign = new wxRadioBox(this, wxID_ANY, _T("&Horz alignment"),
230 wxDefaultPosition, wxDefaultSize,
231 WXSIZEOF(halign), halign);
232 m_radioVAlign = new wxRadioBox(this, wxID_ANY, _T("&Vert alignment"),
233 wxDefaultPosition, wxDefaultSize,
234 WXSIZEOF(valign), valign);
235
236 sizerLeft->Add(m_radioHAlign, 0, wxGROW | wxALL, 5);
237 sizerLeft->Add(m_radioVAlign, 0, wxGROW | wxALL, 5);
238
239 sizerLeft->Add(5, 5, 0, wxGROW | wxALL, 5); // spacer
240
241 wxButton *btn = new wxButton(this, ButtonPage_Reset, _T("&Reset"));
242 sizerLeft->Add(btn, 0, wxALIGN_CENTRE_HORIZONTAL | wxALL, 15);
243
244 // middle pane
245 wxStaticBox *box2 = new wxStaticBox(this, wxID_ANY, _T("&Operations"));
246 wxSizer *sizerMiddle = new wxStaticBoxSizer(box2, wxVERTICAL);
247
248 wxSizer *sizerRow = CreateSizerWithTextAndButton(ButtonPage_ChangeLabel,
249 _T("Change label"),
250 wxID_ANY,
251 &m_textLabel);
252 m_textLabel->SetValue(_T("&Press me!"));
253
254 sizerMiddle->Add(sizerRow, 0, wxALL | wxGROW, 5);
255
256 // right pane
257 m_sizerButton = new wxBoxSizer(wxHORIZONTAL);
258 m_sizerButton->SetMinSize(150, 0);
259
260 // the 3 panes panes compose the window
261 sizerTop->Add(sizerLeft, 0, wxGROW | (wxALL & ~wxLEFT), 10);
262 sizerTop->Add(sizerMiddle, 1, wxGROW | wxALL, 10);
263 sizerTop->Add(m_sizerButton, 1, wxGROW | (wxALL & ~wxRIGHT), 10);
264
265 // do create the main control
266 Reset();
267 CreateButton();
268
269 SetSizer(sizerTop);
270
271 sizerTop->Fit(this);
272 }
273
274 // ----------------------------------------------------------------------------
275 // operations
276 // ----------------------------------------------------------------------------
277
278 void ButtonWidgetsPage::Reset()
279 {
280 m_chkBitmap->SetValue(false);
281 m_chkFit->SetValue(true);
282 m_chkImage->SetValue(false);
283 m_chkDefault->SetValue(false);
284
285 m_chkUseSelected->SetValue(true);
286 m_chkUseFocused->SetValue(true);
287 m_chkUseHover->SetValue(true);
288 m_chkUseDisabled->SetValue(true);
289
290 m_radioHAlign->SetSelection(ButtonHAlign_Centre);
291 m_radioVAlign->SetSelection(ButtonVAlign_Centre);
292 }
293
294 void ButtonWidgetsPage::CreateButton()
295 {
296 wxString label;
297 if ( m_button )
298 {
299 label = m_button->GetLabel();
300
301 size_t count = m_sizerButton->GetChildren().GetCount();
302 for ( size_t n = 0; n < count; n++ )
303 {
304 m_sizerButton->Remove( 0 );
305 }
306
307 delete m_button;
308 }
309
310 if ( label.empty() )
311 {
312 // creating for the first time or recreating a button after bitmap
313 // button
314 label = m_textLabel->GetValue();
315 }
316
317 int flags = ms_defaultFlags;
318 switch ( m_radioHAlign->GetSelection() )
319 {
320 case ButtonHAlign_Left:
321 flags |= wxBU_LEFT;
322 break;
323
324 default:
325 wxFAIL_MSG(_T("unexpected radiobox selection"));
326 // fall through
327
328 case ButtonHAlign_Centre:
329 break;
330
331 case ButtonHAlign_Right:
332 flags |= wxBU_RIGHT;
333 break;
334 }
335
336 switch ( m_radioVAlign->GetSelection() )
337 {
338 case ButtonVAlign_Top:
339 flags |= wxBU_TOP;
340 break;
341
342 default:
343 wxFAIL_MSG(_T("unexpected radiobox selection"));
344 // fall through
345
346 case ButtonVAlign_Centre:
347 // centre vertical alignment is the default (no style)
348 break;
349
350 case ButtonVAlign_Bottom:
351 flags |= wxBU_BOTTOM;
352 break;
353 }
354
355 const bool isBitmapButton = m_chkBitmap->GetValue();
356 if ( isBitmapButton )
357 {
358 wxBitmapButton *bbtn = new wxBitmapButton(this, ButtonPage_Button,
359 CreateBitmap(_T("normal")));
360 if ( m_chkUseSelected->GetValue() )
361 bbtn->SetBitmapSelected(CreateBitmap(_T("pushed")));
362 if ( m_chkUseFocused->GetValue() )
363 bbtn->SetBitmapFocus(CreateBitmap(_T("focused")));
364 if ( m_chkUseHover->GetValue() )
365 bbtn->SetBitmapHover(CreateBitmap(_T("hover")));
366 if ( m_chkUseDisabled->GetValue() )
367 bbtn->SetBitmapDisabled(CreateBitmap(_T("disabled")));
368 m_button = bbtn;
369 }
370 else // normal button
371 {
372 m_button = new wxButton(this, ButtonPage_Button, label,
373 wxDefaultPosition, wxDefaultSize,
374 flags);
375 }
376
377 m_chkUseSelected->Enable(isBitmapButton);
378 m_chkUseFocused->Enable(isBitmapButton);
379 m_chkUseHover->Enable(isBitmapButton);
380 m_chkUseDisabled->Enable(isBitmapButton);
381
382 #ifdef __WXUNIVERSAL__
383 if ( m_chkImage->GetValue() )
384 {
385 m_button->SetImageLabel(wxArtProvider::GetIcon(wxART_INFORMATION));
386 }
387 #endif // wxUniv
388
389 if ( m_chkDefault->GetValue() )
390 {
391 m_button->SetDefault();
392 }
393
394 AddButtonToSizer();
395
396 m_sizerButton->Layout();
397 }
398
399 void ButtonWidgetsPage::AddButtonToSizer()
400 {
401 if ( m_chkFit->GetValue() )
402 {
403 m_sizerButton->AddStretchSpacer(1);
404 m_sizerButton->Add(m_button, wxSizerFlags(0).Centre().Border());
405 m_sizerButton->AddStretchSpacer(1);
406 }
407 else // take up the entire space
408 {
409 m_sizerButton->Add(m_button, wxSizerFlags(1).Expand().Border());
410 }
411 }
412
413 // ----------------------------------------------------------------------------
414 // event handlers
415 // ----------------------------------------------------------------------------
416
417 void ButtonWidgetsPage::OnButtonReset(wxCommandEvent& WXUNUSED(event))
418 {
419 Reset();
420
421 CreateButton();
422 }
423
424 void ButtonWidgetsPage::OnCheckOrRadioBox(wxCommandEvent& WXUNUSED(event))
425 {
426 CreateButton();
427 }
428
429 void ButtonWidgetsPage::OnButtonChangeLabel(wxCommandEvent& WXUNUSED(event))
430 {
431 m_button->SetLabel(m_textLabel->GetValue());
432 }
433
434 void ButtonWidgetsPage::OnButton(wxCommandEvent& WXUNUSED(event))
435 {
436 wxLogMessage(_T("Test button clicked."));
437 }
438
439 // ----------------------------------------------------------------------------
440 // bitmap button stuff
441 // ----------------------------------------------------------------------------
442
443 wxBitmap ButtonWidgetsPage::CreateBitmap(const wxString& label)
444 {
445 wxBitmap bmp(180, 70); // shouldn't hardcode but it's simpler like this
446 wxMemoryDC dc;
447 dc.SelectObject(bmp);
448 dc.SetBackground(wxBrush(*wxWHITE));
449 dc.Clear();
450 dc.SetTextForeground(*wxBLUE);
451 dc.DrawLabel(wxStripMenuCodes(m_textLabel->GetValue()) + _T("\n")
452 _T("(") + label + _T(" state)"),
453 wxArtProvider::GetBitmap(wxART_INFORMATION),
454 wxRect(10, 10, bmp.GetWidth() - 20, bmp.GetHeight() - 20),
455 wxALIGN_CENTRE);
456
457 return bmp;
458 }
459