]> git.saurik.com Git - wxWidgets.git/blob - samples/listctrl/listtest.cpp
Better fix for 'int i' used twice in the same block.
[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 #ifdef __GNUG__
13 #pragma implementation
14 #pragma interface
15 #endif
16
17 // For compilers that support precompilation, includes "wx/wx.h".
18 #include "wx/wxprec.h"
19
20 #ifdef __BORLANDC__
21 #pragma hdrstop
22 #endif
23
24 #ifndef WX_PRECOMP
25 #include "wx/wx.h"
26 #endif
27
28 #if !defined(__WXMSW__) && !defined(__WXPM__)
29 #include "mondrian.xpm"
30 #endif
31
32 #ifndef __WXMSW__
33 #include "bitmaps/toolbrai.xpm"
34 #include "bitmaps/toolchar.xpm"
35 #include "bitmaps/tooldata.xpm"
36 #include "bitmaps/toolnote.xpm"
37 #include "bitmaps/tooltodo.xpm"
38 #include "bitmaps/toolchec.xpm"
39 #include "bitmaps/toolgame.xpm"
40 #include "bitmaps/tooltime.xpm"
41 #include "bitmaps/toolword.xpm"
42 #include "bitmaps/small1.xpm"
43 #endif
44
45 #include "wx/imaglist.h"
46 #include "wx/listctrl.h"
47 #include "wx/timer.h" // for wxStopWatch
48 #include "wx/colordlg.h" // for wxGetColourFromUser
49
50 #include "listtest.h"
51
52 const wxChar *SMALL_VIRTUAL_VIEW_ITEMS[][2] =
53 {
54 { _T("Cat"), _T("meow") },
55 { _T("Cow"), _T("moo") },
56 { _T("Crow"), _T("caw") },
57 { _T("Dog"), _T("woof") },
58 { _T("Duck"), _T("quack") },
59 { _T("Mouse"), _T("squeak") },
60 { _T("Owl"), _T("hoo") },
61 { _T("Pig"), _T("oink") },
62 { _T("Pigeon"), _T("coo") },
63 { _T("Sheep"), _T("baaah") },
64 };
65
66
67 BEGIN_EVENT_TABLE(MyFrame, wxFrame)
68 EVT_SIZE(MyFrame::OnSize)
69
70 EVT_MENU(LIST_QUIT, MyFrame::OnQuit)
71 EVT_MENU(LIST_ABOUT, MyFrame::OnAbout)
72 EVT_MENU(LIST_LIST_VIEW, MyFrame::OnListView)
73 EVT_MENU(LIST_REPORT_VIEW, MyFrame::OnReportView)
74 EVT_MENU(LIST_ICON_VIEW, MyFrame::OnIconView)
75 EVT_MENU(LIST_ICON_TEXT_VIEW, MyFrame::OnIconTextView)
76 EVT_MENU(LIST_SMALL_ICON_VIEW, MyFrame::OnSmallIconView)
77 EVT_MENU(LIST_SMALL_ICON_TEXT_VIEW, MyFrame::OnSmallIconTextView)
78 EVT_MENU(LIST_VIRTUAL_VIEW, MyFrame::OnVirtualView)
79 EVT_MENU(LIST_SMALL_VIRTUAL_VIEW, MyFrame::OnSmallVirtualView)
80
81 EVT_MENU(LIST_FOCUS_LAST, MyFrame::OnFocusLast)
82 EVT_MENU(LIST_TOGGLE_FIRST, MyFrame::OnToggleFirstSel)
83 EVT_MENU(LIST_DESELECT_ALL, MyFrame::OnDeselectAll)
84 EVT_MENU(LIST_SELECT_ALL, MyFrame::OnSelectAll)
85 EVT_MENU(LIST_DELETE, MyFrame::OnDelete)
86 EVT_MENU(LIST_ADD, MyFrame::OnAdd)
87 EVT_MENU(LIST_EDIT, MyFrame::OnEdit)
88 EVT_MENU(LIST_DELETE_ALL, MyFrame::OnDeleteAll)
89 EVT_MENU(LIST_SORT, MyFrame::OnSort)
90 EVT_MENU(LIST_SET_FG_COL, MyFrame::OnSetFgColour)
91 EVT_MENU(LIST_SET_BG_COL, MyFrame::OnSetBgColour)
92 EVT_MENU(LIST_TOGGLE_MULTI_SEL, MyFrame::OnToggleMultiSel)
93 EVT_MENU(LIST_SHOW_COL_INFO, MyFrame::OnShowColInfo)
94 EVT_MENU(LIST_SHOW_SEL_INFO, MyFrame::OnShowSelInfo)
95 EVT_MENU(LIST_FREEZE, MyFrame::OnFreeze)
96 EVT_MENU(LIST_THAW, MyFrame::OnThaw)
97 EVT_MENU(LIST_TOGGLE_LINES, MyFrame::OnToggleLines)
98
99 EVT_UPDATE_UI(LIST_SHOW_COL_INFO, MyFrame::OnUpdateShowColInfo)
100 EVT_UPDATE_UI(LIST_TOGGLE_MULTI_SEL, MyFrame::OnUpdateToggleMultiSel)
101 END_EVENT_TABLE()
102
103 BEGIN_EVENT_TABLE(MyListCtrl, wxListCtrl)
104 EVT_LIST_BEGIN_DRAG(LIST_CTRL, MyListCtrl::OnBeginDrag)
105 EVT_LIST_BEGIN_RDRAG(LIST_CTRL, MyListCtrl::OnBeginRDrag)
106 EVT_LIST_BEGIN_LABEL_EDIT(LIST_CTRL, MyListCtrl::OnBeginLabelEdit)
107 EVT_LIST_END_LABEL_EDIT(LIST_CTRL, MyListCtrl::OnEndLabelEdit)
108 EVT_LIST_DELETE_ITEM(LIST_CTRL, MyListCtrl::OnDeleteItem)
109 EVT_LIST_DELETE_ALL_ITEMS(LIST_CTRL, MyListCtrl::OnDeleteAllItems)
110 #if WXWIN_COMPATIBILITY_2_4
111 EVT_LIST_GET_INFO(LIST_CTRL, MyListCtrl::OnGetInfo)
112 EVT_LIST_SET_INFO(LIST_CTRL, MyListCtrl::OnSetInfo)
113 #endif
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 EVT_CHAR(MyListCtrl::OnChar)
129 END_EVENT_TABLE()
130
131 IMPLEMENT_APP(MyApp)
132
133 // number of items in list/report view
134 static const int NUM_ITEMS = 30;
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 // Create the main frame window
154 MyFrame *frame = new MyFrame(wxT("wxListCtrl Test"), 50, 50, 450, 340);
155
156 // Show the frame
157 frame->Show(true);
158
159 SetTopWindow(frame);
160
161 return true;
162 }
163
164 // My frame constructor
165 MyFrame::MyFrame(const wxChar *title, int x, int y, int w, int h)
166 : wxFrame(NULL, wxID_ANY, title, wxPoint(x, y), wxSize(w, h))
167 {
168 m_listCtrl = NULL;
169 m_logWindow = NULL;
170 m_smallVirtual = false;
171
172 // Give it an icon
173 SetIcon( wxICON(mondrian) );
174
175 // Make an image list containing large icons
176 m_imageListNormal = new wxImageList(32, 32, true);
177 m_imageListSmall = new wxImageList(16, 16, true);
178
179 #ifdef __WXMSW__
180 m_imageListNormal->Add( wxIcon(_T("icon1"), wxBITMAP_TYPE_ICO_RESOURCE) );
181 m_imageListNormal->Add( wxIcon(_T("icon2"), wxBITMAP_TYPE_ICO_RESOURCE) );
182 m_imageListNormal->Add( wxIcon(_T("icon3"), wxBITMAP_TYPE_ICO_RESOURCE) );
183 m_imageListNormal->Add( wxIcon(_T("icon4"), wxBITMAP_TYPE_ICO_RESOURCE) );
184 m_imageListNormal->Add( wxIcon(_T("icon5"), wxBITMAP_TYPE_ICO_RESOURCE) );
185 m_imageListNormal->Add( wxIcon(_T("icon6"), wxBITMAP_TYPE_ICO_RESOURCE) );
186 m_imageListNormal->Add( wxIcon(_T("icon7"), wxBITMAP_TYPE_ICO_RESOURCE) );
187 m_imageListNormal->Add( wxIcon(_T("icon8"), wxBITMAP_TYPE_ICO_RESOURCE) );
188 m_imageListNormal->Add( wxIcon(_T("icon9"), wxBITMAP_TYPE_ICO_RESOURCE) );
189
190 m_imageListSmall->Add( wxIcon(_T("iconsmall"), wxBITMAP_TYPE_ICO_RESOURCE) );
191
192 #else
193 m_imageListNormal->Add( wxIcon( toolbrai_xpm ) );
194 m_imageListNormal->Add( wxIcon( toolchar_xpm ) );
195 m_imageListNormal->Add( wxIcon( tooldata_xpm ) );
196 m_imageListNormal->Add( wxIcon( toolnote_xpm ) );
197 m_imageListNormal->Add( wxIcon( tooltodo_xpm ) );
198 m_imageListNormal->Add( wxIcon( toolchec_xpm ) );
199 m_imageListNormal->Add( wxIcon( toolgame_xpm ) );
200 m_imageListNormal->Add( wxIcon( tooltime_xpm ) );
201 m_imageListNormal->Add( wxIcon( toolword_xpm ) );
202
203 m_imageListSmall->Add( wxIcon( small1_xpm) );
204 #endif
205
206 // Make a menubar
207 wxMenu *menuFile = new wxMenu;
208 menuFile->Append(LIST_ABOUT, _T("&About"));
209 menuFile->AppendSeparator();
210 menuFile->Append(LIST_QUIT, _T("E&xit\tAlt-X"));
211
212 wxMenu *menuView = new wxMenu;
213 menuView->Append(LIST_LIST_VIEW, _T("&List view\tF1"));
214 menuView->Append(LIST_REPORT_VIEW, _T("&Report view\tF2"));
215 menuView->Append(LIST_ICON_VIEW, _T("&Icon view\tF3"));
216 menuView->Append(LIST_ICON_TEXT_VIEW, _T("Icon view with &text\tF4"));
217 menuView->Append(LIST_SMALL_ICON_VIEW, _T("&Small icon view\tF5"));
218 menuView->Append(LIST_SMALL_ICON_TEXT_VIEW, _T("Small icon &view with text\tF6"));
219 menuView->Append(LIST_VIRTUAL_VIEW, _T("&Virtual view\tF7"));
220 menuView->Append(LIST_SMALL_VIRTUAL_VIEW, _T("Small virtual vie&w\tF8"));
221
222 wxMenu *menuList = new wxMenu;
223 menuList->Append(LIST_FOCUS_LAST, _T("&Make last item current\tCtrl-L"));
224 menuList->Append(LIST_TOGGLE_FIRST, _T("To&ggle first item\tCtrl-G"));
225 menuList->Append(LIST_DESELECT_ALL, _T("&Deselect All\tCtrl-D"));
226 menuList->Append(LIST_SELECT_ALL, _T("S&elect All\tCtrl-A"));
227 menuList->AppendSeparator();
228 menuList->Append(LIST_SHOW_COL_INFO, _T("Show &column info\tCtrl-C"));
229 menuList->Append(LIST_SHOW_SEL_INFO, _T("Show &selected items\tCtrl-S"));
230 menuList->AppendSeparator();
231 menuList->Append(LIST_SORT, _T("&Sort\tCtrl-S"));
232 menuList->AppendSeparator();
233 menuList->Append(LIST_ADD, _T("&Append an item\tCtrl-P"));
234 menuList->Append(LIST_EDIT, _T("&Edit the item\tCtrl-E"));
235 menuList->Append(LIST_DELETE, _T("&Delete first item\tCtrl-X"));
236 menuList->Append(LIST_DELETE_ALL, _T("Delete &all items"));
237 menuList->AppendSeparator();
238 menuList->Append(LIST_FREEZE, _T("Free&ze\tCtrl-Z"));
239 menuList->Append(LIST_THAW, _T("Tha&w\tCtrl-W"));
240 menuList->AppendSeparator();
241 menuList->AppendCheckItem(LIST_TOGGLE_LINES, _T("Toggle &lines\tCtrl-I"));
242 menuList->Append(LIST_TOGGLE_MULTI_SEL, _T("&Multiple selection\tCtrl-M"),
243 _T("Toggle multiple selection"), true);
244
245 wxMenu *menuCol = new wxMenu;
246 menuCol->Append(LIST_SET_FG_COL, _T("&Foreground colour..."));
247 menuCol->Append(LIST_SET_BG_COL, _T("&Background colour..."));
248
249 wxMenuBar *menubar = new wxMenuBar;
250 menubar->Append(menuFile, _T("&File"));
251 menubar->Append(menuView, _T("&View"));
252 menubar->Append(menuList, _T("&List"));
253 menubar->Append(menuCol, _T("&Colour"));
254 SetMenuBar(menubar);
255
256 m_panel = new wxPanel(this, wxID_ANY);
257 m_logWindow = new wxTextCtrl(m_panel, wxID_ANY, wxEmptyString,
258 wxDefaultPosition, wxDefaultSize,
259 wxTE_MULTILINE | wxSUNKEN_BORDER);
260
261 m_logOld = wxLog::SetActiveTarget(new wxLogTextCtrl(m_logWindow));
262
263 RecreateList(wxLC_REPORT | wxLC_SINGLE_SEL);
264
265 #if wxUSE_STATUSBAR
266 CreateStatusBar(3);
267 #endif // wxUSE_STATUSBAR
268 }
269
270 MyFrame::~MyFrame()
271 {
272 delete wxLog::SetActiveTarget(m_logOld);
273
274 delete m_imageListNormal;
275 delete m_imageListSmall;
276 }
277
278 void MyFrame::OnSize(wxSizeEvent& event)
279 {
280 DoSize();
281
282 event.Skip();
283 }
284
285 void MyFrame::DoSize()
286 {
287 if ( !m_logWindow )
288 return;
289
290 wxSize size = GetClientSize();
291 wxCoord y = (2*size.y)/3;
292 m_listCtrl->SetSize(0, 0, size.x, y);
293 m_logWindow->SetSize(0, y + 1, size.x, size.y - y);
294 }
295
296 bool MyFrame::CheckNonVirtual() const
297 {
298 if ( !m_listCtrl->HasFlag(wxLC_VIRTUAL) )
299 return true;
300
301 // "this" == whatever
302 wxLogWarning(_T("Can't do this in virtual view, sorry."));
303
304 return false;
305 }
306
307 void MyFrame::OnQuit(wxCommandEvent& WXUNUSED(event))
308 {
309 Close(true);
310 }
311
312 void MyFrame::OnAbout(wxCommandEvent& WXUNUSED(event))
313 {
314 wxMessageDialog dialog(this, _T("List test sample\nJulian Smart (c) 1997"),
315 _T("About list test"), wxOK|wxCANCEL);
316
317 dialog.ShowModal();
318 }
319
320 void MyFrame::OnFreeze(wxCommandEvent& WXUNUSED(event))
321 {
322 wxLogMessage(_T("Freezing the control"));
323
324 m_listCtrl->Freeze();
325 }
326
327 void MyFrame::OnThaw(wxCommandEvent& WXUNUSED(event))
328 {
329 wxLogMessage(_T("Thawing the control"));
330
331 m_listCtrl->Thaw();
332 }
333
334 void MyFrame::OnToggleLines(wxCommandEvent& event)
335 {
336 m_listCtrl->SetSingleStyle(wxLC_HRULES | wxLC_VRULES, event.IsChecked());
337 }
338
339 void MyFrame::OnFocusLast(wxCommandEvent& WXUNUSED(event))
340 {
341 long index = m_listCtrl->GetItemCount() - 1;
342 if ( index == -1 )
343 {
344 return;
345 }
346
347 m_listCtrl->SetItemState(index, wxLIST_STATE_FOCUSED, wxLIST_STATE_FOCUSED);
348 m_listCtrl->EnsureVisible(index);
349 }
350
351 void MyFrame::OnToggleFirstSel(wxCommandEvent& WXUNUSED(event))
352 {
353 m_listCtrl->SetItemState(0, (~m_listCtrl->GetItemState(0, wxLIST_STATE_SELECTED) ) & wxLIST_STATE_SELECTED, wxLIST_STATE_SELECTED);
354 }
355
356 void MyFrame::OnDeselectAll(wxCommandEvent& WXUNUSED(event))
357 {
358 if ( !CheckNonVirtual() )
359 return;
360
361 int n = m_listCtrl->GetItemCount();
362 for (int i = 0; i < n; i++)
363 m_listCtrl->SetItemState(i,0,wxLIST_STATE_SELECTED);
364 }
365
366 void MyFrame::OnSelectAll(wxCommandEvent& WXUNUSED(event))
367 {
368 if ( !CheckNonVirtual() )
369 return;
370
371 int n = m_listCtrl->GetItemCount();
372 for (int i = 0; i < n; i++)
373 m_listCtrl->SetItemState(i,wxLIST_STATE_SELECTED, wxLIST_STATE_SELECTED);
374 }
375
376 // ----------------------------------------------------------------------------
377 // changing listctrl modes
378 // ----------------------------------------------------------------------------
379
380 void MyFrame::RecreateList(long flags, bool withText)
381 {
382 // we could avoid recreating it if we don't set/clear the wxLC_VIRTUAL
383 // style, but it is more trouble to do it than not
384 #if 0
385 if ( !m_listCtrl || ((flags & wxLC_VIRTUAL) !=
386 (m_listCtrl->GetWindowStyleFlag() & wxLC_VIRTUAL)) )
387 #endif
388 {
389 delete m_listCtrl;
390
391 m_listCtrl = new MyListCtrl(m_panel, LIST_CTRL,
392 wxDefaultPosition, wxDefaultSize,
393 flags |
394 wxSUNKEN_BORDER | wxLC_EDIT_LABELS);
395
396 switch ( flags & wxLC_MASK_TYPE )
397 {
398 case wxLC_LIST:
399 InitWithListItems();
400 break;
401
402 case wxLC_ICON:
403 InitWithIconItems(withText);
404 break;
405
406 case wxLC_SMALL_ICON:
407 InitWithIconItems(withText, true);
408 break;
409
410 case wxLC_REPORT:
411 if ( flags & wxLC_VIRTUAL )
412 InitWithVirtualItems();
413 else
414 InitWithReportItems();
415 break;
416
417 default:
418 wxFAIL_MSG( _T("unknown listctrl mode") );
419 }
420 }
421
422 DoSize();
423
424 m_logWindow->Clear();
425 }
426
427 void MyFrame::OnListView(wxCommandEvent& WXUNUSED(event))
428 {
429 RecreateList(wxLC_LIST);
430 }
431
432 void MyFrame::InitWithListItems()
433 {
434 for ( int i = 0; i < NUM_ITEMS; i++ )
435 {
436 m_listCtrl->InsertItem(i, wxString::Format(_T("Item %d"), i));
437 }
438 }
439
440 void MyFrame::OnReportView(wxCommandEvent& WXUNUSED(event))
441 {
442 RecreateList(wxLC_REPORT);
443 }
444
445 void MyFrame::InitWithReportItems()
446 {
447 m_listCtrl->SetImageList(m_imageListSmall, wxIMAGE_LIST_SMALL);
448
449 // note that under MSW for SetColumnWidth() to work we need to create the
450 // items with images initially even if we specify dummy image id
451 wxListItem itemCol;
452 itemCol.SetText(_T("Column 1"));
453 itemCol.SetImage(-1);
454 m_listCtrl->InsertColumn(0, itemCol);
455
456 itemCol.SetText(_T("Column 2"));
457 itemCol.SetAlign(wxLIST_FORMAT_CENTRE);
458 m_listCtrl->InsertColumn(1, itemCol);
459
460 itemCol.SetText(_T("Column 3"));
461 itemCol.SetAlign(wxLIST_FORMAT_RIGHT);
462 m_listCtrl->InsertColumn(2, itemCol);
463
464 // to speed up inserting we hide the control temporarily
465 m_listCtrl->Hide();
466
467 wxStopWatch sw;
468
469 for ( int i = 0; i < NUM_ITEMS; i++ )
470 {
471 m_listCtrl->InsertItemInReportView(i);
472 }
473
474 m_logWindow->WriteText(wxString::Format(_T("%d items inserted in %ldms\n"),
475 NUM_ITEMS, sw.Time()));
476 m_listCtrl->Show();
477
478 // we leave all mask fields to 0 and only change the colour
479 wxListItem item;
480 item.m_itemId = 0;
481 item.SetTextColour(*wxRED);
482 m_listCtrl->SetItem( item );
483
484 item.m_itemId = 2;
485 item.SetTextColour(*wxGREEN);
486 m_listCtrl->SetItem( item );
487 item.m_itemId = 4;
488 item.SetTextColour(*wxLIGHT_GREY);
489 item.SetFont(*wxITALIC_FONT);
490 item.SetBackgroundColour(*wxRED);
491 m_listCtrl->SetItem( item );
492
493 m_listCtrl->SetTextColour(*wxBLUE);
494 m_listCtrl->SetBackgroundColour(*wxLIGHT_GREY);
495
496 m_listCtrl->SetColumnWidth( 0, wxLIST_AUTOSIZE );
497 m_listCtrl->SetColumnWidth( 1, wxLIST_AUTOSIZE );
498 m_listCtrl->SetColumnWidth( 2, wxLIST_AUTOSIZE );
499
500 // test SetItemFont too
501 m_listCtrl->SetItemFont(0, *wxITALIC_FONT);
502 }
503
504 void MyFrame::InitWithIconItems(bool withText, bool sameIcon)
505 {
506 m_listCtrl->SetImageList(m_imageListNormal, wxIMAGE_LIST_NORMAL);
507 m_listCtrl->SetImageList(m_imageListSmall, wxIMAGE_LIST_SMALL);
508
509 for ( int i = 0; i < NUM_ICONS; i++ )
510 {
511 int image = sameIcon ? 0 : i;
512
513 if ( withText )
514 {
515 m_listCtrl->InsertItem(i, wxString::Format(_T("Label %d"), i),
516 image);
517 }
518 else
519 {
520 m_listCtrl->InsertItem(i, image);
521 }
522 }
523 }
524
525 void MyFrame::OnIconView(wxCommandEvent& WXUNUSED(event))
526 {
527 RecreateList(wxLC_ICON, false);
528 }
529
530 void MyFrame::OnIconTextView(wxCommandEvent& WXUNUSED(event))
531 {
532 RecreateList(wxLC_ICON);
533 }
534
535 void MyFrame::OnSmallIconView(wxCommandEvent& WXUNUSED(event))
536 {
537 RecreateList(wxLC_SMALL_ICON, false);
538 }
539
540 void MyFrame::OnSmallIconTextView(wxCommandEvent& WXUNUSED(event))
541 {
542 RecreateList(wxLC_SMALL_ICON);
543 }
544
545 void MyFrame::OnVirtualView(wxCommandEvent& WXUNUSED(event))
546 {
547 m_smallVirtual = false;
548 RecreateList(wxLC_REPORT | wxLC_VIRTUAL);
549 }
550
551 void MyFrame::OnSmallVirtualView(wxCommandEvent& WXUNUSED(event))
552 {
553 m_smallVirtual = true;
554 RecreateList(wxLC_REPORT | wxLC_VIRTUAL);
555 }
556
557 void MyFrame::InitWithVirtualItems()
558 {
559 m_listCtrl->SetImageList(m_imageListSmall, wxIMAGE_LIST_SMALL);
560
561 if ( m_smallVirtual )
562 {
563 m_listCtrl->InsertColumn(0, _T("Animal"));
564 m_listCtrl->InsertColumn(1, _T("Sound"));
565 m_listCtrl->SetItemCount(WXSIZEOF(SMALL_VIRTUAL_VIEW_ITEMS));
566 }
567 else
568 {
569 m_listCtrl->InsertColumn(0, _T("First Column"));
570 m_listCtrl->InsertColumn(1, _T("Second Column"));
571 m_listCtrl->SetColumnWidth(0, 150);
572 m_listCtrl->SetColumnWidth(1, 150);
573 m_listCtrl->SetItemCount(1000000);
574 }
575 }
576
577 void MyFrame::OnSort(wxCommandEvent& WXUNUSED(event))
578 {
579 wxStopWatch sw;
580
581 m_listCtrl->SortItems(MyCompareFunction, 0);
582
583 m_logWindow->WriteText(wxString::Format(_T("Sorting %d items took %ld ms\n"),
584 m_listCtrl->GetItemCount(),
585 sw.Time()));
586 }
587
588 void MyFrame::OnShowSelInfo(wxCommandEvent& WXUNUSED(event))
589 {
590 int selCount = m_listCtrl->GetSelectedItemCount();
591 wxLogMessage(_T("%d items selected:"), selCount);
592
593 // don't show too many items
594 size_t shownCount = 0;
595
596 long item = m_listCtrl->GetNextItem(-1, wxLIST_NEXT_ALL,
597 wxLIST_STATE_SELECTED);
598 while ( item != -1 )
599 {
600 wxLogMessage(_T("\t%ld (%s)"),
601 item, m_listCtrl->GetItemText(item).c_str());
602
603 if ( ++shownCount > 10 )
604 {
605 wxLogMessage(_T("\t... more selected items snipped..."));
606 break;
607 }
608
609 item = m_listCtrl->GetNextItem(item, wxLIST_NEXT_ALL,
610 wxLIST_STATE_SELECTED);
611 }
612 }
613
614 void MyFrame::OnShowColInfo(wxCommandEvent& WXUNUSED(event))
615 {
616 int count = m_listCtrl->GetColumnCount();
617 wxLogMessage(wxT("%d columns:"), count);
618 for ( int c = 0; c < count; c++ )
619 {
620 wxLogMessage(wxT("\tcolumn %d has width %d"), c,
621 m_listCtrl->GetColumnWidth(c));
622 }
623 }
624
625 void MyFrame::OnUpdateShowColInfo(wxUpdateUIEvent& event)
626 {
627 event.Enable( (m_listCtrl->GetWindowStyleFlag() & wxLC_REPORT) != 0 );
628 }
629
630 void MyFrame::OnToggleMultiSel(wxCommandEvent& WXUNUSED(event))
631 {
632 long flags = m_listCtrl->GetWindowStyleFlag();
633 if ( flags & wxLC_SINGLE_SEL )
634 flags &= ~wxLC_SINGLE_SEL;
635 else
636 flags |= wxLC_SINGLE_SEL;
637
638 m_logWindow->WriteText(wxString::Format(wxT("Current selection mode: %sle\n"),
639 (flags & wxLC_SINGLE_SEL) ? _T("sing") : _T("multip")));
640
641 RecreateList(flags);
642 }
643
644 void MyFrame::OnUpdateToggleMultiSel(wxUpdateUIEvent& event)
645 {
646 event.Check((m_listCtrl->GetWindowStyleFlag() & wxLC_SINGLE_SEL) == 0);
647 }
648
649 void MyFrame::OnSetFgColour(wxCommandEvent& WXUNUSED(event))
650 {
651 m_listCtrl->SetForegroundColour(wxGetColourFromUser(this));
652 m_listCtrl->Refresh();
653 }
654
655 void MyFrame::OnSetBgColour(wxCommandEvent& WXUNUSED(event))
656 {
657 m_listCtrl->SetBackgroundColour(wxGetColourFromUser(this));
658 m_listCtrl->Refresh();
659 }
660
661 void MyFrame::OnAdd(wxCommandEvent& WXUNUSED(event))
662 {
663 m_listCtrl->InsertItem(m_listCtrl->GetItemCount(), _T("Appended item"));
664 }
665
666 void MyFrame::OnEdit(wxCommandEvent& WXUNUSED(event))
667 {
668 long itemCur = m_listCtrl->GetNextItem(-1, wxLIST_NEXT_ALL,
669 wxLIST_STATE_FOCUSED);
670
671 if ( itemCur != -1 )
672 {
673 m_listCtrl->EditLabel(itemCur);
674 }
675 else
676 {
677 m_logWindow->WriteText(_T("No item to edit"));
678 }
679 }
680
681 void MyFrame::OnDelete(wxCommandEvent& WXUNUSED(event))
682 {
683 if ( m_listCtrl->GetItemCount() )
684 {
685 m_listCtrl->DeleteItem(0);
686 }
687 else
688 {
689 m_logWindow->WriteText(_T("Nothing to delete"));
690 }
691 }
692
693 void MyFrame::OnDeleteAll(wxCommandEvent& WXUNUSED(event))
694 {
695 wxStopWatch sw;
696
697 size_t itemCount = m_listCtrl->GetItemCount();
698
699 m_listCtrl->DeleteAllItems();
700
701 m_logWindow->WriteText(wxString::Format(_T("Deleting %d items took %ld ms\n"),
702 itemCount,
703 sw.Time()));
704 }
705
706 // MyListCtrl
707
708 void MyListCtrl::OnCacheHint(wxListEvent& event)
709 {
710 wxLogMessage( wxT("OnCacheHint: cache items %ld..%ld"),
711 event.GetCacheFrom(), event.GetCacheTo() );
712 }
713
714 void MyListCtrl::SetColumnImage(int col, int image)
715 {
716 wxListItem item;
717 item.SetMask(wxLIST_MASK_IMAGE);
718 item.SetImage(image);
719 SetColumn(col, item);
720 }
721
722 void MyListCtrl::OnColClick(wxListEvent& event)
723 {
724 int col = event.GetColumn();
725
726 // set or unset image
727 static bool x = false;
728 x = !x;
729 SetColumnImage(col, x ? 0 : -1);
730
731 wxLogMessage( wxT("OnColumnClick at %d."), col );
732 }
733
734 void MyListCtrl::OnColRightClick(wxListEvent& event)
735 {
736 int col = event.GetColumn();
737 if ( col != -1 )
738 {
739 SetColumnImage(col, -1);
740 }
741
742 // Show popupmenu at position
743 wxMenu menu(wxT("Test"));
744 menu.Append(LIST_ABOUT, _T("&About"));
745 PopupMenu(&menu, event.GetPoint());
746
747 wxLogMessage( wxT("OnColumnRightClick at %d."), event.GetColumn() );
748 }
749
750 void MyListCtrl::LogColEvent(const wxListEvent& event, const wxChar *name)
751 {
752 const int col = event.GetColumn();
753
754 wxLogMessage(wxT("%s: column %d (width = %d or %d)."),
755 name,
756 col,
757 event.GetItem().GetWidth(),
758 GetColumnWidth(col));
759 }
760
761 void MyListCtrl::OnColBeginDrag(wxListEvent& event)
762 {
763 LogColEvent( event, wxT("OnColBeginDrag") );
764
765 if ( event.GetColumn() == 0 )
766 {
767 wxLogMessage(_T("Resizing this column shouldn't work."));
768
769 event.Veto();
770 }
771 }
772
773 void MyListCtrl::OnColDragging(wxListEvent& event)
774 {
775 LogColEvent( event, wxT("OnColDragging") );
776 }
777
778 void MyListCtrl::OnColEndDrag(wxListEvent& event)
779 {
780 LogColEvent( event, wxT("OnColEndDrag") );
781 }
782
783 void MyListCtrl::OnBeginDrag(wxListEvent& event)
784 {
785 const wxPoint& pt = event.m_pointDrag;
786
787 int flags;
788 wxLogMessage( wxT("OnBeginDrag at (%d, %d), item %ld."),
789 pt.x, pt.y, HitTest(pt, flags) );
790 }
791
792 void MyListCtrl::OnBeginRDrag(wxListEvent& event)
793 {
794 wxLogMessage( wxT("OnBeginRDrag at %d,%d."),
795 event.m_pointDrag.x, event.m_pointDrag.y );
796 }
797
798 void MyListCtrl::OnBeginLabelEdit(wxListEvent& event)
799 {
800 wxLogMessage( wxT("OnBeginLabelEdit: %s"), event.m_item.m_text.c_str());
801 }
802
803 void MyListCtrl::OnEndLabelEdit(wxListEvent& event)
804 {
805 wxLogMessage( wxT("OnEndLabelEdit: %s"),
806 event.IsEditCancelled() ? _T("[cancelled]")
807 : event.m_item.m_text.c_str());
808 }
809
810 void MyListCtrl::OnDeleteItem(wxListEvent& event)
811 {
812 LogEvent(event, _T("OnDeleteItem"));
813 }
814
815 void MyListCtrl::OnDeleteAllItems(wxListEvent& event)
816 {
817 LogEvent(event, _T("OnDeleteAllItems"));
818 }
819
820 #if WXWIN_COMPATIBILITY_2_4
821 void MyListCtrl::OnGetInfo(wxListEvent& event)
822 {
823 wxString msg;
824
825 msg << _T("OnGetInfo (") << event.m_item.m_itemId << _T(", ") << event.m_item.m_col << _T(")");
826 if ( event.m_item.m_mask & wxLIST_MASK_STATE )
827 msg << _T(" wxLIST_MASK_STATE");
828 if ( event.m_item.m_mask & wxLIST_MASK_TEXT )
829 msg << _T(" wxLIST_MASK_TEXT");
830 if ( event.m_item.m_mask & wxLIST_MASK_IMAGE )
831 msg << _T(" wxLIST_MASK_IMAGE");
832 if ( event.m_item.m_mask & wxLIST_MASK_DATA )
833 msg << _T(" wxLIST_MASK_DATA");
834 if ( event.m_item.m_mask & wxLIST_SET_ITEM )
835 msg << _T(" wxLIST_SET_ITEM");
836 if ( event.m_item.m_mask & wxLIST_MASK_WIDTH )
837 msg << _T(" wxLIST_MASK_WIDTH");
838 if ( event.m_item.m_mask & wxLIST_MASK_FORMAT )
839 msg << _T(" wxLIST_MASK_WIDTH");
840
841 if ( event.m_item.m_mask & wxLIST_MASK_TEXT )
842 {
843 event.m_item.m_text = _T("My callback text");
844 }
845
846 wxLogMessage(msg);
847 }
848
849 void MyListCtrl::OnSetInfo(wxListEvent& event)
850 {
851 LogEvent(event, _T("OnSetInfo"));
852 }
853 #endif
854
855 void MyListCtrl::OnSelected(wxListEvent& event)
856 {
857 LogEvent(event, _T("OnSelected"));
858
859 if ( GetWindowStyle() & wxLC_REPORT )
860 {
861 wxListItem info;
862 info.m_itemId = event.m_itemIndex;
863 info.m_col = 1;
864 info.m_mask = wxLIST_MASK_TEXT;
865 if ( GetItem(info) )
866 {
867 wxLogMessage(wxT("Value of the 2nd field of the selected item: %s"),
868 info.m_text.c_str());
869 }
870 else
871 {
872 wxFAIL_MSG(wxT("wxListCtrl::GetItem() failed"));
873 }
874 }
875 }
876
877 void MyListCtrl::OnDeselected(wxListEvent& event)
878 {
879 LogEvent(event, _T("OnDeselected"));
880 }
881
882 void MyListCtrl::OnActivated(wxListEvent& event)
883 {
884 LogEvent(event, _T("OnActivated"));
885 }
886
887 void MyListCtrl::OnFocused(wxListEvent& event)
888 {
889 LogEvent(event, _T("OnFocused"));
890
891 event.Skip();
892 }
893
894 void MyListCtrl::OnListKeyDown(wxListEvent& event)
895 {
896 long item;
897
898 switch ( event.GetKeyCode() )
899 {
900 case 'c': // colorize
901 case 'C':
902 {
903 wxListItem info;
904 info.m_itemId = event.GetIndex();
905 GetItem(info);
906
907 wxListItemAttr *attr = info.GetAttributes();
908 if ( !attr || !attr->HasTextColour() )
909 {
910 info.SetTextColour(*wxCYAN);
911
912 SetItem(info);
913
914 RefreshItem(info.m_itemId);
915 }
916 }
917 break;
918
919 case 'n': // next
920 case 'N':
921 item = GetNextItem(-1, wxLIST_NEXT_ALL, wxLIST_STATE_FOCUSED);
922 if ( item++ == GetItemCount() - 1 )
923 {
924 item = 0;
925 }
926
927 wxLogMessage(_T("Focusing item %ld"), item);
928
929 SetItemState(item, wxLIST_STATE_FOCUSED, wxLIST_STATE_FOCUSED);
930 EnsureVisible(item);
931 break;
932
933 case 'r': // show bounding Rect
934 case 'R':
935 {
936 item = event.GetIndex();
937 wxRect r;
938 if ( !GetItemRect(item, r) )
939 {
940 wxLogError(_T("Failed to retrieve rect of item %ld"), item);
941 break;
942 }
943
944 wxLogMessage(_T("Bounding rect of item %ld is (%d, %d)-(%d, %d)"),
945 item, r.x, r.y, r.x + r.width, r.y + r.height);
946 }
947 break;
948
949 case WXK_DELETE:
950 item = GetNextItem(-1, wxLIST_NEXT_ALL, wxLIST_STATE_SELECTED);
951 while ( item != -1 )
952 {
953 DeleteItem(item);
954
955 wxLogMessage(_T("Item %ld deleted"), item);
956
957 // -1 because the indices were shifted by DeleteItem()
958 item = GetNextItem(item - 1,
959 wxLIST_NEXT_ALL, wxLIST_STATE_SELECTED);
960 }
961 break;
962
963 case WXK_INSERT:
964 if ( GetWindowStyle() & wxLC_REPORT )
965 {
966 if ( GetWindowStyle() & wxLC_VIRTUAL )
967 {
968 SetItemCount(GetItemCount() + 1);
969 }
970 else // !virtual
971 {
972 InsertItemInReportView(event.GetIndex());
973 }
974 }
975 //else: fall through
976
977 default:
978 LogEvent(event, _T("OnListKeyDown"));
979
980 event.Skip();
981 }
982 }
983
984 void MyListCtrl::OnChar(wxKeyEvent& event)
985 {
986 wxLogMessage(_T("Got char event."));
987
988 switch ( event.GetKeyCode() )
989 {
990 case 'n':
991 case 'N':
992 case 'c':
993 case 'C':
994 // these are the keys we process ourselves
995 break;
996
997 default:
998 event.Skip();
999 }
1000 }
1001
1002 void MyListCtrl::LogEvent(const wxListEvent& event, const wxChar *eventName)
1003 {
1004 wxLogMessage(_T("Item %ld: %s (item text = %s, data = %ld)"),
1005 event.GetIndex(), eventName,
1006 event.GetText().c_str(), event.GetData());
1007 }
1008
1009 wxString MyListCtrl::OnGetItemText(long item, long column) const
1010 {
1011 if ( GetItemCount() == WXSIZEOF(SMALL_VIRTUAL_VIEW_ITEMS) )
1012 {
1013 return SMALL_VIRTUAL_VIEW_ITEMS[item][column];
1014 }
1015 else
1016 {
1017 return wxString::Format(_T("Column %ld of item %ld"), column, item);
1018 }
1019 }
1020
1021 int MyListCtrl::OnGetItemImage(long WXUNUSED(item)) const
1022 {
1023 return 0;
1024 }
1025
1026 wxListItemAttr *MyListCtrl::OnGetItemAttr(long item) const
1027 {
1028 return item % 2 ? NULL : (wxListItemAttr *)&m_attr;
1029 }
1030
1031 void MyListCtrl::InsertItemInReportView(int i)
1032 {
1033 wxString buf;
1034 buf.Printf(_T("This is item %d"), i);
1035 long tmp = InsertItem(i, buf, 0);
1036 SetItemData(tmp, i);
1037
1038 buf.Printf(_T("Col 1, item %d"), i);
1039 SetItem(i, 1, buf);
1040
1041 buf.Printf(_T("Item %d in column 2"), i);
1042 SetItem(i, 2, buf);
1043 }