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 OnDropdown(wxCommandEvent
& event
);
118 void OnCloseup(wxCommandEvent
& event
);
119 void OnComboBox(wxCommandEvent
& event
);
120 void OnComboText(wxCommandEvent
& event
);
122 void OnCheckOrRadioBox(wxCommandEvent
& event
);
124 void OnUpdateUIInsertionPointText(wxUpdateUIEvent
& event
);
126 void OnUpdateUIInsert(wxUpdateUIEvent
& event
);
127 void OnUpdateUIAddSeveral(wxUpdateUIEvent
& event
);
128 void OnUpdateUIClearButton(wxUpdateUIEvent
& event
);
129 void OnUpdateUIDeleteButton(wxUpdateUIEvent
& event
);
130 void OnUpdateUIDeleteSelButton(wxUpdateUIEvent
& event
);
131 void OnUpdateUIResetButton(wxUpdateUIEvent
& event
);
132 void OnUpdateUISetCurrent(wxUpdateUIEvent
& event
);
134 // reset the combobox parameters
137 // (re)create the combobox
143 // the sel mode radiobox
144 wxRadioBox
*m_radioKind
;
146 // the checkboxes for styles
147 wxCheckBox
*m_chkSort
,
151 // the combobox itself and the sizer it is in
152 wxComboBox
*m_combobox
;
153 wxSizer
*m_sizerCombo
;
155 // the text entries for "Add/change string" and "Delete" buttons
156 wxTextCtrl
*m_textInsert
,
164 DECLARE_EVENT_TABLE()
165 DECLARE_WIDGETS_PAGE(ComboboxWidgetsPage
)
168 // ----------------------------------------------------------------------------
170 // ----------------------------------------------------------------------------
172 BEGIN_EVENT_TABLE(ComboboxWidgetsPage
, WidgetsPage
)
173 EVT_BUTTON(ComboPage_Reset
, ComboboxWidgetsPage::OnButtonReset
)
174 EVT_BUTTON(ComboPage_Change
, ComboboxWidgetsPage::OnButtonChange
)
175 EVT_BUTTON(ComboPage_Delete
, ComboboxWidgetsPage::OnButtonDelete
)
176 EVT_BUTTON(ComboPage_DeleteSel
, ComboboxWidgetsPage::OnButtonDeleteSel
)
177 EVT_BUTTON(ComboPage_Clear
, ComboboxWidgetsPage::OnButtonClear
)
178 EVT_BUTTON(ComboPage_Insert
, ComboboxWidgetsPage::OnButtonInsert
)
179 EVT_BUTTON(ComboPage_Add
, ComboboxWidgetsPage::OnButtonAdd
)
180 EVT_BUTTON(ComboPage_AddSeveral
, ComboboxWidgetsPage::OnButtonAddSeveral
)
181 EVT_BUTTON(ComboPage_AddMany
, ComboboxWidgetsPage::OnButtonAddMany
)
182 EVT_BUTTON(ComboPage_SetValue
, ComboboxWidgetsPage::OnButtonSetValue
)
183 EVT_BUTTON(ComboPage_SetCurrent
, ComboboxWidgetsPage::OnButtonSetCurrent
)
184 EVT_BUTTON(ComboPage_ContainerTests
, ItemContainerWidgetsPage::OnButtonTestItemContainer
)
186 EVT_TEXT_ENTER(ComboPage_InsertText
, ComboboxWidgetsPage::OnButtonInsert
)
187 EVT_TEXT_ENTER(ComboPage_AddText
, ComboboxWidgetsPage::OnButtonAdd
)
188 EVT_TEXT_ENTER(ComboPage_DeleteText
, ComboboxWidgetsPage::OnButtonDelete
)
190 EVT_UPDATE_UI(ComboPage_InsertionPointText
, ComboboxWidgetsPage::OnUpdateUIInsertionPointText
)
192 EVT_UPDATE_UI(ComboPage_Reset
, ComboboxWidgetsPage::OnUpdateUIResetButton
)
193 EVT_UPDATE_UI(ComboPage_Insert
, ComboboxWidgetsPage::OnUpdateUIInsert
)
194 EVT_UPDATE_UI(ComboPage_AddSeveral
, ComboboxWidgetsPage::OnUpdateUIAddSeveral
)
195 EVT_UPDATE_UI(ComboPage_Clear
, ComboboxWidgetsPage::OnUpdateUIClearButton
)
196 EVT_UPDATE_UI(ComboPage_DeleteText
, ComboboxWidgetsPage::OnUpdateUIClearButton
)
197 EVT_UPDATE_UI(ComboPage_Delete
, ComboboxWidgetsPage::OnUpdateUIDeleteButton
)
198 EVT_UPDATE_UI(ComboPage_Change
, ComboboxWidgetsPage::OnUpdateUIDeleteSelButton
)
199 EVT_UPDATE_UI(ComboPage_ChangeText
, ComboboxWidgetsPage::OnUpdateUIDeleteSelButton
)
200 EVT_UPDATE_UI(ComboPage_DeleteSel
, ComboboxWidgetsPage::OnUpdateUIDeleteSelButton
)
201 EVT_UPDATE_UI(ComboPage_SetCurrent
, ComboboxWidgetsPage::OnUpdateUISetCurrent
)
203 EVT_COMBOBOX(ComboPage_Combo
, ComboboxWidgetsPage::OnComboBox
)
204 EVT_COMBOBOX_DROPDOWN(ComboPage_Combo
, ComboboxWidgetsPage::OnDropdown
)
205 EVT_COMBOBOX_CLOSEUP(ComboPage_Combo
, ComboboxWidgetsPage::OnCloseup
)
206 EVT_TEXT(ComboPage_Combo
, ComboboxWidgetsPage::OnComboText
)
207 EVT_TEXT_ENTER(ComboPage_Combo
, ComboboxWidgetsPage::OnComboText
)
209 EVT_CHECKBOX(wxID_ANY
, ComboboxWidgetsPage::OnCheckOrRadioBox
)
210 EVT_RADIOBOX(wxID_ANY
, ComboboxWidgetsPage::OnCheckOrRadioBox
)
213 // ============================================================================
215 // ============================================================================
217 #if defined(__WXUNIVERSAL__)
218 #define FAMILY_CTRLS UNIVERSAL_CTRLS
220 #define FAMILY_CTRLS NATIVE_CTRLS
223 IMPLEMENT_WIDGETS_PAGE(ComboboxWidgetsPage
, wxT("Combobox"),
224 FAMILY_CTRLS
| WITH_ITEMS_CTRLS
| COMBO_CTRLS
227 ComboboxWidgetsPage::ComboboxWidgetsPage(WidgetsBookCtrl
*book
,
228 wxImageList
*imaglist
)
229 : ItemContainerWidgetsPage(book
, imaglist
, combobox_xpm
)
234 m_chkFilename
= (wxCheckBox
*)NULL
;
236 m_combobox
= (wxComboBox
*)NULL
;
237 m_sizerCombo
= (wxSizer
*)NULL
;
240 void ComboboxWidgetsPage::CreateContent()
243 What we create here is a frame having 3 panes: style pane is the
244 leftmost one, in the middle the pane with buttons allowing to perform
245 miscellaneous combobox operations and the pane containing the combobox
248 wxSizer
*sizerTop
= new wxBoxSizer(wxHORIZONTAL
);
251 wxStaticBox
*box
= new wxStaticBox(this, wxID_ANY
, wxT("&Set style"));
253 // should be in sync with ComboKind_XXX values
254 static const wxString kinds
[] =
261 m_radioKind
= new wxRadioBox(this, wxID_ANY
, wxT("Combobox &kind:"),
262 wxDefaultPosition
, wxDefaultSize
,
263 WXSIZEOF(kinds
), kinds
,
264 1, wxRA_SPECIFY_COLS
);
266 wxSizer
*sizerLeft
= new wxStaticBoxSizer(box
, wxVERTICAL
);
268 m_chkSort
= CreateCheckBoxAndAddToSizer(sizerLeft
, wxT("&Sort items"));
269 m_chkReadonly
= CreateCheckBoxAndAddToSizer(sizerLeft
, wxT("&Read only"));
270 m_chkFilename
= CreateCheckBoxAndAddToSizer(sizerLeft
, wxT("&File name"));
271 m_chkFilename
->Disable(); // not implemented yet
273 sizerLeft
->Add(5, 5, 0, wxGROW
| wxALL
, 5); // spacer
274 sizerLeft
->Add(m_radioKind
, 0, wxGROW
| wxALL
, 5);
276 wxButton
*btn
= new wxButton(this, ComboPage_Reset
, wxT("&Reset"));
277 sizerLeft
->Add(btn
, 0, wxALIGN_CENTRE_HORIZONTAL
| wxALL
, 15);
280 wxStaticBox
*box2
= new wxStaticBox(this, wxID_ANY
,
281 wxT("&Change combobox contents"));
282 wxSizer
*sizerMiddle
= new wxStaticBoxSizer(box2
, wxVERTICAL
);
286 sizerRow
= CreateSizerWithTextAndButton(ComboPage_SetCurrent
,
287 wxT("Current &selection"),
291 sizerMiddle
->Add(sizerRow
, 0, wxALL
| wxGROW
, 5);
294 sizerRow
= CreateSizerWithTextAndLabel(wxT("Insertion Point"),
295 ComboPage_InsertionPointText
,
297 text
->SetEditable(false);
299 sizerMiddle
->Add(sizerRow
, 0, wxALL
| wxGROW
, 5);
301 sizerRow
= CreateSizerWithTextAndButton(ComboPage_Insert
,
302 wxT("&Insert this string"),
303 ComboPage_InsertText
,
305 sizerMiddle
->Add(sizerRow
, 0, wxALL
| wxGROW
, 5);
307 sizerRow
= CreateSizerWithTextAndButton(ComboPage_Add
,
308 wxT("&Add this string"),
311 sizerMiddle
->Add(sizerRow
, 0, wxALL
| wxGROW
, 5);
313 btn
= new wxButton(this, ComboPage_AddSeveral
, wxT("&Append a few strings"));
314 sizerMiddle
->Add(btn
, 0, wxALL
| wxGROW
, 5);
316 btn
= new wxButton(this, ComboPage_AddMany
, wxT("Append &many strings"));
317 sizerMiddle
->Add(btn
, 0, wxALL
| wxGROW
, 5);
319 sizerRow
= CreateSizerWithTextAndButton(ComboPage_Change
,
320 wxT("C&hange current"),
321 ComboPage_ChangeText
,
323 sizerMiddle
->Add(sizerRow
, 0, wxALL
| wxGROW
, 5);
325 sizerRow
= CreateSizerWithTextAndButton(ComboPage_Delete
,
326 wxT("&Delete this item"),
327 ComboPage_DeleteText
,
329 sizerMiddle
->Add(sizerRow
, 0, wxALL
| wxGROW
, 5);
331 btn
= new wxButton(this, ComboPage_DeleteSel
, wxT("Delete &selection"));
332 sizerMiddle
->Add(btn
, 0, wxALL
| wxGROW
, 5);
334 btn
= new wxButton(this, ComboPage_Clear
, wxT("&Clear"));
335 sizerMiddle
->Add(btn
, 0, wxALL
| wxGROW
, 5);
337 sizerRow
= CreateSizerWithTextAndButton(ComboPage_SetValue
,
339 ComboPage_SetValueText
,
341 sizerMiddle
->Add(sizerRow
, 0, wxALL
| wxGROW
, 5);
343 btn
= new wxButton(this, ComboPage_ContainerTests
, wxT("Run &tests"));
344 sizerMiddle
->Add(btn
, 0, wxALL
| wxGROW
, 5);
349 wxSizer
*sizerRight
= new wxBoxSizer(wxVERTICAL
);
350 m_combobox
= new wxComboBox(this, ComboPage_Combo
, wxEmptyString
,
351 wxDefaultPosition
, wxDefaultSize
,
354 sizerRight
->Add(m_combobox
, 0, wxGROW
| wxALL
, 5);
355 sizerRight
->SetMinSize(150, 0);
356 m_sizerCombo
= sizerRight
; // save it to modify it later
358 // the 3 panes panes compose the window
359 sizerTop
->Add(sizerLeft
, 0, wxGROW
| (wxALL
& ~wxLEFT
), 10);
360 sizerTop
->Add(sizerMiddle
, 1, wxGROW
| wxALL
, 10);
361 sizerTop
->Add(sizerRight
, 1, wxGROW
| (wxALL
& ~wxRIGHT
), 10);
363 // final initializations
369 // ----------------------------------------------------------------------------
371 // ----------------------------------------------------------------------------
373 void ComboboxWidgetsPage::Reset()
375 m_chkSort
->SetValue(false);
376 m_chkReadonly
->SetValue(false);
377 m_chkFilename
->SetValue(false);
380 void ComboboxWidgetsPage::CreateCombo()
382 int flags
= ms_defaultFlags
;
384 if ( m_chkSort
->GetValue() )
386 if ( m_chkReadonly
->GetValue() )
387 flags
|= wxCB_READONLY
;
389 switch ( m_radioKind
->GetSelection() )
392 wxFAIL_MSG( wxT("unknown combo kind") );
395 case ComboKind_Default
:
398 case ComboKind_Simple
:
399 flags
|= wxCB_SIMPLE
;
402 case ComboKind_DropDown
:
403 flags
= wxCB_DROPDOWN
;
410 unsigned int count
= m_combobox
->GetCount();
411 for ( unsigned int n
= 0; n
< count
; n
++ )
413 items
.Add(m_combobox
->GetString(n
));
416 m_sizerCombo
->Detach( m_combobox
);
420 m_combobox
= new wxComboBox(this, ComboPage_Combo
, wxEmptyString
,
421 wxDefaultPosition
, wxDefaultSize
,
426 if ( m_chkFilename
->GetValue() )
430 unsigned int count
= items
.GetCount();
431 for ( unsigned int n
= 0; n
< count
; n
++ )
433 m_combobox
->Append(items
[n
]);
436 m_sizerCombo
->Add(m_combobox
, 0, wxGROW
| wxALL
, 5);
437 m_sizerCombo
->Layout();
440 // ----------------------------------------------------------------------------
442 // ----------------------------------------------------------------------------
444 void ComboboxWidgetsPage::OnButtonReset(wxCommandEvent
& WXUNUSED(event
))
451 void ComboboxWidgetsPage::OnButtonChange(wxCommandEvent
& WXUNUSED(event
))
453 int sel
= m_combobox
->GetSelection();
454 if ( sel
!= wxNOT_FOUND
)
457 m_combobox
->SetString(sel
, m_textChange
->GetValue());
459 wxLogMessage(wxT("Not implemented in wxGTK"));
464 void ComboboxWidgetsPage::OnButtonDelete(wxCommandEvent
& WXUNUSED(event
))
467 if ( !m_textDelete
->GetValue().ToULong(&n
) ||
468 (n
>= m_combobox
->GetCount()) )
473 m_combobox
->Delete(n
);
476 void ComboboxWidgetsPage::OnButtonDeleteSel(wxCommandEvent
& WXUNUSED(event
))
478 int sel
= m_combobox
->GetSelection();
479 if ( sel
!= wxNOT_FOUND
)
481 m_combobox
->Delete(sel
);
485 void ComboboxWidgetsPage::OnButtonSetValue(wxCommandEvent
& WXUNUSED(event
))
487 wxString value
= m_textSetValue
->GetValue();
488 m_combobox
->SetValue( value
);
491 void ComboboxWidgetsPage::OnButtonClear(wxCommandEvent
& WXUNUSED(event
))
496 void ComboboxWidgetsPage::OnButtonInsert(wxCommandEvent
& WXUNUSED(event
))
498 static unsigned int s_item
= 0;
500 wxString s
= m_textInsert
->GetValue();
501 if ( !m_textInsert
->IsModified() )
503 // update the default string
504 m_textInsert
->SetValue(wxString::Format(wxT("test item %u"), ++s_item
));
507 if (m_combobox
->GetSelection() >= 0)
508 m_combobox
->Insert(s
, m_combobox
->GetSelection());
511 void ComboboxWidgetsPage::OnButtonAdd(wxCommandEvent
& WXUNUSED(event
))
513 static unsigned int s_item
= 0;
515 wxString s
= m_textAdd
->GetValue();
516 if ( !m_textAdd
->IsModified() )
518 // update the default string
519 m_textAdd
->SetValue(wxString::Format(wxT("test item %u"), ++s_item
));
522 m_combobox
->Append(s
);
525 void ComboboxWidgetsPage::OnButtonAddMany(wxCommandEvent
& WXUNUSED(event
))
527 // "many" means 1000 here
528 for ( unsigned int n
= 0; n
< 1000; n
++ )
530 m_combobox
->Append(wxString::Format(wxT("item #%u"), n
));
534 void ComboboxWidgetsPage::OnButtonSetCurrent(wxCommandEvent
& WXUNUSED(event
))
537 if ( !m_textCur
->GetValue().ToLong(&n
) )
540 m_combobox
->SetSelection(n
);
543 void ComboboxWidgetsPage::OnButtonAddSeveral(wxCommandEvent
& WXUNUSED(event
))
545 m_combobox
->Append(wxT("First"));
546 m_combobox
->Append(wxT("another one"));
547 m_combobox
->Append(wxT("and the last (very very very very very very very very very very long) one"));
550 void ComboboxWidgetsPage::OnUpdateUIInsertionPointText(wxUpdateUIEvent
& event
)
553 event
.SetText( wxString::Format(wxT("%ld"), m_combobox
->GetInsertionPoint()) );
556 void ComboboxWidgetsPage::OnUpdateUIResetButton(wxUpdateUIEvent
& event
)
558 event
.Enable( m_chkSort
->GetValue() ||
559 m_chkReadonly
->GetValue() ||
560 m_chkFilename
->GetValue() );
563 void ComboboxWidgetsPage::OnUpdateUIInsert(wxUpdateUIEvent
& event
)
567 bool enable
= !(m_combobox
->GetWindowStyle() & wxCB_SORT
) &&
568 (m_combobox
->GetSelection() >= 0);
570 event
.Enable(enable
);
574 void ComboboxWidgetsPage::OnUpdateUIDeleteButton(wxUpdateUIEvent
& event
)
579 event
.Enable(m_textDelete
->GetValue().ToULong(&n
) &&
580 (n
< (unsigned)m_combobox
->GetCount()));
584 void ComboboxWidgetsPage::OnUpdateUIDeleteSelButton(wxUpdateUIEvent
& event
)
587 event
.Enable(m_combobox
->GetSelection() != wxNOT_FOUND
);
590 void ComboboxWidgetsPage::OnUpdateUIClearButton(wxUpdateUIEvent
& event
)
593 event
.Enable(m_combobox
->GetCount() != 0);
596 void ComboboxWidgetsPage::OnUpdateUIAddSeveral(wxUpdateUIEvent
& event
)
599 event
.Enable(!(m_combobox
->GetWindowStyle() & wxCB_SORT
));
602 void ComboboxWidgetsPage::OnUpdateUISetCurrent(wxUpdateUIEvent
& event
)
605 event
.Enable( m_textCur
->GetValue().ToLong(&n
) &&
607 (n
>= 0 && (unsigned)n
< m_combobox
->GetCount())) );
610 void ComboboxWidgetsPage::OnComboText(wxCommandEvent
& event
)
615 wxString s
= event
.GetString();
617 wxASSERT_MSG( s
== m_combobox
->GetValue(),
618 wxT("event and combobox values should be the same") );
620 if (event
.GetEventType() == wxEVT_COMMAND_TEXT_ENTER
)
622 wxLogMessage(wxT("Combobox enter pressed (now '%s')"), s
.c_str());
626 wxLogMessage(wxT("Combobox text changed (now '%s')"), s
.c_str());
630 void ComboboxWidgetsPage::OnComboBox(wxCommandEvent
& event
)
632 long sel
= event
.GetInt();
633 const wxString selstr
= wxString::Format(wxT("%ld"), sel
);
634 m_textDelete
->SetValue(selstr
);
635 m_textCur
->SetValue(selstr
);
637 wxLogMessage(wxT("Combobox item %ld selected"), sel
);
639 wxLogMessage(wxT("Combobox GetValue(): %s"), m_combobox
->GetValue().c_str() );
642 void ComboboxWidgetsPage::OnCheckOrRadioBox(wxCommandEvent
& WXUNUSED(event
))
647 void ComboboxWidgetsPage::OnDropdown(wxCommandEvent
& WXUNUSED(event
))
649 wxLogMessage(wxT("Combobox dropped down"));
652 void ComboboxWidgetsPage::OnCloseup(wxCommandEvent
& WXUNUSED(event
))
654 wxLogMessage(wxT("Combobox closed up"));
657 #endif //wxUSE_COMBOBOX