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