2003-12-08 20:23:28 -05:00
|
|
|
|
|
|
|
import wx
|
|
|
|
import wx.grid as gridlib
|
|
|
|
#import wx.lib.mixins.grid as mixins
|
2000-07-15 15:51:35 -04:00
|
|
|
|
|
|
|
#---------------------------------------------------------------------------
|
|
|
|
|
2003-12-08 20:23:28 -05:00
|
|
|
class SimpleGrid(gridlib.Grid): ##, mixins.GridAutoEditMixin):
|
2000-07-15 15:51:35 -04:00
|
|
|
def __init__(self, parent, log):
|
2003-12-08 20:23:28 -05:00
|
|
|
gridlib.Grid.__init__(self, parent, -1)
|
|
|
|
##mixins.GridAutoEditMixin.__init__(self)
|
2000-07-15 15:51:35 -04:00
|
|
|
self.log = log
|
2000-10-30 16:08:42 -05:00
|
|
|
self.moveTo = None
|
|
|
|
|
2003-12-08 20:23:28 -05:00
|
|
|
self.Bind(wx.EVT_IDLE, self.OnIdle)
|
2000-07-15 15:51:35 -04:00
|
|
|
|
2004-10-27 20:43:09 -04:00
|
|
|
self.CreateGrid(25, 25)#, gridlib.Grid.SelectRows)
|
2003-03-25 01:35:27 -05:00
|
|
|
##self.EnableEditing(False)
|
2000-07-15 15:51:35 -04:00
|
|
|
|
|
|
|
# simple cell formatting
|
|
|
|
self.SetColSize(3, 200)
|
|
|
|
self.SetRowSize(4, 45)
|
|
|
|
self.SetCellValue(0, 0, "First cell")
|
|
|
|
self.SetCellValue(1, 1, "Another cell")
|
|
|
|
self.SetCellValue(2, 2, "Yet another cell")
|
2001-05-17 18:47:09 -04:00
|
|
|
self.SetCellValue(3, 3, "This cell is read-only")
|
2003-12-08 20:23:28 -05:00
|
|
|
self.SetCellFont(0, 0, wx.Font(12, wx.ROMAN, wx.ITALIC, wx.NORMAL))
|
|
|
|
self.SetCellTextColour(1, 1, wx.RED)
|
|
|
|
self.SetCellBackgroundColour(2, 2, wx.CYAN)
|
2003-03-25 01:35:27 -05:00
|
|
|
self.SetReadOnly(3, 3, True)
|
2001-05-17 18:47:09 -04:00
|
|
|
|
2003-12-08 20:23:28 -05:00
|
|
|
self.SetCellEditor(5, 0, gridlib.GridCellNumberEditor(1,1000))
|
2001-06-15 17:43:26 -04:00
|
|
|
self.SetCellValue(5, 0, "123")
|
2003-12-08 20:23:28 -05:00
|
|
|
self.SetCellEditor(6, 0, gridlib.GridCellFloatEditor())
|
2001-06-15 17:43:26 -04:00
|
|
|
self.SetCellValue(6, 0, "123.34")
|
2003-12-08 20:23:28 -05:00
|
|
|
self.SetCellEditor(7, 0, gridlib.GridCellNumberEditor())
|
2000-07-15 15:51:35 -04:00
|
|
|
|
2001-11-02 15:56:23 -05:00
|
|
|
self.SetCellValue(6, 3, "You can veto editing this cell")
|
|
|
|
|
2003-10-01 19:28:31 -04:00
|
|
|
#self.SetRowLabelSize(0)
|
|
|
|
#self.SetColLabelSize(0)
|
2001-11-02 15:56:23 -05:00
|
|
|
|
2000-07-15 15:51:35 -04:00
|
|
|
# attribute objects let you keep a set of formatting values
|
|
|
|
# in one spot, and reuse them if needed
|
2003-12-08 20:23:28 -05:00
|
|
|
attr = gridlib.GridCellAttr()
|
|
|
|
attr.SetTextColour(wx.BLACK)
|
|
|
|
attr.SetBackgroundColour(wx.RED)
|
|
|
|
attr.SetFont(wx.Font(10, wx.SWISS, wx.NORMAL, wx.BOLD))
|
2000-07-15 15:51:35 -04:00
|
|
|
|
|
|
|
# you can set cell attributes for the whole row (or column)
|
|
|
|
self.SetRowAttr(5, attr)
|
|
|
|
|
2000-08-08 02:11:51 -04:00
|
|
|
self.SetColLabelValue(0, "Custom")
|
|
|
|
self.SetColLabelValue(1, "column")
|
|
|
|
self.SetColLabelValue(2, "labels")
|
2000-07-15 15:51:35 -04:00
|
|
|
|
2003-12-08 20:23:28 -05:00
|
|
|
self.SetColLabelAlignment(wx.ALIGN_LEFT, wx.ALIGN_BOTTOM)
|
2001-04-01 23:49:52 -04:00
|
|
|
|
2003-03-25 01:35:27 -05:00
|
|
|
#self.SetDefaultCellOverflow(False)
|
2005-01-13 15:45:17 -05:00
|
|
|
#r = gridlib.GridCellAutoWrapStringRenderer()
|
2003-03-25 01:35:27 -05:00
|
|
|
#self.SetCellRenderer(9, 1, r)
|
|
|
|
|
|
|
|
# overflow cells
|
|
|
|
self.SetCellValue( 9, 1, "This default cell will overflow into neighboring cells, but not if you turn overflow off.");
|
|
|
|
self.SetCellSize(11, 1, 3, 3);
|
2003-12-08 20:23:28 -05:00
|
|
|
self.SetCellAlignment(11, 1, wx.ALIGN_CENTRE, wx.ALIGN_CENTRE);
|
2003-03-25 01:35:27 -05:00
|
|
|
self.SetCellValue(11, 1, "This cell is set to span 3 rows and 3 columns");
|
|
|
|
|
|
|
|
|
2003-12-08 20:23:28 -05:00
|
|
|
editor = gridlib.GridCellTextEditor()
|
2003-07-02 19:13:10 -04:00
|
|
|
editor.SetParameters('10')
|
|
|
|
self.SetCellEditor(0, 4, editor)
|
|
|
|
self.SetCellValue(0, 4, "Limited text")
|
|
|
|
|
2006-11-20 22:35:32 -05:00
|
|
|
renderer = gridlib.GridCellAutoWrapStringRenderer()
|
|
|
|
self.SetCellRenderer(15,0, renderer)
|
|
|
|
self.SetCellValue(15,0, "The text in this cell will be rendered with word-wrapping")
|
2003-03-25 01:35:27 -05:00
|
|
|
|
2006-11-20 22:35:32 -05:00
|
|
|
|
2000-07-15 15:51:35 -04:00
|
|
|
# test all the events
|
2003-12-08 20:23:28 -05:00
|
|
|
self.Bind(gridlib.EVT_GRID_CELL_LEFT_CLICK, self.OnCellLeftClick)
|
|
|
|
self.Bind(gridlib.EVT_GRID_CELL_RIGHT_CLICK, self.OnCellRightClick)
|
|
|
|
self.Bind(gridlib.EVT_GRID_CELL_LEFT_DCLICK, self.OnCellLeftDClick)
|
|
|
|
self.Bind(gridlib.EVT_GRID_CELL_RIGHT_DCLICK, self.OnCellRightDClick)
|
2000-07-15 15:51:35 -04:00
|
|
|
|
2003-12-08 20:23:28 -05:00
|
|
|
self.Bind(gridlib.EVT_GRID_LABEL_LEFT_CLICK, self.OnLabelLeftClick)
|
|
|
|
self.Bind(gridlib.EVT_GRID_LABEL_RIGHT_CLICK, self.OnLabelRightClick)
|
|
|
|
self.Bind(gridlib.EVT_GRID_LABEL_LEFT_DCLICK, self.OnLabelLeftDClick)
|
|
|
|
self.Bind(gridlib.EVT_GRID_LABEL_RIGHT_DCLICK, self.OnLabelRightDClick)
|
2000-07-15 15:51:35 -04:00
|
|
|
|
2003-12-08 20:23:28 -05:00
|
|
|
self.Bind(gridlib.EVT_GRID_ROW_SIZE, self.OnRowSize)
|
|
|
|
self.Bind(gridlib.EVT_GRID_COL_SIZE, self.OnColSize)
|
2000-07-15 15:51:35 -04:00
|
|
|
|
2003-12-08 20:23:28 -05:00
|
|
|
self.Bind(gridlib.EVT_GRID_RANGE_SELECT, self.OnRangeSelect)
|
|
|
|
self.Bind(gridlib.EVT_GRID_CELL_CHANGE, self.OnCellChange)
|
|
|
|
self.Bind(gridlib.EVT_GRID_SELECT_CELL, self.OnSelectCell)
|
2000-07-15 15:51:35 -04:00
|
|
|
|
2003-12-08 20:23:28 -05:00
|
|
|
self.Bind(gridlib.EVT_GRID_EDITOR_SHOWN, self.OnEditorShown)
|
|
|
|
self.Bind(gridlib.EVT_GRID_EDITOR_HIDDEN, self.OnEditorHidden)
|
|
|
|
self.Bind(gridlib.EVT_GRID_EDITOR_CREATED, self.OnEditorCreated)
|
2000-07-15 15:51:35 -04:00
|
|
|
|
2001-09-15 21:51:13 -04:00
|
|
|
|
2000-07-15 15:51:35 -04:00
|
|
|
def OnCellLeftClick(self, evt):
|
|
|
|
self.log.write("OnCellLeftClick: (%d,%d) %s\n" %
|
|
|
|
(evt.GetRow(), evt.GetCol(), evt.GetPosition()))
|
|
|
|
evt.Skip()
|
|
|
|
|
|
|
|
def OnCellRightClick(self, evt):
|
|
|
|
self.log.write("OnCellRightClick: (%d,%d) %s\n" %
|
|
|
|
(evt.GetRow(), evt.GetCol(), evt.GetPosition()))
|
|
|
|
evt.Skip()
|
|
|
|
|
|
|
|
def OnCellLeftDClick(self, evt):
|
|
|
|
self.log.write("OnCellLeftDClick: (%d,%d) %s\n" %
|
|
|
|
(evt.GetRow(), evt.GetCol(), evt.GetPosition()))
|
|
|
|
evt.Skip()
|
|
|
|
|
|
|
|
def OnCellRightDClick(self, evt):
|
|
|
|
self.log.write("OnCellRightDClick: (%d,%d) %s\n" %
|
|
|
|
(evt.GetRow(), evt.GetCol(), evt.GetPosition()))
|
|
|
|
evt.Skip()
|
|
|
|
|
|
|
|
def OnLabelLeftClick(self, evt):
|
|
|
|
self.log.write("OnLabelLeftClick: (%d,%d) %s\n" %
|
|
|
|
(evt.GetRow(), evt.GetCol(), evt.GetPosition()))
|
|
|
|
evt.Skip()
|
|
|
|
|
|
|
|
def OnLabelRightClick(self, evt):
|
|
|
|
self.log.write("OnLabelRightClick: (%d,%d) %s\n" %
|
|
|
|
(evt.GetRow(), evt.GetCol(), evt.GetPosition()))
|
|
|
|
evt.Skip()
|
|
|
|
|
|
|
|
def OnLabelLeftDClick(self, evt):
|
|
|
|
self.log.write("OnLabelLeftDClick: (%d,%d) %s\n" %
|
|
|
|
(evt.GetRow(), evt.GetCol(), evt.GetPosition()))
|
|
|
|
evt.Skip()
|
|
|
|
|
|
|
|
def OnLabelRightDClick(self, evt):
|
|
|
|
self.log.write("OnLabelRightDClick: (%d,%d) %s\n" %
|
|
|
|
(evt.GetRow(), evt.GetCol(), evt.GetPosition()))
|
|
|
|
evt.Skip()
|
|
|
|
|
|
|
|
def OnRowSize(self, evt):
|
|
|
|
self.log.write("OnRowSize: row %d, %s\n" %
|
|
|
|
(evt.GetRowOrCol(), evt.GetPosition()))
|
|
|
|
evt.Skip()
|
|
|
|
|
|
|
|
def OnColSize(self, evt):
|
|
|
|
self.log.write("OnColSize: col %d, %s\n" %
|
|
|
|
(evt.GetRowOrCol(), evt.GetPosition()))
|
|
|
|
evt.Skip()
|
|
|
|
|
|
|
|
def OnRangeSelect(self, evt):
|
|
|
|
if evt.Selecting():
|
|
|
|
self.log.write("OnRangeSelect: top-left %s, bottom-right %s\n" %
|
|
|
|
(evt.GetTopLeftCoords(), evt.GetBottomRightCoords()))
|
|
|
|
evt.Skip()
|
|
|
|
|
2000-10-30 16:08:42 -05:00
|
|
|
|
2000-07-15 15:51:35 -04:00
|
|
|
def OnCellChange(self, evt):
|
|
|
|
self.log.write("OnCellChange: (%d,%d) %s\n" %
|
|
|
|
(evt.GetRow(), evt.GetCol(), evt.GetPosition()))
|
2000-10-30 16:08:42 -05:00
|
|
|
|
|
|
|
# Show how to stay in a cell that has bad data. We can't just
|
|
|
|
# call SetGridCursor here since we are nested inside one so it
|
2003-10-01 20:58:06 -04:00
|
|
|
# won't have any effect. Instead, set coordinates to move to in
|
2000-10-30 16:08:42 -05:00
|
|
|
# idle time.
|
|
|
|
value = self.GetCellValue(evt.GetRow(), evt.GetCol())
|
2003-12-08 20:23:28 -05:00
|
|
|
|
2000-10-30 16:08:42 -05:00
|
|
|
if value == 'no good':
|
|
|
|
self.moveTo = evt.GetRow(), evt.GetCol()
|
|
|
|
|
2001-06-15 17:43:26 -04:00
|
|
|
|
2000-10-30 16:08:42 -05:00
|
|
|
def OnIdle(self, evt):
|
|
|
|
if self.moveTo != None:
|
|
|
|
self.SetGridCursor(self.moveTo[0], self.moveTo[1])
|
|
|
|
self.moveTo = None
|
2003-12-08 20:23:28 -05:00
|
|
|
|
2001-06-15 17:43:26 -04:00
|
|
|
evt.Skip()
|
2000-10-30 16:08:42 -05:00
|
|
|
|
2000-07-15 15:51:35 -04:00
|
|
|
|
|
|
|
def OnSelectCell(self, evt):
|
|
|
|
self.log.write("OnSelectCell: (%d,%d) %s\n" %
|
|
|
|
(evt.GetRow(), evt.GetCol(), evt.GetPosition()))
|
2000-10-30 16:08:42 -05:00
|
|
|
|
|
|
|
# Another way to stay in a cell that has a bad value...
|
|
|
|
row = self.GetGridCursorRow()
|
|
|
|
col = self.GetGridCursorCol()
|
2003-12-08 20:23:28 -05:00
|
|
|
|
2000-10-30 16:08:42 -05:00
|
|
|
if self.IsCellEditControlEnabled():
|
|
|
|
self.HideCellEditControl()
|
|
|
|
self.DisableCellEditControl()
|
2003-12-08 20:23:28 -05:00
|
|
|
|
2000-10-30 16:08:42 -05:00
|
|
|
value = self.GetCellValue(row, col)
|
2003-12-08 20:23:28 -05:00
|
|
|
|
2000-10-30 16:08:42 -05:00
|
|
|
if value == 'no good 2':
|
|
|
|
return # cancels the cell selection
|
2003-12-08 20:23:28 -05:00
|
|
|
|
2001-06-15 17:43:26 -04:00
|
|
|
evt.Skip()
|
2000-10-30 16:08:42 -05:00
|
|
|
|
2000-07-15 15:51:35 -04:00
|
|
|
|
|
|
|
def OnEditorShown(self, evt):
|
2001-11-02 15:56:23 -05:00
|
|
|
if evt.GetRow() == 6 and evt.GetCol() == 3 and \
|
2003-12-08 20:23:28 -05:00
|
|
|
wx.MessageBox("Are you sure you wish to edit this cell?",
|
|
|
|
"Checking", wx.YES_NO) == wx.NO:
|
2001-11-02 15:56:23 -05:00
|
|
|
evt.Veto()
|
|
|
|
return
|
|
|
|
|
2000-07-15 15:51:35 -04:00
|
|
|
self.log.write("OnEditorShown: (%d,%d) %s\n" %
|
|
|
|
(evt.GetRow(), evt.GetCol(), evt.GetPosition()))
|
|
|
|
evt.Skip()
|
|
|
|
|
2001-11-02 15:56:23 -05:00
|
|
|
|
2000-07-15 15:51:35 -04:00
|
|
|
def OnEditorHidden(self, evt):
|
2001-11-02 15:56:23 -05:00
|
|
|
if evt.GetRow() == 6 and evt.GetCol() == 3 and \
|
2003-12-08 20:23:28 -05:00
|
|
|
wx.MessageBox("Are you sure you wish to finish editing this cell?",
|
|
|
|
"Checking", wx.YES_NO) == wx.NO:
|
2001-11-02 15:56:23 -05:00
|
|
|
evt.Veto()
|
|
|
|
return
|
|
|
|
|
2000-07-15 15:51:35 -04:00
|
|
|
self.log.write("OnEditorHidden: (%d,%d) %s\n" %
|
|
|
|
(evt.GetRow(), evt.GetCol(), evt.GetPosition()))
|
|
|
|
evt.Skip()
|
|
|
|
|
2001-11-02 15:56:23 -05:00
|
|
|
|
2001-05-31 02:23:06 -04:00
|
|
|
def OnEditorCreated(self, evt):
|
|
|
|
self.log.write("OnEditorCreated: (%d, %d) %s\n" %
|
|
|
|
(evt.GetRow(), evt.GetCol(), evt.GetControl()))
|
|
|
|
|
|
|
|
|
2000-07-15 15:51:35 -04:00
|
|
|
|
|
|
|
#---------------------------------------------------------------------------
|
|
|
|
|
2003-12-08 20:23:28 -05:00
|
|
|
class TestFrame(wx.Frame):
|
2000-07-15 15:51:35 -04:00
|
|
|
def __init__(self, parent, log):
|
2003-12-08 20:23:28 -05:00
|
|
|
wx.Frame.__init__(self, parent, -1, "Simple Grid Demo", size=(640,480))
|
2006-07-22 19:04:08 -04:00
|
|
|
self.grid = SimpleGrid(self, log)
|
2001-09-04 00:17:38 -04:00
|
|
|
|
2000-07-15 15:51:35 -04:00
|
|
|
|
|
|
|
|
|
|
|
#---------------------------------------------------------------------------
|
|
|
|
|
|
|
|
if __name__ == '__main__':
|
|
|
|
import sys
|
2007-04-02 17:21:35 -04:00
|
|
|
from wx.lib.mixins.inspection import InspectableApp
|
2007-03-26 21:49:46 -04:00
|
|
|
app = InspectableApp(False)
|
2000-07-15 15:51:35 -04:00
|
|
|
frame = TestFrame(None, sys.stdout)
|
2003-03-25 01:35:27 -05:00
|
|
|
frame.Show(True)
|
2006-07-22 19:04:08 -04:00
|
|
|
#import wx.py
|
|
|
|
#shell = wx.py.shell.ShellFrame(frame, locals={'wx':wx, 'frame':frame})
|
|
|
|
#shell.Show()
|
2000-07-15 15:51:35 -04:00
|
|
|
app.MainLoop()
|
|
|
|
|
|
|
|
|
|
|
|
#---------------------------------------------------------------------------
|
2000-10-30 16:08:42 -05:00
|
|
|
|
|
|
|
|