1*cdf0e10cSrcweir /*************************************************************************
2*cdf0e10cSrcweir  *
3*cdf0e10cSrcweir  * DO NOT ALTER OR REMOVE COPYRIGHT NOTICES OR THIS FILE HEADER.
4*cdf0e10cSrcweir  *
5*cdf0e10cSrcweir  * Copyright 2000, 2010 Oracle and/or its affiliates.
6*cdf0e10cSrcweir  *
7*cdf0e10cSrcweir  * OpenOffice.org - a multi-platform office productivity suite
8*cdf0e10cSrcweir  *
9*cdf0e10cSrcweir  * This file is part of OpenOffice.org.
10*cdf0e10cSrcweir  *
11*cdf0e10cSrcweir  * OpenOffice.org is free software: you can redistribute it and/or modify
12*cdf0e10cSrcweir  * it under the terms of the GNU Lesser General Public License version 3
13*cdf0e10cSrcweir  * only, as published by the Free Software Foundation.
14*cdf0e10cSrcweir  *
15*cdf0e10cSrcweir  * OpenOffice.org is distributed in the hope that it will be useful,
16*cdf0e10cSrcweir  * but WITHOUT ANY WARRANTY; without even the implied warranty of
17*cdf0e10cSrcweir  * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the
18*cdf0e10cSrcweir  * GNU Lesser General Public License version 3 for more details
19*cdf0e10cSrcweir  * (a copy is included in the LICENSE file that accompanied this code).
20*cdf0e10cSrcweir  *
21*cdf0e10cSrcweir  * You should have received a copy of the GNU Lesser General Public License
22*cdf0e10cSrcweir  * version 3 along with OpenOffice.org.  If not, see
23*cdf0e10cSrcweir  * <http://www.openoffice.org/license.html>
24*cdf0e10cSrcweir  * for a copy of the LGPLv3 License.
25*cdf0e10cSrcweir  *
26*cdf0e10cSrcweir  ************************************************************************/
27*cdf0e10cSrcweir 
28*cdf0e10cSrcweir #include "oox/drawingml/table/tableproperties.hxx"
29*cdf0e10cSrcweir #include "oox/drawingml/drawingmltypes.hxx"
30*cdf0e10cSrcweir #include <com/sun/star/table/XTable.hpp>
31*cdf0e10cSrcweir #include <com/sun/star/container/XNameContainer.hpp>
32*cdf0e10cSrcweir #include <com/sun/star/beans/XMultiPropertySet.hpp>
33*cdf0e10cSrcweir #include <com/sun/star/lang/XMultiServiceFactory.hpp>
34*cdf0e10cSrcweir #include <com/sun/star/table/XMergeableCellRange.hpp>
35*cdf0e10cSrcweir #include <com/sun/star/table/BorderLine.hpp>
36*cdf0e10cSrcweir #include "oox/core/xmlfilterbase.hxx"
37*cdf0e10cSrcweir #include "oox/helper/propertyset.hxx"
38*cdf0e10cSrcweir 
39*cdf0e10cSrcweir using rtl::OUString;
40*cdf0e10cSrcweir using namespace ::oox::core;
41*cdf0e10cSrcweir using namespace ::com::sun::star;
42*cdf0e10cSrcweir using namespace ::com::sun::star::uno;
43*cdf0e10cSrcweir using namespace ::com::sun::star::beans;
44*cdf0e10cSrcweir using namespace ::com::sun::star::table;
45*cdf0e10cSrcweir 
46*cdf0e10cSrcweir 
47*cdf0e10cSrcweir namespace oox { namespace drawingml { namespace table {
48*cdf0e10cSrcweir 
49*cdf0e10cSrcweir TableProperties::TableProperties()
50*cdf0e10cSrcweir : mbRtl( sal_False )
51*cdf0e10cSrcweir , mbFirstRow( sal_False )
52*cdf0e10cSrcweir , mbFirstCol( sal_False )
53*cdf0e10cSrcweir , mbLastRow( sal_False )
54*cdf0e10cSrcweir , mbLastCol( sal_False )
55*cdf0e10cSrcweir , mbBandRow( sal_False )
56*cdf0e10cSrcweir , mbBandCol( sal_False )
57*cdf0e10cSrcweir {
58*cdf0e10cSrcweir }
59*cdf0e10cSrcweir TableProperties::~TableProperties()
60*cdf0e10cSrcweir {
61*cdf0e10cSrcweir }
62*cdf0e10cSrcweir 
63*cdf0e10cSrcweir void TableProperties::apply( const TablePropertiesPtr& /* rSourceTableProperties */ )
64*cdf0e10cSrcweir {
65*cdf0e10cSrcweir }
66*cdf0e10cSrcweir 
67*cdf0e10cSrcweir void CreateTableRows( uno::Reference< XTableRows > xTableRows, const std::vector< TableRow >& rvTableRows )
68*cdf0e10cSrcweir {
69*cdf0e10cSrcweir 	if ( rvTableRows.size() > 1 )
70*cdf0e10cSrcweir 		xTableRows->insertByIndex( 0, rvTableRows.size() - 1 );
71*cdf0e10cSrcweir 	std::vector< TableRow >::const_iterator aTableRowIter( rvTableRows.begin() );
72*cdf0e10cSrcweir 	uno::Reference< container::XIndexAccess > xIndexAccess( xTableRows, UNO_QUERY_THROW );
73*cdf0e10cSrcweir 	for ( sal_Int32 n = 0; n < xIndexAccess->getCount(); n++ )
74*cdf0e10cSrcweir 	{
75*cdf0e10cSrcweir 		static const rtl::OUString	sHeight( RTL_CONSTASCII_USTRINGPARAM ( "Height" ) );
76*cdf0e10cSrcweir 		Reference< XPropertySet > xPropSet( xIndexAccess->getByIndex( n ), UNO_QUERY_THROW );
77*cdf0e10cSrcweir 		xPropSet->setPropertyValue( sHeight, Any( static_cast< sal_Int32 >( aTableRowIter->getHeight() / 360 ) ) );
78*cdf0e10cSrcweir 		aTableRowIter++;
79*cdf0e10cSrcweir 	}
80*cdf0e10cSrcweir }
81*cdf0e10cSrcweir 
82*cdf0e10cSrcweir void CreateTableColumns( Reference< XTableColumns > xTableColumns, const std::vector< sal_Int32 >& rvTableGrid )
83*cdf0e10cSrcweir {
84*cdf0e10cSrcweir 	if ( rvTableGrid.size() > 1 )
85*cdf0e10cSrcweir 		xTableColumns->insertByIndex( 0, rvTableGrid.size() - 1 );
86*cdf0e10cSrcweir 	std::vector< sal_Int32 >::const_iterator aTableGridIter( rvTableGrid.begin() );
87*cdf0e10cSrcweir 	uno::Reference< container::XIndexAccess > xIndexAccess( xTableColumns, UNO_QUERY_THROW );
88*cdf0e10cSrcweir 	for ( sal_Int32 n = 0; n < xIndexAccess->getCount(); n++ )
89*cdf0e10cSrcweir 	{
90*cdf0e10cSrcweir 		static const rtl::OUString	sWidth( RTL_CONSTASCII_USTRINGPARAM ( "Width" ) );
91*cdf0e10cSrcweir 		Reference< XPropertySet > xPropSet( xIndexAccess->getByIndex( n ), UNO_QUERY_THROW );
92*cdf0e10cSrcweir 		xPropSet->setPropertyValue( sWidth, Any( static_cast< sal_Int32 >( *aTableGridIter++ / 360 ) ) );
93*cdf0e10cSrcweir 	}
94*cdf0e10cSrcweir }
95*cdf0e10cSrcweir 
96*cdf0e10cSrcweir void MergeCells( const uno::Reference< XTable >& xTable, sal_Int32 nCol, sal_Int32 nRow, sal_Int32 nColSpan, sal_Int32 nRowSpan )
97*cdf0e10cSrcweir {
98*cdf0e10cSrcweir    if( xTable.is() ) try
99*cdf0e10cSrcweir    {
100*cdf0e10cSrcweir        Reference< XMergeableCellRange > xRange( xTable->createCursorByRange( xTable->getCellRangeByPosition( nCol, nRow,nCol + nColSpan - 1, nRow + nRowSpan - 1 ) ), UNO_QUERY_THROW );
101*cdf0e10cSrcweir        if( xRange->isMergeable() )
102*cdf0e10cSrcweir                xRange->merge();
103*cdf0e10cSrcweir    }
104*cdf0e10cSrcweir    catch( Exception& )
105*cdf0e10cSrcweir    {
106*cdf0e10cSrcweir    }
107*cdf0e10cSrcweir }
108*cdf0e10cSrcweir 
109*cdf0e10cSrcweir static TableStyle* pDefaultTableStyle = new TableStyle();
110*cdf0e10cSrcweir 
111*cdf0e10cSrcweir const TableStyle& TableProperties::getUsedTableStyle( const ::oox::core::XmlFilterBase& rFilterBase )
112*cdf0e10cSrcweir {
113*cdf0e10cSrcweir 	::oox::core::XmlFilterBase& rBase( const_cast< ::oox::core::XmlFilterBase& >( rFilterBase ) );
114*cdf0e10cSrcweir 
115*cdf0e10cSrcweir 	TableStyle* pTableStyle = NULL;
116*cdf0e10cSrcweir 	if ( mpTableStyle )
117*cdf0e10cSrcweir 		pTableStyle = &*mpTableStyle;
118*cdf0e10cSrcweir 	else if ( rBase.getTableStyles() )
119*cdf0e10cSrcweir 	{
120*cdf0e10cSrcweir 		const std::vector< TableStyle >& rTableStyles( rBase.getTableStyles()->getTableStyles() );
121*cdf0e10cSrcweir 		const rtl::OUString aStyleId( getStyleId().getLength() ? getStyleId() : rBase.getTableStyles()->getDefaultStyleId() );
122*cdf0e10cSrcweir 		std::vector< TableStyle >::const_iterator aIter( rTableStyles.begin() );
123*cdf0e10cSrcweir 		while( aIter != rTableStyles.end() )
124*cdf0e10cSrcweir 		{
125*cdf0e10cSrcweir 			if ( const_cast< TableStyle& >( *aIter ).getStyleId() == aStyleId )
126*cdf0e10cSrcweir 			{
127*cdf0e10cSrcweir 				pTableStyle = &const_cast< TableStyle& >( *aIter );
128*cdf0e10cSrcweir 				break;	// we get the correct style
129*cdf0e10cSrcweir 			}
130*cdf0e10cSrcweir 			aIter++;
131*cdf0e10cSrcweir 		}
132*cdf0e10cSrcweir 	}
133*cdf0e10cSrcweir 	if ( !pTableStyle )
134*cdf0e10cSrcweir 		pTableStyle = pDefaultTableStyle;
135*cdf0e10cSrcweir 
136*cdf0e10cSrcweir 	return *pTableStyle;
137*cdf0e10cSrcweir }
138*cdf0e10cSrcweir 
139*cdf0e10cSrcweir void TableProperties::pushToPropSet( const ::oox::core::XmlFilterBase& rFilterBase,
140*cdf0e10cSrcweir 	const Reference < XPropertySet >& xPropSet, TextListStylePtr pMasterTextListStyle )
141*cdf0e10cSrcweir {
142*cdf0e10cSrcweir 	TableStyleListPtr( const_cast< ::oox::core::XmlFilterBase& >( rFilterBase ).getTableStyles() );
143*cdf0e10cSrcweir 
144*cdf0e10cSrcweir 	uno::Reference< XColumnRowRange > xColumnRowRange(
145*cdf0e10cSrcweir  		xPropSet->getPropertyValue( OUString(RTL_CONSTASCII_USTRINGPARAM("Model") ) ), uno::UNO_QUERY_THROW );
146*cdf0e10cSrcweir 
147*cdf0e10cSrcweir 	CreateTableColumns( xColumnRowRange->getColumns(), mvTableGrid );
148*cdf0e10cSrcweir 	CreateTableRows( xColumnRowRange->getRows(), mvTableRows );
149*cdf0e10cSrcweir 
150*cdf0e10cSrcweir 	const TableStyle& rTableStyle( getUsedTableStyle( rFilterBase ) );
151*cdf0e10cSrcweir 	sal_Int32 nRow = 0;
152*cdf0e10cSrcweir 	std::vector< TableRow >::iterator aTableRowIter( mvTableRows.begin() );
153*cdf0e10cSrcweir 	while( aTableRowIter != mvTableRows.end() )
154*cdf0e10cSrcweir 	{
155*cdf0e10cSrcweir 		sal_Int32 nColumn = 0;
156*cdf0e10cSrcweir 		std::vector< TableCell >::iterator aTableCellIter( aTableRowIter->getTableCells().begin() );
157*cdf0e10cSrcweir 		while( aTableCellIter != aTableRowIter->getTableCells().end() )
158*cdf0e10cSrcweir 		{
159*cdf0e10cSrcweir 			TableCell& rTableCell( *aTableCellIter );
160*cdf0e10cSrcweir 			if ( !rTableCell.getvMerge() && !rTableCell.gethMerge() )
161*cdf0e10cSrcweir 			{
162*cdf0e10cSrcweir 				uno::Reference< XTable > xTable( xColumnRowRange, uno::UNO_QUERY_THROW );
163*cdf0e10cSrcweir 				if ( ( rTableCell.getRowSpan() > 1 ) || ( rTableCell.getGridSpan() > 1 ) )
164*cdf0e10cSrcweir 					MergeCells( xTable, nColumn, nRow, rTableCell.getGridSpan(), rTableCell.getRowSpan() );
165*cdf0e10cSrcweir 
166*cdf0e10cSrcweir 				Reference< XCellRange > xCellRange( xTable, UNO_QUERY_THROW );
167*cdf0e10cSrcweir 				rTableCell.pushToXCell( rFilterBase, pMasterTextListStyle, xCellRange->getCellByPosition( nColumn, nRow ), *this, rTableStyle,
168*cdf0e10cSrcweir 					nColumn, aTableRowIter->getTableCells().size(), nRow, mvTableRows.size() );
169*cdf0e10cSrcweir 			}
170*cdf0e10cSrcweir 			nColumn++;
171*cdf0e10cSrcweir 			aTableCellIter++;
172*cdf0e10cSrcweir 		}
173*cdf0e10cSrcweir 		nRow++;
174*cdf0e10cSrcweir 		aTableRowIter++;
175*cdf0e10cSrcweir 	}
176*cdf0e10cSrcweir }
177*cdf0e10cSrcweir 
178*cdf0e10cSrcweir } } }
179