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