1 /* -*- Mode: C++; tab-width: 4; indent-tabs-mode: nil; c-basic-offset: 4 -*- */
3 * This file is part of the LibreOffice project.
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/.
9 * This file incorporates work covered by the following license notice:
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 .
20 #include <sal/config.h>
22 #include <toolkit/helper/vclunohelper.hxx>
23 #include <svl/itemprop.hxx>
24 #include <svl/hint.hxx>
26 #include <vcl/svapp.hxx>
27 #include <osl/diagnose.h>
28 #include <com/sun/star/awt/XBitmap.hpp>
29 #include <com/sun/star/beans/PropertyAttribute.hpp>
31 #include <targuno.hxx>
32 #include <miscuno.hxx>
34 #include <datauno.hxx>
35 #include <nameuno.hxx>
37 #include <content.hxx>
38 #include <scresid.hxx>
39 #include <strings.hrc>
40 #include <bitmaps.hlst>
41 #include <unonames.hxx>
43 using namespace ::com::sun::star
;
45 const TranslateId aTypeResIds
[SC_LINKTARGETTYPE_COUNT
] =
47 SCSTR_CONTENT_TABLE
, // SC_LINKTARGETTYPE_SHEET
48 SCSTR_CONTENT_RANGENAME
, // SC_LINKTARGETTYPE_RANGENAME
49 SCSTR_CONTENT_DBAREA
// SC_LINKTARGETTYPE_DBAREA
52 static std::span
<const SfxItemPropertyMapEntry
> lcl_GetLinkTargetMap()
54 static const SfxItemPropertyMapEntry aLinkTargetMap_Impl
[] =
56 { SC_UNO_LINKDISPBIT
, 0, cppu::UnoType
<awt::XBitmap
>::get(), beans::PropertyAttribute::READONLY
, 0 },
57 { SC_UNO_LINKDISPNAME
, 0, cppu::UnoType
<OUString
>::get(), beans::PropertyAttribute::READONLY
, 0 },
59 return aLinkTargetMap_Impl
;
62 // service for ScLinkTargetTypeObj is not defined
63 // must not support document::LinkTarget because the target type cannot be used as a target
65 SC_SIMPLE_SERVICE_INFO( ScLinkTargetTypesObj
, u
"ScLinkTargetTypesObj"_ustr
, u
"com.sun.star.document.LinkTargets"_ustr
)
66 SC_SIMPLE_SERVICE_INFO( ScLinkTargetTypeObj
, u
"ScLinkTargetTypeObj"_ustr
, u
"com.sun.star.document.LinkTargetSupplier"_ustr
)
67 SC_SIMPLE_SERVICE_INFO( ScLinkTargetsObj
, u
"ScLinkTargetsObj"_ustr
, u
"com.sun.star.document.LinkTargets"_ustr
)
69 ScLinkTargetTypesObj::ScLinkTargetTypesObj(ScDocShell
* pDocSh
) :
72 pDocShell
->GetDocument().AddUnoObject(*this);
74 for (sal_uInt16 i
=0; i
<SC_LINKTARGETTYPE_COUNT
; i
++)
75 aNames
[i
] = ScResId(aTypeResIds
[i
]);
78 ScLinkTargetTypesObj::~ScLinkTargetTypesObj()
83 pDocShell
->GetDocument().RemoveUnoObject(*this);
86 void ScLinkTargetTypesObj::Notify( SfxBroadcaster
&, const SfxHint
& rHint
)
88 if ( rHint
.GetId() == SfxHintId::Dying
)
89 pDocShell
= nullptr; // document gone
92 // container::XNameAccess
94 uno::Any SAL_CALL
ScLinkTargetTypesObj::getByName(const OUString
& aName
)
98 for (sal_uInt16 i
=0; i
<SC_LINKTARGETTYPE_COUNT
; i
++)
99 if ( aNames
[i
] == aName
)
100 return uno::Any(uno::Reference
< beans::XPropertySet
>(new ScLinkTargetTypeObj( pDocShell
, i
)));
103 throw container::NoSuchElementException();
106 uno::Sequence
<OUString
> SAL_CALL
ScLinkTargetTypesObj::getElementNames()
108 uno::Sequence
<OUString
> aRet(SC_LINKTARGETTYPE_COUNT
);
109 OUString
* pArray
= aRet
.getArray();
110 for (sal_uInt16 i
=0; i
<SC_LINKTARGETTYPE_COUNT
; i
++)
111 pArray
[i
] = aNames
[i
];
115 sal_Bool SAL_CALL
ScLinkTargetTypesObj::hasByName(const OUString
& aName
)
117 for (const auto & i
: aNames
)
123 // container::XElementAccess
125 uno::Type SAL_CALL
ScLinkTargetTypesObj::getElementType()
127 return cppu::UnoType
<beans::XPropertySet
>::get();
130 sal_Bool SAL_CALL
ScLinkTargetTypesObj::hasElements()
135 ScLinkTargetTypeObj::ScLinkTargetTypeObj(ScDocShell
* pDocSh
, sal_uInt16 nT
) :
139 pDocShell
->GetDocument().AddUnoObject(*this);
140 aName
= ScResId(aTypeResIds
[nType
]); //! on demand?
143 ScLinkTargetTypeObj::~ScLinkTargetTypeObj()
148 pDocShell
->GetDocument().RemoveUnoObject(*this);
151 void ScLinkTargetTypeObj::Notify( SfxBroadcaster
&, const SfxHint
& rHint
)
153 if ( rHint
.GetId() == SfxHintId::Dying
)
154 pDocShell
= nullptr; // document gone
157 // document::XLinkTargetSupplier
159 uno::Reference
< container::XNameAccess
> SAL_CALL
ScLinkTargetTypeObj::getLinks()
161 uno::Reference
< container::XNameAccess
> xCollection
;
167 case SC_LINKTARGETTYPE_SHEET
:
168 xCollection
.set(new ScTableSheetsObj(pDocShell
));
170 case SC_LINKTARGETTYPE_RANGENAME
:
171 xCollection
.set(new ScGlobalNamedRangesObj(pDocShell
));
173 case SC_LINKTARGETTYPE_DBAREA
:
174 xCollection
.set(new ScDatabaseRangesObj(pDocShell
));
177 OSL_FAIL("invalid type");
181 // wrap collection in ScLinkTargetsObj because service document::LinkTargets requires
182 // beans::XPropertySet as ElementType in container::XNameAccess.
183 if ( xCollection
.is() )
184 return new ScLinkTargetsObj( xCollection
);
188 // beans::XPropertySet
190 uno::Reference
< beans::XPropertySetInfo
> SAL_CALL
ScLinkTargetTypeObj::getPropertySetInfo()
192 static uno::Reference
< beans::XPropertySetInfo
> aRef(new SfxItemPropertySetInfo( lcl_GetLinkTargetMap() ));
196 void SAL_CALL
ScLinkTargetTypeObj::setPropertyValue(const OUString
& /* aPropertyName */,
197 const uno::Any
& /* aValue */)
199 // everything is read-only
203 constexpr OUString aContentBmps
[]=
205 RID_BMP_CONTENT_TABLE
,
206 RID_BMP_CONTENT_RANGENAME
,
207 RID_BMP_CONTENT_DBAREA
,
208 RID_BMP_CONTENT_GRAPHIC
,
209 RID_BMP_CONTENT_OLEOBJECT
,
210 RID_BMP_CONTENT_NOTE
,
211 RID_BMP_CONTENT_AREALINK
,
212 RID_BMP_CONTENT_DRAWING
215 void ScLinkTargetTypeObj::SetLinkTargetBitmap( uno::Any
& rRet
, sal_uInt16 nType
)
217 ScContentId nImgId
= ScContentId::ROOT
;
220 case SC_LINKTARGETTYPE_SHEET
:
221 nImgId
= ScContentId::TABLE
;
223 case SC_LINKTARGETTYPE_RANGENAME
:
224 nImgId
= ScContentId::RANGENAME
;
226 case SC_LINKTARGETTYPE_DBAREA
:
227 nImgId
= ScContentId::DBAREA
;
230 if (nImgId
!= ScContentId::ROOT
)
232 BitmapEx aBitmapEx
{ aContentBmps
[static_cast<int>(nImgId
) -1 ] };
233 rRet
<<= VCLUnoHelper::CreateBitmap(aBitmapEx
);
237 uno::Any SAL_CALL
ScLinkTargetTypeObj::getPropertyValue(const OUString
& PropertyName
)
240 if ( PropertyName
== SC_UNO_LINKDISPBIT
)
241 SetLinkTargetBitmap( aRet
, nType
);
242 else if ( PropertyName
== SC_UNO_LINKDISPNAME
)
248 SC_IMPL_DUMMY_PROPERTY_LISTENER( ScLinkTargetTypeObj
)
250 ScLinkTargetsObj::ScLinkTargetsObj( uno::Reference
< container::XNameAccess
> xColl
) :
251 xCollection(std::move( xColl
))
253 OSL_ENSURE( xCollection
.is(), "ScLinkTargetsObj: NULL" );
256 ScLinkTargetsObj::~ScLinkTargetsObj()
260 // container::XNameAccess
262 uno::Any SAL_CALL
ScLinkTargetsObj::getByName(const OUString
& aName
)
264 uno::Reference
<beans::XPropertySet
> xProp(xCollection
->getByName(aName
), uno::UNO_QUERY
);
266 return uno::Any(xProp
);
268 throw container::NoSuchElementException();
271 uno::Sequence
<OUString
> SAL_CALL
ScLinkTargetsObj::getElementNames()
273 return xCollection
->getElementNames();
276 sal_Bool SAL_CALL
ScLinkTargetsObj::hasByName(const OUString
& aName
)
278 return xCollection
->hasByName(aName
);
281 // container::XElementAccess
283 uno::Type SAL_CALL
ScLinkTargetsObj::getElementType()
285 return cppu::UnoType
<beans::XPropertySet
>::get();
288 sal_Bool SAL_CALL
ScLinkTargetsObj::hasElements()
290 return xCollection
->hasElements();
293 /* vim:set shiftwidth=4 softtabstop=4 expandtab: */