]> git.saurik.com Git - wxWidgets.git/blob - samples/widgets/radiobox.cpp
Removed WXWIN_COMPATIBILITY_2_2 together with code guarded by it.
[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_Enable2nd,
60 RadioPage_Show2nd,
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 // ----------------------------------------------------------------------------
77 // RadioWidgetsPage
78 // ----------------------------------------------------------------------------
79
80 class RadioWidgetsPage : public WidgetsPage
81 {
82 public:
83 RadioWidgetsPage(wxBookCtrlBase *book, wxImageList *imaglist);
84 virtual ~RadioWidgetsPage(){};
85
86 virtual wxControl *GetWidget() const { return m_radio; }
87
88 protected:
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;
114 wxCheckBox *m_2ndEnabled;
115 wxCheckBox *m_2ndShown;
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
130 private:
131 DECLARE_EVENT_TABLE()
132 DECLARE_WIDGETS_PAGE(RadioWidgetsPage)
133 };
134
135 // ----------------------------------------------------------------------------
136 // event tables
137 // ----------------------------------------------------------------------------
138
139 BEGIN_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
153 EVT_CHECKBOX(wxID_ANY, RadioWidgetsPage::OnCheckOrRadioBox)
154 EVT_RADIOBOX(wxID_ANY, RadioWidgetsPage::OnCheckOrRadioBox)
155 END_EVENT_TABLE()
156
157 // ============================================================================
158 // implementation
159 // ============================================================================
160
161 IMPLEMENT_WIDGETS_PAGE(RadioWidgetsPage, _T("Radio"));
162
163 RadioWidgetsPage::RadioWidgetsPage(wxBookCtrlBase *book,
164 wxImageList *imaglist)
165 : WidgetsPage(book)
166 {
167 imaglist->Add(wxBitmap(radio_xpm));
168
169 // init everything
170 m_chkVert = (wxCheckBox *)NULL;
171 m_2ndEnabled = (wxCheckBox *)NULL;
172 m_2ndShown = (wxCheckBox *)NULL;
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
185 wxStaticBox *box = new wxStaticBox(this, wxID_ANY, _T("&Set style"));
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
198 m_radioDir = new wxRadioBox(this, wxID_ANY, _T("Numbering:"),
199 wxDefaultPosition, wxDefaultSize,
200 WXSIZEOF(layoutDir), layoutDir,
201 1, wxRA_SPECIFY_COLS);
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;
210 sizerRow = CreateSizerWithTextAndLabel(_T("&Major dimension:"),
211 wxID_ANY,
212 &m_textMajorDim);
213 sizerLeft->Add(sizerRow, 0, wxGROW | wxALL, 5);
214
215 sizerRow = CreateSizerWithTextAndLabel(_T("&Number of buttons:"),
216 wxID_ANY,
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
230 wxStaticBox *box2 = new wxStaticBox(this, wxID_ANY, _T("&Change parameters"));
231 wxSizer *sizerMiddle = new wxStaticBoxSizer(box2, wxVERTICAL);
232
233 sizerRow = CreateSizerWithTextAndLabel(_T("Current selection:"),
234 wxID_ANY,
235 &m_textCurSel);
236 sizerMiddle->Add(sizerRow, 0, wxGROW | wxALL, 5);
237
238 sizerRow = CreateSizerWithTextAndButton(RadioPage_Selection,
239 _T("&Change selection:"),
240 wxID_ANY,
241 &m_textSel);
242 sizerMiddle->Add(sizerRow, 0, wxGROW | wxALL, 5);
243
244 sizerRow = CreateSizerWithTextAndButton(RadioPage_Label,
245 _T("&Label for box:"),
246 wxID_ANY,
247 &m_textLabel);
248 sizerMiddle->Add(sizerRow, 0, wxGROW | wxALL, 5);
249
250 sizerRow = CreateSizerWithTextAndButton(RadioPage_LabelBtn,
251 _T("&Label for buttons:"),
252 wxID_ANY,
253 &m_textLabelBtns);
254 sizerMiddle->Add(sizerRow, 0, wxGROW | wxALL, 5);
255
256 m_2ndEnabled = CreateCheckBoxAndAddToSizer(sizerMiddle, _T("2nd item enabled"));
257 m_2ndShown = CreateCheckBoxAndAddToSizer(sizerMiddle, _T("2nd item shown"));
258
259 // right pane
260 wxSizer *sizerRight = new wxBoxSizer(wxHORIZONTAL);
261 sizerRight->SetMinSize(150, 0);
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);
270 sizerTop->Add(sizerRight, 0, wxGROW | (wxALL & ~wxRIGHT), 10);
271
272 // final initializations
273 SetSizer(sizerTop);
274
275 sizerTop->Fit(this);
276 }
277
278 // ----------------------------------------------------------------------------
279 // operations
280 // ----------------------------------------------------------------------------
281
282 void RadioWidgetsPage::Reset()
283 {
284 m_textMajorDim->SetValue(wxString::Format(_T("%u"), DEFAULT_MAJOR_DIM));
285 m_textNumBtns->SetValue(wxString::Format(_T("%u"), DEFAULT_NUM_ENTRIES));
286 m_textLabel->SetValue(_T("I'm a radiobox"));
287 m_textLabelBtns->SetValue(_T("item"));
288
289 m_chkVert->SetValue(false);
290 m_2ndEnabled->SetValue(true);
291 m_2ndShown->SetValue(true);
292 m_radioDir->SetSelection(RadioDir_Default);
293 }
294
295 void RadioWidgetsPage::CreateRadio()
296 {
297 int sel;
298 if ( m_radio )
299 {
300 sel = m_radio->GetSelection();
301
302 m_sizerRadio->Detach( m_radio );
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 {
334 items[n] = wxString::Format(_T("%s %lu"),
335 labelBtn.c_str(), (unsigned long)n + 1);
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();
377
378 m_radio->Enable( 1 , m_2ndEnabled->GetValue() );
379 m_radio->Show( 1 , m_2ndShown->GetValue() );
380 }
381
382 // ----------------------------------------------------------------------------
383 // event handlers
384 // ----------------------------------------------------------------------------
385
386 void RadioWidgetsPage::OnButtonReset(wxCommandEvent& WXUNUSED(event))
387 {
388 Reset();
389
390 CreateRadio();
391 }
392
393 void RadioWidgetsPage::OnCheckOrRadioBox(wxCommandEvent& WXUNUSED(event))
394 {
395 CreateRadio();
396 }
397
398 void RadioWidgetsPage::OnRadioBox(wxCommandEvent& event)
399 {
400 int sel = m_radio->GetSelection();
401 int event_sel = event.GetSelection();
402 wxUnusedVar(event_sel);
403
404 wxLogMessage(_T("Radiobox selection changed, now %d"), sel);
405
406 wxASSERT_MSG( sel == event_sel,
407 _T("selection should be the same in event and radiobox") );
408
409 m_textCurSel->SetValue(wxString::Format(_T("%d"), sel));
410 }
411
412 void RadioWidgetsPage::OnButtonRecreate(wxCommandEvent& WXUNUSED(event))
413 {
414 CreateRadio();
415 }
416
417 void RadioWidgetsPage::OnButtonSetLabel(wxCommandEvent& WXUNUSED(event))
418 {
419 m_radio->wxControl::SetLabel(m_textLabel->GetValue());
420 }
421
422 void 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
436 void 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
443 void 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
450 void 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
474 #endif // wxUSE_RADIOBOX