]> git.lyx.org Git - lyx.git/blob - src/TocBackend.h
1e58ec2f344482c4c7728cafac2069491303ea4b
[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 /* Toc types are described by strings. They cannot be converted into an enum
34  * because of the user-configurable categories for index and the user-definable
35  * toc types provided in layout files.
36  *
37  * Here is a summary of built-in toc types
38  *
39  * Non-customizable (used without TocBuilder): "tableofcontents", "change",
40  * "citation", "label", "senseless".
41  *
42  * Built-in but customizable (used with TocBuilder): "child", "graphics",
43  * "equation", "index", "index:<user-str>", "nomencl", "listings", "math-macro",
44  * "external", any float type (as defined in the layouts).
45  *
46  * The following are used for XHTML output: "tableofcontents" (InsetText),
47  * "citation" (InsetCitation), any float type.
48  *
49  * Other types are defined in the layouts.
50  */
51
52 ///
53 /**
54 */
55 class TocItem
56 {
57         friend class TocBackend;
58         friend class TocBuilder;
59
60 public:
61         /// Default constructor for STL containers.
62         TocItem() : dit_(0), depth_(0), output_(false) {}
63         ///
64         TocItem(DocIterator const & dit,
65                 int depth,
66                 docstring const & s,
67                 bool output_active,
68                 FuncRequest action = FuncRequest(LFUN_UNKNOWN_ACTION)
69                 );
70         ///
71         ~TocItem() {}
72         ///
73         int id() const;
74         ///
75         int depth() const { return depth_; }
76         ///
77         docstring const & str() const { return str_; }
78         ///
79         void str(docstring const & s) { str_ = s; }
80         /// String for display, e.g. it has a mark if output is inactive
81         docstring const asString() const;
82         ///
83         DocIterator const & dit() const { return dit_; }
84         ///
85         bool isOutput() const { return output_; }
86         ///
87         void setAction(FuncRequest a) { action_ = a; }
88         /// custom action, or the default one (paragraph-goto) if not customised
89         FuncRequest action() const;
90
91 protected:
92         /// Current position of item.
93         DocIterator dit_;
94
95 private:
96         /// nesting depth
97         int depth_;
98         /// Full item string
99         docstring str_;
100         /// Is this item in a note, inactive branch, etc?
101         bool output_;
102         /// Custom action
103         FuncRequest action_;
104 };
105
106
107 /// Caption-enabled TOC builders
108 class TocBuilder
109 {
110 public:
111         TocBuilder(std::shared_ptr<Toc> toc);
112         /// When entering a float or flex or paragraph (with AddToToc)
113         void pushItem(DocIterator const & dit, docstring const & s,
114                       bool output_active, bool is_captioned = false);
115         /// When encountering a float caption
116         void captionItem(DocIterator const & dit, docstring const & s,
117                          bool output_active);
118         /// When encountering an argument (with isTocCaption) for flex or paragraph
119         void argumentItem(docstring const & arg_str);
120         /// When exiting a float or flex or paragraph
121         void pop();
122 private:
123         TocBuilder(){}
124         ///
125         struct frame {
126                 Toc::size_type pos;
127                 bool is_captioned;
128         };
129         ///
130         std::shared_ptr<Toc> const toc_;
131         ///
132         std::stack<frame> stack_;
133 };
134
135
136 /// Class to build and access the Tocs of a particular buffer.
137 class TocBackend
138 {
139 public:
140         static Toc::const_iterator findItem(Toc const & toc,
141                                             DocIterator const & dit);
142         /// Look for a TocItem given its depth and string.
143         /// \return The first matching item.
144         /// \retval end() if no item was found.
145         static Toc::iterator findItem(Toc & toc, int depth, docstring const & str);
146         ///
147         TocBackend(Buffer const * buffer) : buffer_(buffer) {}
148         ///
149         void setBuffer(Buffer const * buffer) { buffer_ = buffer; }
150         ///
151         void update(bool output_active, UpdateType utype);
152         /// \return true if the item was updated.
153         bool updateItem(DocIterator const & pit);
154         ///
155         TocList const & tocs() const { return tocs_; }
156         /// never null
157         std::shared_ptr<Toc const> toc(std::string const & type) const;
158         /// never null
159         std::shared_ptr<Toc> toc(std::string const & type);
160         /// \return the current TocBuilder for the Toc of type \param type, or
161         /// creates one if it does not already exist.
162         TocBuilder & builder(std::string const & type);
163         /// \return the first Toc Item before the cursor.
164         /// \param type: Type of Toc.
165         /// \param dit: The cursor location in the document.
166         Toc::const_iterator
167         item(std::string const & type, DocIterator const & dit) const;
168
169         ///
170         void writePlaintextTocList(std::string const & type,
171                 odocstringstream & os, size_t max_length) const;
172         ///
173         docstring outlinerName(std::string const & type) const;
174         /// Whether a toc type is less important and appears in the "Other lists"
175         /// submenu
176         static bool isOther(std::string const & type);
177
178 private:
179         ///
180         TocList tocs_;
181         ///
182         std::map<std::string, unique_ptr<TocBuilder>> builders_;
183         ///
184         Buffer const * buffer_;
185 }; // TocBackend
186
187
188 } // namespace lyx
189
190 #endif // TOC_BACKEND_H