update dev300-m57
[ooovba.git] / sc / workben / addin.cxx
blob571454f2584efe5f7d8d67a1d21027a9cf3e6a26
1 /*************************************************************************
3 * DO NOT ALTER OR REMOVE COPYRIGHT NOTICES OR THIS FILE HEADER.
4 *
5 * Copyright 2008 by Sun Microsystems, Inc.
7 * OpenOffice.org - a multi-platform office productivity suite
9 * $RCSfile: addin.cxx,v $
10 * $Revision: 1.4 $
12 * This file is part of OpenOffice.org.
14 * OpenOffice.org is free software: you can redistribute it and/or modify
15 * it under the terms of the GNU Lesser General Public License version 3
16 * only, as published by the Free Software Foundation.
18 * OpenOffice.org is distributed in the hope that it will be useful,
19 * but WITHOUT ANY WARRANTY; without even the implied warranty of
20 * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
21 * GNU Lesser General Public License version 3 for more details
22 * (a copy is included in the LICENSE file that accompanied this code).
24 * You should have received a copy of the GNU Lesser General Public License
25 * version 3 along with OpenOffice.org. If not, see
26 * <http://www.openoffice.org/license.html>
27 * for a copy of the LGPLv3 License.
29 ************************************************************************/
31 #ifdef _MSC_VER
32 #pragma hdrstop
33 #endif
35 #include <usr/factoryhlp.hxx>
36 #include <usr/macros.hxx>
37 #include <usr/reflserv.hxx>
38 #include <vos/mutex.hxx>
39 #include <vcl/svapp.hxx>
40 #include <tools/debug.hxx>
41 #include <tools/date.hxx>
42 #include <svtools/itemprop.hxx>
43 #include <usr/proptypehlp.hxx>
44 #include <cppuhelper/factory.hxx>
45 #include <uno/lbnames.h>
46 #include <osl/diagnose.h>
48 #include <com/sun/star/util/date.hpp>
50 #include "addin.hxx"
51 #include "result.hxx"
53 using namespace com::sun::star;
55 //------------------------------------------------------------------------
58 //------------------------------------------------------------------------
60 SMART_UNO_IMPLEMENTATION( ScTestAddIn, UsrObject );
62 #define SCADDIN_SERVICE L"com.sun.star.sheet.AddIn"
63 #define SCTESTADDIN_SERVICE L"stardiv.one.sheet.DemoAddIn"
65 //------------------------------------------------------------------------
67 extern "C" {
69 void SAL_CALL component_getImplementationEnvironment(
70 const sal_Char ** ppEnvTypeName, uno_Environment ** ppEnv )
72 *ppEnvTypeName = CPPU_CURRENT_LANGUAGE_BINDING_NAME;
75 sal_Bool SAL_CALL component_writeInfo(
76 void * pServiceManager, registry::XRegistryKey * pRegistryKey )
78 if (pRegistryKey)
80 try
82 UString aImpl = L"/";
83 aImpl += ScTestAddIn::getImplementationName_Static();
84 aImpl += L"/UNO/SERVICES";
86 uno::Reference<registry::XRegistryKey> xNewKey(
87 reinterpret_cast<registry::XRegistryKey*>(pRegistryKey)->createKey(aImpl) );
89 uno::Sequence <rtl::OUString> aSequ = ScTestAddIn::getSupportedServiceNames_Static();
90 const rtl::OUString * pArray = aSequ.getConstArray();
91 for( INT32 i = 0; i < aSequ.getLength(); i++ )
92 xNewKey->createKey( pArray[i] );
94 return sal_True;
96 catch (registry::InvalidRegistryException&)
98 OSL_ENSHURE( sal_False, "### InvalidRegistryException!" );
101 return sal_False;
104 void * SAL_CALL component_getFactory(
105 const sal_Char * pImplName, void * pServiceManager, void * pRegistryKey )
107 void* pRet = 0;
109 if ( pServiceManager && UString(pImplName) == ScTestAddIn::getImplementationName_Static() )
111 uno::Reference<lang::XSingleServiceFactory> xFactory( cppu::createOneInstanceFactory(
112 reinterpret_cast<lang::XMultiServiceFactory*>(pServiceManager),
113 ScTestAddIn::getImplementationName_Static(),
114 ScTestAddIn_CreateInstance,
115 ScTestAddIn::getSupportedServiceNames_Static() ) );
117 if (xFactory.is())
119 xFactory->acquire();
120 pRet = xFactory.get();
124 return pRet;
127 } // extern C
129 //------------------------------------------------------------------------
131 ScTestAddIn::ScTestAddIn()
135 ScTestAddIn::~ScTestAddIn()
139 UString ScTestAddIn::getImplementationName_Static()
141 return L"stardiv.StarCalc.ScTestAddIn";
144 ::com::sun::star::uno::Sequence< ::rtl::OUString > ScTestAddIn::getSupportedServiceNames_Static()
146 uno::Sequence< rtl::OUString > aRet(2);
147 rtl::OUString* pArray = aRet.getArray();
148 pArray[0] = SCADDIN_SERVICE;
149 pArray[1] = SCTESTADDIN_SERVICE;
150 return aRet;
153 uno::Reference<uno::XInterface> ScTestAddIn_CreateInstance(
154 const uno::Reference<lang::XMultiServiceFactory>& )
156 static uno::Reference<uno::XInterface> xInst = (cppu::OWeakObject*)new ScTestAddIn();
157 return xInst;
161 // XAddIn
163 UString ScTestAddIn::getProgrammaticFuntionName(const UString& aDisplayName)
164 THROWS( (UsrSystemException) )
166 //!...
167 return UString();
170 UString ScTestAddIn::getDisplayFunctionName(const UString& aProgrammaticName)
171 THROWS( (UsrSystemException) )
173 // return translated strings
175 UString aRet;
176 if ( aProgrammaticName == L"countParams" ) aRet = L"ParamAnzahl";
177 else if ( aProgrammaticName == L"addOne" ) aRet = L"PlusEins";
178 else if ( aProgrammaticName == L"repeatStr" ) aRet = L"WiederholeString";
179 else if ( aProgrammaticName == L"getDateString" ) aRet = L"Datumsstring";
180 else if ( aProgrammaticName == L"getColorValue" ) aRet = L"Farbwert";
181 else if ( aProgrammaticName == L"transpose" ) aRet = L"Transponieren";
182 else if ( aProgrammaticName == L"transposeInt" ) aRet = L"IntegerTransponieren";
183 else if ( aProgrammaticName == L"repeatMultiple" )aRet = L"Mehrfach";
184 else if ( aProgrammaticName == L"getStrOrVal" ) aRet = L"StringOderWert";
185 else if ( aProgrammaticName == L"callAsync" ) aRet = L"Asynchron";
186 return aRet;
189 UString ScTestAddIn::getFunctionDescription(const UString& aProgrammaticName)
190 THROWS( (UsrSystemException) )
192 // return translated strings
194 UString aRet;
195 if ( aProgrammaticName == L"countParams" ) aRet = L"Gibt die Anzahl der Parameter zurueck.";
196 else if ( aProgrammaticName == L"addOne" ) aRet = L"Addiert 1 zur uebergebenen Zahl.";
197 else if ( aProgrammaticName == L"repeatStr" ) aRet = L"Wiederholt eine Zeichenkette.";
198 else if ( aProgrammaticName == L"getDateString" ) aRet = L"Wandelt ein Datum in eine Zeichenkette.";
199 else if ( aProgrammaticName == L"getColorValue" ) aRet = L"Gibt den Farbwert eines Zellbereichs zurueck. Bei transparentem Hintergrund wird -1 zurueckgegeben";
200 else if ( aProgrammaticName == L"transpose" ) aRet = L"Transponiert eine Matrix.";
201 else if ( aProgrammaticName == L"transposeInt" ) aRet = L"Transponiert eine Matrix mit Ganzzahlen.";
202 else if ( aProgrammaticName == L"repeatMultiple" )aRet = L"Wiederholt mehrere Bestandteile.";
203 else if ( aProgrammaticName == L"getStrOrVal" ) aRet = L"Gibt einen String oder einen Wert zurueck.";
204 else if ( aProgrammaticName == L"callAsync" ) aRet = L"Test fuer asynchrone Funktion.";
205 return aRet;
208 UString ScTestAddIn::getDisplayArgumentName(const UString& aProgrammaticFunctionName,
209 INT32 nArgument) THROWS( (UsrSystemException) )
211 // return translated strings
213 UString aRet;
214 if ( aProgrammaticFunctionName == L"countParams" )
216 if ( nArgument == 0 ) aRet = L"Parameter";
218 else if ( aProgrammaticFunctionName == L"addOne" )
220 if ( nArgument == 0 ) aRet = L"Wert";
222 else if ( aProgrammaticFunctionName == L"repeatStr" )
224 if ( nArgument == 0 ) aRet = L"String";
225 else if ( nArgument == 1 ) aRet = L"Anzahl";
227 else if ( aProgrammaticFunctionName == L"getDateString" )
229 if ( nArgument == 0 ) aRet = L"Dokument";
230 else if ( nArgument == 1 ) aRet = L"Wert";
232 else if ( aProgrammaticFunctionName == L"getColorValue" )
234 if ( nArgument == 0 ) aRet = L"Bereich";
236 else if ( aProgrammaticFunctionName == L"transpose" )
238 if ( nArgument == 0 ) aRet = L"Matrix";
240 else if ( aProgrammaticFunctionName == L"transposeInt" )
242 if ( nArgument == 0 ) aRet = L"Matrix";
244 else if ( aProgrammaticFunctionName == L"repeatMultiple" )
246 if ( nArgument == 0 ) aRet = L"Anzahl";
247 else if ( nArgument == 1 ) aRet = L"Trenner";
248 else if ( nArgument == 2 ) aRet = L"Inhalt";
250 else if ( aProgrammaticFunctionName == L"getStrOrVal" )
252 if ( nArgument == 0 ) aRet = L"Flag";
254 else if ( aProgrammaticFunctionName == L"callAsync" )
256 if ( nArgument == 0 ) aRet = L"Name";
258 return aRet;
261 UString ScTestAddIn::getArgumentDescription(const UString& aProgrammaticFunctionName,
262 INT32 nArgument) THROWS( (UsrSystemException) )
264 // return translated strings
266 UString aRet;
267 if ( aProgrammaticFunctionName == L"countParams" )
269 if ( nArgument == 0 ) aRet = L"Beliebiger Parameter";
271 else if ( aProgrammaticFunctionName == L"addOne" )
273 if ( nArgument == 0 ) aRet = L"Der Wert, zu dem 1 addiert wird";
275 else if ( aProgrammaticFunctionName == L"repeatStr" )
277 if ( nArgument == 0 ) aRet = L"Der Text, der wiederholt wird";
278 else if ( nArgument == 1 ) aRet = L"Die Anzahl der Wiederholungen";
280 else if ( aProgrammaticFunctionName == L"getDateString" )
282 if ( nArgument == 0 ) aRet = L"(intern)";
283 else if ( nArgument == 1 ) aRet = L"Der Wert, der als Datum formatiert wird";
285 else if ( aProgrammaticFunctionName == L"getColorValue" )
287 if ( nArgument == 0 ) aRet = L"Der Bereich, dessen Hintergrundfarbe abgefragt wird";
289 else if ( aProgrammaticFunctionName == L"transpose" )
291 if ( nArgument == 0 ) aRet = L"Die Matrix, die transponiert werden soll";
293 else if ( aProgrammaticFunctionName == L"transposeInt" )
295 if ( nArgument == 0 ) aRet = L"Die Matrix, die transponiert werden soll";
297 else if ( aProgrammaticFunctionName == L"repeatMultiple" )
299 if ( nArgument == 0 ) aRet = L"Anzahl der Wiederholungen";
300 else if ( nArgument == 1 ) aRet = L"Text, der zwischen den Inhalten erscheint";
301 else if ( nArgument == 2 ) aRet = L"Mehrere Inhalte";
303 else if ( aProgrammaticFunctionName == L"getStrOrVal" )
305 if ( nArgument == 0 ) aRet = L"Wenn Flag 0 ist, wird ein Wert zurueckgegeben, sonst ein String.";
307 else if ( aProgrammaticFunctionName == L"callAsync" )
309 if ( nArgument == 0 ) aRet = L"Ein String";
311 return aRet;
314 UString ScTestAddIn::getProgrammaticCategoryName(const UString& aProgrammaticFunctionName)
315 THROWS( (UsrSystemException) )
317 // return non-translated strings
319 UString aRet;
320 if ( aProgrammaticFunctionName == L"countParams" ) aRet = L"Information";
321 else if ( aProgrammaticFunctionName == L"addOne" ) aRet = L"Mathematical";
322 else if ( aProgrammaticFunctionName == L"repeatStr" ) aRet = L"Text";
323 else if ( aProgrammaticFunctionName == L"getDateString" ) aRet = L"Date&Time";
324 else if ( aProgrammaticFunctionName == L"getColorValue" ) aRet = L"Spreadsheet";
325 else if ( aProgrammaticFunctionName == L"transpose" ) aRet = L"Matrix";
326 else if ( aProgrammaticFunctionName == L"transposeInt" ) aRet = L"Matrix";
327 else if ( aProgrammaticFunctionName == L"repeatMultiple" )aRet = L"Text";
328 else if ( aProgrammaticFunctionName == L"getStrOrVal" ) aRet = L"Add-In";
329 else if ( aProgrammaticFunctionName == L"callAsync" ) aRet = L"Realtime"; // new group
330 return aRet;
333 UString ScTestAddIn::getDisplayCategoryName(const UString& aProgrammaticFunctionName)
334 THROWS( (UsrSystemException) )
336 // return translated strings
338 return L"irgendwas"; // not used for predefined categories
341 // XLocalizable
343 void ScTestAddIn::setLocale(const lang::Locale& eLocale) THROWS( (UsrSystemException) )
345 aFuncLoc = eLocale;
346 // DBG_ERROR( UStringToString(aFuncLoc.Language, CHARSET_SYSTEM) + String("-") +
347 // UStringToString(aFuncLoc.Country, CHARSET_SYSTEM) );
350 ::com::sun::star::lang::Locale SAL_CALL ScTestAddIn::getLocale( ) throw(::com::sun::star::uno::RuntimeException)
352 return aFuncLoc;
355 // XTestAddIn
357 sal_Int32 SAL_CALL ScTestAddIn::countParams( const ::com::sun::star::uno::Sequence< ::com::sun::star::uno::Any >& aArgs ) throw(::com::sun::star::uno::RuntimeException)
359 return aArgs.getLength();
362 double SAL_CALL ScTestAddIn::addOne( double fValue ) throw(::com::sun::star::uno::RuntimeException)
364 return fValue + 1.0;
367 ::rtl::OUString SAL_CALL ScTestAddIn::repeatStr( const ::rtl::OUString& aStr, sal_Int32 nCount ) throw(::com::sun::star::uno::RuntimeException)
369 String aRet;
370 String aStrStr = OUStringToString( aStr, CHARSET_SYSTEM );
371 for (long i=0; i<nCount; i++)
372 aRet += aStrStr;
374 return StringToOUString( aRet, CHARSET_SYSTEM );
377 ::rtl::OUString SAL_CALL ScTestAddIn::getDateString( const ::com::sun::star::uno::Reference< ::com::sun::star::beans::XPropertySet >& xCaller, double fValue ) throw(::com::sun::star::uno::RuntimeException)
379 uno::Any aDateAny = xCaller->getPropertyValue( L"NullDate" );
380 //! if ( aDateAny.getReflection()->equals( *Date_getReflection() ) )
382 util::Date aDate;
383 aDateAny >>= aDate;
384 //const Date* pDate = (const Date*)aDateAny.get();
385 //if (pDate)
387 //Date aNewDate = *pDate;
388 Date aNewDate( aDate.Day, aDate.Month, aDate.Year );
390 aNewDate += (long)(fValue+0.5);
392 String aRet;
393 aRet += aNewDate.GetDay();
394 aRet += '.';
395 aRet += aNewDate.GetMonth();
396 aRet += '.';
397 aRet += aNewDate.GetYear();
398 return StringToOUString( aRet, CHARSET_SYSTEM );
402 return L"**ERROR**";
405 sal_Int32 SAL_CALL ScTestAddIn::getColorValue( const ::com::sun::star::uno::Reference< ::com::sun::star::table::XCellRange >& xRange ) throw(::com::sun::star::uno::RuntimeException)
407 uno::Reference<beans::XPropertySet> xProp( xRange, uno::UNO_QUERY );
408 if (xProp.is())
410 long nRet = -1;
411 uno::Any aTrans = xProp->getPropertyValue( L"IsCellBackgroundTransparent" );
412 BOOL bIsTrans;
413 aTrans >>= bIsTrans; //! dont use >>= for BOOL
414 if (!bIsTrans)
416 uno::Any aCol = xProp->getPropertyValue( L"CellBackColor" );
417 //nRet = NAMESPACE_USR(OPropertyTypeConversion)::toINT32( aCol );
418 aCol >>= nRet;
420 return nRet;
422 return 0;
425 double lcl_GetDoubleElement( const uno::Sequence< uno::Sequence<double> >& aMatrix, long nCol, long nRow )
427 if ( nRow < aMatrix.getLength() )
429 const uno::Sequence<double>& rRowSeq = aMatrix.getConstArray()[nRow];
430 if ( nCol < rRowSeq.getLength() )
431 return rRowSeq.getConstArray()[nCol];
433 return 0.0; // error
436 INT32 lcl_GetLongElement( const uno::Sequence< uno::Sequence<INT32> >& aMatrix, long nCol, long nRow )
438 if ( nRow < aMatrix.getLength() )
440 const uno::Sequence<INT32>& rRowSeq = aMatrix.getConstArray()[nRow];
441 if ( nCol < rRowSeq.getLength() )
442 return rRowSeq.getConstArray()[nCol];
444 return 0.0; // error
447 ::com::sun::star::uno::Sequence< ::com::sun::star::uno::Sequence< double > > SAL_CALL ScTestAddIn::transpose( const ::com::sun::star::uno::Sequence< ::com::sun::star::uno::Sequence< double > >& aMatrix ) throw(::com::sun::star::uno::RuntimeException)
449 long nRowCount = aMatrix.getLength();
450 long nColCount = 0;
451 if ( nRowCount )
452 nColCount = aMatrix.getConstArray()[0].getLength();
454 uno::Sequence< uno::Sequence<double> > aRet( nColCount );
455 for (long nCol=0; nCol<nColCount; nCol++)
457 uno::Sequence<double> aSubSeq(nRowCount);
458 for (long nRow=0; nRow<nRowCount; nRow++)
459 aSubSeq.getArray()[nRow] = lcl_GetDoubleElement( aMatrix, nCol, nRow );
461 aRet.getArray()[nCol] = aSubSeq;
464 return aRet;
467 ::com::sun::star::uno::Sequence< ::com::sun::star::uno::Sequence< sal_Int32 > > SAL_CALL ScTestAddIn::transposeInt( const ::com::sun::star::uno::Sequence< ::com::sun::star::uno::Sequence< sal_Int32 > >& aMatrix ) throw(::com::sun::star::uno::RuntimeException)
469 long nRowCount = aMatrix.getLength();
470 long nColCount = 0;
471 if ( nRowCount )
472 nColCount = aMatrix.getConstArray()[0].getLength();
474 uno::Sequence< uno::Sequence<INT32> > aRet( nColCount );
475 for (long nCol=0; nCol<nColCount; nCol++)
477 uno::Sequence<INT32> aSubSeq(nRowCount);
478 for (long nRow=0; nRow<nRowCount; nRow++)
479 aSubSeq.getArray()[nRow] = lcl_GetLongElement( aMatrix, nCol, nRow );
481 aRet.getArray()[nCol] = aSubSeq;
484 return aRet;
487 ::rtl::OUString SAL_CALL ScTestAddIn::repeatMultiple( sal_Int32 nCount, const ::com::sun::star::uno::Any& aFirst, const ::com::sun::star::uno::Sequence< ::com::sun::star::uno::Any >& aFollow ) throw(::com::sun::star::uno::RuntimeException)
489 String aSeparator;
490 if ( !aFirst.hasValue() ) // not specified
491 aSeparator = ';';
492 else
494 rtl::OUString aUStr;
495 aFirst >>= aUStr;
496 aSeparator = OUStringToString( aUStr, CHARSET_SYSTEM );
499 String aContent;
500 long nContCount = aFollow.getLength();
501 const uno::Any* pArr = aFollow.getConstArray();
502 for (long nPos=0; nPos<nContCount; nPos++)
504 if ( nPos > 0 )
505 aContent += ' ';
506 rtl::OUString aUStr;
507 pArr[nPos] >>= aUStr;
508 aContent += OUStringToString( aUStr, CHARSET_SYSTEM );
511 String aRet;
513 for (long i=0; i<nCount; i++)
515 if (i>0)
516 aRet += aSeparator;
517 aRet += aContent;
520 return StringToOUString(aRet, CHARSET_SYSTEM);
523 ::com::sun::star::uno::Any SAL_CALL ScTestAddIn::getStrOrVal( sal_Int32 nFlag ) throw(::com::sun::star::uno::RuntimeException)
525 uno::Any aRet;
527 //! Test
528 if ( nFlag == 42 )
530 uno::Sequence<rtl::OUString> aInner(3);
531 aInner.getArray()[0] = L"Bla";
532 aInner.getArray()[1] = L"Fasel";
533 aInner.getArray()[2] = L"Suelz";
534 uno::Sequence< uno::Sequence<rtl::OUString> > aOuter( &aInner, 1 );
536 //return uno::Any( &aOuter, Sequence< Sequence<UString> >::getReflection() );
538 aRet <<= aOuter;
539 return aRet;
541 //! Test
543 if ( nFlag )
544 aRet <<= UString(L"This is a string.");
545 else
546 aRet <<= (INT32)42;
548 return aRet;
551 ::com::sun::star::uno::Reference< ::com::sun::star::sheet::XVolatileResult > SAL_CALL ScTestAddIn::callAsync( const ::rtl::OUString& aString ) throw(::com::sun::star::uno::RuntimeException)
553 String aStr = OUStringToString( aString, CHARSET_SYSTEM );
554 char c = (char) aStr;
555 if ( c >= '0' && c <= '9' )
557 if (!xNumResult.is())
558 xNumResult = new ScAddInResult( "Num" );
559 return xNumResult;
561 else
563 if (!xAlphaResult.is())
564 xAlphaResult = new ScAddInResult( "Str" );
565 return xAlphaResult;
570 // XServiceName
572 ::rtl::OUString SAL_CALL ScTestAddIn::getServiceName( ) throw(::com::sun::star::uno::RuntimeException)
574 return SCTESTADDIN_SERVICE; // name of specific AddIn service
577 // XServiceInfo
579 ::rtl::OUString SAL_CALL ScTestAddIn::getImplementationName( ) throw(::com::sun::star::uno::RuntimeException)
581 return getImplementationName_Static();
584 sal_Bool SAL_CALL ScTestAddIn::supportsService( const ::rtl::OUString& ServiceName ) throw(::com::sun::star::uno::RuntimeException)
586 return ServiceName == SCADDIN_SERVICE ||
587 ServiceName == SCTESTADDIN_SERVICE;
590 ::com::sun::star::uno::Sequence< ::rtl::OUString > SAL_CALL ScTestAddIn::getSupportedServiceNames( ) throw(::com::sun::star::uno::RuntimeException)
592 return getSupportedServiceNames_Static();
595 //------------------------------------------------------------------------