Version 7.6.3.2-android, tag libreoffice-7.6.3.2-android
[LibreOffice.git] / sw / source / uibase / table / swtablerep.cxx
blob44a87df90bc18fa7465238ca7f5450f16fbb34ce
1 /* -*- Mode: C++; tab-width: 4; indent-tabs-mode: nil; c-basic-offset: 4 -*- */
2 /*
3 * This file is part of the LibreOffice project.
5 * This Source Code Form is subject to the terms of the Mozilla Public
6 * License, v. 2.0. If a copy of the MPL was not distributed with this
7 * file, You can obtain one at http://mozilla.org/MPL/2.0/.
9 * This file incorporates work covered by the following license notice:
11 * Licensed to the Apache Software Foundation (ASF) under one or more
12 * contributor license agreements. See the NOTICE file distributed
13 * with this work for additional information regarding copyright
14 * ownership. The ASF licenses this file to you under the Apache
15 * License, Version 2.0 (the "License"); you may not use this file
16 * except in compliance with the License. You may obtain a copy of
17 * the License at http://www.apache.org/licenses/LICENSE-2.0 .
20 #include <tabcol.hxx>
22 #include <swtablerep.hxx>
23 #include <memory>
25 SwTableRep::SwTableRep( const SwTabCols& rTabCol )
27 m_nTableWidth(0),
28 m_nSpace(0),
29 m_nLeftSpace(0),
30 m_nRightSpace(0),
31 m_nAlign(0),
32 m_nWidthPercent(0),
33 m_bLineSelected(false),
34 m_bWidthChanged(false),
35 m_bColsChanged(false)
37 m_nAllCols = m_nColCount = rTabCol.Count();
38 m_aTColumns.resize(m_nColCount + 1);
39 SwTwips nStart = 0,
40 nEnd;
41 for( sal_uInt16 i = 0; i < m_nAllCols; ++i )
43 nEnd = rTabCol[ i ] - rTabCol.GetLeft();
44 m_aTColumns[ i ].nWidth = nEnd - nStart;
45 m_aTColumns[ i ].bVisible = !rTabCol.IsHidden(i);
46 if(!m_aTColumns[ i ].bVisible)
47 m_nColCount --;
48 nStart = nEnd;
50 m_aTColumns[ m_nAllCols ].nWidth = rTabCol.GetRight() - rTabCol.GetLeft() - nStart;
51 m_aTColumns[ m_nAllCols ].bVisible = true;
52 m_nColCount++;
53 m_nAllCols++;
56 SwTableRep::~SwTableRep()
60 bool SwTableRep::FillTabCols( SwTabCols& rTabCols ) const
62 tools::Long nOldLeft = rTabCols.GetLeft(),
63 nOldRight = rTabCols.GetRight();
65 bool bSingleLine = false;
67 for ( size_t i = 0; i < rTabCols.Count(); ++i )
68 if(!m_aTColumns[i].bVisible)
70 bSingleLine = true;
71 break;
74 SwTwips nPos = 0;
75 const SwTwips nLeft = GetLeftSpace();
76 rTabCols.SetLeft(nLeft);
77 if(bSingleLine)
79 // The invisible separators are taken from the old TabCols,
80 // the visible coming from pTColumns.
81 std::unique_ptr<TColumn[]> pOldTColumns(new TColumn[m_nAllCols + 1]);
82 SwTwips nStart = 0;
83 for ( sal_uInt16 i = 0; i < m_nAllCols - 1; ++i )
85 const SwTwips nEnd = rTabCols[i] - rTabCols.GetLeft();
86 pOldTColumns[i].nWidth = nEnd - nStart;
87 pOldTColumns[i].bVisible = !rTabCols.IsHidden(i);
88 nStart = nEnd;
90 pOldTColumns[m_nAllCols - 1].nWidth = rTabCols.GetRight() - rTabCols.GetLeft() - nStart;
91 pOldTColumns[m_nAllCols - 1].bVisible = true;
93 sal_uInt16 nOldPos = 0;
94 sal_uInt16 nNewPos = 0;
95 SwTwips nOld = 0;
96 SwTwips nNew = 0;
97 bool bOld = false;
98 bool bFirst = true;
100 for ( sal_uInt16 i = 0; i < m_nAllCols - 1; ++i )
102 while((bFirst || bOld ) && nOldPos < m_nAllCols )
104 nOld += pOldTColumns[nOldPos].nWidth;
105 nOldPos++;
106 if(!pOldTColumns[nOldPos - 1].bVisible)
107 break;
109 while((bFirst || !bOld ) && nNewPos < m_nAllCols )
111 nNew += m_aTColumns[nNewPos].nWidth;
112 nNewPos++;
113 if(pOldTColumns[nNewPos - 1].bVisible)
114 break;
116 bFirst = false;
117 // They have to be inserted sorted.
118 bOld = nOld < nNew;
119 nPos = bOld ? nOld : nNew;
120 rTabCols[i] = nPos + nLeft;
121 rTabCols.SetHidden( i, bOld );
123 rTabCols.SetRight(nLeft + m_nTableWidth);
125 else
127 for ( sal_uInt16 i = 0; i < m_nAllCols - 1; ++i )
129 nPos += m_aTColumns[i].nWidth;
130 rTabCols[i] = nPos + rTabCols.GetLeft();
131 rTabCols.SetHidden( i, !m_aTColumns[i].bVisible );
132 rTabCols.SetRight(nLeft + m_aTColumns[m_nAllCols - 1].nWidth + nPos);
136 // intercept rounding errors
137 if(std::abs(nOldLeft - rTabCols.GetLeft()) < 3)
138 rTabCols.SetLeft(nOldLeft);
140 if(std::abs(nOldRight - rTabCols.GetRight()) < 3)
141 rTabCols.SetRight(nOldRight);
143 if(GetRightSpace() >= 0 &&
144 rTabCols.GetRight() > rTabCols.GetRightMax())
145 rTabCols.SetRight(rTabCols.GetRightMax());
146 return bSingleLine;
149 /* vim:set shiftwidth=4 softtabstop=4 expandtab: */