LibreOffice Module sc (master)  1
xlescher.cxx
Go to the documentation of this file.
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 <sal/config.h>
21 
22 #include <algorithm>
23 
24 #include <xlescher.hxx>
25 
26 #include <com/sun/star/drawing/XControlShape.hpp>
27 #include <com/sun/star/script/ScriptEventDescriptor.hpp>
28 #include <document.hxx>
29 #include <xistream.hxx>
30 #include <xlroot.hxx>
31 #include <xltools.hxx>
32 
33 using ::com::sun::star::uno::Reference;
34 using ::com::sun::star::uno::UNO_QUERY;
35 using ::com::sun::star::drawing::XShape;
36 using ::com::sun::star::drawing::XControlShape;
37 using ::com::sun::star::awt::XControlModel;
38 using ::com::sun::star::script::ScriptEventDescriptor;
39 
40 namespace {
41 
43 double lclGetTwipsScale( MapUnit eMapUnit )
44 {
45  /* We cannot use OutputDevice::LogicToLogic() or the XclTools
46  conversion functions to calculate drawing layer coordinates due to
47  Calc's strange definition of a point (1 inch == 72.27 points, instead
48  of 72 points).
49  NOTE: Calc's definition changed from TeX points (72.27) to PS points
50  (72), so the MapUnit::MapTwip case now actually also delivers a scale of 1.0
51  */
52  double fScale = 1.0;
53  switch( eMapUnit )
54  {
55  case MapUnit::MapTwip: fScale = 1; break; // Calc twips <-> real twips
56  case MapUnit::Map100thMM: fScale = HMM_PER_TWIPS; break; // Calc twips <-> 1/100mm
57  default: OSL_FAIL( "lclGetTwipsScale - map unit not implemented" );
58  }
59  return fScale;
60 }
61 
63 tools::Long lclGetXFromCol( const ScDocument& rDoc, SCTAB nScTab, sal_uInt16 nXclCol, sal_uInt16 nOffset, double fScale )
64 {
65  SCCOL nScCol = static_cast< SCCOL >( nXclCol );
66  return static_cast< tools::Long >( fScale * (rDoc.GetColOffset( nScCol, nScTab ) +
67  ::std::min( nOffset / 1024.0, 1.0 ) * rDoc.GetColWidth( nScCol, nScTab )) + 0.5 );
68 }
69 
71 tools::Long lclGetYFromRow( const ScDocument& rDoc, SCTAB nScTab, sal_uInt16 nXclRow, sal_uInt16 nOffset, double fScale )
72 {
73  SCROW nScRow = static_cast< SCROW >( nXclRow );
74  return static_cast< tools::Long >( fScale * (rDoc.GetRowOffset( nScRow, nScTab ) +
75  ::std::min( nOffset / 256.0, 1.0 ) * rDoc.GetRowHeight( nScRow, nScTab )) + 0.5 );
76 }
77 
79 void lclGetColFromX(
80  const ScDocument& rDoc, SCTAB nScTab, sal_uInt16& rnXclCol,
81  sal_uInt16& rnOffset, sal_uInt16 nXclStartCol, sal_uInt16 nXclMaxCol,
82  tools::Long& rnStartW, tools::Long nX, double fScale )
83 {
84  // rnStartW in conjunction with nXclStartCol is used as buffer for previously calculated width
85  tools::Long nTwipsX = static_cast< tools::Long >( nX / fScale + 0.5 );
86  tools::Long nColW = 0;
87  for( rnXclCol = nXclStartCol; rnXclCol <= nXclMaxCol; ++rnXclCol )
88  {
89  nColW = rDoc.GetColWidth( static_cast< SCCOL >( rnXclCol ), nScTab );
90  if( rnStartW + nColW > nTwipsX )
91  break;
92  rnStartW += nColW;
93  }
94  rnOffset = nColW ? static_cast< sal_uInt16 >( (nTwipsX - rnStartW) * 1024.0 / nColW + 0.5 ) : 0;
95 }
96 
98 void lclGetRowFromY(
99  const ScDocument& rDoc, SCTAB nScTab, sal_uInt32& rnXclRow,
100  sal_uInt32& rnOffset, sal_uInt32 nXclStartRow, sal_uInt32 nXclMaxRow,
101  tools::Long& rnStartH, tools::Long nY, double fScale )
102 {
103  // rnStartH in conjunction with nXclStartRow is used as buffer for previously calculated height
104  tools::Long nTwipsY = static_cast< tools::Long >( nY / fScale + 0.5 );
105  tools::Long nRowH = 0;
106  bool bFound = false;
107  for( sal_uInt32 nRow = nXclStartRow; nRow <= nXclMaxRow; ++nRow )
108  {
109  nRowH = rDoc.GetRowHeight( nRow, nScTab );
110  if( rnStartH + nRowH > nTwipsY )
111  {
112  rnXclRow = nRow;
113  bFound = true;
114  break;
115  }
116  rnStartH += nRowH;
117  }
118  if( !bFound )
119  rnXclRow = nXclMaxRow;
120  rnOffset = static_cast< sal_uInt32 >( nRowH ? std::max((nTwipsY - rnStartH) * 256.0 / nRowH + 0.5, 0.0) : 0 );
121 }
122 
124 void lclMirrorRectangle( tools::Rectangle& rRect )
125 {
126  tools::Long nLeft = rRect.Left();
127  rRect.SetLeft( -rRect.Right() );
128  rRect.SetRight( -nLeft );
129 }
130 
131 sal_uInt16 lclGetEmbeddedScale( tools::Long nPageSize, sal_Int32 nPageScale, tools::Long nPos, double fPosScale )
132 {
133  return static_cast< sal_uInt16 >( nPos * fPosScale / nPageSize * nPageScale + 0.5 );
134 }
135 
136 } // namespace
137 
139  mnLX( 0 ),
140  mnTY( 0 ),
141  mnRX( 0 ),
142  mnBY( 0 )
143 {
144 }
145 
146 tools::Rectangle XclObjAnchor::GetRect( const XclRoot& rRoot, SCTAB nScTab, MapUnit eMapUnit ) const
147 {
148  ScDocument& rDoc = rRoot.GetDoc();
149  double fScale = lclGetTwipsScale( eMapUnit );
150  tools::Rectangle aRect(
151  lclGetXFromCol(rDoc, nScTab, std::min<SCCOL>(maFirst.mnCol, rDoc.MaxCol()), mnLX, fScale),
152  lclGetYFromRow(rDoc, nScTab, std::min<SCROW>(maFirst.mnRow, rDoc.MaxRow()), mnTY, fScale),
153  lclGetXFromCol(rDoc, nScTab, std::min<SCCOL>(maLast.mnCol, rDoc.MaxCol()), mnRX + 1, fScale),
154  lclGetYFromRow(rDoc, nScTab, std::min<SCROW>(maLast.mnRow, rDoc.MaxRow()), mnBY, fScale));
155 
156  // adjust coordinates in mirrored sheets
157  if( rDoc.IsLayoutRTL( nScTab ) )
158  lclMirrorRectangle( aRect );
159  return aRect;
160 }
161 
162 void XclObjAnchor::SetRect( const XclRoot& rRoot, SCTAB nScTab, const tools::Rectangle& rRect, MapUnit eMapUnit )
163 {
164  ScDocument& rDoc = rRoot.GetDoc();
165  sal_uInt16 nXclMaxCol = rRoot.GetXclMaxPos().Col();
166  sal_uInt16 nXclMaxRow = static_cast<sal_uInt16>( rRoot.GetXclMaxPos().Row());
167 
168  // adjust coordinates in mirrored sheets
169  tools::Rectangle aRect( rRect );
170  if( rDoc.IsLayoutRTL( nScTab ) )
171  lclMirrorRectangle( aRect );
172 
173  double fScale = lclGetTwipsScale( eMapUnit );
174  tools::Long nDummy = 0;
175  lclGetColFromX( rDoc, nScTab, maFirst.mnCol, mnLX, 0, nXclMaxCol, nDummy, aRect.Left(), fScale );
176  lclGetColFromX( rDoc, nScTab, maLast.mnCol, mnRX, maFirst.mnCol, nXclMaxCol, nDummy, aRect.Right(), fScale );
177  nDummy = 0;
178  lclGetRowFromY( rDoc, nScTab, maFirst.mnRow, mnTY, 0, nXclMaxRow, nDummy, aRect.Top(), fScale );
179  lclGetRowFromY( rDoc, nScTab, maLast.mnRow, mnBY, maFirst.mnRow, nXclMaxRow, nDummy, aRect.Bottom(), fScale );
180 }
181 
182 void XclObjAnchor::SetRect( const Size& rPageSize, sal_Int32 nScaleX, sal_Int32 nScaleY,
183  const tools::Rectangle& rRect, MapUnit eMapUnit )
184 {
185  double fScale = 1.0;
186  switch( eMapUnit )
187  {
188  case MapUnit::MapTwip: fScale = HMM_PER_TWIPS; break; // Calc twips -> 1/100mm
189  case MapUnit::Map100thMM: fScale = 1.0; break; // Calc 1/100mm -> 1/100mm
190  default: OSL_FAIL( "XclObjAnchor::SetRect - map unit not implemented" );
191  }
192 
193  /* In objects with DFF client anchor, the position of the shape is stored
194  in the cell address components of the client anchor. In old BIFF3-BIFF5
195  objects, the position is stored in the offset components of the anchor. */
196  maFirst.mnCol = lclGetEmbeddedScale( rPageSize.Width(), nScaleX, rRect.Left(), fScale );
197  maFirst.mnRow = lclGetEmbeddedScale( rPageSize.Height(), nScaleY, rRect.Top(), fScale );
198  maLast.mnCol = lclGetEmbeddedScale( rPageSize.Width(), nScaleX, rRect.Right(), fScale );
199  maLast.mnRow = lclGetEmbeddedScale( rPageSize.Height(), nScaleY, rRect.Bottom(), fScale );
200 
201  // for safety, clear the other members
202  mnLX = mnTY = mnRX = mnBY = 0;
203 }
204 
206  mnColorIdx( EXC_OBJ_LINE_AUTOCOLOR ),
207  mnStyle( EXC_OBJ_LINE_SOLID ),
209  mnAuto( EXC_OBJ_LINE_AUTO )
210 {
211 }
212 
214 {
215  rLineData.mnColorIdx = rStrm.ReaduInt8();
216  rLineData.mnStyle = rStrm.ReaduInt8();
217  rLineData.mnWidth = rStrm.ReaduInt8();
218  rLineData.mnAuto = rStrm.ReaduInt8();
219  return rStrm;
220 }
221 
223  mnBackColorIdx( EXC_OBJ_LINE_AUTOCOLOR ),
224  mnPattColorIdx( EXC_OBJ_FILL_AUTOCOLOR ),
225  mnPattern( EXC_PATT_SOLID ),
226  mnAuto( EXC_OBJ_FILL_AUTO )
227 {
228 }
229 
231 {
232  rFillData.mnBackColorIdx = rStrm.ReaduInt8();
233  rFillData.mnPattColorIdx = rStrm.ReaduInt8();
234  rFillData.mnPattern = rStrm.ReaduInt8();
235  rFillData.mnAuto = rStrm.ReaduInt8();
236  return rStrm;
237 }
238 
240  mnTextLen( 0 ),
241  mnFormatSize( 0 ),
242  mnLinkSize( 0 ),
243  mnDefFontIdx( EXC_FONT_APP ),
244  mnFlags( 0 ),
245  mnOrient( EXC_OBJ_ORIENT_NONE ),
246  mnButtonFlags( 0 ),
247  mnShortcut( 0 ),
248  mnShortcutEA( 0 )
249 {
250 }
251 
253 {
254  mnTextLen = rStrm.ReaduInt16();
255  rStrm.Ignore( 2 );
256  mnFormatSize = rStrm.ReaduInt16();
257  mnDefFontIdx = rStrm.ReaduInt16();
258  rStrm.Ignore( 2 );
259  mnFlags = rStrm.ReaduInt16();
260  mnOrient = rStrm.ReaduInt16();
261  rStrm.Ignore( 8 );
262 }
263 
265 {
266  mnTextLen = rStrm.ReaduInt16();
267  rStrm.Ignore( 2 );
268  mnFormatSize = rStrm.ReaduInt16();
269  mnDefFontIdx = rStrm.ReaduInt16();
270  rStrm.Ignore( 2 );
271  mnFlags = rStrm.ReaduInt16();
272  mnOrient = rStrm.ReaduInt16();
273  rStrm.Ignore( 2 );
274  mnLinkSize = rStrm.ReaduInt16();
275  rStrm.Ignore( 2 );
276  mnButtonFlags = rStrm.ReaduInt16();
277  mnShortcut = rStrm.ReaduInt16();
278  mnShortcutEA = rStrm.ReaduInt16();
279 }
280 
282 {
283  mnFlags = rStrm.ReaduInt16();
284  mnOrient = rStrm.ReaduInt16();
285  mnButtonFlags = rStrm.ReaduInt16();
286  mnShortcut = rStrm.ReaduInt16();
287  mnShortcutEA = rStrm.ReaduInt16();
288  mnTextLen = rStrm.ReaduInt16();
289  mnFormatSize = rStrm.ReaduInt16();
290 }
291 
292 Reference< XControlModel > XclControlHelper::GetControlModel( Reference< XShape > const & xShape )
293 {
294  Reference< XControlModel > xCtrlModel;
295  Reference< XControlShape > xCtrlShape( xShape, UNO_QUERY );
296  if( xCtrlShape.is() )
297  xCtrlModel = xCtrlShape->getControl();
298  return xCtrlModel;
299 }
300 
301 namespace {
302 
303 const struct
304 {
305  const char* mpcListenerType;
306  const 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 bool XclControlHelper::FillMacroDescriptor( ScriptEventDescriptor& rDescriptor,
321  XclTbxEventType eEventType, const OUString& rXclMacroName, SfxObjectShell* pDocShell )
322 {
323  if( !rXclMacroName.isEmpty() )
324  {
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 );
329  return true;
330  }
331  return false;
332 }
333 
335  const ScriptEventDescriptor& rDescriptor, XclTbxEventType eEventType )
336 {
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 OUString();
343 }
344 
345 /* vim:set shiftwidth=4 softtabstop=4 expandtab: */
XclImpStream & operator>>(XclImpStream &rStrm, XclObjLineData &rLineData)
Definition: xlescher.cxx:213
const sal_uInt8 EXC_OBJ_FILL_AUTOCOLOR
Definition: xlescher.hxx:115
sal_uInt16 mnFormatSize
Definition: xlescher.hxx:384
SCROW Row() const
Definition: address.hxx:262
sal_uInt16 mnOrient
Definition: xlescher.hxx:388
sal_uInt8 mnPattern
Definition: xlescher.hxx:370
const sal_uInt16 EXC_OBJ_ORIENT_NONE
Definition: xlescher.hxx:133
sal_uInt16 mnRX
Y offset in top row (1/256 of row height).
Definition: xlescher.hxx:288
long Long
sal_uInt8 mnColorIdx
Definition: xlescher.hxx:353
static bool FillMacroDescriptor(css::script::ScriptEventDescriptor &rDescriptor, XclTbxEventType eEventType, const OUString &rXclMacroName, SfxObjectShell *pDocShell)
Fills the macro descriptor according to the passed macro name.
Definition: xlescher.cxx:320
XclAddress maLast
Definition: xladdress.hxx:61
sal_uInt32 mnRow
Definition: xladdress.hxx:33
XclAddress maFirst
Definition: xladdress.hxx:60
void ReadObj3(XclImpStream &rStrm)
Reads text data from a BIFF3/BIFF4 OBJ record.
Definition: xlescher.cxx:252
ScDocument & GetDoc() const
Returns reference to the destination document (import) or source document (export).
Definition: xlroot.cxx:276
void ReadObj5(XclImpStream &rStrm)
Reads text data from a BIFF5 OBJ record.
Definition: xlescher.cxx:264
void SetRight(tools::Long v)
SC_DLLPUBLIC sal_uInt16 GetRowHeight(SCROW nRow, SCTAB nTab, bool bHiddenAsZero=true) const
Definition: document.cxx:4161
sal_uInt8 mnWidth
Definition: xlescher.hxx:355
FuncFlags mnFlags
Information about all parameters.
const sal_uInt16 EXC_FONT_APP
Definition: xlstyle.hxx:77
tools::Long Left() const
void SetLeft(tools::Long v)
SC_DLLPUBLIC SCROW MaxRow() const
Definition: document.hxx:873
sal_uInt8 mnBackColorIdx
Definition: xlescher.hxx:368
tools::Rectangle GetRect(const XclRoot &rRoot, SCTAB nScTab, MapUnit eMapUnit) const
Calculates a rectangle from the contained coordinates.
Definition: xlescher.cxx:146
XclTbxEventType
Definition: xlescher.hxx:406
static OUString GetXclMacroName(const OUString &rSbMacroUrl)
Returns the Excel macro name from a full StarBasic macro URL.
Definition: xltools.cxx:712
tools::Long Bottom() const
#define HMM_PER_TWIPS
Definition: global.hxx:92
const sal_uInt8 EXC_OBJ_LINE_SOLID
Definition: xlescher.hxx:82
sal_uInt16 mnFlags
Definition: xlescher.hxx:387
sal_uInt32 mnBY
X offset in right column (1/1024 of column width).
Definition: xlescher.hxx:289
sal_uInt16 mnDefFontIdx
Definition: xlescher.hxx:386
SC_DLLPUBLIC SCCOL MaxCol() const
Definition: document.hxx:872
static OUString ExtractFromMacroDescriptor(const css::script::ScriptEventDescriptor &rDescriptor, XclTbxEventType eEventType)
Tries to extract an Excel macro name from the passed macro descriptor.
Definition: xlescher.cxx:334
sal_uInt16 mnLX
Definition: xlescher.hxx:286
sal_uInt8 mnAuto
Definition: xlescher.hxx:371
void ReadTxo8(XclImpStream &rStrm)
Reads text data from a BIFF8 TXO record.
Definition: xlescher.cxx:281
sal_Int16 SCCOL
Definition: types.hxx:22
tools::Long Width() const
sal_uInt8 mnAuto
Definition: xlescher.hxx:356
const sal_uInt8 EXC_OBJ_LINE_HAIR
Definition: xlescher.hxx:92
sal_uInt16 mnLinkSize
Definition: xlescher.hxx:385
sal_uInt8 mnStyle
Definition: xlescher.hxx:354
sal_uInt16 mnCol
Definition: xladdress.hxx:32
tools::Long Top() const
sal_uInt16 ReaduInt16()
Definition: xistream.cxx:648
SCCOL Col() const
Definition: address.hxx:267
sal_Int32 SCROW
Definition: types.hxx:18
static OUString GetSbMacroUrl(const OUString &rMacroName, SfxObjectShell *pDocShell)
Returns the full StarBasic macro URL from an Excel macro name.
Definition: xltools.cxx:703
const sal_uInt8 EXC_PATT_SOLID
Definition: xlstyle.hxx:56
const sal_uInt8 EXC_OBJ_FILL_AUTO
Definition: xlescher.hxx:117
void Ignore(std::size_t nBytes)
Seeks forward inside the current record.
Definition: xistream.cxx:795
sal_uInt16 mnButtonFlags
Definition: xlescher.hxx:389
sal_uInt8 ReaduInt8()
Definition: xistream.cxx:616
double mnWidth
const ScAddress & GetXclMaxPos() const
Returns the highest possible cell address in an Excel document (using current BIFF version)...
Definition: xlroot.hxx:245
tools::Long Height() const
const sal_uInt8 EXC_OBJ_LINE_AUTOCOLOR
Definition: xlescher.hxx:80
sal_uInt32 mnTY
X offset in left column (1/1024 of column width).
Definition: xlescher.hxx:287
sal_uInt16 mnShortcutEA
Definition: xlescher.hxx:391
sal_uInt16 mnShortcut
Definition: xlescher.hxx:390
SC_DLLPUBLIC bool IsLayoutRTL(SCTAB nTab) const
Definition: document.cxx:994
sal_uInt8 mnPattColorIdx
Definition: xlescher.hxx:369
MapUnit
This class is used to import record oriented streams.
Definition: xistream.hxx:278
XclObjAnchor()
Y offset in bottom row (1/256 of row height).
Definition: xlescher.cxx:138
SC_DLLPUBLIC sal_uInt16 GetColWidth(SCCOL nCol, SCTAB nTab, bool bHiddenAsZero=true) const
Definition: document.cxx:4120
const sal_uInt8 EXC_OBJ_LINE_AUTO
Definition: xlescher.hxx:97
Access to global data for a filter object (imported or exported document) from other classes...
Definition: xlroot.hxx:127
sal_uInt16 mnTextLen
Definition: xlescher.hxx:383
tools::Long Right() const
SC_DLLPUBLIC sal_uLong GetColOffset(SCCOL nCol, SCTAB nTab, bool bHiddenAsZero=true) const
Definition: document.cxx:4224
sal_Int16 SCTAB
Definition: types.hxx:23
SC_DLLPUBLIC sal_uLong GetRowOffset(SCROW nRow, SCTAB nTab, bool bHiddenAsZero=true) const
Definition: document.cxx:4232
void SetRect(const XclRoot &rRoot, SCTAB nScTab, const tools::Rectangle &rRect, MapUnit eMapUnit)
Initializes the anchor coordinates for a sheet.
Definition: xlescher.cxx:162
static css::uno::Reference< css::awt::XControlModel > GetControlModel(css::uno::Reference< css::drawing::XShape > const &xShape)
Returns the API control model from the passed API shape object.
Definition: xlescher.cxx:292