1 /////////////////////////////////////////////////////////////////////////////
2 // Program: wxWindows 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"
27 // for all others, include the necessary headers
31 #include "wx/bitmap.h"
32 #include "wx/button.h"
33 #include "wx/checkbox.h"
34 #include "wx/combobox.h"
35 #include "wx/listbox.h"
36 #include "wx/radiobox.h"
37 #include "wx/statbox.h"
38 #include "wx/textctrl.h"
43 #include "wx/checklst.h"
47 #include "icons/listbox.xpm"
49 // ----------------------------------------------------------------------------
51 // ----------------------------------------------------------------------------
56 ListboxPage_Reset
= 100,
59 ListboxPage_AddSeveral
,
63 ListboxPage_ChangeText
,
65 ListboxPage_DeleteText
,
66 ListboxPage_DeleteSel
,
70 // ----------------------------------------------------------------------------
72 // ----------------------------------------------------------------------------
74 class ListboxWidgetsPage
: public WidgetsPage
77 ListboxWidgetsPage(wxNotebook
*notebook
, wxImageList
*imaglist
);
81 void OnButtonReset(wxCommandEvent
& event
);
82 void OnButtonChange(wxCommandEvent
& event
);
83 void OnButtonDelete(wxCommandEvent
& event
);
84 void OnButtonDeleteSel(wxCommandEvent
& event
);
85 void OnButtonClear(wxCommandEvent
& event
);
86 void OnButtonAdd(wxCommandEvent
& event
);
87 void OnButtonAddSeveral(wxCommandEvent
& event
);
88 void OnButtonAddMany(wxCommandEvent
& event
);
90 void OnListbox(wxCommandEvent
& event
);
91 void OnListboxDClick(wxCommandEvent
& event
);
92 void OnCheckListbox(wxCommandEvent
& event
);
94 void OnCheckOrRadioBox(wxCommandEvent
& event
);
96 void OnUpdateUIAddSeveral(wxUpdateUIEvent
& event
);
97 void OnUpdateUIClearButton(wxUpdateUIEvent
& event
);
98 void OnUpdateUIDeleteButton(wxUpdateUIEvent
& event
);
99 void OnUpdateUIDeleteSelButton(wxUpdateUIEvent
& event
);
100 void OnUpdateUIResetButton(wxUpdateUIEvent
& event
);
102 // reset the listbox parameters
105 // (re)create the listbox
108 // listbox parameters
109 // ------------------
111 // the selection mode
119 // should it be sorted?
122 // should it have horz scroll/vert scrollbar permanently shown?
129 // the sel mode radiobox
130 wxRadioBox
*m_radioSelMode
;
133 wxCheckBox
*m_chkSort
,
138 // the listbox itself and the sizer it is in
140 wxSizer
*m_sizerLbox
;
142 // the text entries for "Add/change string" and "Delete" buttons
143 wxTextCtrl
*m_textAdd
,
148 DECLARE_EVENT_TABLE()
149 DECLARE_WIDGETS_PAGE(ListboxWidgetsPage
)
152 // ----------------------------------------------------------------------------
154 // ----------------------------------------------------------------------------
156 BEGIN_EVENT_TABLE(ListboxWidgetsPage
, WidgetsPage
)
157 EVT_BUTTON(ListboxPage_Reset
, ListboxWidgetsPage::OnButtonReset
)
158 EVT_BUTTON(ListboxPage_Change
, ListboxWidgetsPage::OnButtonChange
)
159 EVT_BUTTON(ListboxPage_Delete
, ListboxWidgetsPage::OnButtonDelete
)
160 EVT_BUTTON(ListboxPage_DeleteSel
, ListboxWidgetsPage::OnButtonDeleteSel
)
161 EVT_BUTTON(ListboxPage_Clear
, ListboxWidgetsPage::OnButtonClear
)
162 EVT_BUTTON(ListboxPage_Add
, ListboxWidgetsPage::OnButtonAdd
)
163 EVT_BUTTON(ListboxPage_AddSeveral
, ListboxWidgetsPage::OnButtonAddSeveral
)
164 EVT_BUTTON(ListboxPage_AddMany
, ListboxWidgetsPage::OnButtonAddMany
)
166 EVT_TEXT_ENTER(ListboxPage_AddText
, ListboxWidgetsPage::OnButtonAdd
)
167 EVT_TEXT_ENTER(ListboxPage_DeleteText
, ListboxWidgetsPage::OnButtonDelete
)
169 EVT_UPDATE_UI(ListboxPage_Reset
, ListboxWidgetsPage::OnUpdateUIResetButton
)
170 EVT_UPDATE_UI(ListboxPage_AddSeveral
, ListboxWidgetsPage::OnUpdateUIAddSeveral
)
171 EVT_UPDATE_UI(ListboxPage_Clear
, ListboxWidgetsPage::OnUpdateUIClearButton
)
172 EVT_UPDATE_UI(ListboxPage_DeleteText
, ListboxWidgetsPage::OnUpdateUIClearButton
)
173 EVT_UPDATE_UI(ListboxPage_Delete
, ListboxWidgetsPage::OnUpdateUIDeleteButton
)
174 EVT_UPDATE_UI(ListboxPage_Change
, ListboxWidgetsPage::OnUpdateUIDeleteSelButton
)
175 EVT_UPDATE_UI(ListboxPage_ChangeText
, ListboxWidgetsPage::OnUpdateUIDeleteSelButton
)
176 EVT_UPDATE_UI(ListboxPage_DeleteSel
, ListboxWidgetsPage::OnUpdateUIDeleteSelButton
)
178 EVT_LISTBOX(ListboxPage_Listbox
, ListboxWidgetsPage::OnListbox
)
179 EVT_LISTBOX_DCLICK(ListboxPage_Listbox
, ListboxWidgetsPage::OnListboxDClick
)
180 EVT_CHECKLISTBOX(ListboxPage_Listbox
, ListboxWidgetsPage::OnCheckListbox
)
182 EVT_CHECKBOX(-1, ListboxWidgetsPage::OnCheckOrRadioBox
)
183 EVT_RADIOBOX(-1, ListboxWidgetsPage::OnCheckOrRadioBox
)
186 // ============================================================================
188 // ============================================================================
190 IMPLEMENT_WIDGETS_PAGE(ListboxWidgetsPage
, _T("Listbox"));
192 ListboxWidgetsPage::ListboxWidgetsPage(wxNotebook
*notebook
,
193 wxImageList
*imaglist
)
194 : WidgetsPage(notebook
)
196 imaglist
->Add(wxBitmap(listbox_xpm
));
199 m_radioSelMode
= (wxRadioBox
*)NULL
;
204 m_chkSort
= (wxCheckBox
*)NULL
;
206 m_lbox
= (wxListBox
*)NULL
;
207 m_sizerLbox
= (wxSizer
*)NULL
;
210 What we create here is a frame having 3 panes: style pane is the
211 leftmost one, in the middle the pane with buttons allowing to perform
212 miscellaneous listbox operations and the pane containing the listbox
215 wxSizer
*sizerTop
= new wxBoxSizer(wxHORIZONTAL
);
218 wxStaticBox
*box
= new wxStaticBox(this, -1, _T("&Set listbox parameters"));
219 wxSizer
*sizerLeft
= new wxStaticBoxSizer(box
, wxVERTICAL
);
221 static const wxString modes
[] =
228 m_radioSelMode
= new wxRadioBox(this, -1, _T("Selection &mode:"),
229 wxDefaultPosition
, wxDefaultSize
,
230 WXSIZEOF(modes
), modes
,
231 1, wxRA_SPECIFY_COLS
);
233 m_chkVScroll
= CreateCheckBoxAndAddToSizer
236 _T("Always show &vertical scrollbar")
238 m_chkHScroll
= CreateCheckBoxAndAddToSizer
241 _T("Show &horizontal scrollbar")
243 m_chkCheck
= CreateCheckBoxAndAddToSizer(sizerLeft
, _T("&Check list box"));
244 m_chkSort
= CreateCheckBoxAndAddToSizer(sizerLeft
, _T("&Sort items"));
246 sizerLeft
->Add(5, 5, 0, wxGROW
| wxALL
, 5); // spacer
247 sizerLeft
->Add(m_radioSelMode
, 0, wxGROW
| wxALL
, 5);
249 wxButton
*btn
= new wxButton(this, ListboxPage_Reset
, _T("&Reset"));
250 sizerLeft
->Add(btn
, 0, wxALIGN_CENTRE_HORIZONTAL
| wxALL
, 15);
253 wxStaticBox
*box2
= new wxStaticBox(this, -1, _T("&Change listbox contents"));
254 wxSizer
*sizerMiddle
= new wxStaticBoxSizer(box2
, wxVERTICAL
);
256 wxSizer
*sizerRow
= new wxBoxSizer(wxHORIZONTAL
);
257 btn
= new wxButton(this, ListboxPage_Add
, _T("&Add this string"));
258 m_textAdd
= new wxTextCtrl(this, ListboxPage_AddText
, _T("test item 0"));
259 sizerRow
->Add(btn
, 0, wxRIGHT
, 5);
260 sizerRow
->Add(m_textAdd
, 1, wxLEFT
, 5);
261 sizerMiddle
->Add(sizerRow
, 0, wxALL
| wxGROW
, 5);
263 btn
= new wxButton(this, ListboxPage_AddSeveral
, _T("&Insert a few strings"));
264 sizerMiddle
->Add(btn
, 0, wxALL
| wxGROW
, 5);
266 btn
= new wxButton(this, ListboxPage_AddMany
, _T("Add &many strings"));
267 sizerMiddle
->Add(btn
, 0, wxALL
| wxGROW
, 5);
269 sizerRow
= new wxBoxSizer(wxHORIZONTAL
);
270 btn
= new wxButton(this, ListboxPage_Change
, _T("C&hange current"));
271 m_textChange
= new wxTextCtrl(this, ListboxPage_ChangeText
, _T(""));
272 sizerRow
->Add(btn
, 0, wxRIGHT
, 5);
273 sizerRow
->Add(m_textChange
, 1, wxLEFT
, 5);
274 sizerMiddle
->Add(sizerRow
, 0, wxALL
| wxGROW
, 5);
276 sizerRow
= new wxBoxSizer(wxHORIZONTAL
);
277 btn
= new wxButton(this, ListboxPage_Delete
, _T("&Delete this item"));
278 m_textDelete
= new wxTextCtrl(this, ListboxPage_DeleteText
, _T(""));
279 sizerRow
->Add(btn
, 0, wxRIGHT
, 5);
280 sizerRow
->Add(m_textDelete
, 1, wxLEFT
, 5);
281 sizerMiddle
->Add(sizerRow
, 0, wxALL
| wxGROW
, 5);
283 btn
= new wxButton(this, ListboxPage_DeleteSel
, _T("Delete &selection"));
284 sizerMiddle
->Add(btn
, 0, wxALL
| wxGROW
, 5);
286 btn
= new wxButton(this, ListboxPage_Clear
, _T("&Clear"));
287 sizerMiddle
->Add(btn
, 0, wxALL
| wxGROW
, 5);
290 wxSizer
*sizerRight
= new wxBoxSizer(wxVERTICAL
);
291 m_lbox
= new wxListBox(this, ListboxPage_Listbox
,
292 wxDefaultPosition
, wxDefaultSize
,
295 sizerRight
->Add(m_lbox
, 1, wxGROW
| wxALL
, 5);
296 sizerRight
->SetMinSize(150, 0);
297 m_sizerLbox
= sizerRight
; // save it to modify it later
299 // the 3 panes panes compose the window
300 sizerTop
->Add(sizerLeft
, 0, wxGROW
| (wxALL
& ~wxLEFT
), 10);
301 sizerTop
->Add(sizerMiddle
, 1, wxGROW
| wxALL
, 10);
302 sizerTop
->Add(sizerRight
, 1, wxGROW
| (wxALL
& ~wxRIGHT
), 10);
304 // final initializations
313 // ----------------------------------------------------------------------------
315 // ----------------------------------------------------------------------------
317 void ListboxWidgetsPage::Reset()
319 m_radioSelMode
->SetSelection(LboxSel_Single
);
320 m_chkSort
->SetValue(FALSE
);
321 m_chkCheck
->SetValue(FALSE
);
322 m_chkHScroll
->SetValue(TRUE
);
323 m_chkVScroll
->SetValue(FALSE
);
326 void ListboxWidgetsPage::CreateLbox()
329 switch ( m_radioSelMode
->GetSelection() )
332 wxFAIL_MSG( _T("unexpected radio box selection") );
334 case LboxSel_Single
: flags
|= wxLB_SINGLE
; break;
335 case LboxSel_Extended
: flags
|= wxLB_EXTENDED
; break;
336 case LboxSel_Multiple
: flags
|= wxLB_MULTIPLE
; break;
339 if ( m_chkVScroll
->GetValue() )
340 flags
|= wxLB_ALWAYS_SB
;
341 if ( m_chkHScroll
->GetValue() )
342 flags
|= wxLB_HSCROLL
;
343 if ( m_chkSort
->GetValue() )
349 int count
= m_lbox
->GetCount();
350 for ( int n
= 0; n
< count
; n
++ )
352 items
.Add(m_lbox
->GetString(n
));
355 m_sizerLbox
->Detach( m_lbox
);
359 #if wxUSE_CHECKLISTBOX
360 if ( m_chkCheck
->GetValue() )
362 m_lbox
= new wxCheckListBox(this, ListboxPage_Listbox
,
363 wxDefaultPosition
, wxDefaultSize
,
367 else // just a listbox
370 m_lbox
= new wxListBox(this, ListboxPage_Listbox
,
371 wxDefaultPosition
, wxDefaultSize
,
377 m_sizerLbox
->Add(m_lbox
, 1, wxGROW
| wxALL
, 5);
378 m_sizerLbox
->Layout();
381 // ----------------------------------------------------------------------------
383 // ----------------------------------------------------------------------------
385 void ListboxWidgetsPage::OnButtonReset(wxCommandEvent
& WXUNUSED(event
))
392 void ListboxWidgetsPage::OnButtonChange(wxCommandEvent
& WXUNUSED(event
))
394 wxArrayInt selections
;
395 int count
= m_lbox
->GetSelections(selections
);
396 wxString s
= m_textChange
->GetValue();
397 for ( int n
= 0; n
< count
; n
++ )
399 m_lbox
->SetString(selections
[n
], s
);
403 void ListboxWidgetsPage::OnButtonDelete(wxCommandEvent
& WXUNUSED(event
))
406 if ( !m_textDelete
->GetValue().ToULong(&n
) ||
407 (n
>= (unsigned)m_lbox
->GetCount()) )
415 void ListboxWidgetsPage::OnButtonDeleteSel(wxCommandEvent
& WXUNUSED(event
))
417 wxArrayInt selections
;
418 int n
= m_lbox
->GetSelections(selections
);
421 m_lbox
->Delete(selections
[--n
]);
425 void ListboxWidgetsPage::OnButtonClear(wxCommandEvent
& WXUNUSED(event
))
430 void ListboxWidgetsPage::OnButtonAdd(wxCommandEvent
& WXUNUSED(event
))
432 static unsigned int s_item
= 0;
434 wxString s
= m_textAdd
->GetValue();
435 if ( !m_textAdd
->IsModified() )
437 // update the default string
438 m_textAdd
->SetValue(wxString::Format(_T("test item %u"), ++s_item
));
444 void ListboxWidgetsPage::OnButtonAddMany(wxCommandEvent
& WXUNUSED(event
))
446 // "many" means 1000 here
447 for ( unsigned int n
= 0; n
< 1000; n
++ )
449 m_lbox
->Append(wxString::Format(_T("item #%u"), n
));
453 void ListboxWidgetsPage::OnButtonAddSeveral(wxCommandEvent
& WXUNUSED(event
))
456 items
.Add(_T("First"));
457 items
.Add(_T("another one"));
458 items
.Add(_T("and the last (very very very very very very very very very very long) one"));
459 m_lbox
->InsertItems(items
, 0);
462 void ListboxWidgetsPage::OnUpdateUIResetButton(wxUpdateUIEvent
& event
)
464 event
.Enable( (m_radioSelMode
->GetSelection() != LboxSel_Single
) ||
465 m_chkSort
->GetValue() ||
466 !m_chkHScroll
->GetValue() ||
467 m_chkVScroll
->GetValue() );
470 void ListboxWidgetsPage::OnUpdateUIDeleteButton(wxUpdateUIEvent
& event
)
473 event
.Enable(m_textDelete
->GetValue().ToULong(&n
) &&
474 (n
< (unsigned)m_lbox
->GetCount()));
477 void ListboxWidgetsPage::OnUpdateUIDeleteSelButton(wxUpdateUIEvent
& event
)
479 wxArrayInt selections
;
480 event
.Enable(m_lbox
->GetSelections(selections
) != 0);
483 void ListboxWidgetsPage::OnUpdateUIClearButton(wxUpdateUIEvent
& event
)
485 event
.Enable(m_lbox
->GetCount() != 0);
488 void ListboxWidgetsPage::OnUpdateUIAddSeveral(wxUpdateUIEvent
& event
)
490 event
.Enable(!(m_lbox
->GetWindowStyle() & wxLB_SORT
));
493 void ListboxWidgetsPage::OnListbox(wxCommandEvent
& event
)
495 long sel
= event
.GetInt();
496 m_textDelete
->SetValue(wxString::Format(_T("%ld"), sel
));
498 wxLogMessage(_T("Listbox item %ld selected"), sel
);
501 void ListboxWidgetsPage::OnListboxDClick(wxCommandEvent
& event
)
503 wxLogMessage( _T("Listbox item %ld double clicked"), event
.GetInt() );
506 void ListboxWidgetsPage::OnCheckListbox(wxCommandEvent
& event
)
508 wxLogMessage( _T("Listbox item %ld toggled"), event
.GetInt() );
511 void ListboxWidgetsPage::OnCheckOrRadioBox(wxCommandEvent
& WXUNUSED(event
))