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