]> git.saurik.com Git - wxWidgets.git/blame - wxPython/samples/wxPIA_book/Chapter-14/grid_editor.py
fix building/running of tex2rtf
[wxWidgets.git] / wxPython / samples / wxPIA_book / Chapter-14 / grid_editor.py
CommitLineData
be05b434
RD
1import wx
2import wx.grid
3import string
4
5class UpCaseCellEditor(wx.grid.PyGridCellEditor):
6 def __init__(self):
7 wx.grid.PyGridCellEditor.__init__(self)
8
9 def Create(self, parent, id, evtHandler):
10 """
11 Called to create the control, which must derive from wx.Control.
12 *Must Override*
13 """
14 self._tc = wx.TextCtrl(parent, id, "")
15 self._tc.SetInsertionPoint(0)
16 self.SetControl(self._tc)
17
18 if evtHandler:
19 self._tc.PushEventHandler(evtHandler)
20
21 self._tc.Bind(wx.EVT_CHAR, self.OnChar)
22
23 def SetSize(self, rect):
24 """
25 Called to position/size the edit control within the cell rectangle.
26 If you don't fill the cell (the rect) then be sure to override
27 PaintBackground and do something meaningful there.
28 """
29 self._tc.SetDimensions(rect.x, rect.y, rect.width+2, rect.height+2,
30 wx.SIZE_ALLOW_MINUS_ONE)
31
32 def BeginEdit(self, row, col, grid):
33 """
34 Fetch the value from the table and prepare the edit control
35 to begin editing. Set the focus to the edit control.
36 *Must Override*
37 """
38 self.startValue = grid.GetTable().GetValue(row, col)
39 self._tc.SetValue(self.startValue)
40 self._tc.SetInsertionPointEnd()
41 self._tc.SetFocus()
42 self._tc.SetSelection(0, self._tc.GetLastPosition())
43
44 def EndEdit(self, row, col, grid):
45 """
46 Complete the editing of the current cell. Returns True if the value
47 has changed. If necessary, the control may be destroyed.
48 *Must Override*
49 """
50 changed = False
51
52 val = self._tc.GetValue()
53
54 if val != self.startValue:
55 changed = True
56 grid.GetTable().SetValue(row, col, val) # update the table
57
58 self.startValue = ''
59 self._tc.SetValue('')
60 return changed
61
62 def Reset(self):
63 """
64 Reset the value in the control back to its starting value.
65 *Must Override*
66 """
67 self._tc.SetValue(self.startValue)
68 self._tc.SetInsertionPointEnd()
69
70 def Clone(self):
71 """
72 Create a new object which is the copy of this one
73 *Must Override*
74 """
75 return UpCaseCellEditor()
76
77 def StartingKey(self, evt):
78 """
79 If the editor is enabled by pressing keys on the grid, this will be
80 called to let the editor do something about that first key if desired.
81 """
82 self.OnChar(evt)
83 if evt.GetSkipped():
84 self._tc.EmulateKeyPress(evt)
85
86 def OnChar(self, evt):
87 key = evt.GetKeyCode()
88 if key > 255:
89 evt.Skip()
90 return
91 char = chr(key)
92 if char in string.letters:
93 char = char.upper()
94 self._tc.WriteText(char)
95 else:
96 evt.Skip()
97
98class TestFrame(wx.Frame):
99 def __init__(self):
100 wx.Frame.__init__(self, None, title="Grid Editor",
101 size=(640,480))
102
103 grid = wx.grid.Grid(self)
104 grid.CreateGrid(50,50)
105 grid.SetDefaultEditor(UpCaseCellEditor())
106
107
108app = wx.PySimpleApp()
109frame = TestFrame()
110frame.Show()
111app.MainLoop()