]> git.saurik.com Git - wxWidgets.git/blame - samples/listctrl/listtest.cpp
do not exceed the 80 column limit when printing the list of available libraries
[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
457814b5
JS
12// For compilers that support precompilation, includes "wx/wx.h".
13#include "wx/wxprec.h"
14
15#ifdef __BORLANDC__
16#pragma hdrstop
17#endif
18
19#ifndef WX_PRECOMP
20#include "wx/wx.h"
21#endif
22
5f4d35b8 23#if !defined(__WXMSW__) && !defined(__WXPM__)
c41ea66a 24 #include "mondrian.xpm"
5f4d35b8 25#endif
c41ea66a 26
5f4d35b8 27#ifndef __WXMSW__
c41ea66a
VZ
28 #include "bitmaps/toolbrai.xpm"
29 #include "bitmaps/toolchar.xpm"
30 #include "bitmaps/tooldata.xpm"
31 #include "bitmaps/toolnote.xpm"
32 #include "bitmaps/tooltodo.xpm"
33 #include "bitmaps/toolchec.xpm"
34 #include "bitmaps/toolgame.xpm"
35 #include "bitmaps/tooltime.xpm"
36 #include "bitmaps/toolword.xpm"
37 #include "bitmaps/small1.xpm"
907789a0
RR
38#endif
39
2e8a1588 40#include "wx/imaglist.h"
457814b5 41#include "wx/listctrl.h"
81278df2 42#include "wx/timer.h" // for wxStopWatch
11f26ea0 43#include "wx/colordlg.h" // for wxGetColourFromUser
107ff668 44#include "wx/settings.h"
2458daa7 45#include "wx/sysopt.h"
11f26ea0 46
457814b5
JS
47#include "listtest.h"
48
a0d0799b
FM
49
50// ----------------------------------------------------------------------------
51// Constants and globals
52// ----------------------------------------------------------------------------
53
b0401bea
VZ
54const wxChar *SMALL_VIRTUAL_VIEW_ITEMS[][2] =
55{
56 { _T("Cat"), _T("meow") },
57 { _T("Cow"), _T("moo") },
58 { _T("Crow"), _T("caw") },
59 { _T("Dog"), _T("woof") },
60 { _T("Duck"), _T("quack") },
61 { _T("Mouse"), _T("squeak") },
62 { _T("Owl"), _T("hoo") },
63 { _T("Pig"), _T("oink") },
64 { _T("Pigeon"), _T("coo") },
65 { _T("Sheep"), _T("baaah") },
66};
67
a0d0799b
FM
68// number of items in list/report view
69static const int NUM_ITEMS = 10;
70
71// number of items in icon/small icon view
72static const int NUM_ICONS = 9;
73
74int wxCALLBACK MyCompareFunction(long item1, long item2, long WXUNUSED(sortData))
75{
76 // inverse the order
77 if (item1 < item2)
78 return -1;
79 if (item1 > item2)
80 return 1;
81
82 return 0;
83}
84
85
86// ----------------------------------------------------------------------------
87// MyApp
88// ----------------------------------------------------------------------------
89
90IMPLEMENT_APP(MyApp)
91
92// `Main program' equivalent, creating windows and returning main app frame
93bool MyApp::OnInit()
94{
95 if ( !wxApp::OnInit() )
96 return false;
97
98 // Create the main frame window
99 MyFrame *frame = new MyFrame(wxT("wxListCtrl Test"));
100
101 // Show the frame
102 frame->Show(true);
103
104 SetTopWindow(frame);
105
106 return true;
107}
108
109
110
111// ----------------------------------------------------------------------------
112// MyFrame
113// ----------------------------------------------------------------------------
b0401bea 114
457814b5 115BEGIN_EVENT_TABLE(MyFrame, wxFrame)
98ec9dbe
VZ
116 EVT_SIZE(MyFrame::OnSize)
117
5ea47806
VZ
118 EVT_MENU(LIST_QUIT, MyFrame::OnQuit)
119 EVT_MENU(LIST_ABOUT, MyFrame::OnAbout)
120 EVT_MENU(LIST_LIST_VIEW, MyFrame::OnListView)
121 EVT_MENU(LIST_REPORT_VIEW, MyFrame::OnReportView)
122 EVT_MENU(LIST_ICON_VIEW, MyFrame::OnIconView)
123 EVT_MENU(LIST_ICON_TEXT_VIEW, MyFrame::OnIconTextView)
124 EVT_MENU(LIST_SMALL_ICON_VIEW, MyFrame::OnSmallIconView)
125 EVT_MENU(LIST_SMALL_ICON_TEXT_VIEW, MyFrame::OnSmallIconTextView)
98ec9dbe 126 EVT_MENU(LIST_VIRTUAL_VIEW, MyFrame::OnVirtualView)
b0401bea 127 EVT_MENU(LIST_SMALL_VIRTUAL_VIEW, MyFrame::OnSmallVirtualView)
98ec9dbe 128
6ef2b230 129 EVT_MENU(LIST_GOTO, MyFrame::OnGoTo)
88b792af 130 EVT_MENU(LIST_FOCUS_LAST, MyFrame::OnFocusLast)
58b3bdc9 131 EVT_MENU(LIST_TOGGLE_FIRST, MyFrame::OnToggleFirstSel)
5ea47806
VZ
132 EVT_MENU(LIST_DESELECT_ALL, MyFrame::OnDeselectAll)
133 EVT_MENU(LIST_SELECT_ALL, MyFrame::OnSelectAll)
bec0a261 134 EVT_MENU(LIST_DELETE, MyFrame::OnDelete)
cf1dfa6b 135 EVT_MENU(LIST_ADD, MyFrame::OnAdd)
efe23391 136 EVT_MENU(LIST_EDIT, MyFrame::OnEdit)
5ea47806 137 EVT_MENU(LIST_DELETE_ALL, MyFrame::OnDeleteAll)
fa5f6926 138 EVT_MENU(LIST_SORT, MyFrame::OnSort)
11f26ea0
VZ
139 EVT_MENU(LIST_SET_FG_COL, MyFrame::OnSetFgColour)
140 EVT_MENU(LIST_SET_BG_COL, MyFrame::OnSetBgColour)
7b848b0d 141 EVT_MENU(LIST_TOGGLE_MULTI_SEL, MyFrame::OnToggleMultiSel)
f6bcfd97 142 EVT_MENU(LIST_SHOW_COL_INFO, MyFrame::OnShowColInfo)
b54e41c5 143 EVT_MENU(LIST_SHOW_SEL_INFO, MyFrame::OnShowSelInfo)
929b7901 144 EVT_MENU(LIST_SHOW_VIEW_RECT, MyFrame::OnShowViewRect)
80cc5fc7
VZ
145#ifdef wxHAS_LISTCTRL_COLUMN_ORDER
146 EVT_MENU(LIST_SET_COL_ORDER, MyFrame::OnSetColOrder)
147 EVT_MENU(LIST_GET_COL_ORDER, MyFrame::OnGetColOrder)
148#endif // wxHAS_LISTCTRL_COLUMN_ORDER
c5c528fc
VZ
149 EVT_MENU(LIST_FREEZE, MyFrame::OnFreeze)
150 EVT_MENU(LIST_THAW, MyFrame::OnThaw)
494ab5de 151 EVT_MENU(LIST_TOGGLE_LINES, MyFrame::OnToggleLines)
2458daa7 152 EVT_MENU(LIST_MAC_USE_GENERIC, MyFrame::OnToggleMacUseGeneric)
98ec9dbe 153
f6bcfd97 154 EVT_UPDATE_UI(LIST_SHOW_COL_INFO, MyFrame::OnUpdateShowColInfo)
5f4d35b8 155 EVT_UPDATE_UI(LIST_TOGGLE_MULTI_SEL, MyFrame::OnUpdateToggleMultiSel)
457814b5
JS
156END_EVENT_TABLE()
157
457814b5 158// My frame constructor
107ff668 159MyFrame::MyFrame(const wxChar *title)
37e8bdb8 160 : wxFrame(NULL, wxID_ANY, title, wxDefaultPosition, wxSize(800, 500))
457814b5 161{
b0401bea
VZ
162 m_listCtrl = NULL;
163 m_logWindow = NULL;
37e8bdb8 164 m_simpleListCtrl = NULL;
b0401bea 165 m_smallVirtual = false;
c41ea66a
VZ
166
167 // Give it an icon
168 SetIcon( wxICON(mondrian) );
169
170 // Make an image list containing large icons
1550e402
WS
171 m_imageListNormal = new wxImageList(32, 32, true);
172 m_imageListSmall = new wxImageList(16, 16, true);
c41ea66a
VZ
173
174#ifdef __WXMSW__
42ed7532
MB
175 m_imageListNormal->Add( wxIcon(_T("icon1"), wxBITMAP_TYPE_ICO_RESOURCE) );
176 m_imageListNormal->Add( wxIcon(_T("icon2"), wxBITMAP_TYPE_ICO_RESOURCE) );
177 m_imageListNormal->Add( wxIcon(_T("icon3"), wxBITMAP_TYPE_ICO_RESOURCE) );
178 m_imageListNormal->Add( wxIcon(_T("icon4"), wxBITMAP_TYPE_ICO_RESOURCE) );
179 m_imageListNormal->Add( wxIcon(_T("icon5"), wxBITMAP_TYPE_ICO_RESOURCE) );
180 m_imageListNormal->Add( wxIcon(_T("icon6"), wxBITMAP_TYPE_ICO_RESOURCE) );
181 m_imageListNormal->Add( wxIcon(_T("icon7"), wxBITMAP_TYPE_ICO_RESOURCE) );
182 m_imageListNormal->Add( wxIcon(_T("icon8"), wxBITMAP_TYPE_ICO_RESOURCE) );
183 m_imageListNormal->Add( wxIcon(_T("icon9"), wxBITMAP_TYPE_ICO_RESOURCE) );
184
185 m_imageListSmall->Add( wxIcon(_T("iconsmall"), wxBITMAP_TYPE_ICO_RESOURCE) );
c41ea66a
VZ
186
187#else
188 m_imageListNormal->Add( wxIcon( toolbrai_xpm ) );
189 m_imageListNormal->Add( wxIcon( toolchar_xpm ) );
190 m_imageListNormal->Add( wxIcon( tooldata_xpm ) );
191 m_imageListNormal->Add( wxIcon( toolnote_xpm ) );
192 m_imageListNormal->Add( wxIcon( tooltodo_xpm ) );
193 m_imageListNormal->Add( wxIcon( toolchec_xpm ) );
194 m_imageListNormal->Add( wxIcon( toolgame_xpm ) );
195 m_imageListNormal->Add( wxIcon( tooltime_xpm ) );
196 m_imageListNormal->Add( wxIcon( toolword_xpm ) );
197
198 m_imageListSmall->Add( wxIcon( small1_xpm) );
199#endif
200
201 // Make a menubar
202 wxMenu *menuFile = new wxMenu;
98ec9dbe 203 menuFile->Append(LIST_ABOUT, _T("&About"));
c41ea66a 204 menuFile->AppendSeparator();
98ec9dbe 205 menuFile->Append(LIST_QUIT, _T("E&xit\tAlt-X"));
c41ea66a
VZ
206
207 wxMenu *menuView = new wxMenu;
98ec9dbe
VZ
208 menuView->Append(LIST_LIST_VIEW, _T("&List view\tF1"));
209 menuView->Append(LIST_REPORT_VIEW, _T("&Report view\tF2"));
210 menuView->Append(LIST_ICON_VIEW, _T("&Icon view\tF3"));
211 menuView->Append(LIST_ICON_TEXT_VIEW, _T("Icon view with &text\tF4"));
212 menuView->Append(LIST_SMALL_ICON_VIEW, _T("&Small icon view\tF5"));
213 menuView->Append(LIST_SMALL_ICON_TEXT_VIEW, _T("Small icon &view with text\tF6"));
b0401bea
VZ
214 menuView->Append(LIST_VIRTUAL_VIEW, _T("&Virtual view\tF7"));
215 menuView->Append(LIST_SMALL_VIRTUAL_VIEW, _T("Small virtual vie&w\tF8"));
2458daa7
KO
216#ifdef __WXMAC__
217 menuView->AppendCheckItem(LIST_MAC_USE_GENERIC, _T("Mac: Use Generic Control"));
218#endif
c41ea66a
VZ
219
220 wxMenu *menuList = new wxMenu;
6ef2b230 221 menuList->Append(LIST_GOTO, _T("&Go to item #3\tCtrl-3"));
88b792af 222 menuList->Append(LIST_FOCUS_LAST, _T("&Make last item current\tCtrl-L"));
c5c528fc 223 menuList->Append(LIST_TOGGLE_FIRST, _T("To&ggle first item\tCtrl-G"));
98ec9dbe
VZ
224 menuList->Append(LIST_DESELECT_ALL, _T("&Deselect All\tCtrl-D"));
225 menuList->Append(LIST_SELECT_ALL, _T("S&elect All\tCtrl-A"));
b54e41c5 226 menuList->AppendSeparator();
98ec9dbe 227 menuList->Append(LIST_SHOW_COL_INFO, _T("Show &column info\tCtrl-C"));
b54e41c5 228 menuList->Append(LIST_SHOW_SEL_INFO, _T("Show &selected items\tCtrl-S"));
929b7901 229 menuList->Append(LIST_SHOW_VIEW_RECT, _T("Show &view rect"));
80cc5fc7
VZ
230#ifdef wxHAS_LISTCTRL_COLUMN_ORDER
231 menuList->Append(LIST_SET_COL_ORDER, _T("Se&t columns order\tShift-Ctrl-O"));
232 menuList->Append(LIST_GET_COL_ORDER, _T("Show&w columns order\tCtrl-O"));
233#endif // wxHAS_LISTCTRL_COLUMN_ORDER
c41ea66a 234 menuList->AppendSeparator();
d6d9c223 235 menuList->Append(LIST_SORT, _T("Sor&t\tCtrl-T"));
c41ea66a 236 menuList->AppendSeparator();
cf1dfa6b 237 menuList->Append(LIST_ADD, _T("&Append an item\tCtrl-P"));
efe23391 238 menuList->Append(LIST_EDIT, _T("&Edit the item\tCtrl-E"));
cf1dfa6b 239 menuList->Append(LIST_DELETE, _T("&Delete first item\tCtrl-X"));
98ec9dbe 240 menuList->Append(LIST_DELETE_ALL, _T("Delete &all items"));
c41ea66a 241 menuList->AppendSeparator();
c5c528fc
VZ
242 menuList->Append(LIST_FREEZE, _T("Free&ze\tCtrl-Z"));
243 menuList->Append(LIST_THAW, _T("Tha&w\tCtrl-W"));
244 menuList->AppendSeparator();
494ab5de 245 menuList->AppendCheckItem(LIST_TOGGLE_LINES, _T("Toggle &lines\tCtrl-I"));
98ec9dbe 246 menuList->Append(LIST_TOGGLE_MULTI_SEL, _T("&Multiple selection\tCtrl-M"),
1550e402 247 _T("Toggle multiple selection"), true);
c41ea66a
VZ
248
249 wxMenu *menuCol = new wxMenu;
98ec9dbe
VZ
250 menuCol->Append(LIST_SET_FG_COL, _T("&Foreground colour..."));
251 menuCol->Append(LIST_SET_BG_COL, _T("&Background colour..."));
c41ea66a
VZ
252
253 wxMenuBar *menubar = new wxMenuBar;
98ec9dbe
VZ
254 menubar->Append(menuFile, _T("&File"));
255 menubar->Append(menuView, _T("&View"));
256 menubar->Append(menuList, _T("&List"));
257 menubar->Append(menuCol, _T("&Colour"));
c41ea66a
VZ
258 SetMenuBar(menubar);
259
1550e402
WS
260 m_panel = new wxPanel(this, wxID_ANY);
261 m_logWindow = new wxTextCtrl(m_panel, wxID_ANY, wxEmptyString,
c41ea66a 262 wxDefaultPosition, wxDefaultSize,
e1641772 263 wxTE_READONLY | wxTE_MULTILINE | wxSUNKEN_BORDER);
c41ea66a
VZ
264
265 m_logOld = wxLog::SetActiveTarget(new wxLogTextCtrl(m_logWindow));
266
776a33cf 267 RecreateList(wxLC_REPORT | wxLC_SINGLE_SEL);
a0d0799b 268
37e8bdb8 269 // Also add a simple list to the sample
a0d0799b
FM
270
271 m_simpleListCtrl = new wxListCtrl( m_panel, -1, wxDefaultPosition, wxDefaultSize,
37e8bdb8
RR
272 wxLC_REPORT | wxLC_SINGLE_SEL );
273 m_simpleListCtrl->InsertColumn(0, "Column 1", wxLIST_FORMAT_LEFT, 80);
274 m_simpleListCtrl->InsertColumn(1, "Column 2", wxLIST_FORMAT_LEFT, 100);
275 m_simpleListCtrl->InsertColumn(2, "Column 3", wxLIST_FORMAT_LEFT, 100);
a0d0799b 276
37e8bdb8
RR
277 for ( int i = 0; i < NUM_ITEMS; i++ )
278 {
279 wxString buf;
280 buf.Printf(_T("Item %d"), i);
281 long tmp = m_simpleListCtrl->InsertItem(i, buf, 0);
282 buf.Printf(_T("Col 1, item %d"), i);
283 m_simpleListCtrl->SetItem(tmp, 1, buf);
284 buf.Printf(_T("Col 2, item %d"), i);
285 m_simpleListCtrl->SetItem(tmp, 2, buf);
286 }
a0d0799b 287
8520f137 288#if wxUSE_STATUSBAR
e1641772 289 CreateStatusBar();
8520f137 290#endif // wxUSE_STATUSBAR
457814b5
JS
291}
292
11f26ea0 293MyFrame::~MyFrame()
457814b5 294{
c41ea66a
VZ
295 delete wxLog::SetActiveTarget(m_logOld);
296
297 delete m_imageListNormal;
298 delete m_imageListSmall;
457814b5
JS
299}
300
98ec9dbe 301void MyFrame::OnSize(wxSizeEvent& event)
07bf769e
VZ
302{
303 DoSize();
304
305 event.Skip();
306}
307
308void MyFrame::DoSize()
457814b5 309{
98ec9dbe
VZ
310 if ( !m_logWindow )
311 return;
457814b5 312
98ec9dbe
VZ
313 wxSize size = GetClientSize();
314 wxCoord y = (2*size.y)/3;
37e8bdb8
RR
315 m_listCtrl->SetSize(0, 0, size.x-320, y);
316 if (m_simpleListCtrl) m_simpleListCtrl->SetSize(size.x-320+1, 0, 320-1, y);
2a0c05eb 317 m_logWindow->SetSize(0, y + 1, size.x, size.y - y -1);
57246713
KB
318}
319
68457180
VZ
320bool MyFrame::CheckNonVirtual() const
321{
322 if ( !m_listCtrl->HasFlag(wxLC_VIRTUAL) )
323 return true;
324
325 // "this" == whatever
326 wxLogWarning(_T("Can't do this in virtual view, sorry."));
327
328 return false;
329}
330
98ec9dbe 331void MyFrame::OnQuit(wxCommandEvent& WXUNUSED(event))
57246713 332{
1550e402 333 Close(true);
57246713
KB
334}
335
b0d77f43 336void MyFrame::OnAbout(wxCommandEvent& WXUNUSED(event))
457814b5 337{
efe23391
VZ
338 wxMessageDialog dialog(this, _T("List test sample\nJulian Smart (c) 1997"),
339 _T("About list test"), wxOK|wxCANCEL);
457814b5 340
5ea47806 341 dialog.ShowModal();
457814b5
JS
342}
343
c71963cd 344void MyFrame::OnFreeze(wxCommandEvent& WXUNUSED(event))
c5c528fc
VZ
345{
346 wxLogMessage(_T("Freezing the control"));
347
348 m_listCtrl->Freeze();
349}
350
c71963cd 351void MyFrame::OnThaw(wxCommandEvent& WXUNUSED(event))
c5c528fc
VZ
352{
353 wxLogMessage(_T("Thawing the control"));
354
355 m_listCtrl->Thaw();
356}
357
494ab5de
VZ
358void MyFrame::OnToggleLines(wxCommandEvent& event)
359{
360 m_listCtrl->SetSingleStyle(wxLC_HRULES | wxLC_VRULES, event.IsChecked());
361}
362
2458daa7
KO
363void MyFrame::OnToggleMacUseGeneric(wxCommandEvent& event)
364{
365 wxSystemOptions::SetOption(wxT("mac.listctrl.always_use_generic"), event.IsChecked());
366}
367
6ef2b230
VZ
368void MyFrame::OnGoTo(wxCommandEvent& WXUNUSED(event))
369{
370 long index = 3;
371 m_listCtrl->SetItemState(index, wxLIST_STATE_FOCUSED, wxLIST_STATE_FOCUSED);
372
373 long sel = m_listCtrl->GetNextItem(-1, wxLIST_NEXT_ALL,
374 wxLIST_STATE_SELECTED);
375 if ( sel != -1 )
376 m_listCtrl->SetItemState(sel, 0, wxLIST_STATE_SELECTED);
377 m_listCtrl->SetItemState(index, wxLIST_STATE_SELECTED, wxLIST_STATE_SELECTED);
378}
379
88b792af
VZ
380void MyFrame::OnFocusLast(wxCommandEvent& WXUNUSED(event))
381{
382 long index = m_listCtrl->GetItemCount() - 1;
383 if ( index == -1 )
384 {
385 return;
386 }
387
388 m_listCtrl->SetItemState(index, wxLIST_STATE_FOCUSED, wxLIST_STATE_FOCUSED);
389 m_listCtrl->EnsureVisible(index);
390}
391
58b3bdc9
VZ
392void MyFrame::OnToggleFirstSel(wxCommandEvent& WXUNUSED(event))
393{
95f913a9 394 m_listCtrl->SetItemState(0, (~m_listCtrl->GetItemState(0, wxLIST_STATE_SELECTED) ) & wxLIST_STATE_SELECTED, wxLIST_STATE_SELECTED);
58b3bdc9
VZ
395}
396
c4771147
KB
397void MyFrame::OnDeselectAll(wxCommandEvent& WXUNUSED(event))
398{
68457180
VZ
399 if ( !CheckNonVirtual() )
400 return;
401
5ea47806
VZ
402 int n = m_listCtrl->GetItemCount();
403 for (int i = 0; i < n; i++)
404 m_listCtrl->SetItemState(i,0,wxLIST_STATE_SELECTED);
c4771147
KB
405}
406
407void MyFrame::OnSelectAll(wxCommandEvent& WXUNUSED(event))
408{
68457180
VZ
409 if ( !CheckNonVirtual() )
410 return;
411
5ea47806
VZ
412 int n = m_listCtrl->GetItemCount();
413 for (int i = 0; i < n; i++)
414 m_listCtrl->SetItemState(i,wxLIST_STATE_SELECTED, wxLIST_STATE_SELECTED);
c4771147
KB
415}
416
776a33cf
VZ
417// ----------------------------------------------------------------------------
418// changing listctrl modes
419// ----------------------------------------------------------------------------
420
421void MyFrame::RecreateList(long flags, bool withText)
457814b5 422{
776a33cf
VZ
423 // we could avoid recreating it if we don't set/clear the wxLC_VIRTUAL
424 // style, but it is more trouble to do it than not
425#if 0
426 if ( !m_listCtrl || ((flags & wxLC_VIRTUAL) !=
427 (m_listCtrl->GetWindowStyleFlag() & wxLC_VIRTUAL)) )
428#endif
429 {
430 delete m_listCtrl;
431
2b5f62a0 432 m_listCtrl = new MyListCtrl(m_panel, LIST_CTRL,
776a33cf
VZ
433 wxDefaultPosition, wxDefaultSize,
434 flags |
a91a64da 435 wxBORDER_THEME | wxLC_EDIT_LABELS);
776a33cf
VZ
436
437 switch ( flags & wxLC_MASK_TYPE )
438 {
439 case wxLC_LIST:
440 InitWithListItems();
441 break;
442
443 case wxLC_ICON:
444 InitWithIconItems(withText);
445 break;
446
447 case wxLC_SMALL_ICON:
1550e402 448 InitWithIconItems(withText, true);
776a33cf
VZ
449 break;
450
451 case wxLC_REPORT:
452 if ( flags & wxLC_VIRTUAL )
453 InitWithVirtualItems();
454 else
455 InitWithReportItems();
456 break;
457
458 default:
459 wxFAIL_MSG( _T("unknown listctrl mode") );
460 }
461 }
462
07bf769e 463 DoSize();
776a33cf 464
0180dad6 465 m_logWindow->Clear();
776a33cf 466}
95bf655c 467
776a33cf
VZ
468void MyFrame::OnListView(wxCommandEvent& WXUNUSED(event))
469{
470 RecreateList(wxLC_LIST);
471}
457814b5 472
776a33cf
VZ
473void MyFrame::InitWithListItems()
474{
475 for ( int i = 0; i < NUM_ITEMS; i++ )
0180dad6 476 {
98ec9dbe 477 m_listCtrl->InsertItem(i, wxString::Format(_T("Item %d"), i));
0180dad6 478 }
457814b5
JS
479}
480
b0d77f43 481void MyFrame::OnReportView(wxCommandEvent& WXUNUSED(event))
457814b5 482{
776a33cf
VZ
483 RecreateList(wxLC_REPORT);
484}
5ea47806 485
776a33cf
VZ
486void MyFrame::InitWithReportItems()
487{
c41ea66a 488 m_listCtrl->SetImageList(m_imageListSmall, wxIMAGE_LIST_SMALL);
457814b5 489
caa20b1e
VZ
490 // note that under MSW for SetColumnWidth() to work we need to create the
491 // items with images initially even if we specify dummy image id
6f806543 492 wxListItem itemCol;
caa20b1e
VZ
493 itemCol.SetText(_T("Column 1"));
494 itemCol.SetImage(-1);
6f806543 495 m_listCtrl->InsertColumn(0, itemCol);
caa20b1e
VZ
496
497 itemCol.SetText(_T("Column 2"));
498 itemCol.SetAlign(wxLIST_FORMAT_CENTRE);
6f806543 499 m_listCtrl->InsertColumn(1, itemCol);
caa20b1e
VZ
500
501 itemCol.SetText(_T("Column 3"));
502 itemCol.SetAlign(wxLIST_FORMAT_RIGHT);
6f806543 503 m_listCtrl->InsertColumn(2, itemCol);
0180dad6 504
81278df2
VZ
505 // to speed up inserting we hide the control temporarily
506 m_listCtrl->Hide();
507
508 wxStopWatch sw;
509
81278df2 510 for ( int i = 0; i < NUM_ITEMS; i++ )
0180dad6 511 {
5cd89174 512 m_listCtrl->InsertItemInReportView(i);
0180dad6 513 }
bdc72a22 514
81278df2
VZ
515 m_logWindow->WriteText(wxString::Format(_T("%d items inserted in %ldms\n"),
516 NUM_ITEMS, sw.Time()));
517 m_listCtrl->Show();
518
f6b77239 519 // we leave all mask fields to 0 and only change the colour
bdc72a22
VZ
520 wxListItem item;
521 item.m_itemId = 0;
0530737d 522 item.SetTextColour(*wxRED);
bdc72a22
VZ
523 m_listCtrl->SetItem( item );
524
525 item.m_itemId = 2;
d62228a6 526 item.SetTextColour(*wxGREEN);
bdc72a22 527 m_listCtrl->SetItem( item );
d62228a6 528 item.m_itemId = 4;
bdc72a22 529 item.SetTextColour(*wxLIGHT_GREY);
d62228a6
VZ
530 item.SetFont(*wxITALIC_FONT);
531 item.SetBackgroundColour(*wxRED);
bdc72a22 532 m_listCtrl->SetItem( item );
5ea47806 533
d62228a6 534 m_listCtrl->SetTextColour(*wxBLUE);
d62228a6 535
0180dad6
RR
536 m_listCtrl->SetColumnWidth( 0, wxLIST_AUTOSIZE );
537 m_listCtrl->SetColumnWidth( 1, wxLIST_AUTOSIZE );
538 m_listCtrl->SetColumnWidth( 2, wxLIST_AUTOSIZE );
35c2acd4 539
06db67bc
RD
540 // Set images in columns
541 m_listCtrl->SetItemColumnImage(1, 1, 0);
542
543 wxListItem info;
544 info.SetImage(0);
545 info.SetId(3);
546 info.SetColumn(2);
547 m_listCtrl->SetItem(info);
548
35c2acd4
MW
549 // test SetItemFont too
550 m_listCtrl->SetItemFont(0, *wxITALIC_FONT);
457814b5
JS
551}
552
776a33cf 553void MyFrame::InitWithIconItems(bool withText, bool sameIcon)
457814b5 554{
c41ea66a
VZ
555 m_listCtrl->SetImageList(m_imageListNormal, wxIMAGE_LIST_NORMAL);
556 m_listCtrl->SetImageList(m_imageListSmall, wxIMAGE_LIST_SMALL);
457814b5 557
152e8878 558 for ( int i = 0; i < NUM_ICONS; i++ )
5ea47806 559 {
152e8878 560 int image = sameIcon ? 0 : i;
776a33cf
VZ
561
562 if ( withText )
563 {
564 m_listCtrl->InsertItem(i, wxString::Format(_T("Label %d"), i),
565 image);
566 }
567 else
568 {
569 m_listCtrl->InsertItem(i, image);
570 }
5ea47806 571 }
457814b5
JS
572}
573
776a33cf 574void MyFrame::OnIconView(wxCommandEvent& WXUNUSED(event))
457814b5 575{
1550e402 576 RecreateList(wxLC_ICON, false);
776a33cf 577}
457814b5 578
776a33cf
VZ
579void MyFrame::OnIconTextView(wxCommandEvent& WXUNUSED(event))
580{
581 RecreateList(wxLC_ICON);
457814b5
JS
582}
583
b0d77f43 584void MyFrame::OnSmallIconView(wxCommandEvent& WXUNUSED(event))
457814b5 585{
1550e402 586 RecreateList(wxLC_SMALL_ICON, false);
457814b5
JS
587}
588
b0d77f43 589void MyFrame::OnSmallIconTextView(wxCommandEvent& WXUNUSED(event))
457814b5 590{
776a33cf 591 RecreateList(wxLC_SMALL_ICON);
5ea47806
VZ
592}
593
98ec9dbe
VZ
594void MyFrame::OnVirtualView(wxCommandEvent& WXUNUSED(event))
595{
b0401bea
VZ
596 m_smallVirtual = false;
597 RecreateList(wxLC_REPORT | wxLC_VIRTUAL);
598}
599
600void MyFrame::OnSmallVirtualView(wxCommandEvent& WXUNUSED(event))
601{
602 m_smallVirtual = true;
776a33cf
VZ
603 RecreateList(wxLC_REPORT | wxLC_VIRTUAL);
604}
98ec9dbe 605
776a33cf
VZ
606void MyFrame::InitWithVirtualItems()
607{
27770773
VZ
608 m_listCtrl->SetImageList(m_imageListSmall, wxIMAGE_LIST_SMALL);
609
b0401bea
VZ
610 if ( m_smallVirtual )
611 {
612 m_listCtrl->InsertColumn(0, _T("Animal"));
613 m_listCtrl->InsertColumn(1, _T("Sound"));
614 m_listCtrl->SetItemCount(WXSIZEOF(SMALL_VIRTUAL_VIEW_ITEMS));
615 }
616 else
617 {
618 m_listCtrl->InsertColumn(0, _T("First Column"));
619 m_listCtrl->InsertColumn(1, _T("Second Column"));
620 m_listCtrl->SetColumnWidth(0, 150);
621 m_listCtrl->SetColumnWidth(1, 150);
622 m_listCtrl->SetItemCount(1000000);
623 }
98ec9dbe
VZ
624}
625
fa5f6926
VZ
626void MyFrame::OnSort(wxCommandEvent& WXUNUSED(event))
627{
81278df2
VZ
628 wxStopWatch sw;
629
fa5f6926 630 m_listCtrl->SortItems(MyCompareFunction, 0);
81278df2
VZ
631
632 m_logWindow->WriteText(wxString::Format(_T("Sorting %d items took %ld ms\n"),
633 m_listCtrl->GetItemCount(),
634 sw.Time()));
fa5f6926
VZ
635}
636
c71963cd 637void MyFrame::OnShowSelInfo(wxCommandEvent& WXUNUSED(event))
b54e41c5
VZ
638{
639 int selCount = m_listCtrl->GetSelectedItemCount();
640 wxLogMessage(_T("%d items selected:"), selCount);
641
642 // don't show too many items
643 size_t shownCount = 0;
644
645 long item = m_listCtrl->GetNextItem(-1, wxLIST_NEXT_ALL,
646 wxLIST_STATE_SELECTED);
647 while ( item != -1 )
648 {
649 wxLogMessage(_T("\t%ld (%s)"),
650 item, m_listCtrl->GetItemText(item).c_str());
651
652 if ( ++shownCount > 10 )
653 {
654 wxLogMessage(_T("\t... more selected items snipped..."));
655 break;
656 }
657
658 item = m_listCtrl->GetNextItem(item, wxLIST_NEXT_ALL,
659 wxLIST_STATE_SELECTED);
660 }
661}
662
929b7901
VZ
663void MyFrame::OnShowViewRect(wxCommandEvent& WXUNUSED(event))
664{
665 const wxRect r = m_listCtrl->GetViewRect();
666 wxLogMessage("View rect: (%d, %d)-(%d, %d)",
667 r.GetLeft(), r.GetTop(), r.GetRight(), r.GetBottom());
668}
669
80cc5fc7
VZ
670// ----------------------------------------------------------------------------
671// column order tests
672// ----------------------------------------------------------------------------
673
674#ifdef wxHAS_LISTCTRL_COLUMN_ORDER
675
676static wxString DumpIntArray(const wxArrayInt& a)
677{
678 wxString s("{ ");
679 const size_t count = a.size();
680 for ( size_t n = 0; n < count; n++ )
681 {
682 if ( n )
683 s += ", ";
684 s += wxString::Format("%lu", (unsigned long)a[n]);
685 }
686
687 s += " }";
688
689 return s;
690}
691
692void MyFrame::OnSetColOrder(wxCommandEvent& WXUNUSED(event))
693{
694 wxArrayInt order(3);
695 order[0] = 2;
696 order[1] = 0;
697 order[2] = 1;
698 if ( m_listCtrl->SetColumnsOrder(order) )
699 wxLogMessage("Column order set to %s", DumpIntArray(order));
700}
701
702void MyFrame::OnGetColOrder(wxCommandEvent& WXUNUSED(event))
703{
704 // show what GetColumnsOrder() returns
705 const wxArrayInt order = m_listCtrl->GetColumnsOrder();
706 wxString msg = "Columns order: " +
707 DumpIntArray(m_listCtrl->GetColumnsOrder()) + "\n";
708
709 int n;
710 const int count = m_listCtrl->GetColumnCount();
711
712 // show the results of GetColumnOrder() for each column
713 msg += "GetColumnOrder() results:\n";
714 for ( n = 0; n < count; n++ )
715 {
716 msg += wxString::Format(" %2d -> %2d\n",
717 n, m_listCtrl->GetColumnOrder(n));
718 }
719
720 // and the results of GetColumnIndexFromOrder() too
721 msg += "GetColumnIndexFromOrder() results:\n";
722 for ( n = 0; n < count; n++ )
723 {
724 msg += wxString::Format(" %2d -> %2d\n",
725 n, m_listCtrl->GetColumnIndexFromOrder(n));
726 }
727
728 wxLogMessage("%s", msg);
729}
730
731#endif // wxHAS_LISTCTRL_COLUMN_ORDER
732
c71963cd 733void MyFrame::OnShowColInfo(wxCommandEvent& WXUNUSED(event))
f6bcfd97
BP
734{
735 int count = m_listCtrl->GetColumnCount();
4acb6ca6 736 wxLogMessage(wxT("%d columns:"), count);
f6bcfd97
BP
737 for ( int c = 0; c < count; c++ )
738 {
4acb6ca6 739 wxLogMessage(wxT("\tcolumn %d has width %d"), c,
f6bcfd97
BP
740 m_listCtrl->GetColumnWidth(c));
741 }
742}
743
744void MyFrame::OnUpdateShowColInfo(wxUpdateUIEvent& event)
745{
746 event.Enable( (m_listCtrl->GetWindowStyleFlag() & wxLC_REPORT) != 0 );
747}
748
7b848b0d
VZ
749void MyFrame::OnToggleMultiSel(wxCommandEvent& WXUNUSED(event))
750{
7b848b0d
VZ
751 long flags = m_listCtrl->GetWindowStyleFlag();
752 if ( flags & wxLC_SINGLE_SEL )
776a33cf 753 flags &= ~wxLC_SINGLE_SEL;
7b848b0d 754 else
776a33cf
VZ
755 flags |= wxLC_SINGLE_SEL;
756
4acb6ca6 757 m_logWindow->WriteText(wxString::Format(wxT("Current selection mode: %sle\n"),
efe23391 758 (flags & wxLC_SINGLE_SEL) ? _T("sing") : _T("multip")));
7b848b0d 759
776a33cf 760 RecreateList(flags);
7b848b0d
VZ
761}
762
ae403b11
JS
763void MyFrame::OnUpdateToggleMultiSel(wxUpdateUIEvent& event)
764{
765 event.Check((m_listCtrl->GetWindowStyleFlag() & wxLC_SINGLE_SEL) == 0);
766}
767
11f26ea0
VZ
768void MyFrame::OnSetFgColour(wxCommandEvent& WXUNUSED(event))
769{
770 m_listCtrl->SetForegroundColour(wxGetColourFromUser(this));
771 m_listCtrl->Refresh();
772}
773
774void MyFrame::OnSetBgColour(wxCommandEvent& WXUNUSED(event))
775{
776 m_listCtrl->SetBackgroundColour(wxGetColourFromUser(this));
777 m_listCtrl->Refresh();
778}
779
cf1dfa6b
VZ
780void MyFrame::OnAdd(wxCommandEvent& WXUNUSED(event))
781{
782 m_listCtrl->InsertItem(m_listCtrl->GetItemCount(), _T("Appended item"));
783}
784
efe23391
VZ
785void MyFrame::OnEdit(wxCommandEvent& WXUNUSED(event))
786{
787 long itemCur = m_listCtrl->GetNextItem(-1, wxLIST_NEXT_ALL,
788 wxLIST_STATE_FOCUSED);
789
790 if ( itemCur != -1 )
791 {
792 m_listCtrl->EditLabel(itemCur);
793 }
794 else
795 {
796 m_logWindow->WriteText(_T("No item to edit"));
797 }
798}
799
bec0a261
VZ
800void MyFrame::OnDelete(wxCommandEvent& WXUNUSED(event))
801{
802 if ( m_listCtrl->GetItemCount() )
803 {
804 m_listCtrl->DeleteItem(0);
805 }
806 else
807 {
efe23391 808 m_logWindow->WriteText(_T("Nothing to delete"));
bec0a261
VZ
809 }
810}
811
5ea47806
VZ
812void MyFrame::OnDeleteAll(wxCommandEvent& WXUNUSED(event))
813{
81278df2 814 wxStopWatch sw;
5ea47806 815
b143cf70 816 int itemCount = m_listCtrl->GetItemCount();
fde548e2 817
5ea47806
VZ
818 m_listCtrl->DeleteAllItems();
819
81278df2 820 m_logWindow->WriteText(wxString::Format(_T("Deleting %d items took %ld ms\n"),
fde548e2 821 itemCount,
81278df2 822 sw.Time()));
457814b5
JS
823}
824
a0d0799b
FM
825
826// ----------------------------------------------------------------------------
457814b5 827// MyListCtrl
a0d0799b
FM
828// ----------------------------------------------------------------------------
829
830BEGIN_EVENT_TABLE(MyListCtrl, wxListCtrl)
831 EVT_LIST_BEGIN_DRAG(LIST_CTRL, MyListCtrl::OnBeginDrag)
832 EVT_LIST_BEGIN_RDRAG(LIST_CTRL, MyListCtrl::OnBeginRDrag)
833 EVT_LIST_BEGIN_LABEL_EDIT(LIST_CTRL, MyListCtrl::OnBeginLabelEdit)
834 EVT_LIST_END_LABEL_EDIT(LIST_CTRL, MyListCtrl::OnEndLabelEdit)
835 EVT_LIST_DELETE_ITEM(LIST_CTRL, MyListCtrl::OnDeleteItem)
836 EVT_LIST_DELETE_ALL_ITEMS(LIST_CTRL, MyListCtrl::OnDeleteAllItems)
837 EVT_LIST_ITEM_SELECTED(LIST_CTRL, MyListCtrl::OnSelected)
838 EVT_LIST_ITEM_DESELECTED(LIST_CTRL, MyListCtrl::OnDeselected)
839 EVT_LIST_KEY_DOWN(LIST_CTRL, MyListCtrl::OnListKeyDown)
840 EVT_LIST_ITEM_ACTIVATED(LIST_CTRL, MyListCtrl::OnActivated)
841 EVT_LIST_ITEM_FOCUSED(LIST_CTRL, MyListCtrl::OnFocused)
842
843 EVT_LIST_COL_CLICK(LIST_CTRL, MyListCtrl::OnColClick)
844 EVT_LIST_COL_RIGHT_CLICK(LIST_CTRL, MyListCtrl::OnColRightClick)
845 EVT_LIST_COL_BEGIN_DRAG(LIST_CTRL, MyListCtrl::OnColBeginDrag)
846 EVT_LIST_COL_DRAGGING(LIST_CTRL, MyListCtrl::OnColDragging)
847 EVT_LIST_COL_END_DRAG(LIST_CTRL, MyListCtrl::OnColEndDrag)
848
849 EVT_LIST_CACHE_HINT(LIST_CTRL, MyListCtrl::OnCacheHint)
850
851#if USE_CONTEXT_MENU
852 EVT_CONTEXT_MENU(MyListCtrl::OnContextMenu)
853#endif
854 EVT_CHAR(MyListCtrl::OnChar)
855
856 EVT_RIGHT_DOWN(MyListCtrl::OnRightClick)
857END_EVENT_TABLE()
457814b5 858
614391dc
VZ
859void MyListCtrl::OnCacheHint(wxListEvent& event)
860{
4acb6ca6 861 wxLogMessage( wxT("OnCacheHint: cache items %ld..%ld"),
614391dc
VZ
862 event.GetCacheFrom(), event.GetCacheTo() );
863}
864
6f806543
VZ
865void MyListCtrl::SetColumnImage(int col, int image)
866{
867 wxListItem item;
868 item.SetMask(wxLIST_MASK_IMAGE);
869 item.SetImage(image);
870 SetColumn(col, item);
871}
872
8636aed8
RR
873void MyListCtrl::OnColClick(wxListEvent& event)
874{
6f806543 875 int col = event.GetColumn();
f0cf38b7
VZ
876
877 // set or unset image
4979a56c 878 static bool x = false;
f0cf38b7
VZ
879 x = !x;
880 SetColumnImage(col, x ? 0 : -1);
6f806543
VZ
881
882 wxLogMessage( wxT("OnColumnClick at %d."), col );
8636aed8
RR
883}
884
11358d39
VZ
885void MyListCtrl::OnColRightClick(wxListEvent& event)
886{
6f806543 887 int col = event.GetColumn();
62313c27
VZ
888 if ( col != -1 )
889 {
890 SetColumnImage(col, -1);
891 }
6f806543 892
77ace0c5
WS
893 // Show popupmenu at position
894 wxMenu menu(wxT("Test"));
895 menu.Append(LIST_ABOUT, _T("&About"));
5f4d35b8 896 PopupMenu(&menu, event.GetPoint());
77ace0c5 897
11358d39
VZ
898 wxLogMessage( wxT("OnColumnRightClick at %d."), event.GetColumn() );
899}
900
2b5f62a0
VZ
901void MyListCtrl::LogColEvent(const wxListEvent& event, const wxChar *name)
902{
903 const int col = event.GetColumn();
904
905 wxLogMessage(wxT("%s: column %d (width = %d or %d)."),
906 name,
907 col,
908 event.GetItem().GetWidth(),
909 GetColumnWidth(col));
910}
911
11358d39
VZ
912void MyListCtrl::OnColBeginDrag(wxListEvent& event)
913{
2b5f62a0 914 LogColEvent( event, wxT("OnColBeginDrag") );
355f2407
VZ
915
916 if ( event.GetColumn() == 0 )
917 {
918 wxLogMessage(_T("Resizing this column shouldn't work."));
919
920 event.Veto();
921 }
11358d39
VZ
922}
923
924void MyListCtrl::OnColDragging(wxListEvent& event)
925{
2b5f62a0 926 LogColEvent( event, wxT("OnColDragging") );
11358d39
VZ
927}
928
929void MyListCtrl::OnColEndDrag(wxListEvent& event)
930{
2b5f62a0 931 LogColEvent( event, wxT("OnColEndDrag") );
11358d39
VZ
932}
933
fd9811b1 934void MyListCtrl::OnBeginDrag(wxListEvent& event)
457814b5 935{
6dc34ebb
VZ
936 const wxPoint& pt = event.m_pointDrag;
937
938 int flags;
939 wxLogMessage( wxT("OnBeginDrag at (%d, %d), item %ld."),
940 pt.x, pt.y, HitTest(pt, flags) );
457814b5
JS
941}
942
fd9811b1 943void MyListCtrl::OnBeginRDrag(wxListEvent& event)
457814b5 944{
4acb6ca6 945 wxLogMessage( wxT("OnBeginRDrag at %d,%d."),
c41ea66a 946 event.m_pointDrag.x, event.m_pointDrag.y );
457814b5
JS
947}
948
fd9811b1 949void MyListCtrl::OnBeginLabelEdit(wxListEvent& event)
457814b5 950{
4acb6ca6 951 wxLogMessage( wxT("OnBeginLabelEdit: %s"), event.m_item.m_text.c_str());
508b6523
VZ
952
953 wxTextCtrl * const text = GetEditControl();
954 if ( !text )
955 {
956 wxLogMessage("BUG: started to edit but no edit control");
957 }
958 else
959 {
960 wxLogMessage("Edit control value: \"%s\"", text->GetValue());
961 }
457814b5
JS
962}
963
fd9811b1 964void MyListCtrl::OnEndLabelEdit(wxListEvent& event)
457814b5 965{
1bf37bf5 966 wxLogMessage( wxT("OnEndLabelEdit: %s"),
15836000 967 (
a0d0799b
FM
968 event.IsEditCancelled() ?
969 wxString("[cancelled]") :
15836000
CE
970 event.m_item.m_text
971 ).c_str()
972 );
457814b5
JS
973}
974
efbb7287 975void MyListCtrl::OnDeleteItem(wxListEvent& event)
457814b5 976{
c41ea66a 977 LogEvent(event, _T("OnDeleteItem"));
a95d5751 978 wxLogMessage( wxT("Number of items when delete event is sent: %d"), GetItemCount() );
457814b5
JS
979}
980
c41ea66a 981void MyListCtrl::OnDeleteAllItems(wxListEvent& event)
12c1b46a 982{
c41ea66a 983 LogEvent(event, _T("OnDeleteAllItems"));
12c1b46a
RR
984}
985
74b31181 986void MyListCtrl::OnSelected(wxListEvent& event)
457814b5 987{
c41ea66a 988 LogEvent(event, _T("OnSelected"));
457814b5 989
40779a03 990 if ( GetWindowStyle() & wxLC_REPORT )
74b31181 991 {
40779a03
VZ
992 wxListItem info;
993 info.m_itemId = event.m_itemIndex;
994 info.m_col = 1;
995 info.m_mask = wxLIST_MASK_TEXT;
996 if ( GetItem(info) )
997 {
4acb6ca6 998 wxLogMessage(wxT("Value of the 2nd field of the selected item: %s"),
c41ea66a 999 info.m_text.c_str());
40779a03
VZ
1000 }
1001 else
1002 {
4acb6ca6 1003 wxFAIL_MSG(wxT("wxListCtrl::GetItem() failed"));
40779a03 1004 }
74b31181 1005 }
457814b5
JS
1006}
1007
efbb7287 1008void MyListCtrl::OnDeselected(wxListEvent& event)
457814b5 1009{
c41ea66a 1010 LogEvent(event, _T("OnDeselected"));
457814b5
JS
1011}
1012
efbb7287 1013void MyListCtrl::OnActivated(wxListEvent& event)
435fe83e 1014{
c41ea66a 1015 LogEvent(event, _T("OnActivated"));
435fe83e
RR
1016}
1017
0ddefeb0
VZ
1018void MyListCtrl::OnFocused(wxListEvent& event)
1019{
1020 LogEvent(event, _T("OnFocused"));
27770773
VZ
1021
1022 event.Skip();
0ddefeb0
VZ
1023}
1024
8e1d4f96 1025void MyListCtrl::OnListKeyDown(wxListEvent& event)
457814b5 1026{
100f649f
VZ
1027 long item;
1028
2936eaf0 1029 switch ( event.GetKeyCode() )
5cd89174 1030 {
ebc9b89d 1031 case 'C': // colorize
6c02c329
VZ
1032 {
1033 wxListItem info;
1034 info.m_itemId = event.GetIndex();
63c95faf
VZ
1035 if ( info.m_itemId == -1 )
1036 {
1037 // no item
1038 break;
1039 }
1040
6c02c329
VZ
1041 GetItem(info);
1042
1043 wxListItemAttr *attr = info.GetAttributes();
1044 if ( !attr || !attr->HasTextColour() )
1045 {
1046 info.SetTextColour(*wxCYAN);
1047
1048 SetItem(info);
2d33aec9
VZ
1049
1050 RefreshItem(info.m_itemId);
6c02c329
VZ
1051 }
1052 }
1053 break;
1054
ebc9b89d 1055 case 'N': // next
100f649f
VZ
1056 item = GetNextItem(-1, wxLIST_NEXT_ALL, wxLIST_STATE_FOCUSED);
1057 if ( item++ == GetItemCount() - 1 )
1058 {
1059 item = 0;
1060 }
1061
1062 wxLogMessage(_T("Focusing item %ld"), item);
1063
1064 SetItemState(item, wxLIST_STATE_FOCUSED, wxLIST_STATE_FOCUSED);
1065 EnsureVisible(item);
1066 break;
1067
ebc9b89d 1068 case 'R': // show bounding rectangle
2d33aec9 1069 {
100f649f
VZ
1070 item = event.GetIndex();
1071 wxRect r;
1072 if ( !GetItemRect(item, r) )
2d33aec9 1073 {
100f649f
VZ
1074 wxLogError(_T("Failed to retrieve rect of item %ld"), item);
1075 break;
2d33aec9
VZ
1076 }
1077
100f649f
VZ
1078 wxLogMessage(_T("Bounding rect of item %ld is (%d, %d)-(%d, %d)"),
1079 item, r.x, r.y, r.x + r.width, r.y + r.height);
2d33aec9
VZ
1080 }
1081 break;
1082
e974c5d2
VZ
1083 case '1': // show sub item bounding rectangle
1084 case '2':
1085 case '3':
1086 case '4': // this column is invalid but we want to test it too
1087 if ( InReportView() )
1088 {
1089 int subItem = event.GetKeyCode() - '1';
1090 item = event.GetIndex();
1091 wxRect r;
1092 if ( !GetSubItemRect(item, subItem, r) )
1093 {
1094 wxLogError(_T("Failed to retrieve rect of item %ld column %d"), item, subItem + 1);
1095 break;
1096 }
1097
1098 wxLogMessage(_T("Bounding rect of item %ld column %d is (%d, %d)-(%d, %d)"),
1099 item, subItem + 1,
1100 r.x, r.y, r.x + r.width, r.y + r.height);
1101 }
1102 break;
1103
ebc9b89d
VZ
1104 case 'U': // update
1105 if ( !IsVirtual() )
1106 break;
1107
1108 if ( m_updated != -1 )
1109 RefreshItem(m_updated);
1110
1111 m_updated = event.GetIndex();
1112 if ( m_updated != -1 )
1113 {
1114 // we won't see changes to this item as it's selected, update
1115 // the next one (or the first one if we're on the last item)
1116 if ( ++m_updated == GetItemCount() )
1117 m_updated = 0;
1118
1119 wxLogMessage("Updating colour of the item %ld", m_updated);
1120 RefreshItem(m_updated);
1121 }
1122 break;
1123
17483f70 1124 case 'D': // delete
100f649f
VZ
1125 item = GetNextItem(-1, wxLIST_NEXT_ALL, wxLIST_STATE_SELECTED);
1126 while ( item != -1 )
91c6cc0e 1127 {
100f649f 1128 DeleteItem(item);
91c6cc0e 1129
100f649f 1130 wxLogMessage(_T("Item %ld deleted"), item);
f6bcfd97 1131
100f649f
VZ
1132 // -1 because the indices were shifted by DeleteItem()
1133 item = GetNextItem(item - 1,
1134 wxLIST_NEXT_ALL, wxLIST_STATE_SELECTED);
91c6cc0e 1135 }
5cd89174
VZ
1136 break;
1137
17483f70 1138 case 'I': // insert
5cd89174
VZ
1139 if ( GetWindowStyle() & wxLC_REPORT )
1140 {
1141 if ( GetWindowStyle() & wxLC_VIRTUAL )
1142 {
1143 SetItemCount(GetItemCount() + 1);
1144 }
1145 else // !virtual
1146 {
1147 InsertItemInReportView(event.GetIndex());
1148 }
1149 }
1150 //else: fall through
1151
1152 default:
1153 LogEvent(event, _T("OnListKeyDown"));
1154
1155 event.Skip();
1156 }
f6bcfd97
BP
1157}
1158
1159void MyListCtrl::OnChar(wxKeyEvent& event)
1160{
1161 wxLogMessage(_T("Got char event."));
1162
2d33aec9
VZ
1163 switch ( event.GetKeyCode() )
1164 {
1165 case 'n':
1166 case 'N':
1167 case 'c':
1168 case 'C':
ebc9b89d
VZ
1169 case 'r':
1170 case 'R':
1171 case 'u':
1172 case 'U':
17483f70
VZ
1173 case 'd':
1174 case 'D':
1175 case 'i':
1176 case 'I':
2d33aec9
VZ
1177 // these are the keys we process ourselves
1178 break;
1179
1180 default:
1181 event.Skip();
1182 }
457814b5
JS
1183}
1184
164a7972
VZ
1185void MyListCtrl::OnRightClick(wxMouseEvent& event)
1186{
1187 if ( !event.ControlDown() )
1188 {
1189 event.Skip();
1190 return;
1191 }
1192
1193 int flags;
1194 long subitem;
1195 long item = HitTest(event.GetPosition(), flags, &subitem);
1196
1197 wxString where;
1198 switch ( flags )
1199 {
1200 case wxLIST_HITTEST_ABOVE: where = _T("above"); break;
1201 case wxLIST_HITTEST_BELOW: where = _T("below"); break;
1202 case wxLIST_HITTEST_NOWHERE: where = _T("nowhere near"); break;
1203 case wxLIST_HITTEST_ONITEMICON: where = _T("on icon of"); break;
1204 case wxLIST_HITTEST_ONITEMLABEL: where = _T("on label of"); break;
1205 case wxLIST_HITTEST_ONITEMRIGHT: where = _T("right on"); break;
1206 case wxLIST_HITTEST_TOLEFT: where = _T("to the left of"); break;
1207 case wxLIST_HITTEST_TORIGHT: where = _T("to the right of"); break;
1208 default: where = _T("not clear exactly where on"); break;
1209 }
1210
1211 wxLogMessage(_T("Right double click %s item %ld, subitem %ld"),
1212 where.c_str(), item, subitem);
1213}
1214
c41ea66a
VZ
1215void MyListCtrl::LogEvent(const wxListEvent& event, const wxChar *eventName)
1216{
f6bcfd97
BP
1217 wxLogMessage(_T("Item %ld: %s (item text = %s, data = %ld)"),
1218 event.GetIndex(), eventName,
1219 event.GetText().c_str(), event.GetData());
c41ea66a 1220}
435fe83e 1221
98ec9dbe
VZ
1222wxString MyListCtrl::OnGetItemText(long item, long column) const
1223{
b0401bea
VZ
1224 if ( GetItemCount() == WXSIZEOF(SMALL_VIRTUAL_VIEW_ITEMS) )
1225 {
1226 return SMALL_VIRTUAL_VIEW_ITEMS[item][column];
1227 }
ebc9b89d 1228 else // "big" virtual control
b0401bea
VZ
1229 {
1230 return wxString::Format(_T("Column %ld of item %ld"), column, item);
1231 }
98ec9dbe
VZ
1232}
1233
06db67bc 1234int MyListCtrl::OnGetItemColumnImage(long item, long column) const
98ec9dbe 1235{
06db67bc
RD
1236 if (!column)
1237 return 0;
1238
ebc9b89d 1239 if (!(item % 3) && column == 1)
06db67bc
RD
1240 return 0;
1241
1242 return -1;
98ec9dbe
VZ
1243}
1244
6c02c329
VZ
1245wxListItemAttr *MyListCtrl::OnGetItemAttr(long item) const
1246{
ebc9b89d
VZ
1247 // test to check that RefreshItem() works correctly: when m_updated is
1248 // set to some item and it is refreshed, we highlight the item
1249 if ( item == m_updated )
1250 {
1251 static wxListItemAttr s_attrHighlight(*wxRED, wxNullColour, wxNullFont);
1252 return &s_attrHighlight;
1253 }
1254
6c02c329
VZ
1255 return item % 2 ? NULL : (wxListItemAttr *)&m_attr;
1256}
1257
5cd89174
VZ
1258void MyListCtrl::InsertItemInReportView(int i)
1259{
1260 wxString buf;
1261 buf.Printf(_T("This is item %d"), i);
1262 long tmp = InsertItem(i, buf, 0);
1263 SetItemData(tmp, i);
1264
1265 buf.Printf(_T("Col 1, item %d"), i);
eb257f65 1266 SetItem(tmp, 1, buf);
5cd89174
VZ
1267
1268 buf.Printf(_T("Item %d in column 2"), i);
eb257f65 1269 SetItem(tmp, 2, buf);
5cd89174 1270}
107ff668
JS
1271
1272#if USE_CONTEXT_MENU
1273void MyListCtrl::OnContextMenu(wxContextMenuEvent& event)
1274{
d53b09d8
VZ
1275 if (GetEditControl() == NULL)
1276 {
1277 wxPoint point = event.GetPosition();
1278 // If from keyboard
1279 if ( (point.x == -1) && (point.y == -1) )
1280 {
1281 wxSize size = GetSize();
1282 point.x = size.x / 2;
1283 point.y = size.y / 2;
1284 }
1285 else
1286 {
1287 point = ScreenToClient(point);
1288 }
1289 ShowContextMenu(point);
1290 }
1291 else
1292 {
1293 // the user is editing:
1294 // allow the text control to display its context menu
1295 // if it has one (it has on Windows) rather than display our one
1296 event.Skip();
107ff668 1297 }
107ff668
JS
1298}
1299#endif
1300
1301void MyListCtrl::ShowContextMenu(const wxPoint& pos)
1302{
1303 wxMenu menu;
1304
1305 menu.Append(wxID_ABOUT, _T("&About"));
1306 menu.AppendSeparator();
1307 menu.Append(wxID_EXIT, _T("E&xit"));
1308
1309 PopupMenu(&menu, pos.x, pos.y);
1310}