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