added test for HitTest(m_dragPoint) bug to the sample
[wxWidgets.git] / samples / listctrl / listtest.cpp
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 and Markus Holzem
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
50 BEGIN_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_DELETE_ALL, MyFrame::OnDeleteAll)
70 EVT_MENU(LIST_SORT, MyFrame::OnSort)
71 EVT_MENU(LIST_SET_FG_COL, MyFrame::OnSetFgColour)
72 EVT_MENU(LIST_SET_BG_COL, MyFrame::OnSetBgColour)
73 EVT_MENU(LIST_TOGGLE_MULTI_SEL, MyFrame::OnToggleMultiSel)
74 EVT_MENU(LIST_SHOW_COL_INFO, MyFrame::OnShowColInfo)
75 EVT_MENU(LIST_SHOW_SEL_INFO, MyFrame::OnShowSelInfo)
76
77 EVT_UPDATE_UI(LIST_SHOW_COL_INFO, MyFrame::OnUpdateShowColInfo)
78 END_EVENT_TABLE()
79
80 BEGIN_EVENT_TABLE(MyListCtrl, wxListCtrl)
81 EVT_LIST_BEGIN_DRAG(LIST_CTRL, MyListCtrl::OnBeginDrag)
82 EVT_LIST_BEGIN_RDRAG(LIST_CTRL, MyListCtrl::OnBeginRDrag)
83 EVT_LIST_BEGIN_LABEL_EDIT(LIST_CTRL, MyListCtrl::OnBeginLabelEdit)
84 EVT_LIST_END_LABEL_EDIT(LIST_CTRL, MyListCtrl::OnEndLabelEdit)
85 EVT_LIST_DELETE_ITEM(LIST_CTRL, MyListCtrl::OnDeleteItem)
86 EVT_LIST_DELETE_ALL_ITEMS(LIST_CTRL, MyListCtrl::OnDeleteAllItems)
87 EVT_LIST_GET_INFO(LIST_CTRL, MyListCtrl::OnGetInfo)
88 EVT_LIST_SET_INFO(LIST_CTRL, MyListCtrl::OnSetInfo)
89 EVT_LIST_ITEM_SELECTED(LIST_CTRL, MyListCtrl::OnSelected)
90 EVT_LIST_ITEM_DESELECTED(LIST_CTRL, MyListCtrl::OnDeselected)
91 EVT_LIST_KEY_DOWN(LIST_CTRL, MyListCtrl::OnListKeyDown)
92 EVT_LIST_ITEM_ACTIVATED(LIST_CTRL, MyListCtrl::OnActivated)
93 EVT_LIST_COL_CLICK(LIST_CTRL, MyListCtrl::OnColClick)
94 EVT_LIST_CACHE_HINT(LIST_CTRL, MyListCtrl::OnCacheHint)
95
96 EVT_CHAR(MyListCtrl::OnChar)
97 END_EVENT_TABLE()
98
99 IMPLEMENT_APP(MyApp)
100
101 // number of items in list/report view
102 static const int NUM_ITEMS = 30;
103
104 // number of items in icon/small icon view
105 static const int NUM_ICONS = 9;
106
107 int wxCALLBACK MyCompareFunction(long item1, long item2, long sortData)
108 {
109 // inverse the order
110 return item1 < item2;
111 }
112
113 // `Main program' equivalent, creating windows and returning main app frame
114 bool MyApp::OnInit()
115 {
116 // Create the main frame window
117 MyFrame *frame = new MyFrame(wxT("wxListCtrl Test"), 50, 50, 450, 340);
118
119 // Show the frame
120 frame->Show(TRUE);
121
122 SetTopWindow(frame);
123
124 return TRUE;
125 }
126
127 // My frame constructor
128 MyFrame::MyFrame(const wxChar *title, int x, int y, int w, int h)
129 : wxFrame((wxFrame *)NULL, -1, title, wxPoint(x, y), wxSize(w, h))
130 {
131 m_listCtrl = (MyListCtrl *) NULL;
132 m_logWindow = (wxTextCtrl *) NULL;
133
134 // Give it an icon
135 SetIcon( wxICON(mondrian) );
136
137 // Make an image list containing large icons
138 m_imageListNormal = new wxImageList(32, 32, TRUE);
139 m_imageListSmall = new wxImageList(16, 16, TRUE);
140
141 #ifdef __WXMSW__
142 m_imageListNormal->Add( wxIcon("icon1", wxBITMAP_TYPE_ICO_RESOURCE) );
143 m_imageListNormal->Add( wxIcon("icon2", wxBITMAP_TYPE_ICO_RESOURCE) );
144 m_imageListNormal->Add( wxIcon("icon3", wxBITMAP_TYPE_ICO_RESOURCE) );
145 m_imageListNormal->Add( wxIcon("icon4", wxBITMAP_TYPE_ICO_RESOURCE) );
146 m_imageListNormal->Add( wxIcon("icon5", wxBITMAP_TYPE_ICO_RESOURCE) );
147 m_imageListNormal->Add( wxIcon("icon6", wxBITMAP_TYPE_ICO_RESOURCE) );
148 m_imageListNormal->Add( wxIcon("icon7", wxBITMAP_TYPE_ICO_RESOURCE) );
149 m_imageListNormal->Add( wxIcon("icon8", wxBITMAP_TYPE_ICO_RESOURCE) );
150 m_imageListNormal->Add( wxIcon("icon9", wxBITMAP_TYPE_ICO_RESOURCE) );
151
152 m_imageListSmall->Add( wxIcon("iconsmall", wxBITMAP_TYPE_ICO_RESOURCE) );
153
154 #else
155 m_imageListNormal->Add( wxIcon( toolbrai_xpm ) );
156 m_imageListNormal->Add( wxIcon( toolchar_xpm ) );
157 m_imageListNormal->Add( wxIcon( tooldata_xpm ) );
158 m_imageListNormal->Add( wxIcon( toolnote_xpm ) );
159 m_imageListNormal->Add( wxIcon( tooltodo_xpm ) );
160 m_imageListNormal->Add( wxIcon( toolchec_xpm ) );
161 m_imageListNormal->Add( wxIcon( toolgame_xpm ) );
162 m_imageListNormal->Add( wxIcon( tooltime_xpm ) );
163 m_imageListNormal->Add( wxIcon( toolword_xpm ) );
164
165 m_imageListSmall->Add( wxIcon( small1_xpm) );
166 #endif
167
168 // Make a menubar
169 wxMenu *menuFile = new wxMenu;
170 menuFile->Append(LIST_ABOUT, _T("&About"));
171 menuFile->AppendSeparator();
172 menuFile->Append(LIST_QUIT, _T("E&xit\tAlt-X"));
173
174 wxMenu *menuView = new wxMenu;
175 menuView->Append(LIST_LIST_VIEW, _T("&List view\tF1"));
176 menuView->Append(LIST_REPORT_VIEW, _T("&Report view\tF2"));
177 menuView->Append(LIST_ICON_VIEW, _T("&Icon view\tF3"));
178 menuView->Append(LIST_ICON_TEXT_VIEW, _T("Icon view with &text\tF4"));
179 menuView->Append(LIST_SMALL_ICON_VIEW, _T("&Small icon view\tF5"));
180 menuView->Append(LIST_SMALL_ICON_TEXT_VIEW, _T("Small icon &view with text\tF6"));
181 menuView->Append(LIST_VIRTUAL_VIEW, _T("Virtual view\tF7"));
182
183 wxMenu *menuList = new wxMenu;
184 menuList->Append(LIST_FOCUS_LAST, _T("&Make last item current\tCtrl-L"));
185 menuList->Append(LIST_TOGGLE_FIRST, _T("&Toggle first item\tCtrl-T"));
186 menuList->Append(LIST_DESELECT_ALL, _T("&Deselect All\tCtrl-D"));
187 menuList->Append(LIST_SELECT_ALL, _T("S&elect All\tCtrl-A"));
188 menuList->AppendSeparator();
189 menuList->Append(LIST_SHOW_COL_INFO, _T("Show &column info\tCtrl-C"));
190 menuList->Append(LIST_SHOW_SEL_INFO, _T("Show &selected items\tCtrl-S"));
191 menuList->AppendSeparator();
192 menuList->Append(LIST_SORT, _T("&Sort\tCtrl-S"));
193 menuList->AppendSeparator();
194 menuList->Append(LIST_ADD, _T("&Append an item\tCtrl-P"));
195 menuList->Append(LIST_DELETE, _T("&Delete first item\tCtrl-X"));
196 menuList->Append(LIST_DELETE_ALL, _T("Delete &all items"));
197 menuList->AppendSeparator();
198 menuList->Append(LIST_TOGGLE_MULTI_SEL, _T("&Multiple selection\tCtrl-M"),
199 _T("Toggle multiple selection"), TRUE);
200
201 wxMenu *menuCol = new wxMenu;
202 menuCol->Append(LIST_SET_FG_COL, _T("&Foreground colour..."));
203 menuCol->Append(LIST_SET_BG_COL, _T("&Background colour..."));
204
205 wxMenuBar *menubar = new wxMenuBar;
206 menubar->Append(menuFile, _T("&File"));
207 menubar->Append(menuView, _T("&View"));
208 menubar->Append(menuList, _T("&List"));
209 menubar->Append(menuCol, _T("&Colour"));
210 SetMenuBar(menubar);
211
212 m_logWindow = new wxTextCtrl(this, -1, wxEmptyString,
213 wxDefaultPosition, wxDefaultSize,
214 wxTE_MULTILINE | wxSUNKEN_BORDER);
215
216 m_logOld = wxLog::SetActiveTarget(new wxLogTextCtrl(m_logWindow));
217
218 RecreateList(wxLC_REPORT | wxLC_SINGLE_SEL);
219
220 CreateStatusBar(3);
221 }
222
223 MyFrame::~MyFrame()
224 {
225 delete wxLog::SetActiveTarget(m_logOld);
226
227 delete m_imageListNormal;
228 delete m_imageListSmall;
229 }
230
231 void MyFrame::OnSize(wxSizeEvent& event)
232 {
233 if ( !m_logWindow )
234 return;
235
236 wxSize size = GetClientSize();
237 wxCoord y = (2*size.y)/3;
238 m_listCtrl->SetSize(0, 0, size.x, y);
239 m_logWindow->SetSize(0, y + 1, size.x, size.y - y);
240
241 event.Skip();
242 }
243
244 void MyFrame::OnQuit(wxCommandEvent& WXUNUSED(event))
245 {
246 Close(TRUE);
247 }
248
249 void MyFrame::OnAbout(wxCommandEvent& WXUNUSED(event))
250 {
251 wxMessageDialog dialog(this, "List test sample\nJulian Smart (c) 1997",
252 "About list test", wxOK|wxCANCEL);
253
254 dialog.ShowModal();
255 }
256
257 void MyFrame::OnFocusLast(wxCommandEvent& WXUNUSED(event))
258 {
259 long index = m_listCtrl->GetItemCount() - 1;
260 if ( index == -1 )
261 {
262 return;
263 }
264
265 m_listCtrl->SetItemState(index, wxLIST_STATE_FOCUSED, wxLIST_STATE_FOCUSED);
266 m_listCtrl->EnsureVisible(index);
267 }
268
269 void MyFrame::OnToggleFirstSel(wxCommandEvent& WXUNUSED(event))
270 {
271 m_listCtrl->SetItemState(0, wxLIST_STATE_SELECTED, wxLIST_STATE_SELECTED);
272 }
273
274 void MyFrame::OnDeselectAll(wxCommandEvent& WXUNUSED(event))
275 {
276 int n = m_listCtrl->GetItemCount();
277 for (int i = 0; i < n; i++)
278 m_listCtrl->SetItemState(i,0,wxLIST_STATE_SELECTED);
279 }
280
281 void MyFrame::OnSelectAll(wxCommandEvent& WXUNUSED(event))
282 {
283 int n = m_listCtrl->GetItemCount();
284 for (int i = 0; i < n; i++)
285 m_listCtrl->SetItemState(i,wxLIST_STATE_SELECTED, wxLIST_STATE_SELECTED);
286 }
287
288 // ----------------------------------------------------------------------------
289 // changing listctrl modes
290 // ----------------------------------------------------------------------------
291
292 void MyFrame::RecreateList(long flags, bool withText)
293 {
294 // we could avoid recreating it if we don't set/clear the wxLC_VIRTUAL
295 // style, but it is more trouble to do it than not
296 #if 0
297 if ( !m_listCtrl || ((flags & wxLC_VIRTUAL) !=
298 (m_listCtrl->GetWindowStyleFlag() & wxLC_VIRTUAL)) )
299 #endif
300 {
301 delete m_listCtrl;
302
303 m_listCtrl = new MyListCtrl(this, LIST_CTRL,
304 wxDefaultPosition, wxDefaultSize,
305 flags |
306 wxSUNKEN_BORDER);
307
308 switch ( flags & wxLC_MASK_TYPE )
309 {
310 case wxLC_LIST:
311 InitWithListItems();
312 break;
313
314 case wxLC_ICON:
315 InitWithIconItems(withText);
316 break;
317
318 case wxLC_SMALL_ICON:
319 InitWithIconItems(withText, TRUE);
320 break;
321
322 case wxLC_REPORT:
323 if ( flags & wxLC_VIRTUAL )
324 InitWithVirtualItems();
325 else
326 InitWithReportItems();
327 break;
328
329 default:
330 wxFAIL_MSG( _T("unknown listctrl mode") );
331 }
332 }
333
334 #ifdef __WXMSW__
335 SendSizeEvent();
336 #endif
337
338 m_logWindow->Clear();
339 }
340
341 void MyFrame::OnListView(wxCommandEvent& WXUNUSED(event))
342 {
343 RecreateList(wxLC_LIST);
344 }
345
346 void MyFrame::InitWithListItems()
347 {
348 for ( int i = 0; i < NUM_ITEMS; i++ )
349 {
350 m_listCtrl->InsertItem(i, wxString::Format(_T("Item %d"), i));
351 }
352 }
353
354 void MyFrame::OnReportView(wxCommandEvent& WXUNUSED(event))
355 {
356 RecreateList(wxLC_REPORT);
357 }
358
359 void MyFrame::InitWithReportItems()
360 {
361 m_listCtrl->SetImageList(m_imageListSmall, wxIMAGE_LIST_SMALL);
362
363 m_listCtrl->InsertColumn(0, "Column 1"); // , wxLIST_FORMAT_LEFT, 140);
364 m_listCtrl->InsertColumn(1, "Column 2"); // , wxLIST_FORMAT_LEFT, 140);
365 m_listCtrl->InsertColumn(2, "One More Column (2)"); // , wxLIST_FORMAT_LEFT, 140);
366
367 // to speed up inserting we hide the control temporarily
368 m_listCtrl->Hide();
369
370 wxStopWatch sw;
371
372 for ( int i = 0; i < NUM_ITEMS; i++ )
373 {
374 m_listCtrl->InsertItemInReportView(i);
375 }
376
377 m_logWindow->WriteText(wxString::Format(_T("%d items inserted in %ldms\n"),
378 NUM_ITEMS, sw.Time()));
379 m_listCtrl->Show();
380
381 // we leave all mask fields to 0 and only change the colour
382 wxListItem item;
383 item.m_itemId = 0;
384 item.SetTextColour(*wxRED);
385 m_listCtrl->SetItem( item );
386
387 item.m_itemId = 2;
388 item.SetTextColour(*wxGREEN);
389 m_listCtrl->SetItem( item );
390 item.m_itemId = 4;
391 item.SetTextColour(*wxLIGHT_GREY);
392 item.SetFont(*wxITALIC_FONT);
393 item.SetBackgroundColour(*wxRED);
394 m_listCtrl->SetItem( item );
395
396 m_listCtrl->SetTextColour(*wxBLUE);
397 m_listCtrl->SetBackgroundColour(*wxLIGHT_GREY);
398
399 m_listCtrl->SetColumnWidth( 0, wxLIST_AUTOSIZE );
400 m_listCtrl->SetColumnWidth( 1, wxLIST_AUTOSIZE );
401 m_listCtrl->SetColumnWidth( 2, wxLIST_AUTOSIZE );
402 }
403
404 void MyFrame::InitWithIconItems(bool withText, bool sameIcon)
405 {
406 m_listCtrl->SetImageList(m_imageListNormal, wxIMAGE_LIST_NORMAL);
407 m_listCtrl->SetImageList(m_imageListSmall, wxIMAGE_LIST_SMALL);
408
409 for ( int i = 0; i < NUM_ICONS; i++ )
410 {
411 int image = sameIcon ? 0 : i;
412
413 if ( withText )
414 {
415 m_listCtrl->InsertItem(i, wxString::Format(_T("Label %d"), i),
416 image);
417 }
418 else
419 {
420 m_listCtrl->InsertItem(i, image);
421 }
422 }
423 }
424
425 void MyFrame::OnIconView(wxCommandEvent& WXUNUSED(event))
426 {
427 RecreateList(wxLC_ICON, FALSE);
428 }
429
430 void MyFrame::OnIconTextView(wxCommandEvent& WXUNUSED(event))
431 {
432 RecreateList(wxLC_ICON);
433 }
434
435 void MyFrame::OnSmallIconView(wxCommandEvent& WXUNUSED(event))
436 {
437 RecreateList(wxLC_SMALL_ICON, FALSE);
438 }
439
440 void MyFrame::OnSmallIconTextView(wxCommandEvent& WXUNUSED(event))
441 {
442 RecreateList(wxLC_SMALL_ICON);
443 }
444
445 void MyFrame::OnVirtualView(wxCommandEvent& WXUNUSED(event))
446 {
447 RecreateList(wxLC_REPORT | wxLC_VIRTUAL);
448 }
449
450 void MyFrame::InitWithVirtualItems()
451 {
452 m_listCtrl->InsertColumn(0, "First Column");
453 m_listCtrl->InsertColumn(1, "Second Column");
454 m_listCtrl->SetColumnWidth(0, 150);
455 m_listCtrl->SetColumnWidth(1, 150);
456
457 m_listCtrl->SetItemCount(1000000);
458 }
459
460 void MyFrame::OnSort(wxCommandEvent& WXUNUSED(event))
461 {
462 wxStopWatch sw;
463
464 m_listCtrl->SortItems(MyCompareFunction, 0);
465
466 m_logWindow->WriteText(wxString::Format(_T("Sorting %d items took %ld ms\n"),
467 m_listCtrl->GetItemCount(),
468 sw.Time()));
469 }
470
471 void MyFrame::OnShowSelInfo(wxCommandEvent& event)
472 {
473 int selCount = m_listCtrl->GetSelectedItemCount();
474 wxLogMessage(_T("%d items selected:"), selCount);
475
476 // don't show too many items
477 size_t shownCount = 0;
478
479 long item = m_listCtrl->GetNextItem(-1, wxLIST_NEXT_ALL,
480 wxLIST_STATE_SELECTED);
481 while ( item != -1 )
482 {
483 wxLogMessage(_T("\t%ld (%s)"),
484 item, m_listCtrl->GetItemText(item).c_str());
485
486 if ( ++shownCount > 10 )
487 {
488 wxLogMessage(_T("\t... more selected items snipped..."));
489 break;
490 }
491
492 item = m_listCtrl->GetNextItem(item, wxLIST_NEXT_ALL,
493 wxLIST_STATE_SELECTED);
494 }
495 }
496
497 void MyFrame::OnShowColInfo(wxCommandEvent& event)
498 {
499 int count = m_listCtrl->GetColumnCount();
500 wxLogMessage(wxT("%d columns:"), count);
501 for ( int c = 0; c < count; c++ )
502 {
503 wxLogMessage(wxT("\tcolumn %d has width %d"), c,
504 m_listCtrl->GetColumnWidth(c));
505 }
506 }
507
508 void MyFrame::OnUpdateShowColInfo(wxUpdateUIEvent& event)
509 {
510 event.Enable( (m_listCtrl->GetWindowStyleFlag() & wxLC_REPORT) != 0 );
511 }
512
513 void MyFrame::OnToggleMultiSel(wxCommandEvent& WXUNUSED(event))
514 {
515 long flags = m_listCtrl->GetWindowStyleFlag();
516 if ( flags & wxLC_SINGLE_SEL )
517 flags &= ~wxLC_SINGLE_SEL;
518 else
519 flags |= wxLC_SINGLE_SEL;
520
521 m_logWindow->WriteText(wxString::Format(wxT("Current selection mode: %sle\n"),
522 (flags & wxLC_SINGLE_SEL) ? "sing" : "multip"));
523
524 RecreateList(flags);
525 }
526
527 void MyFrame::OnSetFgColour(wxCommandEvent& WXUNUSED(event))
528 {
529 m_listCtrl->SetForegroundColour(wxGetColourFromUser(this));
530 m_listCtrl->Refresh();
531 }
532
533 void MyFrame::OnSetBgColour(wxCommandEvent& WXUNUSED(event))
534 {
535 m_listCtrl->SetBackgroundColour(wxGetColourFromUser(this));
536 m_listCtrl->Refresh();
537 }
538
539 void MyFrame::OnAdd(wxCommandEvent& WXUNUSED(event))
540 {
541 m_listCtrl->InsertItem(m_listCtrl->GetItemCount(), _T("Appended item"));
542 }
543
544 void MyFrame::OnDelete(wxCommandEvent& WXUNUSED(event))
545 {
546 if ( m_listCtrl->GetItemCount() )
547 {
548 m_listCtrl->DeleteItem(0);
549 }
550 else
551 {
552 m_logWindow->WriteText("Nothing to delete");
553 }
554 }
555
556 void MyFrame::OnDeleteAll(wxCommandEvent& WXUNUSED(event))
557 {
558 wxStopWatch sw;
559
560 m_listCtrl->DeleteAllItems();
561
562 m_logWindow->WriteText(wxString::Format(_T("Deleting %d items took %ld ms\n"),
563 m_listCtrl->GetItemCount(),
564 sw.Time()));
565 }
566
567 // MyListCtrl
568
569 void MyListCtrl::OnCacheHint(wxListEvent& event)
570 {
571 wxLogMessage( wxT("OnCacheHint: cache items %ld..%ld"),
572 event.GetCacheFrom(), event.GetCacheTo() );
573 }
574
575 void MyListCtrl::OnColClick(wxListEvent& event)
576 {
577 wxLogMessage( wxT("OnColumnClick at %d."), event.GetColumn() );
578 }
579
580 void MyListCtrl::OnBeginDrag(wxListEvent& event)
581 {
582 const wxPoint& pt = event.m_pointDrag;
583
584 int flags;
585 wxLogMessage( wxT("OnBeginDrag at (%d, %d), item %ld."),
586 pt.x, pt.y, HitTest(pt, flags) );
587 }
588
589 void MyListCtrl::OnBeginRDrag(wxListEvent& event)
590 {
591 wxLogMessage( wxT("OnBeginRDrag at %d,%d."),
592 event.m_pointDrag.x, event.m_pointDrag.y );
593 }
594
595 void MyListCtrl::OnBeginLabelEdit(wxListEvent& event)
596 {
597 wxLogMessage( wxT("OnBeginLabelEdit: %s"), event.m_item.m_text.c_str());
598 }
599
600 void MyListCtrl::OnEndLabelEdit(wxListEvent& event)
601 {
602 wxLogMessage( wxT("OnEndLabelEdit: %s"), event.m_item.m_text.c_str());
603 }
604
605 void MyListCtrl::OnDeleteItem(wxListEvent& event)
606 {
607 LogEvent(event, _T("OnDeleteItem"));
608 }
609
610 void MyListCtrl::OnDeleteAllItems(wxListEvent& event)
611 {
612 LogEvent(event, _T("OnDeleteAllItems"));
613 }
614
615 void MyListCtrl::OnGetInfo(wxListEvent& event)
616 {
617 wxString msg;
618
619 msg << "OnGetInfo (" << event.m_item.m_itemId << ", " << event.m_item.m_col << ")";
620 if ( event.m_item.m_mask & wxLIST_MASK_STATE )
621 msg << " wxLIST_MASK_STATE";
622 if ( event.m_item.m_mask & wxLIST_MASK_TEXT )
623 msg << " wxLIST_MASK_TEXT";
624 if ( event.m_item.m_mask & wxLIST_MASK_IMAGE )
625 msg << " wxLIST_MASK_IMAGE";
626 if ( event.m_item.m_mask & wxLIST_MASK_DATA )
627 msg << " wxLIST_MASK_DATA";
628 if ( event.m_item.m_mask & wxLIST_SET_ITEM )
629 msg << " wxLIST_SET_ITEM";
630 if ( event.m_item.m_mask & wxLIST_MASK_WIDTH )
631 msg << " wxLIST_MASK_WIDTH";
632 if ( event.m_item.m_mask & wxLIST_MASK_FORMAT )
633 msg << " wxLIST_MASK_WIDTH";
634
635 if ( event.m_item.m_mask & wxLIST_MASK_TEXT )
636 {
637 event.m_item.m_text = "My callback text";
638 }
639
640 wxLogMessage(msg);
641 }
642
643 void MyListCtrl::OnSetInfo(wxListEvent& event)
644 {
645 LogEvent(event, _T("OnSetInfo"));
646 }
647
648 void MyListCtrl::OnSelected(wxListEvent& event)
649 {
650 LogEvent(event, _T("OnSelected"));
651
652 if ( GetWindowStyle() & wxLC_REPORT )
653 {
654 wxListItem info;
655 info.m_itemId = event.m_itemIndex;
656 info.m_col = 1;
657 info.m_mask = wxLIST_MASK_TEXT;
658 if ( GetItem(info) )
659 {
660 wxLogMessage(wxT("Value of the 2nd field of the selected item: %s"),
661 info.m_text.c_str());
662 }
663 else
664 {
665 wxFAIL_MSG(wxT("wxListCtrl::GetItem() failed"));
666 }
667 }
668 }
669
670 void MyListCtrl::OnDeselected(wxListEvent& event)
671 {
672 LogEvent(event, _T("OnDeselected"));
673 }
674
675 void MyListCtrl::OnActivated(wxListEvent& event)
676 {
677 LogEvent(event, _T("OnActivated"));
678 }
679
680 void MyListCtrl::OnListKeyDown(wxListEvent& event)
681 {
682 switch ( event.GetCode() )
683 {
684 case 'c':
685 {
686 wxListItem info;
687 info.m_itemId = event.GetIndex();
688 GetItem(info);
689
690 wxListItemAttr *attr = info.GetAttributes();
691 if ( !attr || !attr->HasTextColour() )
692 {
693 info.SetTextColour(*wxCYAN);
694
695 SetItem(info);
696 }
697 }
698 break;
699
700 case WXK_DELETE:
701 {
702 long item = GetNextItem(-1,
703 wxLIST_NEXT_ALL, wxLIST_STATE_SELECTED);
704 while ( item != -1 )
705 {
706 DeleteItem(item);
707
708 wxLogMessage(_T("Item %ld deleted"), item);
709
710 // -1 because the indices were shifted by DeleteItem()
711 item = GetNextItem(item - 1,
712 wxLIST_NEXT_ALL, wxLIST_STATE_SELECTED);
713 }
714 }
715 break;
716
717 case WXK_INSERT:
718 if ( GetWindowStyle() & wxLC_REPORT )
719 {
720 if ( GetWindowStyle() & wxLC_VIRTUAL )
721 {
722 SetItemCount(GetItemCount() + 1);
723 }
724 else // !virtual
725 {
726 InsertItemInReportView(event.GetIndex());
727 }
728 }
729 //else: fall through
730
731 default:
732 LogEvent(event, _T("OnListKeyDown"));
733
734 event.Skip();
735 }
736 }
737
738 void MyListCtrl::OnChar(wxKeyEvent& event)
739 {
740 wxLogMessage(_T("Got char event."));
741
742 event.Skip();
743 }
744
745 void MyListCtrl::LogEvent(const wxListEvent& event, const wxChar *eventName)
746 {
747 wxLogMessage(_T("Item %ld: %s (item text = %s, data = %ld)"),
748 event.GetIndex(), eventName,
749 event.GetText().c_str(), event.GetData());
750 }
751
752 wxString MyListCtrl::OnGetItemText(long item, long column) const
753 {
754 return wxString::Format(_T("Column %ld of item %ld"), column, item);
755 }
756
757 int MyListCtrl::OnGetItemImage(long item) const
758 {
759 return 0;
760 }
761
762 wxListItemAttr *MyListCtrl::OnGetItemAttr(long item) const
763 {
764 return item % 2 ? NULL : (wxListItemAttr *)&m_attr;
765 }
766
767 void MyListCtrl::InsertItemInReportView(int i)
768 {
769 wxString buf;
770 buf.Printf(_T("This is item %d"), i);
771 long tmp = InsertItem(i, buf, 0);
772 SetItemData(tmp, i);
773
774 buf.Printf(_T("Col 1, item %d"), i);
775 SetItem(i, 1, buf);
776
777 buf.Printf(_T("Item %d in column 2"), i);
778 SetItem(i, 2, buf);
779 }
780