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
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
222 m_chkUseDisabled
= (wxCheckBox
*)NULL
;
226 m_radioVAlign
= (wxRadioBox
*)NULL
;
228 m_textLabel
= (wxTextCtrl
*)NULL
;
230 m_button
= (wxButton
*)NULL
;
231 m_sizerButton
= (wxSizer
*)NULL
;
234 void ButtonWidgetsPage::CreateContent()
236 wxSizer
*sizerTop
= new wxBoxSizer(wxHORIZONTAL
);
239 wxStaticBox
*box
= new wxStaticBox(this, wxID_ANY
, wxT("&Set style"));
241 wxSizer
*sizerLeft
= new wxStaticBoxSizer(box
, wxVERTICAL
);
243 m_chkBitmapOnly
= CreateCheckBoxAndAddToSizer(sizerLeft
, "&Bitmap only");
244 m_chkTextAndBitmap
= CreateCheckBoxAndAddToSizer(sizerLeft
, "Text &and bitmap");
245 m_chkFit
= CreateCheckBoxAndAddToSizer(sizerLeft
, wxT("&Fit exactly"));
246 m_chkAuthNeeded
= CreateCheckBoxAndAddToSizer(sizerLeft
, wxT("Require a&uth"));
247 #if wxUSE_COMMANDLINKBUTTON
248 m_chkCommandLink
= CreateCheckBoxAndAddToSizer(sizerLeft
, wxT("Use command &link button"));
251 m_chkUseMarkup
= CreateCheckBoxAndAddToSizer(sizerLeft
, "Interpret &markup");
252 #endif // wxUSE_MARKUP
253 m_chkDefault
= CreateCheckBoxAndAddToSizer(sizerLeft
, wxT("&Default"));
255 sizerLeft
->AddSpacer(5);
257 wxSizer
*sizerUseLabels
=
258 new wxStaticBoxSizer(wxVERTICAL
, this,
259 "&Use the following bitmaps in addition to the normal one?");
260 m_chkUsePressed
= CreateCheckBoxAndAddToSizer(sizerUseLabels
,
261 "&Pressed (small help icon)");
262 m_chkUseFocused
= CreateCheckBoxAndAddToSizer(sizerUseLabels
,
263 "&Focused (small error icon)");
264 m_chkUseCurrent
= CreateCheckBoxAndAddToSizer(sizerUseLabels
,
265 "&Current (small warning icon)");
266 m_chkUseDisabled
= CreateCheckBoxAndAddToSizer(sizerUseLabels
,
267 "&Disabled (broken image icon)");
268 sizerLeft
->Add(sizerUseLabels
, wxSizerFlags().Expand().Border());
270 sizerLeft
->AddSpacer(10);
272 static const wxString dirs
[] =
274 "left", "right", "top", "bottom",
276 m_radioImagePos
= new wxRadioBox(this, wxID_ANY
, "Image &position",
277 wxDefaultPosition
, wxDefaultSize
,
278 WXSIZEOF(dirs
), dirs
);
279 sizerLeft
->Add(m_radioImagePos
, 0, wxGROW
| wxALL
, 5);
280 sizerLeft
->AddSpacer(15);
282 // should be in sync with enums Button[HV]Align!
283 static const wxString halign
[] =
290 static const wxString valign
[] =
297 m_radioHAlign
= new wxRadioBox(this, wxID_ANY
, wxT("&Horz alignment"),
298 wxDefaultPosition
, wxDefaultSize
,
299 WXSIZEOF(halign
), halign
);
300 m_radioVAlign
= new wxRadioBox(this, wxID_ANY
, wxT("&Vert alignment"),
301 wxDefaultPosition
, wxDefaultSize
,
302 WXSIZEOF(valign
), valign
);
304 sizerLeft
->Add(m_radioHAlign
, 0, wxGROW
| wxALL
, 5);
305 sizerLeft
->Add(m_radioVAlign
, 0, wxGROW
| wxALL
, 5);
307 sizerLeft
->Add(5, 5, 0, wxGROW
| wxALL
, 5); // spacer
309 wxButton
*btn
= new wxButton(this, ButtonPage_Reset
, wxT("&Reset"));
310 sizerLeft
->Add(btn
, 0, wxALIGN_CENTRE_HORIZONTAL
| wxALL
, 15);
313 wxStaticBox
*box2
= new wxStaticBox(this, wxID_ANY
, wxT("&Operations"));
314 wxSizer
*sizerMiddle
= new wxStaticBoxSizer(box2
, wxVERTICAL
);
316 wxSizer
*sizerRow
= CreateSizerWithTextAndButton(ButtonPage_ChangeLabel
,
320 m_textLabel
->SetValue(wxT("&Press me!"));
321 sizerMiddle
->Add(sizerRow
, 0, wxALL
| wxGROW
, 5);
323 #if wxUSE_COMMANDLINKBUTTON
324 m_sizerNote
= CreateSizerWithTextAndButton(ButtonPage_ChangeNote
,
328 m_textNote
->SetValue(wxT("Writes down button clicks in the log."));
330 sizerMiddle
->Add(m_sizerNote
, 0, wxALL
| wxGROW
, 5);
334 m_sizerButton
= new wxBoxSizer(wxHORIZONTAL
);
335 m_sizerButton
->SetMinSize(150, 0);
337 // the 3 panes panes compose the window
338 sizerTop
->Add(sizerLeft
, 0, wxGROW
| (wxALL
& ~wxLEFT
), 10);
339 sizerTop
->Add(sizerMiddle
, 1, wxGROW
| wxALL
, 10);
340 sizerTop
->Add(m_sizerButton
, 1, wxGROW
| (wxALL
& ~wxRIGHT
), 10);
342 // do create the main control
349 // ----------------------------------------------------------------------------
351 // ----------------------------------------------------------------------------
353 void ButtonWidgetsPage::Reset()
355 m_chkBitmapOnly
->SetValue(false);
356 m_chkFit
->SetValue(true);
357 m_chkAuthNeeded
->SetValue(false);
358 m_chkTextAndBitmap
->SetValue(false);
359 m_chkDefault
->SetValue(false);
360 #if wxUSE_COMMANDLINKBUTTON
361 m_chkCommandLink
->SetValue(false);
364 m_chkUseMarkup
->SetValue(false);
365 #endif // wxUSE_MARKUP
367 m_chkUsePressed
->SetValue(true);
368 m_chkUseFocused
->SetValue(true);
369 m_chkUseCurrent
->SetValue(true);
370 m_chkUseDisabled
->SetValue(true);
372 m_radioImagePos
->SetSelection(ButtonImagePos_Left
);
373 m_radioHAlign
->SetSelection(ButtonHAlign_Centre
);
374 m_radioVAlign
->SetSelection(ButtonVAlign_Centre
);
377 void ButtonWidgetsPage::CreateButton()
382 #if wxUSE_COMMANDLINKBUTTON
383 if ( m_cmdLnkButton
)
384 label
= m_cmdLnkButton
->GetMainLabel();
387 label
= m_button
->GetLabel();
389 size_t count
= m_sizerButton
->GetChildren().GetCount();
390 for ( size_t n
= 0; n
< count
; n
++ )
392 m_sizerButton
->Remove( 0 );
400 // creating for the first time or recreating a button after bitmap
402 label
= m_textLabel
->GetValue();
405 int flags
= ms_defaultFlags
;
406 switch ( m_radioHAlign
->GetSelection() )
408 case ButtonHAlign_Left
:
413 wxFAIL_MSG(wxT("unexpected radiobox selection"));
416 case ButtonHAlign_Centre
:
419 case ButtonHAlign_Right
:
424 switch ( m_radioVAlign
->GetSelection() )
426 case ButtonVAlign_Top
:
431 wxFAIL_MSG(wxT("unexpected radiobox selection"));
434 case ButtonVAlign_Centre
:
435 // centre vertical alignment is the default (no style)
438 case ButtonVAlign_Bottom
:
439 flags
|= wxBU_BOTTOM
;
443 #if wxUSE_COMMANDLINKBUTTON
444 m_sizerNote
->Show(m_chkCommandLink
->GetValue());
447 bool showsBitmap
= false;
448 if ( m_chkBitmapOnly
->GetValue() )
452 wxBitmapButton
*bbtn
= new wxBitmapButton(this, ButtonPage_Button
,
453 CreateBitmap(wxT("normal")));
454 if ( m_chkUsePressed
->GetValue() )
455 bbtn
->SetBitmapPressed(CreateBitmap(wxT("pushed")));
456 if ( m_chkUseFocused
->GetValue() )
457 bbtn
->SetBitmapFocus(CreateBitmap(wxT("focused")));
458 if ( m_chkUseCurrent
->GetValue() )
459 bbtn
->SetBitmapCurrent(CreateBitmap(wxT("hover")));
460 if ( m_chkUseDisabled
->GetValue() )
461 bbtn
->SetBitmapDisabled(CreateBitmap(wxT("disabled")));
463 #if wxUSE_COMMANDLINKBUTTON
464 m_cmdLnkButton
= NULL
;
467 else // normal button
469 #if wxUSE_COMMANDLINKBUTTON
470 m_cmdLnkButton
= NULL
;
472 if ( m_chkCommandLink
->GetValue() )
474 m_cmdLnkButton
= new wxCommandLinkButton(this, ButtonPage_Button
,
476 m_textNote
->GetValue(),
480 m_button
= m_cmdLnkButton
;
483 #endif // wxUSE_COMMANDLINKBUTTON
485 m_button
= new wxButton(this, ButtonPage_Button
, label
,
486 wxDefaultPosition
, wxDefaultSize
,
491 if ( !showsBitmap
&& m_chkTextAndBitmap
->GetValue() )
495 static const wxDirection positions
[] =
497 wxLEFT
, wxRIGHT
, wxTOP
, wxBOTTOM
500 m_button
->SetBitmap(wxArtProvider::GetIcon(wxART_INFORMATION
, wxART_BUTTON
),
501 positions
[m_radioImagePos
->GetSelection()]);
503 if ( m_chkUsePressed
->GetValue() )
504 m_button
->SetBitmapPressed(wxArtProvider::GetIcon(wxART_HELP
, wxART_BUTTON
));
505 if ( m_chkUseFocused
->GetValue() )
506 m_button
->SetBitmapFocus(wxArtProvider::GetIcon(wxART_ERROR
, wxART_BUTTON
));
507 if ( m_chkUseCurrent
->GetValue() )
508 m_button
->SetBitmapCurrent(wxArtProvider::GetIcon(wxART_WARNING
, wxART_BUTTON
));
509 if ( m_chkUseDisabled
->GetValue() )
510 m_button
->SetBitmapDisabled(wxArtProvider::GetIcon(wxART_MISSING_IMAGE
, wxART_BUTTON
));
513 m_chkUsePressed
->Enable(showsBitmap
);
514 m_chkUseFocused
->Enable(showsBitmap
);
515 m_chkUseCurrent
->Enable(showsBitmap
);
516 m_chkUseDisabled
->Enable(showsBitmap
);
518 if ( m_chkAuthNeeded
->GetValue() )
519 m_button
->SetAuthNeeded();
521 if ( m_chkDefault
->GetValue() )
522 m_button
->SetDefault();
526 m_sizerButton
->Layout();
529 void ButtonWidgetsPage::AddButtonToSizer()
531 if ( m_chkFit
->GetValue() )
533 m_sizerButton
->AddStretchSpacer(1);
534 m_sizerButton
->Add(m_button
, wxSizerFlags(0).Centre().Border());
535 m_sizerButton
->AddStretchSpacer(1);
537 else // take up the entire space
539 m_sizerButton
->Add(m_button
, wxSizerFlags(1).Expand().Border());
543 // ----------------------------------------------------------------------------
545 // ----------------------------------------------------------------------------
547 void ButtonWidgetsPage::OnButtonReset(wxCommandEvent
& WXUNUSED(event
))
554 void ButtonWidgetsPage::OnCheckOrRadioBox(wxCommandEvent
& WXUNUSED(event
))
557 Layout(); // make sure the text field for changing note displays correctly.
560 void ButtonWidgetsPage::OnButtonChangeLabel(wxCommandEvent
& WXUNUSED(event
))
562 const wxString labelText
= m_textLabel
->GetValue();
564 #if wxUSE_COMMANDLINKBUTTON
565 if ( m_cmdLnkButton
)
566 m_cmdLnkButton
->SetMainLabel(labelText
);
568 #endif // wxUSE_COMMANDLINKBUTTON
571 if ( m_chkUseMarkup
->GetValue() )
572 m_button
->SetLabelMarkup(labelText
);
574 #endif // wxUSE_MARKUP
575 m_button
->SetLabel(labelText
);
578 m_sizerButton
->Layout();
581 void ButtonWidgetsPage::OnButtonChangeNote(wxCommandEvent
& WXUNUSED(event
))
583 #if wxUSE_COMMANDLINKBUTTON
584 m_cmdLnkButton
->SetNote(m_textNote
->GetValue());
586 m_sizerButton
->Layout();
587 #endif // wxUSE_COMMANDLINKBUTTON
590 void ButtonWidgetsPage::OnButton(wxCommandEvent
& WXUNUSED(event
))
592 wxLogMessage(wxT("Test button clicked."));
595 // ----------------------------------------------------------------------------
596 // bitmap button stuff
597 // ----------------------------------------------------------------------------
599 wxBitmap
ButtonWidgetsPage::CreateBitmap(const wxString
& label
)
601 wxBitmap
bmp(180, 70); // shouldn't hardcode but it's simpler like this
603 dc
.SelectObject(bmp
);
604 dc
.SetBackground(wxBrush(*wxCYAN
));
606 dc
.SetTextForeground(*wxBLACK
);
607 dc
.DrawLabel(wxStripMenuCodes(m_textLabel
->GetValue()) + wxT("\n")
608 wxT("(") + label
+ wxT(" state)"),
609 wxArtProvider::GetBitmap(wxART_INFORMATION
),
610 wxRect(10, 10, bmp
.GetWidth() - 20, bmp
.GetHeight() - 20),