tdf#130857 qt weld: Implement QtInstanceWidget::strip_mnemonic
[LibreOffice.git] / sc / source / ui / unoobj / eventuno.cxx
blob9802bb5539ec8c05d0fee3191340a128cabdb37e
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 <eventuno.hxx>
21 #include <miscuno.hxx>
22 #include <docsh.hxx>
23 #include <sheetevents.hxx>
24 #include <unonames.hxx>
25 #include <comphelper/propertysequence.hxx>
26 #include <vcl/svapp.hxx>
28 using namespace ::com::sun::star;
30 SC_SIMPLE_SERVICE_INFO( ScSheetEventsObj, u"ScSheetEventsObj"_ustr, u"com.sun.star.document.Events"_ustr )
32 ScSheetEventsObj::ScSheetEventsObj(ScDocShell* pDocSh, SCTAB nT) :
33 mpDocShell( pDocSh ),
34 mnTab( nT )
36 mpDocShell->GetDocument().AddUnoObject(*this);
39 ScSheetEventsObj::~ScSheetEventsObj()
41 SolarMutexGuard g;
43 if (mpDocShell)
44 mpDocShell->GetDocument().RemoveUnoObject(*this);
47 void ScSheetEventsObj::Notify( SfxBroadcaster&, const SfxHint& rHint )
49 //! reference update
50 if ( rHint.GetId() == SfxHintId::Dying )
52 mpDocShell = nullptr;
56 static ScSheetEventId lcl_GetEventFromName( std::u16string_view aName )
58 for (sal_Int32 nEvent=0; nEvent<static_cast<sal_Int32>(ScSheetEventId::COUNT); ++nEvent)
59 if ( aName == ScSheetEvents::GetEventName(static_cast<ScSheetEventId>(nEvent)) )
60 return static_cast<ScSheetEventId>(nEvent);
62 return ScSheetEventId::NOTFOUND; // not found
65 // XNameReplace
67 void SAL_CALL ScSheetEventsObj::replaceByName( const OUString& aName, const uno::Any& aElement )
69 SolarMutexGuard aGuard;
70 if (!mpDocShell)
71 throw uno::RuntimeException();
73 ScSheetEventId nEvent = lcl_GetEventFromName(aName);
74 if (nEvent == ScSheetEventId::NOTFOUND)
75 throw container::NoSuchElementException();
77 std::unique_ptr<ScSheetEvents> pNewEvents(new ScSheetEvents);
78 const ScSheetEvents* pOldEvents = mpDocShell->GetDocument().GetSheetEvents(mnTab);
79 if (pOldEvents)
80 *pNewEvents = *pOldEvents;
82 OUString aScript;
83 if ( aElement.hasValue() ) // empty Any -> reset event
85 uno::Sequence<beans::PropertyValue> aPropSeq;
86 if ( aElement >>= aPropSeq )
88 for (const beans::PropertyValue& rProp : aPropSeq)
90 if ( rProp.Name == SC_UNO_EVENTTYPE )
92 OUString aEventType;
93 if ( rProp.Value >>= aEventType )
95 // only "Script" is supported
96 if ( aEventType != SC_UNO_SCRIPT )
97 throw lang::IllegalArgumentException();
100 else if ( rProp.Name == SC_UNO_SCRIPT )
101 rProp.Value >>= aScript;
105 if (!aScript.isEmpty())
106 pNewEvents->SetScript( nEvent, &aScript );
107 else
108 pNewEvents->SetScript( nEvent, nullptr ); // reset
110 mpDocShell->GetDocument().SetSheetEvents( mnTab, std::move(pNewEvents) );
111 mpDocShell->SetDocumentModified();
114 // XNameAccess
116 uno::Any SAL_CALL ScSheetEventsObj::getByName( const OUString& aName )
118 SolarMutexGuard aGuard;
119 ScSheetEventId nEvent = lcl_GetEventFromName(aName);
120 if (nEvent == ScSheetEventId::NOTFOUND)
121 throw container::NoSuchElementException();
123 const OUString* pScript = nullptr;
124 if (mpDocShell)
126 const ScSheetEvents* pEvents = mpDocShell->GetDocument().GetSheetEvents(mnTab);
127 if (pEvents)
128 pScript = pEvents->GetScript(nEvent);
131 uno::Any aRet;
132 if (pScript)
134 uno::Sequence<beans::PropertyValue> aPropSeq( comphelper::InitPropertySequence({
135 { "EventType", uno::Any( u"Script"_ustr ) },
136 { "Script", uno::Any( *pScript ) }
137 }));
138 aRet <<= aPropSeq;
140 // empty Any if nothing was set
141 return aRet;
144 uno::Sequence<OUString> SAL_CALL ScSheetEventsObj::getElementNames()
146 auto aNames = uno::Sequence<OUString>(int(ScSheetEventId::COUNT));
147 auto pNames = aNames.getArray();
148 for (sal_Int32 nEvent=0; nEvent<int(ScSheetEventId::COUNT); ++nEvent)
149 pNames[nEvent] = ScSheetEvents::GetEventName(static_cast<ScSheetEventId>(nEvent));
150 return aNames;
153 sal_Bool SAL_CALL ScSheetEventsObj::hasByName( const OUString& aName )
155 ScSheetEventId nEvent = lcl_GetEventFromName(aName);
156 return (nEvent != ScSheetEventId::NOTFOUND);
159 // XElementAccess
161 uno::Type SAL_CALL ScSheetEventsObj::getElementType()
163 return cppu::UnoType<uno::Sequence<beans::PropertyValue>>::get();
166 sal_Bool SAL_CALL ScSheetEventsObj::hasElements()
168 SolarMutexGuard aGuard;
169 if (mpDocShell)
170 return true;
171 return false;
174 /* vim:set shiftwidth=4 softtabstop=4 expandtab: */