How to add multiple void* data into wxListBox? 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

How to add multiple void* data into wxListBox?

Post by Tapsa »

I know how to add wxArrayString into wxListBox with single Append but I haven't figured out how I can add custom data at the same time.

This is how I add custom data with string one by one:
myWxListBox->Append(myString, (void*)&myVariableInVector);

How to preadd custom data into void** (no C malloc stuff etc) and then add it at once into wxListBox?
I cannot simply call myWxListBox->Append(myWxArrayString, myVector);
Appends several items at once into the control.
Notice that calling this method is usually much faster than appending them one by one if you need to add a lot of items.
I have about 11 000 items to add and the list needs to be refreshed repeatedly as the shown items change.
User avatar
doublemax
Moderator
Moderator
Posts: 19116
Joined: Fri Apr 21, 2006 8:03 pm
Location: $FCE2

Re: How to add multiple void* data into wxListBox?

Post by doublemax »

As you already noticed yourself, there is no method to add multiple entries together with client data.

However, for that number of items, especially if they're dynamic, you should always use a virtual control. In this case, i'd suggest a virtual wxListCtrl. Without header and with only one column, it would look almost the same as a wxListBox.
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: How to add multiple void* data into wxListBox?

Post by Tapsa »

:( Thanks for the suggestion. Sounds a bit complicated...
So would myWxListBox->GetClientData(integer) still work?
Why do I need to override useless OnGetItemText?

Anyhow, it appears that assigning data later in a for loop and appending all strings as wxArrayString decreases the overall time by ~30 %.
User avatar
doublemax
Moderator
Moderator
Posts: 19116
Joined: Fri Apr 21, 2006 8:03 pm
Location: $FCE2

Re: How to add multiple void* data into wxListBox?

Post by doublemax »

Thanks for the suggestion. Sounds a bit complicated.
Not really. The idea is that you don't put any data into the control. You just tell the control how many entries it has. Then you subclass wxListCtrl and override wxListCtrl::OnGetItemText(). For each row/column that needs to be displayed, this methods will be called and you just return the string.

It's ideal if you already have all the data in memory. And i assume that's the case for you. And as you're working on your own dataset, anything like SetClientData() is not necessary (and wouldn't work for a virtual control anyway).
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: How to add multiple void* data into wxListBox?

Post by Tapsa »

It seems that wxListCtrl doesn't have GetSelections(), SetSelection(), SetFirstItem().
For example, in a multiple selection virtual list control, the selections won't be sent when many items are selected at once because this could mean iterating over all the items.
Yes, I already have all the data in memory.
Maybe I could hide integers (position in vector) behind \0 in wxString.
User avatar
doublemax
Moderator
Moderator
Posts: 19116
Joined: Fri Apr 21, 2006 8:03 pm
Location: $FCE2

Re: How to add multiple void* data into wxListBox?

Post by doublemax »

It seems that wxListCtrl doesn't have GetSelections(), SetSelection(), SetFirstItem().
The API for wxListCtrl is different, but this is all possible. Check the "listctrl" sample that comes with wxWIdgets. Alternatively check wxListView which is a simple wrapper around wxListCtrl.
http://docs.wxwidgets.org/trunk/classwx_list_view.html
Yes, I already have all the data in memory.
Maybe I could hide integers (position in vector) behind \0 in wxString.
No. You don't need to store any client data. When OnGetItemText() gets called, you get "item" as parameter. This is the index into your (internal) dat and this should be all you need.
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: How to add multiple void* data into wxListBox?

Post by Tapsa »

With wxListBox I can select multiple items by holding mouse left button down. Is it possible with custom wxListView?
I also need only one column which should always be the size of the widest item string.
Custom wxListView seems to be insanely faster than wxListBox :)
User avatar
doublemax
Moderator
Moderator
Posts: 19116
Joined: Fri Apr 21, 2006 8:03 pm
Location: $FCE2

Re: How to add multiple void* data into wxListBox?

Post by doublemax »

With wxListBox I can select multiple items by holding mouse left button down. Is it possible with custom wxListView
Yes, by *not* setting the wxLC_SINGLE_SEL style flag when creating the control. (Multiple selection is the default)
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: How to add multiple void* data into wxListBox?

Post by Tapsa »

I have not set wxLC_SINGLE_SEL. I use this:
wxListView(parent, wxID_ANY, wxDefaultPosition, size, wxLC_VIRTUAL | wxLC_REPORT | wxLC_NO_HEADER)

Without mouse drag selection selecting many at once becomes a lot harder (needs keyboard input).
User avatar
doublemax
Moderator
Moderator
Posts: 19116
Joined: Fri Apr 21, 2006 8:03 pm
Location: $FCE2

Re: How to add multiple void* data into wxListBox?

Post by doublemax »

Which platform and wxWidgets version are you using?

Check the "listctrl" sample, press F7 to test a virtual view. Multiple selection with mouse and keyboard works fine there for me.
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: How to add multiple void* data into wxListBox?

Post by Tapsa »

Well, I don't mean multiple selecting like it is in that sample... :(
wxMSW and 3.0.0.
User avatar
doublemax
Moderator
Moderator
Posts: 19116
Joined: Fri Apr 21, 2006 8:03 pm
Location: $FCE2

Re: How to add multiple void* data into wxListBox?

Post by doublemax »

Well, I don't mean multiple selecting like it is in that sample
What do you mean then?
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: How to add multiple void* data into wxListBox?

Post by Tapsa »

Multiple selecting with single mouse click/drag.
User avatar
doublemax
Moderator
Moderator
Posts: 19116
Joined: Fri Apr 21, 2006 8:03 pm
Location: $FCE2

Re: How to add multiple void* data into wxListBox?

Post by doublemax »

So you mean the wxLB_EXTENDED style for wxListBox. Seems that wxListCtrl does not support that.
Use the source, Luke!
Post Reply