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"
46 #include "wx/numdlg.h"
51 // ----------------------------------------------------------------------------
52 // Constants and globals
53 // ----------------------------------------------------------------------------
55 const wxChar
*SMALL_VIRTUAL_VIEW_ITEMS
[][2] =
57 { _T("Cat"), _T("meow") },
58 { _T("Cow"), _T("moo") },
59 { _T("Crow"), _T("caw") },
60 { _T("Dog"), _T("woof") },
61 { _T("Duck"), _T("quack") },
62 { _T("Mouse"), _T("squeak") },
63 { _T("Owl"), _T("hoo") },
64 { _T("Pig"), _T("oink") },
65 { _T("Pigeon"), _T("coo") },
66 { _T("Sheep"), _T("baaah") },
69 // number of items in icon/small icon view
70 static const int NUM_ICONS
= 9;
73 MyCompareFunction(long item1
, long item2
, wxIntPtr
WXUNUSED(sortData
))
85 // ----------------------------------------------------------------------------
87 // ----------------------------------------------------------------------------
91 // `Main program' equivalent, creating windows and returning main app frame
94 if ( !wxApp::OnInit() )
97 // Create the main frame window
98 MyFrame
*frame
= new MyFrame(wxT("wxListCtrl Test"));
110 // ----------------------------------------------------------------------------
112 // ----------------------------------------------------------------------------
114 BEGIN_EVENT_TABLE(MyFrame
, wxFrame
)
115 EVT_SIZE(MyFrame::OnSize
)
117 EVT_MENU(LIST_QUIT
, MyFrame::OnQuit
)
118 EVT_MENU(LIST_ABOUT
, MyFrame::OnAbout
)
119 EVT_MENU(LIST_LIST_VIEW
, MyFrame::OnListView
)
120 EVT_MENU(LIST_REPORT_VIEW
, MyFrame::OnReportView
)
121 EVT_MENU(LIST_ICON_VIEW
, MyFrame::OnIconView
)
122 EVT_MENU(LIST_ICON_TEXT_VIEW
, MyFrame::OnIconTextView
)
123 EVT_MENU(LIST_SMALL_ICON_VIEW
, MyFrame::OnSmallIconView
)
124 EVT_MENU(LIST_SMALL_ICON_TEXT_VIEW
, MyFrame::OnSmallIconTextView
)
125 EVT_MENU(LIST_VIRTUAL_VIEW
, MyFrame::OnVirtualView
)
126 EVT_MENU(LIST_SMALL_VIRTUAL_VIEW
, MyFrame::OnSmallVirtualView
)
128 EVT_MENU(LIST_SET_ITEMS_COUNT
, MyFrame::OnSetItemsCount
)
130 EVT_MENU(LIST_GOTO
, MyFrame::OnGoTo
)
131 EVT_MENU(LIST_FOCUS_LAST
, MyFrame::OnFocusLast
)
132 EVT_MENU(LIST_TOGGLE_FIRST
, MyFrame::OnToggleFirstSel
)
133 EVT_MENU(LIST_DESELECT_ALL
, MyFrame::OnDeselectAll
)
134 EVT_MENU(LIST_SELECT_ALL
, MyFrame::OnSelectAll
)
135 EVT_MENU(LIST_DELETE
, MyFrame::OnDelete
)
136 EVT_MENU(LIST_ADD
, MyFrame::OnAdd
)
137 EVT_MENU(LIST_EDIT
, MyFrame::OnEdit
)
138 EVT_MENU(LIST_DELETE_ALL
, MyFrame::OnDeleteAll
)
139 EVT_MENU(LIST_SORT
, MyFrame::OnSort
)
140 EVT_MENU(LIST_SET_FG_COL
, MyFrame::OnSetFgColour
)
141 EVT_MENU(LIST_SET_BG_COL
, MyFrame::OnSetBgColour
)
142 EVT_MENU(LIST_TOGGLE_MULTI_SEL
, MyFrame::OnToggleMultiSel
)
143 EVT_MENU(LIST_SHOW_COL_INFO
, MyFrame::OnShowColInfo
)
144 EVT_MENU(LIST_SHOW_SEL_INFO
, MyFrame::OnShowSelInfo
)
145 EVT_MENU(LIST_SHOW_VIEW_RECT
, MyFrame::OnShowViewRect
)
146 #ifdef wxHAS_LISTCTRL_COLUMN_ORDER
147 EVT_MENU(LIST_SET_COL_ORDER
, MyFrame::OnSetColOrder
)
148 EVT_MENU(LIST_GET_COL_ORDER
, MyFrame::OnGetColOrder
)
149 #endif // wxHAS_LISTCTRL_COLUMN_ORDER
150 EVT_MENU(LIST_FREEZE
, MyFrame::OnFreeze
)
151 EVT_MENU(LIST_THAW
, MyFrame::OnThaw
)
152 EVT_MENU(LIST_TOGGLE_LINES
, MyFrame::OnToggleLines
)
154 EVT_MENU(LIST_MAC_USE_GENERIC
, MyFrame::OnToggleMacUseGeneric
)
156 EVT_MENU(LIST_FIND
, MyFrame::OnFind
)
158 EVT_UPDATE_UI(LIST_SHOW_COL_INFO
, MyFrame::OnUpdateShowColInfo
)
159 EVT_UPDATE_UI(LIST_TOGGLE_MULTI_SEL
, MyFrame::OnUpdateToggleMultiSel
)
162 // My frame constructor
163 MyFrame::MyFrame(const wxChar
*title
)
164 : wxFrame(NULL
, wxID_ANY
, title
, wxDefaultPosition
, wxSize(600, 500))
168 m_smallVirtual
= false;
172 SetIcon( wxICON(mondrian
) );
174 // Make an image list containing large icons
175 m_imageListNormal
= new wxImageList(32, 32, true);
176 m_imageListSmall
= new wxImageList(16, 16, true);
179 m_imageListNormal
->Add( wxIcon(_T("icon1"), wxBITMAP_TYPE_ICO_RESOURCE
) );
180 m_imageListNormal
->Add( wxIcon(_T("icon2"), wxBITMAP_TYPE_ICO_RESOURCE
) );
181 m_imageListNormal
->Add( wxIcon(_T("icon3"), wxBITMAP_TYPE_ICO_RESOURCE
) );
182 m_imageListNormal
->Add( wxIcon(_T("icon4"), wxBITMAP_TYPE_ICO_RESOURCE
) );
183 m_imageListNormal
->Add( wxIcon(_T("icon5"), wxBITMAP_TYPE_ICO_RESOURCE
) );
184 m_imageListNormal
->Add( wxIcon(_T("icon6"), wxBITMAP_TYPE_ICO_RESOURCE
) );
185 m_imageListNormal
->Add( wxIcon(_T("icon7"), wxBITMAP_TYPE_ICO_RESOURCE
) );
186 m_imageListNormal
->Add( wxIcon(_T("icon8"), wxBITMAP_TYPE_ICO_RESOURCE
) );
187 m_imageListNormal
->Add( wxIcon(_T("icon9"), wxBITMAP_TYPE_ICO_RESOURCE
) );
189 m_imageListSmall
->Add( wxIcon(_T("iconsmall"), wxBITMAP_TYPE_ICO_RESOURCE
) );
192 m_imageListNormal
->Add( wxIcon( toolbrai_xpm
) );
193 m_imageListNormal
->Add( wxIcon( toolchar_xpm
) );
194 m_imageListNormal
->Add( wxIcon( tooldata_xpm
) );
195 m_imageListNormal
->Add( wxIcon( toolnote_xpm
) );
196 m_imageListNormal
->Add( wxIcon( tooltodo_xpm
) );
197 m_imageListNormal
->Add( wxIcon( toolchec_xpm
) );
198 m_imageListNormal
->Add( wxIcon( toolgame_xpm
) );
199 m_imageListNormal
->Add( wxIcon( tooltime_xpm
) );
200 m_imageListNormal
->Add( wxIcon( toolword_xpm
) );
202 m_imageListSmall
->Add( wxIcon( small1_xpm
) );
206 wxMenu
*menuFile
= new wxMenu
;
207 menuFile
->Append(LIST_ABOUT
, _T("&About"));
208 menuFile
->AppendSeparator();
209 menuFile
->Append(LIST_QUIT
, _T("E&xit\tAlt-X"));
211 wxMenu
*menuView
= new wxMenu
;
212 menuView
->Append(LIST_LIST_VIEW
, _T("&List view\tF1"));
213 menuView
->Append(LIST_REPORT_VIEW
, _T("&Report view\tF2"));
214 menuView
->Append(LIST_ICON_VIEW
, _T("&Icon view\tF3"));
215 menuView
->Append(LIST_ICON_TEXT_VIEW
, _T("Icon view with &text\tF4"));
216 menuView
->Append(LIST_SMALL_ICON_VIEW
, _T("&Small icon view\tF5"));
217 menuView
->Append(LIST_SMALL_ICON_TEXT_VIEW
, _T("Small icon &view with text\tF6"));
218 menuView
->Append(LIST_VIRTUAL_VIEW
, _T("&Virtual view\tF7"));
219 menuView
->Append(LIST_SMALL_VIRTUAL_VIEW
, _T("Small virtual vie&w\tF8"));
220 menuView
->AppendSeparator();
221 menuView
->Append(LIST_SET_ITEMS_COUNT
, "Set &number of items");
223 menuView
->AppendSeparator();
224 menuView
->AppendCheckItem(LIST_MAC_USE_GENERIC
, _T("Mac: Use Generic Control"));
227 wxMenu
*menuList
= new wxMenu
;
228 menuList
->Append(LIST_GOTO
, _T("&Go to item #3\tCtrl-3"));
229 menuList
->Append(LIST_FOCUS_LAST
, _T("&Make last item current\tCtrl-L"));
230 menuList
->Append(LIST_TOGGLE_FIRST
, _T("To&ggle first item\tCtrl-G"));
231 menuList
->Append(LIST_DESELECT_ALL
, _T("&Deselect All\tCtrl-D"));
232 menuList
->Append(LIST_SELECT_ALL
, _T("S&elect All\tCtrl-A"));
233 menuList
->AppendSeparator();
234 menuList
->Append(LIST_SHOW_COL_INFO
, _T("Show &column info\tCtrl-C"));
235 menuList
->Append(LIST_SHOW_SEL_INFO
, _T("Show &selected items\tCtrl-S"));
236 menuList
->Append(LIST_SHOW_VIEW_RECT
, _T("Show &view rect"));
237 #ifdef wxHAS_LISTCTRL_COLUMN_ORDER
238 menuList
->Append(LIST_SET_COL_ORDER
, _T("Se&t columns order\tShift-Ctrl-O"));
239 menuList
->Append(LIST_GET_COL_ORDER
, _T("Sho&w columns order\tCtrl-O"));
240 #endif // wxHAS_LISTCTRL_COLUMN_ORDER
241 menuList
->AppendSeparator();
242 menuList
->Append(LIST_SORT
, _T("Sor&t\tCtrl-T"));
243 menuList
->Append(LIST_FIND
, "Test Find() performance");
244 menuList
->AppendSeparator();
245 menuList
->Append(LIST_ADD
, _T("&Append an item\tCtrl-P"));
246 menuList
->Append(LIST_EDIT
, _T("&Edit the item\tCtrl-E"));
247 menuList
->Append(LIST_DELETE
, _T("&Delete first item\tCtrl-X"));
248 menuList
->Append(LIST_DELETE_ALL
, _T("Delete &all items"));
249 menuList
->AppendSeparator();
250 menuList
->Append(LIST_FREEZE
, _T("Free&ze\tCtrl-Z"));
251 menuList
->Append(LIST_THAW
, _T("Tha&w\tCtrl-W"));
252 menuList
->AppendSeparator();
253 menuList
->AppendCheckItem(LIST_TOGGLE_LINES
, _T("Toggle &lines\tCtrl-I"));
254 menuList
->Append(LIST_TOGGLE_MULTI_SEL
, _T("&Multiple selection\tCtrl-M"),
255 _T("Toggle multiple selection"), true);
257 wxMenu
*menuCol
= new wxMenu
;
258 menuCol
->Append(LIST_SET_FG_COL
, _T("&Foreground colour..."));
259 menuCol
->Append(LIST_SET_BG_COL
, _T("&Background colour..."));
261 wxMenuBar
*menubar
= new wxMenuBar
;
262 menubar
->Append(menuFile
, _T("&File"));
263 menubar
->Append(menuView
, _T("&View"));
264 menubar
->Append(menuList
, _T("&List"));
265 menubar
->Append(menuCol
, _T("&Colour"));
268 m_panel
= new wxPanel(this, wxID_ANY
);
269 m_logWindow
= new wxTextCtrl(m_panel
, wxID_ANY
, wxEmptyString
,
270 wxDefaultPosition
, wxDefaultSize
,
271 wxTE_READONLY
| wxTE_MULTILINE
| wxSUNKEN_BORDER
);
273 m_logOld
= wxLog::SetActiveTarget(new wxLogTextCtrl(m_logWindow
));
275 RecreateList(wxLC_REPORT
| wxLC_SINGLE_SEL
);
278 // this is useful to know specially when debugging :)
279 wxLogMessage("Your version of comctl32.dll is: %d",
280 wxApp::GetComCtl32Version());
285 #endif // wxUSE_STATUSBAR
290 delete wxLog::SetActiveTarget(m_logOld
);
292 delete m_imageListNormal
;
293 delete m_imageListSmall
;
296 void MyFrame::OnSize(wxSizeEvent
& event
)
303 void MyFrame::DoSize()
308 wxSize size
= GetClientSize();
309 wxCoord y
= (2*size
.y
)/3;
310 m_listCtrl
->SetSize(0, 0, size
.x
, y
);
311 m_logWindow
->SetSize(0, y
+ 1, size
.x
, size
.y
- y
-1);
314 bool MyFrame::CheckNonVirtual() const
316 if ( !m_listCtrl
->HasFlag(wxLC_VIRTUAL
) )
319 // "this" == whatever
320 wxLogWarning(_T("Can't do this in virtual view, sorry."));
325 void MyFrame::OnQuit(wxCommandEvent
& WXUNUSED(event
))
330 void MyFrame::OnAbout(wxCommandEvent
& WXUNUSED(event
))
332 wxMessageDialog
dialog(this, _T("List test sample\nJulian Smart (c) 1997"),
333 _T("About list test"), wxOK
|wxCANCEL
);
338 void MyFrame::OnFreeze(wxCommandEvent
& WXUNUSED(event
))
340 wxLogMessage(_T("Freezing the control"));
342 m_listCtrl
->Freeze();
345 void MyFrame::OnThaw(wxCommandEvent
& WXUNUSED(event
))
347 wxLogMessage(_T("Thawing the control"));
352 void MyFrame::OnToggleLines(wxCommandEvent
& event
)
354 m_listCtrl
->SetSingleStyle(wxLC_HRULES
| wxLC_VRULES
, event
.IsChecked());
359 void MyFrame::OnToggleMacUseGeneric(wxCommandEvent
& event
)
361 wxSystemOptions::SetOption(wxT("mac.listctrl.always_use_generic"), event
.IsChecked());
366 void MyFrame::OnGoTo(wxCommandEvent
& WXUNUSED(event
))
369 m_listCtrl
->SetItemState(index
, wxLIST_STATE_FOCUSED
, wxLIST_STATE_FOCUSED
);
371 long sel
= m_listCtrl
->GetNextItem(-1, wxLIST_NEXT_ALL
,
372 wxLIST_STATE_SELECTED
);
374 m_listCtrl
->SetItemState(sel
, 0, wxLIST_STATE_SELECTED
);
375 m_listCtrl
->SetItemState(index
, wxLIST_STATE_SELECTED
, wxLIST_STATE_SELECTED
);
378 void MyFrame::OnFocusLast(wxCommandEvent
& WXUNUSED(event
))
380 long index
= m_listCtrl
->GetItemCount() - 1;
386 m_listCtrl
->SetItemState(index
, wxLIST_STATE_FOCUSED
, wxLIST_STATE_FOCUSED
);
387 m_listCtrl
->EnsureVisible(index
);
390 void MyFrame::OnToggleFirstSel(wxCommandEvent
& WXUNUSED(event
))
392 m_listCtrl
->SetItemState(0, (~m_listCtrl
->GetItemState(0, wxLIST_STATE_SELECTED
) ) & wxLIST_STATE_SELECTED
, wxLIST_STATE_SELECTED
);
395 void MyFrame::OnDeselectAll(wxCommandEvent
& WXUNUSED(event
))
397 if ( !CheckNonVirtual() )
400 int n
= m_listCtrl
->GetItemCount();
401 for (int i
= 0; i
< n
; i
++)
402 m_listCtrl
->SetItemState(i
,0,wxLIST_STATE_SELECTED
);
405 void MyFrame::OnSelectAll(wxCommandEvent
& WXUNUSED(event
))
407 if ( !CheckNonVirtual() )
410 int n
= m_listCtrl
->GetItemCount();
411 for (int i
= 0; i
< n
; i
++)
412 m_listCtrl
->SetItemState(i
,wxLIST_STATE_SELECTED
, wxLIST_STATE_SELECTED
);
415 // ----------------------------------------------------------------------------
416 // changing listctrl modes
417 // ----------------------------------------------------------------------------
419 void MyFrame::RecreateList(long flags
, bool withText
)
421 // we could avoid recreating it if we don't set/clear the wxLC_VIRTUAL
422 // style, but it is more trouble to do it than not
424 if ( !m_listCtrl
|| ((flags
& wxLC_VIRTUAL
) !=
425 (m_listCtrl
->GetWindowStyleFlag() & wxLC_VIRTUAL
)) )
430 m_listCtrl
= new MyListCtrl(m_panel
, LIST_CTRL
,
431 wxDefaultPosition
, wxDefaultSize
,
433 wxBORDER_THEME
| wxLC_EDIT_LABELS
);
435 switch ( flags
& wxLC_MASK_TYPE
)
442 InitWithIconItems(withText
);
445 case wxLC_SMALL_ICON
:
446 InitWithIconItems(withText
, true);
450 if ( flags
& wxLC_VIRTUAL
)
451 InitWithVirtualItems();
453 InitWithReportItems();
457 wxFAIL_MSG( _T("unknown listctrl mode") );
463 m_logWindow
->Clear();
466 void MyFrame::OnListView(wxCommandEvent
& WXUNUSED(event
))
468 RecreateList(wxLC_LIST
);
471 void MyFrame::InitWithListItems()
473 for ( int i
= 0; i
< m_numListItems
; i
++ )
475 m_listCtrl
->InsertItem(i
, wxString::Format(_T("Item %d"), i
));
479 void MyFrame::OnReportView(wxCommandEvent
& WXUNUSED(event
))
481 RecreateList(wxLC_REPORT
);
484 void MyFrame::InitWithReportItems()
486 m_listCtrl
->SetImageList(m_imageListSmall
, wxIMAGE_LIST_SMALL
);
488 // note that under MSW for SetColumnWidth() to work we need to create the
489 // items with images initially even if we specify dummy image id
491 itemCol
.SetText(_T("Column 1"));
492 itemCol
.SetImage(-1);
493 m_listCtrl
->InsertColumn(0, itemCol
);
495 itemCol
.SetText(_T("Column 2"));
496 itemCol
.SetAlign(wxLIST_FORMAT_CENTRE
);
497 m_listCtrl
->InsertColumn(1, itemCol
);
499 itemCol
.SetText(_T("Column 3"));
500 itemCol
.SetAlign(wxLIST_FORMAT_RIGHT
);
501 m_listCtrl
->InsertColumn(2, itemCol
);
503 // to speed up inserting we hide the control temporarily
508 for ( int i
= 0; i
< m_numListItems
; i
++ )
510 m_listCtrl
->InsertItemInReportView(i
);
513 m_logWindow
->WriteText(wxString::Format(_T("%d items inserted in %ldms\n"),
514 m_numListItems
, sw
.Time()));
517 // we leave all mask fields to 0 and only change the colour
520 item
.SetTextColour(*wxRED
);
521 m_listCtrl
->SetItem( item
);
524 item
.SetTextColour(*wxGREEN
);
525 m_listCtrl
->SetItem( item
);
527 item
.SetTextColour(*wxLIGHT_GREY
);
528 item
.SetFont(*wxITALIC_FONT
);
529 item
.SetBackgroundColour(*wxRED
);
530 m_listCtrl
->SetItem( item
);
532 m_listCtrl
->SetTextColour(*wxBLUE
);
534 m_listCtrl
->SetColumnWidth( 0, wxLIST_AUTOSIZE
);
535 m_listCtrl
->SetColumnWidth( 1, wxLIST_AUTOSIZE
);
536 m_listCtrl
->SetColumnWidth( 2, wxLIST_AUTOSIZE
);
538 // Set images in columns
539 m_listCtrl
->SetItemColumnImage(1, 1, 0);
545 m_listCtrl
->SetItem(info
);
547 // test SetItemFont too
548 m_listCtrl
->SetItemFont(0, *wxITALIC_FONT
);
551 void MyFrame::InitWithIconItems(bool withText
, bool sameIcon
)
553 m_listCtrl
->SetImageList(m_imageListNormal
, wxIMAGE_LIST_NORMAL
);
554 m_listCtrl
->SetImageList(m_imageListSmall
, wxIMAGE_LIST_SMALL
);
556 for ( int i
= 0; i
< NUM_ICONS
; i
++ )
558 int image
= sameIcon
? 0 : i
;
562 m_listCtrl
->InsertItem(i
, wxString::Format(_T("Label %d"), i
),
567 m_listCtrl
->InsertItem(i
, image
);
572 void MyFrame::OnIconView(wxCommandEvent
& WXUNUSED(event
))
574 RecreateList(wxLC_ICON
, false);
577 void MyFrame::OnIconTextView(wxCommandEvent
& WXUNUSED(event
))
579 RecreateList(wxLC_ICON
);
582 void MyFrame::OnSmallIconView(wxCommandEvent
& WXUNUSED(event
))
584 RecreateList(wxLC_SMALL_ICON
, false);
587 void MyFrame::OnSmallIconTextView(wxCommandEvent
& WXUNUSED(event
))
589 RecreateList(wxLC_SMALL_ICON
);
592 void MyFrame::OnVirtualView(wxCommandEvent
& WXUNUSED(event
))
594 m_smallVirtual
= false;
595 RecreateList(wxLC_REPORT
| wxLC_VIRTUAL
);
598 void MyFrame::OnSmallVirtualView(wxCommandEvent
& WXUNUSED(event
))
600 m_smallVirtual
= true;
601 RecreateList(wxLC_REPORT
| wxLC_VIRTUAL
);
604 void MyFrame::OnSetItemsCount(wxCommandEvent
& WXUNUSED(event
))
606 int numItems
= wxGetNumberFromUser
608 "Enter the initial number of items for "
609 "the list and report views",
611 "wxWidgets wxListCtrl sample",
617 if ( numItems
== -1 || numItems
== m_numListItems
)
620 m_numListItems
= numItems
;
622 if ( m_listCtrl
->HasFlag(wxLC_REPORT
) &&
623 !m_listCtrl
->HasFlag(wxLC_VIRTUAL
) )
624 RecreateList(wxLC_REPORT
);
625 else if ( m_listCtrl
->HasFlag(wxLC_LIST
) )
626 RecreateList(wxLC_LIST
);
629 void MyFrame::InitWithVirtualItems()
631 m_listCtrl
->SetImageList(m_imageListSmall
, wxIMAGE_LIST_SMALL
);
633 if ( m_smallVirtual
)
635 m_listCtrl
->InsertColumn(0, _T("Animal"));
636 m_listCtrl
->InsertColumn(1, _T("Sound"));
637 m_listCtrl
->SetItemCount(WXSIZEOF(SMALL_VIRTUAL_VIEW_ITEMS
));
641 m_listCtrl
->InsertColumn(0, _T("First Column"));
642 m_listCtrl
->InsertColumn(1, _T("Second Column"));
643 m_listCtrl
->SetColumnWidth(0, 150);
644 m_listCtrl
->SetColumnWidth(1, 150);
645 m_listCtrl
->SetItemCount(1000000);
649 void MyFrame::OnSort(wxCommandEvent
& WXUNUSED(event
))
653 m_listCtrl
->SortItems(MyCompareFunction
, 0);
655 m_logWindow
->WriteText(wxString::Format(_T("Sorting %d items took %ld ms\n"),
656 m_listCtrl
->GetItemCount(),
660 void MyFrame::OnFind(wxCommandEvent
& WXUNUSED(event
))
664 const int itemCount
= m_listCtrl
->GetItemCount();
665 for ( int i
= 0; i
< itemCount
; i
++ )
666 m_listCtrl
->FindItem(-1, i
);
668 wxLogMessage("Calling Find() for all %d items took %ld ms",
669 itemCount
, sw
.Time());
672 void MyFrame::OnShowSelInfo(wxCommandEvent
& WXUNUSED(event
))
674 int selCount
= m_listCtrl
->GetSelectedItemCount();
675 wxLogMessage(_T("%d items selected:"), selCount
);
677 // don't show too many items
678 size_t shownCount
= 0;
680 long item
= m_listCtrl
->GetNextItem(-1, wxLIST_NEXT_ALL
,
681 wxLIST_STATE_SELECTED
);
684 wxLogMessage(_T("\t%ld (%s)"),
685 item
, m_listCtrl
->GetItemText(item
).c_str());
687 if ( ++shownCount
> 10 )
689 wxLogMessage(_T("\t... more selected items snipped..."));
693 item
= m_listCtrl
->GetNextItem(item
, wxLIST_NEXT_ALL
,
694 wxLIST_STATE_SELECTED
);
698 void MyFrame::OnShowViewRect(wxCommandEvent
& WXUNUSED(event
))
700 const wxRect r
= m_listCtrl
->GetViewRect();
701 wxLogMessage("View rect: (%d, %d)-(%d, %d)",
702 r
.GetLeft(), r
.GetTop(), r
.GetRight(), r
.GetBottom());
705 // ----------------------------------------------------------------------------
706 // column order tests
707 // ----------------------------------------------------------------------------
709 #ifdef wxHAS_LISTCTRL_COLUMN_ORDER
711 static wxString
DumpIntArray(const wxArrayInt
& a
)
714 const size_t count
= a
.size();
715 for ( size_t n
= 0; n
< count
; n
++ )
719 s
+= wxString::Format("%lu", (unsigned long)a
[n
]);
727 void MyFrame::OnSetColOrder(wxCommandEvent
& WXUNUSED(event
))
733 if ( m_listCtrl
->SetColumnsOrder(order
) )
734 wxLogMessage("Column order set to %s", DumpIntArray(order
));
737 void MyFrame::OnGetColOrder(wxCommandEvent
& WXUNUSED(event
))
739 // show what GetColumnsOrder() returns
740 const wxArrayInt order
= m_listCtrl
->GetColumnsOrder();
741 wxString msg
= "Columns order: " +
742 DumpIntArray(m_listCtrl
->GetColumnsOrder()) + "\n";
745 const int count
= m_listCtrl
->GetColumnCount();
747 // show the results of GetColumnOrder() for each column
748 msg
+= "GetColumnOrder() results:\n";
749 for ( n
= 0; n
< count
; n
++ )
751 msg
+= wxString::Format(" %2d -> %2d\n",
752 n
, m_listCtrl
->GetColumnOrder(n
));
755 // and the results of GetColumnIndexFromOrder() too
756 msg
+= "GetColumnIndexFromOrder() results:\n";
757 for ( n
= 0; n
< count
; n
++ )
759 msg
+= wxString::Format(" %2d -> %2d\n",
760 n
, m_listCtrl
->GetColumnIndexFromOrder(n
));
763 wxLogMessage("%s", msg
);
766 #endif // wxHAS_LISTCTRL_COLUMN_ORDER
768 void MyFrame::OnShowColInfo(wxCommandEvent
& WXUNUSED(event
))
770 int count
= m_listCtrl
->GetColumnCount();
771 wxLogMessage(wxT("%d columns:"), count
);
772 for ( int c
= 0; c
< count
; c
++ )
774 wxLogMessage(wxT("\tcolumn %d has width %d"), c
,
775 m_listCtrl
->GetColumnWidth(c
));
779 void MyFrame::OnUpdateShowColInfo(wxUpdateUIEvent
& event
)
781 event
.Enable( (m_listCtrl
->GetWindowStyleFlag() & wxLC_REPORT
) != 0 );
784 void MyFrame::OnToggleMultiSel(wxCommandEvent
& WXUNUSED(event
))
786 long flags
= m_listCtrl
->GetWindowStyleFlag();
787 if ( flags
& wxLC_SINGLE_SEL
)
788 flags
&= ~wxLC_SINGLE_SEL
;
790 flags
|= wxLC_SINGLE_SEL
;
792 m_logWindow
->WriteText(wxString::Format(wxT("Current selection mode: %sle\n"),
793 (flags
& wxLC_SINGLE_SEL
) ? _T("sing") : _T("multip")));
798 void MyFrame::OnUpdateToggleMultiSel(wxUpdateUIEvent
& event
)
800 event
.Check((m_listCtrl
->GetWindowStyleFlag() & wxLC_SINGLE_SEL
) == 0);
803 void MyFrame::OnSetFgColour(wxCommandEvent
& WXUNUSED(event
))
805 m_listCtrl
->SetForegroundColour(wxGetColourFromUser(this));
806 m_listCtrl
->Refresh();
809 void MyFrame::OnSetBgColour(wxCommandEvent
& WXUNUSED(event
))
811 m_listCtrl
->SetBackgroundColour(wxGetColourFromUser(this));
812 m_listCtrl
->Refresh();
815 void MyFrame::OnAdd(wxCommandEvent
& WXUNUSED(event
))
817 m_listCtrl
->InsertItem(m_listCtrl
->GetItemCount(), _T("Appended item"));
820 void MyFrame::OnEdit(wxCommandEvent
& WXUNUSED(event
))
822 long itemCur
= m_listCtrl
->GetNextItem(-1, wxLIST_NEXT_ALL
,
823 wxLIST_STATE_FOCUSED
);
827 m_listCtrl
->EditLabel(itemCur
);
831 m_logWindow
->WriteText(_T("No item to edit"));
835 void MyFrame::OnDelete(wxCommandEvent
& WXUNUSED(event
))
837 if ( m_listCtrl
->GetItemCount() )
839 m_listCtrl
->DeleteItem(0);
843 m_logWindow
->WriteText(_T("Nothing to delete"));
847 void MyFrame::OnDeleteAll(wxCommandEvent
& WXUNUSED(event
))
851 int itemCount
= m_listCtrl
->GetItemCount();
853 m_listCtrl
->DeleteAllItems();
855 m_logWindow
->WriteText(wxString::Format(_T("Deleting %d items took %ld ms\n"),
861 // ----------------------------------------------------------------------------
863 // ----------------------------------------------------------------------------
865 BEGIN_EVENT_TABLE(MyListCtrl
, wxListCtrl
)
866 EVT_LIST_BEGIN_DRAG(LIST_CTRL
, MyListCtrl::OnBeginDrag
)
867 EVT_LIST_BEGIN_RDRAG(LIST_CTRL
, MyListCtrl::OnBeginRDrag
)
868 EVT_LIST_BEGIN_LABEL_EDIT(LIST_CTRL
, MyListCtrl::OnBeginLabelEdit
)
869 EVT_LIST_END_LABEL_EDIT(LIST_CTRL
, MyListCtrl::OnEndLabelEdit
)
870 EVT_LIST_DELETE_ITEM(LIST_CTRL
, MyListCtrl::OnDeleteItem
)
871 EVT_LIST_DELETE_ALL_ITEMS(LIST_CTRL
, MyListCtrl::OnDeleteAllItems
)
872 EVT_LIST_ITEM_SELECTED(LIST_CTRL
, MyListCtrl::OnSelected
)
873 EVT_LIST_ITEM_DESELECTED(LIST_CTRL
, MyListCtrl::OnDeselected
)
874 EVT_LIST_KEY_DOWN(LIST_CTRL
, MyListCtrl::OnListKeyDown
)
875 EVT_LIST_ITEM_ACTIVATED(LIST_CTRL
, MyListCtrl::OnActivated
)
876 EVT_LIST_ITEM_FOCUSED(LIST_CTRL
, MyListCtrl::OnFocused
)
878 EVT_LIST_COL_CLICK(LIST_CTRL
, MyListCtrl::OnColClick
)
879 EVT_LIST_COL_RIGHT_CLICK(LIST_CTRL
, MyListCtrl::OnColRightClick
)
880 EVT_LIST_COL_BEGIN_DRAG(LIST_CTRL
, MyListCtrl::OnColBeginDrag
)
881 EVT_LIST_COL_DRAGGING(LIST_CTRL
, MyListCtrl::OnColDragging
)
882 EVT_LIST_COL_END_DRAG(LIST_CTRL
, MyListCtrl::OnColEndDrag
)
884 EVT_LIST_CACHE_HINT(LIST_CTRL
, MyListCtrl::OnCacheHint
)
887 EVT_CONTEXT_MENU(MyListCtrl::OnContextMenu
)
889 EVT_CHAR(MyListCtrl::OnChar
)
891 EVT_RIGHT_DOWN(MyListCtrl::OnRightClick
)
894 void MyListCtrl::OnCacheHint(wxListEvent
& event
)
896 wxLogMessage( wxT("OnCacheHint: cache items %ld..%ld"),
897 event
.GetCacheFrom(), event
.GetCacheTo() );
900 void MyListCtrl::SetColumnImage(int col
, int image
)
903 item
.SetMask(wxLIST_MASK_IMAGE
);
904 item
.SetImage(image
);
905 SetColumn(col
, item
);
908 void MyListCtrl::OnColClick(wxListEvent
& event
)
910 int col
= event
.GetColumn();
912 // set or unset image
913 static bool x
= false;
915 SetColumnImage(col
, x
? 0 : -1);
917 wxLogMessage( wxT("OnColumnClick at %d."), col
);
920 void MyListCtrl::OnColRightClick(wxListEvent
& event
)
922 int col
= event
.GetColumn();
925 SetColumnImage(col
, -1);
928 // Show popupmenu at position
929 wxMenu
menu(wxT("Test"));
930 menu
.Append(LIST_ABOUT
, _T("&About"));
931 PopupMenu(&menu
, event
.GetPoint());
933 wxLogMessage( wxT("OnColumnRightClick at %d."), event
.GetColumn() );
936 void MyListCtrl::LogColEvent(const wxListEvent
& event
, const wxChar
*name
)
938 const int col
= event
.GetColumn();
940 wxLogMessage(wxT("%s: column %d (width = %d or %d)."),
943 event
.GetItem().GetWidth(),
944 GetColumnWidth(col
));
947 void MyListCtrl::OnColBeginDrag(wxListEvent
& event
)
949 LogColEvent( event
, wxT("OnColBeginDrag") );
951 if ( event
.GetColumn() == 0 )
953 wxLogMessage(_T("Resizing this column shouldn't work."));
959 void MyListCtrl::OnColDragging(wxListEvent
& event
)
961 LogColEvent( event
, wxT("OnColDragging") );
964 void MyListCtrl::OnColEndDrag(wxListEvent
& event
)
966 LogColEvent( event
, wxT("OnColEndDrag") );
969 void MyListCtrl::OnBeginDrag(wxListEvent
& event
)
971 const wxPoint
& pt
= event
.m_pointDrag
;
974 wxLogMessage( wxT("OnBeginDrag at (%d, %d), item %ld."),
975 pt
.x
, pt
.y
, HitTest(pt
, flags
) );
978 void MyListCtrl::OnBeginRDrag(wxListEvent
& event
)
980 wxLogMessage( wxT("OnBeginRDrag at %d,%d."),
981 event
.m_pointDrag
.x
, event
.m_pointDrag
.y
);
984 void MyListCtrl::OnBeginLabelEdit(wxListEvent
& event
)
986 wxLogMessage( wxT("OnBeginLabelEdit: %s"), event
.m_item
.m_text
.c_str());
988 wxTextCtrl
* const text
= GetEditControl();
991 wxLogMessage("BUG: started to edit but no edit control");
995 wxLogMessage("Edit control value: \"%s\"", text
->GetValue());
999 void MyListCtrl::OnEndLabelEdit(wxListEvent
& event
)
1001 wxLogMessage( wxT("OnEndLabelEdit: %s"),
1003 event
.IsEditCancelled() ?
1004 wxString("[cancelled]") :
1010 void MyListCtrl::OnDeleteItem(wxListEvent
& event
)
1012 LogEvent(event
, _T("OnDeleteItem"));
1013 wxLogMessage( wxT("Number of items when delete event is sent: %d"), GetItemCount() );
1016 void MyListCtrl::OnDeleteAllItems(wxListEvent
& event
)
1018 LogEvent(event
, _T("OnDeleteAllItems"));
1021 void MyListCtrl::OnSelected(wxListEvent
& event
)
1023 LogEvent(event
, _T("OnSelected"));
1025 if ( GetWindowStyle() & wxLC_REPORT
)
1028 info
.m_itemId
= event
.m_itemIndex
;
1030 info
.m_mask
= wxLIST_MASK_TEXT
;
1031 if ( GetItem(info
) )
1033 wxLogMessage(wxT("Value of the 2nd field of the selected item: %s"),
1034 info
.m_text
.c_str());
1038 wxFAIL_MSG(wxT("wxListCtrl::GetItem() failed"));
1043 void MyListCtrl::OnDeselected(wxListEvent
& event
)
1045 LogEvent(event
, _T("OnDeselected"));
1048 void MyListCtrl::OnActivated(wxListEvent
& event
)
1050 LogEvent(event
, _T("OnActivated"));
1053 void MyListCtrl::OnFocused(wxListEvent
& event
)
1055 LogEvent(event
, _T("OnFocused"));
1060 void MyListCtrl::OnListKeyDown(wxListEvent
& event
)
1064 switch ( event
.GetKeyCode() )
1066 case 'C': // colorize
1069 info
.m_itemId
= event
.GetIndex();
1070 if ( info
.m_itemId
== -1 )
1078 wxListItemAttr
*attr
= info
.GetAttributes();
1079 if ( !attr
|| !attr
->HasTextColour() )
1081 info
.SetTextColour(*wxCYAN
);
1085 RefreshItem(info
.m_itemId
);
1091 item
= GetNextItem(-1, wxLIST_NEXT_ALL
, wxLIST_STATE_FOCUSED
);
1092 if ( item
++ == GetItemCount() - 1 )
1097 wxLogMessage(_T("Focusing item %ld"), item
);
1099 SetItemState(item
, wxLIST_STATE_FOCUSED
, wxLIST_STATE_FOCUSED
);
1100 EnsureVisible(item
);
1103 case 'R': // show bounding rectangle
1105 item
= event
.GetIndex();
1107 if ( !GetItemRect(item
, r
) )
1109 wxLogError(_T("Failed to retrieve rect of item %ld"), item
);
1113 wxLogMessage(_T("Bounding rect of item %ld is (%d, %d)-(%d, %d)"),
1114 item
, r
.x
, r
.y
, r
.x
+ r
.width
, r
.y
+ r
.height
);
1118 case '1': // show sub item bounding rectangle
1121 case '4': // this column is invalid but we want to test it too
1122 if ( InReportView() )
1124 int subItem
= event
.GetKeyCode() - '1';
1125 item
= event
.GetIndex();
1127 if ( !GetSubItemRect(item
, subItem
, r
) )
1129 wxLogError(_T("Failed to retrieve rect of item %ld column %d"), item
, subItem
+ 1);
1133 wxLogMessage(_T("Bounding rect of item %ld column %d is (%d, %d)-(%d, %d)"),
1135 r
.x
, r
.y
, r
.x
+ r
.width
, r
.y
+ r
.height
);
1143 if ( m_updated
!= -1 )
1144 RefreshItem(m_updated
);
1146 m_updated
= event
.GetIndex();
1147 if ( m_updated
!= -1 )
1149 // we won't see changes to this item as it's selected, update
1150 // the next one (or the first one if we're on the last item)
1151 if ( ++m_updated
== GetItemCount() )
1154 wxLogMessage("Updating colour of the item %ld", m_updated
);
1155 RefreshItem(m_updated
);
1160 item
= GetNextItem(-1, wxLIST_NEXT_ALL
, wxLIST_STATE_SELECTED
);
1161 while ( item
!= -1 )
1165 wxLogMessage(_T("Item %ld deleted"), item
);
1167 // -1 because the indices were shifted by DeleteItem()
1168 item
= GetNextItem(item
- 1,
1169 wxLIST_NEXT_ALL
, wxLIST_STATE_SELECTED
);
1174 if ( GetWindowStyle() & wxLC_REPORT
)
1176 if ( GetWindowStyle() & wxLC_VIRTUAL
)
1178 SetItemCount(GetItemCount() + 1);
1182 InsertItemInReportView(event
.GetIndex());
1185 //else: fall through
1188 LogEvent(event
, _T("OnListKeyDown"));
1194 void MyListCtrl::OnChar(wxKeyEvent
& event
)
1196 wxLogMessage(_T("Got char event."));
1198 switch ( event
.GetKeyCode() )
1212 // these are the keys we process ourselves
1220 void MyListCtrl::OnRightClick(wxMouseEvent
& event
)
1222 if ( !event
.ControlDown() )
1230 long item
= HitTest(event
.GetPosition(), flags
, &subitem
);
1235 case wxLIST_HITTEST_ABOVE
: where
= _T("above"); break;
1236 case wxLIST_HITTEST_BELOW
: where
= _T("below"); break;
1237 case wxLIST_HITTEST_NOWHERE
: where
= _T("nowhere near"); break;
1238 case wxLIST_HITTEST_ONITEMICON
: where
= _T("on icon of"); break;
1239 case wxLIST_HITTEST_ONITEMLABEL
: where
= _T("on label of"); break;
1240 case wxLIST_HITTEST_ONITEMRIGHT
: where
= _T("right on"); break;
1241 case wxLIST_HITTEST_TOLEFT
: where
= _T("to the left of"); break;
1242 case wxLIST_HITTEST_TORIGHT
: where
= _T("to the right of"); break;
1243 default: where
= _T("not clear exactly where on"); break;
1246 wxLogMessage(_T("Right double click %s item %ld, subitem %ld"),
1247 where
.c_str(), item
, subitem
);
1250 void MyListCtrl::LogEvent(const wxListEvent
& event
, const wxChar
*eventName
)
1252 wxLogMessage(_T("Item %ld: %s (item text = %s, data = %ld)"),
1253 event
.GetIndex(), eventName
,
1254 event
.GetText().c_str(), event
.GetData());
1257 wxString
MyListCtrl::OnGetItemText(long item
, long column
) const
1259 if ( GetItemCount() == WXSIZEOF(SMALL_VIRTUAL_VIEW_ITEMS
) )
1261 return SMALL_VIRTUAL_VIEW_ITEMS
[item
][column
];
1263 else // "big" virtual control
1265 return wxString::Format(_T("Column %ld of item %ld"), column
, item
);
1269 int MyListCtrl::OnGetItemColumnImage(long item
, long column
) const
1274 if (!(item
% 3) && column
== 1)
1280 wxListItemAttr
*MyListCtrl::OnGetItemAttr(long item
) const
1282 // test to check that RefreshItem() works correctly: when m_updated is
1283 // set to some item and it is refreshed, we highlight the item
1284 if ( item
== m_updated
)
1286 static wxListItemAttr
s_attrHighlight(*wxRED
, wxNullColour
, wxNullFont
);
1287 return &s_attrHighlight
;
1290 return item
% 2 ? NULL
: (wxListItemAttr
*)&m_attr
;
1293 void MyListCtrl::InsertItemInReportView(int i
)
1296 buf
.Printf(_T("This is item %d"), i
);
1297 long tmp
= InsertItem(i
, buf
, 0);
1298 SetItemData(tmp
, i
);
1300 buf
.Printf(_T("Col 1, item %d"), i
);
1301 SetItem(tmp
, 1, buf
);
1303 buf
.Printf(_T("Item %d in column 2"), i
);
1304 SetItem(tmp
, 2, buf
);
1307 #if USE_CONTEXT_MENU
1308 void MyListCtrl::OnContextMenu(wxContextMenuEvent
& event
)
1310 if (GetEditControl() == NULL
)
1312 wxPoint point
= event
.GetPosition();
1314 if ( (point
.x
== -1) && (point
.y
== -1) )
1316 wxSize size
= GetSize();
1317 point
.x
= size
.x
/ 2;
1318 point
.y
= size
.y
/ 2;
1322 point
= ScreenToClient(point
);
1324 ShowContextMenu(point
);
1328 // the user is editing:
1329 // allow the text control to display its context menu
1330 // if it has one (it has on Windows) rather than display our one
1336 void MyListCtrl::ShowContextMenu(const wxPoint
& pos
)
1340 menu
.Append(wxID_ABOUT
, _T("&About"));
1341 menu
.AppendSeparator();
1342 menu
.Append(wxID_EXIT
, _T("E&xit"));
1344 PopupMenu(&menu
, pos
.x
, pos
.y
);