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
,
134 // more checkboxes for wxBitmapButton only
135 wxCheckBox
*m_chkUsePressed
,
140 // and an image position choice used if m_chkTextAndBitmap is on
141 wxRadioBox
*m_radioImagePos
;
143 wxRadioBox
*m_radioHAlign
,
146 // the button itself and the sizer it is in
149 #if wxUSE_COMMANDLINKBUTTON
150 // same as m_button or NULL if not showing a command link button currently
151 wxCommandLinkButton
*m_cmdLnkButton
;
152 #endif // wxUSE_COMMANDLINKBUTTON
154 wxSizer
*m_sizerButton
;
156 // the text entries for command parameters
157 wxTextCtrl
*m_textLabel
;
159 #if wxUSE_COMMANDLINKBUTTON
160 wxTextCtrl
*m_textNote
;
162 // used to hide or show button for changing note
163 wxSizer
*m_sizerNote
;
164 #endif // wxUSE_COMMANDLINKBUTTON
167 DECLARE_EVENT_TABLE()
168 DECLARE_WIDGETS_PAGE(ButtonWidgetsPage
)
171 // ----------------------------------------------------------------------------
173 // ----------------------------------------------------------------------------
175 BEGIN_EVENT_TABLE(ButtonWidgetsPage
, WidgetsPage
)
176 EVT_BUTTON(ButtonPage_Button
, ButtonWidgetsPage::OnButton
)
178 EVT_BUTTON(ButtonPage_Reset
, ButtonWidgetsPage::OnButtonReset
)
179 EVT_BUTTON(ButtonPage_ChangeLabel
, ButtonWidgetsPage::OnButtonChangeLabel
)
180 EVT_BUTTON(ButtonPage_ChangeNote
, ButtonWidgetsPage::OnButtonChangeNote
)
182 EVT_CHECKBOX(wxID_ANY
, ButtonWidgetsPage::OnCheckOrRadioBox
)
183 EVT_RADIOBOX(wxID_ANY
, ButtonWidgetsPage::OnCheckOrRadioBox
)
186 // ============================================================================
188 // ============================================================================
190 #if defined(__WXUNIVERSAL__)
191 #define FAMILY_CTRLS UNIVERSAL_CTRLS
193 #define FAMILY_CTRLS NATIVE_CTRLS
196 IMPLEMENT_WIDGETS_PAGE(ButtonWidgetsPage
, wxT("Button"), FAMILY_CTRLS
);
198 ButtonWidgetsPage::ButtonWidgetsPage(WidgetsBookCtrl
*book
,
199 wxImageList
*imaglist
)
200 : WidgetsPage(book
, imaglist
, button_xpm
)
212 m_chkUseDisabled
= (wxCheckBox
*)NULL
;
216 m_radioVAlign
= (wxRadioBox
*)NULL
;
218 m_textLabel
= (wxTextCtrl
*)NULL
;
220 m_button
= (wxButton
*)NULL
;
221 m_sizerButton
= (wxSizer
*)NULL
;
224 void ButtonWidgetsPage::CreateContent()
226 wxSizer
*sizerTop
= new wxBoxSizer(wxHORIZONTAL
);
229 wxStaticBox
*box
= new wxStaticBox(this, wxID_ANY
, wxT("&Set style"));
231 wxSizer
*sizerLeft
= new wxStaticBoxSizer(box
, wxVERTICAL
);
233 m_chkBitmapOnly
= CreateCheckBoxAndAddToSizer(sizerLeft
, "&Bitmap only");
234 m_chkTextAndBitmap
= CreateCheckBoxAndAddToSizer(sizerLeft
, "Text &and bitmap");
235 m_chkFit
= CreateCheckBoxAndAddToSizer(sizerLeft
, wxT("&Fit exactly"));
236 m_chkAuthNeeded
= CreateCheckBoxAndAddToSizer(sizerLeft
, wxT("Require a&uth"));
237 #if wxUSE_COMMANDLINKBUTTON
238 m_chkCommandLink
= CreateCheckBoxAndAddToSizer(sizerLeft
, wxT("Use command &link button"));
240 m_chkDefault
= CreateCheckBoxAndAddToSizer(sizerLeft
, wxT("&Default"));
242 sizerLeft
->AddSpacer(5);
244 wxSizer
*sizerUseLabels
=
245 new wxStaticBoxSizer(wxVERTICAL
, this,
246 "&Use the following bitmaps in addition to the normal one?");
247 m_chkUsePressed
= CreateCheckBoxAndAddToSizer(sizerUseLabels
,
248 "&Pressed (small help icon)");
249 m_chkUseFocused
= CreateCheckBoxAndAddToSizer(sizerUseLabels
,
250 "&Focused (small error icon)");
251 m_chkUseCurrent
= CreateCheckBoxAndAddToSizer(sizerUseLabels
,
252 "&Current (small warning icon)");
253 m_chkUseDisabled
= CreateCheckBoxAndAddToSizer(sizerUseLabels
,
254 "&Disabled (broken image icon)");
255 sizerLeft
->Add(sizerUseLabels
, wxSizerFlags().Expand().Border());
257 sizerLeft
->AddSpacer(10);
259 static const wxString dirs
[] =
261 "left", "right", "top", "bottom",
263 m_radioImagePos
= new wxRadioBox(this, wxID_ANY
, "Image &position",
264 wxDefaultPosition
, wxDefaultSize
,
265 WXSIZEOF(dirs
), dirs
);
266 sizerLeft
->Add(m_radioImagePos
, 0, wxGROW
| wxALL
, 5);
267 sizerLeft
->AddSpacer(15);
269 // should be in sync with enums Button[HV]Align!
270 static const wxString halign
[] =
277 static const wxString valign
[] =
284 m_radioHAlign
= new wxRadioBox(this, wxID_ANY
, wxT("&Horz alignment"),
285 wxDefaultPosition
, wxDefaultSize
,
286 WXSIZEOF(halign
), halign
);
287 m_radioVAlign
= new wxRadioBox(this, wxID_ANY
, wxT("&Vert alignment"),
288 wxDefaultPosition
, wxDefaultSize
,
289 WXSIZEOF(valign
), valign
);
291 sizerLeft
->Add(m_radioHAlign
, 0, wxGROW
| wxALL
, 5);
292 sizerLeft
->Add(m_radioVAlign
, 0, wxGROW
| wxALL
, 5);
294 sizerLeft
->Add(5, 5, 0, wxGROW
| wxALL
, 5); // spacer
296 wxButton
*btn
= new wxButton(this, ButtonPage_Reset
, wxT("&Reset"));
297 sizerLeft
->Add(btn
, 0, wxALIGN_CENTRE_HORIZONTAL
| wxALL
, 15);
300 wxStaticBox
*box2
= new wxStaticBox(this, wxID_ANY
, wxT("&Operations"));
301 wxSizer
*sizerMiddle
= new wxStaticBoxSizer(box2
, wxVERTICAL
);
303 wxSizer
*sizerRow
= CreateSizerWithTextAndButton(ButtonPage_ChangeLabel
,
307 m_textLabel
->SetValue(wxT("&Press me!"));
308 sizerMiddle
->Add(sizerRow
, 0, wxALL
| wxGROW
, 5);
310 #if wxUSE_COMMANDLINKBUTTON
311 m_sizerNote
= CreateSizerWithTextAndButton(ButtonPage_ChangeNote
,
315 m_textNote
->SetValue(wxT("Writes down button clicks in the log."));
317 sizerMiddle
->Add(m_sizerNote
, 0, wxALL
| wxGROW
, 5);
321 m_sizerButton
= new wxBoxSizer(wxHORIZONTAL
);
322 m_sizerButton
->SetMinSize(150, 0);
324 // the 3 panes panes compose the window
325 sizerTop
->Add(sizerLeft
, 0, wxGROW
| (wxALL
& ~wxLEFT
), 10);
326 sizerTop
->Add(sizerMiddle
, 1, wxGROW
| wxALL
, 10);
327 sizerTop
->Add(m_sizerButton
, 1, wxGROW
| (wxALL
& ~wxRIGHT
), 10);
329 // do create the main control
336 // ----------------------------------------------------------------------------
338 // ----------------------------------------------------------------------------
340 void ButtonWidgetsPage::Reset()
342 m_chkBitmapOnly
->SetValue(false);
343 m_chkFit
->SetValue(true);
344 m_chkAuthNeeded
->SetValue(false);
345 m_chkTextAndBitmap
->SetValue(false);
346 m_chkDefault
->SetValue(false);
347 #if wxUSE_COMMANDLINKBUTTON
348 m_chkCommandLink
->SetValue(false);
351 m_chkUsePressed
->SetValue(true);
352 m_chkUseFocused
->SetValue(true);
353 m_chkUseCurrent
->SetValue(true);
354 m_chkUseDisabled
->SetValue(true);
356 m_radioImagePos
->SetSelection(ButtonImagePos_Left
);
357 m_radioHAlign
->SetSelection(ButtonHAlign_Centre
);
358 m_radioVAlign
->SetSelection(ButtonVAlign_Centre
);
361 void ButtonWidgetsPage::CreateButton()
366 #if wxUSE_COMMANDLINKBUTTON
367 if ( m_cmdLnkButton
)
368 label
= m_cmdLnkButton
->GetMainLabel();
371 label
= m_button
->GetLabel();
373 size_t count
= m_sizerButton
->GetChildren().GetCount();
374 for ( size_t n
= 0; n
< count
; n
++ )
376 m_sizerButton
->Remove( 0 );
384 // creating for the first time or recreating a button after bitmap
386 label
= m_textLabel
->GetValue();
389 int flags
= ms_defaultFlags
;
390 switch ( m_radioHAlign
->GetSelection() )
392 case ButtonHAlign_Left
:
397 wxFAIL_MSG(wxT("unexpected radiobox selection"));
400 case ButtonHAlign_Centre
:
403 case ButtonHAlign_Right
:
408 switch ( m_radioVAlign
->GetSelection() )
410 case ButtonVAlign_Top
:
415 wxFAIL_MSG(wxT("unexpected radiobox selection"));
418 case ButtonVAlign_Centre
:
419 // centre vertical alignment is the default (no style)
422 case ButtonVAlign_Bottom
:
423 flags
|= wxBU_BOTTOM
;
427 #if wxUSE_COMMANDLINKBUTTON
428 m_sizerNote
->Show(m_chkCommandLink
->GetValue());
431 bool showsBitmap
= false;
432 if ( m_chkBitmapOnly
->GetValue() )
436 wxBitmapButton
*bbtn
= new wxBitmapButton(this, ButtonPage_Button
,
437 CreateBitmap(wxT("normal")));
438 if ( m_chkUsePressed
->GetValue() )
439 bbtn
->SetBitmapPressed(CreateBitmap(wxT("pushed")));
440 if ( m_chkUseFocused
->GetValue() )
441 bbtn
->SetBitmapFocus(CreateBitmap(wxT("focused")));
442 if ( m_chkUseCurrent
->GetValue() )
443 bbtn
->SetBitmapCurrent(CreateBitmap(wxT("hover")));
444 if ( m_chkUseDisabled
->GetValue() )
445 bbtn
->SetBitmapDisabled(CreateBitmap(wxT("disabled")));
447 #if wxUSE_COMMANDLINKBUTTON
448 m_cmdLnkButton
= NULL
;
451 else // normal button
453 #if wxUSE_COMMANDLINKBUTTON
454 m_cmdLnkButton
= NULL
;
456 if ( m_chkCommandLink
->GetValue() )
458 m_cmdLnkButton
= new wxCommandLinkButton(this, ButtonPage_Button
,
460 m_textNote
->GetValue(),
464 m_button
= m_cmdLnkButton
;
467 #endif // wxUSE_COMMANDLINKBUTTON
469 m_button
= new wxButton(this, ButtonPage_Button
, label
,
470 wxDefaultPosition
, wxDefaultSize
,
475 if ( !showsBitmap
&& m_chkTextAndBitmap
->GetValue() )
479 static const wxDirection positions
[] =
481 wxLEFT
, wxRIGHT
, wxTOP
, wxBOTTOM
484 m_button
->SetBitmap(wxArtProvider::GetIcon(wxART_INFORMATION
),
485 positions
[m_radioImagePos
->GetSelection()]);
487 if ( m_chkUsePressed
->GetValue() )
488 m_button
->SetBitmapPressed(wxArtProvider::GetIcon(wxART_HELP
));
489 if ( m_chkUseFocused
->GetValue() )
490 m_button
->SetBitmapFocus(wxArtProvider::GetIcon(wxART_ERROR
));
491 if ( m_chkUseCurrent
->GetValue() )
492 m_button
->SetBitmapCurrent(wxArtProvider::GetIcon(wxART_WARNING
));
493 if ( m_chkUseDisabled
->GetValue() )
494 m_button
->SetBitmapDisabled(wxArtProvider::GetIcon(wxART_MISSING_IMAGE
));
497 m_chkUsePressed
->Enable(showsBitmap
);
498 m_chkUseFocused
->Enable(showsBitmap
);
499 m_chkUseCurrent
->Enable(showsBitmap
);
500 m_chkUseDisabled
->Enable(showsBitmap
);
502 if ( m_chkAuthNeeded
->GetValue() )
503 m_button
->SetAuthNeeded();
505 if ( m_chkDefault
->GetValue() )
506 m_button
->SetDefault();
510 m_sizerButton
->Layout();
513 void ButtonWidgetsPage::AddButtonToSizer()
515 if ( m_chkFit
->GetValue() )
517 m_sizerButton
->AddStretchSpacer(1);
518 m_sizerButton
->Add(m_button
, wxSizerFlags(0).Centre().Border());
519 m_sizerButton
->AddStretchSpacer(1);
521 else // take up the entire space
523 m_sizerButton
->Add(m_button
, wxSizerFlags(1).Expand().Border());
527 // ----------------------------------------------------------------------------
529 // ----------------------------------------------------------------------------
531 void ButtonWidgetsPage::OnButtonReset(wxCommandEvent
& WXUNUSED(event
))
538 void ButtonWidgetsPage::OnCheckOrRadioBox(wxCommandEvent
& WXUNUSED(event
))
541 Layout(); // make sure the text field for changing note displays correctly.
544 void ButtonWidgetsPage::OnButtonChangeLabel(wxCommandEvent
& WXUNUSED(event
))
546 #if wxUSE_COMMANDLINKBUTTON
547 if ( m_cmdLnkButton
)
548 m_cmdLnkButton
->SetMainLabel(m_textLabel
->GetValue());
550 #endif // wxUSE_COMMANDLINKBUTTON
551 m_button
->SetLabel(m_textLabel
->GetValue());
553 m_sizerButton
->Layout();
556 void ButtonWidgetsPage::OnButtonChangeNote(wxCommandEvent
& WXUNUSED(event
))
558 #if wxUSE_COMMANDLINKBUTTON
559 m_cmdLnkButton
->SetNote(m_textNote
->GetValue());
561 m_sizerButton
->Layout();
562 #endif // wxUSE_COMMANDLINKBUTTON
565 void ButtonWidgetsPage::OnButton(wxCommandEvent
& WXUNUSED(event
))
567 wxLogMessage(wxT("Test button clicked."));
570 // ----------------------------------------------------------------------------
571 // bitmap button stuff
572 // ----------------------------------------------------------------------------
574 wxBitmap
ButtonWidgetsPage::CreateBitmap(const wxString
& label
)
576 wxBitmap
bmp(180, 70); // shouldn't hardcode but it's simpler like this
578 dc
.SelectObject(bmp
);
579 dc
.SetBackground(wxBrush(*wxCYAN
));
581 dc
.SetTextForeground(*wxBLACK
);
582 dc
.DrawLabel(wxStripMenuCodes(m_textLabel
->GetValue()) + wxT("\n")
583 wxT("(") + label
+ wxT(" state)"),
584 wxArtProvider::GetBitmap(wxART_INFORMATION
),
585 wxRect(10, 10, bmp
.GetWidth() - 20, bmp
.GetHeight() - 20),