]> git.saurik.com Git - wxWidgets.git/blob - wxPython/samples/wxPIA_book/Chapter-14/grid_renderer.py
don't use strlen() to verify the length of the string as it can contain embedded...
[wxWidgets.git] / wxPython / samples / wxPIA_book / Chapter-14 / grid_renderer.py
1 import wx
2 import wx.grid
3 import random
4
5 class RandomBackgroundRenderer(wx.grid.PyGridCellRenderer):
6 def __init__(self):
7 wx.grid.PyGridCellRenderer.__init__(self)
8
9
10 def Draw(self, grid, attr, dc, rect, row, col, isSelected):
11 text = grid.GetCellValue(row, col)
12 hAlign, vAlign = attr.GetAlignment()
13 dc.SetFont( attr.GetFont() )
14 if isSelected:
15 bg = grid.GetSelectionBackground()
16 fg = grid.GetSelectionForeground()
17 else:
18 bg = random.choice(["pink", "sky blue", "cyan", "yellow", "plum"])
19 fg = attr.GetTextColour()
20
21 dc.SetTextBackground(bg)
22 dc.SetTextForeground(fg)
23 dc.SetBrush(wx.Brush(bg, wx.SOLID))
24 dc.SetPen(wx.TRANSPARENT_PEN)
25 dc.DrawRectangleRect(rect)
26 grid.DrawTextRectangle(dc, text, rect, hAlign, vAlign)
27
28
29 def GetBestSize(self, grid, attr, dc, row, col):
30 text = grid.GetCellValue(row, col)
31 dc.SetFont(attr.GetFont())
32 w, h = dc.GetTextExtent(text)
33 return wx.Size(w, h)
34
35 def Clone(self):
36 return RandomBackgroundRenderer()
37
38 class TestFrame(wx.Frame):
39 def __init__(self):
40 wx.Frame.__init__(self, None, title="Grid Renderer",
41 size=(640,480))
42
43 grid = wx.grid.Grid(self)
44 grid.CreateGrid(50,50)
45
46 # Set this custom renderer just for row 4
47 attr = wx.grid.GridCellAttr()
48 attr.SetRenderer(RandomBackgroundRenderer())
49 grid.SetRowAttr(4, attr)
50
51 for row in range(10):
52 for col in range(10):
53 grid.SetCellValue(row, col,
54 "cell (%d,%d)" % (row, col))
55
56 app = wx.PySimpleApp()
57 frame = TestFrame()
58 frame.Show()
59 app.MainLoop()