Can I disable multi-row select in a grid

Hi

Is it possible to disable multi-row selects in a grid?

I want to use the grid to display fixed data so I've intercepted
left/right clicks and just select the row that was clicked in (grid is in
row select mode anyway).

However, if the mouse is dragged in a cell then a range selection is done
from the first cell.

You can see the effect in GridSimple.py from the demo if you change the
handler for OnCellLeftClick to:

    def OnCellLeftClick(self, evt):
        self.log.write("OnCellLeftClick: (%d,%d) %s\n" %
                       (evt.GetRow(), evt.GetCol(), evt.GetPosition()))
        self.SelectRow(evt.GetRow(), addToSelected = False)
        evt.Veto()

Clicking on a cell selects the whole row, but if you click and drag
/within a cell/ you get a selection block appear from the first cell to
the cell you are dragging in.

I've looked at the grid source code from wxWidgets (grid.cpp) and it
appears that it's in the code at wxGrid::ProcessGridCellMouseEvent -
first, highlighting while dragging and then selecting when drag is
finished. There doesn't appear to be any hooks to disable drag
selection there.

Do I have to process all the mouse events myself, intercepting them before
they reach the grid?

Hugh

Hugh Gibson wrote:

Clicking on a cell selects the whole row, but if you click and drag /within a cell/ you get a selection block appear from the first cell to the cell you are dragging in.

I've looked at the grid source code from wxWidgets (grid.cpp) and it appears that it's in the code at wxGrid::ProcessGridCellMouseEvent - first, highlighting while dragging and then selecting when drag is finished. There doesn't appear to be any hooks to disable drag selection there.

Do I have to process all the mouse events myself, intercepting them before they reach the grid?

You might be able to do it by catching the various selection events and unselecting the prior rows as needed. Otherwise please ask about this on wx-users or wx-dev.

···

--
Robin Dunn
Software Craftsman
http://wxPython.org Java give you jitters? Relax with wxPython!

However, if the mouse is dragged in a cell then a range selection is
done from the first cell.

I was able to disable this by intercepting mouse events on the grid window
itself. The key to this was realising that the grid control is made up of
a number of windows - the column labels, the row labels, the top left
corner and the main area of the grid.

So there's no point in trying to pick up mouse moves on the main grid
class - you have to do this:

   wx.EVT_MOTION(self.GetGridWindow(), self.OnMouseMotion)
      
   def OnMouseMotion(self, oEvent):

···

#
      # Don't allow any dragging on the window
      #
      if oEvent.Dragging(): # mouse being dragged?
         pass # eat the event
      else:
         oEvent.Skip() # no dragging, pass on to the window
      
Along with SetCellHighlightPenWidth this makes single row selection quite
clean.

Hugh

Hi

Is it possible to disable multi-row selects in a grid?

I had the same problem and I've solved it in the following way:

self.Grid.Bind(wx.grid.EVT_GRID_RANGE_SELECT, self.OnClickMove)

def OnClickMove(self, event):
    rowT = event.GetTopRow()
    rowB = event.GetBottomRow()
    if rowT != rowB:
       # Unfortunately there doesn't seem to be a "DeselectAll()"
       for i in range(self.Grid.GetNumberRows()):
           self.Grid.DeselectRow(i)

Now its still possible to select more than one row, but if the user is
ready with the selection and more than one row is selected, everything
gets deselected.
And this also works, if the user select cells by cursor keys.

Clicking on a cell selects the whole row, but if you click and drag
/within a cell/ you get a selection block appear from the first cell to
the cell you are dragging in.

No you get a selection from the cell, where the grid cursor is, to the
cell you are dragging in. And yes you are right, this is really
confusing.

But now I've realized, that I have still another problem - how to
hide/disable the grid cursor?

I had done it this way:
try: self.Grid.SetGridCursor(0, self.Grid.GetNumberCols())
except wx.PyAssertionError: pass

But this only works, if the user doesn't use the cursor keys. Of course
I can Bind the EVT_KEY_DOWN, but isn't there a cleaner way to hide the
grid cursor?

cu boesi

···

Am 27.10.2004 16:51:00 schrieb Hugh Gibson:
--
baka baka #1671 : icq-intern
                                                 #73628288 : icq-extern
                                                  boesi111 : aim
                                                      i171 : reallife

But now I've realized, that I have still another problem - how to
hide/disable the grid cursor?

Robin posted a solution on 30th October:

I have a panel with a grid and several other controls. When the user is
doing something in another control (i.e. the grid does not have the
focus), it would make it more obvious where input is going if there was
no cursor rectangle showing in the grid.

You can use the grid's SetCellHighlightPenWidth method to change the
width of the line drawn for the cell cursor.

Hugh