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 : #ifndef INCLUDED_SC_SOURCE_FILTER_INC_DEFNAMESBUFFER_HXX
21 : #define INCLUDED_SC_SOURCE_FILTER_INC_DEFNAMESBUFFER_HXX
22 :
23 : #include "formulabase.hxx"
24 : #include "rangenam.hxx"
25 :
26 : #include <memory>
27 :
28 : class ScTokenArray;
29 :
30 : namespace com { namespace sun { namespace star {
31 : namespace sheet { class XNamedRange; }
32 : } } }
33 :
34 : namespace oox {
35 : namespace xls {
36 :
37 : class BiffInputStreamPos;
38 :
39 : // codes for built-in names
40 : const sal_Unicode BIFF_DEFNAME_CONSOLIDATEAREA = '\x00';
41 : const sal_Unicode BIFF_DEFNAME_AUTOOPEN = '\x01'; // Sheet macro executed when workbook is opened.
42 : const sal_Unicode BIFF_DEFNAME_AUTOCLOSE = '\x02'; // Sheet macro executed when workbook is closed.
43 : const sal_Unicode BIFF_DEFNAME_EXTRACT = '\x03'; // Filter output destination for advanced filter.
44 : const sal_Unicode BIFF_DEFNAME_DATABASE = '\x04';
45 : const sal_Unicode BIFF_DEFNAME_CRITERIA = '\x05'; // Filter criteria source range for advanced filter.
46 : const sal_Unicode BIFF_DEFNAME_PRINTAREA = '\x06'; // Print ranges.
47 : const sal_Unicode BIFF_DEFNAME_PRINTTITLES = '\x07'; // Rows/columns repeated on each page when printing.
48 : const sal_Unicode BIFF_DEFNAME_RECORDER = '\x08';
49 : const sal_Unicode BIFF_DEFNAME_DATAFORM = '\x09';
50 : const sal_Unicode BIFF_DEFNAME_AUTOACTIVATE = '\x0A'; // Sheet macro executed when workbook is activated.
51 : const sal_Unicode BIFF_DEFNAME_AUTODEACTIVATE = '\x0B'; // Sheet macro executed when workbook is deactivated.
52 : const sal_Unicode BIFF_DEFNAME_SHEETTITLE = '\x0C';
53 : const sal_Unicode BIFF_DEFNAME_FILTERDATABASE = '\x0D'; // Sheet range autofilter or advanced filter works on.
54 : const sal_Unicode BIFF_DEFNAME_UNKNOWN = '\x0E';
55 :
56 11 : struct DefinedNameModel
57 : {
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.
66 :
67 : explicit DefinedNameModel();
68 : };
69 :
70 : /** Base class for defined names and external names. */
71 11 : class DefinedNameBase : public WorkbookHelper
72 : {
73 : public:
74 : explicit DefinedNameBase( const WorkbookHelper& rHelper );
75 :
76 : /** Returns the original name as imported from or exported to the file. */
77 0 : inline const OUString& getModelName() const { return maModel.maName; }
78 : /** Returns the name as used in the Calc document. */
79 0 : inline const OUString& getCalcName() const { return maCalcName; }
80 :
81 : /** Returns the original name as imported from or exported to the file. */
82 : const OUString& getUpcaseModelName() const;
83 : /** Returns an Any with a SingleReference or ComplexReference, or an empty Any. */
84 : ::com::sun::star::uno::Any getReference( const ::com::sun::star::table::CellAddress& rBaseAddr ) const;
85 :
86 : protected:
87 : /** Converts the OOXML formula string stored in the own model. */
88 : ApiTokenSequence importOoxFormula( sal_Int16 nBaseSheet );
89 : /** Imports the BIFF12 formula from the passed stream. */
90 : ApiTokenSequence importBiff12Formula( sal_Int16 nBaseSheet, SequenceInputStream& rStrm );
91 : /** Imports the BIFF formula from the passed stream. */
92 : ApiTokenSequence importBiffFormula( sal_Int16 nBaseSheet, BiffInputStream& rStrm, const sal_uInt16* pnFmlaSize = 0 );
93 :
94 : protected:
95 : DefinedNameModel maModel; /// Model data for this defined name.
96 : mutable OUString maUpModelName; /// Model name converted to uppercase ASCII.
97 : OUString maCalcName; /// Final name used in the Calc document.
98 : ::com::sun::star::uno::Any maRefAny; /// Single cell/range reference.
99 : };
100 :
101 22 : class DefinedName : public DefinedNameBase
102 : {
103 : public:
104 : explicit DefinedName( const WorkbookHelper& rHelper );
105 :
106 : /** Sets the attributes for this defined name from the passed attribute set. */
107 : void importDefinedName( const AttributeList& rAttribs );
108 : /** Sets the formula string from the body of the definedName element. */
109 : void setFormula( const OUString& rFormula );
110 : /** Imports the defined name from a DEFINEDNAME record in the passed stream. */
111 : void importDefinedName( SequenceInputStream& rStrm );
112 :
113 : /** Creates a defined name in the Calc document. */
114 : void createNameObject( sal_Int32 nIndex );
115 : /** Converts the formula string or BIFF token array for this defined name. */
116 : void convertFormula();
117 : ApiTokenSequence getTokens();
118 : std::unique_ptr<ScTokenArray> getScTokens();
119 : /** Returns true, if this defined name is global in the document. */
120 33 : inline bool isGlobalName() const { return mnCalcSheet < 0; }
121 : /** Returns true, if this defined name is a special builtin name. */
122 16 : inline bool isBuiltinName() const { return mcBuiltinId != BIFF_DEFNAME_UNKNOWN; }
123 : /** Returns true, if this defined name is a macro function call. */
124 0 : inline bool isMacroFunction() const { return maModel.mbMacro && maModel.mbFunction; }
125 : /** Returns true, if this defined name is a reference to a VBA macro. */
126 0 : inline bool isVBName() const { return maModel.mbMacro && maModel.mbVBName; }
127 :
128 : /** Returns the 0-based sheet index for local names, or -1 for global names. */
129 12 : inline sal_Int16 getLocalCalcSheet() const { return mnCalcSheet; }
130 : /** Returns the built-in identifier of the defined name. */
131 1 : inline sal_Unicode getBuiltinId() const { return mcBuiltinId; }
132 : /** Returns the token index used in API token arrays (com.sun.star.sheet.FormulaToken). */
133 11 : inline sal_Int32 getTokenIndex() const { return mnTokenIndex; }
134 : /** Tries to resolve the defined name to an absolute cell range. */
135 : bool getAbsoluteRange( ::com::sun::star::table::CellRangeAddress& orRange ) const;
136 :
137 : private:
138 : /** Imports the OOXML or BIFF12 definition of the name. */
139 : void implImportOoxFormula();
140 : /** Imports the BIFF definition of the name. */
141 : void implImportBiffFormula();
142 :
143 : private:
144 : typedef ::std::unique_ptr< StreamDataSequence > StreamDataSeqPtr;
145 : typedef ::std::unique_ptr< BiffInputStreamPos > BiffStreamPosPtr;
146 :
147 : ScRangeData* mpScRangeData; /// ScRangeData of the defined name.
148 : sal_Int32 mnTokenIndex; /// Name index used in API token array.
149 : sal_Int16 mnCalcSheet; /// Calc sheet index for sheet-local names.
150 : sal_Unicode mcBuiltinId; /// Identifier for built-in defined names.
151 : StreamDataSeqPtr mxFormula; /// Formula data for BIFF12 import.
152 : BiffStreamPosPtr mxBiffStrm; /// Cached BIFF stream for formula import.
153 : sal_uInt16 mnFmlaSize; /// Cached BIFF formula size for formula import.
154 : };
155 :
156 : typedef std::shared_ptr< DefinedName > DefinedNameRef;
157 :
158 290 : class DefinedNamesBuffer : public WorkbookHelper
159 : {
160 : public:
161 : explicit DefinedNamesBuffer( const WorkbookHelper& rHelper );
162 :
163 : /** Imports a defined name from the passed attribute set. */
164 : DefinedNameRef importDefinedName( const AttributeList& rAttribs );
165 : /** Imports a defined name from a DEFINEDNAME record in the passed stream. */
166 : void importDefinedName( SequenceInputStream& rStrm );
167 :
168 : /** Creates all defined names in the document. */
169 : void finalizeImport();
170 :
171 : /** Returns a defined name by zero-based index (order of appearance). */
172 : DefinedNameRef getByIndex( sal_Int32 nIndex ) const;
173 : /** Returns a defined name by token index (index in XDefinedNames container). */
174 : DefinedNameRef getByTokenIndex( sal_Int32 nIndex ) const;
175 : /** Returns a defined name by its model name.
176 : @param nSheet The sheet index for local names or -1 for global names.
177 : If no local name is found, tries to find a matching global name.
178 : @return Reference to the defined name or empty reference. */
179 : DefinedNameRef getByModelName( const OUString& rModelName, sal_Int16 nCalcSheet = -1 ) const;
180 : /** Returns a built-in defined name by its built-in identifier.
181 : @param nSheet The sheet index of the built-in name.
182 : @return Reference to the defined name or empty reference. */
183 : DefinedNameRef getByBuiltinId( sal_Unicode cBuiltinId, sal_Int16 nCalcSheet ) const;
184 :
185 : private:
186 : DefinedNameRef createDefinedName();
187 :
188 : private:
189 : typedef ::std::pair< sal_Int16, OUString > SheetNameKey;
190 : typedef ::std::pair< sal_Int16, sal_Unicode > BuiltinKey;
191 :
192 : typedef RefVector< DefinedName > DefNameVector;
193 : typedef RefMap< SheetNameKey, DefinedName > DefNameNameMap;
194 : typedef RefMap< BuiltinKey, DefinedName > DefNameBuiltinMap;
195 : typedef RefMap< sal_Int32, DefinedName > DefNameTokenIdMap;
196 :
197 : DefNameVector maDefNames; /// List of all defined names in insertion order.
198 : DefNameNameMap maModelNameMap; /// Maps all defined names by sheet index and model name.
199 : DefNameBuiltinMap maBuiltinMap; /// Maps all defined names by sheet index and built-in identifier.
200 : DefNameTokenIdMap maTokenIdMap; /// Maps all defined names by API token index.
201 : };
202 :
203 : } // namespace xls
204 : } // namespace oox
205 :
206 : #endif
207 :
208 : /* vim:set shiftwidth=4 softtabstop=4 expandtab: */
|