]> git.saurik.com Git - wxWidgets.git/blob - samples/widgets/radiobox.cpp
compilation fix for WXWIN_COMPATIBILITY_2 == 1
[wxWidgets.git] / samples / widgets / radiobox.cpp
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 #if 1
42 #include "icons/radiobox.xpm"
43
44 // ----------------------------------------------------------------------------
45 // constants
46 // ----------------------------------------------------------------------------
47
48 // control ids
49 enum
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
60 enum
61 {
62 RadioDir_Default,
63 RadioDir_LtoR,
64 RadioDir_TtoB
65 };
66
67 // default values for the number of radiobox items
68 static const unsigned int DEFAULT_NUM_ENTRIES = 12;
69 static const unsigned int DEFAULT_MAJOR_DIM = 3;
70
71 // ----------------------------------------------------------------------------
72 // RadioWidgetsPage
73 // ----------------------------------------------------------------------------
74
75 class RadioWidgetsPage : public WidgetsPage
76 {
77 public:
78 RadioWidgetsPage(wxNotebook *notebook, wxImageList *imaglist);
79 virtual ~RadioWidgetsPage();
80
81 protected:
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
121 private:
122 DECLARE_EVENT_TABLE()
123 DECLARE_WIDGETS_PAGE(RadioWidgetsPage)
124 };
125
126 // ----------------------------------------------------------------------------
127 // event tables
128 // ----------------------------------------------------------------------------
129
130 BEGIN_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)
146 END_EVENT_TABLE()
147
148 // ============================================================================
149 // implementation
150 // ============================================================================
151
152 IMPLEMENT_WIDGETS_PAGE(RadioWidgetsPage, _T("Radio"));
153
154 RadioWidgetsPage::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 1, wxRA_SPECIFY_COLS);
191 sizerLeft->Add(m_radioDir, 0, wxGROW | wxALL, 5);
192
193 // if it's not defined, we can't change the radiobox direction
194 #ifndef wxRA_LEFTTORIGHT
195 m_radioDir->Disable();
196 #endif // wxRA_LEFTTORIGHT
197
198 wxSizer *sizerRow;
199 sizerRow = CreateSizerWithTextAndLabel(_T("&Major dimension:"),
200 -1,
201 &m_textMajorDim);
202 sizerLeft->Add(sizerRow, 0, wxGROW | wxALL, 5);
203
204 sizerRow = CreateSizerWithTextAndLabel(_T("&Number of buttons:"),
205 -1,
206 &m_textNumBtns);
207 sizerLeft->Add(sizerRow, 0, wxGROW | wxALL, 5);
208
209 wxButton *btn;
210 btn = new wxButton(this, RadioPage_Update, _T("&Update"));
211 sizerLeft->Add(btn, 0, wxALIGN_CENTRE_HORIZONTAL | wxALL, 5);
212
213 sizerLeft->Add(5, 5, 0, wxGROW | wxALL, 5); // spacer
214
215 btn = new wxButton(this, RadioPage_Reset, _T("&Reset"));
216 sizerLeft->Add(btn, 0, wxALIGN_CENTRE_HORIZONTAL | wxALL, 15);
217
218 // middle pane
219 wxStaticBox *box2 = new wxStaticBox(this, -1, _T("&Change parameters"));
220 wxSizer *sizerMiddle = new wxStaticBoxSizer(box2, wxVERTICAL);
221
222 sizerRow = CreateSizerWithTextAndLabel(_T("Current selection:"),
223 -1,
224 &m_textCurSel);
225 sizerMiddle->Add(sizerRow, 0, wxGROW | wxALL, 5);
226
227 sizerRow = CreateSizerWithTextAndButton(RadioPage_Selection,
228 _T("&Change selection:"),
229 -1,
230 &m_textSel);
231 sizerMiddle->Add(sizerRow, 0, wxGROW | wxALL, 5);
232
233 sizerRow = CreateSizerWithTextAndButton(RadioPage_Label,
234 _T("&Label for box:"),
235 -1,
236 &m_textLabel);
237 sizerMiddle->Add(sizerRow, 0, wxGROW | wxALL, 5);
238
239 sizerRow = CreateSizerWithTextAndButton(RadioPage_LabelBtn,
240 _T("&Label for buttons:"),
241 -1,
242 &m_textLabelBtns);
243 sizerMiddle->Add(sizerRow, 0, wxGROW | wxALL, 5);
244
245 // right pane
246 wxSizer *sizerRight = new wxBoxSizer(wxHORIZONTAL);
247 sizerRight->SetMinSize(150, 0);
248 m_sizerRadio = sizerRight; // save it to modify it later
249
250 Reset();
251 CreateRadio();
252
253 // the 3 panes panes compose the window
254 sizerTop->Add(sizerLeft, 0, wxGROW | (wxALL & ~wxLEFT), 10);
255 sizerTop->Add(sizerMiddle, 1, wxGROW | wxALL, 10);
256 sizerTop->Add(sizerRight, 0, wxGROW | (wxALL & ~wxRIGHT), 10);
257
258 // final initializations
259 SetAutoLayout(TRUE);
260 SetSizer(sizerTop);
261
262 sizerTop->Fit(this);
263 }
264
265 RadioWidgetsPage::~RadioWidgetsPage()
266 {
267 }
268
269 // ----------------------------------------------------------------------------
270 // operations
271 // ----------------------------------------------------------------------------
272
273 void RadioWidgetsPage::Reset()
274 {
275 m_textMajorDim->SetValue(wxString::Format(_T("%u"), DEFAULT_MAJOR_DIM));
276 m_textNumBtns->SetValue(wxString::Format(_T("%u"), DEFAULT_NUM_ENTRIES));
277 m_textLabel->SetValue(_T("I'm a radiobox"));
278 m_textLabelBtns->SetValue(_T("item"));
279
280 m_chkVert->SetValue(FALSE);
281 m_radioDir->SetSelection(RadioDir_Default);
282 }
283
284 void RadioWidgetsPage::CreateRadio()
285 {
286 int sel;
287 if ( m_radio )
288 {
289 sel = m_radio->GetSelection();
290
291 m_sizerRadio->Detach( m_radio );
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 {
323 items[n] = wxString::Format(_T("%s %lu"),
324 labelBtn.c_str(), (unsigned long)n + 1);
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
372 void RadioWidgetsPage::OnButtonReset(wxCommandEvent& WXUNUSED(event))
373 {
374 Reset();
375
376 CreateRadio();
377 }
378
379 void RadioWidgetsPage::OnCheckOrRadioBox(wxCommandEvent& event)
380 {
381 CreateRadio();
382 }
383
384 void RadioWidgetsPage::OnRadioBox(wxCommandEvent& event)
385 {
386 int sel = m_radio->GetSelection();
387
388 wxLogMessage(_T("Radiobox selection changed, now %d"), sel);
389
390 wxASSERT_MSG( sel == event.GetSelection(),
391 _T("selection should be the same in event and radiobox") );
392
393 m_textCurSel->SetValue(wxString::Format(_T("%d"), sel));
394 }
395
396 void RadioWidgetsPage::OnButtonRecreate(wxCommandEvent& WXUNUSED(event))
397 {
398 CreateRadio();
399 }
400
401 void RadioWidgetsPage::OnButtonSetLabel(wxCommandEvent& WXUNUSED(event))
402 {
403 m_radio->wxControl::SetLabel(m_textLabel->GetValue());
404 }
405
406 void RadioWidgetsPage::OnButtonSelection(wxCommandEvent& WXUNUSED(event))
407 {
408 unsigned long sel;
409 if ( !m_textSel->GetValue().ToULong(&sel) ||
410 (sel >= (size_t)m_radio->GetCount()) )
411 {
412 wxLogWarning(_T("Invalid number specified as new selection."));
413 }
414 else
415 {
416 m_radio->SetSelection(sel);
417 }
418 }
419
420 void RadioWidgetsPage::OnUpdateUIUpdate(wxUpdateUIEvent& event)
421 {
422 unsigned long n;
423 event.Enable( m_textNumBtns->GetValue().ToULong(&n) &&
424 m_textMajorDim->GetValue().ToULong(&n) );
425 }
426
427 void RadioWidgetsPage::OnUpdateUISelection(wxUpdateUIEvent& event)
428 {
429 unsigned long n;
430 event.Enable( m_textSel->GetValue().ToULong(&n) &&
431 (n < (size_t)m_radio->GetCount()) );
432 }
433
434 void RadioWidgetsPage::OnUpdateUIReset(wxUpdateUIEvent& event)
435 {
436 // only enable it if something is not set to default
437 bool enable = m_chkVert->GetValue();
438
439 if ( !enable )
440 {
441 unsigned long numEntries;
442
443 enable = !m_textNumBtns->GetValue().ToULong(&numEntries) ||
444 numEntries != DEFAULT_NUM_ENTRIES;
445
446 if ( !enable )
447 {
448 unsigned long majorDim;
449
450 enable = !m_textMajorDim->GetValue().ToULong(&majorDim) ||
451 majorDim != DEFAULT_MAJOR_DIM;
452 }
453 }
454
455 event.Enable(enable);
456 }
457
458 #endif