tdf#35361 Add a Quick Look plugins for .od* files on macOS
[LibreOffice.git] / sw / source / ui / vba / vbatablehelper.cxx
bloba5908635b9f8e2f7b0556bd4dbd1240b64449bde
1 /* -*- Mode: C++; tab-width: 4; indent-tabs-mode: nil; c-basic-offset: 4 -*- */
2 /*
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>
21 #include <utility>
22 #include <vbahelper/vbahelper.hxx>
23 #include "vbatablehelper.hxx"
24 #include <swtable.hxx>
25 #include <unotbl.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());
40 if( !pXTextTable )
41 throw uno::RuntimeException();
43 SwFrameFormat* pFrameFormat = pXTextTable->GetFrameFormat();
44 if( !pFrameFormat )
45 throw uno::RuntimeException();
47 SwTable* pTable = SwTable::FindTable( pFrameFormat );
48 return pTable;
51 sal_Int32 SwVbaTableHelper::getTabColumnsCount( sal_Int32 nRowIndex )
53 sal_Int32 nRet = 0;
54 if(!m_pTable->IsTableComplex())
56 SwTableLines& rLines = m_pTable->GetTabLines();
57 SwTableLine* pLine = rLines[ nRowIndex ];
58 nRet = pLine->GetTabBoxes().size();
60 return nRet;
63 sal_Int32 SwVbaTableHelper::getTabColumnsMaxCount( )
65 sal_Int32 nRet = 0;
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 )
71 nRet = nColCount;
73 return nRet;
76 sal_Int32 SwVbaTableHelper::getTabRowIndex( const OUString& rCellName )
78 sal_Int32 nRet = 0;
79 SwTableBox* pBox = const_cast<SwTableBox*>(m_pTable->GetTableBox( rCellName ));
80 if( !pBox )
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 );
87 return nRet;
90 sal_Int32 SwVbaTableHelper::getTabColIndex( const OUString& rCellName )
92 const SwTableBox* pBox = m_pTable->GetTableBox( rCellName );
93 if( !pBox )
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'
101 sal_Int32 nCalc = 0;
103 OUString sRet;
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;
111 nCol = nCol - nCalc;
112 if( 0 == nCol )
113 break;
114 nCol /= coDiff;
115 --nCol;
116 }while(true);
117 return 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;
130 else
132 xTableProps->getPropertyValue(u"Width"_ustr) >>= nWidth;
134 return 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 ];
151 if( !pStart )
152 throw uno::RuntimeException();
154 return pStart;
157 void SwVbaTableHelper::InitTabCols( SwTabCols& rCols, const SwTableBox *pStart )
159 rCols.SetLeftMin ( 0 );
160 rCols.SetLeft ( 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))
171 nCount ++;
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");
178 sal_Int32 i = 0;
179 while( nNum >= 0 )
181 if( !rCols.IsHidden(i) )
182 nNum--;
183 i++;
185 return i - 1;
188 sal_Int32 SwVbaTableHelper::GetColWidth( sal_Int32 nCol, sal_Int32 nRow )
190 SwTableBox* pStart = GetTabBox( nCol, nRow );
191 SwTabCols aCols;
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 )
202 SwTwips nWidth = 0;
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];
210 else
212 nWidth = rCols[nNum];
213 if(nNum == 0)
214 nWidth -= rCols.GetLeft();
215 else
216 nWidth -= rCols[nNum-1];
219 else
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;
230 else
231 nWidth = rCols.GetRight();
233 return nWidth;
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( );
240 if (!nTableWidth)
241 throw uno::RuntimeException();
242 sal_Int32 nNewWidth = dAbsWidth/nTableWidth * UNO_TABLE_COLUMN_SUM;
244 SwTableBox* pStart = GetTabBox( nCol, nRow );
245 SwTabCols aOldCols;
246 InitTabCols( aOldCols, pStart );
248 SwTabCols aCols( aOldCols );
249 if ( aCols.Count() > 0 )
251 SwTwips nWidth = GetColWidth( aCols, nCol);
253 int nDiff = nNewWidth - nWidth;
254 if( !nCol )
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;
260 else
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;
267 else
268 aCols[ GetRightSeparator( aCols, nCol-1 ) ] -= nDiff;
270 else
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: */