112 lines
3.2 KiB
Python
112 lines
3.2 KiB
Python
|
import wx
|
||
|
import wx.grid
|
||
|
import string
|
||
|
|
||
|
class UpCaseCellEditor(wx.grid.PyGridCellEditor):
|
||
|
def __init__(self):
|
||
|
wx.grid.PyGridCellEditor.__init__(self)
|
||
|
|
||
|
def Create(self, parent, id, evtHandler):
|
||
|
"""
|
||
|
Called to create the control, which must derive from wx.Control.
|
||
|
*Must Override*
|
||
|
"""
|
||
|
self._tc = wx.TextCtrl(parent, id, "")
|
||
|
self._tc.SetInsertionPoint(0)
|
||
|
self.SetControl(self._tc)
|
||
|
|
||
|
if evtHandler:
|
||
|
self._tc.PushEventHandler(evtHandler)
|
||
|
|
||
|
self._tc.Bind(wx.EVT_CHAR, self.OnChar)
|
||
|
|
||
|
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._tc.SetDimensions(rect.x, rect.y, rect.width+2, rect.height+2,
|
||
|
wx.SIZE_ALLOW_MINUS_ONE)
|
||
|
|
||
|
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.startValue = grid.GetTable().GetValue(row, col)
|
||
|
self._tc.SetValue(self.startValue)
|
||
|
self._tc.SetInsertionPointEnd()
|
||
|
self._tc.SetFocus()
|
||
|
self._tc.SetSelection(0, self._tc.GetLastPosition())
|
||
|
|
||
|
def EndEdit(self, row, col, grid):
|
||
|
"""
|
||
|
Complete the editing of the current cell. Returns True if the value
|
||
|
has changed. If necessary, the control may be destroyed.
|
||
|
*Must Override*
|
||
|
"""
|
||
|
changed = False
|
||
|
|
||
|
val = self._tc.GetValue()
|
||
|
|
||
|
if val != self.startValue:
|
||
|
changed = True
|
||
|
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._tc.SetValue(self.startValue)
|
||
|
self._tc.SetInsertionPointEnd()
|
||
|
|
||
|
def Clone(self):
|
||
|
"""
|
||
|
Create a new object which is the copy of this one
|
||
|
*Must Override*
|
||
|
"""
|
||
|
return UpCaseCellEditor()
|
||
|
|
||
|
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.OnChar(evt)
|
||
|
if evt.GetSkipped():
|
||
|
self._tc.EmulateKeyPress(evt)
|
||
|
|
||
|
def OnChar(self, evt):
|
||
|
key = evt.GetKeyCode()
|
||
|
if key > 255:
|
||
|
evt.Skip()
|
||
|
return
|
||
|
char = chr(key)
|
||
|
if char in string.letters:
|
||
|
char = char.upper()
|
||
|
self._tc.WriteText(char)
|
||
|
else:
|
||
|
evt.Skip()
|
||
|
|
||
|
class TestFrame(wx.Frame):
|
||
|
def __init__(self):
|
||
|
wx.Frame.__init__(self, None, title="Grid Editor",
|
||
|
size=(640,480))
|
||
|
|
||
|
grid = wx.grid.Grid(self)
|
||
|
grid.CreateGrid(50,50)
|
||
|
grid.SetDefaultEditor(UpCaseCellEditor())
|
||
|
|
||
|
|
||
|
app = wx.PySimpleApp()
|
||
|
frame = TestFrame()
|
||
|
frame.Show()
|
||
|
app.MainLoop()
|