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 : StatusIndicator_BASE(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( u
""_ustr
);
70 osl_atomic_decrement(&m_refCount
);
73 StatusIndicator::~StatusIndicator() {}
77 void SAL_CALL
StatusIndicator::start( const OUString
& sText
, sal_Int32 nRange
)
79 // Ready for multithreading
80 MutexGuard
aGuard( m_aMutex
);
82 // Initialize status controls with given values.
83 m_xText
->setText( sText
);
84 m_xProgressBar
->setRange( 0, nRange
);
85 // force repaint ... fixedtext has changed !
86 impl_recalcLayout ( WindowEvent(getXWeak(),0,0,impl_getWidth(),impl_getHeight(),0,0,0,0) );
91 void SAL_CALL
StatusIndicator::end()
93 // Ready for multithreading
94 MutexGuard
aGuard( m_aMutex
);
96 // Clear values of status controls.
97 m_xText
->setText( OUString() );
98 m_xProgressBar
->setValue( 0 );
104 void SAL_CALL
StatusIndicator::setText( const OUString
& sText
)
106 // Ready for multithreading
107 MutexGuard
aGuard( m_aMutex
);
109 // Take text on right control
110 m_xText
->setText( sText
);
115 void SAL_CALL
StatusIndicator::setValue( sal_Int32 nValue
)
117 // Ready for multithreading
118 MutexGuard
aGuard( m_aMutex
);
120 // Take value on right control
121 m_xProgressBar
->setValue( nValue
);
126 void SAL_CALL
StatusIndicator::reset()
128 // Ready for multithreading
129 MutexGuard
aGuard( m_aMutex
);
131 // Clear values of status controls.
132 // (Don't hide the window! User will reset current values ... but he will not finish using of indicator!)
133 m_xText
->setText( OUString() );
134 m_xProgressBar
->setValue( 0 );
139 Size SAL_CALL
StatusIndicator::getMinimumSize ()
141 return Size (STATUSINDICATOR_DEFAULT_WIDTH
, STATUSINDICATOR_DEFAULT_HEIGHT
);
146 Size SAL_CALL
StatusIndicator::getPreferredSize ()
148 // Ready for multithreading
149 ClearableMutexGuard
aGuard ( m_aMutex
);
151 // get information about required place of child controls
152 css::uno::Reference
< XLayoutConstrains
> xTextLayout ( m_xText
, UNO_QUERY
);
153 Size aTextSize
= xTextLayout
->getPreferredSize();
157 // calc preferred size of status indicator
158 sal_Int32 nWidth
= impl_getWidth();
159 sal_Int32 nHeight
= (2*STATUSINDICATOR_FREEBORDER
)+aTextSize
.Height
;
162 if ( nWidth
<STATUSINDICATOR_DEFAULT_WIDTH
)
164 nWidth
= STATUSINDICATOR_DEFAULT_WIDTH
;
166 if ( nHeight
<STATUSINDICATOR_DEFAULT_HEIGHT
)
168 nHeight
= STATUSINDICATOR_DEFAULT_HEIGHT
;
172 return Size ( nWidth
, nHeight
);
177 Size SAL_CALL
StatusIndicator::calcAdjustedSize ( const Size
& /*rNewSize*/ )
179 return getPreferredSize ();
184 void SAL_CALL
StatusIndicator::createPeer (
185 const css::uno::Reference
< XToolkit
> & rToolkit
,
186 const css::uno::Reference
< XWindowPeer
> & rParent
189 if( !getPeer().is() )
191 BaseContainerControl::createPeer( rToolkit
, rParent
);
193 // If user forget to call "setPosSize()", we have still a correct size.
194 // And a "MinimumSize" IS A "MinimumSize"!
195 // We change not the position of control at this point.
196 Size aDefaultSize
= getMinimumSize ();
197 setPosSize ( 0, 0, aDefaultSize
.Width
, aDefaultSize
.Height
, PosSize::SIZE
);
203 sal_Bool SAL_CALL
StatusIndicator::setModel ( const css::uno::Reference
< XControlModel
> & /*rModel*/ )
211 css::uno::Reference
< XControlModel
> SAL_CALL
StatusIndicator::getModel ()
214 // return (XControlModel*)this;
215 return css::uno::Reference
< XControlModel
> ();
220 void SAL_CALL
StatusIndicator::dispose ()
222 // Ready for multithreading
223 MutexGuard
aGuard ( m_aMutex
);
225 // "removeControl()" control the state of a reference
226 css::uno::Reference
< XControl
> xTextControl ( m_xText
, UNO_QUERY
);
228 removeControl( xTextControl
);
229 removeControl( m_xProgressBar
);
231 // don't use "...->clear ()" or "... = XFixedText ()"
232 // when other hold a reference at this object !!!
233 xTextControl
->dispose();
234 m_xProgressBar
->dispose();
235 m_xProgressBar
.clear();
237 BaseContainerControl::dispose();
242 void SAL_CALL
StatusIndicator::setPosSize (
250 Rectangle aBasePosSize
= getPosSize ();
251 BaseContainerControl::setPosSize (nX
, nY
, nWidth
, nHeight
, nFlags
);
253 // if position or size changed
255 ( nWidth
!= aBasePosSize
.Width
) ||
256 ( nHeight
!= aBasePosSize
.Height
)
259 // calc new layout for controls
260 impl_recalcLayout ( WindowEvent(getXWeak(),0,0,nWidth
,nHeight
,0,0,0,0) );
261 // clear background (!)
262 // [Children were repainted in "recalcLayout" by setPosSize() automatically!]
263 getPeer()->invalidate(2);
264 // and repaint the control
265 impl_paint ( 0, 0, impl_getGraphicsPeer() );
271 WindowDescriptor
StatusIndicator::impl_getWindowDescriptor( const css::uno::Reference
< XWindowPeer
>& xParentPeer
)
273 WindowDescriptor aDescriptor
;
275 aDescriptor
.Type
= WindowClass_SIMPLE
;
276 aDescriptor
.WindowServiceName
= "floatingwindow";
277 aDescriptor
.ParentIndex
= -1;
278 aDescriptor
.Parent
= xParentPeer
;
279 aDescriptor
.Bounds
= getPosSize ();
286 void StatusIndicator::impl_paint ( sal_Int32 nX
, sal_Int32 nY
, const css::uno::Reference
< XGraphics
> & rGraphics
)
288 // This paint method is not buffered!
289 // Every request paint the completely control. (But only, if peer exist)
290 if ( !rGraphics
.is () )
293 MutexGuard
aGuard (m_aMutex
);
296 css::uno::Reference
< XWindowPeer
> xPeer( impl_getPeerWindow(), UNO_QUERY
);
298 xPeer
->setBackground( STATUSINDICATOR_BACKGROUNDCOLOR
);
300 // FixedText background = gray
301 css::uno::Reference
< XControl
> xTextControl( m_xText
, UNO_QUERY
);
302 xPeer
= xTextControl
->getPeer();
304 xPeer
->setBackground( STATUSINDICATOR_BACKGROUNDCOLOR
);
306 // Progress background = gray
307 xPeer
= m_xProgressBar
->getPeer();
309 xPeer
->setBackground( STATUSINDICATOR_BACKGROUNDCOLOR
);
311 // paint shadow border
312 rGraphics
->setLineColor ( STATUSINDICATOR_LINECOLOR_BRIGHT
);
313 rGraphics
->drawLine ( nX
, nY
, impl_getWidth(), nY
);
314 rGraphics
->drawLine ( nX
, nY
, nX
, impl_getHeight() );
316 rGraphics
->setLineColor ( STATUSINDICATOR_LINECOLOR_SHADOW
);
317 rGraphics
->drawLine ( impl_getWidth()-1, impl_getHeight()-1, impl_getWidth()-1, nY
);
318 rGraphics
->drawLine ( impl_getWidth()-1, impl_getHeight()-1, nX
, impl_getHeight()-1 );
323 void StatusIndicator::impl_recalcLayout ( const WindowEvent
& aEvent
)
325 sal_Int32 nX_ProgressBar
;
326 sal_Int32 nY_ProgressBar
;
327 sal_Int32 nWidth_ProgressBar
;
328 sal_Int32 nHeight_ProgressBar
;
331 sal_Int32 nWidth_Text
;
332 sal_Int32 nHeight_Text
;
334 // Ready for multithreading
335 MutexGuard
aGuard ( m_aMutex
);
337 // get information about required place of child controls
338 Size
aWindowSize ( aEvent
.Width
, aEvent
.Height
);
339 css::uno::Reference
< XLayoutConstrains
> xTextLayout ( m_xText
, UNO_QUERY
);
340 Size aTextSize
= xTextLayout
->getPreferredSize();
342 if( aWindowSize
.Width
< STATUSINDICATOR_DEFAULT_WIDTH
)
344 aWindowSize
.Width
= STATUSINDICATOR_DEFAULT_WIDTH
;
346 if( aWindowSize
.Height
< STATUSINDICATOR_DEFAULT_HEIGHT
)
348 aWindowSize
.Height
= STATUSINDICATOR_DEFAULT_HEIGHT
;
351 // calc position and size of child controls
352 nX_Text
= STATUSINDICATOR_FREEBORDER
;
353 nY_Text
= STATUSINDICATOR_FREEBORDER
;
354 nWidth_Text
= aTextSize
.Width
;
355 nHeight_Text
= aTextSize
.Height
;
357 nX_ProgressBar
= nX_Text
+nWidth_Text
+STATUSINDICATOR_FREEBORDER
;
358 nY_ProgressBar
= nY_Text
;
359 nWidth_ProgressBar
= aWindowSize
.Width
-nWidth_Text
-(3*STATUSINDICATOR_FREEBORDER
);
360 nHeight_ProgressBar
= nHeight_Text
;
362 // Set new position and size on all controls
363 css::uno::Reference
< XWindow
> xTextWindow ( m_xText
, UNO_QUERY
);
365 xTextWindow
->setPosSize ( nX_Text
, nY_Text
, nWidth_Text
, nHeight_Text
, 15 );
366 m_xProgressBar
->setPosSize( nX_ProgressBar
, nY_ProgressBar
, nWidth_ProgressBar
, nHeight_ProgressBar
, 15 );
369 } // namespace unocontrols
371 extern "C" SAL_DLLPUBLIC_EXPORT
css::uno::XInterface
*
372 stardiv_UnoControls_StatusIndicator_get_implementation(
373 css::uno::XComponentContext
* context
, css::uno::Sequence
<css::uno::Any
> const&)
375 return cppu::acquire(new unocontrols::StatusIndicator(context
));
378 /* vim:set shiftwidth=4 softtabstop=4 expandtab: */