LibreOffice Module sw (master)  1
vbatablehelper.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 <vbahelper/vbahelper.hxx>
21 #include "vbatablehelper.hxx"
22 #include <swtable.hxx>
23 #include <unotbl.hxx>
24 
25 using namespace ::com::sun::star;
26 using namespace ::ooo::vba;
27 
28 #define UNO_TABLE_COLUMN_SUM 10000
29 
30 SwVbaTableHelper::SwVbaTableHelper( const uno::Reference< text::XTextTable >& xTextTable ) : mxTextTable( xTextTable )
31 {
33 }
34 
35 SwTable* SwVbaTableHelper::GetSwTable( const uno::Reference< text::XTextTable >& xTextTable )
36 {
37  uno::Reference< lang::XUnoTunnel > xTunnel( xTextTable, uno::UNO_QUERY_THROW );
38  SwXTextTable* pXTextTable = reinterpret_cast< SwXTextTable * >( sal::static_int_cast< sal_IntPtr >(xTunnel->getSomething(SwXTextTable::getUnoTunnelId())));
39  if( !pXTextTable )
40  throw uno::RuntimeException();
41 
42  SwFrameFormat* pFrameFormat = pXTextTable->GetFrameFormat();
43  if( !pFrameFormat )
44  throw uno::RuntimeException();
45 
46  SwTable* pTable = SwTable::FindTable( pFrameFormat );
47  return pTable;
48 }
49 
50 sal_Int32 SwVbaTableHelper::getTabColumnsCount( sal_Int32 nRowIndex )
51 {
52  sal_Int32 nRet = 0;
53  if(!pTable->IsTableComplex())
54  {
55  SwTableLines& rLines = pTable->GetTabLines();
56  SwTableLine* pLine = rLines[ nRowIndex ];
57  nRet = pLine->GetTabBoxes().size();
58  }
59  return nRet;
60 }
61 
63 {
64  sal_Int32 nRet = 0;
65  sal_Int32 nRowCount = pTable->GetTabLines().size();
66  for( sal_Int32 index = 0; index < nRowCount; index++ )
67  {
68  sal_Int32 nColCount = getTabColumnsCount( index );
69  if( nRet < nColCount )
70  nRet = nColCount;
71  }
72  return nRet;
73 }
74 
75 sal_Int32 SwVbaTableHelper::getTabRowIndex( const OUString& rCellName )
76 {
77  sal_Int32 nRet = 0;
78  SwTableBox* pBox = const_cast<SwTableBox*>(pTable->GetTableBox( rCellName ));
79  if( !pBox )
80  throw uno::RuntimeException();
81 
82  const SwTableLine* pLine = pBox->GetUpper();
83  const SwTableLines* pLines = pLine->GetUpper()
84  ? &pLine->GetUpper()->GetTabLines() : &pTable->GetTabLines();
85  nRet = pLines->GetPos( pLine );
86  return nRet;
87 }
88 
89 sal_Int32 SwVbaTableHelper::getTabColIndex( const OUString& rCellName )
90 {
91  const SwTableBox* pBox = pTable->GetTableBox( rCellName );
92  if( !pBox )
93  throw uno::RuntimeException();
94  return pBox->GetUpper()->GetBoxPos( pBox );
95 }
96 
97 OUString SwVbaTableHelper::getColumnStr( sal_Int32 nCol )
98 {
99  const sal_Int32 coDiff = 52; // 'A'-'Z' 'a' - 'z'
100  sal_Int32 nCalc = 0;
101 
102  OUString sRet;
103  do{
104  nCalc = nCol % coDiff;
105  if( nCalc >= 26 )
106  sRet = OUStringChar( sal_Unicode('a' - 26 + nCalc) ) + sRet;
107  else
108  sRet = OUStringChar( sal_Unicode('A' + nCalc) ) + sRet;
109 
110  nCol = nCol - nCalc;
111  if( 0 == nCol )
112  break;
113  nCol /= coDiff;
114  --nCol;
115  }while(true);
116  return sRet;
117 }
118 
120 {
121  sal_Int32 nWidth = 0;
122  bool isWidthRelatvie = false;
123  uno::Reference< beans::XPropertySet > xTableProps( mxTextTable, uno::UNO_QUERY_THROW );
124  xTableProps->getPropertyValue("IsWidthRelative") >>= isWidthRelatvie;
125  if( isWidthRelatvie )
126  {
127  xTableProps->getPropertyValue("RelativeWidth") >>= nWidth;
128  }
129  else
130  {
131  xTableProps->getPropertyValue("Width") >>= nWidth;
132  }
133  return nWidth;
134 }
135 
136 SwTableBox* SwVbaTableHelper::GetTabBox( sal_Int32 nCol, sal_Int32 nRow )
137 {
138  SwTableLines& rLines = pTable->GetTabLines();
139  sal_Int32 nRowCount = rLines.size();
140  if (nRow < 0 || nRow >= nRowCount)
141  throw uno::RuntimeException();
142 
143  SwTableLine* pLine = rLines[ nRow ];
144  sal_Int32 nColCount = pLine->GetTabBoxes().size();
145  if (nCol < 0 || nCol >= nColCount)
146  throw uno::RuntimeException();
147 
148  SwTableBox* pStart = pLine->GetTabBoxes()[ nCol ];
149 
150  if( !pStart )
151  throw uno::RuntimeException();
152 
153  return pStart;
154 }
155 
157 {
158  rCols.SetLeftMin ( 0 );
159  rCols.SetLeft ( 0 );
160  rCols.SetRight ( UNO_TABLE_COLUMN_SUM );
162  pTable->GetTabCols( rCols, pStart );
163 }
164 
165 sal_Int32 SwVbaTableHelper::GetColCount( SwTabCols const & rCols )
166 {
167  sal_Int32 nCount = 0;
168  for( size_t i = 0; i < rCols.Count(); ++i )
169  if(rCols.IsHidden(i))
170  nCount ++;
171  return rCols.Count() - nCount;
172 }
173 
174 sal_Int32 SwVbaTableHelper::GetRightSeparator( SwTabCols const & rCols, sal_Int32 nNum)
175 {
176  OSL_ENSURE( nNum < GetColCount( rCols ) ,"Index out of range");
177  sal_Int32 i = 0;
178  while( nNum >= 0 )
179  {
180  if( !rCols.IsHidden(i) )
181  nNum--;
182  i++;
183  }
184  return i - 1;
185 }
186 
187 sal_Int32 SwVbaTableHelper::GetColWidth( sal_Int32 nCol, sal_Int32 nRow )
188 {
189  SwTableBox* pStart = GetTabBox( nCol, nRow );
190  SwTabCols aCols;
191  InitTabCols( aCols, pStart );
192  sal_Int32 nWidth = GetColWidth( aCols, nCol );
193 
194  sal_Int32 nTableWidth = getTableWidth( );
195  double dAbsWidth = ( static_cast<double>(nWidth) / UNO_TABLE_COLUMN_SUM ) * static_cast<double>(nTableWidth);
196  return static_cast<sal_Int32>(Millimeter::getInPoints( static_cast<int>(dAbsWidth) ));
197 }
198 
199 sal_Int32 SwVbaTableHelper::GetColWidth( SwTabCols& rCols, sal_Int32 nNum )
200 {
201  SwTwips nWidth = 0;
202 
203  if( rCols.Count() > 0 )
204  {
205  if(rCols.Count() == static_cast<size_t>(GetColCount( rCols )))
206  {
207  if(static_cast<size_t>(nNum) == rCols.Count())
208  nWidth = rCols.GetRight() - rCols[nNum-1];
209  else
210  {
211  nWidth = rCols[nNum];
212  if(nNum == 0)
213  nWidth -= rCols.GetLeft();
214  else
215  nWidth -= rCols[nNum-1];
216  }
217  }
218  else
219  {
220  SwTwips nRValid = nNum < GetColCount( rCols ) ?
221  rCols[GetRightSeparator( rCols, nNum )]:
222  rCols.GetRight();
223  SwTwips nLValid = nNum ?
224  rCols[GetRightSeparator( rCols, nNum - 1 )]:
225  rCols.GetLeft();
226  nWidth = nRValid - nLValid;
227  }
228  }
229  else
230  nWidth = rCols.GetRight();
231 
232  return nWidth;
233 }
234 
235 void SwVbaTableHelper::SetColWidth( sal_Int32 _width, sal_Int32 nCol, sal_Int32 nRow, bool bCurRowOnly )
236 {
237  double dAbsWidth = Millimeter::getInHundredthsOfOneMillimeter( _width );
238  sal_Int32 nTableWidth = getTableWidth( );
239  if (!nTableWidth)
240  throw uno::RuntimeException();
241  sal_Int32 nNewWidth = dAbsWidth/nTableWidth * UNO_TABLE_COLUMN_SUM;
242 
243  SwTableBox* pStart = GetTabBox( nCol, nRow );
244  SwTabCols aOldCols;
245  InitTabCols( aOldCols, pStart );
246 
247  SwTabCols aCols( aOldCols );
248  if ( aCols.Count() > 0 )
249  {
250  SwTwips nWidth = GetColWidth( aCols, nCol);
251 
252  int nDiff = nNewWidth - nWidth;
253  if( !nCol )
254  aCols[ GetRightSeparator(aCols, 0) ] += nDiff;
255  else if( nCol < GetColCount( aCols ) )
256  {
257  if(nDiff < GetColWidth( aCols, nCol + 1) - MINLAY)
258  aCols[ GetRightSeparator( aCols, nCol ) ] += nDiff;
259  else
260  {
261  int nDiffLeft = nDiff - static_cast<int>(GetColWidth( aCols, nCol + 1)) + int(MINLAY);
262  aCols[ GetRightSeparator( aCols, nCol ) ] += (nDiff - nDiffLeft);
263  aCols[ GetRightSeparator( aCols, nCol - 1 ) ] -= nDiffLeft;
264  }
265  }
266  else
267  aCols[ GetRightSeparator( aCols, nCol-1 ) ] -= nDiff;
268  }
269  else
270  aCols.SetRight( std::min( static_cast<tools::Long>(nNewWidth), aCols.GetRightMax()) );
271 
272  pTable->SetTabCols(aCols, aOldCols, pStart, bCurRowOnly );
273 }
274 
275 /* vim:set shiftwidth=4 softtabstop=4 expandtab: */
bool IsTableComplex() const
Definition: swtable.cxx:1428
const int nColCount
void SetLeft(tools::Long nNew)
Definition: tabcol.hxx:83
const SwTableBox * GetTableBox(const OUString &rName, const bool bPerformValidCheck=false) const
Definition: swtable.cxx:1327
void SetRight(tools::Long nNew)
Definition: tabcol.hxx:84
#define MINLAY
Definition: swtypes.hxx:66
tools::Long GetLeft() const
Definition: tabcol.hxx:78
tools::Long GetRightMax() const
Definition: tabcol.hxx:80
sal_Int32 getTableWidth() const
static sal_Int32 GetColWidth(SwTabCols &rCols, sal_Int32 nNum)
SwTableBox * GetTabBox(sal_Int32 nCol, sal_Int32 nRow)
SwTableLine is one table row in the document model.
Definition: swtable.hxx:351
#define UNO_TABLE_COLUMN_SUM
size_type size() const
Definition: swtable.hxx:75
void InitTabCols(SwTabCols &rCols, const SwTableBox *pStart)
sal_Int32 getTabColIndex(const OUString &sCellName)
void SetColWidth(sal_Int32 _width, sal_Int32 nCol, sal_Int32 nRow=0, bool bCurRowOnly=false)
void SetRightMax(tools::Long nNew)
Definition: tabcol.hxx:85
sal_uInt16 sal_Unicode
void GetTabCols(SwTabCols &rToFill, const SwTableBox *pStart, bool bHidden=false, bool bCurRowOnly=false) const
Definition: swtable.cxx:509
static sal_Int32 GetRightSeparator(SwTabCols const &rCols, sal_Int32 nNum)
sal_Int32 getTabColumnsCount(sal_Int32 nRowIndex)
int nCount
void SetTabCols(const SwTabCols &rNew, const SwTabCols &rOld, const SwTableBox *pStart, bool bCurRowOnly)
Definition: swtable.cxx:814
bool IsHidden(size_t nPos) const
Definition: tabcol.hxx:67
static SwTable * FindTable(SwFrameFormat const *const pFormat)
Definition: swtable.cxx:1905
Style of a layout element.
Definition: frmfmt.hxx:58
int i
css::uno::Reference< css::text::XTextTable > mxTextTable
static OUString getColumnStr(sal_Int32 nCol)
sal_Int32 getTabColumnsMaxCount()
void SetLeftMin(tools::Long nNew)
Definition: tabcol.hxx:82
tuple index
sal_uInt16 GetBoxPos(const SwTableBox *pBox) const
Definition: swtable.hxx:363
SwTableLines & GetTabLines()
Definition: swtable.hxx:200
tools::Long SwTwips
Definition: swtypes.hxx:49
SwTable is one table in the document model, containing rows (which contain cells).
Definition: swtable.hxx:111
SwTableLines & GetTabLines()
Definition: swtable.hxx:425
SwTableBoxes & GetTabBoxes()
Definition: swtable.hxx:361
SwVbaTableHelper(const css::uno::Reference< css::text::XTextTable > &xTextTable)
SwTableBox is one table cell in the document model.
Definition: swtable.hxx:393
static sal_Int32 GetColCount(SwTabCols const &rCols)
SwTableBox * GetUpper()
Definition: swtable.hxx:369
static SwTable * GetSwTable(const css::uno::Reference< css::text::XTextTable > &xTextTable)
sal_uInt16 GetPos(const SwTableLine *pBox) const
Definition: swtable.hxx:97
sal_Int32 getTabRowIndex(const OUString &sCellName)
SwTableLine * GetUpper()
Definition: swtable.hxx:428
static SW_DLLPUBLIC const css::uno::Sequence< sal_Int8 > & getUnoTunnelId()
Definition: unotbl.cxx:1987
tools::Long GetRight() const
Definition: tabcol.hxx:79
size_t Count() const
Definition: tabcol.hxx:65
SW_DLLPUBLIC SwFrameFormat * GetFrameFormat()
Definition: unotbl.cxx:2030