diff options
author | Michael Stahl <mst@openoffice.org> | 2010-12-21 16:48:30 +0100 |
---|---|---|
committer | Michael Stahl <mst@openoffice.org> | 2010-12-21 16:48:30 +0100 |
commit | f9ab560c8b9bb08d3ad7e6b72ea8a8ecbedcdf47 (patch) | |
tree | 7b0e30f5f3e8d37aedf937195a56744d6fc5fa5d /sw | |
parent | c6d658988ed10b4bdfe4d66b4deb4e1f79df33ff (diff) |
sw34bf03: #i115315#: refactor the copying of meta/meta-field:
no longer delay the call to DoCopy() until after the new hint is inserted,
but do it before; this fixes a crash on drag&drop in the same text node.
this is also quite a bit less ugly, but there are still some hacks required:
DoCopy() is now called by a new factory CreateTxtMeta().
DoCopy() needs to know the text node where the hint will be inserted.
so the MakeTxtAttr() function needs to know whether it should insert a copy,
and the target text node.
this requires a new bit in the SetAttrMode enum: SETATTR_IS_COPY.
also, sw::Meta can no longer get the text node from SwTxtMeta, because it is
copied before the SwTxtMeta is inserted, so it now has a SwTxtNode* member.
furthermore the notification stuff is refactored a bit.
Diffstat (limited to 'sw')
-rwxr-xr-x | sw/inc/fmtmeta.hxx | 25 | ||||
-rw-r--r-- | sw/inc/ndhints.hxx | 10 | ||||
-rw-r--r-- | sw/inc/swtypes.hxx | 2 | ||||
-rw-r--r-- | sw/inc/txtatr.hxx | 21 | ||||
-rw-r--r-- | sw/source/core/txtnode/fmtatr2.cxx | 85 | ||||
-rw-r--r-- | sw/source/core/txtnode/ndtxt.cxx | 26 | ||||
-rw-r--r-- | sw/source/core/txtnode/thints.cxx | 9 | ||||
-rw-r--r-- | sw/source/core/txtnode/txtatr2.cxx | 18 | ||||
-rw-r--r-- | sw/source/core/unocore/unorefmk.cxx | 2 |
9 files changed, 117 insertions, 81 deletions
diff --git a/sw/inc/fmtmeta.hxx b/sw/inc/fmtmeta.hxx index d09df11bb1aa..5a382c36fd56 100755 --- a/sw/inc/fmtmeta.hxx +++ b/sw/inc/fmtmeta.hxx @@ -74,17 +74,13 @@ namespace com { namespace sun { namespace star { * <li>The pool item is cloned (because it is non-poolable); the clone * points to the same metadatable entity, but the metadatable entity's * reverse pointer is unchanged.</li> - * <li>A new text hint is created, taking over the new pool item. - * Unfortunately, this also makes the metadatable entity point at the - * cloned pool item.</li> - * <li>The text hint is inserted into the hints array of some text node.</li> - * <li>The DoCopy() method must be called at the new pool item: - * it will clone the metadatable entity (using RegisterAsCopyOf), - * and fix the reverse pointer of the original to point at the - * original pool item. + * <li>The DoCopy() method is called at the new pool item: + * it will clone the metadatable entity (using RegisterAsCopyOf). * This is necessary, because first, a metadatable entity may * only be inserted once into a document, and second, the copy may be * inserted into a different document than the source document!</li> + * <li>A new text hint is created, taking over the new pool item.</li> + * <li>The text hint is inserted into the hints array of some text node.</li> * </ol> */ @@ -99,7 +95,7 @@ class SwFmtMeta : public SfxPoolItem { private: - friend class SwTxtMeta; // needs SetTxtAttr + friend class SwTxtMeta; // needs SetTxtAttr, DoCopy friend class ::sw::Meta; // needs m_pTxtAttr ::boost::shared_ptr< ::sw::Meta > m_pMeta; @@ -108,6 +104,10 @@ private: SwTxtMeta * GetTxtAttr() { return m_pTxtAttr; } void SetTxtAttr(SwTxtMeta * const i_pTxtAttr); + /// this method <em>must</em> be called when the hint is actually copied + void DoCopy(::sw::MetaFieldManager & i_rTargetDocManager, + SwTxtNode & i_rTargetTxtNode); + explicit SwFmtMeta( const USHORT i_nWhich ); public: @@ -119,14 +119,11 @@ public: // SfxPoolItem virtual int operator==( const SfxPoolItem & ) const; virtual SfxPoolItem * Clone( SfxItemPool *pPool = 0 ) const; -// TYPEINFO(); /// notify clients registered at m_pMeta that this meta is being (re-)moved void NotifyChangeTxtNode(SwTxtNode *const pTxtNode); static SwFmtMeta * CreatePoolDefault( const USHORT i_nWhich ); ::sw::Meta * GetMeta() { return m_pMeta.get(); } - /// this method <em>must</em> be called when the hint is actually copied - void DoCopy( SwFmtMeta & rOriginalMeta ); }; @@ -146,6 +143,7 @@ protected: ::com::sun::star::rdf::XMetadatable> m_wXMeta; SwFmtMeta * m_pFmt; + SwTxtNode * m_pTxtNode; SwTxtMeta * GetTxtAttr() const; SwTxtNode * GetTxtNode() const; // returns 0 if not in document (undo) @@ -153,7 +151,8 @@ protected: SwFmtMeta * GetFmtMeta() const { return m_pFmt; } void SetFmtMeta( SwFmtMeta * const i_pFmt ) { m_pFmt = i_pFmt; }; - void NotifyChangeTxtNode(); + void NotifyChangeTxtNodeImpl(); + void NotifyChangeTxtNode(SwTxtNode *const pTxtNode); ::com::sun::star::uno::WeakReference< ::com::sun::star::rdf::XMetadatable> const& GetXMeta() const diff --git a/sw/inc/ndhints.hxx b/sw/inc/ndhints.hxx index c1d210464eb3..34224539e22b 100644 --- a/sw/inc/ndhints.hxx +++ b/sw/inc/ndhints.hxx @@ -33,7 +33,6 @@ #include "swtypes.hxx" -//#include "numrule.hxx" class SwTxtNode; class SwRegHistory; // steht im RolBck.hxx @@ -44,9 +43,16 @@ class SfxPoolItem; class SfxItemSet; class SwDoc; +typedef enum { + COPY = true, + NEW = false, +} CopyOrNew_t; + +// if COPY then pTxtNode must be given! SW_DLLPRIVATE SwTxtAttr * MakeTxtAttr( SwDoc & rDoc, SfxPoolItem & rNew, - xub_StrLen nStt, xub_StrLen nEnd ); + xub_StrLen const nStt, xub_StrLen const nEnd, + CopyOrNew_t const bIsCopy = NEW, SwTxtNode *const pTxtNode = 0); SW_DLLPRIVATE SwTxtAttr * MakeTxtAttr( SwDoc & rDoc, const SfxItemSet & rSet, xub_StrLen nStt, xub_StrLen nEnd ); diff --git a/sw/inc/swtypes.hxx b/sw/inc/swtypes.hxx index 0d37e63c367d..d754b450b3b4 100644 --- a/sw/inc/swtypes.hxx +++ b/sw/inc/swtypes.hxx @@ -228,6 +228,8 @@ namespace nsSetAttrMode // functionality will be disabled) /// force hint expand (only matters for hints with CH_TXTATR) const SetAttrMode SETATTR_FORCEHINTEXPAND= 0x0080; + /// the inserted item is a copy -- intended for use in ndtxt.cxx + const SetAttrMode SETATTR_IS_COPY = 0x0100; } //Umrechnung Twip<-> 1/100 mm fuer UNO diff --git a/sw/inc/txtatr.hxx b/sw/inc/txtatr.hxx index 65ecd951868c..75be35e44821 100644 --- a/sw/inc/txtatr.hxx +++ b/sw/inc/txtatr.hxx @@ -34,6 +34,11 @@ class SwTxtNode; // fuer SwTxtFld class SwCharFmt; +namespace sw { + class MetaFieldManager; +} + + // ATT_CHARFMT ********************************************* class SwTxtCharFmt : public SwTxtAttrEnd @@ -61,7 +66,7 @@ public: class SwTxtAttrNesting : public SwTxtAttrEnd { -public: +protected: SwTxtAttrNesting( SfxPoolItem & i_rAttr, const xub_StrLen i_nStart, const xub_StrLen i_nEnd ); virtual ~SwTxtAttrNesting(); @@ -70,16 +75,20 @@ public: class SwTxtMeta : public SwTxtAttrNesting { private: - SwTxtNode * m_pTxtNode; - -public: SwTxtMeta( SwFmtMeta & i_rAttr, const xub_StrLen i_nStart, const xub_StrLen i_nEnd ); + +public: + static SwTxtMeta * CreateTxtMeta( + ::sw::MetaFieldManager & i_rTargetDocManager, + SwTxtNode *const i_pTargetTxtNode, + SwFmtMeta & i_rAttr, + xub_StrLen const i_nStart, xub_StrLen const i_nEnd, + bool const i_bIsCopy); + virtual ~SwTxtMeta(); void ChgTxtNode(SwTxtNode * const pNode); - SwTxtNode * GetTxtNode() const { return m_pTxtNode; } - }; diff --git a/sw/source/core/txtnode/fmtatr2.cxx b/sw/source/core/txtnode/fmtatr2.cxx index 245ec916fd4a..76d5b41de943 100644 --- a/sw/source/core/txtnode/fmtatr2.cxx +++ b/sw/source/core/txtnode/fmtatr2.cxx @@ -613,6 +613,7 @@ SwFmtMeta::~SwFmtMeta() { if (m_pMeta && (m_pMeta->GetFmtMeta() == this)) { + NotifyChangeTxtNode(0); m_pMeta->SetFmtMeta(0); } } @@ -633,16 +634,24 @@ SfxPoolItem * SwFmtMeta::Clone( SfxItemPool * /*pPool*/ ) const void SwFmtMeta::SetTxtAttr(SwTxtMeta * const i_pTxtAttr) { - ASSERT(!(m_pTxtAttr && i_pTxtAttr), + OSL_ENSURE(!(m_pTxtAttr && i_pTxtAttr), "SwFmtMeta::SetTxtAttr: already has text attribute?"); - ASSERT( m_pTxtAttr || i_pTxtAttr , + OSL_ENSURE( m_pTxtAttr || i_pTxtAttr , "SwFmtMeta::SetTxtAttr: no attribute to remove?"); m_pTxtAttr = i_pTxtAttr; - ASSERT(m_pMeta, "inserted SwFmtMeta has no sw::Meta?"); + OSL_ENSURE(m_pMeta, "inserted SwFmtMeta has no sw::Meta?"); // the sw::Meta must be able to find the current text attribute! - if (i_pTxtAttr && m_pMeta) + if (m_pMeta) { - m_pMeta->SetFmtMeta(this); + if (i_pTxtAttr) + { + m_pMeta->SetFmtMeta(this); + } + else if (m_pMeta->GetFmtMeta() == this) + { // text attribute gone => de-register from text node! + NotifyChangeTxtNode(0); + m_pMeta->SetFmtMeta(0); + } } } @@ -650,36 +659,22 @@ void SwFmtMeta::NotifyChangeTxtNode(SwTxtNode *const pTxtNode) { // N.B.: do not reset m_pTxtAttr here: see call in nodes.cxx, // where the hint is not deleted! - ASSERT(m_pMeta, "NotifyRemoval: no meta ?"); - if (m_pMeta) - { - if (!pTxtNode) - { - SwPtrMsgPoolItem aMsgHint( RES_REMOVE_UNO_OBJECT, - &static_cast<SwModify&>(*m_pMeta) ); // cast to base class! - m_pMeta->Modify(&aMsgHint, &aMsgHint); - } - else - { // do not call Modify, that would call SwXMeta::Modify! - m_pMeta->NotifyChangeTxtNode(); - } + OSL_ENSURE(m_pMeta, "SwFmtMeta::NotifyChangeTxtNode: no Meta?"); + if (m_pMeta && (m_pMeta->GetFmtMeta() == this)) + { // do not call Modify, that would call SwXMeta::Modify! + m_pMeta->NotifyChangeTxtNode(pTxtNode); } } -// UGLY: this really awful method fixes up an inconsistent state, -// and if it is not called when copying, total chaos will undoubtedly ensue -void SwFmtMeta::DoCopy(SwFmtMeta & rOriginalMeta) +// this SwFmtMeta has been cloned and points at the same sw::Meta as the source +// this method copies the sw::Meta +void SwFmtMeta::DoCopy(::sw::MetaFieldManager & i_rTargetDocManager, + SwTxtNode & i_rTargetTxtNode) { - ASSERT(m_pMeta, "DoCopy called for SwFmtMeta with no sw::Meta?"); + OSL_ENSURE(m_pMeta, "DoCopy called for SwFmtMeta with no sw::Meta?"); if (m_pMeta) { const ::boost::shared_ptr< ::sw::Meta> pOriginal( m_pMeta ); - // UGLY: original sw::Meta now points at _this_ due to being already - // inserted via MakeTxtAttr! so fix it up to point at the original item - // (maybe would be better to tell MakeTxtAttr that it creates a copy?) - pOriginal->SetFmtMeta(&rOriginalMeta); - // force pOriginal to register in original text node! - pOriginal->NotifyChangeTxtNode(); if (RES_TXTATR_META == Which()) { m_pMeta.reset( new ::sw::Meta(this) ); @@ -688,14 +683,13 @@ void SwFmtMeta::DoCopy(SwFmtMeta & rOriginalMeta) { ::sw::MetaField *const pMetaField( static_cast< ::sw::MetaField* >(pOriginal.get())); - SwDoc * const pTargetDoc( GetTxtAttr()->GetTxtNode()->GetDoc() ); - m_pMeta = pTargetDoc->GetMetaFieldManager().makeMetaField( this, + m_pMeta = i_rTargetDocManager.makeMetaField( this, pMetaField->m_nNumberFormat, pMetaField->IsFixedLanguage() ); } + // Meta must have a text node before calling RegisterAsCopyOf + m_pMeta->NotifyChangeTxtNode(& i_rTargetTxtNode); // this cannot be done in Clone: a Clone is not necessarily a copy! m_pMeta->RegisterAsCopyOf(*pOriginal); - // force copy Meta to register in target text node! - m_pMeta->NotifyChangeTxtNode(); } } @@ -724,27 +718,36 @@ SwTxtMeta * Meta::GetTxtAttr() const SwTxtNode * Meta::GetTxtNode() const { - SwTxtMeta * const pTxtAttr( GetTxtAttr() ); - return (pTxtAttr) ? pTxtAttr->GetTxtNode() : 0; + return m_pTxtNode; } -void Meta::NotifyChangeTxtNode() +void Meta::NotifyChangeTxtNodeImpl() { - SwTxtNode * const pTxtNode( GetTxtNode() ); - if (pTxtNode && (GetRegisteredIn() != pTxtNode)) + if (m_pTxtNode && (GetRegisteredIn() != m_pTxtNode)) { - pTxtNode->Add(this); + m_pTxtNode->Add(this); } - else if (!pTxtNode && GetRegisteredIn()) + else if (!m_pTxtNode && GetRegisteredIn()) { const_cast<SwModify *>(GetRegisteredIn())->Remove(this); } } +void Meta::NotifyChangeTxtNode(SwTxtNode *const pTxtNode) +{ + m_pTxtNode = pTxtNode; + NotifyChangeTxtNodeImpl(); + if (!pTxtNode) // text node gone? invalidate UNO object! + { + SwPtrMsgPoolItem aMsgHint( RES_REMOVE_UNO_OBJECT, + &static_cast<SwModify&>(*this) ); // cast to base class! + this->Modify(&aMsgHint, &aMsgHint); + } +} + // SwClient void Meta::Modify( SfxPoolItem *pOld, SfxPoolItem *pNew ) { - NotifyChangeTxtNode(); SwModify::Modify(pOld, pNew); if (pOld && (RES_REMOVE_UNO_OBJECT == pOld->Which())) { // invalidate cached uno object @@ -752,7 +755,7 @@ void Meta::Modify( SfxPoolItem *pOld, SfxPoolItem *pNew ) } } -// sw::Metadatable +// sfx2::Metadatable ::sfx2::IXmlIdRegistry& Meta::GetRegistry() { SwTxtNode * const pTxtNode( GetTxtNode() ); diff --git a/sw/source/core/txtnode/ndtxt.cxx b/sw/source/core/txtnode/ndtxt.cxx index d6a6dfb045ac..66f7ea09d1c7 100644 --- a/sw/source/core/txtnode/ndtxt.cxx +++ b/sw/source/core/txtnode/ndtxt.cxx @@ -1434,15 +1434,10 @@ void lcl_CopyHint( const USHORT nWhich, const SwTxtAttr * const pHt, } case RES_TXTATR_META: case RES_TXTATR_METAFIELD: - ASSERT(pNewHt, "copying META should not fail! cannot call DoCopy"); - ASSERT(pDest && (CH_TXTATR_INWORD == + OSL_ENSURE(pNewHt, "copying Meta should not fail!"); + OSL_ENSURE(pDest && (CH_TXTATR_INWORD == pDest->GetTxt().GetChar(*pNewHt->GetStart())), "missing CH_TXTATR?"); - if (pNewHt) - { - SwFmtMeta & rMeta(static_cast<SwFmtMeta&>(pNewHt->GetAttr())); - rMeta.DoCopy( const_cast<SwFmtMeta&>(pHt->GetMeta()) ); - } break; } } @@ -1483,7 +1478,8 @@ void SwTxtNode::CopyAttr( SwTxtNode *pDest, const xub_StrLen nTxtStartIdx, { // attribute in the area => copy SwTxtAttr *const pNewHt = pDest->InsertItem( - pHt->GetAttr(), nOldPos, nOldPos ); + pHt->GetAttr(), nOldPos, nOldPos, + nsSetAttrMode::SETATTR_IS_COPY); if ( pNewHt ) { lcl_CopyHint( nWhich, pHt, pNewHt, @@ -1494,7 +1490,8 @@ void SwTxtNode::CopyAttr( SwTxtNode *pDest, const xub_StrLen nTxtStartIdx, : 0 == pOtherDoc->GetRefMark( pHt->GetRefMark().GetRefName() ) ) { - pDest->InsertItem( pHt->GetAttr(), nOldPos, nOldPos ); + pDest->InsertItem( pHt->GetAttr(), nOldPos, nOldPos, + nsSetAttrMode::SETATTR_IS_COPY); } } } @@ -1716,9 +1713,9 @@ void SwTxtNode::CopyText( SwTxtNode *const pDest, if( pDest == this ) { - // die Daten kopieren + // copy the hint here, but insert it later pNewHt = MakeTxtAttr( *GetDoc(), pHt->GetAttr(), - nAttrStt, nAttrEnd ); + nAttrStt, nAttrEnd, COPY, pDest ); lcl_CopyHint(nWhich, pHt, pNewHt, 0, pDest); aArr.C40_INSERT( SwTxtAttr, pNewHt, aArr.Count() ); @@ -1726,7 +1723,9 @@ void SwTxtNode::CopyText( SwTxtNode *const pDest, else { pNewHt = pDest->InsertItem( pHt->GetAttr(), nAttrStt - nDeletedDummyChars, - nAttrEnd - nDeletedDummyChars, nsSetAttrMode::SETATTR_NOTXTATRCHR ); + nAttrEnd - nDeletedDummyChars, + nsSetAttrMode::SETATTR_NOTXTATRCHR + | nsSetAttrMode::SETATTR_IS_COPY); if (pNewHt) { lcl_CopyHint( nWhich, pHt, pNewHt, pOtherDoc, pDest ); @@ -2206,7 +2205,8 @@ void SwTxtNode::CutImpl( SwTxtNode * const pDest, const SwIndex & rDestStart, { const bool bSuccess( pDest->InsertHint( pNewHt, nsSetAttrMode::SETATTR_NOTXTATRCHR - | nsSetAttrMode::SETATTR_DONTREPLACE ) ); + | nsSetAttrMode::SETATTR_DONTREPLACE + | nsSetAttrMode::SETATTR_IS_COPY) ); if (bSuccess) { lcl_CopyHint( nWhich, pHt, pNewHt, pOtherDoc, pDest ); diff --git a/sw/source/core/txtnode/thints.cxx b/sw/source/core/txtnode/thints.cxx index 4d56942ecef4..4a0132bc7e68 100644 --- a/sw/source/core/txtnode/thints.cxx +++ b/sw/source/core/txtnode/thints.cxx @@ -995,7 +995,8 @@ SwTxtAttr* MakeRedlineTxtAttr( SwDoc & rDoc, SfxPoolItem & rAttr ) // create new text attribute SwTxtAttr* MakeTxtAttr( SwDoc & rDoc, SfxPoolItem& rAttr, - xub_StrLen nStt, xub_StrLen nEnd ) + xub_StrLen const nStt, xub_StrLen const nEnd, + CopyOrNew_t const bIsCopy, SwTxtNode *const pTxtNode) { if ( isCHRATR(rAttr.Which()) ) { @@ -1076,7 +1077,8 @@ SwTxtAttr* MakeTxtAttr( SwDoc & rDoc, SfxPoolItem& rAttr, break; case RES_TXTATR_META: case RES_TXTATR_METAFIELD: - pNew = new SwTxtMeta( static_cast<SwFmtMeta&>(rNew), nStt, nEnd ); + pNew = SwTxtMeta::CreateTxtMeta( rDoc.GetMetaFieldManager(), pTxtNode, + static_cast<SwFmtMeta&>(rNew), nStt, nEnd, bIsCopy ); break; default: ASSERT(RES_TXTATR_AUTOFMT == rNew.Which(), "unknown attribute"); @@ -1210,7 +1212,8 @@ SwTxtNode::InsertItem( SfxPoolItem& rAttr, ASSERT( !isCHRATR(rAttr.Which()), "AUTOSTYLES - " "SwTxtNode::InsertItem should not be called with character attributes"); - SwTxtAttr* const pNew = MakeTxtAttr( *GetDoc(), rAttr, nStart, nEnd ); + SwTxtAttr *const pNew = MakeTxtAttr( *GetDoc(), rAttr, nStart, nEnd, + (nMode & nsSetAttrMode::SETATTR_IS_COPY) ? COPY : NEW, this ); if ( pNew ) { diff --git a/sw/source/core/txtnode/txtatr2.cxx b/sw/source/core/txtnode/txtatr2.cxx index 5499eeab4439..482d7d56a16a 100644 --- a/sw/source/core/txtnode/txtatr2.cxx +++ b/sw/source/core/txtnode/txtatr2.cxx @@ -308,10 +308,25 @@ SwCharFmt* SwTxtRuby::GetCharFmt() * class SwTxtMeta *************************************************************************/ +SwTxtMeta * +SwTxtMeta::CreateTxtMeta( + ::sw::MetaFieldManager & i_rTargetDocManager, + SwTxtNode *const i_pTargetTxtNode, + SwFmtMeta & i_rAttr, + xub_StrLen const i_nStart, xub_StrLen const i_nEnd, bool const i_bIsCopy) +{ + if (COPY == i_bIsCopy) + { // i_rAttr is already cloned, now call DoCopy to copy the sw::Meta + OSL_ENSURE(i_pTargetTxtNode, "cannot copy Meta without target node"); + i_rAttr.DoCopy(i_rTargetDocManager, *i_pTargetTxtNode); + } + SwTxtMeta *const pTxtMeta(new SwTxtMeta(i_rAttr, i_nStart, i_nEnd)); + return pTxtMeta; +} + SwTxtMeta::SwTxtMeta( SwFmtMeta & i_rAttr, const xub_StrLen i_nStart, const xub_StrLen i_nEnd ) : SwTxtAttrNesting( i_rAttr, i_nStart, i_nEnd ) - , m_pTxtNode( 0 ) { i_rAttr.SetTxtAttr( this ); SetHasDummyChar(true); @@ -328,7 +343,6 @@ SwTxtMeta::~SwTxtMeta() void SwTxtMeta::ChgTxtNode(SwTxtNode * const pNode) { - m_pTxtNode = pNode; // before Notify! SwFmtMeta & rFmtMeta( static_cast<SwFmtMeta &>(GetAttr()) ); if (rFmtMeta.GetTxtAttr() == this) { diff --git a/sw/source/core/unocore/unorefmk.cxx b/sw/source/core/unocore/unorefmk.cxx index 25ce3ec6c441..c1c003791320 100644 --- a/sw/source/core/unocore/unorefmk.cxx +++ b/sw/source/core/unocore/unorefmk.cxx @@ -886,7 +886,7 @@ bool SwXMeta::SetContentRange( SwTxtMeta const * const pTxtAttr( pMeta->GetTxtAttr() ); if (pTxtAttr) { - rpNode = pTxtAttr->GetTxtNode(); + rpNode = pMeta->GetTxtNode(); if (rpNode) { // rStart points at the first position _within_ the meta! |