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(wxBookCtrlBase
*book
, wxImageList
*imaglist
);
87 virtual ~RadioWidgetsPage(){};
89 virtual wxControl
*GetWidget() const { return m_radio
; }
93 void OnCheckOrRadioBox(wxCommandEvent
& event
);
94 void OnRadioBox(wxCommandEvent
& event
);
96 void OnButtonReset(wxCommandEvent
& event
);
97 void OnButtonRecreate(wxCommandEvent
& event
);
99 void OnButtonSelection(wxCommandEvent
& event
);
100 void OnButtonSetLabel(wxCommandEvent
& event
);
102 void OnEnableItem(wxCommandEvent
& event
);
103 void OnShowItem(wxCommandEvent
& event
);
105 void OnUpdateUIReset(wxUpdateUIEvent
& event
);
106 void OnUpdateUIUpdate(wxUpdateUIEvent
& event
);
107 void OnUpdateUISelection(wxUpdateUIEvent
& event
);
108 void OnUpdateUIEnableItem(wxUpdateUIEvent
& event
);
109 void OnUpdateUIShowItem(wxUpdateUIEvent
& event
);
111 // reset the wxRadioBox parameters
114 // (re)create the wxRadioBox
120 // the check/radio boxes for styles
121 wxCheckBox
*m_chkVert
;
122 wxCheckBox
*m_chkEnableItem
;
123 wxCheckBox
*m_chkShowItem
;
124 wxRadioBox
*m_radioDir
;
126 // the gauge itself and the sizer it is in
128 wxSizer
*m_sizerRadio
;
130 // the text entries for command parameters
131 wxTextCtrl
*m_textNumBtns
,
139 DECLARE_EVENT_TABLE()
140 DECLARE_WIDGETS_PAGE(RadioWidgetsPage
)
143 // ----------------------------------------------------------------------------
145 // ----------------------------------------------------------------------------
147 BEGIN_EVENT_TABLE(RadioWidgetsPage
, WidgetsPage
)
148 EVT_BUTTON(RadioPage_Reset
, RadioWidgetsPage::OnButtonReset
)
150 EVT_BUTTON(RadioPage_Update
, RadioWidgetsPage::OnButtonRecreate
)
151 EVT_BUTTON(RadioPage_LabelBtn
, RadioWidgetsPage::OnButtonRecreate
)
153 EVT_BUTTON(RadioPage_Selection
, RadioWidgetsPage::OnButtonSelection
)
154 EVT_BUTTON(RadioPage_Label
, RadioWidgetsPage::OnButtonSetLabel
)
156 EVT_UPDATE_UI(RadioPage_Update
, RadioWidgetsPage::OnUpdateUIUpdate
)
157 EVT_UPDATE_UI(RadioPage_Selection
, RadioWidgetsPage::OnUpdateUISelection
)
159 EVT_RADIOBOX(RadioPage_Radio
, RadioWidgetsPage::OnRadioBox
)
161 EVT_CHECKBOX(RadioPage_EnableItem
, RadioWidgetsPage::OnEnableItem
)
162 EVT_CHECKBOX(RadioPage_ShowItem
, RadioWidgetsPage::OnShowItem
)
164 EVT_UPDATE_UI(RadioPage_EnableItem
, RadioWidgetsPage::OnUpdateUIEnableItem
)
165 EVT_UPDATE_UI(RadioPage_ShowItem
, RadioWidgetsPage::OnUpdateUIShowItem
)
167 EVT_CHECKBOX(wxID_ANY
, RadioWidgetsPage::OnCheckOrRadioBox
)
168 EVT_RADIOBOX(wxID_ANY
, RadioWidgetsPage::OnCheckOrRadioBox
)
171 // ============================================================================
173 // ============================================================================
175 IMPLEMENT_WIDGETS_PAGE(RadioWidgetsPage
, _T("Radio"));
177 RadioWidgetsPage::RadioWidgetsPage(wxBookCtrlBase
*book
,
178 wxImageList
*imaglist
)
181 imaglist
->Add(wxBitmap(radio_xpm
));
184 m_chkVert
= (wxCheckBox
*)NULL
;
185 m_chkEnableItem
= (wxCheckBox
*)NULL
;
186 m_chkShowItem
= (wxCheckBox
*)NULL
;
190 m_textLabel
= (wxTextCtrl
*)NULL
;
193 m_radioDir
= (wxRadioBox
*)NULL
;
194 m_sizerRadio
= (wxSizer
*)NULL
;
196 wxSizer
*sizerTop
= new wxBoxSizer(wxHORIZONTAL
);
199 wxStaticBox
*box
= new wxStaticBox(this, wxID_ANY
, _T("&Set style"));
201 wxSizer
*sizerLeft
= new wxStaticBoxSizer(box
, wxVERTICAL
);
203 m_chkVert
= CreateCheckBoxAndAddToSizer(sizerLeft
, _T("&Vertical layout"));
205 static const wxString layoutDir
[] =
212 m_radioDir
= new wxRadioBox(this, wxID_ANY
, _T("Numbering:"),
213 wxDefaultPosition
, wxDefaultSize
,
214 WXSIZEOF(layoutDir
), layoutDir
,
215 1, wxRA_SPECIFY_COLS
);
216 sizerLeft
->Add(m_radioDir
, 0, wxGROW
| wxALL
, 5);
218 // if it's not defined, we can't change the radiobox direction
219 #ifndef wxRA_LEFTTORIGHT
220 m_radioDir
->Disable();
221 #endif // wxRA_LEFTTORIGHT
224 sizerRow
= CreateSizerWithTextAndLabel(_T("&Major dimension:"),
227 sizerLeft
->Add(sizerRow
, 0, wxGROW
| wxALL
, 5);
229 sizerRow
= CreateSizerWithTextAndLabel(_T("&Number of buttons:"),
232 sizerLeft
->Add(sizerRow
, 0, wxGROW
| wxALL
, 5);
235 btn
= new wxButton(this, RadioPage_Update
, _T("&Update"));
236 sizerLeft
->Add(btn
, 0, wxALIGN_CENTRE_HORIZONTAL
| wxALL
, 5);
238 sizerLeft
->Add(5, 5, 0, wxGROW
| wxALL
, 5); // spacer
240 btn
= new wxButton(this, RadioPage_Reset
, _T("&Reset"));
241 sizerLeft
->Add(btn
, 0, wxALIGN_CENTRE_HORIZONTAL
| wxALL
, 15);
244 wxStaticBox
*box2
= new wxStaticBox(this, wxID_ANY
, _T("&Change parameters"));
245 wxSizer
*sizerMiddle
= new wxStaticBoxSizer(box2
, wxVERTICAL
);
247 sizerRow
= CreateSizerWithTextAndLabel(_T("Current selection:"),
250 sizerMiddle
->Add(sizerRow
, 0, wxGROW
| wxALL
, 5);
252 sizerRow
= CreateSizerWithTextAndButton(RadioPage_Selection
,
253 _T("&Change selection:"),
256 sizerMiddle
->Add(sizerRow
, 0, wxGROW
| wxALL
, 5);
258 sizerRow
= CreateSizerWithTextAndButton(RadioPage_Label
,
259 _T("&Label for box:"),
262 sizerMiddle
->Add(sizerRow
, 0, wxGROW
| wxALL
, 5);
264 sizerRow
= CreateSizerWithTextAndButton(RadioPage_LabelBtn
,
265 _T("&Label for buttons:"),
268 sizerMiddle
->Add(sizerRow
, 0, wxGROW
| wxALL
, 5);
270 m_chkEnableItem
= CreateCheckBoxAndAddToSizer(sizerMiddle
,
271 _T("Disable &2nd item"),
272 RadioPage_EnableItem
);
273 m_chkShowItem
= CreateCheckBoxAndAddToSizer(sizerMiddle
,
274 _T("Hide 2nd &item"),
278 wxSizer
*sizerRight
= new wxBoxSizer(wxHORIZONTAL
);
279 sizerRight
->SetMinSize(150, 0);
280 m_sizerRadio
= sizerRight
; // save it to modify it later
285 // the 3 panes panes compose the window
286 sizerTop
->Add(sizerLeft
, 0, wxGROW
| (wxALL
& ~wxLEFT
), 10);
287 sizerTop
->Add(sizerMiddle
, 1, wxGROW
| wxALL
, 10);
288 sizerTop
->Add(sizerRight
, 0, wxGROW
| (wxALL
& ~wxRIGHT
), 10);
290 // final initializations
296 // ----------------------------------------------------------------------------
298 // ----------------------------------------------------------------------------
300 void RadioWidgetsPage::Reset()
302 m_textMajorDim
->SetValue(wxString::Format(_T("%u"), DEFAULT_MAJOR_DIM
));
303 m_textNumBtns
->SetValue(wxString::Format(_T("%u"), DEFAULT_NUM_ENTRIES
));
304 m_textLabel
->SetValue(_T("I'm a radiobox"));
305 m_textLabelBtns
->SetValue(_T("item"));
307 m_chkVert
->SetValue(false);
308 m_chkEnableItem
->SetValue(true);
309 m_chkShowItem
->SetValue(true);
310 m_radioDir
->SetSelection(RadioDir_Default
);
313 void RadioWidgetsPage::CreateRadio()
318 sel
= m_radio
->GetSelection();
320 m_sizerRadio
->Detach( m_radio
);
324 else // first time creation, no old selection to preserve
330 if ( !m_textNumBtns
->GetValue().ToULong(&count
) )
332 wxLogWarning(_T("Should have a valid number for number of items."));
334 // fall back to default
335 count
= DEFAULT_NUM_ENTRIES
;
338 unsigned long majorDim
;
339 if ( !m_textMajorDim
->GetValue().ToULong(&majorDim
) )
341 wxLogWarning(_T("Should have a valid major dimension number."));
343 // fall back to default
344 majorDim
= DEFAULT_MAJOR_DIM
;
347 wxString
*items
= new wxString
[count
];
349 wxString labelBtn
= m_textLabelBtns
->GetValue();
350 for ( size_t n
= 0; n
< count
; n
++ )
352 items
[n
] = wxString::Format(_T("%s %lu"),
353 labelBtn
.c_str(), (unsigned long)n
+ 1);
356 int flags
= m_chkVert
->GetValue() ? wxRA_VERTICAL
359 #ifdef wxRA_LEFTTORIGHT
360 switch ( m_radioDir
->GetSelection() )
363 wxFAIL_MSG( _T("unexpected wxRadioBox layout direction") );
366 case RadioDir_Default
:
370 flags
|= wxRA_LEFTTORIGHT
;
374 flags
|= wxRA_TOPTOBOTTOM
;
377 #endif // wxRA_LEFTTORIGHT
379 m_radio
= new wxRadioBox(this, RadioPage_Radio
,
380 m_textLabel
->GetValue(),
381 wxDefaultPosition
, wxDefaultSize
,
388 if ( sel
>= 0 && (size_t)sel
< count
)
390 m_radio
->SetSelection(sel
);
393 m_sizerRadio
->Add(m_radio
, 1, wxGROW
);
394 m_sizerRadio
->Layout();
396 m_chkEnableItem
->SetValue(true);
397 m_chkEnableItem
->SetValue(true);
400 // ----------------------------------------------------------------------------
402 // ----------------------------------------------------------------------------
404 void RadioWidgetsPage::OnButtonReset(wxCommandEvent
& WXUNUSED(event
))
411 void RadioWidgetsPage::OnCheckOrRadioBox(wxCommandEvent
& WXUNUSED(event
))
416 void RadioWidgetsPage::OnRadioBox(wxCommandEvent
& event
)
418 int sel
= m_radio
->GetSelection();
419 int event_sel
= event
.GetSelection();
420 wxUnusedVar(event_sel
);
422 wxLogMessage(_T("Radiobox selection changed, now %d"), sel
);
424 wxASSERT_MSG( sel
== event_sel
,
425 _T("selection should be the same in event and radiobox") );
427 m_textCurSel
->SetValue(wxString::Format(_T("%d"), sel
));
430 void RadioWidgetsPage::OnButtonRecreate(wxCommandEvent
& WXUNUSED(event
))
435 void RadioWidgetsPage::OnButtonSetLabel(wxCommandEvent
& WXUNUSED(event
))
437 m_radio
->wxControl::SetLabel(m_textLabel
->GetValue());
440 void RadioWidgetsPage::OnButtonSelection(wxCommandEvent
& WXUNUSED(event
))
443 if ( !m_textSel
->GetValue().ToULong(&sel
) ||
444 (sel
>= (size_t)m_radio
->GetCount()) )
446 wxLogWarning(_T("Invalid number specified as new selection."));
450 m_radio
->SetSelection(sel
);
454 void RadioWidgetsPage::OnEnableItem(wxCommandEvent
& event
)
456 m_radio
->Enable(TEST_BUTTON
, event
.IsChecked());
459 void RadioWidgetsPage::OnShowItem(wxCommandEvent
& event
)
461 m_radio
->Show(TEST_BUTTON
, event
.IsChecked());
464 void RadioWidgetsPage::OnUpdateUIUpdate(wxUpdateUIEvent
& event
)
467 event
.Enable( m_textNumBtns
->GetValue().ToULong(&n
) &&
468 m_textMajorDim
->GetValue().ToULong(&n
) );
471 void RadioWidgetsPage::OnUpdateUISelection(wxUpdateUIEvent
& event
)
474 event
.Enable( m_textSel
->GetValue().ToULong(&n
) &&
475 (n
< (size_t)m_radio
->GetCount()) );
478 void RadioWidgetsPage::OnUpdateUIReset(wxUpdateUIEvent
& event
)
480 // only enable it if something is not set to default
481 bool enable
= m_chkVert
->GetValue();
485 unsigned long numEntries
;
487 enable
= !m_textNumBtns
->GetValue().ToULong(&numEntries
) ||
488 numEntries
!= DEFAULT_NUM_ENTRIES
;
492 unsigned long majorDim
;
494 enable
= !m_textMajorDim
->GetValue().ToULong(&majorDim
) ||
495 majorDim
!= DEFAULT_MAJOR_DIM
;
499 event
.Enable(enable
);
502 void RadioWidgetsPage::OnUpdateUIEnableItem(wxUpdateUIEvent
& event
)
504 event
.SetText(m_radio
->IsItemEnabled(TEST_BUTTON
) ? _T("Disable &2nd item")
505 : _T("Enable &2nd item"));
508 void RadioWidgetsPage::OnUpdateUIShowItem(wxUpdateUIEvent
& event
)
510 event
.SetText(m_radio
->IsItemShown(TEST_BUTTON
) ? _T("Hide 2nd &item")
511 : _T("Show 2nd &item"));
514 #endif // wxUSE_RADIOBOX