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"
46 #include "icons/combobox.xpm"
48 // ----------------------------------------------------------------------------
50 // ----------------------------------------------------------------------------
55 ComboPage_Reset
= wxID_HIGHEST
,
58 ComboPage_InsertionPointText
,
72 ComboPage_SetValueText
,
74 ComboPage_ContainerTests
77 // kinds of comboboxes
85 // ----------------------------------------------------------------------------
86 // ComboboxWidgetsPage
87 // ----------------------------------------------------------------------------
89 class ComboboxWidgetsPage
: public ItemContainerWidgetsPage
92 ComboboxWidgetsPage(WidgetsBookCtrl
*book
, wxImageList
*imaglist
);
94 virtual wxControl
*GetWidget() const { return m_combobox
; }
95 virtual wxTextEntryBase
*GetTextEntry() const { return m_combobox
; }
96 virtual wxItemContainer
* GetContainer() const { return m_combobox
; }
97 virtual void RecreateWidget() { CreateCombo(); }
99 // lazy creation of the content
100 virtual void CreateContent();
104 void OnButtonReset(wxCommandEvent
& event
);
105 void OnButtonChange(wxCommandEvent
& event
);
106 void OnButtonDelete(wxCommandEvent
& event
);
107 void OnButtonDeleteSel(wxCommandEvent
& event
);
108 void OnButtonClear(wxCommandEvent
& event
);
109 void OnButtonInsert(wxCommandEvent
&event
);
110 void OnButtonAdd(wxCommandEvent
& event
);
111 void OnButtonAddSeveral(wxCommandEvent
& event
);
112 void OnButtonAddMany(wxCommandEvent
& event
);
113 void OnButtonSetValue(wxCommandEvent
& event
);
114 void OnButtonSetCurrent(wxCommandEvent
& event
);
116 void OnDropdown(wxCommandEvent
& event
);
117 void OnCloseup(wxCommandEvent
& event
);
118 void OnComboBox(wxCommandEvent
& event
);
119 void OnComboText(wxCommandEvent
& event
);
121 void OnCheckOrRadioBox(wxCommandEvent
& event
);
123 void OnUpdateUIInsertionPointText(wxUpdateUIEvent
& event
);
125 void OnUpdateUIInsert(wxUpdateUIEvent
& event
);
126 void OnUpdateUIAddSeveral(wxUpdateUIEvent
& event
);
127 void OnUpdateUIClearButton(wxUpdateUIEvent
& event
);
128 void OnUpdateUIDeleteButton(wxUpdateUIEvent
& event
);
129 void OnUpdateUIDeleteSelButton(wxUpdateUIEvent
& event
);
130 void OnUpdateUIResetButton(wxUpdateUIEvent
& event
);
131 void OnUpdateUISetCurrent(wxUpdateUIEvent
& event
);
133 // reset the combobox parameters
136 // (re)create the combobox
142 // the sel mode radiobox
143 wxRadioBox
*m_radioKind
;
145 // the checkboxes for styles
146 wxCheckBox
*m_chkSort
,
150 // the combobox itself and the sizer it is in
151 wxComboBox
*m_combobox
;
152 wxSizer
*m_sizerCombo
;
154 // the text entries for "Add/change string" and "Delete" buttons
155 wxTextCtrl
*m_textInsert
,
163 DECLARE_EVENT_TABLE()
164 DECLARE_WIDGETS_PAGE(ComboboxWidgetsPage
)
167 // ----------------------------------------------------------------------------
169 // ----------------------------------------------------------------------------
171 BEGIN_EVENT_TABLE(ComboboxWidgetsPage
, WidgetsPage
)
172 EVT_BUTTON(ComboPage_Reset
, ComboboxWidgetsPage::OnButtonReset
)
173 EVT_BUTTON(ComboPage_Change
, ComboboxWidgetsPage::OnButtonChange
)
174 EVT_BUTTON(ComboPage_Delete
, ComboboxWidgetsPage::OnButtonDelete
)
175 EVT_BUTTON(ComboPage_DeleteSel
, ComboboxWidgetsPage::OnButtonDeleteSel
)
176 EVT_BUTTON(ComboPage_Clear
, ComboboxWidgetsPage::OnButtonClear
)
177 EVT_BUTTON(ComboPage_Insert
, ComboboxWidgetsPage::OnButtonInsert
)
178 EVT_BUTTON(ComboPage_Add
, ComboboxWidgetsPage::OnButtonAdd
)
179 EVT_BUTTON(ComboPage_AddSeveral
, ComboboxWidgetsPage::OnButtonAddSeveral
)
180 EVT_BUTTON(ComboPage_AddMany
, ComboboxWidgetsPage::OnButtonAddMany
)
181 EVT_BUTTON(ComboPage_SetValue
, ComboboxWidgetsPage::OnButtonSetValue
)
182 EVT_BUTTON(ComboPage_SetCurrent
, ComboboxWidgetsPage::OnButtonSetCurrent
)
183 EVT_BUTTON(ComboPage_ContainerTests
, ItemContainerWidgetsPage::OnButtonTestItemContainer
)
185 EVT_TEXT_ENTER(ComboPage_InsertText
, ComboboxWidgetsPage::OnButtonInsert
)
186 EVT_TEXT_ENTER(ComboPage_AddText
, ComboboxWidgetsPage::OnButtonAdd
)
187 EVT_TEXT_ENTER(ComboPage_DeleteText
, ComboboxWidgetsPage::OnButtonDelete
)
189 EVT_UPDATE_UI(ComboPage_InsertionPointText
, ComboboxWidgetsPage::OnUpdateUIInsertionPointText
)
191 EVT_UPDATE_UI(ComboPage_Reset
, ComboboxWidgetsPage::OnUpdateUIResetButton
)
192 EVT_UPDATE_UI(ComboPage_Insert
, ComboboxWidgetsPage::OnUpdateUIInsert
)
193 EVT_UPDATE_UI(ComboPage_AddSeveral
, ComboboxWidgetsPage::OnUpdateUIAddSeveral
)
194 EVT_UPDATE_UI(ComboPage_Clear
, ComboboxWidgetsPage::OnUpdateUIClearButton
)
195 EVT_UPDATE_UI(ComboPage_DeleteText
, ComboboxWidgetsPage::OnUpdateUIClearButton
)
196 EVT_UPDATE_UI(ComboPage_Delete
, ComboboxWidgetsPage::OnUpdateUIDeleteButton
)
197 EVT_UPDATE_UI(ComboPage_Change
, ComboboxWidgetsPage::OnUpdateUIDeleteSelButton
)
198 EVT_UPDATE_UI(ComboPage_ChangeText
, ComboboxWidgetsPage::OnUpdateUIDeleteSelButton
)
199 EVT_UPDATE_UI(ComboPage_DeleteSel
, ComboboxWidgetsPage::OnUpdateUIDeleteSelButton
)
200 EVT_UPDATE_UI(ComboPage_SetCurrent
, ComboboxWidgetsPage::OnUpdateUISetCurrent
)
202 EVT_COMBOBOX(ComboPage_Combo
, ComboboxWidgetsPage::OnComboBox
)
203 EVT_COMBOBOX_DROPDOWN(ComboPage_Combo
, ComboboxWidgetsPage::OnDropdown
)
204 EVT_COMBOBOX_CLOSEUP(ComboPage_Combo
, ComboboxWidgetsPage::OnCloseup
)
205 EVT_TEXT(ComboPage_Combo
, ComboboxWidgetsPage::OnComboText
)
206 EVT_TEXT_ENTER(ComboPage_Combo
, ComboboxWidgetsPage::OnComboText
)
208 EVT_CHECKBOX(wxID_ANY
, ComboboxWidgetsPage::OnCheckOrRadioBox
)
209 EVT_RADIOBOX(wxID_ANY
, ComboboxWidgetsPage::OnCheckOrRadioBox
)
212 // ============================================================================
214 // ============================================================================
216 #if defined(__WXUNIVERSAL__)
217 #define FAMILY_CTRLS UNIVERSAL_CTRLS
219 #define FAMILY_CTRLS NATIVE_CTRLS
222 IMPLEMENT_WIDGETS_PAGE(ComboboxWidgetsPage
, wxT("Combobox"),
223 FAMILY_CTRLS
| WITH_ITEMS_CTRLS
| COMBO_CTRLS
226 ComboboxWidgetsPage::ComboboxWidgetsPage(WidgetsBookCtrl
*book
,
227 wxImageList
*imaglist
)
228 : ItemContainerWidgetsPage(book
, imaglist
, combobox_xpm
)
233 m_chkFilename
= (wxCheckBox
*)NULL
;
235 m_combobox
= (wxComboBox
*)NULL
;
236 m_sizerCombo
= (wxSizer
*)NULL
;
239 void ComboboxWidgetsPage::CreateContent()
242 What we create here is a frame having 3 panes: style pane is the
243 leftmost one, in the middle the pane with buttons allowing to perform
244 miscellaneous combobox operations and the pane containing the combobox
247 wxSizer
*sizerTop
= new wxBoxSizer(wxHORIZONTAL
);
250 wxStaticBox
*box
= new wxStaticBox(this, wxID_ANY
, wxT("&Set style"));
252 // should be in sync with ComboKind_XXX values
253 static const wxString kinds
[] =
260 m_radioKind
= new wxRadioBox(this, wxID_ANY
, wxT("Combobox &kind:"),
261 wxDefaultPosition
, wxDefaultSize
,
262 WXSIZEOF(kinds
), kinds
,
263 1, wxRA_SPECIFY_COLS
);
265 wxSizer
*sizerLeft
= new wxStaticBoxSizer(box
, wxVERTICAL
);
267 m_chkSort
= CreateCheckBoxAndAddToSizer(sizerLeft
, wxT("&Sort items"));
268 m_chkReadonly
= CreateCheckBoxAndAddToSizer(sizerLeft
, wxT("&Read only"));
269 m_chkFilename
= CreateCheckBoxAndAddToSizer(sizerLeft
, wxT("&File name"));
270 m_chkFilename
->Disable(); // not implemented yet
272 sizerLeft
->Add(5, 5, 0, wxGROW
| wxALL
, 5); // spacer
273 sizerLeft
->Add(m_radioKind
, 0, wxGROW
| wxALL
, 5);
275 wxButton
*btn
= new wxButton(this, ComboPage_Reset
, wxT("&Reset"));
276 sizerLeft
->Add(btn
, 0, wxALIGN_CENTRE_HORIZONTAL
| wxALL
, 15);
279 wxStaticBox
*box2
= new wxStaticBox(this, wxID_ANY
,
280 wxT("&Change combobox contents"));
281 wxSizer
*sizerMiddle
= new wxStaticBoxSizer(box2
, wxVERTICAL
);
285 sizerRow
= CreateSizerWithTextAndButton(ComboPage_SetCurrent
,
286 wxT("Current &selection"),
290 sizerMiddle
->Add(sizerRow
, 0, wxALL
| wxGROW
, 5);
293 sizerRow
= CreateSizerWithTextAndLabel(wxT("Insertion Point"),
294 ComboPage_InsertionPointText
,
296 text
->SetEditable(false);
298 sizerMiddle
->Add(sizerRow
, 0, wxALL
| wxGROW
, 5);
300 sizerRow
= CreateSizerWithTextAndButton(ComboPage_Insert
,
301 wxT("&Insert this string"),
302 ComboPage_InsertText
,
304 sizerMiddle
->Add(sizerRow
, 0, wxALL
| wxGROW
, 5);
306 sizerRow
= CreateSizerWithTextAndButton(ComboPage_Add
,
307 wxT("&Add this string"),
310 sizerMiddle
->Add(sizerRow
, 0, wxALL
| wxGROW
, 5);
312 btn
= new wxButton(this, ComboPage_AddSeveral
, wxT("&Append a few strings"));
313 sizerMiddle
->Add(btn
, 0, wxALL
| wxGROW
, 5);
315 btn
= new wxButton(this, ComboPage_AddMany
, wxT("Append &many strings"));
316 sizerMiddle
->Add(btn
, 0, wxALL
| wxGROW
, 5);
318 sizerRow
= CreateSizerWithTextAndButton(ComboPage_Change
,
319 wxT("C&hange current"),
320 ComboPage_ChangeText
,
322 sizerMiddle
->Add(sizerRow
, 0, wxALL
| wxGROW
, 5);
324 sizerRow
= CreateSizerWithTextAndButton(ComboPage_Delete
,
325 wxT("&Delete this item"),
326 ComboPage_DeleteText
,
328 sizerMiddle
->Add(sizerRow
, 0, wxALL
| wxGROW
, 5);
330 btn
= new wxButton(this, ComboPage_DeleteSel
, wxT("Delete &selection"));
331 sizerMiddle
->Add(btn
, 0, wxALL
| wxGROW
, 5);
333 btn
= new wxButton(this, ComboPage_Clear
, wxT("&Clear"));
334 sizerMiddle
->Add(btn
, 0, wxALL
| wxGROW
, 5);
336 sizerRow
= CreateSizerWithTextAndButton(ComboPage_SetValue
,
338 ComboPage_SetValueText
,
340 sizerMiddle
->Add(sizerRow
, 0, wxALL
| wxGROW
, 5);
342 btn
= new wxButton(this, ComboPage_ContainerTests
, wxT("Run &tests"));
343 sizerMiddle
->Add(btn
, 0, wxALL
| wxGROW
, 5);
348 wxSizer
*sizerRight
= new wxBoxSizer(wxVERTICAL
);
349 m_combobox
= new wxComboBox(this, ComboPage_Combo
, wxEmptyString
,
350 wxDefaultPosition
, wxDefaultSize
,
353 sizerRight
->Add(m_combobox
, 0, wxGROW
| wxALL
, 5);
354 sizerRight
->SetMinSize(150, 0);
355 m_sizerCombo
= sizerRight
; // save it to modify it later
357 // the 3 panes panes compose the window
358 sizerTop
->Add(sizerLeft
, 0, wxGROW
| (wxALL
& ~wxLEFT
), 10);
359 sizerTop
->Add(sizerMiddle
, 1, wxGROW
| wxALL
, 10);
360 sizerTop
->Add(sizerRight
, 1, wxGROW
| (wxALL
& ~wxRIGHT
), 10);
362 // final initializations
368 // ----------------------------------------------------------------------------
370 // ----------------------------------------------------------------------------
372 void ComboboxWidgetsPage::Reset()
374 m_chkSort
->SetValue(false);
375 m_chkReadonly
->SetValue(false);
376 m_chkFilename
->SetValue(false);
379 void ComboboxWidgetsPage::CreateCombo()
381 int flags
= ms_defaultFlags
;
383 if ( m_chkSort
->GetValue() )
385 if ( m_chkReadonly
->GetValue() )
386 flags
|= wxCB_READONLY
;
388 switch ( m_radioKind
->GetSelection() )
391 wxFAIL_MSG( wxT("unknown combo kind") );
394 case ComboKind_Default
:
397 case ComboKind_Simple
:
398 flags
|= wxCB_SIMPLE
;
401 case ComboKind_DropDown
:
402 flags
= wxCB_DROPDOWN
;
409 unsigned int count
= m_combobox
->GetCount();
410 for ( unsigned int n
= 0; n
< count
; n
++ )
412 items
.Add(m_combobox
->GetString(n
));
415 m_sizerCombo
->Detach( m_combobox
);
419 m_combobox
= new wxComboBox(this, ComboPage_Combo
, wxEmptyString
,
420 wxDefaultPosition
, wxDefaultSize
,
425 if ( m_chkFilename
->GetValue() )
429 unsigned int count
= items
.GetCount();
430 for ( unsigned int n
= 0; n
< count
; n
++ )
432 m_combobox
->Append(items
[n
]);
435 m_sizerCombo
->Add(m_combobox
, 0, wxGROW
| wxALL
, 5);
436 m_sizerCombo
->Layout();
439 // ----------------------------------------------------------------------------
441 // ----------------------------------------------------------------------------
443 void ComboboxWidgetsPage::OnButtonReset(wxCommandEvent
& WXUNUSED(event
))
450 void ComboboxWidgetsPage::OnButtonChange(wxCommandEvent
& WXUNUSED(event
))
452 int sel
= m_combobox
->GetSelection();
453 if ( sel
!= wxNOT_FOUND
)
456 m_combobox
->SetString(sel
, m_textChange
->GetValue());
458 wxLogMessage(wxT("Not implemented in wxGTK"));
463 void ComboboxWidgetsPage::OnButtonDelete(wxCommandEvent
& WXUNUSED(event
))
466 if ( !m_textDelete
->GetValue().ToULong(&n
) ||
467 (n
>= m_combobox
->GetCount()) )
472 m_combobox
->Delete(n
);
475 void ComboboxWidgetsPage::OnButtonDeleteSel(wxCommandEvent
& WXUNUSED(event
))
477 int sel
= m_combobox
->GetSelection();
478 if ( sel
!= wxNOT_FOUND
)
480 m_combobox
->Delete(sel
);
484 void ComboboxWidgetsPage::OnButtonSetValue(wxCommandEvent
& WXUNUSED(event
))
486 wxString value
= m_textSetValue
->GetValue();
487 m_combobox
->SetValue( value
);
490 void ComboboxWidgetsPage::OnButtonClear(wxCommandEvent
& WXUNUSED(event
))
495 void ComboboxWidgetsPage::OnButtonInsert(wxCommandEvent
& WXUNUSED(event
))
497 static unsigned int s_item
= 0;
499 wxString s
= m_textInsert
->GetValue();
500 if ( !m_textInsert
->IsModified() )
502 // update the default string
503 m_textInsert
->SetValue(wxString::Format(wxT("test item %u"), ++s_item
));
506 if (m_combobox
->GetSelection() >= 0)
507 m_combobox
->Insert(s
, m_combobox
->GetSelection());
510 void ComboboxWidgetsPage::OnButtonAdd(wxCommandEvent
& WXUNUSED(event
))
512 static unsigned int s_item
= 0;
514 wxString s
= m_textAdd
->GetValue();
515 if ( !m_textAdd
->IsModified() )
517 // update the default string
518 m_textAdd
->SetValue(wxString::Format(wxT("test item %u"), ++s_item
));
521 m_combobox
->Append(s
);
524 void ComboboxWidgetsPage::OnButtonAddMany(wxCommandEvent
& WXUNUSED(event
))
526 // "many" means 1000 here
527 for ( unsigned int n
= 0; n
< 1000; n
++ )
529 m_combobox
->Append(wxString::Format(wxT("item #%u"), n
));
533 void ComboboxWidgetsPage::OnButtonSetCurrent(wxCommandEvent
& WXUNUSED(event
))
536 if ( !m_textCur
->GetValue().ToLong(&n
) )
539 m_combobox
->SetSelection(n
);
542 void ComboboxWidgetsPage::OnButtonAddSeveral(wxCommandEvent
& WXUNUSED(event
))
544 m_combobox
->Append(wxT("First"));
545 m_combobox
->Append(wxT("another one"));
546 m_combobox
->Append(wxT("and the last (very very very very very very very very very very long) one"));
549 void ComboboxWidgetsPage::OnUpdateUIInsertionPointText(wxUpdateUIEvent
& event
)
552 event
.SetText( wxString::Format(wxT("%ld"), m_combobox
->GetInsertionPoint()) );
555 void ComboboxWidgetsPage::OnUpdateUIResetButton(wxUpdateUIEvent
& event
)
557 event
.Enable( m_chkSort
->GetValue() ||
558 m_chkReadonly
->GetValue() ||
559 m_chkFilename
->GetValue() );
562 void ComboboxWidgetsPage::OnUpdateUIInsert(wxUpdateUIEvent
& event
)
566 bool enable
= !(m_combobox
->GetWindowStyle() & wxCB_SORT
) &&
567 (m_combobox
->GetSelection() >= 0);
569 event
.Enable(enable
);
573 void ComboboxWidgetsPage::OnUpdateUIDeleteButton(wxUpdateUIEvent
& event
)
578 event
.Enable(m_textDelete
->GetValue().ToULong(&n
) &&
579 (n
< (unsigned)m_combobox
->GetCount()));
583 void ComboboxWidgetsPage::OnUpdateUIDeleteSelButton(wxUpdateUIEvent
& event
)
586 event
.Enable(m_combobox
->GetSelection() != wxNOT_FOUND
);
589 void ComboboxWidgetsPage::OnUpdateUIClearButton(wxUpdateUIEvent
& event
)
592 event
.Enable(m_combobox
->GetCount() != 0);
595 void ComboboxWidgetsPage::OnUpdateUIAddSeveral(wxUpdateUIEvent
& event
)
598 event
.Enable(!(m_combobox
->GetWindowStyle() & wxCB_SORT
));
601 void ComboboxWidgetsPage::OnUpdateUISetCurrent(wxUpdateUIEvent
& event
)
604 event
.Enable( m_textCur
->GetValue().ToLong(&n
) &&
606 (n
>= 0 && (unsigned)n
< m_combobox
->GetCount())) );
609 void ComboboxWidgetsPage::OnComboText(wxCommandEvent
& event
)
614 wxString s
= event
.GetString();
616 wxASSERT_MSG( s
== m_combobox
->GetValue(),
617 wxT("event and combobox values should be the same") );
619 if (event
.GetEventType() == wxEVT_COMMAND_TEXT_ENTER
)
621 wxLogMessage(wxT("Combobox enter pressed (now '%s')"), s
.c_str());
625 wxLogMessage(wxT("Combobox text changed (now '%s')"), s
.c_str());
629 void ComboboxWidgetsPage::OnComboBox(wxCommandEvent
& event
)
631 long sel
= event
.GetInt();
632 const wxString selstr
= wxString::Format(wxT("%ld"), sel
);
633 m_textDelete
->SetValue(selstr
);
634 m_textCur
->SetValue(selstr
);
636 wxLogMessage(wxT("Combobox item %ld selected"), sel
);
638 wxLogMessage(wxT("Combobox GetValue(): %s"), m_combobox
->GetValue().c_str() );
641 void ComboboxWidgetsPage::OnCheckOrRadioBox(wxCommandEvent
& WXUNUSED(event
))
646 void ComboboxWidgetsPage::OnDropdown(wxCommandEvent
& WXUNUSED(event
))
648 wxLogMessage(wxT("Combobox dropped down"));
651 void ComboboxWidgetsPage::OnCloseup(wxCommandEvent
& WXUNUSED(event
))
653 wxLogMessage(wxT("Combobox closed up"));
656 #endif // wxUSE_COMBOBOX