]> git.lyx.org Git - lyx.git/blob - src/TocBackend.h
Merge branch 'master' of git.lyx.org:lyx
[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 using std::shared_ptr;
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         MATH_MACRO,//"math-macro"
59         EXTERNAL,//"external"
60         SENSELESS,//"senseless"
61         TOC_TYPE_COUNT
62 }
63  */
64
65 ///
66 /**
67 */
68 class TocItem
69 {
70         friend class TocBackend;
71         friend class TocBuilder;
72
73 public:
74         /// Default constructor for STL containers.
75         TocItem() : dit_(0), depth_(0), output_(false) {}
76         ///
77         TocItem(DocIterator const & dit,
78                 int depth,
79                 docstring const & s,
80                 bool output_active,
81                 docstring const & t = docstring(),
82                 FuncRequest action = FuncRequest(LFUN_UNKNOWN_ACTION));
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 /// Caption-enabled TOC builders
125 class TocBuilder
126 {
127 public:
128         TocBuilder(shared_ptr<Toc> const toc);
129         /// When entering a float
130         void pushItem(DocIterator const & dit, docstring const & s,
131                       bool output_active, bool is_captioned = false);
132         /// When encountering a caption
133         void captionItem(DocIterator const & dit, docstring const & s,
134                          bool output_active);
135         /// When exiting a float
136         void pop();
137 private:
138         TocBuilder(){}
139         ///
140         struct frame {
141                 Toc::size_type pos;
142                 bool is_captioned;
143         };
144         ///
145         shared_ptr<Toc> const toc_;
146         ///
147         std::stack<frame> stack_;
148 };
149
150
151 /// Class to build and access the Tocs of a particular buffer.
152 class TocBackend
153 {
154 public:
155         static Toc::const_iterator findItem(Toc const & toc,
156                                             DocIterator const & dit);
157         /// Look for a TocItem given its depth and string.
158         /// \return The first matching item.
159         /// \retval end() if no item was found.
160         static Toc::iterator findItem(Toc & toc, int depth, docstring const & str);
161         ///
162         TocBackend(Buffer const * buffer) : buffer_(buffer) {}
163         ///
164         void setBuffer(Buffer const * buffer) { buffer_ = buffer; }
165         ///
166         void update(bool output_active, UpdateType utype);
167         /// \return true if the item was updated.
168         bool updateItem(DocIterator const & pit);
169         ///
170         TocList const & tocs() const { return tocs_; }
171         /// never null
172         shared_ptr<Toc const> toc(std::string const & type) const;
173         /// never null
174         shared_ptr<Toc> toc(std::string const & type);
175         /// \return the current TocBuilder for the Toc of type \param type, or
176         /// creates one if it does not already exist.
177         TocBuilder & builder(std::string const & type);
178         /// \return the first Toc Item before the cursor.
179         /// \param type: Type of Toc.
180         /// \param dit: The cursor location in the document.
181         Toc::const_iterator
182         item(std::string const & type, DocIterator const & dit) const;
183
184         ///
185         void writePlaintextTocList(std::string const & type,
186                 odocstringstream & os, size_t max_length) const;
187         ///
188         docstring outlinerName(std::string const & type) const;
189
190 private:
191         ///
192         TocList tocs_;
193         ///
194         std::map<std::string, unique_ptr<TocBuilder>> builders_;
195         ///
196         Buffer const * buffer_;
197 }; // TocBackend
198
199
200 } // namespace lyx
201
202 #endif // TOC_BACKEND_H