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