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