more wxListCtrl fixes: inserting/deleting items now works again (tested in the sample...
[wxWidgets.git] / samples / listctrl / listtest.cpp
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
9 // Licence: wxWindows license
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
28 #ifndef __WXMSW__
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"
41 #endif
42
43 #include "wx/imaglist.h"
44 #include "wx/listctrl.h"
45 #include "wx/timer.h" // for wxStopWatch
46 #include "wx/colordlg.h" // for wxGetColourFromUser
47
48 #include "listtest.h"
49
50 BEGIN_EVENT_TABLE(MyFrame, wxFrame)
51 EVT_SIZE(MyFrame::OnSize)
52
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)
61 EVT_MENU(LIST_VIRTUAL_VIEW, MyFrame::OnVirtualView)
62
63 EVT_MENU(LIST_TOGGLE_FIRST, MyFrame::OnToggleFirstSel)
64 EVT_MENU(LIST_DESELECT_ALL, MyFrame::OnDeselectAll)
65 EVT_MENU(LIST_SELECT_ALL, MyFrame::OnSelectAll)
66 EVT_MENU(LIST_DELETE, MyFrame::OnDelete)
67 EVT_MENU(LIST_ADD, MyFrame::OnAdd)
68 EVT_MENU(LIST_DELETE_ALL, MyFrame::OnDeleteAll)
69 EVT_MENU(LIST_SORT, MyFrame::OnSort)
70 EVT_MENU(LIST_SET_FG_COL, MyFrame::OnSetFgColour)
71 EVT_MENU(LIST_SET_BG_COL, MyFrame::OnSetBgColour)
72 EVT_MENU(LIST_TOGGLE_MULTI_SEL, MyFrame::OnToggleMultiSel)
73 EVT_MENU(LIST_SHOW_COL_INFO, MyFrame::OnShowColInfo)
74 EVT_MENU(LIST_SHOW_SEL_INFO, MyFrame::OnShowSelInfo)
75
76 EVT_UPDATE_UI(LIST_SHOW_COL_INFO, MyFrame::OnUpdateShowColInfo)
77 END_EVENT_TABLE()
78
79 BEGIN_EVENT_TABLE(MyListCtrl, wxListCtrl)
80 EVT_LIST_BEGIN_DRAG(LIST_CTRL, MyListCtrl::OnBeginDrag)
81 EVT_LIST_BEGIN_RDRAG(LIST_CTRL, MyListCtrl::OnBeginRDrag)
82 EVT_LIST_BEGIN_LABEL_EDIT(LIST_CTRL, MyListCtrl::OnBeginLabelEdit)
83 EVT_LIST_END_LABEL_EDIT(LIST_CTRL, MyListCtrl::OnEndLabelEdit)
84 EVT_LIST_DELETE_ITEM(LIST_CTRL, MyListCtrl::OnDeleteItem)
85 EVT_LIST_DELETE_ALL_ITEMS(LIST_CTRL, MyListCtrl::OnDeleteAllItems)
86 EVT_LIST_GET_INFO(LIST_CTRL, MyListCtrl::OnGetInfo)
87 EVT_LIST_SET_INFO(LIST_CTRL, MyListCtrl::OnSetInfo)
88 EVT_LIST_ITEM_SELECTED(LIST_CTRL, MyListCtrl::OnSelected)
89 EVT_LIST_ITEM_DESELECTED(LIST_CTRL, MyListCtrl::OnDeselected)
90 EVT_LIST_KEY_DOWN(LIST_CTRL, MyListCtrl::OnListKeyDown)
91 EVT_LIST_ITEM_ACTIVATED(LIST_CTRL, MyListCtrl::OnActivated)
92 EVT_LIST_COL_CLICK(LIST_CTRL, MyListCtrl::OnColClick)
93
94 EVT_CHAR(MyListCtrl::OnChar)
95 END_EVENT_TABLE()
96
97 IMPLEMENT_APP(MyApp)
98
99 // number of items in list/report view
100 static const int NUM_ITEMS = 30;
101
102 // number of items in icon/small icon view
103 static const int NUM_ICONS = 9;
104
105 int wxCALLBACK MyCompareFunction(long item1, long item2, long sortData)
106 {
107 // inverse the order
108 return item1 < item2;
109 }
110
111 // `Main program' equivalent, creating windows and returning main app frame
112 bool MyApp::OnInit()
113 {
114 // Create the main frame window
115 MyFrame *frame = new MyFrame("wxListCtrl Test", 50, 50, 450, 340);
116
117 // Show the frame
118 frame->Show(TRUE);
119
120 SetTopWindow(frame);
121
122 return TRUE;
123 }
124
125 // My frame constructor
126 MyFrame::MyFrame(const wxChar *title, int x, int y, int w, int h)
127 : wxFrame((wxFrame *)NULL, -1, title, wxPoint(x, y), wxSize(w, h))
128 {
129 m_listCtrl = (MyListCtrl *) NULL;
130 m_logWindow = (wxTextCtrl *) NULL;
131
132 // Give it an icon
133 SetIcon( wxICON(mondrian) );
134
135 // Make an image list containing large icons
136 m_imageListNormal = new wxImageList(32, 32, TRUE);
137 m_imageListSmall = new wxImageList(16, 16, TRUE);
138
139 #ifdef __WXMSW__
140 m_imageListNormal->Add( wxIcon("icon1", wxBITMAP_TYPE_ICO_RESOURCE) );
141 m_imageListNormal->Add( wxIcon("icon2", wxBITMAP_TYPE_ICO_RESOURCE) );
142 m_imageListNormal->Add( wxIcon("icon3", wxBITMAP_TYPE_ICO_RESOURCE) );
143 m_imageListNormal->Add( wxIcon("icon4", wxBITMAP_TYPE_ICO_RESOURCE) );
144 m_imageListNormal->Add( wxIcon("icon5", wxBITMAP_TYPE_ICO_RESOURCE) );
145 m_imageListNormal->Add( wxIcon("icon6", wxBITMAP_TYPE_ICO_RESOURCE) );
146 m_imageListNormal->Add( wxIcon("icon7", wxBITMAP_TYPE_ICO_RESOURCE) );
147 m_imageListNormal->Add( wxIcon("icon8", wxBITMAP_TYPE_ICO_RESOURCE) );
148 m_imageListNormal->Add( wxIcon("icon9", wxBITMAP_TYPE_ICO_RESOURCE) );
149
150 m_imageListSmall->Add( wxIcon("iconsmall", wxBITMAP_TYPE_ICO_RESOURCE) );
151
152 #else
153 m_imageListNormal->Add( wxIcon( toolbrai_xpm ) );
154 m_imageListNormal->Add( wxIcon( toolchar_xpm ) );
155 m_imageListNormal->Add( wxIcon( tooldata_xpm ) );
156 m_imageListNormal->Add( wxIcon( toolnote_xpm ) );
157 m_imageListNormal->Add( wxIcon( tooltodo_xpm ) );
158 m_imageListNormal->Add( wxIcon( toolchec_xpm ) );
159 m_imageListNormal->Add( wxIcon( toolgame_xpm ) );
160 m_imageListNormal->Add( wxIcon( tooltime_xpm ) );
161 m_imageListNormal->Add( wxIcon( toolword_xpm ) );
162
163 m_imageListSmall->Add( wxIcon( small1_xpm) );
164 #endif
165
166 // Make a menubar
167 wxMenu *menuFile = new wxMenu;
168 menuFile->Append(LIST_ABOUT, _T("&About"));
169 menuFile->AppendSeparator();
170 menuFile->Append(LIST_QUIT, _T("E&xit\tAlt-X"));
171
172 wxMenu *menuView = new wxMenu;
173 menuView->Append(LIST_LIST_VIEW, _T("&List view\tF1"));
174 menuView->Append(LIST_REPORT_VIEW, _T("&Report view\tF2"));
175 menuView->Append(LIST_ICON_VIEW, _T("&Icon view\tF3"));
176 menuView->Append(LIST_ICON_TEXT_VIEW, _T("Icon view with &text\tF4"));
177 menuView->Append(LIST_SMALL_ICON_VIEW, _T("&Small icon view\tF5"));
178 menuView->Append(LIST_SMALL_ICON_TEXT_VIEW, _T("Small icon &view with text\tF6"));
179 menuView->Append(LIST_VIRTUAL_VIEW, _T("Virtual view\tF7"));
180
181 wxMenu *menuList = new wxMenu;
182 menuList->Append(LIST_TOGGLE_FIRST, _T("&Toggle first item\tCtrl-T"));
183 menuList->Append(LIST_DESELECT_ALL, _T("&Deselect All\tCtrl-D"));
184 menuList->Append(LIST_SELECT_ALL, _T("S&elect All\tCtrl-A"));
185 menuList->AppendSeparator();
186 menuList->Append(LIST_SHOW_COL_INFO, _T("Show &column info\tCtrl-C"));
187 menuList->Append(LIST_SHOW_SEL_INFO, _T("Show &selected items\tCtrl-S"));
188 menuList->AppendSeparator();
189 menuList->Append(LIST_SORT, _T("&Sort\tCtrl-S"));
190 menuList->AppendSeparator();
191 menuList->Append(LIST_ADD, _T("&Append an item\tCtrl-P"));
192 menuList->Append(LIST_DELETE, _T("&Delete first item\tCtrl-X"));
193 menuList->Append(LIST_DELETE_ALL, _T("Delete &all items"));
194 menuList->AppendSeparator();
195 menuList->Append(LIST_TOGGLE_MULTI_SEL, _T("&Multiple selection\tCtrl-M"),
196 _T("Toggle multiple selection"), TRUE);
197
198 wxMenu *menuCol = new wxMenu;
199 menuCol->Append(LIST_SET_FG_COL, _T("&Foreground colour..."));
200 menuCol->Append(LIST_SET_BG_COL, _T("&Background colour..."));
201
202 wxMenuBar *menubar = new wxMenuBar;
203 menubar->Append(menuFile, _T("&File"));
204 menubar->Append(menuView, _T("&View"));
205 menubar->Append(menuList, _T("&List"));
206 menubar->Append(menuCol, _T("&Colour"));
207 SetMenuBar(menubar);
208
209 m_logWindow = new wxTextCtrl(this, -1, wxEmptyString,
210 wxDefaultPosition, wxDefaultSize,
211 wxTE_MULTILINE | wxSUNKEN_BORDER);
212
213 m_logOld = wxLog::SetActiveTarget(new wxLogTextCtrl(m_logWindow));
214
215 RecreateList(wxLC_REPORT | wxLC_SINGLE_SEL);
216
217 CreateStatusBar(3);
218 }
219
220 MyFrame::~MyFrame()
221 {
222 delete wxLog::SetActiveTarget(m_logOld);
223
224 delete m_imageListNormal;
225 delete m_imageListSmall;
226 }
227
228 void MyFrame::OnSize(wxSizeEvent& event)
229 {
230 if ( !m_logWindow )
231 return;
232
233 wxSize size = GetClientSize();
234 wxCoord y = (2*size.y)/3;
235 m_listCtrl->SetSize(0, 0, size.x, y);
236 m_logWindow->SetSize(0, y + 1, size.x, size.y - y);
237
238 event.Skip();
239 }
240
241 void MyFrame::OnQuit(wxCommandEvent& WXUNUSED(event))
242 {
243 Close(TRUE);
244 }
245
246 void MyFrame::OnAbout(wxCommandEvent& WXUNUSED(event))
247 {
248 wxMessageDialog dialog(this, "List test sample\nJulian Smart (c) 1997",
249 "About list test", wxOK|wxCANCEL);
250
251 dialog.ShowModal();
252 }
253
254 void MyFrame::OnToggleFirstSel(wxCommandEvent& WXUNUSED(event))
255 {
256 m_listCtrl->SetItemState(0, wxLIST_STATE_SELECTED, wxLIST_STATE_SELECTED);
257 }
258
259 void MyFrame::OnDeselectAll(wxCommandEvent& WXUNUSED(event))
260 {
261 int n = m_listCtrl->GetItemCount();
262 for (int i = 0; i < n; i++)
263 m_listCtrl->SetItemState(i,0,wxLIST_STATE_SELECTED);
264 }
265
266 void MyFrame::OnSelectAll(wxCommandEvent& WXUNUSED(event))
267 {
268 int n = m_listCtrl->GetItemCount();
269 for (int i = 0; i < n; i++)
270 m_listCtrl->SetItemState(i,wxLIST_STATE_SELECTED, wxLIST_STATE_SELECTED);
271 }
272
273 // ----------------------------------------------------------------------------
274 // changing listctrl modes
275 // ----------------------------------------------------------------------------
276
277 void MyFrame::RecreateList(long flags, bool withText)
278 {
279 // we could avoid recreating it if we don't set/clear the wxLC_VIRTUAL
280 // style, but it is more trouble to do it than not
281 #if 0
282 if ( !m_listCtrl || ((flags & wxLC_VIRTUAL) !=
283 (m_listCtrl->GetWindowStyleFlag() & wxLC_VIRTUAL)) )
284 #endif
285 {
286 delete m_listCtrl;
287
288 m_listCtrl = new MyListCtrl(this, LIST_CTRL,
289 wxDefaultPosition, wxDefaultSize,
290 flags |
291 wxSUNKEN_BORDER);
292
293 switch ( flags & wxLC_MASK_TYPE )
294 {
295 case wxLC_LIST:
296 InitWithListItems();
297 break;
298
299 case wxLC_ICON:
300 InitWithIconItems(withText);
301 break;
302
303 case wxLC_SMALL_ICON:
304 InitWithIconItems(withText, TRUE);
305 break;
306
307 case wxLC_REPORT:
308 if ( flags & wxLC_VIRTUAL )
309 InitWithVirtualItems();
310 else
311 InitWithReportItems();
312 break;
313
314 default:
315 wxFAIL_MSG( _T("unknown listctrl mode") );
316 }
317 }
318
319 #ifdef __WXMSW__
320 SendSizeEvent();
321 #endif
322
323 m_logWindow->Clear();
324 }
325
326 void MyFrame::OnListView(wxCommandEvent& WXUNUSED(event))
327 {
328 RecreateList(wxLC_LIST);
329 }
330
331 void MyFrame::InitWithListItems()
332 {
333 for ( int i = 0; i < NUM_ITEMS; i++ )
334 {
335 m_listCtrl->InsertItem(i, wxString::Format(_T("Item %d"), i));
336 }
337 }
338
339 void MyFrame::OnReportView(wxCommandEvent& WXUNUSED(event))
340 {
341 RecreateList(wxLC_REPORT);
342 }
343
344 void MyFrame::InitWithReportItems()
345 {
346 m_listCtrl->SetImageList(m_imageListSmall, wxIMAGE_LIST_SMALL);
347
348 m_listCtrl->InsertColumn(0, "Column 1"); // , wxLIST_FORMAT_LEFT, 140);
349 m_listCtrl->InsertColumn(1, "Column 2"); // , wxLIST_FORMAT_LEFT, 140);
350 m_listCtrl->InsertColumn(2, "One More Column (2)"); // , wxLIST_FORMAT_LEFT, 140);
351
352 // to speed up inserting we hide the control temporarily
353 m_listCtrl->Hide();
354
355 wxStopWatch sw;
356
357 for ( int i = 0; i < NUM_ITEMS; i++ )
358 {
359 m_listCtrl->InsertItemInReportView(i);
360 }
361
362 m_logWindow->WriteText(wxString::Format(_T("%d items inserted in %ldms\n"),
363 NUM_ITEMS, sw.Time()));
364 m_listCtrl->Show();
365
366 // we leave all mask fields to 0 and only change the colour
367 wxListItem item;
368 item.m_itemId = 0;
369 item.SetTextColour(*wxRED);
370 m_listCtrl->SetItem( item );
371
372 item.m_itemId = 2;
373 item.SetTextColour(*wxGREEN);
374 m_listCtrl->SetItem( item );
375 item.m_itemId = 4;
376 item.SetTextColour(*wxLIGHT_GREY);
377 item.SetFont(*wxITALIC_FONT);
378 item.SetBackgroundColour(*wxRED);
379 m_listCtrl->SetItem( item );
380
381 m_listCtrl->SetTextColour(*wxBLUE);
382 m_listCtrl->SetBackgroundColour(*wxLIGHT_GREY);
383
384 m_listCtrl->SetColumnWidth( 0, wxLIST_AUTOSIZE );
385 m_listCtrl->SetColumnWidth( 1, wxLIST_AUTOSIZE );
386 m_listCtrl->SetColumnWidth( 2, wxLIST_AUTOSIZE );
387 }
388
389 void MyFrame::InitWithIconItems(bool withText, bool sameIcon)
390 {
391 m_listCtrl->SetImageList(m_imageListNormal, wxIMAGE_LIST_NORMAL);
392 m_listCtrl->SetImageList(m_imageListSmall, wxIMAGE_LIST_SMALL);
393
394 for ( int i = 0; i < NUM_ICONS; i++ )
395 {
396 int image = sameIcon ? 0 : i;
397
398 if ( withText )
399 {
400 m_listCtrl->InsertItem(i, wxString::Format(_T("Label %d"), i),
401 image);
402 }
403 else
404 {
405 m_listCtrl->InsertItem(i, image);
406 }
407 }
408 }
409
410 void MyFrame::OnIconView(wxCommandEvent& WXUNUSED(event))
411 {
412 RecreateList(wxLC_ICON, FALSE);
413 }
414
415 void MyFrame::OnIconTextView(wxCommandEvent& WXUNUSED(event))
416 {
417 RecreateList(wxLC_ICON);
418 }
419
420 void MyFrame::OnSmallIconView(wxCommandEvent& WXUNUSED(event))
421 {
422 RecreateList(wxLC_SMALL_ICON, FALSE);
423 }
424
425 void MyFrame::OnSmallIconTextView(wxCommandEvent& WXUNUSED(event))
426 {
427 RecreateList(wxLC_SMALL_ICON);
428 }
429
430 void MyFrame::OnVirtualView(wxCommandEvent& WXUNUSED(event))
431 {
432 RecreateList(wxLC_REPORT | wxLC_VIRTUAL);
433 }
434
435 void MyFrame::InitWithVirtualItems()
436 {
437 m_listCtrl->InsertColumn(0, "First Column");
438 m_listCtrl->InsertColumn(1, "Second Column");
439 m_listCtrl->SetColumnWidth(0, 150);
440 m_listCtrl->SetColumnWidth(1, 150);
441
442 m_listCtrl->SetItemCount(1000000);
443 }
444
445 void MyFrame::OnSort(wxCommandEvent& WXUNUSED(event))
446 {
447 wxStopWatch sw;
448
449 m_listCtrl->SortItems(MyCompareFunction, 0);
450
451 m_logWindow->WriteText(wxString::Format(_T("Sorting %d items took %ld ms\n"),
452 m_listCtrl->GetItemCount(),
453 sw.Time()));
454 }
455
456 void MyFrame::OnShowSelInfo(wxCommandEvent& event)
457 {
458 int selCount = m_listCtrl->GetSelectedItemCount();
459 wxLogMessage(_T("%d items selected:"), selCount);
460
461 // don't show too many items
462 size_t shownCount = 0;
463
464 long item = m_listCtrl->GetNextItem(-1, wxLIST_NEXT_ALL,
465 wxLIST_STATE_SELECTED);
466 while ( item != -1 )
467 {
468 wxLogMessage(_T("\t%ld (%s)"),
469 item, m_listCtrl->GetItemText(item).c_str());
470
471 if ( ++shownCount > 10 )
472 {
473 wxLogMessage(_T("\t... more selected items snipped..."));
474 break;
475 }
476
477 item = m_listCtrl->GetNextItem(item, wxLIST_NEXT_ALL,
478 wxLIST_STATE_SELECTED);
479 }
480 }
481
482 void MyFrame::OnShowColInfo(wxCommandEvent& event)
483 {
484 int count = m_listCtrl->GetColumnCount();
485 wxLogMessage("%d columns:", count);
486 for ( int c = 0; c < count; c++ )
487 {
488 wxLogMessage("\tcolumn %d has width %d", c,
489 m_listCtrl->GetColumnWidth(c));
490 }
491 }
492
493 void MyFrame::OnUpdateShowColInfo(wxUpdateUIEvent& event)
494 {
495 event.Enable( (m_listCtrl->GetWindowStyleFlag() & wxLC_REPORT) != 0 );
496 }
497
498 void MyFrame::OnToggleMultiSel(wxCommandEvent& WXUNUSED(event))
499 {
500 long flags = m_listCtrl->GetWindowStyleFlag();
501 if ( flags & wxLC_SINGLE_SEL )
502 flags &= ~wxLC_SINGLE_SEL;
503 else
504 flags |= wxLC_SINGLE_SEL;
505
506 m_logWindow->WriteText(wxString::Format("Current selection mode: %sle\n",
507 (flags & wxLC_SINGLE_SEL) ? "sing" : "multip"));
508
509 RecreateList(flags);
510 }
511
512 void MyFrame::OnSetFgColour(wxCommandEvent& WXUNUSED(event))
513 {
514 m_listCtrl->SetForegroundColour(wxGetColourFromUser(this));
515 m_listCtrl->Refresh();
516 }
517
518 void MyFrame::OnSetBgColour(wxCommandEvent& WXUNUSED(event))
519 {
520 m_listCtrl->SetBackgroundColour(wxGetColourFromUser(this));
521 m_listCtrl->Refresh();
522 }
523
524 void MyFrame::OnAdd(wxCommandEvent& WXUNUSED(event))
525 {
526 m_listCtrl->InsertItem(m_listCtrl->GetItemCount(), _T("Appended item"));
527 }
528
529 void MyFrame::OnDelete(wxCommandEvent& WXUNUSED(event))
530 {
531 if ( m_listCtrl->GetItemCount() )
532 {
533 m_listCtrl->DeleteItem(0);
534 }
535 else
536 {
537 m_logWindow->WriteText("Nothing to delete");
538 }
539 }
540
541 void MyFrame::OnDeleteAll(wxCommandEvent& WXUNUSED(event))
542 {
543 wxStopWatch sw;
544
545 m_listCtrl->DeleteAllItems();
546
547 m_logWindow->WriteText(wxString::Format(_T("Deleting %d items took %ld ms\n"),
548 m_listCtrl->GetItemCount(),
549 sw.Time()));
550 }
551
552 // MyListCtrl
553
554 void MyListCtrl::OnColClick(wxListEvent& event)
555 {
556 wxLogMessage( "OnColumnClick at %d.", event.GetColumn() );
557 }
558
559 void MyListCtrl::OnBeginDrag(wxListEvent& event)
560 {
561 wxLogMessage( "OnBeginDrag at %d,%d.",
562 event.m_pointDrag.x, event.m_pointDrag.y );
563 }
564
565 void MyListCtrl::OnBeginRDrag(wxListEvent& event)
566 {
567 wxLogMessage( "OnBeginRDrag at %d,%d.",
568 event.m_pointDrag.x, event.m_pointDrag.y );
569 }
570
571 void MyListCtrl::OnBeginLabelEdit(wxListEvent& event)
572 {
573 wxLogMessage("OnBeginLabelEdit: %s", event.m_item.m_text.c_str());
574 }
575
576 void MyListCtrl::OnEndLabelEdit(wxListEvent& event)
577 {
578 wxLogMessage("OnEndLabelEdit: %s", event.m_item.m_text.c_str());
579 }
580
581 void MyListCtrl::OnDeleteItem(wxListEvent& event)
582 {
583 LogEvent(event, _T("OnDeleteItem"));
584 }
585
586 void MyListCtrl::OnDeleteAllItems(wxListEvent& event)
587 {
588 LogEvent(event, _T("OnDeleteAllItems"));
589 }
590
591 void MyListCtrl::OnGetInfo(wxListEvent& event)
592 {
593 wxString msg;
594
595 msg << "OnGetInfo (" << event.m_item.m_itemId << ", " << event.m_item.m_col << ")";
596 if ( event.m_item.m_mask & wxLIST_MASK_STATE )
597 msg << " wxLIST_MASK_STATE";
598 if ( event.m_item.m_mask & wxLIST_MASK_TEXT )
599 msg << " wxLIST_MASK_TEXT";
600 if ( event.m_item.m_mask & wxLIST_MASK_IMAGE )
601 msg << " wxLIST_MASK_IMAGE";
602 if ( event.m_item.m_mask & wxLIST_MASK_DATA )
603 msg << " wxLIST_MASK_DATA";
604 if ( event.m_item.m_mask & wxLIST_SET_ITEM )
605 msg << " wxLIST_SET_ITEM";
606 if ( event.m_item.m_mask & wxLIST_MASK_WIDTH )
607 msg << " wxLIST_MASK_WIDTH";
608 if ( event.m_item.m_mask & wxLIST_MASK_FORMAT )
609 msg << " wxLIST_MASK_WIDTH";
610
611 if ( event.m_item.m_mask & wxLIST_MASK_TEXT )
612 {
613 event.m_item.m_text = "My callback text";
614 }
615
616 wxLogMessage(msg);
617 }
618
619 void MyListCtrl::OnSetInfo(wxListEvent& event)
620 {
621 LogEvent(event, _T("OnSetInfo"));
622 }
623
624 void MyListCtrl::OnSelected(wxListEvent& event)
625 {
626 LogEvent(event, _T("OnSelected"));
627
628 if ( GetWindowStyle() & wxLC_REPORT )
629 {
630 wxListItem info;
631 info.m_itemId = event.m_itemIndex;
632 info.m_col = 1;
633 info.m_mask = wxLIST_MASK_TEXT;
634 if ( GetItem(info) )
635 {
636 wxLogMessage("Value of the 2nd field of the selected item: %s",
637 info.m_text.c_str());
638 }
639 else
640 {
641 wxFAIL_MSG("wxListCtrl::GetItem() failed");
642 }
643 }
644 }
645
646 void MyListCtrl::OnDeselected(wxListEvent& event)
647 {
648 LogEvent(event, _T("OnDeselected"));
649 }
650
651 void MyListCtrl::OnActivated(wxListEvent& event)
652 {
653 LogEvent(event, _T("OnActivated"));
654 }
655
656 void MyListCtrl::OnListKeyDown(wxListEvent& event)
657 {
658 switch ( event.GetCode() )
659 {
660 case WXK_DELETE:
661 DeleteItem(event.GetIndex());
662
663 wxLogMessage(_T("Item %d deleted"), event.GetIndex());
664 break;
665
666 case WXK_INSERT:
667 if ( GetWindowStyle() & wxLC_REPORT )
668 {
669 if ( GetWindowStyle() & wxLC_VIRTUAL )
670 {
671 SetItemCount(GetItemCount() + 1);
672 }
673 else // !virtual
674 {
675 InsertItemInReportView(event.GetIndex());
676 }
677 }
678 //else: fall through
679
680 default:
681 LogEvent(event, _T("OnListKeyDown"));
682
683 event.Skip();
684 }
685 }
686
687 void MyListCtrl::OnChar(wxKeyEvent& event)
688 {
689 wxLogMessage(_T("Got char event."));
690
691 event.Skip();
692 }
693
694 void MyListCtrl::LogEvent(const wxListEvent& event, const wxChar *eventName)
695 {
696 wxLogMessage(_T("Item %ld: %s (item text = %s, data = %ld)"),
697 event.GetIndex(), eventName,
698 event.GetText().c_str(), event.GetData());
699 }
700
701 wxString MyListCtrl::OnGetItemText(long item, long column) const
702 {
703 return wxString::Format(_T("Column %ld of item %ld"), column, item);
704 }
705
706 int MyListCtrl::OnGetItemImage(long item) const
707 {
708 return 0;
709 }
710
711 void MyListCtrl::InsertItemInReportView(int i)
712 {
713 wxString buf;
714 buf.Printf(_T("This is item %d"), i);
715 long tmp = InsertItem(i, buf, 0);
716 SetItemData(tmp, i);
717
718 buf.Printf(_T("Col 1, item %d"), i);
719 SetItem(i, 1, buf);
720
721 buf.Printf(_T("Item %d in column 2"), i);
722 SetItem(i, 2, buf);
723 }
724