wxDataViewColumn with more than one wxDataViewChoiceRenderer?

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
buttonsrtoys
Experienced Solver
Experienced Solver
Posts: 58
Joined: Mon Jul 03, 2017 12:03 am

wxDataViewColumn with more than one wxDataViewChoiceRenderer?

Post by buttonsrtoys »

The code below creates two columns of widgets in a wxDataViewListCtrl. The first is a column of toggles and the second is a column of choices that present a drop-down that gives the options "Default"/"Custom". Works fine except I'm realizing I need different choices for each row. E.g., row 1 might have Default/Custom, row 2 Red/Blue/Green, etc.

I'm new to Wx. What would be the widgets-of-choice to adapt below?

Code: Select all

wxDataViewListCtrl* MapsPanel::CreateLayerCtrl()
{
	int style = wxTL_CHECKBOX;

	wxDataViewListCtrl* const ctrl = new wxDataViewListCtrl( this, kMPLayerCtrl, wxDefaultPosition, wxDefaultSize, style );
	wxDataViewToggleRenderer *toggleRenderer = new wxDataViewToggleRenderer( "bool", wxDATAVIEW_CELL_ACTIVATABLE );
	wxDataViewColumn *toggleColumn = new wxDataViewColumn( "Show", toggleRenderer, 0, 20 );
	ctrl->AppendColumn( toggleColumn, "bool" );

	wxArrayString f;
	f.Add("Default");
	f.Add("Custom");
	wxDataViewChoiceRenderer* featuresRenderer = new wxDataViewChoiceRenderer(f);
	wxDataViewColumn *featuresColumn = new wxDataViewColumn( "Features", featuresRenderer, 1, 80 );
	ctrl->AppendColumn( featuresColumn, "int" );

	return ctrl;
}
Last edited by doublemax on Wed Jun 13, 2018 4:53 pm, edited 1 time in total.
Reason: Added code tags
User avatar
doublemax
Moderator
Moderator
Posts: 19114
Joined: Fri Apr 21, 2006 8:03 pm
Location: $FCE2

Re: wxDataViewColumn with more than one wxDataViewChoiceRenderer?

Post by doublemax »

I don't know much about wxDVC, but i would try to subclass wxDataViewChoiceRenderer, overload wxDataViewRenderer::StartEditing, and set the list of choices depending on the item.
Use the source, Luke!
buttonsrtoys
Experienced Solver
Experienced Solver
Posts: 58
Joined: Mon Jul 03, 2017 12:03 am

Re: wxDataViewColumn with more than one wxDataViewChoiceRenderer?

Post by buttonsrtoys »

Thanks doublemax. I subclassed as you suggested and override ::StartEditing(), but I'm stuck on how to change the list of choices. I'm probably missing something, but the wxDataViewChoiceRenderer constructor accepts a wxArrayString as a list of choices, but there's no "SetChoices" method to subsequently change the choices?
User avatar
doublemax
Moderator
Moderator
Posts: 19114
Joined: Fri Apr 21, 2006 8:03 pm
Location: $FCE2

Re: wxDataViewColumn with more than one wxDataViewChoiceRenderer?

Post by doublemax »

Try
wxWindow* wxDataViewRenderer::GetEditorCtrl() and cast it to wxChoice*.
http://docs.wxwidgets.org/trunk/classwx ... 217db219db

I've never done this before, just guessing :)
Use the source, Luke!
buttonsrtoys
Experienced Solver
Experienced Solver
Posts: 58
Joined: Mon Jul 03, 2017 12:03 am

Re: wxDataViewColumn with more than one wxDataViewChoiceRenderer?

Post by buttonsrtoys »

Thanks for the ideas doublemax! Unfortunately, GetEditorCtrl() returned null :(
User avatar
doublemax
Moderator
Moderator
Posts: 19114
Joined: Fri Apr 21, 2006 8:03 pm
Location: $FCE2

Re: wxDataViewColumn with more than one wxDataViewChoiceRenderer?

Post by doublemax »

First call the base method for StartEditing(), afterwards the editor should have been created.

BTW: All this will most likely only work in the generic implementation of wxDVC which is only used under Windows.

After taking a closer look the the methods, i don't see any "clean" way to do it. The control is not designed for datasets like that, it assumes that all fields in one column are treated equally.
Use the source, Luke!
buttonsrtoys
Experienced Solver
Experienced Solver
Posts: 58
Joined: Mon Jul 03, 2017 12:03 am

Re: wxDataViewColumn with more than one wxDataViewChoiceRenderer?

Post by buttonsrtoys »

Ah. OK, calling wxDVR::StartEditing() created the editor as you predicted.

I'm OK with Windows-only.

Because there's no clean way to make the choices row-dependent, I should bail on wxDataViewColumns? I'm OK with that. Just clarifying.
User avatar
doublemax
Moderator
Moderator
Posts: 19114
Joined: Fri Apr 21, 2006 8:03 pm
Location: $FCE2

Re: wxDataViewColumn with more than one wxDataViewChoiceRenderer?

Post by doublemax »

buttonsrtoys wrote:Because there's no clean way to make the choices row-dependent, I should bail on wxDataViewColumns? I'm OK with that. Just clarifying.
I'm not sure what you mean by that. How else do you want to so it?
Use the source, Luke!
Manolo
Can't get richer than this
Can't get richer than this
Posts: 827
Joined: Mon Apr 30, 2012 11:07 pm

Re: wxDataViewColumn with more than one wxDataViewChoiceRenderer?

Post by Manolo »

If all your choices are the same for all items (rows) then the best is that you use wxDataViewChoiceRenderer or wxDataViewChoiceByIndexRenderer. In the 'dataview' sample you can see how to use it.

Now, you want different choices for every item. If wxDataViewChoiceRenderer would have a member to change the choices... But it hasn't, likely because native wxDVC doesn't allow it.

When you [double] click on an item wxEVT_DATAVIEW_ITEM_START_EDITING event is triggered. And in its handler the wxDataViewEvent contains both item and column. You can store them in your model.

Now, to change the choices you need your own "wxDataViewChoiceRenderer". Derive one from wxDataViewCustomRenderer. You must override some members: http://docs.wxwidgets.org/trunk/classwx ... derer.html. Use the item and column you stored to set the proper choices.

As a side note, your model may identify choices by IDs instead of by strings. Be sure to give different values to all choices items.
buttonsrtoys
Experienced Solver
Experienced Solver
Posts: 58
Joined: Mon Jul 03, 2017 12:03 am

Re: wxDataViewColumn with more than one wxDataViewChoiceRenderer?

Post by buttonsrtoys »

All I meant was what I stated in my OP, which is I need each row of my column to support a different choice. E.g., row 1 might have Default/Custom, row 2 might have Red/Green/Blue. If I'm following our exchange, there's no subclass of wxDataViewRenderer that supports this? So, I should bail on using wxDataViewColumns?
buttonsrtoys
Experienced Solver
Experienced Solver
Posts: 58
Joined: Mon Jul 03, 2017 12:03 am

Re: wxDataViewColumn with more than one wxDataViewChoiceRenderer?

Post by buttonsrtoys »

Thanks Manolo and doublemax! Much appreciated
diogom12
Earned a small fee
Earned a small fee
Posts: 24
Joined: Sat Jun 13, 2020 6:07 am

Re: wxDataViewColumn with more than one wxDataViewChoiceRenderer?

Post by diogom12 »

I'm stuck in the same problem here, did you managed to make it work using wxDataViewCustomRenderer? If possible can you show how did you do it? thanks!
diogom12
Earned a small fee
Earned a small fee
Posts: 24
Joined: Sat Jun 13, 2020 6:07 am

Re: wxDataViewColumn with more than one wxDataViewChoiceRenderer?

Post by diogom12 »

Just to update, I gave up using these controlers and used wxGrid instead, much easier.
ONEEYEMAN
Part Of The Furniture
Part Of The Furniture
Posts: 7458
Joined: Sat Apr 16, 2005 7:22 am
Location: USA, Ukraine

Re: wxDataViewColumn with more than one wxDataViewChoiceRenderer?

Post by ONEEYEMAN »

Hi,
Or you can try to use wxListCtrl.

Thank you.
Post Reply