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