Branch data Line data Source code
1 : : /* -*- Mode: C++; tab-width: 4; indent-tabs-mode: nil; c-basic-offset: 4 -*- */
2 : : /*************************************************************************
3 : : *
4 : : * DO NOT ALTER OR REMOVE COPYRIGHT NOTICES OR THIS FILE HEADER.
5 : : *
6 : : * Copyright 2000, 2010 Oracle and/or its affiliates.
7 : : *
8 : : * OpenOffice.org - a multi-platform office productivity suite
9 : : *
10 : : * This file is part of OpenOffice.org.
11 : : *
12 : : * OpenOffice.org is free software: you can redistribute it and/or modify
13 : : * it under the terms of the GNU Lesser General Public License version 3
14 : : * only, as published by the Free Software Foundation.
15 : : *
16 : : * OpenOffice.org is distributed in the hope that it will be useful,
17 : : * but WITHOUT ANY WARRANTY; without even the implied warranty of
18 : : * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
19 : : * GNU Lesser General Public License version 3 for more details
20 : : * (a copy is included in the LICENSE file that accompanied this code).
21 : : *
22 : : * You should have received a copy of the GNU Lesser General Public License
23 : : * version 3 along with OpenOffice.org. If not, see
24 : : * <http://www.openoffice.org/license.html>
25 : : * for a copy of the LGPLv3 License.
26 : : *
27 : : ************************************************************************/
28 : :
29 : : #ifndef OOX_XLS_DEFINEDNAMESBUFFER_HXX
30 : : #define OOX_XLS_DEFINEDNAMESBUFFER_HXX
31 : :
32 : : #include "formulabase.hxx"
33 : : #include "rangenam.hxx"
34 : :
35 : : namespace com { namespace sun { namespace star {
36 : : namespace sheet { class XNamedRange; }
37 : : } } }
38 : :
39 : : namespace oox {
40 : : namespace xls {
41 : :
42 : : class BiffInputStreamPos;
43 : :
44 : : // ============================================================================
45 : :
46 : : // codes for built-in names
47 : : const sal_Unicode BIFF_DEFNAME_CONSOLIDATEAREA = '\x00';
48 : : const sal_Unicode BIFF_DEFNAME_AUTOOPEN = '\x01'; // Sheet macro executed when workbook is opened.
49 : : const sal_Unicode BIFF_DEFNAME_AUTOCLOSE = '\x02'; // Sheet macro executed when workbook is closed.
50 : : const sal_Unicode BIFF_DEFNAME_EXTRACT = '\x03'; // Filter output destination for advanced filter.
51 : : const sal_Unicode BIFF_DEFNAME_DATABASE = '\x04';
52 : : const sal_Unicode BIFF_DEFNAME_CRITERIA = '\x05'; // Filter criteria source range for advanced filter.
53 : : const sal_Unicode BIFF_DEFNAME_PRINTAREA = '\x06'; // Print ranges.
54 : : const sal_Unicode BIFF_DEFNAME_PRINTTITLES = '\x07'; // Rows/columns repeated on each page when printing.
55 : : const sal_Unicode BIFF_DEFNAME_RECORDER = '\x08';
56 : : const sal_Unicode BIFF_DEFNAME_DATAFORM = '\x09';
57 : : const sal_Unicode BIFF_DEFNAME_AUTOACTIVATE = '\x0A'; // Sheet macro executed when workbook is activated.
58 : : const sal_Unicode BIFF_DEFNAME_AUTODEACTIVATE = '\x0B'; // Sheet macro executed when workbook is deactivated.
59 : : const sal_Unicode BIFF_DEFNAME_SHEETTITLE = '\x0C';
60 : : const sal_Unicode BIFF_DEFNAME_FILTERDATABASE = '\x0D'; // Sheet range autofilter or advanced filter works on.
61 : : const sal_Unicode BIFF_DEFNAME_UNKNOWN = '\x0E';
62 : :
63 : : // ============================================================================
64 : :
65 : 33 : struct DefinedNameModel
66 : : {
67 : : ::rtl::OUString maName; /// The original name.
68 : : ::rtl::OUString maFormula; /// The formula string.
69 : : sal_Int32 mnSheet; /// Sheet index for local names.
70 : : sal_Int32 mnFuncGroupId; /// Function group identifier.
71 : : bool mbMacro; /// True = Macro name (VBA or sheet macro).
72 : : bool mbFunction; /// True = function, false = command.
73 : : bool mbVBName; /// True = VBA macro, false = sheet macro.
74 : : bool mbHidden; /// True = name hidden in UI.
75 : :
76 : : explicit DefinedNameModel();
77 : : };
78 : :
79 : : // ============================================================================
80 : :
81 : : /** Base class for defined names and external names. */
82 [ - + ]: 33 : class DefinedNameBase : public WorkbookHelper
83 : : {
84 : : public:
85 : : explicit DefinedNameBase( const WorkbookHelper& rHelper );
86 : :
87 : : /** Returns the original name as imported from or exported to the file. */
88 : 0 : inline const ::rtl::OUString& getModelName() const { return maModel.maName; }
89 : : /** Returns the name as used in the Calc document. */
90 : 0 : inline const ::rtl::OUString& getCalcName() const { return maCalcName; }
91 : :
92 : : /** Returns the original name as imported from or exported to the file. */
93 : : const ::rtl::OUString& getUpcaseModelName() const;
94 : : /** Returns an Any with a SingleReference or ComplexReference, or an empty Any. */
95 : : ::com::sun::star::uno::Any getReference( const ::com::sun::star::table::CellAddress& rBaseAddr ) const;
96 : :
97 : : protected:
98 : : /** Converts the OOXML formula string stored in the own model. */
99 : : ApiTokenSequence importOoxFormula( sal_Int16 nBaseSheet );
100 : : /** Imports the BIFF12 formula from the passed stream. */
101 : : ApiTokenSequence importBiff12Formula( sal_Int16 nBaseSheet, SequenceInputStream& rStrm );
102 : : /** Imports the BIFF formula from the passed stream. */
103 : : ApiTokenSequence importBiffFormula( sal_Int16 nBaseSheet, BiffInputStream& rStrm, const sal_uInt16* pnFmlaSize = 0 );
104 : :
105 : : protected:
106 : : DefinedNameModel maModel; /// Model data for this defined name.
107 : : mutable ::rtl::OUString maUpModelName; /// Model name converted to uppercase ASCII.
108 : : ::rtl::OUString maCalcName; /// Final name used in the Calc document.
109 : : ::com::sun::star::uno::Any maRefAny; /// Single cell/range reference.
110 : : };
111 : :
112 : : // ============================================================================
113 : :
114 [ + - ][ - + ]: 66 : class DefinedName : public DefinedNameBase
115 : : {
116 : : public:
117 : : explicit DefinedName( const WorkbookHelper& rHelper );
118 : :
119 : : /** Sets the attributes for this defined name from the passed attribute set. */
120 : : void importDefinedName( const AttributeList& rAttribs );
121 : : /** Sets the formula string from the body of the definedName element. */
122 : : void setFormula( const ::rtl::OUString& rFormula );
123 : : /** Imports the defined name from a DEFINEDNAME record in the passed stream. */
124 : : void importDefinedName( SequenceInputStream& rStrm );
125 : :
126 : : /** Creates a defined name in the Calc document. */
127 : : void createNameObject( sal_Int32 nIndex );
128 : : /** Converts the formula string or BIFF token array for this defined name. */
129 : : void convertFormula();
130 : : ApiTokenSequence getTokens();
131 : : /** Returns true, if this defined name is global in the document. */
132 : 99 : inline bool isGlobalName() const { return mnCalcSheet < 0; }
133 : : /** Returns true, if this defined name is a special builtin name. */
134 : 48 : inline bool isBuiltinName() const { return mcBuiltinId != BIFF_DEFNAME_UNKNOWN; }
135 : : /** Returns true, if this defined name is a macro function call. */
136 [ # # ][ # # ]: 0 : inline bool isMacroFunction() const { return maModel.mbMacro && maModel.mbFunction; }
137 : : /** Returns true, if this defined name is a reference to a VBA macro. */
138 [ # # ][ # # ]: 0 : inline bool isVBName() const { return maModel.mbMacro && maModel.mbVBName; }
139 : :
140 : : /** Returns the 0-based sheet index for local names, or -1 for global names. */
141 : 36 : inline sal_Int16 getLocalCalcSheet() const { return mnCalcSheet; }
142 : : /** Returns the built-in identifier of the defined name. */
143 : 3 : inline sal_Unicode getBuiltinId() const { return mcBuiltinId; }
144 : : /** Returns the token index used in API token arrays (com.sun.star.sheet.FormulaToken). */
145 : 33 : inline sal_Int32 getTokenIndex() const { return mnTokenIndex; }
146 : : /** Tries to resolve the defined name to an absolute cell range. */
147 : : bool getAbsoluteRange( ::com::sun::star::table::CellRangeAddress& orRange ) const;
148 : :
149 : : private:
150 : : /** Imports the OOXML or BIFF12 definition of the name. */
151 : : void implImportOoxFormula();
152 : : /** Imports the BIFF definition of the name. */
153 : : void implImportBiffFormula();
154 : :
155 : : private:
156 : : typedef ::std::auto_ptr< StreamDataSequence > StreamDataSeqPtr;
157 : : typedef ::std::auto_ptr< BiffInputStreamPos > BiffStreamPosPtr;
158 : :
159 : : ScRangeData* mpScRangeData; /// ScRangeData of the defined name.
160 : : sal_Int32 mnTokenIndex; /// Name index used in API token array.
161 : : sal_Int16 mnCalcSheet; /// Calc sheet index for sheet-local names.
162 : : sal_Unicode mcBuiltinId; /// Identifier for built-in defined names.
163 : : StreamDataSeqPtr mxFormula; /// Formula data for BIFF12 import.
164 : : BiffStreamPosPtr mxBiffStrm; /// Cached BIFF stream for formula import.
165 : : sal_uInt16 mnFmlaSize; /// Cached BIFF formula size for formula import.
166 : : };
167 : :
168 : : typedef ::boost::shared_ptr< DefinedName > DefinedNameRef;
169 : :
170 : : // ============================================================================
171 : :
172 [ - + ]: 48 : class DefinedNamesBuffer : public WorkbookHelper
173 : : {
174 : : public:
175 : : explicit DefinedNamesBuffer( const WorkbookHelper& rHelper );
176 : :
177 : : /** Imports a defined name from the passed attribute set. */
178 : : DefinedNameRef importDefinedName( const AttributeList& rAttribs );
179 : : /** Imports a defined name from a DEFINEDNAME record in the passed stream. */
180 : : void importDefinedName( SequenceInputStream& rStrm );
181 : :
182 : : /** Creates all defined names in the document. */
183 : : void finalizeImport();
184 : :
185 : : /** Returns a defined name by zero-based index (order of appearance). */
186 : : DefinedNameRef getByIndex( sal_Int32 nIndex ) const;
187 : : /** Returns a defined name by token index (index in XDefinedNames container). */
188 : : DefinedNameRef getByTokenIndex( sal_Int32 nIndex ) const;
189 : : /** Returns a defined name by its model name.
190 : : @param nSheet The sheet index for local names or -1 for global names.
191 : : If no local name is found, tries to find a matching global name.
192 : : @return Reference to the defined name or empty reference. */
193 : : DefinedNameRef getByModelName( const ::rtl::OUString& rModelName, sal_Int16 nCalcSheet = -1 ) const;
194 : : /** Returns a built-in defined name by its built-in identifier.
195 : : @param nSheet The sheet index of the built-in name.
196 : : @return Reference to the defined name or empty reference. */
197 : : DefinedNameRef getByBuiltinId( sal_Unicode cBuiltinId, sal_Int16 nCalcSheet ) const;
198 : :
199 : : private:
200 : : DefinedNameRef createDefinedName();
201 : :
202 : : private:
203 : : typedef ::std::pair< sal_Int16, ::rtl::OUString > SheetNameKey;
204 : : typedef ::std::pair< sal_Int16, sal_Unicode > BuiltinKey;
205 : :
206 : : typedef RefVector< DefinedName > DefNameVector;
207 : : typedef RefMap< SheetNameKey, DefinedName > DefNameNameMap;
208 : : typedef RefMap< BuiltinKey, DefinedName > DefNameBuiltinMap;
209 : : typedef RefMap< sal_Int32, DefinedName > DefNameTokenIdMap;
210 : :
211 : : DefNameVector maDefNames; /// List of all defined names in insertion order.
212 : : DefNameNameMap maModelNameMap; /// Maps all defined names by sheet index and model name.
213 : : DefNameBuiltinMap maBuiltinMap; /// Maps all defined names by sheet index and built-in identifier.
214 : : DefNameTokenIdMap maTokenIdMap; /// Maps all defined names by API token index.
215 : : sal_Int16 mnCalcSheet; /// Current sheet index for BIFF2-BIFF4 names (always sheet-local).
216 : : };
217 : :
218 : : // ============================================================================
219 : :
220 : : } // namespace xls
221 : : } // namespace oox
222 : :
223 : : #endif
224 : :
225 : : /* vim:set shiftwidth=4 softtabstop=4 expandtab: */
|