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 : SwFrmFmt* pFrmFmt = pXTextTable->GetFrmFmt();
42 0 : if( !pFrmFmt )
43 0 : throw uno::RuntimeException();
44 :
45 0 : SwTable* pTable = SwTable::FindTable( pFrmFmt );
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->IsTblComplex())
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 = (SwTableBox*)pTable->GetTblBox( 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 = (SwTableBox*)pTable->GetTblBox( 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 : sal_Bool isWidthRelatvie = sal_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, sal_False, sal_False );
167 0 : }
168 :
169 0 : sal_Int32 SwVbaTableHelper::GetColCount( SwTabCols& rCols ) const
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) const
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 : nWidth = ((static_cast<size_t>(nNum) == rCols.Count()) ?
212 0 : rCols.GetRight() - rCols[nNum-1] :
213 0 : nNum == 0 ? rCols[nNum] - rCols.GetLeft() :
214 0 : rCols[nNum] - rCols[nNum-1]);
215 : }
216 : else
217 : {
218 0 : SwTwips nRValid = nNum < GetColCount( rCols ) ?
219 0 : rCols[GetRightSeparator( rCols, nNum )]:
220 0 : rCols.GetRight();
221 : SwTwips nLValid = nNum ?
222 0 : rCols[GetRightSeparator( rCols, nNum - 1 )]:
223 0 : rCols.GetLeft();
224 0 : nWidth = nRValid - nLValid;
225 : }
226 : }
227 : else
228 0 : nWidth = rCols.GetRight();
229 :
230 0 : return nWidth;
231 : }
232 :
233 0 : void SwVbaTableHelper::SetColWidth( sal_Int32 _width, sal_Int32 nCol, sal_Int32 nRow, bool bCurRowOnly ) throw (css::uno::RuntimeException)
234 : {
235 0 : double dAbsWidth = Millimeter::getInHundredthsOfOneMillimeter( _width );
236 0 : sal_Int32 nTableWidth = getTableWidth( );
237 0 : sal_Int32 nNewWidth = dAbsWidth/nTableWidth * UNO_TABLE_COLUMN_SUM;
238 :
239 0 : SwTableBox* pStart = GetTabBox( nCol, nRow );
240 0 : SwTabCols aOldCols;
241 0 : InitTabCols( aOldCols, pStart, bCurRowOnly );
242 :
243 0 : SwTabCols aCols( aOldCols );
244 0 : if ( aCols.Count() > 0 )
245 : {
246 0 : SwTwips nWidth = GetColWidth( aCols, nCol);
247 :
248 0 : int nDiff = nNewWidth - nWidth;
249 0 : if( !nCol )
250 0 : aCols[ GetRightSeparator(aCols, 0) ] += nDiff;
251 0 : else if( nCol < GetColCount( aCols ) )
252 : {
253 0 : if(nDiff < GetColWidth( aCols, nCol + 1) - MINLAY)
254 0 : aCols[ GetRightSeparator( aCols, nCol ) ] += nDiff;
255 : else
256 : {
257 0 : int nDiffLeft = nDiff - (int)GetColWidth( aCols, nCol + 1) + (int)MINLAY;
258 0 : aCols[ GetRightSeparator( aCols, nCol ) ] += (nDiff - nDiffLeft);
259 0 : aCols[ GetRightSeparator( aCols, nCol - 1 ) ] -= nDiffLeft;
260 : }
261 : }
262 : else
263 0 : aCols[ GetRightSeparator( aCols, nCol-1 ) ] -= nDiff;
264 : }
265 : else
266 0 : aCols.SetRight( std::min( (long)nNewWidth, aCols.GetRightMax()) );
267 :
268 0 : pTable->SetTabCols(aCols, aOldCols, pStart, bCurRowOnly );
269 0 : }
270 :
271 : /* vim:set shiftwidth=4 softtabstop=4 expandtab: */
|