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(); }
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 #if defined(__WXUNIVERSAL__)
158 #define FAMILY_CTRLS UNIVERSAL_CTRLS
160 #define FAMILY_CTRLS NATIVE_CTRLS
163 IMPLEMENT_WIDGETS_PAGE(ButtonWidgetsPage
, _T("Button"), FAMILY_CTRLS
);
165 ButtonWidgetsPage
::ButtonWidgetsPage(WidgetsBookCtrl
*book
,
166 wxImageList
*imaglist
)
169 imaglist
->Add(wxBitmap(button_xpm
));
179 m_chkUseDisabled
= (wxCheckBox
*)NULL
;
182 m_radioVAlign
= (wxRadioBox
*)NULL
;
184 m_textLabel
= (wxTextCtrl
*)NULL
;
186 m_button
= (wxButton
*)NULL
;
187 m_sizerButton
= (wxSizer
*)NULL
;
189 wxSizer
*sizerTop
= new wxBoxSizer(wxHORIZONTAL
);
192 wxStaticBox
*box
= new wxStaticBox(this, wxID_ANY
, _T("&Set style"));
194 wxSizer
*sizerLeft
= new wxStaticBoxSizer(box
, wxVERTICAL
);
196 m_chkBitmap
= CreateCheckBoxAndAddToSizer(sizerLeft
, _T("&Bitmap button"));
197 m_chkImage
= CreateCheckBoxAndAddToSizer(sizerLeft
, _T("With &image"));
198 m_chkFit
= CreateCheckBoxAndAddToSizer(sizerLeft
, _T("&Fit exactly"));
199 m_chkDefault
= CreateCheckBoxAndAddToSizer(sizerLeft
, _T("&Default"));
201 #ifndef __WXUNIVERSAL__
202 // only wxUniv currently supports buttons with images
203 m_chkImage
->Disable();
206 sizerLeft
->AddSpacer(5);
208 wxSizer
*sizerUseLabels
=
209 new wxStaticBoxSizer(wxVERTICAL
, this, _T("&Use the following labels?"));
210 m_chkUseSelected
= CreateCheckBoxAndAddToSizer(sizerUseLabels
, _T("&Pushed"));
211 m_chkUseFocused
= CreateCheckBoxAndAddToSizer(sizerUseLabels
, _T("&Focused"));
212 m_chkUseHover
= CreateCheckBoxAndAddToSizer(sizerUseLabels
, _T("&Hover"));
213 m_chkUseDisabled
= CreateCheckBoxAndAddToSizer(sizerUseLabels
, _T("&Disabled"));
214 sizerLeft
->Add(sizerUseLabels
, wxSizerFlags().Expand().Border());
216 sizerLeft
->AddSpacer(15);
218 // should be in sync with enums Button[HV]Align!
219 static const wxString halign
[] =
226 static const wxString valign
[] =
233 m_radioHAlign
= new wxRadioBox(this, wxID_ANY
, _T("&Horz alignment"),
234 wxDefaultPosition
, wxDefaultSize
,
235 WXSIZEOF(halign
), halign
);
236 m_radioVAlign
= new wxRadioBox(this, wxID_ANY
, _T("&Vert alignment"),
237 wxDefaultPosition
, wxDefaultSize
,
238 WXSIZEOF(valign
), valign
);
240 sizerLeft
->Add(m_radioHAlign
, 0, wxGROW
| wxALL
, 5);
241 sizerLeft
->Add(m_radioVAlign
, 0, wxGROW
| wxALL
, 5);
243 sizerLeft
->Add(5, 5, 0, wxGROW
| wxALL
, 5); // spacer
245 wxButton
*btn
= new wxButton(this, ButtonPage_Reset
, _T("&Reset"));
246 sizerLeft
->Add(btn
, 0, wxALIGN_CENTRE_HORIZONTAL
| wxALL
, 15);
249 wxStaticBox
*box2
= new wxStaticBox(this, wxID_ANY
, _T("&Operations"));
250 wxSizer
*sizerMiddle
= new wxStaticBoxSizer(box2
, wxVERTICAL
);
252 wxSizer
*sizerRow
= CreateSizerWithTextAndButton(ButtonPage_ChangeLabel
,
256 m_textLabel
->SetValue(_T("&Press me!"));
258 sizerMiddle
->Add(sizerRow
, 0, wxALL
| wxGROW
, 5);
261 m_sizerButton
= new wxBoxSizer(wxHORIZONTAL
);
262 m_sizerButton
->SetMinSize(150, 0);
264 // the 3 panes panes compose the window
265 sizerTop
->Add(sizerLeft
, 0, wxGROW
| (wxALL
& ~wxLEFT
), 10);
266 sizerTop
->Add(sizerMiddle
, 1, wxGROW
| wxALL
, 10);
267 sizerTop
->Add(m_sizerButton
, 1, wxGROW
| (wxALL
& ~wxRIGHT
), 10);
269 // do create the main control
278 // ----------------------------------------------------------------------------
280 // ----------------------------------------------------------------------------
282 void ButtonWidgetsPage
::Reset()
284 m_chkBitmap
->SetValue(false);
285 m_chkFit
->SetValue(true);
286 m_chkImage
->SetValue(false);
287 m_chkDefault
->SetValue(false);
289 m_chkUseSelected
->SetValue(true);
290 m_chkUseFocused
->SetValue(true);
291 m_chkUseHover
->SetValue(true);
292 m_chkUseDisabled
->SetValue(true);
294 m_radioHAlign
->SetSelection(ButtonHAlign_Centre
);
295 m_radioVAlign
->SetSelection(ButtonVAlign_Centre
);
298 void ButtonWidgetsPage
::CreateButton()
303 label
= m_button
->GetLabel();
305 size_t count
= m_sizerButton
->GetChildren().GetCount();
306 for ( size_t n
= 0; n
< count
; n
++ )
308 m_sizerButton
->Remove( 0 );
316 // creating for the first time or recreating a button after bitmap
318 label
= m_textLabel
->GetValue();
321 int flags
= ms_defaultFlags
;
322 switch ( m_radioHAlign
->GetSelection() )
324 case ButtonHAlign_Left
:
329 wxFAIL_MSG(_T("unexpected radiobox selection"));
332 case ButtonHAlign_Centre
:
335 case ButtonHAlign_Right
:
340 switch ( m_radioVAlign
->GetSelection() )
342 case ButtonVAlign_Top
:
347 wxFAIL_MSG(_T("unexpected radiobox selection"));
350 case ButtonVAlign_Centre
:
351 // centre vertical alignment is the default (no style)
354 case ButtonVAlign_Bottom
:
355 flags
|= wxBU_BOTTOM
;
359 const bool isBitmapButton
= m_chkBitmap
->GetValue();
360 if ( isBitmapButton
)
362 wxBitmapButton
*bbtn
= new wxBitmapButton(this, ButtonPage_Button
,
363 CreateBitmap(_T("normal")));
364 if ( m_chkUseSelected
->GetValue() )
365 bbtn
->SetBitmapSelected(CreateBitmap(_T("pushed")));
366 if ( m_chkUseFocused
->GetValue() )
367 bbtn
->SetBitmapFocus(CreateBitmap(_T("focused")));
368 if ( m_chkUseHover
->GetValue() )
369 bbtn
->SetBitmapHover(CreateBitmap(_T("hover")));
370 if ( m_chkUseDisabled
->GetValue() )
371 bbtn
->SetBitmapDisabled(CreateBitmap(_T("disabled")));
374 else // normal button
376 m_button
= new wxButton(this, ButtonPage_Button
, label
,
377 wxDefaultPosition
, wxDefaultSize
,
381 m_chkUseSelected
->Enable(isBitmapButton
);
382 m_chkUseFocused
->Enable(isBitmapButton
);
383 m_chkUseHover
->Enable(isBitmapButton
);
384 m_chkUseDisabled
->Enable(isBitmapButton
);
386 #ifdef __WXUNIVERSAL__
387 if ( m_chkImage
->GetValue() )
389 m_button
->SetImageLabel(wxArtProvider
::GetIcon(wxART_INFORMATION
));
393 if ( m_chkDefault
->GetValue() )
395 m_button
->SetDefault();
400 m_sizerButton
->Layout();
403 void ButtonWidgetsPage
::AddButtonToSizer()
405 if ( m_chkFit
->GetValue() )
407 m_sizerButton
->AddStretchSpacer(1);
408 m_sizerButton
->Add(m_button
, wxSizerFlags(0).Centre().Border());
409 m_sizerButton
->AddStretchSpacer(1);
411 else // take up the entire space
413 m_sizerButton
->Add(m_button
, wxSizerFlags(1).Expand().Border());
417 // ----------------------------------------------------------------------------
419 // ----------------------------------------------------------------------------
421 void ButtonWidgetsPage
::OnButtonReset(wxCommandEvent
& WXUNUSED(event
))
428 void ButtonWidgetsPage
::OnCheckOrRadioBox(wxCommandEvent
& WXUNUSED(event
))
433 void ButtonWidgetsPage
::OnButtonChangeLabel(wxCommandEvent
& WXUNUSED(event
))
435 m_button
->SetLabel(m_textLabel
->GetValue());
438 void ButtonWidgetsPage
::OnButton(wxCommandEvent
& WXUNUSED(event
))
440 wxLogMessage(_T("Test button clicked."));
443 // ----------------------------------------------------------------------------
444 // bitmap button stuff
445 // ----------------------------------------------------------------------------
447 wxBitmap ButtonWidgetsPage
::CreateBitmap(const wxString
& label
)
449 wxBitmap
bmp(180, 70); // shouldn't hardcode but it's simpler like this
451 dc
.SelectObject(bmp
);
452 dc
.SetBackground(wxBrush(*wxWHITE
));
454 dc
.SetTextForeground(*wxBLUE
);
455 dc
.DrawLabel(wxStripMenuCodes(m_textLabel
->GetValue()) + _T("\n")
456 _T("(") + label
+ _T(" state)"),
457 wxArtProvider
::GetBitmap(wxART_INFORMATION
),
458 wxRect(10, 10, bmp
.GetWidth() - 20, bmp
.GetHeight() - 20),