1 /////////////////////////////////////////////////////////////////////////////
2 // Program: wxWindows Widgets Sample
4 // Purpose: Part of the widgets sample showing wxComboBox
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/button.h"
32 #include "wx/checkbox.h"
33 #include "wx/combobox.h"
34 #include "wx/radiobox.h"
35 #include "wx/statbox.h"
36 #include "wx/textctrl.h"
43 #include "icons/combobox.xpm"
45 // ----------------------------------------------------------------------------
47 // ----------------------------------------------------------------------------
52 ComboPage_Reset
= 100,
67 // kinds of comboboxes
75 // ----------------------------------------------------------------------------
76 // ComboboxWidgetsPage
77 // ----------------------------------------------------------------------------
79 class ComboboxWidgetsPage
: public WidgetsPage
82 ComboboxWidgetsPage(wxNotebook
*notebook
, wxImageList
*imaglist
);
86 void OnButtonReset(wxCommandEvent
& event
);
87 void OnButtonChange(wxCommandEvent
& event
);
88 void OnButtonDelete(wxCommandEvent
& event
);
89 void OnButtonDeleteSel(wxCommandEvent
& event
);
90 void OnButtonClear(wxCommandEvent
& event
);
91 void OnButtonAdd(wxCommandEvent
& event
);
92 void OnButtonAddSeveral(wxCommandEvent
& event
);
93 void OnButtonAddMany(wxCommandEvent
& event
);
95 void OnComboBox(wxCommandEvent
& event
);
96 void OnComboText(wxCommandEvent
& event
);
98 void OnCheckOrRadioBox(wxCommandEvent
& event
);
100 void OnUpdateUICurText(wxUpdateUIEvent
& event
);
102 void OnUpdateUIAddSeveral(wxUpdateUIEvent
& event
);
103 void OnUpdateUIClearButton(wxUpdateUIEvent
& event
);
104 void OnUpdateUIDeleteButton(wxUpdateUIEvent
& event
);
105 void OnUpdateUIDeleteSelButton(wxUpdateUIEvent
& event
);
106 void OnUpdateUIResetButton(wxUpdateUIEvent
& event
);
108 // reset the combobox parameters
111 // (re)create the combobox
117 // the sel mode radiobox
118 wxRadioBox
*m_radioKind
;
120 // the checkboxes for styles
121 wxCheckBox
*m_chkSort
,
124 // the combobox itself and the sizer it is in
125 wxComboBox
*m_combobox
;
126 wxSizer
*m_sizerCombo
;
128 // the text entries for "Add/change string" and "Delete" buttons
129 wxTextCtrl
*m_textAdd
,
134 DECLARE_EVENT_TABLE();
135 DECLARE_WIDGETS_PAGE(ComboboxWidgetsPage
);
138 // ----------------------------------------------------------------------------
140 // ----------------------------------------------------------------------------
142 BEGIN_EVENT_TABLE(ComboboxWidgetsPage
, WidgetsPage
)
143 EVT_BUTTON(ComboPage_Reset
, ComboboxWidgetsPage::OnButtonReset
)
144 EVT_BUTTON(ComboPage_Change
, ComboboxWidgetsPage::OnButtonChange
)
145 EVT_BUTTON(ComboPage_Delete
, ComboboxWidgetsPage::OnButtonDelete
)
146 EVT_BUTTON(ComboPage_DeleteSel
, ComboboxWidgetsPage::OnButtonDeleteSel
)
147 EVT_BUTTON(ComboPage_Clear
, ComboboxWidgetsPage::OnButtonClear
)
148 EVT_BUTTON(ComboPage_Add
, ComboboxWidgetsPage::OnButtonAdd
)
149 EVT_BUTTON(ComboPage_AddSeveral
, ComboboxWidgetsPage::OnButtonAddSeveral
)
150 EVT_BUTTON(ComboPage_AddMany
, ComboboxWidgetsPage::OnButtonAddMany
)
152 EVT_TEXT_ENTER(ComboPage_AddText
, ComboboxWidgetsPage::OnButtonAdd
)
153 EVT_TEXT_ENTER(ComboPage_DeleteText
, ComboboxWidgetsPage::OnButtonDelete
)
155 EVT_UPDATE_UI(ComboPage_CurText
, ComboboxWidgetsPage::OnUpdateUICurText
)
157 EVT_UPDATE_UI(ComboPage_Reset
, ComboboxWidgetsPage::OnUpdateUIResetButton
)
158 EVT_UPDATE_UI(ComboPage_AddSeveral
, ComboboxWidgetsPage::OnUpdateUIAddSeveral
)
159 EVT_UPDATE_UI(ComboPage_Clear
, ComboboxWidgetsPage::OnUpdateUIClearButton
)
160 EVT_UPDATE_UI(ComboPage_DeleteText
, ComboboxWidgetsPage::OnUpdateUIClearButton
)
161 EVT_UPDATE_UI(ComboPage_Delete
, ComboboxWidgetsPage::OnUpdateUIDeleteButton
)
162 EVT_UPDATE_UI(ComboPage_Change
, ComboboxWidgetsPage::OnUpdateUIDeleteSelButton
)
163 EVT_UPDATE_UI(ComboPage_ChangeText
, ComboboxWidgetsPage::OnUpdateUIDeleteSelButton
)
164 EVT_UPDATE_UI(ComboPage_DeleteSel
, ComboboxWidgetsPage::OnUpdateUIDeleteSelButton
)
166 EVT_COMBOBOX(ComboPage_Combo
, ComboboxWidgetsPage::OnComboBox
)
167 EVT_TEXT(ComboPage_Combo
, ComboboxWidgetsPage::OnComboText
)
169 EVT_CHECKBOX(-1, ComboboxWidgetsPage::OnCheckOrRadioBox
)
170 EVT_RADIOBOX(-1, ComboboxWidgetsPage::OnCheckOrRadioBox
)
173 // ============================================================================
175 // ============================================================================
177 IMPLEMENT_WIDGETS_PAGE(ComboboxWidgetsPage
, _T("Combobox"));
179 ComboboxWidgetsPage::ComboboxWidgetsPage(wxNotebook
*notebook
,
180 wxImageList
*imaglist
)
181 : WidgetsPage(notebook
)
183 imaglist
->Add(wxBitmap(combobox_xpm
));
187 m_chkReadonly
= (wxCheckBox
*)NULL
;
189 m_combobox
= (wxComboBox
*)NULL
;
190 m_sizerCombo
= (wxSizer
*)NULL
;
193 What we create here is a frame having 3 panes: style pane is the
194 leftmost one, in the middle the pane with buttons allowing to perform
195 miscellaneous combobox operations and the pane containing the combobox
198 wxSizer
*sizerTop
= new wxBoxSizer(wxHORIZONTAL
);
201 wxStaticBox
*box
= new wxStaticBox(this, -1, _T("&Set style"));
203 // should be in sync with ComboKind_XXX values
204 static const wxString kinds
[] =
211 m_radioKind
= new wxRadioBox(this, -1, _T("Combobox &kind:"),
212 wxDefaultPosition
, wxDefaultSize
,
213 WXSIZEOF(kinds
), kinds
,
214 1, wxRA_SPECIFY_COLS
);
216 wxSizer
*sizerLeft
= new wxStaticBoxSizer(box
, wxVERTICAL
);
218 m_chkSort
= CreateCheckBoxAndAddToSizer(sizerLeft
, _T("&Sort items"));
219 m_chkReadonly
= CreateCheckBoxAndAddToSizer(sizerLeft
, _T("&Read only"));
221 sizerLeft
->Add(5, 5, 0, wxGROW
| wxALL
, 5); // spacer
222 sizerLeft
->Add(m_radioKind
, 0, wxGROW
| wxALL
, 5);
224 wxButton
*btn
= new wxButton(this, ComboPage_Reset
, _T("&Reset"));
225 sizerLeft
->Add(btn
, 0, wxALIGN_CENTRE_HORIZONTAL
| wxALL
, 15);
228 wxStaticBox
*box2
= new wxStaticBox(this, -1, _T("&Change combobox contents"));
229 wxSizer
*sizerMiddle
= new wxStaticBoxSizer(box2
, wxVERTICAL
);
234 sizerRow
= CreateSizerWithTextAndLabel(_T("Current selection"),
237 text
->SetEditable(FALSE
);
239 sizerMiddle
->Add(sizerRow
, 0, wxALL
| wxGROW
, 5);
241 sizerRow
= CreateSizerWithTextAndButton(ComboPage_Add
,
242 _T("&Add this string"),
245 sizerMiddle
->Add(sizerRow
, 0, wxALL
| wxGROW
, 5);
247 btn
= new wxButton(this, ComboPage_AddSeveral
, _T("&Insert a few strings"));
248 sizerMiddle
->Add(btn
, 0, wxALL
| wxGROW
, 5);
250 btn
= new wxButton(this, ComboPage_AddMany
, _T("Add &many strings"));
251 sizerMiddle
->Add(btn
, 0, wxALL
| wxGROW
, 5);
253 sizerRow
= CreateSizerWithTextAndButton(ComboPage_Change
,
254 _T("C&hange current"),
255 ComboPage_ChangeText
,
257 sizerMiddle
->Add(sizerRow
, 0, wxALL
| wxGROW
, 5);
259 sizerRow
= CreateSizerWithTextAndButton(ComboPage_Delete
,
260 _T("&Delete this item"),
261 ComboPage_DeleteText
,
263 sizerMiddle
->Add(sizerRow
, 0, wxALL
| wxGROW
, 5);
265 btn
= new wxButton(this, ComboPage_DeleteSel
, _T("Delete &selection"));
266 sizerMiddle
->Add(btn
, 0, wxALL
| wxGROW
, 5);
268 btn
= new wxButton(this, ComboPage_Clear
, _T("&Clear"));
269 sizerMiddle
->Add(btn
, 0, wxALL
| wxGROW
, 5);
272 wxSizer
*sizerRight
= new wxBoxSizer(wxVERTICAL
);
273 m_combobox
= new wxComboBox(this, ComboPage_Combo
, _T(""),
274 wxDefaultPosition
, wxDefaultSize
,
277 sizerRight
->Add(m_combobox
, 1, wxGROW
| wxALL
, 5);
278 sizerRight
->SetMinSize(250, 0);
279 m_sizerCombo
= sizerRight
; // save it to modify it later
281 // the 3 panes panes compose the window
282 sizerTop
->Add(sizerLeft
, 0, wxGROW
| (wxALL
& ~wxLEFT
), 10);
283 sizerTop
->Add(sizerMiddle
, 1, wxGROW
| wxALL
, 10);
284 sizerTop
->Add(sizerRight
, 1, wxGROW
| (wxALL
& ~wxRIGHT
), 10);
286 // final initializations
295 // ----------------------------------------------------------------------------
297 // ----------------------------------------------------------------------------
299 void ComboboxWidgetsPage::Reset()
301 m_chkSort
->SetValue(FALSE
);
302 m_chkReadonly
->SetValue(FALSE
);
305 void ComboboxWidgetsPage::CreateCombo()
309 if ( m_chkSort
->GetValue() )
311 if ( m_chkReadonly
->GetValue() )
312 flags
|= wxCB_READONLY
;
314 switch ( m_radioKind
->GetSelection() )
317 wxFAIL_MSG( _T("unknown combo kind") );
320 case ComboKind_Default
:
323 case ComboKind_Simple
:
324 flags
|= wxCB_SIMPLE
;
327 case ComboKind_DropDown
:
328 flags
= wxCB_DROPDOWN
;
335 int count
= m_combobox
->GetCount();
336 for ( int n
= 0; n
< count
; n
++ )
338 items
.Add(m_combobox
->GetString(n
));
341 m_sizerCombo
->Remove(m_combobox
);
345 m_combobox
= new wxComboBox(this, ComboPage_Combo
, _T(""),
346 wxDefaultPosition
, wxDefaultSize
,
350 size_t count
= items
.GetCount();
351 for ( size_t n
= 0; n
< count
; n
++ )
353 m_combobox
->Append(items
[n
]);
356 m_sizerCombo
->Add(m_combobox
, 1, wxGROW
| wxALL
, 5);
357 m_sizerCombo
->Layout();
360 // ----------------------------------------------------------------------------
362 // ----------------------------------------------------------------------------
364 void ComboboxWidgetsPage::OnButtonReset(wxCommandEvent
& WXUNUSED(event
))
371 void ComboboxWidgetsPage::OnButtonChange(wxCommandEvent
& WXUNUSED(event
))
373 int sel
= m_combobox
->GetSelection();
377 m_combobox
->SetString(sel
, m_textChange
->GetValue());
379 wxLogMessage(_T("Not implemented in wxGTK"));
384 void ComboboxWidgetsPage::OnButtonDelete(wxCommandEvent
& WXUNUSED(event
))
387 if ( !m_textDelete
->GetValue().ToULong(&n
) ||
388 (n
>= (unsigned)m_combobox
->GetCount()) )
393 m_combobox
->Delete(n
);
396 void ComboboxWidgetsPage::OnButtonDeleteSel(wxCommandEvent
& WXUNUSED(event
))
398 int sel
= m_combobox
->GetSelection();
401 m_combobox
->Delete(sel
);
405 void ComboboxWidgetsPage::OnButtonClear(wxCommandEvent
& event
)
410 void ComboboxWidgetsPage::OnButtonAdd(wxCommandEvent
& event
)
412 static size_t s_item
= 0;
414 wxString s
= m_textAdd
->GetValue();
415 if ( !m_textAdd
->IsModified() )
417 // update the default string
418 m_textAdd
->SetValue(wxString::Format(_T("test item %u"), ++s_item
));
421 m_combobox
->Append(s
);
424 void ComboboxWidgetsPage::OnButtonAddMany(wxCommandEvent
& WXUNUSED(event
))
426 // "many" means 1000 here
427 for ( size_t n
= 0; n
< 1000; n
++ )
429 m_combobox
->Append(wxString::Format(_T("item #%u"), n
));
433 void ComboboxWidgetsPage::OnButtonAddSeveral(wxCommandEvent
& event
)
435 m_combobox
->Append(_T("First"));
436 m_combobox
->Append(_T("another one"));
437 m_combobox
->Append(_T("and the last (very very very very very very very very very very long) one"));
440 void ComboboxWidgetsPage::OnUpdateUICurText(wxUpdateUIEvent
& event
)
442 event
.SetText( wxString::Format(_T("%d"), m_combobox
->GetSelection()) );
445 void ComboboxWidgetsPage::OnUpdateUIResetButton(wxUpdateUIEvent
& event
)
447 event
.Enable( m_chkSort
->GetValue() || m_chkReadonly
->GetValue() );
450 void ComboboxWidgetsPage::OnUpdateUIDeleteButton(wxUpdateUIEvent
& event
)
453 event
.Enable(m_textDelete
->GetValue().ToULong(&n
) &&
454 (n
< (unsigned)m_combobox
->GetCount()));
457 void ComboboxWidgetsPage::OnUpdateUIDeleteSelButton(wxUpdateUIEvent
& event
)
459 event
.Enable(m_combobox
->GetSelection() != -1);
462 void ComboboxWidgetsPage::OnUpdateUIClearButton(wxUpdateUIEvent
& event
)
464 event
.Enable(m_combobox
->GetCount() != 0);
467 void ComboboxWidgetsPage::OnUpdateUIAddSeveral(wxUpdateUIEvent
& event
)
469 event
.Enable(!(m_combobox
->GetWindowStyle() & wxCB_SORT
));
472 void ComboboxWidgetsPage::OnComboText(wxCommandEvent
& event
)
474 wxString s
= event
.GetString();
476 wxASSERT_MSG( s
== m_combobox
->GetValue(),
477 _T("event and combobox values should be the same") );
479 wxLogMessage(_T("Combobox text changed (now '%s')"), s
.c_str());
482 void ComboboxWidgetsPage::OnComboBox(wxCommandEvent
& event
)
484 int sel
= event
.GetInt();
485 m_textDelete
->SetValue(wxString::Format(_T("%ld"), sel
));
487 wxLogMessage(_T("Combobox item %d selected"), sel
);
490 void ComboboxWidgetsPage::OnCheckOrRadioBox(wxCommandEvent
& event
)