merge the formfield patch from ooo-build
[ooovba.git] / oox / source / helper / progressbar.cxx
blobf5099f2ccc9b98dd762734911404b5dadce37788
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: progressbar.cxx,v $
10 * $Revision: 1.3 $
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 #include "oox/helper/progressbar.hxx"
32 #include <com/sun/star/task/XStatusIndicator.hpp>
33 #include "oox/helper/helper.hxx"
35 using ::rtl::OUString;
36 using ::com::sun::star::uno::Reference;
37 using ::com::sun::star::task::XStatusIndicator;
39 namespace oox {
41 // ============================================================================
43 namespace {
45 const sal_Int32 PROGRESS_RANGE = 1000000;
47 } // namespace
49 // ============================================================================
51 IProgressBar::~IProgressBar()
55 // ----------------------------------------------------------------------------
57 ISegmentProgressBar::~ISegmentProgressBar()
61 // ============================================================================
62 // ============================================================================
64 ProgressBar::ProgressBar( const Reference< XStatusIndicator >& rxIndicator, const OUString& rText ) :
65 mxIndicator( rxIndicator ),
66 mfPosition( 0 )
68 if( mxIndicator.is() )
69 mxIndicator->start( rText, PROGRESS_RANGE );
72 ProgressBar::~ProgressBar()
74 if( mxIndicator.is() )
75 mxIndicator->end();
78 double ProgressBar::getPosition() const
80 return mfPosition;
83 void ProgressBar::setPosition( double fPosition )
85 OSL_ENSURE( (mfPosition <= fPosition) && (fPosition <= 1.0), "ProgressBar::setPosition - invalid position" );
86 mfPosition = getLimitedValue< double >( fPosition, mfPosition, 1.0 );
87 if( mxIndicator.is() )
88 mxIndicator->setValue( static_cast< sal_Int32 >( mfPosition * PROGRESS_RANGE ) );
91 // ============================================================================
93 namespace prv {
95 class SubSegment : public ISegmentProgressBar
97 public:
98 explicit SubSegment( IProgressBar& rParentProgress, double fStartPos, double fLength );
100 virtual double getPosition() const;
101 virtual void setPosition( double fPosition );
103 virtual double getFreeLength() const;
104 virtual ISegmentProgressBarRef createSegment( double fLength );
106 private:
107 IProgressBar& mrParentProgress;
108 double mfStartPos;
109 double mfLength;
110 double mfPosition;
111 double mfFreeStart;
114 // ----------------------------------------------------------------------------
116 SubSegment::SubSegment( IProgressBar& rParentProgress, double fStartPos, double fLength ) :
117 mrParentProgress( rParentProgress ),
118 mfStartPos( fStartPos ),
119 mfLength( fLength ),
120 mfPosition( 0.0 ),
121 mfFreeStart( 0.0 )
125 double SubSegment::getPosition() const
127 return mfPosition;
130 void SubSegment::setPosition( double fPosition )
132 OSL_ENSURE( (mfPosition <= fPosition) && (fPosition <= 1.0), "SubSegment::setPosition - invalid position" );
133 mfPosition = getLimitedValue< double >( fPosition, mfPosition, 1.0 );
134 mrParentProgress.setPosition( mfStartPos + mfPosition * mfLength );
137 double SubSegment::getFreeLength() const
139 return 1.0 - mfFreeStart;
142 ISegmentProgressBarRef SubSegment::createSegment( double fLength )
144 OSL_ENSURE( (0.0 < fLength) && (fLength <= getFreeLength()), "SubSegment::createSegment - invalid length" );
145 fLength = getLimitedValue< double >( fLength, 0.0, getFreeLength() );
146 ISegmentProgressBarRef xSegment( new prv::SubSegment( *this, mfFreeStart, fLength ) );
147 mfFreeStart += fLength;
148 return xSegment;
151 } // namespace prv
153 // ============================================================================
155 SegmentProgressBar::SegmentProgressBar( const Reference< XStatusIndicator >& rxIndicator, const OUString& rText ) :
156 maProgress( rxIndicator, rText ),
157 mfFreeStart( 0.0 )
161 double SegmentProgressBar::getPosition() const
163 return maProgress.getPosition();
166 void SegmentProgressBar::setPosition( double fPosition )
168 maProgress.setPosition( fPosition );
171 double SegmentProgressBar::getFreeLength() const
173 return 1.0 - mfFreeStart;
176 ISegmentProgressBarRef SegmentProgressBar::createSegment( double fLength )
178 OSL_ENSURE( (0.0 < fLength) && (fLength <= getFreeLength()), "SegmentProgressBar::createSegment - invalid length" );
179 fLength = getLimitedValue< double >( fLength, 0.0, getFreeLength() );
180 ISegmentProgressBarRef xSegment( new prv::SubSegment( maProgress, mfFreeStart, fLength ) );
181 mfFreeStart += fLength;
182 return xSegment;
185 // ============================================================================
187 } // namespace oox