Update ooo320-m1
[ooovba.git] / sc / source / filter / ftools / fprogressbar.cxx
blobc6e40b61ef5354ef94b67681489afe6b474fdde6
1 /*************************************************************************
3 * DO NOT ALTER OR REMOVE COPYRIGHT NOTICES OR THIS FILE HEADER.
4 *
5 * Copyright 2008 by Sun Microsystems, Inc.
7 * OpenOffice.org - a multi-platform office productivity suite
9 * $RCSfile: fprogressbar.cxx,v $
10 * $Revision: 1.10.32.2 $
12 * This file is part of OpenOffice.org.
14 * OpenOffice.org is free software: you can redistribute it and/or modify
15 * it under the terms of the GNU Lesser General Public License version 3
16 * only, as published by the Free Software Foundation.
18 * OpenOffice.org is distributed in the hope that it will be useful,
19 * but WITHOUT ANY WARRANTY; without even the implied warranty of
20 * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
21 * GNU Lesser General Public License version 3 for more details
22 * (a copy is included in the LICENSE file that accompanied this code).
24 * You should have received a copy of the GNU Lesser General Public License
25 * version 3 along with OpenOffice.org. If not, see
26 * <http://www.openoffice.org/license.html>
27 * for a copy of the LGPLv3 License.
29 ************************************************************************/
31 // MARKER(update_precomp.py): autogen include statement, do not remove
32 #include "precompiled_sc.hxx"
33 #include "fprogressbar.hxx"
34 #include "global.hxx"
35 #include "progress.hxx"
37 // ============================================================================
39 ScfProgressBar::ScfProgressSegment::ScfProgressSegment( sal_Size nSize ) :
40 mxProgress( 0 ),
41 mnSize( nSize ),
42 mnPos( 0 )
46 ScfProgressBar::ScfProgressSegment::~ScfProgressSegment()
50 // ============================================================================
52 ScfProgressBar::ScfProgressBar( SfxObjectShell* pDocShell, const String& rText ) :
53 maText( rText )
55 Init( pDocShell );
58 ScfProgressBar::ScfProgressBar( SfxObjectShell* pDocShell, USHORT nResId ) :
59 maText( ScGlobal::GetRscString( nResId ) )
61 Init( pDocShell );
64 ScfProgressBar::ScfProgressBar( ScfProgressBar& rParProgress, ScfProgressSegment* pParSegment )
66 Init( rParProgress.mpDocShell );
67 mpParentProgress = &rParProgress;
68 mpParentSegment = pParSegment;
71 ScfProgressBar::~ScfProgressBar()
75 void ScfProgressBar::Init( SfxObjectShell* pDocShell )
77 mpDocShell = pDocShell;
78 mpParentProgress = 0;
79 mpParentSegment = mpCurrSegment = 0;
80 mnTotalSize = mnTotalPos = mnUnitSize = mnNextUnitPos = 0;
81 mnSysProgressScale = 1; // used to workaround the ULONG_MAX/100 limit
82 mbInProgress = false;
85 ScfProgressBar::ScfProgressSegment* ScfProgressBar::GetSegment( sal_Int32 nSegment ) const
87 if( nSegment < 0 )
88 return 0;
89 DBG_ASSERT( maSegments.GetObject( nSegment ), "ScfProgressBar::GetSegment - invalid segment index" );
90 return maSegments.GetObject( nSegment );
93 void ScfProgressBar::SetCurrSegment( ScfProgressSegment* pSegment )
95 if( mpCurrSegment != pSegment )
97 mpCurrSegment = pSegment;
99 if( mpParentProgress && mpParentSegment )
101 mpParentProgress->SetCurrSegment( mpParentSegment );
103 else if( !mxSysProgress.get() && (mnTotalSize > 0) )
105 // System progress has an internal limit of ULONG_MAX/100.
106 mnSysProgressScale = 1;
107 ULONG nSysTotalSize = static_cast< ULONG >( mnTotalSize );
108 while( nSysTotalSize >= ULONG_MAX / 100 )
110 nSysTotalSize /= 2;
111 mnSysProgressScale *= 2;
113 mxSysProgress.reset( new ScProgress( mpDocShell, maText, nSysTotalSize ) );
116 if( !mbInProgress && mpCurrSegment && (mnTotalSize > 0) )
118 mnUnitSize = mnTotalSize / 256 + 1; // at most 256 calls of system progress
119 mnNextUnitPos = 0;
120 mbInProgress = true;
125 void ScfProgressBar::IncreaseProgressBar( sal_Size nDelta )
127 sal_Size nNewPos = mnTotalPos + nDelta;
129 // call back to parent progress bar
130 if( mpParentProgress && mpParentSegment )
132 // calculate new position of parent progress bar
133 sal_Size nParentPos = static_cast< sal_Size >(
134 static_cast< double >( nNewPos ) * mpParentSegment->mnSize / mnTotalSize );
135 mpParentProgress->ProgressAbs( nParentPos );
137 // modify system progress bar
138 else if( mxSysProgress.get() )
140 if( nNewPos >= mnNextUnitPos )
142 mnNextUnitPos = nNewPos + mnUnitSize;
143 mxSysProgress->SetState( static_cast< ULONG >( nNewPos / mnSysProgressScale ) );
146 else
148 DBG_ERRORFILE( "ScfProgressBar::IncreaseProgressBar - no progress bar found" );
151 mnTotalPos = nNewPos;
154 sal_Int32 ScfProgressBar::AddSegment( sal_Size nSize )
156 DBG_ASSERT( !mbInProgress, "ScfProgressBar::AddSegment - already in progress mode" );
157 if( nSize == 0 )
158 return SCF_INV_SEGMENT;
160 maSegments.Append( new ScfProgressSegment( nSize ) );
161 mnTotalSize += nSize;
162 return static_cast< sal_Int32 >( maSegments.Count() - 1 );
165 ScfProgressBar& ScfProgressBar::GetSegmentProgressBar( sal_Int32 nSegment )
167 ScfProgressSegment* pSegment = GetSegment( nSegment );
168 DBG_ASSERT( !pSegment || (pSegment->mnPos == 0), "ScfProgressBar::GetSegmentProgressBar - segment already started" );
169 if( pSegment && (pSegment->mnPos == 0) )
171 if( !pSegment->mxProgress.get() )
172 pSegment->mxProgress.reset( new ScfProgressBar( *this, pSegment ) );
173 return *pSegment->mxProgress;
175 return *this;
178 bool ScfProgressBar::IsFull() const
180 DBG_ASSERT( mbInProgress && mpCurrSegment, "ScfProgressBar::IsFull - no segment started" );
181 return mpCurrSegment && (mpCurrSegment->mnPos >= mpCurrSegment->mnSize);
184 void ScfProgressBar::ActivateSegment( sal_Int32 nSegment )
186 DBG_ASSERT( mnTotalSize > 0, "ScfProgressBar::ActivateSegment - progress range is zero" );
187 if( mnTotalSize > 0 )
188 SetCurrSegment( GetSegment( nSegment ) );
191 void ScfProgressBar::ProgressAbs( sal_Size nPos )
193 DBG_ASSERT( mbInProgress && mpCurrSegment, "ScfProgressBar::ProgressAbs - no segment started" );
194 if( mpCurrSegment )
196 DBG_ASSERT( mpCurrSegment->mnPos <= nPos, "ScfProgressBar::ProgressAbs - delta pos < 0" );
197 DBG_ASSERT( nPos <= mpCurrSegment->mnSize, "ScfProgressBar::ProgressAbs - segment overflow" );
198 if( (mpCurrSegment->mnPos < nPos) && (nPos <= mpCurrSegment->mnSize) )
200 IncreaseProgressBar( nPos - mpCurrSegment->mnPos );
201 mpCurrSegment->mnPos = nPos;
206 void ScfProgressBar::Progress( sal_Size nDelta )
208 ProgressAbs( mpCurrSegment ? (mpCurrSegment->mnPos + nDelta) : 0 );
211 // ============================================================================
213 ScfSimpleProgressBar::ScfSimpleProgressBar( sal_Size nSize, SfxObjectShell* pDocShell, const String& rText ) :
214 maProgress( pDocShell, rText )
216 Init( nSize );
219 ScfSimpleProgressBar::ScfSimpleProgressBar( sal_Size nSize, SfxObjectShell* pDocShell, USHORT nResId ) :
220 maProgress( pDocShell, nResId )
222 Init( nSize );
225 void ScfSimpleProgressBar::Init( sal_Size nSize )
227 sal_Int32 nSegment = maProgress.AddSegment( nSize );
228 if( nSegment >= 0 )
229 maProgress.ActivateSegment( nSegment );
232 // ============================================================================
234 //UNUSED2008-05 ScfStreamProgressBar::ScfStreamProgressBar( SvStream& rStrm, SfxObjectShell* pDocShell, const String& rText ) :
235 //UNUSED2008-05 mrStrm( rStrm )
236 //UNUSED2008-05 {
237 //UNUSED2008-05 Init( pDocShell, rText );
238 //UNUSED2008-05 }
240 ScfStreamProgressBar::ScfStreamProgressBar( SvStream& rStrm, SfxObjectShell* pDocShell, USHORT nResId ) :
241 mrStrm( rStrm )
243 Init( pDocShell, ScGlobal::GetRscString( nResId ) );
246 void ScfStreamProgressBar::Progress()
248 mxProgress->ProgressAbs( mrStrm.Tell() );
251 void ScfStreamProgressBar::Init( SfxObjectShell* pDocShell, const String& rText )
253 sal_Size nPos = mrStrm.Tell();
254 mrStrm.Seek( STREAM_SEEK_TO_END );
255 sal_Size nSize = mrStrm.Tell();
256 mrStrm.Seek( nPos );
258 mxProgress.reset( new ScfSimpleProgressBar( nSize, pDocShell, rText ) );
259 Progress();
262 // ============================================================================