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