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.
buttonsrtoys
Knows some wx things
Knows some wx things
Posts: 42
Joined: Mon Jul 03, 2017 12:03 am

wxDataViewColumn with more than one wxDataViewChoiceRenderer?

Postby buttonsrtoys » Wed Jun 13, 2018 4:29 pm

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: 12603
Joined: Fri Apr 21, 2006 8:03 pm
Location: $FCE2

Re: wxDataViewColumn with more than one wxDataViewChoiceRenderer?

Postby doublemax » Wed Jun 13, 2018 4:59 pm

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
Knows some wx things
Knows some wx things
Posts: 42
Joined: Mon Jul 03, 2017 12:03 am

Re: wxDataViewColumn with more than one wxDataViewChoiceRenderer?

Postby buttonsrtoys » Wed Jun 13, 2018 7:48 pm

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: 12603
Joined: Fri Apr 21, 2006 8:03 pm
Location: $FCE2

Re: wxDataViewColumn with more than one wxDataViewChoiceRenderer?

Postby doublemax » Wed Jun 13, 2018 8:17 pm

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
Knows some wx things
Knows some wx things
Posts: 42
Joined: Mon Jul 03, 2017 12:03 am

Re: wxDataViewColumn with more than one wxDataViewChoiceRenderer?

Postby buttonsrtoys » Wed Jun 13, 2018 10:29 pm

Thanks for the ideas doublemax! Unfortunately, GetEditorCtrl() returned null :(

User avatar
doublemax
Moderator
Moderator
Posts: 12603
Joined: Fri Apr 21, 2006 8:03 pm
Location: $FCE2

Re: wxDataViewColumn with more than one wxDataViewChoiceRenderer?

Postby doublemax » Wed Jun 13, 2018 10:58 pm

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
Knows some wx things
Knows some wx things
Posts: 42
Joined: Mon Jul 03, 2017 12:03 am

Re: wxDataViewColumn with more than one wxDataViewChoiceRenderer?

Postby buttonsrtoys » Thu Jun 14, 2018 10:54 am

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: 12603
Joined: Fri Apr 21, 2006 8:03 pm
Location: $FCE2

Re: wxDataViewColumn with more than one wxDataViewChoiceRenderer?

Postby doublemax » Thu Jun 14, 2018 12:20 pm

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
Ultimate wxWidgets Guru
Ultimate wxWidgets Guru
Posts: 579
Joined: Mon Apr 30, 2012 11:07 pm

Re: wxDataViewColumn with more than one wxDataViewChoiceRenderer?

Postby Manolo » Thu Jun 14, 2018 12:54 pm

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
Knows some wx things
Knows some wx things
Posts: 42
Joined: Mon Jul 03, 2017 12:03 am

Re: wxDataViewColumn with more than one wxDataViewChoiceRenderer?

Postby buttonsrtoys » Thu Jun 14, 2018 12:58 pm

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
Knows some wx things
Knows some wx things
Posts: 42
Joined: Mon Jul 03, 2017 12:03 am

Re: wxDataViewColumn with more than one wxDataViewChoiceRenderer?

Postby buttonsrtoys » Thu Jun 14, 2018 1:20 pm

Thanks Manolo and doublemax! Much appreciated


Return to “C++ Development”

Who is online

Users browsing this forum: Google [Bot], rudolfninja and 5 guests