+ int n = m_listCtrl->GetItemCount();
+ for (int i = 0; i < n; i++)
+ m_listCtrl->SetItemState(i,wxLIST_STATE_SELECTED, wxLIST_STATE_SELECTED);
+}
+
+// ----------------------------------------------------------------------------
+// changing listctrl modes
+// ----------------------------------------------------------------------------
+
+void MyFrame::RecreateList(long flags, bool withText)
+{
+ // we could avoid recreating it if we don't set/clear the wxLC_VIRTUAL
+ // style, but it is more trouble to do it than not
+#if 0
+ if ( !m_listCtrl || ((flags & wxLC_VIRTUAL) !=
+ (m_listCtrl->GetWindowStyleFlag() & wxLC_VIRTUAL)) )
+#endif
+ {
+ delete m_listCtrl;
+
+ m_listCtrl = new MyListCtrl(m_panel, LIST_CTRL,
+ wxDefaultPosition, wxDefaultSize,
+ flags |
+ wxSUNKEN_BORDER | wxLC_EDIT_LABELS);
+
+ switch ( flags & wxLC_MASK_TYPE )
+ {
+ case wxLC_LIST:
+ InitWithListItems();
+ break;
+
+ case wxLC_ICON:
+ InitWithIconItems(withText);
+ break;
+
+ case wxLC_SMALL_ICON:
+ InitWithIconItems(withText, TRUE);
+ break;
+
+ case wxLC_REPORT:
+ if ( flags & wxLC_VIRTUAL )
+ InitWithVirtualItems();
+ else
+ InitWithReportItems();
+ break;
+
+ default:
+ wxFAIL_MSG( _T("unknown listctrl mode") );
+ }
+ }
+
+ DoSize();
+
+ m_logWindow->Clear();
+}
+
+void MyFrame::OnListView(wxCommandEvent& WXUNUSED(event))
+{
+ RecreateList(wxLC_LIST);
+}
+
+void MyFrame::InitWithListItems()
+{
+ for ( int i = 0; i < NUM_ITEMS; i++ )
+ {
+ m_listCtrl->InsertItem(i, wxString::Format(_T("Item %d"), i));
+ }
+}
+
+void MyFrame::OnReportView(wxCommandEvent& WXUNUSED(event))
+{
+ RecreateList(wxLC_REPORT);
+}
+
+void MyFrame::InitWithReportItems()
+{
+ m_listCtrl->SetImageList(m_imageListSmall, wxIMAGE_LIST_SMALL);
+
+ // note that under MSW for SetColumnWidth() to work we need to create the
+ // items with images initially even if we specify dummy image id
+ wxListItem itemCol;
+ itemCol.SetText(_T("Column 1"));
+ itemCol.SetImage(-1);
+ m_listCtrl->InsertColumn(0, itemCol);
+
+ itemCol.SetText(_T("Column 2"));
+ itemCol.SetAlign(wxLIST_FORMAT_CENTRE);
+ m_listCtrl->InsertColumn(1, itemCol);
+
+ itemCol.SetText(_T("Column 3"));
+ itemCol.SetAlign(wxLIST_FORMAT_RIGHT);
+ m_listCtrl->InsertColumn(2, itemCol);
+
+ // to speed up inserting we hide the control temporarily
+ m_listCtrl->Hide();
+
+ wxStopWatch sw;
+
+ for ( int i = 0; i < NUM_ITEMS; i++ )
+ {
+ m_listCtrl->InsertItemInReportView(i);
+ }
+
+ m_logWindow->WriteText(wxString::Format(_T("%d items inserted in %ldms\n"),
+ NUM_ITEMS, sw.Time()));
+ m_listCtrl->Show();
+
+ // we leave all mask fields to 0 and only change the colour
+ wxListItem item;
+ item.m_itemId = 0;
+ item.SetTextColour(*wxRED);
+ m_listCtrl->SetItem( item );
+
+ item.m_itemId = 2;
+ item.SetTextColour(*wxGREEN);
+ m_listCtrl->SetItem( item );
+ item.m_itemId = 4;
+ item.SetTextColour(*wxLIGHT_GREY);
+ item.SetFont(*wxITALIC_FONT);
+ item.SetBackgroundColour(*wxRED);
+ m_listCtrl->SetItem( item );
+
+ m_listCtrl->SetTextColour(*wxBLUE);
+ m_listCtrl->SetBackgroundColour(*wxLIGHT_GREY);
+
+ m_listCtrl->SetColumnWidth( 0, wxLIST_AUTOSIZE );
+ m_listCtrl->SetColumnWidth( 1, wxLIST_AUTOSIZE );
+ m_listCtrl->SetColumnWidth( 2, wxLIST_AUTOSIZE );
+}
+
+void MyFrame::InitWithIconItems(bool withText, bool sameIcon)
+{
+ m_listCtrl->SetImageList(m_imageListNormal, wxIMAGE_LIST_NORMAL);
+ m_listCtrl->SetImageList(m_imageListSmall, wxIMAGE_LIST_SMALL);
+
+ for ( int i = 0; i < NUM_ICONS; i++ )
+ {
+ int image = sameIcon ? 0 : i;
+
+ if ( withText )
+ {
+ m_listCtrl->InsertItem(i, wxString::Format(_T("Label %d"), i),
+ image);
+ }
+ else
+ {
+ m_listCtrl->InsertItem(i, image);
+ }
+ }
+}
+
+void MyFrame::OnIconView(wxCommandEvent& WXUNUSED(event))
+{
+ RecreateList(wxLC_ICON, FALSE);
+}
+
+void MyFrame::OnIconTextView(wxCommandEvent& WXUNUSED(event))
+{
+ RecreateList(wxLC_ICON);
+}
+
+void MyFrame::OnSmallIconView(wxCommandEvent& WXUNUSED(event))
+{
+ RecreateList(wxLC_SMALL_ICON, FALSE);
+}
+
+void MyFrame::OnSmallIconTextView(wxCommandEvent& WXUNUSED(event))
+{
+ RecreateList(wxLC_SMALL_ICON);
+}
+
+void MyFrame::OnVirtualView(wxCommandEvent& WXUNUSED(event))
+{
+ RecreateList(wxLC_REPORT | wxLC_VIRTUAL);
+}
+
+void MyFrame::InitWithVirtualItems()
+{
+ m_listCtrl->SetImageList(m_imageListSmall, wxIMAGE_LIST_SMALL);
+
+ m_listCtrl->InsertColumn(0, _T("First Column"));
+ m_listCtrl->InsertColumn(1, _T("Second Column"));
+ m_listCtrl->SetColumnWidth(0, 150);
+ m_listCtrl->SetColumnWidth(1, 150);
+
+ m_listCtrl->SetItemCount(1000000);
+}
+
+void MyFrame::OnSort(wxCommandEvent& WXUNUSED(event))
+{
+ wxStopWatch sw;
+
+ m_listCtrl->SortItems(MyCompareFunction, 0);
+
+ m_logWindow->WriteText(wxString::Format(_T("Sorting %d items took %ld ms\n"),
+ m_listCtrl->GetItemCount(),
+ sw.Time()));
+}
+
+void MyFrame::OnShowSelInfo(wxCommandEvent& WXUNUSED(event))
+{
+ int selCount = m_listCtrl->GetSelectedItemCount();
+ wxLogMessage(_T("%d items selected:"), selCount);
+
+ // don't show too many items
+ size_t shownCount = 0;
+
+ long item = m_listCtrl->GetNextItem(-1, wxLIST_NEXT_ALL,
+ wxLIST_STATE_SELECTED);
+ while ( item != -1 )
+ {
+ wxLogMessage(_T("\t%ld (%s)"),
+ item, m_listCtrl->GetItemText(item).c_str());
+
+ if ( ++shownCount > 10 )
+ {
+ wxLogMessage(_T("\t... more selected items snipped..."));
+ break;
+ }
+
+ item = m_listCtrl->GetNextItem(item, wxLIST_NEXT_ALL,
+ wxLIST_STATE_SELECTED);
+ }
+}
+
+void MyFrame::OnShowColInfo(wxCommandEvent& WXUNUSED(event))
+{
+ int count = m_listCtrl->GetColumnCount();
+ wxLogMessage(wxT("%d columns:"), count);
+ for ( int c = 0; c < count; c++ )
+ {
+ wxLogMessage(wxT("\tcolumn %d has width %d"), c,
+ m_listCtrl->GetColumnWidth(c));
+ }
+}
+
+void MyFrame::OnUpdateShowColInfo(wxUpdateUIEvent& event)
+{
+ event.Enable( (m_listCtrl->GetWindowStyleFlag() & wxLC_REPORT) != 0 );
+}
+
+void MyFrame::OnToggleMultiSel(wxCommandEvent& WXUNUSED(event))
+{
+ long flags = m_listCtrl->GetWindowStyleFlag();
+ if ( flags & wxLC_SINGLE_SEL )
+ flags &= ~wxLC_SINGLE_SEL;
+ else
+ flags |= wxLC_SINGLE_SEL;
+
+ m_logWindow->WriteText(wxString::Format(wxT("Current selection mode: %sle\n"),
+ (flags & wxLC_SINGLE_SEL) ? _T("sing") : _T("multip")));
+
+ RecreateList(flags);
+}
+
+void MyFrame::OnUpdateToggleMultiSel(wxUpdateUIEvent& event)
+{
+ event.Check((m_listCtrl->GetWindowStyleFlag() & wxLC_SINGLE_SEL) == 0);
+}
+
+void MyFrame::OnSetFgColour(wxCommandEvent& WXUNUSED(event))
+{
+ m_listCtrl->SetForegroundColour(wxGetColourFromUser(this));
+ m_listCtrl->Refresh();
+}
+
+void MyFrame::OnSetBgColour(wxCommandEvent& WXUNUSED(event))
+{
+ m_listCtrl->SetBackgroundColour(wxGetColourFromUser(this));
+ m_listCtrl->Refresh();
+}
+
+void MyFrame::OnAdd(wxCommandEvent& WXUNUSED(event))
+{
+ m_listCtrl->InsertItem(m_listCtrl->GetItemCount(), _T("Appended item"));
+}
+
+void MyFrame::OnEdit(wxCommandEvent& WXUNUSED(event))
+{
+ long itemCur = m_listCtrl->GetNextItem(-1, wxLIST_NEXT_ALL,
+ wxLIST_STATE_FOCUSED);
+
+ if ( itemCur != -1 )
+ {
+ m_listCtrl->EditLabel(itemCur);
+ }
+ else
+ {
+ m_logWindow->WriteText(_T("No item to edit"));
+ }
+}
+
+void MyFrame::OnDelete(wxCommandEvent& WXUNUSED(event))
+{
+ if ( m_listCtrl->GetItemCount() )
+ {
+ m_listCtrl->DeleteItem(0);
+ }
+ else
+ {
+ m_logWindow->WriteText(_T("Nothing to delete"));
+ }
+}
+
+void MyFrame::OnDeleteAll(wxCommandEvent& WXUNUSED(event))
+{
+ wxStopWatch sw;
+
+ m_listCtrl->DeleteAllItems();
+
+ m_logWindow->WriteText(wxString::Format(_T("Deleting %d items took %ld ms\n"),
+ m_listCtrl->GetItemCount(),
+ sw.Time()));