]> git.lyx.org Git - lyx.git/blob - src/TocBackend.h
Avoid full metrics computation with Update:FitCursor
[lyx.git] / src / TocBackend.h
1 // -*- C++ -*-
2 /**
3  * \file TocBackend.h
4  * This file is part of LyX, the document processor.
5  * Licence details can be found in the file COPYING.
6  *
7  * \author Jean-Marc Lasgouttes
8  * \author Angus Leeming
9  * \author Abdelrazak Younes
10  * \author Guillaume Munch
11  *
12  * Full author contact details are available in file CREDITS.
13  */
14
15 #ifndef TOC_BACKEND_H
16 #define TOC_BACKEND_H
17
18 #include "DocIterator.h"
19 #include "FuncRequest.h"
20 #include "OutputEnums.h"
21 #include "Toc.h"
22 #include "TocBuilder.h"
23
24 #include "support/strfwd.h"
25 #include "support/unique_ptr.h"
26
27
28 namespace lyx {
29
30 class Buffer;
31
32 /* Toc types are described by strings. They cannot be converted into an enum
33  * because of the user-configurable categories for index and the user-definable
34  * toc types provided in layout files.
35  *
36  * Here is a summary of built-in toc types
37  *
38  * Non-customizable (used without TocBuilder): "tableofcontents", "change",
39  * "citation", "label", "senseless".
40  *
41  * Built-in but customizable (used with TocBuilder): "child", "graphics",
42  * "equation", "index", "index:<user-str>", "nomencl", "listings", "math-macro",
43  * "external", any float type (as defined in the layouts).
44  *
45  * The following are used for XHTML output: "tableofcontents" (InsetText),
46  * "citation" (InsetCitation), any float type.
47  *
48  * Other types are defined in the layouts.
49  */
50
51 ///
52 /**
53 */
54 class TocItem
55 {
56 public:
57         /// Default constructor for STL containers.
58         TocItem() : dit_(0), depth_(0), output_(false), missing_(false) {}
59         ///
60         TocItem(DocIterator const & dit,
61                 int depth,
62                 docstring const & s,
63                 bool output_active,
64                 bool missing = false,
65                 FuncRequest const & action = FuncRequest(LFUN_UNKNOWN_ACTION)
66                 );
67         ///
68         DocIterator const & dit() const { return dit_; }
69         ///
70         int depth() const { return depth_; }
71         ///
72         docstring const & str() const { return str_; }
73         ///
74         void str(docstring const & s) { str_ = s; }
75         ///
76         docstring const & prettyStr() const { return pretty_str_; }
77         ///
78         void prettyStr (docstring const & s) { pretty_str_ = s; }
79         ///
80         bool isOutput() const { return output_; }
81         ///
82         bool isMissing() const { return missing_; }
83         ///
84         void setAction(FuncRequest const & a) { action_ = a; }
85
86         /// custom action, or the default one (paragraph-goto) if not customised
87         FuncRequest action() const;
88         ///
89         int id() const;
90         /// String for display, e.g. it has a mark if output is inactive
91         docstring const asString() const;
92
93 private:
94         /// Current position of item.
95         DocIterator dit_;
96         /// nesting depth
97         int depth_;
98         /// Full item string
99         docstring str_;
100         /// Dereferenced name, for labels (e.g. Label 5.2 instead of lem:foobar)
101         docstring pretty_str_;
102         /// Is this item in a note, inactive branch, etc?
103         bool output_;
104         /// Is this item missing, e.g. missing label?
105         bool missing_;
106         /// Custom action
107         FuncRequest action_;
108 };
109
110
111 /// Class to build and access the Tocs of a particular buffer.
112 class TocBackend
113 {
114 public:
115         static Toc::const_iterator findItem(Toc const & toc,
116                                             DocIterator const & dit);
117         /// Look for a TocItem given its depth and string.
118         /// \return The first matching item.
119         /// \retval end() if no item was found.
120         static Toc::iterator findItem(Toc & toc, int depth, docstring const & str);
121         ///
122         TocBackend(Buffer const * buffer) : buffer_(buffer) {}
123         ///
124         void setBuffer(Buffer const * buffer) { buffer_ = buffer; }
125         ///
126         void update(bool output_active, UpdateType utype);
127         ///
128         void reset();
129         /// \return true if the item was updated.
130         bool updateItem(DocIterator const & pit) const;
131         ///
132         TocList const & tocs() const { return tocs_; }
133         /// never null
134         std::shared_ptr<Toc const> toc(std::string const & type) const;
135         /// never null
136         std::shared_ptr<Toc> toc(std::string const & type);
137         /// \return the current TocBuilder for the Toc of type \param type, or
138         /// creates one if it does not already exist.
139         TocBuilder & builder(std::string const & type);
140         /// \return the first Toc Item before the cursor.
141         /// \param type: Type of Toc.
142         /// \param dit: The cursor location in the document.
143         Toc::const_iterator
144         item(std::string const & type, DocIterator const & dit) const;
145
146         ///
147         void writePlaintextTocList(std::string const & type,
148                 odocstringstream & os, size_t max_length) const;
149         /// Localised name for type
150         docstring outlinerName(std::string const & type) const;
151         /// Add a new (localised) name if yet unknown
152         void addName(std::string const & type, docstring const & name);
153         /// Whether a toc type is less important and appears in the "Other lists"
154         /// submenu
155         static bool isOther(std::string const & type);
156
157 private:
158         ///
159         void resetOutlinerNames();
160         ///
161         TocList tocs_;
162         ///
163     std::map<std::string, std::unique_ptr<TocBuilder>> builders_;
164         /// Stores localised outliner names from this buffer and its children
165         std::map<std::string, docstring> outliner_names_;
166         ///
167         Buffer const * buffer_;
168 }; // TocBackend
169
170
171 } // namespace lyx
172
173 #endif // TOC_BACKEND_H