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