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