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 <uielement/progressbarwrapper.hxx>
21 
22 #include <helper/statusindicator.hxx>
23 #include <uielement/statusindicatorinterfacewrapper.hxx>
24 
25 #include <com/sun/star/ui/UIElementType.hpp>
26 #include <com/sun/star/lang/DisposedException.hpp>
27 
28 #include <vcl/status.hxx>
29 #include <vcl/svapp.hxx>
30 #include <toolkit/helper/vclunohelper.hxx>
31 
32 using namespace ::com::sun::star;
33 
34 namespace framework{
35 
ProgressBarWrapper()36 ProgressBarWrapper::ProgressBarWrapper() :
37 UIElementWrapperBase( css::ui::UIElementType::PROGRESSBAR )
38     ,   m_bOwnsInstance( false )
39     ,   m_nRange( 100 )
40     ,   m_nValue( 0 )
41 {
42 }
43 
~ProgressBarWrapper()44 ProgressBarWrapper::~ProgressBarWrapper()
45 {
46 }
47 
48 // public interfaces
setStatusBar(const uno::Reference<awt::XWindow> & rStatusBar,bool bOwnsInstance)49 void ProgressBarWrapper::setStatusBar( const uno::Reference< awt::XWindow >& rStatusBar, bool bOwnsInstance )
50 {
51     SolarMutexGuard g;
52 
53     if ( m_bDisposed )
54         return;
55 
56     if ( m_bOwnsInstance )
57     {
58         // dispose XWindow reference of our status bar
59         try
60         {
61             if ( m_xStatusBar.is() )
62                 m_xStatusBar->dispose();
63         }
64         catch ( const uno::Exception& )
65         {
66         }
67         m_xStatusBar.clear();
68     }
69 
70     m_bOwnsInstance = bOwnsInstance;
71     m_xStatusBar    = rStatusBar;
72 }
73 
getStatusBar() const74 uno::Reference< awt::XWindow > ProgressBarWrapper::getStatusBar() const
75 {
76     SolarMutexGuard g;
77 
78     if ( m_bDisposed )
79         return uno::Reference< awt::XWindow >();
80 
81     return m_xStatusBar;
82 }
83 
84 // wrapped methods of css::task::XStatusIndicator
start(const OUString & Text,::sal_Int32 Range)85 void ProgressBarWrapper::start( const OUString& Text, ::sal_Int32 Range )
86 {
87     uno::Reference< awt::XWindow > xWindow;
88     sal_Int32                      nValue( 0 );
89 
90     {
91         SolarMutexGuard g;
92 
93         if ( m_bDisposed )
94             return;
95 
96         xWindow  = m_xStatusBar;
97         m_nValue = 0;
98         m_nRange = Range;
99         nValue   = m_nValue;
100     }
101 
102     if ( xWindow.is() )
103     {
104         SolarMutexGuard aSolarMutexGuard;
105         VclPtr<vcl::Window> pWindow = VCLUnoHelper::GetWindow( xWindow );
106         if ( pWindow && pWindow->GetType() == WindowType::STATUSBAR )
107         {
108             StatusBar* pStatusBar = static_cast<StatusBar *>(pWindow.get());
109             if ( !pStatusBar->IsProgressMode() )
110                 pStatusBar->StartProgressMode( Text );
111             else
112             {
113                 pStatusBar->SetUpdateMode( false );
114                 pStatusBar->EndProgressMode();
115                 pStatusBar->StartProgressMode( Text );
116                 pStatusBar->SetProgressValue( sal_uInt16( nValue ));
117                 pStatusBar->SetUpdateMode( true );
118             }
119             pStatusBar->Show( true, ShowFlags::NoFocusChange | ShowFlags::NoActivate );
120         }
121     }
122 }
123 
end()124 void ProgressBarWrapper::end()
125 {
126     uno::Reference< awt::XWindow > xWindow;
127 
128     {
129         SolarMutexGuard g;
130 
131         if ( m_bDisposed )
132             return;
133 
134         xWindow  = m_xStatusBar;
135         m_nRange = 100;
136         m_nValue = 0;
137     }
138 
139     if ( xWindow.is() )
140     {
141         SolarMutexGuard aSolarMutexGuard;
142         VclPtr<vcl::Window> pWindow = VCLUnoHelper::GetWindow( xWindow );
143         if ( pWindow && pWindow->GetType() == WindowType::STATUSBAR )
144         {
145             StatusBar* pStatusBar = static_cast<StatusBar *>(pWindow.get());
146             if ( pStatusBar->IsProgressMode() )
147                 pStatusBar->EndProgressMode();
148         }
149     }
150 }
151 
setText(const OUString & Text)152 void ProgressBarWrapper::setText( const OUString& Text )
153 {
154     uno::Reference< awt::XWindow > xWindow;
155     sal_Int32 nValue( 0 );
156 
157     {
158         SolarMutexGuard g;
159 
160         if ( m_bDisposed )
161             return;
162 
163         xWindow  = m_xStatusBar;
164         m_aText  = Text;
165         nValue   = m_nValue;
166     }
167 
168     if ( xWindow.is() )
169     {
170         SolarMutexGuard aSolarMutexGuard;
171         VclPtr<vcl::Window> pWindow = VCLUnoHelper::GetWindow( xWindow );
172         if ( pWindow && pWindow->GetType() == WindowType::STATUSBAR )
173         {
174             StatusBar* pStatusBar = static_cast<StatusBar *>(pWindow.get());
175             if( pStatusBar->IsProgressMode() )
176             {
177                 pStatusBar->SetUpdateMode( false );
178                 pStatusBar->EndProgressMode();
179                 pStatusBar->StartProgressMode( Text );
180                 pStatusBar->SetProgressValue( sal_uInt16( nValue ));
181                 pStatusBar->SetUpdateMode( true );
182             }
183             else
184                 pStatusBar->SetText( Text );
185         }
186     }
187 }
188 
setValue(::sal_Int32 nValue)189 void ProgressBarWrapper::setValue( ::sal_Int32 nValue )
190 {
191     uno::Reference< awt::XWindow > xWindow;
192     OUString aText;
193     bool      bSetValue( false );
194 
195     {
196         SolarMutexGuard g;
197 
198         if ( m_bDisposed )
199             return;
200 
201         xWindow  = m_xStatusBar;
202 
203         double fVal( 0 );
204         if ( m_nRange > 0 )
205         {
206             fVal = ( double( nValue ) / double( m_nRange )) * 100;
207             fVal = std::max( double( 0 ), std::min( fVal, double( 100 )));
208         }
209 
210         if ( m_nValue != sal_Int32( fVal ))
211         {
212             m_nValue = sal_Int32( fVal );
213             bSetValue = true;
214         }
215 
216         nValue   = m_nValue;
217         aText    = m_aText;
218     }
219 
220     if ( xWindow.is() && bSetValue )
221     {
222         SolarMutexGuard aSolarMutexGuard;
223         VclPtr<vcl::Window> pWindow = VCLUnoHelper::GetWindow( xWindow );
224         if ( pWindow && pWindow->GetType() == WindowType::STATUSBAR )
225         {
226             StatusBar* pStatusBar = static_cast<StatusBar *>(pWindow.get());
227             if ( !pStatusBar->IsProgressMode() )
228                 pStatusBar->StartProgressMode( aText );
229             pStatusBar->SetProgressValue( sal_uInt16( nValue ));
230         }
231     }
232 }
233 
reset()234 void ProgressBarWrapper::reset()
235 {
236     setText( OUString() );
237     setValue( 0 );
238 }
239 
240 // XInitialization
initialize(const uno::Sequence<uno::Any> &)241 void SAL_CALL ProgressBarWrapper::initialize( const uno::Sequence< uno::Any >& )
242 {
243     // dummy - do nothing
244 }
245 
246 // XUpdatable
update()247 void SAL_CALL ProgressBarWrapper::update()
248 {
249     // dummy - do nothing
250 }
251 
252 // XComponent
dispose()253 void SAL_CALL ProgressBarWrapper::dispose()
254 {
255     uno::Reference< lang::XComponent > xThis(
256         static_cast< cppu::OWeakObject* >(this),
257         uno::UNO_QUERY );
258 
259     {
260         SolarMutexGuard g;
261 
262         if ( m_bDisposed )
263             return;
264     }
265 
266     {
267         lang::EventObject aEvent( xThis );
268         m_aListenerContainer.disposeAndClear( aEvent );
269 
270         SolarMutexGuard g;
271         if ( m_bOwnsInstance )
272         {
273             try
274             {
275                 if ( m_xStatusBar.is() )
276                     m_xStatusBar->dispose();
277             }
278             catch ( const lang::DisposedException& )
279             {
280             }
281         }
282 
283         m_xStatusBar.clear();
284         m_bDisposed = true;
285     }
286 }
287 
288 // XUIElement
getRealInterface()289 uno::Reference< uno::XInterface > SAL_CALL ProgressBarWrapper::getRealInterface()
290 {
291     SolarMutexGuard g;
292 
293     if ( m_bDisposed )
294         return uno::Reference< uno::XInterface >();
295     else
296     {
297         uno::Reference< uno::XInterface > xComp( m_xProgressBarIfacWrapper );
298         if ( !xComp.is() )
299         {
300             StatusIndicatorInterfaceWrapper* pWrapper =
301                 new StatusIndicatorInterfaceWrapper(
302                     uno::Reference< lang::XComponent >(
303                         static_cast< cppu::OWeakObject* >( this ),
304                         uno::UNO_QUERY ));
305             xComp.set(static_cast< cppu::OWeakObject* >( pWrapper ),
306                         uno::UNO_QUERY );
307             m_xProgressBarIfacWrapper = xComp;
308         }
309 
310         return xComp;
311     }
312 }
313 
314 }       //  namespace framework
315 
316 /* vim:set shiftwidth=4 softtabstop=4 expandtab: */
317