tdf#130857 qt weld: Implement QtInstanceWidget::strip_mnemonic
[LibreOffice.git] / sc / source / core / tool / callform.cxx
blob2c441164cfdcd2881664e404633d9247333efd23
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 <sal/config.h>
22 #include <utility>
23 #include <vcl/svapp.hxx>
24 #include <vcl/settings.hxx>
25 #include <osl/module.hxx>
26 #include <i18nlangtag/languagetag.hxx>
27 #include <memory>
29 #include <callform.hxx>
30 #include <global.hxx>
31 #include <adiasync.hxx>
33 extern "C" {
35 typedef void (CALLTYPE* ExFuncPtr1)(void*);
36 typedef void (CALLTYPE* ExFuncPtr2)(void*, void*);
37 typedef void (CALLTYPE* ExFuncPtr3)(void*, void*, void*);
38 typedef void (CALLTYPE* ExFuncPtr4)(void*, void*, void*, void*);
39 typedef void (CALLTYPE* ExFuncPtr5)(void*, void*, void*, void*, void*);
40 typedef void (CALLTYPE* ExFuncPtr6)(void*, void*, void*, void*, void*, void*);
41 typedef void (CALLTYPE* ExFuncPtr7)(void*, void*, void*, void*, void*, void*, void*);
42 typedef void (CALLTYPE* ExFuncPtr8)(void*, void*, void*, void*, void*, void*, void*, void*);
43 typedef void (CALLTYPE* ExFuncPtr9)(void*, void*, void*, void*, void*, void*, void*, void*, void*);
44 typedef void (CALLTYPE* ExFuncPtr10)(void*, void*, void*, void*, void*, void*, void*, void*, void*, void*);
45 typedef void (CALLTYPE* ExFuncPtr11)(void*, void*, void*, void*, void*, void*, void*, void*, void*, void*, void*);
46 typedef void (CALLTYPE* ExFuncPtr12)(void*, void*, void*, void*, void*, void*, void*, void*, void*, void*, void*, void*);
47 typedef void (CALLTYPE* ExFuncPtr13)(void*, void*, void*, void*, void*, void*, void*, void*, void*, void*, void*, void*, void*);
48 typedef void (CALLTYPE* ExFuncPtr14)(void*, void*, void*, void*, void*, void*, void*, void*, void*, void*, void*, void*, void*, void*);
49 typedef void (CALLTYPE* ExFuncPtr15)(void*, void*, void*, void*, void*, void*, void*, void*, void*, void*, void*, void*, void*, void*, void*);
50 typedef void (CALLTYPE* ExFuncPtr16)(void*, void*, void*, void*, void*, void*, void*, void*, void*, void*, void*, void*, void*, void*, void*, void*);
52 typedef void (CALLTYPE* GetFuncCountPtr)(sal_uInt16& nCount);
53 typedef void (CALLTYPE* GetFuncDataPtr)
54 (sal_uInt16& nNo, char* pFuncName, sal_uInt16& nParamCount, ParamType* peType, char* pInternalName);
56 typedef void (CALLTYPE* SetLanguagePtr)( sal_uInt16& nLanguage );
57 typedef void (CALLTYPE* GetParamDesc)
58 (sal_uInt16& nNo, sal_uInt16& nParam, char* pName, char* pDesc );
60 typedef void (CALLTYPE* IsAsync) ( sal_uInt16& nNo,
61 ParamType* peType );
62 typedef void (CALLTYPE* Advice) ( sal_uInt16& nNo,
63 AdvData& pfCallback );
64 typedef void (CALLTYPE* Unadvice)( double& nHandle );
68 #ifndef DISABLE_DYNLOADING
69 constexpr OUStringLiteral GETFUNCTIONCOUNT = u"GetFunctionCount";
70 constexpr OUStringLiteral GETFUNCTIONDATA = u"GetFunctionData";
71 constexpr OUStringLiteral SETLANGUAGE = u"SetLanguage";
72 constexpr OUStringLiteral GETPARAMDESC = u"GetParameterDescription";
73 constexpr OUStringLiteral ISASYNC = u"IsAsync";
74 constexpr OUStringLiteral ADVICE = u"Advice";
75 constexpr OUStringLiteral UNADVICE = u"Unadvice";
76 #endif
78 class ModuleData
80 friend class ModuleCollection;
81 OUString aName;
82 std::unique_ptr<osl::Module> pInstance;
83 public:
84 ModuleData(const ModuleData&) = delete;
85 const ModuleData& operator=(const ModuleData&) = delete;
87 ModuleData(OUString aStr, std::unique_ptr<osl::Module> pInst) : aName(std::move(aStr)), pInstance(std::move(pInst)) {}
89 const OUString& GetName() const { return aName; }
90 osl::Module* GetInstance() const { return pInstance.get(); }
93 LegacyFuncData::LegacyFuncData(const ModuleData*pModule,
94 OUString aIName,
95 OUString aFName,
96 sal_uInt16 nNo,
97 sal_uInt16 nCount,
98 const ParamType* peType,
99 ParamType eType) :
100 pModuleData (pModule),
101 aInternalName (std::move(aIName)),
102 aFuncName (std::move(aFName)),
103 nNumber (nNo),
104 nParamCount (nCount),
105 eAsyncType (eType)
107 for (sal_uInt16 i = 0; i < MAXFUNCPARAM; i++)
108 eParamType[i] = peType[i];
111 LegacyFuncData::LegacyFuncData(const LegacyFuncData& rData) :
112 pModuleData (rData.pModuleData),
113 aInternalName (rData.aInternalName),
114 aFuncName (rData.aFuncName),
115 nNumber (rData.nNumber),
116 nParamCount (rData.nParamCount),
117 eAsyncType (rData.eAsyncType)
119 for (sal_uInt16 i = 0; i < MAXFUNCPARAM; i++)
120 eParamType[i] = rData.eParamType[i];
123 namespace {
125 class ModuleCollection
127 typedef std::map<OUString, std::unique_ptr<ModuleData>> MapType;
128 MapType m_Data;
129 public:
130 ModuleCollection() {}
132 const ModuleData* findByName(const OUString& rName) const;
133 void insert(ModuleData* pNew);
134 void clear();
137 const ModuleData* ModuleCollection::findByName(const OUString& rName) const
139 MapType::const_iterator it = m_Data.find(rName);
140 return it == m_Data.end() ? nullptr : it->second.get();
143 void ModuleCollection::insert(ModuleData* pNew)
145 if (!pNew)
146 return;
148 OUString aName = pNew->GetName();
149 m_Data.insert(std::make_pair(aName, std::unique_ptr<ModuleData>(pNew)));
152 void ModuleCollection::clear()
154 m_Data.clear();
157 ModuleCollection aModuleCollection;
161 bool InitExternalFunc(const OUString& rModuleName)
163 #ifdef DISABLE_DYNLOADING
164 (void) rModuleName;
165 return false;
166 #else
167 // Module already loaded?
168 const ModuleData* pTemp = aModuleCollection.findByName(rModuleName);
169 if (pTemp)
170 return false;
172 OUString aNP = rModuleName;
174 std::unique_ptr<osl::Module> pLib(new osl::Module( aNP ));
175 if (!pLib->is())
176 return false;
178 oslGenericFunction fpGetCount = pLib->getFunctionSymbol(GETFUNCTIONCOUNT);
179 oslGenericFunction fpGetData = pLib->getFunctionSymbol(GETFUNCTIONDATA);
180 if ((fpGetCount == nullptr) || (fpGetData == nullptr))
181 return false;
183 oslGenericFunction fpIsAsync = pLib->getFunctionSymbol(ISASYNC);
184 oslGenericFunction fpAdvice = pLib->getFunctionSymbol(ADVICE);
185 oslGenericFunction fpSetLanguage = pLib->getFunctionSymbol(SETLANGUAGE);
186 if ( fpSetLanguage )
188 LanguageType eLanguage = Application::GetSettings().GetUILanguageTag().getLanguageType();
189 sal_uInt16 nLanguage = static_cast<sal_uInt16>(eLanguage);
190 (*reinterpret_cast<SetLanguagePtr>(fpSetLanguage))( nLanguage );
193 // include module into the collection
194 ModuleData* pModuleData = new ModuleData(rModuleName, std::move(pLib));
195 aModuleCollection.insert(pModuleData);
197 // initialize interface
198 AdvData pfCallBack = &ScAddInAsyncCallBack;
199 LegacyFuncCollection* pLegacyFuncCol = ScGlobal::GetLegacyFuncCollection();
200 sal_uInt16 nCount;
201 (*reinterpret_cast<GetFuncCountPtr>(fpGetCount))(nCount);
202 for (sal_uInt16 i=0; i < nCount; i++)
204 char cFuncName[256];
205 char cInternalName[256];
206 sal_uInt16 nParamCount;
207 ParamType eParamType[MAXFUNCPARAM];
208 ParamType eAsyncType = ParamType::NONE;
209 // initialize all, in case the AddIn behaves bad
210 cFuncName[0] = 0;
211 cInternalName[0] = 0;
212 nParamCount = 0;
213 for (ParamType & rParamType : eParamType)
215 rParamType = ParamType::NONE;
217 (*reinterpret_cast<GetFuncDataPtr>(fpGetData))(i, cFuncName, nParamCount,
218 eParamType, cInternalName);
219 if( fpIsAsync )
221 (*reinterpret_cast<IsAsync>(fpIsAsync))(i, &eAsyncType);
222 if ( fpAdvice && eAsyncType != ParamType::NONE )
223 (*reinterpret_cast<Advice>(fpAdvice))( i, pfCallBack );
225 OUString aInternalName( cInternalName, strlen(cInternalName), osl_getThreadTextEncoding() );
226 OUString aFuncName( cFuncName, strlen(cFuncName), osl_getThreadTextEncoding() );
227 LegacyFuncData* pLegacyFuncData = new LegacyFuncData( pModuleData,
228 aInternalName,
229 aFuncName,
231 nParamCount,
232 eParamType,
233 eAsyncType );
234 pLegacyFuncCol->insert(pLegacyFuncData);
236 return true;
237 #endif
240 void ExitExternalFunc()
242 aModuleCollection.clear();
245 void LegacyFuncData::Call(void** ppParam) const
247 #ifdef DISABLE_DYNLOADING
248 (void) ppParam;
249 #else
250 osl::Module* pLib = pModuleData->GetInstance();
251 oslGenericFunction fProc = pLib->getFunctionSymbol(aFuncName);
252 if (fProc == nullptr)
253 return;
255 switch (nParamCount)
257 case 1 :
258 (*reinterpret_cast<ExFuncPtr1>(fProc))(ppParam[0]);
259 break;
260 case 2 :
261 (*reinterpret_cast<ExFuncPtr2>(fProc))(ppParam[0], ppParam[1]);
262 break;
263 case 3 :
264 (*reinterpret_cast<ExFuncPtr3>(fProc))(ppParam[0], ppParam[1], ppParam[2]);
265 break;
266 case 4 :
267 (*reinterpret_cast<ExFuncPtr4>(fProc))(ppParam[0], ppParam[1], ppParam[2], ppParam[3]);
268 break;
269 case 5 :
270 (*reinterpret_cast<ExFuncPtr5>(fProc))(ppParam[0], ppParam[1], ppParam[2], ppParam[3], ppParam[4]);
271 break;
272 case 6 :
273 (*reinterpret_cast<ExFuncPtr6>(fProc))(ppParam[0], ppParam[1], ppParam[2], ppParam[3], ppParam[4], ppParam[5]);
274 break;
275 case 7 :
276 (*reinterpret_cast<ExFuncPtr7>(fProc))( ppParam[0], ppParam[1], ppParam[2], ppParam[3], ppParam[4], ppParam[5],
277 ppParam[6]);
278 break;
279 case 8 :
280 (*reinterpret_cast<ExFuncPtr8>(fProc))( ppParam[0], ppParam[1], ppParam[2], ppParam[3], ppParam[4], ppParam[5],
281 ppParam[6], ppParam[7]);
282 break;
283 case 9 :
284 (*reinterpret_cast<ExFuncPtr9>(fProc))( ppParam[0], ppParam[1], ppParam[2], ppParam[3], ppParam[4], ppParam[5],
285 ppParam[6], ppParam[7], ppParam[8]);
286 break;
287 case 10 :
288 (*reinterpret_cast<ExFuncPtr10>(fProc))( ppParam[0], ppParam[1], ppParam[2], ppParam[3], ppParam[4], ppParam[5],
289 ppParam[6], ppParam[7], ppParam[8], ppParam[9]);
290 break;
291 case 11 :
292 (*reinterpret_cast<ExFuncPtr11>(fProc))( ppParam[0], ppParam[1], ppParam[2], ppParam[3], ppParam[4], ppParam[5],
293 ppParam[6], ppParam[7], ppParam[8], ppParam[9], ppParam[10]);
294 break;
295 case 12:
296 (*reinterpret_cast<ExFuncPtr12>(fProc))( ppParam[0], ppParam[1], ppParam[2], ppParam[3], ppParam[4], ppParam[5],
297 ppParam[6], ppParam[7], ppParam[8], ppParam[9], ppParam[10], ppParam[11]);
298 break;
299 case 13:
300 (*reinterpret_cast<ExFuncPtr13>(fProc))( ppParam[0], ppParam[1], ppParam[2], ppParam[3], ppParam[4], ppParam[5],
301 ppParam[6], ppParam[7], ppParam[8], ppParam[9], ppParam[10], ppParam[11],
302 ppParam[12]);
303 break;
304 case 14 :
305 (*reinterpret_cast<ExFuncPtr14>(fProc))( ppParam[0], ppParam[1], ppParam[2], ppParam[3], ppParam[4], ppParam[5],
306 ppParam[6], ppParam[7], ppParam[8], ppParam[9], ppParam[10], ppParam[11],
307 ppParam[12], ppParam[13]);
308 break;
309 case 15 :
310 (*reinterpret_cast<ExFuncPtr15>(fProc))( ppParam[0], ppParam[1], ppParam[2], ppParam[3], ppParam[4], ppParam[5],
311 ppParam[6], ppParam[7], ppParam[8], ppParam[9], ppParam[10], ppParam[11],
312 ppParam[12], ppParam[13], ppParam[14]);
313 break;
314 case 16 :
315 (*reinterpret_cast<ExFuncPtr16>(fProc))( ppParam[0], ppParam[1], ppParam[2], ppParam[3], ppParam[4], ppParam[5],
316 ppParam[6], ppParam[7], ppParam[8], ppParam[9], ppParam[10], ppParam[11],
317 ppParam[12], ppParam[13], ppParam[14], ppParam[15]);
318 break;
319 default : break;
321 #endif
324 void LegacyFuncData::Unadvice( double nHandle )
326 #ifdef DISABLE_DYNLOADING
327 (void) nHandle;
328 #else
329 osl::Module* pLib = pModuleData->GetInstance();
330 oslGenericFunction fProc = pLib->getFunctionSymbol(UNADVICE);
331 if (fProc != nullptr)
333 reinterpret_cast< ::Unadvice>(fProc)(nHandle);
335 #endif
338 const OUString& LegacyFuncData::GetModuleName() const
340 return pModuleData->GetName();
343 void LegacyFuncData::getParamDesc( OUString& aName, OUString& aDesc, sal_uInt16 nParam ) const
345 #ifdef DISABLE_DYNLOADING
346 (void) aName;
347 (void) aDesc;
348 (void) nParam;
349 #else
350 bool bRet = false;
351 if ( nParam <= nParamCount )
353 osl::Module* pLib = pModuleData->GetInstance();
354 oslGenericFunction fProc = pLib->getFunctionSymbol(GETPARAMDESC);
355 if ( fProc != nullptr )
357 char pcName[256];
358 char pcDesc[256];
359 *pcName = *pcDesc = 0;
360 sal_uInt16 nFuncNo = nNumber; // don't let it mess up via reference...
361 reinterpret_cast< ::GetParamDesc>(fProc)( nFuncNo, nParam, pcName, pcDesc );
362 aName = OUString( pcName, 256, osl_getThreadTextEncoding() );
363 aDesc = OUString( pcDesc, 256, osl_getThreadTextEncoding() );
364 bRet = true;
367 if ( !bRet )
369 aName.clear();
370 aDesc.clear();
372 #endif
375 LegacyFuncCollection::LegacyFuncCollection() {}
376 LegacyFuncCollection::LegacyFuncCollection(const LegacyFuncCollection& r)
378 for (auto const& it : r.m_Data)
380 m_Data.insert(std::make_pair(it.first, std::make_unique<LegacyFuncData>(*it.second)));
384 const LegacyFuncData* LegacyFuncCollection::findByName(const OUString& rName) const
386 MapType::const_iterator it = m_Data.find(rName);
387 return it == m_Data.end() ? nullptr : it->second.get();
390 LegacyFuncData* LegacyFuncCollection::findByName(const OUString& rName)
392 MapType::iterator it = m_Data.find(rName);
393 return it == m_Data.end() ? nullptr : it->second.get();
396 void LegacyFuncCollection::insert(LegacyFuncData* pNew)
398 OUString aName = pNew->GetInternalName();
399 m_Data.insert(std::make_pair(aName, std::unique_ptr<LegacyFuncData>(pNew)));
402 LegacyFuncCollection::const_iterator LegacyFuncCollection::begin() const
404 return m_Data.begin();
407 LegacyFuncCollection::const_iterator LegacyFuncCollection::end() const
409 return m_Data.end();
412 /* vim:set shiftwidth=4 softtabstop=4 expandtab: */