+void MyListCtrl::OnBeginLabelEdit(wxListEvent& event)
+{
+ wxLogMessage( wxT("OnBeginLabelEdit: %s"), event.m_item.m_text.c_str());
+
+ wxTextCtrl * const text = GetEditControl();
+ if ( !text )
+ {
+ wxLogMessage("BUG: started to edit but no edit control");
+ }
+ else
+ {
+ wxLogMessage("Edit control value: \"%s\"", text->GetValue());
+ }
+}
+
+void MyListCtrl::OnEndLabelEdit(wxListEvent& event)
+{
+ wxLogMessage( wxT("OnEndLabelEdit: %s"),
+ (
+ event.IsEditCancelled() ?
+ wxString("[cancelled]") :
+ event.m_item.m_text
+ ).c_str()
+ );
+}
+
+void MyListCtrl::OnDeleteItem(wxListEvent& event)
+{
+ LogEvent(event, wxT("OnDeleteItem"));
+ wxLogMessage( wxT("Number of items when delete event is sent: %d"), GetItemCount() );
+}
+
+void MyListCtrl::OnDeleteAllItems(wxListEvent& event)
+{
+ LogEvent(event, wxT("OnDeleteAllItems"));
+}
+
+void MyListCtrl::OnSelected(wxListEvent& event)
+{
+ LogEvent(event, wxT("OnSelected"));
+
+ if ( GetWindowStyle() & wxLC_REPORT )
+ {
+ wxListItem info;
+ info.m_itemId = event.m_itemIndex;
+ info.m_col = 1;
+ info.m_mask = wxLIST_MASK_TEXT;
+ if ( GetItem(info) )
+ {
+ wxLogMessage(wxT("Value of the 2nd field of the selected item: %s"),
+ info.m_text.c_str());
+ }
+ else
+ {
+ wxFAIL_MSG(wxT("wxListCtrl::GetItem() failed"));
+ }
+ }
+}
+
+void MyListCtrl::OnDeselected(wxListEvent& event)
+{
+ LogEvent(event, wxT("OnDeselected"));
+}
+
+void MyListCtrl::OnActivated(wxListEvent& event)
+{
+ LogEvent(event, wxT("OnActivated"));
+}
+
+void MyListCtrl::OnFocused(wxListEvent& event)
+{
+ LogEvent(event, wxT("OnFocused"));
+
+ event.Skip();
+}
+
+void MyListCtrl::OnListKeyDown(wxListEvent& event)
+{
+ long item;
+
+ if ( !wxGetKeyState(WXK_SHIFT) )
+ {
+ LogEvent(event, wxT("OnListKeyDown"));
+ event.Skip();
+ return;
+ }
+
+ switch ( event.GetKeyCode() )
+ {
+ case 'C': // colorize
+ {
+ wxListItem info;
+ info.m_itemId = event.GetIndex();
+ if ( info.m_itemId == -1 )
+ {
+ // no item
+ break;
+ }
+
+ GetItem(info);
+
+ wxListItemAttr *attr = info.GetAttributes();
+ if ( !attr || !attr->HasTextColour() )
+ {
+ info.SetTextColour(*wxCYAN);
+
+ SetItem(info);
+
+ RefreshItem(info.m_itemId);
+ }
+ }
+ break;
+
+ case 'N': // next
+ item = GetNextItem(-1, wxLIST_NEXT_ALL, wxLIST_STATE_FOCUSED);
+ if ( item++ == GetItemCount() - 1 )
+ {
+ item = 0;
+ }
+
+ wxLogMessage(wxT("Focusing item %ld"), item);
+
+ SetItemState(item, wxLIST_STATE_FOCUSED, wxLIST_STATE_FOCUSED);
+ EnsureVisible(item);
+ break;
+
+ case 'R': // show bounding rectangle
+ {
+ item = event.GetIndex();
+ wxRect r;
+ if ( !GetItemRect(item, r) )
+ {
+ wxLogError(wxT("Failed to retrieve rect of item %ld"), item);
+ break;
+ }
+
+ wxLogMessage(wxT("Bounding rect of item %ld is (%d, %d)-(%d, %d)"),
+ item, r.x, r.y, r.x + r.width, r.y + r.height);
+ }
+ break;
+
+ case '1': // show sub item bounding rectangle for the given column
+ case '2': // (and icon/label rectangle if Shift/Ctrl is pressed)
+ case '3':
+ case '4': // this column is invalid but we want to test it too
+ if ( InReportView() )
+ {
+ int subItem = event.GetKeyCode() - '1';
+ item = event.GetIndex();
+ wxRect r;
+
+ int code = wxLIST_RECT_BOUNDS;
+ if ( wxGetKeyState(WXK_SHIFT) )
+ code = wxLIST_RECT_ICON;
+ else if ( wxGetKeyState(WXK_CONTROL) )
+ code = wxLIST_RECT_LABEL;
+
+ if ( !GetSubItemRect(item, subItem, r, code) )
+ {
+ wxLogError(wxT("Failed to retrieve rect of item %ld column %d"), item, subItem + 1);
+ break;
+ }
+
+ wxLogMessage(wxT("Bounding rect of item %ld column %d is (%d, %d)-(%d, %d)"),
+ item, subItem + 1,
+ r.x, r.y, r.x + r.width, r.y + r.height);
+ }
+ break;
+
+ case 'U': // update
+ if ( !IsVirtual() )
+ break;
+
+ if ( m_updated != -1 )
+ RefreshItem(m_updated);
+
+ m_updated = event.GetIndex();
+ if ( m_updated != -1 )
+ {
+ // we won't see changes to this item as it's selected, update
+ // the next one (or the first one if we're on the last item)
+ if ( ++m_updated == GetItemCount() )
+ m_updated = 0;
+
+ wxLogMessage("Updating colour of the item %ld", m_updated);
+ RefreshItem(m_updated);
+ }
+ break;
+
+ case 'D': // delete
+ item = GetNextItem(-1, wxLIST_NEXT_ALL, wxLIST_STATE_SELECTED);
+ while ( item != -1 )
+ {
+ DeleteItem(item);
+
+ wxLogMessage(wxT("Item %ld deleted"), item);
+
+ // -1 because the indices were shifted by DeleteItem()
+ item = GetNextItem(item - 1,
+ wxLIST_NEXT_ALL, wxLIST_STATE_SELECTED);
+ }
+ break;
+
+ case 'I': // insert
+ if ( GetWindowStyle() & wxLC_REPORT )
+ {
+ if ( GetWindowStyle() & wxLC_VIRTUAL )
+ {
+ SetItemCount(GetItemCount() + 1);
+ }
+ else // !virtual
+ {
+ InsertItemInReportView(event.GetIndex());
+ }
+ }
+ //else: fall through
+
+ default:
+ LogEvent(event, wxT("OnListKeyDown"));
+
+ event.Skip();
+ }
+}
+
+void MyListCtrl::OnChar(wxKeyEvent& event)
+{
+ wxLogMessage(wxT("Got char event."));
+
+ event.Skip();
+}
+
+void MyListCtrl::OnRightClick(wxMouseEvent& event)
+{
+ if ( !event.ControlDown() )
+ {
+ event.Skip();
+ return;
+ }
+
+ int flags;
+ long subitem;
+ long item = HitTest(event.GetPosition(), flags, &subitem);
+
+ wxString where;
+ switch ( flags )
+ {
+ case wxLIST_HITTEST_ABOVE: where = wxT("above"); break;
+ case wxLIST_HITTEST_BELOW: where = wxT("below"); break;
+ case wxLIST_HITTEST_NOWHERE: where = wxT("nowhere near"); break;
+ case wxLIST_HITTEST_ONITEMICON: where = wxT("on icon of"); break;
+ case wxLIST_HITTEST_ONITEMLABEL: where = wxT("on label of"); break;
+ case wxLIST_HITTEST_ONITEMRIGHT: where = wxT("right on"); break;
+ case wxLIST_HITTEST_TOLEFT: where = wxT("to the left of"); break;
+ case wxLIST_HITTEST_TORIGHT: where = wxT("to the right of"); break;
+ default: where = wxT("not clear exactly where on"); break;
+ }
+
+ wxLogMessage(wxT("Right double click %s item %ld, subitem %ld"),
+ where.c_str(), item, subitem);
+}
+
+void MyListCtrl::LogEvent(const wxListEvent& event, const wxChar *eventName)
+{
+ wxLogMessage(wxT("Item %ld: %s (item text = %s, data = %ld)"),
+ event.GetIndex(), eventName,
+ event.GetText().c_str(), event.GetData());
+}
+
+wxString MyListCtrl::OnGetItemText(long item, long column) const
+{
+ if ( GetItemCount() == WXSIZEOF(SMALL_VIRTUAL_VIEW_ITEMS) )
+ {
+ return SMALL_VIRTUAL_VIEW_ITEMS[item][column];
+ }
+ else // "big" virtual control
+ {
+ return wxString::Format(wxT("Column %ld of item %ld"), column, item);
+ }
+}
+
+int MyListCtrl::OnGetItemColumnImage(long item, long column) const
+{
+ if (!column)
+ return 0;
+
+ if (!(item % 3) && column == 1)
+ return 0;
+
+ return -1;
+}
+
+wxListItemAttr *MyListCtrl::OnGetItemAttr(long item) const
+{
+ // test to check that RefreshItem() works correctly: when m_updated is
+ // set to some item and it is refreshed, we highlight the item
+ if ( item == m_updated )
+ {
+ static wxListItemAttr s_attrHighlight(*wxRED, wxNullColour, wxNullFont);
+ return &s_attrHighlight;
+ }
+
+ return wxListCtrl::OnGetItemAttr(item);
+}
+
+void MyListCtrl::InsertItemInReportView(int i)
+{
+ wxString buf;
+ buf.Printf(wxT("This is item %d"), i);
+ long tmp = InsertItem(i, buf, 0);
+ SetItemData(tmp, i);
+
+ buf.Printf(wxT("Col 1, item %d"), i);
+ SetItem(tmp, 1, buf);
+
+ buf.Printf(wxT("Item %d in column 2"), i);
+ SetItem(tmp, 2, buf);
+}
+
+#if USE_CONTEXT_MENU
+void MyListCtrl::OnContextMenu(wxContextMenuEvent& event)
+{
+ if (GetEditControl() == NULL)
+ {
+ wxPoint point = event.GetPosition();
+ // If from keyboard
+ if ( (point.x == -1) && (point.y == -1) )
+ {
+ wxSize size = GetSize();
+ point.x = size.x / 2;
+ point.y = size.y / 2;
+ }
+ else
+ {
+ point = ScreenToClient(point);
+ }
+ ShowContextMenu(point);
+ }
+ else
+ {
+ // the user is editing:
+ // allow the text control to display its context menu
+ // if it has one (it has on Windows) rather than display our one
+ event.Skip();
+ }
+}
+#endif
+
+void MyListCtrl::ShowContextMenu(const wxPoint& pos)
+{
+ wxMenu menu;
+
+ menu.Append(wxID_ABOUT, wxT("&About"));
+ menu.AppendSeparator();
+ menu.Append(wxID_EXIT, wxT("E&xit"));
+
+ PopupMenu(&menu, pos.x, pos.y);
+}