xref: /aoo41x/main/sw/source/core/doc/docbasic.cxx (revision efeef26f)
1*efeef26fSAndrew Rist /**************************************************************
2cdf0e10cSrcweir  *
3*efeef26fSAndrew Rist  * Licensed to the Apache Software Foundation (ASF) under one
4*efeef26fSAndrew Rist  * or more contributor license agreements.  See the NOTICE file
5*efeef26fSAndrew Rist  * distributed with this work for additional information
6*efeef26fSAndrew Rist  * regarding copyright ownership.  The ASF licenses this file
7*efeef26fSAndrew Rist  * to you under the Apache License, Version 2.0 (the
8*efeef26fSAndrew Rist  * "License"); you may not use this file except in compliance
9*efeef26fSAndrew Rist  * with the License.  You may obtain a copy of the License at
10*efeef26fSAndrew Rist  *
11*efeef26fSAndrew Rist  *   http://www.apache.org/licenses/LICENSE-2.0
12*efeef26fSAndrew Rist  *
13*efeef26fSAndrew Rist  * Unless required by applicable law or agreed to in writing,
14*efeef26fSAndrew Rist  * software distributed under the License is distributed on an
15*efeef26fSAndrew Rist  * "AS IS" BASIS, WITHOUT WARRANTIES OR CONDITIONS OF ANY
16*efeef26fSAndrew Rist  * KIND, either express or implied.  See the License for the
17*efeef26fSAndrew Rist  * specific language governing permissions and limitations
18*efeef26fSAndrew Rist  * under the License.
19*efeef26fSAndrew Rist  *
20*efeef26fSAndrew Rist  *************************************************************/
21*efeef26fSAndrew Rist 
22*efeef26fSAndrew Rist 
23cdf0e10cSrcweir 
24cdf0e10cSrcweir // MARKER(update_precomp.py): autogen include statement, do not remove
25cdf0e10cSrcweir #include "precompiled_sw.hxx"
26cdf0e10cSrcweir 
27cdf0e10cSrcweir 
28cdf0e10cSrcweir #include <hintids.hxx>
29cdf0e10cSrcweir 
30cdf0e10cSrcweir #ifndef _RTL_USTRING_HXX //autogen
31cdf0e10cSrcweir #include <rtl/ustring.hxx>
32cdf0e10cSrcweir #endif
33cdf0e10cSrcweir #include <svtools/imap.hxx>
34cdf0e10cSrcweir #include <svtools/imapobj.hxx>
35cdf0e10cSrcweir #include <basic/sbx.hxx>
36cdf0e10cSrcweir #include <frmfmt.hxx>
37cdf0e10cSrcweir #include <fmtinfmt.hxx>
38cdf0e10cSrcweir #include <fmturl.hxx>
39cdf0e10cSrcweir #include <frmatr.hxx>
40cdf0e10cSrcweir #include <docary.hxx>
41cdf0e10cSrcweir #include <doc.hxx>
42cdf0e10cSrcweir #ifndef _DOCSH_HXX
43cdf0e10cSrcweir #include <docsh.hxx>
44cdf0e10cSrcweir #endif
45cdf0e10cSrcweir #include <swevent.hxx>
46cdf0e10cSrcweir 
47cdf0e10cSrcweir using namespace ::com::sun::star::uno;
48cdf0e10cSrcweir using ::rtl::OUString;
49cdf0e10cSrcweir 
lcl_docbasic_convertArgs(SbxArray & rArgs)50cdf0e10cSrcweir static Sequence<Any> *lcl_docbasic_convertArgs( SbxArray& rArgs )
51cdf0e10cSrcweir {
52cdf0e10cSrcweir 	Sequence<Any> *pRet = 0;
53cdf0e10cSrcweir 
54cdf0e10cSrcweir 	sal_uInt16 nCount = rArgs.Count();
55cdf0e10cSrcweir 	if( nCount > 1 )
56cdf0e10cSrcweir 	{
57cdf0e10cSrcweir 		nCount--;
58cdf0e10cSrcweir 		pRet = new Sequence<Any>( nCount );
59cdf0e10cSrcweir 		Any *pUnoArgs = pRet->getArray();
60cdf0e10cSrcweir 		for( sal_uInt16 i=0; i<nCount; i++ )
61cdf0e10cSrcweir 		{
62cdf0e10cSrcweir 			SbxVariable *pVar = rArgs.Get( i+1 );
63cdf0e10cSrcweir 			switch( pVar->GetType() )
64cdf0e10cSrcweir 			{
65cdf0e10cSrcweir 			case SbxSTRING:
66cdf0e10cSrcweir 				pUnoArgs[i] <<= OUString( pVar->GetString() );
67cdf0e10cSrcweir 				break;
68cdf0e10cSrcweir 			case SbxCHAR:
69cdf0e10cSrcweir 				pUnoArgs[i] <<= (sal_Int16)pVar->GetChar() ;
70cdf0e10cSrcweir 				break;
71cdf0e10cSrcweir 			case SbxUSHORT:
72cdf0e10cSrcweir 				pUnoArgs[i] <<= (sal_Int16)pVar->GetUShort();
73cdf0e10cSrcweir 				break;
74cdf0e10cSrcweir 			case SbxLONG:
75cdf0e10cSrcweir 				pUnoArgs[i] <<= (sal_Int32)pVar->GetLong();
76cdf0e10cSrcweir 				break;
77cdf0e10cSrcweir 			default:
78cdf0e10cSrcweir 				pUnoArgs[i].setValue(0, ::getVoidCppuType());
79cdf0e10cSrcweir 				break;
80cdf0e10cSrcweir 			}
81cdf0e10cSrcweir 		}
82cdf0e10cSrcweir 	}
83cdf0e10cSrcweir 
84cdf0e10cSrcweir 	return pRet;
85cdf0e10cSrcweir }
86cdf0e10cSrcweir 
ExecMacro(const SvxMacro & rMacro,String * pRet,SbxArray * pArgs)87cdf0e10cSrcweir sal_Bool SwDoc::ExecMacro( const SvxMacro& rMacro, String* pRet, SbxArray* pArgs )
88cdf0e10cSrcweir {
89cdf0e10cSrcweir 	ErrCode eErr = 0;
90cdf0e10cSrcweir 	switch( rMacro.GetScriptType() )
91cdf0e10cSrcweir 	{
92cdf0e10cSrcweir 	case STARBASIC:
93cdf0e10cSrcweir 		{
94cdf0e10cSrcweir 			SbxBaseRef aRef;
95cdf0e10cSrcweir 			SbxValue* pRetValue = new SbxValue;
96cdf0e10cSrcweir 			aRef = pRetValue;
97cdf0e10cSrcweir 			eErr = pDocShell->CallBasic( rMacro.GetMacName(),
98cdf0e10cSrcweir 										 rMacro.GetLibName(),
99cdf0e10cSrcweir 										 pArgs, pRet ? pRetValue : 0 );
100cdf0e10cSrcweir 
101cdf0e10cSrcweir 			if( pRet && SbxNULL <  pRetValue->GetType() &&
102cdf0e10cSrcweir 						SbxVOID != pRetValue->GetType() )
103cdf0e10cSrcweir 				// gueltiger Wert, also setzen
104cdf0e10cSrcweir 				*pRet = pRetValue->GetString();
105cdf0e10cSrcweir 		}
106cdf0e10cSrcweir 		break;
107cdf0e10cSrcweir 	case JAVASCRIPT:
108cdf0e10cSrcweir 		// ignore JavaScript calls
109cdf0e10cSrcweir 		break;
110cdf0e10cSrcweir 	case EXTENDED_STYPE:
111cdf0e10cSrcweir 		{
112cdf0e10cSrcweir 			Sequence<Any> *pUnoArgs = 0;
113cdf0e10cSrcweir 			if( pArgs )
114cdf0e10cSrcweir 			{
115cdf0e10cSrcweir 				// better to rename the local function to lcl_translateBasic2Uno and
116cdf0e10cSrcweir 				// a much shorter routine can be found in sfx2/source/doc/objmisc.cxx
117cdf0e10cSrcweir 				pUnoArgs = lcl_docbasic_convertArgs( *pArgs );
118cdf0e10cSrcweir 			}
119cdf0e10cSrcweir 
120cdf0e10cSrcweir 			if (!pUnoArgs)
121cdf0e10cSrcweir 			{
122cdf0e10cSrcweir 				pUnoArgs = new Sequence< Any > (0);
123cdf0e10cSrcweir 			}
124cdf0e10cSrcweir 
125cdf0e10cSrcweir 			// TODO - return value is not handled
126cdf0e10cSrcweir 			Any aRet;
127cdf0e10cSrcweir 			Sequence< sal_Int16 > aOutArgsIndex;
128cdf0e10cSrcweir 			Sequence< Any > aOutArgs;
129cdf0e10cSrcweir 
130cdf0e10cSrcweir 			OSL_TRACE( "SwDoc::ExecMacro URL is %s", ByteString( rMacro.GetMacName(),
131cdf0e10cSrcweir 				RTL_TEXTENCODING_UTF8).GetBuffer() );
132cdf0e10cSrcweir 
133cdf0e10cSrcweir 			eErr = pDocShell->CallXScript(
134cdf0e10cSrcweir 				rMacro.GetMacName(), *pUnoArgs, aRet, aOutArgsIndex, aOutArgs);
135cdf0e10cSrcweir 
136cdf0e10cSrcweir 			//*pRet = pRetValue->GetString();
137cdf0e10cSrcweir 			// use the AnyConverter to return a String if appropriate?
138cdf0e10cSrcweir 
139cdf0e10cSrcweir 			// need to call something like lcl_translateUno2Basic
140cdf0e10cSrcweir 			// pArgs = lcl_translateUno2Basic( pUnoArgs );
141cdf0e10cSrcweir 
142cdf0e10cSrcweir 			delete pUnoArgs;
143cdf0e10cSrcweir 			break;
144cdf0e10cSrcweir 		}
145cdf0e10cSrcweir 	}
146cdf0e10cSrcweir 
147cdf0e10cSrcweir 	return 0 == eErr;
148cdf0e10cSrcweir }
149cdf0e10cSrcweir 
150cdf0e10cSrcweir 
151cdf0e10cSrcweir 
CallEvent(sal_uInt16 nEvent,const SwCallMouseEvent & rCallEvent,sal_Bool bCheckPtr,SbxArray * pArgs,const Link *)152cdf0e10cSrcweir sal_uInt16 SwDoc::CallEvent( sal_uInt16 nEvent, const SwCallMouseEvent& rCallEvent,
153cdf0e10cSrcweir 					sal_Bool bCheckPtr, SbxArray* pArgs, const Link* )
154cdf0e10cSrcweir {
155cdf0e10cSrcweir 	if( !pDocShell )		// ohne DocShell geht das nicht!
156cdf0e10cSrcweir 		return 0;
157cdf0e10cSrcweir 
158cdf0e10cSrcweir 	sal_uInt16 nRet = 0;
159cdf0e10cSrcweir 	const SvxMacroTableDtor* pTbl = 0;
160cdf0e10cSrcweir 	switch( rCallEvent.eType )
161cdf0e10cSrcweir 	{
162cdf0e10cSrcweir 	case EVENT_OBJECT_INETATTR:
163cdf0e10cSrcweir 		if( bCheckPtr  )
164cdf0e10cSrcweir 		{
165cdf0e10cSrcweir 			const SfxPoolItem* pItem;
166cdf0e10cSrcweir 			sal_uInt32 n, nMaxItems = GetAttrPool().GetItemCount2( RES_TXTATR_INETFMT );
167cdf0e10cSrcweir 			for( n = 0; n < nMaxItems; ++n )
168cdf0e10cSrcweir 				if( 0 != (pItem = GetAttrPool().GetItem2( RES_TXTATR_INETFMT, n ) )
169cdf0e10cSrcweir 					&& rCallEvent.PTR.pINetAttr == pItem )
170cdf0e10cSrcweir 				{
171cdf0e10cSrcweir 					bCheckPtr = sal_False;		// als Flag missbrauchen
172cdf0e10cSrcweir 					break;
173cdf0e10cSrcweir 				}
174cdf0e10cSrcweir 		}
175cdf0e10cSrcweir 		if( !bCheckPtr )
176cdf0e10cSrcweir 			pTbl = rCallEvent.PTR.pINetAttr->GetMacroTbl();
177cdf0e10cSrcweir 		break;
178cdf0e10cSrcweir 
179cdf0e10cSrcweir 	case EVENT_OBJECT_URLITEM:
180cdf0e10cSrcweir 	case EVENT_OBJECT_IMAGE:
181cdf0e10cSrcweir 		{
182cdf0e10cSrcweir 			const SwFrmFmtPtr pFmt = (SwFrmFmtPtr)rCallEvent.PTR.pFmt;
183cdf0e10cSrcweir 			if( bCheckPtr )
184cdf0e10cSrcweir 			{
185cdf0e10cSrcweir 				sal_uInt16 nPos = GetSpzFrmFmts()->GetPos( pFmt );
186cdf0e10cSrcweir 				if( USHRT_MAX != nPos )
187cdf0e10cSrcweir 					bCheckPtr = sal_False;		// als Flag missbrauchen
188cdf0e10cSrcweir 			}
189cdf0e10cSrcweir 			if( !bCheckPtr )
190cdf0e10cSrcweir 				pTbl = &pFmt->GetMacro().GetMacroTable();
191cdf0e10cSrcweir 		}
192cdf0e10cSrcweir 		break;
193cdf0e10cSrcweir 
194cdf0e10cSrcweir 	case EVENT_OBJECT_IMAGEMAP:
195cdf0e10cSrcweir 		{
196cdf0e10cSrcweir 			const IMapObject* pIMapObj = rCallEvent.PTR.IMAP.pIMapObj;
197cdf0e10cSrcweir 			if( bCheckPtr )
198cdf0e10cSrcweir 			{
199cdf0e10cSrcweir 				const SwFrmFmtPtr pFmt = (SwFrmFmtPtr)rCallEvent.PTR.IMAP.pFmt;
200cdf0e10cSrcweir 				sal_uInt16 nPos = GetSpzFrmFmts()->GetPos( pFmt );
201cdf0e10cSrcweir 				const ImageMap* pIMap;
202cdf0e10cSrcweir 				if( USHRT_MAX != nPos &&
203cdf0e10cSrcweir 					0 != (pIMap = pFmt->GetURL().GetMap()) )
204cdf0e10cSrcweir 				{
205cdf0e10cSrcweir 					for( nPos = pIMap->GetIMapObjectCount(); nPos; )
206cdf0e10cSrcweir 						if( pIMapObj == pIMap->GetIMapObject( --nPos ))
207cdf0e10cSrcweir 						{
208cdf0e10cSrcweir 							bCheckPtr = sal_False;		// als Flag missbrauchen
209cdf0e10cSrcweir 							break;
210cdf0e10cSrcweir 						}
211cdf0e10cSrcweir 				}
212cdf0e10cSrcweir 			}
213cdf0e10cSrcweir 			if( !bCheckPtr )
214cdf0e10cSrcweir 				pTbl = &pIMapObj->GetMacroTable();
215cdf0e10cSrcweir 		}
216cdf0e10cSrcweir 		break;
217cdf0e10cSrcweir 	default:
218cdf0e10cSrcweir 		break;
219cdf0e10cSrcweir 	}
220cdf0e10cSrcweir 
221cdf0e10cSrcweir 	if( pTbl )
222cdf0e10cSrcweir 	{
223cdf0e10cSrcweir 		nRet = 0x1;
224cdf0e10cSrcweir 		if( pTbl->IsKeyValid( nEvent ) )
225cdf0e10cSrcweir 		{
226cdf0e10cSrcweir 			const SvxMacro& rMacro = *pTbl->Get( nEvent );
227cdf0e10cSrcweir 			if( STARBASIC == rMacro.GetScriptType() )
228cdf0e10cSrcweir 			{
229cdf0e10cSrcweir 				nRet += 0 == pDocShell->CallBasic( rMacro.GetMacName(),
230cdf0e10cSrcweir 									rMacro.GetLibName(), pArgs ) ? 1 : 0;
231cdf0e10cSrcweir 			}
232cdf0e10cSrcweir 			else if( EXTENDED_STYPE == rMacro.GetScriptType() )
233cdf0e10cSrcweir 			{
234cdf0e10cSrcweir 				Sequence<Any> *pUnoArgs = 0;
235cdf0e10cSrcweir 
236cdf0e10cSrcweir 				if( pArgs )
237cdf0e10cSrcweir 				{
238cdf0e10cSrcweir 					pUnoArgs = lcl_docbasic_convertArgs( *pArgs );
239cdf0e10cSrcweir 				}
240cdf0e10cSrcweir 
241cdf0e10cSrcweir 				if (!pUnoArgs)
242cdf0e10cSrcweir 				{
243cdf0e10cSrcweir 					pUnoArgs = new Sequence <Any> (0);
244cdf0e10cSrcweir 				}
245cdf0e10cSrcweir 
246cdf0e10cSrcweir 				Any aRet;
247cdf0e10cSrcweir 				Sequence< sal_Int16 > aOutArgsIndex;
248cdf0e10cSrcweir 				Sequence< Any > aOutArgs;
249cdf0e10cSrcweir 
250cdf0e10cSrcweir 				OSL_TRACE( "SwDoc::CallEvent URL is %s", ByteString(
251cdf0e10cSrcweir 					rMacro.GetMacName(), RTL_TEXTENCODING_UTF8).GetBuffer() );
252cdf0e10cSrcweir 
253cdf0e10cSrcweir 				nRet += 0 == pDocShell->CallXScript(
254cdf0e10cSrcweir 					rMacro.GetMacName(), *pUnoArgs,aRet, aOutArgsIndex, aOutArgs) ? 1 : 0;
255cdf0e10cSrcweir 
256cdf0e10cSrcweir 				//*pRet = pRetValue->GetString();
257cdf0e10cSrcweir 				// use the AnyConverter to return a String if appropriate?
258cdf0e10cSrcweir 
259cdf0e10cSrcweir 				// need to call something like lcl_translateUno2Basic
260cdf0e10cSrcweir 				// pArgs = lcl_translateUno2Basic( pUnoArgs );
261cdf0e10cSrcweir 
262cdf0e10cSrcweir 				delete pUnoArgs;
263cdf0e10cSrcweir 			}
264cdf0e10cSrcweir 			// JavaScript calls are ignored
265cdf0e10cSrcweir 		}
266cdf0e10cSrcweir 	}
267cdf0e10cSrcweir 	return nRet;
268cdf0e10cSrcweir }
269cdf0e10cSrcweir 
270cdf0e10cSrcweir 
271cdf0e10cSrcweir 
272cdf0e10cSrcweir 
273