1 /////////////////////////////////////////////////////////////////////////////
2 // Program: wxWidgets Widgets Sample
4 // Purpose: Part of the widgets sample showing wxChoice
7 // License: wxWindows license
8 /////////////////////////////////////////////////////////////////////////////
10 // ============================================================================
12 // ============================================================================
14 // ----------------------------------------------------------------------------
16 // ----------------------------------------------------------------------------
18 // for compilers that support precompilation, includes "wx/wx.h".
19 #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/choice.h"
35 #include "wx/combobox.h"
36 #include "wx/radiobox.h"
37 #include "wx/statbox.h"
38 #include "wx/textctrl.h"
43 #include "wx/checklst.h"
45 #include "itemcontainer.h"
48 #include "icons/choice.xpm"
50 // ----------------------------------------------------------------------------
52 // ----------------------------------------------------------------------------
57 ChoicePage_Reset
= wxID_HIGHEST
,
60 ChoicePage_AddSeveral
,
64 ChoicePage_ChangeText
,
66 ChoicePage_DeleteText
,
69 ChoicePage_ContainerTests
72 // ----------------------------------------------------------------------------
74 // ----------------------------------------------------------------------------
76 class ChoiceWidgetsPage
: public ItemContainerWidgetsPage
79 ChoiceWidgetsPage(WidgetsBookCtrl
*book
, wxImageList
*imaglist
);
81 virtual wxControl
*GetWidget() const { return m_choice
; }
82 virtual wxItemContainer
* GetContainer() const { return m_choice
; }
83 virtual void RecreateWidget() { CreateChoice(); }
85 // lazy creation of the content
86 virtual void CreateContent();
90 void OnButtonReset(wxCommandEvent
& event
);
91 void OnButtonChange(wxCommandEvent
& event
);
92 void OnButtonDelete(wxCommandEvent
& event
);
93 void OnButtonDeleteSel(wxCommandEvent
& event
);
94 void OnButtonClear(wxCommandEvent
& event
);
95 void OnButtonAdd(wxCommandEvent
& event
);
96 void OnButtonAddSeveral(wxCommandEvent
& event
);
97 void OnButtonAddMany(wxCommandEvent
& event
);
99 void OnChoice(wxCommandEvent
& event
);
101 void OnCheckOrRadioBox(wxCommandEvent
& event
);
103 void OnUpdateUIAddSeveral(wxUpdateUIEvent
& event
);
104 void OnUpdateUIClearButton(wxUpdateUIEvent
& event
);
105 void OnUpdateUIDeleteButton(wxUpdateUIEvent
& event
);
106 void OnUpdateUIDeleteSelButton(wxUpdateUIEvent
& event
);
107 void OnUpdateUIResetButton(wxUpdateUIEvent
& event
);
109 // reset the choice parameters
112 // (re)create the choice
115 // should it be sorted?
122 wxCheckBox
*m_chkSort
;
124 // the choice itself and the sizer it is in
132 wxSizer
*m_sizerChoice
;
134 // the text entries for "Add/change string" and "Delete" buttons
135 wxTextCtrl
*m_textAdd
,
140 DECLARE_EVENT_TABLE()
141 DECLARE_WIDGETS_PAGE(ChoiceWidgetsPage
)
144 // ----------------------------------------------------------------------------
146 // ----------------------------------------------------------------------------
148 BEGIN_EVENT_TABLE(ChoiceWidgetsPage
, WidgetsPage
)
149 EVT_BUTTON(ChoicePage_Reset
, ChoiceWidgetsPage::OnButtonReset
)
150 EVT_BUTTON(ChoicePage_Change
, ChoiceWidgetsPage::OnButtonChange
)
151 EVT_BUTTON(ChoicePage_Delete
, ChoiceWidgetsPage::OnButtonDelete
)
152 EVT_BUTTON(ChoicePage_DeleteSel
, ChoiceWidgetsPage::OnButtonDeleteSel
)
153 EVT_BUTTON(ChoicePage_Clear
, ChoiceWidgetsPage::OnButtonClear
)
154 EVT_BUTTON(ChoicePage_Add
, ChoiceWidgetsPage::OnButtonAdd
)
155 EVT_BUTTON(ChoicePage_AddSeveral
, ChoiceWidgetsPage::OnButtonAddSeveral
)
156 EVT_BUTTON(ChoicePage_AddMany
, ChoiceWidgetsPage::OnButtonAddMany
)
157 EVT_BUTTON(ChoicePage_ContainerTests
, ItemContainerWidgetsPage::OnButtonTestItemContainer
)
159 EVT_TEXT_ENTER(ChoicePage_AddText
, ChoiceWidgetsPage::OnButtonAdd
)
160 EVT_TEXT_ENTER(ChoicePage_DeleteText
, ChoiceWidgetsPage::OnButtonDelete
)
162 EVT_UPDATE_UI(ChoicePage_Reset
, ChoiceWidgetsPage::OnUpdateUIResetButton
)
163 EVT_UPDATE_UI(ChoicePage_AddSeveral
, ChoiceWidgetsPage::OnUpdateUIAddSeveral
)
164 EVT_UPDATE_UI(ChoicePage_Clear
, ChoiceWidgetsPage::OnUpdateUIClearButton
)
165 EVT_UPDATE_UI(ChoicePage_DeleteText
, ChoiceWidgetsPage::OnUpdateUIClearButton
)
166 EVT_UPDATE_UI(ChoicePage_Delete
, ChoiceWidgetsPage::OnUpdateUIDeleteButton
)
167 EVT_UPDATE_UI(ChoicePage_Change
, ChoiceWidgetsPage::OnUpdateUIDeleteSelButton
)
168 EVT_UPDATE_UI(ChoicePage_ChangeText
, ChoiceWidgetsPage::OnUpdateUIDeleteSelButton
)
169 EVT_UPDATE_UI(ChoicePage_DeleteSel
, ChoiceWidgetsPage::OnUpdateUIDeleteSelButton
)
171 EVT_CHOICE(ChoicePage_Choice
, ChoiceWidgetsPage::OnChoice
)
173 EVT_CHECKBOX(wxID_ANY
, ChoiceWidgetsPage::OnCheckOrRadioBox
)
174 EVT_RADIOBOX(wxID_ANY
, ChoiceWidgetsPage::OnCheckOrRadioBox
)
177 // ============================================================================
179 // ============================================================================
181 #if defined(__WXUNIVERSAL__)
182 #define FAMILY_CTRLS UNIVERSAL_CTRLS
184 #define FAMILY_CTRLS NATIVE_CTRLS
187 IMPLEMENT_WIDGETS_PAGE(ChoiceWidgetsPage
, _T("Choice"),
188 FAMILY_CTRLS
| WITH_ITEMS_CTRLS
191 ChoiceWidgetsPage::ChoiceWidgetsPage(WidgetsBookCtrl
*book
,
192 wxImageList
*imaglist
)
193 : ItemContainerWidgetsPage(book
, imaglist
, choice_xpm
)
197 m_chkSort
= (wxCheckBox
*)NULL
;
200 m_sizerChoice
= (wxSizer
*)NULL
;
204 void ChoiceWidgetsPage::CreateContent()
207 What we create here is a frame having 3 panes: style pane is the
208 leftmost one, in the middle the pane with buttons allowing to perform
209 miscellaneous choice operations and the pane containing the choice
212 wxSizer
*sizerTop
= new wxBoxSizer(wxHORIZONTAL
);
215 wxStaticBox
*box
= new wxStaticBox(this, wxID_ANY
,
216 _T("&Set choice parameters"));
217 wxSizer
*sizerLeft
= new wxStaticBoxSizer(box
, wxVERTICAL
);
219 static const wxString modes
[] =
226 m_chkSort
= CreateCheckBoxAndAddToSizer(sizerLeft
, _T("&Sort items"));
228 wxButton
*btn
= new wxButton(this, ChoicePage_Reset
, _T("&Reset"));
229 sizerLeft
->Add(btn
, 0, wxALIGN_CENTRE_HORIZONTAL
| wxALL
, 15);
232 wxStaticBox
*box2
= new wxStaticBox(this, wxID_ANY
,
233 _T("&Change choice contents"));
234 wxSizer
*sizerMiddle
= new wxStaticBoxSizer(box2
, wxVERTICAL
);
236 wxSizer
*sizerRow
= new wxBoxSizer(wxHORIZONTAL
);
237 btn
= new wxButton(this, ChoicePage_Add
, _T("&Add this string"));
238 m_textAdd
= new wxTextCtrl(this, ChoicePage_AddText
, _T("test item 0"));
239 sizerRow
->Add(btn
, 0, wxRIGHT
, 5);
240 sizerRow
->Add(m_textAdd
, 1, wxLEFT
, 5);
241 sizerMiddle
->Add(sizerRow
, 0, wxALL
| wxGROW
, 5);
243 btn
= new wxButton(this, ChoicePage_AddSeveral
, _T("&Insert a few strings"));
244 sizerMiddle
->Add(btn
, 0, wxALL
| wxGROW
, 5);
246 btn
= new wxButton(this, ChoicePage_AddMany
, _T("Add &many strings"));
247 sizerMiddle
->Add(btn
, 0, wxALL
| wxGROW
, 5);
249 sizerRow
= new wxBoxSizer(wxHORIZONTAL
);
250 btn
= new wxButton(this, ChoicePage_Change
, _T("C&hange current"));
251 m_textChange
= new wxTextCtrl(this, ChoicePage_ChangeText
, wxEmptyString
);
252 sizerRow
->Add(btn
, 0, wxRIGHT
, 5);
253 sizerRow
->Add(m_textChange
, 1, wxLEFT
, 5);
254 sizerMiddle
->Add(sizerRow
, 0, wxALL
| wxGROW
, 5);
256 sizerRow
= new wxBoxSizer(wxHORIZONTAL
);
257 btn
= new wxButton(this, ChoicePage_Delete
, _T("&Delete this item"));
258 m_textDelete
= new wxTextCtrl(this, ChoicePage_DeleteText
, wxEmptyString
);
259 sizerRow
->Add(btn
, 0, wxRIGHT
, 5);
260 sizerRow
->Add(m_textDelete
, 1, wxLEFT
, 5);
261 sizerMiddle
->Add(sizerRow
, 0, wxALL
| wxGROW
, 5);
263 btn
= new wxButton(this, ChoicePage_DeleteSel
, _T("Delete &selection"));
264 sizerMiddle
->Add(btn
, 0, wxALL
| wxGROW
, 5);
266 btn
= new wxButton(this, ChoicePage_Clear
, _T("&Clear"));
267 sizerMiddle
->Add(btn
, 0, wxALL
| wxGROW
, 5);
269 btn
= new wxButton(this, ChoicePage_ContainerTests
, _T("Run &tests"));
270 sizerMiddle
->Add(btn
, 0, wxALL
| wxGROW
, 5);
273 wxSizer
*sizerRight
= new wxBoxSizer(wxVERTICAL
);
274 m_choice
= new wxChoice(this, ChoicePage_Choice
);
275 sizerRight
->Add(m_choice
, 0, wxALL
| wxGROW
, 5);
276 sizerRight
->SetMinSize(150, 0);
277 m_sizerChoice
= sizerRight
; // save it to modify it later
279 // the 3 panes panes compose the window
280 sizerTop
->Add(sizerLeft
, 0, wxGROW
| (wxALL
& ~wxLEFT
), 10);
281 sizerTop
->Add(sizerMiddle
, 1, wxGROW
| wxALL
, 10);
282 sizerTop
->Add(sizerRight
, 1, wxGROW
| (wxALL
& ~wxRIGHT
), 10);
284 // final initializations
290 // ----------------------------------------------------------------------------
292 // ----------------------------------------------------------------------------
294 void ChoiceWidgetsPage::Reset()
296 m_chkSort
->SetValue(false);
299 void ChoiceWidgetsPage::CreateChoice()
301 int flags
= ms_defaultFlags
;
303 if ( m_chkSort
->GetValue() )
309 int count
= m_choice
->GetCount();
310 for ( int n
= 0; n
< count
; n
++ )
312 items
.Add(m_choice
->GetString(n
));
315 m_sizerChoice
->Detach( m_choice
);
319 m_choice
= new wxChoice(this, ChoicePage_Choice
,
320 wxDefaultPosition
, wxDefaultSize
,
324 m_choice
->Set(items
);
325 m_sizerChoice
->Add(m_choice
, 1, wxGROW
| wxALL
, 5);
326 m_sizerChoice
->Layout();
329 // ----------------------------------------------------------------------------
331 // ----------------------------------------------------------------------------
333 void ChoiceWidgetsPage::OnButtonReset(wxCommandEvent
& WXUNUSED(event
))
340 void ChoiceWidgetsPage::OnButtonChange(wxCommandEvent
& WXUNUSED(event
))
342 int selection
= m_choice
->GetSelection();
343 if(selection
!= wxNOT_FOUND
)
345 m_choice
->SetString(selection
, m_textChange
->GetValue());
349 void ChoiceWidgetsPage::OnButtonDelete(wxCommandEvent
& WXUNUSED(event
))
352 if ( !m_textDelete
->GetValue().ToULong(&n
) ||
353 (n
>= (unsigned)m_choice
->GetCount()) )
361 void ChoiceWidgetsPage::OnButtonDeleteSel(wxCommandEvent
& WXUNUSED(event
))
363 int selection
= m_choice
->GetSelection();
364 if(selection
!= wxNOT_FOUND
)
366 m_choice
->Delete(selection
);
370 void ChoiceWidgetsPage::OnButtonClear(wxCommandEvent
& WXUNUSED(event
))
375 void ChoiceWidgetsPage::OnButtonAdd(wxCommandEvent
& WXUNUSED(event
))
377 static unsigned int s_item
= 0;
379 wxString s
= m_textAdd
->GetValue();
380 if ( !m_textAdd
->IsModified() )
382 // update the default string
383 m_textAdd
->SetValue(wxString::Format(_T("test item %u"), ++s_item
));
389 void ChoiceWidgetsPage::OnButtonAddMany(wxCommandEvent
& WXUNUSED(event
))
391 // "many" means 1000 here
392 wxArrayString strings
;
393 for ( unsigned int n
= 0; n
< 1000; n
++ )
395 strings
.Add(wxString::Format(_T("item #%u"), n
));
397 m_choice
->Append(strings
);
400 void ChoiceWidgetsPage::OnButtonAddSeveral(wxCommandEvent
& WXUNUSED(event
))
403 items
.Add(_T("First"));
404 items
.Add(_T("another one"));
405 items
.Add(_T("and the last (very very very very very very very very very very long) one"));
406 m_choice
->Insert(items
, 0);
409 void ChoiceWidgetsPage::OnUpdateUIResetButton(wxUpdateUIEvent
& event
)
411 event
.Enable( m_chkSort
->GetValue() );
414 void ChoiceWidgetsPage::OnUpdateUIDeleteButton(wxUpdateUIEvent
& event
)
417 event
.Enable(m_textDelete
->GetValue().ToULong(&n
) &&
418 (n
< (unsigned)m_choice
->GetCount()));
421 void ChoiceWidgetsPage::OnUpdateUIDeleteSelButton(wxUpdateUIEvent
& event
)
423 wxArrayInt selections
;
424 event
.Enable(m_choice
->GetSelection() != wxNOT_FOUND
);
427 void ChoiceWidgetsPage::OnUpdateUIClearButton(wxUpdateUIEvent
& event
)
429 event
.Enable(m_choice
->GetCount() != 0);
432 void ChoiceWidgetsPage::OnUpdateUIAddSeveral(wxUpdateUIEvent
& event
)
434 event
.Enable(!m_choice
->HasFlag(wxCB_SORT
));
437 void ChoiceWidgetsPage::OnChoice(wxCommandEvent
& event
)
439 long sel
= event
.GetSelection();
440 m_textDelete
->SetValue(wxString::Format(_T("%ld"), sel
));
442 wxLogMessage(_T("Choice item %ld selected"), sel
);
445 void ChoiceWidgetsPage::OnCheckOrRadioBox(wxCommandEvent
& WXUNUSED(event
))
450 #endif // wxUSE_CHOICE