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 <vcl/svapp.hxx>
21 :
22 : #include "document.hxx"
23 : #include "brdcst.hxx"
24 : #include "bcaslot.hxx"
25 : #include "formulacell.hxx"
26 : #include "formula/errorcodes.hxx" // errCircularReference
27 : #include "scerrors.hxx"
28 : #include "docoptio.hxx"
29 : #include "refupdat.hxx"
30 : #include "table.hxx"
31 : #include "progress.hxx"
32 : #include "scmod.hxx" // SC_MOD
33 : #include "inputopt.hxx" // GetExpandRefs
34 : #include "conditio.hxx"
35 : #include "colorscale.hxx"
36 : #include "sheetevents.hxx"
37 : #include "tokenarray.hxx"
38 : #include "listenercontext.hxx"
39 :
40 : #include <tools/shl.hxx>
41 :
42 :
43 : #include "globstr.hrc"
44 :
45 : extern const ScFormulaCell* pLastFormulaTreeTop; // cellform.cxx Err527 WorkAround
46 :
47 : // STATIC DATA -----------------------------------------------------------
48 :
49 : // -----------------------------------------------------------------------
50 :
51 5431 : void ScDocument::StartListeningArea( const ScRange& rRange,
52 : SvtListener* pListener
53 : )
54 : {
55 5431 : if ( pBASM )
56 5431 : pBASM->StartListeningArea( rRange, pListener );
57 5431 : }
58 :
59 :
60 4765 : void ScDocument::EndListeningArea( const ScRange& rRange,
61 : SvtListener* pListener
62 : )
63 : {
64 4765 : if ( pBASM )
65 4765 : pBASM->EndListeningArea( rRange, pListener );
66 4765 : }
67 :
68 45405 : void ScDocument::Broadcast( const ScHint& rHint )
69 : {
70 45405 : if ( !pBASM )
71 46119 : return ; // Clipboard or Undo
72 44691 : if ( !bHardRecalcState )
73 : {
74 44615 : ScBulkBroadcast aBulkBroadcast( pBASM); // scoped bulk broadcast
75 44615 : bool bIsBroadcasted = false;
76 44615 : SvtBroadcaster* pBC = GetBroadcaster(rHint.GetAddress());
77 44615 : if ( pBC )
78 : {
79 192 : pBC->Broadcast( rHint );
80 192 : bIsBroadcasted = true;
81 : }
82 44615 : if ( pBASM->AreaBroadcast( rHint ) || bIsBroadcasted )
83 305 : TrackFormulas( rHint.GetId() );
84 : }
85 :
86 : // Repaint fuer bedingte Formate mit relativen Referenzen:
87 117685 : for(SCTAB nTab = 0; nTab < static_cast<SCTAB>(maTabs.size()); ++nTab)
88 : {
89 72994 : if(!maTabs[nTab])
90 0 : continue;
91 :
92 72994 : ScConditionalFormatList* pCondFormList = GetCondFormList(nTab);
93 72994 : if ( pCondFormList && rHint.GetAddress() != BCA_BRDCST_ALWAYS )
94 47483 : pCondFormList->SourceChanged( rHint.GetAddress() );
95 :
96 : }
97 :
98 44691 : if ( rHint.GetAddress() != BCA_BRDCST_ALWAYS )
99 : {
100 31780 : SCTAB nTab = rHint.GetAddress().Tab();
101 31780 : if (nTab < static_cast<SCTAB>(maTabs.size()) && maTabs[nTab] && maTabs[nTab]->IsStreamValid())
102 8 : maTabs[nTab]->SetStreamValid(false);
103 : }
104 : }
105 :
106 :
107 5 : void ScDocument::AreaBroadcast( const ScHint& rHint )
108 : {
109 5 : if ( !pBASM )
110 5 : return ; // Clipboard or Undo
111 5 : if ( !bHardRecalcState )
112 : {
113 5 : ScBulkBroadcast aBulkBroadcast( pBASM); // scoped bulk broadcast
114 5 : if ( pBASM->AreaBroadcast( rHint ) )
115 0 : TrackFormulas( rHint.GetId() );
116 : }
117 :
118 13 : for(SCTAB nTab = 0; nTab < static_cast<SCTAB>(maTabs.size()); ++nTab)
119 : {
120 8 : if(!maTabs[nTab])
121 0 : continue;
122 :
123 8 : ScConditionalFormatList* pCondFormList = GetCondFormList(nTab);
124 8 : if ( pCondFormList && rHint.GetAddress() != BCA_BRDCST_ALWAYS )
125 8 : pCondFormList->SourceChanged( rHint.GetAddress() );
126 : }
127 : }
128 :
129 :
130 3 : void ScDocument::AreaBroadcastInRange( const ScRange& rRange, const ScHint& rHint )
131 : {
132 3 : if ( !pBASM )
133 3 : return ; // Clipboard or Undo
134 3 : if ( !bHardRecalcState )
135 : {
136 3 : ScBulkBroadcast aBulkBroadcast( pBASM); // scoped bulk broadcast
137 3 : if ( pBASM->AreaBroadcastInRange( rRange, rHint ) )
138 0 : TrackFormulas( rHint.GetId() );
139 : }
140 :
141 : // Repaint for conditional formats containing relative references.
142 : //! This is _THE_ bottle neck!
143 3 : TableContainer::iterator itr = maTabs.begin();
144 6 : for(; itr != maTabs.end(); ++itr)
145 : {
146 3 : if(!*itr)
147 0 : continue;
148 :
149 3 : ScConditionalFormatList* pCondFormList = (*itr)->GetCondFormList();
150 3 : if ( pCondFormList )
151 : {
152 : SCCOL nCol1;
153 : SCROW nRow1;
154 : SCTAB nTab1;
155 : SCCOL nCol2;
156 : SCROW nRow2;
157 : SCTAB nTab2;
158 3 : rRange.GetVars( nCol1, nRow1, nTab1, nCol2, nRow2, nTab2 );
159 3 : ScAddress aAddress( rRange.aStart );
160 6 : for ( SCTAB nTab = nTab1; nTab <= nTab2; ++nTab )
161 : {
162 3 : aAddress.SetTab( nTab );
163 6 : for ( SCCOL nCol = nCol1; nCol <= nCol2; ++nCol )
164 : {
165 3 : aAddress.SetCol( nCol );
166 6 : for ( SCROW nRow = nRow1; nRow <= nRow2; ++nRow )
167 : {
168 3 : aAddress.SetRow( nRow );
169 3 : pCondFormList->SourceChanged( aAddress );
170 : }
171 : }
172 : }
173 : }
174 :
175 : }
176 : }
177 :
178 :
179 76 : void ScDocument::DelBroadcastAreasInRange( const ScRange& rRange )
180 : {
181 76 : if ( pBASM )
182 76 : pBASM->DelBroadcastAreasInRange( rRange );
183 76 : }
184 :
185 5650 : void ScDocument::StartListeningCell( const ScAddress& rAddress,
186 : SvtListener* pListener )
187 : {
188 : OSL_ENSURE(pListener, "StartListeningCell: pListener Null");
189 5650 : SCTAB nTab = rAddress.Tab();
190 5650 : if (ValidTab(nTab) && nTab < static_cast<SCTAB>(maTabs.size()) && maTabs[nTab])
191 5650 : maTabs[nTab]->StartListening( rAddress, pListener );
192 5650 : }
193 :
194 276 : void ScDocument::EndListeningCell( const ScAddress& rAddress,
195 : SvtListener* pListener )
196 : {
197 : OSL_ENSURE(pListener, "EndListeningCell: pListener Null");
198 276 : SCTAB nTab = rAddress.Tab();
199 276 : if (ValidTab(nTab) && nTab < static_cast<SCTAB>(maTabs.size()) && maTabs[nTab])
200 276 : maTabs[nTab]->EndListening( rAddress, pListener );
201 276 : }
202 :
203 12 : void ScDocument::StartListeningCell(
204 : sc::StartListeningContext& rCxt, const ScAddress& rPos, SvtListener& rListener )
205 : {
206 12 : ScTable* pTab = FetchTable(rPos.Tab());
207 12 : if (!pTab)
208 12 : return;
209 :
210 12 : pTab->StartListening(rCxt, rPos.Col(), rPos.Row(), rListener);
211 : }
212 :
213 38 : void ScDocument::EndListeningCell(
214 : sc::EndListeningContext& rCxt, const ScAddress& rPos, SvtListener& rListener )
215 : {
216 38 : ScTable* pTab = FetchTable(rPos.Tab());
217 38 : if (!pTab)
218 38 : return;
219 :
220 38 : pTab->EndListening(rCxt, rPos.Col(), rPos.Row(), rListener);
221 : }
222 :
223 4214 : void ScDocument::EndListeningFormulaCells( std::vector<ScFormulaCell*>& rCells )
224 : {
225 4214 : if (rCells.empty())
226 8387 : return;
227 :
228 41 : sc::EndListeningContext aCxt(*this);
229 41 : std::vector<ScFormulaCell*>::iterator it = rCells.begin(), itEnd = rCells.end();
230 158 : for (; it != itEnd; ++it)
231 117 : (*it)->EndListeningTo(aCxt);
232 :
233 41 : aCxt.purgeEmptyBroadcasters();
234 : }
235 :
236 3898 : void ScDocument::PutInFormulaTree( ScFormulaCell* pCell )
237 : {
238 : OSL_ENSURE( pCell, "PutInFormulaTree: pCell Null" );
239 3898 : RemoveFromFormulaTree( pCell );
240 : // anhaengen
241 3898 : if ( pEOFormulaTree )
242 3516 : pEOFormulaTree->SetNext( pCell );
243 : else
244 382 : pFormulaTree = pCell; // kein Ende, kein Anfang..
245 3898 : pCell->SetPrevious( pEOFormulaTree );
246 3898 : pCell->SetNext( 0 );
247 3898 : pEOFormulaTree = pCell;
248 3898 : nFormulaCodeInTree += pCell->GetCode()->GetCodeLen();
249 3898 : }
250 :
251 :
252 19530 : void ScDocument::RemoveFromFormulaTree( ScFormulaCell* pCell )
253 : {
254 : OSL_ENSURE( pCell, "RemoveFromFormulaTree: pCell Null" );
255 19530 : ScFormulaCell* pPrev = pCell->GetPrevious();
256 : // wenn die Zelle die erste oder sonstwo ist
257 19530 : if ( pPrev || pFormulaTree == pCell )
258 : {
259 3796 : ScFormulaCell* pNext = pCell->GetNext();
260 3796 : if ( pPrev )
261 2324 : pPrev->SetNext( pNext ); // gibt Vorlaeufer
262 : else
263 1472 : pFormulaTree = pNext; // ist erste Zelle
264 3796 : if ( pNext )
265 3235 : pNext->SetPrevious( pPrev ); // gibt Nachfolger
266 : else
267 561 : pEOFormulaTree = pPrev; // ist letzte Zelle
268 3796 : pCell->SetPrevious( 0 );
269 3796 : pCell->SetNext( 0 );
270 3796 : sal_uInt16 nRPN = pCell->GetCode()->GetCodeLen();
271 3796 : if ( nFormulaCodeInTree >= nRPN )
272 3511 : nFormulaCodeInTree -= nRPN;
273 : else
274 : {
275 : OSL_FAIL( "RemoveFromFormulaTree: nFormulaCodeInTree < nRPN" );
276 285 : nFormulaCodeInTree = 0;
277 3796 : }
278 : }
279 15734 : else if ( !pFormulaTree && nFormulaCodeInTree )
280 : {
281 : OSL_FAIL( "!pFormulaTree && nFormulaCodeInTree != 0" );
282 0 : nFormulaCodeInTree = 0;
283 : }
284 19530 : }
285 :
286 :
287 2644 : bool ScDocument::IsInFormulaTree( ScFormulaCell* pCell ) const
288 : {
289 2644 : return pCell->GetPrevious() || pFormulaTree == pCell;
290 : }
291 :
292 :
293 30 : void ScDocument::CalcFormulaTree( bool bOnlyForced, bool bProgressBar, bool bSetAllDirty )
294 : {
295 : OSL_ENSURE( !IsCalculatingFormulaTree(), "CalcFormulaTree recursion" );
296 : // never ever recurse into this, might end up lost in infinity
297 30 : if ( IsCalculatingFormulaTree() )
298 30 : return ;
299 30 : bCalculatingFormulaTree = true;
300 :
301 30 : SetForcedFormulaPending( false );
302 30 : bool bOldIdleEnabled = IsIdleEnabled();
303 30 : EnableIdle(false);
304 30 : bool bOldAutoCalc = GetAutoCalc();
305 : //! _nicht_ SetAutoCalc( true ) weil das evtl. CalcFormulaTree( true )
306 : //! aufruft, wenn vorher disabled war und bHasForcedFormulas gesetzt ist
307 30 : bAutoCalc = true;
308 30 : if ( bHardRecalcState )
309 0 : CalcAll();
310 : else
311 : {
312 30 : ScFormulaCell* pCell = pFormulaTree;
313 68 : while ( pCell )
314 : {
315 8 : if ( pCell->GetDirty() )
316 6 : pCell = pCell->GetNext(); // alles klar
317 : else
318 : {
319 2 : if ( pCell->GetCode()->IsRecalcModeAlways() )
320 : {
321 : // pCell wird im SetDirty neu angehaengt!
322 2 : ScFormulaCell* pNext = pCell->GetNext();
323 2 : pCell->SetDirty();
324 : // falls pNext==0 und neue abhaengige hinten angehaengt
325 : // wurden, so macht das nichts, da die alle bDirty sind
326 2 : pCell = pNext;
327 : }
328 : else
329 : { // andere simpel berechnen
330 0 : if( bSetAllDirty )
331 0 : pCell->SetDirtyVar();
332 0 : pCell = pCell->GetNext();
333 : }
334 : }
335 : }
336 30 : bool bProgress = !bOnlyForced && nFormulaCodeInTree && bProgressBar;
337 30 : if ( bProgress )
338 3 : ScProgress::CreateInterpretProgress( this, true );
339 :
340 30 : pCell = pFormulaTree;
341 30 : ScFormulaCell* pLastNoGood = 0;
342 68 : while ( pCell )
343 : {
344 : // Interpret setzt bDirty zurueck und callt Remove, auch der referierten!
345 : // bei RECALCMODE_ALWAYS bleibt die Zelle
346 8 : if ( bOnlyForced )
347 : {
348 0 : if ( pCell->GetCode()->IsRecalcModeForced() )
349 0 : pCell->Interpret();
350 : }
351 : else
352 : {
353 8 : pCell->Interpret();
354 : }
355 8 : if ( pCell->GetPrevious() || pCell == pFormulaTree )
356 : { // (IsInFormulaTree(pCell)) kein Remove gewesen => next
357 2 : pLastNoGood = pCell;
358 2 : pCell = pCell->GetNext();
359 : }
360 : else
361 : {
362 6 : if ( pFormulaTree )
363 : {
364 0 : if ( pFormulaTree->GetDirty() && !bOnlyForced )
365 : {
366 0 : pCell = pFormulaTree;
367 0 : pLastNoGood = 0;
368 : }
369 : else
370 : {
371 : // IsInFormulaTree(pLastNoGood)
372 0 : if ( pLastNoGood && (pLastNoGood->GetPrevious() ||
373 0 : pLastNoGood == pFormulaTree) )
374 0 : pCell = pLastNoGood->GetNext();
375 : else
376 : {
377 0 : pCell = pFormulaTree;
378 0 : while ( pCell && !pCell->GetDirty() )
379 0 : pCell = pCell->GetNext();
380 0 : if ( pCell )
381 0 : pLastNoGood = pCell->GetPrevious();
382 : }
383 : }
384 : }
385 : else
386 6 : pCell = 0;
387 : }
388 8 : if ( ScProgress::IsUserBreak() )
389 0 : pCell = 0;
390 : }
391 30 : if ( bProgress )
392 3 : ScProgress::DeleteInterpretProgress();
393 : }
394 30 : bAutoCalc = bOldAutoCalc;
395 30 : EnableIdle(bOldIdleEnabled);
396 30 : bCalculatingFormulaTree = false;
397 : }
398 :
399 :
400 113 : void ScDocument::ClearFormulaTree()
401 : {
402 : ScFormulaCell* pCell;
403 113 : ScFormulaCell* pTree = pFormulaTree;
404 250 : while ( pTree )
405 : {
406 24 : pCell = pTree;
407 24 : pTree = pCell->GetNext();
408 24 : if ( !pCell->GetCode()->IsRecalcModeAlways() )
409 0 : RemoveFromFormulaTree( pCell );
410 : }
411 113 : }
412 :
413 :
414 1332 : void ScDocument::AppendToFormulaTrack( ScFormulaCell* pCell )
415 : {
416 : OSL_ENSURE( pCell, "AppendToFormulaTrack: pCell Null" );
417 : // Zelle kann nicht in beiden Listen gleichzeitig sein
418 1332 : RemoveFromFormulaTrack( pCell );
419 1332 : RemoveFromFormulaTree( pCell );
420 1332 : if ( pEOFormulaTrack )
421 106 : pEOFormulaTrack->SetNextTrack( pCell );
422 : else
423 1226 : pFormulaTrack = pCell; // kein Ende, kein Anfang..
424 1332 : pCell->SetPreviousTrack( pEOFormulaTrack );
425 1332 : pCell->SetNextTrack( 0 );
426 1332 : pEOFormulaTrack = pCell;
427 1332 : ++nFormulaTrackCount;
428 1332 : }
429 :
430 :
431 2664 : void ScDocument::RemoveFromFormulaTrack( ScFormulaCell* pCell )
432 : {
433 : OSL_ENSURE( pCell, "RemoveFromFormulaTrack: pCell Null" );
434 2664 : ScFormulaCell* pPrev = pCell->GetPreviousTrack();
435 : // wenn die Zelle die erste oder sonstwo ist
436 2664 : if ( pPrev || pFormulaTrack == pCell )
437 : {
438 1332 : ScFormulaCell* pNext = pCell->GetNextTrack();
439 1332 : if ( pPrev )
440 0 : pPrev->SetNextTrack( pNext ); // gibt Vorlaeufer
441 : else
442 1332 : pFormulaTrack = pNext; // ist erste Zelle
443 1332 : if ( pNext )
444 106 : pNext->SetPreviousTrack( pPrev ); // gibt Nachfolger
445 : else
446 1226 : pEOFormulaTrack = pPrev; // ist letzte Zelle
447 1332 : pCell->SetPreviousTrack( 0 );
448 1332 : pCell->SetNextTrack( 0 );
449 1332 : --nFormulaTrackCount;
450 : }
451 2664 : }
452 :
453 :
454 296 : bool ScDocument::IsInFormulaTrack( ScFormulaCell* pCell ) const
455 : {
456 296 : return pCell->GetPreviousTrack() || pFormulaTrack == pCell;
457 : }
458 :
459 :
460 : /*
461 : Der erste wird gebroadcastet,
462 : die dadurch entstehenden werden durch das Notify an den Track gehaengt.
463 : Der nachfolgende broadcastet wieder usw.
464 : View stoesst Interpret an.
465 : */
466 1349 : void ScDocument::TrackFormulas( sal_uLong nHintId )
467 : {
468 :
469 1349 : if ( pFormulaTrack )
470 : {
471 : // outside the loop, check if any sheet has a "calculate" event script
472 1226 : bool bCalcEvent = HasAnySheetEventScript( SC_SHEETEVENT_CALCULATE, true );
473 : SvtBroadcaster* pBC;
474 : ScFormulaCell* pTrack;
475 : ScFormulaCell* pNext;
476 1226 : pTrack = pFormulaTrack;
477 1332 : do
478 : {
479 1332 : pBC = GetBroadcaster(pTrack->aPos);
480 1332 : ScHint aHint(nHintId, pTrack->aPos);
481 1332 : if (pBC)
482 43 : pBC->Broadcast( aHint );
483 1332 : pBASM->AreaBroadcast( aHint );
484 : // Repaint fuer bedingte Formate mit relativen Referenzen:
485 1332 : TableContainer::iterator itr = maTabs.begin();
486 4313 : for(; itr != maTabs.end(); ++itr)
487 : {
488 2981 : if(!*itr)
489 0 : continue;
490 2981 : ScConditionalFormatList* pCondFormList = (*itr)->GetCondFormList();
491 2981 : if ( pCondFormList )
492 2981 : pCondFormList->SourceChanged( pTrack->aPos );
493 : }
494 : // for "calculate" event, keep track of which sheets are affected by tracked formulas
495 1332 : if ( bCalcEvent )
496 0 : SetCalcNotification( pTrack->aPos.Tab() );
497 1332 : pTrack = pTrack->GetNextTrack();
498 : } while ( pTrack );
499 1226 : pTrack = pFormulaTrack;
500 1226 : bool bHaveForced = false;
501 1332 : do
502 : {
503 1332 : pNext = pTrack->GetNextTrack();
504 1332 : RemoveFromFormulaTrack( pTrack );
505 1332 : PutInFormulaTree( pTrack );
506 1332 : if ( pTrack->GetCode()->IsRecalcModeForced() )
507 0 : bHaveForced = true;
508 1332 : pTrack = pNext;
509 : } while ( pTrack );
510 1226 : if ( bHaveForced )
511 : {
512 0 : SetForcedFormulas( true );
513 0 : if ( bAutoCalc && !IsAutoCalcShellDisabled() && !IsInInterpreter()
514 0 : && !IsCalculatingFormulaTree() )
515 0 : CalcFormulaTree( true );
516 : else
517 0 : SetForcedFormulaPending( true );
518 : }
519 : }
520 : OSL_ENSURE( nFormulaTrackCount==0, "TrackFormulas: nFormulaTrackCount!=0" );
521 1349 : }
522 :
523 :
524 7 : void ScDocument::StartAllListeners()
525 : {
526 14 : for ( SCTAB i = 0; i < static_cast<SCTAB>(maTabs.size()); ++i )
527 7 : if ( maTabs[i] )
528 7 : maTabs[i]->StartAllListeners();
529 7 : }
530 :
531 138 : void ScDocument::UpdateBroadcastAreas( UpdateRefMode eUpdateRefMode,
532 : const ScRange& rRange, SCsCOL nDx, SCsROW nDy, SCsTAB nDz
533 : )
534 : {
535 138 : bool bExpandRefsOld = IsExpandRefs();
536 138 : if ( eUpdateRefMode == URM_INSDEL && (nDx > 0 || nDy > 0 || nDz > 0) )
537 63 : SetExpandRefs( SC_MOD()->GetInputOptions().GetExpandRefs() );
538 138 : if ( pBASM )
539 138 : pBASM->UpdateBroadcastAreas( eUpdateRefMode, rRange, nDx, nDy, nDz );
540 138 : SetExpandRefs( bExpandRefsOld );
541 138 : }
542 :
543 1292938 : void ScDocument::SetAutoCalc( bool bNewAutoCalc )
544 : {
545 1292938 : bool bOld = bAutoCalc;
546 1292938 : bAutoCalc = bNewAutoCalc;
547 1292938 : if ( !bOld && bNewAutoCalc && bHasForcedFormulas )
548 : {
549 0 : if ( IsAutoCalcShellDisabled() )
550 0 : SetForcedFormulaPending( true );
551 0 : else if ( !IsInInterpreter() )
552 0 : CalcFormulaTree( true );
553 : }
554 1293031 : }
555 :
556 :
557 :
558 : /* vim:set shiftwidth=4 softtabstop=4 expandtab: */
|