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 // more checkboxes for wxBitmapButton only
118 wxCheckBox
*m_chkUseSelected
,
123 wxRadioBox
*m_radioHAlign
,
126 // the button itself and the sizer it is in
128 wxSizer
*m_sizerButton
;
130 // the text entries for command parameters
131 wxTextCtrl
*m_textLabel
;
134 DECLARE_EVENT_TABLE()
135 DECLARE_WIDGETS_PAGE(ButtonWidgetsPage
)
138 // ----------------------------------------------------------------------------
140 // ----------------------------------------------------------------------------
142 BEGIN_EVENT_TABLE(ButtonWidgetsPage
, WidgetsPage
)
143 EVT_BUTTON(ButtonPage_Button
, ButtonWidgetsPage::OnButton
)
145 EVT_BUTTON(ButtonPage_Reset
, ButtonWidgetsPage::OnButtonReset
)
146 EVT_BUTTON(ButtonPage_ChangeLabel
, ButtonWidgetsPage::OnButtonChangeLabel
)
148 EVT_CHECKBOX(wxID_ANY
, ButtonWidgetsPage::OnCheckOrRadioBox
)
149 EVT_RADIOBOX(wxID_ANY
, ButtonWidgetsPage::OnCheckOrRadioBox
)
152 // ============================================================================
154 // ============================================================================
156 IMPLEMENT_WIDGETS_PAGE(ButtonWidgetsPage
, _T("Button"));
158 ButtonWidgetsPage::ButtonWidgetsPage(wxBookCtrlBase
*book
,
159 wxImageList
*imaglist
)
162 imaglist
->Add(wxBitmap(button_xpm
));
172 m_chkUseDisabled
= (wxCheckBox
*)NULL
;
175 m_radioVAlign
= (wxRadioBox
*)NULL
;
177 m_textLabel
= (wxTextCtrl
*)NULL
;
179 m_button
= (wxButton
*)NULL
;
180 m_sizerButton
= (wxSizer
*)NULL
;
182 wxSizer
*sizerTop
= new wxBoxSizer(wxHORIZONTAL
);
185 wxStaticBox
*box
= new wxStaticBox(this, wxID_ANY
, _T("&Set style"));
187 wxSizer
*sizerLeft
= new wxStaticBoxSizer(box
, wxVERTICAL
);
189 m_chkBitmap
= CreateCheckBoxAndAddToSizer(sizerLeft
, _T("&Bitmap button"));
190 m_chkImage
= CreateCheckBoxAndAddToSizer(sizerLeft
, _T("With &image"));
191 m_chkFit
= CreateCheckBoxAndAddToSizer(sizerLeft
, _T("&Fit exactly"));
192 m_chkDefault
= CreateCheckBoxAndAddToSizer(sizerLeft
, _T("&Default"));
194 #ifndef __WXUNIVERSAL__
195 // only wxUniv currently supports buttons with images
196 m_chkImage
->Disable();
199 sizerLeft
->AddSpacer(5);
201 wxSizer
*sizerUseLabels
=
202 new wxStaticBoxSizer(wxVERTICAL
, this, _T("&Use the following labels?"));
203 m_chkUseSelected
= CreateCheckBoxAndAddToSizer(sizerUseLabels
, _T("&Pushed"));
204 m_chkUseFocused
= CreateCheckBoxAndAddToSizer(sizerUseLabels
, _T("&Focused"));
205 m_chkUseHover
= CreateCheckBoxAndAddToSizer(sizerUseLabels
, _T("&Hover"));
206 m_chkUseDisabled
= CreateCheckBoxAndAddToSizer(sizerUseLabels
, _T("&Disabled"));
207 sizerLeft
->Add(sizerUseLabels
, wxSizerFlags().Expand().Border());
209 sizerLeft
->AddSpacer(15);
211 // should be in sync with enums Button[HV]Align!
212 static const wxString halign
[] =
219 static const wxString valign
[] =
226 m_radioHAlign
= new wxRadioBox(this, wxID_ANY
, _T("&Horz alignment"),
227 wxDefaultPosition
, wxDefaultSize
,
228 WXSIZEOF(halign
), halign
);
229 m_radioVAlign
= new wxRadioBox(this, wxID_ANY
, _T("&Vert alignment"),
230 wxDefaultPosition
, wxDefaultSize
,
231 WXSIZEOF(valign
), valign
);
233 sizerLeft
->Add(m_radioHAlign
, 0, wxGROW
| wxALL
, 5);
234 sizerLeft
->Add(m_radioVAlign
, 0, wxGROW
| wxALL
, 5);
236 sizerLeft
->Add(5, 5, 0, wxGROW
| wxALL
, 5); // spacer
238 wxButton
*btn
= new wxButton(this, ButtonPage_Reset
, _T("&Reset"));
239 sizerLeft
->Add(btn
, 0, wxALIGN_CENTRE_HORIZONTAL
| wxALL
, 15);
242 wxStaticBox
*box2
= new wxStaticBox(this, wxID_ANY
, _T("&Operations"));
243 wxSizer
*sizerMiddle
= new wxStaticBoxSizer(box2
, wxVERTICAL
);
245 wxSizer
*sizerRow
= CreateSizerWithTextAndButton(ButtonPage_ChangeLabel
,
249 m_textLabel
->SetValue(_T("&Press me!"));
251 sizerMiddle
->Add(sizerRow
, 0, wxALL
| wxGROW
, 5);
254 m_sizerButton
= new wxBoxSizer(wxHORIZONTAL
);
255 m_sizerButton
->SetMinSize(150, 0);
257 // the 3 panes panes compose the window
258 sizerTop
->Add(sizerLeft
, 0, wxGROW
| (wxALL
& ~wxLEFT
), 10);
259 sizerTop
->Add(sizerMiddle
, 1, wxGROW
| wxALL
, 10);
260 sizerTop
->Add(m_sizerButton
, 1, wxGROW
| (wxALL
& ~wxRIGHT
), 10);
262 // do create the main control
271 // ----------------------------------------------------------------------------
273 // ----------------------------------------------------------------------------
275 void ButtonWidgetsPage::Reset()
277 m_chkBitmap
->SetValue(false);
278 m_chkFit
->SetValue(true);
279 m_chkImage
->SetValue(false);
280 m_chkDefault
->SetValue(false);
282 m_chkUseSelected
->SetValue(true);
283 m_chkUseFocused
->SetValue(true);
284 m_chkUseHover
->SetValue(true);
285 m_chkUseDisabled
->SetValue(true);
287 m_radioHAlign
->SetSelection(ButtonHAlign_Centre
);
288 m_radioVAlign
->SetSelection(ButtonVAlign_Centre
);
291 void ButtonWidgetsPage::CreateButton()
296 label
= m_button
->GetLabel();
298 size_t count
= m_sizerButton
->GetChildren().GetCount();
299 for ( size_t n
= 0; n
< count
; n
++ )
301 m_sizerButton
->Remove( 0 );
309 // creating for the first time or recreating a button after bitmap
311 label
= m_textLabel
->GetValue();
315 switch ( m_radioHAlign
->GetSelection() )
317 case ButtonHAlign_Left
:
322 wxFAIL_MSG(_T("unexpected radiobox selection"));
325 case ButtonHAlign_Centre
:
328 case ButtonHAlign_Right
:
333 switch ( m_radioVAlign
->GetSelection() )
335 case ButtonVAlign_Top
:
340 wxFAIL_MSG(_T("unexpected radiobox selection"));
343 case ButtonVAlign_Centre
:
344 // centre vertical alignment is the default (no style)
347 case ButtonVAlign_Bottom
:
348 flags
|= wxBU_BOTTOM
;
352 const bool isBitmapButton
= m_chkBitmap
->GetValue();
353 if ( isBitmapButton
)
355 wxBitmapButton
*bbtn
= new wxBitmapButton(this, ButtonPage_Button
,
356 CreateBitmap(_T("normal")));
357 if ( m_chkUseSelected
->GetValue() )
358 bbtn
->SetBitmapSelected(CreateBitmap(_T("pushed")));
359 if ( m_chkUseFocused
->GetValue() )
360 bbtn
->SetBitmapFocus(CreateBitmap(_T("focused")));
361 if ( m_chkUseHover
->GetValue() )
362 bbtn
->SetBitmapHover(CreateBitmap(_T("hover")));
363 if ( m_chkUseDisabled
->GetValue() )
364 bbtn
->SetBitmapDisabled(CreateBitmap(_T("disabled")));
367 else // normal button
369 m_button
= new wxButton(this, ButtonPage_Button
, label
,
370 wxDefaultPosition
, wxDefaultSize
,
374 m_chkUseSelected
->Enable(isBitmapButton
);
375 m_chkUseFocused
->Enable(isBitmapButton
);
376 m_chkUseHover
->Enable(isBitmapButton
);
377 m_chkUseDisabled
->Enable(isBitmapButton
);
379 #ifdef __WXUNIVERSAL__
380 if ( m_chkImage
->GetValue() )
382 m_button
->SetImageLabel(wxArtProvider::GetIcon(wxART_INFORMATION
));
386 if ( m_chkDefault
->GetValue() )
388 m_button
->SetDefault();
393 m_sizerButton
->Layout();
396 void ButtonWidgetsPage::AddButtonToSizer()
398 if ( m_chkFit
->GetValue() )
400 m_sizerButton
->AddStretchSpacer(1);
401 m_sizerButton
->Add(m_button
, wxSizerFlags(0).Centre().Border());
402 m_sizerButton
->AddStretchSpacer(1);
404 else // take up the entire space
406 m_sizerButton
->Add(m_button
, wxSizerFlags(1).Expand().Border());
410 // ----------------------------------------------------------------------------
412 // ----------------------------------------------------------------------------
414 void ButtonWidgetsPage::OnButtonReset(wxCommandEvent
& WXUNUSED(event
))
421 void ButtonWidgetsPage::OnCheckOrRadioBox(wxCommandEvent
& WXUNUSED(event
))
426 void ButtonWidgetsPage::OnButtonChangeLabel(wxCommandEvent
& WXUNUSED(event
))
428 m_button
->SetLabel(m_textLabel
->GetValue());
431 void ButtonWidgetsPage::OnButton(wxCommandEvent
& WXUNUSED(event
))
433 wxLogMessage(_T("Test button clicked."));
436 // ----------------------------------------------------------------------------
437 // bitmap button stuff
438 // ----------------------------------------------------------------------------
440 wxBitmap
ButtonWidgetsPage::CreateBitmap(const wxString
& label
)
442 wxBitmap
bmp(180, 70); // shouldn't hardcode but it's simpler like this
444 dc
.SelectObject(bmp
);
445 dc
.SetBackground(wxBrush(*wxWHITE
));
447 dc
.SetTextForeground(*wxBLUE
);
448 dc
.DrawLabel(wxStripMenuCodes(m_textLabel
->GetValue()) + _T("\n")
449 _T("(") + label
+ _T(" state)"),
450 wxArtProvider::GetBitmap(wxART_INFORMATION
),
451 wxRect(10, 10, bmp
.GetWidth() - 20, bmp
.GetHeight() - 20),