]> git.lyx.org Git - lyx.git/blob - src/TocBackend.h
f267d3c774f8023a305ceb99e20d4d0e63aabc1a
[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  *
11  * Full author contact details are available in file CREDITS.
12  */
13
14 #ifndef TOC_BACKEND_H
15 #define TOC_BACKEND_H
16
17 #include "DocIterator.h"
18
19 #include "support/strfwd.h"
20
21 #include <map>
22 #include <vector>
23 #include <string>
24
25
26 namespace lyx {
27
28 class Buffer;
29 class FuncRequest;
30
31 ///
32 /**
33 */
34 class TocItem
35 {
36         friend class Toc;
37         friend class TocBackend;
38
39 public:
40         /// Default constructor for STL containers.
41         TocItem() {}
42         ///
43         TocItem(DocIterator const & dit,
44                 int depth,
45                 docstring const & s
46                 );
47         ///
48         ~TocItem() {}
49         ///
50         int id() const;
51         ///
52         int depth() const;
53         ///
54         docstring const & str() const;
55         ///
56         docstring const asString() const;
57
58         /// the action corresponding to the goTo above
59         FuncRequest action() const;
60
61 protected:
62         /// Current position of item.
63         DocIterator dit_;
64
65         /// nesting depth
66         int depth_;
67
68         /// Full item string
69         docstring str_;
70 };
71
72
73 ///
74 class Toc : public std::vector<TocItem>
75 {
76 public:
77         typedef std::vector<TocItem>::const_iterator const_iterator;
78         typedef std::vector<TocItem>::iterator iterator;
79         const_iterator item(DocIterator const & dit) const;
80         /// Look for a TocItem given its depth and string.
81         /// \return The first matching item.
82         /// \retval end() if no item was found.
83         iterator item(int depth, docstring const & str);
84 };
85
86 typedef Toc::const_iterator TocIterator;
87
88 /// The ToC list.
89 /// A class and no typedef because we want to forward declare it.
90 class TocList : public std::map<std::string, Toc> {};
91
92
93 ///
94 /**
95 */
96 class TocBackend
97 {
98 public:
99         ///
100         TocBackend(Buffer const * buffer) : buffer_(buffer) {}
101         ///
102         void setBuffer(Buffer const * buffer) { buffer_ = buffer; }
103         ///
104         void update();
105         /// \return true if the item was updated.
106         bool updateItem(DocIterator const & pit);
107
108         ///
109         TocList const & tocs() const { return tocs_; }
110         TocList & tocs() { return tocs_; }
111
112         ///
113         Toc const & toc(std::string const & type) const;
114         Toc & toc(std::string const & type);
115
116         /// Return the first Toc Item before the cursor
117         TocIterator item(
118                 std::string const & type, ///< Type of Toc.
119                 DocIterator const & dit ///< The cursor location in the document.
120         ) const;
121
122         ///
123         void writePlaintextTocList(std::string const & type, odocstream & os) const;
124
125 private:
126         ///
127         TocList tocs_;
128         ///
129         Buffer const * buffer_;
130 }; // TocBackend
131
132 inline bool operator==(TocItem const & a, TocItem const & b)
133 {
134         return a.id() == b.id() && a.str() == b.str() && a.depth() == b.depth();
135 }
136
137
138 inline bool operator!=(TocItem const & a, TocItem const & b)
139 {
140         return !(a == b);
141 }
142
143
144 } // namespace lyx
145
146 #endif // TOC_BACKEND_H