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