merge the formfield patch from ooo-build
[ooovba.git] / sw / source / core / doc / docbasic.cxx
blob51dc3132b6761126567799583057452f0a20b328
1 /*************************************************************************
3 * DO NOT ALTER OR REMOVE COPYRIGHT NOTICES OR THIS FILE HEADER.
4 *
5 * Copyright 2008 by Sun Microsystems, Inc.
7 * OpenOffice.org - a multi-platform office productivity suite
9 * $RCSfile: docbasic.cxx,v $
10 * $Revision: 1.15 $
12 * This file is part of OpenOffice.org.
14 * OpenOffice.org is free software: you can redistribute it and/or modify
15 * it under the terms of the GNU Lesser General Public License version 3
16 * only, as published by the Free Software Foundation.
18 * OpenOffice.org is distributed in the hope that it will be useful,
19 * but WITHOUT ANY WARRANTY; without even the implied warranty of
20 * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
21 * GNU Lesser General Public License version 3 for more details
22 * (a copy is included in the LICENSE file that accompanied this code).
24 * You should have received a copy of the GNU Lesser General Public License
25 * version 3 along with OpenOffice.org. If not, see
26 * <http://www.openoffice.org/license.html>
27 * for a copy of the LGPLv3 License.
29 ************************************************************************/
31 // MARKER(update_precomp.py): autogen include statement, do not remove
32 #include "precompiled_sw.hxx"
35 #include <hintids.hxx>
37 #ifndef _RTL_USTRING_HXX //autogen
38 #include <rtl/ustring.hxx>
39 #endif
40 #include <svtools/imap.hxx>
41 #include <svtools/imapobj.hxx>
42 #include <basic/sbx.hxx>
43 #include <frmfmt.hxx>
44 #include <fmtinfmt.hxx>
45 #include <fmturl.hxx>
46 #include <frmatr.hxx>
47 #include <docary.hxx>
48 #include <doc.hxx>
49 #ifndef _DOCSH_HXX
50 #include <docsh.hxx>
51 #endif
52 #include <swevent.hxx>
54 using namespace ::com::sun::star::uno;
55 using ::rtl::OUString;
57 static Sequence<Any> *lcl_docbasic_convertArgs( SbxArray& rArgs )
59 Sequence<Any> *pRet = 0;
61 USHORT nCount = rArgs.Count();
62 if( nCount > 1 )
64 nCount--;
65 pRet = new Sequence<Any>( nCount );
66 Any *pUnoArgs = pRet->getArray();
67 for( USHORT i=0; i<nCount; i++ )
69 SbxVariable *pVar = rArgs.Get( i+1 );
70 switch( pVar->GetType() )
72 case SbxSTRING:
73 pUnoArgs[i] <<= OUString( pVar->GetString() );
74 break;
75 case SbxCHAR:
76 pUnoArgs[i] <<= (sal_Int16)pVar->GetChar() ;
77 break;
78 case SbxUSHORT:
79 pUnoArgs[i] <<= (sal_Int16)pVar->GetUShort();
80 break;
81 case SbxLONG:
82 pUnoArgs[i] <<= (sal_Int32)pVar->GetLong();
83 break;
84 default:
85 pUnoArgs[i].setValue(0, ::getVoidCppuType());
86 break;
91 return pRet;
94 BOOL SwDoc::ExecMacro( const SvxMacro& rMacro, String* pRet, SbxArray* pArgs )
96 ErrCode eErr = 0;
97 switch( rMacro.GetScriptType() )
99 case STARBASIC:
101 SbxBaseRef aRef;
102 SbxValue* pRetValue = new SbxValue;
103 aRef = pRetValue;
104 eErr = pDocShell->CallBasic( rMacro.GetMacName(),
105 rMacro.GetLibName(),
106 0, pArgs, pRet ? pRetValue : 0 );
108 if( pRet && SbxNULL < pRetValue->GetType() &&
109 SbxVOID != pRetValue->GetType() )
110 // gueltiger Wert, also setzen
111 *pRet = pRetValue->GetString();
113 break;
114 case JAVASCRIPT:
115 // ignore JavaScript calls
116 break;
117 case EXTENDED_STYPE:
119 Sequence<Any> *pUnoArgs = 0;
120 if( pArgs )
122 // better to rename the local function to lcl_translateBasic2Uno and
123 // a much shorter routine can be found in sfx2/source/doc/objmisc.cxx
124 pUnoArgs = lcl_docbasic_convertArgs( *pArgs );
127 if (!pUnoArgs)
129 pUnoArgs = new Sequence< Any > (0);
132 // TODO - return value is not handled
133 Any aRet;
134 Sequence< sal_Int16 > aOutArgsIndex;
135 Sequence< Any > aOutArgs;
137 OSL_TRACE( "SwDoc::ExecMacro URL is %s", ByteString( rMacro.GetMacName(),
138 RTL_TEXTENCODING_UTF8).GetBuffer() );
140 eErr = pDocShell->CallXScript(
141 rMacro.GetMacName(), *pUnoArgs, aRet, aOutArgsIndex, aOutArgs);
143 //*pRet = pRetValue->GetString();
144 // use the AnyConverter to return a String if appropriate?
146 // need to call something like lcl_translateUno2Basic
147 // pArgs = lcl_translateUno2Basic( pUnoArgs );
149 delete pUnoArgs;
150 break;
154 return 0 == eErr;
159 USHORT SwDoc::CallEvent( USHORT nEvent, const SwCallMouseEvent& rCallEvent,
160 BOOL bCheckPtr, SbxArray* pArgs, const Link* )
162 if( !pDocShell ) // ohne DocShell geht das nicht!
163 return 0;
165 USHORT nRet = 0;
166 const SvxMacroTableDtor* pTbl = 0;
167 switch( rCallEvent.eType )
169 case EVENT_OBJECT_INETATTR:
170 if( bCheckPtr )
172 const SfxPoolItem* pItem;
173 USHORT n, nMaxItems = GetAttrPool().GetItemCount( RES_TXTATR_INETFMT );
174 for( n = 0; n < nMaxItems; ++n )
175 if( 0 != (pItem = GetAttrPool().GetItem( RES_TXTATR_INETFMT, n ) )
176 && rCallEvent.PTR.pINetAttr == pItem )
178 bCheckPtr = FALSE; // als Flag missbrauchen
179 break;
182 if( !bCheckPtr )
183 pTbl = rCallEvent.PTR.pINetAttr->GetMacroTbl();
184 break;
186 case EVENT_OBJECT_URLITEM:
187 case EVENT_OBJECT_IMAGE:
189 const SwFrmFmtPtr pFmt = (SwFrmFmtPtr)rCallEvent.PTR.pFmt;
190 if( bCheckPtr )
192 USHORT nPos = GetSpzFrmFmts()->GetPos( pFmt );
193 if( USHRT_MAX != nPos )
194 bCheckPtr = FALSE; // als Flag missbrauchen
196 if( !bCheckPtr )
197 pTbl = &pFmt->GetMacro().GetMacroTable();
199 break;
201 case EVENT_OBJECT_IMAGEMAP:
203 const IMapObject* pIMapObj = rCallEvent.PTR.IMAP.pIMapObj;
204 if( bCheckPtr )
206 const SwFrmFmtPtr pFmt = (SwFrmFmtPtr)rCallEvent.PTR.IMAP.pFmt;
207 USHORT nPos = GetSpzFrmFmts()->GetPos( pFmt );
208 const ImageMap* pIMap;
209 if( USHRT_MAX != nPos &&
210 0 != (pIMap = pFmt->GetURL().GetMap()) )
212 for( nPos = pIMap->GetIMapObjectCount(); nPos; )
213 if( pIMapObj == pIMap->GetIMapObject( --nPos ))
215 bCheckPtr = FALSE; // als Flag missbrauchen
216 break;
220 if( !bCheckPtr )
221 pTbl = &pIMapObj->GetMacroTable();
223 break;
224 default:
225 break;
228 if( pTbl )
230 nRet = 0x1;
231 if( pTbl->IsKeyValid( nEvent ) )
233 const SvxMacro& rMacro = *pTbl->Get( nEvent );
234 if( STARBASIC == rMacro.GetScriptType() )
236 nRet += 0 == pDocShell->CallBasic( rMacro.GetMacName(),
237 rMacro.GetLibName(), 0, pArgs ) ? 1 : 0;
239 else if( EXTENDED_STYPE == rMacro.GetScriptType() )
241 Sequence<Any> *pUnoArgs = 0;
243 if( pArgs )
245 pUnoArgs = lcl_docbasic_convertArgs( *pArgs );
248 if (!pUnoArgs)
250 pUnoArgs = new Sequence <Any> (0);
253 Any aRet;
254 Sequence< sal_Int16 > aOutArgsIndex;
255 Sequence< Any > aOutArgs;
257 OSL_TRACE( "SwDoc::CallEvent URL is %s", ByteString(
258 rMacro.GetMacName(), RTL_TEXTENCODING_UTF8).GetBuffer() );
260 nRet += 0 == pDocShell->CallXScript(
261 rMacro.GetMacName(), *pUnoArgs,aRet, aOutArgsIndex, aOutArgs) ? 1 : 0;
263 //*pRet = pRetValue->GetString();
264 // use the AnyConverter to return a String if appropriate?
266 // need to call something like lcl_translateUno2Basic
267 // pArgs = lcl_translateUno2Basic( pUnoArgs );
269 delete pUnoArgs;
271 // JavaScript calls are ignored
274 return nRet;