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