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>
24 #include <com/sun/star/lang/XServiceInfo.hpp>
25 #include <com/sun/star/container/XNameContainer.hpp>
26 #include <com/sun/star/drawing/PointSequence.hpp>
28 #include <comphelper/sequence.hxx>
29 #include <cppuhelper/implbase.hxx>
30 #include <cppuhelper/supportsservice.hxx>
31 #include <svl/itempool.hxx>
32 #include <svl/itemset.hxx>
33 #include <svl/lstner.hxx>
34 #include <svx/xlnedit.hxx>
35 #include <svx/xlnstit.hxx>
36 #include <svx/svdmodel.hxx>
37 #include <svx/xdef.hxx>
40 #include <vcl/svapp.hxx>
43 #include <svx/unofill.hxx>
45 #include <svx/unoapi.hxx>
47 using namespace ::com::sun::star
;
48 using namespace ::cppu
;
50 typedef std::vector
<std::unique_ptr
<SfxItemSet
>> ItemPoolVector
;
54 class SvxUnoMarkerTable
: public WeakImplHelper
< container::XNameContainer
, lang::XServiceInfo
>,
59 SfxItemPool
* mpModelPool
;
61 ItemPoolVector maItemSetVector
;
64 explicit SvxUnoMarkerTable( SdrModel
* pModel
) throw();
65 virtual ~SvxUnoMarkerTable() throw() override
;
70 virtual void Notify( SfxBroadcaster
& rBC
, const SfxHint
& rHint
) throw () override
;
72 void ImplInsertByName( const OUString
& aName
, const uno::Any
& aElement
);
75 virtual OUString SAL_CALL
getImplementationName( ) override
;
76 virtual sal_Bool SAL_CALL
supportsService( const OUString
& ServiceName
) override
;
77 virtual uno::Sequence
< OUString
> SAL_CALL
getSupportedServiceNames( ) override
;
80 virtual void SAL_CALL
insertByName( const OUString
& aName
, const uno::Any
& aElement
) override
;
81 virtual void SAL_CALL
removeByName( const OUString
& Name
) override
;
84 virtual void SAL_CALL
replaceByName( const OUString
& aName
, const uno::Any
& aElement
) override
;
87 virtual uno::Any SAL_CALL
getByName( const OUString
& aName
) override
;
88 virtual uno::Sequence
< OUString
> SAL_CALL
getElementNames( ) override
;
89 virtual sal_Bool SAL_CALL
hasByName( const OUString
& aName
) override
;
92 virtual uno::Type SAL_CALL
getElementType( ) override
;
93 virtual sal_Bool SAL_CALL
hasElements( ) override
;
98 SvxUnoMarkerTable::SvxUnoMarkerTable( SdrModel
* pModel
) throw()
100 mpModelPool( pModel
? &pModel
->GetItemPool() : nullptr )
103 StartListening( *pModel
);
106 SvxUnoMarkerTable::~SvxUnoMarkerTable() throw()
109 EndListening( *mpModel
);
113 void SvxUnoMarkerTable::dispose()
115 maItemSetVector
.clear();
119 void SvxUnoMarkerTable::Notify( SfxBroadcaster
&, const SfxHint
& rHint
) throw()
121 if (rHint
.GetId() == SfxHintId::ThisIsAnSdrHint
)
123 const SdrHint
* pSdrHint
= static_cast<const SdrHint
*>(&rHint
);
124 if( SdrHintKind::ModelCleared
== pSdrHint
->GetKind() )
129 sal_Bool SAL_CALL
SvxUnoMarkerTable::supportsService( const OUString
& ServiceName
)
131 return cppu::supportsService(this, ServiceName
);
134 OUString SAL_CALL
SvxUnoMarkerTable::getImplementationName()
136 return "SvxUnoMarkerTable";
139 uno::Sequence
< OUString
> SAL_CALL
SvxUnoMarkerTable::getSupportedServiceNames( )
141 uno::Sequence
<OUString
> aSNS
{ "com.sun.star.drawing.MarkerTable" };
145 void SvxUnoMarkerTable::ImplInsertByName( const OUString
& aName
, const uno::Any
& aElement
)
147 maItemSetVector
.push_back(
148 std::make_unique
<SfxItemSet
>( *mpModelPool
, svl::Items
<XATTR_LINESTART
, XATTR_LINEEND
>{} ));
149 auto pInSet
= maItemSetVector
.back().get();
151 XLineEndItem
aEndMarker(XATTR_LINEEND
);
152 aEndMarker
.SetName( aName
);
153 aEndMarker
.PutValue( aElement
, 0 );
155 pInSet
->Put( aEndMarker
);
157 XLineStartItem
aStartMarker(XATTR_LINESTART
);
158 aStartMarker
.SetName( aName
);
159 aStartMarker
.PutValue( aElement
, 0 );
161 pInSet
->Put( aStartMarker
);
165 void SAL_CALL
SvxUnoMarkerTable::insertByName( const OUString
& aApiName
, const uno::Any
& aElement
)
167 SolarMutexGuard aGuard
;
169 if( hasByName( aApiName
) )
170 throw container::ElementExistException();
172 OUString aName
= SvxUnogetInternalNameForItem(XATTR_LINEEND
, aApiName
);
174 ImplInsertByName( aName
, aElement
);
177 void SAL_CALL
SvxUnoMarkerTable::removeByName( const OUString
& aApiName
)
179 SolarMutexGuard aGuard
;
181 // a little quickfix for 2.0 to let applications clear api
182 // created items that are not used
183 if ( aApiName
== "~clear~" )
189 OUString aName
= SvxUnogetInternalNameForItem(XATTR_LINEEND
, aApiName
);
191 auto aIter
= std::find_if(maItemSetVector
.begin(), maItemSetVector
.end(),
192 [&aName
](const std::unique_ptr
<SfxItemSet
>& rpItem
) {
193 const NameOrIndex
*pItem
= &(rpItem
->Get( XATTR_LINEEND
) );
194 return pItem
->GetName() == aName
;
196 if (aIter
!= maItemSetVector
.end())
198 maItemSetVector
.erase( aIter
);
202 if( !hasByName( aName
) )
203 throw container::NoSuchElementException();
207 void SAL_CALL
SvxUnoMarkerTable::replaceByName( const OUString
& aApiName
, const uno::Any
& aElement
)
209 SolarMutexGuard aGuard
;
211 const OUString aName
= SvxUnogetInternalNameForItem(XATTR_LINEEND
, aApiName
);
213 auto aIter
= std::find_if(maItemSetVector
.begin(), maItemSetVector
.end(),
214 [&aName
](const std::unique_ptr
<SfxItemSet
>& rpItem
) {
215 const NameOrIndex
*pItem
= &(rpItem
->Get( XATTR_LINEEND
) );
216 return pItem
->GetName() == aName
;
218 if (aIter
!= maItemSetVector
.end())
220 XLineEndItem
aEndMarker(XATTR_LINEEND
);
221 aEndMarker
.SetName( aName
);
222 if( !aEndMarker
.PutValue( aElement
, 0 ) )
223 throw lang::IllegalArgumentException();
225 (*aIter
)->Put( aEndMarker
);
227 XLineStartItem
aStartMarker(XATTR_LINESTART
);
228 aStartMarker
.SetName( aName
);
229 aStartMarker
.PutValue( aElement
, 0 );
231 (*aIter
)->Put( aStartMarker
);
235 // if it is not in our own sets, modify the pool!
239 for (const SfxPoolItem
* p
: mpModelPool
->GetItemSurrogates(XATTR_LINESTART
))
241 NameOrIndex
*pItem
= const_cast<NameOrIndex
*>(static_cast<const NameOrIndex
*>(p
));
242 if( pItem
&& pItem
->GetName() == aName
)
244 pItem
->PutValue( aElement
, 0 );
251 for (const SfxPoolItem
* p
: mpModelPool
->GetItemSurrogates(XATTR_LINEEND
))
253 NameOrIndex
*pItem
= const_cast<NameOrIndex
*>(static_cast<const NameOrIndex
*>(p
));
254 if( pItem
&& pItem
->GetName() == aName
)
256 pItem
->PutValue( aElement
, 0 );
263 throw container::NoSuchElementException();
265 ImplInsertByName( aName
, aElement
);
268 static bool getByNameFromPool( const OUString
& rSearchName
, SfxItemPool
const * pPool
, sal_uInt16 nWhich
, uno::Any
& rAny
)
271 for (const SfxPoolItem
* p
: pPool
->GetItemSurrogates(nWhich
))
273 const NameOrIndex
*pItem
= static_cast<const NameOrIndex
*>(p
);
275 if( pItem
&& pItem
->GetName() == rSearchName
)
277 pItem
->QueryValue( rAny
);
286 uno::Any SAL_CALL
SvxUnoMarkerTable::getByName( const OUString
& aApiName
)
288 SolarMutexGuard aGuard
;
290 OUString aName
= SvxUnogetInternalNameForItem(XATTR_LINEEND
, aApiName
);
294 if (mpModelPool
&& !aName
.isEmpty())
298 if (getByNameFromPool(aName
, mpModelPool
, XATTR_LINESTART
, aAny
))
301 if (getByNameFromPool(aName
, mpModelPool
, XATTR_LINEEND
, aAny
))
304 throw container::NoSuchElementException();
312 static void createNamesForPool( SfxItemPool
const * pPool
, sal_uInt16 nWhich
, std::set
< OUString
>& rNameSet
)
314 for (const SfxPoolItem
* p
: pPool
->GetItemSurrogates(nWhich
))
316 const NameOrIndex
* pItem
= static_cast<const NameOrIndex
*>(p
);
318 if( pItem
== nullptr || pItem
->GetName().isEmpty() )
321 OUString aName
= SvxUnogetApiNameForItem(XATTR_LINEEND
, pItem
->GetName());
322 rNameSet
.insert( aName
);
326 uno::Sequence
< OUString
> SAL_CALL
SvxUnoMarkerTable::getElementNames()
328 SolarMutexGuard aGuard
;
330 std::set
< OUString
> aNameSet
;
332 // search model pool for line starts
333 createNamesForPool( mpModelPool
, XATTR_LINESTART
, aNameSet
);
335 // search model pool for line ends
336 createNamesForPool( mpModelPool
, XATTR_LINEEND
, aNameSet
);
338 return comphelper::containerToSequence(aNameSet
);
341 sal_Bool SAL_CALL
SvxUnoMarkerTable::hasByName( const OUString
& aName
)
343 SolarMutexGuard aGuard
;
345 if( aName
.isEmpty() )
348 OUString aSearchName
;
350 const NameOrIndex
*pItem
;
352 aSearchName
= SvxUnogetInternalNameForItem(XATTR_LINESTART
, aName
);
354 for (const SfxPoolItem
* p
: mpModelPool
->GetItemSurrogates(XATTR_LINESTART
))
356 pItem
= static_cast<const NameOrIndex
*>(p
);
357 if( pItem
&& pItem
->GetName() == aSearchName
)
361 aSearchName
= SvxUnogetInternalNameForItem(XATTR_LINEEND
, aName
);
363 for (const SfxPoolItem
* p
: mpModelPool
->GetItemSurrogates(XATTR_LINEEND
))
365 pItem
= static_cast<const NameOrIndex
*>(p
);
366 if( pItem
&& pItem
->GetName() == aSearchName
)
374 uno::Type SAL_CALL
SvxUnoMarkerTable::getElementType( )
376 return cppu::UnoType
<drawing::PointSequence
>::get();
379 sal_Bool SAL_CALL
SvxUnoMarkerTable::hasElements( )
381 SolarMutexGuard aGuard
;
383 const NameOrIndex
*pItem
;
386 for (const SfxPoolItem
* p
: mpModelPool
->GetItemSurrogates(XATTR_LINESTART
))
388 pItem
= static_cast<const NameOrIndex
*>(p
);
389 if( pItem
&& !pItem
->GetName().isEmpty() )
394 for (const SfxPoolItem
* p
: mpModelPool
->GetItemSurrogates(XATTR_LINEEND
))
396 pItem
= static_cast<const NameOrIndex
*>(p
);
397 if( pItem
&& !pItem
->GetName().isEmpty() )
405 * Create a hatchtable
407 uno::Reference
< uno::XInterface
> SvxUnoMarkerTable_createInstance( SdrModel
* pModel
)
409 return *new SvxUnoMarkerTable(pModel
);
413 /* vim:set shiftwidth=4 softtabstop=4 expandtab: */