2000-07-15 19:51:35 +00:00
|
|
|
|
2003-12-09 01:23:28 +00:00
|
|
|
import random
|
|
|
|
|
|
|
|
import wx
|
|
|
|
import wx.grid as gridlib
|
2000-07-15 19:51:35 +00:00
|
|
|
|
|
|
|
#---------------------------------------------------------------------------
|
|
|
|
|
2003-12-09 01:23:28 +00:00
|
|
|
class MyCustomRenderer(gridlib.PyGridCellRenderer):
|
2000-07-15 19:51:35 +00:00
|
|
|
def __init__(self):
|
2003-12-09 01:23:28 +00:00
|
|
|
gridlib.PyGridCellRenderer.__init__(self)
|
2000-07-15 19:51:35 +00:00
|
|
|
|
|
|
|
def Draw(self, grid, attr, dc, rect, row, col, isSelected):
|
2003-12-11 19:55:48 +00:00
|
|
|
dc.SetBackgroundMode(wx.SOLID)
|
|
|
|
dc.SetBrush(wx.Brush(wx.BLACK, wx.SOLID))
|
|
|
|
dc.SetPen(wx.TRANSPARENT_PEN)
|
2003-11-21 07:36:53 +00:00
|
|
|
dc.DrawRectangleRect(rect)
|
2000-07-15 19:51:35 +00:00
|
|
|
|
2003-12-09 01:23:28 +00:00
|
|
|
dc.SetBackgroundMode(wx.TRANSPARENT)
|
2000-07-15 19:51:35 +00:00
|
|
|
dc.SetFont(attr.GetFont())
|
|
|
|
|
|
|
|
text = grid.GetCellValue(row, col)
|
2003-11-21 07:36:53 +00:00
|
|
|
colors = ["RED", "WHITE", "SKY BLUE"]
|
2000-07-15 19:51:35 +00:00
|
|
|
x = rect.x + 1
|
|
|
|
y = rect.y + 1
|
2003-12-09 01:23:28 +00:00
|
|
|
|
2000-07-15 19:51:35 +00:00
|
|
|
for ch in text:
|
|
|
|
dc.SetTextForeground(random.choice(colors))
|
2004-05-02 02:41:33 +00:00
|
|
|
dc.DrawText(ch, x, y)
|
2000-07-15 19:51:35 +00:00
|
|
|
w, h = dc.GetTextExtent(ch)
|
|
|
|
x = x + w
|
|
|
|
if x > rect.right - 5:
|
|
|
|
break
|
|
|
|
|
|
|
|
|
|
|
|
def GetBestSize(self, grid, attr, dc, row, col):
|
|
|
|
text = grid.GetCellValue(row, col)
|
|
|
|
dc.SetFont(attr.GetFont())
|
|
|
|
w, h = dc.GetTextExtent(text)
|
2003-12-09 01:23:28 +00:00
|
|
|
return wx.Size(w, h)
|
2000-07-15 19:51:35 +00:00
|
|
|
|
|
|
|
|
|
|
|
def Clone(self):
|
|
|
|
return MyCustomRenderer()
|
|
|
|
|
|
|
|
|
|
|
|
#---------------------------------------------------------------------------
|
|
|
|
|
|
|
|
rendererDemoData = [
|
2003-12-09 01:23:28 +00:00
|
|
|
('GridCellStringRenderer\n(the default)', 'this is a text value', gridlib.GridCellStringRenderer, ()),
|
|
|
|
('GridCellNumberRenderer', '12345', gridlib.GridCellNumberRenderer, ()),
|
|
|
|
('GridCellFloatRenderer', '1234.5678', gridlib.GridCellFloatRenderer, (6,2)),
|
|
|
|
('GridCellBoolRenderer', '1', gridlib.GridCellBoolRenderer, ()),
|
2000-07-15 19:51:35 +00:00
|
|
|
('MyCustomRenderer', 'This is my renderer', MyCustomRenderer, ()),
|
|
|
|
]
|
|
|
|
|
|
|
|
editorDemoData = [
|
2003-12-09 01:23:28 +00:00
|
|
|
('GridCellTextEditor\n(the default)', 'Here is some more text', gridlib.GridCellTextEditor, ()),
|
|
|
|
('GridCellNumberEditor\nwith min,max', '101', gridlib.GridCellNumberEditor, (5, 10005)),
|
|
|
|
('GridCellNumberEditor\nwithout bounds', '101', gridlib.GridCellNumberEditor, ()),
|
|
|
|
('GridCellFloatEditor', '1234.5678', gridlib.GridCellFloatEditor, ()),
|
|
|
|
('GridCellBoolEditor', '1', gridlib.GridCellBoolEditor, ()),
|
|
|
|
('GridCellChoiceEditor', 'one', gridlib.GridCellChoiceEditor, (['one', 'two', 'three', 'four',
|
2000-07-15 19:51:35 +00:00
|
|
|
'kick', 'Microsoft', 'out the',
|
2003-03-25 06:35:27 +00:00
|
|
|
'door'], False)),
|
2000-07-15 19:51:35 +00:00
|
|
|
]
|
|
|
|
|
|
|
|
comboDemoData = [
|
2003-12-09 01:23:28 +00:00
|
|
|
('GridCellNumberRenderer\nGridCellNumberEditor', '20792', gridlib.GridCellNumberRenderer, gridlib.GridCellNumberEditor),
|
|
|
|
('GridCellBoolRenderer\nGridCellBoolEditor', '1', gridlib.GridCellBoolRenderer, gridlib.GridCellBoolEditor),
|
2000-07-15 19:51:35 +00:00
|
|
|
]
|
|
|
|
|
|
|
|
|
2003-12-09 01:23:28 +00:00
|
|
|
class EditorsAndRenderersGrid(gridlib.Grid):
|
2000-07-15 19:51:35 +00:00
|
|
|
def __init__(self, parent, log):
|
2003-12-09 01:23:28 +00:00
|
|
|
gridlib.Grid.__init__(self, parent, -1)
|
2000-07-15 19:51:35 +00:00
|
|
|
self.log = log
|
|
|
|
|
|
|
|
self.CreateGrid(25, 8)
|
|
|
|
renCol = 1
|
|
|
|
edCol = 4
|
|
|
|
|
|
|
|
|
|
|
|
self.SetCellValue(0, renCol, '''\
|
|
|
|
Cell Renderers are used to draw
|
|
|
|
the contents of the cell when they
|
|
|
|
need to be refreshed. Different
|
|
|
|
types of Renderers can be plugged in
|
|
|
|
to different cells in the grid, it can
|
|
|
|
even be automatically determined based
|
|
|
|
on the type of data in the cell.
|
|
|
|
''')
|
|
|
|
|
|
|
|
self.SetCellValue(0, edCol, '''\
|
|
|
|
Cell Editors are used when the
|
|
|
|
value of the cell is edited by
|
|
|
|
the user. An editor class is
|
|
|
|
wrapped around a an object
|
|
|
|
derived from wxControl and it
|
|
|
|
implements some methods required
|
|
|
|
to integrate with the grid.
|
|
|
|
''')
|
|
|
|
|
|
|
|
self.SetCellValue(16, renCol, '''\
|
|
|
|
Here are some combinations of Editors and
|
|
|
|
Renderers used together.
|
|
|
|
''')
|
|
|
|
|
|
|
|
row = 2
|
2003-12-09 01:23:28 +00:00
|
|
|
|
2000-07-15 19:51:35 +00:00
|
|
|
for label, value, renderClass, args in rendererDemoData:
|
2003-11-21 07:36:53 +00:00
|
|
|
renderer = renderClass(*args)
|
2000-07-15 19:51:35 +00:00
|
|
|
self.SetCellValue(row, renCol, label)
|
|
|
|
self.SetCellValue(row, renCol+1, value)
|
|
|
|
self.SetCellRenderer(row, renCol+1, renderer)
|
|
|
|
row = row + 2
|
|
|
|
|
|
|
|
|
|
|
|
row = 2
|
2003-12-09 01:23:28 +00:00
|
|
|
|
2000-07-15 19:51:35 +00:00
|
|
|
for label, value, editorClass, args in editorDemoData:
|
2003-11-21 07:36:53 +00:00
|
|
|
editor = editorClass(*args)
|
2000-07-15 19:51:35 +00:00
|
|
|
self.SetCellValue(row, edCol, label)
|
|
|
|
self.SetCellValue(row, edCol+1, value)
|
|
|
|
self.SetCellEditor(row, edCol+1, editor)
|
|
|
|
row = row + 2
|
|
|
|
|
|
|
|
|
|
|
|
row = 18
|
2003-12-09 01:23:28 +00:00
|
|
|
|
2000-07-15 19:51:35 +00:00
|
|
|
for label, value, renClass, edClass in comboDemoData:
|
|
|
|
self.SetCellValue(row, renCol, label)
|
|
|
|
self.SetCellValue(row, renCol+1, value)
|
2003-11-21 07:36:53 +00:00
|
|
|
editor = edClass()
|
|
|
|
renderer = renClass()
|
2000-07-15 19:51:35 +00:00
|
|
|
self.SetCellEditor(row, renCol+1, editor)
|
|
|
|
self.SetCellRenderer(row, renCol+1, renderer)
|
|
|
|
row = row + 2
|
|
|
|
|
|
|
|
font = self.GetFont()
|
2003-12-09 01:23:28 +00:00
|
|
|
font.SetWeight(wx.BOLD)
|
|
|
|
attr = gridlib.GridCellAttr()
|
2000-07-15 19:51:35 +00:00
|
|
|
attr.SetFont(font)
|
2003-12-09 01:23:28 +00:00
|
|
|
attr.SetBackgroundColour(wx.LIGHT_GREY)
|
2003-03-25 06:35:27 +00:00
|
|
|
attr.SetReadOnly(True)
|
2003-12-09 01:23:28 +00:00
|
|
|
attr.SetAlignment(wx.RIGHT, -1)
|
2000-07-15 19:51:35 +00:00
|
|
|
self.SetColAttr(renCol, attr)
|
2001-05-17 22:47:09 +00:00
|
|
|
attr.IncRef()
|
2000-07-15 19:51:35 +00:00
|
|
|
self.SetColAttr(edCol, attr)
|
|
|
|
|
|
|
|
# There is a bug in wxGTK for this method...
|
2003-03-25 06:35:27 +00:00
|
|
|
self.AutoSizeColumns(True)
|
|
|
|
self.AutoSizeRows(True)
|
2000-07-15 19:51:35 +00:00
|
|
|
|
2003-12-09 01:23:28 +00:00
|
|
|
self.Bind(gridlib.EVT_GRID_CELL_LEFT_DCLICK, self.OnLeftDClick)
|
2000-07-15 19:51:35 +00:00
|
|
|
|
|
|
|
|
|
|
|
# I do this because I don't like the default behaviour of not starting the
|
|
|
|
# cell editor on double clicks, but only a second click.
|
|
|
|
def OnLeftDClick(self, evt):
|
|
|
|
if self.CanEnableCellControl():
|
|
|
|
self.EnableCellEditControl()
|
|
|
|
|
|
|
|
|
|
|
|
#---------------------------------------------------------------------------
|
|
|
|
|
2003-12-09 01:23:28 +00:00
|
|
|
class TestFrame(wx.Frame):
|
2000-07-15 19:51:35 +00:00
|
|
|
def __init__(self, parent, log):
|
2003-12-09 01:23:28 +00:00
|
|
|
wx.Frame.__init__(self, parent, -1, "Editors and Renderers Demo", size=(640,480))
|
2000-07-15 19:51:35 +00:00
|
|
|
grid = EditorsAndRenderersGrid(self, log)
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
#---------------------------------------------------------------------------
|
|
|
|
|
|
|
|
if __name__ == '__main__':
|
|
|
|
import sys
|
2003-12-09 01:23:28 +00:00
|
|
|
app = wx.PySimpleApp()
|
2000-07-15 19:51:35 +00:00
|
|
|
frame = TestFrame(None, sys.stdout)
|
2003-03-25 06:35:27 +00:00
|
|
|
frame.Show(True)
|
2000-07-15 19:51:35 +00:00
|
|
|
app.MainLoop()
|
|
|
|
|
|
|
|
|
|
|
|
#---------------------------------------------------------------------------
|