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