wxGrid and wxBitmap Topic is solved
Re: wxGrid and wxBitmap
The wxGridCellRenderer::Draw() should be outside the if-check. It will redraw the background and by that clear the image. And of course you need to call Refresh() on the grid, if you toggle image visibility, so that all cells get redrawn.
Use the source, Luke!
Re: wxGrid and wxBitmap
Moved Draw() out of the if block.
Also, when installing / removing the checkbox, I do Refresh the grid
But all the same, part of the picture is displayed when the line height is reduced. It's not clear when Draw() is called to draw the grid, since I don't see an explicit Draw() call.
Code: Select all
void wxBmpGridCellRenderer::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);
if(m_IsVisiblePhoto)
{
dc.SetClippingRegion(rect);
m_img.Rescale(wxSize(109,145).GetWidth(),wxSize(109,145).GetHeight());
wxBitmap cellBitmap(m_img);
dc.DrawBitmap(cellBitmap,rect.x,rect.y);
dc.DestroyClippingRegion();
}
}
Code: Select all
void testWxSmithFrame::OnCheckBoxEnableFotoClick(wxCommandEvent& event)
{
if(CheckBoxEnableFoto->GetValue())
{
Grid1->SetDefaultRowSize(145); //line height is 14px by default
Grid1->Refresh();
}
else
{
Grid1->SetDefaultRowSize(25); //line height is 25px by default
Grid1->Refresh();
}
}
Re: wxGrid and wxBitmap
How to you change the m_IsVisiblePhoto flag in the renderers?
Maybe it's easier to chose my second suggestion of subclassing wxGrid. It will make it much easier if you want to add another similar functionlity in the future.
Maybe it's easier to chose my second suggestion of subclassing wxGrid. It will make it much easier if you want to add another similar functionlity in the future.
Use the source, Luke!
Re: wxGrid and wxBitmap
Yes, indeed, I forgot to set the m_IsVisibleFoto flag in the OnCheckBoxEnableFotoClick(wxCommandEvent& event) handler
I added methods to the renderer class to work with m_IsVisibleFoto
but I did not add a change to this flag in the OnCheckBoxEnableFotoClick handler, because I do not see these methods in the handler
it turns out that to work with these methods, I have to pass the wxGrid parent to the OnCheckBoxEnableFotoClick(wxCommandEvent& event) handler? to refer to the SetVisible() and GetVisible() methods
I added methods to the renderer class to work with m_IsVisibleFoto
Code: Select all
class wxBmpGridCellRenderer : public wxGridCellRenderer
{
public:
wxBmpGridCellRenderer(const wxImage& image) : wxGridCellRenderer(), m_img(image) {}
void Draw(wxGrid& grid, wxGridCellAttr& attr, wxDC& dc,
const wxRect& rect, int row, int col, bool isSelected);
wxSize GetBestSize(wxGrid& grid, wxGridCellAttr& attr, wxDC& dc,
int row, int col)
{
return wxSize(m_img.GetWidth(), m_img.GetHeight());
}
wxGridCellRenderer *Clone() const
{
return new wxBmpGridCellRenderer(m_img);
}
void ClearCellGrid(wxGrid&grid);
void SetVisible(bool flag)
{
m_IsVisibleFoto = flag;
}
bool GetVisible()
{
return m_IsVisiblePhoto;
}
private:
bool m_IsVisibleFoto = 1;
wxImage m_img;
};
it turns out that to work with these methods, I have to pass the wxGrid parent to the OnCheckBoxEnableFotoClick(wxCommandEvent& event) handler? to refer to the SetVisible() and GetVisible() methods
Re: wxGrid and wxBitmap
Use wxGrid::GetCellRenderer() to get the renderer of all cells with images.
Code: Select all
// get cell renderer
wxGridCellRenderer *renderer = grid->GetCellRenderer(row, col);
// make sure it's the type we expect
wxBmpGridCellRenderer *bmprenderer = wxDynamicCast(renderer, wxBmpGridCellRenderer);
if( bmprenderer != NULL )
{
bmprenderer->SetVisible(...);
}
renderer->DecRef();
Use the source, Luke!
Re: wxGrid and wxBitmap
Gives a compilation error
to the following code
Code: Select all
void testWxSmithFrame::OnCheckBoxEnableFotoClick(wxCommandEvent& event)
{
for(int i = 0; i < Grid1->GetNumberRows(); i++)
{
//get cell renderer
wxGridCellRenderer *renderer = Grid1->GetCellRenderer(i, 0);
// check if this is the type we expect
wxBmpGridCellRenderer *bmprenderer = wxDynamicCast(renderer, wxBmpGridCellRenderer);
if( bmprenderer != NULL )
{
if(CheckBoxEnableFoto->GetValue())
bmprenderer->SetVisible(true);
else
bmprenderer->SetVisible(false);
}
renderer->DecRef();
}
if(CheckBoxEnableFoto->GetValue())
{
Grid1->SetDefaultRowSize(145); //line height is 14px by default
Grid1->Refresh();
}
else
{
Grid1->SetDefaultRowSize(25); //line height is 25px by default
Grid1->Refresh();
}
}
Re: wxGrid and wxBitmap
Hmm, wxDynamicCast only seems to work with classes derived from wxObject.
Try this:
If that doesn't work, you'll have to use a static cast and make sure that you get the right cells.
Try this:
Code: Select all
wxBmpGridCellRenderer *bmprenderer = dynamic_cast<wxBmpGridCellRenderer*>renderer;
Use the source, Luke!
Re: wxGrid and wxBitmap
Good morning!
This is how it works, thanks a lot.
PS:doublemax,But do not tell me, why should I use this method?
I would like to understand, but I can not understand why this method call is required. I know what I have in the first column, why should I get a renderer for these cells?
This is how it works, thanks a lot.
Code: Select all
wxBmpGridCellRenderer *bmprenderer = dynamic_cast<wxBmpGridCellRenderer*>(renderer);
Code: Select all
GetCellRenderer(row , col );
Re: wxGrid and wxBitmap
I thought you had assigned individual renderers to all cells with image content. If that's the case, you must access them all and change the image visibility.
Use the source, Luke!