LibreOffice Module svx (master) 1
unogaltheme.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 <algorithm>
21
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>
33
34using namespace ::com::sun::star;
35
36namespace unogallery {
37
38
39GalleryTheme::GalleryTheme( std::u16string_view rThemeName )
40{
42 mpTheme = ( mpGallery ? mpGallery->AcquireTheme( rThemeName, *this ) : nullptr );
43
44 if( mpGallery )
46}
47
48
50{
51 const SolarMutexGuard aGuard;
52
53 DBG_ASSERT( !mpTheme || mpGallery, "Theme is living without Gallery" );
54
55 implReleaseItems( nullptr );
56
57 if( mpGallery )
58 {
60
61 if( mpTheme )
63 }
64}
65
66
68{
69 return "com.sun.star.comp.gallery.GalleryTheme";
70}
71
72sal_Bool SAL_CALL GalleryTheme::supportsService( const OUString& ServiceName )
73{
74 return cppu::supportsService( this, ServiceName );
75}
76
77uno::Sequence< OUString > SAL_CALL GalleryTheme::getSupportedServiceNames()
78{
79 return { "com.sun.star.gallery.GalleryTheme" };
80}
81
82uno::Sequence< uno::Type > SAL_CALL GalleryTheme::getTypes()
83{
84 static const uno::Sequence aTypes {
90 };
91 return aTypes;
92}
93
94uno::Sequence< sal_Int8 > SAL_CALL GalleryTheme::getImplementationId()
95{
96 return css::uno::Sequence<sal_Int8>();
97}
98
99
101{
103}
104
105
107{
108 const SolarMutexGuard aGuard;
109
110 return( ( mpTheme != nullptr ) && ( mpTheme->GetObjectCount() > 0 ) );
111}
112
113
114sal_Int32 SAL_CALL GalleryTheme::getCount()
115{
116 const SolarMutexGuard aGuard;
117
118 return( mpTheme ? mpTheme->GetObjectCount() : 0 );
119}
120
121
122uno::Any SAL_CALL GalleryTheme::getByIndex( ::sal_Int32 nIndex )
123{
124 const SolarMutexGuard aGuard;
125 uno::Any aRet;
126
127 if( mpTheme )
128 {
129 if( ( nIndex < 0 ) || ( nIndex >= getCount() ) )
130 {
131 throw lang::IndexOutOfBoundsException();
132 }
133 const GalleryObject* pObj = mpTheme->maGalleryObjectCollection.getForPosition( nIndex );
134
135 if( pObj )
136 aRet <<= uno::Reference< gallery::XGalleryItem >( new GalleryItem( *this, *pObj ) );
137 }
138
139 return aRet;
140}
141
142
143OUString SAL_CALL GalleryTheme::getName( )
144{
145 const SolarMutexGuard aGuard;
146 OUString aRet;
147
148 if( mpTheme )
149 aRet = mpTheme->GetName();
150
151 return aRet;
152}
153
154
155void SAL_CALL GalleryTheme::update( )
156{
157 const SolarMutexGuard aGuard;
158
159 if( mpTheme )
160 {
161 const Link<const INetURLObject&, void> aDummyLink;
162 mpTheme->Actualize( aDummyLink );
163 }
164}
165
166
168 const OUString& rURL, ::sal_Int32 nIndex )
169{
170 const SolarMutexGuard aGuard;
171 sal_Int32 nRet = -1;
172
173 if( mpTheme )
174 {
175 try
176 {
177 const INetURLObject aURL( rURL );
178
179 nIndex = std::clamp( nIndex, sal_Int32(0), getCount() );
180
181 if( ( aURL.GetProtocol() != INetProtocol::NotValid ) && mpTheme->InsertURL( aURL, nIndex ) )
182 {
183 const GalleryObject* pObj = mpTheme->maGalleryObjectCollection.searchObjectWithURL( aURL );
184
185 if( pObj )
186 nRet = mpTheme->maGalleryObjectCollection.searchPosWithObject( pObj );
187 }
188 }
189 catch( ... )
190 {
191 }
192 }
193
194 return nRet;
195}
196
197
199 const uno::Reference< graphic::XGraphic >& rxGraphic, sal_Int32 nIndex )
200{
201 const SolarMutexGuard aGuard;
202 sal_Int32 nRet = -1;
203
204 if( mpTheme )
205 {
206 try
207 {
208 const Graphic aGraphic( rxGraphic );
209
210 nIndex = std::clamp( nIndex, sal_Int32(0), getCount() );
211
212 if( mpTheme->InsertGraphic( aGraphic, nIndex ) )
213 nRet = nIndex;
214 }
215 catch( ... )
216 {
217 }
218 }
219
220 return nRet;
221}
222
223
225 const uno::Reference< lang::XComponent >& Drawing, sal_Int32 nIndex )
226{
227 const SolarMutexGuard aGuard;
228 sal_Int32 nRet = -1;
229
230 if( mpTheme )
231 {
232 GalleryDrawingModel* pModel = comphelper::getFromUnoTunnel<GalleryDrawingModel>( Drawing );
233
234 if( pModel && dynamic_cast<const FmFormModel*>(pModel->GetDoc()) )
235 {
236 // Here we're inserting something that's already a gallery theme drawing
237 nIndex = std::clamp( nIndex, sal_Int32(0), getCount() );
238
239 if( mpTheme->InsertModel( *static_cast< FmFormModel* >( pModel->GetDoc() ), nIndex ) )
240 nRet = nIndex;
241 }
242 else if (!pModel)
243 {
244 // #i80184# Try to do the right thing and make a Gallery drawing out
245 // of an ordinary Drawing if possible.
246 try
247 {
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::getFromUnoTunnel<SvxDrawPage>( xPage ) : nullptr;
252 SdrModel* pOrigModel = pUnoPage ? &pUnoPage->GetSdrPage()->getSdrModelFromSdrPage() : nullptr;
253 SdrPage* pOrigPage = pUnoPage ? pUnoPage->GetSdrPage() : nullptr;
254
255 if (pOrigPage && pOrigModel)
256 {
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);
261
262 uno::Reference< lang::XComponent > xDrawing( new GalleryDrawingModel( pTmpModel ) );
263 pTmpModel->setUnoModel( uno::Reference< uno::XInterface >::query( xDrawing ) );
264
265 nRet = insertDrawingByIndex( xDrawing, nIndex );
266 return nRet;
267 }
268 }
269 catch (...)
270 {
271 }
272 }
273 }
274
275 return nRet;
276}
277
278
279void SAL_CALL GalleryTheme::removeByIndex( sal_Int32 nIndex )
280{
281 const SolarMutexGuard aGuard;
282
283 if( mpTheme )
284 {
285 if( ( nIndex < 0 ) || ( nIndex >= getCount() ) )
286 throw lang::IndexOutOfBoundsException();
287 mpTheme->RemoveObject( nIndex );
288 }
289}
290
291
293{
294 const SolarMutexGuard aGuard;
295 const GalleryHint& rGalleryHint = static_cast< const GalleryHint& >( rHint );
296
297 switch( rGalleryHint.GetType() )
298 {
300 {
301 DBG_ASSERT( !mpTheme || mpGallery, "Theme is living without Gallery" );
302
303 implReleaseItems( nullptr );
304
305 if( mpGallery && mpTheme )
306 {
307 mpGallery->ReleaseTheme( mpTheme, *this );
308 mpTheme = nullptr;
309 }
310 }
311 break;
312
314 {
315 GalleryObject* pObj = static_cast< GalleryObject* >( rGalleryHint.GetData1() );
316
317 if( pObj )
318 implReleaseItems( pObj );
319 }
320 break;
321
322 default:
323 break;
324 }
325}
326
327
329{
330 const SolarMutexGuard aGuard;
331
332 for( GalleryItemVector::iterator aIter = maItemVector.begin(); aIter != maItemVector.end(); )
333 {
334 if( !pObj || ( (*aIter)->implGetObject() == pObj ) )
335 {
336 (*aIter)->implSetInvalid();
337 aIter = maItemVector.erase( aIter );
338 }
339 else
340 ++aIter;
341 }
342}
343
344
346{
347 const SolarMutexGuard aGuard;
348
349 maItemVector.push_back( &rItem );
350}
351
352
354{
355 const SolarMutexGuard aGuard;
356
357 maItemVector.erase(std::remove(maItemVector.begin(), maItemVector.end(), &rItem), maItemVector.end());
358}
359
360}
361
362/* vim:set shiftwidth=4 softtabstop=4 expandtab: */
virtual void InsertPage(SdrPage *pPage, sal_uInt16 nPos=0xFFFF) override
Definition: fmmodel.cxx:80
void * GetData1() const
Definition: galmisc.hxx:195
GalleryHintType GetType() const
Definition: galmisc.hxx:192
static Gallery * GetGalleryInstance()
Definition: gallery1.cxx:242
void ReleaseTheme(GalleryTheme *pTheme, SfxListener &rListener)
Definition: gallery1.cxx:711
GalleryTheme * AcquireTheme(std::u16string_view rThemeName, SfxListener &rListener)
Definition: gallery1.cxx:697
const SfxItemPool & GetItemPool() const
Definition: svdmodel.hxx:318
void setUnoModel(const css::uno::Reference< css::uno::XInterface > &xModel)
Definition: svdmodel.cxx:1583
A SdrPage contains exactly one SdrObjList and a description of the physical page dimensions (size / m...
Definition: svdpage.hxx:379
virtual rtl::Reference< SdrPage > CloneSdrPage(SdrModel &rTargetModel) const
Definition: svdpage.cxx:1421
SdrModel & getSdrModelFromSdrPage() const
Definition: svdpage.hxx:403
void StartListening(SfxBroadcaster &rBroadcaster, DuplicateHandling eDuplicateHanding=DuplicateHandling::Unexpected)
void EndListening(SfxBroadcaster &rBroadcaster, bool bRemoveAllDuplicates=false)
SdrPage * GetSdrPage() const
Definition: unopage.hxx:77
SdrModel * GetDoc() const
Definition: unomodel.hxx:66
css::uno::Type const & get()
virtual ::sal_Int32 SAL_CALL insertURLByIndex(const OUString &URL, ::sal_Int32 Index) override
virtual sal_Bool SAL_CALL supportsService(const OUString &ServiceName) override
Definition: unogaltheme.cxx:72
virtual void SAL_CALL update() override
virtual ~GalleryTheme() override
Definition: unogaltheme.cxx:49
virtual ::sal_Int32 SAL_CALL insertDrawingByIndex(const css::uno::Reference< css::lang::XComponent > &Drawing, ::sal_Int32 Index) override
virtual OUString SAL_CALL getName() override
virtual css::uno::Sequence< OUString > SAL_CALL getSupportedServiceNames() override
Definition: unogaltheme.cxx:77
GalleryItemVector maItemVector
Definition: unogaltheme.hxx:84
GalleryTheme(std::u16string_view rThemeName)
Definition: unogaltheme.cxx:39
virtual sal_Bool SAL_CALL hasElements() override
virtual void Notify(SfxBroadcaster &rBC, const SfxHint &rHint) override
virtual ::sal_Int32 SAL_CALL insertGraphicByIndex(const css::uno::Reference< css::graphic::XGraphic > &Graphic, ::sal_Int32 Index) override
void implRegisterGalleryItem(::unogallery::GalleryItem &rItem)
virtual css::uno::Any SAL_CALL getByIndex(::sal_Int32 Index) override
virtual css::uno::Sequence< css::uno::Type > SAL_CALL getTypes() override
Definition: unogaltheme.cxx:82
virtual css::uno::Type SAL_CALL getElementType() override
::GalleryTheme * mpTheme
Definition: unogaltheme.hxx:86
virtual css::uno::Sequence< sal_Int8 > SAL_CALL getImplementationId() override
Definition: unogaltheme.cxx:94
void implDeregisterGalleryItem(::unogallery::GalleryItem &rItem)
virtual OUString SAL_CALL getImplementationName() override
Definition: unogaltheme.cxx:67
virtual void SAL_CALL removeByIndex(::sal_Int32 Index) override
virtual ::sal_Int32 SAL_CALL getCount() override
void implReleaseItems(GalleryObject const *pObj)
#define DBG_ASSERT(sCon, aError)
URL aURL
sal_Int32 nIndex
bool CPPUHELPER_DLLPUBLIC supportsService(css::lang::XServiceInfo *implementation, rtl::OUString const &name)
unsigned char sal_Bool
const SvXMLTokenMapEntry aTypes[]