]>
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 show here.  The 
  12     ones that must be overridden are marked with "*Must Override*" in the 
  15     Notice that in order to call the base class version of these special 
  16     methods we use the method name preceded by "base_".  This is because these 
  17     methods are "virtual" in C++ so if we try to call wx.GridCellEditor.Create 
  18     for example, then when the wxPython extension module tries to call 
  19     ptr->Create(...) then it actually calls the derived class version which 
  20     looks up the method in this class and calls it, causing a recursion loop. 
  21     If you don't understand any of this, don't worry, just call the "base_" 
  24     def __init__(self
, log
): 
  26         self
.log
.write("MyCellEditor ctor\n") 
  27         gridlib
.PyGridCellEditor
.__init
__(self
) 
  30     def Create(self
, parent
, id, evtHandler
): 
  32         Called to create the control, which must derive from wx.Control. 
  35         self
.log
.write("MyCellEditor: Create\n") 
  36         self
._tc 
= wx
.TextCtrl(parent
, id, "") 
  37         self
._tc
.SetInsertionPoint(0) 
  38         self
.SetControl(self
._tc
) 
  41             self
._tc
.PushEventHandler(evtHandler
) 
  44     def SetSize(self
, rect
): 
  46         Called to position/size the edit control within the cell rectangle. 
  47         If you don't fill the cell (the rect) then be sure to override 
  48         PaintBackground and do something meaningful there. 
  50         self
.log
.write("MyCellEditor: SetSize %s\n" % rect
) 
  51         self
._tc
.SetDimensions(rect
.x
, rect
.y
, rect
.width
+2, rect
.height
+2, 
  52                                wx
.SIZE_ALLOW_MINUS_ONE
) 
  55     def Show(self
, show
, attr
): 
  57         Show or hide the edit control.  You can use the attr (if not None) 
  58         to set colours or fonts for the control. 
  60         self
.log
.write("MyCellEditor: Show(self, %s, %s)\n" % (show
, attr
)) 
  61         self
.base_Show(show
, attr
) 
  64     def PaintBackground(self
, rect
, attr
): 
  66         Draws the part of the cell not occupied by the edit control.  The 
  67         base  class version just fills it with background colour from the 
  68         attribute.  In this class the edit control fills the whole cell so 
  69         don't do anything at all in order to reduce flicker. 
  71         self
.log
.write("MyCellEditor: PaintBackground\n") 
  74     def BeginEdit(self
, row
, col
, grid
): 
  76         Fetch the value from the table and prepare the edit control 
  77         to begin editing.  Set the focus to the edit control. 
  80         self
.log
.write("MyCellEditor: BeginEdit (%d,%d)\n" % (row
, col
)) 
  81         self
.startValue 
= grid
.GetTable().GetValue(row
, col
) 
  82         self
._tc
.SetValue(self
.startValue
) 
  83         self
._tc
.SetInsertionPointEnd() 
  86         # For this example, select the text 
  87         self
._tc
.SetSelection(0, self
._tc
.GetLastPosition()) 
  90     def EndEdit(self
, row
, col
, grid
): 
  92         Complete the editing of the current cell. Returns True if the value 
  93         has changed.  If necessary, the control may be destroyed. 
  96         self
.log
.write("MyCellEditor: EndEdit (%d,%d)\n" % (row
, col
)) 
  99         val 
= self
._tc
.GetValue() 
 101         if val 
!= self
.startValue
: 
 103             grid
.GetTable().SetValue(row
, col
, val
) # update the table 
 106         self
._tc
.SetValue('') 
 112         Reset the value in the control back to its starting value. 
 115         self
.log
.write("MyCellEditor: Reset\n") 
 116         self
._tc
.SetValue(self
.startValue
) 
 117         self
._tc
.SetInsertionPointEnd() 
 120     def IsAcceptedKey(self
, evt
): 
 122         Return True to allow the given key to start editing: the base class 
 123         version only checks that the event has no modifiers.  F2 is special 
 124         and will always start the editor. 
 126         self
.log
.write("MyCellEditor: IsAcceptedKey: %d\n" % (evt
.GetKeyCode())) 
 128         ## Oops, there's a bug here, we'll have to do it ourself.. 
 129         ##return self.base_IsAcceptedKey(evt) 
 131         return (not (evt
.ControlDown() or evt
.AltDown()) and 
 132                 evt
.GetKeyCode() != wx
.WXK_SHIFT
) 
 135     def StartingKey(self
, evt
): 
 137         If the editor is enabled by pressing keys on the grid, this will be 
 138         called to let the editor do something about that first key if desired. 
 140         self
.log
.write("MyCellEditor: StartingKey %d\n" % evt
.GetKeyCode()) 
 141         key 
= evt
.GetKeyCode() 
 143         if key 
in [ wx
.WXK_NUMPAD0
, wx
.WXK_NUMPAD1
, wx
.WXK_NUMPAD2
, wx
.WXK_NUMPAD3
,  
 144                     wx
.WXK_NUMPAD4
, wx
.WXK_NUMPAD5
, wx
.WXK_NUMPAD6
, wx
.WXK_NUMPAD7
,  
 145                     wx
.WXK_NUMPAD8
, wx
.WXK_NUMPAD9
 
 148             ch 
= ch 
= chr(ord('0') + key 
- wx
.WXK_NUMPAD0
) 
 150         elif key 
< 256 and key 
>= 0 and chr(key
) in string
.printable
: 
 152             if not evt
.ShiftDown(): 
 156             # For this example, replace the text.  Normally we would append it. 
 157             #self._tc.AppendText(ch) 
 158             self
._tc
.SetValue(ch
) 
 159             self
._tc
.SetInsertionPointEnd() 
 164     def StartingClick(self
): 
 166         If the editor is enabled by clicking on the cell, this method will be 
 167         called to allow the editor to simulate the click on the control if 
 170         self
.log
.write("MyCellEditor: StartingClick\n") 
 175         self
.log
.write("MyCellEditor: Destroy\n") 
 181         Create a new object which is the copy of this one 
 184         self
.log
.write("MyCellEditor: Clone\n") 
 185         return MyCellEditor(self
.log
) 
 188 #--------------------------------------------------------------------------- 
 189 class GridEditorTest(gridlib
.Grid
): 
 190     def __init__(self
, parent
, log
): 
 191         gridlib
.Grid
.__init
__(self
, parent
, -1) 
 194         self
.CreateGrid(10, 3) 
 196         # Somebody changed the grid so the type registry takes precedence 
 197         # over the default attribute set for editors and renderers, so we 
 198         # have to set null handlers for the type registry before the 
 199         # default editor will get used otherwise... 
 200         #self.RegisterDataType(wxGRID_VALUE_STRING, None, None) 
 201         #self.SetDefaultEditor(MyCellEditor(self.log)) 
 203         # Or we could just do it like this: 
 204         #self.RegisterDataType(wx.GRID_VALUE_STRING, 
 205         #                      wx.GridCellStringRenderer(), 
 206         #                      MyCellEditor(self.log)) 
 209         # but for this example, we'll just set the custom editor on one cell 
 210         self
.SetCellEditor(1, 0, MyCellEditor(self
.log
)) 
 211         self
.SetCellValue(1, 0, "Try to edit this box") 
 214         attr 
= gridlib
.GridCellAttr() 
 215         attr
.SetEditor(MyCellEditor(self
.log
)) 
 216         self
.SetColAttr(2, attr
) 
 217         self
.SetCellValue(1, 2, "or any in this column") 
 219         self
.SetColSize(0, 150) 
 220         self
.SetColSize(1, 150) 
 221         self
.SetColSize(2, 150) 
 224 #--------------------------------------------------------------------------- 
 226 class TestFrame(wx
.Frame
): 
 227     def __init__(self
, parent
, log
): 
 228         wx
.Frame
.__init
__(self
, parent
, -1, "Custom Grid Cell Editor Test", 
 230         grid 
= GridEditorTest(self
, log
) 
 232 #--------------------------------------------------------------------------- 
 234 if __name__ 
== '__main__': 
 236     app 
= wx
.PySimpleApp() 
 237     frame 
= TestFrame(None, sys
.stdout
)