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