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