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