Version 5.4.3.2, tag libreoffice-5.4.3.2
[LibreOffice.git] / toolkit / source / controls / eventcontainer.cxx
bloba64f8094b07b6721713e99dc084a92b32d1d366a
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 <osl/mutex.hxx>
21 #include <cppuhelper/queryinterface.hxx>
22 #include <cppuhelper/weak.hxx>
23 #include <cppuhelper/factory.hxx>
24 #include <cppuhelper/interfacecontainer.hxx>
26 #include "toolkit/controls/eventcontainer.hxx"
27 #include <com/sun/star/script/ScriptEventDescriptor.hpp>
30 using namespace com::sun::star::uno;
31 using namespace com::sun::star::lang;
32 using namespace com::sun::star::container;
33 using namespace com::sun::star::registry;
34 using namespace com::sun::star::script;
35 using namespace cppu;
36 using namespace osl;
37 using namespace std;
40 namespace toolkit
43 // Methods XElementAccess
44 Type ScriptEventContainer::getElementType()
46 return mType;
49 sal_Bool ScriptEventContainer::hasElements()
51 bool bRet = (mnElementCount > 0);
52 return bRet;
55 // Methods XNameAccess
56 Any ScriptEventContainer::getByName( const OUString& aName )
58 NameContainerNameMap::iterator aIt = mHashMap.find( aName );
59 if( aIt == mHashMap.end() )
61 throw NoSuchElementException();
63 sal_Int32 iHashResult = (*aIt).second;
64 Any aRetAny = mValues[ iHashResult ];
65 return aRetAny;
68 Sequence< OUString > ScriptEventContainer::getElementNames()
70 return mNames;
73 sal_Bool ScriptEventContainer::hasByName( const OUString& aName )
75 NameContainerNameMap::iterator aIt = mHashMap.find( aName );
76 bool bRet = ( aIt != mHashMap.end() );
77 return bRet;
81 // Methods XNameReplace
82 void ScriptEventContainer::replaceByName( const OUString& aName, const Any& aElement )
84 const Type& aAnyType = aElement.getValueType();
85 if( mType != aAnyType )
86 throw IllegalArgumentException();
88 NameContainerNameMap::iterator aIt = mHashMap.find( aName );
89 if( aIt == mHashMap.end() )
91 throw NoSuchElementException();
93 sal_Int32 iHashResult = (*aIt).second;
94 Any aOldElement = mValues[ iHashResult ];
95 mValues[ iHashResult ] = aElement;
97 // Fire event
98 ContainerEvent aEvent;
99 aEvent.Source = *this;
100 aEvent.Element = aElement;
101 aEvent.ReplacedElement = aOldElement;
102 aEvent.Accessor <<= aName;
103 maContainerListeners.elementReplaced( aEvent );
107 // Methods XNameContainer
108 void ScriptEventContainer::insertByName( const OUString& aName, const Any& aElement )
110 const Type& aAnyType = aElement.getValueType();
111 if( mType != aAnyType )
112 throw IllegalArgumentException();
114 NameContainerNameMap::iterator aIt = mHashMap.find( aName );
115 if( aIt != mHashMap.end() )
117 throw ElementExistException();
120 sal_Int32 nCount = mNames.getLength();
121 mNames.realloc( nCount + 1 );
122 mValues.resize( nCount + 1 );
123 mNames.getArray()[ nCount ] = aName;
124 mValues[ nCount ] = aElement;
125 mHashMap[ aName ] = nCount;
127 // Fire event
128 ContainerEvent aEvent;
129 aEvent.Source = *this;
130 aEvent.Element = aElement;
131 aEvent.Accessor <<= aName;
132 maContainerListeners.elementInserted( aEvent );
135 void ScriptEventContainer::removeByName( const OUString& Name )
137 NameContainerNameMap::iterator aIt = mHashMap.find( Name );
138 if( aIt == mHashMap.end() )
140 throw NoSuchElementException();
143 sal_Int32 iHashResult = (*aIt).second;
144 Any aOldElement = mValues[ iHashResult ];
146 // Fire event
147 ContainerEvent aEvent;
148 aEvent.Source = *this;
149 aEvent.Element = aOldElement;
150 aEvent.Accessor <<= Name;
151 maContainerListeners.elementRemoved( aEvent );
153 mHashMap.erase( aIt );
154 sal_Int32 iLast = mNames.getLength() - 1;
155 if( iLast != iHashResult )
157 OUString* pNames = mNames.getArray();
158 pNames[ iHashResult ] = pNames[ iLast ];
159 mValues[ iHashResult ] = mValues[ iLast ];
160 mHashMap[ pNames[ iHashResult ] ] = iHashResult;
162 mNames.realloc( iLast );
163 mValues.resize( iLast );
166 // Methods XContainer
167 void ScriptEventContainer::addContainerListener( const css::uno::Reference< css::container::XContainerListener >& l )
169 maContainerListeners.addInterface( l );
172 void ScriptEventContainer::removeContainerListener( const css::uno::Reference< css::container::XContainerListener >& l )
174 maContainerListeners.removeInterface( l );
178 ScriptEventContainer::ScriptEventContainer()
179 : mnElementCount( 0 ),
180 mType( cppu::UnoType<ScriptEventDescriptor>::get() ),
181 maContainerListeners( *this )
188 /* vim:set shiftwidth=4 softtabstop=4 expandtab: */