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