LCOV - code coverage report
Current view: top level - sc/source/filter/inc - xihelper.hxx (source / functions) Hit Total Coverage
Test: libreoffice_filtered.info Lines: 5 10 50.0 %
Date: 2012-08-25 Functions: 6 12 50.0 %
Legend: Lines: hit not hit | Branches: + taken - not taken # not executed Branches: 1 10 10.0 %

           Branch data     Line data    Source code
       1                 :            : /* -*- Mode: C++; tab-width: 4; indent-tabs-mode: nil; c-basic-offset: 4 -*- */
       2                 :            : /*************************************************************************
       3                 :            :  *
       4                 :            :  * DO NOT ALTER OR REMOVE COPYRIGHT NOTICES OR THIS FILE HEADER.
       5                 :            :  *
       6                 :            :  * Copyright 2000, 2010 Oracle and/or its affiliates.
       7                 :            :  *
       8                 :            :  * OpenOffice.org - a multi-platform office productivity suite
       9                 :            :  *
      10                 :            :  * This file is part of OpenOffice.org.
      11                 :            :  *
      12                 :            :  * OpenOffice.org is free software: you can redistribute it and/or modify
      13                 :            :  * it under the terms of the GNU Lesser General Public License version 3
      14                 :            :  * only, as published by the Free Software Foundation.
      15                 :            :  *
      16                 :            :  * OpenOffice.org is distributed in the hope that it will be useful,
      17                 :            :  * but WITHOUT ANY WARRANTY; without even the implied warranty of
      18                 :            :  * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the
      19                 :            :  * GNU Lesser General Public License version 3 for more details
      20                 :            :  * (a copy is included in the LICENSE file that accompanied this code).
      21                 :            :  *
      22                 :            :  * You should have received a copy of the GNU Lesser General Public License
      23                 :            :  * version 3 along with OpenOffice.org.  If not, see
      24                 :            :  * <http://www.openoffice.org/license.html>
      25                 :            :  * for a copy of the LGPLv3 License.
      26                 :            :  *
      27                 :            :  ************************************************************************/
      28                 :            : 
      29                 :            : #ifndef SC_XIHELPER_HXX
      30                 :            : #define SC_XIHELPER_HXX
      31                 :            : 
      32                 :            : #include <editeng/editdata.hxx>
      33                 :            : #include <boost/noncopyable.hpp>
      34                 :            : #include <boost/shared_ptr.hpp>
      35                 :            : #include <boost/ptr_container/ptr_vector.hpp>
      36                 :            : #include "scmatrix.hxx"
      37                 :            : #include "xladdress.hxx"
      38                 :            : #include "xiroot.hxx"
      39                 :            : #include "xistring.hxx"
      40                 :            : 
      41                 :            : class ScRangeList;
      42                 :            : 
      43                 :            : // Excel->Calc cell address/range conversion ==================================
      44                 :            : 
      45                 :            : /** Provides functions to convert Excel cell addresses to Calc cell addresses. */
      46         [ -  + ]:        110 : class XclImpAddressConverter : public XclAddressConverterBase
      47                 :            : {
      48                 :            : public:
      49                 :            :     explicit            XclImpAddressConverter( const XclImpRoot& rRoot );
      50                 :            : 
      51                 :            :     // cell address -----------------------------------------------------------
      52                 :            : 
      53                 :            :     /** Checks if the passed Excel cell address is valid.
      54                 :            :         @param rXclPos  The Excel cell address to check.
      55                 :            :         @param bWarn  true = Sets the internal flag that produces a warning box
      56                 :            :             after loading/saving the file, if the cell address is not valid.
      57                 :            :         @return  true = Cell address in rXclPos is valid. */
      58                 :            :     bool                CheckAddress( const XclAddress& rXclPos, bool bWarn );
      59                 :            : 
      60                 :            :     /** Converts the passed Excel cell address to a Calc cell address.
      61                 :            :         @param rScPos  (Out) The converted Calc cell address, if valid.
      62                 :            :         @param rXclPos  The Excel cell address to convert.
      63                 :            :         @param bWarn  true = Sets the internal flag that produces a warning box
      64                 :            :             after loading/saving the file, if the cell address is invalid.
      65                 :            :         @return  true = Cell address returned in rScPos is valid. */
      66                 :            :     bool                ConvertAddress( ScAddress& rScPos,
      67                 :            :                             const XclAddress& rXclPos, SCTAB nScTab, bool bWarn );
      68                 :            : 
      69                 :            :     /** Returns a valid cell address by moving it into allowed dimensions.
      70                 :            :         @param rXclPos  The Excel cell address to convert.
      71                 :            :         @param bWarn  true = Sets the internal flag that produces a warning box
      72                 :            :             after loading/saving the file, if the cell address is invalid.
      73                 :            :         @return  The converted Calc cell address. */
      74                 :            :     ScAddress           CreateValidAddress( const XclAddress& rXclPos,
      75                 :            :                             SCTAB nScTab, bool bWarn );
      76                 :            : 
      77                 :            :     // cell range -------------------------------------------------------------
      78                 :            : 
      79                 :            :     /** Converts the passed Excel cell range to a Calc cell range.
      80                 :            :         @param rScRange  (Out) The converted Calc cell range, if valid.
      81                 :            :         @param rXclRange  The Excel cell range to convert.
      82                 :            :         @param bWarn  true = Sets the internal flag that produces a warning box
      83                 :            :             after loading/saving the file, if the cell range contains invalid cells.
      84                 :            :         @return  true = Cell range returned in rScRange is valid (original or cropped). */
      85                 :            :     bool                ConvertRange( ScRange& rScRange, const XclRange& rXclRange,
      86                 :            :                             SCTAB nScTab1, SCTAB nScTab2, bool bWarn );
      87                 :            : 
      88                 :            :     // cell range list --------------------------------------------------------
      89                 :            : 
      90                 :            :     /** Converts the passed Excel cell range list to a Calc cell range list.
      91                 :            :         @descr  The start position of the ranges will not be modified. Cell
      92                 :            :             ranges that fit partly into valid dimensions are cropped
      93                 :            :             accordingly. Cell ranges that do not fit at all, are not inserted
      94                 :            :             into the Calc cell range list.
      95                 :            :         @param rScRanges  (Out) The converted Calc cell range list.
      96                 :            :         @param rXclRanges  The Excel cell range list to convert.
      97                 :            :         @param bWarn  true = Sets the internal flag that produces a warning box
      98                 :            :             after loading/saving the file, if at least one of the cell ranges
      99                 :            :             contains invalid cells. */
     100                 :            :     void                ConvertRangeList( ScRangeList& rScRanges,
     101                 :            :                             const XclRangeList& rXclRanges, SCTAB nScTab, bool bWarn );
     102                 :            : };
     103                 :            : 
     104                 :            : // String->EditEngine conversion ==============================================
     105                 :            : 
     106                 :            : class ScBaseCell;
     107                 :            : class EditTextObject;
     108                 :            : 
     109                 :            : /** This class provides methods to convert an XclImpString.
     110                 :            :     @The string can be converted to an edit engine text object or directly
     111                 :            :     to a Calc edit cell. */
     112                 :            : class XclImpStringHelper : boost::noncopyable
     113                 :            : {
     114                 :            : public:
     115                 :            :     /** Returns a new edit engine text object.
     116                 :            :         @param nXFIndex  Index to XF for first text portion (for escapement). */
     117                 :            :     static EditTextObject* CreateTextObject(
     118                 :            :                             const XclImpRoot& rRoot,
     119                 :            :                             const XclImpString& rString );
     120                 :            : 
     121                 :            :     /** Creates a new text cell or edit cell for a Calc document.
     122                 :            :         @param nXFIndex  Index to XF for first text portion (for escapement). */
     123                 :            :     static ScBaseCell*  CreateCell(
     124                 :            :                             const XclImpRoot& rRoot,
     125                 :            :                             const XclImpString& rString,
     126                 :            :                             sal_uInt16 nXFIndex = 0 );
     127                 :            : private:
     128                 :            :     /** We don't want anybody to instantiate this class, since it is just a
     129                 :            :         collection of static methods. To enforce this, the default constructor
     130                 :            :         is made private */
     131                 :            :     XclImpStringHelper();
     132                 :            : };
     133                 :            : 
     134                 :            : // Header/footer conversion ===================================================
     135                 :            : 
     136                 :            : class EditEngine;
     137                 :            : class EditTextObject;
     138                 :            : class SfxItemSet;
     139                 :            : class SvxFieldItem;
     140                 :            : struct XclFontData;
     141                 :            : 
     142                 :            : /** Converts an Excel header/footer string into three edit engine text objects.
     143                 :            :     @descr  Header/footer content is divided into three parts: Left, center and
     144                 :            :     right portion. All formatting information is encoded in the Excel string
     145                 :            :     using special character seuences. A control sequence starts with the ampersand
     146                 :            :     character.
     147                 :            : 
     148                 :            :     Supported control sequences:
     149                 :            :     &L                      start of left portion
     150                 :            :     &C                      start of center portion
     151                 :            :     &R                      start of right portion
     152                 :            :     &P                      current page number
     153                 :            :     &N                      page count
     154                 :            :     &D                      current date
     155                 :            :     &T                      current time
     156                 :            :     &A                      table name
     157                 :            :     &F                      file name without path (see also &Z&F)
     158                 :            :     &Z                      file path without file name (converted to full file name, see also &Z&F)
     159                 :            :     &Z&F                    file path and name
     160                 :            :     &U                      underlining on/off
     161                 :            :     &E                      double underlining on/off
     162                 :            :     &S                      strikeout characters on/off
     163                 :            :     &X                      superscript on/off
     164                 :            :     &Y                      subscript on/off
     165                 :            :     &"fontname,fontstyle"   use font with name 'fontname' and style 'fontstyle'
     166                 :            :     &fontheight             set font height in points ('fontheight' is a decimal value)
     167                 :            : 
     168                 :            :     Known but unsupported control sequences:
     169                 :            :     &G                      picture
     170                 :            :  */
     171                 :            : class XclImpHFConverter : protected XclImpRoot, private boost::noncopyable
     172                 :            : {
     173                 :            : public:
     174                 :            :     explicit            XclImpHFConverter( const XclImpRoot& rRoot );
     175                 :            :                         ~XclImpHFConverter();
     176                 :            : 
     177                 :            :     /** Parses the passed string and creates three new edit engine text objects. */
     178                 :            :     void                ParseString( const String& rHFString );
     179                 :            : 
     180                 :            :     /** Creates a ScPageHFItem and inserts it into the passed item set. */
     181                 :            :     void                FillToItemSet( SfxItemSet& rItemSet, sal_uInt16 nWhichId ) const;
     182                 :            :     /** Returns the total height of the converted header or footer in twips. */
     183                 :            :     sal_Int32           GetTotalHeight() const;
     184                 :            : 
     185                 :            : private:    // types
     186                 :            :     typedef ::std::auto_ptr< XclFontData > XclFontDataPtr;
     187                 :            : 
     188                 :            :     /** Enumerates the supported header/footer portions. */
     189                 :            :     enum XclImpHFPortion { EXC_HF_LEFT, EXC_HF_CENTER, EXC_HF_RIGHT, EXC_HF_PORTION_COUNT };
     190                 :            : 
     191                 :            :     /** Contains all information about a header/footer portion. */
     192                 :        270 :     struct XclImpHFPortionInfo
     193                 :            :     {
     194                 :            :         typedef boost::shared_ptr< EditTextObject > EditTextObjectRef;
     195                 :            :         EditTextObjectRef   mxObj;          /// Edit engine text object.
     196                 :            :         ESelection          maSel;          /// Edit engine selection.
     197                 :            :         sal_Int32           mnHeight;       /// Height of previous lines in twips.
     198                 :            :         sal_uInt16          mnMaxLineHt;    /// Maximum font height for the current text line.
     199                 :            :         explicit            XclImpHFPortionInfo();
     200                 :            :     };
     201                 :            :     typedef ::std::vector< XclImpHFPortionInfo > XclImpHFPortionInfoVec;
     202                 :            : 
     203                 :            : private:
     204                 :            :     /** Returns the current edit engine text object. */
     205                 :        351 :     inline XclImpHFPortionInfo& GetCurrInfo() { return maInfos[ meCurrObj ]; }
     206                 :            :     /** Returns the current edit engine text object. */
     207                 :        108 :     inline XclImpHFPortionInfo::EditTextObjectRef& GetCurrObj() { return GetCurrInfo().mxObj; }
     208                 :            :     /** Returns the current selection. */
     209                 :        243 :     inline ESelection&  GetCurrSel() { return GetCurrInfo().maSel; }
     210                 :            : 
     211                 :            :     /** Returns the maximum line height of the specified portion. */
     212                 :            :     sal_uInt16          GetMaxLineHeight( XclImpHFPortion ePortion ) const;
     213                 :            :     /** Returns the current maximum line height. */
     214                 :            :     sal_uInt16          GetCurrMaxLineHeight() const;
     215                 :            : 
     216                 :            :     /** Updates the maximum line height of the specified portion, using the current font size. */
     217                 :            :     void                UpdateMaxLineHeight( XclImpHFPortion ePortion );
     218                 :            :     /** Updates the current maximum line height, using the current font size. */
     219                 :            :     void                UpdateCurrMaxLineHeight();
     220                 :            : 
     221                 :            :     /** Sets the font attributes at the current selection.
     222                 :            :         @descr  After that, the start position of the current selection object is
     223                 :            :         adjusted to the end of the selection. */
     224                 :            :     void                SetAttribs();
     225                 :            :     /** Resets font data to application default font. */
     226                 :            :     void                ResetFontData();
     227                 :            : 
     228                 :            :     /** Inserts maCurrText into edit engine and adjusts the current selection object.
     229                 :            :         @descr  The text shall not contain a newline character.
     230                 :            :         The text will be cleared after insertion. */
     231                 :            :     void                InsertText();
     232                 :            :     /** Inserts the passed text field and adjusts the current selection object. */
     233                 :            :     void                InsertField( const SvxFieldItem& rFieldItem );
     234                 :            :     /** Inserts a line break and adjusts the current selection object. */
     235                 :            :     void                InsertLineBreak();
     236                 :            : 
     237                 :            :     /** Creates the edit engine text object of current portion from edit engine. */
     238                 :            :     void                CreateCurrObject();
     239                 :            :     /** Changes current header/footer portion to eNew.
     240                 :            :         @descr  Creates text object of current portion and reinitializes edit engine. */
     241                 :            :     void                SetNewPortion( XclImpHFPortion eNew );
     242                 :            : 
     243                 :            : private:
     244                 :            :     EditEngine&         mrEE;               /// The header/footer edit engine.
     245                 :            :     XclImpHFPortionInfoVec maInfos;         /// Edit engine text objects for all portions.
     246                 :            :     String              maCurrText;         /// Current text to insert into edit engine.
     247                 :            :     XclFontDataPtr      mxFontData;         /// Font data of current text.
     248                 :            :     XclImpHFPortion     meCurrObj;          /// The current portion.
     249                 :            : };
     250                 :            : 
     251                 :            : // URL conversion =============================================================
     252                 :            : 
     253                 :            : /** This class contains static methods to decode an URL stored in an Excel file.
     254                 :            :     @descr  Excel URLs can contain a sheet name, for instance: path\[test.xls]Sheet1
     255                 :            :     This sheet name will be extracted automatically. */
     256                 :            : class XclImpUrlHelper : boost::noncopyable
     257                 :            : {
     258                 :            : public:
     259                 :            :     /** Decodes an encoded external document URL with optional sheet name.
     260                 :            :         @param rUrl  Returns the decoded file name incl. path.
     261                 :            :         @param rTabName  Returns the decoded sheet name.
     262                 :            :         @param rbSameWb  Returns true, if the URL is a reference to the own workbook.
     263                 :            :         @param rEncodedUrl   An encoded URL from Excel. */
     264                 :            :     static void         DecodeUrl(
     265                 :            :                             String& rUrl,
     266                 :            :                             String& rTabName,
     267                 :            :                             bool& rbSameWb,
     268                 :            :                             const XclImpRoot& rRoot,
     269                 :            :                             const String& rEncodedUrl );
     270                 :            : 
     271                 :            :     /** Decodes an encoded external document URL without sheet name.
     272                 :            :         @param rUrl  Returns the decoded file name incl. path.
     273                 :            :         @param rbSameWb  Returns true, if the URL is a reference to the own workbook.
     274                 :            :         @param rEncodedUrl   An encoded URL from Excel. */
     275                 :            :     static void         DecodeUrl(
     276                 :            :                             String& rUrl,
     277                 :            :                             bool& rbSameWb,
     278                 :            :                             const XclImpRoot& rRoot,
     279                 :            :                             const String& rEncodedUrl );
     280                 :            : 
     281                 :            :     static void         DecodeUrl(
     282                 :            :                             ::rtl::OUString& rUrl,
     283                 :            :                             bool& rbSameWb,
     284                 :            :                             const XclImpRoot& rRoot,
     285                 :            :                             const ::rtl::OUString& rEncodedUrl );
     286                 :            : 
     287                 :            :     /** Decodes the passed URL to OLE or DDE link components.
     288                 :            :         @descr  For DDE links: Decodes to application name and topic.
     289                 :            :         For OLE object links: Decodes to class name and document URL.
     290                 :            :         @return  true = decoding was successful, returned strings are valid (not empty). */
     291                 :            :     static bool         DecodeLink( String& rApplic, String& rTopic, const String rEncUrl );
     292                 :            : 
     293                 :            : private:
     294                 :            :     /** We don't want anybody to instantiate this class, since it is just a
     295                 :            :         collection of static methods. To enforce this, the default constructor
     296                 :            :         is made private */
     297                 :            :     XclImpUrlHelper();
     298                 :            : };
     299                 :            : 
     300                 :            : // Cached values ==============================================================
     301                 :            : 
     302                 :            : class ScTokenArray;
     303                 :            : 
     304                 :            : /** This class stores one cached value of a cached value list (used for instance in
     305                 :            :     CRN, EXTERNNAME, tArray). */
     306                 :            : class XclImpCachedValue : boost::noncopyable
     307                 :            : {
     308                 :            : public:
     309                 :            :     /** Creates a cached value and reads contents from stream and stores it with its array address. */
     310                 :            :     explicit            XclImpCachedValue( XclImpStream& rStrm );
     311                 :            :     virtual             ~XclImpCachedValue();
     312                 :            : 
     313                 :            :     /** Returns the type of the cached value (EXC_CACHEDVAL_*). */
     314                 :          0 :     inline sal_uInt8    GetType() const     { return mnType; }
     315                 :            :     /** Returns the cached string value, if this value is a string, else an empty string. */
     316         [ #  # ]:          0 :     inline const String& GetString() const  { return mxStr.get() ? *mxStr : EMPTY_STRING; }
     317                 :            :     /** Returns the cached number, if this value has number type, else 0.0. */
     318                 :          0 :     inline double       GetValue() const    { return mfValue; }
     319                 :            :     /** Returns the cached Boolean value, if this value has Boolean type, else false. */
     320 [ #  # ][ #  # ]:          0 :     inline bool         GetBool() const     { return (mnType == EXC_CACHEDVAL_BOOL) && (mnBoolErr != 0); }
     321                 :            :     /** Returns the cached Calc error code, if this value has Error type, else 0. */
     322         [ #  # ]:          0 :     inline sal_uInt8    GetXclError() const { return (mnType == EXC_CACHEDVAL_ERROR) ? mnBoolErr : EXC_ERR_NA; }
     323                 :            :     /** Returns the cached Calc error code, if this value has Error type, else 0. */
     324                 :            :     sal_uInt16              GetScError() const;
     325                 :            :     /** Returns the token array if this is a Boolean value or error value, else 0. */
     326                 :            :     inline const ScTokenArray* GetBoolErrFmla() const { return mxTokArr.get(); }
     327                 :            : 
     328                 :            : protected:
     329                 :            :     typedef ::std::auto_ptr< String >               StringPtr;
     330                 :            :     typedef ::std::auto_ptr< const ScTokenArray >   ScTokenArrayPtr;
     331                 :            : 
     332                 :            :     StringPtr           mxStr;      /// Cached value is a string.
     333                 :            :     double              mfValue;    /// Cached value is a double.
     334                 :            :     ScTokenArrayPtr     mxTokArr;   /// Cached value is a formula or error code or Boolean.
     335                 :            :     sal_uInt8           mnBoolErr;  /// Boolean value or Excel error code.
     336                 :            :     sal_uInt8           mnType;     /// The type of the cached value (EXC_CACHEDVAL_*).
     337                 :            : };
     338                 :            : 
     339                 :            : // ----------------------------------------------------------------------------
     340                 :            : 
     341                 :            : /** Contains cached values in a 2-dimensional array. */
     342                 :            : class XclImpCachedMatrix
     343                 :            : {
     344                 :            : public:
     345                 :            :     explicit            XclImpCachedMatrix( XclImpStream& rStrm );
     346                 :            :                         ~XclImpCachedMatrix();
     347                 :            : 
     348                 :            :     /** Creates a new ScMatrix object and fills it with the contained values. */
     349                 :            :     ScMatrixRef         CreateScMatrix() const;
     350                 :            : 
     351                 :            : private:
     352                 :            :     typedef boost::ptr_vector< XclImpCachedValue > XclImpValueList;
     353                 :            : 
     354                 :            :     XclImpValueList     maValueList;    /// List of cached cell values.
     355                 :            :     SCSIZE              mnScCols;       /// Number of cached columns.
     356                 :            :     SCSIZE              mnScRows;       /// Number of cached rows.
     357                 :            : };
     358                 :            : 
     359                 :            : // ============================================================================
     360                 :            : 
     361                 :            : #endif
     362                 :            : 
     363                 :            : /* vim:set shiftwidth=4 softtabstop=4 expandtab: */

Generated by: LCOV version 1.10