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 290 : 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 290 : double fScale = 1.0;
51 290 : switch( eMapUnit )
52 : {
53 8 : case MAP_TWIP: fScale = PS_POINTS_PER_INCH / POINTS_PER_INCH; break; // Calc twips <-> real twips
54 282 : case MAP_100TH_MM: fScale = HMM_PER_TWIPS; break; // Calc twips <-> 1/100mm
55 : default: OSL_FAIL( "lclGetTwipsScale - map unit not implemented" );
56 : }
57 290 : return fScale;
58 : }
59 :
60 : /** Calculates a drawing layer X position (in twips) from an object column position. */
61 520 : long lclGetXFromCol( ScDocument& rDoc, SCTAB nScTab, sal_uInt16 nXclCol, sal_uInt16 nOffset, double fScale )
62 : {
63 520 : SCCOL nScCol = static_cast< SCCOL >( nXclCol );
64 1040 : return static_cast< long >( fScale * (rDoc.GetColOffset( nScCol, nScTab ) +
65 1040 : ::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 520 : long lclGetYFromRow( ScDocument& rDoc, SCTAB nScTab, sal_uInt16 nXclRow, sal_uInt16 nOffset, double fScale )
70 : {
71 520 : SCROW nScRow = static_cast< SCROW >( nXclRow );
72 1040 : return static_cast< long >( fScale * (rDoc.GetRowOffset( nScRow, nScTab ) +
73 1040 : ::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 60 : 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 60 : long nTwipsX = static_cast< long >( nX / fScale + 0.5 );
84 60 : long nColW = 0;
85 304 : for( rnXclCol = nXclStartCol; rnXclCol <= nXclMaxCol; ++rnXclCol )
86 : {
87 304 : nColW = rDoc.GetColWidth( static_cast< SCCOL >( rnXclCol ), nScTab );
88 304 : if( rnStartW + nColW > nTwipsX )
89 60 : break;
90 244 : rnStartW += nColW;
91 : }
92 60 : rnOffset = nColW ? static_cast< sal_uInt16 >( (nTwipsX - rnStartW) * 1024.0 / nColW + 0.5 ) : 0;
93 60 : }
94 :
95 : /** Calculates an object row position from a drawing layer Y position (in twips). */
96 60 : 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 60 : long nTwipsY = static_cast< long >( nY / fScale + 0.5 );
103 60 : long nRowH = 0;
104 60 : bool bFound = false;
105 760 : for( SCROW nRow = static_cast< SCROW >( nXclStartRow ); static_cast<unsigned>(nRow) <= nXclMaxRow; ++nRow )
106 : {
107 760 : nRowH = rDoc.GetRowHeight( nRow, nScTab );
108 760 : if( rnStartH + nRowH > nTwipsY )
109 : {
110 60 : rnXclRow = static_cast< sal_uInt32 >( nRow );
111 60 : bFound = true;
112 60 : break;
113 : }
114 700 : rnStartH += nRowH;
115 : }
116 60 : if( !bFound )
117 0 : rnXclRow = nXclMaxRow;
118 60 : rnOffset = static_cast< sal_uInt32 >( nRowH ? ((nTwipsY - rnStartH) * 256.0 / nRowH + 0.5) : 0 );
119 60 : }
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 550 : XclObjAnchor::XclObjAnchor() :
137 : mnLX( 0 ),
138 : mnTY( 0 ),
139 : mnRX( 0 ),
140 550 : mnBY( 0 )
141 : {
142 550 : }
143 :
144 260 : Rectangle XclObjAnchor::GetRect( const XclRoot& rRoot, SCTAB nScTab, MapUnit eMapUnit ) const
145 : {
146 260 : ScDocument& rDoc = rRoot.GetDoc();
147 260 : 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 260 : lclGetYFromRow( rDoc, nScTab, maLast.mnRow, mnBY, fScale ) );
153 :
154 : // adjust coordinates in mirrored sheets
155 260 : if( rDoc.IsLayoutRTL( nScTab ) )
156 0 : lclMirrorRectangle( aRect );
157 260 : return aRect;
158 : }
159 :
160 30 : void XclObjAnchor::SetRect( const XclRoot& rRoot, SCTAB nScTab, const Rectangle& rRect, MapUnit eMapUnit )
161 : {
162 30 : ScDocument& rDoc = rRoot.GetDoc();
163 30 : sal_uInt16 nXclMaxCol = rRoot.GetXclMaxPos().Col();
164 30 : sal_uInt16 nXclMaxRow = static_cast<sal_uInt16>( rRoot.GetXclMaxPos().Row());
165 :
166 : // adjust coordinates in mirrored sheets
167 30 : Rectangle aRect( rRect );
168 30 : if( rDoc.IsLayoutRTL( nScTab ) )
169 0 : lclMirrorRectangle( aRect );
170 :
171 30 : double fScale = lclGetTwipsScale( eMapUnit );
172 30 : long nDummy = 0;
173 30 : lclGetColFromX( rDoc, nScTab, maFirst.mnCol, mnLX, 0, nXclMaxCol, nDummy, aRect.Left(), fScale );
174 30 : lclGetColFromX( rDoc, nScTab, maLast.mnCol, mnRX, maFirst.mnCol, nXclMaxCol, nDummy, aRect.Right(), fScale );
175 30 : nDummy = 0;
176 30 : lclGetRowFromY( rDoc, nScTab, maFirst.mnRow, mnTY, 0, nXclMaxRow, nDummy, aRect.Top(), fScale );
177 30 : lclGetRowFromY( rDoc, nScTab, maLast.mnRow, mnBY, maFirst.mnRow, nXclMaxRow, nDummy, aRect.Bottom(), fScale );
178 30 : }
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 258 : XclObjLineData::XclObjLineData() :
207 : mnColorIdx( EXC_OBJ_LINE_AUTOCOLOR ),
208 : mnStyle( EXC_OBJ_LINE_SOLID ),
209 : mnWidth( EXC_OBJ_LINE_HAIR ),
210 258 : mnAuto( EXC_OBJ_LINE_AUTO )
211 : {
212 258 : }
213 :
214 0 : XclImpStream& operator>>( XclImpStream& rStrm, XclObjLineData& rLineData )
215 : {
216 : return rStrm
217 0 : >> rLineData.mnColorIdx
218 0 : >> rLineData.mnStyle
219 0 : >> rLineData.mnWidth
220 0 : >> rLineData.mnAuto;
221 : }
222 :
223 258 : XclObjFillData::XclObjFillData() :
224 : mnBackColorIdx( EXC_OBJ_LINE_AUTOCOLOR ),
225 : mnPattColorIdx( EXC_OBJ_FILL_AUTOCOLOR ),
226 : mnPattern( EXC_PATT_SOLID ),
227 258 : mnAuto( EXC_OBJ_FILL_AUTO )
228 : {
229 258 : }
230 :
231 0 : XclImpStream& operator>>( XclImpStream& rStrm, XclObjFillData& rFillData )
232 : {
233 : return rStrm
234 0 : >> rFillData.mnBackColorIdx
235 0 : >> rFillData.mnPattColorIdx
236 0 : >> rFillData.mnPattern
237 0 : >> rFillData.mnAuto;
238 : }
239 :
240 136 : 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 136 : mnShortcutEA( 0 )
250 : {
251 136 : }
252 :
253 0 : void XclObjTextData::ReadObj3( XclImpStream& rStrm )
254 : {
255 0 : rStrm >> mnTextLen;
256 0 : rStrm.Ignore( 2 );
257 0 : rStrm >> mnFormatSize >> mnDefFontIdx;
258 0 : rStrm.Ignore( 2 );
259 0 : rStrm >> mnFlags >> mnOrient;
260 0 : rStrm.Ignore( 8 );
261 0 : }
262 :
263 0 : void XclObjTextData::ReadObj5( XclImpStream& rStrm )
264 : {
265 0 : rStrm >> mnTextLen;
266 0 : rStrm.Ignore( 2 );
267 0 : rStrm >> mnFormatSize >> mnDefFontIdx;
268 0 : rStrm.Ignore( 2 );
269 0 : rStrm >> mnFlags >> mnOrient;
270 0 : rStrm.Ignore( 2 );
271 0 : rStrm >> mnLinkSize;
272 0 : rStrm.Ignore( 2 );
273 0 : rStrm >> mnButtonFlags >> mnShortcut >> mnShortcutEA;
274 0 : }
275 :
276 40 : void XclObjTextData::ReadTxo8( XclImpStream& rStrm )
277 : {
278 40 : rStrm >> mnFlags >> mnOrient >> mnButtonFlags >> mnShortcut >> mnShortcutEA >> mnTextLen >> mnFormatSize;
279 40 : }
280 :
281 142 : Reference< XControlModel > XclControlHelper::GetControlModel( Reference< XShape > xShape )
282 : {
283 142 : Reference< XControlModel > xCtrlModel;
284 284 : Reference< XControlShape > xCtrlShape( xShape, UNO_QUERY );
285 142 : if( xCtrlShape.is() )
286 142 : xCtrlModel = xCtrlShape->getControl();
287 284 : return xCtrlModel;
288 : }
289 :
290 : namespace {
291 :
292 : static const struct
293 : {
294 : const sal_Char* mpcListenerType;
295 : const sal_Char* mpcEventMethod;
296 : }
297 : spTbxListenerData[] =
298 : {
299 : // Attention: MUST be in order of the XclTbxEventType enum!
300 : /*EXC_TBX_EVENT_ACTION*/ { "XActionListener", "actionPerformed" },
301 : /*EXC_TBX_EVENT_MOUSE*/ { "XMouseListener", "mouseReleased" },
302 : /*EXC_TBX_EVENT_TEXT*/ { "XTextListener", "textChanged" },
303 : /*EXC_TBX_EVENT_VALUE*/ { "XAdjustmentListener", "adjustmentValueChanged" },
304 : /*EXC_TBX_EVENT_CHANGE*/ { "XChangeListener", "changed" }
305 : };
306 :
307 : } // namespace
308 :
309 12 : bool XclControlHelper::FillMacroDescriptor( ScriptEventDescriptor& rDescriptor,
310 : XclTbxEventType eEventType, const OUString& rXclMacroName, SfxObjectShell* pDocShell )
311 : {
312 12 : if( !rXclMacroName.isEmpty() )
313 : {
314 2 : rDescriptor.ListenerType = OUString::createFromAscii( spTbxListenerData[ eEventType ].mpcListenerType );
315 2 : rDescriptor.EventMethod = OUString::createFromAscii( spTbxListenerData[ eEventType ].mpcEventMethod );
316 2 : rDescriptor.ScriptType = "Script";
317 2 : rDescriptor.ScriptCode = XclTools::GetSbMacroUrl( rXclMacroName, pDocShell );
318 2 : return true;
319 : }
320 10 : return false;
321 : }
322 :
323 0 : OUString XclControlHelper::ExtractFromMacroDescriptor(
324 : const ScriptEventDescriptor& rDescriptor, XclTbxEventType eEventType, SfxObjectShell* /*pShell*/ )
325 : {
326 0 : if( (!rDescriptor.ScriptCode.isEmpty()) &&
327 0 : rDescriptor.ScriptType.equalsIgnoreAsciiCase("Script") &&
328 0 : rDescriptor.ListenerType.equalsAscii( spTbxListenerData[ eEventType ].mpcListenerType ) &&
329 0 : rDescriptor.EventMethod.equalsAscii( spTbxListenerData[ eEventType ].mpcEventMethod ) )
330 0 : return XclTools::GetXclMacroName( rDescriptor.ScriptCode );
331 0 : return OUString();
332 48 : }
333 :
334 : /* vim:set shiftwidth=4 softtabstop=4 expandtab: */
|