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