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