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