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/WindowAttribute.hpp>
23 #include <com/sun/star/uno/XComponentContext.hpp>
24 #include <cppuhelper/queryinterface.hxx>
25 #include <cppuhelper/typeprovider.hxx>
27 #include <progressbar.hxx>
29 using namespace ::cppu
;
30 using namespace ::osl
;
31 using namespace ::com::sun::star::uno
;
32 using namespace ::com::sun::star::lang
;
33 using namespace ::com::sun::star::awt
;
34 using namespace ::com::sun::star::task
;
36 namespace unocontrols
{
40 StatusIndicator::StatusIndicator( const css::uno::Reference
< XComponentContext
>& rxContext
)
41 : BaseContainerControl ( rxContext
)
43 // It's not allowed to work with member in this method (refcounter !!!)
44 // But with a HACK (++refcount) its "OK" :-(
47 // Create instances for fixedtext and progress ...
48 m_xText
.set( rxContext
->getServiceManager()->createInstanceWithContext( FIXEDTEXT_SERVICENAME
, rxContext
), UNO_QUERY
);
49 m_xProgressBar
= new ProgressBar(rxContext
);
50 // ... cast controls to css::uno::Reference< XControl > and set model ...
51 // ( ProgressBar has no model !!! )
52 css::uno::Reference
< XControl
> xTextControl ( m_xText
, UNO_QUERY
);
53 xTextControl
->setModel( css::uno::Reference
< XControlModel
>( rxContext
->getServiceManager()->createInstanceWithContext( FIXEDTEXT_MODELNAME
, rxContext
), UNO_QUERY
) );
54 // ... and add controls to basecontainercontrol!
55 addControl( CONTROLNAME_TEXT
, xTextControl
);
56 addControl( CONTROLNAME_PROGRESSBAR
, m_xProgressBar
.get() );
57 // FixedText make it automatically visible by himself ... but not the progressbar !!!
58 // it must be set explicitly
59 m_xProgressBar
->setVisible( true );
60 // Reset to defaults !!!
61 // (progressbar take automatically its own defaults)
62 m_xText
->setText( "" );
67 StatusIndicator::~StatusIndicator() {}
71 Any SAL_CALL
StatusIndicator::queryInterface( const Type
& rType
)
74 // Don't use mutex or guard in this method!!! Is a method of XInterface.
76 css::uno::Reference
< XInterface
> xDel
= BaseContainerControl::impl_getDelegator();
79 // If an delegator exist, forward question to his queryInterface.
80 // Delegator will ask his own queryAggregation!
81 aReturn
= xDel
->queryInterface( rType
);
85 // If an delegator unknown, forward question to own queryAggregation.
86 aReturn
= queryAggregation( rType
);
94 void SAL_CALL
StatusIndicator::acquire() throw()
97 // Don't use mutex or guard in this method!!! Is a method of XInterface.
99 // Forward to baseclass
100 BaseControl::acquire();
105 void SAL_CALL
StatusIndicator::release() throw()
108 // Don't use mutex or guard in this method!!! Is a method of XInterface.
110 // Forward to baseclass
111 BaseControl::release();
116 Sequence
< Type
> SAL_CALL
StatusIndicator::getTypes()
118 static OTypeCollection
ourTypeCollection(
119 cppu::UnoType
<XLayoutConstrains
>::get(),
120 cppu::UnoType
<XStatusIndicator
>::get(),
121 BaseContainerControl::getTypes() );
123 return ourTypeCollection
.getTypes();
128 Any SAL_CALL
StatusIndicator::queryAggregation( const Type
& aType
)
130 // Ask for my own supported interfaces ...
131 // Attention: XTypeProvider and XInterface are supported by OComponentHelper!
132 Any
aReturn ( ::cppu::queryInterface( aType
,
133 static_cast< XLayoutConstrains
* > ( this ) ,
134 static_cast< XStatusIndicator
* > ( this )
138 // If searched interface not supported by this class ...
139 if ( !aReturn
.hasValue() )
141 // ... ask baseclasses.
142 aReturn
= BaseControl::queryAggregation( aType
);
150 void SAL_CALL
StatusIndicator::start( const OUString
& sText
, sal_Int32 nRange
)
152 // Ready for multithreading
153 MutexGuard
aGuard( m_aMutex
);
155 // Initialize status controls with given values.
156 m_xText
->setText( sText
);
157 m_xProgressBar
->setRange( 0, nRange
);
158 // force repaint ... fixedtext has changed !
159 impl_recalcLayout ( WindowEvent(static_cast< OWeakObject
* >(this),0,0,impl_getWidth(),impl_getHeight(),0,0,0,0) );
164 void SAL_CALL
StatusIndicator::end()
166 // Ready for multithreading
167 MutexGuard
aGuard( m_aMutex
);
169 // Clear values of status controls.
170 m_xText
->setText( OUString() );
171 m_xProgressBar
->setValue( 0 );
177 void SAL_CALL
StatusIndicator::setText( const OUString
& sText
)
179 // Ready for multithreading
180 MutexGuard
aGuard( m_aMutex
);
182 // Take text on right control
183 m_xText
->setText( sText
);
188 void SAL_CALL
StatusIndicator::setValue( sal_Int32 nValue
)
190 // Ready for multithreading
191 MutexGuard
aGuard( m_aMutex
);
193 // Take value on right control
194 m_xProgressBar
->setValue( nValue
);
199 void SAL_CALL
StatusIndicator::reset()
201 // Ready for multithreading
202 MutexGuard
aGuard( m_aMutex
);
204 // Clear values of status controls.
205 // (Don't hide the window! User will reset current values ... but he will not finish using of indicator!)
206 m_xText
->setText( OUString() );
207 m_xProgressBar
->setValue( 0 );
212 Size SAL_CALL
StatusIndicator::getMinimumSize ()
214 return Size (STATUSINDICATOR_DEFAULT_WIDTH
, STATUSINDICATOR_DEFAULT_HEIGHT
);
219 Size SAL_CALL
StatusIndicator::getPreferredSize ()
221 // Ready for multithreading
222 ClearableMutexGuard
aGuard ( m_aMutex
);
224 // get information about required place of child controls
225 css::uno::Reference
< XLayoutConstrains
> xTextLayout ( m_xText
, UNO_QUERY
);
226 Size aTextSize
= xTextLayout
->getPreferredSize();
230 // calc preferred size of status indicator
231 sal_Int32 nWidth
= impl_getWidth();
232 sal_Int32 nHeight
= (2*STATUSINDICATOR_FREEBORDER
)+aTextSize
.Height
;
235 if ( nWidth
<STATUSINDICATOR_DEFAULT_WIDTH
)
237 nWidth
= STATUSINDICATOR_DEFAULT_WIDTH
;
239 if ( nHeight
<STATUSINDICATOR_DEFAULT_HEIGHT
)
241 nHeight
= STATUSINDICATOR_DEFAULT_HEIGHT
;
245 return Size ( nWidth
, nHeight
);
250 Size SAL_CALL
StatusIndicator::calcAdjustedSize ( const Size
& /*rNewSize*/ )
252 return getPreferredSize ();
257 void SAL_CALL
StatusIndicator::createPeer (
258 const css::uno::Reference
< XToolkit
> & rToolkit
,
259 const css::uno::Reference
< XWindowPeer
> & rParent
262 if( !getPeer().is() )
264 BaseContainerControl::createPeer( rToolkit
, rParent
);
266 // If user forget to call "setPosSize()", we have still a correct size.
267 // And a "MinimumSize" IS A "MinimumSize"!
268 // We change not the position of control at this point.
269 Size aDefaultSize
= getMinimumSize ();
270 setPosSize ( 0, 0, aDefaultSize
.Width
, aDefaultSize
.Height
, PosSize::SIZE
);
276 sal_Bool SAL_CALL
StatusIndicator::setModel ( const css::uno::Reference
< XControlModel
> & /*rModel*/ )
284 css::uno::Reference
< XControlModel
> SAL_CALL
StatusIndicator::getModel ()
287 // return (XControlModel*)this;
288 return css::uno::Reference
< XControlModel
> ();
293 void SAL_CALL
StatusIndicator::dispose ()
295 // Ready for multithreading
296 MutexGuard
aGuard ( m_aMutex
);
298 // "removeControl()" control the state of a reference
299 css::uno::Reference
< XControl
> xTextControl ( m_xText
, UNO_QUERY
);
301 removeControl( xTextControl
);
302 removeControl( m_xProgressBar
.get() );
304 // do'nt use "...->clear ()" or "... = XFixedText ()"
305 // when other hold a reference at this object !!!
306 xTextControl
->dispose();
307 m_xProgressBar
->dispose();
308 BaseContainerControl::dispose();
313 void SAL_CALL
StatusIndicator::setPosSize (
321 Rectangle aBasePosSize
= getPosSize ();
322 BaseContainerControl::setPosSize (nX
, nY
, nWidth
, nHeight
, nFlags
);
324 // if position or size changed
326 ( nWidth
!= aBasePosSize
.Width
) ||
327 ( nHeight
!= aBasePosSize
.Height
)
330 // calc new layout for controls
331 impl_recalcLayout ( WindowEvent(static_cast< OWeakObject
* >(this),0,0,nWidth
,nHeight
,0,0,0,0) );
332 // clear background (!)
333 // [Children were repainted in "recalcLayout" by setPosSize() automatically!]
334 getPeer()->invalidate(2);
335 // and repaint the control
336 impl_paint ( 0, 0, impl_getGraphicsPeer() );
340 // impl but public method to register service
342 const Sequence
< OUString
> StatusIndicator::impl_getStaticSupportedServiceNames()
344 return css::uno::Sequence
<OUString
>();
347 // impl but public method to register service
349 const OUString
StatusIndicator::impl_getStaticImplementationName()
351 return OUString("stardiv.UnoControls.StatusIndicator");
356 WindowDescriptor
* StatusIndicator::impl_getWindowDescriptor( const css::uno::Reference
< XWindowPeer
>& xParentPeer
)
358 // - used from "createPeer()" to set the values of an css::awt::WindowDescriptor !!!
359 // - if you will change the descriptor-values, you must override this virtuell function
360 // - the caller must release the memory for this dynamical descriptor !!!
362 WindowDescriptor
* pDescriptor
= new WindowDescriptor
;
364 pDescriptor
->Type
= WindowClass_SIMPLE
;
365 pDescriptor
->WindowServiceName
= "floatingwindow";
366 pDescriptor
->ParentIndex
= -1;
367 pDescriptor
->Parent
= xParentPeer
;
368 pDescriptor
->Bounds
= getPosSize ();
375 void StatusIndicator::impl_paint ( sal_Int32 nX
, sal_Int32 nY
, const css::uno::Reference
< XGraphics
> & rGraphics
)
377 // This paint method is not buffered !!
378 // Every request paint the completely control. ( but only, if peer exist )
379 if ( rGraphics
.is () )
381 MutexGuard
aGuard (m_aMutex
);
384 css::uno::Reference
< XWindowPeer
> xPeer( impl_getPeerWindow(), UNO_QUERY
);
386 xPeer
->setBackground( STATUSINDICATOR_BACKGROUNDCOLOR
);
388 // FixedText background = gray
389 css::uno::Reference
< XControl
> xTextControl( m_xText
, UNO_QUERY
);
390 xPeer
= xTextControl
->getPeer();
392 xPeer
->setBackground( STATUSINDICATOR_BACKGROUNDCOLOR
);
394 // Progress background = gray
395 xPeer
= m_xProgressBar
->getPeer();
397 xPeer
->setBackground( STATUSINDICATOR_BACKGROUNDCOLOR
);
399 // paint shadow border
400 rGraphics
->setLineColor ( STATUSINDICATOR_LINECOLOR_BRIGHT
);
401 rGraphics
->drawLine ( nX
, nY
, impl_getWidth(), nY
);
402 rGraphics
->drawLine ( nX
, nY
, nX
, impl_getHeight() );
404 rGraphics
->setLineColor ( STATUSINDICATOR_LINECOLOR_SHADOW
);
405 rGraphics
->drawLine ( impl_getWidth()-1, impl_getHeight()-1, impl_getWidth()-1, nY
);
406 rGraphics
->drawLine ( impl_getWidth()-1, impl_getHeight()-1, nX
, impl_getHeight()-1 );
412 void StatusIndicator::impl_recalcLayout ( const WindowEvent
& aEvent
)
414 sal_Int32 nX_ProgressBar
;
415 sal_Int32 nY_ProgressBar
;
416 sal_Int32 nWidth_ProgressBar
;
417 sal_Int32 nHeight_ProgressBar
;
420 sal_Int32 nWidth_Text
;
421 sal_Int32 nHeight_Text
;
423 // Ready for multithreading
424 MutexGuard
aGuard ( m_aMutex
);
426 // get information about required place of child controls
427 Size
aWindowSize ( aEvent
.Width
, aEvent
.Height
);
428 css::uno::Reference
< XLayoutConstrains
> xTextLayout ( m_xText
, UNO_QUERY
);
429 Size aTextSize
= xTextLayout
->getPreferredSize();
431 if( aWindowSize
.Width
< STATUSINDICATOR_DEFAULT_WIDTH
)
433 aWindowSize
.Width
= STATUSINDICATOR_DEFAULT_WIDTH
;
435 if( aWindowSize
.Height
< STATUSINDICATOR_DEFAULT_HEIGHT
)
437 aWindowSize
.Height
= STATUSINDICATOR_DEFAULT_HEIGHT
;
440 // calc position and size of child controls
441 nX_Text
= STATUSINDICATOR_FREEBORDER
;
442 nY_Text
= STATUSINDICATOR_FREEBORDER
;
443 nWidth_Text
= aTextSize
.Width
;
444 nHeight_Text
= aTextSize
.Height
;
446 nX_ProgressBar
= nX_Text
+nWidth_Text
+STATUSINDICATOR_FREEBORDER
;
447 nY_ProgressBar
= nY_Text
;
448 nWidth_ProgressBar
= aWindowSize
.Width
-nWidth_Text
-(3*STATUSINDICATOR_FREEBORDER
);
449 nHeight_ProgressBar
= nHeight_Text
;
451 // Set new position and size on all controls
452 css::uno::Reference
< XWindow
> xTextWindow ( m_xText
, UNO_QUERY
);
454 xTextWindow
->setPosSize ( nX_Text
, nY_Text
, nWidth_Text
, nHeight_Text
, 15 );
455 m_xProgressBar
->setPosSize( nX_ProgressBar
, nY_ProgressBar
, nWidth_ProgressBar
, nHeight_ProgressBar
, 15 );
458 } // namespace unocontrols
460 /* vim:set shiftwidth=4 softtabstop=4 expandtab: */