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"
29 // for all others, include the necessary headers
33 #include "wx/button.h"
34 #include "wx/checkbox.h"
35 #include "wx/combobox.h"
36 #include "wx/radiobox.h"
37 #include "wx/statbox.h"
38 #include "wx/textctrl.h"
45 #include "icons/combobox.xpm"
47 // ----------------------------------------------------------------------------
49 // ----------------------------------------------------------------------------
54 ComboPage_Reset
= 100,
69 // kinds of comboboxes
77 // ----------------------------------------------------------------------------
78 // ComboboxWidgetsPage
79 // ----------------------------------------------------------------------------
81 class ComboboxWidgetsPage
: public WidgetsPage
84 ComboboxWidgetsPage(wxNotebook
*notebook
, wxImageList
*imaglist
);
88 void OnButtonReset(wxCommandEvent
& event
);
89 void OnButtonChange(wxCommandEvent
& event
);
90 void OnButtonDelete(wxCommandEvent
& event
);
91 void OnButtonDeleteSel(wxCommandEvent
& event
);
92 void OnButtonClear(wxCommandEvent
& event
);
93 void OnButtonAdd(wxCommandEvent
& event
);
94 void OnButtonAddSeveral(wxCommandEvent
& event
);
95 void OnButtonAddMany(wxCommandEvent
& event
);
97 void OnComboBox(wxCommandEvent
& event
);
98 void OnComboText(wxCommandEvent
& event
);
100 void OnCheckOrRadioBox(wxCommandEvent
& event
);
102 void OnUpdateUICurText(wxUpdateUIEvent
& event
);
104 void OnUpdateUIAddSeveral(wxUpdateUIEvent
& event
);
105 void OnUpdateUIClearButton(wxUpdateUIEvent
& event
);
106 void OnUpdateUIDeleteButton(wxUpdateUIEvent
& event
);
107 void OnUpdateUIDeleteSelButton(wxUpdateUIEvent
& event
);
108 void OnUpdateUIResetButton(wxUpdateUIEvent
& event
);
110 // reset the combobox parameters
113 // (re)create the combobox
119 // the sel mode radiobox
120 wxRadioBox
*m_radioKind
;
122 // the checkboxes for styles
123 wxCheckBox
*m_chkSort
,
126 // the combobox itself and the sizer it is in
127 wxComboBox
*m_combobox
;
128 wxSizer
*m_sizerCombo
;
130 // the text entries for "Add/change string" and "Delete" buttons
131 wxTextCtrl
*m_textAdd
,
136 DECLARE_EVENT_TABLE()
137 DECLARE_WIDGETS_PAGE(ComboboxWidgetsPage
)
140 // ----------------------------------------------------------------------------
142 // ----------------------------------------------------------------------------
144 BEGIN_EVENT_TABLE(ComboboxWidgetsPage
, WidgetsPage
)
145 EVT_BUTTON(ComboPage_Reset
, ComboboxWidgetsPage::OnButtonReset
)
146 EVT_BUTTON(ComboPage_Change
, ComboboxWidgetsPage::OnButtonChange
)
147 EVT_BUTTON(ComboPage_Delete
, ComboboxWidgetsPage::OnButtonDelete
)
148 EVT_BUTTON(ComboPage_DeleteSel
, ComboboxWidgetsPage::OnButtonDeleteSel
)
149 EVT_BUTTON(ComboPage_Clear
, ComboboxWidgetsPage::OnButtonClear
)
150 EVT_BUTTON(ComboPage_Add
, ComboboxWidgetsPage::OnButtonAdd
)
151 EVT_BUTTON(ComboPage_AddSeveral
, ComboboxWidgetsPage::OnButtonAddSeveral
)
152 EVT_BUTTON(ComboPage_AddMany
, ComboboxWidgetsPage::OnButtonAddMany
)
154 EVT_TEXT_ENTER(ComboPage_AddText
, ComboboxWidgetsPage::OnButtonAdd
)
155 EVT_TEXT_ENTER(ComboPage_DeleteText
, ComboboxWidgetsPage::OnButtonDelete
)
157 EVT_UPDATE_UI(ComboPage_CurText
, ComboboxWidgetsPage::OnUpdateUICurText
)
159 EVT_UPDATE_UI(ComboPage_Reset
, ComboboxWidgetsPage::OnUpdateUIResetButton
)
160 EVT_UPDATE_UI(ComboPage_AddSeveral
, ComboboxWidgetsPage::OnUpdateUIAddSeveral
)
161 EVT_UPDATE_UI(ComboPage_Clear
, ComboboxWidgetsPage::OnUpdateUIClearButton
)
162 EVT_UPDATE_UI(ComboPage_DeleteText
, ComboboxWidgetsPage::OnUpdateUIClearButton
)
163 EVT_UPDATE_UI(ComboPage_Delete
, ComboboxWidgetsPage::OnUpdateUIDeleteButton
)
164 EVT_UPDATE_UI(ComboPage_Change
, ComboboxWidgetsPage::OnUpdateUIDeleteSelButton
)
165 EVT_UPDATE_UI(ComboPage_ChangeText
, ComboboxWidgetsPage::OnUpdateUIDeleteSelButton
)
166 EVT_UPDATE_UI(ComboPage_DeleteSel
, ComboboxWidgetsPage::OnUpdateUIDeleteSelButton
)
168 EVT_COMBOBOX(ComboPage_Combo
, ComboboxWidgetsPage::OnComboBox
)
169 EVT_TEXT(ComboPage_Combo
, ComboboxWidgetsPage::OnComboText
)
171 EVT_CHECKBOX(-1, ComboboxWidgetsPage::OnCheckOrRadioBox
)
172 EVT_RADIOBOX(-1, ComboboxWidgetsPage::OnCheckOrRadioBox
)
175 // ============================================================================
177 // ============================================================================
179 IMPLEMENT_WIDGETS_PAGE(ComboboxWidgetsPage
, _T("Combobox"));
181 ComboboxWidgetsPage::ComboboxWidgetsPage(wxNotebook
*notebook
,
182 wxImageList
*imaglist
)
183 : WidgetsPage(notebook
)
187 m_chkReadonly
= (wxCheckBox
*)NULL
;
189 m_combobox
= (wxComboBox
*)NULL
;
190 m_sizerCombo
= (wxSizer
*)NULL
;
192 imaglist
->Add(wxBitmap(combobox_xpm
));
195 What we create here is a frame having 3 panes: style pane is the
196 leftmost one, in the middle the pane with buttons allowing to perform
197 miscellaneous combobox operations and the pane containing the combobox
200 wxSizer
*sizerTop
= new wxBoxSizer(wxHORIZONTAL
);
203 wxStaticBox
*box
= new wxStaticBox(this, -1, _T("&Set style"));
205 // should be in sync with ComboKind_XXX values
206 static const wxString kinds
[] =
213 m_radioKind
= new wxRadioBox(this, -1, _T("Combobox &kind:"),
214 wxDefaultPosition
, wxDefaultSize
,
215 WXSIZEOF(kinds
), kinds
,
216 1, wxRA_SPECIFY_COLS
);
218 wxSizer
*sizerLeft
= new wxStaticBoxSizer(box
, wxVERTICAL
);
220 m_chkSort
= CreateCheckBoxAndAddToSizer(sizerLeft
, _T("&Sort items"));
221 m_chkReadonly
= CreateCheckBoxAndAddToSizer(sizerLeft
, _T("&Read only"));
223 sizerLeft
->Add(5, 5, 0, wxGROW
| wxALL
, 5); // spacer
224 sizerLeft
->Add(m_radioKind
, 0, wxGROW
| wxALL
, 5);
226 wxButton
*btn
= new wxButton(this, ComboPage_Reset
, _T("&Reset"));
227 sizerLeft
->Add(btn
, 0, wxALIGN_CENTRE_HORIZONTAL
| wxALL
, 15);
230 wxStaticBox
*box2
= new wxStaticBox(this, -1, _T("&Change combobox contents"));
231 wxSizer
*sizerMiddle
= new wxStaticBoxSizer(box2
, wxVERTICAL
);
236 sizerRow
= CreateSizerWithTextAndLabel(_T("Current selection"),
239 text
->SetEditable(FALSE
);
241 sizerMiddle
->Add(sizerRow
, 0, wxALL
| wxGROW
, 5);
243 sizerRow
= CreateSizerWithTextAndButton(ComboPage_Add
,
244 _T("&Add this string"),
247 sizerMiddle
->Add(sizerRow
, 0, wxALL
| wxGROW
, 5);
249 btn
= new wxButton(this, ComboPage_AddSeveral
, _T("&Insert a few strings"));
250 sizerMiddle
->Add(btn
, 0, wxALL
| wxGROW
, 5);
252 btn
= new wxButton(this, ComboPage_AddMany
, _T("Add &many strings"));
253 sizerMiddle
->Add(btn
, 0, wxALL
| wxGROW
, 5);
255 sizerRow
= CreateSizerWithTextAndButton(ComboPage_Change
,
256 _T("C&hange current"),
257 ComboPage_ChangeText
,
259 sizerMiddle
->Add(sizerRow
, 0, wxALL
| wxGROW
, 5);
261 sizerRow
= CreateSizerWithTextAndButton(ComboPage_Delete
,
262 _T("&Delete this item"),
263 ComboPage_DeleteText
,
265 sizerMiddle
->Add(sizerRow
, 0, wxALL
| wxGROW
, 5);
267 btn
= new wxButton(this, ComboPage_DeleteSel
, _T("Delete &selection"));
268 sizerMiddle
->Add(btn
, 0, wxALL
| wxGROW
, 5);
270 btn
= new wxButton(this, ComboPage_Clear
, _T("&Clear"));
271 sizerMiddle
->Add(btn
, 0, wxALL
| wxGROW
, 5);
274 wxSizer
*sizerRight
= new wxBoxSizer(wxVERTICAL
);
275 m_combobox
= new wxComboBox(this, ComboPage_Combo
, _T(""),
276 wxDefaultPosition
, wxDefaultSize
,
279 sizerRight
->Add(m_combobox
, 1, wxGROW
| wxALL
, 5);
280 sizerRight
->SetMinSize(150, 0);
281 m_sizerCombo
= sizerRight
; // save it to modify it later
283 // the 3 panes panes compose the window
284 sizerTop
->Add(sizerLeft
, 0, wxGROW
| (wxALL
& ~wxLEFT
), 10);
285 sizerTop
->Add(sizerMiddle
, 1, wxGROW
| wxALL
, 10);
286 sizerTop
->Add(sizerRight
, 1, wxGROW
| (wxALL
& ~wxRIGHT
), 10);
288 // final initializations
297 // ----------------------------------------------------------------------------
299 // ----------------------------------------------------------------------------
301 void ComboboxWidgetsPage::Reset()
303 m_chkSort
->SetValue(FALSE
);
304 m_chkReadonly
->SetValue(FALSE
);
307 void ComboboxWidgetsPage::CreateCombo()
311 if ( m_chkSort
->GetValue() )
313 if ( m_chkReadonly
->GetValue() )
314 flags
|= wxCB_READONLY
;
316 switch ( m_radioKind
->GetSelection() )
319 wxFAIL_MSG( _T("unknown combo kind") );
322 case ComboKind_Default
:
325 case ComboKind_Simple
:
326 flags
|= wxCB_SIMPLE
;
329 case ComboKind_DropDown
:
330 flags
= wxCB_DROPDOWN
;
337 int count
= m_combobox
->GetCount();
338 for ( int n
= 0; n
< count
; n
++ )
340 items
.Add(m_combobox
->GetString(n
));
343 m_sizerCombo
->Remove(m_combobox
);
347 m_combobox
= new wxComboBox(this, ComboPage_Combo
, _T(""),
348 wxDefaultPosition
, wxDefaultSize
,
352 size_t count
= items
.GetCount();
353 for ( size_t n
= 0; n
< count
; n
++ )
355 m_combobox
->Append(items
[n
]);
358 m_sizerCombo
->Add(m_combobox
, 1, wxGROW
| wxALL
, 5);
359 m_sizerCombo
->Layout();
362 // ----------------------------------------------------------------------------
364 // ----------------------------------------------------------------------------
366 void ComboboxWidgetsPage::OnButtonReset(wxCommandEvent
& WXUNUSED(event
))
373 void ComboboxWidgetsPage::OnButtonChange(wxCommandEvent
& WXUNUSED(event
))
375 int sel
= m_combobox
->GetSelection();
379 m_combobox
->SetString(sel
, m_textChange
->GetValue());
381 wxLogMessage(_T("Not implemented in wxGTK"));
386 void ComboboxWidgetsPage::OnButtonDelete(wxCommandEvent
& WXUNUSED(event
))
389 if ( !m_textDelete
->GetValue().ToULong(&n
) ||
390 (n
>= (unsigned)m_combobox
->GetCount()) )
395 m_combobox
->Delete(n
);
398 void ComboboxWidgetsPage::OnButtonDeleteSel(wxCommandEvent
& WXUNUSED(event
))
400 int sel
= m_combobox
->GetSelection();
403 m_combobox
->Delete(sel
);
407 void ComboboxWidgetsPage::OnButtonClear(wxCommandEvent
& event
)
412 void ComboboxWidgetsPage::OnButtonAdd(wxCommandEvent
& event
)
414 static size_t s_item
= 0;
416 wxString s
= m_textAdd
->GetValue();
417 if ( !m_textAdd
->IsModified() )
419 // update the default string
420 m_textAdd
->SetValue(wxString::Format(_T("test item %u"), ++s_item
));
423 m_combobox
->Append(s
);
426 void ComboboxWidgetsPage::OnButtonAddMany(wxCommandEvent
& WXUNUSED(event
))
428 // "many" means 1000 here
429 for ( size_t n
= 0; n
< 1000; n
++ )
431 m_combobox
->Append(wxString::Format(_T("item #%u"), n
));
435 void ComboboxWidgetsPage::OnButtonAddSeveral(wxCommandEvent
& event
)
437 m_combobox
->Append(_T("First"));
438 m_combobox
->Append(_T("another one"));
439 m_combobox
->Append(_T("and the last (very very very very very very very very very very long) one"));
442 void ComboboxWidgetsPage::OnUpdateUICurText(wxUpdateUIEvent
& event
)
445 event
.SetText( wxString::Format(_T("%d"), m_combobox
->GetSelection()) );
448 void ComboboxWidgetsPage::OnUpdateUIResetButton(wxUpdateUIEvent
& event
)
451 event
.Enable( m_chkSort
->GetValue() || m_chkReadonly
->GetValue() );
454 void ComboboxWidgetsPage::OnUpdateUIDeleteButton(wxUpdateUIEvent
& event
)
459 event
.Enable(m_textDelete
->GetValue().ToULong(&n
) &&
460 (n
< (unsigned)m_combobox
->GetCount()));
464 void ComboboxWidgetsPage::OnUpdateUIDeleteSelButton(wxUpdateUIEvent
& event
)
467 event
.Enable(m_combobox
->GetSelection() != -1);
470 void ComboboxWidgetsPage::OnUpdateUIClearButton(wxUpdateUIEvent
& event
)
473 event
.Enable(m_combobox
->GetCount() != 0);
476 void ComboboxWidgetsPage::OnUpdateUIAddSeveral(wxUpdateUIEvent
& event
)
479 event
.Enable(!(m_combobox
->GetWindowStyle() & wxCB_SORT
));
482 void ComboboxWidgetsPage::OnComboText(wxCommandEvent
& event
)
487 wxString s
= event
.GetString();
489 wxASSERT_MSG( s
== m_combobox
->GetValue(),
490 _T("event and combobox values should be the same") );
492 wxLogMessage(_T("Combobox text changed (now '%s')"), s
.c_str());
495 void ComboboxWidgetsPage::OnComboBox(wxCommandEvent
& event
)
497 int sel
= event
.GetInt();
498 m_textDelete
->SetValue(wxString::Format(_T("%ld"), sel
));
500 wxLogMessage(_T("Combobox item %d selected"), sel
);
503 void ComboboxWidgetsPage::OnCheckOrRadioBox(wxCommandEvent
& event
)
508 #endif //wxUSE_COMBOBOX