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