]> git.saurik.com Git - wxWidgets.git/blame_incremental - samples/listbox/lboxtest.cpp
added wxListBox::HitTest() from Ryan (patch 1446207)
[wxWidgets.git] / samples / listbox / lboxtest.cpp
... / ...
CommitLineData
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
63enum
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
89class LboxTestApp : public wxApp
90{
91public:
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
102class LboxTestFrame : public wxFrame
103{
104public:
105 // ctor(s) and dtor
106 LboxTestFrame(const wxString& title);
107 virtual ~LboxTestFrame();
108
109protected:
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#if wxUSE_LOG
180 // the listbox for logging messages
181 wxListBox *m_lboxLog;
182#endif // wxUSE_LOG
183
184 // the text entries for "Add/change string" and "Delete" buttons
185 wxTextCtrl *m_textAdd,
186 *m_textChange,
187 *m_textDelete;
188
189private:
190#if wxUSE_LOG
191 // the log target we use to redirect messages to the listbox
192 wxLog *m_logTarget;
193#endif // wxUSE_LOG
194
195 // any class wishing to process wxWidgets events must use this macro
196 DECLARE_EVENT_TABLE()
197};
198
199#if wxUSE_LOG
200// A log target which just redirects the messages to a listbox
201class LboxLogger : public wxLog
202{
203public:
204 LboxLogger(wxListBox *lbox, wxLog *logOld)
205 {
206 m_lbox = lbox;
207 //m_lbox->Disable(); -- looks ugly under MSW
208 m_logOld = logOld;
209 }
210
211 virtual ~LboxLogger()
212 {
213 wxLog::SetActiveTarget(m_logOld);
214 }
215
216private:
217 // implement sink functions
218 virtual void DoLog(wxLogLevel level, const wxChar *szString, time_t t)
219 {
220 // don't put trace messages into listbox or we can get into infinite
221 // recursion
222 if ( level == wxLOG_Trace )
223 {
224 if ( m_logOld )
225 {
226 // cast is needed to call protected method
227 ((LboxLogger *)m_logOld)->DoLog(level, szString, t);
228 }
229 }
230 else
231 {
232 wxLog::DoLog(level, szString, t);
233 }
234 }
235
236 virtual void DoLogString(const wxChar *szString, time_t WXUNUSED(t))
237 {
238 wxString msg;
239 TimeStamp(&msg);
240 msg += szString;
241 #ifdef __WXUNIVERSAL__
242 m_lbox->AppendAndEnsureVisible(msg);
243 #else // other ports don't have this method yet
244 m_lbox->Append(msg);
245
246 // SetFirstItem() isn't implemented in wxGTK
247 #ifndef __WXGTK__
248 m_lbox->SetFirstItem(m_lbox->GetCount() - 1);
249 #endif
250 #endif
251 }
252
253 // the control we use
254 wxListBox *m_lbox;
255
256 // the old log target
257 wxLog *m_logOld;
258};
259#endif // wxUSE_LOG
260
261// ----------------------------------------------------------------------------
262// misc macros
263// ----------------------------------------------------------------------------
264
265IMPLEMENT_APP(LboxTestApp)
266
267// ----------------------------------------------------------------------------
268// event tables
269// ----------------------------------------------------------------------------
270
271BEGIN_EVENT_TABLE(LboxTestFrame, wxFrame)
272 EVT_BUTTON(LboxTest_Reset, LboxTestFrame::OnButtonReset)
273 EVT_BUTTON(LboxTest_Create, LboxTestFrame::OnButtonCreate)
274 EVT_BUTTON(LboxTest_Change, LboxTestFrame::OnButtonChange)
275 EVT_BUTTON(LboxTest_Delete, LboxTestFrame::OnButtonDelete)
276 EVT_BUTTON(LboxTest_DeleteSel, LboxTestFrame::OnButtonDeleteSel)
277 EVT_BUTTON(LboxTest_Clear, LboxTestFrame::OnButtonClear)
278#if wxUSE_LOG
279 EVT_BUTTON(LboxTest_ClearLog, LboxTestFrame::OnButtonClearLog)
280#endif // wxUSE_LOG
281 EVT_BUTTON(LboxTest_Add, LboxTestFrame::OnButtonAdd)
282 EVT_BUTTON(LboxTest_AddSeveral, LboxTestFrame::OnButtonAddSeveral)
283 EVT_BUTTON(LboxTest_AddMany, LboxTestFrame::OnButtonAddMany)
284 EVT_BUTTON(LboxTest_Quit, LboxTestFrame::OnButtonQuit)
285
286 EVT_TEXT_ENTER(LboxTest_AddText, LboxTestFrame::OnButtonAdd)
287 EVT_TEXT_ENTER(LboxTest_DeleteText, LboxTestFrame::OnButtonDelete)
288
289 EVT_UPDATE_UI_RANGE(LboxTest_Reset, LboxTest_Create,
290 LboxTestFrame::OnUpdateUICreateButton)
291
292 EVT_UPDATE_UI(LboxTest_AddSeveral, LboxTestFrame::OnUpdateUIAddSeveral)
293 EVT_UPDATE_UI(LboxTest_Clear, LboxTestFrame::OnUpdateUIClearButton)
294 EVT_UPDATE_UI(LboxTest_DeleteText, LboxTestFrame::OnUpdateUIClearButton)
295 EVT_UPDATE_UI(LboxTest_Delete, LboxTestFrame::OnUpdateUIDeleteButton)
296 EVT_UPDATE_UI(LboxTest_Change, LboxTestFrame::OnUpdateUIDeleteSelButton)
297 EVT_UPDATE_UI(LboxTest_ChangeText, LboxTestFrame::OnUpdateUIDeleteSelButton)
298 EVT_UPDATE_UI(LboxTest_DeleteSel, LboxTestFrame::OnUpdateUIDeleteSelButton)
299
300 EVT_LISTBOX(LboxTest_Listbox, LboxTestFrame::OnListbox)
301 EVT_LISTBOX_DCLICK(wxID_ANY, LboxTestFrame::OnListboxDClick)
302 EVT_CHECKBOX(wxID_ANY, LboxTestFrame::OnCheckOrRadioBox)
303 EVT_RADIOBOX(wxID_ANY, LboxTestFrame::OnCheckOrRadioBox)
304END_EVENT_TABLE()
305
306// ============================================================================
307// implementation
308// ============================================================================
309
310// ----------------------------------------------------------------------------
311// app class
312// ----------------------------------------------------------------------------
313
314bool LboxTestApp::OnInit()
315{
316 wxFrame *frame = new LboxTestFrame(_T("wxListBox sample"));
317 frame->Show();
318
319#if wxUSE_LOG
320 //wxLog::AddTraceMask(_T("listbox"));
321 wxLog::AddTraceMask(_T("scrollbar"));
322#endif // wxUSE_LOG
323
324 return true;
325}
326
327// ----------------------------------------------------------------------------
328// top level frame class
329// ----------------------------------------------------------------------------
330
331LboxTestFrame::LboxTestFrame(const wxString& title)
332 : wxFrame(NULL, wxID_ANY, title, wxPoint(100, 100))
333{
334 // init everything
335 m_dirty = false;
336 m_radioSelMode = (wxRadioBox *)NULL;
337
338 m_chkVScroll =
339 m_chkHScroll =
340 m_chkSort = (wxCheckBox *)NULL;
341
342 m_lbox = (wxListBox *)NULL;
343#if wxUSE_LOG
344 m_lboxLog = (wxListBox *)NULL;
345#endif // wxUSE_LOG
346 m_sizerLbox = (wxSizer *)NULL;
347
348#if wxUSE_LOG
349 m_logTarget = (wxLog *)NULL;
350#endif // wxUSE_LOG
351
352 wxPanel *panel = new wxPanel(this, wxID_ANY);
353
354 /*
355 What we create here is a frame having 3 panes: the explanatory pane to
356 the left allowing to set the listbox styles and recreate the control,
357 the pane containing the listbox itself and the lower pane containing
358 the buttons which allow to add/change/delete strings to/from it.
359 */
360 wxSizer *sizerTop = new wxBoxSizer(wxVERTICAL),
361 *sizerUp = new wxBoxSizer(wxHORIZONTAL),
362 *sizerLeft,
363 *sizerRight = new wxBoxSizer(wxVERTICAL);
364
365 // upper left pane
366 static const wxString modes[] =
367 {
368 _T("single"),
369 _T("extended"),
370 _T("multiple"),
371 };
372
373 wxStaticBox *box = new wxStaticBox(panel, wxID_ANY, _T("&Set listbox parameters"));
374 m_radioSelMode = new wxRadioBox(panel, wxID_ANY, _T("Selection &mode:"),
375 wxDefaultPosition, wxDefaultSize,
376 WXSIZEOF(modes), modes,
377 1, wxRA_SPECIFY_COLS);
378
379 m_chkVScroll = new wxCheckBox(panel, wxID_ANY, _T("Always show &vertical scrollbar"));
380 m_chkHScroll = new wxCheckBox(panel, wxID_ANY, _T("Show &horizontal scrollbar"));
381 m_chkSort = new wxCheckBox(panel, wxID_ANY, _T("&Sort items"));
382
383 sizerLeft = new wxStaticBoxSizer(box, wxVERTICAL);
384
385 sizerLeft->Add(m_chkVScroll, 0, wxLEFT | wxRIGHT, 5);
386 sizerLeft->Add(m_chkHScroll, 0, wxLEFT | wxRIGHT, 5);
387 sizerLeft->Add(m_chkSort, 0, wxLEFT | wxRIGHT, 5);
388 sizerLeft->Add(5, 5, 0, wxGROW | wxALL, 5); // spacer
389 sizerLeft->Add(m_radioSelMode, 0, wxGROW | wxALL, 5);
390
391 wxSizer *sizerBtn = new wxBoxSizer(wxHORIZONTAL);
392 wxButton *btn = new wxButton(panel, LboxTest_Reset, _T("&Reset"));
393 sizerBtn->Add(btn, 0, wxLEFT | wxRIGHT, 5);
394 btn = new wxButton(panel, LboxTest_Create, _T("&Create"));
395 sizerBtn->Add(btn, 0, wxLEFT | wxRIGHT, 5);
396 sizerLeft->Add(sizerBtn, 0, wxALIGN_CENTRE_HORIZONTAL | wxALL, 15);
397
398 // middle pane
399 wxStaticBox *box2 = new wxStaticBox(panel, wxID_ANY, _T("&Change listbox contents"));
400 wxSizer *sizerMiddle = new wxStaticBoxSizer(box2, wxVERTICAL);
401
402 wxSizer *sizerRow = new wxBoxSizer(wxHORIZONTAL);
403 btn = new wxButton(panel, LboxTest_Add, _T("&Add this string"));
404 m_textAdd = new wxTextCtrl(panel, LboxTest_AddText, _T("test item 0"));
405 sizerRow->Add(btn, 0, wxRIGHT, 5);
406 sizerRow->Add(m_textAdd, 1, wxLEFT, 5);
407 sizerMiddle->Add(sizerRow, 0, wxALL | wxGROW, 5);
408
409 btn = new wxButton(panel, LboxTest_AddSeveral, _T("&Insert a few strings"));
410 sizerMiddle->Add(btn, 0, wxALL | wxGROW, 5);
411
412 btn = new wxButton(panel, LboxTest_AddMany, _T("Add &many strings"));
413 sizerMiddle->Add(btn, 0, wxALL | wxGROW, 5);
414
415 sizerRow = new wxBoxSizer(wxHORIZONTAL);
416 btn = new wxButton(panel, LboxTest_Change, _T("C&hange current"));
417 m_textChange = new wxTextCtrl(panel, LboxTest_ChangeText, wxEmptyString);
418 sizerRow->Add(btn, 0, wxRIGHT, 5);
419 sizerRow->Add(m_textChange, 1, wxLEFT, 5);
420 sizerMiddle->Add(sizerRow, 0, wxALL | wxGROW, 5);
421
422 sizerRow = new wxBoxSizer(wxHORIZONTAL);
423 btn = new wxButton(panel, LboxTest_Delete, _T("&Delete this item"));
424 m_textDelete = new wxTextCtrl(panel, LboxTest_DeleteText, wxEmptyString);
425 sizerRow->Add(btn, 0, wxRIGHT, 5);
426 sizerRow->Add(m_textDelete, 1, wxLEFT, 5);
427 sizerMiddle->Add(sizerRow, 0, wxALL | wxGROW, 5);
428
429 btn = new wxButton(panel, LboxTest_DeleteSel, _T("Delete &selection"));
430 sizerMiddle->Add(btn, 0, wxALL | wxGROW, 5);
431
432 btn = new wxButton(panel, LboxTest_Clear, _T("&Clear"));
433 sizerMiddle->Add(btn, 0, wxALL | wxGROW, 5);
434
435 // right pane
436 m_lbox = new wxListBox(panel, LboxTest_Listbox,
437 wxDefaultPosition, wxDefaultSize,
438 0, NULL,
439 wxLB_HSCROLL);
440 sizerRight->Add(m_lbox, 1, wxGROW | wxALL, 5);
441 sizerRight->SetMinSize(250, 0);
442 m_sizerLbox = sizerRight; // save it to modify it later
443
444 // the 3 panes panes compose the upper part of the window
445 sizerUp->Add(sizerLeft, 0, wxGROW | (wxALL & ~wxLEFT), 10);
446 sizerUp->Add(sizerMiddle, 1, wxGROW | wxALL, 10);
447 sizerUp->Add(sizerRight, 1, wxGROW | (wxALL & ~wxRIGHT), 10);
448
449 // the lower one only has the log listbox and a button to clear it
450#if wxUSE_LOG
451 wxSizer *sizerDown = new wxStaticBoxSizer
452 (
453 new wxStaticBox(panel, wxID_ANY, _T("&Log window")),
454 wxVERTICAL
455 );
456 m_lboxLog = new wxListBox(panel, wxID_ANY);
457 sizerDown->Add(m_lboxLog, 1, wxGROW | wxALL, 5);
458#else
459 wxSizer *sizerDown = new wxBoxSizer(wxVERTICAL);
460#endif // wxUSE_LOG
461 wxBoxSizer *sizerBtns = new wxBoxSizer(wxHORIZONTAL);
462#if wxUSE_LOG
463 btn = new wxButton(panel, LboxTest_ClearLog, _T("Clear &log"));
464 sizerBtns->Add(btn);
465 sizerBtns->Add(10, 0); // spacer
466#endif // wxUSE_LOG
467 btn = new wxButton(panel, LboxTest_Quit, _T("E&xit"));
468 sizerBtns->Add(btn);
469 sizerDown->Add(sizerBtns, 0, wxALL | wxALIGN_RIGHT, 5);
470
471 // put everything together
472 sizerTop->Add(sizerUp, 1, wxGROW | (wxALL & ~wxBOTTOM), 10);
473 sizerTop->Add(0, 5, 0, wxGROW); // spacer in between
474 sizerTop->Add(sizerDown, 0, wxGROW | (wxALL & ~wxTOP), 10);
475
476 // final initialization
477 Reset();
478 m_dirty = false;
479
480 panel->SetSizer(sizerTop);
481
482 sizerTop->Fit(this);
483 sizerTop->SetSizeHints(this);
484
485#if wxUSE_LOG
486 // now that everything is created we can redirect the log messages to the
487 // listbox
488 m_logTarget = new LboxLogger(m_lboxLog, wxLog::GetActiveTarget());
489 wxLog::SetActiveTarget(m_logTarget);
490#endif // wxUSE_LOG
491
492 m_lbox->Connect(wxEVT_RIGHT_DOWN,
493 wxMouseEventHandler(LboxTestFrame::OnListboxRDown), NULL, this);
494}
495
496LboxTestFrame::~LboxTestFrame()
497{
498#if wxUSE_LOG
499 delete m_logTarget;
500#endif // wxUSE_LOG
501}
502
503// ----------------------------------------------------------------------------
504// operations
505// ----------------------------------------------------------------------------
506
507void LboxTestFrame::Reset()
508{
509 if ( m_radioSelMode->GetSelection() == LboxSel_Single &&
510 !m_chkSort->GetValue() &&
511 m_chkHScroll->GetValue() &&
512 !m_chkVScroll->GetValue() )
513 {
514 // nothing to do
515 return;
516 }
517
518 m_radioSelMode->SetSelection(LboxSel_Single);
519 m_chkSort->SetValue(false);
520 m_chkHScroll->SetValue(true);
521 m_chkVScroll->SetValue(false);
522
523 m_dirty = true;
524}
525
526void LboxTestFrame::CreateLbox()
527{
528 wxWindow *parent = m_lbox->GetParent();
529 int flags = 0;
530 switch ( m_radioSelMode->GetSelection() )
531 {
532 default:
533 wxFAIL_MSG( _T("unexpected radio box selection") );
534
535 case LboxSel_Single: flags |= wxLB_SINGLE; break;
536 case LboxSel_Extended: flags |= wxLB_EXTENDED; break;
537 case LboxSel_Multiple: flags |= wxLB_MULTIPLE; break;
538 }
539
540 if ( m_chkVScroll->GetValue() )
541 flags |= wxLB_ALWAYS_SB;
542 if ( m_chkHScroll->GetValue() )
543 flags |= wxLB_HSCROLL;
544 if ( m_chkSort->GetValue() )
545 flags |= wxLB_SORT;
546
547 wxArrayString items;
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 m_lbox = new wxListBox(parent, LboxTest_Listbox,
558 wxDefaultPosition, wxDefaultSize,
559 0, NULL,
560 flags);
561 m_lbox->Set(items);
562 m_sizerLbox->Add(m_lbox, 1, wxGROW | wxALL, 5);
563 m_sizerLbox->Layout();
564
565 m_dirty = false;
566}
567
568// ----------------------------------------------------------------------------
569// event handlers
570// ----------------------------------------------------------------------------
571
572void LboxTestFrame::OnButtonQuit(wxCommandEvent& WXUNUSED(event))
573{
574 Close();
575}
576
577void LboxTestFrame::OnButtonReset(wxCommandEvent& WXUNUSED(event))
578{
579 Reset();
580}
581
582void LboxTestFrame::OnButtonCreate(wxCommandEvent& WXUNUSED(event))
583{
584 CreateLbox();
585}
586
587void LboxTestFrame::OnButtonChange(wxCommandEvent& WXUNUSED(event))
588{
589 wxArrayInt selections;
590 int count = m_lbox->GetSelections(selections);
591 wxString s = m_textChange->GetValue();
592 for ( int n = 0; n < count; n++ )
593 {
594 m_lbox->SetString(selections[n], s);
595 }
596}
597
598void LboxTestFrame::OnButtonDelete(wxCommandEvent& WXUNUSED(event))
599{
600 unsigned long n;
601 if ( !m_textDelete->GetValue().ToULong(&n) ||
602 (n >= (unsigned)m_lbox->GetCount()) )
603 {
604 return;
605 }
606
607 m_lbox->Delete(n);
608}
609
610void LboxTestFrame::OnButtonDeleteSel(wxCommandEvent& WXUNUSED(event))
611{
612 wxArrayInt selections;
613 int n = m_lbox->GetSelections(selections);
614 while ( n > 0 )
615 {
616 m_lbox->Delete(selections[--n]);
617 }
618}
619
620void LboxTestFrame::OnButtonClear(wxCommandEvent& WXUNUSED(event))
621{
622 m_lbox->Clear();
623}
624
625#if wxUSE_LOG
626void LboxTestFrame::OnButtonClearLog(wxCommandEvent& WXUNUSED(event))
627{
628 m_lboxLog->Clear();
629}
630#endif // wxUSE_LOG
631
632void LboxTestFrame::OnButtonAdd(wxCommandEvent& WXUNUSED(event))
633{
634 static size_t s_item = 0;
635
636 wxString s = m_textAdd->GetValue();
637 if ( !m_textAdd->IsModified() )
638 {
639 // update the default string
640 m_textAdd->SetValue(wxString::Format(_T("test item %u"), ++s_item));
641 }
642
643 m_lbox->Append(s);
644}
645
646void LboxTestFrame::OnButtonAddMany(wxCommandEvent& WXUNUSED(event))
647{
648 // "many" means 1000 here
649 for ( size_t n = 0; n < 1000; n++ )
650 {
651 m_lbox->Append(wxString::Format(_T("item #%u"), n));
652 }
653}
654
655void LboxTestFrame::OnButtonAddSeveral(wxCommandEvent& WXUNUSED(event))
656{
657 wxArrayString items;
658 items.Add(_T("First"));
659 items.Add(_T("another one"));
660 items.Add(_T("and the last (very very very very very very very very very very long) one"));
661 m_lbox->InsertItems(items, 0);
662}
663
664void LboxTestFrame::OnUpdateUICreateButton(wxUpdateUIEvent& event)
665{
666 event.Enable(m_dirty);
667}
668
669void LboxTestFrame::OnUpdateUIDeleteButton(wxUpdateUIEvent& event)
670{
671 unsigned long n;
672 event.Enable(m_textDelete->GetValue().ToULong(&n) &&
673 (n < (unsigned)m_lbox->GetCount()));
674}
675
676void LboxTestFrame::OnUpdateUIDeleteSelButton(wxUpdateUIEvent& event)
677{
678 wxArrayInt selections;
679 event.Enable(m_lbox->GetSelections(selections) != 0);
680}
681
682void LboxTestFrame::OnUpdateUIClearButton(wxUpdateUIEvent& event)
683{
684 event.Enable(m_lbox->GetCount() != 0);
685}
686
687void LboxTestFrame::OnUpdateUIAddSeveral(wxUpdateUIEvent& event)
688{
689 event.Enable(!(m_lbox->GetWindowStyle() & wxLB_SORT));
690}
691
692void LboxTestFrame::OnListbox(wxCommandEvent& event)
693{
694 int sel = event.GetInt();
695 m_textDelete->SetValue(wxString::Format(_T("%d"), sel));
696
697 wxLogMessage(_T("Listbox item %d selected"), sel);
698}
699
700void LboxTestFrame::OnListboxDClick(wxCommandEvent& event)
701{
702 int sel = event.GetInt();
703 wxLogMessage(_T("Listbox item %d double clicked"), sel);
704}
705
706void LboxTestFrame::OnListboxRDown(wxMouseEvent& event)
707{
708 int item = m_lbox->HitTest(event.GetPosition());
709
710 if ( item != wxNOT_FOUND )
711 wxLogMessage(_T("Listbox item %d right clicked"), item);
712 else
713 wxLogMessage(_T("Listbox right clicked but no item clicked upon"));
714
715 event.Skip();
716}
717
718void LboxTestFrame::OnCheckOrRadioBox(wxCommandEvent& WXUNUSED(event))
719{
720 m_dirty = true;
721}
722