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