]>
Commit | Line | Data |
---|---|---|
1 | from wxPython.wx import * | |
2 | from wxPython.grid import * | |
3 | from wxPython.lib.mixins.grid import wxGridAutoEditMixin | |
4 | ||
5 | #--------------------------------------------------------------------------- | |
6 | ||
7 | class SimpleGrid(wxGrid): ##, wxGridAutoEditMixin): | |
8 | def __init__(self, parent, log): | |
9 | wxGrid.__init__(self, parent, -1) | |
10 | ##wxGridAutoEditMixin.__init__(self) | |
11 | self.log = log | |
12 | self.moveTo = None | |
13 | ||
14 | EVT_IDLE(self, self.OnIdle) | |
15 | ||
16 | self.CreateGrid(25, 25) #, wxGrid.wxGridSelectRows) | |
17 | ##self.EnableEditing(False) | |
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") | |
25 | self.SetCellValue(3, 3, "This cell is read-only") | |
26 | self.SetCellFont(0, 0, wxFont(12, wxROMAN, wxITALIC, wxNORMAL)) | |
27 | self.SetCellTextColour(1, 1, wxRED) | |
28 | self.SetCellBackgroundColour(2, 2, wxCYAN) | |
29 | self.SetReadOnly(3, 3, True) | |
30 | ||
31 | self.SetCellEditor(5, 0, wxGridCellNumberEditor(1,1000)) | |
32 | self.SetCellValue(5, 0, "123") | |
33 | self.SetCellEditor(6, 0, wxGridCellFloatEditor()) | |
34 | self.SetCellValue(6, 0, "123.34") | |
35 | self.SetCellEditor(7, 0, wxGridCellNumberEditor()) | |
36 | ||
37 | self.SetCellValue(6, 3, "You can veto editing this cell") | |
38 | ||
39 | #self.SetRowLabelSize(0) | |
40 | #self.SetColLabelSize(0) | |
41 | ||
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 | ||
52 | self.SetColLabelValue(0, "Custom") | |
53 | self.SetColLabelValue(1, "column") | |
54 | self.SetColLabelValue(2, "labels") | |
55 | ||
56 | self.SetColLabelAlignment(wxALIGN_LEFT, wxALIGN_BOTTOM) | |
57 | ||
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 | ||
69 | editor = wxGridCellTextEditor() | |
70 | editor.SetParameters('10') | |
71 | self.SetCellEditor(0, 4, editor) | |
72 | self.SetCellValue(0, 4, "Limited text") | |
73 | ||
74 | ||
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) | |
95 | EVT_GRID_EDITOR_CREATED(self, self.OnEditorCreated) | |
96 | ||
97 | ||
98 | ||
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 | ||
156 | ||
157 | def OnCellChange(self, evt): | |
158 | self.log.write("OnCellChange: (%d,%d) %s\n" % | |
159 | (evt.GetRow(), evt.GetCol(), evt.GetPosition())) | |
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 | |
163 | # won't have any effect. Instead, set coordinates to move to in | |
164 | # idle time. | |
165 | value = self.GetCellValue(evt.GetRow(), evt.GetCol()) | |
166 | if value == 'no good': | |
167 | self.moveTo = evt.GetRow(), evt.GetCol() | |
168 | ||
169 | ||
170 | def OnIdle(self, evt): | |
171 | if self.moveTo != None: | |
172 | self.SetGridCursor(self.moveTo[0], self.moveTo[1]) | |
173 | self.moveTo = None | |
174 | evt.Skip() | |
175 | ||
176 | ||
177 | def OnSelectCell(self, evt): | |
178 | self.log.write("OnSelectCell: (%d,%d) %s\n" % | |
179 | (evt.GetRow(), evt.GetCol(), evt.GetPosition())) | |
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 | |
190 | evt.Skip() | |
191 | ||
192 | ||
193 | def OnEditorShown(self, evt): | |
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 | ||
200 | self.log.write("OnEditorShown: (%d,%d) %s\n" % | |
201 | (evt.GetRow(), evt.GetCol(), evt.GetPosition())) | |
202 | evt.Skip() | |
203 | ||
204 | ||
205 | def OnEditorHidden(self, evt): | |
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 | ||
212 | self.log.write("OnEditorHidden: (%d,%d) %s\n" % | |
213 | (evt.GetRow(), evt.GetCol(), evt.GetPosition())) | |
214 | evt.Skip() | |
215 | ||
216 | ||
217 | def OnEditorCreated(self, evt): | |
218 | self.log.write("OnEditorCreated: (%d, %d) %s\n" % | |
219 | (evt.GetRow(), evt.GetCol(), evt.GetControl())) | |
220 | ||
221 | ||
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)) | |
228 | grid = SimpleGrid(self, log) | |
229 | ||
230 | ||
231 | ||
232 | #--------------------------------------------------------------------------- | |
233 | ||
234 | if __name__ == '__main__': | |
235 | import sys | |
236 | app = wxPySimpleApp() | |
237 | frame = TestFrame(None, sys.stdout) | |
238 | frame.Show(True) | |
239 | app.MainLoop() | |
240 | ||
241 | ||
242 | #--------------------------------------------------------------------------- | |
243 | ||
244 |