]>
Commit | Line | Data |
---|---|---|
8fa876ca RD |
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 | # | |
f6bcfd97 | 6 | |
8fa876ca RD |
7 | import random |
8 | ||
9 | import wx | |
10 | import wx.grid as gridlib | |
f6bcfd97 BP |
11 | |
12 | #--------------------------------------------------------------------------- | |
13 | ||
8fa876ca | 14 | class MyCustomRenderer(gridlib.PyGridCellRenderer): |
f6bcfd97 | 15 | def __init__(self): |
8fa876ca | 16 | gridlib.PyGridCellRenderer.__init__(self) |
f6bcfd97 BP |
17 | |
18 | def Draw(self, grid, attr, dc, rect, row, col, isSelected): | |
19 | dc.SetBackgroundMode(wxSOLID) | |
20 | dc.SetBrush(wxBrush(wxBLACK, wxSOLID)) | |
21 | dc.SetPen(wxTRANSPARENT_PEN) | |
fbd5dd1d | 22 | dc.DrawRectangleRect(rect) |
f6bcfd97 | 23 | |
8fa876ca | 24 | dc.SetBackgroundMode(wx.TRANSPARENT) |
f6bcfd97 BP |
25 | dc.SetFont(attr.GetFont()) |
26 | ||
27 | text = grid.GetCellValue(row, col) | |
fbd5dd1d | 28 | colors = ["RED", "WHITE", "SKY BLUE"] |
f6bcfd97 BP |
29 | x = rect.x + 1 |
30 | y = rect.y + 1 | |
8fa876ca | 31 | |
f6bcfd97 BP |
32 | for ch in text: |
33 | dc.SetTextForeground(random.choice(colors)) | |
fbd5dd1d | 34 | dc.DrawText(ch, (x, y)) |
f6bcfd97 BP |
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) | |
8fa876ca | 45 | return wx.Size(w, h) |
f6bcfd97 BP |
46 | |
47 | ||
48 | def Clone(self): | |
49 | return MyCustomRenderer() | |
50 | ||
51 | ||
52 | #--------------------------------------------------------------------------- | |
53 | ||
54 | rendererDemoData = [ | |
8fa876ca RD |
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, ()), | |
f6bcfd97 BP |
59 | ('MyCustomRenderer', 'This is my renderer', MyCustomRenderer, ()), |
60 | ] | |
61 | ||
62 | editorDemoData = [ | |
8fa876ca RD |
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', | |
f6bcfd97 | 69 | 'kick', 'Microsoft', 'out the', |
1e4a197e | 70 | 'door'], False)), |
f6bcfd97 BP |
71 | ] |
72 | ||
f6bcfd97 | 73 | comboDemoData = [ |
8fa876ca RD |
74 | ('GridCellNumberRenderer\nGridCellNumberEditor', '20792', gridlib.GridCellNumberRenderer, gridlib.GridCellNumberEditor), |
75 | ('GridCellBoolRenderer\nGridCellBoolEditor', '1', gridlib.GridCellBoolRenderer, gridlib.GridCellBoolEditor), | |
f6bcfd97 BP |
76 | ] |
77 | ||
78 | ||
8fa876ca | 79 | class EditorsAndRenderersGrid(gridlib.Grid): |
f6bcfd97 | 80 | def __init__(self, parent, log): |
8fa876ca | 81 | gridlib.Grid.__init__(self, parent, -1) |
f6bcfd97 BP |
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 | |
8fa876ca | 115 | |
f6bcfd97 | 116 | for label, value, renderClass, args in rendererDemoData: |
fbd5dd1d | 117 | renderer = renderClass(*args) |
f6bcfd97 BP |
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 | |
8fa876ca | 125 | |
f6bcfd97 | 126 | for label, value, editorClass, args in editorDemoData: |
fbd5dd1d | 127 | editor = editorClass(*args) |
f6bcfd97 BP |
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 | |
8fa876ca | 135 | |
f6bcfd97 BP |
136 | for label, value, renClass, edClass in comboDemoData: |
137 | self.SetCellValue(row, renCol, label) | |
138 | self.SetCellValue(row, renCol+1, value) | |
fbd5dd1d RD |
139 | editor = edClass() |
140 | renderer = renClass() | |
f6bcfd97 BP |
141 | self.SetCellEditor(row, renCol+1, editor) |
142 | self.SetCellRenderer(row, renCol+1, renderer) | |
143 | row = row + 2 | |
144 | ||
f6bcfd97 | 145 | font = self.GetFont() |
8fa876ca RD |
146 | font.SetWeight(wx.BOLD) |
147 | attr = gridlib.GridCellAttr() | |
f6bcfd97 | 148 | attr.SetFont(font) |
8fa876ca | 149 | attr.SetBackgroundColour(wx.LIGHT_GREY) |
1e4a197e | 150 | attr.SetReadOnly(True) |
8fa876ca | 151 | attr.SetAlignment(wx.RIGHT, -1) |
f6bcfd97 | 152 | self.SetColAttr(renCol, attr) |
9416aa89 | 153 | attr.IncRef() |
f6bcfd97 BP |
154 | self.SetColAttr(edCol, attr) |
155 | ||
156 | # There is a bug in wxGTK for this method... | |
1e4a197e RD |
157 | self.AutoSizeColumns(True) |
158 | self.AutoSizeRows(True) | |
f6bcfd97 | 159 | |
8fa876ca | 160 | self.Bind(gridlib.EVT_GRID_CELL_LEFT_DCLICK, self.OnLeftDClick) |
f6bcfd97 BP |
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 | ||
8fa876ca | 172 | class TestFrame(wx.Frame): |
f6bcfd97 | 173 | def __init__(self, parent, log): |
8fa876ca | 174 | wx.Frame.__init__(self, parent, -1, "Editors and Renderers Demo", size=(640,480)) |
f6bcfd97 BP |
175 | grid = EditorsAndRenderersGrid(self, log) |
176 | ||
177 | ||
178 | ||
179 | #--------------------------------------------------------------------------- | |
180 | ||
181 | if __name__ == '__main__': | |
182 | import sys | |
8fa876ca | 183 | app = wx.PySimpleApp() |
f6bcfd97 | 184 | frame = TestFrame(None, sys.stdout) |
1e4a197e | 185 | frame.Show(True) |
f6bcfd97 BP |
186 | app.MainLoop() |
187 | ||
188 | ||
189 | #--------------------------------------------------------------------------- |