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 INCLUDED_SC_SOURCE_FILTER_INC_DEFNAMESBUFFER_HXX
21 #define INCLUDED_SC_SOURCE_FILTER_INC_DEFNAMESBUFFER_HXX
23 #include "workbookhelper.hxx"
24 #include <oox/helper/binarystreambase.hxx>
25 #include <oox/helper/refvector.hxx>
31 namespace oox
{ class AttributeList
; }
32 namespace oox
{ class SequenceInputStream
; }
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 struct DefinedNameModel
56 OUString maName
; /// The original name.
57 OUString maFormula
; /// The formula string.
58 sal_Int32 mnSheet
; /// Sheet index for local names.
59 sal_Int32 mnFuncGroupId
; /// Function group identifier.
60 bool mbMacro
; /// True = Macro name (VBA or sheet macro).
61 bool mbFunction
; /// True = function, false = command.
62 bool mbVBName
; /// True = VBA macro, false = sheet macro.
63 bool mbHidden
; /// True = name hidden in UI.
65 explicit DefinedNameModel();
68 /** Base class for defined names and external names. */
69 class DefinedNameBase
: public WorkbookHelper
72 explicit DefinedNameBase( const WorkbookHelper
& rHelper
);
74 /** Returns the original name as imported from or exported to the file. */
75 const OUString
& getModelName() const { return maModel
.maName
; }
76 /** Returns the name as used in the Calc document. */
77 const OUString
& getCalcName() const { return maCalcName
; }
79 /** Returns the original name as imported from or exported to the file. */
80 const OUString
& getUpcaseModelName() const;
83 DefinedNameModel maModel
; /// Model data for this defined name.
84 mutable OUString maUpModelName
; /// Model name converted to uppercase ASCII.
85 OUString maCalcName
; /// Final name used in the Calc document.
88 class DefinedName
: public DefinedNameBase
91 explicit DefinedName( const WorkbookHelper
& rHelper
);
93 /** Sets the attributes for this defined name from the passed attribute set. */
94 void importDefinedName( const AttributeList
& rAttribs
);
95 /** Sets the formula string from the body of the definedName element. */
96 void setFormula( const OUString
& rFormula
);
97 /** Imports the defined name from a DEFINEDNAME record in the passed stream. */
98 void importDefinedName( SequenceInputStream
& rStrm
);
100 /** Creates a defined name in the Calc document. */
101 void createNameObject( sal_Int32 nIndex
);
102 /** Converts the formula string or BIFF token array for this defined name. */
103 void convertFormula( const css::uno::Sequence
<css::sheet::ExternalLinkInfo
>& rExternalLinks
);
104 std::unique_ptr
<ScTokenArray
> getScTokens( const css::uno::Sequence
<css::sheet::ExternalLinkInfo
>& rExternalLinks
);
105 /** Returns true, if this defined name is global in the document. */
106 bool isGlobalName() const { return mnCalcSheet
< 0; }
107 /** Returns true, if this defined name is a special builtin name. */
108 bool isBuiltinName() const { return mcBuiltinId
!= BIFF_DEFNAME_UNKNOWN
; }
109 /** Returns true, if this defined name is a macro function call. */
110 bool isMacroFunction() const { return maModel
.mbMacro
&& maModel
.mbFunction
; }
111 /** Returns true, if this defined name is a reference to a VBA macro. */
112 bool isVBName() const { return maModel
.mbMacro
&& maModel
.mbVBName
; }
114 /** Returns the 0-based sheet index for local names, or -1 for global names. */
115 sal_Int16
getLocalCalcSheet() const { return mnCalcSheet
; }
116 /** Returns the built-in identifier of the defined name. */
117 sal_Unicode
getBuiltinId() const { return mcBuiltinId
; }
118 /** Returns the token index used in API token arrays (com.sun.star.sheet.FormulaToken). */
119 sal_Int32
getTokenIndex() const { return mnTokenIndex
; }
120 /** Tries to resolve the defined name to an absolute cell range. */
121 bool getAbsoluteRange( ScRange
& orRange
) const;
124 typedef ::std::unique_ptr
< StreamDataSequence
> StreamDataSeqPtr
;
126 ScRangeData
* mpScRangeData
; /// ScRangeData of the defined name.
127 sal_Int32 mnTokenIndex
; /// Name index used in API token array.
128 sal_Int16 mnCalcSheet
; /// Calc sheet index for sheet-local names.
129 sal_Unicode mcBuiltinId
; /// Identifier for built-in defined names.
130 StreamDataSeqPtr mxFormula
; /// Formula data for BIFF12 import.
133 typedef std::shared_ptr
< DefinedName
> DefinedNameRef
;
135 class DefinedNamesBuffer
: public WorkbookHelper
138 explicit DefinedNamesBuffer( const WorkbookHelper
& rHelper
);
140 /** Imports a defined name from the passed attribute set. */
141 DefinedNameRef
importDefinedName( const AttributeList
& rAttribs
);
142 /** Imports a defined name from a DEFINEDNAME record in the passed stream. */
143 void importDefinedName( SequenceInputStream
& rStrm
);
145 /** Creates all defined names in the document. */
146 void finalizeImport();
148 /** Returns a defined name by zero-based index (order of appearance). */
149 DefinedNameRef
getByIndex( sal_Int32 nIndex
) const;
150 /** Returns a defined name by token index (index in XDefinedNames container). */
151 DefinedNameRef
getByTokenIndex( sal_Int32 nIndex
) const;
152 /** Returns a defined name by its model name.
153 @param nSheet The sheet index for local names or -1 for global names.
154 If no local name is found, tries to find a matching global name.
155 @return Reference to the defined name or empty reference. */
156 DefinedNameRef
getByModelName( const OUString
& rModelName
, sal_Int16 nCalcSheet
= -1 ) const;
157 /** Returns a built-in defined name by its built-in identifier.
158 @param nSheet The sheet index of the built-in name.
159 @return Reference to the defined name or empty reference. */
160 DefinedNameRef
getByBuiltinId( sal_Unicode cBuiltinId
, sal_Int16 nCalcSheet
) const;
163 DefinedNameRef
createDefinedName();
166 typedef ::std::pair
< sal_Int16
, OUString
> SheetNameKey
;
167 typedef ::std::pair
< sal_Int16
, sal_Unicode
> BuiltinKey
;
169 typedef RefVector
< DefinedName
> DefNameVector
;
170 typedef RefMap
< SheetNameKey
, DefinedName
> DefNameNameMap
;
171 typedef RefMap
< BuiltinKey
, DefinedName
> DefNameBuiltinMap
;
172 typedef RefMap
< sal_Int32
, DefinedName
> DefNameTokenIdMap
;
174 DefNameVector maDefNames
; /// List of all defined names in insertion order.
175 DefNameNameMap maModelNameMap
; /// Maps all defined names by sheet index and model name.
176 DefNameBuiltinMap maBuiltinMap
; /// Maps all defined names by sheet index and built-in identifier.
177 DefNameTokenIdMap maTokenIdMap
; /// Maps all defined names by API token index.
185 /* vim:set shiftwidth=4 softtabstop=4 expandtab: */