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/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(-1, RadioWidgetsPage::OnCheckOrRadioBox
)
146 EVT_RADIOBOX(-1, 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, -1, _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, -1, _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, -1, _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
266 RadioWidgetsPage::~RadioWidgetsPage()
270 // ----------------------------------------------------------------------------
272 // ----------------------------------------------------------------------------
274 void RadioWidgetsPage::Reset()
276 m_textMajorDim
->SetValue(wxString::Format(_T("%u"), DEFAULT_MAJOR_DIM
));
277 m_textNumBtns
->SetValue(wxString::Format(_T("%u"), DEFAULT_NUM_ENTRIES
));
278 m_textLabel
->SetValue(_T("I'm a radiobox"));
279 m_textLabelBtns
->SetValue(_T("item"));
281 m_chkVert
->SetValue(FALSE
);
282 m_radioDir
->SetSelection(RadioDir_Default
);
285 void RadioWidgetsPage::CreateRadio()
290 sel
= m_radio
->GetSelection();
292 m_sizerRadio
->Detach( m_radio
);
296 else // first time creation, no old selection to preserve
302 if ( !m_textNumBtns
->GetValue().ToULong(&count
) )
304 wxLogWarning(_T("Should have a valid number for number of items."));
306 // fall back to default
307 count
= DEFAULT_NUM_ENTRIES
;
310 unsigned long majorDim
;
311 if ( !m_textMajorDim
->GetValue().ToULong(&majorDim
) )
313 wxLogWarning(_T("Should have a valid major dimension number."));
315 // fall back to default
316 majorDim
= DEFAULT_MAJOR_DIM
;
319 wxString
*items
= new wxString
[count
];
321 wxString labelBtn
= m_textLabelBtns
->GetValue();
322 for ( size_t n
= 0; n
< count
; n
++ )
324 items
[n
] = wxString::Format(_T("%s %lu"),
325 labelBtn
.c_str(), (unsigned long)n
+ 1);
328 int flags
= m_chkVert
->GetValue() ? wxRA_VERTICAL
331 #ifdef wxRA_LEFTTORIGHT
332 switch ( m_radioDir
->GetSelection() )
335 wxFAIL_MSG( _T("unexpected wxRadioBox layout direction") );
338 case RadioDir_Default
:
342 flags
|= wxRA_LEFTTORIGHT
;
346 flags
|= wxRA_TOPTOBOTTOM
;
349 #endif // wxRA_LEFTTORIGHT
351 m_radio
= new wxRadioBox(this, RadioPage_Radio
,
352 m_textLabel
->GetValue(),
353 wxDefaultPosition
, wxDefaultSize
,
360 if ( sel
>= 0 && (size_t)sel
< count
)
362 m_radio
->SetSelection(sel
);
365 m_sizerRadio
->Add(m_radio
, 1, wxGROW
);
366 m_sizerRadio
->Layout();
369 // ----------------------------------------------------------------------------
371 // ----------------------------------------------------------------------------
373 void RadioWidgetsPage::OnButtonReset(wxCommandEvent
& WXUNUSED(event
))
380 void RadioWidgetsPage::OnCheckOrRadioBox(wxCommandEvent
& event
)
385 void RadioWidgetsPage::OnRadioBox(wxCommandEvent
& event
)
387 int sel
= m_radio
->GetSelection();
389 wxLogMessage(_T("Radiobox selection changed, now %d"), sel
);
391 wxASSERT_MSG( sel
== event
.GetSelection(),
392 _T("selection should be the same in event and radiobox") );
394 m_textCurSel
->SetValue(wxString::Format(_T("%d"), sel
));
397 void RadioWidgetsPage::OnButtonRecreate(wxCommandEvent
& WXUNUSED(event
))
402 void RadioWidgetsPage::OnButtonSetLabel(wxCommandEvent
& WXUNUSED(event
))
404 m_radio
->wxControl::SetLabel(m_textLabel
->GetValue());
407 void RadioWidgetsPage::OnButtonSelection(wxCommandEvent
& WXUNUSED(event
))
410 if ( !m_textSel
->GetValue().ToULong(&sel
) ||
411 (sel
>= (size_t)m_radio
->GetCount()) )
413 wxLogWarning(_T("Invalid number specified as new selection."));
417 m_radio
->SetSelection(sel
);
421 void RadioWidgetsPage::OnUpdateUIUpdate(wxUpdateUIEvent
& event
)
424 event
.Enable( m_textNumBtns
->GetValue().ToULong(&n
) &&
425 m_textMajorDim
->GetValue().ToULong(&n
) );
428 void RadioWidgetsPage::OnUpdateUISelection(wxUpdateUIEvent
& event
)
431 event
.Enable( m_textSel
->GetValue().ToULong(&n
) &&
432 (n
< (size_t)m_radio
->GetCount()) );
435 void RadioWidgetsPage::OnUpdateUIReset(wxUpdateUIEvent
& event
)
437 // only enable it if something is not set to default
438 bool enable
= m_chkVert
->GetValue();
442 unsigned long numEntries
;
444 enable
= !m_textNumBtns
->GetValue().ToULong(&numEntries
) ||
445 numEntries
!= DEFAULT_NUM_ENTRIES
;
449 unsigned long majorDim
;
451 enable
= !m_textMajorDim
->GetValue().ToULong(&majorDim
) ||
452 majorDim
!= DEFAULT_MAJOR_DIM
;
456 event
.Enable(enable
);