bump product version to 7.6.3.2-android
[LibreOffice.git] / vcl / source / app / svdata.cxx
blob9840eec37fc6e383f14e97e4b58bc73aae01d7b0
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 <string.h>
22 #include <comphelper/lok.hxx>
23 #include <comphelper/processfactory.hxx>
24 #include <comphelper/diagnose_ex.hxx>
25 #include <o3tl/string_view.hxx>
26 #include <unotools/resmgr.hxx>
27 #include <sal/log.hxx>
29 #include <configsettings.hxx>
30 #include <vcl/QueueInfo.hxx>
31 #include <vcl/cvtgrf.hxx>
32 #include <vcl/dockwin.hxx>
33 #include <vcl/fieldvalues.hxx>
34 #include <vcl/menu.hxx>
35 #include <vcl/print.hxx>
36 #include <vcl/settings.hxx>
37 #include <vcl/svapp.hxx>
38 #include <vcl/virdev.hxx>
39 #include <vcl/wrkwin.hxx>
40 #include <vcl/uitest/logger.hxx>
41 #include <salframe.hxx>
42 #include <scrwnd.hxx>
43 #include <helpwin.hxx>
44 #include <vcl/toolkit/dialog.hxx>
45 #include <salinst.hxx>
46 #include <salgdi.hxx>
47 #include <svdata.hxx>
48 #include <salsys.hxx>
49 #include <windowdev.hxx>
50 #include <units.hrc>
51 #include <print.h>
53 #include <com/sun/star/accessibility/MSAAService.hpp>
55 #include <config_features.h>
56 #include <basegfx/utils/systemdependentdata.hxx>
57 #include <mutex>
59 using namespace com::sun::star::uno;
60 using namespace com::sun::star::lang;
61 using namespace com::sun::star::awt;
63 namespace
65 struct private_aImplSVData :
66 public rtl::Static<ImplSVData, private_aImplSVData> {};
67 /// Default instance ensures that ImplSVData::mpHelpData is never null.
68 struct private_aImplSVHelpData :
69 public rtl::Static<ImplSVHelpData, private_aImplSVHelpData> {};
71 /// Default instance ensures that ImplSVData::mpWinData is never null.
72 struct private_aImplSVWinData :
73 public rtl::Static<ImplSVWinData, private_aImplSVWinData> {};
77 ImplSVData* ImplGetSVData() {
78 return &private_aImplSVData::get();
81 SalSystem* ImplGetSalSystem()
83 ImplSVData* pSVData = ImplGetSVData();
84 if( ! pSVData->mpSalSystem )
85 pSVData->mpSalSystem.reset( pSVData->mpDefInst->CreateSalSystem() );
86 return pSVData->mpSalSystem.get();
89 void ImplDeInitSVData()
91 ImplSVData* pSVData = ImplGetSVData();
93 // delete global instance data
94 pSVData->mpSettingsConfigItem.reset();
96 pSVData->mpDockingManager.reset();
98 pSVData->maCtrlData.maFieldUnitStrings.clear();
99 pSVData->maCtrlData.maCleanUnitStrings.clear();
100 pSVData->maPaperNames.clear();
103 namespace
105 typedef ::std::map< basegfx::SystemDependentData_SharedPtr, sal_uInt32 > EntryMap;
107 class SystemDependentDataBuffer final : public basegfx::SystemDependentDataManager
109 private:
110 std::mutex m_aMutex;
111 std::unique_ptr<AutoTimer> maTimer;
112 EntryMap maEntries;
114 DECL_LINK(implTimeoutHdl, Timer *, void);
116 public:
117 SystemDependentDataBuffer(const char* pDebugName)
118 : maTimer(std::make_unique<AutoTimer>(pDebugName))
120 maTimer->SetTimeout(1000);
121 maTimer->SetInvokeHandler(LINK(this, SystemDependentDataBuffer, implTimeoutHdl));
124 virtual ~SystemDependentDataBuffer() override
126 flushAll();
129 void startUsage(basegfx::SystemDependentData_SharedPtr& rData) override
131 std::unique_lock aGuard(m_aMutex);
132 EntryMap::iterator aFound(maEntries.find(rData));
134 if(aFound == maEntries.end())
136 if(maTimer && !maTimer->IsActive())
138 maTimer->Start();
141 maEntries[rData] = rData->calculateCombinedHoldCyclesInSeconds();
145 void endUsage(basegfx::SystemDependentData_SharedPtr& rData) override
147 std::unique_lock aGuard(m_aMutex);
148 EntryMap::iterator aFound(maEntries.find(rData));
150 if(aFound != maEntries.end())
152 maEntries.erase(aFound);
156 void touchUsage(basegfx::SystemDependentData_SharedPtr& rData) override
158 std::unique_lock aGuard(m_aMutex);
159 EntryMap::iterator aFound(maEntries.find(rData));
161 if(aFound != maEntries.end())
163 aFound->second = rData->calculateCombinedHoldCyclesInSeconds();
167 void flushAll() override
169 std::unique_lock aGuard(m_aMutex);
171 if(maTimer)
173 maTimer->Stop();
174 maTimer.reset();
177 maEntries.clear();
181 IMPL_LINK_NOARG(SystemDependentDataBuffer, implTimeoutHdl, Timer *, void)
183 std::unique_lock aGuard(m_aMutex);
184 EntryMap::iterator aIter(maEntries.begin());
186 while(aIter != maEntries.end())
188 if(aIter->second)
190 aIter->second--;
191 ++aIter;
193 else
195 aIter = maEntries.erase(aIter);
199 if (maEntries.empty())
200 maTimer->Stop();
204 basegfx::SystemDependentDataManager& ImplGetSystemDependentDataManager()
206 static SystemDependentDataBuffer aSystemDependentDataBuffer("vcl SystemDependentDataBuffer aSystemDependentDataBuffer");
208 return aSystemDependentDataBuffer;
211 /// Returns either the application window, or the default GL context window
212 vcl::Window* ImplGetDefaultWindow()
214 ImplSVData* pSVData = ImplGetSVData();
215 if (pSVData->maFrameData.mpAppWin)
216 return pSVData->maFrameData.mpAppWin;
217 else
218 return ImplGetDefaultContextWindow();
221 /// returns the default window created to hold the persistent VCL GL context.
222 vcl::Window *ImplGetDefaultContextWindow()
224 ImplSVData* pSVData = ImplGetSVData();
226 // Double check locking on mpDefaultWin.
227 if ( !pSVData->mpDefaultWin )
229 SolarMutexGuard aGuard;
231 if (!pSVData->mpDefaultWin && !pSVData->mbDeInit)
235 SAL_INFO( "vcl", "ImplGetDefaultWindow(): No AppWindow" );
237 pSVData->mpDefaultWin = VclPtr<WorkWindow>::Create(nullptr, WB_DEFAULTWIN);
238 pSVData->mpDefaultWin->SetText( "VCL ImplGetDefaultWindow" );
240 catch (const css::uno::Exception&)
242 TOOLS_WARN_EXCEPTION("vcl", "unable to create Default Window");
247 return pSVData->mpDefaultWin;
250 const std::locale& ImplGetResLocale()
252 ImplSVData* pSVData = ImplGetSVData();
253 if (!pSVData->mbResLocaleSet || comphelper::LibreOfficeKit::isActive())
255 pSVData->maResLocale = Translate::Create("vcl");
256 pSVData->mbResLocaleSet = true;
258 return pSVData->maResLocale;
261 OUString VclResId(TranslateId aId)
263 return Translate::get(aId, ImplGetResLocale());
266 const FieldUnitStringList& ImplGetFieldUnits()
268 ImplSVData* pSVData = ImplGetSVData();
269 if( pSVData->maCtrlData.maFieldUnitStrings.empty() )
271 sal_uInt32 nUnits = SAL_N_ELEMENTS(SV_FUNIT_STRINGS);
272 pSVData->maCtrlData.maFieldUnitStrings.reserve( nUnits );
273 for (sal_uInt32 i = 0; i < nUnits; i++)
275 std::pair<OUString, FieldUnit> aElement(VclResId(SV_FUNIT_STRINGS[i].first), SV_FUNIT_STRINGS[i].second);
276 pSVData->maCtrlData.maFieldUnitStrings.push_back( aElement );
279 return pSVData->maCtrlData.maFieldUnitStrings;
282 namespace vcl
284 FieldUnit EnglishStringToMetric(std::u16string_view rEnglishMetricString)
286 sal_uInt32 nUnits = SAL_N_ELEMENTS(SV_FUNIT_STRINGS);
287 for (sal_uInt32 i = 0; i < nUnits; ++i)
289 if (o3tl::equalsAscii(rEnglishMetricString, SV_FUNIT_STRINGS[i].first.mpId))
290 return SV_FUNIT_STRINGS[i].second;
292 return FieldUnit::NONE;
296 const FieldUnitStringList& ImplGetCleanedFieldUnits()
298 ImplSVData* pSVData = ImplGetSVData();
299 if( pSVData->maCtrlData.maCleanUnitStrings.empty() )
301 const FieldUnitStringList& rUnits = ImplGetFieldUnits();
302 size_t nUnits = rUnits.size();
303 pSVData->maCtrlData.maCleanUnitStrings.reserve(nUnits);
304 for (size_t i = 0; i < nUnits; ++i)
306 OUString aUnit(rUnits[i].first);
307 aUnit = aUnit.replaceAll(" ", "");
308 aUnit = aUnit.toAsciiLowerCase();
309 std::pair<OUString, FieldUnit> aElement(aUnit, rUnits[i].second);
310 pSVData->maCtrlData.maCleanUnitStrings.push_back(aElement);
313 return pSVData->maCtrlData.maCleanUnitStrings;
316 DockingManager* ImplGetDockingManager()
318 ImplSVData* pSVData = ImplGetSVData();
319 if ( !pSVData->mpDockingManager )
320 pSVData->mpDockingManager.reset(new DockingManager());
322 return pSVData->mpDockingManager.get();
325 BlendFrameCache* ImplGetBlendFrameCache()
327 ImplSVData* pSVData = ImplGetSVData();
328 if ( !pSVData->mpBlendFrameCache)
329 pSVData->mpBlendFrameCache.reset( new BlendFrameCache() );
331 return pSVData->mpBlendFrameCache.get();
334 #ifdef _WIN32
335 bool ImplInitAccessBridge()
337 ImplSVData* pSVData = ImplGetSVData();
338 if( ! pSVData->mxAccessBridge.is() )
340 css::uno::Reference< XComponentContext > xContext(comphelper::getProcessComponentContext());
342 if (!HasAtHook() && !getenv("SAL_FORCE_IACCESSIBLE2"))
344 SAL_INFO("vcl", "Apparently no running AT -> "
345 "not enabling IAccessible2 integration");
347 else
349 try {
350 pSVData->mxAccessBridge
351 = css::accessibility::MSAAService::create(xContext);
352 SAL_INFO("vcl", "got IAccessible2 bridge");
353 return true;
354 } catch (css::uno::DeploymentException &) {
355 TOOLS_WARN_EXCEPTION(
356 "vcl",
357 "got no IAccessible2 bridge");
358 return false;
363 return true;
365 #endif
367 void LocaleConfigurationListener::ConfigurationChanged( utl::ConfigurationBroadcaster*, ConfigurationHints nHint )
369 AllSettings::LocaleSettingsChanged( nHint );
372 ImplSVWinData* CreateSVWinData()
374 if (!comphelper::LibreOfficeKit::isActive())
375 return nullptr;
377 ImplSVWinData* p = new ImplSVWinData;
379 ImplSVData* pSVData = ImplGetSVData();
380 assert(pSVData && pSVData->mpWinData);
382 p->mpFocusWin = pSVData->mpWinData->mpFocusWin;
383 return p;
386 void DestroySVWinData(ImplSVWinData* pData)
388 delete pData;
391 void SetSVWinData(ImplSVWinData* pSVWinData)
393 if (!comphelper::LibreOfficeKit::isActive())
394 return;
396 ImplSVData* pSVData = ImplGetSVData();
397 assert(pSVData != nullptr);
399 if (pSVData->mpWinData == pSVWinData)
400 return;
402 // If current one is the static, clean it up to avoid having lingering references.
403 if (pSVData->mpWinData == &private_aImplSVWinData::get())
405 pSVData->mpWinData->mpFocusWin.reset();
408 pSVData->mpWinData = pSVWinData;
409 if (pSVData->mpWinData == nullptr)
411 pSVData->mpWinData = &private_aImplSVWinData::get(); // Never leave it null.
415 ImplSVData::ImplSVData()
417 mpHelpData = &private_aImplSVHelpData::get();
418 mpWinData = &private_aImplSVWinData::get();
421 void ImplSVData::dropCaches()
423 // we are iterating over a map and doing erase while inside a loop which is doing erase
424 // hence we can't use clear() here
425 maGDIData.maScaleCache.remove_if([](const lru_scale_cache::key_value_pair_t&)
426 { return true; });
428 maGDIData.maThemeDrawCommandsCache.clear();
429 maGDIData.maThemeImageCache.clear();
432 void ImplSVData::dumpState(rtl::OStringBuffer &rState)
434 rState.append("\nScaleCache:\t");
435 rState.append(static_cast<sal_Int32>(maGDIData.maScaleCache.size()));
436 rState.append("\t items:");
438 for (auto it = maGDIData.maScaleCache.begin();
439 it != maGDIData.maScaleCache.begin(); ++it)
441 rState.append("\n\t");
442 rState.append(static_cast<sal_Int32>(it->first.maDestSize.Width()));
443 rState.append("x");
444 rState.append(static_cast<sal_Int32>(it->first.maDestSize.Height()));
448 ImplSVHelpData* CreateSVHelpData()
450 if (!comphelper::LibreOfficeKit::isActive())
451 return nullptr;
453 ImplSVHelpData* pNewData = new ImplSVHelpData;
455 // Set options set globally
456 ImplSVHelpData& aStaticHelpData = private_aImplSVHelpData::get();
457 pNewData->mbContextHelp = aStaticHelpData.mbContextHelp;
458 pNewData->mbExtHelp = aStaticHelpData.mbExtHelp;
459 pNewData->mbExtHelpMode = aStaticHelpData.mbExtHelpMode;
460 pNewData->mbOldBalloonMode = aStaticHelpData.mbOldBalloonMode;
461 pNewData->mbBalloonHelp = aStaticHelpData.mbBalloonHelp;
462 pNewData->mbQuickHelp = aStaticHelpData.mbQuickHelp;
464 return pNewData;
467 void DestroySVHelpData(ImplSVHelpData* pSVHelpData)
469 if (!comphelper::LibreOfficeKit::isActive())
470 return;
472 // Change the SVData's help date if necessary
473 if(ImplGetSVData()->mpHelpData == pSVHelpData)
475 ImplGetSVData()->mpHelpData = &private_aImplSVHelpData::get();
478 if(pSVHelpData)
480 ImplDestroyHelpWindow(*pSVHelpData, false);
481 delete pSVHelpData;
485 void SetSVHelpData(ImplSVHelpData* pSVHelpData)
487 if (!comphelper::LibreOfficeKit::isActive())
488 return;
490 ImplSVData* pSVData = ImplGetSVData();
491 if (pSVData->mpHelpData == pSVHelpData)
492 return;
494 // If current one is the static, clean it up to avoid having lingering references.
495 if (pSVData->mpHelpData == &private_aImplSVHelpData::get())
497 pSVData->mpHelpData->mpHelpWin.reset();
500 pSVData->mpHelpData = pSVHelpData;
501 if (pSVData->mpHelpData == nullptr)
503 pSVData->mpHelpData = &private_aImplSVHelpData::get(); // Never leave it null.
507 ImplSVHelpData& ImplGetSVHelpData()
509 ImplSVData* pSVData = ImplGetSVData();
510 if(pSVData->mpHelpData)
512 return *pSVData->mpHelpData;
514 else
516 return private_aImplSVHelpData::get();
520 ImplSVData::~ImplSVData() {}
522 ImplSVAppData::ImplSVAppData()
524 m_bUseSystemLoop = getenv("SAL_USE_SYSTEM_LOOP") != nullptr;
525 SAL_WARN_IF(m_bUseSystemLoop, "vcl.schedule", "Overriding to run LO on system event loop!");
528 ImplSVAppData::~ImplSVAppData() {}
529 ImplSVGDIData::~ImplSVGDIData() {}
530 ImplSVFrameData::~ImplSVFrameData() {}
531 ImplSVWinData::~ImplSVWinData() {}
532 ImplSVHelpData::~ImplSVHelpData() {}
534 /* vim:set shiftwidth=4 softtabstop=4 expandtab: */