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