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