1 /////////////////////////////////////////////////////////////////////////////
2 // Program: wxWidgets Widgets Sample
4 // Purpose: Part of the widgets sample showing wxRadioBox
5 // Author: Vadim Zeitlin
7 // Copyright: (c) 2001 Vadim Zeitlin
8 // Licence: wxWindows licence
9 /////////////////////////////////////////////////////////////////////////////
11 // ============================================================================
13 // ============================================================================
15 // ----------------------------------------------------------------------------
17 // ----------------------------------------------------------------------------
19 // for compilers that support precompilation, includes "wx/wx.h".
20 #include "wx/wxprec.h"
28 // for all others, include the necessary headers
32 #include "wx/bitmap.h"
33 #include "wx/button.h"
34 #include "wx/checkbox.h"
35 #include "wx/radiobox.h"
36 #include "wx/statbox.h"
37 #include "wx/textctrl.h"
44 #include "icons/radiobox.xpm"
46 // ----------------------------------------------------------------------------
48 // ----------------------------------------------------------------------------
53 RadioPage_Reset
= wxID_HIGHEST
,
63 // layout direction radiobox selections
71 // default values for the number of radiobox items
72 static const unsigned int DEFAULT_NUM_ENTRIES
= 12;
73 static const unsigned int DEFAULT_MAJOR_DIM
= 3;
75 // this item is enabled/disabled shown/hidden by the test checkboxes
76 static const int TEST_BUTTON
= 1;
78 // ----------------------------------------------------------------------------
80 // ----------------------------------------------------------------------------
82 class RadioWidgetsPage
: public WidgetsPage
85 RadioWidgetsPage(WidgetsBookCtrl
*book
, wxImageList
*imaglist
);
86 virtual ~RadioWidgetsPage(){};
88 virtual wxControl
*GetWidget() const { return m_radio
; }
89 virtual void RecreateWidget() { CreateRadio(); }
91 // lazy creation of the content
92 virtual void CreateContent();
96 void OnCheckOrRadioBox(wxCommandEvent
& event
);
97 void OnRadioBox(wxCommandEvent
& event
);
99 void OnButtonReset(wxCommandEvent
& event
);
100 void OnButtonRecreate(wxCommandEvent
& event
);
102 void OnButtonSelection(wxCommandEvent
& event
);
103 void OnButtonSetLabel(wxCommandEvent
& event
);
105 void OnEnableItem(wxCommandEvent
& event
);
106 void OnShowItem(wxCommandEvent
& event
);
108 void OnUpdateUIReset(wxUpdateUIEvent
& event
);
109 void OnUpdateUIUpdate(wxUpdateUIEvent
& event
);
110 void OnUpdateUISelection(wxUpdateUIEvent
& event
);
111 void OnUpdateUIEnableItem(wxUpdateUIEvent
& event
);
112 void OnUpdateUIShowItem(wxUpdateUIEvent
& event
);
114 // reset the wxRadioBox parameters
117 // (re)create the wxRadioBox
123 // the check/radio boxes for styles
124 wxCheckBox
*m_chkSpecifyRows
;
125 wxCheckBox
*m_chkEnableItem
;
126 wxCheckBox
*m_chkShowItem
;
127 wxRadioBox
*m_radioDir
;
129 // the gauge itself and the sizer it is in
131 wxSizer
*m_sizerRadio
;
133 // the text entries for command parameters
134 wxTextCtrl
*m_textNumBtns
,
142 DECLARE_EVENT_TABLE()
143 DECLARE_WIDGETS_PAGE(RadioWidgetsPage
)
146 // ----------------------------------------------------------------------------
148 // ----------------------------------------------------------------------------
150 BEGIN_EVENT_TABLE(RadioWidgetsPage
, WidgetsPage
)
151 EVT_BUTTON(RadioPage_Reset
, RadioWidgetsPage::OnButtonReset
)
153 EVT_BUTTON(RadioPage_Update
, RadioWidgetsPage::OnButtonRecreate
)
154 EVT_BUTTON(RadioPage_LabelBtn
, RadioWidgetsPage::OnButtonRecreate
)
156 EVT_BUTTON(RadioPage_Selection
, RadioWidgetsPage::OnButtonSelection
)
157 EVT_BUTTON(RadioPage_Label
, RadioWidgetsPage::OnButtonSetLabel
)
159 EVT_UPDATE_UI(RadioPage_Update
, RadioWidgetsPage::OnUpdateUIUpdate
)
160 EVT_UPDATE_UI(RadioPage_Selection
, RadioWidgetsPage::OnUpdateUISelection
)
162 EVT_RADIOBOX(RadioPage_Radio
, RadioWidgetsPage::OnRadioBox
)
164 EVT_CHECKBOX(RadioPage_EnableItem
, RadioWidgetsPage::OnEnableItem
)
165 EVT_CHECKBOX(RadioPage_ShowItem
, RadioWidgetsPage::OnShowItem
)
167 EVT_UPDATE_UI(RadioPage_EnableItem
, RadioWidgetsPage::OnUpdateUIEnableItem
)
168 EVT_UPDATE_UI(RadioPage_ShowItem
, RadioWidgetsPage::OnUpdateUIShowItem
)
170 EVT_CHECKBOX(wxID_ANY
, RadioWidgetsPage::OnCheckOrRadioBox
)
171 EVT_RADIOBOX(wxID_ANY
, RadioWidgetsPage::OnCheckOrRadioBox
)
174 // ============================================================================
176 // ============================================================================
178 #if defined(__WXUNIVERSAL__)
179 #define FAMILY_CTRLS UNIVERSAL_CTRLS
181 #define FAMILY_CTRLS NATIVE_CTRLS
184 IMPLEMENT_WIDGETS_PAGE(RadioWidgetsPage
, wxT("Radio"),
185 FAMILY_CTRLS
| WITH_ITEMS_CTRLS
188 RadioWidgetsPage::RadioWidgetsPage(WidgetsBookCtrl
*book
,
189 wxImageList
*imaglist
)
190 : WidgetsPage(book
, imaglist
, radio_xpm
)
193 m_chkSpecifyRows
= (wxCheckBox
*)NULL
;
194 m_chkEnableItem
= (wxCheckBox
*)NULL
;
195 m_chkShowItem
= (wxCheckBox
*)NULL
;
199 m_textLabel
= (wxTextCtrl
*)NULL
;
202 m_radioDir
= (wxRadioBox
*)NULL
;
203 m_sizerRadio
= (wxSizer
*)NULL
;
206 void RadioWidgetsPage::CreateContent()
208 wxSizer
*sizerTop
= new wxBoxSizer(wxHORIZONTAL
);
211 wxStaticBox
*box
= new wxStaticBox(this, wxID_ANY
, wxT("&Set style"));
213 wxSizer
*sizerLeft
= new wxStaticBoxSizer(box
, wxVERTICAL
);
215 m_chkSpecifyRows
= CreateCheckBoxAndAddToSizer
218 "Major specifies &rows count"
221 static const wxString layoutDir
[] =
224 wxT("left to right"),
228 m_radioDir
= new wxRadioBox(this, wxID_ANY
, wxT("Numbering:"),
229 wxDefaultPosition
, wxDefaultSize
,
230 WXSIZEOF(layoutDir
), layoutDir
,
231 1, wxRA_SPECIFY_COLS
);
232 sizerLeft
->Add(m_radioDir
, 0, wxGROW
| wxALL
, 5);
234 // if it's not defined, we can't change the radiobox direction
235 #ifndef wxRA_LEFTTORIGHT
236 m_radioDir
->Disable();
237 #endif // wxRA_LEFTTORIGHT
240 sizerRow
= CreateSizerWithTextAndLabel(wxT("&Major dimension:"),
243 sizerLeft
->Add(sizerRow
, 0, wxGROW
| wxALL
, 5);
245 sizerRow
= CreateSizerWithTextAndLabel(wxT("&Number of buttons:"),
248 sizerLeft
->Add(sizerRow
, 0, wxGROW
| wxALL
, 5);
251 btn
= new wxButton(this, RadioPage_Update
, wxT("&Update"));
252 sizerLeft
->Add(btn
, 0, wxALIGN_CENTRE_HORIZONTAL
| wxALL
, 5);
254 sizerLeft
->Add(5, 5, 0, wxGROW
| wxALL
, 5); // spacer
256 btn
= new wxButton(this, RadioPage_Reset
, wxT("&Reset"));
257 sizerLeft
->Add(btn
, 0, wxALIGN_CENTRE_HORIZONTAL
| wxALL
, 15);
260 wxStaticBox
*box2
= new wxStaticBox(this, wxID_ANY
, wxT("&Change parameters"));
261 wxSizer
*sizerMiddle
= new wxStaticBoxSizer(box2
, wxVERTICAL
);
263 sizerRow
= CreateSizerWithTextAndLabel(wxT("Current selection:"),
266 sizerMiddle
->Add(sizerRow
, 0, wxGROW
| wxALL
, 5);
268 sizerRow
= CreateSizerWithTextAndButton(RadioPage_Selection
,
269 wxT("&Change selection:"),
272 sizerMiddle
->Add(sizerRow
, 0, wxGROW
| wxALL
, 5);
274 sizerRow
= CreateSizerWithTextAndButton(RadioPage_Label
,
275 wxT("&Label for box:"),
278 sizerMiddle
->Add(sizerRow
, 0, wxGROW
| wxALL
, 5);
280 sizerRow
= CreateSizerWithTextAndButton(RadioPage_LabelBtn
,
281 wxT("&Label for buttons:"),
284 sizerMiddle
->Add(sizerRow
, 0, wxGROW
| wxALL
, 5);
286 m_chkEnableItem
= CreateCheckBoxAndAddToSizer(sizerMiddle
,
287 wxT("Disable &2nd item"),
288 RadioPage_EnableItem
);
289 m_chkShowItem
= CreateCheckBoxAndAddToSizer(sizerMiddle
,
290 wxT("Hide 2nd &item"),
294 wxSizer
*sizerRight
= new wxBoxSizer(wxHORIZONTAL
);
295 sizerRight
->SetMinSize(150, 0);
296 m_sizerRadio
= sizerRight
; // save it to modify it later
301 // the 3 panes panes compose the window
302 sizerTop
->Add(sizerLeft
, 0, wxGROW
| (wxALL
& ~wxLEFT
), 10);
303 sizerTop
->Add(sizerMiddle
, 1, wxGROW
| wxALL
, 10);
304 sizerTop
->Add(sizerRight
, 0, wxGROW
| (wxALL
& ~wxRIGHT
), 10);
306 // final initializations
310 // ----------------------------------------------------------------------------
312 // ----------------------------------------------------------------------------
314 void RadioWidgetsPage::Reset()
316 m_textMajorDim
->SetValue(wxString::Format(wxT("%u"), DEFAULT_MAJOR_DIM
));
317 m_textNumBtns
->SetValue(wxString::Format(wxT("%u"), DEFAULT_NUM_ENTRIES
));
318 m_textLabel
->SetValue(wxT("I'm a radiobox"));
319 m_textLabelBtns
->SetValue(wxT("item"));
321 m_chkSpecifyRows
->SetValue(false);
322 m_chkEnableItem
->SetValue(true);
323 m_chkShowItem
->SetValue(true);
324 m_radioDir
->SetSelection(RadioDir_Default
);
327 void RadioWidgetsPage::CreateRadio()
332 sel
= m_radio
->GetSelection();
334 m_sizerRadio
->Detach( m_radio
);
338 else // first time creation, no old selection to preserve
344 if ( !m_textNumBtns
->GetValue().ToULong(&count
) )
346 wxLogWarning(wxT("Should have a valid number for number of items."));
348 // fall back to default
349 count
= DEFAULT_NUM_ENTRIES
;
352 unsigned long majorDim
;
353 if ( !m_textMajorDim
->GetValue().ToULong(&majorDim
) )
355 wxLogWarning(wxT("Should have a valid major dimension number."));
357 // fall back to default
358 majorDim
= DEFAULT_MAJOR_DIM
;
361 wxString
*items
= new wxString
[count
];
363 wxString labelBtn
= m_textLabelBtns
->GetValue();
364 for ( size_t n
= 0; n
< count
; n
++ )
366 items
[n
] = wxString::Format(wxT("%s %lu"),
367 labelBtn
.c_str(), (unsigned long)n
+ 1);
370 int flags
= m_chkSpecifyRows
->GetValue() ? wxRA_SPECIFY_ROWS
373 flags
|= ms_defaultFlags
;
375 #ifdef wxRA_LEFTTORIGHT
376 switch ( m_radioDir
->GetSelection() )
379 wxFAIL_MSG( wxT("unexpected wxRadioBox layout direction") );
382 case RadioDir_Default
:
386 flags
|= wxRA_LEFTTORIGHT
;
390 flags
|= wxRA_TOPTOBOTTOM
;
393 #endif // wxRA_LEFTTORIGHT
395 m_radio
= new wxRadioBox(this, RadioPage_Radio
,
396 m_textLabel
->GetValue(),
397 wxDefaultPosition
, wxDefaultSize
,
404 if ( sel
>= 0 && (size_t)sel
< count
)
406 m_radio
->SetSelection(sel
);
409 m_sizerRadio
->Add(m_radio
, 1, wxGROW
);
410 m_sizerRadio
->Layout();
412 m_chkEnableItem
->SetValue(true);
413 m_chkEnableItem
->SetValue(true);
416 // ----------------------------------------------------------------------------
418 // ----------------------------------------------------------------------------
420 void RadioWidgetsPage::OnButtonReset(wxCommandEvent
& WXUNUSED(event
))
427 void RadioWidgetsPage::OnCheckOrRadioBox(wxCommandEvent
& WXUNUSED(event
))
432 void RadioWidgetsPage::OnRadioBox(wxCommandEvent
& event
)
434 int sel
= m_radio
->GetSelection();
435 int event_sel
= event
.GetSelection();
436 wxUnusedVar(event_sel
);
438 wxLogMessage(wxT("Radiobox selection changed, now %d"), sel
);
440 wxASSERT_MSG( sel
== event_sel
,
441 wxT("selection should be the same in event and radiobox") );
443 m_textCurSel
->SetValue(wxString::Format(wxT("%d"), sel
));
446 void RadioWidgetsPage::OnButtonRecreate(wxCommandEvent
& WXUNUSED(event
))
451 void RadioWidgetsPage::OnButtonSetLabel(wxCommandEvent
& WXUNUSED(event
))
453 m_radio
->wxControl::SetLabel(m_textLabel
->GetValue());
456 void RadioWidgetsPage::OnButtonSelection(wxCommandEvent
& WXUNUSED(event
))
459 if ( !m_textSel
->GetValue().ToULong(&sel
) ||
460 (sel
>= (size_t)m_radio
->GetCount()) )
462 wxLogWarning(wxT("Invalid number specified as new selection."));
466 m_radio
->SetSelection(sel
);
470 void RadioWidgetsPage::OnEnableItem(wxCommandEvent
& event
)
472 m_radio
->Enable(TEST_BUTTON
, event
.IsChecked());
475 void RadioWidgetsPage::OnShowItem(wxCommandEvent
& event
)
477 m_radio
->Show(TEST_BUTTON
, event
.IsChecked());
480 void RadioWidgetsPage::OnUpdateUIUpdate(wxUpdateUIEvent
& event
)
483 event
.Enable( m_textNumBtns
->GetValue().ToULong(&n
) &&
484 m_textMajorDim
->GetValue().ToULong(&n
) );
487 void RadioWidgetsPage::OnUpdateUISelection(wxUpdateUIEvent
& event
)
490 event
.Enable( m_textSel
->GetValue().ToULong(&n
) &&
491 (n
< (size_t)m_radio
->GetCount()) );
494 void RadioWidgetsPage::OnUpdateUIReset(wxUpdateUIEvent
& event
)
496 // only enable it if something is not set to default
497 bool enable
= m_chkSpecifyRows
->GetValue();
501 unsigned long numEntries
;
503 enable
= !m_textNumBtns
->GetValue().ToULong(&numEntries
) ||
504 numEntries
!= DEFAULT_NUM_ENTRIES
;
508 unsigned long majorDim
;
510 enable
= !m_textMajorDim
->GetValue().ToULong(&majorDim
) ||
511 majorDim
!= DEFAULT_MAJOR_DIM
;
515 event
.Enable(enable
);
518 void RadioWidgetsPage::OnUpdateUIEnableItem(wxUpdateUIEvent
& event
)
520 event
.SetText(m_radio
->IsItemEnabled(TEST_BUTTON
) ? wxT("Disable &2nd item")
521 : wxT("Enable &2nd item"));
524 void RadioWidgetsPage::OnUpdateUIShowItem(wxUpdateUIEvent
& event
)
526 event
.SetText(m_radio
->IsItemShown(TEST_BUTTON
) ? wxT("Hide 2nd &item")
527 : wxT("Show 2nd &item"));
530 #endif // wxUSE_RADIOBOX