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 <tools/UnitConversion.hxx>
29 #include <document.hxx>
30 #include <xistream.hxx>
31 #include <xlroot.hxx>
32 #include <xltools.hxx>
33 
34 using ::com::sun::star::uno::Reference;
35 using ::com::sun::star::uno::UNO_QUERY;
36 using ::com::sun::star::drawing::XShape;
37 using ::com::sun::star::drawing::XControlShape;
38 using ::com::sun::star::awt::XControlModel;
39 using ::com::sun::star::script::ScriptEventDescriptor;
40 
41 namespace {
42 
44 double lclGetTwipsScale( MapUnit eMapUnit )
45 {
46  double fScale = 1.0;
47  if (const auto eTo = MapToO3tlLength(eMapUnit); eTo != o3tl::Length::invalid)
48  fScale = o3tl::convert(1.0, o3tl::Length::twip, eTo);
49  else
50  OSL_FAIL("lclGetTwipsScale - map unit not implemented");
51  return fScale;
52 }
53 
55 tools::Long lclGetXFromCol( const ScDocument& rDoc, SCTAB nScTab, sal_uInt16 nXclCol, sal_uInt16 nOffset, double fScale )
56 {
57  SCCOL nScCol = static_cast< SCCOL >( nXclCol );
58  return static_cast< tools::Long >( fScale * (rDoc.GetColOffset( nScCol, nScTab ) +
59  ::std::min( nOffset / 1024.0, 1.0 ) * rDoc.GetColWidth( nScCol, nScTab )) + 0.5 );
60 }
61 
63 tools::Long lclGetYFromRow( const ScDocument& rDoc, SCTAB nScTab, sal_uInt16 nXclRow, sal_uInt16 nOffset, double fScale )
64 {
65  SCROW nScRow = static_cast< SCROW >( nXclRow );
66  return static_cast< tools::Long >( fScale * (rDoc.GetRowOffset( nScRow, nScTab ) +
67  ::std::min( nOffset / 256.0, 1.0 ) * rDoc.GetRowHeight( nScRow, nScTab )) + 0.5 );
68 }
69 
71 void lclGetColFromX(
72  const ScDocument& rDoc, SCTAB nScTab, sal_uInt16& rnXclCol,
73  sal_uInt16& rnOffset, sal_uInt16 nXclStartCol, sal_uInt16 nXclMaxCol,
74  tools::Long& rnStartW, tools::Long nX, double fScale )
75 {
76  // rnStartW in conjunction with nXclStartCol is used as buffer for previously calculated width
77  tools::Long nTwipsX = static_cast< tools::Long >( nX / fScale + 0.5 );
78  tools::Long nColW = 0;
79  for( rnXclCol = nXclStartCol; rnXclCol <= nXclMaxCol; ++rnXclCol )
80  {
81  nColW = rDoc.GetColWidth( static_cast< SCCOL >( rnXclCol ), nScTab );
82  if( rnStartW + nColW > nTwipsX )
83  break;
84  rnStartW += nColW;
85  }
86  rnOffset = nColW ? static_cast< sal_uInt16 >( (nTwipsX - rnStartW) * 1024.0 / nColW + 0.5 ) : 0;
87 }
88 
90 void lclGetRowFromY(
91  const ScDocument& rDoc, SCTAB nScTab, sal_uInt32& rnXclRow,
92  sal_uInt32& rnOffset, sal_uInt32 nXclStartRow, sal_uInt32 nXclMaxRow,
93  tools::Long& rnStartH, tools::Long nY, double fScale )
94 {
95  // rnStartH in conjunction with nXclStartRow is used as buffer for previously calculated height
96  tools::Long nTwipsY = static_cast< tools::Long >( nY / fScale + 0.5 );
97  tools::Long nRowH = 0;
98  bool bFound = false;
99  for( sal_uInt32 nRow = nXclStartRow; nRow <= nXclMaxRow; ++nRow )
100  {
101  nRowH = rDoc.GetRowHeight( nRow, nScTab );
102  if( rnStartH + nRowH > nTwipsY )
103  {
104  rnXclRow = nRow;
105  bFound = true;
106  break;
107  }
108  rnStartH += nRowH;
109  }
110  if( !bFound )
111  rnXclRow = nXclMaxRow;
112  rnOffset = static_cast< sal_uInt32 >( nRowH ? std::max((nTwipsY - rnStartH) * 256.0 / nRowH + 0.5, 0.0) : 0 );
113 }
114 
116 void lclMirrorRectangle( tools::Rectangle& rRect )
117 {
118  tools::Long nLeft = rRect.Left();
119  rRect.SetLeft( -rRect.Right() );
120  rRect.SetRight( -nLeft );
121 }
122 
123 sal_uInt16 lclGetEmbeddedScale( tools::Long nPageSize, sal_Int32 nPageScale, tools::Long nPos, double fPosScale )
124 {
125  return static_cast< sal_uInt16 >( nPos * fPosScale / nPageSize * nPageScale + 0.5 );
126 }
127 
128 } // namespace
129 
131  mnLX( 0 ),
132  mnTY( 0 ),
133  mnRX( 0 ),
134  mnBY( 0 )
135 {
136 }
137 
138 tools::Rectangle XclObjAnchor::GetRect( const XclRoot& rRoot, SCTAB nScTab, MapUnit eMapUnit ) const
139 {
140  ScDocument& rDoc = rRoot.GetDoc();
141  double fScale = lclGetTwipsScale( eMapUnit );
142  tools::Rectangle aRect(
143  lclGetXFromCol(rDoc, nScTab, std::min<SCCOL>(maFirst.mnCol, rDoc.MaxCol()), mnLX, fScale),
144  lclGetYFromRow(rDoc, nScTab, std::min<SCROW>(maFirst.mnRow, rDoc.MaxRow()), mnTY, fScale),
145  lclGetXFromCol(rDoc, nScTab, std::min<SCCOL>(maLast.mnCol, rDoc.MaxCol()), mnRX + 1, fScale),
146  lclGetYFromRow(rDoc, nScTab, std::min<SCROW>(maLast.mnRow, rDoc.MaxRow()), mnBY, fScale));
147 
148  // adjust coordinates in mirrored sheets
149  if( rDoc.IsLayoutRTL( nScTab ) )
150  lclMirrorRectangle( aRect );
151  return aRect;
152 }
153 
154 void XclObjAnchor::SetRect( const XclRoot& rRoot, SCTAB nScTab, const tools::Rectangle& rRect, MapUnit eMapUnit )
155 {
156  ScDocument& rDoc = rRoot.GetDoc();
157  sal_uInt16 nXclMaxCol = rRoot.GetXclMaxPos().Col();
158  sal_uInt16 nXclMaxRow = static_cast<sal_uInt16>( rRoot.GetXclMaxPos().Row());
159 
160  // adjust coordinates in mirrored sheets
161  tools::Rectangle aRect( rRect );
162  if( rDoc.IsLayoutRTL( nScTab ) )
163  lclMirrorRectangle( aRect );
164 
165  double fScale = lclGetTwipsScale( eMapUnit );
166  tools::Long nDummy = 0;
167  lclGetColFromX( rDoc, nScTab, maFirst.mnCol, mnLX, 0, nXclMaxCol, nDummy, aRect.Left(), fScale );
168  lclGetColFromX( rDoc, nScTab, maLast.mnCol, mnRX, maFirst.mnCol, nXclMaxCol, nDummy, aRect.Right(), fScale );
169  nDummy = 0;
170  lclGetRowFromY( rDoc, nScTab, maFirst.mnRow, mnTY, 0, nXclMaxRow, nDummy, aRect.Top(), fScale );
171  lclGetRowFromY( rDoc, nScTab, maLast.mnRow, mnBY, maFirst.mnRow, nXclMaxRow, nDummy, aRect.Bottom(), fScale );
172 }
173 
174 void XclObjAnchor::SetRect( const Size& rPageSize, sal_Int32 nScaleX, sal_Int32 nScaleY,
175  const tools::Rectangle& rRect, MapUnit eMapUnit )
176 {
177  double fScale = 1.0;
178  if (const auto eFrom = MapToO3tlLength(eMapUnit); eFrom != o3tl::Length::invalid)
179  fScale = o3tl::convert(1.0, eFrom, o3tl::Length::mm100);
180  else
181  OSL_FAIL("XclObjAnchor::SetRect - map unit not implemented");
182 
183  /* In objects with DFF client anchor, the position of the shape is stored
184  in the cell address components of the client anchor. In old BIFF3-BIFF5
185  objects, the position is stored in the offset components of the anchor. */
186  maFirst.mnCol = lclGetEmbeddedScale( rPageSize.Width(), nScaleX, rRect.Left(), fScale );
187  maFirst.mnRow = lclGetEmbeddedScale( rPageSize.Height(), nScaleY, rRect.Top(), fScale );
188  maLast.mnCol = lclGetEmbeddedScale( rPageSize.Width(), nScaleX, rRect.Right(), fScale );
189  maLast.mnRow = lclGetEmbeddedScale( rPageSize.Height(), nScaleY, rRect.Bottom(), fScale );
190 
191  // for safety, clear the other members
192  mnLX = mnTY = mnRX = mnBY = 0;
193 }
194 
196  mnColorIdx( EXC_OBJ_LINE_AUTOCOLOR ),
197  mnStyle( EXC_OBJ_LINE_SOLID ),
199  mnAuto( EXC_OBJ_LINE_AUTO )
200 {
201 }
202 
204 {
205  rLineData.mnColorIdx = rStrm.ReaduInt8();
206  rLineData.mnStyle = rStrm.ReaduInt8();
207  rLineData.mnWidth = rStrm.ReaduInt8();
208  rLineData.mnAuto = rStrm.ReaduInt8();
209  return rStrm;
210 }
211 
213  mnBackColorIdx( EXC_OBJ_LINE_AUTOCOLOR ),
214  mnPattColorIdx( EXC_OBJ_FILL_AUTOCOLOR ),
215  mnPattern( EXC_PATT_SOLID ),
216  mnAuto( EXC_OBJ_FILL_AUTO )
217 {
218 }
219 
221 {
222  rFillData.mnBackColorIdx = rStrm.ReaduInt8();
223  rFillData.mnPattColorIdx = rStrm.ReaduInt8();
224  rFillData.mnPattern = rStrm.ReaduInt8();
225  rFillData.mnAuto = rStrm.ReaduInt8();
226  return rStrm;
227 }
228 
230  mnTextLen( 0 ),
231  mnFormatSize( 0 ),
232  mnLinkSize( 0 ),
233  mnDefFontIdx( EXC_FONT_APP ),
234  mnFlags( 0 ),
235  mnOrient( EXC_OBJ_ORIENT_NONE ),
236  mnButtonFlags( 0 ),
237  mnShortcut( 0 ),
238  mnShortcutEA( 0 )
239 {
240 }
241 
243 {
244  mnTextLen = rStrm.ReaduInt16();
245  rStrm.Ignore( 2 );
246  mnFormatSize = rStrm.ReaduInt16();
247  mnDefFontIdx = rStrm.ReaduInt16();
248  rStrm.Ignore( 2 );
249  mnFlags = rStrm.ReaduInt16();
250  mnOrient = rStrm.ReaduInt16();
251  rStrm.Ignore( 8 );
252 }
253 
255 {
256  mnTextLen = rStrm.ReaduInt16();
257  rStrm.Ignore( 2 );
258  mnFormatSize = rStrm.ReaduInt16();
259  mnDefFontIdx = rStrm.ReaduInt16();
260  rStrm.Ignore( 2 );
261  mnFlags = rStrm.ReaduInt16();
262  mnOrient = rStrm.ReaduInt16();
263  rStrm.Ignore( 2 );
264  mnLinkSize = rStrm.ReaduInt16();
265  rStrm.Ignore( 2 );
266  mnButtonFlags = rStrm.ReaduInt16();
267  mnShortcut = rStrm.ReaduInt16();
268  mnShortcutEA = rStrm.ReaduInt16();
269 }
270 
272 {
273  mnFlags = rStrm.ReaduInt16();
274  mnOrient = rStrm.ReaduInt16();
275  mnButtonFlags = rStrm.ReaduInt16();
276  mnShortcut = rStrm.ReaduInt16();
277  mnShortcutEA = rStrm.ReaduInt16();
278  mnTextLen = rStrm.ReaduInt16();
279  mnFormatSize = rStrm.ReaduInt16();
280 }
281 
282 Reference< XControlModel > XclControlHelper::GetControlModel( Reference< XShape > const & xShape )
283 {
284  Reference< XControlModel > xCtrlModel;
285  Reference< XControlShape > xCtrlShape( xShape, UNO_QUERY );
286  if( xCtrlShape.is() )
287  xCtrlModel = xCtrlShape->getControl();
288  return xCtrlModel;
289 }
290 
291 namespace {
292 
293 const struct
294 {
295  const char* mpcListenerType;
296  const char* mpcEventMethod;
297 }
298 spTbxListenerData[] =
299 {
300  // Attention: MUST be in order of the XclTbxEventType enum!
301  /*EXC_TBX_EVENT_ACTION*/ { "XActionListener", "actionPerformed" },
302  /*EXC_TBX_EVENT_MOUSE*/ { "XMouseListener", "mouseReleased" },
303  /*EXC_TBX_EVENT_TEXT*/ { "XTextListener", "textChanged" },
304  /*EXC_TBX_EVENT_VALUE*/ { "XAdjustmentListener", "adjustmentValueChanged" },
305  /*EXC_TBX_EVENT_CHANGE*/ { "XChangeListener", "changed" }
306 };
307 
308 } // namespace
309 
310 bool XclControlHelper::FillMacroDescriptor( ScriptEventDescriptor& rDescriptor,
311  XclTbxEventType eEventType, const OUString& rXclMacroName, SfxObjectShell* pDocShell )
312 {
313  if( !rXclMacroName.isEmpty() )
314  {
315  rDescriptor.ListenerType = OUString::createFromAscii( spTbxListenerData[ eEventType ].mpcListenerType );
316  rDescriptor.EventMethod = OUString::createFromAscii( spTbxListenerData[ eEventType ].mpcEventMethod );
317  rDescriptor.ScriptType = "Script";
318  rDescriptor.ScriptCode = XclTools::GetSbMacroUrl( rXclMacroName, pDocShell );
319  return true;
320  }
321  return false;
322 }
323 
325  const ScriptEventDescriptor& rDescriptor, XclTbxEventType eEventType )
326 {
327  if( (!rDescriptor.ScriptCode.isEmpty()) &&
328  rDescriptor.ScriptType.equalsIgnoreAsciiCase("Script") &&
329  rDescriptor.ListenerType.equalsAscii( spTbxListenerData[ eEventType ].mpcListenerType ) &&
330  rDescriptor.EventMethod.equalsAscii( spTbxListenerData[ eEventType ].mpcEventMethod ) )
331  return XclTools::GetXclMacroName( rDescriptor.ScriptCode );
332  return OUString();
333 }
334 
335 /* vim:set shiftwidth=4 softtabstop=4 expandtab: */
XclImpStream & operator>>(XclImpStream &rStrm, XclObjLineData &rLineData)
Definition: xlescher.cxx:203
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:310
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:242
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:254
void SetRight(tools::Long v)
SC_DLLPUBLIC sal_uInt16 GetRowHeight(SCROW nRow, SCTAB nTab, bool bHiddenAsZero=true) const
Definition: document.cxx:4151
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:871
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:138
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:718
tools::Long Bottom() const
const sal_uInt8 EXC_OBJ_LINE_SOLID
Definition: xlescher.hxx:82
constexpr auto convert(N n, sal_Int64 mul, sal_Int64 div)
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:870
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:324
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:271
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:709
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
constexpr o3tl::Length MapToO3tlLength(MapUnit eU, o3tl::Length ePixelValue=o3tl::Length::px)
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:984
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:130
SC_DLLPUBLIC sal_uInt16 GetColWidth(SCCOL nCol, SCTAB nTab, bool bHiddenAsZero=true) const
Definition: document.cxx:4110
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:4214
sal_Int16 SCTAB
Definition: types.hxx:23
SC_DLLPUBLIC sal_uLong GetRowOffset(SCROW nRow, SCTAB nTab, bool bHiddenAsZero=true) const
Definition: document.cxx:4222
void SetRect(const XclRoot &rRoot, SCTAB nScTab, const tools::Rectangle &rRect, MapUnit eMapUnit)
Initializes the anchor coordinates for a sheet.
Definition: xlescher.cxx:154
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:282