]> git.saurik.com Git - wxWidgets.git/blob - wxPython/demo/GridStdEdRend.py
Added PyPlot
[wxWidgets.git] / wxPython / demo / GridStdEdRend.py
1 # 11/25/2003 - Jeff Grimmett (grimmtooth@softhome.net)
2 #
3 # o Updated for V2.5
4 # o There is one wx.Size() I haven't figured out how to get rid of yet.
5 #
6
7 import random
8
9 import wx
10 import wx.grid as gridlib
11
12 #---------------------------------------------------------------------------
13
14 class MyCustomRenderer(gridlib.PyGridCellRenderer):
15 def __init__(self):
16 gridlib.PyGridCellRenderer.__init__(self)
17
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)
23
24 dc.SetBackgroundMode(wx.TRANSPARENT)
25 dc.SetFont(attr.GetFont())
26
27 text = grid.GetCellValue(row, col)
28 colors = ["RED", "WHITE", "SKY BLUE"]
29 x = rect.x + 1
30 y = rect.y + 1
31
32 for ch in text:
33 dc.SetTextForeground(random.choice(colors))
34 dc.DrawText(ch, (x, y))
35 w, h = dc.GetTextExtent(ch)
36 x = x + w
37 if x > rect.right - 5:
38 break
39
40
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)
45 return wx.Size(w, h)
46
47
48 def Clone(self):
49 return MyCustomRenderer()
50
51
52 #---------------------------------------------------------------------------
53
54 rendererDemoData = [
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, ()),
60 ]
61
62 editorDemoData = [
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',
70 'door'], False)),
71 ]
72
73 comboDemoData = [
74 ('GridCellNumberRenderer\nGridCellNumberEditor', '20792', gridlib.GridCellNumberRenderer, gridlib.GridCellNumberEditor),
75 ('GridCellBoolRenderer\nGridCellBoolEditor', '1', gridlib.GridCellBoolRenderer, gridlib.GridCellBoolEditor),
76 ]
77
78
79 class EditorsAndRenderersGrid(gridlib.Grid):
80 def __init__(self, parent, log):
81 gridlib.Grid.__init__(self, parent, -1)
82 self.log = log
83
84 self.CreateGrid(25, 8)
85 renCol = 1
86 edCol = 4
87
88
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.
97 ''')
98
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.
107 ''')
108
109 self.SetCellValue(16, renCol, '''\
110 Here are some combinations of Editors and
111 Renderers used together.
112 ''')
113
114 row = 2
115
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)
121 row = row + 2
122
123
124 row = 2
125
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)
131 row = row + 2
132
133
134 row = 18
135
136 for label, value, renClass, edClass in comboDemoData:
137 self.SetCellValue(row, renCol, label)
138 self.SetCellValue(row, renCol+1, value)
139 editor = edClass()
140 renderer = renClass()
141 self.SetCellEditor(row, renCol+1, editor)
142 self.SetCellRenderer(row, renCol+1, renderer)
143 row = row + 2
144
145 font = self.GetFont()
146 font.SetWeight(wx.BOLD)
147 attr = gridlib.GridCellAttr()
148 attr.SetFont(font)
149 attr.SetBackgroundColour(wx.LIGHT_GREY)
150 attr.SetReadOnly(True)
151 attr.SetAlignment(wx.RIGHT, -1)
152 self.SetColAttr(renCol, attr)
153 attr.IncRef()
154 self.SetColAttr(edCol, attr)
155
156 # There is a bug in wxGTK for this method...
157 self.AutoSizeColumns(True)
158 self.AutoSizeRows(True)
159
160 self.Bind(gridlib.EVT_GRID_CELL_LEFT_DCLICK, self.OnLeftDClick)
161
162
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()
168
169
170 #---------------------------------------------------------------------------
171
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)
176
177
178
179 #---------------------------------------------------------------------------
180
181 if __name__ == '__main__':
182 import sys
183 app = wx.PySimpleApp()
184 frame = TestFrame(None, sys.stdout)
185 frame.Show(True)
186 app.MainLoop()
187
188
189 #---------------------------------------------------------------------------