]> git.saurik.com Git - wxWidgets.git/blame - samples/listctrl/listtest.cpp
Correct file format errors
[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)
07bf769e
VZ
252{
253 DoSize();
254
255 event.Skip();
256}
257
258void MyFrame::DoSize()
457814b5 259{
98ec9dbe
VZ
260 if ( !m_logWindow )
261 return;
457814b5 262
98ec9dbe
VZ
263 wxSize size = GetClientSize();
264 wxCoord y = (2*size.y)/3;
265 m_listCtrl->SetSize(0, 0, size.x, y);
266 m_logWindow->SetSize(0, y + 1, size.x, size.y - y);
57246713
KB
267}
268
68457180
VZ
269bool MyFrame::CheckNonVirtual() const
270{
271 if ( !m_listCtrl->HasFlag(wxLC_VIRTUAL) )
272 return true;
273
274 // "this" == whatever
275 wxLogWarning(_T("Can't do this in virtual view, sorry."));
276
277 return false;
278}
279
98ec9dbe 280void MyFrame::OnQuit(wxCommandEvent& WXUNUSED(event))
57246713 281{
98ec9dbe 282 Close(TRUE);
57246713
KB
283}
284
b0d77f43 285void MyFrame::OnAbout(wxCommandEvent& WXUNUSED(event))
457814b5 286{
efe23391
VZ
287 wxMessageDialog dialog(this, _T("List test sample\nJulian Smart (c) 1997"),
288 _T("About list test"), wxOK|wxCANCEL);
457814b5 289
5ea47806 290 dialog.ShowModal();
457814b5
JS
291}
292
c71963cd 293void MyFrame::OnFreeze(wxCommandEvent& WXUNUSED(event))
c5c528fc
VZ
294{
295 wxLogMessage(_T("Freezing the control"));
296
297 m_listCtrl->Freeze();
298}
299
c71963cd 300void MyFrame::OnThaw(wxCommandEvent& WXUNUSED(event))
c5c528fc
VZ
301{
302 wxLogMessage(_T("Thawing the control"));
303
304 m_listCtrl->Thaw();
305}
306
88b792af
VZ
307void MyFrame::OnFocusLast(wxCommandEvent& WXUNUSED(event))
308{
309 long index = m_listCtrl->GetItemCount() - 1;
310 if ( index == -1 )
311 {
312 return;
313 }
314
315 m_listCtrl->SetItemState(index, wxLIST_STATE_FOCUSED, wxLIST_STATE_FOCUSED);
316 m_listCtrl->EnsureVisible(index);
317}
318
58b3bdc9
VZ
319void MyFrame::OnToggleFirstSel(wxCommandEvent& WXUNUSED(event))
320{
95f913a9 321 m_listCtrl->SetItemState(0, (~m_listCtrl->GetItemState(0, wxLIST_STATE_SELECTED) ) & wxLIST_STATE_SELECTED, wxLIST_STATE_SELECTED);
58b3bdc9
VZ
322}
323
c4771147
KB
324void MyFrame::OnDeselectAll(wxCommandEvent& WXUNUSED(event))
325{
68457180
VZ
326 if ( !CheckNonVirtual() )
327 return;
328
5ea47806
VZ
329 int n = m_listCtrl->GetItemCount();
330 for (int i = 0; i < n; i++)
331 m_listCtrl->SetItemState(i,0,wxLIST_STATE_SELECTED);
c4771147
KB
332}
333
334void MyFrame::OnSelectAll(wxCommandEvent& WXUNUSED(event))
335{
68457180
VZ
336 if ( !CheckNonVirtual() )
337 return;
338
5ea47806
VZ
339 int n = m_listCtrl->GetItemCount();
340 for (int i = 0; i < n; i++)
341 m_listCtrl->SetItemState(i,wxLIST_STATE_SELECTED, wxLIST_STATE_SELECTED);
c4771147
KB
342}
343
776a33cf
VZ
344// ----------------------------------------------------------------------------
345// changing listctrl modes
346// ----------------------------------------------------------------------------
347
348void MyFrame::RecreateList(long flags, bool withText)
457814b5 349{
776a33cf
VZ
350 // we could avoid recreating it if we don't set/clear the wxLC_VIRTUAL
351 // style, but it is more trouble to do it than not
352#if 0
353 if ( !m_listCtrl || ((flags & wxLC_VIRTUAL) !=
354 (m_listCtrl->GetWindowStyleFlag() & wxLC_VIRTUAL)) )
355#endif
356 {
357 delete m_listCtrl;
358
2b5f62a0 359 m_listCtrl = new MyListCtrl(m_panel, LIST_CTRL,
776a33cf
VZ
360 wxDefaultPosition, wxDefaultSize,
361 flags |
efe23391 362 wxSUNKEN_BORDER | wxLC_EDIT_LABELS);
776a33cf
VZ
363
364 switch ( flags & wxLC_MASK_TYPE )
365 {
366 case wxLC_LIST:
367 InitWithListItems();
368 break;
369
370 case wxLC_ICON:
371 InitWithIconItems(withText);
372 break;
373
374 case wxLC_SMALL_ICON:
375 InitWithIconItems(withText, TRUE);
376 break;
377
378 case wxLC_REPORT:
379 if ( flags & wxLC_VIRTUAL )
380 InitWithVirtualItems();
381 else
382 InitWithReportItems();
383 break;
384
385 default:
386 wxFAIL_MSG( _T("unknown listctrl mode") );
387 }
388 }
389
07bf769e 390 DoSize();
776a33cf 391
0180dad6 392 m_logWindow->Clear();
776a33cf 393}
95bf655c 394
776a33cf
VZ
395void MyFrame::OnListView(wxCommandEvent& WXUNUSED(event))
396{
397 RecreateList(wxLC_LIST);
398}
457814b5 399
776a33cf
VZ
400void MyFrame::InitWithListItems()
401{
402 for ( int i = 0; i < NUM_ITEMS; i++ )
0180dad6 403 {
98ec9dbe 404 m_listCtrl->InsertItem(i, wxString::Format(_T("Item %d"), i));
0180dad6 405 }
457814b5
JS
406}
407
b0d77f43 408void MyFrame::OnReportView(wxCommandEvent& WXUNUSED(event))
457814b5 409{
776a33cf
VZ
410 RecreateList(wxLC_REPORT);
411}
5ea47806 412
776a33cf
VZ
413void MyFrame::InitWithReportItems()
414{
c41ea66a 415 m_listCtrl->SetImageList(m_imageListSmall, wxIMAGE_LIST_SMALL);
457814b5 416
caa20b1e
VZ
417 // note that under MSW for SetColumnWidth() to work we need to create the
418 // items with images initially even if we specify dummy image id
6f806543 419 wxListItem itemCol;
caa20b1e
VZ
420 itemCol.SetText(_T("Column 1"));
421 itemCol.SetImage(-1);
6f806543 422 m_listCtrl->InsertColumn(0, itemCol);
caa20b1e
VZ
423
424 itemCol.SetText(_T("Column 2"));
425 itemCol.SetAlign(wxLIST_FORMAT_CENTRE);
6f806543 426 m_listCtrl->InsertColumn(1, itemCol);
caa20b1e
VZ
427
428 itemCol.SetText(_T("Column 3"));
429 itemCol.SetAlign(wxLIST_FORMAT_RIGHT);
6f806543 430 m_listCtrl->InsertColumn(2, itemCol);
0180dad6 431
81278df2
VZ
432 // to speed up inserting we hide the control temporarily
433 m_listCtrl->Hide();
434
435 wxStopWatch sw;
436
81278df2 437 for ( int i = 0; i < NUM_ITEMS; i++ )
0180dad6 438 {
5cd89174 439 m_listCtrl->InsertItemInReportView(i);
0180dad6 440 }
bdc72a22 441
81278df2
VZ
442 m_logWindow->WriteText(wxString::Format(_T("%d items inserted in %ldms\n"),
443 NUM_ITEMS, sw.Time()));
444 m_listCtrl->Show();
445
f6b77239 446 // we leave all mask fields to 0 and only change the colour
bdc72a22
VZ
447 wxListItem item;
448 item.m_itemId = 0;
0530737d 449 item.SetTextColour(*wxRED);
bdc72a22
VZ
450 m_listCtrl->SetItem( item );
451
452 item.m_itemId = 2;
d62228a6 453 item.SetTextColour(*wxGREEN);
bdc72a22 454 m_listCtrl->SetItem( item );
d62228a6 455 item.m_itemId = 4;
bdc72a22 456 item.SetTextColour(*wxLIGHT_GREY);
d62228a6
VZ
457 item.SetFont(*wxITALIC_FONT);
458 item.SetBackgroundColour(*wxRED);
bdc72a22 459 m_listCtrl->SetItem( item );
5ea47806 460
d62228a6
VZ
461 m_listCtrl->SetTextColour(*wxBLUE);
462 m_listCtrl->SetBackgroundColour(*wxLIGHT_GREY);
463
0180dad6
RR
464 m_listCtrl->SetColumnWidth( 0, wxLIST_AUTOSIZE );
465 m_listCtrl->SetColumnWidth( 1, wxLIST_AUTOSIZE );
466 m_listCtrl->SetColumnWidth( 2, wxLIST_AUTOSIZE );
457814b5
JS
467}
468
776a33cf 469void MyFrame::InitWithIconItems(bool withText, bool sameIcon)
457814b5 470{
c41ea66a
VZ
471 m_listCtrl->SetImageList(m_imageListNormal, wxIMAGE_LIST_NORMAL);
472 m_listCtrl->SetImageList(m_imageListSmall, wxIMAGE_LIST_SMALL);
457814b5 473
776a33cf 474 for ( int i = 0; i < NUM_ICONS; i++ )
5ea47806 475 {
776a33cf
VZ
476 int image = sameIcon ? 0 : i;
477
478 if ( withText )
479 {
480 m_listCtrl->InsertItem(i, wxString::Format(_T("Label %d"), i),
481 image);
482 }
483 else
484 {
485 m_listCtrl->InsertItem(i, image);
486 }
5ea47806 487 }
457814b5
JS
488}
489
776a33cf 490void MyFrame::OnIconView(wxCommandEvent& WXUNUSED(event))
457814b5 491{
776a33cf
VZ
492 RecreateList(wxLC_ICON, FALSE);
493}
457814b5 494
776a33cf
VZ
495void MyFrame::OnIconTextView(wxCommandEvent& WXUNUSED(event))
496{
497 RecreateList(wxLC_ICON);
457814b5
JS
498}
499
b0d77f43 500void MyFrame::OnSmallIconView(wxCommandEvent& WXUNUSED(event))
457814b5 501{
776a33cf 502 RecreateList(wxLC_SMALL_ICON, FALSE);
457814b5
JS
503}
504
b0d77f43 505void MyFrame::OnSmallIconTextView(wxCommandEvent& WXUNUSED(event))
457814b5 506{
776a33cf 507 RecreateList(wxLC_SMALL_ICON);
5ea47806
VZ
508}
509
98ec9dbe
VZ
510void MyFrame::OnVirtualView(wxCommandEvent& WXUNUSED(event))
511{
776a33cf
VZ
512 RecreateList(wxLC_REPORT | wxLC_VIRTUAL);
513}
98ec9dbe 514
776a33cf
VZ
515void MyFrame::InitWithVirtualItems()
516{
27770773
VZ
517 m_listCtrl->SetImageList(m_imageListSmall, wxIMAGE_LIST_SMALL);
518
efe23391
VZ
519 m_listCtrl->InsertColumn(0, _T("First Column"));
520 m_listCtrl->InsertColumn(1, _T("Second Column"));
98ec9dbe
VZ
521 m_listCtrl->SetColumnWidth(0, 150);
522 m_listCtrl->SetColumnWidth(1, 150);
523
524 m_listCtrl->SetItemCount(1000000);
98ec9dbe
VZ
525}
526
fa5f6926
VZ
527void MyFrame::OnSort(wxCommandEvent& WXUNUSED(event))
528{
81278df2
VZ
529 wxStopWatch sw;
530
fa5f6926 531 m_listCtrl->SortItems(MyCompareFunction, 0);
81278df2
VZ
532
533 m_logWindow->WriteText(wxString::Format(_T("Sorting %d items took %ld ms\n"),
534 m_listCtrl->GetItemCount(),
535 sw.Time()));
fa5f6926
VZ
536}
537
c71963cd 538void MyFrame::OnShowSelInfo(wxCommandEvent& WXUNUSED(event))
b54e41c5
VZ
539{
540 int selCount = m_listCtrl->GetSelectedItemCount();
541 wxLogMessage(_T("%d items selected:"), selCount);
542
543 // don't show too many items
544 size_t shownCount = 0;
545
546 long item = m_listCtrl->GetNextItem(-1, wxLIST_NEXT_ALL,
547 wxLIST_STATE_SELECTED);
548 while ( item != -1 )
549 {
550 wxLogMessage(_T("\t%ld (%s)"),
551 item, m_listCtrl->GetItemText(item).c_str());
552
553 if ( ++shownCount > 10 )
554 {
555 wxLogMessage(_T("\t... more selected items snipped..."));
556 break;
557 }
558
559 item = m_listCtrl->GetNextItem(item, wxLIST_NEXT_ALL,
560 wxLIST_STATE_SELECTED);
561 }
562}
563
c71963cd 564void MyFrame::OnShowColInfo(wxCommandEvent& WXUNUSED(event))
f6bcfd97
BP
565{
566 int count = m_listCtrl->GetColumnCount();
4acb6ca6 567 wxLogMessage(wxT("%d columns:"), count);
f6bcfd97
BP
568 for ( int c = 0; c < count; c++ )
569 {
4acb6ca6 570 wxLogMessage(wxT("\tcolumn %d has width %d"), c,
f6bcfd97
BP
571 m_listCtrl->GetColumnWidth(c));
572 }
573}
574
575void MyFrame::OnUpdateShowColInfo(wxUpdateUIEvent& event)
576{
577 event.Enable( (m_listCtrl->GetWindowStyleFlag() & wxLC_REPORT) != 0 );
578}
579
7b848b0d
VZ
580void MyFrame::OnToggleMultiSel(wxCommandEvent& WXUNUSED(event))
581{
7b848b0d
VZ
582 long flags = m_listCtrl->GetWindowStyleFlag();
583 if ( flags & wxLC_SINGLE_SEL )
776a33cf 584 flags &= ~wxLC_SINGLE_SEL;
7b848b0d 585 else
776a33cf
VZ
586 flags |= wxLC_SINGLE_SEL;
587
4acb6ca6 588 m_logWindow->WriteText(wxString::Format(wxT("Current selection mode: %sle\n"),
efe23391 589 (flags & wxLC_SINGLE_SEL) ? _T("sing") : _T("multip")));
7b848b0d 590
776a33cf 591 RecreateList(flags);
7b848b0d
VZ
592}
593
11f26ea0
VZ
594void MyFrame::OnSetFgColour(wxCommandEvent& WXUNUSED(event))
595{
596 m_listCtrl->SetForegroundColour(wxGetColourFromUser(this));
597 m_listCtrl->Refresh();
598}
599
600void MyFrame::OnSetBgColour(wxCommandEvent& WXUNUSED(event))
601{
602 m_listCtrl->SetBackgroundColour(wxGetColourFromUser(this));
603 m_listCtrl->Refresh();
604}
605
cf1dfa6b
VZ
606void MyFrame::OnAdd(wxCommandEvent& WXUNUSED(event))
607{
608 m_listCtrl->InsertItem(m_listCtrl->GetItemCount(), _T("Appended item"));
609}
610
efe23391
VZ
611void MyFrame::OnEdit(wxCommandEvent& WXUNUSED(event))
612{
613 long itemCur = m_listCtrl->GetNextItem(-1, wxLIST_NEXT_ALL,
614 wxLIST_STATE_FOCUSED);
615
616 if ( itemCur != -1 )
617 {
618 m_listCtrl->EditLabel(itemCur);
619 }
620 else
621 {
622 m_logWindow->WriteText(_T("No item to edit"));
623 }
624}
625
bec0a261
VZ
626void MyFrame::OnDelete(wxCommandEvent& WXUNUSED(event))
627{
628 if ( m_listCtrl->GetItemCount() )
629 {
630 m_listCtrl->DeleteItem(0);
631 }
632 else
633 {
efe23391 634 m_logWindow->WriteText(_T("Nothing to delete"));
bec0a261
VZ
635 }
636}
637
5ea47806
VZ
638void MyFrame::OnDeleteAll(wxCommandEvent& WXUNUSED(event))
639{
81278df2 640 wxStopWatch sw;
5ea47806 641
5ea47806
VZ
642 m_listCtrl->DeleteAllItems();
643
81278df2
VZ
644 m_logWindow->WriteText(wxString::Format(_T("Deleting %d items took %ld ms\n"),
645 m_listCtrl->GetItemCount(),
646 sw.Time()));
457814b5
JS
647}
648
649// MyListCtrl
650
614391dc
VZ
651void MyListCtrl::OnCacheHint(wxListEvent& event)
652{
4acb6ca6 653 wxLogMessage( wxT("OnCacheHint: cache items %ld..%ld"),
614391dc
VZ
654 event.GetCacheFrom(), event.GetCacheTo() );
655}
656
6f806543
VZ
657void MyListCtrl::SetColumnImage(int col, int image)
658{
659 wxListItem item;
660 item.SetMask(wxLIST_MASK_IMAGE);
661 item.SetImage(image);
662 SetColumn(col, item);
663}
664
8636aed8
RR
665void MyListCtrl::OnColClick(wxListEvent& event)
666{
6f806543
VZ
667 int col = event.GetColumn();
668 SetColumnImage(col, 0);
669
670 wxLogMessage( wxT("OnColumnClick at %d."), col );
8636aed8
RR
671}
672
11358d39
VZ
673void MyListCtrl::OnColRightClick(wxListEvent& event)
674{
6f806543 675 int col = event.GetColumn();
62313c27
VZ
676 if ( col != -1 )
677 {
678 SetColumnImage(col, -1);
679 }
6f806543 680
11358d39
VZ
681 wxLogMessage( wxT("OnColumnRightClick at %d."), event.GetColumn() );
682}
683
2b5f62a0
VZ
684void MyListCtrl::LogColEvent(const wxListEvent& event, const wxChar *name)
685{
686 const int col = event.GetColumn();
687
688 wxLogMessage(wxT("%s: column %d (width = %d or %d)."),
689 name,
690 col,
691 event.GetItem().GetWidth(),
692 GetColumnWidth(col));
693}
694
11358d39
VZ
695void MyListCtrl::OnColBeginDrag(wxListEvent& event)
696{
2b5f62a0 697 LogColEvent( event, wxT("OnColBeginDrag") );
355f2407
VZ
698
699 if ( event.GetColumn() == 0 )
700 {
701 wxLogMessage(_T("Resizing this column shouldn't work."));
702
703 event.Veto();
704 }
11358d39
VZ
705}
706
707void MyListCtrl::OnColDragging(wxListEvent& event)
708{
2b5f62a0 709 LogColEvent( event, wxT("OnColDragging") );
11358d39
VZ
710}
711
712void MyListCtrl::OnColEndDrag(wxListEvent& event)
713{
2b5f62a0 714 LogColEvent( event, wxT("OnColEndDrag") );
11358d39
VZ
715}
716
fd9811b1 717void MyListCtrl::OnBeginDrag(wxListEvent& event)
457814b5 718{
6dc34ebb
VZ
719 const wxPoint& pt = event.m_pointDrag;
720
721 int flags;
722 wxLogMessage( wxT("OnBeginDrag at (%d, %d), item %ld."),
723 pt.x, pt.y, HitTest(pt, flags) );
457814b5
JS
724}
725
fd9811b1 726void MyListCtrl::OnBeginRDrag(wxListEvent& event)
457814b5 727{
4acb6ca6 728 wxLogMessage( wxT("OnBeginRDrag at %d,%d."),
c41ea66a 729 event.m_pointDrag.x, event.m_pointDrag.y );
457814b5
JS
730}
731
fd9811b1 732void MyListCtrl::OnBeginLabelEdit(wxListEvent& event)
457814b5 733{
4acb6ca6 734 wxLogMessage( wxT("OnBeginLabelEdit: %s"), event.m_item.m_text.c_str());
457814b5
JS
735}
736
fd9811b1 737void MyListCtrl::OnEndLabelEdit(wxListEvent& event)
457814b5 738{
1bf37bf5
VZ
739 wxLogMessage( wxT("OnEndLabelEdit: %s"),
740 event.IsEditCancelled() ? _T("[cancelled]")
741 : event.m_item.m_text.c_str());
457814b5
JS
742}
743
efbb7287 744void MyListCtrl::OnDeleteItem(wxListEvent& event)
457814b5 745{
c41ea66a 746 LogEvent(event, _T("OnDeleteItem"));
457814b5
JS
747}
748
c41ea66a 749void MyListCtrl::OnDeleteAllItems(wxListEvent& event)
12c1b46a 750{
c41ea66a 751 LogEvent(event, _T("OnDeleteAllItems"));
12c1b46a
RR
752}
753
fd9811b1 754void MyListCtrl::OnGetInfo(wxListEvent& event)
457814b5 755{
c41ea66a 756 wxString msg;
5ea47806 757
efe23391 758 msg << _T("OnGetInfo (") << event.m_item.m_itemId << _T(", ") << event.m_item.m_col << _T(")");
5ea47806 759 if ( event.m_item.m_mask & wxLIST_MASK_STATE )
efe23391 760 msg << _T(" wxLIST_MASK_STATE");
5ea47806 761 if ( event.m_item.m_mask & wxLIST_MASK_TEXT )
efe23391 762 msg << _T(" wxLIST_MASK_TEXT");
5ea47806 763 if ( event.m_item.m_mask & wxLIST_MASK_IMAGE )
efe23391 764 msg << _T(" wxLIST_MASK_IMAGE");
5ea47806 765 if ( event.m_item.m_mask & wxLIST_MASK_DATA )
efe23391 766 msg << _T(" wxLIST_MASK_DATA");
5ea47806 767 if ( event.m_item.m_mask & wxLIST_SET_ITEM )
efe23391 768 msg << _T(" wxLIST_SET_ITEM");
5ea47806 769 if ( event.m_item.m_mask & wxLIST_MASK_WIDTH )
efe23391 770 msg << _T(" wxLIST_MASK_WIDTH");
5ea47806 771 if ( event.m_item.m_mask & wxLIST_MASK_FORMAT )
efe23391 772 msg << _T(" wxLIST_MASK_WIDTH");
5ea47806
VZ
773
774 if ( event.m_item.m_mask & wxLIST_MASK_TEXT )
775 {
efe23391 776 event.m_item.m_text = _T("My callback text");
5ea47806 777 }
c41ea66a
VZ
778
779 wxLogMessage(msg);
457814b5
JS
780}
781
efbb7287 782void MyListCtrl::OnSetInfo(wxListEvent& event)
457814b5 783{
c41ea66a 784 LogEvent(event, _T("OnSetInfo"));
457814b5
JS
785}
786
74b31181 787void MyListCtrl::OnSelected(wxListEvent& event)
457814b5 788{
c41ea66a 789 LogEvent(event, _T("OnSelected"));
457814b5 790
40779a03 791 if ( GetWindowStyle() & wxLC_REPORT )
74b31181 792 {
40779a03
VZ
793 wxListItem info;
794 info.m_itemId = event.m_itemIndex;
795 info.m_col = 1;
796 info.m_mask = wxLIST_MASK_TEXT;
797 if ( GetItem(info) )
798 {
4acb6ca6 799 wxLogMessage(wxT("Value of the 2nd field of the selected item: %s"),
c41ea66a 800 info.m_text.c_str());
40779a03
VZ
801 }
802 else
803 {
4acb6ca6 804 wxFAIL_MSG(wxT("wxListCtrl::GetItem() failed"));
40779a03 805 }
74b31181 806 }
457814b5
JS
807}
808
efbb7287 809void MyListCtrl::OnDeselected(wxListEvent& event)
457814b5 810{
c41ea66a 811 LogEvent(event, _T("OnDeselected"));
457814b5
JS
812}
813
efbb7287 814void MyListCtrl::OnActivated(wxListEvent& event)
435fe83e 815{
c41ea66a 816 LogEvent(event, _T("OnActivated"));
435fe83e
RR
817}
818
0ddefeb0
VZ
819void MyListCtrl::OnFocused(wxListEvent& event)
820{
821 LogEvent(event, _T("OnFocused"));
27770773
VZ
822
823 event.Skip();
0ddefeb0
VZ
824}
825
8e1d4f96 826void MyListCtrl::OnListKeyDown(wxListEvent& event)
457814b5 827{
2936eaf0 828 switch ( event.GetKeyCode() )
5cd89174 829 {
2d33aec9
VZ
830 case 'c': // colorize
831 case 'C':
6c02c329
VZ
832 {
833 wxListItem info;
834 info.m_itemId = event.GetIndex();
835 GetItem(info);
836
837 wxListItemAttr *attr = info.GetAttributes();
838 if ( !attr || !attr->HasTextColour() )
839 {
840 info.SetTextColour(*wxCYAN);
841
842 SetItem(info);
2d33aec9
VZ
843
844 RefreshItem(info.m_itemId);
6c02c329
VZ
845 }
846 }
847 break;
848
2d33aec9
VZ
849 case 'n': // next
850 case 'N':
851 {
852 long item = GetNextItem(-1,
853 wxLIST_NEXT_ALL, wxLIST_STATE_FOCUSED);
854 if ( item++ == GetItemCount() - 1 )
855 {
856 item = 0;
857 }
858
859 wxLogMessage(_T("Focusing item %ld"), item);
860
861 SetItemState(item, wxLIST_STATE_FOCUSED, wxLIST_STATE_FOCUSED);
862 EnsureVisible(item);
863 }
864 break;
865
5cd89174 866 case WXK_DELETE:
91c6cc0e
VZ
867 {
868 long item = GetNextItem(-1,
869 wxLIST_NEXT_ALL, wxLIST_STATE_SELECTED);
870 while ( item != -1 )
871 {
872 DeleteItem(item);
873
874 wxLogMessage(_T("Item %ld deleted"), item);
f6bcfd97 875
91c6cc0e
VZ
876 // -1 because the indices were shifted by DeleteItem()
877 item = GetNextItem(item - 1,
878 wxLIST_NEXT_ALL, wxLIST_STATE_SELECTED);
879 }
880 }
5cd89174
VZ
881 break;
882
883 case WXK_INSERT:
884 if ( GetWindowStyle() & wxLC_REPORT )
885 {
886 if ( GetWindowStyle() & wxLC_VIRTUAL )
887 {
888 SetItemCount(GetItemCount() + 1);
889 }
890 else // !virtual
891 {
892 InsertItemInReportView(event.GetIndex());
893 }
894 }
895 //else: fall through
896
897 default:
898 LogEvent(event, _T("OnListKeyDown"));
899
900 event.Skip();
901 }
f6bcfd97
BP
902}
903
904void MyListCtrl::OnChar(wxKeyEvent& event)
905{
906 wxLogMessage(_T("Got char event."));
907
2d33aec9
VZ
908 switch ( event.GetKeyCode() )
909 {
910 case 'n':
911 case 'N':
912 case 'c':
913 case 'C':
914 // these are the keys we process ourselves
915 break;
916
917 default:
918 event.Skip();
919 }
457814b5
JS
920}
921
c41ea66a
VZ
922void MyListCtrl::LogEvent(const wxListEvent& event, const wxChar *eventName)
923{
f6bcfd97
BP
924 wxLogMessage(_T("Item %ld: %s (item text = %s, data = %ld)"),
925 event.GetIndex(), eventName,
926 event.GetText().c_str(), event.GetData());
c41ea66a 927}
435fe83e 928
98ec9dbe
VZ
929wxString MyListCtrl::OnGetItemText(long item, long column) const
930{
931 return wxString::Format(_T("Column %ld of item %ld"), column, item);
932}
933
c71963cd 934int MyListCtrl::OnGetItemImage(long WXUNUSED(item)) const
98ec9dbe
VZ
935{
936 return 0;
937}
938
6c02c329
VZ
939wxListItemAttr *MyListCtrl::OnGetItemAttr(long item) const
940{
941 return item % 2 ? NULL : (wxListItemAttr *)&m_attr;
942}
943
5cd89174
VZ
944void MyListCtrl::InsertItemInReportView(int i)
945{
946 wxString buf;
947 buf.Printf(_T("This is item %d"), i);
948 long tmp = InsertItem(i, buf, 0);
949 SetItemData(tmp, i);
950
951 buf.Printf(_T("Col 1, item %d"), i);
952 SetItem(i, 1, buf);
953
954 buf.Printf(_T("Item %d in column 2"), i);
955 SetItem(i, 2, buf);
956}
957