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