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 const wxChar
*SMALL_VIRTUAL_VIEW_ITEMS
[][2] =
52 { _T("Cat"), _T("meow") },
53 { _T("Cow"), _T("moo") },
54 { _T("Crow"), _T("caw") },
55 { _T("Dog"), _T("woof") },
56 { _T("Duck"), _T("quack") },
57 { _T("Mouse"), _T("squeak") },
58 { _T("Owl"), _T("hoo") },
59 { _T("Pig"), _T("oink") },
60 { _T("Pigeon"), _T("coo") },
61 { _T("Sheep"), _T("baaah") },
65 BEGIN_EVENT_TABLE(MyFrame
, wxFrame
)
66 EVT_SIZE(MyFrame::OnSize
)
68 EVT_MENU(LIST_QUIT
, MyFrame::OnQuit
)
69 EVT_MENU(LIST_ABOUT
, MyFrame::OnAbout
)
70 EVT_MENU(LIST_LIST_VIEW
, MyFrame::OnListView
)
71 EVT_MENU(LIST_REPORT_VIEW
, MyFrame::OnReportView
)
72 EVT_MENU(LIST_ICON_VIEW
, MyFrame::OnIconView
)
73 EVT_MENU(LIST_ICON_TEXT_VIEW
, MyFrame::OnIconTextView
)
74 EVT_MENU(LIST_SMALL_ICON_VIEW
, MyFrame::OnSmallIconView
)
75 EVT_MENU(LIST_SMALL_ICON_TEXT_VIEW
, MyFrame::OnSmallIconTextView
)
76 EVT_MENU(LIST_VIRTUAL_VIEW
, MyFrame::OnVirtualView
)
77 EVT_MENU(LIST_SMALL_VIRTUAL_VIEW
, MyFrame::OnSmallVirtualView
)
79 EVT_MENU(LIST_FOCUS_LAST
, MyFrame::OnFocusLast
)
80 EVT_MENU(LIST_TOGGLE_FIRST
, MyFrame::OnToggleFirstSel
)
81 EVT_MENU(LIST_DESELECT_ALL
, MyFrame::OnDeselectAll
)
82 EVT_MENU(LIST_SELECT_ALL
, MyFrame::OnSelectAll
)
83 EVT_MENU(LIST_DELETE
, MyFrame::OnDelete
)
84 EVT_MENU(LIST_ADD
, MyFrame::OnAdd
)
85 EVT_MENU(LIST_EDIT
, MyFrame::OnEdit
)
86 EVT_MENU(LIST_DELETE_ALL
, MyFrame::OnDeleteAll
)
87 EVT_MENU(LIST_SORT
, MyFrame::OnSort
)
88 EVT_MENU(LIST_SET_FG_COL
, MyFrame::OnSetFgColour
)
89 EVT_MENU(LIST_SET_BG_COL
, MyFrame::OnSetBgColour
)
90 EVT_MENU(LIST_TOGGLE_MULTI_SEL
, MyFrame::OnToggleMultiSel
)
91 EVT_MENU(LIST_SHOW_COL_INFO
, MyFrame::OnShowColInfo
)
92 EVT_MENU(LIST_SHOW_SEL_INFO
, MyFrame::OnShowSelInfo
)
93 EVT_MENU(LIST_FREEZE
, MyFrame::OnFreeze
)
94 EVT_MENU(LIST_THAW
, MyFrame::OnThaw
)
95 EVT_MENU(LIST_TOGGLE_LINES
, MyFrame::OnToggleLines
)
97 EVT_UPDATE_UI(LIST_SHOW_COL_INFO
, MyFrame::OnUpdateShowColInfo
)
98 EVT_UPDATE_UI(LIST_TOGGLE_MULTI_SEL
, MyFrame::OnUpdateToggleMultiSel
)
101 BEGIN_EVENT_TABLE(MyListCtrl
, wxListCtrl
)
102 EVT_LIST_BEGIN_DRAG(LIST_CTRL
, MyListCtrl::OnBeginDrag
)
103 EVT_LIST_BEGIN_RDRAG(LIST_CTRL
, MyListCtrl::OnBeginRDrag
)
104 EVT_LIST_BEGIN_LABEL_EDIT(LIST_CTRL
, MyListCtrl::OnBeginLabelEdit
)
105 EVT_LIST_END_LABEL_EDIT(LIST_CTRL
, MyListCtrl::OnEndLabelEdit
)
106 EVT_LIST_DELETE_ITEM(LIST_CTRL
, MyListCtrl::OnDeleteItem
)
107 EVT_LIST_DELETE_ALL_ITEMS(LIST_CTRL
, MyListCtrl::OnDeleteAllItems
)
108 #if WXWIN_COMPATIBILITY_2_4
109 EVT_LIST_GET_INFO(LIST_CTRL
, MyListCtrl::OnGetInfo
)
110 EVT_LIST_SET_INFO(LIST_CTRL
, MyListCtrl::OnSetInfo
)
112 EVT_LIST_ITEM_SELECTED(LIST_CTRL
, MyListCtrl::OnSelected
)
113 EVT_LIST_ITEM_DESELECTED(LIST_CTRL
, MyListCtrl::OnDeselected
)
114 EVT_LIST_KEY_DOWN(LIST_CTRL
, MyListCtrl::OnListKeyDown
)
115 EVT_LIST_ITEM_ACTIVATED(LIST_CTRL
, MyListCtrl::OnActivated
)
116 EVT_LIST_ITEM_FOCUSED(LIST_CTRL
, MyListCtrl::OnFocused
)
118 EVT_LIST_COL_CLICK(LIST_CTRL
, MyListCtrl::OnColClick
)
119 EVT_LIST_COL_RIGHT_CLICK(LIST_CTRL
, MyListCtrl::OnColRightClick
)
120 EVT_LIST_COL_BEGIN_DRAG(LIST_CTRL
, MyListCtrl::OnColBeginDrag
)
121 EVT_LIST_COL_DRAGGING(LIST_CTRL
, MyListCtrl::OnColDragging
)
122 EVT_LIST_COL_END_DRAG(LIST_CTRL
, MyListCtrl::OnColEndDrag
)
124 EVT_LIST_CACHE_HINT(LIST_CTRL
, MyListCtrl::OnCacheHint
)
126 EVT_CHAR(MyListCtrl::OnChar
)
131 // number of items in list/report view
132 static const int NUM_ITEMS
= 30;
134 // number of items in icon/small icon view
135 static const int NUM_ICONS
= 9;
137 int wxCALLBACK
MyCompareFunction(long item1
, long item2
, long WXUNUSED(sortData
))
148 // `Main program' equivalent, creating windows and returning main app frame
151 // Create the main frame window
152 MyFrame
*frame
= new MyFrame(wxT("wxListCtrl Test"), 50, 50, 450, 340);
162 // My frame constructor
163 MyFrame::MyFrame(const wxChar
*title
, int x
, int y
, int w
, int h
)
164 : wxFrame(NULL
, wxID_ANY
, title
, wxPoint(x
, y
), wxSize(w
, h
))
168 m_smallVirtual
= false;
171 SetIcon( wxICON(mondrian
) );
173 // Make an image list containing large icons
174 m_imageListNormal
= new wxImageList(32, 32, true);
175 m_imageListSmall
= new wxImageList(16, 16, true);
178 m_imageListNormal
->Add( wxIcon(_T("icon1"), wxBITMAP_TYPE_ICO_RESOURCE
) );
179 m_imageListNormal
->Add( wxIcon(_T("icon2"), wxBITMAP_TYPE_ICO_RESOURCE
) );
180 m_imageListNormal
->Add( wxIcon(_T("icon3"), wxBITMAP_TYPE_ICO_RESOURCE
) );
181 m_imageListNormal
->Add( wxIcon(_T("icon4"), wxBITMAP_TYPE_ICO_RESOURCE
) );
182 m_imageListNormal
->Add( wxIcon(_T("icon5"), wxBITMAP_TYPE_ICO_RESOURCE
) );
183 m_imageListNormal
->Add( wxIcon(_T("icon6"), wxBITMAP_TYPE_ICO_RESOURCE
) );
184 m_imageListNormal
->Add( wxIcon(_T("icon7"), wxBITMAP_TYPE_ICO_RESOURCE
) );
185 m_imageListNormal
->Add( wxIcon(_T("icon8"), wxBITMAP_TYPE_ICO_RESOURCE
) );
186 m_imageListNormal
->Add( wxIcon(_T("icon9"), wxBITMAP_TYPE_ICO_RESOURCE
) );
188 m_imageListSmall
->Add( wxIcon(_T("iconsmall"), wxBITMAP_TYPE_ICO_RESOURCE
) );
191 m_imageListNormal
->Add( wxIcon( toolbrai_xpm
) );
192 m_imageListNormal
->Add( wxIcon( toolchar_xpm
) );
193 m_imageListNormal
->Add( wxIcon( tooldata_xpm
) );
194 m_imageListNormal
->Add( wxIcon( toolnote_xpm
) );
195 m_imageListNormal
->Add( wxIcon( tooltodo_xpm
) );
196 m_imageListNormal
->Add( wxIcon( toolchec_xpm
) );
197 m_imageListNormal
->Add( wxIcon( toolgame_xpm
) );
198 m_imageListNormal
->Add( wxIcon( tooltime_xpm
) );
199 m_imageListNormal
->Add( wxIcon( toolword_xpm
) );
201 m_imageListSmall
->Add( wxIcon( small1_xpm
) );
205 wxMenu
*menuFile
= new wxMenu
;
206 menuFile
->Append(LIST_ABOUT
, _T("&About"));
207 menuFile
->AppendSeparator();
208 menuFile
->Append(LIST_QUIT
, _T("E&xit\tAlt-X"));
210 wxMenu
*menuView
= new wxMenu
;
211 menuView
->Append(LIST_LIST_VIEW
, _T("&List view\tF1"));
212 menuView
->Append(LIST_REPORT_VIEW
, _T("&Report view\tF2"));
213 menuView
->Append(LIST_ICON_VIEW
, _T("&Icon view\tF3"));
214 menuView
->Append(LIST_ICON_TEXT_VIEW
, _T("Icon view with &text\tF4"));
215 menuView
->Append(LIST_SMALL_ICON_VIEW
, _T("&Small icon view\tF5"));
216 menuView
->Append(LIST_SMALL_ICON_TEXT_VIEW
, _T("Small icon &view with text\tF6"));
217 menuView
->Append(LIST_VIRTUAL_VIEW
, _T("&Virtual view\tF7"));
218 menuView
->Append(LIST_SMALL_VIRTUAL_VIEW
, _T("Small virtual vie&w\tF8"));
220 wxMenu
*menuList
= new wxMenu
;
221 menuList
->Append(LIST_FOCUS_LAST
, _T("&Make last item current\tCtrl-L"));
222 menuList
->Append(LIST_TOGGLE_FIRST
, _T("To&ggle first item\tCtrl-G"));
223 menuList
->Append(LIST_DESELECT_ALL
, _T("&Deselect All\tCtrl-D"));
224 menuList
->Append(LIST_SELECT_ALL
, _T("S&elect All\tCtrl-A"));
225 menuList
->AppendSeparator();
226 menuList
->Append(LIST_SHOW_COL_INFO
, _T("Show &column info\tCtrl-C"));
227 menuList
->Append(LIST_SHOW_SEL_INFO
, _T("Show &selected items\tCtrl-S"));
228 menuList
->AppendSeparator();
229 menuList
->Append(LIST_SORT
, _T("&Sort\tCtrl-S"));
230 menuList
->AppendSeparator();
231 menuList
->Append(LIST_ADD
, _T("&Append an item\tCtrl-P"));
232 menuList
->Append(LIST_EDIT
, _T("&Edit the item\tCtrl-E"));
233 menuList
->Append(LIST_DELETE
, _T("&Delete first item\tCtrl-X"));
234 menuList
->Append(LIST_DELETE_ALL
, _T("Delete &all items"));
235 menuList
->AppendSeparator();
236 menuList
->Append(LIST_FREEZE
, _T("Free&ze\tCtrl-Z"));
237 menuList
->Append(LIST_THAW
, _T("Tha&w\tCtrl-W"));
238 menuList
->AppendSeparator();
239 menuList
->AppendCheckItem(LIST_TOGGLE_LINES
, _T("Toggle &lines\tCtrl-I"));
240 menuList
->Append(LIST_TOGGLE_MULTI_SEL
, _T("&Multiple selection\tCtrl-M"),
241 _T("Toggle multiple selection"), true);
243 wxMenu
*menuCol
= new wxMenu
;
244 menuCol
->Append(LIST_SET_FG_COL
, _T("&Foreground colour..."));
245 menuCol
->Append(LIST_SET_BG_COL
, _T("&Background colour..."));
247 wxMenuBar
*menubar
= new wxMenuBar
;
248 menubar
->Append(menuFile
, _T("&File"));
249 menubar
->Append(menuView
, _T("&View"));
250 menubar
->Append(menuList
, _T("&List"));
251 menubar
->Append(menuCol
, _T("&Colour"));
254 m_panel
= new wxPanel(this, wxID_ANY
);
255 m_logWindow
= new wxTextCtrl(m_panel
, wxID_ANY
, wxEmptyString
,
256 wxDefaultPosition
, wxDefaultSize
,
257 wxTE_MULTILINE
| wxSUNKEN_BORDER
);
259 m_logOld
= wxLog::SetActiveTarget(new wxLogTextCtrl(m_logWindow
));
261 RecreateList(wxLC_REPORT
| wxLC_SINGLE_SEL
);
265 #endif // wxUSE_STATUSBAR
270 delete wxLog::SetActiveTarget(m_logOld
);
272 delete m_imageListNormal
;
273 delete m_imageListSmall
;
276 void MyFrame::OnSize(wxSizeEvent
& event
)
283 void MyFrame::DoSize()
288 wxSize size
= GetClientSize();
289 wxCoord y
= (2*size
.y
)/3;
290 m_listCtrl
->SetSize(0, 0, size
.x
, y
);
291 m_logWindow
->SetSize(0, y
+ 1, size
.x
, size
.y
- y
);
294 bool MyFrame::CheckNonVirtual() const
296 if ( !m_listCtrl
->HasFlag(wxLC_VIRTUAL
) )
299 // "this" == whatever
300 wxLogWarning(_T("Can't do this in virtual view, sorry."));
305 void MyFrame::OnQuit(wxCommandEvent
& WXUNUSED(event
))
310 void MyFrame::OnAbout(wxCommandEvent
& WXUNUSED(event
))
312 wxMessageDialog
dialog(this, _T("List test sample\nJulian Smart (c) 1997"),
313 _T("About list test"), wxOK
|wxCANCEL
);
318 void MyFrame::OnFreeze(wxCommandEvent
& WXUNUSED(event
))
320 wxLogMessage(_T("Freezing the control"));
322 m_listCtrl
->Freeze();
325 void MyFrame::OnThaw(wxCommandEvent
& WXUNUSED(event
))
327 wxLogMessage(_T("Thawing the control"));
332 void MyFrame::OnToggleLines(wxCommandEvent
& event
)
334 m_listCtrl
->SetSingleStyle(wxLC_HRULES
| wxLC_VRULES
, event
.IsChecked());
337 void MyFrame::OnFocusLast(wxCommandEvent
& WXUNUSED(event
))
339 long index
= m_listCtrl
->GetItemCount() - 1;
345 m_listCtrl
->SetItemState(index
, wxLIST_STATE_FOCUSED
, wxLIST_STATE_FOCUSED
);
346 m_listCtrl
->EnsureVisible(index
);
349 void MyFrame::OnToggleFirstSel(wxCommandEvent
& WXUNUSED(event
))
351 m_listCtrl
->SetItemState(0, (~m_listCtrl
->GetItemState(0, wxLIST_STATE_SELECTED
) ) & wxLIST_STATE_SELECTED
, wxLIST_STATE_SELECTED
);
354 void MyFrame::OnDeselectAll(wxCommandEvent
& WXUNUSED(event
))
356 if ( !CheckNonVirtual() )
359 int n
= m_listCtrl
->GetItemCount();
360 for (int i
= 0; i
< n
; i
++)
361 m_listCtrl
->SetItemState(i
,0,wxLIST_STATE_SELECTED
);
364 void MyFrame::OnSelectAll(wxCommandEvent
& WXUNUSED(event
))
366 if ( !CheckNonVirtual() )
369 int n
= m_listCtrl
->GetItemCount();
370 for (int i
= 0; i
< n
; i
++)
371 m_listCtrl
->SetItemState(i
,wxLIST_STATE_SELECTED
, wxLIST_STATE_SELECTED
);
374 // ----------------------------------------------------------------------------
375 // changing listctrl modes
376 // ----------------------------------------------------------------------------
378 void MyFrame::RecreateList(long flags
, bool withText
)
380 // we could avoid recreating it if we don't set/clear the wxLC_VIRTUAL
381 // style, but it is more trouble to do it than not
383 if ( !m_listCtrl
|| ((flags
& wxLC_VIRTUAL
) !=
384 (m_listCtrl
->GetWindowStyleFlag() & wxLC_VIRTUAL
)) )
389 m_listCtrl
= new MyListCtrl(m_panel
, LIST_CTRL
,
390 wxDefaultPosition
, wxDefaultSize
,
392 wxSUNKEN_BORDER
| wxLC_EDIT_LABELS
);
394 switch ( flags
& wxLC_MASK_TYPE
)
401 InitWithIconItems(withText
);
404 case wxLC_SMALL_ICON
:
405 InitWithIconItems(withText
, true);
409 if ( flags
& wxLC_VIRTUAL
)
410 InitWithVirtualItems();
412 InitWithReportItems();
416 wxFAIL_MSG( _T("unknown listctrl mode") );
422 m_logWindow
->Clear();
425 void MyFrame::OnListView(wxCommandEvent
& WXUNUSED(event
))
427 RecreateList(wxLC_LIST
);
430 void MyFrame::InitWithListItems()
432 for ( int i
= 0; i
< NUM_ITEMS
; i
++ )
434 m_listCtrl
->InsertItem(i
, wxString::Format(_T("Item %d"), i
));
438 void MyFrame::OnReportView(wxCommandEvent
& WXUNUSED(event
))
440 RecreateList(wxLC_REPORT
);
443 void MyFrame::InitWithReportItems()
445 m_listCtrl
->SetImageList(m_imageListSmall
, wxIMAGE_LIST_SMALL
);
447 // note that under MSW for SetColumnWidth() to work we need to create the
448 // items with images initially even if we specify dummy image id
450 itemCol
.SetText(_T("Column 1"));
451 itemCol
.SetImage(-1);
452 m_listCtrl
->InsertColumn(0, itemCol
);
454 itemCol
.SetText(_T("Column 2"));
455 itemCol
.SetAlign(wxLIST_FORMAT_CENTRE
);
456 m_listCtrl
->InsertColumn(1, itemCol
);
458 itemCol
.SetText(_T("Column 3"));
459 itemCol
.SetAlign(wxLIST_FORMAT_RIGHT
);
460 m_listCtrl
->InsertColumn(2, itemCol
);
462 // to speed up inserting we hide the control temporarily
467 for ( int i
= 0; i
< NUM_ITEMS
; i
++ )
469 m_listCtrl
->InsertItemInReportView(i
);
472 m_logWindow
->WriteText(wxString::Format(_T("%d items inserted in %ldms\n"),
473 NUM_ITEMS
, sw
.Time()));
476 // we leave all mask fields to 0 and only change the colour
479 item
.SetTextColour(*wxRED
);
480 m_listCtrl
->SetItem( item
);
483 item
.SetTextColour(*wxGREEN
);
484 m_listCtrl
->SetItem( item
);
486 item
.SetTextColour(*wxLIGHT_GREY
);
487 item
.SetFont(*wxITALIC_FONT
);
488 item
.SetBackgroundColour(*wxRED
);
489 m_listCtrl
->SetItem( item
);
491 m_listCtrl
->SetTextColour(*wxBLUE
);
492 m_listCtrl
->SetBackgroundColour(*wxLIGHT_GREY
);
494 m_listCtrl
->SetColumnWidth( 0, wxLIST_AUTOSIZE
);
495 m_listCtrl
->SetColumnWidth( 1, wxLIST_AUTOSIZE
);
496 m_listCtrl
->SetColumnWidth( 2, wxLIST_AUTOSIZE
);
499 void MyFrame::InitWithIconItems(bool withText
, bool sameIcon
)
501 m_listCtrl
->SetImageList(m_imageListNormal
, wxIMAGE_LIST_NORMAL
);
502 m_listCtrl
->SetImageList(m_imageListSmall
, wxIMAGE_LIST_SMALL
);
504 for ( int i
= 0; i
< NUM_ICONS
; i
++ )
506 int image
= sameIcon
? 0 : i
;
510 m_listCtrl
->InsertItem(i
, wxString::Format(_T("Label %d"), i
),
515 m_listCtrl
->InsertItem(i
, image
);
520 void MyFrame::OnIconView(wxCommandEvent
& WXUNUSED(event
))
522 RecreateList(wxLC_ICON
, false);
525 void MyFrame::OnIconTextView(wxCommandEvent
& WXUNUSED(event
))
527 RecreateList(wxLC_ICON
);
530 void MyFrame::OnSmallIconView(wxCommandEvent
& WXUNUSED(event
))
532 RecreateList(wxLC_SMALL_ICON
, false);
535 void MyFrame::OnSmallIconTextView(wxCommandEvent
& WXUNUSED(event
))
537 RecreateList(wxLC_SMALL_ICON
);
540 void MyFrame::OnVirtualView(wxCommandEvent
& WXUNUSED(event
))
542 m_smallVirtual
= false;
543 RecreateList(wxLC_REPORT
| wxLC_VIRTUAL
);
546 void MyFrame::OnSmallVirtualView(wxCommandEvent
& WXUNUSED(event
))
548 m_smallVirtual
= true;
549 RecreateList(wxLC_REPORT
| wxLC_VIRTUAL
);
552 void MyFrame::InitWithVirtualItems()
554 m_listCtrl
->SetImageList(m_imageListSmall
, wxIMAGE_LIST_SMALL
);
556 if ( m_smallVirtual
)
558 m_listCtrl
->InsertColumn(0, _T("Animal"));
559 m_listCtrl
->InsertColumn(1, _T("Sound"));
560 m_listCtrl
->SetItemCount(WXSIZEOF(SMALL_VIRTUAL_VIEW_ITEMS
));
564 m_listCtrl
->InsertColumn(0, _T("First Column"));
565 m_listCtrl
->InsertColumn(1, _T("Second Column"));
566 m_listCtrl
->SetColumnWidth(0, 150);
567 m_listCtrl
->SetColumnWidth(1, 150);
568 m_listCtrl
->SetItemCount(1000000);
572 void MyFrame::OnSort(wxCommandEvent
& WXUNUSED(event
))
576 m_listCtrl
->SortItems(MyCompareFunction
, 0);
578 m_logWindow
->WriteText(wxString::Format(_T("Sorting %d items took %ld ms\n"),
579 m_listCtrl
->GetItemCount(),
583 void MyFrame::OnShowSelInfo(wxCommandEvent
& WXUNUSED(event
))
585 int selCount
= m_listCtrl
->GetSelectedItemCount();
586 wxLogMessage(_T("%d items selected:"), selCount
);
588 // don't show too many items
589 size_t shownCount
= 0;
591 long item
= m_listCtrl
->GetNextItem(-1, wxLIST_NEXT_ALL
,
592 wxLIST_STATE_SELECTED
);
595 wxLogMessage(_T("\t%ld (%s)"),
596 item
, m_listCtrl
->GetItemText(item
).c_str());
598 if ( ++shownCount
> 10 )
600 wxLogMessage(_T("\t... more selected items snipped..."));
604 item
= m_listCtrl
->GetNextItem(item
, wxLIST_NEXT_ALL
,
605 wxLIST_STATE_SELECTED
);
609 void MyFrame::OnShowColInfo(wxCommandEvent
& WXUNUSED(event
))
611 int count
= m_listCtrl
->GetColumnCount();
612 wxLogMessage(wxT("%d columns:"), count
);
613 for ( int c
= 0; c
< count
; c
++ )
615 wxLogMessage(wxT("\tcolumn %d has width %d"), c
,
616 m_listCtrl
->GetColumnWidth(c
));
620 void MyFrame::OnUpdateShowColInfo(wxUpdateUIEvent
& event
)
622 event
.Enable( (m_listCtrl
->GetWindowStyleFlag() & wxLC_REPORT
) != 0 );
625 void MyFrame::OnToggleMultiSel(wxCommandEvent
& WXUNUSED(event
))
627 long flags
= m_listCtrl
->GetWindowStyleFlag();
628 if ( flags
& wxLC_SINGLE_SEL
)
629 flags
&= ~wxLC_SINGLE_SEL
;
631 flags
|= wxLC_SINGLE_SEL
;
633 m_logWindow
->WriteText(wxString::Format(wxT("Current selection mode: %sle\n"),
634 (flags
& wxLC_SINGLE_SEL
) ? _T("sing") : _T("multip")));
639 void MyFrame::OnUpdateToggleMultiSel(wxUpdateUIEvent
& event
)
641 event
.Check((m_listCtrl
->GetWindowStyleFlag() & wxLC_SINGLE_SEL
) == 0);
644 void MyFrame::OnSetFgColour(wxCommandEvent
& WXUNUSED(event
))
646 m_listCtrl
->SetForegroundColour(wxGetColourFromUser(this));
647 m_listCtrl
->Refresh();
650 void MyFrame::OnSetBgColour(wxCommandEvent
& WXUNUSED(event
))
652 m_listCtrl
->SetBackgroundColour(wxGetColourFromUser(this));
653 m_listCtrl
->Refresh();
656 void MyFrame::OnAdd(wxCommandEvent
& WXUNUSED(event
))
658 m_listCtrl
->InsertItem(m_listCtrl
->GetItemCount(), _T("Appended item"));
661 void MyFrame::OnEdit(wxCommandEvent
& WXUNUSED(event
))
663 long itemCur
= m_listCtrl
->GetNextItem(-1, wxLIST_NEXT_ALL
,
664 wxLIST_STATE_FOCUSED
);
668 m_listCtrl
->EditLabel(itemCur
);
672 m_logWindow
->WriteText(_T("No item to edit"));
676 void MyFrame::OnDelete(wxCommandEvent
& WXUNUSED(event
))
678 if ( m_listCtrl
->GetItemCount() )
680 m_listCtrl
->DeleteItem(0);
684 m_logWindow
->WriteText(_T("Nothing to delete"));
688 void MyFrame::OnDeleteAll(wxCommandEvent
& WXUNUSED(event
))
692 size_t itemCount
= m_listCtrl
->GetItemCount();
694 m_listCtrl
->DeleteAllItems();
696 m_logWindow
->WriteText(wxString::Format(_T("Deleting %d items took %ld ms\n"),
703 void MyListCtrl::OnCacheHint(wxListEvent
& event
)
705 wxLogMessage( wxT("OnCacheHint: cache items %ld..%ld"),
706 event
.GetCacheFrom(), event
.GetCacheTo() );
709 void MyListCtrl::SetColumnImage(int col
, int image
)
712 item
.SetMask(wxLIST_MASK_IMAGE
);
713 item
.SetImage(image
);
714 SetColumn(col
, item
);
717 void MyListCtrl::OnColClick(wxListEvent
& event
)
719 int col
= event
.GetColumn();
720 SetColumnImage(col
, 0);
722 wxLogMessage( wxT("OnColumnClick at %d."), col
);
725 void MyListCtrl::OnColRightClick(wxListEvent
& event
)
727 int col
= event
.GetColumn();
730 SetColumnImage(col
, -1);
733 // Show popupmenu at position
734 wxMenu
menu(wxT("Test"));
735 menu
.Append(LIST_ABOUT
, _T("&About"));
736 PopupMenu(&menu
, event
.GetPoint());
738 wxLogMessage( wxT("OnColumnRightClick at %d."), event
.GetColumn() );
741 void MyListCtrl::LogColEvent(const wxListEvent
& event
, const wxChar
*name
)
743 const int col
= event
.GetColumn();
745 wxLogMessage(wxT("%s: column %d (width = %d or %d)."),
748 event
.GetItem().GetWidth(),
749 GetColumnWidth(col
));
752 void MyListCtrl::OnColBeginDrag(wxListEvent
& event
)
754 LogColEvent( event
, wxT("OnColBeginDrag") );
756 if ( event
.GetColumn() == 0 )
758 wxLogMessage(_T("Resizing this column shouldn't work."));
764 void MyListCtrl::OnColDragging(wxListEvent
& event
)
766 LogColEvent( event
, wxT("OnColDragging") );
769 void MyListCtrl::OnColEndDrag(wxListEvent
& event
)
771 LogColEvent( event
, wxT("OnColEndDrag") );
774 void MyListCtrl::OnBeginDrag(wxListEvent
& event
)
776 const wxPoint
& pt
= event
.m_pointDrag
;
779 wxLogMessage( wxT("OnBeginDrag at (%d, %d), item %ld."),
780 pt
.x
, pt
.y
, HitTest(pt
, flags
) );
783 void MyListCtrl::OnBeginRDrag(wxListEvent
& event
)
785 wxLogMessage( wxT("OnBeginRDrag at %d,%d."),
786 event
.m_pointDrag
.x
, event
.m_pointDrag
.y
);
789 void MyListCtrl::OnBeginLabelEdit(wxListEvent
& event
)
791 wxLogMessage( wxT("OnBeginLabelEdit: %s"), event
.m_item
.m_text
.c_str());
794 void MyListCtrl::OnEndLabelEdit(wxListEvent
& event
)
796 wxLogMessage( wxT("OnEndLabelEdit: %s"),
797 event
.IsEditCancelled() ? _T("[cancelled]")
798 : event
.m_item
.m_text
.c_str());
801 void MyListCtrl::OnDeleteItem(wxListEvent
& event
)
803 LogEvent(event
, _T("OnDeleteItem"));
806 void MyListCtrl::OnDeleteAllItems(wxListEvent
& event
)
808 LogEvent(event
, _T("OnDeleteAllItems"));
811 #if WXWIN_COMPATIBILITY_2_4
812 void MyListCtrl::OnGetInfo(wxListEvent
& event
)
816 msg
<< _T("OnGetInfo (") << event
.m_item
.m_itemId
<< _T(", ") << event
.m_item
.m_col
<< _T(")");
817 if ( event
.m_item
.m_mask
& wxLIST_MASK_STATE
)
818 msg
<< _T(" wxLIST_MASK_STATE");
819 if ( event
.m_item
.m_mask
& wxLIST_MASK_TEXT
)
820 msg
<< _T(" wxLIST_MASK_TEXT");
821 if ( event
.m_item
.m_mask
& wxLIST_MASK_IMAGE
)
822 msg
<< _T(" wxLIST_MASK_IMAGE");
823 if ( event
.m_item
.m_mask
& wxLIST_MASK_DATA
)
824 msg
<< _T(" wxLIST_MASK_DATA");
825 if ( event
.m_item
.m_mask
& wxLIST_SET_ITEM
)
826 msg
<< _T(" wxLIST_SET_ITEM");
827 if ( event
.m_item
.m_mask
& wxLIST_MASK_WIDTH
)
828 msg
<< _T(" wxLIST_MASK_WIDTH");
829 if ( event
.m_item
.m_mask
& wxLIST_MASK_FORMAT
)
830 msg
<< _T(" wxLIST_MASK_WIDTH");
832 if ( event
.m_item
.m_mask
& wxLIST_MASK_TEXT
)
834 event
.m_item
.m_text
= _T("My callback text");
840 void MyListCtrl::OnSetInfo(wxListEvent
& event
)
842 LogEvent(event
, _T("OnSetInfo"));
846 void MyListCtrl::OnSelected(wxListEvent
& event
)
848 LogEvent(event
, _T("OnSelected"));
850 if ( GetWindowStyle() & wxLC_REPORT
)
853 info
.m_itemId
= event
.m_itemIndex
;
855 info
.m_mask
= wxLIST_MASK_TEXT
;
858 wxLogMessage(wxT("Value of the 2nd field of the selected item: %s"),
859 info
.m_text
.c_str());
863 wxFAIL_MSG(wxT("wxListCtrl::GetItem() failed"));
868 void MyListCtrl::OnDeselected(wxListEvent
& event
)
870 LogEvent(event
, _T("OnDeselected"));
873 void MyListCtrl::OnActivated(wxListEvent
& event
)
875 LogEvent(event
, _T("OnActivated"));
878 void MyListCtrl::OnFocused(wxListEvent
& event
)
880 LogEvent(event
, _T("OnFocused"));
885 void MyListCtrl::OnListKeyDown(wxListEvent
& event
)
889 switch ( event
.GetKeyCode() )
891 case 'c': // colorize
895 info
.m_itemId
= event
.GetIndex();
898 wxListItemAttr
*attr
= info
.GetAttributes();
899 if ( !attr
|| !attr
->HasTextColour() )
901 info
.SetTextColour(*wxCYAN
);
905 RefreshItem(info
.m_itemId
);
912 item
= GetNextItem(-1, wxLIST_NEXT_ALL
, wxLIST_STATE_FOCUSED
);
913 if ( item
++ == GetItemCount() - 1 )
918 wxLogMessage(_T("Focusing item %ld"), item
);
920 SetItemState(item
, wxLIST_STATE_FOCUSED
, wxLIST_STATE_FOCUSED
);
924 case 'r': // show bounding Rect
927 item
= event
.GetIndex();
929 if ( !GetItemRect(item
, r
) )
931 wxLogError(_T("Failed to retrieve rect of item %ld"), item
);
935 wxLogMessage(_T("Bounding rect of item %ld is (%d, %d)-(%d, %d)"),
936 item
, r
.x
, r
.y
, r
.x
+ r
.width
, r
.y
+ r
.height
);
941 item
= GetNextItem(-1, wxLIST_NEXT_ALL
, wxLIST_STATE_SELECTED
);
946 wxLogMessage(_T("Item %ld deleted"), item
);
948 // -1 because the indices were shifted by DeleteItem()
949 item
= GetNextItem(item
- 1,
950 wxLIST_NEXT_ALL
, wxLIST_STATE_SELECTED
);
955 if ( GetWindowStyle() & wxLC_REPORT
)
957 if ( GetWindowStyle() & wxLC_VIRTUAL
)
959 SetItemCount(GetItemCount() + 1);
963 InsertItemInReportView(event
.GetIndex());
969 LogEvent(event
, _T("OnListKeyDown"));
975 void MyListCtrl::OnChar(wxKeyEvent
& event
)
977 wxLogMessage(_T("Got char event."));
979 switch ( event
.GetKeyCode() )
985 // these are the keys we process ourselves
993 void MyListCtrl::LogEvent(const wxListEvent
& event
, const wxChar
*eventName
)
995 wxLogMessage(_T("Item %ld: %s (item text = %s, data = %ld)"),
996 event
.GetIndex(), eventName
,
997 event
.GetText().c_str(), event
.GetData());
1000 wxString
MyListCtrl::OnGetItemText(long item
, long column
) const
1002 if ( GetItemCount() == WXSIZEOF(SMALL_VIRTUAL_VIEW_ITEMS
) )
1004 return SMALL_VIRTUAL_VIEW_ITEMS
[item
][column
];
1008 return wxString::Format(_T("Column %ld of item %ld"), column
, item
);
1012 int MyListCtrl::OnGetItemImage(long WXUNUSED(item
)) const
1017 wxListItemAttr
*MyListCtrl::OnGetItemAttr(long item
) const
1019 return item
% 2 ? NULL
: (wxListItemAttr
*)&m_attr
;
1022 void MyListCtrl::InsertItemInReportView(int i
)
1025 buf
.Printf(_T("This is item %d"), i
);
1026 long tmp
= InsertItem(i
, buf
, 0);
1027 SetItemData(tmp
, i
);
1029 buf
.Printf(_T("Col 1, item %d"), i
);
1032 buf
.Printf(_T("Item %d in column 2"), i
);