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