xref: /trunk/main/sc/source/ui/vba/vbastyles.cxx (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 #include "vbastyles.hxx"
28 #include "vbastyle.hxx"
29 #include <ooo/vba/excel/XRange.hpp>
30 
31 using namespace ::ooo::vba;
32 using namespace ::com::sun::star;
33 
34 static rtl::OUString SDEFAULTCELLSTYLENAME( RTL_CONSTASCII_USTRINGPARAM("Default") );
35 css::uno::Any
36 lcl_createAPIStyleToVBAObject( const css::uno::Any& aObject, const uno::Reference< XHelperInterface >& xParent, const uno::Reference< uno::XComponentContext >& xContext, const uno::Reference< frame::XModel >& xModel )
37 {
38     uno::Reference< beans::XPropertySet > xStyleProps( aObject, uno::UNO_QUERY_THROW );
39     uno::Reference< excel::XStyle > xStyle( new ScVbaStyle( xParent, xContext, xStyleProps, xModel ) );
40     return uno::makeAny( xStyle );
41 }
42 
43 
44 ScVbaStyles::ScVbaStyles( const uno::Reference< XHelperInterface >& xParent, const uno::Reference< css::uno::XComponentContext > & xContext, const uno::Reference< frame::XModel >& xModel ) throw ( script::BasicErrorException ) : ScVbaStyles_BASE( xParent, xContext, uno::Reference< container::XIndexAccess >( ScVbaStyle::getStylesNameContainer( xModel ), uno::UNO_QUERY_THROW ) ), mxModel( xModel ), mxParent( xParent )
45 {
46     try
47     {
48         mxMSF.set( mxModel, uno::UNO_QUERY_THROW );
49         mxNameContainerCellStyles.set( m_xNameAccess, uno::UNO_QUERY_THROW );
50     }
51     catch (uno::Exception& )
52     {
53         DebugHelper::exception(SbERR_METHOD_FAILED, rtl::OUString());
54     }
55 }
56 
57 uno::Sequence< rtl::OUString >
58 ScVbaStyles::getStyleNames() throw ( uno::RuntimeException )
59 {
60     return mxNameContainerCellStyles->getElementNames();
61 }
62 
63 
64 uno::Any
65 ScVbaStyles::createCollectionObject(const uno::Any& aObject)
66 {
67     return lcl_createAPIStyleToVBAObject( aObject, mxParent, mxContext, mxModel );
68 }
69 
70 uno::Type SAL_CALL
71 ScVbaStyles::getElementType() throw (uno::RuntimeException)
72 {
73     return excel::XStyle::static_type(0);
74 }
75 
76 class EnumWrapper : public EnumerationHelper_BASE
77 {
78         uno::Reference<container::XIndexAccess > m_xIndexAccess;
79         uno::Reference<XHelperInterface > m_xParent;
80         uno::Reference<uno::XComponentContext > m_xContext;
81         uno::Reference<frame::XModel > m_xModel;
82 
83         sal_Int32 nIndex;
84 public:
85         EnumWrapper( const uno::Reference< container::XIndexAccess >& xIndexAccess, const uno::Reference<XHelperInterface >& xParent, const uno::Reference<uno::XComponentContext >& xContext, const uno::Reference<frame::XModel >& xModel ) : m_xIndexAccess( xIndexAccess ), m_xParent( xParent ), m_xContext( xContext ), m_xModel( xModel ), nIndex( 0 ) {}
86         virtual ::sal_Bool SAL_CALL hasMoreElements(  ) throw (uno::RuntimeException)
87         {
88                 return ( nIndex < m_xIndexAccess->getCount() );
89         }
90         virtual uno::Any SAL_CALL nextElement(  ) throw (container::NoSuchElementException, lang::WrappedTargetException, uno::RuntimeException)
91         {
92                 if ( nIndex < m_xIndexAccess->getCount() )
93                         return lcl_createAPIStyleToVBAObject( m_xIndexAccess->getByIndex( nIndex++ ), m_xParent, m_xContext, m_xModel );
94                 throw container::NoSuchElementException();
95         }
96 };
97 
98 uno::Reference< container::XEnumeration > SAL_CALL
99 ScVbaStyles::createEnumeration() throw (uno::RuntimeException)
100 {
101     return new EnumWrapper( m_xIndexAccess, mxParent, mxContext, mxModel );
102 }
103 
104 uno::Reference< excel::XStyle > SAL_CALL
105 ScVbaStyles::Add( const ::rtl::OUString& _sName, const uno::Any& _aBasedOn ) throw (script::BasicErrorException, uno::RuntimeException)
106 {
107     uno::Reference< excel::XStyle > aRet;
108     try
109     {
110         rtl::OUString sParentCellStyleName( RTL_CONSTASCII_USTRINGPARAM("Default"));
111         if ( _aBasedOn.hasValue() )
112         {
113             uno::Reference< excel::XRange > oRange;
114             if ( _aBasedOn >>= oRange)
115             {
116                 uno::Reference< excel::XStyle > oStyle( oRange->getStyle(), uno::UNO_QUERY_THROW );
117                 if ( oStyle.is() )
118                 {
119                     sParentCellStyleName = oStyle->getName();
120                 }
121                 else
122                 {
123                     DebugHelper::exception(SbERR_BAD_ARGUMENT, rtl::OUString() );
124                 }
125             }
126             else
127             {
128                 DebugHelper::exception(SbERR_BAD_ARGUMENT, rtl::OUString());
129             }
130         }
131 
132         uno::Reference< style::XStyle > xStyle( mxMSF->createInstance( rtl::OUString( RTL_CONSTASCII_USTRINGPARAM("com.sun.star.style.CellStyle"))), uno::UNO_QUERY_THROW );
133 
134         if (!mxNameContainerCellStyles->hasByName(_sName))
135         {
136             mxNameContainerCellStyles->insertByName(_sName, uno::makeAny( xStyle) );
137         }
138         if (!sParentCellStyleName.equals(SDEFAULTCELLSTYLENAME))
139         {
140             xStyle->setParentStyle( sParentCellStyleName );
141         }
142         aRet.set( Item( uno::makeAny( _sName ), uno::Any() ), uno::UNO_QUERY_THROW );
143     }
144     catch (uno::Exception& )
145     {
146         DebugHelper::exception(SbERR_METHOD_FAILED, rtl::OUString());
147     }
148     return aRet;
149 }
150 
151 void
152 ScVbaStyles::Delete(const rtl::OUString _sStyleName) throw ( script::BasicErrorException )
153 {
154     try
155     {
156         if (mxNameContainerCellStyles->hasByName( _sStyleName ) )
157             mxNameContainerCellStyles->removeByName( _sStyleName );
158     }
159     catch (uno::Exception& )
160     {
161         DebugHelper::exception(SbERR_METHOD_FAILED, rtl::OUString());
162     }
163 }
164 
165 rtl::OUString&
166 ScVbaStyles::getServiceImplName()
167 {
168         static rtl::OUString sImplName( RTL_CONSTASCII_USTRINGPARAM("ScVbaStyles") );
169         return sImplName;
170 }
171 
172 uno::Sequence< rtl::OUString >
173 ScVbaStyles::getServiceNames()
174 {
175         static uno::Sequence< rtl::OUString > aServiceNames;
176         if ( aServiceNames.getLength() == 0 )
177         {
178                 aServiceNames.realloc( 1 );
179                 aServiceNames[ 0 ] = rtl::OUString( RTL_CONSTASCII_USTRINGPARAM("ooo.vba.excel.XStyles" ) );
180         }
181         return aServiceNames;
182 }
183