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