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