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