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
= 100,
61 ListboxPage_AddSeveral
,
65 ListboxPage_ChangeText
,
67 ListboxPage_DeleteText
,
68 ListboxPage_DeleteSel
,
72 // ----------------------------------------------------------------------------
74 // ----------------------------------------------------------------------------
76 class ListboxWidgetsPage
: public WidgetsPage
79 ListboxWidgetsPage(wxNotebook
*notebook
, wxImageList
*imaglist
);
81 virtual wxControl
*GetWidget() const { return m_lbox
; }
85 void OnButtonReset(wxCommandEvent
& event
);
86 void OnButtonChange(wxCommandEvent
& event
);
87 void OnButtonDelete(wxCommandEvent
& event
);
88 void OnButtonDeleteSel(wxCommandEvent
& event
);
89 void OnButtonClear(wxCommandEvent
& event
);
90 void OnButtonAdd(wxCommandEvent
& event
);
91 void OnButtonAddSeveral(wxCommandEvent
& event
);
92 void OnButtonAddMany(wxCommandEvent
& event
);
94 void OnListbox(wxCommandEvent
& event
);
95 void OnListboxDClick(wxCommandEvent
& event
);
96 void OnCheckListbox(wxCommandEvent
& event
);
98 void OnCheckOrRadioBox(wxCommandEvent
& event
);
100 void OnUpdateUIAddSeveral(wxUpdateUIEvent
& event
);
101 void OnUpdateUIClearButton(wxUpdateUIEvent
& event
);
102 void OnUpdateUIDeleteButton(wxUpdateUIEvent
& event
);
103 void OnUpdateUIDeleteSelButton(wxUpdateUIEvent
& event
);
104 void OnUpdateUIResetButton(wxUpdateUIEvent
& event
);
106 // reset the listbox parameters
109 // (re)create the listbox
112 // listbox parameters
113 // ------------------
115 // the selection mode
123 // should it be sorted?
126 // should it have horz scroll/vert scrollbar permanently shown?
133 // the sel mode radiobox
134 wxRadioBox
*m_radioSelMode
;
137 wxCheckBox
*m_chkSort
,
142 // the listbox itself and the sizer it is in
144 wxSizer
*m_sizerLbox
;
146 // the text entries for "Add/change string" and "Delete" buttons
147 wxTextCtrl
*m_textAdd
,
152 DECLARE_EVENT_TABLE()
153 DECLARE_WIDGETS_PAGE(ListboxWidgetsPage
)
156 // ----------------------------------------------------------------------------
158 // ----------------------------------------------------------------------------
160 BEGIN_EVENT_TABLE(ListboxWidgetsPage
, WidgetsPage
)
161 EVT_BUTTON(ListboxPage_Reset
, ListboxWidgetsPage::OnButtonReset
)
162 EVT_BUTTON(ListboxPage_Change
, ListboxWidgetsPage::OnButtonChange
)
163 EVT_BUTTON(ListboxPage_Delete
, ListboxWidgetsPage::OnButtonDelete
)
164 EVT_BUTTON(ListboxPage_DeleteSel
, ListboxWidgetsPage::OnButtonDeleteSel
)
165 EVT_BUTTON(ListboxPage_Clear
, ListboxWidgetsPage::OnButtonClear
)
166 EVT_BUTTON(ListboxPage_Add
, ListboxWidgetsPage::OnButtonAdd
)
167 EVT_BUTTON(ListboxPage_AddSeveral
, ListboxWidgetsPage::OnButtonAddSeveral
)
168 EVT_BUTTON(ListboxPage_AddMany
, ListboxWidgetsPage::OnButtonAddMany
)
170 EVT_TEXT_ENTER(ListboxPage_AddText
, ListboxWidgetsPage::OnButtonAdd
)
171 EVT_TEXT_ENTER(ListboxPage_DeleteText
, ListboxWidgetsPage::OnButtonDelete
)
173 EVT_UPDATE_UI(ListboxPage_Reset
, ListboxWidgetsPage::OnUpdateUIResetButton
)
174 EVT_UPDATE_UI(ListboxPage_AddSeveral
, ListboxWidgetsPage::OnUpdateUIAddSeveral
)
175 EVT_UPDATE_UI(ListboxPage_Clear
, ListboxWidgetsPage::OnUpdateUIClearButton
)
176 EVT_UPDATE_UI(ListboxPage_DeleteText
, ListboxWidgetsPage::OnUpdateUIClearButton
)
177 EVT_UPDATE_UI(ListboxPage_Delete
, ListboxWidgetsPage::OnUpdateUIDeleteButton
)
178 EVT_UPDATE_UI(ListboxPage_Change
, ListboxWidgetsPage::OnUpdateUIDeleteSelButton
)
179 EVT_UPDATE_UI(ListboxPage_ChangeText
, ListboxWidgetsPage::OnUpdateUIDeleteSelButton
)
180 EVT_UPDATE_UI(ListboxPage_DeleteSel
, ListboxWidgetsPage::OnUpdateUIDeleteSelButton
)
182 EVT_LISTBOX(ListboxPage_Listbox
, ListboxWidgetsPage::OnListbox
)
183 EVT_LISTBOX_DCLICK(ListboxPage_Listbox
, ListboxWidgetsPage::OnListboxDClick
)
184 EVT_CHECKLISTBOX(ListboxPage_Listbox
, ListboxWidgetsPage::OnCheckListbox
)
186 EVT_CHECKBOX(wxID_ANY
, ListboxWidgetsPage::OnCheckOrRadioBox
)
187 EVT_RADIOBOX(wxID_ANY
, ListboxWidgetsPage::OnCheckOrRadioBox
)
190 // ============================================================================
192 // ============================================================================
194 IMPLEMENT_WIDGETS_PAGE(ListboxWidgetsPage
, _T("Listbox"));
196 ListboxWidgetsPage::ListboxWidgetsPage(wxNotebook
*notebook
,
197 wxImageList
*imaglist
)
198 : WidgetsPage(notebook
)
200 imaglist
->Add(wxBitmap(listbox_xpm
));
203 m_radioSelMode
= (wxRadioBox
*)NULL
;
208 m_chkSort
= (wxCheckBox
*)NULL
;
210 m_lbox
= (wxListBox
*)NULL
;
211 m_sizerLbox
= (wxSizer
*)NULL
;
214 What we create here is a frame having 3 panes: style pane is the
215 leftmost one, in the middle the pane with buttons allowing to perform
216 miscellaneous listbox operations and the pane containing the listbox
219 wxSizer
*sizerTop
= new wxBoxSizer(wxHORIZONTAL
);
222 wxStaticBox
*box
= new wxStaticBox(this, wxID_ANY
,
223 _T("&Set listbox parameters"));
224 wxSizer
*sizerLeft
= new wxStaticBoxSizer(box
, wxVERTICAL
);
226 static const wxString modes
[] =
233 m_radioSelMode
= new wxRadioBox(this, wxID_ANY
, _T("Selection &mode:"),
234 wxDefaultPosition
, wxDefaultSize
,
235 WXSIZEOF(modes
), modes
,
236 1, wxRA_SPECIFY_COLS
);
238 m_chkVScroll
= CreateCheckBoxAndAddToSizer
241 _T("Always show &vertical scrollbar")
243 m_chkHScroll
= CreateCheckBoxAndAddToSizer
246 _T("Show &horizontal scrollbar")
248 m_chkCheck
= CreateCheckBoxAndAddToSizer(sizerLeft
, _T("&Check list box"));
249 m_chkSort
= CreateCheckBoxAndAddToSizer(sizerLeft
, _T("&Sort items"));
251 sizerLeft
->Add(5, 5, 0, wxGROW
| wxALL
, 5); // spacer
252 sizerLeft
->Add(m_radioSelMode
, 0, wxGROW
| wxALL
, 5);
254 wxButton
*btn
= new wxButton(this, ListboxPage_Reset
, _T("&Reset"));
255 sizerLeft
->Add(btn
, 0, wxALIGN_CENTRE_HORIZONTAL
| wxALL
, 15);
258 wxStaticBox
*box2
= new wxStaticBox(this, wxID_ANY
,
259 _T("&Change listbox contents"));
260 wxSizer
*sizerMiddle
= new wxStaticBoxSizer(box2
, wxVERTICAL
);
262 wxSizer
*sizerRow
= new wxBoxSizer(wxHORIZONTAL
);
263 btn
= new wxButton(this, ListboxPage_Add
, _T("&Add this string"));
264 m_textAdd
= new wxTextCtrl(this, ListboxPage_AddText
, _T("test item 0"));
265 sizerRow
->Add(btn
, 0, wxRIGHT
, 5);
266 sizerRow
->Add(m_textAdd
, 1, wxLEFT
, 5);
267 sizerMiddle
->Add(sizerRow
, 0, wxALL
| wxGROW
, 5);
269 btn
= new wxButton(this, ListboxPage_AddSeveral
, _T("&Insert a few strings"));
270 sizerMiddle
->Add(btn
, 0, wxALL
| wxGROW
, 5);
272 btn
= new wxButton(this, ListboxPage_AddMany
, _T("Add &many strings"));
273 sizerMiddle
->Add(btn
, 0, wxALL
| wxGROW
, 5);
275 sizerRow
= new wxBoxSizer(wxHORIZONTAL
);
276 btn
= new wxButton(this, ListboxPage_Change
, _T("C&hange current"));
277 m_textChange
= new wxTextCtrl(this, ListboxPage_ChangeText
, wxEmptyString
);
278 sizerRow
->Add(btn
, 0, wxRIGHT
, 5);
279 sizerRow
->Add(m_textChange
, 1, wxLEFT
, 5);
280 sizerMiddle
->Add(sizerRow
, 0, wxALL
| wxGROW
, 5);
282 sizerRow
= new wxBoxSizer(wxHORIZONTAL
);
283 btn
= new wxButton(this, ListboxPage_Delete
, _T("&Delete this item"));
284 m_textDelete
= new wxTextCtrl(this, ListboxPage_DeleteText
, wxEmptyString
);
285 sizerRow
->Add(btn
, 0, wxRIGHT
, 5);
286 sizerRow
->Add(m_textDelete
, 1, wxLEFT
, 5);
287 sizerMiddle
->Add(sizerRow
, 0, wxALL
| wxGROW
, 5);
289 btn
= new wxButton(this, ListboxPage_DeleteSel
, _T("Delete &selection"));
290 sizerMiddle
->Add(btn
, 0, wxALL
| wxGROW
, 5);
292 btn
= new wxButton(this, ListboxPage_Clear
, _T("&Clear"));
293 sizerMiddle
->Add(btn
, 0, wxALL
| wxGROW
, 5);
296 wxSizer
*sizerRight
= new wxBoxSizer(wxVERTICAL
);
297 m_lbox
= new wxListBox(this, ListboxPage_Listbox
,
298 wxDefaultPosition
, wxDefaultSize
,
301 sizerRight
->Add(m_lbox
, 1, wxGROW
| wxALL
, 5);
302 sizerRight
->SetMinSize(150, 0);
303 m_sizerLbox
= sizerRight
; // save it to modify it later
305 // the 3 panes panes compose the window
306 sizerTop
->Add(sizerLeft
, 0, wxGROW
| (wxALL
& ~wxLEFT
), 10);
307 sizerTop
->Add(sizerMiddle
, 1, wxGROW
| wxALL
, 10);
308 sizerTop
->Add(sizerRight
, 1, wxGROW
| (wxALL
& ~wxRIGHT
), 10);
310 // final initializations
318 // ----------------------------------------------------------------------------
320 // ----------------------------------------------------------------------------
322 void ListboxWidgetsPage::Reset()
324 m_radioSelMode
->SetSelection(LboxSel_Single
);
325 m_chkSort
->SetValue(false);
326 m_chkCheck
->SetValue(false);
327 m_chkHScroll
->SetValue(true);
328 m_chkVScroll
->SetValue(false);
331 void ListboxWidgetsPage::CreateLbox()
334 switch ( m_radioSelMode
->GetSelection() )
337 wxFAIL_MSG( _T("unexpected radio box selection") );
339 case LboxSel_Single
: flags
|= wxLB_SINGLE
; break;
340 case LboxSel_Extended
: flags
|= wxLB_EXTENDED
; break;
341 case LboxSel_Multiple
: flags
|= wxLB_MULTIPLE
; break;
344 if ( m_chkVScroll
->GetValue() )
345 flags
|= wxLB_ALWAYS_SB
;
346 if ( m_chkHScroll
->GetValue() )
347 flags
|= wxLB_HSCROLL
;
348 if ( m_chkSort
->GetValue() )
354 int count
= m_lbox
->GetCount();
355 for ( int n
= 0; n
< count
; n
++ )
357 items
.Add(m_lbox
->GetString(n
));
360 m_sizerLbox
->Detach( m_lbox
);
364 #if wxUSE_CHECKLISTBOX
365 if ( m_chkCheck
->GetValue() )
367 m_lbox
= new wxCheckListBox(this, ListboxPage_Listbox
,
368 wxDefaultPosition
, wxDefaultSize
,
372 else // just a listbox
375 m_lbox
= new wxListBox(this, ListboxPage_Listbox
,
376 wxDefaultPosition
, wxDefaultSize
,
382 m_sizerLbox
->Add(m_lbox
, 1, wxGROW
| wxALL
, 5);
383 m_sizerLbox
->Layout();
386 // ----------------------------------------------------------------------------
388 // ----------------------------------------------------------------------------
390 void ListboxWidgetsPage::OnButtonReset(wxCommandEvent
& WXUNUSED(event
))
397 void ListboxWidgetsPage::OnButtonChange(wxCommandEvent
& WXUNUSED(event
))
399 wxArrayInt selections
;
400 int count
= m_lbox
->GetSelections(selections
);
401 wxString s
= m_textChange
->GetValue();
402 for ( int n
= 0; n
< count
; n
++ )
404 m_lbox
->SetString(selections
[n
], s
);
408 void ListboxWidgetsPage::OnButtonDelete(wxCommandEvent
& WXUNUSED(event
))
411 if ( !m_textDelete
->GetValue().ToULong(&n
) ||
412 (n
>= (unsigned)m_lbox
->GetCount()) )
420 void ListboxWidgetsPage::OnButtonDeleteSel(wxCommandEvent
& WXUNUSED(event
))
422 wxArrayInt selections
;
423 int n
= m_lbox
->GetSelections(selections
);
426 m_lbox
->Delete(selections
[--n
]);
430 void ListboxWidgetsPage::OnButtonClear(wxCommandEvent
& WXUNUSED(event
))
435 void ListboxWidgetsPage::OnButtonAdd(wxCommandEvent
& WXUNUSED(event
))
437 static unsigned int s_item
= 0;
439 wxString s
= m_textAdd
->GetValue();
440 if ( !m_textAdd
->IsModified() )
442 // update the default string
443 m_textAdd
->SetValue(wxString::Format(_T("test item %u"), ++s_item
));
449 void ListboxWidgetsPage::OnButtonAddMany(wxCommandEvent
& WXUNUSED(event
))
451 // "many" means 1000 here
452 for ( unsigned int n
= 0; n
< 1000; n
++ )
454 m_lbox
->Append(wxString::Format(_T("item #%u"), n
));
458 void ListboxWidgetsPage::OnButtonAddSeveral(wxCommandEvent
& WXUNUSED(event
))
461 items
.Add(_T("First"));
462 items
.Add(_T("another one"));
463 items
.Add(_T("and the last (very very very very very very very very very very long) one"));
464 m_lbox
->InsertItems(items
, 0);
467 void ListboxWidgetsPage::OnUpdateUIResetButton(wxUpdateUIEvent
& event
)
469 event
.Enable( (m_radioSelMode
->GetSelection() != LboxSel_Single
) ||
470 m_chkSort
->GetValue() ||
471 !m_chkHScroll
->GetValue() ||
472 m_chkVScroll
->GetValue() );
475 void ListboxWidgetsPage::OnUpdateUIDeleteButton(wxUpdateUIEvent
& event
)
478 event
.Enable(m_textDelete
->GetValue().ToULong(&n
) &&
479 (n
< (unsigned)m_lbox
->GetCount()));
482 void ListboxWidgetsPage::OnUpdateUIDeleteSelButton(wxUpdateUIEvent
& event
)
484 wxArrayInt selections
;
485 event
.Enable(m_lbox
->GetSelections(selections
) != 0);
488 void ListboxWidgetsPage::OnUpdateUIClearButton(wxUpdateUIEvent
& event
)
490 event
.Enable(m_lbox
->GetCount() != 0);
493 void ListboxWidgetsPage::OnUpdateUIAddSeveral(wxUpdateUIEvent
& event
)
495 event
.Enable(!(m_lbox
->GetWindowStyle() & wxLB_SORT
));
498 void ListboxWidgetsPage::OnListbox(wxCommandEvent
& event
)
500 long sel
= event
.GetSelection();
501 m_textDelete
->SetValue(wxString::Format(_T("%ld"), sel
));
503 if (event
.IsSelection())
504 wxLogMessage(_T("Listbox item %ld selected"), sel
);
506 wxLogMessage(_T("Listbox item %ld deselected"), sel
);
509 void ListboxWidgetsPage::OnListboxDClick(wxCommandEvent
& event
)
511 wxLogMessage( _T("Listbox item %ld double clicked"), event
.GetInt() );
514 void ListboxWidgetsPage::OnCheckListbox(wxCommandEvent
& event
)
516 wxLogMessage( _T("Listbox item %ld toggled"), event
.GetInt() );
519 void ListboxWidgetsPage::OnCheckOrRadioBox(wxCommandEvent
& WXUNUSED(event
))
524 #endif // wxUSE_LISTBOX