1 /////////////////////////////////////////////////////////////////////////////
3 // Purpose: wxListCtrl sample
4 // Author: Julian Smart
8 // Copyright: (c) Julian Smart
9 // Licence: wxWindows license
10 /////////////////////////////////////////////////////////////////////////////
12 // For compilers that support precompilation, includes "wx/wx.h".
13 #include "wx/wxprec.h"
23 #if !defined(__WXMSW__) && !defined(__WXPM__)
24 #include "mondrian.xpm"
28 #include "bitmaps/toolbrai.xpm"
29 #include "bitmaps/toolchar.xpm"
30 #include "bitmaps/tooldata.xpm"
31 #include "bitmaps/toolnote.xpm"
32 #include "bitmaps/tooltodo.xpm"
33 #include "bitmaps/toolchec.xpm"
34 #include "bitmaps/toolgame.xpm"
35 #include "bitmaps/tooltime.xpm"
36 #include "bitmaps/toolword.xpm"
37 #include "bitmaps/small1.xpm"
40 #include "wx/imaglist.h"
41 #include "wx/listctrl.h"
42 #include "wx/timer.h" // for wxStopWatch
43 #include "wx/colordlg.h" // for wxGetColourFromUser
47 const wxChar
*SMALL_VIRTUAL_VIEW_ITEMS
[][2] =
49 { _T("Cat"), _T("meow") },
50 { _T("Cow"), _T("moo") },
51 { _T("Crow"), _T("caw") },
52 { _T("Dog"), _T("woof") },
53 { _T("Duck"), _T("quack") },
54 { _T("Mouse"), _T("squeak") },
55 { _T("Owl"), _T("hoo") },
56 { _T("Pig"), _T("oink") },
57 { _T("Pigeon"), _T("coo") },
58 { _T("Sheep"), _T("baaah") },
62 BEGIN_EVENT_TABLE(MyFrame
, wxFrame
)
63 EVT_SIZE(MyFrame::OnSize
)
65 EVT_MENU(LIST_QUIT
, MyFrame::OnQuit
)
66 EVT_MENU(LIST_ABOUT
, MyFrame::OnAbout
)
67 EVT_MENU(LIST_LIST_VIEW
, MyFrame::OnListView
)
68 EVT_MENU(LIST_REPORT_VIEW
, MyFrame::OnReportView
)
69 EVT_MENU(LIST_ICON_VIEW
, MyFrame::OnIconView
)
70 EVT_MENU(LIST_ICON_TEXT_VIEW
, MyFrame::OnIconTextView
)
71 EVT_MENU(LIST_SMALL_ICON_VIEW
, MyFrame::OnSmallIconView
)
72 EVT_MENU(LIST_SMALL_ICON_TEXT_VIEW
, MyFrame::OnSmallIconTextView
)
73 EVT_MENU(LIST_VIRTUAL_VIEW
, MyFrame::OnVirtualView
)
74 EVT_MENU(LIST_SMALL_VIRTUAL_VIEW
, MyFrame::OnSmallVirtualView
)
76 EVT_MENU(LIST_FOCUS_LAST
, MyFrame::OnFocusLast
)
77 EVT_MENU(LIST_TOGGLE_FIRST
, MyFrame::OnToggleFirstSel
)
78 EVT_MENU(LIST_DESELECT_ALL
, MyFrame::OnDeselectAll
)
79 EVT_MENU(LIST_SELECT_ALL
, MyFrame::OnSelectAll
)
80 EVT_MENU(LIST_DELETE
, MyFrame::OnDelete
)
81 EVT_MENU(LIST_ADD
, MyFrame::OnAdd
)
82 EVT_MENU(LIST_EDIT
, MyFrame::OnEdit
)
83 EVT_MENU(LIST_DELETE_ALL
, MyFrame::OnDeleteAll
)
84 EVT_MENU(LIST_SORT
, MyFrame::OnSort
)
85 EVT_MENU(LIST_SET_FG_COL
, MyFrame::OnSetFgColour
)
86 EVT_MENU(LIST_SET_BG_COL
, MyFrame::OnSetBgColour
)
87 EVT_MENU(LIST_TOGGLE_MULTI_SEL
, MyFrame::OnToggleMultiSel
)
88 EVT_MENU(LIST_SHOW_COL_INFO
, MyFrame::OnShowColInfo
)
89 EVT_MENU(LIST_SHOW_SEL_INFO
, MyFrame::OnShowSelInfo
)
90 EVT_MENU(LIST_FREEZE
, MyFrame::OnFreeze
)
91 EVT_MENU(LIST_THAW
, MyFrame::OnThaw
)
92 EVT_MENU(LIST_TOGGLE_LINES
, MyFrame::OnToggleLines
)
94 EVT_UPDATE_UI(LIST_SHOW_COL_INFO
, MyFrame::OnUpdateShowColInfo
)
95 EVT_UPDATE_UI(LIST_TOGGLE_MULTI_SEL
, MyFrame::OnUpdateToggleMultiSel
)
98 BEGIN_EVENT_TABLE(MyListCtrl
, wxListCtrl
)
99 EVT_LIST_BEGIN_DRAG(LIST_CTRL
, MyListCtrl::OnBeginDrag
)
100 EVT_LIST_BEGIN_RDRAG(LIST_CTRL
, MyListCtrl::OnBeginRDrag
)
101 EVT_LIST_BEGIN_LABEL_EDIT(LIST_CTRL
, MyListCtrl::OnBeginLabelEdit
)
102 EVT_LIST_END_LABEL_EDIT(LIST_CTRL
, MyListCtrl::OnEndLabelEdit
)
103 EVT_LIST_DELETE_ITEM(LIST_CTRL
, MyListCtrl::OnDeleteItem
)
104 EVT_LIST_DELETE_ALL_ITEMS(LIST_CTRL
, MyListCtrl::OnDeleteAllItems
)
105 #if WXWIN_COMPATIBILITY_2_4
106 EVT_LIST_GET_INFO(LIST_CTRL
, MyListCtrl::OnGetInfo
)
107 EVT_LIST_SET_INFO(LIST_CTRL
, MyListCtrl::OnSetInfo
)
109 EVT_LIST_ITEM_SELECTED(LIST_CTRL
, MyListCtrl::OnSelected
)
110 EVT_LIST_ITEM_DESELECTED(LIST_CTRL
, MyListCtrl::OnDeselected
)
111 EVT_LIST_KEY_DOWN(LIST_CTRL
, MyListCtrl::OnListKeyDown
)
112 EVT_LIST_ITEM_ACTIVATED(LIST_CTRL
, MyListCtrl::OnActivated
)
113 EVT_LIST_ITEM_FOCUSED(LIST_CTRL
, MyListCtrl::OnFocused
)
115 EVT_LIST_COL_CLICK(LIST_CTRL
, MyListCtrl::OnColClick
)
116 EVT_LIST_COL_RIGHT_CLICK(LIST_CTRL
, MyListCtrl::OnColRightClick
)
117 EVT_LIST_COL_BEGIN_DRAG(LIST_CTRL
, MyListCtrl::OnColBeginDrag
)
118 EVT_LIST_COL_DRAGGING(LIST_CTRL
, MyListCtrl::OnColDragging
)
119 EVT_LIST_COL_END_DRAG(LIST_CTRL
, MyListCtrl::OnColEndDrag
)
121 EVT_LIST_CACHE_HINT(LIST_CTRL
, MyListCtrl::OnCacheHint
)
123 EVT_CHAR(MyListCtrl::OnChar
)
128 // number of items in list/report view
129 static const int NUM_ITEMS
= 30;
131 // number of items in icon/small icon view
132 static const int NUM_ICONS
= 9;
134 int wxCALLBACK
MyCompareFunction(long item1
, long item2
, long WXUNUSED(sortData
))
145 // `Main program' equivalent, creating windows and returning main app frame
148 // Create the main frame window
149 MyFrame
*frame
= new MyFrame(wxT("wxListCtrl Test"), 50, 50, 450, 340);
159 // My frame constructor
160 MyFrame::MyFrame(const wxChar
*title
, int x
, int y
, int w
, int h
)
161 : wxFrame(NULL
, wxID_ANY
, title
, wxPoint(x
, y
), wxSize(w
, h
))
165 m_smallVirtual
= false;
168 SetIcon( wxICON(mondrian
) );
170 // Make an image list containing large icons
171 m_imageListNormal
= new wxImageList(32, 32, true);
172 m_imageListSmall
= new wxImageList(16, 16, true);
175 m_imageListNormal
->Add( wxIcon(_T("icon1"), wxBITMAP_TYPE_ICO_RESOURCE
) );
176 m_imageListNormal
->Add( wxIcon(_T("icon2"), wxBITMAP_TYPE_ICO_RESOURCE
) );
177 m_imageListNormal
->Add( wxIcon(_T("icon3"), wxBITMAP_TYPE_ICO_RESOURCE
) );
178 m_imageListNormal
->Add( wxIcon(_T("icon4"), wxBITMAP_TYPE_ICO_RESOURCE
) );
179 m_imageListNormal
->Add( wxIcon(_T("icon5"), wxBITMAP_TYPE_ICO_RESOURCE
) );
180 m_imageListNormal
->Add( wxIcon(_T("icon6"), wxBITMAP_TYPE_ICO_RESOURCE
) );
181 m_imageListNormal
->Add( wxIcon(_T("icon7"), wxBITMAP_TYPE_ICO_RESOURCE
) );
182 m_imageListNormal
->Add( wxIcon(_T("icon8"), wxBITMAP_TYPE_ICO_RESOURCE
) );
183 m_imageListNormal
->Add( wxIcon(_T("icon9"), wxBITMAP_TYPE_ICO_RESOURCE
) );
185 m_imageListSmall
->Add( wxIcon(_T("iconsmall"), wxBITMAP_TYPE_ICO_RESOURCE
) );
188 m_imageListNormal
->Add( wxIcon( toolbrai_xpm
) );
189 m_imageListNormal
->Add( wxIcon( toolchar_xpm
) );
190 m_imageListNormal
->Add( wxIcon( tooldata_xpm
) );
191 m_imageListNormal
->Add( wxIcon( toolnote_xpm
) );
192 m_imageListNormal
->Add( wxIcon( tooltodo_xpm
) );
193 m_imageListNormal
->Add( wxIcon( toolchec_xpm
) );
194 m_imageListNormal
->Add( wxIcon( toolgame_xpm
) );
195 m_imageListNormal
->Add( wxIcon( tooltime_xpm
) );
196 m_imageListNormal
->Add( wxIcon( toolword_xpm
) );
198 m_imageListSmall
->Add( wxIcon( small1_xpm
) );
202 wxMenu
*menuFile
= new wxMenu
;
203 menuFile
->Append(LIST_ABOUT
, _T("&About"));
204 menuFile
->AppendSeparator();
205 menuFile
->Append(LIST_QUIT
, _T("E&xit\tAlt-X"));
207 wxMenu
*menuView
= new wxMenu
;
208 menuView
->Append(LIST_LIST_VIEW
, _T("&List view\tF1"));
209 menuView
->Append(LIST_REPORT_VIEW
, _T("&Report view\tF2"));
210 menuView
->Append(LIST_ICON_VIEW
, _T("&Icon view\tF3"));
211 menuView
->Append(LIST_ICON_TEXT_VIEW
, _T("Icon view with &text\tF4"));
212 menuView
->Append(LIST_SMALL_ICON_VIEW
, _T("&Small icon view\tF5"));
213 menuView
->Append(LIST_SMALL_ICON_TEXT_VIEW
, _T("Small icon &view with text\tF6"));
214 menuView
->Append(LIST_VIRTUAL_VIEW
, _T("&Virtual view\tF7"));
215 menuView
->Append(LIST_SMALL_VIRTUAL_VIEW
, _T("Small virtual vie&w\tF8"));
217 wxMenu
*menuList
= new wxMenu
;
218 menuList
->Append(LIST_FOCUS_LAST
, _T("&Make last item current\tCtrl-L"));
219 menuList
->Append(LIST_TOGGLE_FIRST
, _T("To&ggle first item\tCtrl-G"));
220 menuList
->Append(LIST_DESELECT_ALL
, _T("&Deselect All\tCtrl-D"));
221 menuList
->Append(LIST_SELECT_ALL
, _T("S&elect All\tCtrl-A"));
222 menuList
->AppendSeparator();
223 menuList
->Append(LIST_SHOW_COL_INFO
, _T("Show &column info\tCtrl-C"));
224 menuList
->Append(LIST_SHOW_SEL_INFO
, _T("Show &selected items\tCtrl-S"));
225 menuList
->AppendSeparator();
226 menuList
->Append(LIST_SORT
, _T("&Sort\tCtrl-S"));
227 menuList
->AppendSeparator();
228 menuList
->Append(LIST_ADD
, _T("&Append an item\tCtrl-P"));
229 menuList
->Append(LIST_EDIT
, _T("&Edit the item\tCtrl-E"));
230 menuList
->Append(LIST_DELETE
, _T("&Delete first item\tCtrl-X"));
231 menuList
->Append(LIST_DELETE_ALL
, _T("Delete &all items"));
232 menuList
->AppendSeparator();
233 menuList
->Append(LIST_FREEZE
, _T("Free&ze\tCtrl-Z"));
234 menuList
->Append(LIST_THAW
, _T("Tha&w\tCtrl-W"));
235 menuList
->AppendSeparator();
236 menuList
->AppendCheckItem(LIST_TOGGLE_LINES
, _T("Toggle &lines\tCtrl-I"));
237 menuList
->Append(LIST_TOGGLE_MULTI_SEL
, _T("&Multiple selection\tCtrl-M"),
238 _T("Toggle multiple selection"), true);
240 wxMenu
*menuCol
= new wxMenu
;
241 menuCol
->Append(LIST_SET_FG_COL
, _T("&Foreground colour..."));
242 menuCol
->Append(LIST_SET_BG_COL
, _T("&Background colour..."));
244 wxMenuBar
*menubar
= new wxMenuBar
;
245 menubar
->Append(menuFile
, _T("&File"));
246 menubar
->Append(menuView
, _T("&View"));
247 menubar
->Append(menuList
, _T("&List"));
248 menubar
->Append(menuCol
, _T("&Colour"));
251 m_panel
= new wxPanel(this, wxID_ANY
);
252 m_logWindow
= new wxTextCtrl(m_panel
, wxID_ANY
, wxEmptyString
,
253 wxDefaultPosition
, wxDefaultSize
,
254 wxTE_MULTILINE
| wxSUNKEN_BORDER
);
256 m_logOld
= wxLog::SetActiveTarget(new wxLogTextCtrl(m_logWindow
));
258 RecreateList(wxLC_REPORT
| wxLC_SINGLE_SEL
);
262 #endif // wxUSE_STATUSBAR
267 delete wxLog::SetActiveTarget(m_logOld
);
269 delete m_imageListNormal
;
270 delete m_imageListSmall
;
273 void MyFrame::OnSize(wxSizeEvent
& event
)
280 void MyFrame::DoSize()
285 wxSize size
= GetClientSize();
286 wxCoord y
= (2*size
.y
)/3;
287 m_listCtrl
->SetSize(0, 0, size
.x
, y
);
288 m_logWindow
->SetSize(0, y
+ 1, size
.x
, size
.y
- y
);
291 bool MyFrame::CheckNonVirtual() const
293 if ( !m_listCtrl
->HasFlag(wxLC_VIRTUAL
) )
296 // "this" == whatever
297 wxLogWarning(_T("Can't do this in virtual view, sorry."));
302 void MyFrame::OnQuit(wxCommandEvent
& WXUNUSED(event
))
307 void MyFrame::OnAbout(wxCommandEvent
& WXUNUSED(event
))
309 wxMessageDialog
dialog(this, _T("List test sample\nJulian Smart (c) 1997"),
310 _T("About list test"), wxOK
|wxCANCEL
);
315 void MyFrame::OnFreeze(wxCommandEvent
& WXUNUSED(event
))
317 wxLogMessage(_T("Freezing the control"));
319 m_listCtrl
->Freeze();
322 void MyFrame::OnThaw(wxCommandEvent
& WXUNUSED(event
))
324 wxLogMessage(_T("Thawing the control"));
329 void MyFrame::OnToggleLines(wxCommandEvent
& event
)
331 m_listCtrl
->SetSingleStyle(wxLC_HRULES
| wxLC_VRULES
, event
.IsChecked());
334 void MyFrame::OnFocusLast(wxCommandEvent
& WXUNUSED(event
))
336 long index
= m_listCtrl
->GetItemCount() - 1;
342 m_listCtrl
->SetItemState(index
, wxLIST_STATE_FOCUSED
, wxLIST_STATE_FOCUSED
);
343 m_listCtrl
->EnsureVisible(index
);
346 void MyFrame::OnToggleFirstSel(wxCommandEvent
& WXUNUSED(event
))
348 m_listCtrl
->SetItemState(0, (~m_listCtrl
->GetItemState(0, wxLIST_STATE_SELECTED
) ) & wxLIST_STATE_SELECTED
, wxLIST_STATE_SELECTED
);
351 void MyFrame::OnDeselectAll(wxCommandEvent
& WXUNUSED(event
))
353 if ( !CheckNonVirtual() )
356 int n
= m_listCtrl
->GetItemCount();
357 for (int i
= 0; i
< n
; i
++)
358 m_listCtrl
->SetItemState(i
,0,wxLIST_STATE_SELECTED
);
361 void MyFrame::OnSelectAll(wxCommandEvent
& WXUNUSED(event
))
363 if ( !CheckNonVirtual() )
366 int n
= m_listCtrl
->GetItemCount();
367 for (int i
= 0; i
< n
; i
++)
368 m_listCtrl
->SetItemState(i
,wxLIST_STATE_SELECTED
, wxLIST_STATE_SELECTED
);
371 // ----------------------------------------------------------------------------
372 // changing listctrl modes
373 // ----------------------------------------------------------------------------
375 void MyFrame::RecreateList(long flags
, bool withText
)
377 // we could avoid recreating it if we don't set/clear the wxLC_VIRTUAL
378 // style, but it is more trouble to do it than not
380 if ( !m_listCtrl
|| ((flags
& wxLC_VIRTUAL
) !=
381 (m_listCtrl
->GetWindowStyleFlag() & wxLC_VIRTUAL
)) )
386 m_listCtrl
= new MyListCtrl(m_panel
, LIST_CTRL
,
387 wxDefaultPosition
, wxDefaultSize
,
389 wxSUNKEN_BORDER
| wxLC_EDIT_LABELS
);
391 switch ( flags
& wxLC_MASK_TYPE
)
398 InitWithIconItems(withText
);
401 case wxLC_SMALL_ICON
:
402 InitWithIconItems(withText
, true);
406 if ( flags
& wxLC_VIRTUAL
)
407 InitWithVirtualItems();
409 InitWithReportItems();
413 wxFAIL_MSG( _T("unknown listctrl mode") );
419 m_logWindow
->Clear();
422 void MyFrame::OnListView(wxCommandEvent
& WXUNUSED(event
))
424 RecreateList(wxLC_LIST
);
427 void MyFrame::InitWithListItems()
429 for ( int i
= 0; i
< NUM_ITEMS
; i
++ )
431 m_listCtrl
->InsertItem(i
, wxString::Format(_T("Item %d"), i
));
435 void MyFrame::OnReportView(wxCommandEvent
& WXUNUSED(event
))
437 RecreateList(wxLC_REPORT
);
440 void MyFrame::InitWithReportItems()
442 m_listCtrl
->SetImageList(m_imageListSmall
, wxIMAGE_LIST_SMALL
);
444 // note that under MSW for SetColumnWidth() to work we need to create the
445 // items with images initially even if we specify dummy image id
447 itemCol
.SetText(_T("Column 1"));
448 itemCol
.SetImage(-1);
449 m_listCtrl
->InsertColumn(0, itemCol
);
451 itemCol
.SetText(_T("Column 2"));
452 itemCol
.SetAlign(wxLIST_FORMAT_CENTRE
);
453 m_listCtrl
->InsertColumn(1, itemCol
);
455 itemCol
.SetText(_T("Column 3"));
456 itemCol
.SetAlign(wxLIST_FORMAT_RIGHT
);
457 m_listCtrl
->InsertColumn(2, itemCol
);
459 // to speed up inserting we hide the control temporarily
464 for ( int i
= 0; i
< NUM_ITEMS
; i
++ )
466 m_listCtrl
->InsertItemInReportView(i
);
469 m_logWindow
->WriteText(wxString::Format(_T("%d items inserted in %ldms\n"),
470 NUM_ITEMS
, sw
.Time()));
473 // we leave all mask fields to 0 and only change the colour
476 item
.SetTextColour(*wxRED
);
477 m_listCtrl
->SetItem( item
);
480 item
.SetTextColour(*wxGREEN
);
481 m_listCtrl
->SetItem( item
);
483 item
.SetTextColour(*wxLIGHT_GREY
);
484 item
.SetFont(*wxITALIC_FONT
);
485 item
.SetBackgroundColour(*wxRED
);
486 m_listCtrl
->SetItem( item
);
488 m_listCtrl
->SetTextColour(*wxBLUE
);
490 m_listCtrl
->SetColumnWidth( 0, wxLIST_AUTOSIZE
);
491 m_listCtrl
->SetColumnWidth( 1, wxLIST_AUTOSIZE
);
492 m_listCtrl
->SetColumnWidth( 2, wxLIST_AUTOSIZE
);
494 // test SetItemFont too
495 m_listCtrl
->SetItemFont(0, *wxITALIC_FONT
);
498 void MyFrame::InitWithIconItems(bool withText
, bool sameIcon
)
500 m_listCtrl
->SetImageList(m_imageListNormal
, wxIMAGE_LIST_NORMAL
);
501 m_listCtrl
->SetImageList(m_imageListSmall
, wxIMAGE_LIST_SMALL
);
503 for ( int i
= 0; i
< NUM_ICONS
; i
++ )
505 int image
= sameIcon
? 0 : i
;
509 m_listCtrl
->InsertItem(i
, wxString::Format(_T("Label %d"), i
),
514 m_listCtrl
->InsertItem(i
, image
);
519 void MyFrame::OnIconView(wxCommandEvent
& WXUNUSED(event
))
521 RecreateList(wxLC_ICON
, false);
524 void MyFrame::OnIconTextView(wxCommandEvent
& WXUNUSED(event
))
526 RecreateList(wxLC_ICON
);
529 void MyFrame::OnSmallIconView(wxCommandEvent
& WXUNUSED(event
))
531 RecreateList(wxLC_SMALL_ICON
, false);
534 void MyFrame::OnSmallIconTextView(wxCommandEvent
& WXUNUSED(event
))
536 RecreateList(wxLC_SMALL_ICON
);
539 void MyFrame::OnVirtualView(wxCommandEvent
& WXUNUSED(event
))
541 m_smallVirtual
= false;
542 RecreateList(wxLC_REPORT
| wxLC_VIRTUAL
);
545 void MyFrame::OnSmallVirtualView(wxCommandEvent
& WXUNUSED(event
))
547 m_smallVirtual
= true;
548 RecreateList(wxLC_REPORT
| wxLC_VIRTUAL
);
551 void MyFrame::InitWithVirtualItems()
553 m_listCtrl
->SetImageList(m_imageListSmall
, wxIMAGE_LIST_SMALL
);
555 if ( m_smallVirtual
)
557 m_listCtrl
->InsertColumn(0, _T("Animal"));
558 m_listCtrl
->InsertColumn(1, _T("Sound"));
559 m_listCtrl
->SetItemCount(WXSIZEOF(SMALL_VIRTUAL_VIEW_ITEMS
));
563 m_listCtrl
->InsertColumn(0, _T("First Column"));
564 m_listCtrl
->InsertColumn(1, _T("Second Column"));
565 m_listCtrl
->SetColumnWidth(0, 150);
566 m_listCtrl
->SetColumnWidth(1, 150);
567 m_listCtrl
->SetItemCount(1000000);
571 void MyFrame::OnSort(wxCommandEvent
& WXUNUSED(event
))
575 m_listCtrl
->SortItems(MyCompareFunction
, 0);
577 m_logWindow
->WriteText(wxString::Format(_T("Sorting %d items took %ld ms\n"),
578 m_listCtrl
->GetItemCount(),
582 void MyFrame::OnShowSelInfo(wxCommandEvent
& WXUNUSED(event
))
584 int selCount
= m_listCtrl
->GetSelectedItemCount();
585 wxLogMessage(_T("%d items selected:"), selCount
);
587 // don't show too many items
588 size_t shownCount
= 0;
590 long item
= m_listCtrl
->GetNextItem(-1, wxLIST_NEXT_ALL
,
591 wxLIST_STATE_SELECTED
);
594 wxLogMessage(_T("\t%ld (%s)"),
595 item
, m_listCtrl
->GetItemText(item
).c_str());
597 if ( ++shownCount
> 10 )
599 wxLogMessage(_T("\t... more selected items snipped..."));
603 item
= m_listCtrl
->GetNextItem(item
, wxLIST_NEXT_ALL
,
604 wxLIST_STATE_SELECTED
);
608 void MyFrame::OnShowColInfo(wxCommandEvent
& WXUNUSED(event
))
610 int count
= m_listCtrl
->GetColumnCount();
611 wxLogMessage(wxT("%d columns:"), count
);
612 for ( int c
= 0; c
< count
; c
++ )
614 wxLogMessage(wxT("\tcolumn %d has width %d"), c
,
615 m_listCtrl
->GetColumnWidth(c
));
619 void MyFrame::OnUpdateShowColInfo(wxUpdateUIEvent
& event
)
621 event
.Enable( (m_listCtrl
->GetWindowStyleFlag() & wxLC_REPORT
) != 0 );
624 void MyFrame::OnToggleMultiSel(wxCommandEvent
& WXUNUSED(event
))
626 long flags
= m_listCtrl
->GetWindowStyleFlag();
627 if ( flags
& wxLC_SINGLE_SEL
)
628 flags
&= ~wxLC_SINGLE_SEL
;
630 flags
|= wxLC_SINGLE_SEL
;
632 m_logWindow
->WriteText(wxString::Format(wxT("Current selection mode: %sle\n"),
633 (flags
& wxLC_SINGLE_SEL
) ? _T("sing") : _T("multip")));
638 void MyFrame::OnUpdateToggleMultiSel(wxUpdateUIEvent
& event
)
640 event
.Check((m_listCtrl
->GetWindowStyleFlag() & wxLC_SINGLE_SEL
) == 0);
643 void MyFrame::OnSetFgColour(wxCommandEvent
& WXUNUSED(event
))
645 m_listCtrl
->SetForegroundColour(wxGetColourFromUser(this));
646 m_listCtrl
->Refresh();
649 void MyFrame::OnSetBgColour(wxCommandEvent
& WXUNUSED(event
))
651 m_listCtrl
->SetBackgroundColour(wxGetColourFromUser(this));
652 m_listCtrl
->Refresh();
655 void MyFrame::OnAdd(wxCommandEvent
& WXUNUSED(event
))
657 m_listCtrl
->InsertItem(m_listCtrl
->GetItemCount(), _T("Appended item"));
660 void MyFrame::OnEdit(wxCommandEvent
& WXUNUSED(event
))
662 long itemCur
= m_listCtrl
->GetNextItem(-1, wxLIST_NEXT_ALL
,
663 wxLIST_STATE_FOCUSED
);
667 m_listCtrl
->EditLabel(itemCur
);
671 m_logWindow
->WriteText(_T("No item to edit"));
675 void MyFrame::OnDelete(wxCommandEvent
& WXUNUSED(event
))
677 if ( m_listCtrl
->GetItemCount() )
679 m_listCtrl
->DeleteItem(0);
683 m_logWindow
->WriteText(_T("Nothing to delete"));
687 void MyFrame::OnDeleteAll(wxCommandEvent
& WXUNUSED(event
))
691 size_t itemCount
= m_listCtrl
->GetItemCount();
693 m_listCtrl
->DeleteAllItems();
695 m_logWindow
->WriteText(wxString::Format(_T("Deleting %d items took %ld ms\n"),
702 void MyListCtrl::OnCacheHint(wxListEvent
& event
)
704 wxLogMessage( wxT("OnCacheHint: cache items %ld..%ld"),
705 event
.GetCacheFrom(), event
.GetCacheTo() );
708 void MyListCtrl::SetColumnImage(int col
, int image
)
711 item
.SetMask(wxLIST_MASK_IMAGE
);
712 item
.SetImage(image
);
713 SetColumn(col
, item
);
716 void MyListCtrl::OnColClick(wxListEvent
& event
)
718 int col
= event
.GetColumn();
720 // set or unset image
721 static bool x
= false;
723 SetColumnImage(col
, x
? 0 : -1);
725 wxLogMessage( wxT("OnColumnClick at %d."), col
);
728 void MyListCtrl::OnColRightClick(wxListEvent
& event
)
730 int col
= event
.GetColumn();
733 SetColumnImage(col
, -1);
736 // Show popupmenu at position
737 wxMenu
menu(wxT("Test"));
738 menu
.Append(LIST_ABOUT
, _T("&About"));
739 PopupMenu(&menu
, event
.GetPoint());
741 wxLogMessage( wxT("OnColumnRightClick at %d."), event
.GetColumn() );
744 void MyListCtrl::LogColEvent(const wxListEvent
& event
, const wxChar
*name
)
746 const int col
= event
.GetColumn();
748 wxLogMessage(wxT("%s: column %d (width = %d or %d)."),
751 event
.GetItem().GetWidth(),
752 GetColumnWidth(col
));
755 void MyListCtrl::OnColBeginDrag(wxListEvent
& event
)
757 LogColEvent( event
, wxT("OnColBeginDrag") );
759 if ( event
.GetColumn() == 0 )
761 wxLogMessage(_T("Resizing this column shouldn't work."));
767 void MyListCtrl::OnColDragging(wxListEvent
& event
)
769 LogColEvent( event
, wxT("OnColDragging") );
772 void MyListCtrl::OnColEndDrag(wxListEvent
& event
)
774 LogColEvent( event
, wxT("OnColEndDrag") );
777 void MyListCtrl::OnBeginDrag(wxListEvent
& event
)
779 const wxPoint
& pt
= event
.m_pointDrag
;
782 wxLogMessage( wxT("OnBeginDrag at (%d, %d), item %ld."),
783 pt
.x
, pt
.y
, HitTest(pt
, flags
) );
786 void MyListCtrl::OnBeginRDrag(wxListEvent
& event
)
788 wxLogMessage( wxT("OnBeginRDrag at %d,%d."),
789 event
.m_pointDrag
.x
, event
.m_pointDrag
.y
);
792 void MyListCtrl::OnBeginLabelEdit(wxListEvent
& event
)
794 wxLogMessage( wxT("OnBeginLabelEdit: %s"), event
.m_item
.m_text
.c_str());
797 void MyListCtrl::OnEndLabelEdit(wxListEvent
& event
)
799 wxLogMessage( wxT("OnEndLabelEdit: %s"),
800 event
.IsEditCancelled() ? _T("[cancelled]")
801 : event
.m_item
.m_text
.c_str());
804 void MyListCtrl::OnDeleteItem(wxListEvent
& event
)
806 LogEvent(event
, _T("OnDeleteItem"));
809 void MyListCtrl::OnDeleteAllItems(wxListEvent
& event
)
811 LogEvent(event
, _T("OnDeleteAllItems"));
814 #if WXWIN_COMPATIBILITY_2_4
815 void MyListCtrl::OnGetInfo(wxListEvent
& event
)
819 msg
<< _T("OnGetInfo (") << event
.m_item
.m_itemId
<< _T(", ") << event
.m_item
.m_col
<< _T(")");
820 if ( event
.m_item
.m_mask
& wxLIST_MASK_STATE
)
821 msg
<< _T(" wxLIST_MASK_STATE");
822 if ( event
.m_item
.m_mask
& wxLIST_MASK_TEXT
)
823 msg
<< _T(" wxLIST_MASK_TEXT");
824 if ( event
.m_item
.m_mask
& wxLIST_MASK_IMAGE
)
825 msg
<< _T(" wxLIST_MASK_IMAGE");
826 if ( event
.m_item
.m_mask
& wxLIST_MASK_DATA
)
827 msg
<< _T(" wxLIST_MASK_DATA");
828 if ( event
.m_item
.m_mask
& wxLIST_SET_ITEM
)
829 msg
<< _T(" wxLIST_SET_ITEM");
830 if ( event
.m_item
.m_mask
& wxLIST_MASK_WIDTH
)
831 msg
<< _T(" wxLIST_MASK_WIDTH");
832 if ( event
.m_item
.m_mask
& wxLIST_MASK_FORMAT
)
833 msg
<< _T(" wxLIST_MASK_WIDTH");
835 if ( event
.m_item
.m_mask
& wxLIST_MASK_TEXT
)
837 event
.m_item
.m_text
= _T("My callback text");
843 void MyListCtrl::OnSetInfo(wxListEvent
& event
)
845 LogEvent(event
, _T("OnSetInfo"));
849 void MyListCtrl::OnSelected(wxListEvent
& event
)
851 LogEvent(event
, _T("OnSelected"));
853 if ( GetWindowStyle() & wxLC_REPORT
)
856 info
.m_itemId
= event
.m_itemIndex
;
858 info
.m_mask
= wxLIST_MASK_TEXT
;
861 wxLogMessage(wxT("Value of the 2nd field of the selected item: %s"),
862 info
.m_text
.c_str());
866 wxFAIL_MSG(wxT("wxListCtrl::GetItem() failed"));
871 void MyListCtrl::OnDeselected(wxListEvent
& event
)
873 LogEvent(event
, _T("OnDeselected"));
876 void MyListCtrl::OnActivated(wxListEvent
& event
)
878 LogEvent(event
, _T("OnActivated"));
881 void MyListCtrl::OnFocused(wxListEvent
& event
)
883 LogEvent(event
, _T("OnFocused"));
888 void MyListCtrl::OnListKeyDown(wxListEvent
& event
)
892 switch ( event
.GetKeyCode() )
894 case 'c': // colorize
898 info
.m_itemId
= event
.GetIndex();
901 wxListItemAttr
*attr
= info
.GetAttributes();
902 if ( !attr
|| !attr
->HasTextColour() )
904 info
.SetTextColour(*wxCYAN
);
908 RefreshItem(info
.m_itemId
);
915 item
= GetNextItem(-1, wxLIST_NEXT_ALL
, wxLIST_STATE_FOCUSED
);
916 if ( item
++ == GetItemCount() - 1 )
921 wxLogMessage(_T("Focusing item %ld"), item
);
923 SetItemState(item
, wxLIST_STATE_FOCUSED
, wxLIST_STATE_FOCUSED
);
927 case 'r': // show bounding Rect
930 item
= event
.GetIndex();
932 if ( !GetItemRect(item
, r
) )
934 wxLogError(_T("Failed to retrieve rect of item %ld"), item
);
938 wxLogMessage(_T("Bounding rect of item %ld is (%d, %d)-(%d, %d)"),
939 item
, r
.x
, r
.y
, r
.x
+ r
.width
, r
.y
+ r
.height
);
944 item
= GetNextItem(-1, wxLIST_NEXT_ALL
, wxLIST_STATE_SELECTED
);
949 wxLogMessage(_T("Item %ld deleted"), item
);
951 // -1 because the indices were shifted by DeleteItem()
952 item
= GetNextItem(item
- 1,
953 wxLIST_NEXT_ALL
, wxLIST_STATE_SELECTED
);
958 if ( GetWindowStyle() & wxLC_REPORT
)
960 if ( GetWindowStyle() & wxLC_VIRTUAL
)
962 SetItemCount(GetItemCount() + 1);
966 InsertItemInReportView(event
.GetIndex());
972 LogEvent(event
, _T("OnListKeyDown"));
978 void MyListCtrl::OnChar(wxKeyEvent
& event
)
980 wxLogMessage(_T("Got char event."));
982 switch ( event
.GetKeyCode() )
988 // these are the keys we process ourselves
996 void MyListCtrl::LogEvent(const wxListEvent
& event
, const wxChar
*eventName
)
998 wxLogMessage(_T("Item %ld: %s (item text = %s, data = %ld)"),
999 event
.GetIndex(), eventName
,
1000 event
.GetText().c_str(), event
.GetData());
1003 wxString
MyListCtrl::OnGetItemText(long item
, long column
) const
1005 if ( GetItemCount() == WXSIZEOF(SMALL_VIRTUAL_VIEW_ITEMS
) )
1007 return SMALL_VIRTUAL_VIEW_ITEMS
[item
][column
];
1011 return wxString::Format(_T("Column %ld of item %ld"), column
, item
);
1015 int MyListCtrl::OnGetItemImage(long WXUNUSED(item
)) const
1020 wxListItemAttr
*MyListCtrl::OnGetItemAttr(long item
) const
1022 return item
% 2 ? NULL
: (wxListItemAttr
*)&m_attr
;
1025 void MyListCtrl::InsertItemInReportView(int i
)
1028 buf
.Printf(_T("This is item %d"), i
);
1029 long tmp
= InsertItem(i
, buf
, 0);
1030 SetItemData(tmp
, i
);
1032 buf
.Printf(_T("Col 1, item %d"), i
);
1035 buf
.Printf(_T("Item %d in column 2"), i
);