1 /* -*- Mode: C++; tab-width: 4; indent-tabs-mode: nil; c-basic-offset: 4 -*- */
3 * This file is part of the LibreOffice project.
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/.
9 * This file incorporates work covered by the following license notice:
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 .
20 #include <comphelper/servicehelper.hxx>
22 #include <vbahelper/vbahelper.hxx>
23 #include "vbatablehelper.hxx"
24 #include <swtable.hxx>
27 using namespace ::com::sun::star
;
28 using namespace ::ooo::vba
;
30 #define UNO_TABLE_COLUMN_SUM 10000
32 SwVbaTableHelper::SwVbaTableHelper( uno::Reference
< text::XTextTable
> xTextTable
) : mxTextTable(std::move( xTextTable
))
34 m_pTable
= GetSwTable( mxTextTable
);
37 SwTable
* SwVbaTableHelper::GetSwTable( const uno::Reference
< text::XTextTable
>& xTextTable
)
39 SwXTextTable
* pXTextTable
= dynamic_cast<SwXTextTable
*>(xTextTable
.get());
41 throw uno::RuntimeException();
43 SwFrameFormat
* pFrameFormat
= pXTextTable
->GetFrameFormat();
45 throw uno::RuntimeException();
47 SwTable
* pTable
= SwTable::FindTable( pFrameFormat
);
51 sal_Int32
SwVbaTableHelper::getTabColumnsCount( sal_Int32 nRowIndex
)
54 if(!m_pTable
->IsTableComplex())
56 SwTableLines
& rLines
= m_pTable
->GetTabLines();
57 SwTableLine
* pLine
= rLines
[ nRowIndex
];
58 nRet
= pLine
->GetTabBoxes().size();
63 sal_Int32
SwVbaTableHelper::getTabColumnsMaxCount( )
66 sal_Int32 nRowCount
= m_pTable
->GetTabLines().size();
67 for( sal_Int32 index
= 0; index
< nRowCount
; index
++ )
69 sal_Int32 nColCount
= getTabColumnsCount( index
);
70 if( nRet
< nColCount
)
76 sal_Int32
SwVbaTableHelper::getTabRowIndex( const OUString
& rCellName
)
79 SwTableBox
* pBox
= const_cast<SwTableBox
*>(m_pTable
->GetTableBox( rCellName
));
81 throw uno::RuntimeException();
83 const SwTableLine
* pLine
= pBox
->GetUpper();
84 const SwTableLines
* pLines
= pLine
->GetUpper()
85 ? &pLine
->GetUpper()->GetTabLines() : &m_pTable
->GetTabLines();
86 nRet
= pLines
->GetPos( pLine
);
90 sal_Int32
SwVbaTableHelper::getTabColIndex( const OUString
& rCellName
)
92 const SwTableBox
* pBox
= m_pTable
->GetTableBox( rCellName
);
94 throw uno::RuntimeException();
95 return pBox
->GetUpper()->GetBoxPos( pBox
);
98 OUString
SwVbaTableHelper::getColumnStr( sal_Int32 nCol
)
100 const sal_Int32 coDiff
= 52; // 'A'-'Z' 'a' - 'z'
105 nCalc
= nCol
% coDiff
;
107 sRet
= OUStringChar( sal_Unicode('a' - 26 + nCalc
) ) + sRet
;
109 sRet
= OUStringChar( sal_Unicode('A' + nCalc
) ) + sRet
;
120 sal_Int32
SwVbaTableHelper::getTableWidth( ) const
122 sal_Int32 nWidth
= 0;
123 bool isWidthRelatvie
= false;
124 uno::Reference
< beans::XPropertySet
> xTableProps( mxTextTable
, uno::UNO_QUERY_THROW
);
125 xTableProps
->getPropertyValue(u
"IsWidthRelative"_ustr
) >>= isWidthRelatvie
;
126 if( isWidthRelatvie
)
128 xTableProps
->getPropertyValue(u
"RelativeWidth"_ustr
) >>= nWidth
;
132 xTableProps
->getPropertyValue(u
"Width"_ustr
) >>= nWidth
;
137 SwTableBox
* SwVbaTableHelper::GetTabBox( sal_Int32 nCol
, sal_Int32 nRow
)
139 SwTableLines
& rLines
= m_pTable
->GetTabLines();
140 sal_Int32 nRowCount
= rLines
.size();
141 if (nRow
< 0 || nRow
>= nRowCount
)
142 throw uno::RuntimeException();
144 SwTableLine
* pLine
= rLines
[ nRow
];
145 sal_Int32 nColCount
= pLine
->GetTabBoxes().size();
146 if (nCol
< 0 || nCol
>= nColCount
)
147 throw uno::RuntimeException();
149 SwTableBox
* pStart
= pLine
->GetTabBoxes()[ nCol
];
152 throw uno::RuntimeException();
157 void SwVbaTableHelper::InitTabCols( SwTabCols
& rCols
, const SwTableBox
*pStart
)
159 rCols
.SetLeftMin ( 0 );
161 rCols
.SetRight ( UNO_TABLE_COLUMN_SUM
);
162 rCols
.SetRightMax( UNO_TABLE_COLUMN_SUM
);
163 m_pTable
->GetTabCols( rCols
, pStart
);
166 sal_Int32
SwVbaTableHelper::GetColCount( SwTabCols
const & rCols
)
168 sal_Int32 nCount
= 0;
169 for( size_t i
= 0; i
< rCols
.Count(); ++i
)
170 if(rCols
.IsHidden(i
))
172 return rCols
.Count() - nCount
;
175 sal_Int32
SwVbaTableHelper::GetRightSeparator( SwTabCols
const & rCols
, sal_Int32 nNum
)
177 OSL_ENSURE( nNum
< GetColCount( rCols
) ,"Index out of range");
181 if( !rCols
.IsHidden(i
) )
188 sal_Int32
SwVbaTableHelper::GetColWidth( sal_Int32 nCol
, sal_Int32 nRow
)
190 SwTableBox
* pStart
= GetTabBox( nCol
, nRow
);
192 InitTabCols( aCols
, pStart
);
193 sal_Int32 nWidth
= GetColWidth( aCols
, nCol
);
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
) ));
200 sal_Int32
SwVbaTableHelper::GetColWidth( SwTabCols
& rCols
, sal_Int32 nNum
)
204 if( rCols
.Count() > 0 )
206 if(rCols
.Count() == static_cast<size_t>(GetColCount( rCols
)))
208 if(static_cast<size_t>(nNum
) == rCols
.Count())
209 nWidth
= rCols
.GetRight() - rCols
[nNum
-1];
212 nWidth
= rCols
[nNum
];
214 nWidth
-= rCols
.GetLeft();
216 nWidth
-= rCols
[nNum
-1];
221 SwTwips nRValid
= nNum
< GetColCount( rCols
) ?
222 rCols
[GetRightSeparator( rCols
, nNum
)]:
224 SwTwips nLValid
= nNum
?
225 rCols
[GetRightSeparator( rCols
, nNum
- 1 )]:
227 nWidth
= nRValid
- nLValid
;
231 nWidth
= rCols
.GetRight();
236 void SwVbaTableHelper::SetColWidth( sal_Int32 _width
, sal_Int32 nCol
, sal_Int32 nRow
, bool bCurRowOnly
)
238 double dAbsWidth
= Millimeter::getInHundredthsOfOneMillimeter( _width
);
239 sal_Int32 nTableWidth
= getTableWidth( );
241 throw uno::RuntimeException();
242 sal_Int32 nNewWidth
= dAbsWidth
/nTableWidth
* UNO_TABLE_COLUMN_SUM
;
244 SwTableBox
* pStart
= GetTabBox( nCol
, nRow
);
246 InitTabCols( aOldCols
, pStart
);
248 SwTabCols
aCols( aOldCols
);
249 if ( aCols
.Count() > 0 )
251 SwTwips nWidth
= GetColWidth( aCols
, nCol
);
253 int nDiff
= nNewWidth
- nWidth
;
255 aCols
[ GetRightSeparator(aCols
, 0) ] += nDiff
;
256 else if( nCol
< GetColCount( aCols
) )
258 if(nDiff
< GetColWidth( aCols
, nCol
+ 1) - MINLAY
)
259 aCols
[ GetRightSeparator( aCols
, nCol
) ] += nDiff
;
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
;
268 aCols
[ GetRightSeparator( aCols
, nCol
-1 ) ] -= nDiff
;
271 aCols
.SetRight( std::min( static_cast<tools::Long
>(nNewWidth
), aCols
.GetRightMax()) );
273 m_pTable
->SetTabCols(aCols
, aOldCols
, pStart
, bCurRowOnly
);
276 /* vim:set shiftwidth=4 softtabstop=4 expandtab: */