xref: /AOO41X/main/framework/inc/helper/statusindicatorfactory.hxx (revision cdf0e10c4e3984b49a9502b011690b615761d4a3)
1*cdf0e10cSrcweir /*************************************************************************
2*cdf0e10cSrcweir  *
3*cdf0e10cSrcweir  * DO NOT ALTER OR REMOVE COPYRIGHT NOTICES OR THIS FILE HEADER.
4*cdf0e10cSrcweir  *
5*cdf0e10cSrcweir  * Copyright 2000, 2010 Oracle and/or its affiliates.
6*cdf0e10cSrcweir  *
7*cdf0e10cSrcweir  * OpenOffice.org - a multi-platform office productivity suite
8*cdf0e10cSrcweir  *
9*cdf0e10cSrcweir  * This file is part of OpenOffice.org.
10*cdf0e10cSrcweir  *
11*cdf0e10cSrcweir  * OpenOffice.org is free software: you can redistribute it and/or modify
12*cdf0e10cSrcweir  * it under the terms of the GNU Lesser General Public License version 3
13*cdf0e10cSrcweir  * only, as published by the Free Software Foundation.
14*cdf0e10cSrcweir  *
15*cdf0e10cSrcweir  * OpenOffice.org is distributed in the hope that it will be useful,
16*cdf0e10cSrcweir  * but WITHOUT ANY WARRANTY; without even the implied warranty of
17*cdf0e10cSrcweir  * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the
18*cdf0e10cSrcweir  * GNU Lesser General Public License version 3 for more details
19*cdf0e10cSrcweir  * (a copy is included in the LICENSE file that accompanied this code).
20*cdf0e10cSrcweir  *
21*cdf0e10cSrcweir  * You should have received a copy of the GNU Lesser General Public License
22*cdf0e10cSrcweir  * version 3 along with OpenOffice.org.  If not, see
23*cdf0e10cSrcweir  * <http://www.openoffice.org/license.html>
24*cdf0e10cSrcweir  * for a copy of the LGPLv3 License.
25*cdf0e10cSrcweir  *
26*cdf0e10cSrcweir  ************************************************************************/
27*cdf0e10cSrcweir 
28*cdf0e10cSrcweir #ifndef __FRAMEWORK_HELPER_STATUSINDICATORFACTORY_HXX_
29*cdf0e10cSrcweir #define __FRAMEWORK_HELPER_STATUSINDICATORFACTORY_HXX_
30*cdf0e10cSrcweir 
31*cdf0e10cSrcweir // Attention: stl headers must(!) be included at first. Otherwhise it can make trouble
32*cdf0e10cSrcweir // with solaris headers ...
33*cdf0e10cSrcweir #include <vector>
34*cdf0e10cSrcweir 
35*cdf0e10cSrcweir //_______________________________________________
36*cdf0e10cSrcweir // include files of own module
37*cdf0e10cSrcweir #include <helper/wakeupthread.hxx>
38*cdf0e10cSrcweir #include <threadhelp/threadhelpbase.hxx>
39*cdf0e10cSrcweir #include <macros/xinterface.hxx>
40*cdf0e10cSrcweir #include <macros/xtypeprovider.hxx>
41*cdf0e10cSrcweir #include <macros/xserviceinfo.hxx>
42*cdf0e10cSrcweir #include <macros/debug.hxx>
43*cdf0e10cSrcweir #include <macros/generic.hxx>
44*cdf0e10cSrcweir #include <general.h>
45*cdf0e10cSrcweir 
46*cdf0e10cSrcweir //_______________________________________________
47*cdf0e10cSrcweir // include uno interfaces
48*cdf0e10cSrcweir #include <com/sun/star/lang/XTypeProvider.hpp>
49*cdf0e10cSrcweir #include <com/sun/star/lang/XServiceInfo.hpp>
50*cdf0e10cSrcweir #include <com/sun/star/lang/XInitialization.hpp>
51*cdf0e10cSrcweir #include <com/sun/star/lang/XEventListener.hpp>
52*cdf0e10cSrcweir #include <com/sun/star/task/XStatusIndicatorFactory.hpp>
53*cdf0e10cSrcweir #include <com/sun/star/task/XStatusIndicator.hpp>
54*cdf0e10cSrcweir #include <com/sun/star/awt/XWindow.hpp>
55*cdf0e10cSrcweir #include <com/sun/star/awt/XWindowListener.hpp>
56*cdf0e10cSrcweir #include <com/sun/star/lang/EventObject.hpp>
57*cdf0e10cSrcweir #include <com/sun/star/awt/WindowEvent.hpp>
58*cdf0e10cSrcweir #include <com/sun/star/lang/XMultiServiceFactory.hpp>
59*cdf0e10cSrcweir #include <com/sun/star/frame/XFrame.hpp>
60*cdf0e10cSrcweir 
61*cdf0e10cSrcweir #ifndef _COM_SUN_STAR_URTIL_XUPDATABLE_HPP_
62*cdf0e10cSrcweir #include <com/sun/star/util/XUpdatable.hpp>
63*cdf0e10cSrcweir #endif
64*cdf0e10cSrcweir 
65*cdf0e10cSrcweir //_______________________________________________
66*cdf0e10cSrcweir // include others
67*cdf0e10cSrcweir #include <vcl/status.hxx>
68*cdf0e10cSrcweir #include <cppuhelper/weak.hxx>
69*cdf0e10cSrcweir #include <osl/thread.hxx>
70*cdf0e10cSrcweir 
71*cdf0e10cSrcweir //_______________________________________________
72*cdf0e10cSrcweir // namespace
73*cdf0e10cSrcweir 
74*cdf0e10cSrcweir namespace framework{
75*cdf0e10cSrcweir 
76*cdf0e10cSrcweir //_______________________________________________
77*cdf0e10cSrcweir // definitions
78*cdf0e10cSrcweir 
79*cdf0e10cSrcweir //===============================================
80*cdf0e10cSrcweir /**
81*cdf0e10cSrcweir     @descr  This struct hold some informations about all currently running progress proccesses.
82*cdf0e10cSrcweir             Because the can be used on a stack, we must cache her states but must paint only
83*cdf0e10cSrcweir             the top most one.
84*cdf0e10cSrcweir  */
85*cdf0e10cSrcweir struct IndicatorInfo
86*cdf0e10cSrcweir {
87*cdf0e10cSrcweir     //-------------------------------------------
88*cdf0e10cSrcweir     // member
89*cdf0e10cSrcweir     public:
90*cdf0e10cSrcweir 
91*cdf0e10cSrcweir         /** @short  points to the indicator child, where we hold its states
92*cdf0e10cSrcweir                     alive here. */
93*cdf0e10cSrcweir         css::uno::Reference< css::task::XStatusIndicator > m_xIndicator;
94*cdf0e10cSrcweir 
95*cdf0e10cSrcweir         /** @short  the last set text for this indicator */
96*cdf0e10cSrcweir         ::rtl::OUString m_sText;
97*cdf0e10cSrcweir 
98*cdf0e10cSrcweir         /** @short  the max range for this indicator. */
99*cdf0e10cSrcweir         sal_Int32 m_nRange;
100*cdf0e10cSrcweir 
101*cdf0e10cSrcweir         /** @short  the last set value for this indicator */
102*cdf0e10cSrcweir         sal_Int32 m_nValue;
103*cdf0e10cSrcweir 
104*cdf0e10cSrcweir     //-------------------------------------------
105*cdf0e10cSrcweir     // interface
106*cdf0e10cSrcweir     public:
107*cdf0e10cSrcweir 
108*cdf0e10cSrcweir         //---------------------------------------
109*cdf0e10cSrcweir         /** @short  initialize new instance of this class
110*cdf0e10cSrcweir 
111*cdf0e10cSrcweir             @param  xIndicator
112*cdf0e10cSrcweir                     the new child indiactor of our factory.
113*cdf0e10cSrcweir 
114*cdf0e10cSrcweir             @param  sText
115*cdf0e10cSrcweir                     its initial text.
116*cdf0e10cSrcweir 
117*cdf0e10cSrcweir             @param  nRange
118*cdf0e10cSrcweir                     the max range for this indicator.
119*cdf0e10cSrcweir          */
120*cdf0e10cSrcweir         IndicatorInfo(const css::uno::Reference< css::task::XStatusIndicator >& xIndicator,
121*cdf0e10cSrcweir                       const ::rtl::OUString&                                    sText     ,
122*cdf0e10cSrcweir                             sal_Int32                                           nRange    )
123*cdf0e10cSrcweir         {
124*cdf0e10cSrcweir             m_xIndicator = xIndicator;
125*cdf0e10cSrcweir             m_sText      = sText     ;
126*cdf0e10cSrcweir             m_nRange     = nRange    ;
127*cdf0e10cSrcweir             m_nValue     = 0         ;
128*cdf0e10cSrcweir         }
129*cdf0e10cSrcweir 
130*cdf0e10cSrcweir         //---------------------------------------
131*cdf0e10cSrcweir         /** @short  Don't forget to free used references!
132*cdf0e10cSrcweir          */
133*cdf0e10cSrcweir         ~IndicatorInfo()
134*cdf0e10cSrcweir         {
135*cdf0e10cSrcweir             m_xIndicator.clear();
136*cdf0e10cSrcweir         }
137*cdf0e10cSrcweir 
138*cdf0e10cSrcweir         //---------------------------------------------------------------------------------------------------------
139*cdf0e10cSrcweir         /** @short  Used to locate an info struct inside a stl structure ...
140*cdf0e10cSrcweir 
141*cdf0e10cSrcweir             @descr  The indicator object itself is used as key. Its values
142*cdf0e10cSrcweir                     are not interesting then. Because mor then one child
143*cdf0e10cSrcweir                     indicator can use the same values ...
144*cdf0e10cSrcweir          */
145*cdf0e10cSrcweir         sal_Bool operator==(const css::uno::Reference< css::task::XStatusIndicator >& xIndicator)
146*cdf0e10cSrcweir         {
147*cdf0e10cSrcweir             return (m_xIndicator == xIndicator);
148*cdf0e10cSrcweir         }
149*cdf0e10cSrcweir };
150*cdf0e10cSrcweir /*
151*cdf0e10cSrcweir     //---------------------------------------------------------------------------------------------------------
152*cdf0e10cSrcweir     // norm nValue to fit range of 0..100%
153*cdf0e10cSrcweir     sal_Int32 calcPercentage()
154*cdf0e10cSrcweir     {
155*cdf0e10cSrcweir     return ::std::min( (( m_nValue * 100 )/ ::std::max( m_nRange, (sal_Int32)1 ) ), (sal_Int32)100 );
156*cdf0e10cSrcweir     }
157*cdf0e10cSrcweir */
158*cdf0e10cSrcweir 
159*cdf0e10cSrcweir //===============================================
160*cdf0e10cSrcweir /** @descr  Define a lits of child indicator objects and her data. */
161*cdf0e10cSrcweir typedef ::std::vector< IndicatorInfo > IndicatorStack;
162*cdf0e10cSrcweir 
163*cdf0e10cSrcweir //===============================================
164*cdf0e10cSrcweir /** @short          implement a factory service to create new status indicator objects
165*cdf0e10cSrcweir 
166*cdf0e10cSrcweir     @descr			Internaly it uses:
167*cdf0e10cSrcweir                     - a vcl based
168*cdf0e10cSrcweir                     - or an uno based and by the frame layouted
169*cdf0e10cSrcweir                     progress implementation.
170*cdf0e10cSrcweir 
171*cdf0e10cSrcweir                     This factory create different indicators and control his access
172*cdf0e10cSrcweir                     to a shared output device! Only the last activated component
173*cdf0e10cSrcweir                     can write his state to this device. All other requests will be
174*cdf0e10cSrcweir                     cached only.
175*cdf0e10cSrcweir 
176*cdf0e10cSrcweir     @devstatus		ready to use
177*cdf0e10cSrcweir     @threadsafe     yes
178*cdf0e10cSrcweir  */
179*cdf0e10cSrcweir class StatusIndicatorFactory : public  css::lang::XTypeProvider
180*cdf0e10cSrcweir                              , public  css::lang::XServiceInfo
181*cdf0e10cSrcweir                              , public  css::lang::XInitialization
182*cdf0e10cSrcweir                              , public  css::task::XStatusIndicatorFactory
183*cdf0e10cSrcweir                              , public  css::util::XUpdatable
184*cdf0e10cSrcweir                              , private ThreadHelpBase
185*cdf0e10cSrcweir                              , public  ::cppu::OWeakObject                   // => XInterface
186*cdf0e10cSrcweir {
187*cdf0e10cSrcweir     //-------------------------------------------
188*cdf0e10cSrcweir     // member
189*cdf0e10cSrcweir     private:
190*cdf0e10cSrcweir 
191*cdf0e10cSrcweir         /** stack with all current indicator childs. */
192*cdf0e10cSrcweir         IndicatorStack m_aStack;
193*cdf0e10cSrcweir 
194*cdf0e10cSrcweir         /** uno service manager to create own needed uno resources. */
195*cdf0e10cSrcweir         css::uno::Reference< css::lang::XMultiServiceFactory > m_xSMGR;
196*cdf0e10cSrcweir 
197*cdf0e10cSrcweir         /** most active indicator child, which could work with our shared indicator window only. */
198*cdf0e10cSrcweir         css::uno::Reference< css::task::XStatusIndicator > m_xActiveChild;
199*cdf0e10cSrcweir 
200*cdf0e10cSrcweir         /** used to show the progress on the frame (layouted!) or
201*cdf0e10cSrcweir             as a plugged vcl window. */
202*cdf0e10cSrcweir         css::uno::Reference< css::task::XStatusIndicator > m_xProgress;
203*cdf0e10cSrcweir 
204*cdf0e10cSrcweir         /** points to the frame, where we show the progress (in case
205*cdf0e10cSrcweir             m_xProgress points to a frame progress. */
206*cdf0e10cSrcweir         css::uno::WeakReference< css::frame::XFrame > m_xFrame;
207*cdf0e10cSrcweir 
208*cdf0e10cSrcweir         /** points to an outside window, where we show the progress (in case
209*cdf0e10cSrcweir             we are plugged into such window). */
210*cdf0e10cSrcweir         css::uno::WeakReference< css::awt::XWindow > m_xPluggWindow;
211*cdf0e10cSrcweir 
212*cdf0e10cSrcweir         /** Notify us if a fix time is over. We use it to implement an
213*cdf0e10cSrcweir             intelligent "Reschedule" ... */
214*cdf0e10cSrcweir         WakeUpThread* m_pWakeUp;
215*cdf0e10cSrcweir 
216*cdf0e10cSrcweir         /** Our WakeUpThread calls us in our interface method "XUpdatable::update().
217*cdf0e10cSrcweir             There we set this member m_bAllowReschedule to sal_True. Next time if our impl_reschedule()
218*cdf0e10cSrcweir             method is called, we know, that an Application::Reschedule() should be made.
219*cdf0e10cSrcweir             Because the last made Reschedule can be was taken long time ago ... may be.*/
220*cdf0e10cSrcweir         sal_Bool m_bAllowReschedule;
221*cdf0e10cSrcweir 
222*cdf0e10cSrcweir         /** enable/disable automatic showing of our parent window. */
223*cdf0e10cSrcweir         sal_Bool m_bAllowParentShow;
224*cdf0e10cSrcweir 
225*cdf0e10cSrcweir         /** enable/disable rescheduling. Default=enabled*/
226*cdf0e10cSrcweir         sal_Bool m_bDisableReschedule;
227*cdf0e10cSrcweir 
228*cdf0e10cSrcweir         /** prevent recursive calling of Application::Reschedule(). */
229*cdf0e10cSrcweir         static sal_Int32 m_nInReschedule;
230*cdf0e10cSrcweir 
231*cdf0e10cSrcweir         /** time where there last start call was made. */
232*cdf0e10cSrcweir         sal_Int32 m_nStartTime;
233*cdf0e10cSrcweir 
234*cdf0e10cSrcweir     //-------------------------------------------
235*cdf0e10cSrcweir     // interface
236*cdf0e10cSrcweir 
237*cdf0e10cSrcweir 	public:
238*cdf0e10cSrcweir 
239*cdf0e10cSrcweir         //---------------------------------------
240*cdf0e10cSrcweir         // ctor
241*cdf0e10cSrcweir         StatusIndicatorFactory(const css::uno::Reference< css::lang::XMultiServiceFactory >& xSMGR);
242*cdf0e10cSrcweir 
243*cdf0e10cSrcweir         //---------------------------------------
244*cdf0e10cSrcweir         // XInterface, XTypeProvider, XServiceInfo
245*cdf0e10cSrcweir         FWK_DECLARE_XINTERFACE
246*cdf0e10cSrcweir         FWK_DECLARE_XTYPEPROVIDER
247*cdf0e10cSrcweir         DECLARE_XSERVICEINFO
248*cdf0e10cSrcweir 
249*cdf0e10cSrcweir         //---------------------------------------
250*cdf0e10cSrcweir         // XInitialization
251*cdf0e10cSrcweir         virtual void SAL_CALL initialize(const css::uno::Sequence< css::uno::Any >& lArguments)
252*cdf0e10cSrcweir             throw(css::uno::Exception       ,
253*cdf0e10cSrcweir                   css::uno::RuntimeException);
254*cdf0e10cSrcweir 
255*cdf0e10cSrcweir         //---------------------------------------
256*cdf0e10cSrcweir         // XStatusIndicatorFactory
257*cdf0e10cSrcweir 	    virtual css::uno::Reference< css::task::XStatusIndicator > SAL_CALL createStatusIndicator()
258*cdf0e10cSrcweir             throw(css::uno::RuntimeException);
259*cdf0e10cSrcweir 
260*cdf0e10cSrcweir         //---------------------------------------
261*cdf0e10cSrcweir         // XUpdatable
262*cdf0e10cSrcweir         virtual void SAL_CALL update()
263*cdf0e10cSrcweir             throw(css::uno::RuntimeException);
264*cdf0e10cSrcweir 
265*cdf0e10cSrcweir         //---------------------------------------
266*cdf0e10cSrcweir         // similar (XStatusIndicator)
267*cdf0e10cSrcweir         virtual void start(const css::uno::Reference< css::task::XStatusIndicator >& xChild,
268*cdf0e10cSrcweir                            const ::rtl::OUString&                                    sText ,
269*cdf0e10cSrcweir                                  sal_Int32                                           nRange);
270*cdf0e10cSrcweir 
271*cdf0e10cSrcweir         virtual void SAL_CALL reset(const css::uno::Reference< css::task::XStatusIndicator >& xChild);
272*cdf0e10cSrcweir 
273*cdf0e10cSrcweir         virtual void SAL_CALL end(const css::uno::Reference< css::task::XStatusIndicator >& xChild);
274*cdf0e10cSrcweir 
275*cdf0e10cSrcweir         virtual void SAL_CALL setText(const css::uno::Reference< css::task::XStatusIndicator >& xChild,
276*cdf0e10cSrcweir                                       const ::rtl::OUString&                                    sText );
277*cdf0e10cSrcweir 
278*cdf0e10cSrcweir         virtual void SAL_CALL setValue(const css::uno::Reference< css::task::XStatusIndicator >& xChild,
279*cdf0e10cSrcweir                                              sal_Int32                                           nValue);
280*cdf0e10cSrcweir 
281*cdf0e10cSrcweir     //-------------------------------------------
282*cdf0e10cSrcweir     // specials
283*cdf0e10cSrcweir 
284*cdf0e10cSrcweir 	protected:
285*cdf0e10cSrcweir 
286*cdf0e10cSrcweir         virtual ~StatusIndicatorFactory();
287*cdf0e10cSrcweir 
288*cdf0e10cSrcweir     //-------------------------------------------
289*cdf0e10cSrcweir     // helper
290*cdf0e10cSrcweir 	private:
291*cdf0e10cSrcweir 
292*cdf0e10cSrcweir         /** @short  show the parent window of this progress ...
293*cdf0e10cSrcweir                     if it's allowed to do so.
294*cdf0e10cSrcweir 
295*cdf0e10cSrcweir 
296*cdf0e10cSrcweir             @descr  By default we show the parent window automaticly
297*cdf0e10cSrcweir                     if this progress is used.
298*cdf0e10cSrcweir                     If that isn't a valid operation, the user of this
299*cdf0e10cSrcweir                     progress can suppress this feature by initializaing
300*cdf0e10cSrcweir                     us with a special parameter.
301*cdf0e10cSrcweir 
302*cdf0e10cSrcweir             @seealso    initialize()
303*cdf0e10cSrcweir          */
304*cdf0e10cSrcweir         void implts_makeParentVisibleIfAllowed();
305*cdf0e10cSrcweir 
306*cdf0e10cSrcweir         /** @short  creates a new internal used progress.
307*cdf0e10cSrcweir             @descr  This factory does not paint the progress itself.
308*cdf0e10cSrcweir                     It uses helper for that. They can be vcl based or
309*cdf0e10cSrcweir                     layouted by the frame and provided as an uno interface.
310*cdf0e10cSrcweir          */
311*cdf0e10cSrcweir         void impl_createProgress();
312*cdf0e10cSrcweir 
313*cdf0e10cSrcweir         /** @short  shows the internal used progress.
314*cdf0e10cSrcweir             @descr  This factory does not paint the progress itself.
315*cdf0e10cSrcweir                     It uses helper for that. They can be vcl based or
316*cdf0e10cSrcweir                     layouted by the frame and provided as an uno interface.
317*cdf0e10cSrcweir          */
318*cdf0e10cSrcweir         void impl_showProgress();
319*cdf0e10cSrcweir 
320*cdf0e10cSrcweir         /** @short  hides the internal used progress.
321*cdf0e10cSrcweir             @descr  This factory does not paint the progress itself.
322*cdf0e10cSrcweir                     It uses helper for that. They can be vcl based or
323*cdf0e10cSrcweir                     layouted by the frame and provided as an uno interface.
324*cdf0e10cSrcweir          */
325*cdf0e10cSrcweir         void impl_hideProgress();
326*cdf0e10cSrcweir 
327*cdf0e10cSrcweir         /** @short  try to "share the current thread in an intelligent manner" :-)
328*cdf0e10cSrcweir 
329*cdf0e10cSrcweir             @param  Overwrites our algorithm for Reschedule and force it to be shure
330*cdf0e10cSrcweir                     that our progress was painted right.
331*cdf0e10cSrcweir          */
332*cdf0e10cSrcweir         void impl_reschedule(sal_Bool bForceUpdate);
333*cdf0e10cSrcweir 
334*cdf0e10cSrcweir         void impl_startWakeUpThread();
335*cdf0e10cSrcweir         void impl_stopWakeUpThread();
336*cdf0e10cSrcweir 
337*cdf0e10cSrcweir }; // class StatusIndicatorFactory
338*cdf0e10cSrcweir 
339*cdf0e10cSrcweir } // namespace framework
340*cdf0e10cSrcweir 
341*cdf0e10cSrcweir #endif // #ifndef __FRAMEWORK_HELPER_STATUSINDICATORFACTORY_HXX_
342