1 /**************************************************************
2  *
3  * Licensed to the Apache Software Foundation (ASF) under one
4  * or more contributor license agreements.  See the NOTICE file
5  * distributed with this work for additional information
6  * regarding copyright ownership.  The ASF licenses this file
7  * to you under the Apache License, Version 2.0 (the
8  * "License"); you may not use this file except in compliance
9  * with the License.  You may obtain a copy of the License at
10  *
11  *   http://www.apache.org/licenses/LICENSE-2.0
12  *
13  * Unless required by applicable law or agreed to in writing,
14  * software distributed under the License is distributed on an
15  * "AS IS" BASIS, WITHOUT WARRANTIES OR CONDITIONS OF ANY
16  * KIND, either express or implied.  See the License for the
17  * specific language governing permissions and limitations
18  * under the License.
19  *
20  *************************************************************/
21 
22 
23 
24 // MARKER(update_precomp.py): autogen include statement, do not remove
25 #include "precompiled_framework.hxx"
26 #include <helper/vclstatusindicator.hxx>
27 
28 //-----------------------------------------------
29 // includes of own modules
30 #include <threadhelp/readguard.hxx>
31 #include <threadhelp/writeguard.hxx>
32 
33 //-----------------------------------------------
34 // includes of interfaces
35 
36 //-----------------------------------------------
37 // includes of external modules
38 
39 #ifndef _TOOLKIT_HELPER_VCLUNOHELPER_HXX_
40 #include <toolkit/helper/vclunohelper.hxx>
41 #endif
42 #include <vcl/svapp.hxx>
43 
44 //-----------------------------------------------
45 // namespace
46 
47 namespace framework {
48 
49 //-----------------------------------------------
50 // definitions
51 
52 //-----------------------------------------------
DEFINE_XINTERFACE_1(VCLStatusIndicator,OWeakObject,DIRECT_INTERFACE (css::task::XStatusIndicator))53 DEFINE_XINTERFACE_1(VCLStatusIndicator                           ,
54                     OWeakObject                                  ,
55                     DIRECT_INTERFACE(css::task::XStatusIndicator))
56 
57 //-----------------------------------------------
58 VCLStatusIndicator::VCLStatusIndicator(const css::uno::Reference< css::lang::XMultiServiceFactory >& xSMGR        ,
59                                        const css::uno::Reference< css::awt::XWindow >&               xParentWindow)
60     : ThreadHelpBase     (&Application::GetSolarMutex())
61     , ::cppu::OWeakObject(                             )
62     , m_xSMGR            (xSMGR                        )
63     , m_xParentWindow    (xParentWindow                )
64     , m_pStatusBar       (0                            )
65     , m_nRange           (0                            )
66     , m_nValue           (0                            )
67 {
68     if (!m_xParentWindow.is())
69         throw css::uno::RuntimeException(
70                 ::rtl::OUString::createFromAscii("Can't work without a parent window!"),
71                 static_cast< css::task::XStatusIndicator* >(this));
72 }
73 
74 //-----------------------------------------------
~VCLStatusIndicator()75 VCLStatusIndicator::~VCLStatusIndicator()
76 {
77 }
78 
79 //-----------------------------------------------
start(const::rtl::OUString & sText,sal_Int32 nRange)80 void SAL_CALL VCLStatusIndicator::start(const ::rtl::OUString& sText ,
81                                               sal_Int32        nRange)
82     throw(css::uno::RuntimeException)
83 {
84     // SAFE -> ----------------------------------
85     ReadGuard aReadLock(m_aLock);
86     css::uno::Reference< css::awt::XWindow > xParentWindow = m_xParentWindow;
87     aReadLock.unlock();
88     // <- SAFE ----------------------------------
89 
90     // SOLAR SAFE -> ----------------------------
91     ::vos::OClearableGuard aSolarLock(Application::GetSolarMutex());
92 
93     Window* pParentWindow = VCLUnoHelper::GetWindow(xParentWindow);
94     if (!m_pStatusBar)
95         m_pStatusBar = new StatusBar(pParentWindow, WB_3DLOOK|WB_BORDER);
96 
97     VCLStatusIndicator::impl_recalcLayout(m_pStatusBar, pParentWindow);
98 
99     m_pStatusBar->Show();
100     m_pStatusBar->StartProgressMode(sText);
101     m_pStatusBar->SetProgressValue(0);
102 
103     // force repaint!
104     pParentWindow->Show();
105     pParentWindow->Invalidate(INVALIDATE_CHILDREN);
106     pParentWindow->Flush();
107 
108     aSolarLock.clear();
109     // <- SOLAR SAFE ----------------------------
110 
111     // SAFE -> ----------------------------------
112     WriteGuard aWriteLock(m_aLock);
113     m_sText  = sText;
114     m_nRange = nRange;
115     m_nValue = 0;
116     aWriteLock.unlock();
117     // <- SAFE ----------------------------------
118 }
119 
120 //-----------------------------------------------
reset()121 void SAL_CALL VCLStatusIndicator::reset()
122     throw(css::uno::RuntimeException)
123 {
124     // SOLAR SAFE -> ----------------------------
125     ::vos::OClearableGuard aSolarLock(Application::GetSolarMutex());
126     if (m_pStatusBar)
127     {
128         m_pStatusBar->SetProgressValue(0);
129         m_pStatusBar->SetText(String());
130     }
131     aSolarLock.clear();
132     // <- SOLAR SAFE ----------------------------
133 }
134 
135 //-----------------------------------------------
end()136 void SAL_CALL VCLStatusIndicator::end()
137     throw(css::uno::RuntimeException)
138 {
139     // SAFE -> ----------------------------------
140     WriteGuard aWriteLock(m_aLock);
141     m_sText  = ::rtl::OUString();
142     m_nRange = 0;
143     m_nValue = 0;
144     aWriteLock.unlock();
145     // <- SAFE ----------------------------------
146 
147     // SOLAR SAFE -> ----------------------------
148     ::vos::OClearableGuard aSolarLock(Application::GetSolarMutex());
149     if (m_pStatusBar)
150     {
151         m_pStatusBar->EndProgressMode();
152         m_pStatusBar->Show(sal_False);
153 
154         delete m_pStatusBar;
155         m_pStatusBar = 0;
156     }
157     aSolarLock.clear();
158     // <- SOLAR SAFE ----------------------------
159 }
160 
161 //-----------------------------------------------
setText(const::rtl::OUString & sText)162 void SAL_CALL VCLStatusIndicator::setText(const ::rtl::OUString& sText)
163     throw(css::uno::RuntimeException)
164 {
165     // SAFE -> ----------------------------------
166     WriteGuard aWriteLock(m_aLock);
167     m_sText = sText;
168     aWriteLock.unlock();
169     // <- SAFE ----------------------------------
170 
171     // SOLAR SAFE -> ----------------------------
172     ::vos::OClearableGuard aSolarLock(Application::GetSolarMutex());
173     if (m_pStatusBar)
174         m_pStatusBar->SetText(sText);
175     aSolarLock.clear();
176     // <- SOLAR SAFE ----------------------------
177 }
178 
179 //-----------------------------------------------
setValue(sal_Int32 nValue)180 void SAL_CALL VCLStatusIndicator::setValue(sal_Int32 nValue)
181     throw(css::uno::RuntimeException)
182 {
183     // SAFE -> ----------------------------------
184     WriteGuard aWriteLock(m_aLock);
185 
186     if (nValue <= m_nRange)
187         m_nValue = nValue;
188     else
189         m_nValue = m_nRange;
190 
191     sal_Int32 nRange = m_nRange;
192               nValue = m_nValue;
193 
194     aWriteLock.unlock();
195     // <- SAFE ----------------------------------
196 
197     // normalize value to fit the range of 0-100 %
198     sal_uInt16 nPercent = sal::static_int_cast< sal_uInt16 >(
199         ::std::min(
200             ((nValue*100) / ::std::max(nRange,(sal_Int32)1)), (sal_Int32)100));
201 
202     // SOLAR SAFE -> ----------------------------
203     ::vos::OClearableGuard aSolarLock(Application::GetSolarMutex());
204     if (m_pStatusBar)
205         m_pStatusBar->SetProgressValue(nPercent);
206     aSolarLock.clear();
207     // <- SOLAR SAFE ----------------------------
208 }
209 
210 //-----------------------------------------------
impl_recalcLayout(Window * pStatusBar,Window * pParentWindow)211 void VCLStatusIndicator::impl_recalcLayout(Window* pStatusBar   ,
212                                            Window* pParentWindow)
213 {
214     if (
215         (!pStatusBar   ) ||
216         (!pParentWindow)
217        )
218        return;
219 
220     Size aParentSize = pParentWindow->GetSizePixel();
221     pStatusBar->SetPosSizePixel(0,
222                                 0,
223                                 aParentSize.Width(),
224                                 aParentSize.Height());
225 }
226 
227 } // namespace framework
228