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