]> git.saurik.com Git - wxWidgets.git/blame - samples/listbox/lboxtest.cpp
allow adjusting the combo button size (patch 1489452)
[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
4f37a20b
VZ
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
dacaa6f1 36 #include "wx/wx.h"
4f37a20b
VZ
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,
b29903d4 72#if wxUSE_LOG
4f37a20b 73 LboxTest_ClearLog,
b29903d4 74#endif // wxUSE_LOG
4f37a20b
VZ
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);
b29903d4 117#if wxUSE_LOG
4f37a20b 118 void OnButtonClearLog(wxCommandEvent& event);
b29903d4 119#endif // wxUSE_LOG
4f37a20b
VZ
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);
c00fed0e 127 void OnListboxRDown(wxMouseEvent& event);
4f37a20b
VZ
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
b4deaacb
VZ
179 // panel the controls such as the listbox are in
180 wxPanel* m_panel;
181
b29903d4 182#if wxUSE_LOG
4f37a20b
VZ
183 // the listbox for logging messages
184 wxListBox *m_lboxLog;
b29903d4 185#endif // wxUSE_LOG
4f37a20b
VZ
186
187 // the text entries for "Add/change string" and "Delete" buttons
188 wxTextCtrl *m_textAdd,
189 *m_textChange,
190 *m_textDelete;
191
192private:
b29903d4 193#if wxUSE_LOG
4f37a20b
VZ
194 // the log target we use to redirect messages to the listbox
195 wxLog *m_logTarget;
b29903d4 196#endif // wxUSE_LOG
4f37a20b 197
be5a51fb 198 // any class wishing to process wxWidgets events must use this macro
4f37a20b
VZ
199 DECLARE_EVENT_TABLE()
200};
201
b29903d4 202#if wxUSE_LOG
4f37a20b
VZ
203// A log target which just redirects the messages to a listbox
204class LboxLogger : public wxLog
205{
206public:
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
219private:
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
d1f47235 239 virtual void DoLogString(const wxChar *szString, time_t WXUNUSED(t))
4f37a20b
VZ
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};
b29903d4 262#endif // wxUSE_LOG
4f37a20b
VZ
263
264// ----------------------------------------------------------------------------
265// misc macros
266// ----------------------------------------------------------------------------
267
268IMPLEMENT_APP(LboxTestApp)
269
4f37a20b
VZ
270// ----------------------------------------------------------------------------
271// event tables
272// ----------------------------------------------------------------------------
273
274BEGIN_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)
b29903d4 281#if wxUSE_LOG
4f37a20b 282 EVT_BUTTON(LboxTest_ClearLog, LboxTestFrame::OnButtonClearLog)
b29903d4 283#endif // wxUSE_LOG
4f37a20b
VZ
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)
11180f77
WS
304 EVT_LISTBOX_DCLICK(wxID_ANY, LboxTestFrame::OnListboxDClick)
305 EVT_CHECKBOX(wxID_ANY, LboxTestFrame::OnCheckOrRadioBox)
306 EVT_RADIOBOX(wxID_ANY, LboxTestFrame::OnCheckOrRadioBox)
4f37a20b
VZ
307END_EVENT_TABLE()
308
309// ============================================================================
310// implementation
311// ============================================================================
312
313// ----------------------------------------------------------------------------
314// app class
315// ----------------------------------------------------------------------------
316
317bool LboxTestApp::OnInit()
318{
319 wxFrame *frame = new LboxTestFrame(_T("wxListBox sample"));
320 frame->Show();
321
b29903d4 322#if wxUSE_LOG
4f37a20b
VZ
323 //wxLog::AddTraceMask(_T("listbox"));
324 wxLog::AddTraceMask(_T("scrollbar"));
b29903d4 325#endif // wxUSE_LOG
4f37a20b 326
11180f77 327 return true;
4f37a20b
VZ
328}
329
330// ----------------------------------------------------------------------------
331// top level frame class
332// ----------------------------------------------------------------------------
333
334LboxTestFrame::LboxTestFrame(const wxString& title)
11180f77 335 : wxFrame(NULL, wxID_ANY, title, wxPoint(100, 100))
4f37a20b
VZ
336{
337 // init everything
11180f77 338 m_dirty = false;
4f37a20b
VZ
339 m_radioSelMode = (wxRadioBox *)NULL;
340
341 m_chkVScroll =
342 m_chkHScroll =
343 m_chkSort = (wxCheckBox *)NULL;
344
b29903d4
WS
345 m_lbox = (wxListBox *)NULL;
346#if wxUSE_LOG
4f37a20b 347 m_lboxLog = (wxListBox *)NULL;
b29903d4 348#endif // wxUSE_LOG
4f37a20b
VZ
349 m_sizerLbox = (wxSizer *)NULL;
350
b29903d4 351#if wxUSE_LOG
4f37a20b 352 m_logTarget = (wxLog *)NULL;
b29903d4 353#endif // wxUSE_LOG
4f37a20b 354
b4deaacb 355 m_panel = new wxPanel(this, wxID_ANY);
4f37a20b
VZ
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
b4deaacb
VZ
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:"),
4f37a20b
VZ
378 wxDefaultPosition, wxDefaultSize,
379 WXSIZEOF(modes), modes,
380 1, wxRA_SPECIFY_COLS);
381
b4deaacb
VZ
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"));
4f37a20b
VZ
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);
b4deaacb 395 wxButton *btn = new wxButton(m_panel, LboxTest_Reset, _T("&Reset"));
4f37a20b 396 sizerBtn->Add(btn, 0, wxLEFT | wxRIGHT, 5);
b4deaacb 397 btn = new wxButton(m_panel, LboxTest_Create, _T("&Create"));
4f37a20b
VZ
398 sizerBtn->Add(btn, 0, wxLEFT | wxRIGHT, 5);
399 sizerLeft->Add(sizerBtn, 0, wxALIGN_CENTRE_HORIZONTAL | wxALL, 15);
400
401 // middle pane
b4deaacb 402 wxStaticBox *box2 = new wxStaticBox(m_panel, wxID_ANY, _T("&Change listbox contents"));
4f37a20b
VZ
403 wxSizer *sizerMiddle = new wxStaticBoxSizer(box2, wxVERTICAL);
404
405 wxSizer *sizerRow = new wxBoxSizer(wxHORIZONTAL);
b4deaacb
VZ
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"));
4f37a20b
VZ
408 sizerRow->Add(btn, 0, wxRIGHT, 5);
409 sizerRow->Add(m_textAdd, 1, wxLEFT, 5);
410 sizerMiddle->Add(sizerRow, 0, wxALL | wxGROW, 5);
411
b4deaacb 412 btn = new wxButton(m_panel, LboxTest_AddSeveral, _T("&Insert a few strings"));
4f37a20b
VZ
413 sizerMiddle->Add(btn, 0, wxALL | wxGROW, 5);
414
b4deaacb 415 btn = new wxButton(m_panel, LboxTest_AddMany, _T("Add &many strings"));
4f37a20b
VZ
416 sizerMiddle->Add(btn, 0, wxALL | wxGROW, 5);
417
418 sizerRow = new wxBoxSizer(wxHORIZONTAL);
b4deaacb
VZ
419 btn = new wxButton(m_panel, LboxTest_Change, _T("C&hange current"));
420 m_textChange = new wxTextCtrl(m_panel, LboxTest_ChangeText, wxEmptyString);
4f37a20b
VZ
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);
b4deaacb
VZ
426 btn = new wxButton(m_panel, LboxTest_Delete, _T("&Delete this item"));
427 m_textDelete = new wxTextCtrl(m_panel, LboxTest_DeleteText, wxEmptyString);
4f37a20b
VZ
428 sizerRow->Add(btn, 0, wxRIGHT, 5);
429 sizerRow->Add(m_textDelete, 1, wxLEFT, 5);
430 sizerMiddle->Add(sizerRow, 0, wxALL | wxGROW, 5);
431
b4deaacb 432 btn = new wxButton(m_panel, LboxTest_DeleteSel, _T("Delete &selection"));
4f37a20b
VZ
433 sizerMiddle->Add(btn, 0, wxALL | wxGROW, 5);
434
b4deaacb 435 btn = new wxButton(m_panel, LboxTest_Clear, _T("&Clear"));
4f37a20b
VZ
436 sizerMiddle->Add(btn, 0, wxALL | wxGROW, 5);
437
438 // right pane
4f37a20b
VZ
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
b29903d4 448#if wxUSE_LOG
4f37a20b
VZ
449 wxSizer *sizerDown = new wxStaticBoxSizer
450 (
b4deaacb 451 new wxStaticBox(m_panel, wxID_ANY, _T("&Log window")),
4f37a20b
VZ
452 wxVERTICAL
453 );
b4deaacb 454 m_lboxLog = new wxListBox(m_panel, wxID_ANY);
4f37a20b 455 sizerDown->Add(m_lboxLog, 1, wxGROW | wxALL, 5);
b29903d4
WS
456#else
457 wxSizer *sizerDown = new wxBoxSizer(wxVERTICAL);
458#endif // wxUSE_LOG
4f37a20b 459 wxBoxSizer *sizerBtns = new wxBoxSizer(wxHORIZONTAL);
b29903d4 460#if wxUSE_LOG
b4deaacb 461 btn = new wxButton(m_panel, LboxTest_ClearLog, _T("Clear &log"));
4f37a20b
VZ
462 sizerBtns->Add(btn);
463 sizerBtns->Add(10, 0); // spacer
b29903d4 464#endif // wxUSE_LOG
b4deaacb 465 btn = new wxButton(m_panel, LboxTest_Quit, _T("E&xit"));
4f37a20b
VZ
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
b4deaacb 474 // final initialization and create the listbox
4f37a20b 475 Reset();
b4deaacb 476 CreateLbox();
4f37a20b 477
b4deaacb 478 m_panel->SetSizer(sizerTop);
4f37a20b
VZ
479
480 sizerTop->Fit(this);
481 sizerTop->SetSizeHints(this);
482
b29903d4 483#if wxUSE_LOG
4f37a20b
VZ
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);
b29903d4 488#endif // wxUSE_LOG
4f37a20b
VZ
489}
490
491LboxTestFrame::~LboxTestFrame()
492{
b29903d4 493#if wxUSE_LOG
4f37a20b 494 delete m_logTarget;
b29903d4 495#endif // wxUSE_LOG
4f37a20b
VZ
496}
497
498// ----------------------------------------------------------------------------
499// operations
500// ----------------------------------------------------------------------------
501
502void LboxTestFrame::Reset()
503{
504 if ( m_radioSelMode->GetSelection() == LboxSel_Single &&
505 !m_chkSort->GetValue() &&
506 m_chkHScroll->GetValue() &&
507 !m_chkVScroll->GetValue() )
508 {
509 // nothing to do
510 return;
511 }
512
513 m_radioSelMode->SetSelection(LboxSel_Single);
11180f77
WS
514 m_chkSort->SetValue(false);
515 m_chkHScroll->SetValue(true);
516 m_chkVScroll->SetValue(false);
4f37a20b 517
11180f77 518 m_dirty = true;
4f37a20b
VZ
519}
520
521void LboxTestFrame::CreateLbox()
522{
523 int flags = 0;
524 switch ( m_radioSelMode->GetSelection() )
525 {
526 default:
527 wxFAIL_MSG( _T("unexpected radio box selection") );
528
529 case LboxSel_Single: flags |= wxLB_SINGLE; break;
530 case LboxSel_Extended: flags |= wxLB_EXTENDED; break;
531 case LboxSel_Multiple: flags |= wxLB_MULTIPLE; break;
532 }
533
534 if ( m_chkVScroll->GetValue() )
535 flags |= wxLB_ALWAYS_SB;
536 if ( m_chkHScroll->GetValue() )
537 flags |= wxLB_HSCROLL;
538 if ( m_chkSort->GetValue() )
539 flags |= wxLB_SORT;
540
541 wxArrayString items;
b4deaacb
VZ
542
543 if ( m_lbox ) // cache old items to restore later if listbox existed
544 {
023e504b
JS
545 int count = m_lbox->GetCount();
546 for ( int n = 0; n < count; n++ )
4f37a20b 547 {
023e504b 548 items.Add(m_lbox->GetString(n));
4f37a20b
VZ
549 }
550
023e504b
JS
551 m_sizerLbox->Detach(m_lbox);
552 delete m_lbox;
b4deaacb 553 }
023e504b 554
b4deaacb 555 m_lbox = new wxListBox(m_panel, LboxTest_Listbox,
4f37a20b
VZ
556 wxDefaultPosition, wxDefaultSize,
557 0, NULL,
558 flags);
b4deaacb 559
4f37a20b
VZ
560 m_lbox->Set(items);
561 m_sizerLbox->Add(m_lbox, 1, wxGROW | wxALL, 5);
562 m_sizerLbox->Layout();
563
11180f77 564 m_dirty = false;
b4deaacb
VZ
565
566 m_lbox->Connect(wxEVT_RIGHT_DOWN,
567 wxMouseEventHandler(LboxTestFrame::OnListboxRDown), NULL, this);
4f37a20b
VZ
568}
569
570// ----------------------------------------------------------------------------
571// event handlers
572// ----------------------------------------------------------------------------
573
574void LboxTestFrame::OnButtonQuit(wxCommandEvent& WXUNUSED(event))
575{
576 Close();
577}
578
579void LboxTestFrame::OnButtonReset(wxCommandEvent& WXUNUSED(event))
580{
581 Reset();
582}
583
584void LboxTestFrame::OnButtonCreate(wxCommandEvent& WXUNUSED(event))
585{
586 CreateLbox();
587}
588
589void LboxTestFrame::OnButtonChange(wxCommandEvent& WXUNUSED(event))
590{
591 wxArrayInt selections;
592 int count = m_lbox->GetSelections(selections);
593 wxString s = m_textChange->GetValue();
594 for ( int n = 0; n < count; n++ )
595 {
596 m_lbox->SetString(selections[n], s);
597 }
598}
599
600void LboxTestFrame::OnButtonDelete(wxCommandEvent& WXUNUSED(event))
601{
602 unsigned long n;
603 if ( !m_textDelete->GetValue().ToULong(&n) ||
604 (n >= (unsigned)m_lbox->GetCount()) )
605 {
606 return;
607 }
608
609 m_lbox->Delete(n);
610}
611
612void LboxTestFrame::OnButtonDeleteSel(wxCommandEvent& WXUNUSED(event))
613{
614 wxArrayInt selections;
615 int n = m_lbox->GetSelections(selections);
616 while ( n > 0 )
617 {
618 m_lbox->Delete(selections[--n]);
619 }
620}
621
87728739 622void LboxTestFrame::OnButtonClear(wxCommandEvent& WXUNUSED(event))
4f37a20b
VZ
623{
624 m_lbox->Clear();
625}
626
b29903d4 627#if wxUSE_LOG
87728739 628void LboxTestFrame::OnButtonClearLog(wxCommandEvent& WXUNUSED(event))
4f37a20b
VZ
629{
630 m_lboxLog->Clear();
631}
b29903d4 632#endif // wxUSE_LOG
4f37a20b 633
87728739 634void LboxTestFrame::OnButtonAdd(wxCommandEvent& WXUNUSED(event))
4f37a20b
VZ
635{
636 static size_t s_item = 0;
637
638 wxString s = m_textAdd->GetValue();
639 if ( !m_textAdd->IsModified() )
640 {
641 // update the default string
642 m_textAdd->SetValue(wxString::Format(_T("test item %u"), ++s_item));
643 }
644
645 m_lbox->Append(s);
646}
647
648void LboxTestFrame::OnButtonAddMany(wxCommandEvent& WXUNUSED(event))
649{
650 // "many" means 1000 here
651 for ( size_t n = 0; n < 1000; n++ )
652 {
653 m_lbox->Append(wxString::Format(_T("item #%u"), n));
654 }
655}
656
87728739 657void LboxTestFrame::OnButtonAddSeveral(wxCommandEvent& WXUNUSED(event))
4f37a20b
VZ
658{
659 wxArrayString items;
660 items.Add(_T("First"));
661 items.Add(_T("another one"));
662 items.Add(_T("and the last (very very very very very very very very very very long) one"));
663 m_lbox->InsertItems(items, 0);
664}
665
666void LboxTestFrame::OnUpdateUICreateButton(wxUpdateUIEvent& event)
667{
668 event.Enable(m_dirty);
669}
670
671void LboxTestFrame::OnUpdateUIDeleteButton(wxUpdateUIEvent& event)
672{
673 unsigned long n;
674 event.Enable(m_textDelete->GetValue().ToULong(&n) &&
675 (n < (unsigned)m_lbox->GetCount()));
676}
677
678void LboxTestFrame::OnUpdateUIDeleteSelButton(wxUpdateUIEvent& event)
679{
680 wxArrayInt selections;
681 event.Enable(m_lbox->GetSelections(selections) != 0);
682}
683
684void LboxTestFrame::OnUpdateUIClearButton(wxUpdateUIEvent& event)
685{
686 event.Enable(m_lbox->GetCount() != 0);
687}
688
689void LboxTestFrame::OnUpdateUIAddSeveral(wxUpdateUIEvent& event)
690{
691 event.Enable(!(m_lbox->GetWindowStyle() & wxLB_SORT));
692}
693
694void LboxTestFrame::OnListbox(wxCommandEvent& event)
695{
696 int sel = event.GetInt();
dacaa6f1 697 m_textDelete->SetValue(wxString::Format(_T("%d"), sel));
4f37a20b
VZ
698
699 wxLogMessage(_T("Listbox item %d selected"), sel);
700}
701
702void LboxTestFrame::OnListboxDClick(wxCommandEvent& event)
703{
dacaa6f1
JS
704 int sel = event.GetInt();
705 wxLogMessage(_T("Listbox item %d double clicked"), sel);
4f37a20b
VZ
706}
707
c00fed0e
VZ
708void LboxTestFrame::OnListboxRDown(wxMouseEvent& event)
709{
710 int item = m_lbox->HitTest(event.GetPosition());
711
712 if ( item != wxNOT_FOUND )
713 wxLogMessage(_T("Listbox item %d right clicked"), item);
714 else
715 wxLogMessage(_T("Listbox right clicked but no item clicked upon"));
716
717 event.Skip();
718}
719
87728739 720void LboxTestFrame::OnCheckOrRadioBox(wxCommandEvent& WXUNUSED(event))
4f37a20b 721{
11180f77 722 m_dirty = true;
4f37a20b
VZ
723}
724