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