LibreOffice Module sc (master)  1
progress.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 <sfx2/app.hxx>
21 #include <sfx2/objsh.hxx>
22 #include <sfx2/progress.hxx>
23 #include <sfx2/docfile.hxx>
24 #include <sfx2/sfxsids.hrc>
25 #include <svl/eitem.hxx>
26 #include <svl/itemset.hxx>
27 #include <osl/diagnose.h>
28 
29 #include <com/sun/star/frame/XModel.hpp>
30 
31 #define SC_PROGRESS_CXX
32 #include <progress.hxx>
33 #include <document.hxx>
34 #include <global.hxx>
35 #include <globstr.hrc>
36 #include <scresid.hxx>
37 
38 using namespace com::sun::star;
39 
47 bool ScProgress::bIdleWasEnabled = false;
48 
49 static bool lcl_IsHiddenDocument( const SfxObjectShell* pObjSh )
50 {
51  if (pObjSh)
52  {
53  SfxMedium* pMed = pObjSh->GetMedium();
54  if (pMed)
55  {
56  SfxItemSet* pSet = pMed->GetItemSet();
57  const SfxPoolItem* pItem;
58  if ( pSet && SfxItemState::SET == pSet->GetItemState( SID_HIDDEN, true, &pItem ) &&
59  static_cast<const SfxBoolItem*>(pItem)->GetValue() )
60  return true;
61  }
62  }
63  return false;
64 }
65 
66 static bool lcl_HasControllersLocked( const SfxObjectShell& rObjSh )
67 {
68  uno::Reference<frame::XModel> xModel( rObjSh.GetBaseModel() );
69  if (xModel.is())
70  return xModel->hasControllersLocked();
71  return false;
72 }
73 
74 ScProgress::ScProgress(SfxObjectShell* pObjSh, const OUString& rText,
75  sal_uLong nRange, bool bWait)
76  : bEnabled(true)
77 {
78 
80  {
81  if ( lcl_IsHiddenDocument(pObjSh) )
82  {
83  // loading a hidden document while a progress is active is possible - no error
84  pProgress = nullptr;
85  }
86  else
87  {
88  OSL_FAIL( "ScProgress: there can be only one!" );
89  pProgress = nullptr;
90  }
91  }
92  else if ( SfxGetpApp()->IsDowning() )
93  {
94  // This happens. E.g. when saving the clipboard-content as OLE when closing the app.
95  // In this case a SfxProgress would produce dirt in memory.
96  //TODO: Should that be this way ???
97 
98  pProgress = nullptr;
99  }
100  else if ( pObjSh && ( pObjSh->GetCreateMode() == SfxObjectCreateMode::EMBEDDED ||
101  pObjSh->GetProgress() ||
102  lcl_HasControllersLocked(*pObjSh) ) )
103  {
104  // no own progress for embedded objects,
105  // no second progress if the document already has one
106 
107  pProgress = nullptr;
108  }
109  else
110  {
111  pProgress.reset(new SfxProgress( pObjSh, rText, nRange, bWait ));
112  pGlobalProgress = pProgress.get();
113  nGlobalRange = nRange;
114  nGlobalPercent = 0;
115  }
116 }
117 
119  : bEnabled(true)
120 {
121  // DummyInterpret
122 }
123 
125 {
126  if ( pProgress )
127  {
128  pProgress.reset();
129  pGlobalProgress = nullptr;
130  nGlobalRange = 0;
131  nGlobalPercent = 0;
132  }
133 }
134 
136 {
137  if ( nInterpretProgress )
139  else if ( pDoc->GetAutoCalc() )
140  {
141  nInterpretProgress = 1;
142  bIdleWasEnabled = pDoc->IsIdleEnabled();
143  pDoc->EnableIdle(false);
144  // Interpreter may be called in many circumstances, also if another
145  // progress bar is active, for example while adapting row heights.
146  // Keep the dummy interpret progress.
147  if ( !pGlobalProgress )
149  ScResId( STR_PROGRESS_CALCULATING ),
151  pInterpretDoc = pDoc;
152  }
153 }
154 
156 {
157  if ( !nInterpretProgress )
158  return;
159 
160  /* Do not decrement 'nInterpretProgress', before 'pInterpretProgress'
161  is deleted. In rare cases, deletion of 'pInterpretProgress' causes
162  a refresh of the sheet window which may call CreateInterpretProgress
163  and DeleteInterpretProgress again (from Output::DrawStrings),
164  resulting in double deletion of 'pInterpretProgress'. */
165  if ( nInterpretProgress == 1 )
166  {
167  if ( pInterpretProgress != &theDummyInterpretProgress )
168  {
169  // move pointer to local temporary to avoid double deletion
170  ScProgress* pTmpProgress = pInterpretProgress;
172  delete pTmpProgress;
173  }
174  if ( pInterpretDoc )
176  }
178 }
179 
180 /* vim:set shiftwidth=4 softtabstop=4 expandtab: */
static SfxProgress * pGlobalProgress
Definition: progress.hxx:43
void EnableIdle(bool bDo)
Definition: document.hxx:2118
std::string GetValue
static sal_uLong nInterpretProgress
Definition: progress.hxx:47
sal_uIntPtr sal_uLong
SfxApplication * SfxGetpApp()
sal_uLong GetFormulaCodeInTree() const
Definition: document.hxx:2308
#define MIN_NO_CODES_PER_PROGRESS_UPDATE
Definition: progress.hxx:38
std::unique_ptr< SfxProgress > pProgress
Definition: progress.hxx:52
SfxObjectCreateMode GetCreateMode() const
css::uno::Reference< css::frame::XModel > GetBaseModel() const
SfxItemState GetItemState(sal_uInt16 nWhich, bool bSrchInParent=true, const SfxPoolItem **ppItem=nullptr) const
static bool lcl_HasControllersLocked(const SfxObjectShell &rObjSh)
Definition: progress.cxx:66
static sal_uLong nGlobalRange
Definition: progress.hxx:44
OUString ScResId(const char *pId)
Definition: scdll.cxx:95
static bool lcl_IsHiddenDocument(const SfxObjectShell *pObjSh)
Definition: progress.cxx:49
static sal_uLong nGlobalPercent
Definition: progress.hxx:45
static void DeleteInterpretProgress()
Definition: progress.cxx:155
SfxItemSet * GetItemSet() const
static ScDocument * pInterpretDoc
Definition: progress.hxx:48
static void CreateInterpretProgress(ScDocument *pDoc, bool bWait=true)
Definition: progress.cxx:135
static ScProgress * pInterpretProgress
Definition: progress.hxx:46
SfxProgress * GetProgress() const
static bool bIdleWasEnabled
Definition: progress.hxx:49
ScProgress(const ScProgress &)=delete
static SfxProgress * GetActiveProgress(SfxObjectShell const *pDocSh=nullptr)
static ScProgress theDummyInterpretProgress
Definition: progress.cxx:40
SfxObjectShell * GetDocumentShell() const
Definition: document.hxx:1058
Reference< XModel > xModel
bool IsIdleEnabled() const
Definition: document.hxx:2117
SC_DLLPUBLIC bool GetAutoCalc() const
Definition: document.hxx:1360
SfxMedium * GetMedium() const