]> git.saurik.com Git - wxWidgets.git/blame - samples/widgets/button.cpp
Correct placement of calling convention keyword: must follow the return type.
[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
233f10bf
VZ
61enum
62{
63 ButtonImagePos_Left,
64 ButtonImagePos_Right,
65 ButtonImagePos_Top,
66 ButtonImagePos_Bottom
67};
68
32b8ec41
VZ
69enum
70{
71 ButtonHAlign_Left,
72 ButtonHAlign_Centre,
73 ButtonHAlign_Right
74};
75
76enum
77{
78 ButtonVAlign_Top,
79 ButtonVAlign_Centre,
80 ButtonVAlign_Bottom
81};
82
83// ----------------------------------------------------------------------------
84// ButtonWidgetsPage
85// ----------------------------------------------------------------------------
86
87class ButtonWidgetsPage : public WidgetsPage
88{
89public:
f2fdc4d5 90 ButtonWidgetsPage(WidgetsBookCtrl *book, wxImageList *imaglist);
8f6eaec9 91 virtual ~ButtonWidgetsPage(){};
32b8ec41 92
195df7a7 93 virtual wxControl *GetWidget() const { return m_button; }
1301e228 94 virtual void RecreateWidget() { CreateButton(); }
195df7a7 95
453535a7
WS
96 // lazy creation of the content
97 virtual void CreateContent();
98
32b8ec41
VZ
99protected:
100 // event handlers
101 void OnCheckOrRadioBox(wxCommandEvent& event);
102
103 void OnButton(wxCommandEvent& event);
104 void OnButtonReset(wxCommandEvent& event);
105 void OnButtonChangeLabel(wxCommandEvent& event);
106
107 // reset the wxButton parameters
108 void Reset();
109
110 // (re)create the wxButton
111 void CreateButton();
112
552271d6
VZ
113 // add m_button to m_sizerButton using current value of m_chkFit
114 void AddButtonToSizer();
115
116 // helper function: create a bitmap for wxBitmapButton
117 wxBitmap CreateBitmap(const wxString& label);
118
119
32b8ec41
VZ
120 // the controls
121 // ------------
122
123 // the check/radio boxes for styles
d5b98eb9
VZ
124 wxCheckBox *m_chkBitmapOnly,
125 *m_chkTextAndBitmap,
32b8ec41
VZ
126 *m_chkFit,
127 *m_chkDefault;
128
3e764e2f 129 // more checkboxes for wxBitmapButton only
d5b98eb9 130 wxCheckBox *m_chkUsePressed,
3e764e2f 131 *m_chkUseFocused,
d5b98eb9 132 *m_chkUseCurrent,
3e764e2f
VZ
133 *m_chkUseDisabled;
134
d5b98eb9 135 // and an image position choice used if m_chkTextAndBitmap is on
233f10bf
VZ
136 wxRadioBox *m_radioImagePos;
137
32b8ec41
VZ
138 wxRadioBox *m_radioHAlign,
139 *m_radioVAlign;
140
8772bb08 141 // the button itself and the sizer it is in
32b8ec41
VZ
142 wxButton *m_button;
143 wxSizer *m_sizerButton;
144
145 // the text entries for command parameters
146 wxTextCtrl *m_textLabel;
147
148private:
5e173f35
GD
149 DECLARE_EVENT_TABLE()
150 DECLARE_WIDGETS_PAGE(ButtonWidgetsPage)
32b8ec41
VZ
151};
152
153// ----------------------------------------------------------------------------
154// event tables
155// ----------------------------------------------------------------------------
156
157BEGIN_EVENT_TABLE(ButtonWidgetsPage, WidgetsPage)
158 EVT_BUTTON(ButtonPage_Button, ButtonWidgetsPage::OnButton)
159
160 EVT_BUTTON(ButtonPage_Reset, ButtonWidgetsPage::OnButtonReset)
161 EVT_BUTTON(ButtonPage_ChangeLabel, ButtonWidgetsPage::OnButtonChangeLabel)
162
206d3a16
JS
163 EVT_CHECKBOX(wxID_ANY, ButtonWidgetsPage::OnCheckOrRadioBox)
164 EVT_RADIOBOX(wxID_ANY, ButtonWidgetsPage::OnCheckOrRadioBox)
32b8ec41
VZ
165END_EVENT_TABLE()
166
167// ============================================================================
168// implementation
169// ============================================================================
170
d8d07a79 171#if defined(__WXUNIVERSAL__)
d8a731ee 172 #define FAMILY_CTRLS UNIVERSAL_CTRLS
d8d07a79 173#else
f0fa4312 174 #define FAMILY_CTRLS NATIVE_CTRLS
d8d07a79 175#endif
d8a731ee 176
9a83f860 177IMPLEMENT_WIDGETS_PAGE(ButtonWidgetsPage, wxT("Button"), FAMILY_CTRLS );
32b8ec41 178
f2fdc4d5 179ButtonWidgetsPage::ButtonWidgetsPage(WidgetsBookCtrl *book,
61c083e7 180 wxImageList *imaglist)
261357eb 181 : WidgetsPage(book, imaglist, button_xpm)
32b8ec41 182{
32b8ec41 183 // init everything
d5b98eb9
VZ
184 m_chkBitmapOnly =
185 m_chkTextAndBitmap =
32b8ec41 186 m_chkFit =
3e764e2f 187 m_chkDefault =
d5b98eb9 188 m_chkUsePressed =
3e764e2f 189 m_chkUseFocused =
d5b98eb9 190 m_chkUseCurrent =
3e764e2f 191 m_chkUseDisabled = (wxCheckBox *)NULL;
32b8ec41 192
233f10bf 193 m_radioImagePos =
32b8ec41
VZ
194 m_radioHAlign =
195 m_radioVAlign = (wxRadioBox *)NULL;
196
197 m_textLabel = (wxTextCtrl *)NULL;
198
199 m_button = (wxButton *)NULL;
200 m_sizerButton = (wxSizer *)NULL;
453535a7 201}
32b8ec41 202
453535a7
WS
203void ButtonWidgetsPage::CreateContent()
204{
32b8ec41
VZ
205 wxSizer *sizerTop = new wxBoxSizer(wxHORIZONTAL);
206
207 // left pane
9a83f860 208 wxStaticBox *box = new wxStaticBox(this, wxID_ANY, wxT("&Set style"));
32b8ec41
VZ
209
210 wxSizer *sizerLeft = new wxStaticBoxSizer(box, wxVERTICAL);
211
d5b98eb9
VZ
212 m_chkBitmapOnly = CreateCheckBoxAndAddToSizer(sizerLeft, "&Bitmap only");
213 m_chkTextAndBitmap = CreateCheckBoxAndAddToSizer(sizerLeft, "Text &and &bitmap");
9a83f860
VZ
214 m_chkFit = CreateCheckBoxAndAddToSizer(sizerLeft, wxT("&Fit exactly"));
215 m_chkDefault = CreateCheckBoxAndAddToSizer(sizerLeft, wxT("&Default"));
32b8ec41 216
3e764e2f
VZ
217 sizerLeft->AddSpacer(5);
218
219 wxSizer *sizerUseLabels =
d5b98eb9
VZ
220 new wxStaticBoxSizer(wxVERTICAL, this,
221 "&Use the following bitmaps in addition to the normal one?");
222 m_chkUsePressed = CreateCheckBoxAndAddToSizer(sizerUseLabels,
223 "&Pressed (small help icon)");
224 m_chkUseFocused = CreateCheckBoxAndAddToSizer(sizerUseLabels,
225 "&Focused (small error icon)");
226 m_chkUseCurrent = CreateCheckBoxAndAddToSizer(sizerUseLabels,
227 "&Current (small warning icon)");
228 m_chkUseDisabled = CreateCheckBoxAndAddToSizer(sizerUseLabels,
229 "&Disabled (broken image icon)");
3e764e2f
VZ
230 sizerLeft->Add(sizerUseLabels, wxSizerFlags().Expand().Border());
231
233f10bf
VZ
232 sizerLeft->AddSpacer(10);
233
234 static const wxString dirs[] =
235 {
236 "left", "right", "top", "bottom",
237 };
238 m_radioImagePos = new wxRadioBox(this, wxID_ANY, "Image &position",
239 wxDefaultPosition, wxDefaultSize,
240 WXSIZEOF(dirs), dirs);
241 sizerLeft->Add(m_radioImagePos, 0, wxGROW | wxALL, 5);
3e764e2f 242 sizerLeft->AddSpacer(15);
32b8ec41
VZ
243
244 // should be in sync with enums Button[HV]Align!
245 static const wxString halign[] =
246 {
9a83f860
VZ
247 wxT("left"),
248 wxT("centre"),
249 wxT("right"),
32b8ec41
VZ
250 };
251
252 static const wxString valign[] =
253 {
9a83f860
VZ
254 wxT("top"),
255 wxT("centre"),
256 wxT("bottom"),
32b8ec41
VZ
257 };
258
9a83f860 259 m_radioHAlign = new wxRadioBox(this, wxID_ANY, wxT("&Horz alignment"),
32b8ec41
VZ
260 wxDefaultPosition, wxDefaultSize,
261 WXSIZEOF(halign), halign);
9a83f860 262 m_radioVAlign = new wxRadioBox(this, wxID_ANY, wxT("&Vert alignment"),
32b8ec41
VZ
263 wxDefaultPosition, wxDefaultSize,
264 WXSIZEOF(valign), valign);
265
266 sizerLeft->Add(m_radioHAlign, 0, wxGROW | wxALL, 5);
267 sizerLeft->Add(m_radioVAlign, 0, wxGROW | wxALL, 5);
268
269 sizerLeft->Add(5, 5, 0, wxGROW | wxALL, 5); // spacer
270
9a83f860 271 wxButton *btn = new wxButton(this, ButtonPage_Reset, wxT("&Reset"));
32b8ec41
VZ
272 sizerLeft->Add(btn, 0, wxALIGN_CENTRE_HORIZONTAL | wxALL, 15);
273
274 // middle pane
9a83f860 275 wxStaticBox *box2 = new wxStaticBox(this, wxID_ANY, wxT("&Operations"));
32b8ec41
VZ
276 wxSizer *sizerMiddle = new wxStaticBoxSizer(box2, wxVERTICAL);
277
278 wxSizer *sizerRow = CreateSizerWithTextAndButton(ButtonPage_ChangeLabel,
9a83f860 279 wxT("Change label"),
206d3a16 280 wxID_ANY,
32b8ec41 281 &m_textLabel);
9a83f860 282 m_textLabel->SetValue(wxT("&Press me!"));
32b8ec41
VZ
283
284 sizerMiddle->Add(sizerRow, 0, wxALL | wxGROW, 5);
285
286 // right pane
552271d6
VZ
287 m_sizerButton = new wxBoxSizer(wxHORIZONTAL);
288 m_sizerButton->SetMinSize(150, 0);
32b8ec41
VZ
289
290 // the 3 panes panes compose the window
291 sizerTop->Add(sizerLeft, 0, wxGROW | (wxALL & ~wxLEFT), 10);
292 sizerTop->Add(sizerMiddle, 1, wxGROW | wxALL, 10);
552271d6 293 sizerTop->Add(m_sizerButton, 1, wxGROW | (wxALL & ~wxRIGHT), 10);
32b8ec41 294
3e764e2f 295 // do create the main control
32b8ec41 296 Reset();
3e764e2f 297 CreateButton();
552271d6 298
32b8ec41 299 SetSizer(sizerTop);
32b8ec41
VZ
300}
301
32b8ec41
VZ
302// ----------------------------------------------------------------------------
303// operations
304// ----------------------------------------------------------------------------
305
306void ButtonWidgetsPage::Reset()
307{
d5b98eb9 308 m_chkBitmapOnly->SetValue(false);
206d3a16 309 m_chkFit->SetValue(true);
d5b98eb9 310 m_chkTextAndBitmap->SetValue(false);
206d3a16 311 m_chkDefault->SetValue(false);
32b8ec41 312
d5b98eb9 313 m_chkUsePressed->SetValue(true);
3e764e2f 314 m_chkUseFocused->SetValue(true);
d5b98eb9 315 m_chkUseCurrent->SetValue(true);
3e764e2f
VZ
316 m_chkUseDisabled->SetValue(true);
317
233f10bf 318 m_radioImagePos->SetSelection(ButtonImagePos_Left);
32b8ec41
VZ
319 m_radioHAlign->SetSelection(ButtonHAlign_Centre);
320 m_radioVAlign->SetSelection(ButtonVAlign_Centre);
321}
322
323void ButtonWidgetsPage::CreateButton()
324{
325 wxString label;
326 if ( m_button )
327 {
328 label = m_button->GetLabel();
329
330 size_t count = m_sizerButton->GetChildren().GetCount();
331 for ( size_t n = 0; n < count; n++ )
332 {
9dd96c0f 333 m_sizerButton->Remove( 0 );
32b8ec41
VZ
334 }
335
336 delete m_button;
337 }
552271d6
VZ
338
339 if ( label.empty() )
32b8ec41 340 {
552271d6
VZ
341 // creating for the first time or recreating a button after bitmap
342 // button
343 label = m_textLabel->GetValue();
32b8ec41
VZ
344 }
345
1301e228 346 int flags = ms_defaultFlags;
32b8ec41
VZ
347 switch ( m_radioHAlign->GetSelection() )
348 {
349 case ButtonHAlign_Left:
c0495687 350 flags |= wxBU_LEFT;
32b8ec41
VZ
351 break;
352
353 default:
9a83f860 354 wxFAIL_MSG(wxT("unexpected radiobox selection"));
32b8ec41
VZ
355 // fall through
356
357 case ButtonHAlign_Centre:
32b8ec41
VZ
358 break;
359
360 case ButtonHAlign_Right:
c0495687 361 flags |= wxBU_RIGHT;
32b8ec41
VZ
362 break;
363 }
364
365 switch ( m_radioVAlign->GetSelection() )
366 {
367 case ButtonVAlign_Top:
c0495687 368 flags |= wxBU_TOP;
32b8ec41
VZ
369 break;
370
371 default:
9a83f860 372 wxFAIL_MSG(wxT("unexpected radiobox selection"));
32b8ec41
VZ
373 // fall through
374
375 case ButtonVAlign_Centre:
4ee8e5cd 376 // centre vertical alignment is the default (no style)
32b8ec41
VZ
377 break;
378
379 case ButtonVAlign_Bottom:
c0495687 380 flags |= wxBU_BOTTOM;
32b8ec41
VZ
381 break;
382 }
383
d5b98eb9
VZ
384 bool showsBitmap = false;
385 if ( m_chkBitmapOnly->GetValue() )
552271d6 386 {
d5b98eb9
VZ
387 showsBitmap = true;
388
552271d6 389 wxBitmapButton *bbtn = new wxBitmapButton(this, ButtonPage_Button,
9a83f860 390 CreateBitmap(wxT("normal")));
d5b98eb9 391 if ( m_chkUsePressed->GetValue() )
9a83f860 392 bbtn->SetBitmapPressed(CreateBitmap(wxT("pushed")));
3e764e2f 393 if ( m_chkUseFocused->GetValue() )
9a83f860 394 bbtn->SetBitmapFocus(CreateBitmap(wxT("focused")));
d5b98eb9 395 if ( m_chkUseCurrent->GetValue() )
9a83f860 396 bbtn->SetBitmapCurrent(CreateBitmap(wxT("hover")));
3e764e2f 397 if ( m_chkUseDisabled->GetValue() )
9a83f860 398 bbtn->SetBitmapDisabled(CreateBitmap(wxT("disabled")));
552271d6
VZ
399 m_button = bbtn;
400 }
401 else // normal button
402 {
403 m_button = new wxButton(this, ButtonPage_Button, label,
404 wxDefaultPosition, wxDefaultSize,
405 flags);
406 }
32b8ec41 407
d5b98eb9 408 if ( !showsBitmap && m_chkTextAndBitmap->GetValue() )
32b8ec41 409 {
d5b98eb9
VZ
410 showsBitmap = true;
411
233f10bf
VZ
412 static const wxDirection positions[] =
413 {
414 wxLEFT, wxRIGHT, wxTOP, wxBOTTOM
415 };
416
417 m_button->SetBitmap(wxArtProvider::GetIcon(wxART_INFORMATION),
418 positions[m_radioImagePos->GetSelection()]);
d5b98eb9
VZ
419
420 if ( m_chkUsePressed->GetValue() )
421 m_button->SetBitmapPressed(wxArtProvider::GetIcon(wxART_HELP));
422 if ( m_chkUseFocused->GetValue() )
423 m_button->SetBitmapFocus(wxArtProvider::GetIcon(wxART_ERROR));
424 if ( m_chkUseCurrent->GetValue() )
425 m_button->SetBitmapCurrent(wxArtProvider::GetIcon(wxART_WARNING));
426 if ( m_chkUseDisabled->GetValue() )
427 m_button->SetBitmapDisabled(wxArtProvider::GetIcon(wxART_MISSING_IMAGE));
32b8ec41 428 }
32b8ec41 429
d5b98eb9
VZ
430 m_chkUsePressed->Enable(showsBitmap);
431 m_chkUseFocused->Enable(showsBitmap);
432 m_chkUseCurrent->Enable(showsBitmap);
433 m_chkUseDisabled->Enable(showsBitmap);
434
32b8ec41
VZ
435 if ( m_chkDefault->GetValue() )
436 {
437 m_button->SetDefault();
438 }
439
552271d6
VZ
440 AddButtonToSizer();
441
442 m_sizerButton->Layout();
443}
444
445void ButtonWidgetsPage::AddButtonToSizer()
446{
32b8ec41
VZ
447 if ( m_chkFit->GetValue() )
448 {
552271d6
VZ
449 m_sizerButton->AddStretchSpacer(1);
450 m_sizerButton->Add(m_button, wxSizerFlags(0).Centre().Border());
451 m_sizerButton->AddStretchSpacer(1);
32b8ec41 452 }
552271d6 453 else // take up the entire space
32b8ec41 454 {
552271d6 455 m_sizerButton->Add(m_button, wxSizerFlags(1).Expand().Border());
32b8ec41 456 }
32b8ec41
VZ
457}
458
459// ----------------------------------------------------------------------------
460// event handlers
461// ----------------------------------------------------------------------------
462
463void ButtonWidgetsPage::OnButtonReset(wxCommandEvent& WXUNUSED(event))
464{
465 Reset();
466
467 CreateButton();
468}
469
c02e5a31 470void ButtonWidgetsPage::OnCheckOrRadioBox(wxCommandEvent& WXUNUSED(event))
32b8ec41
VZ
471{
472 CreateButton();
473}
474
475void ButtonWidgetsPage::OnButtonChangeLabel(wxCommandEvent& WXUNUSED(event))
476{
477 m_button->SetLabel(m_textLabel->GetValue());
d46a35d0
VZ
478
479 m_sizerButton->Layout();
32b8ec41
VZ
480}
481
c02e5a31 482void ButtonWidgetsPage::OnButton(wxCommandEvent& WXUNUSED(event))
32b8ec41 483{
9a83f860 484 wxLogMessage(wxT("Test button clicked."));
32b8ec41
VZ
485}
486
552271d6
VZ
487// ----------------------------------------------------------------------------
488// bitmap button stuff
489// ----------------------------------------------------------------------------
490
491wxBitmap ButtonWidgetsPage::CreateBitmap(const wxString& label)
492{
493 wxBitmap bmp(180, 70); // shouldn't hardcode but it's simpler like this
494 wxMemoryDC dc;
495 dc.SelectObject(bmp);
f2a4f50e 496 dc.SetBackground(wxBrush(*wxCYAN));
552271d6 497 dc.Clear();
f2a4f50e 498 dc.SetTextForeground(*wxBLACK);
9a83f860
VZ
499 dc.DrawLabel(wxStripMenuCodes(m_textLabel->GetValue()) + wxT("\n")
500 wxT("(") + label + wxT(" state)"),
552271d6
VZ
501 wxArtProvider::GetBitmap(wxART_INFORMATION),
502 wxRect(10, 10, bmp.GetWidth() - 20, bmp.GetHeight() - 20),
503 wxALIGN_CENTRE);
504
505 return bmp;
506}
507