Continuation of 'widgets' sample improvements.
[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 = wxID_HIGHEST,
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(WidgetsBookCtrl *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 #if defined(__WXUNIVERSAL__)
177 #define FAMILY_CTRLS UNIVERSAL_CTRLS
178 #else
179 #define FAMILY_CTRLS NATIVE_CTRLS
180 #endif
181
182 IMPLEMENT_WIDGETS_PAGE(RadioWidgetsPage, _T("Radio"),
183 FAMILY_CTRLS | WITH_ITEMS_CTRLS
184 );
185
186 RadioWidgetsPage::RadioWidgetsPage(WidgetsBookCtrl *book,
187 wxImageList *imaglist)
188 : WidgetsPage(book)
189 {
190 imaglist->Add(wxBitmap(radio_xpm));
191
192 // init everything
193 m_chkVert = (wxCheckBox *)NULL;
194 m_chkEnableItem = (wxCheckBox *)NULL;
195 m_chkShowItem = (wxCheckBox *)NULL;
196
197 m_textNumBtns =
198 m_textLabelBtns =
199 m_textLabel = (wxTextCtrl *)NULL;
200
201 m_radio =
202 m_radioDir = (wxRadioBox *)NULL;
203 m_sizerRadio = (wxSizer *)NULL;
204
205 wxSizer *sizerTop = new wxBoxSizer(wxHORIZONTAL);
206
207 // left pane
208 wxStaticBox *box = new wxStaticBox(this, wxID_ANY, _T("&Set style"));
209
210 wxSizer *sizerLeft = new wxStaticBoxSizer(box, wxVERTICAL);
211
212 m_chkVert = CreateCheckBoxAndAddToSizer(sizerLeft, _T("&Vertical layout"));
213
214 static const wxString layoutDir[] =
215 {
216 _T("default"),
217 _T("left to right"),
218 _T("top to bottom")
219 };
220
221 m_radioDir = new wxRadioBox(this, wxID_ANY, _T("Numbering:"),
222 wxDefaultPosition, wxDefaultSize,
223 WXSIZEOF(layoutDir), layoutDir,
224 1, wxRA_SPECIFY_COLS);
225 sizerLeft->Add(m_radioDir, 0, wxGROW | wxALL, 5);
226
227 // if it's not defined, we can't change the radiobox direction
228 #ifndef wxRA_LEFTTORIGHT
229 m_radioDir->Disable();
230 #endif // wxRA_LEFTTORIGHT
231
232 wxSizer *sizerRow;
233 sizerRow = CreateSizerWithTextAndLabel(_T("&Major dimension:"),
234 wxID_ANY,
235 &m_textMajorDim);
236 sizerLeft->Add(sizerRow, 0, wxGROW | wxALL, 5);
237
238 sizerRow = CreateSizerWithTextAndLabel(_T("&Number of buttons:"),
239 wxID_ANY,
240 &m_textNumBtns);
241 sizerLeft->Add(sizerRow, 0, wxGROW | wxALL, 5);
242
243 wxButton *btn;
244 btn = new wxButton(this, RadioPage_Update, _T("&Update"));
245 sizerLeft->Add(btn, 0, wxALIGN_CENTRE_HORIZONTAL | wxALL, 5);
246
247 sizerLeft->Add(5, 5, 0, wxGROW | wxALL, 5); // spacer
248
249 btn = new wxButton(this, RadioPage_Reset, _T("&Reset"));
250 sizerLeft->Add(btn, 0, wxALIGN_CENTRE_HORIZONTAL | wxALL, 15);
251
252 // middle pane
253 wxStaticBox *box2 = new wxStaticBox(this, wxID_ANY, _T("&Change parameters"));
254 wxSizer *sizerMiddle = new wxStaticBoxSizer(box2, wxVERTICAL);
255
256 sizerRow = CreateSizerWithTextAndLabel(_T("Current selection:"),
257 wxID_ANY,
258 &m_textCurSel);
259 sizerMiddle->Add(sizerRow, 0, wxGROW | wxALL, 5);
260
261 sizerRow = CreateSizerWithTextAndButton(RadioPage_Selection,
262 _T("&Change selection:"),
263 wxID_ANY,
264 &m_textSel);
265 sizerMiddle->Add(sizerRow, 0, wxGROW | wxALL, 5);
266
267 sizerRow = CreateSizerWithTextAndButton(RadioPage_Label,
268 _T("&Label for box:"),
269 wxID_ANY,
270 &m_textLabel);
271 sizerMiddle->Add(sizerRow, 0, wxGROW | wxALL, 5);
272
273 sizerRow = CreateSizerWithTextAndButton(RadioPage_LabelBtn,
274 _T("&Label for buttons:"),
275 wxID_ANY,
276 &m_textLabelBtns);
277 sizerMiddle->Add(sizerRow, 0, wxGROW | wxALL, 5);
278
279 m_chkEnableItem = CreateCheckBoxAndAddToSizer(sizerMiddle,
280 _T("Disable &2nd item"),
281 RadioPage_EnableItem);
282 m_chkShowItem = CreateCheckBoxAndAddToSizer(sizerMiddle,
283 _T("Hide 2nd &item"),
284 RadioPage_ShowItem);
285
286 // right pane
287 wxSizer *sizerRight = new wxBoxSizer(wxHORIZONTAL);
288 sizerRight->SetMinSize(150, 0);
289 m_sizerRadio = sizerRight; // save it to modify it later
290
291 Reset();
292 CreateRadio();
293
294 // the 3 panes panes compose the window
295 sizerTop->Add(sizerLeft, 0, wxGROW | (wxALL & ~wxLEFT), 10);
296 sizerTop->Add(sizerMiddle, 1, wxGROW | wxALL, 10);
297 sizerTop->Add(sizerRight, 0, wxGROW | (wxALL & ~wxRIGHT), 10);
298
299 // final initializations
300 SetSizer(sizerTop);
301
302 sizerTop->Fit(this);
303 }
304
305 // ----------------------------------------------------------------------------
306 // operations
307 // ----------------------------------------------------------------------------
308
309 void RadioWidgetsPage::Reset()
310 {
311 m_textMajorDim->SetValue(wxString::Format(_T("%u"), DEFAULT_MAJOR_DIM));
312 m_textNumBtns->SetValue(wxString::Format(_T("%u"), DEFAULT_NUM_ENTRIES));
313 m_textLabel->SetValue(_T("I'm a radiobox"));
314 m_textLabelBtns->SetValue(_T("item"));
315
316 m_chkVert->SetValue(false);
317 m_chkEnableItem->SetValue(true);
318 m_chkShowItem->SetValue(true);
319 m_radioDir->SetSelection(RadioDir_Default);
320 }
321
322 void RadioWidgetsPage::CreateRadio()
323 {
324 int sel;
325 if ( m_radio )
326 {
327 sel = m_radio->GetSelection();
328
329 m_sizerRadio->Detach( m_radio );
330
331 delete m_radio;
332 }
333 else // first time creation, no old selection to preserve
334 {
335 sel = -1;
336 }
337
338 unsigned long count;
339 if ( !m_textNumBtns->GetValue().ToULong(&count) )
340 {
341 wxLogWarning(_T("Should have a valid number for number of items."));
342
343 // fall back to default
344 count = DEFAULT_NUM_ENTRIES;
345 }
346
347 unsigned long majorDim;
348 if ( !m_textMajorDim->GetValue().ToULong(&majorDim) )
349 {
350 wxLogWarning(_T("Should have a valid major dimension number."));
351
352 // fall back to default
353 majorDim = DEFAULT_MAJOR_DIM;
354 }
355
356 wxString *items = new wxString[count];
357
358 wxString labelBtn = m_textLabelBtns->GetValue();
359 for ( size_t n = 0; n < count; n++ )
360 {
361 items[n] = wxString::Format(_T("%s %lu"),
362 labelBtn.c_str(), (unsigned long)n + 1);
363 }
364
365 int flags = m_chkVert->GetValue() ? wxRA_VERTICAL
366 : wxRA_HORIZONTAL;
367
368 flags |= ms_defaultFlags;
369
370 #ifdef wxRA_LEFTTORIGHT
371 switch ( m_radioDir->GetSelection() )
372 {
373 default:
374 wxFAIL_MSG( _T("unexpected wxRadioBox layout direction") );
375 // fall through
376
377 case RadioDir_Default:
378 break;
379
380 case RadioDir_LtoR:
381 flags |= wxRA_LEFTTORIGHT;
382 break;
383
384 case RadioDir_TtoB:
385 flags |= wxRA_TOPTOBOTTOM;
386 break;
387 }
388 #endif // wxRA_LEFTTORIGHT
389
390 m_radio = new wxRadioBox(this, RadioPage_Radio,
391 m_textLabel->GetValue(),
392 wxDefaultPosition, wxDefaultSize,
393 count, items,
394 majorDim,
395 flags);
396
397 delete [] items;
398
399 if ( sel >= 0 && (size_t)sel < count )
400 {
401 m_radio->SetSelection(sel);
402 }
403
404 m_sizerRadio->Add(m_radio, 1, wxGROW);
405 m_sizerRadio->Layout();
406
407 m_chkEnableItem->SetValue(true);
408 m_chkEnableItem->SetValue(true);
409 }
410
411 // ----------------------------------------------------------------------------
412 // event handlers
413 // ----------------------------------------------------------------------------
414
415 void RadioWidgetsPage::OnButtonReset(wxCommandEvent& WXUNUSED(event))
416 {
417 Reset();
418
419 CreateRadio();
420 }
421
422 void RadioWidgetsPage::OnCheckOrRadioBox(wxCommandEvent& WXUNUSED(event))
423 {
424 CreateRadio();
425 }
426
427 void RadioWidgetsPage::OnRadioBox(wxCommandEvent& event)
428 {
429 int sel = m_radio->GetSelection();
430 int event_sel = event.GetSelection();
431 wxUnusedVar(event_sel);
432
433 wxLogMessage(_T("Radiobox selection changed, now %d"), sel);
434
435 wxASSERT_MSG( sel == event_sel,
436 _T("selection should be the same in event and radiobox") );
437
438 m_textCurSel->SetValue(wxString::Format(_T("%d"), sel));
439 }
440
441 void RadioWidgetsPage::OnButtonRecreate(wxCommandEvent& WXUNUSED(event))
442 {
443 CreateRadio();
444 }
445
446 void RadioWidgetsPage::OnButtonSetLabel(wxCommandEvent& WXUNUSED(event))
447 {
448 m_radio->wxControl::SetLabel(m_textLabel->GetValue());
449 }
450
451 void RadioWidgetsPage::OnButtonSelection(wxCommandEvent& WXUNUSED(event))
452 {
453 unsigned long sel;
454 if ( !m_textSel->GetValue().ToULong(&sel) ||
455 (sel >= (size_t)m_radio->GetCount()) )
456 {
457 wxLogWarning(_T("Invalid number specified as new selection."));
458 }
459 else
460 {
461 m_radio->SetSelection(sel);
462 }
463 }
464
465 void RadioWidgetsPage::OnEnableItem(wxCommandEvent& event)
466 {
467 m_radio->Enable(TEST_BUTTON, event.IsChecked());
468 }
469
470 void RadioWidgetsPage::OnShowItem(wxCommandEvent& event)
471 {
472 m_radio->Show(TEST_BUTTON, event.IsChecked());
473 }
474
475 void RadioWidgetsPage::OnUpdateUIUpdate(wxUpdateUIEvent& event)
476 {
477 unsigned long n;
478 event.Enable( m_textNumBtns->GetValue().ToULong(&n) &&
479 m_textMajorDim->GetValue().ToULong(&n) );
480 }
481
482 void RadioWidgetsPage::OnUpdateUISelection(wxUpdateUIEvent& event)
483 {
484 unsigned long n;
485 event.Enable( m_textSel->GetValue().ToULong(&n) &&
486 (n < (size_t)m_radio->GetCount()) );
487 }
488
489 void RadioWidgetsPage::OnUpdateUIReset(wxUpdateUIEvent& event)
490 {
491 // only enable it if something is not set to default
492 bool enable = m_chkVert->GetValue();
493
494 if ( !enable )
495 {
496 unsigned long numEntries;
497
498 enable = !m_textNumBtns->GetValue().ToULong(&numEntries) ||
499 numEntries != DEFAULT_NUM_ENTRIES;
500
501 if ( !enable )
502 {
503 unsigned long majorDim;
504
505 enable = !m_textMajorDim->GetValue().ToULong(&majorDim) ||
506 majorDim != DEFAULT_MAJOR_DIM;
507 }
508 }
509
510 event.Enable(enable);
511 }
512
513 void RadioWidgetsPage::OnUpdateUIEnableItem(wxUpdateUIEvent& event)
514 {
515 event.SetText(m_radio->IsItemEnabled(TEST_BUTTON) ? _T("Disable &2nd item")
516 : _T("Enable &2nd item"));
517 }
518
519 void RadioWidgetsPage::OnUpdateUIShowItem(wxUpdateUIEvent& event)
520 {
521 event.SetText(m_radio->IsItemShown(TEST_BUTTON) ? _T("Hide 2nd &item")
522 : _T("Show 2nd &item"));
523 }
524
525 #endif // wxUSE_RADIOBOX