]> git.lyx.org Git - lyx.git/blob - src/TocBackend.h
fix last traces of bug #10068
[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
22 #include "support/shared_ptr.h"
23 #include "support/strfwd.h"
24
25 #include <map>
26 #include <vector>
27 #include <stack>
28 #include <string>
29
30
31 namespace lyx {
32
33 class Buffer;
34
35
36 /* FIXME: toc types are currently identified by strings. It cannot be converted
37  * into an enum because of the user-configurable indexing categories and
38  * the user-definable float types provided by layout files.
39  *
40  * I leave this for documentation purposes for the moment.
41  *
42 enum TocType {
43         TABLE_OF_CONTENTS,//"tableofcontents"
44         CHILD,//"child"
45         GRAPHICS,//"graphics"
46         NOTE,//"note"
47         BRANCH,//"branch"
48         CHANGE,//"change"
49         LABEL,//"label"
50         CITATION,//"citation"
51         EQUATION,//"equation"
52         FOOTNOTE,//"footnote"
53         MARGINAL_NOTE,//"marginalnote"
54         INDEX,//"index", "index:<user-str>" (from interface)
55         NOMENCL,//"nomencl"
56         LISTING,//"listings"
57         FLOAT,//"figure", "table", "algorithm", user-defined (from layout?)
58         MATH_MACRO,//"math-macro"
59         EXTERNAL,//"external"
60         SENSELESS,//"senseless"
61         TOC_TYPE_COUNT
62 }
63  */
64
65 ///
66 /**
67 */
68 class TocItem
69 {
70         friend class Toc;
71         friend class TocBackend;
72         friend class TocBuilder;
73
74 public:
75         /// Default constructor for STL containers.
76         TocItem() : dit_(0), depth_(0), output_(false) {}
77         ///
78         TocItem(DocIterator const & dit,
79                 int depth,
80                 docstring const & s,
81                 bool output_active,
82                 docstring const & t = docstring(),
83                 FuncRequest action = FuncRequest(LFUN_UNKNOWN_ACTION)
84                 );
85         ///
86         ~TocItem() {}
87         ///
88         int id() const;
89         ///
90         int depth() const { return depth_; }
91         ///
92         docstring const & str() const { return str_; }
93         ///
94         void str(docstring const & s) { str_ = s; }
95         ///
96         docstring const & tooltip() const;
97         /// String for display, e.g. it has a mark if output is inactive
98         docstring const asString() const;
99         ///
100         DocIterator const & dit() const { return dit_; }
101         ///
102         bool isOutput() const { return output_; }
103         ///
104         void setAction(FuncRequest a) { action_ = a; }
105         /// custom action, or the default one (paragraph-goto) if not customised
106         FuncRequest action() const;
107
108 protected:
109         /// Current position of item.
110         DocIterator dit_;
111
112 private:
113         /// nesting depth
114         int depth_;
115         /// Full item string
116         docstring str_;
117         /// The tooltip string
118         docstring tooltip_;
119         /// Is this item in a note, inactive branch, etc?
120         bool output_;
121         /// Custom action
122         FuncRequest action_;
123 };
124
125
126 ///
127 class Toc : public std::vector<TocItem>
128 {
129 public:
130         // This is needed to work around a libc++ bug
131         // https://llvm.org/bugs/show_bug.cgi?id=24137
132         Toc() {}
133         typedef std::vector<TocItem>::const_iterator const_iterator;
134         typedef std::vector<TocItem>::iterator iterator;
135         const_iterator item(DocIterator const & dit) const;
136         /// Look for a TocItem given its depth and string.
137         /// \return The first matching item.
138         /// \retval end() if no item was found.
139         iterator item(int depth, docstring const & str);
140 };
141
142 typedef Toc::const_iterator TocIterator;
143
144
145 /// Caption-enabled TOC builders
146 class TocBuilder
147 {
148 public:
149         TocBuilder(shared_ptr<Toc> const toc);
150         /// When entering a float
151         void pushItem(DocIterator const & dit, docstring const & s,
152                                   bool output_active, bool is_captioned = false);
153         /// When encountering a caption
154         void captionItem(DocIterator const & dit, docstring const & s,
155                                          bool output_active);
156         /// When exiting a float
157         void pop();
158 private:
159         TocBuilder(){}
160         ///
161         struct frame {
162                 Toc::size_type pos;
163                 bool is_captioned;
164         };
165         ///
166         shared_ptr<Toc> const toc_;
167         ///
168         std::stack<frame> stack_;
169 };
170
171
172 /// The ToC list.
173 /// A class and no typedef because we want to forward declare it.
174 class TocList : public std::map<std::string, shared_ptr<Toc> >
175 {
176 private:
177         // this can create null pointers
178         using std::map<std::string, shared_ptr<Toc> >::operator[];
179 };
180
181
182 ///
183 class TocBuilderStore
184 {
185 public:
186         TocBuilderStore() {};
187         ///
188         shared_ptr<TocBuilder> get(std::string const & type, shared_ptr<Toc> toc);
189         ///
190         void clear() { map_.clear(); };
191 private:
192         typedef std::map<std::string, shared_ptr<TocBuilder> > map_t;
193         map_t map_;
194 };
195
196
197 ///
198 /**
199 */
200 class TocBackend
201 {
202 public:
203         ///
204         TocBackend(Buffer const * buffer) : buffer_(buffer) {}
205         ///
206         void setBuffer(Buffer const * buffer) { buffer_ = buffer; }
207         ///
208         void update(bool output_active, UpdateType utype);
209         /// \return true if the item was updated.
210         bool updateItem(DocIterator const & pit);
211         ///
212         TocList const & tocs() const { return tocs_; }
213         /// never null
214         shared_ptr<Toc const> toc(std::string const & type) const;
215         shared_ptr<Toc> toc(std::string const & type);
216         /// nevel null
217         shared_ptr<TocBuilder> builder(std::string const & type);
218         /// Return the first Toc Item before the cursor
219         TocIterator item(
220                 std::string const & type, ///< Type of Toc.
221                 DocIterator const & dit ///< The cursor location in the document.
222         ) const;
223
224         ///
225         void writePlaintextTocList(std::string const & type,
226                 odocstringstream & os, size_t max_length) const;
227         ///
228         docstring outlinerName(std::string const & type) const;
229
230 private:
231         ///
232         TocList tocs_;
233         ///
234         TocBuilderStore builders_;
235         ///
236         Buffer const * buffer_;
237 }; // TocBackend
238
239
240 } // namespace lyx
241
242 #endif // TOC_BACKEND_H