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