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 52 : 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;
47 :
48 0 : static bool lcl_IsHiddenDocument( SfxObjectShell* pObjSh )
49 : {
50 0 : if (pObjSh)
51 : {
52 0 : SfxMedium* pMed = pObjSh->GetMedium();
53 0 : if (pMed)
54 : {
55 0 : SfxItemSet* pSet = pMed->GetItemSet();
56 : const SfxPoolItem* pItem;
57 0 : if ( pSet && SfxItemState::SET == pSet->GetItemState( SID_HIDDEN, true, &pItem ) &&
58 0 : static_cast<const SfxBoolItem*>(pItem)->GetValue() )
59 0 : return true;
60 : }
61 : }
62 0 : return false;
63 : }
64 :
65 252 : static bool lcl_HasControllersLocked( SfxObjectShell& rObjSh )
66 : {
67 252 : uno::Reference<frame::XModel> xModel( rObjSh.GetBaseModel() );
68 252 : if (xModel.is())
69 252 : return xModel->hasControllersLocked();
70 0 : return false;
71 : }
72 :
73 533 : ScProgress::ScProgress(SfxObjectShell* pObjSh, const OUString& rText,
74 : sal_uLong nRange, bool bAllDocs, bool bWait)
75 533 : : bEnabled(true)
76 : {
77 :
78 533 : if ( pGlobalProgress || SfxProgress::GetActiveProgress( NULL ) )
79 : {
80 0 : if ( lcl_IsHiddenDocument(pObjSh) )
81 : {
82 : // loading a hidden document while a progress is active is possible - no error
83 0 : pProgress = NULL;
84 : }
85 : else
86 : {
87 : OSL_FAIL( "ScProgress: there can be only one!" );
88 0 : pProgress = NULL;
89 : }
90 : }
91 533 : else if ( SfxGetpApp()->IsDowning() )
92 : {
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 ???
96 :
97 0 : pProgress = NULL;
98 : }
99 1118 : else if ( pObjSh && ( pObjSh->GetCreateMode() == SfxObjectCreateMode::EMBEDDED ||
100 504 : pObjSh->GetProgress() ||
101 252 : lcl_HasControllersLocked(*pObjSh) ) )
102 : {
103 : // no own progress for embedded objects,
104 : // no second progress if the document already has one
105 :
106 333 : pProgress = NULL;
107 : }
108 : else
109 : {
110 200 : pProgress = new SfxProgress( pObjSh, rText, nRange, bAllDocs, bWait );
111 200 : pGlobalProgress = pProgress;
112 200 : nGlobalRange = nRange;
113 200 : nGlobalPercent = 0;
114 200 : bGlobalNoUserBreak = true;
115 : }
116 533 : }
117 :
118 52 : ScProgress::ScProgress()
119 : : bEnabled(true)
120 52 : , pProgress(NULL)
121 : {
122 : // DummyInterpret
123 52 : }
124 :
125 585 : ScProgress::~ScProgress()
126 : {
127 585 : if ( pProgress )
128 : {
129 200 : delete pProgress;
130 200 : pGlobalProgress = NULL;
131 200 : nGlobalRange = 0;
132 200 : nGlobalPercent = 0;
133 200 : bGlobalNoUserBreak = true;
134 : }
135 585 : }
136 :
137 71 : void ScProgress::CreateInterpretProgress( ScDocument* pDoc, bool bWait )
138 : {
139 71 : if ( bAllowInterpretProgress )
140 : {
141 71 : if ( nInterpretProgress )
142 0 : nInterpretProgress++;
143 71 : else if ( pDoc->GetAutoCalc() )
144 : {
145 63 : nInterpretProgress = 1;
146 63 : bIdleWasEnabled = pDoc->IsIdleEnabled();
147 63 : 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 63 : if ( !pGlobalProgress )
152 : pInterpretProgress = new ScProgress( pDoc->GetDocumentShell(),
153 : ScGlobal::GetRscString( STR_PROGRESS_CALCULATING ),
154 63 : pDoc->GetFormulaCodeInTree()/MIN_NO_CODES_PER_PROGRESS_UPDATE, false, bWait );
155 63 : pInterpretDoc = pDoc;
156 : }
157 : }
158 71 : }
159 :
160 71 : void ScProgress::DeleteInterpretProgress()
161 : {
162 71 : if ( bAllowInterpretProgress && nInterpretProgress )
163 : {
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 63 : if ( nInterpretProgress == 1 )
170 : {
171 63 : if ( pInterpretProgress != &theDummyInterpretProgress )
172 : {
173 : // move pointer to local temporary to avoid double deletion
174 63 : ScProgress* pTmpProgress = pInterpretProgress;
175 63 : pInterpretProgress = &theDummyInterpretProgress;
176 63 : delete pTmpProgress;
177 : }
178 63 : if ( pInterpretDoc )
179 63 : pInterpretDoc->EnableIdle(bIdleWasEnabled);
180 : }
181 63 : --nInterpretProgress;
182 : }
183 227 : }
184 :
185 : /* vim:set shiftwidth=4 softtabstop=4 expandtab: */
|