1 /* -*- Mode: C++; tab-width: 4; indent-tabs-mode: nil; c-basic-offset: 4 -*- */
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 <statusindicator.hxx>
22 #include <com/sun/star/awt/PosSize.hpp>
23 #include <com/sun/star/awt/XFixedText.hpp>
24 #include <com/sun/star/uno/XComponentContext.hpp>
25 #include <cppuhelper/queryinterface.hxx>
26 #include <cppuhelper/typeprovider.hxx>
28 #include <progressbar.hxx>
30 using namespace ::cppu
;
31 using namespace ::osl
;
32 using namespace ::com::sun::star::uno
;
33 using namespace ::com::sun::star::lang
;
34 using namespace ::com::sun::star::awt
;
35 using namespace ::com::sun::star::task
;
37 constexpr OUStringLiteral FIXEDTEXT_SERVICENAME
= u
"com.sun.star.awt.UnoControlFixedText";
38 constexpr OUStringLiteral FIXEDTEXT_MODELNAME
= u
"com.sun.star.awt.UnoControlFixedTextModel";
39 constexpr OUStringLiteral CONTROLNAME_TEXT
= u
"Text"; // identifier the control in container
40 constexpr OUStringLiteral CONTROLNAME_PROGRESSBAR
= u
"ProgressBar"; // -||-
42 namespace unocontrols
{
46 StatusIndicator::StatusIndicator( const css::uno::Reference
< XComponentContext
>& rxContext
)
47 : BaseContainerControl ( rxContext
)
49 // It's not allowed to work with member in this method (refcounter !!!)
50 // But with a HACK (++refcount) its "OK" :-(
51 osl_atomic_increment(&m_refCount
);
53 // Create instances for fixedtext and progress ...
54 m_xText
.set( rxContext
->getServiceManager()->createInstanceWithContext( FIXEDTEXT_SERVICENAME
, rxContext
), UNO_QUERY
);
55 m_xProgressBar
= new ProgressBar(rxContext
);
56 // ... cast controls to css::uno::Reference< XControl > and set model ...
57 // ( ProgressBar has no model !!! )
58 css::uno::Reference
< XControl
> xTextControl ( m_xText
, UNO_QUERY
);
59 xTextControl
->setModel( css::uno::Reference
< XControlModel
>( rxContext
->getServiceManager()->createInstanceWithContext( FIXEDTEXT_MODELNAME
, rxContext
), UNO_QUERY
) );
60 // ... and add controls to basecontainercontrol!
61 addControl( CONTROLNAME_TEXT
, xTextControl
);
62 addControl( CONTROLNAME_PROGRESSBAR
, m_xProgressBar
);
63 // FixedText make it automatically visible by himself ... but not the progressbar !!!
64 // it must be set explicitly
65 m_xProgressBar
->setVisible( true );
66 // Reset to defaults !!!
67 // (progressbar take automatically its own defaults)
68 m_xText
->setText( "" );
70 osl_atomic_decrement(&m_refCount
);
73 StatusIndicator::~StatusIndicator() {}
77 Any SAL_CALL
StatusIndicator::queryInterface( const Type
& rType
)
79 // Ask for my own supported interfaces ...
80 // Attention: XTypeProvider and XInterface are supported by WeakComponentImplHelper!
81 Any
aReturn ( ::cppu::queryInterface( rType
,
82 static_cast< XLayoutConstrains
* > ( this ) ,
83 static_cast< XStatusIndicator
* > ( this )
87 // If searched interface not supported by this class ...
88 if ( !aReturn
.hasValue() )
90 // ... ask baseclasses.
91 aReturn
= BaseControl::queryInterface( rType
);
99 void SAL_CALL
StatusIndicator::acquire() noexcept
102 // Don't use mutex or guard in this method!!! Is a method of XInterface.
104 // Forward to baseclass
105 BaseControl::acquire();
110 void SAL_CALL
StatusIndicator::release() noexcept
113 // Don't use mutex or guard in this method!!! Is a method of XInterface.
115 // Forward to baseclass
116 BaseControl::release();
121 Sequence
< Type
> SAL_CALL
StatusIndicator::getTypes()
123 static OTypeCollection
ourTypeCollection(
124 cppu::UnoType
<XLayoutConstrains
>::get(),
125 cppu::UnoType
<XStatusIndicator
>::get(),
126 BaseContainerControl::getTypes() );
128 return ourTypeCollection
.getTypes();
133 void SAL_CALL
StatusIndicator::start( const OUString
& sText
, sal_Int32 nRange
)
135 // Ready for multithreading
136 MutexGuard
aGuard( m_aMutex
);
138 // Initialize status controls with given values.
139 m_xText
->setText( sText
);
140 m_xProgressBar
->setRange( 0, nRange
);
141 // force repaint ... fixedtext has changed !
142 impl_recalcLayout ( WindowEvent(getXWeak(),0,0,impl_getWidth(),impl_getHeight(),0,0,0,0) );
147 void SAL_CALL
StatusIndicator::end()
149 // Ready for multithreading
150 MutexGuard
aGuard( m_aMutex
);
152 // Clear values of status controls.
153 m_xText
->setText( OUString() );
154 m_xProgressBar
->setValue( 0 );
160 void SAL_CALL
StatusIndicator::setText( const OUString
& sText
)
162 // Ready for multithreading
163 MutexGuard
aGuard( m_aMutex
);
165 // Take text on right control
166 m_xText
->setText( sText
);
171 void SAL_CALL
StatusIndicator::setValue( sal_Int32 nValue
)
173 // Ready for multithreading
174 MutexGuard
aGuard( m_aMutex
);
176 // Take value on right control
177 m_xProgressBar
->setValue( nValue
);
182 void SAL_CALL
StatusIndicator::reset()
184 // Ready for multithreading
185 MutexGuard
aGuard( m_aMutex
);
187 // Clear values of status controls.
188 // (Don't hide the window! User will reset current values ... but he will not finish using of indicator!)
189 m_xText
->setText( OUString() );
190 m_xProgressBar
->setValue( 0 );
195 Size SAL_CALL
StatusIndicator::getMinimumSize ()
197 return Size (STATUSINDICATOR_DEFAULT_WIDTH
, STATUSINDICATOR_DEFAULT_HEIGHT
);
202 Size SAL_CALL
StatusIndicator::getPreferredSize ()
204 // Ready for multithreading
205 ClearableMutexGuard
aGuard ( m_aMutex
);
207 // get information about required place of child controls
208 css::uno::Reference
< XLayoutConstrains
> xTextLayout ( m_xText
, UNO_QUERY
);
209 Size aTextSize
= xTextLayout
->getPreferredSize();
213 // calc preferred size of status indicator
214 sal_Int32 nWidth
= impl_getWidth();
215 sal_Int32 nHeight
= (2*STATUSINDICATOR_FREEBORDER
)+aTextSize
.Height
;
218 if ( nWidth
<STATUSINDICATOR_DEFAULT_WIDTH
)
220 nWidth
= STATUSINDICATOR_DEFAULT_WIDTH
;
222 if ( nHeight
<STATUSINDICATOR_DEFAULT_HEIGHT
)
224 nHeight
= STATUSINDICATOR_DEFAULT_HEIGHT
;
228 return Size ( nWidth
, nHeight
);
233 Size SAL_CALL
StatusIndicator::calcAdjustedSize ( const Size
& /*rNewSize*/ )
235 return getPreferredSize ();
240 void SAL_CALL
StatusIndicator::createPeer (
241 const css::uno::Reference
< XToolkit
> & rToolkit
,
242 const css::uno::Reference
< XWindowPeer
> & rParent
245 if( !getPeer().is() )
247 BaseContainerControl::createPeer( rToolkit
, rParent
);
249 // If user forget to call "setPosSize()", we have still a correct size.
250 // And a "MinimumSize" IS A "MinimumSize"!
251 // We change not the position of control at this point.
252 Size aDefaultSize
= getMinimumSize ();
253 setPosSize ( 0, 0, aDefaultSize
.Width
, aDefaultSize
.Height
, PosSize::SIZE
);
259 sal_Bool SAL_CALL
StatusIndicator::setModel ( const css::uno::Reference
< XControlModel
> & /*rModel*/ )
267 css::uno::Reference
< XControlModel
> SAL_CALL
StatusIndicator::getModel ()
270 // return (XControlModel*)this;
271 return css::uno::Reference
< XControlModel
> ();
276 void SAL_CALL
StatusIndicator::dispose ()
278 // Ready for multithreading
279 MutexGuard
aGuard ( m_aMutex
);
281 // "removeControl()" control the state of a reference
282 css::uno::Reference
< XControl
> xTextControl ( m_xText
, UNO_QUERY
);
284 removeControl( xTextControl
);
285 removeControl( m_xProgressBar
);
287 // don't use "...->clear ()" or "... = XFixedText ()"
288 // when other hold a reference at this object !!!
289 xTextControl
->dispose();
290 m_xProgressBar
->dispose();
291 BaseContainerControl::dispose();
296 void SAL_CALL
StatusIndicator::setPosSize (
304 Rectangle aBasePosSize
= getPosSize ();
305 BaseContainerControl::setPosSize (nX
, nY
, nWidth
, nHeight
, nFlags
);
307 // if position or size changed
309 ( nWidth
!= aBasePosSize
.Width
) ||
310 ( nHeight
!= aBasePosSize
.Height
)
313 // calc new layout for controls
314 impl_recalcLayout ( WindowEvent(getXWeak(),0,0,nWidth
,nHeight
,0,0,0,0) );
315 // clear background (!)
316 // [Children were repainted in "recalcLayout" by setPosSize() automatically!]
317 getPeer()->invalidate(2);
318 // and repaint the control
319 impl_paint ( 0, 0, impl_getGraphicsPeer() );
325 WindowDescriptor
StatusIndicator::impl_getWindowDescriptor( const css::uno::Reference
< XWindowPeer
>& xParentPeer
)
327 WindowDescriptor aDescriptor
;
329 aDescriptor
.Type
= WindowClass_SIMPLE
;
330 aDescriptor
.WindowServiceName
= "floatingwindow";
331 aDescriptor
.ParentIndex
= -1;
332 aDescriptor
.Parent
= xParentPeer
;
333 aDescriptor
.Bounds
= getPosSize ();
340 void StatusIndicator::impl_paint ( sal_Int32 nX
, sal_Int32 nY
, const css::uno::Reference
< XGraphics
> & rGraphics
)
342 // This paint method is not buffered!
343 // Every request paint the completely control. (But only, if peer exist)
344 if ( !rGraphics
.is () )
347 MutexGuard
aGuard (m_aMutex
);
350 css::uno::Reference
< XWindowPeer
> xPeer( impl_getPeerWindow(), UNO_QUERY
);
352 xPeer
->setBackground( STATUSINDICATOR_BACKGROUNDCOLOR
);
354 // FixedText background = gray
355 css::uno::Reference
< XControl
> xTextControl( m_xText
, UNO_QUERY
);
356 xPeer
= xTextControl
->getPeer();
358 xPeer
->setBackground( STATUSINDICATOR_BACKGROUNDCOLOR
);
360 // Progress background = gray
361 xPeer
= m_xProgressBar
->getPeer();
363 xPeer
->setBackground( STATUSINDICATOR_BACKGROUNDCOLOR
);
365 // paint shadow border
366 rGraphics
->setLineColor ( STATUSINDICATOR_LINECOLOR_BRIGHT
);
367 rGraphics
->drawLine ( nX
, nY
, impl_getWidth(), nY
);
368 rGraphics
->drawLine ( nX
, nY
, nX
, impl_getHeight() );
370 rGraphics
->setLineColor ( STATUSINDICATOR_LINECOLOR_SHADOW
);
371 rGraphics
->drawLine ( impl_getWidth()-1, impl_getHeight()-1, impl_getWidth()-1, nY
);
372 rGraphics
->drawLine ( impl_getWidth()-1, impl_getHeight()-1, nX
, impl_getHeight()-1 );
377 void StatusIndicator::impl_recalcLayout ( const WindowEvent
& aEvent
)
379 sal_Int32 nX_ProgressBar
;
380 sal_Int32 nY_ProgressBar
;
381 sal_Int32 nWidth_ProgressBar
;
382 sal_Int32 nHeight_ProgressBar
;
385 sal_Int32 nWidth_Text
;
386 sal_Int32 nHeight_Text
;
388 // Ready for multithreading
389 MutexGuard
aGuard ( m_aMutex
);
391 // get information about required place of child controls
392 Size
aWindowSize ( aEvent
.Width
, aEvent
.Height
);
393 css::uno::Reference
< XLayoutConstrains
> xTextLayout ( m_xText
, UNO_QUERY
);
394 Size aTextSize
= xTextLayout
->getPreferredSize();
396 if( aWindowSize
.Width
< STATUSINDICATOR_DEFAULT_WIDTH
)
398 aWindowSize
.Width
= STATUSINDICATOR_DEFAULT_WIDTH
;
400 if( aWindowSize
.Height
< STATUSINDICATOR_DEFAULT_HEIGHT
)
402 aWindowSize
.Height
= STATUSINDICATOR_DEFAULT_HEIGHT
;
405 // calc position and size of child controls
406 nX_Text
= STATUSINDICATOR_FREEBORDER
;
407 nY_Text
= STATUSINDICATOR_FREEBORDER
;
408 nWidth_Text
= aTextSize
.Width
;
409 nHeight_Text
= aTextSize
.Height
;
411 nX_ProgressBar
= nX_Text
+nWidth_Text
+STATUSINDICATOR_FREEBORDER
;
412 nY_ProgressBar
= nY_Text
;
413 nWidth_ProgressBar
= aWindowSize
.Width
-nWidth_Text
-(3*STATUSINDICATOR_FREEBORDER
);
414 nHeight_ProgressBar
= nHeight_Text
;
416 // Set new position and size on all controls
417 css::uno::Reference
< XWindow
> xTextWindow ( m_xText
, UNO_QUERY
);
419 xTextWindow
->setPosSize ( nX_Text
, nY_Text
, nWidth_Text
, nHeight_Text
, 15 );
420 m_xProgressBar
->setPosSize( nX_ProgressBar
, nY_ProgressBar
, nWidth_ProgressBar
, nHeight_ProgressBar
, 15 );
423 } // namespace unocontrols
425 extern "C" SAL_DLLPUBLIC_EXPORT
css::uno::XInterface
*
426 stardiv_UnoControls_StatusIndicator_get_implementation(
427 css::uno::XComponentContext
* context
, css::uno::Sequence
<css::uno::Any
> const&)
429 return cppu::acquire(new unocontrols::StatusIndicator(context
));
432 /* vim:set shiftwidth=4 softtabstop=4 expandtab: */