What is the wxListCtrl equivalent of wxEVT_COMMAND_LISTBOX_SELECTED? Topic is solved

If you are using the main C++ distribution of wxWidgets, Feel free to ask any question related to wxWidgets development here. This means questions regarding to C++ and wxWidgets, not compile problems.
Post Reply
Tapsa
Earned some good credits
Earned some good credits
Posts: 147
Joined: Tue Dec 06, 2011 5:52 pm
Location: Helsinki

What is the wxListCtrl equivalent of wxEVT_COMMAND_LISTBOX_SELECTED?

Post by Tapsa »

And if I want my derived control to save selected items, how can I know that shift key was used to selected several items at once?
I assume that selection event has stored the selected index that I could use, but probably skipping items selected using shift key.
User avatar
doublemax
Moderator
Moderator
Posts: 19114
Joined: Fri Apr 21, 2006 8:03 pm
Location: $FCE2

Re: What is the wxListCtrl equivalent of wxEVT_COMMAND_LISTBOX_SELECTED?

Post by doublemax »

wxEVT_COMMAND_LISTBOX_SELECTED ~ wxEVT_LIST_ITEM_SELECTED

With wxListCtrl::GetSelectedItemCount() you get the total number of selected items.
With wxListCtrl::GetNextItem() you can get all selected items in a loop.
http://docs.wxwidgets.org/trunk/classwx ... 89caa32e78
Use the source, Luke!
Tapsa
Earned some good credits
Earned some good credits
Posts: 147
Joined: Tue Dec 06, 2011 5:52 pm
Location: Helsinki

Re: What is the wxListCtrl equivalent of wxEVT_COMMAND_LISTBOX_SELECTED?

Post by Tapsa »

Thanks. I got the next item code to work (though from an event hooked to wxListBox). I get
error: 'wxEVT_LIST_ITEM_SELECTED' was not declared in this scope
with the following code and I do have <wx/listctrl.h> included.

Code: Select all

Connect(Graphics_Graphics_ListV->GetId(), wxEVT_LIST_ITEM_SELECTED , wxListEventHandler(AGE_Frame::OnGraphicsSelect));
Can connect be used with wxListEvents?

It looks like wxWidgets 2.8 has wxEVT_COMMAND_LIST_ITEM_SELECTED.
User avatar
doublemax
Moderator
Moderator
Posts: 19114
Joined: Fri Apr 21, 2006 8:03 pm
Location: $FCE2

Re: What is the wxListCtrl equivalent of wxEVT_COMMAND_LISTBOX_SELECTED?

Post by doublemax »

It looks like wxWidgets 2.8 has wxEVT_COMMAND_LIST_ITEM_SELECTED.
If you're using 2.8 you have to use wxEVT_COMMAND_LIST_ITEM_SELECTED indeed.
Use the source, Luke!
Tapsa
Earned some good credits
Earned some good credits
Posts: 147
Joined: Tue Dec 06, 2011 5:52 pm
Location: Helsinki

Re: What is the wxListCtrl equivalent of wxEVT_COMMAND_LISTBOX_SELECTED?

Post by Tapsa »

While that kinda solves my original question, that event doesn't catch selection changes when deselecting or when selecting many items with shift button down. I was looking for one event to rule them all. I guess I can write a wrapper that connects all the necessary events. During my short affair with Qt I faced the same problem and I had to connect the underlaying selection model, but it had selectionChanged event that covered all my use cases.

I also find it annoying that I can't select many items just by holding down left mouse button, like I can with wxListBox.
And I would like the column width to always be the width of the text control.
User avatar
doublemax
Moderator
Moderator
Posts: 19114
Joined: Fri Apr 21, 2006 8:03 pm
Location: $FCE2

Re: What is the wxListCtrl equivalent of wxEVT_COMMAND_LISTBOX_SELECTED?

Post by doublemax »

that event doesn't catch selection changes when deselecting or when selecting many items with shift button down.
I just tested this with the "listctrl" sample in 2.8.12 and 3.0.2. I always get Selection/Deselection events, even when the Shift-key was used to select multiple items.
Use the source, Luke!
Tapsa
Earned some good credits
Earned some good credits
Posts: 147
Joined: Tue Dec 06, 2011 5:52 pm
Location: Helsinki

Re: What is the wxListCtrl equivalent of wxEVT_COMMAND_LISTBOX_SELECTED?

Post by Tapsa »

Did you treat it as a wxListEvent or wxCommandEvent? It looks like both can be applied and the result is same.
It's time for me to write some wrapper function then.

This is my simple list control class. Perhaps it's those style flags :?

Code: Select all

class AGEListView: public wxListCtrl
{
public:
    AGEListView(wxWindow *parent, const wxSize &size):
    wxListCtrl(parent, wxID_ANY, wxDefaultPosition, size, wxLC_VIRTUAL | wxLC_REPORT | wxLC_NO_HEADER)
    {
        SetItemCount(0);
        InsertColumn(0, wxEmptyString, wxLIST_FORMAT_LEFT, 200);
    }

    wxArrayString names;
    vector<int> indexes;

private:
    virtual wxString OnGetItemText(long item, long column) const;
};
Seeing that there are both EVT_LIST_ITEM_SELECTED and EVT_LIST_ITEM_DESELECTED macros and that the documentation doesn't have other selection event macros, I think EVT_LIST_ITEM_SELECTED is meant to only process basic left mouse button clicks that make an item selected. In short, there is no equivalent for wxEVT_COMMAND_LISTBOX_SELECTED in wxListCtrl.
User avatar
doublemax
Moderator
Moderator
Posts: 19114
Joined: Fri Apr 21, 2006 8:03 pm
Location: $FCE2

Re: What is the wxListCtrl equivalent of wxEVT_COMMAND_LISTBOX_SELECTED?

Post by doublemax »

Yes, in virtual mode the situation is different. The only event you can use in this case is wxEVT_LIST_ITEM_FOCUSED.
Use the source, Luke!
Tapsa
Earned some good credits
Earned some good credits
Posts: 147
Joined: Tue Dec 06, 2011 5:52 pm
Location: Helsinki

Re: What is the wxListCtrl equivalent of wxEVT_COMMAND_LISTBOX_SELECTED?

Post by Tapsa »

Fantastic! :D It seems to fire exactly when I need it to. Not firing on mouse hover :)
Post Reply