]> git.lyx.org Git - lyx.git/blob - src/TocBackend.h
2520927406dd15a2cebbf5aa86d4cf9d9be96c2d
[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 namespace lyx {
29
30 class Buffer;
31
32
33 /* FIXME: toc types are currently identified by strings. It cannot be converted
34  * into an enum because of the user-configurable indexing categories and
35  * the user-definable float types provided by layout files.
36  *
37  * I leave this for documentation purposes for the moment.
38  *
39 enum TocType {
40         TABLE_OF_CONTENTS,//"tableofcontents"
41         CHILD,//"child"
42         GRAPHICS,//"graphics"
43         NOTE,//"note"
44         BRANCH,//"branch"
45         CHANGE,//"change"
46         LABEL,//"label"
47         CITATION,//"citation"
48         EQUATION,//"equation"
49         FOOTNOTE,//"footnote"
50         MARGINAL_NOTE,//"marginalnote"
51         INDEX,//"index", "index:<user-str>" (from interface)
52         NOMENCL,//"nomencl"
53         LISTING,//"listings"
54         FLOAT,//"figure", "table", "algorithm", user-defined (from layout?)
55         MATH_MACRO,//"math-macro"
56         EXTERNAL,//"external"
57         SENSELESS,//"senseless"
58         TOC_TYPE_COUNT
59 }
60  */
61
62 ///
63 /**
64 */
65 class TocItem
66 {
67         friend class TocBackend;
68         friend class TocBuilder;
69
70 public:
71         /// Default constructor for STL containers.
72         TocItem() : dit_(0), depth_(0), output_(false) {}
73         ///
74         TocItem(DocIterator const & dit,
75                 int depth,
76                 docstring const & s,
77                 bool output_active,
78                 docstring const & t = docstring(),
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         ///
92         docstring const & tooltip() const;
93         /// String for display, e.g. it has a mark if output is inactive
94         docstring const asString() const;
95         ///
96         DocIterator const & dit() const { return dit_; }
97         ///
98         bool isOutput() const { return output_; }
99         ///
100         void setAction(FuncRequest a) { action_ = a; }
101         /// custom action, or the default one (paragraph-goto) if not customised
102         FuncRequest action() const;
103
104 protected:
105         /// Current position of item.
106         DocIterator dit_;
107
108 private:
109         /// nesting depth
110         int depth_;
111         /// Full item string
112         docstring str_;
113         /// The tooltip string
114         docstring tooltip_;
115         /// Is this item in a note, inactive branch, etc?
116         bool output_;
117         /// Custom action
118         FuncRequest action_;
119 };
120
121
122 /// Caption-enabled TOC builders
123 class TocBuilder
124 {
125 public:
126         TocBuilder(shared_ptr<Toc> const toc);
127         /// When entering a float
128         void pushItem(DocIterator const & dit, docstring const & s,
129                                   bool output_active, bool is_captioned = false);
130         /// When encountering a caption
131         void captionItem(DocIterator const & dit, docstring const & s,
132                                          bool output_active);
133         /// When exiting a float
134         void pop();
135 private:
136         TocBuilder(){}
137         ///
138         struct frame {
139                 Toc::size_type pos;
140                 bool is_captioned;
141         };
142         ///
143         shared_ptr<Toc> const toc_;
144         ///
145         std::stack<frame> stack_;
146 };
147
148
149 ///
150 class TocBuilderStore
151 {
152 public:
153         TocBuilderStore() {};
154         ///
155         shared_ptr<TocBuilder> get(std::string const & type, shared_ptr<Toc> toc);
156         ///
157         void clear() { map_.clear(); };
158 private:
159         typedef std::map<std::string, shared_ptr<TocBuilder> > map_t;
160         map_t map_;
161 };
162
163
164 ///
165 /**
166 */
167 class TocBackend
168 {
169 public:
170         static Toc::const_iterator findItem(Toc const & toc,
171                                             DocIterator const & dit);
172         /// Look for a TocItem given its depth and string.
173         /// \return The first matching item.
174         /// \retval end() if no item was found.
175         static Toc::iterator findItem(Toc & toc, int depth, docstring const & str);
176         ///
177         TocBackend(Buffer const * buffer) : buffer_(buffer) {}
178         ///
179         void setBuffer(Buffer const * buffer) { buffer_ = buffer; }
180         ///
181         void update(bool output_active, UpdateType utype);
182         /// \return true if the item was updated.
183         bool updateItem(DocIterator const & pit);
184         ///
185         TocList const & tocs() const { return tocs_; }
186         /// never null
187         shared_ptr<Toc const> toc(std::string const & type) const;
188         shared_ptr<Toc> toc(std::string const & type);
189         /// nevel null
190         shared_ptr<TocBuilder> builder(std::string const & type);
191         /// Return the first Toc Item before the cursor
192         Toc::const_iterator item(
193                 std::string const & type, ///< Type of Toc.
194                 DocIterator const & dit ///< The cursor location in the document.
195         ) const;
196
197         ///
198         void writePlaintextTocList(std::string const & type,
199                 odocstringstream & os, size_t max_length) const;
200         ///
201         docstring outlinerName(std::string const & type) const;
202
203 private:
204         ///
205         TocList tocs_;
206         ///
207         TocBuilderStore builders_;
208         ///
209         Buffer const * buffer_;
210 }; // TocBackend
211
212
213 } // namespace lyx
214
215 #endif // TOC_BACKEND_H