Search code examples
gridwxpythonwxwidgets

WxPython: How to get the range of displayed cells in a wx grid?


I'm trying to get the range of visible rows/columns on a grid. The grid can be scrolled, I want the first displayed row and the last displayed row on the screen. If the function could do the same with columns it would be a plus.

Right now I came up with this solution:

def GetVisibleRowsRange(self):
    """
    return the row number of the first and last visible rows
    """
    r = 0;
    max = self.GetNumberRows()
    while r < max and not self.IsVisible(row=r, col=0, wholeCellVisible=False):
        r += 1
    firstRow = r    
    while r < max and self.IsVisible(row=r, col=0, wholeCellVisible=False):
        r += 1
    lastRow = r
    return firstRow, lastRow

This only works if the first column is visible.

I was wondering if I Can get the range of visible row/columns directly by using the windows virtual size functions?


Solution

  • I'm using wxPerl, and the following python code is untested. This method works, but you might need to "get a little creative using unbound methods".

    ux, uy = self.GetScrollPixelsPerUnit()
    sx, sy = self.GetViewStart()
    w, h = self.GetGridWindow().GetClientSize().Get()
    sx *= ux ; sy *= uy
    
    x0 = self.XToCol(sx)
    y0 = self.YToRow(sy)
    x1 = self.XToCol(sx + w, True)
    y1 = self.YToRow(sy + h, True)