xref: /trunk/main/svx/source/table/tablelayouter.hxx (revision 1ecadb572e7010ff3b3382ad9bf179dbc6efadbb)
1 /*************************************************************************
2  *
3  * DO NOT ALTER OR REMOVE COPYRIGHT NOTICES OR THIS FILE HEADER.
4  *
5  * Copyright 2000, 2010 Oracle and/or its affiliates.
6  *
7  * OpenOffice.org - a multi-platform office productivity suite
8  *
9  * This file is part of OpenOffice.org.
10  *
11  * OpenOffice.org is free software: you can redistribute it and/or modify
12  * it under the terms of the GNU Lesser General Public License version 3
13  * only, as published by the Free Software Foundation.
14  *
15  * OpenOffice.org is distributed in the hope that it will be useful,
16  * but WITHOUT ANY WARRANTY; without even the implied warranty of
17  * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the
18  * GNU Lesser General Public License version 3 for more details
19  * (a copy is included in the LICENSE file that accompanied this code).
20  *
21  * You should have received a copy of the GNU Lesser General Public License
22  * version 3 along with OpenOffice.org.  If not, see
23  * <http://www.openoffice.org/license.html>
24  * for a copy of the LGPLv3 License.
25  *
26  ************************************************************************/
27 
28 #ifndef _SVX_TABLE_TABLELAYOUTER_HXX_
29 #define _SVX_TABLE_TABLELAYOUTER_HXX_
30 
31 #include <com/sun/star/container/XIndexAccess.hpp>
32 #include <com/sun/star/text/WritingMode.hpp>
33 #include <com/sun/star/table/XTable.hpp>
34 #include <basegfx/range/b2irectangle.hxx>
35 #include <basegfx/tuple/b2ituple.hxx>
36 #include <tools/gen.hxx>
37 #include <boost/shared_ptr.hpp>
38 #include <vector>
39 #include <map>
40 
41 #include "svx/svdotable.hxx"
42 
43 // -----------------------------------------------------------------------------
44 
45 class SvxBorderLine;
46 
47 namespace sdr { namespace table {
48 
49 /** returns true if the cell(nMergedCol,nMergedRow) is merged with other cells.
50     the returned cell( rOriginCol, rOriginRow ) is the origin( top left cell ) of the merge.
51 */
52 bool findMergeOrigin( const TableModelRef& xTable, sal_Int32 nMergedCol, sal_Int32 nMergedRow, sal_Int32& rOriginCol, sal_Int32& rOriginRow );
53 
54 typedef std::vector< SvxBorderLine* > BorderLineVector;
55 typedef std::vector< BorderLineVector > BorderLineMap;
56 
57 // -----------------------------------------------------------------------------
58 // TableModel
59 // -----------------------------------------------------------------------------
60 
61 class TableLayouter
62 {
63 public:
64     TableLayouter( const TableModelRef& xTableModel );
65     virtual ~TableLayouter();
66 
67     /** this checks if new rows are inserted or old rows where removed.
68         This can be used to grow or shrink the table shape in this case.
69         @returns
70             the height difference
71     sal_Int32 detectInsertedOrRemovedRows();
72     */
73 
74     /** try to fit the table into the given rectangle.
75         If the rectangle is to small, it will be grown to fit the table.
76 
77         if bFitWidth or bFitHeight is set, the layouter tries to scale
78         the rows and/or columns to the given area. The result my be bigger
79         to fullfill constrains.
80 
81         if bFitWidth or bFitHeight is set, the model is changed.
82     */
83     void LayoutTable( ::Rectangle& rRectangle, bool bFitWidth, bool bFitHeight );
84 
85     /** after a call to LayoutTable, this method can be used to set the new
86         column and row sizes back to the model. */
87 //  void SetLayoutToModel();
88 
89     void UpdateBorderLayout();
90 
91     basegfx::B2ITuple getCellSize( const CellPos& rPos ) const;
92     bool getCellArea( const CellRef& xCell, basegfx::B2IRectangle& rArea ) const;
93     bool getCellArea( const CellPos& rPos, basegfx::B2IRectangle& rArea ) const;
94 
95     ::sal_Int32 getRowCount() const { return static_cast< ::sal_Int32 >( maRows.size() ); }
96     ::sal_Int32 getColumnCount() const { return static_cast< ::sal_Int32 >( maColumns.size() ); }
97 
98     sal_Int32 getRowHeight( sal_Int32 nRow );
99 
100     // sets the layout height of the given row hard, LayoutTable must be called directly after calling this method! */
101     void setRowHeight( sal_Int32 nRow, sal_Int32 nHeight );
102 
103     sal_Int32 getColumnWidth( sal_Int32 nColumn );
104 
105     // sets the layout width of the given column hard, LayoutTable must be called directly after calling this method! */
106     void setColumnWidth( sal_Int32 nColumn, sal_Int32 nWidth );
107 
108     sal_Int32 getMinimumColumnWidth( sal_Int32 nColumn );
109 
110     sal_Int32 getColumnStart( sal_Int32 nColumn ) const;
111     sal_Int32 getRowStart( sal_Int32 nRow ) const;
112 
113     /** checks if the given edge is visible.
114         Edges between merged cells are not visible.
115     */
116     bool isEdgeVisible( sal_Int32 nEdgeX, sal_Int32 nEdgeY, bool bHorizontal ) const;
117 
118     /** returns the requested borderline in rpBorderLine or a null pointer if there is no border at this edge */
119     SvxBorderLine* getBorderLine( sal_Int32 nEdgeX, sal_Int32 nEdgeY, bool bHorizontal )const;
120 
121     void updateCells( ::Rectangle& rRectangle );
122 
123     sal_Int32 getHorizontalEdge( int nEdgeY, sal_Int32* pnMin = 0, sal_Int32* pnMax = 0 );
124     sal_Int32 getVerticalEdge( int nEdgeX , sal_Int32* pnMin = 0, sal_Int32* pnMax = 0);
125 
126     void DistributeColumns( ::Rectangle& rArea, sal_Int32 nFirstCol, sal_Int32 nLastCol );
127     void DistributeRows( ::Rectangle& rArea, sal_Int32 nFirstRow, sal_Int32 nLastRow );
128 
129     com::sun::star::text::WritingMode GetWritingMode() const { return meWritingMode; }
130     void SetWritingMode( com::sun::star::text::WritingMode eWritingMode );
131 
132 private:
133     CellRef getCell( const CellPos& rPos ) const;
134 
135     void LayoutTableWidth( ::Rectangle& rArea, bool bFit );
136     void LayoutTableHeight( ::Rectangle& rArea, bool bFit );
137 
138     inline bool isValidColumn( sal_Int32 nColumn ) const { return (nColumn >= 0) && (nColumn < static_cast<sal_Int32>( maColumns.size())); }
139     inline bool isValidRow( sal_Int32 nRow ) const { return (nRow >= 0) && (nRow < static_cast<sal_Int32>( maRows.size())); }
140     inline bool isValid( const CellPos& rPos ) const { return isValidColumn( rPos.mnCol ) && isValidRow( rPos.mnRow ); }
141 
142     void ClearBorderLayout();
143     void ClearBorderLayout(BorderLineMap& rMap);
144     void ResizeBorderLayout();
145     void ResizeBorderLayout( BorderLineMap& rMap );
146 
147     void SetBorder( sal_Int32 nCol, sal_Int32 nRow, bool bHorizontal, const SvxBorderLine* pLine );
148 
149     static bool HasPriority( const SvxBorderLine* pThis, const SvxBorderLine* pOther );
150 
151     struct Layout
152     {
153         sal_Int32 mnPos;
154         sal_Int32 mnSize;
155         sal_Int32 mnMinSize;
156 
157         Layout() : mnPos( 0 ), mnSize( 0 ), mnMinSize( 0 ) {}
158         void clear() { mnPos = 0; mnSize = 0; mnMinSize = 0; }
159     };
160     typedef std::vector< Layout > LayoutVector;
161 
162     sal_Int32 distribute( LayoutVector& rLayouts, sal_Int32 nDistribute );
163 
164     TableModelRef mxTable;
165     LayoutVector maRows;
166     LayoutVector maColumns;
167 
168     BorderLineMap maHorizontalBorders;
169     BorderLineMap maVerticalBorders;
170 
171     com::sun::star::text::WritingMode   meWritingMode;
172 
173     const rtl::OUString msSize;
174 };
175 
176 } }
177 
178 #endif
179