]> git.saurik.com Git - wxWidgets.git/blob - samples/widgets/button.cpp
Move menu messages handling from wxFrame to wxTLW in wxMSW.
[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 // Licence: wxWindows licence
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 #include "wx/commandlinkbutton.h"
44
45 #include "widgets.h"
46
47 #include "icons/button.xpm"
48
49 // ----------------------------------------------------------------------------
50 // constants
51 // ----------------------------------------------------------------------------
52
53 // control ids
54 enum
55 {
56 ButtonPage_Reset = wxID_HIGHEST,
57 ButtonPage_ChangeLabel,
58 ButtonPage_ChangeNote,
59 ButtonPage_Button
60 };
61
62 // radio boxes
63 enum
64 {
65 ButtonImagePos_Left,
66 ButtonImagePos_Right,
67 ButtonImagePos_Top,
68 ButtonImagePos_Bottom
69 };
70
71 enum
72 {
73 ButtonHAlign_Left,
74 ButtonHAlign_Centre,
75 ButtonHAlign_Right
76 };
77
78 enum
79 {
80 ButtonVAlign_Top,
81 ButtonVAlign_Centre,
82 ButtonVAlign_Bottom
83 };
84
85 // ----------------------------------------------------------------------------
86 // ButtonWidgetsPage
87 // ----------------------------------------------------------------------------
88
89 class ButtonWidgetsPage : public WidgetsPage
90 {
91 public:
92 ButtonWidgetsPage(WidgetsBookCtrl *book, wxImageList *imaglist);
93 virtual ~ButtonWidgetsPage(){};
94
95 virtual wxControl *GetWidget() const { return m_button; }
96 virtual void RecreateWidget() { CreateButton(); }
97
98 // lazy creation of the content
99 virtual void CreateContent();
100
101 protected:
102 // event handlers
103 void OnCheckOrRadioBox(wxCommandEvent& event);
104
105 void OnButton(wxCommandEvent& event);
106 void OnButtonReset(wxCommandEvent& event);
107 void OnButtonChangeLabel(wxCommandEvent& event);
108 void OnButtonChangeNote(wxCommandEvent& event);
109
110 // reset the wxButton parameters
111 void Reset();
112
113 // (re)create the wxButton
114 void CreateButton();
115
116 // add m_button to m_sizerButton using current value of m_chkFit
117 void AddButtonToSizer();
118
119 // helper function: create a bitmap for wxBitmapButton
120 wxBitmap CreateBitmap(const wxString& label);
121
122
123 // the controls
124 // ------------
125
126 // the check/radio boxes for styles
127 wxCheckBox *m_chkBitmapOnly,
128 *m_chkTextAndBitmap,
129 *m_chkFit,
130 *m_chkAuthNeeded,
131 #if wxUSE_COMMANDLINKBUTTON
132 *m_chkCommandLink,
133 #endif // wxUSE_COMMANDLINKBUTTON
134 #if wxUSE_MARKUP
135 *m_chkUseMarkup,
136 #endif // wxUSE_MARKUP
137 *m_chkDefault,
138 *m_chkUseBitmapClass;
139
140 // more checkboxes for wxBitmapButton only
141 wxCheckBox *m_chkUsePressed,
142 *m_chkUseFocused,
143 *m_chkUseCurrent,
144 *m_chkUseDisabled;
145
146 // and an image position choice used if m_chkTextAndBitmap is on
147 wxRadioBox *m_radioImagePos;
148
149 wxRadioBox *m_radioHAlign,
150 *m_radioVAlign;
151
152 // the button itself and the sizer it is in
153 wxButton *m_button;
154
155 #if wxUSE_COMMANDLINKBUTTON
156 // same as m_button or NULL if not showing a command link button currently
157 wxCommandLinkButton *m_cmdLnkButton;
158 #endif // wxUSE_COMMANDLINKBUTTON
159
160 wxSizer *m_sizerButton;
161
162 // the text entries for command parameters
163 wxTextCtrl *m_textLabel;
164
165 #if wxUSE_COMMANDLINKBUTTON
166 wxTextCtrl *m_textNote;
167
168 // used to hide or show button for changing note
169 wxSizer *m_sizerNote;
170 #endif // wxUSE_COMMANDLINKBUTTON
171
172 private:
173 DECLARE_EVENT_TABLE()
174 DECLARE_WIDGETS_PAGE(ButtonWidgetsPage)
175 };
176
177 // ----------------------------------------------------------------------------
178 // event tables
179 // ----------------------------------------------------------------------------
180
181 BEGIN_EVENT_TABLE(ButtonWidgetsPage, WidgetsPage)
182 EVT_BUTTON(ButtonPage_Button, ButtonWidgetsPage::OnButton)
183
184 EVT_BUTTON(ButtonPage_Reset, ButtonWidgetsPage::OnButtonReset)
185 EVT_BUTTON(ButtonPage_ChangeLabel, ButtonWidgetsPage::OnButtonChangeLabel)
186 EVT_BUTTON(ButtonPage_ChangeNote, ButtonWidgetsPage::OnButtonChangeNote)
187
188 EVT_CHECKBOX(wxID_ANY, ButtonWidgetsPage::OnCheckOrRadioBox)
189 EVT_RADIOBOX(wxID_ANY, ButtonWidgetsPage::OnCheckOrRadioBox)
190 END_EVENT_TABLE()
191
192 // ============================================================================
193 // implementation
194 // ============================================================================
195
196 #if defined(__WXUNIVERSAL__)
197 #define FAMILY_CTRLS UNIVERSAL_CTRLS
198 #else
199 #define FAMILY_CTRLS NATIVE_CTRLS
200 #endif
201
202 IMPLEMENT_WIDGETS_PAGE(ButtonWidgetsPage, wxT("Button"), FAMILY_CTRLS );
203
204 ButtonWidgetsPage::ButtonWidgetsPage(WidgetsBookCtrl *book,
205 wxImageList *imaglist)
206 : WidgetsPage(book, imaglist, button_xpm)
207 {
208 // init everything
209 m_chkBitmapOnly =
210 m_chkTextAndBitmap =
211 m_chkFit =
212 m_chkAuthNeeded =
213 #if wxUSE_COMMANDLINKBUTTON
214 m_chkCommandLink =
215 #endif // wxUSE_COMMANDLINKBUTTON
216 #if wxUSE_MARKUP
217 m_chkUseMarkup =
218 #endif // wxUSE_MARKUP
219 m_chkDefault =
220 m_chkUseBitmapClass =
221 m_chkUsePressed =
222 m_chkUseFocused =
223 m_chkUseCurrent =
224 m_chkUseDisabled = (wxCheckBox *)NULL;
225
226 m_radioImagePos =
227 m_radioHAlign =
228 m_radioVAlign = (wxRadioBox *)NULL;
229
230 m_textLabel = (wxTextCtrl *)NULL;
231
232 m_button = (wxButton *)NULL;
233 m_sizerButton = (wxSizer *)NULL;
234 }
235
236 void ButtonWidgetsPage::CreateContent()
237 {
238 wxSizer *sizerTop = new wxBoxSizer(wxHORIZONTAL);
239
240 // left pane
241 wxStaticBox *box = new wxStaticBox(this, wxID_ANY, wxT("&Set style"));
242
243 wxSizer *sizerLeft = new wxStaticBoxSizer(box, wxVERTICAL);
244
245 m_chkBitmapOnly = CreateCheckBoxAndAddToSizer(sizerLeft, "&Bitmap only");
246 m_chkTextAndBitmap = CreateCheckBoxAndAddToSizer(sizerLeft, "Text &and bitmap");
247 m_chkFit = CreateCheckBoxAndAddToSizer(sizerLeft, wxT("&Fit exactly"));
248 m_chkAuthNeeded = CreateCheckBoxAndAddToSizer(sizerLeft, wxT("Require a&uth"));
249 #if wxUSE_COMMANDLINKBUTTON
250 m_chkCommandLink = CreateCheckBoxAndAddToSizer(sizerLeft, wxT("Use command &link button"));
251 #endif
252 #if wxUSE_MARKUP
253 m_chkUseMarkup = CreateCheckBoxAndAddToSizer(sizerLeft, "Interpret &markup");
254 #endif // wxUSE_MARKUP
255 m_chkDefault = CreateCheckBoxAndAddToSizer(sizerLeft, wxT("&Default"));
256
257 m_chkUseBitmapClass = CreateCheckBoxAndAddToSizer(sizerLeft,
258 "Use wxBitmapButton");
259 m_chkUseBitmapClass->SetValue(true);
260
261 sizerLeft->AddSpacer(5);
262
263 wxSizer *sizerUseLabels =
264 new wxStaticBoxSizer(wxVERTICAL, this,
265 "&Use the following bitmaps in addition to the normal one?");
266 m_chkUsePressed = CreateCheckBoxAndAddToSizer(sizerUseLabels,
267 "&Pressed (small help icon)");
268 m_chkUseFocused = CreateCheckBoxAndAddToSizer(sizerUseLabels,
269 "&Focused (small error icon)");
270 m_chkUseCurrent = CreateCheckBoxAndAddToSizer(sizerUseLabels,
271 "&Current (small warning icon)");
272 m_chkUseDisabled = CreateCheckBoxAndAddToSizer(sizerUseLabels,
273 "&Disabled (broken image icon)");
274 sizerLeft->Add(sizerUseLabels, wxSizerFlags().Expand().Border());
275
276 sizerLeft->AddSpacer(10);
277
278 static const wxString dirs[] =
279 {
280 "left", "right", "top", "bottom",
281 };
282 m_radioImagePos = new wxRadioBox(this, wxID_ANY, "Image &position",
283 wxDefaultPosition, wxDefaultSize,
284 WXSIZEOF(dirs), dirs);
285 sizerLeft->Add(m_radioImagePos, 0, wxGROW | wxALL, 5);
286 sizerLeft->AddSpacer(15);
287
288 // should be in sync with enums Button[HV]Align!
289 static const wxString halign[] =
290 {
291 wxT("left"),
292 wxT("centre"),
293 wxT("right"),
294 };
295
296 static const wxString valign[] =
297 {
298 wxT("top"),
299 wxT("centre"),
300 wxT("bottom"),
301 };
302
303 m_radioHAlign = new wxRadioBox(this, wxID_ANY, wxT("&Horz alignment"),
304 wxDefaultPosition, wxDefaultSize,
305 WXSIZEOF(halign), halign);
306 m_radioVAlign = new wxRadioBox(this, wxID_ANY, wxT("&Vert alignment"),
307 wxDefaultPosition, wxDefaultSize,
308 WXSIZEOF(valign), valign);
309
310 sizerLeft->Add(m_radioHAlign, 0, wxGROW | wxALL, 5);
311 sizerLeft->Add(m_radioVAlign, 0, wxGROW | wxALL, 5);
312
313 sizerLeft->Add(5, 5, 0, wxGROW | wxALL, 5); // spacer
314
315 wxButton *btn = new wxButton(this, ButtonPage_Reset, wxT("&Reset"));
316 sizerLeft->Add(btn, 0, wxALIGN_CENTRE_HORIZONTAL | wxALL, 15);
317
318 // middle pane
319 wxStaticBox *box2 = new wxStaticBox(this, wxID_ANY, wxT("&Operations"));
320 wxSizer *sizerMiddle = new wxStaticBoxSizer(box2, wxVERTICAL);
321
322 wxSizer *sizerRow = CreateSizerWithTextAndButton(ButtonPage_ChangeLabel,
323 wxT("Change label"),
324 wxID_ANY,
325 &m_textLabel);
326 m_textLabel->SetValue(wxT("&Press me!"));
327 sizerMiddle->Add(sizerRow, 0, wxALL | wxGROW, 5);
328
329 #if wxUSE_COMMANDLINKBUTTON
330 m_sizerNote = CreateSizerWithTextAndButton(ButtonPage_ChangeNote,
331 wxT("Change note"),
332 wxID_ANY,
333 &m_textNote);
334 m_textNote->SetValue(wxT("Writes down button clicks in the log."));
335
336 sizerMiddle->Add(m_sizerNote, 0, wxALL | wxGROW, 5);
337 #endif
338
339 // right pane
340 m_sizerButton = new wxBoxSizer(wxHORIZONTAL);
341 m_sizerButton->SetMinSize(150, 0);
342
343 // the 3 panes panes compose the window
344 sizerTop->Add(sizerLeft, 0, wxGROW | (wxALL & ~wxLEFT), 10);
345 sizerTop->Add(sizerMiddle, 1, wxGROW | wxALL, 10);
346 sizerTop->Add(m_sizerButton, 1, wxGROW | (wxALL & ~wxRIGHT), 10);
347
348 // do create the main control
349 Reset();
350 CreateButton();
351
352 SetSizer(sizerTop);
353 }
354
355 // ----------------------------------------------------------------------------
356 // operations
357 // ----------------------------------------------------------------------------
358
359 void ButtonWidgetsPage::Reset()
360 {
361 m_chkBitmapOnly->SetValue(false);
362 m_chkFit->SetValue(true);
363 m_chkAuthNeeded->SetValue(false);
364 m_chkTextAndBitmap->SetValue(false);
365 m_chkDefault->SetValue(false);
366 #if wxUSE_COMMANDLINKBUTTON
367 m_chkCommandLink->SetValue(false);
368 #endif
369 #if wxUSE_MARKUP
370 m_chkUseMarkup->SetValue(false);
371 #endif // wxUSE_MARKUP
372 m_chkUseBitmapClass->SetValue(true);
373
374 m_chkUsePressed->SetValue(true);
375 m_chkUseFocused->SetValue(true);
376 m_chkUseCurrent->SetValue(true);
377 m_chkUseDisabled->SetValue(true);
378
379 m_radioImagePos->SetSelection(ButtonImagePos_Left);
380 m_radioHAlign->SetSelection(ButtonHAlign_Centre);
381 m_radioVAlign->SetSelection(ButtonVAlign_Centre);
382 }
383
384 void ButtonWidgetsPage::CreateButton()
385 {
386 wxString label;
387 if ( m_button )
388 {
389 #if wxUSE_COMMANDLINKBUTTON
390 if ( m_cmdLnkButton )
391 label = m_cmdLnkButton->GetMainLabel();
392 else
393 #endif
394 label = m_button->GetLabel();
395
396 size_t count = m_sizerButton->GetChildren().GetCount();
397 for ( size_t n = 0; n < count; n++ )
398 {
399 m_sizerButton->Remove( 0 );
400 }
401
402 delete m_button;
403 }
404
405 if ( label.empty() )
406 {
407 // creating for the first time or recreating a button after bitmap
408 // button
409 label = m_textLabel->GetValue();
410 }
411
412 int flags = ms_defaultFlags;
413 switch ( m_radioHAlign->GetSelection() )
414 {
415 case ButtonHAlign_Left:
416 flags |= wxBU_LEFT;
417 break;
418
419 default:
420 wxFAIL_MSG(wxT("unexpected radiobox selection"));
421 // fall through
422
423 case ButtonHAlign_Centre:
424 break;
425
426 case ButtonHAlign_Right:
427 flags |= wxBU_RIGHT;
428 break;
429 }
430
431 switch ( m_radioVAlign->GetSelection() )
432 {
433 case ButtonVAlign_Top:
434 flags |= wxBU_TOP;
435 break;
436
437 default:
438 wxFAIL_MSG(wxT("unexpected radiobox selection"));
439 // fall through
440
441 case ButtonVAlign_Centre:
442 // centre vertical alignment is the default (no style)
443 break;
444
445 case ButtonVAlign_Bottom:
446 flags |= wxBU_BOTTOM;
447 break;
448 }
449
450 #if wxUSE_COMMANDLINKBUTTON
451 m_sizerNote->Show(m_chkCommandLink->GetValue());
452 #endif
453
454 bool showsBitmap = false;
455 if ( m_chkBitmapOnly->GetValue() )
456 {
457 showsBitmap = true;
458
459 wxButton *bbtn;
460 if ( m_chkUseBitmapClass->GetValue() )
461 {
462 bbtn = new wxBitmapButton(this, ButtonPage_Button,
463 CreateBitmap(wxT("normal")));
464 }
465 else
466 {
467 bbtn = new wxButton(this, ButtonPage_Button);
468 bbtn->SetBitmapLabel(CreateBitmap(wxT("normal")));
469 }
470 if ( m_chkUsePressed->GetValue() )
471 bbtn->SetBitmapPressed(CreateBitmap(wxT("pushed")));
472 if ( m_chkUseFocused->GetValue() )
473 bbtn->SetBitmapFocus(CreateBitmap(wxT("focused")));
474 if ( m_chkUseCurrent->GetValue() )
475 bbtn->SetBitmapCurrent(CreateBitmap(wxT("hover")));
476 if ( m_chkUseDisabled->GetValue() )
477 bbtn->SetBitmapDisabled(CreateBitmap(wxT("disabled")));
478 m_button = bbtn;
479 #if wxUSE_COMMANDLINKBUTTON
480 m_cmdLnkButton = NULL;
481 #endif
482 }
483 else // normal button
484 {
485 #if wxUSE_COMMANDLINKBUTTON
486 m_cmdLnkButton = NULL;
487
488 if ( m_chkCommandLink->GetValue() )
489 {
490 m_cmdLnkButton = new wxCommandLinkButton(this, ButtonPage_Button,
491 label,
492 m_textNote->GetValue(),
493 wxDefaultPosition,
494 wxDefaultSize,
495 flags);
496 m_button = m_cmdLnkButton;
497 }
498 else
499 #endif // wxUSE_COMMANDLINKBUTTON
500 {
501 m_button = new wxButton(this, ButtonPage_Button, label,
502 wxDefaultPosition, wxDefaultSize,
503 flags);
504 }
505 }
506
507 if ( !showsBitmap && m_chkTextAndBitmap->GetValue() )
508 {
509 showsBitmap = true;
510
511 static const wxDirection positions[] =
512 {
513 wxLEFT, wxRIGHT, wxTOP, wxBOTTOM
514 };
515
516 m_button->SetBitmap(wxArtProvider::GetIcon(wxART_INFORMATION, wxART_BUTTON),
517 positions[m_radioImagePos->GetSelection()]);
518
519 if ( m_chkUsePressed->GetValue() )
520 m_button->SetBitmapPressed(wxArtProvider::GetIcon(wxART_HELP, wxART_BUTTON));
521 if ( m_chkUseFocused->GetValue() )
522 m_button->SetBitmapFocus(wxArtProvider::GetIcon(wxART_ERROR, wxART_BUTTON));
523 if ( m_chkUseCurrent->GetValue() )
524 m_button->SetBitmapCurrent(wxArtProvider::GetIcon(wxART_WARNING, wxART_BUTTON));
525 if ( m_chkUseDisabled->GetValue() )
526 m_button->SetBitmapDisabled(wxArtProvider::GetIcon(wxART_MISSING_IMAGE, wxART_BUTTON));
527 }
528
529 m_chkUseBitmapClass->Enable(showsBitmap);
530
531 m_chkUsePressed->Enable(showsBitmap);
532 m_chkUseFocused->Enable(showsBitmap);
533 m_chkUseCurrent->Enable(showsBitmap);
534 m_chkUseDisabled->Enable(showsBitmap);
535
536 if ( m_chkAuthNeeded->GetValue() )
537 m_button->SetAuthNeeded();
538
539 if ( m_chkDefault->GetValue() )
540 m_button->SetDefault();
541
542 AddButtonToSizer();
543
544 m_sizerButton->Layout();
545 }
546
547 void ButtonWidgetsPage::AddButtonToSizer()
548 {
549 if ( m_chkFit->GetValue() )
550 {
551 m_sizerButton->AddStretchSpacer(1);
552 m_sizerButton->Add(m_button, wxSizerFlags(0).Centre().Border());
553 m_sizerButton->AddStretchSpacer(1);
554 }
555 else // take up the entire space
556 {
557 m_sizerButton->Add(m_button, wxSizerFlags(1).Expand().Border());
558 }
559 }
560
561 // ----------------------------------------------------------------------------
562 // event handlers
563 // ----------------------------------------------------------------------------
564
565 void ButtonWidgetsPage::OnButtonReset(wxCommandEvent& WXUNUSED(event))
566 {
567 Reset();
568
569 CreateButton();
570 }
571
572 void ButtonWidgetsPage::OnCheckOrRadioBox(wxCommandEvent& WXUNUSED(event))
573 {
574 CreateButton();
575 Layout(); // make sure the text field for changing note displays correctly.
576 }
577
578 void ButtonWidgetsPage::OnButtonChangeLabel(wxCommandEvent& WXUNUSED(event))
579 {
580 const wxString labelText = m_textLabel->GetValue();
581
582 #if wxUSE_COMMANDLINKBUTTON
583 if ( m_cmdLnkButton )
584 m_cmdLnkButton->SetMainLabel(labelText);
585 else
586 #endif // wxUSE_COMMANDLINKBUTTON
587 {
588 #if wxUSE_MARKUP
589 if ( m_chkUseMarkup->GetValue() )
590 m_button->SetLabelMarkup(labelText);
591 else
592 #endif // wxUSE_MARKUP
593 m_button->SetLabel(labelText);
594 }
595
596 m_sizerButton->Layout();
597 }
598
599 void ButtonWidgetsPage::OnButtonChangeNote(wxCommandEvent& WXUNUSED(event))
600 {
601 #if wxUSE_COMMANDLINKBUTTON
602 m_cmdLnkButton->SetNote(m_textNote->GetValue());
603
604 m_sizerButton->Layout();
605 #endif // wxUSE_COMMANDLINKBUTTON
606 }
607
608 void ButtonWidgetsPage::OnButton(wxCommandEvent& WXUNUSED(event))
609 {
610 wxLogMessage(wxT("Test button clicked."));
611 }
612
613 // ----------------------------------------------------------------------------
614 // bitmap button stuff
615 // ----------------------------------------------------------------------------
616
617 wxBitmap ButtonWidgetsPage::CreateBitmap(const wxString& label)
618 {
619 wxBitmap bmp(180, 70); // shouldn't hardcode but it's simpler like this
620 wxMemoryDC dc;
621 dc.SelectObject(bmp);
622 dc.SetBackground(*wxCYAN_BRUSH);
623 dc.Clear();
624 dc.SetTextForeground(*wxBLACK);
625 dc.DrawLabel(wxStripMenuCodes(m_textLabel->GetValue()) + wxT("\n")
626 wxT("(") + label + wxT(" state)"),
627 wxArtProvider::GetBitmap(wxART_INFORMATION),
628 wxRect(10, 10, bmp.GetWidth() - 20, bmp.GetHeight() - 20),
629 wxALIGN_CENTRE);
630
631 return bmp;
632 }
633