1 /////////////////////////////////////////////////////////////////////////////
2 // Program: wxWindows 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"
27 // for all others, include the necessary headers
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"
42 #include "icons/radiobox.xpm"
44 // ----------------------------------------------------------------------------
46 // ----------------------------------------------------------------------------
51 RadioPage_Reset
= 100,
59 // layout direction radiobox selections
67 // default values for the number of radiobox items
68 static const size_t DEFAULT_NUM_ENTRIES
= 12;
69 static const size_t DEFAULT_MAJOR_DIM
= 4;
71 // ----------------------------------------------------------------------------
73 // ----------------------------------------------------------------------------
75 class RadioWidgetsPage
: public WidgetsPage
78 RadioWidgetsPage(wxNotebook
*notebook
, wxImageList
*imaglist
);
79 virtual ~RadioWidgetsPage();
83 void OnCheckOrRadioBox(wxCommandEvent
& event
);
84 void OnRadioBox(wxCommandEvent
& event
);
86 void OnButtonReset(wxCommandEvent
& event
);
87 void OnButtonRecreate(wxCommandEvent
& event
);
89 void OnButtonSelection(wxCommandEvent
& event
);
90 void OnButtonSetLabel(wxCommandEvent
& event
);
92 void OnUpdateUIReset(wxUpdateUIEvent
& event
);
93 void OnUpdateUIUpdate(wxUpdateUIEvent
& event
);
94 void OnUpdateUISelection(wxUpdateUIEvent
& event
);
96 // reset the wxRadioBox parameters
99 // (re)create the wxRadioBox
105 // the check/radio boxes for styles
106 wxCheckBox
*m_chkVert
;
107 wxRadioBox
*m_radioDir
;
109 // the gauge itself and the sizer it is in
111 wxSizer
*m_sizerRadio
;
113 // the text entries for command parameters
114 wxTextCtrl
*m_textNumBtns
,
122 DECLARE_EVENT_TABLE()
123 DECLARE_WIDGETS_PAGE(RadioWidgetsPage
)
126 // ----------------------------------------------------------------------------
128 // ----------------------------------------------------------------------------
130 BEGIN_EVENT_TABLE(RadioWidgetsPage
, WidgetsPage
)
131 EVT_BUTTON(RadioPage_Reset
, RadioWidgetsPage::OnButtonReset
)
133 EVT_BUTTON(RadioPage_Update
, RadioWidgetsPage::OnButtonRecreate
)
134 EVT_BUTTON(RadioPage_LabelBtn
, RadioWidgetsPage::OnButtonRecreate
)
136 EVT_BUTTON(RadioPage_Selection
, RadioWidgetsPage::OnButtonSelection
)
137 EVT_BUTTON(RadioPage_Label
, RadioWidgetsPage::OnButtonSetLabel
)
139 EVT_UPDATE_UI(RadioPage_Update
, RadioWidgetsPage::OnUpdateUIUpdate
)
140 EVT_UPDATE_UI(RadioPage_Selection
, RadioWidgetsPage::OnUpdateUISelection
)
142 EVT_RADIOBOX(RadioPage_Radio
, RadioWidgetsPage::OnRadioBox
)
144 EVT_CHECKBOX(-1, RadioWidgetsPage::OnCheckOrRadioBox
)
145 EVT_RADIOBOX(-1, RadioWidgetsPage::OnCheckOrRadioBox
)
148 // ============================================================================
150 // ============================================================================
152 IMPLEMENT_WIDGETS_PAGE(RadioWidgetsPage
, _T("Radio"));
154 RadioWidgetsPage::RadioWidgetsPage(wxNotebook
*notebook
,
155 wxImageList
*imaglist
)
156 : WidgetsPage(notebook
)
158 imaglist
->Add(wxBitmap(radio_xpm
));
161 m_chkVert
= (wxCheckBox
*)NULL
;
165 m_textLabel
= (wxTextCtrl
*)NULL
;
168 m_radioDir
= (wxRadioBox
*)NULL
;
169 m_sizerRadio
= (wxSizer
*)NULL
;
171 wxSizer
*sizerTop
= new wxBoxSizer(wxHORIZONTAL
);
174 wxStaticBox
*box
= new wxStaticBox(this, -1, _T("&Set style"));
176 wxSizer
*sizerLeft
= new wxStaticBoxSizer(box
, wxVERTICAL
);
178 m_chkVert
= CreateCheckBoxAndAddToSizer(sizerLeft
, _T("&Vertical layout"));
180 static const wxString layoutDir
[] =
187 m_radioDir
= new wxRadioBox(this, -1, _T("Numbering:"),
188 wxDefaultPosition
, wxDefaultSize
,
189 WXSIZEOF(layoutDir
), layoutDir
);
190 sizerLeft
->Add(m_radioDir
, 0, wxGROW
| wxALL
, 5);
192 // if it's not defined, we can't change the radiobox direction
193 #ifndef wxRA_LEFTTORIGHT
194 m_radioDir
->Disable();
195 #endif // wxRA_LEFTTORIGHT
198 sizerRow
= CreateSizerWithTextAndLabel(_T("&Major dimension"),
201 sizerLeft
->Add(sizerRow
, 0, wxGROW
| wxALL
, 5);
203 sizerRow
= CreateSizerWithTextAndLabel(_T("&Number of buttons"),
206 sizerLeft
->Add(sizerRow
, 0, wxGROW
| wxALL
, 5);
209 btn
= new wxButton(this, RadioPage_Update
, _T("&Update"));
210 sizerLeft
->Add(btn
, 0, wxALIGN_CENTRE_HORIZONTAL
| wxALL
, 5);
212 sizerLeft
->Add(5, 5, 0, wxGROW
| wxALL
, 5); // spacer
214 btn
= new wxButton(this, RadioPage_Reset
, _T("&Reset"));
215 sizerLeft
->Add(btn
, 0, wxALIGN_CENTRE_HORIZONTAL
| wxALL
, 15);
218 wxStaticBox
*box2
= new wxStaticBox(this, -1, _T("&Change parameters"));
219 wxSizer
*sizerMiddle
= new wxStaticBoxSizer(box2
, wxVERTICAL
);
221 sizerRow
= CreateSizerWithTextAndLabel(_T("Current selection"),
224 sizerMiddle
->Add(sizerRow
, 0, wxGROW
| wxALL
, 5);
226 sizerRow
= CreateSizerWithTextAndButton(RadioPage_Selection
,
227 _T("&Change selection"),
230 sizerMiddle
->Add(sizerRow
, 0, wxGROW
| wxALL
, 5);
232 sizerRow
= CreateSizerWithTextAndButton(RadioPage_Label
,
233 _T("&Label for box"),
236 sizerMiddle
->Add(sizerRow
, 0, wxGROW
| wxALL
, 5);
238 sizerRow
= CreateSizerWithTextAndButton(RadioPage_LabelBtn
,
239 _T("&Label for buttons"),
242 sizerMiddle
->Add(sizerRow
, 0, wxGROW
| wxALL
, 5);
245 wxSizer
*sizerRight
= new wxBoxSizer(wxHORIZONTAL
);
246 sizerRight
->SetMinSize(250, 0);
247 m_sizerRadio
= sizerRight
; // save it to modify it later
252 // the 3 panes panes compose the window
253 sizerTop
->Add(sizerLeft
, 0, wxGROW
| (wxALL
& ~wxLEFT
), 10);
254 sizerTop
->Add(sizerMiddle
, 1, wxGROW
| wxALL
, 10);
255 sizerTop
->Add(sizerRight
, 1, wxGROW
| (wxALL
& ~wxRIGHT
), 10);
257 // final initializations
264 RadioWidgetsPage::~RadioWidgetsPage()
268 // ----------------------------------------------------------------------------
270 // ----------------------------------------------------------------------------
272 void RadioWidgetsPage::Reset()
274 m_textMajorDim
->SetValue(wxString::Format(_T("%d"), DEFAULT_MAJOR_DIM
));
275 m_textNumBtns
->SetValue(wxString::Format(_T("%d"), DEFAULT_NUM_ENTRIES
));
276 m_textLabel
->SetValue(_T("I'm a radiobox"));
277 m_textLabelBtns
->SetValue(_T("item"));
279 m_chkVert
->SetValue(FALSE
);
280 m_radioDir
->SetSelection(RadioDir_Default
);
283 void RadioWidgetsPage::CreateRadio()
288 sel
= m_radio
->GetSelection();
290 m_sizerRadio
->Remove(m_radio
);
294 else // first time creation, no old selection to preserve
300 if ( !m_textNumBtns
->GetValue().ToULong(&count
) )
302 wxLogWarning(_T("Should have a valid number for number of items."));
304 // fall back to default
305 count
= DEFAULT_NUM_ENTRIES
;
308 unsigned long majorDim
;
309 if ( !m_textMajorDim
->GetValue().ToULong(&majorDim
) )
311 wxLogWarning(_T("Should have a valid major dimension number."));
313 // fall back to default
314 majorDim
= DEFAULT_MAJOR_DIM
;
317 wxString
*items
= new wxString
[count
];
319 wxString labelBtn
= m_textLabelBtns
->GetValue();
320 for ( size_t n
= 0; n
< count
; n
++ )
322 items
[n
] = wxString::Format(_T("%s %u"), labelBtn
.c_str(), n
+ 1);
325 int flags
= m_chkVert
->GetValue() ? wxRA_VERTICAL
328 #ifdef wxRA_LEFTTORIGHT
329 switch ( m_radioDir
->GetSelection() )
332 wxFAIL_MSG( _T("unexpected wxRadioBox layout direction") );
335 case RadioDir_Default
:
339 flags
|= wxRA_LEFTTORIGHT
;
343 flags
|= wxRA_TOPTOBOTTOM
;
346 #endif // wxRA_LEFTTORIGHT
348 m_radio
= new wxRadioBox(this, RadioPage_Radio
,
349 m_textLabel
->GetValue(),
350 wxDefaultPosition
, wxDefaultSize
,
357 if ( sel
>= 0 && (size_t)sel
< count
)
359 m_radio
->SetSelection(sel
);
362 m_sizerRadio
->Add(m_radio
, 1, wxGROW
);
363 m_sizerRadio
->Layout();
366 // ----------------------------------------------------------------------------
368 // ----------------------------------------------------------------------------
370 void RadioWidgetsPage::OnButtonReset(wxCommandEvent
& WXUNUSED(event
))
377 void RadioWidgetsPage::OnCheckOrRadioBox(wxCommandEvent
& event
)
382 void RadioWidgetsPage::OnRadioBox(wxCommandEvent
& event
)
384 int sel
= m_radio
->GetSelection();
386 wxLogMessage(_T("Radiobox selection changed, now %d"), sel
);
388 wxASSERT_MSG( sel
== event
.GetSelection(),
389 _T("selection should be the same in event and radiobox") );
391 m_textCurSel
->SetValue(wxString::Format(_T("%d"), sel
));
394 void RadioWidgetsPage::OnButtonRecreate(wxCommandEvent
& WXUNUSED(event
))
399 void RadioWidgetsPage::OnButtonSetLabel(wxCommandEvent
& WXUNUSED(event
))
401 m_radio
->wxControl::SetLabel(m_textLabel
->GetValue());
404 void RadioWidgetsPage::OnButtonSelection(wxCommandEvent
& WXUNUSED(event
))
407 if ( !m_textSel
->GetValue().ToULong(&sel
) ||
408 (sel
>= (size_t)m_radio
->GetCount()) )
410 wxLogWarning(_T("Invalid number specified as new selection."));
414 m_radio
->SetSelection(sel
);
418 void RadioWidgetsPage::OnUpdateUIUpdate(wxUpdateUIEvent
& event
)
421 event
.Enable( m_textNumBtns
->GetValue().ToULong(&n
) &&
422 m_textMajorDim
->GetValue().ToULong(&n
) );
425 void RadioWidgetsPage::OnUpdateUISelection(wxUpdateUIEvent
& event
)
428 event
.Enable( m_textSel
->GetValue().ToULong(&n
) &&
429 (n
< (size_t)m_radio
->GetCount()) );
432 void RadioWidgetsPage::OnUpdateUIReset(wxUpdateUIEvent
& event
)
434 // only enable it if something is not set to default
435 bool enable
= m_chkVert
->GetValue();
439 unsigned long numEntries
;
441 enable
= !m_textNumBtns
->GetValue().ToULong(&numEntries
) ||
442 numEntries
!= DEFAULT_NUM_ENTRIES
;
446 unsigned long majorDim
;
448 enable
= !m_textMajorDim
->GetValue().ToULong(&majorDim
) ||
449 majorDim
!= DEFAULT_MAJOR_DIM
;
453 event
.Enable(enable
);