From 74b0602a82a8599fab52d6f58d18d0f39946280d Mon Sep 17 00:00:00 2001 From: Bjoern Michaelsen Date: Thu, 10 May 2018 10:51:42 +0200 Subject: dont use SwClient/SwModify in unocore: Section MIME-Version: 1.0 Content-Type: text/plain; charset=UTF-8 Content-Transfer-Encoding: 8bit Change-Id: Ic594c5129664b309ce1b6ddd0c715a17df5a3265 Reviewed-on: https://gerrit.libreoffice.org/54091 Tested-by: Jenkins Reviewed-by: Björn Michaelsen --- sw/inc/calbck.hxx | 14 ++++++++++ sw/inc/section.hxx | 1 + sw/source/core/unocore/unosect.cxx | 52 ++++++++++++++++++++------------------ 3 files changed, 43 insertions(+), 24 deletions(-) diff --git a/sw/inc/calbck.hxx b/sw/inc/calbck.hxx index 9f809ea24dbd..595a3deab14d 100644 --- a/sw/inc/calbck.hxx +++ b/sw/inc/calbck.hxx @@ -21,6 +21,7 @@ #define INCLUDED_SW_INC_CALBCK_HXX #include +#include #include #include "swdllapi.h" #include "ring.hxx" @@ -71,6 +72,19 @@ namespace sw virtual ~ModifyChangedHint() override; const SwModify* m_pNew; }; + // Observer pattern using svl implementation + // use this instead of SwClient/SwModify whereever possible + // In writer layout, this might not always be possible, + // but for listeners outside of it (e.g. unocore) this should be used. + // The only "magic" signal this class issues is a ModifyChangedHint + // proclaiming its death. It does NOT however provide a new SwModify for + // listeners to switch to like the old SwModify/SwClient did, as that leads + // to madness. + class SW_DLLPUBLIC BroadcasterMixin { + SvtBroadcaster m_aNotifier; + public: + SvtBroadcaster& GetNotifier() { return m_aNotifier; } + }; /// refactoring out the some of the more sane SwClient functionality class SW_DLLPUBLIC WriterListener { diff --git a/sw/inc/section.hxx b/sw/inc/section.hxx index 30b582b89a80..eb4cc6913f38 100644 --- a/sw/inc/section.hxx +++ b/sw/inc/section.hxx @@ -270,6 +270,7 @@ enum class SectionSort { Not, Pos }; class SW_DLLPUBLIC SwSectionFormat : public SwFrameFormat , public ::sfx2::Metadatable + , public sw::BroadcasterMixin { friend class SwDoc; diff --git a/sw/source/core/unocore/unosect.cxx b/sw/source/core/unocore/unosect.cxx index ec5197cbbfcd..188299134f04 100644 --- a/sw/source/core/unocore/unosect.cxx +++ b/sw/source/core/unocore/unosect.cxx @@ -29,6 +29,7 @@ #include #include #include +#include #include #include #include @@ -100,7 +101,7 @@ struct SwTextSectionProperties_Impl }; class SwXTextSection::Impl - : public SwClient + : public SvtListener { private: ::osl::Mutex m_Mutex; // just for OInterfaceContainerHelper2 @@ -114,25 +115,33 @@ public: bool m_bIsDescriptor; OUString m_sName; std::unique_ptr m_pProps; + SwSectionFormat* m_pFormat; - Impl( SwXTextSection & rThis, - SwSectionFormat *const pFormat, const bool bIndexHeader) - : SwClient(pFormat) + Impl( SwXTextSection& rThis, + SwSectionFormat* const pFormat, const bool bIndexHeader) + : SvtListener() , m_rThis(rThis) , m_rPropSet(*aSwMapProvider.GetPropertySet(PROPERTY_MAP_SECTION)) , m_EventListeners(m_Mutex) , m_bIndexHeader(bIndexHeader) , m_bIsDescriptor(nullptr == pFormat) , m_pProps(pFormat ? nullptr : new SwTextSectionProperties_Impl()) + , m_pFormat(pFormat) { + if(m_pFormat) + StartListening(m_pFormat->GetNotifier()); } - SwSectionFormat * GetSectionFormat() const + void Attach(SwSectionFormat* pFormat) { - return static_cast(const_cast( - GetRegisteredIn())); + EndListeningAll(); + StartListening(pFormat->GetNotifier()); + m_pFormat = pFormat; } + SwSectionFormat* GetSectionFormat() const + { return m_pFormat; } + SwSectionFormat & GetSectionFormatOrThrow() const { SwSectionFormat *const pFormat( GetSectionFormat() ); if (!pFormat) { @@ -155,27 +164,22 @@ public: uno::Sequence< uno::Any > GetPropertyValues_Impl( const uno::Sequence< OUString >& rPropertyNames); -protected: - // SwClient - virtual void Modify(const SfxPoolItem *pOld, const SfxPoolItem *pNew) override; - + virtual void Notify(const SfxHint& rHint) override; }; -void SwXTextSection::Impl::Modify( const SfxPoolItem *pOld, const SfxPoolItem *pNew) +void SwXTextSection::Impl::Notify(const SfxHint& rHint) { - ClientModify(this, pOld, pNew); - if (GetRegisteredIn()) + if(rHint.GetId() == SfxHintId::Dying) { - return; // core object still alive - } - - uno::Reference const xThis(m_wThis); - if (!xThis.is()) - { // fdo#72695: if UNO object is already dead, don't revive it with event - return; + m_pFormat = nullptr; + uno::Reference const xThis(m_wThis); + if (!xThis.is()) + { // fdo#72695: if UNO object is already dead, don't revive it with event + return; + } + lang::EventObject const ev(xThis); + m_EventListeners.disposeAndClear(ev); } - lang::EventObject const ev(xThis); - m_EventListeners.disposeAndClear(ev); } SwSectionFormat * SwXTextSection::GetFormat() const @@ -407,7 +411,7 @@ SwXTextSection::attach(const uno::Reference< text::XTextRange > & xTextRange) "SwXTextSection::attach(): invalid TextRange", static_cast< ::cppu::OWeakObject*>(this), 0); } - pRet->GetFormat()->Add(m_pImpl.get()); + m_pImpl->Attach(pRet->GetFormat()); pRet->GetFormat()->SetXObject(static_cast< ::cppu::OWeakObject*>(this)); // XML import must hide sections depending on their old -- cgit