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