tdf#130857 qt weld: Implement QtInstanceWidget::strip_mnemonic
[LibreOffice.git] / sc / source / ui / unoobj / miscuno.cxx
blobc0eba32bcd21221587d64b5039573eb34815bb8d
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 <sal/config.h>
22 #include <com/sun/star/lang/IndexOutOfBoundsException.hpp>
23 #include <cppuhelper/supportsservice.hxx>
24 #include <o3tl/any.hxx>
25 #include <utility>
26 #include <vcl/svapp.hxx>
28 #include <miscuno.hxx>
30 using namespace com::sun::star;
31 using ::com::sun::star::uno::Reference;
32 using ::com::sun::star::uno::Any;
34 SC_SIMPLE_SERVICE_INFO( ScNameToIndexAccess, u"ScNameToIndexAccess"_ustr, u"stardiv.unknown"_ustr )
36 bool ScUnoHelpFunctions::GetBoolProperty( const uno::Reference<beans::XPropertySet>& xProp,
37 const OUString& rName, bool bDefault )
39 bool bRet = bDefault;
40 if ( xProp.is() )
42 try
44 xProp->getPropertyValue( rName ) >>= bRet;
46 catch(uno::Exception&)
48 // keep default
51 return bRet;
54 sal_Int16 ScUnoHelpFunctions::GetShortProperty( const css::uno::Reference< css::beans::XPropertySet>& xProp,
55 const OUString& rName, sal_Int16 nDefault )
57 sal_Int16 nRet = nDefault;
58 if ( xProp.is() )
60 try
62 xProp->getPropertyValue( rName ) >>= nRet;
64 catch(uno::Exception&)
66 // keep default
69 return nRet;
72 sal_Int32 ScUnoHelpFunctions::GetLongProperty( const uno::Reference<beans::XPropertySet>& xProp,
73 const OUString& rName )
75 sal_Int32 nRet = 0;
76 if ( xProp.is() )
78 try
80 //! type conversion???
81 xProp->getPropertyValue( rName ) >>= nRet;
83 catch(uno::Exception&)
85 // keep default
88 return nRet;
91 sal_Int32 ScUnoHelpFunctions::GetEnumPropertyImpl( const uno::Reference<beans::XPropertySet>& xProp,
92 const OUString& rName, sal_Int32 nDefault )
94 sal_Int32 nRet = nDefault;
95 if ( xProp.is() )
97 try
99 uno::Any aAny(xProp->getPropertyValue( rName ));
101 if ( aAny.getValueTypeClass() == uno::TypeClass_ENUM )
103 //! get enum value from any???
104 nRet = *static_cast<sal_Int32 const *>(aAny.getValue());
106 else
108 //! type conversion???
109 aAny >>= nRet;
112 catch(uno::Exception&)
114 // keep default
117 return nRet;
120 OUString ScUnoHelpFunctions::GetStringProperty(
121 const Reference<beans::XPropertySet>& xProp, const OUString& rName, const OUString& rDefault )
123 OUString aRet = rDefault;
124 if (!xProp.is())
125 return aRet;
129 Any any = xProp->getPropertyValue(rName);
130 any >>= aRet;
132 catch (const uno::Exception&)
136 return aRet;
139 bool ScUnoHelpFunctions::GetBoolFromAny( const uno::Any& aAny )
141 auto b = o3tl::tryAccess<bool>(aAny);
142 return b.has_value() && *b;
145 sal_Int16 ScUnoHelpFunctions::GetInt16FromAny( const uno::Any& aAny )
147 sal_Int16 nRet = 0;
148 if ( aAny >>= nRet )
149 return nRet;
150 return 0;
153 sal_Int32 ScUnoHelpFunctions::GetInt32FromAny( const uno::Any& aAny )
155 sal_Int32 nRet = 0;
156 if ( aAny >>= nRet )
157 return nRet;
158 return 0;
161 sal_Int32 ScUnoHelpFunctions::GetEnumFromAny( const uno::Any& aAny )
163 sal_Int32 nRet = 0;
164 if ( aAny.getValueTypeClass() == uno::TypeClass_ENUM )
165 nRet = *static_cast<sal_Int32 const *>(aAny.getValue());
166 else
167 aAny >>= nRet;
168 return nRet;
171 void ScUnoHelpFunctions::SetOptionalPropertyValue(
172 const Reference<beans::XPropertySet>& rPropSet, const char* pPropName, const Any& rVal )
174 SetOptionalPropertyValue(rPropSet, OUString::createFromAscii(pPropName), rVal);
177 void ScUnoHelpFunctions::SetOptionalPropertyValue(
178 const Reference<beans::XPropertySet>& rPropSet, const OUString& sPropName, const Any& rVal )
182 rPropSet->setPropertyValue(sPropName, rVal);
184 catch (const beans::UnknownPropertyException&)
186 // ignored - not supported.
190 ScIndexEnumeration::ScIndexEnumeration(uno::Reference<container::XIndexAccess> xInd,
191 OUString aServiceName) :
192 xIndex(std::move( xInd )),
193 sServiceName(std::move(aServiceName)),
194 nPos( 0 )
198 ScIndexEnumeration::~ScIndexEnumeration()
202 // XEnumeration
204 sal_Bool SAL_CALL ScIndexEnumeration::hasMoreElements()
206 SolarMutexGuard aGuard;
207 return ( nPos < xIndex->getCount() );
210 uno::Any SAL_CALL ScIndexEnumeration::nextElement()
212 SolarMutexGuard aGuard;
213 uno::Any aReturn;
216 aReturn = xIndex->getByIndex(nPos++);
218 catch (lang::IndexOutOfBoundsException&)
220 throw container::NoSuchElementException();
222 return aReturn;
225 OUString SAL_CALL ScIndexEnumeration::getImplementationName()
227 return u"ScIndexEnumeration"_ustr;
230 sal_Bool SAL_CALL ScIndexEnumeration::supportsService( const OUString& ServiceName )
232 return cppu::supportsService(this, ServiceName);
235 css::uno::Sequence< OUString >
236 SAL_CALL ScIndexEnumeration::getSupportedServiceNames()
238 return { sServiceName };
241 ScNameToIndexAccess::ScNameToIndexAccess( css::uno::Reference<
242 css::container::XNameAccess> xNameObj ) :
243 xNameAccess(std::move( xNameObj ))
245 //! test for XIndexAccess interface at rNameObj, use that instead!
247 if ( xNameAccess.is() )
248 aNames = xNameAccess->getElementNames();
251 ScNameToIndexAccess::~ScNameToIndexAccess()
255 // XIndexAccess
257 sal_Int32 SAL_CALL ScNameToIndexAccess::getCount( )
259 return aNames.getLength();
262 css::uno::Any SAL_CALL ScNameToIndexAccess::getByIndex( sal_Int32 nIndex )
264 if ( xNameAccess.is() && nIndex >= 0 && nIndex < aNames.getLength() )
265 return xNameAccess->getByName( aNames.getConstArray()[nIndex] );
267 throw lang::IndexOutOfBoundsException();
270 // XElementAccess
272 css::uno::Type SAL_CALL ScNameToIndexAccess::getElementType( )
274 if ( xNameAccess.is() )
275 return xNameAccess->getElementType();
276 else
277 return uno::Type();
280 sal_Bool SAL_CALL ScNameToIndexAccess::hasElements( )
282 return getCount() > 0;
285 /* vim:set shiftwidth=4 softtabstop=4 expandtab: */