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 "fprogressbar.hxx"
21 : #include "global.hxx"
22 : #include "progress.hxx"
23 :
24 216 : ScfProgressBar::ScfProgressSegment::ScfProgressSegment( sal_Size nSize ) :
25 : mxProgress( 0 ),
26 : mnSize( nSize ),
27 216 : mnPos( 0 )
28 : {
29 216 : }
30 :
31 216 : ScfProgressBar::ScfProgressSegment::~ScfProgressSegment()
32 : {
33 216 : }
34 :
35 2 : ScfProgressBar::ScfProgressBar( SfxObjectShell* pDocShell, const OUString& rText ) :
36 2 : maText( rText )
37 : {
38 2 : Init( pDocShell );
39 2 : }
40 :
41 122 : ScfProgressBar::ScfProgressBar( SfxObjectShell* pDocShell, sal_uInt16 nResId ) :
42 122 : maText( ScGlobal::GetRscString( nResId ) )
43 : {
44 122 : Init( pDocShell );
45 122 : }
46 :
47 48 : ScfProgressBar::ScfProgressBar( ScfProgressBar& rParProgress, ScfProgressSegment* pParSegment )
48 : {
49 48 : Init( rParProgress.mpDocShell );
50 48 : mpParentProgress = &rParProgress;
51 48 : mpParentSegment = pParSegment;
52 48 : }
53 :
54 280 : ScfProgressBar::~ScfProgressBar()
55 : {
56 280 : }
57 :
58 172 : void ScfProgressBar::Init( SfxObjectShell* pDocShell )
59 : {
60 172 : mpDocShell = pDocShell;
61 172 : mpParentProgress = 0;
62 172 : mpParentSegment = mpCurrSegment = 0;
63 172 : mnTotalSize = mnTotalPos = mnUnitSize = mnNextUnitPos = 0;
64 172 : mnSysProgressScale = 1; // used to workaround the ULONG_MAX/100 limit
65 172 : mbInProgress = false;
66 172 : }
67 :
68 236 : ScfProgressBar::ScfProgressSegment* ScfProgressBar::GetSegment( sal_Int32 nSegment )
69 : {
70 236 : if( nSegment < 0 )
71 0 : return 0;
72 236 : return &(maSegments.at( nSegment ));
73 : }
74 :
75 256 : void ScfProgressBar::SetCurrSegment( ScfProgressSegment* pSegment )
76 : {
77 256 : if( mpCurrSegment != pSegment )
78 : {
79 216 : mpCurrSegment = pSegment;
80 :
81 216 : if( mpParentProgress && mpParentSegment )
82 : {
83 68 : mpParentProgress->SetCurrSegment( mpParentSegment );
84 : }
85 148 : else if( !mxSysProgress.get() && (mnTotalSize > 0) )
86 : {
87 : // System progress has an internal limit of ULONG_MAX/100.
88 124 : mnSysProgressScale = 1;
89 124 : sal_uLong nSysTotalSize = static_cast< sal_uLong >( mnTotalSize );
90 248 : while( nSysTotalSize >= ULONG_MAX / 100 )
91 : {
92 0 : nSysTotalSize /= 2;
93 0 : mnSysProgressScale *= 2;
94 : }
95 124 : mxSysProgress.reset( new ScProgress( mpDocShell, maText, nSysTotalSize ) );
96 : }
97 :
98 216 : if( !mbInProgress && mpCurrSegment && (mnTotalSize > 0) )
99 : {
100 172 : mnUnitSize = mnTotalSize / 256 + 1; // at most 256 calls of system progress
101 172 : mnNextUnitPos = 0;
102 172 : mbInProgress = true;
103 : }
104 : }
105 256 : }
106 :
107 442464 : void ScfProgressBar::IncreaseProgressBar( sal_Size nDelta )
108 : {
109 442464 : sal_Size nNewPos = mnTotalPos + nDelta;
110 :
111 : // call back to parent progress bar
112 442464 : if( mpParentProgress && mpParentSegment )
113 : {
114 : // calculate new position of parent progress bar
115 : sal_Size nParentPos = static_cast< sal_Size >(
116 618 : static_cast< double >( nNewPos ) * mpParentSegment->mnSize / mnTotalSize );
117 618 : mpParentProgress->ProgressAbs( nParentPos );
118 : }
119 : // modify system progress bar
120 441846 : else if( mxSysProgress.get() )
121 : {
122 441846 : if( nNewPos >= mnNextUnitPos )
123 : {
124 8171 : mnNextUnitPos = nNewPos + mnUnitSize;
125 8171 : mxSysProgress->SetState( static_cast< sal_uLong >( nNewPos / mnSysProgressScale ) );
126 : }
127 : }
128 : else
129 : {
130 : OSL_FAIL( "ScfProgressBar::IncreaseProgressBar - no progress bar found" );
131 : }
132 :
133 442464 : mnTotalPos = nNewPos;
134 442464 : }
135 :
136 216 : sal_Int32 ScfProgressBar::AddSegment( sal_Size nSize )
137 : {
138 : OSL_ENSURE( !mbInProgress, "ScfProgressBar::AddSegment - already in progress mode" );
139 216 : if( nSize == 0 )
140 0 : return SCF_INV_SEGMENT;
141 :
142 216 : maSegments.push_back( new ScfProgressSegment( nSize ) );
143 216 : mnTotalSize += nSize;
144 216 : return static_cast< sal_Int32 >( maSegments.size() - 1 );
145 : }
146 :
147 48 : ScfProgressBar& ScfProgressBar::GetSegmentProgressBar( sal_Int32 nSegment )
148 : {
149 48 : ScfProgressSegment* pSegment = GetSegment( nSegment );
150 : OSL_ENSURE( !pSegment || (pSegment->mnPos == 0), "ScfProgressBar::GetSegmentProgressBar - segment already started" );
151 48 : if( pSegment && (pSegment->mnPos == 0) )
152 : {
153 48 : if( !pSegment->mxProgress.get() )
154 48 : pSegment->mxProgress.reset( new ScfProgressBar( *this, pSegment ) );
155 48 : return *pSegment->mxProgress;
156 : }
157 0 : return *this;
158 : }
159 :
160 694 : bool ScfProgressBar::IsFull() const
161 : {
162 : OSL_ENSURE( mbInProgress && mpCurrSegment, "ScfProgressBar::IsFull - no segment started" );
163 694 : return mpCurrSegment && (mpCurrSegment->mnPos >= mpCurrSegment->mnSize);
164 : }
165 :
166 188 : void ScfProgressBar::ActivateSegment( sal_Int32 nSegment )
167 : {
168 : OSL_ENSURE( mnTotalSize > 0, "ScfProgressBar::ActivateSegment - progress range is zero" );
169 188 : if( mnTotalSize > 0 )
170 188 : SetCurrSegment( GetSegment( nSegment ) );
171 188 : }
172 :
173 443318 : void ScfProgressBar::ProgressAbs( sal_Size nPos )
174 : {
175 : OSL_ENSURE( mbInProgress && mpCurrSegment, "ScfProgressBar::ProgressAbs - no segment started" );
176 443318 : if( mpCurrSegment )
177 : {
178 : OSL_ENSURE( mpCurrSegment->mnPos <= nPos, "ScfProgressBar::ProgressAbs - delta pos < 0" );
179 : OSL_ENSURE( nPos <= mpCurrSegment->mnSize, "ScfProgressBar::ProgressAbs - segment overflow" );
180 443318 : if( (mpCurrSegment->mnPos < nPos) && (nPos <= mpCurrSegment->mnSize) )
181 : {
182 442464 : IncreaseProgressBar( nPos - mpCurrSegment->mnPos );
183 442464 : mpCurrSegment->mnPos = nPos;
184 : }
185 : }
186 443318 : }
187 :
188 765 : void ScfProgressBar::Progress( sal_Size nDelta )
189 : {
190 765 : ProgressAbs( mpCurrSegment ? (mpCurrSegment->mnPos + nDelta) : 0 );
191 765 : }
192 :
193 2 : ScfSimpleProgressBar::ScfSimpleProgressBar( sal_Size nSize, SfxObjectShell* pDocShell, const OUString& rText ) :
194 2 : maProgress( pDocShell, rText )
195 : {
196 2 : Init( nSize );
197 2 : }
198 :
199 62 : ScfSimpleProgressBar::ScfSimpleProgressBar( sal_Size nSize, SfxObjectShell* pDocShell, sal_uInt16 nResId ) :
200 62 : maProgress( pDocShell, nResId )
201 : {
202 62 : Init( nSize );
203 62 : }
204 :
205 64 : void ScfSimpleProgressBar::Init( sal_Size nSize )
206 : {
207 64 : sal_Int32 nSegment = maProgress.AddSegment( nSize );
208 64 : if( nSegment >= 0 )
209 64 : maProgress.ActivateSegment( nSegment );
210 64 : }
211 :
212 2 : ScfStreamProgressBar::ScfStreamProgressBar( SvStream& rStrm, SfxObjectShell* pDocShell, sal_uInt16 nResId ) :
213 2 : mrStrm( rStrm )
214 : {
215 2 : Init( pDocShell, ScGlobal::GetRscString( nResId ) );
216 2 : }
217 :
218 310 : void ScfStreamProgressBar::Progress()
219 : {
220 310 : mxProgress->ProgressAbs( mrStrm.Tell() );
221 310 : }
222 :
223 2 : void ScfStreamProgressBar::Init( SfxObjectShell* pDocShell, const OUString& rText )
224 : {
225 2 : sal_Size nPos = mrStrm.Tell();
226 2 : mrStrm.Seek( STREAM_SEEK_TO_END );
227 2 : sal_Size nSize = mrStrm.Tell();
228 2 : mrStrm.Seek( nPos );
229 :
230 2 : mxProgress.reset( new ScfSimpleProgressBar( nSize, pDocShell, rText ) );
231 2 : Progress();
232 2 : }
233 :
234 : /* vim:set shiftwidth=4 softtabstop=4 expandtab: */
|