1 /////////////////////////////////////////////////////////////////////////////
2 // Program: wxWidgets Widgets Sample
4 // Purpose: Part of the widgets sample showing wxButton
5 // Author: Vadim Zeitlin
8 // Copyright: (c) 2001 Vadim Zeitlin
9 // Licence: wxWindows licence
10 /////////////////////////////////////////////////////////////////////////////
12 // ============================================================================
14 // ============================================================================
16 // ----------------------------------------------------------------------------
18 // ----------------------------------------------------------------------------
20 // for compilers that support precompilation, includes "wx/wx.h".
21 #include "wx/wxprec.h"
27 // for all others, include the necessary headers
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"
40 #include "wx/artprov.h"
42 #include "wx/dcmemory.h"
43 #include "wx/commandlinkbutton.h"
47 #include "icons/button.xpm"
49 // ----------------------------------------------------------------------------
51 // ----------------------------------------------------------------------------
56 ButtonPage_Reset
= wxID_HIGHEST
,
57 ButtonPage_ChangeLabel
,
58 ButtonPage_ChangeNote
,
85 // ----------------------------------------------------------------------------
87 // ----------------------------------------------------------------------------
89 class ButtonWidgetsPage
: public WidgetsPage
92 ButtonWidgetsPage(WidgetsBookCtrl
*book
, wxImageList
*imaglist
);
93 virtual ~ButtonWidgetsPage(){};
95 virtual wxControl
*GetWidget() const { return m_button
; }
96 virtual void RecreateWidget() { CreateButton(); }
98 // lazy creation of the content
99 virtual void CreateContent();
103 void OnCheckOrRadioBox(wxCommandEvent
& event
);
105 void OnButton(wxCommandEvent
& event
);
106 void OnButtonReset(wxCommandEvent
& event
);
107 void OnButtonChangeLabel(wxCommandEvent
& event
);
108 void OnButtonChangeNote(wxCommandEvent
& event
);
110 // reset the wxButton parameters
113 // (re)create the wxButton
116 // add m_button to m_sizerButton using current value of m_chkFit
117 void AddButtonToSizer();
119 // helper function: create a bitmap for wxBitmapButton
120 wxBitmap
CreateBitmap(const wxString
& label
);
126 // the check/radio boxes for styles
127 wxCheckBox
*m_chkBitmapOnly
,
131 #if wxUSE_COMMANDLINKBUTTON
133 #endif // wxUSE_COMMANDLINKBUTTON
136 #endif // wxUSE_MARKUP
138 *m_chkUseBitmapClass
;
140 // more checkboxes for wxBitmapButton only
141 wxCheckBox
*m_chkUsePressed
,
146 // and an image position choice used if m_chkTextAndBitmap is on
147 wxRadioBox
*m_radioImagePos
;
149 wxRadioBox
*m_radioHAlign
,
152 // the button itself and the sizer it is in
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
160 wxSizer
*m_sizerButton
;
162 // the text entries for command parameters
163 wxTextCtrl
*m_textLabel
;
165 #if wxUSE_COMMANDLINKBUTTON
166 wxTextCtrl
*m_textNote
;
168 // used to hide or show button for changing note
169 wxSizer
*m_sizerNote
;
170 #endif // wxUSE_COMMANDLINKBUTTON
173 DECLARE_EVENT_TABLE()
174 DECLARE_WIDGETS_PAGE(ButtonWidgetsPage
)
177 // ----------------------------------------------------------------------------
179 // ----------------------------------------------------------------------------
181 BEGIN_EVENT_TABLE(ButtonWidgetsPage
, WidgetsPage
)
182 EVT_BUTTON(ButtonPage_Button
, ButtonWidgetsPage::OnButton
)
184 EVT_BUTTON(ButtonPage_Reset
, ButtonWidgetsPage::OnButtonReset
)
185 EVT_BUTTON(ButtonPage_ChangeLabel
, ButtonWidgetsPage::OnButtonChangeLabel
)
186 EVT_BUTTON(ButtonPage_ChangeNote
, ButtonWidgetsPage::OnButtonChangeNote
)
188 EVT_CHECKBOX(wxID_ANY
, ButtonWidgetsPage::OnCheckOrRadioBox
)
189 EVT_RADIOBOX(wxID_ANY
, ButtonWidgetsPage::OnCheckOrRadioBox
)
192 // ============================================================================
194 // ============================================================================
196 #if defined(__WXUNIVERSAL__)
197 #define FAMILY_CTRLS UNIVERSAL_CTRLS
199 #define FAMILY_CTRLS NATIVE_CTRLS
202 IMPLEMENT_WIDGETS_PAGE(ButtonWidgetsPage
, wxT("Button"), FAMILY_CTRLS
);
204 ButtonWidgetsPage::ButtonWidgetsPage(WidgetsBookCtrl
*book
,
205 wxImageList
*imaglist
)
206 : WidgetsPage(book
, imaglist
, button_xpm
)
213 #if wxUSE_COMMANDLINKBUTTON
215 #endif // wxUSE_COMMANDLINKBUTTON
218 #endif // wxUSE_MARKUP
220 m_chkUseBitmapClass
=
224 m_chkUseDisabled
= (wxCheckBox
*)NULL
;
228 m_radioVAlign
= (wxRadioBox
*)NULL
;
230 m_textLabel
= (wxTextCtrl
*)NULL
;
232 m_button
= (wxButton
*)NULL
;
233 m_sizerButton
= (wxSizer
*)NULL
;
236 void ButtonWidgetsPage::CreateContent()
238 wxSizer
*sizerTop
= new wxBoxSizer(wxHORIZONTAL
);
241 wxStaticBox
*box
= new wxStaticBox(this, wxID_ANY
, wxT("&Set style"));
243 wxSizer
*sizerLeft
= new wxStaticBoxSizer(box
, wxVERTICAL
);
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"));
253 m_chkUseMarkup
= CreateCheckBoxAndAddToSizer(sizerLeft
, "Interpret &markup");
254 #endif // wxUSE_MARKUP
255 m_chkDefault
= CreateCheckBoxAndAddToSizer(sizerLeft
, wxT("&Default"));
257 m_chkUseBitmapClass
= CreateCheckBoxAndAddToSizer(sizerLeft
,
258 "Use wxBitmapButton");
259 m_chkUseBitmapClass
->SetValue(true);
261 sizerLeft
->AddSpacer(5);
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());
276 sizerLeft
->AddSpacer(10);
278 static const wxString dirs
[] =
280 "left", "right", "top", "bottom",
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);
288 // should be in sync with enums Button[HV]Align!
289 static const wxString halign
[] =
296 static const wxString valign
[] =
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
);
310 sizerLeft
->Add(m_radioHAlign
, 0, wxGROW
| wxALL
, 5);
311 sizerLeft
->Add(m_radioVAlign
, 0, wxGROW
| wxALL
, 5);
313 sizerLeft
->Add(5, 5, 0, wxGROW
| wxALL
, 5); // spacer
315 wxButton
*btn
= new wxButton(this, ButtonPage_Reset
, wxT("&Reset"));
316 sizerLeft
->Add(btn
, 0, wxALIGN_CENTRE_HORIZONTAL
| wxALL
, 15);
319 wxStaticBox
*box2
= new wxStaticBox(this, wxID_ANY
, wxT("&Operations"));
320 wxSizer
*sizerMiddle
= new wxStaticBoxSizer(box2
, wxVERTICAL
);
322 wxSizer
*sizerRow
= CreateSizerWithTextAndButton(ButtonPage_ChangeLabel
,
326 m_textLabel
->SetValue(wxT("&Press me!"));
327 sizerMiddle
->Add(sizerRow
, 0, wxALL
| wxGROW
, 5);
329 #if wxUSE_COMMANDLINKBUTTON
330 m_sizerNote
= CreateSizerWithTextAndButton(ButtonPage_ChangeNote
,
334 m_textNote
->SetValue(wxT("Writes down button clicks in the log."));
336 sizerMiddle
->Add(m_sizerNote
, 0, wxALL
| wxGROW
, 5);
340 m_sizerButton
= new wxBoxSizer(wxHORIZONTAL
);
341 m_sizerButton
->SetMinSize(150, 0);
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);
348 // do create the main control
355 // ----------------------------------------------------------------------------
357 // ----------------------------------------------------------------------------
359 void ButtonWidgetsPage::Reset()
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);
370 m_chkUseMarkup
->SetValue(false);
371 #endif // wxUSE_MARKUP
372 m_chkUseBitmapClass
->SetValue(true);
374 m_chkUsePressed
->SetValue(true);
375 m_chkUseFocused
->SetValue(true);
376 m_chkUseCurrent
->SetValue(true);
377 m_chkUseDisabled
->SetValue(true);
379 m_radioImagePos
->SetSelection(ButtonImagePos_Left
);
380 m_radioHAlign
->SetSelection(ButtonHAlign_Centre
);
381 m_radioVAlign
->SetSelection(ButtonVAlign_Centre
);
384 void ButtonWidgetsPage::CreateButton()
389 #if wxUSE_COMMANDLINKBUTTON
390 if ( m_cmdLnkButton
)
391 label
= m_cmdLnkButton
->GetMainLabel();
394 label
= m_button
->GetLabel();
396 size_t count
= m_sizerButton
->GetChildren().GetCount();
397 for ( size_t n
= 0; n
< count
; n
++ )
399 m_sizerButton
->Remove( 0 );
407 // creating for the first time or recreating a button after bitmap
409 label
= m_textLabel
->GetValue();
412 int flags
= ms_defaultFlags
;
413 switch ( m_radioHAlign
->GetSelection() )
415 case ButtonHAlign_Left
:
420 wxFAIL_MSG(wxT("unexpected radiobox selection"));
423 case ButtonHAlign_Centre
:
426 case ButtonHAlign_Right
:
431 switch ( m_radioVAlign
->GetSelection() )
433 case ButtonVAlign_Top
:
438 wxFAIL_MSG(wxT("unexpected radiobox selection"));
441 case ButtonVAlign_Centre
:
442 // centre vertical alignment is the default (no style)
445 case ButtonVAlign_Bottom
:
446 flags
|= wxBU_BOTTOM
;
450 #if wxUSE_COMMANDLINKBUTTON
451 m_sizerNote
->Show(m_chkCommandLink
->GetValue());
454 bool showsBitmap
= false;
455 if ( m_chkBitmapOnly
->GetValue() )
460 if ( m_chkUseBitmapClass
->GetValue() )
462 bbtn
= new wxBitmapButton(this, ButtonPage_Button
,
463 CreateBitmap(wxT("normal")));
467 bbtn
= new wxButton(this, ButtonPage_Button
);
468 bbtn
->SetBitmapLabel(CreateBitmap(wxT("normal")));
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")));
479 #if wxUSE_COMMANDLINKBUTTON
480 m_cmdLnkButton
= NULL
;
483 else // normal button
485 #if wxUSE_COMMANDLINKBUTTON
486 m_cmdLnkButton
= NULL
;
488 if ( m_chkCommandLink
->GetValue() )
490 m_cmdLnkButton
= new wxCommandLinkButton(this, ButtonPage_Button
,
492 m_textNote
->GetValue(),
496 m_button
= m_cmdLnkButton
;
499 #endif // wxUSE_COMMANDLINKBUTTON
501 m_button
= new wxButton(this, ButtonPage_Button
, label
,
502 wxDefaultPosition
, wxDefaultSize
,
507 if ( !showsBitmap
&& m_chkTextAndBitmap
->GetValue() )
511 static const wxDirection positions
[] =
513 wxLEFT
, wxRIGHT
, wxTOP
, wxBOTTOM
516 m_button
->SetBitmap(wxArtProvider::GetIcon(wxART_INFORMATION
, wxART_BUTTON
),
517 positions
[m_radioImagePos
->GetSelection()]);
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
));
529 m_chkUseBitmapClass
->Enable(showsBitmap
);
531 m_chkUsePressed
->Enable(showsBitmap
);
532 m_chkUseFocused
->Enable(showsBitmap
);
533 m_chkUseCurrent
->Enable(showsBitmap
);
534 m_chkUseDisabled
->Enable(showsBitmap
);
536 if ( m_chkAuthNeeded
->GetValue() )
537 m_button
->SetAuthNeeded();
539 if ( m_chkDefault
->GetValue() )
540 m_button
->SetDefault();
544 m_sizerButton
->Layout();
547 void ButtonWidgetsPage::AddButtonToSizer()
549 if ( m_chkFit
->GetValue() )
551 m_sizerButton
->AddStretchSpacer(1);
552 m_sizerButton
->Add(m_button
, wxSizerFlags(0).Centre().Border());
553 m_sizerButton
->AddStretchSpacer(1);
555 else // take up the entire space
557 m_sizerButton
->Add(m_button
, wxSizerFlags(1).Expand().Border());
561 // ----------------------------------------------------------------------------
563 // ----------------------------------------------------------------------------
565 void ButtonWidgetsPage::OnButtonReset(wxCommandEvent
& WXUNUSED(event
))
572 void ButtonWidgetsPage::OnCheckOrRadioBox(wxCommandEvent
& WXUNUSED(event
))
575 Layout(); // make sure the text field for changing note displays correctly.
578 void ButtonWidgetsPage::OnButtonChangeLabel(wxCommandEvent
& WXUNUSED(event
))
580 const wxString labelText
= m_textLabel
->GetValue();
582 #if wxUSE_COMMANDLINKBUTTON
583 if ( m_cmdLnkButton
)
584 m_cmdLnkButton
->SetMainLabel(labelText
);
586 #endif // wxUSE_COMMANDLINKBUTTON
589 if ( m_chkUseMarkup
->GetValue() )
590 m_button
->SetLabelMarkup(labelText
);
592 #endif // wxUSE_MARKUP
593 m_button
->SetLabel(labelText
);
596 m_sizerButton
->Layout();
599 void ButtonWidgetsPage::OnButtonChangeNote(wxCommandEvent
& WXUNUSED(event
))
601 #if wxUSE_COMMANDLINKBUTTON
602 m_cmdLnkButton
->SetNote(m_textNote
->GetValue());
604 m_sizerButton
->Layout();
605 #endif // wxUSE_COMMANDLINKBUTTON
608 void ButtonWidgetsPage::OnButton(wxCommandEvent
& WXUNUSED(event
))
610 wxLogMessage(wxT("Test button clicked."));
613 // ----------------------------------------------------------------------------
614 // bitmap button stuff
615 // ----------------------------------------------------------------------------
617 wxBitmap
ButtonWidgetsPage::CreateBitmap(const wxString
& label
)
619 wxBitmap
bmp(180, 70); // shouldn't hardcode but it's simpler like this
621 dc
.SelectObject(bmp
);
622 dc
.SetBackground(*wxCYAN_BRUSH
);
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),