]>
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 | ||
39 | ||
f6bcfd97 BP |
40 | # attribute objects let you keep a set of formatting values |
41 | # in one spot, and reuse them if needed | |
42 | attr = wxGridCellAttr() | |
43 | attr.SetTextColour(wxBLACK) | |
44 | attr.SetBackgroundColour(wxRED) | |
45 | attr.SetFont(wxFont(10, wxSWISS, wxNORMAL, wxBOLD)) | |
46 | ||
47 | # you can set cell attributes for the whole row (or column) | |
48 | self.SetRowAttr(5, attr) | |
49 | ||
3ca6a5f0 BP |
50 | self.SetColLabelValue(0, "Custom") |
51 | self.SetColLabelValue(1, "column") | |
52 | self.SetColLabelValue(2, "labels") | |
f6bcfd97 | 53 | |
f3d9dc1d RD |
54 | self.SetColLabelAlignment(wxALIGN_LEFT, wxALIGN_BOTTOM) |
55 | ||
1e4a197e RD |
56 | #self.SetDefaultCellOverflow(False) |
57 | #r = wxGridCellAutoWrapStringRenderer() | |
58 | #self.SetCellRenderer(9, 1, r) | |
59 | ||
60 | # overflow cells | |
61 | self.SetCellValue( 9, 1, "This default cell will overflow into neighboring cells, but not if you turn overflow off."); | |
62 | self.SetCellSize(11, 1, 3, 3); | |
63 | self.SetCellAlignment(11, 1, wxALIGN_CENTRE, wxALIGN_CENTRE); | |
64 | self.SetCellValue(11, 1, "This cell is set to span 3 rows and 3 columns"); | |
65 | ||
66 | ||
67 | ||
f6bcfd97 BP |
68 | # test all the events |
69 | EVT_GRID_CELL_LEFT_CLICK(self, self.OnCellLeftClick) | |
70 | EVT_GRID_CELL_RIGHT_CLICK(self, self.OnCellRightClick) | |
71 | EVT_GRID_CELL_LEFT_DCLICK(self, self.OnCellLeftDClick) | |
72 | EVT_GRID_CELL_RIGHT_DCLICK(self, self.OnCellRightDClick) | |
73 | ||
74 | EVT_GRID_LABEL_LEFT_CLICK(self, self.OnLabelLeftClick) | |
75 | EVT_GRID_LABEL_RIGHT_CLICK(self, self.OnLabelRightClick) | |
76 | EVT_GRID_LABEL_LEFT_DCLICK(self, self.OnLabelLeftDClick) | |
77 | EVT_GRID_LABEL_RIGHT_DCLICK(self, self.OnLabelRightDClick) | |
78 | ||
79 | EVT_GRID_ROW_SIZE(self, self.OnRowSize) | |
80 | EVT_GRID_COL_SIZE(self, self.OnColSize) | |
81 | ||
82 | EVT_GRID_RANGE_SELECT(self, self.OnRangeSelect) | |
83 | EVT_GRID_CELL_CHANGE(self, self.OnCellChange) | |
84 | EVT_GRID_SELECT_CELL(self, self.OnSelectCell) | |
85 | ||
86 | EVT_GRID_EDITOR_SHOWN(self, self.OnEditorShown) | |
87 | EVT_GRID_EDITOR_HIDDEN(self, self.OnEditorHidden) | |
bf7945ce | 88 | EVT_GRID_EDITOR_CREATED(self, self.OnEditorCreated) |
f6bcfd97 BP |
89 | |
90 | ||
19a97bd6 | 91 | |
f6bcfd97 BP |
92 | def OnCellLeftClick(self, evt): |
93 | self.log.write("OnCellLeftClick: (%d,%d) %s\n" % | |
94 | (evt.GetRow(), evt.GetCol(), evt.GetPosition())) | |
95 | evt.Skip() | |
96 | ||
97 | def OnCellRightClick(self, evt): | |
98 | self.log.write("OnCellRightClick: (%d,%d) %s\n" % | |
99 | (evt.GetRow(), evt.GetCol(), evt.GetPosition())) | |
100 | evt.Skip() | |
101 | ||
102 | def OnCellLeftDClick(self, evt): | |
103 | self.log.write("OnCellLeftDClick: (%d,%d) %s\n" % | |
104 | (evt.GetRow(), evt.GetCol(), evt.GetPosition())) | |
105 | evt.Skip() | |
106 | ||
107 | def OnCellRightDClick(self, evt): | |
108 | self.log.write("OnCellRightDClick: (%d,%d) %s\n" % | |
109 | (evt.GetRow(), evt.GetCol(), evt.GetPosition())) | |
110 | evt.Skip() | |
111 | ||
112 | def OnLabelLeftClick(self, evt): | |
113 | self.log.write("OnLabelLeftClick: (%d,%d) %s\n" % | |
114 | (evt.GetRow(), evt.GetCol(), evt.GetPosition())) | |
115 | evt.Skip() | |
116 | ||
117 | def OnLabelRightClick(self, evt): | |
118 | self.log.write("OnLabelRightClick: (%d,%d) %s\n" % | |
119 | (evt.GetRow(), evt.GetCol(), evt.GetPosition())) | |
120 | evt.Skip() | |
121 | ||
122 | def OnLabelLeftDClick(self, evt): | |
123 | self.log.write("OnLabelLeftDClick: (%d,%d) %s\n" % | |
124 | (evt.GetRow(), evt.GetCol(), evt.GetPosition())) | |
125 | evt.Skip() | |
126 | ||
127 | def OnLabelRightDClick(self, evt): | |
128 | self.log.write("OnLabelRightDClick: (%d,%d) %s\n" % | |
129 | (evt.GetRow(), evt.GetCol(), evt.GetPosition())) | |
130 | evt.Skip() | |
131 | ||
132 | ||
133 | def OnRowSize(self, evt): | |
134 | self.log.write("OnRowSize: row %d, %s\n" % | |
135 | (evt.GetRowOrCol(), evt.GetPosition())) | |
136 | evt.Skip() | |
137 | ||
138 | def OnColSize(self, evt): | |
139 | self.log.write("OnColSize: col %d, %s\n" % | |
140 | (evt.GetRowOrCol(), evt.GetPosition())) | |
141 | evt.Skip() | |
142 | ||
143 | def OnRangeSelect(self, evt): | |
144 | if evt.Selecting(): | |
145 | self.log.write("OnRangeSelect: top-left %s, bottom-right %s\n" % | |
146 | (evt.GetTopLeftCoords(), evt.GetBottomRightCoords())) | |
147 | evt.Skip() | |
148 | ||
c368d904 | 149 | |
f6bcfd97 BP |
150 | def OnCellChange(self, evt): |
151 | self.log.write("OnCellChange: (%d,%d) %s\n" % | |
152 | (evt.GetRow(), evt.GetCol(), evt.GetPosition())) | |
c368d904 RD |
153 | |
154 | # Show how to stay in a cell that has bad data. We can't just | |
155 | # call SetGridCursor here since we are nested inside one so it | |
156 | # won't have any effect. Instead, set coordinants to move to in | |
157 | # idle time. | |
158 | value = self.GetCellValue(evt.GetRow(), evt.GetCol()) | |
159 | if value == 'no good': | |
160 | self.moveTo = evt.GetRow(), evt.GetCol() | |
161 | ||
d56cebe7 | 162 | |
c368d904 RD |
163 | def OnIdle(self, evt): |
164 | if self.moveTo != None: | |
165 | self.SetGridCursor(self.moveTo[0], self.moveTo[1]) | |
166 | self.moveTo = None | |
d56cebe7 | 167 | evt.Skip() |
c368d904 | 168 | |
f6bcfd97 BP |
169 | |
170 | def OnSelectCell(self, evt): | |
171 | self.log.write("OnSelectCell: (%d,%d) %s\n" % | |
172 | (evt.GetRow(), evt.GetCol(), evt.GetPosition())) | |
c368d904 RD |
173 | |
174 | # Another way to stay in a cell that has a bad value... | |
175 | row = self.GetGridCursorRow() | |
176 | col = self.GetGridCursorCol() | |
177 | if self.IsCellEditControlEnabled(): | |
178 | self.HideCellEditControl() | |
179 | self.DisableCellEditControl() | |
180 | value = self.GetCellValue(row, col) | |
181 | if value == 'no good 2': | |
182 | return # cancels the cell selection | |
d56cebe7 | 183 | evt.Skip() |
c368d904 | 184 | |
f6bcfd97 BP |
185 | |
186 | def OnEditorShown(self, evt): | |
ab11ebfa RD |
187 | if evt.GetRow() == 6 and evt.GetCol() == 3 and \ |
188 | wxMessageBox("Are you sure you wish to edit this cell?", | |
189 | "Checking", wxYES_NO) == wxNO: | |
190 | evt.Veto() | |
191 | return | |
192 | ||
f6bcfd97 BP |
193 | self.log.write("OnEditorShown: (%d,%d) %s\n" % |
194 | (evt.GetRow(), evt.GetCol(), evt.GetPosition())) | |
195 | evt.Skip() | |
196 | ||
ab11ebfa | 197 | |
f6bcfd97 | 198 | def OnEditorHidden(self, evt): |
ab11ebfa RD |
199 | if evt.GetRow() == 6 and evt.GetCol() == 3 and \ |
200 | wxMessageBox("Are you sure you wish to finish editing this cell?", | |
201 | "Checking", wxYES_NO) == wxNO: | |
202 | evt.Veto() | |
203 | return | |
204 | ||
f6bcfd97 BP |
205 | self.log.write("OnEditorHidden: (%d,%d) %s\n" % |
206 | (evt.GetRow(), evt.GetCol(), evt.GetPosition())) | |
207 | evt.Skip() | |
208 | ||
ab11ebfa | 209 | |
bf7945ce RD |
210 | def OnEditorCreated(self, evt): |
211 | self.log.write("OnEditorCreated: (%d, %d) %s\n" % | |
212 | (evt.GetRow(), evt.GetCol(), evt.GetControl())) | |
213 | ||
214 | ||
f6bcfd97 BP |
215 | |
216 | #--------------------------------------------------------------------------- | |
217 | ||
218 | class TestFrame(wxFrame): | |
219 | def __init__(self, parent, log): | |
220 | wxFrame.__init__(self, parent, -1, "Simple Grid Demo", size=(640,480)) | |
19a97bd6 | 221 | grid = SimpleGrid(self, log) |
53fe40ba | 222 | |
f6bcfd97 BP |
223 | |
224 | ||
225 | #--------------------------------------------------------------------------- | |
226 | ||
227 | if __name__ == '__main__': | |
228 | import sys | |
229 | app = wxPySimpleApp() | |
230 | frame = TestFrame(None, sys.stdout) | |
1e4a197e | 231 | frame.Show(True) |
f6bcfd97 BP |
232 | app.MainLoop() |
233 | ||
234 | ||
235 | #--------------------------------------------------------------------------- | |
c368d904 RD |
236 | |
237 |