Stop leaking all ScPostIt instances.
[LibreOffice.git] / sc / source / core / tool / callform.cxx
blob7e3d5af94922a29fa22261c32b63c86c5594cfe5
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 <vcl/svapp.hxx>
21 #include <osl/module.hxx>
22 #include <osl/file.hxx>
23 #include <unotools/transliterationwrapper.hxx>
25 #include "callform.hxx"
26 #include "global.hxx"
27 #include "adiasync.hxx"
29 extern "C" {
31 typedef void (CALLTYPE* ExFuncPtr1)(void*);
32 typedef void (CALLTYPE* ExFuncPtr2)(void*, void*);
33 typedef void (CALLTYPE* ExFuncPtr3)(void*, void*, void*);
34 typedef void (CALLTYPE* ExFuncPtr4)(void*, void*, void*, void*);
35 typedef void (CALLTYPE* ExFuncPtr5)(void*, void*, void*, void*, void*);
36 typedef void (CALLTYPE* ExFuncPtr6)(void*, void*, void*, void*, void*, void*);
37 typedef void (CALLTYPE* ExFuncPtr7)(void*, void*, void*, void*, void*, void*, void*);
38 typedef void (CALLTYPE* ExFuncPtr8)(void*, void*, void*, void*, void*, void*, void*, void*);
39 typedef void (CALLTYPE* ExFuncPtr9)(void*, void*, void*, void*, void*, void*, void*, void*, void*);
40 typedef void (CALLTYPE* ExFuncPtr10)(void*, void*, void*, void*, void*, void*, void*, void*, void*, void*);
41 typedef void (CALLTYPE* ExFuncPtr11)(void*, void*, void*, void*, void*, void*, void*, void*, void*, void*, void*);
42 typedef void (CALLTYPE* ExFuncPtr12)(void*, void*, void*, void*, void*, void*, void*, void*, void*, void*, void*, void*);
43 typedef void (CALLTYPE* ExFuncPtr13)(void*, void*, void*, void*, void*, void*, void*, void*, void*, void*, void*, void*, void*);
44 typedef void (CALLTYPE* ExFuncPtr14)(void*, void*, void*, void*, void*, void*, void*, void*, void*, void*, void*, void*, void*, void*);
45 typedef void (CALLTYPE* ExFuncPtr15)(void*, void*, void*, void*, void*, void*, void*, void*, void*, void*, void*, void*, void*, void*, void*);
46 typedef void (CALLTYPE* ExFuncPtr16)(void*, void*, void*, void*, void*, void*, void*, void*, void*, void*, void*, void*, void*, void*, void*, void*);
48 typedef void (CALLTYPE* GetFuncCountPtr)(sal_uInt16& nCount);
49 typedef void (CALLTYPE* GetFuncDataPtr)
50 (sal_uInt16& nNo, sal_Char* pFuncName, sal_uInt16& nParamCount, ParamType* peType, sal_Char* pInternalName);
52 typedef void (CALLTYPE* SetLanguagePtr)( sal_uInt16& nLanguage );
53 typedef void (CALLTYPE* GetParamDesc)
54 (sal_uInt16& nNo, sal_uInt16& nParam, sal_Char* pName, sal_Char* pDesc );
56 typedef void (CALLTYPE* IsAsync) ( sal_uInt16& nNo,
57 ParamType* peType );
58 typedef void (CALLTYPE* Advice) ( sal_uInt16& nNo,
59 AdvData& pfCallback );
60 typedef void (CALLTYPE* Unadvice)( double& nHandle );
62 typedef void (CALLTYPE* FARPROC) ( void );
66 #ifndef DISABLE_DYNLOADING
67 #define GETFUNCTIONCOUNT "GetFunctionCount"
68 #define GETFUNCTIONDATA "GetFunctionData"
69 #define SETLANGUAGE "SetLanguage"
70 #define GETPARAMDESC "GetParameterDescription"
71 #define ISASYNC "IsAsync"
72 #define ADVICE "Advice"
73 #define UNADVICE "Unadvice"
74 #endif
76 class ModuleData
78 friend class ModuleCollection;
79 OUString aName;
80 osl::Module* pInstance;
81 public:
82 ModuleData(const OUString& rStr, osl::Module* pInst) : aName(rStr), pInstance(pInst) {}
83 ModuleData(const ModuleData& rData) : aName(rData.aName)
85 #ifndef DISABLE_DYNLOADING
86 pInstance = new osl::Module(aName);
87 #else
88 pInstance = NULL;
89 #endif
91 ~ModuleData() { delete pInstance; }
93 const OUString& GetName() const { return aName; }
94 osl::Module* GetInstance() const { return pInstance; }
95 void FreeInstance() { delete pInstance; pInstance = 0; }
98 FuncData::FuncData(const ModuleData*pModule,
99 const OUString& rIName,
100 const OUString& rFName,
101 sal_uInt16 nNo,
102 sal_uInt16 nCount,
103 const ParamType* peType,
104 ParamType eType) :
105 pModuleData (pModule),
106 aInternalName (rIName),
107 aFuncName (rFName),
108 nNumber (nNo),
109 nParamCount (nCount),
110 eAsyncType (eType)
112 for (sal_uInt16 i = 0; i < MAXFUNCPARAM; i++)
113 eParamType[i] = peType[i];
116 FuncData::FuncData(const FuncData& rData) :
117 pModuleData (rData.pModuleData),
118 aInternalName (rData.aInternalName),
119 aFuncName (rData.aFuncName),
120 nNumber (rData.nNumber),
121 nParamCount (rData.nParamCount),
122 eAsyncType (rData.eAsyncType)
124 for (sal_uInt16 i = 0; i < MAXFUNCPARAM; i++)
125 eParamType[i] = rData.eParamType[i];
128 namespace {
130 class ModuleCollection
132 typedef boost::ptr_map<OUString, ModuleData> MapType;
133 MapType maData;
134 public:
135 ModuleCollection() {}
136 ModuleCollection(const ModuleCollection& r) : maData(r.maData) {}
138 const ModuleData* findByName(const OUString& rName) const;
139 void insert(ModuleData* pNew);
140 void clear();
143 const ModuleData* ModuleCollection::findByName(const OUString& rName) const
145 MapType::const_iterator it = maData.find(rName);
146 return it == maData.end() ? NULL : it->second;
149 void ModuleCollection::insert(ModuleData* pNew)
151 if (!pNew)
152 return;
154 OUString aName = pNew->GetName();
155 maData.insert(aName, pNew);
158 void ModuleCollection::clear()
160 maData.clear();
163 ModuleCollection aModuleCollection;
167 bool InitExternalFunc(const OUString& rModuleName)
169 #ifdef DISABLE_DYNLOADING
170 (void) rModuleName;
171 return false;
172 #else
173 // Module already loaded?
174 const ModuleData* pTemp = aModuleCollection.findByName(rModuleName);
175 if (pTemp)
176 return false;
178 OUString aNP;
179 aNP = rModuleName;
181 bool bRet = false;
182 osl::Module* pLib = new osl::Module( aNP );
183 if (pLib->is())
185 FARPROC fpGetCount = (FARPROC)pLib->getFunctionSymbol(GETFUNCTIONCOUNT);
186 FARPROC fpGetData = (FARPROC)pLib->getFunctionSymbol(GETFUNCTIONDATA);
187 if ((fpGetCount != NULL) && (fpGetData != NULL))
189 FARPROC fpIsAsync = (FARPROC)pLib->getFunctionSymbol(ISASYNC);
190 FARPROC fpAdvice = (FARPROC)pLib->getFunctionSymbol(ADVICE);
191 FARPROC fpSetLanguage = (FARPROC)pLib->getFunctionSymbol(SETLANGUAGE);
192 if ( fpSetLanguage )
194 LanguageType eLanguage = Application::GetSettings().GetUILanguageTag().getLanguageType();
195 sal_uInt16 nLanguage = (sal_uInt16) eLanguage;
196 (*((SetLanguagePtr)fpSetLanguage))( nLanguage );
199 // Module in die Collection aufnehmen
200 ModuleData* pModuleData = new ModuleData(rModuleName, pLib);
201 aModuleCollection.insert(pModuleData);
203 // Schnittstelle initialisieren
204 AdvData pfCallBack = &ScAddInAsyncCallBack;
205 FuncData* pFuncData;
206 FuncCollection* pFuncCol = ScGlobal::GetFuncCollection();
207 sal_uInt16 nCount;
208 (*((GetFuncCountPtr)fpGetCount))(nCount);
209 for (sal_uInt16 i=0; i < nCount; i++)
211 sal_Char cFuncName[256];
212 sal_Char cInternalName[256];
213 sal_uInt16 nParamCount;
214 ParamType eParamType[MAXFUNCPARAM];
215 ParamType eAsyncType = NONE;
216 // initialize all, in case the AddIn behaves bad
217 cFuncName[0] = 0;
218 cInternalName[0] = 0;
219 nParamCount = 0;
220 for ( sal_uInt16 j=0; j<MAXFUNCPARAM; j++ )
222 eParamType[j] = NONE;
224 (*((GetFuncDataPtr)fpGetData))(i, cFuncName, nParamCount,
225 eParamType, cInternalName);
226 if( fpIsAsync )
228 (*((IsAsync)fpIsAsync))(i, &eAsyncType);
229 if ( fpAdvice && eAsyncType != NONE )
230 (*((Advice)fpAdvice))( i, pfCallBack );
232 OUString aInternalName( cInternalName, strlen(cInternalName), osl_getThreadTextEncoding() );
233 OUString aFuncName( cFuncName, strlen(cFuncName), osl_getThreadTextEncoding() );
234 pFuncData = new FuncData( pModuleData,
235 aInternalName,
236 aFuncName,
238 nParamCount,
239 eParamType,
240 eAsyncType );
241 pFuncCol->insert(pFuncData);
243 bRet = sal_True;
245 else
246 delete pLib;
248 else
249 delete pLib;
250 return bRet;
251 #endif
254 void ExitExternalFunc()
256 aModuleCollection.clear();
259 bool FuncData::Call(void** ppParam) const
261 #ifdef DISABLE_DYNLOADING
262 (void) ppParam;
263 return false;
264 #else
265 bool bRet = false;
266 osl::Module* pLib = pModuleData->GetInstance();
267 FARPROC fProc = (FARPROC)pLib->getFunctionSymbol(aFuncName);
268 if (fProc != NULL)
270 switch (nParamCount)
272 case 1 :
273 (*((ExFuncPtr1)fProc))(ppParam[0]);
274 bRet = true;
275 break;
276 case 2 :
277 (*((ExFuncPtr2)fProc))(ppParam[0], ppParam[1]);
278 bRet = true;
279 break;
280 case 3 :
281 (*((ExFuncPtr3)fProc))(ppParam[0], ppParam[1], ppParam[2]);
282 bRet = true;
283 break;
284 case 4 :
285 (*((ExFuncPtr4)fProc))(ppParam[0], ppParam[1], ppParam[2], ppParam[3]);
286 bRet = true;
287 break;
288 case 5 :
289 (*((ExFuncPtr5)fProc))(ppParam[0], ppParam[1], ppParam[2], ppParam[3], ppParam[4]);
290 bRet = true;
291 break;
292 case 6 :
293 (*((ExFuncPtr6)fProc))(ppParam[0], ppParam[1], ppParam[2], ppParam[3], ppParam[4], ppParam[5]);
294 bRet = true;
295 break;
296 case 7 :
297 (*((ExFuncPtr7)fProc))( ppParam[0], ppParam[1], ppParam[2], ppParam[3], ppParam[4], ppParam[5],
298 ppParam[6]);
299 bRet = true;
300 break;
301 case 8 :
302 (*((ExFuncPtr8)fProc))( ppParam[0], ppParam[1], ppParam[2], ppParam[3], ppParam[4], ppParam[5],
303 ppParam[6], ppParam[7]);
304 bRet = true;
305 break;
306 case 9 :
307 (*((ExFuncPtr9)fProc))( ppParam[0], ppParam[1], ppParam[2], ppParam[3], ppParam[4], ppParam[5],
308 ppParam[6], ppParam[7], ppParam[8]);
309 bRet = true;
310 break;
311 case 10 :
312 (*((ExFuncPtr10)fProc))( ppParam[0], ppParam[1], ppParam[2], ppParam[3], ppParam[4], ppParam[5],
313 ppParam[6], ppParam[7], ppParam[8], ppParam[9]);
314 bRet = true;
315 break;
316 case 11 :
317 (*((ExFuncPtr11)fProc))( ppParam[0], ppParam[1], ppParam[2], ppParam[3], ppParam[4], ppParam[5],
318 ppParam[6], ppParam[7], ppParam[8], ppParam[9], ppParam[10]);
319 bRet = true;
320 break;
321 case 12:
322 (*((ExFuncPtr12)fProc))( ppParam[0], ppParam[1], ppParam[2], ppParam[3], ppParam[4], ppParam[5],
323 ppParam[6], ppParam[7], ppParam[8], ppParam[9], ppParam[10], ppParam[11]);
324 bRet = true;
325 break;
326 case 13:
327 (*((ExFuncPtr13)fProc))( ppParam[0], ppParam[1], ppParam[2], ppParam[3], ppParam[4], ppParam[5],
328 ppParam[6], ppParam[7], ppParam[8], ppParam[9], ppParam[10], ppParam[11],
329 ppParam[12]);
330 bRet = true;
331 break;
332 case 14 :
333 (*((ExFuncPtr14)fProc))( ppParam[0], ppParam[1], ppParam[2], ppParam[3], ppParam[4], ppParam[5],
334 ppParam[6], ppParam[7], ppParam[8], ppParam[9], ppParam[10], ppParam[11],
335 ppParam[12], ppParam[13]);
336 bRet = true;
337 break;
338 case 15 :
339 (*((ExFuncPtr15)fProc))( ppParam[0], ppParam[1], ppParam[2], ppParam[3], ppParam[4], ppParam[5],
340 ppParam[6], ppParam[7], ppParam[8], ppParam[9], ppParam[10], ppParam[11],
341 ppParam[12], ppParam[13], ppParam[14]);
342 bRet = true;
343 break;
344 case 16 :
345 (*((ExFuncPtr16)fProc))( ppParam[0], ppParam[1], ppParam[2], ppParam[3], ppParam[4], ppParam[5],
346 ppParam[6], ppParam[7], ppParam[8], ppParam[9], ppParam[10], ppParam[11],
347 ppParam[12], ppParam[13], ppParam[14], ppParam[15]);
348 bRet = true;
349 break;
350 default : break;
353 return bRet;
354 #endif
357 bool FuncData::Unadvice( double nHandle )
359 #ifdef DISABLE_DYNLOADING
360 (void) nHandle;
361 return false;
362 #else
363 bool bRet = false;
364 osl::Module* pLib = pModuleData->GetInstance();
365 FARPROC fProc = (FARPROC)pLib->getFunctionSymbol(UNADVICE);
366 if (fProc != NULL)
368 ((::Unadvice)fProc)(nHandle);
369 bRet = true;
371 return bRet;
372 #endif
375 const OUString& FuncData::GetModuleName() const
377 return pModuleData->GetName();
380 bool FuncData::getParamDesc( OUString& aName, OUString& aDesc, sal_uInt16 nParam ) const
382 #ifdef DISABLE_DYNLOADING
383 (void) aName;
384 (void) aDesc;
385 (void) nParam;
386 return false;
387 #else
388 bool bRet = false;
389 if ( nParam <= nParamCount )
391 osl::Module* pLib = pModuleData->GetInstance();
392 FARPROC fProc = (FARPROC) pLib->getFunctionSymbol(GETPARAMDESC);
393 if ( fProc != NULL )
395 sal_Char pcName[256];
396 sal_Char pcDesc[256];
397 *pcName = *pcDesc = 0;
398 sal_uInt16 nFuncNo = nNumber; // nicht per Reference versauen lassen..
399 ((::GetParamDesc)fProc)( nFuncNo, nParam, pcName, pcDesc );
400 aName = OUString( pcName, 256, osl_getThreadTextEncoding() );
401 aDesc = OUString( pcDesc, 256, osl_getThreadTextEncoding() );
402 bRet = true;
405 if ( !bRet )
407 aName = OUString();
408 aDesc = OUString();
410 return bRet;
411 #endif
414 FuncCollection::FuncCollection() {}
415 FuncCollection::FuncCollection(const FuncCollection& r) : maData(r.maData) {}
417 const FuncData* FuncCollection::findByName(const OUString& rName) const
419 MapType::const_iterator it = maData.find(rName);
420 return it == maData.end() ? NULL : it->second;
423 FuncData* FuncCollection::findByName(const OUString& rName)
425 MapType::iterator it = maData.find(rName);
426 return it == maData.end() ? NULL : it->second;
429 void FuncCollection::insert(FuncData* pNew)
431 OUString aName = pNew->GetInternalName();
432 maData.insert(aName, pNew);
435 FuncCollection::const_iterator FuncCollection::begin() const
437 return maData.begin();
440 FuncCollection::const_iterator FuncCollection::end() const
442 return maData.end();
445 /* vim:set shiftwidth=4 softtabstop=4 expandtab: */