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