LibreOffice Module sw (master)  1
swbaslnk.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 <vcl/svapp.hxx>
22 
23 #include <sfx2/docfile.hxx>
24 #include <sfx2/lnkbase.hxx>
25 #include <sfx2/objsh.hxx>
26 #include <editeng/boxitem.hxx>
27 #include <sfx2/linkmgr.hxx>
28 #include <sfx2/event.hxx>
29 #include <sot/exchange.hxx>
30 #include <fmtfsize.hxx>
31 #include <fmtanchr.hxx>
32 #include <frmatr.hxx>
33 #include <frmfmt.hxx>
34 #include <doc.hxx>
37 #include <pam.hxx>
38 #include <swtable.hxx>
39 #include <swevent.hxx>
40 #include <swbaslnk.hxx>
41 #include <swserv.hxx>
42 #include <viewsh.hxx>
43 #include <ndgrf.hxx>
44 #include <hints.hxx>
45 #include <cntfrm.hxx>
46 #include <htmltbl.hxx>
47 #include <calbck.hxx>
48 #include <dialoghelp.hxx>
49 #include <memory>
50 
51 using namespace com::sun::star;
52 
53 static bool SetGrfFlySize( const Size& rGrfSz, SwGrfNode* pGrfNd, const Size &rOrigGrfSize );
54 
55 
56 static void lcl_CallModify( SwGrfNode& rGrfNd, SfxPoolItem& rItem )
57 {
58  //call first all not SwNoTextFrames, then the SwNoTextFrames.
59  // The reason is, that in the SwNoTextFrames the Graphic
60  // after a Paint will be swapped out! So all other "behind"
61  // them haven't a loaded Graphic.
62  rGrfNd.LockModify();
63  {
64  SwIterator<SwClient,SwGrfNode> aIter(rGrfNd);
65  for(SwClient* pLast = aIter.First(); pLast; pLast = aIter.Next())
66  if(dynamic_cast<const SwContentFrame*>( pLast) == nullptr)
67  pLast->ModifyNotification(&rItem, &rItem);
68  }
69  {
71  for(SwClient* pLast = aIter.First(); pLast; pLast = aIter.Next())
72  pLast->ModifyNotification(&rItem, &rItem);
73  }
74  rGrfNd.UnlockModify();
75 }
76 
78  const OUString& rMimeType, const uno::Any & rValue )
79 {
80  if( !m_pContentNode )
81  {
82  OSL_ENSURE(false, "DataChanged without ContentNode" );
83  return ERROR_GENERAL;
84  }
85 
86  SwDoc* pDoc = m_pContentNode->GetDoc();
87  if( pDoc->IsInDtor() || ChkNoDataFlag() )
88  {
89  return SUCCESS;
90  }
91 
93 
94  if( m_pContentNode->IsNoTextNode() &&
96  {
97  // Only a status change - serve Events?
98  OUString sState;
99 
100  if( rValue.hasValue() && ( rValue >>= sState ))
101  {
102  SvMacroItemId nEvent = SvMacroItemId::NONE;
103  switch( sState.toInt32() )
104  {
105  case sfx2::LinkManager::STATE_LOAD_OK: nEvent = SvMacroItemId::OnImageLoadDone; break;
106  case sfx2::LinkManager::STATE_LOAD_ERROR: nEvent = SvMacroItemId::OnImageLoadError; break;
107  case sfx2::LinkManager::STATE_LOAD_ABORT: nEvent = SvMacroItemId::OnImageLoadCancel; break;
108  }
109 
110  SwFrameFormat* pFormat;
111  if( nEvent != SvMacroItemId::NONE && nullptr != ( pFormat = m_pContentNode->GetFlyFormat() ))
112  {
113  SwCallMouseEvent aCallEvent;
114  aCallEvent.Set( EVENT_OBJECT_IMAGE, pFormat );
115  pDoc->CallEvent( nEvent, aCallEvent );
116  }
117  }
118  return SUCCESS; // That's it!
119  }
120 
121  bool bUpdate = false;
122  bool bFrameInPaint = false;
123  Size aGrfSz, aOldSz;
124 
125  SwGrfNode* pSwGrfNode = nullptr;
126 
127  if (m_pContentNode->IsGrfNode())
128  {
129  pSwGrfNode = m_pContentNode->GetGrfNode();
130  assert(pSwGrfNode && "Error, pSwGrfNode expected when node answers IsGrfNode() with true (!)");
131  aOldSz = pSwGrfNode->GetTwipSize();
132  const GraphicObject& rGrfObj = pSwGrfNode->GetGrfObj();
133 
134  bFrameInPaint = pSwGrfNode->IsFrameInPaint();
135 
136  Graphic aGrf;
137 
138  // tdf#124698 if any auth dialog is needed, find what the parent window should be
139  weld::Window* pDlgParent = GetFrameWeld(pDoc);
140 
141  if (pDoc->getIDocumentLinksAdministration().GetLinkManager().GetGraphicFromAny(rMimeType, rValue, aGrf, pDlgParent) &&
142  ( GraphicType::Default != aGrf.GetType() ||
143  GraphicType::Default != rGrfObj.GetType() ) )
144  {
145  aGrfSz = ::GetGraphicSizeTwip( aGrf, nullptr );
146 
147  pSwGrfNode->SetGraphic(aGrf);
148  bUpdate = true;
149 
150  // Always use the correct graphic size
151  if( aGrfSz.Height() && aGrfSz.Width() &&
152  aOldSz.Height() && aOldSz.Width() &&
153  aGrfSz != aOldSz )
154  {
155  pSwGrfNode->SetTwipSize(aGrfSz);
156  aOldSz = aGrfSz;
157  }
158  }
159  }
160  else if( m_pContentNode->IsOLENode() )
161  bUpdate = true;
162 
163  if ( !bUpdate || bFrameInPaint )
164  return SUCCESS;
165 
166  if (pSwGrfNode)
167  {
168  if (!SetGrfFlySize(aGrfSz, pSwGrfNode, aOldSz))
169  {
171  lcl_CallModify(*pSwGrfNode, aMsgHint);
172  return SUCCESS;
173  }
174  }
175 
176  return SUCCESS;
177 }
178 
179 static bool SetGrfFlySize( const Size& rGrfSz, SwGrfNode* pGrfNd, const Size& rOrigGrfSize )
180 {
181  bool bRet = false;
183  std::unique_ptr<CurrShell> pCurr;
184  if ( pGrfNd->GetDoc()->GetEditShell() )
185  pCurr.reset(new CurrShell( pSh ));
186 
187  Size aSz = rOrigGrfSize;
188  if ( !(aSz.Width() && aSz.Height()) &&
189  rGrfSz.Width() && rGrfSz.Height() )
190  {
191  SwFrameFormat* pFormat = nullptr;
192  if (pGrfNd->IsChgTwipSize())
193  pFormat = pGrfNd->GetFlyFormat();
194  if (nullptr != pFormat)
195  {
196  Size aCalcSz( aSz );
197  if ( !aSz.Height() && aSz.Width() )
198  // Calculate the right height
199  aCalcSz.setHeight( rGrfSz.Height() *
200  aSz.Width() / rGrfSz.Width() );
201  else if ( !aSz.Width() && aSz.Height() )
202  // Calculate the right width
203  aCalcSz.setWidth( rGrfSz.Width() *
204  aSz.Height() / rGrfSz.Height() );
205  else
206  // Take over height and width
207  aCalcSz = rGrfSz;
208 
209  const SvxBoxItem &rBox = pFormat->GetBox();
210  aCalcSz.AdjustWidth(rBox.CalcLineSpace(SvxBoxItemLine::LEFT) +
211  rBox.CalcLineSpace(SvxBoxItemLine::RIGHT) );
212  aCalcSz.AdjustHeight(rBox.CalcLineSpace(SvxBoxItemLine::TOP) +
213  rBox.CalcLineSpace(SvxBoxItemLine::BOTTOM) );
214  const SwFormatFrameSize& rOldAttr = pFormat->GetFrameSize();
215  if( rOldAttr.GetSize() != aCalcSz )
216  {
217  SwFormatFrameSize aAttr( rOldAttr );
218  aAttr.SetSize( aCalcSz );
219  pFormat->SetFormatAttr( aAttr );
220  bRet = true;
221  }
222 
223  if( !aSz.Width() )
224  {
225  // If the graphic is anchored in a table, we need to recalculate
226  // the table rows
227  const SwDoc *pDoc = pGrfNd->GetDoc();
228  const SwPosition* pAPos = pFormat->GetAnchor().GetContentAnchor();
229  SwTableNode *pTableNd;
230  if (pAPos && nullptr != (pTableNd = pAPos->nNode.GetNode().FindTableNode()))
231  {
232  const bool bLastGrf = !pTableNd->GetTable().DecGrfsThatResize();
233  SwHTMLTableLayout *pLayout =
234  pTableNd->GetTable().GetHTMLTableLayout();
235  if( pLayout )
236  {
237  const sal_uInt16 nBrowseWidth =
238  pLayout->GetBrowseWidthByTable( *pDoc );
239  if ( nBrowseWidth )
240  {
241  pLayout->Resize( nBrowseWidth, true, true,
242  bLastGrf ? HTMLTABLE_RESIZE_NOW
243  : 500 );
244  }
245  }
246  }
247  }
248  }
249 
250  // SetTwipSize rescales an ImageMap if needed for which
251  // it requires the Frame Format
252  pGrfNd->SetTwipSize( rGrfSz );
253  }
254 
255  return bRet;
256 }
257 
258 bool SwBaseLink::SwapIn( bool bWaitForData, bool bNativFormat )
259 {
260  if( !GetObj() && ( bNativFormat || ( !IsSynchron() && bWaitForData ) ))
261  {
262  AddNextRef();
263  GetRealObject_();
264  ReleaseRef();
265  }
266 
267  bool bRes = false;
268 
269  if( GetObj() )
270  {
271  OUString aMimeType( SotExchange::GetFormatMimeType( GetContentType() ));
272  uno::Any aValue;
273  (void)GetObj()->GetData( aValue, aMimeType, !IsSynchron() && bWaitForData );
274 
275  if( bWaitForData && !GetObj() )
276  {
277  OSL_ENSURE( false, "The SvxFileObject was deleted in a GetData!" );
278  }
279  else
280  {
281  bRes = aValue.hasValue();
282  if ( bRes )
283  {
284  DataChanged( aMimeType, aValue );
285  }
286  }
287  }
288  else if( !IsSynchron() && bWaitForData )
289  {
290  SetSynchron( true );
291  bRes = Update();
292  SetSynchron( false );
293  }
294  else
295  bRes = Update();
296 
297  return bRes;
298 }
299 
301 {
302  if( m_pContentNode && !m_pContentNode->GetDoc()->IsInDtor() )
303  {
304  // Delete the connection
305  if( m_pContentNode->IsGrfNode() )
306  static_cast<SwGrfNode*>(m_pContentNode)->ReleaseLink();
307  }
308  SvBaseLink::Closed();
309 }
310 
312 {
313  if (m_pContentNode)
314  {
315  SwFrameFormat *const pFormat = m_pContentNode->GetFlyFormat();
316  if (pFormat)
317  {
318  const SwFormatAnchor& rAnchor = pFormat->GetAnchor();
319  SwPosition const*const pAPos = rAnchor.GetContentAnchor();
320  if (pAPos &&
321  ((RndStdIds::FLY_AS_CHAR == rAnchor.GetAnchorId()) ||
322  (RndStdIds::FLY_AT_CHAR == rAnchor.GetAnchorId()) ||
323  (RndStdIds::FLY_AT_FLY == rAnchor.GetAnchorId()) ||
324  (RndStdIds::FLY_AT_PARA == rAnchor.GetAnchorId())))
325  {
326  return &pAPos->nNode.GetNode();
327  }
328  return nullptr;
329  }
330  }
331 
332  OSL_ENSURE( false, "GetAnchor is not shadowed" );
333  return nullptr;
334 }
335 
336 bool SwBaseLink::IsRecursion( const SwBaseLink* pChkLnk ) const
337 {
338  tools::SvRef<SwServerObject> aRef( static_cast<SwServerObject*>(GetObj()) );
339  if( aRef.is() )
340  {
341  // As it's a ServerObject, we query all contained Links
342  // if we are contained in them. Else we have a recursion.
343  return aRef->IsLinkInServer( pChkLnk );
344  }
345  return false;
346 }
347 
349 {
350  // Not Graphic or OLE Links
351  // Fields or Sections have their own derivation!
352  return false;
353 }
354 
356 {
357 }
358 
359 /* vim:set shiftwidth=4 softtabstop=4 expandtab: */
long Width() const
bool is() const
bool hasValue()
Marks a position in the document model.
Definition: pam.hxx:35
bool IsLinkInServer(const SwBaseLink *) const
Definition: swserv.cxx:181
long AdjustWidth(long n)
long Height() const
SwNodeIndex nNode
Definition: pam.hxx:37
sal_uIntPtr sal_uLong
bool IsFrameInPaint() const
Definition: ndgrf.hxx:102
Definition: doc.hxx:184
TElementType * Next()
Definition: calbck.hxx:351
IDocumentLinksAdministration const & getIDocumentLinksAdministration() const
Definition: doc.cxx:259
SvMacroItemId
SwNode & GetNode() const
Definition: ndindex.hxx:119
long AdjustHeight(long n)
const SvxBoxItem & GetBox(bool=true) const
Definition: frmatr.hxx:84
sal_uInt16 CallEvent(SvMacroItemId nEvent, const SwCallMouseEvent &rCallEvent, bool bChkPtr=false)
Definition: docbasic.cxx:131
const BorderLinePrimitive2D *pCandidateB assert(pCandidateA)
const SwTable & GetTable() const
Definition: node.hxx:497
SotClipboardFormatId
weld::Window * GetFrameWeld(const SfxFrame *pFrame)
Definition: dialoghelp.cxx:20
static bool SetGrfFlySize(const Size &rGrfSz, SwGrfNode *pGrfNd, const Size &rOrigGrfSize)
Definition: swbaslnk.cxx:179
sal_uInt16 GetBrowseWidthByTable(const SwDoc &rDoc) const
Calculates available width by the table-frame or static GetBrowseWidth if no layout exists...
Definition: htmltbl.cxx:369
Style of a layout element.
Definition: frmfmt.hxx:57
const SwFormatAnchor & GetAnchor(bool=true) const
Definition: fmtanchr.hxx:81
void SetGraphic(const Graphic &rGraphic)
isolated only way to set GraphicObject to allow more actions when doing so
Definition: ndgrf.cxx:356
GraphicType GetType() const
RndStdIds GetAnchorId() const
Definition: fmtanchr.hxx:65
SwEditShell const * GetEditShell() const
Definition: doccorr.cxx:327
const SwPosition * GetContentAnchor() const
Definition: fmtanchr.hxx:67
TElementType * First()
Definition: calbck.hxx:343
FlyAnchors.
Definition: fmtanchr.hxx:34
bool IsChgTwipSize() const
Definition: ndgrf.hxx:96
SwDoc * GetDoc()
Definition: node.hxx:702
const char *const aMimeType[]
static void lcl_CallModify(SwGrfNode &rGrfNd, SfxPoolItem &rItem)
Definition: swbaslnk.cxx:56
void UnlockModify()
Definition: calbck.hxx:214
void LockModify()
Definition: calbck.hxx:213
bool Resize(sal_uInt16 nAbsAvail, bool bRecalc=false, bool bForce=false, sal_uLong nDelay=0)
Recalculation of table widths for available width that has been passed.
Definition: htmltbl.cxx:1691
static OUString GetFormatMimeType(SotClipboardFormatId nFormat)
void SetTwipSize(const Size &rSz)
Definition: ndgrf.cxx:621
bool GetGraphicFromAny(const OUString &rMimeType, const css::uno::Any &rValue, Graphic &rGrf, weld::Window *pParentWin)
const GraphicObject & GetGrfObj(bool bWait=false) const
Definition: ndgrf.cxx:368
IDocumentLayoutAccess const & getIDocumentLayoutAccess() const
Definition: doc.cxx:404
GraphicType GetType() const
virtual Size GetTwipSize() const override
Definition: ndgrf.cxx:421
void Set(SwCallEventObjectType eTyp, const SwFrameFormat *pFormat)
Definition: swevent.hxx:87
virtual bool SetFormatAttr(const SfxPoolItem &rAttr)
Definition: format.cxx:458
sal_uInt16 DecGrfsThatResize()
Definition: swtable.hxx:179
virtual const SwViewShell * GetCurrentViewShell() const =0
Returns the layout set at the document.
static SotClipboardFormatId GetFormatIdFromMimeType(const OUString &rMimeType)
SwHTMLTableLayout * GetHTMLTableLayout()
Definition: swtable.hxx:174
void SetSize(const Size &rSize)
const SwFormatFrameSize & GetFrameSize(bool=true) const
Definition: fmtfsize.hxx:104
SwTableNode * FindTableNode()
Search table node, in which it is.
Definition: node.cxx:352
sal_uInt16 CalcLineSpace(SvxBoxItemLine nLine, bool bEvenIfNoLine=false) const
virtual sfx2::LinkManager & GetLinkManager()=0
constexpr TypedWhichId< SwMsgPoolItem > RES_GRAPHIC_ARRIVED(174)
bool IsInDtor() const
Definition: doc.hxx:398
SwGrfNode * GetGrfNode()
Definition: ndgrf.hxx:153
static SotClipboardFormatId RegisterStatusInfoId()
void setWidth(long nWidth)
SwFrameFormat * GetFlyFormat() const
If node is in a fly return the respective format.
Definition: node.cxx:714
#define HTMLTABLE_RESIZE_NOW
Definition: htmltbl.hxx:36
const Size & GetSize() const
Size GetGraphicSizeTwip(const Graphic &rGraphic, vcl::RenderContext *pOutDev)
Definition: swtypes.cxx:28
Base class of the Writer document model elements.
Definition: node.hxx:79
void setHeight(long nHeight)
typedef void(CALLTYPE *GetFuncDataPtr)(sal_uInt16 &nNo