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
,
130 // more checkboxes for wxBitmapButton only
131 wxCheckBox
*m_chkUsePressed
,
136 // and an image position choice used if m_chkTextAndBitmap is on
137 wxRadioBox
*m_radioImagePos
;
139 wxRadioBox
*m_radioHAlign
,
142 // the button itself and the sizer it is in
144 wxSizer
*m_sizerButton
;
146 // the text entries for command parameters
147 wxTextCtrl
*m_textLabel
;
150 DECLARE_EVENT_TABLE()
151 DECLARE_WIDGETS_PAGE(ButtonWidgetsPage
)
154 // ----------------------------------------------------------------------------
156 // ----------------------------------------------------------------------------
158 BEGIN_EVENT_TABLE(ButtonWidgetsPage
, WidgetsPage
)
159 EVT_BUTTON(ButtonPage_Button
, ButtonWidgetsPage::OnButton
)
161 EVT_BUTTON(ButtonPage_Reset
, ButtonWidgetsPage::OnButtonReset
)
162 EVT_BUTTON(ButtonPage_ChangeLabel
, ButtonWidgetsPage::OnButtonChangeLabel
)
164 EVT_CHECKBOX(wxID_ANY
, ButtonWidgetsPage::OnCheckOrRadioBox
)
165 EVT_RADIOBOX(wxID_ANY
, ButtonWidgetsPage::OnCheckOrRadioBox
)
168 // ============================================================================
170 // ============================================================================
172 #if defined(__WXUNIVERSAL__)
173 #define FAMILY_CTRLS UNIVERSAL_CTRLS
175 #define FAMILY_CTRLS NATIVE_CTRLS
178 IMPLEMENT_WIDGETS_PAGE(ButtonWidgetsPage
, wxT("Button"), FAMILY_CTRLS
);
180 ButtonWidgetsPage::ButtonWidgetsPage(WidgetsBookCtrl
*book
,
181 wxImageList
*imaglist
)
182 : WidgetsPage(book
, imaglist
, button_xpm
)
193 m_chkUseDisabled
= (wxCheckBox
*)NULL
;
197 m_radioVAlign
= (wxRadioBox
*)NULL
;
199 m_textLabel
= (wxTextCtrl
*)NULL
;
201 m_button
= (wxButton
*)NULL
;
202 m_sizerButton
= (wxSizer
*)NULL
;
205 void ButtonWidgetsPage::CreateContent()
207 wxSizer
*sizerTop
= new wxBoxSizer(wxHORIZONTAL
);
210 wxStaticBox
*box
= new wxStaticBox(this, wxID_ANY
, wxT("&Set style"));
212 wxSizer
*sizerLeft
= new wxStaticBoxSizer(box
, wxVERTICAL
);
214 m_chkBitmapOnly
= CreateCheckBoxAndAddToSizer(sizerLeft
, "&Bitmap only");
215 m_chkTextAndBitmap
= CreateCheckBoxAndAddToSizer(sizerLeft
, "Text &and bitmap");
216 m_chkFit
= CreateCheckBoxAndAddToSizer(sizerLeft
, wxT("&Fit exactly"));
217 m_chkAuthNeeded
= CreateCheckBoxAndAddToSizer(sizerLeft
, wxT("Require a&uth"));
218 m_chkDefault
= CreateCheckBoxAndAddToSizer(sizerLeft
, wxT("&Default"));
220 sizerLeft
->AddSpacer(5);
222 wxSizer
*sizerUseLabels
=
223 new wxStaticBoxSizer(wxVERTICAL
, this,
224 "&Use the following bitmaps in addition to the normal one?");
225 m_chkUsePressed
= CreateCheckBoxAndAddToSizer(sizerUseLabels
,
226 "&Pressed (small help icon)");
227 m_chkUseFocused
= CreateCheckBoxAndAddToSizer(sizerUseLabels
,
228 "&Focused (small error icon)");
229 m_chkUseCurrent
= CreateCheckBoxAndAddToSizer(sizerUseLabels
,
230 "&Current (small warning icon)");
231 m_chkUseDisabled
= CreateCheckBoxAndAddToSizer(sizerUseLabels
,
232 "&Disabled (broken image icon)");
233 sizerLeft
->Add(sizerUseLabels
, wxSizerFlags().Expand().Border());
235 sizerLeft
->AddSpacer(10);
237 static const wxString dirs
[] =
239 "left", "right", "top", "bottom",
241 m_radioImagePos
= new wxRadioBox(this, wxID_ANY
, "Image &position",
242 wxDefaultPosition
, wxDefaultSize
,
243 WXSIZEOF(dirs
), dirs
);
244 sizerLeft
->Add(m_radioImagePos
, 0, wxGROW
| wxALL
, 5);
245 sizerLeft
->AddSpacer(15);
247 // should be in sync with enums Button[HV]Align!
248 static const wxString halign
[] =
255 static const wxString valign
[] =
262 m_radioHAlign
= new wxRadioBox(this, wxID_ANY
, wxT("&Horz alignment"),
263 wxDefaultPosition
, wxDefaultSize
,
264 WXSIZEOF(halign
), halign
);
265 m_radioVAlign
= new wxRadioBox(this, wxID_ANY
, wxT("&Vert alignment"),
266 wxDefaultPosition
, wxDefaultSize
,
267 WXSIZEOF(valign
), valign
);
269 sizerLeft
->Add(m_radioHAlign
, 0, wxGROW
| wxALL
, 5);
270 sizerLeft
->Add(m_radioVAlign
, 0, wxGROW
| wxALL
, 5);
272 sizerLeft
->Add(5, 5, 0, wxGROW
| wxALL
, 5); // spacer
274 wxButton
*btn
= new wxButton(this, ButtonPage_Reset
, wxT("&Reset"));
275 sizerLeft
->Add(btn
, 0, wxALIGN_CENTRE_HORIZONTAL
| wxALL
, 15);
278 wxStaticBox
*box2
= new wxStaticBox(this, wxID_ANY
, wxT("&Operations"));
279 wxSizer
*sizerMiddle
= new wxStaticBoxSizer(box2
, wxVERTICAL
);
281 wxSizer
*sizerRow
= CreateSizerWithTextAndButton(ButtonPage_ChangeLabel
,
285 m_textLabel
->SetValue(wxT("&Press me!"));
287 sizerMiddle
->Add(sizerRow
, 0, wxALL
| wxGROW
, 5);
290 m_sizerButton
= new wxBoxSizer(wxHORIZONTAL
);
291 m_sizerButton
->SetMinSize(150, 0);
293 // the 3 panes panes compose the window
294 sizerTop
->Add(sizerLeft
, 0, wxGROW
| (wxALL
& ~wxLEFT
), 10);
295 sizerTop
->Add(sizerMiddle
, 1, wxGROW
| wxALL
, 10);
296 sizerTop
->Add(m_sizerButton
, 1, wxGROW
| (wxALL
& ~wxRIGHT
), 10);
298 // do create the main control
305 // ----------------------------------------------------------------------------
307 // ----------------------------------------------------------------------------
309 void ButtonWidgetsPage::Reset()
311 m_chkBitmapOnly
->SetValue(false);
312 m_chkFit
->SetValue(true);
313 m_chkAuthNeeded
->SetValue(false);
314 m_chkTextAndBitmap
->SetValue(false);
315 m_chkDefault
->SetValue(false);
317 m_chkUsePressed
->SetValue(true);
318 m_chkUseFocused
->SetValue(true);
319 m_chkUseCurrent
->SetValue(true);
320 m_chkUseDisabled
->SetValue(true);
322 m_radioImagePos
->SetSelection(ButtonImagePos_Left
);
323 m_radioHAlign
->SetSelection(ButtonHAlign_Centre
);
324 m_radioVAlign
->SetSelection(ButtonVAlign_Centre
);
327 void ButtonWidgetsPage::CreateButton()
332 label
= m_button
->GetLabel();
334 size_t count
= m_sizerButton
->GetChildren().GetCount();
335 for ( size_t n
= 0; n
< count
; n
++ )
337 m_sizerButton
->Remove( 0 );
345 // creating for the first time or recreating a button after bitmap
347 label
= m_textLabel
->GetValue();
350 int flags
= ms_defaultFlags
;
351 switch ( m_radioHAlign
->GetSelection() )
353 case ButtonHAlign_Left
:
358 wxFAIL_MSG(wxT("unexpected radiobox selection"));
361 case ButtonHAlign_Centre
:
364 case ButtonHAlign_Right
:
369 switch ( m_radioVAlign
->GetSelection() )
371 case ButtonVAlign_Top
:
376 wxFAIL_MSG(wxT("unexpected radiobox selection"));
379 case ButtonVAlign_Centre
:
380 // centre vertical alignment is the default (no style)
383 case ButtonVAlign_Bottom
:
384 flags
|= wxBU_BOTTOM
;
388 bool showsBitmap
= false;
389 if ( m_chkBitmapOnly
->GetValue() )
393 wxBitmapButton
*bbtn
= new wxBitmapButton(this, ButtonPage_Button
,
394 CreateBitmap(wxT("normal")));
395 if ( m_chkUsePressed
->GetValue() )
396 bbtn
->SetBitmapPressed(CreateBitmap(wxT("pushed")));
397 if ( m_chkUseFocused
->GetValue() )
398 bbtn
->SetBitmapFocus(CreateBitmap(wxT("focused")));
399 if ( m_chkUseCurrent
->GetValue() )
400 bbtn
->SetBitmapCurrent(CreateBitmap(wxT("hover")));
401 if ( m_chkUseDisabled
->GetValue() )
402 bbtn
->SetBitmapDisabled(CreateBitmap(wxT("disabled")));
405 else // normal button
407 m_button
= new wxButton(this, ButtonPage_Button
, label
,
408 wxDefaultPosition
, wxDefaultSize
,
412 if ( !showsBitmap
&& m_chkTextAndBitmap
->GetValue() )
416 static const wxDirection positions
[] =
418 wxLEFT
, wxRIGHT
, wxTOP
, wxBOTTOM
421 m_button
->SetBitmap(wxArtProvider::GetIcon(wxART_INFORMATION
),
422 positions
[m_radioImagePos
->GetSelection()]);
424 if ( m_chkUsePressed
->GetValue() )
425 m_button
->SetBitmapPressed(wxArtProvider::GetIcon(wxART_HELP
));
426 if ( m_chkUseFocused
->GetValue() )
427 m_button
->SetBitmapFocus(wxArtProvider::GetIcon(wxART_ERROR
));
428 if ( m_chkUseCurrent
->GetValue() )
429 m_button
->SetBitmapCurrent(wxArtProvider::GetIcon(wxART_WARNING
));
430 if ( m_chkUseDisabled
->GetValue() )
431 m_button
->SetBitmapDisabled(wxArtProvider::GetIcon(wxART_MISSING_IMAGE
));
434 m_chkUsePressed
->Enable(showsBitmap
);
435 m_chkUseFocused
->Enable(showsBitmap
);
436 m_chkUseCurrent
->Enable(showsBitmap
);
437 m_chkUseDisabled
->Enable(showsBitmap
);
439 if ( m_chkAuthNeeded
->GetValue() )
440 m_button
->SetAuthNeeded();
442 if ( m_chkDefault
->GetValue() )
443 m_button
->SetDefault();
447 m_sizerButton
->Layout();
450 void ButtonWidgetsPage::AddButtonToSizer()
452 if ( m_chkFit
->GetValue() )
454 m_sizerButton
->AddStretchSpacer(1);
455 m_sizerButton
->Add(m_button
, wxSizerFlags(0).Centre().Border());
456 m_sizerButton
->AddStretchSpacer(1);
458 else // take up the entire space
460 m_sizerButton
->Add(m_button
, wxSizerFlags(1).Expand().Border());
464 // ----------------------------------------------------------------------------
466 // ----------------------------------------------------------------------------
468 void ButtonWidgetsPage::OnButtonReset(wxCommandEvent
& WXUNUSED(event
))
475 void ButtonWidgetsPage::OnCheckOrRadioBox(wxCommandEvent
& WXUNUSED(event
))
480 void ButtonWidgetsPage::OnButtonChangeLabel(wxCommandEvent
& WXUNUSED(event
))
482 m_button
->SetLabel(m_textLabel
->GetValue());
484 m_sizerButton
->Layout();
487 void ButtonWidgetsPage::OnButton(wxCommandEvent
& WXUNUSED(event
))
489 wxLogMessage(wxT("Test button clicked."));
492 // ----------------------------------------------------------------------------
493 // bitmap button stuff
494 // ----------------------------------------------------------------------------
496 wxBitmap
ButtonWidgetsPage::CreateBitmap(const wxString
& label
)
498 wxBitmap
bmp(180, 70); // shouldn't hardcode but it's simpler like this
500 dc
.SelectObject(bmp
);
501 dc
.SetBackground(wxBrush(*wxCYAN
));
503 dc
.SetTextForeground(*wxBLACK
);
504 dc
.DrawLabel(wxStripMenuCodes(m_textLabel
->GetValue()) + wxT("\n")
505 wxT("(") + label
+ wxT(" state)"),
506 wxArtProvider::GetBitmap(wxART_INFORMATION
),
507 wxRect(10, 10, bmp
.GetWidth() - 20, bmp
.GetHeight() - 20),