merge the formfield patch from ooo-build
[ooovba.git] / chart2 / source / tools / TitleHelper.cxx
blobbba2cd8ce97740d5069ab445acb172ebb510ed18
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: 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"
35 #include "macros.hxx"
36 #include "AxisHelper.hxx"
37 #include "DiagramHelper.hxx"
38 #include <com/sun/star/chart2/XChartDocument.hpp>
39 #include <rtl/ustrbuf.hxx>
41 //.............................................................................
42 namespace chart
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 )
59 bool bDummy = false;
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;
64 else
65 nTitleIndex = bIsVertical ? TitleHelper::Y_AXIS_TITLE : TitleHelper::X_AXIS_TITLE;
69 switch( nTitleIndex )
71 case TitleHelper::SUB_TITLE:
72 if( xDiagram.is())
73 xResult.set( xDiagram, uno::UNO_QUERY );
74 break;
75 case TitleHelper::X_AXIS_TITLE:
76 if( xDiagram.is())
77 xResult.set( AxisHelper::getAxis( 0, true, xDiagram ), uno::UNO_QUERY );
78 break;
79 case TitleHelper::Y_AXIS_TITLE:
80 if( xDiagram.is())
81 xResult.set( AxisHelper::getAxis( 1, true, xDiagram ), uno::UNO_QUERY );
82 break;
83 case TitleHelper::Z_AXIS_TITLE:
84 if( xDiagram.is())
85 xResult.set( AxisHelper::getAxis( 2, true, xDiagram ), uno::UNO_QUERY );
86 break;
87 case TitleHelper::SECONDARY_X_AXIS_TITLE:
88 if( xDiagram.is())
89 xResult.set( AxisHelper::getAxis( 0, false, xDiagram ), uno::UNO_QUERY );
90 break;
91 case TitleHelper::SECONDARY_Y_AXIS_TITLE:
92 if( xDiagram.is())
93 xResult.set( AxisHelper::getAxis( 1, false, xDiagram ), uno::UNO_QUERY );
94 break;
96 case TitleHelper::MAIN_TITLE:
97 default:
98 OSL_ENSURE( false, "Unsupported Title-Type requested" );
99 break;
102 return xResult;
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;
111 if( xChartDoc.is())
112 xDiagram.set( xChartDoc->getFirstDiagram());
114 switch( nTitleIndex )
116 case TitleHelper::MAIN_TITLE:
117 xResult.set( xModel, uno::UNO_QUERY );
118 break;
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 ));
128 break;
129 default:
130 OSL_ENSURE( false, "Unsupported Title-Type requested" );
131 break;
134 return xResult;
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 ) );
141 if( xTitled.is())
142 return xTitled->getTitleObject();
143 return NULL;
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 ) );
156 if( !xTitled.is() )
158 uno::Reference< XDiagram > xDiagram( ChartModelHelper::findDiagram( xModel ) );
159 uno::Reference< chart2::XAxis > xAxis;
160 switch( eTitleType )
162 case TitleHelper::SECONDARY_X_AXIS_TITLE:
163 xAxis = AxisHelper::createAxis( 0, false, xDiagram, xContext );
164 break;
165 case TitleHelper::SECONDARY_Y_AXIS_TITLE:
166 xAxis = AxisHelper::createAxis( 1, false, xDiagram, xContext );
167 break;
168 default:
169 break;
171 uno::Reference< beans::XPropertySet > xProps( xAxis, uno::UNO_QUERY );
172 if( xProps.is() )
174 xProps->setPropertyValue( C2U( "Show" ), uno::makeAny( sal_False ) );
175 xTitled = lcl_getTitleParent( eTitleType, xModel );
179 if(xTitled.is())
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 );
187 if(xTitle.is())
189 // default char height (main: 13.0 == default)
190 float fDefaultCharHeightSub = 11.0;
191 float fDefaultCharHeightAxis = 9.0;
192 switch( eTitleType )
194 case TitleHelper::SUB_TITLE:
195 TitleHelper::setCompleteString(
196 rTitleText, xTitle, xContext, & fDefaultCharHeightSub );
197 break;
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 );
207 break;
208 default:
209 TitleHelper::setCompleteString( rTitleText, xTitle, xContext );
210 break;
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 )
228 bool bDummy = false;
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 );
249 return xTitle;
253 rtl::OUString TitleHelper::getCompleteString( const uno::Reference< XTitle >& xTitle )
255 rtl::OUString aRet;
256 if(!xTitle.is())
257 return aRet;
258 uno::Sequence< uno::Reference< XFormattedString > > aStringList = xTitle->getText();
259 for( sal_Int32 nN=0; nN<aStringList.getLength();nN++ )
260 aRet += aStringList[nN]->getString();
261 return aRet;
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
270 if(!xTitle.is())
271 return;
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;
280 if( 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 );
291 if( aChar != '\n' )
293 aUnstackedStr.append( aChar );
294 bBreakIgnored = false;
296 else if( aChar == '\n' && bBreakIgnored )
297 aUnstackedStr.append( aChar );
298 else
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 );
312 else
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 ) );
349 if( xTitled.is())
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() )
362 return false;
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);
371 return true;
375 return false;
378 //.............................................................................
379 } //namespace chart
380 //.............................................................................