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
= wxID_HIGHEST
,
56 ButtonPage_ChangeLabel
,
75 // ----------------------------------------------------------------------------
77 // ----------------------------------------------------------------------------
79 class ButtonWidgetsPage
: public WidgetsPage
82 ButtonWidgetsPage(WidgetsBookCtrl
*book
, wxImageList
*imaglist
);
83 virtual ~ButtonWidgetsPage(){};
85 virtual wxControl
*GetWidget() const { return m_button
; }
86 virtual void RecreateWidget() { CreateButton(); }
88 // lazy creation of the content
89 virtual void CreateContent();
93 void OnCheckOrRadioBox(wxCommandEvent
& event
);
95 void OnButton(wxCommandEvent
& event
);
96 void OnButtonReset(wxCommandEvent
& event
);
97 void OnButtonChangeLabel(wxCommandEvent
& event
);
99 // reset the wxButton parameters
102 // (re)create the wxButton
105 // add m_button to m_sizerButton using current value of m_chkFit
106 void AddButtonToSizer();
108 // helper function: create a bitmap for wxBitmapButton
109 wxBitmap
CreateBitmap(const wxString
& label
);
115 // the check/radio boxes for styles
116 wxCheckBox
*m_chkBitmap
,
121 // more checkboxes for wxBitmapButton only
122 wxCheckBox
*m_chkUseSelected
,
127 wxRadioBox
*m_radioHAlign
,
130 // the button itself and the sizer it is in
132 wxSizer
*m_sizerButton
;
134 // the text entries for command parameters
135 wxTextCtrl
*m_textLabel
;
138 DECLARE_EVENT_TABLE()
139 DECLARE_WIDGETS_PAGE(ButtonWidgetsPage
)
142 // ----------------------------------------------------------------------------
144 // ----------------------------------------------------------------------------
146 BEGIN_EVENT_TABLE(ButtonWidgetsPage
, WidgetsPage
)
147 EVT_BUTTON(ButtonPage_Button
, ButtonWidgetsPage::OnButton
)
149 EVT_BUTTON(ButtonPage_Reset
, ButtonWidgetsPage::OnButtonReset
)
150 EVT_BUTTON(ButtonPage_ChangeLabel
, ButtonWidgetsPage::OnButtonChangeLabel
)
152 EVT_CHECKBOX(wxID_ANY
, ButtonWidgetsPage::OnCheckOrRadioBox
)
153 EVT_RADIOBOX(wxID_ANY
, ButtonWidgetsPage::OnCheckOrRadioBox
)
156 // ============================================================================
158 // ============================================================================
160 #if defined(__WXUNIVERSAL__)
161 #define FAMILY_CTRLS UNIVERSAL_CTRLS
163 #define FAMILY_CTRLS NATIVE_CTRLS
166 IMPLEMENT_WIDGETS_PAGE(ButtonWidgetsPage
, _T("Button"), FAMILY_CTRLS
);
168 ButtonWidgetsPage::ButtonWidgetsPage(WidgetsBookCtrl
*book
,
169 wxImageList
*imaglist
)
170 : WidgetsPage(book
, imaglist
, button_xpm
)
180 m_chkUseDisabled
= (wxCheckBox
*)NULL
;
183 m_radioVAlign
= (wxRadioBox
*)NULL
;
185 m_textLabel
= (wxTextCtrl
*)NULL
;
187 m_button
= (wxButton
*)NULL
;
188 m_sizerButton
= (wxSizer
*)NULL
;
191 void ButtonWidgetsPage::CreateContent()
193 wxSizer
*sizerTop
= new wxBoxSizer(wxHORIZONTAL
);
196 wxStaticBox
*box
= new wxStaticBox(this, wxID_ANY
, _T("&Set style"));
198 wxSizer
*sizerLeft
= new wxStaticBoxSizer(box
, wxVERTICAL
);
200 m_chkBitmap
= CreateCheckBoxAndAddToSizer(sizerLeft
, _T("&Bitmap button"));
201 m_chkImage
= CreateCheckBoxAndAddToSizer(sizerLeft
, _T("With &image"));
202 m_chkFit
= CreateCheckBoxAndAddToSizer(sizerLeft
, _T("&Fit exactly"));
203 m_chkDefault
= CreateCheckBoxAndAddToSizer(sizerLeft
, _T("&Default"));
205 #ifndef __WXUNIVERSAL__
206 // only wxUniv currently supports buttons with images
207 m_chkImage
->Disable();
210 sizerLeft
->AddSpacer(5);
212 wxSizer
*sizerUseLabels
=
213 new wxStaticBoxSizer(wxVERTICAL
, this, _T("&Use the following labels?"));
214 m_chkUseSelected
= CreateCheckBoxAndAddToSizer(sizerUseLabels
, _T("&Pushed"));
215 m_chkUseFocused
= CreateCheckBoxAndAddToSizer(sizerUseLabels
, _T("&Focused"));
216 m_chkUseHover
= CreateCheckBoxAndAddToSizer(sizerUseLabels
, _T("&Hover"));
217 m_chkUseDisabled
= CreateCheckBoxAndAddToSizer(sizerUseLabels
, _T("&Disabled"));
218 sizerLeft
->Add(sizerUseLabels
, wxSizerFlags().Expand().Border());
220 sizerLeft
->AddSpacer(15);
222 // should be in sync with enums Button[HV]Align!
223 static const wxString halign
[] =
230 static const wxString valign
[] =
237 m_radioHAlign
= new wxRadioBox(this, wxID_ANY
, _T("&Horz alignment"),
238 wxDefaultPosition
, wxDefaultSize
,
239 WXSIZEOF(halign
), halign
);
240 m_radioVAlign
= new wxRadioBox(this, wxID_ANY
, _T("&Vert alignment"),
241 wxDefaultPosition
, wxDefaultSize
,
242 WXSIZEOF(valign
), valign
);
244 sizerLeft
->Add(m_radioHAlign
, 0, wxGROW
| wxALL
, 5);
245 sizerLeft
->Add(m_radioVAlign
, 0, wxGROW
| wxALL
, 5);
247 sizerLeft
->Add(5, 5, 0, wxGROW
| wxALL
, 5); // spacer
249 wxButton
*btn
= new wxButton(this, ButtonPage_Reset
, _T("&Reset"));
250 sizerLeft
->Add(btn
, 0, wxALIGN_CENTRE_HORIZONTAL
| wxALL
, 15);
253 wxStaticBox
*box2
= new wxStaticBox(this, wxID_ANY
, _T("&Operations"));
254 wxSizer
*sizerMiddle
= new wxStaticBoxSizer(box2
, wxVERTICAL
);
256 wxSizer
*sizerRow
= CreateSizerWithTextAndButton(ButtonPage_ChangeLabel
,
260 m_textLabel
->SetValue(_T("&Press me!"));
262 sizerMiddle
->Add(sizerRow
, 0, wxALL
| wxGROW
, 5);
265 m_sizerButton
= new wxBoxSizer(wxHORIZONTAL
);
266 m_sizerButton
->SetMinSize(150, 0);
268 // the 3 panes panes compose the window
269 sizerTop
->Add(sizerLeft
, 0, wxGROW
| (wxALL
& ~wxLEFT
), 10);
270 sizerTop
->Add(sizerMiddle
, 1, wxGROW
| wxALL
, 10);
271 sizerTop
->Add(m_sizerButton
, 1, wxGROW
| (wxALL
& ~wxRIGHT
), 10);
273 // do create the main control
282 // ----------------------------------------------------------------------------
284 // ----------------------------------------------------------------------------
286 void ButtonWidgetsPage::Reset()
288 m_chkBitmap
->SetValue(false);
289 m_chkFit
->SetValue(true);
290 m_chkImage
->SetValue(false);
291 m_chkDefault
->SetValue(false);
293 m_chkUseSelected
->SetValue(true);
294 m_chkUseFocused
->SetValue(true);
295 m_chkUseHover
->SetValue(true);
296 m_chkUseDisabled
->SetValue(true);
298 m_radioHAlign
->SetSelection(ButtonHAlign_Centre
);
299 m_radioVAlign
->SetSelection(ButtonVAlign_Centre
);
302 void ButtonWidgetsPage::CreateButton()
307 label
= m_button
->GetLabel();
309 size_t count
= m_sizerButton
->GetChildren().GetCount();
310 for ( size_t n
= 0; n
< count
; n
++ )
312 m_sizerButton
->Remove( 0 );
320 // creating for the first time or recreating a button after bitmap
322 label
= m_textLabel
->GetValue();
325 int flags
= ms_defaultFlags
;
326 switch ( m_radioHAlign
->GetSelection() )
328 case ButtonHAlign_Left
:
333 wxFAIL_MSG(_T("unexpected radiobox selection"));
336 case ButtonHAlign_Centre
:
339 case ButtonHAlign_Right
:
344 switch ( m_radioVAlign
->GetSelection() )
346 case ButtonVAlign_Top
:
351 wxFAIL_MSG(_T("unexpected radiobox selection"));
354 case ButtonVAlign_Centre
:
355 // centre vertical alignment is the default (no style)
358 case ButtonVAlign_Bottom
:
359 flags
|= wxBU_BOTTOM
;
363 const bool isBitmapButton
= m_chkBitmap
->GetValue();
364 if ( isBitmapButton
)
366 wxBitmapButton
*bbtn
= new wxBitmapButton(this, ButtonPage_Button
,
367 CreateBitmap(_T("normal")));
368 if ( m_chkUseSelected
->GetValue() )
369 bbtn
->SetBitmapSelected(CreateBitmap(_T("pushed")));
370 if ( m_chkUseFocused
->GetValue() )
371 bbtn
->SetBitmapFocus(CreateBitmap(_T("focused")));
372 if ( m_chkUseHover
->GetValue() )
373 bbtn
->SetBitmapHover(CreateBitmap(_T("hover")));
374 if ( m_chkUseDisabled
->GetValue() )
375 bbtn
->SetBitmapDisabled(CreateBitmap(_T("disabled")));
378 else // normal button
380 m_button
= new wxButton(this, ButtonPage_Button
, label
,
381 wxDefaultPosition
, wxDefaultSize
,
385 m_chkUseSelected
->Enable(isBitmapButton
);
386 m_chkUseFocused
->Enable(isBitmapButton
);
387 m_chkUseHover
->Enable(isBitmapButton
);
388 m_chkUseDisabled
->Enable(isBitmapButton
);
390 #ifdef __WXUNIVERSAL__
391 if ( m_chkImage
->GetValue() )
393 m_button
->SetImageLabel(wxArtProvider::GetIcon(wxART_INFORMATION
));
397 if ( m_chkDefault
->GetValue() )
399 m_button
->SetDefault();
404 m_sizerButton
->Layout();
407 void ButtonWidgetsPage::AddButtonToSizer()
409 if ( m_chkFit
->GetValue() )
411 m_sizerButton
->AddStretchSpacer(1);
412 m_sizerButton
->Add(m_button
, wxSizerFlags(0).Centre().Border());
413 m_sizerButton
->AddStretchSpacer(1);
415 else // take up the entire space
417 m_sizerButton
->Add(m_button
, wxSizerFlags(1).Expand().Border());
421 // ----------------------------------------------------------------------------
423 // ----------------------------------------------------------------------------
425 void ButtonWidgetsPage::OnButtonReset(wxCommandEvent
& WXUNUSED(event
))
432 void ButtonWidgetsPage::OnCheckOrRadioBox(wxCommandEvent
& WXUNUSED(event
))
437 void ButtonWidgetsPage::OnButtonChangeLabel(wxCommandEvent
& WXUNUSED(event
))
439 m_button
->SetLabel(m_textLabel
->GetValue());
442 void ButtonWidgetsPage::OnButton(wxCommandEvent
& WXUNUSED(event
))
444 wxLogMessage(_T("Test button clicked."));
447 // ----------------------------------------------------------------------------
448 // bitmap button stuff
449 // ----------------------------------------------------------------------------
451 wxBitmap
ButtonWidgetsPage::CreateBitmap(const wxString
& label
)
453 wxBitmap
bmp(180, 70); // shouldn't hardcode but it's simpler like this
455 dc
.SelectObject(bmp
);
456 dc
.SetBackground(wxBrush(*wxWHITE
));
458 dc
.SetTextForeground(*wxBLUE
);
459 dc
.DrawLabel(wxStripMenuCodes(m_textLabel
->GetValue()) + _T("\n")
460 _T("(") + label
+ _T(" state)"),
461 wxArtProvider::GetBitmap(wxART_INFORMATION
),
462 wxRect(10, 10, bmp
.GetWidth() - 20, bmp
.GetHeight() - 20),