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