Version 7.6.3.2-android, tag libreoffice-7.6.3.2-android
[LibreOffice.git] / UnoControls / source / controls / progressbar.cxx
blob6ba8874fbb885b462ee7cdb5bf76e7d351c41cf9
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 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 {
35 // construct/destruct
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()
54 // XInterface
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 );
73 return aReturn;
76 // XInterface
78 void SAL_CALL ProgressBar::acquire() noexcept
80 // Attention:
81 // Don't use mutex or guard in this method!!! Is a method of XInterface.
83 // Forward to baseclass
84 BaseControl::acquire();
87 // XInterface
89 void SAL_CALL ProgressBar::release() noexcept
91 // Attention:
92 // Don't use mutex or guard in this method!!! Is a method of XInterface.
94 // Forward to baseclass
95 BaseControl::release();
98 // XTypeProvider
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();
110 // XProgressBar
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);
120 // Repaint control
121 impl_paint ( 0, 0, impl_getGraphicsPeer() );
124 // XProgressBar
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);
134 // Repaint control
135 impl_paint ( 0, 0, impl_getGraphicsPeer() );
138 // XProgressBar
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!
154 if (
155 ( nValue >= m_nMinRange ) &&
156 ( nValue <= m_nMaxRange )
159 // New value is ok => save this
160 m_nValue = nValue;
162 // Repaint to display changes
163 impl_paint ( 0, 0, impl_getGraphicsPeer() );
167 // XProgressBar
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
173 // 2) Min < Max
174 // 3) Min > Max
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
185 if ( nMin < nMax )
187 // Take correct Min and Max
188 m_nMinRange = nMin;
189 m_nMaxRange = nMax;
191 else
193 // Change Min and Max automatically
194 m_nMinRange = nMax;
195 m_nMaxRange = nMin;
198 // assure that m_nValue is within the range
199 if (m_nMinRange >= m_nValue || m_nValue >= m_nMaxRange)
200 m_nValue = m_nMinRange;
202 impl_recalcRange ();
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.
209 // XProgressBar
211 sal_Int32 SAL_CALL ProgressBar::getValue ()
213 // Ready for multithreading
214 MutexGuard aGuard (m_aMutex);
216 return m_nValue;
219 // XWindow
221 void SAL_CALL ProgressBar::setPosSize (
222 sal_Int32 nX,
223 sal_Int32 nY,
224 sal_Int32 nWidth,
225 sal_Int32 nHeight,
226 sal_Int16 nFlags
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.
235 if (
236 ( nWidth != aBasePosSize.Width ) ||
237 ( nHeight != aBasePosSize.Height )
240 impl_recalcRange ( );
241 impl_paint ( 0, 0, impl_getGraphicsPeer () );
245 // XControl
247 sal_Bool SAL_CALL ProgressBar::setModel( const Reference< XControlModel >& /*xModel*/ )
249 // A model is not possible for this control.
250 return false;
253 // XControl
255 Reference< XControlModel > SAL_CALL ProgressBar::getModel()
257 // A model is not possible for this control.
258 return Reference< XControlModel >();
261 // protected method
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 () )
271 return;
273 MutexGuard aGuard (m_aMutex);
275 // Clear background
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()"
290 if (m_bHorizontal)
292 // Step to left side of window
293 nBlockStart = nX;
295 for ( sal_Int32 i=1; i<=nBlockCount; ++i )
297 // step free field
298 nBlockStart += PROGRESSBAR_FREESPACE;
299 // paint block
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()"
307 else
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 )
315 // step free field
316 nBlockStart -= PROGRESSBAR_FREESPACE;
317 // paint block
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 );
334 // protected method
336 void ProgressBar::impl_recalcRange ()
338 MutexGuard aGuard (m_aMutex);
340 sal_Int32 nWindowWidth = impl_getWidth();
341 sal_Int32 nWindowHeight = impl_getHeight();
342 double fBlockHeight;
343 double fBlockWidth;
344 double fMaxBlocks;
346 if( nWindowWidth > nWindowHeight )
348 m_bHorizontal = true;
349 fBlockHeight = (nWindowHeight-(2*PROGRESSBAR_FREESPACE));
350 fBlockWidth = fBlockHeight;
351 fMaxBlocks = nWindowWidth/(fBlockWidth+PROGRESSBAR_FREESPACE);
353 else
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: */