]> git.lyx.org Git - lyx.git/blob - src/TocBackend.cpp
Update my email and status.
[lyx.git] / src / TocBackend.cpp
1 /**
2  * \file TocBackend.cpp
3  * This file is part of LyX, the document processor.
4  * Licence details can be found in the file COPYING.
5  *
6  * \author Jean-Marc Lasgouttes
7  * \author Angus Leeming
8  * \author Abdelrazak Younes
9  *
10  * Full author contact details are available in file CREDITS.
11  */
12
13 #include <config.h>
14
15 #include "TocBackend.h"
16
17 #include "Buffer.h"
18 #include "BufferParams.h"
19 #include "FloatList.h"
20 #include "FuncRequest.h"
21 #include "InsetList.h"
22 #include "Layout.h"
23 #include "LyXAction.h"
24 #include "Paragraph.h"
25 #include "ParIterator.h"
26 #include "TextClass.h"
27
28 #include "insets/InsetArgument.h"
29
30 #include "support/convert.h"
31 #include "support/debug.h"
32 #include "support/docstream.h"
33
34 #include "support/lassert.h"
35
36 using namespace std;
37
38
39 namespace lyx {
40
41 ///////////////////////////////////////////////////////////////////////////
42 //
43 // TocItem implementation
44 //
45 ///////////////////////////////////////////////////////////////////////////
46
47 TocItem::TocItem(DocIterator const & dit, int d, docstring const & s,
48         docstring const & t) : dit_(dit), depth_(d), str_(s), tooltip_(t)
49 {
50 }
51
52
53 int TocItem::id() const
54 {
55         return dit_.paragraph().id();
56 }
57
58
59 int TocItem::depth() const
60 {
61         return depth_;
62 }
63
64
65 docstring const & TocItem::str() const
66 {
67         return str_;
68 }
69
70
71 docstring const & TocItem::tooltip() const
72 {
73         return tooltip_.empty() ? str_ : tooltip_;
74 }
75
76
77 docstring const TocItem::asString() const
78 {
79         return docstring(4 * depth_, ' ') + str_;
80 }
81
82
83 DocIterator const & TocItem::dit() const
84 {
85         return dit_;
86 }
87
88
89 FuncRequest TocItem::action() const
90 {
91         string const arg = convert<string>(dit_.paragraph().id())
92                 + ' ' + convert<string>(dit_.pos());
93         return FuncRequest(LFUN_PARAGRAPH_GOTO, arg);
94 }
95
96
97 ///////////////////////////////////////////////////////////////////////////
98 //
99 // TocBackend implementation
100 //
101 ///////////////////////////////////////////////////////////////////////////
102
103 Toc const & TocBackend::toc(string const & type) const
104 {
105         // Is the type already supported?
106         TocList::const_iterator it = tocs_.find(type);
107         LASSERT(it != tocs_.end(), /**/);
108
109         return it->second;
110 }
111
112
113 Toc & TocBackend::toc(string const & type)
114 {
115         return tocs_[type];
116 }
117
118
119 bool TocBackend::updateItem(DocIterator const & dit)
120 {
121         if (dit.text()->getTocLevel(dit.pit()) == Layout::NOT_IN_TOC)
122                 return false;
123
124         if (toc("tableofcontents").empty()) {
125                 // FIXME: should not happen, 
126                 // a call to TocBackend::update() is missing somewhere
127                 LYXERR0("TocBackend::updateItem called but the TOC is empty!");
128                 return false;
129         }
130
131         BufferParams const & bufparams = buffer_->params();
132         const int min_toclevel = bufparams.documentClass().min_toclevel();
133
134         TocIterator toc_item = item("tableofcontents", dit);
135
136         docstring tocstring;
137
138         // For each paragraph, traverse its insets and let them add
139         // their toc items
140         Paragraph & par = toc_item->dit_.paragraph();
141         InsetList::const_iterator it = par.insetList().begin();
142         InsetList::const_iterator end = par.insetList().end();
143         for (; it != end; ++it) {
144                 Inset & inset = *it->inset;
145                 if (inset.lyxCode() == ARG_CODE) {
146                         if (!tocstring.empty())
147                                 break;
148                         Paragraph const & inset_par =
149                                 *static_cast<InsetArgument&>(inset).paragraphs().begin();
150                         if (!par.labelString().empty())
151                                 tocstring = par.labelString() + ' ';
152                         tocstring += inset_par.asString(AS_STR_INSETS);
153                         break;
154                 }
155         }
156
157         int const toclevel = toc_item->dit_.text()->getTocLevel(toc_item->dit_.pit());
158         if (toclevel != Layout::NOT_IN_TOC && toclevel >= min_toclevel
159                 && tocstring.empty())
160                         tocstring = par.asString(AS_STR_LABEL | AS_STR_INSETS);
161
162         const_cast<TocItem &>(*toc_item).str_ = tocstring;
163
164         buffer_->updateTocItem("tableofcontents", dit);
165         return true;
166 }
167
168
169 void TocBackend::update()
170 {
171         tocs_.clear();
172         if (!buffer_->isInternal()) {
173                 DocIterator dit;
174                 buffer_->inset().addToToc(dit);
175         }
176 }
177
178
179 TocIterator TocBackend::item(string const & type,
180                 DocIterator const & dit) const
181 {
182         TocList::const_iterator toclist_it = tocs_.find(type);
183         // Is the type supported?
184         LASSERT(toclist_it != tocs_.end(), /**/);
185         return toclist_it->second.item(dit);
186 }
187
188
189 TocIterator Toc::item(DocIterator const & dit) const
190 {
191         TocIterator last = begin();
192         TocIterator it = end();
193         if (it == last)
194                 return it;
195
196         --it;
197
198         DocIterator dit_text = dit;
199         if (dit_text.inMathed()) {
200                 // We are only interested in text so remove the math CursorSlice.
201                 while (dit_text.inMathed())
202                         dit_text.pop_back();
203         }
204
205         for (; it != last; --it) {
206                 // We verify that we don't compare contents of two
207                 // different document. This happens when you
208                 // have parent and child documents.
209                 if (&it->dit_[0].inset() != &dit_text[0].inset())
210                         continue;
211                 if (it->dit_ <= dit_text)
212                         return it;
213         }
214
215         // We are before the first Toc Item:
216         return last;
217 }
218
219
220 Toc::iterator Toc::item(int depth, docstring const & str)
221 {
222         if (empty())
223                 return end();
224         iterator it = begin();
225         iterator itend = end();
226         for (; it != itend; ++it) {
227                 if (it->depth() == depth && it->str() == str)
228                         break;
229         }
230         return it;
231 }
232
233
234 void TocBackend::writePlaintextTocList(string const & type, odocstream & os) const
235 {
236         TocList::const_iterator cit = tocs_.find(type);
237         if (cit != tocs_.end()) {
238                 TocIterator ccit = cit->second.begin();
239                 TocIterator end = cit->second.end();
240                 for (; ccit != end; ++ccit)
241                         os << ccit->asString() << from_utf8("\n");
242         }
243 }
244
245
246 } // namespace lyx