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