]>
Commit | Line | Data |
---|---|---|
1 | ||
2 | import string | |
3 | ||
4 | import wx | |
5 | import wx.grid as gridlib | |
6 | ||
7 | #--------------------------------------------------------------------------- | |
8 | class MyCellEditor(gridlib.PyGridCellEditor): | |
9 | """ | |
10 | This is a sample GridCellEditor that shows you how to make your own custom | |
11 | grid editors. All the methods that can be overridden are shown here. The | |
12 | ones that must be overridden are marked with "*Must Override*" in the | |
13 | docstring. | |
14 | """ | |
15 | def __init__(self, log): | |
16 | self.log = log | |
17 | self.log.write("MyCellEditor ctor\n") | |
18 | gridlib.PyGridCellEditor.__init__(self) | |
19 | ||
20 | ||
21 | def Create(self, parent, id, evtHandler): | |
22 | """ | |
23 | Called to create the control, which must derive from wx.Control. | |
24 | *Must Override* | |
25 | """ | |
26 | self.log.write("MyCellEditor: Create\n") | |
27 | self._tc = wx.TextCtrl(parent, id, "") | |
28 | self._tc.SetInsertionPoint(0) | |
29 | self.SetControl(self._tc) | |
30 | ||
31 | if evtHandler: | |
32 | self._tc.PushEventHandler(evtHandler) | |
33 | ||
34 | ||
35 | def SetSize(self, rect): | |
36 | """ | |
37 | Called to position/size the edit control within the cell rectangle. | |
38 | If you don't fill the cell (the rect) then be sure to override | |
39 | PaintBackground and do something meaningful there. | |
40 | """ | |
41 | self.log.write("MyCellEditor: SetSize %s\n" % rect) | |
42 | self._tc.SetDimensions(rect.x, rect.y, rect.width+2, rect.height+2, | |
43 | wx.SIZE_ALLOW_MINUS_ONE) | |
44 | ||
45 | ||
46 | def Show(self, show, attr): | |
47 | """ | |
48 | Show or hide the edit control. You can use the attr (if not None) | |
49 | to set colours or fonts for the control. | |
50 | """ | |
51 | self.log.write("MyCellEditor: Show(self, %s, %s)\n" % (show, attr)) | |
52 | super(MyCellEditor, self).Show(show, attr) | |
53 | ||
54 | ||
55 | def PaintBackground(self, rect, attr): | |
56 | """ | |
57 | Draws the part of the cell not occupied by the edit control. The | |
58 | base class version just fills it with background colour from the | |
59 | attribute. In this class the edit control fills the whole cell so | |
60 | don't do anything at all in order to reduce flicker. | |
61 | """ | |
62 | self.log.write("MyCellEditor: PaintBackground\n") | |
63 | ||
64 | ||
65 | def BeginEdit(self, row, col, grid): | |
66 | """ | |
67 | Fetch the value from the table and prepare the edit control | |
68 | to begin editing. Set the focus to the edit control. | |
69 | *Must Override* | |
70 | """ | |
71 | self.log.write("MyCellEditor: BeginEdit (%d,%d)\n" % (row, col)) | |
72 | self.startValue = grid.GetTable().GetValue(row, col) | |
73 | self._tc.SetValue(self.startValue) | |
74 | self._tc.SetInsertionPointEnd() | |
75 | self._tc.SetFocus() | |
76 | ||
77 | # For this example, select the text | |
78 | self._tc.SetSelection(0, self._tc.GetLastPosition()) | |
79 | ||
80 | ||
81 | def EndEdit(self, row, col, grid): | |
82 | """ | |
83 | Complete the editing of the current cell. Returns True if the value | |
84 | has changed. If necessary, the control may be destroyed. | |
85 | *Must Override* | |
86 | """ | |
87 | self.log.write("MyCellEditor: EndEdit (%d,%d)\n" % (row, col)) | |
88 | changed = False | |
89 | ||
90 | val = self._tc.GetValue() | |
91 | ||
92 | if val != self.startValue: | |
93 | changed = True | |
94 | grid.GetTable().SetValue(row, col, val) # update the table | |
95 | ||
96 | self.startValue = '' | |
97 | self._tc.SetValue('') | |
98 | return changed | |
99 | ||
100 | ||
101 | def Reset(self): | |
102 | """ | |
103 | Reset the value in the control back to its starting value. | |
104 | *Must Override* | |
105 | """ | |
106 | self.log.write("MyCellEditor: Reset\n") | |
107 | self._tc.SetValue(self.startValue) | |
108 | self._tc.SetInsertionPointEnd() | |
109 | ||
110 | ||
111 | def IsAcceptedKey(self, evt): | |
112 | """ | |
113 | Return True to allow the given key to start editing: the base class | |
114 | version only checks that the event has no modifiers. F2 is special | |
115 | and will always start the editor. | |
116 | """ | |
117 | self.log.write("MyCellEditor: IsAcceptedKey: %d\n" % (evt.GetKeyCode())) | |
118 | ||
119 | ## We can ask the base class to do it | |
120 | #return super(MyCellEditor, self).IsAcceptedKey(evt) | |
121 | ||
122 | # or do it ourselves | |
123 | return (not (evt.ControlDown() or evt.AltDown()) and | |
124 | evt.GetKeyCode() != wx.WXK_SHIFT) | |
125 | ||
126 | ||
127 | def StartingKey(self, evt): | |
128 | """ | |
129 | If the editor is enabled by pressing keys on the grid, this will be | |
130 | called to let the editor do something about that first key if desired. | |
131 | """ | |
132 | self.log.write("MyCellEditor: StartingKey %d\n" % evt.GetKeyCode()) | |
133 | key = evt.GetKeyCode() | |
134 | ch = None | |
135 | if key in [ wx.WXK_NUMPAD0, wx.WXK_NUMPAD1, wx.WXK_NUMPAD2, wx.WXK_NUMPAD3, | |
136 | wx.WXK_NUMPAD4, wx.WXK_NUMPAD5, wx.WXK_NUMPAD6, wx.WXK_NUMPAD7, | |
137 | wx.WXK_NUMPAD8, wx.WXK_NUMPAD9 | |
138 | ]: | |
139 | ||
140 | ch = ch = chr(ord('0') + key - wx.WXK_NUMPAD0) | |
141 | ||
142 | elif key < 256 and key >= 0 and chr(key) in string.printable: | |
143 | ch = chr(key) | |
144 | ||
145 | if ch is not None: | |
146 | # For this example, replace the text. Normally we would append it. | |
147 | #self._tc.AppendText(ch) | |
148 | self._tc.SetValue(ch) | |
149 | self._tc.SetInsertionPointEnd() | |
150 | else: | |
151 | evt.Skip() | |
152 | ||
153 | ||
154 | def StartingClick(self): | |
155 | """ | |
156 | If the editor is enabled by clicking on the cell, this method will be | |
157 | called to allow the editor to simulate the click on the control if | |
158 | needed. | |
159 | """ | |
160 | self.log.write("MyCellEditor: StartingClick\n") | |
161 | ||
162 | ||
163 | def Destroy(self): | |
164 | """final cleanup""" | |
165 | self.log.write("MyCellEditor: Destroy\n") | |
166 | super(MyCellEditor, self).Destroy() | |
167 | ||
168 | ||
169 | def Clone(self): | |
170 | """ | |
171 | Create a new object which is the copy of this one | |
172 | *Must Override* | |
173 | """ | |
174 | self.log.write("MyCellEditor: Clone\n") | |
175 | return MyCellEditor(self.log) | |
176 | ||
177 | ||
178 | #--------------------------------------------------------------------------- | |
179 | class GridEditorTest(gridlib.Grid): | |
180 | def __init__(self, parent, log): | |
181 | gridlib.Grid.__init__(self, parent, -1) | |
182 | self.log = log | |
183 | ||
184 | self.CreateGrid(10, 3) | |
185 | ||
186 | # Somebody changed the grid so the type registry takes precedence | |
187 | # over the default attribute set for editors and renderers, so we | |
188 | # have to set null handlers for the type registry before the | |
189 | # default editor will get used otherwise... | |
190 | #self.RegisterDataType(wxGRID_VALUE_STRING, None, None) | |
191 | #self.SetDefaultEditor(MyCellEditor(self.log)) | |
192 | ||
193 | # Or we could just do it like this: | |
194 | #self.RegisterDataType(wx.GRID_VALUE_STRING, | |
195 | # wx.GridCellStringRenderer(), | |
196 | # MyCellEditor(self.log)) | |
197 | # ) | |
198 | ||
199 | # but for this example, we'll just set the custom editor on one cell | |
200 | self.SetCellEditor(1, 0, MyCellEditor(self.log)) | |
201 | self.SetCellValue(1, 0, "Try to edit this box") | |
202 | ||
203 | # and on a column | |
204 | attr = gridlib.GridCellAttr() | |
205 | attr.SetEditor(MyCellEditor(self.log)) | |
206 | self.SetColAttr(2, attr) | |
207 | self.SetCellValue(1, 2, "or any in this column") | |
208 | ||
209 | self.SetColSize(0, 150) | |
210 | self.SetColSize(1, 150) | |
211 | self.SetColSize(2, 150) | |
212 | ||
213 | ||
214 | #--------------------------------------------------------------------------- | |
215 | ||
216 | class TestFrame(wx.Frame): | |
217 | def __init__(self, parent, log): | |
218 | wx.Frame.__init__(self, parent, -1, "Custom Grid Cell Editor Test", | |
219 | size=(640,480)) | |
220 | grid = GridEditorTest(self, log) | |
221 | ||
222 | #--------------------------------------------------------------------------- | |
223 | ||
224 | if __name__ == '__main__': | |
225 | import sys | |
226 | app = wx.PySimpleApp() | |
227 | frame = TestFrame(None, sys.stdout) | |
228 | frame.Show(True) | |
229 | app.MainLoop() | |
230 | ||
231 |