Remove duplicate code in wxSTCListBox::OnDrawItem
To remove code duplication in the OnDrawItem in wxSTCListBox and wxSTCListBoxD2D, move the code for drawing just the text to a new virtual method named OnDrawItemText. Then have the OnDrawItem method call the new function.
This commit is contained in:
@@ -2679,6 +2679,8 @@ protected:
|
|||||||
void SelectHelper(int i);
|
void SelectHelper(int i);
|
||||||
void RecalculateItemHeight();
|
void RecalculateItemHeight();
|
||||||
int TextBoxFromClientEdge() const;
|
int TextBoxFromClientEdge() const;
|
||||||
|
virtual void OnDrawItemText(wxDC&, const wxRect&,
|
||||||
|
const wxString&, const wxColour&) const;
|
||||||
|
|
||||||
// Event handlers
|
// Event handlers
|
||||||
void OnSelection(wxCommandEvent&);
|
void OnSelection(wxCommandEvent&);
|
||||||
@@ -3019,6 +3021,17 @@ wxCoord wxSTCListBox::OnMeasureItem(size_t WXUNUSED(n)) const
|
|||||||
// x = m_imagePadding + m_imageAreaWidth + m_imagePadding.
|
// x = m_imagePadding + m_imageAreaWidth + m_imagePadding.
|
||||||
// Text is drawn at x + m_textBoxToTextGap and centered vertically.
|
// Text is drawn at x + m_textBoxToTextGap and centered vertically.
|
||||||
|
|
||||||
|
void wxSTCListBox::OnDrawItemText(wxDC& dc, const wxRect& rect,
|
||||||
|
const wxString& label,
|
||||||
|
const wxColour& textCol) const
|
||||||
|
{
|
||||||
|
wxDCTextColourChanger tcc(dc, textCol);
|
||||||
|
|
||||||
|
wxString ellipsizedlabel = wxControl::Ellipsize(label, dc, wxELLIPSIZE_END,
|
||||||
|
rect.GetWidth());
|
||||||
|
dc.DrawText(ellipsizedlabel, rect.GetLeft(), rect.GetTop());
|
||||||
|
}
|
||||||
|
|
||||||
void wxSTCListBox::OnDrawItem(wxDC& dc, const wxRect& rect, size_t n) const
|
void wxSTCListBox::OnDrawItem(wxDC& dc, const wxRect& rect, size_t n) const
|
||||||
{
|
{
|
||||||
wxString label;
|
wxString label;
|
||||||
@@ -3041,11 +3054,10 @@ void wxSTCListBox::OnDrawItem(wxDC& dc, const wxRect& rect, size_t n) const
|
|||||||
else
|
else
|
||||||
textCol = m_visualData->GetTextColour();
|
textCol = m_visualData->GetTextColour();
|
||||||
|
|
||||||
wxDCTextColourChanger tcc(dc, textCol);
|
wxRect rect2(rect.GetLeft() + leftGap, rect.GetTop() + topGap,
|
||||||
|
rect.GetWidth() - leftGap, m_textHeight);
|
||||||
|
|
||||||
label = wxControl::Ellipsize(label, dc, wxELLIPSIZE_END,
|
OnDrawItemText(dc, rect2, label, textCol);
|
||||||
rect.GetWidth() - leftGap);
|
|
||||||
dc.DrawText(label, rect.GetLeft() + leftGap, rect.GetTop() + topGap);
|
|
||||||
|
|
||||||
const wxBitmap* b = m_visualData->GetImage(imageNo);
|
const wxBitmap* b = m_visualData->GetImage(imageNo);
|
||||||
if ( b )
|
if ( b )
|
||||||
@@ -3148,47 +3160,25 @@ public:
|
|||||||
RecalculateItemHeight();
|
RecalculateItemHeight();
|
||||||
}
|
}
|
||||||
|
|
||||||
void OnDrawItem(wxDC& dc, const wxRect& rect, size_t n) const wxOVERRIDE
|
void OnDrawItemText(wxDC& dc, const wxRect& rect, const wxString& label,
|
||||||
|
const wxColour& textCol) const wxOVERRIDE
|
||||||
{
|
{
|
||||||
wxString label;
|
wxFontWithAscent* fontCopy = new wxFontWithAscent(wxFont());
|
||||||
int imageNo = -1;
|
SurfaceFontDataD2D* sfd = new SurfaceFontDataD2D(*m_surfaceFontData);
|
||||||
if ( n < m_labels.size() )
|
fontCopy->SetSurfaceFontData(sfd);
|
||||||
{
|
Font tempFont;
|
||||||
label = m_labels[n];
|
tempFont.SetID(fontCopy);
|
||||||
imageNo = m_imageNos[n];
|
|
||||||
}
|
|
||||||
|
|
||||||
int topGap = m_textTopGap;
|
|
||||||
int leftGap = TextBoxFromClientEdge() + m_textBoxToTextGap;
|
|
||||||
|
|
||||||
wxColour textCol;
|
|
||||||
|
|
||||||
if ( IsSelected(n) )
|
|
||||||
textCol = m_visualData->GetHighlightTextColour();
|
|
||||||
else if ( static_cast<int>(n) == m_currentRow )
|
|
||||||
textCol = m_visualData->GetCurrentTextColour();
|
|
||||||
else
|
|
||||||
textCol = m_visualData->GetTextColour();
|
|
||||||
|
|
||||||
SurfaceD2D surface;
|
SurfaceD2D surface;
|
||||||
surface.Init(&dc, GetGrandParent());
|
surface.Init(&dc, GetGrandParent());
|
||||||
|
|
||||||
wxString ellipsizedLabel = label;
|
wxString ellipsizedLabel = label;
|
||||||
|
|
||||||
wxFontWithAscent* fontCopy = new wxFontWithAscent(wxFont());
|
wxCharBuffer buffer = wx2stc(ellipsizedLabel);
|
||||||
SurfaceFontDataD2D* newSurfaceData =
|
|
||||||
new SurfaceFontDataD2D(*m_surfaceFontData);
|
|
||||||
fontCopy->SetSurfaceFontData(newSurfaceData);
|
|
||||||
Font tempFont;
|
|
||||||
tempFont.SetID(fontCopy);
|
|
||||||
|
|
||||||
wxCharBuffer buffer = wx2stc(label);
|
|
||||||
int curWidth = surface.WidthText(tempFont, buffer.data(),
|
int curWidth = surface.WidthText(tempFont, buffer.data(),
|
||||||
wx2stclen(wxString(), buffer));
|
wx2stclen(wxString(), buffer));
|
||||||
|
|
||||||
int maxWidth = rect.GetWidth() - leftGap;
|
if ( curWidth > rect.GetWidth() )
|
||||||
|
|
||||||
if ( curWidth > maxWidth )
|
|
||||||
{
|
{
|
||||||
int len = label.Length();
|
int len = label.Length();
|
||||||
|
|
||||||
@@ -3201,34 +3191,22 @@ public:
|
|||||||
curWidth = surface.WidthText(tempFont, buffer.data(),
|
curWidth = surface.WidthText(tempFont, buffer.data(),
|
||||||
wx2stclen(wxString(), buffer));
|
wx2stclen(wxString(), buffer));
|
||||||
|
|
||||||
if ( curWidth <= maxWidth )
|
if ( curWidth <= rect.GetWidth() )
|
||||||
{
|
{
|
||||||
break;
|
break;
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
|
|
||||||
wxRect rect2(rect.GetLeft() + leftGap, rect.GetTop() + topGap,
|
PRectangle prect = PRectangleFromwxRect(rect);
|
||||||
rect.GetWidth() - leftGap, m_textHeight);
|
|
||||||
|
|
||||||
PRectangle prect = PRectangleFromwxRect(rect2);
|
|
||||||
ColourDesired fore(textCol.Red(), textCol.Green(), textCol.Blue());
|
ColourDesired fore(textCol.Red(), textCol.Green(), textCol.Blue());
|
||||||
|
|
||||||
XYPOSITION ybase = rect2.GetTop() + m_surfaceFontData->GetAscent();
|
XYPOSITION ybase = rect.GetTop() + m_surfaceFontData->GetAscent();
|
||||||
|
|
||||||
surface.DrawTextTransparent(prect, tempFont, ybase, buffer.data(),
|
surface.DrawTextTransparent(prect, tempFont, ybase, buffer.data(),
|
||||||
wx2stclen(wxString(), buffer), fore);
|
wx2stclen(wxString(), buffer), fore);
|
||||||
tempFont.Release();
|
tempFont.Release();
|
||||||
surface.Release();
|
surface.Release();
|
||||||
|
|
||||||
const wxBitmap* b = m_visualData->GetImage(imageNo);
|
|
||||||
if ( b )
|
|
||||||
{
|
|
||||||
const int width = m_visualData->GetImageAreaWidth();
|
|
||||||
topGap = (m_itemHeight - b->GetHeight()) / 2;
|
|
||||||
leftGap = m_imagePadding + (width - b->GetWidth()) / 2;
|
|
||||||
dc.DrawBitmap(*b, rect.GetLeft() + leftGap, rect.GetTop() + topGap, true);
|
|
||||||
}
|
|
||||||
}
|
}
|
||||||
|
|
||||||
private:
|
private:
|
||||||
|
Reference in New Issue
Block a user