]> git.saurik.com Git - wxWidgets.git/blame - samples/listctrl/listtest.cpp
Added sample to reproduce resize display bug.
[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$
8// Copyright: (c) Julian Smart and Markus Holzem
5ea47806 9// Licence: wxWindows license
457814b5
JS
10/////////////////////////////////////////////////////////////////////////////
11
12#ifdef __GNUG__
13#pragma implementation
14#pragma interface
15#endif
16
17// For compilers that support precompilation, includes "wx/wx.h".
18#include "wx/wxprec.h"
19
20#ifdef __BORLANDC__
21#pragma hdrstop
22#endif
23
24#ifndef WX_PRECOMP
25#include "wx/wx.h"
26#endif
27
907789a0 28#ifndef __WXMSW__
c41ea66a
VZ
29 #include "mondrian.xpm"
30
31 #include "bitmaps/toolbrai.xpm"
32 #include "bitmaps/toolchar.xpm"
33 #include "bitmaps/tooldata.xpm"
34 #include "bitmaps/toolnote.xpm"
35 #include "bitmaps/tooltodo.xpm"
36 #include "bitmaps/toolchec.xpm"
37 #include "bitmaps/toolgame.xpm"
38 #include "bitmaps/tooltime.xpm"
39 #include "bitmaps/toolword.xpm"
40 #include "bitmaps/small1.xpm"
907789a0
RR
41#endif
42
2e8a1588 43#include "wx/imaglist.h"
457814b5 44#include "wx/listctrl.h"
81278df2 45#include "wx/timer.h" // for wxStopWatch
11f26ea0
VZ
46#include "wx/colordlg.h" // for wxGetColourFromUser
47
457814b5
JS
48#include "listtest.h"
49
50BEGIN_EVENT_TABLE(MyFrame, wxFrame)
98ec9dbe
VZ
51 EVT_SIZE(MyFrame::OnSize)
52
5ea47806
VZ
53 EVT_MENU(LIST_QUIT, MyFrame::OnQuit)
54 EVT_MENU(LIST_ABOUT, MyFrame::OnAbout)
55 EVT_MENU(LIST_LIST_VIEW, MyFrame::OnListView)
56 EVT_MENU(LIST_REPORT_VIEW, MyFrame::OnReportView)
57 EVT_MENU(LIST_ICON_VIEW, MyFrame::OnIconView)
58 EVT_MENU(LIST_ICON_TEXT_VIEW, MyFrame::OnIconTextView)
59 EVT_MENU(LIST_SMALL_ICON_VIEW, MyFrame::OnSmallIconView)
60 EVT_MENU(LIST_SMALL_ICON_TEXT_VIEW, MyFrame::OnSmallIconTextView)
98ec9dbe
VZ
61 EVT_MENU(LIST_VIRTUAL_VIEW, MyFrame::OnVirtualView)
62
88b792af 63 EVT_MENU(LIST_FOCUS_LAST, MyFrame::OnFocusLast)
58b3bdc9 64 EVT_MENU(LIST_TOGGLE_FIRST, MyFrame::OnToggleFirstSel)
5ea47806
VZ
65 EVT_MENU(LIST_DESELECT_ALL, MyFrame::OnDeselectAll)
66 EVT_MENU(LIST_SELECT_ALL, MyFrame::OnSelectAll)
bec0a261 67 EVT_MENU(LIST_DELETE, MyFrame::OnDelete)
cf1dfa6b 68 EVT_MENU(LIST_ADD, MyFrame::OnAdd)
5ea47806 69 EVT_MENU(LIST_DELETE_ALL, MyFrame::OnDeleteAll)
fa5f6926 70 EVT_MENU(LIST_SORT, MyFrame::OnSort)
11f26ea0
VZ
71 EVT_MENU(LIST_SET_FG_COL, MyFrame::OnSetFgColour)
72 EVT_MENU(LIST_SET_BG_COL, MyFrame::OnSetBgColour)
7b848b0d 73 EVT_MENU(LIST_TOGGLE_MULTI_SEL, MyFrame::OnToggleMultiSel)
f6bcfd97 74 EVT_MENU(LIST_SHOW_COL_INFO, MyFrame::OnShowColInfo)
b54e41c5 75 EVT_MENU(LIST_SHOW_SEL_INFO, MyFrame::OnShowSelInfo)
c5c528fc
VZ
76 EVT_MENU(LIST_FREEZE, MyFrame::OnFreeze)
77 EVT_MENU(LIST_THAW, MyFrame::OnThaw)
98ec9dbe 78
f6bcfd97 79 EVT_UPDATE_UI(LIST_SHOW_COL_INFO, MyFrame::OnUpdateShowColInfo)
457814b5
JS
80END_EVENT_TABLE()
81
82BEGIN_EVENT_TABLE(MyListCtrl, wxListCtrl)
5ea47806
VZ
83 EVT_LIST_BEGIN_DRAG(LIST_CTRL, MyListCtrl::OnBeginDrag)
84 EVT_LIST_BEGIN_RDRAG(LIST_CTRL, MyListCtrl::OnBeginRDrag)
85 EVT_LIST_BEGIN_LABEL_EDIT(LIST_CTRL, MyListCtrl::OnBeginLabelEdit)
86 EVT_LIST_END_LABEL_EDIT(LIST_CTRL, MyListCtrl::OnEndLabelEdit)
87 EVT_LIST_DELETE_ITEM(LIST_CTRL, MyListCtrl::OnDeleteItem)
12c1b46a 88 EVT_LIST_DELETE_ALL_ITEMS(LIST_CTRL, MyListCtrl::OnDeleteAllItems)
5ea47806
VZ
89 EVT_LIST_GET_INFO(LIST_CTRL, MyListCtrl::OnGetInfo)
90 EVT_LIST_SET_INFO(LIST_CTRL, MyListCtrl::OnSetInfo)
91 EVT_LIST_ITEM_SELECTED(LIST_CTRL, MyListCtrl::OnSelected)
92 EVT_LIST_ITEM_DESELECTED(LIST_CTRL, MyListCtrl::OnDeselected)
93 EVT_LIST_KEY_DOWN(LIST_CTRL, MyListCtrl::OnListKeyDown)
94 EVT_LIST_ITEM_ACTIVATED(LIST_CTRL, MyListCtrl::OnActivated)
0ddefeb0 95 EVT_LIST_ITEM_FOCUSED(LIST_CTRL, MyListCtrl::OnFocused)
11358d39 96
8636aed8 97 EVT_LIST_COL_CLICK(LIST_CTRL, MyListCtrl::OnColClick)
11358d39
VZ
98 EVT_LIST_COL_RIGHT_CLICK(LIST_CTRL, MyListCtrl::OnColRightClick)
99 EVT_LIST_COL_BEGIN_DRAG(LIST_CTRL, MyListCtrl::OnColBeginDrag)
100 EVT_LIST_COL_DRAGGING(LIST_CTRL, MyListCtrl::OnColDragging)
101 EVT_LIST_COL_END_DRAG(LIST_CTRL, MyListCtrl::OnColEndDrag)
102
614391dc 103 EVT_LIST_CACHE_HINT(LIST_CTRL, MyListCtrl::OnCacheHint)
f6bcfd97
BP
104
105 EVT_CHAR(MyListCtrl::OnChar)
457814b5
JS
106END_EVENT_TABLE()
107
108IMPLEMENT_APP(MyApp)
109
776a33cf
VZ
110// number of items in list/report view
111static const int NUM_ITEMS = 30;
112
113// number of items in icon/small icon view
114static const int NUM_ICONS = 9;
115
fa5f6926
VZ
116int wxCALLBACK MyCompareFunction(long item1, long item2, long sortData)
117{
118 // inverse the order
119 return item1 < item2;
120}
121
457814b5 122// `Main program' equivalent, creating windows and returning main app frame
11f26ea0 123bool MyApp::OnInit()
457814b5
JS
124{
125 // Create the main frame window
4acb6ca6 126 MyFrame *frame = new MyFrame(wxT("wxListCtrl Test"), 50, 50, 450, 340);
457814b5
JS
127
128 // Show the frame
129 frame->Show(TRUE);
5ea47806 130
457814b5
JS
131 SetTopWindow(frame);
132
133 return TRUE;
134}
135
136// My frame constructor
c41ea66a
VZ
137MyFrame::MyFrame(const wxChar *title, int x, int y, int w, int h)
138 : wxFrame((wxFrame *)NULL, -1, title, wxPoint(x, y), wxSize(w, h))
457814b5 139{
5ea47806
VZ
140 m_listCtrl = (MyListCtrl *) NULL;
141 m_logWindow = (wxTextCtrl *) NULL;
c41ea66a
VZ
142
143 // Give it an icon
144 SetIcon( wxICON(mondrian) );
145
146 // Make an image list containing large icons
147 m_imageListNormal = new wxImageList(32, 32, TRUE);
148 m_imageListSmall = new wxImageList(16, 16, TRUE);
149
150#ifdef __WXMSW__
151 m_imageListNormal->Add( wxIcon("icon1", wxBITMAP_TYPE_ICO_RESOURCE) );
152 m_imageListNormal->Add( wxIcon("icon2", wxBITMAP_TYPE_ICO_RESOURCE) );
153 m_imageListNormal->Add( wxIcon("icon3", wxBITMAP_TYPE_ICO_RESOURCE) );
154 m_imageListNormal->Add( wxIcon("icon4", wxBITMAP_TYPE_ICO_RESOURCE) );
155 m_imageListNormal->Add( wxIcon("icon5", wxBITMAP_TYPE_ICO_RESOURCE) );
156 m_imageListNormal->Add( wxIcon("icon6", wxBITMAP_TYPE_ICO_RESOURCE) );
157 m_imageListNormal->Add( wxIcon("icon7", wxBITMAP_TYPE_ICO_RESOURCE) );
158 m_imageListNormal->Add( wxIcon("icon8", wxBITMAP_TYPE_ICO_RESOURCE) );
159 m_imageListNormal->Add( wxIcon("icon9", wxBITMAP_TYPE_ICO_RESOURCE) );
160
161 m_imageListSmall->Add( wxIcon("iconsmall", wxBITMAP_TYPE_ICO_RESOURCE) );
162
163#else
164 m_imageListNormal->Add( wxIcon( toolbrai_xpm ) );
165 m_imageListNormal->Add( wxIcon( toolchar_xpm ) );
166 m_imageListNormal->Add( wxIcon( tooldata_xpm ) );
167 m_imageListNormal->Add( wxIcon( toolnote_xpm ) );
168 m_imageListNormal->Add( wxIcon( tooltodo_xpm ) );
169 m_imageListNormal->Add( wxIcon( toolchec_xpm ) );
170 m_imageListNormal->Add( wxIcon( toolgame_xpm ) );
171 m_imageListNormal->Add( wxIcon( tooltime_xpm ) );
172 m_imageListNormal->Add( wxIcon( toolword_xpm ) );
173
174 m_imageListSmall->Add( wxIcon( small1_xpm) );
175#endif
176
177 // Make a menubar
178 wxMenu *menuFile = new wxMenu;
98ec9dbe 179 menuFile->Append(LIST_ABOUT, _T("&About"));
c41ea66a 180 menuFile->AppendSeparator();
98ec9dbe 181 menuFile->Append(LIST_QUIT, _T("E&xit\tAlt-X"));
c41ea66a
VZ
182
183 wxMenu *menuView = new wxMenu;
98ec9dbe
VZ
184 menuView->Append(LIST_LIST_VIEW, _T("&List view\tF1"));
185 menuView->Append(LIST_REPORT_VIEW, _T("&Report view\tF2"));
186 menuView->Append(LIST_ICON_VIEW, _T("&Icon view\tF3"));
187 menuView->Append(LIST_ICON_TEXT_VIEW, _T("Icon view with &text\tF4"));
188 menuView->Append(LIST_SMALL_ICON_VIEW, _T("&Small icon view\tF5"));
189 menuView->Append(LIST_SMALL_ICON_TEXT_VIEW, _T("Small icon &view with text\tF6"));
190 menuView->Append(LIST_VIRTUAL_VIEW, _T("Virtual view\tF7"));
c41ea66a
VZ
191
192 wxMenu *menuList = new wxMenu;
88b792af 193 menuList->Append(LIST_FOCUS_LAST, _T("&Make last item current\tCtrl-L"));
c5c528fc 194 menuList->Append(LIST_TOGGLE_FIRST, _T("To&ggle first item\tCtrl-G"));
98ec9dbe
VZ
195 menuList->Append(LIST_DESELECT_ALL, _T("&Deselect All\tCtrl-D"));
196 menuList->Append(LIST_SELECT_ALL, _T("S&elect All\tCtrl-A"));
b54e41c5 197 menuList->AppendSeparator();
98ec9dbe 198 menuList->Append(LIST_SHOW_COL_INFO, _T("Show &column info\tCtrl-C"));
b54e41c5 199 menuList->Append(LIST_SHOW_SEL_INFO, _T("Show &selected items\tCtrl-S"));
c41ea66a 200 menuList->AppendSeparator();
98ec9dbe 201 menuList->Append(LIST_SORT, _T("&Sort\tCtrl-S"));
c41ea66a 202 menuList->AppendSeparator();
cf1dfa6b
VZ
203 menuList->Append(LIST_ADD, _T("&Append an item\tCtrl-P"));
204 menuList->Append(LIST_DELETE, _T("&Delete first item\tCtrl-X"));
98ec9dbe 205 menuList->Append(LIST_DELETE_ALL, _T("Delete &all items"));
c41ea66a 206 menuList->AppendSeparator();
c5c528fc
VZ
207 menuList->Append(LIST_FREEZE, _T("Free&ze\tCtrl-Z"));
208 menuList->Append(LIST_THAW, _T("Tha&w\tCtrl-W"));
209 menuList->AppendSeparator();
98ec9dbe
VZ
210 menuList->Append(LIST_TOGGLE_MULTI_SEL, _T("&Multiple selection\tCtrl-M"),
211 _T("Toggle multiple selection"), TRUE);
c41ea66a
VZ
212
213 wxMenu *menuCol = new wxMenu;
98ec9dbe
VZ
214 menuCol->Append(LIST_SET_FG_COL, _T("&Foreground colour..."));
215 menuCol->Append(LIST_SET_BG_COL, _T("&Background colour..."));
c41ea66a
VZ
216
217 wxMenuBar *menubar = new wxMenuBar;
98ec9dbe
VZ
218 menubar->Append(menuFile, _T("&File"));
219 menubar->Append(menuView, _T("&View"));
220 menubar->Append(menuList, _T("&List"));
221 menubar->Append(menuCol, _T("&Colour"));
c41ea66a
VZ
222 SetMenuBar(menubar);
223
c41ea66a
VZ
224 m_logWindow = new wxTextCtrl(this, -1, wxEmptyString,
225 wxDefaultPosition, wxDefaultSize,
226 wxTE_MULTILINE | wxSUNKEN_BORDER);
227
228 m_logOld = wxLog::SetActiveTarget(new wxLogTextCtrl(m_logWindow));
229
776a33cf 230 RecreateList(wxLC_REPORT | wxLC_SINGLE_SEL);
c41ea66a
VZ
231
232 CreateStatusBar(3);
457814b5
JS
233}
234
11f26ea0 235MyFrame::~MyFrame()
457814b5 236{
c41ea66a
VZ
237 delete wxLog::SetActiveTarget(m_logOld);
238
239 delete m_imageListNormal;
240 delete m_imageListSmall;
457814b5
JS
241}
242
98ec9dbe 243void MyFrame::OnSize(wxSizeEvent& event)
457814b5 244{
98ec9dbe
VZ
245 if ( !m_logWindow )
246 return;
457814b5 247
98ec9dbe
VZ
248 wxSize size = GetClientSize();
249 wxCoord y = (2*size.y)/3;
250 m_listCtrl->SetSize(0, 0, size.x, y);
251 m_logWindow->SetSize(0, y + 1, size.x, size.y - y);
252
253 event.Skip();
57246713
KB
254}
255
98ec9dbe 256void MyFrame::OnQuit(wxCommandEvent& WXUNUSED(event))
57246713 257{
98ec9dbe 258 Close(TRUE);
57246713
KB
259}
260
b0d77f43 261void MyFrame::OnAbout(wxCommandEvent& WXUNUSED(event))
457814b5 262{
5ea47806
VZ
263 wxMessageDialog dialog(this, "List test sample\nJulian Smart (c) 1997",
264 "About list test", wxOK|wxCANCEL);
457814b5 265
5ea47806 266 dialog.ShowModal();
457814b5
JS
267}
268
c5c528fc
VZ
269void MyFrame::OnFreeze(wxCommandEvent& event)
270{
271 wxLogMessage(_T("Freezing the control"));
272
273 m_listCtrl->Freeze();
274}
275
276void MyFrame::OnThaw(wxCommandEvent& event)
277{
278 wxLogMessage(_T("Thawing the control"));
279
280 m_listCtrl->Thaw();
281}
282
88b792af
VZ
283void MyFrame::OnFocusLast(wxCommandEvent& WXUNUSED(event))
284{
285 long index = m_listCtrl->GetItemCount() - 1;
286 if ( index == -1 )
287 {
288 return;
289 }
290
291 m_listCtrl->SetItemState(index, wxLIST_STATE_FOCUSED, wxLIST_STATE_FOCUSED);
292 m_listCtrl->EnsureVisible(index);
293}
294
58b3bdc9
VZ
295void MyFrame::OnToggleFirstSel(wxCommandEvent& WXUNUSED(event))
296{
297 m_listCtrl->SetItemState(0, wxLIST_STATE_SELECTED, wxLIST_STATE_SELECTED);
298}
299
c4771147
KB
300void MyFrame::OnDeselectAll(wxCommandEvent& WXUNUSED(event))
301{
5ea47806
VZ
302 int n = m_listCtrl->GetItemCount();
303 for (int i = 0; i < n; i++)
304 m_listCtrl->SetItemState(i,0,wxLIST_STATE_SELECTED);
c4771147
KB
305}
306
307void MyFrame::OnSelectAll(wxCommandEvent& WXUNUSED(event))
308{
5ea47806
VZ
309 int n = m_listCtrl->GetItemCount();
310 for (int i = 0; i < n; i++)
311 m_listCtrl->SetItemState(i,wxLIST_STATE_SELECTED, wxLIST_STATE_SELECTED);
c4771147
KB
312}
313
776a33cf
VZ
314// ----------------------------------------------------------------------------
315// changing listctrl modes
316// ----------------------------------------------------------------------------
317
318void MyFrame::RecreateList(long flags, bool withText)
457814b5 319{
776a33cf
VZ
320 // we could avoid recreating it if we don't set/clear the wxLC_VIRTUAL
321 // style, but it is more trouble to do it than not
322#if 0
323 if ( !m_listCtrl || ((flags & wxLC_VIRTUAL) !=
324 (m_listCtrl->GetWindowStyleFlag() & wxLC_VIRTUAL)) )
325#endif
326 {
327 delete m_listCtrl;
328
329 m_listCtrl = new MyListCtrl(this, LIST_CTRL,
330 wxDefaultPosition, wxDefaultSize,
331 flags |
332 wxSUNKEN_BORDER);
333
334 switch ( flags & wxLC_MASK_TYPE )
335 {
336 case wxLC_LIST:
337 InitWithListItems();
338 break;
339
340 case wxLC_ICON:
341 InitWithIconItems(withText);
342 break;
343
344 case wxLC_SMALL_ICON:
345 InitWithIconItems(withText, TRUE);
346 break;
347
348 case wxLC_REPORT:
349 if ( flags & wxLC_VIRTUAL )
350 InitWithVirtualItems();
351 else
352 InitWithReportItems();
353 break;
354
355 default:
356 wxFAIL_MSG( _T("unknown listctrl mode") );
357 }
358 }
359
360#ifdef __WXMSW__
361 SendSizeEvent();
362#endif
363
0180dad6 364 m_logWindow->Clear();
776a33cf 365}
95bf655c 366
776a33cf
VZ
367void MyFrame::OnListView(wxCommandEvent& WXUNUSED(event))
368{
369 RecreateList(wxLC_LIST);
370}
457814b5 371
776a33cf
VZ
372void MyFrame::InitWithListItems()
373{
374 for ( int i = 0; i < NUM_ITEMS; i++ )
0180dad6 375 {
98ec9dbe 376 m_listCtrl->InsertItem(i, wxString::Format(_T("Item %d"), i));
0180dad6 377 }
457814b5
JS
378}
379
b0d77f43 380void MyFrame::OnReportView(wxCommandEvent& WXUNUSED(event))
457814b5 381{
776a33cf
VZ
382 RecreateList(wxLC_REPORT);
383}
5ea47806 384
776a33cf
VZ
385void MyFrame::InitWithReportItems()
386{
c41ea66a 387 m_listCtrl->SetImageList(m_imageListSmall, wxIMAGE_LIST_SMALL);
457814b5 388
6f806543
VZ
389 // under MSW for SetColumnWidth() to work we need to create the items with
390 // images initially
391#if 1
392 wxListItem itemCol;
393 itemCol.m_mask = wxLIST_MASK_TEXT | wxLIST_MASK_IMAGE;
394 itemCol.m_text = "Column 1";
395 itemCol.m_image = -1;
396 m_listCtrl->InsertColumn(0, itemCol);
397 itemCol.m_text = "Column 2";
398 m_listCtrl->InsertColumn(1, itemCol);
399 itemCol.m_text = "Column 3";
400 m_listCtrl->InsertColumn(2, itemCol);
401#else
0180dad6
RR
402 m_listCtrl->InsertColumn(0, "Column 1"); // , wxLIST_FORMAT_LEFT, 140);
403 m_listCtrl->InsertColumn(1, "Column 2"); // , wxLIST_FORMAT_LEFT, 140);
404 m_listCtrl->InsertColumn(2, "One More Column (2)"); // , wxLIST_FORMAT_LEFT, 140);
6f806543 405#endif
0180dad6 406
81278df2
VZ
407 // to speed up inserting we hide the control temporarily
408 m_listCtrl->Hide();
409
410 wxStopWatch sw;
411
81278df2 412 for ( int i = 0; i < NUM_ITEMS; i++ )
0180dad6 413 {
5cd89174 414 m_listCtrl->InsertItemInReportView(i);
0180dad6 415 }
bdc72a22 416
81278df2
VZ
417 m_logWindow->WriteText(wxString::Format(_T("%d items inserted in %ldms\n"),
418 NUM_ITEMS, sw.Time()));
419 m_listCtrl->Show();
420
f6b77239 421 // we leave all mask fields to 0 and only change the colour
bdc72a22
VZ
422 wxListItem item;
423 item.m_itemId = 0;
0530737d 424 item.SetTextColour(*wxRED);
bdc72a22
VZ
425 m_listCtrl->SetItem( item );
426
427 item.m_itemId = 2;
d62228a6 428 item.SetTextColour(*wxGREEN);
bdc72a22 429 m_listCtrl->SetItem( item );
d62228a6 430 item.m_itemId = 4;
bdc72a22 431 item.SetTextColour(*wxLIGHT_GREY);
d62228a6
VZ
432 item.SetFont(*wxITALIC_FONT);
433 item.SetBackgroundColour(*wxRED);
bdc72a22 434 m_listCtrl->SetItem( item );
5ea47806 435
d62228a6
VZ
436 m_listCtrl->SetTextColour(*wxBLUE);
437 m_listCtrl->SetBackgroundColour(*wxLIGHT_GREY);
438
0180dad6
RR
439 m_listCtrl->SetColumnWidth( 0, wxLIST_AUTOSIZE );
440 m_listCtrl->SetColumnWidth( 1, wxLIST_AUTOSIZE );
441 m_listCtrl->SetColumnWidth( 2, wxLIST_AUTOSIZE );
457814b5
JS
442}
443
776a33cf 444void MyFrame::InitWithIconItems(bool withText, bool sameIcon)
457814b5 445{
c41ea66a
VZ
446 m_listCtrl->SetImageList(m_imageListNormal, wxIMAGE_LIST_NORMAL);
447 m_listCtrl->SetImageList(m_imageListSmall, wxIMAGE_LIST_SMALL);
457814b5 448
776a33cf 449 for ( int i = 0; i < NUM_ICONS; i++ )
5ea47806 450 {
776a33cf
VZ
451 int image = sameIcon ? 0 : i;
452
453 if ( withText )
454 {
455 m_listCtrl->InsertItem(i, wxString::Format(_T("Label %d"), i),
456 image);
457 }
458 else
459 {
460 m_listCtrl->InsertItem(i, image);
461 }
5ea47806 462 }
457814b5
JS
463}
464
776a33cf 465void MyFrame::OnIconView(wxCommandEvent& WXUNUSED(event))
457814b5 466{
776a33cf
VZ
467 RecreateList(wxLC_ICON, FALSE);
468}
457814b5 469
776a33cf
VZ
470void MyFrame::OnIconTextView(wxCommandEvent& WXUNUSED(event))
471{
472 RecreateList(wxLC_ICON);
457814b5
JS
473}
474
b0d77f43 475void MyFrame::OnSmallIconView(wxCommandEvent& WXUNUSED(event))
457814b5 476{
776a33cf 477 RecreateList(wxLC_SMALL_ICON, FALSE);
457814b5
JS
478}
479
b0d77f43 480void MyFrame::OnSmallIconTextView(wxCommandEvent& WXUNUSED(event))
457814b5 481{
776a33cf 482 RecreateList(wxLC_SMALL_ICON);
5ea47806
VZ
483}
484
98ec9dbe
VZ
485void MyFrame::OnVirtualView(wxCommandEvent& WXUNUSED(event))
486{
776a33cf
VZ
487 RecreateList(wxLC_REPORT | wxLC_VIRTUAL);
488}
98ec9dbe 489
776a33cf
VZ
490void MyFrame::InitWithVirtualItems()
491{
98ec9dbe
VZ
492 m_listCtrl->InsertColumn(0, "First Column");
493 m_listCtrl->InsertColumn(1, "Second Column");
494 m_listCtrl->SetColumnWidth(0, 150);
495 m_listCtrl->SetColumnWidth(1, 150);
496
497 m_listCtrl->SetItemCount(1000000);
98ec9dbe
VZ
498}
499
fa5f6926
VZ
500void MyFrame::OnSort(wxCommandEvent& WXUNUSED(event))
501{
81278df2
VZ
502 wxStopWatch sw;
503
fa5f6926 504 m_listCtrl->SortItems(MyCompareFunction, 0);
81278df2
VZ
505
506 m_logWindow->WriteText(wxString::Format(_T("Sorting %d items took %ld ms\n"),
507 m_listCtrl->GetItemCount(),
508 sw.Time()));
fa5f6926
VZ
509}
510
b54e41c5
VZ
511void MyFrame::OnShowSelInfo(wxCommandEvent& event)
512{
513 int selCount = m_listCtrl->GetSelectedItemCount();
514 wxLogMessage(_T("%d items selected:"), selCount);
515
516 // don't show too many items
517 size_t shownCount = 0;
518
519 long item = m_listCtrl->GetNextItem(-1, wxLIST_NEXT_ALL,
520 wxLIST_STATE_SELECTED);
521 while ( item != -1 )
522 {
523 wxLogMessage(_T("\t%ld (%s)"),
524 item, m_listCtrl->GetItemText(item).c_str());
525
526 if ( ++shownCount > 10 )
527 {
528 wxLogMessage(_T("\t... more selected items snipped..."));
529 break;
530 }
531
532 item = m_listCtrl->GetNextItem(item, wxLIST_NEXT_ALL,
533 wxLIST_STATE_SELECTED);
534 }
535}
536
f6bcfd97
BP
537void MyFrame::OnShowColInfo(wxCommandEvent& event)
538{
539 int count = m_listCtrl->GetColumnCount();
4acb6ca6 540 wxLogMessage(wxT("%d columns:"), count);
f6bcfd97
BP
541 for ( int c = 0; c < count; c++ )
542 {
4acb6ca6 543 wxLogMessage(wxT("\tcolumn %d has width %d"), c,
f6bcfd97
BP
544 m_listCtrl->GetColumnWidth(c));
545 }
546}
547
548void MyFrame::OnUpdateShowColInfo(wxUpdateUIEvent& event)
549{
550 event.Enable( (m_listCtrl->GetWindowStyleFlag() & wxLC_REPORT) != 0 );
551}
552
7b848b0d
VZ
553void MyFrame::OnToggleMultiSel(wxCommandEvent& WXUNUSED(event))
554{
7b848b0d
VZ
555 long flags = m_listCtrl->GetWindowStyleFlag();
556 if ( flags & wxLC_SINGLE_SEL )
776a33cf 557 flags &= ~wxLC_SINGLE_SEL;
7b848b0d 558 else
776a33cf
VZ
559 flags |= wxLC_SINGLE_SEL;
560
4acb6ca6 561 m_logWindow->WriteText(wxString::Format(wxT("Current selection mode: %sle\n"),
776a33cf 562 (flags & wxLC_SINGLE_SEL) ? "sing" : "multip"));
7b848b0d 563
776a33cf 564 RecreateList(flags);
7b848b0d
VZ
565}
566
11f26ea0
VZ
567void MyFrame::OnSetFgColour(wxCommandEvent& WXUNUSED(event))
568{
569 m_listCtrl->SetForegroundColour(wxGetColourFromUser(this));
570 m_listCtrl->Refresh();
571}
572
573void MyFrame::OnSetBgColour(wxCommandEvent& WXUNUSED(event))
574{
575 m_listCtrl->SetBackgroundColour(wxGetColourFromUser(this));
576 m_listCtrl->Refresh();
577}
578
cf1dfa6b
VZ
579void MyFrame::OnAdd(wxCommandEvent& WXUNUSED(event))
580{
581 m_listCtrl->InsertItem(m_listCtrl->GetItemCount(), _T("Appended item"));
582}
583
bec0a261
VZ
584void MyFrame::OnDelete(wxCommandEvent& WXUNUSED(event))
585{
586 if ( m_listCtrl->GetItemCount() )
587 {
588 m_listCtrl->DeleteItem(0);
589 }
590 else
591 {
592 m_logWindow->WriteText("Nothing to delete");
593 }
594}
595
5ea47806
VZ
596void MyFrame::OnDeleteAll(wxCommandEvent& WXUNUSED(event))
597{
81278df2 598 wxStopWatch sw;
5ea47806 599
5ea47806
VZ
600 m_listCtrl->DeleteAllItems();
601
81278df2
VZ
602 m_logWindow->WriteText(wxString::Format(_T("Deleting %d items took %ld ms\n"),
603 m_listCtrl->GetItemCount(),
604 sw.Time()));
457814b5
JS
605}
606
607// MyListCtrl
608
614391dc
VZ
609void MyListCtrl::OnCacheHint(wxListEvent& event)
610{
4acb6ca6 611 wxLogMessage( wxT("OnCacheHint: cache items %ld..%ld"),
614391dc
VZ
612 event.GetCacheFrom(), event.GetCacheTo() );
613}
614
6f806543
VZ
615void MyListCtrl::SetColumnImage(int col, int image)
616{
617 wxListItem item;
618 item.SetMask(wxLIST_MASK_IMAGE);
619 item.SetImage(image);
620 SetColumn(col, item);
621}
622
8636aed8
RR
623void MyListCtrl::OnColClick(wxListEvent& event)
624{
6f806543
VZ
625 int col = event.GetColumn();
626 SetColumnImage(col, 0);
627
628 wxLogMessage( wxT("OnColumnClick at %d."), col );
8636aed8
RR
629}
630
11358d39
VZ
631void MyListCtrl::OnColRightClick(wxListEvent& event)
632{
6f806543 633 int col = event.GetColumn();
62313c27
VZ
634 if ( col != -1 )
635 {
636 SetColumnImage(col, -1);
637 }
6f806543 638
11358d39
VZ
639 wxLogMessage( wxT("OnColumnRightClick at %d."), event.GetColumn() );
640}
641
642void MyListCtrl::OnColBeginDrag(wxListEvent& event)
643{
644 wxLogMessage( wxT("OnColBeginDrag: column %d."), event.GetColumn() );
645}
646
647void MyListCtrl::OnColDragging(wxListEvent& event)
648{
649 wxLogMessage( wxT("OnColDragging: column %d."), event.GetColumn() );
650}
651
652void MyListCtrl::OnColEndDrag(wxListEvent& event)
653{
654 wxLogMessage( wxT("OnColEndDrag: column %d."), event.GetColumn() );
655}
656
fd9811b1 657void MyListCtrl::OnBeginDrag(wxListEvent& event)
457814b5 658{
6dc34ebb
VZ
659 const wxPoint& pt = event.m_pointDrag;
660
661 int flags;
662 wxLogMessage( wxT("OnBeginDrag at (%d, %d), item %ld."),
663 pt.x, pt.y, HitTest(pt, flags) );
457814b5
JS
664}
665
fd9811b1 666void MyListCtrl::OnBeginRDrag(wxListEvent& event)
457814b5 667{
4acb6ca6 668 wxLogMessage( wxT("OnBeginRDrag at %d,%d."),
c41ea66a 669 event.m_pointDrag.x, event.m_pointDrag.y );
457814b5
JS
670}
671
fd9811b1 672void MyListCtrl::OnBeginLabelEdit(wxListEvent& event)
457814b5 673{
4acb6ca6 674 wxLogMessage( wxT("OnBeginLabelEdit: %s"), event.m_item.m_text.c_str());
457814b5
JS
675}
676
fd9811b1 677void MyListCtrl::OnEndLabelEdit(wxListEvent& event)
457814b5 678{
4acb6ca6 679 wxLogMessage( wxT("OnEndLabelEdit: %s"), event.m_item.m_text.c_str());
457814b5
JS
680}
681
efbb7287 682void MyListCtrl::OnDeleteItem(wxListEvent& event)
457814b5 683{
c41ea66a 684 LogEvent(event, _T("OnDeleteItem"));
457814b5
JS
685}
686
c41ea66a 687void MyListCtrl::OnDeleteAllItems(wxListEvent& event)
12c1b46a 688{
c41ea66a 689 LogEvent(event, _T("OnDeleteAllItems"));
12c1b46a
RR
690}
691
fd9811b1 692void MyListCtrl::OnGetInfo(wxListEvent& event)
457814b5 693{
c41ea66a 694 wxString msg;
5ea47806 695
c41ea66a 696 msg << "OnGetInfo (" << event.m_item.m_itemId << ", " << event.m_item.m_col << ")";
5ea47806 697 if ( event.m_item.m_mask & wxLIST_MASK_STATE )
c41ea66a 698 msg << " wxLIST_MASK_STATE";
5ea47806 699 if ( event.m_item.m_mask & wxLIST_MASK_TEXT )
c41ea66a 700 msg << " wxLIST_MASK_TEXT";
5ea47806 701 if ( event.m_item.m_mask & wxLIST_MASK_IMAGE )
c41ea66a 702 msg << " wxLIST_MASK_IMAGE";
5ea47806 703 if ( event.m_item.m_mask & wxLIST_MASK_DATA )
c41ea66a 704 msg << " wxLIST_MASK_DATA";
5ea47806 705 if ( event.m_item.m_mask & wxLIST_SET_ITEM )
c41ea66a 706 msg << " wxLIST_SET_ITEM";
5ea47806 707 if ( event.m_item.m_mask & wxLIST_MASK_WIDTH )
c41ea66a 708 msg << " wxLIST_MASK_WIDTH";
5ea47806 709 if ( event.m_item.m_mask & wxLIST_MASK_FORMAT )
c41ea66a 710 msg << " wxLIST_MASK_WIDTH";
5ea47806
VZ
711
712 if ( event.m_item.m_mask & wxLIST_MASK_TEXT )
713 {
714 event.m_item.m_text = "My callback text";
715 }
c41ea66a
VZ
716
717 wxLogMessage(msg);
457814b5
JS
718}
719
efbb7287 720void MyListCtrl::OnSetInfo(wxListEvent& event)
457814b5 721{
c41ea66a 722 LogEvent(event, _T("OnSetInfo"));
457814b5
JS
723}
724
74b31181 725void MyListCtrl::OnSelected(wxListEvent& event)
457814b5 726{
c41ea66a 727 LogEvent(event, _T("OnSelected"));
457814b5 728
40779a03 729 if ( GetWindowStyle() & wxLC_REPORT )
74b31181 730 {
40779a03
VZ
731 wxListItem info;
732 info.m_itemId = event.m_itemIndex;
733 info.m_col = 1;
734 info.m_mask = wxLIST_MASK_TEXT;
735 if ( GetItem(info) )
736 {
4acb6ca6 737 wxLogMessage(wxT("Value of the 2nd field of the selected item: %s"),
c41ea66a 738 info.m_text.c_str());
40779a03
VZ
739 }
740 else
741 {
4acb6ca6 742 wxFAIL_MSG(wxT("wxListCtrl::GetItem() failed"));
40779a03 743 }
74b31181 744 }
457814b5
JS
745}
746
efbb7287 747void MyListCtrl::OnDeselected(wxListEvent& event)
457814b5 748{
c41ea66a 749 LogEvent(event, _T("OnDeselected"));
457814b5
JS
750}
751
efbb7287 752void MyListCtrl::OnActivated(wxListEvent& event)
435fe83e 753{
c41ea66a 754 LogEvent(event, _T("OnActivated"));
435fe83e
RR
755}
756
0ddefeb0
VZ
757void MyListCtrl::OnFocused(wxListEvent& event)
758{
759 LogEvent(event, _T("OnFocused"));
760}
761
8e1d4f96 762void MyListCtrl::OnListKeyDown(wxListEvent& event)
457814b5 763{
5cd89174
VZ
764 switch ( event.GetCode() )
765 {
2d33aec9
VZ
766 case 'c': // colorize
767 case 'C':
6c02c329
VZ
768 {
769 wxListItem info;
770 info.m_itemId = event.GetIndex();
771 GetItem(info);
772
773 wxListItemAttr *attr = info.GetAttributes();
774 if ( !attr || !attr->HasTextColour() )
775 {
776 info.SetTextColour(*wxCYAN);
777
778 SetItem(info);
2d33aec9
VZ
779
780 RefreshItem(info.m_itemId);
6c02c329
VZ
781 }
782 }
783 break;
784
2d33aec9
VZ
785 case 'n': // next
786 case 'N':
787 {
788 long item = GetNextItem(-1,
789 wxLIST_NEXT_ALL, wxLIST_STATE_FOCUSED);
790 if ( item++ == GetItemCount() - 1 )
791 {
792 item = 0;
793 }
794
795 wxLogMessage(_T("Focusing item %ld"), item);
796
797 SetItemState(item, wxLIST_STATE_FOCUSED, wxLIST_STATE_FOCUSED);
798 EnsureVisible(item);
799 }
800 break;
801
5cd89174 802 case WXK_DELETE:
91c6cc0e
VZ
803 {
804 long item = GetNextItem(-1,
805 wxLIST_NEXT_ALL, wxLIST_STATE_SELECTED);
806 while ( item != -1 )
807 {
808 DeleteItem(item);
809
810 wxLogMessage(_T("Item %ld deleted"), item);
f6bcfd97 811
91c6cc0e
VZ
812 // -1 because the indices were shifted by DeleteItem()
813 item = GetNextItem(item - 1,
814 wxLIST_NEXT_ALL, wxLIST_STATE_SELECTED);
815 }
816 }
5cd89174
VZ
817 break;
818
819 case WXK_INSERT:
820 if ( GetWindowStyle() & wxLC_REPORT )
821 {
822 if ( GetWindowStyle() & wxLC_VIRTUAL )
823 {
824 SetItemCount(GetItemCount() + 1);
825 }
826 else // !virtual
827 {
828 InsertItemInReportView(event.GetIndex());
829 }
830 }
831 //else: fall through
832
833 default:
834 LogEvent(event, _T("OnListKeyDown"));
835
836 event.Skip();
837 }
f6bcfd97
BP
838}
839
840void MyListCtrl::OnChar(wxKeyEvent& event)
841{
842 wxLogMessage(_T("Got char event."));
843
2d33aec9
VZ
844 switch ( event.GetKeyCode() )
845 {
846 case 'n':
847 case 'N':
848 case 'c':
849 case 'C':
850 // these are the keys we process ourselves
851 break;
852
853 default:
854 event.Skip();
855 }
457814b5
JS
856}
857
c41ea66a
VZ
858void MyListCtrl::LogEvent(const wxListEvent& event, const wxChar *eventName)
859{
f6bcfd97
BP
860 wxLogMessage(_T("Item %ld: %s (item text = %s, data = %ld)"),
861 event.GetIndex(), eventName,
862 event.GetText().c_str(), event.GetData());
c41ea66a 863}
435fe83e 864
98ec9dbe
VZ
865wxString MyListCtrl::OnGetItemText(long item, long column) const
866{
867 return wxString::Format(_T("Column %ld of item %ld"), column, item);
868}
869
870int MyListCtrl::OnGetItemImage(long item) const
871{
872 return 0;
873}
874
6c02c329
VZ
875wxListItemAttr *MyListCtrl::OnGetItemAttr(long item) const
876{
877 return item % 2 ? NULL : (wxListItemAttr *)&m_attr;
878}
879
5cd89174
VZ
880void MyListCtrl::InsertItemInReportView(int i)
881{
882 wxString buf;
883 buf.Printf(_T("This is item %d"), i);
884 long tmp = InsertItem(i, buf, 0);
885 SetItemData(tmp, i);
886
887 buf.Printf(_T("Col 1, item %d"), i);
888 SetItem(i, 1, buf);
889
890 buf.Printf(_T("Item %d in column 2"), i);
891 SetItem(i, 2, buf);
892}
893