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