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(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 IMPLEMENT_WIDGETS_PAGE(ButtonWidgetsPage
, _T("Button"),
158 #if defined(__WXUNIVERSAL__)
160 #elif defined(__WXMSW__) || \
161 defined(__WXMOTIF__) || \
162 defined(__WXGTK__) || \
163 defined(__WXMAC__) || \
164 defined(__WXPM__) || \
165 defined(__WXPALMOS__)
172 ButtonWidgetsPage::ButtonWidgetsPage(WidgetsBookCtrl
*book
,
173 wxImageList
*imaglist
)
176 imaglist
->Add(wxBitmap(button_xpm
));
186 m_chkUseDisabled
= (wxCheckBox
*)NULL
;
189 m_radioVAlign
= (wxRadioBox
*)NULL
;
191 m_textLabel
= (wxTextCtrl
*)NULL
;
193 m_button
= (wxButton
*)NULL
;
194 m_sizerButton
= (wxSizer
*)NULL
;
196 wxSizer
*sizerTop
= new wxBoxSizer(wxHORIZONTAL
);
199 wxStaticBox
*box
= new wxStaticBox(this, wxID_ANY
, _T("&Set style"));
201 wxSizer
*sizerLeft
= new wxStaticBoxSizer(box
, wxVERTICAL
);
203 m_chkBitmap
= CreateCheckBoxAndAddToSizer(sizerLeft
, _T("&Bitmap button"));
204 m_chkImage
= CreateCheckBoxAndAddToSizer(sizerLeft
, _T("With &image"));
205 m_chkFit
= CreateCheckBoxAndAddToSizer(sizerLeft
, _T("&Fit exactly"));
206 m_chkDefault
= CreateCheckBoxAndAddToSizer(sizerLeft
, _T("&Default"));
208 #ifndef __WXUNIVERSAL__
209 // only wxUniv currently supports buttons with images
210 m_chkImage
->Disable();
213 sizerLeft
->AddSpacer(5);
215 wxSizer
*sizerUseLabels
=
216 new wxStaticBoxSizer(wxVERTICAL
, this, _T("&Use the following labels?"));
217 m_chkUseSelected
= CreateCheckBoxAndAddToSizer(sizerUseLabels
, _T("&Pushed"));
218 m_chkUseFocused
= CreateCheckBoxAndAddToSizer(sizerUseLabels
, _T("&Focused"));
219 m_chkUseHover
= CreateCheckBoxAndAddToSizer(sizerUseLabels
, _T("&Hover"));
220 m_chkUseDisabled
= CreateCheckBoxAndAddToSizer(sizerUseLabels
, _T("&Disabled"));
221 sizerLeft
->Add(sizerUseLabels
, wxSizerFlags().Expand().Border());
223 sizerLeft
->AddSpacer(15);
225 // should be in sync with enums Button[HV]Align!
226 static const wxString halign
[] =
233 static const wxString valign
[] =
240 m_radioHAlign
= new wxRadioBox(this, wxID_ANY
, _T("&Horz alignment"),
241 wxDefaultPosition
, wxDefaultSize
,
242 WXSIZEOF(halign
), halign
);
243 m_radioVAlign
= new wxRadioBox(this, wxID_ANY
, _T("&Vert alignment"),
244 wxDefaultPosition
, wxDefaultSize
,
245 WXSIZEOF(valign
), valign
);
247 sizerLeft
->Add(m_radioHAlign
, 0, wxGROW
| wxALL
, 5);
248 sizerLeft
->Add(m_radioVAlign
, 0, wxGROW
| wxALL
, 5);
250 sizerLeft
->Add(5, 5, 0, wxGROW
| wxALL
, 5); // spacer
252 wxButton
*btn
= new wxButton(this, ButtonPage_Reset
, _T("&Reset"));
253 sizerLeft
->Add(btn
, 0, wxALIGN_CENTRE_HORIZONTAL
| wxALL
, 15);
256 wxStaticBox
*box2
= new wxStaticBox(this, wxID_ANY
, _T("&Operations"));
257 wxSizer
*sizerMiddle
= new wxStaticBoxSizer(box2
, wxVERTICAL
);
259 wxSizer
*sizerRow
= CreateSizerWithTextAndButton(ButtonPage_ChangeLabel
,
263 m_textLabel
->SetValue(_T("&Press me!"));
265 sizerMiddle
->Add(sizerRow
, 0, wxALL
| wxGROW
, 5);
268 m_sizerButton
= new wxBoxSizer(wxHORIZONTAL
);
269 m_sizerButton
->SetMinSize(150, 0);
271 // the 3 panes panes compose the window
272 sizerTop
->Add(sizerLeft
, 0, wxGROW
| (wxALL
& ~wxLEFT
), 10);
273 sizerTop
->Add(sizerMiddle
, 1, wxGROW
| wxALL
, 10);
274 sizerTop
->Add(m_sizerButton
, 1, wxGROW
| (wxALL
& ~wxRIGHT
), 10);
276 // do create the main control
285 // ----------------------------------------------------------------------------
287 // ----------------------------------------------------------------------------
289 void ButtonWidgetsPage::Reset()
291 m_chkBitmap
->SetValue(false);
292 m_chkFit
->SetValue(true);
293 m_chkImage
->SetValue(false);
294 m_chkDefault
->SetValue(false);
296 m_chkUseSelected
->SetValue(true);
297 m_chkUseFocused
->SetValue(true);
298 m_chkUseHover
->SetValue(true);
299 m_chkUseDisabled
->SetValue(true);
301 m_radioHAlign
->SetSelection(ButtonHAlign_Centre
);
302 m_radioVAlign
->SetSelection(ButtonVAlign_Centre
);
305 void ButtonWidgetsPage::CreateButton()
310 label
= m_button
->GetLabel();
312 size_t count
= m_sizerButton
->GetChildren().GetCount();
313 for ( size_t n
= 0; n
< count
; n
++ )
315 m_sizerButton
->Remove( 0 );
323 // creating for the first time or recreating a button after bitmap
325 label
= m_textLabel
->GetValue();
328 int flags
= ms_defaultFlags
;
329 switch ( m_radioHAlign
->GetSelection() )
331 case ButtonHAlign_Left
:
336 wxFAIL_MSG(_T("unexpected radiobox selection"));
339 case ButtonHAlign_Centre
:
342 case ButtonHAlign_Right
:
347 switch ( m_radioVAlign
->GetSelection() )
349 case ButtonVAlign_Top
:
354 wxFAIL_MSG(_T("unexpected radiobox selection"));
357 case ButtonVAlign_Centre
:
358 // centre vertical alignment is the default (no style)
361 case ButtonVAlign_Bottom
:
362 flags
|= wxBU_BOTTOM
;
366 const bool isBitmapButton
= m_chkBitmap
->GetValue();
367 if ( isBitmapButton
)
369 wxBitmapButton
*bbtn
= new wxBitmapButton(this, ButtonPage_Button
,
370 CreateBitmap(_T("normal")));
371 if ( m_chkUseSelected
->GetValue() )
372 bbtn
->SetBitmapSelected(CreateBitmap(_T("pushed")));
373 if ( m_chkUseFocused
->GetValue() )
374 bbtn
->SetBitmapFocus(CreateBitmap(_T("focused")));
375 if ( m_chkUseHover
->GetValue() )
376 bbtn
->SetBitmapHover(CreateBitmap(_T("hover")));
377 if ( m_chkUseDisabled
->GetValue() )
378 bbtn
->SetBitmapDisabled(CreateBitmap(_T("disabled")));
381 else // normal button
383 m_button
= new wxButton(this, ButtonPage_Button
, label
,
384 wxDefaultPosition
, wxDefaultSize
,
388 m_chkUseSelected
->Enable(isBitmapButton
);
389 m_chkUseFocused
->Enable(isBitmapButton
);
390 m_chkUseHover
->Enable(isBitmapButton
);
391 m_chkUseDisabled
->Enable(isBitmapButton
);
393 #ifdef __WXUNIVERSAL__
394 if ( m_chkImage
->GetValue() )
396 m_button
->SetImageLabel(wxArtProvider::GetIcon(wxART_INFORMATION
));
400 if ( m_chkDefault
->GetValue() )
402 m_button
->SetDefault();
407 m_sizerButton
->Layout();
410 void ButtonWidgetsPage::AddButtonToSizer()
412 if ( m_chkFit
->GetValue() )
414 m_sizerButton
->AddStretchSpacer(1);
415 m_sizerButton
->Add(m_button
, wxSizerFlags(0).Centre().Border());
416 m_sizerButton
->AddStretchSpacer(1);
418 else // take up the entire space
420 m_sizerButton
->Add(m_button
, wxSizerFlags(1).Expand().Border());
424 // ----------------------------------------------------------------------------
426 // ----------------------------------------------------------------------------
428 void ButtonWidgetsPage::OnButtonReset(wxCommandEvent
& WXUNUSED(event
))
435 void ButtonWidgetsPage::OnCheckOrRadioBox(wxCommandEvent
& WXUNUSED(event
))
440 void ButtonWidgetsPage::OnButtonChangeLabel(wxCommandEvent
& WXUNUSED(event
))
442 m_button
->SetLabel(m_textLabel
->GetValue());
445 void ButtonWidgetsPage::OnButton(wxCommandEvent
& WXUNUSED(event
))
447 wxLogMessage(_T("Test button clicked."));
450 // ----------------------------------------------------------------------------
451 // bitmap button stuff
452 // ----------------------------------------------------------------------------
454 wxBitmap
ButtonWidgetsPage::CreateBitmap(const wxString
& label
)
456 wxBitmap
bmp(180, 70); // shouldn't hardcode but it's simpler like this
458 dc
.SelectObject(bmp
);
459 dc
.SetBackground(wxBrush(*wxWHITE
));
461 dc
.SetTextForeground(*wxBLUE
);
462 dc
.DrawLabel(wxStripMenuCodes(m_textLabel
->GetValue()) + _T("\n")
463 _T("(") + label
+ _T(" state)"),
464 wxArtProvider::GetBitmap(wxART_INFORMATION
),
465 wxRect(10, 10, bmp
.GetWidth() - 20, bmp
.GetHeight() - 20),