]> git.lyx.org Git - lyx.git/blob - src/TocBackend.h
08754b90b0af2ecb5f26c02bca2c52b6e18c3aba
[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         USER_DEFINED,//any value defined in the layouts
60         TOC_TYPE_COUNT
61 }
62  */
63
64 ///
65 /**
66 */
67 class TocItem
68 {
69         friend class TocBackend;
70         friend class TocBuilder;
71
72 public:
73         /// Default constructor for STL containers.
74         TocItem() : dit_(0), depth_(0), output_(false) {}
75         ///
76         TocItem(DocIterator const & dit,
77                 int depth,
78                 docstring const & s,
79                 bool output_active,
80                 FuncRequest action = FuncRequest(LFUN_UNKNOWN_ACTION)
81                 );
82         ///
83         ~TocItem() {}
84         ///
85         int id() const;
86         ///
87         int depth() const { return depth_; }
88         ///
89         docstring const & str() const { return str_; }
90         ///
91         void str(docstring const & s) { str_ = s; }
92         /// String for display, e.g. it has a mark if output is inactive
93         docstring const asString() const;
94         ///
95         DocIterator const & dit() const { return dit_; }
96         ///
97         bool isOutput() const { return output_; }
98         ///
99         void setAction(FuncRequest a) { action_ = a; }
100         /// custom action, or the default one (paragraph-goto) if not customised
101         FuncRequest action() const;
102
103 protected:
104         /// Current position of item.
105         DocIterator dit_;
106
107 private:
108         /// nesting depth
109         int depth_;
110         /// Full item string
111         docstring str_;
112         /// Is this item in a note, inactive branch, etc?
113         bool output_;
114         /// Custom action
115         FuncRequest action_;
116 };
117
118
119 /// Caption-enabled TOC builders
120 class TocBuilder
121 {
122 public:
123         TocBuilder(std::shared_ptr<Toc> toc);
124         /// When entering a float or flex or paragraph (with AddToToc)
125         void pushItem(DocIterator const & dit, docstring const & s,
126                       bool output_active, bool is_captioned = false);
127         /// When encountering a float caption
128         void captionItem(DocIterator const & dit, docstring const & s,
129                          bool output_active);
130         /// When encountering an argument (with isTocCaption) for flex or paragraph
131         void argumentItem(docstring const & arg_str);
132         /// When exiting a float or flex or paragraph
133         void pop();
134 private:
135         TocBuilder(){}
136         ///
137         struct frame {
138                 Toc::size_type pos;
139                 bool is_captioned;
140         };
141         ///
142         std::shared_ptr<Toc> const toc_;
143         ///
144         std::stack<frame> stack_;
145 };
146
147
148 /// Class to build and access the Tocs of a particular buffer.
149 class TocBackend
150 {
151 public:
152         static Toc::const_iterator findItem(Toc const & toc,
153                                             DocIterator const & dit);
154         /// Look for a TocItem given its depth and string.
155         /// \return The first matching item.
156         /// \retval end() if no item was found.
157         static Toc::iterator findItem(Toc & toc, int depth, docstring const & str);
158         ///
159         TocBackend(Buffer const * buffer) : buffer_(buffer) {}
160         ///
161         void setBuffer(Buffer const * buffer) { buffer_ = buffer; }
162         ///
163         void update(bool output_active, UpdateType utype);
164         /// \return true if the item was updated.
165         bool updateItem(DocIterator const & pit);
166         ///
167         TocList const & tocs() const { return tocs_; }
168         /// never null
169         std::shared_ptr<Toc const> toc(std::string const & type) const;
170         /// never null
171         std::shared_ptr<Toc> toc(std::string const & type);
172         /// \return the current TocBuilder for the Toc of type \param type, or
173         /// creates one if it does not already exist.
174         TocBuilder & builder(std::string const & type);
175         /// \return the first Toc Item before the cursor.
176         /// \param type: Type of Toc.
177         /// \param dit: The cursor location in the document.
178         Toc::const_iterator
179         item(std::string const & type, DocIterator const & dit) const;
180
181         ///
182         void writePlaintextTocList(std::string const & type,
183                 odocstringstream & os, size_t max_length) const;
184         ///
185         docstring outlinerName(std::string const & type) const;
186         /// Whether a toc type is less important and appears in the "Other lists"
187         /// submenu
188         static bool isOther(std::string const & type);
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