]> git.saurik.com Git - wxWidgets.git/blob - samples/widgets/button.cpp
Do a little less anti-alias so it looks better on dark backgrounds
[wxWidgets.git] / samples / widgets / button.cpp
1 /////////////////////////////////////////////////////////////////////////////
2 // Program: wxWidgets Widgets Sample
3 // Name: button.cpp
4 // Purpose: Part of the widgets sample showing wxButton
5 // Author: Vadim Zeitlin
6 // Created: 10.04.01
7 // Id: $Id$
8 // Copyright: (c) 2001 Vadim Zeitlin
9 // License: wxWindows license
10 /////////////////////////////////////////////////////////////////////////////
11
12 // ============================================================================
13 // declarations
14 // ============================================================================
15
16 // ----------------------------------------------------------------------------
17 // headers
18 // ----------------------------------------------------------------------------
19
20 // for compilers that support precompilation, includes "wx/wx.h".
21 #include "wx/wxprec.h"
22
23 #ifdef __BORLANDC__
24 #pragma hdrstop
25 #endif
26
27 // for all others, include the necessary headers
28 #ifndef WX_PRECOMP
29 #include "wx/app.h"
30 #include "wx/log.h"
31
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"
37 #endif
38
39 #include "wx/artprov.h"
40 #include "wx/sizer.h"
41
42 #include "widgets.h"
43
44 #include "icons/button.xpm"
45
46 // ----------------------------------------------------------------------------
47 // constants
48 // ----------------------------------------------------------------------------
49
50 // control ids
51 enum
52 {
53 ButtonPage_Reset = 100,
54 ButtonPage_ChangeLabel,
55 ButtonPage_Button
56 };
57
58 // radio boxes
59 enum
60 {
61 ButtonHAlign_Left,
62 ButtonHAlign_Centre,
63 ButtonHAlign_Right
64 };
65
66 enum
67 {
68 ButtonVAlign_Top,
69 ButtonVAlign_Centre,
70 ButtonVAlign_Bottom
71 };
72
73 // ----------------------------------------------------------------------------
74 // ButtonWidgetsPage
75 // ----------------------------------------------------------------------------
76
77 class ButtonWidgetsPage : public WidgetsPage
78 {
79 public:
80 ButtonWidgetsPage(wxBookCtrl *book, wxImageList *imaglist);
81 virtual ~ButtonWidgetsPage(){};
82
83 virtual wxControl *GetWidget() const { return m_button; }
84
85 protected:
86 // event handlers
87 void OnCheckOrRadioBox(wxCommandEvent& event);
88
89 void OnButton(wxCommandEvent& event);
90 void OnButtonReset(wxCommandEvent& event);
91 void OnButtonChangeLabel(wxCommandEvent& event);
92
93 // reset the wxButton parameters
94 void Reset();
95
96 // (re)create the wxButton
97 void CreateButton();
98
99 // the controls
100 // ------------
101
102 // the check/radio boxes for styles
103 wxCheckBox *m_chkImage,
104 *m_chkFit,
105 *m_chkDefault;
106
107 wxRadioBox *m_radioHAlign,
108 *m_radioVAlign;
109
110 // the button itself and the sizer it is in
111 wxButton *m_button;
112 wxSizer *m_sizerButton;
113
114 // the text entries for command parameters
115 wxTextCtrl *m_textLabel;
116
117 private:
118 DECLARE_EVENT_TABLE()
119 DECLARE_WIDGETS_PAGE(ButtonWidgetsPage)
120 };
121
122 // ----------------------------------------------------------------------------
123 // event tables
124 // ----------------------------------------------------------------------------
125
126 BEGIN_EVENT_TABLE(ButtonWidgetsPage, WidgetsPage)
127 EVT_BUTTON(ButtonPage_Button, ButtonWidgetsPage::OnButton)
128
129 EVT_BUTTON(ButtonPage_Reset, ButtonWidgetsPage::OnButtonReset)
130 EVT_BUTTON(ButtonPage_ChangeLabel, ButtonWidgetsPage::OnButtonChangeLabel)
131
132 EVT_CHECKBOX(wxID_ANY, ButtonWidgetsPage::OnCheckOrRadioBox)
133 EVT_RADIOBOX(wxID_ANY, ButtonWidgetsPage::OnCheckOrRadioBox)
134 END_EVENT_TABLE()
135
136 // ============================================================================
137 // implementation
138 // ============================================================================
139
140 IMPLEMENT_WIDGETS_PAGE(ButtonWidgetsPage, _T("Button"));
141
142 ButtonWidgetsPage::ButtonWidgetsPage(wxBookCtrl *book,
143 wxImageList *imaglist)
144 : WidgetsPage(book)
145 {
146 imaglist->Add(wxBitmap(button_xpm));
147
148 // init everything
149 m_chkImage =
150 m_chkFit =
151 m_chkDefault = (wxCheckBox *)NULL;
152
153 m_radioHAlign =
154 m_radioVAlign = (wxRadioBox *)NULL;
155
156 m_textLabel = (wxTextCtrl *)NULL;
157
158 m_button = (wxButton *)NULL;
159 m_sizerButton = (wxSizer *)NULL;
160
161 wxSizer *sizerTop = new wxBoxSizer(wxHORIZONTAL);
162
163 // left pane
164 wxStaticBox *box = new wxStaticBox(this, wxID_ANY, _T("&Set style"));
165
166 wxSizer *sizerLeft = new wxStaticBoxSizer(box, wxVERTICAL);
167
168 m_chkImage = CreateCheckBoxAndAddToSizer(sizerLeft, _T("With &image"));
169 m_chkFit = CreateCheckBoxAndAddToSizer(sizerLeft, _T("&Fit exactly"));
170 m_chkDefault = CreateCheckBoxAndAddToSizer(sizerLeft, _T("&Default"));
171
172 #ifndef __WXUNIVERSAL__
173 // only wxUniv currently supports buttons with images
174 m_chkImage->Disable();
175 #endif // !wxUniv
176
177 sizerLeft->Add(5, 5, 0, wxGROW | wxALL, 5); // spacer
178
179 // should be in sync with enums Button[HV]Align!
180 static const wxString halign[] =
181 {
182 _T("left"),
183 _T("centre"),
184 _T("right"),
185 };
186
187 static const wxString valign[] =
188 {
189 _T("top"),
190 _T("centre"),
191 _T("bottom"),
192 };
193
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);
200
201 sizerLeft->Add(m_radioHAlign, 0, wxGROW | wxALL, 5);
202 sizerLeft->Add(m_radioVAlign, 0, wxGROW | wxALL, 5);
203
204 sizerLeft->Add(5, 5, 0, wxGROW | wxALL, 5); // spacer
205
206 wxButton *btn = new wxButton(this, ButtonPage_Reset, _T("&Reset"));
207 sizerLeft->Add(btn, 0, wxALIGN_CENTRE_HORIZONTAL | wxALL, 15);
208
209 // middle pane
210 wxStaticBox *box2 = new wxStaticBox(this, wxID_ANY, _T("&Operations"));
211 wxSizer *sizerMiddle = new wxStaticBoxSizer(box2, wxVERTICAL);
212
213 wxSizer *sizerRow = CreateSizerWithTextAndButton(ButtonPage_ChangeLabel,
214 _T("Change label"),
215 wxID_ANY,
216 &m_textLabel);
217
218 sizerMiddle->Add(sizerRow, 0, wxALL | wxGROW, 5);
219
220 // right pane
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
228
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);
233
234 // final initializations
235 Reset();
236
237 SetSizer(sizerTop);
238
239 sizerTop->Fit(this);
240 }
241
242 // ----------------------------------------------------------------------------
243 // operations
244 // ----------------------------------------------------------------------------
245
246 void ButtonWidgetsPage::Reset()
247 {
248 m_chkFit->SetValue(true);
249 m_chkImage->SetValue(false);
250 m_chkDefault->SetValue(false);
251
252 m_radioHAlign->SetSelection(ButtonHAlign_Centre);
253 m_radioVAlign->SetSelection(ButtonVAlign_Centre);
254 }
255
256 void ButtonWidgetsPage::CreateButton()
257 {
258 wxString label;
259 if ( m_button )
260 {
261 label = m_button->GetLabel();
262
263 size_t count = m_sizerButton->GetChildren().GetCount();
264 for ( size_t n = 0; n < count; n++ )
265 {
266 m_sizerButton->Remove( 0 );
267 }
268
269 delete m_button;
270 }
271 else
272 {
273 label = _T("&Press me!");
274 }
275
276 int flags = 0;
277 switch ( m_radioHAlign->GetSelection() )
278 {
279 case ButtonHAlign_Left:
280 flags |= wxBU_LEFT;
281 break;
282
283 default:
284 wxFAIL_MSG(_T("unexpected radiobox selection"));
285 // fall through
286
287 case ButtonHAlign_Centre:
288 break;
289
290 case ButtonHAlign_Right:
291 flags |= wxBU_RIGHT;
292 break;
293 }
294
295 switch ( m_radioVAlign->GetSelection() )
296 {
297 case ButtonVAlign_Top:
298 flags |= wxBU_TOP;
299 break;
300
301 default:
302 wxFAIL_MSG(_T("unexpected radiobox selection"));
303 // fall through
304
305 case ButtonVAlign_Centre:
306 flags |= wxALIGN_CENTRE_VERTICAL;
307 break;
308
309 case ButtonVAlign_Bottom:
310 flags |= wxBU_BOTTOM;
311 break;
312 }
313
314 m_button = new wxButton(this, ButtonPage_Button, label,
315 wxDefaultPosition, wxDefaultSize,
316 flags);
317
318 #ifdef __WXUNIVERSAL__
319 if ( m_chkImage->GetValue() )
320 {
321 m_button->SetImageLabel(wxArtProvider::GetIcon(wxART_INFORMATION));
322 }
323 #endif // wxUniv
324
325 if ( m_chkDefault->GetValue() )
326 {
327 m_button->SetDefault();
328 }
329
330 if ( m_chkFit->GetValue() )
331 {
332 m_sizerButton->Add(0, 0, 1, wxCENTRE);
333 m_sizerButton->Add(m_button, 1, wxCENTRE);
334 m_sizerButton->Add(0, 0, 1, wxCENTRE);
335 }
336 else
337 {
338 m_sizerButton->Add(m_button, 1, wxGROW | wxALL, 5);
339 }
340
341 m_sizerButton->Layout();
342 }
343
344 // ----------------------------------------------------------------------------
345 // event handlers
346 // ----------------------------------------------------------------------------
347
348 void ButtonWidgetsPage::OnButtonReset(wxCommandEvent& WXUNUSED(event))
349 {
350 Reset();
351
352 CreateButton();
353 }
354
355 void ButtonWidgetsPage::OnCheckOrRadioBox(wxCommandEvent& WXUNUSED(event))
356 {
357 CreateButton();
358 }
359
360 void ButtonWidgetsPage::OnButtonChangeLabel(wxCommandEvent& WXUNUSED(event))
361 {
362 m_button->SetLabel(m_textLabel->GetValue());
363 }
364
365 void ButtonWidgetsPage::OnButton(wxCommandEvent& WXUNUSED(event))
366 {
367 wxLogMessage(_T("Test button clicked."));
368 }
369