1 /**************************************************************
2  *
3  * Licensed to the Apache Software Foundation (ASF) under one
4  * or more contributor license agreements.  See the NOTICE file
5  * distributed with this work for additional information
6  * regarding copyright ownership.  The ASF licenses this file
7  * to you under the Apache License, Version 2.0 (the
8  * "License"); you may not use this file except in compliance
9  * with the License.  You may obtain a copy of the License at
10  *
11  *   http://www.apache.org/licenses/LICENSE-2.0
12  *
13  * Unless required by applicable law or agreed to in writing,
14  * software distributed under the License is distributed on an
15  * "AS IS" BASIS, WITHOUT WARRANTIES OR CONDITIONS OF ANY
16  * KIND, either express or implied.  See the License for the
17  * specific language governing permissions and limitations
18  * under the License.
19  *
20  *************************************************************/
21 
22 
23 
24 #ifndef _SVX_TABLE_TABLELAYOUTER_HXX_
25 #define _SVX_TABLE_TABLELAYOUTER_HXX_
26 
27 #include <com/sun/star/container/XIndexAccess.hpp>
28 #include <com/sun/star/text/WritingMode.hpp>
29 #include <com/sun/star/table/XTable.hpp>
30 #include <basegfx/range/b2irectangle.hxx>
31 #include <basegfx/tuple/b2ituple.hxx>
32 #include <tools/gen.hxx>
33 #include <boost/shared_ptr.hpp>
34 #include <vector>
35 #include <map>
36 
37 #include "svx/svdotable.hxx"
38 
39 // -----------------------------------------------------------------------------
40 
41 class SvxBorderLine;
42 
43 namespace sdr { namespace table {
44 
45 /** returns true if the cell(nMergedCol,nMergedRow) is merged with other cells.
46 	the returned cell( rOriginCol, rOriginRow ) is the origin( top left cell ) of the merge.
47 */
48 bool findMergeOrigin( const TableModelRef& xTable, sal_Int32 nMergedCol, sal_Int32 nMergedRow, sal_Int32& rOriginCol, sal_Int32& rOriginRow );
49 
50 typedef std::vector< SvxBorderLine* > BorderLineVector;
51 typedef std::vector< BorderLineVector > BorderLineMap;
52 
53 // -----------------------------------------------------------------------------
54 // TableModel
55 // -----------------------------------------------------------------------------
56 
57 class TableLayouter
58 {
59 public:
60 	TableLayouter( const TableModelRef& xTableModel );
61 	virtual ~TableLayouter();
62 
63 	/** this checks if new rows are inserted or old rows where removed.
64 		This can be used to grow or shrink the table shape in this case.
65 		@returns
66 			the height difference
67 	sal_Int32 detectInsertedOrRemovedRows();
68 	*/
69 
70 	/** try to fit the table into the given rectangle.
71 		If the rectangle is to small, it will be grown to fit the table.
72 
73 		if bFitWidth or bFitHeight is set, the layouter tries to scale
74 		the rows and/or columns to the given area. The result my be bigger
75 		to fullfill constrains.
76 
77 		if bFitWidth or bFitHeight is set, the model is changed.
78 	*/
79 	void LayoutTable( ::Rectangle& rRectangle, bool bFitWidth, bool bFitHeight );
80 
81 	/** after a call to LayoutTable, this method can be used to set the new
82 		column and row sizes back to the model. */
83 //	void SetLayoutToModel();
84 
85 	void UpdateBorderLayout();
86 
87 	basegfx::B2ITuple getCellSize( const CellPos& rPos ) const;
88 	bool getCellArea( const CellRef& xCell, basegfx::B2IRectangle& rArea ) const;
89 	bool getCellArea( const CellPos& rPos, basegfx::B2IRectangle& rArea ) const;
90 
getRowCount() const91 	::sal_Int32 getRowCount() const { return static_cast< ::sal_Int32 >( maRows.size() ); }
getColumnCount() const92 	::sal_Int32 getColumnCount() const { return static_cast< ::sal_Int32 >( maColumns.size() ); }
93 
94 	sal_Int32 getRowHeight( sal_Int32 nRow );
95 
96 	// sets the layout height of the given row hard, LayoutTable must be called directly after calling this method! */
97 	void setRowHeight( sal_Int32 nRow, sal_Int32 nHeight );
98 
99 	sal_Int32 getColumnWidth( sal_Int32 nColumn );
100 
101 	// sets the layout width of the given column hard, LayoutTable must be called directly after calling this method! */
102 	void setColumnWidth( sal_Int32 nColumn, sal_Int32 nWidth );
103 
104 	sal_Int32 getMinimumColumnWidth( sal_Int32 nColumn );
105 
106 	sal_Int32 getColumnStart( sal_Int32 nColumn ) const;
107 	sal_Int32 getRowStart( sal_Int32 nRow ) const;
108 
109 	/** checks if the given edge is visible.
110 		Edges between merged cells are not visible.
111 	*/
112 	bool isEdgeVisible( sal_Int32 nEdgeX, sal_Int32 nEdgeY, bool bHorizontal ) const;
113 
114 	/** returns the requested borderline in rpBorderLine or a null pointer if there is no border at this edge */
115 	SvxBorderLine* getBorderLine( sal_Int32 nEdgeX, sal_Int32 nEdgeY, bool bHorizontal )const;
116 
117 	void updateCells( ::Rectangle& rRectangle );
118 
119 	sal_Int32 getHorizontalEdge( int nEdgeY, sal_Int32* pnMin = 0, sal_Int32* pnMax = 0 );
120 	sal_Int32 getVerticalEdge( int nEdgeX , sal_Int32* pnMin = 0, sal_Int32* pnMax = 0);
121 
122 	void DistributeColumns( ::Rectangle& rArea, sal_Int32 nFirstCol, sal_Int32 nLastCol );
123 	void DistributeRows( ::Rectangle& rArea, sal_Int32 nFirstRow, sal_Int32 nLastRow );
124 
GetWritingMode() const125 	com::sun::star::text::WritingMode GetWritingMode() const { return meWritingMode; }
126 	void SetWritingMode( com::sun::star::text::WritingMode eWritingMode );
127 
128 private:
129 	CellRef getCell( const CellPos& rPos ) const;
130 
131 	void LayoutTableWidth( ::Rectangle& rArea, bool bFit );
132 	void LayoutTableHeight( ::Rectangle& rArea, bool bFit );
133 
isValidColumn(sal_Int32 nColumn) const134 	inline bool isValidColumn( sal_Int32 nColumn ) const { return (nColumn >= 0) && (nColumn < static_cast<sal_Int32>( maColumns.size())); }
isValidRow(sal_Int32 nRow) const135 	inline bool isValidRow( sal_Int32 nRow ) const { return (nRow >= 0) && (nRow < static_cast<sal_Int32>( maRows.size())); }
isValid(const CellPos & rPos) const136 	inline bool isValid( const CellPos& rPos ) const { return isValidColumn( rPos.mnCol ) && isValidRow( rPos.mnRow ); }
137 
138 	void ClearBorderLayout();
139 	void ClearBorderLayout(BorderLineMap& rMap);
140 	void ResizeBorderLayout();
141 	void ResizeBorderLayout( BorderLineMap& rMap );
142 
143 	void SetBorder( sal_Int32 nCol, sal_Int32 nRow, bool bHorizontal, const SvxBorderLine* pLine );
144 
145 	static bool HasPriority( const SvxBorderLine* pThis, const SvxBorderLine* pOther );
146 
147 	struct Layout
148 	{
149 		sal_Int32 mnPos;
150 		sal_Int32 mnSize;
151 		sal_Int32 mnMinSize;
152 
Layoutsdr::table::TableLayouter::Layout153 		Layout() : mnPos( 0 ), mnSize( 0 ), mnMinSize( 0 ) {}
clearsdr::table::TableLayouter::Layout154 		void clear() { mnPos = 0; mnSize = 0; mnMinSize = 0; }
155 	};
156 	typedef std::vector< Layout > LayoutVector;
157 
158 	sal_Int32 distribute( LayoutVector& rLayouts, sal_Int32 nDistribute );
159 
160 	TableModelRef mxTable;
161 	LayoutVector maRows;
162 	LayoutVector maColumns;
163 
164 	BorderLineMap maHorizontalBorders;
165 	BorderLineMap maVerticalBorders;
166 
167 	com::sun::star::text::WritingMode	meWritingMode;
168 
169 	const rtl::OUString msSize;
170 };
171 
172 } }
173 
174 #endif
175