From 3e33568e0bd8416417916d194fc9a50cab9636bf Mon Sep 17 00:00:00 2001 From: Vadim Zeitlin Date: Tue, 17 Sep 2013 15:25:47 +0000 Subject: [PATCH] Add wxDataViewRendererBase::GetEffectiveAlignment() and use it. This helper method falls back on the alignment of the column if the renderer alignment is not specified. This is almost always what should be used instead of GetAlignment() to determine the alignment that really should be used in the drawing code. In particular, using GetEffectiveAlignment() in wxDataViewCustomRenderer fixes the problem with bitmap columns ignoring column alignment for their bitmaps. Closes #15498. git-svn-id: https://svn.wxwidgets.org/svn/wx/wxWidgets/trunk@74825 c3d73ce0-8a6f-49c7-b76d-6d57e0e08775 --- include/wx/dvrenderers.h | 9 ++++++ src/common/datavcmn.cpp | 80 +++++++++++++++++++++++++----------------------- 2 files changed, 51 insertions(+), 38 deletions(-) diff --git a/include/wx/dvrenderers.h b/include/wx/dvrenderers.h index 1cf3c45..3f623cf 100644 --- a/include/wx/dvrenderers.h +++ b/include/wx/dvrenderers.h @@ -167,6 +167,15 @@ protected: // Called from {Cancel,Finish}Editing() to cleanup m_editorCtrl void DestroyEditControl(); + // Return the alignment of this renderer if it's specified (i.e. has value + // different from the default wxDVR_DEFAULT_ALIGNMENT) or the alignment of + // the column it is used for otherwise. + // + // Unlike GetAlignment(), this always returns a valid combination of + // wxALIGN_XXX flags (although possibly wxALIGN_NOT) and never returns + // wxDVR_DEFAULT_ALIGNMENT. + int GetEffectiveAlignment() const; + wxString m_variantType; wxDataViewColumn *m_owner; wxWeakRef m_editorCtrl; diff --git a/src/common/datavcmn.cpp b/src/common/datavcmn.cpp index 2c5621e..2cad362 100644 --- a/src/common/datavcmn.cpp +++ b/src/common/datavcmn.cpp @@ -771,6 +771,20 @@ void wxDataViewRendererBase::PrepareForItem(const wxDataViewModel *model, } +int wxDataViewRendererBase::GetEffectiveAlignment() const +{ + int alignment = GetAlignment(); + + if ( alignment == wxDVR_DEFAULT_ALIGNMENT ) + { + // if we don't have an explicit alignment ourselves, use that of the + // column in horizontal direction and default vertical alignment + alignment = GetOwner()->GetAlignment() | wxALIGN_CENTRE_VERTICAL; + } + + return alignment; +} + // ---------------------------------------------------------------------------- // wxDataViewCustomRendererBase // ---------------------------------------------------------------------------- @@ -807,40 +821,38 @@ wxDataViewCustomRendererBase::WXCallRender(wxRect rectCell, wxDC *dc, int state) // adjust the rectangle ourselves to account for the alignment wxRect rectItem = rectCell; - const int align = GetAlignment(); - if ( align != wxDVR_DEFAULT_ALIGNMENT ) - { - const wxSize size = GetSize(); + const int align = GetEffectiveAlignment(); - // take alignment into account only if there is enough space, otherwise - // show as much contents as possible - // - // notice that many existing renderers (e.g. wxDataViewSpinRenderer) - // return hard-coded size which can be more than they need and if we - // trusted their GetSize() we'd draw the text out of cell bounds - // entirely + const wxSize size = GetSize(); - if ( size.x >= 0 && size.x < rectCell.width ) - { - if ( align & wxALIGN_CENTER_HORIZONTAL ) - rectItem.x += (rectCell.width - size.x)/2; - else if ( align & wxALIGN_RIGHT ) - rectItem.x += rectCell.width - size.x; - // else: wxALIGN_LEFT is the default + // take alignment into account only if there is enough space, otherwise + // show as much contents as possible + // + // notice that many existing renderers (e.g. wxDataViewSpinRenderer) + // return hard-coded size which can be more than they need and if we + // trusted their GetSize() we'd draw the text out of cell bounds + // entirely - rectItem.width = size.x; - } + if ( size.x >= 0 && size.x < rectCell.width ) + { + if ( align & wxALIGN_CENTER_HORIZONTAL ) + rectItem.x += (rectCell.width - size.x)/2; + else if ( align & wxALIGN_RIGHT ) + rectItem.x += rectCell.width - size.x; + // else: wxALIGN_LEFT is the default - if ( size.y >= 0 && size.y < rectCell.height ) - { - if ( align & wxALIGN_CENTER_VERTICAL ) - rectItem.y += (rectCell.height - size.y)/2; - else if ( align & wxALIGN_BOTTOM ) - rectItem.y += rectCell.height - size.y; - // else: wxALIGN_TOP is the default + rectItem.width = size.x; + } - rectItem.height = size.y; - } + if ( size.y >= 0 && size.y < rectCell.height ) + { + if ( align & wxALIGN_CENTER_VERTICAL ) + rectItem.y += (rectCell.height - size.y)/2; + else if ( align & wxALIGN_BOTTOM ) + rectItem.y += rectCell.height - size.y; + // else: wxALIGN_TOP is the default + + rectItem.height = size.y; } @@ -909,16 +921,8 @@ wxDataViewCustomRendererBase::RenderText(const wxString& text, } // get the alignment to use - int align = GetAlignment(); - if ( align == wxDVR_DEFAULT_ALIGNMENT ) - { - // if we don't have an explicit alignment ourselves, use that of the - // column in horizontal direction and default vertical alignment - align = GetOwner()->GetAlignment() | wxALIGN_CENTRE_VERTICAL; - } - dc->DrawLabel(ellipsizedText.empty() ? text : ellipsizedText, - rectText, align); + rectText, GetEffectiveAlignment()); } //----------------------------------------------------------------------------- -- 2.7.4