1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23 24 25 26 27 28 29 30 31 32 33 34 35 36 37 38 39 40 41 42 43 44 45 46 47 48 49 50 51 52 53 54 55 56 57 58 59 60 61 62 63 64 65 66 67 68 69 70 71 72 73 74 75 76 77 78 79 80 81 82 83 84 85 86 87 88 89 90 91 92 93 94 95 96 97 98 99 100 101 102 103 104 105 106 107 108 109 110 111 112 113 114 115 116 117 118 119 120 121 122 123 124 125 126 127 128 129 130 131 132 133 134 135 136 137 138 139 140 141 142 143 144 145 146 147 148 149 150 151 152 153 154 155 156 157 158 159 160 161 162 163 164 165 166 167 168 169 170 171 172 173
|
import sys
import wx
import wx.grid
class CharCellEditor (wx.grid.PyGridCellEditor):
def __init__(self, grid, attributes):
wx.grid.PyGridCellEditor.__init__(self)
self.grid = grid
self.attributes = attributes
def Create(self, parent, id, evtHandler):
self._tc = wx.TextCtrl(parent, id, "")
self._tc.SetInsertionPoint(0)
self.SetControl(self._tc)
if evtHandler:
self._tc.PushEventHandler(evtHandler)
def SetSize(self, rect):
self._tc.SetDimensions(rect.x, rect.y, rect.width+2, rect.height+2)
# def Show(self, show, attr):
# self.base_Show(show, attr)
def PaintBackground(self, rect, attr):
pass
def BeginEdit(self, row, col, grid):
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):
changed = False
val = self._tc.GetValue()
if val != self.startValue:
changed = True
grid.GetTable().SetValue(row, col, val)
self.startValue = ''
self._tc.SetValue('')
return changed
def Reset(self):
self._tc.SetValue(self.startValue)
self._tc.SetInsertionPointEnd()
def IsAcceptedKey(self, evt):
return (not (evt.ControlDown() or evt.AltDown()) and
evt.GetKeyCode() != wx.WXK_SHIFT)
def StartingKey(self, evt):
key = evt.GetKeyCode()
ch = None
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)
elif key < 256 and key >= 0 and chr(key):
ch = chr(key)
if not evt.ShiftDown():
ch = ch.lower()
if ch is not None:
self._tc.SetValue(ch)
self._tc.SetInsertionPointEnd()
else:
evt.Skip()
def StartingClick(self):
pass
# def Destroy(self):
# self.base_Destroy()
def Clone(self):
return CharCellEditor(self.grid, self.attributes)
class MyCellChoiceEditor (CharCellEditor):
def __init__(self, grid, attributes):
CharCellEditor.__init__(self, grid, attributes)
def Create(self, parent, id, evtHandler):
self._tc = wx.ComboBox(parent, id, "", choices=["un", "deux", "trois"])#, style=wx.CB_READONLY)
self.SetControl(self._tc)
if evtHandler:
self._tc.PushEventHandler(evtHandler)
def BeginEdit(self, row, col, grid):
self.startValue = grid.GetTable().GetValue(row, col)
self._tc.SetStringSelection(self.startValue)
self._tc.SetFocus()
def EndEdit(self, row, col, grid):
changed = False
val = self._tc.GetStringSelection()
if val != self.startValue:
changed = True
grid.GetTable().SetValue(row, col, "%s" % val) # update the table
self.startValue = ''
self._tc.SetSelection(0)
return changed
def Reset(self):
self._tc.SetSelection(self.startValue)
#self._tc.SetInsertionPointEnd()
def StartingKey(self, evt):
key = evt.GetKeyCode()
ch = None
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)
elif key < 256 and key >= 0 and chr(key):
ch = chr(key)
if not evt.ShiftDown():
ch = ch.lower()
if ch is not None:
self._tc.SetValue(ch)
self._tc.SetInsertionPointEnd()
else:
evt.Skip()
#---------------------------------------------------------------------------
class GridEditorTest(wx.grid.Grid):
def __init__(self, parent, log):
wx.grid.Grid.__init__(self, parent, -1)
self.log = log
self.CreateGrid(1, 1)
self.SetCellEditor(0, 0, MyCellChoiceEditor(self, None))
self.SetColSize(0, 150)
#---------------------------------------------------------------------------
class TestFrame(wx.Frame):
def __init__(self, parent, log):
wx.Frame.__init__(self, parent, -1, "Custom Grid Cell Editor Test",
size=(300, 100))
grid = GridEditorTest(self, log)
#---------------------------------------------------------------------------
if __name__ == '__main__':
app = wx.PySimpleApp()
frame = TestFrame(None, sys.stdout)
frame.Show(True)
app.MainLoop() |
Partager