xref: /AOO41X/main/framework/source/uielement/progressbarwrapper.cxx (revision cdf0e10c4e3984b49a9502b011690b615761d4a3)
1*cdf0e10cSrcweir /*************************************************************************
2*cdf0e10cSrcweir  *
3*cdf0e10cSrcweir  * DO NOT ALTER OR REMOVE COPYRIGHT NOTICES OR THIS FILE HEADER.
4*cdf0e10cSrcweir  *
5*cdf0e10cSrcweir  * Copyright 2000, 2010 Oracle and/or its affiliates.
6*cdf0e10cSrcweir  *
7*cdf0e10cSrcweir  * OpenOffice.org - a multi-platform office productivity suite
8*cdf0e10cSrcweir  *
9*cdf0e10cSrcweir  * This file is part of OpenOffice.org.
10*cdf0e10cSrcweir  *
11*cdf0e10cSrcweir  * OpenOffice.org is free software: you can redistribute it and/or modify
12*cdf0e10cSrcweir  * it under the terms of the GNU Lesser General Public License version 3
13*cdf0e10cSrcweir  * only, as published by the Free Software Foundation.
14*cdf0e10cSrcweir  *
15*cdf0e10cSrcweir  * OpenOffice.org is distributed in the hope that it will be useful,
16*cdf0e10cSrcweir  * but WITHOUT ANY WARRANTY; without even the implied warranty of
17*cdf0e10cSrcweir  * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the
18*cdf0e10cSrcweir  * GNU Lesser General Public License version 3 for more details
19*cdf0e10cSrcweir  * (a copy is included in the LICENSE file that accompanied this code).
20*cdf0e10cSrcweir  *
21*cdf0e10cSrcweir  * You should have received a copy of the GNU Lesser General Public License
22*cdf0e10cSrcweir  * version 3 along with OpenOffice.org.  If not, see
23*cdf0e10cSrcweir  * <http://www.openoffice.org/license.html>
24*cdf0e10cSrcweir  * for a copy of the LGPLv3 License.
25*cdf0e10cSrcweir  *
26*cdf0e10cSrcweir  ************************************************************************/
27*cdf0e10cSrcweir 
28*cdf0e10cSrcweir // MARKER(update_precomp.py): autogen include statement, do not remove
29*cdf0e10cSrcweir #include "precompiled_framework.hxx"
30*cdf0e10cSrcweir 
31*cdf0e10cSrcweir #ifndef __FRAMEWORK_UIELEMENT_PROGRESSBARWRAPPER_HXX_
32*cdf0e10cSrcweir #include <uielement/progressbarwrapper.hxx>
33*cdf0e10cSrcweir #endif
34*cdf0e10cSrcweir 
35*cdf0e10cSrcweir //_________________________________________________________________________________________________________________
36*cdf0e10cSrcweir //	my own includes
37*cdf0e10cSrcweir //_________________________________________________________________________________________________________________
38*cdf0e10cSrcweir #include <helper/statusindicator.hxx>
39*cdf0e10cSrcweir #include <threadhelp/resetableguard.hxx>
40*cdf0e10cSrcweir #include <uielement/statusindicatorinterfacewrapper.hxx>
41*cdf0e10cSrcweir 
42*cdf0e10cSrcweir //_________________________________________________________________________________________________________________
43*cdf0e10cSrcweir //	interface includes
44*cdf0e10cSrcweir //_________________________________________________________________________________________________________________
45*cdf0e10cSrcweir #include <com/sun/star/ui/UIElementType.hpp>
46*cdf0e10cSrcweir #include <com/sun/star/lang/DisposedException.hpp>
47*cdf0e10cSrcweir 
48*cdf0e10cSrcweir //_________________________________________________________________________________________________________________
49*cdf0e10cSrcweir //	includes of other projects
50*cdf0e10cSrcweir //_________________________________________________________________________________________________________________
51*cdf0e10cSrcweir #include <vcl/svapp.hxx>
52*cdf0e10cSrcweir #include <toolkit/helper/vclunohelper.hxx>
53*cdf0e10cSrcweir 
54*cdf0e10cSrcweir //_________________________________________________________________________________________________________________
55*cdf0e10cSrcweir //	namespace
56*cdf0e10cSrcweir //_________________________________________________________________________________________________________________
57*cdf0e10cSrcweir using namespace ::com::sun::star;
58*cdf0e10cSrcweir 
59*cdf0e10cSrcweir namespace framework{
60*cdf0e10cSrcweir 
61*cdf0e10cSrcweir //_________________________________________________________________________________________________________________
62*cdf0e10cSrcweir //	non exported const
63*cdf0e10cSrcweir //_________________________________________________________________________________________________________________
64*cdf0e10cSrcweir 
65*cdf0e10cSrcweir //_________________________________________________________________________________________________________________
66*cdf0e10cSrcweir //	non exported definitions
67*cdf0e10cSrcweir //_________________________________________________________________________________________________________________
68*cdf0e10cSrcweir 
69*cdf0e10cSrcweir //_________________________________________________________________________________________________________________
70*cdf0e10cSrcweir //	declarations
71*cdf0e10cSrcweir //_________________________________________________________________________________________________________________
72*cdf0e10cSrcweir 
73*cdf0e10cSrcweir ProgressBarWrapper::ProgressBarWrapper() :
74*cdf0e10cSrcweir UIElementWrapperBase( ::com::sun::star::ui::UIElementType::PROGRESSBAR )
75*cdf0e10cSrcweir     ,   m_bOwnsInstance( sal_False )
76*cdf0e10cSrcweir     ,   m_nRange( 100 )
77*cdf0e10cSrcweir     ,   m_nValue( 0 )
78*cdf0e10cSrcweir {
79*cdf0e10cSrcweir }
80*cdf0e10cSrcweir 
81*cdf0e10cSrcweir ProgressBarWrapper::~ProgressBarWrapper()
82*cdf0e10cSrcweir {
83*cdf0e10cSrcweir }
84*cdf0e10cSrcweir 
85*cdf0e10cSrcweir // public interfaces
86*cdf0e10cSrcweir void ProgressBarWrapper::setStatusBar( const uno::Reference< awt::XWindow >& rStatusBar, sal_Bool bOwnsInstance )
87*cdf0e10cSrcweir {
88*cdf0e10cSrcweir     ResetableGuard aGuard( m_aLock );
89*cdf0e10cSrcweir 
90*cdf0e10cSrcweir     if ( m_bDisposed )
91*cdf0e10cSrcweir         return;
92*cdf0e10cSrcweir 
93*cdf0e10cSrcweir     if ( m_bOwnsInstance )
94*cdf0e10cSrcweir     {
95*cdf0e10cSrcweir         // dispose XWindow reference our our status bar
96*cdf0e10cSrcweir         uno::Reference< lang::XComponent > xComponent( m_xStatusBar, uno::UNO_QUERY );
97*cdf0e10cSrcweir         try
98*cdf0e10cSrcweir         {
99*cdf0e10cSrcweir             if ( xComponent.is() )
100*cdf0e10cSrcweir                 xComponent->dispose();
101*cdf0e10cSrcweir         }
102*cdf0e10cSrcweir         catch ( uno::Exception& )
103*cdf0e10cSrcweir         {
104*cdf0e10cSrcweir         }
105*cdf0e10cSrcweir         m_xStatusBar.clear();
106*cdf0e10cSrcweir     }
107*cdf0e10cSrcweir 
108*cdf0e10cSrcweir     m_bOwnsInstance = bOwnsInstance;
109*cdf0e10cSrcweir     m_xStatusBar    = rStatusBar;
110*cdf0e10cSrcweir }
111*cdf0e10cSrcweir 
112*cdf0e10cSrcweir uno::Reference< awt::XWindow > ProgressBarWrapper::getStatusBar() const
113*cdf0e10cSrcweir {
114*cdf0e10cSrcweir     ResetableGuard aGuard( m_aLock );
115*cdf0e10cSrcweir 
116*cdf0e10cSrcweir     if ( m_bDisposed )
117*cdf0e10cSrcweir         return uno::Reference< awt::XWindow >();
118*cdf0e10cSrcweir 
119*cdf0e10cSrcweir     return m_xStatusBar;
120*cdf0e10cSrcweir }
121*cdf0e10cSrcweir 
122*cdf0e10cSrcweir // wrapped methods of ::com::sun::star::task::XStatusIndicator
123*cdf0e10cSrcweir void ProgressBarWrapper::start( const ::rtl::OUString& Text, ::sal_Int32 Range )
124*cdf0e10cSrcweir throw (uno::RuntimeException)
125*cdf0e10cSrcweir {
126*cdf0e10cSrcweir     uno::Reference< awt::XWindow > xWindow;
127*cdf0e10cSrcweir     sal_Int32                      nValue( 0 );
128*cdf0e10cSrcweir 
129*cdf0e10cSrcweir     {
130*cdf0e10cSrcweir         ResetableGuard aGuard( m_aLock );
131*cdf0e10cSrcweir 
132*cdf0e10cSrcweir         if ( m_bDisposed )
133*cdf0e10cSrcweir             return;
134*cdf0e10cSrcweir 
135*cdf0e10cSrcweir         xWindow  = m_xStatusBar;
136*cdf0e10cSrcweir         m_nValue = 0;
137*cdf0e10cSrcweir         m_nRange = Range;
138*cdf0e10cSrcweir         nValue   = m_nValue;
139*cdf0e10cSrcweir     }
140*cdf0e10cSrcweir 
141*cdf0e10cSrcweir     if ( xWindow.is() )
142*cdf0e10cSrcweir     {
143*cdf0e10cSrcweir         vos::OGuard aSolarMutexGuard( Application::GetSolarMutex() );
144*cdf0e10cSrcweir         Window* pWindow = VCLUnoHelper::GetWindow( xWindow );
145*cdf0e10cSrcweir         if ( pWindow && pWindow->GetType() == WINDOW_STATUSBAR )
146*cdf0e10cSrcweir         {
147*cdf0e10cSrcweir             StatusBar* pStatusBar = (StatusBar *)pWindow;
148*cdf0e10cSrcweir             if ( !pStatusBar->IsProgressMode() )
149*cdf0e10cSrcweir                 pStatusBar->StartProgressMode( Text );
150*cdf0e10cSrcweir             else
151*cdf0e10cSrcweir             {
152*cdf0e10cSrcweir 			    pStatusBar->SetUpdateMode( sal_False );
153*cdf0e10cSrcweir 			    pStatusBar->EndProgressMode();
154*cdf0e10cSrcweir                 pStatusBar->StartProgressMode( Text );
155*cdf0e10cSrcweir 			    pStatusBar->SetProgressValue( sal_uInt16( nValue ));
156*cdf0e10cSrcweir                 pStatusBar->SetUpdateMode( sal_True );
157*cdf0e10cSrcweir             }
158*cdf0e10cSrcweir             pStatusBar->Show( sal_True, SHOW_NOFOCUSCHANGE | SHOW_NOACTIVATE );
159*cdf0e10cSrcweir         }
160*cdf0e10cSrcweir     }
161*cdf0e10cSrcweir }
162*cdf0e10cSrcweir 
163*cdf0e10cSrcweir void ProgressBarWrapper::end()
164*cdf0e10cSrcweir throw (uno::RuntimeException)
165*cdf0e10cSrcweir {
166*cdf0e10cSrcweir     uno::Reference< awt::XWindow > xWindow;
167*cdf0e10cSrcweir 
168*cdf0e10cSrcweir     {
169*cdf0e10cSrcweir         ResetableGuard aGuard( m_aLock );
170*cdf0e10cSrcweir 
171*cdf0e10cSrcweir         if ( m_bDisposed )
172*cdf0e10cSrcweir             return;
173*cdf0e10cSrcweir 
174*cdf0e10cSrcweir         xWindow  = m_xStatusBar;
175*cdf0e10cSrcweir         m_nRange = 100;
176*cdf0e10cSrcweir         m_nValue = 0;
177*cdf0e10cSrcweir     }
178*cdf0e10cSrcweir 
179*cdf0e10cSrcweir     if ( xWindow.is() )
180*cdf0e10cSrcweir     {
181*cdf0e10cSrcweir         vos::OGuard aSolarMutexGuard( Application::GetSolarMutex() );
182*cdf0e10cSrcweir         Window* pWindow = VCLUnoHelper::GetWindow( xWindow );
183*cdf0e10cSrcweir         if ( pWindow && pWindow->GetType() == WINDOW_STATUSBAR )
184*cdf0e10cSrcweir         {
185*cdf0e10cSrcweir             StatusBar* pStatusBar = (StatusBar *)pWindow;
186*cdf0e10cSrcweir             if ( pStatusBar->IsProgressMode() )
187*cdf0e10cSrcweir                 pStatusBar->EndProgressMode();
188*cdf0e10cSrcweir         }
189*cdf0e10cSrcweir     }
190*cdf0e10cSrcweir }
191*cdf0e10cSrcweir 
192*cdf0e10cSrcweir void ProgressBarWrapper::setText( const ::rtl::OUString& Text )
193*cdf0e10cSrcweir throw (uno::RuntimeException)
194*cdf0e10cSrcweir {
195*cdf0e10cSrcweir     uno::Reference< awt::XWindow > xWindow;
196*cdf0e10cSrcweir     sal_Int32 nValue( 0 );
197*cdf0e10cSrcweir 
198*cdf0e10cSrcweir     {
199*cdf0e10cSrcweir         ResetableGuard aGuard( m_aLock );
200*cdf0e10cSrcweir 
201*cdf0e10cSrcweir         if ( m_bDisposed )
202*cdf0e10cSrcweir             return;
203*cdf0e10cSrcweir 
204*cdf0e10cSrcweir         xWindow  = m_xStatusBar;
205*cdf0e10cSrcweir         m_aText  = Text;
206*cdf0e10cSrcweir         nValue   = m_nValue;
207*cdf0e10cSrcweir     }
208*cdf0e10cSrcweir 
209*cdf0e10cSrcweir     if ( xWindow.is() )
210*cdf0e10cSrcweir     {
211*cdf0e10cSrcweir         vos::OGuard aSolarMutexGuard( Application::GetSolarMutex() );
212*cdf0e10cSrcweir         Window* pWindow = VCLUnoHelper::GetWindow( xWindow );
213*cdf0e10cSrcweir         if ( pWindow && pWindow->GetType() == WINDOW_STATUSBAR )
214*cdf0e10cSrcweir         {
215*cdf0e10cSrcweir             StatusBar* pStatusBar = (StatusBar *)pWindow;
216*cdf0e10cSrcweir 			if( pStatusBar->IsProgressMode() )
217*cdf0e10cSrcweir             {
218*cdf0e10cSrcweir 			    pStatusBar->SetUpdateMode( sal_False );
219*cdf0e10cSrcweir 			    pStatusBar->EndProgressMode();
220*cdf0e10cSrcweir                 pStatusBar->StartProgressMode( Text );
221*cdf0e10cSrcweir 			    pStatusBar->SetProgressValue( sal_uInt16( nValue ));
222*cdf0e10cSrcweir                 pStatusBar->SetUpdateMode( sal_True );
223*cdf0e10cSrcweir             }
224*cdf0e10cSrcweir             else
225*cdf0e10cSrcweir                 pStatusBar->SetText( Text );
226*cdf0e10cSrcweir         }
227*cdf0e10cSrcweir     }
228*cdf0e10cSrcweir }
229*cdf0e10cSrcweir 
230*cdf0e10cSrcweir void ProgressBarWrapper::setValue( ::sal_Int32 nValue )
231*cdf0e10cSrcweir throw (uno::RuntimeException)
232*cdf0e10cSrcweir {
233*cdf0e10cSrcweir     uno::Reference< awt::XWindow > xWindow;
234*cdf0e10cSrcweir     rtl::OUString aText;
235*cdf0e10cSrcweir     sal_Bool      bSetValue( sal_False );
236*cdf0e10cSrcweir 
237*cdf0e10cSrcweir     {
238*cdf0e10cSrcweir         ResetableGuard aGuard( m_aLock );
239*cdf0e10cSrcweir 
240*cdf0e10cSrcweir         if ( m_bDisposed )
241*cdf0e10cSrcweir             return;
242*cdf0e10cSrcweir 
243*cdf0e10cSrcweir         xWindow  = m_xStatusBar;
244*cdf0e10cSrcweir 
245*cdf0e10cSrcweir         double fVal( 0 );
246*cdf0e10cSrcweir         if ( m_nRange > 0 )
247*cdf0e10cSrcweir         {
248*cdf0e10cSrcweir             fVal = ( double( nValue ) / double( m_nRange )) * 100;
249*cdf0e10cSrcweir             fVal = std::max( double( 0 ), std::min( fVal, double( 100 )));
250*cdf0e10cSrcweir         }
251*cdf0e10cSrcweir 
252*cdf0e10cSrcweir         if ( m_nValue != sal_Int32( fVal ))
253*cdf0e10cSrcweir         {
254*cdf0e10cSrcweir             m_nValue = sal_Int32( fVal );
255*cdf0e10cSrcweir             bSetValue = sal_True;
256*cdf0e10cSrcweir         }
257*cdf0e10cSrcweir 
258*cdf0e10cSrcweir         nValue   = m_nValue;
259*cdf0e10cSrcweir         aText    = m_aText;
260*cdf0e10cSrcweir     }
261*cdf0e10cSrcweir 
262*cdf0e10cSrcweir     if ( xWindow.is() && bSetValue )
263*cdf0e10cSrcweir     {
264*cdf0e10cSrcweir         vos::OGuard aSolarMutexGuard( Application::GetSolarMutex() );
265*cdf0e10cSrcweir         Window* pWindow = VCLUnoHelper::GetWindow( xWindow );
266*cdf0e10cSrcweir         if ( pWindow && pWindow->GetType() == WINDOW_STATUSBAR )
267*cdf0e10cSrcweir         {
268*cdf0e10cSrcweir             StatusBar* pStatusBar = (StatusBar *)pWindow;
269*cdf0e10cSrcweir             if ( !pStatusBar->IsProgressMode() )
270*cdf0e10cSrcweir                 pStatusBar->StartProgressMode( aText );
271*cdf0e10cSrcweir             pStatusBar->SetProgressValue( sal_uInt16( nValue ));
272*cdf0e10cSrcweir         }
273*cdf0e10cSrcweir     }
274*cdf0e10cSrcweir }
275*cdf0e10cSrcweir 
276*cdf0e10cSrcweir void ProgressBarWrapper::reset()
277*cdf0e10cSrcweir throw (uno::RuntimeException)
278*cdf0e10cSrcweir {
279*cdf0e10cSrcweir     setText( rtl::OUString() );
280*cdf0e10cSrcweir     setValue( 0 );
281*cdf0e10cSrcweir }
282*cdf0e10cSrcweir 
283*cdf0e10cSrcweir // XInitialization
284*cdf0e10cSrcweir void SAL_CALL ProgressBarWrapper::initialize( const uno::Sequence< uno::Any >& )
285*cdf0e10cSrcweir throw (uno::Exception, uno::RuntimeException)
286*cdf0e10cSrcweir {
287*cdf0e10cSrcweir     // dummy - do nothing
288*cdf0e10cSrcweir }
289*cdf0e10cSrcweir 
290*cdf0e10cSrcweir // XUpdatable
291*cdf0e10cSrcweir void SAL_CALL ProgressBarWrapper::update()
292*cdf0e10cSrcweir throw (uno::RuntimeException)
293*cdf0e10cSrcweir {
294*cdf0e10cSrcweir     // dummy - do nothing
295*cdf0e10cSrcweir }
296*cdf0e10cSrcweir 
297*cdf0e10cSrcweir // XComponent
298*cdf0e10cSrcweir void SAL_CALL ProgressBarWrapper::dispose()
299*cdf0e10cSrcweir throw (uno::RuntimeException)
300*cdf0e10cSrcweir {
301*cdf0e10cSrcweir     uno::Reference< lang::XComponent > xThis(
302*cdf0e10cSrcweir         static_cast< cppu::OWeakObject* >(this),
303*cdf0e10cSrcweir         uno::UNO_QUERY );
304*cdf0e10cSrcweir 
305*cdf0e10cSrcweir     {
306*cdf0e10cSrcweir         ResetableGuard aLock( m_aLock );
307*cdf0e10cSrcweir 
308*cdf0e10cSrcweir         if ( m_bDisposed )
309*cdf0e10cSrcweir             return;
310*cdf0e10cSrcweir     }
311*cdf0e10cSrcweir 
312*cdf0e10cSrcweir     {
313*cdf0e10cSrcweir         lang::EventObject aEvent( xThis );
314*cdf0e10cSrcweir         m_aListenerContainer.disposeAndClear( aEvent );
315*cdf0e10cSrcweir 
316*cdf0e10cSrcweir         ResetableGuard aLock( m_aLock );
317*cdf0e10cSrcweir         if ( m_bOwnsInstance )
318*cdf0e10cSrcweir         {
319*cdf0e10cSrcweir             try
320*cdf0e10cSrcweir             {
321*cdf0e10cSrcweir                 uno::Reference< lang::XComponent > xComponent( m_xStatusBar, uno::UNO_QUERY );
322*cdf0e10cSrcweir                 if ( xComponent.is() )
323*cdf0e10cSrcweir                     xComponent->dispose();
324*cdf0e10cSrcweir             }
325*cdf0e10cSrcweir             catch ( lang::DisposedException& )
326*cdf0e10cSrcweir             {
327*cdf0e10cSrcweir             }
328*cdf0e10cSrcweir         }
329*cdf0e10cSrcweir 
330*cdf0e10cSrcweir         m_xStatusBar.clear();
331*cdf0e10cSrcweir         m_bDisposed = sal_True;
332*cdf0e10cSrcweir     }
333*cdf0e10cSrcweir }
334*cdf0e10cSrcweir 
335*cdf0e10cSrcweir // XUIElement
336*cdf0e10cSrcweir uno::Reference< uno::XInterface > SAL_CALL ProgressBarWrapper::getRealInterface()
337*cdf0e10cSrcweir throw (uno::RuntimeException)
338*cdf0e10cSrcweir {
339*cdf0e10cSrcweir     /* SAFE AREA ----------------------------------------------------------------------------------------------- */
340*cdf0e10cSrcweir     // Ready for multithreading
341*cdf0e10cSrcweir     ResetableGuard aLock( m_aLock );
342*cdf0e10cSrcweir 
343*cdf0e10cSrcweir     if ( m_bDisposed )
344*cdf0e10cSrcweir         return uno::Reference< uno::XInterface >();
345*cdf0e10cSrcweir     else
346*cdf0e10cSrcweir     {
347*cdf0e10cSrcweir         uno::Reference< uno::XInterface > xComp( m_xProgressBarIfacWrapper );
348*cdf0e10cSrcweir         if ( !xComp.is() )
349*cdf0e10cSrcweir         {
350*cdf0e10cSrcweir             StatusIndicatorInterfaceWrapper* pWrapper =
351*cdf0e10cSrcweir                 new StatusIndicatorInterfaceWrapper(
352*cdf0e10cSrcweir                     uno::Reference< lang::XComponent >(
353*cdf0e10cSrcweir                         static_cast< cppu::OWeakObject* >( this ),
354*cdf0e10cSrcweir                         uno::UNO_QUERY ));
355*cdf0e10cSrcweir                 xComp = uno::Reference< uno::XInterface >(
356*cdf0e10cSrcweir                     static_cast< cppu::OWeakObject* >( pWrapper ),
357*cdf0e10cSrcweir                     uno::UNO_QUERY );
358*cdf0e10cSrcweir              m_xProgressBarIfacWrapper = xComp;
359*cdf0e10cSrcweir         }
360*cdf0e10cSrcweir 
361*cdf0e10cSrcweir         return xComp;
362*cdf0e10cSrcweir     }
363*cdf0e10cSrcweir }
364*cdf0e10cSrcweir 
365*cdf0e10cSrcweir }		//	namespace framework
366