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"
44 #include "itemcontainer.h"
47 #include "icons/combobox.xpm"
49 // ----------------------------------------------------------------------------
51 // ----------------------------------------------------------------------------
56 ComboPage_Reset
= wxID_HIGHEST
,
59 ComboPage_InsertionPointText
,
73 ComboPage_SetValueText
,
75 ComboPage_ContainerTests
78 // kinds of comboboxes
86 // ----------------------------------------------------------------------------
87 // ComboboxWidgetsPage
88 // ----------------------------------------------------------------------------
90 class ComboboxWidgetsPage
: public ItemContainerWidgetsPage
93 ComboboxWidgetsPage(WidgetsBookCtrl
*book
, wxImageList
*imaglist
);
95 virtual wxControl
*GetWidget() const { return m_combobox
; }
96 virtual wxTextEntryBase
*GetTextEntry() const { return m_combobox
; }
97 virtual wxItemContainer
* GetContainer() const { return m_combobox
; }
98 virtual void RecreateWidget() { CreateCombo(); }
100 // lazy creation of the content
101 virtual void CreateContent();
105 void OnButtonReset(wxCommandEvent
& event
);
106 void OnButtonChange(wxCommandEvent
& event
);
107 void OnButtonDelete(wxCommandEvent
& event
);
108 void OnButtonDeleteSel(wxCommandEvent
& event
);
109 void OnButtonClear(wxCommandEvent
& event
);
110 void OnButtonInsert(wxCommandEvent
&event
);
111 void OnButtonAdd(wxCommandEvent
& event
);
112 void OnButtonAddSeveral(wxCommandEvent
& event
);
113 void OnButtonAddMany(wxCommandEvent
& event
);
114 void OnButtonSetValue(wxCommandEvent
& event
);
115 void OnButtonSetCurrent(wxCommandEvent
& event
);
117 void OnComboBox(wxCommandEvent
& event
);
118 void OnComboText(wxCommandEvent
& event
);
120 void OnCheckOrRadioBox(wxCommandEvent
& event
);
122 void OnUpdateUIInsertionPointText(wxUpdateUIEvent
& event
);
124 void OnUpdateUIInsert(wxUpdateUIEvent
& event
);
125 void OnUpdateUIAddSeveral(wxUpdateUIEvent
& event
);
126 void OnUpdateUIClearButton(wxUpdateUIEvent
& event
);
127 void OnUpdateUIDeleteButton(wxUpdateUIEvent
& event
);
128 void OnUpdateUIDeleteSelButton(wxUpdateUIEvent
& event
);
129 void OnUpdateUIResetButton(wxUpdateUIEvent
& event
);
130 void OnUpdateUISetCurrent(wxUpdateUIEvent
& event
);
132 // reset the combobox parameters
135 // (re)create the combobox
141 // the sel mode radiobox
142 wxRadioBox
*m_radioKind
;
144 // the checkboxes for styles
145 wxCheckBox
*m_chkSort
,
149 // the combobox itself and the sizer it is in
150 wxComboBox
*m_combobox
;
151 wxSizer
*m_sizerCombo
;
153 // the text entries for "Add/change string" and "Delete" buttons
154 wxTextCtrl
*m_textInsert
,
162 DECLARE_EVENT_TABLE()
163 DECLARE_WIDGETS_PAGE(ComboboxWidgetsPage
)
166 // ----------------------------------------------------------------------------
168 // ----------------------------------------------------------------------------
170 BEGIN_EVENT_TABLE(ComboboxWidgetsPage
, WidgetsPage
)
171 EVT_BUTTON(ComboPage_Reset
, ComboboxWidgetsPage::OnButtonReset
)
172 EVT_BUTTON(ComboPage_Change
, ComboboxWidgetsPage::OnButtonChange
)
173 EVT_BUTTON(ComboPage_Delete
, ComboboxWidgetsPage::OnButtonDelete
)
174 EVT_BUTTON(ComboPage_DeleteSel
, ComboboxWidgetsPage::OnButtonDeleteSel
)
175 EVT_BUTTON(ComboPage_Clear
, ComboboxWidgetsPage::OnButtonClear
)
176 EVT_BUTTON(ComboPage_Insert
, ComboboxWidgetsPage::OnButtonInsert
)
177 EVT_BUTTON(ComboPage_Add
, ComboboxWidgetsPage::OnButtonAdd
)
178 EVT_BUTTON(ComboPage_AddSeveral
, ComboboxWidgetsPage::OnButtonAddSeveral
)
179 EVT_BUTTON(ComboPage_AddMany
, ComboboxWidgetsPage::OnButtonAddMany
)
180 EVT_BUTTON(ComboPage_SetValue
, ComboboxWidgetsPage::OnButtonSetValue
)
181 EVT_BUTTON(ComboPage_SetCurrent
, ComboboxWidgetsPage::OnButtonSetCurrent
)
182 EVT_BUTTON(ComboPage_ContainerTests
, ItemContainerWidgetsPage::OnButtonTestItemContainer
)
184 EVT_TEXT_ENTER(ComboPage_InsertText
, ComboboxWidgetsPage::OnButtonInsert
)
185 EVT_TEXT_ENTER(ComboPage_AddText
, ComboboxWidgetsPage::OnButtonAdd
)
186 EVT_TEXT_ENTER(ComboPage_DeleteText
, ComboboxWidgetsPage::OnButtonDelete
)
188 EVT_UPDATE_UI(ComboPage_InsertionPointText
, ComboboxWidgetsPage::OnUpdateUIInsertionPointText
)
190 EVT_UPDATE_UI(ComboPage_Reset
, ComboboxWidgetsPage::OnUpdateUIResetButton
)
191 EVT_UPDATE_UI(ComboPage_Insert
, ComboboxWidgetsPage::OnUpdateUIInsert
)
192 EVT_UPDATE_UI(ComboPage_AddSeveral
, ComboboxWidgetsPage::OnUpdateUIAddSeveral
)
193 EVT_UPDATE_UI(ComboPage_Clear
, ComboboxWidgetsPage::OnUpdateUIClearButton
)
194 EVT_UPDATE_UI(ComboPage_DeleteText
, ComboboxWidgetsPage::OnUpdateUIClearButton
)
195 EVT_UPDATE_UI(ComboPage_Delete
, ComboboxWidgetsPage::OnUpdateUIDeleteButton
)
196 EVT_UPDATE_UI(ComboPage_Change
, ComboboxWidgetsPage::OnUpdateUIDeleteSelButton
)
197 EVT_UPDATE_UI(ComboPage_ChangeText
, ComboboxWidgetsPage::OnUpdateUIDeleteSelButton
)
198 EVT_UPDATE_UI(ComboPage_DeleteSel
, ComboboxWidgetsPage::OnUpdateUIDeleteSelButton
)
199 EVT_UPDATE_UI(ComboPage_SetCurrent
, ComboboxWidgetsPage::OnUpdateUISetCurrent
)
201 EVT_COMBOBOX(ComboPage_Combo
, ComboboxWidgetsPage::OnComboBox
)
202 EVT_TEXT(ComboPage_Combo
, ComboboxWidgetsPage::OnComboText
)
203 EVT_TEXT_ENTER(ComboPage_Combo
, ComboboxWidgetsPage::OnComboText
)
205 EVT_CHECKBOX(wxID_ANY
, ComboboxWidgetsPage::OnCheckOrRadioBox
)
206 EVT_RADIOBOX(wxID_ANY
, ComboboxWidgetsPage::OnCheckOrRadioBox
)
209 // ============================================================================
211 // ============================================================================
213 #if defined(__WXUNIVERSAL__)
214 #define FAMILY_CTRLS UNIVERSAL_CTRLS
216 #define FAMILY_CTRLS NATIVE_CTRLS
219 IMPLEMENT_WIDGETS_PAGE(ComboboxWidgetsPage
, _T("Combobox"),
220 FAMILY_CTRLS
| WITH_ITEMS_CTRLS
| COMBO_CTRLS
223 ComboboxWidgetsPage::ComboboxWidgetsPage(WidgetsBookCtrl
*book
,
224 wxImageList
*imaglist
)
225 : ItemContainerWidgetsPage(book
, imaglist
, combobox_xpm
)
230 m_chkFilename
= (wxCheckBox
*)NULL
;
232 m_combobox
= (wxComboBox
*)NULL
;
233 m_sizerCombo
= (wxSizer
*)NULL
;
236 void ComboboxWidgetsPage::CreateContent()
239 What we create here is a frame having 3 panes: style pane is the
240 leftmost one, in the middle the pane with buttons allowing to perform
241 miscellaneous combobox operations and the pane containing the combobox
244 wxSizer
*sizerTop
= new wxBoxSizer(wxHORIZONTAL
);
247 wxStaticBox
*box
= new wxStaticBox(this, wxID_ANY
, _T("&Set style"));
249 // should be in sync with ComboKind_XXX values
250 static const wxString kinds
[] =
257 m_radioKind
= new wxRadioBox(this, wxID_ANY
, _T("Combobox &kind:"),
258 wxDefaultPosition
, wxDefaultSize
,
259 WXSIZEOF(kinds
), kinds
,
260 1, wxRA_SPECIFY_COLS
);
262 wxSizer
*sizerLeft
= new wxStaticBoxSizer(box
, wxVERTICAL
);
264 m_chkSort
= CreateCheckBoxAndAddToSizer(sizerLeft
, _T("&Sort items"));
265 m_chkReadonly
= CreateCheckBoxAndAddToSizer(sizerLeft
, _T("&Read only"));
266 m_chkFilename
= CreateCheckBoxAndAddToSizer(sizerLeft
, _T("&File name"));
267 m_chkFilename
->Disable(); // not implemented yet
269 sizerLeft
->Add(5, 5, 0, wxGROW
| wxALL
, 5); // spacer
270 sizerLeft
->Add(m_radioKind
, 0, wxGROW
| wxALL
, 5);
272 wxButton
*btn
= new wxButton(this, ComboPage_Reset
, _T("&Reset"));
273 sizerLeft
->Add(btn
, 0, wxALIGN_CENTRE_HORIZONTAL
| wxALL
, 15);
276 wxStaticBox
*box2
= new wxStaticBox(this, wxID_ANY
,
277 _T("&Change combobox contents"));
278 wxSizer
*sizerMiddle
= new wxStaticBoxSizer(box2
, wxVERTICAL
);
282 sizerRow
= CreateSizerWithTextAndButton(ComboPage_SetCurrent
,
283 _T("Current &selection"),
287 sizerMiddle
->Add(sizerRow
, 0, wxALL
| wxGROW
, 5);
290 sizerRow
= CreateSizerWithTextAndLabel(_T("Insertion Point"),
291 ComboPage_InsertionPointText
,
293 text
->SetEditable(false);
295 sizerMiddle
->Add(sizerRow
, 0, wxALL
| wxGROW
, 5);
297 sizerRow
= CreateSizerWithTextAndButton(ComboPage_Insert
,
298 _T("&Insert this string"),
299 ComboPage_InsertText
,
301 sizerMiddle
->Add(sizerRow
, 0, wxALL
| wxGROW
, 5);
303 sizerRow
= CreateSizerWithTextAndButton(ComboPage_Add
,
304 _T("&Add this string"),
307 sizerMiddle
->Add(sizerRow
, 0, wxALL
| wxGROW
, 5);
309 btn
= new wxButton(this, ComboPage_AddSeveral
, _T("&Append a few strings"));
310 sizerMiddle
->Add(btn
, 0, wxALL
| wxGROW
, 5);
312 btn
= new wxButton(this, ComboPage_AddMany
, _T("Append &many strings"));
313 sizerMiddle
->Add(btn
, 0, wxALL
| wxGROW
, 5);
315 sizerRow
= CreateSizerWithTextAndButton(ComboPage_Change
,
316 _T("C&hange current"),
317 ComboPage_ChangeText
,
319 sizerMiddle
->Add(sizerRow
, 0, wxALL
| wxGROW
, 5);
321 sizerRow
= CreateSizerWithTextAndButton(ComboPage_Delete
,
322 _T("&Delete this item"),
323 ComboPage_DeleteText
,
325 sizerMiddle
->Add(sizerRow
, 0, wxALL
| wxGROW
, 5);
327 btn
= new wxButton(this, ComboPage_DeleteSel
, _T("Delete &selection"));
328 sizerMiddle
->Add(btn
, 0, wxALL
| wxGROW
, 5);
330 btn
= new wxButton(this, ComboPage_Clear
, _T("&Clear"));
331 sizerMiddle
->Add(btn
, 0, wxALL
| wxGROW
, 5);
333 sizerRow
= CreateSizerWithTextAndButton(ComboPage_SetValue
,
335 ComboPage_SetValueText
,
337 sizerMiddle
->Add(sizerRow
, 0, wxALL
| wxGROW
, 5);
339 btn
= new wxButton(this, ComboPage_ContainerTests
, _T("Run &tests"));
340 sizerMiddle
->Add(btn
, 0, wxALL
| wxGROW
, 5);
345 wxSizer
*sizerRight
= new wxBoxSizer(wxVERTICAL
);
346 m_combobox
= new wxComboBox(this, ComboPage_Combo
, wxEmptyString
,
347 wxDefaultPosition
, wxDefaultSize
,
350 sizerRight
->Add(m_combobox
, 0, wxGROW
| wxALL
, 5);
351 sizerRight
->SetMinSize(150, 0);
352 m_sizerCombo
= sizerRight
; // save it to modify it later
354 // the 3 panes panes compose the window
355 sizerTop
->Add(sizerLeft
, 0, wxGROW
| (wxALL
& ~wxLEFT
), 10);
356 sizerTop
->Add(sizerMiddle
, 1, wxGROW
| wxALL
, 10);
357 sizerTop
->Add(sizerRight
, 1, wxGROW
| (wxALL
& ~wxRIGHT
), 10);
359 // final initializations
365 // ----------------------------------------------------------------------------
367 // ----------------------------------------------------------------------------
369 void ComboboxWidgetsPage::Reset()
371 m_chkSort
->SetValue(false);
372 m_chkReadonly
->SetValue(false);
373 m_chkFilename
->SetValue(false);
376 void ComboboxWidgetsPage::CreateCombo()
378 int flags
= ms_defaultFlags
;
380 if ( m_chkSort
->GetValue() )
382 if ( m_chkReadonly
->GetValue() )
383 flags
|= wxCB_READONLY
;
385 switch ( m_radioKind
->GetSelection() )
388 wxFAIL_MSG( _T("unknown combo kind") );
391 case ComboKind_Default
:
394 case ComboKind_Simple
:
395 flags
|= wxCB_SIMPLE
;
398 case ComboKind_DropDown
:
399 flags
= wxCB_DROPDOWN
;
406 unsigned int count
= m_combobox
->GetCount();
407 for ( unsigned int n
= 0; n
< count
; n
++ )
409 items
.Add(m_combobox
->GetString(n
));
412 m_sizerCombo
->Detach( m_combobox
);
416 m_combobox
= new wxComboBox(this, ComboPage_Combo
, wxEmptyString
,
417 wxDefaultPosition
, wxDefaultSize
,
422 if ( m_chkFilename
->GetValue() )
426 unsigned int count
= items
.GetCount();
427 for ( unsigned int n
= 0; n
< count
; n
++ )
429 m_combobox
->Append(items
[n
]);
432 m_sizerCombo
->Add(m_combobox
, 0, wxGROW
| wxALL
, 5);
433 m_sizerCombo
->Layout();
436 // ----------------------------------------------------------------------------
438 // ----------------------------------------------------------------------------
440 void ComboboxWidgetsPage::OnButtonReset(wxCommandEvent
& WXUNUSED(event
))
447 void ComboboxWidgetsPage::OnButtonChange(wxCommandEvent
& WXUNUSED(event
))
449 int sel
= m_combobox
->GetSelection();
450 if ( sel
!= wxNOT_FOUND
)
453 m_combobox
->SetString(sel
, m_textChange
->GetValue());
455 wxLogMessage(_T("Not implemented in wxGTK"));
460 void ComboboxWidgetsPage::OnButtonDelete(wxCommandEvent
& WXUNUSED(event
))
463 if ( !m_textDelete
->GetValue().ToULong(&n
) ||
464 (n
>= m_combobox
->GetCount()) )
469 m_combobox
->Delete(n
);
472 void ComboboxWidgetsPage::OnButtonDeleteSel(wxCommandEvent
& WXUNUSED(event
))
474 int sel
= m_combobox
->GetSelection();
475 if ( sel
!= wxNOT_FOUND
)
477 m_combobox
->Delete(sel
);
481 void ComboboxWidgetsPage::OnButtonSetValue(wxCommandEvent
& WXUNUSED(event
))
483 wxString value
= m_textSetValue
->GetValue();
484 m_combobox
->SetValue( value
);
487 void ComboboxWidgetsPage::OnButtonClear(wxCommandEvent
& WXUNUSED(event
))
492 void ComboboxWidgetsPage::OnButtonInsert(wxCommandEvent
& WXUNUSED(event
))
494 static unsigned int s_item
= 0;
496 wxString s
= m_textInsert
->GetValue();
497 if ( !m_textInsert
->IsModified() )
499 // update the default string
500 m_textInsert
->SetValue(wxString::Format(_T("test item %u"), ++s_item
));
503 if (m_combobox
->GetSelection() >= 0)
504 m_combobox
->Insert(s
, m_combobox
->GetSelection());
507 void ComboboxWidgetsPage::OnButtonAdd(wxCommandEvent
& WXUNUSED(event
))
509 static unsigned int s_item
= 0;
511 wxString s
= m_textAdd
->GetValue();
512 if ( !m_textAdd
->IsModified() )
514 // update the default string
515 m_textAdd
->SetValue(wxString::Format(_T("test item %u"), ++s_item
));
518 m_combobox
->Append(s
);
521 void ComboboxWidgetsPage::OnButtonAddMany(wxCommandEvent
& WXUNUSED(event
))
523 // "many" means 1000 here
524 for ( unsigned int n
= 0; n
< 1000; n
++ )
526 m_combobox
->Append(wxString::Format(_T("item #%u"), n
));
530 void ComboboxWidgetsPage::OnButtonSetCurrent(wxCommandEvent
& WXUNUSED(event
))
533 if ( !m_textCur
->GetValue().ToLong(&n
) )
536 m_combobox
->SetSelection(n
);
539 void ComboboxWidgetsPage::OnButtonAddSeveral(wxCommandEvent
& WXUNUSED(event
))
541 m_combobox
->Append(_T("First"));
542 m_combobox
->Append(_T("another one"));
543 m_combobox
->Append(_T("and the last (very very very very very very very very very very long) one"));
546 void ComboboxWidgetsPage::OnUpdateUIInsertionPointText(wxUpdateUIEvent
& event
)
549 event
.SetText( wxString::Format(_T("%ld"), m_combobox
->GetInsertionPoint()) );
552 void ComboboxWidgetsPage::OnUpdateUIResetButton(wxUpdateUIEvent
& event
)
554 event
.Enable( m_chkSort
->GetValue() ||
555 m_chkReadonly
->GetValue() ||
556 m_chkFilename
->GetValue() );
559 void ComboboxWidgetsPage::OnUpdateUIInsert(wxUpdateUIEvent
& event
)
563 bool enable
= !(m_combobox
->GetWindowStyle() & wxCB_SORT
) &&
564 (m_combobox
->GetSelection() >= 0);
566 event
.Enable(enable
);
570 void ComboboxWidgetsPage::OnUpdateUIDeleteButton(wxUpdateUIEvent
& event
)
575 event
.Enable(m_textDelete
->GetValue().ToULong(&n
) &&
576 (n
< (unsigned)m_combobox
->GetCount()));
580 void ComboboxWidgetsPage::OnUpdateUIDeleteSelButton(wxUpdateUIEvent
& event
)
583 event
.Enable(m_combobox
->GetSelection() != wxNOT_FOUND
);
586 void ComboboxWidgetsPage::OnUpdateUIClearButton(wxUpdateUIEvent
& event
)
589 event
.Enable(m_combobox
->GetCount() != 0);
592 void ComboboxWidgetsPage::OnUpdateUIAddSeveral(wxUpdateUIEvent
& event
)
595 event
.Enable(!(m_combobox
->GetWindowStyle() & wxCB_SORT
));
598 void ComboboxWidgetsPage::OnUpdateUISetCurrent(wxUpdateUIEvent
& event
)
601 event
.Enable( m_textCur
->GetValue().ToLong(&n
) &&
603 (n
>= 0 && (unsigned)n
< m_combobox
->GetCount())) );
606 void ComboboxWidgetsPage::OnComboText(wxCommandEvent
& event
)
611 wxString s
= event
.GetString();
613 wxASSERT_MSG( s
== m_combobox
->GetValue(),
614 _T("event and combobox values should be the same") );
616 if (event
.GetEventType() == wxEVT_COMMAND_TEXT_ENTER
)
617 wxLogMessage(_T("Combobox enter pressed (now '%s')"), s
.c_str());
619 wxLogMessage(_T("Combobox text changed (now '%s')"), s
.c_str());
622 void ComboboxWidgetsPage::OnComboBox(wxCommandEvent
& event
)
624 long sel
= event
.GetInt();
625 const wxString selstr
= wxString::Format(_T("%ld"), sel
);
626 m_textDelete
->SetValue(selstr
);
627 m_textCur
->SetValue(selstr
);
629 wxLogMessage(_T("Combobox item %ld selected"), sel
);
631 wxLogMessage(_T("Combobox GetValue(): %s"), m_combobox
->GetValue().c_str() );
634 void ComboboxWidgetsPage::OnCheckOrRadioBox(wxCommandEvent
& WXUNUSED(event
))
639 #endif //wxUSE_COMBOBOX