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 #include <textboxhelper.hxx>
37 
40  m_pTextAttr( nullptr ),
41  m_pFormat( pFrameFormat )
42 {
43 }
44 
45 bool SwFormatFlyCnt::operator==( const SfxPoolItem& rAttr ) const
46 {
47  assert(SfxPoolItem::operator==(rAttr));
48  return( m_pTextAttr && static_cast<const SwFormatFlyCnt&>(rAttr).m_pTextAttr &&
49  m_pTextAttr->GetStart() == static_cast<const SwFormatFlyCnt&>(rAttr).m_pTextAttr->GetStart() &&
50  m_pFormat == static_cast<const SwFormatFlyCnt&>(rAttr).GetFrameFormat() );
51 }
52 
54 {
55  return new SwFormatFlyCnt( m_pFormat );
56 }
57 
58 SwTextFlyCnt::SwTextFlyCnt( SwFormatFlyCnt& rAttr, sal_Int32 nStartPos )
59  : SwTextAttr( rAttr, nStartPos )
60 {
61  rAttr.m_pTextAttr = this;
62  SetHasDummyChar(true);
63 }
64 
92 {
93  SwFrameFormat* pFormat = GetFlyCnt().GetFrameFormat();
94  assert(pFormat);
95  // The FlyFrameFormat must be copied - CopyLayoutFormat
96  // (DocumentLayoutManager.cxx) creates the FlyFrameFormat and copies the
97  // content.
98 
99  // disable undo while copying attribute
100  ::sw::UndoGuard const undoGuard(pDoc->GetIDocumentUndoRedo());
101  SwFormatAnchor aAnchor( pFormat->GetAnchor() );
102  if ((RndStdIds::FLY_AT_PAGE != aAnchor.GetAnchorId()) &&
103  (pDoc != pFormat->GetDoc())) // different documents?
104  {
105  // JP 03.06.96: ensure that the copied anchor points to valid content!
106  // setting it to the correct position is done later.
107  SwNodeIndex aIdx( pDoc->GetNodes().GetEndOfExtras(), +2 );
108  SwContentNode* pCNd = aIdx.GetNode().GetContentNode();
109  if( !pCNd )
110  pCNd = pDoc->GetNodes().GoNext( &aIdx );
111 
112  SwPosition pos = *aAnchor.GetContentAnchor();
113  pos.nNode = aIdx;
114  if (RndStdIds::FLY_AS_CHAR == aAnchor.GetAnchorId())
115  {
116  pos.nContent.Assign( pCNd, 0 );
117  }
118  else
119  {
120  pos.nContent.Assign( nullptr, 0 );
121  assert(false);
122  }
123  aAnchor.SetAnchor( &pos );
124  }
125 
126  SwFrameFormat* pNew = pDoc->getIDocumentLayoutAccess().CopyLayoutFormat( *pFormat, aAnchor, false, false );
127  const_cast<SwFormatFlyCnt&>(GetFlyCnt()).SetFlyFormat( pNew );
128 }
129 
136 {
137  // for Undo, the new anchor must be known already!
138 
139  SwDoc* pDoc = const_cast<SwDoc*>(pNode->GetDoc());
140 
141  SwIndex aIdx( const_cast<SwTextNode*>(pNode), GetStart() );
142  SwPosition aPos( *pNode->StartOfSectionNode(), aIdx );
143  SwFrameFormat* pFormat = GetFlyCnt().GetFrameFormat();
144  SwFormatAnchor aAnchor( pFormat->GetAnchor() );
145  SwNode *const pOldNode(aAnchor.GetContentAnchor()
146  ? &aAnchor.GetContentAnchor()->nNode.GetNode()
147  : nullptr);
148 
149  if (!pOldNode || !pOldNode->GetNodes().IsDocNodes() ||
150  pOldNode != static_cast<SwNode const *>(pNode))
151  {
152  aPos.nNode = *pNode;
153  }
154  else
155  {
156  aPos.nNode = *pOldNode;
157  }
158 
159  aAnchor.SetType( RndStdIds::FLY_AS_CHAR ); // default!
160  aAnchor.SetAnchor( &aPos );
161 
162  // in case of anchor change, delete all FlyFrames
163  // JP 25.04.95: if the Frames can be moved within SplitNode, they don't
164  // need to be deleted
165  if( ( !pNode->GetpSwpHints() || !pNode->GetpSwpHints()->IsInSplitNode() )
166  && RES_DRAWFRMFMT != pFormat->Which() )
167  pFormat->DelFrames();
168 
169  // copy into a different document?
170  if( pDoc != pFormat->GetDoc() )
171  {
172  // disable undo while copying attribute
173  ::sw::UndoGuard const undoGuard(pDoc->GetIDocumentUndoRedo());
174  SwFrameFormat* pNew = pDoc->getIDocumentLayoutAccess().CopyLayoutFormat( *pFormat, aAnchor, false, false );
175 
176  ::sw::UndoGuard const undoGuardFormat(
177  pFormat->GetDoc()->GetIDocumentUndoRedo());
178  pFormat->GetDoc()->getIDocumentLayoutAccess().DelLayoutFormat( pFormat );
179  const_cast<SwFormatFlyCnt&>(GetFlyCnt()).SetFlyFormat( pNew );
180  }
181  else if( pNode->GetpSwpHints() &&
182  pNode->GetpSwpHints()->IsInSplitNode() &&
183  RES_DRAWFRMFMT != pFormat->Which() )
184  {
185  pFormat->LockModify();
186  pFormat->SetFormatAttr( aAnchor ); // only set the anchor
187  // tdf#91228 must notify the anchor nodes despite LockModify
188  assert(pOldNode);
189  pOldNode->RemoveAnchoredFly(pFormat);
190  aPos.nNode.GetNode().AddAnchoredFly(pFormat);
191  pFormat->UnlockModify();
192  }
193  else
194  {
195  assert(!pFormat->IsModifyLocked()); // need to notify anchor node
196  if (RES_DRAWFRMFMT == pFormat->Which())
197  {
198  if (SdrObject const*const pObj = pFormat->FindSdrObject())
199  { // tdf#123259 disconnect with *old* anchor position
200  static_cast<SwDrawContact*>(::GetUserCall(pObj))->DisconnectFromLayout(false);
201  }
202  }
203  pFormat->SetFormatAttr( aAnchor ); // only set the anchor
204 
205  // If the draw format has a TextBox, then set its anchor as well.
206  if (SwFrameFormat* pTextBox
208  {
209  SwFormatAnchor aTextBoxAnchor(pTextBox->GetAnchor());
210  aTextBoxAnchor.SetAnchor(aAnchor.GetContentAnchor());
211 
212  // SwFlyAtContentFrame::Modify() assumes the anchor has a matching layout frame, which
213  // may not be the case when we're in the process of a node split, so block
214  // notifications.
215  bool bIsInSplitNode = pNode->GetpSwpHints() && pNode->GetpSwpHints()->IsInSplitNode();
216  if (bIsInSplitNode)
217  {
218  pTextBox->LockModify();
219  }
220 
221  pTextBox->SetFormatAttr(aTextBoxAnchor);
222 
223  if (bIsInSplitNode)
224  {
225  pOldNode->RemoveAnchoredFly(pTextBox);
226  aPos.nNode.GetNode().AddAnchoredFly(pTextBox);
227  pTextBox->UnlockModify();
228  }
229  }
230  }
231 
232  // The node may have several SwTextFrames - for every SwTextFrame a
233  // SwFlyInContentFrame is created.
234 }
235 
236 
242 {
243  SwFrameFormat* pFrameFormat = GetFlyCnt().GetFrameFormat();
244  if( RES_DRAWFRMFMT == pFrameFormat->Which() )
245  {
246  OSL_ENSURE( false, "SwTextFlyCnt::GetFlyFrame_: DrawInCnt-under construction!" );
247  return nullptr;
248  }
249 
250  SwIterator<SwFlyFrame,SwFormat> aIter( *GetFlyCnt().m_pFormat );
251  assert(pCurrFrame->IsTextFrame());
252  SwFrame* pFrame = aIter.First();
253  if ( pFrame )
254  {
255  SwTextFrame *pFirst = const_cast<SwTextFrame*>(static_cast<const SwTextFrame*>(pCurrFrame));
256  while ( pFirst->IsFollow() )
257  pFirst = pFirst->FindMaster();
258  do
259  {
260  SwTextFrame *pTmp = pFirst;
261  do
262  { if( static_cast<SwFlyFrame*>(pFrame)->GetAnchorFrame() == static_cast<SwFrame*>(pTmp) )
263  {
264  if ( pTmp != pCurrFrame )
265  {
266  pTmp->RemoveFly( static_cast<SwFlyFrame*>(pFrame) );
267  const_cast<SwTextFrame*>(static_cast<const SwTextFrame*>(pCurrFrame))->AppendFly( static_cast<SwFlyFrame*>(pFrame) );
268  }
269  return static_cast<SwFlyInContentFrame*>(pFrame);
270  }
271  pTmp = pTmp->GetFollow();
272  } while ( pTmp );
273 
274  pFrame = aIter.Next();
275 
276  } while( pFrame );
277  }
278 
279  // We did not find a matching FlyFrame, so create a new one.
280  // AppendFly() triggers a reformat of pCurrentFrame. However, the
281  // recursion is blocked by the lock mechanism in SwTextFrame::Format().
282  SwFrame* pCurrentFrame = const_cast<SwFrame*>(pCurrFrame);
283  SwFlyInContentFrame *pFly = new SwFlyInContentFrame(static_cast<SwFlyFrameFormat*>(pFrameFormat), pCurrentFrame, pCurrentFrame);
284  pCurrentFrame->AppendFly(pFly);
285  pFly->RegistFlys();
286 
287  // We must ensure that the content of the FlyInCnt is fully formatted
288  // right after construction.
289  // #i26945# - Use new object formatter to format Writer
290  // fly frame and its content.
291  SwObjectFormatter::FormatObj( *pFly, const_cast<SwFrame*>(pCurrFrame),
292  pCurrFrame->FindPageFrame() );
293 
294  return pFly;
295 }
296 
297 /* 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:135
Base class of the Writer layout elements.
Definition: frame.hxx:297
Represents the visualization of a paragraph.
Definition: txtfrm.hxx:151
bool IsFollow() const
Definition: flowfrm.hxx:166
Marks a position in the document model.
Definition: pam.hxx:35
SwpHints * GetpSwpHints()
Definition: ndtxt.hxx:219
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:241
Definition: doc.hxx:184
TElementType * Next()
Definition: calbck.hxx:351
virtual SwFrameFormat * CopyLayoutFormat(const SwFrameFormat &rSrc, const SwFormatAnchor &rNewAnchor, bool bSetTextFlyAtt, bool bMakeFrames)=0
constexpr TypedWhichId< SwFormatFlyCnt > RES_TXTATR_FLYCNT(57)
SwFormatFlyCnt(SwFrameFormat *pFrameFormat)
Definition: atrflyin.cxx:38
IDocumentUndoRedo & GetIDocumentUndoRedo()
Definition: doc.cxx:143
SwContact * GetUserCall(const SdrObject *pObj)
Returns the UserCall if applicable from the group object.
Definition: dcontact.cxx:171
static SwFrameFormat * getOtherTextBoxFormat(const SwFrameFormat *pFormat, sal_uInt16 nType)
If we have an associated TextFrame, then return that.
virtual bool operator==(const SfxPoolItem &) const override
"Pure virtual methods" of SfxPoolItem.
Definition: atrflyin.cxx:45
virtual SwFormatFlyCnt * Clone(SfxItemPool *pPool=nullptr) const override
Definition: atrflyin.cxx:53
SwFrameFormat * m_pFormat
My Fly/DrawFrame-format.
Definition: fmtflcnt.hxx:36
void AppendFly(SwFlyFrame *pNew)
Definition: fly.cxx:2119
SwIndex nContent
Definition: pam.hxx:38
const BorderLinePrimitive2D *pCandidateB assert(pCandidateA)
size_t pos
bool IsTextFrame() const
Definition: frame.hxx:1212
sal_Int32 GetStart() const
Definition: txatbase.hxx:82
SwTextFrame * GetFollow()
Definition: txtfrm.hxx:846
SwTextFlyCnt(SwFormatFlyCnt &rAttr, sal_Int32 nStart)
Definition: atrflyin.cxx:58
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:2137
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:206
SwPageFrame * FindPageFrame()
Definition: frame.hxx:660
TElementType * First()
Definition: calbck.hxx:343
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:1388
SwDoc * GetDoc()
Definition: node.hxx:702
Marks a character position inside a document model node.
Definition: index.hxx:33
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:91
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:80
SwTextFrame * FindMaster() const
Definition: flowfrm.cxx:681
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:403
constexpr TypedWhichId< SwDrawFrameFormat > RES_DRAWFRMFMT(157)
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:1283
void SetAnchor(const SwPosition *pPos)
Definition: atrfrm.cxx:1516
const SwFormatFlyCnt & GetFlyCnt() const
Definition: txatbase.hxx:206
Base class of the Writer document model elements.
Definition: node.hxx:79