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