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