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"
45 #include "wx/sysopt.h"
49 const wxChar
*SMALL_VIRTUAL_VIEW_ITEMS
[][2] =
51 { _T("Cat"), _T("meow") },
52 { _T("Cow"), _T("moo") },
53 { _T("Crow"), _T("caw") },
54 { _T("Dog"), _T("woof") },
55 { _T("Duck"), _T("quack") },
56 { _T("Mouse"), _T("squeak") },
57 { _T("Owl"), _T("hoo") },
58 { _T("Pig"), _T("oink") },
59 { _T("Pigeon"), _T("coo") },
60 { _T("Sheep"), _T("baaah") },
64 BEGIN_EVENT_TABLE(MyFrame
, wxFrame
)
65 EVT_SIZE(MyFrame::OnSize
)
67 EVT_MENU(LIST_QUIT
, MyFrame::OnQuit
)
68 EVT_MENU(LIST_ABOUT
, MyFrame::OnAbout
)
69 EVT_MENU(LIST_LIST_VIEW
, MyFrame::OnListView
)
70 EVT_MENU(LIST_REPORT_VIEW
, MyFrame::OnReportView
)
71 EVT_MENU(LIST_ICON_VIEW
, MyFrame::OnIconView
)
72 EVT_MENU(LIST_ICON_TEXT_VIEW
, MyFrame::OnIconTextView
)
73 EVT_MENU(LIST_SMALL_ICON_VIEW
, MyFrame::OnSmallIconView
)
74 EVT_MENU(LIST_SMALL_ICON_TEXT_VIEW
, MyFrame::OnSmallIconTextView
)
75 EVT_MENU(LIST_VIRTUAL_VIEW
, MyFrame::OnVirtualView
)
76 EVT_MENU(LIST_SMALL_VIRTUAL_VIEW
, MyFrame::OnSmallVirtualView
)
78 EVT_MENU(LIST_GOTO
, MyFrame::OnGoTo
)
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_SHOW_VIEW_RECT
, MyFrame::OnShowViewRect
)
94 EVT_MENU(LIST_FREEZE
, MyFrame::OnFreeze
)
95 EVT_MENU(LIST_THAW
, MyFrame::OnThaw
)
96 EVT_MENU(LIST_TOGGLE_LINES
, MyFrame::OnToggleLines
)
97 EVT_MENU(LIST_MAC_USE_GENERIC
, MyFrame::OnToggleMacUseGeneric
)
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 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
)
129 EVT_RIGHT_DOWN(MyListCtrl::OnRightClick
)
134 // number of items in list/report view
135 static const int NUM_ITEMS
= 10;
137 // number of items in icon/small icon view
138 static const int NUM_ICONS
= 9;
140 int wxCALLBACK
MyCompareFunction(long item1
, long item2
, long WXUNUSED(sortData
))
151 // `Main program' equivalent, creating windows and returning main app frame
154 if ( !wxApp::OnInit() )
157 // Create the main frame window
158 MyFrame
*frame
= new MyFrame(wxT("wxListCtrl Test"));
168 // My frame constructor
169 MyFrame::MyFrame(const wxChar
*title
)
170 : wxFrame(NULL
, wxID_ANY
, title
, wxDefaultPosition
, wxSize(600, 500))
174 m_smallVirtual
= false;
177 SetIcon( wxICON(mondrian
) );
179 // Make an image list containing large icons
180 m_imageListNormal
= new wxImageList(32, 32, true);
181 m_imageListSmall
= new wxImageList(16, 16, true);
184 m_imageListNormal
->Add( wxIcon(_T("icon1"), wxBITMAP_TYPE_ICO_RESOURCE
) );
185 m_imageListNormal
->Add( wxIcon(_T("icon2"), wxBITMAP_TYPE_ICO_RESOURCE
) );
186 m_imageListNormal
->Add( wxIcon(_T("icon3"), wxBITMAP_TYPE_ICO_RESOURCE
) );
187 m_imageListNormal
->Add( wxIcon(_T("icon4"), wxBITMAP_TYPE_ICO_RESOURCE
) );
188 m_imageListNormal
->Add( wxIcon(_T("icon5"), wxBITMAP_TYPE_ICO_RESOURCE
) );
189 m_imageListNormal
->Add( wxIcon(_T("icon6"), wxBITMAP_TYPE_ICO_RESOURCE
) );
190 m_imageListNormal
->Add( wxIcon(_T("icon7"), wxBITMAP_TYPE_ICO_RESOURCE
) );
191 m_imageListNormal
->Add( wxIcon(_T("icon8"), wxBITMAP_TYPE_ICO_RESOURCE
) );
192 m_imageListNormal
->Add( wxIcon(_T("icon9"), wxBITMAP_TYPE_ICO_RESOURCE
) );
194 m_imageListSmall
->Add( wxIcon(_T("iconsmall"), wxBITMAP_TYPE_ICO_RESOURCE
) );
197 m_imageListNormal
->Add( wxIcon( toolbrai_xpm
) );
198 m_imageListNormal
->Add( wxIcon( toolchar_xpm
) );
199 m_imageListNormal
->Add( wxIcon( tooldata_xpm
) );
200 m_imageListNormal
->Add( wxIcon( toolnote_xpm
) );
201 m_imageListNormal
->Add( wxIcon( tooltodo_xpm
) );
202 m_imageListNormal
->Add( wxIcon( toolchec_xpm
) );
203 m_imageListNormal
->Add( wxIcon( toolgame_xpm
) );
204 m_imageListNormal
->Add( wxIcon( tooltime_xpm
) );
205 m_imageListNormal
->Add( wxIcon( toolword_xpm
) );
207 m_imageListSmall
->Add( wxIcon( small1_xpm
) );
211 wxMenu
*menuFile
= new wxMenu
;
212 menuFile
->Append(LIST_ABOUT
, _T("&About"));
213 menuFile
->AppendSeparator();
214 menuFile
->Append(LIST_QUIT
, _T("E&xit\tAlt-X"));
216 wxMenu
*menuView
= new wxMenu
;
217 menuView
->Append(LIST_LIST_VIEW
, _T("&List view\tF1"));
218 menuView
->Append(LIST_REPORT_VIEW
, _T("&Report view\tF2"));
219 menuView
->Append(LIST_ICON_VIEW
, _T("&Icon view\tF3"));
220 menuView
->Append(LIST_ICON_TEXT_VIEW
, _T("Icon view with &text\tF4"));
221 menuView
->Append(LIST_SMALL_ICON_VIEW
, _T("&Small icon view\tF5"));
222 menuView
->Append(LIST_SMALL_ICON_TEXT_VIEW
, _T("Small icon &view with text\tF6"));
223 menuView
->Append(LIST_VIRTUAL_VIEW
, _T("&Virtual view\tF7"));
224 menuView
->Append(LIST_SMALL_VIRTUAL_VIEW
, _T("Small virtual vie&w\tF8"));
226 menuView
->AppendCheckItem(LIST_MAC_USE_GENERIC
, _T("Mac: Use Generic Control"));
229 wxMenu
*menuList
= new wxMenu
;
230 menuList
->Append(LIST_GOTO
, _T("&Go to item #3\tCtrl-3"));
231 menuList
->Append(LIST_FOCUS_LAST
, _T("&Make last item current\tCtrl-L"));
232 menuList
->Append(LIST_TOGGLE_FIRST
, _T("To&ggle first item\tCtrl-G"));
233 menuList
->Append(LIST_DESELECT_ALL
, _T("&Deselect All\tCtrl-D"));
234 menuList
->Append(LIST_SELECT_ALL
, _T("S&elect All\tCtrl-A"));
235 menuList
->AppendSeparator();
236 menuList
->Append(LIST_SHOW_COL_INFO
, _T("Show &column info\tCtrl-C"));
237 menuList
->Append(LIST_SHOW_SEL_INFO
, _T("Show &selected items\tCtrl-S"));
238 menuList
->Append(LIST_SHOW_VIEW_RECT
, _T("Show &view rect"));
239 menuList
->AppendSeparator();
240 menuList
->Append(LIST_SORT
, _T("Sor&t\tCtrl-T"));
241 menuList
->AppendSeparator();
242 menuList
->Append(LIST_ADD
, _T("&Append an item\tCtrl-P"));
243 menuList
->Append(LIST_EDIT
, _T("&Edit the item\tCtrl-E"));
244 menuList
->Append(LIST_DELETE
, _T("&Delete first item\tCtrl-X"));
245 menuList
->Append(LIST_DELETE_ALL
, _T("Delete &all items"));
246 menuList
->AppendSeparator();
247 menuList
->Append(LIST_FREEZE
, _T("Free&ze\tCtrl-Z"));
248 menuList
->Append(LIST_THAW
, _T("Tha&w\tCtrl-W"));
249 menuList
->AppendSeparator();
250 menuList
->AppendCheckItem(LIST_TOGGLE_LINES
, _T("Toggle &lines\tCtrl-I"));
251 menuList
->Append(LIST_TOGGLE_MULTI_SEL
, _T("&Multiple selection\tCtrl-M"),
252 _T("Toggle multiple selection"), true);
254 wxMenu
*menuCol
= new wxMenu
;
255 menuCol
->Append(LIST_SET_FG_COL
, _T("&Foreground colour..."));
256 menuCol
->Append(LIST_SET_BG_COL
, _T("&Background colour..."));
258 wxMenuBar
*menubar
= new wxMenuBar
;
259 menubar
->Append(menuFile
, _T("&File"));
260 menubar
->Append(menuView
, _T("&View"));
261 menubar
->Append(menuList
, _T("&List"));
262 menubar
->Append(menuCol
, _T("&Colour"));
265 m_panel
= new wxPanel(this, wxID_ANY
);
266 m_logWindow
= new wxTextCtrl(m_panel
, wxID_ANY
, wxEmptyString
,
267 wxDefaultPosition
, wxDefaultSize
,
268 wxTE_READONLY
| wxTE_MULTILINE
| wxSUNKEN_BORDER
);
270 m_logOld
= wxLog::SetActiveTarget(new wxLogTextCtrl(m_logWindow
));
272 RecreateList(wxLC_REPORT
| wxLC_SINGLE_SEL
);
276 #endif // wxUSE_STATUSBAR
281 delete wxLog::SetActiveTarget(m_logOld
);
283 delete m_imageListNormal
;
284 delete m_imageListSmall
;
287 void MyFrame::OnSize(wxSizeEvent
& event
)
294 void MyFrame::DoSize()
299 wxSize size
= GetClientSize();
300 wxCoord y
= (2*size
.y
)/3;
301 m_listCtrl
->SetSize(0, 0, size
.x
, y
);
302 m_logWindow
->SetSize(0, y
+ 1, size
.x
, size
.y
- y
);
305 bool MyFrame::CheckNonVirtual() const
307 if ( !m_listCtrl
->HasFlag(wxLC_VIRTUAL
) )
310 // "this" == whatever
311 wxLogWarning(_T("Can't do this in virtual view, sorry."));
316 void MyFrame::OnQuit(wxCommandEvent
& WXUNUSED(event
))
321 void MyFrame::OnAbout(wxCommandEvent
& WXUNUSED(event
))
323 wxMessageDialog
dialog(this, _T("List test sample\nJulian Smart (c) 1997"),
324 _T("About list test"), wxOK
|wxCANCEL
);
329 void MyFrame::OnFreeze(wxCommandEvent
& WXUNUSED(event
))
331 wxLogMessage(_T("Freezing the control"));
333 m_listCtrl
->Freeze();
336 void MyFrame::OnThaw(wxCommandEvent
& WXUNUSED(event
))
338 wxLogMessage(_T("Thawing the control"));
343 void MyFrame::OnToggleLines(wxCommandEvent
& event
)
345 m_listCtrl
->SetSingleStyle(wxLC_HRULES
| wxLC_VRULES
, event
.IsChecked());
348 void MyFrame::OnToggleMacUseGeneric(wxCommandEvent
& event
)
350 wxSystemOptions::SetOption(wxT("mac.listctrl.always_use_generic"), event
.IsChecked());
353 void MyFrame::OnGoTo(wxCommandEvent
& WXUNUSED(event
))
356 m_listCtrl
->SetItemState(index
, wxLIST_STATE_FOCUSED
, wxLIST_STATE_FOCUSED
);
358 long sel
= m_listCtrl
->GetNextItem(-1, wxLIST_NEXT_ALL
,
359 wxLIST_STATE_SELECTED
);
361 m_listCtrl
->SetItemState(sel
, 0, wxLIST_STATE_SELECTED
);
362 m_listCtrl
->SetItemState(index
, wxLIST_STATE_SELECTED
, wxLIST_STATE_SELECTED
);
365 void MyFrame::OnFocusLast(wxCommandEvent
& WXUNUSED(event
))
367 long index
= m_listCtrl
->GetItemCount() - 1;
373 m_listCtrl
->SetItemState(index
, wxLIST_STATE_FOCUSED
, wxLIST_STATE_FOCUSED
);
374 m_listCtrl
->EnsureVisible(index
);
377 void MyFrame::OnToggleFirstSel(wxCommandEvent
& WXUNUSED(event
))
379 m_listCtrl
->SetItemState(0, (~m_listCtrl
->GetItemState(0, wxLIST_STATE_SELECTED
) ) & wxLIST_STATE_SELECTED
, wxLIST_STATE_SELECTED
);
382 void MyFrame::OnDeselectAll(wxCommandEvent
& WXUNUSED(event
))
384 if ( !CheckNonVirtual() )
387 int n
= m_listCtrl
->GetItemCount();
388 for (int i
= 0; i
< n
; i
++)
389 m_listCtrl
->SetItemState(i
,0,wxLIST_STATE_SELECTED
);
392 void MyFrame::OnSelectAll(wxCommandEvent
& WXUNUSED(event
))
394 if ( !CheckNonVirtual() )
397 int n
= m_listCtrl
->GetItemCount();
398 for (int i
= 0; i
< n
; i
++)
399 m_listCtrl
->SetItemState(i
,wxLIST_STATE_SELECTED
, wxLIST_STATE_SELECTED
);
402 // ----------------------------------------------------------------------------
403 // changing listctrl modes
404 // ----------------------------------------------------------------------------
406 void MyFrame::RecreateList(long flags
, bool withText
)
408 // we could avoid recreating it if we don't set/clear the wxLC_VIRTUAL
409 // style, but it is more trouble to do it than not
411 if ( !m_listCtrl
|| ((flags
& wxLC_VIRTUAL
) !=
412 (m_listCtrl
->GetWindowStyleFlag() & wxLC_VIRTUAL
)) )
417 m_listCtrl
= new MyListCtrl(m_panel
, LIST_CTRL
,
418 wxDefaultPosition
, wxDefaultSize
,
420 wxSUNKEN_BORDER
| wxLC_EDIT_LABELS
);
422 switch ( flags
& wxLC_MASK_TYPE
)
429 InitWithIconItems(withText
);
432 case wxLC_SMALL_ICON
:
433 InitWithIconItems(withText
, true);
437 if ( flags
& wxLC_VIRTUAL
)
438 InitWithVirtualItems();
440 InitWithReportItems();
444 wxFAIL_MSG( _T("unknown listctrl mode") );
450 m_logWindow
->Clear();
453 void MyFrame::OnListView(wxCommandEvent
& WXUNUSED(event
))
455 RecreateList(wxLC_LIST
);
458 void MyFrame::InitWithListItems()
460 for ( int i
= 0; i
< NUM_ITEMS
; i
++ )
462 m_listCtrl
->InsertItem(i
, wxString::Format(_T("Item %d"), i
));
466 void MyFrame::OnReportView(wxCommandEvent
& WXUNUSED(event
))
468 RecreateList(wxLC_REPORT
);
471 void MyFrame::InitWithReportItems()
473 m_listCtrl
->SetImageList(m_imageListSmall
, wxIMAGE_LIST_SMALL
);
475 // note that under MSW for SetColumnWidth() to work we need to create the
476 // items with images initially even if we specify dummy image id
478 itemCol
.SetText(_T("Column 1"));
479 itemCol
.SetImage(-1);
480 m_listCtrl
->InsertColumn(0, itemCol
);
482 itemCol
.SetText(_T("Column 2"));
483 itemCol
.SetAlign(wxLIST_FORMAT_CENTRE
);
484 m_listCtrl
->InsertColumn(1, itemCol
);
486 itemCol
.SetText(_T("Column 3"));
487 itemCol
.SetAlign(wxLIST_FORMAT_RIGHT
);
488 m_listCtrl
->InsertColumn(2, itemCol
);
490 // to speed up inserting we hide the control temporarily
495 for ( int i
= 0; i
< NUM_ITEMS
; i
++ )
497 m_listCtrl
->InsertItemInReportView(i
);
500 m_logWindow
->WriteText(wxString::Format(_T("%d items inserted in %ldms\n"),
501 NUM_ITEMS
, sw
.Time()));
504 // we leave all mask fields to 0 and only change the colour
507 item
.SetTextColour(*wxRED
);
508 m_listCtrl
->SetItem( item
);
511 item
.SetTextColour(*wxGREEN
);
512 m_listCtrl
->SetItem( item
);
514 item
.SetTextColour(*wxLIGHT_GREY
);
515 item
.SetFont(*wxITALIC_FONT
);
516 item
.SetBackgroundColour(*wxRED
);
517 m_listCtrl
->SetItem( item
);
519 m_listCtrl
->SetTextColour(*wxBLUE
);
521 m_listCtrl
->SetColumnWidth( 0, wxLIST_AUTOSIZE
);
522 m_listCtrl
->SetColumnWidth( 1, wxLIST_AUTOSIZE
);
523 m_listCtrl
->SetColumnWidth( 2, wxLIST_AUTOSIZE
);
525 // Set images in columns
526 m_listCtrl
->SetItemColumnImage(1, 1, 0);
532 m_listCtrl
->SetItem(info
);
534 // test SetItemFont too
535 m_listCtrl
->SetItemFont(0, *wxITALIC_FONT
);
538 void MyFrame::InitWithIconItems(bool withText
, bool sameIcon
)
540 m_listCtrl
->SetImageList(m_imageListNormal
, wxIMAGE_LIST_NORMAL
);
541 m_listCtrl
->SetImageList(m_imageListSmall
, wxIMAGE_LIST_SMALL
);
543 for ( int i
= 0; i
< NUM_ICONS
; i
++ )
545 int image
= sameIcon
? 0 : i
;
549 m_listCtrl
->InsertItem(i
, wxString::Format(_T("Label %d"), i
),
554 m_listCtrl
->InsertItem(i
, image
);
559 void MyFrame::OnIconView(wxCommandEvent
& WXUNUSED(event
))
561 RecreateList(wxLC_ICON
, false);
564 void MyFrame::OnIconTextView(wxCommandEvent
& WXUNUSED(event
))
566 RecreateList(wxLC_ICON
);
569 void MyFrame::OnSmallIconView(wxCommandEvent
& WXUNUSED(event
))
571 RecreateList(wxLC_SMALL_ICON
, false);
574 void MyFrame::OnSmallIconTextView(wxCommandEvent
& WXUNUSED(event
))
576 RecreateList(wxLC_SMALL_ICON
);
579 void MyFrame::OnVirtualView(wxCommandEvent
& WXUNUSED(event
))
581 m_smallVirtual
= false;
582 RecreateList(wxLC_REPORT
| wxLC_VIRTUAL
);
585 void MyFrame::OnSmallVirtualView(wxCommandEvent
& WXUNUSED(event
))
587 m_smallVirtual
= true;
588 RecreateList(wxLC_REPORT
| wxLC_VIRTUAL
);
591 void MyFrame::InitWithVirtualItems()
593 m_listCtrl
->SetImageList(m_imageListSmall
, wxIMAGE_LIST_SMALL
);
595 if ( m_smallVirtual
)
597 m_listCtrl
->InsertColumn(0, _T("Animal"));
598 m_listCtrl
->InsertColumn(1, _T("Sound"));
599 m_listCtrl
->SetItemCount(WXSIZEOF(SMALL_VIRTUAL_VIEW_ITEMS
));
603 m_listCtrl
->InsertColumn(0, _T("First Column"));
604 m_listCtrl
->InsertColumn(1, _T("Second Column"));
605 m_listCtrl
->SetColumnWidth(0, 150);
606 m_listCtrl
->SetColumnWidth(1, 150);
607 m_listCtrl
->SetItemCount(1000000);
611 void MyFrame::OnSort(wxCommandEvent
& WXUNUSED(event
))
615 m_listCtrl
->SortItems(MyCompareFunction
, 0);
617 m_logWindow
->WriteText(wxString::Format(_T("Sorting %d items took %ld ms\n"),
618 m_listCtrl
->GetItemCount(),
622 void MyFrame::OnShowSelInfo(wxCommandEvent
& WXUNUSED(event
))
624 int selCount
= m_listCtrl
->GetSelectedItemCount();
625 wxLogMessage(_T("%d items selected:"), selCount
);
627 // don't show too many items
628 size_t shownCount
= 0;
630 long item
= m_listCtrl
->GetNextItem(-1, wxLIST_NEXT_ALL
,
631 wxLIST_STATE_SELECTED
);
634 wxLogMessage(_T("\t%ld (%s)"),
635 item
, m_listCtrl
->GetItemText(item
).c_str());
637 if ( ++shownCount
> 10 )
639 wxLogMessage(_T("\t... more selected items snipped..."));
643 item
= m_listCtrl
->GetNextItem(item
, wxLIST_NEXT_ALL
,
644 wxLIST_STATE_SELECTED
);
648 void MyFrame::OnShowViewRect(wxCommandEvent
& WXUNUSED(event
))
650 const wxRect r
= m_listCtrl
->GetViewRect();
651 wxLogMessage("View rect: (%d, %d)-(%d, %d)",
652 r
.GetLeft(), r
.GetTop(), r
.GetRight(), r
.GetBottom());
655 void MyFrame::OnShowColInfo(wxCommandEvent
& WXUNUSED(event
))
657 int count
= m_listCtrl
->GetColumnCount();
658 wxLogMessage(wxT("%d columns:"), count
);
659 for ( int c
= 0; c
< count
; c
++ )
661 wxLogMessage(wxT("\tcolumn %d has width %d"), c
,
662 m_listCtrl
->GetColumnWidth(c
));
666 void MyFrame::OnUpdateShowColInfo(wxUpdateUIEvent
& event
)
668 event
.Enable( (m_listCtrl
->GetWindowStyleFlag() & wxLC_REPORT
) != 0 );
671 void MyFrame::OnToggleMultiSel(wxCommandEvent
& WXUNUSED(event
))
673 long flags
= m_listCtrl
->GetWindowStyleFlag();
674 if ( flags
& wxLC_SINGLE_SEL
)
675 flags
&= ~wxLC_SINGLE_SEL
;
677 flags
|= wxLC_SINGLE_SEL
;
679 m_logWindow
->WriteText(wxString::Format(wxT("Current selection mode: %sle\n"),
680 (flags
& wxLC_SINGLE_SEL
) ? _T("sing") : _T("multip")));
685 void MyFrame::OnUpdateToggleMultiSel(wxUpdateUIEvent
& event
)
687 event
.Check((m_listCtrl
->GetWindowStyleFlag() & wxLC_SINGLE_SEL
) == 0);
690 void MyFrame::OnSetFgColour(wxCommandEvent
& WXUNUSED(event
))
692 m_listCtrl
->SetForegroundColour(wxGetColourFromUser(this));
693 m_listCtrl
->Refresh();
696 void MyFrame::OnSetBgColour(wxCommandEvent
& WXUNUSED(event
))
698 m_listCtrl
->SetBackgroundColour(wxGetColourFromUser(this));
699 m_listCtrl
->Refresh();
702 void MyFrame::OnAdd(wxCommandEvent
& WXUNUSED(event
))
704 m_listCtrl
->InsertItem(m_listCtrl
->GetItemCount(), _T("Appended item"));
707 void MyFrame::OnEdit(wxCommandEvent
& WXUNUSED(event
))
709 long itemCur
= m_listCtrl
->GetNextItem(-1, wxLIST_NEXT_ALL
,
710 wxLIST_STATE_FOCUSED
);
714 m_listCtrl
->EditLabel(itemCur
);
718 m_logWindow
->WriteText(_T("No item to edit"));
722 void MyFrame::OnDelete(wxCommandEvent
& WXUNUSED(event
))
724 if ( m_listCtrl
->GetItemCount() )
726 m_listCtrl
->DeleteItem(0);
730 m_logWindow
->WriteText(_T("Nothing to delete"));
734 void MyFrame::OnDeleteAll(wxCommandEvent
& WXUNUSED(event
))
738 int itemCount
= m_listCtrl
->GetItemCount();
740 m_listCtrl
->DeleteAllItems();
742 m_logWindow
->WriteText(wxString::Format(_T("Deleting %d items took %ld ms\n"),
749 void MyListCtrl::OnCacheHint(wxListEvent
& event
)
751 wxLogMessage( wxT("OnCacheHint: cache items %ld..%ld"),
752 event
.GetCacheFrom(), event
.GetCacheTo() );
755 void MyListCtrl::SetColumnImage(int col
, int image
)
758 item
.SetMask(wxLIST_MASK_IMAGE
);
759 item
.SetImage(image
);
760 SetColumn(col
, item
);
763 void MyListCtrl::OnColClick(wxListEvent
& event
)
765 int col
= event
.GetColumn();
767 // set or unset image
768 static bool x
= false;
770 SetColumnImage(col
, x
? 0 : -1);
772 wxLogMessage( wxT("OnColumnClick at %d."), col
);
775 void MyListCtrl::OnColRightClick(wxListEvent
& event
)
777 int col
= event
.GetColumn();
780 SetColumnImage(col
, -1);
783 // Show popupmenu at position
784 wxMenu
menu(wxT("Test"));
785 menu
.Append(LIST_ABOUT
, _T("&About"));
786 PopupMenu(&menu
, event
.GetPoint());
788 wxLogMessage( wxT("OnColumnRightClick at %d."), event
.GetColumn() );
791 void MyListCtrl::LogColEvent(const wxListEvent
& event
, const wxChar
*name
)
793 const int col
= event
.GetColumn();
795 wxLogMessage(wxT("%s: column %d (width = %d or %d)."),
798 event
.GetItem().GetWidth(),
799 GetColumnWidth(col
));
802 void MyListCtrl::OnColBeginDrag(wxListEvent
& event
)
804 LogColEvent( event
, wxT("OnColBeginDrag") );
806 if ( event
.GetColumn() == 0 )
808 wxLogMessage(_T("Resizing this column shouldn't work."));
814 void MyListCtrl::OnColDragging(wxListEvent
& event
)
816 LogColEvent( event
, wxT("OnColDragging") );
819 void MyListCtrl::OnColEndDrag(wxListEvent
& event
)
821 LogColEvent( event
, wxT("OnColEndDrag") );
824 void MyListCtrl::OnBeginDrag(wxListEvent
& event
)
826 const wxPoint
& pt
= event
.m_pointDrag
;
829 wxLogMessage( wxT("OnBeginDrag at (%d, %d), item %ld."),
830 pt
.x
, pt
.y
, HitTest(pt
, flags
) );
833 void MyListCtrl::OnBeginRDrag(wxListEvent
& event
)
835 wxLogMessage( wxT("OnBeginRDrag at %d,%d."),
836 event
.m_pointDrag
.x
, event
.m_pointDrag
.y
);
839 void MyListCtrl::OnBeginLabelEdit(wxListEvent
& event
)
841 wxLogMessage( wxT("OnBeginLabelEdit: %s"), event
.m_item
.m_text
.c_str());
843 wxTextCtrl
* const text
= GetEditControl();
846 wxLogMessage("BUG: started to edit but no edit control");
850 wxLogMessage("Edit control value: \"%s\"", text
->GetValue());
854 void MyListCtrl::OnEndLabelEdit(wxListEvent
& event
)
856 wxLogMessage( wxT("OnEndLabelEdit: %s"),
858 event
.IsEditCancelled() ?
859 wxString("[cancelled]") :
865 void MyListCtrl::OnDeleteItem(wxListEvent
& event
)
867 LogEvent(event
, _T("OnDeleteItem"));
868 wxLogMessage( wxT("Number of items when delete event is sent: %d"), GetItemCount() );
871 void MyListCtrl::OnDeleteAllItems(wxListEvent
& event
)
873 LogEvent(event
, _T("OnDeleteAllItems"));
876 void MyListCtrl::OnSelected(wxListEvent
& event
)
878 LogEvent(event
, _T("OnSelected"));
880 if ( GetWindowStyle() & wxLC_REPORT
)
883 info
.m_itemId
= event
.m_itemIndex
;
885 info
.m_mask
= wxLIST_MASK_TEXT
;
888 wxLogMessage(wxT("Value of the 2nd field of the selected item: %s"),
889 info
.m_text
.c_str());
893 wxFAIL_MSG(wxT("wxListCtrl::GetItem() failed"));
898 void MyListCtrl::OnDeselected(wxListEvent
& event
)
900 LogEvent(event
, _T("OnDeselected"));
903 void MyListCtrl::OnActivated(wxListEvent
& event
)
905 LogEvent(event
, _T("OnActivated"));
908 void MyListCtrl::OnFocused(wxListEvent
& event
)
910 LogEvent(event
, _T("OnFocused"));
915 void MyListCtrl::OnListKeyDown(wxListEvent
& event
)
919 switch ( event
.GetKeyCode() )
921 case 'C': // colorize
924 info
.m_itemId
= event
.GetIndex();
925 if ( info
.m_itemId
== -1 )
933 wxListItemAttr
*attr
= info
.GetAttributes();
934 if ( !attr
|| !attr
->HasTextColour() )
936 info
.SetTextColour(*wxCYAN
);
940 RefreshItem(info
.m_itemId
);
946 item
= GetNextItem(-1, wxLIST_NEXT_ALL
, wxLIST_STATE_FOCUSED
);
947 if ( item
++ == GetItemCount() - 1 )
952 wxLogMessage(_T("Focusing item %ld"), item
);
954 SetItemState(item
, wxLIST_STATE_FOCUSED
, wxLIST_STATE_FOCUSED
);
958 case 'R': // show bounding rectangle
960 item
= event
.GetIndex();
962 if ( !GetItemRect(item
, r
) )
964 wxLogError(_T("Failed to retrieve rect of item %ld"), item
);
968 wxLogMessage(_T("Bounding rect of item %ld is (%d, %d)-(%d, %d)"),
969 item
, r
.x
, r
.y
, r
.x
+ r
.width
, r
.y
+ r
.height
);
973 case '1': // show sub item bounding rectangle
976 case '4': // this column is invalid but we want to test it too
977 if ( InReportView() )
979 int subItem
= event
.GetKeyCode() - '1';
980 item
= event
.GetIndex();
982 if ( !GetSubItemRect(item
, subItem
, r
) )
984 wxLogError(_T("Failed to retrieve rect of item %ld column %d"), item
, subItem
+ 1);
988 wxLogMessage(_T("Bounding rect of item %ld column %d is (%d, %d)-(%d, %d)"),
990 r
.x
, r
.y
, r
.x
+ r
.width
, r
.y
+ r
.height
);
998 if ( m_updated
!= -1 )
999 RefreshItem(m_updated
);
1001 m_updated
= event
.GetIndex();
1002 if ( m_updated
!= -1 )
1004 // we won't see changes to this item as it's selected, update
1005 // the next one (or the first one if we're on the last item)
1006 if ( ++m_updated
== GetItemCount() )
1009 wxLogMessage("Updating colour of the item %ld", m_updated
);
1010 RefreshItem(m_updated
);
1015 item
= GetNextItem(-1, wxLIST_NEXT_ALL
, wxLIST_STATE_SELECTED
);
1016 while ( item
!= -1 )
1020 wxLogMessage(_T("Item %ld deleted"), item
);
1022 // -1 because the indices were shifted by DeleteItem()
1023 item
= GetNextItem(item
- 1,
1024 wxLIST_NEXT_ALL
, wxLIST_STATE_SELECTED
);
1029 if ( GetWindowStyle() & wxLC_REPORT
)
1031 if ( GetWindowStyle() & wxLC_VIRTUAL
)
1033 SetItemCount(GetItemCount() + 1);
1037 InsertItemInReportView(event
.GetIndex());
1040 //else: fall through
1043 LogEvent(event
, _T("OnListKeyDown"));
1049 void MyListCtrl::OnChar(wxKeyEvent
& event
)
1051 wxLogMessage(_T("Got char event."));
1053 switch ( event
.GetKeyCode() )
1067 // these are the keys we process ourselves
1075 void MyListCtrl::OnRightClick(wxMouseEvent
& event
)
1077 if ( !event
.ControlDown() )
1085 long item
= HitTest(event
.GetPosition(), flags
, &subitem
);
1090 case wxLIST_HITTEST_ABOVE
: where
= _T("above"); break;
1091 case wxLIST_HITTEST_BELOW
: where
= _T("below"); break;
1092 case wxLIST_HITTEST_NOWHERE
: where
= _T("nowhere near"); break;
1093 case wxLIST_HITTEST_ONITEMICON
: where
= _T("on icon of"); break;
1094 case wxLIST_HITTEST_ONITEMLABEL
: where
= _T("on label of"); break;
1095 case wxLIST_HITTEST_ONITEMRIGHT
: where
= _T("right on"); break;
1096 case wxLIST_HITTEST_TOLEFT
: where
= _T("to the left of"); break;
1097 case wxLIST_HITTEST_TORIGHT
: where
= _T("to the right of"); break;
1098 default: where
= _T("not clear exactly where on"); break;
1101 wxLogMessage(_T("Right double click %s item %ld, subitem %ld"),
1102 where
.c_str(), item
, subitem
);
1105 void MyListCtrl::LogEvent(const wxListEvent
& event
, const wxChar
*eventName
)
1107 wxLogMessage(_T("Item %ld: %s (item text = %s, data = %ld)"),
1108 event
.GetIndex(), eventName
,
1109 event
.GetText().c_str(), event
.GetData());
1112 wxString
MyListCtrl::OnGetItemText(long item
, long column
) const
1114 if ( GetItemCount() == WXSIZEOF(SMALL_VIRTUAL_VIEW_ITEMS
) )
1116 return SMALL_VIRTUAL_VIEW_ITEMS
[item
][column
];
1118 else // "big" virtual control
1120 return wxString::Format(_T("Column %ld of item %ld"), column
, item
);
1124 int MyListCtrl::OnGetItemColumnImage(long item
, long column
) const
1129 if (!(item
% 3) && column
== 1)
1135 wxListItemAttr
*MyListCtrl::OnGetItemAttr(long item
) const
1137 // test to check that RefreshItem() works correctly: when m_updated is
1138 // set to some item and it is refreshed, we highlight the item
1139 if ( item
== m_updated
)
1141 static wxListItemAttr
s_attrHighlight(*wxRED
, wxNullColour
, wxNullFont
);
1142 return &s_attrHighlight
;
1145 return item
% 2 ? NULL
: (wxListItemAttr
*)&m_attr
;
1148 void MyListCtrl::InsertItemInReportView(int i
)
1151 buf
.Printf(_T("This is item %d"), i
);
1152 long tmp
= InsertItem(i
, buf
, 0);
1153 SetItemData(tmp
, i
);
1155 buf
.Printf(_T("Col 1, item %d"), i
);
1156 SetItem(tmp
, 1, buf
);
1158 buf
.Printf(_T("Item %d in column 2"), i
);
1159 SetItem(tmp
, 2, buf
);
1162 #if USE_CONTEXT_MENU
1163 void MyListCtrl::OnContextMenu(wxContextMenuEvent
& event
)
1165 if (GetEditControl() == NULL
)
1167 wxPoint point
= event
.GetPosition();
1169 if ( (point
.x
== -1) && (point
.y
== -1) )
1171 wxSize size
= GetSize();
1172 point
.x
= size
.x
/ 2;
1173 point
.y
= size
.y
/ 2;
1177 point
= ScreenToClient(point
);
1179 ShowContextMenu(point
);
1183 // the user is editing:
1184 // allow the text control to display its context menu
1185 // if it has one (it has on Windows) rather than display our one
1191 void MyListCtrl::ShowContextMenu(const wxPoint
& pos
)
1195 menu
.Append(wxID_ABOUT
, _T("&About"));
1196 menu
.AppendSeparator();
1197 menu
.Append(wxID_EXIT
, _T("E&xit"));
1199 PopupMenu(&menu
, pos
.x
, pos
.y
);