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 // License: wxWindows license
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"
46 #include "icons/button.xpm"
48 // ----------------------------------------------------------------------------
50 // ----------------------------------------------------------------------------
55 ButtonPage_Reset
= 100,
56 ButtonPage_ChangeLabel
,
75 // ----------------------------------------------------------------------------
77 // ----------------------------------------------------------------------------
79 class ButtonWidgetsPage
: public WidgetsPage
82 ButtonWidgetsPage(wxBookCtrlBase
*book
, wxImageList
*imaglist
);
83 virtual ~ButtonWidgetsPage(){};
85 virtual wxControl
*GetWidget() const { return m_button
; }
86 virtual void RecreateWidget() { CreateButton(); }
90 void OnCheckOrRadioBox(wxCommandEvent
& event
);
92 void OnButton(wxCommandEvent
& event
);
93 void OnButtonReset(wxCommandEvent
& event
);
94 void OnButtonChangeLabel(wxCommandEvent
& event
);
96 // reset the wxButton parameters
99 // (re)create the wxButton
102 // add m_button to m_sizerButton using current value of m_chkFit
103 void AddButtonToSizer();
105 // helper function: create a bitmap for wxBitmapButton
106 wxBitmap
CreateBitmap(const wxString
& label
);
112 // the check/radio boxes for styles
113 wxCheckBox
*m_chkBitmap
,
118 // more checkboxes for wxBitmapButton only
119 wxCheckBox
*m_chkUseSelected
,
124 wxRadioBox
*m_radioHAlign
,
127 // the button itself and the sizer it is in
129 wxSizer
*m_sizerButton
;
131 // the text entries for command parameters
132 wxTextCtrl
*m_textLabel
;
135 DECLARE_EVENT_TABLE()
136 DECLARE_WIDGETS_PAGE(ButtonWidgetsPage
)
139 // ----------------------------------------------------------------------------
141 // ----------------------------------------------------------------------------
143 BEGIN_EVENT_TABLE(ButtonWidgetsPage
, WidgetsPage
)
144 EVT_BUTTON(ButtonPage_Button
, ButtonWidgetsPage::OnButton
)
146 EVT_BUTTON(ButtonPage_Reset
, ButtonWidgetsPage::OnButtonReset
)
147 EVT_BUTTON(ButtonPage_ChangeLabel
, ButtonWidgetsPage::OnButtonChangeLabel
)
149 EVT_CHECKBOX(wxID_ANY
, ButtonWidgetsPage::OnCheckOrRadioBox
)
150 EVT_RADIOBOX(wxID_ANY
, ButtonWidgetsPage::OnCheckOrRadioBox
)
153 // ============================================================================
155 // ============================================================================
157 IMPLEMENT_WIDGETS_PAGE(ButtonWidgetsPage
, _T("Button"));
159 ButtonWidgetsPage::ButtonWidgetsPage(wxBookCtrlBase
*book
,
160 wxImageList
*imaglist
)
163 imaglist
->Add(wxBitmap(button_xpm
));
173 m_chkUseDisabled
= (wxCheckBox
*)NULL
;
176 m_radioVAlign
= (wxRadioBox
*)NULL
;
178 m_textLabel
= (wxTextCtrl
*)NULL
;
180 m_button
= (wxButton
*)NULL
;
181 m_sizerButton
= (wxSizer
*)NULL
;
183 wxSizer
*sizerTop
= new wxBoxSizer(wxHORIZONTAL
);
186 wxStaticBox
*box
= new wxStaticBox(this, wxID_ANY
, _T("&Set style"));
188 wxSizer
*sizerLeft
= new wxStaticBoxSizer(box
, wxVERTICAL
);
190 m_chkBitmap
= CreateCheckBoxAndAddToSizer(sizerLeft
, _T("&Bitmap button"));
191 m_chkImage
= CreateCheckBoxAndAddToSizer(sizerLeft
, _T("With &image"));
192 m_chkFit
= CreateCheckBoxAndAddToSizer(sizerLeft
, _T("&Fit exactly"));
193 m_chkDefault
= CreateCheckBoxAndAddToSizer(sizerLeft
, _T("&Default"));
195 #ifndef __WXUNIVERSAL__
196 // only wxUniv currently supports buttons with images
197 m_chkImage
->Disable();
200 sizerLeft
->AddSpacer(5);
202 wxSizer
*sizerUseLabels
=
203 new wxStaticBoxSizer(wxVERTICAL
, this, _T("&Use the following labels?"));
204 m_chkUseSelected
= CreateCheckBoxAndAddToSizer(sizerUseLabels
, _T("&Pushed"));
205 m_chkUseFocused
= CreateCheckBoxAndAddToSizer(sizerUseLabels
, _T("&Focused"));
206 m_chkUseHover
= CreateCheckBoxAndAddToSizer(sizerUseLabels
, _T("&Hover"));
207 m_chkUseDisabled
= CreateCheckBoxAndAddToSizer(sizerUseLabels
, _T("&Disabled"));
208 sizerLeft
->Add(sizerUseLabels
, wxSizerFlags().Expand().Border());
210 sizerLeft
->AddSpacer(15);
212 // should be in sync with enums Button[HV]Align!
213 static const wxString halign
[] =
220 static const wxString valign
[] =
227 m_radioHAlign
= new wxRadioBox(this, wxID_ANY
, _T("&Horz alignment"),
228 wxDefaultPosition
, wxDefaultSize
,
229 WXSIZEOF(halign
), halign
);
230 m_radioVAlign
= new wxRadioBox(this, wxID_ANY
, _T("&Vert alignment"),
231 wxDefaultPosition
, wxDefaultSize
,
232 WXSIZEOF(valign
), valign
);
234 sizerLeft
->Add(m_radioHAlign
, 0, wxGROW
| wxALL
, 5);
235 sizerLeft
->Add(m_radioVAlign
, 0, wxGROW
| wxALL
, 5);
237 sizerLeft
->Add(5, 5, 0, wxGROW
| wxALL
, 5); // spacer
239 wxButton
*btn
= new wxButton(this, ButtonPage_Reset
, _T("&Reset"));
240 sizerLeft
->Add(btn
, 0, wxALIGN_CENTRE_HORIZONTAL
| wxALL
, 15);
243 wxStaticBox
*box2
= new wxStaticBox(this, wxID_ANY
, _T("&Operations"));
244 wxSizer
*sizerMiddle
= new wxStaticBoxSizer(box2
, wxVERTICAL
);
246 wxSizer
*sizerRow
= CreateSizerWithTextAndButton(ButtonPage_ChangeLabel
,
250 m_textLabel
->SetValue(_T("&Press me!"));
252 sizerMiddle
->Add(sizerRow
, 0, wxALL
| wxGROW
, 5);
255 m_sizerButton
= new wxBoxSizer(wxHORIZONTAL
);
256 m_sizerButton
->SetMinSize(150, 0);
258 // the 3 panes panes compose the window
259 sizerTop
->Add(sizerLeft
, 0, wxGROW
| (wxALL
& ~wxLEFT
), 10);
260 sizerTop
->Add(sizerMiddle
, 1, wxGROW
| wxALL
, 10);
261 sizerTop
->Add(m_sizerButton
, 1, wxGROW
| (wxALL
& ~wxRIGHT
), 10);
263 // do create the main control
272 // ----------------------------------------------------------------------------
274 // ----------------------------------------------------------------------------
276 void ButtonWidgetsPage::Reset()
278 m_chkBitmap
->SetValue(false);
279 m_chkFit
->SetValue(true);
280 m_chkImage
->SetValue(false);
281 m_chkDefault
->SetValue(false);
283 m_chkUseSelected
->SetValue(true);
284 m_chkUseFocused
->SetValue(true);
285 m_chkUseHover
->SetValue(true);
286 m_chkUseDisabled
->SetValue(true);
288 m_radioHAlign
->SetSelection(ButtonHAlign_Centre
);
289 m_radioVAlign
->SetSelection(ButtonVAlign_Centre
);
292 void ButtonWidgetsPage::CreateButton()
297 label
= m_button
->GetLabel();
299 size_t count
= m_sizerButton
->GetChildren().GetCount();
300 for ( size_t n
= 0; n
< count
; n
++ )
302 m_sizerButton
->Remove( 0 );
310 // creating for the first time or recreating a button after bitmap
312 label
= m_textLabel
->GetValue();
315 int flags
= ms_defaultFlags
;
316 switch ( m_radioHAlign
->GetSelection() )
318 case ButtonHAlign_Left
:
323 wxFAIL_MSG(_T("unexpected radiobox selection"));
326 case ButtonHAlign_Centre
:
329 case ButtonHAlign_Right
:
334 switch ( m_radioVAlign
->GetSelection() )
336 case ButtonVAlign_Top
:
341 wxFAIL_MSG(_T("unexpected radiobox selection"));
344 case ButtonVAlign_Centre
:
345 // centre vertical alignment is the default (no style)
348 case ButtonVAlign_Bottom
:
349 flags
|= wxBU_BOTTOM
;
353 const bool isBitmapButton
= m_chkBitmap
->GetValue();
354 if ( isBitmapButton
)
356 wxBitmapButton
*bbtn
= new wxBitmapButton(this, ButtonPage_Button
,
357 CreateBitmap(_T("normal")));
358 if ( m_chkUseSelected
->GetValue() )
359 bbtn
->SetBitmapSelected(CreateBitmap(_T("pushed")));
360 if ( m_chkUseFocused
->GetValue() )
361 bbtn
->SetBitmapFocus(CreateBitmap(_T("focused")));
362 if ( m_chkUseHover
->GetValue() )
363 bbtn
->SetBitmapHover(CreateBitmap(_T("hover")));
364 if ( m_chkUseDisabled
->GetValue() )
365 bbtn
->SetBitmapDisabled(CreateBitmap(_T("disabled")));
368 else // normal button
370 m_button
= new wxButton(this, ButtonPage_Button
, label
,
371 wxDefaultPosition
, wxDefaultSize
,
375 m_chkUseSelected
->Enable(isBitmapButton
);
376 m_chkUseFocused
->Enable(isBitmapButton
);
377 m_chkUseHover
->Enable(isBitmapButton
);
378 m_chkUseDisabled
->Enable(isBitmapButton
);
380 #ifdef __WXUNIVERSAL__
381 if ( m_chkImage
->GetValue() )
383 m_button
->SetImageLabel(wxArtProvider::GetIcon(wxART_INFORMATION
));
387 if ( m_chkDefault
->GetValue() )
389 m_button
->SetDefault();
394 m_sizerButton
->Layout();
397 void ButtonWidgetsPage::AddButtonToSizer()
399 if ( m_chkFit
->GetValue() )
401 m_sizerButton
->AddStretchSpacer(1);
402 m_sizerButton
->Add(m_button
, wxSizerFlags(0).Centre().Border());
403 m_sizerButton
->AddStretchSpacer(1);
405 else // take up the entire space
407 m_sizerButton
->Add(m_button
, wxSizerFlags(1).Expand().Border());
411 // ----------------------------------------------------------------------------
413 // ----------------------------------------------------------------------------
415 void ButtonWidgetsPage::OnButtonReset(wxCommandEvent
& WXUNUSED(event
))
422 void ButtonWidgetsPage::OnCheckOrRadioBox(wxCommandEvent
& WXUNUSED(event
))
427 void ButtonWidgetsPage::OnButtonChangeLabel(wxCommandEvent
& WXUNUSED(event
))
429 m_button
->SetLabel(m_textLabel
->GetValue());
432 void ButtonWidgetsPage::OnButton(wxCommandEvent
& WXUNUSED(event
))
434 wxLogMessage(_T("Test button clicked."));
437 // ----------------------------------------------------------------------------
438 // bitmap button stuff
439 // ----------------------------------------------------------------------------
441 wxBitmap
ButtonWidgetsPage::CreateBitmap(const wxString
& label
)
443 wxBitmap
bmp(180, 70); // shouldn't hardcode but it's simpler like this
445 dc
.SelectObject(bmp
);
446 dc
.SetBackground(wxBrush(*wxWHITE
));
448 dc
.SetTextForeground(*wxBLUE
);
449 dc
.DrawLabel(wxStripMenuCodes(m_textLabel
->GetValue()) + _T("\n")
450 _T("(") + label
+ _T(" state)"),
451 wxArtProvider::GetBitmap(wxART_INFORMATION
),
452 wxRect(10, 10, bmp
.GetWidth() - 20, bmp
.GetHeight() - 20),