1 /////////////////////////////////////////////////////////////////////////////
3 // Purpose: wxListCtrl sample
4 // Author: Julian Smart
8 // Copyright: (c) Julian Smart
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_EDIT
, MyFrame::OnEdit
)
70 EVT_MENU(LIST_DELETE_ALL
, MyFrame::OnDeleteAll
)
71 EVT_MENU(LIST_SORT
, MyFrame::OnSort
)
72 EVT_MENU(LIST_SET_FG_COL
, MyFrame::OnSetFgColour
)
73 EVT_MENU(LIST_SET_BG_COL
, MyFrame::OnSetBgColour
)
74 EVT_MENU(LIST_TOGGLE_MULTI_SEL
, MyFrame::OnToggleMultiSel
)
75 EVT_MENU(LIST_SHOW_COL_INFO
, MyFrame::OnShowColInfo
)
76 EVT_MENU(LIST_SHOW_SEL_INFO
, MyFrame::OnShowSelInfo
)
77 EVT_MENU(LIST_FREEZE
, MyFrame::OnFreeze
)
78 EVT_MENU(LIST_THAW
, MyFrame::OnThaw
)
80 EVT_UPDATE_UI(LIST_SHOW_COL_INFO
, MyFrame::OnUpdateShowColInfo
)
83 BEGIN_EVENT_TABLE(MyListCtrl
, wxListCtrl
)
84 EVT_LIST_BEGIN_DRAG(LIST_CTRL
, MyListCtrl::OnBeginDrag
)
85 EVT_LIST_BEGIN_RDRAG(LIST_CTRL
, MyListCtrl::OnBeginRDrag
)
86 EVT_LIST_BEGIN_LABEL_EDIT(LIST_CTRL
, MyListCtrl::OnBeginLabelEdit
)
87 EVT_LIST_END_LABEL_EDIT(LIST_CTRL
, MyListCtrl::OnEndLabelEdit
)
88 EVT_LIST_DELETE_ITEM(LIST_CTRL
, MyListCtrl::OnDeleteItem
)
89 EVT_LIST_DELETE_ALL_ITEMS(LIST_CTRL
, MyListCtrl::OnDeleteAllItems
)
90 EVT_LIST_GET_INFO(LIST_CTRL
, MyListCtrl::OnGetInfo
)
91 EVT_LIST_SET_INFO(LIST_CTRL
, MyListCtrl::OnSetInfo
)
92 EVT_LIST_ITEM_SELECTED(LIST_CTRL
, MyListCtrl::OnSelected
)
93 EVT_LIST_ITEM_DESELECTED(LIST_CTRL
, MyListCtrl::OnDeselected
)
94 EVT_LIST_KEY_DOWN(LIST_CTRL
, MyListCtrl::OnListKeyDown
)
95 EVT_LIST_ITEM_ACTIVATED(LIST_CTRL
, MyListCtrl::OnActivated
)
96 EVT_LIST_ITEM_FOCUSED(LIST_CTRL
, MyListCtrl::OnFocused
)
98 EVT_LIST_COL_CLICK(LIST_CTRL
, MyListCtrl::OnColClick
)
99 EVT_LIST_COL_RIGHT_CLICK(LIST_CTRL
, MyListCtrl::OnColRightClick
)
100 EVT_LIST_COL_BEGIN_DRAG(LIST_CTRL
, MyListCtrl::OnColBeginDrag
)
101 EVT_LIST_COL_DRAGGING(LIST_CTRL
, MyListCtrl::OnColDragging
)
102 EVT_LIST_COL_END_DRAG(LIST_CTRL
, MyListCtrl::OnColEndDrag
)
104 EVT_LIST_CACHE_HINT(LIST_CTRL
, MyListCtrl::OnCacheHint
)
106 EVT_CHAR(MyListCtrl::OnChar
)
111 // number of items in list/report view
112 static const int NUM_ITEMS
= 30;
114 // number of items in icon/small icon view
115 static const int NUM_ICONS
= 9;
117 int wxCALLBACK
MyCompareFunction(long item1
, long item2
, long sortData
)
120 if (item1
< item2
) return -1;
121 if (item1
> item2
) return 1;
125 // `Main program' equivalent, creating windows and returning main app frame
128 // Create the main frame window
129 MyFrame
*frame
= new MyFrame(wxT("wxListCtrl Test"), 50, 50, 450, 340);
139 // My frame constructor
140 MyFrame::MyFrame(const wxChar
*title
, int x
, int y
, int w
, int h
)
141 : wxFrame((wxFrame
*)NULL
, -1, title
, wxPoint(x
, y
), wxSize(w
, h
))
143 m_listCtrl
= (MyListCtrl
*) NULL
;
144 m_logWindow
= (wxTextCtrl
*) NULL
;
147 SetIcon( wxICON(mondrian
) );
149 // Make an image list containing large icons
150 m_imageListNormal
= new wxImageList(32, 32, TRUE
);
151 m_imageListSmall
= new wxImageList(16, 16, TRUE
);
154 m_imageListNormal
->Add( wxIcon(_T("icon1"), wxBITMAP_TYPE_ICO_RESOURCE
) );
155 m_imageListNormal
->Add( wxIcon(_T("icon2"), wxBITMAP_TYPE_ICO_RESOURCE
) );
156 m_imageListNormal
->Add( wxIcon(_T("icon3"), wxBITMAP_TYPE_ICO_RESOURCE
) );
157 m_imageListNormal
->Add( wxIcon(_T("icon4"), wxBITMAP_TYPE_ICO_RESOURCE
) );
158 m_imageListNormal
->Add( wxIcon(_T("icon5"), wxBITMAP_TYPE_ICO_RESOURCE
) );
159 m_imageListNormal
->Add( wxIcon(_T("icon6"), wxBITMAP_TYPE_ICO_RESOURCE
) );
160 m_imageListNormal
->Add( wxIcon(_T("icon7"), wxBITMAP_TYPE_ICO_RESOURCE
) );
161 m_imageListNormal
->Add( wxIcon(_T("icon8"), wxBITMAP_TYPE_ICO_RESOURCE
) );
162 m_imageListNormal
->Add( wxIcon(_T("icon9"), wxBITMAP_TYPE_ICO_RESOURCE
) );
164 m_imageListSmall
->Add( wxIcon(_T("iconsmall"), wxBITMAP_TYPE_ICO_RESOURCE
) );
167 m_imageListNormal
->Add( wxIcon( toolbrai_xpm
) );
168 m_imageListNormal
->Add( wxIcon( toolchar_xpm
) );
169 m_imageListNormal
->Add( wxIcon( tooldata_xpm
) );
170 m_imageListNormal
->Add( wxIcon( toolnote_xpm
) );
171 m_imageListNormal
->Add( wxIcon( tooltodo_xpm
) );
172 m_imageListNormal
->Add( wxIcon( toolchec_xpm
) );
173 m_imageListNormal
->Add( wxIcon( toolgame_xpm
) );
174 m_imageListNormal
->Add( wxIcon( tooltime_xpm
) );
175 m_imageListNormal
->Add( wxIcon( toolword_xpm
) );
177 m_imageListSmall
->Add( wxIcon( small1_xpm
) );
181 wxMenu
*menuFile
= new wxMenu
;
182 menuFile
->Append(LIST_ABOUT
, _T("&About"));
183 menuFile
->AppendSeparator();
184 menuFile
->Append(LIST_QUIT
, _T("E&xit\tAlt-X"));
186 wxMenu
*menuView
= new wxMenu
;
187 menuView
->Append(LIST_LIST_VIEW
, _T("&List view\tF1"));
188 menuView
->Append(LIST_REPORT_VIEW
, _T("&Report view\tF2"));
189 menuView
->Append(LIST_ICON_VIEW
, _T("&Icon view\tF3"));
190 menuView
->Append(LIST_ICON_TEXT_VIEW
, _T("Icon view with &text\tF4"));
191 menuView
->Append(LIST_SMALL_ICON_VIEW
, _T("&Small icon view\tF5"));
192 menuView
->Append(LIST_SMALL_ICON_TEXT_VIEW
, _T("Small icon &view with text\tF6"));
193 menuView
->Append(LIST_VIRTUAL_VIEW
, _T("Virtual view\tF7"));
195 wxMenu
*menuList
= new wxMenu
;
196 menuList
->Append(LIST_FOCUS_LAST
, _T("&Make last item current\tCtrl-L"));
197 menuList
->Append(LIST_TOGGLE_FIRST
, _T("To&ggle first item\tCtrl-G"));
198 menuList
->Append(LIST_DESELECT_ALL
, _T("&Deselect All\tCtrl-D"));
199 menuList
->Append(LIST_SELECT_ALL
, _T("S&elect All\tCtrl-A"));
200 menuList
->AppendSeparator();
201 menuList
->Append(LIST_SHOW_COL_INFO
, _T("Show &column info\tCtrl-C"));
202 menuList
->Append(LIST_SHOW_SEL_INFO
, _T("Show &selected items\tCtrl-S"));
203 menuList
->AppendSeparator();
204 menuList
->Append(LIST_SORT
, _T("&Sort\tCtrl-S"));
205 menuList
->AppendSeparator();
206 menuList
->Append(LIST_ADD
, _T("&Append an item\tCtrl-P"));
207 menuList
->Append(LIST_EDIT
, _T("&Edit the item\tCtrl-E"));
208 menuList
->Append(LIST_DELETE
, _T("&Delete first item\tCtrl-X"));
209 menuList
->Append(LIST_DELETE_ALL
, _T("Delete &all items"));
210 menuList
->AppendSeparator();
211 menuList
->Append(LIST_FREEZE
, _T("Free&ze\tCtrl-Z"));
212 menuList
->Append(LIST_THAW
, _T("Tha&w\tCtrl-W"));
213 menuList
->AppendSeparator();
214 menuList
->Append(LIST_TOGGLE_MULTI_SEL
, _T("&Multiple selection\tCtrl-M"),
215 _T("Toggle multiple selection"), TRUE
);
217 wxMenu
*menuCol
= new wxMenu
;
218 menuCol
->Append(LIST_SET_FG_COL
, _T("&Foreground colour..."));
219 menuCol
->Append(LIST_SET_BG_COL
, _T("&Background colour..."));
221 wxMenuBar
*menubar
= new wxMenuBar
;
222 menubar
->Append(menuFile
, _T("&File"));
223 menubar
->Append(menuView
, _T("&View"));
224 menubar
->Append(menuList
, _T("&List"));
225 menubar
->Append(menuCol
, _T("&Colour"));
228 m_panel
= new wxPanel(this, -1);
229 m_logWindow
= new wxTextCtrl(m_panel
, -1, wxEmptyString
,
230 wxDefaultPosition
, wxDefaultSize
,
231 wxTE_MULTILINE
| wxSUNKEN_BORDER
);
233 m_logOld
= wxLog::SetActiveTarget(new wxLogTextCtrl(m_logWindow
));
235 RecreateList(wxLC_REPORT
| wxLC_SINGLE_SEL
);
242 delete wxLog::SetActiveTarget(m_logOld
);
244 delete m_imageListNormal
;
245 delete m_imageListSmall
;
248 void MyFrame::OnSize(wxSizeEvent
& event
)
253 wxSize size
= GetClientSize();
254 wxCoord y
= (2*size
.y
)/3;
255 m_listCtrl
->SetSize(0, 0, size
.x
, y
);
256 m_logWindow
->SetSize(0, y
+ 1, size
.x
, size
.y
- y
);
261 void MyFrame::OnQuit(wxCommandEvent
& WXUNUSED(event
))
266 void MyFrame::OnAbout(wxCommandEvent
& WXUNUSED(event
))
268 wxMessageDialog
dialog(this, _T("List test sample\nJulian Smart (c) 1997"),
269 _T("About list test"), wxOK
|wxCANCEL
);
274 void MyFrame::OnFreeze(wxCommandEvent
& event
)
276 wxLogMessage(_T("Freezing the control"));
278 m_listCtrl
->Freeze();
281 void MyFrame::OnThaw(wxCommandEvent
& event
)
283 wxLogMessage(_T("Thawing the control"));
288 void MyFrame::OnFocusLast(wxCommandEvent
& WXUNUSED(event
))
290 long index
= m_listCtrl
->GetItemCount() - 1;
296 m_listCtrl
->SetItemState(index
, wxLIST_STATE_FOCUSED
, wxLIST_STATE_FOCUSED
);
297 m_listCtrl
->EnsureVisible(index
);
300 void MyFrame::OnToggleFirstSel(wxCommandEvent
& WXUNUSED(event
))
302 m_listCtrl
->SetItemState(0, wxLIST_STATE_SELECTED
, wxLIST_STATE_SELECTED
);
305 void MyFrame::OnDeselectAll(wxCommandEvent
& WXUNUSED(event
))
307 int n
= m_listCtrl
->GetItemCount();
308 for (int i
= 0; i
< n
; i
++)
309 m_listCtrl
->SetItemState(i
,0,wxLIST_STATE_SELECTED
);
312 void MyFrame::OnSelectAll(wxCommandEvent
& WXUNUSED(event
))
314 int n
= m_listCtrl
->GetItemCount();
315 for (int i
= 0; i
< n
; i
++)
316 m_listCtrl
->SetItemState(i
,wxLIST_STATE_SELECTED
, wxLIST_STATE_SELECTED
);
319 // ----------------------------------------------------------------------------
320 // changing listctrl modes
321 // ----------------------------------------------------------------------------
323 void MyFrame::RecreateList(long flags
, bool withText
)
325 // we could avoid recreating it if we don't set/clear the wxLC_VIRTUAL
326 // style, but it is more trouble to do it than not
328 if ( !m_listCtrl
|| ((flags
& wxLC_VIRTUAL
) !=
329 (m_listCtrl
->GetWindowStyleFlag() & wxLC_VIRTUAL
)) )
334 m_listCtrl
= new MyListCtrl(m_panel
, LIST_CTRL
,
335 wxDefaultPosition
, wxDefaultSize
,
337 wxSUNKEN_BORDER
| wxLC_EDIT_LABELS
);
339 switch ( flags
& wxLC_MASK_TYPE
)
346 InitWithIconItems(withText
);
349 case wxLC_SMALL_ICON
:
350 InitWithIconItems(withText
, TRUE
);
354 if ( flags
& wxLC_VIRTUAL
)
355 InitWithVirtualItems();
357 InitWithReportItems();
361 wxFAIL_MSG( _T("unknown listctrl mode") );
369 m_logWindow
->Clear();
372 void MyFrame::OnListView(wxCommandEvent
& WXUNUSED(event
))
374 RecreateList(wxLC_LIST
);
377 void MyFrame::InitWithListItems()
379 for ( int i
= 0; i
< NUM_ITEMS
; i
++ )
381 m_listCtrl
->InsertItem(i
, wxString::Format(_T("Item %d"), i
));
385 void MyFrame::OnReportView(wxCommandEvent
& WXUNUSED(event
))
387 RecreateList(wxLC_REPORT
);
390 void MyFrame::InitWithReportItems()
392 m_listCtrl
->SetImageList(m_imageListSmall
, wxIMAGE_LIST_SMALL
);
394 // note that under MSW for SetColumnWidth() to work we need to create the
395 // items with images initially even if we specify dummy image id
397 itemCol
.SetText(_T("Column 1"));
398 itemCol
.SetImage(-1);
399 m_listCtrl
->InsertColumn(0, itemCol
);
401 itemCol
.SetText(_T("Column 2"));
402 itemCol
.SetAlign(wxLIST_FORMAT_CENTRE
);
403 m_listCtrl
->InsertColumn(1, itemCol
);
405 itemCol
.SetText(_T("Column 3"));
406 itemCol
.SetAlign(wxLIST_FORMAT_RIGHT
);
407 m_listCtrl
->InsertColumn(2, itemCol
);
409 // to speed up inserting we hide the control temporarily
414 for ( int i
= 0; i
< NUM_ITEMS
; i
++ )
416 m_listCtrl
->InsertItemInReportView(i
);
419 m_logWindow
->WriteText(wxString::Format(_T("%d items inserted in %ldms\n"),
420 NUM_ITEMS
, sw
.Time()));
423 // we leave all mask fields to 0 and only change the colour
426 item
.SetTextColour(*wxRED
);
427 m_listCtrl
->SetItem( item
);
430 item
.SetTextColour(*wxGREEN
);
431 m_listCtrl
->SetItem( item
);
433 item
.SetTextColour(*wxLIGHT_GREY
);
434 item
.SetFont(*wxITALIC_FONT
);
435 item
.SetBackgroundColour(*wxRED
);
436 m_listCtrl
->SetItem( item
);
438 m_listCtrl
->SetTextColour(*wxBLUE
);
439 m_listCtrl
->SetBackgroundColour(*wxLIGHT_GREY
);
441 m_listCtrl
->SetColumnWidth( 0, wxLIST_AUTOSIZE
);
442 m_listCtrl
->SetColumnWidth( 1, wxLIST_AUTOSIZE
);
443 m_listCtrl
->SetColumnWidth( 2, wxLIST_AUTOSIZE
);
446 void MyFrame::InitWithIconItems(bool withText
, bool sameIcon
)
448 m_listCtrl
->SetImageList(m_imageListNormal
, wxIMAGE_LIST_NORMAL
);
449 m_listCtrl
->SetImageList(m_imageListSmall
, wxIMAGE_LIST_SMALL
);
451 for ( int i
= 0; i
< NUM_ICONS
; i
++ )
453 int image
= sameIcon
? 0 : i
;
457 m_listCtrl
->InsertItem(i
, wxString::Format(_T("Label %d"), i
),
462 m_listCtrl
->InsertItem(i
, image
);
467 void MyFrame::OnIconView(wxCommandEvent
& WXUNUSED(event
))
469 RecreateList(wxLC_ICON
, FALSE
);
472 void MyFrame::OnIconTextView(wxCommandEvent
& WXUNUSED(event
))
474 RecreateList(wxLC_ICON
);
477 void MyFrame::OnSmallIconView(wxCommandEvent
& WXUNUSED(event
))
479 RecreateList(wxLC_SMALL_ICON
, FALSE
);
482 void MyFrame::OnSmallIconTextView(wxCommandEvent
& WXUNUSED(event
))
484 RecreateList(wxLC_SMALL_ICON
);
487 void MyFrame::OnVirtualView(wxCommandEvent
& WXUNUSED(event
))
489 RecreateList(wxLC_REPORT
| wxLC_VIRTUAL
);
492 void MyFrame::InitWithVirtualItems()
494 m_listCtrl
->SetImageList(m_imageListSmall
, wxIMAGE_LIST_SMALL
);
496 m_listCtrl
->InsertColumn(0, _T("First Column"));
497 m_listCtrl
->InsertColumn(1, _T("Second Column"));
498 m_listCtrl
->SetColumnWidth(0, 150);
499 m_listCtrl
->SetColumnWidth(1, 150);
501 m_listCtrl
->SetItemCount(1000000);
504 void MyFrame::OnSort(wxCommandEvent
& WXUNUSED(event
))
508 m_listCtrl
->SortItems(MyCompareFunction
, 0);
510 m_logWindow
->WriteText(wxString::Format(_T("Sorting %d items took %ld ms\n"),
511 m_listCtrl
->GetItemCount(),
515 void MyFrame::OnShowSelInfo(wxCommandEvent
& event
)
517 int selCount
= m_listCtrl
->GetSelectedItemCount();
518 wxLogMessage(_T("%d items selected:"), selCount
);
520 // don't show too many items
521 size_t shownCount
= 0;
523 long item
= m_listCtrl
->GetNextItem(-1, wxLIST_NEXT_ALL
,
524 wxLIST_STATE_SELECTED
);
527 wxLogMessage(_T("\t%ld (%s)"),
528 item
, m_listCtrl
->GetItemText(item
).c_str());
530 if ( ++shownCount
> 10 )
532 wxLogMessage(_T("\t... more selected items snipped..."));
536 item
= m_listCtrl
->GetNextItem(item
, wxLIST_NEXT_ALL
,
537 wxLIST_STATE_SELECTED
);
541 void MyFrame::OnShowColInfo(wxCommandEvent
& event
)
543 int count
= m_listCtrl
->GetColumnCount();
544 wxLogMessage(wxT("%d columns:"), count
);
545 for ( int c
= 0; c
< count
; c
++ )
547 wxLogMessage(wxT("\tcolumn %d has width %d"), c
,
548 m_listCtrl
->GetColumnWidth(c
));
552 void MyFrame::OnUpdateShowColInfo(wxUpdateUIEvent
& event
)
554 event
.Enable( (m_listCtrl
->GetWindowStyleFlag() & wxLC_REPORT
) != 0 );
557 void MyFrame::OnToggleMultiSel(wxCommandEvent
& WXUNUSED(event
))
559 long flags
= m_listCtrl
->GetWindowStyleFlag();
560 if ( flags
& wxLC_SINGLE_SEL
)
561 flags
&= ~wxLC_SINGLE_SEL
;
563 flags
|= wxLC_SINGLE_SEL
;
565 m_logWindow
->WriteText(wxString::Format(wxT("Current selection mode: %sle\n"),
566 (flags
& wxLC_SINGLE_SEL
) ? _T("sing") : _T("multip")));
571 void MyFrame::OnSetFgColour(wxCommandEvent
& WXUNUSED(event
))
573 m_listCtrl
->SetForegroundColour(wxGetColourFromUser(this));
574 m_listCtrl
->Refresh();
577 void MyFrame::OnSetBgColour(wxCommandEvent
& WXUNUSED(event
))
579 m_listCtrl
->SetBackgroundColour(wxGetColourFromUser(this));
580 m_listCtrl
->Refresh();
583 void MyFrame::OnAdd(wxCommandEvent
& WXUNUSED(event
))
585 m_listCtrl
->InsertItem(m_listCtrl
->GetItemCount(), _T("Appended item"));
588 void MyFrame::OnEdit(wxCommandEvent
& WXUNUSED(event
))
590 long itemCur
= m_listCtrl
->GetNextItem(-1, wxLIST_NEXT_ALL
,
591 wxLIST_STATE_FOCUSED
);
595 m_listCtrl
->EditLabel(itemCur
);
599 m_logWindow
->WriteText(_T("No item to edit"));
603 void MyFrame::OnDelete(wxCommandEvent
& WXUNUSED(event
))
605 if ( m_listCtrl
->GetItemCount() )
607 m_listCtrl
->DeleteItem(0);
611 m_logWindow
->WriteText(_T("Nothing to delete"));
615 void MyFrame::OnDeleteAll(wxCommandEvent
& WXUNUSED(event
))
619 m_listCtrl
->DeleteAllItems();
621 m_logWindow
->WriteText(wxString::Format(_T("Deleting %d items took %ld ms\n"),
622 m_listCtrl
->GetItemCount(),
628 void MyListCtrl::OnCacheHint(wxListEvent
& event
)
630 wxLogMessage( wxT("OnCacheHint: cache items %ld..%ld"),
631 event
.GetCacheFrom(), event
.GetCacheTo() );
634 void MyListCtrl::SetColumnImage(int col
, int image
)
637 item
.SetMask(wxLIST_MASK_IMAGE
);
638 item
.SetImage(image
);
639 SetColumn(col
, item
);
642 void MyListCtrl::OnColClick(wxListEvent
& event
)
644 int col
= event
.GetColumn();
645 SetColumnImage(col
, 0);
647 wxLogMessage( wxT("OnColumnClick at %d."), col
);
650 void MyListCtrl::OnColRightClick(wxListEvent
& event
)
652 int col
= event
.GetColumn();
655 SetColumnImage(col
, -1);
658 wxLogMessage( wxT("OnColumnRightClick at %d."), event
.GetColumn() );
661 void MyListCtrl::LogColEvent(const wxListEvent
& event
, const wxChar
*name
)
663 const int col
= event
.GetColumn();
665 wxLogMessage(wxT("%s: column %d (width = %d or %d)."),
668 event
.GetItem().GetWidth(),
669 GetColumnWidth(col
));
672 void MyListCtrl::OnColBeginDrag(wxListEvent
& event
)
674 LogColEvent( event
, wxT("OnColBeginDrag") );
676 if ( event
.GetColumn() == 0 )
678 wxLogMessage(_T("Resizing this column shouldn't work."));
684 void MyListCtrl::OnColDragging(wxListEvent
& event
)
686 LogColEvent( event
, wxT("OnColDragging") );
689 void MyListCtrl::OnColEndDrag(wxListEvent
& event
)
691 LogColEvent( event
, wxT("OnColEndDrag") );
694 void MyListCtrl::OnBeginDrag(wxListEvent
& event
)
696 const wxPoint
& pt
= event
.m_pointDrag
;
699 wxLogMessage( wxT("OnBeginDrag at (%d, %d), item %ld."),
700 pt
.x
, pt
.y
, HitTest(pt
, flags
) );
703 void MyListCtrl::OnBeginRDrag(wxListEvent
& event
)
705 wxLogMessage( wxT("OnBeginRDrag at %d,%d."),
706 event
.m_pointDrag
.x
, event
.m_pointDrag
.y
);
709 void MyListCtrl::OnBeginLabelEdit(wxListEvent
& event
)
711 wxLogMessage( wxT("OnBeginLabelEdit: %s"), event
.m_item
.m_text
.c_str());
714 void MyListCtrl::OnEndLabelEdit(wxListEvent
& event
)
716 wxLogMessage( wxT("OnEndLabelEdit: %s"), event
.m_item
.m_text
.c_str());
719 void MyListCtrl::OnDeleteItem(wxListEvent
& event
)
721 LogEvent(event
, _T("OnDeleteItem"));
724 void MyListCtrl::OnDeleteAllItems(wxListEvent
& event
)
726 LogEvent(event
, _T("OnDeleteAllItems"));
729 void MyListCtrl::OnGetInfo(wxListEvent
& event
)
733 msg
<< _T("OnGetInfo (") << event
.m_item
.m_itemId
<< _T(", ") << event
.m_item
.m_col
<< _T(")");
734 if ( event
.m_item
.m_mask
& wxLIST_MASK_STATE
)
735 msg
<< _T(" wxLIST_MASK_STATE");
736 if ( event
.m_item
.m_mask
& wxLIST_MASK_TEXT
)
737 msg
<< _T(" wxLIST_MASK_TEXT");
738 if ( event
.m_item
.m_mask
& wxLIST_MASK_IMAGE
)
739 msg
<< _T(" wxLIST_MASK_IMAGE");
740 if ( event
.m_item
.m_mask
& wxLIST_MASK_DATA
)
741 msg
<< _T(" wxLIST_MASK_DATA");
742 if ( event
.m_item
.m_mask
& wxLIST_SET_ITEM
)
743 msg
<< _T(" wxLIST_SET_ITEM");
744 if ( event
.m_item
.m_mask
& wxLIST_MASK_WIDTH
)
745 msg
<< _T(" wxLIST_MASK_WIDTH");
746 if ( event
.m_item
.m_mask
& wxLIST_MASK_FORMAT
)
747 msg
<< _T(" wxLIST_MASK_WIDTH");
749 if ( event
.m_item
.m_mask
& wxLIST_MASK_TEXT
)
751 event
.m_item
.m_text
= _T("My callback text");
757 void MyListCtrl::OnSetInfo(wxListEvent
& event
)
759 LogEvent(event
, _T("OnSetInfo"));
762 void MyListCtrl::OnSelected(wxListEvent
& event
)
764 LogEvent(event
, _T("OnSelected"));
766 if ( GetWindowStyle() & wxLC_REPORT
)
769 info
.m_itemId
= event
.m_itemIndex
;
771 info
.m_mask
= wxLIST_MASK_TEXT
;
774 wxLogMessage(wxT("Value of the 2nd field of the selected item: %s"),
775 info
.m_text
.c_str());
779 wxFAIL_MSG(wxT("wxListCtrl::GetItem() failed"));
784 void MyListCtrl::OnDeselected(wxListEvent
& event
)
786 LogEvent(event
, _T("OnDeselected"));
789 void MyListCtrl::OnActivated(wxListEvent
& event
)
791 LogEvent(event
, _T("OnActivated"));
794 void MyListCtrl::OnFocused(wxListEvent
& event
)
796 LogEvent(event
, _T("OnFocused"));
801 void MyListCtrl::OnListKeyDown(wxListEvent
& event
)
803 switch ( event
.GetKeyCode() )
805 case 'c': // colorize
809 info
.m_itemId
= event
.GetIndex();
812 wxListItemAttr
*attr
= info
.GetAttributes();
813 if ( !attr
|| !attr
->HasTextColour() )
815 info
.SetTextColour(*wxCYAN
);
819 RefreshItem(info
.m_itemId
);
827 long item
= GetNextItem(-1,
828 wxLIST_NEXT_ALL
, wxLIST_STATE_FOCUSED
);
829 if ( item
++ == GetItemCount() - 1 )
834 wxLogMessage(_T("Focusing item %ld"), item
);
836 SetItemState(item
, wxLIST_STATE_FOCUSED
, wxLIST_STATE_FOCUSED
);
843 long item
= GetNextItem(-1,
844 wxLIST_NEXT_ALL
, wxLIST_STATE_SELECTED
);
849 wxLogMessage(_T("Item %ld deleted"), item
);
851 // -1 because the indices were shifted by DeleteItem()
852 item
= GetNextItem(item
- 1,
853 wxLIST_NEXT_ALL
, wxLIST_STATE_SELECTED
);
859 if ( GetWindowStyle() & wxLC_REPORT
)
861 if ( GetWindowStyle() & wxLC_VIRTUAL
)
863 SetItemCount(GetItemCount() + 1);
867 InsertItemInReportView(event
.GetIndex());
873 LogEvent(event
, _T("OnListKeyDown"));
879 void MyListCtrl::OnChar(wxKeyEvent
& event
)
881 wxLogMessage(_T("Got char event."));
883 switch ( event
.GetKeyCode() )
889 // these are the keys we process ourselves
897 void MyListCtrl::LogEvent(const wxListEvent
& event
, const wxChar
*eventName
)
899 wxLogMessage(_T("Item %ld: %s (item text = %s, data = %ld)"),
900 event
.GetIndex(), eventName
,
901 event
.GetText().c_str(), event
.GetData());
904 wxString
MyListCtrl::OnGetItemText(long item
, long column
) const
906 return wxString::Format(_T("Column %ld of item %ld"), column
, item
);
909 int MyListCtrl::OnGetItemImage(long item
) const
914 wxListItemAttr
*MyListCtrl::OnGetItemAttr(long item
) const
916 return item
% 2 ? NULL
: (wxListItemAttr
*)&m_attr
;
919 void MyListCtrl::InsertItemInReportView(int i
)
922 buf
.Printf(_T("This is item %d"), i
);
923 long tmp
= InsertItem(i
, buf
, 0);
926 buf
.Printf(_T("Col 1, item %d"), i
);
929 buf
.Printf(_T("Item %d in column 2"), i
);