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