Bump version to 6.4-15
[LibreOffice.git] / UnoControls / source / controls / progressbar.cxx
blob37ac9e379195bed180a076782a7071c920c5cf44
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/XGraphics.hpp>
23 #include <tools/debug.hxx>
24 #include <cppuhelper/queryinterface.hxx>
25 #include <cppuhelper/typeprovider.hxx>
27 #include <limits.h>
29 using namespace ::cppu;
30 using namespace ::osl;
31 using namespace ::com::sun::star::uno;
32 using namespace ::com::sun::star::lang;
33 using namespace ::com::sun::star::awt;
35 namespace unocontrols {
37 // construct/destruct
39 ProgressBar::ProgressBar( const Reference< XComponentContext >& rxContext )
40 : BaseControl ( rxContext )
41 , m_bHorizontal ( PROGRESSBAR_DEFAULT_HORIZONTAL )
42 , m_aBlockSize ( PROGRESSBAR_DEFAULT_BLOCKDIMENSION )
43 , m_nForegroundColor ( PROGRESSBAR_DEFAULT_FOREGROUNDCOLOR )
44 , m_nBackgroundColor ( PROGRESSBAR_DEFAULT_BACKGROUNDCOLOR )
45 , m_nMinRange ( PROGRESSBAR_DEFAULT_MINRANGE )
46 , m_nMaxRange ( PROGRESSBAR_DEFAULT_MAXRANGE )
47 , m_nBlockValue ( PROGRESSBAR_DEFAULT_BLOCKVALUE )
48 , m_nValue ( PROGRESSBAR_DEFAULT_VALUE )
52 ProgressBar::~ProgressBar()
56 // XInterface
58 Any SAL_CALL ProgressBar::queryInterface( const Type& rType )
60 // Attention:
61 // Don't use mutex or guard in this method!!! Is a method of XInterface.
62 Any aReturn;
63 Reference< XInterface > xDel = BaseControl::impl_getDelegator();
64 if ( xDel.is() )
66 // If a delegator exists, forward question to its queryInterface.
67 // Delegator will ask its own queryAggregation!
68 aReturn = xDel->queryInterface( rType );
70 else
72 // If a delegator is unknown, forward question to own queryAggregation.
73 aReturn = queryAggregation( rType );
76 return aReturn;
79 // XInterface
81 void SAL_CALL ProgressBar::acquire() throw()
83 // Attention:
84 // Don't use mutex or guard in this method!!! Is a method of XInterface.
86 // Forward to baseclass
87 BaseControl::acquire();
90 // XInterface
92 void SAL_CALL ProgressBar::release() throw()
94 // Attention:
95 // Don't use mutex or guard in this method!!! Is a method of XInterface.
97 // Forward to baseclass
98 BaseControl::release();
101 // XTypeProvider
103 Sequence< Type > SAL_CALL ProgressBar::getTypes()
105 static OTypeCollection ourTypeCollection(
106 cppu::UnoType<XControlModel>::get(),
107 cppu::UnoType<XProgressBar>::get(),
108 BaseControl::getTypes() );
110 return ourTypeCollection.getTypes();
113 // XAggregation
115 Any SAL_CALL ProgressBar::queryAggregation( const Type& aType )
117 // Ask for my own supported interfaces ...
118 // Attention: XTypeProvider and XInterface are supported by OComponentHelper!
119 Any aReturn ( ::cppu::queryInterface( aType ,
120 static_cast< XControlModel* > ( this ) ,
121 static_cast< XProgressBar* > ( this )
125 // If searched interface not supported by this class ...
126 if ( !aReturn.hasValue() )
128 // ... ask baseclasses.
129 aReturn = BaseControl::queryAggregation( aType );
132 return aReturn;
135 // XProgressBar
137 void SAL_CALL ProgressBar::setForegroundColor( sal_Int32 nColor )
139 // Ready for multithreading
140 MutexGuard aGuard (m_aMutex);
142 // Safe color for later use.
143 m_nForegroundColor = Color(nColor);
145 // Repaint control
146 impl_paint ( 0, 0, impl_getGraphicsPeer() );
149 // XProgressBar
151 void SAL_CALL ProgressBar::setBackgroundColor ( sal_Int32 nColor )
153 // Ready for multithreading
154 MutexGuard aGuard (m_aMutex);
156 // Safe color for later use.
157 m_nBackgroundColor = Color(nColor);
159 // Repaint control
160 impl_paint ( 0, 0, impl_getGraphicsPeer() );
163 // XProgressBar
165 void SAL_CALL ProgressBar::setValue ( sal_Int32 nValue )
167 // This method is defined for follow things:
168 // 1) Values >= _nMinRange
169 // 2) Values <= _nMaxRange
171 // Ready for multithreading
172 MutexGuard aGuard (m_aMutex);
174 // save impossible cases
175 // This method is only defined for valid values
176 DBG_ASSERT ( (( nValue >= m_nMinRange ) && ( nValue <= m_nMaxRange )), "ProgressBar::setValue()\nNot valid value.\n" );
178 // If new value not valid ... do nothing in release version!
179 if (
180 ( nValue >= m_nMinRange ) &&
181 ( nValue <= m_nMaxRange )
184 // New value is ok => save this
185 m_nValue = nValue;
187 // Repaint to display changes
188 impl_paint ( 0, 0, impl_getGraphicsPeer() );
192 // XProgressBar
194 void SAL_CALL ProgressBar::setRange ( sal_Int32 nMin, sal_Int32 nMax )
196 // This method is defined for follow things:
197 // 1) All values of sal_Int32
198 // 2) Min < Max
199 // 3) Min > Max
201 // save impossible cases
202 // This method is only defined for valid values
203 // If you ignore this, the release version will produce an error "division by zero" in "ProgressBar::setValue()"!
204 DBG_ASSERT ( ( nMin != nMax ) , "ProgressBar::setRange()\nValues for MIN and MAX are the same. This is not allowed!\n" );
206 // Ready for multithreading
207 MutexGuard aGuard (m_aMutex);
209 // control the values for min and max
210 if ( nMin < nMax )
212 // Take correct Min and Max
213 m_nMinRange = nMin;
214 m_nMaxRange = nMax;
216 else
218 // Change Min and Max automatically
219 m_nMinRange = nMax;
220 m_nMaxRange = nMin;
223 // assure that m_nValue is within the range
224 if (!(m_nMinRange < m_nValue && m_nValue < m_nMaxRange))
225 m_nValue = m_nMinRange;
227 impl_recalcRange ();
229 // Do not repaint the control at this place!!!
230 // An old "m_nValue" is set and can not be correct for this new range.
231 // Next call of "ProgressBar::setValue()" do this.
234 // XProgressBar
236 sal_Int32 SAL_CALL ProgressBar::getValue ()
238 // Ready for multithreading
239 MutexGuard aGuard (m_aMutex);
241 return m_nValue;
244 // XWindow
246 void SAL_CALL ProgressBar::setPosSize (
247 sal_Int32 nX,
248 sal_Int32 nY,
249 sal_Int32 nWidth,
250 sal_Int32 nHeight,
251 sal_Int16 nFlags
254 // Take old size BEFORE you set the new values at baseclass!
255 // You will control changes. At the other way, the values are the same!
256 Rectangle aBasePosSize = getPosSize ();
257 BaseControl::setPosSize (nX, nY, nWidth, nHeight, nFlags);
259 // Do only, if size has changed.
260 if (
261 ( nWidth != aBasePosSize.Width ) ||
262 ( nHeight != aBasePosSize.Height )
265 impl_recalcRange ( );
266 impl_paint ( 0, 0, impl_getGraphicsPeer () );
270 // XControl
272 sal_Bool SAL_CALL ProgressBar::setModel( const Reference< XControlModel >& /*xModel*/ )
274 // A model is not possible for this control.
275 return false;
278 // XControl
280 Reference< XControlModel > SAL_CALL ProgressBar::getModel()
282 // A model is not possible for this control.
283 return Reference< XControlModel >();
286 // impl but public method to register service
288 Sequence< OUString > ProgressBar::impl_getStaticSupportedServiceNames()
290 return css::uno::Sequence<OUString>();
293 // impl but public method to register service
295 OUString ProgressBar::impl_getStaticImplementationName()
297 return "stardiv.UnoControls.ProgressBar";
300 // protected method
302 void ProgressBar::impl_paint ( sal_Int32 nX, sal_Int32 nY, const Reference< XGraphics > & rGraphics )
304 // save impossible cases
305 DBG_ASSERT ( rGraphics.is(), "ProgressBar::paint()\nCalled with invalid Reference< XGraphics > ." );
307 // This paint method is not buffered !!
308 // Every request paint the completely control. ( but only, if peer exist )
309 if ( rGraphics.is () )
311 MutexGuard aGuard (m_aMutex);
313 // Clear background
314 // (same color for line and fill)
315 rGraphics->setFillColor ( sal_Int32(m_nBackgroundColor) );
316 rGraphics->setLineColor ( sal_Int32(m_nBackgroundColor) );
317 rGraphics->drawRect ( nX, nY, impl_getWidth(), impl_getHeight() );
319 // same color for line and fill for blocks
320 rGraphics->setFillColor ( sal_Int32(m_nForegroundColor) );
321 rGraphics->setLineColor ( sal_Int32(m_nForegroundColor) );
323 sal_Int32 nBlockStart = 0; // = left site of new block
324 sal_Int32 nBlockCount = m_nBlockValue!=0.00 ? static_cast<sal_Int32>((m_nValue-m_nMinRange)/m_nBlockValue) : 0; // = number of next block
326 // Draw horizontal progressbar
327 // decision in "recalcRange()"
328 if (m_bHorizontal)
330 // Step to left side of window
331 nBlockStart = nX;
333 for ( sal_Int32 i=1; i<=nBlockCount; ++i )
335 // step free field
336 nBlockStart += PROGRESSBAR_FREESPACE;
337 // paint block
338 rGraphics->drawRect (nBlockStart, nY+PROGRESSBAR_FREESPACE, m_aBlockSize.Width, m_aBlockSize.Height);
339 // step next free field
340 nBlockStart += m_aBlockSize.Width;
343 // draw vertical progressbar
344 // decision in "recalcRange()"
345 else
347 // step to bottom side of window
348 nBlockStart = nY+impl_getHeight();
349 nBlockStart -= m_aBlockSize.Height;
351 for ( sal_Int32 i=1; i<=nBlockCount; ++i )
353 // step free field
354 nBlockStart -= PROGRESSBAR_FREESPACE;
355 // paint block
356 rGraphics->drawRect (nX+PROGRESSBAR_FREESPACE, nBlockStart, m_aBlockSize.Width, m_aBlockSize.Height);
357 // step next free field
358 nBlockStart -= m_aBlockSize.Height;
362 // Paint shadow border around the progressbar
363 rGraphics->setLineColor ( PROGRESSBAR_LINECOLOR_SHADOW );
364 rGraphics->drawLine ( nX, nY, impl_getWidth(), nY );
365 rGraphics->drawLine ( nX, nY, nX , impl_getHeight() );
367 rGraphics->setLineColor ( PROGRESSBAR_LINECOLOR_BRIGHT );
368 rGraphics->drawLine ( impl_getWidth()-1, impl_getHeight()-1, impl_getWidth()-1, nY );
369 rGraphics->drawLine ( impl_getWidth()-1, impl_getHeight()-1, nX , impl_getHeight()-1 );
373 // protected method
375 void ProgressBar::impl_recalcRange ()
377 MutexGuard aGuard (m_aMutex);
379 sal_Int32 nWindowWidth = impl_getWidth();
380 sal_Int32 nWindowHeight = impl_getHeight();
381 double fBlockHeight;
382 double fBlockWidth;
383 double fMaxBlocks;
385 if( nWindowWidth > nWindowHeight )
387 m_bHorizontal = true;
388 fBlockHeight = (nWindowHeight-(2*PROGRESSBAR_FREESPACE));
389 fBlockWidth = fBlockHeight;
390 fMaxBlocks = nWindowWidth/(fBlockWidth+PROGRESSBAR_FREESPACE);
392 else
394 m_bHorizontal = false;
395 fBlockWidth = (nWindowWidth-(2*PROGRESSBAR_FREESPACE));
396 fBlockHeight = fBlockWidth;
397 fMaxBlocks = nWindowHeight/(fBlockHeight+PROGRESSBAR_FREESPACE);
400 double fRange = m_nMaxRange-m_nMinRange;
401 double fBlockValue = fRange/fMaxBlocks;
403 m_nBlockValue = fBlockValue;
404 m_aBlockSize.Height = static_cast<sal_Int32>(fBlockHeight);
405 m_aBlockSize.Width = static_cast<sal_Int32>(fBlockWidth);
408 } // namespace unocontrols
410 /* vim:set shiftwidth=4 softtabstop=4 expandtab: */