2000-11-04 07:38:17 +00:00
|
|
|
|
2003-03-25 06:35:27 +00:00
|
|
|
import string
|
2003-12-09 01:23:28 +00:00
|
|
|
|
|
|
|
import wx
|
|
|
|
import wx.grid as gridlib
|
|
|
|
|
2000-11-04 07:38:17 +00:00
|
|
|
#---------------------------------------------------------------------------
|
2003-12-09 01:23:28 +00:00
|
|
|
class MyCellEditor(gridlib.PyGridCellEditor):
|
2000-11-04 07:38:17 +00:00
|
|
|
"""
|
|
|
|
This is a sample GridCellEditor that shows you how to make your own custom
|
|
|
|
grid editors. All the methods that can be overridden are show here. The
|
|
|
|
ones that must be overridden are marked with "*Must Override*" in the
|
|
|
|
docstring.
|
|
|
|
|
|
|
|
Notice that in order to call the base class version of these special
|
|
|
|
methods we use the method name preceded by "base_". This is because these
|
2004-01-13 03:17:17 +00:00
|
|
|
methods are "virtual" in C++ so if we try to call wx.GridCellEditor.Create
|
2000-11-04 07:38:17 +00:00
|
|
|
for example, then when the wxPython extension module tries to call
|
|
|
|
ptr->Create(...) then it actually calls the derived class version which
|
|
|
|
looks up the method in this class and calls it, causing a recursion loop.
|
|
|
|
If you don't understand any of this, don't worry, just call the "base_"
|
|
|
|
version instead.
|
|
|
|
"""
|
|
|
|
def __init__(self, log):
|
|
|
|
self.log = log
|
|
|
|
self.log.write("MyCellEditor ctor\n")
|
2003-12-09 01:23:28 +00:00
|
|
|
gridlib.PyGridCellEditor.__init__(self)
|
2000-11-04 07:38:17 +00:00
|
|
|
|
|
|
|
|
|
|
|
def Create(self, parent, id, evtHandler):
|
|
|
|
"""
|
2003-12-09 01:23:28 +00:00
|
|
|
Called to create the control, which must derive from wx.Control.
|
2000-11-04 07:38:17 +00:00
|
|
|
*Must Override*
|
|
|
|
"""
|
|
|
|
self.log.write("MyCellEditor: Create\n")
|
2003-12-09 01:23:28 +00:00
|
|
|
self._tc = wx.TextCtrl(parent, id, "")
|
2000-11-04 07:38:17 +00:00
|
|
|
self._tc.SetInsertionPoint(0)
|
|
|
|
self.SetControl(self._tc)
|
2003-12-09 01:23:28 +00:00
|
|
|
|
2000-11-04 07:38:17 +00:00
|
|
|
if evtHandler:
|
|
|
|
self._tc.PushEventHandler(evtHandler)
|
|
|
|
|
|
|
|
|
|
|
|
def SetSize(self, rect):
|
|
|
|
"""
|
|
|
|
Called to position/size the edit control within the cell rectangle.
|
|
|
|
If you don't fill the cell (the rect) then be sure to override
|
|
|
|
PaintBackground and do something meaningful there.
|
|
|
|
"""
|
|
|
|
self.log.write("MyCellEditor: SetSize %s\n" % rect)
|
2001-07-28 03:06:02 +00:00
|
|
|
self._tc.SetDimensions(rect.x, rect.y, rect.width+2, rect.height+2,
|
2003-12-09 01:23:28 +00:00
|
|
|
wx.SIZE_ALLOW_MINUS_ONE)
|
2000-11-04 07:38:17 +00:00
|
|
|
|
|
|
|
|
|
|
|
def Show(self, show, attr):
|
|
|
|
"""
|
|
|
|
Show or hide the edit control. You can use the attr (if not None)
|
|
|
|
to set colours or fonts for the control.
|
|
|
|
"""
|
|
|
|
self.log.write("MyCellEditor: Show(self, %s, %s)\n" % (show, attr))
|
|
|
|
self.base_Show(show, attr)
|
|
|
|
|
|
|
|
|
|
|
|
def PaintBackground(self, rect, attr):
|
|
|
|
"""
|
|
|
|
Draws the part of the cell not occupied by the edit control. The
|
|
|
|
base class version just fills it with background colour from the
|
|
|
|
attribute. In this class the edit control fills the whole cell so
|
|
|
|
don't do anything at all in order to reduce flicker.
|
|
|
|
"""
|
|
|
|
self.log.write("MyCellEditor: PaintBackground\n")
|
|
|
|
|
|
|
|
|
|
|
|
def BeginEdit(self, row, col, grid):
|
|
|
|
"""
|
|
|
|
Fetch the value from the table and prepare the edit control
|
|
|
|
to begin editing. Set the focus to the edit control.
|
|
|
|
*Must Override*
|
|
|
|
"""
|
|
|
|
self.log.write("MyCellEditor: BeginEdit (%d,%d)\n" % (row, col))
|
|
|
|
self.startValue = grid.GetTable().GetValue(row, col)
|
|
|
|
self._tc.SetValue(self.startValue)
|
|
|
|
self._tc.SetInsertionPointEnd()
|
|
|
|
self._tc.SetFocus()
|
|
|
|
|
|
|
|
# For this example, select the text
|
|
|
|
self._tc.SetSelection(0, self._tc.GetLastPosition())
|
|
|
|
|
|
|
|
|
|
|
|
def EndEdit(self, row, col, grid):
|
|
|
|
"""
|
2003-03-25 06:35:27 +00:00
|
|
|
Complete the editing of the current cell. Returns True if the value
|
2000-11-04 07:38:17 +00:00
|
|
|
has changed. If necessary, the control may be destroyed.
|
|
|
|
*Must Override*
|
|
|
|
"""
|
|
|
|
self.log.write("MyCellEditor: EndEdit (%d,%d)\n" % (row, col))
|
2003-03-25 06:35:27 +00:00
|
|
|
changed = False
|
2000-11-04 07:38:17 +00:00
|
|
|
|
|
|
|
val = self._tc.GetValue()
|
2003-12-09 01:23:28 +00:00
|
|
|
|
2000-11-04 07:38:17 +00:00
|
|
|
if val != self.startValue:
|
2003-03-25 06:35:27 +00:00
|
|
|
changed = True
|
2000-11-04 07:38:17 +00:00
|
|
|
grid.GetTable().SetValue(row, col, val) # update the table
|
|
|
|
|
|
|
|
self.startValue = ''
|
|
|
|
self._tc.SetValue('')
|
|
|
|
return changed
|
|
|
|
|
|
|
|
|
|
|
|
def Reset(self):
|
|
|
|
"""
|
|
|
|
Reset the value in the control back to its starting value.
|
|
|
|
*Must Override*
|
|
|
|
"""
|
|
|
|
self.log.write("MyCellEditor: Reset\n")
|
|
|
|
self._tc.SetValue(self.startValue)
|
|
|
|
self._tc.SetInsertionPointEnd()
|
|
|
|
|
|
|
|
|
|
|
|
def IsAcceptedKey(self, evt):
|
|
|
|
"""
|
2003-03-25 06:35:27 +00:00
|
|
|
Return True to allow the given key to start editing: the base class
|
2000-11-04 07:38:17 +00:00
|
|
|
version only checks that the event has no modifiers. F2 is special
|
|
|
|
and will always start the editor.
|
|
|
|
"""
|
|
|
|
self.log.write("MyCellEditor: IsAcceptedKey: %d\n" % (evt.GetKeyCode()))
|
|
|
|
|
|
|
|
## Oops, there's a bug here, we'll have to do it ourself..
|
|
|
|
##return self.base_IsAcceptedKey(evt)
|
|
|
|
|
2000-12-11 02:55:03 +00:00
|
|
|
return (not (evt.ControlDown() or evt.AltDown()) and
|
2003-12-09 01:23:28 +00:00
|
|
|
evt.GetKeyCode() != wx.WXK_SHIFT)
|
2000-11-04 07:38:17 +00:00
|
|
|
|
|
|
|
|
|
|
|
def StartingKey(self, evt):
|
|
|
|
"""
|
|
|
|
If the editor is enabled by pressing keys on the grid, this will be
|
|
|
|
called to let the editor do something about that first key if desired.
|
|
|
|
"""
|
|
|
|
self.log.write("MyCellEditor: StartingKey %d\n" % evt.GetKeyCode())
|
|
|
|
key = evt.GetKeyCode()
|
|
|
|
ch = None
|
2003-12-09 01:23:28 +00:00
|
|
|
if key in [ wx.WXK_NUMPAD0, wx.WXK_NUMPAD1, wx.WXK_NUMPAD2, wx.WXK_NUMPAD3,
|
|
|
|
wx.WXK_NUMPAD4, wx.WXK_NUMPAD5, wx.WXK_NUMPAD6, wx.WXK_NUMPAD7,
|
|
|
|
wx.WXK_NUMPAD8, wx.WXK_NUMPAD9
|
|
|
|
]:
|
|
|
|
|
|
|
|
ch = ch = chr(ord('0') + key - wx.WXK_NUMPAD0)
|
2000-11-04 07:38:17 +00:00
|
|
|
|
|
|
|
elif key < 256 and key >= 0 and chr(key) in string.printable:
|
|
|
|
ch = chr(key)
|
|
|
|
if not evt.ShiftDown():
|
2003-03-25 06:35:27 +00:00
|
|
|
ch = ch.lower()
|
2000-11-04 07:38:17 +00:00
|
|
|
|
|
|
|
if ch is not None:
|
|
|
|
# For this example, replace the text. Normally we would append it.
|
|
|
|
#self._tc.AppendText(ch)
|
|
|
|
self._tc.SetValue(ch)
|
2003-03-25 06:35:27 +00:00
|
|
|
self._tc.SetInsertionPointEnd()
|
2000-11-04 07:38:17 +00:00
|
|
|
else:
|
|
|
|
evt.Skip()
|
|
|
|
|
|
|
|
|
|
|
|
def StartingClick(self):
|
|
|
|
"""
|
|
|
|
If the editor is enabled by clicking on the cell, this method will be
|
|
|
|
called to allow the editor to simulate the click on the control if
|
|
|
|
needed.
|
|
|
|
"""
|
|
|
|
self.log.write("MyCellEditor: StartingClick\n")
|
|
|
|
|
|
|
|
|
|
|
|
def Destroy(self):
|
|
|
|
"""final cleanup"""
|
|
|
|
self.log.write("MyCellEditor: Destroy\n")
|
|
|
|
self.base_Destroy()
|
|
|
|
|
|
|
|
|
|
|
|
def Clone(self):
|
|
|
|
"""
|
|
|
|
Create a new object which is the copy of this one
|
|
|
|
*Must Override*
|
|
|
|
"""
|
|
|
|
self.log.write("MyCellEditor: Clone\n")
|
|
|
|
return MyCellEditor(self.log)
|
|
|
|
|
|
|
|
|
|
|
|
#---------------------------------------------------------------------------
|
2003-12-09 01:23:28 +00:00
|
|
|
class GridEditorTest(gridlib.Grid):
|
2000-11-04 07:38:17 +00:00
|
|
|
def __init__(self, parent, log):
|
2003-12-09 01:23:28 +00:00
|
|
|
gridlib.Grid.__init__(self, parent, -1)
|
2000-11-04 07:38:17 +00:00
|
|
|
self.log = log
|
|
|
|
|
|
|
|
self.CreateGrid(10, 3)
|
|
|
|
|
|
|
|
# Somebody changed the grid so the type registry takes precedence
|
|
|
|
# over the default attribute set for editors and renderers, so we
|
|
|
|
# have to set null handlers for the type registry before the
|
|
|
|
# default editor will get used otherwise...
|
|
|
|
#self.RegisterDataType(wxGRID_VALUE_STRING, None, None)
|
|
|
|
#self.SetDefaultEditor(MyCellEditor(self.log))
|
|
|
|
|
|
|
|
# Or we could just do it like this:
|
2003-12-09 01:23:28 +00:00
|
|
|
#self.RegisterDataType(wx.GRID_VALUE_STRING,
|
|
|
|
# wx.GridCellStringRenderer(),
|
2000-11-04 07:38:17 +00:00
|
|
|
# MyCellEditor(self.log))
|
2003-12-09 01:23:28 +00:00
|
|
|
# )
|
2000-11-04 07:38:17 +00:00
|
|
|
|
|
|
|
# but for this example, we'll just set the custom editor on one cell
|
|
|
|
self.SetCellEditor(1, 0, MyCellEditor(self.log))
|
|
|
|
self.SetCellValue(1, 0, "Try to edit this box")
|
|
|
|
|
|
|
|
# and on a column
|
2003-12-09 01:23:28 +00:00
|
|
|
attr = gridlib.GridCellAttr()
|
2000-11-04 07:38:17 +00:00
|
|
|
attr.SetEditor(MyCellEditor(self.log))
|
|
|
|
self.SetColAttr(2, attr)
|
|
|
|
self.SetCellValue(1, 2, "or any in this column")
|
|
|
|
|
|
|
|
self.SetColSize(0, 150)
|
|
|
|
self.SetColSize(1, 150)
|
|
|
|
self.SetColSize(2, 150)
|
|
|
|
|
2001-04-09 19:36:36 +00:00
|
|
|
|
2000-11-04 07:38:17 +00:00
|
|
|
#---------------------------------------------------------------------------
|
|
|
|
|
2003-12-09 01:23:28 +00:00
|
|
|
class TestFrame(wx.Frame):
|
2000-11-04 07:38:17 +00:00
|
|
|
def __init__(self, parent, log):
|
2003-12-09 01:23:28 +00:00
|
|
|
wx.Frame.__init__(self, parent, -1, "Custom Grid Cell Editor Test",
|
2000-11-04 07:38:17 +00:00
|
|
|
size=(640,480))
|
|
|
|
grid = GridEditorTest(self, log)
|
|
|
|
|
|
|
|
#---------------------------------------------------------------------------
|
|
|
|
|
|
|
|
if __name__ == '__main__':
|
|
|
|
import sys
|
2003-12-09 01:23:28 +00:00
|
|
|
app = wx.PySimpleApp()
|
2000-11-04 07:38:17 +00:00
|
|
|
frame = TestFrame(None, sys.stdout)
|
2003-03-25 06:35:27 +00:00
|
|
|
frame.Show(True)
|
2000-11-04 07:38:17 +00:00
|
|
|
app.MainLoop()
|
|
|
|
|
|
|
|
|