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_FOCUS_LAST
, MyFrame::OnFocusLast
)
64 EVT_MENU(LIST_TOGGLE_FIRST
, MyFrame::OnToggleFirstSel
)
65 EVT_MENU(LIST_DESELECT_ALL
, MyFrame::OnDeselectAll
)
66 EVT_MENU(LIST_SELECT_ALL
, MyFrame::OnSelectAll
)
67 EVT_MENU(LIST_DELETE
, MyFrame::OnDelete
)
68 EVT_MENU(LIST_ADD
, MyFrame::OnAdd
)
69 EVT_MENU(LIST_DELETE_ALL
, MyFrame::OnDeleteAll
)
70 EVT_MENU(LIST_SORT
, MyFrame::OnSort
)
71 EVT_MENU(LIST_SET_FG_COL
, MyFrame::OnSetFgColour
)
72 EVT_MENU(LIST_SET_BG_COL
, MyFrame::OnSetBgColour
)
73 EVT_MENU(LIST_TOGGLE_MULTI_SEL
, MyFrame::OnToggleMultiSel
)
74 EVT_MENU(LIST_SHOW_COL_INFO
, MyFrame::OnShowColInfo
)
75 EVT_MENU(LIST_SHOW_SEL_INFO
, MyFrame::OnShowSelInfo
)
77 EVT_UPDATE_UI(LIST_SHOW_COL_INFO
, MyFrame::OnUpdateShowColInfo
)
80 BEGIN_EVENT_TABLE(MyListCtrl
, wxListCtrl
)
81 EVT_LIST_BEGIN_DRAG(LIST_CTRL
, MyListCtrl::OnBeginDrag
)
82 EVT_LIST_BEGIN_RDRAG(LIST_CTRL
, MyListCtrl::OnBeginRDrag
)
83 EVT_LIST_BEGIN_LABEL_EDIT(LIST_CTRL
, MyListCtrl::OnBeginLabelEdit
)
84 EVT_LIST_END_LABEL_EDIT(LIST_CTRL
, MyListCtrl::OnEndLabelEdit
)
85 EVT_LIST_DELETE_ITEM(LIST_CTRL
, MyListCtrl::OnDeleteItem
)
86 EVT_LIST_DELETE_ALL_ITEMS(LIST_CTRL
, MyListCtrl::OnDeleteAllItems
)
87 EVT_LIST_GET_INFO(LIST_CTRL
, MyListCtrl::OnGetInfo
)
88 EVT_LIST_SET_INFO(LIST_CTRL
, MyListCtrl::OnSetInfo
)
89 EVT_LIST_ITEM_SELECTED(LIST_CTRL
, MyListCtrl::OnSelected
)
90 EVT_LIST_ITEM_DESELECTED(LIST_CTRL
, MyListCtrl::OnDeselected
)
91 EVT_LIST_KEY_DOWN(LIST_CTRL
, MyListCtrl::OnListKeyDown
)
92 EVT_LIST_ITEM_ACTIVATED(LIST_CTRL
, MyListCtrl::OnActivated
)
93 EVT_LIST_COL_CLICK(LIST_CTRL
, MyListCtrl::OnColClick
)
94 EVT_LIST_CACHE_HINT(LIST_CTRL
, MyListCtrl::OnCacheHint
)
96 EVT_CHAR(MyListCtrl::OnChar
)
101 // number of items in list/report view
102 static const int NUM_ITEMS
= 30;
104 // number of items in icon/small icon view
105 static const int NUM_ICONS
= 9;
107 int wxCALLBACK
MyCompareFunction(long item1
, long item2
, long sortData
)
110 return item1
< item2
;
113 // `Main program' equivalent, creating windows and returning main app frame
116 // Create the main frame window
117 MyFrame
*frame
= new MyFrame(wxT("wxListCtrl Test"), 50, 50, 450, 340);
127 // My frame constructor
128 MyFrame::MyFrame(const wxChar
*title
, int x
, int y
, int w
, int h
)
129 : wxFrame((wxFrame
*)NULL
, -1, title
, wxPoint(x
, y
), wxSize(w
, h
))
131 m_listCtrl
= (MyListCtrl
*) NULL
;
132 m_logWindow
= (wxTextCtrl
*) NULL
;
135 SetIcon( wxICON(mondrian
) );
137 // Make an image list containing large icons
138 m_imageListNormal
= new wxImageList(32, 32, TRUE
);
139 m_imageListSmall
= new wxImageList(16, 16, TRUE
);
142 m_imageListNormal
->Add( wxIcon("icon1", wxBITMAP_TYPE_ICO_RESOURCE
) );
143 m_imageListNormal
->Add( wxIcon("icon2", wxBITMAP_TYPE_ICO_RESOURCE
) );
144 m_imageListNormal
->Add( wxIcon("icon3", wxBITMAP_TYPE_ICO_RESOURCE
) );
145 m_imageListNormal
->Add( wxIcon("icon4", wxBITMAP_TYPE_ICO_RESOURCE
) );
146 m_imageListNormal
->Add( wxIcon("icon5", wxBITMAP_TYPE_ICO_RESOURCE
) );
147 m_imageListNormal
->Add( wxIcon("icon6", wxBITMAP_TYPE_ICO_RESOURCE
) );
148 m_imageListNormal
->Add( wxIcon("icon7", wxBITMAP_TYPE_ICO_RESOURCE
) );
149 m_imageListNormal
->Add( wxIcon("icon8", wxBITMAP_TYPE_ICO_RESOURCE
) );
150 m_imageListNormal
->Add( wxIcon("icon9", wxBITMAP_TYPE_ICO_RESOURCE
) );
152 m_imageListSmall
->Add( wxIcon("iconsmall", wxBITMAP_TYPE_ICO_RESOURCE
) );
155 m_imageListNormal
->Add( wxIcon( toolbrai_xpm
) );
156 m_imageListNormal
->Add( wxIcon( toolchar_xpm
) );
157 m_imageListNormal
->Add( wxIcon( tooldata_xpm
) );
158 m_imageListNormal
->Add( wxIcon( toolnote_xpm
) );
159 m_imageListNormal
->Add( wxIcon( tooltodo_xpm
) );
160 m_imageListNormal
->Add( wxIcon( toolchec_xpm
) );
161 m_imageListNormal
->Add( wxIcon( toolgame_xpm
) );
162 m_imageListNormal
->Add( wxIcon( tooltime_xpm
) );
163 m_imageListNormal
->Add( wxIcon( toolword_xpm
) );
165 m_imageListSmall
->Add( wxIcon( small1_xpm
) );
169 wxMenu
*menuFile
= new wxMenu
;
170 menuFile
->Append(LIST_ABOUT
, _T("&About"));
171 menuFile
->AppendSeparator();
172 menuFile
->Append(LIST_QUIT
, _T("E&xit\tAlt-X"));
174 wxMenu
*menuView
= new wxMenu
;
175 menuView
->Append(LIST_LIST_VIEW
, _T("&List view\tF1"));
176 menuView
->Append(LIST_REPORT_VIEW
, _T("&Report view\tF2"));
177 menuView
->Append(LIST_ICON_VIEW
, _T("&Icon view\tF3"));
178 menuView
->Append(LIST_ICON_TEXT_VIEW
, _T("Icon view with &text\tF4"));
179 menuView
->Append(LIST_SMALL_ICON_VIEW
, _T("&Small icon view\tF5"));
180 menuView
->Append(LIST_SMALL_ICON_TEXT_VIEW
, _T("Small icon &view with text\tF6"));
181 menuView
->Append(LIST_VIRTUAL_VIEW
, _T("Virtual view\tF7"));
183 wxMenu
*menuList
= new wxMenu
;
184 menuList
->Append(LIST_FOCUS_LAST
, _T("&Make last item current\tCtrl-L"));
185 menuList
->Append(LIST_TOGGLE_FIRST
, _T("&Toggle first item\tCtrl-T"));
186 menuList
->Append(LIST_DESELECT_ALL
, _T("&Deselect All\tCtrl-D"));
187 menuList
->Append(LIST_SELECT_ALL
, _T("S&elect All\tCtrl-A"));
188 menuList
->AppendSeparator();
189 menuList
->Append(LIST_SHOW_COL_INFO
, _T("Show &column info\tCtrl-C"));
190 menuList
->Append(LIST_SHOW_SEL_INFO
, _T("Show &selected items\tCtrl-S"));
191 menuList
->AppendSeparator();
192 menuList
->Append(LIST_SORT
, _T("&Sort\tCtrl-S"));
193 menuList
->AppendSeparator();
194 menuList
->Append(LIST_ADD
, _T("&Append an item\tCtrl-P"));
195 menuList
->Append(LIST_DELETE
, _T("&Delete first item\tCtrl-X"));
196 menuList
->Append(LIST_DELETE_ALL
, _T("Delete &all items"));
197 menuList
->AppendSeparator();
198 menuList
->Append(LIST_TOGGLE_MULTI_SEL
, _T("&Multiple selection\tCtrl-M"),
199 _T("Toggle multiple selection"), TRUE
);
201 wxMenu
*menuCol
= new wxMenu
;
202 menuCol
->Append(LIST_SET_FG_COL
, _T("&Foreground colour..."));
203 menuCol
->Append(LIST_SET_BG_COL
, _T("&Background colour..."));
205 wxMenuBar
*menubar
= new wxMenuBar
;
206 menubar
->Append(menuFile
, _T("&File"));
207 menubar
->Append(menuView
, _T("&View"));
208 menubar
->Append(menuList
, _T("&List"));
209 menubar
->Append(menuCol
, _T("&Colour"));
212 m_logWindow
= new wxTextCtrl(this, -1, wxEmptyString
,
213 wxDefaultPosition
, wxDefaultSize
,
214 wxTE_MULTILINE
| wxSUNKEN_BORDER
);
216 m_logOld
= wxLog::SetActiveTarget(new wxLogTextCtrl(m_logWindow
));
218 RecreateList(wxLC_REPORT
| wxLC_SINGLE_SEL
);
225 delete wxLog::SetActiveTarget(m_logOld
);
227 delete m_imageListNormal
;
228 delete m_imageListSmall
;
231 void MyFrame::OnSize(wxSizeEvent
& event
)
236 wxSize size
= GetClientSize();
237 wxCoord y
= (2*size
.y
)/3;
238 m_listCtrl
->SetSize(0, 0, size
.x
, y
);
239 m_logWindow
->SetSize(0, y
+ 1, size
.x
, size
.y
- y
);
244 void MyFrame::OnQuit(wxCommandEvent
& WXUNUSED(event
))
249 void MyFrame::OnAbout(wxCommandEvent
& WXUNUSED(event
))
251 wxMessageDialog
dialog(this, "List test sample\nJulian Smart (c) 1997",
252 "About list test", wxOK
|wxCANCEL
);
257 void MyFrame::OnFocusLast(wxCommandEvent
& WXUNUSED(event
))
259 long index
= m_listCtrl
->GetItemCount() - 1;
265 m_listCtrl
->SetItemState(index
, wxLIST_STATE_FOCUSED
, wxLIST_STATE_FOCUSED
);
266 m_listCtrl
->EnsureVisible(index
);
269 void MyFrame::OnToggleFirstSel(wxCommandEvent
& WXUNUSED(event
))
271 m_listCtrl
->SetItemState(0, wxLIST_STATE_SELECTED
, wxLIST_STATE_SELECTED
);
274 void MyFrame::OnDeselectAll(wxCommandEvent
& WXUNUSED(event
))
276 int n
= m_listCtrl
->GetItemCount();
277 for (int i
= 0; i
< n
; i
++)
278 m_listCtrl
->SetItemState(i
,0,wxLIST_STATE_SELECTED
);
281 void MyFrame::OnSelectAll(wxCommandEvent
& WXUNUSED(event
))
283 int n
= m_listCtrl
->GetItemCount();
284 for (int i
= 0; i
< n
; i
++)
285 m_listCtrl
->SetItemState(i
,wxLIST_STATE_SELECTED
, wxLIST_STATE_SELECTED
);
288 // ----------------------------------------------------------------------------
289 // changing listctrl modes
290 // ----------------------------------------------------------------------------
292 void MyFrame::RecreateList(long flags
, bool withText
)
294 // we could avoid recreating it if we don't set/clear the wxLC_VIRTUAL
295 // style, but it is more trouble to do it than not
297 if ( !m_listCtrl
|| ((flags
& wxLC_VIRTUAL
) !=
298 (m_listCtrl
->GetWindowStyleFlag() & wxLC_VIRTUAL
)) )
303 m_listCtrl
= new MyListCtrl(this, LIST_CTRL
,
304 wxDefaultPosition
, wxDefaultSize
,
308 switch ( flags
& wxLC_MASK_TYPE
)
315 InitWithIconItems(withText
);
318 case wxLC_SMALL_ICON
:
319 InitWithIconItems(withText
, TRUE
);
323 if ( flags
& wxLC_VIRTUAL
)
324 InitWithVirtualItems();
326 InitWithReportItems();
330 wxFAIL_MSG( _T("unknown listctrl mode") );
338 m_logWindow
->Clear();
341 void MyFrame::OnListView(wxCommandEvent
& WXUNUSED(event
))
343 RecreateList(wxLC_LIST
);
346 void MyFrame::InitWithListItems()
348 for ( int i
= 0; i
< NUM_ITEMS
; i
++ )
350 m_listCtrl
->InsertItem(i
, wxString::Format(_T("Item %d"), i
));
354 void MyFrame::OnReportView(wxCommandEvent
& WXUNUSED(event
))
356 RecreateList(wxLC_REPORT
);
359 void MyFrame::InitWithReportItems()
361 m_listCtrl
->SetImageList(m_imageListSmall
, wxIMAGE_LIST_SMALL
);
363 m_listCtrl
->InsertColumn(0, "Column 1"); // , wxLIST_FORMAT_LEFT, 140);
364 m_listCtrl
->InsertColumn(1, "Column 2"); // , wxLIST_FORMAT_LEFT, 140);
365 m_listCtrl
->InsertColumn(2, "One More Column (2)"); // , wxLIST_FORMAT_LEFT, 140);
367 // to speed up inserting we hide the control temporarily
372 for ( int i
= 0; i
< NUM_ITEMS
; i
++ )
374 m_listCtrl
->InsertItemInReportView(i
);
377 m_logWindow
->WriteText(wxString::Format(_T("%d items inserted in %ldms\n"),
378 NUM_ITEMS
, sw
.Time()));
381 // we leave all mask fields to 0 and only change the colour
384 item
.SetTextColour(*wxRED
);
385 m_listCtrl
->SetItem( item
);
388 item
.SetTextColour(*wxGREEN
);
389 m_listCtrl
->SetItem( item
);
391 item
.SetTextColour(*wxLIGHT_GREY
);
392 item
.SetFont(*wxITALIC_FONT
);
393 item
.SetBackgroundColour(*wxRED
);
394 m_listCtrl
->SetItem( item
);
396 m_listCtrl
->SetTextColour(*wxBLUE
);
397 m_listCtrl
->SetBackgroundColour(*wxLIGHT_GREY
);
399 m_listCtrl
->SetColumnWidth( 0, wxLIST_AUTOSIZE
);
400 m_listCtrl
->SetColumnWidth( 1, wxLIST_AUTOSIZE
);
401 m_listCtrl
->SetColumnWidth( 2, wxLIST_AUTOSIZE
);
404 void MyFrame::InitWithIconItems(bool withText
, bool sameIcon
)
406 m_listCtrl
->SetImageList(m_imageListNormal
, wxIMAGE_LIST_NORMAL
);
407 m_listCtrl
->SetImageList(m_imageListSmall
, wxIMAGE_LIST_SMALL
);
409 for ( int i
= 0; i
< NUM_ICONS
; i
++ )
411 int image
= sameIcon
? 0 : i
;
415 m_listCtrl
->InsertItem(i
, wxString::Format(_T("Label %d"), i
),
420 m_listCtrl
->InsertItem(i
, image
);
425 void MyFrame::OnIconView(wxCommandEvent
& WXUNUSED(event
))
427 RecreateList(wxLC_ICON
, FALSE
);
430 void MyFrame::OnIconTextView(wxCommandEvent
& WXUNUSED(event
))
432 RecreateList(wxLC_ICON
);
435 void MyFrame::OnSmallIconView(wxCommandEvent
& WXUNUSED(event
))
437 RecreateList(wxLC_SMALL_ICON
, FALSE
);
440 void MyFrame::OnSmallIconTextView(wxCommandEvent
& WXUNUSED(event
))
442 RecreateList(wxLC_SMALL_ICON
);
445 void MyFrame::OnVirtualView(wxCommandEvent
& WXUNUSED(event
))
447 RecreateList(wxLC_REPORT
| wxLC_VIRTUAL
);
450 void MyFrame::InitWithVirtualItems()
452 m_listCtrl
->InsertColumn(0, "First Column");
453 m_listCtrl
->InsertColumn(1, "Second Column");
454 m_listCtrl
->SetColumnWidth(0, 150);
455 m_listCtrl
->SetColumnWidth(1, 150);
457 m_listCtrl
->SetItemCount(1000000);
460 void MyFrame::OnSort(wxCommandEvent
& WXUNUSED(event
))
464 m_listCtrl
->SortItems(MyCompareFunction
, 0);
466 m_logWindow
->WriteText(wxString::Format(_T("Sorting %d items took %ld ms\n"),
467 m_listCtrl
->GetItemCount(),
471 void MyFrame::OnShowSelInfo(wxCommandEvent
& event
)
473 int selCount
= m_listCtrl
->GetSelectedItemCount();
474 wxLogMessage(_T("%d items selected:"), selCount
);
476 // don't show too many items
477 size_t shownCount
= 0;
479 long item
= m_listCtrl
->GetNextItem(-1, wxLIST_NEXT_ALL
,
480 wxLIST_STATE_SELECTED
);
483 wxLogMessage(_T("\t%ld (%s)"),
484 item
, m_listCtrl
->GetItemText(item
).c_str());
486 if ( ++shownCount
> 10 )
488 wxLogMessage(_T("\t... more selected items snipped..."));
492 item
= m_listCtrl
->GetNextItem(item
, wxLIST_NEXT_ALL
,
493 wxLIST_STATE_SELECTED
);
497 void MyFrame::OnShowColInfo(wxCommandEvent
& event
)
499 int count
= m_listCtrl
->GetColumnCount();
500 wxLogMessage(wxT("%d columns:"), count
);
501 for ( int c
= 0; c
< count
; c
++ )
503 wxLogMessage(wxT("\tcolumn %d has width %d"), c
,
504 m_listCtrl
->GetColumnWidth(c
));
508 void MyFrame::OnUpdateShowColInfo(wxUpdateUIEvent
& event
)
510 event
.Enable( (m_listCtrl
->GetWindowStyleFlag() & wxLC_REPORT
) != 0 );
513 void MyFrame::OnToggleMultiSel(wxCommandEvent
& WXUNUSED(event
))
515 long flags
= m_listCtrl
->GetWindowStyleFlag();
516 if ( flags
& wxLC_SINGLE_SEL
)
517 flags
&= ~wxLC_SINGLE_SEL
;
519 flags
|= wxLC_SINGLE_SEL
;
521 m_logWindow
->WriteText(wxString::Format(wxT("Current selection mode: %sle\n"),
522 (flags
& wxLC_SINGLE_SEL
) ? "sing" : "multip"));
527 void MyFrame::OnSetFgColour(wxCommandEvent
& WXUNUSED(event
))
529 m_listCtrl
->SetForegroundColour(wxGetColourFromUser(this));
530 m_listCtrl
->Refresh();
533 void MyFrame::OnSetBgColour(wxCommandEvent
& WXUNUSED(event
))
535 m_listCtrl
->SetBackgroundColour(wxGetColourFromUser(this));
536 m_listCtrl
->Refresh();
539 void MyFrame::OnAdd(wxCommandEvent
& WXUNUSED(event
))
541 m_listCtrl
->InsertItem(m_listCtrl
->GetItemCount(), _T("Appended item"));
544 void MyFrame::OnDelete(wxCommandEvent
& WXUNUSED(event
))
546 if ( m_listCtrl
->GetItemCount() )
548 m_listCtrl
->DeleteItem(0);
552 m_logWindow
->WriteText("Nothing to delete");
556 void MyFrame::OnDeleteAll(wxCommandEvent
& WXUNUSED(event
))
560 m_listCtrl
->DeleteAllItems();
562 m_logWindow
->WriteText(wxString::Format(_T("Deleting %d items took %ld ms\n"),
563 m_listCtrl
->GetItemCount(),
569 void MyListCtrl::OnCacheHint(wxListEvent
& event
)
571 wxLogMessage( wxT("OnCacheHint: cache items %ld..%ld"),
572 event
.GetCacheFrom(), event
.GetCacheTo() );
575 void MyListCtrl::OnColClick(wxListEvent
& event
)
577 wxLogMessage( wxT("OnColumnClick at %d."), event
.GetColumn() );
580 void MyListCtrl::OnBeginDrag(wxListEvent
& event
)
582 wxLogMessage( wxT("OnBeginDrag at %d,%d."),
583 event
.m_pointDrag
.x
, event
.m_pointDrag
.y
);
586 void MyListCtrl::OnBeginRDrag(wxListEvent
& event
)
588 wxLogMessage( wxT("OnBeginRDrag at %d,%d."),
589 event
.m_pointDrag
.x
, event
.m_pointDrag
.y
);
592 void MyListCtrl::OnBeginLabelEdit(wxListEvent
& event
)
594 wxLogMessage( wxT("OnBeginLabelEdit: %s"), event
.m_item
.m_text
.c_str());
597 void MyListCtrl::OnEndLabelEdit(wxListEvent
& event
)
599 wxLogMessage( wxT("OnEndLabelEdit: %s"), event
.m_item
.m_text
.c_str());
602 void MyListCtrl::OnDeleteItem(wxListEvent
& event
)
604 LogEvent(event
, _T("OnDeleteItem"));
607 void MyListCtrl::OnDeleteAllItems(wxListEvent
& event
)
609 LogEvent(event
, _T("OnDeleteAllItems"));
612 void MyListCtrl::OnGetInfo(wxListEvent
& event
)
616 msg
<< "OnGetInfo (" << event
.m_item
.m_itemId
<< ", " << event
.m_item
.m_col
<< ")";
617 if ( event
.m_item
.m_mask
& wxLIST_MASK_STATE
)
618 msg
<< " wxLIST_MASK_STATE";
619 if ( event
.m_item
.m_mask
& wxLIST_MASK_TEXT
)
620 msg
<< " wxLIST_MASK_TEXT";
621 if ( event
.m_item
.m_mask
& wxLIST_MASK_IMAGE
)
622 msg
<< " wxLIST_MASK_IMAGE";
623 if ( event
.m_item
.m_mask
& wxLIST_MASK_DATA
)
624 msg
<< " wxLIST_MASK_DATA";
625 if ( event
.m_item
.m_mask
& wxLIST_SET_ITEM
)
626 msg
<< " wxLIST_SET_ITEM";
627 if ( event
.m_item
.m_mask
& wxLIST_MASK_WIDTH
)
628 msg
<< " wxLIST_MASK_WIDTH";
629 if ( event
.m_item
.m_mask
& wxLIST_MASK_FORMAT
)
630 msg
<< " wxLIST_MASK_WIDTH";
632 if ( event
.m_item
.m_mask
& wxLIST_MASK_TEXT
)
634 event
.m_item
.m_text
= "My callback text";
640 void MyListCtrl::OnSetInfo(wxListEvent
& event
)
642 LogEvent(event
, _T("OnSetInfo"));
645 void MyListCtrl::OnSelected(wxListEvent
& event
)
647 LogEvent(event
, _T("OnSelected"));
649 if ( GetWindowStyle() & wxLC_REPORT
)
652 info
.m_itemId
= event
.m_itemIndex
;
654 info
.m_mask
= wxLIST_MASK_TEXT
;
657 wxLogMessage(wxT("Value of the 2nd field of the selected item: %s"),
658 info
.m_text
.c_str());
662 wxFAIL_MSG(wxT("wxListCtrl::GetItem() failed"));
667 void MyListCtrl::OnDeselected(wxListEvent
& event
)
669 LogEvent(event
, _T("OnDeselected"));
672 void MyListCtrl::OnActivated(wxListEvent
& event
)
674 LogEvent(event
, _T("OnActivated"));
677 void MyListCtrl::OnListKeyDown(wxListEvent
& event
)
679 switch ( event
.GetCode() )
684 info
.m_itemId
= event
.GetIndex();
687 wxListItemAttr
*attr
= info
.GetAttributes();
688 if ( !attr
|| !attr
->HasTextColour() )
690 info
.SetTextColour(*wxCYAN
);
699 long item
= GetNextItem(-1,
700 wxLIST_NEXT_ALL
, wxLIST_STATE_SELECTED
);
705 wxLogMessage(_T("Item %ld deleted"), item
);
707 // -1 because the indices were shifted by DeleteItem()
708 item
= GetNextItem(item
- 1,
709 wxLIST_NEXT_ALL
, wxLIST_STATE_SELECTED
);
715 if ( GetWindowStyle() & wxLC_REPORT
)
717 if ( GetWindowStyle() & wxLC_VIRTUAL
)
719 SetItemCount(GetItemCount() + 1);
723 InsertItemInReportView(event
.GetIndex());
729 LogEvent(event
, _T("OnListKeyDown"));
735 void MyListCtrl::OnChar(wxKeyEvent
& event
)
737 wxLogMessage(_T("Got char event."));
742 void MyListCtrl::LogEvent(const wxListEvent
& event
, const wxChar
*eventName
)
744 wxLogMessage(_T("Item %ld: %s (item text = %s, data = %ld)"),
745 event
.GetIndex(), eventName
,
746 event
.GetText().c_str(), event
.GetData());
749 wxString
MyListCtrl::OnGetItemText(long item
, long column
) const
751 return wxString::Format(_T("Column %ld of item %ld"), column
, item
);
754 int MyListCtrl::OnGetItemImage(long item
) const
759 wxListItemAttr
*MyListCtrl::OnGetItemAttr(long item
) const
761 return item
% 2 ? NULL
: (wxListItemAttr
*)&m_attr
;
764 void MyListCtrl::InsertItemInReportView(int i
)
767 buf
.Printf(_T("This is item %d"), i
);
768 long tmp
= InsertItem(i
, buf
, 0);
771 buf
.Printf(_T("Col 1, item %d"), i
);
774 buf
.Printf(_T("Item %d in column 2"), i
);