1 /////////////////////////////////////////////////////////////////////////////
2 // Program: wxWindows 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
31 #include "wx/button.h"
32 #include "wx/checkbox.h"
33 #include "wx/radiobox.h"
34 #include "wx/statbox.h"
35 #include "wx/textctrl.h"
42 #include "icons/button.xpm"
44 // ----------------------------------------------------------------------------
46 // ----------------------------------------------------------------------------
51 ButtonPage_Reset
= 100,
52 ButtonPage_ChangeLabel
,
71 // ----------------------------------------------------------------------------
73 // ----------------------------------------------------------------------------
75 class ButtonWidgetsPage
: public WidgetsPage
78 ButtonWidgetsPage(wxNotebook
*notebook
, wxImageList
*imaglist
);
79 virtual ~ButtonWidgetsPage();
83 void OnCheckOrRadioBox(wxCommandEvent
& event
);
85 void OnButton(wxCommandEvent
& event
);
86 void OnButtonReset(wxCommandEvent
& event
);
87 void OnButtonChangeLabel(wxCommandEvent
& event
);
89 // reset the wxButton parameters
92 // (re)create the wxButton
98 // the check/radio boxes for styles
99 wxCheckBox
*m_chkImage
,
103 wxRadioBox
*m_radioHAlign
,
106 // the gauge itself and the sizer it is in
108 wxSizer
*m_sizerButton
;
110 // the text entries for command parameters
111 wxTextCtrl
*m_textLabel
;
114 DECLARE_EVENT_TABLE();
115 DECLARE_WIDGETS_PAGE(ButtonWidgetsPage
);
118 // ----------------------------------------------------------------------------
120 // ----------------------------------------------------------------------------
122 BEGIN_EVENT_TABLE(ButtonWidgetsPage
, WidgetsPage
)
123 EVT_BUTTON(ButtonPage_Button
, ButtonWidgetsPage::OnButton
)
125 EVT_BUTTON(ButtonPage_Reset
, ButtonWidgetsPage::OnButtonReset
)
126 EVT_BUTTON(ButtonPage_ChangeLabel
, ButtonWidgetsPage::OnButtonChangeLabel
)
128 EVT_CHECKBOX(-1, ButtonWidgetsPage::OnCheckOrRadioBox
)
129 EVT_RADIOBOX(-1, ButtonWidgetsPage::OnCheckOrRadioBox
)
132 // ============================================================================
134 // ============================================================================
136 IMPLEMENT_WIDGETS_PAGE(ButtonWidgetsPage
, _T("Button"));
138 ButtonWidgetsPage::ButtonWidgetsPage(wxNotebook
*notebook
,
139 wxImageList
*imaglist
)
140 : WidgetsPage(notebook
)
142 imaglist
->Add(wxBitmap(button_xpm
));
147 m_chkDefault
= (wxCheckBox
*)NULL
;
150 m_radioVAlign
= (wxRadioBox
*)NULL
;
152 m_textLabel
= (wxTextCtrl
*)NULL
;
154 m_button
= (wxButton
*)NULL
;
155 m_sizerButton
= (wxSizer
*)NULL
;
157 wxSizer
*sizerTop
= new wxBoxSizer(wxHORIZONTAL
);
160 wxStaticBox
*box
= new wxStaticBox(this, -1, _T("&Set style"));
162 wxSizer
*sizerLeft
= new wxStaticBoxSizer(box
, wxVERTICAL
);
164 m_chkImage
= CreateCheckBoxAndAddToSizer(sizerLeft
, _T("With &image"));
165 m_chkFit
= CreateCheckBoxAndAddToSizer(sizerLeft
, _T("&Fit exactly"));
166 m_chkDefault
= CreateCheckBoxAndAddToSizer(sizerLeft
, _T("&Default"));
168 #ifndef __WXUNIVERSAL__
169 // only wxUniv currently supoprts buttons with images
170 m_chkImage
->Disable();
173 sizerLeft
->Add(5, 5, 0, wxGROW
| wxALL
, 5); // spacer
175 // should be in sync with enums Button[HV]Align!
176 static const wxString halign
[] =
183 static const wxString valign
[] =
190 m_radioHAlign
= new wxRadioBox(this, -1, _T("&Horz alignment"),
191 wxDefaultPosition
, wxDefaultSize
,
192 WXSIZEOF(halign
), halign
);
193 m_radioVAlign
= new wxRadioBox(this, -1, _T("&Vert alignment"),
194 wxDefaultPosition
, wxDefaultSize
,
195 WXSIZEOF(valign
), valign
);
197 sizerLeft
->Add(m_radioHAlign
, 0, wxGROW
| wxALL
, 5);
198 sizerLeft
->Add(m_radioVAlign
, 0, wxGROW
| wxALL
, 5);
200 sizerLeft
->Add(5, 5, 0, wxGROW
| wxALL
, 5); // spacer
202 wxButton
*btn
= new wxButton(this, ButtonPage_Reset
, _T("&Reset"));
203 sizerLeft
->Add(btn
, 0, wxALIGN_CENTRE_HORIZONTAL
| wxALL
, 15);
206 wxStaticBox
*box2
= new wxStaticBox(this, -1, _T("&Operations"));
207 wxSizer
*sizerMiddle
= new wxStaticBoxSizer(box2
, wxVERTICAL
);
209 wxSizer
*sizerRow
= CreateSizerWithTextAndButton(ButtonPage_ChangeLabel
,
214 sizerMiddle
->Add(sizerRow
, 0, wxALL
| wxGROW
, 5);
217 wxSizer
*sizerRight
= new wxBoxSizer(wxHORIZONTAL
);
218 m_button
= new wxButton(this, ButtonPage_Button
, _T("&Press me!"));
219 sizerRight
->Add(0, 0, 1, wxCENTRE
);
220 sizerRight
->Add(m_button
, 1, wxCENTRE
);
221 sizerRight
->Add(0, 0, 1, wxCENTRE
);
222 sizerRight
->SetMinSize(250, 0);
223 m_sizerButton
= sizerRight
; // save it to modify it later
225 // the 3 panes panes compose the window
226 sizerTop
->Add(sizerLeft
, 0, wxGROW
| (wxALL
& ~wxLEFT
), 10);
227 sizerTop
->Add(sizerMiddle
, 1, wxGROW
| wxALL
, 10);
228 sizerTop
->Add(sizerRight
, 1, wxGROW
| (wxALL
& ~wxRIGHT
), 10);
230 // final initializations
239 ButtonWidgetsPage::~ButtonWidgetsPage()
243 // ----------------------------------------------------------------------------
245 // ----------------------------------------------------------------------------
247 void ButtonWidgetsPage::Reset()
249 m_chkFit
->SetValue(TRUE
);
250 m_chkImage
->SetValue(FALSE
);
251 m_chkDefault
->SetValue(FALSE
);
253 m_radioHAlign
->SetSelection(ButtonHAlign_Centre
);
254 m_radioVAlign
->SetSelection(ButtonVAlign_Centre
);
257 void ButtonWidgetsPage::CreateButton()
262 label
= m_button
->GetLabel();
264 size_t count
= m_sizerButton
->GetChildren().GetCount();
265 for ( size_t n
= 0; n
< count
; n
++ )
267 m_sizerButton
->Remove(0);
274 label
= _T("&Press me!");
278 switch ( m_radioHAlign
->GetSelection() )
280 case ButtonHAlign_Left
:
281 flags
|= wxALIGN_LEFT
;
285 wxFAIL_MSG(_T("unexpected radiobox selection"));
288 case ButtonHAlign_Centre
:
289 flags
|= wxALIGN_CENTRE_HORIZONTAL
;
292 case ButtonHAlign_Right
:
293 flags
|= wxALIGN_RIGHT
;
297 switch ( m_radioVAlign
->GetSelection() )
299 case ButtonVAlign_Top
:
300 flags
|= wxALIGN_TOP
;
304 wxFAIL_MSG(_T("unexpected radiobox selection"));
307 case ButtonVAlign_Centre
:
308 flags
|= wxALIGN_CENTRE_VERTICAL
;
311 case ButtonVAlign_Bottom
:
312 flags
|= wxALIGN_BOTTOM
;
316 m_button
= new wxButton(this, ButtonPage_Button
, label
,
317 wxDefaultPosition
, wxDefaultSize
,
320 #ifdef __WXUNIVERSAL__
321 if ( m_chkImage
->GetValue() )
323 m_button
->SetImageLabel(wxTheApp
->GetStdIcon(wxICON_INFORMATION
));
327 if ( m_chkDefault
->GetValue() )
329 m_button
->SetDefault();
332 if ( m_chkFit
->GetValue() )
334 m_sizerButton
->Add(0, 0, 1, wxCENTRE
);
335 m_sizerButton
->Add(m_button
, 1, wxCENTRE
);
336 m_sizerButton
->Add(0, 0, 1, wxCENTRE
);
340 m_sizerButton
->Add(m_button
, 1, wxGROW
| wxALL
, 5);
343 m_sizerButton
->Layout();
346 // ----------------------------------------------------------------------------
348 // ----------------------------------------------------------------------------
350 void ButtonWidgetsPage::OnButtonReset(wxCommandEvent
& WXUNUSED(event
))
357 void ButtonWidgetsPage::OnCheckOrRadioBox(wxCommandEvent
& event
)
362 void ButtonWidgetsPage::OnButtonChangeLabel(wxCommandEvent
& WXUNUSED(event
))
364 m_button
->SetLabel(m_textLabel
->GetValue());
367 void ButtonWidgetsPage::OnButton(wxCommandEvent
& event
)
369 wxLogMessage(_T("Test button clicked."));