]> git.saurik.com Git - wxWidgets.git/blame - samples/listctrl/listtest.cpp
Compile fix.
[wxWidgets.git] / samples / listctrl / listtest.cpp
CommitLineData
457814b5
JS
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 and Markus Holzem
5ea47806 9// Licence: wxWindows license
457814b5
JS
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
907789a0 28#ifndef __WXMSW__
c41ea66a
VZ
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"
907789a0
RR
41#endif
42
2e8a1588 43#include "wx/imaglist.h"
457814b5 44#include "wx/listctrl.h"
81278df2 45#include "wx/timer.h" // for wxStopWatch
11f26ea0
VZ
46#include "wx/colordlg.h" // for wxGetColourFromUser
47
457814b5
JS
48#include "listtest.h"
49
50BEGIN_EVENT_TABLE(MyFrame, wxFrame)
98ec9dbe
VZ
51 EVT_SIZE(MyFrame::OnSize)
52
5ea47806
VZ
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)
98ec9dbe
VZ
61 EVT_MENU(LIST_VIRTUAL_VIEW, MyFrame::OnVirtualView)
62
88b792af 63 EVT_MENU(LIST_FOCUS_LAST, MyFrame::OnFocusLast)
58b3bdc9 64 EVT_MENU(LIST_TOGGLE_FIRST, MyFrame::OnToggleFirstSel)
5ea47806
VZ
65 EVT_MENU(LIST_DESELECT_ALL, MyFrame::OnDeselectAll)
66 EVT_MENU(LIST_SELECT_ALL, MyFrame::OnSelectAll)
bec0a261 67 EVT_MENU(LIST_DELETE, MyFrame::OnDelete)
cf1dfa6b 68 EVT_MENU(LIST_ADD, MyFrame::OnAdd)
5ea47806 69 EVT_MENU(LIST_DELETE_ALL, MyFrame::OnDeleteAll)
fa5f6926 70 EVT_MENU(LIST_SORT, MyFrame::OnSort)
11f26ea0
VZ
71 EVT_MENU(LIST_SET_FG_COL, MyFrame::OnSetFgColour)
72 EVT_MENU(LIST_SET_BG_COL, MyFrame::OnSetBgColour)
7b848b0d 73 EVT_MENU(LIST_TOGGLE_MULTI_SEL, MyFrame::OnToggleMultiSel)
f6bcfd97 74 EVT_MENU(LIST_SHOW_COL_INFO, MyFrame::OnShowColInfo)
b54e41c5 75 EVT_MENU(LIST_SHOW_SEL_INFO, MyFrame::OnShowSelInfo)
98ec9dbe 76
f6bcfd97 77 EVT_UPDATE_UI(LIST_SHOW_COL_INFO, MyFrame::OnUpdateShowColInfo)
457814b5
JS
78END_EVENT_TABLE()
79
80BEGIN_EVENT_TABLE(MyListCtrl, wxListCtrl)
5ea47806
VZ
81 EVT_LIST_BEGIN_DRAG(LIST_CTRL, MyListCtrl::OnBeginDrag)
82 EVT_LIST_BEGIN_RDRAG(LIST_CTRL, MyListCtrl::OnBeginRDrag)
83 EVT_LIST_BEGIN_LABEL_EDIT(LIST_CTRL, MyListCtrl::OnBeginLabelEdit)
84 EVT_LIST_END_LABEL_EDIT(LIST_CTRL, MyListCtrl::OnEndLabelEdit)
85 EVT_LIST_DELETE_ITEM(LIST_CTRL, MyListCtrl::OnDeleteItem)
12c1b46a 86 EVT_LIST_DELETE_ALL_ITEMS(LIST_CTRL, MyListCtrl::OnDeleteAllItems)
5ea47806
VZ
87 EVT_LIST_GET_INFO(LIST_CTRL, MyListCtrl::OnGetInfo)
88 EVT_LIST_SET_INFO(LIST_CTRL, MyListCtrl::OnSetInfo)
89 EVT_LIST_ITEM_SELECTED(LIST_CTRL, MyListCtrl::OnSelected)
90 EVT_LIST_ITEM_DESELECTED(LIST_CTRL, MyListCtrl::OnDeselected)
91 EVT_LIST_KEY_DOWN(LIST_CTRL, MyListCtrl::OnListKeyDown)
92 EVT_LIST_ITEM_ACTIVATED(LIST_CTRL, MyListCtrl::OnActivated)
8636aed8 93 EVT_LIST_COL_CLICK(LIST_CTRL, MyListCtrl::OnColClick)
614391dc 94 EVT_LIST_CACHE_HINT(LIST_CTRL, MyListCtrl::OnCacheHint)
f6bcfd97
BP
95
96 EVT_CHAR(MyListCtrl::OnChar)
457814b5
JS
97END_EVENT_TABLE()
98
99IMPLEMENT_APP(MyApp)
100
776a33cf
VZ
101// number of items in list/report view
102static const int NUM_ITEMS = 30;
103
104// number of items in icon/small icon view
105static const int NUM_ICONS = 9;
106
fa5f6926
VZ
107int wxCALLBACK MyCompareFunction(long item1, long item2, long sortData)
108{
109 // inverse the order
110 return item1 < item2;
111}
112
457814b5 113// `Main program' equivalent, creating windows and returning main app frame
11f26ea0 114bool MyApp::OnInit()
457814b5
JS
115{
116 // Create the main frame window
4acb6ca6 117 MyFrame *frame = new MyFrame(wxT("wxListCtrl Test"), 50, 50, 450, 340);
457814b5
JS
118
119 // Show the frame
120 frame->Show(TRUE);
5ea47806 121
457814b5
JS
122 SetTopWindow(frame);
123
124 return TRUE;
125}
126
127// My frame constructor
c41ea66a
VZ
128MyFrame::MyFrame(const wxChar *title, int x, int y, int w, int h)
129 : wxFrame((wxFrame *)NULL, -1, title, wxPoint(x, y), wxSize(w, h))
457814b5 130{
5ea47806
VZ
131 m_listCtrl = (MyListCtrl *) NULL;
132 m_logWindow = (wxTextCtrl *) NULL;
c41ea66a
VZ
133
134 // Give it an icon
135 SetIcon( wxICON(mondrian) );
136
137 // Make an image list containing large icons
138 m_imageListNormal = new wxImageList(32, 32, TRUE);
139 m_imageListSmall = new wxImageList(16, 16, TRUE);
140
141#ifdef __WXMSW__
142 m_imageListNormal->Add( wxIcon("icon1", wxBITMAP_TYPE_ICO_RESOURCE) );
143 m_imageListNormal->Add( wxIcon("icon2", wxBITMAP_TYPE_ICO_RESOURCE) );
144 m_imageListNormal->Add( wxIcon("icon3", wxBITMAP_TYPE_ICO_RESOURCE) );
145 m_imageListNormal->Add( wxIcon("icon4", wxBITMAP_TYPE_ICO_RESOURCE) );
146 m_imageListNormal->Add( wxIcon("icon5", wxBITMAP_TYPE_ICO_RESOURCE) );
147 m_imageListNormal->Add( wxIcon("icon6", wxBITMAP_TYPE_ICO_RESOURCE) );
148 m_imageListNormal->Add( wxIcon("icon7", wxBITMAP_TYPE_ICO_RESOURCE) );
149 m_imageListNormal->Add( wxIcon("icon8", wxBITMAP_TYPE_ICO_RESOURCE) );
150 m_imageListNormal->Add( wxIcon("icon9", wxBITMAP_TYPE_ICO_RESOURCE) );
151
152 m_imageListSmall->Add( wxIcon("iconsmall", wxBITMAP_TYPE_ICO_RESOURCE) );
153
154#else
155 m_imageListNormal->Add( wxIcon( toolbrai_xpm ) );
156 m_imageListNormal->Add( wxIcon( toolchar_xpm ) );
157 m_imageListNormal->Add( wxIcon( tooldata_xpm ) );
158 m_imageListNormal->Add( wxIcon( toolnote_xpm ) );
159 m_imageListNormal->Add( wxIcon( tooltodo_xpm ) );
160 m_imageListNormal->Add( wxIcon( toolchec_xpm ) );
161 m_imageListNormal->Add( wxIcon( toolgame_xpm ) );
162 m_imageListNormal->Add( wxIcon( tooltime_xpm ) );
163 m_imageListNormal->Add( wxIcon( toolword_xpm ) );
164
165 m_imageListSmall->Add( wxIcon( small1_xpm) );
166#endif
167
168 // Make a menubar
169 wxMenu *menuFile = new wxMenu;
98ec9dbe 170 menuFile->Append(LIST_ABOUT, _T("&About"));
c41ea66a 171 menuFile->AppendSeparator();
98ec9dbe 172 menuFile->Append(LIST_QUIT, _T("E&xit\tAlt-X"));
c41ea66a
VZ
173
174 wxMenu *menuView = new wxMenu;
98ec9dbe
VZ
175 menuView->Append(LIST_LIST_VIEW, _T("&List view\tF1"));
176 menuView->Append(LIST_REPORT_VIEW, _T("&Report view\tF2"));
177 menuView->Append(LIST_ICON_VIEW, _T("&Icon view\tF3"));
178 menuView->Append(LIST_ICON_TEXT_VIEW, _T("Icon view with &text\tF4"));
179 menuView->Append(LIST_SMALL_ICON_VIEW, _T("&Small icon view\tF5"));
180 menuView->Append(LIST_SMALL_ICON_TEXT_VIEW, _T("Small icon &view with text\tF6"));
181 menuView->Append(LIST_VIRTUAL_VIEW, _T("Virtual view\tF7"));
c41ea66a
VZ
182
183 wxMenu *menuList = new wxMenu;
88b792af 184 menuList->Append(LIST_FOCUS_LAST, _T("&Make last item current\tCtrl-L"));
98ec9dbe
VZ
185 menuList->Append(LIST_TOGGLE_FIRST, _T("&Toggle first item\tCtrl-T"));
186 menuList->Append(LIST_DESELECT_ALL, _T("&Deselect All\tCtrl-D"));
187 menuList->Append(LIST_SELECT_ALL, _T("S&elect All\tCtrl-A"));
b54e41c5 188 menuList->AppendSeparator();
98ec9dbe 189 menuList->Append(LIST_SHOW_COL_INFO, _T("Show &column info\tCtrl-C"));
b54e41c5 190 menuList->Append(LIST_SHOW_SEL_INFO, _T("Show &selected items\tCtrl-S"));
c41ea66a 191 menuList->AppendSeparator();
98ec9dbe 192 menuList->Append(LIST_SORT, _T("&Sort\tCtrl-S"));
c41ea66a 193 menuList->AppendSeparator();
cf1dfa6b
VZ
194 menuList->Append(LIST_ADD, _T("&Append an item\tCtrl-P"));
195 menuList->Append(LIST_DELETE, _T("&Delete first item\tCtrl-X"));
98ec9dbe 196 menuList->Append(LIST_DELETE_ALL, _T("Delete &all items"));
c41ea66a 197 menuList->AppendSeparator();
98ec9dbe
VZ
198 menuList->Append(LIST_TOGGLE_MULTI_SEL, _T("&Multiple selection\tCtrl-M"),
199 _T("Toggle multiple selection"), TRUE);
c41ea66a
VZ
200
201 wxMenu *menuCol = new wxMenu;
98ec9dbe
VZ
202 menuCol->Append(LIST_SET_FG_COL, _T("&Foreground colour..."));
203 menuCol->Append(LIST_SET_BG_COL, _T("&Background colour..."));
c41ea66a
VZ
204
205 wxMenuBar *menubar = new wxMenuBar;
98ec9dbe
VZ
206 menubar->Append(menuFile, _T("&File"));
207 menubar->Append(menuView, _T("&View"));
208 menubar->Append(menuList, _T("&List"));
209 menubar->Append(menuCol, _T("&Colour"));
c41ea66a
VZ
210 SetMenuBar(menubar);
211
c41ea66a
VZ
212 m_logWindow = new wxTextCtrl(this, -1, wxEmptyString,
213 wxDefaultPosition, wxDefaultSize,
214 wxTE_MULTILINE | wxSUNKEN_BORDER);
215
216 m_logOld = wxLog::SetActiveTarget(new wxLogTextCtrl(m_logWindow));
217
776a33cf 218 RecreateList(wxLC_REPORT | wxLC_SINGLE_SEL);
c41ea66a
VZ
219
220 CreateStatusBar(3);
457814b5
JS
221}
222
11f26ea0 223MyFrame::~MyFrame()
457814b5 224{
c41ea66a
VZ
225 delete wxLog::SetActiveTarget(m_logOld);
226
227 delete m_imageListNormal;
228 delete m_imageListSmall;
457814b5
JS
229}
230
98ec9dbe 231void MyFrame::OnSize(wxSizeEvent& event)
457814b5 232{
98ec9dbe
VZ
233 if ( !m_logWindow )
234 return;
457814b5 235
98ec9dbe
VZ
236 wxSize size = GetClientSize();
237 wxCoord y = (2*size.y)/3;
238 m_listCtrl->SetSize(0, 0, size.x, y);
239 m_logWindow->SetSize(0, y + 1, size.x, size.y - y);
240
241 event.Skip();
57246713
KB
242}
243
98ec9dbe 244void MyFrame::OnQuit(wxCommandEvent& WXUNUSED(event))
57246713 245{
98ec9dbe 246 Close(TRUE);
57246713
KB
247}
248
b0d77f43 249void MyFrame::OnAbout(wxCommandEvent& WXUNUSED(event))
457814b5 250{
5ea47806
VZ
251 wxMessageDialog dialog(this, "List test sample\nJulian Smart (c) 1997",
252 "About list test", wxOK|wxCANCEL);
457814b5 253
5ea47806 254 dialog.ShowModal();
457814b5
JS
255}
256
88b792af
VZ
257void MyFrame::OnFocusLast(wxCommandEvent& WXUNUSED(event))
258{
259 long index = m_listCtrl->GetItemCount() - 1;
260 if ( index == -1 )
261 {
262 return;
263 }
264
265 m_listCtrl->SetItemState(index, wxLIST_STATE_FOCUSED, wxLIST_STATE_FOCUSED);
266 m_listCtrl->EnsureVisible(index);
267}
268
58b3bdc9
VZ
269void MyFrame::OnToggleFirstSel(wxCommandEvent& WXUNUSED(event))
270{
271 m_listCtrl->SetItemState(0, wxLIST_STATE_SELECTED, wxLIST_STATE_SELECTED);
272}
273
c4771147
KB
274void MyFrame::OnDeselectAll(wxCommandEvent& WXUNUSED(event))
275{
5ea47806
VZ
276 int n = m_listCtrl->GetItemCount();
277 for (int i = 0; i < n; i++)
278 m_listCtrl->SetItemState(i,0,wxLIST_STATE_SELECTED);
c4771147
KB
279}
280
281void MyFrame::OnSelectAll(wxCommandEvent& WXUNUSED(event))
282{
5ea47806
VZ
283 int n = m_listCtrl->GetItemCount();
284 for (int i = 0; i < n; i++)
285 m_listCtrl->SetItemState(i,wxLIST_STATE_SELECTED, wxLIST_STATE_SELECTED);
c4771147
KB
286}
287
776a33cf
VZ
288// ----------------------------------------------------------------------------
289// changing listctrl modes
290// ----------------------------------------------------------------------------
291
292void MyFrame::RecreateList(long flags, bool withText)
457814b5 293{
776a33cf
VZ
294 // we could avoid recreating it if we don't set/clear the wxLC_VIRTUAL
295 // style, but it is more trouble to do it than not
296#if 0
297 if ( !m_listCtrl || ((flags & wxLC_VIRTUAL) !=
298 (m_listCtrl->GetWindowStyleFlag() & wxLC_VIRTUAL)) )
299#endif
300 {
301 delete m_listCtrl;
302
303 m_listCtrl = new MyListCtrl(this, LIST_CTRL,
304 wxDefaultPosition, wxDefaultSize,
305 flags |
306 wxSUNKEN_BORDER);
307
308 switch ( flags & wxLC_MASK_TYPE )
309 {
310 case wxLC_LIST:
311 InitWithListItems();
312 break;
313
314 case wxLC_ICON:
315 InitWithIconItems(withText);
316 break;
317
318 case wxLC_SMALL_ICON:
319 InitWithIconItems(withText, TRUE);
320 break;
321
322 case wxLC_REPORT:
323 if ( flags & wxLC_VIRTUAL )
324 InitWithVirtualItems();
325 else
326 InitWithReportItems();
327 break;
328
329 default:
330 wxFAIL_MSG( _T("unknown listctrl mode") );
331 }
332 }
333
334#ifdef __WXMSW__
335 SendSizeEvent();
336#endif
337
0180dad6 338 m_logWindow->Clear();
776a33cf 339}
95bf655c 340
776a33cf
VZ
341void MyFrame::OnListView(wxCommandEvent& WXUNUSED(event))
342{
343 RecreateList(wxLC_LIST);
344}
457814b5 345
776a33cf
VZ
346void MyFrame::InitWithListItems()
347{
348 for ( int i = 0; i < NUM_ITEMS; i++ )
0180dad6 349 {
98ec9dbe 350 m_listCtrl->InsertItem(i, wxString::Format(_T("Item %d"), i));
0180dad6 351 }
457814b5
JS
352}
353
b0d77f43 354void MyFrame::OnReportView(wxCommandEvent& WXUNUSED(event))
457814b5 355{
776a33cf
VZ
356 RecreateList(wxLC_REPORT);
357}
5ea47806 358
776a33cf
VZ
359void MyFrame::InitWithReportItems()
360{
c41ea66a 361 m_listCtrl->SetImageList(m_imageListSmall, wxIMAGE_LIST_SMALL);
457814b5 362
0180dad6
RR
363 m_listCtrl->InsertColumn(0, "Column 1"); // , wxLIST_FORMAT_LEFT, 140);
364 m_listCtrl->InsertColumn(1, "Column 2"); // , wxLIST_FORMAT_LEFT, 140);
365 m_listCtrl->InsertColumn(2, "One More Column (2)"); // , wxLIST_FORMAT_LEFT, 140);
366
81278df2
VZ
367 // to speed up inserting we hide the control temporarily
368 m_listCtrl->Hide();
369
370 wxStopWatch sw;
371
81278df2 372 for ( int i = 0; i < NUM_ITEMS; i++ )
0180dad6 373 {
5cd89174 374 m_listCtrl->InsertItemInReportView(i);
0180dad6 375 }
bdc72a22 376
81278df2
VZ
377 m_logWindow->WriteText(wxString::Format(_T("%d items inserted in %ldms\n"),
378 NUM_ITEMS, sw.Time()));
379 m_listCtrl->Show();
380
f6b77239 381 // we leave all mask fields to 0 and only change the colour
bdc72a22
VZ
382 wxListItem item;
383 item.m_itemId = 0;
0530737d 384 item.SetTextColour(*wxRED);
bdc72a22
VZ
385 m_listCtrl->SetItem( item );
386
387 item.m_itemId = 2;
d62228a6 388 item.SetTextColour(*wxGREEN);
bdc72a22 389 m_listCtrl->SetItem( item );
d62228a6 390 item.m_itemId = 4;
bdc72a22 391 item.SetTextColour(*wxLIGHT_GREY);
d62228a6
VZ
392 item.SetFont(*wxITALIC_FONT);
393 item.SetBackgroundColour(*wxRED);
bdc72a22 394 m_listCtrl->SetItem( item );
5ea47806 395
d62228a6
VZ
396 m_listCtrl->SetTextColour(*wxBLUE);
397 m_listCtrl->SetBackgroundColour(*wxLIGHT_GREY);
398
0180dad6
RR
399 m_listCtrl->SetColumnWidth( 0, wxLIST_AUTOSIZE );
400 m_listCtrl->SetColumnWidth( 1, wxLIST_AUTOSIZE );
401 m_listCtrl->SetColumnWidth( 2, wxLIST_AUTOSIZE );
457814b5
JS
402}
403
776a33cf 404void MyFrame::InitWithIconItems(bool withText, bool sameIcon)
457814b5 405{
c41ea66a
VZ
406 m_listCtrl->SetImageList(m_imageListNormal, wxIMAGE_LIST_NORMAL);
407 m_listCtrl->SetImageList(m_imageListSmall, wxIMAGE_LIST_SMALL);
457814b5 408
776a33cf 409 for ( int i = 0; i < NUM_ICONS; i++ )
5ea47806 410 {
776a33cf
VZ
411 int image = sameIcon ? 0 : i;
412
413 if ( withText )
414 {
415 m_listCtrl->InsertItem(i, wxString::Format(_T("Label %d"), i),
416 image);
417 }
418 else
419 {
420 m_listCtrl->InsertItem(i, image);
421 }
5ea47806 422 }
457814b5
JS
423}
424
776a33cf 425void MyFrame::OnIconView(wxCommandEvent& WXUNUSED(event))
457814b5 426{
776a33cf
VZ
427 RecreateList(wxLC_ICON, FALSE);
428}
457814b5 429
776a33cf
VZ
430void MyFrame::OnIconTextView(wxCommandEvent& WXUNUSED(event))
431{
432 RecreateList(wxLC_ICON);
457814b5
JS
433}
434
b0d77f43 435void MyFrame::OnSmallIconView(wxCommandEvent& WXUNUSED(event))
457814b5 436{
776a33cf 437 RecreateList(wxLC_SMALL_ICON, FALSE);
457814b5
JS
438}
439
b0d77f43 440void MyFrame::OnSmallIconTextView(wxCommandEvent& WXUNUSED(event))
457814b5 441{
776a33cf 442 RecreateList(wxLC_SMALL_ICON);
5ea47806
VZ
443}
444
98ec9dbe
VZ
445void MyFrame::OnVirtualView(wxCommandEvent& WXUNUSED(event))
446{
776a33cf
VZ
447 RecreateList(wxLC_REPORT | wxLC_VIRTUAL);
448}
98ec9dbe 449
776a33cf
VZ
450void MyFrame::InitWithVirtualItems()
451{
98ec9dbe
VZ
452 m_listCtrl->InsertColumn(0, "First Column");
453 m_listCtrl->InsertColumn(1, "Second Column");
454 m_listCtrl->SetColumnWidth(0, 150);
455 m_listCtrl->SetColumnWidth(1, 150);
456
457 m_listCtrl->SetItemCount(1000000);
98ec9dbe
VZ
458}
459
fa5f6926
VZ
460void MyFrame::OnSort(wxCommandEvent& WXUNUSED(event))
461{
81278df2
VZ
462 wxStopWatch sw;
463
fa5f6926 464 m_listCtrl->SortItems(MyCompareFunction, 0);
81278df2
VZ
465
466 m_logWindow->WriteText(wxString::Format(_T("Sorting %d items took %ld ms\n"),
467 m_listCtrl->GetItemCount(),
468 sw.Time()));
fa5f6926
VZ
469}
470
b54e41c5
VZ
471void MyFrame::OnShowSelInfo(wxCommandEvent& event)
472{
473 int selCount = m_listCtrl->GetSelectedItemCount();
474 wxLogMessage(_T("%d items selected:"), selCount);
475
476 // don't show too many items
477 size_t shownCount = 0;
478
479 long item = m_listCtrl->GetNextItem(-1, wxLIST_NEXT_ALL,
480 wxLIST_STATE_SELECTED);
481 while ( item != -1 )
482 {
483 wxLogMessage(_T("\t%ld (%s)"),
484 item, m_listCtrl->GetItemText(item).c_str());
485
486 if ( ++shownCount > 10 )
487 {
488 wxLogMessage(_T("\t... more selected items snipped..."));
489 break;
490 }
491
492 item = m_listCtrl->GetNextItem(item, wxLIST_NEXT_ALL,
493 wxLIST_STATE_SELECTED);
494 }
495}
496
f6bcfd97
BP
497void MyFrame::OnShowColInfo(wxCommandEvent& event)
498{
499 int count = m_listCtrl->GetColumnCount();
4acb6ca6 500 wxLogMessage(wxT("%d columns:"), count);
f6bcfd97
BP
501 for ( int c = 0; c < count; c++ )
502 {
4acb6ca6 503 wxLogMessage(wxT("\tcolumn %d has width %d"), c,
f6bcfd97
BP
504 m_listCtrl->GetColumnWidth(c));
505 }
506}
507
508void MyFrame::OnUpdateShowColInfo(wxUpdateUIEvent& event)
509{
510 event.Enable( (m_listCtrl->GetWindowStyleFlag() & wxLC_REPORT) != 0 );
511}
512
7b848b0d
VZ
513void MyFrame::OnToggleMultiSel(wxCommandEvent& WXUNUSED(event))
514{
7b848b0d
VZ
515 long flags = m_listCtrl->GetWindowStyleFlag();
516 if ( flags & wxLC_SINGLE_SEL )
776a33cf 517 flags &= ~wxLC_SINGLE_SEL;
7b848b0d 518 else
776a33cf
VZ
519 flags |= wxLC_SINGLE_SEL;
520
4acb6ca6 521 m_logWindow->WriteText(wxString::Format(wxT("Current selection mode: %sle\n"),
776a33cf 522 (flags & wxLC_SINGLE_SEL) ? "sing" : "multip"));
7b848b0d 523
776a33cf 524 RecreateList(flags);
7b848b0d
VZ
525}
526
11f26ea0
VZ
527void MyFrame::OnSetFgColour(wxCommandEvent& WXUNUSED(event))
528{
529 m_listCtrl->SetForegroundColour(wxGetColourFromUser(this));
530 m_listCtrl->Refresh();
531}
532
533void MyFrame::OnSetBgColour(wxCommandEvent& WXUNUSED(event))
534{
535 m_listCtrl->SetBackgroundColour(wxGetColourFromUser(this));
536 m_listCtrl->Refresh();
537}
538
cf1dfa6b
VZ
539void MyFrame::OnAdd(wxCommandEvent& WXUNUSED(event))
540{
541 m_listCtrl->InsertItem(m_listCtrl->GetItemCount(), _T("Appended item"));
542}
543
bec0a261
VZ
544void MyFrame::OnDelete(wxCommandEvent& WXUNUSED(event))
545{
546 if ( m_listCtrl->GetItemCount() )
547 {
548 m_listCtrl->DeleteItem(0);
549 }
550 else
551 {
552 m_logWindow->WriteText("Nothing to delete");
553 }
554}
555
5ea47806
VZ
556void MyFrame::OnDeleteAll(wxCommandEvent& WXUNUSED(event))
557{
81278df2 558 wxStopWatch sw;
5ea47806 559
5ea47806
VZ
560 m_listCtrl->DeleteAllItems();
561
81278df2
VZ
562 m_logWindow->WriteText(wxString::Format(_T("Deleting %d items took %ld ms\n"),
563 m_listCtrl->GetItemCount(),
564 sw.Time()));
457814b5
JS
565}
566
567// MyListCtrl
568
614391dc
VZ
569void MyListCtrl::OnCacheHint(wxListEvent& event)
570{
4acb6ca6 571 wxLogMessage( wxT("OnCacheHint: cache items %ld..%ld"),
614391dc
VZ
572 event.GetCacheFrom(), event.GetCacheTo() );
573}
574
8636aed8
RR
575void MyListCtrl::OnColClick(wxListEvent& event)
576{
4acb6ca6 577 wxLogMessage( wxT("OnColumnClick at %d."), event.GetColumn() );
8636aed8
RR
578}
579
fd9811b1 580void MyListCtrl::OnBeginDrag(wxListEvent& event)
457814b5 581{
4acb6ca6 582 wxLogMessage( wxT("OnBeginDrag at %d,%d."),
c41ea66a 583 event.m_pointDrag.x, event.m_pointDrag.y );
457814b5
JS
584}
585
fd9811b1 586void MyListCtrl::OnBeginRDrag(wxListEvent& event)
457814b5 587{
4acb6ca6 588 wxLogMessage( wxT("OnBeginRDrag at %d,%d."),
c41ea66a 589 event.m_pointDrag.x, event.m_pointDrag.y );
457814b5
JS
590}
591
fd9811b1 592void MyListCtrl::OnBeginLabelEdit(wxListEvent& event)
457814b5 593{
4acb6ca6 594 wxLogMessage( wxT("OnBeginLabelEdit: %s"), event.m_item.m_text.c_str());
457814b5
JS
595}
596
fd9811b1 597void MyListCtrl::OnEndLabelEdit(wxListEvent& event)
457814b5 598{
4acb6ca6 599 wxLogMessage( wxT("OnEndLabelEdit: %s"), event.m_item.m_text.c_str());
457814b5
JS
600}
601
efbb7287 602void MyListCtrl::OnDeleteItem(wxListEvent& event)
457814b5 603{
c41ea66a 604 LogEvent(event, _T("OnDeleteItem"));
457814b5
JS
605}
606
c41ea66a 607void MyListCtrl::OnDeleteAllItems(wxListEvent& event)
12c1b46a 608{
c41ea66a 609 LogEvent(event, _T("OnDeleteAllItems"));
12c1b46a
RR
610}
611
fd9811b1 612void MyListCtrl::OnGetInfo(wxListEvent& event)
457814b5 613{
c41ea66a 614 wxString msg;
5ea47806 615
c41ea66a 616 msg << "OnGetInfo (" << event.m_item.m_itemId << ", " << event.m_item.m_col << ")";
5ea47806 617 if ( event.m_item.m_mask & wxLIST_MASK_STATE )
c41ea66a 618 msg << " wxLIST_MASK_STATE";
5ea47806 619 if ( event.m_item.m_mask & wxLIST_MASK_TEXT )
c41ea66a 620 msg << " wxLIST_MASK_TEXT";
5ea47806 621 if ( event.m_item.m_mask & wxLIST_MASK_IMAGE )
c41ea66a 622 msg << " wxLIST_MASK_IMAGE";
5ea47806 623 if ( event.m_item.m_mask & wxLIST_MASK_DATA )
c41ea66a 624 msg << " wxLIST_MASK_DATA";
5ea47806 625 if ( event.m_item.m_mask & wxLIST_SET_ITEM )
c41ea66a 626 msg << " wxLIST_SET_ITEM";
5ea47806 627 if ( event.m_item.m_mask & wxLIST_MASK_WIDTH )
c41ea66a 628 msg << " wxLIST_MASK_WIDTH";
5ea47806 629 if ( event.m_item.m_mask & wxLIST_MASK_FORMAT )
c41ea66a 630 msg << " wxLIST_MASK_WIDTH";
5ea47806
VZ
631
632 if ( event.m_item.m_mask & wxLIST_MASK_TEXT )
633 {
634 event.m_item.m_text = "My callback text";
635 }
c41ea66a
VZ
636
637 wxLogMessage(msg);
457814b5
JS
638}
639
efbb7287 640void MyListCtrl::OnSetInfo(wxListEvent& event)
457814b5 641{
c41ea66a 642 LogEvent(event, _T("OnSetInfo"));
457814b5
JS
643}
644
74b31181 645void MyListCtrl::OnSelected(wxListEvent& event)
457814b5 646{
c41ea66a 647 LogEvent(event, _T("OnSelected"));
457814b5 648
40779a03 649 if ( GetWindowStyle() & wxLC_REPORT )
74b31181 650 {
40779a03
VZ
651 wxListItem info;
652 info.m_itemId = event.m_itemIndex;
653 info.m_col = 1;
654 info.m_mask = wxLIST_MASK_TEXT;
655 if ( GetItem(info) )
656 {
4acb6ca6 657 wxLogMessage(wxT("Value of the 2nd field of the selected item: %s"),
c41ea66a 658 info.m_text.c_str());
40779a03
VZ
659 }
660 else
661 {
4acb6ca6 662 wxFAIL_MSG(wxT("wxListCtrl::GetItem() failed"));
40779a03 663 }
74b31181 664 }
457814b5
JS
665}
666
efbb7287 667void MyListCtrl::OnDeselected(wxListEvent& event)
457814b5 668{
c41ea66a 669 LogEvent(event, _T("OnDeselected"));
457814b5
JS
670}
671
efbb7287 672void MyListCtrl::OnActivated(wxListEvent& event)
435fe83e 673{
c41ea66a 674 LogEvent(event, _T("OnActivated"));
435fe83e
RR
675}
676
8e1d4f96 677void MyListCtrl::OnListKeyDown(wxListEvent& event)
457814b5 678{
5cd89174
VZ
679 switch ( event.GetCode() )
680 {
6c02c329
VZ
681 case 'c':
682 {
683 wxListItem info;
684 info.m_itemId = event.GetIndex();
685 GetItem(info);
686
687 wxListItemAttr *attr = info.GetAttributes();
688 if ( !attr || !attr->HasTextColour() )
689 {
690 info.SetTextColour(*wxCYAN);
691
692 SetItem(info);
693 }
694 }
695 break;
696
5cd89174 697 case WXK_DELETE:
91c6cc0e
VZ
698 {
699 long item = GetNextItem(-1,
700 wxLIST_NEXT_ALL, wxLIST_STATE_SELECTED);
701 while ( item != -1 )
702 {
703 DeleteItem(item);
704
705 wxLogMessage(_T("Item %ld deleted"), item);
f6bcfd97 706
91c6cc0e
VZ
707 // -1 because the indices were shifted by DeleteItem()
708 item = GetNextItem(item - 1,
709 wxLIST_NEXT_ALL, wxLIST_STATE_SELECTED);
710 }
711 }
5cd89174
VZ
712 break;
713
714 case WXK_INSERT:
715 if ( GetWindowStyle() & wxLC_REPORT )
716 {
717 if ( GetWindowStyle() & wxLC_VIRTUAL )
718 {
719 SetItemCount(GetItemCount() + 1);
720 }
721 else // !virtual
722 {
723 InsertItemInReportView(event.GetIndex());
724 }
725 }
726 //else: fall through
727
728 default:
729 LogEvent(event, _T("OnListKeyDown"));
730
731 event.Skip();
732 }
f6bcfd97
BP
733}
734
735void MyListCtrl::OnChar(wxKeyEvent& event)
736{
737 wxLogMessage(_T("Got char event."));
738
739 event.Skip();
457814b5
JS
740}
741
c41ea66a
VZ
742void MyListCtrl::LogEvent(const wxListEvent& event, const wxChar *eventName)
743{
f6bcfd97
BP
744 wxLogMessage(_T("Item %ld: %s (item text = %s, data = %ld)"),
745 event.GetIndex(), eventName,
746 event.GetText().c_str(), event.GetData());
c41ea66a 747}
435fe83e 748
98ec9dbe
VZ
749wxString MyListCtrl::OnGetItemText(long item, long column) const
750{
751 return wxString::Format(_T("Column %ld of item %ld"), column, item);
752}
753
754int MyListCtrl::OnGetItemImage(long item) const
755{
756 return 0;
757}
758
6c02c329
VZ
759wxListItemAttr *MyListCtrl::OnGetItemAttr(long item) const
760{
761 return item % 2 ? NULL : (wxListItemAttr *)&m_attr;
762}
763
5cd89174
VZ
764void MyListCtrl::InsertItemInReportView(int i)
765{
766 wxString buf;
767 buf.Printf(_T("This is item %d"), i);
768 long tmp = InsertItem(i, buf, 0);
769 SetItemData(tmp, i);
770
771 buf.Printf(_T("Col 1, item %d"), i);
772 SetItem(i, 1, buf);
773
774 buf.Printf(_T("Item %d in column 2"), i);
775 SetItem(i, 2, buf);
776}
777