]> git.saurik.com Git - wxWidgets.git/blame - samples/widgets/radiobox.cpp
updated VC build instructions
[wxWidgets.git] / samples / widgets / radiobox.cpp
CommitLineData
32b8ec41
VZ
1/////////////////////////////////////////////////////////////////////////////
2// Program: wxWindows 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// for all others, include the necessary headers
28#ifndef WX_PRECOMP
29 #include "wx/log.h"
30
3bb70c40 31 #include "wx/bitmap.h"
32b8ec41
VZ
32 #include "wx/button.h"
33 #include "wx/checkbox.h"
34 #include "wx/radiobox.h"
35 #include "wx/statbox.h"
36 #include "wx/textctrl.h"
37#endif
38
39#include "wx/sizer.h"
40
41#include "widgets.h"
7b127900 42#if 1
32b8ec41
VZ
43#include "icons/radiobox.xpm"
44
45// ----------------------------------------------------------------------------
46// constants
47// ----------------------------------------------------------------------------
48
49// control ids
50enum
51{
52 RadioPage_Reset = 100,
53 RadioPage_Update,
54 RadioPage_Selection,
55 RadioPage_Label,
56 RadioPage_LabelBtn,
57 RadioPage_Radio
58};
59
60// layout direction radiobox selections
61enum
62{
63 RadioDir_Default,
64 RadioDir_LtoR,
65 RadioDir_TtoB
66};
67
68// default values for the number of radiobox items
0c61716c
VZ
69static const unsigned int DEFAULT_NUM_ENTRIES = 12;
70static const unsigned int DEFAULT_MAJOR_DIM = 3;
32b8ec41
VZ
71
72// ----------------------------------------------------------------------------
73// RadioWidgetsPage
74// ----------------------------------------------------------------------------
75
76class RadioWidgetsPage : public WidgetsPage
77{
78public:
79 RadioWidgetsPage(wxNotebook *notebook, wxImageList *imaglist);
80 virtual ~RadioWidgetsPage();
81
82protected:
83 // event handlers
84 void OnCheckOrRadioBox(wxCommandEvent& event);
85 void OnRadioBox(wxCommandEvent& event);
86
87 void OnButtonReset(wxCommandEvent& event);
88 void OnButtonRecreate(wxCommandEvent& event);
89
90 void OnButtonSelection(wxCommandEvent& event);
91 void OnButtonSetLabel(wxCommandEvent& event);
92
93 void OnUpdateUIReset(wxUpdateUIEvent& event);
94 void OnUpdateUIUpdate(wxUpdateUIEvent& event);
95 void OnUpdateUISelection(wxUpdateUIEvent& event);
96
97 // reset the wxRadioBox parameters
98 void Reset();
99
100 // (re)create the wxRadioBox
101 void CreateRadio();
102
103 // the controls
104 // ------------
105
106 // the check/radio boxes for styles
107 wxCheckBox *m_chkVert;
108 wxRadioBox *m_radioDir;
109
110 // the gauge itself and the sizer it is in
111 wxRadioBox *m_radio;
112 wxSizer *m_sizerRadio;
113
114 // the text entries for command parameters
115 wxTextCtrl *m_textNumBtns,
116 *m_textMajorDim,
117 *m_textCurSel,
118 *m_textSel,
119 *m_textLabel,
120 *m_textLabelBtns;
121
122private:
5e173f35
GD
123 DECLARE_EVENT_TABLE()
124 DECLARE_WIDGETS_PAGE(RadioWidgetsPage)
32b8ec41
VZ
125};
126
127// ----------------------------------------------------------------------------
128// event tables
129// ----------------------------------------------------------------------------
130
131BEGIN_EVENT_TABLE(RadioWidgetsPage, WidgetsPage)
132 EVT_BUTTON(RadioPage_Reset, RadioWidgetsPage::OnButtonReset)
133
134 EVT_BUTTON(RadioPage_Update, RadioWidgetsPage::OnButtonRecreate)
135 EVT_BUTTON(RadioPage_LabelBtn, RadioWidgetsPage::OnButtonRecreate)
136
137 EVT_BUTTON(RadioPage_Selection, RadioWidgetsPage::OnButtonSelection)
138 EVT_BUTTON(RadioPage_Label, RadioWidgetsPage::OnButtonSetLabel)
139
140 EVT_UPDATE_UI(RadioPage_Update, RadioWidgetsPage::OnUpdateUIUpdate)
141 EVT_UPDATE_UI(RadioPage_Selection, RadioWidgetsPage::OnUpdateUISelection)
142
143 EVT_RADIOBOX(RadioPage_Radio, RadioWidgetsPage::OnRadioBox)
144
206d3a16
JS
145 EVT_CHECKBOX(wxID_ANY, RadioWidgetsPage::OnCheckOrRadioBox)
146 EVT_RADIOBOX(wxID_ANY, RadioWidgetsPage::OnCheckOrRadioBox)
32b8ec41
VZ
147END_EVENT_TABLE()
148
149// ============================================================================
150// implementation
151// ============================================================================
152
153IMPLEMENT_WIDGETS_PAGE(RadioWidgetsPage, _T("Radio"));
154
155RadioWidgetsPage::RadioWidgetsPage(wxNotebook *notebook,
156 wxImageList *imaglist)
157 : WidgetsPage(notebook)
158{
159 imaglist->Add(wxBitmap(radio_xpm));
160
161 // init everything
162 m_chkVert = (wxCheckBox *)NULL;
163
164 m_textNumBtns =
165 m_textLabelBtns =
166 m_textLabel = (wxTextCtrl *)NULL;
167
168 m_radio =
169 m_radioDir = (wxRadioBox *)NULL;
170 m_sizerRadio = (wxSizer *)NULL;
171
172 wxSizer *sizerTop = new wxBoxSizer(wxHORIZONTAL);
173
174 // left pane
206d3a16 175 wxStaticBox *box = new wxStaticBox(this, wxID_ANY, _T("&Set style"));
32b8ec41
VZ
176
177 wxSizer *sizerLeft = new wxStaticBoxSizer(box, wxVERTICAL);
178
179 m_chkVert = CreateCheckBoxAndAddToSizer(sizerLeft, _T("&Vertical layout"));
180
181 static const wxString layoutDir[] =
182 {
183 _T("default"),
184 _T("left to right"),
185 _T("top to bottom")
186 };
187
206d3a16 188 m_radioDir = new wxRadioBox(this, wxID_ANY, _T("Numbering:"),
32b8ec41 189 wxDefaultPosition, wxDefaultSize,
7b127900
JS
190 WXSIZEOF(layoutDir), layoutDir,
191 1, wxRA_SPECIFY_COLS);
32b8ec41
VZ
192 sizerLeft->Add(m_radioDir, 0, wxGROW | wxALL, 5);
193
194 // if it's not defined, we can't change the radiobox direction
195#ifndef wxRA_LEFTTORIGHT
196 m_radioDir->Disable();
197#endif // wxRA_LEFTTORIGHT
198
199 wxSizer *sizerRow;
7b127900 200 sizerRow = CreateSizerWithTextAndLabel(_T("&Major dimension:"),
206d3a16 201 wxID_ANY,
32b8ec41
VZ
202 &m_textMajorDim);
203 sizerLeft->Add(sizerRow, 0, wxGROW | wxALL, 5);
204
7b127900 205 sizerRow = CreateSizerWithTextAndLabel(_T("&Number of buttons:"),
206d3a16 206 wxID_ANY,
32b8ec41
VZ
207 &m_textNumBtns);
208 sizerLeft->Add(sizerRow, 0, wxGROW | wxALL, 5);
209
210 wxButton *btn;
211 btn = new wxButton(this, RadioPage_Update, _T("&Update"));
212 sizerLeft->Add(btn, 0, wxALIGN_CENTRE_HORIZONTAL | wxALL, 5);
213
214 sizerLeft->Add(5, 5, 0, wxGROW | wxALL, 5); // spacer
215
216 btn = new wxButton(this, RadioPage_Reset, _T("&Reset"));
217 sizerLeft->Add(btn, 0, wxALIGN_CENTRE_HORIZONTAL | wxALL, 15);
218
219 // middle pane
206d3a16 220 wxStaticBox *box2 = new wxStaticBox(this, wxID_ANY, _T("&Change parameters"));
32b8ec41
VZ
221 wxSizer *sizerMiddle = new wxStaticBoxSizer(box2, wxVERTICAL);
222
7b127900 223 sizerRow = CreateSizerWithTextAndLabel(_T("Current selection:"),
206d3a16 224 wxID_ANY,
32b8ec41
VZ
225 &m_textCurSel);
226 sizerMiddle->Add(sizerRow, 0, wxGROW | wxALL, 5);
227
228 sizerRow = CreateSizerWithTextAndButton(RadioPage_Selection,
7b127900 229 _T("&Change selection:"),
206d3a16 230 wxID_ANY,
32b8ec41
VZ
231 &m_textSel);
232 sizerMiddle->Add(sizerRow, 0, wxGROW | wxALL, 5);
233
234 sizerRow = CreateSizerWithTextAndButton(RadioPage_Label,
7b127900 235 _T("&Label for box:"),
206d3a16 236 wxID_ANY,
32b8ec41
VZ
237 &m_textLabel);
238 sizerMiddle->Add(sizerRow, 0, wxGROW | wxALL, 5);
239
240 sizerRow = CreateSizerWithTextAndButton(RadioPage_LabelBtn,
7b127900 241 _T("&Label for buttons:"),
206d3a16 242 wxID_ANY,
32b8ec41
VZ
243 &m_textLabelBtns);
244 sizerMiddle->Add(sizerRow, 0, wxGROW | wxALL, 5);
245
246 // right pane
247 wxSizer *sizerRight = new wxBoxSizer(wxHORIZONTAL);
7b127900 248 sizerRight->SetMinSize(150, 0);
32b8ec41
VZ
249 m_sizerRadio = sizerRight; // save it to modify it later
250
251 Reset();
252 CreateRadio();
253
254 // the 3 panes panes compose the window
255 sizerTop->Add(sizerLeft, 0, wxGROW | (wxALL & ~wxLEFT), 10);
256 sizerTop->Add(sizerMiddle, 1, wxGROW | wxALL, 10);
7b127900 257 sizerTop->Add(sizerRight, 0, wxGROW | (wxALL & ~wxRIGHT), 10);
32b8ec41
VZ
258
259 // final initializations
32b8ec41
VZ
260 SetSizer(sizerTop);
261
262 sizerTop->Fit(this);
263}
264
265RadioWidgetsPage::~RadioWidgetsPage()
266{
267}
268
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
aec18ff7 460#endif