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