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
= wxID_HIGHEST
,
61 ListboxPage_AddSeveral
,
65 ListboxPage_ChangeText
,
67 ListboxPage_DeleteText
,
68 ListboxPage_DeleteSel
,
72 // ----------------------------------------------------------------------------
74 // ----------------------------------------------------------------------------
76 class ListboxWidgetsPage
: public WidgetsPage
79 ListboxWidgetsPage(WidgetsBookCtrl
*book
, wxImageList
*imaglist
);
81 virtual wxControl
*GetWidget() const { return m_lbox
; }
82 virtual void RecreateWidget() { CreateLbox(); }
86 void OnButtonReset(wxCommandEvent
& event
);
87 void OnButtonChange(wxCommandEvent
& event
);
88 void OnButtonDelete(wxCommandEvent
& event
);
89 void OnButtonDeleteSel(wxCommandEvent
& event
);
90 void OnButtonClear(wxCommandEvent
& event
);
91 void OnButtonAdd(wxCommandEvent
& event
);
92 void OnButtonAddSeveral(wxCommandEvent
& event
);
93 void OnButtonAddMany(wxCommandEvent
& event
);
95 void OnListbox(wxCommandEvent
& event
);
96 void OnListboxDClick(wxCommandEvent
& event
);
97 void OnCheckListbox(wxCommandEvent
& event
);
99 void OnCheckOrRadioBox(wxCommandEvent
& event
);
101 void OnUpdateUIAddSeveral(wxUpdateUIEvent
& event
);
102 void OnUpdateUIClearButton(wxUpdateUIEvent
& event
);
103 void OnUpdateUIDeleteButton(wxUpdateUIEvent
& event
);
104 void OnUpdateUIDeleteSelButton(wxUpdateUIEvent
& event
);
105 void OnUpdateUIResetButton(wxUpdateUIEvent
& event
);
107 // reset the listbox parameters
110 // (re)create the listbox
113 // listbox parameters
114 // ------------------
116 // the selection mode
124 // should it be sorted?
127 // should it have horz scroll/vert scrollbar permanently shown?
134 // the sel mode radiobox
135 wxRadioBox
*m_radioSelMode
;
138 wxCheckBox
*m_chkVScroll
,
144 // the listbox itself and the sizer it is in
145 wxListBoxBase
*m_lbox
;
146 wxSizer
*m_sizerLbox
;
148 // the text entries for "Add/change string" and "Delete" buttons
149 wxTextCtrl
*m_textAdd
,
154 DECLARE_EVENT_TABLE()
155 DECLARE_WIDGETS_PAGE(ListboxWidgetsPage
)
158 // ----------------------------------------------------------------------------
160 // ----------------------------------------------------------------------------
162 BEGIN_EVENT_TABLE(ListboxWidgetsPage
, WidgetsPage
)
163 EVT_BUTTON(ListboxPage_Reset
, ListboxWidgetsPage::OnButtonReset
)
164 EVT_BUTTON(ListboxPage_Change
, ListboxWidgetsPage::OnButtonChange
)
165 EVT_BUTTON(ListboxPage_Delete
, ListboxWidgetsPage::OnButtonDelete
)
166 EVT_BUTTON(ListboxPage_DeleteSel
, ListboxWidgetsPage::OnButtonDeleteSel
)
167 EVT_BUTTON(ListboxPage_Clear
, ListboxWidgetsPage::OnButtonClear
)
168 EVT_BUTTON(ListboxPage_Add
, ListboxWidgetsPage::OnButtonAdd
)
169 EVT_BUTTON(ListboxPage_AddSeveral
, ListboxWidgetsPage::OnButtonAddSeveral
)
170 EVT_BUTTON(ListboxPage_AddMany
, ListboxWidgetsPage::OnButtonAddMany
)
172 EVT_TEXT_ENTER(ListboxPage_AddText
, ListboxWidgetsPage::OnButtonAdd
)
173 EVT_TEXT_ENTER(ListboxPage_DeleteText
, ListboxWidgetsPage::OnButtonDelete
)
175 EVT_UPDATE_UI(ListboxPage_Reset
, ListboxWidgetsPage::OnUpdateUIResetButton
)
176 EVT_UPDATE_UI(ListboxPage_AddSeveral
, ListboxWidgetsPage::OnUpdateUIAddSeveral
)
177 EVT_UPDATE_UI(ListboxPage_Clear
, ListboxWidgetsPage::OnUpdateUIClearButton
)
178 EVT_UPDATE_UI(ListboxPage_DeleteText
, ListboxWidgetsPage::OnUpdateUIClearButton
)
179 EVT_UPDATE_UI(ListboxPage_Delete
, ListboxWidgetsPage::OnUpdateUIDeleteButton
)
180 EVT_UPDATE_UI(ListboxPage_Change
, ListboxWidgetsPage::OnUpdateUIDeleteSelButton
)
181 EVT_UPDATE_UI(ListboxPage_ChangeText
, ListboxWidgetsPage::OnUpdateUIDeleteSelButton
)
182 EVT_UPDATE_UI(ListboxPage_DeleteSel
, ListboxWidgetsPage::OnUpdateUIDeleteSelButton
)
184 EVT_LISTBOX(ListboxPage_Listbox
, ListboxWidgetsPage::OnListbox
)
185 EVT_LISTBOX_DCLICK(ListboxPage_Listbox
, ListboxWidgetsPage::OnListboxDClick
)
186 EVT_CHECKLISTBOX(ListboxPage_Listbox
, ListboxWidgetsPage::OnCheckListbox
)
188 EVT_CHECKBOX(wxID_ANY
, ListboxWidgetsPage::OnCheckOrRadioBox
)
189 EVT_RADIOBOX(wxID_ANY
, ListboxWidgetsPage::OnCheckOrRadioBox
)
192 // ============================================================================
194 // ============================================================================
196 #if defined(__WXUNIVERSAL__)
197 #define FAMILY_CTRLS UNIVERSAL_CTRLS
199 #define FAMILY_CTRLS NATIVE_CTRLS
202 IMPLEMENT_WIDGETS_PAGE(ListboxWidgetsPage
, _T("Listbox"),
203 FAMILY_CTRLS
| WITH_ITEMS_CTRLS
206 ListboxWidgetsPage::ListboxWidgetsPage(WidgetsBookCtrl
*book
,
207 wxImageList
*imaglist
)
208 : WidgetsPage(book
, imaglist
, listbox_xpm
)
211 m_radioSelMode
= (wxRadioBox
*)NULL
;
217 m_chkOwnerDraw
= (wxCheckBox
*)NULL
;
219 m_lbox
= (wxListBoxBase
*)NULL
;
220 m_sizerLbox
= (wxSizer
*)NULL
;
223 What we create here is a frame having 3 panes: style pane is the
224 leftmost one, in the middle the pane with buttons allowing to perform
225 miscellaneous listbox operations and the pane containing the listbox
228 wxSizer
*sizerTop
= new wxBoxSizer(wxHORIZONTAL
);
231 wxStaticBox
*box
= new wxStaticBox(this, wxID_ANY
,
232 _T("&Set listbox parameters"));
233 wxSizer
*sizerLeft
= new wxStaticBoxSizer(box
, wxVERTICAL
);
235 static const wxString modes
[] =
242 m_radioSelMode
= new wxRadioBox(this, wxID_ANY
, _T("Selection &mode:"),
243 wxDefaultPosition
, wxDefaultSize
,
244 WXSIZEOF(modes
), modes
,
245 1, wxRA_SPECIFY_COLS
);
247 m_chkVScroll
= CreateCheckBoxAndAddToSizer
250 _T("Always show &vertical scrollbar")
252 m_chkHScroll
= CreateCheckBoxAndAddToSizer
255 _T("Show &horizontal scrollbar")
257 m_chkCheck
= CreateCheckBoxAndAddToSizer(sizerLeft
, _T("&Check list box"));
258 m_chkSort
= CreateCheckBoxAndAddToSizer(sizerLeft
, _T("&Sort items"));
259 m_chkOwnerDraw
= CreateCheckBoxAndAddToSizer(sizerLeft
, _T("&Owner drawn"));
261 sizerLeft
->Add(5, 5, 0, wxGROW
| wxALL
, 5); // spacer
262 sizerLeft
->Add(m_radioSelMode
, 0, wxGROW
| wxALL
, 5);
264 wxButton
*btn
= new wxButton(this, ListboxPage_Reset
, _T("&Reset"));
265 sizerLeft
->Add(btn
, 0, wxALIGN_CENTRE_HORIZONTAL
| wxALL
, 15);
268 wxStaticBox
*box2
= new wxStaticBox(this, wxID_ANY
,
269 _T("&Change listbox contents"));
270 wxSizer
*sizerMiddle
= new wxStaticBoxSizer(box2
, wxVERTICAL
);
272 wxSizer
*sizerRow
= new wxBoxSizer(wxHORIZONTAL
);
273 btn
= new wxButton(this, ListboxPage_Add
, _T("&Add this string"));
274 m_textAdd
= new wxTextCtrl(this, ListboxPage_AddText
, _T("test item 0"));
275 sizerRow
->Add(btn
, 0, wxRIGHT
, 5);
276 sizerRow
->Add(m_textAdd
, 1, wxLEFT
, 5);
277 sizerMiddle
->Add(sizerRow
, 0, wxALL
| wxGROW
, 5);
279 btn
= new wxButton(this, ListboxPage_AddSeveral
, _T("&Insert a few strings"));
280 sizerMiddle
->Add(btn
, 0, wxALL
| wxGROW
, 5);
282 btn
= new wxButton(this, ListboxPage_AddMany
, _T("Add &many strings"));
283 sizerMiddle
->Add(btn
, 0, wxALL
| wxGROW
, 5);
285 sizerRow
= new wxBoxSizer(wxHORIZONTAL
);
286 btn
= new wxButton(this, ListboxPage_Change
, _T("C&hange current"));
287 m_textChange
= new wxTextCtrl(this, ListboxPage_ChangeText
, wxEmptyString
);
288 sizerRow
->Add(btn
, 0, wxRIGHT
, 5);
289 sizerRow
->Add(m_textChange
, 1, wxLEFT
, 5);
290 sizerMiddle
->Add(sizerRow
, 0, wxALL
| wxGROW
, 5);
292 sizerRow
= new wxBoxSizer(wxHORIZONTAL
);
293 btn
= new wxButton(this, ListboxPage_Delete
, _T("&Delete this item"));
294 m_textDelete
= new wxTextCtrl(this, ListboxPage_DeleteText
, wxEmptyString
);
295 sizerRow
->Add(btn
, 0, wxRIGHT
, 5);
296 sizerRow
->Add(m_textDelete
, 1, wxLEFT
, 5);
297 sizerMiddle
->Add(sizerRow
, 0, wxALL
| wxGROW
, 5);
299 btn
= new wxButton(this, ListboxPage_DeleteSel
, _T("Delete &selection"));
300 sizerMiddle
->Add(btn
, 0, wxALL
| wxGROW
, 5);
302 btn
= new wxButton(this, ListboxPage_Clear
, _T("&Clear"));
303 sizerMiddle
->Add(btn
, 0, wxALL
| wxGROW
, 5);
306 wxSizer
*sizerRight
= new wxBoxSizer(wxVERTICAL
);
307 m_lbox
= new wxListBox(this, ListboxPage_Listbox
,
308 wxDefaultPosition
, wxDefaultSize
,
311 sizerRight
->Add(m_lbox
, 1, wxGROW
| wxALL
, 5);
312 sizerRight
->SetMinSize(150, 0);
313 m_sizerLbox
= sizerRight
; // save it to modify it later
315 // the 3 panes panes compose the window
316 sizerTop
->Add(sizerLeft
, 0, wxGROW
| (wxALL
& ~wxLEFT
), 10);
317 sizerTop
->Add(sizerMiddle
, 1, wxGROW
| wxALL
, 10);
318 sizerTop
->Add(sizerRight
, 1, wxGROW
| (wxALL
& ~wxRIGHT
), 10);
320 // final initializations
328 // ----------------------------------------------------------------------------
330 // ----------------------------------------------------------------------------
332 void ListboxWidgetsPage::Reset()
334 m_radioSelMode
->SetSelection(LboxSel_Single
);
335 m_chkVScroll
->SetValue(false);
336 m_chkHScroll
->SetValue(true);
337 m_chkCheck
->SetValue(false);
338 m_chkSort
->SetValue(false);
339 m_chkOwnerDraw
->SetValue(false);
342 void ListboxWidgetsPage::CreateLbox()
344 int flags
= ms_defaultFlags
;
345 switch ( m_radioSelMode
->GetSelection() )
348 wxFAIL_MSG( _T("unexpected radio box selection") );
350 case LboxSel_Single
: flags
|= wxLB_SINGLE
; break;
351 case LboxSel_Extended
: flags
|= wxLB_EXTENDED
; break;
352 case LboxSel_Multiple
: flags
|= wxLB_MULTIPLE
; break;
355 if ( m_chkVScroll
->GetValue() )
356 flags
|= wxLB_ALWAYS_SB
;
357 if ( m_chkHScroll
->GetValue() )
358 flags
|= wxLB_HSCROLL
;
359 if ( m_chkSort
->GetValue() )
361 if ( m_chkOwnerDraw
->GetValue() )
362 flags
|= wxLB_OWNERDRAW
;
367 int count
= m_lbox
->GetCount();
368 for ( int n
= 0; n
< count
; n
++ )
370 items
.Add(m_lbox
->GetString(n
));
373 m_sizerLbox
->Detach( m_lbox
);
377 #if wxUSE_CHECKLISTBOX
378 if ( m_chkCheck
->GetValue() )
380 m_lbox
= new wxCheckListBox(this, ListboxPage_Listbox
,
381 wxDefaultPosition
, wxDefaultSize
,
385 else // just a listbox
388 m_lbox
= new wxListBox(this, ListboxPage_Listbox
,
389 wxDefaultPosition
, wxDefaultSize
,
395 m_sizerLbox
->Add(m_lbox
, 1, wxGROW
| wxALL
, 5);
396 m_sizerLbox
->Layout();
399 // ----------------------------------------------------------------------------
401 // ----------------------------------------------------------------------------
403 void ListboxWidgetsPage::OnButtonReset(wxCommandEvent
& WXUNUSED(event
))
410 void ListboxWidgetsPage::OnButtonChange(wxCommandEvent
& WXUNUSED(event
))
412 wxArrayInt selections
;
413 int count
= m_lbox
->GetSelections(selections
);
414 wxString s
= m_textChange
->GetValue();
415 for ( int n
= 0; n
< count
; n
++ )
417 m_lbox
->SetString(selections
[n
], s
);
421 void ListboxWidgetsPage::OnButtonDelete(wxCommandEvent
& WXUNUSED(event
))
424 if ( !m_textDelete
->GetValue().ToULong(&n
) ||
425 (n
>= (unsigned)m_lbox
->GetCount()) )
433 void ListboxWidgetsPage::OnButtonDeleteSel(wxCommandEvent
& WXUNUSED(event
))
435 wxArrayInt selections
;
436 int n
= m_lbox
->GetSelections(selections
);
439 m_lbox
->Delete(selections
[--n
]);
443 void ListboxWidgetsPage::OnButtonClear(wxCommandEvent
& WXUNUSED(event
))
448 void ListboxWidgetsPage::OnButtonAdd(wxCommandEvent
& WXUNUSED(event
))
450 static unsigned int s_item
= 0;
452 wxString s
= m_textAdd
->GetValue();
453 if ( !m_textAdd
->IsModified() )
455 // update the default string
456 m_textAdd
->SetValue(wxString::Format(_T("test item %u"), ++s_item
));
462 void ListboxWidgetsPage::OnButtonAddMany(wxCommandEvent
& WXUNUSED(event
))
464 // "many" means 1000 here
465 for ( unsigned int n
= 0; n
< 1000; n
++ )
467 m_lbox
->Append(wxString::Format(_T("item #%u"), n
));
471 void ListboxWidgetsPage::OnButtonAddSeveral(wxCommandEvent
& WXUNUSED(event
))
474 items
.Add(_T("First"));
475 items
.Add(_T("another one"));
476 items
.Add(_T("and the last (very very very very very very very very very very long) one"));
477 m_lbox
->InsertItems(items
, 0);
480 void ListboxWidgetsPage::OnUpdateUIResetButton(wxUpdateUIEvent
& event
)
482 event
.Enable( (m_radioSelMode
->GetSelection() != LboxSel_Single
) ||
483 m_chkSort
->GetValue() ||
484 m_chkOwnerDraw
->GetValue() ||
485 !m_chkHScroll
->GetValue() ||
486 m_chkVScroll
->GetValue() );
489 void ListboxWidgetsPage::OnUpdateUIDeleteButton(wxUpdateUIEvent
& event
)
492 event
.Enable(m_textDelete
->GetValue().ToULong(&n
) &&
493 (n
< (unsigned)m_lbox
->GetCount()));
496 void ListboxWidgetsPage::OnUpdateUIDeleteSelButton(wxUpdateUIEvent
& event
)
498 wxArrayInt selections
;
499 event
.Enable(m_lbox
->GetSelections(selections
) != 0);
502 void ListboxWidgetsPage::OnUpdateUIClearButton(wxUpdateUIEvent
& event
)
504 event
.Enable(m_lbox
->GetCount() != 0);
507 void ListboxWidgetsPage::OnUpdateUIAddSeveral(wxUpdateUIEvent
& event
)
509 event
.Enable(!(m_lbox
->GetWindowStyle() & wxLB_SORT
));
512 void ListboxWidgetsPage::OnListbox(wxCommandEvent
& event
)
514 long sel
= event
.GetSelection();
515 m_textDelete
->SetValue(wxString::Format(_T("%ld"), sel
));
517 if (event
.IsSelection())
518 wxLogMessage(_T("Listbox item %ld selected"), sel
);
520 wxLogMessage(_T("Listbox item %ld deselected"), sel
);
523 void ListboxWidgetsPage::OnListboxDClick(wxCommandEvent
& event
)
525 wxLogMessage( _T("Listbox item %d double clicked"), event
.GetInt() );
528 void ListboxWidgetsPage::OnCheckListbox(wxCommandEvent
& event
)
530 wxLogMessage( _T("Listbox item %d toggled"), event
.GetInt() );
533 void ListboxWidgetsPage::OnCheckOrRadioBox(wxCommandEvent
& WXUNUSED(event
))
538 #endif // wxUSE_LISTBOX