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