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