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_ContainerTests
74 // ----------------------------------------------------------------------------
76 // ----------------------------------------------------------------------------
78 class ListboxWidgetsPage
: public ItemContainerWidgetsPage
81 ListboxWidgetsPage(WidgetsBookCtrl
*book
, wxImageList
*imaglist
);
83 virtual wxControl
*GetWidget() const { return m_lbox
; }
84 virtual wxItemContainer
* GetContainer() const { return m_lbox
; }
85 virtual void RecreateWidget() { CreateLbox(); }
87 // lazy creation of the content
88 virtual void CreateContent();
92 void OnButtonReset(wxCommandEvent
& event
);
93 void OnButtonChange(wxCommandEvent
& event
);
94 void OnButtonDelete(wxCommandEvent
& event
);
95 void OnButtonDeleteSel(wxCommandEvent
& event
);
96 void OnButtonClear(wxCommandEvent
& event
);
97 void OnButtonAdd(wxCommandEvent
& event
);
98 void OnButtonAddSeveral(wxCommandEvent
& event
);
99 void OnButtonAddMany(wxCommandEvent
& event
);
101 void OnListbox(wxCommandEvent
& event
);
102 void OnListboxDClick(wxCommandEvent
& event
);
103 void OnCheckListbox(wxCommandEvent
& event
);
105 void OnCheckOrRadioBox(wxCommandEvent
& event
);
107 void OnUpdateUIAddSeveral(wxUpdateUIEvent
& event
);
108 void OnUpdateUIClearButton(wxUpdateUIEvent
& event
);
109 void OnUpdateUIDeleteButton(wxUpdateUIEvent
& event
);
110 void OnUpdateUIDeleteSelButton(wxUpdateUIEvent
& event
);
111 void OnUpdateUIResetButton(wxUpdateUIEvent
& event
);
113 // reset the listbox parameters
116 // (re)create the listbox
119 // listbox parameters
120 // ------------------
122 // the selection mode
130 // should it be sorted?
133 // should it have horz scroll/vert scrollbar permanently shown?
140 // the sel mode radiobox
141 wxRadioBox
*m_radioSelMode
;
144 wxCheckBox
*m_chkVScroll
,
150 // the listbox itself and the sizer it is in
158 wxSizer
*m_sizerLbox
;
160 // the text entries for "Add/change string" and "Delete" buttons
161 wxTextCtrl
*m_textAdd
,
166 DECLARE_EVENT_TABLE()
167 DECLARE_WIDGETS_PAGE(ListboxWidgetsPage
)
170 // ----------------------------------------------------------------------------
172 // ----------------------------------------------------------------------------
174 BEGIN_EVENT_TABLE(ListboxWidgetsPage
, WidgetsPage
)
175 EVT_BUTTON(ListboxPage_Reset
, ListboxWidgetsPage::OnButtonReset
)
176 EVT_BUTTON(ListboxPage_Change
, ListboxWidgetsPage::OnButtonChange
)
177 EVT_BUTTON(ListboxPage_Delete
, ListboxWidgetsPage::OnButtonDelete
)
178 EVT_BUTTON(ListboxPage_DeleteSel
, ListboxWidgetsPage::OnButtonDeleteSel
)
179 EVT_BUTTON(ListboxPage_Clear
, ListboxWidgetsPage::OnButtonClear
)
180 EVT_BUTTON(ListboxPage_Add
, ListboxWidgetsPage::OnButtonAdd
)
181 EVT_BUTTON(ListboxPage_AddSeveral
, ListboxWidgetsPage::OnButtonAddSeveral
)
182 EVT_BUTTON(ListboxPage_AddMany
, ListboxWidgetsPage::OnButtonAddMany
)
183 EVT_BUTTON(ListboxPage_ContainerTests
, ItemContainerWidgetsPage::OnButtonTestItemContainer
)
185 EVT_TEXT_ENTER(ListboxPage_AddText
, ListboxWidgetsPage::OnButtonAdd
)
186 EVT_TEXT_ENTER(ListboxPage_DeleteText
, ListboxWidgetsPage::OnButtonDelete
)
188 EVT_UPDATE_UI(ListboxPage_Reset
, ListboxWidgetsPage::OnUpdateUIResetButton
)
189 EVT_UPDATE_UI(ListboxPage_AddSeveral
, ListboxWidgetsPage::OnUpdateUIAddSeveral
)
190 EVT_UPDATE_UI(ListboxPage_Clear
, ListboxWidgetsPage::OnUpdateUIClearButton
)
191 EVT_UPDATE_UI(ListboxPage_DeleteText
, ListboxWidgetsPage::OnUpdateUIClearButton
)
192 EVT_UPDATE_UI(ListboxPage_Delete
, ListboxWidgetsPage::OnUpdateUIDeleteButton
)
193 EVT_UPDATE_UI(ListboxPage_Change
, ListboxWidgetsPage::OnUpdateUIDeleteSelButton
)
194 EVT_UPDATE_UI(ListboxPage_ChangeText
, ListboxWidgetsPage::OnUpdateUIDeleteSelButton
)
195 EVT_UPDATE_UI(ListboxPage_DeleteSel
, ListboxWidgetsPage::OnUpdateUIDeleteSelButton
)
197 EVT_LISTBOX(ListboxPage_Listbox
, ListboxWidgetsPage::OnListbox
)
198 EVT_LISTBOX_DCLICK(ListboxPage_Listbox
, ListboxWidgetsPage::OnListboxDClick
)
199 EVT_CHECKLISTBOX(ListboxPage_Listbox
, ListboxWidgetsPage::OnCheckListbox
)
201 EVT_CHECKBOX(wxID_ANY
, ListboxWidgetsPage::OnCheckOrRadioBox
)
202 EVT_RADIOBOX(wxID_ANY
, ListboxWidgetsPage::OnCheckOrRadioBox
)
205 // ============================================================================
207 // ============================================================================
209 #if defined(__WXUNIVERSAL__)
210 #define FAMILY_CTRLS UNIVERSAL_CTRLS
212 #define FAMILY_CTRLS NATIVE_CTRLS
215 IMPLEMENT_WIDGETS_PAGE(ListboxWidgetsPage
, _T("Listbox"),
216 FAMILY_CTRLS
| WITH_ITEMS_CTRLS
219 ListboxWidgetsPage::ListboxWidgetsPage(WidgetsBookCtrl
*book
,
220 wxImageList
*imaglist
)
221 : ItemContainerWidgetsPage(book
, imaglist
, listbox_xpm
)
224 m_radioSelMode
= (wxRadioBox
*)NULL
;
230 m_chkOwnerDraw
= (wxCheckBox
*)NULL
;
233 m_sizerLbox
= (wxSizer
*)NULL
;
237 void ListboxWidgetsPage::CreateContent()
240 What we create here is a frame having 3 panes: style pane is the
241 leftmost one, in the middle the pane with buttons allowing to perform
242 miscellaneous listbox operations and the pane containing the listbox
245 wxSizer
*sizerTop
= new wxBoxSizer(wxHORIZONTAL
);
248 wxStaticBox
*box
= new wxStaticBox(this, wxID_ANY
,
249 _T("&Set listbox parameters"));
250 wxSizer
*sizerLeft
= new wxStaticBoxSizer(box
, wxVERTICAL
);
252 static const wxString modes
[] =
259 m_radioSelMode
= new wxRadioBox(this, wxID_ANY
, _T("Selection &mode:"),
260 wxDefaultPosition
, wxDefaultSize
,
261 WXSIZEOF(modes
), modes
,
262 1, wxRA_SPECIFY_COLS
);
264 m_chkVScroll
= CreateCheckBoxAndAddToSizer
267 _T("Always show &vertical scrollbar")
269 m_chkHScroll
= CreateCheckBoxAndAddToSizer
272 _T("Show &horizontal scrollbar")
274 m_chkCheck
= CreateCheckBoxAndAddToSizer(sizerLeft
, _T("&Check list box"));
275 m_chkSort
= CreateCheckBoxAndAddToSizer(sizerLeft
, _T("&Sort items"));
276 m_chkOwnerDraw
= CreateCheckBoxAndAddToSizer(sizerLeft
, _T("&Owner drawn"));
278 sizerLeft
->Add(5, 5, 0, wxGROW
| wxALL
, 5); // spacer
279 sizerLeft
->Add(m_radioSelMode
, 0, wxGROW
| wxALL
, 5);
281 wxButton
*btn
= new wxButton(this, ListboxPage_Reset
, _T("&Reset"));
282 sizerLeft
->Add(btn
, 0, wxALIGN_CENTRE_HORIZONTAL
| wxALL
, 15);
285 wxStaticBox
*box2
= new wxStaticBox(this, wxID_ANY
,
286 _T("&Change listbox contents"));
287 wxSizer
*sizerMiddle
= new wxStaticBoxSizer(box2
, wxVERTICAL
);
289 wxSizer
*sizerRow
= new wxBoxSizer(wxHORIZONTAL
);
290 btn
= new wxButton(this, ListboxPage_Add
, _T("&Add this string"));
291 m_textAdd
= new wxTextCtrl(this, ListboxPage_AddText
, _T("test item 0"));
292 sizerRow
->Add(btn
, 0, wxRIGHT
, 5);
293 sizerRow
->Add(m_textAdd
, 1, wxLEFT
, 5);
294 sizerMiddle
->Add(sizerRow
, 0, wxALL
| wxGROW
, 5);
296 btn
= new wxButton(this, ListboxPage_AddSeveral
, _T("&Insert a few strings"));
297 sizerMiddle
->Add(btn
, 0, wxALL
| wxGROW
, 5);
299 btn
= new wxButton(this, ListboxPage_AddMany
, _T("Add &many strings"));
300 sizerMiddle
->Add(btn
, 0, wxALL
| wxGROW
, 5);
302 sizerRow
= new wxBoxSizer(wxHORIZONTAL
);
303 btn
= new wxButton(this, ListboxPage_Change
, _T("C&hange current"));
304 m_textChange
= new wxTextCtrl(this, ListboxPage_ChangeText
, wxEmptyString
);
305 sizerRow
->Add(btn
, 0, wxRIGHT
, 5);
306 sizerRow
->Add(m_textChange
, 1, wxLEFT
, 5);
307 sizerMiddle
->Add(sizerRow
, 0, wxALL
| wxGROW
, 5);
309 sizerRow
= new wxBoxSizer(wxHORIZONTAL
);
310 btn
= new wxButton(this, ListboxPage_Delete
, _T("&Delete this item"));
311 m_textDelete
= new wxTextCtrl(this, ListboxPage_DeleteText
, wxEmptyString
);
312 sizerRow
->Add(btn
, 0, wxRIGHT
, 5);
313 sizerRow
->Add(m_textDelete
, 1, wxLEFT
, 5);
314 sizerMiddle
->Add(sizerRow
, 0, wxALL
| wxGROW
, 5);
316 btn
= new wxButton(this, ListboxPage_DeleteSel
, _T("Delete &selection"));
317 sizerMiddle
->Add(btn
, 0, wxALL
| wxGROW
, 5);
319 btn
= new wxButton(this, ListboxPage_Clear
, _T("&Clear"));
320 sizerMiddle
->Add(btn
, 0, wxALL
| wxGROW
, 5);
322 btn
= new wxButton(this, ListboxPage_ContainerTests
, _T("Run &tests"));
323 sizerMiddle
->Add(btn
, 0, wxALL
| wxGROW
, 5);
326 wxSizer
*sizerRight
= new wxBoxSizer(wxVERTICAL
);
327 m_lbox
= new wxListBox(this, ListboxPage_Listbox
,
328 wxDefaultPosition
, wxDefaultSize
,
331 sizerRight
->Add(m_lbox
, 1, wxGROW
| wxALL
, 5);
332 sizerRight
->SetMinSize(150, 0);
333 m_sizerLbox
= sizerRight
; // save it to modify it later
335 // the 3 panes panes compose the window
336 sizerTop
->Add(sizerLeft
, 0, wxGROW
| (wxALL
& ~wxLEFT
), 10);
337 sizerTop
->Add(sizerMiddle
, 1, wxGROW
| wxALL
, 10);
338 sizerTop
->Add(sizerRight
, 1, wxGROW
| (wxALL
& ~wxRIGHT
), 10);
340 // final initializations
346 // ----------------------------------------------------------------------------
348 // ----------------------------------------------------------------------------
350 void ListboxWidgetsPage::Reset()
352 m_radioSelMode
->SetSelection(LboxSel_Single
);
353 m_chkVScroll
->SetValue(false);
354 m_chkHScroll
->SetValue(true);
355 m_chkCheck
->SetValue(false);
356 m_chkSort
->SetValue(false);
357 m_chkOwnerDraw
->SetValue(false);
360 void ListboxWidgetsPage::CreateLbox()
362 int flags
= ms_defaultFlags
;
363 switch ( m_radioSelMode
->GetSelection() )
366 wxFAIL_MSG( _T("unexpected radio box selection") );
368 case LboxSel_Single
: flags
|= wxLB_SINGLE
; break;
369 case LboxSel_Extended
: flags
|= wxLB_EXTENDED
; break;
370 case LboxSel_Multiple
: flags
|= wxLB_MULTIPLE
; break;
373 if ( m_chkVScroll
->GetValue() )
374 flags
|= wxLB_ALWAYS_SB
;
375 if ( m_chkHScroll
->GetValue() )
376 flags
|= wxLB_HSCROLL
;
377 if ( m_chkSort
->GetValue() )
379 if ( m_chkOwnerDraw
->GetValue() )
380 flags
|= wxLB_OWNERDRAW
;
385 int count
= m_lbox
->GetCount();
386 for ( int n
= 0; n
< count
; n
++ )
388 items
.Add(m_lbox
->GetString(n
));
391 m_sizerLbox
->Detach( m_lbox
);
395 #if wxUSE_CHECKLISTBOX
396 if ( m_chkCheck
->GetValue() )
398 m_lbox
= new wxCheckListBox(this, ListboxPage_Listbox
,
399 wxDefaultPosition
, wxDefaultSize
,
403 else // just a listbox
406 m_lbox
= new wxListBox(this, ListboxPage_Listbox
,
407 wxDefaultPosition
, wxDefaultSize
,
413 m_sizerLbox
->Add(m_lbox
, 1, wxGROW
| wxALL
, 5);
414 m_sizerLbox
->Layout();
417 // ----------------------------------------------------------------------------
419 // ----------------------------------------------------------------------------
421 void ListboxWidgetsPage::OnButtonReset(wxCommandEvent
& WXUNUSED(event
))
428 void ListboxWidgetsPage::OnButtonChange(wxCommandEvent
& WXUNUSED(event
))
430 wxArrayInt selections
;
431 int count
= m_lbox
->GetSelections(selections
);
432 wxString s
= m_textChange
->GetValue();
433 for ( int n
= 0; n
< count
; n
++ )
435 m_lbox
->SetString(selections
[n
], s
);
439 void ListboxWidgetsPage::OnButtonDelete(wxCommandEvent
& WXUNUSED(event
))
442 if ( !m_textDelete
->GetValue().ToULong(&n
) ||
443 (n
>= (unsigned)m_lbox
->GetCount()) )
451 void ListboxWidgetsPage::OnButtonDeleteSel(wxCommandEvent
& WXUNUSED(event
))
453 wxArrayInt selections
;
454 int n
= m_lbox
->GetSelections(selections
);
457 m_lbox
->Delete(selections
[--n
]);
461 void ListboxWidgetsPage::OnButtonClear(wxCommandEvent
& WXUNUSED(event
))
466 void ListboxWidgetsPage::OnButtonAdd(wxCommandEvent
& WXUNUSED(event
))
468 static unsigned int s_item
= 0;
470 wxString s
= m_textAdd
->GetValue();
471 if ( !m_textAdd
->IsModified() )
473 // update the default string
474 m_textAdd
->SetValue(wxString::Format(_T("test item %u"), ++s_item
));
480 void ListboxWidgetsPage::OnButtonAddMany(wxCommandEvent
& WXUNUSED(event
))
482 // "many" means 1000 here
483 for ( unsigned int n
= 0; n
< 1000; n
++ )
485 m_lbox
->Append(wxString::Format(_T("item #%u"), n
));
489 void ListboxWidgetsPage::OnButtonAddSeveral(wxCommandEvent
& WXUNUSED(event
))
492 items
.Add(_T("First"));
493 items
.Add(_T("another one"));
494 items
.Add(_T("and the last (very very very very very very very very very very long) one"));
495 m_lbox
->InsertItems(items
, 0);
498 void ListboxWidgetsPage::OnUpdateUIResetButton(wxUpdateUIEvent
& event
)
500 event
.Enable( (m_radioSelMode
->GetSelection() != LboxSel_Single
) ||
501 m_chkSort
->GetValue() ||
502 m_chkOwnerDraw
->GetValue() ||
503 !m_chkHScroll
->GetValue() ||
504 m_chkVScroll
->GetValue() );
507 void ListboxWidgetsPage::OnUpdateUIDeleteButton(wxUpdateUIEvent
& event
)
510 event
.Enable(m_textDelete
->GetValue().ToULong(&n
) &&
511 (n
< (unsigned)m_lbox
->GetCount()));
514 void ListboxWidgetsPage::OnUpdateUIDeleteSelButton(wxUpdateUIEvent
& event
)
516 wxArrayInt selections
;
517 event
.Enable(m_lbox
->GetSelections(selections
) != 0);
520 void ListboxWidgetsPage::OnUpdateUIClearButton(wxUpdateUIEvent
& event
)
522 event
.Enable(m_lbox
->GetCount() != 0);
525 void ListboxWidgetsPage::OnUpdateUIAddSeveral(wxUpdateUIEvent
& event
)
527 event
.Enable(!(m_lbox
->GetWindowStyle() & wxLB_SORT
));
530 void ListboxWidgetsPage::OnListbox(wxCommandEvent
& event
)
532 long sel
= event
.GetSelection();
533 m_textDelete
->SetValue(wxString::Format(_T("%ld"), sel
));
535 if (event
.IsSelection())
536 wxLogMessage(_T("Listbox item %ld selected"), sel
);
538 wxLogMessage(_T("Listbox item %ld deselected"), sel
);
541 void ListboxWidgetsPage::OnListboxDClick(wxCommandEvent
& event
)
543 wxLogMessage( _T("Listbox item %d double clicked"), event
.GetInt() );
546 void ListboxWidgetsPage::OnCheckListbox(wxCommandEvent
& event
)
548 wxLogMessage( _T("Listbox item %d toggled"), event
.GetInt() );
551 void ListboxWidgetsPage::OnCheckOrRadioBox(wxCommandEvent
& WXUNUSED(event
))
556 #endif // wxUSE_LISTBOX