]>
git.saurik.com Git - wxWidgets.git/blob - wxPython/samples/wxPIA_book/Chapter-14/grid_editor.py
   5 class UpCaseCellEditor(wx
.grid
.PyGridCellEditor
): 
   7         wx
.grid
.PyGridCellEditor
.__init
__(self
) 
   9     def Create(self
, parent
, id, evtHandler
): 
  11         Called to create the control, which must derive from wx.Control. 
  14         self
._tc 
= wx
.TextCtrl(parent
, id, "") 
  15         self
._tc
.SetInsertionPoint(0) 
  16         self
.SetControl(self
._tc
) 
  19             self
._tc
.PushEventHandler(evtHandler
) 
  21         self
._tc
.Bind(wx
.EVT_CHAR
, self
.OnChar
) 
  23     def SetSize(self
, rect
): 
  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. 
  29         self
._tc
.SetDimensions(rect
.x
, rect
.y
, rect
.width
+2, rect
.height
+2, 
  30                                wx
.SIZE_ALLOW_MINUS_ONE
) 
  32     def BeginEdit(self
, row
, col
, grid
): 
  34         Fetch the value from the table and prepare the edit control 
  35         to begin editing.  Set the focus to the edit control. 
  38         self
.startValue 
= grid
.GetTable().GetValue(row
, col
) 
  39         self
._tc
.SetValue(self
.startValue
) 
  40         self
._tc
.SetInsertionPointEnd() 
  42         self
._tc
.SetSelection(0, self
._tc
.GetLastPosition()) 
  44     def EndEdit(self
, row
, col
, grid
): 
  46         Complete the editing of the current cell. Returns True if the value 
  47         has changed.  If necessary, the control may be destroyed. 
  52         val 
= self
._tc
.GetValue() 
  54         if val 
!= self
.startValue
: 
  56             grid
.GetTable().SetValue(row
, col
, val
) # update the table 
  64         Reset the value in the control back to its starting value. 
  67         self
._tc
.SetValue(self
.startValue
) 
  68         self
._tc
.SetInsertionPointEnd() 
  72         Create a new object which is the copy of this one 
  75         return UpCaseCellEditor() 
  77     def StartingKey(self
, evt
): 
  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. 
  84             self
._tc
.EmulateKeyPress(evt
) 
  86     def OnChar(self
, evt
): 
  87         key 
= evt
.GetKeyCode() 
  92         if char 
in string
.letters
: 
  94             self
._tc
.WriteText(char
) 
  98 class TestFrame(wx
.Frame
): 
 100         wx
.Frame
.__init
__(self
, None, title
="Grid Editor", 
 103         grid 
= wx
.grid
.Grid(self
) 
 104         grid
.CreateGrid(50,50) 
 105         grid
.SetDefaultEditor(UpCaseCellEditor()) 
 108 app 
= wx
.PySimpleApp()