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 240 : ProgressBarWrapper::ProgressBarWrapper() :
37 : UIElementWrapperBase( ::com::sun::star::ui::UIElementType::PROGRESSBAR )
38 : , m_bOwnsInstance( sal_False )
39 : , m_nRange( 100 )
40 240 : , m_nValue( 0 )
41 : {
42 240 : }
43 :
44 126 : ProgressBarWrapper::~ProgressBarWrapper()
45 : {
46 126 : }
47 :
48 : // public interfaces
49 899 : void ProgressBarWrapper::setStatusBar( const uno::Reference< awt::XWindow >& rStatusBar, sal_Bool bOwnsInstance )
50 : {
51 899 : ResetableGuard aGuard( m_aLock );
52 :
53 899 : if ( m_bDisposed )
54 899 : return;
55 :
56 899 : if ( m_bOwnsInstance )
57 : {
58 : // dispose XWindow reference our our status bar
59 236 : uno::Reference< lang::XComponent > xComponent( m_xStatusBar, uno::UNO_QUERY );
60 : try
61 : {
62 236 : if ( xComponent.is() )
63 236 : xComponent->dispose();
64 : }
65 0 : catch ( const uno::Exception& )
66 : {
67 : }
68 236 : m_xStatusBar.clear();
69 : }
70 :
71 899 : m_bOwnsInstance = bOwnsInstance;
72 899 : m_xStatusBar = rStatusBar;
73 : }
74 :
75 1776 : uno::Reference< awt::XWindow > ProgressBarWrapper::getStatusBar() const
76 : {
77 1776 : ResetableGuard aGuard( m_aLock );
78 :
79 1776 : if ( m_bDisposed )
80 0 : return uno::Reference< awt::XWindow >();
81 :
82 1776 : return m_xStatusBar;
83 : }
84 :
85 : // wrapped methods of ::com::sun::star::task::XStatusIndicator
86 153 : void ProgressBarWrapper::start( const ::rtl::OUString& Text, ::sal_Int32 Range )
87 : throw (uno::RuntimeException)
88 : {
89 153 : uno::Reference< awt::XWindow > xWindow;
90 153 : sal_Int32 nValue( 0 );
91 :
92 : {
93 153 : ResetableGuard aGuard( m_aLock );
94 :
95 153 : if ( m_bDisposed )
96 153 : return;
97 :
98 153 : xWindow = m_xStatusBar;
99 153 : m_nValue = 0;
100 153 : m_nRange = Range;
101 153 : nValue = m_nValue;
102 : }
103 :
104 153 : if ( xWindow.is() )
105 : {
106 153 : SolarMutexGuard aSolarMutexGuard;
107 153 : Window* pWindow = VCLUnoHelper::GetWindow( xWindow );
108 153 : if ( pWindow && pWindow->GetType() == WINDOW_STATUSBAR )
109 : {
110 153 : StatusBar* pStatusBar = (StatusBar *)pWindow;
111 153 : if ( !pStatusBar->IsProgressMode() )
112 144 : pStatusBar->StartProgressMode( Text );
113 : else
114 : {
115 9 : pStatusBar->SetUpdateMode( sal_False );
116 9 : pStatusBar->EndProgressMode();
117 9 : pStatusBar->StartProgressMode( Text );
118 9 : pStatusBar->SetProgressValue( sal_uInt16( nValue ));
119 9 : pStatusBar->SetUpdateMode( sal_True );
120 : }
121 153 : pStatusBar->Show( sal_True, SHOW_NOFOCUSCHANGE | SHOW_NOACTIVATE );
122 153 : }
123 153 : }
124 : }
125 :
126 144 : void ProgressBarWrapper::end()
127 : throw (uno::RuntimeException)
128 : {
129 144 : uno::Reference< awt::XWindow > xWindow;
130 :
131 : {
132 144 : ResetableGuard aGuard( m_aLock );
133 :
134 144 : if ( m_bDisposed )
135 144 : return;
136 :
137 144 : xWindow = m_xStatusBar;
138 144 : m_nRange = 100;
139 144 : m_nValue = 0;
140 : }
141 :
142 144 : if ( xWindow.is() )
143 : {
144 144 : SolarMutexGuard aSolarMutexGuard;
145 144 : Window* pWindow = VCLUnoHelper::GetWindow( xWindow );
146 144 : if ( pWindow && pWindow->GetType() == WINDOW_STATUSBAR )
147 : {
148 144 : StatusBar* pStatusBar = (StatusBar *)pWindow;
149 144 : if ( pStatusBar->IsProgressMode() )
150 144 : pStatusBar->EndProgressMode();
151 144 : }
152 144 : }
153 : }
154 :
155 20 : void ProgressBarWrapper::setText( const ::rtl::OUString& Text )
156 : throw (uno::RuntimeException)
157 : {
158 20 : uno::Reference< awt::XWindow > xWindow;
159 20 : sal_Int32 nValue( 0 );
160 :
161 : {
162 20 : ResetableGuard aGuard( m_aLock );
163 :
164 20 : if ( m_bDisposed )
165 20 : return;
166 :
167 20 : xWindow = m_xStatusBar;
168 20 : m_aText = Text;
169 20 : nValue = m_nValue;
170 : }
171 :
172 20 : if ( xWindow.is() )
173 : {
174 20 : SolarMutexGuard aSolarMutexGuard;
175 20 : Window* pWindow = VCLUnoHelper::GetWindow( xWindow );
176 20 : if ( pWindow && pWindow->GetType() == WINDOW_STATUSBAR )
177 : {
178 20 : StatusBar* pStatusBar = (StatusBar *)pWindow;
179 20 : if( pStatusBar->IsProgressMode() )
180 : {
181 20 : pStatusBar->SetUpdateMode( sal_False );
182 20 : pStatusBar->EndProgressMode();
183 20 : pStatusBar->StartProgressMode( Text );
184 20 : pStatusBar->SetProgressValue( sal_uInt16( nValue ));
185 20 : pStatusBar->SetUpdateMode( sal_True );
186 : }
187 : else
188 0 : pStatusBar->SetText( Text );
189 20 : }
190 20 : }
191 : }
192 :
193 5176 : void ProgressBarWrapper::setValue( ::sal_Int32 nValue )
194 : throw (uno::RuntimeException)
195 : {
196 5176 : uno::Reference< awt::XWindow > xWindow;
197 5176 : rtl::OUString aText;
198 5176 : sal_Bool bSetValue( sal_False );
199 :
200 : {
201 5176 : ResetableGuard aGuard( m_aLock );
202 :
203 5176 : if ( m_bDisposed )
204 5176 : return;
205 :
206 5176 : xWindow = m_xStatusBar;
207 :
208 5176 : double fVal( 0 );
209 5176 : if ( m_nRange > 0 )
210 : {
211 5176 : fVal = ( double( nValue ) / double( m_nRange )) * 100;
212 5176 : fVal = std::max( double( 0 ), std::min( fVal, double( 100 )));
213 : }
214 :
215 5176 : if ( m_nValue != sal_Int32( fVal ))
216 : {
217 5176 : m_nValue = sal_Int32( fVal );
218 5176 : bSetValue = sal_True;
219 : }
220 :
221 5176 : nValue = m_nValue;
222 5176 : aText = m_aText;
223 : }
224 :
225 5176 : if ( xWindow.is() && bSetValue )
226 : {
227 5176 : SolarMutexGuard aSolarMutexGuard;
228 5176 : Window* pWindow = VCLUnoHelper::GetWindow( xWindow );
229 5176 : if ( pWindow && pWindow->GetType() == WINDOW_STATUSBAR )
230 : {
231 5176 : StatusBar* pStatusBar = (StatusBar *)pWindow;
232 5176 : if ( !pStatusBar->IsProgressMode() )
233 0 : pStatusBar->StartProgressMode( aText );
234 5176 : pStatusBar->SetProgressValue( sal_uInt16( nValue ));
235 5176 : }
236 5176 : }
237 : }
238 :
239 20 : void ProgressBarWrapper::reset()
240 : throw (uno::RuntimeException)
241 : {
242 20 : setText( rtl::OUString() );
243 20 : setValue( 0 );
244 20 : }
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 393 : uno::Reference< uno::XInterface > SAL_CALL ProgressBarWrapper::getRealInterface()
300 : throw (uno::RuntimeException)
301 : {
302 : /* SAFE AREA ----------------------------------------------------------------------------------------------- */
303 : // Ready for multithreading
304 393 : ResetableGuard aLock( m_aLock );
305 :
306 393 : if ( m_bDisposed )
307 0 : return uno::Reference< uno::XInterface >();
308 : else
309 : {
310 393 : uno::Reference< uno::XInterface > xComp( m_xProgressBarIfacWrapper );
311 393 : if ( !xComp.is() )
312 : {
313 : StatusIndicatorInterfaceWrapper* pWrapper =
314 : new StatusIndicatorInterfaceWrapper(
315 : uno::Reference< lang::XComponent >(
316 : static_cast< cppu::OWeakObject* >( this ),
317 240 : uno::UNO_QUERY ));
318 : xComp = uno::Reference< uno::XInterface >(
319 : static_cast< cppu::OWeakObject* >( pWrapper ),
320 240 : uno::UNO_QUERY );
321 240 : m_xProgressBarIfacWrapper = xComp;
322 : }
323 :
324 393 : return xComp;
325 393 : }
326 : }
327 :
328 : } // namespace framework
329 :
330 : /* vim:set shiftwidth=4 softtabstop=4 expandtab: */
|