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