Version 3.6.0.4, tag libreoffice-3.6.0.4
[LibreOffice.git] / UnoControls / source / controls / statusindicator.cxx
blobc3988d80d25f2d0172a7db983a0891d59bdb0a55
1 /* -*- Mode: C++; tab-width: 4; indent-tabs-mode: nil; c-basic-offset: 4 -*- */
2 /*************************************************************************
4 * DO NOT ALTER OR REMOVE COPYRIGHT NOTICES OR THIS FILE HEADER.
6 * Copyright 2000, 2010 Oracle and/or its affiliates.
8 * OpenOffice.org - a multi-platform office productivity suite
10 * This file is part of OpenOffice.org.
12 * OpenOffice.org is free software: you can redistribute it and/or modify
13 * it under the terms of the GNU Lesser General Public License version 3
14 * only, as published by the Free Software Foundation.
16 * OpenOffice.org is distributed in the hope that it will be useful,
17 * but WITHOUT ANY WARRANTY; without even the implied warranty of
18 * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
19 * GNU Lesser General Public License version 3 for more details
20 * (a copy is included in the LICENSE file that accompanied this code).
22 * You should have received a copy of the GNU Lesser General Public License
23 * version 3 along with OpenOffice.org. If not, see
24 * <http://www.openoffice.org/license.html>
25 * for a copy of the LGPLv3 License.
27 ************************************************************************/
29 //____________________________________________________________________________________________________________
30 // my own includes
31 //____________________________________________________________________________________________________________
33 #include "statusindicator.hxx"
35 //____________________________________________________________________________________________________________
36 // includes of other projects
37 //____________________________________________________________________________________________________________
38 #include <com/sun/star/awt/InvalidateStyle.hpp>
39 #include <com/sun/star/awt/WindowAttribute.hpp>
40 #include <cppuhelper/typeprovider.hxx>
42 //____________________________________________________________________________________________________________
43 // includes of my project
44 //____________________________________________________________________________________________________________
45 #include "progressbar.hxx"
47 //____________________________________________________________________________________________________________
48 // namespace
49 //____________________________________________________________________________________________________________
51 using namespace ::cppu ;
52 using namespace ::osl ;
53 using namespace ::rtl ;
54 using namespace ::com::sun::star::uno ;
55 using namespace ::com::sun::star::lang ;
56 using namespace ::com::sun::star::awt ;
57 using namespace ::com::sun::star::task ;
59 namespace unocontrols{
61 //____________________________________________________________________________________________________________
62 // construct/destruct
63 //____________________________________________________________________________________________________________
65 StatusIndicator::StatusIndicator( const Reference< XMultiServiceFactory >& xFactory )
66 : BaseContainerControl ( xFactory )
68 // Its not allowed to work with member in this method (refcounter !!!)
69 // But with a HACK (++refcount) its "OK" :-(
70 ++m_refCount ;
72 // Create instances for fixedtext and progress ...
73 m_xText = Reference< XFixedText > ( xFactory->createInstance( FIXEDTEXT_SERVICENAME ), UNO_QUERY );
74 m_xProgressBar = Reference< XProgressBar > ( xFactory->createInstance( SERVICENAME_PROGRESSBAR ), UNO_QUERY );
75 // ... cast controls to Reference< XControl > and set model ...
76 // ( ProgressBar has no model !!! )
77 Reference< XControl > xTextControl ( m_xText , UNO_QUERY );
78 Reference< XControl > xProgressControl ( m_xProgressBar, UNO_QUERY );
79 xTextControl->setModel( Reference< XControlModel >( xFactory->createInstance( FIXEDTEXT_MODELNAME ), UNO_QUERY ) );
80 // ... and add controls to basecontainercontrol!
81 addControl( CONTROLNAME_TEXT, xTextControl );
82 addControl( CONTROLNAME_PROGRESSBAR, xProgressControl );
83 // FixedText make it automaticly visible by himself ... but not the progressbar !!!
84 // it must be set explicitly
85 Reference< XWindow > xProgressWindow( m_xProgressBar, UNO_QUERY );
86 xProgressWindow->setVisible( sal_True );
87 // Reset to defaults !!!
88 // (progressbar take automaticly its own defaults)
89 m_xText->setText( STATUSINDICATOR_DEFAULT_TEXT );
91 --m_refCount ;
94 StatusIndicator::~StatusIndicator()
96 // Release all references
97 m_xText = Reference< XFixedText >();
98 m_xProgressBar = Reference< XProgressBar >();
101 //____________________________________________________________________________________________________________
102 // XInterface
103 //____________________________________________________________________________________________________________
105 Any SAL_CALL StatusIndicator::queryInterface( const Type& rType ) throw( RuntimeException )
107 // Attention:
108 // Don't use mutex or guard in this method!!! Is a method of XInterface.
109 Any aReturn ;
110 Reference< XInterface > xDel = BaseContainerControl::impl_getDelegator();
111 if ( xDel.is() )
113 // If an delegator exist, forward question to his queryInterface.
114 // Delegator will ask his own queryAggregation!
115 aReturn = xDel->queryInterface( rType );
117 else
119 // If an delegator unknown, forward question to own queryAggregation.
120 aReturn = queryAggregation( rType );
123 return aReturn ;
126 //____________________________________________________________________________________________________________
127 // XInterface
128 //____________________________________________________________________________________________________________
130 void SAL_CALL StatusIndicator::acquire() throw()
132 // Attention:
133 // Don't use mutex or guard in this method!!! Is a method of XInterface.
135 // Forward to baseclass
136 BaseControl::acquire();
139 //____________________________________________________________________________________________________________
140 // XInterface
141 //____________________________________________________________________________________________________________
143 void SAL_CALL StatusIndicator::release() throw()
145 // Attention:
146 // Don't use mutex or guard in this method!!! Is a method of XInterface.
148 // Forward to baseclass
149 BaseControl::release();
152 //____________________________________________________________________________________________________________
153 // XTypeProvider
154 //____________________________________________________________________________________________________________
156 Sequence< Type > SAL_CALL StatusIndicator::getTypes() throw( RuntimeException )
158 // Optimize this method !
159 // We initialize a static variable only one time. And we don't must use a mutex at every call!
160 // For the first call; pTypeCollection is NULL - for the second call pTypeCollection is different from NULL!
161 static OTypeCollection* pTypeCollection = NULL ;
163 if ( pTypeCollection == NULL )
165 // Ready for multithreading; get global mutex for first call of this method only! see before
166 MutexGuard aGuard( Mutex::getGlobalMutex() );
168 // Control these pointer again ... it can be, that another instance will be faster then these!
169 if ( pTypeCollection == NULL )
171 // Create a static typecollection ...
172 static OTypeCollection aTypeCollection ( ::getCppuType(( const Reference< XLayoutConstrains >*)NULL ) ,
173 ::getCppuType(( const Reference< XStatusIndicator >*)NULL ) ,
174 BaseContainerControl::getTypes()
176 // ... and set his address to static pointer!
177 pTypeCollection = &aTypeCollection ;
181 return pTypeCollection->getTypes();
184 //____________________________________________________________________________________________________________
185 // XAggregation
186 //____________________________________________________________________________________________________________
188 Any SAL_CALL StatusIndicator::queryAggregation( const Type& aType ) throw( RuntimeException )
190 // Ask for my own supported interfaces ...
191 // Attention: XTypeProvider and XInterface are supported by OComponentHelper!
192 Any aReturn ( ::cppu::queryInterface( aType ,
193 static_cast< XLayoutConstrains* > ( this ) ,
194 static_cast< XStatusIndicator* > ( this )
198 // If searched interface not supported by this class ...
199 if ( aReturn.hasValue() == sal_False )
201 // ... ask baseclasses.
202 aReturn = BaseControl::queryAggregation( aType );
205 return aReturn ;
208 //____________________________________________________________________________________________________________
209 // XStatusIndicator
210 //____________________________________________________________________________________________________________
212 void SAL_CALL StatusIndicator::start( const OUString& sText, sal_Int32 nRange ) throw( RuntimeException )
214 // Ready for multithreading
215 MutexGuard aGuard( m_aMutex );
217 // Initialize status controls with given values.
218 m_xText->setText( sText );
219 m_xProgressBar->setRange( 0, nRange );
220 // force repaint ... fixedtext has changed !
221 impl_recalcLayout ( WindowEvent(static_cast< OWeakObject* >(this),0,0,impl_getWidth(),impl_getHeight(),0,0,0,0) ) ;
224 //____________________________________________________________________________________________________________
225 // XStatusIndicator
226 //____________________________________________________________________________________________________________
228 void SAL_CALL StatusIndicator::end() throw( RuntimeException )
230 // Ready for multithreading
231 MutexGuard aGuard( m_aMutex );
233 // Clear values of status controls.
234 m_xText->setText( OUString() );
235 m_xProgressBar->setValue( 0 );
236 setVisible( sal_False );
239 //____________________________________________________________________________________________________________
240 // XStatusIndicator
241 //____________________________________________________________________________________________________________
243 void SAL_CALL StatusIndicator::setText( const OUString& sText ) throw( RuntimeException )
245 // Ready for multithreading
246 MutexGuard aGuard( m_aMutex );
248 // Take text on right control
249 m_xText->setText( sText );
252 //____________________________________________________________________________________________________________
253 // XStatusIndicator
254 //____________________________________________________________________________________________________________
256 void SAL_CALL StatusIndicator::setValue( sal_Int32 nValue ) throw( RuntimeException )
258 // Ready for multithreading
259 MutexGuard aGuard( m_aMutex );
261 // Take value on right control
262 m_xProgressBar->setValue( nValue );
265 //____________________________________________________________________________________________________________
266 // XStatusIndicator
267 //____________________________________________________________________________________________________________
269 void SAL_CALL StatusIndicator::reset() throw( RuntimeException )
271 // Ready for multithreading
272 MutexGuard aGuard( m_aMutex );
274 // Clear values of status controls.
275 // (Don't hide the window! User will reset current values ... but he will not finish using of indicator!)
276 m_xText->setText( OUString() );
277 m_xProgressBar->setValue( 0 );
280 //____________________________________________________________________________________________________________
281 // XLayoutConstrains
282 //____________________________________________________________________________________________________________
284 Size SAL_CALL StatusIndicator::getMinimumSize () throw( RuntimeException )
286 return Size (STATUSINDICATOR_DEFAULT_WIDTH, STATUSINDICATOR_DEFAULT_HEIGHT) ;
289 //____________________________________________________________________________________________________________
290 // XLayoutConstrains
291 //____________________________________________________________________________________________________________
293 Size SAL_CALL StatusIndicator::getPreferredSize () throw( RuntimeException )
295 // Ready for multithreading
296 ClearableMutexGuard aGuard ( m_aMutex ) ;
298 // get information about required place of child controls
299 Reference< XLayoutConstrains > xTextLayout ( m_xText, UNO_QUERY );
300 Size aTextSize = xTextLayout->getPreferredSize();
302 aGuard.clear () ;
304 // calc preferred size of status indicator
305 sal_Int32 nWidth = impl_getWidth() ;
306 sal_Int32 nHeight = (2*STATUSINDICATOR_FREEBORDER)+aTextSize.Height ;
308 // norm to minimum
309 if ( nWidth<STATUSINDICATOR_DEFAULT_WIDTH )
311 nWidth = STATUSINDICATOR_DEFAULT_WIDTH ;
313 if ( nHeight<STATUSINDICATOR_DEFAULT_HEIGHT )
315 nHeight = STATUSINDICATOR_DEFAULT_HEIGHT ;
318 // return to caller
319 return Size ( nWidth, nHeight ) ;
322 //____________________________________________________________________________________________________________
323 // XLayoutConstrains
324 //____________________________________________________________________________________________________________
326 Size SAL_CALL StatusIndicator::calcAdjustedSize ( const Size& /*rNewSize*/ ) throw( RuntimeException )
328 return getPreferredSize () ;
331 //____________________________________________________________________________________________________________
332 // XControl
333 //____________________________________________________________________________________________________________
335 void SAL_CALL StatusIndicator::createPeer (
336 const Reference< XToolkit > & rToolkit,
337 const Reference< XWindowPeer > & rParent
338 ) throw( RuntimeException )
340 if( getPeer().is() == sal_False )
342 BaseContainerControl::createPeer( rToolkit, rParent );
344 // If user forget to call "setPosSize()", we have still a correct size.
345 // And a "MinimumSize" IS A "MinimumSize"!
346 // We change not the position of control at this point.
347 Size aDefaultSize = getMinimumSize () ;
348 setPosSize ( 0, 0, aDefaultSize.Width, aDefaultSize.Height, PosSize::SIZE ) ;
352 //____________________________________________________________________________________________________________
353 // XControl
354 //____________________________________________________________________________________________________________
356 sal_Bool SAL_CALL StatusIndicator::setModel ( const Reference< XControlModel > & /*rModel*/ ) throw( RuntimeException )
358 // We have no model.
359 return sal_False ;
362 //____________________________________________________________________________________________________________
363 // XControl
364 //____________________________________________________________________________________________________________
366 Reference< XControlModel > SAL_CALL StatusIndicator::getModel () throw( RuntimeException )
368 // We have no model.
369 // return (XControlModel*)this ;
370 return Reference< XControlModel > () ;
373 //____________________________________________________________________________________________________________
374 // XComponent
375 //____________________________________________________________________________________________________________
377 void SAL_CALL StatusIndicator::dispose () throw( RuntimeException )
379 // Ready for multithreading
380 MutexGuard aGuard ( m_aMutex ) ;
382 // "removeControl()" control the state of a reference
383 Reference< XControl > xTextControl ( m_xText , UNO_QUERY );
384 Reference< XControl > xProgressControl ( m_xProgressBar, UNO_QUERY );
386 removeControl( xTextControl );
387 removeControl( xProgressControl );
389 // do'nt use "...->clear ()" or "... = XFixedText ()"
390 // when other hold a reference at this object !!!
391 xTextControl->dispose();
392 xProgressControl->dispose();
393 BaseContainerControl::dispose();
396 //____________________________________________________________________________________________________________
397 // XWindow
398 //____________________________________________________________________________________________________________
400 void SAL_CALL StatusIndicator::setPosSize (
401 sal_Int32 nX,
402 sal_Int32 nY,
403 sal_Int32 nWidth,
404 sal_Int32 nHeight,
405 sal_Int16 nFlags
406 ) throw( RuntimeException )
408 Rectangle aBasePosSize = getPosSize () ;
409 BaseContainerControl::setPosSize (nX, nY, nWidth, nHeight, nFlags) ;
411 // if position or size changed
412 if (
413 ( nWidth != aBasePosSize.Width ) ||
414 ( nHeight != aBasePosSize.Height)
417 // calc new layout for controls
418 impl_recalcLayout ( WindowEvent(static_cast< OWeakObject* >(this),0,0,nWidth,nHeight,0,0,0,0) ) ;
419 // clear background (!)
420 // [Children were repainted in "recalcLayout" by setPosSize() automaticly!]
421 getPeer()->invalidate(2);
422 // and repaint the control
423 impl_paint ( 0, 0, impl_getGraphicsPeer() ) ;
427 //____________________________________________________________________________________________________________
428 // impl but public method to register service
429 //____________________________________________________________________________________________________________
431 const Sequence< OUString > StatusIndicator::impl_getStaticSupportedServiceNames()
433 MutexGuard aGuard( Mutex::getGlobalMutex() );
434 Sequence< OUString > seqServiceNames( 1 );
435 seqServiceNames.getArray() [0] = SERVICENAME_STATUSINDICATOR;
436 return seqServiceNames ;
439 //____________________________________________________________________________________________________________
440 // impl but public method to register service
441 //____________________________________________________________________________________________________________
443 const OUString StatusIndicator::impl_getStaticImplementationName()
445 return OUString(IMPLEMENTATIONNAME_STATUSINDICATOR);
448 //____________________________________________________________________________________________________________
449 // protected method
450 //____________________________________________________________________________________________________________
452 WindowDescriptor* StatusIndicator::impl_getWindowDescriptor( const Reference< XWindowPeer >& xParentPeer )
454 // - used from "createPeer()" to set the values of an ::com::sun::star::awt::WindowDescriptor !!!
455 // - if you will change the descriptor-values, you must override this virtuell function
456 // - the caller must release the memory for this dynamical descriptor !!!
458 WindowDescriptor* pDescriptor = new WindowDescriptor ;
460 pDescriptor->Type = WindowClass_SIMPLE ;
461 pDescriptor->WindowServiceName = "floatingwindow" ;
462 pDescriptor->ParentIndex = -1 ;
463 pDescriptor->Parent = xParentPeer ;
464 pDescriptor->Bounds = getPosSize () ;
466 return pDescriptor ;
469 //____________________________________________________________________________________________________________
470 // protected method
471 //____________________________________________________________________________________________________________
473 void StatusIndicator::impl_paint ( sal_Int32 nX, sal_Int32 nY, const Reference< XGraphics > & rGraphics )
475 // This paint method ist not buffered !!
476 // Every request paint the completely control. ( but only, if peer exist )
477 if ( rGraphics.is () )
479 MutexGuard aGuard (m_aMutex) ;
481 // background = gray
482 Reference< XWindowPeer > xPeer( impl_getPeerWindow(), UNO_QUERY );
483 if( xPeer.is() == sal_True )
484 xPeer->setBackground( STATUSINDICATOR_BACKGROUNDCOLOR );
486 // FixedText background = gray
487 Reference< XControl > xTextControl( m_xText, UNO_QUERY );
488 xPeer = xTextControl->getPeer();
489 if( xPeer.is() == sal_True )
490 xPeer->setBackground( STATUSINDICATOR_BACKGROUNDCOLOR );
492 // Progress background = gray
493 xPeer = Reference< XWindowPeer >( m_xProgressBar, UNO_QUERY );
494 if( xPeer.is() == sal_True )
495 xPeer->setBackground( STATUSINDICATOR_BACKGROUNDCOLOR );
497 // paint shadow border
498 rGraphics->setLineColor ( STATUSINDICATOR_LINECOLOR_BRIGHT );
499 rGraphics->drawLine ( nX, nY, impl_getWidth(), nY );
500 rGraphics->drawLine ( nX, nY, nX , impl_getHeight() );
502 rGraphics->setLineColor ( STATUSINDICATOR_LINECOLOR_SHADOW );
503 rGraphics->drawLine ( impl_getWidth()-1, impl_getHeight()-1, impl_getWidth()-1, nY );
504 rGraphics->drawLine ( impl_getWidth()-1, impl_getHeight()-1, nX , impl_getHeight()-1 );
508 //____________________________________________________________________________________________________________
509 // protected method
510 //____________________________________________________________________________________________________________
512 void StatusIndicator::impl_recalcLayout ( const WindowEvent& aEvent )
514 sal_Int32 nX_ProgressBar ;
515 sal_Int32 nY_ProgressBar ;
516 sal_Int32 nWidth_ProgressBar ;
517 sal_Int32 nHeight_ProgressBar ;
518 sal_Int32 nX_Text ;
519 sal_Int32 nY_Text ;
520 sal_Int32 nWidth_Text ;
521 sal_Int32 nHeight_Text ;
523 // Ready for multithreading
524 MutexGuard aGuard ( m_aMutex ) ;
526 // get information about required place of child controls
527 Size aWindowSize ( aEvent.Width, aEvent.Height );
528 Reference< XLayoutConstrains > xTextLayout ( m_xText, UNO_QUERY );
529 Size aTextSize = xTextLayout->getPreferredSize();
531 if( aWindowSize.Width < STATUSINDICATOR_DEFAULT_WIDTH )
533 aWindowSize.Width = STATUSINDICATOR_DEFAULT_WIDTH;
535 if( aWindowSize.Height < STATUSINDICATOR_DEFAULT_HEIGHT )
537 aWindowSize.Height = STATUSINDICATOR_DEFAULT_HEIGHT;
540 // calc position and size of child controls
541 nX_Text = STATUSINDICATOR_FREEBORDER ;
542 nY_Text = STATUSINDICATOR_FREEBORDER ;
543 nWidth_Text = aTextSize.Width ;
544 nHeight_Text = aTextSize.Height ;
546 nX_ProgressBar = nX_Text+nWidth_Text+STATUSINDICATOR_FREEBORDER ;
547 nY_ProgressBar = nY_Text ;
548 nWidth_ProgressBar = aWindowSize.Width-nWidth_Text-(3*STATUSINDICATOR_FREEBORDER) ;
549 nHeight_ProgressBar = nHeight_Text ;
551 // Set new position and size on all controls
552 Reference< XWindow > xTextWindow ( m_xText , UNO_QUERY );
553 Reference< XWindow > xProgressWindow ( m_xProgressBar, UNO_QUERY );
555 xTextWindow->setPosSize ( nX_Text , nY_Text , nWidth_Text , nHeight_Text , 15 ) ;
556 xProgressWindow->setPosSize ( nX_ProgressBar, nY_ProgressBar, nWidth_ProgressBar, nHeight_ProgressBar , 15 ) ;
559 } // namespace unocontrols
561 /* vim:set shiftwidth=4 softtabstop=4 expandtab: */