xref: /aoo42x/main/svx/source/table/tablelayouter.hxx (revision cdf0e10c)
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 #ifndef _SVX_TABLE_TABLELAYOUTER_HXX_
29*cdf0e10cSrcweir #define _SVX_TABLE_TABLELAYOUTER_HXX_
30*cdf0e10cSrcweir 
31*cdf0e10cSrcweir #include <com/sun/star/container/XIndexAccess.hpp>
32*cdf0e10cSrcweir #include <com/sun/star/text/WritingMode.hpp>
33*cdf0e10cSrcweir #include <com/sun/star/table/XTable.hpp>
34*cdf0e10cSrcweir #include <basegfx/range/b2irectangle.hxx>
35*cdf0e10cSrcweir #include <basegfx/tuple/b2ituple.hxx>
36*cdf0e10cSrcweir #include <tools/gen.hxx>
37*cdf0e10cSrcweir #include <boost/shared_ptr.hpp>
38*cdf0e10cSrcweir #include <vector>
39*cdf0e10cSrcweir #include <map>
40*cdf0e10cSrcweir 
41*cdf0e10cSrcweir #include "svx/svdotable.hxx"
42*cdf0e10cSrcweir 
43*cdf0e10cSrcweir // -----------------------------------------------------------------------------
44*cdf0e10cSrcweir 
45*cdf0e10cSrcweir class SvxBorderLine;
46*cdf0e10cSrcweir 
47*cdf0e10cSrcweir namespace sdr { namespace table {
48*cdf0e10cSrcweir 
49*cdf0e10cSrcweir /** returns true if the cell(nMergedCol,nMergedRow) is merged with other cells.
50*cdf0e10cSrcweir 	the returned cell( rOriginCol, rOriginRow ) is the origin( top left cell ) of the merge.
51*cdf0e10cSrcweir */
52*cdf0e10cSrcweir bool findMergeOrigin( const TableModelRef& xTable, sal_Int32 nMergedCol, sal_Int32 nMergedRow, sal_Int32& rOriginCol, sal_Int32& rOriginRow );
53*cdf0e10cSrcweir 
54*cdf0e10cSrcweir typedef std::vector< SvxBorderLine* > BorderLineVector;
55*cdf0e10cSrcweir typedef std::vector< BorderLineVector > BorderLineMap;
56*cdf0e10cSrcweir 
57*cdf0e10cSrcweir // -----------------------------------------------------------------------------
58*cdf0e10cSrcweir // TableModel
59*cdf0e10cSrcweir // -----------------------------------------------------------------------------
60*cdf0e10cSrcweir 
61*cdf0e10cSrcweir class TableLayouter
62*cdf0e10cSrcweir {
63*cdf0e10cSrcweir public:
64*cdf0e10cSrcweir 	TableLayouter( const TableModelRef& xTableModel );
65*cdf0e10cSrcweir 	virtual ~TableLayouter();
66*cdf0e10cSrcweir 
67*cdf0e10cSrcweir 	/** this checks if new rows are inserted or old rows where removed.
68*cdf0e10cSrcweir 		This can be used to grow or shrink the table shape in this case.
69*cdf0e10cSrcweir 		@returns
70*cdf0e10cSrcweir 			the height difference
71*cdf0e10cSrcweir 	sal_Int32 detectInsertedOrRemovedRows();
72*cdf0e10cSrcweir 	*/
73*cdf0e10cSrcweir 
74*cdf0e10cSrcweir 	/** try to fit the table into the given rectangle.
75*cdf0e10cSrcweir 		If the rectangle is to small, it will be grown to fit the table.
76*cdf0e10cSrcweir 
77*cdf0e10cSrcweir 		if bFitWidth or bFitHeight is set, the layouter tries to scale
78*cdf0e10cSrcweir 		the rows and/or columns to the given area. The result my be bigger
79*cdf0e10cSrcweir 		to fullfill constrains.
80*cdf0e10cSrcweir 
81*cdf0e10cSrcweir 		if bFitWidth or bFitHeight is set, the model is changed.
82*cdf0e10cSrcweir 	*/
83*cdf0e10cSrcweir 	void LayoutTable( ::Rectangle& rRectangle, bool bFitWidth, bool bFitHeight );
84*cdf0e10cSrcweir 
85*cdf0e10cSrcweir 	/** after a call to LayoutTable, this method can be used to set the new
86*cdf0e10cSrcweir 		column and row sizes back to the model. */
87*cdf0e10cSrcweir //	void SetLayoutToModel();
88*cdf0e10cSrcweir 
89*cdf0e10cSrcweir 	void UpdateBorderLayout();
90*cdf0e10cSrcweir 
91*cdf0e10cSrcweir 	basegfx::B2ITuple getCellSize( const CellPos& rPos ) const;
92*cdf0e10cSrcweir 	bool getCellArea( const CellRef& xCell, basegfx::B2IRectangle& rArea ) const;
93*cdf0e10cSrcweir 	bool getCellArea( const CellPos& rPos, basegfx::B2IRectangle& rArea ) const;
94*cdf0e10cSrcweir 
95*cdf0e10cSrcweir 	::sal_Int32 getRowCount() const { return static_cast< ::sal_Int32 >( maRows.size() ); }
96*cdf0e10cSrcweir 	::sal_Int32 getColumnCount() const { return static_cast< ::sal_Int32 >( maColumns.size() ); }
97*cdf0e10cSrcweir 
98*cdf0e10cSrcweir 	sal_Int32 getRowHeight( sal_Int32 nRow );
99*cdf0e10cSrcweir 
100*cdf0e10cSrcweir 	// sets the layout height of the given row hard, LayoutTable must be called directly after calling this method! */
101*cdf0e10cSrcweir 	void setRowHeight( sal_Int32 nRow, sal_Int32 nHeight );
102*cdf0e10cSrcweir 
103*cdf0e10cSrcweir 	sal_Int32 getColumnWidth( sal_Int32 nColumn );
104*cdf0e10cSrcweir 
105*cdf0e10cSrcweir 	// sets the layout width of the given column hard, LayoutTable must be called directly after calling this method! */
106*cdf0e10cSrcweir 	void setColumnWidth( sal_Int32 nColumn, sal_Int32 nWidth );
107*cdf0e10cSrcweir 
108*cdf0e10cSrcweir 	sal_Int32 getMinimumColumnWidth( sal_Int32 nColumn );
109*cdf0e10cSrcweir 
110*cdf0e10cSrcweir 	sal_Int32 getColumnStart( sal_Int32 nColumn ) const;
111*cdf0e10cSrcweir 	sal_Int32 getRowStart( sal_Int32 nRow ) const;
112*cdf0e10cSrcweir 
113*cdf0e10cSrcweir 	/** checks if the given edge is visible.
114*cdf0e10cSrcweir 		Edges between merged cells are not visible.
115*cdf0e10cSrcweir 	*/
116*cdf0e10cSrcweir 	bool isEdgeVisible( sal_Int32 nEdgeX, sal_Int32 nEdgeY, bool bHorizontal ) const;
117*cdf0e10cSrcweir 
118*cdf0e10cSrcweir 	/** returns the requested borderline in rpBorderLine or a null pointer if there is no border at this edge */
119*cdf0e10cSrcweir 	SvxBorderLine* getBorderLine( sal_Int32 nEdgeX, sal_Int32 nEdgeY, bool bHorizontal )const;
120*cdf0e10cSrcweir 
121*cdf0e10cSrcweir 	void updateCells( ::Rectangle& rRectangle );
122*cdf0e10cSrcweir 
123*cdf0e10cSrcweir 	sal_Int32 getHorizontalEdge( int nEdgeY, sal_Int32* pnMin = 0, sal_Int32* pnMax = 0 );
124*cdf0e10cSrcweir 	sal_Int32 getVerticalEdge( int nEdgeX , sal_Int32* pnMin = 0, sal_Int32* pnMax = 0);
125*cdf0e10cSrcweir 
126*cdf0e10cSrcweir 	void DistributeColumns( ::Rectangle& rArea, sal_Int32 nFirstCol, sal_Int32 nLastCol );
127*cdf0e10cSrcweir 	void DistributeRows( ::Rectangle& rArea, sal_Int32 nFirstRow, sal_Int32 nLastRow );
128*cdf0e10cSrcweir 
129*cdf0e10cSrcweir 	com::sun::star::text::WritingMode GetWritingMode() const { return meWritingMode; }
130*cdf0e10cSrcweir 	void SetWritingMode( com::sun::star::text::WritingMode eWritingMode );
131*cdf0e10cSrcweir 
132*cdf0e10cSrcweir private:
133*cdf0e10cSrcweir 	CellRef getCell( const CellPos& rPos ) const;
134*cdf0e10cSrcweir 
135*cdf0e10cSrcweir 	void LayoutTableWidth( ::Rectangle& rArea, bool bFit );
136*cdf0e10cSrcweir 	void LayoutTableHeight( ::Rectangle& rArea, bool bFit );
137*cdf0e10cSrcweir 
138*cdf0e10cSrcweir 	inline bool isValidColumn( sal_Int32 nColumn ) const { return (nColumn >= 0) && (nColumn < static_cast<sal_Int32>( maColumns.size())); }
139*cdf0e10cSrcweir 	inline bool isValidRow( sal_Int32 nRow ) const { return (nRow >= 0) && (nRow < static_cast<sal_Int32>( maRows.size())); }
140*cdf0e10cSrcweir 	inline bool isValid( const CellPos& rPos ) const { return isValidColumn( rPos.mnCol ) && isValidRow( rPos.mnRow ); }
141*cdf0e10cSrcweir 
142*cdf0e10cSrcweir 	void ClearBorderLayout();
143*cdf0e10cSrcweir 	void ClearBorderLayout(BorderLineMap& rMap);
144*cdf0e10cSrcweir 	void ResizeBorderLayout();
145*cdf0e10cSrcweir 	void ResizeBorderLayout( BorderLineMap& rMap );
146*cdf0e10cSrcweir 
147*cdf0e10cSrcweir 	void SetBorder( sal_Int32 nCol, sal_Int32 nRow, bool bHorizontal, const SvxBorderLine* pLine );
148*cdf0e10cSrcweir 
149*cdf0e10cSrcweir 	static bool HasPriority( const SvxBorderLine* pThis, const SvxBorderLine* pOther );
150*cdf0e10cSrcweir 
151*cdf0e10cSrcweir 	struct Layout
152*cdf0e10cSrcweir 	{
153*cdf0e10cSrcweir 		sal_Int32 mnPos;
154*cdf0e10cSrcweir 		sal_Int32 mnSize;
155*cdf0e10cSrcweir 		sal_Int32 mnMinSize;
156*cdf0e10cSrcweir 
157*cdf0e10cSrcweir 		Layout() : mnPos( 0 ), mnSize( 0 ), mnMinSize( 0 ) {}
158*cdf0e10cSrcweir 		void clear() { mnPos = 0; mnSize = 0; mnMinSize = 0; }
159*cdf0e10cSrcweir 	};
160*cdf0e10cSrcweir 	typedef std::vector< Layout > LayoutVector;
161*cdf0e10cSrcweir 
162*cdf0e10cSrcweir 	sal_Int32 distribute( LayoutVector& rLayouts, sal_Int32 nDistribute );
163*cdf0e10cSrcweir 
164*cdf0e10cSrcweir 	TableModelRef mxTable;
165*cdf0e10cSrcweir 	LayoutVector maRows;
166*cdf0e10cSrcweir 	LayoutVector maColumns;
167*cdf0e10cSrcweir 
168*cdf0e10cSrcweir 	BorderLineMap maHorizontalBorders;
169*cdf0e10cSrcweir 	BorderLineMap maVerticalBorders;
170*cdf0e10cSrcweir 
171*cdf0e10cSrcweir 	com::sun::star::text::WritingMode	meWritingMode;
172*cdf0e10cSrcweir 
173*cdf0e10cSrcweir 	const rtl::OUString msSize;
174*cdf0e10cSrcweir };
175*cdf0e10cSrcweir 
176*cdf0e10cSrcweir } }
177*cdf0e10cSrcweir 
178*cdf0e10cSrcweir #endif
179