]> git.lyx.org Git - lyx.git/blob - src/TocBackend.h
1579cf2fdcd0b34ba9b9c3311a7add50f0b107a9
[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
25 #include <stack>
26
27
28 using std::shared_ptr;
29
30 namespace lyx {
31
32 class Buffer;
33
34
35 /* FIXME: toc types are currently identified by strings. It cannot be converted
36  * into an enum because of the user-configurable indexing categories and
37  * the user-definable float types provided by layout files.
38  *
39  * I leave this for documentation purposes for the moment.
40  *
41 enum TocType {
42         TABLE_OF_CONTENTS,//"tableofcontents"
43         CHILD,//"child"
44         GRAPHICS,//"graphics"
45         NOTE,//"note"
46         BRANCH,//"branch"
47         CHANGE,//"change"
48         LABEL,//"label"
49         CITATION,//"citation"
50         EQUATION,//"equation"
51         FOOTNOTE,//"footnote"
52         MARGINAL_NOTE,//"marginalnote"
53         INDEX,//"index", "index:<user-str>" (from interface)
54         NOMENCL,//"nomencl"
55         LISTING,//"listings"
56         FLOAT,//"figure", "table", "algorithm", user-defined (from layout?)
57         MATH_MACRO,//"math-macro"
58         EXTERNAL,//"external"
59         SENSELESS,//"senseless"
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                 docstring const & t = docstring(),
81                 FuncRequest action = FuncRequest(LFUN_UNKNOWN_ACTION)
82                 );
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 ///
152 class TocBuilderStore
153 {
154 public:
155         TocBuilderStore() {};
156         ///
157         shared_ptr<TocBuilder> get(std::string const & type, shared_ptr<Toc> toc);
158         ///
159         void clear() { map_.clear(); };
160 private:
161         typedef std::map<std::string, shared_ptr<TocBuilder>> map_t;
162         map_t map_;
163 };
164
165
166 ///
167 /**
168 */
169 class TocBackend
170 {
171 public:
172         static Toc::const_iterator findItem(Toc const & toc,
173                                             DocIterator const & dit);
174         /// Look for a TocItem given its depth and string.
175         /// \return The first matching item.
176         /// \retval end() if no item was found.
177         static Toc::iterator findItem(Toc & toc, int depth, docstring const & str);
178         ///
179         TocBackend(Buffer const * buffer) : buffer_(buffer) {}
180         ///
181         void setBuffer(Buffer const * buffer) { buffer_ = buffer; }
182         ///
183         void update(bool output_active, UpdateType utype);
184         /// \return true if the item was updated.
185         bool updateItem(DocIterator const & pit);
186         ///
187         TocList const & tocs() const { return tocs_; }
188         /// never null
189         shared_ptr<Toc const> toc(std::string const & type) const;
190         shared_ptr<Toc> toc(std::string const & type);
191         /// nevel null
192         shared_ptr<TocBuilder> builder(std::string const & type);
193         /// Return the first Toc Item before the cursor
194         Toc::const_iterator item(
195                 std::string const & type, ///< Type of Toc.
196                 DocIterator const & dit ///< The cursor location in the document.
197         ) const;
198
199         ///
200         void writePlaintextTocList(std::string const & type,
201                 odocstringstream & os, size_t max_length) const;
202         ///
203         docstring outlinerName(std::string const & type) const;
204
205 private:
206         ///
207         TocList tocs_;
208         ///
209         TocBuilderStore builders_;
210         ///
211         Buffer const * buffer_;
212 }; // TocBackend
213
214
215 } // namespace lyx
216
217 #endif // TOC_BACKEND_H