wx.Grid problem, extra lines in a cell after calling GridCellAutoWrapStringRenderer()

wxpython-users,

I am having
a problem using wx.Grid.

Depending
upon the length of the cell text, I see one or two extra lines in the cell.

GridCellAutoWrapStringRenderer() appears to be causing this problem.

Can someone explain why and is there a
solution?

Thanks,

Bruce

Below is sample code to show the problem:

···

import wx
import wx.grid as gridlib

using wxPython version 2.8.10.1

class GridTest(wx.Frame):

def __init__(self):
    wx.Frame.__init__(self, parent=None, title="Grid Test - Merge Cells", size=(600,300))
    panel = wx.Panel(self)

    someGrid = gridlib.Grid(panel)
    someGrid.CreateGrid(6,8)


    # Cell 1,2 - OK
    someGrid.SetCellFont(1, 2, wx.Font(8, wx.SWISS, wx.NORMAL, wx.NORMAL, False, 'Tahoma'))
    someGrid.SetCellAlignment(1, 2, wx.ALIGN_LEFT, wx.ALIGN_TOP)
    someGrid.SetCellValue(1, 2,"No Extra Lines")  

    # Cell 2,2 - Not OK when GridCellAutoWrapStringRenderer() is used, extra line in cell
    someGrid.SetCellFont(2, 2, wx.Font(8, wx.SWISS, wx.NORMAL, wx.NORMAL, False, 'Tahoma'))
    someGrid.SetCellAlignment(2, 2, wx.ALIGN_LEFT, wx.ALIGN_TOP)

    someGrid.SetCellValue(2, 2,"oops, there is an extra line")   
    someGrid.SetCellEditor(2, 2, gridlib.GridCellAutoWrapStringEditor())
    someGrid.SetCellRenderer(2, 2, gridlib.GridCellAutoWrapStringRenderer())

   
    # Cell 3,2&3 - Not OK when GridCellAutoWrapStringRenderer() is used, extra line in cell
    someGrid.SetCellFont(3, 2, wx.Font(8, wx.SWISS, wx.NORMAL, wx.NORMAL, False, 'Tahoma'))

    someGrid.SetCellAlignment(3, 2, wx.ALIGN_LEFT, wx.ALIGN_TOP)
    someGrid.SetCellSize(3, 2, 1, 2)
    someGrid.SetCellValue (3, 2, "Why is there an extra line in this cell?")
    someGrid.SetCellEditor(3, 2, gridlib.GridCellAutoWrapStringEditor())

    someGrid.SetCellRenderer(3, 2, gridlib.GridCellAutoWrapStringRenderer())

    # Cell 4,2&3 - Not OK when GridCellAutoWrapStringRenderer() is used, extra two lines in cell
    someGrid.SetCellFont(4, 2, wx.Font(8, wx.SWISS, wx.NORMAL, wx.NORMAL, False, 'Tahoma'))

    someGrid.SetCellAlignment(4, 2, wx.ALIGN_LEFT, wx.ALIGN_TOP)
    someGrid.SetCellSize(4, 2, 1, 2)
    someGrid.SetCellValue (4, 2, "Now this, why is there an extra two lines in this cell?")

    someGrid.SetCellEditor(4, 2, gridlib.GridCellAutoWrapStringEditor())
    someGrid.SetCellRenderer(4, 2, gridlib.GridCellAutoWrapStringRenderer())

    someGrid.AutoSizeRows(setAsMin=False)
    someGrid.ForceRefresh()

    sizer = wx.BoxSizer(wx.VERTICAL)
    sizer.Add(someGrid)
    panel.SetSizer(sizer)

if name == “main”:
app = wx.PySimpleApp()
frame = GridTest()
frame.Show()

app.MainLoop()

wxpython-users,

I am having a problem using wx.Grid.

Depending upon the length of the cell text, I see one or two extra lines
in the cell.

GridCellAutoWrapStringRenderer()appears to be causing this problem.

Can someone explain why

It looks like GridCellAutoWrapStringRenderer.GetBestSize is not taking spanned columns into account, so it measuring the text as if it was wrapping it in just a single column.

http://trac.wxwidgets.org/browser/wxWidgets/trunk/src/generic/gridctrl.cpp#L348

It may be that the font is not being set in the dc when calculating the best size either, but I'd have to take a closer look to be sure...

and is there a solution?

It's not ideal but you could always implement your own renderer. Either way please check if there is already a ticket about this in Trac and add one if not.

···

On 3/4/12 12:19 PM, bruce g wrote:

--
Robin Dunn
Software Craftsman

wxpython-users,

I am having
a problem using wx.Grid.

Depending
upon the length of the cell text, I see one or two extra lines in the cell.

GridCellAutoWrapStringRenderer() appears to be causing this problem.

Can someone explain why and is there a
solution?

Thanks,

Bruce

Below is sample code to show the problem:

import wx
import wx.grid as gridlib

using wxPython version 2.8.10.1

class GridTest(wx.Frame):

def __init__(self):
    wx.Frame.__init__(self, parent=None, title="Grid Test - Merge Cells", size=(600,300))
    panel = wx.Panel(self)

    someGrid = gridlib.Grid(panel)
    someGrid.CreateGrid(6,8)


    # Cell 1,2 - OK
    someGrid.SetCellFont(1, 2, wx.Font(8, wx.SWISS, wx.NORMAL, wx.NORMAL, False, 'Tahoma'))
    someGrid.SetCellAlignment(1, 2, wx.ALIGN_LEFT, wx.ALIGN_TOP)
    someGrid.SetCellValue(1, 2,"No Extra Lines")  


    # Cell 2,2 - Not OK when GridCellAutoWrapStringRenderer() is used, extra line in cell
    someGrid.SetCellFont(2, 2, wx.Font(8, wx.SWISS, wx.NORMAL, wx.NORMAL, False, 'Tahoma'))
    someGrid.SetCellAlignment(2, 2, wx.ALIGN_LEFT, wx.ALIGN_TOP)

    someGrid.SetCellValue(2, 2,"oops, there is an extra line")   
    someGrid.SetCellEditor(2, 2, gridlib.GridCellAutoWrapStringEditor())
    someGrid.SetCellRenderer(2, 2, gridlib.GridCellAutoWrapStringRenderer())

   
    # Cell 3,2&3 - Not OK when GridCellAutoWrapStringRenderer() is used, extra line in cell
    someGrid.SetCellFont(3, 2, wx.Font(8, wx.SWISS, wx.NORMAL, wx.NORMAL, False, 'Tahoma'))

    someGrid.SetCellAlignment(3, 2, wx.ALIGN_LEFT, wx.ALIGN_TOP)
    someGrid.SetCellSize(3, 2, 1, 2)
    someGrid.SetCellValue (3, 2, "Why is there an extra line in this cell?")
    someGrid.SetCellEditor(3, 2, gridlib.GridCellAutoWrapStringEditor())

    someGrid.SetCellRenderer(3, 2, gridlib.GridCellAutoWrapStringRenderer())

    # Cell 4,2&3 - Not OK when GridCellAutoWrapStringRenderer() is used, extra two lines in cell
    someGrid.SetCellFont(4, 2, wx.Font(8, wx.SWISS, wx.NORMAL, wx.NORMAL, False, 'Tahoma'))

    someGrid.SetCellAlignment(4, 2, wx.ALIGN_LEFT, wx.ALIGN_TOP)
    someGrid.SetCellSize(4, 2, 1, 2)
    someGrid.SetCellValue (4, 2, "Now this, why is there an extra two lines in this cell?")

    someGrid.SetCellEditor(4, 2, gridlib.GridCellAutoWrapStringEditor())
    someGrid.SetCellRenderer(4, 2, gridlib.GridCellAutoWrapStringRenderer())

    someGrid.AutoSizeRows(setAsMin=False)
    someGrid.ForceRefresh()


    sizer = wx.BoxSizer(wx.VERTICAL)
    sizer.Add(someGrid)
    panel.SetSizer(sizer)

if name == “main”:
app = wx.PySimpleApp()
frame = GridTest()
frame.Show()

app.MainLoop()

wxpython-users,

I am having
a problem using wx.Grid.

Depending
upon the length of the cell text, I see one or two extra lines in the cell.

GridCellAutoWrapStringRenderer() appears to be causing this problem.

Can someone explain why and is there a
solution?

Thanks,

Bruce

Below is sample code to show the problem:

import wx
import wx.grid as gridlib

using wxPython version 2.8.10.1

class GridTest(wx.Frame):

def __init__(self):
    wx.Frame.__init__(self, parent=None, title="Grid Test - Merge Cells", size=(600,300))
    panel = wx.Panel(self)

    someGrid = gridlib.Grid(panel)
    someGrid.CreateGrid(6,8)


    # Cell 1,2 - OK
    someGrid.SetCellFont(1, 2, wx.Font(8, wx.SWISS, wx.NORMAL, wx.NORMAL, False, 'Tahoma'))
    someGrid.SetCellAlignment(1, 2, wx.ALIGN_LEFT, wx.ALIGN_TOP)
    someGrid.SetCellValue(1, 2,"No Extra Lines")  


    # Cell 2,2 - Not OK when GridCellAutoWrapStringRenderer() is used, extra line in cell
    someGrid.SetCellFont(2, 2, wx.Font(8, wx.SWISS, wx.NORMAL, wx.NORMAL, False, 'Tahoma'))
    someGrid.SetCellAlignment(2, 2, wx.ALIGN_LEFT, wx.ALIGN_TOP)

    someGrid.SetCellValue(2, 2,"oops, there is an extra line")   
    someGrid.SetCellEditor(2, 2, gridlib.GridCellAutoWrapStringEditor())
    someGrid.SetCellRenderer(2, 2, gridlib.GridCellAutoWrapStringRenderer())

   
    # Cell 3,2&3 - Not OK when GridCellAutoWrapStringRenderer() is used, extra line in cell
    someGrid.SetCellFont(3, 2, wx.Font(8, wx.SWISS, wx.NORMAL, wx.NORMAL, False, 'Tahoma'))

    someGrid.SetCellAlignment(3, 2, wx.ALIGN_LEFT, wx.ALIGN_TOP)
    someGrid.SetCellSize(3, 2, 1, 2)
    someGrid.SetCellValue (3, 2, "Why is there an extra line in this cell?")
    someGrid.SetCellEditor(3, 2, gridlib.GridCellAutoWrapStringEditor())

    someGrid.SetCellRenderer(3, 2, gridlib.GridCellAutoWrapStringRenderer())

    # Cell 4,2&3 - Not OK when GridCellAutoWrapStringRenderer() is used, extra two lines in cell
    someGrid.SetCellFont(4, 2, wx.Font(8, wx.SWISS, wx.NORMAL, wx.NORMAL, False, 'Tahoma'))

    someGrid.SetCellAlignment(4, 2, wx.ALIGN_LEFT, wx.ALIGN_TOP)
    someGrid.SetCellSize(4, 2, 1, 2)
    someGrid.SetCellValue (4, 2, "Now this, why is there an extra two lines in this cell?")

    someGrid.SetCellEditor(4, 2, gridlib.GridCellAutoWrapStringEditor())
    someGrid.SetCellRenderer(4, 2, gridlib.GridCellAutoWrapStringRenderer())

    someGrid.AutoSizeRows(setAsMin=False)
    someGrid.ForceRefresh()


    sizer = wx.BoxSizer(wx.VERTICAL)
    sizer.Add(someGrid)
    panel.SetSizer(sizer)

if name == “main”:
app = wx.PySimpleApp()
frame = GridTest()
frame.Show()

app.MainLoop()

wxpython-users,

I am having
a problem using wx.Grid.

Depending
upon the length of the cell text, I see one or two extra lines in the cell.

GridCellAutoWrapStringRenderer() appears to be causing this problem.

Can someone explain why and is there a
solution?

Thanks,

Bruce

Below is sample code to show the problem:

import wx
import wx.grid as gridlib

using wxPython version 2.8.10.1

class GridTest(wx.Frame):

def __init__(self):
    wx.Frame.__init__(self, parent=None, title="Grid Test - Merge Cells", size=(600,300))
    panel = wx.Panel(self)

    someGrid = gridlib.Grid(panel)
    someGrid.CreateGrid(6,8)


    # Cell 1,2 - OK
    someGrid.SetCellFont(1, 2, wx.Font(8, wx.SWISS, wx.NORMAL, wx.NORMAL, False, 'Tahoma'))
    someGrid.SetCellAlignment(1, 2, wx.ALIGN_LEFT, wx.ALIGN_TOP)
    someGrid.SetCellValue(1, 2,"No Extra Lines")  


    # Cell 2,2 - Not OK when GridCellAutoWrapStringRenderer() is used, extra line in cell
    someGrid.SetCellFont(2, 2, wx.Font(8, wx.SWISS, wx.NORMAL, wx.NORMAL, False, 'Tahoma'))
    someGrid.SetCellAlignment(2, 2, wx.ALIGN_LEFT, wx.ALIGN_TOP)

    someGrid.SetCellValue(2, 2,"oops, there is an extra line")   
    someGrid.SetCellEditor(2, 2, gridlib.GridCellAutoWrapStringEditor())
    someGrid.SetCellRenderer(2, 2, gridlib.GridCellAutoWrapStringRenderer())

   
    # Cell 3,2&3 - Not OK when GridCellAutoWrapStringRenderer() is used, extra line in cell
    someGrid.SetCellFont(3, 2, wx.Font(8, wx.SWISS, wx.NORMAL, wx.NORMAL, False, 'Tahoma'))

    someGrid.SetCellAlignment(3, 2, wx.ALIGN_LEFT, wx.ALIGN_TOP)
    someGrid.SetCellSize(3, 2, 1, 2)
    someGrid.SetCellValue (3, 2, "Why is there an extra line in this cell?")
    someGrid.SetCellEditor(3, 2, gridlib.GridCellAutoWrapStringEditor())

    someGrid.SetCellRenderer(3, 2, gridlib.GridCellAutoWrapStringRenderer())

    # Cell 4,2&3 - Not OK when GridCellAutoWrapStringRenderer() is used, extra two lines in cell
    someGrid.SetCellFont(4, 2, wx.Font(8, wx.SWISS, wx.NORMAL, wx.NORMAL, False, 'Tahoma'))

    someGrid.SetCellAlignment(4, 2, wx.ALIGN_LEFT, wx.ALIGN_TOP)
    someGrid.SetCellSize(4, 2, 1, 2)
    someGrid.SetCellValue (4, 2, "Now this, why is there an extra two lines in this cell?")

    someGrid.SetCellEditor(4, 2, gridlib.GridCellAutoWrapStringEditor())
    someGrid.SetCellRenderer(4, 2, gridlib.GridCellAutoWrapStringRenderer())

    someGrid.AutoSizeRows(setAsMin=False)
    someGrid.ForceRefresh()


    sizer = wx.BoxSizer(wx.VERTICAL)
    sizer.Add(someGrid)
    panel.SetSizer(sizer)

if name == “main”:
app = wx.PySimpleApp()
frame = GridTest()
frame.Show()

app.MainLoop()

wxpython-users,

I am having
a problem using wx.Grid.

Depending
upon the length of the cell text, I see one or two extra lines in the cell.

GridCellAutoWrapStringRenderer() appears to be causing this problem.

Can someone explain why and is there a
solution?

Thanks,

Bruce

Below is sample code to show the problem:

import wx
import wx.grid as gridlib

using wxPython version 2.8.10.1

class GridTest(wx.Frame):

def __init__(self):
    wx.Frame.__init__(self, parent=None, title="Grid Test - Merge Cells", size=(600,300))
    panel = wx.Panel(self)

    someGrid = gridlib.Grid(panel)
    someGrid.CreateGrid(6,8)


    # Cell 1,2 - OK
    someGrid.SetCellFont(1, 2, wx.Font(8, wx.SWISS, wx.NORMAL, wx.NORMAL, False, 'Tahoma'))
    someGrid.SetCellAlignment(1, 2, wx.ALIGN_LEFT, wx.ALIGN_TOP)
    someGrid.SetCellValue(1, 2,"No Extra Lines")  


    # Cell 2,2 - Not OK when GridCellAutoWrapStringRenderer() is used, extra line in cell
    someGrid.SetCellFont(2, 2, wx.Font(8, wx.SWISS, wx.NORMAL, wx.NORMAL, False, 'Tahoma'))
    someGrid.SetCellAlignment(2, 2, wx.ALIGN_LEFT, wx.ALIGN_TOP)

    someGrid.SetCellValue(2, 2,"oops, there is an extra line")   
    someGrid.SetCellEditor(2, 2, gridlib.GridCellAutoWrapStringEditor())
    someGrid.SetCellRenderer(2, 2, gridlib.GridCellAutoWrapStringRenderer())

   
    # Cell 3,2&3 - Not OK when GridCellAutoWrapStringRenderer() is used, extra line in cell
    someGrid.SetCellFont(3, 2, wx.Font(8, wx.SWISS, wx.NORMAL, wx.NORMAL, False, 'Tahoma'))

    someGrid.SetCellAlignment(3, 2, wx.ALIGN_LEFT, wx.ALIGN_TOP)
    someGrid.SetCellSize(3, 2, 1, 2)
    someGrid.SetCellValue (3, 2, "Why is there an extra line in this cell?")
    someGrid.SetCellEditor(3, 2, gridlib.GridCellAutoWrapStringEditor())

    someGrid.SetCellRenderer(3, 2, gridlib.GridCellAutoWrapStringRenderer())

    # Cell 4,2&3 - Not OK when GridCellAutoWrapStringRenderer() is used, extra two lines in cell
    someGrid.SetCellFont(4, 2, wx.Font(8, wx.SWISS, wx.NORMAL, wx.NORMAL, False, 'Tahoma'))

    someGrid.SetCellAlignment(4, 2, wx.ALIGN_LEFT, wx.ALIGN_TOP)
    someGrid.SetCellSize(4, 2, 1, 2)
    someGrid.SetCellValue (4, 2, "Now this, why is there an extra two lines in this cell?")

    someGrid.SetCellEditor(4, 2, gridlib.GridCellAutoWrapStringEditor())
    someGrid.SetCellRenderer(4, 2, gridlib.GridCellAutoWrapStringRenderer())

    someGrid.AutoSizeRows(setAsMin=False)
    someGrid.ForceRefresh()


    sizer = wx.BoxSizer(wx.VERTICAL)
    sizer.Add(someGrid)
    panel.SetSizer(sizer)

if name == “main”:
app = wx.PySimpleApp()
frame = GridTest()
frame.Show()

app.MainLoop()
···

On Sunday, March 4, 2012 3:19:15 PM UTC-5, bruce g wrote:
On Sunday, March 4, 2012 3:19:15 PM UTC-5, bruce g wrote:
On Sunday, March 4, 2012 3:19:15 PM UTC-5, bruce g wrote:
On Sunday, March 4, 2012 3:19:15 PM UTC-5, bruce g wrote:

Robin,

Thank you for answering my email.
The problem I am having also occurs in non-spanned column (see cell 2,2).
I have tried other fonts such as Courier New with no luck.
I will add a ticket (once I figure out how to)

Bruce

···

On Mon, Mar 5, 2012 at 6:45 PM, Robin Dunn robin@alldunn.com wrote:

On 3/4/12 12:19 PM, bruce g wrote:

wxpython-users,

I am having a problem using wx.Grid.

Depending upon the length of the cell text, I see one or two extra lines

in the cell.

GridCellAutoWrapStringRenderer()appears to be causing this problem.

Can someone explain why

It looks like GridCellAutoWrapStringRenderer.GetBestSize is not taking spanned columns into account, so it measuring the text as if it was wrapping it in just a single column.

http://trac.wxwidgets.org/browser/wxWidgets/trunk/src/generic/gridctrl.cpp#L348

It may be that the font is not being set in the dc when calculating the best size either, but I’d have to take a closer look to be sure…

and is there a solution?

It’s not ideal but you could always implement your own renderer. Either way please check if there is already a ticket about this in Trac and add one if not.

Robin Dunn

Software Craftsman

http://wxPython.org

To unsubscribe, send email to wxPython-users+unsubscribe@googlegroups.com

or visit http://groups.google.com/group/wxPython-users?hl=en