LibreOffice Module svx (master)  1
ImageMapInfo.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 <svx/ImageMapInfo.hxx>
21 
22 #include <svx/svdobj.hxx>
23 #include <svx/svdograf.hxx>
24 #include <svx/svdoole2.hxx>
25 #include <svx/unoshape.hxx>
26 #include <vcl/imapobj.hxx>
27 #include <vcl/svapp.hxx>
28 #include <vcl/window.hxx>
29 
31 {
32  assert(pObject);
33 
34  SvxIMapInfo* pIMapInfo = nullptr;
35  sal_uInt16 nCount = pObject->GetUserDataCount();
36 
37  // Can we find IMap information within the user data?
38  for (sal_uInt16 i = 0; i < nCount; i++)
39  {
40  SdrObjUserData* pUserData = pObject->GetUserData(i);
41 
42  if ((pUserData->GetInventor() == SdrInventor::StarDrawUserData)
43  && (pUserData->GetId() == SVX_IMAPINFO_ID))
44  pIMapInfo = static_cast<SvxIMapInfo*>(pUserData);
45  }
46 
47  return pIMapInfo;
48 }
49 
51  const vcl::Window* rCmpWnd)
52 {
53  SvxIMapInfo* pIMapInfo = GetIMapInfo(pObj);
54  IMapObject* pIMapObj = nullptr;
55 
56  if (pIMapInfo)
57  {
58  const MapMode aMap100(MapUnit::Map100thMM);
59  Size aGraphSize;
60  Point aRelPoint(rWinPoint);
61  ImageMap& rImageMap = const_cast<ImageMap&>(pIMapInfo->GetImageMap());
62  tools::Rectangle& rRect = const_cast<tools::Rectangle&>(pObj->GetLogicRect());
63 
64  if (rCmpWnd)
65  {
66  MapMode aWndMode = rCmpWnd->GetMapMode();
67  aRelPoint = rCmpWnd->LogicToLogic(rWinPoint, &aWndMode, &aMap100);
68  rRect = rCmpWnd->LogicToLogic(pObj->GetLogicRect(), &aWndMode, &aMap100);
69  }
70 
71  bool bObjSupported = false;
72 
73  // execute HitTest
74  if (auto pGrafObj = dynamic_cast<const SdrGrafObj*>(pObj)) // simple graphics object
75  {
76  const GeoStat& rGeo = pGrafObj->GetGeoStat();
77  std::unique_ptr<SdrGrafObjGeoData> pGeoData(
78  static_cast<SdrGrafObjGeoData*>(pGrafObj->GetGeoData()));
79 
80  // Undo rotation
81  if (rGeo.nRotationAngle)
82  RotatePoint(aRelPoint, rRect.TopLeft(), -rGeo.nSin, rGeo.nCos);
83 
84  // Undo mirroring
85  if (pGeoData->bMirrored)
86  aRelPoint.setX(rRect.Right() + rRect.Left() - aRelPoint.X());
87 
88  // Undo shearing
89  if (rGeo.nShearAngle)
90  ShearPoint(aRelPoint, rRect.TopLeft(), -rGeo.nTan);
91 
92  if (pGrafObj->GetGrafPrefMapMode().GetMapUnit() == MapUnit::MapPixel)
94  pGrafObj->GetGrafPrefSize(), aMap100);
95  else
96  aGraphSize = OutputDevice::LogicToLogic(pGrafObj->GetGrafPrefSize(),
97  pGrafObj->GetGrafPrefMapMode(), aMap100);
98 
99  bObjSupported = true;
100  }
101  else if (auto pOleObj = dynamic_cast<const SdrOle2Obj*>(pObj)) // OLE object
102  {
103  aGraphSize = pOleObj->GetOrigObjSize();
104  bObjSupported = true;
105  }
106 
107  // Everything worked out well, thus execute HitTest
108  if (bObjSupported)
109  {
110  // Calculate relative position of mouse cursor
111  aRelPoint -= rRect.TopLeft();
112  pIMapObj = rImageMap.GetHitIMapObject(aGraphSize, rRect.GetSize(), aRelPoint);
113 
114  // We don't care about deactivated objects
115  if (pIMapObj && !pIMapObj->IsActive())
116  pIMapObj = nullptr;
117  }
118  }
119 
120  return pIMapObj;
121 }
bool IsActive() const
Point TopLeft() const
User data of a drawing object, e.g.
Definition: svdobj.hxx:204
void ShearPoint(Point &rPnt, const Point &rRef, double tn, bool bVShear=false)
Definition: svdtrans.hxx:122
Point LogicToLogic(const Point &rPtSource, const MapMode *pMapModeSource, const MapMode *pMapModeDest) const
double nSin
Definition: svdtrans.hxx:219
long nRotationAngle
Definition: svdtrans.hxx:216
const MapMode & GetMapMode() const
static OutputDevice * GetDefaultDevice()
long Right() const
void setX(long nX)
The transformation of a rectangle into a polygon, by using angle parameters from GeoStat.
Definition: svdtrans.hxx:214
int nCount
double nCos
Definition: svdtrans.hxx:220
int i
double nTan
Definition: svdtrans.hxx:218
Abstract DrawObject.
Definition: svdobj.hxx:312
IMapObject * GetHitIMapObject(const Size &rOriginalSize, const Size &rDisplaySize, const Point &rRelHitPoint, sal_uLong nFlags=0)
long X() const
Size GetSize() const
long nShearAngle
Definition: svdtrans.hxx:217
Point PixelToLogic(const Point &rDevicePt) const
void RotatePoint(Point &rPnt, const Point &rRef, double sn, double cs)
Definition: svdtrans.hxx:114
static IMapObject * GetHitIMapObject(const SdrObject *pObj, const Point &rWinPoint, const vcl::Window *rCmpWnd=nullptr)
sal_uInt16 GetId() const
Definition: svdobj.hxx:220
SdrObjUserData * GetUserData(sal_uInt16 nNum) const
Definition: svdobj.cxx:2636
sal_uInt16 GetUserDataCount() const
Definition: svdobj.cxx:2630
long Left() const
const ImageMap & GetImageMap() const
#define SVX_IMAPINFO_ID
virtual const tools::Rectangle & GetLogicRect() const
Definition: svdobj.cxx:1634
SdrInventor GetInventor() const
Definition: svdobj.hxx:219
static SvxIMapInfo * GetIMapInfo(const SdrObject *pObject)