xref: /trunk/main/oox/source/ppt/pptshape.cxx (revision ca5ec200)
1 /**************************************************************
2  *
3  * Licensed to the Apache Software Foundation (ASF) under one
4  * or more contributor license agreements.  See the NOTICE file
5  * distributed with this work for additional information
6  * regarding copyright ownership.  The ASF licenses this file
7  * to you under the Apache License, Version 2.0 (the
8  * "License"); you may not use this file except in compliance
9  * with the License.  You may obtain a copy of the License at
10  *
11  *   http://www.apache.org/licenses/LICENSE-2.0
12  *
13  * Unless required by applicable law or agreed to in writing,
14  * software distributed under the License is distributed on an
15  * "AS IS" BASIS, WITHOUT WARRANTIES OR CONDITIONS OF ANY
16  * KIND, either express or implied.  See the License for the
17  * specific language governing permissions and limitations
18  * under the License.
19  *
20  *************************************************************/
21 
22 
23 
24 #include "oox/ppt/pptshape.hxx"
25 #include "oox/core/xmlfilterbase.hxx"
26 #include "oox/drawingml/textbody.hxx"
27 
28 #include <com/sun/star/container/XNamed.hpp>
29 #include <com/sun/star/beans/XMultiPropertySet.hpp>
30 #include <com/sun/star/lang/XMultiServiceFactory.hpp>
31 #include <com/sun/star/drawing/HomogenMatrix3.hpp>
32 #include <com/sun/star/text/XText.hpp>
33 #include <basegfx/matrix/b2dhommatrix.hxx>
34 #include "oox/ppt/slidepersist.hxx"
35 
36 using rtl::OUString;
37 using namespace ::oox::core;
38 using namespace ::oox::drawingml;
39 using namespace ::com::sun::star;
40 using namespace ::com::sun::star::awt;
41 using namespace ::com::sun::star::uno;
42 using namespace ::com::sun::star::beans;
43 using namespace ::com::sun::star::frame;
44 using namespace ::com::sun::star::text;
45 using namespace ::com::sun::star::drawing;
46 
47 namespace oox { namespace ppt {
48 
PPTShape(const oox::ppt::ShapeLocation eShapeLocation,const sal_Char * pServiceName)49 PPTShape::PPTShape( const oox::ppt::ShapeLocation eShapeLocation, const sal_Char* pServiceName )
50 : Shape( pServiceName )
51 , meShapeLocation( eShapeLocation )
52 , mbReferenced( sal_False )
53 {
54 }
55 
~PPTShape()56 PPTShape::~PPTShape()
57 {
58 }
59 
addShape(oox::core::XmlFilterBase & rFilterBase,const SlidePersist & rSlidePersist,const oox::drawingml::Theme * pTheme,const Reference<XShapes> & rxShapes,const awt::Rectangle * pShapeRect,::oox::drawingml::ShapeIdMap * pShapeMap)60 void PPTShape::addShape(
61         oox::core::XmlFilterBase& rFilterBase,
62         const SlidePersist& rSlidePersist,
63         const oox::drawingml::Theme* pTheme,
64         const Reference< XShapes >& rxShapes,
65         const awt::Rectangle* pShapeRect,
66         ::oox::drawingml::ShapeIdMap* pShapeMap )
67 {
68 	// only placeholder from layout are being inserted
69 	if ( mnSubType && ( meShapeLocation == Master ) )
70 		return;
71 	try
72     {
73 		rtl::OUString sServiceName( msServiceName );
74 		if( sServiceName.getLength() )
75 		{
76 			oox::drawingml::TextListStylePtr aMasterTextListStyle;
77             Reference< lang::XMultiServiceFactory > xServiceFact( rFilterBase.getModel(), UNO_QUERY_THROW );
78 			sal_Bool bClearText = sal_False;
79 
80 			if ( sServiceName != OUString::createFromAscii( "com.sun.star.drawing.GraphicObjectShape" ) )
81 			{
82 				switch( mnSubType )
83 				{
84 					case XML_ctrTitle :
85 					case XML_title :
86 					{
87 						const rtl::OUString sTitleShapeService( RTL_CONSTASCII_USTRINGPARAM( "com.sun.star.presentation.TitleTextShape" ) );
88 						sServiceName = sTitleShapeService;
89 						aMasterTextListStyle = rSlidePersist.getMasterPersist().get() ? rSlidePersist.getMasterPersist()->getTitleTextStyle() : rSlidePersist.getTitleTextStyle();
90 					}
91 					break;
92 					case XML_subTitle :
93 					{
94 						if ( ( meShapeLocation == Master ) || ( meShapeLocation == Layout ) )
95 							sServiceName = rtl::OUString();
96 						else {
97 							const rtl::OUString sTitleShapeService( RTL_CONSTASCII_USTRINGPARAM( "com.sun.star.presentation.SubtitleShape" ) );
98 							sServiceName = sTitleShapeService;
99 							aMasterTextListStyle = rSlidePersist.getMasterPersist().get() ? rSlidePersist.getMasterPersist()->getTitleTextStyle() : rSlidePersist.getTitleTextStyle();
100 						}
101 					}
102 					break;
103    				    case XML_obj :
104 					{
105 						const rtl::OUString sOutlinerShapeService( RTL_CONSTASCII_USTRINGPARAM( "com.sun.star.presentation.OutlinerShape" ) );
106 						sServiceName = sOutlinerShapeService;
107 						aMasterTextListStyle = rSlidePersist.getMasterPersist().get() ? rSlidePersist.getMasterPersist()->getBodyTextStyle() : rSlidePersist.getBodyTextStyle();
108 					}
109 					break;
110 					case XML_body :
111 					{
112 						const rtl::OUString sNotesShapeService( RTL_CONSTASCII_USTRINGPARAM( "com.sun.star.presentation.NotesShape" ) );
113 						const rtl::OUString sOutlinerShapeService( RTL_CONSTASCII_USTRINGPARAM( "com.sun.star.presentation.OutlinerShape" ) );
114 						if ( rSlidePersist.isNotesPage() )
115 						{
116 							sServiceName = sNotesShapeService;
117 							aMasterTextListStyle = rSlidePersist.getMasterPersist().get() ? rSlidePersist.getMasterPersist()->getNotesTextStyle() : rSlidePersist.getNotesTextStyle();
118 						}
119 						else
120 						{
121 							sServiceName = sOutlinerShapeService;
122 							aMasterTextListStyle = rSlidePersist.getMasterPersist().get() ? rSlidePersist.getMasterPersist()->getBodyTextStyle() : rSlidePersist.getBodyTextStyle();
123 						}
124 					}
125 					break;
126 					case XML_dt :
127 					{
128 						const rtl::OUString sDateTimeShapeService( RTL_CONSTASCII_USTRINGPARAM( "com.sun.star.presentation.DateTimeShape" ) );
129 						sServiceName = sDateTimeShapeService;
130 						bClearText = sal_True;
131 					}
132 					break;
133 					case XML_hdr :
134 					{
135 						const rtl::OUString sHeaderShapeService( RTL_CONSTASCII_USTRINGPARAM( "com.sun.star.presentation.HeaderShape" ) );
136 						sServiceName = sHeaderShapeService;
137 						bClearText = sal_True;
138 					}
139 					break;
140 					case XML_ftr :
141 					{
142 						const rtl::OUString sFooterShapeService( RTL_CONSTASCII_USTRINGPARAM( "com.sun.star.presentation.FooterShape" ) );
143 						sServiceName = sFooterShapeService;
144 						bClearText = sal_True;
145 					}
146 					break;
147 					case XML_sldNum :
148 					{
149 						const rtl::OUString sSlideNumberShapeService( RTL_CONSTASCII_USTRINGPARAM( "com.sun.star.presentation.SlideNumberShape" ) );
150 						sServiceName = sSlideNumberShapeService;
151 						bClearText = sal_True;
152 					}
153 					break;
154 					case XML_sldImg :
155 					{
156 						const rtl::OUString sPageShapeService( RTL_CONSTASCII_USTRINGPARAM( "com.sun.star.presentation.PageShape" ) );
157 						sServiceName = sPageShapeService;
158 					}
159 					break;
160 
161 					default:
162 					break;
163 				}
164 			}
165 
166 /*
167 			// use placeholder index if possible
168 			if( mnSubType && getSubTypeIndex() && rSlidePersist.getMasterPersist().get() ) {
169 			    oox::drawingml::ShapePtr pPlaceholder = PPTShape::findPlaceholderByIndex( getSubTypeIndex(), rSlidePersist.getMasterPersist()->getShapes()->getChildren() );
170 			    if( pPlaceholder.get() && pPlaceholder->getTextBody() ) {
171 				TextListStylePtr pNewTextListStyle ( new TextListStyle() );
172 
173 				pNewTextListStyle->apply( pPlaceholder->getTextBody()->getTextListStyle() );
174 				if( pPlaceholder->getMasterTextListStyle().get() )
175 				    pNewTextListStyle->apply( *pPlaceholder->getMasterTextListStyle() );
176 
177 				aMasterTextListStyle = pNewTextListStyle;
178 			    }
179 			}
180 */
181 			if ( sServiceName.getLength() )
182 			{
183 				if ( !aMasterTextListStyle.get() )
184 					aMasterTextListStyle = rSlidePersist.getMasterPersist().get() ? rSlidePersist.getMasterPersist()->getOtherTextStyle() : rSlidePersist.getOtherTextStyle();
185 				setMasterTextListStyle( aMasterTextListStyle );
186 
187 				Reference< XShape > xShape( createAndInsert( rFilterBase, sServiceName, pTheme, rxShapes, pShapeRect, bClearText ) );
188 				if ( !rSlidePersist.isMasterPage() && rSlidePersist.getPage().is() && ( (sal_Int32)mnSubType == XML_title ) )
189  				{
190 					try
191 					{
192 						rtl::OUString aTitleText;
193 						Reference< XTextRange > xText( xShape, UNO_QUERY_THROW );
194 						aTitleText = xText->getString();
195 						if ( aTitleText.getLength() && ( aTitleText.getLength() < 64 ) )	// just a magic value, but we don't want to set slide names which are too long
196 						{
197 							Reference< container::XNamed > xName( rSlidePersist.getPage(), UNO_QUERY_THROW );
198 							xName->setName( aTitleText );
199 						}
200 					}
201 					catch( uno::Exception& )
202 					{
203 
204 					}
205 				}
206 				if( pShapeMap && msId.getLength() )
207 				{
208 					(*pShapeMap)[ msId ] = shared_from_this();
209 				}
210 
211 				// if this is a group shape, we have to add also each child shape
212 				Reference< XShapes > xShapes( xShape, UNO_QUERY );
213 				if ( xShapes.is() )
214 					addChildren( rFilterBase, *this, pTheme, xShapes, pShapeRect ? *pShapeRect : awt::Rectangle( maPosition.X, maPosition.Y, maSize.Width, maSize.Height ), pShapeMap );
215             }
216 		}
217 	}
218 	catch( const Exception&  )
219 	{
220 	}
221 }
222 
applyShapeReference(const oox::drawingml::Shape & rReferencedShape)223 void PPTShape::applyShapeReference( const oox::drawingml::Shape& rReferencedShape )
224 {
225 	Shape::applyShapeReference( rReferencedShape );
226 }
227 
findPlaceholder(const sal_Int32 nMasterPlaceholder,std::vector<oox::drawingml::ShapePtr> & rShapes)228 oox::drawingml::ShapePtr PPTShape::findPlaceholder( const sal_Int32 nMasterPlaceholder, std::vector< oox::drawingml::ShapePtr >& rShapes )
229 {
230 	oox::drawingml::ShapePtr aShapePtr;
231 	std::vector< oox::drawingml::ShapePtr >::reverse_iterator aRevIter( rShapes.rbegin() );
232 	while( aRevIter != rShapes.rend() )
233 	{
234 		if ( (*aRevIter)->getSubType() == nMasterPlaceholder )
235 		{
236 			aShapePtr = *aRevIter;
237 			break;
238 		}
239         std::vector< oox::drawingml::ShapePtr >& rChildren = (*aRevIter)->getChildren();
240         aShapePtr = findPlaceholder( nMasterPlaceholder, rChildren );
241 		if ( aShapePtr.get() )
242 			break;
243 		aRevIter++;
244 	}
245 	return aShapePtr;
246 }
247 
findPlaceholderByIndex(const sal_Int32 nIdx,std::vector<oox::drawingml::ShapePtr> & rShapes)248 oox::drawingml::ShapePtr PPTShape::findPlaceholderByIndex( const sal_Int32 nIdx, std::vector< oox::drawingml::ShapePtr >& rShapes )
249 {
250 	oox::drawingml::ShapePtr aShapePtr;
251 	std::vector< oox::drawingml::ShapePtr >::reverse_iterator aRevIter( rShapes.rbegin() );
252 	while( aRevIter != rShapes.rend() )
253 	{
254 		if ( (*aRevIter)->getSubTypeIndex() == nIdx )
255 		{
256 			aShapePtr = *aRevIter;
257 			break;
258 		}
259 		std::vector< oox::drawingml::ShapePtr >& rChildren = (*aRevIter)->getChildren();
260 		aShapePtr = findPlaceholderByIndex( nIdx, rChildren );
261 		if ( aShapePtr.get() )
262 		    break;
263 		aRevIter++;
264 	}
265 	return aShapePtr;
266 }
267 
268 // if nFirstPlaceholder can't be found, it will be searched for nSecondPlaceholder
findPlaceholder(sal_Int32 nFirstPlaceholder,sal_Int32 nSecondPlaceholder,std::vector<oox::drawingml::ShapePtr> & rShapes)269 oox::drawingml::ShapePtr PPTShape::findPlaceholder( sal_Int32 nFirstPlaceholder, sal_Int32 nSecondPlaceholder, std::vector< oox::drawingml::ShapePtr >& rShapes )
270 {
271 	oox::drawingml::ShapePtr pPlaceholder = findPlaceholder( nFirstPlaceholder, rShapes );
272 	return !nSecondPlaceholder || pPlaceholder.get() ? pPlaceholder : findPlaceholder( nSecondPlaceholder, rShapes );
273 }
274 
275 } }
276