Line data Source code
1 : /* -*- Mode: C++; tab-width: 4; indent-tabs-mode: nil; c-basic-offset: 4 -*- */
2 : /*
3 : * This file is part of the LibreOffice project.
4 : *
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/.
8 : *
9 : * This file incorporates work covered by the following license notice:
10 : *
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 .
18 : */
19 :
20 : #include "progressbar.hxx"
21 :
22 : #include <com/sun/star/awt/GradientStyle.hpp>
23 : #include <com/sun/star/awt/RasterOperation.hpp>
24 : #include <com/sun/star/awt/Gradient.hpp>
25 : #include <com/sun/star/awt/XGraphics.hpp>
26 : #include <tools/debug.hxx>
27 : #include <cppuhelper/typeprovider.hxx>
28 :
29 : #include <math.h>
30 : #include <limits.h>
31 :
32 : using namespace ::cppu;
33 : using namespace ::osl;
34 : using namespace ::rtl;
35 : using namespace ::com::sun::star::uno;
36 : using namespace ::com::sun::star::lang;
37 : using namespace ::com::sun::star::awt;
38 :
39 : namespace unocontrols{
40 :
41 : // construct/destruct
42 :
43 0 : ProgressBar::ProgressBar( const Reference< XComponentContext >& rxContext )
44 : : BaseControl ( rxContext )
45 : , m_bHorizontal ( PROGRESSBAR_DEFAULT_HORIZONTAL )
46 : , m_aBlockSize ( PROGRESSBAR_DEFAULT_BLOCKDIMENSION )
47 : , m_nForegroundColor ( PROGRESSBAR_DEFAULT_FOREGROUNDCOLOR )
48 : , m_nBackgroundColor ( PROGRESSBAR_DEFAULT_BACKGROUNDCOLOR )
49 : , m_nMinRange ( PROGRESSBAR_DEFAULT_MINRANGE )
50 : , m_nMaxRange ( PROGRESSBAR_DEFAULT_MAXRANGE )
51 : , m_nBlockValue ( PROGRESSBAR_DEFAULT_BLOCKVALUE )
52 0 : , m_nValue ( PROGRESSBAR_DEFAULT_VALUE )
53 : {
54 0 : }
55 :
56 0 : ProgressBar::~ProgressBar()
57 : {
58 0 : }
59 :
60 : // XInterface
61 :
62 0 : Any SAL_CALL ProgressBar::queryInterface( const Type& rType ) throw( RuntimeException, std::exception )
63 : {
64 : // Attention:
65 : // Don't use mutex or guard in this method!!! Is a method of XInterface.
66 0 : Any aReturn;
67 0 : Reference< XInterface > xDel = BaseControl::impl_getDelegator();
68 0 : if ( xDel.is() )
69 : {
70 : // If an delegator exist, forward question to his queryInterface.
71 : // Delegator will ask his own queryAggregation!
72 0 : aReturn = xDel->queryInterface( rType );
73 : }
74 : else
75 : {
76 : // If an delegator unknown, forward question to own queryAggregation.
77 0 : aReturn = queryAggregation( rType );
78 : }
79 :
80 0 : return aReturn;
81 : }
82 :
83 : // XInterface
84 :
85 0 : void SAL_CALL ProgressBar::acquire() throw()
86 : {
87 : // Attention:
88 : // Don't use mutex or guard in this method!!! Is a method of XInterface.
89 :
90 : // Forward to baseclass
91 0 : BaseControl::acquire();
92 0 : }
93 :
94 : // XInterface
95 :
96 0 : void SAL_CALL ProgressBar::release() throw()
97 : {
98 : // Attention:
99 : // Don't use mutex or guard in this method!!! Is a method of XInterface.
100 :
101 : // Forward to baseclass
102 0 : BaseControl::release();
103 0 : }
104 :
105 : // XTypeProvider
106 :
107 0 : Sequence< Type > SAL_CALL ProgressBar::getTypes() throw( RuntimeException, std::exception )
108 : {
109 : // Optimize this method !
110 : // We initialize a static variable only one time. And we don't must use a mutex at every call!
111 : // For the first call; pTypeCollection is NULL - for the second call pTypeCollection is different from NULL!
112 : static OTypeCollection* pTypeCollection = NULL;
113 :
114 0 : if ( pTypeCollection == NULL )
115 : {
116 : // Ready for multithreading; get global mutex for first call of this method only! see before
117 0 : MutexGuard aGuard( Mutex::getGlobalMutex() );
118 :
119 : // Control these pointer again ... it can be, that another instance will be faster then these!
120 0 : if ( pTypeCollection == NULL )
121 : {
122 : // Create a static typecollection ...
123 0 : static OTypeCollection aTypeCollection ( ::getCppuType(( const Reference< XControlModel >*) NULL ) ,
124 0 : ::getCppuType(( const Reference< XProgressBar >*) NULL ) ,
125 : BaseControl::getTypes()
126 0 : );
127 : // ... and set his address to static pointer!
128 0 : pTypeCollection = &aTypeCollection;
129 0 : }
130 : }
131 :
132 0 : return pTypeCollection->getTypes();
133 : }
134 :
135 : // XAggregation
136 :
137 0 : Any SAL_CALL ProgressBar::queryAggregation( const Type& aType ) throw( RuntimeException, std::exception )
138 : {
139 : // Ask for my own supported interfaces ...
140 : // Attention: XTypeProvider and XInterface are supported by OComponentHelper!
141 : Any aReturn ( ::cppu::queryInterface( aType ,
142 : static_cast< XControlModel* > ( this ) ,
143 : static_cast< XProgressBar* > ( this )
144 : )
145 0 : );
146 :
147 : // If searched interface not supported by this class ...
148 0 : if ( !aReturn.hasValue() )
149 : {
150 : // ... ask baseclasses.
151 0 : aReturn = BaseControl::queryAggregation( aType );
152 : }
153 :
154 0 : return aReturn;
155 : }
156 :
157 : // XProgressBar
158 :
159 0 : void SAL_CALL ProgressBar::setForegroundColor( sal_Int32 nColor ) throw( RuntimeException, std::exception )
160 : {
161 : // Ready for multithreading
162 0 : MutexGuard aGuard (m_aMutex);
163 :
164 : // Safe color for later use.
165 0 : m_nForegroundColor = nColor;
166 :
167 : // Repaint control
168 0 : impl_paint ( 0, 0, impl_getGraphicsPeer() );
169 0 : }
170 :
171 : // XProgressBar
172 :
173 0 : void SAL_CALL ProgressBar::setBackgroundColor ( sal_Int32 nColor ) throw( RuntimeException, std::exception )
174 : {
175 : // Ready for multithreading
176 0 : MutexGuard aGuard (m_aMutex);
177 :
178 : // Safe color for later use.
179 0 : m_nBackgroundColor = nColor;
180 :
181 : // Repaint control
182 0 : impl_paint ( 0, 0, impl_getGraphicsPeer() );
183 0 : }
184 :
185 : // XProgressBar
186 :
187 0 : void SAL_CALL ProgressBar::setValue ( sal_Int32 nValue ) throw( RuntimeException, std::exception )
188 : {
189 : // This method is defined for follow things:
190 : // 1) Values >= _nMinRange
191 : // 2) Values <= _nMaxRange
192 :
193 : // Ready for multithreading
194 0 : MutexGuard aGuard (m_aMutex);
195 :
196 : // save impossible cases
197 : // This method is only defined for valid values
198 : DBG_ASSERT ( (( nValue >= m_nMinRange ) && ( nValue <= m_nMaxRange )), "ProgressBar::setValue()\nNot valid value.\n" );
199 :
200 : // If new value not valid ... do nothing in release version!
201 0 : if (
202 0 : ( nValue >= m_nMinRange ) &&
203 0 : ( nValue <= m_nMaxRange )
204 : )
205 : {
206 : // New value is ok => save this
207 0 : m_nValue = nValue;
208 :
209 : // Repaint to display changes
210 0 : impl_paint ( 0, 0, impl_getGraphicsPeer() );
211 0 : }
212 0 : }
213 :
214 : // XProgressBar
215 :
216 0 : void SAL_CALL ProgressBar::setRange ( sal_Int32 nMin, sal_Int32 nMax ) throw( RuntimeException, std::exception )
217 : {
218 : // This method is defined for follow things:
219 : // 1) All values of sal_Int32
220 : // 2) Min < Max
221 : // 3) Min > Max
222 :
223 : // save impossible cases
224 : // This method is only defined for valid values
225 : // If you ignore this, the release version wil produce an error "division by zero" in "ProgressBar::setValue()"!
226 : DBG_ASSERT ( ( nMin != nMax ) , "ProgressBar::setRange()\nValues for MIN and MAX are the same. This is not allowed!\n" );
227 :
228 : // Ready for multithreading
229 0 : MutexGuard aGuard (m_aMutex);
230 :
231 : // control the values for min and max
232 0 : if ( nMin < nMax )
233 : {
234 : // Take correct Min and Max
235 0 : m_nMinRange = nMin;
236 0 : m_nMaxRange = nMax;
237 : }
238 : else
239 : {
240 : // Change Min and Max automaticly
241 0 : m_nMinRange = nMax;
242 0 : m_nMaxRange = nMin;
243 : }
244 :
245 : // assure that m_nValue is within the range
246 0 : if (!(m_nMinRange < m_nValue && m_nValue < m_nMaxRange))
247 0 : m_nValue = m_nMinRange;
248 :
249 0 : impl_recalcRange ();
250 :
251 : // Do not repaint the control at this place!!!
252 : // An old "m_nValue" is set and can not be correct for this new range.
253 : // Next call of "ProgressBar::setValue()" do this.
254 0 : }
255 :
256 : // XProgressBar
257 :
258 0 : sal_Int32 SAL_CALL ProgressBar::getValue () throw( RuntimeException, std::exception )
259 : {
260 : // Ready for multithreading
261 0 : MutexGuard aGuard (m_aMutex);
262 :
263 0 : return ( m_nValue );
264 : }
265 :
266 : // XWindow
267 :
268 0 : void SAL_CALL ProgressBar::setPosSize (
269 : sal_Int32 nX,
270 : sal_Int32 nY,
271 : sal_Int32 nWidth,
272 : sal_Int32 nHeight,
273 : sal_Int16 nFlags
274 : ) throw( RuntimeException, std::exception )
275 : {
276 : // Take old size BEFORE you set the new values at baseclass!
277 : // You will control changes. At the other way, the values are the same!
278 0 : Rectangle aBasePosSize = getPosSize ();
279 0 : BaseControl::setPosSize (nX, nY, nWidth, nHeight, nFlags);
280 :
281 : // Do only, if size has changed.
282 0 : if (
283 0 : ( nWidth != aBasePosSize.Width ) ||
284 0 : ( nHeight != aBasePosSize.Height )
285 : )
286 : {
287 0 : impl_recalcRange ( );
288 0 : impl_paint ( 0, 0, impl_getGraphicsPeer () );
289 : }
290 0 : }
291 :
292 : // XControl
293 :
294 0 : sal_Bool SAL_CALL ProgressBar::setModel( const Reference< XControlModel >& /*xModel*/ ) throw( RuntimeException, std::exception )
295 : {
296 : // A model is not possible for this control.
297 0 : return sal_False;
298 : }
299 :
300 : // XControl
301 :
302 0 : Reference< XControlModel > SAL_CALL ProgressBar::getModel() throw( RuntimeException, std::exception )
303 : {
304 : // A model is not possible for this control.
305 0 : return Reference< XControlModel >();
306 : }
307 :
308 : // impl but public method to register service
309 :
310 0 : const Sequence< OUString > ProgressBar::impl_getStaticSupportedServiceNames()
311 : {
312 0 : return css::uno::Sequence<OUString>();
313 : }
314 :
315 : // impl but public method to register service
316 :
317 0 : const OUString ProgressBar::impl_getStaticImplementationName()
318 : {
319 0 : return OUString("stardiv.UnoControls.ProgressBar");
320 : }
321 :
322 : // protected method
323 :
324 0 : void ProgressBar::impl_paint ( sal_Int32 nX, sal_Int32 nY, const Reference< XGraphics > & rGraphics )
325 : {
326 : // save impossible cases
327 : DBG_ASSERT ( rGraphics.is(), "ProgressBar::paint()\nCalled with invalid Reference< XGraphics > ." );
328 :
329 : // This paint method ist not buffered !!
330 : // Every request paint the completely control. ( but only, if peer exist )
331 0 : if ( rGraphics.is () )
332 : {
333 0 : MutexGuard aGuard (m_aMutex);
334 :
335 : // Clear background
336 : // (same color for line and fill)
337 0 : rGraphics->setFillColor ( m_nBackgroundColor );
338 0 : rGraphics->setLineColor ( m_nBackgroundColor );
339 0 : rGraphics->drawRect ( nX, nY, impl_getWidth(), impl_getHeight() );
340 :
341 : // same color for line and fill for blocks
342 0 : rGraphics->setFillColor ( m_nForegroundColor );
343 0 : rGraphics->setLineColor ( m_nForegroundColor );
344 :
345 0 : sal_Int32 nBlockStart = 0; // = left site of new block
346 0 : sal_Int32 nBlockCount = m_nBlockValue!=0.00 ? (sal_Int32)((m_nValue-m_nMinRange)/m_nBlockValue) : 0; // = number of next block
347 :
348 : // Draw horizontal progressbar
349 : // decision in "recalcRange()"
350 0 : if (m_bHorizontal)
351 : {
352 : // Step to left side of window
353 0 : nBlockStart = nX;
354 :
355 0 : for ( sal_Int16 i=1; i<=nBlockCount; ++i )
356 : {
357 : // step free field
358 0 : nBlockStart += PROGRESSBAR_FREESPACE;
359 : // paint block
360 0 : rGraphics->drawRect (nBlockStart, nY+PROGRESSBAR_FREESPACE, m_aBlockSize.Width, m_aBlockSize.Height);
361 : // step next free field
362 0 : nBlockStart += m_aBlockSize.Width;
363 : }
364 : }
365 : // draw vertikal progressbar
366 : // decision in "recalcRange()"
367 : else
368 : {
369 : // step to bottom side of window
370 0 : nBlockStart = nY+impl_getHeight();
371 0 : nBlockStart -= m_aBlockSize.Height;
372 :
373 0 : for ( sal_Int16 i=1; i<=nBlockCount; ++i )
374 : {
375 : // step free field
376 0 : nBlockStart -= PROGRESSBAR_FREESPACE;
377 : // paint block
378 0 : rGraphics->drawRect (nX+PROGRESSBAR_FREESPACE, nBlockStart, m_aBlockSize.Width, m_aBlockSize.Height);
379 : // step next free field
380 0 : nBlockStart -= m_aBlockSize.Height;
381 : }
382 : }
383 :
384 : // Paint shadow border around the progressbar
385 0 : rGraphics->setLineColor ( PROGRESSBAR_LINECOLOR_SHADOW );
386 0 : rGraphics->drawLine ( nX, nY, impl_getWidth(), nY );
387 0 : rGraphics->drawLine ( nX, nY, nX , impl_getHeight() );
388 :
389 0 : rGraphics->setLineColor ( PROGRESSBAR_LINECOLOR_BRIGHT );
390 0 : rGraphics->drawLine ( impl_getWidth()-1, impl_getHeight()-1, impl_getWidth()-1, nY );
391 0 : rGraphics->drawLine ( impl_getWidth()-1, impl_getHeight()-1, nX , impl_getHeight()-1 );
392 : }
393 0 : }
394 :
395 : // protected method
396 :
397 0 : void ProgressBar::impl_recalcRange ()
398 : {
399 0 : MutexGuard aGuard (m_aMutex);
400 :
401 0 : sal_Int32 nWindowWidth = impl_getWidth();
402 0 : sal_Int32 nWindowHeight = impl_getHeight();
403 : double fBlockHeight;
404 : double fBlockWidth;
405 : double fMaxBlocks;
406 :
407 0 : if( nWindowWidth > nWindowHeight )
408 : {
409 0 : m_bHorizontal = true;
410 0 : fBlockHeight = (nWindowHeight-(2*PROGRESSBAR_FREESPACE));
411 0 : fBlockWidth = fBlockHeight;
412 0 : fMaxBlocks = nWindowWidth/(fBlockWidth+PROGRESSBAR_FREESPACE);
413 : }
414 : else
415 : {
416 0 : m_bHorizontal = false;
417 0 : fBlockWidth = (nWindowWidth-(2*PROGRESSBAR_FREESPACE));
418 0 : fBlockHeight = fBlockWidth;
419 0 : fMaxBlocks = nWindowHeight/(fBlockHeight+PROGRESSBAR_FREESPACE);
420 : }
421 :
422 0 : double fRange = m_nMaxRange-m_nMinRange;
423 0 : double fBlockValue = fRange/fMaxBlocks;
424 :
425 0 : m_nBlockValue = fBlockValue;
426 0 : m_aBlockSize.Height = (sal_Int32)fBlockHeight;
427 0 : m_aBlockSize.Width = (sal_Int32)fBlockWidth;
428 0 : }
429 :
430 : } // namespace unocontrols
431 :
432 : /* vim:set shiftwidth=4 softtabstop=4 expandtab: */
|