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