1 # 11/25/2003 - Jeff Grimmett (grimmtooth@softhome.net)
4 # o There is one wx.Size() I haven't figured out how to get rid of yet.
10 import wx
.grid
as gridlib
12 #---------------------------------------------------------------------------
14 class MyCustomRenderer(gridlib
.PyGridCellRenderer
):
16 gridlib
.PyGridCellRenderer
.__init
__(self
)
18 def Draw(self
, grid
, attr
, dc
, rect
, row
, col
, isSelected
):
19 dc
.SetBackgroundMode(wx
.SOLID
)
20 dc
.SetBrush(wx
.Brush(wx
.BLACK
, wx
.SOLID
))
21 dc
.SetPen(wx
.TRANSPARENT_PEN
)
22 dc
.DrawRectangleRect(rect
)
24 dc
.SetBackgroundMode(wx
.TRANSPARENT
)
25 dc
.SetFont(attr
.GetFont())
27 text
= grid
.GetCellValue(row
, col
)
28 colors
= ["RED", "WHITE", "SKY BLUE"]
33 dc
.SetTextForeground(random
.choice(colors
))
34 dc
.DrawText(ch
, (x
, y
))
35 w
, h
= dc
.GetTextExtent(ch
)
37 if x
> rect
.right
- 5:
41 def GetBestSize(self
, grid
, attr
, dc
, row
, col
):
42 text
= grid
.GetCellValue(row
, col
)
43 dc
.SetFont(attr
.GetFont())
44 w
, h
= dc
.GetTextExtent(text
)
49 return MyCustomRenderer()
52 #---------------------------------------------------------------------------
55 ('GridCellStringRenderer\n(the default)', 'this is a text value', gridlib
.GridCellStringRenderer
, ()),
56 ('GridCellNumberRenderer', '12345', gridlib
.GridCellNumberRenderer
, ()),
57 ('GridCellFloatRenderer', '1234.5678', gridlib
.GridCellFloatRenderer
, (6,2)),
58 ('GridCellBoolRenderer', '1', gridlib
.GridCellBoolRenderer
, ()),
59 ('MyCustomRenderer', 'This is my renderer', MyCustomRenderer
, ()),
63 ('GridCellTextEditor\n(the default)', 'Here is some more text', gridlib
.GridCellTextEditor
, ()),
64 ('GridCellNumberEditor\nwith min,max', '101', gridlib
.GridCellNumberEditor
, (5, 10005)),
65 ('GridCellNumberEditor\nwithout bounds', '101', gridlib
.GridCellNumberEditor
, ()),
66 ('GridCellFloatEditor', '1234.5678', gridlib
.GridCellFloatEditor
, ()),
67 ('GridCellBoolEditor', '1', gridlib
.GridCellBoolEditor
, ()),
68 ('GridCellChoiceEditor', 'one', gridlib
.GridCellChoiceEditor
, (['one', 'two', 'three', 'four',
69 'kick', 'Microsoft', 'out the',
74 ('GridCellNumberRenderer\nGridCellNumberEditor', '20792', gridlib
.GridCellNumberRenderer
, gridlib
.GridCellNumberEditor
),
75 ('GridCellBoolRenderer\nGridCellBoolEditor', '1', gridlib
.GridCellBoolRenderer
, gridlib
.GridCellBoolEditor
),
79 class EditorsAndRenderersGrid(gridlib
.Grid
):
80 def __init__(self
, parent
, log
):
81 gridlib
.Grid
.__init
__(self
, parent
, -1)
84 self
.CreateGrid(25, 8)
89 self
.SetCellValue(0, renCol
, '''\
90 Cell Renderers are used to draw
91 the contents of the cell when they
92 need to be refreshed. Different
93 types of Renderers can be plugged in
94 to different cells in the grid, it can
95 even be automatically determined based
96 on the type of data in the cell.
99 self
.SetCellValue(0, edCol
, '''\
100 Cell Editors are used when the
101 value of the cell is edited by
102 the user. An editor class is
103 wrapped around a an object
104 derived from wxControl and it
105 implements some methods required
106 to integrate with the grid.
109 self
.SetCellValue(16, renCol
, '''\
110 Here are some combinations of Editors and
111 Renderers used together.
116 for label
, value
, renderClass
, args
in rendererDemoData
:
117 renderer
= renderClass(*args
)
118 self
.SetCellValue(row
, renCol
, label
)
119 self
.SetCellValue(row
, renCol
+1, value
)
120 self
.SetCellRenderer(row
, renCol
+1, renderer
)
126 for label
, value
, editorClass
, args
in editorDemoData
:
127 editor
= editorClass(*args
)
128 self
.SetCellValue(row
, edCol
, label
)
129 self
.SetCellValue(row
, edCol
+1, value
)
130 self
.SetCellEditor(row
, edCol
+1, editor
)
136 for label
, value
, renClass
, edClass
in comboDemoData
:
137 self
.SetCellValue(row
, renCol
, label
)
138 self
.SetCellValue(row
, renCol
+1, value
)
140 renderer
= renClass()
141 self
.SetCellEditor(row
, renCol
+1, editor
)
142 self
.SetCellRenderer(row
, renCol
+1, renderer
)
145 font
= self
.GetFont()
146 font
.SetWeight(wx
.BOLD
)
147 attr
= gridlib
.GridCellAttr()
149 attr
.SetBackgroundColour(wx
.LIGHT_GREY
)
150 attr
.SetReadOnly(True)
151 attr
.SetAlignment(wx
.RIGHT
, -1)
152 self
.SetColAttr(renCol
, attr
)
154 self
.SetColAttr(edCol
, attr
)
156 # There is a bug in wxGTK for this method...
157 self
.AutoSizeColumns(True)
158 self
.AutoSizeRows(True)
160 self
.Bind(gridlib
.EVT_GRID_CELL_LEFT_DCLICK
, self
.OnLeftDClick
)
163 # I do this because I don't like the default behaviour of not starting the
164 # cell editor on double clicks, but only a second click.
165 def OnLeftDClick(self
, evt
):
166 if self
.CanEnableCellControl():
167 self
.EnableCellEditControl()
170 #---------------------------------------------------------------------------
172 class TestFrame(wx
.Frame
):
173 def __init__(self
, parent
, log
):
174 wx
.Frame
.__init
__(self
, parent
, -1, "Editors and Renderers Demo", size
=(640,480))
175 grid
= EditorsAndRenderersGrid(self
, log
)
179 #---------------------------------------------------------------------------
181 if __name__
== '__main__':
183 app
= wx
.PySimpleApp()
184 frame
= TestFrame(None, sys
.stdout
)
189 #---------------------------------------------------------------------------