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