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