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