escape sequences and wx.Grid

How is it possible to render text in a wx.Grid’s cell in a true multiline looking?
While text containing escape sequences (\n and \r) is correctly reproduced in a wx.TextCtrl, the same sequences are ignored in the grid. Also wx.grid.GridCellAutoWrapStringRenderer feeds a new line only if the text extent is larger than the column width.

Hi Raffaello,

How is it possible to render text in a wx.Grid's cell in a true multiline
looking?
While text containing escape sequences (\n and \r) is correctly reproduced
in a wx.TextCtrl, the same sequences are ignored in the grid. Also
wx.grid.GridCellAutoWrapStringRenderer feeds a new line only if the text
extent is larger than the column width.

I would go with custom grid renderers, for which the wxPython demo
Grid_MegaExample is a good starting point. Basically you just need to
do something like this (untested):

class MegaTextRenderer(Grid.PyGridCellRenderer):
    def __init__(self, table):
        """Render data."""

        Grid.PyGridCellRenderer.__init__(self)
        self.table = table

    def Draw(self, grid, attr, dc, rect, row, col, isSelected):
        # Here we draw text in a grid cell.
        # We have to set the clipping region on
        # the grid's DC, otherwise the text will spill over
        # to the next cell
        dc.SetClippingRect(rect)

        # clear the background
        dc.SetBackgroundMode(wx.SOLID)

        if isSelected:
            dc.SetBrush(wx.Brush(wx.BLUE, wx.SOLID))
            dc.SetPen(wx.Pen(wx.BLUE, 1, wx.SOLID))
        else:
            dc.SetBrush(wx.Brush(wx.WHITE, wx.SOLID))
            dc.SetPen(wx.Pen(wx.WHITE, 1, wx.SOLID))
        dc.DrawRectangleRect(rect)

        text = self.table.GetValue(row, col)
        dc.SetBackgroundMode(wx.SOLID)

        # change the text background based on whether the grid is selected
        # or not
        if isSelected:
            dc.SetBrush(self.selectedBrush)
            dc.SetTextBackground("blue")
        else:
            dc.SetBrush(self.normalBrush)
            dc.SetTextBackground("white")

        dc.SetTextForeground(wx.BLACK)
        dc.SetFont(wx.SystemSettings.GetFont(wx.SYS_DEFAULT_GUI_FONT))

        dc.DrawLabel(text, rect)
        dc.DestroyClippingRegion()

Or something along these lines.

Andrea.

"Imagination Is The Only Weapon In The War Against Reality."
http://xoomer.alice.it/infinity77/

···

On Wed, Mar 11, 2009 at 7:17 AM, raffaello wrote:

Sorry, Andrea, it clips the text but doesn’t feed lines. Thanks all the same.

···

2009/3/11 Andrea Gavana andrea.gavana@gmail.com

Hi Raffaello,

On Wed, Mar 11, 2009 at 7:17 AM, raffaello wrote:

How is it possible to render text in a wx.Grid’s cell in a true multiline

looking?

While text containing escape sequences (\n and \r) is correctly reproduced

in a wx.TextCtrl, the same sequences are ignored in the grid. Also

wx.grid.GridCellAutoWrapStringRenderer feeds a new line only if the text

extent is larger than the column width.

I would go with custom grid renderers, for which the wxPython demo

Grid_MegaExample is a good starting point. Basically you just need to

do something like this (untested):

class MegaTextRenderer(Grid.PyGridCellRenderer):

def __init__(self, table):

    """Render data."""



    Grid.PyGridCellRenderer.__init__(self)

    self.table = table



def Draw(self, grid, attr, dc, rect, row, col, isSelected):

    # Here we draw text in a grid cell.

    # We have to set the clipping region on

    # the grid's DC, otherwise the text will spill over

    # to the next cell

    dc.SetClippingRect(rect)



    # clear the background

    dc.SetBackgroundMode(wx.SOLID)



    if isSelected:

        dc.SetBrush(wx.Brush(wx.BLUE, wx.SOLID))

        dc.SetPen(wx.Pen(wx.BLUE, 1, wx.SOLID))

    else:

        dc.SetBrush(wx.Brush(wx.WHITE, wx.SOLID))

        dc.SetPen(wx.Pen(wx.WHITE, 1, wx.SOLID))

    dc.DrawRectangleRect(rect)



    text = self.table.GetValue(row, col)

    dc.SetBackgroundMode(wx.SOLID)



    # change the text background based on whether the grid is selected

    # or not

    if isSelected:

        dc.SetBrush(self.selectedBrush)

        dc.SetTextBackground("blue")

    else:

        dc.SetBrush(self.normalBrush)

        dc.SetTextBackground("white")



    dc.SetTextForeground(wx.BLACK)

    dc.SetFont(wx.SystemSettings.GetFont(wx.SYS_DEFAULT_GUI_FONT))



    dc.DrawLabel(text, rect)

    dc.DestroyClippingRegion()

Or something along these lines.

Andrea.

“Imagination Is The Only Weapon In The War Against Reality.”

http://xoomer.alice.it/infinity77/


wxpython-users mailing list

wxpython-users@lists.wxwidgets.org

http://lists.wxwidgets.org/mailman/listinfo/wxpython-users