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