]> git.saurik.com Git - wxWidgets.git/blame_incremental - samples/listctrl/listtest.cpp
fixed MSW linking (check for wxUSE_GENERIC_DIALOGS_IN_MSW)
[wxWidgets.git] / samples / listctrl / listtest.cpp
... / ...
CommitLineData
1/////////////////////////////////////////////////////////////////////////////
2// Name: listctrl.cpp
3// Purpose: wxListCtrl sample
4// Author: Julian Smart
5// Modified by:
6// Created: 04/01/98
7// RCS-ID: $Id$
8// Copyright: (c) Julian Smart
9// Licence: wxWindows license
10/////////////////////////////////////////////////////////////////////////////
11
12#ifdef __GNUG__
13#pragma implementation
14#pragma interface
15#endif
16
17// For compilers that support precompilation, includes "wx/wx.h".
18#include "wx/wxprec.h"
19
20#ifdef __BORLANDC__
21#pragma hdrstop
22#endif
23
24#ifndef WX_PRECOMP
25#include "wx/wx.h"
26#endif
27
28#ifndef __WXMSW__
29 #include "mondrian.xpm"
30
31 #include "bitmaps/toolbrai.xpm"
32 #include "bitmaps/toolchar.xpm"
33 #include "bitmaps/tooldata.xpm"
34 #include "bitmaps/toolnote.xpm"
35 #include "bitmaps/tooltodo.xpm"
36 #include "bitmaps/toolchec.xpm"
37 #include "bitmaps/toolgame.xpm"
38 #include "bitmaps/tooltime.xpm"
39 #include "bitmaps/toolword.xpm"
40 #include "bitmaps/small1.xpm"
41#endif
42
43#include "wx/imaglist.h"
44#include "wx/listctrl.h"
45#include "wx/timer.h" // for wxStopWatch
46#include "wx/colordlg.h" // for wxGetColourFromUser
47
48#include "listtest.h"
49
50BEGIN_EVENT_TABLE(MyFrame, wxFrame)
51 EVT_SIZE(MyFrame::OnSize)
52
53 EVT_MENU(LIST_QUIT, MyFrame::OnQuit)
54 EVT_MENU(LIST_ABOUT, MyFrame::OnAbout)
55 EVT_MENU(LIST_LIST_VIEW, MyFrame::OnListView)
56 EVT_MENU(LIST_REPORT_VIEW, MyFrame::OnReportView)
57 EVT_MENU(LIST_ICON_VIEW, MyFrame::OnIconView)
58 EVT_MENU(LIST_ICON_TEXT_VIEW, MyFrame::OnIconTextView)
59 EVT_MENU(LIST_SMALL_ICON_VIEW, MyFrame::OnSmallIconView)
60 EVT_MENU(LIST_SMALL_ICON_TEXT_VIEW, MyFrame::OnSmallIconTextView)
61 EVT_MENU(LIST_VIRTUAL_VIEW, MyFrame::OnVirtualView)
62
63 EVT_MENU(LIST_FOCUS_LAST, MyFrame::OnFocusLast)
64 EVT_MENU(LIST_TOGGLE_FIRST, MyFrame::OnToggleFirstSel)
65 EVT_MENU(LIST_DESELECT_ALL, MyFrame::OnDeselectAll)
66 EVT_MENU(LIST_SELECT_ALL, MyFrame::OnSelectAll)
67 EVT_MENU(LIST_DELETE, MyFrame::OnDelete)
68 EVT_MENU(LIST_ADD, MyFrame::OnAdd)
69 EVT_MENU(LIST_EDIT, MyFrame::OnEdit)
70 EVT_MENU(LIST_DELETE_ALL, MyFrame::OnDeleteAll)
71 EVT_MENU(LIST_SORT, MyFrame::OnSort)
72 EVT_MENU(LIST_SET_FG_COL, MyFrame::OnSetFgColour)
73 EVT_MENU(LIST_SET_BG_COL, MyFrame::OnSetBgColour)
74 EVT_MENU(LIST_TOGGLE_MULTI_SEL, MyFrame::OnToggleMultiSel)
75 EVT_MENU(LIST_SHOW_COL_INFO, MyFrame::OnShowColInfo)
76 EVT_MENU(LIST_SHOW_SEL_INFO, MyFrame::OnShowSelInfo)
77 EVT_MENU(LIST_FREEZE, MyFrame::OnFreeze)
78 EVT_MENU(LIST_THAW, MyFrame::OnThaw)
79
80 EVT_UPDATE_UI(LIST_SHOW_COL_INFO, MyFrame::OnUpdateShowColInfo)
81 EVT_UPDATE_UI(LIST_TOGGLE_MULTI_SEL, MyFrame::OnUpdateToggleMultiSel)
82END_EVENT_TABLE()
83
84BEGIN_EVENT_TABLE(MyListCtrl, wxListCtrl)
85 EVT_LIST_BEGIN_DRAG(LIST_CTRL, MyListCtrl::OnBeginDrag)
86 EVT_LIST_BEGIN_RDRAG(LIST_CTRL, MyListCtrl::OnBeginRDrag)
87 EVT_LIST_BEGIN_LABEL_EDIT(LIST_CTRL, MyListCtrl::OnBeginLabelEdit)
88 EVT_LIST_END_LABEL_EDIT(LIST_CTRL, MyListCtrl::OnEndLabelEdit)
89 EVT_LIST_DELETE_ITEM(LIST_CTRL, MyListCtrl::OnDeleteItem)
90 EVT_LIST_DELETE_ALL_ITEMS(LIST_CTRL, MyListCtrl::OnDeleteAllItems)
91 EVT_LIST_GET_INFO(LIST_CTRL, MyListCtrl::OnGetInfo)
92 EVT_LIST_SET_INFO(LIST_CTRL, MyListCtrl::OnSetInfo)
93 EVT_LIST_ITEM_SELECTED(LIST_CTRL, MyListCtrl::OnSelected)
94 EVT_LIST_ITEM_DESELECTED(LIST_CTRL, MyListCtrl::OnDeselected)
95 EVT_LIST_KEY_DOWN(LIST_CTRL, MyListCtrl::OnListKeyDown)
96 EVT_LIST_ITEM_ACTIVATED(LIST_CTRL, MyListCtrl::OnActivated)
97 EVT_LIST_ITEM_FOCUSED(LIST_CTRL, MyListCtrl::OnFocused)
98
99 EVT_LIST_COL_CLICK(LIST_CTRL, MyListCtrl::OnColClick)
100 EVT_LIST_COL_RIGHT_CLICK(LIST_CTRL, MyListCtrl::OnColRightClick)
101 EVT_LIST_COL_BEGIN_DRAG(LIST_CTRL, MyListCtrl::OnColBeginDrag)
102 EVT_LIST_COL_DRAGGING(LIST_CTRL, MyListCtrl::OnColDragging)
103 EVT_LIST_COL_END_DRAG(LIST_CTRL, MyListCtrl::OnColEndDrag)
104
105 EVT_LIST_CACHE_HINT(LIST_CTRL, MyListCtrl::OnCacheHint)
106
107 EVT_CHAR(MyListCtrl::OnChar)
108END_EVENT_TABLE()
109
110IMPLEMENT_APP(MyApp)
111
112// number of items in list/report view
113static const int NUM_ITEMS = 30;
114
115// number of items in icon/small icon view
116static const int NUM_ICONS = 9;
117
118int wxCALLBACK MyCompareFunction(long item1, long item2, long WXUNUSED(sortData))
119{
120 // inverse the order
121 if (item1 < item2)
122 return -1;
123 if (item1 > item2)
124 return 1;
125
126 return 0;
127}
128
129// `Main program' equivalent, creating windows and returning main app frame
130bool MyApp::OnInit()
131{
132 // Create the main frame window
133 MyFrame *frame = new MyFrame(wxT("wxListCtrl Test"), 50, 50, 450, 340);
134
135 // Show the frame
136 frame->Show(true);
137
138 SetTopWindow(frame);
139
140 return true;
141}
142
143// My frame constructor
144MyFrame::MyFrame(const wxChar *title, int x, int y, int w, int h)
145 : wxFrame((wxFrame *)NULL, wxID_ANY, title, wxPoint(x, y), wxSize(w, h))
146{
147 m_listCtrl = (MyListCtrl *) NULL;
148 m_logWindow = (wxTextCtrl *) NULL;
149
150 // Give it an icon
151 SetIcon( wxICON(mondrian) );
152
153 // Make an image list containing large icons
154 m_imageListNormal = new wxImageList(32, 32, true);
155 m_imageListSmall = new wxImageList(16, 16, true);
156
157#ifdef __WXMSW__
158 m_imageListNormal->Add( wxIcon(_T("icon1"), wxBITMAP_TYPE_ICO_RESOURCE) );
159 m_imageListNormal->Add( wxIcon(_T("icon2"), wxBITMAP_TYPE_ICO_RESOURCE) );
160 m_imageListNormal->Add( wxIcon(_T("icon3"), wxBITMAP_TYPE_ICO_RESOURCE) );
161 m_imageListNormal->Add( wxIcon(_T("icon4"), wxBITMAP_TYPE_ICO_RESOURCE) );
162 m_imageListNormal->Add( wxIcon(_T("icon5"), wxBITMAP_TYPE_ICO_RESOURCE) );
163 m_imageListNormal->Add( wxIcon(_T("icon6"), wxBITMAP_TYPE_ICO_RESOURCE) );
164 m_imageListNormal->Add( wxIcon(_T("icon7"), wxBITMAP_TYPE_ICO_RESOURCE) );
165 m_imageListNormal->Add( wxIcon(_T("icon8"), wxBITMAP_TYPE_ICO_RESOURCE) );
166 m_imageListNormal->Add( wxIcon(_T("icon9"), wxBITMAP_TYPE_ICO_RESOURCE) );
167
168 m_imageListSmall->Add( wxIcon(_T("iconsmall"), wxBITMAP_TYPE_ICO_RESOURCE) );
169
170#else
171 m_imageListNormal->Add( wxIcon( toolbrai_xpm ) );
172 m_imageListNormal->Add( wxIcon( toolchar_xpm ) );
173 m_imageListNormal->Add( wxIcon( tooldata_xpm ) );
174 m_imageListNormal->Add( wxIcon( toolnote_xpm ) );
175 m_imageListNormal->Add( wxIcon( tooltodo_xpm ) );
176 m_imageListNormal->Add( wxIcon( toolchec_xpm ) );
177 m_imageListNormal->Add( wxIcon( toolgame_xpm ) );
178 m_imageListNormal->Add( wxIcon( tooltime_xpm ) );
179 m_imageListNormal->Add( wxIcon( toolword_xpm ) );
180
181 m_imageListSmall->Add( wxIcon( small1_xpm) );
182#endif
183
184 // Make a menubar
185 wxMenu *menuFile = new wxMenu;
186 menuFile->Append(LIST_ABOUT, _T("&About"));
187 menuFile->AppendSeparator();
188 menuFile->Append(LIST_QUIT, _T("E&xit\tAlt-X"));
189
190 wxMenu *menuView = new wxMenu;
191 menuView->Append(LIST_LIST_VIEW, _T("&List view\tF1"));
192 menuView->Append(LIST_REPORT_VIEW, _T("&Report view\tF2"));
193 menuView->Append(LIST_ICON_VIEW, _T("&Icon view\tF3"));
194 menuView->Append(LIST_ICON_TEXT_VIEW, _T("Icon view with &text\tF4"));
195 menuView->Append(LIST_SMALL_ICON_VIEW, _T("&Small icon view\tF5"));
196 menuView->Append(LIST_SMALL_ICON_TEXT_VIEW, _T("Small icon &view with text\tF6"));
197 menuView->Append(LIST_VIRTUAL_VIEW, _T("Virtual view\tF7"));
198
199 wxMenu *menuList = new wxMenu;
200 menuList->Append(LIST_FOCUS_LAST, _T("&Make last item current\tCtrl-L"));
201 menuList->Append(LIST_TOGGLE_FIRST, _T("To&ggle first item\tCtrl-G"));
202 menuList->Append(LIST_DESELECT_ALL, _T("&Deselect All\tCtrl-D"));
203 menuList->Append(LIST_SELECT_ALL, _T("S&elect All\tCtrl-A"));
204 menuList->AppendSeparator();
205 menuList->Append(LIST_SHOW_COL_INFO, _T("Show &column info\tCtrl-C"));
206 menuList->Append(LIST_SHOW_SEL_INFO, _T("Show &selected items\tCtrl-S"));
207 menuList->AppendSeparator();
208 menuList->Append(LIST_SORT, _T("&Sort\tCtrl-S"));
209 menuList->AppendSeparator();
210 menuList->Append(LIST_ADD, _T("&Append an item\tCtrl-P"));
211 menuList->Append(LIST_EDIT, _T("&Edit the item\tCtrl-E"));
212 menuList->Append(LIST_DELETE, _T("&Delete first item\tCtrl-X"));
213 menuList->Append(LIST_DELETE_ALL, _T("Delete &all items"));
214 menuList->AppendSeparator();
215 menuList->Append(LIST_FREEZE, _T("Free&ze\tCtrl-Z"));
216 menuList->Append(LIST_THAW, _T("Tha&w\tCtrl-W"));
217 menuList->AppendSeparator();
218 menuList->Append(LIST_TOGGLE_MULTI_SEL, _T("&Multiple selection\tCtrl-M"),
219 _T("Toggle multiple selection"), true);
220
221 wxMenu *menuCol = new wxMenu;
222 menuCol->Append(LIST_SET_FG_COL, _T("&Foreground colour..."));
223 menuCol->Append(LIST_SET_BG_COL, _T("&Background colour..."));
224
225 wxMenuBar *menubar = new wxMenuBar;
226 menubar->Append(menuFile, _T("&File"));
227 menubar->Append(menuView, _T("&View"));
228 menubar->Append(menuList, _T("&List"));
229 menubar->Append(menuCol, _T("&Colour"));
230 SetMenuBar(menubar);
231
232 m_panel = new wxPanel(this, wxID_ANY);
233 m_logWindow = new wxTextCtrl(m_panel, wxID_ANY, wxEmptyString,
234 wxDefaultPosition, wxDefaultSize,
235 wxTE_MULTILINE | wxSUNKEN_BORDER);
236
237 m_logOld = wxLog::SetActiveTarget(new wxLogTextCtrl(m_logWindow));
238
239 RecreateList(wxLC_REPORT | wxLC_SINGLE_SEL);
240
241#if wxUSE_STATUSBAR
242 CreateStatusBar(3);
243#endif // wxUSE_STATUSBAR
244}
245
246MyFrame::~MyFrame()
247{
248 delete wxLog::SetActiveTarget(m_logOld);
249
250 delete m_imageListNormal;
251 delete m_imageListSmall;
252}
253
254void MyFrame::OnSize(wxSizeEvent& event)
255{
256 DoSize();
257
258 event.Skip();
259}
260
261void MyFrame::DoSize()
262{
263 if ( !m_logWindow )
264 return;
265
266 wxSize size = GetClientSize();
267 wxCoord y = (2*size.y)/3;
268 m_listCtrl->SetSize(0, 0, size.x, y);
269 m_logWindow->SetSize(0, y + 1, size.x, size.y - y);
270}
271
272bool MyFrame::CheckNonVirtual() const
273{
274 if ( !m_listCtrl->HasFlag(wxLC_VIRTUAL) )
275 return true;
276
277 // "this" == whatever
278 wxLogWarning(_T("Can't do this in virtual view, sorry."));
279
280 return false;
281}
282
283void MyFrame::OnQuit(wxCommandEvent& WXUNUSED(event))
284{
285 Close(true);
286}
287
288void MyFrame::OnAbout(wxCommandEvent& WXUNUSED(event))
289{
290 wxMessageDialog dialog(this, _T("List test sample\nJulian Smart (c) 1997"),
291 _T("About list test"), wxOK|wxCANCEL);
292
293 dialog.ShowModal();
294}
295
296void MyFrame::OnFreeze(wxCommandEvent& WXUNUSED(event))
297{
298 wxLogMessage(_T("Freezing the control"));
299
300 m_listCtrl->Freeze();
301}
302
303void MyFrame::OnThaw(wxCommandEvent& WXUNUSED(event))
304{
305 wxLogMessage(_T("Thawing the control"));
306
307 m_listCtrl->Thaw();
308}
309
310void MyFrame::OnFocusLast(wxCommandEvent& WXUNUSED(event))
311{
312 long index = m_listCtrl->GetItemCount() - 1;
313 if ( index == -1 )
314 {
315 return;
316 }
317
318 m_listCtrl->SetItemState(index, wxLIST_STATE_FOCUSED, wxLIST_STATE_FOCUSED);
319 m_listCtrl->EnsureVisible(index);
320}
321
322void MyFrame::OnToggleFirstSel(wxCommandEvent& WXUNUSED(event))
323{
324 m_listCtrl->SetItemState(0, (~m_listCtrl->GetItemState(0, wxLIST_STATE_SELECTED) ) & wxLIST_STATE_SELECTED, wxLIST_STATE_SELECTED);
325}
326
327void MyFrame::OnDeselectAll(wxCommandEvent& WXUNUSED(event))
328{
329 if ( !CheckNonVirtual() )
330 return;
331
332 int n = m_listCtrl->GetItemCount();
333 for (int i = 0; i < n; i++)
334 m_listCtrl->SetItemState(i,0,wxLIST_STATE_SELECTED);
335}
336
337void MyFrame::OnSelectAll(wxCommandEvent& WXUNUSED(event))
338{
339 if ( !CheckNonVirtual() )
340 return;
341
342 int n = m_listCtrl->GetItemCount();
343 for (int i = 0; i < n; i++)
344 m_listCtrl->SetItemState(i,wxLIST_STATE_SELECTED, wxLIST_STATE_SELECTED);
345}
346
347// ----------------------------------------------------------------------------
348// changing listctrl modes
349// ----------------------------------------------------------------------------
350
351void MyFrame::RecreateList(long flags, bool withText)
352{
353 // we could avoid recreating it if we don't set/clear the wxLC_VIRTUAL
354 // style, but it is more trouble to do it than not
355#if 0
356 if ( !m_listCtrl || ((flags & wxLC_VIRTUAL) !=
357 (m_listCtrl->GetWindowStyleFlag() & wxLC_VIRTUAL)) )
358#endif
359 {
360 delete m_listCtrl;
361
362 m_listCtrl = new MyListCtrl(m_panel, LIST_CTRL,
363 wxDefaultPosition, wxDefaultSize,
364 flags |
365 wxSUNKEN_BORDER | wxLC_EDIT_LABELS);
366
367 switch ( flags & wxLC_MASK_TYPE )
368 {
369 case wxLC_LIST:
370 InitWithListItems();
371 break;
372
373 case wxLC_ICON:
374 InitWithIconItems(withText);
375 break;
376
377 case wxLC_SMALL_ICON:
378 InitWithIconItems(withText, true);
379 break;
380
381 case wxLC_REPORT:
382 if ( flags & wxLC_VIRTUAL )
383 InitWithVirtualItems();
384 else
385 InitWithReportItems();
386 break;
387
388 default:
389 wxFAIL_MSG( _T("unknown listctrl mode") );
390 }
391 }
392
393 DoSize();
394
395 m_logWindow->Clear();
396}
397
398void MyFrame::OnListView(wxCommandEvent& WXUNUSED(event))
399{
400 RecreateList(wxLC_LIST);
401}
402
403void MyFrame::InitWithListItems()
404{
405 for ( int i = 0; i < NUM_ITEMS; i++ )
406 {
407 m_listCtrl->InsertItem(i, wxString::Format(_T("Item %d"), i));
408 }
409}
410
411void MyFrame::OnReportView(wxCommandEvent& WXUNUSED(event))
412{
413 RecreateList(wxLC_REPORT);
414}
415
416void MyFrame::InitWithReportItems()
417{
418 m_listCtrl->SetImageList(m_imageListSmall, wxIMAGE_LIST_SMALL);
419
420 // note that under MSW for SetColumnWidth() to work we need to create the
421 // items with images initially even if we specify dummy image id
422 wxListItem itemCol;
423 itemCol.SetText(_T("Column 1"));
424 itemCol.SetImage(-1);
425 m_listCtrl->InsertColumn(0, itemCol);
426
427 itemCol.SetText(_T("Column 2"));
428 itemCol.SetAlign(wxLIST_FORMAT_CENTRE);
429 m_listCtrl->InsertColumn(1, itemCol);
430
431 itemCol.SetText(_T("Column 3"));
432 itemCol.SetAlign(wxLIST_FORMAT_RIGHT);
433 m_listCtrl->InsertColumn(2, itemCol);
434
435 // to speed up inserting we hide the control temporarily
436 m_listCtrl->Hide();
437
438 wxStopWatch sw;
439
440 for ( int i = 0; i < NUM_ITEMS; i++ )
441 {
442 m_listCtrl->InsertItemInReportView(i);
443 }
444
445 m_logWindow->WriteText(wxString::Format(_T("%d items inserted in %ldms\n"),
446 NUM_ITEMS, sw.Time()));
447 m_listCtrl->Show();
448
449 // we leave all mask fields to 0 and only change the colour
450 wxListItem item;
451 item.m_itemId = 0;
452 item.SetTextColour(*wxRED);
453 m_listCtrl->SetItem( item );
454
455 item.m_itemId = 2;
456 item.SetTextColour(*wxGREEN);
457 m_listCtrl->SetItem( item );
458 item.m_itemId = 4;
459 item.SetTextColour(*wxLIGHT_GREY);
460 item.SetFont(*wxITALIC_FONT);
461 item.SetBackgroundColour(*wxRED);
462 m_listCtrl->SetItem( item );
463
464 m_listCtrl->SetTextColour(*wxBLUE);
465 m_listCtrl->SetBackgroundColour(*wxLIGHT_GREY);
466
467 m_listCtrl->SetColumnWidth( 0, wxLIST_AUTOSIZE );
468 m_listCtrl->SetColumnWidth( 1, wxLIST_AUTOSIZE );
469 m_listCtrl->SetColumnWidth( 2, wxLIST_AUTOSIZE );
470}
471
472void MyFrame::InitWithIconItems(bool withText, bool sameIcon)
473{
474 m_listCtrl->SetImageList(m_imageListNormal, wxIMAGE_LIST_NORMAL);
475 m_listCtrl->SetImageList(m_imageListSmall, wxIMAGE_LIST_SMALL);
476
477 for ( int i = 0; i < NUM_ICONS; i++ )
478 {
479 int image = sameIcon ? 0 : i;
480
481 if ( withText )
482 {
483 m_listCtrl->InsertItem(i, wxString::Format(_T("Label %d"), i),
484 image);
485 }
486 else
487 {
488 m_listCtrl->InsertItem(i, image);
489 }
490 }
491}
492
493void MyFrame::OnIconView(wxCommandEvent& WXUNUSED(event))
494{
495 RecreateList(wxLC_ICON, false);
496}
497
498void MyFrame::OnIconTextView(wxCommandEvent& WXUNUSED(event))
499{
500 RecreateList(wxLC_ICON);
501}
502
503void MyFrame::OnSmallIconView(wxCommandEvent& WXUNUSED(event))
504{
505 RecreateList(wxLC_SMALL_ICON, false);
506}
507
508void MyFrame::OnSmallIconTextView(wxCommandEvent& WXUNUSED(event))
509{
510 RecreateList(wxLC_SMALL_ICON);
511}
512
513void MyFrame::OnVirtualView(wxCommandEvent& WXUNUSED(event))
514{
515 RecreateList(wxLC_REPORT | wxLC_VIRTUAL);
516}
517
518void MyFrame::InitWithVirtualItems()
519{
520 m_listCtrl->SetImageList(m_imageListSmall, wxIMAGE_LIST_SMALL);
521
522 m_listCtrl->InsertColumn(0, _T("First Column"));
523 m_listCtrl->InsertColumn(1, _T("Second Column"));
524 m_listCtrl->SetColumnWidth(0, 150);
525 m_listCtrl->SetColumnWidth(1, 150);
526
527 m_listCtrl->SetItemCount(1000000);
528}
529
530void MyFrame::OnSort(wxCommandEvent& WXUNUSED(event))
531{
532 wxStopWatch sw;
533
534 m_listCtrl->SortItems(MyCompareFunction, 0);
535
536 m_logWindow->WriteText(wxString::Format(_T("Sorting %d items took %ld ms\n"),
537 m_listCtrl->GetItemCount(),
538 sw.Time()));
539}
540
541void MyFrame::OnShowSelInfo(wxCommandEvent& WXUNUSED(event))
542{
543 int selCount = m_listCtrl->GetSelectedItemCount();
544 wxLogMessage(_T("%d items selected:"), selCount);
545
546 // don't show too many items
547 size_t shownCount = 0;
548
549 long item = m_listCtrl->GetNextItem(-1, wxLIST_NEXT_ALL,
550 wxLIST_STATE_SELECTED);
551 while ( item != -1 )
552 {
553 wxLogMessage(_T("\t%ld (%s)"),
554 item, m_listCtrl->GetItemText(item).c_str());
555
556 if ( ++shownCount > 10 )
557 {
558 wxLogMessage(_T("\t... more selected items snipped..."));
559 break;
560 }
561
562 item = m_listCtrl->GetNextItem(item, wxLIST_NEXT_ALL,
563 wxLIST_STATE_SELECTED);
564 }
565}
566
567void MyFrame::OnShowColInfo(wxCommandEvent& WXUNUSED(event))
568{
569 int count = m_listCtrl->GetColumnCount();
570 wxLogMessage(wxT("%d columns:"), count);
571 for ( int c = 0; c < count; c++ )
572 {
573 wxLogMessage(wxT("\tcolumn %d has width %d"), c,
574 m_listCtrl->GetColumnWidth(c));
575 }
576}
577
578void MyFrame::OnUpdateShowColInfo(wxUpdateUIEvent& event)
579{
580 event.Enable( (m_listCtrl->GetWindowStyleFlag() & wxLC_REPORT) != 0 );
581}
582
583void MyFrame::OnToggleMultiSel(wxCommandEvent& WXUNUSED(event))
584{
585 long flags = m_listCtrl->GetWindowStyleFlag();
586 if ( flags & wxLC_SINGLE_SEL )
587 flags &= ~wxLC_SINGLE_SEL;
588 else
589 flags |= wxLC_SINGLE_SEL;
590
591 m_logWindow->WriteText(wxString::Format(wxT("Current selection mode: %sle\n"),
592 (flags & wxLC_SINGLE_SEL) ? _T("sing") : _T("multip")));
593
594 RecreateList(flags);
595}
596
597void MyFrame::OnUpdateToggleMultiSel(wxUpdateUIEvent& event)
598{
599 event.Check((m_listCtrl->GetWindowStyleFlag() & wxLC_SINGLE_SEL) == 0);
600}
601
602void MyFrame::OnSetFgColour(wxCommandEvent& WXUNUSED(event))
603{
604 m_listCtrl->SetForegroundColour(wxGetColourFromUser(this));
605 m_listCtrl->Refresh();
606}
607
608void MyFrame::OnSetBgColour(wxCommandEvent& WXUNUSED(event))
609{
610 m_listCtrl->SetBackgroundColour(wxGetColourFromUser(this));
611 m_listCtrl->Refresh();
612}
613
614void MyFrame::OnAdd(wxCommandEvent& WXUNUSED(event))
615{
616 m_listCtrl->InsertItem(m_listCtrl->GetItemCount(), _T("Appended item"));
617}
618
619void MyFrame::OnEdit(wxCommandEvent& WXUNUSED(event))
620{
621 long itemCur = m_listCtrl->GetNextItem(-1, wxLIST_NEXT_ALL,
622 wxLIST_STATE_FOCUSED);
623
624 if ( itemCur != -1 )
625 {
626 m_listCtrl->EditLabel(itemCur);
627 }
628 else
629 {
630 m_logWindow->WriteText(_T("No item to edit"));
631 }
632}
633
634void MyFrame::OnDelete(wxCommandEvent& WXUNUSED(event))
635{
636 if ( m_listCtrl->GetItemCount() )
637 {
638 m_listCtrl->DeleteItem(0);
639 }
640 else
641 {
642 m_logWindow->WriteText(_T("Nothing to delete"));
643 }
644}
645
646void MyFrame::OnDeleteAll(wxCommandEvent& WXUNUSED(event))
647{
648 wxStopWatch sw;
649
650 m_listCtrl->DeleteAllItems();
651
652 m_logWindow->WriteText(wxString::Format(_T("Deleting %d items took %ld ms\n"),
653 m_listCtrl->GetItemCount(),
654 sw.Time()));
655}
656
657// MyListCtrl
658
659void MyListCtrl::OnCacheHint(wxListEvent& event)
660{
661 wxLogMessage( wxT("OnCacheHint: cache items %ld..%ld"),
662 event.GetCacheFrom(), event.GetCacheTo() );
663}
664
665void MyListCtrl::SetColumnImage(int col, int image)
666{
667 wxListItem item;
668 item.SetMask(wxLIST_MASK_IMAGE);
669 item.SetImage(image);
670 SetColumn(col, item);
671}
672
673void MyListCtrl::OnColClick(wxListEvent& event)
674{
675 int col = event.GetColumn();
676 SetColumnImage(col, 0);
677
678 wxLogMessage( wxT("OnColumnClick at %d."), col );
679}
680
681void MyListCtrl::OnColRightClick(wxListEvent& event)
682{
683 int col = event.GetColumn();
684 if ( col != -1 )
685 {
686 SetColumnImage(col, -1);
687 }
688
689 // Show popupmenu at position
690 wxMenu menu(wxT("Test"));
691 menu.Append(LIST_ABOUT, _T("&About"));
692 PopupMenu(&menu, event.GetPoint());
693
694 wxLogMessage( wxT("OnColumnRightClick at %d."), event.GetColumn() );
695}
696
697void MyListCtrl::LogColEvent(const wxListEvent& event, const wxChar *name)
698{
699 const int col = event.GetColumn();
700
701 wxLogMessage(wxT("%s: column %d (width = %d or %d)."),
702 name,
703 col,
704 event.GetItem().GetWidth(),
705 GetColumnWidth(col));
706}
707
708void MyListCtrl::OnColBeginDrag(wxListEvent& event)
709{
710 LogColEvent( event, wxT("OnColBeginDrag") );
711
712 if ( event.GetColumn() == 0 )
713 {
714 wxLogMessage(_T("Resizing this column shouldn't work."));
715
716 event.Veto();
717 }
718}
719
720void MyListCtrl::OnColDragging(wxListEvent& event)
721{
722 LogColEvent( event, wxT("OnColDragging") );
723}
724
725void MyListCtrl::OnColEndDrag(wxListEvent& event)
726{
727 LogColEvent( event, wxT("OnColEndDrag") );
728}
729
730void MyListCtrl::OnBeginDrag(wxListEvent& event)
731{
732 const wxPoint& pt = event.m_pointDrag;
733
734 int flags;
735 wxLogMessage( wxT("OnBeginDrag at (%d, %d), item %ld."),
736 pt.x, pt.y, HitTest(pt, flags) );
737}
738
739void MyListCtrl::OnBeginRDrag(wxListEvent& event)
740{
741 wxLogMessage( wxT("OnBeginRDrag at %d,%d."),
742 event.m_pointDrag.x, event.m_pointDrag.y );
743}
744
745void MyListCtrl::OnBeginLabelEdit(wxListEvent& event)
746{
747 wxLogMessage( wxT("OnBeginLabelEdit: %s"), event.m_item.m_text.c_str());
748}
749
750void MyListCtrl::OnEndLabelEdit(wxListEvent& event)
751{
752 wxLogMessage( wxT("OnEndLabelEdit: %s"),
753 event.IsEditCancelled() ? _T("[cancelled]")
754 : event.m_item.m_text.c_str());
755}
756
757void MyListCtrl::OnDeleteItem(wxListEvent& event)
758{
759 LogEvent(event, _T("OnDeleteItem"));
760}
761
762void MyListCtrl::OnDeleteAllItems(wxListEvent& event)
763{
764 LogEvent(event, _T("OnDeleteAllItems"));
765}
766
767void MyListCtrl::OnGetInfo(wxListEvent& event)
768{
769 wxString msg;
770
771 msg << _T("OnGetInfo (") << event.m_item.m_itemId << _T(", ") << event.m_item.m_col << _T(")");
772 if ( event.m_item.m_mask & wxLIST_MASK_STATE )
773 msg << _T(" wxLIST_MASK_STATE");
774 if ( event.m_item.m_mask & wxLIST_MASK_TEXT )
775 msg << _T(" wxLIST_MASK_TEXT");
776 if ( event.m_item.m_mask & wxLIST_MASK_IMAGE )
777 msg << _T(" wxLIST_MASK_IMAGE");
778 if ( event.m_item.m_mask & wxLIST_MASK_DATA )
779 msg << _T(" wxLIST_MASK_DATA");
780 if ( event.m_item.m_mask & wxLIST_SET_ITEM )
781 msg << _T(" wxLIST_SET_ITEM");
782 if ( event.m_item.m_mask & wxLIST_MASK_WIDTH )
783 msg << _T(" wxLIST_MASK_WIDTH");
784 if ( event.m_item.m_mask & wxLIST_MASK_FORMAT )
785 msg << _T(" wxLIST_MASK_WIDTH");
786
787 if ( event.m_item.m_mask & wxLIST_MASK_TEXT )
788 {
789 event.m_item.m_text = _T("My callback text");
790 }
791
792 wxLogMessage(msg);
793}
794
795void MyListCtrl::OnSetInfo(wxListEvent& event)
796{
797 LogEvent(event, _T("OnSetInfo"));
798}
799
800void MyListCtrl::OnSelected(wxListEvent& event)
801{
802 LogEvent(event, _T("OnSelected"));
803
804 if ( GetWindowStyle() & wxLC_REPORT )
805 {
806 wxListItem info;
807 info.m_itemId = event.m_itemIndex;
808 info.m_col = 1;
809 info.m_mask = wxLIST_MASK_TEXT;
810 if ( GetItem(info) )
811 {
812 wxLogMessage(wxT("Value of the 2nd field of the selected item: %s"),
813 info.m_text.c_str());
814 }
815 else
816 {
817 wxFAIL_MSG(wxT("wxListCtrl::GetItem() failed"));
818 }
819 }
820}
821
822void MyListCtrl::OnDeselected(wxListEvent& event)
823{
824 LogEvent(event, _T("OnDeselected"));
825}
826
827void MyListCtrl::OnActivated(wxListEvent& event)
828{
829 LogEvent(event, _T("OnActivated"));
830}
831
832void MyListCtrl::OnFocused(wxListEvent& event)
833{
834 LogEvent(event, _T("OnFocused"));
835
836 event.Skip();
837}
838
839void MyListCtrl::OnListKeyDown(wxListEvent& event)
840{
841 switch ( event.GetKeyCode() )
842 {
843 case 'c': // colorize
844 case 'C':
845 {
846 wxListItem info;
847 info.m_itemId = event.GetIndex();
848 GetItem(info);
849
850 wxListItemAttr *attr = info.GetAttributes();
851 if ( !attr || !attr->HasTextColour() )
852 {
853 info.SetTextColour(*wxCYAN);
854
855 SetItem(info);
856
857 RefreshItem(info.m_itemId);
858 }
859 }
860 break;
861
862 case 'n': // next
863 case 'N':
864 {
865 long item = GetNextItem(-1,
866 wxLIST_NEXT_ALL, wxLIST_STATE_FOCUSED);
867 if ( item++ == GetItemCount() - 1 )
868 {
869 item = 0;
870 }
871
872 wxLogMessage(_T("Focusing item %ld"), item);
873
874 SetItemState(item, wxLIST_STATE_FOCUSED, wxLIST_STATE_FOCUSED);
875 EnsureVisible(item);
876 }
877 break;
878
879 case WXK_DELETE:
880 {
881 long item = GetNextItem(-1,
882 wxLIST_NEXT_ALL, wxLIST_STATE_SELECTED);
883 while ( item != -1 )
884 {
885 DeleteItem(item);
886
887 wxLogMessage(_T("Item %ld deleted"), item);
888
889 // -1 because the indices were shifted by DeleteItem()
890 item = GetNextItem(item - 1,
891 wxLIST_NEXT_ALL, wxLIST_STATE_SELECTED);
892 }
893 }
894 break;
895
896 case WXK_INSERT:
897 if ( GetWindowStyle() & wxLC_REPORT )
898 {
899 if ( GetWindowStyle() & wxLC_VIRTUAL )
900 {
901 SetItemCount(GetItemCount() + 1);
902 }
903 else // !virtual
904 {
905 InsertItemInReportView(event.GetIndex());
906 }
907 }
908 //else: fall through
909
910 default:
911 LogEvent(event, _T("OnListKeyDown"));
912
913 event.Skip();
914 }
915}
916
917void MyListCtrl::OnChar(wxKeyEvent& event)
918{
919 wxLogMessage(_T("Got char event."));
920
921 switch ( event.GetKeyCode() )
922 {
923 case 'n':
924 case 'N':
925 case 'c':
926 case 'C':
927 // these are the keys we process ourselves
928 break;
929
930 default:
931 event.Skip();
932 }
933}
934
935void MyListCtrl::LogEvent(const wxListEvent& event, const wxChar *eventName)
936{
937 wxLogMessage(_T("Item %ld: %s (item text = %s, data = %ld)"),
938 event.GetIndex(), eventName,
939 event.GetText().c_str(), event.GetData());
940}
941
942wxString MyListCtrl::OnGetItemText(long item, long column) const
943{
944 return wxString::Format(_T("Column %ld of item %ld"), column, item);
945}
946
947int MyListCtrl::OnGetItemImage(long WXUNUSED(item)) const
948{
949 return 0;
950}
951
952wxListItemAttr *MyListCtrl::OnGetItemAttr(long item) const
953{
954 return item % 2 ? NULL : (wxListItemAttr *)&m_attr;
955}
956
957void MyListCtrl::InsertItemInReportView(int i)
958{
959 wxString buf;
960 buf.Printf(_T("This is item %d"), i);
961 long tmp = InsertItem(i, buf, 0);
962 SetItemData(tmp, i);
963
964 buf.Printf(_T("Col 1, item %d"), i);
965 SetItem(i, 1, buf);
966
967 buf.Printf(_T("Item %d in column 2"), i);
968 SetItem(i, 2, buf);
969}
970