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