LibreOffice Module sw (master)  1
swtablerep.cxx
Go to the documentation of this file.
1 /* -*- Mode: C++; tab-width: 4; indent-tabs-mode: nil; c-basic-offset: 4 -*- */
2 /*
3  * This file is part of the LibreOffice project.
4  *
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/.
8  *
9  * This file incorporates work covered by the following license notice:
10  *
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 .
18  */
19 
20 #include <tabcol.hxx>
21 #include "tablepg.hxx"
22 
23 #include <swtablerep.hxx>
24 #include <memory>
25 
27  :
28  m_nTableWidth(0),
29  m_nSpace(0),
30  m_nLeftSpace(0),
31  m_nRightSpace(0),
32  m_nAlign(0),
33  m_nWidthPercent(0),
34  m_bLineSelected(false),
35  m_bWidthChanged(false),
36  m_bColsChanged(false)
37 {
38  m_nAllCols = m_nColCount = rTabCol.Count();
39  m_pTColumns.reset( new TColumn[ m_nColCount + 1 ] );
40  SwTwips nStart = 0,
41  nEnd;
42  for( sal_uInt16 i = 0; i < m_nAllCols; ++i )
43  {
44  nEnd = rTabCol[ i ] - rTabCol.GetLeft();
45  m_pTColumns[ i ].nWidth = nEnd - nStart;
46  m_pTColumns[ i ].bVisible = !rTabCol.IsHidden(i);
47  if(!m_pTColumns[ i ].bVisible)
48  m_nColCount --;
49  nStart = nEnd;
50  }
51  m_pTColumns[ m_nAllCols ].nWidth = rTabCol.GetRight() - rTabCol.GetLeft() - nStart;
52  m_pTColumns[ m_nAllCols ].bVisible = true;
53  m_nColCount++;
54  m_nAllCols++;
55 }
56 
58 {
59 }
60 
61 bool SwTableRep::FillTabCols( SwTabCols& rTabCols ) const
62 {
63  long nOldLeft = rTabCols.GetLeft(),
64  nOldRight = rTabCols.GetRight();
65 
66  bool bSingleLine = false;
67 
68  for ( size_t i = 0; i < rTabCols.Count(); ++i )
69  if(!m_pTColumns[i].bVisible)
70  {
71  bSingleLine = true;
72  break;
73  }
74 
75  SwTwips nPos = 0;
76  const SwTwips nLeft = GetLeftSpace();
77  rTabCols.SetLeft(nLeft);
78  if(bSingleLine)
79  {
80  // The invisible separators are taken from the old TabCols,
81  // the visible coming from pTColumns.
82  std::unique_ptr<TColumn[]> pOldTColumns(new TColumn[m_nAllCols + 1]);
83  SwTwips nStart = 0;
84  for ( sal_uInt16 i = 0; i < m_nAllCols - 1; ++i )
85  {
86  const SwTwips nEnd = rTabCols[i] - rTabCols.GetLeft();
87  pOldTColumns[i].nWidth = nEnd - nStart;
88  pOldTColumns[i].bVisible = !rTabCols.IsHidden(i);
89  nStart = nEnd;
90  }
91  pOldTColumns[m_nAllCols - 1].nWidth = rTabCols.GetRight() - rTabCols.GetLeft() - nStart;
92  pOldTColumns[m_nAllCols - 1].bVisible = true;
93 
94  sal_uInt16 nOldPos = 0;
95  sal_uInt16 nNewPos = 0;
96  SwTwips nOld = 0;
97  SwTwips nNew = 0;
98  bool bOld = false;
99  bool bFirst = true;
100 
101  for ( sal_uInt16 i = 0; i < m_nAllCols - 1; ++i )
102  {
103  while((bFirst || bOld ) && nOldPos < m_nAllCols )
104  {
105  nOld += pOldTColumns[nOldPos].nWidth;
106  nOldPos++;
107  if(!pOldTColumns[nOldPos - 1].bVisible)
108  break;
109  }
110  while((bFirst || !bOld ) && nNewPos < m_nAllCols )
111  {
112  nNew += m_pTColumns[nNewPos].nWidth;
113  nNewPos++;
114  if(pOldTColumns[nNewPos - 1].bVisible)
115  break;
116  }
117  bFirst = false;
118  // They have to be inserted sorted.
119  bOld = nOld < nNew;
120  nPos = bOld ? nOld : nNew;
121  rTabCols[i] = nPos + nLeft;
122  rTabCols.SetHidden( i, bOld );
123  }
124  rTabCols.SetRight(nLeft + m_nTableWidth);
125  }
126  else
127  {
128  for ( sal_uInt16 i = 0; i < m_nAllCols - 1; ++i )
129  {
130  nPos += m_pTColumns[i].nWidth;
131  rTabCols[i] = nPos + rTabCols.GetLeft();
132  rTabCols.SetHidden( i, !m_pTColumns[i].bVisible );
133  rTabCols.SetRight(nLeft + m_pTColumns[m_nAllCols - 1].nWidth + nPos);
134  }
135  }
136 
137  // intercept rounding errors
138  if(std::abs(nOldLeft - rTabCols.GetLeft()) < 3)
139  rTabCols.SetLeft(nOldLeft);
140 
141  if(std::abs(nOldRight - rTabCols.GetRight()) < 3)
142  rTabCols.SetRight(nOldRight);
143 
144  if(GetRightSpace() >= 0 &&
145  rTabCols.GetRight() > rTabCols.GetRightMax())
146  rTabCols.SetRight(rTabCols.GetRightMax());
147  return bSingleLine;
148 }
149 
150 /* vim:set shiftwidth=4 softtabstop=4 expandtab: */
sal_uInt16 m_nColCount
Definition: swtablerep.hxx:37
bool bVisible
SwTwips GetLeftSpace() const
Definition: swtablerep.hxx:50
std::unique_ptr< TColumn[]> m_pTColumns
Definition: swtablerep.hxx:30
sal_uInt16 m_nAllCols
Definition: swtablerep.hxx:38
long GetRightMax() const
Definition: tabcol.hxx:79
bool FillTabCols(SwTabCols &rTabCol) const
Definition: swtablerep.cxx:61
long SwTwips
Definition: swtypes.hxx:49
SwTwips GetRightSpace() const
Definition: swtablerep.hxx:53
long GetRight() const
Definition: tabcol.hxx:78
bool IsHidden(size_t nPos) const
Definition: tabcol.hxx:66
int i
void SetLeft(long nNew)
Definition: tabcol.hxx:82
SwTwips m_nTableWidth
Definition: swtablerep.hxx:32
SwTableRep(const SwTabCols &rTabCol)
Definition: swtablerep.cxx:26
void SetHidden(size_t nPos, bool bValue)
Definition: tabcol.hxx:67
void SetRight(long nNew)
Definition: tabcol.hxx:83
sal_uInt16 nPos
long GetLeft() const
Definition: tabcol.hxx:77
size_t Count() const
Definition: tabcol.hxx:64