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
,
60 ComboPage_InsertionPointText
,
74 ComboPage_SetValueText
,
76 ComboPage_ContainerTests
79 // kinds of comboboxes
87 // ----------------------------------------------------------------------------
88 // ComboboxWidgetsPage
89 // ----------------------------------------------------------------------------
91 class ComboboxWidgetsPage
: public ItemContainerWidgetsPage
94 ComboboxWidgetsPage(WidgetsBookCtrl
*book
, wxImageList
*imaglist
);
96 virtual wxControl
*GetWidget() const { return m_combobox
; }
97 virtual wxTextEntryBase
*GetTextEntry() const { return m_combobox
; }
98 virtual wxItemContainer
* GetContainer() const { return m_combobox
; }
99 virtual void RecreateWidget() { CreateCombo(); }
101 // lazy creation of the content
102 virtual void CreateContent();
106 void OnButtonReset(wxCommandEvent
& event
);
107 void OnButtonPopup(wxCommandEvent
&) { m_combobox
->Popup(); }
108 void OnButtonDismiss(wxCommandEvent
&) { m_combobox
->Dismiss(); }
109 void OnButtonChange(wxCommandEvent
& event
);
110 void OnButtonDelete(wxCommandEvent
& event
);
111 void OnButtonDeleteSel(wxCommandEvent
& event
);
112 void OnButtonClear(wxCommandEvent
& event
);
113 void OnButtonInsert(wxCommandEvent
&event
);
114 void OnButtonAdd(wxCommandEvent
& event
);
115 void OnButtonAddSeveral(wxCommandEvent
& event
);
116 void OnButtonAddMany(wxCommandEvent
& event
);
117 void OnButtonSetValue(wxCommandEvent
& event
);
118 void OnButtonSetCurrent(wxCommandEvent
& event
);
120 void OnDropdown(wxCommandEvent
& event
);
121 void OnCloseup(wxCommandEvent
& event
);
122 void OnComboBox(wxCommandEvent
& event
);
123 void OnComboText(wxCommandEvent
& event
);
125 void OnCheckOrRadioBox(wxCommandEvent
& event
);
127 void OnUpdateUIInsertionPointText(wxUpdateUIEvent
& event
);
129 void OnUpdateUIInsert(wxUpdateUIEvent
& event
);
130 void OnUpdateUIAddSeveral(wxUpdateUIEvent
& event
);
131 void OnUpdateUIClearButton(wxUpdateUIEvent
& event
);
132 void OnUpdateUIDeleteButton(wxUpdateUIEvent
& event
);
133 void OnUpdateUIDeleteSelButton(wxUpdateUIEvent
& event
);
134 void OnUpdateUIResetButton(wxUpdateUIEvent
& event
);
135 void OnUpdateUISetCurrent(wxUpdateUIEvent
& event
);
137 // reset the combobox parameters
140 // (re)create the combobox
146 // the sel mode radiobox
147 wxRadioBox
*m_radioKind
;
149 // the checkboxes for styles
150 wxCheckBox
*m_chkSort
,
154 // the combobox itself and the sizer it is in
155 wxComboBox
*m_combobox
;
156 wxSizer
*m_sizerCombo
;
158 // the text entries for "Add/change string" and "Delete" buttons
159 wxTextCtrl
*m_textInsert
,
167 DECLARE_EVENT_TABLE()
168 DECLARE_WIDGETS_PAGE(ComboboxWidgetsPage
)
171 // ----------------------------------------------------------------------------
173 // ----------------------------------------------------------------------------
175 BEGIN_EVENT_TABLE(ComboboxWidgetsPage
, WidgetsPage
)
176 EVT_BUTTON(ComboPage_Reset
, ComboboxWidgetsPage::OnButtonReset
)
177 EVT_BUTTON(ComboPage_Popup
, ComboboxWidgetsPage::OnButtonPopup
)
178 EVT_BUTTON(ComboPage_Dismiss
, ComboboxWidgetsPage::OnButtonDismiss
)
179 EVT_BUTTON(ComboPage_Change
, ComboboxWidgetsPage::OnButtonChange
)
180 EVT_BUTTON(ComboPage_Delete
, ComboboxWidgetsPage::OnButtonDelete
)
181 EVT_BUTTON(ComboPage_DeleteSel
, ComboboxWidgetsPage::OnButtonDeleteSel
)
182 EVT_BUTTON(ComboPage_Clear
, ComboboxWidgetsPage::OnButtonClear
)
183 EVT_BUTTON(ComboPage_Insert
, ComboboxWidgetsPage::OnButtonInsert
)
184 EVT_BUTTON(ComboPage_Add
, ComboboxWidgetsPage::OnButtonAdd
)
185 EVT_BUTTON(ComboPage_AddSeveral
, ComboboxWidgetsPage::OnButtonAddSeveral
)
186 EVT_BUTTON(ComboPage_AddMany
, ComboboxWidgetsPage::OnButtonAddMany
)
187 EVT_BUTTON(ComboPage_SetValue
, ComboboxWidgetsPage::OnButtonSetValue
)
188 EVT_BUTTON(ComboPage_SetCurrent
, ComboboxWidgetsPage::OnButtonSetCurrent
)
189 EVT_BUTTON(ComboPage_ContainerTests
, ItemContainerWidgetsPage::OnButtonTestItemContainer
)
191 EVT_TEXT_ENTER(ComboPage_InsertText
, ComboboxWidgetsPage::OnButtonInsert
)
192 EVT_TEXT_ENTER(ComboPage_AddText
, ComboboxWidgetsPage::OnButtonAdd
)
193 EVT_TEXT_ENTER(ComboPage_DeleteText
, ComboboxWidgetsPage::OnButtonDelete
)
195 EVT_UPDATE_UI(ComboPage_InsertionPointText
, ComboboxWidgetsPage::OnUpdateUIInsertionPointText
)
197 EVT_UPDATE_UI(ComboPage_Reset
, ComboboxWidgetsPage::OnUpdateUIResetButton
)
198 EVT_UPDATE_UI(ComboPage_Insert
, ComboboxWidgetsPage::OnUpdateUIInsert
)
199 EVT_UPDATE_UI(ComboPage_AddSeveral
, ComboboxWidgetsPage::OnUpdateUIAddSeveral
)
200 EVT_UPDATE_UI(ComboPage_Clear
, ComboboxWidgetsPage::OnUpdateUIClearButton
)
201 EVT_UPDATE_UI(ComboPage_DeleteText
, ComboboxWidgetsPage::OnUpdateUIClearButton
)
202 EVT_UPDATE_UI(ComboPage_Delete
, ComboboxWidgetsPage::OnUpdateUIDeleteButton
)
203 EVT_UPDATE_UI(ComboPage_Change
, ComboboxWidgetsPage::OnUpdateUIDeleteSelButton
)
204 EVT_UPDATE_UI(ComboPage_ChangeText
, ComboboxWidgetsPage::OnUpdateUIDeleteSelButton
)
205 EVT_UPDATE_UI(ComboPage_DeleteSel
, ComboboxWidgetsPage::OnUpdateUIDeleteSelButton
)
206 EVT_UPDATE_UI(ComboPage_SetCurrent
, ComboboxWidgetsPage::OnUpdateUISetCurrent
)
208 EVT_COMBOBOX(ComboPage_Combo
, ComboboxWidgetsPage::OnComboBox
)
209 EVT_COMBOBOX_DROPDOWN(ComboPage_Combo
, ComboboxWidgetsPage::OnDropdown
)
210 EVT_COMBOBOX_CLOSEUP(ComboPage_Combo
, ComboboxWidgetsPage::OnCloseup
)
211 EVT_TEXT(ComboPage_Combo
, ComboboxWidgetsPage::OnComboText
)
212 EVT_TEXT_ENTER(ComboPage_Combo
, ComboboxWidgetsPage::OnComboText
)
214 EVT_CHECKBOX(wxID_ANY
, ComboboxWidgetsPage::OnCheckOrRadioBox
)
215 EVT_RADIOBOX(wxID_ANY
, ComboboxWidgetsPage::OnCheckOrRadioBox
)
218 // ============================================================================
220 // ============================================================================
222 #if defined(__WXUNIVERSAL__)
223 #define FAMILY_CTRLS UNIVERSAL_CTRLS
225 #define FAMILY_CTRLS NATIVE_CTRLS
228 IMPLEMENT_WIDGETS_PAGE(ComboboxWidgetsPage
, wxT("Combobox"),
229 FAMILY_CTRLS
| WITH_ITEMS_CTRLS
| COMBO_CTRLS
232 ComboboxWidgetsPage::ComboboxWidgetsPage(WidgetsBookCtrl
*book
,
233 wxImageList
*imaglist
)
234 : ItemContainerWidgetsPage(book
, imaglist
, combobox_xpm
)
239 m_chkFilename
= (wxCheckBox
*)NULL
;
241 m_combobox
= (wxComboBox
*)NULL
;
242 m_sizerCombo
= (wxSizer
*)NULL
;
245 void ComboboxWidgetsPage::CreateContent()
248 What we create here is a frame having 3 panes: style pane is the
249 leftmost one, in the middle the pane with buttons allowing to perform
250 miscellaneous combobox operations and the pane containing the combobox
253 wxSizer
*sizerTop
= new wxBoxSizer(wxHORIZONTAL
);
257 // should be in sync with ComboKind_XXX values
258 static const wxString kinds
[] =
265 m_radioKind
= new wxRadioBox(this, wxID_ANY
, wxT("Combobox &kind:"),
266 wxDefaultPosition
, wxDefaultSize
,
267 WXSIZEOF(kinds
), kinds
,
268 1, wxRA_SPECIFY_COLS
);
270 wxSizer
*sizerLeftTop
= new wxStaticBoxSizer(wxVERTICAL
, this, "&Set style");
272 m_chkSort
= CreateCheckBoxAndAddToSizer(sizerLeftTop
, wxT("&Sort items"));
273 m_chkReadonly
= CreateCheckBoxAndAddToSizer(sizerLeftTop
, wxT("&Read only"));
274 m_chkFilename
= CreateCheckBoxAndAddToSizer(sizerLeftTop
, wxT("&File name"));
275 m_chkFilename
->Disable(); // not implemented yet
277 sizerLeftTop
->Add(5, 5, 0, wxGROW
| wxALL
, 5); // spacer
278 sizerLeftTop
->Add(m_radioKind
, 0, wxGROW
| wxALL
, 5);
280 wxButton
*btn
= new wxButton(this, ComboPage_Reset
, wxT("&Reset"));
281 sizerLeftTop
->Add(btn
, 0, wxALIGN_CENTRE_HORIZONTAL
| wxALL
, 15);
284 wxSizer
*sizerLeftBottom
= new wxStaticBoxSizer(wxVERTICAL
, this, "&Popup");
285 sizerLeftBottom
->Add(new wxButton(this, ComboPage_Popup
, "&Show"),
286 wxSizerFlags().Border().Centre());
287 sizerLeftBottom
->Add(new wxButton(this, ComboPage_Dismiss
, "&Hide"),
288 wxSizerFlags().Border().Centre());
291 wxSizer
*sizerLeft
= new wxBoxSizer(wxVERTICAL
);
292 sizerLeft
->Add(sizerLeftTop
);
293 sizerLeft
->AddSpacer(10);
294 sizerLeft
->Add(sizerLeftBottom
);
297 wxStaticBox
*box2
= new wxStaticBox(this, wxID_ANY
,
298 wxT("&Change combobox contents"));
299 wxSizer
*sizerMiddle
= new wxStaticBoxSizer(box2
, wxVERTICAL
);
303 sizerRow
= CreateSizerWithTextAndButton(ComboPage_SetCurrent
,
304 wxT("Current &selection"),
308 sizerMiddle
->Add(sizerRow
, 0, wxALL
| wxGROW
, 5);
311 sizerRow
= CreateSizerWithTextAndLabel(wxT("Insertion Point"),
312 ComboPage_InsertionPointText
,
314 text
->SetEditable(false);
316 sizerMiddle
->Add(sizerRow
, 0, wxALL
| wxGROW
, 5);
318 sizerRow
= CreateSizerWithTextAndButton(ComboPage_Insert
,
319 wxT("&Insert this string"),
320 ComboPage_InsertText
,
322 sizerMiddle
->Add(sizerRow
, 0, wxALL
| wxGROW
, 5);
324 sizerRow
= CreateSizerWithTextAndButton(ComboPage_Add
,
325 wxT("&Add this string"),
328 sizerMiddle
->Add(sizerRow
, 0, wxALL
| wxGROW
, 5);
330 btn
= new wxButton(this, ComboPage_AddSeveral
, wxT("&Append a few strings"));
331 sizerMiddle
->Add(btn
, 0, wxALL
| wxGROW
, 5);
333 btn
= new wxButton(this, ComboPage_AddMany
, wxT("Append &many strings"));
334 sizerMiddle
->Add(btn
, 0, wxALL
| wxGROW
, 5);
336 sizerRow
= CreateSizerWithTextAndButton(ComboPage_Change
,
337 wxT("C&hange current"),
338 ComboPage_ChangeText
,
340 sizerMiddle
->Add(sizerRow
, 0, wxALL
| wxGROW
, 5);
342 sizerRow
= CreateSizerWithTextAndButton(ComboPage_Delete
,
343 wxT("&Delete this item"),
344 ComboPage_DeleteText
,
346 sizerMiddle
->Add(sizerRow
, 0, wxALL
| wxGROW
, 5);
348 btn
= new wxButton(this, ComboPage_DeleteSel
, wxT("Delete &selection"));
349 sizerMiddle
->Add(btn
, 0, wxALL
| wxGROW
, 5);
351 btn
= new wxButton(this, ComboPage_Clear
, wxT("&Clear"));
352 sizerMiddle
->Add(btn
, 0, wxALL
| wxGROW
, 5);
354 sizerRow
= CreateSizerWithTextAndButton(ComboPage_SetValue
,
356 ComboPage_SetValueText
,
358 sizerMiddle
->Add(sizerRow
, 0, wxALL
| wxGROW
, 5);
360 btn
= new wxButton(this, ComboPage_ContainerTests
, wxT("Run &tests"));
361 sizerMiddle
->Add(btn
, 0, wxALL
| wxGROW
, 5);
366 wxSizer
*sizerRight
= new wxBoxSizer(wxVERTICAL
);
367 m_combobox
= new wxComboBox(this, ComboPage_Combo
, wxEmptyString
,
368 wxDefaultPosition
, wxDefaultSize
,
371 sizerRight
->Add(m_combobox
, 0, wxGROW
| wxALL
, 5);
372 sizerRight
->SetMinSize(150, 0);
373 m_sizerCombo
= sizerRight
; // save it to modify it later
375 // the 3 panes panes compose the window
376 sizerTop
->Add(sizerLeft
, 0, wxGROW
| (wxALL
& ~wxLEFT
), 10);
377 sizerTop
->Add(sizerMiddle
, 1, wxGROW
| wxALL
, 10);
378 sizerTop
->Add(sizerRight
, 1, wxGROW
| (wxALL
& ~wxRIGHT
), 10);
380 // final initializations
386 // ----------------------------------------------------------------------------
388 // ----------------------------------------------------------------------------
390 void ComboboxWidgetsPage::Reset()
392 m_chkSort
->SetValue(false);
393 m_chkReadonly
->SetValue(false);
394 m_chkFilename
->SetValue(false);
397 void ComboboxWidgetsPage::CreateCombo()
399 int flags
= ms_defaultFlags
;
401 if ( m_chkSort
->GetValue() )
403 if ( m_chkReadonly
->GetValue() )
404 flags
|= wxCB_READONLY
;
406 switch ( m_radioKind
->GetSelection() )
409 wxFAIL_MSG( wxT("unknown combo kind") );
412 case ComboKind_Default
:
415 case ComboKind_Simple
:
416 flags
|= wxCB_SIMPLE
;
419 case ComboKind_DropDown
:
420 flags
= wxCB_DROPDOWN
;
427 unsigned int count
= m_combobox
->GetCount();
428 for ( unsigned int n
= 0; n
< count
; n
++ )
430 items
.Add(m_combobox
->GetString(n
));
433 m_sizerCombo
->Detach( m_combobox
);
437 m_combobox
= new wxComboBox(this, ComboPage_Combo
, wxEmptyString
,
438 wxDefaultPosition
, wxDefaultSize
,
443 if ( m_chkFilename
->GetValue() )
447 unsigned int count
= items
.GetCount();
448 for ( unsigned int n
= 0; n
< count
; n
++ )
450 m_combobox
->Append(items
[n
]);
453 m_sizerCombo
->Add(m_combobox
, 0, wxGROW
| wxALL
, 5);
454 m_sizerCombo
->Layout();
457 // ----------------------------------------------------------------------------
459 // ----------------------------------------------------------------------------
461 void ComboboxWidgetsPage::OnButtonReset(wxCommandEvent
& WXUNUSED(event
))
468 void ComboboxWidgetsPage::OnButtonChange(wxCommandEvent
& WXUNUSED(event
))
470 int sel
= m_combobox
->GetSelection();
471 if ( sel
!= wxNOT_FOUND
)
474 m_combobox
->SetString(sel
, m_textChange
->GetValue());
476 wxLogMessage(wxT("Not implemented in wxGTK"));
481 void ComboboxWidgetsPage::OnButtonDelete(wxCommandEvent
& WXUNUSED(event
))
484 if ( !m_textDelete
->GetValue().ToULong(&n
) ||
485 (n
>= m_combobox
->GetCount()) )
490 m_combobox
->Delete(n
);
493 void ComboboxWidgetsPage::OnButtonDeleteSel(wxCommandEvent
& WXUNUSED(event
))
495 int sel
= m_combobox
->GetSelection();
496 if ( sel
!= wxNOT_FOUND
)
498 m_combobox
->Delete(sel
);
502 void ComboboxWidgetsPage::OnButtonSetValue(wxCommandEvent
& WXUNUSED(event
))
504 wxString value
= m_textSetValue
->GetValue();
505 m_combobox
->SetValue( value
);
508 void ComboboxWidgetsPage::OnButtonClear(wxCommandEvent
& WXUNUSED(event
))
513 void ComboboxWidgetsPage::OnButtonInsert(wxCommandEvent
& WXUNUSED(event
))
515 static unsigned int s_item
= 0;
517 wxString s
= m_textInsert
->GetValue();
518 if ( !m_textInsert
->IsModified() )
520 // update the default string
521 m_textInsert
->SetValue(wxString::Format(wxT("test item %u"), ++s_item
));
524 if (m_combobox
->GetSelection() >= 0)
525 m_combobox
->Insert(s
, m_combobox
->GetSelection());
528 void ComboboxWidgetsPage::OnButtonAdd(wxCommandEvent
& WXUNUSED(event
))
530 static unsigned int s_item
= 0;
532 wxString s
= m_textAdd
->GetValue();
533 if ( !m_textAdd
->IsModified() )
535 // update the default string
536 m_textAdd
->SetValue(wxString::Format(wxT("test item %u"), ++s_item
));
539 m_combobox
->Append(s
);
542 void ComboboxWidgetsPage::OnButtonAddMany(wxCommandEvent
& WXUNUSED(event
))
544 // "many" means 1000 here
545 for ( unsigned int n
= 0; n
< 1000; n
++ )
547 m_combobox
->Append(wxString::Format(wxT("item #%u"), n
));
551 void ComboboxWidgetsPage::OnButtonSetCurrent(wxCommandEvent
& WXUNUSED(event
))
554 if ( !m_textCur
->GetValue().ToLong(&n
) )
557 m_combobox
->SetSelection(n
);
560 void ComboboxWidgetsPage::OnButtonAddSeveral(wxCommandEvent
& WXUNUSED(event
))
562 m_combobox
->Append(wxT("First"));
563 m_combobox
->Append(wxT("another one"));
564 m_combobox
->Append(wxT("and the last (very very very very very very very very very very long) one"));
567 void ComboboxWidgetsPage::OnUpdateUIInsertionPointText(wxUpdateUIEvent
& event
)
570 event
.SetText( wxString::Format(wxT("%ld"), m_combobox
->GetInsertionPoint()) );
573 void ComboboxWidgetsPage::OnUpdateUIResetButton(wxUpdateUIEvent
& event
)
575 event
.Enable( m_chkSort
->GetValue() ||
576 m_chkReadonly
->GetValue() ||
577 m_chkFilename
->GetValue() );
580 void ComboboxWidgetsPage::OnUpdateUIInsert(wxUpdateUIEvent
& event
)
584 bool enable
= !(m_combobox
->GetWindowStyle() & wxCB_SORT
) &&
585 (m_combobox
->GetSelection() >= 0);
587 event
.Enable(enable
);
591 void ComboboxWidgetsPage::OnUpdateUIDeleteButton(wxUpdateUIEvent
& event
)
596 event
.Enable(m_textDelete
->GetValue().ToULong(&n
) &&
597 (n
< (unsigned)m_combobox
->GetCount()));
601 void ComboboxWidgetsPage::OnUpdateUIDeleteSelButton(wxUpdateUIEvent
& event
)
604 event
.Enable(m_combobox
->GetSelection() != wxNOT_FOUND
);
607 void ComboboxWidgetsPage::OnUpdateUIClearButton(wxUpdateUIEvent
& event
)
610 event
.Enable(m_combobox
->GetCount() != 0);
613 void ComboboxWidgetsPage::OnUpdateUIAddSeveral(wxUpdateUIEvent
& event
)
616 event
.Enable(!(m_combobox
->GetWindowStyle() & wxCB_SORT
));
619 void ComboboxWidgetsPage::OnUpdateUISetCurrent(wxUpdateUIEvent
& event
)
622 event
.Enable( m_textCur
->GetValue().ToLong(&n
) &&
624 (n
>= 0 && (unsigned)n
< m_combobox
->GetCount())) );
627 void ComboboxWidgetsPage::OnComboText(wxCommandEvent
& event
)
632 wxString s
= event
.GetString();
634 wxASSERT_MSG( s
== m_combobox
->GetValue(),
635 wxT("event and combobox values should be the same") );
637 if (event
.GetEventType() == wxEVT_COMMAND_TEXT_ENTER
)
639 wxLogMessage(wxT("Combobox enter pressed (now '%s')"), s
.c_str());
643 wxLogMessage(wxT("Combobox text changed (now '%s')"), s
.c_str());
647 void ComboboxWidgetsPage::OnComboBox(wxCommandEvent
& event
)
649 long sel
= event
.GetInt();
650 const wxString selstr
= wxString::Format(wxT("%ld"), sel
);
651 m_textDelete
->SetValue(selstr
);
652 m_textCur
->SetValue(selstr
);
654 wxLogMessage(wxT("Combobox item %ld selected"), sel
);
656 wxLogMessage(wxT("Combobox GetValue(): %s"), m_combobox
->GetValue().c_str() );
659 void ComboboxWidgetsPage::OnCheckOrRadioBox(wxCommandEvent
& WXUNUSED(event
))
664 void ComboboxWidgetsPage::OnDropdown(wxCommandEvent
& WXUNUSED(event
))
666 wxLogMessage(wxT("Combobox dropped down"));
669 void ComboboxWidgetsPage::OnCloseup(wxCommandEvent
& WXUNUSED(event
))
671 wxLogMessage(wxT("Combobox closed up"));
674 #endif // wxUSE_COMBOBOX