1 /* -*- Mode: C++; tab-width: 4; indent-tabs-mode: nil; c-basic-offset: 4 -*- */
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 <progressbar.hxx>
22 #include <com/sun/star/awt/XGraphics.hpp>
23 #include <tools/debug.hxx>
24 #include <cppuhelper/queryinterface.hxx>
25 #include <cppuhelper/typeprovider.hxx>
27 using namespace ::cppu
;
28 using namespace ::osl
;
29 using namespace ::com::sun::star::uno
;
30 using namespace ::com::sun::star::lang
;
31 using namespace ::com::sun::star::awt
;
33 namespace unocontrols
{
37 ProgressBar::ProgressBar( const Reference
< XComponentContext
>& rxContext
)
38 : BaseControl ( rxContext
)
39 , m_bHorizontal ( PROGRESSBAR_DEFAULT_HORIZONTAL
)
40 , m_aBlockSize ( PROGRESSBAR_DEFAULT_BLOCKDIMENSION
)
41 , m_nForegroundColor ( PROGRESSBAR_DEFAULT_FOREGROUNDCOLOR
)
42 , m_nBackgroundColor ( PROGRESSBAR_DEFAULT_BACKGROUNDCOLOR
)
43 , m_nMinRange ( PROGRESSBAR_DEFAULT_MINRANGE
)
44 , m_nMaxRange ( PROGRESSBAR_DEFAULT_MAXRANGE
)
45 , m_nBlockValue ( PROGRESSBAR_DEFAULT_BLOCKVALUE
)
46 , m_nValue ( PROGRESSBAR_DEFAULT_VALUE
)
50 ProgressBar::~ProgressBar()
56 Any SAL_CALL
ProgressBar::queryInterface( const Type
& rType
)
58 // Ask for my own supported interfaces ...
59 // Attention: XTypeProvider and XInterface are supported by WeakComponentImplHelper!
60 Any
aReturn ( ::cppu::queryInterface( rType
,
61 static_cast< XControlModel
* > ( this ) ,
62 static_cast< XProgressBar
* > ( this )
66 // If searched interface not supported by this class ...
67 if ( !aReturn
.hasValue() )
69 // ... ask baseclasses.
70 aReturn
= BaseControl::queryInterface( rType
);
78 void SAL_CALL
ProgressBar::acquire() noexcept
81 // Don't use mutex or guard in this method!!! Is a method of XInterface.
83 // Forward to baseclass
84 BaseControl::acquire();
89 void SAL_CALL
ProgressBar::release() noexcept
92 // Don't use mutex or guard in this method!!! Is a method of XInterface.
94 // Forward to baseclass
95 BaseControl::release();
100 Sequence
< Type
> SAL_CALL
ProgressBar::getTypes()
102 static OTypeCollection
ourTypeCollection(
103 cppu::UnoType
<XControlModel
>::get(),
104 cppu::UnoType
<XProgressBar
>::get(),
105 BaseControl::getTypes() );
107 return ourTypeCollection
.getTypes();
112 void SAL_CALL
ProgressBar::setForegroundColor( sal_Int32 nColor
)
114 // Ready for multithreading
115 MutexGuard
aGuard (m_aMutex
);
117 // Safe color for later use.
118 m_nForegroundColor
= Color(ColorTransparency
, nColor
);
121 impl_paint ( 0, 0, impl_getGraphicsPeer() );
126 void SAL_CALL
ProgressBar::setBackgroundColor ( sal_Int32 nColor
)
128 // Ready for multithreading
129 MutexGuard
aGuard (m_aMutex
);
131 // Safe color for later use.
132 m_nBackgroundColor
= Color(ColorTransparency
, nColor
);
135 impl_paint ( 0, 0, impl_getGraphicsPeer() );
140 void SAL_CALL
ProgressBar::setValue ( sal_Int32 nValue
)
142 // This method is defined for follow things:
143 // 1) Values >= _nMinRange
144 // 2) Values <= _nMaxRange
146 // Ready for multithreading
147 MutexGuard
aGuard (m_aMutex
);
149 // save impossible cases
150 // This method is only defined for valid values
151 DBG_ASSERT ( (( nValue
>= m_nMinRange
) && ( nValue
<= m_nMaxRange
)), "ProgressBar::setValue()\nNot valid value.\n" );
153 // If new value not valid ... do nothing in release version!
155 ( nValue
>= m_nMinRange
) &&
156 ( nValue
<= m_nMaxRange
)
159 // New value is ok => save this
162 // Repaint to display changes
163 impl_paint ( 0, 0, impl_getGraphicsPeer() );
169 void SAL_CALL
ProgressBar::setRange ( sal_Int32 nMin
, sal_Int32 nMax
)
171 // This method is defined for follow things:
172 // 1) All values of sal_Int32
176 // save impossible cases
177 // This method is only defined for valid values
178 // If you ignore this, the release version will produce an error "division by zero" in "ProgressBar::setValue()"!
179 DBG_ASSERT ( ( nMin
!= nMax
) , "ProgressBar::setRange()\nValues for MIN and MAX are the same. This is not allowed!\n" );
181 // Ready for multithreading
182 MutexGuard
aGuard (m_aMutex
);
184 // control the values for min and max
187 // Take correct Min and Max
193 // Change Min and Max automatically
198 // assure that m_nValue is within the range
199 if (m_nMinRange
>= m_nValue
|| m_nValue
>= m_nMaxRange
)
200 m_nValue
= m_nMinRange
;
204 // Do not repaint the control at this place!!!
205 // An old "m_nValue" is set and can not be correct for this new range.
206 // Next call of "ProgressBar::setValue()" do this.
211 sal_Int32 SAL_CALL
ProgressBar::getValue ()
213 // Ready for multithreading
214 MutexGuard
aGuard (m_aMutex
);
221 void SAL_CALL
ProgressBar::setPosSize (
229 // Take old size BEFORE you set the new values at baseclass!
230 // You will control changes. At the other way, the values are the same!
231 Rectangle aBasePosSize
= getPosSize ();
232 BaseControl::setPosSize (nX
, nY
, nWidth
, nHeight
, nFlags
);
234 // Do only, if size has changed.
236 ( nWidth
!= aBasePosSize
.Width
) ||
237 ( nHeight
!= aBasePosSize
.Height
)
240 impl_recalcRange ( );
241 impl_paint ( 0, 0, impl_getGraphicsPeer () );
247 sal_Bool SAL_CALL
ProgressBar::setModel( const Reference
< XControlModel
>& /*xModel*/ )
249 // A model is not possible for this control.
255 Reference
< XControlModel
> SAL_CALL
ProgressBar::getModel()
257 // A model is not possible for this control.
258 return Reference
< XControlModel
>();
263 void ProgressBar::impl_paint ( sal_Int32 nX
, sal_Int32 nY
, const Reference
< XGraphics
> & rGraphics
)
265 // save impossible cases
266 DBG_ASSERT ( rGraphics
.is(), "ProgressBar::paint()\nCalled with invalid Reference< XGraphics > ." );
268 // This paint method is not buffered !!
269 // Every request paint the completely control. ( but only, if peer exist )
270 if ( !rGraphics
.is () )
273 MutexGuard
aGuard (m_aMutex
);
276 // (same color for line and fill)
277 rGraphics
->setFillColor ( sal_Int32(m_nBackgroundColor
) );
278 rGraphics
->setLineColor ( sal_Int32(m_nBackgroundColor
) );
279 rGraphics
->drawRect ( nX
, nY
, impl_getWidth(), impl_getHeight() );
281 // same color for line and fill for blocks
282 rGraphics
->setFillColor ( sal_Int32(m_nForegroundColor
) );
283 rGraphics
->setLineColor ( sal_Int32(m_nForegroundColor
) );
285 sal_Int32 nBlockStart
= 0; // = left site of new block
286 sal_Int32 nBlockCount
= m_nBlockValue
!=0.00 ? static_cast<sal_Int32
>((m_nValue
-m_nMinRange
)/m_nBlockValue
) : 0; // = number of next block
288 // Draw horizontal progressbar
289 // decision in "recalcRange()"
292 // Step to left side of window
295 for ( sal_Int32 i
=1; i
<=nBlockCount
; ++i
)
298 nBlockStart
+= PROGRESSBAR_FREESPACE
;
300 rGraphics
->drawRect (nBlockStart
, nY
+PROGRESSBAR_FREESPACE
, m_aBlockSize
.Width
, m_aBlockSize
.Height
);
301 // step next free field
302 nBlockStart
+= m_aBlockSize
.Width
;
305 // draw vertical progressbar
306 // decision in "recalcRange()"
309 // step to bottom side of window
310 nBlockStart
= nY
+impl_getHeight();
311 nBlockStart
-= m_aBlockSize
.Height
;
313 for ( sal_Int32 i
=1; i
<=nBlockCount
; ++i
)
316 nBlockStart
-= PROGRESSBAR_FREESPACE
;
318 rGraphics
->drawRect (nX
+PROGRESSBAR_FREESPACE
, nBlockStart
, m_aBlockSize
.Width
, m_aBlockSize
.Height
);
319 // step next free field
320 nBlockStart
-= m_aBlockSize
.Height
;
324 // Paint shadow border around the progressbar
325 rGraphics
->setLineColor ( PROGRESSBAR_LINECOLOR_SHADOW
);
326 rGraphics
->drawLine ( nX
, nY
, impl_getWidth(), nY
);
327 rGraphics
->drawLine ( nX
, nY
, nX
, impl_getHeight() );
329 rGraphics
->setLineColor ( PROGRESSBAR_LINECOLOR_BRIGHT
);
330 rGraphics
->drawLine ( impl_getWidth()-1, impl_getHeight()-1, impl_getWidth()-1, nY
);
331 rGraphics
->drawLine ( impl_getWidth()-1, impl_getHeight()-1, nX
, impl_getHeight()-1 );
336 void ProgressBar::impl_recalcRange ()
338 MutexGuard
aGuard (m_aMutex
);
340 sal_Int32 nWindowWidth
= impl_getWidth();
341 sal_Int32 nWindowHeight
= impl_getHeight();
346 if( nWindowWidth
> nWindowHeight
)
348 m_bHorizontal
= true;
349 fBlockHeight
= (nWindowHeight
-(2*PROGRESSBAR_FREESPACE
));
350 fBlockWidth
= fBlockHeight
;
351 fMaxBlocks
= nWindowWidth
/(fBlockWidth
+PROGRESSBAR_FREESPACE
);
355 m_bHorizontal
= false;
356 fBlockWidth
= (nWindowWidth
-(2*PROGRESSBAR_FREESPACE
));
357 fBlockHeight
= fBlockWidth
;
358 fMaxBlocks
= nWindowHeight
/(fBlockHeight
+PROGRESSBAR_FREESPACE
);
361 double fRange
= m_nMaxRange
-m_nMinRange
;
362 double fBlockValue
= fRange
/fMaxBlocks
;
364 m_nBlockValue
= fBlockValue
;
365 m_aBlockSize
.Height
= static_cast<sal_Int32
>(fBlockHeight
);
366 m_aBlockSize
.Width
= static_cast<sal_Int32
>(fBlockWidth
);
369 } // namespace unocontrols
371 extern "C" SAL_DLLPUBLIC_EXPORT
css::uno::XInterface
*
372 stardiv_UnoControls_ProgressBar_get_implementation(
373 css::uno::XComponentContext
* context
, css::uno::Sequence
<css::uno::Any
> const&)
375 return cppu::acquire(new unocontrols::ProgressBar(context
));
377 /* vim:set shiftwidth=4 softtabstop=4 expandtab: */