LibreOffice Module sc (master)  1
drawvie3.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 <sal/config.h>
21 
22 #include <cstdlib>
23 
24 #include <svx/svdograf.hxx>
25 #include <svx/svdoole2.hxx>
26 #include <svx/ImageMapInfo.hxx>
27 #include <sfx2/viewfrm.hxx>
28 
29 #include <strings.hrc>
30 #include <scresid.hxx>
31 #include <drawview.hxx>
32 #include <drwlayer.hxx>
33 #include "imapwrap.hxx"
34 #include <viewdata.hxx>
35 #include <document.hxx>
36 #include <userdat.hxx>
37 #include <tabvwsh.hxx>
38 #include <docsh.hxx>
39 
41  OutputDevice* pOut,
42  ScViewData* pData )
43 : FmFormView(*pData->GetDocument().GetDrawLayer(), pOut),
44  pViewData( pData ),
45  pDev( pOut ),
46  rDoc( pData->GetDocument() ),
47  nTab( pData->GetTabNo() ),
48  pDropMarkObj( nullptr ),
49  bInConstruct( true )
50 {
51  // #i73602# Use default from the configuration
52  SetBufferedOverlayAllowed(getOptionsDrawinglayer().IsOverlayBuffer_Calc());
53 
54  // #i74769#, #i75172# Use default from the configuration
55  SetBufferedOutputAllowed(getOptionsDrawinglayer().IsPaintBuffer_Calc());
56 
57  Construct();
58 }
59 
60 // set anchor
61 
63 {
64  if( !AreObjectsMarked() )
65  return;
66 
67  const SdrMarkList* pMark = &GetMarkedObjectList();
68  const size_t nCount = pMark->GetMarkCount();
69 
70  BegUndo(ScResId(SCSTR_UNDO_PAGE_ANCHOR));
71  for( size_t i=0; i<nCount; ++i )
72  {
73  SdrObject* pObj = pMark->GetMark(i)->GetMarkedSdrObj();
74  AddUndo (std::make_unique<ScUndoAnchorData>( pObj, &rDoc, nTab ));
76  }
77  EndUndo();
78 
79  if ( pViewData )
81 
82  // Remove the anchor object.
83  maHdlList.RemoveAllByKind(SdrHdlKind::Anchor);
84  maHdlList.RemoveAllByKind(SdrHdlKind::Anchor_TR);
85 }
86 
87 void ScDrawView::SetCellAnchored(bool bResizeWithCell)
88 {
89  if( !AreObjectsMarked() )
90  return;
91 
92  const SdrMarkList* pMark = &GetMarkedObjectList();
93  const size_t nCount = pMark->GetMarkCount();
94 
95  BegUndo(ScResId(SCSTR_UNDO_CELL_ANCHOR));
96  for( size_t i=0; i<nCount; ++i )
97  {
98  SdrObject* pObj = pMark->GetMark(i)->GetMarkedSdrObj();
99  AddUndo (std::make_unique<ScUndoAnchorData>( pObj, &rDoc, nTab ));
100  ScDrawLayer::SetCellAnchoredFromPosition(*pObj, rDoc, nTab, bResizeWithCell);
101  }
102  EndUndo();
103 
104  if ( pViewData )
105  {
107 
108  // Set the anchor object.
109  AddCustomHdl();
110  }
111 }
112 
114 {
115  bool bPage = false;
116  bool bCell = false;
117  bool bCellResize = false;
118  if( AreObjectsMarked() )
119  {
120  const SdrMarkList* pMark = &GetMarkedObjectList();
121  const size_t nCount = pMark->GetMarkCount();
122  for( size_t i=0; i<nCount; ++i )
123  {
124  const SdrObject* pObj = pMark->GetMark(i)->GetMarkedSdrObj();
125  const ScAnchorType aAnchorType = ScDrawLayer::GetAnchorType( *pObj );
126  if( aAnchorType == SCA_CELL )
127  bCell =true;
128  else if (aAnchorType == SCA_CELL_RESIZE)
129  bCellResize = true;
130  else
131  bPage = true;
132  }
133  }
134  if( bPage && !bCell && !bCellResize )
135  return SCA_PAGE;
136  if( !bPage && bCell && !bCellResize )
137  return SCA_CELL;
138  if( !bPage && !bCell && bCellResize )
139  return SCA_CELL_RESIZE;
140  return SCA_DONTKNOW;
141 }
142 
143 namespace {
144 
145 bool lcl_AreRectanglesApproxEqual(const tools::Rectangle& rRectA, const tools::Rectangle& rRectB)
146 {
147  // Twips <-> Hmm conversions introduce +-1 differences although the rectangles should actually
148  // be equal. Therefore test with == is not appropriate in some cases.
149  if (std::abs(rRectA.Left() - rRectB.Left()) > 1)
150  return false;
151  if (std::abs(rRectA.Top() - rRectB.Top()) > 1)
152  return false;
153  if (std::abs(rRectA.Right() - rRectB.Right()) > 1)
154  return false;
155  if (std::abs(rRectA.Bottom() - rRectB.Bottom()) > 1)
156  return false;
157  return true;
158 }
159 
164 void adjustAnchoredPosition(const SdrHint& rHint, const ScDocument& rDoc, SCTAB nTab)
165 {
166  if (rHint.GetKind() != SdrHintKind::ObjectChange && rHint.GetKind() != SdrHintKind::ObjectInserted)
167  return;
168 
169  SdrObject* pObj = const_cast<SdrObject*>(rHint.GetObject());
170  if (!pObj)
171  return;
172 
173  ScDrawObjData *pAnchor = ScDrawLayer::GetObjData(pObj);
174  if (!pAnchor)
175  return;
176 
177  if (pAnchor->meType == ScDrawObjData::CellNote)
178  return;
179 
180  // SetCellAnchoredFromPosition has to be called only if shape geometry has been changed, and not
181  // if only shape visibility has been changed. It is not enough to test shape rect, because e.g. a
182  // 180deg rotation changes only the logic rect (tdf#139583).
183  ScDrawObjData& rNoRotatedAnchor = *ScDrawLayer::GetNonRotatedObjData(pObj, true /*bCreate*/);
184  if (lcl_AreRectanglesApproxEqual(pAnchor->getShapeRect(), pObj->GetSnapRect())
185  && lcl_AreRectanglesApproxEqual(rNoRotatedAnchor.getShapeRect(), pObj->GetLogicRect()))
186  return;
187 
188  if (pAnchor->maStart.Tab() != nTab)
189  // The object is not anchored on the current sheet. Skip it.
190  // TODO: In the future, we may want to adjust objects that are
191  // anchored on all selected sheets.
192  return;
193 
194  ScDrawLayer::SetCellAnchoredFromPosition(*pObj, rDoc, pAnchor->maStart.Tab(), pAnchor->mbResizeWithCell);
195 }
196 
197 }
198 
199 void ScDrawView::Notify( SfxBroadcaster& rBC, const SfxHint& rHint )
200 {
201  if (rHint.GetId() == SfxHintId::ThisIsAnSdrHint)
202  {
203  const SdrHint* pSdrHint = static_cast<const SdrHint*>( &rHint );
204  adjustAnchoredPosition(*pSdrHint, rDoc, nTab);
205  FmFormView::Notify( rBC,rHint );
206  }
207  else if (auto pDeletedHint = dynamic_cast<const ScTabDeletedHint*>(&rHint)) // Sheet has been deleted
208  {
209  SCTAB nDelTab = pDeletedHint->GetTab();
210  if (ValidTab(nDelTab))
211  {
212  // used to be: HidePagePgNum(nDelTab) - hide only if the deleted sheet is shown here
213  if ( nDelTab == nTab )
214  HideSdrPage();
215  }
216  }
217  else if (auto pChangedHint = dynamic_cast<const ScTabSizeChangedHint*>(&rHint)) // Size has been changed
218  {
219  if ( nTab == pChangedHint->GetTab() )
220  UpdateWorkArea();
221  }
222  else
223  FmFormView::Notify( rBC,rHint );
224 }
225 
227 {
228  if ( !(pViewData &&
230  pObj && ( dynamic_cast<const SdrGrafObj*>( pObj) != nullptr || dynamic_cast<const SdrOle2Obj*>( pObj) != nullptr )) )
231  return;
232 
233  Graphic aGraphic;
234  TargetList aTargetList;
235  SvxIMapInfo* pIMapInfo = SvxIMapInfo::GetIMapInfo( pObj );
236  const ImageMap* pImageMap = nullptr;
237  if ( pIMapInfo )
238  pImageMap = &pIMapInfo->GetImageMap();
239 
240  // handle target list
241  SfxViewFrame::GetTargetList( aTargetList );
242 
243  // handle graphics from object
244  if ( auto pGrafObj = dynamic_cast<SdrGrafObj*>( pObj) )
245  aGraphic = pGrafObj->GetGraphic();
246  else
247  {
248  const Graphic* pGraphic = static_cast<const SdrOle2Obj*>(pObj)->GetGraphic();
249  if ( pGraphic )
250  aGraphic = *pGraphic;
251  }
252 
253  ScIMapDlgSet( aGraphic, pImageMap, &aTargetList, pObj ); // from imapwrap
254 }
255 
256 /* vim:set shiftwidth=4 softtabstop=4 expandtab: */
SfxViewFrame * GetViewFrame() const
ScDocShell * GetDocShell() const
Definition: viewdata.hxx:353
SdrHintKind GetKind() const
size_t GetMarkCount() const
void UpdateIMap(SdrObject *pObj)
Definition: drawvie3.cxx:226
static void SetPageAnchored(SdrObject &)
Definition: drwlayer.cxx:2429
bool AreObjectsMarked() const
static void GetTargetList(TargetList &rList)
static ScDrawObjData * GetObjData(SdrObject *pObj, bool bCreate=false)
Definition: drwlayer.cxx:2591
void BegUndo()
SdrMark * GetMark(size_t nNum) const
virtual void AddCustomHdl() override
Definition: drawview.cxx:138
ScViewData * pViewData
Definition: drawview.hxx:35
ScAddress maStart
Definition: userdat.hxx:36
SfxHintId GetId() const
virtual const tools::Rectangle & GetSnapRect() const
virtual void Notify(SfxBroadcaster &rBC, const SfxHint &rHint) override
Definition: drawvie3.cxx:199
void SetBufferedOutputAllowed(bool bNew)
virtual void Notify(SfxBroadcaster &rBC, const SfxHint &rHint) override
tools::Long Left() const
int nCount
tools::Long Bottom() const
SCTAB Tab() const
Definition: address.hxx:270
void EndUndo()
void UpdateWorkArea()
Definition: drawview.cxx:263
ScTabViewShell * GetViewShell() const
Definition: viewdata.hxx:356
SdrObject * GetMarkedSdrObj() const
bool mbResizeWithCell
Definition: userdat.hxx:41
const tools::Rectangle & getShapeRect() const
Definition: userdat.hxx:46
ScAnchorType
Definition: global.hxx:382
void AddUndo(std::unique_ptr< SdrUndoAction > pUndo)
void Construct()
Definition: drawview.cxx:71
void SetBufferedOverlayAllowed(bool bNew)
int i
void SetCellAnchored(bool bResizeWithCell)
Definition: drawvie3.cxx:87
static ScAnchorType GetAnchorType(const SdrObject &)
Definition: drwlayer.cxx:2435
static void SetCellAnchoredFromPosition(SdrObject &rObj, const ScDocument &rDoc, SCTAB nTab, bool bResizeWithCell)
Definition: drwlayer.cxx:2286
const SdrMarkList & GetMarkedObjectList() const
void RemoveAllByKind(SdrHdlKind eKind)
void ScIMapDlgSet(const Graphic &rGraphic, const ImageMap *pImageMap, const TargetList *pTargetList, void *pEditingObj)
Definition: imapwrap.cxx:29
ScDrawView(OutputDevice *pOut, ScViewData *pData)
Definition: drawvie3.cxx:40
OUString ScResId(const char *pId)
Definition: scdll.cxx:89
void SetPageAnchored()
Definition: drawvie3.cxx:62
tools::Long Top() const
const SvtOptionsDrawinglayer & getOptionsDrawinglayer() const
bool HasChildWindow(sal_uInt16)
ScAnchorType GetAnchorType() const
Definition: drawvie3.cxx:113
static ScDrawObjData * GetNonRotatedObjData(SdrObject *pObj, bool bCreate=false)
Definition: drwlayer.cxx:2572
ScDocument & rDoc
needed ?
Definition: drawview.hxx:37
const ImageMap & GetImageMap() const
virtual void HideSdrPage() override
SdrHdlList maHdlList
void SetDrawModified()
SetDrawModified - without Formula update.
Definition: docsh.cxx:2884
virtual const tools::Rectangle & GetLogicRect() const
::std::vector< OUString > TargetList
bool ValidTab(SCTAB nTab)
Definition: address.hxx:104
tools::Long Right() const
sal_Int16 SCTAB
Definition: types.hxx:22
SCTAB nTab
Definition: drawview.hxx:38
static SvxIMapInfo * GetIMapInfo(const SdrObject *pObject)
const SdrObject * GetObject() const
sal_uInt16 ScIMapChildWindowId()
Definition: imapwrap.cxx:24