Version 6.4.0.0.beta1, tag libreoffice-6.4.0.0.beta1
[LibreOffice.git] / vcl / source / app / svdata.cxx
blob877f3011d536d2bf1f23493a6f194306021975f2
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 <tools/diagnose_ex.h>
25 #include <unotools/resmgr.hxx>
26 #include <sal/log.hxx>
28 #include <vcl/configsettings.hxx>
29 #include <vcl/dockwin.hxx>
30 #include <vcl/menu.hxx>
31 #include <vcl/print.hxx>
32 #include <vcl/settings.hxx>
33 #include <vcl/svapp.hxx>
34 #include <vcl/virdev.hxx>
35 #include <vcl/wrkwin.hxx>
36 #include <vcl/uitest/logger.hxx>
37 #include <salframe.hxx>
38 #include <scrwnd.hxx>
39 #include <helpwin.hxx>
40 #include <vcl/dialog.hxx>
41 #include <salinst.hxx>
42 #include <salgdi.hxx>
43 #include <svdata.hxx>
44 #include <salsys.hxx>
45 #include <units.hrc>
46 #include <print.h>
48 #include <com/sun/star/accessibility/MSAAService.hpp>
50 #include <config_features.h>
51 #if HAVE_FEATURE_OPENGL
52 #include <vcl/opengl/OpenGLContext.hxx>
53 #endif
54 #include <basegfx/utils/systemdependentdata.hxx>
55 #include <cppuhelper/basemutex.hxx>
57 using namespace com::sun::star::uno;
58 using namespace com::sun::star::lang;
59 using namespace com::sun::star::awt;
61 namespace
63 struct private_aImplSVData :
64 public rtl::Static<ImplSVData, private_aImplSVData> {};
67 ImplSVData* ImplGetSVData() {
68 return &private_aImplSVData::get();
71 SalSystem* ImplGetSalSystem()
73 ImplSVData* pSVData = ImplGetSVData();
74 if( ! pSVData->mpSalSystem )
75 pSVData->mpSalSystem.reset( pSVData->mpDefInst->CreateSalSystem() );
76 return pSVData->mpSalSystem.get();
79 void ImplDeInitSVData()
81 ImplSVData* pSVData = ImplGetSVData();
83 // delete global instance data
84 pSVData->mpSettingsConfigItem.reset();
86 pSVData->mpDockingManager.reset();
88 pSVData->maCtrlData.maFieldUnitStrings.clear();
89 pSVData->maCtrlData.maCleanUnitStrings.clear();
90 pSVData->maPaperNames.clear();
93 namespace
95 typedef ::std::map< basegfx::SystemDependentData_SharedPtr, sal_uInt32 > EntryMap;
97 class SystemDependentDataBuffer final : public basegfx::SystemDependentDataManager, protected cppu::BaseMutex
99 private:
100 std::unique_ptr<AutoTimer> maTimer;
101 EntryMap maEntries;
103 DECL_LINK(implTimeoutHdl, Timer *, void);
105 public:
106 SystemDependentDataBuffer(const sal_Char* pDebugName)
107 : basegfx::SystemDependentDataManager(),
108 maTimer(std::make_unique<AutoTimer>(pDebugName))
110 maTimer->SetTimeout(1000);
111 maTimer->SetInvokeHandler(LINK(this, SystemDependentDataBuffer, implTimeoutHdl));
114 virtual ~SystemDependentDataBuffer() override
116 flushAll();
119 void startUsage(basegfx::SystemDependentData_SharedPtr& rData) override
121 ::osl::MutexGuard aGuard(m_aMutex);
122 EntryMap::iterator aFound(maEntries.find(rData));
124 if(aFound == maEntries.end())
126 if(maTimer && !maTimer->IsActive())
128 maTimer->Start();
131 maEntries[rData] = rData->calculateCombinedHoldCyclesInSeconds();
135 void endUsage(basegfx::SystemDependentData_SharedPtr& rData) override
137 ::osl::MutexGuard aGuard(m_aMutex);
138 EntryMap::iterator aFound(maEntries.find(rData));
140 if(aFound != maEntries.end())
142 maEntries.erase(aFound);
146 void touchUsage(basegfx::SystemDependentData_SharedPtr& rData) override
148 ::osl::MutexGuard aGuard(m_aMutex);
149 EntryMap::iterator aFound(maEntries.find(rData));
151 if(aFound != maEntries.end())
153 aFound->second = rData->calculateCombinedHoldCyclesInSeconds();
157 void flushAll() override
159 ::osl::MutexGuard aGuard(m_aMutex);
161 if(maTimer)
163 maTimer->Stop();
164 maTimer.reset();
167 maEntries.clear();
171 IMPL_LINK_NOARG(SystemDependentDataBuffer, implTimeoutHdl, Timer *, void)
173 ::osl::MutexGuard aGuard(m_aMutex);
174 EntryMap::iterator aIter(maEntries.begin());
176 while(aIter != maEntries.end())
178 if(aIter->second)
180 aIter->second--;
181 ++aIter;
183 else
185 aIter = maEntries.erase(aIter);
189 if (maEntries.empty())
190 maTimer->Stop();
194 basegfx::SystemDependentDataManager& ImplGetSystemDependentDataManager()
196 static SystemDependentDataBuffer aSystemDependentDataBuffer("vcl SystemDependentDataBuffer aSystemDependentDataBuffer");
198 return aSystemDependentDataBuffer;
201 /// Returns either the application window, or the default GL context window
202 vcl::Window* ImplGetDefaultWindow()
204 ImplSVData* pSVData = ImplGetSVData();
205 if ( pSVData->maWinData.mpAppWin )
206 return pSVData->maWinData.mpAppWin;
207 else
208 return ImplGetDefaultContextWindow();
211 /// returns the default window created to hold the persistent VCL GL context.
212 vcl::Window *ImplGetDefaultContextWindow()
214 ImplSVData* pSVData = ImplGetSVData();
216 // Double check locking on mpDefaultWin.
217 if ( !pSVData->mpDefaultWin )
219 SolarMutexGuard aGuard;
221 if (!pSVData->mpDefaultWin && !pSVData->mbDeInit)
225 SAL_INFO( "vcl", "ImplGetDefaultWindow(): No AppWindow" );
227 pSVData->mpDefaultWin = VclPtr<WorkWindow>::Create( nullptr, WB_DEFAULTWIN );
228 pSVData->mpDefaultWin->SetText( "VCL ImplGetDefaultWindow" );
230 #if HAVE_FEATURE_OPENGL
231 // Add a reference to the default context so it never gets deleted
232 rtl::Reference<OpenGLContext> pContext = pSVData->mpDefaultWin->GetGraphics()->GetOpenGLContext();
233 if( pContext.is() )
234 pContext->acquire();
235 #endif
237 catch (const css::uno::Exception&)
239 TOOLS_WARN_EXCEPTION("vcl", "unable to create Default Window");
244 return pSVData->mpDefaultWin;
247 const std::locale& ImplGetResLocale()
249 ImplSVData* pSVData = ImplGetSVData();
250 if (!pSVData->mbResLocaleSet || comphelper::LibreOfficeKit::isActive())
252 pSVData->maResLocale = Translate::Create("vcl");
253 pSVData->mbResLocaleSet = true;
255 return pSVData->maResLocale;
258 OUString VclResId(const char* pId)
260 return Translate::get(pId, ImplGetResLocale());
263 const FieldUnitStringList& ImplGetFieldUnits()
265 ImplSVData* pSVData = ImplGetSVData();
266 if( pSVData->maCtrlData.maFieldUnitStrings.empty() )
268 sal_uInt32 nUnits = SAL_N_ELEMENTS(SV_FUNIT_STRINGS);
269 pSVData->maCtrlData.maFieldUnitStrings.reserve( nUnits );
270 for (sal_uInt32 i = 0; i < nUnits; i++)
272 std::pair<OUString, FieldUnit> aElement(VclResId(SV_FUNIT_STRINGS[i].first), SV_FUNIT_STRINGS[i].second);
273 pSVData->maCtrlData.maFieldUnitStrings.push_back( aElement );
276 return pSVData->maCtrlData.maFieldUnitStrings;
279 const FieldUnitStringList& ImplGetCleanedFieldUnits()
281 ImplSVData* pSVData = ImplGetSVData();
282 if( pSVData->maCtrlData.maCleanUnitStrings.empty() )
284 const FieldUnitStringList& rUnits = ImplGetFieldUnits();
285 size_t nUnits = rUnits.size();
286 pSVData->maCtrlData.maCleanUnitStrings.reserve(nUnits);
287 for (size_t i = 0; i < nUnits; ++i)
289 OUString aUnit(rUnits[i].first);
290 aUnit = aUnit.replaceAll(" ", "");
291 aUnit = aUnit.toAsciiLowerCase();
292 std::pair<OUString, FieldUnit> aElement(aUnit, rUnits[i].second);
293 pSVData->maCtrlData.maCleanUnitStrings.push_back(aElement);
296 return pSVData->maCtrlData.maCleanUnitStrings;
299 DockingManager* ImplGetDockingManager()
301 ImplSVData* pSVData = ImplGetSVData();
302 if ( !pSVData->mpDockingManager )
303 pSVData->mpDockingManager.reset(new DockingManager());
305 return pSVData->mpDockingManager.get();
308 BlendFrameCache* ImplGetBlendFrameCache()
310 ImplSVData* pSVData = ImplGetSVData();
311 if ( !pSVData->mpBlendFrameCache)
312 pSVData->mpBlendFrameCache.reset( new BlendFrameCache() );
314 return pSVData->mpBlendFrameCache.get();
317 #ifdef _WIN32
318 bool ImplInitAccessBridge()
320 ImplSVData* pSVData = ImplGetSVData();
321 if( ! pSVData->mxAccessBridge.is() )
323 css::uno::Reference< XComponentContext > xContext(comphelper::getProcessComponentContext());
325 if (!HasAtHook() && !getenv("SAL_FORCE_IACCESSIBLE2"))
327 SAL_INFO("vcl", "Apparently no running AT -> "
328 "not enabling IAccessible2 integration");
330 else
332 try {
333 pSVData->mxAccessBridge
334 = css::accessibility::MSAAService::create(xContext);
335 SAL_INFO("vcl", "got IAccessible2 bridge");
336 return true;
337 } catch (css::uno::DeploymentException &) {
338 TOOLS_WARN_EXCEPTION(
339 "vcl",
340 "got no IAccessible2 bridge");
341 return false;
346 return true;
348 #endif
350 void LocaleConfigurationListener::ConfigurationChanged( utl::ConfigurationBroadcaster*, ConfigurationHints nHint )
352 AllSettings::LocaleSettingsChanged( nHint );
356 ImplSVData::~ImplSVData() {}
357 ImplSVAppData::~ImplSVAppData() {}
358 ImplSVGDIData::~ImplSVGDIData() {}
359 ImplSVWinData::~ImplSVWinData() {}
360 ImplSVHelpData::~ImplSVHelpData() {}
362 /* vim:set shiftwidth=4 softtabstop=4 expandtab: */