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: TitleHelper.cxx,v $
10 * $Revision: 1.10.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 ************************************************************************/
31 // MARKER(update_precomp.py): autogen include statement, do not remove
32 #include "precompiled_chart2.hxx"
33 #include "TitleHelper.hxx"
34 #include "ChartModelHelper.hxx"
36 #include "AxisHelper.hxx"
37 #include "DiagramHelper.hxx"
38 #include <com/sun/star/chart2/XChartDocument.hpp>
39 #include <rtl/ustrbuf.hxx>
41 //.............................................................................
44 //.............................................................................
46 using namespace ::com::sun::star
;
47 using namespace ::com::sun::star::chart2
;
48 using ::com::sun::star::uno::Reference
;
50 uno::Reference
< XTitled
> lcl_getTitleParentFromDiagram(
51 TitleHelper::eTitleType nTitleIndex
52 , const uno::Reference
< XDiagram
>& xDiagram
)
54 uno::Reference
< XTitled
> xResult
;
56 if( nTitleIndex
== TitleHelper::TITLE_AT_STANDARD_X_AXIS_POSITION
||
57 nTitleIndex
== TitleHelper::TITLE_AT_STANDARD_Y_AXIS_POSITION
)
60 bool bIsVertical
= DiagramHelper::getVertical( xDiagram
, bDummy
, bDummy
);
62 if( nTitleIndex
== TitleHelper::TITLE_AT_STANDARD_Y_AXIS_POSITION
)
63 nTitleIndex
= bIsVertical
? TitleHelper::X_AXIS_TITLE
: TitleHelper::Y_AXIS_TITLE
;
65 nTitleIndex
= bIsVertical
? TitleHelper::Y_AXIS_TITLE
: TitleHelper::X_AXIS_TITLE
;
71 case TitleHelper::SUB_TITLE
:
73 xResult
.set( xDiagram
, uno::UNO_QUERY
);
75 case TitleHelper::X_AXIS_TITLE
:
77 xResult
.set( AxisHelper::getAxis( 0, true, xDiagram
), uno::UNO_QUERY
);
79 case TitleHelper::Y_AXIS_TITLE
:
81 xResult
.set( AxisHelper::getAxis( 1, true, xDiagram
), uno::UNO_QUERY
);
83 case TitleHelper::Z_AXIS_TITLE
:
85 xResult
.set( AxisHelper::getAxis( 2, true, xDiagram
), uno::UNO_QUERY
);
87 case TitleHelper::SECONDARY_X_AXIS_TITLE
:
89 xResult
.set( AxisHelper::getAxis( 0, false, xDiagram
), uno::UNO_QUERY
);
91 case TitleHelper::SECONDARY_Y_AXIS_TITLE
:
93 xResult
.set( AxisHelper::getAxis( 1, false, xDiagram
), uno::UNO_QUERY
);
96 case TitleHelper::MAIN_TITLE
:
98 OSL_ENSURE( false, "Unsupported Title-Type requested" );
105 uno::Reference
< XTitled
> lcl_getTitleParent( TitleHelper::eTitleType nTitleIndex
106 , const uno::Reference
< frame::XModel
>& xModel
)
108 uno::Reference
< XTitled
> xResult
;
109 uno::Reference
< XChartDocument
> xChartDoc( xModel
, uno::UNO_QUERY
);
110 uno::Reference
< XDiagram
> xDiagram
;
112 xDiagram
.set( xChartDoc
->getFirstDiagram());
114 switch( nTitleIndex
)
116 case TitleHelper::MAIN_TITLE
:
117 xResult
.set( xModel
, uno::UNO_QUERY
);
119 case TitleHelper::SUB_TITLE
:
120 case TitleHelper::X_AXIS_TITLE
:
121 case TitleHelper::Y_AXIS_TITLE
:
122 case TitleHelper::Z_AXIS_TITLE
:
123 case TitleHelper::TITLE_AT_STANDARD_X_AXIS_POSITION
:
124 case TitleHelper::TITLE_AT_STANDARD_Y_AXIS_POSITION
:
125 case TitleHelper::SECONDARY_X_AXIS_TITLE
:
126 case TitleHelper::SECONDARY_Y_AXIS_TITLE
:
127 xResult
.set( lcl_getTitleParentFromDiagram( nTitleIndex
, xDiagram
));
130 OSL_ENSURE( false, "Unsupported Title-Type requested" );
137 uno::Reference
< XTitle
> TitleHelper::getTitle( TitleHelper::eTitleType nTitleIndex
138 , const uno::Reference
< frame::XModel
>& xModel
)
140 uno::Reference
< XTitled
> xTitled( lcl_getTitleParent( nTitleIndex
, xModel
) );
142 return xTitled
->getTitleObject();
146 uno::Reference
< XTitle
> TitleHelper::createTitle(
147 TitleHelper::eTitleType eTitleType
148 , const rtl::OUString
& rTitleText
149 , const uno::Reference
< frame::XModel
>& xModel
150 , const uno::Reference
< uno::XComponentContext
> & xContext
151 , ReferenceSizeProvider
* pRefSizeProvider
)
153 uno::Reference
< XTitle
> xTitle
;
154 uno::Reference
< XTitled
> xTitled( lcl_getTitleParent( eTitleType
, xModel
) );
158 uno::Reference
< XDiagram
> xDiagram( ChartModelHelper::findDiagram( xModel
) );
159 uno::Reference
< chart2::XAxis
> xAxis
;
162 case TitleHelper::SECONDARY_X_AXIS_TITLE
:
163 xAxis
= AxisHelper::createAxis( 0, false, xDiagram
, xContext
);
165 case TitleHelper::SECONDARY_Y_AXIS_TITLE
:
166 xAxis
= AxisHelper::createAxis( 1, false, xDiagram
, xContext
);
171 uno::Reference
< beans::XPropertySet
> xProps( xAxis
, uno::UNO_QUERY
);
174 xProps
->setPropertyValue( C2U( "Show" ), uno::makeAny( sal_False
) );
175 xTitled
= lcl_getTitleParent( eTitleType
, xModel
);
181 uno::Reference
< XDiagram
> xDiagram( ChartModelHelper::findDiagram( xModel
) );
183 xTitle
.set( xContext
->getServiceManager()->createInstanceWithContext(
184 C2U( "com.sun.star.chart2.Title" ),
185 xContext
), uno::UNO_QUERY
);
189 // default char height (main: 13.0 == default)
190 float fDefaultCharHeightSub
= 11.0;
191 float fDefaultCharHeightAxis
= 9.0;
194 case TitleHelper::SUB_TITLE
:
195 TitleHelper::setCompleteString(
196 rTitleText
, xTitle
, xContext
, & fDefaultCharHeightSub
);
198 case TitleHelper::X_AXIS_TITLE
:
199 case TitleHelper::Y_AXIS_TITLE
:
200 case TitleHelper::Z_AXIS_TITLE
:
201 case TitleHelper::TITLE_AT_STANDARD_X_AXIS_POSITION
:
202 case TitleHelper::TITLE_AT_STANDARD_Y_AXIS_POSITION
:
203 case TitleHelper::SECONDARY_X_AXIS_TITLE
:
204 case TitleHelper::SECONDARY_Y_AXIS_TITLE
:
205 TitleHelper::setCompleteString(
206 rTitleText
, xTitle
, xContext
, & fDefaultCharHeightAxis
);
209 TitleHelper::setCompleteString( rTitleText
, xTitle
, xContext
);
213 // set/clear autoscale
214 if( pRefSizeProvider
)
215 pRefSizeProvider
->setValuesAtTitle( xTitle
);
217 xTitled
->setTitleObject( xTitle
);
219 //default rotation 90 degree for y axis title in normal coordinatesystems or for x axis title for swapped coordinatesystems
220 if( eTitleType
== TitleHelper::X_AXIS_TITLE
||
221 eTitleType
== TitleHelper::Y_AXIS_TITLE
||
222 eTitleType
== TitleHelper::SECONDARY_X_AXIS_TITLE
||
223 eTitleType
== TitleHelper::SECONDARY_Y_AXIS_TITLE
)
229 bool bIsVertical
= DiagramHelper::getVertical( xDiagram
, bDummy
, bDummy
);
231 Reference
< beans::XPropertySet
> xTitleProps( xTitle
, uno::UNO_QUERY
);
232 if( xTitleProps
.is() )
234 double fNewAngleDegree
= 90.0;
235 if( (!bIsVertical
&& eTitleType
== TitleHelper::Y_AXIS_TITLE
)
236 || (bIsVertical
&& eTitleType
== TitleHelper::X_AXIS_TITLE
)
237 || (!bIsVertical
&& eTitleType
== TitleHelper::SECONDARY_Y_AXIS_TITLE
)
238 || (bIsVertical
&& eTitleType
== TitleHelper::SECONDARY_X_AXIS_TITLE
) )
239 xTitleProps
->setPropertyValue( C2U( "TextRotation" ), uno::makeAny( fNewAngleDegree
));
242 catch( uno::Exception
& ex
)
244 ASSERT_EXCEPTION( ex
);
253 rtl::OUString
TitleHelper::getCompleteString( const uno::Reference
< XTitle
>& xTitle
)
258 uno::Sequence
< uno::Reference
< XFormattedString
> > aStringList
= xTitle
->getText();
259 for( sal_Int32 nN
=0; nN
<aStringList
.getLength();nN
++ )
260 aRet
+= aStringList
[nN
]->getString();
264 void TitleHelper::setCompleteString( const rtl::OUString
& rNewText
265 , const uno::Reference
< XTitle
>& xTitle
266 , const uno::Reference
< uno::XComponentContext
> & xContext
267 , float * pDefaultCharHeight
/* = 0 */ )
269 //the format of the first old text portion will be maintained if there is any
273 rtl::OUString aNewText
= rNewText
;
275 bool bStacked
= false;
276 uno::Reference
< beans::XPropertySet
> xTitleProperties( xTitle
, uno::UNO_QUERY
);
277 if( xTitleProperties
.is() )
278 xTitleProperties
->getPropertyValue( C2U( "StackCharacters" ) ) >>= bStacked
;
282 //#i99841# remove linebreaks that were added for vertical stacking
283 rtl::OUStringBuffer aUnstackedStr
;
284 rtl::OUStringBuffer
aSource(rNewText
);
286 bool bBreakIgnored
= false;
287 sal_Int32 nLen
= rNewText
.getLength();
288 for( sal_Int32 nPos
= 0; nPos
< nLen
; ++nPos
)
290 sal_Unicode aChar
= aSource
.charAt( nPos
);
293 aUnstackedStr
.append( aChar
);
294 bBreakIgnored
= false;
296 else if( aChar
== '\n' && bBreakIgnored
)
297 aUnstackedStr
.append( aChar
);
299 bBreakIgnored
= true;
301 aNewText
= aUnstackedStr
.makeStringAndClear();
304 uno::Sequence
< uno::Reference
< XFormattedString
> > aNewStringList(1);
306 uno::Sequence
< uno::Reference
< XFormattedString
> > aOldStringList
= xTitle
->getText();
307 if( aOldStringList
.getLength() )
309 aNewStringList
[0].set( aOldStringList
[0] );
310 aNewStringList
[0]->setString( aNewText
);
314 uno::Reference
< uno::XInterface
> xI(
315 xContext
->getServiceManager()->createInstanceWithContext(
316 C2U( "com.sun.star.chart2.FormattedString" ), xContext
) );
317 uno::Reference
< XFormattedString
> xFormattedString( xI
, uno::UNO_QUERY
);
319 if(xFormattedString
.is())
321 xFormattedString
->setString( aNewText
);
322 aNewStringList
[0].set( xFormattedString
);
323 if( pDefaultCharHeight
!= 0 )
327 uno::Reference
< beans::XPropertySet
> xProp( xFormattedString
, uno::UNO_QUERY_THROW
);
329 uno::Any
aFontSize( uno::makeAny( *pDefaultCharHeight
));
330 xProp
->setPropertyValue( C2U("CharHeight"), aFontSize
);
331 xProp
->setPropertyValue( C2U("CharHeightAsian"), aFontSize
);
332 xProp
->setPropertyValue( C2U("CharHeightComplex"), aFontSize
);
334 catch( uno::Exception
& ex
)
336 ASSERT_EXCEPTION( ex
);
341 xTitle
->setText( aNewStringList
);
344 void TitleHelper::removeTitle( TitleHelper::eTitleType nTitleIndex
345 , const ::com::sun::star::uno::Reference
<
346 ::com::sun::star::frame::XModel
>& xModel
)
348 uno::Reference
< XTitled
> xTitled( lcl_getTitleParent( nTitleIndex
, xModel
) );
351 xTitled
->setTitleObject(NULL
);
355 bool TitleHelper::getTitleType( eTitleType
& rType
356 , const ::com::sun::star::uno::Reference
<
357 ::com::sun::star::chart2::XTitle
>& xTitle
358 , const ::com::sun::star::uno::Reference
<
359 ::com::sun::star::frame::XModel
>& xModel
)
361 if( !xTitle
.is() || !xModel
.is() )
364 Reference
< chart2::XTitle
> xCurrentTitle
;
365 for( sal_Int32 nTitleType
= TITLE_BEGIN
; nTitleType
< NORMAL_TITLE_END
; nTitleType
++ )
367 xCurrentTitle
= TitleHelper::getTitle( static_cast<eTitleType
>(nTitleType
), xModel
);
368 if( xCurrentTitle
== xTitle
)
370 rType
= static_cast<eTitleType
>(nTitleType
);
378 //.............................................................................
380 //.............................................................................