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