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