added test of wxBitmapButton
[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
87 protected:
88 // event handlers
89 void OnCheckOrRadioBox(wxCommandEvent& event);
90
91 void OnButton(wxCommandEvent& event);
92 void OnButtonReset(wxCommandEvent& event);
93 void OnButtonChangeLabel(wxCommandEvent& event);
94
95 // reset the wxButton parameters
96 void Reset();
97
98 // (re)create the wxButton
99 void CreateButton();
100
101 // add m_button to m_sizerButton using current value of m_chkFit
102 void AddButtonToSizer();
103
104 // helper function: create a bitmap for wxBitmapButton
105 wxBitmap CreateBitmap(const wxString& label);
106
107
108 // the controls
109 // ------------
110
111 // the check/radio boxes for styles
112 wxCheckBox *m_chkBitmap,
113 *m_chkImage,
114 *m_chkFit,
115 *m_chkDefault;
116
117 wxRadioBox *m_radioHAlign,
118 *m_radioVAlign;
119
120 // the button itself and the sizer it is in
121 wxButton *m_button;
122 wxSizer *m_sizerButton;
123
124 // the text entries for command parameters
125 wxTextCtrl *m_textLabel;
126
127 private:
128 DECLARE_EVENT_TABLE()
129 DECLARE_WIDGETS_PAGE(ButtonWidgetsPage)
130 };
131
132 // ----------------------------------------------------------------------------
133 // event tables
134 // ----------------------------------------------------------------------------
135
136 BEGIN_EVENT_TABLE(ButtonWidgetsPage, WidgetsPage)
137 EVT_BUTTON(ButtonPage_Button, ButtonWidgetsPage::OnButton)
138
139 EVT_BUTTON(ButtonPage_Reset, ButtonWidgetsPage::OnButtonReset)
140 EVT_BUTTON(ButtonPage_ChangeLabel, ButtonWidgetsPage::OnButtonChangeLabel)
141
142 EVT_CHECKBOX(wxID_ANY, ButtonWidgetsPage::OnCheckOrRadioBox)
143 EVT_RADIOBOX(wxID_ANY, ButtonWidgetsPage::OnCheckOrRadioBox)
144 END_EVENT_TABLE()
145
146 // ============================================================================
147 // implementation
148 // ============================================================================
149
150 IMPLEMENT_WIDGETS_PAGE(ButtonWidgetsPage, _T("Button"));
151
152 ButtonWidgetsPage::ButtonWidgetsPage(wxBookCtrlBase *book,
153 wxImageList *imaglist)
154 : WidgetsPage(book)
155 {
156 imaglist->Add(wxBitmap(button_xpm));
157
158 // init everything
159 m_chkBitmap =
160 m_chkImage =
161 m_chkFit =
162 m_chkDefault = (wxCheckBox *)NULL;
163
164 m_radioHAlign =
165 m_radioVAlign = (wxRadioBox *)NULL;
166
167 m_textLabel = (wxTextCtrl *)NULL;
168
169 m_button = (wxButton *)NULL;
170 m_sizerButton = (wxSizer *)NULL;
171
172 wxSizer *sizerTop = new wxBoxSizer(wxHORIZONTAL);
173
174 // left pane
175 wxStaticBox *box = new wxStaticBox(this, wxID_ANY, _T("&Set style"));
176
177 wxSizer *sizerLeft = new wxStaticBoxSizer(box, wxVERTICAL);
178
179 m_chkBitmap = CreateCheckBoxAndAddToSizer(sizerLeft, _T("&Bitmap button"));
180 m_chkImage = CreateCheckBoxAndAddToSizer(sizerLeft, _T("With &image"));
181 m_chkFit = CreateCheckBoxAndAddToSizer(sizerLeft, _T("&Fit exactly"));
182 m_chkDefault = CreateCheckBoxAndAddToSizer(sizerLeft, _T("&Default"));
183
184 #ifndef __WXUNIVERSAL__
185 // only wxUniv currently supports buttons with images
186 m_chkImage->Disable();
187 #endif // !wxUniv
188
189 sizerLeft->Add(5, 5, 0, wxGROW | wxALL, 5); // spacer
190
191 // should be in sync with enums Button[HV]Align!
192 static const wxString halign[] =
193 {
194 _T("left"),
195 _T("centre"),
196 _T("right"),
197 };
198
199 static const wxString valign[] =
200 {
201 _T("top"),
202 _T("centre"),
203 _T("bottom"),
204 };
205
206 m_radioHAlign = new wxRadioBox(this, wxID_ANY, _T("&Horz alignment"),
207 wxDefaultPosition, wxDefaultSize,
208 WXSIZEOF(halign), halign);
209 m_radioVAlign = new wxRadioBox(this, wxID_ANY, _T("&Vert alignment"),
210 wxDefaultPosition, wxDefaultSize,
211 WXSIZEOF(valign), valign);
212
213 sizerLeft->Add(m_radioHAlign, 0, wxGROW | wxALL, 5);
214 sizerLeft->Add(m_radioVAlign, 0, wxGROW | wxALL, 5);
215
216 sizerLeft->Add(5, 5, 0, wxGROW | wxALL, 5); // spacer
217
218 wxButton *btn = new wxButton(this, ButtonPage_Reset, _T("&Reset"));
219 sizerLeft->Add(btn, 0, wxALIGN_CENTRE_HORIZONTAL | wxALL, 15);
220
221 // middle pane
222 wxStaticBox *box2 = new wxStaticBox(this, wxID_ANY, _T("&Operations"));
223 wxSizer *sizerMiddle = new wxStaticBoxSizer(box2, wxVERTICAL);
224
225 wxSizer *sizerRow = CreateSizerWithTextAndButton(ButtonPage_ChangeLabel,
226 _T("Change label"),
227 wxID_ANY,
228 &m_textLabel);
229 m_textLabel->SetValue(_T("&Press me!"));
230
231 sizerMiddle->Add(sizerRow, 0, wxALL | wxGROW, 5);
232
233 // right pane
234 m_sizerButton = new wxBoxSizer(wxHORIZONTAL);
235 m_sizerButton->SetMinSize(150, 0);
236
237 // the 3 panes panes compose the window
238 sizerTop->Add(sizerLeft, 0, wxGROW | (wxALL & ~wxLEFT), 10);
239 sizerTop->Add(sizerMiddle, 1, wxGROW | wxALL, 10);
240 sizerTop->Add(m_sizerButton, 1, wxGROW | (wxALL & ~wxRIGHT), 10);
241
242 // final initializations
243 Reset();
244
245 m_button = new wxButton(this, ButtonPage_Button, _T("&Press me!"));
246 AddButtonToSizer();
247
248 SetSizer(sizerTop);
249
250 sizerTop->Fit(this);
251 }
252
253 // ----------------------------------------------------------------------------
254 // operations
255 // ----------------------------------------------------------------------------
256
257 void ButtonWidgetsPage::Reset()
258 {
259 m_chkBitmap->SetValue(false);
260 m_chkFit->SetValue(true);
261 m_chkImage->SetValue(false);
262 m_chkDefault->SetValue(false);
263
264 m_radioHAlign->SetSelection(ButtonHAlign_Centre);
265 m_radioVAlign->SetSelection(ButtonVAlign_Centre);
266 }
267
268 void ButtonWidgetsPage::CreateButton()
269 {
270 wxString label;
271 if ( m_button )
272 {
273 label = m_button->GetLabel();
274
275 size_t count = m_sizerButton->GetChildren().GetCount();
276 for ( size_t n = 0; n < count; n++ )
277 {
278 m_sizerButton->Remove( 0 );
279 }
280
281 delete m_button;
282 }
283
284 if ( label.empty() )
285 {
286 // creating for the first time or recreating a button after bitmap
287 // button
288 label = m_textLabel->GetValue();
289 }
290
291 int flags = 0;
292 switch ( m_radioHAlign->GetSelection() )
293 {
294 case ButtonHAlign_Left:
295 flags |= wxBU_LEFT;
296 break;
297
298 default:
299 wxFAIL_MSG(_T("unexpected radiobox selection"));
300 // fall through
301
302 case ButtonHAlign_Centre:
303 break;
304
305 case ButtonHAlign_Right:
306 flags |= wxBU_RIGHT;
307 break;
308 }
309
310 switch ( m_radioVAlign->GetSelection() )
311 {
312 case ButtonVAlign_Top:
313 flags |= wxBU_TOP;
314 break;
315
316 default:
317 wxFAIL_MSG(_T("unexpected radiobox selection"));
318 // fall through
319
320 case ButtonVAlign_Centre:
321 flags |= wxALIGN_CENTRE_VERTICAL;
322 break;
323
324 case ButtonVAlign_Bottom:
325 flags |= wxBU_BOTTOM;
326 break;
327 }
328
329 if ( m_chkBitmap->GetValue() )
330 {
331 wxBitmapButton *bbtn = new wxBitmapButton(this, ButtonPage_Button,
332 CreateBitmap(_T("normal")));
333 bbtn->SetBitmapSelected(CreateBitmap(_T("pushed")));
334 bbtn->SetBitmapFocus(CreateBitmap(_T("focused")));
335 bbtn->SetBitmapDisabled(CreateBitmap(_T("disabled")));
336 m_button = bbtn;
337 }
338 else // normal button
339 {
340 m_button = new wxButton(this, ButtonPage_Button, label,
341 wxDefaultPosition, wxDefaultSize,
342 flags);
343 }
344
345 #ifdef __WXUNIVERSAL__
346 if ( m_chkImage->GetValue() )
347 {
348 m_button->SetImageLabel(wxArtProvider::GetIcon(wxART_INFORMATION));
349 }
350 #endif // wxUniv
351
352 if ( m_chkDefault->GetValue() )
353 {
354 m_button->SetDefault();
355 }
356
357 AddButtonToSizer();
358
359 m_sizerButton->Layout();
360 }
361
362 void ButtonWidgetsPage::AddButtonToSizer()
363 {
364 if ( m_chkFit->GetValue() )
365 {
366 m_sizerButton->AddStretchSpacer(1);
367 m_sizerButton->Add(m_button, wxSizerFlags(0).Centre().Border());
368 m_sizerButton->AddStretchSpacer(1);
369 }
370 else // take up the entire space
371 {
372 m_sizerButton->Add(m_button, wxSizerFlags(1).Expand().Border());
373 }
374 }
375
376 // ----------------------------------------------------------------------------
377 // event handlers
378 // ----------------------------------------------------------------------------
379
380 void ButtonWidgetsPage::OnButtonReset(wxCommandEvent& WXUNUSED(event))
381 {
382 Reset();
383
384 CreateButton();
385 }
386
387 void ButtonWidgetsPage::OnCheckOrRadioBox(wxCommandEvent& WXUNUSED(event))
388 {
389 CreateButton();
390 }
391
392 void ButtonWidgetsPage::OnButtonChangeLabel(wxCommandEvent& WXUNUSED(event))
393 {
394 m_button->SetLabel(m_textLabel->GetValue());
395 }
396
397 void ButtonWidgetsPage::OnButton(wxCommandEvent& WXUNUSED(event))
398 {
399 wxLogMessage(_T("Test button clicked."));
400 }
401
402 // ----------------------------------------------------------------------------
403 // bitmap button stuff
404 // ----------------------------------------------------------------------------
405
406 wxBitmap ButtonWidgetsPage::CreateBitmap(const wxString& label)
407 {
408 wxBitmap bmp(180, 70); // shouldn't hardcode but it's simpler like this
409 wxMemoryDC dc;
410 dc.SelectObject(bmp);
411 dc.SetBackground(wxBrush(*wxWHITE));
412 dc.Clear();
413 dc.SetTextForeground(*wxBLUE);
414 dc.DrawLabel(wxStripMenuCodes(m_textLabel->GetLabel()) + _T("\n")
415 _T("(") + label + _T(" state)"),
416 wxArtProvider::GetBitmap(wxART_INFORMATION),
417 wxRect(10, 10, bmp.GetWidth() - 20, bmp.GetHeight() - 20),
418 wxALIGN_CENTRE);
419
420 return bmp;
421 }
422