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