xref: /AOO41X/main/vbahelper/inc/vbahelper/vbahelper.hxx (revision cdf0e10c4e3984b49a9502b011690b615761d4a3)
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 OOVBAAPI_VBA_HELPER_HXX
28 #define OOVBAAPI_VBA_HELPER_HXX
29 
30 #include <com/sun/star/drawing/XShape.hpp>
31 #include <com/sun/star/beans/XIntrospectionAccess.hpp>
32 #include <com/sun/star/beans/XPropertySet.hpp>
33 #include <com/sun/star/script/BasicErrorException.hpp>
34 #include <com/sun/star/script/XTypeConverter.hpp>
35 #include <com/sun/star/lang/IllegalArgumentException.hpp>
36 #include <com/sun/star/awt/XControl.hpp>
37 #include <com/sun/star/awt/XDevice.hpp>
38 #include <com/sun/star/awt/XUnitConversion.hpp>
39 #include <basic/basmgr.hxx>
40 #include <basic/sberrors.hxx>
41 #include <cppuhelper/implbase1.hxx>
42 #include <com/sun/star/frame/XModel.hpp>
43 #include <sfx2/dispatch.hxx>
44 #include <sfx2/objsh.hxx>
45 #include <sfx2/docfilt.hxx>
46 #include <sfx2/docfile.hxx>
47 #include <vcl/pointr.hxx>
48 #include <memory>
49 #include "vbaaccesshelper.hxx"
50 
51 namespace css = ::com::sun::star;
52 namespace ooo
53 {
54 	namespace vba
55 	{
56 		template < class T >
57 		css::uno::Reference< T > getXSomethingFromArgs( css::uno::Sequence< css::uno::Any > const & args, sal_Int32 nPos, bool bCanBeNull = true ) throw (css::lang::IllegalArgumentException)
58 		{
59 			if ( args.getLength() < ( nPos + 1) )
60 				throw css::lang::IllegalArgumentException();
61 			css::uno::Reference< T > aSomething( args[ nPos ], css::uno::UNO_QUERY );
62 			if ( !bCanBeNull && !aSomething.is() )
63 				throw css::lang::IllegalArgumentException();
64 			return aSomething;
65 		}
66 
67         class XHelperInterface;
68 
69         /** Returns the VBA document implementation object representing the passed UNO document model. */
70         VBAHELPER_DLLPUBLIC css::uno::Reference< XHelperInterface > getVBADocument( const css::uno::Reference< css::frame::XModel >& xModel );
71         VBAHELPER_DLLPUBLIC css::uno::Reference< XHelperInterface > getUnoDocModule( const String& aModName, SfxObjectShell* pShell );
72         VBAHELPER_DLLPUBLIC SfxObjectShell* getSfxObjShell( const css::uno::Reference< css::frame::XModel >& xModel ) throw ( css::uno::RuntimeException);
73         VBAHELPER_DLLPUBLIC css::uno::Reference< css::uno::XInterface > createVBAUnoAPIService( SfxObjectShell* pShell,  const sal_Char* _pAsciiName ) throw (css::uno::RuntimeException);
74 
75         css::uno::Reference< css::frame::XModel > getCurrentDoc( const rtl::OUString& sKey ) throw (css::uno::RuntimeException);
76         VBAHELPER_DLLPUBLIC css::uno::Reference< css::frame::XModel > getThisExcelDoc( const css::uno::Reference< css::uno::XComponentContext >& xContext ) throw (css::uno::RuntimeException);
77         VBAHELPER_DLLPUBLIC css::uno::Reference< css::frame::XModel > getThisWordDoc( const css::uno::Reference< css::uno::XComponentContext >& xContext ) throw (css::uno::RuntimeException);
78         VBAHELPER_DLLPUBLIC css::uno::Reference< css::frame::XModel > getCurrentExcelDoc( const css::uno::Reference< css::uno::XComponentContext >& xContext ) throw (css::uno::RuntimeException);
79         VBAHELPER_DLLPUBLIC css::uno::Reference< css::frame::XModel > getCurrentWordDoc( const css::uno::Reference< css::uno::XComponentContext >& xContext ) throw (css::uno::RuntimeException);
80 
81 		VBAHELPER_DLLPUBLIC css::uno::Reference< css::beans::XIntrospectionAccess > getIntrospectionAccess( const css::uno::Any& aObject ) throw (css::uno::RuntimeException);
82 		VBAHELPER_DLLPUBLIC css::uno::Reference< css::script::XTypeConverter > getTypeConverter( const css::uno::Reference< css::uno::XComponentContext >& xContext ) throw (css::uno::RuntimeException);
83 
84 		VBAHELPER_DLLPUBLIC void dispatchRequests( const css::uno::Reference< css::frame::XModel>& xModel, const rtl::OUString& aUrl );
85 		VBAHELPER_DLLPUBLIC void dispatchRequests( const css::uno::Reference< css::frame::XModel>& xModel, const rtl::OUString& aUrl, const css::uno::Sequence< css::beans::PropertyValue >& sProps );
86 		VBAHELPER_DLLPUBLIC void dispatchExecute(SfxViewShell* pView, sal_uInt16 nSlot, SfxCallMode nCall = SFX_CALLMODE_SYNCHRON );
87 		VBAHELPER_DLLPUBLIC sal_Int32 OORGBToXLRGB( sal_Int32 );
88 		VBAHELPER_DLLPUBLIC sal_Int32 XLRGBToOORGB( sal_Int32 );
89 		VBAHELPER_DLLPUBLIC css::uno::Any OORGBToXLRGB( const css::uno::Any& );
90 		VBAHELPER_DLLPUBLIC css::uno::Any XLRGBToOORGB( const css::uno::Any& );
91 		// provide a NULL object that can be passed as variant so that
92 		// the object when passed to IsNull will return true. aNULL
93 		// contains an empty object reference
94 		VBAHELPER_DLLPUBLIC const css::uno::Any& aNULL();
95 		VBAHELPER_DLLPUBLIC void PrintOutHelper( SfxViewShell* pViewShell, const css::uno::Any& From, const css::uno::Any& To, const css::uno::Any& Copies, const css::uno::Any& Preview, const css::uno::Any& ActivePrinter, const css::uno::Any& PrintToFile, const css::uno::Any& Collate, const css::uno::Any& PrToFileName, sal_Bool bSelection  );
96 		VBAHELPER_DLLPUBLIC void PrintPreviewHelper( const css::uno::Any& EnableChanges,  SfxViewShell* );
97 
98         /** Extracts a 32-bit integer value from the passed Any, which may contain an integer or floating-point value.
99             Throws, if the Any is empty or contains an incompatible type. */
100         VBAHELPER_DLLPUBLIC sal_Int32 extractIntFromAny( const css::uno::Any& rAny ) throw (css::uno::RuntimeException);
101         /** Extracts a 32-bit integer value from the passed Any, which may contain an integer or floating-point value.
102             Returns nDefault, if rAny is empty. Throws, if the Any contains an incompatible type. */
103         VBAHELPER_DLLPUBLIC sal_Int32 extractIntFromAny( const css::uno::Any& rAny, sal_Int32 nDefault ) throw (css::uno::RuntimeException);
104 
105         /** Extracts a boolean value from the passed Any, which may contain a Boolean or an integer or floating-point value.
106             Throws, if the Any is empty or contains an incompatible type. */
107         VBAHELPER_DLLPUBLIC bool extractBoolFromAny( const css::uno::Any& rAny ) throw (css::uno::RuntimeException);
108         /** Extracts a boolean value from the passed Any, which may contain a Boolean or an integer or floating-point value.
109             Returns bDefault, if rAny is empty. Throws, if the Any contains an incompatible type. */
110         VBAHELPER_DLLPUBLIC bool extractBoolFromAny( const css::uno::Any& rAny, bool bDefault ) throw (css::uno::RuntimeException);
111 
112         /** Extracts a string from the passed Any, which may contain a Boolean, a value, or a string.
113             Throws, if the Any is empty or contains an incompatible type. */
114         VBAHELPER_DLLPUBLIC ::rtl::OUString extractStringFromAny( const css::uno::Any& rAny, bool bUppercaseBool = false ) throw (css::uno::RuntimeException);
115         /** Extracts a string from the passed Any, which may contain a Boolean, a value, or a string.
116             Returns rDefault, if rAny is empty. Throws, if the Any contains an incompatible type. */
117         VBAHELPER_DLLPUBLIC ::rtl::OUString extractStringFromAny( const css::uno::Any& rAny, const ::rtl::OUString& rDefault, bool bUppercaseBool = false ) throw (css::uno::RuntimeException);
118 
119 		VBAHELPER_DLLPUBLIC rtl::OUString getAnyAsString( const css::uno::Any& pvargItem ) throw ( css::uno::RuntimeException );
120 		VBAHELPER_DLLPUBLIC rtl::OUString VBAToRegexp(const rtl::OUString &rIn, bool bForLike = false); // needs to be in an uno service ( already this code is duplicated in basic )
121 	    VBAHELPER_DLLPUBLIC double getPixelTo100thMillimeterConversionFactor( const css::uno::Reference< css::awt::XDevice >& xDevice, sal_Bool bVertical);
122     	VBAHELPER_DLLPUBLIC double PointsToPixels( const css::uno::Reference< css::awt::XDevice >& xDevice, double fPoints, sal_Bool bVertical);
123 	    VBAHELPER_DLLPUBLIC double PixelsToPoints( const css::uno::Reference< css::awt::XDevice >& xDevice, double fPixels, sal_Bool bVertical);
124     	VBAHELPER_DLLPUBLIC sal_Int32 PointsToHmm( double fPoints );
125 	    VBAHELPER_DLLPUBLIC double HmmToPoints( sal_Int32 nHmm );
126         VBAHELPER_DLLPUBLIC sal_Int32 getPointerStyle( const css::uno::Reference< css::frame::XModel >& );
127         VBAHELPER_DLLPUBLIC void setCursorHelper( const css::uno::Reference< css::frame::XModel >& xModel, const Pointer& rPointer, sal_Bool bOverWrite );
128         VBAHELPER_DLLPUBLIC void setDefaultPropByIntrospection( const css::uno::Any& aObj, const css::uno::Any& aValue  ) throw ( css::uno::RuntimeException );
129         VBAHELPER_DLLPUBLIC css::uno::Any getPropertyValue( const css::uno::Sequence< css::beans::PropertyValue >& aProp, const rtl::OUString& aName );
130         VBAHELPER_DLLPUBLIC sal_Bool setPropertyValue( css::uno::Sequence< css::beans::PropertyValue >& aProp, const rtl::OUString& aName, const css::uno::Any& aValue );
131 
132 class VBAHELPER_DLLPUBLIC Millimeter
133 {
134 //Factor to translate between points and hundredths of millimeters:
135 private:
136 	double m_nMillimeter;
137 
138 public:
139 	Millimeter();
140 
141 	Millimeter(double mm);
142 
143 	void set(double mm);
144 	void setInPoints(double points) ;
145 	void setInHundredthsOfOneMillimeter(double hmm);
146 	double get();
147 	double getInHundredthsOfOneMillimeter();
148 	double getInPoints();
149 	static sal_Int32 getInHundredthsOfOneMillimeter(double points);
150 	static double getInPoints(int _hmm);
151 };
152 
153 class VBAHELPER_DLLPUBLIC AbstractGeometryAttributes // probably should replace the ShapeHelper below
154 {
155 public:
156     virtual ~AbstractGeometryAttributes() {}
157     virtual double getLeft() const = 0;
158     virtual void setLeft( double ) = 0;
159     virtual double getTop() const = 0;
160     virtual void setTop( double ) = 0;
161     virtual double getHeight() const = 0;
162     virtual void setHeight( double ) = 0;
163     virtual double getWidth() const = 0;
164     virtual void setWidth( double ) = 0;
165 
166     virtual double getInnerHeight() const { return 0.0; }
167     virtual void setInnerHeight( double ) {}
168     virtual double getInnerWidth() const { return 0.0; }
169     virtual void setInnerWidth( double ) {}
170     virtual double getOffsetX() const { return 0.0; }
171     virtual double getOffsetY() const { return 0.0; }
172 };
173 
174 namespace msforms {
175     class XShape;
176 }
177 
178 class VBAHELPER_DLLPUBLIC ShapeHelper
179 {
180 protected:
181 	css::uno::Reference< css::drawing::XShape > xShape;
182 public:
183 	ShapeHelper( const css::uno::Reference< css::drawing::XShape >& _xShape) throw (css::script::BasicErrorException );
184 
185 	double getHeight() const;
186    	void setHeight(double _fheight) throw ( css::script::BasicErrorException );
187 	double getWidth() const;
188 	void setWidth(double _fWidth) throw ( css::script::BasicErrorException );
189 	double getLeft() const;
190 	void setLeft(double _fLeft);
191 	double getTop() const;
192 	void setTop(double _fTop);
193 };
194 
195 class VBAHELPER_DLLPUBLIC ConcreteXShapeGeometryAttributes : public AbstractGeometryAttributes
196 {
197     std::auto_ptr< ShapeHelper > m_pShapeHelper;
198 public:
199     ConcreteXShapeGeometryAttributes( const css::uno::Reference< css::uno::XComponentContext >& xContext, const css::uno::Reference< css::drawing::XShape >& xShape );
200     virtual double getLeft() const;
201     virtual void setLeft( double nLeft );
202     virtual double getTop() const;
203     virtual void setTop( double nTop );
204     virtual double getHeight() const;
205     virtual void setHeight( double nHeight );
206     virtual double getWidth() const;
207     virtual void setWidth( double nWidth);
208     virtual ~ConcreteXShapeGeometryAttributes();
209 };
210 
211 #define VBA_LEFT "PositionX"
212 #define VBA_TOP "PositionY"
213 #define VBA_HEIGHT "Height"
214 #define VBA_WIDTH "Width"
215 class VBAHELPER_DLLPUBLIC UserFormGeometryHelper : public AbstractGeometryAttributes
216 {
217 public:
218     UserFormGeometryHelper(
219         const css::uno::Reference< css::uno::XComponentContext >& xContext,
220         const css::uno::Reference< css::awt::XControl >& xControl,
221         double fOffsetX, double fOffsetY );
222     virtual double getLeft() const;
223     virtual void setLeft( double fLeft );
224     virtual double getTop() const;
225     virtual void setTop( double fTop );
226     virtual double getWidth() const;
227     virtual void setWidth( double fWidth );
228     virtual double getHeight() const;
229     virtual void setHeight( double fHeight );
230     virtual double getInnerWidth() const;
231     virtual void setInnerWidth( double fWidth );
232     virtual double getInnerHeight() const;
233     virtual void setInnerHeight( double fHeight );
234     virtual double getOffsetX() const;
235     virtual double getOffsetY() const;
236 
237 private:
238     double implGetPos( bool bPosY ) const;
239     void implSetPos( double fPos, bool bPosY );
240     double implGetSize( bool bHeight, bool bOuter ) const;
241     void implSetSize( double fSize, bool bHeight, bool bOuter );
242 
243 private:
244     css::uno::Reference< css::awt::XWindow > mxWindow;
245     css::uno::Reference< css::beans::XPropertySet > mxModelProps;
246     css::uno::Reference< css::awt::XUnitConversion > mxUnitConv;
247     double mfOffsetX;
248     double mfOffsetY;
249     sal_Bool mbDialog;
250 };
251 
252 class VBAHELPER_DLLPUBLIC ContainerUtilities
253 {
254 
255 public:
256 	static rtl::OUString getUniqueName( const css::uno::Sequence< ::rtl::OUString >&  _slist, const rtl::OUString& _sElementName, const ::rtl::OUString& _sSuffixSeparator);
257 	static rtl::OUString getUniqueName( const css::uno::Sequence< rtl::OUString >& _slist, const rtl::OUString _sElementName, const rtl::OUString& _sSuffixSeparator, sal_Int32 _nStartSuffix );
258 
259 	static sal_Int32 FieldInList( const css::uno::Sequence< rtl::OUString >& SearchList, const rtl::OUString& SearchString );
260 };
261 
262 // really just a a place holder to ease the porting pain
263 class VBAHELPER_DLLPUBLIC DebugHelper
264 {
265 public:
266 	static void exception( const rtl::OUString&  DetailedMessage, const css::uno::Exception& ex,  int err, const rtl::OUString& /*additionalArgument*/ ) throw( css::script::BasicErrorException );
267 
268 	static void exception( int err,  const rtl::OUString& additionalArgument ) throw( css::script::BasicErrorException );
269 
270 	static void exception( css::uno::Exception& ex ) throw( css::script::BasicErrorException );
271 };
272 	} // vba
273 } // ooo
274 
275 namespace ov = ooo::vba;
276 
277 #ifdef DEBUG
278 #  define SC_VBA_FIXME(a) OSL_TRACE( a )
279 #  define SC_VBA_STUB() SC_VBA_FIXME(( "%s - stubbed\n", __FUNCTION__ ))
280 #else
281 #  define SC_VBA_FIXME(a)
282 #  define SC_VBA_STUB()
283 #endif
284 
285 #endif
286