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 .
20 #include "eventuno.hxx"
21 #include "miscuno.hxx"
23 #include "sheetevents.hxx"
24 #include "unonames.hxx"
25 #include <vcl/svapp.hxx>
27 using namespace ::com::sun::star
;
29 SC_SIMPLE_SERVICE_INFO( ScSheetEventsObj
, "ScSheetEventsObj", "com.sun.star.document.Events" )
31 ScSheetEventsObj::ScSheetEventsObj(ScDocShell
* pDocSh
, SCTAB nT
) :
35 mpDocShell
->GetDocument().AddUnoObject(*this);
38 ScSheetEventsObj::~ScSheetEventsObj()
43 mpDocShell
->GetDocument().RemoveUnoObject(*this);
46 void ScSheetEventsObj::Notify( SfxBroadcaster
&, const SfxHint
& rHint
)
49 const SfxSimpleHint
* pSimpleHint
= dynamic_cast<const SfxSimpleHint
*>(&rHint
);
50 if ( pSimpleHint
&& pSimpleHint
->GetId() == SFX_HINT_DYING
)
56 static sal_Int32
lcl_GetEventFromName( const OUString
& aName
)
58 for (sal_Int32 nEvent
=0; nEvent
<SC_SHEETEVENT_COUNT
; ++nEvent
)
59 if ( aName
== ScSheetEvents::GetEventName(nEvent
) )
62 return -1; // not found
67 void SAL_CALL
ScSheetEventsObj::replaceByName( const OUString
& aName
, const uno::Any
& aElement
)
68 throw(lang::IllegalArgumentException
, container::NoSuchElementException
,
69 lang::WrappedTargetException
, uno::RuntimeException
, std::exception
)
71 SolarMutexGuard aGuard
;
73 throw uno::RuntimeException();
75 sal_Int32 nEvent
= lcl_GetEventFromName(aName
);
77 throw container::NoSuchElementException();
79 ScSheetEvents aNewEvents
;
80 const ScSheetEvents
* pOldEvents
= mpDocShell
->GetDocument().GetSheetEvents(mnTab
);
82 aNewEvents
= *pOldEvents
;
85 if ( aElement
.hasValue() ) // empty Any -> reset event
87 uno::Sequence
<beans::PropertyValue
> aPropSeq
;
88 if ( aElement
>>= aPropSeq
)
90 sal_Int32 nPropCount
= aPropSeq
.getLength();
91 for (sal_Int32 nPos
=0; nPos
<nPropCount
; ++nPos
)
93 const beans::PropertyValue
& rProp
= aPropSeq
[nPos
];
94 if ( rProp
.Name
== SC_UNO_EVENTTYPE
)
97 if ( rProp
.Value
>>= aEventType
)
99 // only "Script" is supported
100 if ( aEventType
!= SC_UNO_SCRIPT
)
101 throw lang::IllegalArgumentException();
104 else if ( rProp
.Name
== SC_UNO_SCRIPT
)
105 rProp
.Value
>>= aScript
;
109 if (!aScript
.isEmpty())
110 aNewEvents
.SetScript( nEvent
, &aScript
);
112 aNewEvents
.SetScript( nEvent
, NULL
); // reset
114 mpDocShell
->GetDocument().SetSheetEvents( mnTab
, &aNewEvents
);
115 mpDocShell
->SetDocumentModified();
120 uno::Any SAL_CALL
ScSheetEventsObj::getByName( const OUString
& aName
)
121 throw(container::NoSuchElementException
, lang::WrappedTargetException
, uno::RuntimeException
, std::exception
)
123 SolarMutexGuard aGuard
;
124 sal_Int32 nEvent
= lcl_GetEventFromName(aName
);
126 throw container::NoSuchElementException();
128 const OUString
* pScript
= NULL
;
131 const ScSheetEvents
* pEvents
= mpDocShell
->GetDocument().GetSheetEvents(mnTab
);
133 pScript
= pEvents
->GetScript(nEvent
);
139 uno::Sequence
<beans::PropertyValue
> aPropSeq( 2 );
140 aPropSeq
[0] = beans::PropertyValue(
141 OUString("EventType"), -1,
142 uno::makeAny( OUString("Script") ), beans::PropertyState_DIRECT_VALUE
);
143 aPropSeq
[1] = beans::PropertyValue(
144 OUString("Script"), -1,
145 uno::makeAny( *pScript
), beans::PropertyState_DIRECT_VALUE
);
148 // empty Any if nothing was set
152 uno::Sequence
<OUString
> SAL_CALL
ScSheetEventsObj::getElementNames() throw(uno::RuntimeException
, std::exception
)
154 SolarMutexGuard aGuard
;
155 uno::Sequence
<OUString
> aNames(SC_SHEETEVENT_COUNT
);
156 for (sal_Int32 nEvent
=0; nEvent
<SC_SHEETEVENT_COUNT
; ++nEvent
)
157 aNames
[nEvent
] = ScSheetEvents::GetEventName(nEvent
);
161 sal_Bool SAL_CALL
ScSheetEventsObj::hasByName( const OUString
& aName
) throw(uno::RuntimeException
, std::exception
)
163 SolarMutexGuard aGuard
;
164 sal_Int32 nEvent
= lcl_GetEventFromName(aName
);
165 return (nEvent
>= 0);
170 uno::Type SAL_CALL
ScSheetEventsObj::getElementType() throw(uno::RuntimeException
, std::exception
)
172 SolarMutexGuard aGuard
;
173 return cppu::UnoType
<uno::Sequence
<beans::PropertyValue
>>::get();
176 sal_Bool SAL_CALL
ScSheetEventsObj::hasElements() throw(uno::RuntimeException
, std::exception
)
178 SolarMutexGuard aGuard
;
184 /* vim:set shiftwidth=4 softtabstop=4 expandtab: */