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