xref: /trunk/main/oox/inc/oox/xls/defnamesbuffer.hxx (revision a893be29)
1e3508121SAndrew Rist /**************************************************************
2cdf0e10cSrcweir  *
3e3508121SAndrew Rist  * Licensed to the Apache Software Foundation (ASF) under one
4e3508121SAndrew Rist  * or more contributor license agreements.  See the NOTICE file
5e3508121SAndrew Rist  * distributed with this work for additional information
6e3508121SAndrew Rist  * regarding copyright ownership.  The ASF licenses this file
7e3508121SAndrew Rist  * to you under the Apache License, Version 2.0 (the
8e3508121SAndrew Rist  * "License"); you may not use this file except in compliance
9e3508121SAndrew Rist  * with the License.  You may obtain a copy of the License at
10e3508121SAndrew Rist  *
11e3508121SAndrew Rist  *   http://www.apache.org/licenses/LICENSE-2.0
12e3508121SAndrew Rist  *
13e3508121SAndrew Rist  * Unless required by applicable law or agreed to in writing,
14e3508121SAndrew Rist  * software distributed under the License is distributed on an
15e3508121SAndrew Rist  * "AS IS" BASIS, WITHOUT WARRANTIES OR CONDITIONS OF ANY
16e3508121SAndrew Rist  * KIND, either express or implied.  See the License for the
17e3508121SAndrew Rist  * specific language governing permissions and limitations
18e3508121SAndrew Rist  * under the License.
19e3508121SAndrew Rist  *
20e3508121SAndrew Rist  *************************************************************/
21e3508121SAndrew Rist 
22e3508121SAndrew Rist 
23cdf0e10cSrcweir 
24cdf0e10cSrcweir #ifndef OOX_XLS_DEFINEDNAMESBUFFER_HXX
25cdf0e10cSrcweir #define OOX_XLS_DEFINEDNAMESBUFFER_HXX
26cdf0e10cSrcweir 
27cdf0e10cSrcweir #include "oox/xls/formulabase.hxx"
28cdf0e10cSrcweir 
29cdf0e10cSrcweir namespace com { namespace sun { namespace star {
30cdf0e10cSrcweir     namespace sheet { class XNamedRange; }
31cdf0e10cSrcweir } } }
32cdf0e10cSrcweir 
33cdf0e10cSrcweir namespace oox {
34cdf0e10cSrcweir namespace xls {
35cdf0e10cSrcweir 
36cdf0e10cSrcweir class BiffInputStreamPos;
37cdf0e10cSrcweir 
38cdf0e10cSrcweir // ============================================================================
39cdf0e10cSrcweir 
40cdf0e10cSrcweir // codes for built-in names
41cdf0e10cSrcweir const sal_Unicode BIFF_DEFNAME_CONSOLIDATEAREA  = '\x00';
42cdf0e10cSrcweir const sal_Unicode BIFF_DEFNAME_AUTOOPEN         = '\x01';   // Sheet macro executed when workbook is opened.
43cdf0e10cSrcweir const sal_Unicode BIFF_DEFNAME_AUTOCLOSE        = '\x02';   // Sheet macro executed when workbook is closed.
44cdf0e10cSrcweir const sal_Unicode BIFF_DEFNAME_EXTRACT          = '\x03';   // Filter output destination for advanced filter.
45cdf0e10cSrcweir const sal_Unicode BIFF_DEFNAME_DATABASE         = '\x04';
46cdf0e10cSrcweir const sal_Unicode BIFF_DEFNAME_CRITERIA         = '\x05';   // Filter criteria source range for advanced filter.
47cdf0e10cSrcweir const sal_Unicode BIFF_DEFNAME_PRINTAREA        = '\x06';   // Print ranges.
48cdf0e10cSrcweir const sal_Unicode BIFF_DEFNAME_PRINTTITLES      = '\x07';   // Rows/columns repeated on each page when printing.
49cdf0e10cSrcweir const sal_Unicode BIFF_DEFNAME_RECORDER         = '\x08';
50cdf0e10cSrcweir const sal_Unicode BIFF_DEFNAME_DATAFORM         = '\x09';
51cdf0e10cSrcweir const sal_Unicode BIFF_DEFNAME_AUTOACTIVATE     = '\x0A';   // Sheet macro executed when workbook is activated.
52cdf0e10cSrcweir const sal_Unicode BIFF_DEFNAME_AUTODEACTIVATE   = '\x0B';   // Sheet macro executed when workbook is deactivated.
53cdf0e10cSrcweir const sal_Unicode BIFF_DEFNAME_SHEETTITLE       = '\x0C';
54cdf0e10cSrcweir const sal_Unicode BIFF_DEFNAME_FILTERDATABASE   = '\x0D';   // Sheet range autofilter or advanced filter works on.
55cdf0e10cSrcweir const sal_Unicode BIFF_DEFNAME_UNKNOWN          = '\x0E';
56cdf0e10cSrcweir 
57cdf0e10cSrcweir // ============================================================================
58cdf0e10cSrcweir 
59cdf0e10cSrcweir struct DefinedNameModel
60cdf0e10cSrcweir {
61cdf0e10cSrcweir     ::rtl::OUString     maName;         /// The original name.
62cdf0e10cSrcweir     ::rtl::OUString     maFormula;      /// The formula string.
63cdf0e10cSrcweir     sal_Int32           mnSheet;        /// Sheet index for local names.
64cdf0e10cSrcweir     sal_Int32           mnFuncGroupId;  /// Function group identifier.
65cdf0e10cSrcweir     bool                mbMacro;        /// True = Macro name (VBA or sheet macro).
66cdf0e10cSrcweir     bool                mbFunction;     /// True = function, false = command.
67cdf0e10cSrcweir     bool                mbVBName;       /// True = VBA macro, false = sheet macro.
68cdf0e10cSrcweir     bool                mbHidden;       /// True = name hidden in UI.
69cdf0e10cSrcweir 
70cdf0e10cSrcweir     explicit            DefinedNameModel();
71cdf0e10cSrcweir };
72cdf0e10cSrcweir 
73cdf0e10cSrcweir // ============================================================================
74cdf0e10cSrcweir 
75cdf0e10cSrcweir /** Base class for defined names and external names. */
76cdf0e10cSrcweir class DefinedNameBase : public WorkbookHelper
77cdf0e10cSrcweir {
78cdf0e10cSrcweir public:
79cdf0e10cSrcweir     explicit            DefinedNameBase( const WorkbookHelper& rHelper );
80cdf0e10cSrcweir 
81cdf0e10cSrcweir     /** Returns the original name as imported from or exported to the file. */
getModelName() const82cdf0e10cSrcweir     inline const ::rtl::OUString& getModelName() const { return maModel.maName; }
83cdf0e10cSrcweir     /** Returns the name as used in the Calc document. */
getCalcName() const84cdf0e10cSrcweir     inline const ::rtl::OUString& getCalcName() const { return maCalcName; }
85cdf0e10cSrcweir 
86cdf0e10cSrcweir     /** Returns the original name as imported from or exported to the file. */
87cdf0e10cSrcweir     const ::rtl::OUString& getUpcaseModelName() const;
88cdf0e10cSrcweir     /** Returns an Any with a SingleReference or ComplexReference, or an empty Any. */
89cdf0e10cSrcweir     ::com::sun::star::uno::Any getReference( const ::com::sun::star::table::CellAddress& rBaseAddr ) const;
90cdf0e10cSrcweir 
91cdf0e10cSrcweir protected:
92cdf0e10cSrcweir     /** Converts the OOXML formula string stored in the own model. */
93cdf0e10cSrcweir     ApiTokenSequence    importOoxFormula( sal_Int16 nBaseSheet );
94cdf0e10cSrcweir     /** Imports the BIFF12 formula from the passed stream. */
95cdf0e10cSrcweir     ApiTokenSequence    importBiff12Formula( sal_Int16 nBaseSheet, SequenceInputStream& rStrm );
96cdf0e10cSrcweir     /** Imports the BIFF formula from the passed stream. */
97cdf0e10cSrcweir     ApiTokenSequence    importBiffFormula( sal_Int16 nBaseSheet, BiffInputStream& rStrm, const sal_uInt16* pnFmlaSize = 0 );
98cdf0e10cSrcweir 
99cdf0e10cSrcweir     /** Tries to convert the passed token sequence to a SingleReference or ComplexReference. */
100cdf0e10cSrcweir     void                extractReference( const ApiTokenSequence& rTokens );
101cdf0e10cSrcweir 
102cdf0e10cSrcweir protected:
103cdf0e10cSrcweir     DefinedNameModel    maModel;            /// Model data for this defined name.
104cdf0e10cSrcweir     mutable ::rtl::OUString maUpModelName;  /// Model name converted to uppercase ASCII.
105cdf0e10cSrcweir     ::rtl::OUString     maCalcName;         /// Final name used in the Calc document.
106cdf0e10cSrcweir     ::com::sun::star::uno::Any maRefAny;    /// Single cell/range reference.
107cdf0e10cSrcweir };
108cdf0e10cSrcweir 
109cdf0e10cSrcweir // ============================================================================
110cdf0e10cSrcweir 
111cdf0e10cSrcweir class DefinedName : public DefinedNameBase
112cdf0e10cSrcweir {
113cdf0e10cSrcweir public:
114cdf0e10cSrcweir     explicit            DefinedName( const WorkbookHelper& rHelper );
115cdf0e10cSrcweir 
116cdf0e10cSrcweir     /** Sets the attributes for this defined name from the passed attribute set. */
117cdf0e10cSrcweir     void                importDefinedName( const AttributeList& rAttribs );
118cdf0e10cSrcweir     /** Sets the formula string from the body of the definedName element. */
119cdf0e10cSrcweir     void                setFormula( const ::rtl::OUString& rFormula );
120cdf0e10cSrcweir     /** Imports the defined name from a DEFINEDNAME record in the passed stream. */
121cdf0e10cSrcweir     void                importDefinedName( SequenceInputStream& rStrm );
122cdf0e10cSrcweir     /** Imports the defined name from a DEFINEDNAME record in the passed BIFF stream. */
123cdf0e10cSrcweir     void                importDefinedName( BiffInputStream& rStrm, sal_Int16 nCalcSheet );
124cdf0e10cSrcweir 
125cdf0e10cSrcweir     /** Creates a defined name in the Calc document. */
126cdf0e10cSrcweir     void                createNameObject();
127cdf0e10cSrcweir     /** Converts the formula string or BIFF token array for this defined name. */
128cdf0e10cSrcweir     void                convertFormula();
129cdf0e10cSrcweir 
130cdf0e10cSrcweir     /** Returns true, if this defined name is global in the document. */
isGlobalName() const131cdf0e10cSrcweir     inline bool         isGlobalName() const { return mnCalcSheet < 0; }
132cdf0e10cSrcweir     /** Returns true, if this defined name is a special builtin name. */
isBuiltinName() const133cdf0e10cSrcweir     inline bool         isBuiltinName() const { return mcBuiltinId != BIFF_DEFNAME_UNKNOWN; }
134cdf0e10cSrcweir     /** Returns true, if this defined name is a macro function call. */
isMacroFunction() const135cdf0e10cSrcweir     inline bool         isMacroFunction() const { return maModel.mbMacro && maModel.mbFunction; }
136cdf0e10cSrcweir     /** Returns true, if this defined name is a reference to a VBA macro. */
isVBName() const137cdf0e10cSrcweir     inline bool         isVBName() const { return maModel.mbMacro && maModel.mbVBName; }
138cdf0e10cSrcweir 
139cdf0e10cSrcweir     /** Returns the 0-based sheet index for local names, or -1 for global names. */
getLocalCalcSheet() const140cdf0e10cSrcweir     inline sal_Int16    getLocalCalcSheet() const { return mnCalcSheet; }
141cdf0e10cSrcweir     /** Returns the built-in identifier of the defined name. */
getBuiltinId() const142cdf0e10cSrcweir     inline sal_Unicode  getBuiltinId() const { return mcBuiltinId; }
143cdf0e10cSrcweir     /** Returns the token index used in API token arrays (com.sun.star.sheet.FormulaToken). */
getTokenIndex() const144cdf0e10cSrcweir     inline sal_Int32    getTokenIndex() const { return mnTokenIndex; }
145cdf0e10cSrcweir     /** Tries to resolve the defined name to an absolute cell range. */
146cdf0e10cSrcweir     bool                getAbsoluteRange( ::com::sun::star::table::CellRangeAddress& orRange ) const;
147cdf0e10cSrcweir 
148cdf0e10cSrcweir private:
149cdf0e10cSrcweir     /** Imports the OOXML or BIFF12 definition of the name. */
150cdf0e10cSrcweir     void                implImportOoxFormula();
151cdf0e10cSrcweir     /** Imports the BIFF definition of the name. */
152cdf0e10cSrcweir     void                implImportBiffFormula();
153cdf0e10cSrcweir 
154cdf0e10cSrcweir private:
155cdf0e10cSrcweir     typedef ::std::auto_ptr< StreamDataSequence >   StreamDataSeqPtr;
156cdf0e10cSrcweir     typedef ::std::auto_ptr< BiffInputStreamPos >   BiffStreamPosPtr;
157cdf0e10cSrcweir 
158102b8ff7SWang Lei     ::com::sun::star::uno::Reference< ::com::sun::star::sheet::XNamedRange2 >
159cdf0e10cSrcweir                         mxNamedRange;       /// XNamedRange interface of the defined name.
160cdf0e10cSrcweir     sal_Int32           mnTokenIndex;       /// Name index used in API token array.
161cdf0e10cSrcweir     sal_Int16           mnCalcSheet;        /// Calc sheet index for sheet-local names.
162cdf0e10cSrcweir     sal_Unicode         mcBuiltinId;        /// Identifier for built-in defined names.
163cdf0e10cSrcweir     StreamDataSeqPtr    mxFormula;          /// Formula data for BIFF12 import.
164cdf0e10cSrcweir     BiffStreamPosPtr    mxBiffStrm;         /// Cached BIFF stream for formula import.
165cdf0e10cSrcweir     sal_uInt16          mnFmlaSize;         /// Cached BIFF formula size for formula import.
166cdf0e10cSrcweir };
167cdf0e10cSrcweir 
168cdf0e10cSrcweir typedef ::boost::shared_ptr< DefinedName > DefinedNameRef;
169cdf0e10cSrcweir 
170cdf0e10cSrcweir // ============================================================================
171cdf0e10cSrcweir 
172cdf0e10cSrcweir class DefinedNamesBuffer : public WorkbookHelper
173cdf0e10cSrcweir {
174cdf0e10cSrcweir public:
175cdf0e10cSrcweir     explicit            DefinedNamesBuffer( const WorkbookHelper& rHelper );
176cdf0e10cSrcweir 
177cdf0e10cSrcweir     /** Sets the sheet index for local names (BIFF2-BIFF4 only). */
178cdf0e10cSrcweir     void                setLocalCalcSheet( sal_Int16 nCalcSheet );
179cdf0e10cSrcweir 
180cdf0e10cSrcweir     /** Imports a defined name from the passed attribute set. */
181cdf0e10cSrcweir     DefinedNameRef      importDefinedName( const AttributeList& rAttribs );
182cdf0e10cSrcweir     /** Imports a defined name from a DEFINEDNAME record in the passed stream. */
183cdf0e10cSrcweir     void                importDefinedName( SequenceInputStream& rStrm );
184cdf0e10cSrcweir     /** Imports a defined name from a DEFINEDNAME record in the passed BIFF stream. */
185cdf0e10cSrcweir     void                importDefinedName( BiffInputStream& rStrm );
186cdf0e10cSrcweir 
187cdf0e10cSrcweir     /** Creates all defined names in the document. */
188cdf0e10cSrcweir     void                finalizeImport();
189cdf0e10cSrcweir 
190*a893be29SPedro Giffuni     /** Returns a defined name by zero-based index (order of appearance). */
191cdf0e10cSrcweir     DefinedNameRef      getByIndex( sal_Int32 nIndex ) const;
192cdf0e10cSrcweir     /** Returns a defined name by token index (index in XDefinedNames container). */
193cdf0e10cSrcweir     DefinedNameRef      getByTokenIndex( sal_Int32 nIndex ) const;
194cdf0e10cSrcweir     /** Returns a defined name by its model name.
195cdf0e10cSrcweir         @param nSheet  The sheet index for local names or -1 for global names.
196cdf0e10cSrcweir             If no local name is found, tries to find a matching global name.
197cdf0e10cSrcweir         @return  Reference to the defined name or empty reference. */
198cdf0e10cSrcweir     DefinedNameRef      getByModelName( const ::rtl::OUString& rModelName, sal_Int16 nCalcSheet = -1 ) const;
199cdf0e10cSrcweir     /** Returns a built-in defined name by its built-in identifier.
200cdf0e10cSrcweir         @param nSheet  The sheet index of the built-in name.
201cdf0e10cSrcweir         @return  Reference to the defined name or empty reference. */
202cdf0e10cSrcweir     DefinedNameRef      getByBuiltinId( sal_Unicode cBuiltinId, sal_Int16 nCalcSheet ) const;
203cdf0e10cSrcweir 
204cdf0e10cSrcweir private:
205cdf0e10cSrcweir     DefinedNameRef      createDefinedName();
206cdf0e10cSrcweir 
207cdf0e10cSrcweir private:
208cdf0e10cSrcweir     typedef ::std::pair< sal_Int16, ::rtl::OUString >   SheetNameKey;
209cdf0e10cSrcweir     typedef ::std::pair< sal_Int16, sal_Unicode >       BuiltinKey;
210cdf0e10cSrcweir 
211cdf0e10cSrcweir     typedef RefVector< DefinedName >            DefNameVector;
212cdf0e10cSrcweir     typedef RefMap< SheetNameKey, DefinedName > DefNameNameMap;
213cdf0e10cSrcweir     typedef RefMap< BuiltinKey, DefinedName >   DefNameBuiltinMap;
214cdf0e10cSrcweir     typedef RefMap< sal_Int32, DefinedName >    DefNameTokenIdMap;
215cdf0e10cSrcweir 
216cdf0e10cSrcweir     DefNameVector       maDefNames;         /// List of all defined names in insertion order.
217cdf0e10cSrcweir     DefNameNameMap      maModelNameMap;     /// Maps all defined names by sheet index and model name.
218cdf0e10cSrcweir     DefNameBuiltinMap   maBuiltinMap;       /// Maps all defined names by sheet index and built-in identifier.
219cdf0e10cSrcweir     DefNameTokenIdMap   maTokenIdMap;       /// Maps all defined names by API token index.
220cdf0e10cSrcweir     sal_Int16           mnCalcSheet;        /// Current sheet index for BIFF2-BIFF4 names (always sheet-local).
221cdf0e10cSrcweir };
222cdf0e10cSrcweir 
223cdf0e10cSrcweir // ============================================================================
224cdf0e10cSrcweir 
225cdf0e10cSrcweir } // namespace xls
226cdf0e10cSrcweir } // namespace oox
227cdf0e10cSrcweir 
228cdf0e10cSrcweir #endif
229