Line data Source code
1 : /* -*- Mode: C++; tab-width: 4; indent-tabs-mode: nil; c-basic-offset: 4 -*- */
2 : /*
3 : * This file is part of the LibreOffice project.
4 : *
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/.
8 : *
9 : * This file incorporates work covered by the following license notice:
10 : *
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 .
18 : */
19 :
20 : #include "xlescher.hxx"
21 :
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"
28 : #include "xlroot.hxx"
29 : #include "xltools.hxx"
30 :
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;
37 :
38 : namespace {
39 :
40 : /** Returns the scaling factor to calculate coordinates from twips. */
41 195 : double lclGetTwipsScale( MapUnit eMapUnit )
42 : {
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
46 : of 72 points).
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
49 : */
50 195 : double fScale = 1.0;
51 195 : switch( eMapUnit )
52 : {
53 29 : case MAP_TWIP: fScale = PS_POINTS_PER_INCH / POINTS_PER_INCH; break; // Calc twips <-> real twips
54 166 : case MAP_100TH_MM: fScale = HMM_PER_TWIPS; break; // Calc twips <-> 1/100mm
55 : default: OSL_FAIL( "lclGetTwipsScale - map unit not implemented" );
56 : }
57 195 : return fScale;
58 : }
59 :
60 : /** Calculates a drawing layer X position (in twips) from an object column position. */
61 274 : long lclGetXFromCol( ScDocument& rDoc, SCTAB nScTab, sal_uInt16 nXclCol, sal_uInt16 nOffset, double fScale )
62 : {
63 274 : SCCOL nScCol = static_cast< SCCOL >( nXclCol );
64 548 : return static_cast< long >( fScale * (rDoc.GetColOffset( nScCol, nScTab ) +
65 548 : ::std::min( nOffset / 1024.0, 1.0 ) * rDoc.GetColWidth( nScCol, nScTab )) + 0.5 );
66 : }
67 :
68 : /** Calculates a drawing layer Y position (in twips) from an object row position. */
69 274 : long lclGetYFromRow( ScDocument& rDoc, SCTAB nScTab, sal_uInt16 nXclRow, sal_uInt16 nOffset, double fScale )
70 : {
71 274 : SCROW nScRow = static_cast< SCROW >( nXclRow );
72 548 : return static_cast< long >( fScale * (rDoc.GetRowOffset( nScRow, nScTab ) +
73 548 : ::std::min( nOffset / 256.0, 1.0 ) * rDoc.GetRowHeight( nScRow, nScTab )) + 0.5 );
74 : }
75 :
76 : /** Calculates an object column position from a drawing layer X position (in twips). */
77 116 : void lclGetColFromX(
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 )
81 : {
82 : // rnStartW in conjunction with nXclStartCol is used as buffer for previously calculated width
83 116 : long nTwipsX = static_cast< long >( nX / fScale + 0.5 );
84 116 : long nColW = 0;
85 775 : for( rnXclCol = nXclStartCol; rnXclCol <= nXclMaxCol; ++rnXclCol )
86 : {
87 775 : nColW = rDoc.GetColWidth( static_cast< SCCOL >( rnXclCol ), nScTab );
88 775 : if( rnStartW + nColW > nTwipsX )
89 116 : break;
90 659 : rnStartW += nColW;
91 : }
92 116 : rnOffset = nColW ? static_cast< sal_uInt16 >( (nTwipsX - rnStartW) * 1024.0 / nColW + 0.5 ) : 0;
93 116 : }
94 :
95 : /** Calculates an object row position from a drawing layer Y position (in twips). */
96 116 : void lclGetRowFromY(
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 )
100 : {
101 : // rnStartH in conjunction with nXclStartRow is used as buffer for previously calculated height
102 116 : long nTwipsY = static_cast< long >( nY / fScale + 0.5 );
103 116 : long nRowH = 0;
104 116 : bool bFound = false;
105 1540 : for( SCROW nRow = static_cast< SCROW >( nXclStartRow ); static_cast<unsigned>(nRow) <= nXclMaxRow; ++nRow )
106 : {
107 1540 : nRowH = rDoc.GetRowHeight( nRow, nScTab );
108 1540 : if( rnStartH + nRowH > nTwipsY )
109 : {
110 116 : rnXclRow = static_cast< sal_uInt32 >( nRow );
111 116 : bFound = true;
112 116 : break;
113 : }
114 1424 : rnStartH += nRowH;
115 : }
116 116 : if( !bFound )
117 0 : rnXclRow = nXclMaxRow;
118 116 : rnOffset = static_cast< sal_uInt32 >( nRowH ? ((nTwipsY - rnStartH) * 256.0 / nRowH + 0.5) : 0 );
119 116 : }
120 :
121 : /** Mirrors a rectangle (from LTR to RTL layout or vice versa). */
122 0 : void lclMirrorRectangle( Rectangle& rRect )
123 : {
124 0 : long nLeft = rRect.Left();
125 0 : rRect.Left() = -rRect.Right();
126 0 : rRect.Right() = -nLeft;
127 0 : }
128 :
129 0 : sal_uInt16 lclGetEmbeddedScale( long nPageSize, sal_Int32 nPageScale, long nPos, double fPosScale )
130 : {
131 0 : return static_cast< sal_uInt16 >( nPos * fPosScale / nPageSize * nPageScale + 0.5 );
132 : }
133 :
134 : } // namespace
135 :
136 330 : XclObjAnchor::XclObjAnchor() :
137 : mnLX( 0 ),
138 : mnTY( 0 ),
139 : mnRX( 0 ),
140 330 : mnBY( 0 )
141 : {
142 330 : }
143 :
144 137 : Rectangle XclObjAnchor::GetRect( const XclRoot& rRoot, SCTAB nScTab, MapUnit eMapUnit ) const
145 : {
146 137 : ScDocument& rDoc = rRoot.GetDoc();
147 137 : double fScale = lclGetTwipsScale( eMapUnit );
148 : Rectangle aRect(
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 137 : lclGetYFromRow( rDoc, nScTab, maLast.mnRow, mnBY, fScale ) );
153 :
154 : // adjust coordinates in mirrored sheets
155 137 : if( rDoc.IsLayoutRTL( nScTab ) )
156 0 : lclMirrorRectangle( aRect );
157 137 : return aRect;
158 : }
159 :
160 58 : void XclObjAnchor::SetRect( const XclRoot& rRoot, SCTAB nScTab, const Rectangle& rRect, MapUnit eMapUnit )
161 : {
162 58 : ScDocument& rDoc = rRoot.GetDoc();
163 58 : sal_uInt16 nXclMaxCol = rRoot.GetXclMaxPos().Col();
164 58 : sal_uInt16 nXclMaxRow = static_cast<sal_uInt16>( rRoot.GetXclMaxPos().Row());
165 :
166 : // adjust coordinates in mirrored sheets
167 58 : Rectangle aRect( rRect );
168 58 : if( rDoc.IsLayoutRTL( nScTab ) )
169 0 : lclMirrorRectangle( aRect );
170 :
171 58 : double fScale = lclGetTwipsScale( eMapUnit );
172 58 : long nDummy = 0;
173 58 : lclGetColFromX( rDoc, nScTab, maFirst.mnCol, mnLX, 0, nXclMaxCol, nDummy, aRect.Left(), fScale );
174 58 : lclGetColFromX( rDoc, nScTab, maLast.mnCol, mnRX, maFirst.mnCol, nXclMaxCol, nDummy, aRect.Right(), fScale );
175 58 : nDummy = 0;
176 58 : lclGetRowFromY( rDoc, nScTab, maFirst.mnRow, mnTY, 0, nXclMaxRow, nDummy, aRect.Top(), fScale );
177 58 : lclGetRowFromY( rDoc, nScTab, maLast.mnRow, mnBY, maFirst.mnRow, nXclMaxRow, nDummy, aRect.Bottom(), fScale );
178 58 : }
179 :
180 0 : void XclObjAnchor::SetRect( const Size& rPageSize, sal_Int32 nScaleX, sal_Int32 nScaleY,
181 : const Rectangle& rRect, MapUnit eMapUnit, bool bDffAnchor )
182 : {
183 0 : double fScale = 1.0;
184 0 : switch( eMapUnit )
185 : {
186 0 : case MAP_TWIP: fScale = HMM_PER_TWIPS; break; // Calc twips -> 1/100mm
187 0 : case MAP_100TH_MM: fScale = 1.0; break; // Calc 1/100mm -> 1/100mm
188 : default: OSL_FAIL( "XclObjAnchor::SetRect - map unit not implemented" );
189 : }
190 :
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 0 : (bDffAnchor ? maFirst.mnCol : mnLX) = lclGetEmbeddedScale( rPageSize.Width(), nScaleX, rRect.Left(), fScale );
195 0 : (bDffAnchor ? maFirst.mnRow : mnTY) = lclGetEmbeddedScale( rPageSize.Height(), nScaleY, rRect.Top(), fScale );
196 0 : (bDffAnchor ? maLast.mnCol : mnRX) = lclGetEmbeddedScale( rPageSize.Width(), nScaleX, rRect.Right(), fScale );
197 0 : (bDffAnchor ? maLast.mnRow : mnBY) = lclGetEmbeddedScale( rPageSize.Height(), nScaleY, rRect.Bottom(), fScale );
198 :
199 : // for safety, clear the other members
200 0 : if( bDffAnchor )
201 0 : mnLX = mnTY = mnRX = mnBY = 0;
202 : else
203 0 : Set( 0, 0, 0, 0 );
204 0 : }
205 :
206 136 : XclObjLineData::XclObjLineData() :
207 : mnColorIdx( EXC_OBJ_LINE_AUTOCOLOR ),
208 : mnStyle( EXC_OBJ_LINE_SOLID ),
209 : mnWidth( EXC_OBJ_LINE_HAIR ),
210 136 : mnAuto( EXC_OBJ_LINE_AUTO )
211 : {
212 136 : }
213 :
214 2 : XclImpStream& operator>>( XclImpStream& rStrm, XclObjLineData& rLineData )
215 : {
216 2 : rLineData.mnColorIdx = rStrm.ReaduInt8();
217 2 : rLineData.mnStyle = rStrm.ReaduInt8();
218 2 : rLineData.mnWidth = rStrm.ReaduInt8();
219 2 : rLineData.mnAuto = rStrm.ReaduInt8();
220 2 : return rStrm;
221 : }
222 :
223 136 : XclObjFillData::XclObjFillData() :
224 : mnBackColorIdx( EXC_OBJ_LINE_AUTOCOLOR ),
225 : mnPattColorIdx( EXC_OBJ_FILL_AUTOCOLOR ),
226 : mnPattern( EXC_PATT_SOLID ),
227 136 : mnAuto( EXC_OBJ_FILL_AUTO )
228 : {
229 136 : }
230 :
231 2 : XclImpStream& operator>>( XclImpStream& rStrm, XclObjFillData& rFillData )
232 : {
233 2 : rFillData.mnBackColorIdx = rStrm.ReaduInt8();
234 2 : rFillData.mnPattColorIdx = rStrm.ReaduInt8();
235 2 : rFillData.mnPattern = rStrm.ReaduInt8();
236 2 : rFillData.mnAuto = rStrm.ReaduInt8();
237 2 : return rStrm;
238 : }
239 :
240 68 : XclObjTextData::XclObjTextData() :
241 : mnTextLen( 0 ),
242 : mnFormatSize( 0 ),
243 : mnLinkSize( 0 ),
244 : mnDefFontIdx( EXC_FONT_APP ),
245 : mnFlags( 0 ),
246 : mnOrient( EXC_OBJ_ORIENT_NONE ),
247 : mnButtonFlags( 0 ),
248 : mnShortcut( 0 ),
249 68 : mnShortcutEA( 0 )
250 : {
251 68 : }
252 :
253 0 : void XclObjTextData::ReadObj3( XclImpStream& rStrm )
254 : {
255 0 : mnTextLen = rStrm.ReaduInt16();
256 0 : rStrm.Ignore( 2 );
257 0 : mnFormatSize = rStrm.ReaduInt16();
258 0 : mnDefFontIdx = rStrm.ReaduInt16();
259 0 : rStrm.Ignore( 2 );
260 0 : mnFlags = rStrm.ReaduInt16();
261 0 : mnOrient = rStrm.ReaduInt16();
262 0 : rStrm.Ignore( 8 );
263 0 : }
264 :
265 0 : void XclObjTextData::ReadObj5( XclImpStream& rStrm )
266 : {
267 0 : mnTextLen = rStrm.ReaduInt16();
268 0 : rStrm.Ignore( 2 );
269 0 : mnFormatSize = rStrm.ReaduInt16();
270 0 : mnDefFontIdx = rStrm.ReaduInt16();
271 0 : rStrm.Ignore( 2 );
272 0 : mnFlags = rStrm.ReaduInt16();
273 0 : mnOrient = rStrm.ReaduInt16();
274 0 : rStrm.Ignore( 2 );
275 0 : mnLinkSize = rStrm.ReaduInt16();
276 0 : rStrm.Ignore( 2 );
277 0 : mnButtonFlags = rStrm.ReaduInt16();
278 0 : mnShortcut = rStrm.ReaduInt16();
279 0 : mnShortcutEA = rStrm.ReaduInt16();
280 0 : }
281 :
282 20 : void XclObjTextData::ReadTxo8( XclImpStream& rStrm )
283 : {
284 20 : mnFlags = rStrm.ReaduInt16();
285 20 : mnOrient = rStrm.ReaduInt16();
286 20 : mnButtonFlags = rStrm.ReaduInt16();
287 20 : mnShortcut = rStrm.ReaduInt16();
288 20 : mnShortcutEA = rStrm.ReaduInt16();
289 20 : mnTextLen = rStrm.ReaduInt16();
290 20 : mnFormatSize = rStrm.ReaduInt16();
291 20 : }
292 :
293 71 : Reference< XControlModel > XclControlHelper::GetControlModel( Reference< XShape > xShape )
294 : {
295 71 : Reference< XControlModel > xCtrlModel;
296 142 : Reference< XControlShape > xCtrlShape( xShape, UNO_QUERY );
297 71 : if( xCtrlShape.is() )
298 71 : xCtrlModel = xCtrlShape->getControl();
299 142 : return xCtrlModel;
300 : }
301 :
302 : namespace {
303 :
304 : static const struct
305 : {
306 : const sal_Char* mpcListenerType;
307 : const sal_Char* mpcEventMethod;
308 : }
309 : spTbxListenerData[] =
310 : {
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" }
317 : };
318 :
319 : } // namespace
320 :
321 6 : bool XclControlHelper::FillMacroDescriptor( ScriptEventDescriptor& rDescriptor,
322 : XclTbxEventType eEventType, const OUString& rXclMacroName, SfxObjectShell* pDocShell )
323 : {
324 6 : if( !rXclMacroName.isEmpty() )
325 : {
326 1 : rDescriptor.ListenerType = OUString::createFromAscii( spTbxListenerData[ eEventType ].mpcListenerType );
327 1 : rDescriptor.EventMethod = OUString::createFromAscii( spTbxListenerData[ eEventType ].mpcEventMethod );
328 1 : rDescriptor.ScriptType = "Script";
329 1 : rDescriptor.ScriptCode = XclTools::GetSbMacroUrl( rXclMacroName, pDocShell );
330 1 : return true;
331 : }
332 5 : return false;
333 : }
334 :
335 0 : OUString XclControlHelper::ExtractFromMacroDescriptor(
336 : const ScriptEventDescriptor& rDescriptor, XclTbxEventType eEventType, SfxObjectShell* /*pShell*/ )
337 : {
338 0 : if( (!rDescriptor.ScriptCode.isEmpty()) &&
339 0 : rDescriptor.ScriptType.equalsIgnoreAsciiCase("Script") &&
340 0 : rDescriptor.ListenerType.equalsAscii( spTbxListenerData[ eEventType ].mpcListenerType ) &&
341 0 : rDescriptor.EventMethod.equalsAscii( spTbxListenerData[ eEventType ].mpcEventMethod ) )
342 0 : return XclTools::GetXclMacroName( rDescriptor.ScriptCode );
343 0 : return OUString();
344 30 : }
345 :
346 : /* vim:set shiftwidth=4 softtabstop=4 expandtab: */
|