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
)
81 EVT_UPDATE_UI(LIST_TOGGLE_MULTI_SEL
, MyFrame::OnUpdateToggleMultiSel
)
84 BEGIN_EVENT_TABLE(MyListCtrl
, wxListCtrl
)
85 EVT_LIST_BEGIN_DRAG(LIST_CTRL
, MyListCtrl::OnBeginDrag
)
86 EVT_LIST_BEGIN_RDRAG(LIST_CTRL
, MyListCtrl::OnBeginRDrag
)
87 EVT_LIST_BEGIN_LABEL_EDIT(LIST_CTRL
, MyListCtrl::OnBeginLabelEdit
)
88 EVT_LIST_END_LABEL_EDIT(LIST_CTRL
, MyListCtrl::OnEndLabelEdit
)
89 EVT_LIST_DELETE_ITEM(LIST_CTRL
, MyListCtrl::OnDeleteItem
)
90 EVT_LIST_DELETE_ALL_ITEMS(LIST_CTRL
, MyListCtrl::OnDeleteAllItems
)
91 EVT_LIST_GET_INFO(LIST_CTRL
, MyListCtrl::OnGetInfo
)
92 EVT_LIST_SET_INFO(LIST_CTRL
, MyListCtrl::OnSetInfo
)
93 EVT_LIST_ITEM_SELECTED(LIST_CTRL
, MyListCtrl::OnSelected
)
94 EVT_LIST_ITEM_DESELECTED(LIST_CTRL
, MyListCtrl::OnDeselected
)
95 EVT_LIST_KEY_DOWN(LIST_CTRL
, MyListCtrl::OnListKeyDown
)
96 EVT_LIST_ITEM_ACTIVATED(LIST_CTRL
, MyListCtrl::OnActivated
)
97 EVT_LIST_ITEM_FOCUSED(LIST_CTRL
, MyListCtrl::OnFocused
)
99 EVT_LIST_COL_CLICK(LIST_CTRL
, MyListCtrl::OnColClick
)
100 EVT_LIST_COL_RIGHT_CLICK(LIST_CTRL
, MyListCtrl::OnColRightClick
)
101 EVT_LIST_COL_BEGIN_DRAG(LIST_CTRL
, MyListCtrl::OnColBeginDrag
)
102 EVT_LIST_COL_DRAGGING(LIST_CTRL
, MyListCtrl::OnColDragging
)
103 EVT_LIST_COL_END_DRAG(LIST_CTRL
, MyListCtrl::OnColEndDrag
)
105 EVT_LIST_CACHE_HINT(LIST_CTRL
, MyListCtrl::OnCacheHint
)
107 EVT_CHAR(MyListCtrl::OnChar
)
112 // number of items in list/report view
113 static const int NUM_ITEMS
= 30;
115 // number of items in icon/small icon view
116 static const int NUM_ICONS
= 9;
118 int wxCALLBACK
MyCompareFunction(long item1
, long item2
, long WXUNUSED(sortData
))
129 // `Main program' equivalent, creating windows and returning main app frame
132 // Create the main frame window
133 MyFrame
*frame
= new MyFrame(wxT("wxListCtrl Test"), 50, 50, 450, 340);
143 // My frame constructor
144 MyFrame::MyFrame(const wxChar
*title
, int x
, int y
, int w
, int h
)
145 : wxFrame((wxFrame
*)NULL
, wxID_ANY
, title
, wxPoint(x
, y
), wxSize(w
, h
))
147 m_listCtrl
= (MyListCtrl
*) NULL
;
148 m_logWindow
= (wxTextCtrl
*) NULL
;
151 SetIcon( wxICON(mondrian
) );
153 // Make an image list containing large icons
154 m_imageListNormal
= new wxImageList(32, 32, true);
155 m_imageListSmall
= new wxImageList(16, 16, true);
158 m_imageListNormal
->Add( wxIcon(_T("icon1"), wxBITMAP_TYPE_ICO_RESOURCE
) );
159 m_imageListNormal
->Add( wxIcon(_T("icon2"), wxBITMAP_TYPE_ICO_RESOURCE
) );
160 m_imageListNormal
->Add( wxIcon(_T("icon3"), wxBITMAP_TYPE_ICO_RESOURCE
) );
161 m_imageListNormal
->Add( wxIcon(_T("icon4"), wxBITMAP_TYPE_ICO_RESOURCE
) );
162 m_imageListNormal
->Add( wxIcon(_T("icon5"), wxBITMAP_TYPE_ICO_RESOURCE
) );
163 m_imageListNormal
->Add( wxIcon(_T("icon6"), wxBITMAP_TYPE_ICO_RESOURCE
) );
164 m_imageListNormal
->Add( wxIcon(_T("icon7"), wxBITMAP_TYPE_ICO_RESOURCE
) );
165 m_imageListNormal
->Add( wxIcon(_T("icon8"), wxBITMAP_TYPE_ICO_RESOURCE
) );
166 m_imageListNormal
->Add( wxIcon(_T("icon9"), wxBITMAP_TYPE_ICO_RESOURCE
) );
168 m_imageListSmall
->Add( wxIcon(_T("iconsmall"), wxBITMAP_TYPE_ICO_RESOURCE
) );
171 m_imageListNormal
->Add( wxIcon( toolbrai_xpm
) );
172 m_imageListNormal
->Add( wxIcon( toolchar_xpm
) );
173 m_imageListNormal
->Add( wxIcon( tooldata_xpm
) );
174 m_imageListNormal
->Add( wxIcon( toolnote_xpm
) );
175 m_imageListNormal
->Add( wxIcon( tooltodo_xpm
) );
176 m_imageListNormal
->Add( wxIcon( toolchec_xpm
) );
177 m_imageListNormal
->Add( wxIcon( toolgame_xpm
) );
178 m_imageListNormal
->Add( wxIcon( tooltime_xpm
) );
179 m_imageListNormal
->Add( wxIcon( toolword_xpm
) );
181 m_imageListSmall
->Add( wxIcon( small1_xpm
) );
185 wxMenu
*menuFile
= new wxMenu
;
186 menuFile
->Append(LIST_ABOUT
, _T("&About"));
187 menuFile
->AppendSeparator();
188 menuFile
->Append(LIST_QUIT
, _T("E&xit\tAlt-X"));
190 wxMenu
*menuView
= new wxMenu
;
191 menuView
->Append(LIST_LIST_VIEW
, _T("&List view\tF1"));
192 menuView
->Append(LIST_REPORT_VIEW
, _T("&Report view\tF2"));
193 menuView
->Append(LIST_ICON_VIEW
, _T("&Icon view\tF3"));
194 menuView
->Append(LIST_ICON_TEXT_VIEW
, _T("Icon view with &text\tF4"));
195 menuView
->Append(LIST_SMALL_ICON_VIEW
, _T("&Small icon view\tF5"));
196 menuView
->Append(LIST_SMALL_ICON_TEXT_VIEW
, _T("Small icon &view with text\tF6"));
197 menuView
->Append(LIST_VIRTUAL_VIEW
, _T("Virtual view\tF7"));
199 wxMenu
*menuList
= new wxMenu
;
200 menuList
->Append(LIST_FOCUS_LAST
, _T("&Make last item current\tCtrl-L"));
201 menuList
->Append(LIST_TOGGLE_FIRST
, _T("To&ggle first item\tCtrl-G"));
202 menuList
->Append(LIST_DESELECT_ALL
, _T("&Deselect All\tCtrl-D"));
203 menuList
->Append(LIST_SELECT_ALL
, _T("S&elect All\tCtrl-A"));
204 menuList
->AppendSeparator();
205 menuList
->Append(LIST_SHOW_COL_INFO
, _T("Show &column info\tCtrl-C"));
206 menuList
->Append(LIST_SHOW_SEL_INFO
, _T("Show &selected items\tCtrl-S"));
207 menuList
->AppendSeparator();
208 menuList
->Append(LIST_SORT
, _T("&Sort\tCtrl-S"));
209 menuList
->AppendSeparator();
210 menuList
->Append(LIST_ADD
, _T("&Append an item\tCtrl-P"));
211 menuList
->Append(LIST_EDIT
, _T("&Edit the item\tCtrl-E"));
212 menuList
->Append(LIST_DELETE
, _T("&Delete first item\tCtrl-X"));
213 menuList
->Append(LIST_DELETE_ALL
, _T("Delete &all items"));
214 menuList
->AppendSeparator();
215 menuList
->Append(LIST_FREEZE
, _T("Free&ze\tCtrl-Z"));
216 menuList
->Append(LIST_THAW
, _T("Tha&w\tCtrl-W"));
217 menuList
->AppendSeparator();
218 menuList
->Append(LIST_TOGGLE_MULTI_SEL
, _T("&Multiple selection\tCtrl-M"),
219 _T("Toggle multiple selection"), true);
221 wxMenu
*menuCol
= new wxMenu
;
222 menuCol
->Append(LIST_SET_FG_COL
, _T("&Foreground colour..."));
223 menuCol
->Append(LIST_SET_BG_COL
, _T("&Background colour..."));
225 wxMenuBar
*menubar
= new wxMenuBar
;
226 menubar
->Append(menuFile
, _T("&File"));
227 menubar
->Append(menuView
, _T("&View"));
228 menubar
->Append(menuList
, _T("&List"));
229 menubar
->Append(menuCol
, _T("&Colour"));
232 m_panel
= new wxPanel(this, wxID_ANY
);
233 m_logWindow
= new wxTextCtrl(m_panel
, wxID_ANY
, wxEmptyString
,
234 wxDefaultPosition
, wxDefaultSize
,
235 wxTE_MULTILINE
| wxSUNKEN_BORDER
);
237 m_logOld
= wxLog::SetActiveTarget(new wxLogTextCtrl(m_logWindow
));
239 RecreateList(wxLC_REPORT
| wxLC_SINGLE_SEL
);
243 #endif // wxUSE_STATUSBAR
248 delete wxLog::SetActiveTarget(m_logOld
);
250 delete m_imageListNormal
;
251 delete m_imageListSmall
;
254 void MyFrame::OnSize(wxSizeEvent
& event
)
261 void MyFrame::DoSize()
266 wxSize size
= GetClientSize();
267 wxCoord y
= (2*size
.y
)/3;
268 m_listCtrl
->SetSize(0, 0, size
.x
, y
);
269 m_logWindow
->SetSize(0, y
+ 1, size
.x
, size
.y
- y
);
272 bool MyFrame::CheckNonVirtual() const
274 if ( !m_listCtrl
->HasFlag(wxLC_VIRTUAL
) )
277 // "this" == whatever
278 wxLogWarning(_T("Can't do this in virtual view, sorry."));
283 void MyFrame::OnQuit(wxCommandEvent
& WXUNUSED(event
))
288 void MyFrame::OnAbout(wxCommandEvent
& WXUNUSED(event
))
290 wxMessageDialog
dialog(this, _T("List test sample\nJulian Smart (c) 1997"),
291 _T("About list test"), wxOK
|wxCANCEL
);
296 void MyFrame::OnFreeze(wxCommandEvent
& WXUNUSED(event
))
298 wxLogMessage(_T("Freezing the control"));
300 m_listCtrl
->Freeze();
303 void MyFrame::OnThaw(wxCommandEvent
& WXUNUSED(event
))
305 wxLogMessage(_T("Thawing the control"));
310 void MyFrame::OnFocusLast(wxCommandEvent
& WXUNUSED(event
))
312 long index
= m_listCtrl
->GetItemCount() - 1;
318 m_listCtrl
->SetItemState(index
, wxLIST_STATE_FOCUSED
, wxLIST_STATE_FOCUSED
);
319 m_listCtrl
->EnsureVisible(index
);
322 void MyFrame::OnToggleFirstSel(wxCommandEvent
& WXUNUSED(event
))
324 m_listCtrl
->SetItemState(0, (~m_listCtrl
->GetItemState(0, wxLIST_STATE_SELECTED
) ) & wxLIST_STATE_SELECTED
, wxLIST_STATE_SELECTED
);
327 void MyFrame::OnDeselectAll(wxCommandEvent
& WXUNUSED(event
))
329 if ( !CheckNonVirtual() )
332 int n
= m_listCtrl
->GetItemCount();
333 for (int i
= 0; i
< n
; i
++)
334 m_listCtrl
->SetItemState(i
,0,wxLIST_STATE_SELECTED
);
337 void MyFrame::OnSelectAll(wxCommandEvent
& WXUNUSED(event
))
339 if ( !CheckNonVirtual() )
342 int n
= m_listCtrl
->GetItemCount();
343 for (int i
= 0; i
< n
; i
++)
344 m_listCtrl
->SetItemState(i
,wxLIST_STATE_SELECTED
, wxLIST_STATE_SELECTED
);
347 // ----------------------------------------------------------------------------
348 // changing listctrl modes
349 // ----------------------------------------------------------------------------
351 void MyFrame::RecreateList(long flags
, bool withText
)
353 // we could avoid recreating it if we don't set/clear the wxLC_VIRTUAL
354 // style, but it is more trouble to do it than not
356 if ( !m_listCtrl
|| ((flags
& wxLC_VIRTUAL
) !=
357 (m_listCtrl
->GetWindowStyleFlag() & wxLC_VIRTUAL
)) )
362 m_listCtrl
= new MyListCtrl(m_panel
, LIST_CTRL
,
363 wxDefaultPosition
, wxDefaultSize
,
365 wxSUNKEN_BORDER
| wxLC_EDIT_LABELS
);
367 switch ( flags
& wxLC_MASK_TYPE
)
374 InitWithIconItems(withText
);
377 case wxLC_SMALL_ICON
:
378 InitWithIconItems(withText
, true);
382 if ( flags
& wxLC_VIRTUAL
)
383 InitWithVirtualItems();
385 InitWithReportItems();
389 wxFAIL_MSG( _T("unknown listctrl mode") );
395 m_logWindow
->Clear();
398 void MyFrame::OnListView(wxCommandEvent
& WXUNUSED(event
))
400 RecreateList(wxLC_LIST
);
403 void MyFrame::InitWithListItems()
405 for ( int i
= 0; i
< NUM_ITEMS
; i
++ )
407 m_listCtrl
->InsertItem(i
, wxString::Format(_T("Item %d"), i
));
411 void MyFrame::OnReportView(wxCommandEvent
& WXUNUSED(event
))
413 RecreateList(wxLC_REPORT
);
416 void MyFrame::InitWithReportItems()
418 m_listCtrl
->SetImageList(m_imageListSmall
, wxIMAGE_LIST_SMALL
);
420 // note that under MSW for SetColumnWidth() to work we need to create the
421 // items with images initially even if we specify dummy image id
423 itemCol
.SetText(_T("Column 1"));
424 itemCol
.SetImage(-1);
425 m_listCtrl
->InsertColumn(0, itemCol
);
427 itemCol
.SetText(_T("Column 2"));
428 itemCol
.SetAlign(wxLIST_FORMAT_CENTRE
);
429 m_listCtrl
->InsertColumn(1, itemCol
);
431 itemCol
.SetText(_T("Column 3"));
432 itemCol
.SetAlign(wxLIST_FORMAT_RIGHT
);
433 m_listCtrl
->InsertColumn(2, itemCol
);
435 // to speed up inserting we hide the control temporarily
440 for ( int i
= 0; i
< NUM_ITEMS
; i
++ )
442 m_listCtrl
->InsertItemInReportView(i
);
445 m_logWindow
->WriteText(wxString::Format(_T("%d items inserted in %ldms\n"),
446 NUM_ITEMS
, sw
.Time()));
449 // we leave all mask fields to 0 and only change the colour
452 item
.SetTextColour(*wxRED
);
453 m_listCtrl
->SetItem( item
);
456 item
.SetTextColour(*wxGREEN
);
457 m_listCtrl
->SetItem( item
);
459 item
.SetTextColour(*wxLIGHT_GREY
);
460 item
.SetFont(*wxITALIC_FONT
);
461 item
.SetBackgroundColour(*wxRED
);
462 m_listCtrl
->SetItem( item
);
464 m_listCtrl
->SetTextColour(*wxBLUE
);
465 m_listCtrl
->SetBackgroundColour(*wxLIGHT_GREY
);
467 m_listCtrl
->SetColumnWidth( 0, wxLIST_AUTOSIZE
);
468 m_listCtrl
->SetColumnWidth( 1, wxLIST_AUTOSIZE
);
469 m_listCtrl
->SetColumnWidth( 2, wxLIST_AUTOSIZE
);
472 void MyFrame::InitWithIconItems(bool withText
, bool sameIcon
)
474 m_listCtrl
->SetImageList(m_imageListNormal
, wxIMAGE_LIST_NORMAL
);
475 m_listCtrl
->SetImageList(m_imageListSmall
, wxIMAGE_LIST_SMALL
);
477 for ( int i
= 0; i
< NUM_ICONS
; i
++ )
479 int image
= sameIcon
? 0 : i
;
483 m_listCtrl
->InsertItem(i
, wxString::Format(_T("Label %d"), i
),
488 m_listCtrl
->InsertItem(i
, image
);
493 void MyFrame::OnIconView(wxCommandEvent
& WXUNUSED(event
))
495 RecreateList(wxLC_ICON
, false);
498 void MyFrame::OnIconTextView(wxCommandEvent
& WXUNUSED(event
))
500 RecreateList(wxLC_ICON
);
503 void MyFrame::OnSmallIconView(wxCommandEvent
& WXUNUSED(event
))
505 RecreateList(wxLC_SMALL_ICON
, false);
508 void MyFrame::OnSmallIconTextView(wxCommandEvent
& WXUNUSED(event
))
510 RecreateList(wxLC_SMALL_ICON
);
513 void MyFrame::OnVirtualView(wxCommandEvent
& WXUNUSED(event
))
515 RecreateList(wxLC_REPORT
| wxLC_VIRTUAL
);
518 void MyFrame::InitWithVirtualItems()
520 m_listCtrl
->SetImageList(m_imageListSmall
, wxIMAGE_LIST_SMALL
);
522 m_listCtrl
->InsertColumn(0, _T("First Column"));
523 m_listCtrl
->InsertColumn(1, _T("Second Column"));
524 m_listCtrl
->SetColumnWidth(0, 150);
525 m_listCtrl
->SetColumnWidth(1, 150);
527 m_listCtrl
->SetItemCount(1000000);
530 void MyFrame::OnSort(wxCommandEvent
& WXUNUSED(event
))
534 m_listCtrl
->SortItems(MyCompareFunction
, 0);
536 m_logWindow
->WriteText(wxString::Format(_T("Sorting %d items took %ld ms\n"),
537 m_listCtrl
->GetItemCount(),
541 void MyFrame::OnShowSelInfo(wxCommandEvent
& WXUNUSED(event
))
543 int selCount
= m_listCtrl
->GetSelectedItemCount();
544 wxLogMessage(_T("%d items selected:"), selCount
);
546 // don't show too many items
547 size_t shownCount
= 0;
549 long item
= m_listCtrl
->GetNextItem(-1, wxLIST_NEXT_ALL
,
550 wxLIST_STATE_SELECTED
);
553 wxLogMessage(_T("\t%ld (%s)"),
554 item
, m_listCtrl
->GetItemText(item
).c_str());
556 if ( ++shownCount
> 10 )
558 wxLogMessage(_T("\t... more selected items snipped..."));
562 item
= m_listCtrl
->GetNextItem(item
, wxLIST_NEXT_ALL
,
563 wxLIST_STATE_SELECTED
);
567 void MyFrame::OnShowColInfo(wxCommandEvent
& WXUNUSED(event
))
569 int count
= m_listCtrl
->GetColumnCount();
570 wxLogMessage(wxT("%d columns:"), count
);
571 for ( int c
= 0; c
< count
; c
++ )
573 wxLogMessage(wxT("\tcolumn %d has width %d"), c
,
574 m_listCtrl
->GetColumnWidth(c
));
578 void MyFrame::OnUpdateShowColInfo(wxUpdateUIEvent
& event
)
580 event
.Enable( (m_listCtrl
->GetWindowStyleFlag() & wxLC_REPORT
) != 0 );
583 void MyFrame::OnToggleMultiSel(wxCommandEvent
& WXUNUSED(event
))
585 long flags
= m_listCtrl
->GetWindowStyleFlag();
586 if ( flags
& wxLC_SINGLE_SEL
)
587 flags
&= ~wxLC_SINGLE_SEL
;
589 flags
|= wxLC_SINGLE_SEL
;
591 m_logWindow
->WriteText(wxString::Format(wxT("Current selection mode: %sle\n"),
592 (flags
& wxLC_SINGLE_SEL
) ? _T("sing") : _T("multip")));
597 void MyFrame::OnUpdateToggleMultiSel(wxUpdateUIEvent
& event
)
599 event
.Check((m_listCtrl
->GetWindowStyleFlag() & wxLC_SINGLE_SEL
) == 0);
602 void MyFrame::OnSetFgColour(wxCommandEvent
& WXUNUSED(event
))
604 m_listCtrl
->SetForegroundColour(wxGetColourFromUser(this));
605 m_listCtrl
->Refresh();
608 void MyFrame::OnSetBgColour(wxCommandEvent
& WXUNUSED(event
))
610 m_listCtrl
->SetBackgroundColour(wxGetColourFromUser(this));
611 m_listCtrl
->Refresh();
614 void MyFrame::OnAdd(wxCommandEvent
& WXUNUSED(event
))
616 m_listCtrl
->InsertItem(m_listCtrl
->GetItemCount(), _T("Appended item"));
619 void MyFrame::OnEdit(wxCommandEvent
& WXUNUSED(event
))
621 long itemCur
= m_listCtrl
->GetNextItem(-1, wxLIST_NEXT_ALL
,
622 wxLIST_STATE_FOCUSED
);
626 m_listCtrl
->EditLabel(itemCur
);
630 m_logWindow
->WriteText(_T("No item to edit"));
634 void MyFrame::OnDelete(wxCommandEvent
& WXUNUSED(event
))
636 if ( m_listCtrl
->GetItemCount() )
638 m_listCtrl
->DeleteItem(0);
642 m_logWindow
->WriteText(_T("Nothing to delete"));
646 void MyFrame::OnDeleteAll(wxCommandEvent
& WXUNUSED(event
))
650 m_listCtrl
->DeleteAllItems();
652 m_logWindow
->WriteText(wxString::Format(_T("Deleting %d items took %ld ms\n"),
653 m_listCtrl
->GetItemCount(),
659 void MyListCtrl::OnCacheHint(wxListEvent
& event
)
661 wxLogMessage( wxT("OnCacheHint: cache items %ld..%ld"),
662 event
.GetCacheFrom(), event
.GetCacheTo() );
665 void MyListCtrl::SetColumnImage(int col
, int image
)
668 item
.SetMask(wxLIST_MASK_IMAGE
);
669 item
.SetImage(image
);
670 SetColumn(col
, item
);
673 void MyListCtrl::OnColClick(wxListEvent
& event
)
675 int col
= event
.GetColumn();
676 SetColumnImage(col
, 0);
678 wxLogMessage( wxT("OnColumnClick at %d."), col
);
681 void MyListCtrl::OnColRightClick(wxListEvent
& event
)
683 int col
= event
.GetColumn();
686 SetColumnImage(col
, -1);
689 wxLogMessage( wxT("OnColumnRightClick at %d."), event
.GetColumn() );
692 void MyListCtrl::LogColEvent(const wxListEvent
& event
, const wxChar
*name
)
694 const int col
= event
.GetColumn();
696 wxLogMessage(wxT("%s: column %d (width = %d or %d)."),
699 event
.GetItem().GetWidth(),
700 GetColumnWidth(col
));
703 void MyListCtrl::OnColBeginDrag(wxListEvent
& event
)
705 LogColEvent( event
, wxT("OnColBeginDrag") );
707 if ( event
.GetColumn() == 0 )
709 wxLogMessage(_T("Resizing this column shouldn't work."));
715 void MyListCtrl::OnColDragging(wxListEvent
& event
)
717 LogColEvent( event
, wxT("OnColDragging") );
720 void MyListCtrl::OnColEndDrag(wxListEvent
& event
)
722 LogColEvent( event
, wxT("OnColEndDrag") );
725 void MyListCtrl::OnBeginDrag(wxListEvent
& event
)
727 const wxPoint
& pt
= event
.m_pointDrag
;
730 wxLogMessage( wxT("OnBeginDrag at (%d, %d), item %ld."),
731 pt
.x
, pt
.y
, HitTest(pt
, flags
) );
734 void MyListCtrl::OnBeginRDrag(wxListEvent
& event
)
736 wxLogMessage( wxT("OnBeginRDrag at %d,%d."),
737 event
.m_pointDrag
.x
, event
.m_pointDrag
.y
);
740 void MyListCtrl::OnBeginLabelEdit(wxListEvent
& event
)
742 wxLogMessage( wxT("OnBeginLabelEdit: %s"), event
.m_item
.m_text
.c_str());
745 void MyListCtrl::OnEndLabelEdit(wxListEvent
& event
)
747 wxLogMessage( wxT("OnEndLabelEdit: %s"),
748 event
.IsEditCancelled() ? _T("[cancelled]")
749 : event
.m_item
.m_text
.c_str());
752 void MyListCtrl::OnDeleteItem(wxListEvent
& event
)
754 LogEvent(event
, _T("OnDeleteItem"));
757 void MyListCtrl::OnDeleteAllItems(wxListEvent
& event
)
759 LogEvent(event
, _T("OnDeleteAllItems"));
762 void MyListCtrl::OnGetInfo(wxListEvent
& event
)
766 msg
<< _T("OnGetInfo (") << event
.m_item
.m_itemId
<< _T(", ") << event
.m_item
.m_col
<< _T(")");
767 if ( event
.m_item
.m_mask
& wxLIST_MASK_STATE
)
768 msg
<< _T(" wxLIST_MASK_STATE");
769 if ( event
.m_item
.m_mask
& wxLIST_MASK_TEXT
)
770 msg
<< _T(" wxLIST_MASK_TEXT");
771 if ( event
.m_item
.m_mask
& wxLIST_MASK_IMAGE
)
772 msg
<< _T(" wxLIST_MASK_IMAGE");
773 if ( event
.m_item
.m_mask
& wxLIST_MASK_DATA
)
774 msg
<< _T(" wxLIST_MASK_DATA");
775 if ( event
.m_item
.m_mask
& wxLIST_SET_ITEM
)
776 msg
<< _T(" wxLIST_SET_ITEM");
777 if ( event
.m_item
.m_mask
& wxLIST_MASK_WIDTH
)
778 msg
<< _T(" wxLIST_MASK_WIDTH");
779 if ( event
.m_item
.m_mask
& wxLIST_MASK_FORMAT
)
780 msg
<< _T(" wxLIST_MASK_WIDTH");
782 if ( event
.m_item
.m_mask
& wxLIST_MASK_TEXT
)
784 event
.m_item
.m_text
= _T("My callback text");
790 void MyListCtrl::OnSetInfo(wxListEvent
& event
)
792 LogEvent(event
, _T("OnSetInfo"));
795 void MyListCtrl::OnSelected(wxListEvent
& event
)
797 LogEvent(event
, _T("OnSelected"));
799 if ( GetWindowStyle() & wxLC_REPORT
)
802 info
.m_itemId
= event
.m_itemIndex
;
804 info
.m_mask
= wxLIST_MASK_TEXT
;
807 wxLogMessage(wxT("Value of the 2nd field of the selected item: %s"),
808 info
.m_text
.c_str());
812 wxFAIL_MSG(wxT("wxListCtrl::GetItem() failed"));
817 void MyListCtrl::OnDeselected(wxListEvent
& event
)
819 LogEvent(event
, _T("OnDeselected"));
822 void MyListCtrl::OnActivated(wxListEvent
& event
)
824 LogEvent(event
, _T("OnActivated"));
827 void MyListCtrl::OnFocused(wxListEvent
& event
)
829 LogEvent(event
, _T("OnFocused"));
834 void MyListCtrl::OnListKeyDown(wxListEvent
& event
)
836 switch ( event
.GetKeyCode() )
838 case 'c': // colorize
842 info
.m_itemId
= event
.GetIndex();
845 wxListItemAttr
*attr
= info
.GetAttributes();
846 if ( !attr
|| !attr
->HasTextColour() )
848 info
.SetTextColour(*wxCYAN
);
852 RefreshItem(info
.m_itemId
);
860 long item
= GetNextItem(-1,
861 wxLIST_NEXT_ALL
, wxLIST_STATE_FOCUSED
);
862 if ( item
++ == GetItemCount() - 1 )
867 wxLogMessage(_T("Focusing item %ld"), item
);
869 SetItemState(item
, wxLIST_STATE_FOCUSED
, wxLIST_STATE_FOCUSED
);
876 long item
= GetNextItem(-1,
877 wxLIST_NEXT_ALL
, wxLIST_STATE_SELECTED
);
882 wxLogMessage(_T("Item %ld deleted"), item
);
884 // -1 because the indices were shifted by DeleteItem()
885 item
= GetNextItem(item
- 1,
886 wxLIST_NEXT_ALL
, wxLIST_STATE_SELECTED
);
892 if ( GetWindowStyle() & wxLC_REPORT
)
894 if ( GetWindowStyle() & wxLC_VIRTUAL
)
896 SetItemCount(GetItemCount() + 1);
900 InsertItemInReportView(event
.GetIndex());
906 LogEvent(event
, _T("OnListKeyDown"));
912 void MyListCtrl::OnChar(wxKeyEvent
& event
)
914 wxLogMessage(_T("Got char event."));
916 switch ( event
.GetKeyCode() )
922 // these are the keys we process ourselves
930 void MyListCtrl::LogEvent(const wxListEvent
& event
, const wxChar
*eventName
)
932 wxLogMessage(_T("Item %ld: %s (item text = %s, data = %ld)"),
933 event
.GetIndex(), eventName
,
934 event
.GetText().c_str(), event
.GetData());
937 wxString
MyListCtrl::OnGetItemText(long item
, long column
) const
939 return wxString::Format(_T("Column %ld of item %ld"), column
, item
);
942 int MyListCtrl::OnGetItemImage(long WXUNUSED(item
)) const
947 wxListItemAttr
*MyListCtrl::OnGetItemAttr(long item
) const
949 return item
% 2 ? NULL
: (wxListItemAttr
*)&m_attr
;
952 void MyListCtrl::InsertItemInReportView(int i
)
955 buf
.Printf(_T("This is item %d"), i
);
956 long tmp
= InsertItem(i
, buf
, 0);
959 buf
.Printf(_T("Col 1, item %d"), i
);
962 buf
.Printf(_T("Item %d in column 2"), i
);