wxGrid: Drawing an image in a cell highlighted (e.g. for selected cells) Topic is solved

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
skowski
Earned a small fee
Earned a small fee
Posts: 15
Joined: Fri Dec 04, 2015 11:01 am

wxGrid: Drawing an image in a cell highlighted (e.g. for selected cells)

Post by skowski »

Hi,

I just wrote a grid cell renderer to be able to draw bitmaps as a cell value. It works fine, but if a cell is being selected, it is not highlighted.

Draw() method today:

Code: Select all

if (isSelected)
{
    // draw image in selected state
    // ???
}
else
{
    // draw image in normal state
    dc.DrawBitmap(*bitmap, pos.x, pos.y);
}
Any idea how to draw the bitmap in the selected state?

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

Re: wxGrid: Drawing an image in a cell highlighted (e.g. for selected cells)

Post by ONEEYEMAN »

Hi,
What do you mean "draw aqn image in selected state"? Show some kind of border? Show border around the cell?

Please describe what you want to achieve.

Thank you.
User avatar
doublemax
Moderator
Moderator
Posts: 19116
Joined: Fri Apr 21, 2006 8:03 pm
Location: $FCE2

Re: wxGrid: Drawing an image in a cell highlighted (e.g. for selected cells)

Post by doublemax »

If you look at custom renderers in the grid sample, you see that they usually call the base Draw() method before drawing their own content on top.

Example:

Code: Select all

void MyGridCellRenderer::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);

    dc.SetPen(*wxGREEN_PEN);
    dc.SetBrush(*wxTRANSPARENT_BRUSH);
    dc.DrawEllipse(rect);
}
By that you would get the default behavior, i.e. a different background color if the cell is selected. If you want any other effect, you'll have to code that yourself.
Use the source, Luke!
skowski
Earned a small fee
Earned a small fee
Posts: 15
Joined: Fri Dec 04, 2015 11:01 am

Re: wxGrid: Drawing an image in a cell highlighted (e.g. for selected cells)

Post by skowski »

Hi,

my grid has white background and black text (in text columns). When text cells are selected, they will be drawn with blue blackground and white text, i.e. some kind of inverted.

I'd like to achieve something similar with the icons.
skowski
Earned a small fee
Earned a small fee
Posts: 15
Joined: Fri Dec 04, 2015 11:01 am

Re: wxGrid: Drawing an image in a cell highlighted (e.g. for selected cells)

Post by skowski »

Thanks, doublemax, that solved it! :-)

Code: Select all

void wxGridCellImageRenderer::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);

    wxBitmap *bitmap = GetCellBitmap(grid, row, col);
    if (bitmap != 0)
    {
        wxPoint pos = CenterCellBitmap(rect, bitmap->GetSize());

        dc.DrawBitmap(*bitmap, pos.x, pos.y);
    }
}
Post Reply