xref: /trunk/main/sc/source/core/tool/progress.cxx (revision cf6516809c57e1bb0a940545cca99cdad54d4ce2)
1*b3f79822SAndrew Rist /**************************************************************
2cdf0e10cSrcweir  *
3*b3f79822SAndrew Rist  * Licensed to the Apache Software Foundation (ASF) under one
4*b3f79822SAndrew Rist  * or more contributor license agreements.  See the NOTICE file
5*b3f79822SAndrew Rist  * distributed with this work for additional information
6*b3f79822SAndrew Rist  * regarding copyright ownership.  The ASF licenses this file
7*b3f79822SAndrew Rist  * to you under the Apache License, Version 2.0 (the
8*b3f79822SAndrew Rist  * "License"); you may not use this file except in compliance
9*b3f79822SAndrew Rist  * with the License.  You may obtain a copy of the License at
10cdf0e10cSrcweir  *
11*b3f79822SAndrew Rist  *   http://www.apache.org/licenses/LICENSE-2.0
12cdf0e10cSrcweir  *
13*b3f79822SAndrew Rist  * Unless required by applicable law or agreed to in writing,
14*b3f79822SAndrew Rist  * software distributed under the License is distributed on an
15*b3f79822SAndrew Rist  * "AS IS" BASIS, WITHOUT WARRANTIES OR CONDITIONS OF ANY
16*b3f79822SAndrew Rist  * KIND, either express or implied.  See the License for the
17*b3f79822SAndrew Rist  * specific language governing permissions and limitations
18*b3f79822SAndrew Rist  * under the License.
19cdf0e10cSrcweir  *
20*b3f79822SAndrew Rist  *************************************************************/
21*b3f79822SAndrew Rist 
22*b3f79822SAndrew Rist 
23cdf0e10cSrcweir 
24cdf0e10cSrcweir // MARKER(update_precomp.py): autogen include statement, do not remove
25cdf0e10cSrcweir #include "precompiled_sc.hxx"
26cdf0e10cSrcweir 
27cdf0e10cSrcweir 
28cdf0e10cSrcweir 
29cdf0e10cSrcweir //------------------------------------------------------------------------
30cdf0e10cSrcweir 
31cdf0e10cSrcweir #include <sfx2/app.hxx>
32cdf0e10cSrcweir #include <sfx2/objsh.hxx>
33cdf0e10cSrcweir #include <sfx2/progress.hxx>
34cdf0e10cSrcweir #include <sfx2/docfile.hxx>
35cdf0e10cSrcweir #include <sfx2/sfxsids.hrc>
36cdf0e10cSrcweir #include <svl/eitem.hxx>
37cdf0e10cSrcweir #include <svl/itemset.hxx>
38cdf0e10cSrcweir 
39cdf0e10cSrcweir #define SC_PROGRESS_CXX
40cdf0e10cSrcweir #include "progress.hxx"
41cdf0e10cSrcweir #include "document.hxx"
42cdf0e10cSrcweir #include "global.hxx"
43cdf0e10cSrcweir #include "globstr.hrc"
44cdf0e10cSrcweir 
45cdf0e10cSrcweir using namespace com::sun::star;
46cdf0e10cSrcweir 
47cdf0e10cSrcweir 
48cdf0e10cSrcweir static ScProgress theDummyInterpretProgress;
49cdf0e10cSrcweir SfxProgress*    ScProgress::pGlobalProgress = NULL;
50cdf0e10cSrcweir sal_uLong           ScProgress::nGlobalRange = 0;
51cdf0e10cSrcweir sal_uLong           ScProgress::nGlobalPercent = 0;
52cdf0e10cSrcweir sal_Bool            ScProgress::bGlobalNoUserBreak = sal_True;
53cdf0e10cSrcweir ScProgress*     ScProgress::pInterpretProgress = &theDummyInterpretProgress;
54cdf0e10cSrcweir ScProgress*     ScProgress::pOldInterpretProgress = NULL;
55cdf0e10cSrcweir sal_uLong           ScProgress::nInterpretProgress = 0;
56cdf0e10cSrcweir sal_Bool            ScProgress::bAllowInterpretProgress = sal_True;
57cdf0e10cSrcweir ScDocument*     ScProgress::pInterpretDoc;
58cdf0e10cSrcweir sal_Bool            ScProgress::bIdleWasDisabled = sal_False;
59cdf0e10cSrcweir 
60cdf0e10cSrcweir 
lcl_IsHiddenDocument(SfxObjectShell * pObjSh)61cdf0e10cSrcweir sal_Bool lcl_IsHiddenDocument( SfxObjectShell* pObjSh )
62cdf0e10cSrcweir {
63cdf0e10cSrcweir     if (pObjSh)
64cdf0e10cSrcweir     {
65cdf0e10cSrcweir         SfxMedium* pMed = pObjSh->GetMedium();
66cdf0e10cSrcweir         if (pMed)
67cdf0e10cSrcweir         {
68cdf0e10cSrcweir             SfxItemSet* pSet = pMed->GetItemSet();
69cdf0e10cSrcweir             const SfxPoolItem* pItem;
70cdf0e10cSrcweir             if ( pSet && SFX_ITEM_SET == pSet->GetItemState( SID_HIDDEN, sal_True, &pItem ) &&
71cdf0e10cSrcweir                         ((const SfxBoolItem*)pItem)->GetValue() )
72cdf0e10cSrcweir                 return sal_True;
73cdf0e10cSrcweir         }
74cdf0e10cSrcweir     }
75cdf0e10cSrcweir     return sal_False;
76cdf0e10cSrcweir }
77cdf0e10cSrcweir 
lcl_HasControllersLocked(SfxObjectShell & rObjSh)78cdf0e10cSrcweir bool lcl_HasControllersLocked( SfxObjectShell& rObjSh )
79cdf0e10cSrcweir {
80cdf0e10cSrcweir     uno::Reference<frame::XModel> xModel( rObjSh.GetBaseModel() );
81cdf0e10cSrcweir     if (xModel.is())
82cdf0e10cSrcweir         return xModel->hasControllersLocked();
83cdf0e10cSrcweir     return false;
84cdf0e10cSrcweir }
85cdf0e10cSrcweir 
ScProgress(SfxObjectShell * pObjSh,const String & rText,sal_uLong nRange,sal_Bool bAllDocs,sal_Bool bWait)86cdf0e10cSrcweir ScProgress::ScProgress( SfxObjectShell* pObjSh, const String& rText,
87cdf0e10cSrcweir                         sal_uLong nRange, sal_Bool bAllDocs, sal_Bool bWait )
88cdf0e10cSrcweir {
89cdf0e10cSrcweir 
90cdf0e10cSrcweir     if ( pGlobalProgress || SfxProgress::GetActiveProgress( NULL ) )
91cdf0e10cSrcweir     {
92cdf0e10cSrcweir         if ( lcl_IsHiddenDocument(pObjSh) )
93cdf0e10cSrcweir         {
94cdf0e10cSrcweir             // loading a hidden document while a progress is active is possible - no error
95cdf0e10cSrcweir             pProgress = NULL;
96cdf0e10cSrcweir         }
97cdf0e10cSrcweir         else
98cdf0e10cSrcweir         {
99cdf0e10cSrcweir             DBG_ERROR( "ScProgress: there can be only one!" );
100cdf0e10cSrcweir             pProgress = NULL;
101cdf0e10cSrcweir         }
102cdf0e10cSrcweir     }
103cdf0e10cSrcweir     else if ( SFX_APP()->IsDowning() )
104cdf0e10cSrcweir     {
105cdf0e10cSrcweir         //  kommt vor z.B. beim Speichern des Clipboard-Inhalts als OLE beim Beenden
106cdf0e10cSrcweir         //  Dann wuerde ein SfxProgress wild im Speicher rummuellen
107cdf0e10cSrcweir         //! Soll das so sein ???
108cdf0e10cSrcweir 
109cdf0e10cSrcweir         pProgress = NULL;
110cdf0e10cSrcweir     }
111cdf0e10cSrcweir     else if ( pObjSh && ( pObjSh->GetCreateMode() == SFX_CREATE_MODE_EMBEDDED ||
112cdf0e10cSrcweir                           pObjSh->GetProgress() ||
113cdf0e10cSrcweir                           lcl_HasControllersLocked(*pObjSh) ) )
114cdf0e10cSrcweir     {
115cdf0e10cSrcweir         //  #62808# no own progress for embedded objects,
116cdf0e10cSrcweir         //  #73633# no second progress if the document already has one
117cdf0e10cSrcweir         //  #163566# no progress while controllers are locked (repaint disabled)
118cdf0e10cSrcweir 
119cdf0e10cSrcweir         pProgress = NULL;
120cdf0e10cSrcweir     }
121cdf0e10cSrcweir     else
122cdf0e10cSrcweir     {
123cdf0e10cSrcweir         pProgress = new SfxProgress( pObjSh, rText, nRange, bAllDocs, bWait );
124cdf0e10cSrcweir         pGlobalProgress = pProgress;
125cdf0e10cSrcweir         nGlobalRange = nRange;
126cdf0e10cSrcweir         nGlobalPercent = 0;
127cdf0e10cSrcweir         bGlobalNoUserBreak = sal_True;
128cdf0e10cSrcweir     }
129cdf0e10cSrcweir }
130cdf0e10cSrcweir 
131cdf0e10cSrcweir 
ScProgress()132cdf0e10cSrcweir ScProgress::ScProgress() :
133cdf0e10cSrcweir         pProgress( NULL )
134cdf0e10cSrcweir {   // DummyInterpret
135cdf0e10cSrcweir }
136cdf0e10cSrcweir 
137cdf0e10cSrcweir 
~ScProgress()138cdf0e10cSrcweir ScProgress::~ScProgress()
139cdf0e10cSrcweir {
140cdf0e10cSrcweir     if ( pProgress )
141cdf0e10cSrcweir     {
142cdf0e10cSrcweir         delete pProgress;
143cdf0e10cSrcweir         pGlobalProgress = NULL;
144cdf0e10cSrcweir         nGlobalRange = 0;
145cdf0e10cSrcweir         nGlobalPercent = 0;
146cdf0e10cSrcweir         bGlobalNoUserBreak = sal_True;
147cdf0e10cSrcweir     }
148cdf0e10cSrcweir }
149cdf0e10cSrcweir 
150cdf0e10cSrcweir // static
151cdf0e10cSrcweir 
CreateInterpretProgress(ScDocument * pDoc,sal_Bool bWait)152cdf0e10cSrcweir void ScProgress::CreateInterpretProgress( ScDocument* pDoc, sal_Bool bWait )
153cdf0e10cSrcweir {
154cdf0e10cSrcweir     if ( bAllowInterpretProgress )
155cdf0e10cSrcweir     {
156cdf0e10cSrcweir         if ( nInterpretProgress )
157cdf0e10cSrcweir             nInterpretProgress++;
158cdf0e10cSrcweir         else if ( pDoc->GetAutoCalc() )
159cdf0e10cSrcweir         {
160cdf0e10cSrcweir             nInterpretProgress = 1;
161cdf0e10cSrcweir             bIdleWasDisabled = pDoc->IsIdleDisabled();
162cdf0e10cSrcweir             pDoc->DisableIdle( sal_True );
163cdf0e10cSrcweir             // Interpreter may be called in many circumstances, also if another
164cdf0e10cSrcweir             // progress bar is active, for example while adapting row heights.
165cdf0e10cSrcweir             // Keep the dummy interpret progress.
166cdf0e10cSrcweir             if ( !pGlobalProgress )
167cdf0e10cSrcweir                 pInterpretProgress = new ScProgress( pDoc->GetDocumentShell(),
168cdf0e10cSrcweir                     ScGlobal::GetRscString( STR_PROGRESS_CALCULATING ),
169cdf0e10cSrcweir                     pDoc->GetFormulaCodeInTree()/MIN_NO_CODES_PER_PROGRESS_UPDATE, sal_False, bWait );
170cdf0e10cSrcweir             pInterpretDoc = pDoc;
171cdf0e10cSrcweir         }
172cdf0e10cSrcweir     }
173cdf0e10cSrcweir }
174cdf0e10cSrcweir 
175cdf0e10cSrcweir 
176cdf0e10cSrcweir // static
177cdf0e10cSrcweir 
DeleteInterpretProgress()178cdf0e10cSrcweir void ScProgress::DeleteInterpretProgress()
179cdf0e10cSrcweir {
180cdf0e10cSrcweir     if ( bAllowInterpretProgress && nInterpretProgress )
181cdf0e10cSrcweir     {
182cdf0e10cSrcweir         /*  Do not decrement 'nInterpretProgress', before 'pInterpretProgress'
183cdf0e10cSrcweir             is deleted. In rare cases, deletion of 'pInterpretProgress' causes
184cdf0e10cSrcweir             a refresh of the sheet window which may call CreateInterpretProgress
185cdf0e10cSrcweir             and DeleteInterpretProgress again (from Output::DrawStrings),
186cdf0e10cSrcweir             resulting in double deletion of 'pInterpretProgress'. */
187cdf0e10cSrcweir //       if ( --nInterpretProgress == 0 )
188cdf0e10cSrcweir         if ( nInterpretProgress == 1 )
189cdf0e10cSrcweir         {
190cdf0e10cSrcweir             if ( pInterpretProgress != &theDummyInterpretProgress )
191cdf0e10cSrcweir             {
192cdf0e10cSrcweir                 // move pointer to local temporary to avoid double deletion
193cdf0e10cSrcweir                 ScProgress* pTmpProgress = pInterpretProgress;
194cdf0e10cSrcweir                 pInterpretProgress = &theDummyInterpretProgress;
195cdf0e10cSrcweir                 delete pTmpProgress;
196cdf0e10cSrcweir             }
197cdf0e10cSrcweir             if ( pInterpretDoc )
198cdf0e10cSrcweir                 pInterpretDoc->DisableIdle( bIdleWasDisabled );
199cdf0e10cSrcweir         }
200cdf0e10cSrcweir         --nInterpretProgress;
201cdf0e10cSrcweir     }
202cdf0e10cSrcweir }
203