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 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 OnComboBox(wxCommandEvent
& event
);
117 void OnComboText(wxCommandEvent
& event
);
119 void OnCheckOrRadioBox(wxCommandEvent
& event
);
121 void OnUpdateUIInsertionPointText(wxUpdateUIEvent
& event
);
123 void OnUpdateUIInsert(wxUpdateUIEvent
& event
);
124 void OnUpdateUIAddSeveral(wxUpdateUIEvent
& event
);
125 void OnUpdateUIClearButton(wxUpdateUIEvent
& event
);
126 void OnUpdateUIDeleteButton(wxUpdateUIEvent
& event
);
127 void OnUpdateUIDeleteSelButton(wxUpdateUIEvent
& event
);
128 void OnUpdateUIResetButton(wxUpdateUIEvent
& event
);
129 void OnUpdateUISetCurrent(wxUpdateUIEvent
& event
);
131 // reset the combobox parameters
134 // (re)create the combobox
140 // the sel mode radiobox
141 wxRadioBox
*m_radioKind
;
143 // the checkboxes for styles
144 wxCheckBox
*m_chkSort
,
148 // the combobox itself and the sizer it is in
149 wxComboBox
*m_combobox
;
150 wxSizer
*m_sizerCombo
;
152 // the text entries for "Add/change string" and "Delete" buttons
153 wxTextCtrl
*m_textInsert
,
161 DECLARE_EVENT_TABLE()
162 DECLARE_WIDGETS_PAGE(ComboboxWidgetsPage
)
165 // ----------------------------------------------------------------------------
167 // ----------------------------------------------------------------------------
169 BEGIN_EVENT_TABLE(ComboboxWidgetsPage
, WidgetsPage
)
170 EVT_BUTTON(ComboPage_Reset
, ComboboxWidgetsPage::OnButtonReset
)
171 EVT_BUTTON(ComboPage_Change
, ComboboxWidgetsPage::OnButtonChange
)
172 EVT_BUTTON(ComboPage_Delete
, ComboboxWidgetsPage::OnButtonDelete
)
173 EVT_BUTTON(ComboPage_DeleteSel
, ComboboxWidgetsPage::OnButtonDeleteSel
)
174 EVT_BUTTON(ComboPage_Clear
, ComboboxWidgetsPage::OnButtonClear
)
175 EVT_BUTTON(ComboPage_Insert
, ComboboxWidgetsPage::OnButtonInsert
)
176 EVT_BUTTON(ComboPage_Add
, ComboboxWidgetsPage::OnButtonAdd
)
177 EVT_BUTTON(ComboPage_AddSeveral
, ComboboxWidgetsPage::OnButtonAddSeveral
)
178 EVT_BUTTON(ComboPage_AddMany
, ComboboxWidgetsPage::OnButtonAddMany
)
179 EVT_BUTTON(ComboPage_SetValue
, ComboboxWidgetsPage::OnButtonSetValue
)
180 EVT_BUTTON(ComboPage_SetCurrent
, ComboboxWidgetsPage::OnButtonSetCurrent
)
181 EVT_BUTTON(ComboPage_ContainerTests
, ItemContainerWidgetsPage::OnButtonTestItemContainer
)
183 EVT_TEXT_ENTER(ComboPage_InsertText
, ComboboxWidgetsPage::OnButtonInsert
)
184 EVT_TEXT_ENTER(ComboPage_AddText
, ComboboxWidgetsPage::OnButtonAdd
)
185 EVT_TEXT_ENTER(ComboPage_DeleteText
, ComboboxWidgetsPage::OnButtonDelete
)
187 EVT_UPDATE_UI(ComboPage_InsertionPointText
, ComboboxWidgetsPage::OnUpdateUIInsertionPointText
)
189 EVT_UPDATE_UI(ComboPage_Reset
, ComboboxWidgetsPage::OnUpdateUIResetButton
)
190 EVT_UPDATE_UI(ComboPage_Insert
, ComboboxWidgetsPage::OnUpdateUIInsert
)
191 EVT_UPDATE_UI(ComboPage_AddSeveral
, ComboboxWidgetsPage::OnUpdateUIAddSeveral
)
192 EVT_UPDATE_UI(ComboPage_Clear
, ComboboxWidgetsPage::OnUpdateUIClearButton
)
193 EVT_UPDATE_UI(ComboPage_DeleteText
, ComboboxWidgetsPage::OnUpdateUIClearButton
)
194 EVT_UPDATE_UI(ComboPage_Delete
, ComboboxWidgetsPage::OnUpdateUIDeleteButton
)
195 EVT_UPDATE_UI(ComboPage_Change
, ComboboxWidgetsPage::OnUpdateUIDeleteSelButton
)
196 EVT_UPDATE_UI(ComboPage_ChangeText
, ComboboxWidgetsPage::OnUpdateUIDeleteSelButton
)
197 EVT_UPDATE_UI(ComboPage_DeleteSel
, ComboboxWidgetsPage::OnUpdateUIDeleteSelButton
)
198 EVT_UPDATE_UI(ComboPage_SetCurrent
, ComboboxWidgetsPage::OnUpdateUISetCurrent
)
200 EVT_COMBOBOX(ComboPage_Combo
, ComboboxWidgetsPage::OnComboBox
)
201 EVT_TEXT(ComboPage_Combo
, ComboboxWidgetsPage::OnComboText
)
202 EVT_TEXT_ENTER(ComboPage_Combo
, ComboboxWidgetsPage::OnComboText
)
204 EVT_CHECKBOX(wxID_ANY
, ComboboxWidgetsPage::OnCheckOrRadioBox
)
205 EVT_RADIOBOX(wxID_ANY
, ComboboxWidgetsPage::OnCheckOrRadioBox
)
208 // ============================================================================
210 // ============================================================================
212 #if defined(__WXUNIVERSAL__)
213 #define FAMILY_CTRLS UNIVERSAL_CTRLS
215 #define FAMILY_CTRLS NATIVE_CTRLS
218 IMPLEMENT_WIDGETS_PAGE(ComboboxWidgetsPage
, _T("Combobox"),
219 FAMILY_CTRLS
| WITH_ITEMS_CTRLS
| COMBO_CTRLS
222 ComboboxWidgetsPage::ComboboxWidgetsPage(WidgetsBookCtrl
*book
,
223 wxImageList
*imaglist
)
224 : ItemContainerWidgetsPage(book
, imaglist
, combobox_xpm
)
229 m_chkFilename
= (wxCheckBox
*)NULL
;
231 m_combobox
= (wxComboBox
*)NULL
;
232 m_sizerCombo
= (wxSizer
*)NULL
;
235 void ComboboxWidgetsPage::CreateContent()
238 What we create here is a frame having 3 panes: style pane is the
239 leftmost one, in the middle the pane with buttons allowing to perform
240 miscellaneous combobox operations and the pane containing the combobox
243 wxSizer
*sizerTop
= new wxBoxSizer(wxHORIZONTAL
);
246 wxStaticBox
*box
= new wxStaticBox(this, wxID_ANY
, _T("&Set style"));
248 // should be in sync with ComboKind_XXX values
249 static const wxString kinds
[] =
256 m_radioKind
= new wxRadioBox(this, wxID_ANY
, _T("Combobox &kind:"),
257 wxDefaultPosition
, wxDefaultSize
,
258 WXSIZEOF(kinds
), kinds
,
259 1, wxRA_SPECIFY_COLS
);
261 wxSizer
*sizerLeft
= new wxStaticBoxSizer(box
, wxVERTICAL
);
263 m_chkSort
= CreateCheckBoxAndAddToSizer(sizerLeft
, _T("&Sort items"));
264 m_chkReadonly
= CreateCheckBoxAndAddToSizer(sizerLeft
, _T("&Read only"));
265 m_chkFilename
= CreateCheckBoxAndAddToSizer(sizerLeft
, _T("&File name"));
266 m_chkFilename
->Disable(); // not implemented yet
268 sizerLeft
->Add(5, 5, 0, wxGROW
| wxALL
, 5); // spacer
269 sizerLeft
->Add(m_radioKind
, 0, wxGROW
| wxALL
, 5);
271 wxButton
*btn
= new wxButton(this, ComboPage_Reset
, _T("&Reset"));
272 sizerLeft
->Add(btn
, 0, wxALIGN_CENTRE_HORIZONTAL
| wxALL
, 15);
275 wxStaticBox
*box2
= new wxStaticBox(this, wxID_ANY
,
276 _T("&Change combobox contents"));
277 wxSizer
*sizerMiddle
= new wxStaticBoxSizer(box2
, wxVERTICAL
);
281 sizerRow
= CreateSizerWithTextAndButton(ComboPage_SetCurrent
,
282 _T("Current &selection"),
286 sizerMiddle
->Add(sizerRow
, 0, wxALL
| wxGROW
, 5);
289 sizerRow
= CreateSizerWithTextAndLabel(_T("Insertion Point"),
290 ComboPage_InsertionPointText
,
292 text
->SetEditable(false);
294 sizerMiddle
->Add(sizerRow
, 0, wxALL
| wxGROW
, 5);
296 sizerRow
= CreateSizerWithTextAndButton(ComboPage_Insert
,
297 _T("&Insert this string"),
298 ComboPage_InsertText
,
300 sizerMiddle
->Add(sizerRow
, 0, wxALL
| wxGROW
, 5);
302 sizerRow
= CreateSizerWithTextAndButton(ComboPage_Add
,
303 _T("&Add this string"),
306 sizerMiddle
->Add(sizerRow
, 0, wxALL
| wxGROW
, 5);
308 btn
= new wxButton(this, ComboPage_AddSeveral
, _T("&Append a few strings"));
309 sizerMiddle
->Add(btn
, 0, wxALL
| wxGROW
, 5);
311 btn
= new wxButton(this, ComboPage_AddMany
, _T("Append &many strings"));
312 sizerMiddle
->Add(btn
, 0, wxALL
| wxGROW
, 5);
314 sizerRow
= CreateSizerWithTextAndButton(ComboPage_Change
,
315 _T("C&hange current"),
316 ComboPage_ChangeText
,
318 sizerMiddle
->Add(sizerRow
, 0, wxALL
| wxGROW
, 5);
320 sizerRow
= CreateSizerWithTextAndButton(ComboPage_Delete
,
321 _T("&Delete this item"),
322 ComboPage_DeleteText
,
324 sizerMiddle
->Add(sizerRow
, 0, wxALL
| wxGROW
, 5);
326 btn
= new wxButton(this, ComboPage_DeleteSel
, _T("Delete &selection"));
327 sizerMiddle
->Add(btn
, 0, wxALL
| wxGROW
, 5);
329 btn
= new wxButton(this, ComboPage_Clear
, _T("&Clear"));
330 sizerMiddle
->Add(btn
, 0, wxALL
| wxGROW
, 5);
332 sizerRow
= CreateSizerWithTextAndButton(ComboPage_SetValue
,
334 ComboPage_SetValueText
,
336 sizerMiddle
->Add(sizerRow
, 0, wxALL
| wxGROW
, 5);
338 btn
= new wxButton(this, ComboPage_ContainerTests
, _T("Run &tests"));
339 sizerMiddle
->Add(btn
, 0, wxALL
| wxGROW
, 5);
344 wxSizer
*sizerRight
= new wxBoxSizer(wxVERTICAL
);
345 m_combobox
= new wxComboBox(this, ComboPage_Combo
, wxEmptyString
,
346 wxDefaultPosition
, wxDefaultSize
,
349 sizerRight
->Add(m_combobox
, 0, wxGROW
| wxALL
, 5);
350 sizerRight
->SetMinSize(150, 0);
351 m_sizerCombo
= sizerRight
; // save it to modify it later
353 // the 3 panes panes compose the window
354 sizerTop
->Add(sizerLeft
, 0, wxGROW
| (wxALL
& ~wxLEFT
), 10);
355 sizerTop
->Add(sizerMiddle
, 1, wxGROW
| wxALL
, 10);
356 sizerTop
->Add(sizerRight
, 1, wxGROW
| (wxALL
& ~wxRIGHT
), 10);
358 // final initializations
364 // ----------------------------------------------------------------------------
366 // ----------------------------------------------------------------------------
368 void ComboboxWidgetsPage::Reset()
370 m_chkSort
->SetValue(false);
371 m_chkReadonly
->SetValue(false);
372 m_chkFilename
->SetValue(false);
375 void ComboboxWidgetsPage::CreateCombo()
377 int flags
= ms_defaultFlags
;
379 if ( m_chkSort
->GetValue() )
381 if ( m_chkReadonly
->GetValue() )
382 flags
|= wxCB_READONLY
;
384 switch ( m_radioKind
->GetSelection() )
387 wxFAIL_MSG( _T("unknown combo kind") );
390 case ComboKind_Default
:
393 case ComboKind_Simple
:
394 flags
|= wxCB_SIMPLE
;
397 case ComboKind_DropDown
:
398 flags
= wxCB_DROPDOWN
;
405 unsigned int count
= m_combobox
->GetCount();
406 for ( unsigned int n
= 0; n
< count
; n
++ )
408 items
.Add(m_combobox
->GetString(n
));
411 m_sizerCombo
->Detach( m_combobox
);
415 m_combobox
= new wxComboBox(this, ComboPage_Combo
, wxEmptyString
,
416 wxDefaultPosition
, wxDefaultSize
,
421 if ( m_chkFilename
->GetValue() )
425 unsigned int count
= items
.GetCount();
426 for ( unsigned int n
= 0; n
< count
; n
++ )
428 m_combobox
->Append(items
[n
]);
431 m_sizerCombo
->Add(m_combobox
, 1, wxGROW
| wxALL
, 5);
432 m_sizerCombo
->Layout();
435 // ----------------------------------------------------------------------------
437 // ----------------------------------------------------------------------------
439 void ComboboxWidgetsPage::OnButtonReset(wxCommandEvent
& WXUNUSED(event
))
446 void ComboboxWidgetsPage::OnButtonChange(wxCommandEvent
& WXUNUSED(event
))
448 int sel
= m_combobox
->GetSelection();
449 if ( sel
!= wxNOT_FOUND
)
452 m_combobox
->SetString(sel
, m_textChange
->GetValue());
454 wxLogMessage(_T("Not implemented in wxGTK"));
459 void ComboboxWidgetsPage::OnButtonDelete(wxCommandEvent
& WXUNUSED(event
))
462 if ( !m_textDelete
->GetValue().ToULong(&n
) ||
463 (n
>= m_combobox
->GetCount()) )
468 m_combobox
->Delete(n
);
471 void ComboboxWidgetsPage::OnButtonDeleteSel(wxCommandEvent
& WXUNUSED(event
))
473 int sel
= m_combobox
->GetSelection();
474 if ( sel
!= wxNOT_FOUND
)
476 m_combobox
->Delete(sel
);
480 void ComboboxWidgetsPage::OnButtonSetValue(wxCommandEvent
& WXUNUSED(event
))
482 wxString value
= m_textSetValue
->GetValue();
483 m_combobox
->SetValue( value
);
486 void ComboboxWidgetsPage::OnButtonClear(wxCommandEvent
& WXUNUSED(event
))
491 void ComboboxWidgetsPage::OnButtonInsert(wxCommandEvent
& WXUNUSED(event
))
493 static unsigned int s_item
= 0;
495 wxString s
= m_textInsert
->GetValue();
496 if ( !m_textInsert
->IsModified() )
498 // update the default string
499 m_textInsert
->SetValue(wxString::Format(_T("test item %u"), ++s_item
));
502 if (m_combobox
->GetSelection() >= 0)
503 m_combobox
->Insert(s
, m_combobox
->GetSelection());
506 void ComboboxWidgetsPage::OnButtonAdd(wxCommandEvent
& WXUNUSED(event
))
508 static unsigned int s_item
= 0;
510 wxString s
= m_textAdd
->GetValue();
511 if ( !m_textAdd
->IsModified() )
513 // update the default string
514 m_textAdd
->SetValue(wxString::Format(_T("test item %u"), ++s_item
));
517 m_combobox
->Append(s
);
520 void ComboboxWidgetsPage::OnButtonAddMany(wxCommandEvent
& WXUNUSED(event
))
522 // "many" means 1000 here
523 for ( unsigned int n
= 0; n
< 1000; n
++ )
525 m_combobox
->Append(wxString::Format(_T("item #%u"), n
));
529 void ComboboxWidgetsPage::OnButtonSetCurrent(wxCommandEvent
& WXUNUSED(event
))
532 if ( !m_textCur
->GetValue().ToLong(&n
) )
535 m_combobox
->SetSelection(n
);
538 void ComboboxWidgetsPage::OnButtonAddSeveral(wxCommandEvent
& WXUNUSED(event
))
540 m_combobox
->Append(_T("First"));
541 m_combobox
->Append(_T("another one"));
542 m_combobox
->Append(_T("and the last (very very very very very very very very very very long) one"));
545 void ComboboxWidgetsPage::OnUpdateUIInsertionPointText(wxUpdateUIEvent
& event
)
548 event
.SetText( wxString::Format(_T("%ld"), m_combobox
->GetInsertionPoint()) );
551 void ComboboxWidgetsPage::OnUpdateUIResetButton(wxUpdateUIEvent
& event
)
553 event
.Enable( m_chkSort
->GetValue() ||
554 m_chkReadonly
->GetValue() ||
555 m_chkFilename
->GetValue() );
558 void ComboboxWidgetsPage::OnUpdateUIInsert(wxUpdateUIEvent
& event
)
562 bool enable
= !(m_combobox
->GetWindowStyle() & wxCB_SORT
) &&
563 (m_combobox
->GetSelection() >= 0);
565 event
.Enable(enable
);
569 void ComboboxWidgetsPage::OnUpdateUIDeleteButton(wxUpdateUIEvent
& event
)
574 event
.Enable(m_textDelete
->GetValue().ToULong(&n
) &&
575 (n
< (unsigned)m_combobox
->GetCount()));
579 void ComboboxWidgetsPage::OnUpdateUIDeleteSelButton(wxUpdateUIEvent
& event
)
582 event
.Enable(m_combobox
->GetSelection() != wxNOT_FOUND
);
585 void ComboboxWidgetsPage::OnUpdateUIClearButton(wxUpdateUIEvent
& event
)
588 event
.Enable(m_combobox
->GetCount() != 0);
591 void ComboboxWidgetsPage::OnUpdateUIAddSeveral(wxUpdateUIEvent
& event
)
594 event
.Enable(!(m_combobox
->GetWindowStyle() & wxCB_SORT
));
597 void ComboboxWidgetsPage::OnUpdateUISetCurrent(wxUpdateUIEvent
& event
)
600 event
.Enable( m_textCur
->GetValue().ToLong(&n
) &&
602 (n
>= 0 && (unsigned)n
< m_combobox
->GetCount())) );
605 void ComboboxWidgetsPage::OnComboText(wxCommandEvent
& event
)
610 wxString s
= event
.GetString();
612 wxASSERT_MSG( s
== m_combobox
->GetValue(),
613 _T("event and combobox values should be the same") );
615 if (event
.GetEventType() == wxEVT_COMMAND_TEXT_ENTER
)
616 wxLogMessage(_T("Combobox enter pressed (now '%s')"), s
.c_str());
618 wxLogMessage(_T("Combobox text changed (now '%s')"), s
.c_str());
621 void ComboboxWidgetsPage::OnComboBox(wxCommandEvent
& event
)
623 long sel
= event
.GetInt();
624 const wxString selstr
= wxString::Format(_T("%ld"), sel
);
625 m_textDelete
->SetValue(selstr
);
626 m_textCur
->SetValue(selstr
);
628 wxLogMessage(_T("Combobox item %ld selected"), sel
);
630 wxLogMessage(_T("Combobox GetValue(): %s"), m_combobox
->GetValue().c_str() );
633 void ComboboxWidgetsPage::OnCheckOrRadioBox(wxCommandEvent
& WXUNUSED(event
))
638 #endif //wxUSE_COMBOBOX