Line data Source code
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 :
28 : #define SC_PROGRESS_CXX
29 : #include "progress.hxx"
30 : #include "document.hxx"
31 : #include "global.hxx"
32 : #include "globstr.hrc"
33 :
34 : using namespace com::sun::star;
35 :
36 :
37 34 : static ScProgress theDummyInterpretProgress;
38 : SfxProgress* ScProgress::pGlobalProgress = NULL;
39 : sal_uLong ScProgress::nGlobalRange = 0;
40 : sal_uLong ScProgress::nGlobalPercent = 0;
41 : sal_Bool ScProgress::bGlobalNoUserBreak = sal_True;
42 : ScProgress* ScProgress::pInterpretProgress = &theDummyInterpretProgress;
43 : ScProgress* ScProgress::pOldInterpretProgress = NULL;
44 : sal_uLong ScProgress::nInterpretProgress = 0;
45 : sal_Bool ScProgress::bAllowInterpretProgress = sal_True;
46 : ScDocument* ScProgress::pInterpretDoc;
47 : sal_Bool ScProgress::bIdleWasDisabled = false;
48 :
49 :
50 0 : static sal_Bool lcl_IsHiddenDocument( SfxObjectShell* pObjSh )
51 : {
52 0 : if (pObjSh)
53 : {
54 0 : SfxMedium* pMed = pObjSh->GetMedium();
55 0 : if (pMed)
56 : {
57 0 : SfxItemSet* pSet = pMed->GetItemSet();
58 : const SfxPoolItem* pItem;
59 0 : if ( pSet && SFX_ITEM_SET == pSet->GetItemState( SID_HIDDEN, sal_True, &pItem ) &&
60 0 : ((const SfxBoolItem*)pItem)->GetValue() )
61 0 : return sal_True;
62 : }
63 : }
64 0 : return false;
65 : }
66 :
67 28 : static bool lcl_HasControllersLocked( SfxObjectShell& rObjSh )
68 : {
69 28 : uno::Reference<frame::XModel> xModel( rObjSh.GetBaseModel() );
70 28 : if (xModel.is())
71 28 : return xModel->hasControllersLocked();
72 0 : return false;
73 : }
74 :
75 164 : ScProgress::ScProgress( SfxObjectShell* pObjSh, const String& rText,
76 : sal_uLong nRange, sal_Bool bAllDocs, sal_Bool bWait )
77 : {
78 :
79 164 : if ( pGlobalProgress || SfxProgress::GetActiveProgress( NULL ) )
80 : {
81 0 : if ( lcl_IsHiddenDocument(pObjSh) )
82 : {
83 : // loading a hidden document while a progress is active is possible - no error
84 0 : pProgress = NULL;
85 : }
86 : else
87 : {
88 : OSL_FAIL( "ScProgress: there can be only one!" );
89 0 : pProgress = NULL;
90 : }
91 : }
92 164 : else if ( SFX_APP()->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 : //! Should that be this way ???
97 :
98 0 : pProgress = NULL;
99 : }
100 220 : else if ( pObjSh && ( pObjSh->GetCreateMode() == SFX_CREATE_MODE_EMBEDDED ||
101 28 : pObjSh->GetProgress() ||
102 28 : lcl_HasControllersLocked(*pObjSh) ) )
103 : {
104 : // no own progress for embedded objects,
105 : // no second progress if the document already has one
106 :
107 136 : pProgress = NULL;
108 : }
109 : else
110 : {
111 28 : pProgress = new SfxProgress( pObjSh, rText, nRange, bAllDocs, bWait );
112 28 : pGlobalProgress = pProgress;
113 28 : nGlobalRange = nRange;
114 28 : nGlobalPercent = 0;
115 28 : bGlobalNoUserBreak = sal_True;
116 : }
117 164 : }
118 :
119 :
120 34 : ScProgress::ScProgress() :
121 34 : pProgress( NULL )
122 : { // DummyInterpret
123 34 : }
124 :
125 :
126 198 : ScProgress::~ScProgress()
127 : {
128 198 : if ( pProgress )
129 : {
130 28 : delete pProgress;
131 28 : pGlobalProgress = NULL;
132 28 : nGlobalRange = 0;
133 28 : nGlobalPercent = 0;
134 28 : bGlobalNoUserBreak = sal_True;
135 : }
136 198 : }
137 :
138 :
139 6 : void ScProgress::CreateInterpretProgress( ScDocument* pDoc, sal_Bool bWait )
140 : {
141 6 : if ( bAllowInterpretProgress )
142 : {
143 6 : if ( nInterpretProgress )
144 0 : nInterpretProgress++;
145 6 : else if ( pDoc->GetAutoCalc() )
146 : {
147 6 : nInterpretProgress = 1;
148 6 : bIdleWasDisabled = pDoc->IsIdleDisabled();
149 6 : pDoc->DisableIdle( sal_True );
150 : // Interpreter may be called in many circumstances, also if another
151 : // progress bar is active, for example while adapting row heights.
152 : // Keep the dummy interpret progress.
153 6 : if ( !pGlobalProgress )
154 : pInterpretProgress = new ScProgress( pDoc->GetDocumentShell(),
155 : ScGlobal::GetRscString( STR_PROGRESS_CALCULATING ),
156 6 : pDoc->GetFormulaCodeInTree()/MIN_NO_CODES_PER_PROGRESS_UPDATE, false, bWait );
157 6 : pInterpretDoc = pDoc;
158 : }
159 : }
160 6 : }
161 :
162 :
163 :
164 6 : void ScProgress::DeleteInterpretProgress()
165 : {
166 6 : if ( bAllowInterpretProgress && nInterpretProgress )
167 : {
168 : /* Do not decrement 'nInterpretProgress', before 'pInterpretProgress'
169 : is deleted. In rare cases, deletion of 'pInterpretProgress' causes
170 : a refresh of the sheet window which may call CreateInterpretProgress
171 : and DeleteInterpretProgress again (from Output::DrawStrings),
172 : resulting in double deletion of 'pInterpretProgress'. */
173 6 : if ( nInterpretProgress == 1 )
174 : {
175 6 : if ( pInterpretProgress != &theDummyInterpretProgress )
176 : {
177 : // move pointer to local temporary to avoid double deletion
178 6 : ScProgress* pTmpProgress = pInterpretProgress;
179 6 : pInterpretProgress = &theDummyInterpretProgress;
180 6 : delete pTmpProgress;
181 : }
182 6 : if ( pInterpretDoc )
183 6 : pInterpretDoc->DisableIdle( bIdleWasDisabled );
184 : }
185 6 : --nInterpretProgress;
186 : }
187 108 : }
188 :
189 :
190 :
191 : /* vim:set shiftwidth=4 softtabstop=4 expandtab: */
|