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"
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(wxID_ANY
, ListboxWidgetsPage::OnCheckOrRadioBox
)
183 EVT_RADIOBOX(wxID_ANY
, 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, wxID_ANY
,
219 _T("&Set listbox parameters"));
220 wxSizer
*sizerLeft
= new wxStaticBoxSizer(box
, wxVERTICAL
);
222 static const wxString modes
[] =
229 m_radioSelMode
= new wxRadioBox(this, wxID_ANY
, _T("Selection &mode:"),
230 wxDefaultPosition
, wxDefaultSize
,
231 WXSIZEOF(modes
), modes
,
232 1, wxRA_SPECIFY_COLS
);
234 m_chkVScroll
= CreateCheckBoxAndAddToSizer
237 _T("Always show &vertical scrollbar")
239 m_chkHScroll
= CreateCheckBoxAndAddToSizer
242 _T("Show &horizontal scrollbar")
244 m_chkCheck
= CreateCheckBoxAndAddToSizer(sizerLeft
, _T("&Check list box"));
245 m_chkSort
= CreateCheckBoxAndAddToSizer(sizerLeft
, _T("&Sort items"));
247 sizerLeft
->Add(5, 5, 0, wxGROW
| wxALL
, 5); // spacer
248 sizerLeft
->Add(m_radioSelMode
, 0, wxGROW
| wxALL
, 5);
250 wxButton
*btn
= new wxButton(this, ListboxPage_Reset
, _T("&Reset"));
251 sizerLeft
->Add(btn
, 0, wxALIGN_CENTRE_HORIZONTAL
| wxALL
, 15);
254 wxStaticBox
*box2
= new wxStaticBox(this, wxID_ANY
,
255 _T("&Change listbox contents"));
256 wxSizer
*sizerMiddle
= new wxStaticBoxSizer(box2
, wxVERTICAL
);
258 wxSizer
*sizerRow
= new wxBoxSizer(wxHORIZONTAL
);
259 btn
= new wxButton(this, ListboxPage_Add
, _T("&Add this string"));
260 m_textAdd
= new wxTextCtrl(this, ListboxPage_AddText
, _T("test item 0"));
261 sizerRow
->Add(btn
, 0, wxRIGHT
, 5);
262 sizerRow
->Add(m_textAdd
, 1, wxLEFT
, 5);
263 sizerMiddle
->Add(sizerRow
, 0, wxALL
| wxGROW
, 5);
265 btn
= new wxButton(this, ListboxPage_AddSeveral
, _T("&Insert a few strings"));
266 sizerMiddle
->Add(btn
, 0, wxALL
| wxGROW
, 5);
268 btn
= new wxButton(this, ListboxPage_AddMany
, _T("Add &many strings"));
269 sizerMiddle
->Add(btn
, 0, wxALL
| wxGROW
, 5);
271 sizerRow
= new wxBoxSizer(wxHORIZONTAL
);
272 btn
= new wxButton(this, ListboxPage_Change
, _T("C&hange current"));
273 m_textChange
= new wxTextCtrl(this, ListboxPage_ChangeText
, wxEmptyString
);
274 sizerRow
->Add(btn
, 0, wxRIGHT
, 5);
275 sizerRow
->Add(m_textChange
, 1, wxLEFT
, 5);
276 sizerMiddle
->Add(sizerRow
, 0, wxALL
| wxGROW
, 5);
278 sizerRow
= new wxBoxSizer(wxHORIZONTAL
);
279 btn
= new wxButton(this, ListboxPage_Delete
, _T("&Delete this item"));
280 m_textDelete
= new wxTextCtrl(this, ListboxPage_DeleteText
, wxEmptyString
);
281 sizerRow
->Add(btn
, 0, wxRIGHT
, 5);
282 sizerRow
->Add(m_textDelete
, 1, wxLEFT
, 5);
283 sizerMiddle
->Add(sizerRow
, 0, wxALL
| wxGROW
, 5);
285 btn
= new wxButton(this, ListboxPage_DeleteSel
, _T("Delete &selection"));
286 sizerMiddle
->Add(btn
, 0, wxALL
| wxGROW
, 5);
288 btn
= new wxButton(this, ListboxPage_Clear
, _T("&Clear"));
289 sizerMiddle
->Add(btn
, 0, wxALL
| wxGROW
, 5);
292 wxSizer
*sizerRight
= new wxBoxSizer(wxVERTICAL
);
293 m_lbox
= new wxListBox(this, ListboxPage_Listbox
,
294 wxDefaultPosition
, wxDefaultSize
,
297 sizerRight
->Add(m_lbox
, 1, wxGROW
| wxALL
, 5);
298 sizerRight
->SetMinSize(150, 0);
299 m_sizerLbox
= sizerRight
; // save it to modify it later
301 // the 3 panes panes compose the window
302 sizerTop
->Add(sizerLeft
, 0, wxGROW
| (wxALL
& ~wxLEFT
), 10);
303 sizerTop
->Add(sizerMiddle
, 1, wxGROW
| wxALL
, 10);
304 sizerTop
->Add(sizerRight
, 1, wxGROW
| (wxALL
& ~wxRIGHT
), 10);
306 // final initializations
314 // ----------------------------------------------------------------------------
316 // ----------------------------------------------------------------------------
318 void ListboxWidgetsPage::Reset()
320 m_radioSelMode
->SetSelection(LboxSel_Single
);
321 m_chkSort
->SetValue(false);
322 m_chkCheck
->SetValue(false);
323 m_chkHScroll
->SetValue(true);
324 m_chkVScroll
->SetValue(false);
327 void ListboxWidgetsPage::CreateLbox()
330 switch ( m_radioSelMode
->GetSelection() )
333 wxFAIL_MSG( _T("unexpected radio box selection") );
335 case LboxSel_Single
: flags
|= wxLB_SINGLE
; break;
336 case LboxSel_Extended
: flags
|= wxLB_EXTENDED
; break;
337 case LboxSel_Multiple
: flags
|= wxLB_MULTIPLE
; break;
340 if ( m_chkVScroll
->GetValue() )
341 flags
|= wxLB_ALWAYS_SB
;
342 if ( m_chkHScroll
->GetValue() )
343 flags
|= wxLB_HSCROLL
;
344 if ( m_chkSort
->GetValue() )
350 int count
= m_lbox
->GetCount();
351 for ( int n
= 0; n
< count
; n
++ )
353 items
.Add(m_lbox
->GetString(n
));
356 m_sizerLbox
->Detach( m_lbox
);
360 #if wxUSE_CHECKLISTBOX
361 if ( m_chkCheck
->GetValue() )
363 m_lbox
= new wxCheckListBox(this, ListboxPage_Listbox
,
364 wxDefaultPosition
, wxDefaultSize
,
368 else // just a listbox
371 m_lbox
= new wxListBox(this, ListboxPage_Listbox
,
372 wxDefaultPosition
, wxDefaultSize
,
378 m_sizerLbox
->Add(m_lbox
, 1, wxGROW
| wxALL
, 5);
379 m_sizerLbox
->Layout();
382 // ----------------------------------------------------------------------------
384 // ----------------------------------------------------------------------------
386 void ListboxWidgetsPage::OnButtonReset(wxCommandEvent
& WXUNUSED(event
))
393 void ListboxWidgetsPage::OnButtonChange(wxCommandEvent
& WXUNUSED(event
))
395 wxArrayInt selections
;
396 int count
= m_lbox
->GetSelections(selections
);
397 wxString s
= m_textChange
->GetValue();
398 for ( int n
= 0; n
< count
; n
++ )
400 m_lbox
->SetString(selections
[n
], s
);
404 void ListboxWidgetsPage::OnButtonDelete(wxCommandEvent
& WXUNUSED(event
))
407 if ( !m_textDelete
->GetValue().ToULong(&n
) ||
408 (n
>= (unsigned)m_lbox
->GetCount()) )
416 void ListboxWidgetsPage::OnButtonDeleteSel(wxCommandEvent
& WXUNUSED(event
))
418 wxArrayInt selections
;
419 int n
= m_lbox
->GetSelections(selections
);
422 m_lbox
->Delete(selections
[--n
]);
426 void ListboxWidgetsPage::OnButtonClear(wxCommandEvent
& WXUNUSED(event
))
431 void ListboxWidgetsPage::OnButtonAdd(wxCommandEvent
& WXUNUSED(event
))
433 static unsigned int s_item
= 0;
435 wxString s
= m_textAdd
->GetValue();
436 if ( !m_textAdd
->IsModified() )
438 // update the default string
439 m_textAdd
->SetValue(wxString::Format(_T("test item %u"), ++s_item
));
445 void ListboxWidgetsPage::OnButtonAddMany(wxCommandEvent
& WXUNUSED(event
))
447 // "many" means 1000 here
448 for ( unsigned int n
= 0; n
< 1000; n
++ )
450 m_lbox
->Append(wxString::Format(_T("item #%u"), n
));
454 void ListboxWidgetsPage::OnButtonAddSeveral(wxCommandEvent
& WXUNUSED(event
))
457 items
.Add(_T("First"));
458 items
.Add(_T("another one"));
459 items
.Add(_T("and the last (very very very very very very very very very very long) one"));
460 m_lbox
->InsertItems(items
, 0);
463 void ListboxWidgetsPage::OnUpdateUIResetButton(wxUpdateUIEvent
& event
)
465 event
.Enable( (m_radioSelMode
->GetSelection() != LboxSel_Single
) ||
466 m_chkSort
->GetValue() ||
467 !m_chkHScroll
->GetValue() ||
468 m_chkVScroll
->GetValue() );
471 void ListboxWidgetsPage::OnUpdateUIDeleteButton(wxUpdateUIEvent
& event
)
474 event
.Enable(m_textDelete
->GetValue().ToULong(&n
) &&
475 (n
< (unsigned)m_lbox
->GetCount()));
478 void ListboxWidgetsPage::OnUpdateUIDeleteSelButton(wxUpdateUIEvent
& event
)
480 wxArrayInt selections
;
481 event
.Enable(m_lbox
->GetSelections(selections
) != 0);
484 void ListboxWidgetsPage::OnUpdateUIClearButton(wxUpdateUIEvent
& event
)
486 event
.Enable(m_lbox
->GetCount() != 0);
489 void ListboxWidgetsPage::OnUpdateUIAddSeveral(wxUpdateUIEvent
& event
)
491 event
.Enable(!(m_lbox
->GetWindowStyle() & wxLB_SORT
));
494 void ListboxWidgetsPage::OnListbox(wxCommandEvent
& event
)
496 long sel
= event
.GetSelection();
497 m_textDelete
->SetValue(wxString::Format(_T("%ld"), sel
));
499 if (event
.IsSelection())
500 wxLogMessage(_T("Listbox item %ld selected"), sel
);
502 wxLogMessage(_T("Listbox item %ld deselected"), sel
);
505 void ListboxWidgetsPage::OnListboxDClick(wxCommandEvent
& event
)
507 wxLogMessage( _T("Listbox item %ld double clicked"), event
.GetInt() );
510 void ListboxWidgetsPage::OnCheckListbox(wxCommandEvent
& event
)
512 wxLogMessage( _T("Listbox item %ld toggled"), event
.GetInt() );
515 void ListboxWidgetsPage::OnCheckOrRadioBox(wxCommandEvent
& WXUNUSED(event
))