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