Bump version to 6.0-36
[LibreOffice.git] / UnoControls / source / controls / progressbar.cxx
blobd94409e5e337dbb14d5f42cfce1009ffa74fd9a2
1 /* -*- Mode: C++; tab-width: 4; indent-tabs-mode: nil; c-basic-offset: 4 -*- */
2 /*
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/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/queryinterface.hxx>
28 #include <cppuhelper/typeprovider.hxx>
30 #include <math.h>
31 #include <limits.h>
33 using namespace ::cppu;
34 using namespace ::osl;
35 using namespace ::com::sun::star::uno;
36 using namespace ::com::sun::star::lang;
37 using namespace ::com::sun::star::awt;
39 namespace unocontrols{
41 // construct/destruct
43 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 , m_nValue ( PROGRESSBAR_DEFAULT_VALUE )
56 ProgressBar::~ProgressBar()
60 // XInterface
62 Any SAL_CALL ProgressBar::queryInterface( const Type& rType )
64 // Attention:
65 // Don't use mutex or guard in this method!!! Is a method of XInterface.
66 Any aReturn;
67 Reference< XInterface > xDel = BaseControl::impl_getDelegator();
68 if ( xDel.is() )
70 // If an delegator exist, forward question to his queryInterface.
71 // Delegator will ask his own queryAggregation!
72 aReturn = xDel->queryInterface( rType );
74 else
76 // If an delegator unknown, forward question to own queryAggregation.
77 aReturn = queryAggregation( rType );
80 return aReturn;
83 // XInterface
85 void SAL_CALL ProgressBar::acquire() throw()
87 // Attention:
88 // Don't use mutex or guard in this method!!! Is a method of XInterface.
90 // Forward to baseclass
91 BaseControl::acquire();
94 // XInterface
96 void SAL_CALL ProgressBar::release() throw()
98 // Attention:
99 // Don't use mutex or guard in this method!!! Is a method of XInterface.
101 // Forward to baseclass
102 BaseControl::release();
105 // XTypeProvider
107 Sequence< Type > SAL_CALL ProgressBar::getTypes()
109 static OTypeCollection ourTypeCollection(
110 cppu::UnoType<XControlModel>::get(),
111 cppu::UnoType<XProgressBar>::get(),
112 BaseControl::getTypes() );
114 return ourTypeCollection.getTypes();
117 // XAggregation
119 Any SAL_CALL ProgressBar::queryAggregation( const Type& aType )
121 // Ask for my own supported interfaces ...
122 // Attention: XTypeProvider and XInterface are supported by OComponentHelper!
123 Any aReturn ( ::cppu::queryInterface( aType ,
124 static_cast< XControlModel* > ( this ) ,
125 static_cast< XProgressBar* > ( this )
129 // If searched interface not supported by this class ...
130 if ( !aReturn.hasValue() )
132 // ... ask baseclasses.
133 aReturn = BaseControl::queryAggregation( aType );
136 return aReturn;
139 // XProgressBar
141 void SAL_CALL ProgressBar::setForegroundColor( sal_Int32 nColor )
143 // Ready for multithreading
144 MutexGuard aGuard (m_aMutex);
146 // Safe color for later use.
147 m_nForegroundColor = nColor;
149 // Repaint control
150 impl_paint ( 0, 0, impl_getGraphicsPeer() );
153 // XProgressBar
155 void SAL_CALL ProgressBar::setBackgroundColor ( sal_Int32 nColor )
157 // Ready for multithreading
158 MutexGuard aGuard (m_aMutex);
160 // Safe color for later use.
161 m_nBackgroundColor = nColor;
163 // Repaint control
164 impl_paint ( 0, 0, impl_getGraphicsPeer() );
167 // XProgressBar
169 void SAL_CALL ProgressBar::setValue ( sal_Int32 nValue )
171 // This method is defined for follow things:
172 // 1) Values >= _nMinRange
173 // 2) Values <= _nMaxRange
175 // Ready for multithreading
176 MutexGuard aGuard (m_aMutex);
178 // save impossible cases
179 // This method is only defined for valid values
180 DBG_ASSERT ( (( nValue >= m_nMinRange ) && ( nValue <= m_nMaxRange )), "ProgressBar::setValue()\nNot valid value.\n" );
182 // If new value not valid ... do nothing in release version!
183 if (
184 ( nValue >= m_nMinRange ) &&
185 ( nValue <= m_nMaxRange )
188 // New value is ok => save this
189 m_nValue = nValue;
191 // Repaint to display changes
192 impl_paint ( 0, 0, impl_getGraphicsPeer() );
196 // XProgressBar
198 void SAL_CALL ProgressBar::setRange ( sal_Int32 nMin, sal_Int32 nMax )
200 // This method is defined for follow things:
201 // 1) All values of sal_Int32
202 // 2) Min < Max
203 // 3) Min > Max
205 // save impossible cases
206 // This method is only defined for valid values
207 // If you ignore this, the release version wil produce an error "division by zero" in "ProgressBar::setValue()"!
208 DBG_ASSERT ( ( nMin != nMax ) , "ProgressBar::setRange()\nValues for MIN and MAX are the same. This is not allowed!\n" );
210 // Ready for multithreading
211 MutexGuard aGuard (m_aMutex);
213 // control the values for min and max
214 if ( nMin < nMax )
216 // Take correct Min and Max
217 m_nMinRange = nMin;
218 m_nMaxRange = nMax;
220 else
222 // Change Min and Max automatically
223 m_nMinRange = nMax;
224 m_nMaxRange = nMin;
227 // assure that m_nValue is within the range
228 if (!(m_nMinRange < m_nValue && m_nValue < m_nMaxRange))
229 m_nValue = m_nMinRange;
231 impl_recalcRange ();
233 // Do not repaint the control at this place!!!
234 // An old "m_nValue" is set and can not be correct for this new range.
235 // Next call of "ProgressBar::setValue()" do this.
238 // XProgressBar
240 sal_Int32 SAL_CALL ProgressBar::getValue ()
242 // Ready for multithreading
243 MutexGuard aGuard (m_aMutex);
245 return m_nValue;
248 // XWindow
250 void SAL_CALL ProgressBar::setPosSize (
251 sal_Int32 nX,
252 sal_Int32 nY,
253 sal_Int32 nWidth,
254 sal_Int32 nHeight,
255 sal_Int16 nFlags
258 // Take old size BEFORE you set the new values at baseclass!
259 // You will control changes. At the other way, the values are the same!
260 Rectangle aBasePosSize = getPosSize ();
261 BaseControl::setPosSize (nX, nY, nWidth, nHeight, nFlags);
263 // Do only, if size has changed.
264 if (
265 ( nWidth != aBasePosSize.Width ) ||
266 ( nHeight != aBasePosSize.Height )
269 impl_recalcRange ( );
270 impl_paint ( 0, 0, impl_getGraphicsPeer () );
274 // XControl
276 sal_Bool SAL_CALL ProgressBar::setModel( const Reference< XControlModel >& /*xModel*/ )
278 // A model is not possible for this control.
279 return false;
282 // XControl
284 Reference< XControlModel > SAL_CALL ProgressBar::getModel()
286 // A model is not possible for this control.
287 return Reference< XControlModel >();
290 // impl but public method to register service
292 const Sequence< OUString > ProgressBar::impl_getStaticSupportedServiceNames()
294 return css::uno::Sequence<OUString>();
297 // impl but public method to register service
299 const OUString ProgressBar::impl_getStaticImplementationName()
301 return OUString("stardiv.UnoControls.ProgressBar");
304 // protected method
306 void ProgressBar::impl_paint ( sal_Int32 nX, sal_Int32 nY, const Reference< XGraphics > & rGraphics )
308 // save impossible cases
309 DBG_ASSERT ( rGraphics.is(), "ProgressBar::paint()\nCalled with invalid Reference< XGraphics > ." );
311 // This paint method is not buffered !!
312 // Every request paint the completely control. ( but only, if peer exist )
313 if ( rGraphics.is () )
315 MutexGuard aGuard (m_aMutex);
317 // Clear background
318 // (same color for line and fill)
319 rGraphics->setFillColor ( m_nBackgroundColor );
320 rGraphics->setLineColor ( m_nBackgroundColor );
321 rGraphics->drawRect ( nX, nY, impl_getWidth(), impl_getHeight() );
323 // same color for line and fill for blocks
324 rGraphics->setFillColor ( m_nForegroundColor );
325 rGraphics->setLineColor ( m_nForegroundColor );
327 sal_Int32 nBlockStart = 0; // = left site of new block
328 sal_Int32 nBlockCount = m_nBlockValue!=0.00 ? (sal_Int32)((m_nValue-m_nMinRange)/m_nBlockValue) : 0; // = number of next block
330 // Draw horizontal progressbar
331 // decision in "recalcRange()"
332 if (m_bHorizontal)
334 // Step to left side of window
335 nBlockStart = nX;
337 for ( sal_Int32 i=1; i<=nBlockCount; ++i )
339 // step free field
340 nBlockStart += PROGRESSBAR_FREESPACE;
341 // paint block
342 rGraphics->drawRect (nBlockStart, nY+PROGRESSBAR_FREESPACE, m_aBlockSize.Width, m_aBlockSize.Height);
343 // step next free field
344 nBlockStart += m_aBlockSize.Width;
347 // draw vertical progressbar
348 // decision in "recalcRange()"
349 else
351 // step to bottom side of window
352 nBlockStart = nY+impl_getHeight();
353 nBlockStart -= m_aBlockSize.Height;
355 for ( sal_Int32 i=1; i<=nBlockCount; ++i )
357 // step free field
358 nBlockStart -= PROGRESSBAR_FREESPACE;
359 // paint block
360 rGraphics->drawRect (nX+PROGRESSBAR_FREESPACE, nBlockStart, m_aBlockSize.Width, m_aBlockSize.Height);
361 // step next free field
362 nBlockStart -= m_aBlockSize.Height;
366 // Paint shadow border around the progressbar
367 rGraphics->setLineColor ( PROGRESSBAR_LINECOLOR_SHADOW );
368 rGraphics->drawLine ( nX, nY, impl_getWidth(), nY );
369 rGraphics->drawLine ( nX, nY, nX , impl_getHeight() );
371 rGraphics->setLineColor ( PROGRESSBAR_LINECOLOR_BRIGHT );
372 rGraphics->drawLine ( impl_getWidth()-1, impl_getHeight()-1, impl_getWidth()-1, nY );
373 rGraphics->drawLine ( impl_getWidth()-1, impl_getHeight()-1, nX , impl_getHeight()-1 );
377 // protected method
379 void ProgressBar::impl_recalcRange ()
381 MutexGuard aGuard (m_aMutex);
383 sal_Int32 nWindowWidth = impl_getWidth();
384 sal_Int32 nWindowHeight = impl_getHeight();
385 double fBlockHeight;
386 double fBlockWidth;
387 double fMaxBlocks;
389 if( nWindowWidth > nWindowHeight )
391 m_bHorizontal = true;
392 fBlockHeight = (nWindowHeight-(2*PROGRESSBAR_FREESPACE));
393 fBlockWidth = fBlockHeight;
394 fMaxBlocks = nWindowWidth/(fBlockWidth+PROGRESSBAR_FREESPACE);
396 else
398 m_bHorizontal = false;
399 fBlockWidth = (nWindowWidth-(2*PROGRESSBAR_FREESPACE));
400 fBlockHeight = fBlockWidth;
401 fMaxBlocks = nWindowHeight/(fBlockHeight+PROGRESSBAR_FREESPACE);
404 double fRange = m_nMaxRange-m_nMinRange;
405 double fBlockValue = fRange/fMaxBlocks;
407 m_nBlockValue = fBlockValue;
408 m_aBlockSize.Height = (sal_Int32)fBlockHeight;
409 m_aBlockSize.Width = (sal_Int32)fBlockWidth;
412 } // namespace unocontrols
414 /* vim:set shiftwidth=4 softtabstop=4 expandtab: */