xref: /AOO41X/main/reportdesign/source/core/inc/Tools.hxx (revision 1ecadb572e7010ff3b3382ad9bf179dbc6efadbb)
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 #ifndef REPORTDESIGN_TOOLS_HXX
28 #define REPORTDESIGN_TOOLS_HXX
29 
30 #include <com/sun/star/report/XReportDefinition.hpp>
31 #include <com/sun/star/report/XSection.hpp>
32 #include <com/sun/star/awt/Point.hpp>
33 #include <com/sun/star/awt/Size.hpp>
34 #include <com/sun/star/container/XChild.hpp>
35 #include <com/sun/star/uno/XComponentContext.hpp>
36 #include <com/sun/star/lang/XMultiServiceFactory.hpp>
37 #include <com/sun/star/report/XFixedText.hpp>
38 #include <com/sun/star/report/XFormattedField.hpp>
39 
40 
41 #include "Section.hxx"
42 #include "corestrings.hrc"
43 
44 namespace reportdesign
45 {
46 	template <class T> void lcl_createSectionIfNeeded(sal_Bool _bOn,const T& _xParent,::com::sun::star::uno::Reference< ::com::sun::star::report::XSection>& _xSection/*in/out*/,bool _bPageSection = false)
47 	{
48 		if ( _bOn && !_xSection.is() )
49 			_xSection = new OSection(_xParent,_xParent->getContext(),_bPageSection);
50 		else if ( !_bOn )
51             //_xSection.clear();
52             ::comphelper::disposeComponent(_xSection);
53 	}
54 
55 	/** gets the properties which should be removed form the property set implementation.
56 	 *
57 	 * \return A sequence of all properties which should be removed for none char able implementations.
58 	 */
59 	::com::sun::star::uno::Sequence< ::rtl::OUString > lcl_getCharOptionals();
60 
61     /** uses the XChild interface to get the section from any child of it.
62      *
63      * \param _xReportComponent A report component which is a child of the section.
64      * \return The sectin where this report component resists in.
65      */
66     ::com::sun::star::uno::Reference< ::com::sun::star::report::XSection> lcl_getSection(const ::com::sun::star::uno::Reference< ::com::sun::star::uno::XInterface>& _xReportComponent);
67 
68     /** throws an illegal argument exception. The message text is the resource RID_STR_ERROR_WRONG_ARGUMENT + the type as reference.
69      *
70      * \param _sTypeName The reference where to look for the correct values.
71      * \param ExceptionContext_ The exception context.
72      * \param ArgumentPosition_ The argument position.
73      * \param Context_ The context to get the factory service.
74      */
75     void throwIllegallArgumentException(const ::rtl::OUString& _sTypeName
76                                         ,const ::com::sun::star::uno::Reference< ::com::sun::star::uno::XInterface >& ExceptionContext_
77                                         ,const ::sal_Int16& ArgumentPosition_
78                                         ,const ::com::sun::star::uno::Reference< ::com::sun::star::uno::XComponentContext >& Context_);
79 
80     /** clones the given object
81     *
82     * \param _xReportComponent the object to be cloned
83     * \param _xFactory  the factory to create the clone
84     * \param _sServiceName the service of the to be cloned object
85     * \return the clone
86     */
87     ::com::sun::star::uno::Reference< ::com::sun::star::util::XCloneable > cloneObject(
88                                         const ::com::sun::star::uno::Reference< ::com::sun::star::report::XReportComponent>& _xReportComponent
89                                         ,const ::com::sun::star::uno::Reference< ::com::sun::star::lang::XMultiServiceFactory>& _xFactory
90                                         ,const ::rtl::OUString& _sServiceName);
91 
92     class OShapeHelper
93     {
94     public:
95         template<typename T> static void setSize(const ::com::sun::star::awt::Size& aSize,T* _pShape)
96         {
97             OSL_ENSURE(aSize.Width >= 0 && aSize.Height >= 0,"Illegal with or height!");
98 
99             ::osl::MutexGuard aGuard(_pShape->m_aMutex);
100             if ( _pShape->m_aProps.aComponent.m_xShape.is() )
101             {
102                 ::com::sun::star::awt::Size aOldSize = _pShape->m_aProps.aComponent.m_xShape->getSize();
103                 if ( aOldSize.Height != aSize.Height || aOldSize.Width != aSize.Width )
104                 {
105                     _pShape->m_aProps.aComponent.m_nWidth = aOldSize.Width;
106                     _pShape->m_aProps.aComponent.m_nHeight = aOldSize.Height;
107                     _pShape->m_aProps.aComponent.m_xShape->setSize(aSize);
108                 }
109             }
110             _pShape->set(PROPERTY_WIDTH,aSize.Width,_pShape->m_aProps.aComponent.m_nWidth);
111             _pShape->set(PROPERTY_HEIGHT,aSize.Height,_pShape->m_aProps.aComponent.m_nHeight);
112         }
113         template<typename T> static ::com::sun::star::awt::Size getSize( T* _pShape )
114         {
115 	        ::osl::MutexGuard aGuard(_pShape->m_aMutex);
116             if ( _pShape->m_aProps.aComponent.m_xShape.is() )
117             {
118                 ::com::sun::star::awt::Size aSize = _pShape->m_aProps.aComponent.m_xShape->getSize();
119                 OSL_ENSURE(aSize.Width >= 0 && aSize.Height >= 0,"Illegal with or height!");
120                 return aSize;
121             }
122 	        return ::com::sun::star::awt::Size(_pShape->m_aProps.aComponent.m_nWidth,_pShape->m_aProps.aComponent.m_nHeight);
123         }
124 
125         template<typename T> static void setPosition( const ::com::sun::star::awt::Point& _aPosition ,T* _pShape)
126         {
127             // we know it is not allowed that the position in smaller 0, but in NbcMove() it will handled right.
128             // only at 'Undo' it is possible to short set the position smaller 0
129             // OSL_ENSURE(_aPosition.X >= 0 && _aPosition.Y >= 0,"set to Illegal position!");
130             ::osl::MutexGuard aGuard(_pShape->m_aMutex);
131             ::com::sun::star::awt::Point aOldPos;
132             aOldPos.X = _pShape->m_aProps.aComponent.m_nPosX;
133             aOldPos.Y = _pShape->m_aProps.aComponent.m_nPosY;
134 
135             ::com::sun::star::awt::Point aPosition(_aPosition);
136             if ( _pShape->m_aProps.aComponent.m_xShape.is() )
137             {
138                 aOldPos = _pShape->m_aProps.aComponent.m_xShape->getPosition();
139                 if ( aOldPos.X != aPosition.X || aOldPos.Y != aPosition.Y )
140                 {
141                     _pShape->m_aProps.aComponent.m_nPosX = aOldPos.X;
142                     _pShape->m_aProps.aComponent.m_nPosY = aOldPos.Y;
143                     _pShape->m_aProps.aComponent.m_xShape->setPosition(aPosition);
144                 }
145             }
146             _pShape->set(PROPERTY_POSITIONX,aPosition.X,aOldPos.X);
147             _pShape->set(PROPERTY_POSITIONY,aPosition.Y,aOldPos.Y);
148         }
149         template<typename T> static ::com::sun::star::awt::Point getPosition(T* _pShape)
150         {
151             ::osl::MutexGuard aGuard(_pShape->m_aMutex);
152             if ( _pShape->m_aProps.aComponent.m_xShape.is() )
153             {
154                 ::com::sun::star::awt::Point aPosition = _pShape->m_aProps.aComponent.m_xShape->getPosition();
155 //                OSL_ENSURE(aPosition.X >= 0 && aPosition.Y >= 0,"Illegal position!");
156                 return aPosition;
157             }
158 	        return ::com::sun::star::awt::Point(_pShape->m_aProps.aComponent.m_nPosX,_pShape->m_aProps.aComponent.m_nPosY);
159         }
160         template<typename T> static void setParent( const com::sun::star::uno::Reference< com::sun::star::uno::XInterface >& Parent, T* _pShape)
161         {
162             ::osl::MutexGuard aGuard(_pShape->m_aMutex);
163 	        _pShape->m_aProps.aComponent.m_xParent = ::com::sun::star::uno::Reference< ::com::sun::star::container::XChild >(Parent,::com::sun::star::uno::UNO_QUERY);
164             ::com::sun::star::uno::Reference< ::com::sun::star::container::XChild > xChild;
165             comphelper::query_aggregation(_pShape->m_aProps.aComponent.m_xProxy,xChild);
166             if ( xChild.is() )
167 	            xChild->setParent(Parent);
168         }
169         template<typename T> static com::sun::star::uno::Reference< com::sun::star::uno::XInterface > getParent( T* _pShape )
170         {
171             ::osl::MutexGuard aGuard(_pShape->m_aMutex);
172             ::com::sun::star::uno::Reference< ::com::sun::star::container::XChild > xChild;
173             comphelper::query_aggregation(_pShape->m_aProps.aComponent.m_xProxy,xChild);
174             if ( xChild.is() )
175                     return xChild->getParent();
176 	        return _pShape->m_aProps.aComponent.m_xParent;
177         }
178     };
179 // =============================================================================
180 } // namespace reportdesign
181 // =============================================================================
182 #endif // REPORTDESIGN_TOOLS_HXX
183 
184