]>
Commit | Line | Data |
---|---|---|
8fa876ca RD |
1 | |
2 | import wx | |
3 | import wx.grid as gridlib | |
4 | #import wx.lib.mixins.grid as mixins | |
f6bcfd97 BP |
5 | |
6 | #--------------------------------------------------------------------------- | |
7 | ||
8fa876ca | 8 | class SimpleGrid(gridlib.Grid): ##, mixins.GridAutoEditMixin): |
f6bcfd97 | 9 | def __init__(self, parent, log): |
8fa876ca RD |
10 | gridlib.Grid.__init__(self, parent, -1) |
11 | ##mixins.GridAutoEditMixin.__init__(self) | |
f6bcfd97 | 12 | self.log = log |
c368d904 RD |
13 | self.moveTo = None |
14 | ||
8fa876ca | 15 | self.Bind(wx.EVT_IDLE, self.OnIdle) |
f6bcfd97 | 16 | |
c7ea279b | 17 | self.CreateGrid(25, 25)#, gridlib.Grid.SelectRows) |
1e4a197e | 18 | ##self.EnableEditing(False) |
f6bcfd97 BP |
19 | |
20 | # simple cell formatting | |
21 | self.SetColSize(3, 200) | |
22 | self.SetRowSize(4, 45) | |
23 | self.SetCellValue(0, 0, "First cell") | |
24 | self.SetCellValue(1, 1, "Another cell") | |
25 | self.SetCellValue(2, 2, "Yet another cell") | |
9416aa89 | 26 | self.SetCellValue(3, 3, "This cell is read-only") |
8fa876ca RD |
27 | self.SetCellFont(0, 0, wx.Font(12, wx.ROMAN, wx.ITALIC, wx.NORMAL)) |
28 | self.SetCellTextColour(1, 1, wx.RED) | |
29 | self.SetCellBackgroundColour(2, 2, wx.CYAN) | |
1e4a197e | 30 | self.SetReadOnly(3, 3, True) |
9416aa89 | 31 | |
8fa876ca | 32 | self.SetCellEditor(5, 0, gridlib.GridCellNumberEditor(1,1000)) |
d56cebe7 | 33 | self.SetCellValue(5, 0, "123") |
8fa876ca | 34 | self.SetCellEditor(6, 0, gridlib.GridCellFloatEditor()) |
d56cebe7 | 35 | self.SetCellValue(6, 0, "123.34") |
8fa876ca | 36 | self.SetCellEditor(7, 0, gridlib.GridCellNumberEditor()) |
f6bcfd97 | 37 | |
ab11ebfa RD |
38 | self.SetCellValue(6, 3, "You can veto editing this cell") |
39 | ||
2f0f3b0f RD |
40 | #self.SetRowLabelSize(0) |
41 | #self.SetColLabelSize(0) | |
ab11ebfa | 42 | |
f6bcfd97 BP |
43 | # attribute objects let you keep a set of formatting values |
44 | # in one spot, and reuse them if needed | |
8fa876ca RD |
45 | attr = gridlib.GridCellAttr() |
46 | attr.SetTextColour(wx.BLACK) | |
47 | attr.SetBackgroundColour(wx.RED) | |
48 | attr.SetFont(wx.Font(10, wx.SWISS, wx.NORMAL, wx.BOLD)) | |
f6bcfd97 BP |
49 | |
50 | # you can set cell attributes for the whole row (or column) | |
51 | self.SetRowAttr(5, attr) | |
52 | ||
3ca6a5f0 BP |
53 | self.SetColLabelValue(0, "Custom") |
54 | self.SetColLabelValue(1, "column") | |
55 | self.SetColLabelValue(2, "labels") | |
f6bcfd97 | 56 | |
8fa876ca | 57 | self.SetColLabelAlignment(wx.ALIGN_LEFT, wx.ALIGN_BOTTOM) |
f3d9dc1d | 58 | |
1e4a197e | 59 | #self.SetDefaultCellOverflow(False) |
644f2d83 | 60 | #r = gridlib.GridCellAutoWrapStringRenderer() |
1e4a197e RD |
61 | #self.SetCellRenderer(9, 1, r) |
62 | ||
63 | # overflow cells | |
64 | self.SetCellValue( 9, 1, "This default cell will overflow into neighboring cells, but not if you turn overflow off."); | |
65 | self.SetCellSize(11, 1, 3, 3); | |
8fa876ca | 66 | self.SetCellAlignment(11, 1, wx.ALIGN_CENTRE, wx.ALIGN_CENTRE); |
1e4a197e RD |
67 | self.SetCellValue(11, 1, "This cell is set to span 3 rows and 3 columns"); |
68 | ||
69 | ||
8fa876ca | 70 | editor = gridlib.GridCellTextEditor() |
1fded56b RD |
71 | editor.SetParameters('10') |
72 | self.SetCellEditor(0, 4, editor) | |
73 | self.SetCellValue(0, 4, "Limited text") | |
74 | ||
5589b01b RD |
75 | renderer = gridlib.GridCellAutoWrapStringRenderer() |
76 | self.SetCellRenderer(15,0, renderer) | |
77 | self.SetCellValue(15,0, "The text in this cell will be rendered with word-wrapping") | |
1e4a197e | 78 | |
5589b01b | 79 | |
f6bcfd97 | 80 | # test all the events |
8fa876ca RD |
81 | self.Bind(gridlib.EVT_GRID_CELL_LEFT_CLICK, self.OnCellLeftClick) |
82 | self.Bind(gridlib.EVT_GRID_CELL_RIGHT_CLICK, self.OnCellRightClick) | |
83 | self.Bind(gridlib.EVT_GRID_CELL_LEFT_DCLICK, self.OnCellLeftDClick) | |
84 | self.Bind(gridlib.EVT_GRID_CELL_RIGHT_DCLICK, self.OnCellRightDClick) | |
f6bcfd97 | 85 | |
8fa876ca RD |
86 | self.Bind(gridlib.EVT_GRID_LABEL_LEFT_CLICK, self.OnLabelLeftClick) |
87 | self.Bind(gridlib.EVT_GRID_LABEL_RIGHT_CLICK, self.OnLabelRightClick) | |
88 | self.Bind(gridlib.EVT_GRID_LABEL_LEFT_DCLICK, self.OnLabelLeftDClick) | |
89 | self.Bind(gridlib.EVT_GRID_LABEL_RIGHT_DCLICK, self.OnLabelRightDClick) | |
f6bcfd97 | 90 | |
8fa876ca RD |
91 | self.Bind(gridlib.EVT_GRID_ROW_SIZE, self.OnRowSize) |
92 | self.Bind(gridlib.EVT_GRID_COL_SIZE, self.OnColSize) | |
f6bcfd97 | 93 | |
8fa876ca RD |
94 | self.Bind(gridlib.EVT_GRID_RANGE_SELECT, self.OnRangeSelect) |
95 | self.Bind(gridlib.EVT_GRID_CELL_CHANGE, self.OnCellChange) | |
96 | self.Bind(gridlib.EVT_GRID_SELECT_CELL, self.OnSelectCell) | |
f6bcfd97 | 97 | |
8fa876ca RD |
98 | self.Bind(gridlib.EVT_GRID_EDITOR_SHOWN, self.OnEditorShown) |
99 | self.Bind(gridlib.EVT_GRID_EDITOR_HIDDEN, self.OnEditorHidden) | |
100 | self.Bind(gridlib.EVT_GRID_EDITOR_CREATED, self.OnEditorCreated) | |
f6bcfd97 | 101 | |
19a97bd6 | 102 | |
f6bcfd97 BP |
103 | def OnCellLeftClick(self, evt): |
104 | self.log.write("OnCellLeftClick: (%d,%d) %s\n" % | |
105 | (evt.GetRow(), evt.GetCol(), evt.GetPosition())) | |
106 | evt.Skip() | |
107 | ||
108 | def OnCellRightClick(self, evt): | |
109 | self.log.write("OnCellRightClick: (%d,%d) %s\n" % | |
110 | (evt.GetRow(), evt.GetCol(), evt.GetPosition())) | |
111 | evt.Skip() | |
112 | ||
113 | def OnCellLeftDClick(self, evt): | |
114 | self.log.write("OnCellLeftDClick: (%d,%d) %s\n" % | |
115 | (evt.GetRow(), evt.GetCol(), evt.GetPosition())) | |
116 | evt.Skip() | |
117 | ||
118 | def OnCellRightDClick(self, evt): | |
119 | self.log.write("OnCellRightDClick: (%d,%d) %s\n" % | |
120 | (evt.GetRow(), evt.GetCol(), evt.GetPosition())) | |
121 | evt.Skip() | |
122 | ||
123 | def OnLabelLeftClick(self, evt): | |
124 | self.log.write("OnLabelLeftClick: (%d,%d) %s\n" % | |
125 | (evt.GetRow(), evt.GetCol(), evt.GetPosition())) | |
126 | evt.Skip() | |
127 | ||
128 | def OnLabelRightClick(self, evt): | |
129 | self.log.write("OnLabelRightClick: (%d,%d) %s\n" % | |
130 | (evt.GetRow(), evt.GetCol(), evt.GetPosition())) | |
131 | evt.Skip() | |
132 | ||
133 | def OnLabelLeftDClick(self, evt): | |
134 | self.log.write("OnLabelLeftDClick: (%d,%d) %s\n" % | |
135 | (evt.GetRow(), evt.GetCol(), evt.GetPosition())) | |
136 | evt.Skip() | |
137 | ||
138 | def OnLabelRightDClick(self, evt): | |
139 | self.log.write("OnLabelRightDClick: (%d,%d) %s\n" % | |
140 | (evt.GetRow(), evt.GetCol(), evt.GetPosition())) | |
141 | evt.Skip() | |
142 | ||
f6bcfd97 BP |
143 | def OnRowSize(self, evt): |
144 | self.log.write("OnRowSize: row %d, %s\n" % | |
145 | (evt.GetRowOrCol(), evt.GetPosition())) | |
146 | evt.Skip() | |
147 | ||
148 | def OnColSize(self, evt): | |
149 | self.log.write("OnColSize: col %d, %s\n" % | |
150 | (evt.GetRowOrCol(), evt.GetPosition())) | |
151 | evt.Skip() | |
152 | ||
153 | def OnRangeSelect(self, evt): | |
154 | if evt.Selecting(): | |
155 | self.log.write("OnRangeSelect: top-left %s, bottom-right %s\n" % | |
156 | (evt.GetTopLeftCoords(), evt.GetBottomRightCoords())) | |
157 | evt.Skip() | |
158 | ||
c368d904 | 159 | |
f6bcfd97 BP |
160 | def OnCellChange(self, evt): |
161 | self.log.write("OnCellChange: (%d,%d) %s\n" % | |
162 | (evt.GetRow(), evt.GetCol(), evt.GetPosition())) | |
c368d904 RD |
163 | |
164 | # Show how to stay in a cell that has bad data. We can't just | |
165 | # call SetGridCursor here since we are nested inside one so it | |
8b9a4190 | 166 | # won't have any effect. Instead, set coordinates to move to in |
c368d904 RD |
167 | # idle time. |
168 | value = self.GetCellValue(evt.GetRow(), evt.GetCol()) | |
8fa876ca | 169 | |
c368d904 RD |
170 | if value == 'no good': |
171 | self.moveTo = evt.GetRow(), evt.GetCol() | |
172 | ||
d56cebe7 | 173 | |
c368d904 RD |
174 | def OnIdle(self, evt): |
175 | if self.moveTo != None: | |
176 | self.SetGridCursor(self.moveTo[0], self.moveTo[1]) | |
177 | self.moveTo = None | |
8fa876ca | 178 | |
d56cebe7 | 179 | evt.Skip() |
c368d904 | 180 | |
f6bcfd97 BP |
181 | |
182 | def OnSelectCell(self, evt): | |
183 | self.log.write("OnSelectCell: (%d,%d) %s\n" % | |
184 | (evt.GetRow(), evt.GetCol(), evt.GetPosition())) | |
c368d904 RD |
185 | |
186 | # Another way to stay in a cell that has a bad value... | |
187 | row = self.GetGridCursorRow() | |
188 | col = self.GetGridCursorCol() | |
8fa876ca | 189 | |
c368d904 RD |
190 | if self.IsCellEditControlEnabled(): |
191 | self.HideCellEditControl() | |
192 | self.DisableCellEditControl() | |
8fa876ca | 193 | |
c368d904 | 194 | value = self.GetCellValue(row, col) |
8fa876ca | 195 | |
c368d904 RD |
196 | if value == 'no good 2': |
197 | return # cancels the cell selection | |
8fa876ca | 198 | |
d56cebe7 | 199 | evt.Skip() |
c368d904 | 200 | |
f6bcfd97 BP |
201 | |
202 | def OnEditorShown(self, evt): | |
ab11ebfa | 203 | if evt.GetRow() == 6 and evt.GetCol() == 3 and \ |
8fa876ca RD |
204 | wx.MessageBox("Are you sure you wish to edit this cell?", |
205 | "Checking", wx.YES_NO) == wx.NO: | |
ab11ebfa RD |
206 | evt.Veto() |
207 | return | |
208 | ||
f6bcfd97 BP |
209 | self.log.write("OnEditorShown: (%d,%d) %s\n" % |
210 | (evt.GetRow(), evt.GetCol(), evt.GetPosition())) | |
211 | evt.Skip() | |
212 | ||
ab11ebfa | 213 | |
f6bcfd97 | 214 | def OnEditorHidden(self, evt): |
ab11ebfa | 215 | if evt.GetRow() == 6 and evt.GetCol() == 3 and \ |
8fa876ca RD |
216 | wx.MessageBox("Are you sure you wish to finish editing this cell?", |
217 | "Checking", wx.YES_NO) == wx.NO: | |
ab11ebfa RD |
218 | evt.Veto() |
219 | return | |
220 | ||
f6bcfd97 BP |
221 | self.log.write("OnEditorHidden: (%d,%d) %s\n" % |
222 | (evt.GetRow(), evt.GetCol(), evt.GetPosition())) | |
223 | evt.Skip() | |
224 | ||
ab11ebfa | 225 | |
bf7945ce RD |
226 | def OnEditorCreated(self, evt): |
227 | self.log.write("OnEditorCreated: (%d, %d) %s\n" % | |
228 | (evt.GetRow(), evt.GetCol(), evt.GetControl())) | |
229 | ||
230 | ||
f6bcfd97 BP |
231 | |
232 | #--------------------------------------------------------------------------- | |
233 | ||
8fa876ca | 234 | class TestFrame(wx.Frame): |
f6bcfd97 | 235 | def __init__(self, parent, log): |
8fa876ca | 236 | wx.Frame.__init__(self, parent, -1, "Simple Grid Demo", size=(640,480)) |
591ab02f | 237 | self.grid = SimpleGrid(self, log) |
53fe40ba | 238 | |
f6bcfd97 BP |
239 | |
240 | ||
241 | #--------------------------------------------------------------------------- | |
242 | ||
243 | if __name__ == '__main__': | |
244 | import sys | |
8fa876ca | 245 | app = wx.PySimpleApp() |
f6bcfd97 | 246 | frame = TestFrame(None, sys.stdout) |
1e4a197e | 247 | frame.Show(True) |
591ab02f RD |
248 | #import wx.py |
249 | #shell = wx.py.shell.ShellFrame(frame, locals={'wx':wx, 'frame':frame}) | |
250 | #shell.Show() | |
f6bcfd97 BP |
251 | app.MainLoop() |
252 | ||
253 | ||
254 | #--------------------------------------------------------------------------- | |
c368d904 RD |
255 | |
256 |