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