Add wxGridCellHtmlRenderer to wxGrid

46 views
Skip to first unread message

Iwbnwif Yiw

unread,
Apr 26, 2014, 4:48:52 PM4/26/14
to wx-...@googlegroups.com
Sorry to be filling up the list this evening! (hopefully its not pollution...)

I recently had a need to display slightly rich text in a wxGrid. This seems to be something that is requested occasionally but not yet implemented AFAIK.

Following Vadim's suggestion I found it was a very simple implementation to do using the neat wxHTML helper class wxHtmlDCRenderer.

Here is the main part:

#include <wx/html/winpars.h>
#include <wx/html/htmprint.h>

void
myGridCellHtmlRenderer::Draw(wxGrid& grid, 
                                wxGridCellAttr& attr, 
                                wxDC& dc, 
                                const wxRect& rect, 
                                int row, int col, 
                                bool isSelected)
{
    // call base class ::Draw to clear the cell and draw the borders etc.
    wxGridCellRenderer::Draw(grid, attr, dc, rect, row, col, isSelected);
    
    wxHtmlDCRenderer renderer;
    wxFont font = grid.GetCellFont(row, col);

    renderer.SetDC (&dc);
    renderer.SetStandardFonts(font.GetPointSize(), 
                                        font.GetFaceName(), wxEmptyString);
    renderer.SetSize (grid.GetColSize(col) - 4, grid.GetRowSize(row));
    renderer.SetHtmlText (grid.GetCellValue(row, col));
    wxArrayInt value = 0;
    
    renderer.Render (rect.x + 2, rect.y + 2, value);
}

wxSize
myGridCellHtmlRenderer::GetBestSize(wxGrid& grid, 
                                        wxGridCellAttr& attr, 
                                        wxDC& dc, 
                                        int row, int col)
{
    wxHtmlDCRenderer renderer;
    wxFont font = grid.GetCellFont(row, col);
    
    renderer.SetDC (&dc);
    renderer.SetStandardFonts(font.GetPointSize(), 
                                        font.GetFaceName(), wxEmptyString);
    renderer.SetSize (grid.GetColSize(col), grid.GetRowSize(row));
    renderer.SetHtmlText (grid.GetCellValue(row, col));
    
    return wxSize (grid.GetColSize(col) - 4, renderer.GetTotalHeight() + 4);
}

It seems to perform very well in my tests even with more complex elements such as tables, nested ordered and unordered lists. Although I have only been working with relatively short HTML fragments.

Would this be something worth adding to the standard wxGrid?

Vadim Zeitlin

unread,
Apr 26, 2014, 8:00:40 PM4/26/14
to wx-...@googlegroups.com
On Sat, 26 Apr 2014 13:48:52 -0700 (PDT) Iwbnwif Yiw wrote:

IY> Following Vadim's suggestion<https://groups.google.com/forum/?fromgroups#!topic/wx-users/LmuYNtC93qM> I
IY> found it was a very simple implementation to do using the neat wxHTML
IY> helper class wxHtmlDCRenderer.

Glad to hear it!

IY> Would this be something worth adding to the standard wxGrid?

The main problem with adding this would be that naively adding it to wxAdv
library would create a dependency of it on wxHTML which is currently not
required by it. And wxHTML currently doesn't require wxAdv neither.

So unfortunately I don't think it can be added to the library itself.
However it definitely seems useful and I'd like to encourage you to make it
available somewhere, e.g. on wxWiki, or maybe on Github with a link to the
repository from wxWiki.

Thanks,
VZ

Iwbnwif Yiw

unread,
Apr 27, 2014, 3:23:01 PM4/27/14
to wx-...@googlegroups.com
 The main problem with adding this would be that naively adding it to wxAdv
library would create a dependency of it on wxHTML which is currently not
required by it. And wxHTML currently doesn't require wxAdv neither.


Ah, thank you I didn't realise that. In that case I understand that adding to the Wiki is probably the best route.
Reply all
Reply to author
Forward
0 new messages