Problem finding Grid coordinates from context menu

Hi,

I have a grid and a contextual menu that I can display on
the grid.

After the contextual menu fires I want to get the contents
of the grid under the menu ( actually I want to get the
contents by querying the table data source).

Here is a fragment of the application....

class MyGrid(wx.grid.Grid):
     def __init__(self,parent):
         wx.grid.Grid.__init__(self,parent)
         self.lastPopup = None
         self.popupmenu = wx.Menu()
         for text in ["Show Field", "dick","harry"] :
             item = self.popupmenu.Append(-1,text)
             self.Bind(wx.EVT_MENU,self.OnPopupItemSelected,item)
         #self.GetGridWindow().Bind(wx.EVT_CONTEXT_MENU,self.OnShowPopup)
         self.Bind(wx.grid.EVT_GRID_CELL_RIGHT_CLICK,self.OnShowPopup)

     def OnShowPopup(self,event):
         pos=event.GetPosition()
         self.lastPopup = pos
         self.PopupMenu(self.popupmenu,pos)

     def OnPopupItemSelected(self,event) :
         item=self.popupmenu.FindItemById(event.GetId())
         text = item.GetText()
         row = self.YToRow(self.lastPopup[1])
         col = self.XToCol(self.lastPopup[0])
         content = self.GetTable().GetValue(row-1,col-1)
         wx.MessageBox("row= %d, col= %d \n Content = %s" % (row,col, content))

The problem is that sometimes I get the right value and sometimes
I don't. I seem to have the best luck clicking in the upper left
corner of a grid box. Most of the time clicking in the rightmost column
shows a column of -1. ( Most of the time the resulting grid coordinates
are off by one in one direction or the other )

If I scroll the grid I will always get grid coordinates relative to the
initial screen ie if the grid is show rows 101-110 I will get rows in
the messagebox with values 1-10...

Accuracy (even in linux) gets worse it I resize the columns...

Any suggestions?

Jerry

Jerry LeVan wrote:

Hi,

I have a grid and a contextual menu that I can display on
the grid.

        self.Bind(wx.grid.EVT_GRID_CELL_RIGHT_CLICK,self.OnShowPopup)

    def OnShowPopup(self,event):
        pos=event.GetPosition()
        self.lastPopup = pos
        self.PopupMenu(self.popupmenu,pos)

    def OnPopupItemSelected(self,event) :
        item=self.popupmenu.FindItemById(event.GetId())
        text = item.GetText()
        row = self.YToRow(self.lastPopup[1])
        col = self.XToCol(self.lastPopup[0])
        content = self.GetTable().GetValue(row-1,col-1)
        wx.MessageBox("row= %d, col= %d \n Content = %s" % (row,col, content))

The EVT_GRID_CELL_RIGHT_CLICK sends a grid event so you can use event.GetRow() and event.GetCol() to get the row and col, instead of trying to calculate them later.

The problem is that sometimes I get the right value and sometimes
I don't. I seem to have the best luck clicking in the upper left
corner of a grid box. Most of the time clicking in the rightmost column
shows a column of -1. ( Most of the time the resulting grid coordinates
are off by one in one direction or the other )

The position you got from the event is probably adjusted by size of the label windows, and then in XToCol and YToRow they are adjusted again, resulting in wrong row/col values.

···

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

Yes, I feel like molasses sometimes, I even looked at gridevent doc and it did not
even register...The following works fine.

     def OnShowPopup(self,event):
         pos=event.GetPosition()
         self.lastPopup = (event.GetRow(),event.GetCol())
         self.PopupMenu(self.popupmenu,pos)

     def OnPopupItemSelected(self,event) :
         item=self.popupmenu.FindItemById(event.GetId())
         text = item.GetText()
         row = self.lastPopup[0]
         col = self.lastPopup[1]
         content = self.GetTable().GetValue(row,col)
         wx.MessageBox("row= %d, col= %d \n Content = %s" % (row,col, content))

Is there a way to do the task *without* having to store the grid-position of the
context menu in an instance variable ( self.lastPopup)? It seems a bit
kludgy to me.

Jerry

···

On May 24, 2007, at 5:32 PM, Robin Dunn wrote:

Jerry LeVan wrote:

Hi,
I have a grid and a contextual menu that I can display on
the grid.

        self.Bind(wx.grid.EVT_GRID_CELL_RIGHT_CLICK,self.OnShowPopup)
    def OnShowPopup(self,event):
        pos=event.GetPosition()
        self.lastPopup = pos
        self.PopupMenu(self.popupmenu,pos)
    def OnPopupItemSelected(self,event) :
        item=self.popupmenu.FindItemById(event.GetId())
        text = item.GetText()
        row = self.YToRow(self.lastPopup[1])
        col = self.XToCol(self.lastPopup[0])
        content = self.GetTable().GetValue(row-1,col-1)
        wx.MessageBox("row= %d, col= %d \n Content = %s" % (row,col, content))

The EVT_GRID_CELL_RIGHT_CLICK sends a grid event so you can use event.GetRow() and event.GetCol() to get the row and col, instead of trying to calculate them later.

--
Robin Dunn