From: Václav Slavík Date: Sat, 30 Oct 2010 15:57:49 +0000 (+0000) Subject: Account for text attrs in generic wxDataViewCtrl's renderers. X-Git-Url: https://git.saurik.com/wxWidgets.git/commitdiff_plain/86755098f52bd0f2c180d2d5ed2202c5d0e25df2 Account for text attrs in generic wxDataViewCtrl's renderers. When determining cell content's size, GetSize() measured the text using wxDataViewCtrl's font, even though it could be renderer in bold or italics. Corrected by setting the attributes - and not only the value - prior to GetSize() calls, and by using the right font in GetTextExtent() calls. git-svn-id: https://svn.wxwidgets.org/svn/wx/wxWidgets/trunk@65949 c3d73ce0-8a6f-49c7-b76d-6d57e0e08775 --- diff --git a/include/wx/dataview.h b/include/wx/dataview.h index cf597a881b..8950f40567 100644 --- a/include/wx/dataview.h +++ b/include/wx/dataview.h @@ -172,6 +172,9 @@ public: bool IsDefault() const { return !(HasColour() || HasFont()); } + // Return the font based on the given one with this attribute applied to it. + wxFont GetEffectiveFont(const wxFont& font) const; + private: wxColour m_colour; bool m_bold; diff --git a/include/wx/dvrenderers.h b/include/wx/dvrenderers.h index 248e4af121..d22157a16b 100644 --- a/include/wx/dvrenderers.h +++ b/include/wx/dvrenderers.h @@ -267,6 +267,10 @@ public: // Prepare DC to use attributes and call Render(). void WXCallRender(wxRect rect, wxDC *dc, int state); +protected: + // helper for GetSize() implementations, respects attributes + wxSize GetTextExtent(const wxString& str) const; + private: wxDataViewItemAttr m_attr; diff --git a/src/common/datavcmn.cpp b/src/common/datavcmn.cpp index d14aa09706..dd45d2b42e 100644 --- a/src/common/datavcmn.cpp +++ b/src/common/datavcmn.cpp @@ -69,6 +69,24 @@ private: } // anonymous namespace +// --------------------------------------------------------- +// wxDataViewItemAttr +// --------------------------------------------------------- + +wxFont wxDataViewItemAttr::GetEffectiveFont(const wxFont& font) const +{ + if ( !HasFont() ) + return font; + + wxFont f(font); + if ( GetBold() ) + f.MakeBold(); + if ( GetItalic() ) + f.MakeItalic(); + return f; +} + + // --------------------------------------------------------- // wxDataViewModelNotifier // --------------------------------------------------------- @@ -826,19 +844,28 @@ wxDataViewCustomRendererBase::WXCallRender(wxRect rectCell, wxDC *dc, int state) wxDCFontChanger changeFont(*dc); if ( m_attr.HasFont() ) - { - wxFont font(dc->GetFont()); - if ( m_attr.GetBold() ) - font.MakeBold(); - if ( m_attr.GetItalic() ) - font.MakeItalic(); - - changeFont.Set(font); - } + changeFont.Set(m_attr.GetEffectiveFont(dc->GetFont())); Render(rectItem, dc, state); } +wxSize wxDataViewCustomRendererBase::GetTextExtent(const wxString& str) const +{ + const wxDataViewCtrl *view = GetView(); + + if ( m_attr.HasFont() ) + { + wxFont font(m_attr.GetEffectiveFont(view->GetFont())); + wxSize size; + view->GetTextExtent(str, &size.x, &size.y, NULL, NULL, &font); + return size; + } + else + { + return view->GetTextExtent(str); + } +} + void wxDataViewCustomRendererBase::RenderText(const wxString& text, int xoffset, diff --git a/src/generic/datavgen.cpp b/src/generic/datavgen.cpp index ec68fc76a9..2fe3655153 100644 --- a/src/generic/datavgen.cpp +++ b/src/generic/datavgen.cpp @@ -761,10 +761,10 @@ bool wxDataViewTextRenderer::Render(wxRect rect, wxDC *dc, int state) wxSize wxDataViewTextRenderer::GetSize() const { - const wxDataViewCtrl *view = GetView(); if (!m_text.empty()) - return view->wxWindowBase::GetTextExtent( m_text ); - return wxSize(wxDVC_DEFAULT_RENDERER_SIZE,wxDVC_DEFAULT_RENDERER_SIZE); + return GetTextExtent(m_text); + else + return wxSize(wxDVC_DEFAULT_RENDERER_SIZE,wxDVC_DEFAULT_RENDERER_SIZE); } // --------------------------------------------------------- @@ -1016,11 +1016,7 @@ bool wxDataViewDateRenderer::Render( wxRect cell, wxDC *dc, int state ) wxSize wxDataViewDateRenderer::GetSize() const { - const wxDataViewCtrl* view = GetView(); - wxString tmp = m_date.FormatDate(); - wxCoord x,y,d; - view->GetTextExtent( tmp, &x, &y, &d ); - return wxSize(x,y+d); + return GetTextExtent(m_date.FormatDate()); } void wxDataViewDateRenderer::WXOnActivate(wxDataViewModel *model, @@ -1085,15 +1081,13 @@ bool wxDataViewIconTextRenderer::Render(wxRect rect, wxDC *dc, int state) wxSize wxDataViewIconTextRenderer::GetSize() const { - const wxDataViewCtrl *view = GetView(); if (!m_value.GetText().empty()) { - int x,y; - view->GetTextExtent( m_value.GetText(), &x, &y ); + wxSize size = GetTextExtent(m_value.GetText()); if (m_value.GetIcon().IsOk()) - x += m_value.GetIcon().GetWidth() + 4; - return wxSize( x, y ); + size.x += m_value.GetIcon().GetWidth() + 4; + return size; } return wxSize(80,20); } @@ -2524,12 +2518,17 @@ int wxDataViewMainWindow::GetLineStart( unsigned int row ) const !model->HasContainerColumns(item)) continue; // skip it! - wxVariant value; - model->GetValue( value, item, column->GetModelColumn() ); - wxDataViewRenderer *renderer = const_cast(column->GetRenderer()); + + wxVariant value; + model->GetValue( value, item, column->GetModelColumn() ); renderer->SetValue( value ); + + wxDataViewItemAttr attr; + model->GetAttr(item, column->GetModelColumn(), attr); + renderer->SetAttr(attr); + height = wxMax( height, renderer->GetSize().y ); } @@ -2587,12 +2586,17 @@ int wxDataViewMainWindow::GetLineAt( unsigned int y ) const !model->HasContainerColumns(item)) continue; // skip it! - wxVariant value; - model->GetValue( value, item, column->GetModelColumn() ); - wxDataViewRenderer *renderer = const_cast(column->GetRenderer()); + + wxVariant value; + model->GetValue( value, item, column->GetModelColumn() ); renderer->SetValue( value ); + + wxDataViewItemAttr attr; + model->GetAttr(item, column->GetModelColumn(), attr); + renderer->SetAttr(attr); + height = wxMax( height, renderer->GetSize().y ); } @@ -2640,12 +2644,17 @@ int wxDataViewMainWindow::GetLineHeight( unsigned int row ) const !model->HasContainerColumns(item)) continue; // skip it! - wxVariant value; - model->GetValue( value, item, column->GetModelColumn() ); - wxDataViewRenderer *renderer = const_cast(column->GetRenderer()); + + wxVariant value; + model->GetValue( value, item, column->GetModelColumn() ); renderer->SetValue( value ); + + wxDataViewItemAttr attr; + model->GetAttr(item, column->GetModelColumn(), attr); + renderer->SetAttr(attr); + height = wxMax( height, renderer->GetSize().y ); } @@ -3814,6 +3823,11 @@ void wxDataViewMainWindow::OnMouse( wxMouseEvent &event ) wxVariant value; model->GetValue( value, item, col->GetModelColumn() ); custom->SetValue( value ); + + wxDataViewItemAttr attr; + model->GetAttr(item, col->GetModelColumn(), attr); + custom->SetAttr(attr); + wxRect cell_rect( xpos, GetLineStart( current ), col->GetWidth(), GetLineHeight( current ) ); @@ -4169,6 +4183,10 @@ unsigned int wxDataViewCtrl::GetBestColumnWidth(int idx) const GetModel()->GetValue(value, item, column->GetModelColumn()); renderer->SetValue(value); + wxDataViewItemAttr attr; + GetModel()->GetAttr(item, column->GetModelColumn(), attr); + renderer->SetAttr(attr); + max_width = (unsigned)wxMax((int)max_width, renderer->GetSize().x); }