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/button.h"
33 #include "wx/checkbox.h"
34 #include "wx/radiobox.h"
35 #include "wx/statbox.h"
36 #include "wx/textctrl.h"
39 #include "wx/artprov.h"
44 #include "icons/button.xpm"
46 // ----------------------------------------------------------------------------
48 // ----------------------------------------------------------------------------
53 ButtonPage_Reset
= 100,
54 ButtonPage_ChangeLabel
,
73 // ----------------------------------------------------------------------------
75 // ----------------------------------------------------------------------------
77 class ButtonWidgetsPage
: public WidgetsPage
80 ButtonWidgetsPage(wxBookCtrl
*book
, wxImageList
*imaglist
);
81 virtual ~ButtonWidgetsPage(){};
83 virtual wxControl
*GetWidget() const { return m_button
; }
87 void OnCheckOrRadioBox(wxCommandEvent
& event
);
89 void OnButton(wxCommandEvent
& event
);
90 void OnButtonReset(wxCommandEvent
& event
);
91 void OnButtonChangeLabel(wxCommandEvent
& event
);
93 // reset the wxButton parameters
96 // (re)create the wxButton
102 // the check/radio boxes for styles
103 wxCheckBox
*m_chkImage
,
107 wxRadioBox
*m_radioHAlign
,
110 // the button itself and the sizer it is in
112 wxSizer
*m_sizerButton
;
114 // the text entries for command parameters
115 wxTextCtrl
*m_textLabel
;
118 DECLARE_EVENT_TABLE()
119 DECLARE_WIDGETS_PAGE(ButtonWidgetsPage
)
122 // ----------------------------------------------------------------------------
124 // ----------------------------------------------------------------------------
126 BEGIN_EVENT_TABLE(ButtonWidgetsPage
, WidgetsPage
)
127 EVT_BUTTON(ButtonPage_Button
, ButtonWidgetsPage::OnButton
)
129 EVT_BUTTON(ButtonPage_Reset
, ButtonWidgetsPage::OnButtonReset
)
130 EVT_BUTTON(ButtonPage_ChangeLabel
, ButtonWidgetsPage::OnButtonChangeLabel
)
132 EVT_CHECKBOX(wxID_ANY
, ButtonWidgetsPage::OnCheckOrRadioBox
)
133 EVT_RADIOBOX(wxID_ANY
, ButtonWidgetsPage::OnCheckOrRadioBox
)
136 // ============================================================================
138 // ============================================================================
140 IMPLEMENT_WIDGETS_PAGE(ButtonWidgetsPage
, _T("Button"));
142 ButtonWidgetsPage::ButtonWidgetsPage(wxBookCtrl
*book
,
143 wxImageList
*imaglist
)
146 imaglist
->Add(wxBitmap(button_xpm
));
151 m_chkDefault
= (wxCheckBox
*)NULL
;
154 m_radioVAlign
= (wxRadioBox
*)NULL
;
156 m_textLabel
= (wxTextCtrl
*)NULL
;
158 m_button
= (wxButton
*)NULL
;
159 m_sizerButton
= (wxSizer
*)NULL
;
161 wxSizer
*sizerTop
= new wxBoxSizer(wxHORIZONTAL
);
164 wxStaticBox
*box
= new wxStaticBox(this, wxID_ANY
, _T("&Set style"));
166 wxSizer
*sizerLeft
= new wxStaticBoxSizer(box
, wxVERTICAL
);
168 m_chkImage
= CreateCheckBoxAndAddToSizer(sizerLeft
, _T("With &image"));
169 m_chkFit
= CreateCheckBoxAndAddToSizer(sizerLeft
, _T("&Fit exactly"));
170 m_chkDefault
= CreateCheckBoxAndAddToSizer(sizerLeft
, _T("&Default"));
172 #ifndef __WXUNIVERSAL__
173 // only wxUniv currently supports buttons with images
174 m_chkImage
->Disable();
177 sizerLeft
->Add(5, 5, 0, wxGROW
| wxALL
, 5); // spacer
179 // should be in sync with enums Button[HV]Align!
180 static const wxString halign
[] =
187 static const wxString valign
[] =
194 m_radioHAlign
= new wxRadioBox(this, wxID_ANY
, _T("&Horz alignment"),
195 wxDefaultPosition
, wxDefaultSize
,
196 WXSIZEOF(halign
), halign
);
197 m_radioVAlign
= new wxRadioBox(this, wxID_ANY
, _T("&Vert alignment"),
198 wxDefaultPosition
, wxDefaultSize
,
199 WXSIZEOF(valign
), valign
);
201 sizerLeft
->Add(m_radioHAlign
, 0, wxGROW
| wxALL
, 5);
202 sizerLeft
->Add(m_radioVAlign
, 0, wxGROW
| wxALL
, 5);
204 sizerLeft
->Add(5, 5, 0, wxGROW
| wxALL
, 5); // spacer
206 wxButton
*btn
= new wxButton(this, ButtonPage_Reset
, _T("&Reset"));
207 sizerLeft
->Add(btn
, 0, wxALIGN_CENTRE_HORIZONTAL
| wxALL
, 15);
210 wxStaticBox
*box2
= new wxStaticBox(this, wxID_ANY
, _T("&Operations"));
211 wxSizer
*sizerMiddle
= new wxStaticBoxSizer(box2
, wxVERTICAL
);
213 wxSizer
*sizerRow
= CreateSizerWithTextAndButton(ButtonPage_ChangeLabel
,
218 sizerMiddle
->Add(sizerRow
, 0, wxALL
| wxGROW
, 5);
221 wxSizer
*sizerRight
= new wxBoxSizer(wxHORIZONTAL
);
222 m_button
= new wxButton(this, ButtonPage_Button
, _T("&Press me!"));
223 sizerRight
->Add(0, 0, 1, wxCENTRE
);
224 sizerRight
->Add(m_button
, 1, wxCENTRE
);
225 sizerRight
->Add(0, 0, 1, wxCENTRE
);
226 sizerRight
->SetMinSize(150, 0);
227 m_sizerButton
= sizerRight
; // save it to modify it later
229 // the 3 panes panes compose the window
230 sizerTop
->Add(sizerLeft
, 0, wxGROW
| (wxALL
& ~wxLEFT
), 10);
231 sizerTop
->Add(sizerMiddle
, 1, wxGROW
| wxALL
, 10);
232 sizerTop
->Add(sizerRight
, 1, wxGROW
| (wxALL
& ~wxRIGHT
), 10);
234 // final initializations
242 // ----------------------------------------------------------------------------
244 // ----------------------------------------------------------------------------
246 void ButtonWidgetsPage::Reset()
248 m_chkFit
->SetValue(true);
249 m_chkImage
->SetValue(false);
250 m_chkDefault
->SetValue(false);
252 m_radioHAlign
->SetSelection(ButtonHAlign_Centre
);
253 m_radioVAlign
->SetSelection(ButtonVAlign_Centre
);
256 void ButtonWidgetsPage::CreateButton()
261 label
= m_button
->GetLabel();
263 size_t count
= m_sizerButton
->GetChildren().GetCount();
264 for ( size_t n
= 0; n
< count
; n
++ )
266 m_sizerButton
->Remove( 0 );
273 label
= _T("&Press me!");
277 switch ( m_radioHAlign
->GetSelection() )
279 case ButtonHAlign_Left
:
284 wxFAIL_MSG(_T("unexpected radiobox selection"));
287 case ButtonHAlign_Centre
:
290 case ButtonHAlign_Right
:
295 switch ( m_radioVAlign
->GetSelection() )
297 case ButtonVAlign_Top
:
302 wxFAIL_MSG(_T("unexpected radiobox selection"));
305 case ButtonVAlign_Centre
:
306 flags
|= wxALIGN_CENTRE_VERTICAL
;
309 case ButtonVAlign_Bottom
:
310 flags
|= wxBU_BOTTOM
;
314 m_button
= new wxButton(this, ButtonPage_Button
, label
,
315 wxDefaultPosition
, wxDefaultSize
,
318 #ifdef __WXUNIVERSAL__
319 if ( m_chkImage
->GetValue() )
321 m_button
->SetImageLabel(wxArtProvider::GetIcon(wxART_INFORMATION
));
325 if ( m_chkDefault
->GetValue() )
327 m_button
->SetDefault();
330 if ( m_chkFit
->GetValue() )
332 m_sizerButton
->Add(0, 0, 1, wxCENTRE
);
333 m_sizerButton
->Add(m_button
, 1, wxCENTRE
);
334 m_sizerButton
->Add(0, 0, 1, wxCENTRE
);
338 m_sizerButton
->Add(m_button
, 1, wxGROW
| wxALL
, 5);
341 m_sizerButton
->Layout();
344 // ----------------------------------------------------------------------------
346 // ----------------------------------------------------------------------------
348 void ButtonWidgetsPage::OnButtonReset(wxCommandEvent
& WXUNUSED(event
))
355 void ButtonWidgetsPage::OnCheckOrRadioBox(wxCommandEvent
& WXUNUSED(event
))
360 void ButtonWidgetsPage::OnButtonChangeLabel(wxCommandEvent
& WXUNUSED(event
))
362 m_button
->SetLabel(m_textLabel
->GetValue());
365 void ButtonWidgetsPage::OnButton(wxCommandEvent
& WXUNUSED(event
))
367 wxLogMessage(_T("Test button clicked."));