]> git.saurik.com Git - wxWidgets.git/blob - samples/widgets/radiobox.cpp
use wxUSE_DATEPICKCTRL
[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_Radio
60 };
61
62 // layout direction radiobox selections
63 enum
64 {
65 RadioDir_Default,
66 RadioDir_LtoR,
67 RadioDir_TtoB
68 };
69
70 // default values for the number of radiobox items
71 static const unsigned int DEFAULT_NUM_ENTRIES = 12;
72 static const unsigned int DEFAULT_MAJOR_DIM = 3;
73
74 // ----------------------------------------------------------------------------
75 // RadioWidgetsPage
76 // ----------------------------------------------------------------------------
77
78 class RadioWidgetsPage : public WidgetsPage
79 {
80 public:
81 RadioWidgetsPage(wxBookCtrl *book, wxImageList *imaglist);
82 virtual ~RadioWidgetsPage(){};
83
84 virtual wxControl *GetWidget() const { return m_radio; }
85
86 protected:
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
126 private:
127 DECLARE_EVENT_TABLE()
128 DECLARE_WIDGETS_PAGE(RadioWidgetsPage)
129 };
130
131 // ----------------------------------------------------------------------------
132 // event tables
133 // ----------------------------------------------------------------------------
134
135 BEGIN_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
149 EVT_CHECKBOX(wxID_ANY, RadioWidgetsPage::OnCheckOrRadioBox)
150 EVT_RADIOBOX(wxID_ANY, RadioWidgetsPage::OnCheckOrRadioBox)
151 END_EVENT_TABLE()
152
153 // ============================================================================
154 // implementation
155 // ============================================================================
156
157 IMPLEMENT_WIDGETS_PAGE(RadioWidgetsPage, _T("Radio"));
158
159 RadioWidgetsPage::RadioWidgetsPage(wxBookCtrl *book,
160 wxImageList *imaglist)
161 : WidgetsPage(book)
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
179 wxStaticBox *box = new wxStaticBox(this, wxID_ANY, _T("&Set style"));
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
192 m_radioDir = new wxRadioBox(this, wxID_ANY, _T("Numbering:"),
193 wxDefaultPosition, wxDefaultSize,
194 WXSIZEOF(layoutDir), layoutDir,
195 1, wxRA_SPECIFY_COLS);
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;
204 sizerRow = CreateSizerWithTextAndLabel(_T("&Major dimension:"),
205 wxID_ANY,
206 &m_textMajorDim);
207 sizerLeft->Add(sizerRow, 0, wxGROW | wxALL, 5);
208
209 sizerRow = CreateSizerWithTextAndLabel(_T("&Number of buttons:"),
210 wxID_ANY,
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
224 wxStaticBox *box2 = new wxStaticBox(this, wxID_ANY, _T("&Change parameters"));
225 wxSizer *sizerMiddle = new wxStaticBoxSizer(box2, wxVERTICAL);
226
227 sizerRow = CreateSizerWithTextAndLabel(_T("Current selection:"),
228 wxID_ANY,
229 &m_textCurSel);
230 sizerMiddle->Add(sizerRow, 0, wxGROW | wxALL, 5);
231
232 sizerRow = CreateSizerWithTextAndButton(RadioPage_Selection,
233 _T("&Change selection:"),
234 wxID_ANY,
235 &m_textSel);
236 sizerMiddle->Add(sizerRow, 0, wxGROW | wxALL, 5);
237
238 sizerRow = CreateSizerWithTextAndButton(RadioPage_Label,
239 _T("&Label for box:"),
240 wxID_ANY,
241 &m_textLabel);
242 sizerMiddle->Add(sizerRow, 0, wxGROW | wxALL, 5);
243
244 sizerRow = CreateSizerWithTextAndButton(RadioPage_LabelBtn,
245 _T("&Label for buttons:"),
246 wxID_ANY,
247 &m_textLabelBtns);
248 sizerMiddle->Add(sizerRow, 0, wxGROW | wxALL, 5);
249
250 // right pane
251 wxSizer *sizerRight = new wxBoxSizer(wxHORIZONTAL);
252 sizerRight->SetMinSize(150, 0);
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);
261 sizerTop->Add(sizerRight, 0, wxGROW | (wxALL & ~wxRIGHT), 10);
262
263 // final initializations
264 SetSizer(sizerTop);
265
266 sizerTop->Fit(this);
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& WXUNUSED(event))
380 {
381 CreateRadio();
382 }
383
384 void RadioWidgetsPage::OnRadioBox(wxCommandEvent& event)
385 {
386 int sel = m_radio->GetSelection();
387 int event_sel = event.GetSelection();
388 wxUnusedVar(event_sel);
389
390 wxLogMessage(_T("Radiobox selection changed, now %d"), sel);
391
392 wxASSERT_MSG( sel == event_sel,
393 _T("selection should be the same in event and radiobox") );
394
395 m_textCurSel->SetValue(wxString::Format(_T("%d"), sel));
396 }
397
398 void RadioWidgetsPage::OnButtonRecreate(wxCommandEvent& WXUNUSED(event))
399 {
400 CreateRadio();
401 }
402
403 void RadioWidgetsPage::OnButtonSetLabel(wxCommandEvent& WXUNUSED(event))
404 {
405 m_radio->wxControl::SetLabel(m_textLabel->GetValue());
406 }
407
408 void 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
422 void 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
429 void 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
436 void 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
460 #endif // wxUSE_RADIOBOX