]> git.saurik.com Git - wxWidgets.git/blame - wxPython/demo/GridCustEditor.py
add support for wxCONTROL_ISDEFAULT to wxRendererMSW::DrawPushButton() (modified...
[wxWidgets.git] / wxPython / demo / GridCustEditor.py
CommitLineData
83625579 1
1e4a197e 2import string
8fa876ca
RD
3
4import wx
5import wx.grid as gridlib
6
83625579 7#---------------------------------------------------------------------------
8fa876ca 8class MyCellEditor(gridlib.PyGridCellEditor):
83625579
RD
9 """
10 This is a sample GridCellEditor that shows you how to make your own custom
77bfc423 11 grid editors. All the methods that can be overridden are shown here. The
83625579
RD
12 ones that must be overridden are marked with "*Must Override*" in the
13 docstring.
83625579
RD
14 """
15 def __init__(self, log):
16 self.log = log
17 self.log.write("MyCellEditor ctor\n")
8fa876ca 18 gridlib.PyGridCellEditor.__init__(self)
83625579
RD
19
20
21 def Create(self, parent, id, evtHandler):
22 """
8fa876ca 23 Called to create the control, which must derive from wx.Control.
83625579
RD
24 *Must Override*
25 """
26 self.log.write("MyCellEditor: Create\n")
8fa876ca 27 self._tc = wx.TextCtrl(parent, id, "")
83625579
RD
28 self._tc.SetInsertionPoint(0)
29 self.SetControl(self._tc)
8fa876ca 30
83625579
RD
31 if evtHandler:
32 self._tc.PushEventHandler(evtHandler)
33
34
35 def SetSize(self, rect):
36 """
37 Called to position/size the edit control within the cell rectangle.
38 If you don't fill the cell (the rect) then be sure to override
39 PaintBackground and do something meaningful there.
40 """
41 self.log.write("MyCellEditor: SetSize %s\n" % rect)
2f9be787 42 self._tc.SetDimensions(rect.x, rect.y, rect.width+2, rect.height+2,
8fa876ca 43 wx.SIZE_ALLOW_MINUS_ONE)
83625579
RD
44
45
46 def Show(self, show, attr):
47 """
48 Show or hide the edit control. You can use the attr (if not None)
49 to set colours or fonts for the control.
50 """
51 self.log.write("MyCellEditor: Show(self, %s, %s)\n" % (show, attr))
a7a01418 52 super(MyCellEditor, self).Show(show, attr)
83625579
RD
53
54
55 def PaintBackground(self, rect, attr):
56 """
57 Draws the part of the cell not occupied by the edit control. The
58 base class version just fills it with background colour from the
59 attribute. In this class the edit control fills the whole cell so
60 don't do anything at all in order to reduce flicker.
61 """
62 self.log.write("MyCellEditor: PaintBackground\n")
63
64
65 def BeginEdit(self, row, col, grid):
66 """
67 Fetch the value from the table and prepare the edit control
68 to begin editing. Set the focus to the edit control.
69 *Must Override*
70 """
71 self.log.write("MyCellEditor: BeginEdit (%d,%d)\n" % (row, col))
72 self.startValue = grid.GetTable().GetValue(row, col)
73 self._tc.SetValue(self.startValue)
74 self._tc.SetInsertionPointEnd()
75 self._tc.SetFocus()
76
77 # For this example, select the text
78 self._tc.SetSelection(0, self._tc.GetLastPosition())
79
80
81 def EndEdit(self, row, col, grid):
82 """
1e4a197e 83 Complete the editing of the current cell. Returns True if the value
83625579
RD
84 has changed. If necessary, the control may be destroyed.
85 *Must Override*
86 """
87 self.log.write("MyCellEditor: EndEdit (%d,%d)\n" % (row, col))
1e4a197e 88 changed = False
83625579
RD
89
90 val = self._tc.GetValue()
8fa876ca 91
83625579 92 if val != self.startValue:
1e4a197e 93 changed = True
83625579
RD
94 grid.GetTable().SetValue(row, col, val) # update the table
95
96 self.startValue = ''
97 self._tc.SetValue('')
98 return changed
99
100
101 def Reset(self):
102 """
103 Reset the value in the control back to its starting value.
104 *Must Override*
105 """
106 self.log.write("MyCellEditor: Reset\n")
107 self._tc.SetValue(self.startValue)
108 self._tc.SetInsertionPointEnd()
109
110
111 def IsAcceptedKey(self, evt):
112 """
1e4a197e 113 Return True to allow the given key to start editing: the base class
83625579
RD
114 version only checks that the event has no modifiers. F2 is special
115 and will always start the editor.
116 """
117 self.log.write("MyCellEditor: IsAcceptedKey: %d\n" % (evt.GetKeyCode()))
118
80445715 119 ## We can ask the base class to do it
a7a01418 120 #return super(MyCellEditor, self).IsAcceptedKey(evt)
83625579 121
80445715 122 # or do it ourselves
d3f17510 123 return (not (evt.ControlDown() or evt.AltDown()) and
8fa876ca 124 evt.GetKeyCode() != wx.WXK_SHIFT)
83625579
RD
125
126
127 def StartingKey(self, evt):
128 """
129 If the editor is enabled by pressing keys on the grid, this will be
130 called to let the editor do something about that first key if desired.
131 """
132 self.log.write("MyCellEditor: StartingKey %d\n" % evt.GetKeyCode())
133 key = evt.GetKeyCode()
134 ch = None
8fa876ca
RD
135 if key in [ wx.WXK_NUMPAD0, wx.WXK_NUMPAD1, wx.WXK_NUMPAD2, wx.WXK_NUMPAD3,
136 wx.WXK_NUMPAD4, wx.WXK_NUMPAD5, wx.WXK_NUMPAD6, wx.WXK_NUMPAD7,
137 wx.WXK_NUMPAD8, wx.WXK_NUMPAD9
138 ]:
139
140 ch = ch = chr(ord('0') + key - wx.WXK_NUMPAD0)
83625579
RD
141
142 elif key < 256 and key >= 0 and chr(key) in string.printable:
143 ch = chr(key)
83625579
RD
144
145 if ch is not None:
146 # For this example, replace the text. Normally we would append it.
147 #self._tc.AppendText(ch)
148 self._tc.SetValue(ch)
1e4a197e 149 self._tc.SetInsertionPointEnd()
83625579
RD
150 else:
151 evt.Skip()
152
153
154 def StartingClick(self):
155 """
156 If the editor is enabled by clicking on the cell, this method will be
157 called to allow the editor to simulate the click on the control if
158 needed.
159 """
160 self.log.write("MyCellEditor: StartingClick\n")
161
162
163 def Destroy(self):
164 """final cleanup"""
165 self.log.write("MyCellEditor: Destroy\n")
a7a01418 166 super(MyCellEditor, self).Destroy()
83625579
RD
167
168
169 def Clone(self):
170 """
171 Create a new object which is the copy of this one
172 *Must Override*
173 """
174 self.log.write("MyCellEditor: Clone\n")
175 return MyCellEditor(self.log)
176
177
178#---------------------------------------------------------------------------
8fa876ca 179class GridEditorTest(gridlib.Grid):
83625579 180 def __init__(self, parent, log):
8fa876ca 181 gridlib.Grid.__init__(self, parent, -1)
83625579
RD
182 self.log = log
183
184 self.CreateGrid(10, 3)
185
186 # Somebody changed the grid so the type registry takes precedence
187 # over the default attribute set for editors and renderers, so we
188 # have to set null handlers for the type registry before the
189 # default editor will get used otherwise...
190 #self.RegisterDataType(wxGRID_VALUE_STRING, None, None)
191 #self.SetDefaultEditor(MyCellEditor(self.log))
192
193 # Or we could just do it like this:
8fa876ca
RD
194 #self.RegisterDataType(wx.GRID_VALUE_STRING,
195 # wx.GridCellStringRenderer(),
83625579 196 # MyCellEditor(self.log))
8fa876ca 197 # )
83625579
RD
198
199 # but for this example, we'll just set the custom editor on one cell
200 self.SetCellEditor(1, 0, MyCellEditor(self.log))
201 self.SetCellValue(1, 0, "Try to edit this box")
202
203 # and on a column
8fa876ca 204 attr = gridlib.GridCellAttr()
83625579
RD
205 attr.SetEditor(MyCellEditor(self.log))
206 self.SetColAttr(2, attr)
207 self.SetCellValue(1, 2, "or any in this column")
208
209 self.SetColSize(0, 150)
210 self.SetColSize(1, 150)
211 self.SetColSize(2, 150)
212
96bfd053 213
83625579
RD
214#---------------------------------------------------------------------------
215
8fa876ca 216class TestFrame(wx.Frame):
83625579 217 def __init__(self, parent, log):
8fa876ca 218 wx.Frame.__init__(self, parent, -1, "Custom Grid Cell Editor Test",
83625579
RD
219 size=(640,480))
220 grid = GridEditorTest(self, log)
221
222#---------------------------------------------------------------------------
223
224if __name__ == '__main__':
225 import sys
8fa876ca 226 app = wx.PySimpleApp()
83625579 227 frame = TestFrame(None, sys.stdout)
1e4a197e 228 frame.Show(True)
83625579
RD
229 app.MainLoop()
230
231