]> git.lyx.org Git - lyx.git/blob - src/TocBackend.h
listerrors.lyx : Update a link.
[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() : dit_(0) {}
42         ///
43         TocItem(DocIterator const & dit,
44                 int depth,
45                 docstring const & s,
46                 docstring const & t = docstring()
47                 );
48         ///
49         ~TocItem() {}
50         ///
51         int id() const;
52         ///
53         int depth() const;
54         ///
55         docstring const & str() const;
56         ///
57         docstring const & tooltip() const;
58         ///
59         docstring const asString() const;
60         ///
61         DocIterator const & dit() const;
62
63         /// the action corresponding to the goTo above
64         FuncRequest action() const;
65
66 protected:
67         /// Current position of item.
68         DocIterator dit_;
69
70         /// nesting depth
71         int depth_;
72
73         /// Full item string
74         docstring str_;
75
76         /// The tooltip string
77         docstring tooltip_;
78 };
79
80
81 ///
82 class Toc : public std::vector<TocItem>
83 {
84 public:
85         typedef std::vector<TocItem>::const_iterator const_iterator;
86         typedef std::vector<TocItem>::iterator iterator;
87         const_iterator item(DocIterator const & dit) const;
88         /// Look for a TocItem given its depth and string.
89         /// \return The first matching item.
90         /// \retval end() if no item was found.
91         iterator item(int depth, docstring const & str);
92 };
93
94 typedef Toc::const_iterator TocIterator;
95
96 /// The ToC list.
97 /// A class and no typedef because we want to forward declare it.
98 class TocList : public std::map<std::string, Toc> {};
99
100
101 ///
102 /**
103 */
104 class TocBackend
105 {
106 public:
107         ///
108         TocBackend(Buffer const * buffer) : buffer_(buffer) {}
109         ///
110         void setBuffer(Buffer const * buffer) { buffer_ = buffer; }
111         ///
112         void update();
113         /// \return true if the item was updated.
114         bool updateItem(DocIterator const & pit);
115
116         ///
117         TocList const & tocs() const { return tocs_; }
118         TocList & tocs() { return tocs_; }
119
120         ///
121         Toc const & toc(std::string const & type) const;
122         Toc & toc(std::string const & type);
123
124         /// Return the first Toc Item before the cursor
125         TocIterator item(
126                 std::string const & type, ///< Type of Toc.
127                 DocIterator const & dit ///< The cursor location in the document.
128         ) const;
129
130         ///
131         void writePlaintextTocList(std::string const & type, odocstream & os) const;
132
133 private:
134         ///
135         TocList tocs_;
136         ///
137         Buffer const * buffer_;
138 }; // TocBackend
139
140 inline bool operator==(TocItem const & a, TocItem const & b)
141 {
142         return a.id() == b.id() && a.str() == b.str() && a.depth() == b.depth();
143 }
144
145
146 inline bool operator!=(TocItem const & a, TocItem const & b)
147 {
148         return !(a == b);
149 }
150
151
152 } // namespace lyx
153
154 #endif // TOC_BACKEND_H