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