Decouple wxPropertyGridPageState from wxDC

Reimplement functions to calculate column widths to do that without
referring to wxClientDC.
This commit is contained in:
Artur Wieczorek
2022-05-30 19:17:37 +02:00
parent 11201bc1ce
commit b72a742b21
5 changed files with 83 additions and 21 deletions

View File

@@ -434,7 +434,11 @@ public:
return m_width;
}
#if WXWIN_COMPATIBILITY_3_0
wxDEPRECATED_MSG("call GetColumnFullWidth(wxPGProperty*, int) instead")
int GetColumnFullWidth(const wxDC& dc, wxPGProperty* p, unsigned int col);
#endif // WXWIN_COMPATIBILITY_3_0
int GetColumnFullWidth(wxPGProperty* p, unsigned int col) const;
// Returns information about arbitrary position in the grid.
// pt - Logical coordinates in the virtual grid space. Use
@@ -570,8 +574,12 @@ protected:
// Returns minimal width for given column so that all images and texts
// will fit entirely.
// Used by SetSplitterLeft() and DoFitColumns().
#if WXWIN_COMPATIBILITY_3_0
wxDEPRECATED_MSG("call GetColumnFitWidth(wxPGProperty*, int, bool) instead")
int GetColumnFitWidth(const wxDC& dc, wxPGProperty* pwc,
unsigned int col, bool subProps) const;
#endif // WXWIN_COMPATIBILITY_3_0
int GetColumnFitWidth(const wxPGProperty* p, unsigned int col, bool subProps) const;
void SetSplitterLeft(bool subProps = false);

View File

@@ -404,7 +404,7 @@ public:
*/
int GetVirtualWidth() const;
int GetColumnFullWidth(wxClientDC &dc, wxPGProperty *p, unsigned int col);
int GetColumnFullWidth(const wxPGProperty* p, unsigned int col) const;
/**
Returns information about arbitrary position in the grid.

View File

@@ -3209,7 +3209,7 @@ wxPGProperty* GetRealRoot(wxPropertyGrid *grid)
return property ? grid->GetFirstChild(property) : NULL;
}
void GetColumnWidths(wxClientDC &dc, wxPropertyGrid *grid, wxPGProperty *root, int width[3])
void GetColumnWidths(wxPropertyGrid *grid, wxPGProperty *root, int width[3])
{
wxPropertyGridPageState *state = grid->GetState();
@@ -3224,9 +3224,9 @@ void GetColumnWidths(wxClientDC &dc, wxPropertyGrid *grid, wxPGProperty *root, i
{
wxPGProperty* p = root->Item(ii);
width[0] = wxMax(width[0], state->GetColumnFullWidth(dc, p, 0));
width[1] = wxMax(width[1], state->GetColumnFullWidth(dc, p, 1));
width[2] = wxMax(width[2], state->GetColumnFullWidth(dc, p, 2));
width[0] = wxMax(width[0], state->GetColumnFullWidth(p, 0));
width[1] = wxMax(width[1], state->GetColumnFullWidth(p, 1));
width[2] = wxMax(width[2], state->GetColumnFullWidth(p, 2));
}
for (ii = 0; ii < root->GetChildCount(); ++ii)
{
@@ -3234,7 +3234,7 @@ void GetColumnWidths(wxClientDC &dc, wxPropertyGrid *grid, wxPGProperty *root, i
if (p->IsExpanded())
{
int w[3];
GetColumnWidths(dc, grid, p, w);
GetColumnWidths(grid, p, w);
width[0] = wxMax(width[0], w[0]);
width[1] = wxMax(width[1], w[1]);
width[2] = wxMax(width[2], w[2]);
@@ -3246,13 +3246,6 @@ void GetColumnWidths(wxClientDC &dc, wxPropertyGrid *grid, wxPGProperty *root, i
width[2] = wxMax(width[2], minWidths[2]);
}
void GetColumnWidths(wxPropertyGrid *grid, wxPGProperty *root, int width[3])
{
wxClientDC dc(grid);
dc.SetFont(grid->GetFont());
GetColumnWidths(dc, grid, root, width);
}
void SetMinSize(wxPropertyGrid *grid)
{
wxPGProperty *p = GetRealRoot(grid);

View File

@@ -1154,11 +1154,10 @@ wxSize wxPropertyGrid::DoGetBestSize() const
10
);
wxClientDC dc(const_cast<wxPropertyGrid *>(this));
int width = m_marginWidth;
for ( unsigned int i = 0; i < m_pState->GetColumnCount(); i++ )
{
width += m_pState->GetColumnFitWidth(dc, m_pState->DoGetRoot(), i, true);
width += m_pState->GetColumnFitWidth(m_pState->DoGetRoot(), i, true);
}
return wxSize(width, lineHeight*numLines + 40);

View File

@@ -16,7 +16,9 @@
#ifndef WX_PRECOMP
#include "wx/bitmap.h"
#if WXWIN_COMPATIBILITY_3_0
#include "wx/dcclient.h"
#endif // WXWIN_COMPATIBILITY_3_0
#include "wx/event.h"
#include "wx/font.h"
#include "wx/log.h"
@@ -715,6 +717,7 @@ wxPropertyGridPageState::HitTest( const wxPoint&pt ) const
// -----------------------------------------------------------------------
// Used by SetSplitterLeft() and DotFitColumns()
#if WXWIN_COMPATIBILITY_3_0
int wxPropertyGridPageState::GetColumnFitWidth(const wxDC& dc,
wxPGProperty* pwc,
unsigned int col,
@@ -758,7 +761,49 @@ int wxPropertyGridPageState::GetColumnFitWidth(const wxDC& dc,
return maxW;
}
#endif // WXWIN_COMPATIBILITY_3_0
int wxPropertyGridPageState::GetColumnFitWidth(const wxPGProperty* p, unsigned int col, bool subProps) const
{
const wxPropertyGrid* pg = GetGrid();
int maxW = 0;
for ( unsigned int i = 0; i < p->GetChildCount(); i++ )
{
int w;
wxPGProperty* pc = p->Item(i);
if ( !pc->IsCategory() )
{
wxString text;
pc->GetDisplayInfo(col, -1, 0, &text, (wxPGCell*)NULL);
int h;
pg->GetTextExtent(text, &w, &h);
if ( col == 0 )
w += ((pc->GetDepth() - 1) * pg->m_subgroup_extramargin);
// account for the bitmap
if ( col == 1 )
w += pc->GetImageOffset(pg->GetImageRect(pc, -1).GetWidth());
w += (wxPG_XBEFORETEXT * 2);
if ( w > maxW )
maxW = w;
}
if ( pc->GetChildCount() > 0 && (subProps || pc->IsCategory()) )
{
w = GetColumnFitWidth(pc, col, subProps);
if ( w > maxW )
maxW = w;
}
}
return maxW;
}
#if WXWIN_COMPATIBILITY_3_0
int wxPropertyGridPageState::GetColumnFullWidth(const wxDC& dc, wxPGProperty* p, unsigned int col)
{
if ( p->IsCategory() )
@@ -778,6 +823,27 @@ int wxPropertyGridPageState::GetColumnFullWidth(const wxDC& dc, wxPGProperty* p,
w += (wxPG_XBEFORETEXT*2);
return w;
}
#endif // WXWIN_COMPATIBILITY_3_0
int wxPropertyGridPageState::GetColumnFullWidth(wxPGProperty* p, unsigned int col) const
{
if ( p->IsCategory() )
return 0;
wxString text;
p->GetDisplayInfo(col, -1, 0, &text, (wxPGCell*)NULL);
int w = GetGrid()->GetTextExtent(text).x;
if ( col == 0 )
w += p->GetDepth() * GetGrid()->m_subgroup_extramargin;
// account for the bitmap
if ( col == 1 )
w += p->GetImageOffset(GetGrid()->GetImageRect(p, -1).GetWidth());
w += (wxPG_XBEFORETEXT * 2);
return w;
}
int wxPropertyGridPageState::DoGetSplitterPosition( int splitterColumn ) const
{
@@ -870,10 +936,8 @@ void wxPropertyGridPageState::DoSetSplitterPosition( int newXPos,
void wxPropertyGridPageState::SetSplitterLeft( bool subProps )
{
wxPropertyGrid* pg = GetGrid();
wxClientDC dc(pg);
dc.SetFont(pg->GetFont());
int maxW = GetColumnFitWidth(dc, m_properties, 0, subProps);
int maxW = GetColumnFitWidth(m_properties, 0, subProps);
if ( maxW > 0 )
{
@@ -887,8 +951,6 @@ void wxPropertyGridPageState::SetSplitterLeft( bool subProps )
wxSize wxPropertyGridPageState::DoFitColumns( bool WXUNUSED(allowGridResize) )
{
wxPropertyGrid* pg = GetGrid();
wxClientDC dc(pg);
dc.SetFont(pg->GetFont());
int marginWidth = pg->GetMarginWidth();
int accWid = marginWidth;
@@ -896,7 +958,7 @@ wxSize wxPropertyGridPageState::DoFitColumns( bool WXUNUSED(allowGridResize) )
for ( unsigned int col=0; col < GetColumnCount(); col++ )
{
int fitWid = GetColumnFitWidth(dc, m_properties, col, true);
int fitWid = GetColumnFitWidth(m_properties, col, true);
int colMinWidth = GetColumnMinWidth(col);
if ( fitWid < colMinWidth )
fitWid = colMinWidth;