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 
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;
32 
33 namespace unocontrols {
34 
35 // construct/destruct
36 
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 
51 {
52 }
53 
54 // XInterface
55 
56 Any SAL_CALL ProgressBar::queryInterface( const Type& rType )
57 {
58  // Attention:
59  // Don't use mutex or guard in this method!!! Is a method of XInterface.
60  Any aReturn;
61  Reference< XInterface > xDel = BaseControl::impl_getDelegator();
62  if ( xDel.is() )
63  {
64  // If a delegator exists, forward question to its queryInterface.
65  // Delegator will ask its own queryAggregation!
66  aReturn = xDel->queryInterface( rType );
67  }
68  else
69  {
70  // If a delegator is unknown, forward question to own queryAggregation.
71  aReturn = queryAggregation( rType );
72  }
73 
74  return aReturn;
75 }
76 
77 // XInterface
78 
79 void SAL_CALL ProgressBar::acquire() noexcept
80 {
81  // Attention:
82  // Don't use mutex or guard in this method!!! Is a method of XInterface.
83 
84  // Forward to baseclass
86 }
87 
88 // XInterface
89 
90 void SAL_CALL ProgressBar::release() noexcept
91 {
92  // Attention:
93  // Don't use mutex or guard in this method!!! Is a method of XInterface.
94 
95  // Forward to baseclass
97 }
98 
99 // XTypeProvider
100 
101 Sequence< Type > SAL_CALL ProgressBar::getTypes()
102 {
103  static OTypeCollection ourTypeCollection(
107 
108  return ourTypeCollection.getTypes();
109 }
110 
111 // XAggregation
112 
113 Any SAL_CALL ProgressBar::queryAggregation( const Type& aType )
114 {
115  // Ask for my own supported interfaces ...
116  // Attention: XTypeProvider and XInterface are supported by OComponentHelper!
117  Any aReturn ( ::cppu::queryInterface( aType ,
118  static_cast< XControlModel* > ( this ) ,
119  static_cast< XProgressBar* > ( this )
120  )
121  );
122 
123  // If searched interface not supported by this class ...
124  if ( !aReturn.hasValue() )
125  {
126  // ... ask baseclasses.
127  aReturn = BaseControl::queryAggregation( aType );
128  }
129 
130  return aReturn;
131 }
132 
133 // XProgressBar
134 
135 void SAL_CALL ProgressBar::setForegroundColor( sal_Int32 nColor )
136 {
137  // Ready for multithreading
138  MutexGuard aGuard (m_aMutex);
139 
140  // Safe color for later use.
142 
143  // Repaint control
144  impl_paint ( 0, 0, impl_getGraphicsPeer() );
145 }
146 
147 // XProgressBar
148 
149 void SAL_CALL ProgressBar::setBackgroundColor ( sal_Int32 nColor )
150 {
151  // Ready for multithreading
152  MutexGuard aGuard (m_aMutex);
153 
154  // Safe color for later use.
156 
157  // Repaint control
158  impl_paint ( 0, 0, impl_getGraphicsPeer() );
159 }
160 
161 // XProgressBar
162 
163 void SAL_CALL ProgressBar::setValue ( sal_Int32 nValue )
164 {
165  // This method is defined for follow things:
166  // 1) Values >= _nMinRange
167  // 2) Values <= _nMaxRange
168 
169  // Ready for multithreading
170  MutexGuard aGuard (m_aMutex);
171 
172  // save impossible cases
173  // This method is only defined for valid values
174  DBG_ASSERT ( (( nValue >= m_nMinRange ) && ( nValue <= m_nMaxRange )), "ProgressBar::setValue()\nNot valid value.\n" );
175 
176  // If new value not valid ... do nothing in release version!
177  if (
178  ( nValue >= m_nMinRange ) &&
179  ( nValue <= m_nMaxRange )
180  )
181  {
182  // New value is ok => save this
183  m_nValue = nValue;
184 
185  // Repaint to display changes
186  impl_paint ( 0, 0, impl_getGraphicsPeer() );
187  }
188 }
189 
190 // XProgressBar
191 
192 void SAL_CALL ProgressBar::setRange ( sal_Int32 nMin, sal_Int32 nMax )
193 {
194  // This method is defined for follow things:
195  // 1) All values of sal_Int32
196  // 2) Min < Max
197  // 3) Min > Max
198 
199  // save impossible cases
200  // This method is only defined for valid values
201  // If you ignore this, the release version will produce an error "division by zero" in "ProgressBar::setValue()"!
202  DBG_ASSERT ( ( nMin != nMax ) , "ProgressBar::setRange()\nValues for MIN and MAX are the same. This is not allowed!\n" );
203 
204  // Ready for multithreading
205  MutexGuard aGuard (m_aMutex);
206 
207  // control the values for min and max
208  if ( nMin < nMax )
209  {
210  // Take correct Min and Max
211  m_nMinRange = nMin;
212  m_nMaxRange = nMax;
213  }
214  else
215  {
216  // Change Min and Max automatically
217  m_nMinRange = nMax;
218  m_nMaxRange = nMin;
219  }
220 
221  // assure that m_nValue is within the range
224 
225  impl_recalcRange ();
226 
227  // Do not repaint the control at this place!!!
228  // An old "m_nValue" is set and can not be correct for this new range.
229  // Next call of "ProgressBar::setValue()" do this.
230 }
231 
232 // XProgressBar
233 
234 sal_Int32 SAL_CALL ProgressBar::getValue ()
235 {
236  // Ready for multithreading
237  MutexGuard aGuard (m_aMutex);
238 
239  return m_nValue;
240 }
241 
242 // XWindow
243 
244 void SAL_CALL ProgressBar::setPosSize (
245  sal_Int32 nX,
246  sal_Int32 nY,
247  sal_Int32 nWidth,
248  sal_Int32 nHeight,
249  sal_Int16 nFlags
250 )
251 {
252  // Take old size BEFORE you set the new values at baseclass!
253  // You will control changes. At the other way, the values are the same!
254  Rectangle aBasePosSize = getPosSize ();
255  BaseControl::setPosSize (nX, nY, nWidth, nHeight, nFlags);
256 
257  // Do only, if size has changed.
258  if (
259  ( nWidth != aBasePosSize.Width ) ||
260  ( nHeight != aBasePosSize.Height )
261  )
262  {
263  impl_recalcRange ( );
264  impl_paint ( 0, 0, impl_getGraphicsPeer () );
265  }
266 }
267 
268 // XControl
269 
270 sal_Bool SAL_CALL ProgressBar::setModel( const Reference< XControlModel >& /*xModel*/ )
271 {
272  // A model is not possible for this control.
273  return false;
274 }
275 
276 // XControl
277 
278 Reference< XControlModel > SAL_CALL ProgressBar::getModel()
279 {
280  // A model is not possible for this control.
281  return Reference< XControlModel >();
282 }
283 
284 // protected method
285 
286 void ProgressBar::impl_paint ( sal_Int32 nX, sal_Int32 nY, const Reference< XGraphics > & rGraphics )
287 {
288  // save impossible cases
289  DBG_ASSERT ( rGraphics.is(), "ProgressBar::paint()\nCalled with invalid Reference< XGraphics > ." );
290 
291  // This paint method is not buffered !!
292  // Every request paint the completely control. ( but only, if peer exist )
293  if ( !rGraphics.is () )
294  return;
295 
296  MutexGuard aGuard (m_aMutex);
297 
298  // Clear background
299  // (same color for line and fill)
300  rGraphics->setFillColor ( sal_Int32(m_nBackgroundColor) );
301  rGraphics->setLineColor ( sal_Int32(m_nBackgroundColor) );
302  rGraphics->drawRect ( nX, nY, impl_getWidth(), impl_getHeight() );
303 
304  // same color for line and fill for blocks
305  rGraphics->setFillColor ( sal_Int32(m_nForegroundColor) );
306  rGraphics->setLineColor ( sal_Int32(m_nForegroundColor) );
307 
308  sal_Int32 nBlockStart = 0; // = left site of new block
309  sal_Int32 nBlockCount = m_nBlockValue!=0.00 ? static_cast<sal_Int32>((m_nValue-m_nMinRange)/m_nBlockValue) : 0; // = number of next block
310 
311  // Draw horizontal progressbar
312  // decision in "recalcRange()"
313  if (m_bHorizontal)
314  {
315  // Step to left side of window
316  nBlockStart = nX;
317 
318  for ( sal_Int32 i=1; i<=nBlockCount; ++i )
319  {
320  // step free field
321  nBlockStart += PROGRESSBAR_FREESPACE;
322  // paint block
323  rGraphics->drawRect (nBlockStart, nY+PROGRESSBAR_FREESPACE, m_aBlockSize.Width, m_aBlockSize.Height);
324  // step next free field
325  nBlockStart += m_aBlockSize.Width;
326  }
327  }
328  // draw vertical progressbar
329  // decision in "recalcRange()"
330  else
331  {
332  // step to bottom side of window
333  nBlockStart = nY+impl_getHeight();
334  nBlockStart -= m_aBlockSize.Height;
335 
336  for ( sal_Int32 i=1; i<=nBlockCount; ++i )
337  {
338  // step free field
339  nBlockStart -= PROGRESSBAR_FREESPACE;
340  // paint block
341  rGraphics->drawRect (nX+PROGRESSBAR_FREESPACE, nBlockStart, m_aBlockSize.Width, m_aBlockSize.Height);
342  // step next free field
343  nBlockStart -= m_aBlockSize.Height;
344  }
345  }
346 
347  // Paint shadow border around the progressbar
348  rGraphics->setLineColor ( PROGRESSBAR_LINECOLOR_SHADOW );
349  rGraphics->drawLine ( nX, nY, impl_getWidth(), nY );
350  rGraphics->drawLine ( nX, nY, nX , impl_getHeight() );
351 
352  rGraphics->setLineColor ( PROGRESSBAR_LINECOLOR_BRIGHT );
353  rGraphics->drawLine ( impl_getWidth()-1, impl_getHeight()-1, impl_getWidth()-1, nY );
354  rGraphics->drawLine ( impl_getWidth()-1, impl_getHeight()-1, nX , impl_getHeight()-1 );
355 }
356 
357 // protected method
358 
360 {
361  MutexGuard aGuard (m_aMutex);
362 
363  sal_Int32 nWindowWidth = impl_getWidth();
364  sal_Int32 nWindowHeight = impl_getHeight();
365  double fBlockHeight;
366  double fBlockWidth;
367  double fMaxBlocks;
368 
369  if( nWindowWidth > nWindowHeight )
370  {
371  m_bHorizontal = true;
372  fBlockHeight = (nWindowHeight-(2*PROGRESSBAR_FREESPACE));
373  fBlockWidth = fBlockHeight;
374  fMaxBlocks = nWindowWidth/(fBlockWidth+PROGRESSBAR_FREESPACE);
375  }
376  else
377  {
378  m_bHorizontal = false;
379  fBlockWidth = (nWindowWidth-(2*PROGRESSBAR_FREESPACE));
380  fBlockHeight = fBlockWidth;
381  fMaxBlocks = nWindowHeight/(fBlockHeight+PROGRESSBAR_FREESPACE);
382  }
383 
384  double fRange = m_nMaxRange-m_nMinRange;
385  double fBlockValue = fRange/fMaxBlocks;
386 
387  m_nBlockValue = fBlockValue;
388  m_aBlockSize.Height = static_cast<sal_Int32>(fBlockHeight);
389  m_aBlockSize.Width = static_cast<sal_Int32>(fBlockWidth);
390 }
391 
392 } // namespace unocontrols
393 
394 extern "C" SAL_DLLPUBLIC_EXPORT css::uno::XInterface*
396  css::uno::XComponentContext* context, css::uno::Sequence<css::uno::Any> const&)
397 {
398  return cppu::acquire(new unocontrols::ProgressBar(context));
399 }
400 /* vim:set shiftwidth=4 softtabstop=4 expandtab: */
Type
virtual void SAL_CALL setRange(sal_Int32 nMin, sal_Int32 nMax) override
virtual void SAL_CALL acquire() noexcept override
increment refcount XInterface release() A RuntimeException is thrown.
Definition: basecontrol.cxx:90
constexpr Color PROGRESSBAR_DEFAULT_BACKGROUNDCOLOR
Definition: progressbar.hxx:34
virtual css::uno::Sequence< css::uno::Type > SAL_CALL getTypes() override
get information about supported interfaces XTypeProvider
virtual void SAL_CALL setValue(sal_Int32 nValue) override
css::awt::Size m_aBlockSize
mutable::osl::Mutex m_aMutex
virtual void SAL_CALL acquire() noexcept override
increment refcount XInterface release() A RuntimeException is thrown.
Definition: progressbar.cxx:79
const css::uno::Reference< css::awt::XGraphics > & impl_getGraphicsPeer() const
constexpr auto PROGRESSBAR_DEFAULT_BLOCKVALUE
Definition: progressbar.hxx:40
virtual void SAL_CALL release() noexcept override
decrement refcount XInterface acquire() A RuntimeException is thrown.
Definition: progressbar.cxx:90
sal_Int32 impl_getHeight() const
virtual css::uno::Any SAL_CALL queryInterface(const css::uno::Type &aType) override
give answer, if interface is supported The interfaces are searched by type.
Definition: progressbar.cxx:56
constexpr auto PROGRESSBAR_DEFAULT_VALUE
Definition: progressbar.hxx:39
ProgressBar(const css::uno::Reference< css::uno::XComponentContext > &rxContext)
constexpr bool PROGRESSBAR_DEFAULT_HORIZONTAL
Definition: progressbar.hxx:35
#define DBG_ASSERT(sCon, aError)
int i
virtual ~ProgressBar() override
Definition: progressbar.cxx:50
constexpr sal_Int32 PROGRESSBAR_LINECOLOR_SHADOW
Definition: progressbar.hxx:42
sal_Int32 impl_getWidth() const
virtual void SAL_CALL release() noexcept override
decrement refcount XInterface acquire() A RuntimeException is thrown.
unsigned char sal_Bool
constexpr auto PROGRESSBAR_DEFAULT_MAXRANGE
Definition: progressbar.hxx:38
virtual void SAL_CALL setPosSize(sal_Int32 nX, sal_Int32 nY, sal_Int32 nWidth, sal_Int32 nHeight, sal_Int16 nFlags) override
ColorTransparency
virtual css::uno::Reference< css::awt::XControlModel > SAL_CALL getModel() override
constexpr auto PROGRESSBAR_FREESPACE
Definition: progressbar.hxx:36
virtual css::uno::Any SAL_CALL queryAggregation(const css::uno::Type &aType) override
virtual sal_Int32 SAL_CALL getValue() override
constexpr sal_Int32 PROGRESSBAR_LINECOLOR_BRIGHT
Definition: progressbar.hxx:41
virtual void impl_paint(sal_Int32 nX, sal_Int32 nY, const css::uno::Reference< css::awt::XGraphics > &xGraphics) override
virtual css::awt::Rectangle SAL_CALL getPosSize() override
virtual sal_Bool SAL_CALL setModel(const css::uno::Reference< css::awt::XControlModel > &xModel) override
virtual void SAL_CALL setForegroundColor(sal_Int32 nColor) override
virtual void SAL_CALL setPosSize(sal_Int32 nX, sal_Int32 nY, sal_Int32 nWidth, sal_Int32 nHeight, sal_Int16 nFlags) override
const css::uno::Reference< css::uno::XInterface > & impl_getDelegator() const
#define PROGRESSBAR_DEFAULT_BLOCKDIMENSION
Definition: progressbar.hxx:32
constexpr Color PROGRESSBAR_DEFAULT_FOREGROUNDCOLOR
Definition: progressbar.hxx:33
css::uno::Any SAL_CALL queryAggregation(const css::uno::Type &aType) override
virtual css::uno::Sequence< css::uno::Type > SAL_CALL getTypes() override
get information about supported interfaces XTypeProvider
SAL_DLLPUBLIC_EXPORT css::uno::XInterface * stardiv_UnoControls_ProgressBar_get_implementation(css::uno::XComponentContext *context, css::uno::Sequence< css::uno::Any > const &)
constexpr auto PROGRESSBAR_DEFAULT_MINRANGE
Definition: progressbar.hxx:37
virtual void SAL_CALL setBackgroundColor(sal_Int32 nColor) override
css::uno::Any SAL_CALL queryInterface(const css::uno::Type &rType, Interface1 *p1)