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