Changing the color of selected rows in a PyGridTableBase grid

First of all I want to express what a great wx.Python extension wx.grid is. I am developing a little browser for large log files and when I first started to get this done thru native list controls I realized how slow those are when it comes to list large amount of data. Thanks to wx.grid which is basically using a very sophisticated form of a virtual list control my little browser now advanced to a real useful tool.

Documentation of everything around wx.Python is available but not in a form I would call comprehensive. Like with many other libraries software developers are using these days google.com is your friend and you need to google for solutions, search thru code repositories ( like Nullege for Python ) or user groups ( like the wxpython-users group on Google ).  Stackoverflow of course is another great source of answers.

One simple thing I could not get achieved for a couple of hours: changing the color used for selected rows. Like wx.Python list controls wx.Grid uses a dark blue for this which looks odd on my Windows 7 machine.

After gazing again at the MegaTable sample code I finally found the solution: your own row selection ( or call it highlighting ) color can be implemented by changing the Draw method of a font renderer you can use as a plugin to a grid panel.

Here is my version of the Draw method of my MegaFontRenderer using a light grey as a row selection color rather than the odd wx.BLUE ( my changes in yellow ):

    def Draw(self, grid, attr, dc, rect, row, col, isSelected):
        # Here we draw text in a grid cell using various fonts
        # and colors.  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)
        
        HIGHLIGHT_COLOR = (240,240,240)
        
        if isSelected:
            dc.SetBrush(wx.Brush(HIGHLIGHT_COLOR, wx.SOLID))
            dc.SetPen(wx.Pen(HIGHLIGHT_COLOR, 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(HIGHLIGHT_COLOR)
        else:
            dc.SetBrush(self.normalBrush)
            if self.background_color_index:
                idx = self.table.GetValue(row, int(self.background_color_index)-1)
                dc.SetTextBackground(COLORS[int(idx)-1])
            else:            
                dc.SetTextBackground("white")

        dc.SetTextForeground(self.color)
        dc.SetFont(self.font)
        dc.DrawText(text, rect.x+1, rect.y+1)

        # Okay, now for the advanced class 🙂
        # Let's add three dots "..."
        # to indicate that that there is more text to be read
        # when the text is larger than the grid cell

        width, height = dc.GetTextExtent(text)
        
        if width > grid.GetColSize(col) and not self.colSize:
            # width, height = dc.GetTextExtent("...")
            # x = rect.x+1 + rect.width-2 - width
            # dc.DrawRectangle(x, rect.y+1, width+1, height)
            # dc.DrawText("...", x, rect.y+1)
            grid.SetColSize(col, width)

        dc.DestroyClippingRegion()
Advertisements

Leave a Reply

Fill in your details below or click an icon to log in:

WordPress.com Logo

You are commenting using your WordPress.com account. Log Out / Change )

Twitter picture

You are commenting using your Twitter account. Log Out / Change )

Facebook photo

You are commenting using your Facebook account. Log Out / Change )

Google+ photo

You are commenting using your Google+ account. Log Out / Change )

Connecting to %s

%d bloggers like this: