Bump version to 4.3-4
[LibreOffice.git] / sc / source / ui / unoobj / eventuno.cxx
blob969f680f955d54500a14dd5020cf447352ff8b5a
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 "eventuno.hxx"
22 #include "miscuno.hxx"
23 #include "docsh.hxx"
24 #include "sheetevents.hxx"
25 #include "unonames.hxx"
26 #include <vcl/svapp.hxx>
28 using namespace ::com::sun::star;
30 SC_SIMPLE_SERVICE_INFO( ScSheetEventsObj, "ScSheetEventsObj", "com.sun.star.document.Events" )
32 ScSheetEventsObj::ScSheetEventsObj(ScDocShell* pDocSh, SCTAB nT) :
33 mpDocShell( pDocSh ),
34 mnTab( nT )
36 mpDocShell->GetDocument()->AddUnoObject(*this);
39 ScSheetEventsObj::~ScSheetEventsObj()
41 if (mpDocShell)
42 mpDocShell->GetDocument()->RemoveUnoObject(*this);
45 void ScSheetEventsObj::Notify( SfxBroadcaster&, const SfxHint& rHint )
47 //! reference update
48 if ( rHint.ISA( SfxSimpleHint ) &&
49 ((const SfxSimpleHint&)rHint).GetId() == SFX_HINT_DYING )
51 mpDocShell = NULL;
55 static sal_Int32 lcl_GetEventFromName( const OUString& aName )
57 for (sal_Int32 nEvent=0; nEvent<SC_SHEETEVENT_COUNT; ++nEvent)
58 if ( aName == ScSheetEvents::GetEventName(nEvent) )
59 return nEvent;
61 return -1; // not found
64 // XNameReplace
66 void SAL_CALL ScSheetEventsObj::replaceByName( const OUString& aName, const uno::Any& aElement )
67 throw(lang::IllegalArgumentException, container::NoSuchElementException,
68 lang::WrappedTargetException, uno::RuntimeException, std::exception)
70 SolarMutexGuard aGuard;
71 if (!mpDocShell)
72 throw uno::RuntimeException();
74 sal_Int32 nEvent = lcl_GetEventFromName(aName);
75 if (nEvent < 0)
76 throw container::NoSuchElementException();
78 ScSheetEvents aNewEvents;
79 const ScSheetEvents* pOldEvents = mpDocShell->GetDocument()->GetSheetEvents(mnTab);
80 if (pOldEvents)
81 aNewEvents = *pOldEvents;
83 OUString aScript;
84 if ( aElement.hasValue() ) // empty Any -> reset event
86 uno::Sequence<beans::PropertyValue> aPropSeq;
87 if ( aElement >>= aPropSeq )
89 sal_Int32 nPropCount = aPropSeq.getLength();
90 for (sal_Int32 nPos=0; nPos<nPropCount; ++nPos)
92 const beans::PropertyValue& rProp = aPropSeq[nPos];
93 if ( rProp.Name.equalsAscii( SC_UNO_EVENTTYPE ) )
95 OUString aEventType;
96 if ( rProp.Value >>= aEventType )
98 // only "Script" is supported
99 if ( aEventType.compareToAscii( SC_UNO_SCRIPT ) != 0 )
100 throw lang::IllegalArgumentException();
103 else if ( rProp.Name.equalsAscii( SC_UNO_SCRIPT ) )
104 rProp.Value >>= aScript;
108 if (!aScript.isEmpty())
109 aNewEvents.SetScript( nEvent, &aScript );
110 else
111 aNewEvents.SetScript( nEvent, NULL ); // reset
113 mpDocShell->GetDocument()->SetSheetEvents( mnTab, &aNewEvents );
114 mpDocShell->SetDocumentModified();
117 // XNameAccess
119 uno::Any SAL_CALL ScSheetEventsObj::getByName( const OUString& aName )
120 throw(container::NoSuchElementException, lang::WrappedTargetException, uno::RuntimeException, std::exception)
122 SolarMutexGuard aGuard;
123 sal_Int32 nEvent = lcl_GetEventFromName(aName);
124 if (nEvent < 0)
125 throw container::NoSuchElementException();
127 const OUString* pScript = NULL;
128 if (mpDocShell)
130 const ScSheetEvents* pEvents = mpDocShell->GetDocument()->GetSheetEvents(mnTab);
131 if (pEvents)
132 pScript = pEvents->GetScript(nEvent);
135 uno::Any aRet;
136 if (pScript)
138 uno::Sequence<beans::PropertyValue> aPropSeq( 2 );
139 aPropSeq[0] = beans::PropertyValue(
140 OUString("EventType"), -1,
141 uno::makeAny( OUString("Script") ), beans::PropertyState_DIRECT_VALUE );
142 aPropSeq[1] = beans::PropertyValue(
143 OUString("Script"), -1,
144 uno::makeAny( *pScript ), beans::PropertyState_DIRECT_VALUE );
145 aRet <<= aPropSeq;
147 // empty Any if nothing was set
148 return aRet;
151 uno::Sequence<OUString> SAL_CALL ScSheetEventsObj::getElementNames() throw(uno::RuntimeException, std::exception)
153 SolarMutexGuard aGuard;
154 uno::Sequence<OUString> aNames(SC_SHEETEVENT_COUNT);
155 for (sal_Int32 nEvent=0; nEvent<SC_SHEETEVENT_COUNT; ++nEvent)
156 aNames[nEvent] = ScSheetEvents::GetEventName(nEvent);
157 return aNames;
160 sal_Bool SAL_CALL ScSheetEventsObj::hasByName( const OUString& aName ) throw(uno::RuntimeException, std::exception)
162 SolarMutexGuard aGuard;
163 sal_Int32 nEvent = lcl_GetEventFromName(aName);
164 return (nEvent >= 0);
167 // XElementAccess
169 uno::Type SAL_CALL ScSheetEventsObj::getElementType() throw(uno::RuntimeException, std::exception)
171 SolarMutexGuard aGuard;
172 return getCppuType((uno::Sequence<beans::PropertyValue>*)0);
175 sal_Bool SAL_CALL ScSheetEventsObj::hasElements() throw(uno::RuntimeException, std::exception)
177 SolarMutexGuard aGuard;
178 if (mpDocShell)
179 return sal_True;
180 return false;
183 /* vim:set shiftwidth=4 softtabstop=4 expandtab: */