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
= 100,
57 ComboPage_InsertionPointText
,
73 // kinds of comboboxes
81 // ----------------------------------------------------------------------------
82 // ComboboxWidgetsPage
83 // ----------------------------------------------------------------------------
85 class ComboboxWidgetsPage
: public WidgetsPage
88 ComboboxWidgetsPage(wxBookCtrlBase
*book
, wxImageList
*imaglist
);
90 virtual wxControl
*GetWidget() const { return m_combobox
; }
94 void OnButtonReset(wxCommandEvent
& event
);
95 void OnButtonChange(wxCommandEvent
& event
);
96 void OnButtonDelete(wxCommandEvent
& event
);
97 void OnButtonDeleteSel(wxCommandEvent
& event
);
98 void OnButtonClear(wxCommandEvent
& event
);
99 void OnButtonInsert(wxCommandEvent
&event
);
100 void OnButtonAdd(wxCommandEvent
& event
);
101 void OnButtonAddSeveral(wxCommandEvent
& event
);
102 void OnButtonAddMany(wxCommandEvent
& event
);
104 void OnComboBox(wxCommandEvent
& event
);
105 void OnComboText(wxCommandEvent
& event
);
107 void OnCheckOrRadioBox(wxCommandEvent
& event
);
109 void OnUpdateUICurText(wxUpdateUIEvent
& event
);
110 void OnUpdateUIInsertionPointText(wxUpdateUIEvent
& event
);
112 void OnUpdateUIInsert(wxUpdateUIEvent
& event
);
113 void OnUpdateUIAddSeveral(wxUpdateUIEvent
& event
);
114 void OnUpdateUIClearButton(wxUpdateUIEvent
& event
);
115 void OnUpdateUIDeleteButton(wxUpdateUIEvent
& event
);
116 void OnUpdateUIDeleteSelButton(wxUpdateUIEvent
& event
);
117 void OnUpdateUIResetButton(wxUpdateUIEvent
& event
);
119 // reset the combobox parameters
122 // (re)create the combobox
128 // the sel mode radiobox
129 wxRadioBox
*m_radioKind
;
131 // the checkboxes for styles
132 wxCheckBox
*m_chkSort
,
135 // the combobox itself and the sizer it is in
136 wxComboBox
*m_combobox
;
137 wxSizer
*m_sizerCombo
;
139 // the text entries for "Add/change string" and "Delete" buttons
140 wxTextCtrl
*m_textInsert
,
146 DECLARE_EVENT_TABLE()
147 DECLARE_WIDGETS_PAGE(ComboboxWidgetsPage
)
150 // ----------------------------------------------------------------------------
152 // ----------------------------------------------------------------------------
154 BEGIN_EVENT_TABLE(ComboboxWidgetsPage
, WidgetsPage
)
155 EVT_BUTTON(ComboPage_Reset
, ComboboxWidgetsPage::OnButtonReset
)
156 EVT_BUTTON(ComboPage_Change
, ComboboxWidgetsPage::OnButtonChange
)
157 EVT_BUTTON(ComboPage_Delete
, ComboboxWidgetsPage::OnButtonDelete
)
158 EVT_BUTTON(ComboPage_DeleteSel
, ComboboxWidgetsPage::OnButtonDeleteSel
)
159 EVT_BUTTON(ComboPage_Clear
, ComboboxWidgetsPage::OnButtonClear
)
160 EVT_BUTTON(ComboPage_Insert
, ComboboxWidgetsPage::OnButtonInsert
)
161 EVT_BUTTON(ComboPage_Add
, ComboboxWidgetsPage::OnButtonAdd
)
162 EVT_BUTTON(ComboPage_AddSeveral
, ComboboxWidgetsPage::OnButtonAddSeveral
)
163 EVT_BUTTON(ComboPage_AddMany
, ComboboxWidgetsPage::OnButtonAddMany
)
165 EVT_TEXT_ENTER(ComboPage_InsertText
, ComboboxWidgetsPage::OnButtonInsert
)
166 EVT_TEXT_ENTER(ComboPage_AddText
, ComboboxWidgetsPage::OnButtonAdd
)
167 EVT_TEXT_ENTER(ComboPage_DeleteText
, ComboboxWidgetsPage::OnButtonDelete
)
169 EVT_UPDATE_UI(ComboPage_CurText
, ComboboxWidgetsPage::OnUpdateUICurText
)
170 EVT_UPDATE_UI(ComboPage_InsertionPointText
, ComboboxWidgetsPage::OnUpdateUIInsertionPointText
)
172 EVT_UPDATE_UI(ComboPage_Reset
, ComboboxWidgetsPage::OnUpdateUIResetButton
)
173 EVT_UPDATE_UI(ComboPage_Insert
, ComboboxWidgetsPage::OnUpdateUIInsert
)
174 EVT_UPDATE_UI(ComboPage_AddSeveral
, ComboboxWidgetsPage::OnUpdateUIAddSeveral
)
175 EVT_UPDATE_UI(ComboPage_Clear
, ComboboxWidgetsPage::OnUpdateUIClearButton
)
176 EVT_UPDATE_UI(ComboPage_DeleteText
, ComboboxWidgetsPage::OnUpdateUIClearButton
)
177 EVT_UPDATE_UI(ComboPage_Delete
, ComboboxWidgetsPage::OnUpdateUIDeleteButton
)
178 EVT_UPDATE_UI(ComboPage_Change
, ComboboxWidgetsPage::OnUpdateUIDeleteSelButton
)
179 EVT_UPDATE_UI(ComboPage_ChangeText
, ComboboxWidgetsPage::OnUpdateUIDeleteSelButton
)
180 EVT_UPDATE_UI(ComboPage_DeleteSel
, ComboboxWidgetsPage::OnUpdateUIDeleteSelButton
)
182 EVT_COMBOBOX(ComboPage_Combo
, ComboboxWidgetsPage::OnComboBox
)
183 EVT_TEXT(ComboPage_Combo
, ComboboxWidgetsPage::OnComboText
)
184 EVT_TEXT_ENTER(ComboPage_Combo
, ComboboxWidgetsPage::OnComboText
)
186 EVT_CHECKBOX(wxID_ANY
, ComboboxWidgetsPage::OnCheckOrRadioBox
)
187 EVT_RADIOBOX(wxID_ANY
, ComboboxWidgetsPage::OnCheckOrRadioBox
)
190 // ============================================================================
192 // ============================================================================
194 IMPLEMENT_WIDGETS_PAGE(ComboboxWidgetsPage
, _T("Combobox"));
196 ComboboxWidgetsPage::ComboboxWidgetsPage(wxBookCtrlBase
*book
,
197 wxImageList
*imaglist
)
202 m_chkReadonly
= (wxCheckBox
*)NULL
;
204 m_combobox
= (wxComboBox
*)NULL
;
205 m_sizerCombo
= (wxSizer
*)NULL
;
207 imaglist
->Add(wxBitmap(combobox_xpm
));
210 What we create here is a frame having 3 panes: style pane is the
211 leftmost one, in the middle the pane with buttons allowing to perform
212 miscellaneous combobox operations and the pane containing the combobox
215 wxSizer
*sizerTop
= new wxBoxSizer(wxHORIZONTAL
);
218 wxStaticBox
*box
= new wxStaticBox(this, wxID_ANY
, _T("&Set style"));
220 // should be in sync with ComboKind_XXX values
221 static const wxString kinds
[] =
228 m_radioKind
= new wxRadioBox(this, wxID_ANY
, _T("Combobox &kind:"),
229 wxDefaultPosition
, wxDefaultSize
,
230 WXSIZEOF(kinds
), kinds
,
231 1, wxRA_SPECIFY_COLS
);
233 wxSizer
*sizerLeft
= new wxStaticBoxSizer(box
, wxVERTICAL
);
235 m_chkSort
= CreateCheckBoxAndAddToSizer(sizerLeft
, _T("&Sort items"));
236 m_chkReadonly
= CreateCheckBoxAndAddToSizer(sizerLeft
, _T("&Read only"));
238 sizerLeft
->Add(5, 5, 0, wxGROW
| wxALL
, 5); // spacer
239 sizerLeft
->Add(m_radioKind
, 0, wxGROW
| wxALL
, 5);
241 wxButton
*btn
= new wxButton(this, ComboPage_Reset
, _T("&Reset"));
242 sizerLeft
->Add(btn
, 0, wxALIGN_CENTRE_HORIZONTAL
| wxALL
, 15);
245 wxStaticBox
*box2
= new wxStaticBox(this, wxID_ANY
,
246 _T("&Change combobox contents"));
247 wxSizer
*sizerMiddle
= new wxStaticBoxSizer(box2
, wxVERTICAL
);
252 sizerRow
= CreateSizerWithTextAndLabel(_T("Current selection"),
255 text
->SetEditable(false);
257 sizerMiddle
->Add(sizerRow
, 0, wxALL
| wxGROW
, 5);
259 sizerRow
= CreateSizerWithTextAndLabel(_T("Insertion Point"),
260 ComboPage_InsertionPointText
,
262 text
->SetEditable(false);
264 sizerMiddle
->Add(sizerRow
, 0, wxALL
| wxGROW
, 5);
266 sizerRow
= CreateSizerWithTextAndButton(ComboPage_Insert
,
267 _T("&Insert this string"),
268 ComboPage_InsertText
,
270 sizerMiddle
->Add(sizerRow
, 0, wxALL
| wxGROW
, 5);
272 sizerRow
= CreateSizerWithTextAndButton(ComboPage_Add
,
273 _T("&Add this string"),
276 sizerMiddle
->Add(sizerRow
, 0, wxALL
| wxGROW
, 5);
278 btn
= new wxButton(this, ComboPage_AddSeveral
, _T("&Append a few strings"));
279 sizerMiddle
->Add(btn
, 0, wxALL
| wxGROW
, 5);
281 btn
= new wxButton(this, ComboPage_AddMany
, _T("Append &many strings"));
282 sizerMiddle
->Add(btn
, 0, wxALL
| wxGROW
, 5);
284 sizerRow
= CreateSizerWithTextAndButton(ComboPage_Change
,
285 _T("C&hange current"),
286 ComboPage_ChangeText
,
288 sizerMiddle
->Add(sizerRow
, 0, wxALL
| wxGROW
, 5);
290 sizerRow
= CreateSizerWithTextAndButton(ComboPage_Delete
,
291 _T("&Delete this item"),
292 ComboPage_DeleteText
,
294 sizerMiddle
->Add(sizerRow
, 0, wxALL
| wxGROW
, 5);
296 btn
= new wxButton(this, ComboPage_DeleteSel
, _T("Delete &selection"));
297 sizerMiddle
->Add(btn
, 0, wxALL
| wxGROW
, 5);
299 btn
= new wxButton(this, ComboPage_Clear
, _T("&Clear"));
300 sizerMiddle
->Add(btn
, 0, wxALL
| wxGROW
, 5);
303 wxSizer
*sizerRight
= new wxBoxSizer(wxVERTICAL
);
304 m_combobox
= new wxComboBox(this, ComboPage_Combo
, wxEmptyString
,
305 wxDefaultPosition
, wxDefaultSize
,
308 sizerRight
->Add(m_combobox
, 1, wxGROW
| wxALL
, 5);
309 sizerRight
->SetMinSize(150, 0);
310 m_sizerCombo
= sizerRight
; // save it to modify it later
312 // the 3 panes panes compose the window
313 sizerTop
->Add(sizerLeft
, 0, wxGROW
| (wxALL
& ~wxLEFT
), 10);
314 sizerTop
->Add(sizerMiddle
, 1, wxGROW
| wxALL
, 10);
315 sizerTop
->Add(sizerRight
, 1, wxGROW
| (wxALL
& ~wxRIGHT
), 10);
317 // final initializations
325 // ----------------------------------------------------------------------------
327 // ----------------------------------------------------------------------------
329 void ComboboxWidgetsPage::Reset()
331 m_chkSort
->SetValue(false);
332 m_chkReadonly
->SetValue(false);
335 void ComboboxWidgetsPage::CreateCombo()
339 if ( m_chkSort
->GetValue() )
341 if ( m_chkReadonly
->GetValue() )
342 flags
|= wxCB_READONLY
;
344 switch ( m_radioKind
->GetSelection() )
347 wxFAIL_MSG( _T("unknown combo kind") );
350 case ComboKind_Default
:
353 case ComboKind_Simple
:
354 flags
|= wxCB_SIMPLE
;
357 case ComboKind_DropDown
:
358 flags
= wxCB_DROPDOWN
;
365 unsigned int count
= m_combobox
->GetCount();
366 for ( unsigned int n
= 0; n
< count
; n
++ )
368 items
.Add(m_combobox
->GetString(n
));
371 m_sizerCombo
->Detach( m_combobox
);
375 m_combobox
= new wxComboBox(this, ComboPage_Combo
, wxEmptyString
,
376 wxDefaultPosition
, wxDefaultSize
,
380 unsigned int count
= items
.GetCount();
381 for ( unsigned int n
= 0; n
< count
; n
++ )
383 m_combobox
->Append(items
[n
]);
386 m_sizerCombo
->Add(m_combobox
, 1, wxGROW
| wxALL
, 5);
387 m_sizerCombo
->Layout();
390 // ----------------------------------------------------------------------------
392 // ----------------------------------------------------------------------------
394 void ComboboxWidgetsPage::OnButtonReset(wxCommandEvent
& WXUNUSED(event
))
401 void ComboboxWidgetsPage::OnButtonChange(wxCommandEvent
& WXUNUSED(event
))
403 int sel
= m_combobox
->GetSelection();
407 m_combobox
->SetString(sel
, m_textChange
->GetValue());
409 wxLogMessage(_T("Not implemented in wxGTK"));
414 void ComboboxWidgetsPage::OnButtonDelete(wxCommandEvent
& WXUNUSED(event
))
417 if ( !m_textDelete
->GetValue().ToULong(&n
) ||
418 (n
>= m_combobox
->GetCount()) )
423 m_combobox
->Delete(n
);
426 void ComboboxWidgetsPage::OnButtonDeleteSel(wxCommandEvent
& WXUNUSED(event
))
428 int sel
= m_combobox
->GetSelection();
429 if ( sel
!= wxNOT_FOUND
)
431 m_combobox
->Delete(sel
);
435 void ComboboxWidgetsPage::OnButtonClear(wxCommandEvent
& WXUNUSED(event
))
440 void ComboboxWidgetsPage::OnButtonInsert(wxCommandEvent
& WXUNUSED(event
))
442 static unsigned int s_item
= 0;
444 wxString s
= m_textInsert
->GetValue();
445 if ( !m_textInsert
->IsModified() )
447 // update the default string
448 m_textInsert
->SetValue(wxString::Format(_T("test item %u"), ++s_item
));
451 if (m_combobox
->GetSelection() >= 0)
452 m_combobox
->Insert(s
, m_combobox
->GetSelection());
455 void ComboboxWidgetsPage::OnButtonAdd(wxCommandEvent
& WXUNUSED(event
))
457 static unsigned int s_item
= 0;
459 wxString s
= m_textAdd
->GetValue();
460 if ( !m_textAdd
->IsModified() )
462 // update the default string
463 m_textAdd
->SetValue(wxString::Format(_T("test item %u"), ++s_item
));
466 m_combobox
->Append(s
);
469 void ComboboxWidgetsPage::OnButtonAddMany(wxCommandEvent
& WXUNUSED(event
))
471 // "many" means 1000 here
472 for ( unsigned int n
= 0; n
< 1000; n
++ )
474 m_combobox
->Append(wxString::Format(_T("item #%u"), n
));
478 void ComboboxWidgetsPage::OnButtonAddSeveral(wxCommandEvent
& WXUNUSED(event
))
480 m_combobox
->Append(_T("First"));
481 m_combobox
->Append(_T("another one"));
482 m_combobox
->Append(_T("and the last (very very very very very very very very very very long) one"));
485 void ComboboxWidgetsPage::OnUpdateUICurText(wxUpdateUIEvent
& event
)
488 event
.SetText( wxString::Format(_T("%d"), m_combobox
->GetSelection()) );
491 void ComboboxWidgetsPage::OnUpdateUIInsertionPointText(wxUpdateUIEvent
& event
)
494 event
.SetText( wxString::Format(_T("%ld"), m_combobox
->GetInsertionPoint()) );
497 void ComboboxWidgetsPage::OnUpdateUIResetButton(wxUpdateUIEvent
& event
)
500 event
.Enable( m_chkSort
->GetValue() || m_chkReadonly
->GetValue() );
503 void ComboboxWidgetsPage::OnUpdateUIInsert(wxUpdateUIEvent
& event
)
507 bool enable
= !(m_combobox
->GetWindowStyle() & wxCB_SORT
) &&
508 (m_combobox
->GetSelection() >= 0);
510 event
.Enable(enable
);
514 void ComboboxWidgetsPage::OnUpdateUIDeleteButton(wxUpdateUIEvent
& event
)
519 event
.Enable(m_textDelete
->GetValue().ToULong(&n
) &&
520 (n
< (unsigned)m_combobox
->GetCount()));
524 void ComboboxWidgetsPage::OnUpdateUIDeleteSelButton(wxUpdateUIEvent
& event
)
527 event
.Enable(m_combobox
->GetSelection() != -1);
530 void ComboboxWidgetsPage::OnUpdateUIClearButton(wxUpdateUIEvent
& event
)
533 event
.Enable(m_combobox
->GetCount() != 0);
536 void ComboboxWidgetsPage::OnUpdateUIAddSeveral(wxUpdateUIEvent
& event
)
539 event
.Enable(!(m_combobox
->GetWindowStyle() & wxCB_SORT
));
542 void ComboboxWidgetsPage::OnComboText(wxCommandEvent
& event
)
547 wxString s
= event
.GetString();
549 wxASSERT_MSG( s
== m_combobox
->GetValue(),
550 _T("event and combobox values should be the same") );
552 if (event
.GetEventType() == wxEVT_COMMAND_TEXT_ENTER
)
553 wxLogMessage(_T("Combobox enter pressed (now '%s')"), s
.c_str());
555 wxLogMessage(_T("Combobox text changed (now '%s')"), s
.c_str());
558 void ComboboxWidgetsPage::OnComboBox(wxCommandEvent
& event
)
560 long sel
= event
.GetInt();
561 m_textDelete
->SetValue(wxString::Format(_T("%ld"), sel
));
563 wxLogMessage(_T("Combobox item %ld selected"), sel
);
565 wxLogMessage(_T("Combobox GetValue(): %s"), m_combobox
->GetValue().c_str() );
568 void ComboboxWidgetsPage::OnCheckOrRadioBox(wxCommandEvent
& WXUNUSED(event
))
573 #endif //wxUSE_COMBOBOX