1 /* -*- Mode: C++; tab-width: 4; indent-tabs-mode: nil; c-basic-offset: 4 -*- */
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 #include <fprogressbar.hxx>
21 #include <globstr.hrc>
22 #include <scresid.hxx>
23 #include <progress.hxx>
24 #include <osl/diagnose.h>
25 #include <tools/stream.hxx>
29 ScfProgressBar::ScfProgressSegment::ScfProgressSegment( std::size_t nSize
) :
35 ScfProgressBar::ScfProgressSegment::~ScfProgressSegment()
39 ScfProgressBar::ScfProgressBar( SfxObjectShell
* pDocShell
, const OUString
& rText
) :
45 ScfProgressBar::ScfProgressBar(SfxObjectShell
* pDocShell
, const char* pResId
)
46 : maText(ScResId(pResId
))
51 ScfProgressBar::ScfProgressBar( ScfProgressBar
& rParProgress
, ScfProgressSegment
* pParSegment
)
53 Init( rParProgress
.mpDocShell
);
54 mpParentProgress
= &rParProgress
;
55 mpParentSegment
= pParSegment
;
58 ScfProgressBar::~ScfProgressBar()
62 void ScfProgressBar::Init( SfxObjectShell
* pDocShell
)
64 mpDocShell
= pDocShell
;
65 mpParentProgress
= nullptr;
66 mpParentSegment
= mpCurrSegment
= nullptr;
67 mnTotalSize
= mnTotalPos
= mnUnitSize
= mnNextUnitPos
= 0;
68 mnSysProgressScale
= 1; // used to workaround the ULONG_MAX/100 limit
72 ScfProgressBar::ScfProgressSegment
* ScfProgressBar::GetSegment( sal_Int32 nSegment
)
76 return maSegments
.at( nSegment
).get();
79 void ScfProgressBar::SetCurrSegment( ScfProgressSegment
* pSegment
)
81 if( mpCurrSegment
!= pSegment
)
83 mpCurrSegment
= pSegment
;
85 if( mpParentProgress
&& mpParentSegment
)
87 mpParentProgress
->SetCurrSegment( mpParentSegment
);
89 else if( !mxSysProgress
.get() && (mnTotalSize
> 0) )
91 // System progress has an internal limit of ULONG_MAX/100.
92 mnSysProgressScale
= 1;
93 sal_uLong nSysTotalSize
= static_cast< sal_uLong
>( mnTotalSize
);
94 while( nSysTotalSize
>= ULONG_MAX
/ 100 )
97 mnSysProgressScale
*= 2;
99 mxSysProgress
.reset( new ScProgress( mpDocShell
, maText
, nSysTotalSize
, true ) );
102 if( !mbInProgress
&& mpCurrSegment
&& (mnTotalSize
> 0) )
104 mnUnitSize
= mnTotalSize
/ 256 + 1; // at most 256 calls of system progress
111 void ScfProgressBar::IncreaseProgressBar( std::size_t nDelta
)
113 std::size_t nNewPos
= mnTotalPos
+ nDelta
;
115 // call back to parent progress bar
116 if( mpParentProgress
&& mpParentSegment
)
118 // calculate new position of parent progress bar
119 std::size_t nParentPos
= static_cast< std::size_t >(
120 static_cast< double >( nNewPos
) * mpParentSegment
->mnSize
/ mnTotalSize
);
121 mpParentProgress
->ProgressAbs( nParentPos
);
123 // modify system progress bar
124 else if( mxSysProgress
.get() )
126 if( nNewPos
>= mnNextUnitPos
)
128 mnNextUnitPos
= nNewPos
+ mnUnitSize
;
129 mxSysProgress
->SetState( static_cast< sal_uLong
>( nNewPos
/ mnSysProgressScale
) );
134 OSL_FAIL( "ScfProgressBar::IncreaseProgressBar - no progress bar found" );
137 mnTotalPos
= nNewPos
;
140 sal_Int32
ScfProgressBar::AddSegment( std::size_t nSize
)
142 OSL_ENSURE( !mbInProgress
, "ScfProgressBar::AddSegment - already in progress mode" );
144 return SCF_INV_SEGMENT
;
146 maSegments
.push_back( std::make_unique
<ScfProgressSegment
>( nSize
) );
147 mnTotalSize
+= nSize
;
148 return static_cast< sal_Int32
>( maSegments
.size() - 1 );
151 ScfProgressBar
& ScfProgressBar::GetSegmentProgressBar( sal_Int32 nSegment
)
153 ScfProgressSegment
* pSegment
= GetSegment( nSegment
);
154 OSL_ENSURE( !pSegment
|| (pSegment
->mnPos
== 0), "ScfProgressBar::GetSegmentProgressBar - segment already started" );
155 if( pSegment
&& (pSegment
->mnPos
== 0) )
157 if( !pSegment
->mxProgress
.get() )
158 pSegment
->mxProgress
.reset( new ScfProgressBar( *this, pSegment
) );
159 return *pSegment
->mxProgress
;
164 bool ScfProgressBar::IsFull() const
166 OSL_ENSURE( mbInProgress
&& mpCurrSegment
, "ScfProgressBar::IsFull - no segment started" );
167 return mpCurrSegment
&& (mpCurrSegment
->mnPos
>= mpCurrSegment
->mnSize
);
170 void ScfProgressBar::ActivateSegment( sal_Int32 nSegment
)
172 OSL_ENSURE( mnTotalSize
> 0, "ScfProgressBar::ActivateSegment - progress range is zero" );
173 if( mnTotalSize
> 0 )
174 SetCurrSegment( GetSegment( nSegment
) );
177 void ScfProgressBar::ProgressAbs( std::size_t nPos
)
179 OSL_ENSURE( mbInProgress
&& mpCurrSegment
, "ScfProgressBar::ProgressAbs - no segment started" );
182 OSL_ENSURE( mpCurrSegment
->mnPos
<= nPos
, "ScfProgressBar::ProgressAbs - delta pos < 0" );
183 OSL_ENSURE( nPos
<= mpCurrSegment
->mnSize
, "ScfProgressBar::ProgressAbs - segment overflow" );
184 if( (mpCurrSegment
->mnPos
< nPos
) && (nPos
<= mpCurrSegment
->mnSize
) )
186 IncreaseProgressBar( nPos
- mpCurrSegment
->mnPos
);
187 mpCurrSegment
->mnPos
= nPos
;
192 void ScfProgressBar::Progress( std::size_t nDelta
)
194 ProgressAbs( mpCurrSegment
? (mpCurrSegment
->mnPos
+ nDelta
) : 0 );
197 ScfSimpleProgressBar::ScfSimpleProgressBar( std::size_t nSize
, SfxObjectShell
* pDocShell
, const OUString
& rText
) :
198 maProgress( pDocShell
, rText
)
203 ScfSimpleProgressBar::ScfSimpleProgressBar(std::size_t nSize
, SfxObjectShell
* pDocShell
, const char* pResId
)
204 : maProgress(pDocShell
, pResId
)
209 void ScfSimpleProgressBar::Init( std::size_t nSize
)
211 sal_Int32 nSegment
= maProgress
.AddSegment( nSize
);
213 maProgress
.ActivateSegment( nSegment
);
216 ScfStreamProgressBar::ScfStreamProgressBar( SvStream
& rStrm
, SfxObjectShell
* pDocShell
) :
219 Init( pDocShell
, ScResId( STR_LOAD_DOC
) );
222 void ScfStreamProgressBar::Progress()
224 mxProgress
->ProgressAbs( mrStrm
.Tell() );
227 void ScfStreamProgressBar::Init( SfxObjectShell
* pDocShell
, const OUString
& rText
)
229 sal_uInt64
const nSize
= mrStrm
.TellEnd();
230 mxProgress
.reset( new ScfSimpleProgressBar( nSize
, pDocShell
, rText
) );
234 /* vim:set shiftwidth=4 softtabstop=4 expandtab: */