wxGridCellAttr::Clone() added to allow the demo of custom grid cell attr provider to work

git-svn-id: https://svn.wxwidgets.org/svn/wx/wxWidgets/trunk@6339 c3d73ce0-8a6f-49c7-b76d-6d57e0e08775
This commit is contained in:
Vadim Zeitlin 2000-02-28 19:08:01 +00:00
parent 3f5513f5bb
commit a68c1246db
4 changed files with 116 additions and 4 deletions

View File

@ -435,6 +435,7 @@ private:
wxArrayString m_choices;
bool m_allowOthers;
};
// ----------------------------------------------------------------------------
// wxGridCellAttr: this class can be used to alter the cells appearance in
// the grid by changing their colour/font/... from default. An object of this
@ -464,7 +465,10 @@ public:
SetAlignment(hAlign, vAlign);
}
// default copy ctor ok
// creates a new copy of this object: warning, this is destructive copy
// (this is why it's non const), the renderer and editor are "given to"
// the new object
wxGridCellAttr *Clone();
// this class is ref counted: it is created with ref count of 1, so
// calling DecRef() once will delete it. Calling IncRef() allows to lock
@ -540,6 +544,9 @@ private:
bool m_isReadOnly;
// use Clone() instead
DECLARE_NO_COPY_CLASS(wxGridCellAttr);
// suppress the stupid gcc warning about the class having private dtor and
// no friends
friend class wxGridCellAttrDummyFriend;

View File

@ -698,10 +698,50 @@ void MyGridCellRenderer::Draw(wxGrid& grid,
dc.DrawEllipse(rect);
}
// ----------------------------------------------------------------------------
// MyGridCellAttrProvider
// ----------------------------------------------------------------------------
// ----------------------------------------------------------------------------
MyGridCellAttrProvider::MyGridCellAttrProvider()
{
m_attrForOddRows = new wxGridCellAttr;
m_attrForOddRows->SetBackgroundColour(*wxLIGHT_GREY);
}
MyGridCellAttrProvider::~MyGridCellAttrProvider()
{
m_attrForOddRows->DecRef();
}
wxGridCellAttr *MyGridCellAttrProvider::GetAttr(int row, int col) const
{
wxGridCellAttr *attr = wxGridCellAttrProvider::GetAttr(row, col);
if ( row % 2 )
{
if ( !attr )
{
attr = m_attrForOddRows;
attr->IncRef();
}
else
{
if ( !attr->HasBackgroundColour() )
{
wxGridCellAttr *attrNew = attr->Clone();
attr->DecRef();
attr = attrNew;
attr->SetBackgroundColour(*wxLIGHT_GREY);
}
}
}
return attr;
}
// ============================================================================
// BigGridFrame and BigGridTable: Sample of a non-standard table
// ----------------------------------------------------------------------------
// ============================================================================
BigGridFrame::BigGridFrame(long sizeGrid)
: wxFrame(NULL, -1, "Plugin Virtual Table",
@ -709,6 +749,11 @@ BigGridFrame::BigGridFrame(long sizeGrid)
{
m_grid = new wxGrid(this, -1, wxDefaultPosition, wxDefaultSize);
m_table = new BigGridTable(sizeGrid);
// VZ: I don't understand why this slows down the display that much,
// must profile it...
//m_table->SetAttrProvider(new MyGridCellAttrProvider);
m_grid->SetTable(m_table, TRUE);
#if defined __WXMOTIF__
@ -719,8 +764,12 @@ BigGridFrame::BigGridFrame(long sizeGrid)
#endif
}
// ----------------------------------------------------------------------------
// ============================================================================
// BugsGridFrame: a "realistic" table
// ============================================================================
// ----------------------------------------------------------------------------
// bugs table data
// ----------------------------------------------------------------------------
enum Columns
@ -789,6 +838,10 @@ static const wxChar *headers[Col_Max] =
_T("Opened?"),
};
// ----------------------------------------------------------------------------
// BugsGridTable
// ----------------------------------------------------------------------------
wxString BugsGridTable::GetTypeName(int WXUNUSED(row), int col)
{
switch ( col )
@ -992,12 +1045,17 @@ BugsGridTable::BugsGridTable()
{
}
// ----------------------------------------------------------------------------
// BugsGridFrame
// ----------------------------------------------------------------------------
BugsGridFrame::BugsGridFrame()
: wxFrame(NULL, -1, "Bugs table",
wxDefaultPosition, wxSize(500, 300))
{
wxGrid *grid = new wxGrid(this, -1, wxDefaultPosition);
wxGridTableBase *table = new BugsGridTable();
table->SetAttrProvider(new MyGridCellAttrProvider);
grid->SetTable(table, TRUE);
wxGridCellAttr *attrRO = new wxGridCellAttr,

View File

@ -158,6 +158,22 @@ private:
BigGridTable* m_table;
};
// ----------------------------------------------------------------------------
// an example of custom attr provider: this one makes all odd rows appear grey
// ----------------------------------------------------------------------------
class MyGridCellAttrProvider : public wxGridCellAttrProvider
{
public:
MyGridCellAttrProvider();
virtual ~MyGridCellAttrProvider();
virtual wxGridCellAttr *GetAttr(int row, int col) const;
private:
wxGridCellAttr *m_attrForOddRows;
};
// ----------------------------------------------------------------------------
// another, more realistic, grid example: shows typed columns and more
// ----------------------------------------------------------------------------

View File

@ -1354,6 +1354,37 @@ void wxGridCellBoolRenderer::Draw(wxGrid& grid,
// wxGridCellAttr
// ----------------------------------------------------------------------------
wxGridCellAttr *wxGridCellAttr::Clone()
{
wxGridCellAttr *attr = new wxGridCellAttr;
if ( HasTextColour() )
attr->SetTextColour(GetTextColour());
if ( HasBackgroundColour() )
attr->SetBackgroundColour(GetBackgroundColour());
if ( HasFont() )
attr->SetFont(GetFont());
if ( HasAlignment() )
attr->SetAlignment(m_hAlign, m_vAlign);
if ( m_renderer )
{
attr->SetRenderer(m_renderer);
m_renderer = NULL;
}
if ( m_editor )
{
attr->SetEditor(m_editor);
m_editor = NULL;
}
if ( IsReadOnly() )
attr->SetReadOnly();
attr->SetDefAttr(m_defGridAttr);
return attr;
}
const wxColour& wxGridCellAttr::GetTextColour() const
{
if (HasTextColour())