Avoid potential negative array index access to cached text.
[LibreOffice.git] / toolkit / source / controls / tabpagecontainer.cxx
blob367b5c4f22113fc3ae5c3ded224a48aa7e810656
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 .
21 #include <controls/geometrycontrolmodel.hxx>
22 #include <controls/tabpagecontainer.hxx>
23 #include <controls/tabpagemodel.hxx>
24 #include <helper/property.hxx>
26 #include <com/sun/star/awt/XControlModel.hpp>
27 #include <com/sun/star/lang/IndexOutOfBoundsException.hpp>
28 #include <com/sun/star/uno/XComponentContext.hpp>
30 #include <o3tl/safeint.hxx>
31 #include <comphelper/diagnose_ex.hxx>
32 #include <vcl/svapp.hxx>
34 #include <helper/unopropertyarrayhelper.hxx>
36 using namespace ::com::sun::star;
37 using namespace ::com::sun::star::uno;
38 using namespace ::com::sun::star::lang;
39 using namespace ::com::sun::star::beans;
40 using namespace ::com::sun::star::container;
41 using namespace ::com::sun::star::view;
42 using ::com::sun::star::awt::tab::XTabPageModel;
44 constexpr OUStringLiteral WRONG_TYPE_EXCEPTION = u"Type must be css::awt::tab::XTabPageModel!";
47 UnoControlTabPageContainerModel::UnoControlTabPageContainerModel( const Reference< XComponentContext >& i_factory )
48 :UnoControlTabPageContainerModel_Base( i_factory )
49 ,maContainerListeners( *this )
51 ImplRegisterProperty( BASEPROPERTY_BACKGROUNDCOLOR );
52 ImplRegisterProperty( BASEPROPERTY_BORDER );
53 ImplRegisterProperty( BASEPROPERTY_BORDERCOLOR );
54 ImplRegisterProperty( BASEPROPERTY_DEFAULTCONTROL );
55 ImplRegisterProperty( BASEPROPERTY_ENABLED );
56 ImplRegisterProperty( BASEPROPERTY_HELPTEXT );
57 ImplRegisterProperty( BASEPROPERTY_HELPURL );
58 ImplRegisterProperty( BASEPROPERTY_PRINTABLE );
59 ImplRegisterProperty( BASEPROPERTY_TEXT );
62 OUString UnoControlTabPageContainerModel::getServiceName()
64 return "com.sun.star.awt.tab.UnoControlTabPageContainerModel";
67 uno::Any UnoControlTabPageContainerModel::ImplGetDefaultValue( sal_uInt16 nPropId ) const
69 switch(nPropId)
71 case BASEPROPERTY_DEFAULTCONTROL:
72 return uno::Any( OUString("com.sun.star.awt.tab.UnoControlTabPageContainer") );
73 case BASEPROPERTY_BORDER:
74 return uno::Any(sal_Int16(0)); // No Border
75 default:
76 return UnoControlModel::ImplGetDefaultValue( nPropId );
80 ::cppu::IPropertyArrayHelper& UnoControlTabPageContainerModel::getInfoHelper()
82 static UnoPropertyArrayHelper aHelper( ImplGetPropertyIds() );
83 return aHelper;
85 Reference< css::beans::XPropertySetInfo > UnoControlTabPageContainerModel::getPropertySetInfo( )
87 static Reference< css::beans::XPropertySetInfo > xInfo( createPropertySetInfo( getInfoHelper() ) );
88 return xInfo;
91 namespace
93 Reference< XTabPageModel > lcl_createTabPageModel( Reference<XComponentContext> const & i_context,
94 Sequence< Any > const & i_initArguments, Reference< XPropertySet > const & i_parentModel )
96 try
98 Reference< XPropertySetInfo > const xPSI( i_parentModel->getPropertySetInfo() );
99 bool const isGeometryControlModel = xPSI.is() && xPSI->hasPropertyByName("PositionX");
101 Reference< XInterface > xInstance;
102 if ( isGeometryControlModel )
103 xInstance = *( new OGeometryControlModel< UnoControlTabPageModel >( i_context ) );
104 else
105 xInstance = *( new UnoControlTabPageModel( i_context ) );
107 Reference< XTabPageModel > const xTabPageModel( xInstance, UNO_QUERY_THROW );
108 Reference< XInitialization > const xInit( xTabPageModel, UNO_QUERY_THROW );
109 xInit->initialize( i_initArguments );
111 return xTabPageModel;
113 catch( const RuntimeException& )
115 throw;
117 catch( const Exception& )
119 DBG_UNHANDLED_EXCEPTION("toolkit.controls");
121 return nullptr;
125 Reference< XTabPageModel > SAL_CALL UnoControlTabPageContainerModel::createTabPage( ::sal_Int16 i_tabPageID )
127 Sequence< Any > aInitArgs{ Any(i_tabPageID) };
128 return lcl_createTabPageModel( m_xContext, aInitArgs, this );
131 Reference< XTabPageModel > SAL_CALL UnoControlTabPageContainerModel::loadTabPage( ::sal_Int16 i_tabPageID, const OUString& i_resourceURL )
133 Sequence< Any > aInitArgs{ Any(i_tabPageID), Any(i_resourceURL) };
134 return lcl_createTabPageModel( m_xContext, aInitArgs, this );
137 void SAL_CALL UnoControlTabPageContainerModel::insertByIndex( ::sal_Int32 nIndex, const css::uno::Any& aElement)
139 SolarMutexGuard aSolarGuard;
140 uno::Reference < XTabPageModel > xTabPageModel;
141 if(!(aElement >>= xTabPageModel))
142 throw IllegalArgumentException( WRONG_TYPE_EXCEPTION, getXWeak(), 2 );
144 if ( sal_Int32( m_aTabPageVector.size()) ==nIndex )
145 m_aTabPageVector.push_back( xTabPageModel );
146 else if ( sal_Int32( m_aTabPageVector.size()) > nIndex )
148 std::vector< uno::Reference< XTabPageModel > >::iterator aIter = m_aTabPageVector.begin();
149 aIter += nIndex;
150 m_aTabPageVector.insert( aIter, xTabPageModel );
152 else
153 throw IndexOutOfBoundsException( OUString(), getXWeak() );
154 ContainerEvent aEvent;
155 aEvent.Source = *this;
156 aEvent.Element = aElement;
157 aEvent.Accessor <<= OUString::number(nIndex);
158 maContainerListeners.elementInserted( aEvent );
162 void SAL_CALL UnoControlTabPageContainerModel::removeByIndex( ::sal_Int32 /*Index*/ )
165 // XIndexReplace
166 void SAL_CALL UnoControlTabPageContainerModel::replaceByIndex( ::sal_Int32 /*Index*/, const uno::Any& /*Element*/ )
170 // XIndexAccess
171 ::sal_Int32 SAL_CALL UnoControlTabPageContainerModel::getCount( )
173 std::unique_lock aGuard( m_aMutex );
174 return sal_Int32( m_aTabPageVector.size());
177 uno::Any SAL_CALL UnoControlTabPageContainerModel::getByIndex( ::sal_Int32 nIndex )
179 std::unique_lock aGuard( m_aMutex );
180 if ( nIndex < 0 || o3tl::make_unsigned(nIndex) > m_aTabPageVector.size() )
181 throw lang::IndexOutOfBoundsException();
182 return uno::Any(m_aTabPageVector[nIndex]);
185 // XElementAccess
186 uno::Type SAL_CALL UnoControlTabPageContainerModel::getElementType( )
188 return cppu::UnoType<css::awt::XControlModel>::get();
191 sal_Bool SAL_CALL UnoControlTabPageContainerModel::hasElements( )
193 std::unique_lock aGuard( m_aMutex );
194 return !m_aTabPageVector.empty();
196 // XContainer
197 void UnoControlTabPageContainerModel::addContainerListener( const Reference< XContainerListener >& l )
199 maContainerListeners.addInterface( l );
202 void UnoControlTabPageContainerModel::removeContainerListener( const Reference< XContainerListener >& l )
204 maContainerListeners.removeInterface( l );
209 UnoControlTabPageContainer::UnoControlTabPageContainer( const uno::Reference< uno::XComponentContext >& rxContext )
210 :UnoControlTabPageContainer_Base(rxContext)
211 ,m_aTabPageListeners( *this )
215 OUString UnoControlTabPageContainer::GetComponentServiceName() const
217 return "TabPageContainer";
220 void SAL_CALL UnoControlTabPageContainer::dispose( )
222 lang::EventObject aEvt;
223 aEvt.Source = getXWeak();
224 m_aTabPageListeners.disposeAndClear( aEvt );
225 UnoControl::dispose();
228 void UnoControlTabPageContainer::createPeer( const uno::Reference< awt::XToolkit > & rxToolkit, const uno::Reference< awt::XWindowPeer > & rParentPeer )
230 UnoControlBase::createPeer( rxToolkit, rParentPeer );
232 Reference< XTabPageContainer > xTPContainer( getPeer(), UNO_QUERY_THROW );
233 if ( m_aTabPageListeners.getLength() )
234 xTPContainer->addTabPageContainerListener(&m_aTabPageListeners);
238 // XTabPageContainer
240 ::sal_Int16 SAL_CALL UnoControlTabPageContainer::getActiveTabPageID()
242 SolarMutexGuard aSolarGuard;
243 Reference< XTabPageContainer > xTPContainer( getPeer(), UNO_QUERY_THROW );
244 return xTPContainer->getActiveTabPageID();
246 void SAL_CALL UnoControlTabPageContainer::setActiveTabPageID( ::sal_Int16 _activetabpageid )
248 SolarMutexGuard aSolarGuard;
249 Reference< XTabPageContainer > xTPContainer( getPeer(), UNO_QUERY_THROW );
250 xTPContainer->setActiveTabPageID(_activetabpageid);
252 ::sal_Int16 SAL_CALL UnoControlTabPageContainer::getTabPageCount( )
254 SolarMutexGuard aSolarGuard;
255 Reference< XTabPageContainer > xTPContainer( getPeer(), UNO_QUERY_THROW );
256 return xTPContainer->getTabPageCount();
258 sal_Bool SAL_CALL UnoControlTabPageContainer::isTabPageActive( ::sal_Int16 tabPageIndex )
260 SolarMutexGuard aSolarGuard;
261 Reference< XTabPageContainer > xTPContainer( getPeer(), UNO_QUERY_THROW );
262 return xTPContainer->isTabPageActive(tabPageIndex);
264 Reference< css::awt::tab::XTabPage > SAL_CALL UnoControlTabPageContainer::getTabPage( ::sal_Int16 tabPageIndex )
266 SolarMutexGuard aSolarGuard;
267 Reference< XTabPageContainer > xTPContainer( getPeer(), UNO_QUERY_THROW );
268 return xTPContainer->getTabPage(tabPageIndex);
270 Reference< css::awt::tab::XTabPage > SAL_CALL UnoControlTabPageContainer::getTabPageByID( ::sal_Int16 tabPageID )
272 SolarMutexGuard aSolarGuard;
273 Reference< XTabPageContainer > xTPContainer( getPeer(), UNO_QUERY_THROW );
274 return xTPContainer->getTabPageByID(tabPageID);
276 void SAL_CALL UnoControlTabPageContainer::addTabPageContainerListener( const Reference< css::awt::tab::XTabPageContainerListener >& listener )
278 m_aTabPageListeners.addInterface( listener );
279 if( getPeer().is() && m_aTabPageListeners.getLength() == 1 )
281 uno::Reference < awt::tab::XTabPageContainer > xTabPageContainer( getPeer(), uno::UNO_QUERY );
282 xTabPageContainer->addTabPageContainerListener( &m_aTabPageListeners );
285 void SAL_CALL UnoControlTabPageContainer::removeTabPageContainerListener( const Reference< css::awt::tab::XTabPageContainerListener >& listener )
287 if( getPeer().is() && m_aTabPageListeners.getLength() == 1 )
289 uno::Reference < awt::tab::XTabPageContainer > xTabPageContainer( getPeer(), uno::UNO_QUERY );
290 xTabPageContainer->removeTabPageContainerListener( &m_aTabPageListeners );
292 m_aTabPageListeners.removeInterface( listener );
295 void UnoControlTabPageContainer::propertiesChange(const::css::uno::Sequence<PropertyChangeEvent> &aEvent)
297 UnoControlTabPageContainer_Base::propertiesChange(aEvent);
299 SolarMutexGuard aSolarGuard;
300 Reference< XPropertiesChangeListener > xPropertiesChangeListener( getPeer(), UNO_QUERY_THROW );
301 return xPropertiesChangeListener->propertiesChange(aEvent);
304 void UnoControlTabPageContainer::updateFromModel()
306 UnoControlTabPageContainer_Base::updateFromModel();
307 if (!getPeer().is())
308 throw RuntimeException("No peer for tabpage container!");
309 Reference< XContainerListener > xContainerListener( getPeer(), UNO_QUERY );
310 ENSURE_OR_RETURN_VOID( xContainerListener.is(), "UnoListBoxControl::updateFromModel: a peer which is no ItemListListener?!" );
312 ContainerEvent aEvent;
313 aEvent.Source = getModel();
314 const Sequence< Reference< XControl > > aControls = getControls();
316 for ( const Reference< XControl >& rCtrl : aControls )
318 aEvent.Element <<= rCtrl;
319 xContainerListener->elementInserted( aEvent );
322 void SAL_CALL UnoControlTabPageContainer::addControl( const OUString& Name, const Reference< css::awt::XControl >& Control )
324 SolarMutexGuard aSolarGuard;
325 ControlContainerBase::addControl(Name,Control);
326 if (!getPeer().is())
327 throw RuntimeException("No peer for tabpage container!");
328 Reference< XContainerListener > xContainerListener( getPeer(), UNO_QUERY );
329 ContainerEvent aEvent;
330 aEvent.Source = getModel();
331 aEvent.Element <<= Control;
332 xContainerListener->elementInserted( aEvent );
335 extern "C" SAL_DLLPUBLIC_EXPORT css::uno::XInterface *
336 stardiv_Toolkit_UnoControlTabPageContainerModel_get_implementation(
337 css::uno::XComponentContext *context,
338 css::uno::Sequence<css::uno::Any> const &)
340 return cppu::acquire(new UnoControlTabPageContainerModel(context));
343 extern "C" SAL_DLLPUBLIC_EXPORT css::uno::XInterface *
344 stardiv_Toolkit_UnoControlTabPageContainer_get_implementation(
345 css::uno::XComponentContext *context,
346 css::uno::Sequence<css::uno::Any> const &)
348 return cppu::acquire(new UnoControlTabPageContainer(context));
351 /* vim:set shiftwidth=4 softtabstop=4 expandtab: */