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"
46 #include "icons/combobox.xpm"
48 // ----------------------------------------------------------------------------
50 // ----------------------------------------------------------------------------
55 ComboPage_Reset
= wxID_HIGHEST
,
57 ComboPage_InsertionPointText
,
73 // kinds of comboboxes
81 // ----------------------------------------------------------------------------
82 // ComboboxWidgetsPage
83 // ----------------------------------------------------------------------------
85 class ComboboxWidgetsPage
: public WidgetsPage
88 ComboboxWidgetsPage(WidgetsBookCtrl
*book
, wxImageList
*imaglist
);
90 virtual wxControl
*GetWidget() const { return m_combobox
; }
91 virtual void RecreateWidget() { CreateCombo(); }
93 // lazy creation of the content
94 virtual void CreateContent();
98 void OnButtonReset(wxCommandEvent
& event
);
99 void OnButtonChange(wxCommandEvent
& event
);
100 void OnButtonDelete(wxCommandEvent
& event
);
101 void OnButtonDeleteSel(wxCommandEvent
& event
);
102 void OnButtonClear(wxCommandEvent
& event
);
103 void OnButtonInsert(wxCommandEvent
&event
);
104 void OnButtonAdd(wxCommandEvent
& event
);
105 void OnButtonAddSeveral(wxCommandEvent
& event
);
106 void OnButtonAddMany(wxCommandEvent
& event
);
108 void OnComboBox(wxCommandEvent
& event
);
109 void OnComboText(wxCommandEvent
& event
);
111 void OnCheckOrRadioBox(wxCommandEvent
& event
);
113 void OnUpdateUICurText(wxUpdateUIEvent
& event
);
114 void OnUpdateUIInsertionPointText(wxUpdateUIEvent
& event
);
116 void OnUpdateUIInsert(wxUpdateUIEvent
& event
);
117 void OnUpdateUIAddSeveral(wxUpdateUIEvent
& event
);
118 void OnUpdateUIClearButton(wxUpdateUIEvent
& event
);
119 void OnUpdateUIDeleteButton(wxUpdateUIEvent
& event
);
120 void OnUpdateUIDeleteSelButton(wxUpdateUIEvent
& event
);
121 void OnUpdateUIResetButton(wxUpdateUIEvent
& event
);
123 // reset the combobox parameters
126 // (re)create the combobox
132 // the sel mode radiobox
133 wxRadioBox
*m_radioKind
;
135 // the checkboxes for styles
136 wxCheckBox
*m_chkSort
,
139 // the combobox itself and the sizer it is in
140 wxComboBox
*m_combobox
;
141 wxSizer
*m_sizerCombo
;
143 // the text entries for "Add/change string" and "Delete" buttons
144 wxTextCtrl
*m_textInsert
,
150 DECLARE_EVENT_TABLE()
151 DECLARE_WIDGETS_PAGE(ComboboxWidgetsPage
)
154 // ----------------------------------------------------------------------------
156 // ----------------------------------------------------------------------------
158 BEGIN_EVENT_TABLE(ComboboxWidgetsPage
, WidgetsPage
)
159 EVT_BUTTON(ComboPage_Reset
, ComboboxWidgetsPage::OnButtonReset
)
160 EVT_BUTTON(ComboPage_Change
, ComboboxWidgetsPage::OnButtonChange
)
161 EVT_BUTTON(ComboPage_Delete
, ComboboxWidgetsPage::OnButtonDelete
)
162 EVT_BUTTON(ComboPage_DeleteSel
, ComboboxWidgetsPage::OnButtonDeleteSel
)
163 EVT_BUTTON(ComboPage_Clear
, ComboboxWidgetsPage::OnButtonClear
)
164 EVT_BUTTON(ComboPage_Insert
, ComboboxWidgetsPage::OnButtonInsert
)
165 EVT_BUTTON(ComboPage_Add
, ComboboxWidgetsPage::OnButtonAdd
)
166 EVT_BUTTON(ComboPage_AddSeveral
, ComboboxWidgetsPage::OnButtonAddSeveral
)
167 EVT_BUTTON(ComboPage_AddMany
, ComboboxWidgetsPage::OnButtonAddMany
)
169 EVT_TEXT_ENTER(ComboPage_InsertText
, ComboboxWidgetsPage::OnButtonInsert
)
170 EVT_TEXT_ENTER(ComboPage_AddText
, ComboboxWidgetsPage::OnButtonAdd
)
171 EVT_TEXT_ENTER(ComboPage_DeleteText
, ComboboxWidgetsPage::OnButtonDelete
)
173 EVT_UPDATE_UI(ComboPage_CurText
, ComboboxWidgetsPage::OnUpdateUICurText
)
174 EVT_UPDATE_UI(ComboPage_InsertionPointText
, ComboboxWidgetsPage::OnUpdateUIInsertionPointText
)
176 EVT_UPDATE_UI(ComboPage_Reset
, ComboboxWidgetsPage::OnUpdateUIResetButton
)
177 EVT_UPDATE_UI(ComboPage_Insert
, ComboboxWidgetsPage::OnUpdateUIInsert
)
178 EVT_UPDATE_UI(ComboPage_AddSeveral
, ComboboxWidgetsPage::OnUpdateUIAddSeveral
)
179 EVT_UPDATE_UI(ComboPage_Clear
, ComboboxWidgetsPage::OnUpdateUIClearButton
)
180 EVT_UPDATE_UI(ComboPage_DeleteText
, ComboboxWidgetsPage::OnUpdateUIClearButton
)
181 EVT_UPDATE_UI(ComboPage_Delete
, ComboboxWidgetsPage::OnUpdateUIDeleteButton
)
182 EVT_UPDATE_UI(ComboPage_Change
, ComboboxWidgetsPage::OnUpdateUIDeleteSelButton
)
183 EVT_UPDATE_UI(ComboPage_ChangeText
, ComboboxWidgetsPage::OnUpdateUIDeleteSelButton
)
184 EVT_UPDATE_UI(ComboPage_DeleteSel
, ComboboxWidgetsPage::OnUpdateUIDeleteSelButton
)
186 EVT_COMBOBOX(ComboPage_Combo
, ComboboxWidgetsPage::OnComboBox
)
187 EVT_TEXT(ComboPage_Combo
, ComboboxWidgetsPage::OnComboText
)
188 EVT_TEXT_ENTER(ComboPage_Combo
, ComboboxWidgetsPage::OnComboText
)
190 EVT_CHECKBOX(wxID_ANY
, ComboboxWidgetsPage::OnCheckOrRadioBox
)
191 EVT_RADIOBOX(wxID_ANY
, ComboboxWidgetsPage::OnCheckOrRadioBox
)
194 // ============================================================================
196 // ============================================================================
198 #if defined(__WXUNIVERSAL__)
199 #define FAMILY_CTRLS UNIVERSAL_CTRLS
201 #define FAMILY_CTRLS NATIVE_CTRLS
204 IMPLEMENT_WIDGETS_PAGE(ComboboxWidgetsPage
, _T("Combobox"),
205 FAMILY_CTRLS
| WITH_ITEMS_CTRLS
| COMBO_CTRLS
208 ComboboxWidgetsPage::ComboboxWidgetsPage(WidgetsBookCtrl
*book
,
209 wxImageList
*imaglist
)
210 : WidgetsPage(book
, imaglist
, combobox_xpm
)
214 m_chkReadonly
= (wxCheckBox
*)NULL
;
216 m_combobox
= (wxComboBox
*)NULL
;
217 m_sizerCombo
= (wxSizer
*)NULL
;
220 void ComboboxWidgetsPage::CreateContent()
223 What we create here is a frame having 3 panes: style pane is the
224 leftmost one, in the middle the pane with buttons allowing to perform
225 miscellaneous combobox operations and the pane containing the combobox
228 wxSizer
*sizerTop
= new wxBoxSizer(wxHORIZONTAL
);
231 wxStaticBox
*box
= new wxStaticBox(this, wxID_ANY
, _T("&Set style"));
233 // should be in sync with ComboKind_XXX values
234 static const wxString kinds
[] =
241 m_radioKind
= new wxRadioBox(this, wxID_ANY
, _T("Combobox &kind:"),
242 wxDefaultPosition
, wxDefaultSize
,
243 WXSIZEOF(kinds
), kinds
,
244 1, wxRA_SPECIFY_COLS
);
246 wxSizer
*sizerLeft
= new wxStaticBoxSizer(box
, wxVERTICAL
);
248 m_chkSort
= CreateCheckBoxAndAddToSizer(sizerLeft
, _T("&Sort items"));
249 m_chkReadonly
= CreateCheckBoxAndAddToSizer(sizerLeft
, _T("&Read only"));
251 sizerLeft
->Add(5, 5, 0, wxGROW
| wxALL
, 5); // spacer
252 sizerLeft
->Add(m_radioKind
, 0, wxGROW
| wxALL
, 5);
254 wxButton
*btn
= new wxButton(this, ComboPage_Reset
, _T("&Reset"));
255 sizerLeft
->Add(btn
, 0, wxALIGN_CENTRE_HORIZONTAL
| wxALL
, 15);
258 wxStaticBox
*box2
= new wxStaticBox(this, wxID_ANY
,
259 _T("&Change combobox contents"));
260 wxSizer
*sizerMiddle
= new wxStaticBoxSizer(box2
, wxVERTICAL
);
265 sizerRow
= CreateSizerWithTextAndLabel(_T("Current selection"),
268 text
->SetEditable(false);
270 sizerMiddle
->Add(sizerRow
, 0, wxALL
| wxGROW
, 5);
272 sizerRow
= CreateSizerWithTextAndLabel(_T("Insertion Point"),
273 ComboPage_InsertionPointText
,
275 text
->SetEditable(false);
277 sizerMiddle
->Add(sizerRow
, 0, wxALL
| wxGROW
, 5);
279 sizerRow
= CreateSizerWithTextAndButton(ComboPage_Insert
,
280 _T("&Insert this string"),
281 ComboPage_InsertText
,
283 sizerMiddle
->Add(sizerRow
, 0, wxALL
| wxGROW
, 5);
285 sizerRow
= CreateSizerWithTextAndButton(ComboPage_Add
,
286 _T("&Add this string"),
289 sizerMiddle
->Add(sizerRow
, 0, wxALL
| wxGROW
, 5);
291 btn
= new wxButton(this, ComboPage_AddSeveral
, _T("&Append a few strings"));
292 sizerMiddle
->Add(btn
, 0, wxALL
| wxGROW
, 5);
294 btn
= new wxButton(this, ComboPage_AddMany
, _T("Append &many strings"));
295 sizerMiddle
->Add(btn
, 0, wxALL
| wxGROW
, 5);
297 sizerRow
= CreateSizerWithTextAndButton(ComboPage_Change
,
298 _T("C&hange current"),
299 ComboPage_ChangeText
,
301 sizerMiddle
->Add(sizerRow
, 0, wxALL
| wxGROW
, 5);
303 sizerRow
= CreateSizerWithTextAndButton(ComboPage_Delete
,
304 _T("&Delete this item"),
305 ComboPage_DeleteText
,
307 sizerMiddle
->Add(sizerRow
, 0, wxALL
| wxGROW
, 5);
309 btn
= new wxButton(this, ComboPage_DeleteSel
, _T("Delete &selection"));
310 sizerMiddle
->Add(btn
, 0, wxALL
| wxGROW
, 5);
312 btn
= new wxButton(this, ComboPage_Clear
, _T("&Clear"));
313 sizerMiddle
->Add(btn
, 0, wxALL
| wxGROW
, 5);
316 wxSizer
*sizerRight
= new wxBoxSizer(wxVERTICAL
);
317 m_combobox
= new wxComboBox(this, ComboPage_Combo
, wxEmptyString
,
318 wxDefaultPosition
, wxDefaultSize
,
321 sizerRight
->Add(m_combobox
, 1, wxGROW
| wxALL
, 5);
322 sizerRight
->SetMinSize(150, 0);
323 m_sizerCombo
= sizerRight
; // save it to modify it later
325 // the 3 panes panes compose the window
326 sizerTop
->Add(sizerLeft
, 0, wxGROW
| (wxALL
& ~wxLEFT
), 10);
327 sizerTop
->Add(sizerMiddle
, 1, wxGROW
| wxALL
, 10);
328 sizerTop
->Add(sizerRight
, 1, wxGROW
| (wxALL
& ~wxRIGHT
), 10);
330 // final initializations
338 // ----------------------------------------------------------------------------
340 // ----------------------------------------------------------------------------
342 void ComboboxWidgetsPage::Reset()
344 m_chkSort
->SetValue(false);
345 m_chkReadonly
->SetValue(false);
348 void ComboboxWidgetsPage::CreateCombo()
350 int flags
= ms_defaultFlags
;
352 if ( m_chkSort
->GetValue() )
354 if ( m_chkReadonly
->GetValue() )
355 flags
|= wxCB_READONLY
;
357 switch ( m_radioKind
->GetSelection() )
360 wxFAIL_MSG( _T("unknown combo kind") );
363 case ComboKind_Default
:
366 case ComboKind_Simple
:
367 flags
|= wxCB_SIMPLE
;
370 case ComboKind_DropDown
:
371 flags
= wxCB_DROPDOWN
;
378 unsigned int count
= m_combobox
->GetCount();
379 for ( unsigned int n
= 0; n
< count
; n
++ )
381 items
.Add(m_combobox
->GetString(n
));
384 m_sizerCombo
->Detach( m_combobox
);
388 m_combobox
= new wxComboBox(this, ComboPage_Combo
, wxEmptyString
,
389 wxDefaultPosition
, wxDefaultSize
,
393 unsigned int count
= items
.GetCount();
394 for ( unsigned int n
= 0; n
< count
; n
++ )
396 m_combobox
->Append(items
[n
]);
399 m_sizerCombo
->Add(m_combobox
, 1, wxGROW
| wxALL
, 5);
400 m_sizerCombo
->Layout();
403 // ----------------------------------------------------------------------------
405 // ----------------------------------------------------------------------------
407 void ComboboxWidgetsPage::OnButtonReset(wxCommandEvent
& WXUNUSED(event
))
414 void ComboboxWidgetsPage::OnButtonChange(wxCommandEvent
& WXUNUSED(event
))
416 int sel
= m_combobox
->GetSelection();
417 if ( sel
!= wxNOT_FOUND
)
420 m_combobox
->SetString(sel
, m_textChange
->GetValue());
422 wxLogMessage(_T("Not implemented in wxGTK"));
427 void ComboboxWidgetsPage::OnButtonDelete(wxCommandEvent
& WXUNUSED(event
))
430 if ( !m_textDelete
->GetValue().ToULong(&n
) ||
431 (n
>= m_combobox
->GetCount()) )
436 m_combobox
->Delete(n
);
439 void ComboboxWidgetsPage::OnButtonDeleteSel(wxCommandEvent
& WXUNUSED(event
))
441 int sel
= m_combobox
->GetSelection();
442 if ( sel
!= wxNOT_FOUND
)
444 m_combobox
->Delete(sel
);
448 void ComboboxWidgetsPage::OnButtonClear(wxCommandEvent
& WXUNUSED(event
))
453 void ComboboxWidgetsPage::OnButtonInsert(wxCommandEvent
& WXUNUSED(event
))
455 static unsigned int s_item
= 0;
457 wxString s
= m_textInsert
->GetValue();
458 if ( !m_textInsert
->IsModified() )
460 // update the default string
461 m_textInsert
->SetValue(wxString::Format(_T("test item %u"), ++s_item
));
464 if (m_combobox
->GetSelection() >= 0)
465 m_combobox
->Insert(s
, m_combobox
->GetSelection());
468 void ComboboxWidgetsPage::OnButtonAdd(wxCommandEvent
& WXUNUSED(event
))
470 static unsigned int s_item
= 0;
472 wxString s
= m_textAdd
->GetValue();
473 if ( !m_textAdd
->IsModified() )
475 // update the default string
476 m_textAdd
->SetValue(wxString::Format(_T("test item %u"), ++s_item
));
479 m_combobox
->Append(s
);
482 void ComboboxWidgetsPage::OnButtonAddMany(wxCommandEvent
& WXUNUSED(event
))
484 // "many" means 1000 here
485 for ( unsigned int n
= 0; n
< 1000; n
++ )
487 m_combobox
->Append(wxString::Format(_T("item #%u"), n
));
491 void ComboboxWidgetsPage::OnButtonAddSeveral(wxCommandEvent
& WXUNUSED(event
))
493 m_combobox
->Append(_T("First"));
494 m_combobox
->Append(_T("another one"));
495 m_combobox
->Append(_T("and the last (very very very very very very very very very very long) one"));
498 void ComboboxWidgetsPage::OnUpdateUICurText(wxUpdateUIEvent
& event
)
501 event
.SetText( wxString::Format(_T("%d"), m_combobox
->GetSelection()) );
504 void ComboboxWidgetsPage::OnUpdateUIInsertionPointText(wxUpdateUIEvent
& event
)
507 event
.SetText( wxString::Format(_T("%ld"), m_combobox
->GetInsertionPoint()) );
510 void ComboboxWidgetsPage::OnUpdateUIResetButton(wxUpdateUIEvent
& event
)
513 event
.Enable( m_chkSort
->GetValue() || m_chkReadonly
->GetValue() );
516 void ComboboxWidgetsPage::OnUpdateUIInsert(wxUpdateUIEvent
& event
)
520 bool enable
= !(m_combobox
->GetWindowStyle() & wxCB_SORT
) &&
521 (m_combobox
->GetSelection() >= 0);
523 event
.Enable(enable
);
527 void ComboboxWidgetsPage::OnUpdateUIDeleteButton(wxUpdateUIEvent
& event
)
532 event
.Enable(m_textDelete
->GetValue().ToULong(&n
) &&
533 (n
< (unsigned)m_combobox
->GetCount()));
537 void ComboboxWidgetsPage::OnUpdateUIDeleteSelButton(wxUpdateUIEvent
& event
)
540 event
.Enable(m_combobox
->GetSelection() != wxNOT_FOUND
);
543 void ComboboxWidgetsPage::OnUpdateUIClearButton(wxUpdateUIEvent
& event
)
546 event
.Enable(m_combobox
->GetCount() != 0);
549 void ComboboxWidgetsPage::OnUpdateUIAddSeveral(wxUpdateUIEvent
& event
)
552 event
.Enable(!(m_combobox
->GetWindowStyle() & wxCB_SORT
));
555 void ComboboxWidgetsPage::OnComboText(wxCommandEvent
& event
)
560 wxString s
= event
.GetString();
562 wxASSERT_MSG( s
== m_combobox
->GetValue(),
563 _T("event and combobox values should be the same") );
565 if (event
.GetEventType() == wxEVT_COMMAND_TEXT_ENTER
)
566 wxLogMessage(_T("Combobox enter pressed (now '%s')"), s
.c_str());
568 wxLogMessage(_T("Combobox text changed (now '%s')"), s
.c_str());
571 void ComboboxWidgetsPage::OnComboBox(wxCommandEvent
& event
)
573 long sel
= event
.GetInt();
574 m_textDelete
->SetValue(wxString::Format(_T("%ld"), sel
));
576 wxLogMessage(_T("Combobox item %ld selected"), sel
);
578 wxLogMessage(_T("Combobox GetValue(): %s"), m_combobox
->GetValue().c_str() );
581 void ComboboxWidgetsPage::OnCheckOrRadioBox(wxCommandEvent
& WXUNUSED(event
))
586 #endif //wxUSE_COMBOBOX