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