Reading back contents of wxGrid wxGridCellChoiceEditor

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
Widgets
Ultimate wxWidgets Guru
Ultimate wxWidgets Guru
Posts: 534
Joined: Thu Jun 01, 2006 4:36 pm
Location: Right here!

Reading back contents of wxGrid wxGridCellChoiceEditor

Post by Widgets »

In a wxGrid with a variable number of rows, I have one column which can contain a choice drop down.
In order to allow the user to move existing lines up or down in the grid, i.e. reorder the rows, I want to read back the contents of the choice box which is populated when the row is first inserted so that I can reinsert them after the row is moved up or down.
I have tried

Code: Select all

      wxGridCellChoiceEditor *pCellEditor = 
        (wxGridCellChoiceEditor *)m_gridTemplate->GetCellEditor( iSelRow + 1, TE_COL_LIST );
      pCellEditor->DecRef();
      m_gridTemplate->SetCellEditor(    iSelRow - 1, TE_COL_TYPE, pCellEditor->Clone() );
This compiles and runs, but the choice box values are not carried to the new row.

Will I have to remember the contents aside from the choice box and re-insert them once the row is in its new position, or is there a way to access the data and read it back?
TIA
Environment: Win 10/11 64-bit & Mint 21.1
MSVC Express 2019/2022
wxWidgets 3.2.2
User avatar
doublemax
Moderator
Moderator
Posts: 19116
Joined: Fri Apr 21, 2006 8:03 pm
Location: $FCE2

Re: Reading back contents of wxGrid wxGridCellChoiceEditor

Post by doublemax »

Code: Select all

wxGridCellEditor *wxGridCellChoiceEditor::Clone() const
{
    wxGridCellChoiceEditor *editor = new wxGridCellChoiceEditor;
    editor->m_allowOthers = m_allowOthers;
    editor->m_choices = m_choices;

    return editor;
}
In the source code the choices are copied in Clone(). Try tracing through the code and check if m_choices contains the expected values.

Are you sure the "iSelRow + 1" and "iSelRow - 1" are correct? If a row is moved up, the difference between the old position and the new position should be 1, not 2.
Use the source, Luke!
Widgets
Ultimate wxWidgets Guru
Ultimate wxWidgets Guru
Posts: 534
Joined: Thu Jun 01, 2006 4:36 pm
Location: Right here!

Re: Reading back contents of wxGrid wxGridCellChoiceEditor

Post by Widgets »

Thank you :-)

Tracing through the code today finally got me the answer; yesterday it was a bit too late in the day for clear thinking. I had been tracing through the code, but never really caught on ...
Clone() did copy the values, but I stuffed the editor into the wrong column :oops: #-o

As for the indexing, that part is/was correct, because to move a row up I need to insert a new row above the row above the current row (and eventually delete the 'old' row) and thus difference will be 2 - same idea for moving a row down
Environment: Win 10/11 64-bit & Mint 21.1
MSVC Express 2019/2022
wxWidgets 3.2.2
Widgets
Ultimate wxWidgets Guru
Ultimate wxWidgets Guru
Posts: 534
Joined: Thu Jun 01, 2006 4:36 pm
Location: Right here!

Re: Reading back contents of wxGrid wxGridCellChoiceEditor

Post by Widgets »

Is there a way to actually retrieve the data values in the choice box??
I would like to save the edited values, but it seems m_choices is inaccessible :?
Environment: Win 10/11 64-bit & Mint 21.1
MSVC Express 2019/2022
wxWidgets 3.2.2
User avatar
doublemax
Moderator
Moderator
Posts: 19116
Joined: Fri Apr 21, 2006 8:03 pm
Location: $FCE2

Re: Reading back contents of wxGrid wxGridCellChoiceEditor

Post by doublemax »

I would like to save the edited values, but it seems m_choices is inaccessible
Yes, it's not exposed through the API.

1) As you're the one filling the choices, shouldn't you be able to reconstruct the values?

2) You could subclass wxGridCellChoiceEditor
Use the source, Luke!
Widgets
Ultimate wxWidgets Guru
Ultimate wxWidgets Guru
Posts: 534
Joined: Thu Jun 01, 2006 4:36 pm
Location: Right here!

Re: Reading back contents of wxGrid wxGridCellChoiceEditor

Post by Widgets »

doublemax wrote:
I would like to save the edited values, but it seems m_choices is inaccessible
Yes, it's not exposed through the API.
:(
doublemax wrote: 1) As you're the one filling the choices, shouldn't you be able to reconstruct the values?
That is how I am handling it now - but it is a bit of extra trouble; it would be much simpler if one could read back the data for either saving or manipulating it, such as sorting, especially if one wants to set 'allowOthers' to true, so that the user can add to the contents and expect it to be saved for the next invocation. In that case reading back seems most important.
This scenario is something I believe I will need/want - haven't started to implement it, yet - and I can see where it would be even more work.
doublemax wrote: 2) You could subclass wxGridCellChoiceEditor
Another possibility, but it seems to me that the choice box in the grid ought to behave and be used/usable in as much of the same way it is normally used in other contexts.
Environment: Win 10/11 64-bit & Mint 21.1
MSVC Express 2019/2022
wxWidgets 3.2.2
ONEEYEMAN
Part Of The Furniture
Part Of The Furniture
Posts: 7459
Joined: Sat Apr 16, 2005 7:22 am
Location: USA, Ukraine

Re: Reading back contents of wxGrid wxGridCellChoiceEditor

Post by ONEEYEMAN »

Hi,
Behave - yes.
For the usage it is a different issue.

In the grid the editor is created on demand when the user starts editing the value in the cell and is destoryed when the user finishes editing. So it is different in a sense that the control does not stay thruogh the life time of the window.
You can retrieve the actual wxComboBox by getting the editor and then getting the underlying control of the editor and from there you can get a selection and all the choices in the combo box. But that's would be counterintuitive, since you are the one filling it out.

Thank you.
Widgets
Ultimate wxWidgets Guru
Ultimate wxWidgets Guru
Posts: 534
Joined: Thu Jun 01, 2006 4:36 pm
Location: Right here!

Re: Reading back contents of wxGrid wxGridCellChoiceEditor

Post by Widgets »

ONEEYEMAN wrote: Behave - yes.
For the usage it is a different issue.
'Usage' from a user's POV: yes - sort of, because wxChoice as I understand it, does not allow the user to add items to the list, but the wxGrid version does
'usage' from a programmer's POV: NO :D
ONEEYEMAN wrote: ...... But that's would be counterintuitive, since you are the one filling it out.
But I am not filling it out when I allow the user to add items to the list

Code: Select all

wxGridCellChoiceEditor::wxGridCellChoiceEditor  ( size_t  count = 0,  
  const wxString  choices[] = NULL,  
  bool  allowOthers = false  
 )

where the documentation says:
allowOthers If allowOthers is true, the user can type a string not in choices array.
Environment: Win 10/11 64-bit & Mint 21.1
MSVC Express 2019/2022
wxWidgets 3.2.2
Post Reply