]> git.lyx.org Git - lyx.git/blob - src/TocBackend.cpp
revert erroneous previous commit.
[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/InsetOptArg.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         : dit_(dit), depth_(d), str_(s)
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::asString() const
72 {
73         return docstring(4 * depth_, ' ') + str_;
74 }
75
76
77 FuncRequest TocItem::action() const
78 {
79         string const arg = convert<string>(dit_.paragraph().id())
80                 + ' ' + convert<string>(dit_.pos());
81         return FuncRequest(LFUN_PARAGRAPH_GOTO, arg);
82 }
83
84
85 ///////////////////////////////////////////////////////////////////////////
86 //
87 // TocBackend implementation
88 //
89 ///////////////////////////////////////////////////////////////////////////
90
91 Toc const & TocBackend::toc(string const & type) const
92 {
93         // Is the type already supported?
94         TocList::const_iterator it = tocs_.find(type);
95         LASSERT(it != tocs_.end(), /**/);
96
97         return it->second;
98 }
99
100
101 Toc & TocBackend::toc(string const & type)
102 {
103         return tocs_[type];
104 }
105
106
107 bool TocBackend::updateItem(DocIterator const & dit)
108 {
109         if (dit.paragraph().layout().toclevel == Layout::NOT_IN_TOC)
110                 return false;
111
112         if (toc("tableofcontents").empty()) {
113                 // FIXME: should not happen, 
114                 // a call to TocBackend::update() is missing somewhere
115                 LYXERR0("TocBackend::updateItem called but the TOC is empty!");
116                 return false;
117         }
118
119         BufferParams const & bufparams = buffer_->params();
120         const int min_toclevel = bufparams.documentClass().min_toclevel();
121
122         TocIterator toc_item = item("tableofcontents", dit);
123
124         docstring tocstring;
125
126         // For each paragraph, traverse its insets and let them add
127         // their toc items
128         Paragraph & par = toc_item->dit_.paragraph();
129         InsetList::const_iterator it = par.insetList().begin();
130         InsetList::const_iterator end = par.insetList().end();
131         for (; it != end; ++it) {
132                 Inset & inset = *it->inset;
133                 if (inset.lyxCode() == OPTARG_CODE) {
134                         if (!tocstring.empty())
135                                 break;
136                         Paragraph const & inset_par =
137                                 *static_cast<InsetOptArg&>(inset).paragraphs().begin();
138                         if (!par.labelString().empty())
139                                 tocstring = par.labelString() + ' ';
140                         tocstring += inset_par.asString();
141                         break;
142                 }
143         }
144
145         int const toclevel = par.layout().toclevel;
146         if (toclevel != Layout::NOT_IN_TOC && toclevel >= min_toclevel
147                 && tocstring.empty())
148                         tocstring = par.asString(AS_STR_LABEL);
149
150         const_cast<TocItem &>(*toc_item).str_ = tocstring;
151
152         buffer_->updateTocItem("tableofcontents", dit);
153         return true;
154 }
155
156
157 void TocBackend::update()
158 {
159         tocs_.clear();
160         DocIterator dit;
161         buffer_->inset().addToToc(dit);
162 }
163
164
165 TocIterator TocBackend::item(string const & type,
166                 DocIterator const & dit) const
167 {
168         TocList::const_iterator toclist_it = tocs_.find(type);
169         // Is the type supported?
170         LASSERT(toclist_it != tocs_.end(), /**/);
171         return toclist_it->second.item(dit);
172 }
173
174
175 TocIterator Toc::item(DocIterator const & dit) const
176 {
177         TocIterator last = begin();
178         TocIterator it = end();
179         if (it == last)
180                 return it;
181
182         --it;
183
184         DocIterator dit_text = dit;
185         if (dit_text.inMathed()) {
186                 // We are only interested in text so remove the math CursorSlice.
187                 while (dit_text.inMathed())
188                         dit_text.pop_back();
189         }
190
191         for (; it != last; --it) {
192                 // We verify that we don't compare contents of two
193                 // different document. This happens when you
194                 // have parent and child documents.
195                 if (&it->dit_[0].inset() != &dit_text[0].inset())
196                         continue;
197                 if (it->dit_ <= dit_text)
198                         return it;
199         }
200
201         // We are before the first Toc Item:
202         return last;
203 }
204
205
206 Toc::iterator Toc::item(int depth, docstring const & str)
207 {
208         if (empty())
209                 return end();
210         iterator it = begin();
211         iterator itend = end();
212         for (; it != itend; ++it) {
213                 if (it->depth() == depth && it->str() == str)
214                         break;
215         }
216         return it;
217 }
218
219
220 void TocBackend::writePlaintextTocList(string const & type, odocstream & os) const
221 {
222         TocList::const_iterator cit = tocs_.find(type);
223         if (cit != tocs_.end()) {
224                 TocIterator ccit = cit->second.begin();
225                 TocIterator end = cit->second.end();
226                 for (; ccit != end; ++ccit)
227                         os << ccit->asString() << from_utf8("\n");
228         }
229 }
230
231
232 } // namespace lyx