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