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