]>
git.saurik.com Git - wxWidgets.git/blob - wxPython/demo/GridCustEditor.py
1 # 11/6/2003 - Jeff Grimmett (grimmtooth@softhome.net)
3 # Modified for wx namespace
9 import wx
.grid
as gridlib
11 #---------------------------------------------------------------------------
12 class MyCellEditor(gridlib
.PyGridCellEditor
):
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
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_"
28 def __init__(self
, log
):
30 self
.log
.write("MyCellEditor ctor\n")
31 gridlib
.PyGridCellEditor
.__init
__(self
)
34 def Create(self
, parent
, id, evtHandler
):
36 Called to create the control, which must derive from wx.Control.
39 self
.log
.write("MyCellEditor: Create\n")
40 self
._tc
= wx
.TextCtrl(parent
, id, "")
41 self
._tc
.SetInsertionPoint(0)
42 self
.SetControl(self
._tc
)
45 self
._tc
.PushEventHandler(evtHandler
)
48 def SetSize(self
, rect
):
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.
54 self
.log
.write("MyCellEditor: SetSize %s\n" % rect
)
55 self
._tc
.SetDimensions(rect
.x
, rect
.y
, rect
.width
+2, rect
.height
+2,
56 wx
.SIZE_ALLOW_MINUS_ONE
)
59 def Show(self
, show
, attr
):
61 Show or hide the edit control. You can use the attr (if not None)
62 to set colours or fonts for the control.
64 self
.log
.write("MyCellEditor: Show(self, %s, %s)\n" % (show
, attr
))
65 self
.base_Show(show
, attr
)
68 def PaintBackground(self
, rect
, attr
):
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.
75 self
.log
.write("MyCellEditor: PaintBackground\n")
78 def BeginEdit(self
, row
, col
, grid
):
80 Fetch the value from the table and prepare the edit control
81 to begin editing. Set the focus to the edit control.
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()
90 # For this example, select the text
91 self
._tc
.SetSelection(0, self
._tc
.GetLastPosition())
94 def EndEdit(self
, row
, col
, grid
):
96 Complete the editing of the current cell. Returns True if the value
97 has changed. If necessary, the control may be destroyed.
100 self
.log
.write("MyCellEditor: EndEdit (%d,%d)\n" % (row
, col
))
103 val
= self
._tc
.GetValue()
105 if val
!= self
.startValue
:
107 grid
.GetTable().SetValue(row
, col
, val
) # update the table
110 self
._tc
.SetValue('')
116 Reset the value in the control back to its starting value.
119 self
.log
.write("MyCellEditor: Reset\n")
120 self
._tc
.SetValue(self
.startValue
)
121 self
._tc
.SetInsertionPointEnd()
124 def IsAcceptedKey(self
, evt
):
126 Return True to allow the given key to start editing: the base class
127 version only checks that the event has no modifiers. F2 is special
128 and will always start the editor.
130 self
.log
.write("MyCellEditor: IsAcceptedKey: %d\n" % (evt
.GetKeyCode()))
132 ## Oops, there's a bug here, we'll have to do it ourself..
133 ##return self.base_IsAcceptedKey(evt)
135 return (not (evt
.ControlDown() or evt
.AltDown()) and
136 evt
.GetKeyCode() != wx
.WXK_SHIFT
)
139 def StartingKey(self
, evt
):
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.
144 self
.log
.write("MyCellEditor: StartingKey %d\n" % evt
.GetKeyCode())
145 key
= evt
.GetKeyCode()
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
152 ch
= ch
= chr(ord('0') + key
- wx
.WXK_NUMPAD0
)
154 elif key
< 256 and key
>= 0 and chr(key
) in string
.printable
:
156 if not evt
.ShiftDown():
160 # For this example, replace the text. Normally we would append it.
161 #self._tc.AppendText(ch)
162 self
._tc
.SetValue(ch
)
163 self
._tc
.SetInsertionPointEnd()
168 def StartingClick(self
):
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
174 self
.log
.write("MyCellEditor: StartingClick\n")
179 self
.log
.write("MyCellEditor: Destroy\n")
185 Create a new object which is the copy of this one
188 self
.log
.write("MyCellEditor: Clone\n")
189 return MyCellEditor(self
.log
)
192 #---------------------------------------------------------------------------
193 class GridEditorTest(gridlib
.Grid
):
194 def __init__(self
, parent
, log
):
195 gridlib
.Grid
.__init
__(self
, parent
, -1)
198 self
.CreateGrid(10, 3)
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))
207 # Or we could just do it like this:
208 #self.RegisterDataType(wx.GRID_VALUE_STRING,
209 # wx.GridCellStringRenderer(),
210 # MyCellEditor(self.log))
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")
218 attr
= gridlib
.GridCellAttr()
219 attr
.SetEditor(MyCellEditor(self
.log
))
220 self
.SetColAttr(2, attr
)
221 self
.SetCellValue(1, 2, "or any in this column")
223 self
.SetColSize(0, 150)
224 self
.SetColSize(1, 150)
225 self
.SetColSize(2, 150)
228 #---------------------------------------------------------------------------
230 class TestFrame(wx
.Frame
):
231 def __init__(self
, parent
, log
):
232 wx
.Frame
.__init
__(self
, parent
, -1, "Custom Grid Cell Editor Test",
234 grid
= GridEditorTest(self
, log
)
236 #---------------------------------------------------------------------------
238 if __name__
== '__main__':
240 app
= wx
.PySimpleApp()
241 frame
= TestFrame(None, sys
.stdout
)