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 .
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"
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
;
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
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
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" );
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). */
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 );
86 for( rnXclCol
= nXclStartCol
; rnXclCol
<= nXclMaxCol
; ++rnXclCol
)
88 nColW
= rDoc
.GetColWidth( static_cast< SCCOL
>( rnXclCol
), nScTab
);
89 if( rnStartW
+ nColW
> nTwipsX
)
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). */
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 );
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
);
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 );
137 // ----------------------------------------------------------------------------
139 XclObjAnchor::XclObjAnchor() :
147 Rectangle
XclObjAnchor::GetRect( const XclRoot
& rRoot
, SCTAB nScTab
, MapUnit eMapUnit
) const
149 ScDocument
& rDoc
= rRoot
.GetDoc();
150 double fScale
= lclGetTwipsScale( eMapUnit
);
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
);
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
);
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
);
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
)
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
204 mnLX
= mnTY
= mnRX
= mnBY
= 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
)
222 >> rLineData
.mnColorIdx
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
)
241 >> rFillData
.mnBackColorIdx
242 >> rFillData
.mnPattColorIdx
243 >> rFillData
.mnPattern
247 // ----------------------------------------------------------------------------
249 XclObjTextData::XclObjTextData() :
253 mnDefFontIdx( EXC_FONT_APP
),
255 mnOrient( EXC_OBJ_ORIENT_NONE
),
262 void XclObjTextData::ReadObj3( XclImpStream
& rStrm
)
266 rStrm
>> mnFormatSize
>> mnDefFontIdx
;
268 rStrm
>> mnFlags
>> mnOrient
;
272 void XclObjTextData::ReadObj5( XclImpStream
& rStrm
)
276 rStrm
>> mnFormatSize
>> mnDefFontIdx
;
278 rStrm
>> mnFlags
>> mnOrient
;
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();
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" }
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
);
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: */