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