]> git.saurik.com Git - wxWidgets.git/blob - samples/widgets/button.cpp
wx/wxprec.h already includes wx/defs.h (with other minor cleaning).
[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(wxBookCtrlBase *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
159 ButtonWidgetsPage::ButtonWidgetsPage(wxBookCtrlBase *book,
160 wxImageList *imaglist)
161 : WidgetsPage(book)
162 {
163 imaglist->Add(wxBitmap(button_xpm));
164
165 // init everything
166 m_chkBitmap =
167 m_chkImage =
168 m_chkFit =
169 m_chkDefault =
170 m_chkUseSelected =
171 m_chkUseFocused =
172 m_chkUseHover =
173 m_chkUseDisabled = (wxCheckBox *)NULL;
174
175 m_radioHAlign =
176 m_radioVAlign = (wxRadioBox *)NULL;
177
178 m_textLabel = (wxTextCtrl *)NULL;
179
180 m_button = (wxButton *)NULL;
181 m_sizerButton = (wxSizer *)NULL;
182
183 wxSizer *sizerTop = new wxBoxSizer(wxHORIZONTAL);
184
185 // left pane
186 wxStaticBox *box = new wxStaticBox(this, wxID_ANY, _T("&Set style"));
187
188 wxSizer *sizerLeft = new wxStaticBoxSizer(box, wxVERTICAL);
189
190 m_chkBitmap = CreateCheckBoxAndAddToSizer(sizerLeft, _T("&Bitmap button"));
191 m_chkImage = CreateCheckBoxAndAddToSizer(sizerLeft, _T("With &image"));
192 m_chkFit = CreateCheckBoxAndAddToSizer(sizerLeft, _T("&Fit exactly"));
193 m_chkDefault = CreateCheckBoxAndAddToSizer(sizerLeft, _T("&Default"));
194
195 #ifndef __WXUNIVERSAL__
196 // only wxUniv currently supports buttons with images
197 m_chkImage->Disable();
198 #endif // !wxUniv
199
200 sizerLeft->AddSpacer(5);
201
202 wxSizer *sizerUseLabels =
203 new wxStaticBoxSizer(wxVERTICAL, this, _T("&Use the following labels?"));
204 m_chkUseSelected = CreateCheckBoxAndAddToSizer(sizerUseLabels, _T("&Pushed"));
205 m_chkUseFocused = CreateCheckBoxAndAddToSizer(sizerUseLabels, _T("&Focused"));
206 m_chkUseHover = CreateCheckBoxAndAddToSizer(sizerUseLabels, _T("&Hover"));
207 m_chkUseDisabled = CreateCheckBoxAndAddToSizer(sizerUseLabels, _T("&Disabled"));
208 sizerLeft->Add(sizerUseLabels, wxSizerFlags().Expand().Border());
209
210 sizerLeft->AddSpacer(15);
211
212 // should be in sync with enums Button[HV]Align!
213 static const wxString halign[] =
214 {
215 _T("left"),
216 _T("centre"),
217 _T("right"),
218 };
219
220 static const wxString valign[] =
221 {
222 _T("top"),
223 _T("centre"),
224 _T("bottom"),
225 };
226
227 m_radioHAlign = new wxRadioBox(this, wxID_ANY, _T("&Horz alignment"),
228 wxDefaultPosition, wxDefaultSize,
229 WXSIZEOF(halign), halign);
230 m_radioVAlign = new wxRadioBox(this, wxID_ANY, _T("&Vert alignment"),
231 wxDefaultPosition, wxDefaultSize,
232 WXSIZEOF(valign), valign);
233
234 sizerLeft->Add(m_radioHAlign, 0, wxGROW | wxALL, 5);
235 sizerLeft->Add(m_radioVAlign, 0, wxGROW | wxALL, 5);
236
237 sizerLeft->Add(5, 5, 0, wxGROW | wxALL, 5); // spacer
238
239 wxButton *btn = new wxButton(this, ButtonPage_Reset, _T("&Reset"));
240 sizerLeft->Add(btn, 0, wxALIGN_CENTRE_HORIZONTAL | wxALL, 15);
241
242 // middle pane
243 wxStaticBox *box2 = new wxStaticBox(this, wxID_ANY, _T("&Operations"));
244 wxSizer *sizerMiddle = new wxStaticBoxSizer(box2, wxVERTICAL);
245
246 wxSizer *sizerRow = CreateSizerWithTextAndButton(ButtonPage_ChangeLabel,
247 _T("Change label"),
248 wxID_ANY,
249 &m_textLabel);
250 m_textLabel->SetValue(_T("&Press me!"));
251
252 sizerMiddle->Add(sizerRow, 0, wxALL | wxGROW, 5);
253
254 // right pane
255 m_sizerButton = new wxBoxSizer(wxHORIZONTAL);
256 m_sizerButton->SetMinSize(150, 0);
257
258 // the 3 panes panes compose the window
259 sizerTop->Add(sizerLeft, 0, wxGROW | (wxALL & ~wxLEFT), 10);
260 sizerTop->Add(sizerMiddle, 1, wxGROW | wxALL, 10);
261 sizerTop->Add(m_sizerButton, 1, wxGROW | (wxALL & ~wxRIGHT), 10);
262
263 // do create the main control
264 Reset();
265 CreateButton();
266
267 SetSizer(sizerTop);
268
269 sizerTop->Fit(this);
270 }
271
272 // ----------------------------------------------------------------------------
273 // operations
274 // ----------------------------------------------------------------------------
275
276 void ButtonWidgetsPage::Reset()
277 {
278 m_chkBitmap->SetValue(false);
279 m_chkFit->SetValue(true);
280 m_chkImage->SetValue(false);
281 m_chkDefault->SetValue(false);
282
283 m_chkUseSelected->SetValue(true);
284 m_chkUseFocused->SetValue(true);
285 m_chkUseHover->SetValue(true);
286 m_chkUseDisabled->SetValue(true);
287
288 m_radioHAlign->SetSelection(ButtonHAlign_Centre);
289 m_radioVAlign->SetSelection(ButtonVAlign_Centre);
290 }
291
292 void ButtonWidgetsPage::CreateButton()
293 {
294 wxString label;
295 if ( m_button )
296 {
297 label = m_button->GetLabel();
298
299 size_t count = m_sizerButton->GetChildren().GetCount();
300 for ( size_t n = 0; n < count; n++ )
301 {
302 m_sizerButton->Remove( 0 );
303 }
304
305 delete m_button;
306 }
307
308 if ( label.empty() )
309 {
310 // creating for the first time or recreating a button after bitmap
311 // button
312 label = m_textLabel->GetValue();
313 }
314
315 int flags = ms_defaultFlags;
316 switch ( m_radioHAlign->GetSelection() )
317 {
318 case ButtonHAlign_Left:
319 flags |= wxBU_LEFT;
320 break;
321
322 default:
323 wxFAIL_MSG(_T("unexpected radiobox selection"));
324 // fall through
325
326 case ButtonHAlign_Centre:
327 break;
328
329 case ButtonHAlign_Right:
330 flags |= wxBU_RIGHT;
331 break;
332 }
333
334 switch ( m_radioVAlign->GetSelection() )
335 {
336 case ButtonVAlign_Top:
337 flags |= wxBU_TOP;
338 break;
339
340 default:
341 wxFAIL_MSG(_T("unexpected radiobox selection"));
342 // fall through
343
344 case ButtonVAlign_Centre:
345 // centre vertical alignment is the default (no style)
346 break;
347
348 case ButtonVAlign_Bottom:
349 flags |= wxBU_BOTTOM;
350 break;
351 }
352
353 const bool isBitmapButton = m_chkBitmap->GetValue();
354 if ( isBitmapButton )
355 {
356 wxBitmapButton *bbtn = new wxBitmapButton(this, ButtonPage_Button,
357 CreateBitmap(_T("normal")));
358 if ( m_chkUseSelected->GetValue() )
359 bbtn->SetBitmapSelected(CreateBitmap(_T("pushed")));
360 if ( m_chkUseFocused->GetValue() )
361 bbtn->SetBitmapFocus(CreateBitmap(_T("focused")));
362 if ( m_chkUseHover->GetValue() )
363 bbtn->SetBitmapHover(CreateBitmap(_T("hover")));
364 if ( m_chkUseDisabled->GetValue() )
365 bbtn->SetBitmapDisabled(CreateBitmap(_T("disabled")));
366 m_button = bbtn;
367 }
368 else // normal button
369 {
370 m_button = new wxButton(this, ButtonPage_Button, label,
371 wxDefaultPosition, wxDefaultSize,
372 flags);
373 }
374
375 m_chkUseSelected->Enable(isBitmapButton);
376 m_chkUseFocused->Enable(isBitmapButton);
377 m_chkUseHover->Enable(isBitmapButton);
378 m_chkUseDisabled->Enable(isBitmapButton);
379
380 #ifdef __WXUNIVERSAL__
381 if ( m_chkImage->GetValue() )
382 {
383 m_button->SetImageLabel(wxArtProvider::GetIcon(wxART_INFORMATION));
384 }
385 #endif // wxUniv
386
387 if ( m_chkDefault->GetValue() )
388 {
389 m_button->SetDefault();
390 }
391
392 AddButtonToSizer();
393
394 m_sizerButton->Layout();
395 }
396
397 void ButtonWidgetsPage::AddButtonToSizer()
398 {
399 if ( m_chkFit->GetValue() )
400 {
401 m_sizerButton->AddStretchSpacer(1);
402 m_sizerButton->Add(m_button, wxSizerFlags(0).Centre().Border());
403 m_sizerButton->AddStretchSpacer(1);
404 }
405 else // take up the entire space
406 {
407 m_sizerButton->Add(m_button, wxSizerFlags(1).Expand().Border());
408 }
409 }
410
411 // ----------------------------------------------------------------------------
412 // event handlers
413 // ----------------------------------------------------------------------------
414
415 void ButtonWidgetsPage::OnButtonReset(wxCommandEvent& WXUNUSED(event))
416 {
417 Reset();
418
419 CreateButton();
420 }
421
422 void ButtonWidgetsPage::OnCheckOrRadioBox(wxCommandEvent& WXUNUSED(event))
423 {
424 CreateButton();
425 }
426
427 void ButtonWidgetsPage::OnButtonChangeLabel(wxCommandEvent& WXUNUSED(event))
428 {
429 m_button->SetLabel(m_textLabel->GetValue());
430 }
431
432 void ButtonWidgetsPage::OnButton(wxCommandEvent& WXUNUSED(event))
433 {
434 wxLogMessage(_T("Test button clicked."));
435 }
436
437 // ----------------------------------------------------------------------------
438 // bitmap button stuff
439 // ----------------------------------------------------------------------------
440
441 wxBitmap ButtonWidgetsPage::CreateBitmap(const wxString& label)
442 {
443 wxBitmap bmp(180, 70); // shouldn't hardcode but it's simpler like this
444 wxMemoryDC dc;
445 dc.SelectObject(bmp);
446 dc.SetBackground(wxBrush(*wxWHITE));
447 dc.Clear();
448 dc.SetTextForeground(*wxBLUE);
449 dc.DrawLabel(wxStripMenuCodes(m_textLabel->GetValue()) + _T("\n")
450 _T("(") + label + _T(" state)"),
451 wxArtProvider::GetBitmap(wxART_INFORMATION),
452 wxRect(10, 10, bmp.GetWidth() - 20, bmp.GetHeight() - 20),
453 wxALIGN_CENTRE);
454
455 return bmp;
456 }
457