1 /* -*- Mode: C++; tab-width: 4; indent-tabs-mode: nil; c-basic-offset: 4 -*- */
3 * This file is part of the LibreOffice project.
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/.
9 * This file incorporates work covered by the following license notice:
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 .
19 #include "sampleaddin.hxx"
21 #include <cppuhelper/supportsservice.hxx>
22 #include <cppuhelper/factory.hxx>
23 #include <osl/diagnose.h>
25 #include <com/sun/star/drawing/XDrawPageSupplier.hpp>
26 #include <com/sun/star/drawing/XDrawPage.hpp>
27 #include <com/sun/star/chart/XChartDataArray.hpp>
28 #include <com/sun/star/text/XTextRange.hpp>
29 #include <com/sun/star/chart/X3DDisplay.hpp>
31 using namespace com::sun::star
;
33 // code for creating instances of SampleAddIn
37 sal_Bool SAL_CALL
component_writeInfo(
38 void * /*pServiceManager*/, registry::XRegistryKey
* pRegistryKey
)
44 OUString
aImpl( "/" );
45 aImpl
+= SampleAddIn::getImplementationName_Static();
46 aImpl
+= "/UNO/SERVICES";
48 uno::Reference
< registry::XRegistryKey
> xNewKey(
49 reinterpret_cast<registry::XRegistryKey
*>( pRegistryKey
)->createKey( aImpl
) );
51 uno::Sequence
< OUString
> aSequ
= SampleAddIn::getSupportedServiceNames_Static();
52 const OUString
* pArray
= aSequ
.getConstArray();
53 for( sal_Int32 i
= 0; i
< aSequ
.getLength(); i
++ )
54 xNewKey
->createKey( pArray
[i
] );
58 catch( const registry::InvalidRegistryException
& )
60 OSL_FAIL( "### InvalidRegistryException!" );
66 SAL_DLLPUBLIC_EXPORT
void * SAL_CALL
component_getFactory(
67 const sal_Char
* pImplName
, void * pServiceManager
, void * /*pRegistryKey*/ )
71 if ( pServiceManager
&&
72 OUString::createFromAscii( pImplName
) == SampleAddIn::getImplementationName_Static() )
74 uno::Reference
< lang::XSingleServiceFactory
> xFactory( cppu::createSingleFactory(
75 reinterpret_cast<lang::XMultiServiceFactory
*>( pServiceManager
),
76 SampleAddIn::getImplementationName_Static(),
77 SampleAddIn_CreateInstance
,
78 SampleAddIn::getSupportedServiceNames_Static() ) );
83 pRet
= xFactory
.get();
94 SampleAddIn::SampleAddIn()
99 SampleAddIn::~SampleAddIn()
102 // this functionality should be provided by the chart API some day
103 sal_Bool
SampleAddIn::getLogicalPosition( uno::Reference
< drawing::XShape
>& xAxis
,
106 awt::Point
& aOutPosition
)
108 sal_Bool bRet
= sal_False
;
112 awt::Size aSize
= xAxis
->getSize();
113 sal_Int32 nLength
= bVertical
? aSize
.Height
: aSize
.Width
;
115 uno::Reference
< beans::XPropertySet
> xProp( xAxis
, uno::UNO_QUERY
);
120 double fMin(0.0), fMax(0.0);
121 uno::Any aAny
= xProp
->getPropertyValue( "Min" );
123 aAny
= xProp
->getPropertyValue( "Max" );
126 double fRange
= fMax
- fMin
;
127 if( fMin
<= fValue
&& fValue
<= fMax
&&
130 double fPercentage
= (fValue
- fMin
) / fRange
;
131 awt::Point aPos
= xAxis
->getPosition();
135 aOutPosition
.X
= aPos
.X
;
136 aOutPosition
.Y
= static_cast<sal_Int32
>(aPos
.Y
+ nLength
* (1.0 - fPercentage
)); // y scale goes from top to bottom
140 aOutPosition
.X
= static_cast<sal_Int32
>(aPos
.X
+ nLength
* fPercentage
);
141 aOutPosition
.Y
= aPos
.Y
;
146 catch( const beans::UnknownPropertyException
& )
148 // the shape xAxis was no chart axis
156 OUString
SampleAddIn::getImplementationName_Static()
158 return "SampleAddIn";
161 uno::Sequence
< OUString
> SampleAddIn::getSupportedServiceNames_Static()
163 uno::Sequence
< OUString
> aSeq( 4 );
165 aSeq
[ 0 ] = "com.sun.star.chart.ChartAxisXSupplier";
166 aSeq
[ 1 ] = "com.sun.star.chart.ChartAxisYSupplier";
167 aSeq
[ 2 ] = "com.sun.star.chart.Diagram";
168 aSeq
[ 3 ] = "com.sun.star.chart.SampleAddIn";
173 uno::Reference
< uno::XInterface
> SAL_CALL
SampleAddIn_CreateInstance(
174 const uno::Reference
< lang::XMultiServiceFactory
>& )
176 uno::Reference
< uno::XInterface
> xInst
= (cppu::OWeakObject
*)new SampleAddIn();
181 // implementation of interface methods
184 void SAL_CALL
SampleAddIn::initialize( const uno::Sequence
< uno::Any
>& aArguments
)
185 throw( uno::Exception
, uno::RuntimeException
)
187 // first argument should be the XChartDocument
188 OSL_ENSURE( aArguments
.getLength() > 0, "Please initialize Chart AddIn with ChartDocument!" );
190 if( aArguments
.getLength())
192 aArguments
[ 0 ] >>= mxChartDoc
;
193 OSL_ENSURE( mxChartDoc
.is(), "First argument in initialization is not an XChartDocument!" );
195 // set XY chart as base type to be drawn
196 uno::Reference
< beans::XPropertySet
> xDocProp( mxChartDoc
, uno::UNO_QUERY
);
200 aBaseType
<<= "com.sun.star.chart.XYDiagram";
203 xDocProp
->setPropertyValue( "BaseDiagram" , aBaseType
);
209 // change background of plot area to light blue
210 uno::Reference
< chart::X3DDisplay
> xWallSupplier( mxChartDoc
->getDiagram(), uno::UNO_QUERY
);
211 if( xWallSupplier
.is())
213 uno::Reference
< beans::XPropertySet
> xDiaProp( xWallSupplier
->getWall(), uno::UNO_QUERY
);
214 uno::Reference
< beans::XPropertySet
> xLegendProp( mxChartDoc
->getLegend(), uno::UNO_QUERY
);
219 aAny
<<= (sal_Int32
)( 0xe0e0f0 );
220 xDiaProp
->setPropertyValue( "FillColor" , aAny
);
221 xLegendProp
->setPropertyValue( "FillColor" , aAny
);
228 /********************************************************************************
230 * The method refresh is the most important method - here all objects that
231 * are necessary for the chart are created
233 * in the first implementation you will have to insert everything in this
234 * routine - all old objects are deleted beforehand
236 ********************************************************************************/
237 void SAL_CALL
SampleAddIn::refresh() throw( uno::RuntimeException
)
239 if( ! mxChartDoc
.is())
242 // first of all get the draw page
243 uno::Reference
< drawing::XDrawPageSupplier
> xPageSupp( mxChartDoc
, uno::UNO_QUERY
);
244 uno::Reference
< lang::XMultiServiceFactory
> xFactory( mxChartDoc
, uno::UNO_QUERY
);
245 if( xPageSupp
.is() &&
248 uno::Reference
< drawing::XDrawPage
> xPage
= xPageSupp
->getDrawPage();
251 // now we have the page to insert objects
253 // add a horizontal line at the middle value of the first series
255 // get the logical position from the coordinate
257 uno::Reference
< drawing::XShape
> xYAxisShape( getYAxis(), uno::UNO_QUERY
);
258 uno::Reference
< drawing::XShape
> xXAxisShape( getXAxis(), uno::UNO_QUERY
);
260 if( xXAxisShape
.is() &&
263 // create line first time
264 if( ! mxMyRedLine
.is())
267 xFactory
->createInstance( "com.sun.star.drawing.LineShape" ),
269 xPage
->add( mxMyRedLine
);
271 // make line red and thick
272 uno::Reference
< beans::XPropertySet
> xShapeProp( mxMyRedLine
, uno::UNO_QUERY
);
275 uno::Any aColor
, aWidth
;
276 aColor
<<= (sal_Int32
)(0xe01010);
277 aWidth
<<= (sal_Int32
)(50); // 0.5 mm
280 xShapeProp
->setPropertyValue( "LineColor" , aColor
);
281 xShapeProp
->setPropertyValue( "LineWidth" , aWidth
);
287 // create text object first time
291 xFactory
->createInstance( "com.sun.star.drawing.TextShape" ),
293 xPage
->add( mxMyText
);
296 OUString
aText( "Little Example" );
297 uno::Reference
< beans::XPropertySet
> xTextProp( mxMyText
, uno::UNO_QUERY
);
301 aTrueAny
<<= (sal_Bool
)(sal_True
);
304 xTextProp
->setPropertyValue( "TextAutoGrowWidth" , aTrueAny
);
310 uno::Reference
< text::XTextRange
> xTextRange( mxMyText
, uno::UNO_QUERY
);
313 xTextRange
->setString( aText
);
317 // position line and text
319 // get the array. Note: the first dimension is the length
320 // of each series and the second one is the number of series
321 // this should be changed in the future
322 uno::Sequence
< uno::Sequence
< double > > aData
;
323 uno::Reference
< chart::XChartData
> xData
= mxChartDoc
->getData();
324 uno::Reference
< chart::XChartDataArray
> xDataArray( xData
, uno::UNO_QUERY
);
326 aData
= xDataArray
->getData();
328 // get row count == length of each series
329 sal_Int32 nSize
= aData
.getLength();
330 sal_Int32 nMiddle
= nSize
/ 2;
331 // get value for first series
332 double fMiddleVal
= xData
->getNotANumber(); // set to NaN
333 if( aData
[ nMiddle
].getLength()) // we have at least one series
334 fMiddleVal
= aData
[ nMiddle
][ 0 ];
337 getLogicalPosition( xYAxisShape
, fMiddleVal
, sal_True
, aPos
);
338 awt::Size aSize
= xXAxisShape
->getSize();
340 if( mxMyRedLine
.is())
342 awt::Point aEnd
= aPos
;
343 aEnd
.X
+= aSize
.Width
;
345 uno::Sequence
< uno::Sequence
< awt::Point
> > aPtSeq( 1 );
346 aPtSeq
[ 0 ].realloc( 2 );
347 aPtSeq
[ 0 ][ 0 ] = aPos
;
348 aPtSeq
[ 0 ][ 1 ] = aEnd
;
350 uno::Reference
< beans::XPropertySet
> xShapeProp( mxMyRedLine
, uno::UNO_QUERY
);
353 xShapeProp
->setPropertyValue( "PolyPolygon" , Any(aPtSeq
) );
358 // put the text centered below the red line
359 aPos
.X
+= ( aSize
.Width
- mxMyRedLine
->getPosition().X
) / 2;
361 aPos
.Y
+= static_cast<sal_Int32
>(0.1 * xYAxisShape
->getSize().Height
);
362 mxMyText
->setPosition( aPos
);
369 void SAL_CALL
SampleAddIn::addRefreshListener( const uno::Reference
< util::XRefreshListener
>& )
370 throw( uno::RuntimeException
)
372 // not implemented - this is not necessary
373 // (this method exists just because the interface requires it)
376 void SAL_CALL
SampleAddIn::removeRefreshListener( const uno::Reference
< util::XRefreshListener
>& )
377 throw( uno::RuntimeException
)
379 // not implemented - this is not necessary
380 // (this method exists just because the interface requires it)
384 OUString SAL_CALL
SampleAddIn::getDiagramType() throw( uno::RuntimeException
)
386 return "com.sun.star.chart.SampleDiagram";
389 // the following methods just delegate to the "parent diagram" (which in the future might no longer exist)
391 uno::Reference
< beans::XPropertySet
> SAL_CALL
SampleAddIn::getDataRowProperties( sal_Int32 nRow
)
392 throw( lang::IndexOutOfBoundsException
,
393 uno::RuntimeException
)
397 uno::Reference
< chart::XDiagram
> xDia
= mxChartDoc
->getDiagram();
399 return xDia
->getDataRowProperties( nRow
);
402 return uno::Reference
< beans::XPropertySet
>();
405 uno::Reference
< beans::XPropertySet
> SAL_CALL
SampleAddIn::getDataPointProperties( sal_Int32 nCol
, sal_Int32 nRow
)
406 throw( lang::IndexOutOfBoundsException
,
407 uno::RuntimeException
)
411 uno::Reference
< chart::XDiagram
> xDia
= mxChartDoc
->getDiagram();
413 return xDia
->getDataPointProperties( nCol
, nRow
);
416 return uno::Reference
< beans::XPropertySet
>();
419 // XShape ( ::XDiagram )
420 awt::Size SAL_CALL
SampleAddIn::getSize()
421 throw( uno::RuntimeException
)
425 uno::Reference
< drawing::XShape
> xShape( mxChartDoc
->getDiagram(), uno::UNO_QUERY
);
427 return xShape
->getSize();
433 void SAL_CALL
SampleAddIn::setSize( const awt::Size
& aSize
)
434 throw( beans::PropertyVetoException
, uno::RuntimeException
)
438 uno::Reference
< drawing::XShape
> xShape( mxChartDoc
->getDiagram(), uno::UNO_QUERY
);
440 xShape
->setSize( aSize
);
444 awt::Point SAL_CALL
SampleAddIn::getPosition()
445 throw( uno::RuntimeException
)
449 uno::Reference
< drawing::XShape
> xShape( mxChartDoc
->getDiagram(), uno::UNO_QUERY
);
451 return xShape
->getPosition();
457 void SAL_CALL
SampleAddIn::setPosition( const awt::Point
& aPos
)
458 throw( uno::RuntimeException
)
462 uno::Reference
< drawing::XShape
> xShape( mxChartDoc
->getDiagram(), uno::UNO_QUERY
);
464 xShape
->setPosition( aPos
);
468 // XShapeDescriptor ( ::XShape ::XDiagram )
469 OUString SAL_CALL
SampleAddIn::getShapeType() throw( css::uno::RuntimeException
)
471 return "com.sun.star.chart.SampleAddinShape";
475 uno::Reference
< drawing::XShape
> SAL_CALL
SampleAddIn::getXAxisTitle()
476 throw( uno::RuntimeException
)
480 uno::Reference
< chart::XAxisXSupplier
> xAxisSupp( mxChartDoc
->getDiagram(), uno::UNO_QUERY
);
482 return xAxisSupp
->getXAxisTitle();
485 return uno::Reference
< drawing::XShape
>();
488 uno::Reference
< beans::XPropertySet
> SAL_CALL
SampleAddIn::getXAxis()
489 throw( uno::RuntimeException
)
493 uno::Reference
< chart::XAxisXSupplier
> xAxisSupp( mxChartDoc
->getDiagram(), uno::UNO_QUERY
);
495 return xAxisSupp
->getXAxis();
498 return uno::Reference
< beans::XPropertySet
>();
501 uno::Reference
< beans::XPropertySet
> SAL_CALL
SampleAddIn::getXMainGrid()
502 throw( uno::RuntimeException
)
506 uno::Reference
< chart::XAxisXSupplier
> xAxisSupp( mxChartDoc
->getDiagram(), uno::UNO_QUERY
);
508 return xAxisSupp
->getXMainGrid();
511 return uno::Reference
< beans::XPropertySet
>();
514 uno::Reference
< beans::XPropertySet
> SAL_CALL
SampleAddIn::getXHelpGrid()
515 throw( uno::RuntimeException
)
519 uno::Reference
< chart::XAxisXSupplier
> xAxisSupp( mxChartDoc
->getDiagram(), uno::UNO_QUERY
);
521 return xAxisSupp
->getXHelpGrid();
524 return uno::Reference
< beans::XPropertySet
>();
528 uno::Reference
< drawing::XShape
> SAL_CALL
SampleAddIn::getYAxisTitle()
529 throw( uno::RuntimeException
)
533 uno::Reference
< chart::XAxisYSupplier
> xAxisSupp( mxChartDoc
->getDiagram(), uno::UNO_QUERY
);
535 return xAxisSupp
->getYAxisTitle();
538 return uno::Reference
< drawing::XShape
>();
541 uno::Reference
< beans::XPropertySet
> SAL_CALL
SampleAddIn::getYAxis()
542 throw( uno::RuntimeException
)
546 uno::Reference
< chart::XAxisYSupplier
> xAxisSupp( mxChartDoc
->getDiagram(), uno::UNO_QUERY
);
548 return xAxisSupp
->getYAxis();
551 return uno::Reference
< beans::XPropertySet
>();
554 uno::Reference
< beans::XPropertySet
> SAL_CALL
SampleAddIn::getYMainGrid()
555 throw( uno::RuntimeException
)
559 uno::Reference
< chart::XAxisYSupplier
> xAxisSupp( mxChartDoc
->getDiagram(), uno::UNO_QUERY
);
561 return xAxisSupp
->getYMainGrid();
564 return uno::Reference
< beans::XPropertySet
>();
567 uno::Reference
< beans::XPropertySet
> SAL_CALL
SampleAddIn::getYHelpGrid()
568 throw( uno::RuntimeException
)
572 uno::Reference
< chart::XAxisYSupplier
> xAxisSupp( mxChartDoc
->getDiagram(), uno::UNO_QUERY
);
574 return xAxisSupp
->getYHelpGrid();
577 return uno::Reference
< beans::XPropertySet
>();
581 uno::Reference
< beans::XPropertySet
> SAL_CALL
SampleAddIn::getUpBar()
582 throw( uno::RuntimeException
)
586 uno::Reference
< chart::XStatisticDisplay
> xStatDisp( mxChartDoc
->getDiagram(), uno::UNO_QUERY
);
588 return xStatDisp
->getUpBar();
591 return uno::Reference
< beans::XPropertySet
>();
594 uno::Reference
< beans::XPropertySet
> SAL_CALL
SampleAddIn::getDownBar()
595 throw( uno::RuntimeException
)
599 uno::Reference
< chart::XStatisticDisplay
> xStatDisp( mxChartDoc
->getDiagram(), uno::UNO_QUERY
);
601 return xStatDisp
->getDownBar();
604 return uno::Reference
< beans::XPropertySet
>();
607 uno::Reference
< beans::XPropertySet
> SAL_CALL
SampleAddIn::getMinMaxLine()
608 throw( uno::RuntimeException
)
612 uno::Reference
< chart::XStatisticDisplay
> xStatDisp( mxChartDoc
->getDiagram(), uno::UNO_QUERY
);
614 return xStatDisp
->getMinMaxLine();
617 return uno::Reference
< beans::XPropertySet
>();
621 OUString SAL_CALL
SampleAddIn::getServiceName() throw( uno::RuntimeException
)
623 return "com.sun.star.chart.SampleAddIn";
627 OUString SAL_CALL
SampleAddIn::getImplementationName() throw( uno::RuntimeException
)
629 return getImplementationName_Static();
632 sal_Bool SAL_CALL
SampleAddIn::supportsService( const OUString
& ServiceName
)
633 throw( uno::RuntimeException
)
635 return cppu::supportsService(this, ServiceName
);
638 uno::Sequence
< OUString
> SAL_CALL
SampleAddIn::getSupportedServiceNames()
639 throw( uno::RuntimeException
)
641 return getSupportedServiceNames_Static();
645 void SAL_CALL
SampleAddIn::setLocale( const lang::Locale
& eLocale
)
646 throw( uno::RuntimeException
)
651 lang::Locale SAL_CALL
SampleAddIn::getLocale()
652 throw( uno::RuntimeException
)
657 /* vim:set shiftwidth=4 softtabstop=4 expandtab: */