1*b3f79822SAndrew Rist /**************************************************************
2*b3f79822SAndrew Rist *
3*b3f79822SAndrew Rist * Licensed to the Apache Software Foundation (ASF) under one
4*b3f79822SAndrew Rist * or more contributor license agreements. See the NOTICE file
5*b3f79822SAndrew Rist * distributed with this work for additional information
6*b3f79822SAndrew Rist * regarding copyright ownership. The ASF licenses this file
7*b3f79822SAndrew Rist * to you under the Apache License, Version 2.0 (the
8*b3f79822SAndrew Rist * "License"); you may not use this file except in compliance
9*b3f79822SAndrew Rist * with the License. You may obtain a copy of the License at
10*b3f79822SAndrew Rist *
11*b3f79822SAndrew Rist * http://www.apache.org/licenses/LICENSE-2.0
12*b3f79822SAndrew Rist *
13*b3f79822SAndrew Rist * Unless required by applicable law or agreed to in writing,
14*b3f79822SAndrew Rist * software distributed under the License is distributed on an
15*b3f79822SAndrew Rist * "AS IS" BASIS, WITHOUT WARRANTIES OR CONDITIONS OF ANY
16*b3f79822SAndrew Rist * KIND, either express or implied. See the License for the
17*b3f79822SAndrew Rist * specific language governing permissions and limitations
18*b3f79822SAndrew Rist * under the License.
19*b3f79822SAndrew Rist *
20*b3f79822SAndrew Rist *************************************************************/
21*b3f79822SAndrew Rist
22*b3f79822SAndrew Rist
23cdf0e10cSrcweir
24cdf0e10cSrcweir // MARKER(update_precomp.py): autogen include statement, do not remove
25cdf0e10cSrcweir #include "precompiled_sc.hxx"
26cdf0e10cSrcweir
27cdf0e10cSrcweir #include "eventuno.hxx"
28cdf0e10cSrcweir #include "miscuno.hxx"
29cdf0e10cSrcweir #include "unoguard.hxx"
30cdf0e10cSrcweir #include "docsh.hxx"
31cdf0e10cSrcweir #include "sheetevents.hxx"
32cdf0e10cSrcweir #include "unonames.hxx"
33cdf0e10cSrcweir
34cdf0e10cSrcweir using namespace ::com::sun::star;
35cdf0e10cSrcweir
36cdf0e10cSrcweir //------------------------------------------------------------------------
37cdf0e10cSrcweir
38cdf0e10cSrcweir SC_SIMPLE_SERVICE_INFO( ScSheetEventsObj, "ScSheetEventsObj", "com.sun.star.document.Events" )
39cdf0e10cSrcweir
40cdf0e10cSrcweir //------------------------------------------------------------------------
41cdf0e10cSrcweir
ScSheetEventsObj(ScDocShell * pDocSh,SCTAB nT)42cdf0e10cSrcweir ScSheetEventsObj::ScSheetEventsObj(ScDocShell* pDocSh, SCTAB nT) :
43cdf0e10cSrcweir mpDocShell( pDocSh ),
44cdf0e10cSrcweir mnTab( nT )
45cdf0e10cSrcweir {
46cdf0e10cSrcweir mpDocShell->GetDocument()->AddUnoObject(*this);
47cdf0e10cSrcweir }
48cdf0e10cSrcweir
~ScSheetEventsObj()49cdf0e10cSrcweir ScSheetEventsObj::~ScSheetEventsObj()
50cdf0e10cSrcweir {
51cdf0e10cSrcweir if (mpDocShell)
52cdf0e10cSrcweir mpDocShell->GetDocument()->RemoveUnoObject(*this);
53cdf0e10cSrcweir }
54cdf0e10cSrcweir
Notify(SfxBroadcaster &,const SfxHint & rHint)55cdf0e10cSrcweir void ScSheetEventsObj::Notify( SfxBroadcaster&, const SfxHint& rHint )
56cdf0e10cSrcweir {
57cdf0e10cSrcweir //! reference update
58cdf0e10cSrcweir if ( rHint.ISA( SfxSimpleHint ) &&
59cdf0e10cSrcweir ((const SfxSimpleHint&)rHint).GetId() == SFX_HINT_DYING )
60cdf0e10cSrcweir {
61cdf0e10cSrcweir mpDocShell = NULL;
62cdf0e10cSrcweir }
63cdf0e10cSrcweir }
64cdf0e10cSrcweir
lcl_GetEventFromName(const rtl::OUString & aName)65cdf0e10cSrcweir sal_Int32 lcl_GetEventFromName( const rtl::OUString& aName )
66cdf0e10cSrcweir {
67cdf0e10cSrcweir for (sal_Int32 nEvent=0; nEvent<SC_SHEETEVENT_COUNT; ++nEvent)
68cdf0e10cSrcweir if ( aName == ScSheetEvents::GetEventName(nEvent) )
69cdf0e10cSrcweir return nEvent;
70cdf0e10cSrcweir
71cdf0e10cSrcweir return -1; // not found
72cdf0e10cSrcweir }
73cdf0e10cSrcweir
74cdf0e10cSrcweir // XNameReplace
75cdf0e10cSrcweir
replaceByName(const rtl::OUString & aName,const uno::Any & aElement)76cdf0e10cSrcweir void SAL_CALL ScSheetEventsObj::replaceByName( const rtl::OUString& aName, const uno::Any& aElement )
77cdf0e10cSrcweir throw(lang::IllegalArgumentException, container::NoSuchElementException,
78cdf0e10cSrcweir lang::WrappedTargetException, uno::RuntimeException)
79cdf0e10cSrcweir {
80cdf0e10cSrcweir ScUnoGuard aGuard;
81cdf0e10cSrcweir if (!mpDocShell)
82cdf0e10cSrcweir throw uno::RuntimeException();
83cdf0e10cSrcweir
84cdf0e10cSrcweir sal_Int32 nEvent = lcl_GetEventFromName(aName);
85cdf0e10cSrcweir if (nEvent < 0)
86cdf0e10cSrcweir throw container::NoSuchElementException();
87cdf0e10cSrcweir
88cdf0e10cSrcweir ScSheetEvents aNewEvents;
89cdf0e10cSrcweir const ScSheetEvents* pOldEvents = mpDocShell->GetDocument()->GetSheetEvents(mnTab);
90cdf0e10cSrcweir if (pOldEvents)
91cdf0e10cSrcweir aNewEvents = *pOldEvents;
92cdf0e10cSrcweir
93cdf0e10cSrcweir rtl::OUString aScript;
94cdf0e10cSrcweir if ( aElement.hasValue() ) // empty Any -> reset event
95cdf0e10cSrcweir {
96cdf0e10cSrcweir uno::Sequence<beans::PropertyValue> aPropSeq;
97cdf0e10cSrcweir if ( aElement >>= aPropSeq )
98cdf0e10cSrcweir {
99cdf0e10cSrcweir sal_Int32 nPropCount = aPropSeq.getLength();
100cdf0e10cSrcweir for (sal_Int32 nPos=0; nPos<nPropCount; ++nPos)
101cdf0e10cSrcweir {
102cdf0e10cSrcweir const beans::PropertyValue& rProp = aPropSeq[nPos];
103cdf0e10cSrcweir if ( rProp.Name.compareToAscii( SC_UNO_EVENTTYPE ) == 0 )
104cdf0e10cSrcweir {
105cdf0e10cSrcweir rtl::OUString aEventType;
106cdf0e10cSrcweir if ( rProp.Value >>= aEventType )
107cdf0e10cSrcweir {
108cdf0e10cSrcweir // only "Script" is supported
109cdf0e10cSrcweir if ( aEventType.compareToAscii( SC_UNO_SCRIPT ) != 0 )
110cdf0e10cSrcweir throw lang::IllegalArgumentException();
111cdf0e10cSrcweir }
112cdf0e10cSrcweir }
113cdf0e10cSrcweir else if ( rProp.Name.compareToAscii( SC_UNO_SCRIPT ) == 0 )
114cdf0e10cSrcweir rProp.Value >>= aScript;
115cdf0e10cSrcweir }
116cdf0e10cSrcweir }
117cdf0e10cSrcweir }
118cdf0e10cSrcweir if (aScript.getLength())
119cdf0e10cSrcweir aNewEvents.SetScript( nEvent, &aScript );
120cdf0e10cSrcweir else
121cdf0e10cSrcweir aNewEvents.SetScript( nEvent, NULL ); // reset
122cdf0e10cSrcweir
123cdf0e10cSrcweir mpDocShell->GetDocument()->SetSheetEvents( mnTab, &aNewEvents );
124cdf0e10cSrcweir mpDocShell->SetDocumentModified();
125cdf0e10cSrcweir }
126cdf0e10cSrcweir
127cdf0e10cSrcweir // XNameAccess
128cdf0e10cSrcweir
getByName(const rtl::OUString & aName)129cdf0e10cSrcweir uno::Any SAL_CALL ScSheetEventsObj::getByName( const rtl::OUString& aName )
130cdf0e10cSrcweir throw(container::NoSuchElementException, lang::WrappedTargetException, uno::RuntimeException)
131cdf0e10cSrcweir {
132cdf0e10cSrcweir ScUnoGuard aGuard;
133cdf0e10cSrcweir sal_Int32 nEvent = lcl_GetEventFromName(aName);
134cdf0e10cSrcweir if (nEvent < 0)
135cdf0e10cSrcweir throw container::NoSuchElementException();
136cdf0e10cSrcweir
137cdf0e10cSrcweir const rtl::OUString* pScript = NULL;
138cdf0e10cSrcweir if (mpDocShell)
139cdf0e10cSrcweir {
140cdf0e10cSrcweir const ScSheetEvents* pEvents = mpDocShell->GetDocument()->GetSheetEvents(mnTab);
141cdf0e10cSrcweir if (pEvents)
142cdf0e10cSrcweir pScript = pEvents->GetScript(nEvent);
143cdf0e10cSrcweir }
144cdf0e10cSrcweir
145cdf0e10cSrcweir uno::Any aRet;
146cdf0e10cSrcweir if (pScript)
147cdf0e10cSrcweir {
148cdf0e10cSrcweir uno::Sequence<beans::PropertyValue> aPropSeq( 2 );
149cdf0e10cSrcweir aPropSeq[0] = beans::PropertyValue(
150cdf0e10cSrcweir rtl::OUString::createFromAscii("EventType"), -1,
151cdf0e10cSrcweir uno::makeAny( rtl::OUString::createFromAscii("Script") ), beans::PropertyState_DIRECT_VALUE );
152cdf0e10cSrcweir aPropSeq[1] = beans::PropertyValue(
153cdf0e10cSrcweir rtl::OUString::createFromAscii("Script"), -1,
154cdf0e10cSrcweir uno::makeAny( *pScript ), beans::PropertyState_DIRECT_VALUE );
155cdf0e10cSrcweir aRet <<= aPropSeq;
156cdf0e10cSrcweir }
157cdf0e10cSrcweir // empty Any if nothing was set
158cdf0e10cSrcweir return aRet;
159cdf0e10cSrcweir }
160cdf0e10cSrcweir
getElementNames()161cdf0e10cSrcweir uno::Sequence<rtl::OUString> SAL_CALL ScSheetEventsObj::getElementNames() throw(uno::RuntimeException)
162cdf0e10cSrcweir {
163cdf0e10cSrcweir ScUnoGuard aGuard;
164cdf0e10cSrcweir uno::Sequence<rtl::OUString> aNames(SC_SHEETEVENT_COUNT);
165cdf0e10cSrcweir for (sal_Int32 nEvent=0; nEvent<SC_SHEETEVENT_COUNT; ++nEvent)
166cdf0e10cSrcweir aNames[nEvent] = ScSheetEvents::GetEventName(nEvent);
167cdf0e10cSrcweir return aNames;
168cdf0e10cSrcweir }
169cdf0e10cSrcweir
hasByName(const::rtl::OUString & aName)170cdf0e10cSrcweir sal_Bool SAL_CALL ScSheetEventsObj::hasByName( const ::rtl::OUString& aName ) throw(uno::RuntimeException)
171cdf0e10cSrcweir {
172cdf0e10cSrcweir ScUnoGuard aGuard;
173cdf0e10cSrcweir sal_Int32 nEvent = lcl_GetEventFromName(aName);
174cdf0e10cSrcweir return (nEvent >= 0);
175cdf0e10cSrcweir }
176cdf0e10cSrcweir
177cdf0e10cSrcweir // XElementAccess
178cdf0e10cSrcweir
getElementType()179cdf0e10cSrcweir uno::Type SAL_CALL ScSheetEventsObj::getElementType() throw(uno::RuntimeException)
180cdf0e10cSrcweir {
181cdf0e10cSrcweir ScUnoGuard aGuard;
182cdf0e10cSrcweir return getCppuType((uno::Sequence<beans::PropertyValue>*)0);
183cdf0e10cSrcweir }
184cdf0e10cSrcweir
hasElements()185cdf0e10cSrcweir sal_Bool SAL_CALL ScSheetEventsObj::hasElements() throw(uno::RuntimeException)
186cdf0e10cSrcweir {
187cdf0e10cSrcweir ScUnoGuard aGuard;
188cdf0e10cSrcweir if (mpDocShell)
189cdf0e10cSrcweir return sal_True;
190cdf0e10cSrcweir return sal_False;
191cdf0e10cSrcweir }
192cdf0e10cSrcweir
193cdf0e10cSrcweir
194cdf0e10cSrcweir
195