LibreOffice Module sw (master)  1
atrflyin.cxx
Go to the documentation of this file.
1 /* -*- Mode: C++; tab-width: 4; indent-tabs-mode: nil; c-basic-offset: 4 -*- */
2 /*
3  * This file is part of the LibreOffice project.
4  *
5  * This Source Code Form is subject to the terms of the Mozilla Public
6  * License, v. 2.0. If a copy of the MPL was not distributed with this
7  * file, You can obtain one at http://mozilla.org/MPL/2.0/.
8  *
9  * This file incorporates work covered by the following license notice:
10  *
11  * Licensed to the Apache Software Foundation (ASF) under one or more
12  * contributor license agreements. See the NOTICE file distributed
13  * with this work for additional information regarding copyright
14  * ownership. The ASF licenses this file to you under the Apache
15  * License, Version 2.0 (the "License"); you may not use this file
16  * except in compliance with the License. You may obtain a copy of
17  * the License at http://www.apache.org/licenses/LICENSE-2.0 .
18  */
19 
20 #include <hintids.hxx>
21 #include <doc.hxx>
22 #include <IDocumentUndoRedo.hxx>
24 #include <pam.hxx>
25 #include <flyfrm.hxx>
26 #include <ndtxt.hxx>
27 #include <frmfmt.hxx>
28 #include <fmtflcnt.hxx>
29 #include <txtflcnt.hxx>
30 #include <fmtanchr.hxx>
31 #include <txtfrm.hxx>
32 #include <flyfrms.hxx>
33 #include <objectformatter.hxx>
34 #include <calbck.hxx>
35 #include <dcontact.hxx>
36 
39  m_pTextAttr( nullptr ),
40  m_pFormat( pFrameFormat )
41 {
42 }
43 
44 bool SwFormatFlyCnt::operator==( const SfxPoolItem& rAttr ) const
45 {
46  assert(SfxPoolItem::operator==(rAttr));
47  return( m_pTextAttr && static_cast<const SwFormatFlyCnt&>(rAttr).m_pTextAttr &&
48  m_pTextAttr->GetStart() == static_cast<const SwFormatFlyCnt&>(rAttr).m_pTextAttr->GetStart() &&
49  m_pFormat == static_cast<const SwFormatFlyCnt&>(rAttr).GetFrameFormat() );
50 }
51 
53 {
54  return new SwFormatFlyCnt( m_pFormat );
55 }
56 
57 SwTextFlyCnt::SwTextFlyCnt( SwFormatFlyCnt& rAttr, sal_Int32 nStartPos )
58  : SwTextAttr( rAttr, nStartPos )
59 {
60  rAttr.m_pTextAttr = this;
61  SetHasDummyChar(true);
62 }
63 
91 {
92  SwFrameFormat* pFormat = GetFlyCnt().GetFrameFormat();
93  assert(pFormat);
94  // The FlyFrameFormat must be copied - CopyLayoutFormat
95  // (DocumentLayoutManager.cxx) creates the FlyFrameFormat and copies the
96  // content.
97 
98  // disable undo while copying attribute
99  ::sw::UndoGuard const undoGuard(pDoc->GetIDocumentUndoRedo());
100  SwFormatAnchor aAnchor( pFormat->GetAnchor() );
101  if ((RndStdIds::FLY_AT_PAGE != aAnchor.GetAnchorId()) &&
102  (pDoc != pFormat->GetDoc())) // different documents?
103  {
104  // JP 03.06.96: ensure that the copied anchor points to valid content!
105  // setting it to the correct position is done later.
106  SwNodeIndex aIdx( pDoc->GetNodes().GetEndOfExtras(), +2 );
107  SwContentNode* pCNd = aIdx.GetNode().GetContentNode();
108  if( !pCNd )
109  pCNd = pDoc->GetNodes().GoNext( &aIdx );
110 
111  SwPosition pos = *aAnchor.GetContentAnchor();
112  pos.nNode = aIdx;
113  if (RndStdIds::FLY_AS_CHAR == aAnchor.GetAnchorId())
114  {
115  pos.nContent.Assign( pCNd, 0 );
116  }
117  else
118  {
119  pos.nContent.Assign( nullptr, 0 );
120  assert(false);
121  }
122  aAnchor.SetAnchor( &pos );
123  }
124 
125  SwFrameFormat* pNew = pDoc->getIDocumentLayoutAccess().CopyLayoutFormat( *pFormat, aAnchor, false, false );
126  const_cast<SwFormatFlyCnt&>(GetFlyCnt()).SetFlyFormat( pNew );
127 }
128 
135 {
136  // for Undo, the new anchor must be known already!
137 
138  SwDoc* pDoc = const_cast<SwDoc*>(pNode->GetDoc());
139 
140  SwIndex aIdx( const_cast<SwTextNode*>(pNode), GetStart() );
141  SwPosition aPos( *pNode->StartOfSectionNode(), aIdx );
142  SwFrameFormat* pFormat = GetFlyCnt().GetFrameFormat();
143  SwFormatAnchor aAnchor( pFormat->GetAnchor() );
144  SwNode *const pOldNode(aAnchor.GetContentAnchor()
145  ? &aAnchor.GetContentAnchor()->nNode.GetNode()
146  : nullptr);
147 
148  if (!pOldNode || !pOldNode->GetNodes().IsDocNodes() ||
149  pOldNode != static_cast<SwNode const *>(pNode))
150  {
151  aPos.nNode = *pNode;
152  }
153  else
154  {
155  aPos.nNode = *pOldNode;
156  }
157 
158  aAnchor.SetType( RndStdIds::FLY_AS_CHAR ); // default!
159  aAnchor.SetAnchor( &aPos );
160 
161  // in case of anchor change, delete all FlyFrames
162  // JP 25.04.95: if the Frames can be moved within SplitNode, they don't
163  // need to be deleted
164  if( ( !pNode->GetpSwpHints() || !pNode->GetpSwpHints()->IsInSplitNode() )
165  && RES_DRAWFRMFMT != pFormat->Which() )
166  pFormat->DelFrames();
167 
168  // copy into a different document?
169  if( pDoc != pFormat->GetDoc() )
170  {
171  // disable undo while copying attribute
172  ::sw::UndoGuard const undoGuard(pDoc->GetIDocumentUndoRedo());
173  SwFrameFormat* pNew = pDoc->getIDocumentLayoutAccess().CopyLayoutFormat( *pFormat, aAnchor, false, false );
174 
175  ::sw::UndoGuard const undoGuardFormat(
176  pFormat->GetDoc()->GetIDocumentUndoRedo());
177  pFormat->GetDoc()->getIDocumentLayoutAccess().DelLayoutFormat( pFormat );
178  const_cast<SwFormatFlyCnt&>(GetFlyCnt()).SetFlyFormat( pNew );
179  }
180  else if( pNode->GetpSwpHints() &&
181  pNode->GetpSwpHints()->IsInSplitNode() &&
182  RES_DRAWFRMFMT != pFormat->Which() )
183  {
184  pFormat->LockModify();
185  pFormat->SetFormatAttr( aAnchor ); // only set the anchor
186  // tdf#91228 must notify the anchor nodes despite LockModify
187  assert(pOldNode);
188  pOldNode->RemoveAnchoredFly(pFormat);
189  aPos.nNode.GetNode().AddAnchoredFly(pFormat);
190  pFormat->UnlockModify();
191  }
192  else
193  {
194  assert(!pFormat->IsModifyLocked()); // need to notify anchor node
195  if (RES_DRAWFRMFMT == pFormat->Which())
196  {
197  if (SdrObject const*const pObj = pFormat->FindSdrObject())
198  { // tdf#123259 disconnect with *old* anchor position
199  static_cast<SwDrawContact*>(::GetUserCall(pObj))->DisconnectFromLayout(false);
200  }
201  }
202  pFormat->SetFormatAttr( aAnchor ); // only set the anchor
203  }
204 
205  // The node may have several SwTextFrames - for every SwTextFrame a
206  // SwFlyInContentFrame is created.
207 }
208 
209 
215 {
216  SwFrameFormat* pFrameFormat = GetFlyCnt().GetFrameFormat();
217  if( RES_DRAWFRMFMT == pFrameFormat->Which() )
218  {
219  OSL_ENSURE( false, "SwTextFlyCnt::GetFlyFrame_: DrawInCnt-under construction!" );
220  return nullptr;
221  }
222 
223  SwIterator<SwFlyFrame,SwFormat> aIter( *GetFlyCnt().m_pFormat );
224  assert(pCurrFrame->IsTextFrame());
225  SwFrame* pFrame = aIter.First();
226  if ( pFrame )
227  {
228  SwTextFrame *pFirst = const_cast<SwTextFrame*>(static_cast<const SwTextFrame*>(pCurrFrame));
229  while ( pFirst->IsFollow() )
230  pFirst = pFirst->FindMaster();
231  do
232  {
233  SwTextFrame *pTmp = pFirst;
234  do
235  { if( static_cast<SwFlyFrame*>(pFrame)->GetAnchorFrame() == static_cast<SwFrame*>(pTmp) )
236  {
237  if ( pTmp != pCurrFrame )
238  {
239  pTmp->RemoveFly( static_cast<SwFlyFrame*>(pFrame) );
240  const_cast<SwTextFrame*>(static_cast<const SwTextFrame*>(pCurrFrame))->AppendFly( static_cast<SwFlyFrame*>(pFrame) );
241  }
242  return static_cast<SwFlyInContentFrame*>(pFrame);
243  }
244  pTmp = pTmp->GetFollow();
245  } while ( pTmp );
246 
247  pFrame = aIter.Next();
248 
249  } while( pFrame );
250  }
251 
252  // We did not find a matching FlyFrame, so create a new one.
253  // AppendFly() triggers a reformat of pCurrentFrame. However, the
254  // recursion is blocked by the lock mechanism in SwTextFrame::Format().
255  SwFrame* pCurrentFrame = const_cast<SwFrame*>(pCurrFrame);
256  SwFlyInContentFrame *pFly = new SwFlyInContentFrame(static_cast<SwFlyFrameFormat*>(pFrameFormat), pCurrentFrame, pCurrentFrame);
257  pCurrentFrame->AppendFly(pFly);
258  pFly->RegistFlys();
259 
260  // We must ensure that the content of the FlyInCnt is fully formatted
261  // right after construction.
262  // #i26945# - Use new object formatter to format Writer
263  // fly frame and its content.
264  SwObjectFormatter::FormatObj( *pFly, const_cast<SwFrame*>(pCurrFrame),
265  pCurrFrame->FindPageFrame() );
266 
267  return pFly;
268 }
269 
270 /* vim:set shiftwidth=4 softtabstop=4 expandtab: */
void SetAnchor(const SwTextNode *pNode)
SetAnchor() is called by SwTextNode::InsertHint() and sets the anchor position in the SwFlyFrameForma...
Definition: atrflyin.cxx:134
Base class of the Writer layout elements.
Definition: frame.hxx:295
Represents the visualization of a paragraph.
Definition: txtfrm.hxx:149
bool IsFollow() const
Definition: flowfrm.hxx:166
Marks a position in the document model.
Definition: pam.hxx:35
SwpHints * GetpSwpHints()
Definition: ndtxt.hxx:218
void SetHasDummyChar(const bool bFlag)
Definition: txatbase.hxx:72
SwNodeIndex nNode
Definition: pam.hxx:37
SwFlyInContentFrame * GetFlyFrame_(const SwFrame *pCurrFrame)
GetFlyFrame_() is called during text formatting by SwTextFormatter and searches for the SwFlyFrame fo...
Definition: atrflyin.cxx:214
Definition: doc.hxx:186
TElementType * Next()
Definition: calbck.hxx:373
virtual SwFrameFormat * CopyLayoutFormat(const SwFrameFormat &rSrc, const SwFormatAnchor &rNewAnchor, bool bSetTextFlyAtt, bool bMakeFrames)=0
SwFormatFlyCnt(SwFrameFormat *pFrameFormat)
Definition: atrflyin.cxx:37
IDocumentUndoRedo & GetIDocumentUndoRedo()
Definition: doc.cxx:143
SwContact * GetUserCall(const SdrObject *pObj)
Returns the UserCall if applicable from the group object.
Definition: dcontact.cxx:170
virtual bool operator==(const SfxPoolItem &) const override
"Pure virtual methods" of SfxPoolItem.
Definition: atrflyin.cxx:44
virtual SwFormatFlyCnt * Clone(SfxItemPool *pPool=nullptr) const override
Definition: atrflyin.cxx:52
SwFrameFormat * m_pFormat
My Fly/DrawFrame-format.
Definition: fmtflcnt.hxx:36
void AppendFly(SwFlyFrame *pNew)
Definition: fly.cxx:2118
SwIndex nContent
Definition: pam.hxx:38
size_t pos
bool IsTextFrame() const
Definition: frame.hxx:1210
sal_Int32 GetStart() const
Definition: txatbase.hxx:82
SwTextFrame * GetFollow()
Definition: txtfrm.hxx:848
SwTextFlyCnt(SwFormatFlyCnt &rAttr, sal_Int32 nStart)
Definition: atrflyin.cxx:57
SwTextFlyCnt * m_pTextAttr
Definition: fmtflcnt.hxx:35
Style of a layout element.
Definition: frmfmt.hxx:57
bool IsInSplitNode() const
Definition: ndhints.hxx:206
void RemoveFly(SwFlyFrame *pToRemove)
Definition: fly.cxx:2136
const SwFormatAnchor & GetAnchor(bool=true) const
Definition: fmtanchr.hxx:81
const SwStartNode * StartOfSectionNode() const
Definition: node.hxx:131
SwIndex & Assign(SwIndexReg *, sal_Int32)
Definition: index.cxx:198
SwPageFrame * FindPageFrame()
Definition: frame.hxx:658
TElementType * First()
Definition: calbck.hxx:342
SwContentNode * GetContentNode()
Definition: node.hxx:615
FlyAnchors.
Definition: fmtanchr.hxx:34
void DelFrames(SwRootFrame const *pLayout)
Method deletes all views of document for the node.
Definition: node.cxx:1345
SwDoc * GetDoc()
Definition: node.hxx:702
Marks a character position inside a document model node.
Definition: index.hxx:37
Marks a node in the document model.
Definition: ndindex.hxx:31
const SwDoc * GetDoc() const
The document is set in SwAttrPool now, therefore you always can access it.
Definition: format.hxx:119
void CopyFlyFormat(SwDoc *pDoc)
An overview of how a new SwTextFlyCnt is created: MakeTextAttr() is called e.g.
Definition: atrflyin.cxx:90
IDocumentLayoutAccess const & getIDocumentLayoutAccess() const
Definition: doc.cxx:404
sal_uInt16 Which() const
for Querying of Writer-functions.
Definition: format.hxx:78
SwTextNode is a paragraph in the document model.
Definition: ndtxt.hxx:79
#define RES_DRAWFRMFMT
Definition: hintids.hxx:372
SwTextFrame * FindMaster() const
Definition: flowfrm.cxx:679
static bool FormatObj(SwAnchoredObject &_rAnchoredObj, SwFrame *_pAnchorFrame=nullptr, const SwPageFrame *_pPageFrame=nullptr)
method to format a given floating screen object
SwNodes & GetNodes()
Definition: doc.hxx:404
#define RES_TXTATR_FLYCNT
Definition: hintids.hxx:245
Format of a fly content.
Definition: fmtflcnt.hxx:32
SwFrameFormat * GetFrameFormat() const
Definition: fmtflcnt.hxx:45
SwNode & GetEndOfExtras() const
This is the last EndNode of a special section.
Definition: ndarr.hxx:161
SwContentNode * GoNext(SwNodeIndex *) const
Definition: nodes.cxx:1277
const SwFormatFlyCnt & GetFlyCnt() const
Definition: txatbase.hxx:206
Base class of the Writer document model elements.
Definition: node.hxx:79