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 <hintids.hxx>
22 #include <rtl/ustring.hxx>
23 #include <sal/log.hxx>
24 #include <vcl/imap.hxx>
25 #include <vcl/imapobj.hxx>
26 #include <basic/sbx.hxx>
28 #include <fmtinfmt.hxx>
33 #include <swevent.hxx>
34 #include <frameformats.hxx>
37 using namespace ::com::sun::star::uno
;
39 static Sequence
<Any
> *lcl_docbasic_convertArgs( SbxArray
& rArgs
)
41 Sequence
<Any
> *pRet
= nullptr;
43 sal_uInt32 nCount
= rArgs
.Count();
47 pRet
= new Sequence
<Any
>( nCount
);
48 Any
*pUnoArgs
= pRet
->getArray();
49 for( sal_uInt32 i
=0; i
<nCount
; i
++ )
51 SbxVariable
* pVar
= rArgs
.Get(i
+ 1);
52 switch( pVar
->GetType() )
55 pUnoArgs
[i
] <<= pVar
->GetOUString();
58 pUnoArgs
[i
] <<= static_cast<sal_Int16
>(pVar
->GetChar()) ;
61 pUnoArgs
[i
] <<= static_cast<sal_Int16
>(pVar
->GetUShort());
64 pUnoArgs
[i
] <<= pVar
->GetLong();
76 void SwDoc::ExecMacro( const SvxMacro
& rMacro
, OUString
* pRet
, SbxArray
* pArgs
)
78 switch( rMacro
.GetScriptType() )
83 SbxValue
* pRetValue
= new SbxValue
;
85 mpDocShell
->CallBasic( rMacro
.GetMacName(),
87 pArgs
, pRet
? pRetValue
: nullptr );
89 if( pRet
&& SbxNULL
< pRetValue
->GetType() &&
90 SbxVOID
!= pRetValue
->GetType() )
92 // valid value, so set it
93 *pRet
= pRetValue
->GetOUString();
98 // ignore JavaScript calls
102 std::unique_ptr
<Sequence
<Any
> > pUnoArgs
;
105 // better to rename the local function to lcl_translateBasic2Uno and
106 // a much shorter routine can be found in sfx2/source/doc/objmisc.cxx
107 pUnoArgs
.reset(lcl_docbasic_convertArgs( *pArgs
));
112 pUnoArgs
.reset(new Sequence
< Any
> (0));
115 // TODO - return value is not handled
117 Sequence
< sal_Int16
> aOutArgsIndex
;
118 Sequence
< Any
> aOutArgs
;
120 SAL_INFO("sw", "SwDoc::ExecMacro URL is " << rMacro
.GetMacName() );
122 mpDocShell
->CallXScript(
123 rMacro
.GetMacName(), *pUnoArgs
, aRet
, aOutArgsIndex
, aOutArgs
);
130 sal_uInt16
SwDoc::CallEvent( SvMacroItemId nEvent
, const SwCallMouseEvent
& rCallEvent
,
133 if( !mpDocShell
) // we can't do that without a DocShell!
137 const SvxMacroTableDtor
* pTable
= nullptr;
138 switch( rCallEvent
.eType
)
140 case EVENT_OBJECT_INETATTR
:
143 for (const SfxPoolItem
* pItem
: GetAttrPool().GetItemSurrogates(RES_TXTATR_INETFMT
))
145 auto pFormatItem
= dynamic_cast<const SwFormatINetFormat
*>(pItem
);
146 if( pFormatItem
&& rCallEvent
.PTR
.pINetAttr
== pFormatItem
)
148 bCheckPtr
= false; // misuse as a flag
154 pTable
= rCallEvent
.PTR
.pINetAttr
->GetMacroTable();
157 case EVENT_OBJECT_URLITEM
:
158 case EVENT_OBJECT_IMAGE
:
160 const auto pSpz
= static_cast<const sw::SpzFrameFormat
*>(rCallEvent
.PTR
.pFormat
);
163 if (GetSpzFrameFormats()->IsAlive(pSpz
))
164 bCheckPtr
= false; // misuse as a flag
166 // this shouldn't be possible now that SwCallMouseEvent
167 // listens for dying format?
171 pTable
= &pSpz
->GetMacro().GetMacroTable();
175 case EVENT_OBJECT_IMAGEMAP
:
177 const IMapObject
* pIMapObj
= rCallEvent
.PTR
.IMAP
.pIMapObj
;
180 const auto pSpz
= static_cast<const sw::SpzFrameFormat
*>(rCallEvent
.PTR
.IMAP
.pFormat
);
181 if (GetSpzFrameFormats()->IsAlive(pSpz
))
183 const ImageMap
* pIMap
= pSpz
->GetURL().GetMap();
186 for( size_t nPos
= pIMap
->GetIMapObjectCount(); nPos
; )
187 if( pIMapObj
== pIMap
->GetIMapObject( --nPos
))
189 bCheckPtr
= false; // misuse as a flag
196 pTable
= &pIMapObj
->GetMacroTable();
206 if( pTable
->IsKeyValid( nEvent
) )
208 const SvxMacro
& rMacro
= *pTable
->Get( nEvent
);
209 if( STARBASIC
== rMacro
.GetScriptType() )
211 nRet
+= ERRCODE_NONE
== mpDocShell
->CallBasic( rMacro
.GetMacName(),
212 rMacro
.GetLibName(), nullptr ) ? 1 : 0;
214 else if( EXTENDED_STYPE
== rMacro
.GetScriptType() )
216 Sequence
<Any
> aUnoArgs
;
219 Sequence
< sal_Int16
> aOutArgsIndex
;
220 Sequence
< Any
> aOutArgs
;
222 SAL_INFO("sw", "SwDoc::CallEvent URL is " << rMacro
.GetMacName() );
224 nRet
+= ERRCODE_NONE
== mpDocShell
->CallXScript(
225 rMacro
.GetMacName(), aUnoArgs
, aRet
, aOutArgsIndex
, aOutArgs
) ? 1 : 0;
227 // JavaScript calls are ignored
233 /* vim:set shiftwidth=4 softtabstop=4 expandtab: */