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