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
44 #include "wx/settings.h"
48 const wxChar
*SMALL_VIRTUAL_VIEW_ITEMS
[][2] =
50 { _T("Cat"), _T("meow") },
51 { _T("Cow"), _T("moo") },
52 { _T("Crow"), _T("caw") },
53 { _T("Dog"), _T("woof") },
54 { _T("Duck"), _T("quack") },
55 { _T("Mouse"), _T("squeak") },
56 { _T("Owl"), _T("hoo") },
57 { _T("Pig"), _T("oink") },
58 { _T("Pigeon"), _T("coo") },
59 { _T("Sheep"), _T("baaah") },
63 BEGIN_EVENT_TABLE(MyFrame
, wxFrame
)
64 EVT_SIZE(MyFrame::OnSize
)
66 EVT_MENU(LIST_QUIT
, MyFrame::OnQuit
)
67 EVT_MENU(LIST_ABOUT
, MyFrame::OnAbout
)
68 EVT_MENU(LIST_LIST_VIEW
, MyFrame::OnListView
)
69 EVT_MENU(LIST_REPORT_VIEW
, MyFrame::OnReportView
)
70 EVT_MENU(LIST_ICON_VIEW
, MyFrame::OnIconView
)
71 EVT_MENU(LIST_ICON_TEXT_VIEW
, MyFrame::OnIconTextView
)
72 EVT_MENU(LIST_SMALL_ICON_VIEW
, MyFrame::OnSmallIconView
)
73 EVT_MENU(LIST_SMALL_ICON_TEXT_VIEW
, MyFrame::OnSmallIconTextView
)
74 EVT_MENU(LIST_VIRTUAL_VIEW
, MyFrame::OnVirtualView
)
75 EVT_MENU(LIST_SMALL_VIRTUAL_VIEW
, MyFrame::OnSmallVirtualView
)
77 EVT_MENU(LIST_FOCUS_LAST
, MyFrame::OnFocusLast
)
78 EVT_MENU(LIST_TOGGLE_FIRST
, MyFrame::OnToggleFirstSel
)
79 EVT_MENU(LIST_DESELECT_ALL
, MyFrame::OnDeselectAll
)
80 EVT_MENU(LIST_SELECT_ALL
, MyFrame::OnSelectAll
)
81 EVT_MENU(LIST_DELETE
, MyFrame::OnDelete
)
82 EVT_MENU(LIST_ADD
, MyFrame::OnAdd
)
83 EVT_MENU(LIST_EDIT
, MyFrame::OnEdit
)
84 EVT_MENU(LIST_DELETE_ALL
, MyFrame::OnDeleteAll
)
85 EVT_MENU(LIST_SORT
, MyFrame::OnSort
)
86 EVT_MENU(LIST_SET_FG_COL
, MyFrame::OnSetFgColour
)
87 EVT_MENU(LIST_SET_BG_COL
, MyFrame::OnSetBgColour
)
88 EVT_MENU(LIST_TOGGLE_MULTI_SEL
, MyFrame::OnToggleMultiSel
)
89 EVT_MENU(LIST_SHOW_COL_INFO
, MyFrame::OnShowColInfo
)
90 EVT_MENU(LIST_SHOW_SEL_INFO
, MyFrame::OnShowSelInfo
)
91 EVT_MENU(LIST_FREEZE
, MyFrame::OnFreeze
)
92 EVT_MENU(LIST_THAW
, MyFrame::OnThaw
)
93 EVT_MENU(LIST_TOGGLE_LINES
, MyFrame::OnToggleLines
)
95 EVT_UPDATE_UI(LIST_SHOW_COL_INFO
, MyFrame::OnUpdateShowColInfo
)
96 EVT_UPDATE_UI(LIST_TOGGLE_MULTI_SEL
, MyFrame::OnUpdateToggleMultiSel
)
99 BEGIN_EVENT_TABLE(MyListCtrl
, wxListCtrl
)
100 EVT_LIST_BEGIN_DRAG(LIST_CTRL
, MyListCtrl::OnBeginDrag
)
101 EVT_LIST_BEGIN_RDRAG(LIST_CTRL
, MyListCtrl::OnBeginRDrag
)
102 EVT_LIST_BEGIN_LABEL_EDIT(LIST_CTRL
, MyListCtrl::OnBeginLabelEdit
)
103 EVT_LIST_END_LABEL_EDIT(LIST_CTRL
, MyListCtrl::OnEndLabelEdit
)
104 EVT_LIST_DELETE_ITEM(LIST_CTRL
, MyListCtrl::OnDeleteItem
)
105 EVT_LIST_DELETE_ALL_ITEMS(LIST_CTRL
, MyListCtrl::OnDeleteAllItems
)
106 #if WXWIN_COMPATIBILITY_2_4
107 EVT_LIST_GET_INFO(LIST_CTRL
, MyListCtrl::OnGetInfo
)
108 EVT_LIST_SET_INFO(LIST_CTRL
, MyListCtrl::OnSetInfo
)
110 EVT_LIST_ITEM_SELECTED(LIST_CTRL
, MyListCtrl::OnSelected
)
111 EVT_LIST_ITEM_DESELECTED(LIST_CTRL
, MyListCtrl::OnDeselected
)
112 EVT_LIST_KEY_DOWN(LIST_CTRL
, MyListCtrl::OnListKeyDown
)
113 EVT_LIST_ITEM_ACTIVATED(LIST_CTRL
, MyListCtrl::OnActivated
)
114 EVT_LIST_ITEM_FOCUSED(LIST_CTRL
, MyListCtrl::OnFocused
)
116 EVT_LIST_COL_CLICK(LIST_CTRL
, MyListCtrl::OnColClick
)
117 EVT_LIST_COL_RIGHT_CLICK(LIST_CTRL
, MyListCtrl::OnColRightClick
)
118 EVT_LIST_COL_BEGIN_DRAG(LIST_CTRL
, MyListCtrl::OnColBeginDrag
)
119 EVT_LIST_COL_DRAGGING(LIST_CTRL
, MyListCtrl::OnColDragging
)
120 EVT_LIST_COL_END_DRAG(LIST_CTRL
, MyListCtrl::OnColEndDrag
)
122 EVT_LIST_CACHE_HINT(LIST_CTRL
, MyListCtrl::OnCacheHint
)
125 EVT_CONTEXT_MENU(MyListCtrl::OnContextMenu
)
127 EVT_CHAR(MyListCtrl::OnChar
)
132 // number of items in list/report view
133 static const int NUM_ITEMS
= 30;
135 // number of items in icon/small icon view
136 static const int NUM_ICONS
= 9;
138 int wxCALLBACK
MyCompareFunction(long item1
, long item2
, long WXUNUSED(sortData
))
149 // `Main program' equivalent, creating windows and returning main app frame
152 // Create the main frame window
153 MyFrame
*frame
= new MyFrame(wxT("wxListCtrl Test"));
163 // My frame constructor
164 MyFrame::MyFrame(const wxChar
*title
)
165 : wxFrame(NULL
, wxID_ANY
, title
)
167 if (wxSystemSettings::GetScreenType() > wxSYS_SCREEN_SMALL
)
168 SetSize(wxSize(450, 340));
172 m_smallVirtual
= false;
175 SetIcon( wxICON(mondrian
) );
177 // Make an image list containing large icons
178 m_imageListNormal
= new wxImageList(32, 32, true);
179 m_imageListSmall
= new wxImageList(16, 16, true);
182 m_imageListNormal
->Add( wxIcon(_T("icon1"), wxBITMAP_TYPE_ICO_RESOURCE
) );
183 m_imageListNormal
->Add( wxIcon(_T("icon2"), wxBITMAP_TYPE_ICO_RESOURCE
) );
184 m_imageListNormal
->Add( wxIcon(_T("icon3"), wxBITMAP_TYPE_ICO_RESOURCE
) );
185 m_imageListNormal
->Add( wxIcon(_T("icon4"), wxBITMAP_TYPE_ICO_RESOURCE
) );
186 m_imageListNormal
->Add( wxIcon(_T("icon5"), wxBITMAP_TYPE_ICO_RESOURCE
) );
187 m_imageListNormal
->Add( wxIcon(_T("icon6"), wxBITMAP_TYPE_ICO_RESOURCE
) );
188 m_imageListNormal
->Add( wxIcon(_T("icon7"), wxBITMAP_TYPE_ICO_RESOURCE
) );
189 m_imageListNormal
->Add( wxIcon(_T("icon8"), wxBITMAP_TYPE_ICO_RESOURCE
) );
190 m_imageListNormal
->Add( wxIcon(_T("icon9"), wxBITMAP_TYPE_ICO_RESOURCE
) );
192 m_imageListSmall
->Add( wxIcon(_T("iconsmall"), wxBITMAP_TYPE_ICO_RESOURCE
) );
195 m_imageListNormal
->Add( wxIcon( toolbrai_xpm
) );
196 m_imageListNormal
->Add( wxIcon( toolchar_xpm
) );
197 m_imageListNormal
->Add( wxIcon( tooldata_xpm
) );
198 m_imageListNormal
->Add( wxIcon( toolnote_xpm
) );
199 m_imageListNormal
->Add( wxIcon( tooltodo_xpm
) );
200 m_imageListNormal
->Add( wxIcon( toolchec_xpm
) );
201 m_imageListNormal
->Add( wxIcon( toolgame_xpm
) );
202 m_imageListNormal
->Add( wxIcon( tooltime_xpm
) );
203 m_imageListNormal
->Add( wxIcon( toolword_xpm
) );
205 m_imageListSmall
->Add( wxIcon( small1_xpm
) );
209 wxMenu
*menuFile
= new wxMenu
;
210 menuFile
->Append(LIST_ABOUT
, _T("&About"));
211 menuFile
->AppendSeparator();
212 menuFile
->Append(LIST_QUIT
, _T("E&xit\tAlt-X"));
214 wxMenu
*menuView
= new wxMenu
;
215 menuView
->Append(LIST_LIST_VIEW
, _T("&List view\tF1"));
216 menuView
->Append(LIST_REPORT_VIEW
, _T("&Report view\tF2"));
217 menuView
->Append(LIST_ICON_VIEW
, _T("&Icon view\tF3"));
218 menuView
->Append(LIST_ICON_TEXT_VIEW
, _T("Icon view with &text\tF4"));
219 menuView
->Append(LIST_SMALL_ICON_VIEW
, _T("&Small icon view\tF5"));
220 menuView
->Append(LIST_SMALL_ICON_TEXT_VIEW
, _T("Small icon &view with text\tF6"));
221 menuView
->Append(LIST_VIRTUAL_VIEW
, _T("&Virtual view\tF7"));
222 menuView
->Append(LIST_SMALL_VIRTUAL_VIEW
, _T("Small virtual vie&w\tF8"));
224 wxMenu
*menuList
= new wxMenu
;
225 menuList
->Append(LIST_FOCUS_LAST
, _T("&Make last item current\tCtrl-L"));
226 menuList
->Append(LIST_TOGGLE_FIRST
, _T("To&ggle first item\tCtrl-G"));
227 menuList
->Append(LIST_DESELECT_ALL
, _T("&Deselect All\tCtrl-D"));
228 menuList
->Append(LIST_SELECT_ALL
, _T("S&elect All\tCtrl-A"));
229 menuList
->AppendSeparator();
230 menuList
->Append(LIST_SHOW_COL_INFO
, _T("Show &column info\tCtrl-C"));
231 menuList
->Append(LIST_SHOW_SEL_INFO
, _T("Show &selected items\tCtrl-S"));
232 menuList
->AppendSeparator();
233 menuList
->Append(LIST_SORT
, _T("&Sort\tCtrl-S"));
234 menuList
->AppendSeparator();
235 menuList
->Append(LIST_ADD
, _T("&Append an item\tCtrl-P"));
236 menuList
->Append(LIST_EDIT
, _T("&Edit the item\tCtrl-E"));
237 menuList
->Append(LIST_DELETE
, _T("&Delete first item\tCtrl-X"));
238 menuList
->Append(LIST_DELETE_ALL
, _T("Delete &all items"));
239 menuList
->AppendSeparator();
240 menuList
->Append(LIST_FREEZE
, _T("Free&ze\tCtrl-Z"));
241 menuList
->Append(LIST_THAW
, _T("Tha&w\tCtrl-W"));
242 menuList
->AppendSeparator();
243 menuList
->AppendCheckItem(LIST_TOGGLE_LINES
, _T("Toggle &lines\tCtrl-I"));
244 menuList
->Append(LIST_TOGGLE_MULTI_SEL
, _T("&Multiple selection\tCtrl-M"),
245 _T("Toggle multiple selection"), true);
247 wxMenu
*menuCol
= new wxMenu
;
248 menuCol
->Append(LIST_SET_FG_COL
, _T("&Foreground colour..."));
249 menuCol
->Append(LIST_SET_BG_COL
, _T("&Background colour..."));
251 wxMenuBar
*menubar
= new wxMenuBar
;
252 menubar
->Append(menuFile
, _T("&File"));
253 menubar
->Append(menuView
, _T("&View"));
254 menubar
->Append(menuList
, _T("&List"));
255 menubar
->Append(menuCol
, _T("&Colour"));
258 m_panel
= new wxPanel(this, wxID_ANY
);
259 m_logWindow
= new wxTextCtrl(m_panel
, wxID_ANY
, wxEmptyString
,
260 wxDefaultPosition
, wxDefaultSize
,
261 wxTE_MULTILINE
| wxSUNKEN_BORDER
);
263 m_logOld
= wxLog::SetActiveTarget(new wxLogTextCtrl(m_logWindow
));
265 RecreateList(wxLC_REPORT
| wxLC_SINGLE_SEL
);
269 #endif // wxUSE_STATUSBAR
274 delete wxLog::SetActiveTarget(m_logOld
);
276 delete m_imageListNormal
;
277 delete m_imageListSmall
;
280 void MyFrame::OnSize(wxSizeEvent
& event
)
287 void MyFrame::DoSize()
292 wxSize size
= GetClientSize();
293 wxCoord y
= (2*size
.y
)/3;
294 m_listCtrl
->SetSize(0, 0, size
.x
, y
);
295 m_logWindow
->SetSize(0, y
+ 1, size
.x
, size
.y
- y
);
298 bool MyFrame::CheckNonVirtual() const
300 if ( !m_listCtrl
->HasFlag(wxLC_VIRTUAL
) )
303 // "this" == whatever
304 wxLogWarning(_T("Can't do this in virtual view, sorry."));
309 void MyFrame::OnQuit(wxCommandEvent
& WXUNUSED(event
))
314 void MyFrame::OnAbout(wxCommandEvent
& WXUNUSED(event
))
316 wxMessageDialog
dialog(this, _T("List test sample\nJulian Smart (c) 1997"),
317 _T("About list test"), wxOK
|wxCANCEL
);
322 void MyFrame::OnFreeze(wxCommandEvent
& WXUNUSED(event
))
324 wxLogMessage(_T("Freezing the control"));
326 m_listCtrl
->Freeze();
329 void MyFrame::OnThaw(wxCommandEvent
& WXUNUSED(event
))
331 wxLogMessage(_T("Thawing the control"));
336 void MyFrame::OnToggleLines(wxCommandEvent
& event
)
338 m_listCtrl
->SetSingleStyle(wxLC_HRULES
| wxLC_VRULES
, event
.IsChecked());
341 void MyFrame::OnFocusLast(wxCommandEvent
& WXUNUSED(event
))
343 long index
= m_listCtrl
->GetItemCount() - 1;
349 m_listCtrl
->SetItemState(index
, wxLIST_STATE_FOCUSED
, wxLIST_STATE_FOCUSED
);
350 m_listCtrl
->EnsureVisible(index
);
353 void MyFrame::OnToggleFirstSel(wxCommandEvent
& WXUNUSED(event
))
355 m_listCtrl
->SetItemState(0, (~m_listCtrl
->GetItemState(0, wxLIST_STATE_SELECTED
) ) & wxLIST_STATE_SELECTED
, wxLIST_STATE_SELECTED
);
358 void MyFrame::OnDeselectAll(wxCommandEvent
& WXUNUSED(event
))
360 if ( !CheckNonVirtual() )
363 int n
= m_listCtrl
->GetItemCount();
364 for (int i
= 0; i
< n
; i
++)
365 m_listCtrl
->SetItemState(i
,0,wxLIST_STATE_SELECTED
);
368 void MyFrame::OnSelectAll(wxCommandEvent
& WXUNUSED(event
))
370 if ( !CheckNonVirtual() )
373 int n
= m_listCtrl
->GetItemCount();
374 for (int i
= 0; i
< n
; i
++)
375 m_listCtrl
->SetItemState(i
,wxLIST_STATE_SELECTED
, wxLIST_STATE_SELECTED
);
378 // ----------------------------------------------------------------------------
379 // changing listctrl modes
380 // ----------------------------------------------------------------------------
382 void MyFrame::RecreateList(long flags
, bool withText
)
384 // we could avoid recreating it if we don't set/clear the wxLC_VIRTUAL
385 // style, but it is more trouble to do it than not
387 if ( !m_listCtrl
|| ((flags
& wxLC_VIRTUAL
) !=
388 (m_listCtrl
->GetWindowStyleFlag() & wxLC_VIRTUAL
)) )
393 m_listCtrl
= new MyListCtrl(m_panel
, LIST_CTRL
,
394 wxDefaultPosition
, wxDefaultSize
,
396 wxSUNKEN_BORDER
| wxLC_EDIT_LABELS
);
398 switch ( flags
& wxLC_MASK_TYPE
)
405 InitWithIconItems(withText
);
408 case wxLC_SMALL_ICON
:
409 InitWithIconItems(withText
, true);
413 if ( flags
& wxLC_VIRTUAL
)
414 InitWithVirtualItems();
416 InitWithReportItems();
420 wxFAIL_MSG( _T("unknown listctrl mode") );
426 m_logWindow
->Clear();
429 void MyFrame::OnListView(wxCommandEvent
& WXUNUSED(event
))
431 RecreateList(wxLC_LIST
);
434 void MyFrame::InitWithListItems()
436 for ( int i
= 0; i
< NUM_ITEMS
; i
++ )
438 m_listCtrl
->InsertItem(i
, wxString::Format(_T("Item %d"), i
));
442 void MyFrame::OnReportView(wxCommandEvent
& WXUNUSED(event
))
444 RecreateList(wxLC_REPORT
);
447 void MyFrame::InitWithReportItems()
449 m_listCtrl
->SetImageList(m_imageListSmall
, wxIMAGE_LIST_SMALL
);
451 // note that under MSW for SetColumnWidth() to work we need to create the
452 // items with images initially even if we specify dummy image id
454 itemCol
.SetText(_T("Column 1"));
455 itemCol
.SetImage(-1);
456 m_listCtrl
->InsertColumn(0, itemCol
);
458 itemCol
.SetText(_T("Column 2"));
459 itemCol
.SetAlign(wxLIST_FORMAT_CENTRE
);
460 m_listCtrl
->InsertColumn(1, itemCol
);
462 itemCol
.SetText(_T("Column 3"));
463 itemCol
.SetAlign(wxLIST_FORMAT_RIGHT
);
464 m_listCtrl
->InsertColumn(2, itemCol
);
466 // to speed up inserting we hide the control temporarily
471 for ( int i
= 0; i
< NUM_ITEMS
; i
++ )
473 m_listCtrl
->InsertItemInReportView(i
);
476 m_logWindow
->WriteText(wxString::Format(_T("%d items inserted in %ldms\n"),
477 NUM_ITEMS
, sw
.Time()));
480 // we leave all mask fields to 0 and only change the colour
483 item
.SetTextColour(*wxRED
);
484 m_listCtrl
->SetItem( item
);
487 item
.SetTextColour(*wxGREEN
);
488 m_listCtrl
->SetItem( item
);
490 item
.SetTextColour(*wxLIGHT_GREY
);
491 item
.SetFont(*wxITALIC_FONT
);
492 item
.SetBackgroundColour(*wxRED
);
493 m_listCtrl
->SetItem( item
);
495 m_listCtrl
->SetTextColour(*wxBLUE
);
497 m_listCtrl
->SetColumnWidth( 0, wxLIST_AUTOSIZE
);
498 m_listCtrl
->SetColumnWidth( 1, wxLIST_AUTOSIZE
);
499 m_listCtrl
->SetColumnWidth( 2, wxLIST_AUTOSIZE
);
501 // Set images in columns
502 m_listCtrl
->SetItemColumnImage(1, 1, 0);
508 m_listCtrl
->SetItem(info
);
510 // test SetItemFont too
511 m_listCtrl
->SetItemFont(0, *wxITALIC_FONT
);
514 void MyFrame::InitWithIconItems(bool withText
, bool sameIcon
)
516 m_listCtrl
->SetImageList(m_imageListNormal
, wxIMAGE_LIST_NORMAL
);
517 m_listCtrl
->SetImageList(m_imageListSmall
, wxIMAGE_LIST_SMALL
);
519 for ( int i
= 0; i
< NUM_ICONS
; i
++ )
521 int image
= sameIcon
? 0 : i
;
525 m_listCtrl
->InsertItem(i
, wxString::Format(_T("Label %d"), i
),
530 m_listCtrl
->InsertItem(i
, image
);
535 void MyFrame::OnIconView(wxCommandEvent
& WXUNUSED(event
))
537 RecreateList(wxLC_ICON
, false);
540 void MyFrame::OnIconTextView(wxCommandEvent
& WXUNUSED(event
))
542 RecreateList(wxLC_ICON
);
545 void MyFrame::OnSmallIconView(wxCommandEvent
& WXUNUSED(event
))
547 RecreateList(wxLC_SMALL_ICON
, false);
550 void MyFrame::OnSmallIconTextView(wxCommandEvent
& WXUNUSED(event
))
552 RecreateList(wxLC_SMALL_ICON
);
555 void MyFrame::OnVirtualView(wxCommandEvent
& WXUNUSED(event
))
557 m_smallVirtual
= false;
558 RecreateList(wxLC_REPORT
| wxLC_VIRTUAL
);
561 void MyFrame::OnSmallVirtualView(wxCommandEvent
& WXUNUSED(event
))
563 m_smallVirtual
= true;
564 RecreateList(wxLC_REPORT
| wxLC_VIRTUAL
);
567 void MyFrame::InitWithVirtualItems()
569 m_listCtrl
->SetImageList(m_imageListSmall
, wxIMAGE_LIST_SMALL
);
571 if ( m_smallVirtual
)
573 m_listCtrl
->InsertColumn(0, _T("Animal"));
574 m_listCtrl
->InsertColumn(1, _T("Sound"));
575 m_listCtrl
->SetItemCount(WXSIZEOF(SMALL_VIRTUAL_VIEW_ITEMS
));
579 m_listCtrl
->InsertColumn(0, _T("First Column"));
580 m_listCtrl
->InsertColumn(1, _T("Second Column"));
581 m_listCtrl
->SetColumnWidth(0, 150);
582 m_listCtrl
->SetColumnWidth(1, 150);
583 m_listCtrl
->SetItemCount(1000000);
587 void MyFrame::OnSort(wxCommandEvent
& WXUNUSED(event
))
591 m_listCtrl
->SortItems(MyCompareFunction
, 0);
593 m_logWindow
->WriteText(wxString::Format(_T("Sorting %d items took %ld ms\n"),
594 m_listCtrl
->GetItemCount(),
598 void MyFrame::OnShowSelInfo(wxCommandEvent
& WXUNUSED(event
))
600 int selCount
= m_listCtrl
->GetSelectedItemCount();
601 wxLogMessage(_T("%d items selected:"), selCount
);
603 // don't show too many items
604 size_t shownCount
= 0;
606 long item
= m_listCtrl
->GetNextItem(-1, wxLIST_NEXT_ALL
,
607 wxLIST_STATE_SELECTED
);
610 wxLogMessage(_T("\t%ld (%s)"),
611 item
, m_listCtrl
->GetItemText(item
).c_str());
613 if ( ++shownCount
> 10 )
615 wxLogMessage(_T("\t... more selected items snipped..."));
619 item
= m_listCtrl
->GetNextItem(item
, wxLIST_NEXT_ALL
,
620 wxLIST_STATE_SELECTED
);
624 void MyFrame::OnShowColInfo(wxCommandEvent
& WXUNUSED(event
))
626 int count
= m_listCtrl
->GetColumnCount();
627 wxLogMessage(wxT("%d columns:"), count
);
628 for ( int c
= 0; c
< count
; c
++ )
630 wxLogMessage(wxT("\tcolumn %d has width %d"), c
,
631 m_listCtrl
->GetColumnWidth(c
));
635 void MyFrame::OnUpdateShowColInfo(wxUpdateUIEvent
& event
)
637 event
.Enable( (m_listCtrl
->GetWindowStyleFlag() & wxLC_REPORT
) != 0 );
640 void MyFrame::OnToggleMultiSel(wxCommandEvent
& WXUNUSED(event
))
642 long flags
= m_listCtrl
->GetWindowStyleFlag();
643 if ( flags
& wxLC_SINGLE_SEL
)
644 flags
&= ~wxLC_SINGLE_SEL
;
646 flags
|= wxLC_SINGLE_SEL
;
648 m_logWindow
->WriteText(wxString::Format(wxT("Current selection mode: %sle\n"),
649 (flags
& wxLC_SINGLE_SEL
) ? _T("sing") : _T("multip")));
654 void MyFrame::OnUpdateToggleMultiSel(wxUpdateUIEvent
& event
)
656 event
.Check((m_listCtrl
->GetWindowStyleFlag() & wxLC_SINGLE_SEL
) == 0);
659 void MyFrame::OnSetFgColour(wxCommandEvent
& WXUNUSED(event
))
661 m_listCtrl
->SetForegroundColour(wxGetColourFromUser(this));
662 m_listCtrl
->Refresh();
665 void MyFrame::OnSetBgColour(wxCommandEvent
& WXUNUSED(event
))
667 m_listCtrl
->SetBackgroundColour(wxGetColourFromUser(this));
668 m_listCtrl
->Refresh();
671 void MyFrame::OnAdd(wxCommandEvent
& WXUNUSED(event
))
673 m_listCtrl
->InsertItem(m_listCtrl
->GetItemCount(), _T("Appended item"));
676 void MyFrame::OnEdit(wxCommandEvent
& WXUNUSED(event
))
678 long itemCur
= m_listCtrl
->GetNextItem(-1, wxLIST_NEXT_ALL
,
679 wxLIST_STATE_FOCUSED
);
683 m_listCtrl
->EditLabel(itemCur
);
687 m_logWindow
->WriteText(_T("No item to edit"));
691 void MyFrame::OnDelete(wxCommandEvent
& WXUNUSED(event
))
693 if ( m_listCtrl
->GetItemCount() )
695 m_listCtrl
->DeleteItem(0);
699 m_logWindow
->WriteText(_T("Nothing to delete"));
703 void MyFrame::OnDeleteAll(wxCommandEvent
& WXUNUSED(event
))
707 size_t itemCount
= m_listCtrl
->GetItemCount();
709 m_listCtrl
->DeleteAllItems();
711 m_logWindow
->WriteText(wxString::Format(_T("Deleting %d items took %ld ms\n"),
718 void MyListCtrl::OnCacheHint(wxListEvent
& event
)
720 wxLogMessage( wxT("OnCacheHint: cache items %ld..%ld"),
721 event
.GetCacheFrom(), event
.GetCacheTo() );
724 void MyListCtrl::SetColumnImage(int col
, int image
)
727 item
.SetMask(wxLIST_MASK_IMAGE
);
728 item
.SetImage(image
);
729 SetColumn(col
, item
);
732 void MyListCtrl::OnColClick(wxListEvent
& event
)
734 int col
= event
.GetColumn();
736 // set or unset image
737 static bool x
= false;
739 SetColumnImage(col
, x
? 0 : -1);
741 wxLogMessage( wxT("OnColumnClick at %d."), col
);
744 void MyListCtrl::OnColRightClick(wxListEvent
& event
)
746 int col
= event
.GetColumn();
749 SetColumnImage(col
, -1);
752 // Show popupmenu at position
753 wxMenu
menu(wxT("Test"));
754 menu
.Append(LIST_ABOUT
, _T("&About"));
755 PopupMenu(&menu
, event
.GetPoint());
757 wxLogMessage( wxT("OnColumnRightClick at %d."), event
.GetColumn() );
760 void MyListCtrl::LogColEvent(const wxListEvent
& event
, const wxChar
*name
)
762 const int col
= event
.GetColumn();
764 wxLogMessage(wxT("%s: column %d (width = %d or %d)."),
767 event
.GetItem().GetWidth(),
768 GetColumnWidth(col
));
771 void MyListCtrl::OnColBeginDrag(wxListEvent
& event
)
773 LogColEvent( event
, wxT("OnColBeginDrag") );
775 if ( event
.GetColumn() == 0 )
777 wxLogMessage(_T("Resizing this column shouldn't work."));
783 void MyListCtrl::OnColDragging(wxListEvent
& event
)
785 LogColEvent( event
, wxT("OnColDragging") );
788 void MyListCtrl::OnColEndDrag(wxListEvent
& event
)
790 LogColEvent( event
, wxT("OnColEndDrag") );
793 void MyListCtrl::OnBeginDrag(wxListEvent
& event
)
795 const wxPoint
& pt
= event
.m_pointDrag
;
798 wxLogMessage( wxT("OnBeginDrag at (%d, %d), item %ld."),
799 pt
.x
, pt
.y
, HitTest(pt
, flags
) );
802 void MyListCtrl::OnBeginRDrag(wxListEvent
& event
)
804 wxLogMessage( wxT("OnBeginRDrag at %d,%d."),
805 event
.m_pointDrag
.x
, event
.m_pointDrag
.y
);
808 void MyListCtrl::OnBeginLabelEdit(wxListEvent
& event
)
810 wxLogMessage( wxT("OnBeginLabelEdit: %s"), event
.m_item
.m_text
.c_str());
813 void MyListCtrl::OnEndLabelEdit(wxListEvent
& event
)
815 wxLogMessage( wxT("OnEndLabelEdit: %s"),
816 event
.IsEditCancelled() ? _T("[cancelled]")
817 : event
.m_item
.m_text
.c_str());
820 void MyListCtrl::OnDeleteItem(wxListEvent
& event
)
822 LogEvent(event
, _T("OnDeleteItem"));
825 void MyListCtrl::OnDeleteAllItems(wxListEvent
& event
)
827 LogEvent(event
, _T("OnDeleteAllItems"));
830 #if WXWIN_COMPATIBILITY_2_4
831 void MyListCtrl::OnGetInfo(wxListEvent
& event
)
835 msg
<< _T("OnGetInfo (") << event
.m_item
.m_itemId
<< _T(", ") << event
.m_item
.m_col
<< _T(")");
836 if ( event
.m_item
.m_mask
& wxLIST_MASK_STATE
)
837 msg
<< _T(" wxLIST_MASK_STATE");
838 if ( event
.m_item
.m_mask
& wxLIST_MASK_TEXT
)
839 msg
<< _T(" wxLIST_MASK_TEXT");
840 if ( event
.m_item
.m_mask
& wxLIST_MASK_IMAGE
)
841 msg
<< _T(" wxLIST_MASK_IMAGE");
842 if ( event
.m_item
.m_mask
& wxLIST_MASK_DATA
)
843 msg
<< _T(" wxLIST_MASK_DATA");
844 if ( event
.m_item
.m_mask
& wxLIST_SET_ITEM
)
845 msg
<< _T(" wxLIST_SET_ITEM");
846 if ( event
.m_item
.m_mask
& wxLIST_MASK_WIDTH
)
847 msg
<< _T(" wxLIST_MASK_WIDTH");
848 if ( event
.m_item
.m_mask
& wxLIST_MASK_FORMAT
)
849 msg
<< _T(" wxLIST_MASK_WIDTH");
851 if ( event
.m_item
.m_mask
& wxLIST_MASK_TEXT
)
853 event
.m_item
.m_text
= _T("My callback text");
859 void MyListCtrl::OnSetInfo(wxListEvent
& event
)
861 LogEvent(event
, _T("OnSetInfo"));
865 void MyListCtrl::OnSelected(wxListEvent
& event
)
867 LogEvent(event
, _T("OnSelected"));
869 if ( GetWindowStyle() & wxLC_REPORT
)
872 info
.m_itemId
= event
.m_itemIndex
;
874 info
.m_mask
= wxLIST_MASK_TEXT
;
877 wxLogMessage(wxT("Value of the 2nd field of the selected item: %s"),
878 info
.m_text
.c_str());
882 wxFAIL_MSG(wxT("wxListCtrl::GetItem() failed"));
887 void MyListCtrl::OnDeselected(wxListEvent
& event
)
889 LogEvent(event
, _T("OnDeselected"));
892 void MyListCtrl::OnActivated(wxListEvent
& event
)
894 LogEvent(event
, _T("OnActivated"));
897 void MyListCtrl::OnFocused(wxListEvent
& event
)
899 LogEvent(event
, _T("OnFocused"));
904 void MyListCtrl::OnListKeyDown(wxListEvent
& event
)
908 switch ( event
.GetKeyCode() )
910 case 'c': // colorize
914 info
.m_itemId
= event
.GetIndex();
917 wxListItemAttr
*attr
= info
.GetAttributes();
918 if ( !attr
|| !attr
->HasTextColour() )
920 info
.SetTextColour(*wxCYAN
);
924 RefreshItem(info
.m_itemId
);
931 item
= GetNextItem(-1, wxLIST_NEXT_ALL
, wxLIST_STATE_FOCUSED
);
932 if ( item
++ == GetItemCount() - 1 )
937 wxLogMessage(_T("Focusing item %ld"), item
);
939 SetItemState(item
, wxLIST_STATE_FOCUSED
, wxLIST_STATE_FOCUSED
);
943 case 'r': // show bounding Rect
946 item
= event
.GetIndex();
948 if ( !GetItemRect(item
, r
) )
950 wxLogError(_T("Failed to retrieve rect of item %ld"), item
);
954 wxLogMessage(_T("Bounding rect of item %ld is (%d, %d)-(%d, %d)"),
955 item
, r
.x
, r
.y
, r
.x
+ r
.width
, r
.y
+ r
.height
);
960 item
= GetNextItem(-1, wxLIST_NEXT_ALL
, wxLIST_STATE_SELECTED
);
965 wxLogMessage(_T("Item %ld deleted"), item
);
967 // -1 because the indices were shifted by DeleteItem()
968 item
= GetNextItem(item
- 1,
969 wxLIST_NEXT_ALL
, wxLIST_STATE_SELECTED
);
974 if ( GetWindowStyle() & wxLC_REPORT
)
976 if ( GetWindowStyle() & wxLC_VIRTUAL
)
978 SetItemCount(GetItemCount() + 1);
982 InsertItemInReportView(event
.GetIndex());
988 LogEvent(event
, _T("OnListKeyDown"));
994 void MyListCtrl::OnChar(wxKeyEvent
& event
)
996 wxLogMessage(_T("Got char event."));
998 switch ( event
.GetKeyCode() )
1004 // these are the keys we process ourselves
1012 void MyListCtrl::LogEvent(const wxListEvent
& event
, const wxChar
*eventName
)
1014 wxLogMessage(_T("Item %ld: %s (item text = %s, data = %ld)"),
1015 event
.GetIndex(), eventName
,
1016 event
.GetText().c_str(), event
.GetData());
1019 wxString
MyListCtrl::OnGetItemText(long item
, long column
) const
1021 if ( GetItemCount() == WXSIZEOF(SMALL_VIRTUAL_VIEW_ITEMS
) )
1023 return SMALL_VIRTUAL_VIEW_ITEMS
[item
][column
];
1027 return wxString::Format(_T("Column %ld of item %ld"), column
, item
);
1031 int MyListCtrl::OnGetItemColumnImage(long item
, long column
) const
1036 if (!(item %3
) && column
== 1)
1042 wxListItemAttr
*MyListCtrl::OnGetItemAttr(long item
) const
1044 return item
% 2 ? NULL
: (wxListItemAttr
*)&m_attr
;
1047 void MyListCtrl::InsertItemInReportView(int i
)
1050 buf
.Printf(_T("This is item %d"), i
);
1051 long tmp
= InsertItem(i
, buf
, 0);
1052 SetItemData(tmp
, i
);
1054 buf
.Printf(_T("Col 1, item %d"), i
);
1057 buf
.Printf(_T("Item %d in column 2"), i
);
1061 #if USE_CONTEXT_MENU
1062 void MyListCtrl::OnContextMenu(wxContextMenuEvent
& event
)
1064 wxPoint point
= event
.GetPosition();
1066 if (point
.x
== -1 && point
.y
== -1) {
1067 wxSize size
= GetSize();
1068 point
.x
= size
.x
/ 2;
1069 point
.y
= size
.y
/ 2;
1071 point
= ScreenToClient(point
);
1073 ShowContextMenu(point
);
1077 void MyListCtrl::ShowContextMenu(const wxPoint
& pos
)
1081 menu
.Append(wxID_ABOUT
, _T("&About"));
1082 menu
.AppendSeparator();
1083 menu
.Append(wxID_EXIT
, _T("E&xit"));
1085 PopupMenu(&menu
, pos
.x
, pos
.y
);