]> git.saurik.com Git - wxWidgets.git/blame - samples/listctrl/listtest.cpp
Changed back DEFAULT_wxUSE_OPENGL to no so configure no longer completely
[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
fde548e2
RN
659 size_t itemCount = m_listCtrl->GetItemCount();
660
5ea47806
VZ
661 m_listCtrl->DeleteAllItems();
662
81278df2 663 m_logWindow->WriteText(wxString::Format(_T("Deleting %d items took %ld ms\n"),
fde548e2 664 itemCount,
81278df2 665 sw.Time()));
457814b5
JS
666}
667
668// MyListCtrl
669
614391dc
VZ
670void MyListCtrl::OnCacheHint(wxListEvent& event)
671{
4acb6ca6 672 wxLogMessage( wxT("OnCacheHint: cache items %ld..%ld"),
614391dc
VZ
673 event.GetCacheFrom(), event.GetCacheTo() );
674}
675
6f806543
VZ
676void MyListCtrl::SetColumnImage(int col, int image)
677{
678 wxListItem item;
679 item.SetMask(wxLIST_MASK_IMAGE);
680 item.SetImage(image);
681 SetColumn(col, item);
682}
683
8636aed8
RR
684void MyListCtrl::OnColClick(wxListEvent& event)
685{
6f806543
VZ
686 int col = event.GetColumn();
687 SetColumnImage(col, 0);
688
689 wxLogMessage( wxT("OnColumnClick at %d."), col );
8636aed8
RR
690}
691
11358d39
VZ
692void MyListCtrl::OnColRightClick(wxListEvent& event)
693{
6f806543 694 int col = event.GetColumn();
62313c27
VZ
695 if ( col != -1 )
696 {
697 SetColumnImage(col, -1);
698 }
6f806543 699
77ace0c5
WS
700 // Show popupmenu at position
701 wxMenu menu(wxT("Test"));
702 menu.Append(LIST_ABOUT, _T("&About"));
703 PopupMenu(&menu, event.GetPoint());
704
11358d39
VZ
705 wxLogMessage( wxT("OnColumnRightClick at %d."), event.GetColumn() );
706}
707
2b5f62a0
VZ
708void MyListCtrl::LogColEvent(const wxListEvent& event, const wxChar *name)
709{
710 const int col = event.GetColumn();
711
712 wxLogMessage(wxT("%s: column %d (width = %d or %d)."),
713 name,
714 col,
715 event.GetItem().GetWidth(),
716 GetColumnWidth(col));
717}
718
11358d39
VZ
719void MyListCtrl::OnColBeginDrag(wxListEvent& event)
720{
2b5f62a0 721 LogColEvent( event, wxT("OnColBeginDrag") );
355f2407
VZ
722
723 if ( event.GetColumn() == 0 )
724 {
725 wxLogMessage(_T("Resizing this column shouldn't work."));
726
727 event.Veto();
728 }
11358d39
VZ
729}
730
731void MyListCtrl::OnColDragging(wxListEvent& event)
732{
2b5f62a0 733 LogColEvent( event, wxT("OnColDragging") );
11358d39
VZ
734}
735
736void MyListCtrl::OnColEndDrag(wxListEvent& event)
737{
2b5f62a0 738 LogColEvent( event, wxT("OnColEndDrag") );
11358d39
VZ
739}
740
fd9811b1 741void MyListCtrl::OnBeginDrag(wxListEvent& event)
457814b5 742{
6dc34ebb
VZ
743 const wxPoint& pt = event.m_pointDrag;
744
745 int flags;
746 wxLogMessage( wxT("OnBeginDrag at (%d, %d), item %ld."),
747 pt.x, pt.y, HitTest(pt, flags) );
457814b5
JS
748}
749
fd9811b1 750void MyListCtrl::OnBeginRDrag(wxListEvent& event)
457814b5 751{
4acb6ca6 752 wxLogMessage( wxT("OnBeginRDrag at %d,%d."),
c41ea66a 753 event.m_pointDrag.x, event.m_pointDrag.y );
457814b5
JS
754}
755
fd9811b1 756void MyListCtrl::OnBeginLabelEdit(wxListEvent& event)
457814b5 757{
4acb6ca6 758 wxLogMessage( wxT("OnBeginLabelEdit: %s"), event.m_item.m_text.c_str());
457814b5
JS
759}
760
fd9811b1 761void MyListCtrl::OnEndLabelEdit(wxListEvent& event)
457814b5 762{
1bf37bf5
VZ
763 wxLogMessage( wxT("OnEndLabelEdit: %s"),
764 event.IsEditCancelled() ? _T("[cancelled]")
765 : event.m_item.m_text.c_str());
457814b5
JS
766}
767
efbb7287 768void MyListCtrl::OnDeleteItem(wxListEvent& event)
457814b5 769{
c41ea66a 770 LogEvent(event, _T("OnDeleteItem"));
457814b5
JS
771}
772
c41ea66a 773void MyListCtrl::OnDeleteAllItems(wxListEvent& event)
12c1b46a 774{
c41ea66a 775 LogEvent(event, _T("OnDeleteAllItems"));
12c1b46a
RR
776}
777
3614a8dc 778#if WXWIN_COMPATIBILITY_2_4
fd9811b1 779void MyListCtrl::OnGetInfo(wxListEvent& event)
457814b5 780{
c41ea66a 781 wxString msg;
5ea47806 782
efe23391 783 msg << _T("OnGetInfo (") << event.m_item.m_itemId << _T(", ") << event.m_item.m_col << _T(")");
5ea47806 784 if ( event.m_item.m_mask & wxLIST_MASK_STATE )
efe23391 785 msg << _T(" wxLIST_MASK_STATE");
5ea47806 786 if ( event.m_item.m_mask & wxLIST_MASK_TEXT )
efe23391 787 msg << _T(" wxLIST_MASK_TEXT");
5ea47806 788 if ( event.m_item.m_mask & wxLIST_MASK_IMAGE )
efe23391 789 msg << _T(" wxLIST_MASK_IMAGE");
5ea47806 790 if ( event.m_item.m_mask & wxLIST_MASK_DATA )
efe23391 791 msg << _T(" wxLIST_MASK_DATA");
5ea47806 792 if ( event.m_item.m_mask & wxLIST_SET_ITEM )
efe23391 793 msg << _T(" wxLIST_SET_ITEM");
5ea47806 794 if ( event.m_item.m_mask & wxLIST_MASK_WIDTH )
efe23391 795 msg << _T(" wxLIST_MASK_WIDTH");
5ea47806 796 if ( event.m_item.m_mask & wxLIST_MASK_FORMAT )
efe23391 797 msg << _T(" wxLIST_MASK_WIDTH");
5ea47806
VZ
798
799 if ( event.m_item.m_mask & wxLIST_MASK_TEXT )
800 {
efe23391 801 event.m_item.m_text = _T("My callback text");
5ea47806 802 }
c41ea66a
VZ
803
804 wxLogMessage(msg);
457814b5
JS
805}
806
efbb7287 807void MyListCtrl::OnSetInfo(wxListEvent& event)
457814b5 808{
c41ea66a 809 LogEvent(event, _T("OnSetInfo"));
457814b5 810}
3614a8dc 811#endif
457814b5 812
74b31181 813void MyListCtrl::OnSelected(wxListEvent& event)
457814b5 814{
c41ea66a 815 LogEvent(event, _T("OnSelected"));
457814b5 816
40779a03 817 if ( GetWindowStyle() & wxLC_REPORT )
74b31181 818 {
40779a03
VZ
819 wxListItem info;
820 info.m_itemId = event.m_itemIndex;
821 info.m_col = 1;
822 info.m_mask = wxLIST_MASK_TEXT;
823 if ( GetItem(info) )
824 {
4acb6ca6 825 wxLogMessage(wxT("Value of the 2nd field of the selected item: %s"),
c41ea66a 826 info.m_text.c_str());
40779a03
VZ
827 }
828 else
829 {
4acb6ca6 830 wxFAIL_MSG(wxT("wxListCtrl::GetItem() failed"));
40779a03 831 }
74b31181 832 }
457814b5
JS
833}
834
efbb7287 835void MyListCtrl::OnDeselected(wxListEvent& event)
457814b5 836{
c41ea66a 837 LogEvent(event, _T("OnDeselected"));
457814b5
JS
838}
839
efbb7287 840void MyListCtrl::OnActivated(wxListEvent& event)
435fe83e 841{
c41ea66a 842 LogEvent(event, _T("OnActivated"));
435fe83e
RR
843}
844
0ddefeb0
VZ
845void MyListCtrl::OnFocused(wxListEvent& event)
846{
847 LogEvent(event, _T("OnFocused"));
27770773
VZ
848
849 event.Skip();
0ddefeb0
VZ
850}
851
8e1d4f96 852void MyListCtrl::OnListKeyDown(wxListEvent& event)
457814b5 853{
2936eaf0 854 switch ( event.GetKeyCode() )
5cd89174 855 {
2d33aec9
VZ
856 case 'c': // colorize
857 case 'C':
6c02c329
VZ
858 {
859 wxListItem info;
860 info.m_itemId = event.GetIndex();
861 GetItem(info);
862
863 wxListItemAttr *attr = info.GetAttributes();
864 if ( !attr || !attr->HasTextColour() )
865 {
866 info.SetTextColour(*wxCYAN);
867
868 SetItem(info);
2d33aec9
VZ
869
870 RefreshItem(info.m_itemId);
6c02c329
VZ
871 }
872 }
873 break;
874
2d33aec9
VZ
875 case 'n': // next
876 case 'N':
877 {
878 long item = GetNextItem(-1,
879 wxLIST_NEXT_ALL, wxLIST_STATE_FOCUSED);
880 if ( item++ == GetItemCount() - 1 )
881 {
882 item = 0;
883 }
884
885 wxLogMessage(_T("Focusing item %ld"), item);
886
887 SetItemState(item, wxLIST_STATE_FOCUSED, wxLIST_STATE_FOCUSED);
888 EnsureVisible(item);
889 }
890 break;
891
5cd89174 892 case WXK_DELETE:
91c6cc0e
VZ
893 {
894 long item = GetNextItem(-1,
895 wxLIST_NEXT_ALL, wxLIST_STATE_SELECTED);
896 while ( item != -1 )
897 {
898 DeleteItem(item);
899
900 wxLogMessage(_T("Item %ld deleted"), item);
f6bcfd97 901
91c6cc0e
VZ
902 // -1 because the indices were shifted by DeleteItem()
903 item = GetNextItem(item - 1,
904 wxLIST_NEXT_ALL, wxLIST_STATE_SELECTED);
905 }
906 }
5cd89174
VZ
907 break;
908
909 case WXK_INSERT:
910 if ( GetWindowStyle() & wxLC_REPORT )
911 {
912 if ( GetWindowStyle() & wxLC_VIRTUAL )
913 {
914 SetItemCount(GetItemCount() + 1);
915 }
916 else // !virtual
917 {
918 InsertItemInReportView(event.GetIndex());
919 }
920 }
921 //else: fall through
922
923 default:
924 LogEvent(event, _T("OnListKeyDown"));
925
926 event.Skip();
927 }
f6bcfd97
BP
928}
929
930void MyListCtrl::OnChar(wxKeyEvent& event)
931{
932 wxLogMessage(_T("Got char event."));
933
2d33aec9
VZ
934 switch ( event.GetKeyCode() )
935 {
936 case 'n':
937 case 'N':
938 case 'c':
939 case 'C':
940 // these are the keys we process ourselves
941 break;
942
943 default:
944 event.Skip();
945 }
457814b5
JS
946}
947
c41ea66a
VZ
948void MyListCtrl::LogEvent(const wxListEvent& event, const wxChar *eventName)
949{
f6bcfd97
BP
950 wxLogMessage(_T("Item %ld: %s (item text = %s, data = %ld)"),
951 event.GetIndex(), eventName,
952 event.GetText().c_str(), event.GetData());
c41ea66a 953}
435fe83e 954
98ec9dbe
VZ
955wxString MyListCtrl::OnGetItemText(long item, long column) const
956{
957 return wxString::Format(_T("Column %ld of item %ld"), column, item);
958}
959
c71963cd 960int MyListCtrl::OnGetItemImage(long WXUNUSED(item)) const
98ec9dbe
VZ
961{
962 return 0;
963}
964
6c02c329
VZ
965wxListItemAttr *MyListCtrl::OnGetItemAttr(long item) const
966{
967 return item % 2 ? NULL : (wxListItemAttr *)&m_attr;
968}
969
5cd89174
VZ
970void MyListCtrl::InsertItemInReportView(int i)
971{
972 wxString buf;
973 buf.Printf(_T("This is item %d"), i);
974 long tmp = InsertItem(i, buf, 0);
975 SetItemData(tmp, i);
976
977 buf.Printf(_T("Col 1, item %d"), i);
978 SetItem(i, 1, buf);
979
980 buf.Printf(_T("Item %d in column 2"), i);
981 SetItem(i, 2, buf);
982}
983