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