1 /************************************************************************* 2 * 3 * DO NOT ALTER OR REMOVE COPYRIGHT NOTICES OR THIS FILE HEADER. 4 * 5 * Copyright 2000, 2010 Oracle and/or its affiliates. 6 * 7 * OpenOffice.org - a multi-platform office productivity suite 8 * 9 * This file is part of OpenOffice.org. 10 * 11 * OpenOffice.org is free software: you can redistribute it and/or modify 12 * it under the terms of the GNU Lesser General Public License version 3 13 * only, as published by the Free Software Foundation. 14 * 15 * OpenOffice.org is distributed in the hope that it will be useful, 16 * but WITHOUT ANY WARRANTY; without even the implied warranty of 17 * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the 18 * GNU Lesser General Public License version 3 for more details 19 * (a copy is included in the LICENSE file that accompanied this code). 20 * 21 * You should have received a copy of the GNU Lesser General Public License 22 * version 3 along with OpenOffice.org. If not, see 23 * <http://www.openoffice.org/license.html> 24 * for a copy of the LGPLv3 License. 25 * 26 ************************************************************************/ 27 28 #ifndef SC_FPROGRESSBAR_HXX 29 #define SC_FPROGRESSBAR_HXX 30 31 #include "globstr.hrc" 32 #include "ftools.hxx" 33 #include "scdllapi.h" 34 35 class SfxObjectShell; 36 class ScProgress; 37 38 // ============================================================================ 39 40 const sal_Int32 SCF_INV_SEGMENT = -1; 41 42 // ============================================================================ 43 44 /** Progress bar for complex progress representation. 45 46 The progress bar contains one or more segments, each with customable 47 size. Each segment is represented by a unique identifier. While showing the 48 progress bar, several segments can be started simultaneously. The progress 49 bar displays the sum of all started segments on screen. 50 51 It is possible to create a full featured ScfProgressBar object from 52 any segment. This sub progress bar works only on that parent segment, with 53 the effect, that if the sub progress bar reaches 100%, the parent segment is 54 filled completely. 55 56 After adding segments, the progress bar has to be activated. In this step the 57 total size of all segments is calculated. Therefore it is not possible to add 58 more segments from here. 59 60 If a sub progress bar is created from a segment, and the main progress bar 61 has been started (but not the sub progress bar), it is still possible to add 62 segments to the sub progress bar. It is not allowed to get the sub progress bar 63 of a started segment. And it is not allowed to modify the segment containing 64 a sub progress bar directly. 65 66 Following a few code examples, how to use the progress bar. 67 68 Example 1: Simple progress bar (see also ScfSimpleProgressBar below). 69 70 ScfProgressBar aProgress( ... ); 71 sal_Int32 nSeg = aProgress.AddSegment( 50 ); // segment with 50 steps (1 step = 2%) 72 73 aProgress.ActivateSegment( nSeg ); // start segment nSeg 74 aProgress.Progress(); // 0->1; display: 2% 75 aProgress.ProgressAbs( 9 ); // 1->9; display: 18% 76 77 Example 2: Progress bar with 2 segments. 78 79 ScfProgressBar aProgress( ... ); 80 sal_Int32 nSeg1 = aProgress.AddSegment( 70 ); // segment with 70 steps 81 sal_Int32 nSeg2 = aProgress.AddSegment( 30 ); // segment with 30 steps 82 // both segments: 100 steps (1 step = 1%) 83 84 aProgress.ActivateSegment( nSeg1 ); // start first segment 85 aProgress.Progress(); // 0->1, display: 1% 86 aProgress.Progress( 2 ); // 1->3, display: 3% 87 aProgress.ActivateSegment( nSeg2 ); // start second segment 88 aProgress.Progress( 5 ); // 0->5, display: 8% (5+3 steps) 89 aProgress.ActivateSegment( nSeg1 ); // continue with first segment 90 aProgress.Progress(); // 3->4, display: 9% (5+4 steps) 91 92 Example 3: Progress bar with 2 segments, one contains a sub progress bar. 93 94 ScfProgressBar aProgress( ... ); 95 sal_Int32 nSeg1 = aProgress.AddSegment( 75 ); // segment with 75 steps 96 sal_Int32 nSeg2 = aProgress.AddSegment( 25 ); // segment with 25 steps 97 // both segments: 100 steps (1 step = 1%) 98 99 aProgress.ActivateSegment( nSeg1 ); // start first segment 100 aProgress.Progress(); // 0->1, display: 1% 101 102 ScfProgressBar& rSubProgress = aProgress.GetSegmentProgressBar( nSeg2 ); 103 // sub progress bar from second segment 104 sal_Int32 nSubSeg = rSubProgress.AddSegment( 5 ); // 5 steps, mapped to second segment 105 // => 1 step = 5 steps in parent = 5% 106 107 rSubProgress.ActivateSegment( nSubSeg ); // start the segment (auto activate parent segment) 108 rSubProgress.Progress(); // 0->1 (0->5 in parent); display: 6% (1+5) 109 110 // not allowed (second segment active): aProgress.Progress(); 111 // not allowed (first segment not empty): aProgress.GetSegmentProgressBar( nSeg1 ); 112 */ 113 class ScfProgressBar : ScfNoCopy 114 { 115 public: 116 explicit ScfProgressBar( SfxObjectShell* pDocShell, const String& rText ); 117 explicit ScfProgressBar( SfxObjectShell* pDocShell, sal_uInt16 nResId ); 118 virtual ~ScfProgressBar(); 119 120 /** Adds a new segment to the progress bar. 121 @return the identifier of the segment. */ 122 sal_Int32 AddSegment( sal_Size nSize ); 123 /** Returns a complete progress bar for the specified segment. 124 @descr The progress bar can be used to create sub segments inside of the 125 segment. Do not delete it (done by root progress bar)! 126 @return A reference to an ScfProgressBar connected to the segment. */ 127 ScfProgressBar& GetSegmentProgressBar( sal_Int32 nSegment ); 128 129 /** Returns true, if any progress segment has been started. */ 130 inline bool IsStarted() const { return mbInProgress; } 131 /** Returns true, if the current progress segment is already full. */ 132 bool IsFull() const; 133 134 /** Starts the progress bar or activates another segment. */ 135 void ActivateSegment( sal_Int32 nSegment ); 136 /** Starts the progress bar (with first segment). */ 137 inline void Activate() { ActivateSegment( 0 ); } 138 /** Set current segment to the specified absolute position. */ 139 void ProgressAbs( sal_Size nPos ); 140 /** Increase current segment by the passed value. */ 141 void Progress( sal_Size nDelta = 1 ); 142 143 private: 144 struct ScfProgressSegment; 145 146 /** Used to create sub progress bars. */ 147 explicit ScfProgressBar( 148 ScfProgressBar& rParProgress, 149 ScfProgressSegment* pParSegment ); 150 151 /** Initializes all members on construction. */ 152 void Init( SfxObjectShell* pDocShell ); 153 154 /** Returns the segment specified by list index. */ 155 ScfProgressSegment* GetSegment( sal_Int32 nSegment ) const; 156 /** Activates progress bar and sets current segment. */ 157 void SetCurrSegment( ScfProgressSegment* pSegment ); 158 /** Increases mnTotalPos and calls the system progress bar. */ 159 void IncreaseProgressBar( sal_Size nDelta ); 160 161 private: 162 /** Contains all data of a segment of the progress bar. */ 163 struct ScfProgressSegment 164 { 165 typedef ::std::auto_ptr< ScfProgressBar > ScfProgressBarPtr; 166 167 ScfProgressBarPtr mxProgress; /// Pointer to sub progress bar for this segment. 168 sal_Size mnSize; /// Size of this segment. 169 sal_Size mnPos; /// Current position of this segment. 170 171 explicit ScfProgressSegment( sal_Size nSize ); 172 ~ScfProgressSegment(); 173 }; 174 175 typedef ::std::auto_ptr< ScProgress > ScProgressPtr; 176 typedef ScfDelList< ScfProgressSegment > ScfSegmentList; 177 178 ScfSegmentList maSegments; /// List of progress segments. 179 String maText; /// UI string for system progress. 180 181 ScProgressPtr mxSysProgress; /// System progress bar. 182 SfxObjectShell* mpDocShell; /// The document shell for the progress bar. 183 ScfProgressBar* mpParentProgress; /// Parent progress bar, if this is a segment progress bar. 184 ScfProgressSegment* mpParentSegment; /// Parent segment, if this is a segment progress bar. 185 ScfProgressSegment* mpCurrSegment; /// Current segment for progress. 186 187 sal_Size mnTotalSize; /// Total size of all segments. 188 sal_Size mnTotalPos; /// Sum of positions of all segments. 189 sal_Size mnUnitSize; /// Size between two calls of system progress. 190 sal_Size mnNextUnitPos; /// Limit for next system progress call. 191 sal_Size mnSysProgressScale; /// Additionally scaling factor for system progress. 192 bool mbInProgress; /// true = progress bar started. 193 }; 194 195 // ============================================================================ 196 197 /** A simplified progress bar with only one segment. */ 198 class ScfSimpleProgressBar 199 { 200 public: 201 explicit ScfSimpleProgressBar( sal_Size nSize, SfxObjectShell* pDocShell, const String& rText ); 202 explicit ScfSimpleProgressBar( sal_Size nSize, SfxObjectShell* pDocShell, sal_uInt16 nResId ); 203 204 /** Set progress bar to the specified position. */ 205 inline void ProgressAbs( sal_Size nPos ) { maProgress.ProgressAbs( nPos ); } 206 /** Increase progress bar by 1. */ 207 inline void Progress( sal_Size nDelta = 1 ) { maProgress.Progress( nDelta ); } 208 209 private: 210 /** Initializes and starts the progress bar. */ 211 void Init( sal_Size nSize ); 212 213 private: 214 ScfProgressBar maProgress; /// The used progress bar. 215 }; 216 217 // ============================================================================ 218 219 /** A simplified progress bar based on the stream position of an existing stream. */ 220 class ScfStreamProgressBar 221 { 222 public: 223 //UNUSED2008-05 explicit ScfStreamProgressBar( SvStream& rStrm, SfxObjectShell* pDocShell, const String& rText ); 224 explicit ScfStreamProgressBar( SvStream& rStrm, SfxObjectShell* pDocShell, sal_uInt16 nResId = STR_LOAD_DOC ); 225 226 /** Sets the progress bar to the current stream position. */ 227 void Progress(); 228 229 private: 230 /** Initializes and starts the progress bar. */ 231 void Init( SfxObjectShell* pDocShell, const String& rText ); 232 233 private: 234 typedef ::std::auto_ptr< ScfSimpleProgressBar > ScfSimpleProgressBarPtr; 235 236 ScfSimpleProgressBarPtr mxProgress; /// The used progress bar. 237 SvStream& mrStrm; /// The used stream. 238 }; 239 240 // ============================================================================ 241 242 #endif 243 244