summaryrefslogtreecommitdiffstats
path: root/svx/source/sdr/misc/ImageMapInfo.cxx
blob: e396dc95842bbd29b11372a0754de2d4be03e14b (plain)
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
/* -*- Mode: C++; tab-width: 4; indent-tabs-mode: nil; c-basic-offset: 4 -*- */
/*
 * This file is part of the LibreOffice project.
 *
 * This Source Code Form is subject to the terms of the Mozilla Public
 * License, v. 2.0. If a copy of the MPL was not distributed with this
 * file, You can obtain one at http://mozilla.org/MPL/2.0/.
 *
 * This file incorporates work covered by the following license notice:
 *
 *   Licensed to the Apache Software Foundation (ASF) under one or more
 *   contributor license agreements. See the NOTICE file distributed
 *   with this work for additional information regarding copyright
 *   ownership. The ASF licenses this file to you under the Apache
 *   License, Version 2.0 (the "License"); you may not use this file
 *   except in compliance with the License. You may obtain a copy of
 *   the License at http://www.apache.org/licenses/LICENSE-2.0 .
 */

#include <svx/ImageMapInfo.hxx>

#include <svx/svdobj.hxx>
#include <svx/svdograf.hxx>
#include <svx/svdoole2.hxx>
#include <vcl/imapobj.hxx>
#include <vcl/svapp.hxx>
#include <vcl/window.hxx>

SvxIMapInfo* SvxIMapInfo::GetIMapInfo(SdrObject const* pObject)
{
    assert(pObject);

    SvxIMapInfo* pIMapInfo = nullptr;
    sal_uInt16 nCount = pObject->GetUserDataCount();

    // Can we find IMap information within the user data?
    for (sal_uInt16 i = 0; i < nCount; i++)
    {
        SdrObjUserData* pUserData = pObject->GetUserData(i);

        if ((pUserData->GetInventor() == SdrInventor::StarDrawUserData)
            && (pUserData->GetId() == SVX_IMAPINFO_ID))
            pIMapInfo = static_cast<SvxIMapInfo*>(pUserData);
    }

    return pIMapInfo;
}

IMapObject* SvxIMapInfo::GetHitIMapObject(const SdrObject* pObj, const Point& rWinPoint,
                                          const vcl::Window* rCmpWnd)
{
    SvxIMapInfo* pIMapInfo = GetIMapInfo(pObj);
    IMapObject* pIMapObj = nullptr;

    if (pIMapInfo)
    {
        const MapMode aMap100(MapUnit::Map100thMM);
        Size aGraphSize;
        Point aRelPoint(rWinPoint);
        ImageMap& rImageMap = const_cast<ImageMap&>(pIMapInfo->GetImageMap());
        tools::Rectangle& rRect = const_cast<tools::Rectangle&>(pObj->GetLogicRect());

        if (rCmpWnd)
        {
            MapMode aWndMode = rCmpWnd->GetMapMode();
            aRelPoint = rCmpWnd->LogicToLogic(rWinPoint, &aWndMode, &aMap100);
            rRect = rCmpWnd->LogicToLogic(pObj->GetLogicRect(), &aWndMode, &aMap100);
        }

        bool bObjSupported = false;

        // execute HitTest
        if (auto pGrafObj = dynamic_cast<const SdrGrafObj*>(pObj)) // simple graphics object
        {
            const GeoStat& rGeo = pGrafObj->GetGeoStat();
            std::unique_ptr<SdrGrafObjGeoData> pGeoData(
                static_cast<SdrGrafObjGeoData*>(pGrafObj->GetGeoData()));

            // Undo rotation
            if (rGeo.nRotationAngle)
                RotatePoint(aRelPoint, rRect.TopLeft(), -rGeo.nSin, rGeo.nCos);

            // Undo mirroring
            if (pGeoData->bMirrored)
                aRelPoint.setX(rRect.Right() + rRect.Left() - aRelPoint.X());

            // Undo shearing
            if (rGeo.nShearAngle)
                ShearPoint(aRelPoint, rRect.TopLeft(), -rGeo.nTan);

            if (pGrafObj->GetGrafPrefMapMode().GetMapUnit() == MapUnit::MapPixel)
                aGraphSize = Application::GetDefaultDevice()->PixelToLogic(
                    pGrafObj->GetGrafPrefSize(), aMap100);
            else
                aGraphSize = OutputDevice::LogicToLogic(pGrafObj->GetGrafPrefSize(),
                                                        pGrafObj->GetGrafPrefMapMode(), aMap100);

            bObjSupported = true;
        }
        else if (auto pOleObj = dynamic_cast<const SdrOle2Obj*>(pObj)) // OLE object
        {
            aGraphSize = pOleObj->GetOrigObjSize();
            bObjSupported = true;
        }

        // Everything worked out well, thus execute HitTest
        if (bObjSupported)
        {
            // Calculate relative position of mouse cursor
            aRelPoint -= rRect.TopLeft();
            pIMapObj = rImageMap.GetHitIMapObject(aGraphSize, rRect.GetSize(), aRelPoint);

            // We don't care about deactivated objects
            if (pIMapObj && !pIMapObj->IsActive())
                pIMapObj = nullptr;
        }
    }

    return pIMapObj;
}