1 /////////////////////////////////////////////////////////////////////////////
2 // Program: wxWidgets Widgets Sample
4 // Purpose: Part of the widgets sample showing wxListbox
5 // Author: Vadim Zeitlin
7 // Copyright: (c) 2001 Vadim Zeitlin
8 // Licence: wxWindows licence
9 /////////////////////////////////////////////////////////////////////////////
11 // ============================================================================
13 // ============================================================================
15 // ----------------------------------------------------------------------------
17 // ----------------------------------------------------------------------------
19 // for compilers that support precompilation, includes "wx/wx.h".
20 #include "wx/wxprec.h"
28 // for all others, include the necessary headers
32 #include "wx/bitmap.h"
33 #include "wx/button.h"
34 #include "wx/checkbox.h"
35 #include "wx/combobox.h"
36 #include "wx/listbox.h"
37 #include "wx/radiobox.h"
38 #include "wx/statbox.h"
39 #include "wx/textctrl.h"
44 #include "wx/checklst.h"
46 #include "itemcontainer.h"
49 #include "icons/listbox.xpm"
51 // ----------------------------------------------------------------------------
53 // ----------------------------------------------------------------------------
58 ListboxPage_Reset
= wxID_HIGHEST
,
61 ListboxPage_AddSeveral
,
65 ListboxPage_ChangeText
,
67 ListboxPage_DeleteText
,
68 ListboxPage_DeleteSel
,
70 ListboxPage_EnsureVisible
,
71 ListboxPage_EnsureVisibleText
,
72 ListboxPage_ContainerTests
75 // ----------------------------------------------------------------------------
77 // ----------------------------------------------------------------------------
79 class ListboxWidgetsPage
: public ItemContainerWidgetsPage
82 ListboxWidgetsPage(WidgetsBookCtrl
*book
, wxImageList
*imaglist
);
84 virtual wxControl
*GetWidget() const { return m_lbox
; }
85 virtual wxItemContainer
* GetContainer() const { return m_lbox
; }
86 virtual void RecreateWidget() { CreateLbox(); }
88 // lazy creation of the content
89 virtual void CreateContent();
93 void OnButtonReset(wxCommandEvent
& event
);
94 void OnButtonChange(wxCommandEvent
& event
);
95 void OnButtonEnsureVisible(wxCommandEvent
& event
);
96 void OnButtonDelete(wxCommandEvent
& event
);
97 void OnButtonDeleteSel(wxCommandEvent
& event
);
98 void OnButtonClear(wxCommandEvent
& event
);
99 void OnButtonAdd(wxCommandEvent
& event
);
100 void OnButtonAddSeveral(wxCommandEvent
& event
);
101 void OnButtonAddMany(wxCommandEvent
& event
);
103 void OnListbox(wxCommandEvent
& event
);
104 void OnListboxDClick(wxCommandEvent
& event
);
105 void OnCheckListbox(wxCommandEvent
& event
);
107 void OnCheckOrRadioBox(wxCommandEvent
& event
);
109 void OnUpdateUIAddSeveral(wxUpdateUIEvent
& event
);
110 void OnUpdateUIClearButton(wxUpdateUIEvent
& event
);
111 void OnUpdateUIEnsureVisibleButton(wxUpdateUIEvent
& event
);
112 void OnUpdateUIDeleteButton(wxUpdateUIEvent
& event
);
113 void OnUpdateUIDeleteSelButton(wxUpdateUIEvent
& event
);
114 void OnUpdateUIResetButton(wxUpdateUIEvent
& event
);
116 // reset the listbox parameters
119 // (re)create the listbox
122 // read the value of a listbox item index from the given control, return
123 // false if it's invalid
124 bool GetValidIndexFromText(const wxTextCtrl
*text
, int *n
= NULL
) const;
127 // listbox parameters
128 // ------------------
130 // the selection mode
138 // should it be sorted?
141 // should it have horz scroll/vert scrollbar permanently shown?
148 // the sel mode radiobox
149 wxRadioBox
*m_radioSelMode
;
152 wxCheckBox
*m_chkVScroll
,
158 // the listbox itself and the sizer it is in
166 wxSizer
*m_sizerLbox
;
168 // the text entries for "Add/change string" and "Delete" buttons
169 wxTextCtrl
*m_textAdd
,
171 *m_textEnsureVisible
,
175 DECLARE_EVENT_TABLE()
176 DECLARE_WIDGETS_PAGE(ListboxWidgetsPage
)
179 // ----------------------------------------------------------------------------
181 // ----------------------------------------------------------------------------
183 BEGIN_EVENT_TABLE(ListboxWidgetsPage
, WidgetsPage
)
184 EVT_BUTTON(ListboxPage_Reset
, ListboxWidgetsPage::OnButtonReset
)
185 EVT_BUTTON(ListboxPage_Change
, ListboxWidgetsPage::OnButtonChange
)
186 EVT_BUTTON(ListboxPage_Delete
, ListboxWidgetsPage::OnButtonDelete
)
187 EVT_BUTTON(ListboxPage_DeleteSel
, ListboxWidgetsPage::OnButtonDeleteSel
)
188 EVT_BUTTON(ListboxPage_EnsureVisible
, ListboxWidgetsPage::OnButtonEnsureVisible
)
189 EVT_BUTTON(ListboxPage_Clear
, ListboxWidgetsPage::OnButtonClear
)
190 EVT_BUTTON(ListboxPage_Add
, ListboxWidgetsPage::OnButtonAdd
)
191 EVT_BUTTON(ListboxPage_AddSeveral
, ListboxWidgetsPage::OnButtonAddSeveral
)
192 EVT_BUTTON(ListboxPage_AddMany
, ListboxWidgetsPage::OnButtonAddMany
)
193 EVT_BUTTON(ListboxPage_ContainerTests
, ItemContainerWidgetsPage::OnButtonTestItemContainer
)
195 EVT_TEXT_ENTER(ListboxPage_AddText
, ListboxWidgetsPage::OnButtonAdd
)
196 EVT_TEXT_ENTER(ListboxPage_DeleteText
, ListboxWidgetsPage::OnButtonDelete
)
197 EVT_TEXT_ENTER(ListboxPage_EnsureVisibleText
, ListboxWidgetsPage::OnButtonEnsureVisible
)
199 EVT_UPDATE_UI(ListboxPage_Reset
, ListboxWidgetsPage::OnUpdateUIResetButton
)
200 EVT_UPDATE_UI(ListboxPage_AddSeveral
, ListboxWidgetsPage::OnUpdateUIAddSeveral
)
201 EVT_UPDATE_UI(ListboxPage_Clear
, ListboxWidgetsPage::OnUpdateUIClearButton
)
202 EVT_UPDATE_UI(ListboxPage_DeleteText
, ListboxWidgetsPage::OnUpdateUIClearButton
)
203 EVT_UPDATE_UI(ListboxPage_Delete
, ListboxWidgetsPage::OnUpdateUIDeleteButton
)
204 EVT_UPDATE_UI(ListboxPage_Change
, ListboxWidgetsPage::OnUpdateUIDeleteSelButton
)
205 EVT_UPDATE_UI(ListboxPage_ChangeText
, ListboxWidgetsPage::OnUpdateUIDeleteSelButton
)
206 EVT_UPDATE_UI(ListboxPage_DeleteSel
, ListboxWidgetsPage::OnUpdateUIDeleteSelButton
)
207 EVT_UPDATE_UI(ListboxPage_EnsureVisible
, ListboxWidgetsPage::OnUpdateUIEnsureVisibleButton
)
209 EVT_LISTBOX(ListboxPage_Listbox
, ListboxWidgetsPage::OnListbox
)
210 EVT_LISTBOX_DCLICK(ListboxPage_Listbox
, ListboxWidgetsPage::OnListboxDClick
)
211 EVT_CHECKLISTBOX(ListboxPage_Listbox
, ListboxWidgetsPage::OnCheckListbox
)
213 EVT_CHECKBOX(wxID_ANY
, ListboxWidgetsPage::OnCheckOrRadioBox
)
214 EVT_RADIOBOX(wxID_ANY
, ListboxWidgetsPage::OnCheckOrRadioBox
)
217 // ============================================================================
219 // ============================================================================
221 #if defined(__WXUNIVERSAL__)
222 #define FAMILY_CTRLS UNIVERSAL_CTRLS
224 #define FAMILY_CTRLS NATIVE_CTRLS
227 IMPLEMENT_WIDGETS_PAGE(ListboxWidgetsPage
, wxT("Listbox"),
228 FAMILY_CTRLS
| WITH_ITEMS_CTRLS
231 ListboxWidgetsPage::ListboxWidgetsPage(WidgetsBookCtrl
*book
,
232 wxImageList
*imaglist
)
233 : ItemContainerWidgetsPage(book
, imaglist
, listbox_xpm
)
236 m_radioSelMode
= (wxRadioBox
*)NULL
;
242 m_chkOwnerDraw
= (wxCheckBox
*)NULL
;
245 m_sizerLbox
= (wxSizer
*)NULL
;
249 void ListboxWidgetsPage::CreateContent()
252 What we create here is a frame having 3 panes: style pane is the
253 leftmost one, in the middle the pane with buttons allowing to perform
254 miscellaneous listbox operations and the pane containing the listbox
257 wxSizer
*sizerTop
= new wxBoxSizer(wxHORIZONTAL
);
260 wxStaticBox
*box
= new wxStaticBox(this, wxID_ANY
,
261 wxT("&Set listbox parameters"));
262 wxSizer
*sizerLeft
= new wxStaticBoxSizer(box
, wxVERTICAL
);
264 static const wxString modes
[] =
271 m_radioSelMode
= new wxRadioBox(this, wxID_ANY
, wxT("Selection &mode:"),
272 wxDefaultPosition
, wxDefaultSize
,
273 WXSIZEOF(modes
), modes
,
274 1, wxRA_SPECIFY_COLS
);
276 m_chkVScroll
= CreateCheckBoxAndAddToSizer
279 wxT("Always show &vertical scrollbar")
281 m_chkHScroll
= CreateCheckBoxAndAddToSizer
284 wxT("Show &horizontal scrollbar")
286 m_chkCheck
= CreateCheckBoxAndAddToSizer(sizerLeft
, wxT("&Check list box"));
287 m_chkSort
= CreateCheckBoxAndAddToSizer(sizerLeft
, wxT("&Sort items"));
288 m_chkOwnerDraw
= CreateCheckBoxAndAddToSizer(sizerLeft
, wxT("&Owner drawn"));
290 sizerLeft
->Add(5, 5, 0, wxGROW
| wxALL
, 5); // spacer
291 sizerLeft
->Add(m_radioSelMode
, 0, wxGROW
| wxALL
, 5);
293 wxButton
*btn
= new wxButton(this, ListboxPage_Reset
, wxT("&Reset"));
294 sizerLeft
->Add(btn
, 0, wxALIGN_CENTRE_HORIZONTAL
| wxALL
, 15);
297 wxStaticBox
*box2
= new wxStaticBox(this, wxID_ANY
,
298 wxT("&Change listbox contents"));
299 wxSizer
*sizerMiddle
= new wxStaticBoxSizer(box2
, wxVERTICAL
);
301 wxSizer
*sizerRow
= new wxBoxSizer(wxHORIZONTAL
);
302 btn
= new wxButton(this, ListboxPage_Add
, wxT("&Add this string"));
303 m_textAdd
= new wxTextCtrl(this, ListboxPage_AddText
, wxT("test item 0"));
304 sizerRow
->Add(btn
, 0, wxRIGHT
, 5);
305 sizerRow
->Add(m_textAdd
, 1, wxLEFT
, 5);
306 sizerMiddle
->Add(sizerRow
, 0, wxALL
| wxGROW
, 5);
308 btn
= new wxButton(this, ListboxPage_AddSeveral
, wxT("&Insert a few strings"));
309 sizerMiddle
->Add(btn
, 0, wxALL
| wxGROW
, 5);
311 btn
= new wxButton(this, ListboxPage_AddMany
, wxT("Add &many strings"));
312 sizerMiddle
->Add(btn
, 0, wxALL
| wxGROW
, 5);
314 sizerRow
= new wxBoxSizer(wxHORIZONTAL
);
315 btn
= new wxButton(this, ListboxPage_Change
, wxT("C&hange current"));
316 m_textChange
= new wxTextCtrl(this, ListboxPage_ChangeText
, wxEmptyString
);
317 sizerRow
->Add(btn
, 0, wxRIGHT
, 5);
318 sizerRow
->Add(m_textChange
, 1, wxLEFT
, 5);
319 sizerMiddle
->Add(sizerRow
, 0, wxALL
| wxGROW
, 5);
321 sizerRow
= new wxBoxSizer(wxHORIZONTAL
);
322 btn
= new wxButton(this, ListboxPage_EnsureVisible
, wxT("Make item &visible"));
323 m_textEnsureVisible
= new wxTextCtrl(this, ListboxPage_EnsureVisibleText
, wxEmptyString
);
324 sizerRow
->Add(btn
, 0, wxRIGHT
, 5);
325 sizerRow
->Add(m_textEnsureVisible
, 1, wxLEFT
, 5);
326 sizerMiddle
->Add(sizerRow
, 0, wxALL
| wxGROW
, 5);
328 sizerRow
= new wxBoxSizer(wxHORIZONTAL
);
329 btn
= new wxButton(this, ListboxPage_Delete
, wxT("&Delete this item"));
330 m_textDelete
= new wxTextCtrl(this, ListboxPage_DeleteText
, wxEmptyString
);
331 sizerRow
->Add(btn
, 0, wxRIGHT
, 5);
332 sizerRow
->Add(m_textDelete
, 1, wxLEFT
, 5);
333 sizerMiddle
->Add(sizerRow
, 0, wxALL
| wxGROW
, 5);
335 btn
= new wxButton(this, ListboxPage_DeleteSel
, wxT("Delete &selection"));
336 sizerMiddle
->Add(btn
, 0, wxALL
| wxGROW
, 5);
338 btn
= new wxButton(this, ListboxPage_Clear
, wxT("&Clear"));
339 sizerMiddle
->Add(btn
, 0, wxALL
| wxGROW
, 5);
341 btn
= new wxButton(this, ListboxPage_ContainerTests
, wxT("Run &tests"));
342 sizerMiddle
->Add(btn
, 0, wxALL
| wxGROW
, 5);
345 wxSizer
*sizerRight
= new wxBoxSizer(wxVERTICAL
);
346 m_lbox
= new wxListBox(this, ListboxPage_Listbox
,
347 wxDefaultPosition
, wxDefaultSize
,
350 sizerRight
->Add(m_lbox
, 1, wxGROW
| wxALL
, 5);
351 sizerRight
->SetMinSize(150, 0);
352 m_sizerLbox
= sizerRight
; // save it to modify it later
354 // the 3 panes panes compose the window
355 sizerTop
->Add(sizerLeft
, 0, wxGROW
| (wxALL
& ~wxLEFT
), 10);
356 sizerTop
->Add(sizerMiddle
, 1, wxGROW
| wxALL
, 10);
357 sizerTop
->Add(sizerRight
, 1, wxGROW
| (wxALL
& ~wxRIGHT
), 10);
359 // final initializations
365 // ----------------------------------------------------------------------------
367 // ----------------------------------------------------------------------------
369 void ListboxWidgetsPage::Reset()
371 m_radioSelMode
->SetSelection(LboxSel_Single
);
372 m_chkVScroll
->SetValue(false);
373 m_chkHScroll
->SetValue(true);
374 m_chkCheck
->SetValue(false);
375 m_chkSort
->SetValue(false);
376 m_chkOwnerDraw
->SetValue(false);
379 void ListboxWidgetsPage::CreateLbox()
381 int flags
= ms_defaultFlags
;
382 switch ( m_radioSelMode
->GetSelection() )
385 wxFAIL_MSG( wxT("unexpected radio box selection") );
387 case LboxSel_Single
: flags
|= wxLB_SINGLE
; break;
388 case LboxSel_Extended
: flags
|= wxLB_EXTENDED
; break;
389 case LboxSel_Multiple
: flags
|= wxLB_MULTIPLE
; break;
392 if ( m_chkVScroll
->GetValue() )
393 flags
|= wxLB_ALWAYS_SB
;
394 if ( m_chkHScroll
->GetValue() )
395 flags
|= wxLB_HSCROLL
;
396 if ( m_chkSort
->GetValue() )
398 if ( m_chkOwnerDraw
->GetValue() )
399 flags
|= wxLB_OWNERDRAW
;
404 int count
= m_lbox
->GetCount();
405 for ( int n
= 0; n
< count
; n
++ )
407 items
.Add(m_lbox
->GetString(n
));
410 m_sizerLbox
->Detach( m_lbox
);
414 #if wxUSE_CHECKLISTBOX
415 if ( m_chkCheck
->GetValue() )
417 m_lbox
= new wxCheckListBox(this, ListboxPage_Listbox
,
418 wxDefaultPosition
, wxDefaultSize
,
422 else // just a listbox
425 m_lbox
= new wxListBox(this, ListboxPage_Listbox
,
426 wxDefaultPosition
, wxDefaultSize
,
432 m_sizerLbox
->Add(m_lbox
, 1, wxGROW
| wxALL
, 5);
433 m_sizerLbox
->Layout();
436 // ----------------------------------------------------------------------------
437 // miscellaneous helpers
438 // ----------------------------------------------------------------------------
441 ListboxWidgetsPage::GetValidIndexFromText(const wxTextCtrl
*text
, int *n
) const
444 if ( !text
->GetValue().ToULong(&idx
) || (idx
>= m_lbox
->GetCount()) )
446 // don't give the warning if we're just testing but do give it if we
447 // want to retrieve the value as this is only done in answer to a user
451 wxLogWarning("Invalid index \"%s\"", text
->GetValue());
463 // ----------------------------------------------------------------------------
465 // ----------------------------------------------------------------------------
467 void ListboxWidgetsPage::OnButtonReset(wxCommandEvent
& WXUNUSED(event
))
474 void ListboxWidgetsPage::OnButtonChange(wxCommandEvent
& WXUNUSED(event
))
476 wxArrayInt selections
;
477 int count
= m_lbox
->GetSelections(selections
);
478 wxString s
= m_textChange
->GetValue();
479 for ( int n
= 0; n
< count
; n
++ )
481 m_lbox
->SetString(selections
[n
], s
);
485 void ListboxWidgetsPage::OnButtonEnsureVisible(wxCommandEvent
& WXUNUSED(event
))
488 if ( !GetValidIndexFromText(m_textEnsureVisible
, &n
) )
493 m_lbox
->EnsureVisible(n
);
496 void ListboxWidgetsPage::OnButtonDelete(wxCommandEvent
& WXUNUSED(event
))
499 if ( !GetValidIndexFromText(m_textDelete
, &n
) )
507 void ListboxWidgetsPage::OnButtonDeleteSel(wxCommandEvent
& WXUNUSED(event
))
509 wxArrayInt selections
;
510 int n
= m_lbox
->GetSelections(selections
);
513 m_lbox
->Delete(selections
[--n
]);
517 void ListboxWidgetsPage::OnButtonClear(wxCommandEvent
& WXUNUSED(event
))
522 void ListboxWidgetsPage::OnButtonAdd(wxCommandEvent
& WXUNUSED(event
))
524 static unsigned int s_item
= 0;
526 wxString s
= m_textAdd
->GetValue();
527 if ( !m_textAdd
->IsModified() )
529 // update the default string
530 m_textAdd
->SetValue(wxString::Format(wxT("test item %u"), ++s_item
));
536 void ListboxWidgetsPage::OnButtonAddMany(wxCommandEvent
& WXUNUSED(event
))
538 // "many" means 1000 here
539 for ( unsigned int n
= 0; n
< 1000; n
++ )
541 m_lbox
->Append(wxString::Format(wxT("item #%u"), n
));
545 void ListboxWidgetsPage::OnButtonAddSeveral(wxCommandEvent
& WXUNUSED(event
))
548 items
.Add(wxT("First"));
549 items
.Add(wxT("another one"));
550 items
.Add(wxT("and the last (very very very very very very very very very very long) one"));
551 m_lbox
->InsertItems(items
, 0);
554 void ListboxWidgetsPage::OnUpdateUIResetButton(wxUpdateUIEvent
& event
)
556 event
.Enable( (m_radioSelMode
->GetSelection() != LboxSel_Single
) ||
557 m_chkSort
->GetValue() ||
558 m_chkOwnerDraw
->GetValue() ||
559 !m_chkHScroll
->GetValue() ||
560 m_chkVScroll
->GetValue() );
563 void ListboxWidgetsPage::OnUpdateUIEnsureVisibleButton(wxUpdateUIEvent
& event
)
565 event
.Enable(GetValidIndexFromText(m_textEnsureVisible
));
568 void ListboxWidgetsPage::OnUpdateUIDeleteButton(wxUpdateUIEvent
& event
)
570 event
.Enable(GetValidIndexFromText(m_textDelete
));
573 void ListboxWidgetsPage::OnUpdateUIDeleteSelButton(wxUpdateUIEvent
& event
)
575 wxArrayInt selections
;
576 event
.Enable(m_lbox
->GetSelections(selections
) != 0);
579 void ListboxWidgetsPage::OnUpdateUIClearButton(wxUpdateUIEvent
& event
)
581 event
.Enable(m_lbox
->GetCount() != 0);
584 void ListboxWidgetsPage::OnUpdateUIAddSeveral(wxUpdateUIEvent
& event
)
586 event
.Enable(!(m_lbox
->GetWindowStyle() & wxLB_SORT
));
589 void ListboxWidgetsPage::OnListbox(wxCommandEvent
& event
)
591 long sel
= event
.GetSelection();
592 m_textDelete
->SetValue(wxString::Format(wxT("%ld"), sel
));
594 if (event
.IsSelection())
596 wxLogMessage(wxT("Listbox item %ld selected"), sel
);
600 wxLogMessage(wxT("Listbox item %ld deselected"), sel
);
604 void ListboxWidgetsPage::OnListboxDClick(wxCommandEvent
& event
)
606 wxLogMessage( wxT("Listbox item %d double clicked"), event
.GetInt() );
609 void ListboxWidgetsPage::OnCheckListbox(wxCommandEvent
& event
)
611 wxLogMessage( wxT("Listbox item %d toggled"), event
.GetInt() );
614 void ListboxWidgetsPage::OnCheckOrRadioBox(wxCommandEvent
& WXUNUSED(event
))
619 #endif // wxUSE_LISTBOX