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