1 /*************************************************************************
2  *
3  * DO NOT ALTER OR REMOVE COPYRIGHT NOTICES OR THIS FILE HEADER.
4  *
5  * Copyright 2000, 2010 Oracle and/or its affiliates.
6  *
7  * OpenOffice.org - a multi-platform office productivity suite
8  *
9  * This file is part of OpenOffice.org.
10  *
11  * OpenOffice.org is free software: you can redistribute it and/or modify
12  * it under the terms of the GNU Lesser General Public License version 3
13  * only, as published by the Free Software Foundation.
14  *
15  * OpenOffice.org is distributed in the hope that it will be useful,
16  * but WITHOUT ANY WARRANTY; without even the implied warranty of
17  * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the
18  * GNU Lesser General Public License version 3 for more details
19  * (a copy is included in the LICENSE file that accompanied this code).
20  *
21  * You should have received a copy of the GNU Lesser General Public License
22  * version 3 along with OpenOffice.org.  If not, see
23  * <http://www.openoffice.org/license.html>
24  * for a copy of the LGPLv3 License.
25  *
26  ************************************************************************/
27 
28 // MARKER(update_precomp.py): autogen include statement, do not remove
29 #include "precompiled_framework.hxx"
30 #include <helper/vclstatusindicator.hxx>
31 
32 //-----------------------------------------------
33 // includes of own modules
34 #include <threadhelp/readguard.hxx>
35 #include <threadhelp/writeguard.hxx>
36 
37 //-----------------------------------------------
38 // includes of interfaces
39 
40 //-----------------------------------------------
41 // includes of external modules
42 
43 #ifndef _TOOLKIT_HELPER_VCLUNOHELPER_HXX_
44 #include <toolkit/unohlp.hxx>
45 #endif
46 #include <vcl/svapp.hxx>
47 
48 //-----------------------------------------------
49 // namespace
50 
51 namespace framework {
52 
53 //-----------------------------------------------
54 // definitions
55 
56 //-----------------------------------------------
57 DEFINE_XINTERFACE_1(VCLStatusIndicator                           ,
58                     OWeakObject                                  ,
59                     DIRECT_INTERFACE(css::task::XStatusIndicator))
60 
61 //-----------------------------------------------
62 VCLStatusIndicator::VCLStatusIndicator(const css::uno::Reference< css::lang::XMultiServiceFactory >& xSMGR        ,
63                                        const css::uno::Reference< css::awt::XWindow >&               xParentWindow)
64     : ThreadHelpBase     (&Application::GetSolarMutex())
65     , ::cppu::OWeakObject(                             )
66     , m_xSMGR            (xSMGR                        )
67     , m_xParentWindow    (xParentWindow                )
68     , m_pStatusBar       (0                            )
69     , m_nRange           (0                            )
70     , m_nValue           (0                            )
71 {
72     if (!m_xParentWindow.is())
73         throw css::uno::RuntimeException(
74                 ::rtl::OUString::createFromAscii("Cant work without a parent window!"),
75                 static_cast< css::task::XStatusIndicator* >(this));
76 }
77 
78 //-----------------------------------------------
79 VCLStatusIndicator::~VCLStatusIndicator()
80 {
81 }
82 
83 //-----------------------------------------------
84 void SAL_CALL VCLStatusIndicator::start(const ::rtl::OUString& sText ,
85                                               sal_Int32        nRange)
86     throw(css::uno::RuntimeException)
87 {
88     // SAFE -> ----------------------------------
89     ReadGuard aReadLock(m_aLock);
90     css::uno::Reference< css::awt::XWindow > xParentWindow = m_xParentWindow;
91     aReadLock.unlock();
92     // <- SAFE ----------------------------------
93 
94     // SOLAR SAFE -> ----------------------------
95     ::vos::OClearableGuard aSolarLock(Application::GetSolarMutex());
96 
97     Window* pParentWindow = VCLUnoHelper::GetWindow(xParentWindow);
98     if (!m_pStatusBar)
99         m_pStatusBar = new StatusBar(pParentWindow, WB_3DLOOK|WB_BORDER);
100 
101     VCLStatusIndicator::impl_recalcLayout(m_pStatusBar, pParentWindow);
102 
103     m_pStatusBar->Show();
104     m_pStatusBar->StartProgressMode(sText);
105     m_pStatusBar->SetProgressValue(0);
106 
107     // force repaint!
108     pParentWindow->Show();
109     pParentWindow->Invalidate(INVALIDATE_CHILDREN);
110     pParentWindow->Flush();
111 
112     aSolarLock.clear();
113     // <- SOLAR SAFE ----------------------------
114 
115     // SAFE -> ----------------------------------
116     WriteGuard aWriteLock(m_aLock);
117     m_sText  = sText;
118     m_nRange = nRange;
119     m_nValue = 0;
120     aWriteLock.unlock();
121     // <- SAFE ----------------------------------
122 }
123 
124 //-----------------------------------------------
125 void SAL_CALL VCLStatusIndicator::reset()
126     throw(css::uno::RuntimeException)
127 {
128     // SOLAR SAFE -> ----------------------------
129     ::vos::OClearableGuard aSolarLock(Application::GetSolarMutex());
130     if (m_pStatusBar)
131     {
132         m_pStatusBar->SetProgressValue(0);
133         m_pStatusBar->SetText(String());
134     }
135     aSolarLock.clear();
136     // <- SOLAR SAFE ----------------------------
137 }
138 
139 //-----------------------------------------------
140 void SAL_CALL VCLStatusIndicator::end()
141     throw(css::uno::RuntimeException)
142 {
143     // SAFE -> ----------------------------------
144     WriteGuard aWriteLock(m_aLock);
145     m_sText  = ::rtl::OUString();
146     m_nRange = 0;
147     m_nValue = 0;
148     aWriteLock.unlock();
149     // <- SAFE ----------------------------------
150 
151     // SOLAR SAFE -> ----------------------------
152     ::vos::OClearableGuard aSolarLock(Application::GetSolarMutex());
153     if (m_pStatusBar)
154     {
155         m_pStatusBar->EndProgressMode();
156         m_pStatusBar->Show(sal_False);
157 
158         delete m_pStatusBar;
159         m_pStatusBar = 0;
160     }
161     aSolarLock.clear();
162     // <- SOLAR SAFE ----------------------------
163 }
164 
165 //-----------------------------------------------
166 void SAL_CALL VCLStatusIndicator::setText(const ::rtl::OUString& sText)
167     throw(css::uno::RuntimeException)
168 {
169     // SAFE -> ----------------------------------
170     WriteGuard aWriteLock(m_aLock);
171     m_sText = sText;
172     aWriteLock.unlock();
173     // <- SAFE ----------------------------------
174 
175     // SOLAR SAFE -> ----------------------------
176     ::vos::OClearableGuard aSolarLock(Application::GetSolarMutex());
177     if (m_pStatusBar)
178         m_pStatusBar->SetText(sText);
179     aSolarLock.clear();
180     // <- SOLAR SAFE ----------------------------
181 }
182 
183 //-----------------------------------------------
184 void SAL_CALL VCLStatusIndicator::setValue(sal_Int32 nValue)
185     throw(css::uno::RuntimeException)
186 {
187     // SAFE -> ----------------------------------
188     WriteGuard aWriteLock(m_aLock);
189 
190     if (nValue <= m_nRange)
191         m_nValue = nValue;
192     else
193         m_nValue = m_nRange;
194 
195     sal_Int32 nRange = m_nRange;
196               nValue = m_nValue;
197 
198     aWriteLock.unlock();
199     // <- SAFE ----------------------------------
200 
201     // normalize value to fit the range of 0-100 %
202     sal_uInt16 nPercent = sal::static_int_cast< sal_uInt16 >(
203         ::std::min(
204             ((nValue*100) / ::std::max(nRange,(sal_Int32)1)), (sal_Int32)100));
205 
206     // SOLAR SAFE -> ----------------------------
207     ::vos::OClearableGuard aSolarLock(Application::GetSolarMutex());
208     if (m_pStatusBar)
209         m_pStatusBar->SetProgressValue(nPercent);
210     aSolarLock.clear();
211     // <- SOLAR SAFE ----------------------------
212 }
213 
214 //-----------------------------------------------
215 void VCLStatusIndicator::impl_recalcLayout(Window* pStatusBar   ,
216                                            Window* pParentWindow)
217 {
218     if (
219         (!pStatusBar   ) ||
220         (!pParentWindow)
221        )
222        return;
223 
224     Size aParentSize = pParentWindow->GetSizePixel();
225     pStatusBar->SetPosSizePixel(0,
226                                 0,
227                                 aParentSize.Width(),
228                                 aParentSize.Height());
229 }
230 
231 } // namespace framework
232