sw a11y: clang-format SidebarWinAccessible code
[LibreOffice.git] / vcl / source / app / svdata.cxx
blobf767c2483925bff939819ea52f434c057587134e
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 <config_features.h>
54 #include <basegfx/utils/systemdependentdata.hxx>
55 #include <mutex>
57 using namespace com::sun::star::uno;
59 namespace
61 struct private_aImplSVData :
62 public rtl::Static<ImplSVData, private_aImplSVData> {};
63 /// Default instance ensures that ImplSVData::mpHelpData is never null.
64 struct private_aImplSVHelpData :
65 public rtl::Static<ImplSVHelpData, private_aImplSVHelpData> {};
67 /// Default instance ensures that ImplSVData::mpWinData is never null.
68 struct private_aImplSVWinData :
69 public rtl::Static<ImplSVWinData, private_aImplSVWinData> {};
73 ImplSVData* ImplGetSVData() {
74 return &private_aImplSVData::get();
77 SalSystem* ImplGetSalSystem()
79 ImplSVData* pSVData = ImplGetSVData();
80 if( ! pSVData->mpSalSystem )
81 pSVData->mpSalSystem.reset( pSVData->mpDefInst->CreateSalSystem() );
82 return pSVData->mpSalSystem.get();
85 void ImplDeInitSVData()
87 ImplSVData* pSVData = ImplGetSVData();
89 // delete global instance data
90 pSVData->mpSettingsConfigItem.reset();
92 pSVData->mpDockingManager.reset();
94 pSVData->maCtrlData.maFieldUnitStrings.clear();
95 pSVData->maCtrlData.maCleanUnitStrings.clear();
96 pSVData->maPaperNames.clear();
99 namespace
101 typedef ::std::unordered_map< basegfx::SystemDependentData_SharedPtr, sal_uInt32 > EntryMap;
103 class SystemDependentDataBuffer final : public basegfx::SystemDependentDataManager
105 private:
106 std::mutex m_aMutex;
107 std::unique_ptr<AutoTimer> maTimer;
108 EntryMap maEntries;
110 DECL_LINK(implTimeoutHdl, Timer *, void);
112 public:
113 SystemDependentDataBuffer(const char* pDebugName)
114 : maTimer(std::make_unique<AutoTimer>(pDebugName))
116 maTimer->SetTimeout(1000);
117 maTimer->SetInvokeHandler(LINK(this, SystemDependentDataBuffer, implTimeoutHdl));
120 virtual ~SystemDependentDataBuffer() override
122 flushAll();
125 void startUsage(basegfx::SystemDependentData_SharedPtr& rData) override
127 std::unique_lock aGuard(m_aMutex);
128 EntryMap::iterator aFound(maEntries.find(rData));
130 if(aFound == maEntries.end())
132 if(maTimer && !maTimer->IsActive())
134 maTimer->Start();
137 maEntries[rData] = rData->calculateCombinedHoldCyclesInSeconds();
141 void endUsage(basegfx::SystemDependentData_SharedPtr& rData) override
143 // tdf#163428 prepare space for entry to hold to avoid early deletion
144 basegfx::SystemDependentData_SharedPtr aToBeDeletedEntry;
147 // lock m_aMutex in own scope
148 std::unique_lock aGuard(m_aMutex);
149 EntryMap::iterator aFound(maEntries.find(rData));
151 if(aFound != maEntries.end())
153 // tdf#163428 ensure to hold/not delete entry while m_aMutex is locked
154 aToBeDeletedEntry = aFound->first;
156 // remove from list
157 maEntries.erase(aFound);
161 // tdf#163428 aToBeDeletedEntry will be destructed, thus the
162 // entry referenced by SharedPtr may be deleted now
165 void touchUsage(basegfx::SystemDependentData_SharedPtr& rData) override
167 std::unique_lock aGuard(m_aMutex);
168 EntryMap::iterator aFound(maEntries.find(rData));
170 if(aFound != maEntries.end())
172 aFound->second = rData->calculateCombinedHoldCyclesInSeconds();
176 void flushAll() override
178 std::unique_lock aGuard(m_aMutex);
180 if(maTimer)
182 maTimer->Stop();
183 maTimer.reset();
186 maEntries.clear();
190 IMPL_LINK_NOARG(SystemDependentDataBuffer, implTimeoutHdl, Timer *, void)
192 // tdf#163428 prepare a temporary list for SystemDependentData_SharedPtr
193 // entries that need to be removed from the unordered_map, but do not need
194 // locking m_aMutex
195 ::std::vector<basegfx::SystemDependentData_SharedPtr> aToBeDeletedEntries;
198 // lock m_aMutex in own scope
199 std::unique_lock aGuard(m_aMutex);
200 EntryMap::iterator aIter(maEntries.begin());
202 while(aIter != maEntries.end())
204 if(aIter->second)
206 aIter->second--;
207 ++aIter;
209 else
211 // tdf#163428 Do not potentially directly delete SystemDependentData_SharedPtr
212 // entries in the unordered_map (maEntries). That can/would happen when the shared_ptr
213 // has only one reference left. This can cause problems, e.g. we have
214 // BufferedData_ModifiedBitmapEx which can contain a Bitmap with added
215 // DataBuffers itself, thus deleting this here *directly* would trigger e.g.
216 // endUsage above. That would then block when we would still hold m_aMutex.
217 // This can potentially be the case with other derivations of
218 // basegfx::SystemDependentData, too.
219 // To avoid this, protect the part that needs protection by the m_aMutex, that
220 // is the modification of the unordered_map itself. Cleaning up the list entries can be
221 // done after this, without holding the lock.
222 // Thus, remember the SystemDependentData_SharedPtr in a temporary list by adding
223 // a reference/shared_ptr to it to guarantee it gets not deleted when it gets removed
224 // from the unordered_map.
225 // Anyways, only locking while manipulating the list is better, destruction of
226 // objects may be expensive and hold m_aMutex longer than necessary.
227 aToBeDeletedEntries.push_back(aIter->first);
229 // remove from list. This decrements the shared_ptr, but delete is avoided
230 aIter = maEntries.erase(aIter);
234 if (maEntries.empty())
235 maTimer->Stop();
238 // tdf#163428 here aToBeDeletedEntries will be destroyed, the entries will be
239 // decremented and potentially deleted. These are of type SystemDependentData_SharedPtr,
240 // so we do not need to do anything explicitly here
244 basegfx::SystemDependentDataManager& ImplGetSystemDependentDataManager()
246 static SystemDependentDataBuffer aSystemDependentDataBuffer("vcl SystemDependentDataBuffer aSystemDependentDataBuffer");
248 return aSystemDependentDataBuffer;
251 /// Returns either the application window, or the default GL context window
252 vcl::Window* ImplGetDefaultWindow()
254 ImplSVData* pSVData = ImplGetSVData();
255 if (pSVData->maFrameData.mpAppWin)
256 return pSVData->maFrameData.mpAppWin;
257 else
258 return ImplGetDefaultContextWindow();
261 /// returns the default window created to hold the persistent VCL GL context.
262 vcl::Window *ImplGetDefaultContextWindow()
264 ImplSVData* pSVData = ImplGetSVData();
266 // Double check locking on mpDefaultWin.
267 if ( !pSVData->mpDefaultWin )
269 SolarMutexGuard aGuard;
271 if (!pSVData->mpDefaultWin && !pSVData->mbDeInit)
275 SAL_INFO( "vcl", "ImplGetDefaultWindow(): No AppWindow" );
277 pSVData->mpDefaultWin = VclPtr<WorkWindow>::Create(nullptr, WB_DEFAULTWIN);
278 pSVData->mpDefaultWin->SetText( u"VCL ImplGetDefaultWindow"_ustr );
280 catch (const css::uno::Exception&)
282 TOOLS_WARN_EXCEPTION("vcl", "unable to create Default Window");
287 return pSVData->mpDefaultWin;
290 const std::locale& ImplGetResLocale()
292 ImplSVData* pSVData = ImplGetSVData();
293 if (!pSVData->mbResLocaleSet || comphelper::LibreOfficeKit::isActive())
295 pSVData->maResLocale = Translate::Create("vcl");
296 pSVData->mbResLocaleSet = true;
298 return pSVData->maResLocale;
301 OUString VclResId(TranslateId aId)
303 return Translate::get(aId, ImplGetResLocale());
306 const FieldUnitStringList& ImplGetFieldUnits()
308 ImplSVData* pSVData = ImplGetSVData();
309 if( pSVData->maCtrlData.maFieldUnitStrings.empty() )
311 sal_uInt32 nUnits = std::size(SV_FUNIT_STRINGS);
312 pSVData->maCtrlData.maFieldUnitStrings.reserve( nUnits );
313 for (sal_uInt32 i = 0; i < nUnits; i++)
315 std::pair<OUString, FieldUnit> aElement(VclResId(SV_FUNIT_STRINGS[i].first), SV_FUNIT_STRINGS[i].second);
316 pSVData->maCtrlData.maFieldUnitStrings.push_back( aElement );
319 return pSVData->maCtrlData.maFieldUnitStrings;
322 namespace vcl
324 FieldUnit EnglishStringToMetric(std::u16string_view rEnglishMetricString)
326 sal_uInt32 nUnits = std::size(SV_FUNIT_STRINGS);
327 for (sal_uInt32 i = 0; i < nUnits; ++i)
329 if (o3tl::equalsAscii(rEnglishMetricString, SV_FUNIT_STRINGS[i].first.getId()))
330 return SV_FUNIT_STRINGS[i].second;
332 return FieldUnit::NONE;
336 const FieldUnitStringList& ImplGetCleanedFieldUnits()
338 ImplSVData* pSVData = ImplGetSVData();
339 if( pSVData->maCtrlData.maCleanUnitStrings.empty() )
341 const FieldUnitStringList& rUnits = ImplGetFieldUnits();
342 size_t nUnits = rUnits.size();
343 pSVData->maCtrlData.maCleanUnitStrings.reserve(nUnits);
344 for (size_t i = 0; i < nUnits; ++i)
346 OUString aUnit(rUnits[i].first);
347 aUnit = aUnit.replaceAll(" ", "");
348 aUnit = aUnit.toAsciiLowerCase();
349 std::pair<OUString, FieldUnit> aElement(aUnit, rUnits[i].second);
350 pSVData->maCtrlData.maCleanUnitStrings.push_back(aElement);
353 return pSVData->maCtrlData.maCleanUnitStrings;
356 DockingManager* ImplGetDockingManager()
358 ImplSVData* pSVData = ImplGetSVData();
359 if ( !pSVData->mpDockingManager )
360 pSVData->mpDockingManager.reset(new DockingManager());
362 return pSVData->mpDockingManager.get();
365 BlendFrameCache* ImplGetBlendFrameCache()
367 ImplSVData* pSVData = ImplGetSVData();
368 if ( !pSVData->mpBlendFrameCache)
369 pSVData->mpBlendFrameCache.reset( new BlendFrameCache() );
371 return pSVData->mpBlendFrameCache.get();
374 void LocaleConfigurationListener::ConfigurationChanged( utl::ConfigurationBroadcaster*, ConfigurationHints nHint )
376 AllSettings::LocaleSettingsChanged( nHint );
379 ImplSVWinData* CreateSVWinData()
381 if (!comphelper::LibreOfficeKit::isActive())
382 return nullptr;
384 ImplSVWinData* p = new ImplSVWinData;
386 ImplSVData* pSVData = ImplGetSVData();
387 assert(pSVData && pSVData->mpWinData);
389 p->mpFocusWin = pSVData->mpWinData->mpFocusWin;
390 return p;
393 void DestroySVWinData(ImplSVWinData* pData)
395 delete pData;
398 void SetSVWinData(ImplSVWinData* pSVWinData)
400 if (!comphelper::LibreOfficeKit::isActive())
401 return;
403 ImplSVData* pSVData = ImplGetSVData();
404 assert(pSVData != nullptr);
406 if (pSVData->mpWinData == pSVWinData)
407 return;
409 // If current one is the static, clean it up to avoid having lingering references.
410 if (pSVData->mpWinData == &private_aImplSVWinData::get())
412 pSVData->mpWinData->mpFocusWin.reset();
415 pSVData->mpWinData = pSVWinData;
416 if (pSVData->mpWinData == nullptr)
418 pSVData->mpWinData = &private_aImplSVWinData::get(); // Never leave it null.
422 ImplSVData::ImplSVData()
424 mpHelpData = &private_aImplSVHelpData::get();
425 mpWinData = &private_aImplSVWinData::get();
428 void ImplSVData::dropCaches()
430 // we are iterating over a map and doing erase while inside a loop which is doing erase
431 // hence we can't use clear() here
432 maGDIData.maScaleCache.remove_if([](const lru_scale_cache::key_value_pair_t&)
433 { return true; });
435 maGDIData.maThemeDrawCommandsCache.clear();
436 maGDIData.maThemeImageCache.clear();
439 void ImplSVData::dumpState(rtl::OStringBuffer &rState)
441 rState.append("\nScaleCache:\t");
442 rState.append(static_cast<sal_Int32>(maGDIData.maScaleCache.size()));
443 rState.append("\t items:");
445 for (auto it = maGDIData.maScaleCache.begin();
446 it != maGDIData.maScaleCache.end(); ++it)
448 rState.append("\n\t");
449 rState.append(static_cast<sal_Int32>(it->first.maDestSize.Width()));
450 rState.append("x");
451 rState.append(static_cast<sal_Int32>(it->first.maDestSize.Height()));
455 ImplSVHelpData* CreateSVHelpData()
457 if (!comphelper::LibreOfficeKit::isActive())
458 return nullptr;
460 ImplSVHelpData* pNewData = new ImplSVHelpData;
462 // Set options set globally
463 ImplSVHelpData& aStaticHelpData = private_aImplSVHelpData::get();
464 pNewData->mbContextHelp = aStaticHelpData.mbContextHelp;
465 pNewData->mbExtHelp = aStaticHelpData.mbExtHelp;
466 pNewData->mbExtHelpMode = aStaticHelpData.mbExtHelpMode;
467 pNewData->mbOldBalloonMode = aStaticHelpData.mbOldBalloonMode;
468 pNewData->mbBalloonHelp = aStaticHelpData.mbBalloonHelp;
469 pNewData->mbQuickHelp = aStaticHelpData.mbQuickHelp;
471 return pNewData;
474 void DestroySVHelpData(ImplSVHelpData* pSVHelpData)
476 if (!comphelper::LibreOfficeKit::isActive())
477 return;
479 // Change the SVData's help date if necessary
480 if(ImplGetSVData()->mpHelpData == pSVHelpData)
482 ImplGetSVData()->mpHelpData = &private_aImplSVHelpData::get();
485 if(pSVHelpData)
487 ImplDestroyHelpWindow(*pSVHelpData, false);
488 delete pSVHelpData;
492 void SetSVHelpData(ImplSVHelpData* pSVHelpData)
494 if (!comphelper::LibreOfficeKit::isActive())
495 return;
497 ImplSVData* pSVData = ImplGetSVData();
498 if (pSVData->mpHelpData == pSVHelpData)
499 return;
501 // If current one is the static, clean it up to avoid having lingering references.
502 if (pSVData->mpHelpData == &private_aImplSVHelpData::get())
504 pSVData->mpHelpData->mpHelpWin.reset();
507 pSVData->mpHelpData = pSVHelpData;
508 if (pSVData->mpHelpData == nullptr)
510 pSVData->mpHelpData = &private_aImplSVHelpData::get(); // Never leave it null.
514 ImplSVHelpData& ImplGetSVHelpData()
516 ImplSVData* pSVData = ImplGetSVData();
517 if(pSVData->mpHelpData)
519 return *pSVData->mpHelpData;
521 else
523 return private_aImplSVHelpData::get();
527 ImplSVData::~ImplSVData() {}
529 ImplSVAppData::ImplSVAppData()
531 m_bUseSystemLoop = getenv("SAL_USE_SYSTEM_LOOP") != nullptr;
532 SAL_WARN_IF(m_bUseSystemLoop, "vcl.schedule", "Overriding to run LO on system event loop!");
535 ImplSVAppData::~ImplSVAppData() {}
536 ImplSVGDIData::~ImplSVGDIData() {}
537 ImplSVFrameData::~ImplSVFrameData() {}
538 ImplSVWinData::~ImplSVWinData() {}
539 ImplSVHelpData::~ImplSVHelpData() {}
541 /* vim:set shiftwidth=4 softtabstop=4 expandtab: */