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