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