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
,
83 // ----------------------------------------------------------------------------
85 // ----------------------------------------------------------------------------
87 class ButtonWidgetsPage
: public WidgetsPage
90 ButtonWidgetsPage(WidgetsBookCtrl
*book
, wxImageList
*imaglist
);
91 virtual ~ButtonWidgetsPage(){};
93 virtual wxControl
*GetWidget() const { return m_button
; }
94 virtual void RecreateWidget() { CreateButton(); }
96 // lazy creation of the content
97 virtual void CreateContent();
101 void OnCheckOrRadioBox(wxCommandEvent
& event
);
103 void OnButton(wxCommandEvent
& event
);
104 void OnButtonReset(wxCommandEvent
& event
);
105 void OnButtonChangeLabel(wxCommandEvent
& event
);
107 // reset the wxButton parameters
110 // (re)create the wxButton
113 // add m_button to m_sizerButton using current value of m_chkFit
114 void AddButtonToSizer();
116 // helper function: create a bitmap for wxBitmapButton
117 wxBitmap
CreateBitmap(const wxString
& label
);
123 // the check/radio boxes for styles
124 wxCheckBox
*m_chkBitmapOnly
,
129 // more checkboxes for wxBitmapButton only
130 wxCheckBox
*m_chkUsePressed
,
135 // and an image position choice used if m_chkTextAndBitmap is on
136 wxRadioBox
*m_radioImagePos
;
138 wxRadioBox
*m_radioHAlign
,
141 // the button itself and the sizer it is in
143 wxSizer
*m_sizerButton
;
145 // the text entries for command parameters
146 wxTextCtrl
*m_textLabel
;
149 DECLARE_EVENT_TABLE()
150 DECLARE_WIDGETS_PAGE(ButtonWidgetsPage
)
153 // ----------------------------------------------------------------------------
155 // ----------------------------------------------------------------------------
157 BEGIN_EVENT_TABLE(ButtonWidgetsPage
, WidgetsPage
)
158 EVT_BUTTON(ButtonPage_Button
, ButtonWidgetsPage::OnButton
)
160 EVT_BUTTON(ButtonPage_Reset
, ButtonWidgetsPage::OnButtonReset
)
161 EVT_BUTTON(ButtonPage_ChangeLabel
, ButtonWidgetsPage::OnButtonChangeLabel
)
163 EVT_CHECKBOX(wxID_ANY
, ButtonWidgetsPage::OnCheckOrRadioBox
)
164 EVT_RADIOBOX(wxID_ANY
, ButtonWidgetsPage::OnCheckOrRadioBox
)
167 // ============================================================================
169 // ============================================================================
171 #if defined(__WXUNIVERSAL__)
172 #define FAMILY_CTRLS UNIVERSAL_CTRLS
174 #define FAMILY_CTRLS NATIVE_CTRLS
177 IMPLEMENT_WIDGETS_PAGE(ButtonWidgetsPage
, wxT("Button"), FAMILY_CTRLS
);
179 ButtonWidgetsPage::ButtonWidgetsPage(WidgetsBookCtrl
*book
,
180 wxImageList
*imaglist
)
181 : WidgetsPage(book
, imaglist
, button_xpm
)
191 m_chkUseDisabled
= (wxCheckBox
*)NULL
;
195 m_radioVAlign
= (wxRadioBox
*)NULL
;
197 m_textLabel
= (wxTextCtrl
*)NULL
;
199 m_button
= (wxButton
*)NULL
;
200 m_sizerButton
= (wxSizer
*)NULL
;
203 void ButtonWidgetsPage::CreateContent()
205 wxSizer
*sizerTop
= new wxBoxSizer(wxHORIZONTAL
);
208 wxStaticBox
*box
= new wxStaticBox(this, wxID_ANY
, wxT("&Set style"));
210 wxSizer
*sizerLeft
= new wxStaticBoxSizer(box
, wxVERTICAL
);
212 m_chkBitmapOnly
= CreateCheckBoxAndAddToSizer(sizerLeft
, "&Bitmap only");
213 m_chkTextAndBitmap
= CreateCheckBoxAndAddToSizer(sizerLeft
, "Text &and &bitmap");
214 m_chkFit
= CreateCheckBoxAndAddToSizer(sizerLeft
, wxT("&Fit exactly"));
215 m_chkDefault
= CreateCheckBoxAndAddToSizer(sizerLeft
, wxT("&Default"));
217 sizerLeft
->AddSpacer(5);
219 wxSizer
*sizerUseLabels
=
220 new wxStaticBoxSizer(wxVERTICAL
, this,
221 "&Use the following bitmaps in addition to the normal one?");
222 m_chkUsePressed
= CreateCheckBoxAndAddToSizer(sizerUseLabels
,
223 "&Pressed (small help icon)");
224 m_chkUseFocused
= CreateCheckBoxAndAddToSizer(sizerUseLabels
,
225 "&Focused (small error icon)");
226 m_chkUseCurrent
= CreateCheckBoxAndAddToSizer(sizerUseLabels
,
227 "&Current (small warning icon)");
228 m_chkUseDisabled
= CreateCheckBoxAndAddToSizer(sizerUseLabels
,
229 "&Disabled (broken image icon)");
230 sizerLeft
->Add(sizerUseLabels
, wxSizerFlags().Expand().Border());
232 sizerLeft
->AddSpacer(10);
234 static const wxString dirs
[] =
236 "left", "right", "top", "bottom",
238 m_radioImagePos
= new wxRadioBox(this, wxID_ANY
, "Image &position",
239 wxDefaultPosition
, wxDefaultSize
,
240 WXSIZEOF(dirs
), dirs
);
241 sizerLeft
->Add(m_radioImagePos
, 0, wxGROW
| wxALL
, 5);
242 sizerLeft
->AddSpacer(15);
244 // should be in sync with enums Button[HV]Align!
245 static const wxString halign
[] =
252 static const wxString valign
[] =
259 m_radioHAlign
= new wxRadioBox(this, wxID_ANY
, wxT("&Horz alignment"),
260 wxDefaultPosition
, wxDefaultSize
,
261 WXSIZEOF(halign
), halign
);
262 m_radioVAlign
= new wxRadioBox(this, wxID_ANY
, wxT("&Vert alignment"),
263 wxDefaultPosition
, wxDefaultSize
,
264 WXSIZEOF(valign
), valign
);
266 sizerLeft
->Add(m_radioHAlign
, 0, wxGROW
| wxALL
, 5);
267 sizerLeft
->Add(m_radioVAlign
, 0, wxGROW
| wxALL
, 5);
269 sizerLeft
->Add(5, 5, 0, wxGROW
| wxALL
, 5); // spacer
271 wxButton
*btn
= new wxButton(this, ButtonPage_Reset
, wxT("&Reset"));
272 sizerLeft
->Add(btn
, 0, wxALIGN_CENTRE_HORIZONTAL
| wxALL
, 15);
275 wxStaticBox
*box2
= new wxStaticBox(this, wxID_ANY
, wxT("&Operations"));
276 wxSizer
*sizerMiddle
= new wxStaticBoxSizer(box2
, wxVERTICAL
);
278 wxSizer
*sizerRow
= CreateSizerWithTextAndButton(ButtonPage_ChangeLabel
,
282 m_textLabel
->SetValue(wxT("&Press me!"));
284 sizerMiddle
->Add(sizerRow
, 0, wxALL
| wxGROW
, 5);
287 m_sizerButton
= new wxBoxSizer(wxHORIZONTAL
);
288 m_sizerButton
->SetMinSize(150, 0);
290 // the 3 panes panes compose the window
291 sizerTop
->Add(sizerLeft
, 0, wxGROW
| (wxALL
& ~wxLEFT
), 10);
292 sizerTop
->Add(sizerMiddle
, 1, wxGROW
| wxALL
, 10);
293 sizerTop
->Add(m_sizerButton
, 1, wxGROW
| (wxALL
& ~wxRIGHT
), 10);
295 // do create the main control
302 // ----------------------------------------------------------------------------
304 // ----------------------------------------------------------------------------
306 void ButtonWidgetsPage::Reset()
308 m_chkBitmapOnly
->SetValue(false);
309 m_chkFit
->SetValue(true);
310 m_chkTextAndBitmap
->SetValue(false);
311 m_chkDefault
->SetValue(false);
313 m_chkUsePressed
->SetValue(true);
314 m_chkUseFocused
->SetValue(true);
315 m_chkUseCurrent
->SetValue(true);
316 m_chkUseDisabled
->SetValue(true);
318 m_radioImagePos
->SetSelection(ButtonImagePos_Left
);
319 m_radioHAlign
->SetSelection(ButtonHAlign_Centre
);
320 m_radioVAlign
->SetSelection(ButtonVAlign_Centre
);
323 void ButtonWidgetsPage::CreateButton()
328 label
= m_button
->GetLabel();
330 size_t count
= m_sizerButton
->GetChildren().GetCount();
331 for ( size_t n
= 0; n
< count
; n
++ )
333 m_sizerButton
->Remove( 0 );
341 // creating for the first time or recreating a button after bitmap
343 label
= m_textLabel
->GetValue();
346 int flags
= ms_defaultFlags
;
347 switch ( m_radioHAlign
->GetSelection() )
349 case ButtonHAlign_Left
:
354 wxFAIL_MSG(wxT("unexpected radiobox selection"));
357 case ButtonHAlign_Centre
:
360 case ButtonHAlign_Right
:
365 switch ( m_radioVAlign
->GetSelection() )
367 case ButtonVAlign_Top
:
372 wxFAIL_MSG(wxT("unexpected radiobox selection"));
375 case ButtonVAlign_Centre
:
376 // centre vertical alignment is the default (no style)
379 case ButtonVAlign_Bottom
:
380 flags
|= wxBU_BOTTOM
;
384 bool showsBitmap
= false;
385 if ( m_chkBitmapOnly
->GetValue() )
389 wxBitmapButton
*bbtn
= new wxBitmapButton(this, ButtonPage_Button
,
390 CreateBitmap(wxT("normal")));
391 if ( m_chkUsePressed
->GetValue() )
392 bbtn
->SetBitmapPressed(CreateBitmap(wxT("pushed")));
393 if ( m_chkUseFocused
->GetValue() )
394 bbtn
->SetBitmapFocus(CreateBitmap(wxT("focused")));
395 if ( m_chkUseCurrent
->GetValue() )
396 bbtn
->SetBitmapCurrent(CreateBitmap(wxT("hover")));
397 if ( m_chkUseDisabled
->GetValue() )
398 bbtn
->SetBitmapDisabled(CreateBitmap(wxT("disabled")));
401 else // normal button
403 m_button
= new wxButton(this, ButtonPage_Button
, label
,
404 wxDefaultPosition
, wxDefaultSize
,
408 if ( !showsBitmap
&& m_chkTextAndBitmap
->GetValue() )
412 static const wxDirection positions
[] =
414 wxLEFT
, wxRIGHT
, wxTOP
, wxBOTTOM
417 m_button
->SetBitmap(wxArtProvider::GetIcon(wxART_INFORMATION
),
418 positions
[m_radioImagePos
->GetSelection()]);
420 if ( m_chkUsePressed
->GetValue() )
421 m_button
->SetBitmapPressed(wxArtProvider::GetIcon(wxART_HELP
));
422 if ( m_chkUseFocused
->GetValue() )
423 m_button
->SetBitmapFocus(wxArtProvider::GetIcon(wxART_ERROR
));
424 if ( m_chkUseCurrent
->GetValue() )
425 m_button
->SetBitmapCurrent(wxArtProvider::GetIcon(wxART_WARNING
));
426 if ( m_chkUseDisabled
->GetValue() )
427 m_button
->SetBitmapDisabled(wxArtProvider::GetIcon(wxART_MISSING_IMAGE
));
430 m_chkUsePressed
->Enable(showsBitmap
);
431 m_chkUseFocused
->Enable(showsBitmap
);
432 m_chkUseCurrent
->Enable(showsBitmap
);
433 m_chkUseDisabled
->Enable(showsBitmap
);
435 if ( m_chkDefault
->GetValue() )
437 m_button
->SetDefault();
442 m_sizerButton
->Layout();
445 void ButtonWidgetsPage::AddButtonToSizer()
447 if ( m_chkFit
->GetValue() )
449 m_sizerButton
->AddStretchSpacer(1);
450 m_sizerButton
->Add(m_button
, wxSizerFlags(0).Centre().Border());
451 m_sizerButton
->AddStretchSpacer(1);
453 else // take up the entire space
455 m_sizerButton
->Add(m_button
, wxSizerFlags(1).Expand().Border());
459 // ----------------------------------------------------------------------------
461 // ----------------------------------------------------------------------------
463 void ButtonWidgetsPage::OnButtonReset(wxCommandEvent
& WXUNUSED(event
))
470 void ButtonWidgetsPage::OnCheckOrRadioBox(wxCommandEvent
& WXUNUSED(event
))
475 void ButtonWidgetsPage::OnButtonChangeLabel(wxCommandEvent
& WXUNUSED(event
))
477 m_button
->SetLabel(m_textLabel
->GetValue());
479 m_sizerButton
->Layout();
482 void ButtonWidgetsPage::OnButton(wxCommandEvent
& WXUNUSED(event
))
484 wxLogMessage(wxT("Test button clicked."));
487 // ----------------------------------------------------------------------------
488 // bitmap button stuff
489 // ----------------------------------------------------------------------------
491 wxBitmap
ButtonWidgetsPage::CreateBitmap(const wxString
& label
)
493 wxBitmap
bmp(180, 70); // shouldn't hardcode but it's simpler like this
495 dc
.SelectObject(bmp
);
496 dc
.SetBackground(wxBrush(*wxCYAN
));
498 dc
.SetTextForeground(*wxBLACK
);
499 dc
.DrawLabel(wxStripMenuCodes(m_textLabel
->GetValue()) + wxT("\n")
500 wxT("(") + label
+ wxT(" state)"),
501 wxArtProvider::GetBitmap(wxART_INFORMATION
),
502 wxRect(10, 10, bmp
.GetWidth() - 20, bmp
.GetHeight() - 20),