]>
Commit | Line | Data |
---|---|---|
f6bcfd97 BP |
1 | from wxPython.wx import * |
2 | from wxPython.grid import * | |
d56cebe7 | 3 | from wxPython.lib.mixins.grid import wxGridAutoEditMixin |
f6bcfd97 BP |
4 | |
5 | #--------------------------------------------------------------------------- | |
6 | ||
b166c703 | 7 | class SimpleGrid(wxGrid): ##, wxGridAutoEditMixin): |
f6bcfd97 BP |
8 | def __init__(self, parent, log): |
9 | wxGrid.__init__(self, parent, -1) | |
b166c703 | 10 | ##wxGridAutoEditMixin.__init__(self) |
f6bcfd97 | 11 | self.log = log |
c368d904 RD |
12 | self.moveTo = None |
13 | ||
14 | EVT_IDLE(self, self.OnIdle) | |
f6bcfd97 | 15 | |
1e4a197e RD |
16 | self.CreateGrid(25, 25) #, wxGrid.wxGridSelectRows) |
17 | ##self.EnableEditing(False) | |
f6bcfd97 BP |
18 | |
19 | # simple cell formatting | |
20 | self.SetColSize(3, 200) | |
21 | self.SetRowSize(4, 45) | |
22 | self.SetCellValue(0, 0, "First cell") | |
23 | self.SetCellValue(1, 1, "Another cell") | |
24 | self.SetCellValue(2, 2, "Yet another cell") | |
9416aa89 | 25 | self.SetCellValue(3, 3, "This cell is read-only") |
f6bcfd97 BP |
26 | self.SetCellFont(0, 0, wxFont(12, wxROMAN, wxITALIC, wxNORMAL)) |
27 | self.SetCellTextColour(1, 1, wxRED) | |
28 | self.SetCellBackgroundColour(2, 2, wxCYAN) | |
1e4a197e | 29 | self.SetReadOnly(3, 3, True) |
9416aa89 | 30 | |
1e4a197e | 31 | self.SetCellEditor(5, 0, wxGridCellNumberEditor(1,1000)) |
d56cebe7 RD |
32 | self.SetCellValue(5, 0, "123") |
33 | self.SetCellEditor(6, 0, wxGridCellFloatEditor()) | |
34 | self.SetCellValue(6, 0, "123.34") | |
f54a35fe | 35 | self.SetCellEditor(7, 0, wxGridCellNumberEditor()) |
f6bcfd97 | 36 | |
ab11ebfa RD |
37 | self.SetCellValue(6, 3, "You can veto editing this cell") |
38 | ||
2f0f3b0f RD |
39 | #self.SetRowLabelSize(0) |
40 | #self.SetColLabelSize(0) | |
ab11ebfa | 41 | |
f6bcfd97 BP |
42 | # attribute objects let you keep a set of formatting values |
43 | # in one spot, and reuse them if needed | |
44 | attr = wxGridCellAttr() | |
45 | attr.SetTextColour(wxBLACK) | |
46 | attr.SetBackgroundColour(wxRED) | |
47 | attr.SetFont(wxFont(10, wxSWISS, wxNORMAL, wxBOLD)) | |
48 | ||
49 | # you can set cell attributes for the whole row (or column) | |
50 | self.SetRowAttr(5, attr) | |
51 | ||
3ca6a5f0 BP |
52 | self.SetColLabelValue(0, "Custom") |
53 | self.SetColLabelValue(1, "column") | |
54 | self.SetColLabelValue(2, "labels") | |
f6bcfd97 | 55 | |
f3d9dc1d RD |
56 | self.SetColLabelAlignment(wxALIGN_LEFT, wxALIGN_BOTTOM) |
57 | ||
1e4a197e RD |
58 | #self.SetDefaultCellOverflow(False) |
59 | #r = wxGridCellAutoWrapStringRenderer() | |
60 | #self.SetCellRenderer(9, 1, r) | |
61 | ||
62 | # overflow cells | |
63 | self.SetCellValue( 9, 1, "This default cell will overflow into neighboring cells, but not if you turn overflow off."); | |
64 | self.SetCellSize(11, 1, 3, 3); | |
65 | self.SetCellAlignment(11, 1, wxALIGN_CENTRE, wxALIGN_CENTRE); | |
66 | self.SetCellValue(11, 1, "This cell is set to span 3 rows and 3 columns"); | |
67 | ||
68 | ||
1fded56b RD |
69 | editor = wxGridCellTextEditor() |
70 | editor.SetParameters('10') | |
71 | self.SetCellEditor(0, 4, editor) | |
72 | self.SetCellValue(0, 4, "Limited text") | |
73 | ||
1e4a197e | 74 | |
f6bcfd97 BP |
75 | # test all the events |
76 | EVT_GRID_CELL_LEFT_CLICK(self, self.OnCellLeftClick) | |
77 | EVT_GRID_CELL_RIGHT_CLICK(self, self.OnCellRightClick) | |
78 | EVT_GRID_CELL_LEFT_DCLICK(self, self.OnCellLeftDClick) | |
79 | EVT_GRID_CELL_RIGHT_DCLICK(self, self.OnCellRightDClick) | |
80 | ||
81 | EVT_GRID_LABEL_LEFT_CLICK(self, self.OnLabelLeftClick) | |
82 | EVT_GRID_LABEL_RIGHT_CLICK(self, self.OnLabelRightClick) | |
83 | EVT_GRID_LABEL_LEFT_DCLICK(self, self.OnLabelLeftDClick) | |
84 | EVT_GRID_LABEL_RIGHT_DCLICK(self, self.OnLabelRightDClick) | |
85 | ||
86 | EVT_GRID_ROW_SIZE(self, self.OnRowSize) | |
87 | EVT_GRID_COL_SIZE(self, self.OnColSize) | |
88 | ||
89 | EVT_GRID_RANGE_SELECT(self, self.OnRangeSelect) | |
90 | EVT_GRID_CELL_CHANGE(self, self.OnCellChange) | |
91 | EVT_GRID_SELECT_CELL(self, self.OnSelectCell) | |
92 | ||
93 | EVT_GRID_EDITOR_SHOWN(self, self.OnEditorShown) | |
94 | EVT_GRID_EDITOR_HIDDEN(self, self.OnEditorHidden) | |
bf7945ce | 95 | EVT_GRID_EDITOR_CREATED(self, self.OnEditorCreated) |
f6bcfd97 BP |
96 | |
97 | ||
19a97bd6 | 98 | |
f6bcfd97 BP |
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 | ||
140 | def OnRowSize(self, evt): | |
141 | self.log.write("OnRowSize: row %d, %s\n" % | |
142 | (evt.GetRowOrCol(), evt.GetPosition())) | |
143 | evt.Skip() | |
144 | ||
145 | def OnColSize(self, evt): | |
146 | self.log.write("OnColSize: col %d, %s\n" % | |
147 | (evt.GetRowOrCol(), evt.GetPosition())) | |
148 | evt.Skip() | |
149 | ||
150 | def OnRangeSelect(self, evt): | |
151 | if evt.Selecting(): | |
152 | self.log.write("OnRangeSelect: top-left %s, bottom-right %s\n" % | |
153 | (evt.GetTopLeftCoords(), evt.GetBottomRightCoords())) | |
154 | evt.Skip() | |
155 | ||
c368d904 | 156 | |
f6bcfd97 BP |
157 | def OnCellChange(self, evt): |
158 | self.log.write("OnCellChange: (%d,%d) %s\n" % | |
159 | (evt.GetRow(), evt.GetCol(), evt.GetPosition())) | |
c368d904 RD |
160 | |
161 | # Show how to stay in a cell that has bad data. We can't just | |
162 | # call SetGridCursor here since we are nested inside one so it | |
8b9a4190 | 163 | # won't have any effect. Instead, set coordinates to move to in |
c368d904 RD |
164 | # idle time. |
165 | value = self.GetCellValue(evt.GetRow(), evt.GetCol()) | |
166 | if value == 'no good': | |
167 | self.moveTo = evt.GetRow(), evt.GetCol() | |
168 | ||
d56cebe7 | 169 | |
c368d904 RD |
170 | def OnIdle(self, evt): |
171 | if self.moveTo != None: | |
172 | self.SetGridCursor(self.moveTo[0], self.moveTo[1]) | |
173 | self.moveTo = None | |
d56cebe7 | 174 | evt.Skip() |
c368d904 | 175 | |
f6bcfd97 BP |
176 | |
177 | def OnSelectCell(self, evt): | |
178 | self.log.write("OnSelectCell: (%d,%d) %s\n" % | |
179 | (evt.GetRow(), evt.GetCol(), evt.GetPosition())) | |
c368d904 RD |
180 | |
181 | # Another way to stay in a cell that has a bad value... | |
182 | row = self.GetGridCursorRow() | |
183 | col = self.GetGridCursorCol() | |
184 | if self.IsCellEditControlEnabled(): | |
185 | self.HideCellEditControl() | |
186 | self.DisableCellEditControl() | |
187 | value = self.GetCellValue(row, col) | |
188 | if value == 'no good 2': | |
189 | return # cancels the cell selection | |
d56cebe7 | 190 | evt.Skip() |
c368d904 | 191 | |
f6bcfd97 BP |
192 | |
193 | def OnEditorShown(self, evt): | |
ab11ebfa RD |
194 | if evt.GetRow() == 6 and evt.GetCol() == 3 and \ |
195 | wxMessageBox("Are you sure you wish to edit this cell?", | |
196 | "Checking", wxYES_NO) == wxNO: | |
197 | evt.Veto() | |
198 | return | |
199 | ||
f6bcfd97 BP |
200 | self.log.write("OnEditorShown: (%d,%d) %s\n" % |
201 | (evt.GetRow(), evt.GetCol(), evt.GetPosition())) | |
202 | evt.Skip() | |
203 | ||
ab11ebfa | 204 | |
f6bcfd97 | 205 | def OnEditorHidden(self, evt): |
ab11ebfa RD |
206 | if evt.GetRow() == 6 and evt.GetCol() == 3 and \ |
207 | wxMessageBox("Are you sure you wish to finish editing this cell?", | |
208 | "Checking", wxYES_NO) == wxNO: | |
209 | evt.Veto() | |
210 | return | |
211 | ||
f6bcfd97 BP |
212 | self.log.write("OnEditorHidden: (%d,%d) %s\n" % |
213 | (evt.GetRow(), evt.GetCol(), evt.GetPosition())) | |
214 | evt.Skip() | |
215 | ||
ab11ebfa | 216 | |
bf7945ce RD |
217 | def OnEditorCreated(self, evt): |
218 | self.log.write("OnEditorCreated: (%d, %d) %s\n" % | |
219 | (evt.GetRow(), evt.GetCol(), evt.GetControl())) | |
220 | ||
221 | ||
f6bcfd97 BP |
222 | |
223 | #--------------------------------------------------------------------------- | |
224 | ||
225 | class TestFrame(wxFrame): | |
226 | def __init__(self, parent, log): | |
227 | wxFrame.__init__(self, parent, -1, "Simple Grid Demo", size=(640,480)) | |
19a97bd6 | 228 | grid = SimpleGrid(self, log) |
53fe40ba | 229 | |
f6bcfd97 BP |
230 | |
231 | ||
232 | #--------------------------------------------------------------------------- | |
233 | ||
234 | if __name__ == '__main__': | |
235 | import sys | |
236 | app = wxPySimpleApp() | |
237 | frame = TestFrame(None, sys.stdout) | |
1e4a197e | 238 | frame.Show(True) |
f6bcfd97 BP |
239 | app.MainLoop() |
240 | ||
241 | ||
242 | #--------------------------------------------------------------------------- | |
c368d904 RD |
243 | |
244 |