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
= 3;
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 1, wxRA_SPECIFY_COLS
);
191 sizerLeft
->Add(m_radioDir
, 0, wxGROW
| wxALL
, 5);
193 // if it's not defined, we can't change the radiobox direction
194 #ifndef wxRA_LEFTTORIGHT
195 m_radioDir
->Disable();
196 #endif // wxRA_LEFTTORIGHT
199 sizerRow
= CreateSizerWithTextAndLabel(_T("&Major dimension:"),
202 sizerLeft
->Add(sizerRow
, 0, wxGROW
| wxALL
, 5);
204 sizerRow
= CreateSizerWithTextAndLabel(_T("&Number of buttons:"),
207 sizerLeft
->Add(sizerRow
, 0, wxGROW
| wxALL
, 5);
210 btn
= new wxButton(this, RadioPage_Update
, _T("&Update"));
211 sizerLeft
->Add(btn
, 0, wxALIGN_CENTRE_HORIZONTAL
| wxALL
, 5);
213 sizerLeft
->Add(5, 5, 0, wxGROW
| wxALL
, 5); // spacer
215 btn
= new wxButton(this, RadioPage_Reset
, _T("&Reset"));
216 sizerLeft
->Add(btn
, 0, wxALIGN_CENTRE_HORIZONTAL
| wxALL
, 15);
219 wxStaticBox
*box2
= new wxStaticBox(this, -1, _T("&Change parameters"));
220 wxSizer
*sizerMiddle
= new wxStaticBoxSizer(box2
, wxVERTICAL
);
222 sizerRow
= CreateSizerWithTextAndLabel(_T("Current selection:"),
225 sizerMiddle
->Add(sizerRow
, 0, wxGROW
| wxALL
, 5);
227 sizerRow
= CreateSizerWithTextAndButton(RadioPage_Selection
,
228 _T("&Change selection:"),
231 sizerMiddle
->Add(sizerRow
, 0, wxGROW
| wxALL
, 5);
233 sizerRow
= CreateSizerWithTextAndButton(RadioPage_Label
,
234 _T("&Label for box:"),
237 sizerMiddle
->Add(sizerRow
, 0, wxGROW
| wxALL
, 5);
239 sizerRow
= CreateSizerWithTextAndButton(RadioPage_LabelBtn
,
240 _T("&Label for buttons:"),
243 sizerMiddle
->Add(sizerRow
, 0, wxGROW
| wxALL
, 5);
246 wxSizer
*sizerRight
= new wxBoxSizer(wxHORIZONTAL
);
247 sizerRight
->SetMinSize(150, 0);
248 m_sizerRadio
= sizerRight
; // save it to modify it later
253 // the 3 panes panes compose the window
254 sizerTop
->Add(sizerLeft
, 0, wxGROW
| (wxALL
& ~wxLEFT
), 10);
255 sizerTop
->Add(sizerMiddle
, 1, wxGROW
| wxALL
, 10);
256 sizerTop
->Add(sizerRight
, 0, wxGROW
| (wxALL
& ~wxRIGHT
), 10);
258 // final initializations
265 RadioWidgetsPage::~RadioWidgetsPage()
269 // ----------------------------------------------------------------------------
271 // ----------------------------------------------------------------------------
273 void RadioWidgetsPage::Reset()
275 m_textMajorDim
->SetValue(wxString::Format(_T("%d"), DEFAULT_MAJOR_DIM
));
276 m_textNumBtns
->SetValue(wxString::Format(_T("%d"), 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
->Remove(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 %u"), labelBtn
.c_str(), n
+ 1);
326 int flags
= m_chkVert
->GetValue() ? wxRA_VERTICAL
329 #ifdef wxRA_LEFTTORIGHT
330 switch ( m_radioDir
->GetSelection() )
333 wxFAIL_MSG( _T("unexpected wxRadioBox layout direction") );
336 case RadioDir_Default
:
340 flags
|= wxRA_LEFTTORIGHT
;
344 flags
|= wxRA_TOPTOBOTTOM
;
347 #endif // wxRA_LEFTTORIGHT
349 m_radio
= new wxRadioBox(this, RadioPage_Radio
,
350 m_textLabel
->GetValue(),
351 wxDefaultPosition
, wxDefaultSize
,
358 if ( sel
>= 0 && (size_t)sel
< count
)
360 m_radio
->SetSelection(sel
);
363 m_sizerRadio
->Add(m_radio
, 1, wxGROW
);
364 m_sizerRadio
->Layout();
367 // ----------------------------------------------------------------------------
369 // ----------------------------------------------------------------------------
371 void RadioWidgetsPage::OnButtonReset(wxCommandEvent
& WXUNUSED(event
))
378 void RadioWidgetsPage::OnCheckOrRadioBox(wxCommandEvent
& event
)
383 void RadioWidgetsPage::OnRadioBox(wxCommandEvent
& event
)
385 int sel
= m_radio
->GetSelection();
387 wxLogMessage(_T("Radiobox selection changed, now %d"), sel
);
389 wxASSERT_MSG( sel
== event
.GetSelection(),
390 _T("selection should be the same in event and radiobox") );
392 m_textCurSel
->SetValue(wxString::Format(_T("%d"), sel
));
395 void RadioWidgetsPage::OnButtonRecreate(wxCommandEvent
& WXUNUSED(event
))
400 void RadioWidgetsPage::OnButtonSetLabel(wxCommandEvent
& WXUNUSED(event
))
402 m_radio
->wxControl::SetLabel(m_textLabel
->GetValue());
405 void RadioWidgetsPage::OnButtonSelection(wxCommandEvent
& WXUNUSED(event
))
408 if ( !m_textSel
->GetValue().ToULong(&sel
) ||
409 (sel
>= (size_t)m_radio
->GetCount()) )
411 wxLogWarning(_T("Invalid number specified as new selection."));
415 m_radio
->SetSelection(sel
);
419 void RadioWidgetsPage::OnUpdateUIUpdate(wxUpdateUIEvent
& event
)
422 event
.Enable( m_textNumBtns
->GetValue().ToULong(&n
) &&
423 m_textMajorDim
->GetValue().ToULong(&n
) );
426 void RadioWidgetsPage::OnUpdateUISelection(wxUpdateUIEvent
& event
)
429 event
.Enable( m_textSel
->GetValue().ToULong(&n
) &&
430 (n
< (size_t)m_radio
->GetCount()) );
433 void RadioWidgetsPage::OnUpdateUIReset(wxUpdateUIEvent
& event
)
435 // only enable it if something is not set to default
436 bool enable
= m_chkVert
->GetValue();
440 unsigned long numEntries
;
442 enable
= !m_textNumBtns
->GetValue().ToULong(&numEntries
) ||
443 numEntries
!= DEFAULT_NUM_ENTRIES
;
447 unsigned long majorDim
;
449 enable
= !m_textMajorDim
->GetValue().ToULong(&majorDim
) ||
450 majorDim
!= DEFAULT_MAJOR_DIM
;
454 event
.Enable(enable
);