1 /////////////////////////////////////////////////////////////////////////////
2 // Program: wxWidgets Widgets Sample
4 // Purpose: Part of the widgets sample showing wxRadioBox
5 // Author: Vadim Zeitlin
8 // Copyright: (c) 2001 Vadim Zeitlin
9 // License: wxWindows license
10 /////////////////////////////////////////////////////////////////////////////
12 // ============================================================================
14 // ============================================================================
16 // ----------------------------------------------------------------------------
18 // ----------------------------------------------------------------------------
20 // for compilers that support precompilation, includes "wx/wx.h".
21 #include "wx/wxprec.h"
29 // for all others, include the necessary headers
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"
45 #include "icons/radiobox.xpm"
47 // ----------------------------------------------------------------------------
49 // ----------------------------------------------------------------------------
54 RadioPage_Reset
= 100,
64 // layout direction radiobox selections
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;
76 // this item is enabled/disabled shown/hidden by the test checkboxes
77 static const int TEST_BUTTON
= 1;
79 // ----------------------------------------------------------------------------
81 // ----------------------------------------------------------------------------
83 class RadioWidgetsPage
: public WidgetsPage
86 RadioWidgetsPage(WidgetsBookCtrl
*book
, wxImageList
*imaglist
);
87 virtual ~RadioWidgetsPage(){};
89 virtual wxControl
*GetWidget() const { return m_radio
; }
90 virtual void RecreateWidget() { CreateRadio(); }
94 void OnCheckOrRadioBox(wxCommandEvent
& event
);
95 void OnRadioBox(wxCommandEvent
& event
);
97 void OnButtonReset(wxCommandEvent
& event
);
98 void OnButtonRecreate(wxCommandEvent
& event
);
100 void OnButtonSelection(wxCommandEvent
& event
);
101 void OnButtonSetLabel(wxCommandEvent
& event
);
103 void OnEnableItem(wxCommandEvent
& event
);
104 void OnShowItem(wxCommandEvent
& event
);
106 void OnUpdateUIReset(wxUpdateUIEvent
& event
);
107 void OnUpdateUIUpdate(wxUpdateUIEvent
& event
);
108 void OnUpdateUISelection(wxUpdateUIEvent
& event
);
109 void OnUpdateUIEnableItem(wxUpdateUIEvent
& event
);
110 void OnUpdateUIShowItem(wxUpdateUIEvent
& event
);
112 // reset the wxRadioBox parameters
115 // (re)create the wxRadioBox
121 // the check/radio boxes for styles
122 wxCheckBox
*m_chkVert
;
123 wxCheckBox
*m_chkEnableItem
;
124 wxCheckBox
*m_chkShowItem
;
125 wxRadioBox
*m_radioDir
;
127 // the gauge itself and the sizer it is in
129 wxSizer
*m_sizerRadio
;
131 // the text entries for command parameters
132 wxTextCtrl
*m_textNumBtns
,
140 DECLARE_EVENT_TABLE()
141 DECLARE_WIDGETS_PAGE(RadioWidgetsPage
)
144 // ----------------------------------------------------------------------------
146 // ----------------------------------------------------------------------------
148 BEGIN_EVENT_TABLE(RadioWidgetsPage
, WidgetsPage
)
149 EVT_BUTTON(RadioPage_Reset
, RadioWidgetsPage::OnButtonReset
)
151 EVT_BUTTON(RadioPage_Update
, RadioWidgetsPage::OnButtonRecreate
)
152 EVT_BUTTON(RadioPage_LabelBtn
, RadioWidgetsPage::OnButtonRecreate
)
154 EVT_BUTTON(RadioPage_Selection
, RadioWidgetsPage::OnButtonSelection
)
155 EVT_BUTTON(RadioPage_Label
, RadioWidgetsPage::OnButtonSetLabel
)
157 EVT_UPDATE_UI(RadioPage_Update
, RadioWidgetsPage::OnUpdateUIUpdate
)
158 EVT_UPDATE_UI(RadioPage_Selection
, RadioWidgetsPage::OnUpdateUISelection
)
160 EVT_RADIOBOX(RadioPage_Radio
, RadioWidgetsPage::OnRadioBox
)
162 EVT_CHECKBOX(RadioPage_EnableItem
, RadioWidgetsPage::OnEnableItem
)
163 EVT_CHECKBOX(RadioPage_ShowItem
, RadioWidgetsPage::OnShowItem
)
165 EVT_UPDATE_UI(RadioPage_EnableItem
, RadioWidgetsPage::OnUpdateUIEnableItem
)
166 EVT_UPDATE_UI(RadioPage_ShowItem
, RadioWidgetsPage::OnUpdateUIShowItem
)
168 EVT_CHECKBOX(wxID_ANY
, RadioWidgetsPage::OnCheckOrRadioBox
)
169 EVT_RADIOBOX(wxID_ANY
, RadioWidgetsPage::OnCheckOrRadioBox
)
172 // ============================================================================
174 // ============================================================================
176 IMPLEMENT_WIDGETS_PAGE(RadioWidgetsPage
, _T("Radio"),
177 (int)wxPlatform(GENERIC_CTRLS
).If(wxMSW
,NATIVE_CTRLS
)
181 RadioWidgetsPage::RadioWidgetsPage(WidgetsBookCtrl
*book
,
182 wxImageList
*imaglist
)
185 imaglist
->Add(wxBitmap(radio_xpm
));
188 m_chkVert
= (wxCheckBox
*)NULL
;
189 m_chkEnableItem
= (wxCheckBox
*)NULL
;
190 m_chkShowItem
= (wxCheckBox
*)NULL
;
194 m_textLabel
= (wxTextCtrl
*)NULL
;
197 m_radioDir
= (wxRadioBox
*)NULL
;
198 m_sizerRadio
= (wxSizer
*)NULL
;
200 wxSizer
*sizerTop
= new wxBoxSizer(wxHORIZONTAL
);
203 wxStaticBox
*box
= new wxStaticBox(this, wxID_ANY
, _T("&Set style"));
205 wxSizer
*sizerLeft
= new wxStaticBoxSizer(box
, wxVERTICAL
);
207 m_chkVert
= CreateCheckBoxAndAddToSizer(sizerLeft
, _T("&Vertical layout"));
209 static const wxString layoutDir
[] =
216 m_radioDir
= new wxRadioBox(this, wxID_ANY
, _T("Numbering:"),
217 wxDefaultPosition
, wxDefaultSize
,
218 WXSIZEOF(layoutDir
), layoutDir
,
219 1, wxRA_SPECIFY_COLS
);
220 sizerLeft
->Add(m_radioDir
, 0, wxGROW
| wxALL
, 5);
222 // if it's not defined, we can't change the radiobox direction
223 #ifndef wxRA_LEFTTORIGHT
224 m_radioDir
->Disable();
225 #endif // wxRA_LEFTTORIGHT
228 sizerRow
= CreateSizerWithTextAndLabel(_T("&Major dimension:"),
231 sizerLeft
->Add(sizerRow
, 0, wxGROW
| wxALL
, 5);
233 sizerRow
= CreateSizerWithTextAndLabel(_T("&Number of buttons:"),
236 sizerLeft
->Add(sizerRow
, 0, wxGROW
| wxALL
, 5);
239 btn
= new wxButton(this, RadioPage_Update
, _T("&Update"));
240 sizerLeft
->Add(btn
, 0, wxALIGN_CENTRE_HORIZONTAL
| wxALL
, 5);
242 sizerLeft
->Add(5, 5, 0, wxGROW
| wxALL
, 5); // spacer
244 btn
= new wxButton(this, RadioPage_Reset
, _T("&Reset"));
245 sizerLeft
->Add(btn
, 0, wxALIGN_CENTRE_HORIZONTAL
| wxALL
, 15);
248 wxStaticBox
*box2
= new wxStaticBox(this, wxID_ANY
, _T("&Change parameters"));
249 wxSizer
*sizerMiddle
= new wxStaticBoxSizer(box2
, wxVERTICAL
);
251 sizerRow
= CreateSizerWithTextAndLabel(_T("Current selection:"),
254 sizerMiddle
->Add(sizerRow
, 0, wxGROW
| wxALL
, 5);
256 sizerRow
= CreateSizerWithTextAndButton(RadioPage_Selection
,
257 _T("&Change selection:"),
260 sizerMiddle
->Add(sizerRow
, 0, wxGROW
| wxALL
, 5);
262 sizerRow
= CreateSizerWithTextAndButton(RadioPage_Label
,
263 _T("&Label for box:"),
266 sizerMiddle
->Add(sizerRow
, 0, wxGROW
| wxALL
, 5);
268 sizerRow
= CreateSizerWithTextAndButton(RadioPage_LabelBtn
,
269 _T("&Label for buttons:"),
272 sizerMiddle
->Add(sizerRow
, 0, wxGROW
| wxALL
, 5);
274 m_chkEnableItem
= CreateCheckBoxAndAddToSizer(sizerMiddle
,
275 _T("Disable &2nd item"),
276 RadioPage_EnableItem
);
277 m_chkShowItem
= CreateCheckBoxAndAddToSizer(sizerMiddle
,
278 _T("Hide 2nd &item"),
282 wxSizer
*sizerRight
= new wxBoxSizer(wxHORIZONTAL
);
283 sizerRight
->SetMinSize(150, 0);
284 m_sizerRadio
= sizerRight
; // save it to modify it later
289 // the 3 panes panes compose the window
290 sizerTop
->Add(sizerLeft
, 0, wxGROW
| (wxALL
& ~wxLEFT
), 10);
291 sizerTop
->Add(sizerMiddle
, 1, wxGROW
| wxALL
, 10);
292 sizerTop
->Add(sizerRight
, 0, wxGROW
| (wxALL
& ~wxRIGHT
), 10);
294 // final initializations
300 // ----------------------------------------------------------------------------
302 // ----------------------------------------------------------------------------
304 void RadioWidgetsPage::Reset()
306 m_textMajorDim
->SetValue(wxString::Format(_T("%u"), DEFAULT_MAJOR_DIM
));
307 m_textNumBtns
->SetValue(wxString::Format(_T("%u"), DEFAULT_NUM_ENTRIES
));
308 m_textLabel
->SetValue(_T("I'm a radiobox"));
309 m_textLabelBtns
->SetValue(_T("item"));
311 m_chkVert
->SetValue(false);
312 m_chkEnableItem
->SetValue(true);
313 m_chkShowItem
->SetValue(true);
314 m_radioDir
->SetSelection(RadioDir_Default
);
317 void RadioWidgetsPage::CreateRadio()
322 sel
= m_radio
->GetSelection();
324 m_sizerRadio
->Detach( m_radio
);
328 else // first time creation, no old selection to preserve
334 if ( !m_textNumBtns
->GetValue().ToULong(&count
) )
336 wxLogWarning(_T("Should have a valid number for number of items."));
338 // fall back to default
339 count
= DEFAULT_NUM_ENTRIES
;
342 unsigned long majorDim
;
343 if ( !m_textMajorDim
->GetValue().ToULong(&majorDim
) )
345 wxLogWarning(_T("Should have a valid major dimension number."));
347 // fall back to default
348 majorDim
= DEFAULT_MAJOR_DIM
;
351 wxString
*items
= new wxString
[count
];
353 wxString labelBtn
= m_textLabelBtns
->GetValue();
354 for ( size_t n
= 0; n
< count
; n
++ )
356 items
[n
] = wxString::Format(_T("%s %lu"),
357 labelBtn
.c_str(), (unsigned long)n
+ 1);
360 int flags
= m_chkVert
->GetValue() ? wxRA_VERTICAL
363 flags
|= ms_defaultFlags
;
365 #ifdef wxRA_LEFTTORIGHT
366 switch ( m_radioDir
->GetSelection() )
369 wxFAIL_MSG( _T("unexpected wxRadioBox layout direction") );
372 case RadioDir_Default
:
376 flags
|= wxRA_LEFTTORIGHT
;
380 flags
|= wxRA_TOPTOBOTTOM
;
383 #endif // wxRA_LEFTTORIGHT
385 m_radio
= new wxRadioBox(this, RadioPage_Radio
,
386 m_textLabel
->GetValue(),
387 wxDefaultPosition
, wxDefaultSize
,
394 if ( sel
>= 0 && (size_t)sel
< count
)
396 m_radio
->SetSelection(sel
);
399 m_sizerRadio
->Add(m_radio
, 1, wxGROW
);
400 m_sizerRadio
->Layout();
402 m_chkEnableItem
->SetValue(true);
403 m_chkEnableItem
->SetValue(true);
406 // ----------------------------------------------------------------------------
408 // ----------------------------------------------------------------------------
410 void RadioWidgetsPage::OnButtonReset(wxCommandEvent
& WXUNUSED(event
))
417 void RadioWidgetsPage::OnCheckOrRadioBox(wxCommandEvent
& WXUNUSED(event
))
422 void RadioWidgetsPage::OnRadioBox(wxCommandEvent
& event
)
424 int sel
= m_radio
->GetSelection();
425 int event_sel
= event
.GetSelection();
426 wxUnusedVar(event_sel
);
428 wxLogMessage(_T("Radiobox selection changed, now %d"), sel
);
430 wxASSERT_MSG( sel
== event_sel
,
431 _T("selection should be the same in event and radiobox") );
433 m_textCurSel
->SetValue(wxString::Format(_T("%d"), sel
));
436 void RadioWidgetsPage::OnButtonRecreate(wxCommandEvent
& WXUNUSED(event
))
441 void RadioWidgetsPage::OnButtonSetLabel(wxCommandEvent
& WXUNUSED(event
))
443 m_radio
->wxControl::SetLabel(m_textLabel
->GetValue());
446 void RadioWidgetsPage::OnButtonSelection(wxCommandEvent
& WXUNUSED(event
))
449 if ( !m_textSel
->GetValue().ToULong(&sel
) ||
450 (sel
>= (size_t)m_radio
->GetCount()) )
452 wxLogWarning(_T("Invalid number specified as new selection."));
456 m_radio
->SetSelection(sel
);
460 void RadioWidgetsPage::OnEnableItem(wxCommandEvent
& event
)
462 m_radio
->Enable(TEST_BUTTON
, event
.IsChecked());
465 void RadioWidgetsPage::OnShowItem(wxCommandEvent
& event
)
467 m_radio
->Show(TEST_BUTTON
, event
.IsChecked());
470 void RadioWidgetsPage::OnUpdateUIUpdate(wxUpdateUIEvent
& event
)
473 event
.Enable( m_textNumBtns
->GetValue().ToULong(&n
) &&
474 m_textMajorDim
->GetValue().ToULong(&n
) );
477 void RadioWidgetsPage::OnUpdateUISelection(wxUpdateUIEvent
& event
)
480 event
.Enable( m_textSel
->GetValue().ToULong(&n
) &&
481 (n
< (size_t)m_radio
->GetCount()) );
484 void RadioWidgetsPage::OnUpdateUIReset(wxUpdateUIEvent
& event
)
486 // only enable it if something is not set to default
487 bool enable
= m_chkVert
->GetValue();
491 unsigned long numEntries
;
493 enable
= !m_textNumBtns
->GetValue().ToULong(&numEntries
) ||
494 numEntries
!= DEFAULT_NUM_ENTRIES
;
498 unsigned long majorDim
;
500 enable
= !m_textMajorDim
->GetValue().ToULong(&majorDim
) ||
501 majorDim
!= DEFAULT_MAJOR_DIM
;
505 event
.Enable(enable
);
508 void RadioWidgetsPage::OnUpdateUIEnableItem(wxUpdateUIEvent
& event
)
510 event
.SetText(m_radio
->IsItemEnabled(TEST_BUTTON
) ? _T("Disable &2nd item")
511 : _T("Enable &2nd item"));
514 void RadioWidgetsPage::OnUpdateUIShowItem(wxUpdateUIEvent
& event
)
516 event
.SetText(m_radio
->IsItemShown(TEST_BUTTON
) ? _T("Hide 2nd &item")
517 : _T("Show 2nd &item"));
520 #endif // wxUSE_RADIOBOX