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: tableproperties.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/tableproperties.hxx"
32 #include "oox/drawingml/drawingmltypes.hxx"
33 #include <com/sun/star/table/XTable.hpp>
34 #include <com/sun/star/container/XNameContainer.hpp>
35 #include <com/sun/star/beans/XMultiPropertySet.hpp>
36 #include <com/sun/star/lang/XMultiServiceFactory.hpp>
37 #include <com/sun/star/table/XMergeableCellRange.hpp>
38 #include <com/sun/star/table/BorderLine.hpp>
39 #include "oox/core/namespaces.hxx"
40 #include "oox/core/xmlfilterbase.hxx"
41 #include "oox/helper/propertyset.hxx"
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 namespace ::com::sun::star::table
;
52 namespace oox
{ namespace drawingml
{ namespace table
{
54 TableProperties::TableProperties()
56 , mbFirstRow( sal_False
)
57 , mbFirstCol( sal_False
)
58 , mbLastRow( sal_False
)
59 , mbLastCol( sal_False
)
60 , mbBandRow( sal_False
)
61 , mbBandCol( sal_False
)
64 TableProperties::~TableProperties()
68 void TableProperties::apply( const TablePropertiesPtr
& /* rSourceTableProperties */ )
72 void CreateTableRows( uno::Reference
< XTableRows
> xTableRows
, const std::vector
< TableRow
>& rvTableRows
)
74 if ( rvTableRows
.size() > 1 )
75 xTableRows
->insertByIndex( 0, rvTableRows
.size() - 1 );
76 std::vector
< TableRow
>::const_iterator
aTableRowIter( rvTableRows
.begin() );
77 uno::Reference
< container::XIndexAccess
> xIndexAccess( xTableRows
, UNO_QUERY_THROW
);
78 for ( sal_Int32 n
= 0; n
< xIndexAccess
->getCount(); n
++ )
80 static const rtl::OUString
sHeight( RTL_CONSTASCII_USTRINGPARAM ( "Height" ) );
81 Reference
< XPropertySet
> xPropSet( xIndexAccess
->getByIndex( n
), UNO_QUERY_THROW
);
82 xPropSet
->setPropertyValue( sHeight
, Any( static_cast< sal_Int32
>( aTableRowIter
->getHeight() / 360 ) ) );
87 void CreateTableColumns( Reference
< XTableColumns
> xTableColumns
, const std::vector
< sal_Int32
>& rvTableGrid
)
89 if ( rvTableGrid
.size() > 1 )
90 xTableColumns
->insertByIndex( 0, rvTableGrid
.size() - 1 );
91 std::vector
< sal_Int32
>::const_iterator
aTableGridIter( rvTableGrid
.begin() );
92 uno::Reference
< container::XIndexAccess
> xIndexAccess( xTableColumns
, UNO_QUERY_THROW
);
93 for ( sal_Int32 n
= 0; n
< xIndexAccess
->getCount(); n
++ )
95 static const rtl::OUString
sWidth( RTL_CONSTASCII_USTRINGPARAM ( "Width" ) );
96 Reference
< XPropertySet
> xPropSet( xIndexAccess
->getByIndex( n
), UNO_QUERY_THROW
);
97 xPropSet
->setPropertyValue( sWidth
, Any( static_cast< sal_Int32
>( *aTableGridIter
++ / 360 ) ) );
101 void MergeCells( const uno::Reference
< XTable
>& xTable
, sal_Int32 nCol
, sal_Int32 nRow
, sal_Int32 nColSpan
, sal_Int32 nRowSpan
)
103 if( xTable
.is() ) try
105 Reference
< XMergeableCellRange
> xRange( xTable
->createCursorByRange( xTable
->getCellRangeByPosition( nCol
, nRow
,nCol
+ nColSpan
- 1, nRow
+ nRowSpan
- 1 ) ), UNO_QUERY_THROW
);
106 if( xRange
->isMergeable() )
114 static TableStyle
* pDefaultTableStyle
= new TableStyle();
116 const TableStyle
& TableProperties::getUsedTableStyle( const ::oox::core::XmlFilterBase
& rFilterBase
)
118 ::oox::core::XmlFilterBase
& rBase( const_cast< ::oox::core::XmlFilterBase
& >( rFilterBase
) );
120 TableStyle
* pTableStyle
= NULL
;
122 pTableStyle
= &*mpTableStyle
;
123 else if ( rBase
.getTableStyles() )
125 const std::vector
< TableStyle
>& rTableStyles( rBase
.getTableStyles()->getTableStyles() );
126 const rtl::OUString
aStyleId( getStyleId().getLength() ? getStyleId() : rBase
.getTableStyles()->getDefaultStyleId() );
127 std::vector
< TableStyle
>::const_iterator
aIter( rTableStyles
.begin() );
128 while( aIter
!= rTableStyles
.end() )
130 if ( const_cast< TableStyle
& >( *aIter
).getStyleId() == aStyleId
)
132 pTableStyle
= &const_cast< TableStyle
& >( *aIter
);
133 break; // we get the correct style
139 pTableStyle
= pDefaultTableStyle
;
144 void TableProperties::pushToPropSet( const ::oox::core::XmlFilterBase
& rFilterBase
,
145 const Reference
< XPropertySet
>& xPropSet
, TextListStylePtr pMasterTextListStyle
)
147 TableStyleListPtr( const_cast< ::oox::core::XmlFilterBase
& >( rFilterBase
).getTableStyles() );
149 uno::Reference
< XColumnRowRange
> xColumnRowRange(
150 xPropSet
->getPropertyValue( OUString(RTL_CONSTASCII_USTRINGPARAM("Model") ) ), uno::UNO_QUERY_THROW
);
152 CreateTableColumns( xColumnRowRange
->getColumns(), mvTableGrid
);
153 CreateTableRows( xColumnRowRange
->getRows(), mvTableRows
);
155 const TableStyle
& rTableStyle( getUsedTableStyle( rFilterBase
) );
157 std::vector
< TableRow
>::iterator
aTableRowIter( mvTableRows
.begin() );
158 while( aTableRowIter
!= mvTableRows
.end() )
160 sal_Int32 nColumn
= 0;
161 std::vector
< TableCell
>::iterator
aTableCellIter( aTableRowIter
->getTableCells().begin() );
162 while( aTableCellIter
!= aTableRowIter
->getTableCells().end() )
164 TableCell
& rTableCell( *aTableCellIter
);
165 if ( !rTableCell
.getvMerge() && !rTableCell
.gethMerge() )
167 uno::Reference
< XTable
> xTable( xColumnRowRange
, uno::UNO_QUERY_THROW
);
168 if ( ( rTableCell
.getRowSpan() > 1 ) || ( rTableCell
.getGridSpan() > 1 ) )
169 MergeCells( xTable
, nColumn
, nRow
, rTableCell
.getGridSpan(), rTableCell
.getRowSpan() );
171 Reference
< XCellRange
> xCellRange( xTable
, UNO_QUERY_THROW
);
172 rTableCell
.pushToXCell( rFilterBase
, pMasterTextListStyle
, xCellRange
->getCellByPosition( nColumn
, nRow
), *this, rTableStyle
,
173 nColumn
, aTableRowIter
->getTableCells().size(), nRow
, mvTableRows
.size() );