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/drawingml/table/tablecell.hxx"
25 #include "oox/drawingml/table/tableproperties.hxx"
26 #include "oox/drawingml/shapepropertymap.hxx"
27 #include "oox/drawingml/textbody.hxx"
28 #include "oox/core/xmlfilterbase.hxx"
29 #include "oox/helper/propertyset.hxx"
30 #include <com/sun/star/container/XNameContainer.hpp>
31 #include <com/sun/star/beans/XMultiPropertySet.hpp>
32 #include <com/sun/star/table/XTable.hpp>
33 #include <com/sun/star/table/XMergeableCellRange.hpp>
34 #include <com/sun/star/table/BorderLine.hpp>
35 #include <com/sun/star/drawing/LineStyle.hpp>
36 #include <com/sun/star/drawing/TextVerticalAdjust.hpp>
37 #include <com/sun/star/drawing/TextHorizontalAdjust.hpp>
38 #include <com/sun/star/text/XText.hpp>
39 
40 using rtl::OUString;
41 using namespace ::oox::core;
42 using namespace ::com::sun::star;
43 using namespace ::com::sun::star::uno;
44 using namespace ::com::sun::star::beans;
45 using ::com::sun::star::table::BorderLine;
46 using ::com::sun::star::drawing::LineStyle;
47 
48 namespace oox { namespace drawingml { namespace table {
49 
50 TableCell::TableCell()
51 : mnRowSpan ( 1 )
52 , mnGridSpan( 1 )
53 , mbhMerge( sal_False )
54 , mbvMerge( sal_False )
55 , mnMarL( 91440 )
56 , mnMarR( 91440 )
57 , mnMarT( 45720 )
58 , mnMarB( 45720 )
59 , mnVertToken( XML_horz )
60 , mnAnchorToken( XML_t )
61 , mbAnchorCtr( sal_False )
62 , mnHorzOverflowToken( XML_clip )
63 {
64 }
65 TableCell::~TableCell()
66 {
67 }
68 
69 void applyLineAttributes( const ::oox::core::XmlFilterBase& rFilterBase,
70         Reference< XPropertySet >& rxPropSet, oox::drawingml::LineProperties& rLineProperties,
71         sal_Int32 nPropId )
72 {
73     BorderLine aBorderLine( 0, 0, 0, 0 );
74     if( rLineProperties.maLineFill.moFillType.differsFrom( XML_noFill ) )
75     {
76         Color aColor = rLineProperties.maLineFill.getBestSolidColor();
77         aBorderLine.Color = aColor.getColor( rFilterBase.getGraphicHelper() );
78         aBorderLine.OuterLineWidth = static_cast< sal_Int16 >( GetCoordinate( rLineProperties.moLineWidth.get( 0 ) ) / 4 );
79         aBorderLine.InnerLineWidth = static_cast< sal_Int16 >( GetCoordinate( rLineProperties.moLineWidth.get( 0 ) ) / 4 );
80         aBorderLine.LineDistance = 0;
81     }
82 
83     PropertySet aPropSet( rxPropSet );
84     aPropSet.setProperty( nPropId, aBorderLine );
85 }
86 
87 void applyBorder( TableStylePart& rTableStylePart, sal_Int32 nLineType, oox::drawingml::LineProperties& rLineProperties )
88 {
89 	std::map < sal_Int32, ::oox::drawingml::LinePropertiesPtr >& rPartLineBorders( rTableStylePart.getLineBorders() );
90 	std::map < sal_Int32, ::oox::drawingml::LinePropertiesPtr >::const_iterator aIter( rPartLineBorders.find( nLineType ) );
91 	if ( ( aIter != rPartLineBorders.end() ) && aIter->second.get() )
92 		rLineProperties.assignUsed( *aIter->second );
93 }
94 
95 void applyTableStylePart( const ::oox::core::XmlFilterBase& rFilterBase, const Reference < ::com::sun::star::table::XCell >& rxCell, oox::drawingml::FillProperties& rFillProperties,
96 	 oox::drawingml::LineProperties& rLeftBorder,
97 	 oox::drawingml::LineProperties& rRightBorder,
98 	 oox::drawingml::LineProperties& rTopBorder,
99 	 oox::drawingml::LineProperties& rBottomBorder,
100 	 oox::drawingml::LineProperties& rTopLeftToBottomRightBorder,
101 	 oox::drawingml::LineProperties& rBottomLeftToTopRightBorder,
102 	TableStylePart& rTableStylePart )
103 {
104 	boost::shared_ptr< ::oox::drawingml::FillProperties >& rPartFillPropertiesPtr( rTableStylePart.getFillProperties() );
105 	if ( rPartFillPropertiesPtr.get() )
106 		rFillProperties.assignUsed( *rPartFillPropertiesPtr );
107 
108 	applyBorder( rTableStylePart, XML_left, rLeftBorder );
109 	applyBorder( rTableStylePart, XML_right, rRightBorder );
110 	applyBorder( rTableStylePart, XML_top, rTopBorder );
111 	applyBorder( rTableStylePart, XML_bottom, rBottomBorder );
112 	applyBorder( rTableStylePart, XML_tl2br, rTopLeftToBottomRightBorder );
113 	applyBorder( rTableStylePart, XML_tr2bl, rBottomLeftToTopRightBorder );
114 
115     TextCharacterProperties aTextCharProps;
116     aTextCharProps.maLatinFont = rTableStylePart.getLatinFont();
117     aTextCharProps.maAsianFont = rTableStylePart.getAsianFont();
118     aTextCharProps.maComplexFont = rTableStylePart.getComplexFont();
119     aTextCharProps.maSymbolFont = rTableStylePart.getSymbolFont();
120     aTextCharProps.maCharColor = rTableStylePart.getTextColor();
121 
122     PropertySet aPropSet( rxCell );
123     aTextCharProps.pushToPropSet( aPropSet, rFilterBase );
124 }
125 
126 void applyTableCellProperties( const Reference < ::com::sun::star::table::XCell >& rxCell, const TableCell& rTableCell )
127 {
128 	static const rtl::OUString	sTopBorder( RTL_CONSTASCII_USTRINGPARAM( "TextUpperDistance" ) );
129 	static const rtl::OUString	sBottomBorder( RTL_CONSTASCII_USTRINGPARAM( "TextLowerDistance" ) );
130 	static const rtl::OUString	sLeftBorder( RTL_CONSTASCII_USTRINGPARAM( "TextLeftDistance" ) );
131 	static const rtl::OUString	sRightBorder( RTL_CONSTASCII_USTRINGPARAM( "TextRightDistance" ) );
132 	static const rtl::OUString	sVerticalAdjust( RTL_CONSTASCII_USTRINGPARAM( "TextVerticalAdjust" ) );
133 
134 	Reference< XPropertySet > xPropSet( rxCell, UNO_QUERY_THROW );
135 	xPropSet->setPropertyValue( sTopBorder, Any( static_cast< sal_Int32 >( rTableCell.getTopMargin() / 360 ) ) );
136 	xPropSet->setPropertyValue( sRightBorder, Any( static_cast< sal_Int32 >( rTableCell.getRightMargin() / 360 ) ) );
137 	xPropSet->setPropertyValue( sLeftBorder, Any( static_cast< sal_Int32 >( rTableCell.getLeftMargin() / 360 ) ) );
138 	xPropSet->setPropertyValue( sBottomBorder, Any( static_cast< sal_Int32 >( rTableCell.getBottomMargin() / 360 ) ) );
139 
140 	drawing::TextVerticalAdjust eVA;
141 	switch( rTableCell.getAnchorToken() )
142 	{
143 		case XML_ctr:	eVA = drawing::TextVerticalAdjust_CENTER; break;
144 		case XML_b:		eVA = drawing::TextVerticalAdjust_BOTTOM; break;
145 		case XML_just:
146 		case XML_dist:
147 		default:
148 		case XML_t:		eVA = drawing::TextVerticalAdjust_TOP; break;
149 	}
150 	xPropSet->setPropertyValue( sVerticalAdjust, Any( eVA ) );
151 }
152 
153 void TableCell::pushToXCell( const ::oox::core::XmlFilterBase& rFilterBase, ::oox::drawingml::TextListStylePtr pMasterTextListStyle,
154 	const ::com::sun::star::uno::Reference < ::com::sun::star::table::XCell >& rxCell, const TableProperties& rTableProperties,
155 		const TableStyle& rTableStyle, sal_Int32 nColumn, sal_Int32 nMaxColumn, sal_Int32 nRow, sal_Int32 nMaxRow )
156 {
157 	TableStyle& rTable( const_cast< TableStyle& >( rTableStyle ) );
158 	TableProperties& rProperties( const_cast< TableProperties& >( rTableProperties ) );
159 
160 	Reference< text::XText > xText( rxCell, UNO_QUERY_THROW );
161 	Reference< text::XTextCursor > xAt = xText->createTextCursor();
162 
163 	applyTableCellProperties( rxCell, *this );
164     TextCharacterProperties aTextStyleProps;
165 	getTextBody()->insertAt( rFilterBase, xText, xAt, aTextStyleProps, pMasterTextListStyle );
166 
167 	Reference< XPropertySet > xPropSet( rxCell, UNO_QUERY_THROW );
168 	oox::drawingml::FillProperties aFillProperties;
169 	oox::drawingml::LineProperties aLinePropertiesLeft;
170 	oox::drawingml::LineProperties aLinePropertiesRight;
171 	oox::drawingml::LineProperties aLinePropertiesTop;
172 	oox::drawingml::LineProperties aLinePropertiesBottom;
173 	oox::drawingml::LineProperties aLinePropertiesTopLeftToBottomRight;
174 	oox::drawingml::LineProperties aLinePropertiesBottomLeftToTopRight;
175 
176 	boost::shared_ptr< ::oox::drawingml::FillProperties >& rBackgroundFillPropertiesPtr( rTable.getBackgroundFillProperties() );
177 	if ( rBackgroundFillPropertiesPtr.get() )
178 		aFillProperties.assignUsed( *rBackgroundFillPropertiesPtr );
179 
180 	applyTableStylePart( rFilterBase, rxCell, aFillProperties,
181 		aLinePropertiesLeft,
182 		aLinePropertiesRight,
183 		aLinePropertiesTop,
184 		aLinePropertiesBottom,
185 		aLinePropertiesTopLeftToBottomRight,
186 		aLinePropertiesBottomLeftToTopRight,
187 		rTable.getWholeTbl() );
188 
189 	if ( rProperties.isFirstRow() && ( nRow == 0 ) )
190 	{
191 		applyTableStylePart( rFilterBase, rxCell, aFillProperties,
192 			aLinePropertiesLeft,
193 			aLinePropertiesRight,
194 			aLinePropertiesTop,
195 			aLinePropertiesBottom,
196 			aLinePropertiesTopLeftToBottomRight,
197 			aLinePropertiesBottomLeftToTopRight,
198 			rTable.getFirstRow() );
199 	}
200 	if ( rProperties.isLastRow() && ( nRow == nMaxRow ) )
201 	{
202 		applyTableStylePart( rFilterBase, rxCell, aFillProperties,
203 			aLinePropertiesLeft,
204 			aLinePropertiesRight,
205 			aLinePropertiesTop,
206 			aLinePropertiesBottom,
207 			aLinePropertiesTopLeftToBottomRight,
208 			aLinePropertiesBottomLeftToTopRight,
209 			rTable.getLastRow() );
210 	}
211 	if ( rProperties.isFirstCol() && ( nColumn == 0 ) )
212 	{
213 		applyTableStylePart( rFilterBase, rxCell, aFillProperties,
214 			aLinePropertiesLeft,
215 			aLinePropertiesRight,
216 			aLinePropertiesTop,
217 			aLinePropertiesBottom,
218 			aLinePropertiesTopLeftToBottomRight,
219 			aLinePropertiesBottomLeftToTopRight,
220 			rTable.getFirstCol() );
221 	}
222 	if ( rProperties.isLastCol() && ( nColumn == nMaxColumn ) )
223 	{
224 		applyTableStylePart( rFilterBase, rxCell, aFillProperties,
225 			aLinePropertiesLeft,
226 			aLinePropertiesRight,
227 			aLinePropertiesTop,
228 			aLinePropertiesBottom,
229 			aLinePropertiesTopLeftToBottomRight,
230 			aLinePropertiesBottomLeftToTopRight,
231 			rTable.getLastCol() );
232 	}
233 	if ( rProperties.isBandRow() )
234 	{
235 		if ( ( !rProperties.isFirstRow() || ( nRow != 0 ) ) &&
236 			( !rProperties.isLastRow() || ( nRow != nMaxRow ) ) )
237 		{
238 			sal_Int32 nBand = nRow;
239 			if ( rProperties.isFirstRow() )
240 				nBand++;
241 			if ( nBand & 1 )
242 			{
243 				applyTableStylePart( rFilterBase, rxCell, aFillProperties,
244 					aLinePropertiesLeft,
245 					aLinePropertiesRight,
246 					aLinePropertiesTop,
247 					aLinePropertiesBottom,
248 					aLinePropertiesTopLeftToBottomRight,
249 					aLinePropertiesBottomLeftToTopRight,
250 					rTable.getBand2H() );
251 			}
252 			else
253 			{
254 				applyTableStylePart( rFilterBase, rxCell, aFillProperties,
255 					aLinePropertiesLeft,
256 					aLinePropertiesRight,
257 					aLinePropertiesTop,
258 					aLinePropertiesBottom,
259 					aLinePropertiesTopLeftToBottomRight,
260 					aLinePropertiesBottomLeftToTopRight,
261 					rTable.getBand1H() );
262 			}
263 		}
264 	}
265 	if ( ( nRow == 0 ) && ( nColumn == 0 ) )
266 	{
267 		applyTableStylePart( rFilterBase, rxCell, aFillProperties,
268 			aLinePropertiesLeft,
269 			aLinePropertiesRight,
270 			aLinePropertiesTop,
271 			aLinePropertiesBottom,
272 			aLinePropertiesTopLeftToBottomRight,
273 			aLinePropertiesBottomLeftToTopRight,
274 			rTable.getNwCell() );
275 	}
276 	if ( ( nRow == nMaxRow ) && ( nColumn == 0 ) )
277 	{
278 		applyTableStylePart( rFilterBase, rxCell, aFillProperties,
279 			aLinePropertiesLeft,
280 			aLinePropertiesRight,
281 			aLinePropertiesTop,
282 			aLinePropertiesBottom,
283 			aLinePropertiesTopLeftToBottomRight,
284 			aLinePropertiesBottomLeftToTopRight,
285 			rTable.getSwCell() );
286 	}
287 	if ( ( nRow == 0 ) && ( nColumn == nMaxColumn ) )
288 	{
289 		applyTableStylePart( rFilterBase, rxCell, aFillProperties,
290 			aLinePropertiesLeft,
291 			aLinePropertiesRight,
292 			aLinePropertiesTop,
293 			aLinePropertiesBottom,
294 			aLinePropertiesTopLeftToBottomRight,
295 			aLinePropertiesBottomLeftToTopRight,
296 			rTable.getNeCell() );
297 	}
298 	if ( ( nRow == nMaxColumn ) && ( nColumn == nMaxColumn ) )
299 	{
300 		applyTableStylePart( rFilterBase, rxCell, aFillProperties,
301 			aLinePropertiesLeft,
302 			aLinePropertiesRight,
303 			aLinePropertiesTop,
304 			aLinePropertiesBottom,
305 			aLinePropertiesTopLeftToBottomRight,
306 			aLinePropertiesBottomLeftToTopRight,
307 			rTable.getSeCell() );
308 	}
309 	if ( rProperties.isBandCol() )
310 	{
311 		if ( ( !rProperties.isFirstCol() || ( nColumn != 0 ) ) &&
312 			( !rProperties.isLastCol() || ( nColumn != nMaxColumn ) ) )
313 		{
314 			sal_Int32 nBand = nColumn;
315 			if ( rProperties.isFirstCol() )
316 				nBand++;
317 			if ( nBand & 1 )
318 			{
319 				applyTableStylePart( rFilterBase, rxCell, aFillProperties,
320 					aLinePropertiesLeft,
321 					aLinePropertiesRight,
322 					aLinePropertiesTop,
323 					aLinePropertiesBottom,
324 					aLinePropertiesTopLeftToBottomRight,
325 					aLinePropertiesBottomLeftToTopRight,
326 					rTable.getBand2V() );
327 			}
328 			else
329 			{
330 				applyTableStylePart( rFilterBase, rxCell, aFillProperties,
331 					aLinePropertiesLeft,
332 					aLinePropertiesRight,
333 					aLinePropertiesTop,
334 					aLinePropertiesBottom,
335 					aLinePropertiesTopLeftToBottomRight,
336 					aLinePropertiesBottomLeftToTopRight,
337 					rTable.getBand1V() );
338 			}
339 		}
340 	}
341 	aLinePropertiesLeft.assignUsed( maLinePropertiesLeft );
342     aLinePropertiesRight.assignUsed( maLinePropertiesRight );
343     aLinePropertiesTop.assignUsed( maLinePropertiesTop );
344     aLinePropertiesBottom.assignUsed( maLinePropertiesBottom );
345     aLinePropertiesTopLeftToBottomRight.assignUsed( maLinePropertiesTopLeftToBottomRight );
346     aLinePropertiesBottomLeftToTopRight.assignUsed( maLinePropertiesBottomLeftToTopRight );
347     applyLineAttributes( rFilterBase, xPropSet, aLinePropertiesLeft, PROP_LeftBorder );
348     applyLineAttributes( rFilterBase, xPropSet, aLinePropertiesRight, PROP_RightBorder );
349     applyLineAttributes( rFilterBase, xPropSet, aLinePropertiesTop, PROP_TopBorder );
350     applyLineAttributes( rFilterBase, xPropSet, aLinePropertiesBottom, PROP_BottomBorder );
351     applyLineAttributes( rFilterBase, xPropSet, aLinePropertiesTopLeftToBottomRight, PROP_DiagonalTLBR );
352     applyLineAttributes( rFilterBase, xPropSet, aLinePropertiesBottomLeftToTopRight, PROP_DiagonalBLTR );
353 
354 	aFillProperties.assignUsed( maFillProperties );
355     ShapePropertyMap aPropMap( rFilterBase.getModelObjectHelper() );
356     // TODO: phClr?
357     aFillProperties.pushToPropMap( aPropMap, rFilterBase.getGraphicHelper() );
358     PropertySet( xPropSet ).setProperties( aPropMap );
359 }
360 
361 } } }
362