Page 1 of 1

How to add multiple void* data into wxListBox?

Posted: Mon Dec 16, 2013 5:15 pm
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.

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

Posted: Mon Dec 16, 2013 5:35 pm
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.

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

Posted: Mon Dec 16, 2013 6:10 pm
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 %.

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

Posted: Mon Dec 16, 2013 6:19 pm
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).

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

Posted: Mon Dec 16, 2013 7:59 pm
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.

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

Posted: Mon Dec 16, 2013 10:07 pm
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.

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

Posted: Tue Dec 17, 2013 3:26 pm
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 :)

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

Posted: Tue Dec 17, 2013 6:48 pm
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)

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

Posted: Wed Dec 18, 2013 4:49 pm
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).

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

Posted: Wed Dec 18, 2013 4:59 pm
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.

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

Posted: Wed Dec 18, 2013 5:01 pm
by Tapsa
Well, I don't mean multiple selecting like it is in that sample... :(
wxMSW and 3.0.0.

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

Posted: Wed Dec 18, 2013 5:14 pm
by doublemax
Well, I don't mean multiple selecting like it is in that sample
What do you mean then?

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

Posted: Wed Dec 18, 2013 5:31 pm
by Tapsa
Multiple selecting with single mouse click/drag.

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

Posted: Wed Dec 18, 2013 5:43 pm
by doublemax
So you mean the wxLB_EXTENDED style for wxListBox. Seems that wxListCtrl does not support that.