]>
git.saurik.com Git - wxWidgets.git/blob - wxPython/demo/GridCustEditor.py
   5 import  wx
.grid 
as gridlib
 
   7 #--------------------------------------------------------------------------- 
   8 class MyCellEditor(gridlib
.PyGridCellEditor
): 
  10     This is a sample GridCellEditor that shows you how to make your own custom 
  11     grid editors.  All the methods that can be overridden are shown here.  The 
  12     ones that must be overridden are marked with "*Must Override*" in the 
  15     def __init__(self
, log
): 
  17         self
.log
.write("MyCellEditor ctor\n") 
  18         gridlib
.PyGridCellEditor
.__init
__(self
) 
  21     def Create(self
, parent
, id, evtHandler
): 
  23         Called to create the control, which must derive from wx.Control. 
  26         self
.log
.write("MyCellEditor: Create\n") 
  27         self
._tc 
= wx
.TextCtrl(parent
, id, "") 
  28         self
._tc
.SetInsertionPoint(0) 
  29         self
.SetControl(self
._tc
) 
  32             self
._tc
.PushEventHandler(evtHandler
) 
  35     def SetSize(self
, rect
): 
  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. 
  41         self
.log
.write("MyCellEditor: SetSize %s\n" % rect
) 
  42         self
._tc
.SetDimensions(rect
.x
, rect
.y
, rect
.width
+2, rect
.height
+2, 
  43                                wx
.SIZE_ALLOW_MINUS_ONE
) 
  46     def Show(self
, show
, attr
): 
  48         Show or hide the edit control.  You can use the attr (if not None) 
  49         to set colours or fonts for the control. 
  51         self
.log
.write("MyCellEditor: Show(self, %s, %s)\n" % (show
, attr
)) 
  52         super(MyCellEditor
, self
).Show(show
, attr
) 
  55     def PaintBackground(self
, rect
, attr
): 
  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. 
  62         self
.log
.write("MyCellEditor: PaintBackground\n") 
  65     def BeginEdit(self
, row
, col
, grid
): 
  67         Fetch the value from the table and prepare the edit control 
  68         to begin editing.  Set the focus to the edit control. 
  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() 
  77         # For this example, select the text 
  78         self
._tc
.SetSelection(0, self
._tc
.GetLastPosition()) 
  81     def EndEdit(self
, row
, col
, grid
): 
  83         Complete the editing of the current cell. Returns True if the value 
  84         has changed.  If necessary, the control may be destroyed. 
  87         self
.log
.write("MyCellEditor: EndEdit (%d,%d)\n" % (row
, col
)) 
  90         val 
= self
._tc
.GetValue() 
  92         if val 
!= self
.startValue
: 
  94             grid
.GetTable().SetValue(row
, col
, val
) # update the table 
 103         Reset the value in the control back to its starting value. 
 106         self
.log
.write("MyCellEditor: Reset\n") 
 107         self
._tc
.SetValue(self
.startValue
) 
 108         self
._tc
.SetInsertionPointEnd() 
 111     def IsAcceptedKey(self
, evt
): 
 113         Return True to allow the given key to start editing: the base class 
 114         version only checks that the event has no modifiers.  F2 is special 
 115         and will always start the editor. 
 117         self
.log
.write("MyCellEditor: IsAcceptedKey: %d\n" % (evt
.GetKeyCode())) 
 119         ## We can ask the base class to do it 
 120         #return super(MyCellEditor, self).IsAcceptedKey(evt) 
 123         return (not (evt
.ControlDown() or evt
.AltDown()) and 
 124                 evt
.GetKeyCode() != wx
.WXK_SHIFT
) 
 127     def StartingKey(self
, evt
): 
 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. 
 132         self
.log
.write("MyCellEditor: StartingKey %d\n" % evt
.GetKeyCode()) 
 133         key 
= evt
.GetKeyCode() 
 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
 
 140             ch 
= ch 
= chr(ord('0') + key 
- wx
.WXK_NUMPAD0
) 
 142         elif key 
< 256 and key 
>= 0 and chr(key
) in string
.printable
: 
 146             # For this example, replace the text.  Normally we would append it. 
 147             #self._tc.AppendText(ch) 
 148             self
._tc
.SetValue(ch
) 
 149             self
._tc
.SetInsertionPointEnd() 
 154     def StartingClick(self
): 
 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 
 160         self
.log
.write("MyCellEditor: StartingClick\n") 
 165         self
.log
.write("MyCellEditor: Destroy\n") 
 166         super(MyCellEditor
, self
).Destroy() 
 171         Create a new object which is the copy of this one 
 174         self
.log
.write("MyCellEditor: Clone\n") 
 175         return MyCellEditor(self
.log
) 
 178 #--------------------------------------------------------------------------- 
 179 class GridEditorTest(gridlib
.Grid
): 
 180     def __init__(self
, parent
, log
): 
 181         gridlib
.Grid
.__init
__(self
, parent
, -1) 
 184         self
.CreateGrid(10, 3) 
 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)) 
 193         # Or we could just do it like this: 
 194         #self.RegisterDataType(wx.GRID_VALUE_STRING, 
 195         #                      wx.GridCellStringRenderer(), 
 196         #                      MyCellEditor(self.log)) 
 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") 
 204         attr 
= gridlib
.GridCellAttr() 
 205         attr
.SetEditor(MyCellEditor(self
.log
)) 
 206         self
.SetColAttr(2, attr
) 
 207         self
.SetCellValue(1, 2, "or any in this column") 
 209         self
.SetColSize(0, 150) 
 210         self
.SetColSize(1, 150) 
 211         self
.SetColSize(2, 150) 
 214 #--------------------------------------------------------------------------- 
 216 class TestFrame(wx
.Frame
): 
 217     def __init__(self
, parent
, log
): 
 218         wx
.Frame
.__init
__(self
, parent
, -1, "Custom Grid Cell Editor Test", 
 220         grid 
= GridEditorTest(self
, log
) 
 222 #--------------------------------------------------------------------------- 
 224 if __name__ 
== '__main__': 
 226     app 
= wx
.PySimpleApp() 
 227     frame 
= TestFrame(None, sys
.stdout
)