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