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  if( 0 == ( nCol = nCol - nCalc ) )
111  break;
112  nCol /= coDiff;
113  --nCol;
114  }while(true);
115  return sRet;
116 }
117 
119 {
120  sal_Int32 nWidth = 0;
121  bool isWidthRelatvie = false;
122  uno::Reference< beans::XPropertySet > xTableProps( mxTextTable, uno::UNO_QUERY_THROW );
123  xTableProps->getPropertyValue("IsWidthRelative") >>= isWidthRelatvie;
124  if( isWidthRelatvie )
125  {
126  xTableProps->getPropertyValue("RelativeWidth") >>= nWidth;
127  }
128  else
129  {
130  xTableProps->getPropertyValue("Width") >>= nWidth;
131  }
132  return nWidth;
133 }
134 
135 SwTableBox* SwVbaTableHelper::GetTabBox( sal_Int32 nCol, sal_Int32 nRow )
136 {
137  SwTableLines& rLines = pTable->GetTabLines();
138  sal_Int32 nRowCount = rLines.size();
139  if (nRow < 0 || nRow >= nRowCount)
140  throw uno::RuntimeException();
141 
142  SwTableLine* pLine = rLines[ nRow ];
143  sal_Int32 nColCount = pLine->GetTabBoxes().size();
144  if (nCol < 0 || nCol >= nColCount)
145  throw uno::RuntimeException();
146 
147  SwTableBox* pStart = pLine->GetTabBoxes()[ nCol ];
148 
149  if( !pStart )
150  throw uno::RuntimeException();
151 
152  return pStart;
153 }
154 
156 {
157  rCols.SetLeftMin ( 0 );
158  rCols.SetLeft ( 0 );
159  rCols.SetRight ( UNO_TABLE_COLUMN_SUM );
161  pTable->GetTabCols( rCols, pStart );
162 }
163 
164 sal_Int32 SwVbaTableHelper::GetColCount( SwTabCols const & rCols )
165 {
166  sal_Int32 nCount = 0;
167  for( size_t i = 0; i < rCols.Count(); ++i )
168  if(rCols.IsHidden(i))
169  nCount ++;
170  return rCols.Count() - nCount;
171 }
172 
173 sal_Int32 SwVbaTableHelper::GetRightSeparator( SwTabCols const & rCols, sal_Int32 nNum)
174 {
175  OSL_ENSURE( nNum < GetColCount( rCols ) ,"Index out of range");
176  sal_Int32 i = 0;
177  while( nNum >= 0 )
178  {
179  if( !rCols.IsHidden(i) )
180  nNum--;
181  i++;
182  }
183  return i - 1;
184 }
185 
186 sal_Int32 SwVbaTableHelper::GetColWidth( sal_Int32 nCol, sal_Int32 nRow )
187 {
188  SwTableBox* pStart = GetTabBox( nCol, nRow );
189  SwTabCols aCols;
190  InitTabCols( aCols, pStart );
191  sal_Int32 nWidth = GetColWidth( aCols, nCol );
192 
193  sal_Int32 nTableWidth = getTableWidth( );
194  double dAbsWidth = ( static_cast<double>(nWidth) / UNO_TABLE_COLUMN_SUM ) * static_cast<double>(nTableWidth);
195  return static_cast<sal_Int32>(Millimeter::getInPoints( static_cast<int>(dAbsWidth) ));
196 }
197 
198 sal_Int32 SwVbaTableHelper::GetColWidth( SwTabCols& rCols, sal_Int32 nNum )
199 {
200  SwTwips nWidth = 0;
201 
202  if( rCols.Count() > 0 )
203  {
204  if(rCols.Count() == static_cast<size_t>(GetColCount( rCols )))
205  {
206  if(static_cast<size_t>(nNum) == rCols.Count())
207  nWidth = rCols.GetRight() - rCols[nNum-1];
208  else
209  {
210  nWidth = rCols[nNum];
211  if(nNum == 0)
212  nWidth -= rCols.GetLeft();
213  else
214  nWidth -= rCols[nNum-1];
215  }
216  }
217  else
218  {
219  SwTwips nRValid = nNum < GetColCount( rCols ) ?
220  rCols[GetRightSeparator( rCols, nNum )]:
221  rCols.GetRight();
222  SwTwips nLValid = nNum ?
223  rCols[GetRightSeparator( rCols, nNum - 1 )]:
224  rCols.GetLeft();
225  nWidth = nRValid - nLValid;
226  }
227  }
228  else
229  nWidth = rCols.GetRight();
230 
231  return nWidth;
232 }
233 
234 void SwVbaTableHelper::SetColWidth( sal_Int32 _width, sal_Int32 nCol, sal_Int32 nRow, bool bCurRowOnly )
235 {
236  double dAbsWidth = Millimeter::getInHundredthsOfOneMillimeter( _width );
237  sal_Int32 nTableWidth = getTableWidth( );
238  if (!nTableWidth)
239  throw uno::RuntimeException();
240  sal_Int32 nNewWidth = dAbsWidth/nTableWidth * UNO_TABLE_COLUMN_SUM;
241 
242  SwTableBox* pStart = GetTabBox( nCol, nRow );
243  SwTabCols aOldCols;
244  InitTabCols( aOldCols, pStart );
245 
246  SwTabCols aCols( aOldCols );
247  if ( aCols.Count() > 0 )
248  {
249  SwTwips nWidth = GetColWidth( aCols, nCol);
250 
251  int nDiff = nNewWidth - nWidth;
252  if( !nCol )
253  aCols[ GetRightSeparator(aCols, 0) ] += nDiff;
254  else if( nCol < GetColCount( aCols ) )
255  {
256  if(nDiff < GetColWidth( aCols, nCol + 1) - MINLAY)
257  aCols[ GetRightSeparator( aCols, nCol ) ] += nDiff;
258  else
259  {
260  int nDiffLeft = nDiff - static_cast<int>(GetColWidth( aCols, nCol + 1)) + int(MINLAY);
261  aCols[ GetRightSeparator( aCols, nCol ) ] += (nDiff - nDiffLeft);
262  aCols[ GetRightSeparator( aCols, nCol - 1 ) ] -= nDiffLeft;
263  }
264  }
265  else
266  aCols[ GetRightSeparator( aCols, nCol-1 ) ] -= nDiff;
267  }
268  else
269  aCols.SetRight( std::min( static_cast<long>(nNewWidth), aCols.GetRightMax()) );
270 
271  pTable->SetTabCols(aCols, aOldCols, pStart, bCurRowOnly );
272 }
273 
274 /* vim:set shiftwidth=4 softtabstop=4 expandtab: */
bool IsTableComplex() const
Definition: swtable.cxx:1444
const int nColCount
const SwTableBox * GetTableBox(const OUString &rName, const bool bPerformValidCheck=false) const
Definition: swtable.cxx:1343
#define MINLAY
Definition: swtypes.hxx:66
sal_Int32 getTableWidth() const
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
sal_uInt16 sal_Unicode
void GetTabCols(SwTabCols &rToFill, const SwTableBox *pStart, bool bHidden=false, bool bCurRowOnly=false) const
Definition: swtable.cxx:527
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:832
bool IsHidden(size_t nPos) const
Definition: tabcol.hxx:66
static SwTable * FindTable(SwFrameFormat const *const pFormat)
Definition: swtable.cxx:1919
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:1989
long GetLeft() const
Definition: tabcol.hxx:77
size_t Count() const
Definition: tabcol.hxx:64
SW_DLLPUBLIC SwFrameFormat * GetFrameFormat()
Definition: unotbl.cxx:2032