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
= 100,
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 // ----------------------------------------------------------------------------
78 // ----------------------------------------------------------------------------
80 class RadioWidgetsPage
: public WidgetsPage
83 RadioWidgetsPage(wxBookCtrlBase
*book
, wxImageList
*imaglist
);
84 virtual ~RadioWidgetsPage(){};
86 virtual wxControl
*GetWidget() const { return m_radio
; }
90 void OnCheckOrRadioBox(wxCommandEvent
& event
);
91 void OnRadioBox(wxCommandEvent
& event
);
93 void OnButtonReset(wxCommandEvent
& event
);
94 void OnButtonRecreate(wxCommandEvent
& event
);
96 void OnButtonSelection(wxCommandEvent
& event
);
97 void OnButtonSetLabel(wxCommandEvent
& event
);
99 void OnUpdateUIReset(wxUpdateUIEvent
& event
);
100 void OnUpdateUIUpdate(wxUpdateUIEvent
& event
);
101 void OnUpdateUISelection(wxUpdateUIEvent
& event
);
103 // reset the wxRadioBox parameters
106 // (re)create the wxRadioBox
112 // the check/radio boxes for styles
113 wxCheckBox
*m_chkVert
;
114 wxCheckBox
*m_2ndEnabled
;
115 wxCheckBox
*m_2ndShown
;
116 wxRadioBox
*m_radioDir
;
118 // the gauge itself and the sizer it is in
120 wxSizer
*m_sizerRadio
;
122 // the text entries for command parameters
123 wxTextCtrl
*m_textNumBtns
,
131 DECLARE_EVENT_TABLE()
132 DECLARE_WIDGETS_PAGE(RadioWidgetsPage
)
135 // ----------------------------------------------------------------------------
137 // ----------------------------------------------------------------------------
139 BEGIN_EVENT_TABLE(RadioWidgetsPage
, WidgetsPage
)
140 EVT_BUTTON(RadioPage_Reset
, RadioWidgetsPage::OnButtonReset
)
142 EVT_BUTTON(RadioPage_Update
, RadioWidgetsPage::OnButtonRecreate
)
143 EVT_BUTTON(RadioPage_LabelBtn
, RadioWidgetsPage::OnButtonRecreate
)
145 EVT_BUTTON(RadioPage_Selection
, RadioWidgetsPage::OnButtonSelection
)
146 EVT_BUTTON(RadioPage_Label
, RadioWidgetsPage::OnButtonSetLabel
)
148 EVT_UPDATE_UI(RadioPage_Update
, RadioWidgetsPage::OnUpdateUIUpdate
)
149 EVT_UPDATE_UI(RadioPage_Selection
, RadioWidgetsPage::OnUpdateUISelection
)
151 EVT_RADIOBOX(RadioPage_Radio
, RadioWidgetsPage::OnRadioBox
)
153 EVT_CHECKBOX(wxID_ANY
, RadioWidgetsPage::OnCheckOrRadioBox
)
154 EVT_RADIOBOX(wxID_ANY
, RadioWidgetsPage::OnCheckOrRadioBox
)
157 // ============================================================================
159 // ============================================================================
161 IMPLEMENT_WIDGETS_PAGE(RadioWidgetsPage
, _T("Radio"));
163 RadioWidgetsPage::RadioWidgetsPage(wxBookCtrlBase
*book
,
164 wxImageList
*imaglist
)
167 imaglist
->Add(wxBitmap(radio_xpm
));
170 m_chkVert
= (wxCheckBox
*)NULL
;
171 m_2ndEnabled
= (wxCheckBox
*)NULL
;
172 m_2ndShown
= (wxCheckBox
*)NULL
;
176 m_textLabel
= (wxTextCtrl
*)NULL
;
179 m_radioDir
= (wxRadioBox
*)NULL
;
180 m_sizerRadio
= (wxSizer
*)NULL
;
182 wxSizer
*sizerTop
= new wxBoxSizer(wxHORIZONTAL
);
185 wxStaticBox
*box
= new wxStaticBox(this, wxID_ANY
, _T("&Set style"));
187 wxSizer
*sizerLeft
= new wxStaticBoxSizer(box
, wxVERTICAL
);
189 m_chkVert
= CreateCheckBoxAndAddToSizer(sizerLeft
, _T("&Vertical layout"));
191 static const wxString layoutDir
[] =
198 m_radioDir
= new wxRadioBox(this, wxID_ANY
, _T("Numbering:"),
199 wxDefaultPosition
, wxDefaultSize
,
200 WXSIZEOF(layoutDir
), layoutDir
,
201 1, wxRA_SPECIFY_COLS
);
202 sizerLeft
->Add(m_radioDir
, 0, wxGROW
| wxALL
, 5);
204 // if it's not defined, we can't change the radiobox direction
205 #ifndef wxRA_LEFTTORIGHT
206 m_radioDir
->Disable();
207 #endif // wxRA_LEFTTORIGHT
210 sizerRow
= CreateSizerWithTextAndLabel(_T("&Major dimension:"),
213 sizerLeft
->Add(sizerRow
, 0, wxGROW
| wxALL
, 5);
215 sizerRow
= CreateSizerWithTextAndLabel(_T("&Number of buttons:"),
218 sizerLeft
->Add(sizerRow
, 0, wxGROW
| wxALL
, 5);
221 btn
= new wxButton(this, RadioPage_Update
, _T("&Update"));
222 sizerLeft
->Add(btn
, 0, wxALIGN_CENTRE_HORIZONTAL
| wxALL
, 5);
224 sizerLeft
->Add(5, 5, 0, wxGROW
| wxALL
, 5); // spacer
226 btn
= new wxButton(this, RadioPage_Reset
, _T("&Reset"));
227 sizerLeft
->Add(btn
, 0, wxALIGN_CENTRE_HORIZONTAL
| wxALL
, 15);
230 wxStaticBox
*box2
= new wxStaticBox(this, wxID_ANY
, _T("&Change parameters"));
231 wxSizer
*sizerMiddle
= new wxStaticBoxSizer(box2
, wxVERTICAL
);
233 sizerRow
= CreateSizerWithTextAndLabel(_T("Current selection:"),
236 sizerMiddle
->Add(sizerRow
, 0, wxGROW
| wxALL
, 5);
238 sizerRow
= CreateSizerWithTextAndButton(RadioPage_Selection
,
239 _T("&Change selection:"),
242 sizerMiddle
->Add(sizerRow
, 0, wxGROW
| wxALL
, 5);
244 sizerRow
= CreateSizerWithTextAndButton(RadioPage_Label
,
245 _T("&Label for box:"),
248 sizerMiddle
->Add(sizerRow
, 0, wxGROW
| wxALL
, 5);
250 sizerRow
= CreateSizerWithTextAndButton(RadioPage_LabelBtn
,
251 _T("&Label for buttons:"),
254 sizerMiddle
->Add(sizerRow
, 0, wxGROW
| wxALL
, 5);
256 m_2ndEnabled
= CreateCheckBoxAndAddToSizer(sizerMiddle
, _T("2nd item enabled"));
257 m_2ndShown
= CreateCheckBoxAndAddToSizer(sizerMiddle
, _T("2nd item shown"));
260 wxSizer
*sizerRight
= new wxBoxSizer(wxHORIZONTAL
);
261 sizerRight
->SetMinSize(150, 0);
262 m_sizerRadio
= sizerRight
; // save it to modify it later
267 // the 3 panes panes compose the window
268 sizerTop
->Add(sizerLeft
, 0, wxGROW
| (wxALL
& ~wxLEFT
), 10);
269 sizerTop
->Add(sizerMiddle
, 1, wxGROW
| wxALL
, 10);
270 sizerTop
->Add(sizerRight
, 0, wxGROW
| (wxALL
& ~wxRIGHT
), 10);
272 // final initializations
278 // ----------------------------------------------------------------------------
280 // ----------------------------------------------------------------------------
282 void RadioWidgetsPage::Reset()
284 m_textMajorDim
->SetValue(wxString::Format(_T("%u"), DEFAULT_MAJOR_DIM
));
285 m_textNumBtns
->SetValue(wxString::Format(_T("%u"), DEFAULT_NUM_ENTRIES
));
286 m_textLabel
->SetValue(_T("I'm a radiobox"));
287 m_textLabelBtns
->SetValue(_T("item"));
289 m_chkVert
->SetValue(false);
290 m_2ndEnabled
->SetValue(true);
291 m_2ndShown
->SetValue(true);
292 m_radioDir
->SetSelection(RadioDir_Default
);
295 void RadioWidgetsPage::CreateRadio()
300 sel
= m_radio
->GetSelection();
302 m_sizerRadio
->Detach( m_radio
);
306 else // first time creation, no old selection to preserve
312 if ( !m_textNumBtns
->GetValue().ToULong(&count
) )
314 wxLogWarning(_T("Should have a valid number for number of items."));
316 // fall back to default
317 count
= DEFAULT_NUM_ENTRIES
;
320 unsigned long majorDim
;
321 if ( !m_textMajorDim
->GetValue().ToULong(&majorDim
) )
323 wxLogWarning(_T("Should have a valid major dimension number."));
325 // fall back to default
326 majorDim
= DEFAULT_MAJOR_DIM
;
329 wxString
*items
= new wxString
[count
];
331 wxString labelBtn
= m_textLabelBtns
->GetValue();
332 for ( size_t n
= 0; n
< count
; n
++ )
334 items
[n
] = wxString::Format(_T("%s %lu"),
335 labelBtn
.c_str(), (unsigned long)n
+ 1);
338 int flags
= m_chkVert
->GetValue() ? wxRA_VERTICAL
341 #ifdef wxRA_LEFTTORIGHT
342 switch ( m_radioDir
->GetSelection() )
345 wxFAIL_MSG( _T("unexpected wxRadioBox layout direction") );
348 case RadioDir_Default
:
352 flags
|= wxRA_LEFTTORIGHT
;
356 flags
|= wxRA_TOPTOBOTTOM
;
359 #endif // wxRA_LEFTTORIGHT
361 m_radio
= new wxRadioBox(this, RadioPage_Radio
,
362 m_textLabel
->GetValue(),
363 wxDefaultPosition
, wxDefaultSize
,
370 if ( sel
>= 0 && (size_t)sel
< count
)
372 m_radio
->SetSelection(sel
);
375 m_sizerRadio
->Add(m_radio
, 1, wxGROW
);
376 m_sizerRadio
->Layout();
378 m_radio
->Enable( 1 , m_2ndEnabled
->GetValue() );
379 m_radio
->Show( 1 , m_2ndShown
->GetValue() );
382 // ----------------------------------------------------------------------------
384 // ----------------------------------------------------------------------------
386 void RadioWidgetsPage::OnButtonReset(wxCommandEvent
& WXUNUSED(event
))
393 void RadioWidgetsPage::OnCheckOrRadioBox(wxCommandEvent
& WXUNUSED(event
))
398 void RadioWidgetsPage::OnRadioBox(wxCommandEvent
& event
)
400 int sel
= m_radio
->GetSelection();
401 int event_sel
= event
.GetSelection();
402 wxUnusedVar(event_sel
);
404 wxLogMessage(_T("Radiobox selection changed, now %d"), sel
);
406 wxASSERT_MSG( sel
== event_sel
,
407 _T("selection should be the same in event and radiobox") );
409 m_textCurSel
->SetValue(wxString::Format(_T("%d"), sel
));
412 void RadioWidgetsPage::OnButtonRecreate(wxCommandEvent
& WXUNUSED(event
))
417 void RadioWidgetsPage::OnButtonSetLabel(wxCommandEvent
& WXUNUSED(event
))
419 m_radio
->wxControl::SetLabel(m_textLabel
->GetValue());
422 void RadioWidgetsPage::OnButtonSelection(wxCommandEvent
& WXUNUSED(event
))
425 if ( !m_textSel
->GetValue().ToULong(&sel
) ||
426 (sel
>= (size_t)m_radio
->GetCount()) )
428 wxLogWarning(_T("Invalid number specified as new selection."));
432 m_radio
->SetSelection(sel
);
436 void RadioWidgetsPage::OnUpdateUIUpdate(wxUpdateUIEvent
& event
)
439 event
.Enable( m_textNumBtns
->GetValue().ToULong(&n
) &&
440 m_textMajorDim
->GetValue().ToULong(&n
) );
443 void RadioWidgetsPage::OnUpdateUISelection(wxUpdateUIEvent
& event
)
446 event
.Enable( m_textSel
->GetValue().ToULong(&n
) &&
447 (n
< (size_t)m_radio
->GetCount()) );
450 void RadioWidgetsPage::OnUpdateUIReset(wxUpdateUIEvent
& event
)
452 // only enable it if something is not set to default
453 bool enable
= m_chkVert
->GetValue();
457 unsigned long numEntries
;
459 enable
= !m_textNumBtns
->GetValue().ToULong(&numEntries
) ||
460 numEntries
!= DEFAULT_NUM_ENTRIES
;
464 unsigned long majorDim
;
466 enable
= !m_textMajorDim
->GetValue().ToULong(&majorDim
) ||
467 majorDim
!= DEFAULT_MAJOR_DIM
;
471 event
.Enable(enable
);
474 #endif // wxUSE_RADIOBOX