]> git.saurik.com Git - wxWidgets.git/blame - samples/listctrl/listtest.cpp
select current column when Ctrl-Space is pressed; the current row when Shift-Space...
[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)
c5c528fc
VZ
94 EVT_MENU(LIST_FREEZE, MyFrame::OnFreeze)
95 EVT_MENU(LIST_THAW, MyFrame::OnThaw)
494ab5de 96 EVT_MENU(LIST_TOGGLE_LINES, MyFrame::OnToggleLines)
2458daa7 97 EVT_MENU(LIST_MAC_USE_GENERIC, MyFrame::OnToggleMacUseGeneric)
98ec9dbe 98
f6bcfd97 99 EVT_UPDATE_UI(LIST_SHOW_COL_INFO, MyFrame::OnUpdateShowColInfo)
5f4d35b8 100 EVT_UPDATE_UI(LIST_TOGGLE_MULTI_SEL, MyFrame::OnUpdateToggleMultiSel)
457814b5
JS
101END_EVENT_TABLE()
102
103BEGIN_EVENT_TABLE(MyListCtrl, wxListCtrl)
5ea47806
VZ
104 EVT_LIST_BEGIN_DRAG(LIST_CTRL, MyListCtrl::OnBeginDrag)
105 EVT_LIST_BEGIN_RDRAG(LIST_CTRL, MyListCtrl::OnBeginRDrag)
106 EVT_LIST_BEGIN_LABEL_EDIT(LIST_CTRL, MyListCtrl::OnBeginLabelEdit)
107 EVT_LIST_END_LABEL_EDIT(LIST_CTRL, MyListCtrl::OnEndLabelEdit)
108 EVT_LIST_DELETE_ITEM(LIST_CTRL, MyListCtrl::OnDeleteItem)
12c1b46a 109 EVT_LIST_DELETE_ALL_ITEMS(LIST_CTRL, MyListCtrl::OnDeleteAllItems)
5ea47806
VZ
110 EVT_LIST_ITEM_SELECTED(LIST_CTRL, MyListCtrl::OnSelected)
111 EVT_LIST_ITEM_DESELECTED(LIST_CTRL, MyListCtrl::OnDeselected)
112 EVT_LIST_KEY_DOWN(LIST_CTRL, MyListCtrl::OnListKeyDown)
113 EVT_LIST_ITEM_ACTIVATED(LIST_CTRL, MyListCtrl::OnActivated)
0ddefeb0 114 EVT_LIST_ITEM_FOCUSED(LIST_CTRL, MyListCtrl::OnFocused)
11358d39 115
8636aed8 116 EVT_LIST_COL_CLICK(LIST_CTRL, MyListCtrl::OnColClick)
11358d39
VZ
117 EVT_LIST_COL_RIGHT_CLICK(LIST_CTRL, MyListCtrl::OnColRightClick)
118 EVT_LIST_COL_BEGIN_DRAG(LIST_CTRL, MyListCtrl::OnColBeginDrag)
119 EVT_LIST_COL_DRAGGING(LIST_CTRL, MyListCtrl::OnColDragging)
120 EVT_LIST_COL_END_DRAG(LIST_CTRL, MyListCtrl::OnColEndDrag)
121
614391dc 122 EVT_LIST_CACHE_HINT(LIST_CTRL, MyListCtrl::OnCacheHint)
f6bcfd97 123
107ff668
JS
124#if USE_CONTEXT_MENU
125 EVT_CONTEXT_MENU(MyListCtrl::OnContextMenu)
126#endif
f6bcfd97 127 EVT_CHAR(MyListCtrl::OnChar)
164a7972
VZ
128
129 EVT_RIGHT_DOWN(MyListCtrl::OnRightClick)
457814b5
JS
130END_EVENT_TABLE()
131
132IMPLEMENT_APP(MyApp)
133
776a33cf 134// number of items in list/report view
e1641772 135static const int NUM_ITEMS = 10;
776a33cf
VZ
136
137// number of items in icon/small icon view
138static const int NUM_ICONS = 9;
139
c71963cd 140int wxCALLBACK MyCompareFunction(long item1, long item2, long WXUNUSED(sortData))
fa5f6926
VZ
141{
142 // inverse the order
c71963cd
VZ
143 if (item1 < item2)
144 return -1;
145 if (item1 > item2)
146 return 1;
147
2b5f62a0 148 return 0;
fa5f6926
VZ
149}
150
457814b5 151// `Main program' equivalent, creating windows and returning main app frame
11f26ea0 152bool MyApp::OnInit()
457814b5 153{
45e6e6f8
VZ
154 if ( !wxApp::OnInit() )
155 return false;
156
457814b5 157 // Create the main frame window
107ff668 158 MyFrame *frame = new MyFrame(wxT("wxListCtrl Test"));
457814b5
JS
159
160 // Show the frame
1550e402 161 frame->Show(true);
5ea47806 162
457814b5
JS
163 SetTopWindow(frame);
164
1550e402 165 return true;
457814b5
JS
166}
167
168// My frame constructor
107ff668 169MyFrame::MyFrame(const wxChar *title)
e1641772 170 : wxFrame(NULL, wxID_ANY, title, wxDefaultPosition, wxSize(600, 500))
457814b5 171{
b0401bea
VZ
172 m_listCtrl = NULL;
173 m_logWindow = NULL;
174 m_smallVirtual = false;
c41ea66a
VZ
175
176 // Give it an icon
177 SetIcon( wxICON(mondrian) );
178
179 // Make an image list containing large icons
1550e402
WS
180 m_imageListNormal = new wxImageList(32, 32, true);
181 m_imageListSmall = new wxImageList(16, 16, true);
c41ea66a
VZ
182
183#ifdef __WXMSW__
42ed7532
MB
184 m_imageListNormal->Add( wxIcon(_T("icon1"), wxBITMAP_TYPE_ICO_RESOURCE) );
185 m_imageListNormal->Add( wxIcon(_T("icon2"), wxBITMAP_TYPE_ICO_RESOURCE) );
186 m_imageListNormal->Add( wxIcon(_T("icon3"), wxBITMAP_TYPE_ICO_RESOURCE) );
187 m_imageListNormal->Add( wxIcon(_T("icon4"), wxBITMAP_TYPE_ICO_RESOURCE) );
188 m_imageListNormal->Add( wxIcon(_T("icon5"), wxBITMAP_TYPE_ICO_RESOURCE) );
189 m_imageListNormal->Add( wxIcon(_T("icon6"), wxBITMAP_TYPE_ICO_RESOURCE) );
190 m_imageListNormal->Add( wxIcon(_T("icon7"), wxBITMAP_TYPE_ICO_RESOURCE) );
191 m_imageListNormal->Add( wxIcon(_T("icon8"), wxBITMAP_TYPE_ICO_RESOURCE) );
192 m_imageListNormal->Add( wxIcon(_T("icon9"), wxBITMAP_TYPE_ICO_RESOURCE) );
193
194 m_imageListSmall->Add( wxIcon(_T("iconsmall"), wxBITMAP_TYPE_ICO_RESOURCE) );
c41ea66a
VZ
195
196#else
197 m_imageListNormal->Add( wxIcon( toolbrai_xpm ) );
198 m_imageListNormal->Add( wxIcon( toolchar_xpm ) );
199 m_imageListNormal->Add( wxIcon( tooldata_xpm ) );
200 m_imageListNormal->Add( wxIcon( toolnote_xpm ) );
201 m_imageListNormal->Add( wxIcon( tooltodo_xpm ) );
202 m_imageListNormal->Add( wxIcon( toolchec_xpm ) );
203 m_imageListNormal->Add( wxIcon( toolgame_xpm ) );
204 m_imageListNormal->Add( wxIcon( tooltime_xpm ) );
205 m_imageListNormal->Add( wxIcon( toolword_xpm ) );
206
207 m_imageListSmall->Add( wxIcon( small1_xpm) );
208#endif
209
210 // Make a menubar
211 wxMenu *menuFile = new wxMenu;
98ec9dbe 212 menuFile->Append(LIST_ABOUT, _T("&About"));
c41ea66a 213 menuFile->AppendSeparator();
98ec9dbe 214 menuFile->Append(LIST_QUIT, _T("E&xit\tAlt-X"));
c41ea66a
VZ
215
216 wxMenu *menuView = new wxMenu;
98ec9dbe
VZ
217 menuView->Append(LIST_LIST_VIEW, _T("&List view\tF1"));
218 menuView->Append(LIST_REPORT_VIEW, _T("&Report view\tF2"));
219 menuView->Append(LIST_ICON_VIEW, _T("&Icon view\tF3"));
220 menuView->Append(LIST_ICON_TEXT_VIEW, _T("Icon view with &text\tF4"));
221 menuView->Append(LIST_SMALL_ICON_VIEW, _T("&Small icon view\tF5"));
222 menuView->Append(LIST_SMALL_ICON_TEXT_VIEW, _T("Small icon &view with text\tF6"));
b0401bea
VZ
223 menuView->Append(LIST_VIRTUAL_VIEW, _T("&Virtual view\tF7"));
224 menuView->Append(LIST_SMALL_VIRTUAL_VIEW, _T("Small virtual vie&w\tF8"));
2458daa7
KO
225#ifdef __WXMAC__
226 menuView->AppendCheckItem(LIST_MAC_USE_GENERIC, _T("Mac: Use Generic Control"));
227#endif
c41ea66a
VZ
228
229 wxMenu *menuList = new wxMenu;
6ef2b230 230 menuList->Append(LIST_GOTO, _T("&Go to item #3\tCtrl-3"));
88b792af 231 menuList->Append(LIST_FOCUS_LAST, _T("&Make last item current\tCtrl-L"));
c5c528fc 232 menuList->Append(LIST_TOGGLE_FIRST, _T("To&ggle first item\tCtrl-G"));
98ec9dbe
VZ
233 menuList->Append(LIST_DESELECT_ALL, _T("&Deselect All\tCtrl-D"));
234 menuList->Append(LIST_SELECT_ALL, _T("S&elect All\tCtrl-A"));
b54e41c5 235 menuList->AppendSeparator();
98ec9dbe 236 menuList->Append(LIST_SHOW_COL_INFO, _T("Show &column info\tCtrl-C"));
b54e41c5 237 menuList->Append(LIST_SHOW_SEL_INFO, _T("Show &selected items\tCtrl-S"));
929b7901 238 menuList->Append(LIST_SHOW_VIEW_RECT, _T("Show &view rect"));
c41ea66a 239 menuList->AppendSeparator();
d6d9c223 240 menuList->Append(LIST_SORT, _T("Sor&t\tCtrl-T"));
c41ea66a 241 menuList->AppendSeparator();
cf1dfa6b 242 menuList->Append(LIST_ADD, _T("&Append an item\tCtrl-P"));
efe23391 243 menuList->Append(LIST_EDIT, _T("&Edit the item\tCtrl-E"));
cf1dfa6b 244 menuList->Append(LIST_DELETE, _T("&Delete first item\tCtrl-X"));
98ec9dbe 245 menuList->Append(LIST_DELETE_ALL, _T("Delete &all items"));
c41ea66a 246 menuList->AppendSeparator();
c5c528fc
VZ
247 menuList->Append(LIST_FREEZE, _T("Free&ze\tCtrl-Z"));
248 menuList->Append(LIST_THAW, _T("Tha&w\tCtrl-W"));
249 menuList->AppendSeparator();
494ab5de 250 menuList->AppendCheckItem(LIST_TOGGLE_LINES, _T("Toggle &lines\tCtrl-I"));
98ec9dbe 251 menuList->Append(LIST_TOGGLE_MULTI_SEL, _T("&Multiple selection\tCtrl-M"),
1550e402 252 _T("Toggle multiple selection"), true);
c41ea66a
VZ
253
254 wxMenu *menuCol = new wxMenu;
98ec9dbe
VZ
255 menuCol->Append(LIST_SET_FG_COL, _T("&Foreground colour..."));
256 menuCol->Append(LIST_SET_BG_COL, _T("&Background colour..."));
c41ea66a
VZ
257
258 wxMenuBar *menubar = new wxMenuBar;
98ec9dbe
VZ
259 menubar->Append(menuFile, _T("&File"));
260 menubar->Append(menuView, _T("&View"));
261 menubar->Append(menuList, _T("&List"));
262 menubar->Append(menuCol, _T("&Colour"));
c41ea66a
VZ
263 SetMenuBar(menubar);
264
1550e402
WS
265 m_panel = new wxPanel(this, wxID_ANY);
266 m_logWindow = new wxTextCtrl(m_panel, wxID_ANY, wxEmptyString,
c41ea66a 267 wxDefaultPosition, wxDefaultSize,
e1641772 268 wxTE_READONLY | wxTE_MULTILINE | wxSUNKEN_BORDER);
c41ea66a
VZ
269
270 m_logOld = wxLog::SetActiveTarget(new wxLogTextCtrl(m_logWindow));
271
776a33cf 272 RecreateList(wxLC_REPORT | wxLC_SINGLE_SEL);
c41ea66a 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;
301 m_listCtrl->SetSize(0, 0, size.x, y);
302 m_logWindow->SetSize(0, y + 1, size.x, size.y - y);
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 |
efe23391 420 wxSUNKEN_BORDER | 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
c71963cd 655void MyFrame::OnShowColInfo(wxCommandEvent& WXUNUSED(event))
f6bcfd97
BP
656{
657 int count = m_listCtrl->GetColumnCount();
4acb6ca6 658 wxLogMessage(wxT("%d columns:"), count);
f6bcfd97
BP
659 for ( int c = 0; c < count; c++ )
660 {
4acb6ca6 661 wxLogMessage(wxT("\tcolumn %d has width %d"), c,
f6bcfd97
BP
662 m_listCtrl->GetColumnWidth(c));
663 }
664}
665
666void MyFrame::OnUpdateShowColInfo(wxUpdateUIEvent& event)
667{
668 event.Enable( (m_listCtrl->GetWindowStyleFlag() & wxLC_REPORT) != 0 );
669}
670
7b848b0d
VZ
671void MyFrame::OnToggleMultiSel(wxCommandEvent& WXUNUSED(event))
672{
7b848b0d
VZ
673 long flags = m_listCtrl->GetWindowStyleFlag();
674 if ( flags & wxLC_SINGLE_SEL )
776a33cf 675 flags &= ~wxLC_SINGLE_SEL;
7b848b0d 676 else
776a33cf
VZ
677 flags |= wxLC_SINGLE_SEL;
678
4acb6ca6 679 m_logWindow->WriteText(wxString::Format(wxT("Current selection mode: %sle\n"),
efe23391 680 (flags & wxLC_SINGLE_SEL) ? _T("sing") : _T("multip")));
7b848b0d 681
776a33cf 682 RecreateList(flags);
7b848b0d
VZ
683}
684
ae403b11
JS
685void MyFrame::OnUpdateToggleMultiSel(wxUpdateUIEvent& event)
686{
687 event.Check((m_listCtrl->GetWindowStyleFlag() & wxLC_SINGLE_SEL) == 0);
688}
689
11f26ea0
VZ
690void MyFrame::OnSetFgColour(wxCommandEvent& WXUNUSED(event))
691{
692 m_listCtrl->SetForegroundColour(wxGetColourFromUser(this));
693 m_listCtrl->Refresh();
694}
695
696void MyFrame::OnSetBgColour(wxCommandEvent& WXUNUSED(event))
697{
698 m_listCtrl->SetBackgroundColour(wxGetColourFromUser(this));
699 m_listCtrl->Refresh();
700}
701
cf1dfa6b
VZ
702void MyFrame::OnAdd(wxCommandEvent& WXUNUSED(event))
703{
704 m_listCtrl->InsertItem(m_listCtrl->GetItemCount(), _T("Appended item"));
705}
706
efe23391
VZ
707void MyFrame::OnEdit(wxCommandEvent& WXUNUSED(event))
708{
709 long itemCur = m_listCtrl->GetNextItem(-1, wxLIST_NEXT_ALL,
710 wxLIST_STATE_FOCUSED);
711
712 if ( itemCur != -1 )
713 {
714 m_listCtrl->EditLabel(itemCur);
715 }
716 else
717 {
718 m_logWindow->WriteText(_T("No item to edit"));
719 }
720}
721
bec0a261
VZ
722void MyFrame::OnDelete(wxCommandEvent& WXUNUSED(event))
723{
724 if ( m_listCtrl->GetItemCount() )
725 {
726 m_listCtrl->DeleteItem(0);
727 }
728 else
729 {
efe23391 730 m_logWindow->WriteText(_T("Nothing to delete"));
bec0a261
VZ
731 }
732}
733
5ea47806
VZ
734void MyFrame::OnDeleteAll(wxCommandEvent& WXUNUSED(event))
735{
81278df2 736 wxStopWatch sw;
5ea47806 737
b143cf70 738 int itemCount = m_listCtrl->GetItemCount();
fde548e2 739
5ea47806
VZ
740 m_listCtrl->DeleteAllItems();
741
81278df2 742 m_logWindow->WriteText(wxString::Format(_T("Deleting %d items took %ld ms\n"),
fde548e2 743 itemCount,
81278df2 744 sw.Time()));
457814b5
JS
745}
746
747// MyListCtrl
748
614391dc
VZ
749void MyListCtrl::OnCacheHint(wxListEvent& event)
750{
4acb6ca6 751 wxLogMessage( wxT("OnCacheHint: cache items %ld..%ld"),
614391dc
VZ
752 event.GetCacheFrom(), event.GetCacheTo() );
753}
754
6f806543
VZ
755void MyListCtrl::SetColumnImage(int col, int image)
756{
757 wxListItem item;
758 item.SetMask(wxLIST_MASK_IMAGE);
759 item.SetImage(image);
760 SetColumn(col, item);
761}
762
8636aed8
RR
763void MyListCtrl::OnColClick(wxListEvent& event)
764{
6f806543 765 int col = event.GetColumn();
f0cf38b7
VZ
766
767 // set or unset image
4979a56c 768 static bool x = false;
f0cf38b7
VZ
769 x = !x;
770 SetColumnImage(col, x ? 0 : -1);
6f806543
VZ
771
772 wxLogMessage( wxT("OnColumnClick at %d."), col );
8636aed8
RR
773}
774
11358d39
VZ
775void MyListCtrl::OnColRightClick(wxListEvent& event)
776{
6f806543 777 int col = event.GetColumn();
62313c27
VZ
778 if ( col != -1 )
779 {
780 SetColumnImage(col, -1);
781 }
6f806543 782
77ace0c5
WS
783 // Show popupmenu at position
784 wxMenu menu(wxT("Test"));
785 menu.Append(LIST_ABOUT, _T("&About"));
5f4d35b8 786 PopupMenu(&menu, event.GetPoint());
77ace0c5 787
11358d39
VZ
788 wxLogMessage( wxT("OnColumnRightClick at %d."), event.GetColumn() );
789}
790
2b5f62a0
VZ
791void MyListCtrl::LogColEvent(const wxListEvent& event, const wxChar *name)
792{
793 const int col = event.GetColumn();
794
795 wxLogMessage(wxT("%s: column %d (width = %d or %d)."),
796 name,
797 col,
798 event.GetItem().GetWidth(),
799 GetColumnWidth(col));
800}
801
11358d39
VZ
802void MyListCtrl::OnColBeginDrag(wxListEvent& event)
803{
2b5f62a0 804 LogColEvent( event, wxT("OnColBeginDrag") );
355f2407
VZ
805
806 if ( event.GetColumn() == 0 )
807 {
808 wxLogMessage(_T("Resizing this column shouldn't work."));
809
810 event.Veto();
811 }
11358d39
VZ
812}
813
814void MyListCtrl::OnColDragging(wxListEvent& event)
815{
2b5f62a0 816 LogColEvent( event, wxT("OnColDragging") );
11358d39
VZ
817}
818
819void MyListCtrl::OnColEndDrag(wxListEvent& event)
820{
2b5f62a0 821 LogColEvent( event, wxT("OnColEndDrag") );
11358d39
VZ
822}
823
fd9811b1 824void MyListCtrl::OnBeginDrag(wxListEvent& event)
457814b5 825{
6dc34ebb
VZ
826 const wxPoint& pt = event.m_pointDrag;
827
828 int flags;
829 wxLogMessage( wxT("OnBeginDrag at (%d, %d), item %ld."),
830 pt.x, pt.y, HitTest(pt, flags) );
457814b5
JS
831}
832
fd9811b1 833void MyListCtrl::OnBeginRDrag(wxListEvent& event)
457814b5 834{
4acb6ca6 835 wxLogMessage( wxT("OnBeginRDrag at %d,%d."),
c41ea66a 836 event.m_pointDrag.x, event.m_pointDrag.y );
457814b5
JS
837}
838
fd9811b1 839void MyListCtrl::OnBeginLabelEdit(wxListEvent& event)
457814b5 840{
4acb6ca6 841 wxLogMessage( wxT("OnBeginLabelEdit: %s"), event.m_item.m_text.c_str());
508b6523
VZ
842
843 wxTextCtrl * const text = GetEditControl();
844 if ( !text )
845 {
846 wxLogMessage("BUG: started to edit but no edit control");
847 }
848 else
849 {
850 wxLogMessage("Edit control value: \"%s\"", text->GetValue());
851 }
457814b5
JS
852}
853
fd9811b1 854void MyListCtrl::OnEndLabelEdit(wxListEvent& event)
457814b5 855{
1bf37bf5 856 wxLogMessage( wxT("OnEndLabelEdit: %s"),
15836000
CE
857 (
858 event.IsEditCancelled() ?
859 wxString("[cancelled]") :
860 event.m_item.m_text
861 ).c_str()
862 );
457814b5
JS
863}
864
efbb7287 865void MyListCtrl::OnDeleteItem(wxListEvent& event)
457814b5 866{
c41ea66a 867 LogEvent(event, _T("OnDeleteItem"));
a95d5751 868 wxLogMessage( wxT("Number of items when delete event is sent: %d"), GetItemCount() );
457814b5
JS
869}
870
c41ea66a 871void MyListCtrl::OnDeleteAllItems(wxListEvent& event)
12c1b46a 872{
c41ea66a 873 LogEvent(event, _T("OnDeleteAllItems"));
12c1b46a
RR
874}
875
74b31181 876void MyListCtrl::OnSelected(wxListEvent& event)
457814b5 877{
c41ea66a 878 LogEvent(event, _T("OnSelected"));
457814b5 879
40779a03 880 if ( GetWindowStyle() & wxLC_REPORT )
74b31181 881 {
40779a03
VZ
882 wxListItem info;
883 info.m_itemId = event.m_itemIndex;
884 info.m_col = 1;
885 info.m_mask = wxLIST_MASK_TEXT;
886 if ( GetItem(info) )
887 {
4acb6ca6 888 wxLogMessage(wxT("Value of the 2nd field of the selected item: %s"),
c41ea66a 889 info.m_text.c_str());
40779a03
VZ
890 }
891 else
892 {
4acb6ca6 893 wxFAIL_MSG(wxT("wxListCtrl::GetItem() failed"));
40779a03 894 }
74b31181 895 }
457814b5
JS
896}
897
efbb7287 898void MyListCtrl::OnDeselected(wxListEvent& event)
457814b5 899{
c41ea66a 900 LogEvent(event, _T("OnDeselected"));
457814b5
JS
901}
902
efbb7287 903void MyListCtrl::OnActivated(wxListEvent& event)
435fe83e 904{
c41ea66a 905 LogEvent(event, _T("OnActivated"));
435fe83e
RR
906}
907
0ddefeb0
VZ
908void MyListCtrl::OnFocused(wxListEvent& event)
909{
910 LogEvent(event, _T("OnFocused"));
27770773
VZ
911
912 event.Skip();
0ddefeb0
VZ
913}
914
8e1d4f96 915void MyListCtrl::OnListKeyDown(wxListEvent& event)
457814b5 916{
100f649f
VZ
917 long item;
918
2936eaf0 919 switch ( event.GetKeyCode() )
5cd89174 920 {
ebc9b89d 921 case 'C': // colorize
6c02c329
VZ
922 {
923 wxListItem info;
924 info.m_itemId = event.GetIndex();
63c95faf
VZ
925 if ( info.m_itemId == -1 )
926 {
927 // no item
928 break;
929 }
930
6c02c329
VZ
931 GetItem(info);
932
933 wxListItemAttr *attr = info.GetAttributes();
934 if ( !attr || !attr->HasTextColour() )
935 {
936 info.SetTextColour(*wxCYAN);
937
938 SetItem(info);
2d33aec9
VZ
939
940 RefreshItem(info.m_itemId);
6c02c329
VZ
941 }
942 }
943 break;
944
ebc9b89d 945 case 'N': // next
100f649f
VZ
946 item = GetNextItem(-1, wxLIST_NEXT_ALL, wxLIST_STATE_FOCUSED);
947 if ( item++ == GetItemCount() - 1 )
948 {
949 item = 0;
950 }
951
952 wxLogMessage(_T("Focusing item %ld"), item);
953
954 SetItemState(item, wxLIST_STATE_FOCUSED, wxLIST_STATE_FOCUSED);
955 EnsureVisible(item);
956 break;
957
ebc9b89d 958 case 'R': // show bounding rectangle
2d33aec9 959 {
100f649f
VZ
960 item = event.GetIndex();
961 wxRect r;
962 if ( !GetItemRect(item, r) )
2d33aec9 963 {
100f649f
VZ
964 wxLogError(_T("Failed to retrieve rect of item %ld"), item);
965 break;
2d33aec9
VZ
966 }
967
100f649f
VZ
968 wxLogMessage(_T("Bounding rect of item %ld is (%d, %d)-(%d, %d)"),
969 item, r.x, r.y, r.x + r.width, r.y + r.height);
2d33aec9
VZ
970 }
971 break;
972
e974c5d2
VZ
973 case '1': // show sub item bounding rectangle
974 case '2':
975 case '3':
976 case '4': // this column is invalid but we want to test it too
977 if ( InReportView() )
978 {
979 int subItem = event.GetKeyCode() - '1';
980 item = event.GetIndex();
981 wxRect r;
982 if ( !GetSubItemRect(item, subItem, r) )
983 {
984 wxLogError(_T("Failed to retrieve rect of item %ld column %d"), item, subItem + 1);
985 break;
986 }
987
988 wxLogMessage(_T("Bounding rect of item %ld column %d is (%d, %d)-(%d, %d)"),
989 item, subItem + 1,
990 r.x, r.y, r.x + r.width, r.y + r.height);
991 }
992 break;
993
ebc9b89d
VZ
994 case 'U': // update
995 if ( !IsVirtual() )
996 break;
997
998 if ( m_updated != -1 )
999 RefreshItem(m_updated);
1000
1001 m_updated = event.GetIndex();
1002 if ( m_updated != -1 )
1003 {
1004 // we won't see changes to this item as it's selected, update
1005 // the next one (or the first one if we're on the last item)
1006 if ( ++m_updated == GetItemCount() )
1007 m_updated = 0;
1008
1009 wxLogMessage("Updating colour of the item %ld", m_updated);
1010 RefreshItem(m_updated);
1011 }
1012 break;
1013
17483f70 1014 case 'D': // delete
100f649f
VZ
1015 item = GetNextItem(-1, wxLIST_NEXT_ALL, wxLIST_STATE_SELECTED);
1016 while ( item != -1 )
91c6cc0e 1017 {
100f649f 1018 DeleteItem(item);
91c6cc0e 1019
100f649f 1020 wxLogMessage(_T("Item %ld deleted"), item);
f6bcfd97 1021
100f649f
VZ
1022 // -1 because the indices were shifted by DeleteItem()
1023 item = GetNextItem(item - 1,
1024 wxLIST_NEXT_ALL, wxLIST_STATE_SELECTED);
91c6cc0e 1025 }
5cd89174
VZ
1026 break;
1027
17483f70 1028 case 'I': // insert
5cd89174
VZ
1029 if ( GetWindowStyle() & wxLC_REPORT )
1030 {
1031 if ( GetWindowStyle() & wxLC_VIRTUAL )
1032 {
1033 SetItemCount(GetItemCount() + 1);
1034 }
1035 else // !virtual
1036 {
1037 InsertItemInReportView(event.GetIndex());
1038 }
1039 }
1040 //else: fall through
1041
1042 default:
1043 LogEvent(event, _T("OnListKeyDown"));
1044
1045 event.Skip();
1046 }
f6bcfd97
BP
1047}
1048
1049void MyListCtrl::OnChar(wxKeyEvent& event)
1050{
1051 wxLogMessage(_T("Got char event."));
1052
2d33aec9
VZ
1053 switch ( event.GetKeyCode() )
1054 {
1055 case 'n':
1056 case 'N':
1057 case 'c':
1058 case 'C':
ebc9b89d
VZ
1059 case 'r':
1060 case 'R':
1061 case 'u':
1062 case 'U':
17483f70
VZ
1063 case 'd':
1064 case 'D':
1065 case 'i':
1066 case 'I':
2d33aec9
VZ
1067 // these are the keys we process ourselves
1068 break;
1069
1070 default:
1071 event.Skip();
1072 }
457814b5
JS
1073}
1074
164a7972
VZ
1075void MyListCtrl::OnRightClick(wxMouseEvent& event)
1076{
1077 if ( !event.ControlDown() )
1078 {
1079 event.Skip();
1080 return;
1081 }
1082
1083 int flags;
1084 long subitem;
1085 long item = HitTest(event.GetPosition(), flags, &subitem);
1086
1087 wxString where;
1088 switch ( flags )
1089 {
1090 case wxLIST_HITTEST_ABOVE: where = _T("above"); break;
1091 case wxLIST_HITTEST_BELOW: where = _T("below"); break;
1092 case wxLIST_HITTEST_NOWHERE: where = _T("nowhere near"); break;
1093 case wxLIST_HITTEST_ONITEMICON: where = _T("on icon of"); break;
1094 case wxLIST_HITTEST_ONITEMLABEL: where = _T("on label of"); break;
1095 case wxLIST_HITTEST_ONITEMRIGHT: where = _T("right on"); break;
1096 case wxLIST_HITTEST_TOLEFT: where = _T("to the left of"); break;
1097 case wxLIST_HITTEST_TORIGHT: where = _T("to the right of"); break;
1098 default: where = _T("not clear exactly where on"); break;
1099 }
1100
1101 wxLogMessage(_T("Right double click %s item %ld, subitem %ld"),
1102 where.c_str(), item, subitem);
1103}
1104
c41ea66a
VZ
1105void MyListCtrl::LogEvent(const wxListEvent& event, const wxChar *eventName)
1106{
f6bcfd97
BP
1107 wxLogMessage(_T("Item %ld: %s (item text = %s, data = %ld)"),
1108 event.GetIndex(), eventName,
1109 event.GetText().c_str(), event.GetData());
c41ea66a 1110}
435fe83e 1111
98ec9dbe
VZ
1112wxString MyListCtrl::OnGetItemText(long item, long column) const
1113{
b0401bea
VZ
1114 if ( GetItemCount() == WXSIZEOF(SMALL_VIRTUAL_VIEW_ITEMS) )
1115 {
1116 return SMALL_VIRTUAL_VIEW_ITEMS[item][column];
1117 }
ebc9b89d 1118 else // "big" virtual control
b0401bea
VZ
1119 {
1120 return wxString::Format(_T("Column %ld of item %ld"), column, item);
1121 }
98ec9dbe
VZ
1122}
1123
06db67bc 1124int MyListCtrl::OnGetItemColumnImage(long item, long column) const
98ec9dbe 1125{
06db67bc
RD
1126 if (!column)
1127 return 0;
1128
ebc9b89d 1129 if (!(item % 3) && column == 1)
06db67bc
RD
1130 return 0;
1131
1132 return -1;
98ec9dbe
VZ
1133}
1134
6c02c329
VZ
1135wxListItemAttr *MyListCtrl::OnGetItemAttr(long item) const
1136{
ebc9b89d
VZ
1137 // test to check that RefreshItem() works correctly: when m_updated is
1138 // set to some item and it is refreshed, we highlight the item
1139 if ( item == m_updated )
1140 {
1141 static wxListItemAttr s_attrHighlight(*wxRED, wxNullColour, wxNullFont);
1142 return &s_attrHighlight;
1143 }
1144
6c02c329
VZ
1145 return item % 2 ? NULL : (wxListItemAttr *)&m_attr;
1146}
1147
5cd89174
VZ
1148void MyListCtrl::InsertItemInReportView(int i)
1149{
1150 wxString buf;
1151 buf.Printf(_T("This is item %d"), i);
1152 long tmp = InsertItem(i, buf, 0);
1153 SetItemData(tmp, i);
1154
1155 buf.Printf(_T("Col 1, item %d"), i);
eb257f65 1156 SetItem(tmp, 1, buf);
5cd89174
VZ
1157
1158 buf.Printf(_T("Item %d in column 2"), i);
eb257f65 1159 SetItem(tmp, 2, buf);
5cd89174 1160}
107ff668
JS
1161
1162#if USE_CONTEXT_MENU
1163void MyListCtrl::OnContextMenu(wxContextMenuEvent& event)
1164{
d53b09d8
VZ
1165 if (GetEditControl() == NULL)
1166 {
1167 wxPoint point = event.GetPosition();
1168 // If from keyboard
1169 if ( (point.x == -1) && (point.y == -1) )
1170 {
1171 wxSize size = GetSize();
1172 point.x = size.x / 2;
1173 point.y = size.y / 2;
1174 }
1175 else
1176 {
1177 point = ScreenToClient(point);
1178 }
1179 ShowContextMenu(point);
1180 }
1181 else
1182 {
1183 // the user is editing:
1184 // allow the text control to display its context menu
1185 // if it has one (it has on Windows) rather than display our one
1186 event.Skip();
107ff668 1187 }
107ff668
JS
1188}
1189#endif
1190
1191void MyListCtrl::ShowContextMenu(const wxPoint& pos)
1192{
1193 wxMenu menu;
1194
1195 menu.Append(wxID_ABOUT, _T("&About"));
1196 menu.AppendSeparator();
1197 menu.Append(wxID_EXIT, _T("E&xit"));
1198
1199 PopupMenu(&menu, pos.x, pos.y);
1200}