Version 6.1.4.1, tag libreoffice-6.1.4.1
[LibreOffice.git] / toolkit / source / controls / eventcontainer.cxx
blob8873c638ad0a9773351c738b50c752529d704c96
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 <cppuhelper/queryinterface.hxx>
21 #include <cppuhelper/weak.hxx>
22 #include <cppuhelper/factory.hxx>
23 #include <cppuhelper/interfacecontainer.hxx>
25 #include <toolkit/controls/eventcontainer.hxx>
26 #include <com/sun/star/script/ScriptEventDescriptor.hpp>
29 using namespace com::sun::star::uno;
30 using namespace com::sun::star::lang;
31 using namespace com::sun::star::container;
32 using namespace com::sun::star::registry;
33 using namespace com::sun::star::script;
34 using namespace cppu;
35 using namespace std;
38 namespace toolkit
41 // Methods XElementAccess
42 Type ScriptEventContainer::getElementType()
44 return mType;
47 sal_Bool ScriptEventContainer::hasElements()
49 return !mHashMap.empty();
52 // Methods XNameAccess
53 Any ScriptEventContainer::getByName( const OUString& aName )
55 auto aIt = mHashMap.find( aName );
56 if( aIt == mHashMap.end() )
58 throw NoSuchElementException();
60 return aIt->second;
63 Sequence< OUString > ScriptEventContainer::getElementNames()
65 Sequence<OUString> aRet(mHashMap.size());
66 int i = 0;
67 for (auto const & pair : mHashMap)
68 aRet[i++] = pair.first;
69 return aRet;
72 sal_Bool ScriptEventContainer::hasByName( const OUString& aName )
74 auto aIt = mHashMap.find( aName );
75 return aIt != mHashMap.end();
79 // Methods XNameReplace
80 void ScriptEventContainer::replaceByName( const OUString& aName, const Any& aElement )
82 const Type& aAnyType = aElement.getValueType();
83 if( mType != aAnyType )
84 throw IllegalArgumentException();
86 auto aIt = mHashMap.find( aName );
87 if( aIt == mHashMap.end() )
89 throw NoSuchElementException();
91 Any aOldElement = aIt->second;
92 aIt->second = aElement;
94 // Fire event
95 ContainerEvent aEvent;
96 aEvent.Source = *this;
97 aEvent.Element = aElement;
98 aEvent.ReplacedElement = aOldElement;
99 aEvent.Accessor <<= aName;
100 maContainerListeners.elementReplaced( aEvent );
104 // Methods XNameContainer
105 void ScriptEventContainer::insertByName( const OUString& aName, const Any& aElement )
107 const Type& aAnyType = aElement.getValueType();
108 if( mType != aAnyType )
109 throw IllegalArgumentException();
111 auto aIt = mHashMap.find( aName );
112 if( aIt != mHashMap.end() )
114 throw ElementExistException();
117 mHashMap[ aName ] = aElement;
119 // Fire event
120 ContainerEvent aEvent;
121 aEvent.Source = *this;
122 aEvent.Element = aElement;
123 aEvent.Accessor <<= aName;
124 maContainerListeners.elementInserted( aEvent );
127 void ScriptEventContainer::removeByName( const OUString& Name )
129 auto aIt = mHashMap.find( Name );
130 if( aIt == mHashMap.end() )
132 throw NoSuchElementException();
135 // Fire event
136 ContainerEvent aEvent;
137 aEvent.Source = *this;
138 aEvent.Element = aIt->second;
139 aEvent.Accessor <<= Name;
140 maContainerListeners.elementRemoved( aEvent );
142 mHashMap.erase( aIt );
145 // Methods XContainer
146 void ScriptEventContainer::addContainerListener( const css::uno::Reference< css::container::XContainerListener >& l )
148 maContainerListeners.addInterface( l );
151 void ScriptEventContainer::removeContainerListener( const css::uno::Reference< css::container::XContainerListener >& l )
153 maContainerListeners.removeInterface( l );
157 ScriptEventContainer::ScriptEventContainer()
158 : mType( cppu::UnoType<ScriptEventDescriptor>::get() ),
159 maContainerListeners( *this )
166 /* vim:set shiftwidth=4 softtabstop=4 expandtab: */