]> git.saurik.com Git - wxWidgets.git/blob - samples/widgets/radiobox.cpp
stop usign a deprecated method, and ensure that the imagelist
[wxWidgets.git] / samples / widgets / radiobox.cpp
1 /////////////////////////////////////////////////////////////////////////////
2 // Program: wxWindows Widgets Sample
3 // Name: radiobox.cpp
4 // Purpose: Part of the widgets sample showing wxRadioBox
5 // Author: Vadim Zeitlin
6 // Created: 15.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/log.h"
30
31 #include "wx/bitmap.h"
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/sizer.h"
40
41 #include "widgets.h"
42 #if 1
43 #include "icons/radiobox.xpm"
44
45 // ----------------------------------------------------------------------------
46 // constants
47 // ----------------------------------------------------------------------------
48
49 // control ids
50 enum
51 {
52 RadioPage_Reset = 100,
53 RadioPage_Update,
54 RadioPage_Selection,
55 RadioPage_Label,
56 RadioPage_LabelBtn,
57 RadioPage_Radio
58 };
59
60 // layout direction radiobox selections
61 enum
62 {
63 RadioDir_Default,
64 RadioDir_LtoR,
65 RadioDir_TtoB
66 };
67
68 // default values for the number of radiobox items
69 static const unsigned int DEFAULT_NUM_ENTRIES = 12;
70 static const unsigned int DEFAULT_MAJOR_DIM = 3;
71
72 // ----------------------------------------------------------------------------
73 // RadioWidgetsPage
74 // ----------------------------------------------------------------------------
75
76 class RadioWidgetsPage : public WidgetsPage
77 {
78 public:
79 RadioWidgetsPage(wxNotebook *notebook, wxImageList *imaglist);
80 virtual ~RadioWidgetsPage();
81
82 protected:
83 // event handlers
84 void OnCheckOrRadioBox(wxCommandEvent& event);
85 void OnRadioBox(wxCommandEvent& event);
86
87 void OnButtonReset(wxCommandEvent& event);
88 void OnButtonRecreate(wxCommandEvent& event);
89
90 void OnButtonSelection(wxCommandEvent& event);
91 void OnButtonSetLabel(wxCommandEvent& event);
92
93 void OnUpdateUIReset(wxUpdateUIEvent& event);
94 void OnUpdateUIUpdate(wxUpdateUIEvent& event);
95 void OnUpdateUISelection(wxUpdateUIEvent& event);
96
97 // reset the wxRadioBox parameters
98 void Reset();
99
100 // (re)create the wxRadioBox
101 void CreateRadio();
102
103 // the controls
104 // ------------
105
106 // the check/radio boxes for styles
107 wxCheckBox *m_chkVert;
108 wxRadioBox *m_radioDir;
109
110 // the gauge itself and the sizer it is in
111 wxRadioBox *m_radio;
112 wxSizer *m_sizerRadio;
113
114 // the text entries for command parameters
115 wxTextCtrl *m_textNumBtns,
116 *m_textMajorDim,
117 *m_textCurSel,
118 *m_textSel,
119 *m_textLabel,
120 *m_textLabelBtns;
121
122 private:
123 DECLARE_EVENT_TABLE()
124 DECLARE_WIDGETS_PAGE(RadioWidgetsPage)
125 };
126
127 // ----------------------------------------------------------------------------
128 // event tables
129 // ----------------------------------------------------------------------------
130
131 BEGIN_EVENT_TABLE(RadioWidgetsPage, WidgetsPage)
132 EVT_BUTTON(RadioPage_Reset, RadioWidgetsPage::OnButtonReset)
133
134 EVT_BUTTON(RadioPage_Update, RadioWidgetsPage::OnButtonRecreate)
135 EVT_BUTTON(RadioPage_LabelBtn, RadioWidgetsPage::OnButtonRecreate)
136
137 EVT_BUTTON(RadioPage_Selection, RadioWidgetsPage::OnButtonSelection)
138 EVT_BUTTON(RadioPage_Label, RadioWidgetsPage::OnButtonSetLabel)
139
140 EVT_UPDATE_UI(RadioPage_Update, RadioWidgetsPage::OnUpdateUIUpdate)
141 EVT_UPDATE_UI(RadioPage_Selection, RadioWidgetsPage::OnUpdateUISelection)
142
143 EVT_RADIOBOX(RadioPage_Radio, RadioWidgetsPage::OnRadioBox)
144
145 EVT_CHECKBOX(-1, RadioWidgetsPage::OnCheckOrRadioBox)
146 EVT_RADIOBOX(-1, RadioWidgetsPage::OnCheckOrRadioBox)
147 END_EVENT_TABLE()
148
149 // ============================================================================
150 // implementation
151 // ============================================================================
152
153 IMPLEMENT_WIDGETS_PAGE(RadioWidgetsPage, _T("Radio"));
154
155 RadioWidgetsPage::RadioWidgetsPage(wxNotebook *notebook,
156 wxImageList *imaglist)
157 : WidgetsPage(notebook)
158 {
159 imaglist->Add(wxBitmap(radio_xpm));
160
161 // init everything
162 m_chkVert = (wxCheckBox *)NULL;
163
164 m_textNumBtns =
165 m_textLabelBtns =
166 m_textLabel = (wxTextCtrl *)NULL;
167
168 m_radio =
169 m_radioDir = (wxRadioBox *)NULL;
170 m_sizerRadio = (wxSizer *)NULL;
171
172 wxSizer *sizerTop = new wxBoxSizer(wxHORIZONTAL);
173
174 // left pane
175 wxStaticBox *box = new wxStaticBox(this, -1, _T("&Set style"));
176
177 wxSizer *sizerLeft = new wxStaticBoxSizer(box, wxVERTICAL);
178
179 m_chkVert = CreateCheckBoxAndAddToSizer(sizerLeft, _T("&Vertical layout"));
180
181 static const wxString layoutDir[] =
182 {
183 _T("default"),
184 _T("left to right"),
185 _T("top to bottom")
186 };
187
188 m_radioDir = new wxRadioBox(this, -1, _T("Numbering:"),
189 wxDefaultPosition, wxDefaultSize,
190 WXSIZEOF(layoutDir), layoutDir,
191 1, wxRA_SPECIFY_COLS);
192 sizerLeft->Add(m_radioDir, 0, wxGROW | wxALL, 5);
193
194 // if it's not defined, we can't change the radiobox direction
195 #ifndef wxRA_LEFTTORIGHT
196 m_radioDir->Disable();
197 #endif // wxRA_LEFTTORIGHT
198
199 wxSizer *sizerRow;
200 sizerRow = CreateSizerWithTextAndLabel(_T("&Major dimension:"),
201 -1,
202 &m_textMajorDim);
203 sizerLeft->Add(sizerRow, 0, wxGROW | wxALL, 5);
204
205 sizerRow = CreateSizerWithTextAndLabel(_T("&Number of buttons:"),
206 -1,
207 &m_textNumBtns);
208 sizerLeft->Add(sizerRow, 0, wxGROW | wxALL, 5);
209
210 wxButton *btn;
211 btn = new wxButton(this, RadioPage_Update, _T("&Update"));
212 sizerLeft->Add(btn, 0, wxALIGN_CENTRE_HORIZONTAL | wxALL, 5);
213
214 sizerLeft->Add(5, 5, 0, wxGROW | wxALL, 5); // spacer
215
216 btn = new wxButton(this, RadioPage_Reset, _T("&Reset"));
217 sizerLeft->Add(btn, 0, wxALIGN_CENTRE_HORIZONTAL | wxALL, 15);
218
219 // middle pane
220 wxStaticBox *box2 = new wxStaticBox(this, -1, _T("&Change parameters"));
221 wxSizer *sizerMiddle = new wxStaticBoxSizer(box2, wxVERTICAL);
222
223 sizerRow = CreateSizerWithTextAndLabel(_T("Current selection:"),
224 -1,
225 &m_textCurSel);
226 sizerMiddle->Add(sizerRow, 0, wxGROW | wxALL, 5);
227
228 sizerRow = CreateSizerWithTextAndButton(RadioPage_Selection,
229 _T("&Change selection:"),
230 -1,
231 &m_textSel);
232 sizerMiddle->Add(sizerRow, 0, wxGROW | wxALL, 5);
233
234 sizerRow = CreateSizerWithTextAndButton(RadioPage_Label,
235 _T("&Label for box:"),
236 -1,
237 &m_textLabel);
238 sizerMiddle->Add(sizerRow, 0, wxGROW | wxALL, 5);
239
240 sizerRow = CreateSizerWithTextAndButton(RadioPage_LabelBtn,
241 _T("&Label for buttons:"),
242 -1,
243 &m_textLabelBtns);
244 sizerMiddle->Add(sizerRow, 0, wxGROW | wxALL, 5);
245
246 // right pane
247 wxSizer *sizerRight = new wxBoxSizer(wxHORIZONTAL);
248 sizerRight->SetMinSize(150, 0);
249 m_sizerRadio = sizerRight; // save it to modify it later
250
251 Reset();
252 CreateRadio();
253
254 // the 3 panes panes compose the window
255 sizerTop->Add(sizerLeft, 0, wxGROW | (wxALL & ~wxLEFT), 10);
256 sizerTop->Add(sizerMiddle, 1, wxGROW | wxALL, 10);
257 sizerTop->Add(sizerRight, 0, wxGROW | (wxALL & ~wxRIGHT), 10);
258
259 // final initializations
260 SetAutoLayout(TRUE);
261 SetSizer(sizerTop);
262
263 sizerTop->Fit(this);
264 }
265
266 RadioWidgetsPage::~RadioWidgetsPage()
267 {
268 }
269
270 // ----------------------------------------------------------------------------
271 // operations
272 // ----------------------------------------------------------------------------
273
274 void RadioWidgetsPage::Reset()
275 {
276 m_textMajorDim->SetValue(wxString::Format(_T("%u"), DEFAULT_MAJOR_DIM));
277 m_textNumBtns->SetValue(wxString::Format(_T("%u"), DEFAULT_NUM_ENTRIES));
278 m_textLabel->SetValue(_T("I'm a radiobox"));
279 m_textLabelBtns->SetValue(_T("item"));
280
281 m_chkVert->SetValue(FALSE);
282 m_radioDir->SetSelection(RadioDir_Default);
283 }
284
285 void RadioWidgetsPage::CreateRadio()
286 {
287 int sel;
288 if ( m_radio )
289 {
290 sel = m_radio->GetSelection();
291
292 m_sizerRadio->Detach( m_radio );
293
294 delete m_radio;
295 }
296 else // first time creation, no old selection to preserve
297 {
298 sel = -1;
299 }
300
301 unsigned long count;
302 if ( !m_textNumBtns->GetValue().ToULong(&count) )
303 {
304 wxLogWarning(_T("Should have a valid number for number of items."));
305
306 // fall back to default
307 count = DEFAULT_NUM_ENTRIES;
308 }
309
310 unsigned long majorDim;
311 if ( !m_textMajorDim->GetValue().ToULong(&majorDim) )
312 {
313 wxLogWarning(_T("Should have a valid major dimension number."));
314
315 // fall back to default
316 majorDim = DEFAULT_MAJOR_DIM;
317 }
318
319 wxString *items = new wxString[count];
320
321 wxString labelBtn = m_textLabelBtns->GetValue();
322 for ( size_t n = 0; n < count; n++ )
323 {
324 items[n] = wxString::Format(_T("%s %lu"),
325 labelBtn.c_str(), (unsigned long)n + 1);
326 }
327
328 int flags = m_chkVert->GetValue() ? wxRA_VERTICAL
329 : wxRA_HORIZONTAL;
330
331 #ifdef wxRA_LEFTTORIGHT
332 switch ( m_radioDir->GetSelection() )
333 {
334 default:
335 wxFAIL_MSG( _T("unexpected wxRadioBox layout direction") );
336 // fall through
337
338 case RadioDir_Default:
339 break;
340
341 case RadioDir_LtoR:
342 flags |= wxRA_LEFTTORIGHT;
343 break;
344
345 case RadioDir_TtoB:
346 flags |= wxRA_TOPTOBOTTOM;
347 break;
348 }
349 #endif // wxRA_LEFTTORIGHT
350
351 m_radio = new wxRadioBox(this, RadioPage_Radio,
352 m_textLabel->GetValue(),
353 wxDefaultPosition, wxDefaultSize,
354 count, items,
355 majorDim,
356 flags);
357
358 delete [] items;
359
360 if ( sel >= 0 && (size_t)sel < count )
361 {
362 m_radio->SetSelection(sel);
363 }
364
365 m_sizerRadio->Add(m_radio, 1, wxGROW);
366 m_sizerRadio->Layout();
367 }
368
369 // ----------------------------------------------------------------------------
370 // event handlers
371 // ----------------------------------------------------------------------------
372
373 void RadioWidgetsPage::OnButtonReset(wxCommandEvent& WXUNUSED(event))
374 {
375 Reset();
376
377 CreateRadio();
378 }
379
380 void RadioWidgetsPage::OnCheckOrRadioBox(wxCommandEvent& event)
381 {
382 CreateRadio();
383 }
384
385 void RadioWidgetsPage::OnRadioBox(wxCommandEvent& event)
386 {
387 int sel = m_radio->GetSelection();
388
389 wxLogMessage(_T("Radiobox selection changed, now %d"), sel);
390
391 wxASSERT_MSG( sel == event.GetSelection(),
392 _T("selection should be the same in event and radiobox") );
393
394 m_textCurSel->SetValue(wxString::Format(_T("%d"), sel));
395 }
396
397 void RadioWidgetsPage::OnButtonRecreate(wxCommandEvent& WXUNUSED(event))
398 {
399 CreateRadio();
400 }
401
402 void RadioWidgetsPage::OnButtonSetLabel(wxCommandEvent& WXUNUSED(event))
403 {
404 m_radio->wxControl::SetLabel(m_textLabel->GetValue());
405 }
406
407 void RadioWidgetsPage::OnButtonSelection(wxCommandEvent& WXUNUSED(event))
408 {
409 unsigned long sel;
410 if ( !m_textSel->GetValue().ToULong(&sel) ||
411 (sel >= (size_t)m_radio->GetCount()) )
412 {
413 wxLogWarning(_T("Invalid number specified as new selection."));
414 }
415 else
416 {
417 m_radio->SetSelection(sel);
418 }
419 }
420
421 void RadioWidgetsPage::OnUpdateUIUpdate(wxUpdateUIEvent& event)
422 {
423 unsigned long n;
424 event.Enable( m_textNumBtns->GetValue().ToULong(&n) &&
425 m_textMajorDim->GetValue().ToULong(&n) );
426 }
427
428 void RadioWidgetsPage::OnUpdateUISelection(wxUpdateUIEvent& event)
429 {
430 unsigned long n;
431 event.Enable( m_textSel->GetValue().ToULong(&n) &&
432 (n < (size_t)m_radio->GetCount()) );
433 }
434
435 void RadioWidgetsPage::OnUpdateUIReset(wxUpdateUIEvent& event)
436 {
437 // only enable it if something is not set to default
438 bool enable = m_chkVert->GetValue();
439
440 if ( !enable )
441 {
442 unsigned long numEntries;
443
444 enable = !m_textNumBtns->GetValue().ToULong(&numEntries) ||
445 numEntries != DEFAULT_NUM_ENTRIES;
446
447 if ( !enable )
448 {
449 unsigned long majorDim;
450
451 enable = !m_textMajorDim->GetValue().ToULong(&majorDim) ||
452 majorDim != DEFAULT_MAJOR_DIM;
453 }
454 }
455
456 event.Enable(enable);
457 }
458
459 #endif