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