LCOV - code coverage report
Current view: top level - libreoffice/sc/source/filter/excel - xlescher.cxx (source / functions) Hit Total Coverage
Test: libreoffice_filtered.info Lines: 44 147 29.9 %
Date: 2012-12-27 Functions: 13 24 54.2 %
Legend: Lines: hit not hit

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

Generated by: LCOV version 1.10