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  if (lcl_AreRectanglesApproxEqual(pAnchor->getShapeRect(), pObj->GetSnapRect()))
180  return;
181 
182  if (pAnchor->maStart.Tab() != nTab)
183  // The object is not anchored on the current sheet. Skip it.
184  // TODO: In the future, we may want to adjust objects that are
185  // anchored on all selected sheets.
186  return;
187 
188  ScDrawLayer::SetCellAnchoredFromPosition(*pObj, rDoc, pAnchor->maStart.Tab(), pAnchor->mbResizeWithCell);
189 }
190 
191 }
192 
193 void ScDrawView::Notify( SfxBroadcaster& rBC, const SfxHint& rHint )
194 {
195  if (rHint.GetId() == SfxHintId::ThisIsAnSdrHint)
196  {
197  const SdrHint* pSdrHint = static_cast<const SdrHint*>( &rHint );
198  adjustAnchoredPosition(*pSdrHint, rDoc, nTab);
199  FmFormView::Notify( rBC,rHint );
200  }
201  else if (auto pDeletedHint = dynamic_cast<const ScTabDeletedHint*>(&rHint)) // Sheet has been deleted
202  {
203  SCTAB nDelTab = pDeletedHint->GetTab();
204  if (ValidTab(nDelTab))
205  {
206  // used to be: HidePagePgNum(nDelTab) - hide only if the deleted sheet is shown here
207  if ( nDelTab == nTab )
208  HideSdrPage();
209  }
210  }
211  else if (auto pChangedHint = dynamic_cast<const ScTabSizeChangedHint*>(&rHint)) // Size has been changed
212  {
213  if ( nTab == pChangedHint->GetTab() )
214  UpdateWorkArea();
215  }
216  else
217  FmFormView::Notify( rBC,rHint );
218 }
219 
221 {
222  if ( !(pViewData &&
224  pObj && ( dynamic_cast<const SdrGrafObj*>( pObj) != nullptr || dynamic_cast<const SdrOle2Obj*>( pObj) != nullptr )) )
225  return;
226 
227  Graphic aGraphic;
228  TargetList aTargetList;
229  SvxIMapInfo* pIMapInfo = SvxIMapInfo::GetIMapInfo( pObj );
230  const ImageMap* pImageMap = nullptr;
231  if ( pIMapInfo )
232  pImageMap = &pIMapInfo->GetImageMap();
233 
234  // handle target list
235  SfxViewFrame::GetTargetList( aTargetList );
236 
237  // handle graphics from object
238  if ( auto pGrafObj = dynamic_cast<SdrGrafObj*>( pObj) )
239  aGraphic = pGrafObj->GetGraphic();
240  else
241  {
242  const Graphic* pGraphic = static_cast<const SdrOle2Obj*>(pObj)->GetGraphic();
243  if ( pGraphic )
244  aGraphic = *pGraphic;
245  }
246 
247  ScIMapDlgSet( aGraphic, pImageMap, &aTargetList, pObj ); // from imapwrap
248 }
249 
250 /* vim:set shiftwidth=4 softtabstop=4 expandtab: */
SfxViewFrame * GetViewFrame() const
ScDocShell * GetDocShell() const
Definition: viewdata.hxx:354
SdrHintKind GetKind() const
size_t GetMarkCount() const
void UpdateIMap(SdrObject *pObj)
Definition: drawvie3.cxx:220
static void SetPageAnchored(SdrObject &)
Definition: drwlayer.cxx:2319
bool AreObjectsMarked() const
static void GetTargetList(TargetList &rList)
static ScDrawObjData * GetObjData(SdrObject *pObj, bool bCreate=false)
Definition: drwlayer.cxx:2481
void BegUndo()
SdrMark * GetMark(size_t nNum) const
virtual void AddCustomHdl() override
Definition: drawview.cxx:136
ScViewData * pViewData
Definition: drawview.hxx:36
ScAddress maStart
Definition: userdat.hxx:37
SfxHintId GetId() const
virtual const tools::Rectangle & GetSnapRect() const
virtual void Notify(SfxBroadcaster &rBC, const SfxHint &rHint) override
Definition: drawvie3.cxx:193
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:271
void EndUndo()
void UpdateWorkArea()
Definition: drawview.cxx:261
ScTabViewShell * GetViewShell() const
Definition: viewdata.hxx:357
SdrObject * GetMarkedSdrObj() const
bool mbResizeWithCell
Definition: userdat.hxx:42
const tools::Rectangle & getShapeRect() const
Definition: userdat.hxx:47
ScAnchorType
Definition: global.hxx:384
void AddUndo(std::unique_ptr< SdrUndoAction > pUndo)
void Construct()
Definition: drawview.cxx:69
void SetBufferedOverlayAllowed(bool bNew)
int i
void SetCellAnchored(bool bResizeWithCell)
Definition: drawvie3.cxx:87
static ScAnchorType GetAnchorType(const SdrObject &)
Definition: drwlayer.cxx:2325
static void SetCellAnchoredFromPosition(SdrObject &rObj, const ScDocument &rDoc, SCTAB nTab, bool bResizeWithCell)
Definition: drwlayer.cxx:2191
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
ScDocument & rDoc
needed ?
Definition: drawview.hxx:38
const ImageMap & GetImageMap() const
virtual void HideSdrPage() override
SdrHdlList maHdlList
void SetDrawModified()
SetDrawModified - without Formula update.
Definition: docsh.cxx:2884
::std::vector< OUString > TargetList
bool ValidTab(SCTAB nTab)
Definition: address.hxx:105
tools::Long Right() const
sal_Int16 SCTAB
Definition: types.hxx:23
SCTAB nTab
Definition: drawview.hxx:39
static SvxIMapInfo * GetIMapInfo(const SdrObject *pObject)
const SdrObject * GetObject() const
sal_uInt16 ScIMapChildWindowId()
Definition: imapwrap.cxx:24