update credits
[LibreOffice.git] / sw / source / core / doc / docbasic.cxx
blob1437e6135e1533d971f216f7f1bbb6e2f5b0861d
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 <hintids.hxx>
22 #include <rtl/ustring.hxx>
23 #include <svtools/imap.hxx>
24 #include <svtools/imapobj.hxx>
25 #include <basic/sbx.hxx>
26 #include <frmfmt.hxx>
27 #include <fmtinfmt.hxx>
28 #include <fmturl.hxx>
29 #include <frmatr.hxx>
30 #include <docary.hxx>
31 #include <doc.hxx>
32 #include <docsh.hxx>
33 #include <swevent.hxx>
35 using namespace ::com::sun::star::uno;
37 static Sequence<Any> *lcl_docbasic_convertArgs( SbxArray& rArgs )
39 Sequence<Any> *pRet = 0;
41 sal_uInt16 nCount = rArgs.Count();
42 if( nCount > 1 )
44 nCount--;
45 pRet = new Sequence<Any>( nCount );
46 Any *pUnoArgs = pRet->getArray();
47 for( sal_uInt16 i=0; i<nCount; i++ )
49 SbxVariable *pVar = rArgs.Get( i+1 );
50 switch( pVar->GetType() )
52 case SbxSTRING:
53 pUnoArgs[i] <<= OUString( pVar->GetOUString() );
54 break;
55 case SbxCHAR:
56 pUnoArgs[i] <<= (sal_Int16)pVar->GetChar() ;
57 break;
58 case SbxUSHORT:
59 pUnoArgs[i] <<= (sal_Int16)pVar->GetUShort();
60 break;
61 case SbxLONG:
62 pUnoArgs[i] <<= (sal_Int32)pVar->GetLong();
63 break;
64 default:
65 pUnoArgs[i].setValue(0, ::getVoidCppuType());
66 break;
71 return pRet;
74 bool SwDoc::ExecMacro( const SvxMacro& rMacro, String* pRet, SbxArray* pArgs )
76 ErrCode eErr = 0;
77 switch( rMacro.GetScriptType() )
79 case STARBASIC:
81 SbxBaseRef aRef;
82 SbxValue* pRetValue = new SbxValue;
83 aRef = pRetValue;
84 eErr = mpDocShell->CallBasic( rMacro.GetMacName(),
85 rMacro.GetLibName(),
86 pArgs, pRet ? pRetValue : 0 );
88 if( pRet && SbxNULL < pRetValue->GetType() &&
89 SbxVOID != pRetValue->GetType() )
91 // valid value, so set it
92 *pRet = pRetValue->GetOUString();
95 break;
96 case JAVASCRIPT:
97 // ignore JavaScript calls
98 break;
99 case EXTENDED_STYPE:
101 Sequence<Any> *pUnoArgs = 0;
102 if( pArgs )
104 // better to rename the local function to lcl_translateBasic2Uno and
105 // a much shorter routine can be found in sfx2/source/doc/objmisc.cxx
106 pUnoArgs = lcl_docbasic_convertArgs( *pArgs );
109 if (!pUnoArgs)
111 pUnoArgs = new Sequence< Any > (0);
114 // TODO - return value is not handled
115 Any aRet;
116 Sequence< sal_Int16 > aOutArgsIndex;
117 Sequence< Any > aOutArgs;
119 OSL_TRACE( "SwDoc::ExecMacro URL is %s", OUStringToOString( rMacro.GetMacName(),
120 RTL_TEXTENCODING_UTF8).getStr() );
122 eErr = mpDocShell->CallXScript(
123 rMacro.GetMacName(), *pUnoArgs, aRet, aOutArgsIndex, aOutArgs);
125 delete pUnoArgs;
126 break;
130 return 0 == eErr;
135 sal_uInt16 SwDoc::CallEvent( sal_uInt16 nEvent, const SwCallMouseEvent& rCallEvent,
136 bool bCheckPtr, SbxArray* pArgs, const Link* )
138 if( !mpDocShell ) // we can't do that without a DocShell!
139 return 0;
141 sal_uInt16 nRet = 0;
142 const SvxMacroTableDtor* pTbl = 0;
143 switch( rCallEvent.eType )
145 case EVENT_OBJECT_INETATTR:
146 if( bCheckPtr )
148 const SfxPoolItem* pItem;
149 sal_uInt32 n, nMaxItems = GetAttrPool().GetItemCount2( RES_TXTATR_INETFMT );
150 for( n = 0; n < nMaxItems; ++n )
151 if( 0 != (pItem = GetAttrPool().GetItem2( RES_TXTATR_INETFMT, n ) )
152 && rCallEvent.PTR.pINetAttr == pItem )
154 bCheckPtr = false; // misuse as a flag
155 break;
158 if( !bCheckPtr )
159 pTbl = rCallEvent.PTR.pINetAttr->GetMacroTbl();
160 break;
162 case EVENT_OBJECT_URLITEM:
163 case EVENT_OBJECT_IMAGE:
165 const SwFrmFmt* pFmt = (SwFrmFmt*)rCallEvent.PTR.pFmt;
166 if( bCheckPtr )
168 if ( GetSpzFrmFmts()->Contains( pFmt ) )
169 bCheckPtr = false; // misuse as a flag
171 if( !bCheckPtr )
172 pTbl = &pFmt->GetMacro().GetMacroTable();
174 break;
176 case EVENT_OBJECT_IMAGEMAP:
178 const IMapObject* pIMapObj = rCallEvent.PTR.IMAP.pIMapObj;
179 if( bCheckPtr )
181 const SwFrmFmt* pFmt = (SwFrmFmt*)rCallEvent.PTR.IMAP.pFmt;
182 const ImageMap* pIMap;
183 if( GetSpzFrmFmts()->Contains( pFmt ) &&
184 0 != (pIMap = pFmt->GetURL().GetMap()) )
186 for( sal_uInt16 nPos = pIMap->GetIMapObjectCount(); nPos; )
187 if( pIMapObj == pIMap->GetIMapObject( --nPos ))
189 bCheckPtr = false; // misuse as a flag
190 break;
194 if( !bCheckPtr )
195 pTbl = &pIMapObj->GetMacroTable();
197 break;
198 default:
199 break;
202 if( pTbl )
204 nRet = 0x1;
205 if( pTbl->IsKeyValid( nEvent ) )
207 const SvxMacro& rMacro = *pTbl->Get( nEvent );
208 if( STARBASIC == rMacro.GetScriptType() )
210 nRet += 0 == mpDocShell->CallBasic( rMacro.GetMacName(),
211 rMacro.GetLibName(), pArgs ) ? 1 : 0;
213 else if( EXTENDED_STYPE == rMacro.GetScriptType() )
215 Sequence<Any> *pUnoArgs = 0;
217 if( pArgs )
219 pUnoArgs = lcl_docbasic_convertArgs( *pArgs );
222 if (!pUnoArgs)
224 pUnoArgs = new Sequence <Any> (0);
227 Any aRet;
228 Sequence< sal_Int16 > aOutArgsIndex;
229 Sequence< Any > aOutArgs;
231 OSL_TRACE( "SwDoc::CallEvent URL is %s", OUStringToOString(
232 rMacro.GetMacName(), RTL_TEXTENCODING_UTF8).getStr() );
234 nRet += 0 == mpDocShell->CallXScript(
235 rMacro.GetMacName(), *pUnoArgs,aRet, aOutArgsIndex, aOutArgs) ? 1 : 0;
237 delete pUnoArgs;
239 // JavaScript calls are ignored
242 return nRet;
248 /* vim:set shiftwidth=4 softtabstop=4 expandtab: */