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 <vbahelper/helperdecl.hxx> 28 #include "vbaglobals.hxx" 29 30 #include <comphelper/unwrapargs.hxx> 31 32 #include <com/sun/star/lang/XMultiComponentFactory.hpp> 33 #include <com/sun/star/beans/XPropertySet.hpp> 34 #include <cppuhelper/component_context.hxx> 35 36 #include "vbaapplication.hxx" 37 #include "vbaworksheet.hxx" 38 #include "vbarange.hxx" 39 #include <cppuhelper/bootstrap.hxx> 40 using namespace ::com::sun::star; 41 using namespace ::com::sun::star::uno; 42 using namespace ::ooo::vba; 43 44 45 46 // ============================================================================= 47 // ScVbaGlobals 48 // ============================================================================= 49 50 //ScVbaGlobals::ScVbaGlobals( css::uno::Reference< css::uno::XComponentContext >const& rxContext, ) : ScVbaGlobals_BASE( uno::Reference< XHelperInterface >(), rxContext ) 51 rtl::OUString sDocCtxName( RTL_CONSTASCII_USTRINGPARAM("ExcelDocumentContext") ); 52 53 ScVbaGlobals::ScVbaGlobals( uno::Sequence< uno::Any > const& aArgs, uno::Reference< uno::XComponentContext >const& rxContext ) : ScVbaGlobals_BASE( uno::Reference< XHelperInterface >(), rxContext, sDocCtxName ) 54 { 55 OSL_TRACE("ScVbaGlobals::ScVbaGlobals()"); 56 57 uno::Sequence< beans::PropertyValue > aInitArgs( 2 ); 58 aInitArgs[ 0 ].Name = rtl::OUString::createFromAscii("Application"); 59 aInitArgs[ 0 ].Value = uno::makeAny( getApplication() ); 60 aInitArgs[ 1 ].Name = sDocCtxName; 61 aInitArgs[ 1 ].Value = uno::makeAny( getXSomethingFromArgs< frame::XModel >( aArgs, 0 ) ); 62 63 init( aInitArgs ); 64 } 65 66 ScVbaGlobals::~ScVbaGlobals() 67 { 68 OSL_TRACE("ScVbaGlobals::~ScVbaGlobals"); 69 } 70 71 // ============================================================================= 72 // XGlobals 73 // ============================================================================= 74 uno::Reference<excel::XApplication > 75 ScVbaGlobals::getApplication() throw (uno::RuntimeException) 76 { 77 // OSL_TRACE("In ScVbaGlobals::getApplication"); 78 if ( !mxApplication.is() ) 79 mxApplication.set( new ScVbaApplication( mxContext) ); 80 return mxApplication; 81 } 82 83 84 uno::Reference<excel::XApplication > SAL_CALL 85 ScVbaGlobals::getExcel() throw (uno::RuntimeException) 86 { 87 return getApplication(); 88 } 89 90 91 92 uno::Reference< excel::XWorkbook > SAL_CALL 93 ScVbaGlobals::getActiveWorkbook() throw (uno::RuntimeException) 94 { 95 // OSL_TRACE("In ScVbaGlobals::getActiveWorkbook"); 96 uno::Reference< excel::XWorkbook > xWorkbook( getApplication()->getActiveWorkbook(), uno::UNO_QUERY); 97 if ( xWorkbook.is() ) 98 { 99 return xWorkbook; 100 } 101 // FIXME check if this is correct/desired behavior 102 throw uno::RuntimeException( rtl::OUString::createFromAscii( 103 "No activeWorkbook available" ), Reference< uno::XInterface >() ); 104 } 105 106 107 uno::Reference< excel::XWindow > SAL_CALL 108 ScVbaGlobals::getActiveWindow() throw (uno::RuntimeException) 109 { 110 return getApplication()->getActiveWindow(); 111 } 112 113 uno::Reference< excel::XWorksheet > SAL_CALL 114 ScVbaGlobals::getActiveSheet() throw (uno::RuntimeException) 115 { 116 return getApplication()->getActiveSheet(); 117 } 118 119 uno::Any SAL_CALL 120 ScVbaGlobals::WorkBooks( const uno::Any& aIndex ) throw (uno::RuntimeException) 121 { 122 return uno::Any( getApplication()->Workbooks(aIndex) ); 123 } 124 125 uno::Any SAL_CALL 126 ScVbaGlobals::WorkSheets(const uno::Any& aIndex) throw (uno::RuntimeException) 127 { 128 return getApplication()->Worksheets( aIndex ); 129 } 130 uno::Any SAL_CALL 131 ScVbaGlobals::Sheets( const uno::Any& aIndex ) throw (uno::RuntimeException) 132 { 133 return WorkSheets( aIndex ); 134 } 135 136 uno::Any SAL_CALL 137 ScVbaGlobals::Range( const uno::Any& Cell1, const uno::Any& Cell2 ) throw (uno::RuntimeException) 138 { 139 return getApplication()->Range( Cell1, Cell2 ); 140 } 141 142 uno::Any SAL_CALL 143 ScVbaGlobals::Names( const css::uno::Any& aIndex ) throw ( uno::RuntimeException ) 144 { 145 return getApplication()->Names( aIndex ); 146 } 147 148 uno::Reference< excel::XRange > SAL_CALL 149 ScVbaGlobals::getActiveCell() throw (uno::RuntimeException) 150 { 151 return getApplication()->getActiveCell(); 152 } 153 154 uno::Reference< XAssistant > SAL_CALL 155 ScVbaGlobals::getAssistant() throw (uno::RuntimeException) 156 { 157 return getApplication()->getAssistant(); 158 } 159 160 uno::Any SAL_CALL 161 ScVbaGlobals::getSelection() throw (uno::RuntimeException) 162 { 163 return getApplication()->getSelection(); 164 } 165 166 uno::Reference< excel::XWorkbook > SAL_CALL 167 ScVbaGlobals::getThisWorkbook() throw (uno::RuntimeException) 168 { 169 return getApplication()->getThisWorkbook(); 170 } 171 void SAL_CALL 172 ScVbaGlobals::Calculate() throw (::com::sun::star::script::BasicErrorException, ::com::sun::star::uno::RuntimeException) 173 { 174 return getApplication()->Calculate(); 175 } 176 177 uno::Reference< excel::XRange > SAL_CALL 178 ScVbaGlobals::Cells( const uno::Any& RowIndex, const uno::Any& ColumnIndex ) throw (uno::RuntimeException) 179 { 180 return getApplication()->getActiveSheet()->Cells( RowIndex, ColumnIndex ); 181 } 182 uno::Reference< excel::XRange > SAL_CALL 183 ScVbaGlobals::Columns( const uno::Any& aIndex ) throw (uno::RuntimeException) 184 { 185 return getApplication()->getActiveSheet()->Columns( aIndex ); 186 } 187 188 uno::Any SAL_CALL 189 ScVbaGlobals::CommandBars( const uno::Any& aIndex ) throw (uno::RuntimeException) 190 { 191 uno::Reference< XApplicationBase > xBase( getApplication(), uno::UNO_QUERY_THROW ); 192 return xBase->CommandBars( aIndex ); 193 } 194 195 css::uno::Reference< ov::excel::XRange > SAL_CALL 196 ScVbaGlobals::Union( const css::uno::Reference< ov::excel::XRange >& Arg1, const css::uno::Reference< ov::excel::XRange >& Arg2, const css::uno::Any& Arg3, const css::uno::Any& Arg4, const css::uno::Any& Arg5, const css::uno::Any& Arg6, const css::uno::Any& Arg7, const css::uno::Any& Arg8, const css::uno::Any& Arg9, const css::uno::Any& Arg10, const css::uno::Any& Arg11, const css::uno::Any& Arg12, const css::uno::Any& Arg13, const css::uno::Any& Arg14, const css::uno::Any& Arg15, const css::uno::Any& Arg16, const css::uno::Any& Arg17, const css::uno::Any& Arg18, const css::uno::Any& Arg19, const css::uno::Any& Arg20, const css::uno::Any& Arg21, const css::uno::Any& Arg22, const css::uno::Any& Arg23, const css::uno::Any& Arg24, const css::uno::Any& Arg25, const css::uno::Any& Arg26, const css::uno::Any& Arg27, const css::uno::Any& Arg28, const css::uno::Any& Arg29, const css::uno::Any& Arg30 ) throw (css::script::BasicErrorException, css::uno::RuntimeException) 197 { 198 return getApplication()->Union( Arg1, Arg2, Arg3, Arg4, Arg5, Arg6, Arg7, Arg8, Arg9, Arg10, Arg11, Arg12, Arg13, Arg14, Arg15, Arg16, Arg17, Arg18, Arg19, Arg20, Arg21, Arg22, Arg23, Arg24, Arg25, Arg26, Arg27, Arg28, Arg29, Arg30 ); 199 } 200 css::uno::Reference< ov::excel::XRange > SAL_CALL 201 ScVbaGlobals::Intersect( const css::uno::Reference< ov::excel::XRange >& Arg1, const css::uno::Reference< ov::excel::XRange >& Arg2, const css::uno::Any& Arg3, const css::uno::Any& Arg4, const css::uno::Any& Arg5, const css::uno::Any& Arg6, const css::uno::Any& Arg7, const css::uno::Any& Arg8, const css::uno::Any& Arg9, const css::uno::Any& Arg10, const css::uno::Any& Arg11, const css::uno::Any& Arg12, const css::uno::Any& Arg13, const css::uno::Any& Arg14, const css::uno::Any& Arg15, const css::uno::Any& Arg16, const css::uno::Any& Arg17, const css::uno::Any& Arg18, const css::uno::Any& Arg19, const css::uno::Any& Arg20, const css::uno::Any& Arg21, const css::uno::Any& Arg22, const css::uno::Any& Arg23, const css::uno::Any& Arg24, const css::uno::Any& Arg25, const css::uno::Any& Arg26, const css::uno::Any& Arg27, const css::uno::Any& Arg28, const css::uno::Any& Arg29, const css::uno::Any& Arg30 ) throw (css::script::BasicErrorException, css::uno::RuntimeException) 202 { 203 return getApplication()->Intersect( Arg1, Arg2, Arg3, Arg4, Arg5, Arg6, Arg7, Arg8, Arg9, Arg10, Arg11, Arg12, Arg13, Arg14, Arg15, Arg16, Arg17, Arg18, Arg19, Arg20, Arg21, Arg22, Arg23, Arg24, Arg25, Arg26, Arg27, Arg28, Arg29, Arg30 ); 204 } 205 206 uno::Any SAL_CALL 207 ScVbaGlobals::Evaluate( const ::rtl::OUString& Name ) throw (uno::RuntimeException) 208 { 209 return getApplication()->Evaluate( Name ); 210 } 211 212 css::uno::Any SAL_CALL 213 ScVbaGlobals::WorksheetFunction( ) throw (css::uno::RuntimeException) 214 { 215 return getApplication()->WorksheetFunction(); 216 } 217 218 uno::Any SAL_CALL 219 ScVbaGlobals::Windows( const uno::Any& aIndex ) throw (uno::RuntimeException) 220 { 221 return getApplication()->Windows( aIndex ); 222 } 223 224 uno::Reference< excel::XRange > SAL_CALL 225 ScVbaGlobals::Rows( const uno::Any& aIndex ) throw (uno::RuntimeException) 226 { 227 return getApplication()->getActiveSheet()->Rows( aIndex ); 228 229 } 230 231 232 uno::Any SAL_CALL 233 ScVbaGlobals::getDebug() throw (uno::RuntimeException) 234 { 235 try // return empty object on error 236 { 237 uno::Reference< lang::XMultiComponentFactory > xServiceManager( mxContext->getServiceManager(), uno::UNO_SET_THROW ); 238 uno::Reference< uno::XInterface > xVBADebug = xServiceManager->createInstanceWithContext( 239 ::rtl::OUString( RTL_CONSTASCII_USTRINGPARAM( "ooo.vba.Debug" ) ), mxContext ); 240 return uno::Any( xVBADebug ); 241 } 242 catch( uno::Exception& ) 243 { 244 } 245 return uno::Any(); 246 } 247 248 uno::Sequence< ::rtl::OUString > SAL_CALL 249 ScVbaGlobals::getAvailableServiceNames( ) throw (uno::RuntimeException) 250 { 251 static bool bInit = false; 252 static uno::Sequence< rtl::OUString > serviceNames( ScVbaGlobals_BASE::getAvailableServiceNames() ); 253 if ( !bInit ) 254 { 255 rtl::OUString names[] = { 256 ::rtl::OUString( RTL_CONSTASCII_USTRINGPARAM ( "ooo.vba.excel.Range" ) ), 257 ::rtl::OUString( RTL_CONSTASCII_USTRINGPARAM ( "ooo.vba.excel.Workbook" ) ), 258 ::rtl::OUString( RTL_CONSTASCII_USTRINGPARAM ( "ooo.vba.excel.Window" ) ), 259 ::rtl::OUString( RTL_CONSTASCII_USTRINGPARAM ( "ooo.vba.excel.Worksheet" ) ), 260 ::rtl::OUString( RTL_CONSTASCII_USTRINGPARAM ( "ooo.vba.excel.Application" ) ), 261 ::rtl::OUString( RTL_CONSTASCII_USTRINGPARAM ( "ooo.vba.excel.Hyperlink" ) ), 262 ::rtl::OUString( RTL_CONSTASCII_USTRINGPARAM ( "com.sun.star.script.vba.VBASpreadsheetEventProcessor" ) ) 263 }; 264 sal_Int32 nExcelServices = ( sizeof( names )/ sizeof( names[0] ) ); 265 sal_Int32 startIndex = serviceNames.getLength(); 266 serviceNames.realloc( serviceNames.getLength() + nExcelServices ); 267 for ( sal_Int32 index = 0; index < nExcelServices; ++index ) 268 serviceNames[ startIndex + index ] = names[ index ]; 269 bInit = true; 270 } 271 return serviceNames; 272 } 273 274 rtl::OUString& 275 ScVbaGlobals::getServiceImplName() 276 { 277 static rtl::OUString sImplName( RTL_CONSTASCII_USTRINGPARAM("ScVbaGlobals") ); 278 return sImplName; 279 } 280 281 uno::Sequence< rtl::OUString > 282 ScVbaGlobals::getServiceNames() 283 { 284 static uno::Sequence< rtl::OUString > aServiceNames; 285 if ( aServiceNames.getLength() == 0 ) 286 { 287 aServiceNames.realloc( 1 ); 288 aServiceNames[ 0 ] = rtl::OUString( RTL_CONSTASCII_USTRINGPARAM("ooo.vba.excel.Globals" ) ); 289 } 290 return aServiceNames; 291 } 292 293 namespace globals 294 { 295 namespace sdecl = comphelper::service_decl; 296 sdecl::vba_service_class_<ScVbaGlobals, sdecl::with_args<true> > serviceImpl; 297 extern sdecl::ServiceDecl const serviceDecl( 298 serviceImpl, 299 "ScVbaGlobals", 300 "ooo.vba.excel.Globals" ); 301 } 302 303