LibreOffice Module UnoControls (master) 1
progressbar.cxx
Go to the documentation of this file.
1/* -*- Mode: C++; tab-width: 4; indent-tabs-mode: nil; c-basic-offset: 4 -*- */
2/*
3 * This file is part of the LibreOffice project.
4 *
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/.
8 *
9 * This file incorporates work covered by the following license notice:
10 *
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 .
18 */
19
20#include <progressbar.hxx>
21
22#include <com/sun/star/awt/XGraphics.hpp>
23#include <tools/debug.hxx>
26
27using namespace ::cppu;
28using namespace ::osl;
29using namespace ::com::sun::star::uno;
30using namespace ::com::sun::star::lang;
31using namespace ::com::sun::star::awt;
32
33namespace unocontrols {
34
35// construct/destruct
36
37ProgressBar::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 )
47{
48}
49
50ProgressBar::~ProgressBar()
51{
52}
53
54// XInterface
55
56Any SAL_CALL ProgressBar::queryInterface( const Type& rType )
57{
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 )
63 )
64 );
65
66 // If searched interface not supported by this class ...
67 if ( !aReturn.hasValue() )
68 {
69 // ... ask baseclasses.
70 aReturn = BaseControl::queryInterface( rType );
71 }
72
73 return aReturn;
74}
75
76// XInterface
77
78void SAL_CALL ProgressBar::acquire() noexcept
79{
80 // Attention:
81 // Don't use mutex or guard in this method!!! Is a method of XInterface.
82
83 // Forward to baseclass
84 BaseControl::acquire();
85}
86
87// XInterface
88
89void SAL_CALL ProgressBar::release() noexcept
90{
91 // Attention:
92 // Don't use mutex or guard in this method!!! Is a method of XInterface.
93
94 // Forward to baseclass
95 BaseControl::release();
96}
97
98// XTypeProvider
99
100Sequence< Type > SAL_CALL ProgressBar::getTypes()
101{
102 static OTypeCollection ourTypeCollection(
105 BaseControl::getTypes() );
106
107 return ourTypeCollection.getTypes();
108}
109
110// XProgressBar
111
112void SAL_CALL ProgressBar::setForegroundColor( sal_Int32 nColor )
113{
114 // Ready for multithreading
115 MutexGuard aGuard (m_aMutex);
116
117 // Safe color for later use.
118 m_nForegroundColor = Color(ColorTransparency, nColor);
119
120 // Repaint control
121 impl_paint ( 0, 0, impl_getGraphicsPeer() );
122}
123
124// XProgressBar
125
126void SAL_CALL ProgressBar::setBackgroundColor ( sal_Int32 nColor )
127{
128 // Ready for multithreading
129 MutexGuard aGuard (m_aMutex);
130
131 // Safe color for later use.
132 m_nBackgroundColor = Color(ColorTransparency, nColor);
133
134 // Repaint control
135 impl_paint ( 0, 0, impl_getGraphicsPeer() );
136}
137
138// XProgressBar
139
140void SAL_CALL ProgressBar::setValue ( sal_Int32 nValue )
141{
142 // This method is defined for follow things:
143 // 1) Values >= _nMinRange
144 // 2) Values <= _nMaxRange
145
146 // Ready for multithreading
147 MutexGuard aGuard (m_aMutex);
148
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" );
152
153 // If new value not valid ... do nothing in release version!
154 if (
155 ( nValue >= m_nMinRange ) &&
156 ( nValue <= m_nMaxRange )
157 )
158 {
159 // New value is ok => save this
160 m_nValue = nValue;
161
162 // Repaint to display changes
163 impl_paint ( 0, 0, impl_getGraphicsPeer() );
164 }
165}
166
167// XProgressBar
168
169void SAL_CALL ProgressBar::setRange ( sal_Int32 nMin, sal_Int32 nMax )
170{
171 // This method is defined for follow things:
172 // 1) All values of sal_Int32
173 // 2) Min < Max
174 // 3) Min > Max
175
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" );
180
181 // Ready for multithreading
182 MutexGuard aGuard (m_aMutex);
183
184 // control the values for min and max
185 if ( nMin < nMax )
186 {
187 // Take correct Min and Max
188 m_nMinRange = nMin;
189 m_nMaxRange = nMax;
190 }
191 else
192 {
193 // Change Min and Max automatically
194 m_nMinRange = nMax;
195 m_nMaxRange = nMin;
196 }
197
198 // assure that m_nValue is within the range
199 if (m_nMinRange >= m_nValue || m_nValue >= m_nMaxRange)
200 m_nValue = m_nMinRange;
201
202 impl_recalcRange ();
203
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.
207}
208
209// XProgressBar
210
211sal_Int32 SAL_CALL ProgressBar::getValue ()
212{
213 // Ready for multithreading
214 MutexGuard aGuard (m_aMutex);
215
216 return m_nValue;
217}
218
219// XWindow
220
221void SAL_CALL ProgressBar::setPosSize (
222 sal_Int32 nX,
223 sal_Int32 nY,
224 sal_Int32 nWidth,
225 sal_Int32 nHeight,
226 sal_Int16 nFlags
227)
228{
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);
233
234 // Do only, if size has changed.
235 if (
236 ( nWidth != aBasePosSize.Width ) ||
237 ( nHeight != aBasePosSize.Height )
238 )
239 {
240 impl_recalcRange ( );
241 impl_paint ( 0, 0, impl_getGraphicsPeer () );
242 }
243}
244
245// XControl
246
247sal_Bool SAL_CALL ProgressBar::setModel( const Reference< XControlModel >& /*xModel*/ )
248{
249 // A model is not possible for this control.
250 return false;
251}
252
253// XControl
254
255Reference< XControlModel > SAL_CALL ProgressBar::getModel()
256{
257 // A model is not possible for this control.
258 return Reference< XControlModel >();
259}
260
261// protected method
262
263void ProgressBar::impl_paint ( sal_Int32 nX, sal_Int32 nY, const Reference< XGraphics > & rGraphics )
264{
265 // save impossible cases
266 DBG_ASSERT ( rGraphics.is(), "ProgressBar::paint()\nCalled with invalid Reference< XGraphics > ." );
267
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;
272
273 MutexGuard aGuard (m_aMutex);
274
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() );
280
281 // same color for line and fill for blocks
282 rGraphics->setFillColor ( sal_Int32(m_nForegroundColor) );
283 rGraphics->setLineColor ( sal_Int32(m_nForegroundColor) );
284
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
287
288 // Draw horizontal progressbar
289 // decision in "recalcRange()"
290 if (m_bHorizontal)
291 {
292 // Step to left side of window
293 nBlockStart = nX;
294
295 for ( sal_Int32 i=1; i<=nBlockCount; ++i )
296 {
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;
303 }
304 }
305 // draw vertical progressbar
306 // decision in "recalcRange()"
307 else
308 {
309 // step to bottom side of window
310 nBlockStart = nY+impl_getHeight();
311 nBlockStart -= m_aBlockSize.Height;
312
313 for ( sal_Int32 i=1; i<=nBlockCount; ++i )
314 {
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;
321 }
322 }
323
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() );
328
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 );
332}
333
334// protected method
335
336void ProgressBar::impl_recalcRange ()
337{
338 MutexGuard aGuard (m_aMutex);
339
340 sal_Int32 nWindowWidth = impl_getWidth();
341 sal_Int32 nWindowHeight = impl_getHeight();
342 double fBlockHeight;
343 double fBlockWidth;
344 double fMaxBlocks;
345
346 if( nWindowWidth > nWindowHeight )
347 {
348 m_bHorizontal = true;
349 fBlockHeight = (nWindowHeight-(2*PROGRESSBAR_FREESPACE));
350 fBlockWidth = fBlockHeight;
351 fMaxBlocks = nWindowWidth/(fBlockWidth+PROGRESSBAR_FREESPACE);
352 }
353 else
354 {
355 m_bHorizontal = false;
356 fBlockWidth = (nWindowWidth-(2*PROGRESSBAR_FREESPACE));
357 fBlockHeight = fBlockWidth;
358 fMaxBlocks = nWindowHeight/(fBlockHeight+PROGRESSBAR_FREESPACE);
359 }
360
361 double fRange = m_nMaxRange-m_nMinRange;
362 double fBlockValue = fRange/fMaxBlocks;
363
364 m_nBlockValue = fBlockValue;
365 m_aBlockSize.Height = static_cast<sal_Int32>(fBlockHeight);
366 m_aBlockSize.Width = static_cast<sal_Int32>(fBlockWidth);
367}
368
369} // namespace unocontrols
370
371extern "C" SAL_DLLPUBLIC_EXPORT css::uno::XInterface*
373 css::uno::XComponentContext* context, css::uno::Sequence<css::uno::Any> const&)
374{
375 return cppu::acquire(new unocontrols::ProgressBar(context));
376}
377/* vim:set shiftwidth=4 softtabstop=4 expandtab: */
ProgressBar(const css::uno::Reference< css::uno::XComponentContext > &rxContext)
ColorTransparency
#define DBG_ASSERT(sCon, aError)
sal_Int16 nValue
std::mutex m_aMutex
Type
int i
constexpr Color PROGRESSBAR_DEFAULT_FOREGROUNDCOLOR
Definition: progressbar.hxx:33
constexpr bool PROGRESSBAR_DEFAULT_HORIZONTAL
Definition: progressbar.hxx:35
constexpr auto PROGRESSBAR_DEFAULT_VALUE
Definition: progressbar.hxx:39
constexpr auto PROGRESSBAR_DEFAULT_MAXRANGE
Definition: progressbar.hxx:38
constexpr sal_Int32 PROGRESSBAR_LINECOLOR_BRIGHT
Definition: progressbar.hxx:41
constexpr sal_Int32 PROGRESSBAR_LINECOLOR_SHADOW
Definition: progressbar.hxx:42
constexpr auto PROGRESSBAR_DEFAULT_BLOCKVALUE
Definition: progressbar.hxx:40
constexpr auto PROGRESSBAR_DEFAULT_MINRANGE
Definition: progressbar.hxx:37
constexpr Color PROGRESSBAR_DEFAULT_BACKGROUNDCOLOR
Definition: progressbar.hxx:34
constexpr auto PROGRESSBAR_FREESPACE
Definition: progressbar.hxx:36
SAL_DLLPUBLIC_EXPORT css::uno::XInterface * stardiv_UnoControls_ProgressBar_get_implementation(css::uno::XComponentContext *context, css::uno::Sequence< css::uno::Any > const &)
#define PROGRESSBAR_DEFAULT_BLOCKDIMENSION
Definition: progressbar.hxx:32
unsigned char sal_Bool