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/bitmap.h"
34 #include "wx/button.h"
35 #include "wx/checkbox.h"
36 #include "wx/combobox.h"
37 #include "wx/radiobox.h"
38 #include "wx/statbox.h"
39 #include "wx/textctrl.h"
46 #include "icons/combobox.xpm"
48 // ----------------------------------------------------------------------------
50 // ----------------------------------------------------------------------------
55 ComboPage_Reset
= 100,
70 // kinds of comboboxes
78 // ----------------------------------------------------------------------------
79 // ComboboxWidgetsPage
80 // ----------------------------------------------------------------------------
82 class ComboboxWidgetsPage
: public WidgetsPage
85 ComboboxWidgetsPage(wxNotebook
*notebook
, wxImageList
*imaglist
);
89 void OnButtonReset(wxCommandEvent
& event
);
90 void OnButtonChange(wxCommandEvent
& event
);
91 void OnButtonDelete(wxCommandEvent
& event
);
92 void OnButtonDeleteSel(wxCommandEvent
& event
);
93 void OnButtonClear(wxCommandEvent
& event
);
94 void OnButtonAdd(wxCommandEvent
& event
);
95 void OnButtonAddSeveral(wxCommandEvent
& event
);
96 void OnButtonAddMany(wxCommandEvent
& event
);
98 void OnComboBox(wxCommandEvent
& event
);
99 void OnComboText(wxCommandEvent
& event
);
101 void OnCheckOrRadioBox(wxCommandEvent
& event
);
103 void OnUpdateUICurText(wxUpdateUIEvent
& event
);
105 void OnUpdateUIAddSeveral(wxUpdateUIEvent
& event
);
106 void OnUpdateUIClearButton(wxUpdateUIEvent
& event
);
107 void OnUpdateUIDeleteButton(wxUpdateUIEvent
& event
);
108 void OnUpdateUIDeleteSelButton(wxUpdateUIEvent
& event
);
109 void OnUpdateUIResetButton(wxUpdateUIEvent
& event
);
111 // reset the combobox parameters
114 // (re)create the combobox
120 // the sel mode radiobox
121 wxRadioBox
*m_radioKind
;
123 // the checkboxes for styles
124 wxCheckBox
*m_chkSort
,
127 // the combobox itself and the sizer it is in
128 wxComboBox
*m_combobox
;
129 wxSizer
*m_sizerCombo
;
131 // the text entries for "Add/change string" and "Delete" buttons
132 wxTextCtrl
*m_textAdd
,
137 DECLARE_EVENT_TABLE()
138 DECLARE_WIDGETS_PAGE(ComboboxWidgetsPage
)
141 // ----------------------------------------------------------------------------
143 // ----------------------------------------------------------------------------
145 BEGIN_EVENT_TABLE(ComboboxWidgetsPage
, WidgetsPage
)
146 EVT_BUTTON(ComboPage_Reset
, ComboboxWidgetsPage::OnButtonReset
)
147 EVT_BUTTON(ComboPage_Change
, ComboboxWidgetsPage::OnButtonChange
)
148 EVT_BUTTON(ComboPage_Delete
, ComboboxWidgetsPage::OnButtonDelete
)
149 EVT_BUTTON(ComboPage_DeleteSel
, ComboboxWidgetsPage::OnButtonDeleteSel
)
150 EVT_BUTTON(ComboPage_Clear
, ComboboxWidgetsPage::OnButtonClear
)
151 EVT_BUTTON(ComboPage_Add
, ComboboxWidgetsPage::OnButtonAdd
)
152 EVT_BUTTON(ComboPage_AddSeveral
, ComboboxWidgetsPage::OnButtonAddSeveral
)
153 EVT_BUTTON(ComboPage_AddMany
, ComboboxWidgetsPage::OnButtonAddMany
)
155 EVT_TEXT_ENTER(ComboPage_AddText
, ComboboxWidgetsPage::OnButtonAdd
)
156 EVT_TEXT_ENTER(ComboPage_DeleteText
, ComboboxWidgetsPage::OnButtonDelete
)
158 EVT_UPDATE_UI(ComboPage_CurText
, ComboboxWidgetsPage::OnUpdateUICurText
)
160 EVT_UPDATE_UI(ComboPage_Reset
, ComboboxWidgetsPage::OnUpdateUIResetButton
)
161 EVT_UPDATE_UI(ComboPage_AddSeveral
, ComboboxWidgetsPage::OnUpdateUIAddSeveral
)
162 EVT_UPDATE_UI(ComboPage_Clear
, ComboboxWidgetsPage::OnUpdateUIClearButton
)
163 EVT_UPDATE_UI(ComboPage_DeleteText
, ComboboxWidgetsPage::OnUpdateUIClearButton
)
164 EVT_UPDATE_UI(ComboPage_Delete
, ComboboxWidgetsPage::OnUpdateUIDeleteButton
)
165 EVT_UPDATE_UI(ComboPage_Change
, ComboboxWidgetsPage::OnUpdateUIDeleteSelButton
)
166 EVT_UPDATE_UI(ComboPage_ChangeText
, ComboboxWidgetsPage::OnUpdateUIDeleteSelButton
)
167 EVT_UPDATE_UI(ComboPage_DeleteSel
, ComboboxWidgetsPage::OnUpdateUIDeleteSelButton
)
169 EVT_COMBOBOX(ComboPage_Combo
, ComboboxWidgetsPage::OnComboBox
)
170 EVT_TEXT(ComboPage_Combo
, ComboboxWidgetsPage::OnComboText
)
172 EVT_CHECKBOX(-1, ComboboxWidgetsPage::OnCheckOrRadioBox
)
173 EVT_RADIOBOX(-1, ComboboxWidgetsPage::OnCheckOrRadioBox
)
176 // ============================================================================
178 // ============================================================================
180 IMPLEMENT_WIDGETS_PAGE(ComboboxWidgetsPage
, _T("Combobox"));
182 ComboboxWidgetsPage::ComboboxWidgetsPage(wxNotebook
*notebook
,
183 wxImageList
*imaglist
)
184 : WidgetsPage(notebook
)
188 m_chkReadonly
= (wxCheckBox
*)NULL
;
190 m_combobox
= (wxComboBox
*)NULL
;
191 m_sizerCombo
= (wxSizer
*)NULL
;
193 imaglist
->Add(wxBitmap(combobox_xpm
));
196 What we create here is a frame having 3 panes: style pane is the
197 leftmost one, in the middle the pane with buttons allowing to perform
198 miscellaneous combobox operations and the pane containing the combobox
201 wxSizer
*sizerTop
= new wxBoxSizer(wxHORIZONTAL
);
204 wxStaticBox
*box
= new wxStaticBox(this, -1, _T("&Set style"));
206 // should be in sync with ComboKind_XXX values
207 static const wxString kinds
[] =
214 m_radioKind
= new wxRadioBox(this, -1, _T("Combobox &kind:"),
215 wxDefaultPosition
, wxDefaultSize
,
216 WXSIZEOF(kinds
), kinds
,
217 1, wxRA_SPECIFY_COLS
);
219 wxSizer
*sizerLeft
= new wxStaticBoxSizer(box
, wxVERTICAL
);
221 m_chkSort
= CreateCheckBoxAndAddToSizer(sizerLeft
, _T("&Sort items"));
222 m_chkReadonly
= CreateCheckBoxAndAddToSizer(sizerLeft
, _T("&Read only"));
224 sizerLeft
->Add(5, 5, 0, wxGROW
| wxALL
, 5); // spacer
225 sizerLeft
->Add(m_radioKind
, 0, wxGROW
| wxALL
, 5);
227 wxButton
*btn
= new wxButton(this, ComboPage_Reset
, _T("&Reset"));
228 sizerLeft
->Add(btn
, 0, wxALIGN_CENTRE_HORIZONTAL
| wxALL
, 15);
231 wxStaticBox
*box2
= new wxStaticBox(this, -1, _T("&Change combobox contents"));
232 wxSizer
*sizerMiddle
= new wxStaticBoxSizer(box2
, wxVERTICAL
);
237 sizerRow
= CreateSizerWithTextAndLabel(_T("Current selection"),
240 text
->SetEditable(FALSE
);
242 sizerMiddle
->Add(sizerRow
, 0, wxALL
| wxGROW
, 5);
244 sizerRow
= CreateSizerWithTextAndButton(ComboPage_Add
,
245 _T("&Add this string"),
248 sizerMiddle
->Add(sizerRow
, 0, wxALL
| wxGROW
, 5);
250 btn
= new wxButton(this, ComboPage_AddSeveral
, _T("&Insert a few strings"));
251 sizerMiddle
->Add(btn
, 0, wxALL
| wxGROW
, 5);
253 btn
= new wxButton(this, ComboPage_AddMany
, _T("Add &many strings"));
254 sizerMiddle
->Add(btn
, 0, wxALL
| wxGROW
, 5);
256 sizerRow
= CreateSizerWithTextAndButton(ComboPage_Change
,
257 _T("C&hange current"),
258 ComboPage_ChangeText
,
260 sizerMiddle
->Add(sizerRow
, 0, wxALL
| wxGROW
, 5);
262 sizerRow
= CreateSizerWithTextAndButton(ComboPage_Delete
,
263 _T("&Delete this item"),
264 ComboPage_DeleteText
,
266 sizerMiddle
->Add(sizerRow
, 0, wxALL
| wxGROW
, 5);
268 btn
= new wxButton(this, ComboPage_DeleteSel
, _T("Delete &selection"));
269 sizerMiddle
->Add(btn
, 0, wxALL
| wxGROW
, 5);
271 btn
= new wxButton(this, ComboPage_Clear
, _T("&Clear"));
272 sizerMiddle
->Add(btn
, 0, wxALL
| wxGROW
, 5);
275 wxSizer
*sizerRight
= new wxBoxSizer(wxVERTICAL
);
276 m_combobox
= new wxComboBox(this, ComboPage_Combo
, _T(""),
277 wxDefaultPosition
, wxDefaultSize
,
280 sizerRight
->Add(m_combobox
, 1, wxGROW
| wxALL
, 5);
281 sizerRight
->SetMinSize(150, 0);
282 m_sizerCombo
= sizerRight
; // save it to modify it later
284 // the 3 panes panes compose the window
285 sizerTop
->Add(sizerLeft
, 0, wxGROW
| (wxALL
& ~wxLEFT
), 10);
286 sizerTop
->Add(sizerMiddle
, 1, wxGROW
| wxALL
, 10);
287 sizerTop
->Add(sizerRight
, 1, wxGROW
| (wxALL
& ~wxRIGHT
), 10);
289 // final initializations
298 // ----------------------------------------------------------------------------
300 // ----------------------------------------------------------------------------
302 void ComboboxWidgetsPage::Reset()
304 m_chkSort
->SetValue(FALSE
);
305 m_chkReadonly
->SetValue(FALSE
);
308 void ComboboxWidgetsPage::CreateCombo()
312 if ( m_chkSort
->GetValue() )
314 if ( m_chkReadonly
->GetValue() )
315 flags
|= wxCB_READONLY
;
317 switch ( m_radioKind
->GetSelection() )
320 wxFAIL_MSG( _T("unknown combo kind") );
323 case ComboKind_Default
:
326 case ComboKind_Simple
:
327 flags
|= wxCB_SIMPLE
;
330 case ComboKind_DropDown
:
331 flags
= wxCB_DROPDOWN
;
338 int count
= m_combobox
->GetCount();
339 for ( int n
= 0; n
< count
; n
++ )
341 items
.Add(m_combobox
->GetString(n
));
344 m_sizerCombo
->Detach( m_combobox
);
348 m_combobox
= new wxComboBox(this, ComboPage_Combo
, _T(""),
349 wxDefaultPosition
, wxDefaultSize
,
353 size_t count
= items
.GetCount();
354 for ( size_t n
= 0; n
< count
; n
++ )
356 m_combobox
->Append(items
[n
]);
359 m_sizerCombo
->Add(m_combobox
, 1, wxGROW
| wxALL
, 5);
360 m_sizerCombo
->Layout();
363 // ----------------------------------------------------------------------------
365 // ----------------------------------------------------------------------------
367 void ComboboxWidgetsPage::OnButtonReset(wxCommandEvent
& WXUNUSED(event
))
374 void ComboboxWidgetsPage::OnButtonChange(wxCommandEvent
& WXUNUSED(event
))
376 int sel
= m_combobox
->GetSelection();
380 m_combobox
->SetString(sel
, m_textChange
->GetValue());
382 wxLogMessage(_T("Not implemented in wxGTK"));
387 void ComboboxWidgetsPage::OnButtonDelete(wxCommandEvent
& WXUNUSED(event
))
390 if ( !m_textDelete
->GetValue().ToULong(&n
) ||
391 (n
>= (unsigned)m_combobox
->GetCount()) )
396 m_combobox
->Delete(n
);
399 void ComboboxWidgetsPage::OnButtonDeleteSel(wxCommandEvent
& WXUNUSED(event
))
401 int sel
= m_combobox
->GetSelection();
404 m_combobox
->Delete(sel
);
408 void ComboboxWidgetsPage::OnButtonClear(wxCommandEvent
& event
)
413 void ComboboxWidgetsPage::OnButtonAdd(wxCommandEvent
& event
)
415 static unsigned int s_item
= 0;
417 wxString s
= m_textAdd
->GetValue();
418 if ( !m_textAdd
->IsModified() )
420 // update the default string
421 m_textAdd
->SetValue(wxString::Format(_T("test item %u"), ++s_item
));
424 m_combobox
->Append(s
);
427 void ComboboxWidgetsPage::OnButtonAddMany(wxCommandEvent
& WXUNUSED(event
))
429 // "many" means 1000 here
430 for ( unsigned int n
= 0; n
< 1000; n
++ )
432 m_combobox
->Append(wxString::Format(_T("item #%u"), n
));
436 void ComboboxWidgetsPage::OnButtonAddSeveral(wxCommandEvent
& event
)
438 m_combobox
->Append(_T("First"));
439 m_combobox
->Append(_T("another one"));
440 m_combobox
->Append(_T("and the last (very very very very very very very very very very long) one"));
443 void ComboboxWidgetsPage::OnUpdateUICurText(wxUpdateUIEvent
& event
)
446 event
.SetText( wxString::Format(_T("%d"), m_combobox
->GetSelection()) );
449 void ComboboxWidgetsPage::OnUpdateUIResetButton(wxUpdateUIEvent
& event
)
452 event
.Enable( m_chkSort
->GetValue() || m_chkReadonly
->GetValue() );
455 void ComboboxWidgetsPage::OnUpdateUIDeleteButton(wxUpdateUIEvent
& event
)
460 event
.Enable(m_textDelete
->GetValue().ToULong(&n
) &&
461 (n
< (unsigned)m_combobox
->GetCount()));
465 void ComboboxWidgetsPage::OnUpdateUIDeleteSelButton(wxUpdateUIEvent
& event
)
468 event
.Enable(m_combobox
->GetSelection() != -1);
471 void ComboboxWidgetsPage::OnUpdateUIClearButton(wxUpdateUIEvent
& event
)
474 event
.Enable(m_combobox
->GetCount() != 0);
477 void ComboboxWidgetsPage::OnUpdateUIAddSeveral(wxUpdateUIEvent
& event
)
480 event
.Enable(!(m_combobox
->GetWindowStyle() & wxCB_SORT
));
483 void ComboboxWidgetsPage::OnComboText(wxCommandEvent
& event
)
488 wxString s
= event
.GetString();
490 wxASSERT_MSG( s
== m_combobox
->GetValue(),
491 _T("event and combobox values should be the same") );
493 wxLogMessage(_T("Combobox text changed (now '%s')"), s
.c_str());
496 void ComboboxWidgetsPage::OnComboBox(wxCommandEvent
& event
)
498 long sel
= event
.GetInt();
499 m_textDelete
->SetValue(wxString::Format(_T("%ld"), sel
));
501 wxLogMessage(_T("Combobox item %ld selected"), sel
);
504 void ComboboxWidgetsPage::OnCheckOrRadioBox(wxCommandEvent
& event
)
509 #endif //wxUSE_COMBOBOX