1 /* -*- Mode: C++; tab-width: 4; indent-tabs-mode: nil; c-basic-offset: 4 -*- */
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 .
20 #include "xlescher.hxx"
22 #include <com/sun/star/drawing/XControlShape.hpp>
23 #include <com/sun/star/script/ScriptEventDescriptor.hpp>
24 #include <svx/unoapi.hxx>
25 #include "document.hxx"
26 #include "xestream.hxx"
27 #include "xistream.hxx"
29 #include "xltools.hxx"
31 using ::com::sun::star::uno::Reference
;
32 using ::com::sun::star::uno::UNO_QUERY
;
33 using ::com::sun::star::drawing::XShape
;
34 using ::com::sun::star::drawing::XControlShape
;
35 using ::com::sun::star::awt::XControlModel
;
36 using ::com::sun::star::script::ScriptEventDescriptor
;
40 /** Returns the scaling factor to calculate coordinates from twips. */
41 double lclGetTwipsScale( MapUnit eMapUnit
)
43 /* We cannot use OutputDevice::LogicToLogic() or the XclTools
44 conversion functions to calculate drawing layer coordinates due to
45 Calc's strange definition of a point (1 inch == 72.27 points, instead
47 NOTE: Calc's definition changed from TeX points (72.27) to PS points
48 (72), so the MAP_TWIP case now actually also delivers a scale of 1.0
53 case MAP_TWIP
: fScale
= PS_POINTS_PER_INCH
/ POINTS_PER_INCH
; break; // Calc twips <-> real twips
54 case MAP_100TH_MM
: fScale
= HMM_PER_TWIPS
; break; // Calc twips <-> 1/100mm
55 default: OSL_FAIL( "lclGetTwipsScale - map unit not implemented" );
60 /** Calculates a drawing layer X position (in twips) from an object column position. */
61 long lclGetXFromCol( ScDocument
& rDoc
, SCTAB nScTab
, sal_uInt16 nXclCol
, sal_uInt16 nOffset
, double fScale
)
63 SCCOL nScCol
= static_cast< SCCOL
>( nXclCol
);
64 return static_cast< long >( fScale
* (rDoc
.GetColOffset( nScCol
, nScTab
) +
65 ::std::min( nOffset
/ 1024.0, 1.0 ) * rDoc
.GetColWidth( nScCol
, nScTab
)) + 0.5 );
68 /** Calculates a drawing layer Y position (in twips) from an object row position. */
69 long lclGetYFromRow( ScDocument
& rDoc
, SCTAB nScTab
, sal_uInt16 nXclRow
, sal_uInt16 nOffset
, double fScale
)
71 SCROW nScRow
= static_cast< SCROW
>( nXclRow
);
72 return static_cast< long >( fScale
* (rDoc
.GetRowOffset( nScRow
, nScTab
) +
73 ::std::min( nOffset
/ 256.0, 1.0 ) * rDoc
.GetRowHeight( nScRow
, nScTab
)) + 0.5 );
76 /** Calculates an object column position from a drawing layer X position (in twips). */
78 ScDocument
& rDoc
, SCTAB nScTab
, sal_uInt16
& rnXclCol
,
79 sal_uInt16
& rnOffset
, sal_uInt16 nXclStartCol
, sal_uInt16 nXclMaxCol
,
80 long& rnStartW
, long nX
, double fScale
)
82 // rnStartW in conjunction with nXclStartCol is used as buffer for previously calculated width
83 long nTwipsX
= static_cast< long >( nX
/ fScale
+ 0.5 );
85 for( rnXclCol
= nXclStartCol
; rnXclCol
<= nXclMaxCol
; ++rnXclCol
)
87 nColW
= rDoc
.GetColWidth( static_cast< SCCOL
>( rnXclCol
), nScTab
);
88 if( rnStartW
+ nColW
> nTwipsX
)
92 rnOffset
= nColW
? static_cast< sal_uInt16
>( (nTwipsX
- rnStartW
) * 1024.0 / nColW
+ 0.5 ) : 0;
95 /** Calculates an object row position from a drawing layer Y position (in twips). */
97 ScDocument
& rDoc
, SCTAB nScTab
, sal_uInt32
& rnXclRow
,
98 sal_uInt32
& rnOffset
, sal_uInt32 nXclStartRow
, sal_uInt32 nXclMaxRow
,
99 long& rnStartH
, long nY
, double fScale
)
101 // rnStartH in conjunction with nXclStartRow is used as buffer for previously calculated height
102 long nTwipsY
= static_cast< long >( nY
/ fScale
+ 0.5 );
105 for( SCROW nRow
= static_cast< SCROW
>( nXclStartRow
); static_cast<unsigned>(nRow
) <= nXclMaxRow
; ++nRow
)
107 nRowH
= rDoc
.GetRowHeight( nRow
, nScTab
);
108 if( rnStartH
+ nRowH
> nTwipsY
)
110 rnXclRow
= static_cast< sal_uInt32
>( nRow
);
117 rnXclRow
= nXclMaxRow
;
118 rnOffset
= static_cast< sal_uInt32
>( nRowH
? ((nTwipsY
- rnStartH
) * 256.0 / nRowH
+ 0.5) : 0 );
121 /** Mirrors a rectangle (from LTR to RTL layout or vice versa). */
122 void lclMirrorRectangle( Rectangle
& rRect
)
124 long nLeft
= rRect
.Left();
125 rRect
.Left() = -rRect
.Right();
126 rRect
.Right() = -nLeft
;
129 sal_uInt16
lclGetEmbeddedScale( long nPageSize
, sal_Int32 nPageScale
, long nPos
, double fPosScale
)
131 return static_cast< sal_uInt16
>( nPos
* fPosScale
/ nPageSize
* nPageScale
+ 0.5 );
136 XclObjAnchor::XclObjAnchor() :
144 Rectangle
XclObjAnchor::GetRect( const XclRoot
& rRoot
, SCTAB nScTab
, MapUnit eMapUnit
) const
146 ScDocument
& rDoc
= rRoot
.GetDoc();
147 double fScale
= lclGetTwipsScale( eMapUnit
);
149 lclGetXFromCol( rDoc
, nScTab
, maFirst
.mnCol
, mnLX
, fScale
),
150 lclGetYFromRow( rDoc
, nScTab
, maFirst
.mnRow
, mnTY
, fScale
),
151 lclGetXFromCol( rDoc
, nScTab
, maLast
.mnCol
, mnRX
+ 1, fScale
),
152 lclGetYFromRow( rDoc
, nScTab
, maLast
.mnRow
, mnBY
, fScale
) );
154 // adjust coordinates in mirrored sheets
155 if( rDoc
.IsLayoutRTL( nScTab
) )
156 lclMirrorRectangle( aRect
);
160 void XclObjAnchor::SetRect( const XclRoot
& rRoot
, SCTAB nScTab
, const Rectangle
& rRect
, MapUnit eMapUnit
)
162 ScDocument
& rDoc
= rRoot
.GetDoc();
163 sal_uInt16 nXclMaxCol
= rRoot
.GetXclMaxPos().Col();
164 sal_uInt16 nXclMaxRow
= static_cast<sal_uInt16
>( rRoot
.GetXclMaxPos().Row());
166 // adjust coordinates in mirrored sheets
167 Rectangle
aRect( rRect
);
168 if( rDoc
.IsLayoutRTL( nScTab
) )
169 lclMirrorRectangle( aRect
);
171 double fScale
= lclGetTwipsScale( eMapUnit
);
173 lclGetColFromX( rDoc
, nScTab
, maFirst
.mnCol
, mnLX
, 0, nXclMaxCol
, nDummy
, aRect
.Left(), fScale
);
174 lclGetColFromX( rDoc
, nScTab
, maLast
.mnCol
, mnRX
, maFirst
.mnCol
, nXclMaxCol
, nDummy
, aRect
.Right(), fScale
);
176 lclGetRowFromY( rDoc
, nScTab
, maFirst
.mnRow
, mnTY
, 0, nXclMaxRow
, nDummy
, aRect
.Top(), fScale
);
177 lclGetRowFromY( rDoc
, nScTab
, maLast
.mnRow
, mnBY
, maFirst
.mnRow
, nXclMaxRow
, nDummy
, aRect
.Bottom(), fScale
);
180 void XclObjAnchor::SetRect( const Size
& rPageSize
, sal_Int32 nScaleX
, sal_Int32 nScaleY
,
181 const Rectangle
& rRect
, MapUnit eMapUnit
, bool bDffAnchor
)
186 case MAP_TWIP
: fScale
= HMM_PER_TWIPS
; break; // Calc twips -> 1/100mm
187 case MAP_100TH_MM
: fScale
= 1.0; break; // Calc 1/100mm -> 1/100mm
188 default: OSL_FAIL( "XclObjAnchor::SetRect - map unit not implemented" );
191 /* In objects with DFF client anchor, the position of the shape is stored
192 in the cell address components of the client anchor. In old BIFF3-BIFF5
193 objects, the position is stored in the offset components of the anchor. */
194 (bDffAnchor
? maFirst
.mnCol
: mnLX
) = lclGetEmbeddedScale( rPageSize
.Width(), nScaleX
, rRect
.Left(), fScale
);
195 (bDffAnchor
? maFirst
.mnRow
: mnTY
) = lclGetEmbeddedScale( rPageSize
.Height(), nScaleY
, rRect
.Top(), fScale
);
196 (bDffAnchor
? maLast
.mnCol
: mnRX
) = lclGetEmbeddedScale( rPageSize
.Width(), nScaleX
, rRect
.Right(), fScale
);
197 (bDffAnchor
? maLast
.mnRow
: mnBY
) = lclGetEmbeddedScale( rPageSize
.Height(), nScaleY
, rRect
.Bottom(), fScale
);
199 // for safety, clear the other members
201 mnLX
= mnTY
= mnRX
= mnBY
= 0;
206 XclObjLineData::XclObjLineData() :
207 mnColorIdx( EXC_OBJ_LINE_AUTOCOLOR
),
208 mnStyle( EXC_OBJ_LINE_SOLID
),
209 mnWidth( EXC_OBJ_LINE_HAIR
),
210 mnAuto( EXC_OBJ_LINE_AUTO
)
214 XclImpStream
& operator>>( XclImpStream
& rStrm
, XclObjLineData
& rLineData
)
216 rLineData
.mnColorIdx
= rStrm
.ReaduInt8();
217 rLineData
.mnStyle
= rStrm
.ReaduInt8();
218 rLineData
.mnWidth
= rStrm
.ReaduInt8();
219 rLineData
.mnAuto
= rStrm
.ReaduInt8();
223 XclObjFillData::XclObjFillData() :
224 mnBackColorIdx( EXC_OBJ_LINE_AUTOCOLOR
),
225 mnPattColorIdx( EXC_OBJ_FILL_AUTOCOLOR
),
226 mnPattern( EXC_PATT_SOLID
),
227 mnAuto( EXC_OBJ_FILL_AUTO
)
231 XclImpStream
& operator>>( XclImpStream
& rStrm
, XclObjFillData
& rFillData
)
233 rFillData
.mnBackColorIdx
= rStrm
.ReaduInt8();
234 rFillData
.mnPattColorIdx
= rStrm
.ReaduInt8();
235 rFillData
.mnPattern
= rStrm
.ReaduInt8();
236 rFillData
.mnAuto
= rStrm
.ReaduInt8();
240 XclObjTextData::XclObjTextData() :
244 mnDefFontIdx( EXC_FONT_APP
),
246 mnOrient( EXC_OBJ_ORIENT_NONE
),
253 void XclObjTextData::ReadObj3( XclImpStream
& rStrm
)
255 mnTextLen
= rStrm
.ReaduInt16();
257 mnFormatSize
= rStrm
.ReaduInt16();
258 mnDefFontIdx
= rStrm
.ReaduInt16();
260 mnFlags
= rStrm
.ReaduInt16();
261 mnOrient
= rStrm
.ReaduInt16();
265 void XclObjTextData::ReadObj5( XclImpStream
& rStrm
)
267 mnTextLen
= rStrm
.ReaduInt16();
269 mnFormatSize
= rStrm
.ReaduInt16();
270 mnDefFontIdx
= rStrm
.ReaduInt16();
272 mnFlags
= rStrm
.ReaduInt16();
273 mnOrient
= rStrm
.ReaduInt16();
275 mnLinkSize
= rStrm
.ReaduInt16();
277 mnButtonFlags
= rStrm
.ReaduInt16();
278 mnShortcut
= rStrm
.ReaduInt16();
279 mnShortcutEA
= rStrm
.ReaduInt16();
282 void XclObjTextData::ReadTxo8( XclImpStream
& rStrm
)
284 mnFlags
= rStrm
.ReaduInt16();
285 mnOrient
= rStrm
.ReaduInt16();
286 mnButtonFlags
= rStrm
.ReaduInt16();
287 mnShortcut
= rStrm
.ReaduInt16();
288 mnShortcutEA
= rStrm
.ReaduInt16();
289 mnTextLen
= rStrm
.ReaduInt16();
290 mnFormatSize
= rStrm
.ReaduInt16();
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();
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" }
321 bool XclControlHelper::FillMacroDescriptor( ScriptEventDescriptor
& rDescriptor
,
322 XclTbxEventType eEventType
, const OUString
& rXclMacroName
, SfxObjectShell
* pDocShell
)
324 if( !rXclMacroName
.isEmpty() )
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
);
335 OUString
XclControlHelper::ExtractFromMacroDescriptor(
336 const ScriptEventDescriptor
& rDescriptor
, XclTbxEventType eEventType
, SfxObjectShell
* /*pShell*/ )
338 if( (!rDescriptor
.ScriptCode
.isEmpty()) &&
339 rDescriptor
.ScriptType
.equalsIgnoreAsciiCase("Script") &&
340 rDescriptor
.ListenerType
.equalsAscii( spTbxListenerData
[ eEventType
].mpcListenerType
) &&
341 rDescriptor
.EventMethod
.equalsAscii( spTbxListenerData
[ eEventType
].mpcEventMethod
) )
342 return XclTools::GetXclMacroName( rDescriptor
.ScriptCode
);
346 /* vim:set shiftwidth=4 softtabstop=4 expandtab: */