Avoid potential negative array index access to cached text.
[LibreOffice.git] / framework / source / uielement / progressbarwrapper.cxx
blobad147111ff50816651e5f1fd53e28a175bc66fb7
1 /* -*- Mode: C++; tab-width: 4; indent-tabs-mode: nil; c-basic-offset: 4 -*- */
2 /*
3 * This file is part of the LibreOffice project.
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/.
9 * This file incorporates work covered by the following license notice:
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 .
20 #include <uielement/progressbarwrapper.hxx>
22 #include <uielement/statusindicatorinterfacewrapper.hxx>
24 #include <com/sun/star/ui/UIElementType.hpp>
25 #include <com/sun/star/lang/DisposedException.hpp>
27 #include <vcl/status.hxx>
28 #include <vcl/svapp.hxx>
29 #include <toolkit/helper/vclunohelper.hxx>
31 using namespace ::com::sun::star;
33 namespace framework{
35 ProgressBarWrapper::ProgressBarWrapper() :
36 UIElementWrapperBase( css::ui::UIElementType::PROGRESSBAR )
37 , m_bOwnsInstance( false )
38 , m_nRange( 100 )
39 , m_nValue( 0 )
43 ProgressBarWrapper::~ProgressBarWrapper()
47 // public interfaces
48 void ProgressBarWrapper::setStatusBar( const uno::Reference< awt::XWindow >& rStatusBar, bool bOwnsInstance )
50 SolarMutexGuard g;
52 if ( m_bDisposed )
53 return;
55 if ( m_bOwnsInstance )
57 // dispose XWindow reference of our status bar
58 try
60 if ( m_xStatusBar.is() )
61 m_xStatusBar->dispose();
63 catch ( const uno::Exception& )
66 m_xStatusBar.clear();
69 m_bOwnsInstance = bOwnsInstance;
70 m_xStatusBar = rStatusBar;
73 uno::Reference< awt::XWindow > ProgressBarWrapper::getStatusBar() const
75 SolarMutexGuard g;
77 if ( m_bDisposed )
78 return uno::Reference< awt::XWindow >();
80 return m_xStatusBar;
83 // wrapped methods of css::task::XStatusIndicator
84 void ProgressBarWrapper::start( const OUString& Text, ::sal_Int32 Range )
86 uno::Reference< awt::XWindow > xWindow;
87 sal_Int32 nValue( 0 );
90 SolarMutexGuard g;
92 if ( m_bDisposed )
93 return;
95 xWindow = m_xStatusBar;
96 m_nValue = 0;
97 m_nRange = Range;
98 nValue = m_nValue;
101 if ( !xWindow.is() )
102 return;
104 SolarMutexGuard aSolarMutexGuard;
105 VclPtr<vcl::Window> pWindow = VCLUnoHelper::GetWindow( xWindow );
106 if ( !(pWindow && pWindow->GetType() == WindowType::STATUSBAR) )
107 return;
109 StatusBar* pStatusBar = static_cast<StatusBar *>(pWindow.get());
110 if ( !pStatusBar->IsProgressMode() )
111 pStatusBar->StartProgressMode( Text );
112 else
114 pStatusBar->SetUpdateMode( false );
115 pStatusBar->EndProgressMode();
116 pStatusBar->StartProgressMode( Text );
117 pStatusBar->SetProgressValue( sal_uInt16( nValue ));
118 pStatusBar->SetUpdateMode( true );
120 pStatusBar->Show( true, ShowFlags::NoFocusChange | ShowFlags::NoActivate );
123 void ProgressBarWrapper::end()
125 uno::Reference< awt::XWindow > xWindow;
128 SolarMutexGuard g;
130 if ( m_bDisposed )
131 return;
133 xWindow = m_xStatusBar;
134 m_nRange = 100;
135 m_nValue = 0;
138 if ( xWindow.is() )
140 SolarMutexGuard aSolarMutexGuard;
141 VclPtr<vcl::Window> pWindow = VCLUnoHelper::GetWindow( xWindow );
142 if ( pWindow && pWindow->GetType() == WindowType::STATUSBAR )
144 StatusBar* pStatusBar = static_cast<StatusBar *>(pWindow.get());
145 if ( pStatusBar->IsProgressMode() )
146 pStatusBar->EndProgressMode();
151 void ProgressBarWrapper::setText( const OUString& Text )
153 uno::Reference< awt::XWindow > xWindow;
154 sal_Int32 nValue( 0 );
157 SolarMutexGuard g;
159 if ( m_bDisposed )
160 return;
162 xWindow = m_xStatusBar;
163 m_aText = Text;
164 nValue = m_nValue;
167 if ( !xWindow.is() )
168 return;
170 SolarMutexGuard aSolarMutexGuard;
171 VclPtr<vcl::Window> pWindow = VCLUnoHelper::GetWindow( xWindow );
172 if ( !(pWindow && pWindow->GetType() == WindowType::STATUSBAR) )
173 return;
175 StatusBar* pStatusBar = static_cast<StatusBar *>(pWindow.get());
176 if( pStatusBar->IsProgressMode() )
178 pStatusBar->SetUpdateMode( false );
179 pStatusBar->EndProgressMode();
180 pStatusBar->StartProgressMode( Text );
181 pStatusBar->SetProgressValue( sal_uInt16( nValue ));
182 pStatusBar->SetUpdateMode( true );
184 else
185 pStatusBar->SetText( Text );
188 void ProgressBarWrapper::setValue( ::sal_Int32 nValue )
190 uno::Reference< awt::XWindow > xWindow;
191 OUString aText;
192 bool bSetValue( false );
195 SolarMutexGuard g;
197 if ( m_bDisposed )
198 return;
200 xWindow = m_xStatusBar;
202 double fVal( 0 );
203 if ( m_nRange > 0 )
205 fVal = ( double( nValue ) / double( m_nRange )) * 100;
206 fVal = std::clamp( fVal, 0.0, 100.0 );
209 if ( m_nValue != sal_Int32( fVal ))
211 m_nValue = sal_Int32( fVal );
212 bSetValue = true;
215 nValue = m_nValue;
216 aText = m_aText;
219 if ( xWindow.is() && bSetValue )
221 SolarMutexGuard aSolarMutexGuard;
222 VclPtr<vcl::Window> pWindow = VCLUnoHelper::GetWindow( xWindow );
223 if ( pWindow && pWindow->GetType() == WindowType::STATUSBAR )
225 StatusBar* pStatusBar = static_cast<StatusBar *>(pWindow.get());
226 if ( !pStatusBar->IsProgressMode() )
227 pStatusBar->StartProgressMode( aText );
228 pStatusBar->SetProgressValue( sal_uInt16( nValue ));
233 void ProgressBarWrapper::reset()
235 setText( OUString() );
236 setValue( 0 );
239 // XInitialization
240 void SAL_CALL ProgressBarWrapper::initialize( const uno::Sequence< uno::Any >& )
242 // dummy - do nothing
245 // XUpdatable
246 void SAL_CALL ProgressBarWrapper::update()
248 // dummy - do nothing
251 // XComponent
252 void SAL_CALL ProgressBarWrapper::dispose()
254 uno::Reference< lang::XComponent > xThis(this);
257 SolarMutexGuard g;
259 if ( m_bDisposed )
260 return;
264 lang::EventObject aEvent( xThis );
265 m_aListenerContainer.disposeAndClear( aEvent );
267 SolarMutexGuard g;
268 if ( m_bOwnsInstance )
272 if ( m_xStatusBar.is() )
273 m_xStatusBar->dispose();
275 catch ( const lang::DisposedException& )
280 m_xStatusBar.clear();
281 m_bDisposed = true;
285 // XUIElement
286 uno::Reference< uno::XInterface > SAL_CALL ProgressBarWrapper::getRealInterface()
288 SolarMutexGuard g;
290 if ( m_bDisposed )
291 return uno::Reference< uno::XInterface >();
292 else
294 uno::Reference< uno::XInterface > xComp( m_xProgressBarIfacWrapper );
295 if ( !xComp.is() )
297 rtl::Reference<StatusIndicatorInterfaceWrapper> pWrapper =
298 new StatusIndicatorInterfaceWrapper( uno::Reference< lang::XComponent >(this) );
299 xComp.set(static_cast< cppu::OWeakObject* >( pWrapper.get() ),
300 uno::UNO_QUERY );
301 m_xProgressBarIfacWrapper = xComp;
304 return xComp;
308 } // namespace framework
310 /* vim:set shiftwidth=4 softtabstop=4 expandtab: */