]> git.saurik.com Git - wxWidgets.git/blob - samples/listctrl/listtest.cpp
fix tab traversal of wxStaticBox children, closes #15376
[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 // Copyright: (c) Julian Smart
8 // Licence: wxWindows licence
9 /////////////////////////////////////////////////////////////////////////////
10
11 // For compilers that support precompilation, includes "wx/wx.h".
12 #include "wx/wxprec.h"
13
14 #ifdef __BORLANDC__
15 #pragma hdrstop
16 #endif
17
18 #ifndef WX_PRECOMP
19 #include "wx/wx.h"
20 #endif
21
22 #ifndef wxHAS_IMAGES_IN_RESOURCES
23 #include "../sample.xpm"
24 #endif
25
26 #ifndef wxHAS_IMAGES_IN_RESOURCES
27 #include "bitmaps/toolbrai.xpm"
28 #include "bitmaps/toolchar.xpm"
29 #include "bitmaps/tooldata.xpm"
30 #include "bitmaps/toolnote.xpm"
31 #include "bitmaps/tooltodo.xpm"
32 #include "bitmaps/toolchec.xpm"
33 #include "bitmaps/toolgame.xpm"
34 #include "bitmaps/tooltime.xpm"
35 #include "bitmaps/toolword.xpm"
36 #include "bitmaps/small1.xpm"
37 #endif
38
39 #include "wx/imaglist.h"
40 #include "wx/listctrl.h"
41 #include "wx/timer.h" // for wxStopWatch
42 #include "wx/colordlg.h" // for wxGetColourFromUser
43 #include "wx/settings.h"
44 #include "wx/sysopt.h"
45 #include "wx/numdlg.h"
46
47 #include "listtest.h"
48
49
50 // ----------------------------------------------------------------------------
51 // Constants and globals
52 // ----------------------------------------------------------------------------
53
54 const wxChar *SMALL_VIRTUAL_VIEW_ITEMS[][2] =
55 {
56 { wxT("Cat"), wxT("meow") },
57 { wxT("Cow"), wxT("moo") },
58 { wxT("Crow"), wxT("caw") },
59 { wxT("Dog"), wxT("woof") },
60 { wxT("Duck"), wxT("quack") },
61 { wxT("Mouse"), wxT("squeak") },
62 { wxT("Owl"), wxT("hoo") },
63 { wxT("Pig"), wxT("oink") },
64 { wxT("Pigeon"), wxT("coo") },
65 { wxT("Sheep"), wxT("baaah") },
66 };
67
68 // number of items in icon/small icon view
69 static const int NUM_ICONS = 9;
70
71 int wxCALLBACK
72 MyCompareFunction(wxIntPtr item1, wxIntPtr item2, wxIntPtr WXUNUSED(sortData))
73 {
74 // inverse the order
75 if (item1 < item2)
76 return 1;
77 if (item1 > item2)
78 return -1;
79
80 return 0;
81 }
82
83
84 // ----------------------------------------------------------------------------
85 // MyApp
86 // ----------------------------------------------------------------------------
87
88 IMPLEMENT_APP(MyApp)
89
90 // `Main program' equivalent, creating windows and returning main app frame
91 bool MyApp::OnInit()
92 {
93 if ( !wxApp::OnInit() )
94 return false;
95
96 // Create the main frame window
97 MyFrame *frame = new MyFrame(wxT("wxListCtrl Test"));
98
99 // Show the frame
100 frame->Show(true);
101
102 return true;
103 }
104
105
106
107 // ----------------------------------------------------------------------------
108 // MyFrame
109 // ----------------------------------------------------------------------------
110
111 BEGIN_EVENT_TABLE(MyFrame, wxFrame)
112 EVT_SIZE(MyFrame::OnSize)
113
114 EVT_MENU(LIST_QUIT, MyFrame::OnQuit)
115 EVT_MENU(LIST_ABOUT, MyFrame::OnAbout)
116 EVT_MENU(LIST_LIST_VIEW, MyFrame::OnListView)
117 EVT_MENU(LIST_REPORT_VIEW, MyFrame::OnReportView)
118 EVT_MENU(LIST_ICON_VIEW, MyFrame::OnIconView)
119 EVT_MENU(LIST_ICON_TEXT_VIEW, MyFrame::OnIconTextView)
120 EVT_MENU(LIST_SMALL_ICON_VIEW, MyFrame::OnSmallIconView)
121 EVT_MENU(LIST_SMALL_ICON_TEXT_VIEW, MyFrame::OnSmallIconTextView)
122 EVT_MENU(LIST_VIRTUAL_VIEW, MyFrame::OnVirtualView)
123 EVT_MENU(LIST_SMALL_VIRTUAL_VIEW, MyFrame::OnSmallVirtualView)
124
125 EVT_MENU(LIST_SET_ITEMS_COUNT, MyFrame::OnSetItemsCount)
126
127 EVT_MENU(LIST_GOTO, MyFrame::OnGoTo)
128 EVT_MENU(LIST_FOCUS_LAST, MyFrame::OnFocusLast)
129 EVT_MENU(LIST_TOGGLE_FIRST, MyFrame::OnToggleFirstSel)
130 EVT_MENU(LIST_DESELECT_ALL, MyFrame::OnDeselectAll)
131 EVT_MENU(LIST_SELECT_ALL, MyFrame::OnSelectAll)
132 EVT_MENU(LIST_DELETE, MyFrame::OnDelete)
133 EVT_MENU(LIST_ADD, MyFrame::OnAdd)
134 EVT_MENU(LIST_EDIT, MyFrame::OnEdit)
135 EVT_MENU(LIST_DELETE_ALL, MyFrame::OnDeleteAll)
136 EVT_MENU(LIST_SORT, MyFrame::OnSort)
137 EVT_MENU(LIST_SET_FG_COL, MyFrame::OnSetFgColour)
138 EVT_MENU(LIST_SET_BG_COL, MyFrame::OnSetBgColour)
139 EVT_MENU(LIST_ROW_LINES, MyFrame::OnSetRowLines)
140 EVT_MENU(LIST_TOGGLE_MULTI_SEL, MyFrame::OnToggleMultiSel)
141 EVT_MENU(LIST_SHOW_COL_INFO, MyFrame::OnShowColInfo)
142 EVT_MENU(LIST_SHOW_SEL_INFO, MyFrame::OnShowSelInfo)
143 EVT_MENU(LIST_SHOW_VIEW_RECT, MyFrame::OnShowViewRect)
144 #ifdef wxHAS_LISTCTRL_COLUMN_ORDER
145 EVT_MENU(LIST_SET_COL_ORDER, MyFrame::OnSetColOrder)
146 EVT_MENU(LIST_GET_COL_ORDER, MyFrame::OnGetColOrder)
147 #endif // wxHAS_LISTCTRL_COLUMN_ORDER
148 EVT_MENU(LIST_FREEZE, MyFrame::OnFreeze)
149 EVT_MENU(LIST_THAW, MyFrame::OnThaw)
150 EVT_MENU(LIST_TOGGLE_LINES, MyFrame::OnToggleLines)
151 EVT_MENU(LIST_TOGGLE_HEADER, MyFrame::OnToggleHeader)
152 EVT_MENU(LIST_TOGGLE_BELL, MyFrame::OnToggleBell)
153 #ifdef __WXOSX__
154 EVT_MENU(LIST_MAC_USE_GENERIC, MyFrame::OnToggleMacUseGeneric)
155 #endif // __WXOSX__
156 EVT_MENU(LIST_FIND, MyFrame::OnFind)
157
158 EVT_UPDATE_UI(LIST_SHOW_COL_INFO, MyFrame::OnUpdateUIEnableInReport)
159 EVT_UPDATE_UI(LIST_TOGGLE_HEADER, MyFrame::OnUpdateUIEnableInReport)
160
161 EVT_UPDATE_UI(LIST_TOGGLE_MULTI_SEL, MyFrame::OnUpdateToggleMultiSel)
162 EVT_UPDATE_UI(LIST_TOGGLE_HEADER, MyFrame::OnUpdateToggleHeader)
163 EVT_UPDATE_UI(LIST_ROW_LINES, MyFrame::OnUpdateRowLines)
164 END_EVENT_TABLE()
165
166 // My frame constructor
167 MyFrame::MyFrame(const wxChar *title)
168 : wxFrame(NULL, wxID_ANY, title, wxDefaultPosition, wxSize(600, 500))
169 {
170 m_listCtrl = NULL;
171 m_logWindow = NULL;
172 m_smallVirtual = false;
173 m_numListItems = 10;
174
175 // Give it an icon
176 SetIcon(wxICON(sample));
177
178 // Make an image list containing large icons
179 m_imageListNormal = new wxImageList(32, 32, true);
180 m_imageListSmall = new wxImageList(16, 16, true);
181
182 #ifdef wxHAS_IMAGES_IN_RESOURCES
183 m_imageListNormal->Add( wxIcon(wxT("icon1"), wxBITMAP_TYPE_ICO_RESOURCE) );
184 m_imageListNormal->Add( wxIcon(wxT("icon2"), wxBITMAP_TYPE_ICO_RESOURCE) );
185 m_imageListNormal->Add( wxIcon(wxT("icon3"), wxBITMAP_TYPE_ICO_RESOURCE) );
186 m_imageListNormal->Add( wxIcon(wxT("icon4"), wxBITMAP_TYPE_ICO_RESOURCE) );
187 m_imageListNormal->Add( wxIcon(wxT("icon5"), wxBITMAP_TYPE_ICO_RESOURCE) );
188 m_imageListNormal->Add( wxIcon(wxT("icon6"), wxBITMAP_TYPE_ICO_RESOURCE) );
189 m_imageListNormal->Add( wxIcon(wxT("icon7"), wxBITMAP_TYPE_ICO_RESOURCE) );
190 m_imageListNormal->Add( wxIcon(wxT("icon8"), wxBITMAP_TYPE_ICO_RESOURCE) );
191 m_imageListNormal->Add( wxIcon(wxT("icon9"), wxBITMAP_TYPE_ICO_RESOURCE) );
192
193 m_imageListSmall->Add( wxIcon(wxT("iconsmall"), wxBITMAP_TYPE_ICO_RESOURCE) );
194
195 #else
196 m_imageListNormal->Add( wxIcon( toolbrai_xpm ) );
197 m_imageListNormal->Add( wxIcon( toolchar_xpm ) );
198 m_imageListNormal->Add( wxIcon( tooldata_xpm ) );
199 m_imageListNormal->Add( wxIcon( toolnote_xpm ) );
200 m_imageListNormal->Add( wxIcon( tooltodo_xpm ) );
201 m_imageListNormal->Add( wxIcon( toolchec_xpm ) );
202 m_imageListNormal->Add( wxIcon( toolgame_xpm ) );
203 m_imageListNormal->Add( wxIcon( tooltime_xpm ) );
204 m_imageListNormal->Add( wxIcon( toolword_xpm ) );
205
206 m_imageListSmall->Add( wxIcon( small1_xpm) );
207 #endif
208
209 // Make a menubar
210 wxMenu *menuFile = new wxMenu;
211 menuFile->Append(LIST_ABOUT, wxT("&About"));
212 menuFile->AppendSeparator();
213 menuFile->Append(LIST_QUIT, wxT("E&xit\tAlt-X"));
214
215 wxMenu *menuView = new wxMenu;
216 menuView->Append(LIST_LIST_VIEW, wxT("&List view\tF1"));
217 menuView->Append(LIST_REPORT_VIEW, wxT("&Report view\tF2"));
218 menuView->Append(LIST_ICON_VIEW, wxT("&Icon view\tF3"));
219 menuView->Append(LIST_ICON_TEXT_VIEW, wxT("Icon view with &text\tF4"));
220 menuView->Append(LIST_SMALL_ICON_VIEW, wxT("&Small icon view\tF5"));
221 menuView->Append(LIST_SMALL_ICON_TEXT_VIEW, wxT("Small icon &view with text\tF6"));
222 menuView->Append(LIST_VIRTUAL_VIEW, wxT("&Virtual view\tF7"));
223 menuView->Append(LIST_SMALL_VIRTUAL_VIEW, wxT("Small virtual vie&w\tF8"));
224 menuView->AppendSeparator();
225 menuView->Append(LIST_SET_ITEMS_COUNT, "Set &number of items");
226 #ifdef __WXOSX__
227 menuView->AppendSeparator();
228 menuView->AppendCheckItem(LIST_MAC_USE_GENERIC, wxT("Mac: Use Generic Control"));
229 #endif
230
231 wxMenu *menuList = new wxMenu;
232 menuList->Append(LIST_GOTO, wxT("&Go to item #3\tCtrl-3"));
233 menuList->Append(LIST_FOCUS_LAST, wxT("&Make last item current\tCtrl-L"));
234 menuList->Append(LIST_TOGGLE_FIRST, wxT("To&ggle first item\tCtrl-G"));
235 menuList->Append(LIST_DESELECT_ALL, wxT("&Deselect All\tCtrl-D"));
236 menuList->Append(LIST_SELECT_ALL, wxT("S&elect All\tCtrl-A"));
237 menuList->AppendSeparator();
238 menuList->Append(LIST_SHOW_COL_INFO, wxT("Show &column info\tCtrl-C"));
239 menuList->Append(LIST_SHOW_SEL_INFO, wxT("Show &selected items\tCtrl-S"));
240 menuList->Append(LIST_SHOW_VIEW_RECT, wxT("Show &view rect"));
241 #ifdef wxHAS_LISTCTRL_COLUMN_ORDER
242 menuList->Append(LIST_SET_COL_ORDER, wxT("Se&t columns order\tShift-Ctrl-O"));
243 menuList->Append(LIST_GET_COL_ORDER, wxT("Sho&w columns order\tCtrl-O"));
244 #endif // wxHAS_LISTCTRL_COLUMN_ORDER
245 menuList->AppendSeparator();
246 menuList->Append(LIST_SORT, wxT("Sor&t\tCtrl-T"));
247 menuList->Append(LIST_FIND, "Test Find() performance");
248 menuList->AppendSeparator();
249 menuList->Append(LIST_ADD, wxT("&Append an item\tCtrl-P"));
250 menuList->Append(LIST_EDIT, wxT("&Edit the item\tCtrl-E"));
251 menuList->Append(LIST_DELETE, wxT("&Delete first item\tCtrl-X"));
252 menuList->Append(LIST_DELETE_ALL, wxT("Delete &all items"));
253 menuList->AppendSeparator();
254 menuList->Append(LIST_FREEZE, wxT("Free&ze\tCtrl-Z"));
255 menuList->Append(LIST_THAW, wxT("Tha&w\tCtrl-W"));
256 menuList->AppendSeparator();
257 menuList->AppendCheckItem(LIST_TOGGLE_LINES, wxT("Toggle &lines\tCtrl-I"));
258 menuList->AppendCheckItem(LIST_TOGGLE_MULTI_SEL,
259 wxT("&Multiple selection\tCtrl-M"));
260 menuList->Check(LIST_TOGGLE_MULTI_SEL, true);
261 menuList->AppendCheckItem(LIST_TOGGLE_HEADER, "Toggle &header\tCtrl-H");
262 menuList->Check(LIST_TOGGLE_HEADER, true);
263 menuList->AppendCheckItem(LIST_TOGGLE_BELL, "Toggle &bell on no match");
264
265 wxMenu *menuCol = new wxMenu;
266 menuCol->Append(LIST_SET_FG_COL, wxT("&Foreground colour..."));
267 menuCol->Append(LIST_SET_BG_COL, wxT("&Background colour..."));
268 menuCol->AppendCheckItem(LIST_ROW_LINES, wxT("Alternating colours"));
269
270 wxMenuBar *menubar = new wxMenuBar;
271 menubar->Append(menuFile, wxT("&File"));
272 menubar->Append(menuView, wxT("&View"));
273 menubar->Append(menuList, wxT("&List"));
274 menubar->Append(menuCol, wxT("&Colour"));
275 SetMenuBar(menubar);
276
277 m_panel = new wxPanel(this, wxID_ANY);
278 m_logWindow = new wxTextCtrl(m_panel, wxID_ANY, wxEmptyString,
279 wxDefaultPosition, wxDefaultSize,
280 wxTE_READONLY | wxTE_MULTILINE | wxSUNKEN_BORDER);
281
282 m_logOld = wxLog::SetActiveTarget(new wxLogTextCtrl(m_logWindow));
283
284 RecreateList(wxLC_REPORT | wxLC_SINGLE_SEL);
285
286 #ifdef __WXMSW__
287 // this is useful to know specially when debugging :)
288 wxLogMessage("Your version of comctl32.dll is: %d",
289 wxApp::GetComCtl32Version());
290 #endif
291
292 #if wxUSE_STATUSBAR
293 CreateStatusBar();
294 #endif // wxUSE_STATUSBAR
295 }
296
297 MyFrame::~MyFrame()
298 {
299 delete wxLog::SetActiveTarget(m_logOld);
300
301 delete m_imageListNormal;
302 delete m_imageListSmall;
303 }
304
305 void MyFrame::OnSize(wxSizeEvent& event)
306 {
307 DoSize();
308
309 event.Skip();
310 }
311
312 void MyFrame::DoSize()
313 {
314 if ( !m_logWindow )
315 return;
316
317 wxSize size = GetClientSize();
318 wxCoord y = (2*size.y)/3;
319 m_listCtrl->SetSize(0, 0, size.x, y);
320 m_logWindow->SetSize(0, y + 1, size.x, size.y - y -1);
321 }
322
323 bool MyFrame::CheckNonVirtual() const
324 {
325 if ( !m_listCtrl->HasFlag(wxLC_VIRTUAL) )
326 return true;
327
328 // "this" == whatever
329 wxLogWarning(wxT("Can't do this in virtual view, sorry."));
330
331 return false;
332 }
333
334 void MyFrame::OnQuit(wxCommandEvent& WXUNUSED(event))
335 {
336 Close(true);
337 }
338
339 void MyFrame::OnAbout(wxCommandEvent& WXUNUSED(event))
340 {
341 wxMessageDialog dialog(this, wxT("List test sample\nJulian Smart (c) 1997"),
342 wxT("About list test"));
343
344 dialog.ShowModal();
345 }
346
347 void MyFrame::OnFreeze(wxCommandEvent& WXUNUSED(event))
348 {
349 wxLogMessage(wxT("Freezing the control"));
350
351 m_listCtrl->Freeze();
352 }
353
354 void MyFrame::OnThaw(wxCommandEvent& WXUNUSED(event))
355 {
356 wxLogMessage(wxT("Thawing the control"));
357
358 m_listCtrl->Thaw();
359 }
360
361 void MyFrame::OnToggleLines(wxCommandEvent& event)
362 {
363 m_listCtrl->SetSingleStyle(wxLC_HRULES | wxLC_VRULES, event.IsChecked());
364 }
365
366 void MyFrame::OnToggleHeader(wxCommandEvent& event)
367 {
368 wxLogMessage("%s the header", event.IsChecked() ? "Showing" : "Hiding");
369
370 m_listCtrl->ToggleWindowStyle(wxLC_NO_HEADER);
371 }
372
373 void MyFrame::OnToggleBell(wxCommandEvent& event)
374 {
375 m_listCtrl->EnableBellOnNoMatch(event.IsChecked());
376 }
377
378 #ifdef __WXOSX__
379
380 void MyFrame::OnToggleMacUseGeneric(wxCommandEvent& event)
381 {
382 wxSystemOptions::SetOption(wxT("mac.listctrl.always_use_generic"), event.IsChecked());
383 }
384
385 #endif // __WXOSX__
386
387 void MyFrame::OnGoTo(wxCommandEvent& WXUNUSED(event))
388 {
389 long index = 3;
390 m_listCtrl->SetItemState(index, wxLIST_STATE_FOCUSED, wxLIST_STATE_FOCUSED);
391
392 long sel = m_listCtrl->GetNextItem(-1, wxLIST_NEXT_ALL,
393 wxLIST_STATE_SELECTED);
394 if ( sel != -1 )
395 m_listCtrl->SetItemState(sel, 0, wxLIST_STATE_SELECTED);
396 m_listCtrl->SetItemState(index, wxLIST_STATE_SELECTED, wxLIST_STATE_SELECTED);
397 }
398
399 void MyFrame::OnFocusLast(wxCommandEvent& WXUNUSED(event))
400 {
401 long index = m_listCtrl->GetItemCount() - 1;
402 if ( index == -1 )
403 {
404 return;
405 }
406
407 m_listCtrl->SetItemState(index, wxLIST_STATE_FOCUSED, wxLIST_STATE_FOCUSED);
408 m_listCtrl->EnsureVisible(index);
409 }
410
411 void MyFrame::OnToggleFirstSel(wxCommandEvent& WXUNUSED(event))
412 {
413 m_listCtrl->SetItemState(0, (~m_listCtrl->GetItemState(0, wxLIST_STATE_SELECTED) ) & wxLIST_STATE_SELECTED, wxLIST_STATE_SELECTED);
414 }
415
416 void MyFrame::OnDeselectAll(wxCommandEvent& WXUNUSED(event))
417 {
418 if ( !CheckNonVirtual() )
419 return;
420
421 int n = m_listCtrl->GetItemCount();
422 for (int i = 0; i < n; i++)
423 m_listCtrl->SetItemState(i,0,wxLIST_STATE_SELECTED);
424 }
425
426 void MyFrame::OnSelectAll(wxCommandEvent& WXUNUSED(event))
427 {
428 if ( !CheckNonVirtual() )
429 return;
430
431 int n = m_listCtrl->GetItemCount();
432 for (int i = 0; i < n; i++)
433 m_listCtrl->SetItemState(i,wxLIST_STATE_SELECTED, wxLIST_STATE_SELECTED);
434 }
435
436 // ----------------------------------------------------------------------------
437 // changing listctrl modes
438 // ----------------------------------------------------------------------------
439
440 void MyFrame::RecreateList(long flags, bool withText)
441 {
442 // we could avoid recreating it if we don't set/clear the wxLC_VIRTUAL
443 // style, but it is more trouble to do it than not
444 #if 0
445 if ( !m_listCtrl || ((flags & wxLC_VIRTUAL) !=
446 (m_listCtrl->GetWindowStyleFlag() & wxLC_VIRTUAL)) )
447 #endif
448 {
449 delete m_listCtrl;
450
451 m_listCtrl = new MyListCtrl(m_panel, LIST_CTRL,
452 wxDefaultPosition, wxDefaultSize,
453 flags |
454 wxBORDER_THEME | wxLC_EDIT_LABELS);
455
456 switch ( flags & wxLC_MASK_TYPE )
457 {
458 case wxLC_LIST:
459 InitWithListItems();
460 break;
461
462 case wxLC_ICON:
463 InitWithIconItems(withText);
464 break;
465
466 case wxLC_SMALL_ICON:
467 InitWithIconItems(withText, true);
468 break;
469
470 case wxLC_REPORT:
471 if ( flags & wxLC_VIRTUAL )
472 InitWithVirtualItems();
473 else
474 InitWithReportItems();
475 break;
476
477 default:
478 wxFAIL_MSG( wxT("unknown listctrl mode") );
479 }
480
481 wxMenuBar* const mb = GetMenuBar();
482 if ( mb )
483 m_listCtrl->EnableBellOnNoMatch(mb->IsChecked(LIST_TOGGLE_BELL));
484 }
485
486 DoSize();
487
488 GetMenuBar()->Check(LIST_ROW_LINES, false);
489
490 m_logWindow->Clear();
491 }
492
493 void MyFrame::OnListView(wxCommandEvent& WXUNUSED(event))
494 {
495 RecreateList(wxLC_LIST);
496 }
497
498 void MyFrame::InitWithListItems()
499 {
500 for ( int i = 0; i < m_numListItems; i++ )
501 {
502 m_listCtrl->InsertItem(i, wxString::Format(wxT("Item %d"), i));
503 }
504 }
505
506 void MyFrame::OnReportView(wxCommandEvent& WXUNUSED(event))
507 {
508 RecreateList(wxLC_REPORT);
509 }
510
511 void MyFrame::InitWithReportItems()
512 {
513 m_listCtrl->SetImageList(m_imageListSmall, wxIMAGE_LIST_SMALL);
514
515 // note that under MSW for SetColumnWidth() to work we need to create the
516 // items with images initially even if we specify dummy image id
517 wxListItem itemCol;
518 itemCol.SetText(wxT("Column 1"));
519 itemCol.SetImage(-1);
520 m_listCtrl->InsertColumn(0, itemCol);
521
522 itemCol.SetText(wxT("Column 2"));
523 itemCol.SetAlign(wxLIST_FORMAT_CENTRE);
524 m_listCtrl->InsertColumn(1, itemCol);
525
526 itemCol.SetText(wxT("Column 3"));
527 itemCol.SetAlign(wxLIST_FORMAT_RIGHT);
528 m_listCtrl->InsertColumn(2, itemCol);
529
530 // to speed up inserting we hide the control temporarily
531 m_listCtrl->Hide();
532
533 wxStopWatch sw;
534
535 for ( int i = 0; i < m_numListItems; i++ )
536 {
537 m_listCtrl->InsertItemInReportView(i);
538 }
539
540 m_logWindow->WriteText(wxString::Format(wxT("%d items inserted in %ldms\n"),
541 m_numListItems, sw.Time()));
542 m_listCtrl->Show();
543
544 // we leave all mask fields to 0 and only change the colour
545 wxListItem item;
546 item.m_itemId = 0;
547 item.SetTextColour(*wxRED);
548 m_listCtrl->SetItem( item );
549
550 item.m_itemId = 2;
551 item.SetTextColour(*wxGREEN);
552 m_listCtrl->SetItem( item );
553 item.m_itemId = 4;
554 item.SetTextColour(*wxLIGHT_GREY);
555 item.SetFont(*wxITALIC_FONT);
556 item.SetBackgroundColour(*wxRED);
557 m_listCtrl->SetItem( item );
558
559 m_listCtrl->SetTextColour(*wxBLUE);
560
561 m_listCtrl->SetColumnWidth( 0, wxLIST_AUTOSIZE );
562 m_listCtrl->SetColumnWidth( 1, wxLIST_AUTOSIZE );
563 m_listCtrl->SetColumnWidth( 2, wxLIST_AUTOSIZE );
564
565 // Set images in columns
566 m_listCtrl->SetItemColumnImage(1, 1, 0);
567
568 wxListItem info;
569 info.SetImage(0);
570 info.SetId(3);
571 info.SetColumn(2);
572 m_listCtrl->SetItem(info);
573
574 // test SetItemFont too
575 m_listCtrl->SetItemFont(0, *wxITALIC_FONT);
576 }
577
578 void MyFrame::InitWithIconItems(bool withText, bool sameIcon)
579 {
580 m_listCtrl->SetImageList(m_imageListNormal, wxIMAGE_LIST_NORMAL);
581 m_listCtrl->SetImageList(m_imageListSmall, wxIMAGE_LIST_SMALL);
582
583 for ( int i = 0; i < NUM_ICONS; i++ )
584 {
585 int image = sameIcon ? 0 : i;
586
587 if ( withText )
588 {
589 // Make labels of different widths to test the layout.
590 wxString label;
591 if ( !(i % 5) )
592 label.Printf("Longer label %d", i);
593 else
594 label.Printf("Label %d", i);
595
596 m_listCtrl->InsertItem(i, label, image);
597 }
598 else
599 {
600 m_listCtrl->InsertItem(i, image);
601 }
602 }
603 }
604
605 void MyFrame::OnIconView(wxCommandEvent& WXUNUSED(event))
606 {
607 RecreateList(wxLC_ICON, false);
608 }
609
610 void MyFrame::OnIconTextView(wxCommandEvent& WXUNUSED(event))
611 {
612 RecreateList(wxLC_ICON);
613 }
614
615 void MyFrame::OnSmallIconView(wxCommandEvent& WXUNUSED(event))
616 {
617 RecreateList(wxLC_SMALL_ICON, false);
618 }
619
620 void MyFrame::OnSmallIconTextView(wxCommandEvent& WXUNUSED(event))
621 {
622 RecreateList(wxLC_SMALL_ICON);
623 }
624
625 void MyFrame::OnVirtualView(wxCommandEvent& WXUNUSED(event))
626 {
627 m_smallVirtual = false;
628 RecreateList(wxLC_REPORT | wxLC_VIRTUAL);
629 }
630
631 void MyFrame::OnSmallVirtualView(wxCommandEvent& WXUNUSED(event))
632 {
633 m_smallVirtual = true;
634 RecreateList(wxLC_REPORT | wxLC_VIRTUAL);
635 }
636
637 void MyFrame::OnSetItemsCount(wxCommandEvent& WXUNUSED(event))
638 {
639 int numItems = wxGetNumberFromUser
640 (
641 "Enter the initial number of items for "
642 "the list and report views",
643 "Number of items:",
644 "wxWidgets wxListCtrl sample",
645 m_numListItems,
646 0,
647 10000,
648 this
649 );
650 if ( numItems == -1 || numItems == m_numListItems )
651 return;
652
653 m_numListItems = numItems;
654
655 if ( m_listCtrl->HasFlag(wxLC_REPORT) &&
656 !m_listCtrl->HasFlag(wxLC_VIRTUAL) )
657 RecreateList(wxLC_REPORT);
658 else if ( m_listCtrl->HasFlag(wxLC_LIST) )
659 RecreateList(wxLC_LIST);
660 }
661
662 void MyFrame::InitWithVirtualItems()
663 {
664 m_listCtrl->SetImageList(m_imageListSmall, wxIMAGE_LIST_SMALL);
665
666 if ( m_smallVirtual )
667 {
668 m_listCtrl->AppendColumn(wxT("Animal"));
669 m_listCtrl->AppendColumn(wxT("Sound"));
670 m_listCtrl->SetItemCount(WXSIZEOF(SMALL_VIRTUAL_VIEW_ITEMS));
671 }
672 else
673 {
674 m_listCtrl->AppendColumn(wxT("First Column"));
675 m_listCtrl->AppendColumn(wxT("Second Column"));
676 m_listCtrl->SetColumnWidth(0, 150);
677 m_listCtrl->SetColumnWidth(1, 150);
678 m_listCtrl->SetItemCount(1000000);
679 }
680 }
681
682 void MyFrame::OnSort(wxCommandEvent& WXUNUSED(event))
683 {
684 wxStopWatch sw;
685
686 m_listCtrl->SortItems(MyCompareFunction, 0);
687
688 m_logWindow->WriteText(wxString::Format(wxT("Sorting %d items took %ld ms\n"),
689 m_listCtrl->GetItemCount(),
690 sw.Time()));
691 }
692
693 void MyFrame::OnFind(wxCommandEvent& WXUNUSED(event))
694 {
695 wxStopWatch sw;
696
697 const int itemCount = m_listCtrl->GetItemCount();
698 for ( int i = 0; i < itemCount; i++ )
699 m_listCtrl->FindItem(-1, i);
700
701 wxLogMessage("Calling Find() for all %d items took %ld ms",
702 itemCount, sw.Time());
703 }
704
705 void MyFrame::OnShowSelInfo(wxCommandEvent& WXUNUSED(event))
706 {
707 int selCount = m_listCtrl->GetSelectedItemCount();
708 wxLogMessage(wxT("%d items selected:"), selCount);
709
710 // don't show too many items
711 size_t shownCount = 0;
712
713 long item = m_listCtrl->GetNextItem(-1, wxLIST_NEXT_ALL,
714 wxLIST_STATE_SELECTED);
715 while ( item != -1 )
716 {
717 wxLogMessage(wxT("\t%ld (%s)"),
718 item, m_listCtrl->GetItemText(item).c_str());
719
720 if ( ++shownCount > 10 )
721 {
722 wxLogMessage(wxT("\t... more selected items snipped..."));
723 break;
724 }
725
726 item = m_listCtrl->GetNextItem(item, wxLIST_NEXT_ALL,
727 wxLIST_STATE_SELECTED);
728 }
729 }
730
731 void MyFrame::OnShowViewRect(wxCommandEvent& WXUNUSED(event))
732 {
733 const wxRect r = m_listCtrl->GetViewRect();
734 wxLogMessage("View rect: (%d, %d)-(%d, %d)",
735 r.GetLeft(), r.GetTop(), r.GetRight(), r.GetBottom());
736 }
737
738 // ----------------------------------------------------------------------------
739 // column order tests
740 // ----------------------------------------------------------------------------
741
742 #ifdef wxHAS_LISTCTRL_COLUMN_ORDER
743
744 static wxString DumpIntArray(const wxArrayInt& a)
745 {
746 wxString s("{ ");
747 const size_t count = a.size();
748 for ( size_t n = 0; n < count; n++ )
749 {
750 if ( n )
751 s += ", ";
752 s += wxString::Format("%lu", (unsigned long)a[n]);
753 }
754
755 s += " }";
756
757 return s;
758 }
759
760 void MyFrame::OnSetColOrder(wxCommandEvent& WXUNUSED(event))
761 {
762 wxArrayInt order(3);
763 order[0] = 2;
764 order[1] = 0;
765 order[2] = 1;
766 if ( m_listCtrl->SetColumnsOrder(order) )
767 {
768 wxLogMessage("Column order set to %s", DumpIntArray(order));
769 }
770 }
771
772 void MyFrame::OnGetColOrder(wxCommandEvent& WXUNUSED(event))
773 {
774 // show what GetColumnsOrder() returns
775 const wxArrayInt order = m_listCtrl->GetColumnsOrder();
776 wxString msg = "Columns order: " +
777 DumpIntArray(m_listCtrl->GetColumnsOrder()) + "\n";
778
779 int n;
780 const int count = m_listCtrl->GetColumnCount();
781
782 // show the results of GetColumnOrder() for each column
783 msg += "GetColumnOrder() results:\n";
784 for ( n = 0; n < count; n++ )
785 {
786 msg += wxString::Format(" %2d -> %2d\n",
787 n, m_listCtrl->GetColumnOrder(n));
788 }
789
790 // and the results of GetColumnIndexFromOrder() too
791 msg += "GetColumnIndexFromOrder() results:\n";
792 for ( n = 0; n < count; n++ )
793 {
794 msg += wxString::Format(" %2d -> %2d\n",
795 n, m_listCtrl->GetColumnIndexFromOrder(n));
796 }
797
798 wxLogMessage("%s", msg);
799 }
800
801 #endif // wxHAS_LISTCTRL_COLUMN_ORDER
802
803 void MyFrame::OnShowColInfo(wxCommandEvent& WXUNUSED(event))
804 {
805 int count = m_listCtrl->GetColumnCount();
806 wxLogMessage(wxT("%d columns:"), count);
807 for ( int c = 0; c < count; c++ )
808 {
809 wxLogMessage(wxT("\tcolumn %d has width %d"), c,
810 m_listCtrl->GetColumnWidth(c));
811 }
812 }
813
814 void MyFrame::OnUpdateUIEnableInReport(wxUpdateUIEvent& event)
815 {
816 event.Enable( (m_listCtrl->GetWindowStyleFlag() & wxLC_REPORT) != 0 );
817 }
818
819 void MyFrame::OnToggleMultiSel(wxCommandEvent& WXUNUSED(event))
820 {
821 long flags = m_listCtrl->GetWindowStyleFlag();
822 if ( flags & wxLC_SINGLE_SEL )
823 flags &= ~wxLC_SINGLE_SEL;
824 else
825 flags |= wxLC_SINGLE_SEL;
826
827 m_logWindow->WriteText(wxString::Format(wxT("Current selection mode: %sle\n"),
828 (flags & wxLC_SINGLE_SEL) ? wxT("sing") : wxT("multip")));
829
830 RecreateList(flags);
831 }
832
833 void MyFrame::OnUpdateToggleMultiSel(wxUpdateUIEvent& event)
834 {
835 event.Check(!m_listCtrl->HasFlag(wxLC_SINGLE_SEL));
836 }
837
838 void MyFrame::OnUpdateToggleHeader(wxUpdateUIEvent& event)
839 {
840 event.Check(!m_listCtrl->HasFlag(wxLC_NO_HEADER));
841 }
842
843 void MyFrame::OnUpdateRowLines(wxUpdateUIEvent& event)
844 {
845 event.Enable(m_listCtrl->HasFlag(wxLC_VIRTUAL));
846 }
847
848 void MyFrame::OnSetFgColour(wxCommandEvent& WXUNUSED(event))
849 {
850 m_listCtrl->SetForegroundColour(wxGetColourFromUser(this));
851 m_listCtrl->Refresh();
852 }
853
854 void MyFrame::OnSetBgColour(wxCommandEvent& WXUNUSED(event))
855 {
856 m_listCtrl->SetBackgroundColour(wxGetColourFromUser(this));
857 m_listCtrl->Refresh();
858 }
859
860 void MyFrame::OnSetRowLines(wxCommandEvent& event)
861 {
862 m_listCtrl->EnableAlternateRowColours(event.IsChecked());
863 m_listCtrl->Refresh();
864 }
865
866 void MyFrame::OnAdd(wxCommandEvent& WXUNUSED(event))
867 {
868 m_listCtrl->InsertItem(m_listCtrl->GetItemCount(), wxT("Appended item"));
869 }
870
871 void MyFrame::OnEdit(wxCommandEvent& WXUNUSED(event))
872 {
873 // demonstrate cancelling editing: this currently is wxMSW-only
874 #if defined(__WXMSW__) && !defined(__WXUNIVERSAL__)
875 if ( m_listCtrl->GetEditControl() )
876 {
877 m_listCtrl->EndEditLabel(true);
878 }
879 else // start editing
880 #endif // __WXMSW__
881 {
882 long itemCur = m_listCtrl->GetNextItem(-1, wxLIST_NEXT_ALL,
883 wxLIST_STATE_FOCUSED);
884
885 if ( itemCur != -1 )
886 {
887 m_listCtrl->EditLabel(itemCur);
888 }
889 else
890 {
891 m_logWindow->WriteText(wxT("No item to edit"));
892 }
893 }
894 }
895
896 void MyFrame::OnDelete(wxCommandEvent& WXUNUSED(event))
897 {
898 if ( m_listCtrl->GetItemCount() )
899 {
900 m_listCtrl->DeleteItem(0);
901 }
902 else
903 {
904 m_logWindow->WriteText(wxT("Nothing to delete"));
905 }
906 }
907
908 void MyFrame::OnDeleteAll(wxCommandEvent& WXUNUSED(event))
909 {
910 wxStopWatch sw;
911
912 int itemCount = m_listCtrl->GetItemCount();
913
914 m_listCtrl->DeleteAllItems();
915
916 m_logWindow->WriteText(wxString::Format(wxT("Deleting %d items took %ld ms\n"),
917 itemCount,
918 sw.Time()));
919 }
920
921
922 // ----------------------------------------------------------------------------
923 // MyListCtrl
924 // ----------------------------------------------------------------------------
925
926 BEGIN_EVENT_TABLE(MyListCtrl, wxListCtrl)
927 EVT_LIST_BEGIN_DRAG(LIST_CTRL, MyListCtrl::OnBeginDrag)
928 EVT_LIST_BEGIN_RDRAG(LIST_CTRL, MyListCtrl::OnBeginRDrag)
929 EVT_LIST_BEGIN_LABEL_EDIT(LIST_CTRL, MyListCtrl::OnBeginLabelEdit)
930 EVT_LIST_END_LABEL_EDIT(LIST_CTRL, MyListCtrl::OnEndLabelEdit)
931 EVT_LIST_DELETE_ITEM(LIST_CTRL, MyListCtrl::OnDeleteItem)
932 EVT_LIST_DELETE_ALL_ITEMS(LIST_CTRL, MyListCtrl::OnDeleteAllItems)
933 EVT_LIST_ITEM_SELECTED(LIST_CTRL, MyListCtrl::OnSelected)
934 EVT_LIST_ITEM_DESELECTED(LIST_CTRL, MyListCtrl::OnDeselected)
935 EVT_LIST_KEY_DOWN(LIST_CTRL, MyListCtrl::OnListKeyDown)
936 EVT_LIST_ITEM_ACTIVATED(LIST_CTRL, MyListCtrl::OnActivated)
937 EVT_LIST_ITEM_FOCUSED(LIST_CTRL, MyListCtrl::OnFocused)
938
939 EVT_LIST_COL_CLICK(LIST_CTRL, MyListCtrl::OnColClick)
940 EVT_LIST_COL_RIGHT_CLICK(LIST_CTRL, MyListCtrl::OnColRightClick)
941 EVT_LIST_COL_BEGIN_DRAG(LIST_CTRL, MyListCtrl::OnColBeginDrag)
942 EVT_LIST_COL_DRAGGING(LIST_CTRL, MyListCtrl::OnColDragging)
943 EVT_LIST_COL_END_DRAG(LIST_CTRL, MyListCtrl::OnColEndDrag)
944
945 EVT_LIST_CACHE_HINT(LIST_CTRL, MyListCtrl::OnCacheHint)
946
947 #if USE_CONTEXT_MENU
948 EVT_CONTEXT_MENU(MyListCtrl::OnContextMenu)
949 #endif
950 EVT_CHAR(MyListCtrl::OnChar)
951
952 EVT_RIGHT_DOWN(MyListCtrl::OnRightClick)
953 END_EVENT_TABLE()
954
955 void MyListCtrl::OnCacheHint(wxListEvent& event)
956 {
957 wxLogMessage( wxT("OnCacheHint: cache items %ld..%ld"),
958 event.GetCacheFrom(), event.GetCacheTo() );
959 }
960
961 void MyListCtrl::SetColumnImage(int col, int image)
962 {
963 wxListItem item;
964 item.SetMask(wxLIST_MASK_IMAGE);
965 item.SetImage(image);
966 SetColumn(col, item);
967 }
968
969 void MyListCtrl::OnColClick(wxListEvent& event)
970 {
971 int col = event.GetColumn();
972
973 // set or unset image
974 static bool x = false;
975 x = !x;
976 SetColumnImage(col, x ? 0 : -1);
977
978 wxLogMessage( wxT("OnColumnClick at %d."), col );
979 }
980
981 void MyListCtrl::OnColRightClick(wxListEvent& event)
982 {
983 int col = event.GetColumn();
984 if ( col != -1 )
985 {
986 SetColumnImage(col, -1);
987 }
988
989 // Show popupmenu at position
990 wxMenu menu(wxT("Test"));
991 menu.Append(LIST_ABOUT, wxT("&About"));
992 PopupMenu(&menu, event.GetPoint());
993
994 wxLogMessage( wxT("OnColumnRightClick at %d."), event.GetColumn() );
995 }
996
997 void MyListCtrl::LogColEvent(const wxListEvent& event, const wxChar *name)
998 {
999 const int col = event.GetColumn();
1000
1001 wxLogMessage(wxT("%s: column %d (width = %d or %d)."),
1002 name,
1003 col,
1004 event.GetItem().GetWidth(),
1005 GetColumnWidth(col));
1006 }
1007
1008 void MyListCtrl::OnColBeginDrag(wxListEvent& event)
1009 {
1010 LogColEvent( event, wxT("OnColBeginDrag") );
1011
1012 if ( event.GetColumn() == 0 )
1013 {
1014 wxLogMessage(wxT("Resizing this column shouldn't work."));
1015
1016 event.Veto();
1017 }
1018 }
1019
1020 void MyListCtrl::OnColDragging(wxListEvent& event)
1021 {
1022 LogColEvent( event, wxT("OnColDragging") );
1023 }
1024
1025 void MyListCtrl::OnColEndDrag(wxListEvent& event)
1026 {
1027 LogColEvent( event, wxT("OnColEndDrag") );
1028 }
1029
1030 void MyListCtrl::OnBeginDrag(wxListEvent& event)
1031 {
1032 const wxPoint& pt = event.m_pointDrag;
1033
1034 int flags;
1035 wxLogMessage( wxT("OnBeginDrag at (%d, %d), item %ld."),
1036 pt.x, pt.y, HitTest(pt, flags) );
1037 }
1038
1039 void MyListCtrl::OnBeginRDrag(wxListEvent& event)
1040 {
1041 wxLogMessage( wxT("OnBeginRDrag at %d,%d."),
1042 event.m_pointDrag.x, event.m_pointDrag.y );
1043 }
1044
1045 void MyListCtrl::OnBeginLabelEdit(wxListEvent& event)
1046 {
1047 wxLogMessage( wxT("OnBeginLabelEdit: %s"), event.m_item.m_text.c_str());
1048
1049 wxTextCtrl * const text = GetEditControl();
1050 if ( !text )
1051 {
1052 wxLogMessage("BUG: started to edit but no edit control");
1053 }
1054 else
1055 {
1056 wxLogMessage("Edit control value: \"%s\"", text->GetValue());
1057 }
1058 }
1059
1060 void MyListCtrl::OnEndLabelEdit(wxListEvent& event)
1061 {
1062 wxLogMessage( wxT("OnEndLabelEdit: %s"),
1063 (
1064 event.IsEditCancelled() ?
1065 wxString("[cancelled]") :
1066 event.m_item.m_text
1067 ).c_str()
1068 );
1069 }
1070
1071 void MyListCtrl::OnDeleteItem(wxListEvent& event)
1072 {
1073 LogEvent(event, wxT("OnDeleteItem"));
1074 wxLogMessage( wxT("Number of items when delete event is sent: %d"), GetItemCount() );
1075 }
1076
1077 void MyListCtrl::OnDeleteAllItems(wxListEvent& event)
1078 {
1079 LogEvent(event, wxT("OnDeleteAllItems"));
1080 }
1081
1082 void MyListCtrl::OnSelected(wxListEvent& event)
1083 {
1084 LogEvent(event, wxT("OnSelected"));
1085
1086 if ( GetWindowStyle() & wxLC_REPORT )
1087 {
1088 wxListItem info;
1089 info.m_itemId = event.m_itemIndex;
1090 info.m_col = 1;
1091 info.m_mask = wxLIST_MASK_TEXT;
1092 if ( GetItem(info) )
1093 {
1094 wxLogMessage(wxT("Value of the 2nd field of the selected item: %s"),
1095 info.m_text.c_str());
1096 }
1097 else
1098 {
1099 wxFAIL_MSG(wxT("wxListCtrl::GetItem() failed"));
1100 }
1101 }
1102 }
1103
1104 void MyListCtrl::OnDeselected(wxListEvent& event)
1105 {
1106 LogEvent(event, wxT("OnDeselected"));
1107 }
1108
1109 void MyListCtrl::OnActivated(wxListEvent& event)
1110 {
1111 LogEvent(event, wxT("OnActivated"));
1112 }
1113
1114 void MyListCtrl::OnFocused(wxListEvent& event)
1115 {
1116 LogEvent(event, wxT("OnFocused"));
1117
1118 event.Skip();
1119 }
1120
1121 void MyListCtrl::OnListKeyDown(wxListEvent& event)
1122 {
1123 long item;
1124
1125 if ( !wxGetKeyState(WXK_SHIFT) )
1126 {
1127 LogEvent(event, wxT("OnListKeyDown"));
1128 event.Skip();
1129 return;
1130 }
1131
1132 switch ( event.GetKeyCode() )
1133 {
1134 case 'C': // colorize
1135 {
1136 wxListItem info;
1137 info.m_itemId = event.GetIndex();
1138 if ( info.m_itemId == -1 )
1139 {
1140 // no item
1141 break;
1142 }
1143
1144 GetItem(info);
1145
1146 wxListItemAttr *attr = info.GetAttributes();
1147 if ( !attr || !attr->HasTextColour() )
1148 {
1149 info.SetTextColour(*wxCYAN);
1150
1151 SetItem(info);
1152
1153 RefreshItem(info.m_itemId);
1154 }
1155 }
1156 break;
1157
1158 case 'N': // next
1159 item = GetNextItem(-1, wxLIST_NEXT_ALL, wxLIST_STATE_FOCUSED);
1160 if ( item++ == GetItemCount() - 1 )
1161 {
1162 item = 0;
1163 }
1164
1165 wxLogMessage(wxT("Focusing item %ld"), item);
1166
1167 SetItemState(item, wxLIST_STATE_FOCUSED, wxLIST_STATE_FOCUSED);
1168 EnsureVisible(item);
1169 break;
1170
1171 case 'R': // show bounding rectangle
1172 {
1173 item = event.GetIndex();
1174 wxRect r;
1175 if ( !GetItemRect(item, r) )
1176 {
1177 wxLogError(wxT("Failed to retrieve rect of item %ld"), item);
1178 break;
1179 }
1180
1181 wxLogMessage(wxT("Bounding rect of item %ld is (%d, %d)-(%d, %d)"),
1182 item, r.x, r.y, r.x + r.width, r.y + r.height);
1183 }
1184 break;
1185
1186 case '1': // show sub item bounding rectangle for the given column
1187 case '2': // (and icon/label rectangle if Shift/Ctrl is pressed)
1188 case '3':
1189 case '4': // this column is invalid but we want to test it too
1190 if ( InReportView() )
1191 {
1192 int subItem = event.GetKeyCode() - '1';
1193 item = event.GetIndex();
1194 wxRect r;
1195
1196 int code = wxLIST_RECT_BOUNDS;
1197 if ( wxGetKeyState(WXK_SHIFT) )
1198 code = wxLIST_RECT_ICON;
1199 else if ( wxGetKeyState(WXK_CONTROL) )
1200 code = wxLIST_RECT_LABEL;
1201
1202 if ( !GetSubItemRect(item, subItem, r, code) )
1203 {
1204 wxLogError(wxT("Failed to retrieve rect of item %ld column %d"), item, subItem + 1);
1205 break;
1206 }
1207
1208 wxLogMessage(wxT("Bounding rect of item %ld column %d is (%d, %d)-(%d, %d)"),
1209 item, subItem + 1,
1210 r.x, r.y, r.x + r.width, r.y + r.height);
1211 }
1212 break;
1213
1214 case 'U': // update
1215 if ( !IsVirtual() )
1216 break;
1217
1218 if ( m_updated != -1 )
1219 RefreshItem(m_updated);
1220
1221 m_updated = event.GetIndex();
1222 if ( m_updated != -1 )
1223 {
1224 // we won't see changes to this item as it's selected, update
1225 // the next one (or the first one if we're on the last item)
1226 if ( ++m_updated == GetItemCount() )
1227 m_updated = 0;
1228
1229 wxLogMessage("Updating colour of the item %ld", m_updated);
1230 RefreshItem(m_updated);
1231 }
1232 break;
1233
1234 case 'D': // delete
1235 item = GetNextItem(-1, wxLIST_NEXT_ALL, wxLIST_STATE_SELECTED);
1236 while ( item != -1 )
1237 {
1238 DeleteItem(item);
1239
1240 wxLogMessage(wxT("Item %ld deleted"), item);
1241
1242 // -1 because the indices were shifted by DeleteItem()
1243 item = GetNextItem(item - 1,
1244 wxLIST_NEXT_ALL, wxLIST_STATE_SELECTED);
1245 }
1246 break;
1247
1248 case 'I': // insert
1249 if ( GetWindowStyle() & wxLC_REPORT )
1250 {
1251 if ( GetWindowStyle() & wxLC_VIRTUAL )
1252 {
1253 SetItemCount(GetItemCount() + 1);
1254 }
1255 else // !virtual
1256 {
1257 InsertItemInReportView(event.GetIndex());
1258 }
1259 }
1260 //else: fall through
1261
1262 default:
1263 LogEvent(event, wxT("OnListKeyDown"));
1264
1265 event.Skip();
1266 }
1267 }
1268
1269 void MyListCtrl::OnChar(wxKeyEvent& event)
1270 {
1271 wxLogMessage(wxT("Got char event."));
1272
1273 event.Skip();
1274 }
1275
1276 void MyListCtrl::OnRightClick(wxMouseEvent& event)
1277 {
1278 if ( !event.ControlDown() )
1279 {
1280 event.Skip();
1281 return;
1282 }
1283
1284 int flags;
1285 long subitem;
1286 long item = HitTest(event.GetPosition(), flags, &subitem);
1287
1288 wxString where;
1289 switch ( flags )
1290 {
1291 case wxLIST_HITTEST_ABOVE: where = wxT("above"); break;
1292 case wxLIST_HITTEST_BELOW: where = wxT("below"); break;
1293 case wxLIST_HITTEST_NOWHERE: where = wxT("nowhere near"); break;
1294 case wxLIST_HITTEST_ONITEMICON: where = wxT("on icon of"); break;
1295 case wxLIST_HITTEST_ONITEMLABEL: where = wxT("on label of"); break;
1296 case wxLIST_HITTEST_ONITEMRIGHT: where = wxT("right on"); break;
1297 case wxLIST_HITTEST_TOLEFT: where = wxT("to the left of"); break;
1298 case wxLIST_HITTEST_TORIGHT: where = wxT("to the right of"); break;
1299 default: where = wxT("not clear exactly where on"); break;
1300 }
1301
1302 wxLogMessage(wxT("Right double click %s item %ld, subitem %ld"),
1303 where.c_str(), item, subitem);
1304 }
1305
1306 void MyListCtrl::LogEvent(const wxListEvent& event, const wxChar *eventName)
1307 {
1308 wxLogMessage(wxT("Item %ld: %s (item text = %s, data = %ld)"),
1309 event.GetIndex(), eventName,
1310 event.GetText().c_str(), event.GetData());
1311 }
1312
1313 wxString MyListCtrl::OnGetItemText(long item, long column) const
1314 {
1315 if ( GetItemCount() == WXSIZEOF(SMALL_VIRTUAL_VIEW_ITEMS) )
1316 {
1317 return SMALL_VIRTUAL_VIEW_ITEMS[item][column];
1318 }
1319 else // "big" virtual control
1320 {
1321 return wxString::Format(wxT("Column %ld of item %ld"), column, item);
1322 }
1323 }
1324
1325 int MyListCtrl::OnGetItemColumnImage(long item, long column) const
1326 {
1327 if (!column)
1328 return 0;
1329
1330 if (!(item % 3) && column == 1)
1331 return 0;
1332
1333 return -1;
1334 }
1335
1336 wxListItemAttr *MyListCtrl::OnGetItemAttr(long item) const
1337 {
1338 // test to check that RefreshItem() works correctly: when m_updated is
1339 // set to some item and it is refreshed, we highlight the item
1340 if ( item == m_updated )
1341 {
1342 static wxListItemAttr s_attrHighlight(*wxRED, wxNullColour, wxNullFont);
1343 return &s_attrHighlight;
1344 }
1345
1346 return wxListCtrl::OnGetItemAttr(item);
1347 }
1348
1349 void MyListCtrl::InsertItemInReportView(int i)
1350 {
1351 wxString buf;
1352 buf.Printf(wxT("This is item %d"), i);
1353 long tmp = InsertItem(i, buf, 0);
1354 SetItemData(tmp, i);
1355
1356 buf.Printf(wxT("Col 1, item %d"), i);
1357 SetItem(tmp, 1, buf);
1358
1359 buf.Printf(wxT("Item %d in column 2"), i);
1360 SetItem(tmp, 2, buf);
1361 }
1362
1363 #if USE_CONTEXT_MENU
1364 void MyListCtrl::OnContextMenu(wxContextMenuEvent& event)
1365 {
1366 if (GetEditControl() == NULL)
1367 {
1368 wxPoint point = event.GetPosition();
1369 // If from keyboard
1370 if ( (point.x == -1) && (point.y == -1) )
1371 {
1372 wxSize size = GetSize();
1373 point.x = size.x / 2;
1374 point.y = size.y / 2;
1375 }
1376 else
1377 {
1378 point = ScreenToClient(point);
1379 }
1380 ShowContextMenu(point);
1381 }
1382 else
1383 {
1384 // the user is editing:
1385 // allow the text control to display its context menu
1386 // if it has one (it has on Windows) rather than display our one
1387 event.Skip();
1388 }
1389 }
1390 #endif
1391
1392 void MyListCtrl::ShowContextMenu(const wxPoint& pos)
1393 {
1394 wxMenu menu;
1395
1396 menu.Append(wxID_ABOUT, wxT("&About"));
1397 menu.AppendSeparator();
1398 menu.Append(wxID_EXIT, wxT("E&xit"));
1399
1400 PopupMenu(&menu, pos.x, pos.y);
1401 }