]> git.saurik.com Git - wxWidgets.git/blob - samples/listbox/lboxtest.cpp
a5a9117fbafbb18f545026aed5cec23d53d9dc22
[wxWidgets.git] / samples / listbox / lboxtest.cpp
1 /////////////////////////////////////////////////////////////////////////////
2 // Name: lboxtest.cpp
3 // Purpose: wxListBox sample
4 // Author: Vadim Zeitlin
5 // Id: $Id$
6 // Copyright: (c) 2000 Vadim Zeitlin
7 // Licence: wxWindows licence
8 /////////////////////////////////////////////////////////////////////////////
9
10 /*
11 Current bugs:
12
13 +1. horz scrollbar doesn't appear in listbox
14 +2. truncating text ctrl doesn't update display
15 +3. deleting last listbox item doesn't update display
16 4. text ctrl background corrupted after resize
17 */
18
19 // ============================================================================
20 // declarations
21 // ============================================================================
22
23 // ----------------------------------------------------------------------------
24 // headers
25 // ----------------------------------------------------------------------------
26
27 // for compilers that support precompilation, includes "wx/wx.h".
28 #include "wx/wxprec.h"
29
30 #ifdef __BORLANDC__
31 #pragma hdrstop
32 #endif
33
34 // for all others, include the necessary headers
35 #ifndef WX_PRECOMP
36 #include "wx/wx.h"
37 #include "wx/app.h"
38 #include "wx/frame.h"
39 #include "wx/dcclient.h"
40
41 #include "wx/button.h"
42 #include "wx/checkbox.h"
43 #include "wx/checklst.h"
44 #include "wx/listbox.h"
45 #include "wx/radiobox.h"
46 #include "wx/radiobut.h"
47 #include "wx/statbox.h"
48 #include "wx/stattext.h"
49 #include "wx/textctrl.h"
50 #endif
51
52 #include "wx/sizer.h"
53
54 #ifdef __WXUNIVERSAL__
55 #include "wx/univ/theme.h"
56 #endif // __WXUNIVERSAL__
57
58 // ----------------------------------------------------------------------------
59 // constants
60 // ----------------------------------------------------------------------------
61
62 // control ids
63 enum
64 {
65 LboxTest_Reset = 100,
66 LboxTest_Create,
67 LboxTest_Add,
68 LboxTest_AddText,
69 LboxTest_AddSeveral,
70 LboxTest_AddMany,
71 LboxTest_Clear,
72 #if wxUSE_LOG
73 LboxTest_ClearLog,
74 #endif // wxUSE_LOG
75 LboxTest_Change,
76 LboxTest_ChangeText,
77 LboxTest_Delete,
78 LboxTest_DeleteText,
79 LboxTest_DeleteSel,
80 LboxTest_Listbox,
81 LboxTest_Quit
82 };
83
84 // ----------------------------------------------------------------------------
85 // our classes
86 // ----------------------------------------------------------------------------
87
88 // Define a new application type, each program should derive a class from wxApp
89 class LboxTestApp : public wxApp
90 {
91 public:
92 // override base class virtuals
93 // ----------------------------
94
95 // this one is called on application startup and is a good place for the app
96 // initialization (doing it here and not in the ctor allows to have an error
97 // return: if OnInit() returns false, the application terminates)
98 virtual bool OnInit();
99 };
100
101 // Define a new frame type: this is going to be our main frame
102 class LboxTestFrame : public wxFrame
103 {
104 public:
105 // ctor(s) and dtor
106 LboxTestFrame(const wxString& title);
107 virtual ~LboxTestFrame();
108
109 protected:
110 // event handlers
111 void OnButtonReset(wxCommandEvent& event);
112 void OnButtonCreate(wxCommandEvent& event);
113 void OnButtonChange(wxCommandEvent& event);
114 void OnButtonDelete(wxCommandEvent& event);
115 void OnButtonDeleteSel(wxCommandEvent& event);
116 void OnButtonClear(wxCommandEvent& event);
117 #if wxUSE_LOG
118 void OnButtonClearLog(wxCommandEvent& event);
119 #endif // wxUSE_LOG
120 void OnButtonAdd(wxCommandEvent& event);
121 void OnButtonAddSeveral(wxCommandEvent& event);
122 void OnButtonAddMany(wxCommandEvent& event);
123 void OnButtonQuit(wxCommandEvent& event);
124
125 void OnListbox(wxCommandEvent& event);
126 void OnListboxDClick(wxCommandEvent& event);
127 void OnListboxRDown(wxMouseEvent& event);
128
129 void OnCheckOrRadioBox(wxCommandEvent& event);
130
131 void OnUpdateUIAddSeveral(wxUpdateUIEvent& event);
132 void OnUpdateUICreateButton(wxUpdateUIEvent& event);
133 void OnUpdateUIClearButton(wxUpdateUIEvent& event);
134 void OnUpdateUIDeleteButton(wxUpdateUIEvent& event);
135 void OnUpdateUIDeleteSelButton(wxUpdateUIEvent& event);
136
137 // reset the listbox parameters
138 void Reset();
139
140 // (re)create the listbox
141 void CreateLbox();
142
143 // listbox parameters
144 // ------------------
145
146 // the selection mode
147 enum LboxSelection
148 {
149 LboxSel_Single,
150 LboxSel_Extended,
151 LboxSel_Multiple
152 } m_lboxSelMode;
153
154 // should it be sorted?
155 bool m_sorted;
156
157 // should it have horz scroll/vert scrollbar permanently shown?
158 bool m_horzScroll,
159 m_vertScrollAlways;
160
161 // should the recreate button be enabled?
162 bool m_dirty;
163
164 // the controls
165 // ------------
166
167 // the sel mode radiobox
168 wxRadioBox *m_radioSelMode;
169
170 // the checkboxes
171 wxCheckBox *m_chkSort,
172 *m_chkHScroll,
173 *m_chkVScroll;
174
175 // the listbox itself and the sizer it is in
176 wxListBox *m_lbox;
177 wxSizer *m_sizerLbox;
178
179 // panel the controls such as the listbox are in
180 wxPanel* m_panel;
181
182 #if wxUSE_LOG
183 // the listbox for logging messages
184 wxListBox *m_lboxLog;
185 #endif // wxUSE_LOG
186
187 // the text entries for "Add/change string" and "Delete" buttons
188 wxTextCtrl *m_textAdd,
189 *m_textChange,
190 *m_textDelete;
191
192 private:
193 #if wxUSE_LOG
194 // the log target we use to redirect messages to the listbox
195 wxLog *m_logTarget;
196 #endif // wxUSE_LOG
197
198 // any class wishing to process wxWidgets events must use this macro
199 DECLARE_EVENT_TABLE()
200 };
201
202 #if wxUSE_LOG
203 // A log target which just redirects the messages to a listbox
204 class LboxLogger : public wxLog
205 {
206 public:
207 LboxLogger(wxListBox *lbox, wxLog *logOld)
208 {
209 m_lbox = lbox;
210 //m_lbox->Disable(); -- looks ugly under MSW
211 m_logOld = logOld;
212 }
213
214 virtual ~LboxLogger()
215 {
216 wxLog::SetActiveTarget(m_logOld);
217 }
218
219 private:
220 // implement sink functions
221 virtual void DoLog(wxLogLevel level, const wxChar *szString, time_t t)
222 {
223 // don't put trace messages into listbox or we can get into infinite
224 // recursion
225 if ( level == wxLOG_Trace )
226 {
227 if ( m_logOld )
228 {
229 // cast is needed to call protected method
230 ((LboxLogger *)m_logOld)->DoLog(level, szString, t);
231 }
232 }
233 else
234 {
235 wxLog::DoLog(level, szString, t);
236 }
237 }
238
239 virtual void DoLogString(const wxChar *szString, time_t WXUNUSED(t))
240 {
241 wxString msg;
242 TimeStamp(&msg);
243 msg += szString;
244 #ifdef __WXUNIVERSAL__
245 m_lbox->AppendAndEnsureVisible(msg);
246 #else // other ports don't have this method yet
247 m_lbox->Append(msg);
248
249 // SetFirstItem() isn't implemented in wxGTK
250 #ifndef __WXGTK__
251 m_lbox->SetFirstItem(m_lbox->GetCount() - 1);
252 #endif
253 #endif
254 }
255
256 // the control we use
257 wxListBox *m_lbox;
258
259 // the old log target
260 wxLog *m_logOld;
261 };
262 #endif // wxUSE_LOG
263
264 // ----------------------------------------------------------------------------
265 // misc macros
266 // ----------------------------------------------------------------------------
267
268 IMPLEMENT_APP(LboxTestApp)
269
270 // ----------------------------------------------------------------------------
271 // event tables
272 // ----------------------------------------------------------------------------
273
274 BEGIN_EVENT_TABLE(LboxTestFrame, wxFrame)
275 EVT_BUTTON(LboxTest_Reset, LboxTestFrame::OnButtonReset)
276 EVT_BUTTON(LboxTest_Create, LboxTestFrame::OnButtonCreate)
277 EVT_BUTTON(LboxTest_Change, LboxTestFrame::OnButtonChange)
278 EVT_BUTTON(LboxTest_Delete, LboxTestFrame::OnButtonDelete)
279 EVT_BUTTON(LboxTest_DeleteSel, LboxTestFrame::OnButtonDeleteSel)
280 EVT_BUTTON(LboxTest_Clear, LboxTestFrame::OnButtonClear)
281 #if wxUSE_LOG
282 EVT_BUTTON(LboxTest_ClearLog, LboxTestFrame::OnButtonClearLog)
283 #endif // wxUSE_LOG
284 EVT_BUTTON(LboxTest_Add, LboxTestFrame::OnButtonAdd)
285 EVT_BUTTON(LboxTest_AddSeveral, LboxTestFrame::OnButtonAddSeveral)
286 EVT_BUTTON(LboxTest_AddMany, LboxTestFrame::OnButtonAddMany)
287 EVT_BUTTON(LboxTest_Quit, LboxTestFrame::OnButtonQuit)
288
289 EVT_TEXT_ENTER(LboxTest_AddText, LboxTestFrame::OnButtonAdd)
290 EVT_TEXT_ENTER(LboxTest_DeleteText, LboxTestFrame::OnButtonDelete)
291
292 EVT_UPDATE_UI_RANGE(LboxTest_Reset, LboxTest_Create,
293 LboxTestFrame::OnUpdateUICreateButton)
294
295 EVT_UPDATE_UI(LboxTest_AddSeveral, LboxTestFrame::OnUpdateUIAddSeveral)
296 EVT_UPDATE_UI(LboxTest_Clear, LboxTestFrame::OnUpdateUIClearButton)
297 EVT_UPDATE_UI(LboxTest_DeleteText, LboxTestFrame::OnUpdateUIClearButton)
298 EVT_UPDATE_UI(LboxTest_Delete, LboxTestFrame::OnUpdateUIDeleteButton)
299 EVT_UPDATE_UI(LboxTest_Change, LboxTestFrame::OnUpdateUIDeleteSelButton)
300 EVT_UPDATE_UI(LboxTest_ChangeText, LboxTestFrame::OnUpdateUIDeleteSelButton)
301 EVT_UPDATE_UI(LboxTest_DeleteSel, LboxTestFrame::OnUpdateUIDeleteSelButton)
302
303 EVT_LISTBOX(LboxTest_Listbox, LboxTestFrame::OnListbox)
304 EVT_LISTBOX_DCLICK(wxID_ANY, LboxTestFrame::OnListboxDClick)
305 EVT_CHECKBOX(wxID_ANY, LboxTestFrame::OnCheckOrRadioBox)
306 EVT_RADIOBOX(wxID_ANY, LboxTestFrame::OnCheckOrRadioBox)
307 END_EVENT_TABLE()
308
309 // ============================================================================
310 // implementation
311 // ============================================================================
312
313 // ----------------------------------------------------------------------------
314 // app class
315 // ----------------------------------------------------------------------------
316
317 bool LboxTestApp::OnInit()
318 {
319 wxFrame *frame = new LboxTestFrame(_T("wxListBox sample"));
320 frame->Show();
321
322 #if wxUSE_LOG
323 //wxLog::AddTraceMask(_T("listbox"));
324 wxLog::AddTraceMask(_T("scrollbar"));
325 #endif // wxUSE_LOG
326
327 return true;
328 }
329
330 // ----------------------------------------------------------------------------
331 // top level frame class
332 // ----------------------------------------------------------------------------
333
334 LboxTestFrame::LboxTestFrame(const wxString& title)
335 : wxFrame(NULL, wxID_ANY, title, wxPoint(100, 100))
336 {
337 // init everything
338 m_dirty = false;
339 m_radioSelMode = (wxRadioBox *)NULL;
340
341 m_chkVScroll =
342 m_chkHScroll =
343 m_chkSort = (wxCheckBox *)NULL;
344
345 m_lbox = (wxListBox *)NULL;
346 #if wxUSE_LOG
347 m_lboxLog = (wxListBox *)NULL;
348 #endif // wxUSE_LOG
349 m_sizerLbox = (wxSizer *)NULL;
350
351 #if wxUSE_LOG
352 m_logTarget = (wxLog *)NULL;
353 #endif // wxUSE_LOG
354
355 m_panel = new wxPanel(this, wxID_ANY);
356
357 /*
358 What we create here is a frame having 3 panes: the explanatory pane to
359 the left allowing to set the listbox styles and recreate the control,
360 the pane containing the listbox itself and the lower pane containing
361 the buttons which allow to add/change/delete strings to/from it.
362 */
363 wxSizer *sizerTop = new wxBoxSizer(wxVERTICAL),
364 *sizerUp = new wxBoxSizer(wxHORIZONTAL),
365 *sizerLeft,
366 *sizerRight = new wxBoxSizer(wxVERTICAL);
367
368 // upper left pane
369 static const wxString modes[] =
370 {
371 _T("single"),
372 _T("extended"),
373 _T("multiple"),
374 };
375
376 wxStaticBox *box = new wxStaticBox(m_panel, wxID_ANY, _T("&Set listbox parameters"));
377 m_radioSelMode = new wxRadioBox(m_panel, wxID_ANY, _T("Selection &mode:"),
378 wxDefaultPosition, wxDefaultSize,
379 WXSIZEOF(modes), modes,
380 1, wxRA_SPECIFY_COLS);
381
382 m_chkVScroll = new wxCheckBox(m_panel, wxID_ANY, _T("Always show &vertical scrollbar"));
383 m_chkHScroll = new wxCheckBox(m_panel, wxID_ANY, _T("Show &horizontal scrollbar"));
384 m_chkSort = new wxCheckBox(m_panel, wxID_ANY, _T("&Sort items"));
385
386 sizerLeft = new wxStaticBoxSizer(box, wxVERTICAL);
387
388 sizerLeft->Add(m_chkVScroll, 0, wxLEFT | wxRIGHT, 5);
389 sizerLeft->Add(m_chkHScroll, 0, wxLEFT | wxRIGHT, 5);
390 sizerLeft->Add(m_chkSort, 0, wxLEFT | wxRIGHT, 5);
391 sizerLeft->Add(5, 5, 0, wxGROW | wxALL, 5); // spacer
392 sizerLeft->Add(m_radioSelMode, 0, wxGROW | wxALL, 5);
393
394 wxSizer *sizerBtn = new wxBoxSizer(wxHORIZONTAL);
395 wxButton *btn = new wxButton(m_panel, LboxTest_Reset, _T("&Reset"));
396 sizerBtn->Add(btn, 0, wxLEFT | wxRIGHT, 5);
397 btn = new wxButton(m_panel, LboxTest_Create, _T("&Create"));
398 sizerBtn->Add(btn, 0, wxLEFT | wxRIGHT, 5);
399 sizerLeft->Add(sizerBtn, 0, wxALIGN_CENTRE_HORIZONTAL | wxALL, 15);
400
401 // middle pane
402 wxStaticBox *box2 = new wxStaticBox(m_panel, wxID_ANY, _T("&Change listbox contents"));
403 wxSizer *sizerMiddle = new wxStaticBoxSizer(box2, wxVERTICAL);
404
405 wxSizer *sizerRow = new wxBoxSizer(wxHORIZONTAL);
406 btn = new wxButton(m_panel, LboxTest_Add, _T("&Add this string"));
407 m_textAdd = new wxTextCtrl(m_panel, LboxTest_AddText, _T("test item 0"));
408 sizerRow->Add(btn, 0, wxRIGHT, 5);
409 sizerRow->Add(m_textAdd, 1, wxLEFT, 5);
410 sizerMiddle->Add(sizerRow, 0, wxALL | wxGROW, 5);
411
412 btn = new wxButton(m_panel, LboxTest_AddSeveral, _T("&Insert a few strings"));
413 sizerMiddle->Add(btn, 0, wxALL | wxGROW, 5);
414
415 btn = new wxButton(m_panel, LboxTest_AddMany, _T("Add &many strings"));
416 sizerMiddle->Add(btn, 0, wxALL | wxGROW, 5);
417
418 sizerRow = new wxBoxSizer(wxHORIZONTAL);
419 btn = new wxButton(m_panel, LboxTest_Change, _T("C&hange current"));
420 m_textChange = new wxTextCtrl(m_panel, LboxTest_ChangeText, wxEmptyString);
421 sizerRow->Add(btn, 0, wxRIGHT, 5);
422 sizerRow->Add(m_textChange, 1, wxLEFT, 5);
423 sizerMiddle->Add(sizerRow, 0, wxALL | wxGROW, 5);
424
425 sizerRow = new wxBoxSizer(wxHORIZONTAL);
426 btn = new wxButton(m_panel, LboxTest_Delete, _T("&Delete this item"));
427 m_textDelete = new wxTextCtrl(m_panel, LboxTest_DeleteText, wxEmptyString);
428 sizerRow->Add(btn, 0, wxRIGHT, 5);
429 sizerRow->Add(m_textDelete, 1, wxLEFT, 5);
430 sizerMiddle->Add(sizerRow, 0, wxALL | wxGROW, 5);
431
432 btn = new wxButton(m_panel, LboxTest_DeleteSel, _T("Delete &selection"));
433 sizerMiddle->Add(btn, 0, wxALL | wxGROW, 5);
434
435 btn = new wxButton(m_panel, LboxTest_Clear, _T("&Clear"));
436 sizerMiddle->Add(btn, 0, wxALL | wxGROW, 5);
437
438 // right pane
439 sizerRight->SetMinSize(250, 0);
440 m_sizerLbox = sizerRight; // save it to modify it later
441
442 // the 3 panes panes compose the upper part of the window
443 sizerUp->Add(sizerLeft, 0, wxGROW | (wxALL & ~wxLEFT), 10);
444 sizerUp->Add(sizerMiddle, 1, wxGROW | wxALL, 10);
445 sizerUp->Add(sizerRight, 1, wxGROW | (wxALL & ~wxRIGHT), 10);
446
447 // the lower one only has the log listbox and a button to clear it
448 #if wxUSE_LOG
449 wxSizer *sizerDown = new wxStaticBoxSizer
450 (
451 new wxStaticBox(m_panel, wxID_ANY, _T("&Log window")),
452 wxVERTICAL
453 );
454 m_lboxLog = new wxListBox(m_panel, wxID_ANY);
455 sizerDown->Add(m_lboxLog, 1, wxGROW | wxALL, 5);
456 #else
457 wxSizer *sizerDown = new wxBoxSizer(wxVERTICAL);
458 #endif // wxUSE_LOG
459 wxBoxSizer *sizerBtns = new wxBoxSizer(wxHORIZONTAL);
460 #if wxUSE_LOG
461 btn = new wxButton(m_panel, LboxTest_ClearLog, _T("Clear &log"));
462 sizerBtns->Add(btn);
463 sizerBtns->Add(10, 0); // spacer
464 #endif // wxUSE_LOG
465 btn = new wxButton(m_panel, LboxTest_Quit, _T("E&xit"));
466 sizerBtns->Add(btn);
467 sizerDown->Add(sizerBtns, 0, wxALL | wxALIGN_RIGHT, 5);
468
469 // put everything together
470 sizerTop->Add(sizerUp, 1, wxGROW | (wxALL & ~wxBOTTOM), 10);
471 sizerTop->Add(0, 5, 0, wxGROW); // spacer in between
472 sizerTop->Add(sizerDown, 0, wxGROW | (wxALL & ~wxTOP), 10);
473
474 // final initialization and create the listbox
475 Reset();
476 CreateLbox();
477
478 m_panel->SetSizer(sizerTop);
479
480 sizerTop->Fit(this);
481 sizerTop->SetSizeHints(this);
482
483 #if wxUSE_LOG
484 // now that everything is created we can redirect the log messages to the
485 // listbox
486 m_logTarget = new LboxLogger(m_lboxLog, wxLog::GetActiveTarget());
487 wxLog::SetActiveTarget(m_logTarget);
488 #endif // wxUSE_LOG
489
490 m_lbox->Connect(wxEVT_RIGHT_DOWN,
491 wxMouseEventHandler(LboxTestFrame::OnListboxRDown), NULL, this);
492 }
493
494 LboxTestFrame::~LboxTestFrame()
495 {
496 #if wxUSE_LOG
497 delete m_logTarget;
498 #endif // wxUSE_LOG
499 }
500
501 // ----------------------------------------------------------------------------
502 // operations
503 // ----------------------------------------------------------------------------
504
505 void LboxTestFrame::Reset()
506 {
507 if ( m_radioSelMode->GetSelection() == LboxSel_Single &&
508 !m_chkSort->GetValue() &&
509 m_chkHScroll->GetValue() &&
510 !m_chkVScroll->GetValue() )
511 {
512 // nothing to do
513 return;
514 }
515
516 m_radioSelMode->SetSelection(LboxSel_Single);
517 m_chkSort->SetValue(false);
518 m_chkHScroll->SetValue(true);
519 m_chkVScroll->SetValue(false);
520
521 m_dirty = true;
522 }
523
524 void LboxTestFrame::CreateLbox()
525 {
526 int flags = 0;
527 switch ( m_radioSelMode->GetSelection() )
528 {
529 default:
530 wxFAIL_MSG( _T("unexpected radio box selection") );
531
532 case LboxSel_Single: flags |= wxLB_SINGLE; break;
533 case LboxSel_Extended: flags |= wxLB_EXTENDED; break;
534 case LboxSel_Multiple: flags |= wxLB_MULTIPLE; break;
535 }
536
537 if ( m_chkVScroll->GetValue() )
538 flags |= wxLB_ALWAYS_SB;
539 if ( m_chkHScroll->GetValue() )
540 flags |= wxLB_HSCROLL;
541 if ( m_chkSort->GetValue() )
542 flags |= wxLB_SORT;
543
544 wxArrayString items;
545
546 if ( m_lbox ) // cache old items to restore later if listbox existed
547 {
548 int count = m_lbox->GetCount();
549 for ( int n = 0; n < count; n++ )
550 {
551 items.Add(m_lbox->GetString(n));
552 }
553
554 m_sizerLbox->Detach(m_lbox);
555 delete m_lbox;
556 }
557
558 m_lbox = new wxListBox(m_panel, LboxTest_Listbox,
559 wxDefaultPosition, wxDefaultSize,
560 0, NULL,
561 flags);
562
563 m_lbox->Set(items);
564 m_sizerLbox->Add(m_lbox, 1, wxGROW | wxALL, 5);
565 m_sizerLbox->Layout();
566
567 m_dirty = false;
568
569 m_lbox->Connect(wxEVT_RIGHT_DOWN,
570 wxMouseEventHandler(LboxTestFrame::OnListboxRDown), NULL, this);
571 }
572
573 // ----------------------------------------------------------------------------
574 // event handlers
575 // ----------------------------------------------------------------------------
576
577 void LboxTestFrame::OnButtonQuit(wxCommandEvent& WXUNUSED(event))
578 {
579 Close();
580 }
581
582 void LboxTestFrame::OnButtonReset(wxCommandEvent& WXUNUSED(event))
583 {
584 Reset();
585 }
586
587 void LboxTestFrame::OnButtonCreate(wxCommandEvent& WXUNUSED(event))
588 {
589 CreateLbox();
590 }
591
592 void LboxTestFrame::OnButtonChange(wxCommandEvent& WXUNUSED(event))
593 {
594 wxArrayInt selections;
595 int count = m_lbox->GetSelections(selections);
596 wxString s = m_textChange->GetValue();
597 for ( int n = 0; n < count; n++ )
598 {
599 m_lbox->SetString(selections[n], s);
600 }
601 }
602
603 void LboxTestFrame::OnButtonDelete(wxCommandEvent& WXUNUSED(event))
604 {
605 unsigned long n;
606 if ( !m_textDelete->GetValue().ToULong(&n) ||
607 (n >= (unsigned)m_lbox->GetCount()) )
608 {
609 return;
610 }
611
612 m_lbox->Delete(n);
613 }
614
615 void LboxTestFrame::OnButtonDeleteSel(wxCommandEvent& WXUNUSED(event))
616 {
617 wxArrayInt selections;
618 int n = m_lbox->GetSelections(selections);
619 while ( n > 0 )
620 {
621 m_lbox->Delete(selections[--n]);
622 }
623 }
624
625 void LboxTestFrame::OnButtonClear(wxCommandEvent& WXUNUSED(event))
626 {
627 m_lbox->Clear();
628 }
629
630 #if wxUSE_LOG
631 void LboxTestFrame::OnButtonClearLog(wxCommandEvent& WXUNUSED(event))
632 {
633 m_lboxLog->Clear();
634 }
635 #endif // wxUSE_LOG
636
637 void LboxTestFrame::OnButtonAdd(wxCommandEvent& WXUNUSED(event))
638 {
639 static size_t s_item = 0;
640
641 wxString s = m_textAdd->GetValue();
642 if ( !m_textAdd->IsModified() )
643 {
644 // update the default string
645 m_textAdd->SetValue(wxString::Format(_T("test item %u"), ++s_item));
646 }
647
648 m_lbox->Append(s);
649 }
650
651 void LboxTestFrame::OnButtonAddMany(wxCommandEvent& WXUNUSED(event))
652 {
653 // "many" means 1000 here
654 for ( size_t n = 0; n < 1000; n++ )
655 {
656 m_lbox->Append(wxString::Format(_T("item #%u"), n));
657 }
658 }
659
660 void LboxTestFrame::OnButtonAddSeveral(wxCommandEvent& WXUNUSED(event))
661 {
662 wxArrayString items;
663 items.Add(_T("First"));
664 items.Add(_T("another one"));
665 items.Add(_T("and the last (very very very very very very very very very very long) one"));
666 m_lbox->InsertItems(items, 0);
667 }
668
669 void LboxTestFrame::OnUpdateUICreateButton(wxUpdateUIEvent& event)
670 {
671 event.Enable(m_dirty);
672 }
673
674 void LboxTestFrame::OnUpdateUIDeleteButton(wxUpdateUIEvent& event)
675 {
676 unsigned long n;
677 event.Enable(m_textDelete->GetValue().ToULong(&n) &&
678 (n < (unsigned)m_lbox->GetCount()));
679 }
680
681 void LboxTestFrame::OnUpdateUIDeleteSelButton(wxUpdateUIEvent& event)
682 {
683 wxArrayInt selections;
684 event.Enable(m_lbox->GetSelections(selections) != 0);
685 }
686
687 void LboxTestFrame::OnUpdateUIClearButton(wxUpdateUIEvent& event)
688 {
689 event.Enable(m_lbox->GetCount() != 0);
690 }
691
692 void LboxTestFrame::OnUpdateUIAddSeveral(wxUpdateUIEvent& event)
693 {
694 event.Enable(!(m_lbox->GetWindowStyle() & wxLB_SORT));
695 }
696
697 void LboxTestFrame::OnListbox(wxCommandEvent& event)
698 {
699 int sel = event.GetInt();
700 m_textDelete->SetValue(wxString::Format(_T("%d"), sel));
701
702 wxLogMessage(_T("Listbox item %d selected"), sel);
703 }
704
705 void LboxTestFrame::OnListboxDClick(wxCommandEvent& event)
706 {
707 int sel = event.GetInt();
708 wxLogMessage(_T("Listbox item %d double clicked"), sel);
709 }
710
711 void LboxTestFrame::OnListboxRDown(wxMouseEvent& event)
712 {
713 int item = m_lbox->HitTest(event.GetPosition());
714
715 if ( item != wxNOT_FOUND )
716 wxLogMessage(_T("Listbox item %d right clicked"), item);
717 else
718 wxLogMessage(_T("Listbox right clicked but no item clicked upon"));
719
720 event.Skip();
721 }
722
723 void LboxTestFrame::OnCheckOrRadioBox(wxCommandEvent& WXUNUSED(event))
724 {
725 m_dirty = true;
726 }
727