1 /* -*- Mode: C++; tab-width: 4; indent-tabs-mode: nil; c-basic-offset: 4 -*- */
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"
24 #include "sheetevents.hxx"
25 #include "unonames.hxx"
26 #include <vcl/svapp.hxx>
28 using namespace ::com::sun::star
;
30 //------------------------------------------------------------------------
32 SC_SIMPLE_SERVICE_INFO( ScSheetEventsObj
, "ScSheetEventsObj", "com.sun.star.document.Events" )
34 //------------------------------------------------------------------------
36 ScSheetEventsObj::ScSheetEventsObj(ScDocShell
* pDocSh
, SCTAB nT
) :
40 mpDocShell
->GetDocument()->AddUnoObject(*this);
43 ScSheetEventsObj::~ScSheetEventsObj()
46 mpDocShell
->GetDocument()->RemoveUnoObject(*this);
49 void ScSheetEventsObj::Notify( SfxBroadcaster
&, const SfxHint
& rHint
)
52 if ( rHint
.ISA( SfxSimpleHint
) &&
53 ((const SfxSimpleHint
&)rHint
).GetId() == SFX_HINT_DYING
)
59 static sal_Int32
lcl_GetEventFromName( const OUString
& aName
)
61 for (sal_Int32 nEvent
=0; nEvent
<SC_SHEETEVENT_COUNT
; ++nEvent
)
62 if ( aName
== ScSheetEvents::GetEventName(nEvent
) )
65 return -1; // not found
70 void SAL_CALL
ScSheetEventsObj::replaceByName( const OUString
& aName
, const uno::Any
& aElement
)
71 throw(lang::IllegalArgumentException
, container::NoSuchElementException
,
72 lang::WrappedTargetException
, uno::RuntimeException
)
74 SolarMutexGuard aGuard
;
76 throw uno::RuntimeException();
78 sal_Int32 nEvent
= lcl_GetEventFromName(aName
);
80 throw container::NoSuchElementException();
82 ScSheetEvents aNewEvents
;
83 const ScSheetEvents
* pOldEvents
= mpDocShell
->GetDocument()->GetSheetEvents(mnTab
);
85 aNewEvents
= *pOldEvents
;
88 if ( aElement
.hasValue() ) // empty Any -> reset event
90 uno::Sequence
<beans::PropertyValue
> aPropSeq
;
91 if ( aElement
>>= aPropSeq
)
93 sal_Int32 nPropCount
= aPropSeq
.getLength();
94 for (sal_Int32 nPos
=0; nPos
<nPropCount
; ++nPos
)
96 const beans::PropertyValue
& rProp
= aPropSeq
[nPos
];
97 if ( rProp
.Name
.equalsAscii( SC_UNO_EVENTTYPE
) )
100 if ( rProp
.Value
>>= aEventType
)
102 // only "Script" is supported
103 if ( aEventType
.compareToAscii( SC_UNO_SCRIPT
) != 0 )
104 throw lang::IllegalArgumentException();
107 else if ( rProp
.Name
.equalsAscii( SC_UNO_SCRIPT
) )
108 rProp
.Value
>>= aScript
;
112 if (!aScript
.isEmpty())
113 aNewEvents
.SetScript( nEvent
, &aScript
);
115 aNewEvents
.SetScript( nEvent
, NULL
); // reset
117 mpDocShell
->GetDocument()->SetSheetEvents( mnTab
, &aNewEvents
);
118 mpDocShell
->SetDocumentModified();
123 uno::Any SAL_CALL
ScSheetEventsObj::getByName( const OUString
& aName
)
124 throw(container::NoSuchElementException
, lang::WrappedTargetException
, uno::RuntimeException
)
126 SolarMutexGuard aGuard
;
127 sal_Int32 nEvent
= lcl_GetEventFromName(aName
);
129 throw container::NoSuchElementException();
131 const OUString
* pScript
= NULL
;
134 const ScSheetEvents
* pEvents
= mpDocShell
->GetDocument()->GetSheetEvents(mnTab
);
136 pScript
= pEvents
->GetScript(nEvent
);
142 uno::Sequence
<beans::PropertyValue
> aPropSeq( 2 );
143 aPropSeq
[0] = beans::PropertyValue(
144 OUString("EventType"), -1,
145 uno::makeAny( OUString("Script") ), beans::PropertyState_DIRECT_VALUE
);
146 aPropSeq
[1] = beans::PropertyValue(
147 OUString("Script"), -1,
148 uno::makeAny( *pScript
), beans::PropertyState_DIRECT_VALUE
);
151 // empty Any if nothing was set
155 uno::Sequence
<OUString
> SAL_CALL
ScSheetEventsObj::getElementNames() throw(uno::RuntimeException
)
157 SolarMutexGuard aGuard
;
158 uno::Sequence
<OUString
> aNames(SC_SHEETEVENT_COUNT
);
159 for (sal_Int32 nEvent
=0; nEvent
<SC_SHEETEVENT_COUNT
; ++nEvent
)
160 aNames
[nEvent
] = ScSheetEvents::GetEventName(nEvent
);
164 sal_Bool SAL_CALL
ScSheetEventsObj::hasByName( const OUString
& aName
) throw(uno::RuntimeException
)
166 SolarMutexGuard aGuard
;
167 sal_Int32 nEvent
= lcl_GetEventFromName(aName
);
168 return (nEvent
>= 0);
173 uno::Type SAL_CALL
ScSheetEventsObj::getElementType() throw(uno::RuntimeException
)
175 SolarMutexGuard aGuard
;
176 return getCppuType((uno::Sequence
<beans::PropertyValue
>*)0);
179 sal_Bool SAL_CALL
ScSheetEventsObj::hasElements() throw(uno::RuntimeException
)
181 SolarMutexGuard aGuard
;
189 /* vim:set shiftwidth=4 softtabstop=4 expandtab: */