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 : // ============================================================================
36 :
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';
53 :
54 : // ============================================================================
55 :
56 11 : struct DefinedNameModel
57 : {
58 : ::rtl::OUString maName; /// The original name.
59 : ::rtl::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 : // ============================================================================
71 :
72 : /** Base class for defined names and external names. */
73 11 : class DefinedNameBase : public WorkbookHelper
74 : {
75 : public:
76 : explicit DefinedNameBase( const WorkbookHelper& rHelper );
77 :
78 : /** Returns the original name as imported from or exported to the file. */
79 0 : inline const ::rtl::OUString& getModelName() const { return maModel.maName; }
80 : /** Returns the name as used in the Calc document. */
81 0 : inline const ::rtl::OUString& getCalcName() const { return maCalcName; }
82 :
83 : /** Returns the original name as imported from or exported to the file. */
84 : const ::rtl::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;
87 :
88 : protected:
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 );
95 :
96 : protected:
97 : DefinedNameModel maModel; /// Model data for this defined name.
98 : mutable ::rtl::OUString maUpModelName; /// Model name converted to uppercase ASCII.
99 : ::rtl::OUString maCalcName; /// Final name used in the Calc document.
100 : ::com::sun::star::uno::Any maRefAny; /// Single cell/range reference.
101 : };
102 :
103 : // ============================================================================
104 :
105 22 : class DefinedName : public DefinedNameBase
106 : {
107 : public:
108 : explicit DefinedName( const WorkbookHelper& rHelper );
109 :
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 ::rtl::OUString& rFormula );
114 : /** Imports the defined name from a DEFINEDNAME record in the passed stream. */
115 : void importDefinedName( SequenceInputStream& rStrm );
116 :
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 33 : inline bool isGlobalName() const { return mnCalcSheet < 0; }
124 : /** Returns true, if this defined name is a special builtin name. */
125 16 : inline bool isBuiltinName() const { return mcBuiltinId != BIFF_DEFNAME_UNKNOWN; }
126 : /** Returns true, if this defined name is a macro function call. */
127 0 : inline bool isMacroFunction() const { return maModel.mbMacro && maModel.mbFunction; }
128 : /** Returns true, if this defined name is a reference to a VBA macro. */
129 0 : inline bool isVBName() const { return maModel.mbMacro && maModel.mbVBName; }
130 :
131 : /** Returns the 0-based sheet index for local names, or -1 for global names. */
132 12 : inline sal_Int16 getLocalCalcSheet() const { return mnCalcSheet; }
133 : /** Returns the built-in identifier of the defined name. */
134 1 : inline sal_Unicode getBuiltinId() const { return mcBuiltinId; }
135 : /** Returns the token index used in API token arrays (com.sun.star.sheet.FormulaToken). */
136 11 : 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;
139 :
140 : private:
141 : /** Imports the OOXML or BIFF12 definition of the name. */
142 : void implImportOoxFormula();
143 : /** Imports the BIFF definition of the name. */
144 : void implImportBiffFormula();
145 :
146 : private:
147 : typedef ::std::auto_ptr< StreamDataSequence > StreamDataSeqPtr;
148 : typedef ::std::auto_ptr< BiffInputStreamPos > BiffStreamPosPtr;
149 :
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.
157 : };
158 :
159 : typedef ::boost::shared_ptr< DefinedName > DefinedNameRef;
160 :
161 : // ============================================================================
162 :
163 22 : class DefinedNamesBuffer : public WorkbookHelper
164 : {
165 : public:
166 : explicit DefinedNamesBuffer( const WorkbookHelper& rHelper );
167 :
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 );
172 :
173 : /** Creates all defined names in the document. */
174 : void finalizeImport();
175 :
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 ::rtl::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;
189 :
190 : private:
191 : DefinedNameRef createDefinedName();
192 :
193 : private:
194 : typedef ::std::pair< sal_Int16, ::rtl::OUString > SheetNameKey;
195 : typedef ::std::pair< sal_Int16, sal_Unicode > BuiltinKey;
196 :
197 : typedef RefVector< DefinedName > DefNameVector;
198 : typedef RefMap< SheetNameKey, DefinedName > DefNameNameMap;
199 : typedef RefMap< BuiltinKey, DefinedName > DefNameBuiltinMap;
200 : typedef RefMap< sal_Int32, DefinedName > DefNameTokenIdMap;
201 :
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.
206 : };
207 :
208 : // ============================================================================
209 :
210 : } // namespace xls
211 : } // namespace oox
212 :
213 : #endif
214 :
215 : /* vim:set shiftwidth=4 softtabstop=4 expandtab: */
|