1 /*************************************************************************
3 * DO NOT ALTER OR REMOVE COPYRIGHT NOTICES OR THIS FILE HEADER.
5 * Copyright 2008 by Sun Microsystems, Inc.
7 * OpenOffice.org - a multi-platform office productivity suite
9 * $RCSfile: sampleaddin.cxx,v $
10 * $Revision: 1.3.44.1 $
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 ************************************************************************/
30 #include "sampleaddin.hxx"
32 #include <cppuhelper/factory.hxx>
33 #include <osl/diagnose.h>
35 #include <com/sun/star/drawing/XDrawPageSupplier.hpp>
36 #include <com/sun/star/drawing/XDrawPage.hpp>
37 #include <com/sun/star/chart/XChartDataArray.hpp>
38 #include <com/sun/star/text/XTextRange.hpp>
39 #include <com/sun/star/chart/X3DDisplay.hpp>
41 using namespace com::sun::star
;
44 // code for creating instances of SampleAddIn
48 void SAL_CALL
component_getImplementationEnvironment(
49 const sal_Char
** ppEnvTypeName
, uno_Environment
** /*ppEnv*/ )
51 *ppEnvTypeName
= CPPU_CURRENT_LANGUAGE_BINDING_NAME
;
54 sal_Bool SAL_CALL
component_writeInfo(
55 void * /*pServiceManager*/, registry::XRegistryKey
* pRegistryKey
)
61 OUString aImpl
= OUString::createFromAscii( "/" );
62 aImpl
+= SampleAddIn::getImplementationName_Static();
63 aImpl
+= OUString::createFromAscii( "/UNO/SERVICES" );
65 uno::Reference
< registry::XRegistryKey
> xNewKey(
66 reinterpret_cast<registry::XRegistryKey
*>( pRegistryKey
)->createKey( aImpl
) );
68 uno::Sequence
< OUString
> aSequ
= SampleAddIn::getSupportedServiceNames_Static();
69 const OUString
* pArray
= aSequ
.getConstArray();
70 for( sal_Int32 i
= 0; i
< aSequ
.getLength(); i
++ )
71 xNewKey
->createKey( pArray
[i
] );
75 catch( registry::InvalidRegistryException
& )
77 OSL_ENSURE( sal_False
, "### InvalidRegistryException!" );
83 void * SAL_CALL
component_getFactory(
84 const sal_Char
* pImplName
, void * pServiceManager
, void * /*pRegistryKey*/ )
88 if ( pServiceManager
&&
89 OUString::createFromAscii( pImplName
) == SampleAddIn::getImplementationName_Static() )
91 uno::Reference
< lang::XSingleServiceFactory
> xFactory( cppu::createSingleFactory(
92 reinterpret_cast<lang::XMultiServiceFactory
*>( pServiceManager
),
93 SampleAddIn::getImplementationName_Static(),
94 SampleAddIn_CreateInstance
,
95 SampleAddIn::getSupportedServiceNames_Static() ) );
100 pRet
= xFactory
.get();
110 // --------------------
112 // --------------------
114 SampleAddIn::SampleAddIn()
119 SampleAddIn::~SampleAddIn()
123 // this functionality should be provided by the chart API some day
124 sal_Bool
SampleAddIn::getLogicalPosition( uno::Reference
< drawing::XShape
>& xAxis
,
127 awt::Point
& aOutPosition
)
129 sal_Bool bRet
= sal_False
;
133 awt::Size aSize
= xAxis
->getSize();
134 sal_Int32 nLength
= bVertical
? aSize
.Height
: aSize
.Width
;
136 uno::Reference
< beans::XPropertySet
> xProp( xAxis
, uno::UNO_QUERY
);
142 uno::Any aAny
= xProp
->getPropertyValue( OUString::createFromAscii( "Min" ));
144 aAny
= xProp
->getPropertyValue( OUString::createFromAscii( "Max" ));
147 double fRange
= fMax
- fMin
;
148 if( fMin
<= fValue
&& fValue
<= fMax
&&
151 double fPercentage
= (fValue
- fMin
) / fRange
;
152 awt::Point aPos
= xAxis
->getPosition();
156 aOutPosition
.X
= aPos
.X
;
157 aOutPosition
.Y
= static_cast<sal_Int32
>(aPos
.Y
+ nLength
* (1.0 - fPercentage
)); // y scale goes from top to bottom
161 aOutPosition
.X
= static_cast<sal_Int32
>(aPos
.X
+ nLength
* fPercentage
);
162 aOutPosition
.Y
= aPos
.Y
;
167 catch( beans::UnknownPropertyException
)
169 // the shape xAxis was no chart axis
177 OUString
SampleAddIn::getImplementationName_Static()
179 return OUString::createFromAscii( "SampleAddIn" );
182 uno::Sequence
< ::rtl::OUString
> SampleAddIn::getSupportedServiceNames_Static()
184 uno::Sequence
< OUString
> aSeq( 4 );
186 aSeq
[ 0 ] = OUString::createFromAscii( "com.sun.star.chart.ChartAxisXSupplier" );
187 aSeq
[ 1 ] = OUString::createFromAscii( "com.sun.star.chart.ChartAxisYSupplier" );
188 aSeq
[ 2 ] = OUString::createFromAscii( "com.sun.star.chart.Diagram" );
189 aSeq
[ 3 ] = OUString::createFromAscii( "com.sun.star.chart.SampleAddIn" );
194 uno::Reference
< uno::XInterface
> SAL_CALL
SampleAddIn_CreateInstance(
195 const uno::Reference
< lang::XMultiServiceFactory
>& )
197 uno::Reference
< uno::XInterface
> xInst
= (cppu::OWeakObject
*)new SampleAddIn();
202 // implementation of interface methods
205 void SAL_CALL
SampleAddIn::initialize( const uno::Sequence
< uno::Any
>& aArguments
)
206 throw( uno::Exception
, uno::RuntimeException
)
208 // first argument should be the XChartDocument
209 OSL_ENSURE( aArguments
.getLength() > 0, "Please initialize Chart AddIn with ChartDocument!" );
211 if( aArguments
.getLength())
213 aArguments
[ 0 ] >>= mxChartDoc
;
214 OSL_ENSURE( mxChartDoc
.is(), "First argument in initialization is not an XChartDocument!" );
216 // set XY chart as base type to be drawn
217 uno::Reference
< beans::XPropertySet
> xDocProp( mxChartDoc
, uno::UNO_QUERY
);
221 aBaseType
<<= rtl::OUString( RTL_CONSTASCII_USTRINGPARAM( "com.sun.star.chart.XYDiagram" ));
224 xDocProp
->setPropertyValue( rtl::OUString::createFromAscii( "BaseDiagram" ), aBaseType
);
230 // change background of plot area to light blue
231 uno::Reference
< chart::X3DDisplay
> xWallSupplier( mxChartDoc
->getDiagram(), uno::UNO_QUERY
);
232 if( xWallSupplier
.is())
234 uno::Reference
< beans::XPropertySet
> xDiaProp( xWallSupplier
->getWall(), uno::UNO_QUERY
);
235 uno::Reference
< beans::XPropertySet
> xLegendProp( mxChartDoc
->getLegend(), uno::UNO_QUERY
);
240 aAny
<<= (sal_Int32
)( 0xe0e0f0 );
241 xDiaProp
->setPropertyValue( OUString::createFromAscii( "FillColor" ), aAny
);
242 xLegendProp
->setPropertyValue( OUString::createFromAscii( "FillColor" ), aAny
);
249 /********************************************************************************
251 * The method refresh is the most important method - here all objects that
252 * are necessary for the chart are created
254 * in the first implementation you will have to insert everything in this
255 * routine - all old objects are deleted beforehand
257 ********************************************************************************/
258 void SAL_CALL
SampleAddIn::refresh() throw( uno::RuntimeException
)
260 if( ! mxChartDoc
.is())
263 // first of all get the draw page
264 uno::Reference
< drawing::XDrawPageSupplier
> xPageSupp( mxChartDoc
, uno::UNO_QUERY
);
265 uno::Reference
< lang::XMultiServiceFactory
> xFactory( mxChartDoc
, uno::UNO_QUERY
);
266 if( xPageSupp
.is() &&
269 uno::Reference
< drawing::XDrawPage
> xPage
= xPageSupp
->getDrawPage();
272 // now we have the page to insert objects
274 // add a horizontal line at the middle value of the first series
275 // -------------------------------------------------------------
278 // get the logical position from the coordinate
280 uno::Reference
< drawing::XShape
> xYAxisShape( getYAxis(), uno::UNO_QUERY
);
281 uno::Reference
< drawing::XShape
> xXAxisShape( getXAxis(), uno::UNO_QUERY
);
283 if( xXAxisShape
.is() &&
286 // create line first time
287 if( ! mxMyRedLine
.is())
289 mxMyRedLine
= uno::Reference
< drawing::XShape
>(
290 xFactory
->createInstance( OUString::createFromAscii( "com.sun.star.drawing.LineShape" )),
292 xPage
->add( mxMyRedLine
);
294 // make line red and thick
295 uno::Reference
< beans::XPropertySet
> xShapeProp( mxMyRedLine
, uno::UNO_QUERY
);
298 uno::Any aColor
, aWidth
;
299 aColor
<<= (sal_Int32
)(0xe01010);
300 aWidth
<<= (sal_Int32
)(50); // 0.5 mm
303 xShapeProp
->setPropertyValue( OUString::createFromAscii( "LineColor" ), aColor
);
304 xShapeProp
->setPropertyValue( OUString::createFromAscii( "LineWidth" ), aWidth
);
310 // create text object first time
313 mxMyText
= uno::Reference
< drawing::XShape
>(
314 xFactory
->createInstance( OUString::createFromAscii( "com.sun.star.drawing.TextShape" )),
316 xPage
->add( mxMyText
);
320 // if( maLocale.Language.equalsIgnoreCase( OUString::createFromAscii("DE")))
321 // aText = OUString::createFromAscii( "Kleines Beispiel" );
323 aText
= OUString::createFromAscii( "Little Example" );
325 uno::Reference
< beans::XPropertySet
> xTextProp( mxMyText
, uno::UNO_QUERY
);
329 aTrueAny
<<= (sal_Bool
)(sal_True
);
332 xTextProp
->setPropertyValue( rtl::OUString::createFromAscii( "TextAutoGrowWidth" ), aTrueAny
);
338 uno::Reference
< text::XTextRange
> xTextRange( mxMyText
, uno::UNO_QUERY
);
341 xTextRange
->setString( aText
);
346 // position line and text
348 // get the array. Note: the first dimension is the length
349 // of each series and the second one is the number of series
350 // this should be changed in the future
351 uno::Sequence
< uno::Sequence
< double > > aData
;
352 uno::Reference
< chart::XChartData
> xData
= mxChartDoc
->getData();
353 uno::Reference
< chart::XChartDataArray
> xDataArray( xData
, uno::UNO_QUERY
);
355 aData
= xDataArray
->getData();
357 // get row count == length of each series
358 sal_Int32 nSize
= aData
.getLength();
359 sal_Int32 nMiddle
= nSize
/ 2;
360 // get value for first series
361 double fMiddleVal
= xData
->getNotANumber(); // set to NaN
362 if( aData
[ nMiddle
].getLength()) // we have at least one series
363 fMiddleVal
= aData
[ nMiddle
][ 0 ];
366 getLogicalPosition( xYAxisShape
, fMiddleVal
, sal_True
, aPos
);
367 awt::Size aSize
= xXAxisShape
->getSize();
369 if( mxMyRedLine
.is())
371 awt::Point aEnd
= aPos
;
372 aEnd
.X
+= aSize
.Width
;
374 uno::Sequence
< uno::Sequence
< awt::Point
> > aPtSeq( 1 );
375 aPtSeq
[ 0 ].realloc( 2 );
376 aPtSeq
[ 0 ][ 0 ] = aPos
;
377 aPtSeq
[ 0 ][ 1 ] = aEnd
;
379 uno::Reference
< beans::XPropertySet
> xShapeProp( mxMyRedLine
, uno::UNO_QUERY
);
384 xShapeProp
->setPropertyValue( rtl::OUString::createFromAscii( "PolyPolygon" ), aAny
);
389 // put the text centered below the red line
390 aPos
.X
+= ( aSize
.Width
- mxMyRedLine
->getPosition().X
) / 2;
392 aPos
.Y
+= static_cast<sal_Int32
>(0.1 * xYAxisShape
->getSize().Height
);
393 mxMyText
->setPosition( aPos
);
399 // set axis scale to 200
400 // uno::Reference< beans::XPropertySet > xXAxis( getXAxis(), uno::UNO_QUERY );
404 // aAny <<= (sal_Bool)(sal_False);
405 // xXAxis->setPropertyValue( rtl::OUString::createFromAscii( "AutoStepMain" ),
407 // aAny <<= (double)(200.0);
408 // xXAxis->setPropertyValue( rtl::OUString::createFromAscii( "StepMain" ),
412 // try setting symbols
413 // uno::Reference< beans::XPropertySet > xProp = getDataRowProperties( 0 );
417 // aAny <<= (sal_Int32)(-1);
418 // xProp->setPropertyValue( OUString::createFromAscii( "SymbolType" ), aAny );
419 // aAny <<= rtl::OUString::createFromAscii( "http://mib-1168/www/images/go.gif" );
420 // xProp->setPropertyValue( OUString::createFromAscii( "SymbolBitmapURL" ), aAny );
424 void SAL_CALL
SampleAddIn::addRefreshListener( const uno::Reference
< util::XRefreshListener
>& )
425 throw( uno::RuntimeException
)
427 // not implemented - this is not necessary
428 // (this method exists just because the interface requires it)
431 void SAL_CALL
SampleAddIn::removeRefreshListener( const uno::Reference
< util::XRefreshListener
>& )
432 throw( uno::RuntimeException
)
434 // not implemented - this is not necessary
435 // (this method exists just because the interface requires it)
439 OUString SAL_CALL
SampleAddIn::getDiagramType() throw( uno::RuntimeException
)
441 return OUString::createFromAscii( "com.sun.star.chart.SampleDiagram" );
444 // the following methods just delegate to the "parent diagram" (which in the future might no longer exist)
446 uno::Reference
< beans::XPropertySet
> SAL_CALL
SampleAddIn::getDataRowProperties( sal_Int32 nRow
)
447 throw( lang::IndexOutOfBoundsException
,
448 uno::RuntimeException
)
452 uno::Reference
< chart::XDiagram
> xDia
= mxChartDoc
->getDiagram();
454 return xDia
->getDataRowProperties( nRow
);
457 return uno::Reference
< beans::XPropertySet
>();
460 uno::Reference
< beans::XPropertySet
> SAL_CALL
SampleAddIn::getDataPointProperties( sal_Int32 nCol
, sal_Int32 nRow
)
461 throw( lang::IndexOutOfBoundsException
,
462 uno::RuntimeException
)
466 uno::Reference
< chart::XDiagram
> xDia
= mxChartDoc
->getDiagram();
468 return xDia
->getDataPointProperties( nCol
, nRow
);
471 return uno::Reference
< beans::XPropertySet
>();
474 // XShape ( ::XDiagram )
475 awt::Size SAL_CALL
SampleAddIn::getSize()
476 throw( uno::RuntimeException
)
480 uno::Reference
< drawing::XShape
> xShape( mxChartDoc
->getDiagram(), uno::UNO_QUERY
);
482 return xShape
->getSize();
488 void SAL_CALL
SampleAddIn::setSize( const awt::Size
& aSize
)
489 throw( beans::PropertyVetoException
, uno::RuntimeException
)
493 uno::Reference
< drawing::XShape
> xShape( mxChartDoc
->getDiagram(), uno::UNO_QUERY
);
495 xShape
->setSize( aSize
);
499 awt::Point SAL_CALL
SampleAddIn::getPosition()
500 throw( uno::RuntimeException
)
504 uno::Reference
< drawing::XShape
> xShape( mxChartDoc
->getDiagram(), uno::UNO_QUERY
);
506 return xShape
->getPosition();
512 void SAL_CALL
SampleAddIn::setPosition( const awt::Point
& aPos
)
513 throw( uno::RuntimeException
)
517 uno::Reference
< drawing::XShape
> xShape( mxChartDoc
->getDiagram(), uno::UNO_QUERY
);
519 xShape
->setPosition( aPos
);
523 // XShapeDescriptor ( ::XShape ::XDiagram )
524 rtl::OUString SAL_CALL
SampleAddIn::getShapeType() throw( com::sun::star::uno::RuntimeException
)
526 return OUString::createFromAscii( "com.sun.star.chart.SampleAddinShape" );
530 uno::Reference
< drawing::XShape
> SAL_CALL
SampleAddIn::getXAxisTitle()
531 throw( uno::RuntimeException
)
535 uno::Reference
< chart::XAxisXSupplier
> xAxisSupp( mxChartDoc
->getDiagram(), uno::UNO_QUERY
);
537 return xAxisSupp
->getXAxisTitle();
540 return uno::Reference
< drawing::XShape
>();
543 uno::Reference
< beans::XPropertySet
> SAL_CALL
SampleAddIn::getXAxis()
544 throw( uno::RuntimeException
)
548 uno::Reference
< chart::XAxisXSupplier
> xAxisSupp( mxChartDoc
->getDiagram(), uno::UNO_QUERY
);
550 return xAxisSupp
->getXAxis();
553 return uno::Reference
< beans::XPropertySet
>();
556 uno::Reference
< beans::XPropertySet
> SAL_CALL
SampleAddIn::getXMainGrid()
557 throw( uno::RuntimeException
)
561 uno::Reference
< chart::XAxisXSupplier
> xAxisSupp( mxChartDoc
->getDiagram(), uno::UNO_QUERY
);
563 return xAxisSupp
->getXMainGrid();
566 return uno::Reference
< beans::XPropertySet
>();
569 uno::Reference
< beans::XPropertySet
> SAL_CALL
SampleAddIn::getXHelpGrid()
570 throw( uno::RuntimeException
)
574 uno::Reference
< chart::XAxisXSupplier
> xAxisSupp( mxChartDoc
->getDiagram(), uno::UNO_QUERY
);
576 return xAxisSupp
->getXHelpGrid();
579 return uno::Reference
< beans::XPropertySet
>();
583 uno::Reference
< drawing::XShape
> SAL_CALL
SampleAddIn::getYAxisTitle()
584 throw( uno::RuntimeException
)
588 uno::Reference
< chart::XAxisYSupplier
> xAxisSupp( mxChartDoc
->getDiagram(), uno::UNO_QUERY
);
590 return xAxisSupp
->getYAxisTitle();
593 return uno::Reference
< drawing::XShape
>();
596 uno::Reference
< beans::XPropertySet
> SAL_CALL
SampleAddIn::getYAxis()
597 throw( uno::RuntimeException
)
601 uno::Reference
< chart::XAxisYSupplier
> xAxisSupp( mxChartDoc
->getDiagram(), uno::UNO_QUERY
);
603 return xAxisSupp
->getYAxis();
606 return uno::Reference
< beans::XPropertySet
>();
609 uno::Reference
< beans::XPropertySet
> SAL_CALL
SampleAddIn::getYMainGrid()
610 throw( uno::RuntimeException
)
614 uno::Reference
< chart::XAxisYSupplier
> xAxisSupp( mxChartDoc
->getDiagram(), uno::UNO_QUERY
);
616 return xAxisSupp
->getYMainGrid();
619 return uno::Reference
< beans::XPropertySet
>();
622 uno::Reference
< beans::XPropertySet
> SAL_CALL
SampleAddIn::getYHelpGrid()
623 throw( uno::RuntimeException
)
627 uno::Reference
< chart::XAxisYSupplier
> xAxisSupp( mxChartDoc
->getDiagram(), uno::UNO_QUERY
);
629 return xAxisSupp
->getYHelpGrid();
632 return uno::Reference
< beans::XPropertySet
>();
636 uno::Reference
< beans::XPropertySet
> SAL_CALL
SampleAddIn::getUpBar()
637 throw( uno::RuntimeException
)
641 uno::Reference
< chart::XStatisticDisplay
> xStatDisp( mxChartDoc
->getDiagram(), uno::UNO_QUERY
);
643 return xStatDisp
->getUpBar();
646 return uno::Reference
< beans::XPropertySet
>();
649 uno::Reference
< beans::XPropertySet
> SAL_CALL
SampleAddIn::getDownBar()
650 throw( uno::RuntimeException
)
654 uno::Reference
< chart::XStatisticDisplay
> xStatDisp( mxChartDoc
->getDiagram(), uno::UNO_QUERY
);
656 return xStatDisp
->getDownBar();
659 return uno::Reference
< beans::XPropertySet
>();
662 uno::Reference
< beans::XPropertySet
> SAL_CALL
SampleAddIn::getMinMaxLine()
663 throw( uno::RuntimeException
)
667 uno::Reference
< chart::XStatisticDisplay
> xStatDisp( mxChartDoc
->getDiagram(), uno::UNO_QUERY
);
669 return xStatDisp
->getMinMaxLine();
672 return uno::Reference
< beans::XPropertySet
>();
676 OUString SAL_CALL
SampleAddIn::getServiceName() throw( uno::RuntimeException
)
678 return OUString::createFromAscii( "com.sun.star.chart.SampleAddIn" );
682 OUString SAL_CALL
SampleAddIn::getImplementationName() throw( uno::RuntimeException
)
684 return getImplementationName_Static();
687 sal_Bool SAL_CALL
SampleAddIn::supportsService( const OUString
& ServiceName
)
688 throw( uno::RuntimeException
)
690 uno::Sequence
< OUString
> aServiceSeq
= getSupportedServiceNames_Static();
692 sal_Int32 nLength
= aServiceSeq
.getLength();
693 for( sal_Int32 i
=0; i
< nLength
; i
++ )
695 if( ServiceName
.equals( aServiceSeq
[ i
] ))
702 uno::Sequence
< OUString
> SAL_CALL
SampleAddIn::getSupportedServiceNames()
703 throw( uno::RuntimeException
)
705 return getSupportedServiceNames_Static();
709 void SAL_CALL
SampleAddIn::setLocale( const lang::Locale
& eLocale
)
710 throw( uno::RuntimeException
)
715 lang::Locale SAL_CALL
SampleAddIn::getLocale()
716 throw( uno::RuntimeException
)