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"
29 // for all others, include the necessary headers
33 #include "wx/bitmap.h"
34 #include "wx/button.h"
35 #include "wx/checkbox.h"
36 #include "wx/radiobox.h"
37 #include "wx/statbox.h"
38 #include "wx/textctrl.h"
45 #include "icons/radiobox.xpm"
47 // ----------------------------------------------------------------------------
49 // ----------------------------------------------------------------------------
54 RadioPage_Reset
= wxID_HIGHEST
,
64 // layout direction radiobox selections
72 // default values for the number of radiobox items
73 static const unsigned int DEFAULT_NUM_ENTRIES
= 12;
74 static const unsigned int DEFAULT_MAJOR_DIM
= 3;
76 // this item is enabled/disabled shown/hidden by the test checkboxes
77 static const int TEST_BUTTON
= 1;
79 // ----------------------------------------------------------------------------
81 // ----------------------------------------------------------------------------
83 class RadioWidgetsPage
: public WidgetsPage
86 RadioWidgetsPage(WidgetsBookCtrl
*book
, wxImageList
*imaglist
);
87 virtual ~RadioWidgetsPage(){};
89 virtual wxControl
*GetWidget() const { return m_radio
; }
90 virtual void RecreateWidget() { CreateRadio(); }
92 // lazy creation of the content
93 virtual void CreateContent();
97 void OnCheckOrRadioBox(wxCommandEvent
& event
);
98 void OnRadioBox(wxCommandEvent
& event
);
100 void OnButtonReset(wxCommandEvent
& event
);
101 void OnButtonRecreate(wxCommandEvent
& event
);
103 void OnButtonSelection(wxCommandEvent
& event
);
104 void OnButtonSetLabel(wxCommandEvent
& event
);
106 void OnEnableItem(wxCommandEvent
& event
);
107 void OnShowItem(wxCommandEvent
& event
);
109 void OnUpdateUIReset(wxUpdateUIEvent
& event
);
110 void OnUpdateUIUpdate(wxUpdateUIEvent
& event
);
111 void OnUpdateUISelection(wxUpdateUIEvent
& event
);
112 void OnUpdateUIEnableItem(wxUpdateUIEvent
& event
);
113 void OnUpdateUIShowItem(wxUpdateUIEvent
& event
);
115 // reset the wxRadioBox parameters
118 // (re)create the wxRadioBox
124 // the check/radio boxes for styles
125 wxCheckBox
*m_chkSpecifyRows
;
126 wxCheckBox
*m_chkEnableItem
;
127 wxCheckBox
*m_chkShowItem
;
128 wxRadioBox
*m_radioDir
;
130 // the gauge itself and the sizer it is in
132 wxSizer
*m_sizerRadio
;
134 // the text entries for command parameters
135 wxTextCtrl
*m_textNumBtns
,
143 DECLARE_EVENT_TABLE()
144 DECLARE_WIDGETS_PAGE(RadioWidgetsPage
)
147 // ----------------------------------------------------------------------------
149 // ----------------------------------------------------------------------------
151 BEGIN_EVENT_TABLE(RadioWidgetsPage
, WidgetsPage
)
152 EVT_BUTTON(RadioPage_Reset
, RadioWidgetsPage::OnButtonReset
)
154 EVT_BUTTON(RadioPage_Update
, RadioWidgetsPage::OnButtonRecreate
)
155 EVT_BUTTON(RadioPage_LabelBtn
, RadioWidgetsPage::OnButtonRecreate
)
157 EVT_BUTTON(RadioPage_Selection
, RadioWidgetsPage::OnButtonSelection
)
158 EVT_BUTTON(RadioPage_Label
, RadioWidgetsPage::OnButtonSetLabel
)
160 EVT_UPDATE_UI(RadioPage_Update
, RadioWidgetsPage::OnUpdateUIUpdate
)
161 EVT_UPDATE_UI(RadioPage_Selection
, RadioWidgetsPage::OnUpdateUISelection
)
163 EVT_RADIOBOX(RadioPage_Radio
, RadioWidgetsPage::OnRadioBox
)
165 EVT_CHECKBOX(RadioPage_EnableItem
, RadioWidgetsPage::OnEnableItem
)
166 EVT_CHECKBOX(RadioPage_ShowItem
, RadioWidgetsPage::OnShowItem
)
168 EVT_UPDATE_UI(RadioPage_EnableItem
, RadioWidgetsPage::OnUpdateUIEnableItem
)
169 EVT_UPDATE_UI(RadioPage_ShowItem
, RadioWidgetsPage::OnUpdateUIShowItem
)
171 EVT_CHECKBOX(wxID_ANY
, RadioWidgetsPage::OnCheckOrRadioBox
)
172 EVT_RADIOBOX(wxID_ANY
, RadioWidgetsPage::OnCheckOrRadioBox
)
175 // ============================================================================
177 // ============================================================================
179 #if defined(__WXUNIVERSAL__)
180 #define FAMILY_CTRLS UNIVERSAL_CTRLS
182 #define FAMILY_CTRLS NATIVE_CTRLS
185 IMPLEMENT_WIDGETS_PAGE(RadioWidgetsPage
, wxT("Radio"),
186 FAMILY_CTRLS
| WITH_ITEMS_CTRLS
189 RadioWidgetsPage::RadioWidgetsPage(WidgetsBookCtrl
*book
,
190 wxImageList
*imaglist
)
191 : WidgetsPage(book
, imaglist
, radio_xpm
)
194 m_chkSpecifyRows
= (wxCheckBox
*)NULL
;
195 m_chkEnableItem
= (wxCheckBox
*)NULL
;
196 m_chkShowItem
= (wxCheckBox
*)NULL
;
200 m_textLabel
= (wxTextCtrl
*)NULL
;
203 m_radioDir
= (wxRadioBox
*)NULL
;
204 m_sizerRadio
= (wxSizer
*)NULL
;
207 void RadioWidgetsPage::CreateContent()
209 wxSizer
*sizerTop
= new wxBoxSizer(wxHORIZONTAL
);
212 wxStaticBox
*box
= new wxStaticBox(this, wxID_ANY
, wxT("&Set style"));
214 wxSizer
*sizerLeft
= new wxStaticBoxSizer(box
, wxVERTICAL
);
216 m_chkSpecifyRows
= CreateCheckBoxAndAddToSizer
219 "Major specifies &rows count"
222 static const wxString layoutDir
[] =
225 wxT("left to right"),
229 m_radioDir
= new wxRadioBox(this, wxID_ANY
, wxT("Numbering:"),
230 wxDefaultPosition
, wxDefaultSize
,
231 WXSIZEOF(layoutDir
), layoutDir
,
232 1, wxRA_SPECIFY_COLS
);
233 sizerLeft
->Add(m_radioDir
, 0, wxGROW
| wxALL
, 5);
235 // if it's not defined, we can't change the radiobox direction
236 #ifndef wxRA_LEFTTORIGHT
237 m_radioDir
->Disable();
238 #endif // wxRA_LEFTTORIGHT
241 sizerRow
= CreateSizerWithTextAndLabel(wxT("&Major dimension:"),
244 sizerLeft
->Add(sizerRow
, 0, wxGROW
| wxALL
, 5);
246 sizerRow
= CreateSizerWithTextAndLabel(wxT("&Number of buttons:"),
249 sizerLeft
->Add(sizerRow
, 0, wxGROW
| wxALL
, 5);
252 btn
= new wxButton(this, RadioPage_Update
, wxT("&Update"));
253 sizerLeft
->Add(btn
, 0, wxALIGN_CENTRE_HORIZONTAL
| wxALL
, 5);
255 sizerLeft
->Add(5, 5, 0, wxGROW
| wxALL
, 5); // spacer
257 btn
= new wxButton(this, RadioPage_Reset
, wxT("&Reset"));
258 sizerLeft
->Add(btn
, 0, wxALIGN_CENTRE_HORIZONTAL
| wxALL
, 15);
261 wxStaticBox
*box2
= new wxStaticBox(this, wxID_ANY
, wxT("&Change parameters"));
262 wxSizer
*sizerMiddle
= new wxStaticBoxSizer(box2
, wxVERTICAL
);
264 sizerRow
= CreateSizerWithTextAndLabel(wxT("Current selection:"),
267 sizerMiddle
->Add(sizerRow
, 0, wxGROW
| wxALL
, 5);
269 sizerRow
= CreateSizerWithTextAndButton(RadioPage_Selection
,
270 wxT("&Change selection:"),
273 sizerMiddle
->Add(sizerRow
, 0, wxGROW
| wxALL
, 5);
275 sizerRow
= CreateSizerWithTextAndButton(RadioPage_Label
,
276 wxT("&Label for box:"),
279 sizerMiddle
->Add(sizerRow
, 0, wxGROW
| wxALL
, 5);
281 sizerRow
= CreateSizerWithTextAndButton(RadioPage_LabelBtn
,
282 wxT("&Label for buttons:"),
285 sizerMiddle
->Add(sizerRow
, 0, wxGROW
| wxALL
, 5);
287 m_chkEnableItem
= CreateCheckBoxAndAddToSizer(sizerMiddle
,
288 wxT("Disable &2nd item"),
289 RadioPage_EnableItem
);
290 m_chkShowItem
= CreateCheckBoxAndAddToSizer(sizerMiddle
,
291 wxT("Hide 2nd &item"),
295 wxSizer
*sizerRight
= new wxBoxSizer(wxHORIZONTAL
);
296 sizerRight
->SetMinSize(150, 0);
297 m_sizerRadio
= sizerRight
; // save it to modify it later
302 // the 3 panes panes compose the window
303 sizerTop
->Add(sizerLeft
, 0, wxGROW
| (wxALL
& ~wxLEFT
), 10);
304 sizerTop
->Add(sizerMiddle
, 1, wxGROW
| wxALL
, 10);
305 sizerTop
->Add(sizerRight
, 0, wxGROW
| (wxALL
& ~wxRIGHT
), 10);
307 // final initializations
311 // ----------------------------------------------------------------------------
313 // ----------------------------------------------------------------------------
315 void RadioWidgetsPage::Reset()
317 m_textMajorDim
->SetValue(wxString::Format(wxT("%u"), DEFAULT_MAJOR_DIM
));
318 m_textNumBtns
->SetValue(wxString::Format(wxT("%u"), DEFAULT_NUM_ENTRIES
));
319 m_textLabel
->SetValue(wxT("I'm a radiobox"));
320 m_textLabelBtns
->SetValue(wxT("item"));
322 m_chkSpecifyRows
->SetValue(false);
323 m_chkEnableItem
->SetValue(true);
324 m_chkShowItem
->SetValue(true);
325 m_radioDir
->SetSelection(RadioDir_Default
);
328 void RadioWidgetsPage::CreateRadio()
333 sel
= m_radio
->GetSelection();
335 m_sizerRadio
->Detach( m_radio
);
339 else // first time creation, no old selection to preserve
345 if ( !m_textNumBtns
->GetValue().ToULong(&count
) )
347 wxLogWarning(wxT("Should have a valid number for number of items."));
349 // fall back to default
350 count
= DEFAULT_NUM_ENTRIES
;
353 unsigned long majorDim
;
354 if ( !m_textMajorDim
->GetValue().ToULong(&majorDim
) )
356 wxLogWarning(wxT("Should have a valid major dimension number."));
358 // fall back to default
359 majorDim
= DEFAULT_MAJOR_DIM
;
362 wxString
*items
= new wxString
[count
];
364 wxString labelBtn
= m_textLabelBtns
->GetValue();
365 for ( size_t n
= 0; n
< count
; n
++ )
367 items
[n
] = wxString::Format(wxT("%s %lu"),
368 labelBtn
.c_str(), (unsigned long)n
+ 1);
371 int flags
= m_chkSpecifyRows
->GetValue() ? wxRA_SPECIFY_ROWS
374 flags
|= ms_defaultFlags
;
376 #ifdef wxRA_LEFTTORIGHT
377 switch ( m_radioDir
->GetSelection() )
380 wxFAIL_MSG( wxT("unexpected wxRadioBox layout direction") );
383 case RadioDir_Default
:
387 flags
|= wxRA_LEFTTORIGHT
;
391 flags
|= wxRA_TOPTOBOTTOM
;
394 #endif // wxRA_LEFTTORIGHT
396 m_radio
= new wxRadioBox(this, RadioPage_Radio
,
397 m_textLabel
->GetValue(),
398 wxDefaultPosition
, wxDefaultSize
,
405 if ( sel
>= 0 && (size_t)sel
< count
)
407 m_radio
->SetSelection(sel
);
410 m_sizerRadio
->Add(m_radio
, 1, wxGROW
);
411 m_sizerRadio
->Layout();
413 m_chkEnableItem
->SetValue(true);
414 m_chkEnableItem
->SetValue(true);
417 // ----------------------------------------------------------------------------
419 // ----------------------------------------------------------------------------
421 void RadioWidgetsPage::OnButtonReset(wxCommandEvent
& WXUNUSED(event
))
428 void RadioWidgetsPage::OnCheckOrRadioBox(wxCommandEvent
& WXUNUSED(event
))
433 void RadioWidgetsPage::OnRadioBox(wxCommandEvent
& event
)
435 int sel
= m_radio
->GetSelection();
436 int event_sel
= event
.GetSelection();
437 wxUnusedVar(event_sel
);
439 wxLogMessage(wxT("Radiobox selection changed, now %d"), sel
);
441 wxASSERT_MSG( sel
== event_sel
,
442 wxT("selection should be the same in event and radiobox") );
444 m_textCurSel
->SetValue(wxString::Format(wxT("%d"), sel
));
447 void RadioWidgetsPage::OnButtonRecreate(wxCommandEvent
& WXUNUSED(event
))
452 void RadioWidgetsPage::OnButtonSetLabel(wxCommandEvent
& WXUNUSED(event
))
454 m_radio
->wxControl::SetLabel(m_textLabel
->GetValue());
457 void RadioWidgetsPage::OnButtonSelection(wxCommandEvent
& WXUNUSED(event
))
460 if ( !m_textSel
->GetValue().ToULong(&sel
) ||
461 (sel
>= (size_t)m_radio
->GetCount()) )
463 wxLogWarning(wxT("Invalid number specified as new selection."));
467 m_radio
->SetSelection(sel
);
471 void RadioWidgetsPage::OnEnableItem(wxCommandEvent
& event
)
473 m_radio
->Enable(TEST_BUTTON
, event
.IsChecked());
476 void RadioWidgetsPage::OnShowItem(wxCommandEvent
& event
)
478 m_radio
->Show(TEST_BUTTON
, event
.IsChecked());
481 void RadioWidgetsPage::OnUpdateUIUpdate(wxUpdateUIEvent
& event
)
484 event
.Enable( m_textNumBtns
->GetValue().ToULong(&n
) &&
485 m_textMajorDim
->GetValue().ToULong(&n
) );
488 void RadioWidgetsPage::OnUpdateUISelection(wxUpdateUIEvent
& event
)
491 event
.Enable( m_textSel
->GetValue().ToULong(&n
) &&
492 (n
< (size_t)m_radio
->GetCount()) );
495 void RadioWidgetsPage::OnUpdateUIReset(wxUpdateUIEvent
& event
)
497 // only enable it if something is not set to default
498 bool enable
= m_chkSpecifyRows
->GetValue();
502 unsigned long numEntries
;
504 enable
= !m_textNumBtns
->GetValue().ToULong(&numEntries
) ||
505 numEntries
!= DEFAULT_NUM_ENTRIES
;
509 unsigned long majorDim
;
511 enable
= !m_textMajorDim
->GetValue().ToULong(&majorDim
) ||
512 majorDim
!= DEFAULT_MAJOR_DIM
;
516 event
.Enable(enable
);
519 void RadioWidgetsPage::OnUpdateUIEnableItem(wxUpdateUIEvent
& event
)
521 event
.SetText(m_radio
->IsItemEnabled(TEST_BUTTON
) ? wxT("Disable &2nd item")
522 : wxT("Enable &2nd item"));
525 void RadioWidgetsPage::OnUpdateUIShowItem(wxUpdateUIEvent
& event
)
527 event
.SetText(m_radio
->IsItemShown(TEST_BUTTON
) ? wxT("Hide 2nd &item")
528 : wxT("Show 2nd &item"));
531 #endif // wxUSE_RADIOBOX