]> git.saurik.com Git - wxWidgets.git/blob - samples/widgets/listbox.cpp
added IsVisible() test
[wxWidgets.git] / samples / widgets / listbox.cpp
1 /////////////////////////////////////////////////////////////////////////////
2 // Program: wxWindows Widgets Sample
3 // Name: listbox.cpp
4 // Purpose: Part of the widgets sample showing wxListbox
5 // Author: Vadim Zeitlin
6 // Created: 27.03.01
7 // Id: $Id$
8 // Copyright: (c) 2001 Vadim Zeitlin
9 // License: wxWindows license
10 /////////////////////////////////////////////////////////////////////////////
11
12 // ============================================================================
13 // declarations
14 // ============================================================================
15
16 // ----------------------------------------------------------------------------
17 // headers
18 // ----------------------------------------------------------------------------
19
20 // for compilers that support precompilation, includes "wx/wx.h".
21 #include "wx/wxprec.h"
22
23 #ifdef __BORLANDC__
24 #pragma hdrstop
25 #endif
26
27 // for all others, include the necessary headers
28 #ifndef WX_PRECOMP
29 #include "wx/log.h"
30
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"
39 #endif
40
41 #include "wx/sizer.h"
42
43 #include "wx/checklst.h"
44
45 #include "widgets.h"
46 #if 1
47 #include "icons/listbox.xpm"
48
49 // ----------------------------------------------------------------------------
50 // constants
51 // ----------------------------------------------------------------------------
52
53 // control ids
54 enum
55 {
56 ListboxPage_Reset = 100,
57 ListboxPage_Add,
58 ListboxPage_AddText,
59 ListboxPage_AddSeveral,
60 ListboxPage_AddMany,
61 ListboxPage_Clear,
62 ListboxPage_Change,
63 ListboxPage_ChangeText,
64 ListboxPage_Delete,
65 ListboxPage_DeleteText,
66 ListboxPage_DeleteSel,
67 ListboxPage_Listbox
68 };
69
70 // ----------------------------------------------------------------------------
71 // ListboxWidgetsPage
72 // ----------------------------------------------------------------------------
73
74 class ListboxWidgetsPage : public WidgetsPage
75 {
76 public:
77 ListboxWidgetsPage(wxNotebook *notebook, wxImageList *imaglist);
78
79 protected:
80 // event handlers
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);
89
90 void OnListbox(wxCommandEvent& event);
91 void OnListboxDClick(wxCommandEvent& event);
92 void OnCheckListbox(wxCommandEvent& event);
93
94 void OnCheckOrRadioBox(wxCommandEvent& event);
95
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);
101
102 // reset the listbox parameters
103 void Reset();
104
105 // (re)create the listbox
106 void CreateLbox();
107
108 // listbox parameters
109 // ------------------
110
111 // the selection mode
112 enum LboxSelection
113 {
114 LboxSel_Single,
115 LboxSel_Extended,
116 LboxSel_Multiple
117 } m_lboxSelMode;
118
119 // should it be sorted?
120 bool m_sorted;
121
122 // should it have horz scroll/vert scrollbar permanently shown?
123 bool m_horzScroll,
124 m_vertScrollAlways;
125
126 // the controls
127 // ------------
128
129 // the sel mode radiobox
130 wxRadioBox *m_radioSelMode;
131
132 // the checkboxes
133 wxCheckBox *m_chkSort,
134 *m_chkCheck,
135 *m_chkHScroll,
136 *m_chkVScroll;
137
138 // the listbox itself and the sizer it is in
139 wxListBox *m_lbox;
140 wxSizer *m_sizerLbox;
141
142 // the text entries for "Add/change string" and "Delete" buttons
143 wxTextCtrl *m_textAdd,
144 *m_textChange,
145 *m_textDelete;
146
147 private:
148 DECLARE_EVENT_TABLE()
149 DECLARE_WIDGETS_PAGE(ListboxWidgetsPage)
150 };
151
152 // ----------------------------------------------------------------------------
153 // event tables
154 // ----------------------------------------------------------------------------
155
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)
165
166 EVT_TEXT_ENTER(ListboxPage_AddText, ListboxWidgetsPage::OnButtonAdd)
167 EVT_TEXT_ENTER(ListboxPage_DeleteText, ListboxWidgetsPage::OnButtonDelete)
168
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)
177
178 EVT_LISTBOX(ListboxPage_Listbox, ListboxWidgetsPage::OnListbox)
179 EVT_LISTBOX_DCLICK(ListboxPage_Listbox, ListboxWidgetsPage::OnListboxDClick)
180 EVT_CHECKLISTBOX(ListboxPage_Listbox, ListboxWidgetsPage::OnCheckListbox)
181
182 EVT_CHECKBOX(-1, ListboxWidgetsPage::OnCheckOrRadioBox)
183 EVT_RADIOBOX(-1, ListboxWidgetsPage::OnCheckOrRadioBox)
184 END_EVENT_TABLE()
185
186 // ============================================================================
187 // implementation
188 // ============================================================================
189
190 IMPLEMENT_WIDGETS_PAGE(ListboxWidgetsPage, _T("Listbox"));
191
192 ListboxWidgetsPage::ListboxWidgetsPage(wxNotebook *notebook,
193 wxImageList *imaglist)
194 : WidgetsPage(notebook)
195 {
196 imaglist->Add(wxBitmap(listbox_xpm));
197
198 // init everything
199 m_radioSelMode = (wxRadioBox *)NULL;
200
201 m_chkVScroll =
202 m_chkHScroll =
203 m_chkCheck =
204 m_chkSort = (wxCheckBox *)NULL;
205
206 m_lbox = (wxListBox *)NULL;
207 m_sizerLbox = (wxSizer *)NULL;
208
209 /*
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
213 itself to the right
214 */
215 wxSizer *sizerTop = new wxBoxSizer(wxHORIZONTAL);
216
217 // left pane
218 wxStaticBox *box = new wxStaticBox(this, -1, _T("&Set listbox parameters"));
219 wxSizer *sizerLeft = new wxStaticBoxSizer(box, wxVERTICAL);
220
221 static const wxString modes[] =
222 {
223 _T("single"),
224 _T("extended"),
225 _T("multiple"),
226 };
227
228 m_radioSelMode = new wxRadioBox(this, -1, _T("Selection &mode:"),
229 wxDefaultPosition, wxDefaultSize,
230 WXSIZEOF(modes), modes,
231 1, wxRA_SPECIFY_COLS);
232
233 m_chkVScroll = CreateCheckBoxAndAddToSizer
234 (
235 sizerLeft,
236 _T("Always show &vertical scrollbar")
237 );
238 m_chkHScroll = CreateCheckBoxAndAddToSizer
239 (
240 sizerLeft,
241 _T("Show &horizontal scrollbar")
242 );
243 m_chkCheck = CreateCheckBoxAndAddToSizer(sizerLeft, _T("&Check list box"));
244 m_chkSort = CreateCheckBoxAndAddToSizer(sizerLeft, _T("&Sort items"));
245
246 sizerLeft->Add(5, 5, 0, wxGROW | wxALL, 5); // spacer
247 sizerLeft->Add(m_radioSelMode, 0, wxGROW | wxALL, 5);
248
249 wxButton *btn = new wxButton(this, ListboxPage_Reset, _T("&Reset"));
250 sizerLeft->Add(btn, 0, wxALIGN_CENTRE_HORIZONTAL | wxALL, 15);
251
252 // middle pane
253 wxStaticBox *box2 = new wxStaticBox(this, -1, _T("&Change listbox contents"));
254 wxSizer *sizerMiddle = new wxStaticBoxSizer(box2, wxVERTICAL);
255
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);
262
263 btn = new wxButton(this, ListboxPage_AddSeveral, _T("&Insert a few strings"));
264 sizerMiddle->Add(btn, 0, wxALL | wxGROW, 5);
265
266 btn = new wxButton(this, ListboxPage_AddMany, _T("Add &many strings"));
267 sizerMiddle->Add(btn, 0, wxALL | wxGROW, 5);
268
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);
275
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);
282
283 btn = new wxButton(this, ListboxPage_DeleteSel, _T("Delete &selection"));
284 sizerMiddle->Add(btn, 0, wxALL | wxGROW, 5);
285
286 btn = new wxButton(this, ListboxPage_Clear, _T("&Clear"));
287 sizerMiddle->Add(btn, 0, wxALL | wxGROW, 5);
288
289 // right pane
290 wxSizer *sizerRight = new wxBoxSizer(wxVERTICAL);
291 m_lbox = new wxListBox(this, ListboxPage_Listbox,
292 wxDefaultPosition, wxDefaultSize,
293 0, NULL,
294 wxLB_HSCROLL);
295 sizerRight->Add(m_lbox, 1, wxGROW | wxALL, 5);
296 sizerRight->SetMinSize(150, 0);
297 m_sizerLbox = sizerRight; // save it to modify it later
298
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);
303
304 // final initializations
305 Reset();
306
307 SetAutoLayout(TRUE);
308 SetSizer(sizerTop);
309
310 sizerTop->Fit(this);
311 }
312
313 // ----------------------------------------------------------------------------
314 // operations
315 // ----------------------------------------------------------------------------
316
317 void ListboxWidgetsPage::Reset()
318 {
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);
324 }
325
326 void ListboxWidgetsPage::CreateLbox()
327 {
328 int flags = 0;
329 switch ( m_radioSelMode->GetSelection() )
330 {
331 default:
332 wxFAIL_MSG( _T("unexpected radio box selection") );
333
334 case LboxSel_Single: flags |= wxLB_SINGLE; break;
335 case LboxSel_Extended: flags |= wxLB_EXTENDED; break;
336 case LboxSel_Multiple: flags |= wxLB_MULTIPLE; break;
337 }
338
339 if ( m_chkVScroll->GetValue() )
340 flags |= wxLB_ALWAYS_SB;
341 if ( m_chkHScroll->GetValue() )
342 flags |= wxLB_HSCROLL;
343 if ( m_chkSort->GetValue() )
344 flags |= wxLB_SORT;
345
346 wxArrayString items;
347 if ( m_lbox )
348 {
349 int count = m_lbox->GetCount();
350 for ( int n = 0; n < count; n++ )
351 {
352 items.Add(m_lbox->GetString(n));
353 }
354
355 m_sizerLbox->Detach( m_lbox );
356 delete m_lbox;
357 }
358
359 #if wxUSE_CHECKLISTBOX
360 if ( m_chkCheck->GetValue() )
361 {
362 m_lbox = new wxCheckListBox(this, ListboxPage_Listbox,
363 wxDefaultPosition, wxDefaultSize,
364 0, NULL,
365 flags);
366 }
367 else // just a listbox
368 #endif
369 {
370 m_lbox = new wxListBox(this, ListboxPage_Listbox,
371 wxDefaultPosition, wxDefaultSize,
372 0, NULL,
373 flags);
374 }
375
376 m_lbox->Set(items);
377 m_sizerLbox->Add(m_lbox, 1, wxGROW | wxALL, 5);
378 m_sizerLbox->Layout();
379 }
380
381 // ----------------------------------------------------------------------------
382 // event handlers
383 // ----------------------------------------------------------------------------
384
385 void ListboxWidgetsPage::OnButtonReset(wxCommandEvent& WXUNUSED(event))
386 {
387 Reset();
388
389 CreateLbox();
390 }
391
392 void ListboxWidgetsPage::OnButtonChange(wxCommandEvent& WXUNUSED(event))
393 {
394 wxArrayInt selections;
395 int count = m_lbox->GetSelections(selections);
396 wxString s = m_textChange->GetValue();
397 for ( int n = 0; n < count; n++ )
398 {
399 m_lbox->SetString(selections[n], s);
400 }
401 }
402
403 void ListboxWidgetsPage::OnButtonDelete(wxCommandEvent& WXUNUSED(event))
404 {
405 unsigned long n;
406 if ( !m_textDelete->GetValue().ToULong(&n) ||
407 (n >= (unsigned)m_lbox->GetCount()) )
408 {
409 return;
410 }
411
412 m_lbox->Delete(n);
413 }
414
415 void ListboxWidgetsPage::OnButtonDeleteSel(wxCommandEvent& WXUNUSED(event))
416 {
417 wxArrayInt selections;
418 int n = m_lbox->GetSelections(selections);
419 while ( n > 0 )
420 {
421 m_lbox->Delete(selections[--n]);
422 }
423 }
424
425 void ListboxWidgetsPage::OnButtonClear(wxCommandEvent& WXUNUSED(event))
426 {
427 m_lbox->Clear();
428 }
429
430 void ListboxWidgetsPage::OnButtonAdd(wxCommandEvent& WXUNUSED(event))
431 {
432 static unsigned int s_item = 0;
433
434 wxString s = m_textAdd->GetValue();
435 if ( !m_textAdd->IsModified() )
436 {
437 // update the default string
438 m_textAdd->SetValue(wxString::Format(_T("test item %u"), ++s_item));
439 }
440
441 m_lbox->Append(s);
442 }
443
444 void ListboxWidgetsPage::OnButtonAddMany(wxCommandEvent& WXUNUSED(event))
445 {
446 // "many" means 1000 here
447 for ( unsigned int n = 0; n < 1000; n++ )
448 {
449 m_lbox->Append(wxString::Format(_T("item #%u"), n));
450 }
451 }
452
453 void ListboxWidgetsPage::OnButtonAddSeveral(wxCommandEvent& WXUNUSED(event))
454 {
455 wxArrayString items;
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);
460 }
461
462 void ListboxWidgetsPage::OnUpdateUIResetButton(wxUpdateUIEvent& event)
463 {
464 event.Enable( (m_radioSelMode->GetSelection() != LboxSel_Single) ||
465 m_chkSort->GetValue() ||
466 !m_chkHScroll->GetValue() ||
467 m_chkVScroll->GetValue() );
468 }
469
470 void ListboxWidgetsPage::OnUpdateUIDeleteButton(wxUpdateUIEvent& event)
471 {
472 unsigned long n;
473 event.Enable(m_textDelete->GetValue().ToULong(&n) &&
474 (n < (unsigned)m_lbox->GetCount()));
475 }
476
477 void ListboxWidgetsPage::OnUpdateUIDeleteSelButton(wxUpdateUIEvent& event)
478 {
479 wxArrayInt selections;
480 event.Enable(m_lbox->GetSelections(selections) != 0);
481 }
482
483 void ListboxWidgetsPage::OnUpdateUIClearButton(wxUpdateUIEvent& event)
484 {
485 event.Enable(m_lbox->GetCount() != 0);
486 }
487
488 void ListboxWidgetsPage::OnUpdateUIAddSeveral(wxUpdateUIEvent& event)
489 {
490 event.Enable(!(m_lbox->GetWindowStyle() & wxLB_SORT));
491 }
492
493 void ListboxWidgetsPage::OnListbox(wxCommandEvent& event)
494 {
495 long sel = event.GetInt();
496 m_textDelete->SetValue(wxString::Format(_T("%ld"), sel));
497
498 wxLogMessage(_T("Listbox item %ld selected"), sel);
499 }
500
501 void ListboxWidgetsPage::OnListboxDClick(wxCommandEvent& event)
502 {
503 wxLogMessage( _T("Listbox item %ld double clicked"), event.GetInt() );
504 }
505
506 void ListboxWidgetsPage::OnCheckListbox(wxCommandEvent& event)
507 {
508 wxLogMessage( _T("Listbox item %ld toggled"), event.GetInt() );
509 }
510
511 void ListboxWidgetsPage::OnCheckOrRadioBox(wxCommandEvent& WXUNUSED(event))
512 {
513 CreateLbox();
514 }
515
516 #endif