Applied patch [ 829144 ] Compilation error fixes for samples
[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(wxID_ANY, ListboxWidgetsPage::OnCheckOrRadioBox)
183 EVT_RADIOBOX(wxID_ANY, 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, wxID_ANY,
219 _T("&Set listbox parameters"));
220 wxSizer *sizerLeft = new wxStaticBoxSizer(box, wxVERTICAL);
221
222 static const wxString modes[] =
223 {
224 _T("single"),
225 _T("extended"),
226 _T("multiple"),
227 };
228
229 m_radioSelMode = new wxRadioBox(this, wxID_ANY, _T("Selection &mode:"),
230 wxDefaultPosition, wxDefaultSize,
231 WXSIZEOF(modes), modes,
232 1, wxRA_SPECIFY_COLS);
233
234 m_chkVScroll = CreateCheckBoxAndAddToSizer
235 (
236 sizerLeft,
237 _T("Always show &vertical scrollbar")
238 );
239 m_chkHScroll = CreateCheckBoxAndAddToSizer
240 (
241 sizerLeft,
242 _T("Show &horizontal scrollbar")
243 );
244 m_chkCheck = CreateCheckBoxAndAddToSizer(sizerLeft, _T("&Check list box"));
245 m_chkSort = CreateCheckBoxAndAddToSizer(sizerLeft, _T("&Sort items"));
246
247 sizerLeft->Add(5, 5, 0, wxGROW | wxALL, 5); // spacer
248 sizerLeft->Add(m_radioSelMode, 0, wxGROW | wxALL, 5);
249
250 wxButton *btn = new wxButton(this, ListboxPage_Reset, _T("&Reset"));
251 sizerLeft->Add(btn, 0, wxALIGN_CENTRE_HORIZONTAL | wxALL, 15);
252
253 // middle pane
254 wxStaticBox *box2 = new wxStaticBox(this, wxID_ANY,
255 _T("&Change listbox contents"));
256 wxSizer *sizerMiddle = new wxStaticBoxSizer(box2, wxVERTICAL);
257
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);
264
265 btn = new wxButton(this, ListboxPage_AddSeveral, _T("&Insert a few strings"));
266 sizerMiddle->Add(btn, 0, wxALL | wxGROW, 5);
267
268 btn = new wxButton(this, ListboxPage_AddMany, _T("Add &many strings"));
269 sizerMiddle->Add(btn, 0, wxALL | wxGROW, 5);
270
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);
277
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);
284
285 btn = new wxButton(this, ListboxPage_DeleteSel, _T("Delete &selection"));
286 sizerMiddle->Add(btn, 0, wxALL | wxGROW, 5);
287
288 btn = new wxButton(this, ListboxPage_Clear, _T("&Clear"));
289 sizerMiddle->Add(btn, 0, wxALL | wxGROW, 5);
290
291 // right pane
292 wxSizer *sizerRight = new wxBoxSizer(wxVERTICAL);
293 m_lbox = new wxListBox(this, ListboxPage_Listbox,
294 wxDefaultPosition, wxDefaultSize,
295 0, NULL,
296 wxLB_HSCROLL);
297 sizerRight->Add(m_lbox, 1, wxGROW | wxALL, 5);
298 sizerRight->SetMinSize(150, 0);
299 m_sizerLbox = sizerRight; // save it to modify it later
300
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);
305
306 // final initializations
307 Reset();
308
309 SetSizer(sizerTop);
310
311 sizerTop->Fit(this);
312 }
313
314 // ----------------------------------------------------------------------------
315 // operations
316 // ----------------------------------------------------------------------------
317
318 void ListboxWidgetsPage::Reset()
319 {
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);
325 }
326
327 void ListboxWidgetsPage::CreateLbox()
328 {
329 int flags = 0;
330 switch ( m_radioSelMode->GetSelection() )
331 {
332 default:
333 wxFAIL_MSG( _T("unexpected radio box selection") );
334
335 case LboxSel_Single: flags |= wxLB_SINGLE; break;
336 case LboxSel_Extended: flags |= wxLB_EXTENDED; break;
337 case LboxSel_Multiple: flags |= wxLB_MULTIPLE; break;
338 }
339
340 if ( m_chkVScroll->GetValue() )
341 flags |= wxLB_ALWAYS_SB;
342 if ( m_chkHScroll->GetValue() )
343 flags |= wxLB_HSCROLL;
344 if ( m_chkSort->GetValue() )
345 flags |= wxLB_SORT;
346
347 wxArrayString items;
348 if ( m_lbox )
349 {
350 int count = m_lbox->GetCount();
351 for ( int n = 0; n < count; n++ )
352 {
353 items.Add(m_lbox->GetString(n));
354 }
355
356 m_sizerLbox->Detach( m_lbox );
357 delete m_lbox;
358 }
359
360 #if wxUSE_CHECKLISTBOX
361 if ( m_chkCheck->GetValue() )
362 {
363 m_lbox = new wxCheckListBox(this, ListboxPage_Listbox,
364 wxDefaultPosition, wxDefaultSize,
365 0, NULL,
366 flags);
367 }
368 else // just a listbox
369 #endif
370 {
371 m_lbox = new wxListBox(this, ListboxPage_Listbox,
372 wxDefaultPosition, wxDefaultSize,
373 0, NULL,
374 flags);
375 }
376
377 m_lbox->Set(items);
378 m_sizerLbox->Add(m_lbox, 1, wxGROW | wxALL, 5);
379 m_sizerLbox->Layout();
380 }
381
382 // ----------------------------------------------------------------------------
383 // event handlers
384 // ----------------------------------------------------------------------------
385
386 void ListboxWidgetsPage::OnButtonReset(wxCommandEvent& WXUNUSED(event))
387 {
388 Reset();
389
390 CreateLbox();
391 }
392
393 void ListboxWidgetsPage::OnButtonChange(wxCommandEvent& WXUNUSED(event))
394 {
395 wxArrayInt selections;
396 int count = m_lbox->GetSelections(selections);
397 wxString s = m_textChange->GetValue();
398 for ( int n = 0; n < count; n++ )
399 {
400 m_lbox->SetString(selections[n], s);
401 }
402 }
403
404 void ListboxWidgetsPage::OnButtonDelete(wxCommandEvent& WXUNUSED(event))
405 {
406 unsigned long n;
407 if ( !m_textDelete->GetValue().ToULong(&n) ||
408 (n >= (unsigned)m_lbox->GetCount()) )
409 {
410 return;
411 }
412
413 m_lbox->Delete(n);
414 }
415
416 void ListboxWidgetsPage::OnButtonDeleteSel(wxCommandEvent& WXUNUSED(event))
417 {
418 wxArrayInt selections;
419 int n = m_lbox->GetSelections(selections);
420 while ( n > 0 )
421 {
422 m_lbox->Delete(selections[--n]);
423 }
424 }
425
426 void ListboxWidgetsPage::OnButtonClear(wxCommandEvent& WXUNUSED(event))
427 {
428 m_lbox->Clear();
429 }
430
431 void ListboxWidgetsPage::OnButtonAdd(wxCommandEvent& WXUNUSED(event))
432 {
433 static unsigned int s_item = 0;
434
435 wxString s = m_textAdd->GetValue();
436 if ( !m_textAdd->IsModified() )
437 {
438 // update the default string
439 m_textAdd->SetValue(wxString::Format(_T("test item %u"), ++s_item));
440 }
441
442 m_lbox->Append(s);
443 }
444
445 void ListboxWidgetsPage::OnButtonAddMany(wxCommandEvent& WXUNUSED(event))
446 {
447 // "many" means 1000 here
448 for ( unsigned int n = 0; n < 1000; n++ )
449 {
450 m_lbox->Append(wxString::Format(_T("item #%u"), n));
451 }
452 }
453
454 void ListboxWidgetsPage::OnButtonAddSeveral(wxCommandEvent& WXUNUSED(event))
455 {
456 wxArrayString items;
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);
461 }
462
463 void ListboxWidgetsPage::OnUpdateUIResetButton(wxUpdateUIEvent& event)
464 {
465 event.Enable( (m_radioSelMode->GetSelection() != LboxSel_Single) ||
466 m_chkSort->GetValue() ||
467 !m_chkHScroll->GetValue() ||
468 m_chkVScroll->GetValue() );
469 }
470
471 void ListboxWidgetsPage::OnUpdateUIDeleteButton(wxUpdateUIEvent& event)
472 {
473 unsigned long n;
474 event.Enable(m_textDelete->GetValue().ToULong(&n) &&
475 (n < (unsigned)m_lbox->GetCount()));
476 }
477
478 void ListboxWidgetsPage::OnUpdateUIDeleteSelButton(wxUpdateUIEvent& event)
479 {
480 wxArrayInt selections;
481 event.Enable(m_lbox->GetSelections(selections) != 0);
482 }
483
484 void ListboxWidgetsPage::OnUpdateUIClearButton(wxUpdateUIEvent& event)
485 {
486 event.Enable(m_lbox->GetCount() != 0);
487 }
488
489 void ListboxWidgetsPage::OnUpdateUIAddSeveral(wxUpdateUIEvent& event)
490 {
491 event.Enable(!(m_lbox->GetWindowStyle() & wxLB_SORT));
492 }
493
494 void ListboxWidgetsPage::OnListbox(wxCommandEvent& event)
495 {
496 long sel = event.GetInt();
497 m_textDelete->SetValue(wxString::Format(_T("%ld"), sel));
498
499 wxLogMessage(_T("Listbox item %ld selected"), sel);
500 }
501
502 void ListboxWidgetsPage::OnListboxDClick(wxCommandEvent& event)
503 {
504 wxLogMessage( _T("Listbox item %ld double clicked"), event.GetInt() );
505 }
506
507 void ListboxWidgetsPage::OnCheckListbox(wxCommandEvent& event)
508 {
509 wxLogMessage( _T("Listbox item %ld toggled"), event.GetInt() );
510 }
511
512 void ListboxWidgetsPage::OnCheckOrRadioBox(wxCommandEvent& WXUNUSED(event))
513 {
514 CreateLbox();
515 }
516
517 #endif