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"
27 // for all others, include the necessary headers
31 #include "wx/bitmap.h"
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"
43 #include "icons/radiobox.xpm"
45 // ----------------------------------------------------------------------------
47 // ----------------------------------------------------------------------------
52 RadioPage_Reset
= 100,
60 // layout direction radiobox selections
68 // default values for the number of radiobox items
69 static const unsigned int DEFAULT_NUM_ENTRIES
= 12;
70 static const unsigned int DEFAULT_MAJOR_DIM
= 3;
72 // ----------------------------------------------------------------------------
74 // ----------------------------------------------------------------------------
76 class RadioWidgetsPage
: public WidgetsPage
79 RadioWidgetsPage(wxNotebook
*notebook
, wxImageList
*imaglist
);
80 virtual ~RadioWidgetsPage(){};
84 void OnCheckOrRadioBox(wxCommandEvent
& event
);
85 void OnRadioBox(wxCommandEvent
& event
);
87 void OnButtonReset(wxCommandEvent
& event
);
88 void OnButtonRecreate(wxCommandEvent
& event
);
90 void OnButtonSelection(wxCommandEvent
& event
);
91 void OnButtonSetLabel(wxCommandEvent
& event
);
93 void OnUpdateUIReset(wxUpdateUIEvent
& event
);
94 void OnUpdateUIUpdate(wxUpdateUIEvent
& event
);
95 void OnUpdateUISelection(wxUpdateUIEvent
& event
);
97 // reset the wxRadioBox parameters
100 // (re)create the wxRadioBox
106 // the check/radio boxes for styles
107 wxCheckBox
*m_chkVert
;
108 wxRadioBox
*m_radioDir
;
110 // the gauge itself and the sizer it is in
112 wxSizer
*m_sizerRadio
;
114 // the text entries for command parameters
115 wxTextCtrl
*m_textNumBtns
,
123 DECLARE_EVENT_TABLE()
124 DECLARE_WIDGETS_PAGE(RadioWidgetsPage
)
127 // ----------------------------------------------------------------------------
129 // ----------------------------------------------------------------------------
131 BEGIN_EVENT_TABLE(RadioWidgetsPage
, WidgetsPage
)
132 EVT_BUTTON(RadioPage_Reset
, RadioWidgetsPage::OnButtonReset
)
134 EVT_BUTTON(RadioPage_Update
, RadioWidgetsPage::OnButtonRecreate
)
135 EVT_BUTTON(RadioPage_LabelBtn
, RadioWidgetsPage::OnButtonRecreate
)
137 EVT_BUTTON(RadioPage_Selection
, RadioWidgetsPage::OnButtonSelection
)
138 EVT_BUTTON(RadioPage_Label
, RadioWidgetsPage::OnButtonSetLabel
)
140 EVT_UPDATE_UI(RadioPage_Update
, RadioWidgetsPage::OnUpdateUIUpdate
)
141 EVT_UPDATE_UI(RadioPage_Selection
, RadioWidgetsPage::OnUpdateUISelection
)
143 EVT_RADIOBOX(RadioPage_Radio
, RadioWidgetsPage::OnRadioBox
)
145 EVT_CHECKBOX(wxID_ANY
, RadioWidgetsPage::OnCheckOrRadioBox
)
146 EVT_RADIOBOX(wxID_ANY
, RadioWidgetsPage::OnCheckOrRadioBox
)
149 // ============================================================================
151 // ============================================================================
153 IMPLEMENT_WIDGETS_PAGE(RadioWidgetsPage
, _T("Radio"));
155 RadioWidgetsPage::RadioWidgetsPage(wxNotebook
*notebook
,
156 wxImageList
*imaglist
)
157 : WidgetsPage(notebook
)
159 imaglist
->Add(wxBitmap(radio_xpm
));
162 m_chkVert
= (wxCheckBox
*)NULL
;
166 m_textLabel
= (wxTextCtrl
*)NULL
;
169 m_radioDir
= (wxRadioBox
*)NULL
;
170 m_sizerRadio
= (wxSizer
*)NULL
;
172 wxSizer
*sizerTop
= new wxBoxSizer(wxHORIZONTAL
);
175 wxStaticBox
*box
= new wxStaticBox(this, wxID_ANY
, _T("&Set style"));
177 wxSizer
*sizerLeft
= new wxStaticBoxSizer(box
, wxVERTICAL
);
179 m_chkVert
= CreateCheckBoxAndAddToSizer(sizerLeft
, _T("&Vertical layout"));
181 static const wxString layoutDir
[] =
188 m_radioDir
= new wxRadioBox(this, wxID_ANY
, _T("Numbering:"),
189 wxDefaultPosition
, wxDefaultSize
,
190 WXSIZEOF(layoutDir
), layoutDir
,
191 1, wxRA_SPECIFY_COLS
);
192 sizerLeft
->Add(m_radioDir
, 0, wxGROW
| wxALL
, 5);
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
200 sizerRow
= CreateSizerWithTextAndLabel(_T("&Major dimension:"),
203 sizerLeft
->Add(sizerRow
, 0, wxGROW
| wxALL
, 5);
205 sizerRow
= CreateSizerWithTextAndLabel(_T("&Number of buttons:"),
208 sizerLeft
->Add(sizerRow
, 0, wxGROW
| wxALL
, 5);
211 btn
= new wxButton(this, RadioPage_Update
, _T("&Update"));
212 sizerLeft
->Add(btn
, 0, wxALIGN_CENTRE_HORIZONTAL
| wxALL
, 5);
214 sizerLeft
->Add(5, 5, 0, wxGROW
| wxALL
, 5); // spacer
216 btn
= new wxButton(this, RadioPage_Reset
, _T("&Reset"));
217 sizerLeft
->Add(btn
, 0, wxALIGN_CENTRE_HORIZONTAL
| wxALL
, 15);
220 wxStaticBox
*box2
= new wxStaticBox(this, wxID_ANY
, _T("&Change parameters"));
221 wxSizer
*sizerMiddle
= new wxStaticBoxSizer(box2
, wxVERTICAL
);
223 sizerRow
= CreateSizerWithTextAndLabel(_T("Current selection:"),
226 sizerMiddle
->Add(sizerRow
, 0, wxGROW
| wxALL
, 5);
228 sizerRow
= CreateSizerWithTextAndButton(RadioPage_Selection
,
229 _T("&Change selection:"),
232 sizerMiddle
->Add(sizerRow
, 0, wxGROW
| wxALL
, 5);
234 sizerRow
= CreateSizerWithTextAndButton(RadioPage_Label
,
235 _T("&Label for box:"),
238 sizerMiddle
->Add(sizerRow
, 0, wxGROW
| wxALL
, 5);
240 sizerRow
= CreateSizerWithTextAndButton(RadioPage_LabelBtn
,
241 _T("&Label for buttons:"),
244 sizerMiddle
->Add(sizerRow
, 0, wxGROW
| wxALL
, 5);
247 wxSizer
*sizerRight
= new wxBoxSizer(wxHORIZONTAL
);
248 sizerRight
->SetMinSize(150, 0);
249 m_sizerRadio
= sizerRight
; // save it to modify it later
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);
257 sizerTop
->Add(sizerRight
, 0, wxGROW
| (wxALL
& ~wxRIGHT
), 10);
259 // final initializations
265 // ----------------------------------------------------------------------------
267 // ----------------------------------------------------------------------------
269 void RadioWidgetsPage::Reset()
271 m_textMajorDim
->SetValue(wxString::Format(_T("%u"), DEFAULT_MAJOR_DIM
));
272 m_textNumBtns
->SetValue(wxString::Format(_T("%u"), DEFAULT_NUM_ENTRIES
));
273 m_textLabel
->SetValue(_T("I'm a radiobox"));
274 m_textLabelBtns
->SetValue(_T("item"));
276 m_chkVert
->SetValue(false);
277 m_radioDir
->SetSelection(RadioDir_Default
);
280 void RadioWidgetsPage::CreateRadio()
285 sel
= m_radio
->GetSelection();
287 m_sizerRadio
->Detach( m_radio
);
291 else // first time creation, no old selection to preserve
297 if ( !m_textNumBtns
->GetValue().ToULong(&count
) )
299 wxLogWarning(_T("Should have a valid number for number of items."));
301 // fall back to default
302 count
= DEFAULT_NUM_ENTRIES
;
305 unsigned long majorDim
;
306 if ( !m_textMajorDim
->GetValue().ToULong(&majorDim
) )
308 wxLogWarning(_T("Should have a valid major dimension number."));
310 // fall back to default
311 majorDim
= DEFAULT_MAJOR_DIM
;
314 wxString
*items
= new wxString
[count
];
316 wxString labelBtn
= m_textLabelBtns
->GetValue();
317 for ( size_t n
= 0; n
< count
; n
++ )
319 items
[n
] = wxString::Format(_T("%s %lu"),
320 labelBtn
.c_str(), (unsigned long)n
+ 1);
323 int flags
= m_chkVert
->GetValue() ? wxRA_VERTICAL
326 #ifdef wxRA_LEFTTORIGHT
327 switch ( m_radioDir
->GetSelection() )
330 wxFAIL_MSG( _T("unexpected wxRadioBox layout direction") );
333 case RadioDir_Default
:
337 flags
|= wxRA_LEFTTORIGHT
;
341 flags
|= wxRA_TOPTOBOTTOM
;
344 #endif // wxRA_LEFTTORIGHT
346 m_radio
= new wxRadioBox(this, RadioPage_Radio
,
347 m_textLabel
->GetValue(),
348 wxDefaultPosition
, wxDefaultSize
,
355 if ( sel
>= 0 && (size_t)sel
< count
)
357 m_radio
->SetSelection(sel
);
360 m_sizerRadio
->Add(m_radio
, 1, wxGROW
);
361 m_sizerRadio
->Layout();
364 // ----------------------------------------------------------------------------
366 // ----------------------------------------------------------------------------
368 void RadioWidgetsPage::OnButtonReset(wxCommandEvent
& WXUNUSED(event
))
375 void RadioWidgetsPage::OnCheckOrRadioBox(wxCommandEvent
& WXUNUSED(event
))
380 void RadioWidgetsPage::OnRadioBox(wxCommandEvent
& event
)
382 int sel
= m_radio
->GetSelection();
383 int event_sel
= event
.GetSelection();
384 wxUnusedVar(event_sel
);
386 wxLogMessage(_T("Radiobox selection changed, now %d"), sel
);
388 wxASSERT_MSG( sel
== event_sel
,
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
);