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