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