1. fixed typo ('&' instead of '|') in wxNotebook
[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 wxString buf;
358 for ( int i = 0; i < NUM_ITEMS; i++ )
359 {
360 buf.Printf(_T("This is item %d"), i);
361 long tmp = m_listCtrl->InsertItem(i, buf, 0);
362 m_listCtrl->SetItemData(tmp, i);
363
364 buf.Printf(_T("Col 1, item %d"), i);
365 tmp = m_listCtrl->SetItem(i, 1, buf);
366
367 buf.Printf(_T("Item %d in column 2"), i);
368 tmp = m_listCtrl->SetItem(i, 2, buf);
369 }
370
371 m_logWindow->WriteText(wxString::Format(_T("%d items inserted in %ldms\n"),
372 NUM_ITEMS, sw.Time()));
373 m_listCtrl->Show();
374
375 // we leave all mask fields to 0 and only change the colour
376 wxListItem item;
377 item.m_itemId = 0;
378 item.SetTextColour(*wxRED);
379 m_listCtrl->SetItem( item );
380
381 item.m_itemId = 2;
382 item.SetTextColour(*wxGREEN);
383 m_listCtrl->SetItem( item );
384 item.m_itemId = 4;
385 item.SetTextColour(*wxLIGHT_GREY);
386 item.SetFont(*wxITALIC_FONT);
387 item.SetBackgroundColour(*wxRED);
388 m_listCtrl->SetItem( item );
389
390 m_listCtrl->SetTextColour(*wxBLUE);
391 m_listCtrl->SetBackgroundColour(*wxLIGHT_GREY);
392
393 m_listCtrl->SetColumnWidth( 0, wxLIST_AUTOSIZE );
394 m_listCtrl->SetColumnWidth( 1, wxLIST_AUTOSIZE );
395 m_listCtrl->SetColumnWidth( 2, wxLIST_AUTOSIZE );
396 }
397
398 void MyFrame::InitWithIconItems(bool withText, bool sameIcon)
399 {
400 m_listCtrl->SetImageList(m_imageListNormal, wxIMAGE_LIST_NORMAL);
401 m_listCtrl->SetImageList(m_imageListSmall, wxIMAGE_LIST_SMALL);
402
403 for ( int i = 0; i < NUM_ICONS; i++ )
404 {
405 int image = sameIcon ? 0 : i;
406
407 if ( withText )
408 {
409 m_listCtrl->InsertItem(i, wxString::Format(_T("Label %d"), i),
410 image);
411 }
412 else
413 {
414 m_listCtrl->InsertItem(i, image);
415 }
416 }
417 }
418
419 void MyFrame::OnIconView(wxCommandEvent& WXUNUSED(event))
420 {
421 RecreateList(wxLC_ICON, FALSE);
422 }
423
424 void MyFrame::OnIconTextView(wxCommandEvent& WXUNUSED(event))
425 {
426 RecreateList(wxLC_ICON);
427 }
428
429 void MyFrame::OnSmallIconView(wxCommandEvent& WXUNUSED(event))
430 {
431 RecreateList(wxLC_SMALL_ICON, FALSE);
432 }
433
434 void MyFrame::OnSmallIconTextView(wxCommandEvent& WXUNUSED(event))
435 {
436 RecreateList(wxLC_SMALL_ICON);
437 }
438
439 void MyFrame::OnVirtualView(wxCommandEvent& WXUNUSED(event))
440 {
441 RecreateList(wxLC_REPORT | wxLC_VIRTUAL);
442 }
443
444 void MyFrame::InitWithVirtualItems()
445 {
446 m_listCtrl->InsertColumn(0, "First Column");
447 m_listCtrl->InsertColumn(1, "Second Column");
448 m_listCtrl->SetColumnWidth(0, 150);
449 m_listCtrl->SetColumnWidth(1, 150);
450
451 m_listCtrl->SetItemCount(1000000);
452 }
453
454 void MyFrame::OnSort(wxCommandEvent& WXUNUSED(event))
455 {
456 wxStopWatch sw;
457
458 m_listCtrl->SortItems(MyCompareFunction, 0);
459
460 m_logWindow->WriteText(wxString::Format(_T("Sorting %d items took %ld ms\n"),
461 m_listCtrl->GetItemCount(),
462 sw.Time()));
463 }
464
465 void MyFrame::OnShowSelInfo(wxCommandEvent& event)
466 {
467 int selCount = m_listCtrl->GetSelectedItemCount();
468 wxLogMessage(_T("%d items selected:"), selCount);
469
470 // don't show too many items
471 size_t shownCount = 0;
472
473 long item = m_listCtrl->GetNextItem(-1, wxLIST_NEXT_ALL,
474 wxLIST_STATE_SELECTED);
475 while ( item != -1 )
476 {
477 wxLogMessage(_T("\t%ld (%s)"),
478 item, m_listCtrl->GetItemText(item).c_str());
479
480 if ( ++shownCount > 10 )
481 {
482 wxLogMessage(_T("\t... more selected items snipped..."));
483 break;
484 }
485
486 item = m_listCtrl->GetNextItem(item, wxLIST_NEXT_ALL,
487 wxLIST_STATE_SELECTED);
488 }
489 }
490
491 void MyFrame::OnShowColInfo(wxCommandEvent& event)
492 {
493 int count = m_listCtrl->GetColumnCount();
494 wxLogMessage("%d columns:", count);
495 for ( int c = 0; c < count; c++ )
496 {
497 wxLogMessage("\tcolumn %d has width %d", c,
498 m_listCtrl->GetColumnWidth(c));
499 }
500 }
501
502 void MyFrame::OnUpdateShowColInfo(wxUpdateUIEvent& event)
503 {
504 event.Enable( (m_listCtrl->GetWindowStyleFlag() & wxLC_REPORT) != 0 );
505 }
506
507 void MyFrame::OnToggleMultiSel(wxCommandEvent& WXUNUSED(event))
508 {
509 long flags = m_listCtrl->GetWindowStyleFlag();
510 if ( flags & wxLC_SINGLE_SEL )
511 flags &= ~wxLC_SINGLE_SEL;
512 else
513 flags |= wxLC_SINGLE_SEL;
514
515 m_logWindow->WriteText(wxString::Format("Current selection mode: %sle\n",
516 (flags & wxLC_SINGLE_SEL) ? "sing" : "multip"));
517
518 RecreateList(flags);
519 }
520
521 void MyFrame::OnSetFgColour(wxCommandEvent& WXUNUSED(event))
522 {
523 m_listCtrl->SetForegroundColour(wxGetColourFromUser(this));
524 m_listCtrl->Refresh();
525 }
526
527 void MyFrame::OnSetBgColour(wxCommandEvent& WXUNUSED(event))
528 {
529 m_listCtrl->SetBackgroundColour(wxGetColourFromUser(this));
530 m_listCtrl->Refresh();
531 }
532
533 void MyFrame::OnAdd(wxCommandEvent& WXUNUSED(event))
534 {
535 m_listCtrl->InsertItem(m_listCtrl->GetItemCount(), _T("Appended item"));
536 }
537
538 void MyFrame::OnDelete(wxCommandEvent& WXUNUSED(event))
539 {
540 if ( m_listCtrl->GetItemCount() )
541 {
542 m_listCtrl->DeleteItem(0);
543 }
544 else
545 {
546 m_logWindow->WriteText("Nothing to delete");
547 }
548 }
549
550 void MyFrame::OnDeleteAll(wxCommandEvent& WXUNUSED(event))
551 {
552 wxStopWatch sw;
553
554 m_listCtrl->DeleteAllItems();
555
556 m_logWindow->WriteText(wxString::Format(_T("Deleting %d items took %ld ms\n"),
557 m_listCtrl->GetItemCount(),
558 sw.Time()));
559 }
560
561 // MyListCtrl
562
563 void MyListCtrl::OnColClick(wxListEvent& event)
564 {
565 wxLogMessage( "OnColumnClick at %d.", event.GetColumn() );
566 }
567
568 void MyListCtrl::OnBeginDrag(wxListEvent& event)
569 {
570 wxLogMessage( "OnBeginDrag at %d,%d.",
571 event.m_pointDrag.x, event.m_pointDrag.y );
572 }
573
574 void MyListCtrl::OnBeginRDrag(wxListEvent& event)
575 {
576 wxLogMessage( "OnBeginRDrag at %d,%d.",
577 event.m_pointDrag.x, event.m_pointDrag.y );
578 }
579
580 void MyListCtrl::OnBeginLabelEdit(wxListEvent& event)
581 {
582 wxLogMessage("OnBeginLabelEdit: %s", event.m_item.m_text.c_str());
583 }
584
585 void MyListCtrl::OnEndLabelEdit(wxListEvent& event)
586 {
587 wxLogMessage("OnEndLabelEdit: %s", event.m_item.m_text.c_str());
588 }
589
590 void MyListCtrl::OnDeleteItem(wxListEvent& event)
591 {
592 LogEvent(event, _T("OnDeleteItem"));
593 }
594
595 void MyListCtrl::OnDeleteAllItems(wxListEvent& event)
596 {
597 LogEvent(event, _T("OnDeleteAllItems"));
598 }
599
600 void MyListCtrl::OnGetInfo(wxListEvent& event)
601 {
602 wxString msg;
603
604 msg << "OnGetInfo (" << event.m_item.m_itemId << ", " << event.m_item.m_col << ")";
605 if ( event.m_item.m_mask & wxLIST_MASK_STATE )
606 msg << " wxLIST_MASK_STATE";
607 if ( event.m_item.m_mask & wxLIST_MASK_TEXT )
608 msg << " wxLIST_MASK_TEXT";
609 if ( event.m_item.m_mask & wxLIST_MASK_IMAGE )
610 msg << " wxLIST_MASK_IMAGE";
611 if ( event.m_item.m_mask & wxLIST_MASK_DATA )
612 msg << " wxLIST_MASK_DATA";
613 if ( event.m_item.m_mask & wxLIST_SET_ITEM )
614 msg << " wxLIST_SET_ITEM";
615 if ( event.m_item.m_mask & wxLIST_MASK_WIDTH )
616 msg << " wxLIST_MASK_WIDTH";
617 if ( event.m_item.m_mask & wxLIST_MASK_FORMAT )
618 msg << " wxLIST_MASK_WIDTH";
619
620 if ( event.m_item.m_mask & wxLIST_MASK_TEXT )
621 {
622 event.m_item.m_text = "My callback text";
623 }
624
625 wxLogMessage(msg);
626 }
627
628 void MyListCtrl::OnSetInfo(wxListEvent& event)
629 {
630 LogEvent(event, _T("OnSetInfo"));
631 }
632
633 void MyListCtrl::OnSelected(wxListEvent& event)
634 {
635 LogEvent(event, _T("OnSelected"));
636
637 if ( GetWindowStyle() & wxLC_REPORT )
638 {
639 wxListItem info;
640 info.m_itemId = event.m_itemIndex;
641 info.m_col = 1;
642 info.m_mask = wxLIST_MASK_TEXT;
643 if ( GetItem(info) )
644 {
645 wxLogMessage("Value of the 2nd field of the selected item: %s",
646 info.m_text.c_str());
647 }
648 else
649 {
650 wxFAIL_MSG("wxListCtrl::GetItem() failed");
651 }
652 }
653 }
654
655 void MyListCtrl::OnDeselected(wxListEvent& event)
656 {
657 LogEvent(event, _T("OnDeselected"));
658 }
659
660 void MyListCtrl::OnActivated(wxListEvent& event)
661 {
662 LogEvent(event, _T("OnActivated"));
663 }
664
665 void MyListCtrl::OnListKeyDown(wxListEvent& event)
666 {
667 LogEvent(event, _T("OnListKeyDown"));
668
669 event.Skip();
670 }
671
672 void MyListCtrl::OnChar(wxKeyEvent& event)
673 {
674 wxLogMessage(_T("Got char event."));
675
676 event.Skip();
677 }
678
679 void MyListCtrl::LogEvent(const wxListEvent& event, const wxChar *eventName)
680 {
681 wxLogMessage(_T("Item %ld: %s (item text = %s, data = %ld)"),
682 event.GetIndex(), eventName,
683 event.GetText().c_str(), event.GetData());
684 }
685
686 wxString MyListCtrl::OnGetItemText(long item, long column) const
687 {
688 return wxString::Format(_T("Column %ld of item %ld"), column, item);
689 }
690
691 int MyListCtrl::OnGetItemImage(long item) const
692 {
693 return 0;
694 }
695