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