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
; }
89 void OnCheckOrRadioBox(wxCommandEvent
& event
);
91 void OnButton(wxCommandEvent
& event
);
92 void OnButtonReset(wxCommandEvent
& event
);
93 void OnButtonChangeLabel(wxCommandEvent
& event
);
95 // reset the wxButton parameters
98 // (re)create the wxButton
101 // add m_button to m_sizerButton using current value of m_chkFit
102 void AddButtonToSizer();
104 // helper function: create a bitmap for wxBitmapButton
105 wxBitmap
CreateBitmap(const wxString
& label
);
111 // the check/radio boxes for styles
112 wxCheckBox
*m_chkBitmap
,
117 wxRadioBox
*m_radioHAlign
,
120 // the button itself and the sizer it is in
122 wxSizer
*m_sizerButton
;
124 // the text entries for command parameters
125 wxTextCtrl
*m_textLabel
;
128 DECLARE_EVENT_TABLE()
129 DECLARE_WIDGETS_PAGE(ButtonWidgetsPage
)
132 // ----------------------------------------------------------------------------
134 // ----------------------------------------------------------------------------
136 BEGIN_EVENT_TABLE(ButtonWidgetsPage
, WidgetsPage
)
137 EVT_BUTTON(ButtonPage_Button
, ButtonWidgetsPage
::OnButton
)
139 EVT_BUTTON(ButtonPage_Reset
, ButtonWidgetsPage
::OnButtonReset
)
140 EVT_BUTTON(ButtonPage_ChangeLabel
, ButtonWidgetsPage
::OnButtonChangeLabel
)
142 EVT_CHECKBOX(wxID_ANY
, ButtonWidgetsPage
::OnCheckOrRadioBox
)
143 EVT_RADIOBOX(wxID_ANY
, ButtonWidgetsPage
::OnCheckOrRadioBox
)
146 // ============================================================================
148 // ============================================================================
150 IMPLEMENT_WIDGETS_PAGE(ButtonWidgetsPage
, _T("Button"));
152 ButtonWidgetsPage
::ButtonWidgetsPage(wxBookCtrlBase
*book
,
153 wxImageList
*imaglist
)
156 imaglist
->Add(wxBitmap(button_xpm
));
162 m_chkDefault
= (wxCheckBox
*)NULL
;
165 m_radioVAlign
= (wxRadioBox
*)NULL
;
167 m_textLabel
= (wxTextCtrl
*)NULL
;
169 m_button
= (wxButton
*)NULL
;
170 m_sizerButton
= (wxSizer
*)NULL
;
172 wxSizer
*sizerTop
= new wxBoxSizer(wxHORIZONTAL
);
175 wxStaticBox
*box
= new wxStaticBox(this, wxID_ANY
, _T("&Set style"));
177 wxSizer
*sizerLeft
= new wxStaticBoxSizer(box
, wxVERTICAL
);
179 m_chkBitmap
= CreateCheckBoxAndAddToSizer(sizerLeft
, _T("&Bitmap button"));
180 m_chkImage
= CreateCheckBoxAndAddToSizer(sizerLeft
, _T("With &image"));
181 m_chkFit
= CreateCheckBoxAndAddToSizer(sizerLeft
, _T("&Fit exactly"));
182 m_chkDefault
= CreateCheckBoxAndAddToSizer(sizerLeft
, _T("&Default"));
184 #ifndef __WXUNIVERSAL__
185 // only wxUniv currently supports buttons with images
186 m_chkImage
->Disable();
189 sizerLeft
->Add(5, 5, 0, wxGROW
| wxALL
, 5); // spacer
191 // should be in sync with enums Button[HV]Align!
192 static const wxString halign
[] =
199 static const wxString valign
[] =
206 m_radioHAlign
= new wxRadioBox(this, wxID_ANY
, _T("&Horz alignment"),
207 wxDefaultPosition
, wxDefaultSize
,
208 WXSIZEOF(halign
), halign
);
209 m_radioVAlign
= new wxRadioBox(this, wxID_ANY
, _T("&Vert alignment"),
210 wxDefaultPosition
, wxDefaultSize
,
211 WXSIZEOF(valign
), valign
);
213 sizerLeft
->Add(m_radioHAlign
, 0, wxGROW
| wxALL
, 5);
214 sizerLeft
->Add(m_radioVAlign
, 0, wxGROW
| wxALL
, 5);
216 sizerLeft
->Add(5, 5, 0, wxGROW
| wxALL
, 5); // spacer
218 wxButton
*btn
= new wxButton(this, ButtonPage_Reset
, _T("&Reset"));
219 sizerLeft
->Add(btn
, 0, wxALIGN_CENTRE_HORIZONTAL
| wxALL
, 15);
222 wxStaticBox
*box2
= new wxStaticBox(this, wxID_ANY
, _T("&Operations"));
223 wxSizer
*sizerMiddle
= new wxStaticBoxSizer(box2
, wxVERTICAL
);
225 wxSizer
*sizerRow
= CreateSizerWithTextAndButton(ButtonPage_ChangeLabel
,
229 m_textLabel
->SetValue(_T("&Press me!"));
231 sizerMiddle
->Add(sizerRow
, 0, wxALL
| wxGROW
, 5);
234 m_sizerButton
= new wxBoxSizer(wxHORIZONTAL
);
235 m_sizerButton
->SetMinSize(150, 0);
237 // the 3 panes panes compose the window
238 sizerTop
->Add(sizerLeft
, 0, wxGROW
| (wxALL
& ~wxLEFT
), 10);
239 sizerTop
->Add(sizerMiddle
, 1, wxGROW
| wxALL
, 10);
240 sizerTop
->Add(m_sizerButton
, 1, wxGROW
| (wxALL
& ~wxRIGHT
), 10);
242 // final initializations
245 m_button
= new wxButton(this, ButtonPage_Button
, _T("&Press me!"));
253 // ----------------------------------------------------------------------------
255 // ----------------------------------------------------------------------------
257 void ButtonWidgetsPage
::Reset()
259 m_chkBitmap
->SetValue(false);
260 m_chkFit
->SetValue(true);
261 m_chkImage
->SetValue(false);
262 m_chkDefault
->SetValue(false);
264 m_radioHAlign
->SetSelection(ButtonHAlign_Centre
);
265 m_radioVAlign
->SetSelection(ButtonVAlign_Centre
);
268 void ButtonWidgetsPage
::CreateButton()
273 label
= m_button
->GetLabel();
275 size_t count
= m_sizerButton
->GetChildren().GetCount();
276 for ( size_t n
= 0; n
< count
; n
++ )
278 m_sizerButton
->Remove( 0 );
286 // creating for the first time or recreating a button after bitmap
288 label
= m_textLabel
->GetValue();
292 switch ( m_radioHAlign
->GetSelection() )
294 case ButtonHAlign_Left
:
299 wxFAIL_MSG(_T("unexpected radiobox selection"));
302 case ButtonHAlign_Centre
:
305 case ButtonHAlign_Right
:
310 switch ( m_radioVAlign
->GetSelection() )
312 case ButtonVAlign_Top
:
317 wxFAIL_MSG(_T("unexpected radiobox selection"));
320 case ButtonVAlign_Centre
:
321 flags
|= wxALIGN_CENTRE_VERTICAL
;
324 case ButtonVAlign_Bottom
:
325 flags
|= wxBU_BOTTOM
;
329 if ( m_chkBitmap
->GetValue() )
331 wxBitmapButton
*bbtn
= new wxBitmapButton(this, ButtonPage_Button
,
332 CreateBitmap(_T("normal")));
333 bbtn
->SetBitmapSelected(CreateBitmap(_T("pushed")));
334 bbtn
->SetBitmapFocus(CreateBitmap(_T("focused")));
335 bbtn
->SetBitmapDisabled(CreateBitmap(_T("disabled")));
336 bbtn
->SetBitmapHover(CreateBitmap(_T("hover")));
339 else // normal button
341 m_button
= new wxButton(this, ButtonPage_Button
, label
,
342 wxDefaultPosition
, wxDefaultSize
,
346 #ifdef __WXUNIVERSAL__
347 if ( m_chkImage
->GetValue() )
349 m_button
->SetImageLabel(wxArtProvider
::GetIcon(wxART_INFORMATION
));
353 if ( m_chkDefault
->GetValue() )
355 m_button
->SetDefault();
360 m_sizerButton
->Layout();
363 void ButtonWidgetsPage
::AddButtonToSizer()
365 if ( m_chkFit
->GetValue() )
367 m_sizerButton
->AddStretchSpacer(1);
368 m_sizerButton
->Add(m_button
, wxSizerFlags(0).Centre().Border());
369 m_sizerButton
->AddStretchSpacer(1);
371 else // take up the entire space
373 m_sizerButton
->Add(m_button
, wxSizerFlags(1).Expand().Border());
377 // ----------------------------------------------------------------------------
379 // ----------------------------------------------------------------------------
381 void ButtonWidgetsPage
::OnButtonReset(wxCommandEvent
& WXUNUSED(event
))
388 void ButtonWidgetsPage
::OnCheckOrRadioBox(wxCommandEvent
& WXUNUSED(event
))
393 void ButtonWidgetsPage
::OnButtonChangeLabel(wxCommandEvent
& WXUNUSED(event
))
395 m_button
->SetLabel(m_textLabel
->GetValue());
398 void ButtonWidgetsPage
::OnButton(wxCommandEvent
& WXUNUSED(event
))
400 wxLogMessage(_T("Test button clicked."));
403 // ----------------------------------------------------------------------------
404 // bitmap button stuff
405 // ----------------------------------------------------------------------------
407 wxBitmap ButtonWidgetsPage
::CreateBitmap(const wxString
& label
)
409 wxBitmap
bmp(180, 70); // shouldn't hardcode but it's simpler like this
411 dc
.SelectObject(bmp
);
412 dc
.SetBackground(wxBrush(*wxWHITE
));
414 dc
.SetTextForeground(*wxBLUE
);
415 dc
.DrawLabel(wxStripMenuCodes(m_textLabel
->GetLabel()) + _T("\n")
416 _T("(") + label
+ _T(" state)"),
417 wxArtProvider
::GetBitmap(wxART_INFORMATION
),
418 wxRect(10, 10, bmp
.GetWidth() - 20, bmp
.GetHeight() - 20),