]> git.saurik.com Git - wxWidgets.git/blame - samples/widgets/button.cpp
allow adjusting the combo button size (patch 1489452)
[wxWidgets.git] / samples / widgets / button.cpp
CommitLineData
32b8ec41 1/////////////////////////////////////////////////////////////////////////////
be5a51fb 2// Program: wxWidgets Widgets Sample
32b8ec41
VZ
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
3379ed37 29 #include "wx/app.h"
32b8ec41
VZ
30 #include "wx/log.h"
31
552271d6 32 #include "wx/bmpbuttn.h"
32b8ec41
VZ
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
ff8ebfaf 40#include "wx/artprov.h"
32b8ec41 41#include "wx/sizer.h"
552271d6 42#include "wx/dcmemory.h"
32b8ec41
VZ
43
44#include "widgets.h"
ff8ebfaf 45
32b8ec41
VZ
46#include "icons/button.xpm"
47
48// ----------------------------------------------------------------------------
49// constants
50// ----------------------------------------------------------------------------
51
52// control ids
53enum
54{
55 ButtonPage_Reset = 100,
56 ButtonPage_ChangeLabel,
57 ButtonPage_Button
58};
59
60// radio boxes
61enum
62{
63 ButtonHAlign_Left,
64 ButtonHAlign_Centre,
65 ButtonHAlign_Right
66};
67
68enum
69{
70 ButtonVAlign_Top,
71 ButtonVAlign_Centre,
72 ButtonVAlign_Bottom
73};
74
75// ----------------------------------------------------------------------------
76// ButtonWidgetsPage
77// ----------------------------------------------------------------------------
78
79class ButtonWidgetsPage : public WidgetsPage
80{
81public:
5378558e 82 ButtonWidgetsPage(wxBookCtrlBase *book, wxImageList *imaglist);
8f6eaec9 83 virtual ~ButtonWidgetsPage(){};
32b8ec41 84
195df7a7 85 virtual wxControl *GetWidget() const { return m_button; }
1301e228 86 virtual void RecreateWidget() { CreateButton(); }
195df7a7 87
32b8ec41
VZ
88protected:
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
552271d6
VZ
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
32b8ec41
VZ
109 // the controls
110 // ------------
111
112 // the check/radio boxes for styles
552271d6
VZ
113 wxCheckBox *m_chkBitmap,
114 *m_chkImage,
32b8ec41
VZ
115 *m_chkFit,
116 *m_chkDefault;
117
3e764e2f
VZ
118 // more checkboxes for wxBitmapButton only
119 wxCheckBox *m_chkUseSelected,
120 *m_chkUseFocused,
121 *m_chkUseHover,
122 *m_chkUseDisabled;
123
32b8ec41
VZ
124 wxRadioBox *m_radioHAlign,
125 *m_radioVAlign;
126
8772bb08 127 // the button itself and the sizer it is in
32b8ec41
VZ
128 wxButton *m_button;
129 wxSizer *m_sizerButton;
130
131 // the text entries for command parameters
132 wxTextCtrl *m_textLabel;
133
134private:
5e173f35
GD
135 DECLARE_EVENT_TABLE()
136 DECLARE_WIDGETS_PAGE(ButtonWidgetsPage)
32b8ec41
VZ
137};
138
139// ----------------------------------------------------------------------------
140// event tables
141// ----------------------------------------------------------------------------
142
143BEGIN_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
206d3a16
JS
149 EVT_CHECKBOX(wxID_ANY, ButtonWidgetsPage::OnCheckOrRadioBox)
150 EVT_RADIOBOX(wxID_ANY, ButtonWidgetsPage::OnCheckOrRadioBox)
32b8ec41
VZ
151END_EVENT_TABLE()
152
153// ============================================================================
154// implementation
155// ============================================================================
156
157IMPLEMENT_WIDGETS_PAGE(ButtonWidgetsPage, _T("Button"));
158
5378558e 159ButtonWidgetsPage::ButtonWidgetsPage(wxBookCtrlBase *book,
61c083e7
WS
160 wxImageList *imaglist)
161 : WidgetsPage(book)
32b8ec41
VZ
162{
163 imaglist->Add(wxBitmap(button_xpm));
164
165 // init everything
552271d6 166 m_chkBitmap =
32b8ec41
VZ
167 m_chkImage =
168 m_chkFit =
3e764e2f
VZ
169 m_chkDefault =
170 m_chkUseSelected =
171 m_chkUseFocused =
172 m_chkUseHover =
173 m_chkUseDisabled = (wxCheckBox *)NULL;
32b8ec41
VZ
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
206d3a16 186 wxStaticBox *box = new wxStaticBox(this, wxID_ANY, _T("&Set style"));
32b8ec41
VZ
187
188 wxSizer *sizerLeft = new wxStaticBoxSizer(box, wxVERTICAL);
189
552271d6 190 m_chkBitmap = CreateCheckBoxAndAddToSizer(sizerLeft, _T("&Bitmap button"));
32b8ec41
VZ
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__
8772bb08 196 // only wxUniv currently supports buttons with images
32b8ec41
VZ
197 m_chkImage->Disable();
198#endif // !wxUniv
199
3e764e2f
VZ
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);
32b8ec41
VZ
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
206d3a16 227 m_radioHAlign = new wxRadioBox(this, wxID_ANY, _T("&Horz alignment"),
32b8ec41
VZ
228 wxDefaultPosition, wxDefaultSize,
229 WXSIZEOF(halign), halign);
206d3a16 230 m_radioVAlign = new wxRadioBox(this, wxID_ANY, _T("&Vert alignment"),
32b8ec41
VZ
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
206d3a16 243 wxStaticBox *box2 = new wxStaticBox(this, wxID_ANY, _T("&Operations"));
32b8ec41
VZ
244 wxSizer *sizerMiddle = new wxStaticBoxSizer(box2, wxVERTICAL);
245
246 wxSizer *sizerRow = CreateSizerWithTextAndButton(ButtonPage_ChangeLabel,
247 _T("Change label"),
206d3a16 248 wxID_ANY,
32b8ec41 249 &m_textLabel);
552271d6 250 m_textLabel->SetValue(_T("&Press me!"));
32b8ec41
VZ
251
252 sizerMiddle->Add(sizerRow, 0, wxALL | wxGROW, 5);
253
254 // right pane
552271d6
VZ
255 m_sizerButton = new wxBoxSizer(wxHORIZONTAL);
256 m_sizerButton->SetMinSize(150, 0);
32b8ec41
VZ
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);
552271d6 261 sizerTop->Add(m_sizerButton, 1, wxGROW | (wxALL & ~wxRIGHT), 10);
32b8ec41 262
3e764e2f 263 // do create the main control
32b8ec41 264 Reset();
3e764e2f 265 CreateButton();
552271d6 266
32b8ec41
VZ
267 SetSizer(sizerTop);
268
269 sizerTop->Fit(this);
270}
271
32b8ec41
VZ
272// ----------------------------------------------------------------------------
273// operations
274// ----------------------------------------------------------------------------
275
276void ButtonWidgetsPage::Reset()
277{
552271d6 278 m_chkBitmap->SetValue(false);
206d3a16
JS
279 m_chkFit->SetValue(true);
280 m_chkImage->SetValue(false);
281 m_chkDefault->SetValue(false);
32b8ec41 282
3e764e2f
VZ
283 m_chkUseSelected->SetValue(true);
284 m_chkUseFocused->SetValue(true);
285 m_chkUseHover->SetValue(true);
286 m_chkUseDisabled->SetValue(true);
287
32b8ec41
VZ
288 m_radioHAlign->SetSelection(ButtonHAlign_Centre);
289 m_radioVAlign->SetSelection(ButtonVAlign_Centre);
290}
291
292void 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 {
9dd96c0f 302 m_sizerButton->Remove( 0 );
32b8ec41
VZ
303 }
304
305 delete m_button;
306 }
552271d6
VZ
307
308 if ( label.empty() )
32b8ec41 309 {
552271d6
VZ
310 // creating for the first time or recreating a button after bitmap
311 // button
312 label = m_textLabel->GetValue();
32b8ec41
VZ
313 }
314
1301e228 315 int flags = ms_defaultFlags;
32b8ec41
VZ
316 switch ( m_radioHAlign->GetSelection() )
317 {
318 case ButtonHAlign_Left:
c0495687 319 flags |= wxBU_LEFT;
32b8ec41
VZ
320 break;
321
322 default:
323 wxFAIL_MSG(_T("unexpected radiobox selection"));
324 // fall through
325
326 case ButtonHAlign_Centre:
32b8ec41
VZ
327 break;
328
329 case ButtonHAlign_Right:
c0495687 330 flags |= wxBU_RIGHT;
32b8ec41
VZ
331 break;
332 }
333
334 switch ( m_radioVAlign->GetSelection() )
335 {
336 case ButtonVAlign_Top:
c0495687 337 flags |= wxBU_TOP;
32b8ec41
VZ
338 break;
339
340 default:
341 wxFAIL_MSG(_T("unexpected radiobox selection"));
342 // fall through
343
344 case ButtonVAlign_Centre:
4ee8e5cd 345 // centre vertical alignment is the default (no style)
32b8ec41
VZ
346 break;
347
348 case ButtonVAlign_Bottom:
c0495687 349 flags |= wxBU_BOTTOM;
32b8ec41
VZ
350 break;
351 }
352
3e764e2f
VZ
353 const bool isBitmapButton = m_chkBitmap->GetValue();
354 if ( isBitmapButton )
552271d6
VZ
355 {
356 wxBitmapButton *bbtn = new wxBitmapButton(this, ButtonPage_Button,
357 CreateBitmap(_T("normal")));
3e764e2f
VZ
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")));
552271d6
VZ
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 }
32b8ec41 374
3e764e2f
VZ
375 m_chkUseSelected->Enable(isBitmapButton);
376 m_chkUseFocused->Enable(isBitmapButton);
377 m_chkUseHover->Enable(isBitmapButton);
378 m_chkUseDisabled->Enable(isBitmapButton);
379
32b8ec41
VZ
380#ifdef __WXUNIVERSAL__
381 if ( m_chkImage->GetValue() )
382 {
ff8ebfaf 383 m_button->SetImageLabel(wxArtProvider::GetIcon(wxART_INFORMATION));
32b8ec41
VZ
384 }
385#endif // wxUniv
386
387 if ( m_chkDefault->GetValue() )
388 {
389 m_button->SetDefault();
390 }
391
552271d6
VZ
392 AddButtonToSizer();
393
394 m_sizerButton->Layout();
395}
396
397void ButtonWidgetsPage::AddButtonToSizer()
398{
32b8ec41
VZ
399 if ( m_chkFit->GetValue() )
400 {
552271d6
VZ
401 m_sizerButton->AddStretchSpacer(1);
402 m_sizerButton->Add(m_button, wxSizerFlags(0).Centre().Border());
403 m_sizerButton->AddStretchSpacer(1);
32b8ec41 404 }
552271d6 405 else // take up the entire space
32b8ec41 406 {
552271d6 407 m_sizerButton->Add(m_button, wxSizerFlags(1).Expand().Border());
32b8ec41 408 }
32b8ec41
VZ
409}
410
411// ----------------------------------------------------------------------------
412// event handlers
413// ----------------------------------------------------------------------------
414
415void ButtonWidgetsPage::OnButtonReset(wxCommandEvent& WXUNUSED(event))
416{
417 Reset();
418
419 CreateButton();
420}
421
c02e5a31 422void ButtonWidgetsPage::OnCheckOrRadioBox(wxCommandEvent& WXUNUSED(event))
32b8ec41
VZ
423{
424 CreateButton();
425}
426
427void ButtonWidgetsPage::OnButtonChangeLabel(wxCommandEvent& WXUNUSED(event))
428{
429 m_button->SetLabel(m_textLabel->GetValue());
430}
431
c02e5a31 432void ButtonWidgetsPage::OnButton(wxCommandEvent& WXUNUSED(event))
32b8ec41
VZ
433{
434 wxLogMessage(_T("Test button clicked."));
435}
436
552271d6
VZ
437// ----------------------------------------------------------------------------
438// bitmap button stuff
439// ----------------------------------------------------------------------------
440
441wxBitmap 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);
3e764e2f 449 dc.DrawLabel(wxStripMenuCodes(m_textLabel->GetValue()) + _T("\n")
552271d6
VZ
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