1 /////////////////////////////////////////////////////////////////////////////
2 // Program: wxWidgets Widgets Sample
4 // Purpose: Part of the widgets sample showing wxListbox
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/listbox.h"
38 #include "wx/radiobox.h"
39 #include "wx/statbox.h"
40 #include "wx/textctrl.h"
45 #include "wx/checklst.h"
49 #include "icons/listbox.xpm"
51 // ----------------------------------------------------------------------------
53 // ----------------------------------------------------------------------------
58 ListboxPage_Reset
= 100,
61 ListboxPage_AddSeveral
,
65 ListboxPage_ChangeText
,
67 ListboxPage_DeleteText
,
68 ListboxPage_DeleteSel
,
72 // ----------------------------------------------------------------------------
74 // ----------------------------------------------------------------------------
76 class ListboxWidgetsPage
: public WidgetsPage
79 ListboxWidgetsPage(wxNotebook
*notebook
, wxImageList
*imaglist
);
83 void OnButtonReset(wxCommandEvent
& event
);
84 void OnButtonChange(wxCommandEvent
& event
);
85 void OnButtonDelete(wxCommandEvent
& event
);
86 void OnButtonDeleteSel(wxCommandEvent
& event
);
87 void OnButtonClear(wxCommandEvent
& event
);
88 void OnButtonAdd(wxCommandEvent
& event
);
89 void OnButtonAddSeveral(wxCommandEvent
& event
);
90 void OnButtonAddMany(wxCommandEvent
& event
);
92 void OnListbox(wxCommandEvent
& event
);
93 void OnListboxDClick(wxCommandEvent
& event
);
94 void OnCheckListbox(wxCommandEvent
& event
);
96 void OnCheckOrRadioBox(wxCommandEvent
& event
);
98 void OnUpdateUIAddSeveral(wxUpdateUIEvent
& event
);
99 void OnUpdateUIClearButton(wxUpdateUIEvent
& event
);
100 void OnUpdateUIDeleteButton(wxUpdateUIEvent
& event
);
101 void OnUpdateUIDeleteSelButton(wxUpdateUIEvent
& event
);
102 void OnUpdateUIResetButton(wxUpdateUIEvent
& event
);
104 // reset the listbox parameters
107 // (re)create the listbox
110 // listbox parameters
111 // ------------------
113 // the selection mode
121 // should it be sorted?
124 // should it have horz scroll/vert scrollbar permanently shown?
131 // the sel mode radiobox
132 wxRadioBox
*m_radioSelMode
;
135 wxCheckBox
*m_chkSort
,
140 // the listbox itself and the sizer it is in
142 wxSizer
*m_sizerLbox
;
144 // the text entries for "Add/change string" and "Delete" buttons
145 wxTextCtrl
*m_textAdd
,
150 DECLARE_EVENT_TABLE()
151 DECLARE_WIDGETS_PAGE(ListboxWidgetsPage
)
154 // ----------------------------------------------------------------------------
156 // ----------------------------------------------------------------------------
158 BEGIN_EVENT_TABLE(ListboxWidgetsPage
, WidgetsPage
)
159 EVT_BUTTON(ListboxPage_Reset
, ListboxWidgetsPage::OnButtonReset
)
160 EVT_BUTTON(ListboxPage_Change
, ListboxWidgetsPage::OnButtonChange
)
161 EVT_BUTTON(ListboxPage_Delete
, ListboxWidgetsPage::OnButtonDelete
)
162 EVT_BUTTON(ListboxPage_DeleteSel
, ListboxWidgetsPage::OnButtonDeleteSel
)
163 EVT_BUTTON(ListboxPage_Clear
, ListboxWidgetsPage::OnButtonClear
)
164 EVT_BUTTON(ListboxPage_Add
, ListboxWidgetsPage::OnButtonAdd
)
165 EVT_BUTTON(ListboxPage_AddSeveral
, ListboxWidgetsPage::OnButtonAddSeveral
)
166 EVT_BUTTON(ListboxPage_AddMany
, ListboxWidgetsPage::OnButtonAddMany
)
168 EVT_TEXT_ENTER(ListboxPage_AddText
, ListboxWidgetsPage::OnButtonAdd
)
169 EVT_TEXT_ENTER(ListboxPage_DeleteText
, ListboxWidgetsPage::OnButtonDelete
)
171 EVT_UPDATE_UI(ListboxPage_Reset
, ListboxWidgetsPage::OnUpdateUIResetButton
)
172 EVT_UPDATE_UI(ListboxPage_AddSeveral
, ListboxWidgetsPage::OnUpdateUIAddSeveral
)
173 EVT_UPDATE_UI(ListboxPage_Clear
, ListboxWidgetsPage::OnUpdateUIClearButton
)
174 EVT_UPDATE_UI(ListboxPage_DeleteText
, ListboxWidgetsPage::OnUpdateUIClearButton
)
175 EVT_UPDATE_UI(ListboxPage_Delete
, ListboxWidgetsPage::OnUpdateUIDeleteButton
)
176 EVT_UPDATE_UI(ListboxPage_Change
, ListboxWidgetsPage::OnUpdateUIDeleteSelButton
)
177 EVT_UPDATE_UI(ListboxPage_ChangeText
, ListboxWidgetsPage::OnUpdateUIDeleteSelButton
)
178 EVT_UPDATE_UI(ListboxPage_DeleteSel
, ListboxWidgetsPage::OnUpdateUIDeleteSelButton
)
180 EVT_LISTBOX(ListboxPage_Listbox
, ListboxWidgetsPage::OnListbox
)
181 EVT_LISTBOX_DCLICK(ListboxPage_Listbox
, ListboxWidgetsPage::OnListboxDClick
)
182 EVT_CHECKLISTBOX(ListboxPage_Listbox
, ListboxWidgetsPage::OnCheckListbox
)
184 EVT_CHECKBOX(wxID_ANY
, ListboxWidgetsPage::OnCheckOrRadioBox
)
185 EVT_RADIOBOX(wxID_ANY
, ListboxWidgetsPage::OnCheckOrRadioBox
)
188 // ============================================================================
190 // ============================================================================
192 IMPLEMENT_WIDGETS_PAGE(ListboxWidgetsPage
, _T("Listbox"));
194 ListboxWidgetsPage::ListboxWidgetsPage(wxNotebook
*notebook
,
195 wxImageList
*imaglist
)
196 : WidgetsPage(notebook
)
198 imaglist
->Add(wxBitmap(listbox_xpm
));
201 m_radioSelMode
= (wxRadioBox
*)NULL
;
206 m_chkSort
= (wxCheckBox
*)NULL
;
208 m_lbox
= (wxListBox
*)NULL
;
209 m_sizerLbox
= (wxSizer
*)NULL
;
212 What we create here is a frame having 3 panes: style pane is the
213 leftmost one, in the middle the pane with buttons allowing to perform
214 miscellaneous listbox operations and the pane containing the listbox
217 wxSizer
*sizerTop
= new wxBoxSizer(wxHORIZONTAL
);
220 wxStaticBox
*box
= new wxStaticBox(this, wxID_ANY
,
221 _T("&Set listbox parameters"));
222 wxSizer
*sizerLeft
= new wxStaticBoxSizer(box
, wxVERTICAL
);
224 static const wxString modes
[] =
231 m_radioSelMode
= new wxRadioBox(this, wxID_ANY
, _T("Selection &mode:"),
232 wxDefaultPosition
, wxDefaultSize
,
233 WXSIZEOF(modes
), modes
,
234 1, wxRA_SPECIFY_COLS
);
236 m_chkVScroll
= CreateCheckBoxAndAddToSizer
239 _T("Always show &vertical scrollbar")
241 m_chkHScroll
= CreateCheckBoxAndAddToSizer
244 _T("Show &horizontal scrollbar")
246 m_chkCheck
= CreateCheckBoxAndAddToSizer(sizerLeft
, _T("&Check list box"));
247 m_chkSort
= CreateCheckBoxAndAddToSizer(sizerLeft
, _T("&Sort items"));
249 sizerLeft
->Add(5, 5, 0, wxGROW
| wxALL
, 5); // spacer
250 sizerLeft
->Add(m_radioSelMode
, 0, wxGROW
| wxALL
, 5);
252 wxButton
*btn
= new wxButton(this, ListboxPage_Reset
, _T("&Reset"));
253 sizerLeft
->Add(btn
, 0, wxALIGN_CENTRE_HORIZONTAL
| wxALL
, 15);
256 wxStaticBox
*box2
= new wxStaticBox(this, wxID_ANY
,
257 _T("&Change listbox contents"));
258 wxSizer
*sizerMiddle
= new wxStaticBoxSizer(box2
, wxVERTICAL
);
260 wxSizer
*sizerRow
= new wxBoxSizer(wxHORIZONTAL
);
261 btn
= new wxButton(this, ListboxPage_Add
, _T("&Add this string"));
262 m_textAdd
= new wxTextCtrl(this, ListboxPage_AddText
, _T("test item 0"));
263 sizerRow
->Add(btn
, 0, wxRIGHT
, 5);
264 sizerRow
->Add(m_textAdd
, 1, wxLEFT
, 5);
265 sizerMiddle
->Add(sizerRow
, 0, wxALL
| wxGROW
, 5);
267 btn
= new wxButton(this, ListboxPage_AddSeveral
, _T("&Insert a few strings"));
268 sizerMiddle
->Add(btn
, 0, wxALL
| wxGROW
, 5);
270 btn
= new wxButton(this, ListboxPage_AddMany
, _T("Add &many strings"));
271 sizerMiddle
->Add(btn
, 0, wxALL
| wxGROW
, 5);
273 sizerRow
= new wxBoxSizer(wxHORIZONTAL
);
274 btn
= new wxButton(this, ListboxPage_Change
, _T("C&hange current"));
275 m_textChange
= new wxTextCtrl(this, ListboxPage_ChangeText
, wxEmptyString
);
276 sizerRow
->Add(btn
, 0, wxRIGHT
, 5);
277 sizerRow
->Add(m_textChange
, 1, wxLEFT
, 5);
278 sizerMiddle
->Add(sizerRow
, 0, wxALL
| wxGROW
, 5);
280 sizerRow
= new wxBoxSizer(wxHORIZONTAL
);
281 btn
= new wxButton(this, ListboxPage_Delete
, _T("&Delete this item"));
282 m_textDelete
= new wxTextCtrl(this, ListboxPage_DeleteText
, wxEmptyString
);
283 sizerRow
->Add(btn
, 0, wxRIGHT
, 5);
284 sizerRow
->Add(m_textDelete
, 1, wxLEFT
, 5);
285 sizerMiddle
->Add(sizerRow
, 0, wxALL
| wxGROW
, 5);
287 btn
= new wxButton(this, ListboxPage_DeleteSel
, _T("Delete &selection"));
288 sizerMiddle
->Add(btn
, 0, wxALL
| wxGROW
, 5);
290 btn
= new wxButton(this, ListboxPage_Clear
, _T("&Clear"));
291 sizerMiddle
->Add(btn
, 0, wxALL
| wxGROW
, 5);
294 wxSizer
*sizerRight
= new wxBoxSizer(wxVERTICAL
);
295 m_lbox
= new wxListBox(this, ListboxPage_Listbox
,
296 wxDefaultPosition
, wxDefaultSize
,
299 sizerRight
->Add(m_lbox
, 1, wxGROW
| wxALL
, 5);
300 sizerRight
->SetMinSize(150, 0);
301 m_sizerLbox
= sizerRight
; // save it to modify it later
303 // the 3 panes panes compose the window
304 sizerTop
->Add(sizerLeft
, 0, wxGROW
| (wxALL
& ~wxLEFT
), 10);
305 sizerTop
->Add(sizerMiddle
, 1, wxGROW
| wxALL
, 10);
306 sizerTop
->Add(sizerRight
, 1, wxGROW
| (wxALL
& ~wxRIGHT
), 10);
308 // final initializations
316 // ----------------------------------------------------------------------------
318 // ----------------------------------------------------------------------------
320 void ListboxWidgetsPage::Reset()
322 m_radioSelMode
->SetSelection(LboxSel_Single
);
323 m_chkSort
->SetValue(false);
324 m_chkCheck
->SetValue(false);
325 m_chkHScroll
->SetValue(true);
326 m_chkVScroll
->SetValue(false);
329 void ListboxWidgetsPage::CreateLbox()
332 switch ( m_radioSelMode
->GetSelection() )
335 wxFAIL_MSG( _T("unexpected radio box selection") );
337 case LboxSel_Single
: flags
|= wxLB_SINGLE
; break;
338 case LboxSel_Extended
: flags
|= wxLB_EXTENDED
; break;
339 case LboxSel_Multiple
: flags
|= wxLB_MULTIPLE
; break;
342 if ( m_chkVScroll
->GetValue() )
343 flags
|= wxLB_ALWAYS_SB
;
344 if ( m_chkHScroll
->GetValue() )
345 flags
|= wxLB_HSCROLL
;
346 if ( m_chkSort
->GetValue() )
352 int count
= m_lbox
->GetCount();
353 for ( int n
= 0; n
< count
; n
++ )
355 items
.Add(m_lbox
->GetString(n
));
358 m_sizerLbox
->Detach( m_lbox
);
362 #if wxUSE_CHECKLISTBOX
363 if ( m_chkCheck
->GetValue() )
365 m_lbox
= new wxCheckListBox(this, ListboxPage_Listbox
,
366 wxDefaultPosition
, wxDefaultSize
,
370 else // just a listbox
373 m_lbox
= new wxListBox(this, ListboxPage_Listbox
,
374 wxDefaultPosition
, wxDefaultSize
,
380 m_sizerLbox
->Add(m_lbox
, 1, wxGROW
| wxALL
, 5);
381 m_sizerLbox
->Layout();
384 // ----------------------------------------------------------------------------
386 // ----------------------------------------------------------------------------
388 void ListboxWidgetsPage::OnButtonReset(wxCommandEvent
& WXUNUSED(event
))
395 void ListboxWidgetsPage::OnButtonChange(wxCommandEvent
& WXUNUSED(event
))
397 wxArrayInt selections
;
398 int count
= m_lbox
->GetSelections(selections
);
399 wxString s
= m_textChange
->GetValue();
400 for ( int n
= 0; n
< count
; n
++ )
402 m_lbox
->SetString(selections
[n
], s
);
406 void ListboxWidgetsPage::OnButtonDelete(wxCommandEvent
& WXUNUSED(event
))
409 if ( !m_textDelete
->GetValue().ToULong(&n
) ||
410 (n
>= (unsigned)m_lbox
->GetCount()) )
418 void ListboxWidgetsPage::OnButtonDeleteSel(wxCommandEvent
& WXUNUSED(event
))
420 wxArrayInt selections
;
421 int n
= m_lbox
->GetSelections(selections
);
424 m_lbox
->Delete(selections
[--n
]);
428 void ListboxWidgetsPage::OnButtonClear(wxCommandEvent
& WXUNUSED(event
))
433 void ListboxWidgetsPage::OnButtonAdd(wxCommandEvent
& WXUNUSED(event
))
435 static unsigned int s_item
= 0;
437 wxString s
= m_textAdd
->GetValue();
438 if ( !m_textAdd
->IsModified() )
440 // update the default string
441 m_textAdd
->SetValue(wxString::Format(_T("test item %u"), ++s_item
));
447 void ListboxWidgetsPage::OnButtonAddMany(wxCommandEvent
& WXUNUSED(event
))
449 // "many" means 1000 here
450 for ( unsigned int n
= 0; n
< 1000; n
++ )
452 m_lbox
->Append(wxString::Format(_T("item #%u"), n
));
456 void ListboxWidgetsPage::OnButtonAddSeveral(wxCommandEvent
& WXUNUSED(event
))
459 items
.Add(_T("First"));
460 items
.Add(_T("another one"));
461 items
.Add(_T("and the last (very very very very very very very very very very long) one"));
462 m_lbox
->InsertItems(items
, 0);
465 void ListboxWidgetsPage::OnUpdateUIResetButton(wxUpdateUIEvent
& event
)
467 event
.Enable( (m_radioSelMode
->GetSelection() != LboxSel_Single
) ||
468 m_chkSort
->GetValue() ||
469 !m_chkHScroll
->GetValue() ||
470 m_chkVScroll
->GetValue() );
473 void ListboxWidgetsPage::OnUpdateUIDeleteButton(wxUpdateUIEvent
& event
)
476 event
.Enable(m_textDelete
->GetValue().ToULong(&n
) &&
477 (n
< (unsigned)m_lbox
->GetCount()));
480 void ListboxWidgetsPage::OnUpdateUIDeleteSelButton(wxUpdateUIEvent
& event
)
482 wxArrayInt selections
;
483 event
.Enable(m_lbox
->GetSelections(selections
) != 0);
486 void ListboxWidgetsPage::OnUpdateUIClearButton(wxUpdateUIEvent
& event
)
488 event
.Enable(m_lbox
->GetCount() != 0);
491 void ListboxWidgetsPage::OnUpdateUIAddSeveral(wxUpdateUIEvent
& event
)
493 event
.Enable(!(m_lbox
->GetWindowStyle() & wxLB_SORT
));
496 void ListboxWidgetsPage::OnListbox(wxCommandEvent
& event
)
498 long sel
= event
.GetSelection();
499 m_textDelete
->SetValue(wxString::Format(_T("%ld"), sel
));
501 if (event
.IsSelection())
502 wxLogMessage(_T("Listbox item %ld selected"), sel
);
504 wxLogMessage(_T("Listbox item %ld deselected"), sel
);
507 void ListboxWidgetsPage::OnListboxDClick(wxCommandEvent
& event
)
509 wxLogMessage( _T("Listbox item %ld double clicked"), event
.GetInt() );
512 void ListboxWidgetsPage::OnCheckListbox(wxCommandEvent
& event
)
514 wxLogMessage( _T("Listbox item %ld toggled"), event
.GetInt() );
517 void ListboxWidgetsPage::OnCheckOrRadioBox(wxCommandEvent
& WXUNUSED(event
))
522 #endif // wxUSE_LISTBOX