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