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