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 // more checkboxes for wxBitmapButton only
137 wxCheckBox
*m_chkUsePressed
,
142 // and an image position choice used if m_chkTextAndBitmap is on
143 wxRadioBox
*m_radioImagePos
;
145 wxRadioBox
*m_radioHAlign
,
148 // the button itself and the sizer it is in
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
156 wxSizer
*m_sizerButton
;
158 // the text entries for command parameters
159 wxTextCtrl
*m_textLabel
;
161 #if wxUSE_COMMANDLINKBUTTON
162 wxTextCtrl
*m_textNote
;
164 // used to hide or show button for changing note
165 wxSizer
*m_sizerNote
;
166 #endif // wxUSE_COMMANDLINKBUTTON
169 DECLARE_EVENT_TABLE()
170 DECLARE_WIDGETS_PAGE(ButtonWidgetsPage
)
173 // ----------------------------------------------------------------------------
175 // ----------------------------------------------------------------------------
177 BEGIN_EVENT_TABLE(ButtonWidgetsPage
, WidgetsPage
)
178 EVT_BUTTON(ButtonPage_Button
, ButtonWidgetsPage
::OnButton
)
180 EVT_BUTTON(ButtonPage_Reset
, ButtonWidgetsPage
::OnButtonReset
)
181 EVT_BUTTON(ButtonPage_ChangeLabel
, ButtonWidgetsPage
::OnButtonChangeLabel
)
182 EVT_BUTTON(ButtonPage_ChangeNote
, ButtonWidgetsPage
::OnButtonChangeNote
)
184 EVT_CHECKBOX(wxID_ANY
, ButtonWidgetsPage
::OnCheckOrRadioBox
)
185 EVT_RADIOBOX(wxID_ANY
, ButtonWidgetsPage
::OnCheckOrRadioBox
)
188 // ============================================================================
190 // ============================================================================
192 #if defined(__WXUNIVERSAL__)
193 #define FAMILY_CTRLS UNIVERSAL_CTRLS
195 #define FAMILY_CTRLS NATIVE_CTRLS
198 IMPLEMENT_WIDGETS_PAGE(ButtonWidgetsPage
, wxT("Button"), FAMILY_CTRLS
);
200 ButtonWidgetsPage
::ButtonWidgetsPage(WidgetsBookCtrl
*book
,
201 wxImageList
*imaglist
)
202 : WidgetsPage(book
, imaglist
, button_xpm
)
209 #if wxUSE_COMMANDLINKBUTTON
211 #endif // wxUSE_COMMANDLINKBUTTON
216 m_chkUseDisabled
= (wxCheckBox
*)NULL
;
220 m_radioVAlign
= (wxRadioBox
*)NULL
;
222 m_textLabel
= (wxTextCtrl
*)NULL
;
224 m_button
= (wxButton
*)NULL
;
225 m_sizerButton
= (wxSizer
*)NULL
;
228 void ButtonWidgetsPage
::CreateContent()
230 wxSizer
*sizerTop
= new wxBoxSizer(wxHORIZONTAL
);
233 wxStaticBox
*box
= new wxStaticBox(this, wxID_ANY
, wxT("&Set style"));
235 wxSizer
*sizerLeft
= new wxStaticBoxSizer(box
, wxVERTICAL
);
237 m_chkBitmapOnly
= CreateCheckBoxAndAddToSizer(sizerLeft
, "&Bitmap only");
238 m_chkTextAndBitmap
= CreateCheckBoxAndAddToSizer(sizerLeft
, "Text &and bitmap");
239 m_chkFit
= CreateCheckBoxAndAddToSizer(sizerLeft
, wxT("&Fit exactly"));
240 m_chkAuthNeeded
= CreateCheckBoxAndAddToSizer(sizerLeft
, wxT("Require a&uth"));
241 #if wxUSE_COMMANDLINKBUTTON
242 m_chkCommandLink
= CreateCheckBoxAndAddToSizer(sizerLeft
, wxT("Use command &link button"));
244 m_chkDefault
= CreateCheckBoxAndAddToSizer(sizerLeft
, wxT("&Default"));
246 sizerLeft
->AddSpacer(5);
248 wxSizer
*sizerUseLabels
=
249 new wxStaticBoxSizer(wxVERTICAL
, this,
250 "&Use the following bitmaps in addition to the normal one?");
251 m_chkUsePressed
= CreateCheckBoxAndAddToSizer(sizerUseLabels
,
252 "&Pressed (small help icon)");
253 m_chkUseFocused
= CreateCheckBoxAndAddToSizer(sizerUseLabels
,
254 "&Focused (small error icon)");
255 m_chkUseCurrent
= CreateCheckBoxAndAddToSizer(sizerUseLabels
,
256 "&Current (small warning icon)");
257 m_chkUseDisabled
= CreateCheckBoxAndAddToSizer(sizerUseLabels
,
258 "&Disabled (broken image icon)");
259 sizerLeft
->Add(sizerUseLabels
, wxSizerFlags().Expand().Border());
261 sizerLeft
->AddSpacer(10);
263 static const wxString dirs
[] =
265 "left", "right", "top", "bottom",
267 m_radioImagePos
= new wxRadioBox(this, wxID_ANY
, "Image &position",
268 wxDefaultPosition
, wxDefaultSize
,
269 WXSIZEOF(dirs
), dirs
);
270 sizerLeft
->Add(m_radioImagePos
, 0, wxGROW
| wxALL
, 5);
271 sizerLeft
->AddSpacer(15);
273 // should be in sync with enums Button[HV]Align!
274 static const wxString halign
[] =
281 static const wxString valign
[] =
288 m_radioHAlign
= new wxRadioBox(this, wxID_ANY
, wxT("&Horz alignment"),
289 wxDefaultPosition
, wxDefaultSize
,
290 WXSIZEOF(halign
), halign
);
291 m_radioVAlign
= new wxRadioBox(this, wxID_ANY
, wxT("&Vert alignment"),
292 wxDefaultPosition
, wxDefaultSize
,
293 WXSIZEOF(valign
), valign
);
295 sizerLeft
->Add(m_radioHAlign
, 0, wxGROW
| wxALL
, 5);
296 sizerLeft
->Add(m_radioVAlign
, 0, wxGROW
| wxALL
, 5);
298 sizerLeft
->Add(5, 5, 0, wxGROW
| wxALL
, 5); // spacer
300 wxButton
*btn
= new wxButton(this, ButtonPage_Reset
, wxT("&Reset"));
301 sizerLeft
->Add(btn
, 0, wxALIGN_CENTRE_HORIZONTAL
| wxALL
, 15);
304 wxStaticBox
*box2
= new wxStaticBox(this, wxID_ANY
, wxT("&Operations"));
305 wxSizer
*sizerMiddle
= new wxStaticBoxSizer(box2
, wxVERTICAL
);
307 wxSizer
*sizerRow
= CreateSizerWithTextAndButton(ButtonPage_ChangeLabel
,
311 m_textLabel
->SetValue(wxT("&Press me!"));
312 sizerMiddle
->Add(sizerRow
, 0, wxALL
| wxGROW
, 5);
314 #if wxUSE_COMMANDLINKBUTTON
315 m_sizerNote
= CreateSizerWithTextAndButton(ButtonPage_ChangeNote
,
319 m_textNote
->SetValue(wxT("Writes down button clicks in the log."));
321 sizerMiddle
->Add(m_sizerNote
, 0, wxALL
| wxGROW
, 5);
325 m_sizerButton
= new wxBoxSizer(wxHORIZONTAL
);
326 m_sizerButton
->SetMinSize(150, 0);
328 // the 3 panes panes compose the window
329 sizerTop
->Add(sizerLeft
, 0, wxGROW
| (wxALL
& ~wxLEFT
), 10);
330 sizerTop
->Add(sizerMiddle
, 1, wxGROW
| wxALL
, 10);
331 sizerTop
->Add(m_sizerButton
, 1, wxGROW
| (wxALL
& ~wxRIGHT
), 10);
333 // do create the main control
340 // ----------------------------------------------------------------------------
342 // ----------------------------------------------------------------------------
344 void ButtonWidgetsPage
::Reset()
346 m_chkBitmapOnly
->SetValue(false);
347 m_chkFit
->SetValue(true);
348 m_chkAuthNeeded
->SetValue(false);
349 m_chkTextAndBitmap
->SetValue(false);
350 m_chkDefault
->SetValue(false);
351 #if wxUSE_COMMANDLINKBUTTON
352 m_chkCommandLink
->SetValue(false);
355 m_chkUsePressed
->SetValue(true);
356 m_chkUseFocused
->SetValue(true);
357 m_chkUseCurrent
->SetValue(true);
358 m_chkUseDisabled
->SetValue(true);
360 m_radioImagePos
->SetSelection(ButtonImagePos_Left
);
361 m_radioHAlign
->SetSelection(ButtonHAlign_Centre
);
362 m_radioVAlign
->SetSelection(ButtonVAlign_Centre
);
365 void ButtonWidgetsPage
::CreateButton()
370 #if wxUSE_COMMANDLINKBUTTON
371 if ( m_cmdLnkButton
)
372 label
= m_cmdLnkButton
->GetMainLabel();
375 label
= m_button
->GetLabel();
377 size_t count
= m_sizerButton
->GetChildren().GetCount();
378 for ( size_t n
= 0; n
< count
; n
++ )
380 m_sizerButton
->Remove( 0 );
388 // creating for the first time or recreating a button after bitmap
390 label
= m_textLabel
->GetValue();
393 int flags
= ms_defaultFlags
;
394 switch ( m_radioHAlign
->GetSelection() )
396 case ButtonHAlign_Left
:
401 wxFAIL_MSG(wxT("unexpected radiobox selection"));
404 case ButtonHAlign_Centre
:
407 case ButtonHAlign_Right
:
412 switch ( m_radioVAlign
->GetSelection() )
414 case ButtonVAlign_Top
:
419 wxFAIL_MSG(wxT("unexpected radiobox selection"));
422 case ButtonVAlign_Centre
:
423 // centre vertical alignment is the default (no style)
426 case ButtonVAlign_Bottom
:
427 flags
|= wxBU_BOTTOM
;
431 #if wxUSE_COMMANDLINKBUTTON
432 m_sizerNote
->Show(m_chkCommandLink
->GetValue());
435 bool showsBitmap
= false;
436 if ( m_chkBitmapOnly
->GetValue() )
440 wxBitmapButton
*bbtn
= new wxBitmapButton(this, ButtonPage_Button
,
441 CreateBitmap(wxT("normal")));
442 if ( m_chkUsePressed
->GetValue() )
443 bbtn
->SetBitmapPressed(CreateBitmap(wxT("pushed")));
444 if ( m_chkUseFocused
->GetValue() )
445 bbtn
->SetBitmapFocus(CreateBitmap(wxT("focused")));
446 if ( m_chkUseCurrent
->GetValue() )
447 bbtn
->SetBitmapCurrent(CreateBitmap(wxT("hover")));
448 if ( m_chkUseDisabled
->GetValue() )
449 bbtn
->SetBitmapDisabled(CreateBitmap(wxT("disabled")));
451 #if wxUSE_COMMANDLINKBUTTON
452 m_cmdLnkButton
= NULL
;
455 else // normal button
457 #if wxUSE_COMMANDLINKBUTTON
458 m_cmdLnkButton
= NULL
;
460 if ( m_chkCommandLink
->GetValue() )
462 m_cmdLnkButton
= new wxCommandLinkButton(this, ButtonPage_Button
,
464 m_textNote
->GetValue(),
468 m_button
= m_cmdLnkButton
;
471 #endif // wxUSE_COMMANDLINKBUTTON
473 m_button
= new wxButton(this, ButtonPage_Button
, label
,
474 wxDefaultPosition
, wxDefaultSize
,
479 if ( !showsBitmap
&& m_chkTextAndBitmap
->GetValue() )
483 static const wxDirection positions
[] =
485 wxLEFT
, wxRIGHT
, wxTOP
, wxBOTTOM
488 m_button
->SetBitmap(wxArtProvider
::GetIcon(wxART_INFORMATION
),
489 positions
[m_radioImagePos
->GetSelection()]);
491 if ( m_chkUsePressed
->GetValue() )
492 m_button
->SetBitmapPressed(wxArtProvider
::GetIcon(wxART_HELP
));
493 if ( m_chkUseFocused
->GetValue() )
494 m_button
->SetBitmapFocus(wxArtProvider
::GetIcon(wxART_ERROR
));
495 if ( m_chkUseCurrent
->GetValue() )
496 m_button
->SetBitmapCurrent(wxArtProvider
::GetIcon(wxART_WARNING
));
497 if ( m_chkUseDisabled
->GetValue() )
498 m_button
->SetBitmapDisabled(wxArtProvider
::GetIcon(wxART_MISSING_IMAGE
));
501 m_chkUsePressed
->Enable(showsBitmap
);
502 m_chkUseFocused
->Enable(showsBitmap
);
503 m_chkUseCurrent
->Enable(showsBitmap
);
504 m_chkUseDisabled
->Enable(showsBitmap
);
506 if ( m_chkAuthNeeded
->GetValue() )
507 m_button
->SetAuthNeeded();
509 if ( m_chkDefault
->GetValue() )
510 m_button
->SetDefault();
514 m_sizerButton
->Layout();
517 void ButtonWidgetsPage
::AddButtonToSizer()
519 if ( m_chkFit
->GetValue() )
521 m_sizerButton
->AddStretchSpacer(1);
522 m_sizerButton
->Add(m_button
, wxSizerFlags(0).Centre().Border());
523 m_sizerButton
->AddStretchSpacer(1);
525 else // take up the entire space
527 m_sizerButton
->Add(m_button
, wxSizerFlags(1).Expand().Border());
531 // ----------------------------------------------------------------------------
533 // ----------------------------------------------------------------------------
535 void ButtonWidgetsPage
::OnButtonReset(wxCommandEvent
& WXUNUSED(event
))
542 void ButtonWidgetsPage
::OnCheckOrRadioBox(wxCommandEvent
& WXUNUSED(event
))
545 Layout(); // make sure the text field for changing note displays correctly.
548 void ButtonWidgetsPage
::OnButtonChangeLabel(wxCommandEvent
& WXUNUSED(event
))
550 #if wxUSE_COMMANDLINKBUTTON
551 if ( m_cmdLnkButton
)
552 m_cmdLnkButton
->SetMainLabel(m_textLabel
->GetValue());
554 #endif // wxUSE_COMMANDLINKBUTTON
555 m_button
->SetLabel(m_textLabel
->GetValue());
557 m_sizerButton
->Layout();
560 void ButtonWidgetsPage
::OnButtonChangeNote(wxCommandEvent
& WXUNUSED(event
))
562 #if wxUSE_COMMANDLINKBUTTON
563 m_cmdLnkButton
->SetNote(m_textNote
->GetValue());
565 m_sizerButton
->Layout();
566 #endif // wxUSE_COMMANDLINKBUTTON
569 void ButtonWidgetsPage
::OnButton(wxCommandEvent
& WXUNUSED(event
))
571 wxLogMessage(wxT("Test button clicked."));
574 // ----------------------------------------------------------------------------
575 // bitmap button stuff
576 // ----------------------------------------------------------------------------
578 wxBitmap ButtonWidgetsPage
::CreateBitmap(const wxString
& label
)
580 wxBitmap
bmp(180, 70); // shouldn't hardcode but it's simpler like this
582 dc
.SelectObject(bmp
);
583 dc
.SetBackground(wxBrush(*wxCYAN
));
585 dc
.SetTextForeground(*wxBLACK
);
586 dc
.DrawLabel(wxStripMenuCodes(m_textLabel
->GetValue()) + wxT("\n")
587 wxT("(") + label
+ wxT(" state)"),
588 wxArtProvider
::GetBitmap(wxART_INFORMATION
),
589 wxRect(10, 10, bmp
.GetWidth() - 20, bmp
.GetHeight() - 20),