custom string renderer in grid

Hello,

I derived a custom CellRenderer from GridCellStringRenderer::

   class PointerRenderer(wx.grid.GridCellStringRenderer):
  def __init__(self):
    print "__init__"
    wx.grid.GridCellStringRenderer.__init__(self)

  def Draw(self, grid, attr, dc, rect, row, col, isSelected):
    print "Draw"
    row = grid.GetCellValue(row, col)
    text = row.getLabel()
    x = rect.x + 1
    y = rect.y + 1
    dc.DrawText(text, x, y)

  def GetBestSize(self, grid, attr, dc, row, col):
    row = grid.GetCellValue(row, col)
    text = row.getLabel()
    dc.SetFont(attr.GetFont())
    w, h = dc.GetTextExtent(text)
    return wx.Size(w, h)

  def Clone(self):
    return PointerRenderer()

(Values in these cells are not strings but "row instances" whose getLabel() method returns a text to be rendered like a normal string.)

I tried two different methods to tell the Grid to use this Renderer for the columns who need this.

1. First method : using Grid.SetColAttr() with a GridCellAttr instance whose SetRenderer() has been called::

  # during MyGrid.__init__():
  i = 0
  for col in self.table.columns:
   cellattr = wx.grid.GridCellAttr()
   if isinstance(col,Pointer):
     cellattr.SetEditor(wx.grid.GridCellTextEditor())
     cellattr.SetRenderer(PointerRenderer())
   else:
     cellattr.SetEditor(wx.grid.GridCellTextEditor())
     cellattr.SetRenderer(wx.grid.GridCellStringRenderer())
   self.SetColAttr(i,cellattr)
   i += 1

2. Second method :

   # during MyGrid.__init__():
   self.RegisterDataType("Pointer", PointerRenderer(),\
                         wx.grid.GridCellTextEditor())

   # MyTableModel :
   def GetTypeName(self,row,col):
     colType = self.columns[col].type
     if isinstance(colType,Pointer):
  print "Pointer"
  return "Pointer"
     return "string"

But both methods seem to fail: the grid never calls my renderer's Draw method. The cells of those columns are rendered as if they were strings.

I consulted http://wiki.wxpython.org/index.cgi/wxGrid_20Manual
and the wxWindows API doc, but without success.

Any hints?

Luc Saffre

Luc Saffre wrote:

Hello,

I derived a custom CellRenderer from GridCellStringRenderer::

You need to derive from PyGridCellRenderer as is done in the demo. It is the only renderer that is engineered to look for a Python implementation of the overridable methods.

···

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

Thanks! This helped!
Luc

···

On 4/11/2003 2:26, Robin Dunn wrote:

You need to derive from PyGridCellRenderer as is done in the demo. It is the only renderer that is engineered to look for a Python implementation of the overridable methods.