]>
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 | ||
1fded56b RD |
67 | editor = wxGridCellTextEditor() |
68 | editor.SetParameters('10') | |
69 | self.SetCellEditor(0, 4, editor) | |
70 | self.SetCellValue(0, 4, "Limited text") | |
71 | ||
1e4a197e | 72 | |
f6bcfd97 BP |
73 | # test all the events |
74 | EVT_GRID_CELL_LEFT_CLICK(self, self.OnCellLeftClick) | |
75 | EVT_GRID_CELL_RIGHT_CLICK(self, self.OnCellRightClick) | |
76 | EVT_GRID_CELL_LEFT_DCLICK(self, self.OnCellLeftDClick) | |
77 | EVT_GRID_CELL_RIGHT_DCLICK(self, self.OnCellRightDClick) | |
78 | ||
79 | EVT_GRID_LABEL_LEFT_CLICK(self, self.OnLabelLeftClick) | |
80 | EVT_GRID_LABEL_RIGHT_CLICK(self, self.OnLabelRightClick) | |
81 | EVT_GRID_LABEL_LEFT_DCLICK(self, self.OnLabelLeftDClick) | |
82 | EVT_GRID_LABEL_RIGHT_DCLICK(self, self.OnLabelRightDClick) | |
83 | ||
84 | EVT_GRID_ROW_SIZE(self, self.OnRowSize) | |
85 | EVT_GRID_COL_SIZE(self, self.OnColSize) | |
86 | ||
87 | EVT_GRID_RANGE_SELECT(self, self.OnRangeSelect) | |
88 | EVT_GRID_CELL_CHANGE(self, self.OnCellChange) | |
89 | EVT_GRID_SELECT_CELL(self, self.OnSelectCell) | |
90 | ||
91 | EVT_GRID_EDITOR_SHOWN(self, self.OnEditorShown) | |
92 | EVT_GRID_EDITOR_HIDDEN(self, self.OnEditorHidden) | |
bf7945ce | 93 | EVT_GRID_EDITOR_CREATED(self, self.OnEditorCreated) |
f6bcfd97 BP |
94 | |
95 | ||
19a97bd6 | 96 | |
f6bcfd97 BP |
97 | def OnCellLeftClick(self, evt): |
98 | self.log.write("OnCellLeftClick: (%d,%d) %s\n" % | |
99 | (evt.GetRow(), evt.GetCol(), evt.GetPosition())) | |
100 | evt.Skip() | |
101 | ||
102 | def OnCellRightClick(self, evt): | |
103 | self.log.write("OnCellRightClick: (%d,%d) %s\n" % | |
104 | (evt.GetRow(), evt.GetCol(), evt.GetPosition())) | |
105 | evt.Skip() | |
106 | ||
107 | def OnCellLeftDClick(self, evt): | |
108 | self.log.write("OnCellLeftDClick: (%d,%d) %s\n" % | |
109 | (evt.GetRow(), evt.GetCol(), evt.GetPosition())) | |
110 | evt.Skip() | |
111 | ||
112 | def OnCellRightDClick(self, evt): | |
113 | self.log.write("OnCellRightDClick: (%d,%d) %s\n" % | |
114 | (evt.GetRow(), evt.GetCol(), evt.GetPosition())) | |
115 | evt.Skip() | |
116 | ||
117 | def OnLabelLeftClick(self, evt): | |
118 | self.log.write("OnLabelLeftClick: (%d,%d) %s\n" % | |
119 | (evt.GetRow(), evt.GetCol(), evt.GetPosition())) | |
120 | evt.Skip() | |
121 | ||
122 | def OnLabelRightClick(self, evt): | |
123 | self.log.write("OnLabelRightClick: (%d,%d) %s\n" % | |
124 | (evt.GetRow(), evt.GetCol(), evt.GetPosition())) | |
125 | evt.Skip() | |
126 | ||
127 | def OnLabelLeftDClick(self, evt): | |
128 | self.log.write("OnLabelLeftDClick: (%d,%d) %s\n" % | |
129 | (evt.GetRow(), evt.GetCol(), evt.GetPosition())) | |
130 | evt.Skip() | |
131 | ||
132 | def OnLabelRightDClick(self, evt): | |
133 | self.log.write("OnLabelRightDClick: (%d,%d) %s\n" % | |
134 | (evt.GetRow(), evt.GetCol(), evt.GetPosition())) | |
135 | evt.Skip() | |
136 | ||
137 | ||
138 | def OnRowSize(self, evt): | |
139 | self.log.write("OnRowSize: row %d, %s\n" % | |
140 | (evt.GetRowOrCol(), evt.GetPosition())) | |
141 | evt.Skip() | |
142 | ||
143 | def OnColSize(self, evt): | |
144 | self.log.write("OnColSize: col %d, %s\n" % | |
145 | (evt.GetRowOrCol(), evt.GetPosition())) | |
146 | evt.Skip() | |
147 | ||
148 | def OnRangeSelect(self, evt): | |
149 | if evt.Selecting(): | |
150 | self.log.write("OnRangeSelect: top-left %s, bottom-right %s\n" % | |
151 | (evt.GetTopLeftCoords(), evt.GetBottomRightCoords())) | |
152 | evt.Skip() | |
153 | ||
c368d904 | 154 | |
f6bcfd97 BP |
155 | def OnCellChange(self, evt): |
156 | self.log.write("OnCellChange: (%d,%d) %s\n" % | |
157 | (evt.GetRow(), evt.GetCol(), evt.GetPosition())) | |
c368d904 RD |
158 | |
159 | # Show how to stay in a cell that has bad data. We can't just | |
160 | # call SetGridCursor here since we are nested inside one so it | |
161 | # won't have any effect. Instead, set coordinants to move to in | |
162 | # idle time. | |
163 | value = self.GetCellValue(evt.GetRow(), evt.GetCol()) | |
164 | if value == 'no good': | |
165 | self.moveTo = evt.GetRow(), evt.GetCol() | |
166 | ||
d56cebe7 | 167 | |
c368d904 RD |
168 | def OnIdle(self, evt): |
169 | if self.moveTo != None: | |
170 | self.SetGridCursor(self.moveTo[0], self.moveTo[1]) | |
171 | self.moveTo = None | |
d56cebe7 | 172 | evt.Skip() |
c368d904 | 173 | |
f6bcfd97 BP |
174 | |
175 | def OnSelectCell(self, evt): | |
176 | self.log.write("OnSelectCell: (%d,%d) %s\n" % | |
177 | (evt.GetRow(), evt.GetCol(), evt.GetPosition())) | |
c368d904 RD |
178 | |
179 | # Another way to stay in a cell that has a bad value... | |
180 | row = self.GetGridCursorRow() | |
181 | col = self.GetGridCursorCol() | |
182 | if self.IsCellEditControlEnabled(): | |
183 | self.HideCellEditControl() | |
184 | self.DisableCellEditControl() | |
185 | value = self.GetCellValue(row, col) | |
186 | if value == 'no good 2': | |
187 | return # cancels the cell selection | |
d56cebe7 | 188 | evt.Skip() |
c368d904 | 189 | |
f6bcfd97 BP |
190 | |
191 | def OnEditorShown(self, evt): | |
ab11ebfa RD |
192 | if evt.GetRow() == 6 and evt.GetCol() == 3 and \ |
193 | wxMessageBox("Are you sure you wish to edit this cell?", | |
194 | "Checking", wxYES_NO) == wxNO: | |
195 | evt.Veto() | |
196 | return | |
197 | ||
f6bcfd97 BP |
198 | self.log.write("OnEditorShown: (%d,%d) %s\n" % |
199 | (evt.GetRow(), evt.GetCol(), evt.GetPosition())) | |
200 | evt.Skip() | |
201 | ||
ab11ebfa | 202 | |
f6bcfd97 | 203 | def OnEditorHidden(self, evt): |
ab11ebfa RD |
204 | if evt.GetRow() == 6 and evt.GetCol() == 3 and \ |
205 | wxMessageBox("Are you sure you wish to finish editing this cell?", | |
206 | "Checking", wxYES_NO) == wxNO: | |
207 | evt.Veto() | |
208 | return | |
209 | ||
f6bcfd97 BP |
210 | self.log.write("OnEditorHidden: (%d,%d) %s\n" % |
211 | (evt.GetRow(), evt.GetCol(), evt.GetPosition())) | |
212 | evt.Skip() | |
213 | ||
ab11ebfa | 214 | |
bf7945ce RD |
215 | def OnEditorCreated(self, evt): |
216 | self.log.write("OnEditorCreated: (%d, %d) %s\n" % | |
217 | (evt.GetRow(), evt.GetCol(), evt.GetControl())) | |
218 | ||
219 | ||
f6bcfd97 BP |
220 | |
221 | #--------------------------------------------------------------------------- | |
222 | ||
223 | class TestFrame(wxFrame): | |
224 | def __init__(self, parent, log): | |
225 | wxFrame.__init__(self, parent, -1, "Simple Grid Demo", size=(640,480)) | |
19a97bd6 | 226 | grid = SimpleGrid(self, log) |
53fe40ba | 227 | |
f6bcfd97 BP |
228 | |
229 | ||
230 | #--------------------------------------------------------------------------- | |
231 | ||
232 | if __name__ == '__main__': | |
233 | import sys | |
234 | app = wxPySimpleApp() | |
235 | frame = TestFrame(None, sys.stdout) | |
1e4a197e | 236 | frame.Show(True) |
f6bcfd97 BP |
237 | app.MainLoop() |
238 | ||
239 | ||
240 | #--------------------------------------------------------------------------- | |
c368d904 RD |
241 | |
242 |