1 /* -*- Mode: C++; tab-width: 4; indent-tabs-mode: nil; c-basic-offset: 4 -*- */
2 /*
3  * This file is part of the LibreOffice project.
4  *
5  * This Source Code Form is subject to the terms of the Mozilla Public
6  * License, v. 2.0. If a copy of the MPL was not distributed with this
7  * file, You can obtain one at http://mozilla.org/MPL/2.0/.
8  *
9  * This file incorporates work covered by the following license notice:
10  *
11  *   Licensed to the Apache Software Foundation (ASF) under one or more
12  *   contributor license agreements. See the NOTICE file distributed
13  *   with this work for additional information regarding copyright
14  *   ownership. The ASF licenses this file to you under the Apache
15  *   License, Version 2.0 (the "License"); you may not use this file
16  *   except in compliance with the License. You may obtain a copy of
17  *   the License at http://www.apache.org/licenses/LICENSE-2.0 .
18  */
19 #ifndef INCLUDED_VBAHELPER_VBAHELPER_HXX
20 #define INCLUDED_VBAHELPER_VBAHELPER_HXX
21 
22 #include <memory>
23 #include <string_view>
24 
25 #include <com/sun/star/lang/IllegalArgumentException.hpp>
26 #include <com/sun/star/uno/Any.hxx>
27 #include <com/sun/star/uno/Reference.hxx>
28 #include <com/sun/star/uno/Sequence.hxx>
29 #include <rtl/ustring.hxx>
30 #include <sal/types.h>
31 #include <tools/color.hxx>
32 #include <vbahelper/vbadllapi.h>
33 #include <vcl/ptrstyle.hxx>
34 #include <vcl/errcode.hxx>
35 
36 namespace com::sun::star {
37     namespace awt { class XControl; }
38     namespace awt { class XDevice; }
39     namespace awt { class XUnitConversion; }
40     namespace awt { class XWindow; }
41     namespace beans { class XIntrospectionAccess; }
42     namespace beans { class XPropertySet; }
43     namespace beans { struct PropertyValue; }
44     namespace drawing { class XShape; }
45     namespace frame { class XModel; }
46     namespace script { class XTypeConverter; }
47     namespace uno { class Exception; }
48     namespace uno { class XComponentContext; }
49 }
50 
51 class SfxObjectShell;
52 class SfxViewFrame;
53 class SfxViewShell;
54 
55 namespace ooo::vba
56     {
57         /// @throws css::lang::IllegalArgumentException
58         template < class T >
getXSomethingFromArgs(css::uno::Sequence<css::uno::Any> const & args,sal_Int32 nPos,bool bCanBeNull=true)59         css::uno::Reference< T > getXSomethingFromArgs( css::uno::Sequence< css::uno::Any > const & args, sal_Int32 nPos, bool bCanBeNull = true )
60         {
61             if ( args.getLength() < ( nPos + 1) )
62                 throw css::lang::IllegalArgumentException();
63             css::uno::Reference< T > aSomething( args[ nPos ], css::uno::UNO_QUERY );
64             if ( !bCanBeNull && !aSomething.is() )
65                 throw css::lang::IllegalArgumentException();
66             return aSomething;
67         }
68 
69         class XHelperInterface;
70 
71         /** Returns the VBA document implementation object representing the passed UNO document model. */
72         VBAHELPER_DLLPUBLIC css::uno::Reference< XHelperInterface > getVBADocument( const css::uno::Reference< css::frame::XModel >& xModel );
73         VBAHELPER_DLLPUBLIC css::uno::Reference< XHelperInterface > getUnoDocModule( std::u16string_view aModName, SfxObjectShell const * pShell );
74         /// @throws css::uno::RuntimeException
75         VBAHELPER_DLLPUBLIC SfxObjectShell* getSfxObjShell( const css::uno::Reference< css::frame::XModel >& xModel );
76 
77         /// @throws css::uno::RuntimeException
78         css::uno::Reference< css::frame::XModel > getCurrentDoc( const OUString& sKey );
79         /// @throws css::uno::RuntimeException
80         VBAHELPER_DLLPUBLIC css::uno::Reference< css::frame::XModel > getThisExcelDoc( const css::uno::Reference< css::uno::XComponentContext >& xContext );
81         /// @throws css::uno::RuntimeException
82         VBAHELPER_DLLPUBLIC css::uno::Reference< css::frame::XModel > getThisWordDoc( const css::uno::Reference< css::uno::XComponentContext >& xContext );
83         /// @throws css::uno::RuntimeException
84         VBAHELPER_DLLPUBLIC css::uno::Reference< css::frame::XModel > getCurrentExcelDoc( const css::uno::Reference< css::uno::XComponentContext >& xContext );
85         /// @throws css::uno::RuntimeException
86         VBAHELPER_DLLPUBLIC css::uno::Reference< css::frame::XModel > getCurrentWordDoc( const css::uno::Reference< css::uno::XComponentContext >& xContext );
87 
88         /// @throws css::uno::RuntimeException
89         VBAHELPER_DLLPUBLIC css::uno::Reference< css::beans::XIntrospectionAccess > getIntrospectionAccess( const css::uno::Any& aObject );
90         /// @throws css::uno::RuntimeException
91         VBAHELPER_DLLPUBLIC css::uno::Reference< css::script::XTypeConverter > const & getTypeConverter( const css::uno::Reference< css::uno::XComponentContext >& xContext );
92 
93         VBAHELPER_DLLPUBLIC void dispatchRequests( const css::uno::Reference< css::frame::XModel>& xModel, const OUString& aUrl );
94         VBAHELPER_DLLPUBLIC void dispatchRequests (const css::uno::Reference< css::frame::XModel>& xModel, const OUString & aUrl, const css::uno::Sequence< css::beans::PropertyValue >& sProps );
95         VBAHELPER_DLLPUBLIC void dispatchExecute(SfxViewShell const * pView, sal_uInt16 nSlot );
96         VBAHELPER_DLLPUBLIC sal_Int32 OORGBToXLRGB( sal_Int32 );
OORGBToXLRGB(::Color n)97         inline sal_Int32 OORGBToXLRGB( ::Color n ) { return OORGBToXLRGB(sal_Int32(n)); }
98         VBAHELPER_DLLPUBLIC sal_Int32 XLRGBToOORGB( sal_Int32 );
99         VBAHELPER_DLLPUBLIC css::uno::Any OORGBToXLRGB( const css::uno::Any& );
100         VBAHELPER_DLLPUBLIC css::uno::Any XLRGBToOORGB( const css::uno::Any& );
101         // provide a NULL object that can be passed as variant so that
102         // the object when passed to IsNull will return true. aNULL
103         // contains an empty object reference
104         VBAHELPER_DLLPUBLIC const css::uno::Any& aNULL();
105         VBAHELPER_DLLPUBLIC void PrintOutHelper( SfxViewShell const * 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, bool bSelection  );
106         VBAHELPER_DLLPUBLIC void PrintPreviewHelper( const css::uno::Any& EnableChanges, SfxViewShell const * );
107         VBAHELPER_DLLPUBLIC void WaitUntilPreviewIsClosed( SfxViewFrame* );
108 
109         /** Extracts a boolean value from the passed Any, which may contain a Boolean or an integer or floating-point value.
110             @throws css::uno::RuntimeException if the Any is empty or contains an incompatible type. */
111         VBAHELPER_DLLPUBLIC bool extractBoolFromAny( const css::uno::Any& rAny );
112 
113         /** Extracts a string from the passed Any, which may contain a Boolean, a value, or a string.
114             @throws css::uno::RuntimeException if the Any is empty or contains an incompatible type. */
115         VBAHELPER_DLLPUBLIC OUString extractStringFromAny( const css::uno::Any& rAny, bool bUppercaseBool = false );
116         /** Extracts a string from the passed Any, which may contain a Boolean, a value, or a string.
117             Returns rDefault, if rAny is empty.
118             @throws css::uno::RuntimeException if the Any contains an incompatible type. */
119         VBAHELPER_DLLPUBLIC OUString extractStringFromAny( const css::uno::Any& rAny, const OUString& rDefault, bool bUppercaseBool );
120 
121         /// @throws css::uno::RuntimeException
122         VBAHELPER_DLLPUBLIC OUString getAnyAsString( const css::uno::Any& pvargItem );
123         VBAHELPER_DLLPUBLIC OUString VBAToRegexp(const OUString &rIn); // needs to be in a uno service ( already this code is duplicated in basic )
124         VBAHELPER_DLLPUBLIC double getPixelTo100thMillimeterConversionFactor( const css::uno::Reference< css::awt::XDevice >& xDevice, bool bVertical);
125         VBAHELPER_DLLPUBLIC double PointsToPixels( const css::uno::Reference< css::awt::XDevice >& xDevice, double fPoints, bool bVertical);
126         VBAHELPER_DLLPUBLIC double PixelsToPoints( const css::uno::Reference< css::awt::XDevice >& xDevice, double fPixels, bool bVertical);
127         VBAHELPER_DLLPUBLIC sal_Int32 PointsToHmm( double fPoints );
128         VBAHELPER_DLLPUBLIC double HmmToPoints( sal_Int32 nHmm );
129         VBAHELPER_DLLPUBLIC PointerStyle getPointerStyle( const css::uno::Reference< css::frame::XModel >& );
130         VBAHELPER_DLLPUBLIC void setCursorHelper( const css::uno::Reference< css::frame::XModel >& xModel, PointerStyle nPointer, bool bOverWrite );
131         /// @throws css::uno::RuntimeException
132         VBAHELPER_DLLPUBLIC void setDefaultPropByIntrospection( const css::uno::Any& aObj, const css::uno::Any& aValue  );
133         VBAHELPER_DLLPUBLIC css::uno::Any getPropertyValue( const css::uno::Sequence< css::beans::PropertyValue >& aProp, const OUString& aName );
134         VBAHELPER_DLLPUBLIC bool setPropertyValue( css::uno::Sequence< css::beans::PropertyValue >& aProp, const OUString& aName, const css::uno::Any& aValue );
135         VBAHELPER_DLLPUBLIC void setOrAppendPropertyValue( css::uno::Sequence< css::beans::PropertyValue >& aProp, const OUString& aName, const css::uno::Any& aValue );
136 
137 class VBAHELPER_DLLPUBLIC Millimeter
138 {
139 //Factor to translate between points and hundredths of millimeters:
140 private:
141     double m_nMillimeter;
142 
143 public:
144     Millimeter();
145 
146     Millimeter(double mm);
147 
148     void setInPoints(double points) ;
149     double getInHundredthsOfOneMillimeter() const;
150     static sal_Int32 getInHundredthsOfOneMillimeter(double points);
151     static double getInPoints(int _hmm);
152 };
153 
154 class VBAHELPER_DLLPUBLIC AbstractGeometryAttributes // probably should replace the ShapeHelper below
155 {
156 public:
~AbstractGeometryAttributes()157     virtual ~AbstractGeometryAttributes() {}
158     virtual double getLeft() const = 0;
159     virtual void setLeft( double ) = 0;
160     virtual double getTop() const = 0;
161     virtual void setTop( double ) = 0;
162     virtual double getHeight() const = 0;
163     virtual void setHeight( double ) = 0;
164     virtual double getWidth() const = 0;
165     virtual void setWidth( double ) = 0;
166 
getInnerHeight() const167     virtual double getInnerHeight() const { return 0.0; }
setInnerHeight(double)168     virtual void setInnerHeight( double ) {}
getInnerWidth() const169     virtual double getInnerWidth() const { return 0.0; }
setInnerWidth(double)170     virtual void setInnerWidth( double ) {}
getOffsetX() const171     virtual double getOffsetX() const { return 0.0; }
getOffsetY() const172     virtual double getOffsetY() const { return 0.0; }
173 };
174 
175 class VBAHELPER_DLLPUBLIC ShapeHelper
176 {
177     css::uno::Reference< css::drawing::XShape > xShape;
178 public:
179     /// @throws css::script::BasicErrorException
180     /// @throws css::uno::RuntimeException
181     ShapeHelper( const css::uno::Reference< css::drawing::XShape >& _xShape);
182 
183     double getHeight() const;
184     void setHeight(double _fheight);
185     double getWidth() const;
186     void setWidth(double _fWidth);
187     double getLeft() const;
188     void setLeft(double _fLeft);
189     double getTop() const;
190     void setTop(double _fTop);
191 };
192 
193 class VBAHELPER_DLLPUBLIC ConcreteXShapeGeometryAttributes final : public AbstractGeometryAttributes
194 {
195     std::unique_ptr< ShapeHelper > m_pShapeHelper;
196 public:
197     ConcreteXShapeGeometryAttributes( const css::uno::Reference< css::drawing::XShape >& xShape );
198     virtual double getLeft() const override;
199     virtual void setLeft( double nLeft ) override;
200     virtual double getTop() const override;
201     virtual void setTop( double nTop ) override;
202     virtual double getHeight() const override;
203     virtual void setHeight( double nHeight ) override;
204     virtual double getWidth() const override;
205     virtual void setWidth( double nWidth) override;
206     virtual ~ConcreteXShapeGeometryAttributes() override;
207 };
208 
209 #define VBA_LEFT "PositionX"
210 #define VBA_TOP "PositionY"
211 
212 class VBAHELPER_DLLPUBLIC UserFormGeometryHelper final : public AbstractGeometryAttributes
213 {
214 public:
215     UserFormGeometryHelper(
216         const css::uno::Reference< css::awt::XControl >& xControl,
217         double fOffsetX, double fOffsetY );
218     virtual double getLeft() const override;
219     virtual void setLeft( double fLeft ) override;
220     virtual double getTop() const override;
221     virtual void setTop( double fTop ) override;
222     virtual double getWidth() const override;
223     virtual void setWidth( double fWidth ) override;
224     virtual double getHeight() const override;
225     virtual void setHeight( double fHeight ) override;
226     virtual double getInnerWidth() const override;
227     virtual void setInnerWidth( double fWidth ) override;
228     virtual double getInnerHeight() const override;
229     virtual void setInnerHeight( double fHeight ) override;
230     virtual double getOffsetX() const override;
231     virtual double getOffsetY() const override;
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     bool mbDialog;
246 };
247 
248 class VBAHELPER_DLLPUBLIC ContainerUtilities
249 {
250 
251 public:
252     static OUString getUniqueName( const css::uno::Sequence< OUString >&  _slist, const OUString& _sElementName, std::u16string_view _sSuffixSeparator);
253     static OUString getUniqueName( const css::uno::Sequence< OUString >& _slist, const OUString& _sElementName, std::u16string_view _sSuffixSeparator, sal_Int32 _nStartSuffix );
254 
255     static sal_Int32 FieldInList( const css::uno::Sequence< OUString >& SearchList, const OUString& SearchString );
256 };
257 
258 // really just a place holder to ease the porting pain
259 class VBAHELPER_DLLPUBLIC DebugHelper
260 {
261 public:
262     /// @throws css::script::BasicErrorException
263     static void basicexception( const css::uno::Exception& ex, ErrCode err, std::u16string_view /*additionalArgument*/ );
264 
265     /// @throws css::script::BasicErrorException
266     static void basicexception( ErrCode err, std::u16string_view additionalArgument );
267 
268     /// @throws css::script::BasicErrorException
269     static void basicexception( const css::uno::Exception& ex );
270 
271     /// @throws css::script::BasicErrorException
272     static void runtimeexception( ErrCode err );
273 };
274 
275 } // ooo::vba
276 
277 namespace ov = ooo::vba;
278 
279 #endif
280 
281 /* vim:set shiftwidth=4 softtabstop=4 expandtab: */
282