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"
28 #if !defined(__WXMSW__) && !defined(__WXPM__)
29 #include "mondrian.xpm"
33 #include "bitmaps/toolbrai.xpm"
34 #include "bitmaps/toolchar.xpm"
35 #include "bitmaps/tooldata.xpm"
36 #include "bitmaps/toolnote.xpm"
37 #include "bitmaps/tooltodo.xpm"
38 #include "bitmaps/toolchec.xpm"
39 #include "bitmaps/toolgame.xpm"
40 #include "bitmaps/tooltime.xpm"
41 #include "bitmaps/toolword.xpm"
42 #include "bitmaps/small1.xpm"
45 #include "wx/imaglist.h"
46 #include "wx/listctrl.h"
47 #include "wx/timer.h" // for wxStopWatch
48 #include "wx/colordlg.h" // for wxGetColourFromUser
52 const wxChar
*SMALL_VIRTUAL_VIEW_ITEMS
[][2] =
54 { _T("Cat"), _T("meow") },
55 { _T("Cow"), _T("moo") },
56 { _T("Crow"), _T("caw") },
57 { _T("Dog"), _T("woof") },
58 { _T("Duck"), _T("quack") },
59 { _T("Mouse"), _T("squeak") },
60 { _T("Owl"), _T("hoo") },
61 { _T("Pig"), _T("oink") },
62 { _T("Pigeon"), _T("coo") },
63 { _T("Sheep"), _T("baaah") },
67 BEGIN_EVENT_TABLE(MyFrame
, wxFrame
)
68 EVT_SIZE(MyFrame::OnSize
)
70 EVT_MENU(LIST_QUIT
, MyFrame::OnQuit
)
71 EVT_MENU(LIST_ABOUT
, MyFrame::OnAbout
)
72 EVT_MENU(LIST_LIST_VIEW
, MyFrame::OnListView
)
73 EVT_MENU(LIST_REPORT_VIEW
, MyFrame::OnReportView
)
74 EVT_MENU(LIST_ICON_VIEW
, MyFrame::OnIconView
)
75 EVT_MENU(LIST_ICON_TEXT_VIEW
, MyFrame::OnIconTextView
)
76 EVT_MENU(LIST_SMALL_ICON_VIEW
, MyFrame::OnSmallIconView
)
77 EVT_MENU(LIST_SMALL_ICON_TEXT_VIEW
, MyFrame::OnSmallIconTextView
)
78 EVT_MENU(LIST_VIRTUAL_VIEW
, MyFrame::OnVirtualView
)
79 EVT_MENU(LIST_SMALL_VIRTUAL_VIEW
, MyFrame::OnSmallVirtualView
)
81 EVT_MENU(LIST_FOCUS_LAST
, MyFrame::OnFocusLast
)
82 EVT_MENU(LIST_TOGGLE_FIRST
, MyFrame::OnToggleFirstSel
)
83 EVT_MENU(LIST_DESELECT_ALL
, MyFrame::OnDeselectAll
)
84 EVT_MENU(LIST_SELECT_ALL
, MyFrame::OnSelectAll
)
85 EVT_MENU(LIST_DELETE
, MyFrame::OnDelete
)
86 EVT_MENU(LIST_ADD
, MyFrame::OnAdd
)
87 EVT_MENU(LIST_EDIT
, MyFrame::OnEdit
)
88 EVT_MENU(LIST_DELETE_ALL
, MyFrame::OnDeleteAll
)
89 EVT_MENU(LIST_SORT
, MyFrame::OnSort
)
90 EVT_MENU(LIST_SET_FG_COL
, MyFrame::OnSetFgColour
)
91 EVT_MENU(LIST_SET_BG_COL
, MyFrame::OnSetBgColour
)
92 EVT_MENU(LIST_TOGGLE_MULTI_SEL
, MyFrame::OnToggleMultiSel
)
93 EVT_MENU(LIST_SHOW_COL_INFO
, MyFrame::OnShowColInfo
)
94 EVT_MENU(LIST_SHOW_SEL_INFO
, MyFrame::OnShowSelInfo
)
95 EVT_MENU(LIST_FREEZE
, MyFrame::OnFreeze
)
96 EVT_MENU(LIST_THAW
, MyFrame::OnThaw
)
97 EVT_MENU(LIST_TOGGLE_LINES
, MyFrame::OnToggleLines
)
99 EVT_UPDATE_UI(LIST_SHOW_COL_INFO
, MyFrame::OnUpdateShowColInfo
)
100 EVT_UPDATE_UI(LIST_TOGGLE_MULTI_SEL
, MyFrame::OnUpdateToggleMultiSel
)
103 BEGIN_EVENT_TABLE(MyListCtrl
, wxListCtrl
)
104 EVT_LIST_BEGIN_DRAG(LIST_CTRL
, MyListCtrl::OnBeginDrag
)
105 EVT_LIST_BEGIN_RDRAG(LIST_CTRL
, MyListCtrl::OnBeginRDrag
)
106 EVT_LIST_BEGIN_LABEL_EDIT(LIST_CTRL
, MyListCtrl::OnBeginLabelEdit
)
107 EVT_LIST_END_LABEL_EDIT(LIST_CTRL
, MyListCtrl::OnEndLabelEdit
)
108 EVT_LIST_DELETE_ITEM(LIST_CTRL
, MyListCtrl::OnDeleteItem
)
109 EVT_LIST_DELETE_ALL_ITEMS(LIST_CTRL
, MyListCtrl::OnDeleteAllItems
)
110 #if WXWIN_COMPATIBILITY_2_4
111 EVT_LIST_GET_INFO(LIST_CTRL
, MyListCtrl::OnGetInfo
)
112 EVT_LIST_SET_INFO(LIST_CTRL
, MyListCtrl::OnSetInfo
)
114 EVT_LIST_ITEM_SELECTED(LIST_CTRL
, MyListCtrl::OnSelected
)
115 EVT_LIST_ITEM_DESELECTED(LIST_CTRL
, MyListCtrl::OnDeselected
)
116 EVT_LIST_KEY_DOWN(LIST_CTRL
, MyListCtrl::OnListKeyDown
)
117 EVT_LIST_ITEM_ACTIVATED(LIST_CTRL
, MyListCtrl::OnActivated
)
118 EVT_LIST_ITEM_FOCUSED(LIST_CTRL
, MyListCtrl::OnFocused
)
120 EVT_LIST_COL_CLICK(LIST_CTRL
, MyListCtrl::OnColClick
)
121 EVT_LIST_COL_RIGHT_CLICK(LIST_CTRL
, MyListCtrl::OnColRightClick
)
122 EVT_LIST_COL_BEGIN_DRAG(LIST_CTRL
, MyListCtrl::OnColBeginDrag
)
123 EVT_LIST_COL_DRAGGING(LIST_CTRL
, MyListCtrl::OnColDragging
)
124 EVT_LIST_COL_END_DRAG(LIST_CTRL
, MyListCtrl::OnColEndDrag
)
126 EVT_LIST_CACHE_HINT(LIST_CTRL
, MyListCtrl::OnCacheHint
)
128 EVT_CHAR(MyListCtrl::OnChar
)
133 // number of items in list/report view
134 static const int NUM_ITEMS
= 30;
136 // number of items in icon/small icon view
137 static const int NUM_ICONS
= 9;
139 int wxCALLBACK
MyCompareFunction(long item1
, long item2
, long WXUNUSED(sortData
))
150 // `Main program' equivalent, creating windows and returning main app frame
153 // Create the main frame window
154 MyFrame
*frame
= new MyFrame(wxT("wxListCtrl Test"), 50, 50, 450, 340);
164 // My frame constructor
165 MyFrame::MyFrame(const wxChar
*title
, int x
, int y
, int w
, int h
)
166 : wxFrame(NULL
, wxID_ANY
, title
, wxPoint(x
, y
), wxSize(w
, h
))
170 m_smallVirtual
= false;
173 SetIcon( wxICON(mondrian
) );
175 // Make an image list containing large icons
176 m_imageListNormal
= new wxImageList(32, 32, true);
177 m_imageListSmall
= new wxImageList(16, 16, true);
180 m_imageListNormal
->Add( wxIcon(_T("icon1"), wxBITMAP_TYPE_ICO_RESOURCE
) );
181 m_imageListNormal
->Add( wxIcon(_T("icon2"), wxBITMAP_TYPE_ICO_RESOURCE
) );
182 m_imageListNormal
->Add( wxIcon(_T("icon3"), wxBITMAP_TYPE_ICO_RESOURCE
) );
183 m_imageListNormal
->Add( wxIcon(_T("icon4"), wxBITMAP_TYPE_ICO_RESOURCE
) );
184 m_imageListNormal
->Add( wxIcon(_T("icon5"), wxBITMAP_TYPE_ICO_RESOURCE
) );
185 m_imageListNormal
->Add( wxIcon(_T("icon6"), wxBITMAP_TYPE_ICO_RESOURCE
) );
186 m_imageListNormal
->Add( wxIcon(_T("icon7"), wxBITMAP_TYPE_ICO_RESOURCE
) );
187 m_imageListNormal
->Add( wxIcon(_T("icon8"), wxBITMAP_TYPE_ICO_RESOURCE
) );
188 m_imageListNormal
->Add( wxIcon(_T("icon9"), wxBITMAP_TYPE_ICO_RESOURCE
) );
190 m_imageListSmall
->Add( wxIcon(_T("iconsmall"), wxBITMAP_TYPE_ICO_RESOURCE
) );
193 m_imageListNormal
->Add( wxIcon( toolbrai_xpm
) );
194 m_imageListNormal
->Add( wxIcon( toolchar_xpm
) );
195 m_imageListNormal
->Add( wxIcon( tooldata_xpm
) );
196 m_imageListNormal
->Add( wxIcon( toolnote_xpm
) );
197 m_imageListNormal
->Add( wxIcon( tooltodo_xpm
) );
198 m_imageListNormal
->Add( wxIcon( toolchec_xpm
) );
199 m_imageListNormal
->Add( wxIcon( toolgame_xpm
) );
200 m_imageListNormal
->Add( wxIcon( tooltime_xpm
) );
201 m_imageListNormal
->Add( wxIcon( toolword_xpm
) );
203 m_imageListSmall
->Add( wxIcon( small1_xpm
) );
207 wxMenu
*menuFile
= new wxMenu
;
208 menuFile
->Append(LIST_ABOUT
, _T("&About"));
209 menuFile
->AppendSeparator();
210 menuFile
->Append(LIST_QUIT
, _T("E&xit\tAlt-X"));
212 wxMenu
*menuView
= new wxMenu
;
213 menuView
->Append(LIST_LIST_VIEW
, _T("&List view\tF1"));
214 menuView
->Append(LIST_REPORT_VIEW
, _T("&Report view\tF2"));
215 menuView
->Append(LIST_ICON_VIEW
, _T("&Icon view\tF3"));
216 menuView
->Append(LIST_ICON_TEXT_VIEW
, _T("Icon view with &text\tF4"));
217 menuView
->Append(LIST_SMALL_ICON_VIEW
, _T("&Small icon view\tF5"));
218 menuView
->Append(LIST_SMALL_ICON_TEXT_VIEW
, _T("Small icon &view with text\tF6"));
219 menuView
->Append(LIST_VIRTUAL_VIEW
, _T("&Virtual view\tF7"));
220 menuView
->Append(LIST_SMALL_VIRTUAL_VIEW
, _T("Small virtual vie&w\tF8"));
222 wxMenu
*menuList
= new wxMenu
;
223 menuList
->Append(LIST_FOCUS_LAST
, _T("&Make last item current\tCtrl-L"));
224 menuList
->Append(LIST_TOGGLE_FIRST
, _T("To&ggle first item\tCtrl-G"));
225 menuList
->Append(LIST_DESELECT_ALL
, _T("&Deselect All\tCtrl-D"));
226 menuList
->Append(LIST_SELECT_ALL
, _T("S&elect All\tCtrl-A"));
227 menuList
->AppendSeparator();
228 menuList
->Append(LIST_SHOW_COL_INFO
, _T("Show &column info\tCtrl-C"));
229 menuList
->Append(LIST_SHOW_SEL_INFO
, _T("Show &selected items\tCtrl-S"));
230 menuList
->AppendSeparator();
231 menuList
->Append(LIST_SORT
, _T("&Sort\tCtrl-S"));
232 menuList
->AppendSeparator();
233 menuList
->Append(LIST_ADD
, _T("&Append an item\tCtrl-P"));
234 menuList
->Append(LIST_EDIT
, _T("&Edit the item\tCtrl-E"));
235 menuList
->Append(LIST_DELETE
, _T("&Delete first item\tCtrl-X"));
236 menuList
->Append(LIST_DELETE_ALL
, _T("Delete &all items"));
237 menuList
->AppendSeparator();
238 menuList
->Append(LIST_FREEZE
, _T("Free&ze\tCtrl-Z"));
239 menuList
->Append(LIST_THAW
, _T("Tha&w\tCtrl-W"));
240 menuList
->AppendSeparator();
241 menuList
->AppendCheckItem(LIST_TOGGLE_LINES
, _T("Toggle &lines\tCtrl-I"));
242 menuList
->Append(LIST_TOGGLE_MULTI_SEL
, _T("&Multiple selection\tCtrl-M"),
243 _T("Toggle multiple selection"), true);
245 wxMenu
*menuCol
= new wxMenu
;
246 menuCol
->Append(LIST_SET_FG_COL
, _T("&Foreground colour..."));
247 menuCol
->Append(LIST_SET_BG_COL
, _T("&Background colour..."));
249 wxMenuBar
*menubar
= new wxMenuBar
;
250 menubar
->Append(menuFile
, _T("&File"));
251 menubar
->Append(menuView
, _T("&View"));
252 menubar
->Append(menuList
, _T("&List"));
253 menubar
->Append(menuCol
, _T("&Colour"));
256 m_panel
= new wxPanel(this, wxID_ANY
);
257 m_logWindow
= new wxTextCtrl(m_panel
, wxID_ANY
, wxEmptyString
,
258 wxDefaultPosition
, wxDefaultSize
,
259 wxTE_MULTILINE
| wxSUNKEN_BORDER
);
261 m_logOld
= wxLog::SetActiveTarget(new wxLogTextCtrl(m_logWindow
));
263 RecreateList(wxLC_REPORT
| wxLC_SINGLE_SEL
);
267 #endif // wxUSE_STATUSBAR
272 delete wxLog::SetActiveTarget(m_logOld
);
274 delete m_imageListNormal
;
275 delete m_imageListSmall
;
278 void MyFrame::OnSize(wxSizeEvent
& event
)
285 void MyFrame::DoSize()
290 wxSize size
= GetClientSize();
291 wxCoord y
= (2*size
.y
)/3;
292 m_listCtrl
->SetSize(0, 0, size
.x
, y
);
293 m_logWindow
->SetSize(0, y
+ 1, size
.x
, size
.y
- y
);
296 bool MyFrame::CheckNonVirtual() const
298 if ( !m_listCtrl
->HasFlag(wxLC_VIRTUAL
) )
301 // "this" == whatever
302 wxLogWarning(_T("Can't do this in virtual view, sorry."));
307 void MyFrame::OnQuit(wxCommandEvent
& WXUNUSED(event
))
312 void MyFrame::OnAbout(wxCommandEvent
& WXUNUSED(event
))
314 wxMessageDialog
dialog(this, _T("List test sample\nJulian Smart (c) 1997"),
315 _T("About list test"), wxOK
|wxCANCEL
);
320 void MyFrame::OnFreeze(wxCommandEvent
& WXUNUSED(event
))
322 wxLogMessage(_T("Freezing the control"));
324 m_listCtrl
->Freeze();
327 void MyFrame::OnThaw(wxCommandEvent
& WXUNUSED(event
))
329 wxLogMessage(_T("Thawing the control"));
334 void MyFrame::OnToggleLines(wxCommandEvent
& event
)
336 m_listCtrl
->SetSingleStyle(wxLC_HRULES
| wxLC_VRULES
, event
.IsChecked());
339 void MyFrame::OnFocusLast(wxCommandEvent
& WXUNUSED(event
))
341 long index
= m_listCtrl
->GetItemCount() - 1;
347 m_listCtrl
->SetItemState(index
, wxLIST_STATE_FOCUSED
, wxLIST_STATE_FOCUSED
);
348 m_listCtrl
->EnsureVisible(index
);
351 void MyFrame::OnToggleFirstSel(wxCommandEvent
& WXUNUSED(event
))
353 m_listCtrl
->SetItemState(0, (~m_listCtrl
->GetItemState(0, wxLIST_STATE_SELECTED
) ) & wxLIST_STATE_SELECTED
, wxLIST_STATE_SELECTED
);
356 void MyFrame::OnDeselectAll(wxCommandEvent
& WXUNUSED(event
))
358 if ( !CheckNonVirtual() )
361 int n
= m_listCtrl
->GetItemCount();
362 for (int i
= 0; i
< n
; i
++)
363 m_listCtrl
->SetItemState(i
,0,wxLIST_STATE_SELECTED
);
366 void MyFrame::OnSelectAll(wxCommandEvent
& WXUNUSED(event
))
368 if ( !CheckNonVirtual() )
371 int n
= m_listCtrl
->GetItemCount();
372 for (int i
= 0; i
< n
; i
++)
373 m_listCtrl
->SetItemState(i
,wxLIST_STATE_SELECTED
, wxLIST_STATE_SELECTED
);
376 // ----------------------------------------------------------------------------
377 // changing listctrl modes
378 // ----------------------------------------------------------------------------
380 void MyFrame::RecreateList(long flags
, bool withText
)
382 // we could avoid recreating it if we don't set/clear the wxLC_VIRTUAL
383 // style, but it is more trouble to do it than not
385 if ( !m_listCtrl
|| ((flags
& wxLC_VIRTUAL
) !=
386 (m_listCtrl
->GetWindowStyleFlag() & wxLC_VIRTUAL
)) )
391 m_listCtrl
= new MyListCtrl(m_panel
, LIST_CTRL
,
392 wxDefaultPosition
, wxDefaultSize
,
394 wxSUNKEN_BORDER
| wxLC_EDIT_LABELS
);
396 switch ( flags
& wxLC_MASK_TYPE
)
403 InitWithIconItems(withText
);
406 case wxLC_SMALL_ICON
:
407 InitWithIconItems(withText
, true);
411 if ( flags
& wxLC_VIRTUAL
)
412 InitWithVirtualItems();
414 InitWithReportItems();
418 wxFAIL_MSG( _T("unknown listctrl mode") );
424 m_logWindow
->Clear();
427 void MyFrame::OnListView(wxCommandEvent
& WXUNUSED(event
))
429 RecreateList(wxLC_LIST
);
432 void MyFrame::InitWithListItems()
434 for ( int i
= 0; i
< NUM_ITEMS
; i
++ )
436 m_listCtrl
->InsertItem(i
, wxString::Format(_T("Item %d"), i
));
440 void MyFrame::OnReportView(wxCommandEvent
& WXUNUSED(event
))
442 RecreateList(wxLC_REPORT
);
445 void MyFrame::InitWithReportItems()
447 m_listCtrl
->SetImageList(m_imageListSmall
, wxIMAGE_LIST_SMALL
);
449 // note that under MSW for SetColumnWidth() to work we need to create the
450 // items with images initially even if we specify dummy image id
452 itemCol
.SetText(_T("Column 1"));
453 itemCol
.SetImage(-1);
454 m_listCtrl
->InsertColumn(0, itemCol
);
456 itemCol
.SetText(_T("Column 2"));
457 itemCol
.SetAlign(wxLIST_FORMAT_CENTRE
);
458 m_listCtrl
->InsertColumn(1, itemCol
);
460 itemCol
.SetText(_T("Column 3"));
461 itemCol
.SetAlign(wxLIST_FORMAT_RIGHT
);
462 m_listCtrl
->InsertColumn(2, itemCol
);
464 // to speed up inserting we hide the control temporarily
469 for ( int i
= 0; i
< NUM_ITEMS
; i
++ )
471 m_listCtrl
->InsertItemInReportView(i
);
474 m_logWindow
->WriteText(wxString::Format(_T("%d items inserted in %ldms\n"),
475 NUM_ITEMS
, sw
.Time()));
478 // we leave all mask fields to 0 and only change the colour
481 item
.SetTextColour(*wxRED
);
482 m_listCtrl
->SetItem( item
);
485 item
.SetTextColour(*wxGREEN
);
486 m_listCtrl
->SetItem( item
);
488 item
.SetTextColour(*wxLIGHT_GREY
);
489 item
.SetFont(*wxITALIC_FONT
);
490 item
.SetBackgroundColour(*wxRED
);
491 m_listCtrl
->SetItem( item
);
493 m_listCtrl
->SetTextColour(*wxBLUE
);
494 m_listCtrl
->SetBackgroundColour(*wxLIGHT_GREY
);
496 m_listCtrl
->SetColumnWidth( 0, wxLIST_AUTOSIZE
);
497 m_listCtrl
->SetColumnWidth( 1, wxLIST_AUTOSIZE
);
498 m_listCtrl
->SetColumnWidth( 2, wxLIST_AUTOSIZE
);
500 // test SetItemFont too
501 m_listCtrl
->SetItemFont(0, *wxITALIC_FONT
);
504 void MyFrame::InitWithIconItems(bool withText
, bool sameIcon
)
506 m_listCtrl
->SetImageList(m_imageListNormal
, wxIMAGE_LIST_NORMAL
);
507 m_listCtrl
->SetImageList(m_imageListSmall
, wxIMAGE_LIST_SMALL
);
509 for ( int i
= 0; i
< NUM_ICONS
; i
++ )
511 int image
= sameIcon
? 0 : i
;
515 m_listCtrl
->InsertItem(i
, wxString::Format(_T("Label %d"), i
),
520 m_listCtrl
->InsertItem(i
, image
);
525 void MyFrame::OnIconView(wxCommandEvent
& WXUNUSED(event
))
527 RecreateList(wxLC_ICON
, false);
530 void MyFrame::OnIconTextView(wxCommandEvent
& WXUNUSED(event
))
532 RecreateList(wxLC_ICON
);
535 void MyFrame::OnSmallIconView(wxCommandEvent
& WXUNUSED(event
))
537 RecreateList(wxLC_SMALL_ICON
, false);
540 void MyFrame::OnSmallIconTextView(wxCommandEvent
& WXUNUSED(event
))
542 RecreateList(wxLC_SMALL_ICON
);
545 void MyFrame::OnVirtualView(wxCommandEvent
& WXUNUSED(event
))
547 m_smallVirtual
= false;
548 RecreateList(wxLC_REPORT
| wxLC_VIRTUAL
);
551 void MyFrame::OnSmallVirtualView(wxCommandEvent
& WXUNUSED(event
))
553 m_smallVirtual
= true;
554 RecreateList(wxLC_REPORT
| wxLC_VIRTUAL
);
557 void MyFrame::InitWithVirtualItems()
559 m_listCtrl
->SetImageList(m_imageListSmall
, wxIMAGE_LIST_SMALL
);
561 if ( m_smallVirtual
)
563 m_listCtrl
->InsertColumn(0, _T("Animal"));
564 m_listCtrl
->InsertColumn(1, _T("Sound"));
565 m_listCtrl
->SetItemCount(WXSIZEOF(SMALL_VIRTUAL_VIEW_ITEMS
));
569 m_listCtrl
->InsertColumn(0, _T("First Column"));
570 m_listCtrl
->InsertColumn(1, _T("Second Column"));
571 m_listCtrl
->SetColumnWidth(0, 150);
572 m_listCtrl
->SetColumnWidth(1, 150);
573 m_listCtrl
->SetItemCount(1000000);
577 void MyFrame::OnSort(wxCommandEvent
& WXUNUSED(event
))
581 m_listCtrl
->SortItems(MyCompareFunction
, 0);
583 m_logWindow
->WriteText(wxString::Format(_T("Sorting %d items took %ld ms\n"),
584 m_listCtrl
->GetItemCount(),
588 void MyFrame::OnShowSelInfo(wxCommandEvent
& WXUNUSED(event
))
590 int selCount
= m_listCtrl
->GetSelectedItemCount();
591 wxLogMessage(_T("%d items selected:"), selCount
);
593 // don't show too many items
594 size_t shownCount
= 0;
596 long item
= m_listCtrl
->GetNextItem(-1, wxLIST_NEXT_ALL
,
597 wxLIST_STATE_SELECTED
);
600 wxLogMessage(_T("\t%ld (%s)"),
601 item
, m_listCtrl
->GetItemText(item
).c_str());
603 if ( ++shownCount
> 10 )
605 wxLogMessage(_T("\t... more selected items snipped..."));
609 item
= m_listCtrl
->GetNextItem(item
, wxLIST_NEXT_ALL
,
610 wxLIST_STATE_SELECTED
);
614 void MyFrame::OnShowColInfo(wxCommandEvent
& WXUNUSED(event
))
616 int count
= m_listCtrl
->GetColumnCount();
617 wxLogMessage(wxT("%d columns:"), count
);
618 for ( int c
= 0; c
< count
; c
++ )
620 wxLogMessage(wxT("\tcolumn %d has width %d"), c
,
621 m_listCtrl
->GetColumnWidth(c
));
625 void MyFrame::OnUpdateShowColInfo(wxUpdateUIEvent
& event
)
627 event
.Enable( (m_listCtrl
->GetWindowStyleFlag() & wxLC_REPORT
) != 0 );
630 void MyFrame::OnToggleMultiSel(wxCommandEvent
& WXUNUSED(event
))
632 long flags
= m_listCtrl
->GetWindowStyleFlag();
633 if ( flags
& wxLC_SINGLE_SEL
)
634 flags
&= ~wxLC_SINGLE_SEL
;
636 flags
|= wxLC_SINGLE_SEL
;
638 m_logWindow
->WriteText(wxString::Format(wxT("Current selection mode: %sle\n"),
639 (flags
& wxLC_SINGLE_SEL
) ? _T("sing") : _T("multip")));
644 void MyFrame::OnUpdateToggleMultiSel(wxUpdateUIEvent
& event
)
646 event
.Check((m_listCtrl
->GetWindowStyleFlag() & wxLC_SINGLE_SEL
) == 0);
649 void MyFrame::OnSetFgColour(wxCommandEvent
& WXUNUSED(event
))
651 m_listCtrl
->SetForegroundColour(wxGetColourFromUser(this));
652 m_listCtrl
->Refresh();
655 void MyFrame::OnSetBgColour(wxCommandEvent
& WXUNUSED(event
))
657 m_listCtrl
->SetBackgroundColour(wxGetColourFromUser(this));
658 m_listCtrl
->Refresh();
661 void MyFrame::OnAdd(wxCommandEvent
& WXUNUSED(event
))
663 m_listCtrl
->InsertItem(m_listCtrl
->GetItemCount(), _T("Appended item"));
666 void MyFrame::OnEdit(wxCommandEvent
& WXUNUSED(event
))
668 long itemCur
= m_listCtrl
->GetNextItem(-1, wxLIST_NEXT_ALL
,
669 wxLIST_STATE_FOCUSED
);
673 m_listCtrl
->EditLabel(itemCur
);
677 m_logWindow
->WriteText(_T("No item to edit"));
681 void MyFrame::OnDelete(wxCommandEvent
& WXUNUSED(event
))
683 if ( m_listCtrl
->GetItemCount() )
685 m_listCtrl
->DeleteItem(0);
689 m_logWindow
->WriteText(_T("Nothing to delete"));
693 void MyFrame::OnDeleteAll(wxCommandEvent
& WXUNUSED(event
))
697 size_t itemCount
= m_listCtrl
->GetItemCount();
699 m_listCtrl
->DeleteAllItems();
701 m_logWindow
->WriteText(wxString::Format(_T("Deleting %d items took %ld ms\n"),
708 void MyListCtrl::OnCacheHint(wxListEvent
& event
)
710 wxLogMessage( wxT("OnCacheHint: cache items %ld..%ld"),
711 event
.GetCacheFrom(), event
.GetCacheTo() );
714 void MyListCtrl::SetColumnImage(int col
, int image
)
717 item
.SetMask(wxLIST_MASK_IMAGE
);
718 item
.SetImage(image
);
719 SetColumn(col
, item
);
722 void MyListCtrl::OnColClick(wxListEvent
& event
)
724 int col
= event
.GetColumn();
726 // set or unset image
727 static bool x
= false;
729 SetColumnImage(col
, x
? 0 : -1);
731 wxLogMessage( wxT("OnColumnClick at %d."), col
);
734 void MyListCtrl::OnColRightClick(wxListEvent
& event
)
736 int col
= event
.GetColumn();
739 SetColumnImage(col
, -1);
742 // Show popupmenu at position
743 wxMenu
menu(wxT("Test"));
744 menu
.Append(LIST_ABOUT
, _T("&About"));
745 PopupMenu(&menu
, event
.GetPoint());
747 wxLogMessage( wxT("OnColumnRightClick at %d."), event
.GetColumn() );
750 void MyListCtrl::LogColEvent(const wxListEvent
& event
, const wxChar
*name
)
752 const int col
= event
.GetColumn();
754 wxLogMessage(wxT("%s: column %d (width = %d or %d)."),
757 event
.GetItem().GetWidth(),
758 GetColumnWidth(col
));
761 void MyListCtrl::OnColBeginDrag(wxListEvent
& event
)
763 LogColEvent( event
, wxT("OnColBeginDrag") );
765 if ( event
.GetColumn() == 0 )
767 wxLogMessage(_T("Resizing this column shouldn't work."));
773 void MyListCtrl::OnColDragging(wxListEvent
& event
)
775 LogColEvent( event
, wxT("OnColDragging") );
778 void MyListCtrl::OnColEndDrag(wxListEvent
& event
)
780 LogColEvent( event
, wxT("OnColEndDrag") );
783 void MyListCtrl::OnBeginDrag(wxListEvent
& event
)
785 const wxPoint
& pt
= event
.m_pointDrag
;
788 wxLogMessage( wxT("OnBeginDrag at (%d, %d), item %ld."),
789 pt
.x
, pt
.y
, HitTest(pt
, flags
) );
792 void MyListCtrl::OnBeginRDrag(wxListEvent
& event
)
794 wxLogMessage( wxT("OnBeginRDrag at %d,%d."),
795 event
.m_pointDrag
.x
, event
.m_pointDrag
.y
);
798 void MyListCtrl::OnBeginLabelEdit(wxListEvent
& event
)
800 wxLogMessage( wxT("OnBeginLabelEdit: %s"), event
.m_item
.m_text
.c_str());
803 void MyListCtrl::OnEndLabelEdit(wxListEvent
& event
)
805 wxLogMessage( wxT("OnEndLabelEdit: %s"),
806 event
.IsEditCancelled() ? _T("[cancelled]")
807 : event
.m_item
.m_text
.c_str());
810 void MyListCtrl::OnDeleteItem(wxListEvent
& event
)
812 LogEvent(event
, _T("OnDeleteItem"));
815 void MyListCtrl::OnDeleteAllItems(wxListEvent
& event
)
817 LogEvent(event
, _T("OnDeleteAllItems"));
820 #if WXWIN_COMPATIBILITY_2_4
821 void MyListCtrl::OnGetInfo(wxListEvent
& event
)
825 msg
<< _T("OnGetInfo (") << event
.m_item
.m_itemId
<< _T(", ") << event
.m_item
.m_col
<< _T(")");
826 if ( event
.m_item
.m_mask
& wxLIST_MASK_STATE
)
827 msg
<< _T(" wxLIST_MASK_STATE");
828 if ( event
.m_item
.m_mask
& wxLIST_MASK_TEXT
)
829 msg
<< _T(" wxLIST_MASK_TEXT");
830 if ( event
.m_item
.m_mask
& wxLIST_MASK_IMAGE
)
831 msg
<< _T(" wxLIST_MASK_IMAGE");
832 if ( event
.m_item
.m_mask
& wxLIST_MASK_DATA
)
833 msg
<< _T(" wxLIST_MASK_DATA");
834 if ( event
.m_item
.m_mask
& wxLIST_SET_ITEM
)
835 msg
<< _T(" wxLIST_SET_ITEM");
836 if ( event
.m_item
.m_mask
& wxLIST_MASK_WIDTH
)
837 msg
<< _T(" wxLIST_MASK_WIDTH");
838 if ( event
.m_item
.m_mask
& wxLIST_MASK_FORMAT
)
839 msg
<< _T(" wxLIST_MASK_WIDTH");
841 if ( event
.m_item
.m_mask
& wxLIST_MASK_TEXT
)
843 event
.m_item
.m_text
= _T("My callback text");
849 void MyListCtrl::OnSetInfo(wxListEvent
& event
)
851 LogEvent(event
, _T("OnSetInfo"));
855 void MyListCtrl::OnSelected(wxListEvent
& event
)
857 LogEvent(event
, _T("OnSelected"));
859 if ( GetWindowStyle() & wxLC_REPORT
)
862 info
.m_itemId
= event
.m_itemIndex
;
864 info
.m_mask
= wxLIST_MASK_TEXT
;
867 wxLogMessage(wxT("Value of the 2nd field of the selected item: %s"),
868 info
.m_text
.c_str());
872 wxFAIL_MSG(wxT("wxListCtrl::GetItem() failed"));
877 void MyListCtrl::OnDeselected(wxListEvent
& event
)
879 LogEvent(event
, _T("OnDeselected"));
882 void MyListCtrl::OnActivated(wxListEvent
& event
)
884 LogEvent(event
, _T("OnActivated"));
887 void MyListCtrl::OnFocused(wxListEvent
& event
)
889 LogEvent(event
, _T("OnFocused"));
894 void MyListCtrl::OnListKeyDown(wxListEvent
& event
)
898 switch ( event
.GetKeyCode() )
900 case 'c': // colorize
904 info
.m_itemId
= event
.GetIndex();
907 wxListItemAttr
*attr
= info
.GetAttributes();
908 if ( !attr
|| !attr
->HasTextColour() )
910 info
.SetTextColour(*wxCYAN
);
914 RefreshItem(info
.m_itemId
);
921 item
= GetNextItem(-1, wxLIST_NEXT_ALL
, wxLIST_STATE_FOCUSED
);
922 if ( item
++ == GetItemCount() - 1 )
927 wxLogMessage(_T("Focusing item %ld"), item
);
929 SetItemState(item
, wxLIST_STATE_FOCUSED
, wxLIST_STATE_FOCUSED
);
933 case 'r': // show bounding Rect
936 item
= event
.GetIndex();
938 if ( !GetItemRect(item
, r
) )
940 wxLogError(_T("Failed to retrieve rect of item %ld"), item
);
944 wxLogMessage(_T("Bounding rect of item %ld is (%d, %d)-(%d, %d)"),
945 item
, r
.x
, r
.y
, r
.x
+ r
.width
, r
.y
+ r
.height
);
950 item
= GetNextItem(-1, wxLIST_NEXT_ALL
, wxLIST_STATE_SELECTED
);
955 wxLogMessage(_T("Item %ld deleted"), item
);
957 // -1 because the indices were shifted by DeleteItem()
958 item
= GetNextItem(item
- 1,
959 wxLIST_NEXT_ALL
, wxLIST_STATE_SELECTED
);
964 if ( GetWindowStyle() & wxLC_REPORT
)
966 if ( GetWindowStyle() & wxLC_VIRTUAL
)
968 SetItemCount(GetItemCount() + 1);
972 InsertItemInReportView(event
.GetIndex());
978 LogEvent(event
, _T("OnListKeyDown"));
984 void MyListCtrl::OnChar(wxKeyEvent
& event
)
986 wxLogMessage(_T("Got char event."));
988 switch ( event
.GetKeyCode() )
994 // these are the keys we process ourselves
1002 void MyListCtrl::LogEvent(const wxListEvent
& event
, const wxChar
*eventName
)
1004 wxLogMessage(_T("Item %ld: %s (item text = %s, data = %ld)"),
1005 event
.GetIndex(), eventName
,
1006 event
.GetText().c_str(), event
.GetData());
1009 wxString
MyListCtrl::OnGetItemText(long item
, long column
) const
1011 if ( GetItemCount() == WXSIZEOF(SMALL_VIRTUAL_VIEW_ITEMS
) )
1013 return SMALL_VIRTUAL_VIEW_ITEMS
[item
][column
];
1017 return wxString::Format(_T("Column %ld of item %ld"), column
, item
);
1021 int MyListCtrl::OnGetItemImage(long WXUNUSED(item
)) const
1026 wxListItemAttr
*MyListCtrl::OnGetItemAttr(long item
) const
1028 return item
% 2 ? NULL
: (wxListItemAttr
*)&m_attr
;
1031 void MyListCtrl::InsertItemInReportView(int i
)
1034 buf
.Printf(_T("This is item %d"), i
);
1035 long tmp
= InsertItem(i
, buf
, 0);
1036 SetItemData(tmp
, i
);
1038 buf
.Printf(_T("Col 1, item %d"), i
);
1041 buf
.Printf(_T("Item %d in column 2"), i
);