bump product version to 4.1.6.2
[LibreOffice.git] / sc / source / filter / excel / xlescher.cxx
blob974155aac6d4a9360d14be32f68c99537f409909
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 ::com::sun::star::uno::Reference;
33 using ::com::sun::star::uno::UNO_QUERY;
34 using ::com::sun::star::drawing::XShape;
35 using ::com::sun::star::drawing::XControlShape;
36 using ::com::sun::star::awt::XControlModel;
37 using ::com::sun::star::script::ScriptEventDescriptor;
39 namespace {
41 /** Returns the scaling factor to calculate coordinates from twips. */
42 double lclGetTwipsScale( MapUnit eMapUnit )
44 /* We cannot use OutputDevice::LogicToLogic() or the XclTools
45 conversion functions to calculate drawing layer coordinates due to
46 Calc's strange definition of a point (1 inch == 72.27 points, instead
47 of 72 points).
48 NOTE: Calc's definition changed from TeX points (72.27) to PS points
49 (72), so the MAP_TWIP case now actually also delivers a scale of 1.0
51 double fScale = 1.0;
52 switch( eMapUnit )
54 case MAP_TWIP: fScale = PS_POINTS_PER_INCH / POINTS_PER_INCH; break; // Calc twips <-> real twips
55 case MAP_100TH_MM: fScale = HMM_PER_TWIPS; break; // Calc twips <-> 1/100mm
56 default: OSL_FAIL( "lclGetTwipsScale - map unit not implemented" );
58 return fScale;
61 /** Calculates a drawing layer X position (in twips) from an object column position. */
62 long lclGetXFromCol( ScDocument& rDoc, SCTAB nScTab, sal_uInt16 nXclCol, sal_uInt16 nOffset, double fScale )
64 SCCOL nScCol = static_cast< SCCOL >( nXclCol );
65 return static_cast< long >( fScale * (rDoc.GetColOffset( nScCol, nScTab ) +
66 ::std::min( nOffset / 1024.0, 1.0 ) * rDoc.GetColWidth( nScCol, nScTab )) + 0.5 );
69 /** Calculates a drawing layer Y position (in twips) from an object row position. */
70 long lclGetYFromRow( ScDocument& rDoc, SCTAB nScTab, sal_uInt16 nXclRow, sal_uInt16 nOffset, double fScale )
72 SCROW nScRow = static_cast< SCROW >( nXclRow );
73 return static_cast< long >( fScale * (rDoc.GetRowOffset( nScRow, nScTab ) +
74 ::std::min( nOffset / 256.0, 1.0 ) * rDoc.GetRowHeight( nScRow, nScTab )) + 0.5 );
77 /** Calculates an object column position from a drawing layer X position (in twips). */
78 void lclGetColFromX(
79 ScDocument& rDoc, SCTAB nScTab, sal_uInt16& rnXclCol,
80 sal_uInt16& rnOffset, sal_uInt16 nXclStartCol, sal_uInt16 nXclMaxCol,
81 long& rnStartW, long nX, double fScale )
83 // rnStartW in conjunction with nXclStartCol is used as buffer for previously calculated width
84 long nTwipsX = static_cast< long >( nX / fScale + 0.5 );
85 long nColW = 0;
86 for( rnXclCol = nXclStartCol; rnXclCol <= nXclMaxCol; ++rnXclCol )
88 nColW = rDoc.GetColWidth( static_cast< SCCOL >( rnXclCol ), nScTab );
89 if( rnStartW + nColW > nTwipsX )
90 break;
91 rnStartW += nColW;
93 rnOffset = nColW ? static_cast< sal_uInt16 >( (nTwipsX - rnStartW) * 1024.0 / nColW + 0.5 ) : 0;
96 /** Calculates an object row position from a drawing layer Y position (in twips). */
97 void lclGetRowFromY(
98 ScDocument& rDoc, SCTAB nScTab, sal_uInt32& rnXclRow,
99 sal_uInt32& rnOffset, sal_uInt32 nXclStartRow, sal_uInt32 nXclMaxRow,
100 long& rnStartH, long nY, double fScale )
102 // rnStartH in conjunction with nXclStartRow is used as buffer for previously calculated height
103 long nTwipsY = static_cast< long >( nY / fScale + 0.5 );
104 long nRowH = 0;
105 bool bFound = false;
106 for( SCROW nRow = static_cast< SCROW >( nXclStartRow ); static_cast<unsigned>(nRow) <= nXclMaxRow; ++nRow )
108 nRowH = rDoc.GetRowHeight( nRow, nScTab );
109 if( rnStartH + nRowH > nTwipsY )
111 rnXclRow = static_cast< sal_uInt32 >( nRow );
112 bFound = true;
113 break;
115 rnStartH += nRowH;
117 if( !bFound )
118 rnXclRow = nXclMaxRow;
119 rnOffset = static_cast< sal_uInt32 >( nRowH ? ((nTwipsY - rnStartH) * 256.0 / nRowH + 0.5) : 0 );
122 /** Mirrors a rectangle (from LTR to RTL layout or vice versa). */
123 void lclMirrorRectangle( Rectangle& rRect )
125 long nLeft = rRect.Left();
126 rRect.Left() = -rRect.Right();
127 rRect.Right() = -nLeft;
130 sal_uInt16 lclGetEmbeddedScale( long nPageSize, sal_Int32 nPageScale, long nPos, double fPosScale )
132 return static_cast< sal_uInt16 >( nPos * fPosScale / nPageSize * nPageScale + 0.5 );
135 } // namespace
137 // ----------------------------------------------------------------------------
139 XclObjAnchor::XclObjAnchor() :
140 mnLX( 0 ),
141 mnTY( 0 ),
142 mnRX( 0 ),
143 mnBY( 0 )
147 Rectangle XclObjAnchor::GetRect( const XclRoot& rRoot, SCTAB nScTab, MapUnit eMapUnit ) const
149 ScDocument& rDoc = rRoot.GetDoc();
150 double fScale = lclGetTwipsScale( eMapUnit );
151 Rectangle aRect(
152 lclGetXFromCol( rDoc, nScTab, maFirst.mnCol, mnLX, fScale ),
153 lclGetYFromRow( rDoc, nScTab, maFirst.mnRow, mnTY, fScale ),
154 lclGetXFromCol( rDoc, nScTab, maLast.mnCol, mnRX + 1, fScale ),
155 lclGetYFromRow( rDoc, nScTab, maLast.mnRow, mnBY, fScale ) );
157 // adjust coordinates in mirrored sheets
158 if( rDoc.IsLayoutRTL( nScTab ) )
159 lclMirrorRectangle( aRect );
160 return aRect;
163 void XclObjAnchor::SetRect( const XclRoot& rRoot, SCTAB nScTab, const Rectangle& rRect, MapUnit eMapUnit )
165 ScDocument& rDoc = rRoot.GetDoc();
166 sal_uInt16 nXclMaxCol = rRoot.GetXclMaxPos().Col();
167 sal_uInt16 nXclMaxRow = static_cast<sal_uInt16>( rRoot.GetXclMaxPos().Row());
169 // adjust coordinates in mirrored sheets
170 Rectangle aRect( rRect );
171 if( rDoc.IsLayoutRTL( nScTab ) )
172 lclMirrorRectangle( aRect );
174 double fScale = lclGetTwipsScale( eMapUnit );
175 long nDummy = 0;
176 lclGetColFromX( rDoc, nScTab, maFirst.mnCol, mnLX, 0, nXclMaxCol, nDummy, aRect.Left(), fScale );
177 lclGetColFromX( rDoc, nScTab, maLast.mnCol, mnRX, maFirst.mnCol, nXclMaxCol, nDummy, aRect.Right(), fScale );
178 nDummy = 0;
179 lclGetRowFromY( rDoc, nScTab, maFirst.mnRow, mnTY, 0, nXclMaxRow, nDummy, aRect.Top(), fScale );
180 lclGetRowFromY( rDoc, nScTab, maLast.mnRow, mnBY, maFirst.mnRow, nXclMaxRow, nDummy, aRect.Bottom(), fScale );
183 void XclObjAnchor::SetRect( const Size& rPageSize, sal_Int32 nScaleX, sal_Int32 nScaleY,
184 const Rectangle& rRect, MapUnit eMapUnit, bool bDffAnchor )
186 double fScale = 1.0;
187 switch( eMapUnit )
189 case MAP_TWIP: fScale = HMM_PER_TWIPS; break; // Calc twips -> 1/100mm
190 case MAP_100TH_MM: fScale = 1.0; break; // Calc 1/100mm -> 1/100mm
191 default: OSL_FAIL( "XclObjAnchor::SetRect - map unit not implemented" );
194 /* In objects with DFF client anchor, the position of the shape is stored
195 in the cell address components of the client anchor. In old BIFF3-BIFF5
196 objects, the position is stored in the offset components of the anchor. */
197 (bDffAnchor ? maFirst.mnCol : mnLX) = lclGetEmbeddedScale( rPageSize.Width(), nScaleX, rRect.Left(), fScale );
198 (bDffAnchor ? maFirst.mnRow : mnTY) = lclGetEmbeddedScale( rPageSize.Height(), nScaleY, rRect.Top(), fScale );
199 (bDffAnchor ? maLast.mnCol : mnRX) = lclGetEmbeddedScale( rPageSize.Width(), nScaleX, rRect.Right(), fScale );
200 (bDffAnchor ? maLast.mnRow : mnBY) = lclGetEmbeddedScale( rPageSize.Height(), nScaleY, rRect.Bottom(), fScale );
202 // for safety, clear the other members
203 if( bDffAnchor )
204 mnLX = mnTY = mnRX = mnBY = 0;
205 else
206 Set( 0, 0, 0, 0 );
209 // ----------------------------------------------------------------------------
211 XclObjLineData::XclObjLineData() :
212 mnColorIdx( EXC_OBJ_LINE_AUTOCOLOR ),
213 mnStyle( EXC_OBJ_LINE_SOLID ),
214 mnWidth( EXC_OBJ_LINE_HAIR ),
215 mnAuto( EXC_OBJ_LINE_AUTO )
219 XclImpStream& operator>>( XclImpStream& rStrm, XclObjLineData& rLineData )
221 return rStrm
222 >> rLineData.mnColorIdx
223 >> rLineData.mnStyle
224 >> rLineData.mnWidth
225 >> rLineData.mnAuto;
228 // ----------------------------------------------------------------------------
230 XclObjFillData::XclObjFillData() :
231 mnBackColorIdx( EXC_OBJ_LINE_AUTOCOLOR ),
232 mnPattColorIdx( EXC_OBJ_FILL_AUTOCOLOR ),
233 mnPattern( EXC_PATT_SOLID ),
234 mnAuto( EXC_OBJ_FILL_AUTO )
238 XclImpStream& operator>>( XclImpStream& rStrm, XclObjFillData& rFillData )
240 return rStrm
241 >> rFillData.mnBackColorIdx
242 >> rFillData.mnPattColorIdx
243 >> rFillData.mnPattern
244 >> rFillData.mnAuto;
247 // ----------------------------------------------------------------------------
249 XclObjTextData::XclObjTextData() :
250 mnTextLen( 0 ),
251 mnFormatSize( 0 ),
252 mnLinkSize( 0 ),
253 mnDefFontIdx( EXC_FONT_APP ),
254 mnFlags( 0 ),
255 mnOrient( EXC_OBJ_ORIENT_NONE ),
256 mnButtonFlags( 0 ),
257 mnShortcut( 0 ),
258 mnShortcutEA( 0 )
262 void XclObjTextData::ReadObj3( XclImpStream& rStrm )
264 rStrm >> mnTextLen;
265 rStrm.Ignore( 2 );
266 rStrm >> mnFormatSize >> mnDefFontIdx;
267 rStrm.Ignore( 2 );
268 rStrm >> mnFlags >> mnOrient;
269 rStrm.Ignore( 8 );
272 void XclObjTextData::ReadObj5( XclImpStream& rStrm )
274 rStrm >> mnTextLen;
275 rStrm.Ignore( 2 );
276 rStrm >> mnFormatSize >> mnDefFontIdx;
277 rStrm.Ignore( 2 );
278 rStrm >> mnFlags >> mnOrient;
279 rStrm.Ignore( 2 );
280 rStrm >> mnLinkSize;
281 rStrm.Ignore( 2 );
282 rStrm >> mnButtonFlags >> mnShortcut >> mnShortcutEA;
285 void XclObjTextData::ReadTxo8( XclImpStream& rStrm )
287 rStrm >> mnFlags >> mnOrient >> mnButtonFlags >> mnShortcut >> mnShortcutEA >> mnTextLen >> mnFormatSize;
290 // ============================================================================
292 Reference< XControlModel > XclControlHelper::GetControlModel( Reference< XShape > xShape )
294 Reference< XControlModel > xCtrlModel;
295 Reference< XControlShape > xCtrlShape( xShape, UNO_QUERY );
296 if( xCtrlShape.is() )
297 xCtrlModel = xCtrlShape->getControl();
298 return xCtrlModel;
301 namespace {
303 static const struct
305 const sal_Char* mpcListenerType;
306 const sal_Char* mpcEventMethod;
308 spTbxListenerData[] =
310 // Attention: MUST be in order of the XclTbxEventType enum!
311 /*EXC_TBX_EVENT_ACTION*/ { "XActionListener", "actionPerformed" },
312 /*EXC_TBX_EVENT_MOUSE*/ { "XMouseListener", "mouseReleased" },
313 /*EXC_TBX_EVENT_TEXT*/ { "XTextListener", "textChanged" },
314 /*EXC_TBX_EVENT_VALUE*/ { "XAdjustmentListener", "adjustmentValueChanged" },
315 /*EXC_TBX_EVENT_CHANGE*/ { "XChangeListener", "changed" }
318 } // namespace
320 bool XclControlHelper::FillMacroDescriptor( ScriptEventDescriptor& rDescriptor,
321 XclTbxEventType eEventType, const String& rXclMacroName, SfxObjectShell* pDocShell )
323 if( rXclMacroName.Len() > 0 )
325 rDescriptor.ListenerType = OUString::createFromAscii( spTbxListenerData[ eEventType ].mpcListenerType );
326 rDescriptor.EventMethod = OUString::createFromAscii( spTbxListenerData[ eEventType ].mpcEventMethod );
327 rDescriptor.ScriptType = "Script";
328 rDescriptor.ScriptCode = XclTools::GetSbMacroUrl( rXclMacroName, pDocShell );
329 return true;
331 return false;
334 String XclControlHelper::ExtractFromMacroDescriptor(
335 const ScriptEventDescriptor& rDescriptor, XclTbxEventType eEventType, SfxObjectShell* /*pShell*/ )
337 if( (!rDescriptor.ScriptCode.isEmpty()) &&
338 rDescriptor.ScriptType.equalsIgnoreAsciiCase("Script") &&
339 rDescriptor.ListenerType.equalsAscii( spTbxListenerData[ eEventType ].mpcListenerType ) &&
340 rDescriptor.EventMethod.equalsAscii( spTbxListenerData[ eEventType ].mpcEventMethod ) )
341 return XclTools::GetXclMacroName( rDescriptor.ScriptCode );
342 return String::EmptyString();
345 // ============================================================================
347 /* vim:set shiftwidth=4 softtabstop=4 expandtab: */