wxGridCellAttr
wxGridCellAttr objects provide a mechanism for altering the display of particular rows, columns, or individual cells within a grid. The grid cell attribute objects operate as overlapping templates, where a partially "filled" attribute will add only its defined sub-attributes when added to another attribute object.
For instance, if you specified a column attribute which defined only color, a row attribute which only defined font, and a cell attribute which defined read-only status, your grid cell renderer would receive an attribute object with each of those properties (and the default properties for the grid).
XXX I'm not particularly clear on what the order is for the combination (i.e. which attribute object overrides which other attribute object).
Object Lifetime
wxGridCellAttr() -- Default constructor
wxGridCellAttr* Clone() -- Creates a new copy of this object.
void SetDefAttr(wxGridCellAttr* defAttr) -- I assume this is a class method that sets a global default attribute object of some sort, but I don't know what the scope of that "default" is.
Properties
The attribute has a number of properties each of which (with the exception of read-only status) has a Set'Name', a Has'Name' and a Get'Name' method. Read-only status defaults to false, and is always defined.
void SetTextColour(const wxColour& colText)
bool HasTextColour()
const wxColour& GetTextColour()
void SetBackgroundColour(const wxColour& colBack)
bool HasBackgroundColour()
const wxColour& GetBackgroundColour()
void SetFont(const wxFont& font)
bool HasFont()
const wxFont& GetFont()
void SetAlignment(int hAlign, int vAlign)
bool HasAlignment()
void GetAlignment(int* hAlign, int* vAlign)
void SetReadOnly(bool isReadOnly = TRUE)
bool IsReadOnly()
void SetRenderer(wxGridCellRenderer* renderer)
bool HasRenderer()
wxGridCellRenderer* GetRenderer(wxGrid* grid, int row, int col)
void SetEditor(wxGridCellEditor* editor)
bool HasEditor()
wxGridCellEditor* GetEditor(wxGrid* grid, int row, int col)
SetOverflow( bool allow ) -- set whether the cell renderer and editor should be allowed to overflow into adjacent cells. Default value is true. New in 2.4.x
bool GetOverflow() -- return current state of the overflow flag. New in 2.4.x
Example
This table-based example generates an attribute object only if the cell being edited is read-only. Returning a non-null attribute overshadows the default attribute's properties, making the cell read-only. Returning None (null) doesn't overshadow any of the default attribute's properties.
{{{class BasePropertyTable( wxPyGridTableBase):
def GetAttr(self, row, col, someExtraParameter ):
property = self.GetPropertyForCoordinate( row, col ) object = self.GetObjectForCoordinate( row, col ) if property.ReadOnly( object ):
- attr = wxGridCellAttr()
attr.SetReadOnly( 1 ) return attr
- attr = wxGridCellAttr()
}}}
This example is from the wxPython demo, showing use in rendering code.
class MyCustomRenderer(wxPyGridCellRenderer): def Draw(self, grid, attr, dc, rect, row, col, isSelected): dc.SetBackgroundMode(wxSOLID) dc.SetBrush(wxBrush(attr.GetBackgroundColour(), wxSOLID)) dc.SetPen(wxTRANSPARENT_PEN) dc.DrawRectangle(rect.x, rect.y, rect.width, rect.height) dc.SetBackgroundMode(wxTRANSPARENT) dc.SetFont(attr.GetFont()) text = grid.GetCellValue(row, col) colors = [wxRED, wxWHITE, wxCYAN] x = rect.x + 1 y = rect.y + 1 for ch in text: dc.SetTextForeground(random.choice(colors)) dc.DrawText(ch, x, y) w, h = dc.GetTextExtent(ch) x = x + w if x > rect.right - 5: break