LibreOffice Module vcl (master)  1
svdata.cxx
Go to the documentation of this file.
1 /* -*- Mode: C++; tab-width: 4; indent-tabs-mode: nil; c-basic-offset: 4 -*- */
2 /*
3  * This file is part of the LibreOffice project.
4  *
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/.
8  *
9  * This file incorporates work covered by the following license notice:
10  *
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 .
18  */
19 
20 #include <string.h>
21 
22 #include <comphelper/lok.hxx>
24 #include <tools/diagnose_ex.h>
25 #include <unotools/resmgr.hxx>
26 #include <sal/log.hxx>
27 
28 #include <configsettings.hxx>
29 #include <vcl/QueueInfo.hxx>
30 #include <vcl/cvtgrf.hxx>
31 #include <vcl/dockwin.hxx>
32 #include <vcl/fieldvalues.hxx>
33 #include <vcl/menu.hxx>
34 #include <vcl/print.hxx>
35 #include <vcl/settings.hxx>
36 #include <vcl/svapp.hxx>
37 #include <vcl/virdev.hxx>
38 #include <vcl/wrkwin.hxx>
39 #include <vcl/uitest/logger.hxx>
40 #include <salframe.hxx>
41 #include <scrwnd.hxx>
42 #include <helpwin.hxx>
43 #include <vcl/toolkit/dialog.hxx>
44 #include <salinst.hxx>
45 #include <salgdi.hxx>
46 #include <svdata.hxx>
47 #include <salsys.hxx>
48 #include <windowdev.hxx>
49 #include <units.hrc>
50 #include <print.h>
51 
52 #include <com/sun/star/accessibility/MSAAService.hpp>
53 
54 #include <config_features.h>
56 #include <cppuhelper/basemutex.hxx>
57 
58 using namespace com::sun::star::uno;
59 using namespace com::sun::star::lang;
60 using namespace com::sun::star::awt;
61 
62 namespace
63 {
64  struct private_aImplSVData :
65  public rtl::Static<ImplSVData, private_aImplSVData> {};
67  struct private_aImplSVHelpData :
68  public rtl::Static<ImplSVHelpData, private_aImplSVHelpData> {};
69 
71  struct private_aImplSVWinData :
72  public rtl::Static<ImplSVWinData, private_aImplSVWinData> {};
73 
74 }
75 
77  return &private_aImplSVData::get();
78 }
79 
81 {
82  ImplSVData* pSVData = ImplGetSVData();
83  if( ! pSVData->mpSalSystem )
84  pSVData->mpSalSystem.reset( pSVData->mpDefInst->CreateSalSystem() );
85  return pSVData->mpSalSystem.get();
86 }
87 
89 {
90  ImplSVData* pSVData = ImplGetSVData();
91 
92  // delete global instance data
93  pSVData->mpSettingsConfigItem.reset();
94 
95  pSVData->mpDockingManager.reset();
96 
97  pSVData->maCtrlData.maFieldUnitStrings.clear();
98  pSVData->maCtrlData.maCleanUnitStrings.clear();
99  pSVData->maPaperNames.clear();
100 }
101 
102 namespace
103 {
104  typedef ::std::map< basegfx::SystemDependentData_SharedPtr, sal_uInt32 > EntryMap;
105 
106  class SystemDependentDataBuffer final : public basegfx::SystemDependentDataManager, protected cppu::BaseMutex
107  {
108  private:
109  std::unique_ptr<AutoTimer> maTimer;
110  EntryMap maEntries;
111 
112  DECL_LINK(implTimeoutHdl, Timer *, void);
113 
114  public:
115  SystemDependentDataBuffer(const char* pDebugName)
116  : maTimer(std::make_unique<AutoTimer>(pDebugName))
117  {
118  maTimer->SetTimeout(1000);
119  maTimer->SetInvokeHandler(LINK(this, SystemDependentDataBuffer, implTimeoutHdl));
120  }
121 
122  virtual ~SystemDependentDataBuffer() override
123  {
124  flushAll();
125  }
126 
127  void startUsage(basegfx::SystemDependentData_SharedPtr& rData) override
128  {
129  ::osl::MutexGuard aGuard(m_aMutex);
130  EntryMap::iterator aFound(maEntries.find(rData));
131 
132  if(aFound == maEntries.end())
133  {
134  if(maTimer && !maTimer->IsActive())
135  {
136  maTimer->Start();
137  }
138 
139  maEntries[rData] = rData->calculateCombinedHoldCyclesInSeconds();
140  }
141  }
142 
143  void endUsage(basegfx::SystemDependentData_SharedPtr& rData) override
144  {
145  ::osl::MutexGuard aGuard(m_aMutex);
146  EntryMap::iterator aFound(maEntries.find(rData));
147 
148  if(aFound != maEntries.end())
149  {
150  maEntries.erase(aFound);
151  }
152  }
153 
154  void touchUsage(basegfx::SystemDependentData_SharedPtr& rData) override
155  {
156  ::osl::MutexGuard aGuard(m_aMutex);
157  EntryMap::iterator aFound(maEntries.find(rData));
158 
159  if(aFound != maEntries.end())
160  {
161  aFound->second = rData->calculateCombinedHoldCyclesInSeconds();
162  }
163  }
164 
165  void flushAll() override
166  {
167  ::osl::MutexGuard aGuard(m_aMutex);
168 
169  if(maTimer)
170  {
171  maTimer->Stop();
172  maTimer.reset();
173  }
174 
175  maEntries.clear();
176  }
177  };
178 
179  IMPL_LINK_NOARG(SystemDependentDataBuffer, implTimeoutHdl, Timer *, void)
180  {
181  ::osl::MutexGuard aGuard(m_aMutex);
182  EntryMap::iterator aIter(maEntries.begin());
183 
184  while(aIter != maEntries.end())
185  {
186  if(aIter->second)
187  {
188  aIter->second--;
189  ++aIter;
190  }
191  else
192  {
193  aIter = maEntries.erase(aIter);
194  }
195  }
196 
197  if (maEntries.empty())
198  maTimer->Stop();
199  }
200 }
201 
203 {
204  static SystemDependentDataBuffer aSystemDependentDataBuffer("vcl SystemDependentDataBuffer aSystemDependentDataBuffer");
205 
206  return aSystemDependentDataBuffer;
207 }
208 
211 {
212  ImplSVData* pSVData = ImplGetSVData();
213  if (pSVData->maFrameData.mpAppWin)
214  return pSVData->maFrameData.mpAppWin;
215  else
217 }
218 
221 {
222  ImplSVData* pSVData = ImplGetSVData();
223 
224  // Double check locking on mpDefaultWin.
225  if ( !pSVData->mpDefaultWin )
226  {
227  SolarMutexGuard aGuard;
228 
229  if (!pSVData->mpDefaultWin && !pSVData->mbDeInit)
230  {
231  try
232  {
233  SAL_INFO( "vcl", "ImplGetDefaultWindow(): No AppWindow" );
234 
236  pSVData->mpDefaultWin->SetText( "VCL ImplGetDefaultWindow" );
237  }
238  catch (const css::uno::Exception&)
239  {
240  TOOLS_WARN_EXCEPTION("vcl", "unable to create Default Window");
241  }
242  }
243  }
244 
245  return pSVData->mpDefaultWin;
246 }
247 
248 const std::locale& ImplGetResLocale()
249 {
250  ImplSVData* pSVData = ImplGetSVData();
252  {
253  pSVData->maResLocale = Translate::Create("vcl");
254  pSVData->mbResLocaleSet = true;
255  }
256  return pSVData->maResLocale;
257 }
258 
259 OUString VclResId(TranslateId aId)
260 {
261  return Translate::get(aId, ImplGetResLocale());
262 }
263 
265 {
266  ImplSVData* pSVData = ImplGetSVData();
267  if( pSVData->maCtrlData.maFieldUnitStrings.empty() )
268  {
269  sal_uInt32 nUnits = SAL_N_ELEMENTS(SV_FUNIT_STRINGS);
270  pSVData->maCtrlData.maFieldUnitStrings.reserve( nUnits );
271  for (sal_uInt32 i = 0; i < nUnits; i++)
272  {
273  std::pair<OUString, FieldUnit> aElement(VclResId(SV_FUNIT_STRINGS[i].first), SV_FUNIT_STRINGS[i].second);
274  pSVData->maCtrlData.maFieldUnitStrings.push_back( aElement );
275  }
276  }
277  return pSVData->maCtrlData.maFieldUnitStrings;
278 }
279 
280 namespace vcl
281 {
282  FieldUnit EnglishStringToMetric(std::string_view rEnglishMetricString)
283  {
284  sal_uInt32 nUnits = SAL_N_ELEMENTS(SV_FUNIT_STRINGS);
285  for (sal_uInt32 i = 0; i < nUnits; ++i)
286  {
287  if (rEnglishMetricString == SV_FUNIT_STRINGS[i].first.mpId)
288  return SV_FUNIT_STRINGS[i].second;
289  }
290  return FieldUnit::NONE;
291  }
292 }
293 
295 {
296  ImplSVData* pSVData = ImplGetSVData();
297  if( pSVData->maCtrlData.maCleanUnitStrings.empty() )
298  {
299  const FieldUnitStringList& rUnits = ImplGetFieldUnits();
300  size_t nUnits = rUnits.size();
301  pSVData->maCtrlData.maCleanUnitStrings.reserve(nUnits);
302  for (size_t i = 0; i < nUnits; ++i)
303  {
304  OUString aUnit(rUnits[i].first);
305  aUnit = aUnit.replaceAll(" ", "");
306  aUnit = aUnit.toAsciiLowerCase();
307  std::pair<OUString, FieldUnit> aElement(aUnit, rUnits[i].second);
308  pSVData->maCtrlData.maCleanUnitStrings.push_back(aElement);
309  }
310  }
311  return pSVData->maCtrlData.maCleanUnitStrings;
312 }
313 
315 {
316  ImplSVData* pSVData = ImplGetSVData();
317  if ( !pSVData->mpDockingManager )
318  pSVData->mpDockingManager.reset(new DockingManager());
319 
320  return pSVData->mpDockingManager.get();
321 }
322 
324 {
325  ImplSVData* pSVData = ImplGetSVData();
326  if ( !pSVData->mpBlendFrameCache)
327  pSVData->mpBlendFrameCache.reset( new BlendFrameCache() );
328 
329  return pSVData->mpBlendFrameCache.get();
330 }
331 
332 #ifdef _WIN32
333 bool ImplInitAccessBridge()
334 {
335  ImplSVData* pSVData = ImplGetSVData();
336  if( ! pSVData->mxAccessBridge.is() )
337  {
338  css::uno::Reference< XComponentContext > xContext(comphelper::getProcessComponentContext());
339 
340  if (!HasAtHook() && !getenv("SAL_FORCE_IACCESSIBLE2"))
341  {
342  SAL_INFO("vcl", "Apparently no running AT -> "
343  "not enabling IAccessible2 integration");
344  }
345  else
346  {
347  try {
348  pSVData->mxAccessBridge
349  = css::accessibility::MSAAService::create(xContext);
350  SAL_INFO("vcl", "got IAccessible2 bridge");
351  return true;
352  } catch (css::uno::DeploymentException &) {
354  "vcl",
355  "got no IAccessible2 bridge");
356  return false;
357  }
358  }
359  }
360 
361  return true;
362 }
363 #endif
364 
366 {
368 }
369 
371 {
373  return nullptr;
374 
376 
377  ImplSVData* pSVData = ImplGetSVData();
378  assert(pSVData && pSVData->mpWinData);
379 
380  p->mpFocusWin = pSVData->mpWinData->mpFocusWin;
381  return p;
382 }
383 
385 {
386  delete pData;
387 }
388 
389 void SetSVWinData(ImplSVWinData* pSVWinData)
390 {
392  return;
393 
394  ImplSVData* pSVData = ImplGetSVData();
395  assert(pSVData != nullptr);
396 
397  if (pSVData->mpWinData == pSVWinData)
398  return;
399 
400  // If current one is the static, clean it up to avoid having lingering references.
401  if (pSVData->mpWinData == &private_aImplSVWinData::get())
402  {
403  pSVData->mpWinData->mpFocusWin.reset();
404  }
405 
406  pSVData->mpWinData = pSVWinData;
407  if (pSVData->mpWinData == nullptr)
408  {
409  pSVData->mpWinData = &private_aImplSVWinData::get(); // Never leave it null.
410  }
411 }
412 
414 {
415  mpHelpData = &private_aImplSVHelpData::get();
416  mpWinData = &private_aImplSVWinData::get();
417 }
418 
420 {
422  return nullptr;
423 
424  ImplSVHelpData* pNewData = new ImplSVHelpData;
425 
426  // Set options set globally
427  ImplSVHelpData& aStaticHelpData = private_aImplSVHelpData::get();
428  pNewData->mbContextHelp = aStaticHelpData.mbContextHelp;
429  pNewData->mbExtHelp = aStaticHelpData.mbExtHelp;
430  pNewData->mbExtHelpMode = aStaticHelpData.mbExtHelpMode;
431  pNewData->mbOldBalloonMode = aStaticHelpData.mbOldBalloonMode;
432  pNewData->mbBalloonHelp = aStaticHelpData.mbBalloonHelp;
433  pNewData->mbQuickHelp = aStaticHelpData.mbQuickHelp;
434 
435  return pNewData;
436 }
437 
439 {
441  return;
442 
443  // Change the SVData's help date if necessary
444  if(ImplGetSVData()->mpHelpData == pSVHelpData)
445  {
446  ImplGetSVData()->mpHelpData = &private_aImplSVHelpData::get();
447  }
448 
449  if(pSVHelpData)
450  {
451  ImplDestroyHelpWindow(*pSVHelpData, false);
452  delete pSVHelpData;
453  }
454 }
455 
456 void SetSVHelpData(ImplSVHelpData* pSVHelpData)
457 {
459  return;
460 
461  ImplSVData* pSVData = ImplGetSVData();
462  if (pSVData->mpHelpData == pSVHelpData)
463  return;
464 
465  // If current one is the static, clean it up to avoid having lingering references.
466  if (pSVData->mpHelpData == &private_aImplSVHelpData::get())
467  {
468  pSVData->mpHelpData->mpHelpWin.reset();
469  }
470 
471  pSVData->mpHelpData = pSVHelpData;
472  if (pSVData->mpHelpData == nullptr)
473  {
474  pSVData->mpHelpData = &private_aImplSVHelpData::get(); // Never leave it null.
475  }
476 }
477 
479 {
480  ImplSVData* pSVData = ImplGetSVData();
481  if(pSVData->mpHelpData)
482  {
483  return *pSVData->mpHelpData;
484  }
485  else
486  {
487  return private_aImplSVHelpData::get();
488  }
489 }
490 
492 
494 {
495  m_bUseSystemLoop = getenv("SAL_USE_SYSTEM_LOOP") != nullptr;
496  SAL_WARN_IF(m_bUseSystemLoop, "vcl.schedule", "Overriding to run LO on system event loop!");
497 }
498 
504 
505 /* vim:set shiftwidth=4 softtabstop=4 expandtab: */
ImplSVHelpData & ImplGetSVHelpData()
Definition: svdata.cxx:478
ConfigurationHints
DECL_LINK(CheckNameHdl, SvxNameDialog &, bool)
VclPtr< HelpTextWindow > mpHelpWin
Definition: svdata.hxx:307
FieldUnit
std::unique_ptr< SalSystem > mpSalSystem
Definition: svdata.hxx:394
VclPtr< vcl::Window > mpFocusWin
Definition: svdata.hxx:255
const FieldUnitStringList & ImplGetCleanedFieldUnits()
Definition: svdata.cxx:294
void SetSVWinData(ImplSVWinData *pSVWinData)
Definition: svdata.cxx:389
std::unique_ptr< vcl::SettingsConfigItem > mpSettingsConfigItem
Definition: svdata.hxx:414
css::uno::Reference< css::lang::XComponent > mxAccessBridge
Definition: svdata.hxx:413
~ImplSVData()
Definition: svdata.cxx:491
void DestroySVHelpData(ImplSVHelpData *pSVHelpData)
Definition: svdata.cxx:438
ImplSVData()
Definition: svdata.cxx:413
ImplSVHelpData * CreateSVHelpData()
Definition: svdata.cxx:419
BlendFrameCache * ImplGetBlendFrameCache()
Definition: svdata.cxx:323
ImplSVCtrlData maCtrlData
Definition: svdata.hxx:402
bool mbExtHelpMode
Definition: svdata.hxx:300
bool mbResLocaleSet
Definition: svdata.hxx:395
ImplSVHelpData * mpHelpData
Definition: svdata.hxx:403
std::unique_ptr< BlendFrameCache > mpBlendFrameCache
Definition: svdata.hxx:408
basegfx::SystemDependentDataManager & ImplGetSystemDependentDataManager()
Definition: svdata.cxx:202
OUString VclResId(TranslateId aId)
Definition: svdata.cxx:259
#define SAL_N_ELEMENTS(arr)
std::locale Create(std::string_view aPrefixName, const LanguageTag &rLocale)
FieldUnit EnglishStringToMetric(std::string_view rEnglishMetricString)
Definition: svdata.cxx:282
vcl::Window * ImplGetDefaultContextWindow()
returns the default window created to hold the persistent VCL GL context.
Definition: svdata.cxx:220
ImplSVData * ImplGetSVData()
Definition: svdata.cxx:76
An auto-timer is a multi-shot timer re-emitting itself at interval until destroyed or stopped...
Definition: timer.hxx:66
SalSystem * ImplGetSalSystem()
Definition: svdata.cxx:80
#define TOOLS_WARN_EXCEPTION(area, stream)
virtual SalSystem * CreateSalSystem()=0
int i
FieldUnitStringList maCleanUnitStrings
Definition: svdata.hxx:292
bool mbQuickHelp
Definition: svdata.hxx:303
bool mbExtHelp
Definition: svdata.hxx:299
VclPtr< WorkWindow > mpAppWin
Definition: svdata.hxx:247
OUString get(TranslateId sContextAndId, const std::locale &loc)
static SAL_DLLPRIVATE void LocaleSettingsChanged(ConfigurationHints nHint)
bool mbContextHelp
Definition: svdata.hxx:298
void reset(reference_type *pBody)
Definition: vclptr.hxx:153
const std::locale & ImplGetResLocale()
Definition: svdata.cxx:248
virtual void ConfigurationChanged(utl::ConfigurationBroadcaster *, ConfigurationHints) override
Definition: svdata.cxx:365
bool mbBalloonHelp
Definition: svdata.hxx:302
DockingManager * ImplGetDockingManager()
Definition: svdata.cxx:314
bool mbOldBalloonMode
Definition: svdata.hxx:301
#define SAL_WARN_IF(condition, area, stream)
void ImplDeInitSVData()
Definition: svdata.cxx:88
ImplSVFrameData maFrameData
Definition: svdata.hxx:400
#define SAL_INFO(area, stream)
void SetSVHelpData(ImplSVHelpData *pSVHelpData)
Definition: svdata.cxx:456
virtual void SetText(const OUString &rStr) override
Definition: syswin.cxx:1057
static VclPtr< reference_type > Create(Arg &&...arg)
A construction helper for VclPtr.
Definition: vclptr.hxx:127
bool mbDeInit
Definition: svdata.hxx:393
void DestroySVWinData(ImplSVWinData *pData)
Definition: svdata.cxx:384
void * p
Reference< XComponentContext > getProcessComponentContext()
std::vector< std::pair< OUString, FieldUnit > > FieldUnitStringList
Definition: svdata.hxx:274
IMPL_LINK_NOARG(Animation, ImplTimeoutHdl, Timer *, void)
Definition: Animation.cxx:278
WinBits const WB_DEFAULTWIN
ImplSVWinData * CreateSVWinData()
Definition: svdata.cxx:370
FieldUnitStringList maFieldUnitStrings
Definition: svdata.hxx:291
Definition: timer.hxx:26
std::unique_ptr< DockingManager > mpDockingManager
Definition: svdata.hxx:407
const FieldUnitStringList & ImplGetFieldUnits()
Definition: svdata.cxx:264
constexpr OUStringLiteral first
vcl::Window * ImplGetDefaultWindow()
Returns either the application window, or the default GL context window.
Definition: svdata.cxx:210
std::locale maResLocale
Definition: svdata.hxx:396
SalInstance * mpDefInst
Definition: svdata.hxx:390
std::unordered_map< int, OUString > maPaperNames
Definition: svdata.hxx:416
void ImplDestroyHelpWindow(bool bUpdateHideTime)
Definition: help.cxx:550
ImplSVWinData * mpWinData
Definition: svdata.hxx:401
std::shared_ptr< SystemDependentData > SystemDependentData_SharedPtr
VclPtr< WorkWindow > mpDefaultWin
Definition: svdata.hxx:392