1 /*************************************************************************
3 * DO NOT ALTER OR REMOVE COPYRIGHT NOTICES OR THIS FILE HEADER.
5 * Copyright 2000, 2010 Oracle and/or its affiliates.
7 * OpenOffice.org - a multi-platform office productivity suite
9 * This file is part of OpenOffice.org.
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.
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).
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.
26 ************************************************************************/
27 #include "sampleaddin.hxx"
29 #include <cppuhelper/factory.hxx>
30 #include <osl/diagnose.h>
32 #include <com/sun/star/drawing/XDrawPageSupplier.hpp>
33 #include <com/sun/star/drawing/XDrawPage.hpp>
34 #include <com/sun/star/chart/XChartDataArray.hpp>
35 #include <com/sun/star/text/XTextRange.hpp>
36 #include <com/sun/star/chart/X3DDisplay.hpp>
38 using namespace com::sun::star
;
41 // code for creating instances of SampleAddIn
45 void SAL_CALL
component_getImplementationEnvironment(
46 const sal_Char
** ppEnvTypeName
, uno_Environment
** /*ppEnv*/ )
48 *ppEnvTypeName
= CPPU_CURRENT_LANGUAGE_BINDING_NAME
;
51 sal_Bool SAL_CALL
component_writeInfo(
52 void * /*pServiceManager*/, registry::XRegistryKey
* pRegistryKey
)
58 OUString aImpl
= OUString::createFromAscii( "/" );
59 aImpl
+= SampleAddIn::getImplementationName_Static();
60 aImpl
+= OUString::createFromAscii( "/UNO/SERVICES" );
62 uno::Reference
< registry::XRegistryKey
> xNewKey(
63 reinterpret_cast<registry::XRegistryKey
*>( pRegistryKey
)->createKey( aImpl
) );
65 uno::Sequence
< OUString
> aSequ
= SampleAddIn::getSupportedServiceNames_Static();
66 const OUString
* pArray
= aSequ
.getConstArray();
67 for( sal_Int32 i
= 0; i
< aSequ
.getLength(); i
++ )
68 xNewKey
->createKey( pArray
[i
] );
72 catch( registry::InvalidRegistryException
& )
74 OSL_ENSURE( sal_False
, "### InvalidRegistryException!" );
80 void * SAL_CALL
component_getFactory(
81 const sal_Char
* pImplName
, void * pServiceManager
, void * /*pRegistryKey*/ )
85 if ( pServiceManager
&&
86 OUString::createFromAscii( pImplName
) == SampleAddIn::getImplementationName_Static() )
88 uno::Reference
< lang::XSingleServiceFactory
> xFactory( cppu::createSingleFactory(
89 reinterpret_cast<lang::XMultiServiceFactory
*>( pServiceManager
),
90 SampleAddIn::getImplementationName_Static(),
91 SampleAddIn_CreateInstance
,
92 SampleAddIn::getSupportedServiceNames_Static() ) );
97 pRet
= xFactory
.get();
107 // --------------------
109 // --------------------
111 SampleAddIn::SampleAddIn()
116 SampleAddIn::~SampleAddIn()
120 // this functionality should be provided by the chart API some day
121 sal_Bool
SampleAddIn::getLogicalPosition( uno::Reference
< drawing::XShape
>& xAxis
,
124 awt::Point
& aOutPosition
)
126 sal_Bool bRet
= sal_False
;
130 awt::Size aSize
= xAxis
->getSize();
131 sal_Int32 nLength
= bVertical
? aSize
.Height
: aSize
.Width
;
133 uno::Reference
< beans::XPropertySet
> xProp( xAxis
, uno::UNO_QUERY
);
138 double fMin(0.0), fMax(0.0);
139 uno::Any aAny
= xProp
->getPropertyValue( OUString::createFromAscii( "Min" ));
141 aAny
= xProp
->getPropertyValue( OUString::createFromAscii( "Max" ));
144 double fRange
= fMax
- fMin
;
145 if( fMin
<= fValue
&& fValue
<= fMax
&&
148 double fPercentage
= (fValue
- fMin
) / fRange
;
149 awt::Point aPos
= xAxis
->getPosition();
153 aOutPosition
.X
= aPos
.X
;
154 aOutPosition
.Y
= static_cast<sal_Int32
>(aPos
.Y
+ nLength
* (1.0 - fPercentage
)); // y scale goes from top to bottom
158 aOutPosition
.X
= static_cast<sal_Int32
>(aPos
.X
+ nLength
* fPercentage
);
159 aOutPosition
.Y
= aPos
.Y
;
164 catch( beans::UnknownPropertyException
)
166 // the shape xAxis was no chart axis
174 OUString
SampleAddIn::getImplementationName_Static()
176 return OUString::createFromAscii( "SampleAddIn" );
179 uno::Sequence
< ::rtl::OUString
> SampleAddIn::getSupportedServiceNames_Static()
181 uno::Sequence
< OUString
> aSeq( 4 );
183 aSeq
[ 0 ] = OUString::createFromAscii( "com.sun.star.chart.ChartAxisXSupplier" );
184 aSeq
[ 1 ] = OUString::createFromAscii( "com.sun.star.chart.ChartAxisYSupplier" );
185 aSeq
[ 2 ] = OUString::createFromAscii( "com.sun.star.chart.Diagram" );
186 aSeq
[ 3 ] = OUString::createFromAscii( "com.sun.star.chart.SampleAddIn" );
191 uno::Reference
< uno::XInterface
> SAL_CALL
SampleAddIn_CreateInstance(
192 const uno::Reference
< lang::XMultiServiceFactory
>& )
194 uno::Reference
< uno::XInterface
> xInst
= (cppu::OWeakObject
*)new SampleAddIn();
199 // implementation of interface methods
202 void SAL_CALL
SampleAddIn::initialize( const uno::Sequence
< uno::Any
>& aArguments
)
203 throw( uno::Exception
, uno::RuntimeException
)
205 // first argument should be the XChartDocument
206 OSL_ENSURE( aArguments
.getLength() > 0, "Please initialize Chart AddIn with ChartDocument!" );
208 if( aArguments
.getLength())
210 aArguments
[ 0 ] >>= mxChartDoc
;
211 OSL_ENSURE( mxChartDoc
.is(), "First argument in initialization is not an XChartDocument!" );
213 // set XY chart as base type to be drawn
214 uno::Reference
< beans::XPropertySet
> xDocProp( mxChartDoc
, uno::UNO_QUERY
);
218 aBaseType
<<= rtl::OUString( RTL_CONSTASCII_USTRINGPARAM( "com.sun.star.chart.XYDiagram" ));
221 xDocProp
->setPropertyValue( rtl::OUString::createFromAscii( "BaseDiagram" ), aBaseType
);
227 // change background of plot area to light blue
228 uno::Reference
< chart::X3DDisplay
> xWallSupplier( mxChartDoc
->getDiagram(), uno::UNO_QUERY
);
229 if( xWallSupplier
.is())
231 uno::Reference
< beans::XPropertySet
> xDiaProp( xWallSupplier
->getWall(), uno::UNO_QUERY
);
232 uno::Reference
< beans::XPropertySet
> xLegendProp( mxChartDoc
->getLegend(), uno::UNO_QUERY
);
237 aAny
<<= (sal_Int32
)( 0xe0e0f0 );
238 xDiaProp
->setPropertyValue( OUString::createFromAscii( "FillColor" ), aAny
);
239 xLegendProp
->setPropertyValue( OUString::createFromAscii( "FillColor" ), aAny
);
246 /********************************************************************************
248 * The method refresh is the most important method - here all objects that
249 * are necessary for the chart are created
251 * in the first implementation you will have to insert everything in this
252 * routine - all old objects are deleted beforehand
254 ********************************************************************************/
255 void SAL_CALL
SampleAddIn::refresh() throw( uno::RuntimeException
)
257 if( ! mxChartDoc
.is())
260 // first of all get the draw page
261 uno::Reference
< drawing::XDrawPageSupplier
> xPageSupp( mxChartDoc
, uno::UNO_QUERY
);
262 uno::Reference
< lang::XMultiServiceFactory
> xFactory( mxChartDoc
, uno::UNO_QUERY
);
263 if( xPageSupp
.is() &&
266 uno::Reference
< drawing::XDrawPage
> xPage
= xPageSupp
->getDrawPage();
269 // now we have the page to insert objects
271 // add a horizontal line at the middle value of the first series
272 // -------------------------------------------------------------
275 // get the logical position from the coordinate
277 uno::Reference
< drawing::XShape
> xYAxisShape( getYAxis(), uno::UNO_QUERY
);
278 uno::Reference
< drawing::XShape
> xXAxisShape( getXAxis(), uno::UNO_QUERY
);
280 if( xXAxisShape
.is() &&
283 // create line first time
284 if( ! mxMyRedLine
.is())
286 mxMyRedLine
= uno::Reference
< drawing::XShape
>(
287 xFactory
->createInstance( OUString::createFromAscii( "com.sun.star.drawing.LineShape" )),
289 xPage
->add( mxMyRedLine
);
291 // make line red and thick
292 uno::Reference
< beans::XPropertySet
> xShapeProp( mxMyRedLine
, uno::UNO_QUERY
);
295 uno::Any aColor
, aWidth
;
296 aColor
<<= (sal_Int32
)(0xe01010);
297 aWidth
<<= (sal_Int32
)(50); // 0.5 mm
300 xShapeProp
->setPropertyValue( OUString::createFromAscii( "LineColor" ), aColor
);
301 xShapeProp
->setPropertyValue( OUString::createFromAscii( "LineWidth" ), aWidth
);
307 // create text object first time
310 mxMyText
= uno::Reference
< drawing::XShape
>(
311 xFactory
->createInstance( OUString::createFromAscii( "com.sun.star.drawing.TextShape" )),
313 xPage
->add( mxMyText
);
317 // if( maLocale.Language.equalsIgnoreCase( OUString::createFromAscii("DE")))
318 // aText = OUString::createFromAscii( "Kleines Beispiel" );
320 aText
= OUString::createFromAscii( "Little Example" );
322 uno::Reference
< beans::XPropertySet
> xTextProp( mxMyText
, uno::UNO_QUERY
);
326 aTrueAny
<<= (sal_Bool
)(sal_True
);
329 xTextProp
->setPropertyValue( rtl::OUString::createFromAscii( "TextAutoGrowWidth" ), aTrueAny
);
335 uno::Reference
< text::XTextRange
> xTextRange( mxMyText
, uno::UNO_QUERY
);
338 xTextRange
->setString( aText
);
343 // position line and text
345 // get the array. Note: the first dimension is the length
346 // of each series and the second one is the number of series
347 // this should be changed in the future
348 uno::Sequence
< uno::Sequence
< double > > aData
;
349 uno::Reference
< chart::XChartData
> xData
= mxChartDoc
->getData();
350 uno::Reference
< chart::XChartDataArray
> xDataArray( xData
, uno::UNO_QUERY
);
352 aData
= xDataArray
->getData();
354 // get row count == length of each series
355 sal_Int32 nSize
= aData
.getLength();
356 sal_Int32 nMiddle
= nSize
/ 2;
357 // get value for first series
358 double fMiddleVal
= xData
->getNotANumber(); // set to NaN
359 if( aData
[ nMiddle
].getLength()) // we have at least one series
360 fMiddleVal
= aData
[ nMiddle
][ 0 ];
363 getLogicalPosition( xYAxisShape
, fMiddleVal
, sal_True
, aPos
);
364 awt::Size aSize
= xXAxisShape
->getSize();
366 if( mxMyRedLine
.is())
368 awt::Point aEnd
= aPos
;
369 aEnd
.X
+= aSize
.Width
;
371 uno::Sequence
< uno::Sequence
< awt::Point
> > aPtSeq( 1 );
372 aPtSeq
[ 0 ].realloc( 2 );
373 aPtSeq
[ 0 ][ 0 ] = aPos
;
374 aPtSeq
[ 0 ][ 1 ] = aEnd
;
376 uno::Reference
< beans::XPropertySet
> xShapeProp( mxMyRedLine
, uno::UNO_QUERY
);
381 xShapeProp
->setPropertyValue( rtl::OUString::createFromAscii( "PolyPolygon" ), aAny
);
386 // put the text centered below the red line
387 aPos
.X
+= ( aSize
.Width
- mxMyRedLine
->getPosition().X
) / 2;
389 aPos
.Y
+= static_cast<sal_Int32
>(0.1 * xYAxisShape
->getSize().Height
);
390 mxMyText
->setPosition( aPos
);
396 // set axis scale to 200
397 // uno::Reference< beans::XPropertySet > xXAxis( getXAxis(), uno::UNO_QUERY );
401 // aAny <<= (sal_Bool)(sal_False);
402 // xXAxis->setPropertyValue( rtl::OUString::createFromAscii( "AutoStepMain" ),
404 // aAny <<= (double)(200.0);
405 // xXAxis->setPropertyValue( rtl::OUString::createFromAscii( "StepMain" ),
409 // try setting symbols
410 // uno::Reference< beans::XPropertySet > xProp = getDataRowProperties( 0 );
414 // aAny <<= (sal_Int32)(-1);
415 // xProp->setPropertyValue( OUString::createFromAscii( "SymbolType" ), aAny );
416 // aAny <<= rtl::OUString::createFromAscii( "http://mib-1168/www/images/go.gif" );
417 // xProp->setPropertyValue( OUString::createFromAscii( "SymbolBitmapURL" ), aAny );
421 void SAL_CALL
SampleAddIn::addRefreshListener( const uno::Reference
< util::XRefreshListener
>& )
422 throw( uno::RuntimeException
)
424 // not implemented - this is not necessary
425 // (this method exists just because the interface requires it)
428 void SAL_CALL
SampleAddIn::removeRefreshListener( const uno::Reference
< util::XRefreshListener
>& )
429 throw( uno::RuntimeException
)
431 // not implemented - this is not necessary
432 // (this method exists just because the interface requires it)
436 OUString SAL_CALL
SampleAddIn::getDiagramType() throw( uno::RuntimeException
)
438 return OUString::createFromAscii( "com.sun.star.chart.SampleDiagram" );
441 // the following methods just delegate to the "parent diagram" (which in the future might no longer exist)
443 uno::Reference
< beans::XPropertySet
> SAL_CALL
SampleAddIn::getDataRowProperties( sal_Int32 nRow
)
444 throw( lang::IndexOutOfBoundsException
,
445 uno::RuntimeException
)
449 uno::Reference
< chart::XDiagram
> xDia
= mxChartDoc
->getDiagram();
451 return xDia
->getDataRowProperties( nRow
);
454 return uno::Reference
< beans::XPropertySet
>();
457 uno::Reference
< beans::XPropertySet
> SAL_CALL
SampleAddIn::getDataPointProperties( sal_Int32 nCol
, sal_Int32 nRow
)
458 throw( lang::IndexOutOfBoundsException
,
459 uno::RuntimeException
)
463 uno::Reference
< chart::XDiagram
> xDia
= mxChartDoc
->getDiagram();
465 return xDia
->getDataPointProperties( nCol
, nRow
);
468 return uno::Reference
< beans::XPropertySet
>();
471 // XShape ( ::XDiagram )
472 awt::Size SAL_CALL
SampleAddIn::getSize()
473 throw( uno::RuntimeException
)
477 uno::Reference
< drawing::XShape
> xShape( mxChartDoc
->getDiagram(), uno::UNO_QUERY
);
479 return xShape
->getSize();
485 void SAL_CALL
SampleAddIn::setSize( const awt::Size
& aSize
)
486 throw( beans::PropertyVetoException
, uno::RuntimeException
)
490 uno::Reference
< drawing::XShape
> xShape( mxChartDoc
->getDiagram(), uno::UNO_QUERY
);
492 xShape
->setSize( aSize
);
496 awt::Point SAL_CALL
SampleAddIn::getPosition()
497 throw( uno::RuntimeException
)
501 uno::Reference
< drawing::XShape
> xShape( mxChartDoc
->getDiagram(), uno::UNO_QUERY
);
503 return xShape
->getPosition();
509 void SAL_CALL
SampleAddIn::setPosition( const awt::Point
& aPos
)
510 throw( uno::RuntimeException
)
514 uno::Reference
< drawing::XShape
> xShape( mxChartDoc
->getDiagram(), uno::UNO_QUERY
);
516 xShape
->setPosition( aPos
);
520 // XShapeDescriptor ( ::XShape ::XDiagram )
521 rtl::OUString SAL_CALL
SampleAddIn::getShapeType() throw( com::sun::star::uno::RuntimeException
)
523 return OUString::createFromAscii( "com.sun.star.chart.SampleAddinShape" );
527 uno::Reference
< drawing::XShape
> SAL_CALL
SampleAddIn::getXAxisTitle()
528 throw( uno::RuntimeException
)
532 uno::Reference
< chart::XAxisXSupplier
> xAxisSupp( mxChartDoc
->getDiagram(), uno::UNO_QUERY
);
534 return xAxisSupp
->getXAxisTitle();
537 return uno::Reference
< drawing::XShape
>();
540 uno::Reference
< beans::XPropertySet
> SAL_CALL
SampleAddIn::getXAxis()
541 throw( uno::RuntimeException
)
545 uno::Reference
< chart::XAxisXSupplier
> xAxisSupp( mxChartDoc
->getDiagram(), uno::UNO_QUERY
);
547 return xAxisSupp
->getXAxis();
550 return uno::Reference
< beans::XPropertySet
>();
553 uno::Reference
< beans::XPropertySet
> SAL_CALL
SampleAddIn::getXMainGrid()
554 throw( uno::RuntimeException
)
558 uno::Reference
< chart::XAxisXSupplier
> xAxisSupp( mxChartDoc
->getDiagram(), uno::UNO_QUERY
);
560 return xAxisSupp
->getXMainGrid();
563 return uno::Reference
< beans::XPropertySet
>();
566 uno::Reference
< beans::XPropertySet
> SAL_CALL
SampleAddIn::getXHelpGrid()
567 throw( uno::RuntimeException
)
571 uno::Reference
< chart::XAxisXSupplier
> xAxisSupp( mxChartDoc
->getDiagram(), uno::UNO_QUERY
);
573 return xAxisSupp
->getXHelpGrid();
576 return uno::Reference
< beans::XPropertySet
>();
580 uno::Reference
< drawing::XShape
> SAL_CALL
SampleAddIn::getYAxisTitle()
581 throw( uno::RuntimeException
)
585 uno::Reference
< chart::XAxisYSupplier
> xAxisSupp( mxChartDoc
->getDiagram(), uno::UNO_QUERY
);
587 return xAxisSupp
->getYAxisTitle();
590 return uno::Reference
< drawing::XShape
>();
593 uno::Reference
< beans::XPropertySet
> SAL_CALL
SampleAddIn::getYAxis()
594 throw( uno::RuntimeException
)
598 uno::Reference
< chart::XAxisYSupplier
> xAxisSupp( mxChartDoc
->getDiagram(), uno::UNO_QUERY
);
600 return xAxisSupp
->getYAxis();
603 return uno::Reference
< beans::XPropertySet
>();
606 uno::Reference
< beans::XPropertySet
> SAL_CALL
SampleAddIn::getYMainGrid()
607 throw( uno::RuntimeException
)
611 uno::Reference
< chart::XAxisYSupplier
> xAxisSupp( mxChartDoc
->getDiagram(), uno::UNO_QUERY
);
613 return xAxisSupp
->getYMainGrid();
616 return uno::Reference
< beans::XPropertySet
>();
619 uno::Reference
< beans::XPropertySet
> SAL_CALL
SampleAddIn::getYHelpGrid()
620 throw( uno::RuntimeException
)
624 uno::Reference
< chart::XAxisYSupplier
> xAxisSupp( mxChartDoc
->getDiagram(), uno::UNO_QUERY
);
626 return xAxisSupp
->getYHelpGrid();
629 return uno::Reference
< beans::XPropertySet
>();
633 uno::Reference
< beans::XPropertySet
> SAL_CALL
SampleAddIn::getUpBar()
634 throw( uno::RuntimeException
)
638 uno::Reference
< chart::XStatisticDisplay
> xStatDisp( mxChartDoc
->getDiagram(), uno::UNO_QUERY
);
640 return xStatDisp
->getUpBar();
643 return uno::Reference
< beans::XPropertySet
>();
646 uno::Reference
< beans::XPropertySet
> SAL_CALL
SampleAddIn::getDownBar()
647 throw( uno::RuntimeException
)
651 uno::Reference
< chart::XStatisticDisplay
> xStatDisp( mxChartDoc
->getDiagram(), uno::UNO_QUERY
);
653 return xStatDisp
->getDownBar();
656 return uno::Reference
< beans::XPropertySet
>();
659 uno::Reference
< beans::XPropertySet
> SAL_CALL
SampleAddIn::getMinMaxLine()
660 throw( uno::RuntimeException
)
664 uno::Reference
< chart::XStatisticDisplay
> xStatDisp( mxChartDoc
->getDiagram(), uno::UNO_QUERY
);
666 return xStatDisp
->getMinMaxLine();
669 return uno::Reference
< beans::XPropertySet
>();
673 OUString SAL_CALL
SampleAddIn::getServiceName() throw( uno::RuntimeException
)
675 return OUString::createFromAscii( "com.sun.star.chart.SampleAddIn" );
679 OUString SAL_CALL
SampleAddIn::getImplementationName() throw( uno::RuntimeException
)
681 return getImplementationName_Static();
684 sal_Bool SAL_CALL
SampleAddIn::supportsService( const OUString
& ServiceName
)
685 throw( uno::RuntimeException
)
687 uno::Sequence
< OUString
> aServiceSeq
= getSupportedServiceNames_Static();
689 sal_Int32 nLength
= aServiceSeq
.getLength();
690 for( sal_Int32 i
=0; i
< nLength
; i
++ )
692 if( ServiceName
.equals( aServiceSeq
[ i
] ))
699 uno::Sequence
< OUString
> SAL_CALL
SampleAddIn::getSupportedServiceNames()
700 throw( uno::RuntimeException
)
702 return getSupportedServiceNames_Static();
706 void SAL_CALL
SampleAddIn::setLocale( const lang::Locale
& eLocale
)
707 throw( uno::RuntimeException
)
712 lang::Locale SAL_CALL
SampleAddIn::getLocale()
713 throw( uno::RuntimeException
)