Insert image in cell grid.

If you are using the main C++ distribution of wxWidgets, Feel free to ask any question related to wxWidgets development here. This means questions regarding to C++ and wxWidgets, not compile problems.
Post Reply
dkaip
Super wx Problem Solver
Super wx Problem Solver
Posts: 334
Joined: Wed Jan 20, 2010 1:15 pm

Insert image in cell grid.

Post by dkaip »

Hello.
I am trying to input an image file in a cell of wxGrid. I can't find an example in minimal wxGrid.
So looking around find some code, but can't make work. I am putting code.
Thank's
Jim

Code: Select all


class ImageGridCellRenderer : public wxGridCellStringRenderer
{
public:
    virtual void Draw(wxGrid& grid, wxGridCellAttr& attr, wxDC& dc, const wxRect& rect, int row, int col, bool isSelected);
    wxString image;
};

void ImageGridCellRenderer::Draw(wxGrid& grid, wxGridCellAttr& attr, wxDC& dc, const wxRect& rect, int row, int col, bool isSelected)
   {
      wxGridCellStringRenderer::Draw(grid, attr, dc, rect, row, col, isSelected);

      wxImage cellImage;

      if (cellImage.LoadFile(image))
      {
         wxBitmap cellBitmap(cellImage);
         dc.DrawBitmap(cellBitmap, rect.x, rect.y);
      }
      else
      {
         wxLogError(_T("The image in cell:\n row '%d', column '%d',\n didn't load, does it exist?"), row, col);
         grid.SetCellValue(row, col,_T("Here's should be an image"));
      }
   };

bool addimage(wxString s/*image file on disk*/)
{
    m_grid1->SetCellValue(row,0,"image of file..");// col 0
    // col 1 the image..
    ImageGridCellRenderer* R =new ImageGridCellRenderer();
    wxGridCellAttr *att=m_grid1->GetCellAttr(row,1);
    R->Draw(s,m_grid1,att,wxPaintDC(this),wxRect(),row,1,1); // errors ..
    m_grid1->SetCellRenderer(row, 1, R);
}

ONEEYEMAN
Part Of The Furniture
Part Of The Furniture
Posts: 7479
Joined: Sat Apr 16, 2005 7:22 am
Location: USA, Ukraine

Re: Insert image in cell grid.

Post by ONEEYEMAN »

Hi,
You probably don't want the string renderer to be a base class.

Thank you.
dkaip
Super wx Problem Solver
Super wx Problem Solver
Posts: 334
Joined: Wed Jan 20, 2010 1:15 pm

Re: Insert image in cell grid.

Post by dkaip »

I don't know nothing about it. I Just learn and try to find an example for study.
Thanks for reply.
Jim
ONEEYEMAN
Part Of The Furniture
Part Of The Furniture
Posts: 7479
Joined: Sat Apr 16, 2005 7:22 am
Location: USA, Ukraine

Re: Insert image in cell grid.

Post by ONEEYEMAN »

Hi,
Try to derive from this.
And use wxBitmap instead of wxString.

See how to do drawing in the drawing sample provided with wxWidgets.

Thank you.

P.S.: Also you indicated you are getting errors. Can you post them?
Post Reply