// ctors
wxGridCellAttr()
{
+ Init();
SetAlignment(0, 0);
}
int vAlign)
: m_colText(colText), m_colBack(colBack), m_font(font)
{
+ Init();
SetAlignment(hAlign, vAlign);
}
// default copy ctor ok
+ // 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
+ // it until the matching DecRef() is called
+ void IncRef() { m_nRef++; }
+ void DecRef() { if ( !--m_nRef ) delete this; }
+ void SafeDecRef() { if ( this ) DecRef(); }
+
// setters
void SetTextColour(const wxColour& colText) { m_colText = colText; }
void SetBackgroundColour(const wxColour& colBack) { m_colBack = colBack; }
}
private:
+ // the common part of all ctors
+ void Init() { m_nRef = 1; }
+
+ // the dtor is private because only DecRef() can delete us
+ ~wxGridCellAttr() { }
+
+ // the ref count - when it goes to 0, we die
+ size_t m_nRef;
+
wxColour m_colText,
m_colBack;
wxFont m_font;
int m_hAlign,
m_vAlign;
+
+ // suppress the stupid gcc warning about the class having private dtor and
+ // no friends
+ friend class wxGridCellAttrDummyFriend;
};
// ----------------------------------------------------------------------------
wxGridCellAttrProvider();
virtual ~wxGridCellAttrProvider();
+ // DecRef() must be called on the returned pointer
virtual wxGridCellAttr *GetAttr(int row, int col) const;
- virtual void SetAttr(const wxGridCellAttr *attr, int row, int col);
+
+ // takes ownership of the pointer, don't call DecRef() on it
+ virtual void SetAttr(wxGridCellAttr *attr, int row, int col);
private:
void InitData();
virtual wxGridCellAttr *GetAttr( int row, int col );
// takes ownership of the pointer
- virtual void SetAttr(const wxGridCellAttr *attr, int row, int col );
+ virtual void SetAttr(wxGridCellAttr *attr, int row, int col );
private:
wxGrid * m_view;
// do we have some place to store attributes in?
bool CanHaveAttributes();
+ // returns the attribute we may modify in place: a new one if this cell
+ // doesn't have any yet or the existing one if it does
+ //
+ // DecRef() must be called on the returned pointer, as usual
+ wxGridCellAttr *GetCellAttr(int row, int col) const;
+
wxGridCellCoordsArray m_cellsExposed;
wxArrayInt m_rowsExposed;
wxArrayInt m_colsExposed;
struct wxGridCellWithAttr
{
- wxGridCellWithAttr(int row, int col, const wxGridCellAttr *pAttr)
- : coords(row, col), attr(*pAttr)
+ wxGridCellWithAttr(int row, int col, wxGridCellAttr *attr_)
+ : coords(row, col), attr(attr_)
{
}
+ ~wxGridCellWithAttr()
+ {
+ attr->DecRef();
+ }
+
wxGridCellCoords coords;
- wxGridCellAttr attr;
+ wxGridCellAttr *attr;
};
WX_DECLARE_OBJARRAY(wxGridCellWithAttr, wxGridCellWithAttrArray);
class WXDLLEXPORT wxGridCellAttrProviderData
{
public:
- void SetAttr(const wxGridCellAttr *attr, int row, int col);
+ void SetAttr(wxGridCellAttr *attr, int row, int col);
wxGridCellAttr *GetAttr(int row, int col) const;
private:
// wxGridCellAttrProviderData
// ----------------------------------------------------------------------------
-void wxGridCellAttrProviderData::SetAttr(const wxGridCellAttr *attr,
+void wxGridCellAttrProviderData::SetAttr(wxGridCellAttr *attr,
int row, int col)
{
int n = FindIndex(row, col);
if ( attr )
{
// change the attribute
- m_attrs[(size_t)n].attr = *attr;
+ m_attrs[(size_t)n].attr = attr;
}
else
{
m_attrs.RemoveAt((size_t)n);
}
}
-
- delete attr;
}
wxGridCellAttr *wxGridCellAttrProviderData::GetAttr(int row, int col) const
int n = FindIndex(row, col);
if ( n != wxNOT_FOUND )
{
- attr = new wxGridCellAttr(m_attrs[(size_t)n].attr);
+ attr = m_attrs[(size_t)n].attr;
+ attr->IncRef();
}
return attr;
return m_data ? m_data->GetAttr(row, col) : (wxGridCellAttr *)NULL;
}
-void wxGridCellAttrProvider::SetAttr(const wxGridCellAttr *attr,
+void wxGridCellAttrProvider::SetAttr(wxGridCellAttr *attr,
int row, int col)
{
if ( !m_data )
return (wxGridCellAttr *)NULL;
}
-void wxGridTableBase::SetAttr(const wxGridCellAttr *attr, int row, int col )
+void wxGridTableBase::SetAttr(wxGridCellAttr *attr, int row, int col )
{
if ( m_attrProvider )
{
{
// as we take ownership of the pointer and don't store it, we must
// free it now
- delete attr;
+ attr->SafeDecRef();
}
}
return m_colWidths[col];
}
+// ============================================================================
+// access to the grid attributes: each of them has a default value in the grid
+// itself and may be overidden on a per-cell basis
+// ============================================================================
+
+// ----------------------------------------------------------------------------
+// setting default attributes
+// ----------------------------------------------------------------------------
+
+void wxGrid::SetDefaultCellBackgroundColour( const wxColour& col )
+{
+ m_gridWin->SetBackgroundColour(col);
+}
+
+void wxGrid::SetDefaultCellTextColour( const wxColour& col )
+{
+ m_gridWin->SetForegroundColour(col);
+}
+
+void wxGrid::SetDefaultCellAlignment( int horiz, int vert )
+{
+ m_defaultCellHAlign = horiz;
+ m_defaultCellVAlign = vert;
+}
+
+void wxGrid::SetDefaultCellFont( const wxFont& font )
+{
+ m_defaultCellFont = font;
+}
+
+// ----------------------------------------------------------------------------
+// access to the default attrbiutes
+// ----------------------------------------------------------------------------
+
wxColour wxGrid::GetDefaultCellBackgroundColour()
{
return m_gridWin->GetBackgroundColour();
}
-// TODO VZ: this must be optimized to allow only retrieveing attr once!
+wxColour wxGrid::GetDefaultCellTextColour()
+{
+ return m_gridWin->GetForegroundColour();
+}
+
+wxFont wxGrid::GetDefaultCellFont()
+{
+ return m_defaultCellFont;
+}
+
+void wxGrid::GetDefaultCellAlignment( int *horiz, int *vert )
+{
+ if ( horiz )
+ *horiz = m_defaultCellHAlign;
+ if ( vert )
+ *vert = m_defaultCellVAlign;
+}
+
+// ----------------------------------------------------------------------------
+// access to cell attributes
+// ----------------------------------------------------------------------------
+
+// TODO VZ: we must cache the attr to allow only retrieveing it once!
wxColour wxGrid::GetCellBackgroundColour(int row, int col)
{
else
colour = GetDefaultCellBackgroundColour();
- delete attr;
+ attr->SafeDecRef();
return colour;
}
-wxColour wxGrid::GetDefaultCellTextColour()
-{
- return m_gridWin->GetForegroundColour();
-}
-
wxColour wxGrid::GetCellTextColour( int row, int col )
{
wxGridCellAttr *attr = m_table ? m_table->GetAttr(row, col) : NULL;
else
colour = GetDefaultCellTextColour();
- delete attr;
+ attr->SafeDecRef();
return colour;
}
-
-wxFont wxGrid::GetDefaultCellFont()
-{
- return m_defaultCellFont;
-}
-
wxFont wxGrid::GetCellFont( int row, int col )
{
wxGridCellAttr *attr = m_table ? m_table->GetAttr(row, col) : NULL;
else
font = GetDefaultCellFont();
- delete attr;
+ attr->SafeDecRef();
return font;
}
-void wxGrid::GetDefaultCellAlignment( int *horiz, int *vert )
-{
- if ( horiz )
- *horiz = m_defaultCellHAlign;
- if ( vert )
- *vert = m_defaultCellVAlign;
-}
-
void wxGrid::GetCellAlignment( int row, int col, int *horiz, int *vert )
{
wxGridCellAttr *attr = m_table ? m_table->GetAttr(row, col) : NULL;
else
GetDefaultCellAlignment(horiz, vert);
- delete attr;
+ attr->SafeDecRef();
+}
+
+// ----------------------------------------------------------------------------
+// setting cell attributes: this is forwarded to the table
+// ----------------------------------------------------------------------------
+
+bool wxGrid::CanHaveAttributes()
+{
+ if ( !m_table )
+ {
+ return FALSE;
+ }
+
+ if ( !m_table->GetAttrProvider() )
+ {
+ // use the default attr provider by default
+ // (another choice would be to just return FALSE thus forcing the user
+ // to it himself)
+ m_table->SetAttrProvider(new wxGridCellAttrProvider);
+ }
+
+ return TRUE;
+}
+
+wxGridCellAttr *wxGrid::GetCellAttr(int row, int col) const
+{
+ wxGridCellAttr *attr = m_table->GetAttr(row, col);
+ if ( !attr )
+ {
+ attr = new wxGridCellAttr;
+
+ // artificially inc the ref count to match DecRef() in caller
+ attr->IncRef();
+
+ m_table->SetAttr(attr, row, col);
+ }
+
+ return attr;
+}
+
+void wxGrid::SetCellBackgroundColour( int row, int col, const wxColour& colour )
+{
+ if ( CanHaveAttributes() )
+ {
+ wxGridCellAttr *attr = GetCellAttr(row, col);
+ attr->SetBackgroundColour(colour);
+ attr->DecRef();
+ }
+}
+
+void wxGrid::SetCellTextColour( int row, int col, const wxColour& colour )
+{
+ if ( CanHaveAttributes() )
+ {
+ wxGridCellAttr *attr = GetCellAttr(row, col);
+ attr->SetTextColour(colour);
+ attr->DecRef();
+ }
+}
+
+void wxGrid::SetCellFont( int row, int col, const wxFont& font )
+{
+ if ( CanHaveAttributes() )
+ {
+ wxGridCellAttr *attr = GetCellAttr(row, col);
+ attr->SetFont(font);
+ attr->DecRef();
+ }
+}
+
+void wxGrid::SetCellAlignment( int row, int col, int horiz, int vert )
+{
+ if ( CanHaveAttributes() )
+ {
+ wxGridCellAttr *attr = GetCellAttr(row, col);
+ attr->SetAlignment(horiz, vert);
+ attr->DecRef();
+ }
}
+// ----------------------------------------------------------------------------
+// row/col size
+// ----------------------------------------------------------------------------
+
void wxGrid::SetDefaultRowSize( int height, bool resizeExistingRows )
{
m_defaultRowHeight = wxMax( height, WXGRID_MIN_ROW_HEIGHT );
CalcDimensions();
}
-void wxGrid::SetDefaultCellBackgroundColour( const wxColour& col )
-{
- m_gridWin->SetBackgroundColour(col);
-}
-
-void wxGrid::SetDefaultCellTextColour( const wxColour& col )
-{
- m_gridWin->SetForegroundColour(col);
-}
-
-void wxGrid::SetDefaultCellAlignment( int horiz, int vert )
-{
- m_defaultCellHAlign = horiz;
- m_defaultCellVAlign = vert;
-}
-
-bool wxGrid::CanHaveAttributes()
-{
- if ( !m_table )
- {
- return FALSE;
- }
-
- if ( !m_table->GetAttrProvider() )
- {
- // use the default attr provider by default
- // (another choice would be to just return FALSE thus forcing the user
- // to it himself)
- m_table->SetAttrProvider(new wxGridCellAttrProvider);
- }
-
- return TRUE;
-}
-
-void wxGrid::SetCellBackgroundColour( int row, int col, const wxColour& colour )
-{
- if ( CanHaveAttributes() )
- {
- wxGridCellAttr *attr = new wxGridCellAttr;
- attr->SetBackgroundColour(colour);
-
- m_table->SetAttr(attr, row, col);
- }
-}
-
-void wxGrid::SetCellTextColour( int row, int col, const wxColour& colour )
-{
- if ( CanHaveAttributes() )
- {
- wxGridCellAttr *attr = new wxGridCellAttr;
- attr->SetTextColour(colour);
-
- m_table->SetAttr(attr, row, col);
- }
-}
-
-void wxGrid::SetDefaultCellFont( const wxFont& font )
-{
- m_defaultCellFont = font;
-}
-
-void wxGrid::SetCellFont( int row, int col, const wxFont& font )
-{
- if ( CanHaveAttributes() )
- {
- wxGridCellAttr *attr = new wxGridCellAttr;
- attr->SetFont(font);
-
- m_table->SetAttr(attr, row, col);
- }
-}
-
-void wxGrid::SetCellAlignment( int row, int col, int horiz, int vert )
-{
- if ( CanHaveAttributes() )
- {
- wxGridCellAttr *attr = new wxGridCellAttr;
- attr->SetAlignment(horiz, vert);
-
- m_table->SetAttr(attr, row, col);
- }
-}
-
-
//
// ------ cell value accessor functions