1 /////////////////////////////////////////////////////////////////////////////
2 // Program: wxWidgets Widgets Sample
4 // Purpose: Part of the widgets sample showing wxButton
5 // Author: Vadim Zeitlin
7 // Copyright: (c) 2001 Vadim Zeitlin
8 // Licence: wxWindows licence
9 /////////////////////////////////////////////////////////////////////////////
11 // ============================================================================
13 // ============================================================================
15 // ----------------------------------------------------------------------------
17 // ----------------------------------------------------------------------------
19 // for compilers that support precompilation, includes "wx/wx.h".
20 #include "wx/wxprec.h"
26 // for all others, include the necessary headers
31 #include "wx/bmpbuttn.h"
32 #include "wx/button.h"
33 #include "wx/checkbox.h"
34 #include "wx/radiobox.h"
35 #include "wx/statbox.h"
36 #include "wx/textctrl.h"
39 #include "wx/artprov.h"
41 #include "wx/dcmemory.h"
42 #include "wx/commandlinkbutton.h"
46 #include "icons/button.xpm"
48 // ----------------------------------------------------------------------------
50 // ----------------------------------------------------------------------------
55 ButtonPage_Reset
= wxID_HIGHEST
,
56 ButtonPage_ChangeLabel
,
57 ButtonPage_ChangeNote
,
84 // ----------------------------------------------------------------------------
86 // ----------------------------------------------------------------------------
88 class ButtonWidgetsPage
: public WidgetsPage
91 ButtonWidgetsPage(WidgetsBookCtrl
*book
, wxImageList
*imaglist
);
92 virtual ~ButtonWidgetsPage(){};
94 virtual wxControl
*GetWidget() const { return m_button
; }
95 virtual void RecreateWidget() { CreateButton(); }
97 // lazy creation of the content
98 virtual void CreateContent();
102 void OnCheckOrRadioBox(wxCommandEvent
& event
);
104 void OnButton(wxCommandEvent
& event
);
105 void OnButtonReset(wxCommandEvent
& event
);
106 void OnButtonChangeLabel(wxCommandEvent
& event
);
107 void OnButtonChangeNote(wxCommandEvent
& event
);
109 // reset the wxButton parameters
112 // (re)create the wxButton
115 // add m_button to m_sizerButton using current value of m_chkFit
116 void AddButtonToSizer();
118 // helper function: create a bitmap for wxBitmapButton
119 wxBitmap
CreateBitmap(const wxString
& label
);
125 // the check/radio boxes for styles
126 wxCheckBox
*m_chkBitmapOnly
,
130 #if wxUSE_COMMANDLINKBUTTON
132 #endif // wxUSE_COMMANDLINKBUTTON
135 #endif // wxUSE_MARKUP
137 *m_chkUseBitmapClass
;
139 // more checkboxes for wxBitmapButton only
140 wxCheckBox
*m_chkUsePressed
,
145 // and an image position choice used if m_chkTextAndBitmap is on
146 wxRadioBox
*m_radioImagePos
;
148 wxRadioBox
*m_radioHAlign
,
151 // the button itself and the sizer it is in
154 #if wxUSE_COMMANDLINKBUTTON
155 // same as m_button or NULL if not showing a command link button currently
156 wxCommandLinkButton
*m_cmdLnkButton
;
157 #endif // wxUSE_COMMANDLINKBUTTON
159 wxSizer
*m_sizerButton
;
161 // the text entries for command parameters
162 wxTextCtrl
*m_textLabel
;
164 #if wxUSE_COMMANDLINKBUTTON
165 wxTextCtrl
*m_textNote
;
167 // used to hide or show button for changing note
168 wxSizer
*m_sizerNote
;
169 #endif // wxUSE_COMMANDLINKBUTTON
172 DECLARE_EVENT_TABLE()
173 DECLARE_WIDGETS_PAGE(ButtonWidgetsPage
)
176 // ----------------------------------------------------------------------------
178 // ----------------------------------------------------------------------------
180 BEGIN_EVENT_TABLE(ButtonWidgetsPage
, WidgetsPage
)
181 EVT_BUTTON(ButtonPage_Button
, ButtonWidgetsPage::OnButton
)
183 EVT_BUTTON(ButtonPage_Reset
, ButtonWidgetsPage::OnButtonReset
)
184 EVT_BUTTON(ButtonPage_ChangeLabel
, ButtonWidgetsPage::OnButtonChangeLabel
)
185 EVT_BUTTON(ButtonPage_ChangeNote
, ButtonWidgetsPage::OnButtonChangeNote
)
187 EVT_CHECKBOX(wxID_ANY
, ButtonWidgetsPage::OnCheckOrRadioBox
)
188 EVT_RADIOBOX(wxID_ANY
, ButtonWidgetsPage::OnCheckOrRadioBox
)
191 // ============================================================================
193 // ============================================================================
195 #if defined(__WXUNIVERSAL__)
196 #define FAMILY_CTRLS UNIVERSAL_CTRLS
198 #define FAMILY_CTRLS NATIVE_CTRLS
201 IMPLEMENT_WIDGETS_PAGE(ButtonWidgetsPage
, wxT("Button"), FAMILY_CTRLS
);
203 ButtonWidgetsPage::ButtonWidgetsPage(WidgetsBookCtrl
*book
,
204 wxImageList
*imaglist
)
205 : WidgetsPage(book
, imaglist
, button_xpm
)
212 #if wxUSE_COMMANDLINKBUTTON
214 #endif // wxUSE_COMMANDLINKBUTTON
217 #endif // wxUSE_MARKUP
219 m_chkUseBitmapClass
=
223 m_chkUseDisabled
= (wxCheckBox
*)NULL
;
227 m_radioVAlign
= (wxRadioBox
*)NULL
;
229 m_textLabel
= (wxTextCtrl
*)NULL
;
231 m_button
= (wxButton
*)NULL
;
232 m_sizerButton
= (wxSizer
*)NULL
;
235 void ButtonWidgetsPage::CreateContent()
237 wxSizer
*sizerTop
= new wxBoxSizer(wxHORIZONTAL
);
240 wxStaticBox
*box
= new wxStaticBox(this, wxID_ANY
, wxT("&Set style"));
242 wxSizer
*sizerLeft
= new wxStaticBoxSizer(box
, wxVERTICAL
);
244 m_chkBitmapOnly
= CreateCheckBoxAndAddToSizer(sizerLeft
, "&Bitmap only");
245 m_chkTextAndBitmap
= CreateCheckBoxAndAddToSizer(sizerLeft
, "Text &and bitmap");
246 m_chkFit
= CreateCheckBoxAndAddToSizer(sizerLeft
, wxT("&Fit exactly"));
247 m_chkAuthNeeded
= CreateCheckBoxAndAddToSizer(sizerLeft
, wxT("Require a&uth"));
248 #if wxUSE_COMMANDLINKBUTTON
249 m_chkCommandLink
= CreateCheckBoxAndAddToSizer(sizerLeft
, wxT("Use command &link button"));
252 m_chkUseMarkup
= CreateCheckBoxAndAddToSizer(sizerLeft
, "Interpret &markup");
253 #endif // wxUSE_MARKUP
254 m_chkDefault
= CreateCheckBoxAndAddToSizer(sizerLeft
, wxT("&Default"));
256 m_chkUseBitmapClass
= CreateCheckBoxAndAddToSizer(sizerLeft
,
257 "Use wxBitmapButton");
258 m_chkUseBitmapClass
->SetValue(true);
260 sizerLeft
->AddSpacer(5);
262 wxSizer
*sizerUseLabels
=
263 new wxStaticBoxSizer(wxVERTICAL
, this,
264 "&Use the following bitmaps in addition to the normal one?");
265 m_chkUsePressed
= CreateCheckBoxAndAddToSizer(sizerUseLabels
,
266 "&Pressed (small help icon)");
267 m_chkUseFocused
= CreateCheckBoxAndAddToSizer(sizerUseLabels
,
268 "&Focused (small error icon)");
269 m_chkUseCurrent
= CreateCheckBoxAndAddToSizer(sizerUseLabels
,
270 "&Current (small warning icon)");
271 m_chkUseDisabled
= CreateCheckBoxAndAddToSizer(sizerUseLabels
,
272 "&Disabled (broken image icon)");
273 sizerLeft
->Add(sizerUseLabels
, wxSizerFlags().Expand().Border());
275 sizerLeft
->AddSpacer(10);
277 static const wxString dirs
[] =
279 "left", "right", "top", "bottom",
281 m_radioImagePos
= new wxRadioBox(this, wxID_ANY
, "Image &position",
282 wxDefaultPosition
, wxDefaultSize
,
283 WXSIZEOF(dirs
), dirs
);
284 sizerLeft
->Add(m_radioImagePos
, 0, wxGROW
| wxALL
, 5);
285 sizerLeft
->AddSpacer(15);
287 // should be in sync with enums Button[HV]Align!
288 static const wxString halign
[] =
295 static const wxString valign
[] =
302 m_radioHAlign
= new wxRadioBox(this, wxID_ANY
, wxT("&Horz alignment"),
303 wxDefaultPosition
, wxDefaultSize
,
304 WXSIZEOF(halign
), halign
);
305 m_radioVAlign
= new wxRadioBox(this, wxID_ANY
, wxT("&Vert alignment"),
306 wxDefaultPosition
, wxDefaultSize
,
307 WXSIZEOF(valign
), valign
);
309 sizerLeft
->Add(m_radioHAlign
, 0, wxGROW
| wxALL
, 5);
310 sizerLeft
->Add(m_radioVAlign
, 0, wxGROW
| wxALL
, 5);
312 sizerLeft
->Add(5, 5, 0, wxGROW
| wxALL
, 5); // spacer
314 wxButton
*btn
= new wxButton(this, ButtonPage_Reset
, wxT("&Reset"));
315 sizerLeft
->Add(btn
, 0, wxALIGN_CENTRE_HORIZONTAL
| wxALL
, 15);
318 wxStaticBox
*box2
= new wxStaticBox(this, wxID_ANY
, wxT("&Operations"));
319 wxSizer
*sizerMiddle
= new wxStaticBoxSizer(box2
, wxVERTICAL
);
321 wxSizer
*sizerRow
= CreateSizerWithTextAndButton(ButtonPage_ChangeLabel
,
325 m_textLabel
->SetValue(wxT("&Press me!"));
326 sizerMiddle
->Add(sizerRow
, 0, wxALL
| wxGROW
, 5);
328 #if wxUSE_COMMANDLINKBUTTON
329 m_sizerNote
= CreateSizerWithTextAndButton(ButtonPage_ChangeNote
,
333 m_textNote
->SetValue(wxT("Writes down button clicks in the log."));
335 sizerMiddle
->Add(m_sizerNote
, 0, wxALL
| wxGROW
, 5);
339 m_sizerButton
= new wxBoxSizer(wxHORIZONTAL
);
340 m_sizerButton
->SetMinSize(150, 0);
342 // the 3 panes panes compose the window
343 sizerTop
->Add(sizerLeft
, 0, wxGROW
| (wxALL
& ~wxLEFT
), 10);
344 sizerTop
->Add(sizerMiddle
, 1, wxGROW
| wxALL
, 10);
345 sizerTop
->Add(m_sizerButton
, 1, wxGROW
| (wxALL
& ~wxRIGHT
), 10);
347 // do create the main control
354 // ----------------------------------------------------------------------------
356 // ----------------------------------------------------------------------------
358 void ButtonWidgetsPage::Reset()
360 m_chkBitmapOnly
->SetValue(false);
361 m_chkFit
->SetValue(true);
362 m_chkAuthNeeded
->SetValue(false);
363 m_chkTextAndBitmap
->SetValue(false);
364 m_chkDefault
->SetValue(false);
365 #if wxUSE_COMMANDLINKBUTTON
366 m_chkCommandLink
->SetValue(false);
369 m_chkUseMarkup
->SetValue(false);
370 #endif // wxUSE_MARKUP
371 m_chkUseBitmapClass
->SetValue(true);
373 m_chkUsePressed
->SetValue(true);
374 m_chkUseFocused
->SetValue(true);
375 m_chkUseCurrent
->SetValue(true);
376 m_chkUseDisabled
->SetValue(true);
378 m_radioImagePos
->SetSelection(ButtonImagePos_Left
);
379 m_radioHAlign
->SetSelection(ButtonHAlign_Centre
);
380 m_radioVAlign
->SetSelection(ButtonVAlign_Centre
);
383 void ButtonWidgetsPage::CreateButton()
388 #if wxUSE_COMMANDLINKBUTTON
389 if ( m_cmdLnkButton
)
390 label
= m_cmdLnkButton
->GetMainLabel();
393 label
= m_button
->GetLabel();
395 size_t count
= m_sizerButton
->GetChildren().GetCount();
396 for ( size_t n
= 0; n
< count
; n
++ )
398 m_sizerButton
->Remove( 0 );
406 // creating for the first time or recreating a button after bitmap
408 label
= m_textLabel
->GetValue();
411 int flags
= ms_defaultFlags
;
412 switch ( m_radioHAlign
->GetSelection() )
414 case ButtonHAlign_Left
:
419 wxFAIL_MSG(wxT("unexpected radiobox selection"));
422 case ButtonHAlign_Centre
:
425 case ButtonHAlign_Right
:
430 switch ( m_radioVAlign
->GetSelection() )
432 case ButtonVAlign_Top
:
437 wxFAIL_MSG(wxT("unexpected radiobox selection"));
440 case ButtonVAlign_Centre
:
441 // centre vertical alignment is the default (no style)
444 case ButtonVAlign_Bottom
:
445 flags
|= wxBU_BOTTOM
;
449 #if wxUSE_COMMANDLINKBUTTON
450 m_sizerNote
->Show(m_chkCommandLink
->GetValue());
453 bool showsBitmap
= false;
454 if ( m_chkBitmapOnly
->GetValue() )
459 if ( m_chkUseBitmapClass
->GetValue() )
461 bbtn
= new wxBitmapButton(this, ButtonPage_Button
,
462 CreateBitmap(wxT("normal")));
466 bbtn
= new wxButton(this, ButtonPage_Button
);
467 bbtn
->SetBitmapLabel(CreateBitmap(wxT("normal")));
469 if ( m_chkUsePressed
->GetValue() )
470 bbtn
->SetBitmapPressed(CreateBitmap(wxT("pushed")));
471 if ( m_chkUseFocused
->GetValue() )
472 bbtn
->SetBitmapFocus(CreateBitmap(wxT("focused")));
473 if ( m_chkUseCurrent
->GetValue() )
474 bbtn
->SetBitmapCurrent(CreateBitmap(wxT("hover")));
475 if ( m_chkUseDisabled
->GetValue() )
476 bbtn
->SetBitmapDisabled(CreateBitmap(wxT("disabled")));
478 #if wxUSE_COMMANDLINKBUTTON
479 m_cmdLnkButton
= NULL
;
482 else // normal button
484 #if wxUSE_COMMANDLINKBUTTON
485 m_cmdLnkButton
= NULL
;
487 if ( m_chkCommandLink
->GetValue() )
489 m_cmdLnkButton
= new wxCommandLinkButton(this, ButtonPage_Button
,
491 m_textNote
->GetValue(),
495 m_button
= m_cmdLnkButton
;
498 #endif // wxUSE_COMMANDLINKBUTTON
500 m_button
= new wxButton(this, ButtonPage_Button
, label
,
501 wxDefaultPosition
, wxDefaultSize
,
506 if ( !showsBitmap
&& m_chkTextAndBitmap
->GetValue() )
510 static const wxDirection positions
[] =
512 wxLEFT
, wxRIGHT
, wxTOP
, wxBOTTOM
515 m_button
->SetBitmap(wxArtProvider::GetIcon(wxART_INFORMATION
, wxART_BUTTON
),
516 positions
[m_radioImagePos
->GetSelection()]);
518 if ( m_chkUsePressed
->GetValue() )
519 m_button
->SetBitmapPressed(wxArtProvider::GetIcon(wxART_HELP
, wxART_BUTTON
));
520 if ( m_chkUseFocused
->GetValue() )
521 m_button
->SetBitmapFocus(wxArtProvider::GetIcon(wxART_ERROR
, wxART_BUTTON
));
522 if ( m_chkUseCurrent
->GetValue() )
523 m_button
->SetBitmapCurrent(wxArtProvider::GetIcon(wxART_WARNING
, wxART_BUTTON
));
524 if ( m_chkUseDisabled
->GetValue() )
525 m_button
->SetBitmapDisabled(wxArtProvider::GetIcon(wxART_MISSING_IMAGE
, wxART_BUTTON
));
528 m_chkUseBitmapClass
->Enable(showsBitmap
);
530 m_chkUsePressed
->Enable(showsBitmap
);
531 m_chkUseFocused
->Enable(showsBitmap
);
532 m_chkUseCurrent
->Enable(showsBitmap
);
533 m_chkUseDisabled
->Enable(showsBitmap
);
535 if ( m_chkAuthNeeded
->GetValue() )
536 m_button
->SetAuthNeeded();
538 if ( m_chkDefault
->GetValue() )
539 m_button
->SetDefault();
543 m_sizerButton
->Layout();
546 void ButtonWidgetsPage::AddButtonToSizer()
548 if ( m_chkFit
->GetValue() )
550 m_sizerButton
->AddStretchSpacer(1);
551 m_sizerButton
->Add(m_button
, wxSizerFlags(0).Centre().Border());
552 m_sizerButton
->AddStretchSpacer(1);
554 else // take up the entire space
556 m_sizerButton
->Add(m_button
, wxSizerFlags(1).Expand().Border());
560 // ----------------------------------------------------------------------------
562 // ----------------------------------------------------------------------------
564 void ButtonWidgetsPage::OnButtonReset(wxCommandEvent
& WXUNUSED(event
))
571 void ButtonWidgetsPage::OnCheckOrRadioBox(wxCommandEvent
& WXUNUSED(event
))
574 Layout(); // make sure the text field for changing note displays correctly.
577 void ButtonWidgetsPage::OnButtonChangeLabel(wxCommandEvent
& WXUNUSED(event
))
579 const wxString labelText
= m_textLabel
->GetValue();
581 #if wxUSE_COMMANDLINKBUTTON
582 if ( m_cmdLnkButton
)
583 m_cmdLnkButton
->SetMainLabel(labelText
);
585 #endif // wxUSE_COMMANDLINKBUTTON
588 if ( m_chkUseMarkup
->GetValue() )
589 m_button
->SetLabelMarkup(labelText
);
591 #endif // wxUSE_MARKUP
592 m_button
->SetLabel(labelText
);
595 m_sizerButton
->Layout();
598 void ButtonWidgetsPage::OnButtonChangeNote(wxCommandEvent
& WXUNUSED(event
))
600 #if wxUSE_COMMANDLINKBUTTON
601 m_cmdLnkButton
->SetNote(m_textNote
->GetValue());
603 m_sizerButton
->Layout();
604 #endif // wxUSE_COMMANDLINKBUTTON
607 void ButtonWidgetsPage::OnButton(wxCommandEvent
& WXUNUSED(event
))
609 wxLogMessage(wxT("Test button clicked."));
612 // ----------------------------------------------------------------------------
613 // bitmap button stuff
614 // ----------------------------------------------------------------------------
616 wxBitmap
ButtonWidgetsPage::CreateBitmap(const wxString
& label
)
618 wxBitmap
bmp(180, 70); // shouldn't hardcode but it's simpler like this
620 dc
.SelectObject(bmp
);
621 dc
.SetBackground(*wxCYAN_BRUSH
);
623 dc
.SetTextForeground(*wxBLACK
);
624 dc
.DrawLabel(wxStripMenuCodes(m_textLabel
->GetValue()) + wxT("\n")
625 wxT("(") + label
+ wxT(" state)"),
626 wxArtProvider::GetBitmap(wxART_INFORMATION
),
627 wxRect(10, 10, bmp
.GetWidth() - 20, bmp
.GetHeight() - 20),