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::awt
;
32 namespace unocontrols
{
36 ProgressBar::ProgressBar( const Reference
< XComponentContext
>& rxContext
)
37 : ProgressBar_BASE ( rxContext
)
38 , m_bHorizontal ( PROGRESSBAR_DEFAULT_HORIZONTAL
)
39 , m_aBlockSize ( PROGRESSBAR_DEFAULT_BLOCKDIMENSION
)
40 , m_nForegroundColor ( PROGRESSBAR_DEFAULT_FOREGROUNDCOLOR
)
41 , m_nBackgroundColor ( PROGRESSBAR_DEFAULT_BACKGROUNDCOLOR
)
42 , m_nMinRange ( PROGRESSBAR_DEFAULT_MINRANGE
)
43 , m_nMaxRange ( PROGRESSBAR_DEFAULT_MAXRANGE
)
44 , m_nBlockValue ( PROGRESSBAR_DEFAULT_BLOCKVALUE
)
45 , m_nValue ( PROGRESSBAR_DEFAULT_VALUE
)
49 ProgressBar::~ProgressBar()
55 void SAL_CALL
ProgressBar::setForegroundColor( sal_Int32 nColor
)
57 // Ready for multithreading
58 MutexGuard
aGuard (m_aMutex
);
60 // Safe color for later use.
61 m_nForegroundColor
= Color(ColorTransparency
, nColor
);
64 impl_paint ( 0, 0, impl_getGraphicsPeer() );
69 void SAL_CALL
ProgressBar::setBackgroundColor ( sal_Int32 nColor
)
71 // Ready for multithreading
72 MutexGuard
aGuard (m_aMutex
);
74 // Safe color for later use.
75 m_nBackgroundColor
= Color(ColorTransparency
, nColor
);
78 impl_paint ( 0, 0, impl_getGraphicsPeer() );
83 void SAL_CALL
ProgressBar::setValue ( sal_Int32 nValue
)
85 // This method is defined for follow things:
86 // 1) Values >= _nMinRange
87 // 2) Values <= _nMaxRange
89 // Ready for multithreading
90 MutexGuard
aGuard (m_aMutex
);
92 // save impossible cases
93 // This method is only defined for valid values
94 DBG_ASSERT ( (( nValue
>= m_nMinRange
) && ( nValue
<= m_nMaxRange
)), "ProgressBar::setValue()\nNot valid value.\n" );
96 // If new value not valid ... do nothing in release version!
98 ( nValue
>= m_nMinRange
) &&
99 ( nValue
<= m_nMaxRange
)
102 // New value is ok => save this
105 // Repaint to display changes
106 impl_paint ( 0, 0, impl_getGraphicsPeer() );
112 void SAL_CALL
ProgressBar::setRange ( sal_Int32 nMin
, sal_Int32 nMax
)
114 // This method is defined for follow things:
115 // 1) All values of sal_Int32
119 // save impossible cases
120 // This method is only defined for valid values
121 // If you ignore this, the release version will produce an error "division by zero" in "ProgressBar::setValue()"!
122 DBG_ASSERT ( ( nMin
!= nMax
) , "ProgressBar::setRange()\nValues for MIN and MAX are the same. This is not allowed!\n" );
124 // Ready for multithreading
125 MutexGuard
aGuard (m_aMutex
);
127 // control the values for min and max
130 // Take correct Min and Max
136 // Change Min and Max automatically
141 // assure that m_nValue is within the range
142 if (m_nMinRange
>= m_nValue
|| m_nValue
>= m_nMaxRange
)
143 m_nValue
= m_nMinRange
;
147 // Do not repaint the control at this place!!!
148 // An old "m_nValue" is set and can not be correct for this new range.
149 // Next call of "ProgressBar::setValue()" do this.
154 sal_Int32 SAL_CALL
ProgressBar::getValue ()
156 // Ready for multithreading
157 MutexGuard
aGuard (m_aMutex
);
164 void SAL_CALL
ProgressBar::setPosSize (
172 // Take old size BEFORE you set the new values at baseclass!
173 // You will control changes. At the other way, the values are the same!
174 Rectangle aBasePosSize
= getPosSize ();
175 BaseControl::setPosSize (nX
, nY
, nWidth
, nHeight
, nFlags
);
177 // Do only, if size has changed.
179 ( nWidth
!= aBasePosSize
.Width
) ||
180 ( nHeight
!= aBasePosSize
.Height
)
183 impl_recalcRange ( );
184 impl_paint ( 0, 0, impl_getGraphicsPeer () );
190 sal_Bool SAL_CALL
ProgressBar::setModel( const Reference
< XControlModel
>& /*xModel*/ )
192 // A model is not possible for this control.
198 Reference
< XControlModel
> SAL_CALL
ProgressBar::getModel()
200 // A model is not possible for this control.
201 return Reference
< XControlModel
>();
206 void ProgressBar::impl_paint ( sal_Int32 nX
, sal_Int32 nY
, const Reference
< XGraphics
> & rGraphics
)
208 // save impossible cases
209 DBG_ASSERT ( rGraphics
.is(), "ProgressBar::paint()\nCalled with invalid Reference< XGraphics > ." );
211 // This paint method is not buffered !!
212 // Every request paint the completely control. ( but only, if peer exist )
213 if ( !rGraphics
.is () )
216 MutexGuard
aGuard (m_aMutex
);
219 // (same color for line and fill)
220 rGraphics
->setFillColor ( sal_Int32(m_nBackgroundColor
) );
221 rGraphics
->setLineColor ( sal_Int32(m_nBackgroundColor
) );
222 rGraphics
->drawRect ( nX
, nY
, impl_getWidth(), impl_getHeight() );
224 // same color for line and fill for blocks
225 rGraphics
->setFillColor ( sal_Int32(m_nForegroundColor
) );
226 rGraphics
->setLineColor ( sal_Int32(m_nForegroundColor
) );
228 sal_Int32 nBlockStart
= 0; // = left site of new block
229 sal_Int32 nBlockCount
= m_nBlockValue
!=0.00 ? static_cast<sal_Int32
>((m_nValue
-m_nMinRange
)/m_nBlockValue
) : 0; // = number of next block
231 // Draw horizontal progressbar
232 // decision in "recalcRange()"
235 // Step to left side of window
238 for ( sal_Int32 i
=1; i
<=nBlockCount
; ++i
)
241 nBlockStart
+= PROGRESSBAR_FREESPACE
;
243 rGraphics
->drawRect (nBlockStart
, nY
+PROGRESSBAR_FREESPACE
, m_aBlockSize
.Width
, m_aBlockSize
.Height
);
244 // step next free field
245 nBlockStart
+= m_aBlockSize
.Width
;
248 // draw vertical progressbar
249 // decision in "recalcRange()"
252 // step to bottom side of window
253 nBlockStart
= nY
+impl_getHeight();
254 nBlockStart
-= m_aBlockSize
.Height
;
256 for ( sal_Int32 i
=1; i
<=nBlockCount
; ++i
)
259 nBlockStart
-= PROGRESSBAR_FREESPACE
;
261 rGraphics
->drawRect (nX
+PROGRESSBAR_FREESPACE
, nBlockStart
, m_aBlockSize
.Width
, m_aBlockSize
.Height
);
262 // step next free field
263 nBlockStart
-= m_aBlockSize
.Height
;
267 // Paint shadow border around the progressbar
268 rGraphics
->setLineColor ( PROGRESSBAR_LINECOLOR_SHADOW
);
269 rGraphics
->drawLine ( nX
, nY
, impl_getWidth(), nY
);
270 rGraphics
->drawLine ( nX
, nY
, nX
, impl_getHeight() );
272 rGraphics
->setLineColor ( PROGRESSBAR_LINECOLOR_BRIGHT
);
273 rGraphics
->drawLine ( impl_getWidth()-1, impl_getHeight()-1, impl_getWidth()-1, nY
);
274 rGraphics
->drawLine ( impl_getWidth()-1, impl_getHeight()-1, nX
, impl_getHeight()-1 );
279 void ProgressBar::impl_recalcRange ()
281 MutexGuard
aGuard (m_aMutex
);
283 sal_Int32 nWindowWidth
= impl_getWidth();
284 sal_Int32 nWindowHeight
= impl_getHeight();
289 if( nWindowWidth
> nWindowHeight
)
291 m_bHorizontal
= true;
292 fBlockHeight
= (nWindowHeight
-(2*PROGRESSBAR_FREESPACE
));
293 fBlockWidth
= fBlockHeight
;
294 fMaxBlocks
= nWindowWidth
/(fBlockWidth
+PROGRESSBAR_FREESPACE
);
298 m_bHorizontal
= false;
299 fBlockWidth
= (nWindowWidth
-(2*PROGRESSBAR_FREESPACE
));
300 fBlockHeight
= fBlockWidth
;
301 fMaxBlocks
= nWindowHeight
/(fBlockHeight
+PROGRESSBAR_FREESPACE
);
304 double fRange
= m_nMaxRange
-m_nMinRange
;
305 double fBlockValue
= fRange
/fMaxBlocks
;
307 m_nBlockValue
= fBlockValue
;
308 m_aBlockSize
.Height
= static_cast<sal_Int32
>(fBlockHeight
);
309 m_aBlockSize
.Width
= static_cast<sal_Int32
>(fBlockWidth
);
312 } // namespace unocontrols
314 extern "C" SAL_DLLPUBLIC_EXPORT
css::uno::XInterface
*
315 stardiv_UnoControls_ProgressBar_get_implementation(
316 css::uno::XComponentContext
* context
, css::uno::Sequence
<css::uno::Any
> const&)
318 return cppu::acquire(new unocontrols::ProgressBar(context
));
320 /* vim:set shiftwidth=4 softtabstop=4 expandtab: */