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
20 #include "vbastyle.hxx"
21 #include <basic/sberrors.hxx>
22 #include <com/sun/star/style/XStyleFamiliesSupplier.hpp>
23 #include <com/sun/star/beans/XPropertySet.hpp>
24
25 using namespace ::ooo::vba;
26 using namespace ::com::sun::star;
27
28 static const char DISPLAYNAME[] = "DisplayName";
29
30 uno::Reference< container::XNameAccess >
getStylesNameContainer(const uno::Reference<frame::XModel> & xModel)31 ScVbaStyle::getStylesNameContainer( const uno::Reference< frame::XModel >& xModel )
32 {
33 uno::Reference< style::XStyleFamiliesSupplier > xStyleSupplier( xModel, uno::UNO_QUERY_THROW);
34 uno::Reference< container::XNameAccess > xStylesAccess( xStyleSupplier->getStyleFamilies()->getByName("CellStyles"), uno::UNO_QUERY_THROW );
35 return xStylesAccess;
36 }
37
38 /// @throws script::BasicErrorException
39 /// @throws uno::RuntimeException
40 static uno::Reference< beans::XPropertySet >
lcl_getStyleProps(const OUString & sStyleName,const uno::Reference<frame::XModel> & xModel)41 lcl_getStyleProps( const OUString& sStyleName, const uno::Reference< frame::XModel >& xModel )
42 {
43
44 uno::Reference< beans::XPropertySet > xStyleProps( ScVbaStyle::getStylesNameContainer( xModel )->getByName( sStyleName ), uno::UNO_QUERY_THROW );
45 return xStyleProps;
46 }
47
initialise()48 void ScVbaStyle::initialise()
49 {
50 if (!mxModel.is() )
51 DebugHelper::basicexception(ERRCODE_BASIC_METHOD_FAILED, "XModel Interface could not be retrieved" );
52 uno::Reference< lang::XServiceInfo > xServiceInfo( mxPropertySet, uno::UNO_QUERY_THROW );
53 if ( !xServiceInfo->supportsService("com.sun.star.style.CellStyle") )
54 {
55 DebugHelper::basicexception(ERRCODE_BASIC_METHOD_FAILED, OUString() );
56 }
57 mxStyle.set( mxPropertySet, uno::UNO_QUERY_THROW );
58
59 uno::Reference< style::XStyleFamiliesSupplier > xStyleSupplier( mxModel, uno::UNO_QUERY_THROW );
60 mxStyleFamilyNameContainer.set( ScVbaStyle::getStylesNameContainer( mxModel ), uno::UNO_QUERY_THROW );
61
62 }
63
ScVbaStyle(const uno::Reference<ov::XHelperInterface> & xParent,const uno::Reference<uno::XComponentContext> & xContext,const OUString & sStyleName,const uno::Reference<frame::XModel> & _xModel)64 ScVbaStyle::ScVbaStyle( const uno::Reference< ov::XHelperInterface >& xParent,
65 const uno::Reference< uno::XComponentContext > & xContext,
66 const OUString& sStyleName, const uno::Reference< frame::XModel >& _xModel )
67 : ScVbaStyle_BASE( xParent, xContext, lcl_getStyleProps( sStyleName, _xModel ), _xModel, false )
68 {
69 try
70 {
71 initialise();
72 }
73 catch (const uno::Exception& )
74 {
75 DebugHelper::basicexception(ERRCODE_BASIC_METHOD_FAILED, OUString());
76 }
77 }
78
ScVbaStyle(const uno::Reference<XHelperInterface> & xParent,const uno::Reference<uno::XComponentContext> & xContext,const uno::Reference<beans::XPropertySet> & _xPropertySet,const uno::Reference<frame::XModel> & _xModel)79 ScVbaStyle::ScVbaStyle( const uno::Reference< XHelperInterface >& xParent,
80 const uno::Reference< uno::XComponentContext > & xContext,
81 const uno::Reference< beans::XPropertySet >& _xPropertySet,
82 const uno::Reference< frame::XModel >& _xModel )
83 : ScVbaStyle_BASE( xParent, xContext, _xPropertySet, _xModel, false )
84 {
85 try
86 {
87 initialise();
88 }
89 catch (const uno::Exception& )
90 {
91 DebugHelper::basicexception(ERRCODE_BASIC_METHOD_FAILED, OUString());
92 }
93 }
94
95 sal_Bool SAL_CALL
BuiltIn()96 ScVbaStyle::BuiltIn()
97 {
98 return !mxStyle->isUserDefined();
99
100 }
101 void SAL_CALL
setName(const OUString & Name)102 ScVbaStyle::setName( const OUString& Name )
103 {
104 mxStyle->setName(Name);
105 }
106
107 OUString SAL_CALL
getName()108 ScVbaStyle::getName()
109 {
110 return mxStyle->getName();
111 }
112
113 void SAL_CALL
setNameLocal(const OUString & NameLocal)114 ScVbaStyle::setNameLocal( const OUString& NameLocal )
115 {
116 try
117 {
118 mxPropertySet->setPropertyValue(DISPLAYNAME, uno::makeAny( NameLocal ) );
119 }
120 catch (const uno::Exception& e)
121 {
122 DebugHelper::basicexception(e);
123 }
124 }
125
126 OUString SAL_CALL
getNameLocal()127 ScVbaStyle::getNameLocal()
128 {
129 OUString sName;
130 try
131 {
132 mxPropertySet->getPropertyValue(DISPLAYNAME) >>= sName;
133 }
134 catch (const uno::Exception& )
135 {
136 DebugHelper::basicexception(ERRCODE_BASIC_METHOD_FAILED, OUString() );
137 }
138 return sName;
139 }
140
141 void SAL_CALL
Delete()142 ScVbaStyle::Delete()
143 {
144 try
145 {
146 mxStyleFamilyNameContainer->removeByName(mxStyle->getName());
147 }
148 catch (const uno::Exception& )
149 {
150 DebugHelper::basicexception(ERRCODE_BASIC_METHOD_FAILED, OUString());
151 }
152 }
153
154 void SAL_CALL
setMergeCells(const uno::Any &)155 ScVbaStyle::setMergeCells( const uno::Any& /*MergeCells*/ )
156 {
157 DebugHelper::basicexception(ERRCODE_BASIC_NOT_IMPLEMENTED, OUString());
158 }
159
160 uno::Any SAL_CALL
getMergeCells()161 ScVbaStyle::getMergeCells( )
162 {
163 DebugHelper::basicexception(ERRCODE_BASIC_NOT_IMPLEMENTED, OUString());
164 return uno::Any();
165 }
166
167 OUString
getServiceImplName()168 ScVbaStyle::getServiceImplName()
169 {
170 return "ScVbaStyle";
171 }
172
173 uno::Sequence< OUString >
getServiceNames()174 ScVbaStyle::getServiceNames()
175 {
176 static uno::Sequence< OUString > const aServiceNames
177 {
178 "ooo.vba.excel.XStyle"
179 };
180 return aServiceNames;
181 }
182
183 /* vim:set shiftwidth=4 softtabstop=4 expandtab: */
184