+void wxGridCellStringRenderer::SetTextColoursAndFont(wxGrid& grid,
+ wxGridCellAttr& attr,
+ wxDC& dc,
+ bool isSelected)
+{
+ dc.SetBackgroundMode( wxTRANSPARENT );
+
+ // TODO some special colours for attr.IsReadOnly() case?
+
+ // different coloured text when the grid is disabled
+ if( grid.IsEnabled() )
+ {
+ if ( isSelected )
+ {
+ dc.SetTextBackground( grid.GetSelectionBackground() );
+ dc.SetTextForeground( grid.GetSelectionForeground() );
+ }
+ else
+ {
+ dc.SetTextBackground( attr.GetBackgroundColour() );
+ dc.SetTextForeground( attr.GetTextColour() );
+ }
+ }
+ else
+ {
+ dc.SetTextBackground(wxSystemSettings::GetSystemColour(wxSYS_COLOUR_BTNFACE));
+ dc.SetTextForeground(wxSystemSettings::GetSystemColour(wxSYS_COLOUR_GRAYTEXT));
+ }
+
+ dc.SetFont( attr.GetFont() );
+}
+
+wxSize wxGridCellStringRenderer::DoGetBestSize(wxGridCellAttr& attr,
+ wxDC& dc,
+ const wxString& text)
+{
+ wxCoord x = 0, y = 0, max_x = 0;
+ dc.SetFont(attr.GetFont());
+ wxStringTokenizer tk(text, _T('\n'));
+ while ( tk.HasMoreTokens() )
+ {
+ dc.GetTextExtent(tk.GetNextToken(), &x, &y);
+ max_x = wxMax(max_x, x);
+ }
+
+ y *= 1 + text.Freq(wxT('\n')); // multiply by the number of lines.
+
+ return wxSize(max_x, y);
+}
+
+wxSize wxGridCellStringRenderer::GetBestSize(wxGrid& grid,
+ wxGridCellAttr& attr,
+ wxDC& dc,
+ int row, int col)
+{
+ return DoGetBestSize(attr, dc, grid.GetCellValue(row, col));
+}
+
+void wxGridCellStringRenderer::Draw(wxGrid& grid,
+ wxGridCellAttr& attr,
+ wxDC& dc,
+ const wxRect& rectCell,
+ int row, int col,
+ bool isSelected)
+{
+ wxRect rect = rectCell;
+ rect.Inflate(-1);
+
+ // erase only this cells background, overflow cells should have been erased
+ wxGridCellRenderer::Draw(grid, attr, dc, rectCell, row, col, isSelected);
+
+ int hAlign, vAlign;
+ attr.GetAlignment(&hAlign, &vAlign);
+
+ int overflowCols = 0;
+
+ if (attr.GetOverflow())
+ {
+ int cols = grid.GetNumberCols();
+ int best_width = GetBestSize(grid,attr,dc,row,col).GetWidth();
+ int cell_rows, cell_cols;
+ attr.GetSize( &cell_rows, &cell_cols ); // shouldn't get here if <=0
+ if ((best_width > rectCell.width) && (col < cols) && grid.GetTable())
+ {
+ int i, c_cols, c_rows;
+ for (i = col+cell_cols; i < cols; i++)
+ {
+ bool is_empty = TRUE;
+ for (int j=row; j<row+cell_rows; j++)
+ {
+ // check w/ anchor cell for multicell block
+ grid.GetCellSize(j, i, &c_rows, &c_cols);
+ if (c_rows > 0) c_rows = 0;
+ if (!grid.GetTable()->IsEmptyCell(j+c_rows, i))
+ {
+ is_empty = FALSE;
+ break;
+ }
+ }
+ if (is_empty)
+ rect.width += grid.GetColSize(i);
+ else
+ {
+ i--;
+ break;
+ }
+ if (rect.width >= best_width) break;
+ }
+ overflowCols = i - col - cell_cols + 1;
+ if (overflowCols >= cols) overflowCols = cols - 1;
+ }
+
+ if (overflowCols > 0) // redraw overflow cells w/ proper hilight
+ {
+ hAlign = wxALIGN_LEFT; // if oveflowed then it's left aligned
+ wxRect clip = rect;
+ clip.x += rectCell.width;
+ // draw each overflow cell individually
+ int col_end = col+cell_cols+overflowCols;
+ if (col_end >= grid.GetNumberCols())
+ col_end = grid.GetNumberCols() - 1;
+ for (int i = col+cell_cols; i <= col_end; i++)
+ {
+ clip.width = grid.GetColSize(i) - 1;
+ dc.DestroyClippingRegion();
+ dc.SetClippingRegion(clip);
+
+ SetTextColoursAndFont(grid, attr, dc,
+ grid.IsInSelection(row,i));
+
+ grid.DrawTextRectangle(dc, grid.GetCellValue(row, col),
+ rect, hAlign, vAlign);
+ clip.x += grid.GetColSize(i) - 1;
+ }
+
+ rect = rectCell;
+ rect.Inflate(-1);
+ rect.width++;
+ dc.DestroyClippingRegion();
+ }
+ }
+
+ // now we only have to draw the text
+ SetTextColoursAndFont(grid, attr, dc, isSelected);
+
+ grid.DrawTextRectangle(dc, grid.GetCellValue(row, col),
+ rect, hAlign, vAlign);
+}
+
+// ----------------------------------------------------------------------------
+// wxGridCellNumberRenderer
+// ----------------------------------------------------------------------------
+
+wxString wxGridCellNumberRenderer::GetString(wxGrid& grid, int row, int col)
+{
+ wxGridTableBase *table = grid.GetTable();
+ wxString text;
+ if ( table->CanGetValueAs(row, col, wxGRID_VALUE_NUMBER) )
+ {
+ text.Printf(_T("%ld"), table->GetValueAsLong(row, col));
+ }
+ else
+ {
+ text = table->GetValue(row, col);
+ }
+
+ return text;
+}
+
+void wxGridCellNumberRenderer::Draw(wxGrid& grid,
+ wxGridCellAttr& attr,
+ wxDC& dc,
+ const wxRect& rectCell,
+ int row, int col,
+ bool isSelected)
+{
+ wxGridCellRenderer::Draw(grid, attr, dc, rectCell, row, col, isSelected);
+
+ SetTextColoursAndFont(grid, attr, dc, isSelected);
+
+ // draw the text right aligned by default
+ int hAlign, vAlign;
+ attr.GetAlignment(&hAlign, &vAlign);
+ hAlign = wxALIGN_RIGHT;
+
+ wxRect rect = rectCell;
+ rect.Inflate(-1);
+
+ grid.DrawTextRectangle(dc, GetString(grid, row, col), rect, hAlign, vAlign);
+}
+
+wxSize wxGridCellNumberRenderer::GetBestSize(wxGrid& grid,
+ wxGridCellAttr& attr,
+ wxDC& dc,
+ int row, int col)
+{
+ return DoGetBestSize(attr, dc, GetString(grid, row, col));
+}
+
+// ----------------------------------------------------------------------------
+// wxGridCellFloatRenderer
+// ----------------------------------------------------------------------------
+
+wxGridCellFloatRenderer::wxGridCellFloatRenderer(int width, int precision)
+{
+ SetWidth(width);
+ SetPrecision(precision);
+}
+
+wxGridCellRenderer *wxGridCellFloatRenderer::Clone() const
+{
+ wxGridCellFloatRenderer *renderer = new wxGridCellFloatRenderer;
+ renderer->m_width = m_width;
+ renderer->m_precision = m_precision;
+ renderer->m_format = m_format;
+
+ return renderer;
+}
+
+wxString wxGridCellFloatRenderer::GetString(wxGrid& grid, int row, int col)
+{
+ wxGridTableBase *table = grid.GetTable();
+
+ bool hasDouble;
+ double val;
+ wxString text;
+ if ( table->CanGetValueAs(row, col, wxGRID_VALUE_FLOAT) )
+ {
+ val = table->GetValueAsDouble(row, col);
+ hasDouble = TRUE;
+ }
+ else
+ {
+ text = table->GetValue(row, col);
+ hasDouble = text.ToDouble(&val);
+ }
+
+ if ( hasDouble )
+ {
+ if ( !m_format )
+ {
+ if ( m_width == -1 )
+ {
+ if ( m_precision == -1 )
+ {
+ // default width/precision
+ m_format = _T("%f");
+ }
+ else
+ {
+ m_format.Printf(_T("%%.%df"), m_precision);
+ }
+ }
+ else if ( m_precision == -1 )
+ {
+ // default precision
+ m_format.Printf(_T("%%%d.f"), m_width);
+ }
+ else
+ {
+ m_format.Printf(_T("%%%d.%df"), m_width, m_precision);
+ }
+ }
+
+ text.Printf(m_format, val);
+
+ }
+ //else: text already contains the string
+
+ return text;
+}
+
+void wxGridCellFloatRenderer::Draw(wxGrid& grid,
+ wxGridCellAttr& attr,
+ wxDC& dc,
+ const wxRect& rectCell,
+ int row, int col,
+ bool isSelected)
+{
+ wxGridCellRenderer::Draw(grid, attr, dc, rectCell, row, col, isSelected);
+
+ SetTextColoursAndFont(grid, attr, dc, isSelected);
+
+ // draw the text right aligned by default
+ int hAlign, vAlign;
+ attr.GetAlignment(&hAlign, &vAlign);
+ hAlign = wxALIGN_RIGHT;
+
+ wxRect rect = rectCell;
+ rect.Inflate(-1);
+
+ grid.DrawTextRectangle(dc, GetString(grid, row, col), rect, hAlign, vAlign);
+}
+
+wxSize wxGridCellFloatRenderer::GetBestSize(wxGrid& grid,
+ wxGridCellAttr& attr,
+ wxDC& dc,
+ int row, int col)
+{
+ return DoGetBestSize(attr, dc, GetString(grid, row, col));
+}
+
+void wxGridCellFloatRenderer::SetParameters(const wxString& params)
+{
+ if ( !params )
+ {
+ // reset to defaults
+ SetWidth(-1);
+ SetPrecision(-1);
+ }
+ else
+ {
+ wxString tmp = params.BeforeFirst(_T(','));
+ if ( !!tmp )
+ {
+ long width;
+ if ( tmp.ToLong(&width) )
+ {
+ SetWidth((int)width);
+ }
+ else
+ {
+ wxLogDebug(_T("Invalid wxGridCellFloatRenderer width parameter string '%s ignored"), params.c_str());
+ }
+
+ }
+ tmp = params.AfterFirst(_T(','));
+ if ( !!tmp )
+ {
+ long precision;
+ if ( tmp.ToLong(&precision) )
+ {
+ SetPrecision((int)precision);
+ }
+ else
+ {
+ wxLogDebug(_T("Invalid wxGridCellFloatRenderer precision parameter string '%s ignored"), params.c_str());
+ }
+
+ }
+ }
+}
+
+
+// ----------------------------------------------------------------------------
+// wxGridCellBoolRenderer
+// ----------------------------------------------------------------------------
+
+wxSize wxGridCellBoolRenderer::ms_sizeCheckMark;
+
+// FIXME these checkbox size calculations are really ugly...
+
+// between checkmark and box
+static const wxCoord wxGRID_CHECKMARK_MARGIN = 2;
+
+wxSize wxGridCellBoolRenderer::GetBestSize(wxGrid& grid,
+ wxGridCellAttr& WXUNUSED(attr),
+ wxDC& WXUNUSED(dc),
+ int WXUNUSED(row),
+ int WXUNUSED(col))
+{
+ // compute it only once (no locks for MT safeness in GUI thread...)
+ if ( !ms_sizeCheckMark.x )
+ {
+ // get checkbox size
+ wxCoord checkSize = 0;
+ wxCheckBox *checkbox = new wxCheckBox(&grid, -1, wxEmptyString);
+ wxSize size = checkbox->GetBestSize();
+ checkSize = size.y + 2*wxGRID_CHECKMARK_MARGIN;
+
+ // FIXME wxGTK::wxCheckBox::GetBestSize() gives "wrong" result
+#if defined(__WXGTK__) || defined(__WXMOTIF__)
+ checkSize -= size.y / 2;
+#endif
+
+ delete checkbox;
+
+ ms_sizeCheckMark.x = ms_sizeCheckMark.y = checkSize;
+ }
+
+ return ms_sizeCheckMark;
+}
+
+void wxGridCellBoolRenderer::Draw(wxGrid& grid,
+ wxGridCellAttr& attr,
+ wxDC& dc,
+ const wxRect& rect,
+ int row, int col,
+ bool isSelected)
+{
+ wxGridCellRenderer::Draw(grid, attr, dc, rect, row, col, isSelected);
+
+ // draw a check mark in the centre (ignoring alignment - TODO)
+ wxSize size = GetBestSize(grid, attr, dc, row, col);
+
+ // don't draw outside the cell
+ wxCoord minSize = wxMin(rect.width, rect.height);
+ if ( size.x >= minSize || size.y >= minSize )
+ {
+ // and even leave (at least) 1 pixel margin
+ size.x = size.y = minSize - 2;
+ }
+
+ // draw a border around checkmark
+ int vAlign, hAlign;
+ attr.GetAlignment(& hAlign, &vAlign);
+
+ wxRect rectBorder;
+ if (hAlign == wxALIGN_CENTRE)
+ {
+ rectBorder.x = rect.x + rect.width/2 - size.x/2;
+ rectBorder.y = rect.y + rect.height/2 - size.y/2;
+ rectBorder.width = size.x;
+ rectBorder.height = size.y;
+ }
+ else if (hAlign == wxALIGN_LEFT)
+ {
+ rectBorder.x = rect.x + 2;
+ rectBorder.y = rect.y + rect.height/2 - size.y/2;
+ rectBorder.width = size.x;
+ rectBorder.height = size.y;
+ }
+ else if (hAlign == wxALIGN_RIGHT)
+ {
+ rectBorder.x = rect.x + rect.width - size.x - 2;
+ rectBorder.y = rect.y + rect.height/2 - size.y/2;
+ rectBorder.width = size.x;
+ rectBorder.height = size.y;
+ }
+
+ bool value;
+ if ( grid.GetTable()->CanGetValueAs(row, col, wxGRID_VALUE_BOOL) )
+ value = grid.GetTable()->GetValueAsBool(row, col);
+ else
+ {
+ wxString cellval( grid.GetTable()->GetValue(row, col) );
+ value = !( !cellval || (cellval == wxT("0")) );
+ }
+
+ if ( value )
+ {
+ wxRect rectMark = rectBorder;
+#ifdef __WXMSW__
+ // MSW DrawCheckMark() is weird (and should probably be changed...)
+ rectMark.Inflate(-wxGRID_CHECKMARK_MARGIN/2);
+ rectMark.x++;
+ rectMark.y++;
+#else // !MSW
+ rectMark.Inflate(-wxGRID_CHECKMARK_MARGIN);
+#endif // MSW/!MSW
+
+ dc.SetTextForeground(attr.GetTextColour());
+ dc.DrawCheckMark(rectMark);
+ }
+
+ dc.SetBrush(*wxTRANSPARENT_BRUSH);
+ dc.SetPen(wxPen(attr.GetTextColour(), 1, wxSOLID));
+ dc.DrawRectangle(rectBorder);
+}
+
+// ----------------------------------------------------------------------------
+// wxGridCellAttr
+// ----------------------------------------------------------------------------
+
+void wxGridCellAttr::Init(wxGridCellAttr *attrDefault)
+{
+ m_nRef = 1;
+
+ m_isReadOnly = Unset;
+
+ m_renderer = NULL;
+ m_editor = NULL;
+
+ m_attrkind = wxGridCellAttr::Cell;
+
+ m_sizeRows = m_sizeCols = 1;
+ m_overflow = TRUE;
+
+ SetDefAttr(attrDefault);
+}
+
+wxGridCellAttr *wxGridCellAttr::Clone() const