Avoid potential negative array index access to cached text.
[LibreOffice.git] / sc / inc / progress.hxx
blob80d01df3c894576db3715bd8630b5713247400c7
1 /* -*- Mode: C++; tab-width: 4; indent-tabs-mode: nil; c-basic-offset: 4 -*- */
2 /*
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 #pragma once
22 #include <rtl/ustring.hxx>
23 #include <sfx2/progress.hxx>
24 #include <tools/solar.h>
25 #include "scdllapi.h"
27 class ScDocument;
30 * #i102566
31 * Drawing a progress bar update is not cheap, so if we draw it on every
32 * percentage change of 200 calculations we get one progress draw per 2
33 * calculations which is slower than doing the calculations themselves. So as a
34 * rough guide only do an update per MIN_NO_CODES_PER_PROGRESS_UPDATE
35 * calculations
37 #define MIN_NO_CODES_PER_PROGRESS_UPDATE 100
39 class SC_DLLPUBLIC ScProgress
41 private:
42 static SfxProgress* pGlobalProgress;
43 static sal_uInt64 nGlobalRange;
44 static sal_uInt64 nGlobalPercent;
45 static ScProgress* pInterpretProgress;
46 static sal_uInt64 nInterpretProgress;
47 static ScDocument* pInterpretDoc;
48 static bool bIdleWasEnabled;
49 bool bEnabled;
51 std::unique_ptr<SfxProgress> pProgress;
53 ScProgress( const ScProgress& ) = delete;
54 ScProgress& operator=( const ScProgress& ) = delete;
56 static void CalcGlobalPercent( sal_uInt64 nVal )
58 nGlobalPercent = nGlobalRange ?
59 nVal * 100 / nGlobalRange : 0;
62 public:
63 static void CreateInterpretProgress( ScDocument* pDoc,
64 bool bWait = true );
65 static ScProgress* GetInterpretProgress() { return pInterpretProgress; }
66 static void DeleteInterpretProgress();
68 ScProgress( SfxObjectShell* pObjSh,
69 const OUString& rText,
70 sal_uInt64 nRange,
71 bool bWait );
72 ~ScProgress();
74 #ifdef SC_PROGRESS_CXX
75 /// for DummyInterpret only, never use otherwise!!!
76 ScProgress();
77 #endif
78 void SetState( sal_uInt64 nVal, sal_uInt64 nNewRange = 0 )
80 if ( pProgress )
82 if ( nNewRange )
83 nGlobalRange = nNewRange;
84 CalcGlobalPercent( nVal );
85 pProgress->SetState( nVal, nNewRange );
88 void SetStateCountDown( sal_uInt64 nVal )
90 if ( pProgress )
92 CalcGlobalPercent( nGlobalRange - nVal );
93 pProgress->SetState( nGlobalRange - nVal );
96 void SetStateOnPercent( sal_uInt64 nVal )
97 { /// only if percentage increased
98 if ( nGlobalRange && (nVal * 100 /
99 nGlobalRange) > nGlobalPercent )
100 SetState( nVal );
102 void SetStateCountDownOnPercent( sal_uInt64 nVal )
103 { /// only if percentage increased
104 if ( nGlobalRange &&
105 ((nGlobalRange - nVal) * 100 /
106 nGlobalRange) > nGlobalPercent )
107 SetStateCountDown( nVal );
109 sal_uInt64 GetState() const
111 if ( pProgress )
112 return pProgress->GetState();
113 return 0;
115 bool Enabled() const { return bEnabled; }
116 void Disable() { bEnabled = false; }
117 void Enable() { bEnabled = true; }
120 /* vim:set shiftwidth=4 softtabstop=4 expandtab: */