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: tablecell.cxx,v $
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 #include "oox/drawingml/table/tablecell.hxx"
32 #include "oox/drawingml/table/tableproperties.hxx"
33 #include "oox/drawingml/textbody.hxx"
34 #include "oox/core/namespaces.hxx"
35 #include "oox/core/xmlfilterbase.hxx"
36 #include "properties.hxx"
38 #include "oox/helper/propertyset.hxx"
39 #include <com/sun/star/container/XNameContainer.hpp>
40 #include <com/sun/star/beans/XMultiPropertySet.hpp>
41 #include <com/sun/star/table/XTable.hpp>
42 #include <com/sun/star/table/XMergeableCellRange.hpp>
43 #include <com/sun/star/table/BorderLine.hpp>
44 #include <com/sun/star/drawing/LineStyle.hpp>
45 #include <com/sun/star/drawing/TextVerticalAdjust.hpp>
46 #include <com/sun/star/drawing/TextHorizontalAdjust.hpp>
47 #include <com/sun/star/text/XText.hpp>
50 using namespace ::oox::core
;
51 using namespace ::com::sun::star
;
52 using namespace ::com::sun::star::uno
;
53 using namespace ::com::sun::star::beans
;
54 using ::com::sun::star::table::BorderLine
;
55 using ::com::sun::star::drawing::LineStyle
;
57 namespace oox
{ namespace drawingml
{ namespace table
{
59 TableCell::TableCell()
62 , mbhMerge( sal_False
)
63 , mbvMerge( sal_False
)
68 , mnVertToken( XML_horz
)
69 , mnAnchorToken( XML_t
)
70 , mbAnchorCtr( sal_False
)
71 , mnHorzOverflowToken( XML_clip
)
74 TableCell::~TableCell()
78 void applyLineAttributes( const ::oox::core::XmlFilterBase
& rFilterBase
,
79 Reference
< XPropertySet
>& rxPropSet
, oox::drawingml::LineProperties
& rLineProperties
,
82 BorderLine
aBorderLine( 0, 0, 0, 0 );
83 if( rLineProperties
.maLineFill
.moFillType
.differsFrom( XML_noFill
) )
85 Color aColor
= rLineProperties
.maLineFill
.getBestSolidColor();
86 aBorderLine
.Color
= aColor
.getColor( rFilterBase
);
87 aBorderLine
.OuterLineWidth
= static_cast< sal_Int16
>( GetCoordinate( rLineProperties
.moLineWidth
.get( 0 ) ) );
88 aBorderLine
.InnerLineWidth
= 0;
89 aBorderLine
.LineDistance
= 0;
92 PropertySet
aPropSet( rxPropSet
);
93 aPropSet
.setProperty( nPropId
, aBorderLine
);
96 void applyBorder( TableStylePart
& rTableStylePart
, sal_Int32 nLineType
, oox::drawingml::LineProperties
& rLineProperties
)
98 std::map
< sal_Int32
, ::oox::drawingml::LinePropertiesPtr
>& rPartLineBorders( rTableStylePart
.getLineBorders() );
99 std::map
< sal_Int32
, ::oox::drawingml::LinePropertiesPtr
>::const_iterator
aIter( rPartLineBorders
.find( nLineType
) );
100 if ( ( aIter
!= rPartLineBorders
.end() ) && aIter
->second
.get() )
101 rLineProperties
.assignUsed( *aIter
->second
);
104 void applyTableStylePart( const ::oox::core::XmlFilterBase
& rFilterBase
, const Reference
< ::com::sun::star::table::XCell
>& rxCell
, oox::drawingml::FillProperties
& rFillProperties
,
105 oox::drawingml::LineProperties
& rLeftBorder
,
106 oox::drawingml::LineProperties
& rRightBorder
,
107 oox::drawingml::LineProperties
& rTopBorder
,
108 oox::drawingml::LineProperties
& rBottomBorder
,
109 oox::drawingml::LineProperties
& rTopLeftToBottomRightBorder
,
110 oox::drawingml::LineProperties
& rBottomLeftToTopRightBorder
,
111 TableStylePart
& rTableStylePart
)
113 boost::shared_ptr
< ::oox::drawingml::FillProperties
>& rPartFillPropertiesPtr( rTableStylePart
.getFillProperties() );
114 if ( rPartFillPropertiesPtr
.get() )
115 rFillProperties
.assignUsed( *rPartFillPropertiesPtr
);
117 applyBorder( rTableStylePart
, XML_left
, rLeftBorder
);
118 applyBorder( rTableStylePart
, XML_right
, rRightBorder
);
119 applyBorder( rTableStylePart
, XML_top
, rTopBorder
);
120 applyBorder( rTableStylePart
, XML_bottom
, rBottomBorder
);
121 applyBorder( rTableStylePart
, XML_tl2br
, rTopLeftToBottomRightBorder
);
122 applyBorder( rTableStylePart
, XML_tr2bl
, rBottomLeftToTopRightBorder
);
124 TextCharacterProperties aTextCharProps
;
125 aTextCharProps
.maLatinFont
= rTableStylePart
.getLatinFont();
126 aTextCharProps
.maAsianFont
= rTableStylePart
.getAsianFont();
127 aTextCharProps
.maComplexFont
= rTableStylePart
.getComplexFont();
128 aTextCharProps
.maSymbolFont
= rTableStylePart
.getSymbolFont();
129 aTextCharProps
.maCharColor
= rTableStylePart
.getTextColor();
131 PropertySet
aPropSet( rxCell
);
132 aTextCharProps
.pushToPropSet( aPropSet
, rFilterBase
);
135 void applyTableCellProperties( const Reference
< ::com::sun::star::table::XCell
>& rxCell
, const TableCell
& rTableCell
)
137 static const rtl::OUString
sTopBorder( RTL_CONSTASCII_USTRINGPARAM( "TextUpperDistance" ) );
138 static const rtl::OUString
sBottomBorder( RTL_CONSTASCII_USTRINGPARAM( "TextLowerDistance" ) );
139 static const rtl::OUString
sLeftBorder( RTL_CONSTASCII_USTRINGPARAM( "TextLeftDistance" ) );
140 static const rtl::OUString
sRightBorder( RTL_CONSTASCII_USTRINGPARAM( "TextRightDistance" ) );
141 static const rtl::OUString
sVerticalAdjust( RTL_CONSTASCII_USTRINGPARAM( "TextVerticalAdjust" ) );
143 Reference
< XPropertySet
> xPropSet( rxCell
, UNO_QUERY_THROW
);
144 xPropSet
->setPropertyValue( sTopBorder
, Any( static_cast< sal_Int32
>( rTableCell
.getTopMargin() / 360 ) ) );
145 xPropSet
->setPropertyValue( sRightBorder
, Any( static_cast< sal_Int32
>( rTableCell
.getRightMargin() / 360 ) ) );
146 xPropSet
->setPropertyValue( sLeftBorder
, Any( static_cast< sal_Int32
>( rTableCell
.getLeftMargin() / 360 ) ) );
147 xPropSet
->setPropertyValue( sBottomBorder
, Any( static_cast< sal_Int32
>( rTableCell
.getBottomMargin() / 360 ) ) );
149 drawing::TextVerticalAdjust eVA
;
150 switch( rTableCell
.getAnchorToken() )
152 case XML_ctr
: eVA
= drawing::TextVerticalAdjust_CENTER
; break;
153 case XML_b
: eVA
= drawing::TextVerticalAdjust_BOTTOM
; break;
157 case XML_t
: eVA
= drawing::TextVerticalAdjust_TOP
; break;
159 xPropSet
->setPropertyValue( sVerticalAdjust
, Any( eVA
) );
162 void TableCell::pushToXCell( const ::oox::core::XmlFilterBase
& rFilterBase
, ::oox::drawingml::TextListStylePtr pMasterTextListStyle
,
163 const ::com::sun::star::uno::Reference
< ::com::sun::star::table::XCell
>& rxCell
, const TableProperties
& rTableProperties
,
164 const TableStyle
& rTableStyle
, sal_Int32 nColumn
, sal_Int32 nMaxColumn
, sal_Int32 nRow
, sal_Int32 nMaxRow
)
166 TableStyle
& rTable( const_cast< TableStyle
& >( rTableStyle
) );
167 TableProperties
& rProperties( const_cast< TableProperties
& >( rTableProperties
) );
169 Reference
< text::XText
> xText( rxCell
, UNO_QUERY_THROW
);
170 Reference
< text::XTextCursor
> xAt
= xText
->createTextCursor();
172 applyTableCellProperties( rxCell
, *this );
173 TextCharacterProperties aTextStyleProps
;
174 getTextBody()->insertAt( rFilterBase
, xText
, xAt
, aTextStyleProps
, pMasterTextListStyle
);
176 Reference
< XPropertySet
> xPropSet( rxCell
, UNO_QUERY_THROW
);
177 oox::drawingml::FillProperties aFillProperties
;
178 oox::drawingml::LineProperties aLinePropertiesLeft
;
179 oox::drawingml::LineProperties aLinePropertiesRight
;
180 oox::drawingml::LineProperties aLinePropertiesTop
;
181 oox::drawingml::LineProperties aLinePropertiesBottom
;
182 oox::drawingml::LineProperties aLinePropertiesTopLeftToBottomRight
;
183 oox::drawingml::LineProperties aLinePropertiesBottomLeftToTopRight
;
185 boost::shared_ptr
< ::oox::drawingml::FillProperties
>& rBackgroundFillPropertiesPtr( rTable
.getBackgroundFillProperties() );
186 if ( rBackgroundFillPropertiesPtr
.get() )
187 aFillProperties
.assignUsed( *rBackgroundFillPropertiesPtr
);
189 applyTableStylePart( rFilterBase
, rxCell
, aFillProperties
,
191 aLinePropertiesRight
,
193 aLinePropertiesBottom
,
194 aLinePropertiesTopLeftToBottomRight
,
195 aLinePropertiesBottomLeftToTopRight
,
196 rTable
.getWholeTbl() );
198 if ( rProperties
.isFirstRow() && ( nRow
== 0 ) )
200 applyTableStylePart( rFilterBase
, rxCell
, aFillProperties
,
202 aLinePropertiesRight
,
204 aLinePropertiesBottom
,
205 aLinePropertiesTopLeftToBottomRight
,
206 aLinePropertiesBottomLeftToTopRight
,
207 rTable
.getFirstRow() );
209 if ( rProperties
.isLastRow() && ( nRow
== nMaxRow
) )
211 applyTableStylePart( rFilterBase
, rxCell
, aFillProperties
,
213 aLinePropertiesRight
,
215 aLinePropertiesBottom
,
216 aLinePropertiesTopLeftToBottomRight
,
217 aLinePropertiesBottomLeftToTopRight
,
218 rTable
.getLastRow() );
220 if ( rProperties
.isFirstCol() && ( nColumn
== 0 ) )
222 applyTableStylePart( rFilterBase
, rxCell
, aFillProperties
,
224 aLinePropertiesRight
,
226 aLinePropertiesBottom
,
227 aLinePropertiesTopLeftToBottomRight
,
228 aLinePropertiesBottomLeftToTopRight
,
229 rTable
.getFirstCol() );
231 if ( rProperties
.isLastCol() && ( nColumn
== nMaxColumn
) )
233 applyTableStylePart( rFilterBase
, rxCell
, aFillProperties
,
235 aLinePropertiesRight
,
237 aLinePropertiesBottom
,
238 aLinePropertiesTopLeftToBottomRight
,
239 aLinePropertiesBottomLeftToTopRight
,
240 rTable
.getLastCol() );
242 if ( rProperties
.isBandRow() )
244 if ( ( !rProperties
.isFirstRow() || ( nRow
!= 0 ) ) &&
245 ( !rProperties
.isLastRow() || ( nRow
!= nMaxRow
) ) )
247 sal_Int32 nBand
= nRow
;
248 if ( rProperties
.isFirstRow() )
252 applyTableStylePart( rFilterBase
, rxCell
, aFillProperties
,
254 aLinePropertiesRight
,
256 aLinePropertiesBottom
,
257 aLinePropertiesTopLeftToBottomRight
,
258 aLinePropertiesBottomLeftToTopRight
,
259 rTable
.getBand2H() );
263 applyTableStylePart( rFilterBase
, rxCell
, aFillProperties
,
265 aLinePropertiesRight
,
267 aLinePropertiesBottom
,
268 aLinePropertiesTopLeftToBottomRight
,
269 aLinePropertiesBottomLeftToTopRight
,
270 rTable
.getBand1H() );
274 if ( ( nRow
== 0 ) && ( nColumn
== 0 ) )
276 applyTableStylePart( rFilterBase
, rxCell
, aFillProperties
,
278 aLinePropertiesRight
,
280 aLinePropertiesBottom
,
281 aLinePropertiesTopLeftToBottomRight
,
282 aLinePropertiesBottomLeftToTopRight
,
283 rTable
.getNwCell() );
285 if ( ( nRow
== nMaxRow
) && ( nColumn
== 0 ) )
287 applyTableStylePart( rFilterBase
, rxCell
, aFillProperties
,
289 aLinePropertiesRight
,
291 aLinePropertiesBottom
,
292 aLinePropertiesTopLeftToBottomRight
,
293 aLinePropertiesBottomLeftToTopRight
,
294 rTable
.getSwCell() );
296 if ( ( nRow
== 0 ) && ( nColumn
== nMaxColumn
) )
298 applyTableStylePart( rFilterBase
, rxCell
, aFillProperties
,
300 aLinePropertiesRight
,
302 aLinePropertiesBottom
,
303 aLinePropertiesTopLeftToBottomRight
,
304 aLinePropertiesBottomLeftToTopRight
,
305 rTable
.getNeCell() );
307 if ( ( nRow
== nMaxColumn
) && ( nColumn
== nMaxColumn
) )
309 applyTableStylePart( rFilterBase
, rxCell
, aFillProperties
,
311 aLinePropertiesRight
,
313 aLinePropertiesBottom
,
314 aLinePropertiesTopLeftToBottomRight
,
315 aLinePropertiesBottomLeftToTopRight
,
316 rTable
.getSeCell() );
318 if ( rProperties
.isBandCol() )
320 if ( ( !rProperties
.isFirstCol() || ( nColumn
!= 0 ) ) &&
321 ( !rProperties
.isLastCol() || ( nColumn
!= nMaxColumn
) ) )
323 sal_Int32 nBand
= nColumn
;
324 if ( rProperties
.isFirstCol() )
328 applyTableStylePart( rFilterBase
, rxCell
, aFillProperties
,
330 aLinePropertiesRight
,
332 aLinePropertiesBottom
,
333 aLinePropertiesTopLeftToBottomRight
,
334 aLinePropertiesBottomLeftToTopRight
,
335 rTable
.getBand2V() );
339 applyTableStylePart( rFilterBase
, rxCell
, aFillProperties
,
341 aLinePropertiesRight
,
343 aLinePropertiesBottom
,
344 aLinePropertiesTopLeftToBottomRight
,
345 aLinePropertiesBottomLeftToTopRight
,
346 rTable
.getBand1V() );
350 aLinePropertiesLeft
.assignUsed( maLinePropertiesLeft
);
351 aLinePropertiesRight
.assignUsed( maLinePropertiesRight
);
352 aLinePropertiesTop
.assignUsed( maLinePropertiesTop
);
353 aLinePropertiesBottom
.assignUsed( maLinePropertiesBottom
);
354 aLinePropertiesTopLeftToBottomRight
.assignUsed( maLinePropertiesTopLeftToBottomRight
);
355 aLinePropertiesBottomLeftToTopRight
.assignUsed( maLinePropertiesBottomLeftToTopRight
);
356 applyLineAttributes( rFilterBase
, xPropSet
, aLinePropertiesLeft
, PROP_LeftBorder
);
357 applyLineAttributes( rFilterBase
, xPropSet
, aLinePropertiesRight
, PROP_RightBorder
);
358 applyLineAttributes( rFilterBase
, xPropSet
, aLinePropertiesTop
, PROP_TopBorder
);
359 applyLineAttributes( rFilterBase
, xPropSet
, aLinePropertiesBottom
, PROP_BottomBorder
);
360 applyLineAttributes( rFilterBase
, xPropSet
, aLinePropertiesTopLeftToBottomRight
, PROP_DiagonalTLBR
);
361 applyLineAttributes( rFilterBase
, xPropSet
, aLinePropertiesBottomLeftToTopRight
, PROP_DiagonalBLTR
);
363 aFillProperties
.assignUsed( maFillProperties
);
364 PropertySet
aPropSet( xPropSet
);
366 aFillProperties
.pushToPropSet( aPropSet
, rFilterBase
, rFilterBase
.getModelObjectHelper() );