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
; }
91 virtual void RecreateWidget() { CreateCombo(); }
95 void OnButtonReset(wxCommandEvent
& event
);
96 void OnButtonChange(wxCommandEvent
& event
);
97 void OnButtonDelete(wxCommandEvent
& event
);
98 void OnButtonDeleteSel(wxCommandEvent
& event
);
99 void OnButtonClear(wxCommandEvent
& event
);
100 void OnButtonInsert(wxCommandEvent
&event
);
101 void OnButtonAdd(wxCommandEvent
& event
);
102 void OnButtonAddSeveral(wxCommandEvent
& event
);
103 void OnButtonAddMany(wxCommandEvent
& event
);
105 void OnComboBox(wxCommandEvent
& event
);
106 void OnComboText(wxCommandEvent
& event
);
108 void OnCheckOrRadioBox(wxCommandEvent
& event
);
110 void OnUpdateUICurText(wxUpdateUIEvent
& event
);
111 void OnUpdateUIInsertionPointText(wxUpdateUIEvent
& event
);
113 void OnUpdateUIInsert(wxUpdateUIEvent
& event
);
114 void OnUpdateUIAddSeveral(wxUpdateUIEvent
& event
);
115 void OnUpdateUIClearButton(wxUpdateUIEvent
& event
);
116 void OnUpdateUIDeleteButton(wxUpdateUIEvent
& event
);
117 void OnUpdateUIDeleteSelButton(wxUpdateUIEvent
& event
);
118 void OnUpdateUIResetButton(wxUpdateUIEvent
& event
);
120 // reset the combobox parameters
123 // (re)create the combobox
129 // the sel mode radiobox
130 wxRadioBox
*m_radioKind
;
132 // the checkboxes for styles
133 wxCheckBox
*m_chkSort
,
136 // the combobox itself and the sizer it is in
137 wxComboBox
*m_combobox
;
138 wxSizer
*m_sizerCombo
;
140 // the text entries for "Add/change string" and "Delete" buttons
141 wxTextCtrl
*m_textInsert
,
147 DECLARE_EVENT_TABLE()
148 DECLARE_WIDGETS_PAGE(ComboboxWidgetsPage
)
151 // ----------------------------------------------------------------------------
153 // ----------------------------------------------------------------------------
155 BEGIN_EVENT_TABLE(ComboboxWidgetsPage
, WidgetsPage
)
156 EVT_BUTTON(ComboPage_Reset
, ComboboxWidgetsPage::OnButtonReset
)
157 EVT_BUTTON(ComboPage_Change
, ComboboxWidgetsPage::OnButtonChange
)
158 EVT_BUTTON(ComboPage_Delete
, ComboboxWidgetsPage::OnButtonDelete
)
159 EVT_BUTTON(ComboPage_DeleteSel
, ComboboxWidgetsPage::OnButtonDeleteSel
)
160 EVT_BUTTON(ComboPage_Clear
, ComboboxWidgetsPage::OnButtonClear
)
161 EVT_BUTTON(ComboPage_Insert
, ComboboxWidgetsPage::OnButtonInsert
)
162 EVT_BUTTON(ComboPage_Add
, ComboboxWidgetsPage::OnButtonAdd
)
163 EVT_BUTTON(ComboPage_AddSeveral
, ComboboxWidgetsPage::OnButtonAddSeveral
)
164 EVT_BUTTON(ComboPage_AddMany
, ComboboxWidgetsPage::OnButtonAddMany
)
166 EVT_TEXT_ENTER(ComboPage_InsertText
, ComboboxWidgetsPage::OnButtonInsert
)
167 EVT_TEXT_ENTER(ComboPage_AddText
, ComboboxWidgetsPage::OnButtonAdd
)
168 EVT_TEXT_ENTER(ComboPage_DeleteText
, ComboboxWidgetsPage::OnButtonDelete
)
170 EVT_UPDATE_UI(ComboPage_CurText
, ComboboxWidgetsPage::OnUpdateUICurText
)
171 EVT_UPDATE_UI(ComboPage_InsertionPointText
, ComboboxWidgetsPage::OnUpdateUIInsertionPointText
)
173 EVT_UPDATE_UI(ComboPage_Reset
, ComboboxWidgetsPage::OnUpdateUIResetButton
)
174 EVT_UPDATE_UI(ComboPage_Insert
, ComboboxWidgetsPage::OnUpdateUIInsert
)
175 EVT_UPDATE_UI(ComboPage_AddSeveral
, ComboboxWidgetsPage::OnUpdateUIAddSeveral
)
176 EVT_UPDATE_UI(ComboPage_Clear
, ComboboxWidgetsPage::OnUpdateUIClearButton
)
177 EVT_UPDATE_UI(ComboPage_DeleteText
, ComboboxWidgetsPage::OnUpdateUIClearButton
)
178 EVT_UPDATE_UI(ComboPage_Delete
, ComboboxWidgetsPage::OnUpdateUIDeleteButton
)
179 EVT_UPDATE_UI(ComboPage_Change
, ComboboxWidgetsPage::OnUpdateUIDeleteSelButton
)
180 EVT_UPDATE_UI(ComboPage_ChangeText
, ComboboxWidgetsPage::OnUpdateUIDeleteSelButton
)
181 EVT_UPDATE_UI(ComboPage_DeleteSel
, ComboboxWidgetsPage::OnUpdateUIDeleteSelButton
)
183 EVT_COMBOBOX(ComboPage_Combo
, ComboboxWidgetsPage::OnComboBox
)
184 EVT_TEXT(ComboPage_Combo
, ComboboxWidgetsPage::OnComboText
)
185 EVT_TEXT_ENTER(ComboPage_Combo
, ComboboxWidgetsPage::OnComboText
)
187 EVT_CHECKBOX(wxID_ANY
, ComboboxWidgetsPage::OnCheckOrRadioBox
)
188 EVT_RADIOBOX(wxID_ANY
, ComboboxWidgetsPage::OnCheckOrRadioBox
)
191 // ============================================================================
193 // ============================================================================
195 IMPLEMENT_WIDGETS_PAGE(ComboboxWidgetsPage
, _T("Combobox"));
197 ComboboxWidgetsPage::ComboboxWidgetsPage(wxBookCtrlBase
*book
,
198 wxImageList
*imaglist
)
203 m_chkReadonly
= (wxCheckBox
*)NULL
;
205 m_combobox
= (wxComboBox
*)NULL
;
206 m_sizerCombo
= (wxSizer
*)NULL
;
208 imaglist
->Add(wxBitmap(combobox_xpm
));
211 What we create here is a frame having 3 panes: style pane is the
212 leftmost one, in the middle the pane with buttons allowing to perform
213 miscellaneous combobox operations and the pane containing the combobox
216 wxSizer
*sizerTop
= new wxBoxSizer(wxHORIZONTAL
);
219 wxStaticBox
*box
= new wxStaticBox(this, wxID_ANY
, _T("&Set style"));
221 // should be in sync with ComboKind_XXX values
222 static const wxString kinds
[] =
229 m_radioKind
= new wxRadioBox(this, wxID_ANY
, _T("Combobox &kind:"),
230 wxDefaultPosition
, wxDefaultSize
,
231 WXSIZEOF(kinds
), kinds
,
232 1, wxRA_SPECIFY_COLS
);
234 wxSizer
*sizerLeft
= new wxStaticBoxSizer(box
, wxVERTICAL
);
236 m_chkSort
= CreateCheckBoxAndAddToSizer(sizerLeft
, _T("&Sort items"));
237 m_chkReadonly
= CreateCheckBoxAndAddToSizer(sizerLeft
, _T("&Read only"));
239 sizerLeft
->Add(5, 5, 0, wxGROW
| wxALL
, 5); // spacer
240 sizerLeft
->Add(m_radioKind
, 0, wxGROW
| wxALL
, 5);
242 wxButton
*btn
= new wxButton(this, ComboPage_Reset
, _T("&Reset"));
243 sizerLeft
->Add(btn
, 0, wxALIGN_CENTRE_HORIZONTAL
| wxALL
, 15);
246 wxStaticBox
*box2
= new wxStaticBox(this, wxID_ANY
,
247 _T("&Change combobox contents"));
248 wxSizer
*sizerMiddle
= new wxStaticBoxSizer(box2
, wxVERTICAL
);
253 sizerRow
= CreateSizerWithTextAndLabel(_T("Current selection"),
256 text
->SetEditable(false);
258 sizerMiddle
->Add(sizerRow
, 0, wxALL
| wxGROW
, 5);
260 sizerRow
= CreateSizerWithTextAndLabel(_T("Insertion Point"),
261 ComboPage_InsertionPointText
,
263 text
->SetEditable(false);
265 sizerMiddle
->Add(sizerRow
, 0, wxALL
| wxGROW
, 5);
267 sizerRow
= CreateSizerWithTextAndButton(ComboPage_Insert
,
268 _T("&Insert this string"),
269 ComboPage_InsertText
,
271 sizerMiddle
->Add(sizerRow
, 0, wxALL
| wxGROW
, 5);
273 sizerRow
= CreateSizerWithTextAndButton(ComboPage_Add
,
274 _T("&Add this string"),
277 sizerMiddle
->Add(sizerRow
, 0, wxALL
| wxGROW
, 5);
279 btn
= new wxButton(this, ComboPage_AddSeveral
, _T("&Append a few strings"));
280 sizerMiddle
->Add(btn
, 0, wxALL
| wxGROW
, 5);
282 btn
= new wxButton(this, ComboPage_AddMany
, _T("Append &many strings"));
283 sizerMiddle
->Add(btn
, 0, wxALL
| wxGROW
, 5);
285 sizerRow
= CreateSizerWithTextAndButton(ComboPage_Change
,
286 _T("C&hange current"),
287 ComboPage_ChangeText
,
289 sizerMiddle
->Add(sizerRow
, 0, wxALL
| wxGROW
, 5);
291 sizerRow
= CreateSizerWithTextAndButton(ComboPage_Delete
,
292 _T("&Delete this item"),
293 ComboPage_DeleteText
,
295 sizerMiddle
->Add(sizerRow
, 0, wxALL
| wxGROW
, 5);
297 btn
= new wxButton(this, ComboPage_DeleteSel
, _T("Delete &selection"));
298 sizerMiddle
->Add(btn
, 0, wxALL
| wxGROW
, 5);
300 btn
= new wxButton(this, ComboPage_Clear
, _T("&Clear"));
301 sizerMiddle
->Add(btn
, 0, wxALL
| wxGROW
, 5);
304 wxSizer
*sizerRight
= new wxBoxSizer(wxVERTICAL
);
305 m_combobox
= new wxComboBox(this, ComboPage_Combo
, wxEmptyString
,
306 wxDefaultPosition
, wxDefaultSize
,
309 sizerRight
->Add(m_combobox
, 1, wxGROW
| wxALL
, 5);
310 sizerRight
->SetMinSize(150, 0);
311 m_sizerCombo
= sizerRight
; // save it to modify it later
313 // the 3 panes panes compose the window
314 sizerTop
->Add(sizerLeft
, 0, wxGROW
| (wxALL
& ~wxLEFT
), 10);
315 sizerTop
->Add(sizerMiddle
, 1, wxGROW
| wxALL
, 10);
316 sizerTop
->Add(sizerRight
, 1, wxGROW
| (wxALL
& ~wxRIGHT
), 10);
318 // final initializations
326 // ----------------------------------------------------------------------------
328 // ----------------------------------------------------------------------------
330 void ComboboxWidgetsPage::Reset()
332 m_chkSort
->SetValue(false);
333 m_chkReadonly
->SetValue(false);
336 void ComboboxWidgetsPage::CreateCombo()
338 int flags
= ms_defaultFlags
;
340 if ( m_chkSort
->GetValue() )
342 if ( m_chkReadonly
->GetValue() )
343 flags
|= wxCB_READONLY
;
345 switch ( m_radioKind
->GetSelection() )
348 wxFAIL_MSG( _T("unknown combo kind") );
351 case ComboKind_Default
:
354 case ComboKind_Simple
:
355 flags
|= wxCB_SIMPLE
;
358 case ComboKind_DropDown
:
359 flags
= wxCB_DROPDOWN
;
366 unsigned int count
= m_combobox
->GetCount();
367 for ( unsigned int n
= 0; n
< count
; n
++ )
369 items
.Add(m_combobox
->GetString(n
));
372 m_sizerCombo
->Detach( m_combobox
);
376 m_combobox
= new wxComboBox(this, ComboPage_Combo
, wxEmptyString
,
377 wxDefaultPosition
, wxDefaultSize
,
381 unsigned int count
= items
.GetCount();
382 for ( unsigned int n
= 0; n
< count
; n
++ )
384 m_combobox
->Append(items
[n
]);
387 m_sizerCombo
->Add(m_combobox
, 1, wxGROW
| wxALL
, 5);
388 m_sizerCombo
->Layout();
391 // ----------------------------------------------------------------------------
393 // ----------------------------------------------------------------------------
395 void ComboboxWidgetsPage::OnButtonReset(wxCommandEvent
& WXUNUSED(event
))
402 void ComboboxWidgetsPage::OnButtonChange(wxCommandEvent
& WXUNUSED(event
))
404 int sel
= m_combobox
->GetSelection();
408 m_combobox
->SetString(sel
, m_textChange
->GetValue());
410 wxLogMessage(_T("Not implemented in wxGTK"));
415 void ComboboxWidgetsPage::OnButtonDelete(wxCommandEvent
& WXUNUSED(event
))
418 if ( !m_textDelete
->GetValue().ToULong(&n
) ||
419 (n
>= m_combobox
->GetCount()) )
424 m_combobox
->Delete(n
);
427 void ComboboxWidgetsPage::OnButtonDeleteSel(wxCommandEvent
& WXUNUSED(event
))
429 int sel
= m_combobox
->GetSelection();
430 if ( sel
!= wxNOT_FOUND
)
432 m_combobox
->Delete(sel
);
436 void ComboboxWidgetsPage::OnButtonClear(wxCommandEvent
& WXUNUSED(event
))
441 void ComboboxWidgetsPage::OnButtonInsert(wxCommandEvent
& WXUNUSED(event
))
443 static unsigned int s_item
= 0;
445 wxString s
= m_textInsert
->GetValue();
446 if ( !m_textInsert
->IsModified() )
448 // update the default string
449 m_textInsert
->SetValue(wxString::Format(_T("test item %u"), ++s_item
));
452 if (m_combobox
->GetSelection() >= 0)
453 m_combobox
->Insert(s
, m_combobox
->GetSelection());
456 void ComboboxWidgetsPage::OnButtonAdd(wxCommandEvent
& WXUNUSED(event
))
458 static unsigned int s_item
= 0;
460 wxString s
= m_textAdd
->GetValue();
461 if ( !m_textAdd
->IsModified() )
463 // update the default string
464 m_textAdd
->SetValue(wxString::Format(_T("test item %u"), ++s_item
));
467 m_combobox
->Append(s
);
470 void ComboboxWidgetsPage::OnButtonAddMany(wxCommandEvent
& WXUNUSED(event
))
472 // "many" means 1000 here
473 for ( unsigned int n
= 0; n
< 1000; n
++ )
475 m_combobox
->Append(wxString::Format(_T("item #%u"), n
));
479 void ComboboxWidgetsPage::OnButtonAddSeveral(wxCommandEvent
& WXUNUSED(event
))
481 m_combobox
->Append(_T("First"));
482 m_combobox
->Append(_T("another one"));
483 m_combobox
->Append(_T("and the last (very very very very very very very very very very long) one"));
486 void ComboboxWidgetsPage::OnUpdateUICurText(wxUpdateUIEvent
& event
)
489 event
.SetText( wxString::Format(_T("%d"), m_combobox
->GetSelection()) );
492 void ComboboxWidgetsPage::OnUpdateUIInsertionPointText(wxUpdateUIEvent
& event
)
495 event
.SetText( wxString::Format(_T("%ld"), m_combobox
->GetInsertionPoint()) );
498 void ComboboxWidgetsPage::OnUpdateUIResetButton(wxUpdateUIEvent
& event
)
501 event
.Enable( m_chkSort
->GetValue() || m_chkReadonly
->GetValue() );
504 void ComboboxWidgetsPage::OnUpdateUIInsert(wxUpdateUIEvent
& event
)
508 bool enable
= !(m_combobox
->GetWindowStyle() & wxCB_SORT
) &&
509 (m_combobox
->GetSelection() >= 0);
511 event
.Enable(enable
);
515 void ComboboxWidgetsPage::OnUpdateUIDeleteButton(wxUpdateUIEvent
& event
)
520 event
.Enable(m_textDelete
->GetValue().ToULong(&n
) &&
521 (n
< (unsigned)m_combobox
->GetCount()));
525 void ComboboxWidgetsPage::OnUpdateUIDeleteSelButton(wxUpdateUIEvent
& event
)
528 event
.Enable(m_combobox
->GetSelection() != -1);
531 void ComboboxWidgetsPage::OnUpdateUIClearButton(wxUpdateUIEvent
& event
)
534 event
.Enable(m_combobox
->GetCount() != 0);
537 void ComboboxWidgetsPage::OnUpdateUIAddSeveral(wxUpdateUIEvent
& event
)
540 event
.Enable(!(m_combobox
->GetWindowStyle() & wxCB_SORT
));
543 void ComboboxWidgetsPage::OnComboText(wxCommandEvent
& event
)
548 wxString s
= event
.GetString();
550 wxASSERT_MSG( s
== m_combobox
->GetValue(),
551 _T("event and combobox values should be the same") );
553 if (event
.GetEventType() == wxEVT_COMMAND_TEXT_ENTER
)
554 wxLogMessage(_T("Combobox enter pressed (now '%s')"), s
.c_str());
556 wxLogMessage(_T("Combobox text changed (now '%s')"), s
.c_str());
559 void ComboboxWidgetsPage::OnComboBox(wxCommandEvent
& event
)
561 long sel
= event
.GetInt();
562 m_textDelete
->SetValue(wxString::Format(_T("%ld"), sel
));
564 wxLogMessage(_T("Combobox item %ld selected"), sel
);
566 wxLogMessage(_T("Combobox GetValue(): %s"), m_combobox
->GetValue().c_str() );
569 void ComboboxWidgetsPage::OnCheckOrRadioBox(wxCommandEvent
& WXUNUSED(event
))
574 #endif //wxUSE_COMBOBOX