sw/inc/fchrfmt.hxx | 15 ++++--- sw/source/core/text/txtfld.cxx | 2 - sw/source/core/txtnode/fmtatr2.cxx | 72 +++++++++++++++++-------------------- 3 files changed, 45 insertions(+), 44 deletions(-)
New commits: commit ef3a2624657645d61fa66e0188f40f14f506770a Author: Bjoern Michaelsen <bjoern.michael...@libreoffice.org> AuthorDate: Tue Feb 18 15:02:36 2025 +0100 Commit: Bjoern Michaelsen <bjoern.michael...@libreoffice.org> CommitDate: Wed Feb 19 21:02:20 2025 +0100 SwFormatCharFormat: SwClient no more Change-Id: Ic41688ed012373738b00ad24755ca47ad52b956b Reviewed-on: https://gerrit.libreoffice.org/c/core/+/181849 Tested-by: Jenkins Reviewed-by: Noel Grandin <noel.gran...@collabora.co.uk> Reviewed-by: Michael Stahl <michael.st...@allotropia.de> Reviewed-by: Bjoern Michaelsen <bjoern.michael...@libreoffice.org> diff --git a/sw/inc/fchrfmt.hxx b/sw/inc/fchrfmt.hxx index 96c5f9b3d905..26a5226d6b86 100644 --- a/sw/inc/fchrfmt.hxx +++ b/sw/inc/fchrfmt.hxx @@ -20,7 +20,7 @@ #define INCLUDED_SW_INC_FCHRFMT_HXX #include <svl/poolitem.hxx> -#include "calbck.hxx" +#include <svl/listener.hxx> #include "format.hxx" #include "charfmt.hxx" @@ -29,10 +29,11 @@ class IntlWrapper; /// This pool item subclass can appear in the hint array of a text node. It refers to a character /// style. It's owned by SwTextCharFormat. -class SW_DLLPUBLIC SwFormatCharFormat final : public SfxPoolItem, public SwClient +class SW_DLLPUBLIC SwFormatCharFormat final : public SfxPoolItem, public SvtListener { friend class SwTextCharFormat; SwTextCharFormat* m_pTextAttribute; ///< My text attribute. + SwCharFormat* m_pCharFormat; public: /// single argument ctors shall be explicit. @@ -44,7 +45,7 @@ public: SwFormatCharFormat( const SwFormatCharFormat& rAttr ); private: - virtual void SwClientNotify(const SwModify&, const SfxHint&) override; + virtual void Notify(const SfxHint&) override; /// @@@ public copy ctor, but no copy assignment? SwFormatCharFormat & operator= (const SwFormatCharFormat &) = delete; @@ -63,12 +64,14 @@ public: virtual bool QueryValue( css::uno::Any& rVal, sal_uInt8 nMemberId = 0 ) const override; virtual bool PutValue( const css::uno::Any& rVal, sal_uInt8 nMemberId ) override; - void SetCharFormat( SwFormat* pFormat ) + void SetCharFormat( SwCharFormat* pFormat ) { assert(!pFormat->IsDefault()); // expose cases that lead to use-after-free - pFormat->Add(*this); + EndListeningAll(); + StartListening(pFormat->GetNotifier()); + m_pCharFormat = pFormat; } - SwCharFormat* GetCharFormat() const { return const_cast<SwCharFormat*>(static_cast<const SwCharFormat*>(GetRegisteredIn())); } + SwCharFormat* GetCharFormat() const { return m_pCharFormat; } void dumpAsXml(xmlTextWriterPtr pWriter) const override; }; diff --git a/sw/source/core/text/txtfld.cxx b/sw/source/core/text/txtfld.cxx index a9406f55740c..77abcff7e344 100644 --- a/sw/source/core/text/txtfld.cxx +++ b/sw/source/core/text/txtfld.cxx @@ -345,7 +345,7 @@ static void checkApplyParagraphMarkFormatToNumbering(SwFont* pNumFnt, SwTextForm { // Insert attributes of referenced char format into current set const SwFormatCharFormat& rCharFormat = pCleanedSet->Get(RES_TXTATR_CHARFMT); - const SwAttrSet& rStyleAttrs = static_cast<const SwCharFormat *>(rCharFormat.GetRegisteredIn())->GetAttrSet(); + const SwAttrSet& rStyleAttrs = rCharFormat.GetCharFormat()->GetAttrSet(); SfxWhichIter aIter(rStyleAttrs); sal_uInt16 nWhich = aIter.FirstWhich(); while (nWhich) diff --git a/sw/source/core/txtnode/fmtatr2.cxx b/sw/source/core/txtnode/fmtatr2.cxx index 780ecb23cc92..a8c832fa760e 100644 --- a/sw/source/core/txtnode/fmtatr2.cxx +++ b/sw/source/core/txtnode/fmtatr2.cxx @@ -60,20 +60,24 @@ using namespace ::com::sun::star; SfxPoolItem* SwFormatINetFormat::CreateDefault() { return new SwFormatINetFormat; } -SwFormatCharFormat::SwFormatCharFormat( SwCharFormat *pFormat ) - : SfxPoolItem( RES_TXTATR_CHARFMT ), - SwClient(pFormat), - m_pTextAttribute( nullptr ) +SwFormatCharFormat::SwFormatCharFormat(SwCharFormat* pFormat) + : SfxPoolItem(RES_TXTATR_CHARFMT) + , m_pTextAttribute(nullptr) + , m_pCharFormat(pFormat) { + if(m_pCharFormat) + StartListening(m_pCharFormat->GetNotifier()); setNonShareable(); } SwFormatCharFormat::SwFormatCharFormat( const SwFormatCharFormat& rAttr ) - : SfxPoolItem( RES_TXTATR_CHARFMT ), - SwClient( rAttr.GetCharFormat() ), - m_pTextAttribute( nullptr ) + : SfxPoolItem(RES_TXTATR_CHARFMT) + , m_pTextAttribute(nullptr) + , m_pCharFormat(rAttr.m_pCharFormat) { setNonShareable(); + if(m_pCharFormat) + StartListening(m_pCharFormat->GetNotifier()); } SwFormatCharFormat::~SwFormatCharFormat() {} @@ -81,7 +85,7 @@ SwFormatCharFormat::~SwFormatCharFormat() {} bool SwFormatCharFormat::operator==( const SfxPoolItem& rAttr ) const { assert(SfxPoolItem::operator==(rAttr)); - return GetCharFormat() == static_cast<const SwFormatCharFormat&>(rAttr).GetCharFormat(); + return m_pCharFormat == static_cast<const SwFormatCharFormat&>(rAttr).m_pCharFormat; } SwFormatCharFormat* SwFormatCharFormat::Clone( SfxItemPool* ) const @@ -90,44 +94,38 @@ SwFormatCharFormat* SwFormatCharFormat::Clone( SfxItemPool* ) const } // forward to the TextAttribute -void SwFormatCharFormat::SwClientNotify(const SwModify&, const SfxHint& rHint) +void SwFormatCharFormat::Notify(const SfxHint& rHint) { - if(rHint.GetId() == SfxHintId::SwAutoFormatUsedHint) + if(!m_pTextAttribute) + return; + switch(rHint.GetId()) { - if(m_pTextAttribute) + case SfxHintId::SwAutoFormatUsedHint: m_pTextAttribute->HandleAutoFormatUsedHint(static_cast<const sw::AutoFormatUsedHint&>(rHint)); - } - else if (rHint.GetId() == SfxHintId::SwFormatChange) - { - auto pChangeHint = static_cast<const SwFormatChangeHint*>(&rHint); - if(m_pTextAttribute) - m_pTextAttribute->TriggerNodeUpdate(*pChangeHint); - } - else if (rHint.GetId() == SfxHintId::SwAttrSetChange) - { - auto pChangeHint = static_cast<const sw::AttrSetChangeHint*>(&rHint); - if(m_pTextAttribute) - m_pTextAttribute->TriggerNodeUpdate(*pChangeHint); - } - else if (rHint.GetId() == SfxHintId::SwObjectDying) - { - auto pDyingHint = static_cast<const sw::ObjectDyingHint*>(&rHint); - if(m_pTextAttribute) - m_pTextAttribute->TriggerNodeUpdate(*pDyingHint); - } - else if (rHint.GetId() == SfxHintId::SwLegacyModify) - { - auto pLegacy = static_cast<const sw::LegacyModifyHint*>(&rHint); - if(m_pTextAttribute) - m_pTextAttribute->TriggerNodeUpdate(*pLegacy); + break; + case SfxHintId::SwFormatChange: + m_pTextAttribute->TriggerNodeUpdate(*static_cast<const SwFormatChangeHint*>(&rHint)); + break; + case SfxHintId::SwAttrSetChange: + m_pTextAttribute->TriggerNodeUpdate(*static_cast<const sw::AttrSetChangeHint*>(&rHint)); + break; + case SfxHintId::SwObjectDying: + m_pTextAttribute->TriggerNodeUpdate(*static_cast<const sw::ObjectDyingHint*>(&rHint)); + m_pCharFormat = nullptr; + break; + case SfxHintId::SwLegacyModify: + m_pTextAttribute->TriggerNodeUpdate(*static_cast<const sw::LegacyModifyHint*>(&rHint)); + break; + default: + break; } } bool SwFormatCharFormat::QueryValue( uno::Any& rVal, sal_uInt8 ) const { ProgName sCharFormatName; - if(GetCharFormat()) - SwStyleNameMapper::FillProgName(GetCharFormat()->GetName(), sCharFormatName, SwGetPoolIdFromName::ChrFmt ); + if(m_pCharFormat) + SwStyleNameMapper::FillProgName(m_pCharFormat->GetName(), sCharFormatName, SwGetPoolIdFromName::ChrFmt ); rVal <<= sCharFormatName.toString(); return true; }