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"
47 #include "itemcontainer.h"
50 #include "icons/listbox.xpm"
52 // ----------------------------------------------------------------------------
54 // ----------------------------------------------------------------------------
59 ListboxPage_Reset
= wxID_HIGHEST
,
62 ListboxPage_AddSeveral
,
66 ListboxPage_ChangeText
,
68 ListboxPage_DeleteText
,
69 ListboxPage_DeleteSel
,
71 ListboxPage_EnsureVisible
,
72 ListboxPage_EnsureVisibleText
,
73 ListboxPage_ContainerTests
76 // ----------------------------------------------------------------------------
78 // ----------------------------------------------------------------------------
80 class ListboxWidgetsPage
: public ItemContainerWidgetsPage
83 ListboxWidgetsPage(WidgetsBookCtrl
*book
, wxImageList
*imaglist
);
85 virtual wxControl
*GetWidget() const { return m_lbox
; }
86 virtual wxItemContainer
* GetContainer() const { return m_lbox
; }
87 virtual void RecreateWidget() { CreateLbox(); }
89 // lazy creation of the content
90 virtual void CreateContent();
94 void OnButtonReset(wxCommandEvent
& event
);
95 void OnButtonChange(wxCommandEvent
& event
);
96 void OnButtonEnsureVisible(wxCommandEvent
& event
);
97 void OnButtonDelete(wxCommandEvent
& event
);
98 void OnButtonDeleteSel(wxCommandEvent
& event
);
99 void OnButtonClear(wxCommandEvent
& event
);
100 void OnButtonAdd(wxCommandEvent
& event
);
101 void OnButtonAddSeveral(wxCommandEvent
& event
);
102 void OnButtonAddMany(wxCommandEvent
& event
);
104 void OnListbox(wxCommandEvent
& event
);
105 void OnListboxDClick(wxCommandEvent
& event
);
106 void OnCheckListbox(wxCommandEvent
& event
);
108 void OnCheckOrRadioBox(wxCommandEvent
& event
);
110 void OnUpdateUIAddSeveral(wxUpdateUIEvent
& event
);
111 void OnUpdateUIClearButton(wxUpdateUIEvent
& event
);
112 void OnUpdateUIEnsureVisibleButton(wxUpdateUIEvent
& event
);
113 void OnUpdateUIDeleteButton(wxUpdateUIEvent
& event
);
114 void OnUpdateUIDeleteSelButton(wxUpdateUIEvent
& event
);
115 void OnUpdateUIResetButton(wxUpdateUIEvent
& event
);
117 // reset the listbox parameters
120 // (re)create the listbox
123 // read the value of a listbox item index from the given control, return
124 // false if it's invalid
125 bool GetValidIndexFromText(const wxTextCtrl
*text
, int *n
= NULL
) const;
128 // listbox parameters
129 // ------------------
131 // the selection mode
139 // should it be sorted?
142 // should it have horz scroll/vert scrollbar permanently shown?
149 // the sel mode radiobox
150 wxRadioBox
*m_radioSelMode
;
153 wxCheckBox
*m_chkVScroll
,
159 // the listbox itself and the sizer it is in
167 wxSizer
*m_sizerLbox
;
169 // the text entries for "Add/change string" and "Delete" buttons
170 wxTextCtrl
*m_textAdd
,
172 *m_textEnsureVisible
,
176 DECLARE_EVENT_TABLE()
177 DECLARE_WIDGETS_PAGE(ListboxWidgetsPage
)
180 // ----------------------------------------------------------------------------
182 // ----------------------------------------------------------------------------
184 BEGIN_EVENT_TABLE(ListboxWidgetsPage
, WidgetsPage
)
185 EVT_BUTTON(ListboxPage_Reset
, ListboxWidgetsPage::OnButtonReset
)
186 EVT_BUTTON(ListboxPage_Change
, ListboxWidgetsPage::OnButtonChange
)
187 EVT_BUTTON(ListboxPage_Delete
, ListboxWidgetsPage::OnButtonDelete
)
188 EVT_BUTTON(ListboxPage_DeleteSel
, ListboxWidgetsPage::OnButtonDeleteSel
)
189 EVT_BUTTON(ListboxPage_EnsureVisible
, ListboxWidgetsPage::OnButtonEnsureVisible
)
190 EVT_BUTTON(ListboxPage_Clear
, ListboxWidgetsPage::OnButtonClear
)
191 EVT_BUTTON(ListboxPage_Add
, ListboxWidgetsPage::OnButtonAdd
)
192 EVT_BUTTON(ListboxPage_AddSeveral
, ListboxWidgetsPage::OnButtonAddSeveral
)
193 EVT_BUTTON(ListboxPage_AddMany
, ListboxWidgetsPage::OnButtonAddMany
)
194 EVT_BUTTON(ListboxPage_ContainerTests
, ItemContainerWidgetsPage::OnButtonTestItemContainer
)
196 EVT_TEXT_ENTER(ListboxPage_AddText
, ListboxWidgetsPage::OnButtonAdd
)
197 EVT_TEXT_ENTER(ListboxPage_DeleteText
, ListboxWidgetsPage::OnButtonDelete
)
198 EVT_TEXT_ENTER(ListboxPage_EnsureVisibleText
, ListboxWidgetsPage::OnButtonEnsureVisible
)
200 EVT_UPDATE_UI(ListboxPage_Reset
, ListboxWidgetsPage::OnUpdateUIResetButton
)
201 EVT_UPDATE_UI(ListboxPage_AddSeveral
, ListboxWidgetsPage::OnUpdateUIAddSeveral
)
202 EVT_UPDATE_UI(ListboxPage_Clear
, ListboxWidgetsPage::OnUpdateUIClearButton
)
203 EVT_UPDATE_UI(ListboxPage_DeleteText
, ListboxWidgetsPage::OnUpdateUIClearButton
)
204 EVT_UPDATE_UI(ListboxPage_Delete
, ListboxWidgetsPage::OnUpdateUIDeleteButton
)
205 EVT_UPDATE_UI(ListboxPage_Change
, ListboxWidgetsPage::OnUpdateUIDeleteSelButton
)
206 EVT_UPDATE_UI(ListboxPage_ChangeText
, ListboxWidgetsPage::OnUpdateUIDeleteSelButton
)
207 EVT_UPDATE_UI(ListboxPage_DeleteSel
, ListboxWidgetsPage::OnUpdateUIDeleteSelButton
)
208 EVT_UPDATE_UI(ListboxPage_EnsureVisible
, ListboxWidgetsPage::OnUpdateUIEnsureVisibleButton
)
210 EVT_LISTBOX(ListboxPage_Listbox
, ListboxWidgetsPage::OnListbox
)
211 EVT_LISTBOX_DCLICK(ListboxPage_Listbox
, ListboxWidgetsPage::OnListboxDClick
)
212 EVT_CHECKLISTBOX(ListboxPage_Listbox
, ListboxWidgetsPage::OnCheckListbox
)
214 EVT_CHECKBOX(wxID_ANY
, ListboxWidgetsPage::OnCheckOrRadioBox
)
215 EVT_RADIOBOX(wxID_ANY
, ListboxWidgetsPage::OnCheckOrRadioBox
)
218 // ============================================================================
220 // ============================================================================
222 #if defined(__WXUNIVERSAL__)
223 #define FAMILY_CTRLS UNIVERSAL_CTRLS
225 #define FAMILY_CTRLS NATIVE_CTRLS
228 IMPLEMENT_WIDGETS_PAGE(ListboxWidgetsPage
, _T("Listbox"),
229 FAMILY_CTRLS
| WITH_ITEMS_CTRLS
232 ListboxWidgetsPage::ListboxWidgetsPage(WidgetsBookCtrl
*book
,
233 wxImageList
*imaglist
)
234 : ItemContainerWidgetsPage(book
, imaglist
, listbox_xpm
)
237 m_radioSelMode
= (wxRadioBox
*)NULL
;
243 m_chkOwnerDraw
= (wxCheckBox
*)NULL
;
246 m_sizerLbox
= (wxSizer
*)NULL
;
250 void ListboxWidgetsPage::CreateContent()
253 What we create here is a frame having 3 panes: style pane is the
254 leftmost one, in the middle the pane with buttons allowing to perform
255 miscellaneous listbox operations and the pane containing the listbox
258 wxSizer
*sizerTop
= new wxBoxSizer(wxHORIZONTAL
);
261 wxStaticBox
*box
= new wxStaticBox(this, wxID_ANY
,
262 _T("&Set listbox parameters"));
263 wxSizer
*sizerLeft
= new wxStaticBoxSizer(box
, wxVERTICAL
);
265 static const wxString modes
[] =
272 m_radioSelMode
= new wxRadioBox(this, wxID_ANY
, _T("Selection &mode:"),
273 wxDefaultPosition
, wxDefaultSize
,
274 WXSIZEOF(modes
), modes
,
275 1, wxRA_SPECIFY_COLS
);
277 m_chkVScroll
= CreateCheckBoxAndAddToSizer
280 _T("Always show &vertical scrollbar")
282 m_chkHScroll
= CreateCheckBoxAndAddToSizer
285 _T("Show &horizontal scrollbar")
287 m_chkCheck
= CreateCheckBoxAndAddToSizer(sizerLeft
, _T("&Check list box"));
288 m_chkSort
= CreateCheckBoxAndAddToSizer(sizerLeft
, _T("&Sort items"));
289 m_chkOwnerDraw
= CreateCheckBoxAndAddToSizer(sizerLeft
, _T("&Owner drawn"));
291 sizerLeft
->Add(5, 5, 0, wxGROW
| wxALL
, 5); // spacer
292 sizerLeft
->Add(m_radioSelMode
, 0, wxGROW
| wxALL
, 5);
294 wxButton
*btn
= new wxButton(this, ListboxPage_Reset
, _T("&Reset"));
295 sizerLeft
->Add(btn
, 0, wxALIGN_CENTRE_HORIZONTAL
| wxALL
, 15);
298 wxStaticBox
*box2
= new wxStaticBox(this, wxID_ANY
,
299 _T("&Change listbox contents"));
300 wxSizer
*sizerMiddle
= new wxStaticBoxSizer(box2
, wxVERTICAL
);
302 wxSizer
*sizerRow
= new wxBoxSizer(wxHORIZONTAL
);
303 btn
= new wxButton(this, ListboxPage_Add
, _T("&Add this string"));
304 m_textAdd
= new wxTextCtrl(this, ListboxPage_AddText
, _T("test item 0"));
305 sizerRow
->Add(btn
, 0, wxRIGHT
, 5);
306 sizerRow
->Add(m_textAdd
, 1, wxLEFT
, 5);
307 sizerMiddle
->Add(sizerRow
, 0, wxALL
| wxGROW
, 5);
309 btn
= new wxButton(this, ListboxPage_AddSeveral
, _T("&Insert a few strings"));
310 sizerMiddle
->Add(btn
, 0, wxALL
| wxGROW
, 5);
312 btn
= new wxButton(this, ListboxPage_AddMany
, _T("Add &many strings"));
313 sizerMiddle
->Add(btn
, 0, wxALL
| wxGROW
, 5);
315 sizerRow
= new wxBoxSizer(wxHORIZONTAL
);
316 btn
= new wxButton(this, ListboxPage_Change
, _T("C&hange current"));
317 m_textChange
= new wxTextCtrl(this, ListboxPage_ChangeText
, wxEmptyString
);
318 sizerRow
->Add(btn
, 0, wxRIGHT
, 5);
319 sizerRow
->Add(m_textChange
, 1, wxLEFT
, 5);
320 sizerMiddle
->Add(sizerRow
, 0, wxALL
| wxGROW
, 5);
322 sizerRow
= new wxBoxSizer(wxHORIZONTAL
);
323 btn
= new wxButton(this, ListboxPage_EnsureVisible
, _T("Make item &visible"));
324 m_textEnsureVisible
= new wxTextCtrl(this, ListboxPage_EnsureVisibleText
, wxEmptyString
);
325 sizerRow
->Add(btn
, 0, wxRIGHT
, 5);
326 sizerRow
->Add(m_textEnsureVisible
, 1, wxLEFT
, 5);
327 sizerMiddle
->Add(sizerRow
, 0, wxALL
| wxGROW
, 5);
329 sizerRow
= new wxBoxSizer(wxHORIZONTAL
);
330 btn
= new wxButton(this, ListboxPage_Delete
, _T("&Delete this item"));
331 m_textDelete
= new wxTextCtrl(this, ListboxPage_DeleteText
, wxEmptyString
);
332 sizerRow
->Add(btn
, 0, wxRIGHT
, 5);
333 sizerRow
->Add(m_textDelete
, 1, wxLEFT
, 5);
334 sizerMiddle
->Add(sizerRow
, 0, wxALL
| wxGROW
, 5);
336 btn
= new wxButton(this, ListboxPage_DeleteSel
, _T("Delete &selection"));
337 sizerMiddle
->Add(btn
, 0, wxALL
| wxGROW
, 5);
339 btn
= new wxButton(this, ListboxPage_Clear
, _T("&Clear"));
340 sizerMiddle
->Add(btn
, 0, wxALL
| wxGROW
, 5);
342 btn
= new wxButton(this, ListboxPage_ContainerTests
, _T("Run &tests"));
343 sizerMiddle
->Add(btn
, 0, wxALL
| wxGROW
, 5);
346 wxSizer
*sizerRight
= new wxBoxSizer(wxVERTICAL
);
347 m_lbox
= new wxListBox(this, ListboxPage_Listbox
,
348 wxDefaultPosition
, wxDefaultSize
,
351 sizerRight
->Add(m_lbox
, 1, wxGROW
| wxALL
, 5);
352 sizerRight
->SetMinSize(150, 0);
353 m_sizerLbox
= sizerRight
; // save it to modify it later
355 // the 3 panes panes compose the window
356 sizerTop
->Add(sizerLeft
, 0, wxGROW
| (wxALL
& ~wxLEFT
), 10);
357 sizerTop
->Add(sizerMiddle
, 1, wxGROW
| wxALL
, 10);
358 sizerTop
->Add(sizerRight
, 1, wxGROW
| (wxALL
& ~wxRIGHT
), 10);
360 // final initializations
366 // ----------------------------------------------------------------------------
368 // ----------------------------------------------------------------------------
370 void ListboxWidgetsPage::Reset()
372 m_radioSelMode
->SetSelection(LboxSel_Single
);
373 m_chkVScroll
->SetValue(false);
374 m_chkHScroll
->SetValue(true);
375 m_chkCheck
->SetValue(false);
376 m_chkSort
->SetValue(false);
377 m_chkOwnerDraw
->SetValue(false);
380 void ListboxWidgetsPage::CreateLbox()
382 int flags
= ms_defaultFlags
;
383 switch ( m_radioSelMode
->GetSelection() )
386 wxFAIL_MSG( _T("unexpected radio box selection") );
388 case LboxSel_Single
: flags
|= wxLB_SINGLE
; break;
389 case LboxSel_Extended
: flags
|= wxLB_EXTENDED
; break;
390 case LboxSel_Multiple
: flags
|= wxLB_MULTIPLE
; break;
393 if ( m_chkVScroll
->GetValue() )
394 flags
|= wxLB_ALWAYS_SB
;
395 if ( m_chkHScroll
->GetValue() )
396 flags
|= wxLB_HSCROLL
;
397 if ( m_chkSort
->GetValue() )
399 if ( m_chkOwnerDraw
->GetValue() )
400 flags
|= wxLB_OWNERDRAW
;
405 int count
= m_lbox
->GetCount();
406 for ( int n
= 0; n
< count
; n
++ )
408 items
.Add(m_lbox
->GetString(n
));
411 m_sizerLbox
->Detach( m_lbox
);
415 #if wxUSE_CHECKLISTBOX
416 if ( m_chkCheck
->GetValue() )
418 m_lbox
= new wxCheckListBox(this, ListboxPage_Listbox
,
419 wxDefaultPosition
, wxDefaultSize
,
423 else // just a listbox
426 m_lbox
= new wxListBox(this, ListboxPage_Listbox
,
427 wxDefaultPosition
, wxDefaultSize
,
433 m_sizerLbox
->Add(m_lbox
, 1, wxGROW
| wxALL
, 5);
434 m_sizerLbox
->Layout();
437 // ----------------------------------------------------------------------------
438 // miscellaneous helpers
439 // ----------------------------------------------------------------------------
442 ListboxWidgetsPage::GetValidIndexFromText(const wxTextCtrl
*text
, int *n
) const
445 if ( !text
->GetValue().ToULong(&idx
) || (idx
>= m_lbox
->GetCount()) )
447 // don't give the warning if we're just testing but do give it if we
448 // want to retrieve the value as this is only done in answer to a user
451 wxLogWarning("Invalid index \"%s\"", text
->GetValue());
461 // ----------------------------------------------------------------------------
463 // ----------------------------------------------------------------------------
465 void ListboxWidgetsPage::OnButtonReset(wxCommandEvent
& WXUNUSED(event
))
472 void ListboxWidgetsPage::OnButtonChange(wxCommandEvent
& WXUNUSED(event
))
474 wxArrayInt selections
;
475 int count
= m_lbox
->GetSelections(selections
);
476 wxString s
= m_textChange
->GetValue();
477 for ( int n
= 0; n
< count
; n
++ )
479 m_lbox
->SetString(selections
[n
], s
);
483 void ListboxWidgetsPage::OnButtonEnsureVisible(wxCommandEvent
& WXUNUSED(event
))
486 if ( !GetValidIndexFromText(m_textEnsureVisible
, &n
) )
491 m_lbox
->EnsureVisible(n
);
494 void ListboxWidgetsPage::OnButtonDelete(wxCommandEvent
& WXUNUSED(event
))
497 if ( !GetValidIndexFromText(m_textDelete
, &n
) )
505 void ListboxWidgetsPage::OnButtonDeleteSel(wxCommandEvent
& WXUNUSED(event
))
507 wxArrayInt selections
;
508 int n
= m_lbox
->GetSelections(selections
);
511 m_lbox
->Delete(selections
[--n
]);
515 void ListboxWidgetsPage::OnButtonClear(wxCommandEvent
& WXUNUSED(event
))
520 void ListboxWidgetsPage::OnButtonAdd(wxCommandEvent
& WXUNUSED(event
))
522 static unsigned int s_item
= 0;
524 wxString s
= m_textAdd
->GetValue();
525 if ( !m_textAdd
->IsModified() )
527 // update the default string
528 m_textAdd
->SetValue(wxString::Format(_T("test item %u"), ++s_item
));
534 void ListboxWidgetsPage::OnButtonAddMany(wxCommandEvent
& WXUNUSED(event
))
536 // "many" means 1000 here
537 for ( unsigned int n
= 0; n
< 1000; n
++ )
539 m_lbox
->Append(wxString::Format(_T("item #%u"), n
));
543 void ListboxWidgetsPage::OnButtonAddSeveral(wxCommandEvent
& WXUNUSED(event
))
546 items
.Add(_T("First"));
547 items
.Add(_T("another one"));
548 items
.Add(_T("and the last (very very very very very very very very very very long) one"));
549 m_lbox
->InsertItems(items
, 0);
552 void ListboxWidgetsPage::OnUpdateUIResetButton(wxUpdateUIEvent
& event
)
554 event
.Enable( (m_radioSelMode
->GetSelection() != LboxSel_Single
) ||
555 m_chkSort
->GetValue() ||
556 m_chkOwnerDraw
->GetValue() ||
557 !m_chkHScroll
->GetValue() ||
558 m_chkVScroll
->GetValue() );
561 void ListboxWidgetsPage::OnUpdateUIEnsureVisibleButton(wxUpdateUIEvent
& event
)
563 event
.Enable(GetValidIndexFromText(m_textEnsureVisible
));
566 void ListboxWidgetsPage::OnUpdateUIDeleteButton(wxUpdateUIEvent
& event
)
568 event
.Enable(GetValidIndexFromText(m_textDelete
));
571 void ListboxWidgetsPage::OnUpdateUIDeleteSelButton(wxUpdateUIEvent
& event
)
573 wxArrayInt selections
;
574 event
.Enable(m_lbox
->GetSelections(selections
) != 0);
577 void ListboxWidgetsPage::OnUpdateUIClearButton(wxUpdateUIEvent
& event
)
579 event
.Enable(m_lbox
->GetCount() != 0);
582 void ListboxWidgetsPage::OnUpdateUIAddSeveral(wxUpdateUIEvent
& event
)
584 event
.Enable(!(m_lbox
->GetWindowStyle() & wxLB_SORT
));
587 void ListboxWidgetsPage::OnListbox(wxCommandEvent
& event
)
589 long sel
= event
.GetSelection();
590 m_textDelete
->SetValue(wxString::Format(_T("%ld"), sel
));
592 if (event
.IsSelection())
593 wxLogMessage(_T("Listbox item %ld selected"), sel
);
595 wxLogMessage(_T("Listbox item %ld deselected"), sel
);
598 void ListboxWidgetsPage::OnListboxDClick(wxCommandEvent
& event
)
600 wxLogMessage( _T("Listbox item %d double clicked"), event
.GetInt() );
603 void ListboxWidgetsPage::OnCheckListbox(wxCommandEvent
& event
)
605 wxLogMessage( _T("Listbox item %d toggled"), event
.GetInt() );
608 void ListboxWidgetsPage::OnCheckOrRadioBox(wxCommandEvent
& WXUNUSED(event
))
613 #endif // wxUSE_LISTBOX