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(); }
84 // lazy creation of the content
85 virtual void CreateContent();
89 void OnButtonReset(wxCommandEvent
& event
);
90 void OnButtonChange(wxCommandEvent
& event
);
91 void OnButtonDelete(wxCommandEvent
& event
);
92 void OnButtonDeleteSel(wxCommandEvent
& event
);
93 void OnButtonClear(wxCommandEvent
& event
);
94 void OnButtonAdd(wxCommandEvent
& event
);
95 void OnButtonAddSeveral(wxCommandEvent
& event
);
96 void OnButtonAddMany(wxCommandEvent
& event
);
98 void OnListbox(wxCommandEvent
& event
);
99 void OnListboxDClick(wxCommandEvent
& event
);
100 void OnCheckListbox(wxCommandEvent
& event
);
102 void OnCheckOrRadioBox(wxCommandEvent
& event
);
104 void OnUpdateUIAddSeveral(wxUpdateUIEvent
& event
);
105 void OnUpdateUIClearButton(wxUpdateUIEvent
& event
);
106 void OnUpdateUIDeleteButton(wxUpdateUIEvent
& event
);
107 void OnUpdateUIDeleteSelButton(wxUpdateUIEvent
& event
);
108 void OnUpdateUIResetButton(wxUpdateUIEvent
& event
);
110 // reset the listbox parameters
113 // (re)create the listbox
116 // listbox parameters
117 // ------------------
119 // the selection mode
127 // should it be sorted?
130 // should it have horz scroll/vert scrollbar permanently shown?
137 // the sel mode radiobox
138 wxRadioBox
*m_radioSelMode
;
141 wxCheckBox
*m_chkVScroll
,
147 // the listbox itself and the sizer it is in
155 wxSizer
*m_sizerLbox
;
157 // the text entries for "Add/change string" and "Delete" buttons
158 wxTextCtrl
*m_textAdd
,
163 DECLARE_EVENT_TABLE()
164 DECLARE_WIDGETS_PAGE(ListboxWidgetsPage
)
167 // ----------------------------------------------------------------------------
169 // ----------------------------------------------------------------------------
171 BEGIN_EVENT_TABLE(ListboxWidgetsPage
, WidgetsPage
)
172 EVT_BUTTON(ListboxPage_Reset
, ListboxWidgetsPage::OnButtonReset
)
173 EVT_BUTTON(ListboxPage_Change
, ListboxWidgetsPage::OnButtonChange
)
174 EVT_BUTTON(ListboxPage_Delete
, ListboxWidgetsPage::OnButtonDelete
)
175 EVT_BUTTON(ListboxPage_DeleteSel
, ListboxWidgetsPage::OnButtonDeleteSel
)
176 EVT_BUTTON(ListboxPage_Clear
, ListboxWidgetsPage::OnButtonClear
)
177 EVT_BUTTON(ListboxPage_Add
, ListboxWidgetsPage::OnButtonAdd
)
178 EVT_BUTTON(ListboxPage_AddSeveral
, ListboxWidgetsPage::OnButtonAddSeveral
)
179 EVT_BUTTON(ListboxPage_AddMany
, ListboxWidgetsPage::OnButtonAddMany
)
181 EVT_TEXT_ENTER(ListboxPage_AddText
, ListboxWidgetsPage::OnButtonAdd
)
182 EVT_TEXT_ENTER(ListboxPage_DeleteText
, ListboxWidgetsPage::OnButtonDelete
)
184 EVT_UPDATE_UI(ListboxPage_Reset
, ListboxWidgetsPage::OnUpdateUIResetButton
)
185 EVT_UPDATE_UI(ListboxPage_AddSeveral
, ListboxWidgetsPage::OnUpdateUIAddSeveral
)
186 EVT_UPDATE_UI(ListboxPage_Clear
, ListboxWidgetsPage::OnUpdateUIClearButton
)
187 EVT_UPDATE_UI(ListboxPage_DeleteText
, ListboxWidgetsPage::OnUpdateUIClearButton
)
188 EVT_UPDATE_UI(ListboxPage_Delete
, ListboxWidgetsPage::OnUpdateUIDeleteButton
)
189 EVT_UPDATE_UI(ListboxPage_Change
, ListboxWidgetsPage::OnUpdateUIDeleteSelButton
)
190 EVT_UPDATE_UI(ListboxPage_ChangeText
, ListboxWidgetsPage::OnUpdateUIDeleteSelButton
)
191 EVT_UPDATE_UI(ListboxPage_DeleteSel
, ListboxWidgetsPage::OnUpdateUIDeleteSelButton
)
193 EVT_LISTBOX(ListboxPage_Listbox
, ListboxWidgetsPage::OnListbox
)
194 EVT_LISTBOX_DCLICK(ListboxPage_Listbox
, ListboxWidgetsPage::OnListboxDClick
)
195 EVT_CHECKLISTBOX(ListboxPage_Listbox
, ListboxWidgetsPage::OnCheckListbox
)
197 EVT_CHECKBOX(wxID_ANY
, ListboxWidgetsPage::OnCheckOrRadioBox
)
198 EVT_RADIOBOX(wxID_ANY
, ListboxWidgetsPage::OnCheckOrRadioBox
)
201 // ============================================================================
203 // ============================================================================
205 #if defined(__WXUNIVERSAL__)
206 #define FAMILY_CTRLS UNIVERSAL_CTRLS
208 #define FAMILY_CTRLS NATIVE_CTRLS
211 IMPLEMENT_WIDGETS_PAGE(ListboxWidgetsPage
, _T("Listbox"),
212 FAMILY_CTRLS
| WITH_ITEMS_CTRLS
215 ListboxWidgetsPage::ListboxWidgetsPage(WidgetsBookCtrl
*book
,
216 wxImageList
*imaglist
)
217 : WidgetsPage(book
, imaglist
, listbox_xpm
)
220 m_radioSelMode
= (wxRadioBox
*)NULL
;
226 m_chkOwnerDraw
= (wxCheckBox
*)NULL
;
229 m_sizerLbox
= (wxSizer
*)NULL
;
233 void ListboxWidgetsPage::CreateContent()
236 What we create here is a frame having 3 panes: style pane is the
237 leftmost one, in the middle the pane with buttons allowing to perform
238 miscellaneous listbox operations and the pane containing the listbox
241 wxSizer
*sizerTop
= new wxBoxSizer(wxHORIZONTAL
);
244 wxStaticBox
*box
= new wxStaticBox(this, wxID_ANY
,
245 _T("&Set listbox parameters"));
246 wxSizer
*sizerLeft
= new wxStaticBoxSizer(box
, wxVERTICAL
);
248 static const wxString modes
[] =
255 m_radioSelMode
= new wxRadioBox(this, wxID_ANY
, _T("Selection &mode:"),
256 wxDefaultPosition
, wxDefaultSize
,
257 WXSIZEOF(modes
), modes
,
258 1, wxRA_SPECIFY_COLS
);
260 m_chkVScroll
= CreateCheckBoxAndAddToSizer
263 _T("Always show &vertical scrollbar")
265 m_chkHScroll
= CreateCheckBoxAndAddToSizer
268 _T("Show &horizontal scrollbar")
270 m_chkCheck
= CreateCheckBoxAndAddToSizer(sizerLeft
, _T("&Check list box"));
271 m_chkSort
= CreateCheckBoxAndAddToSizer(sizerLeft
, _T("&Sort items"));
272 m_chkOwnerDraw
= CreateCheckBoxAndAddToSizer(sizerLeft
, _T("&Owner drawn"));
274 sizerLeft
->Add(5, 5, 0, wxGROW
| wxALL
, 5); // spacer
275 sizerLeft
->Add(m_radioSelMode
, 0, wxGROW
| wxALL
, 5);
277 wxButton
*btn
= new wxButton(this, ListboxPage_Reset
, _T("&Reset"));
278 sizerLeft
->Add(btn
, 0, wxALIGN_CENTRE_HORIZONTAL
| wxALL
, 15);
281 wxStaticBox
*box2
= new wxStaticBox(this, wxID_ANY
,
282 _T("&Change listbox contents"));
283 wxSizer
*sizerMiddle
= new wxStaticBoxSizer(box2
, wxVERTICAL
);
285 wxSizer
*sizerRow
= new wxBoxSizer(wxHORIZONTAL
);
286 btn
= new wxButton(this, ListboxPage_Add
, _T("&Add this string"));
287 m_textAdd
= new wxTextCtrl(this, ListboxPage_AddText
, _T("test item 0"));
288 sizerRow
->Add(btn
, 0, wxRIGHT
, 5);
289 sizerRow
->Add(m_textAdd
, 1, wxLEFT
, 5);
290 sizerMiddle
->Add(sizerRow
, 0, wxALL
| wxGROW
, 5);
292 btn
= new wxButton(this, ListboxPage_AddSeveral
, _T("&Insert a few strings"));
293 sizerMiddle
->Add(btn
, 0, wxALL
| wxGROW
, 5);
295 btn
= new wxButton(this, ListboxPage_AddMany
, _T("Add &many strings"));
296 sizerMiddle
->Add(btn
, 0, wxALL
| wxGROW
, 5);
298 sizerRow
= new wxBoxSizer(wxHORIZONTAL
);
299 btn
= new wxButton(this, ListboxPage_Change
, _T("C&hange current"));
300 m_textChange
= new wxTextCtrl(this, ListboxPage_ChangeText
, wxEmptyString
);
301 sizerRow
->Add(btn
, 0, wxRIGHT
, 5);
302 sizerRow
->Add(m_textChange
, 1, wxLEFT
, 5);
303 sizerMiddle
->Add(sizerRow
, 0, wxALL
| wxGROW
, 5);
305 sizerRow
= new wxBoxSizer(wxHORIZONTAL
);
306 btn
= new wxButton(this, ListboxPage_Delete
, _T("&Delete this item"));
307 m_textDelete
= new wxTextCtrl(this, ListboxPage_DeleteText
, wxEmptyString
);
308 sizerRow
->Add(btn
, 0, wxRIGHT
, 5);
309 sizerRow
->Add(m_textDelete
, 1, wxLEFT
, 5);
310 sizerMiddle
->Add(sizerRow
, 0, wxALL
| wxGROW
, 5);
312 btn
= new wxButton(this, ListboxPage_DeleteSel
, _T("Delete &selection"));
313 sizerMiddle
->Add(btn
, 0, wxALL
| wxGROW
, 5);
315 btn
= new wxButton(this, ListboxPage_Clear
, _T("&Clear"));
316 sizerMiddle
->Add(btn
, 0, wxALL
| wxGROW
, 5);
319 wxSizer
*sizerRight
= new wxBoxSizer(wxVERTICAL
);
320 m_lbox
= new wxListBox(this, ListboxPage_Listbox
,
321 wxDefaultPosition
, wxDefaultSize
,
324 sizerRight
->Add(m_lbox
, 1, wxGROW
| wxALL
, 5);
325 sizerRight
->SetMinSize(150, 0);
326 m_sizerLbox
= sizerRight
; // save it to modify it later
328 // the 3 panes panes compose the window
329 sizerTop
->Add(sizerLeft
, 0, wxGROW
| (wxALL
& ~wxLEFT
), 10);
330 sizerTop
->Add(sizerMiddle
, 1, wxGROW
| wxALL
, 10);
331 sizerTop
->Add(sizerRight
, 1, wxGROW
| (wxALL
& ~wxRIGHT
), 10);
333 // final initializations
341 // ----------------------------------------------------------------------------
343 // ----------------------------------------------------------------------------
345 void ListboxWidgetsPage::Reset()
347 m_radioSelMode
->SetSelection(LboxSel_Single
);
348 m_chkVScroll
->SetValue(false);
349 m_chkHScroll
->SetValue(true);
350 m_chkCheck
->SetValue(false);
351 m_chkSort
->SetValue(false);
352 m_chkOwnerDraw
->SetValue(false);
355 void ListboxWidgetsPage::CreateLbox()
357 int flags
= ms_defaultFlags
;
358 switch ( m_radioSelMode
->GetSelection() )
361 wxFAIL_MSG( _T("unexpected radio box selection") );
363 case LboxSel_Single
: flags
|= wxLB_SINGLE
; break;
364 case LboxSel_Extended
: flags
|= wxLB_EXTENDED
; break;
365 case LboxSel_Multiple
: flags
|= wxLB_MULTIPLE
; break;
368 if ( m_chkVScroll
->GetValue() )
369 flags
|= wxLB_ALWAYS_SB
;
370 if ( m_chkHScroll
->GetValue() )
371 flags
|= wxLB_HSCROLL
;
372 if ( m_chkSort
->GetValue() )
374 if ( m_chkOwnerDraw
->GetValue() )
375 flags
|= wxLB_OWNERDRAW
;
380 int count
= m_lbox
->GetCount();
381 for ( int n
= 0; n
< count
; n
++ )
383 items
.Add(m_lbox
->GetString(n
));
386 m_sizerLbox
->Detach( m_lbox
);
390 #if wxUSE_CHECKLISTBOX
391 if ( m_chkCheck
->GetValue() )
393 m_lbox
= new wxCheckListBox(this, ListboxPage_Listbox
,
394 wxDefaultPosition
, wxDefaultSize
,
398 else // just a listbox
401 m_lbox
= new wxListBox(this, ListboxPage_Listbox
,
402 wxDefaultPosition
, wxDefaultSize
,
408 m_sizerLbox
->Add(m_lbox
, 1, wxGROW
| wxALL
, 5);
409 m_sizerLbox
->Layout();
412 // ----------------------------------------------------------------------------
414 // ----------------------------------------------------------------------------
416 void ListboxWidgetsPage::OnButtonReset(wxCommandEvent
& WXUNUSED(event
))
423 void ListboxWidgetsPage::OnButtonChange(wxCommandEvent
& WXUNUSED(event
))
425 wxArrayInt selections
;
426 int count
= m_lbox
->GetSelections(selections
);
427 wxString s
= m_textChange
->GetValue();
428 for ( int n
= 0; n
< count
; n
++ )
430 m_lbox
->SetString(selections
[n
], s
);
434 void ListboxWidgetsPage::OnButtonDelete(wxCommandEvent
& WXUNUSED(event
))
437 if ( !m_textDelete
->GetValue().ToULong(&n
) ||
438 (n
>= (unsigned)m_lbox
->GetCount()) )
446 void ListboxWidgetsPage::OnButtonDeleteSel(wxCommandEvent
& WXUNUSED(event
))
448 wxArrayInt selections
;
449 int n
= m_lbox
->GetSelections(selections
);
452 m_lbox
->Delete(selections
[--n
]);
456 void ListboxWidgetsPage::OnButtonClear(wxCommandEvent
& WXUNUSED(event
))
461 void ListboxWidgetsPage::OnButtonAdd(wxCommandEvent
& WXUNUSED(event
))
463 static unsigned int s_item
= 0;
465 wxString s
= m_textAdd
->GetValue();
466 if ( !m_textAdd
->IsModified() )
468 // update the default string
469 m_textAdd
->SetValue(wxString::Format(_T("test item %u"), ++s_item
));
475 void ListboxWidgetsPage::OnButtonAddMany(wxCommandEvent
& WXUNUSED(event
))
477 // "many" means 1000 here
478 for ( unsigned int n
= 0; n
< 1000; n
++ )
480 m_lbox
->Append(wxString::Format(_T("item #%u"), n
));
484 void ListboxWidgetsPage::OnButtonAddSeveral(wxCommandEvent
& WXUNUSED(event
))
487 items
.Add(_T("First"));
488 items
.Add(_T("another one"));
489 items
.Add(_T("and the last (very very very very very very very very very very long) one"));
490 m_lbox
->InsertItems(items
, 0);
493 void ListboxWidgetsPage::OnUpdateUIResetButton(wxUpdateUIEvent
& event
)
495 event
.Enable( (m_radioSelMode
->GetSelection() != LboxSel_Single
) ||
496 m_chkSort
->GetValue() ||
497 m_chkOwnerDraw
->GetValue() ||
498 !m_chkHScroll
->GetValue() ||
499 m_chkVScroll
->GetValue() );
502 void ListboxWidgetsPage::OnUpdateUIDeleteButton(wxUpdateUIEvent
& event
)
505 event
.Enable(m_textDelete
->GetValue().ToULong(&n
) &&
506 (n
< (unsigned)m_lbox
->GetCount()));
509 void ListboxWidgetsPage::OnUpdateUIDeleteSelButton(wxUpdateUIEvent
& event
)
511 wxArrayInt selections
;
512 event
.Enable(m_lbox
->GetSelections(selections
) != 0);
515 void ListboxWidgetsPage::OnUpdateUIClearButton(wxUpdateUIEvent
& event
)
517 event
.Enable(m_lbox
->GetCount() != 0);
520 void ListboxWidgetsPage::OnUpdateUIAddSeveral(wxUpdateUIEvent
& event
)
522 event
.Enable(!(m_lbox
->GetWindowStyle() & wxLB_SORT
));
525 void ListboxWidgetsPage::OnListbox(wxCommandEvent
& event
)
527 long sel
= event
.GetSelection();
528 m_textDelete
->SetValue(wxString::Format(_T("%ld"), sel
));
530 if (event
.IsSelection())
531 wxLogMessage(_T("Listbox item %ld selected"), sel
);
533 wxLogMessage(_T("Listbox item %ld deselected"), sel
);
536 void ListboxWidgetsPage::OnListboxDClick(wxCommandEvent
& event
)
538 wxLogMessage( _T("Listbox item %d double clicked"), event
.GetInt() );
541 void ListboxWidgetsPage::OnCheckListbox(wxCommandEvent
& event
)
543 wxLogMessage( _T("Listbox item %d toggled"), event
.GetInt() );
546 void ListboxWidgetsPage::OnCheckOrRadioBox(wxCommandEvent
& WXUNUSED(event
))
551 #endif // wxUSE_LISTBOX