1 /* -*- Mode: C++; tab-width: 4; indent-tabs-mode: nil; c-basic-offset: 4 -*- */
3 * This file is part of the LibreOffice project.
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/.
9 * This file incorporates work covered by the following license notice:
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 .
20 #ifndef OOX_XLS_DEFINEDNAMESBUFFER_HXX
21 #define OOX_XLS_DEFINEDNAMESBUFFER_HXX
23 #include "formulabase.hxx"
24 #include "rangenam.hxx"
26 namespace com
{ namespace sun
{ namespace star
{
27 namespace sheet
{ class XNamedRange
; }
33 class BiffInputStreamPos
;
35 // ============================================================================
37 // codes for built-in names
38 const sal_Unicode BIFF_DEFNAME_CONSOLIDATEAREA
= '\x00';
39 const sal_Unicode BIFF_DEFNAME_AUTOOPEN
= '\x01'; // Sheet macro executed when workbook is opened.
40 const sal_Unicode BIFF_DEFNAME_AUTOCLOSE
= '\x02'; // Sheet macro executed when workbook is closed.
41 const sal_Unicode BIFF_DEFNAME_EXTRACT
= '\x03'; // Filter output destination for advanced filter.
42 const sal_Unicode BIFF_DEFNAME_DATABASE
= '\x04';
43 const sal_Unicode BIFF_DEFNAME_CRITERIA
= '\x05'; // Filter criteria source range for advanced filter.
44 const sal_Unicode BIFF_DEFNAME_PRINTAREA
= '\x06'; // Print ranges.
45 const sal_Unicode BIFF_DEFNAME_PRINTTITLES
= '\x07'; // Rows/columns repeated on each page when printing.
46 const sal_Unicode BIFF_DEFNAME_RECORDER
= '\x08';
47 const sal_Unicode BIFF_DEFNAME_DATAFORM
= '\x09';
48 const sal_Unicode BIFF_DEFNAME_AUTOACTIVATE
= '\x0A'; // Sheet macro executed when workbook is activated.
49 const sal_Unicode BIFF_DEFNAME_AUTODEACTIVATE
= '\x0B'; // Sheet macro executed when workbook is deactivated.
50 const sal_Unicode BIFF_DEFNAME_SHEETTITLE
= '\x0C';
51 const sal_Unicode BIFF_DEFNAME_FILTERDATABASE
= '\x0D'; // Sheet range autofilter or advanced filter works on.
52 const sal_Unicode BIFF_DEFNAME_UNKNOWN
= '\x0E';
54 // ============================================================================
56 struct DefinedNameModel
58 OUString maName
; /// The original name.
59 OUString maFormula
; /// The formula string.
60 sal_Int32 mnSheet
; /// Sheet index for local names.
61 sal_Int32 mnFuncGroupId
; /// Function group identifier.
62 bool mbMacro
; /// True = Macro name (VBA or sheet macro).
63 bool mbFunction
; /// True = function, false = command.
64 bool mbVBName
; /// True = VBA macro, false = sheet macro.
65 bool mbHidden
; /// True = name hidden in UI.
67 explicit DefinedNameModel();
70 // ============================================================================
72 /** Base class for defined names and external names. */
73 class DefinedNameBase
: public WorkbookHelper
76 explicit DefinedNameBase( const WorkbookHelper
& rHelper
);
78 /** Returns the original name as imported from or exported to the file. */
79 inline const OUString
& getModelName() const { return maModel
.maName
; }
80 /** Returns the name as used in the Calc document. */
81 inline const OUString
& getCalcName() const { return maCalcName
; }
83 /** Returns the original name as imported from or exported to the file. */
84 const OUString
& getUpcaseModelName() const;
85 /** Returns an Any with a SingleReference or ComplexReference, or an empty Any. */
86 ::com::sun::star::uno::Any
getReference( const ::com::sun::star::table::CellAddress
& rBaseAddr
) const;
89 /** Converts the OOXML formula string stored in the own model. */
90 ApiTokenSequence
importOoxFormula( sal_Int16 nBaseSheet
);
91 /** Imports the BIFF12 formula from the passed stream. */
92 ApiTokenSequence
importBiff12Formula( sal_Int16 nBaseSheet
, SequenceInputStream
& rStrm
);
93 /** Imports the BIFF formula from the passed stream. */
94 ApiTokenSequence
importBiffFormula( sal_Int16 nBaseSheet
, BiffInputStream
& rStrm
, const sal_uInt16
* pnFmlaSize
= 0 );
97 DefinedNameModel maModel
; /// Model data for this defined name.
98 mutable OUString maUpModelName
; /// Model name converted to uppercase ASCII.
99 OUString maCalcName
; /// Final name used in the Calc document.
100 ::com::sun::star::uno::Any maRefAny
; /// Single cell/range reference.
103 // ============================================================================
105 class DefinedName
: public DefinedNameBase
108 explicit DefinedName( const WorkbookHelper
& rHelper
);
110 /** Sets the attributes for this defined name from the passed attribute set. */
111 void importDefinedName( const AttributeList
& rAttribs
);
112 /** Sets the formula string from the body of the definedName element. */
113 void setFormula( const OUString
& rFormula
);
114 /** Imports the defined name from a DEFINEDNAME record in the passed stream. */
115 void importDefinedName( SequenceInputStream
& rStrm
);
117 /** Creates a defined name in the Calc document. */
118 void createNameObject( sal_Int32 nIndex
);
119 /** Converts the formula string or BIFF token array for this defined name. */
120 void convertFormula();
121 ApiTokenSequence
getTokens();
122 /** Returns true, if this defined name is global in the document. */
123 inline bool isGlobalName() const { return mnCalcSheet
< 0; }
124 /** Returns true, if this defined name is a special builtin name. */
125 inline bool isBuiltinName() const { return mcBuiltinId
!= BIFF_DEFNAME_UNKNOWN
; }
126 /** Returns true, if this defined name is a macro function call. */
127 inline bool isMacroFunction() const { return maModel
.mbMacro
&& maModel
.mbFunction
; }
128 /** Returns true, if this defined name is a reference to a VBA macro. */
129 inline bool isVBName() const { return maModel
.mbMacro
&& maModel
.mbVBName
; }
131 /** Returns the 0-based sheet index for local names, or -1 for global names. */
132 inline sal_Int16
getLocalCalcSheet() const { return mnCalcSheet
; }
133 /** Returns the built-in identifier of the defined name. */
134 inline sal_Unicode
getBuiltinId() const { return mcBuiltinId
; }
135 /** Returns the token index used in API token arrays (com.sun.star.sheet.FormulaToken). */
136 inline sal_Int32
getTokenIndex() const { return mnTokenIndex
; }
137 /** Tries to resolve the defined name to an absolute cell range. */
138 bool getAbsoluteRange( ::com::sun::star::table::CellRangeAddress
& orRange
) const;
141 /** Imports the OOXML or BIFF12 definition of the name. */
142 void implImportOoxFormula();
143 /** Imports the BIFF definition of the name. */
144 void implImportBiffFormula();
147 typedef ::std::auto_ptr
< StreamDataSequence
> StreamDataSeqPtr
;
148 typedef ::std::auto_ptr
< BiffInputStreamPos
> BiffStreamPosPtr
;
150 ScRangeData
* mpScRangeData
; /// ScRangeData of the defined name.
151 sal_Int32 mnTokenIndex
; /// Name index used in API token array.
152 sal_Int16 mnCalcSheet
; /// Calc sheet index for sheet-local names.
153 sal_Unicode mcBuiltinId
; /// Identifier for built-in defined names.
154 StreamDataSeqPtr mxFormula
; /// Formula data for BIFF12 import.
155 BiffStreamPosPtr mxBiffStrm
; /// Cached BIFF stream for formula import.
156 sal_uInt16 mnFmlaSize
; /// Cached BIFF formula size for formula import.
159 typedef ::boost::shared_ptr
< DefinedName
> DefinedNameRef
;
161 // ============================================================================
163 class DefinedNamesBuffer
: public WorkbookHelper
166 explicit DefinedNamesBuffer( const WorkbookHelper
& rHelper
);
168 /** Imports a defined name from the passed attribute set. */
169 DefinedNameRef
importDefinedName( const AttributeList
& rAttribs
);
170 /** Imports a defined name from a DEFINEDNAME record in the passed stream. */
171 void importDefinedName( SequenceInputStream
& rStrm
);
173 /** Creates all defined names in the document. */
174 void finalizeImport();
176 /** Returns a defined name by zero-based index (order of appearance). */
177 DefinedNameRef
getByIndex( sal_Int32 nIndex
) const;
178 /** Returns a defined name by token index (index in XDefinedNames container). */
179 DefinedNameRef
getByTokenIndex( sal_Int32 nIndex
) const;
180 /** Returns a defined name by its model name.
181 @param nSheet The sheet index for local names or -1 for global names.
182 If no local name is found, tries to find a matching global name.
183 @return Reference to the defined name or empty reference. */
184 DefinedNameRef
getByModelName( const OUString
& rModelName
, sal_Int16 nCalcSheet
= -1 ) const;
185 /** Returns a built-in defined name by its built-in identifier.
186 @param nSheet The sheet index of the built-in name.
187 @return Reference to the defined name or empty reference. */
188 DefinedNameRef
getByBuiltinId( sal_Unicode cBuiltinId
, sal_Int16 nCalcSheet
) const;
191 DefinedNameRef
createDefinedName();
194 typedef ::std::pair
< sal_Int16
, OUString
> SheetNameKey
;
195 typedef ::std::pair
< sal_Int16
, sal_Unicode
> BuiltinKey
;
197 typedef RefVector
< DefinedName
> DefNameVector
;
198 typedef RefMap
< SheetNameKey
, DefinedName
> DefNameNameMap
;
199 typedef RefMap
< BuiltinKey
, DefinedName
> DefNameBuiltinMap
;
200 typedef RefMap
< sal_Int32
, DefinedName
> DefNameTokenIdMap
;
202 DefNameVector maDefNames
; /// List of all defined names in insertion order.
203 DefNameNameMap maModelNameMap
; /// Maps all defined names by sheet index and model name.
204 DefNameBuiltinMap maBuiltinMap
; /// Maps all defined names by sheet index and built-in identifier.
205 DefNameTokenIdMap maTokenIdMap
; /// Maps all defined names by API token index.
208 // ============================================================================
215 /* vim:set shiftwidth=4 softtabstop=4 expandtab: */