Branch data Line data Source code
1 : : /* -*- Mode: C++; tab-width: 4; indent-tabs-mode: nil; c-basic-offset: 4 -*- */
2 : : /*************************************************************************
3 : : *
4 : : * DO NOT ALTER OR REMOVE COPYRIGHT NOTICES OR THIS FILE HEADER.
5 : : *
6 : : * Copyright 2008 by Sun Microsystems, Inc.
7 : : *
8 : : * OpenOffice.org - a multi-platform office productivity suite
9 : : *
10 : : * This file is part of OpenOffice.org.
11 : : *
12 : : * OpenOffice.org is free software: you can redistribute it and/or modify
13 : : * it under the terms of the GNU Lesser General Public License version 3
14 : : * only, as published by the Free Software Foundation.
15 : : *
16 : : * OpenOffice.org is distributed in the hope that it will be useful,
17 : : * but WITHOUT ANY WARRANTY; without even the implied warranty of
18 : : * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
19 : : * GNU Lesser General Public License version 3 for more details
20 : : * (a copy is included in the LICENSE file that accompanied this code).
21 : : *
22 : : * You should have received a copy of the GNU Lesser General Public License
23 : : * version 3 along with OpenOffice.org. If not, see
24 : : * <http://www.openoffice.org/license.html>
25 : : * for a copy of the LGPLv3 License.
26 : : *
27 : : ************************************************************************/
28 : : #include "vbatablehelper.hxx"
29 : : #include <swtable.hxx>
30 : : #include <unotbl.hxx>
31 : : #include <docsh.hxx>
32 : :
33 : : using namespace ::com::sun::star;
34 : : using namespace ::ooo::vba;
35 : :
36 : : #define UNO_TABLE_COLUMN_SUM 10000
37 : :
38 : 0 : SwVbaTableHelper::SwVbaTableHelper( const uno::Reference< text::XTextTable >& xTextTable ) throw (uno::RuntimeException) : mxTextTable( xTextTable ), pDoc( NULL )
39 : : {
40 [ # # ]: 0 : pTable = GetSwTable( mxTextTable );
41 : 0 : }
42 : :
43 : 0 : SwTable* SwVbaTableHelper::GetSwTable( const uno::Reference< text::XTextTable >& xTextTable ) throw (uno::RuntimeException)
44 : : {
45 [ # # ]: 0 : uno::Reference< lang::XUnoTunnel > xTunnel( xTextTable, uno::UNO_QUERY_THROW );
46 [ # # ][ # # ]: 0 : SwXTextTable* pXTextTable = reinterpret_cast< SwXTextTable * >( sal::static_int_cast< sal_IntPtr >(xTunnel->getSomething(SwXTextTable::getUnoTunnelId())));
[ # # ]
47 [ # # ]: 0 : if( !pXTextTable )
48 [ # # ]: 0 : throw uno::RuntimeException();
49 : :
50 : 0 : SwFrmFmt* pFrmFmt = pXTextTable->GetFrmFmt();
51 [ # # ]: 0 : if( !pFrmFmt )
52 [ # # ]: 0 : throw uno::RuntimeException();
53 : :
54 [ # # ]: 0 : SwTable* pTable = SwTable::FindTable( pFrmFmt );
55 : 0 : return pTable;
56 : : }
57 : :
58 : 0 : sal_Int32 SwVbaTableHelper::getTabColumnsCount( sal_Int32 nRowIndex ) throw (uno::RuntimeException)
59 : : {
60 : 0 : sal_Int32 nRet = 0;
61 [ # # ]: 0 : if(!pTable->IsTblComplex())
62 : : {
63 : 0 : SwTableLines& rLines = pTable->GetTabLines();
64 : 0 : SwTableLine* pLine = rLines[ nRowIndex ];
65 : 0 : nRet = pLine->GetTabBoxes().size();
66 : : }
67 : 0 : return nRet;
68 : : }
69 : :
70 : 0 : sal_Int32 SwVbaTableHelper::getTabColumnsMaxCount( ) throw (uno::RuntimeException)
71 : : {
72 : 0 : sal_Int32 nRet = 0;
73 : : //sal_Int32 nRowCount = mxTextTable->getRows()->getCount();
74 : 0 : sal_Int32 nRowCount = pTable->GetTabLines().size();
75 [ # # ]: 0 : for( sal_Int32 index = 0; index < nRowCount; index++ )
76 : : {
77 : 0 : sal_Int32 nColCount = getTabColumnsCount( index );
78 [ # # ]: 0 : if( nRet < nColCount )
79 : 0 : nRet = nColCount;
80 : : }
81 : 0 : return nRet;
82 : : }
83 : :
84 : 0 : sal_Int32 SwVbaTableHelper::getTabRowIndex( const rtl::OUString& CellName ) throw (uno::RuntimeException)
85 : : {
86 : 0 : sal_Int32 nRet = 0;
87 [ # # ]: 0 : String sCellName(CellName);
88 [ # # ]: 0 : SwTableBox* pBox = (SwTableBox*)pTable->GetTblBox( sCellName );
89 [ # # ]: 0 : if( !pBox )
90 [ # # ]: 0 : throw uno::RuntimeException();
91 : :
92 : 0 : const SwTableLine* pLine = pBox->GetUpper();
93 : 0 : const SwTableLines* pLines = pLine->GetUpper()
94 [ # # ]: 0 : ? &pLine->GetUpper()->GetTabLines() : &pTable->GetTabLines();
95 [ # # ]: 0 : nRet = pLines->GetPos( pLine );
96 [ # # ]: 0 : return nRet;
97 : : }
98 : :
99 : 0 : sal_Int32 SwVbaTableHelper::getTabColIndex( const rtl::OUString& CellName ) throw (uno::RuntimeException)
100 : : {
101 : 0 : sal_Int32 nRet = 0;
102 [ # # ]: 0 : String sCellName(CellName);
103 [ # # ]: 0 : const SwTableBox* pBox = (SwTableBox*)pTable->GetTblBox( sCellName );
104 [ # # ]: 0 : if( !pBox )
105 [ # # ]: 0 : throw uno::RuntimeException();
106 : 0 : const SwTableBoxes* pBoxes = &pBox->GetUpper()->GetTabBoxes();
107 [ # # ]: 0 : nRet = pBoxes->GetPos( pBox );
108 [ # # ]: 0 : return nRet;
109 : : }
110 : :
111 : 0 : rtl::OUString SwVbaTableHelper::getColumnStr( sal_Int32 nCol )
112 : : {
113 : 0 : const sal_Int32 coDiff = 52; // 'A'-'Z' 'a' - 'z'
114 : 0 : sal_Int32 nCalc = 0;
115 : :
116 [ # # ]: 0 : String sRet;
117 : 0 : do{
118 : 0 : nCalc = nCol % coDiff;
119 [ # # ]: 0 : if( nCalc >= 26 )
120 [ # # ]: 0 : sRet.Insert( sal_Unicode('a' - 26 + nCalc ), 0 );
121 : : else
122 [ # # ]: 0 : sRet.Insert( sal_Unicode('A' + nCalc ), 0 );
123 : :
124 [ # # ]: 0 : if( 0 == ( nCol = nCol - nCalc ) )
125 : 0 : break;
126 : 0 : nCol /= coDiff;
127 : 0 : --nCol;
128 : : }while(1);
129 [ # # ][ # # ]: 0 : return sRet;
130 : : }
131 : :
132 : 0 : sal_Int32 SwVbaTableHelper::getTableWidth( ) throw (uno::RuntimeException)
133 : : {
134 : 0 : sal_Int32 nWidth = 0;
135 : 0 : sal_Bool isWidthRelatvie = sal_False;
136 [ # # ]: 0 : uno::Reference< beans::XPropertySet > xTableProps( mxTextTable, uno::UNO_QUERY_THROW );
137 [ # # ][ # # ]: 0 : xTableProps->getPropertyValue( rtl::OUString( RTL_CONSTASCII_USTRINGPARAM("IsWidthRelative") ) ) >>= isWidthRelatvie;
[ # # ]
138 [ # # ]: 0 : if( isWidthRelatvie )
139 : : {
140 [ # # ][ # # ]: 0 : xTableProps->getPropertyValue( rtl::OUString( RTL_CONSTASCII_USTRINGPARAM("RelativeWidth") ) ) >>= nWidth;
[ # # ]
141 : : }
142 : : else
143 : : {
144 [ # # ][ # # ]: 0 : xTableProps->getPropertyValue( rtl::OUString( RTL_CONSTASCII_USTRINGPARAM("Width") ) ) >>= nWidth;
[ # # ]
145 : : }
146 : 0 : return nWidth;
147 : : }
148 : :
149 : 0 : SwTableBox* SwVbaTableHelper::GetTabBox( sal_Int32 nCol, sal_Int32 nRow ) throw (css::uno::RuntimeException)
150 : : {
151 : 0 : SwTableLines& rLines = pTable->GetTabLines();
152 : 0 : sal_Int32 nRowCount = rLines.size();
153 [ # # ]: 0 : if( nRowCount < nRow )
154 [ # # ]: 0 : throw uno::RuntimeException();
155 : :
156 : 0 : SwTableBox* pStart = NULL;
157 : 0 : SwTableLine* pLine = rLines[ nRow ];
158 [ # # ]: 0 : if( (sal_Int32)pLine->GetTabBoxes().size() < nCol )
159 [ # # ]: 0 : throw uno::RuntimeException();
160 : :
161 : 0 : pStart = pLine->GetTabBoxes()[ nCol ];
162 : :
163 [ # # ]: 0 : if( !pStart )
164 [ # # ]: 0 : throw uno::RuntimeException();
165 : :
166 : 0 : return pStart;
167 : : }
168 : :
169 : 0 : void SwVbaTableHelper::InitTabCols( SwTabCols& rCols, const SwTableBox *pStart, sal_Bool /*bCurRowOnly*/ )
170 : : {
171 : 0 : rCols.SetLeftMin ( 0 );
172 : 0 : rCols.SetLeft ( 0 );
173 : 0 : rCols.SetRight ( UNO_TABLE_COLUMN_SUM );
174 : 0 : rCols.SetRightMax( UNO_TABLE_COLUMN_SUM );
175 : 0 : pTable->GetTabCols( rCols, pStart, sal_False, sal_False );
176 : 0 : }
177 : :
178 : 0 : sal_Int32 SwVbaTableHelper::GetColCount( SwTabCols& rCols ) const
179 : : {
180 : 0 : sal_Int32 nCount = 0;
181 [ # # ]: 0 : for(sal_Int32 i = 0; i < rCols.Count(); i++ )
182 [ # # ]: 0 : if(rCols.IsHidden(i))
183 : 0 : nCount ++;
184 : 0 : return rCols.Count() - nCount;
185 : : }
186 : :
187 : 0 : sal_Int32 SwVbaTableHelper::GetRightSeparator( SwTabCols& rCols, sal_Int32 nNum) const
188 : : {
189 : : OSL_ENSURE( nNum < (sal_Int32)GetColCount( rCols ) ,"Index out of range");
190 : 0 : sal_Int32 i = 0;
191 [ # # ]: 0 : while( nNum >= 0 )
192 : : {
193 [ # # ]: 0 : if( !rCols.IsHidden( static_cast< sal_uInt16 >(i)) )
194 : 0 : nNum--;
195 : 0 : i++;
196 : : }
197 : 0 : return i - 1;
198 : : }
199 : :
200 : 0 : sal_Int32 SwVbaTableHelper::GetColWidth( sal_Int32 nCol, sal_Int32 nRow, sal_Bool bCurRowOnly ) throw (uno::RuntimeException)
201 : : {
202 [ # # ]: 0 : SwTableBox* pStart = GetTabBox( nCol, nRow );
203 [ # # ]: 0 : SwTabCols aCols;
204 [ # # ]: 0 : InitTabCols( aCols, pStart, bCurRowOnly );
205 [ # # ]: 0 : sal_Int32 nWidth = GetColWidth( aCols, nCol );
206 : :
207 [ # # ]: 0 : sal_Int32 nTableWidth = getTableWidth( );
208 : 0 : double dAbsWidth = ( (double)nWidth / UNO_TABLE_COLUMN_SUM ) * (double) nTableWidth;
209 [ # # ]: 0 : return ( sal_Int32 )Millimeter::getInPoints( static_cast<int>(dAbsWidth) );
210 : : }
211 : :
212 : 0 : sal_Int32 SwVbaTableHelper::GetColWidth( SwTabCols& rCols, sal_Int32 nNum ) throw (uno::RuntimeException)
213 : : {
214 : 0 : SwTwips nWidth = 0;
215 : :
216 [ # # ]: 0 : if( rCols.Count() > 0 )
217 : : {
218 [ # # ]: 0 : if(rCols.Count() == GetColCount( rCols ))
219 : : {
220 : 0 : nWidth = (SwTwips)((nNum == rCols.Count()) ?
221 : 0 : rCols.GetRight() - rCols[nNum-1] :
222 : 0 : nNum == 0 ? rCols[nNum] - rCols.GetLeft() :
223 [ # # ][ # # ]: 0 : rCols[nNum] - rCols[nNum-1]);
224 : : }
225 : : else
226 : : {
227 : 0 : SwTwips nRValid = nNum < GetColCount( rCols ) ?
228 : 0 : rCols[(sal_uInt16)GetRightSeparator( rCols, nNum)]:
229 [ # # ]: 0 : rCols.GetRight();
230 : : SwTwips nLValid = nNum ?
231 : 0 : rCols[(sal_uInt16)GetRightSeparator( rCols, nNum - 1)]:
232 [ # # ]: 0 : rCols.GetLeft();
233 : 0 : nWidth = nRValid - nLValid;
234 : : }
235 : : }
236 : : else
237 : 0 : nWidth = rCols.GetRight();
238 : :
239 : 0 : return nWidth;
240 : : }
241 : :
242 : 0 : void SwVbaTableHelper::SetColWidth( sal_Int32 _width, sal_Int32 nCol, sal_Int32 nRow, sal_Bool bCurRowOnly ) throw (css::uno::RuntimeException)
243 : : {
244 [ # # ]: 0 : double dAbsWidth = Millimeter::getInHundredthsOfOneMillimeter( _width );
245 [ # # ]: 0 : sal_Int32 nTableWidth = getTableWidth( );
246 : 0 : sal_Int32 nNewWidth = (sal_Int32)( dAbsWidth/nTableWidth * UNO_TABLE_COLUMN_SUM );
247 : :
248 [ # # ]: 0 : SwTableBox* pStart = GetTabBox( nCol, nRow );
249 [ # # ]: 0 : SwTabCols aOldCols;
250 [ # # ]: 0 : InitTabCols( aOldCols, pStart, bCurRowOnly );
251 : :
252 [ # # ]: 0 : SwTabCols aCols( aOldCols );
253 [ # # ][ # # ]: 0 : if ( aCols.Count() > 0 )
254 : : {
255 [ # # ]: 0 : SwTwips nWidth = GetColWidth( aCols, nCol);
256 : :
257 : 0 : int nDiff = (int)(nNewWidth - nWidth);
258 [ # # ]: 0 : if( !nCol )
259 [ # # ][ # # ]: 0 : aCols[ static_cast< sal_uInt16 >(GetRightSeparator(aCols, 0)) ] += nDiff;
260 [ # # ][ # # ]: 0 : else if( nCol < GetColCount( aCols ) )
261 : : {
262 [ # # ][ # # ]: 0 : if(nDiff < GetColWidth( aCols, nCol + 1) - MINLAY)
263 [ # # ][ # # ]: 0 : aCols[ static_cast< sal_uInt16 >(GetRightSeparator( aCols, nCol)) ] += nDiff;
264 : : else
265 : : {
266 [ # # ]: 0 : int nDiffLeft = nDiff - (int)GetColWidth( aCols, nCol + 1) + (int)MINLAY;
267 [ # # ][ # # ]: 0 : aCols[ static_cast< sal_uInt16 >(GetRightSeparator( aCols, nCol)) ] += (nDiff - nDiffLeft);
268 [ # # ][ # # ]: 0 : aCols[ static_cast< sal_uInt16 >(GetRightSeparator( aCols, nCol - 1)) ] -= nDiffLeft;
269 : : }
270 : : }
271 : : else
272 [ # # ][ # # ]: 0 : aCols[ static_cast< sal_uInt16 >(GetRightSeparator( aCols, nCol-1)) ] -= nDiff;
273 : : }
274 : : else
275 : 0 : aCols.SetRight( Min( (long)nNewWidth, aCols.GetRightMax()) );
276 : :
277 [ # # ]: 0 : pTable->SetTabCols(aCols, aOldCols, pStart, bCurRowOnly );
278 : 0 : }
279 : :
280 : : /* vim:set shiftwidth=4 softtabstop=4 expandtab: */
|