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