Version 4.0.2.1, tag libreoffice-4.0.2.1
[LibreOffice.git] / sc / source / filter / excel / xlescher.cxx
blobf04d983d2572817849f8e6c19fc3019dd403cfe5
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 .
21 #include "xlescher.hxx"
23 #include <com/sun/star/drawing/XControlShape.hpp>
24 #include <com/sun/star/script/ScriptEventDescriptor.hpp>
25 #include <svx/unoapi.hxx>
26 #include "document.hxx"
27 #include "xestream.hxx"
28 #include "xistream.hxx"
29 #include "xlroot.hxx"
30 #include "xltools.hxx"
32 using ::rtl::OUString;
33 using ::com::sun::star::uno::Reference;
34 using ::com::sun::star::uno::UNO_QUERY;
35 using ::com::sun::star::drawing::XShape;
36 using ::com::sun::star::drawing::XControlShape;
37 using ::com::sun::star::awt::XControlModel;
38 using ::com::sun::star::script::ScriptEventDescriptor;
40 namespace {
42 /** Returns the scaling factor to calculate coordinates from twips. */
43 double lclGetTwipsScale( MapUnit eMapUnit )
45 /* We cannot use OutputDevice::LogicToLogic() or the XclTools
46 conversion functions to calculate drawing layer coordinates due to
47 Calc's strange definition of a point (1 inch == 72.27 points, instead
48 of 72 points).
49 NOTE: Calc's definition changed from TeX points (72.27) to PS points
50 (72), so the MAP_TWIP case now actually also delivers a scale of 1.0
52 double fScale = 1.0;
53 switch( eMapUnit )
55 case MAP_TWIP: fScale = PS_POINTS_PER_INCH / POINTS_PER_INCH; break; // Calc twips <-> real twips
56 case MAP_100TH_MM: fScale = HMM_PER_TWIPS; break; // Calc twips <-> 1/100mm
57 default: OSL_FAIL( "lclGetTwipsScale - map unit not implemented" );
59 return fScale;
62 /** Calculates a drawing layer X position (in twips) from an object column position. */
63 long lclGetXFromCol( ScDocument& rDoc, SCTAB nScTab, sal_uInt16 nXclCol, sal_uInt16 nOffset, double fScale )
65 SCCOL nScCol = static_cast< SCCOL >( nXclCol );
66 return static_cast< long >( fScale * (rDoc.GetColOffset( nScCol, nScTab ) +
67 ::std::min( nOffset / 1024.0, 1.0 ) * rDoc.GetColWidth( nScCol, nScTab )) + 0.5 );
70 /** Calculates a drawing layer Y position (in twips) from an object row position. */
71 long lclGetYFromRow( ScDocument& rDoc, SCTAB nScTab, sal_uInt16 nXclRow, sal_uInt16 nOffset, double fScale )
73 SCROW nScRow = static_cast< SCROW >( nXclRow );
74 return static_cast< long >( fScale * (rDoc.GetRowOffset( nScRow, nScTab ) +
75 ::std::min( nOffset / 256.0, 1.0 ) * rDoc.GetRowHeight( nScRow, nScTab )) + 0.5 );
78 /** Calculates an object column position from a drawing layer X position (in twips). */
79 void lclGetColFromX(
80 ScDocument& rDoc, SCTAB nScTab, sal_uInt16& rnXclCol,
81 sal_uInt16& rnOffset, sal_uInt16 nXclStartCol, sal_uInt16 nXclMaxCol,
82 long& rnStartW, long nX, double fScale )
84 // rnStartW in conjunction with nXclStartCol is used as buffer for previously calculated width
85 long nTwipsX = static_cast< long >( nX / fScale + 0.5 );
86 long nColW = 0;
87 for( rnXclCol = nXclStartCol; rnXclCol <= nXclMaxCol; ++rnXclCol )
89 nColW = rDoc.GetColWidth( static_cast< SCCOL >( rnXclCol ), nScTab );
90 if( rnStartW + nColW > nTwipsX )
91 break;
92 rnStartW += nColW;
94 rnOffset = nColW ? static_cast< sal_uInt16 >( (nTwipsX - rnStartW) * 1024.0 / nColW + 0.5 ) : 0;
97 /** Calculates an object row position from a drawing layer Y position (in twips). */
98 void lclGetRowFromY(
99 ScDocument& rDoc, SCTAB nScTab, sal_uInt32& rnXclRow,
100 sal_uInt32& rnOffset, sal_uInt32 nXclStartRow, sal_uInt32 nXclMaxRow,
101 long& rnStartH, long nY, double fScale )
103 // rnStartH in conjunction with nXclStartRow is used as buffer for previously calculated height
104 long nTwipsY = static_cast< long >( nY / fScale + 0.5 );
105 long nRowH = 0;
106 bool bFound = false;
107 for( SCROW nRow = static_cast< SCROW >( nXclStartRow ); static_cast<unsigned>(nRow) <= nXclMaxRow; ++nRow )
109 nRowH = rDoc.GetRowHeight( nRow, nScTab );
110 if( rnStartH + nRowH > nTwipsY )
112 rnXclRow = static_cast< sal_uInt32 >( nRow );
113 bFound = true;
114 break;
116 rnStartH += nRowH;
118 if( !bFound )
119 rnXclRow = nXclMaxRow;
120 rnOffset = static_cast< sal_uInt32 >( nRowH ? ((nTwipsY - rnStartH) * 256.0 / nRowH + 0.5) : 0 );
123 /** Mirrors a rectangle (from LTR to RTL layout or vice versa). */
124 void lclMirrorRectangle( Rectangle& rRect )
126 long nLeft = rRect.Left();
127 rRect.Left() = -rRect.Right();
128 rRect.Right() = -nLeft;
131 sal_uInt16 lclGetEmbeddedScale( long nPageSize, sal_Int32 nPageScale, long nPos, double fPosScale )
133 return static_cast< sal_uInt16 >( nPos * fPosScale / nPageSize * nPageScale + 0.5 );
136 } // namespace
138 // ----------------------------------------------------------------------------
140 XclObjAnchor::XclObjAnchor() :
141 mnLX( 0 ),
142 mnTY( 0 ),
143 mnRX( 0 ),
144 mnBY( 0 )
148 Rectangle XclObjAnchor::GetRect( const XclRoot& rRoot, SCTAB nScTab, MapUnit eMapUnit ) const
150 ScDocument& rDoc = rRoot.GetDoc();
151 double fScale = lclGetTwipsScale( eMapUnit );
152 Rectangle aRect(
153 lclGetXFromCol( rDoc, nScTab, maFirst.mnCol, mnLX, fScale ),
154 lclGetYFromRow( rDoc, nScTab, maFirst.mnRow, mnTY, fScale ),
155 lclGetXFromCol( rDoc, nScTab, maLast.mnCol, mnRX + 1, fScale ),
156 lclGetYFromRow( rDoc, nScTab, maLast.mnRow, mnBY, fScale ) );
158 // adjust coordinates in mirrored sheets
159 if( rDoc.IsLayoutRTL( nScTab ) )
160 lclMirrorRectangle( aRect );
161 return aRect;
164 void XclObjAnchor::SetRect( const XclRoot& rRoot, SCTAB nScTab, const Rectangle& rRect, MapUnit eMapUnit )
166 ScDocument& rDoc = rRoot.GetDoc();
167 sal_uInt16 nXclMaxCol = rRoot.GetXclMaxPos().Col();
168 sal_uInt16 nXclMaxRow = static_cast<sal_uInt16>( rRoot.GetXclMaxPos().Row());
170 // adjust coordinates in mirrored sheets
171 Rectangle aRect( rRect );
172 if( rDoc.IsLayoutRTL( nScTab ) )
173 lclMirrorRectangle( aRect );
175 double fScale = lclGetTwipsScale( eMapUnit );
176 long nDummy = 0;
177 lclGetColFromX( rDoc, nScTab, maFirst.mnCol, mnLX, 0, nXclMaxCol, nDummy, aRect.Left(), fScale );
178 lclGetColFromX( rDoc, nScTab, maLast.mnCol, mnRX, maFirst.mnCol, nXclMaxCol, nDummy, aRect.Right(), fScale );
179 nDummy = 0;
180 lclGetRowFromY( rDoc, nScTab, maFirst.mnRow, mnTY, 0, nXclMaxRow, nDummy, aRect.Top(), fScale );
181 lclGetRowFromY( rDoc, nScTab, maLast.mnRow, mnBY, maFirst.mnRow, nXclMaxRow, nDummy, aRect.Bottom(), fScale );
184 void XclObjAnchor::SetRect( const Size& rPageSize, sal_Int32 nScaleX, sal_Int32 nScaleY,
185 const Rectangle& rRect, MapUnit eMapUnit, bool bDffAnchor )
187 double fScale = 1.0;
188 switch( eMapUnit )
190 case MAP_TWIP: fScale = HMM_PER_TWIPS; break; // Calc twips -> 1/100mm
191 case MAP_100TH_MM: fScale = 1.0; break; // Calc 1/100mm -> 1/100mm
192 default: OSL_FAIL( "XclObjAnchor::SetRect - map unit not implemented" );
195 /* In objects with DFF client anchor, the position of the shape is stored
196 in the cell address components of the client anchor. In old BIFF3-BIFF5
197 objects, the position is stored in the offset components of the anchor. */
198 (bDffAnchor ? maFirst.mnCol : mnLX) = lclGetEmbeddedScale( rPageSize.Width(), nScaleX, rRect.Left(), fScale );
199 (bDffAnchor ? maFirst.mnRow : mnTY) = lclGetEmbeddedScale( rPageSize.Height(), nScaleY, rRect.Top(), fScale );
200 (bDffAnchor ? maLast.mnCol : mnRX) = lclGetEmbeddedScale( rPageSize.Width(), nScaleX, rRect.Right(), fScale );
201 (bDffAnchor ? maLast.mnRow : mnBY) = lclGetEmbeddedScale( rPageSize.Height(), nScaleY, rRect.Bottom(), fScale );
203 // for safety, clear the other members
204 if( bDffAnchor )
205 mnLX = mnTY = mnRX = mnBY = 0;
206 else
207 Set( 0, 0, 0, 0 );
210 // ----------------------------------------------------------------------------
212 XclObjLineData::XclObjLineData() :
213 mnColorIdx( EXC_OBJ_LINE_AUTOCOLOR ),
214 mnStyle( EXC_OBJ_LINE_SOLID ),
215 mnWidth( EXC_OBJ_LINE_HAIR ),
216 mnAuto( EXC_OBJ_LINE_AUTO )
220 XclImpStream& operator>>( XclImpStream& rStrm, XclObjLineData& rLineData )
222 return rStrm
223 >> rLineData.mnColorIdx
224 >> rLineData.mnStyle
225 >> rLineData.mnWidth
226 >> rLineData.mnAuto;
229 // ----------------------------------------------------------------------------
231 XclObjFillData::XclObjFillData() :
232 mnBackColorIdx( EXC_OBJ_LINE_AUTOCOLOR ),
233 mnPattColorIdx( EXC_OBJ_FILL_AUTOCOLOR ),
234 mnPattern( EXC_PATT_SOLID ),
235 mnAuto( EXC_OBJ_FILL_AUTO )
239 XclImpStream& operator>>( XclImpStream& rStrm, XclObjFillData& rFillData )
241 return rStrm
242 >> rFillData.mnBackColorIdx
243 >> rFillData.mnPattColorIdx
244 >> rFillData.mnPattern
245 >> rFillData.mnAuto;
248 // ----------------------------------------------------------------------------
250 XclObjTextData::XclObjTextData() :
251 mnTextLen( 0 ),
252 mnFormatSize( 0 ),
253 mnLinkSize( 0 ),
254 mnDefFontIdx( EXC_FONT_APP ),
255 mnFlags( 0 ),
256 mnOrient( EXC_OBJ_ORIENT_NONE ),
257 mnButtonFlags( 0 ),
258 mnShortcut( 0 ),
259 mnShortcutEA( 0 )
263 void XclObjTextData::ReadObj3( XclImpStream& rStrm )
265 rStrm >> mnTextLen;
266 rStrm.Ignore( 2 );
267 rStrm >> mnFormatSize >> mnDefFontIdx;
268 rStrm.Ignore( 2 );
269 rStrm >> mnFlags >> mnOrient;
270 rStrm.Ignore( 8 );
273 void XclObjTextData::ReadObj5( XclImpStream& rStrm )
275 rStrm >> mnTextLen;
276 rStrm.Ignore( 2 );
277 rStrm >> mnFormatSize >> mnDefFontIdx;
278 rStrm.Ignore( 2 );
279 rStrm >> mnFlags >> mnOrient;
280 rStrm.Ignore( 2 );
281 rStrm >> mnLinkSize;
282 rStrm.Ignore( 2 );
283 rStrm >> mnButtonFlags >> mnShortcut >> mnShortcutEA;
286 void XclObjTextData::ReadTxo8( XclImpStream& rStrm )
288 rStrm >> mnFlags >> mnOrient >> mnButtonFlags >> mnShortcut >> mnShortcutEA >> mnTextLen >> mnFormatSize;
291 // ============================================================================
293 Reference< XControlModel > XclControlHelper::GetControlModel( Reference< XShape > xShape )
295 Reference< XControlModel > xCtrlModel;
296 Reference< XControlShape > xCtrlShape( xShape, UNO_QUERY );
297 if( xCtrlShape.is() )
298 xCtrlModel = xCtrlShape->getControl();
299 return xCtrlModel;
302 namespace {
304 static const struct
306 const sal_Char* mpcListenerType;
307 const sal_Char* mpcEventMethod;
309 spTbxListenerData[] =
311 // Attention: MUST be in order of the XclTbxEventType enum!
312 /*EXC_TBX_EVENT_ACTION*/ { "XActionListener", "actionPerformed" },
313 /*EXC_TBX_EVENT_MOUSE*/ { "XMouseListener", "mouseReleased" },
314 /*EXC_TBX_EVENT_TEXT*/ { "XTextListener", "textChanged" },
315 /*EXC_TBX_EVENT_VALUE*/ { "XAdjustmentListener", "adjustmentValueChanged" },
316 /*EXC_TBX_EVENT_CHANGE*/ { "XChangeListener", "changed" }
319 } // namespace
321 bool XclControlHelper::FillMacroDescriptor( ScriptEventDescriptor& rDescriptor,
322 XclTbxEventType eEventType, const String& rXclMacroName, SfxObjectShell* pDocShell )
324 if( rXclMacroName.Len() > 0 )
326 rDescriptor.ListenerType = OUString::createFromAscii( spTbxListenerData[ eEventType ].mpcListenerType );
327 rDescriptor.EventMethod = OUString::createFromAscii( spTbxListenerData[ eEventType ].mpcEventMethod );
328 rDescriptor.ScriptType = "Script";
329 rDescriptor.ScriptCode = XclTools::GetSbMacroUrl( rXclMacroName, pDocShell );
330 return true;
332 return false;
335 String XclControlHelper::ExtractFromMacroDescriptor(
336 const ScriptEventDescriptor& rDescriptor, XclTbxEventType eEventType, SfxObjectShell* /*pShell*/ )
338 if( (!rDescriptor.ScriptCode.isEmpty()) &&
339 rDescriptor.ScriptType.equalsIgnoreAsciiCaseAsciiL(RTL_CONSTASCII_STRINGPARAM("Script")) &&
340 rDescriptor.ListenerType.equalsAscii( spTbxListenerData[ eEventType ].mpcListenerType ) &&
341 rDescriptor.EventMethod.equalsAscii( spTbxListenerData[ eEventType ].mpcEventMethod ) )
342 return XclTools::GetXclMacroName( rDescriptor.ScriptCode );
343 return String::EmptyString();
346 // ============================================================================
348 /* vim:set shiftwidth=4 softtabstop=4 expandtab: */