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