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 .
22 #include "unogaltheme.hxx"
23 #include "unogalitem.hxx"
24 #include <svx/galtheme.hxx>
25 #include <svx/gallery1.hxx>
26 #include <svx/galmisc.hxx>
27 #include <svx/fmmodel.hxx>
28 #include <svx/svdpage.hxx>
29 #include <svx/unopage.hxx>
30 #include <vcl/svapp.hxx>
31 #include <com/sun/star/lang/IndexOutOfBoundsException.hpp>
32 #include <cppuhelper/supportsservice.hxx>
34 using namespace ::com::sun::star
;
36 namespace unogallery
{
39 GalleryTheme::GalleryTheme( std::u16string_view rThemeName
)
41 mpGallery
= ::Gallery::GetGalleryInstance();
42 mpTheme
= ( mpGallery
? mpGallery
->AcquireTheme( rThemeName
, *this ) : nullptr );
45 StartListening( *mpGallery
);
49 GalleryTheme::~GalleryTheme()
51 const SolarMutexGuard aGuard
;
53 DBG_ASSERT( !mpTheme
|| mpGallery
, "Theme is living without Gallery" );
55 implReleaseItems( nullptr );
59 EndListening( *mpGallery
);
62 mpGallery
->ReleaseTheme( mpTheme
, *this );
67 OUString SAL_CALL
GalleryTheme::getImplementationName()
69 return "com.sun.star.comp.gallery.GalleryTheme";
72 sal_Bool SAL_CALL
GalleryTheme::supportsService( const OUString
& ServiceName
)
74 return cppu::supportsService( this, ServiceName
);
77 uno::Sequence
< OUString
> SAL_CALL
GalleryTheme::getSupportedServiceNames()
79 return { "com.sun.star.gallery.GalleryTheme" };
82 uno::Sequence
< uno::Type
> SAL_CALL
GalleryTheme::getTypes()
84 static const uno::Sequence aTypes
{
85 cppu::UnoType
<lang::XServiceInfo
>::get(),
86 cppu::UnoType
<lang::XTypeProvider
>::get(),
87 cppu::UnoType
<container::XElementAccess
>::get(),
88 cppu::UnoType
<container::XIndexAccess
>::get(),
89 cppu::UnoType
<gallery::XGalleryTheme
>::get(),
94 uno::Sequence
< sal_Int8
> SAL_CALL
GalleryTheme::getImplementationId()
96 return css::uno::Sequence
<sal_Int8
>();
100 uno::Type SAL_CALL
GalleryTheme::getElementType()
102 return cppu::UnoType
<gallery::XGalleryItem
>::get();
106 sal_Bool SAL_CALL
GalleryTheme::hasElements()
108 const SolarMutexGuard aGuard
;
110 return( ( mpTheme
!= nullptr ) && ( mpTheme
->GetObjectCount() > 0 ) );
114 sal_Int32 SAL_CALL
GalleryTheme::getCount()
116 const SolarMutexGuard aGuard
;
118 return( mpTheme
? mpTheme
->GetObjectCount() : 0 );
122 uno::Any SAL_CALL
GalleryTheme::getByIndex( ::sal_Int32 nIndex
)
124 const SolarMutexGuard aGuard
;
129 if( ( nIndex
< 0 ) || ( nIndex
>= getCount() ) )
131 throw lang::IndexOutOfBoundsException();
133 const GalleryObject
* pObj
= mpTheme
->maGalleryObjectCollection
.getForPosition( nIndex
);
136 aRet
<<= uno::Reference
< gallery::XGalleryItem
>( new GalleryItem( *this, *pObj
) );
143 OUString SAL_CALL
GalleryTheme::getName( )
145 const SolarMutexGuard aGuard
;
149 aRet
= mpTheme
->GetName();
155 void SAL_CALL
GalleryTheme::update( )
157 const SolarMutexGuard aGuard
;
161 const Link
<const INetURLObject
&, void> aDummyLink
;
162 mpTheme
->Actualize( aDummyLink
);
167 ::sal_Int32 SAL_CALL
GalleryTheme::insertURLByIndex(
168 const OUString
& rURL
, ::sal_Int32 nIndex
)
170 const SolarMutexGuard aGuard
;
177 const INetURLObject
aURL( rURL
);
179 nIndex
= std::clamp( nIndex
, sal_Int32(0), getCount() );
181 if( ( aURL
.GetProtocol() != INetProtocol::NotValid
) && mpTheme
->InsertURL( aURL
, nIndex
) )
183 const GalleryObject
* pObj
= mpTheme
->maGalleryObjectCollection
.searchObjectWithURL( aURL
);
186 nRet
= mpTheme
->maGalleryObjectCollection
.searchPosWithObject( pObj
);
198 ::sal_Int32 SAL_CALL
GalleryTheme::insertGraphicByIndex(
199 const uno::Reference
< graphic::XGraphic
>& rxGraphic
, sal_Int32 nIndex
)
201 const SolarMutexGuard aGuard
;
208 const Graphic
aGraphic( rxGraphic
);
210 nIndex
= std::clamp( nIndex
, sal_Int32(0), getCount() );
212 if( mpTheme
->InsertGraphic( aGraphic
, nIndex
) )
224 ::sal_Int32 SAL_CALL
GalleryTheme::insertDrawingByIndex(
225 const uno::Reference
< lang::XComponent
>& Drawing
, sal_Int32 nIndex
)
227 const SolarMutexGuard aGuard
;
232 GalleryDrawingModel
* pModel
= comphelper::getUnoTunnelImplementation
<GalleryDrawingModel
>( Drawing
);
234 if( pModel
&& dynamic_cast<const FmFormModel
*>(pModel
->GetDoc()) )
236 // Here we're inserting something that's already a gallery theme drawing
237 nIndex
= std::clamp( nIndex
, sal_Int32(0), getCount() );
239 if( mpTheme
->InsertModel( *static_cast< FmFormModel
* >( pModel
->GetDoc() ), nIndex
) )
244 // #i80184# Try to do the right thing and make a Gallery drawing out
245 // of an ordinary Drawing if possible.
248 uno::Reference
< drawing::XDrawPagesSupplier
> xDrawPagesSupplier( Drawing
, uno::UNO_QUERY_THROW
);
249 uno::Reference
< drawing::XDrawPages
> xDrawPages( xDrawPagesSupplier
->getDrawPages(), uno::UNO_SET_THROW
);
250 uno::Reference
< drawing::XDrawPage
> xPage( xDrawPages
->getByIndex( 0 ), uno::UNO_QUERY_THROW
);
251 SvxDrawPage
* pUnoPage
= xPage
.is() ? comphelper::getUnoTunnelImplementation
<SvxDrawPage
>( xPage
) : nullptr;
252 SdrModel
* pOrigModel
= pUnoPage
? &pUnoPage
->GetSdrPage()->getSdrModelFromSdrPage() : nullptr;
253 SdrPage
* pOrigPage
= pUnoPage
? pUnoPage
->GetSdrPage() : nullptr;
255 if (pOrigPage
&& pOrigModel
)
257 FmFormModel
* pTmpModel
= new FmFormModel(&pOrigModel
->GetItemPool());
258 // Clone to new target SdrModel
259 rtl::Reference
<SdrPage
> pNewPage
= pOrigPage
->CloneSdrPage(*pTmpModel
);
260 pTmpModel
->InsertPage(pNewPage
.get(), 0);
262 uno::Reference
< lang::XComponent
> xDrawing( new GalleryDrawingModel( pTmpModel
) );
263 pTmpModel
->setUnoModel( uno::Reference
< uno::XInterface
>::query( xDrawing
) );
265 nRet
= insertDrawingByIndex( xDrawing
, nIndex
);
279 void SAL_CALL
GalleryTheme::removeByIndex( sal_Int32 nIndex
)
281 const SolarMutexGuard aGuard
;
285 if( ( nIndex
< 0 ) || ( nIndex
>= getCount() ) )
286 throw lang::IndexOutOfBoundsException();
287 mpTheme
->RemoveObject( nIndex
);
292 void GalleryTheme::Notify( SfxBroadcaster
&, const SfxHint
& rHint
)
294 const SolarMutexGuard aGuard
;
295 const GalleryHint
& rGalleryHint
= static_cast< const GalleryHint
& >( rHint
);
297 switch( rGalleryHint
.GetType() )
299 case GalleryHintType::CLOSE_THEME
:
301 DBG_ASSERT( !mpTheme
|| mpGallery
, "Theme is living without Gallery" );
303 implReleaseItems( nullptr );
305 if( mpGallery
&& mpTheme
)
307 mpGallery
->ReleaseTheme( mpTheme
, *this );
313 case GalleryHintType::CLOSE_OBJECT
:
315 GalleryObject
* pObj
= static_cast< GalleryObject
* >( rGalleryHint
.GetData1() );
318 implReleaseItems( pObj
);
328 void GalleryTheme::implReleaseItems( GalleryObject
const * pObj
)
330 const SolarMutexGuard aGuard
;
332 for( GalleryItemVector::iterator aIter
= maItemVector
.begin(); aIter
!= maItemVector
.end(); )
334 if( !pObj
|| ( (*aIter
)->implGetObject() == pObj
) )
336 (*aIter
)->implSetInvalid();
337 aIter
= maItemVector
.erase( aIter
);
345 void GalleryTheme::implRegisterGalleryItem( ::unogallery::GalleryItem
& rItem
)
347 const SolarMutexGuard aGuard
;
349 maItemVector
.push_back( &rItem
);
353 void GalleryTheme::implDeregisterGalleryItem( ::unogallery::GalleryItem
& rItem
)
355 const SolarMutexGuard aGuard
;
357 maItemVector
.erase(std::remove(maItemVector
.begin(), maItemVector
.end(), &rItem
), maItemVector
.end());
362 /* vim:set shiftwidth=4 softtabstop=4 expandtab: */