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