]> git.lyx.org Git - lyx.git/blob - src/TocBackend.h
Improve the list of equations
[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
23 #include "support/strfwd.h"
24 #include "support/unique_ptr.h"
25
26 #include <stack>
27
28
29 namespace lyx {
30
31 class Buffer;
32
33
34 /* FIXME: toc types are currently identified by strings. It cannot be converted
35  * into an enum because of the user-configurable indexing categories and
36  * the user-definable float types provided by layout files.
37  *
38  * I leave this for documentation purposes for the moment.
39  *
40 enum TocType {
41 // Non-customizable (does not use TocBuilder)
42         //The following is used for XHTML output
43         TABLE_OF_CONTENTS,//"tableofcontents"
44         CHANGE,//"change"
45         //The following is used for XHTML output
46         CITATION,//"citation"
47         LABEL,//"label"
48         SENSELESS,//"senseless"
49 // Built-in but customizable
50         CHILD,//"child"
51         GRAPHICS,//"graphics"
52         EQUATION,//"equation"
53         INDEX,//"index", "index:<user-str>" (from interface)
54         NOMENCL,//"nomencl"
55         LISTING,//"listings"
56         //The following are used for XHTML output
57         FLOAT,//"figure", "table", "algorithm", user-defined (from layout?)
58         MATH_MACRO,//"math-macro"
59         EXTERNAL,//"external"
60 // Defined in layouts
61         NOTE,//"note"
62         FOOTNOTE,//"footnote"
63         MARGINAL_NOTE,//"marginalnote"
64         BRANCH,//"branch"
65         USER_DEFINED //any value defined in the layouts
66 }
67  */
68
69 ///
70 /**
71 */
72 class TocItem
73 {
74         friend class TocBackend;
75         friend class TocBuilder;
76
77 public:
78         /// Default constructor for STL containers.
79         TocItem() : dit_(0), depth_(0), output_(false) {}
80         ///
81         TocItem(DocIterator const & dit,
82                 int depth,
83                 docstring const & s,
84                 bool output_active,
85                 FuncRequest action = FuncRequest(LFUN_UNKNOWN_ACTION)
86                 );
87         ///
88         ~TocItem() {}
89         ///
90         int id() const;
91         ///
92         int depth() const { return depth_; }
93         ///
94         docstring const & str() const { return str_; }
95         ///
96         void str(docstring const & s) { str_ = s; }
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         /// Is this item in a note, inactive branch, etc?
118         bool output_;
119         /// Custom action
120         FuncRequest action_;
121 };
122
123
124 /// Caption-enabled TOC builders
125 class TocBuilder
126 {
127 public:
128         TocBuilder(std::shared_ptr<Toc> toc);
129         /// When entering a float or flex or paragraph (with AddToToc)
130         void pushItem(DocIterator const & dit, docstring const & s,
131                       bool output_active, bool is_captioned = false);
132         /// When encountering a float caption
133         void captionItem(DocIterator const & dit, docstring const & s,
134                          bool output_active);
135         /// When encountering an argument (with isTocCaption) for flex or paragraph
136         void argumentItem(docstring const & arg_str);
137         /// When exiting a float or flex or paragraph
138         void pop();
139 private:
140         TocBuilder(){}
141         ///
142         struct frame {
143                 Toc::size_type pos;
144                 bool is_captioned;
145         };
146         ///
147         std::shared_ptr<Toc> const toc_;
148         ///
149         std::stack<frame> stack_;
150 };
151
152
153 /// Class to build and access the Tocs of a particular buffer.
154 class TocBackend
155 {
156 public:
157         static Toc::const_iterator findItem(Toc const & toc,
158                                             DocIterator const & dit);
159         /// Look for a TocItem given its depth and string.
160         /// \return The first matching item.
161         /// \retval end() if no item was found.
162         static Toc::iterator findItem(Toc & toc, int depth, docstring const & str);
163         ///
164         TocBackend(Buffer const * buffer) : buffer_(buffer) {}
165         ///
166         void setBuffer(Buffer const * buffer) { buffer_ = buffer; }
167         ///
168         void update(bool output_active, UpdateType utype);
169         /// \return true if the item was updated.
170         bool updateItem(DocIterator const & pit);
171         ///
172         TocList const & tocs() const { return tocs_; }
173         /// never null
174         std::shared_ptr<Toc const> toc(std::string const & type) const;
175         /// never null
176         std::shared_ptr<Toc> toc(std::string const & type);
177         /// \return the current TocBuilder for the Toc of type \param type, or
178         /// creates one if it does not already exist.
179         TocBuilder & builder(std::string const & type);
180         /// \return the first Toc Item before the cursor.
181         /// \param type: Type of Toc.
182         /// \param dit: The cursor location in the document.
183         Toc::const_iterator
184         item(std::string const & type, DocIterator const & dit) const;
185
186         ///
187         void writePlaintextTocList(std::string const & type,
188                 odocstringstream & os, size_t max_length) const;
189         ///
190         docstring outlinerName(std::string const & type) const;
191         /// Whether a toc type is less important and appears in the "Other lists"
192         /// submenu
193         static bool isOther(std::string const & type);
194
195 private:
196         ///
197         TocList tocs_;
198         ///
199         std::map<std::string, unique_ptr<TocBuilder>> builders_;
200         ///
201         Buffer const * buffer_;
202 }; // TocBackend
203
204
205 } // namespace lyx
206
207 #endif // TOC_BACKEND_H