Problem with wxGrid -> GetSelectedCells

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
Rast
In need of some credit
In need of some credit
Posts: 2
Joined: Fri Feb 03, 2006 8:18 am

Problem with wxGrid -> GetSelectedCells

Post by Rast » Fri Feb 03, 2006 8:30 am

Hello,

I need to know, which cells of my wxGrid are selected. So I tried to use the wxGrid - function GetSelectedCells(). But the list, which is returned, is always empty.
Here is the code:

Code: Select all

	wxGridCellCoordsArray cells;
	cells = grid->GetSelectedCells();
So why is the list empty although some cells are selected?

phlox81
wxWorld Domination!
wxWorld Domination!
Posts: 1387
Joined: Thu Aug 18, 2005 7:49 pm
Location: Germany
Contact:

Post by phlox81 » Fri Feb 03, 2006 8:45 am


Rast
In need of some credit
In need of some credit
Posts: 2
Joined: Fri Feb 03, 2006 8:18 am

Post by Rast » Fri Feb 03, 2006 11:20 am

Thanks for your answer but I think the thread above doesn't solve my problem.

I understand that there are problems with the selection of full rows. And it is necessary to set the SelectionMode.
In my case it should be possible to select blocks of cells but no full rows or cols. So I set the SelectionMode to wxGridSelectCells, which is also standard for the SelectionMode.
So I think it should be possible to use GetSelectedCells to get a list, which contains all selected cells, shouldn't it?

kbsimm
Earned a small fee
Earned a small fee
Posts: 10
Joined: Tue Feb 14, 2006 3:46 pm
Location: Seattle
Contact:

Solution for wxGrid::GetSelectedCells

Post by kbsimm » Tue Mar 14, 2006 7:02 pm

I too was having problems with retrieving cell selection from wxGrid so I have looked at the source and here is a solution that allows GetSelectedCells to return the correct information. There are other issues about row and column selectio that I will also discuss.

See bugs 1443956 http://sourceforge.net/tracker/index.ph ... tid=109863 and 1217401 http://sourceforge.net/tracker/index.ph ... tid=109863

Row, Column, and Cell selection information is stored in a wxGridSelection object in wxGrid. Once a grid is instantiated one needs to call CreateGrid or SetTable depending on how one is populating the grid. In 2.6.2 the CreateGrid method created a wxGridSelection object but in SetTable wxGridSelection was missing. That problem is fixed in 2.6.3 so both CreateGrid and SetTable will have a wxGridSelection object. (Side question: How do I relate this fix to the exact Bug for which it was fixed?)

Cell selection is kept in a wxGridCellCoordsArray in the wxGridSelection object but it is never populated. So a call to wxGrid::GetSelectedCells always returns 0.

So to fix this we look at the wxGrid::ProcessGridCellMouseEvent method where it calls wxGridSelection::SelectBlock on Event.LeftUp (wxGridSelection is m_selection in the code). In SelectBlock we are missing any code to populate the cells. wxGridSelection is in gridsel.cpp. So about line 515 we need to add the following code (standards aside):

Code: Select all

for (int i = topRow; i <= bottomRow; i++)
{
  for (int j = leftCol; j <= rightCol; j++)
  {
    m_cellSelection.Add(wxGridCellCoords(i, j));
  }
}
Now GetSelectedCells returns the correct information.

Further discussions:
1) GetSelectedRows and GetSelectedCols still return 0. They only report rows and columns if the row and column is selected in the row or column header. If the row or column is selected by just selecting cells they report 0. One could debate rather that is proper behavoir. If in cell selection mode rather than row selection mode is a row consider selected if at least one but less than all cells in that row are selected?
2) When only one cell is selected but the mouse is not dragged (or is that drug) the cell border is highlighted rather than background color highlighting. What is the mode of the cell at that point and should it be selected or not?
3) There seems to be a lot of unneccesary code in SelectBlock. I am not sure of the intent or where all SelectBlock is used but all the code about checking to see if a block is selected relative to the new selection and then deselecting it seems unnecessary from my 10,000 foot view. If I am selecting a new block why not just deselect everything first. Perhaps there is a case where I might be selecting an additional block.
4) There may be other cases such as would occur with CTRL, SHIFT, ALT, etc that may need to be examined.

So what is the process (or where is the process documented) of getting this code into the code base?
thanks

Brad

phlox81
wxWorld Domination!
wxWorld Domination!
Posts: 1387
Joined: Thu Aug 18, 2005 7:49 pm
Location: Germany
Contact:

Re: Solution for wxGrid::GetSelectedCells

Post by phlox81 » Wed Mar 15, 2006 3:18 pm

kbsimm wrote: So to fix this we look at the wxGrid::ProcessGridCellMouseEvent method where it calls wxGridSelection::SelectBlock on Event.LeftUp (wxGridSelection is m_selection in the code). In SelectBlock we are missing any code to populate the cells. wxGridSelection is in gridsel.cpp. So about line 515 we need to add the following code (standards aside):

Code: Select all

for (int i = topRow; i <= bottomRow; i++)
{
  for (int j = leftCol; j <= rightCol; j++)
  {
    m_cellSelection.Add(wxGridCellCoords(i, j));
  }
}
Now GetSelectedCells returns the correct information.

Further discussions:
1) GetSelectedRows and GetSelectedCols still return 0. They only report rows and columns if the row and column is selected in the row or column header. If the row or column is selected by just selecting cells they report 0. One could debate rather that is proper behavoir. If in cell selection mode rather than row selection mode is a row consider selected if at least one but less than all cells in that row are selected?
2) When only one cell is selected but the mouse is not dragged (or is that drug) the cell border is highlighted rather than background color highlighting. What is the mode of the cell at that point and should it be selected or not?
3) There seems to be a lot of unneccesary code in SelectBlock. I am not sure of the intent or where all SelectBlock is used but all the code about checking to see if a block is selected relative to the new selection and then deselecting it seems unnecessary from my 10,000 foot view. If I am selecting a new block why not just deselect everything first. Perhaps there is a case where I might be selecting an additional block.
4) There may be other cases such as would occur with CTRL, SHIFT, ALT, etc that may need to be examined.

So what is the process (or where is the process documented) of getting this code into the code base?
Hm, nice work.

to 1) I think, if I have the selectRow/Col Mode on, than it should select full row or col by clicking one cell. At least for selectRow this would be useful.
to 2) It should be enough to click in a cell to select it afaik.
to 3) hm, dunno. But not all code which looks unnecessary is useless.

phlox

Post Reply