Line data Source code
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 "statusindicator.hxx"
21 :
22 : #include <com/sun/star/awt/InvalidateStyle.hpp>
23 : #include <com/sun/star/awt/WindowAttribute.hpp>
24 : #include <com/sun/star/uno/XComponentContext.hpp>
25 : #include <cppuhelper/typeprovider.hxx>
26 :
27 : #include "progressbar.hxx"
28 :
29 : using namespace ::cppu;
30 : using namespace ::osl;
31 : using namespace ::rtl;
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;
36 :
37 : namespace unocontrols{
38 :
39 : // construct/destruct
40 :
41 0 : StatusIndicator::StatusIndicator( const css::uno::Reference< XComponentContext >& rxContext )
42 0 : : BaseContainerControl ( rxContext )
43 : {
44 : // Its not allowed to work with member in this method (refcounter !!!)
45 : // But with a HACK (++refcount) its "OK" :-(
46 0 : ++m_refCount;
47 :
48 : // Create instances for fixedtext and progress ...
49 0 : m_xText = css::uno::Reference< XFixedText > ( rxContext->getServiceManager()->createInstanceWithContext( FIXEDTEXT_SERVICENAME, rxContext ), UNO_QUERY );
50 0 : m_xProgressBar = new ProgressBar(rxContext);
51 : // ... cast controls to css::uno::Reference< XControl > and set model ...
52 : // ( ProgressBar has no model !!! )
53 0 : css::uno::Reference< XControl > xTextControl ( m_xText , UNO_QUERY );
54 0 : xTextControl->setModel( css::uno::Reference< XControlModel >( rxContext->getServiceManager()->createInstanceWithContext( FIXEDTEXT_MODELNAME, rxContext ), UNO_QUERY ) );
55 : // ... and add controls to basecontainercontrol!
56 0 : addControl( CONTROLNAME_TEXT, xTextControl );
57 0 : addControl( CONTROLNAME_PROGRESSBAR, m_xProgressBar.get() );
58 : // FixedText make it automaticly visible by himself ... but not the progressbar !!!
59 : // it must be set explicitly
60 0 : m_xProgressBar->setVisible( sal_True );
61 : // Reset to defaults !!!
62 : // (progressbar take automaticly its own defaults)
63 0 : m_xText->setText( STATUSINDICATOR_DEFAULT_TEXT );
64 :
65 0 : --m_refCount;
66 0 : }
67 :
68 0 : StatusIndicator::~StatusIndicator() {}
69 :
70 : // XInterface
71 :
72 0 : Any SAL_CALL StatusIndicator::queryInterface( const Type& rType ) throw( RuntimeException, std::exception )
73 : {
74 : // Attention:
75 : // Don't use mutex or guard in this method!!! Is a method of XInterface.
76 0 : Any aReturn;
77 0 : css::uno::Reference< XInterface > xDel = BaseContainerControl::impl_getDelegator();
78 0 : if ( xDel.is() )
79 : {
80 : // If an delegator exist, forward question to his queryInterface.
81 : // Delegator will ask his own queryAggregation!
82 0 : aReturn = xDel->queryInterface( rType );
83 : }
84 : else
85 : {
86 : // If an delegator unknown, forward question to own queryAggregation.
87 0 : aReturn = queryAggregation( rType );
88 : }
89 :
90 0 : return aReturn;
91 : }
92 :
93 : // XInterface
94 :
95 0 : void SAL_CALL StatusIndicator::acquire() throw()
96 : {
97 : // Attention:
98 : // Don't use mutex or guard in this method!!! Is a method of XInterface.
99 :
100 : // Forward to baseclass
101 0 : BaseControl::acquire();
102 0 : }
103 :
104 : // XInterface
105 :
106 0 : void SAL_CALL StatusIndicator::release() throw()
107 : {
108 : // Attention:
109 : // Don't use mutex or guard in this method!!! Is a method of XInterface.
110 :
111 : // Forward to baseclass
112 0 : BaseControl::release();
113 0 : }
114 :
115 : // XTypeProvider
116 :
117 0 : Sequence< Type > SAL_CALL StatusIndicator::getTypes() throw( RuntimeException, std::exception )
118 : {
119 : // Optimize this method !
120 : // We initialize a static variable only one time. And we don't must use a mutex at every call!
121 : // For the first call; pTypeCollection is NULL - for the second call pTypeCollection is different from NULL!
122 : static OTypeCollection* pTypeCollection = NULL;
123 :
124 0 : if ( pTypeCollection == NULL )
125 : {
126 : // Ready for multithreading; get global mutex for first call of this method only! see before
127 0 : MutexGuard aGuard( Mutex::getGlobalMutex() );
128 :
129 : // Control these pointer again ... it can be, that another instance will be faster then these!
130 0 : if ( pTypeCollection == NULL )
131 : {
132 : // Create a static typecollection ...
133 0 : static OTypeCollection aTypeCollection ( ::getCppuType(( const css::uno::Reference< XLayoutConstrains >*)NULL ) ,
134 0 : ::getCppuType(( const css::uno::Reference< XStatusIndicator >*)NULL ) ,
135 : BaseContainerControl::getTypes()
136 0 : );
137 : // ... and set his address to static pointer!
138 0 : pTypeCollection = &aTypeCollection;
139 0 : }
140 : }
141 :
142 0 : return pTypeCollection->getTypes();
143 : }
144 :
145 : // XAggregation
146 :
147 0 : Any SAL_CALL StatusIndicator::queryAggregation( const Type& aType ) throw( RuntimeException, std::exception )
148 : {
149 : // Ask for my own supported interfaces ...
150 : // Attention: XTypeProvider and XInterface are supported by OComponentHelper!
151 : Any aReturn ( ::cppu::queryInterface( aType ,
152 : static_cast< XLayoutConstrains* > ( this ) ,
153 : static_cast< XStatusIndicator* > ( this )
154 : )
155 0 : );
156 :
157 : // If searched interface not supported by this class ...
158 0 : if ( !aReturn.hasValue() )
159 : {
160 : // ... ask baseclasses.
161 0 : aReturn = BaseControl::queryAggregation( aType );
162 : }
163 :
164 0 : return aReturn;
165 : }
166 :
167 : // XStatusIndicator
168 :
169 0 : void SAL_CALL StatusIndicator::start( const OUString& sText, sal_Int32 nRange ) throw( RuntimeException, std::exception )
170 : {
171 : // Ready for multithreading
172 0 : MutexGuard aGuard( m_aMutex );
173 :
174 : // Initialize status controls with given values.
175 0 : m_xText->setText( sText );
176 0 : m_xProgressBar->setRange( 0, nRange );
177 : // force repaint ... fixedtext has changed !
178 0 : impl_recalcLayout ( WindowEvent(static_cast< OWeakObject* >(this),0,0,impl_getWidth(),impl_getHeight(),0,0,0,0) );
179 0 : }
180 :
181 : // XStatusIndicator
182 :
183 0 : void SAL_CALL StatusIndicator::end() throw( RuntimeException, std::exception )
184 : {
185 : // Ready for multithreading
186 0 : MutexGuard aGuard( m_aMutex );
187 :
188 : // Clear values of status controls.
189 0 : m_xText->setText( OUString() );
190 0 : m_xProgressBar->setValue( 0 );
191 0 : setVisible( sal_False );
192 0 : }
193 :
194 : // XStatusIndicator
195 :
196 0 : void SAL_CALL StatusIndicator::setText( const OUString& sText ) throw( RuntimeException, std::exception )
197 : {
198 : // Ready for multithreading
199 0 : MutexGuard aGuard( m_aMutex );
200 :
201 : // Take text on right control
202 0 : m_xText->setText( sText );
203 0 : }
204 :
205 : // XStatusIndicator
206 :
207 0 : void SAL_CALL StatusIndicator::setValue( sal_Int32 nValue ) throw( RuntimeException, std::exception )
208 : {
209 : // Ready for multithreading
210 0 : MutexGuard aGuard( m_aMutex );
211 :
212 : // Take value on right control
213 0 : m_xProgressBar->setValue( nValue );
214 0 : }
215 :
216 : // XStatusIndicator
217 :
218 0 : void SAL_CALL StatusIndicator::reset() throw( RuntimeException, std::exception )
219 : {
220 : // Ready for multithreading
221 0 : MutexGuard aGuard( m_aMutex );
222 :
223 : // Clear values of status controls.
224 : // (Don't hide the window! User will reset current values ... but he will not finish using of indicator!)
225 0 : m_xText->setText( OUString() );
226 0 : m_xProgressBar->setValue( 0 );
227 0 : }
228 :
229 : // XLayoutConstrains
230 :
231 0 : Size SAL_CALL StatusIndicator::getMinimumSize () throw( RuntimeException, std::exception )
232 : {
233 0 : return Size (STATUSINDICATOR_DEFAULT_WIDTH, STATUSINDICATOR_DEFAULT_HEIGHT);
234 : }
235 :
236 : // XLayoutConstrains
237 :
238 0 : Size SAL_CALL StatusIndicator::getPreferredSize () throw( RuntimeException, std::exception )
239 : {
240 : // Ready for multithreading
241 0 : ClearableMutexGuard aGuard ( m_aMutex );
242 :
243 : // get information about required place of child controls
244 0 : css::uno::Reference< XLayoutConstrains > xTextLayout ( m_xText, UNO_QUERY );
245 0 : Size aTextSize = xTextLayout->getPreferredSize();
246 :
247 0 : aGuard.clear ();
248 :
249 : // calc preferred size of status indicator
250 0 : sal_Int32 nWidth = impl_getWidth();
251 0 : sal_Int32 nHeight = (2*STATUSINDICATOR_FREEBORDER)+aTextSize.Height;
252 :
253 : // norm to minimum
254 0 : if ( nWidth<STATUSINDICATOR_DEFAULT_WIDTH )
255 : {
256 0 : nWidth = STATUSINDICATOR_DEFAULT_WIDTH;
257 : }
258 0 : if ( nHeight<STATUSINDICATOR_DEFAULT_HEIGHT )
259 : {
260 0 : nHeight = STATUSINDICATOR_DEFAULT_HEIGHT;
261 : }
262 :
263 : // return to caller
264 0 : return Size ( nWidth, nHeight );
265 : }
266 :
267 : // XLayoutConstrains
268 :
269 0 : Size SAL_CALL StatusIndicator::calcAdjustedSize ( const Size& /*rNewSize*/ ) throw( RuntimeException, std::exception )
270 : {
271 0 : return getPreferredSize ();
272 : }
273 :
274 : // XControl
275 :
276 0 : void SAL_CALL StatusIndicator::createPeer (
277 : const css::uno::Reference< XToolkit > & rToolkit,
278 : const css::uno::Reference< XWindowPeer > & rParent
279 : ) throw( RuntimeException, std::exception )
280 : {
281 0 : if( !getPeer().is() )
282 : {
283 0 : BaseContainerControl::createPeer( rToolkit, rParent );
284 :
285 : // If user forget to call "setPosSize()", we have still a correct size.
286 : // And a "MinimumSize" IS A "MinimumSize"!
287 : // We change not the position of control at this point.
288 0 : Size aDefaultSize = getMinimumSize ();
289 0 : setPosSize ( 0, 0, aDefaultSize.Width, aDefaultSize.Height, PosSize::SIZE );
290 : }
291 0 : }
292 :
293 : // XControl
294 :
295 0 : sal_Bool SAL_CALL StatusIndicator::setModel ( const css::uno::Reference< XControlModel > & /*rModel*/ ) throw( RuntimeException, std::exception )
296 : {
297 : // We have no model.
298 0 : return sal_False;
299 : }
300 :
301 : // XControl
302 :
303 0 : css::uno::Reference< XControlModel > SAL_CALL StatusIndicator::getModel () throw( RuntimeException, std::exception )
304 : {
305 : // We have no model.
306 : // return (XControlModel*)this;
307 0 : return css::uno::Reference< XControlModel > ();
308 : }
309 :
310 : // XComponent
311 :
312 0 : void SAL_CALL StatusIndicator::dispose () throw( RuntimeException, std::exception )
313 : {
314 : // Ready for multithreading
315 0 : MutexGuard aGuard ( m_aMutex );
316 :
317 : // "removeControl()" control the state of a reference
318 0 : css::uno::Reference< XControl > xTextControl ( m_xText , UNO_QUERY );
319 :
320 0 : removeControl( xTextControl );
321 0 : removeControl( m_xProgressBar.get() );
322 :
323 : // do'nt use "...->clear ()" or "... = XFixedText ()"
324 : // when other hold a reference at this object !!!
325 0 : xTextControl->dispose();
326 0 : m_xProgressBar->dispose();
327 0 : BaseContainerControl::dispose();
328 0 : }
329 :
330 : // XWindow
331 :
332 0 : void SAL_CALL StatusIndicator::setPosSize (
333 : sal_Int32 nX,
334 : sal_Int32 nY,
335 : sal_Int32 nWidth,
336 : sal_Int32 nHeight,
337 : sal_Int16 nFlags
338 : ) throw( RuntimeException, std::exception )
339 : {
340 0 : Rectangle aBasePosSize = getPosSize ();
341 0 : BaseContainerControl::setPosSize (nX, nY, nWidth, nHeight, nFlags);
342 :
343 : // if position or size changed
344 0 : if (
345 0 : ( nWidth != aBasePosSize.Width ) ||
346 0 : ( nHeight != aBasePosSize.Height)
347 : )
348 : {
349 : // calc new layout for controls
350 0 : impl_recalcLayout ( WindowEvent(static_cast< OWeakObject* >(this),0,0,nWidth,nHeight,0,0,0,0) );
351 : // clear background (!)
352 : // [Children were repainted in "recalcLayout" by setPosSize() automaticly!]
353 0 : getPeer()->invalidate(2);
354 : // and repaint the control
355 0 : impl_paint ( 0, 0, impl_getGraphicsPeer() );
356 : }
357 0 : }
358 :
359 : // impl but public method to register service
360 :
361 0 : const Sequence< OUString > StatusIndicator::impl_getStaticSupportedServiceNames()
362 : {
363 0 : return css::uno::Sequence<OUString>();
364 : }
365 :
366 : // impl but public method to register service
367 :
368 0 : const OUString StatusIndicator::impl_getStaticImplementationName()
369 : {
370 0 : return OUString("stardiv.UnoControls.StatusIndicator");
371 : }
372 :
373 : // protected method
374 :
375 0 : WindowDescriptor* StatusIndicator::impl_getWindowDescriptor( const css::uno::Reference< XWindowPeer >& xParentPeer )
376 : {
377 : // - used from "createPeer()" to set the values of an ::com::sun::star::awt::WindowDescriptor !!!
378 : // - if you will change the descriptor-values, you must override this virtuell function
379 : // - the caller must release the memory for this dynamical descriptor !!!
380 :
381 0 : WindowDescriptor* pDescriptor = new WindowDescriptor;
382 :
383 0 : pDescriptor->Type = WindowClass_SIMPLE;
384 0 : pDescriptor->WindowServiceName = "floatingwindow";
385 0 : pDescriptor->ParentIndex = -1;
386 0 : pDescriptor->Parent = xParentPeer;
387 0 : pDescriptor->Bounds = getPosSize ();
388 :
389 0 : return pDescriptor;
390 : }
391 :
392 : // protected method
393 :
394 0 : void StatusIndicator::impl_paint ( sal_Int32 nX, sal_Int32 nY, const css::uno::Reference< XGraphics > & rGraphics )
395 : {
396 : // This paint method ist not buffered !!
397 : // Every request paint the completely control. ( but only, if peer exist )
398 0 : if ( rGraphics.is () )
399 : {
400 0 : MutexGuard aGuard (m_aMutex);
401 :
402 : // background = gray
403 0 : css::uno::Reference< XWindowPeer > xPeer( impl_getPeerWindow(), UNO_QUERY );
404 0 : if( xPeer.is() )
405 0 : xPeer->setBackground( STATUSINDICATOR_BACKGROUNDCOLOR );
406 :
407 : // FixedText background = gray
408 0 : css::uno::Reference< XControl > xTextControl( m_xText, UNO_QUERY );
409 0 : xPeer = xTextControl->getPeer();
410 0 : if( xPeer.is() )
411 0 : xPeer->setBackground( STATUSINDICATOR_BACKGROUNDCOLOR );
412 :
413 : // Progress background = gray
414 0 : xPeer = m_xProgressBar->getPeer();
415 0 : if( xPeer.is() )
416 0 : xPeer->setBackground( STATUSINDICATOR_BACKGROUNDCOLOR );
417 :
418 : // paint shadow border
419 0 : rGraphics->setLineColor ( STATUSINDICATOR_LINECOLOR_BRIGHT );
420 0 : rGraphics->drawLine ( nX, nY, impl_getWidth(), nY );
421 0 : rGraphics->drawLine ( nX, nY, nX , impl_getHeight() );
422 :
423 0 : rGraphics->setLineColor ( STATUSINDICATOR_LINECOLOR_SHADOW );
424 0 : rGraphics->drawLine ( impl_getWidth()-1, impl_getHeight()-1, impl_getWidth()-1, nY );
425 0 : rGraphics->drawLine ( impl_getWidth()-1, impl_getHeight()-1, nX , impl_getHeight()-1 );
426 : }
427 0 : }
428 :
429 : // protected method
430 :
431 0 : void StatusIndicator::impl_recalcLayout ( const WindowEvent& aEvent )
432 : {
433 : sal_Int32 nX_ProgressBar;
434 : sal_Int32 nY_ProgressBar;
435 : sal_Int32 nWidth_ProgressBar;
436 : sal_Int32 nHeight_ProgressBar;
437 : sal_Int32 nX_Text;
438 : sal_Int32 nY_Text;
439 : sal_Int32 nWidth_Text;
440 : sal_Int32 nHeight_Text;
441 :
442 : // Ready for multithreading
443 0 : MutexGuard aGuard ( m_aMutex );
444 :
445 : // get information about required place of child controls
446 0 : Size aWindowSize ( aEvent.Width, aEvent.Height );
447 0 : css::uno::Reference< XLayoutConstrains > xTextLayout ( m_xText, UNO_QUERY );
448 0 : Size aTextSize = xTextLayout->getPreferredSize();
449 :
450 0 : if( aWindowSize.Width < STATUSINDICATOR_DEFAULT_WIDTH )
451 : {
452 0 : aWindowSize.Width = STATUSINDICATOR_DEFAULT_WIDTH;
453 : }
454 0 : if( aWindowSize.Height < STATUSINDICATOR_DEFAULT_HEIGHT )
455 : {
456 0 : aWindowSize.Height = STATUSINDICATOR_DEFAULT_HEIGHT;
457 : }
458 :
459 : // calc position and size of child controls
460 0 : nX_Text = STATUSINDICATOR_FREEBORDER;
461 0 : nY_Text = STATUSINDICATOR_FREEBORDER;
462 0 : nWidth_Text = aTextSize.Width;
463 0 : nHeight_Text = aTextSize.Height;
464 :
465 0 : nX_ProgressBar = nX_Text+nWidth_Text+STATUSINDICATOR_FREEBORDER;
466 0 : nY_ProgressBar = nY_Text;
467 0 : nWidth_ProgressBar = aWindowSize.Width-nWidth_Text-(3*STATUSINDICATOR_FREEBORDER);
468 0 : nHeight_ProgressBar = nHeight_Text;
469 :
470 : // Set new position and size on all controls
471 0 : css::uno::Reference< XWindow > xTextWindow ( m_xText , UNO_QUERY );
472 :
473 0 : xTextWindow->setPosSize ( nX_Text , nY_Text , nWidth_Text , nHeight_Text , 15 );
474 0 : m_xProgressBar->setPosSize( nX_ProgressBar, nY_ProgressBar, nWidth_ProgressBar, nHeight_ProgressBar, 15 );
475 0 : }
476 :
477 : } // namespace unocontrols
478 :
479 : /* vim:set shiftwidth=4 softtabstop=4 expandtab: */
|