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