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 // Licence: wxWindows licence
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
, wxT("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 wxT("&Set listbox parameters"));
263 wxSizer
*sizerLeft
= new wxStaticBoxSizer(box
, wxVERTICAL
);
265 static const wxString modes
[] =
272 m_radioSelMode
= new wxRadioBox(this, wxID_ANY
, wxT("Selection &mode:"),
273 wxDefaultPosition
, wxDefaultSize
,
274 WXSIZEOF(modes
), modes
,
275 1, wxRA_SPECIFY_COLS
);
277 m_chkVScroll
= CreateCheckBoxAndAddToSizer
280 wxT("Always show &vertical scrollbar")
282 m_chkHScroll
= CreateCheckBoxAndAddToSizer
285 wxT("Show &horizontal scrollbar")
287 m_chkCheck
= CreateCheckBoxAndAddToSizer(sizerLeft
, wxT("&Check list box"));
288 m_chkSort
= CreateCheckBoxAndAddToSizer(sizerLeft
, wxT("&Sort items"));
289 m_chkOwnerDraw
= CreateCheckBoxAndAddToSizer(sizerLeft
, wxT("&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
, wxT("&Reset"));
295 sizerLeft
->Add(btn
, 0, wxALIGN_CENTRE_HORIZONTAL
| wxALL
, 15);
298 wxStaticBox
*box2
= new wxStaticBox(this, wxID_ANY
,
299 wxT("&Change listbox contents"));
300 wxSizer
*sizerMiddle
= new wxStaticBoxSizer(box2
, wxVERTICAL
);
302 wxSizer
*sizerRow
= new wxBoxSizer(wxHORIZONTAL
);
303 btn
= new wxButton(this, ListboxPage_Add
, wxT("&Add this string"));
304 m_textAdd
= new wxTextCtrl(this, ListboxPage_AddText
, wxT("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
, wxT("&Insert a few strings"));
310 sizerMiddle
->Add(btn
, 0, wxALL
| wxGROW
, 5);
312 btn
= new wxButton(this, ListboxPage_AddMany
, wxT("Add &many strings"));
313 sizerMiddle
->Add(btn
, 0, wxALL
| wxGROW
, 5);
315 sizerRow
= new wxBoxSizer(wxHORIZONTAL
);
316 btn
= new wxButton(this, ListboxPage_Change
, wxT("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
, wxT("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
, wxT("&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
, wxT("Delete &selection"));
337 sizerMiddle
->Add(btn
, 0, wxALL
| wxGROW
, 5);
339 btn
= new wxButton(this, ListboxPage_Clear
, wxT("&Clear"));
340 sizerMiddle
->Add(btn
, 0, wxALL
| wxGROW
, 5);
342 btn
= new wxButton(this, ListboxPage_ContainerTests
, wxT("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( wxT("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
452 wxLogWarning("Invalid index \"%s\"", text
->GetValue());
464 // ----------------------------------------------------------------------------
466 // ----------------------------------------------------------------------------
468 void ListboxWidgetsPage::OnButtonReset(wxCommandEvent
& WXUNUSED(event
))
475 void ListboxWidgetsPage::OnButtonChange(wxCommandEvent
& WXUNUSED(event
))
477 wxArrayInt selections
;
478 int count
= m_lbox
->GetSelections(selections
);
479 wxString s
= m_textChange
->GetValue();
480 for ( int n
= 0; n
< count
; n
++ )
482 m_lbox
->SetString(selections
[n
], s
);
486 void ListboxWidgetsPage::OnButtonEnsureVisible(wxCommandEvent
& WXUNUSED(event
))
489 if ( !GetValidIndexFromText(m_textEnsureVisible
, &n
) )
494 m_lbox
->EnsureVisible(n
);
497 void ListboxWidgetsPage::OnButtonDelete(wxCommandEvent
& WXUNUSED(event
))
500 if ( !GetValidIndexFromText(m_textDelete
, &n
) )
508 void ListboxWidgetsPage::OnButtonDeleteSel(wxCommandEvent
& WXUNUSED(event
))
510 wxArrayInt selections
;
511 int n
= m_lbox
->GetSelections(selections
);
514 m_lbox
->Delete(selections
[--n
]);
518 void ListboxWidgetsPage::OnButtonClear(wxCommandEvent
& WXUNUSED(event
))
523 void ListboxWidgetsPage::OnButtonAdd(wxCommandEvent
& WXUNUSED(event
))
525 static unsigned int s_item
= 0;
527 wxString s
= m_textAdd
->GetValue();
528 if ( !m_textAdd
->IsModified() )
530 // update the default string
531 m_textAdd
->SetValue(wxString::Format(wxT("test item %u"), ++s_item
));
537 void ListboxWidgetsPage::OnButtonAddMany(wxCommandEvent
& WXUNUSED(event
))
539 // "many" means 1000 here
540 for ( unsigned int n
= 0; n
< 1000; n
++ )
542 m_lbox
->Append(wxString::Format(wxT("item #%u"), n
));
546 void ListboxWidgetsPage::OnButtonAddSeveral(wxCommandEvent
& WXUNUSED(event
))
549 items
.Add(wxT("First"));
550 items
.Add(wxT("another one"));
551 items
.Add(wxT("and the last (very very very very very very very very very very long) one"));
552 m_lbox
->InsertItems(items
, 0);
555 void ListboxWidgetsPage::OnUpdateUIResetButton(wxUpdateUIEvent
& event
)
557 event
.Enable( (m_radioSelMode
->GetSelection() != LboxSel_Single
) ||
558 m_chkSort
->GetValue() ||
559 m_chkOwnerDraw
->GetValue() ||
560 !m_chkHScroll
->GetValue() ||
561 m_chkVScroll
->GetValue() );
564 void ListboxWidgetsPage::OnUpdateUIEnsureVisibleButton(wxUpdateUIEvent
& event
)
566 event
.Enable(GetValidIndexFromText(m_textEnsureVisible
));
569 void ListboxWidgetsPage::OnUpdateUIDeleteButton(wxUpdateUIEvent
& event
)
571 event
.Enable(GetValidIndexFromText(m_textDelete
));
574 void ListboxWidgetsPage::OnUpdateUIDeleteSelButton(wxUpdateUIEvent
& event
)
576 wxArrayInt selections
;
577 event
.Enable(m_lbox
->GetSelections(selections
) != 0);
580 void ListboxWidgetsPage::OnUpdateUIClearButton(wxUpdateUIEvent
& event
)
582 event
.Enable(m_lbox
->GetCount() != 0);
585 void ListboxWidgetsPage::OnUpdateUIAddSeveral(wxUpdateUIEvent
& event
)
587 event
.Enable(!(m_lbox
->GetWindowStyle() & wxLB_SORT
));
590 void ListboxWidgetsPage::OnListbox(wxCommandEvent
& event
)
592 long sel
= event
.GetSelection();
593 m_textDelete
->SetValue(wxString::Format(wxT("%ld"), sel
));
595 if (event
.IsSelection())
597 wxLogMessage(wxT("Listbox item %ld selected"), sel
);
601 wxLogMessage(wxT("Listbox item %ld deselected"), sel
);
605 void ListboxWidgetsPage::OnListboxDClick(wxCommandEvent
& event
)
607 wxLogMessage( wxT("Listbox item %d double clicked"), event
.GetInt() );
610 void ListboxWidgetsPage::OnCheckListbox(wxCommandEvent
& event
)
612 wxLogMessage( wxT("Listbox item %d toggled"), event
.GetInt() );
615 void ListboxWidgetsPage::OnCheckOrRadioBox(wxCommandEvent
& WXUNUSED(event
))
620 #endif // wxUSE_LISTBOX