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