tdf#130857 qt weld: Implement QtInstanceWidget::strip_mnemonic
[LibreOffice.git] / sd / source / ui / unoidl / unocpres.cxx
blob87b52623c1945b38a22e6f0801f8e23d8a6b2837
1 /* -*- Mode: C++; tab-width: 4; indent-tabs-mode: nil; c-basic-offset: 4 -*- */
2 /*
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 <algorithm>
22 #include <com/sun/star/lang/DisposedException.hpp>
23 #include <com/sun/star/lang/IndexOutOfBoundsException.hpp>
24 #include <o3tl/safeint.hxx>
25 #include <vcl/svapp.hxx>
26 #include <svx/svdpage.hxx>
27 #include <cppuhelper/supportsservice.hxx>
29 #include <createunocustomshow.hxx>
30 #include <unomodel.hxx>
31 #include <drawdoc.hxx>
32 #include "unocpres.hxx"
33 #include <cusshow.hxx>
34 #include <unopage.hxx>
35 #include <customshowlist.hxx>
37 using namespace ::com::sun::star;
39 uno::Reference< uno::XInterface > createUnoCustomShow( SdCustomShow* pShow )
41 return static_cast<cppu::OWeakObject*>(new SdXCustomPresentation( pShow ));
44 SdXCustomPresentation::SdXCustomPresentation() noexcept
45 : mpSdCustomShow(nullptr), mpModel(nullptr),
46 bDisposing( false )
50 SdXCustomPresentation::SdXCustomPresentation( SdCustomShow* pShow) noexcept
51 : mpSdCustomShow(pShow), mpModel(nullptr),
52 bDisposing( false )
56 SdXCustomPresentation::~SdXCustomPresentation() noexcept
60 // XServiceInfo
61 OUString SAL_CALL SdXCustomPresentation::getImplementationName()
63 return u"SdXCustomPresentation"_ustr ;
66 sal_Bool SAL_CALL SdXCustomPresentation::supportsService( const OUString& ServiceName )
68 return cppu::supportsService( this, ServiceName );
71 uno::Sequence< OUString > SAL_CALL SdXCustomPresentation::getSupportedServiceNames()
73 return { u"com.sun.star.presentation.CustomPresentation"_ustr };
76 // XIndexContainer
77 void SAL_CALL SdXCustomPresentation::insertByIndex( sal_Int32 Index, const uno::Any& Element )
79 SolarMutexGuard aGuard;
81 if( bDisposing )
82 throw lang::DisposedException();
84 if( Index < 0 || o3tl::make_unsigned(Index) > ( mpSdCustomShow ? mpSdCustomShow->PagesVector().size() : 0 ) )
85 throw lang::IndexOutOfBoundsException();
87 uno::Reference< drawing::XDrawPage > xPage;
88 Element >>= xPage;
90 if(!xPage.is())
91 throw lang::IllegalArgumentException();
93 SdDrawPage* pPage = comphelper::getFromUnoTunnel<SdDrawPage>( xPage );
95 if(pPage)
97 if (!mpModel)
98 mpModel = pPage->GetModel();
100 if (!mpSdCustomShow)
101 mpSdCustomShow = new SdCustomShow;
103 mpSdCustomShow->PagesVector().insert(mpSdCustomShow->PagesVector().begin() + Index,
104 static_cast<SdPage*>(pPage->GetSdrPage()));
107 if( mpModel )
108 mpModel->SetModified();
111 void SAL_CALL SdXCustomPresentation::removeByIndex( sal_Int32 Index )
113 SolarMutexGuard aGuard;
115 if( bDisposing )
116 throw lang::DisposedException();
118 if(mpSdCustomShow)
120 uno::Reference< drawing::XDrawPage > xPage;
121 getByIndex( Index ) >>= xPage;
123 if( xPage.is() )
125 SvxDrawPage* pPage = comphelper::getFromUnoTunnel<SvxDrawPage>( xPage );
126 if(pPage)
128 SdCustomShow::PageVec::iterator it = std::find(
129 mpSdCustomShow->PagesVector().begin(),
130 mpSdCustomShow->PagesVector().end(),
131 pPage->GetSdrPage());
132 if (it != mpSdCustomShow->PagesVector().end())
133 mpSdCustomShow->PagesVector().erase(it);
138 if( mpModel )
139 mpModel->SetModified();
142 // XIndexReplace
143 void SAL_CALL SdXCustomPresentation::replaceByIndex( sal_Int32 Index, const uno::Any& Element )
145 removeByIndex( Index );
146 insertByIndex( Index, Element );
149 // XElementAccess
150 uno::Type SAL_CALL SdXCustomPresentation::getElementType()
152 return cppu::UnoType<drawing::XDrawPage>::get();
155 sal_Bool SAL_CALL SdXCustomPresentation::hasElements()
157 SolarMutexGuard aGuard;
159 if( bDisposing )
160 throw lang::DisposedException();
162 return getCount() > 0;
165 // XIndexAccess
166 sal_Int32 SAL_CALL SdXCustomPresentation::getCount()
168 SolarMutexGuard aGuard;
169 if( bDisposing )
170 throw lang::DisposedException();
172 return mpSdCustomShow ? mpSdCustomShow->PagesVector().size() : 0;
175 uno::Any SAL_CALL SdXCustomPresentation::getByIndex( sal_Int32 Index )
177 SolarMutexGuard aGuard;
179 if( bDisposing )
180 throw lang::DisposedException();
182 if (Index < 0 || !mpSdCustomShow || o3tl::make_unsigned(Index) >= mpSdCustomShow->PagesVector().size())
183 throw lang::IndexOutOfBoundsException();
185 uno::Any aAny;
186 SdrPage * pPage = const_cast<SdPage *>(mpSdCustomShow->PagesVector()[Index]);
188 if( pPage )
190 uno::Reference< drawing::XDrawPage > xRef( pPage->getUnoPage(), uno::UNO_QUERY );
191 aAny <<= xRef;
194 return aAny;
197 // XNamed
198 OUString SAL_CALL SdXCustomPresentation::getName()
200 SolarMutexGuard aGuard;
202 if( bDisposing )
203 throw lang::DisposedException();
205 if(mpSdCustomShow)
206 return mpSdCustomShow->GetName();
208 return OUString();
211 void SAL_CALL SdXCustomPresentation::setName( const OUString& aName )
213 SolarMutexGuard aGuard;
215 if( bDisposing )
216 throw lang::DisposedException();
218 if(mpSdCustomShow)
219 mpSdCustomShow->SetName( aName );
222 // XComponent
223 void SAL_CALL SdXCustomPresentation::dispose()
225 SolarMutexGuard aGuard;
227 if( bDisposing )
228 return; // caught a recursion
230 bDisposing = true;
232 uno::Reference< uno::XInterface > xSource( static_cast<cppu::OWeakObject*>(this) );
234 std::unique_lock aGuard2(aDisposeContainerMutex);
235 lang::EventObject aEvt;
236 aEvt.Source = std::move(xSource);
237 aDisposeListeners.disposeAndClear(aGuard2, aEvt);
239 mpSdCustomShow = nullptr;
242 void SAL_CALL SdXCustomPresentation::addEventListener( const uno::Reference< lang::XEventListener >& xListener )
244 if( bDisposing )
245 throw lang::DisposedException();
247 std::unique_lock aGuard(aDisposeContainerMutex);
248 aDisposeListeners.addInterface(aGuard, xListener);
251 void SAL_CALL SdXCustomPresentation::removeEventListener( const uno::Reference< lang::XEventListener >& aListener )
253 if( !bDisposing )
255 std::unique_lock aGuard(aDisposeContainerMutex);
256 aDisposeListeners.removeInterface(aGuard, aListener);
260 /*===========================================================================*
261 * class SdXCustomPresentationAccess : public XCustomPresentationAccess, *
262 * public UsrObject *
263 *===========================================================================*/
265 SdXCustomPresentationAccess::SdXCustomPresentationAccess(SdXImpressDocument& rMyModel) noexcept
266 : mrModel(rMyModel)
270 SdXCustomPresentationAccess::~SdXCustomPresentationAccess() noexcept
274 // XServiceInfo
275 OUString SAL_CALL SdXCustomPresentationAccess::getImplementationName()
277 return u"SdXCustomPresentationAccess"_ustr;
280 sal_Bool SAL_CALL SdXCustomPresentationAccess::supportsService( const OUString& ServiceName )
282 return cppu::supportsService( this, ServiceName );
285 uno::Sequence< OUString > SAL_CALL SdXCustomPresentationAccess::getSupportedServiceNames()
287 return { u"com.sun.star.presentation.CustomPresentationAccess"_ustr };
290 // XSingleServiceFactory
291 uno::Reference< uno::XInterface > SAL_CALL SdXCustomPresentationAccess::createInstance()
293 uno::Reference< uno::XInterface > xRef( static_cast<cppu::OWeakObject*>(new SdXCustomPresentation()) );
294 return xRef;
297 uno::Reference< uno::XInterface > SAL_CALL SdXCustomPresentationAccess::createInstanceWithArguments( const uno::Sequence< uno::Any >& )
299 return createInstance();
302 // XNameContainer
303 void SAL_CALL SdXCustomPresentationAccess::insertByName( const OUString& aName, const uno::Any& aElement )
305 SolarMutexGuard aGuard;
307 // get the documents custom show list
308 SdCustomShowList* pList = nullptr;
309 if(mrModel.GetDoc())
310 pList = mrModel.GetDoc()->GetCustomShowList(true);
312 // no list, no cookies
313 if( nullptr == pList)
314 throw uno::RuntimeException();
316 // do we have a container::XIndexContainer?
317 SdXCustomPresentation* pXShow = nullptr;
319 uno::Reference< container::XIndexContainer > xContainer;
320 if( (aElement >>= xContainer) && xContainer.is() )
321 pXShow = dynamic_cast<SdXCustomPresentation*>(xContainer.get());
323 if( nullptr == pXShow )
324 throw lang::IllegalArgumentException();
326 // get the internal custom show from the api wrapper
327 SdCustomShow* pShow = pXShow->GetSdCustomShow();
328 if( nullptr == pShow )
330 pShow = new SdCustomShow( xContainer );
331 pXShow->SetSdCustomShow( pShow );
333 else
335 if( nullptr == pXShow->GetModel() || *pXShow->GetModel() != mrModel )
336 throw lang::IllegalArgumentException();
339 // give it a name
340 pShow->SetName( aName);
342 // check if this or another customshow with the same name already exists
343 for( SdCustomShow* pCompare = pList->First();
344 pCompare;
345 pCompare = pList->Next() )
347 if( pCompare == pShow || pCompare->GetName() == pShow->GetName() )
348 throw container::ElementExistException();
351 pList->push_back(std::unique_ptr<SdCustomShow>(pShow));
353 mrModel.SetModified();
356 void SAL_CALL SdXCustomPresentationAccess::removeByName( const OUString& Name )
358 SolarMutexGuard aGuard;
360 SdCustomShow* pShow = getSdCustomShow(Name);
362 SdCustomShowList* pList = GetCustomShowList();
363 if(!pList || !pShow)
364 throw container::NoSuchElementException();
366 pList->erase( pShow );
368 mrModel.SetModified();
371 // XNameReplace
372 void SAL_CALL SdXCustomPresentationAccess::replaceByName( const OUString& aName, const uno::Any& aElement )
374 removeByName( aName );
375 insertByName( aName, aElement );
378 // XNameAccess
379 uno::Any SAL_CALL SdXCustomPresentationAccess::getByName( const OUString& aName )
381 SolarMutexGuard aGuard;
383 SdCustomShow* pShow = getSdCustomShow(aName);
384 if(!pShow)
386 throw container::NoSuchElementException();
389 uno::Reference< container::XIndexContainer > xRef( pShow->getUnoCustomShow(), uno::UNO_QUERY );
390 return uno::Any(xRef);
393 uno::Sequence< OUString > SAL_CALL SdXCustomPresentationAccess::getElementNames()
395 SolarMutexGuard aGuard;
397 SdCustomShowList* pList = GetCustomShowList();
398 const sal_uInt32 nCount = pList ? pList->size() : 0;
400 uno::Sequence< OUString > aSequence( nCount );
401 OUString* pStringList = aSequence.getArray();
403 sal_uInt32 nIdx = 0;
404 while( nIdx < nCount )
406 const SdCustomShow* pShow = (*pList)[nIdx].get();
407 pStringList[nIdx] = pShow->GetName();
408 nIdx++;
411 return aSequence;
414 sal_Bool SAL_CALL SdXCustomPresentationAccess::hasByName( const OUString& aName )
416 SolarMutexGuard aGuard;
417 return getSdCustomShow(aName) != nullptr;
420 // XElementAccess
421 uno::Type SAL_CALL SdXCustomPresentationAccess::getElementType()
423 return cppu::UnoType<container::XIndexContainer>::get();
426 sal_Bool SAL_CALL SdXCustomPresentationAccess::hasElements()
428 SolarMutexGuard aGuard;
430 SdCustomShowList* pList = GetCustomShowList();
431 return pList && !pList->empty();
434 SdCustomShow * SdXCustomPresentationAccess::getSdCustomShow( std::u16string_view rName ) const noexcept
436 sal_uInt32 nIdx = 0;
438 SdCustomShowList* pList = GetCustomShowList();
439 const sal_uInt32 nCount = pList ? pList->size() : 0;
441 while( nIdx < nCount )
443 SdCustomShow* pShow = (*pList)[nIdx].get();
444 if( pShow->GetName() == rName )
445 return pShow;
446 nIdx++;
448 return nullptr;
451 /* vim:set shiftwidth=4 softtabstop=4 expandtab: */