1 /////////////////////////////////////////////////////////////////////////////
3 // Purpose: wxListCtrl sample
4 // Author: Julian Smart
8 // Copyright: (c) Julian Smart and Markus Holzem
9 // Licence: wxWindows license
10 /////////////////////////////////////////////////////////////////////////////
13 #pragma implementation
17 // For compilers that support precompilation, includes "wx/wx.h".
18 #include "wx/wxprec.h"
29 #include "mondrian.xpm"
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"
43 #include "wx/imaglist.h"
44 #include "wx/listctrl.h"
45 #include "wx/timer.h" // for wxStopWatch
46 #include "wx/colordlg.h" // for wxGetColourFromUser
50 BEGIN_EVENT_TABLE(MyFrame
, wxFrame
)
51 EVT_SIZE(MyFrame::OnSize
)
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
)
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
)
76 EVT_UPDATE_UI(LIST_SHOW_COL_INFO
, MyFrame::OnUpdateShowColInfo
)
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 EVT_LIST_CACHE_HINT(LIST_CTRL
, MyListCtrl::OnCacheHint
)
95 EVT_CHAR(MyListCtrl::OnChar
)
100 // number of items in list/report view
101 static const int NUM_ITEMS
= 30;
103 // number of items in icon/small icon view
104 static const int NUM_ICONS
= 9;
106 int wxCALLBACK
MyCompareFunction(long item1
, long item2
, long sortData
)
109 return item1
< item2
;
112 // `Main program' equivalent, creating windows and returning main app frame
115 // Create the main frame window
116 MyFrame
*frame
= new MyFrame(wxT("wxListCtrl Test"), 50, 50, 450, 340);
126 // My frame constructor
127 MyFrame::MyFrame(const wxChar
*title
, int x
, int y
, int w
, int h
)
128 : wxFrame((wxFrame
*)NULL
, -1, title
, wxPoint(x
, y
), wxSize(w
, h
))
130 m_listCtrl
= (MyListCtrl
*) NULL
;
131 m_logWindow
= (wxTextCtrl
*) NULL
;
134 SetIcon( wxICON(mondrian
) );
136 // Make an image list containing large icons
137 m_imageListNormal
= new wxImageList(32, 32, TRUE
);
138 m_imageListSmall
= new wxImageList(16, 16, TRUE
);
141 m_imageListNormal
->Add( wxIcon("icon1", wxBITMAP_TYPE_ICO_RESOURCE
) );
142 m_imageListNormal
->Add( wxIcon("icon2", wxBITMAP_TYPE_ICO_RESOURCE
) );
143 m_imageListNormal
->Add( wxIcon("icon3", wxBITMAP_TYPE_ICO_RESOURCE
) );
144 m_imageListNormal
->Add( wxIcon("icon4", wxBITMAP_TYPE_ICO_RESOURCE
) );
145 m_imageListNormal
->Add( wxIcon("icon5", wxBITMAP_TYPE_ICO_RESOURCE
) );
146 m_imageListNormal
->Add( wxIcon("icon6", wxBITMAP_TYPE_ICO_RESOURCE
) );
147 m_imageListNormal
->Add( wxIcon("icon7", wxBITMAP_TYPE_ICO_RESOURCE
) );
148 m_imageListNormal
->Add( wxIcon("icon8", wxBITMAP_TYPE_ICO_RESOURCE
) );
149 m_imageListNormal
->Add( wxIcon("icon9", wxBITMAP_TYPE_ICO_RESOURCE
) );
151 m_imageListSmall
->Add( wxIcon("iconsmall", wxBITMAP_TYPE_ICO_RESOURCE
) );
154 m_imageListNormal
->Add( wxIcon( toolbrai_xpm
) );
155 m_imageListNormal
->Add( wxIcon( toolchar_xpm
) );
156 m_imageListNormal
->Add( wxIcon( tooldata_xpm
) );
157 m_imageListNormal
->Add( wxIcon( toolnote_xpm
) );
158 m_imageListNormal
->Add( wxIcon( tooltodo_xpm
) );
159 m_imageListNormal
->Add( wxIcon( toolchec_xpm
) );
160 m_imageListNormal
->Add( wxIcon( toolgame_xpm
) );
161 m_imageListNormal
->Add( wxIcon( tooltime_xpm
) );
162 m_imageListNormal
->Add( wxIcon( toolword_xpm
) );
164 m_imageListSmall
->Add( wxIcon( small1_xpm
) );
168 wxMenu
*menuFile
= new wxMenu
;
169 menuFile
->Append(LIST_ABOUT
, _T("&About"));
170 menuFile
->AppendSeparator();
171 menuFile
->Append(LIST_QUIT
, _T("E&xit\tAlt-X"));
173 wxMenu
*menuView
= new wxMenu
;
174 menuView
->Append(LIST_LIST_VIEW
, _T("&List view\tF1"));
175 menuView
->Append(LIST_REPORT_VIEW
, _T("&Report view\tF2"));
176 menuView
->Append(LIST_ICON_VIEW
, _T("&Icon view\tF3"));
177 menuView
->Append(LIST_ICON_TEXT_VIEW
, _T("Icon view with &text\tF4"));
178 menuView
->Append(LIST_SMALL_ICON_VIEW
, _T("&Small icon view\tF5"));
179 menuView
->Append(LIST_SMALL_ICON_TEXT_VIEW
, _T("Small icon &view with text\tF6"));
180 menuView
->Append(LIST_VIRTUAL_VIEW
, _T("Virtual view\tF7"));
182 wxMenu
*menuList
= new wxMenu
;
183 menuList
->Append(LIST_TOGGLE_FIRST
, _T("&Toggle first item\tCtrl-T"));
184 menuList
->Append(LIST_DESELECT_ALL
, _T("&Deselect All\tCtrl-D"));
185 menuList
->Append(LIST_SELECT_ALL
, _T("S&elect All\tCtrl-A"));
186 menuList
->AppendSeparator();
187 menuList
->Append(LIST_SHOW_COL_INFO
, _T("Show &column info\tCtrl-C"));
188 menuList
->Append(LIST_SHOW_SEL_INFO
, _T("Show &selected items\tCtrl-S"));
189 menuList
->AppendSeparator();
190 menuList
->Append(LIST_SORT
, _T("&Sort\tCtrl-S"));
191 menuList
->AppendSeparator();
192 menuList
->Append(LIST_ADD
, _T("&Append an item\tCtrl-P"));
193 menuList
->Append(LIST_DELETE
, _T("&Delete first item\tCtrl-X"));
194 menuList
->Append(LIST_DELETE_ALL
, _T("Delete &all items"));
195 menuList
->AppendSeparator();
196 menuList
->Append(LIST_TOGGLE_MULTI_SEL
, _T("&Multiple selection\tCtrl-M"),
197 _T("Toggle multiple selection"), TRUE
);
199 wxMenu
*menuCol
= new wxMenu
;
200 menuCol
->Append(LIST_SET_FG_COL
, _T("&Foreground colour..."));
201 menuCol
->Append(LIST_SET_BG_COL
, _T("&Background colour..."));
203 wxMenuBar
*menubar
= new wxMenuBar
;
204 menubar
->Append(menuFile
, _T("&File"));
205 menubar
->Append(menuView
, _T("&View"));
206 menubar
->Append(menuList
, _T("&List"));
207 menubar
->Append(menuCol
, _T("&Colour"));
210 m_logWindow
= new wxTextCtrl(this, -1, wxEmptyString
,
211 wxDefaultPosition
, wxDefaultSize
,
212 wxTE_MULTILINE
| wxSUNKEN_BORDER
);
214 m_logOld
= wxLog::SetActiveTarget(new wxLogTextCtrl(m_logWindow
));
216 RecreateList(wxLC_REPORT
| wxLC_SINGLE_SEL
);
223 delete wxLog::SetActiveTarget(m_logOld
);
225 delete m_imageListNormal
;
226 delete m_imageListSmall
;
229 void MyFrame::OnSize(wxSizeEvent
& event
)
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
);
242 void MyFrame::OnQuit(wxCommandEvent
& WXUNUSED(event
))
247 void MyFrame::OnAbout(wxCommandEvent
& WXUNUSED(event
))
249 wxMessageDialog
dialog(this, "List test sample\nJulian Smart (c) 1997",
250 "About list test", wxOK
|wxCANCEL
);
255 void MyFrame::OnToggleFirstSel(wxCommandEvent
& WXUNUSED(event
))
257 m_listCtrl
->SetItemState(0, wxLIST_STATE_SELECTED
, wxLIST_STATE_SELECTED
);
260 void MyFrame::OnDeselectAll(wxCommandEvent
& WXUNUSED(event
))
262 int n
= m_listCtrl
->GetItemCount();
263 for (int i
= 0; i
< n
; i
++)
264 m_listCtrl
->SetItemState(i
,0,wxLIST_STATE_SELECTED
);
267 void MyFrame::OnSelectAll(wxCommandEvent
& WXUNUSED(event
))
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
);
274 // ----------------------------------------------------------------------------
275 // changing listctrl modes
276 // ----------------------------------------------------------------------------
278 void MyFrame::RecreateList(long flags
, bool withText
)
280 // we could avoid recreating it if we don't set/clear the wxLC_VIRTUAL
281 // style, but it is more trouble to do it than not
283 if ( !m_listCtrl
|| ((flags
& wxLC_VIRTUAL
) !=
284 (m_listCtrl
->GetWindowStyleFlag() & wxLC_VIRTUAL
)) )
289 m_listCtrl
= new MyListCtrl(this, LIST_CTRL
,
290 wxDefaultPosition
, wxDefaultSize
,
294 switch ( flags
& wxLC_MASK_TYPE
)
301 InitWithIconItems(withText
);
304 case wxLC_SMALL_ICON
:
305 InitWithIconItems(withText
, TRUE
);
309 if ( flags
& wxLC_VIRTUAL
)
310 InitWithVirtualItems();
312 InitWithReportItems();
316 wxFAIL_MSG( _T("unknown listctrl mode") );
324 m_logWindow
->Clear();
327 void MyFrame::OnListView(wxCommandEvent
& WXUNUSED(event
))
329 RecreateList(wxLC_LIST
);
332 void MyFrame::InitWithListItems()
334 for ( int i
= 0; i
< NUM_ITEMS
; i
++ )
336 m_listCtrl
->InsertItem(i
, wxString::Format(_T("Item %d"), i
));
340 void MyFrame::OnReportView(wxCommandEvent
& WXUNUSED(event
))
342 RecreateList(wxLC_REPORT
);
345 void MyFrame::InitWithReportItems()
347 m_listCtrl
->SetImageList(m_imageListSmall
, wxIMAGE_LIST_SMALL
);
349 m_listCtrl
->InsertColumn(0, "Column 1"); // , wxLIST_FORMAT_LEFT, 140);
350 m_listCtrl
->InsertColumn(1, "Column 2"); // , wxLIST_FORMAT_LEFT, 140);
351 m_listCtrl
->InsertColumn(2, "One More Column (2)"); // , wxLIST_FORMAT_LEFT, 140);
353 // to speed up inserting we hide the control temporarily
358 for ( int i
= 0; i
< NUM_ITEMS
; i
++ )
360 m_listCtrl
->InsertItemInReportView(i
);
363 m_logWindow
->WriteText(wxString::Format(_T("%d items inserted in %ldms\n"),
364 NUM_ITEMS
, sw
.Time()));
367 // we leave all mask fields to 0 and only change the colour
370 item
.SetTextColour(*wxRED
);
371 m_listCtrl
->SetItem( item
);
374 item
.SetTextColour(*wxGREEN
);
375 m_listCtrl
->SetItem( item
);
377 item
.SetTextColour(*wxLIGHT_GREY
);
378 item
.SetFont(*wxITALIC_FONT
);
379 item
.SetBackgroundColour(*wxRED
);
380 m_listCtrl
->SetItem( item
);
382 m_listCtrl
->SetTextColour(*wxBLUE
);
383 m_listCtrl
->SetBackgroundColour(*wxLIGHT_GREY
);
385 m_listCtrl
->SetColumnWidth( 0, wxLIST_AUTOSIZE
);
386 m_listCtrl
->SetColumnWidth( 1, wxLIST_AUTOSIZE
);
387 m_listCtrl
->SetColumnWidth( 2, wxLIST_AUTOSIZE
);
390 void MyFrame::InitWithIconItems(bool withText
, bool sameIcon
)
392 m_listCtrl
->SetImageList(m_imageListNormal
, wxIMAGE_LIST_NORMAL
);
393 m_listCtrl
->SetImageList(m_imageListSmall
, wxIMAGE_LIST_SMALL
);
395 for ( int i
= 0; i
< NUM_ICONS
; i
++ )
397 int image
= sameIcon
? 0 : i
;
401 m_listCtrl
->InsertItem(i
, wxString::Format(_T("Label %d"), i
),
406 m_listCtrl
->InsertItem(i
, image
);
411 void MyFrame::OnIconView(wxCommandEvent
& WXUNUSED(event
))
413 RecreateList(wxLC_ICON
, FALSE
);
416 void MyFrame::OnIconTextView(wxCommandEvent
& WXUNUSED(event
))
418 RecreateList(wxLC_ICON
);
421 void MyFrame::OnSmallIconView(wxCommandEvent
& WXUNUSED(event
))
423 RecreateList(wxLC_SMALL_ICON
, FALSE
);
426 void MyFrame::OnSmallIconTextView(wxCommandEvent
& WXUNUSED(event
))
428 RecreateList(wxLC_SMALL_ICON
);
431 void MyFrame::OnVirtualView(wxCommandEvent
& WXUNUSED(event
))
433 RecreateList(wxLC_REPORT
| wxLC_VIRTUAL
);
436 void MyFrame::InitWithVirtualItems()
438 m_listCtrl
->InsertColumn(0, "First Column");
439 m_listCtrl
->InsertColumn(1, "Second Column");
440 m_listCtrl
->SetColumnWidth(0, 150);
441 m_listCtrl
->SetColumnWidth(1, 150);
443 m_listCtrl
->SetItemCount(1000000);
446 void MyFrame::OnSort(wxCommandEvent
& WXUNUSED(event
))
450 m_listCtrl
->SortItems(MyCompareFunction
, 0);
452 m_logWindow
->WriteText(wxString::Format(_T("Sorting %d items took %ld ms\n"),
453 m_listCtrl
->GetItemCount(),
457 void MyFrame::OnShowSelInfo(wxCommandEvent
& event
)
459 int selCount
= m_listCtrl
->GetSelectedItemCount();
460 wxLogMessage(_T("%d items selected:"), selCount
);
462 // don't show too many items
463 size_t shownCount
= 0;
465 long item
= m_listCtrl
->GetNextItem(-1, wxLIST_NEXT_ALL
,
466 wxLIST_STATE_SELECTED
);
469 wxLogMessage(_T("\t%ld (%s)"),
470 item
, m_listCtrl
->GetItemText(item
).c_str());
472 if ( ++shownCount
> 10 )
474 wxLogMessage(_T("\t... more selected items snipped..."));
478 item
= m_listCtrl
->GetNextItem(item
, wxLIST_NEXT_ALL
,
479 wxLIST_STATE_SELECTED
);
483 void MyFrame::OnShowColInfo(wxCommandEvent
& event
)
485 int count
= m_listCtrl
->GetColumnCount();
486 wxLogMessage(wxT("%d columns:"), count
);
487 for ( int c
= 0; c
< count
; c
++ )
489 wxLogMessage(wxT("\tcolumn %d has width %d"), c
,
490 m_listCtrl
->GetColumnWidth(c
));
494 void MyFrame::OnUpdateShowColInfo(wxUpdateUIEvent
& event
)
496 event
.Enable( (m_listCtrl
->GetWindowStyleFlag() & wxLC_REPORT
) != 0 );
499 void MyFrame::OnToggleMultiSel(wxCommandEvent
& WXUNUSED(event
))
501 long flags
= m_listCtrl
->GetWindowStyleFlag();
502 if ( flags
& wxLC_SINGLE_SEL
)
503 flags
&= ~wxLC_SINGLE_SEL
;
505 flags
|= wxLC_SINGLE_SEL
;
507 m_logWindow
->WriteText(wxString::Format(wxT("Current selection mode: %sle\n"),
508 (flags
& wxLC_SINGLE_SEL
) ? "sing" : "multip"));
513 void MyFrame::OnSetFgColour(wxCommandEvent
& WXUNUSED(event
))
515 m_listCtrl
->SetForegroundColour(wxGetColourFromUser(this));
516 m_listCtrl
->Refresh();
519 void MyFrame::OnSetBgColour(wxCommandEvent
& WXUNUSED(event
))
521 m_listCtrl
->SetBackgroundColour(wxGetColourFromUser(this));
522 m_listCtrl
->Refresh();
525 void MyFrame::OnAdd(wxCommandEvent
& WXUNUSED(event
))
527 m_listCtrl
->InsertItem(m_listCtrl
->GetItemCount(), _T("Appended item"));
530 void MyFrame::OnDelete(wxCommandEvent
& WXUNUSED(event
))
532 if ( m_listCtrl
->GetItemCount() )
534 m_listCtrl
->DeleteItem(0);
538 m_logWindow
->WriteText("Nothing to delete");
542 void MyFrame::OnDeleteAll(wxCommandEvent
& WXUNUSED(event
))
546 m_listCtrl
->DeleteAllItems();
548 m_logWindow
->WriteText(wxString::Format(_T("Deleting %d items took %ld ms\n"),
549 m_listCtrl
->GetItemCount(),
555 void MyListCtrl::OnCacheHint(wxListEvent
& event
)
557 wxLogMessage( wxT("OnCacheHint: cache items %ld..%ld"),
558 event
.GetCacheFrom(), event
.GetCacheTo() );
561 void MyListCtrl::OnColClick(wxListEvent
& event
)
563 wxLogMessage( wxT("OnColumnClick at %d."), event
.GetColumn() );
566 void MyListCtrl::OnBeginDrag(wxListEvent
& event
)
568 wxLogMessage( wxT("OnBeginDrag at %d,%d."),
569 event
.m_pointDrag
.x
, event
.m_pointDrag
.y
);
572 void MyListCtrl::OnBeginRDrag(wxListEvent
& event
)
574 wxLogMessage( wxT("OnBeginRDrag at %d,%d."),
575 event
.m_pointDrag
.x
, event
.m_pointDrag
.y
);
578 void MyListCtrl::OnBeginLabelEdit(wxListEvent
& event
)
580 wxLogMessage( wxT("OnBeginLabelEdit: %s"), event
.m_item
.m_text
.c_str());
583 void MyListCtrl::OnEndLabelEdit(wxListEvent
& event
)
585 wxLogMessage( wxT("OnEndLabelEdit: %s"), event
.m_item
.m_text
.c_str());
588 void MyListCtrl::OnDeleteItem(wxListEvent
& event
)
590 LogEvent(event
, _T("OnDeleteItem"));
593 void MyListCtrl::OnDeleteAllItems(wxListEvent
& event
)
595 LogEvent(event
, _T("OnDeleteAllItems"));
598 void MyListCtrl::OnGetInfo(wxListEvent
& event
)
602 msg
<< "OnGetInfo (" << event
.m_item
.m_itemId
<< ", " << event
.m_item
.m_col
<< ")";
603 if ( event
.m_item
.m_mask
& wxLIST_MASK_STATE
)
604 msg
<< " wxLIST_MASK_STATE";
605 if ( event
.m_item
.m_mask
& wxLIST_MASK_TEXT
)
606 msg
<< " wxLIST_MASK_TEXT";
607 if ( event
.m_item
.m_mask
& wxLIST_MASK_IMAGE
)
608 msg
<< " wxLIST_MASK_IMAGE";
609 if ( event
.m_item
.m_mask
& wxLIST_MASK_DATA
)
610 msg
<< " wxLIST_MASK_DATA";
611 if ( event
.m_item
.m_mask
& wxLIST_SET_ITEM
)
612 msg
<< " wxLIST_SET_ITEM";
613 if ( event
.m_item
.m_mask
& wxLIST_MASK_WIDTH
)
614 msg
<< " wxLIST_MASK_WIDTH";
615 if ( event
.m_item
.m_mask
& wxLIST_MASK_FORMAT
)
616 msg
<< " wxLIST_MASK_WIDTH";
618 if ( event
.m_item
.m_mask
& wxLIST_MASK_TEXT
)
620 event
.m_item
.m_text
= "My callback text";
626 void MyListCtrl::OnSetInfo(wxListEvent
& event
)
628 LogEvent(event
, _T("OnSetInfo"));
631 void MyListCtrl::OnSelected(wxListEvent
& event
)
633 LogEvent(event
, _T("OnSelected"));
635 if ( GetWindowStyle() & wxLC_REPORT
)
638 info
.m_itemId
= event
.m_itemIndex
;
640 info
.m_mask
= wxLIST_MASK_TEXT
;
643 wxLogMessage(wxT("Value of the 2nd field of the selected item: %s"),
644 info
.m_text
.c_str());
648 wxFAIL_MSG(wxT("wxListCtrl::GetItem() failed"));
653 void MyListCtrl::OnDeselected(wxListEvent
& event
)
655 LogEvent(event
, _T("OnDeselected"));
658 void MyListCtrl::OnActivated(wxListEvent
& event
)
660 LogEvent(event
, _T("OnActivated"));
663 void MyListCtrl::OnListKeyDown(wxListEvent
& event
)
665 switch ( event
.GetCode() )
670 info
.m_itemId
= event
.GetIndex();
673 wxListItemAttr
*attr
= info
.GetAttributes();
674 if ( !attr
|| !attr
->HasTextColour() )
676 info
.SetTextColour(*wxCYAN
);
685 long item
= GetNextItem(-1,
686 wxLIST_NEXT_ALL
, wxLIST_STATE_SELECTED
);
691 wxLogMessage(_T("Item %ld deleted"), item
);
693 // -1 because the indices were shifted by DeleteItem()
694 item
= GetNextItem(item
- 1,
695 wxLIST_NEXT_ALL
, wxLIST_STATE_SELECTED
);
701 if ( GetWindowStyle() & wxLC_REPORT
)
703 if ( GetWindowStyle() & wxLC_VIRTUAL
)
705 SetItemCount(GetItemCount() + 1);
709 InsertItemInReportView(event
.GetIndex());
715 LogEvent(event
, _T("OnListKeyDown"));
721 void MyListCtrl::OnChar(wxKeyEvent
& event
)
723 wxLogMessage(_T("Got char event."));
728 void MyListCtrl::LogEvent(const wxListEvent
& event
, const wxChar
*eventName
)
730 wxLogMessage(_T("Item %ld: %s (item text = %s, data = %ld)"),
731 event
.GetIndex(), eventName
,
732 event
.GetText().c_str(), event
.GetData());
735 wxString
MyListCtrl::OnGetItemText(long item
, long column
) const
737 return wxString::Format(_T("Column %ld of item %ld"), column
, item
);
740 int MyListCtrl::OnGetItemImage(long item
) const
745 wxListItemAttr
*MyListCtrl::OnGetItemAttr(long item
) const
747 return item
% 2 ? NULL
: (wxListItemAttr
*)&m_attr
;
750 void MyListCtrl::InsertItemInReportView(int i
)
753 buf
.Printf(_T("This is item %d"), i
);
754 long tmp
= InsertItem(i
, buf
, 0);
757 buf
.Printf(_T("Col 1, item %d"), i
);
760 buf
.Printf(_T("Item %d in column 2"), i
);