1 /* -*- Mode: C++; tab-width: 4; indent-tabs-mode: nil; c-basic-offset: 4 -*- */
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 <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>
28 #define SC_PROGRESS_CXX
29 #include "progress.hxx"
30 #include "document.hxx"
32 #include "globstr.hrc"
34 using namespace com::sun::star
;
36 static ScProgress theDummyInterpretProgress
;
37 SfxProgress
* ScProgress::pGlobalProgress
= NULL
;
38 sal_uLong
ScProgress::nGlobalRange
= 0;
39 sal_uLong
ScProgress::nGlobalPercent
= 0;
40 bool ScProgress::bGlobalNoUserBreak
= true;
41 ScProgress
* ScProgress::pInterpretProgress
= &theDummyInterpretProgress
;
42 ScProgress
* ScProgress::pOldInterpretProgress
= NULL
;
43 sal_uLong
ScProgress::nInterpretProgress
= 0;
44 bool ScProgress::bAllowInterpretProgress
= true;
45 ScDocument
* ScProgress::pInterpretDoc
;
46 bool ScProgress::bIdleWasEnabled
= false;
48 static bool lcl_IsHiddenDocument( SfxObjectShell
* pObjSh
)
52 SfxMedium
* pMed
= pObjSh
->GetMedium();
55 SfxItemSet
* pSet
= pMed
->GetItemSet();
56 const SfxPoolItem
* pItem
;
57 if ( pSet
&& SfxItemState::SET
== pSet
->GetItemState( SID_HIDDEN
, true, &pItem
) &&
58 static_cast<const SfxBoolItem
*>(pItem
)->GetValue() )
65 static bool lcl_HasControllersLocked( SfxObjectShell
& rObjSh
)
67 uno::Reference
<frame::XModel
> xModel( rObjSh
.GetBaseModel() );
69 return xModel
->hasControllersLocked();
73 ScProgress::ScProgress(SfxObjectShell
* pObjSh
, const OUString
& rText
,
74 sal_uLong nRange
, bool bAllDocs
, bool bWait
)
78 if ( pGlobalProgress
|| SfxProgress::GetActiveProgress( NULL
) )
80 if ( lcl_IsHiddenDocument(pObjSh
) )
82 // loading a hidden document while a progress is active is possible - no error
87 OSL_FAIL( "ScProgress: there can be only one!" );
91 else if ( SfxGetpApp()->IsDowning() )
93 // This happens. E.g. when saving the clipboard-content as OLE when closing the app.
94 // In this case a SfxProgress would produce dirt in memory.
95 //TODO: Should that be this way ???
99 else if ( pObjSh
&& ( pObjSh
->GetCreateMode() == SfxObjectCreateMode::EMBEDDED
||
100 pObjSh
->GetProgress() ||
101 lcl_HasControllersLocked(*pObjSh
) ) )
103 // no own progress for embedded objects,
104 // no second progress if the document already has one
110 pProgress
= new SfxProgress( pObjSh
, rText
, nRange
, bAllDocs
, bWait
);
111 pGlobalProgress
= pProgress
;
112 nGlobalRange
= nRange
;
114 bGlobalNoUserBreak
= true;
118 ScProgress::ScProgress()
125 ScProgress::~ScProgress()
130 pGlobalProgress
= NULL
;
133 bGlobalNoUserBreak
= true;
137 void ScProgress::CreateInterpretProgress( ScDocument
* pDoc
, bool bWait
)
139 if ( bAllowInterpretProgress
)
141 if ( nInterpretProgress
)
142 nInterpretProgress
++;
143 else if ( pDoc
->GetAutoCalc() )
145 nInterpretProgress
= 1;
146 bIdleWasEnabled
= pDoc
->IsIdleEnabled();
147 pDoc
->EnableIdle(false);
148 // Interpreter may be called in many circumstances, also if another
149 // progress bar is active, for example while adapting row heights.
150 // Keep the dummy interpret progress.
151 if ( !pGlobalProgress
)
152 pInterpretProgress
= new ScProgress( pDoc
->GetDocumentShell(),
153 ScGlobal::GetRscString( STR_PROGRESS_CALCULATING
),
154 pDoc
->GetFormulaCodeInTree()/MIN_NO_CODES_PER_PROGRESS_UPDATE
, false, bWait
);
155 pInterpretDoc
= pDoc
;
160 void ScProgress::DeleteInterpretProgress()
162 if ( bAllowInterpretProgress
&& nInterpretProgress
)
164 /* Do not decrement 'nInterpretProgress', before 'pInterpretProgress'
165 is deleted. In rare cases, deletion of 'pInterpretProgress' causes
166 a refresh of the sheet window which may call CreateInterpretProgress
167 and DeleteInterpretProgress again (from Output::DrawStrings),
168 resulting in double deletion of 'pInterpretProgress'. */
169 if ( nInterpretProgress
== 1 )
171 if ( pInterpretProgress
!= &theDummyInterpretProgress
)
173 // move pointer to local temporary to avoid double deletion
174 ScProgress
* pTmpProgress
= pInterpretProgress
;
175 pInterpretProgress
= &theDummyInterpretProgress
;
179 pInterpretDoc
->EnableIdle(bIdleWasEnabled
);
181 --nInterpretProgress
;
185 /* vim:set shiftwidth=4 softtabstop=4 expandtab: */