Use wxBitmapBundle in wxPropertyGrid

This commit is contained in:
Artur Wieczorek
2021-11-19 17:52:55 +01:00
parent 17c1bfc0f4
commit 87394856f5
11 changed files with 121 additions and 79 deletions

View File

@@ -471,7 +471,7 @@ public:
void Add( const wxString& label, int id = -2 );
#if wxUSE_BMPBUTTON
void Add( const wxBitmap& bitmap, int id = -2 );
void Add( const wxBitmapBundle& bitmap, int id = -2 );
#endif
wxSize GetPrimarySize() const

View File

@@ -17,6 +17,7 @@
#include "wx/propgrid/propgriddefs.h"
#include "wx/bitmap.h"
#include "wx/bmpbndl.h"
#include "wx/font.h"
#include "wx/validate.h"
@@ -135,6 +136,7 @@ public:
// can be passed to DrawText.
int PreDrawCell( wxDC& dc,
const wxRect& rect,
const wxPropertyGrid* propGrid,
const wxPGCell& cell,
int flags ) const;
@@ -180,7 +182,7 @@ public:
m_text = text;
m_hasValidText = true;
}
void SetBitmap( const wxBitmap& bitmap ) { m_bitmap = bitmap; }
void SetBitmap( const wxBitmapBundle& bitmap ) { m_bitmapBundle = bitmap; }
void SetFgCol( const wxColour& col ) { m_fgCol = col; }
void SetBgCol( const wxColour& col ) { m_bgCol = col; }
void SetFont( const wxFont& font ) { m_font = font; }
@@ -189,7 +191,7 @@ protected:
virtual ~wxPGCellData() { }
wxString m_text;
wxBitmap m_bitmap;
wxBitmapBundle m_bitmapBundle;
wxColour m_fgCol;
wxColour m_bgCol;
wxFont m_font;
@@ -210,7 +212,7 @@ public:
}
wxPGCell( const wxString& text,
const wxBitmap& bitmap = wxNullBitmap,
const wxBitmapBundle& bitmap = wxBitmapBundle(),
const wxColour& fgCol = wxNullColour,
const wxColour& bgCol = wxNullColour );
@@ -238,7 +240,7 @@ public:
void MergeFrom( const wxPGCell& srcCell );
void SetText( const wxString& text );
void SetBitmap( const wxBitmap& bitmap );
void SetBitmap( const wxBitmapBundle& bitmap );
void SetFgCol( const wxColour& col );
// Sets font of the cell.
@@ -254,7 +256,7 @@ public:
void SetBgCol( const wxColour& col );
const wxString& GetText() const { return GetData()->m_text; }
const wxBitmap& GetBitmap() const { return GetData()->m_bitmap; }
const wxBitmapBundle& GetBitmap() const { return GetData()->m_bitmapBundle; }
const wxColour& GetFgCol() const { return GetData()->m_fgCol; }
// Returns font of the cell. If no specific font is set for this
@@ -818,7 +820,7 @@ public:
// Adds a single item, with bitmap.
wxPGChoiceEntry& Add( const wxString& label,
const wxBitmap& bitmap,
const wxBitmapBundle& bitmap,
int value = wxPG_INVALID_VALUE );
// Adds a single item with full entry information.
@@ -1539,10 +1541,7 @@ public:
// Returns bitmap that appears next to value text. Only returns non-@NULL
// bitmap if one was set with SetValueImage().
wxBitmap* GetValueImage() const
{
return m_valueBitmap;
}
wxBitmap* GetValueImage() const;
// Returns property attribute value, null variant if not found.
wxVariant GetAttribute( const wxString& name ) const;
@@ -1696,7 +1695,7 @@ public:
// Set wxBitmap in front of the value. This bitmap may be ignored
// by custom cell renderers.
void SetValueImage( wxBitmap& bmp );
void SetValueImage( const wxBitmapBundle& bmp );
// Sets selected choice and changes property value.
// Tries to retain value type, although currently if it is not string,
@@ -2047,7 +2046,8 @@ protected:
// Show this in front of the value
//
// TODO: Can bitmap be implemented with wxPGCell?
wxBitmap* m_valueBitmap;
wxBitmapBundle m_valueBitmapBundle;
wxBitmap m_valueBitmap;
wxVariant m_value;
wxPGAttributeStorage m_attributes;

View File

@@ -812,7 +812,7 @@ public:
void SetPropertyCell( wxPGPropArg id,
int column,
const wxString& text = wxEmptyString,
const wxBitmap& bitmap = wxNullBitmap,
const wxBitmapBundle& bitmap = wxBitmapBundle(),
const wxColour& fgCol = wxNullColour,
const wxColour& bgCol = wxNullColour );
@@ -910,7 +910,7 @@ public:
// Set wxBitmap in front of the value.
// Bitmap will be scaled to a size returned by
// wxPropertyGrid::GetImageSize();
void SetPropertyImage( wxPGPropArg id, wxBitmap& bmp )
void SetPropertyImage( wxPGPropArg id, const wxBitmapBundle& bmp )
{
wxPG_PROP_ARG_CALL_PROLOG()
p->SetValueImage(bmp);

View File

@@ -553,7 +553,7 @@ public:
/**
Adds new bitmap button.
*/
void Add( const wxBitmap& bitmap, int id = -2 );
void Add( const wxBitmapBundle& bitmap, int id = -2 );
/**
Call this in CreateControls() of your custom editor class

View File

@@ -2041,10 +2041,10 @@ public:
int flags = wxPG_SETVAL_REFRESH_EDITOR );
/**
Set wxBitmap in front of the value. This bitmap may be ignored
by custom cell renderers.
Set wxBitmap taken from wxBitmapBundle in front of the value.
This bitmap may be ignored by custom cell renderers.
*/
void SetValueImage( wxBitmap& bmp );
void SetValueImage( wxBitmapBundle& bmp );
/**
Call this function in OnEvent(), OnButtonClick() etc. to change the
@@ -2342,6 +2342,9 @@ public:
@param rect
Box reserved for drawing.
@param propGrid
Property grid to which the cell belongs.
@param cell
Cell information.
@@ -2354,6 +2357,7 @@ public:
*/
int PreDrawCell( wxDC& dc,
const wxRect& rect,
const wxPropertyGrid* propGrid,
const wxPGCell& cell,
int flags ) const;
@@ -2447,7 +2451,7 @@ public:
wxPGCellData();
void SetText( const wxString& text );
void SetBitmap( const wxBitmap& bitmap );
void SetBitmap( const wxBitmapBundle& bitmap );
void SetFgCol( const wxColour& col );
void SetBgCol( const wxColour& col );
void SetFont( const wxFont& font );
@@ -2456,7 +2460,7 @@ protected:
virtual ~wxPGCellData();
wxString m_text;
wxBitmap m_bitmap;
wxBitmapBundle m_bitmapBundle;
wxColour m_fgCol;
wxColour m_bgCol;
wxFont m_font;
@@ -2481,7 +2485,7 @@ public:
wxPGCell();
wxPGCell(const wxPGCell& other);
wxPGCell( const wxString& text,
const wxBitmap& bitmap = wxNullBitmap,
const wxBitmapBundle& bitmap = wxBitmapBundle(),
const wxColour& fgCol = wxNullColour,
const wxColour& bgCol = wxNullColour );
@@ -2506,7 +2510,7 @@ public:
void MergeFrom( const wxPGCell& srcCell );
void SetText( const wxString& text );
void SetBitmap( const wxBitmap& bitmap );
void SetBitmap( const wxBitmapBundle& bitmap );
void SetFgCol( const wxColour& col );
/**
@@ -2525,7 +2529,7 @@ public:
void SetBgCol( const wxColour& col );
const wxString& GetText() const;
const wxBitmap& GetBitmap() const;
const wxBitmapBundle& GetBitmap() const;
const wxColour& GetFgCol() const;
/**

View File

@@ -927,7 +927,7 @@ public:
void SetPropertyCell( wxPGPropArg id,
int column,
const wxString& text = wxEmptyString,
const wxBitmap& bitmap = wxNullBitmap,
const wxBitmapBundle& bitmap = wxBitmapBundle(),
const wxColour& fgCol = wxNullColour,
const wxColour& bgCol = wxNullColour );
@@ -1047,12 +1047,12 @@ public:
void SetPropertyHelpString( wxPGPropArg id, const wxString& helpString );
/**
Set wxBitmap in front of the value.
Set wxBitmap taken from wxBitmapBundle in front of the value.
@remarks Bitmap will be scaled to a size returned by
wxPropertyGrid::GetImageSize();
*/
void SetPropertyImage( wxPGPropArg id, wxBitmap& bmp );
void SetPropertyImage( wxPGPropArg id, wxBitmapBundle& bmp );
/**
Sets maximum length of text in property text editor.

View File

@@ -1159,7 +1159,7 @@ void FormMain::PopulateWithStandardItems ()
continue;
pg->SetPropertyCell( p, 3, "Cell 3", bmp );
pg->SetPropertyCell( p, 4, "Cell 4", wxNullBitmap, *wxWHITE, *wxBLACK );
pg->SetPropertyCell( p, 4, "Cell 4", wxBitmapBundle(), *wxWHITE, *wxBLACK );
}
}
@@ -1384,16 +1384,35 @@ void FormMain::PopulateWithExamples ()
pg->Append( new wxStringProperty( "StringPropertyWithBitmap",
wxPG_LABEL,
"Test Text") );
wxBitmap myTestBitmap(60, 15, 32);
wxMemoryDC mdc;
mdc.SelectObject(myTestBitmap);
mdc.SetBackground(*wxWHITE_BRUSH);
mdc.Clear();
mdc.SetPen(*wxBLACK);
mdc.DrawLine(0, 0, 60, 15);
mdc.SelectObject(wxNullBitmap);
pg->SetPropertyImage( "StringPropertyWithBitmap", myTestBitmap );
wxBitmap myTestBitmap1x(60, 15, 32);
{
wxMemoryDC mdc(myTestBitmap1x);
mdc.SetBackground(*wxWHITE_BRUSH);
mdc.Clear();
mdc.SetPen(*wxBLACK_PEN);
mdc.SetBrush(*wxWHITE_BRUSH);
mdc.DrawRectangle(0, 0, 60, 15);
mdc.DrawLine(0, 0, 59, 14);
mdc.SetTextForeground(*wxBLACK);
mdc.DrawText("x1", 0, 0);
}
wxBitmap myTestBitmap2x(120, 30, 32);
{
wxMemoryDC mdc(myTestBitmap2x);
mdc.SetBackground(*wxWHITE_BRUSH);
mdc.Clear();
mdc.SetPen(wxPen(*wxBLUE, 2));
mdc.SetBrush(*wxWHITE_BRUSH);
mdc.DrawRectangle(0, 0, 120, 30);
mdc.DrawLine(0, 0, 119, 31);
mdc.SetTextForeground(*wxBLUE);
wxFont f = mdc.GetFont();
f.SetPixelSize(2 * f.GetPixelSize());
mdc.SetFont(f);
mdc.DrawText("x2", 0, 0);
}
myTestBitmap2x.SetScaleFactor(2);
pg->SetPropertyImage( "StringPropertyWithBitmap", wxBitmapBundle::FromBitmaps(myTestBitmap1x, myTestBitmap2x));
// this value array would be optional if values matched string indexes
//long flags_prop_values[] = { wxICONIZE, wxCAPTION, wxMINIMIZE_BOX, wxMAXIMIZE_BOX };

View File

@@ -745,18 +745,18 @@ void wxPropertyGrid::OnComboItemPaint( const wxPGComboBox* pCb,
if ( item < 0 )
return;
const wxBitmap* itemBitmap = NULL;
wxBitmap itemBitmap;
if ( comValIndex == -1 && choices.IsOk() && choices.Item(item).GetBitmap().IsOk() )
itemBitmap = &choices.Item(item).GetBitmap();
itemBitmap = choices.Item(item).GetBitmap().GetBitmapFor(this);
//
// Decide what custom image size to use
// (Use item-specific bitmap only if not drawn in the control field.)
wxSize cis;
if ( itemBitmap && !(flags & wxODCB_PAINTING_CONTROL) )
if ( itemBitmap.IsOk() && !(flags & wxODCB_PAINTING_CONTROL) )
{
cis = itemBitmap->GetSize();
cis = itemBitmap.GetSize();
}
else
{
@@ -778,7 +778,7 @@ void wxPropertyGrid::OnComboItemPaint( const wxPGComboBox* pCb,
}
wxPGPaintData paintdata;
paintdata.m_parent = NULL;
paintdata.m_parent = this;
paintdata.m_choiceItem = item;
// This is by the current (1.0.0b) spec - if painting control, item is -1
@@ -834,7 +834,7 @@ void wxPropertyGrid::OnComboItemPaint( const wxPGComboBox* pCb,
// If current choice had a bitmap set by the application, then
// use it instead of any custom paint procedure
// (only if not drawn in the control field).
else if ( itemBitmap && !(flags & wxODCB_PAINTING_CONTROL) )
else if ( itemBitmap.IsOk() && !(flags & wxODCB_PAINTING_CONTROL) )
useCustomPaintProcedure = false;
if ( useCustomPaintProcedure )
@@ -883,7 +883,7 @@ void wxPropertyGrid::OnComboItemPaint( const wxPGComboBox* pCb,
cell = &choices.Item(item);
renderer = wxPGGlobalVars->m_defaultRenderer;
int imageOffset = renderer->PreDrawCell(dc, r, *cell,
int imageOffset = renderer->PreDrawCell(dc, r, this, *cell,
renderFlags );
if ( imageOffset )
imageOffset += wxCC_CUSTOM_IMAGE_MARGIN1 +
@@ -2198,7 +2198,7 @@ typedef wxBitmapButton wxPGEditorBitmapButton;
#endif // __WXGTK__ / !__WXGTK__
void wxPGMultiButton::Add( const wxBitmap& bitmap, int itemid )
void wxPGMultiButton::Add( const wxBitmapBundle& bitmap, int itemid )
{
itemid = GenId(itemid);
wxSize sz = GetSize();
@@ -2219,16 +2219,17 @@ void wxPGMultiButton::Add( const wxBitmap& bitmap, int itemid )
// Maximal heigth of the bitmap
const int hMax = wxMax(4, sz.y - margins);
wxBitmap bmp = bitmap.GetBitmapFor(this);
wxBitmap scaledBmp;
// Scale bitmap down if necessary
if ( bitmap.GetHeight() > hMax )
if ( bmp.GetHeight() > hMax )
{
double scale = (double)hMax / bitmap.GetHeight();
scaledBmp = wxPropertyGrid::RescaleBitmap(bitmap, scale, scale);
double scale = (double)hMax / bmp.GetHeight();
scaledBmp = wxPropertyGrid::RescaleBitmap(bmp, scale, scale);
}
else
{
scaledBmp = bitmap;
scaledBmp = bmp;
}
wxBitmapButton* button = new wxPGEditorBitmapButton(this, itemid, scaledBmp,

View File

@@ -119,7 +119,7 @@ void wxPGCellRenderer::DrawCaptionSelectionRect(wxWindow* win, wxDC& dc,
}
#endif // WXWIN_COMPATIBILITY_3_0/!WXWIN_COMPATIBILITY_3_0
int wxPGCellRenderer::PreDrawCell( wxDC& dc, const wxRect& rect, const wxPGCell& cell, int flags ) const
int wxPGCellRenderer::PreDrawCell( wxDC& dc, const wxRect& rect, const wxPropertyGrid* propGrid, const wxPGCell& cell, int flags ) const
{
int imageWidth = 0;
@@ -146,7 +146,7 @@ int wxPGCellRenderer::PreDrawCell( wxDC& dc, const wxRect& rect, const wxPGCell&
if ( font.IsOk() )
dc.SetFont(font);
const wxBitmap& bmp = cell.GetBitmap();
wxBitmap bmp = cell.GetBitmap().GetBitmapFor(propGrid);
if ( bmp.IsOk() )
{
int hMax = rect.height - wxPG_CUSTOM_IMAGE_SPACINGY;
@@ -229,9 +229,9 @@ bool wxPGDefaultRenderer::Render( wxDC& dc, const wxRect& rect,
// Property image takes precedence over cell image
if ( column == 1 && !isUnspecified && property->GetValueImage() )
cell.SetBitmap(wxNullBitmap);
cell.SetBitmap(wxBitmapBundle());
imageWidth = PreDrawCell( dc, rect, cell, preDrawFlags );
imageWidth = PreDrawCell( dc, rect, propertyGrid, cell, preDrawFlags );
if ( column == 1 )
{
@@ -368,7 +368,7 @@ wxPGCell::wxPGCell()
}
wxPGCell::wxPGCell( const wxString& text,
const wxBitmap& bitmap,
const wxBitmapBundle& bitmap,
const wxColour& fgCol,
const wxColour& bgCol )
: wxObject()
@@ -376,7 +376,7 @@ wxPGCell::wxPGCell( const wxString& text,
wxPGCellData* data = new wxPGCellData();
m_refData = data;
data->m_text = text;
data->m_bitmap = bitmap;
data->m_bitmapBundle = bitmap;
data->m_fgCol = fgCol;
data->m_bgCol = bgCol;
data->m_hasValidText = true;
@@ -387,7 +387,7 @@ wxObjectRefData *wxPGCell::CloneRefData( const wxObjectRefData *data ) const
wxPGCellData* c = new wxPGCellData();
const wxPGCellData* o = (const wxPGCellData*) data;
c->m_text = o->m_text;
c->m_bitmap = o->m_bitmap;
c->m_bitmapBundle = o->m_bitmapBundle;
c->m_fgCol = o->m_fgCol;
c->m_bgCol = o->m_bgCol;
c->m_hasValidText = o->m_hasValidText;
@@ -401,7 +401,7 @@ void wxPGCell::SetText( const wxString& text )
GetData()->SetText(text);
}
void wxPGCell::SetBitmap( const wxBitmap& bitmap )
void wxPGCell::SetBitmap( const wxBitmapBundle& bitmap )
{
AllocExclusive();
@@ -477,7 +477,6 @@ void wxPGProperty::Init()
#if wxUSE_VALIDATORS
m_validator = NULL;
#endif
m_valueBitmap = NULL;
m_maxLen = 0; // infinite maximum length
@@ -690,7 +689,7 @@ wxPGProperty::~wxPGProperty()
Empty(); // this deletes items
delete m_valueBitmap;
// delete m_valueBitmap;
#if wxUSE_VALIDATORS
delete m_validator;
#endif
@@ -1313,20 +1312,26 @@ bool wxPGProperty::SetValueFromInt( long number, int argFlags )
wxSize wxPGProperty::OnMeasureImage( int WXUNUSED(item) ) const
{
if ( m_valueBitmap )
if ( m_valueBitmapBundle.IsOk() )
{
wxPropertyGrid* pg = GetGrid();
wxBitmap bmp;
double scale = 1.0;
if ( pg )
{
bmp = m_valueBitmapBundle.GetBitmapFor(pg);
int hMax = pg->GetImageSize().GetHeight();
if ( m_valueBitmap->GetHeight() > hMax )
if ( bmp.GetHeight() > hMax )
{
scale = (double)hMax / m_valueBitmap->GetHeight();
scale = (double)hMax / bmp.GetHeight();
}
}
else
{
bmp = m_valueBitmapBundle.GetBitmap(m_valueBitmapBundle.GetDefaultSize());
}
return wxSize(wxRound(scale*m_valueBitmap->GetWidth()), wxDefaultCoord);
return wxSize(wxRound(scale*bmp.GetWidth()), wxDefaultCoord);
}
return wxSize(0,0);
@@ -1355,21 +1360,22 @@ wxPGCellRenderer* wxPGProperty::GetCellRenderer( int WXUNUSED(column) ) const
void wxPGProperty::OnCustomPaint( wxDC& dc,
const wxRect& rect,
wxPGPaintData& )
wxPGPaintData& paintData)
{
wxCHECK_RET( m_valueBitmap && m_valueBitmap->IsOk(), wxS("invalid bitmap") );
wxCHECK_RET( m_valueBitmapBundle.IsOk(), wxS("invalid bitmap bundle") );
wxBitmap bmp = m_valueBitmapBundle.GetBitmapFor(paintData.m_parent);
wxBitmap scaledBmp;
int yOfs;
if ( m_valueBitmap->GetHeight() <= rect.height )
if ( bmp.GetHeight() <= rect.height )
{
scaledBmp = *m_valueBitmap;
yOfs = (rect.height - m_valueBitmap->GetHeight()) / 2;
scaledBmp = bmp;
yOfs = (rect.height - bmp.GetHeight()) / 2;
}
else
{
double scale = (double)rect.height / m_valueBitmap->GetHeight();
scaledBmp = wxPropertyGrid::RescaleBitmap(*m_valueBitmap, scale, scale);
double scale = (double)rect.height / bmp.GetHeight();
scaledBmp = wxPropertyGrid::RescaleBitmap(bmp, scale, scale);
yOfs = 0;
}
@@ -2187,21 +2193,19 @@ bool wxPGProperty::RecreateEditor()
}
void wxPGProperty::SetValueImage( wxBitmap& bmp )
void wxPGProperty::SetValueImage( const wxBitmapBundle& bmp )
{
wxCHECK_RET( GetGrid(),
wxS("Cannot set image for detached property") );
delete m_valueBitmap;
if ( bmp.IsOk() )
{
m_valueBitmap = new wxBitmap(bmp);
m_valueBitmapBundle = bmp;
m_flags |= wxPG_PROP_CUSTOMIMAGE;
}
else
{
m_valueBitmap = NULL;
m_valueBitmapBundle = wxBitmapBundle();
m_flags &= ~(wxPG_PROP_CUSTOMIMAGE);
}
}
@@ -2829,6 +2833,20 @@ bool wxPGProperty::SetMaxLength(int maxLen)
return true;
}
wxBitmap* wxPGProperty::GetValueImage() const
{
if ( !m_valueBitmapBundle.IsOk() )
return NULL;
wxPropertyGrid* pg = GetGrid();
if ( pg )
*const_cast<wxBitmap*>(&m_valueBitmap) = m_valueBitmapBundle.GetBitmapFor(pg);
else
*const_cast<wxBitmap*>(&m_valueBitmap) = m_valueBitmapBundle.GetBitmap(m_valueBitmapBundle.GetDefaultSize());
return const_cast<wxBitmap*>(&m_valueBitmap);
}
// -----------------------------------------------------------------------
// wxPGRootProperty
// -----------------------------------------------------------------------
@@ -2944,7 +2962,7 @@ wxPGChoiceEntry& wxPGChoices::Add( const wxString& label, int value )
// -----------------------------------------------------------------------
wxPGChoiceEntry& wxPGChoices::Add( const wxString& label, const wxBitmap& bitmap, int value )
wxPGChoiceEntry& wxPGChoices::Add( const wxString& label, const wxBitmapBundle& bitmap, int value )
{
AllocExclusive();

View File

@@ -5093,7 +5093,7 @@ bool wxPropertyGrid::HandleMouseMove( int x, unsigned int y,
int space = m_pState->GetColumnWidth(m_colHover);
int imageWidth = 0;
const wxBitmap& bmp = cell.GetBitmap();
const wxBitmap& bmp = cell.GetBitmap().GetBitmapFor(this);
if ( bmp.IsOk() )
{
imageWidth = bmp.GetWidth();

View File

@@ -739,7 +739,7 @@ void wxPropertyGridInterface::SetPropertyColoursToDefault(wxPGPropArg id, int fl
void wxPropertyGridInterface::SetPropertyCell( wxPGPropArg id,
int column,
const wxString& text,
const wxBitmap& bitmap,
const wxBitmapBundle& bitmap,
const wxColour& fgCol,
const wxColour& bgCol )
{