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