update dev300-m58
[ooovba.git] / sc / source / core / tool / callform.cxx
blob6445bffcb449117ec8475811d60c56b50d0a2621
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: callform.cxx,v $
10 * $Revision: 1.12.32.1 $
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_sc.hxx"
36 // INCLUDE ---------------------------------------------------------------
37 #include <vcl/svapp.hxx>
38 #include <osl/module.hxx>
39 #include <osl/file.hxx>
40 #include <unotools/transliterationwrapper.hxx>
42 #include "callform.hxx"
43 #include "global.hxx"
44 #include "adiasync.hxx"
46 //------------------------------------------------------------------------
48 extern "C" {
50 typedef void (CALLTYPE* ExFuncPtr1)(void*);
51 typedef void (CALLTYPE* ExFuncPtr2)(void*, void*);
52 typedef void (CALLTYPE* ExFuncPtr3)(void*, void*, void*);
53 typedef void (CALLTYPE* ExFuncPtr4)(void*, void*, void*, void*);
54 typedef void (CALLTYPE* ExFuncPtr5)(void*, void*, void*, void*, void*);
55 typedef void (CALLTYPE* ExFuncPtr6)(void*, void*, void*, void*, void*, void*);
56 typedef void (CALLTYPE* ExFuncPtr7)(void*, void*, void*, void*, void*, void*, void*);
57 typedef void (CALLTYPE* ExFuncPtr8)(void*, void*, void*, void*, void*, void*, void*, void*);
58 typedef void (CALLTYPE* ExFuncPtr9)(void*, void*, void*, void*, void*, void*, void*, void*, void*);
59 typedef void (CALLTYPE* ExFuncPtr10)(void*, void*, void*, void*, void*, void*, void*, void*, void*, void*);
60 typedef void (CALLTYPE* ExFuncPtr11)(void*, void*, void*, void*, void*, void*, void*, void*, void*, void*, void*);
61 typedef void (CALLTYPE* ExFuncPtr12)(void*, void*, void*, void*, void*, void*, void*, void*, void*, void*, void*, void*);
62 typedef void (CALLTYPE* ExFuncPtr13)(void*, void*, void*, void*, void*, void*, void*, void*, void*, void*, void*, void*, void*);
63 typedef void (CALLTYPE* ExFuncPtr14)(void*, void*, void*, void*, void*, void*, void*, void*, void*, void*, void*, void*, void*, void*);
64 typedef void (CALLTYPE* ExFuncPtr15)(void*, void*, void*, void*, void*, void*, void*, void*, void*, void*, void*, void*, void*, void*, void*);
65 typedef void (CALLTYPE* ExFuncPtr16)(void*, void*, void*, void*, void*, void*, void*, void*, void*, void*, void*, void*, void*, void*, void*, void*);
67 typedef void (CALLTYPE* GetFuncCountPtr)(USHORT& nCount);
68 typedef void (CALLTYPE* GetFuncDataPtr)
69 (USHORT& nNo, sal_Char* pFuncName, USHORT& nParamCount, ParamType* peType, sal_Char* pInternalName);
71 typedef void (CALLTYPE* SetLanguagePtr)( USHORT& nLanguage );
72 typedef void (CALLTYPE* GetParamDesc)
73 (USHORT& nNo, USHORT& nParam, sal_Char* pName, sal_Char* pDesc );
75 typedef void (CALLTYPE* IsAsync) ( USHORT& nNo,
76 ParamType* peType );
77 typedef void (CALLTYPE* Advice) ( USHORT& nNo,
78 AdvData& pfCallback );
79 typedef void (CALLTYPE* Unadvice)( double& nHandle );
81 typedef void (CALLTYPE* FARPROC) ( void );
85 #if defined(OS2) && defined(BLC)
86 #define GETFUNCTIONCOUNT "_GetFunctionCount"
87 #define GETFUNCTIONDATA "_GetFunctionData"
88 #define SETLANGUAGE "_SetLanguage"
89 #define GETPARAMDESC "_GetParameterDescription"
90 #define ISASYNC "_IsAsync"
91 #define ADVICE "_Advice"
92 #define UNADVICE "_Unadvice"
93 #else // Pascal oder extern "C"
94 #define GETFUNCTIONCOUNT "GetFunctionCount"
95 #define GETFUNCTIONDATA "GetFunctionData"
96 #define SETLANGUAGE "SetLanguage"
97 #define GETPARAMDESC "GetParameterDescription"
98 #define ISASYNC "IsAsync"
99 #define ADVICE "Advice"
100 #define UNADVICE "Unadvice"
101 #endif
103 #define LIBFUNCNAME( name ) \
104 (String::CreateFromAscii( RTL_CONSTASCII_STRINGPARAM( name ) ))
106 //-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=
108 FuncData::FuncData(const String& rIName) :
109 pModuleData (NULL),
110 aInternalName (rIName),
111 // aFuncName (""),
112 nNumber (0),
113 nParamCount (0),
114 eAsyncType (NONE)
116 for (USHORT i = 0; i < MAXFUNCPARAM; i++)
117 eParamType[i] = PTR_DOUBLE;
120 //------------------------------------------------------------------------
122 FuncData::FuncData(const ModuleData*pModule,
123 const String& rIName,
124 const String& rFName,
125 USHORT nNo,
126 USHORT nCount,
127 const ParamType* peType,
128 ParamType eType) :
129 pModuleData (pModule),
130 aInternalName (rIName),
131 aFuncName (rFName),
132 nNumber (nNo),
133 nParamCount (nCount),
134 eAsyncType (eType)
136 for (USHORT i = 0; i < MAXFUNCPARAM; i++)
137 eParamType[i] = peType[i];
140 //------------------------------------------------------------------------
142 FuncData::FuncData(const FuncData& rData) :
143 ScDataObject(),
144 pModuleData (rData.pModuleData),
145 aInternalName (rData.aInternalName),
146 aFuncName (rData.aFuncName),
147 nNumber (rData.nNumber),
148 nParamCount (rData.nParamCount),
149 eAsyncType (rData.eAsyncType)
151 for (USHORT i = 0; i < MAXFUNCPARAM; i++)
152 eParamType[i] = rData.eParamType[i];
155 //-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=
157 short FuncCollection::Compare(ScDataObject* pKey1, ScDataObject* pKey2) const
159 return (short) ScGlobal::GetpTransliteration()->compareString(
160 ((FuncData*)pKey1)->aInternalName, ((FuncData*)pKey2)->aInternalName );
163 //------------------------------------------------------------------------
165 BOOL FuncCollection::SearchFunc( const String& rName, USHORT& rIndex ) const
167 FuncData aDataObj(rName);
168 return Search( &aDataObj, rIndex );
171 //-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=
172 class ModuleData : public ScDataObject
174 friend class ModuleCollection;
175 String aName;
176 osl::Module* pInstance;
177 public:
178 ModuleData(const String& rStr, osl::Module* pInst) : aName (rStr), pInstance (pInst) {}
179 ModuleData(const ModuleData& rData) : ScDataObject(), aName (rData.aName) {pInstance = new osl::Module(aName);}
180 ~ModuleData() { delete pInstance; }
181 virtual ScDataObject* Clone() const { return new ModuleData(*this); }
183 const String& GetName() const { return aName; }
184 osl::Module* GetInstance() const { return pInstance; }
185 void FreeInstance() { delete pInstance; pInstance = 0; }
188 //-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=
189 class ModuleCollection : public ScSortedCollection
191 public:
192 ModuleCollection(USHORT nLim = 4, USHORT nDel = 4, BOOL bDup = FALSE) : ScSortedCollection ( nLim, nDel, bDup ) {}
193 ModuleCollection(const ModuleCollection& rModuleCollection) : ScSortedCollection ( rModuleCollection ) {}
195 virtual ScDataObject* Clone() const { return new ModuleCollection(*this); }
196 ModuleData* operator[]( const USHORT nIndex) const {return (ModuleData*)At(nIndex);}
197 virtual short Compare(ScDataObject* pKey1, ScDataObject* pKey2) const;
198 BOOL SearchModule( const String& rName,
199 const ModuleData*& rpModule ) const;
202 static ModuleCollection aModuleCollection;
204 //------------------------------------------------------------------------
206 short ModuleCollection::Compare(ScDataObject* pKey1, ScDataObject* pKey2) const
208 return (short) ScGlobal::GetpTransliteration()->compareString(
209 ((ModuleData*)pKey1)->aName, ((ModuleData*)pKey2)->aName );
212 //------------------------------------------------------------------------
214 BOOL ModuleCollection::SearchModule( const String& rName,
215 const ModuleData*& rpModule ) const
217 USHORT nIndex;
218 ModuleData aSearchModule(rName, 0);
219 BOOL bFound = Search( &aSearchModule, nIndex );
220 if (bFound)
221 rpModule = (ModuleData*)At(nIndex);
222 else
223 rpModule = 0;
224 return bFound;
227 //-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=
229 BOOL InitExternalFunc(const rtl::OUString& rModuleName)
231 String aModuleName( rModuleName );
233 // Module schon geladen?
234 const ModuleData* pTemp;
235 if (aModuleCollection.SearchModule(aModuleName, pTemp))
236 return FALSE;
238 rtl::OUString aNP;
239 aNP = rModuleName;
241 BOOL bRet = FALSE;
242 osl::Module* pLib = new osl::Module( aNP );
243 if (pLib->is())
245 FARPROC fpGetCount = (FARPROC)pLib->getFunctionSymbol(LIBFUNCNAME(GETFUNCTIONCOUNT));
246 FARPROC fpGetData = (FARPROC)pLib->getFunctionSymbol(LIBFUNCNAME(GETFUNCTIONDATA));
247 if ((fpGetCount != NULL) && (fpGetData != NULL))
249 FARPROC fpIsAsync = (FARPROC)pLib->getFunctionSymbol(LIBFUNCNAME(ISASYNC));
250 FARPROC fpAdvice = (FARPROC)pLib->getFunctionSymbol(LIBFUNCNAME(ADVICE));
251 FARPROC fpSetLanguage = (FARPROC)pLib->getFunctionSymbol(LIBFUNCNAME(SETLANGUAGE));
252 if ( fpSetLanguage )
254 LanguageType eLanguage = Application::GetSettings().GetUILanguage();
255 USHORT nLanguage = (USHORT) eLanguage;
256 (*((SetLanguagePtr)fpSetLanguage))( nLanguage );
259 // Module in die Collection aufnehmen
260 ModuleData* pModuleData = new ModuleData(aModuleName, pLib);
261 aModuleCollection.Insert(pModuleData);
263 // Schnittstelle initialisieren
264 AdvData pfCallBack = &ScAddInAsyncCallBack;
265 FuncData* pFuncData;
266 FuncCollection* pFuncCol = ScGlobal::GetFuncCollection();
267 USHORT nCount;
268 (*((GetFuncCountPtr)fpGetCount))(nCount);
269 for (USHORT i=0; i < nCount; i++)
271 sal_Char cFuncName[256];
272 sal_Char cInternalName[256];
273 USHORT nParamCount;
274 ParamType eParamType[MAXFUNCPARAM];
275 ParamType eAsyncType = NONE;
276 // #62113# alles initialisieren, falls das AddIn sich schlecht verhaelt
277 cFuncName[0] = 0;
278 cInternalName[0] = 0;
279 nParamCount = 0;
280 for ( USHORT j=0; j<MAXFUNCPARAM; j++ )
282 eParamType[j] = NONE;
284 (*((GetFuncDataPtr)fpGetData))(i, cFuncName, nParamCount,
285 eParamType, cInternalName);
286 if( fpIsAsync )
288 (*((IsAsync)fpIsAsync))(i, &eAsyncType);
289 if ( fpAdvice && eAsyncType != NONE )
290 (*((Advice)fpAdvice))( i, pfCallBack );
292 String aInternalName( cInternalName, osl_getThreadTextEncoding() );
293 String aFuncName( cFuncName, osl_getThreadTextEncoding() );
294 pFuncData = new FuncData( pModuleData,
295 aInternalName,
296 aFuncName,
298 nParamCount,
299 eParamType,
300 eAsyncType );
301 pFuncCol->Insert(pFuncData);
303 bRet = TRUE;
305 else
306 delete pLib;
308 else
309 delete pLib;
310 return bRet;
313 //------------------------------------------------------------------------
315 void ExitExternalFunc()
317 USHORT nCount = aModuleCollection.GetCount();
318 for (USHORT i=0; i<nCount; i++)
320 ModuleData* pData = aModuleCollection[i];
321 pData->FreeInstance();
325 //------------------------------------------------------------------------
327 BOOL FuncData::Call(void** ppParam)
329 BOOL bRet = FALSE;
330 osl::Module* pLib = pModuleData->GetInstance();
331 FARPROC fProc = (FARPROC)pLib->getFunctionSymbol(aFuncName);
332 if (fProc != NULL)
334 switch (nParamCount)
336 case 1 :
337 (*((ExFuncPtr1)fProc))(ppParam[0]);
338 bRet = TRUE;
339 break;
340 case 2 :
341 (*((ExFuncPtr2)fProc))(ppParam[0], ppParam[1]);
342 bRet = TRUE;
343 break;
344 case 3 :
345 (*((ExFuncPtr3)fProc))(ppParam[0], ppParam[1], ppParam[2]);
346 bRet = TRUE;
347 break;
348 case 4 :
349 (*((ExFuncPtr4)fProc))(ppParam[0], ppParam[1], ppParam[2], ppParam[3]);
350 bRet = TRUE;
351 break;
352 case 5 :
353 (*((ExFuncPtr5)fProc))(ppParam[0], ppParam[1], ppParam[2], ppParam[3], ppParam[4]);
354 bRet = TRUE;
355 break;
356 case 6 :
357 (*((ExFuncPtr6)fProc))(ppParam[0], ppParam[1], ppParam[2], ppParam[3], ppParam[4], ppParam[5]);
358 bRet = TRUE;
359 break;
360 case 7 :
361 (*((ExFuncPtr7)fProc))( ppParam[0], ppParam[1], ppParam[2], ppParam[3], ppParam[4], ppParam[5],
362 ppParam[6]);
363 bRet = TRUE;
364 break;
365 case 8 :
366 (*((ExFuncPtr8)fProc))( ppParam[0], ppParam[1], ppParam[2], ppParam[3], ppParam[4], ppParam[5],
367 ppParam[6], ppParam[7]);
368 bRet = TRUE;
369 break;
370 case 9 :
371 (*((ExFuncPtr9)fProc))( ppParam[0], ppParam[1], ppParam[2], ppParam[3], ppParam[4], ppParam[5],
372 ppParam[6], ppParam[7], ppParam[8]);
373 bRet = TRUE;
374 break;
375 case 10 :
376 (*((ExFuncPtr10)fProc))( ppParam[0], ppParam[1], ppParam[2], ppParam[3], ppParam[4], ppParam[5],
377 ppParam[6], ppParam[7], ppParam[8], ppParam[9]);
378 bRet = TRUE;
379 break;
380 case 11 :
381 (*((ExFuncPtr11)fProc))( ppParam[0], ppParam[1], ppParam[2], ppParam[3], ppParam[4], ppParam[5],
382 ppParam[6], ppParam[7], ppParam[8], ppParam[9], ppParam[10]);
383 bRet = TRUE;
384 break;
385 case 12:
386 (*((ExFuncPtr12)fProc))( ppParam[0], ppParam[1], ppParam[2], ppParam[3], ppParam[4], ppParam[5],
387 ppParam[6], ppParam[7], ppParam[8], ppParam[9], ppParam[10], ppParam[11]);
388 bRet = TRUE;
389 break;
390 case 13:
391 (*((ExFuncPtr13)fProc))( ppParam[0], ppParam[1], ppParam[2], ppParam[3], ppParam[4], ppParam[5],
392 ppParam[6], ppParam[7], ppParam[8], ppParam[9], ppParam[10], ppParam[11],
393 ppParam[12]);
394 bRet = TRUE;
395 break;
396 case 14 :
397 (*((ExFuncPtr14)fProc))( ppParam[0], ppParam[1], ppParam[2], ppParam[3], ppParam[4], ppParam[5],
398 ppParam[6], ppParam[7], ppParam[8], ppParam[9], ppParam[10], ppParam[11],
399 ppParam[12], ppParam[13]);
400 bRet = TRUE;
401 break;
402 case 15 :
403 (*((ExFuncPtr15)fProc))( ppParam[0], ppParam[1], ppParam[2], ppParam[3], ppParam[4], ppParam[5],
404 ppParam[6], ppParam[7], ppParam[8], ppParam[9], ppParam[10], ppParam[11],
405 ppParam[12], ppParam[13], ppParam[14]);
406 bRet = TRUE;
407 break;
408 case 16 :
409 (*((ExFuncPtr16)fProc))( ppParam[0], ppParam[1], ppParam[2], ppParam[3], ppParam[4], ppParam[5],
410 ppParam[6], ppParam[7], ppParam[8], ppParam[9], ppParam[10], ppParam[11],
411 ppParam[12], ppParam[13], ppParam[14], ppParam[15]);
412 bRet = TRUE;
413 break;
414 default : break;
417 return bRet;
420 //------------------------------------------------------------------------
422 BOOL FuncData::Unadvice( double nHandle )
424 BOOL bRet = FALSE;
425 osl::Module* pLib = pModuleData->GetInstance();
426 FARPROC fProc = (FARPROC)pLib->getFunctionSymbol(LIBFUNCNAME(UNADVICE));
427 if (fProc != NULL)
429 ((::Unadvice)fProc)(nHandle);
430 bRet = TRUE;
432 return bRet;
435 //------------------------------------------------------------------------
437 const String& FuncData::GetModuleName() const
439 // DBG_ASSERT( pModuleData, "Keine Arme, keine Kekse" ):
440 return pModuleData->GetName();
443 //------------------------------------------------------------------------
445 BOOL FuncData::GetParamDesc( String& aName, String& aDesc, USHORT nParam )
447 BOOL bRet = FALSE;
448 if ( nParam <= nParamCount )
450 osl::Module* pLib = pModuleData->GetInstance();
451 FARPROC fProc = (FARPROC) pLib->getFunctionSymbol( LIBFUNCNAME(GETPARAMDESC) );
452 if ( fProc != NULL )
454 sal_Char pcName[256];
455 sal_Char pcDesc[256];
456 *pcName = *pcDesc = 0;
457 USHORT nFuncNo = nNumber; // nicht per Reference versauen lassen..
458 ((::GetParamDesc)fProc)( nFuncNo, nParam, pcName, pcDesc );
459 aName = String( pcName, osl_getThreadTextEncoding() );
460 aDesc = String( pcDesc, osl_getThreadTextEncoding() );
461 bRet = TRUE;
464 if ( !bRet )
466 aName.Erase();
467 aDesc.Erase();
469 return bRet;