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