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