LibreOffice Module sc (master)  1
targuno.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 
23 #include <svl/itemprop.hxx>
24 #include <svl/hint.hxx>
25 #include <vcl/svapp.hxx>
26 #include <osl/diagnose.h>
27 #include <com/sun/star/awt/XBitmap.hpp>
28 #include <com/sun/star/beans/PropertyAttribute.hpp>
29 
30 #include <targuno.hxx>
31 #include <miscuno.hxx>
32 #include <docuno.hxx>
33 #include <datauno.hxx>
34 #include <nameuno.hxx>
35 #include <docsh.hxx>
36 #include <content.hxx>
37 #include <scresid.hxx>
38 #include <strings.hrc>
39 #include <bitmaps.hlst>
40 #include <unonames.hxx>
41 
42 using namespace ::com::sun::star;
43 
45 {
46  SCSTR_CONTENT_TABLE, // SC_LINKTARGETTYPE_SHEET
47  SCSTR_CONTENT_RANGENAME, // SC_LINKTARGETTYPE_RANGENAME
48  SCSTR_CONTENT_DBAREA // SC_LINKTARGETTYPE_DBAREA
49 };
50 
52 {
53  static const SfxItemPropertyMapEntry aLinkTargetMap_Impl[] =
54  {
55  { SC_UNO_LINKDISPBIT, 0, cppu::UnoType<awt::XBitmap>::get(), beans::PropertyAttribute::READONLY, 0 },
56  { SC_UNO_LINKDISPNAME, 0, cppu::UnoType<OUString>::get(), beans::PropertyAttribute::READONLY, 0 },
57  { u"", 0, css::uno::Type(), 0, 0 }
58  };
59  return aLinkTargetMap_Impl;
60 }
61 
62 // service for ScLinkTargetTypeObj is not defined
63 // must not support document::LinkTarget because the target type cannot be used as a target
64 
65 SC_SIMPLE_SERVICE_INFO( ScLinkTargetTypesObj, "ScLinkTargetTypesObj", "com.sun.star.document.LinkTargets" )
66 SC_SIMPLE_SERVICE_INFO( ScLinkTargetTypeObj, "ScLinkTargetTypeObj", "com.sun.star.document.LinkTargetSupplier" )
67 SC_SIMPLE_SERVICE_INFO( ScLinkTargetsObj, "ScLinkTargetsObj", "com.sun.star.document.LinkTargets" )
68 
70  pDocShell( pDocSh )
71 {
72  pDocShell->GetDocument().AddUnoObject(*this);
73 
74  for (sal_uInt16 i=0; i<SC_LINKTARGETTYPE_COUNT; i++)
75  aNames[i] = ScResId(aTypeResIds[i]);
76 }
77 
79 {
81 
82  if (pDocShell)
84 }
85 
87 {
88  if ( rHint.GetId() == SfxHintId::Dying )
89  pDocShell = nullptr; // document gone
90 }
91 
92 // container::XNameAccess
93 
94 uno::Any SAL_CALL ScLinkTargetTypesObj::getByName(const OUString& aName)
95 {
96  if (pDocShell)
97  {
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 )));
101  }
102 
103  throw container::NoSuchElementException();
104 }
105 
106 uno::Sequence<OUString> SAL_CALL ScLinkTargetTypesObj::getElementNames()
107 {
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];
112  return aRet;
113 }
114 
115 sal_Bool SAL_CALL ScLinkTargetTypesObj::hasByName(const OUString& aName)
116 {
117  for (const auto & i : aNames)
118  if ( i == aName )
119  return true;
120  return false;
121 }
122 
123 // container::XElementAccess
124 
126 {
128 }
129 
131 {
132  return true;
133 }
134 
136  pDocShell( pDocSh ),
137  nType( nT )
138 {
140  aName = ScResId(aTypeResIds[nType]);
141 }
142 
144 {
145  SolarMutexGuard g;
146 
147  if (pDocShell)
149 }
150 
152 {
153  if ( rHint.GetId() == SfxHintId::Dying )
154  pDocShell = nullptr; // document gone
155 }
156 
157 // document::XLinkTargetSupplier
158 
159 uno::Reference< container::XNameAccess > SAL_CALL ScLinkTargetTypeObj::getLinks()
160 {
161  uno::Reference< container::XNameAccess > xCollection;
162 
163  if ( pDocShell )
164  {
165  switch ( nType )
166  {
168  xCollection.set(new ScTableSheetsObj(pDocShell));
169  break;
171  xCollection.set(new ScGlobalNamedRangesObj(pDocShell));
172  break;
174  xCollection.set(new ScDatabaseRangesObj(pDocShell));
175  break;
176  default:
177  OSL_FAIL("invalid type");
178  }
179  }
180 
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 );
185  return nullptr;
186 }
187 
188 // beans::XPropertySet
189 
190 uno::Reference< beans::XPropertySetInfo > SAL_CALL ScLinkTargetTypeObj::getPropertySetInfo()
191 {
192  static uno::Reference< beans::XPropertySetInfo > aRef(new SfxItemPropertySetInfo( lcl_GetLinkTargetMap() ));
193  return aRef;
194 }
195 
196 void SAL_CALL ScLinkTargetTypeObj::setPropertyValue(const OUString& /* aPropertyName */,
197  const uno::Any& /* aValue */)
198 {
199  // everything is read-only
201 }
202 
203 constexpr rtl::OUStringConstExpr aContentBmps[]=
204 {
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
213 };
214 
216 {
218  switch ( nType )
219  {
221  nImgId = ScContentId::TABLE;
222  break;
224  nImgId = ScContentId::RANGENAME;
225  break;
227  nImgId = ScContentId::DBAREA;
228  break;
229  }
230  if (nImgId != ScContentId::ROOT)
231  {
232  BitmapEx aBitmapEx { OUString(aContentBmps[static_cast<int>(nImgId) -1 ]) };
233  rRet <<= VCLUnoHelper::CreateBitmap(aBitmapEx);
234  }
235 }
236 
237 uno::Any SAL_CALL ScLinkTargetTypeObj::getPropertyValue(const OUString& PropertyName)
238 {
239  uno::Any aRet;
240  if ( PropertyName == SC_UNO_LINKDISPBIT )
241  SetLinkTargetBitmap( aRet, nType );
242  else if ( PropertyName == SC_UNO_LINKDISPNAME )
243  aRet <<= aName;
244 
245  return aRet;
246 }
247 
249 
250 ScLinkTargetsObj::ScLinkTargetsObj( const uno::Reference< container::XNameAccess > & rColl ) :
251  xCollection( rColl )
252 {
253  OSL_ENSURE( xCollection.is(), "ScLinkTargetsObj: NULL" );
254 }
255 
257 {
258 }
259 
260 // container::XNameAccess
261 
262 uno::Any SAL_CALL ScLinkTargetsObj::getByName(const OUString& aName)
263 {
264  uno::Reference<beans::XPropertySet> xProp(xCollection->getByName(aName), uno::UNO_QUERY);
265  if (xProp.is())
266  return uno::Any(xProp);
267 
268  throw container::NoSuchElementException();
269 }
270 
271 uno::Sequence<OUString> SAL_CALL ScLinkTargetsObj::getElementNames()
272 {
273  return xCollection->getElementNames();
274 }
275 
276 sal_Bool SAL_CALL ScLinkTargetsObj::hasByName(const OUString& aName)
277 {
278  return xCollection->hasByName(aName);
279 }
280 
281 // container::XElementAccess
282 
284 {
286 }
287 
289 {
290  return xCollection->hasElements();
291 }
292 
293 /* vim:set shiftwidth=4 softtabstop=4 expandtab: */
OUString ScResId(TranslateId aId)
Definition: scdll.cxx:90
virtual css::uno::Reference< css::beans::XPropertySetInfo > SAL_CALL getPropertySetInfo() override
Definition: targuno.cxx:190
#define SC_LINKTARGETTYPE_COUNT
Definition: targuno.hxx:39
static void SetLinkTargetBitmap(css::uno::Any &rRet, sal_uInt16 nType)
Definition: targuno.cxx:215
SfxHintId GetId() const
virtual css::uno::Any SAL_CALL getByName(const OUString &aName) override
Definition: targuno.cxx:262
#define SC_LINKTARGETTYPE_RANGENAME
Definition: targuno.hxx:36
static const SfxItemPropertyMapEntry * lcl_GetLinkTargetMap()
Definition: targuno.cxx:51
virtual void Notify(SfxBroadcaster &rBC, const SfxHint &rHint) override
Definition: targuno.cxx:86
virtual css::uno::Sequence< OUString > SAL_CALL getElementNames() override
Definition: targuno.cxx:271
virtual ~ScLinkTargetTypesObj() override
Definition: targuno.cxx:78
virtual css::uno::Type SAL_CALL getElementType() override
Definition: targuno.cxx:125
static css::uno::Reference< css::awt::XBitmap > CreateBitmap(const BitmapEx &rBitmap)
OUString aNames[SC_LINKTARGETTYPE_COUNT]
Definition: targuno.hxx:52
int i
virtual css::uno::Type SAL_CALL getElementType() override
Definition: targuno.cxx:283
virtual css::uno::Sequence< OUString > SAL_CALL getElementNames() override
Definition: targuno.cxx:106
virtual sal_Bool SAL_CALL hasByName(const OUString &aName) override
Definition: targuno.cxx:276
css::uno::Reference< css::container::XNameAccess > xCollection
Definition: targuno.hxx:122
virtual css::uno::Any SAL_CALL getByName(const OUString &aName) override
Definition: targuno.cxx:94
float u
unsigned char sal_Bool
ScDocShell * pDocShell
Definition: targuno.hxx:51
virtual void SAL_CALL setPropertyValue(const OUString &aPropertyName, const css::uno::Any &aValue) override
Definition: targuno.cxx:196
css::uno::Type const & get()
#define SC_SIMPLE_SERVICE_INFO(ClassName, ClassNameAscii, ServiceAscii)
Definition: miscuno.hxx:63
Graphic / OleObject (need separate collections!)
Definition: targuno.hxx:45
virtual ~ScLinkTargetTypeObj() override
Definition: targuno.cxx:143
ScContentId
Definition: content.hxx:35
#define SC_IMPL_DUMMY_PROPERTY_LISTENER(ClassName)
Definition: miscuno.hxx:72
void AddUnoObject(SfxListener &rObject)
Definition: documen3.cxx:901
virtual void Notify(SfxBroadcaster &rBC, const SfxHint &rHint) override
Definition: targuno.cxx:151
#define SC_LINKTARGETTYPE_SHEET
Definition: targuno.hxx:35
virtual sal_Bool SAL_CALL hasElements() override
Definition: targuno.cxx:288
virtual css::uno::Reference< css::container::XNameAccess > SAL_CALL getLinks() override
Definition: targuno.cxx:159
ScDocShell * pDocShell
Definition: targuno.hxx:82
#define SC_UNO_LINKDISPBIT
Definition: unonames.hxx:190
void RemoveUnoObject(SfxListener &rObject)
Definition: documen3.cxx:909
virtual sal_Bool SAL_CALL hasByName(const OUString &aName) override
Definition: targuno.cxx:115
QPRO_FUNC_TYPE nType
Definition: qproform.cxx:398
ScLinkTargetTypeObj(ScDocShell *pDocSh, sal_uInt16 nT)
Definition: targuno.cxx:135
const ScDocument & GetDocument() const
Definition: docsh.hxx:220
const TranslateId aTypeResIds[SC_LINKTARGETTYPE_COUNT]
Definition: targuno.cxx:44
virtual sal_Bool SAL_CALL hasElements() override
Definition: targuno.cxx:130
sal_uInt16 nType
Definition: targuno.hxx:83
virtual css::uno::Any SAL_CALL getPropertyValue(const OUString &PropertyName) override
Definition: targuno.cxx:237
#define SC_UNO_LINKDISPNAME
Definition: unonames.hxx:191
constexpr rtl::OUStringConstExpr aContentBmps[]
Definition: targuno.cxx:203
#define SC_LINKTARGETTYPE_DBAREA
Definition: targuno.hxx:37
virtual ~ScLinkTargetsObj() override
Definition: targuno.cxx:256