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 RadioWidgetsPage::~RadioWidgetsPage()
269 // ----------------------------------------------------------------------------
271 // ----------------------------------------------------------------------------
273 void RadioWidgetsPage::Reset()
275 m_textMajorDim
->SetValue(wxString::Format(_T("%u"), DEFAULT_MAJOR_DIM
));
276 m_textNumBtns
->SetValue(wxString::Format(_T("%u"), DEFAULT_NUM_ENTRIES
));
277 m_textLabel
->SetValue(_T("I'm a radiobox"));
278 m_textLabelBtns
->SetValue(_T("item"));
280 m_chkVert
->SetValue(false);
281 m_radioDir
->SetSelection(RadioDir_Default
);
284 void RadioWidgetsPage::CreateRadio()
289 sel
= m_radio
->GetSelection();
291 m_sizerRadio
->Detach( m_radio
);
295 else // first time creation, no old selection to preserve
301 if ( !m_textNumBtns
->GetValue().ToULong(&count
) )
303 wxLogWarning(_T("Should have a valid number for number of items."));
305 // fall back to default
306 count
= DEFAULT_NUM_ENTRIES
;
309 unsigned long majorDim
;
310 if ( !m_textMajorDim
->GetValue().ToULong(&majorDim
) )
312 wxLogWarning(_T("Should have a valid major dimension number."));
314 // fall back to default
315 majorDim
= DEFAULT_MAJOR_DIM
;
318 wxString
*items
= new wxString
[count
];
320 wxString labelBtn
= m_textLabelBtns
->GetValue();
321 for ( size_t n
= 0; n
< count
; n
++ )
323 items
[n
] = wxString::Format(_T("%s %lu"),
324 labelBtn
.c_str(), (unsigned long)n
+ 1);
327 int flags
= m_chkVert
->GetValue() ? wxRA_VERTICAL
330 #ifdef wxRA_LEFTTORIGHT
331 switch ( m_radioDir
->GetSelection() )
334 wxFAIL_MSG( _T("unexpected wxRadioBox layout direction") );
337 case RadioDir_Default
:
341 flags
|= wxRA_LEFTTORIGHT
;
345 flags
|= wxRA_TOPTOBOTTOM
;
348 #endif // wxRA_LEFTTORIGHT
350 m_radio
= new wxRadioBox(this, RadioPage_Radio
,
351 m_textLabel
->GetValue(),
352 wxDefaultPosition
, wxDefaultSize
,
359 if ( sel
>= 0 && (size_t)sel
< count
)
361 m_radio
->SetSelection(sel
);
364 m_sizerRadio
->Add(m_radio
, 1, wxGROW
);
365 m_sizerRadio
->Layout();
368 // ----------------------------------------------------------------------------
370 // ----------------------------------------------------------------------------
372 void RadioWidgetsPage::OnButtonReset(wxCommandEvent
& WXUNUSED(event
))
379 void RadioWidgetsPage::OnCheckOrRadioBox(wxCommandEvent
& WXUNUSED(event
))
384 void RadioWidgetsPage::OnRadioBox(wxCommandEvent
& event
)
386 int sel
= m_radio
->GetSelection();
387 int event_sel
= event
.GetSelection();
388 wxUnusedVar(event_sel
);
390 wxLogMessage(_T("Radiobox selection changed, now %d"), sel
);
392 wxASSERT_MSG( sel
== event_sel
,
393 _T("selection should be the same in event and radiobox") );
395 m_textCurSel
->SetValue(wxString::Format(_T("%d"), sel
));
398 void RadioWidgetsPage::OnButtonRecreate(wxCommandEvent
& WXUNUSED(event
))
403 void RadioWidgetsPage::OnButtonSetLabel(wxCommandEvent
& WXUNUSED(event
))
405 m_radio
->wxControl::SetLabel(m_textLabel
->GetValue());
408 void RadioWidgetsPage::OnButtonSelection(wxCommandEvent
& WXUNUSED(event
))
411 if ( !m_textSel
->GetValue().ToULong(&sel
) ||
412 (sel
>= (size_t)m_radio
->GetCount()) )
414 wxLogWarning(_T("Invalid number specified as new selection."));
418 m_radio
->SetSelection(sel
);
422 void RadioWidgetsPage::OnUpdateUIUpdate(wxUpdateUIEvent
& event
)
425 event
.Enable( m_textNumBtns
->GetValue().ToULong(&n
) &&
426 m_textMajorDim
->GetValue().ToULong(&n
) );
429 void RadioWidgetsPage::OnUpdateUISelection(wxUpdateUIEvent
& event
)
432 event
.Enable( m_textSel
->GetValue().ToULong(&n
) &&
433 (n
< (size_t)m_radio
->GetCount()) );
436 void RadioWidgetsPage::OnUpdateUIReset(wxUpdateUIEvent
& event
)
438 // only enable it if something is not set to default
439 bool enable
= m_chkVert
->GetValue();
443 unsigned long numEntries
;
445 enable
= !m_textNumBtns
->GetValue().ToULong(&numEntries
) ||
446 numEntries
!= DEFAULT_NUM_ENTRIES
;
450 unsigned long majorDim
;
452 enable
= !m_textMajorDim
->GetValue().ToULong(&majorDim
) ||
453 majorDim
!= DEFAULT_MAJOR_DIM
;
457 event
.Enable(enable
);