fix baseline build (old cairo) - 'cairo_rectangle_int_t' does not name a type
[LibreOffice.git] / sc / source / filter / ftools / fprogressbar.cxx
blob645323b9cf1cc80447571b51ac052829260f677d
1 /* -*- Mode: C++; tab-width: 4; indent-tabs-mode: nil; c-basic-offset: 4 -*- */
2 /*
3 * This file is part of the LibreOffice project.
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/.
9 * This file incorporates work covered by the following license notice:
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 .
20 #include "fprogressbar.hxx"
21 #include "global.hxx"
22 #include "progress.hxx"
23 #include <osl/diagnose.h>
25 ScfProgressBar::ScfProgressSegment::ScfProgressSegment( sal_Size nSize ) :
26 mnSize( nSize ),
27 mnPos( 0 )
31 ScfProgressBar::ScfProgressSegment::~ScfProgressSegment()
35 ScfProgressBar::ScfProgressBar( SfxObjectShell* pDocShell, const OUString& rText ) :
36 maText( rText )
38 Init( pDocShell );
41 ScfProgressBar::ScfProgressBar( SfxObjectShell* pDocShell, sal_uInt16 nResId ) :
42 maText( ScGlobal::GetRscString( nResId ) )
44 Init( pDocShell );
47 ScfProgressBar::ScfProgressBar( ScfProgressBar& rParProgress, ScfProgressSegment* pParSegment )
49 Init( rParProgress.mpDocShell );
50 mpParentProgress = &rParProgress;
51 mpParentSegment = pParSegment;
54 ScfProgressBar::~ScfProgressBar()
58 void ScfProgressBar::Init( SfxObjectShell* pDocShell )
60 mpDocShell = pDocShell;
61 mpParentProgress = 0;
62 mpParentSegment = mpCurrSegment = 0;
63 mnTotalSize = mnTotalPos = mnUnitSize = mnNextUnitPos = 0;
64 mnSysProgressScale = 1; // used to workaround the ULONG_MAX/100 limit
65 mbInProgress = false;
68 ScfProgressBar::ScfProgressSegment* ScfProgressBar::GetSegment( sal_Int32 nSegment )
70 if( nSegment < 0 )
71 return 0;
72 return &(maSegments.at( nSegment ));
75 void ScfProgressBar::SetCurrSegment( ScfProgressSegment* pSegment )
77 if( mpCurrSegment != pSegment )
79 mpCurrSegment = pSegment;
81 if( mpParentProgress && mpParentSegment )
83 mpParentProgress->SetCurrSegment( mpParentSegment );
85 else if( !mxSysProgress.get() && (mnTotalSize > 0) )
87 // System progress has an internal limit of ULONG_MAX/100.
88 mnSysProgressScale = 1;
89 sal_uLong nSysTotalSize = static_cast< sal_uLong >( mnTotalSize );
90 while( nSysTotalSize >= ULONG_MAX / 100 )
92 nSysTotalSize /= 2;
93 mnSysProgressScale *= 2;
95 mxSysProgress.reset( new ScProgress( mpDocShell, maText, nSysTotalSize ) );
98 if( !mbInProgress && mpCurrSegment && (mnTotalSize > 0) )
100 mnUnitSize = mnTotalSize / 256 + 1; // at most 256 calls of system progress
101 mnNextUnitPos = 0;
102 mbInProgress = true;
107 void ScfProgressBar::IncreaseProgressBar( sal_Size nDelta )
109 sal_Size nNewPos = mnTotalPos + nDelta;
111 // call back to parent progress bar
112 if( mpParentProgress && mpParentSegment )
114 // calculate new position of parent progress bar
115 sal_Size nParentPos = static_cast< sal_Size >(
116 static_cast< double >( nNewPos ) * mpParentSegment->mnSize / mnTotalSize );
117 mpParentProgress->ProgressAbs( nParentPos );
119 // modify system progress bar
120 else if( mxSysProgress.get() )
122 if( nNewPos >= mnNextUnitPos )
124 mnNextUnitPos = nNewPos + mnUnitSize;
125 mxSysProgress->SetState( static_cast< sal_uLong >( nNewPos / mnSysProgressScale ) );
128 else
130 OSL_FAIL( "ScfProgressBar::IncreaseProgressBar - no progress bar found" );
133 mnTotalPos = nNewPos;
136 sal_Int32 ScfProgressBar::AddSegment( sal_Size nSize )
138 OSL_ENSURE( !mbInProgress, "ScfProgressBar::AddSegment - already in progress mode" );
139 if( nSize == 0 )
140 return SCF_INV_SEGMENT;
142 maSegments.push_back( new ScfProgressSegment( nSize ) );
143 mnTotalSize += nSize;
144 return static_cast< sal_Int32 >( maSegments.size() - 1 );
147 ScfProgressBar& ScfProgressBar::GetSegmentProgressBar( sal_Int32 nSegment )
149 ScfProgressSegment* pSegment = GetSegment( nSegment );
150 OSL_ENSURE( !pSegment || (pSegment->mnPos == 0), "ScfProgressBar::GetSegmentProgressBar - segment already started" );
151 if( pSegment && (pSegment->mnPos == 0) )
153 if( !pSegment->mxProgress.get() )
154 pSegment->mxProgress.reset( new ScfProgressBar( *this, pSegment ) );
155 return *pSegment->mxProgress;
157 return *this;
160 bool ScfProgressBar::IsFull() const
162 OSL_ENSURE( mbInProgress && mpCurrSegment, "ScfProgressBar::IsFull - no segment started" );
163 return mpCurrSegment && (mpCurrSegment->mnPos >= mpCurrSegment->mnSize);
166 void ScfProgressBar::ActivateSegment( sal_Int32 nSegment )
168 OSL_ENSURE( mnTotalSize > 0, "ScfProgressBar::ActivateSegment - progress range is zero" );
169 if( mnTotalSize > 0 )
170 SetCurrSegment( GetSegment( nSegment ) );
173 void ScfProgressBar::ProgressAbs( sal_Size nPos )
175 OSL_ENSURE( mbInProgress && mpCurrSegment, "ScfProgressBar::ProgressAbs - no segment started" );
176 if( mpCurrSegment )
178 OSL_ENSURE( mpCurrSegment->mnPos <= nPos, "ScfProgressBar::ProgressAbs - delta pos < 0" );
179 OSL_ENSURE( nPos <= mpCurrSegment->mnSize, "ScfProgressBar::ProgressAbs - segment overflow" );
180 if( (mpCurrSegment->mnPos < nPos) && (nPos <= mpCurrSegment->mnSize) )
182 IncreaseProgressBar( nPos - mpCurrSegment->mnPos );
183 mpCurrSegment->mnPos = nPos;
188 void ScfProgressBar::Progress( sal_Size nDelta )
190 ProgressAbs( mpCurrSegment ? (mpCurrSegment->mnPos + nDelta) : 0 );
193 ScfSimpleProgressBar::ScfSimpleProgressBar( sal_Size nSize, SfxObjectShell* pDocShell, const OUString& rText ) :
194 maProgress( pDocShell, rText )
196 Init( nSize );
199 ScfSimpleProgressBar::ScfSimpleProgressBar( sal_Size nSize, SfxObjectShell* pDocShell, sal_uInt16 nResId ) :
200 maProgress( pDocShell, nResId )
202 Init( nSize );
205 void ScfSimpleProgressBar::Init( sal_Size nSize )
207 sal_Int32 nSegment = maProgress.AddSegment( nSize );
208 if( nSegment >= 0 )
209 maProgress.ActivateSegment( nSegment );
212 ScfStreamProgressBar::ScfStreamProgressBar( SvStream& rStrm, SfxObjectShell* pDocShell, sal_uInt16 nResId ) :
213 mrStrm( rStrm )
215 Init( pDocShell, ScGlobal::GetRscString( nResId ) );
218 void ScfStreamProgressBar::Progress()
220 mxProgress->ProgressAbs( mrStrm.Tell() );
223 void ScfStreamProgressBar::Init( SfxObjectShell* pDocShell, const OUString& rText )
225 sal_Size nPos = mrStrm.Tell();
226 mrStrm.Seek( STREAM_SEEK_TO_END );
227 sal_Size nSize = mrStrm.Tell();
228 mrStrm.Seek( nPos );
230 mxProgress.reset( new ScfSimpleProgressBar( nSize, pDocShell, rText ) );
231 Progress();
234 /* vim:set shiftwidth=4 softtabstop=4 expandtab: */