1 /////////////////////////////////////////////////////////////////////////////
2 // Program: wxWidgets 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,
72 // kinds of comboboxes
80 // ----------------------------------------------------------------------------
81 // ComboboxWidgetsPage
82 // ----------------------------------------------------------------------------
84 class ComboboxWidgetsPage
: public WidgetsPage
87 ComboboxWidgetsPage(wxBookCtrl
*book
, wxImageList
*imaglist
);
89 virtual wxControl
*GetWidget() const { return m_combobox
; }
93 void OnButtonReset(wxCommandEvent
& event
);
94 void OnButtonChange(wxCommandEvent
& event
);
95 void OnButtonDelete(wxCommandEvent
& event
);
96 void OnButtonDeleteSel(wxCommandEvent
& event
);
97 void OnButtonClear(wxCommandEvent
& event
);
98 void OnButtonInsert(wxCommandEvent
&event
);
99 void OnButtonAdd(wxCommandEvent
& event
);
100 void OnButtonAddSeveral(wxCommandEvent
& event
);
101 void OnButtonAddMany(wxCommandEvent
& event
);
103 void OnComboBox(wxCommandEvent
& event
);
104 void OnComboText(wxCommandEvent
& event
);
106 void OnCheckOrRadioBox(wxCommandEvent
& event
);
108 void OnUpdateUICurText(wxUpdateUIEvent
& event
);
110 void OnUpdateUIInsert(wxUpdateUIEvent
& event
);
111 void OnUpdateUIAddSeveral(wxUpdateUIEvent
& event
);
112 void OnUpdateUIClearButton(wxUpdateUIEvent
& event
);
113 void OnUpdateUIDeleteButton(wxUpdateUIEvent
& event
);
114 void OnUpdateUIDeleteSelButton(wxUpdateUIEvent
& event
);
115 void OnUpdateUIResetButton(wxUpdateUIEvent
& event
);
117 // reset the combobox parameters
120 // (re)create the combobox
126 // the sel mode radiobox
127 wxRadioBox
*m_radioKind
;
129 // the checkboxes for styles
130 wxCheckBox
*m_chkSort
,
133 // the combobox itself and the sizer it is in
134 wxComboBox
*m_combobox
;
135 wxSizer
*m_sizerCombo
;
137 // the text entries for "Add/change string" and "Delete" buttons
138 wxTextCtrl
*m_textInsert
,
144 DECLARE_EVENT_TABLE()
145 DECLARE_WIDGETS_PAGE(ComboboxWidgetsPage
)
148 // ----------------------------------------------------------------------------
150 // ----------------------------------------------------------------------------
152 BEGIN_EVENT_TABLE(ComboboxWidgetsPage
, WidgetsPage
)
153 EVT_BUTTON(ComboPage_Reset
, ComboboxWidgetsPage::OnButtonReset
)
154 EVT_BUTTON(ComboPage_Change
, ComboboxWidgetsPage::OnButtonChange
)
155 EVT_BUTTON(ComboPage_Delete
, ComboboxWidgetsPage::OnButtonDelete
)
156 EVT_BUTTON(ComboPage_DeleteSel
, ComboboxWidgetsPage::OnButtonDeleteSel
)
157 EVT_BUTTON(ComboPage_Clear
, ComboboxWidgetsPage::OnButtonClear
)
158 EVT_BUTTON(ComboPage_Insert
, ComboboxWidgetsPage::OnButtonInsert
)
159 EVT_BUTTON(ComboPage_Add
, ComboboxWidgetsPage::OnButtonAdd
)
160 EVT_BUTTON(ComboPage_AddSeveral
, ComboboxWidgetsPage::OnButtonAddSeveral
)
161 EVT_BUTTON(ComboPage_AddMany
, ComboboxWidgetsPage::OnButtonAddMany
)
163 EVT_TEXT_ENTER(ComboPage_InsertText
, ComboboxWidgetsPage::OnButtonInsert
)
164 EVT_TEXT_ENTER(ComboPage_AddText
, ComboboxWidgetsPage::OnButtonAdd
)
165 EVT_TEXT_ENTER(ComboPage_DeleteText
, ComboboxWidgetsPage::OnButtonDelete
)
167 EVT_UPDATE_UI(ComboPage_CurText
, ComboboxWidgetsPage::OnUpdateUICurText
)
169 EVT_UPDATE_UI(ComboPage_Reset
, ComboboxWidgetsPage::OnUpdateUIResetButton
)
170 EVT_UPDATE_UI(ComboPage_Insert
, ComboboxWidgetsPage::OnUpdateUIInsert
)
171 EVT_UPDATE_UI(ComboPage_AddSeveral
, ComboboxWidgetsPage::OnUpdateUIAddSeveral
)
172 EVT_UPDATE_UI(ComboPage_Clear
, ComboboxWidgetsPage::OnUpdateUIClearButton
)
173 EVT_UPDATE_UI(ComboPage_DeleteText
, ComboboxWidgetsPage::OnUpdateUIClearButton
)
174 EVT_UPDATE_UI(ComboPage_Delete
, ComboboxWidgetsPage::OnUpdateUIDeleteButton
)
175 EVT_UPDATE_UI(ComboPage_Change
, ComboboxWidgetsPage::OnUpdateUIDeleteSelButton
)
176 EVT_UPDATE_UI(ComboPage_ChangeText
, ComboboxWidgetsPage::OnUpdateUIDeleteSelButton
)
177 EVT_UPDATE_UI(ComboPage_DeleteSel
, ComboboxWidgetsPage::OnUpdateUIDeleteSelButton
)
179 EVT_COMBOBOX(ComboPage_Combo
, ComboboxWidgetsPage::OnComboBox
)
180 EVT_TEXT(ComboPage_Combo
, ComboboxWidgetsPage::OnComboText
)
181 EVT_TEXT_ENTER(ComboPage_Combo
, ComboboxWidgetsPage::OnComboText
)
183 EVT_CHECKBOX(wxID_ANY
, ComboboxWidgetsPage::OnCheckOrRadioBox
)
184 EVT_RADIOBOX(wxID_ANY
, ComboboxWidgetsPage::OnCheckOrRadioBox
)
187 // ============================================================================
189 // ============================================================================
191 IMPLEMENT_WIDGETS_PAGE(ComboboxWidgetsPage
, _T("Combobox"));
193 ComboboxWidgetsPage::ComboboxWidgetsPage(wxBookCtrl
*book
,
194 wxImageList
*imaglist
)
199 m_chkReadonly
= (wxCheckBox
*)NULL
;
201 m_combobox
= (wxComboBox
*)NULL
;
202 m_sizerCombo
= (wxSizer
*)NULL
;
204 imaglist
->Add(wxBitmap(combobox_xpm
));
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 combobox operations and the pane containing the combobox
212 wxSizer
*sizerTop
= new wxBoxSizer(wxHORIZONTAL
);
215 wxStaticBox
*box
= new wxStaticBox(this, wxID_ANY
, _T("&Set style"));
217 // should be in sync with ComboKind_XXX values
218 static const wxString kinds
[] =
225 m_radioKind
= new wxRadioBox(this, wxID_ANY
, _T("Combobox &kind:"),
226 wxDefaultPosition
, wxDefaultSize
,
227 WXSIZEOF(kinds
), kinds
,
228 1, wxRA_SPECIFY_COLS
);
230 wxSizer
*sizerLeft
= new wxStaticBoxSizer(box
, wxVERTICAL
);
232 m_chkSort
= CreateCheckBoxAndAddToSizer(sizerLeft
, _T("&Sort items"));
233 m_chkReadonly
= CreateCheckBoxAndAddToSizer(sizerLeft
, _T("&Read only"));
235 sizerLeft
->Add(5, 5, 0, wxGROW
| wxALL
, 5); // spacer
236 sizerLeft
->Add(m_radioKind
, 0, wxGROW
| wxALL
, 5);
238 wxButton
*btn
= new wxButton(this, ComboPage_Reset
, _T("&Reset"));
239 sizerLeft
->Add(btn
, 0, wxALIGN_CENTRE_HORIZONTAL
| wxALL
, 15);
242 wxStaticBox
*box2
= new wxStaticBox(this, wxID_ANY
,
243 _T("&Change combobox contents"));
244 wxSizer
*sizerMiddle
= new wxStaticBoxSizer(box2
, wxVERTICAL
);
249 sizerRow
= CreateSizerWithTextAndLabel(_T("Current selection"),
252 text
->SetEditable(false);
254 sizerMiddle
->Add(sizerRow
, 0, wxALL
| wxGROW
, 5);
256 sizerRow
= CreateSizerWithTextAndButton(ComboPage_Insert
,
257 _T("&Insert this string"),
258 ComboPage_InsertText
,
260 sizerMiddle
->Add(sizerRow
, 0, wxALL
| wxGROW
, 5);
262 sizerRow
= CreateSizerWithTextAndButton(ComboPage_Add
,
263 _T("&Add this string"),
266 sizerMiddle
->Add(sizerRow
, 0, wxALL
| wxGROW
, 5);
268 btn
= new wxButton(this, ComboPage_AddSeveral
, _T("&Append a few strings"));
269 sizerMiddle
->Add(btn
, 0, wxALL
| wxGROW
, 5);
271 btn
= new wxButton(this, ComboPage_AddMany
, _T("Append &many strings"));
272 sizerMiddle
->Add(btn
, 0, wxALL
| wxGROW
, 5);
274 sizerRow
= CreateSizerWithTextAndButton(ComboPage_Change
,
275 _T("C&hange current"),
276 ComboPage_ChangeText
,
278 sizerMiddle
->Add(sizerRow
, 0, wxALL
| wxGROW
, 5);
280 sizerRow
= CreateSizerWithTextAndButton(ComboPage_Delete
,
281 _T("&Delete this item"),
282 ComboPage_DeleteText
,
284 sizerMiddle
->Add(sizerRow
, 0, wxALL
| wxGROW
, 5);
286 btn
= new wxButton(this, ComboPage_DeleteSel
, _T("Delete &selection"));
287 sizerMiddle
->Add(btn
, 0, wxALL
| wxGROW
, 5);
289 btn
= new wxButton(this, ComboPage_Clear
, _T("&Clear"));
290 sizerMiddle
->Add(btn
, 0, wxALL
| wxGROW
, 5);
293 wxSizer
*sizerRight
= new wxBoxSizer(wxVERTICAL
);
294 m_combobox
= new wxComboBox(this, ComboPage_Combo
, wxEmptyString
,
295 wxDefaultPosition
, wxDefaultSize
,
298 sizerRight
->Add(m_combobox
, 1, wxGROW
| wxALL
, 5);
299 sizerRight
->SetMinSize(150, 0);
300 m_sizerCombo
= sizerRight
; // save it to modify it later
302 // the 3 panes panes compose the window
303 sizerTop
->Add(sizerLeft
, 0, wxGROW
| (wxALL
& ~wxLEFT
), 10);
304 sizerTop
->Add(sizerMiddle
, 1, wxGROW
| wxALL
, 10);
305 sizerTop
->Add(sizerRight
, 1, wxGROW
| (wxALL
& ~wxRIGHT
), 10);
307 // final initializations
315 // ----------------------------------------------------------------------------
317 // ----------------------------------------------------------------------------
319 void ComboboxWidgetsPage::Reset()
321 m_chkSort
->SetValue(false);
322 m_chkReadonly
->SetValue(false);
325 void ComboboxWidgetsPage::CreateCombo()
329 if ( m_chkSort
->GetValue() )
331 if ( m_chkReadonly
->GetValue() )
332 flags
|= wxCB_READONLY
;
334 switch ( m_radioKind
->GetSelection() )
337 wxFAIL_MSG( _T("unknown combo kind") );
340 case ComboKind_Default
:
343 case ComboKind_Simple
:
344 flags
|= wxCB_SIMPLE
;
347 case ComboKind_DropDown
:
348 flags
= wxCB_DROPDOWN
;
355 int count
= m_combobox
->GetCount();
356 for ( int n
= 0; n
< count
; n
++ )
358 items
.Add(m_combobox
->GetString(n
));
361 m_sizerCombo
->Detach( m_combobox
);
365 m_combobox
= new wxComboBox(this, ComboPage_Combo
, wxEmptyString
,
366 wxDefaultPosition
, wxDefaultSize
,
370 size_t count
= items
.GetCount();
371 for ( size_t n
= 0; n
< count
; n
++ )
373 m_combobox
->Append(items
[n
]);
376 m_sizerCombo
->Add(m_combobox
, 1, wxGROW
| wxALL
, 5);
377 m_sizerCombo
->Layout();
380 // ----------------------------------------------------------------------------
382 // ----------------------------------------------------------------------------
384 void ComboboxWidgetsPage::OnButtonReset(wxCommandEvent
& WXUNUSED(event
))
391 void ComboboxWidgetsPage::OnButtonChange(wxCommandEvent
& WXUNUSED(event
))
393 int sel
= m_combobox
->GetSelection();
397 m_combobox
->SetString(sel
, m_textChange
->GetValue());
399 wxLogMessage(_T("Not implemented in wxGTK"));
404 void ComboboxWidgetsPage::OnButtonDelete(wxCommandEvent
& WXUNUSED(event
))
407 if ( !m_textDelete
->GetValue().ToULong(&n
) ||
408 (n
>= (unsigned)m_combobox
->GetCount()) )
413 m_combobox
->Delete(n
);
416 void ComboboxWidgetsPage::OnButtonDeleteSel(wxCommandEvent
& WXUNUSED(event
))
418 int sel
= m_combobox
->GetSelection();
421 m_combobox
->Delete(sel
);
425 void ComboboxWidgetsPage::OnButtonClear(wxCommandEvent
& WXUNUSED(event
))
430 void ComboboxWidgetsPage::OnButtonInsert(wxCommandEvent
& WXUNUSED(event
))
432 static unsigned int s_item
= 0;
434 wxString s
= m_textInsert
->GetValue();
435 if ( !m_textInsert
->IsModified() )
437 // update the default string
438 m_textInsert
->SetValue(wxString::Format(_T("test item %u"), ++s_item
));
441 if (m_combobox
->GetSelection() >= 0)
442 m_combobox
->Insert(s
, m_combobox
->GetSelection());
445 void ComboboxWidgetsPage::OnButtonAdd(wxCommandEvent
& WXUNUSED(event
))
447 static unsigned int s_item
= 0;
449 wxString s
= m_textAdd
->GetValue();
450 if ( !m_textAdd
->IsModified() )
452 // update the default string
453 m_textAdd
->SetValue(wxString::Format(_T("test item %u"), ++s_item
));
456 m_combobox
->Append(s
);
459 void ComboboxWidgetsPage::OnButtonAddMany(wxCommandEvent
& WXUNUSED(event
))
461 // "many" means 1000 here
462 for ( unsigned int n
= 0; n
< 1000; n
++ )
464 m_combobox
->Append(wxString::Format(_T("item #%u"), n
));
468 void ComboboxWidgetsPage::OnButtonAddSeveral(wxCommandEvent
& WXUNUSED(event
))
470 m_combobox
->Append(_T("First"));
471 m_combobox
->Append(_T("another one"));
472 m_combobox
->Append(_T("and the last (very very very very very very very very very very long) one"));
475 void ComboboxWidgetsPage::OnUpdateUICurText(wxUpdateUIEvent
& event
)
478 event
.SetText( wxString::Format(_T("%d"), m_combobox
->GetSelection()) );
481 void ComboboxWidgetsPage::OnUpdateUIResetButton(wxUpdateUIEvent
& event
)
484 event
.Enable( m_chkSort
->GetValue() || m_chkReadonly
->GetValue() );
487 void ComboboxWidgetsPage::OnUpdateUIInsert(wxUpdateUIEvent
& event
)
491 bool enable
= !(m_combobox
->GetWindowStyle() & wxCB_SORT
) &&
492 (m_combobox
->GetSelection() >= 0);
494 event
.Enable(enable
);
498 void ComboboxWidgetsPage::OnUpdateUIDeleteButton(wxUpdateUIEvent
& event
)
503 event
.Enable(m_textDelete
->GetValue().ToULong(&n
) &&
504 (n
< (unsigned)m_combobox
->GetCount()));
508 void ComboboxWidgetsPage::OnUpdateUIDeleteSelButton(wxUpdateUIEvent
& event
)
511 event
.Enable(m_combobox
->GetSelection() != -1);
514 void ComboboxWidgetsPage::OnUpdateUIClearButton(wxUpdateUIEvent
& event
)
517 event
.Enable(m_combobox
->GetCount() != 0);
520 void ComboboxWidgetsPage::OnUpdateUIAddSeveral(wxUpdateUIEvent
& event
)
523 event
.Enable(!(m_combobox
->GetWindowStyle() & wxCB_SORT
));
526 void ComboboxWidgetsPage::OnComboText(wxCommandEvent
& event
)
531 wxString s
= event
.GetString();
533 wxASSERT_MSG( s
== m_combobox
->GetValue(),
534 _T("event and combobox values should be the same") );
536 if (event
.GetEventType() == wxEVT_COMMAND_TEXT_ENTER
)
537 wxLogMessage(_T("Combobox enter pressed (now '%s')"), s
.c_str());
539 wxLogMessage(_T("Combobox text changed (now '%s')"), s
.c_str());
542 void ComboboxWidgetsPage::OnComboBox(wxCommandEvent
& event
)
544 long sel
= event
.GetInt();
545 m_textDelete
->SetValue(wxString::Format(_T("%ld"), sel
));
547 wxLogMessage(_T("Combobox item %ld selected"), sel
);
549 wxLogMessage(_T("Combobox GetValue(): %s"), m_combobox
->GetValue().c_str() );
552 void ComboboxWidgetsPage::OnCheckOrRadioBox(wxCommandEvent
& WXUNUSED(event
))
557 #endif //wxUSE_COMBOBOX