]> git.lyx.org Git - features.git/blob - src/TocBackend.h
Amend d5a5fbb8e (cosmetic)
[features.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         SENSELESS,//"senseless"
60         TOC_TYPE_COUNT
61 }
62  */
63
64 ///
65 /**
66 */
67 class TocItem
68 {
69         friend class Toc;
70         friend class TocBackend;
71         friend class TocBuilder;
72
73 public:
74         /// Default constructor for STL containers.
75         TocItem() : dit_(0), depth_(0), output_(false) {}
76         ///
77         TocItem(DocIterator const & dit,
78                 int depth,
79                 docstring const & s,
80                 bool output_active,
81                 docstring const & t = docstring(),
82                 FuncRequest action = FuncRequest(LFUN_UNKNOWN_ACTION)
83                 );
84         ///
85         ~TocItem() {}
86         ///
87         int id() const;
88         ///
89         int depth() const { return depth_; }
90         ///
91         docstring const & str() const { return str_; }
92         ///
93         void str(docstring const & s) { str_ = s; }
94         ///
95         docstring const & tooltip() const;
96         /// String for display, e.g. it has a mark if output is inactive
97         docstring const asString() const;
98         ///
99         DocIterator const & dit() const { return dit_; }
100         ///
101         bool isOutput() const { return output_; }
102         ///
103         void setAction(FuncRequest a) { action_ = a; }
104         /// custom action, or the default one (paragraph-goto) if not customised
105         FuncRequest action() const;
106
107 protected:
108         /// Current position of item.
109         DocIterator dit_;
110
111 private:
112         /// nesting depth
113         int depth_;
114         /// Full item string
115         docstring str_;
116         /// The tooltip string
117         docstring tooltip_;
118         /// Is this item in a note, inactive branch, etc?
119         bool output_;
120         /// Custom action
121         FuncRequest action_;
122 };
123
124
125 ///
126 class Toc : public std::vector<TocItem>
127 {
128 public:
129         typedef std::vector<TocItem>::const_iterator const_iterator;
130         typedef std::vector<TocItem>::iterator iterator;
131         const_iterator item(DocIterator const & dit) const;
132         /// Look for a TocItem given its depth and string.
133         /// \return The first matching item.
134         /// \retval end() if no item was found.
135         iterator item(int depth, docstring const & str);
136 };
137
138 typedef Toc::const_iterator TocIterator;
139
140
141 /// Caption-enabled TOC builders
142 class TocBuilder
143 {
144 public:
145         TocBuilder(shared_ptr<Toc> const toc);
146         /// When entering a float
147         void pushItem(DocIterator const & dit, docstring const & s,
148                                   bool output_active, bool is_captioned = false);
149         /// When encountering a caption
150         void captionItem(DocIterator const & dit, docstring const & s,
151                                          bool output_active);
152         /// When exiting a float
153         void pop();
154 private:
155         TocBuilder(){}
156         ///
157         struct frame {
158                 Toc::size_type pos;
159                 bool is_captioned;
160         };
161         ///
162         shared_ptr<Toc> const toc_;
163         ///
164         std::stack<frame> stack_;
165 };
166
167
168 /// The ToC list.
169 /// A class and no typedef because we want to forward declare it.
170 class TocList : public std::map<std::string, shared_ptr<Toc> >
171 {
172 private:
173         // this can create null pointers
174         using std::map<std::string, shared_ptr<Toc> >::operator[];
175 };
176
177
178 ///
179 class TocBuilderStore
180 {
181 public:
182         TocBuilderStore() {};
183         ///
184         shared_ptr<TocBuilder> get(std::string const & type, shared_ptr<Toc> toc);
185         ///
186         void clear() { map_.clear(); };
187 private:
188         typedef std::map<std::string, shared_ptr<TocBuilder> > map_t;
189         map_t map_;
190 };
191
192
193 ///
194 /**
195 */
196 class TocBackend
197 {
198 public:
199         ///
200         TocBackend(Buffer const * buffer) : buffer_(buffer) {}
201         ///
202         void setBuffer(Buffer const * buffer) { buffer_ = buffer; }
203         ///
204         void update(bool output_active, UpdateType utype);
205         /// \return true if the item was updated.
206         bool updateItem(DocIterator const & pit);
207         ///
208         TocList const & tocs() const { return tocs_; }
209         /// never null
210         shared_ptr<Toc const> toc(std::string const & type) const;
211         shared_ptr<Toc> toc(std::string const & type);
212         /// nevel null
213         shared_ptr<TocBuilder> builder(std::string const & type);
214         /// Return the first Toc Item before the cursor
215         TocIterator item(
216                 std::string const & type, ///< Type of Toc.
217                 DocIterator const & dit ///< The cursor location in the document.
218         ) const;
219
220         ///
221         void writePlaintextTocList(std::string const & type,
222                 odocstringstream & os, size_t max_length) const;
223
224 private:
225         ///
226         TocList tocs_;
227         ///
228         TocBuilderStore builders_;
229         ///
230         Buffer const * buffer_;
231 }; // TocBackend
232
233
234 } // namespace lyx
235
236 #endif // TOC_BACKEND_H