3 * This file is part of LyX, the document processor.
4 * Licence details can be found in the file COPYING.
6 * \author Jean-Marc Lasgouttes
7 * \author Angus Leeming
8 * \author Abdelrazak Younes
10 * Full author contact details are available in file CREDITS.
15 #include "TocBackend.h"
18 #include "BufferParams.h"
19 #include "FloatList.h"
20 #include "FuncRequest.h"
21 #include "InsetList.h"
23 #include "LyXAction.h"
24 #include "Paragraph.h"
25 #include "TextClass.h"
27 #include "insets/InsetOptArg.h"
29 #include "support/convert.h"
30 #include "support/debug.h"
31 #include "support/docstream.h"
36 ///////////////////////////////////////////////////////////////////////////
38 // TocItem implementation
40 ///////////////////////////////////////////////////////////////////////////
42 TocItem::TocItem(ParConstIterator const & par_it, int d,
44 : par_it_(par_it), depth_(d), str_(s)
49 int TocItem::id() const
55 int TocItem::depth() const
61 docstring const & TocItem::str() const
67 docstring const TocItem::asString() const
69 return docstring(4 * depth_, ' ') + str_;
73 FuncRequest TocItem::action() const
75 return FuncRequest(LFUN_PARAGRAPH_GOTO, convert<string>(id()));
79 ///////////////////////////////////////////////////////////////////////////
81 // TocBackend implementation
83 ///////////////////////////////////////////////////////////////////////////
85 Toc const & TocBackend::toc(std::string const & type) const
87 // Is the type already supported?
88 TocList::const_iterator it = tocs_.find(type);
89 BOOST_ASSERT(it != tocs_.end());
95 void TocBackend::updateItem(ParConstIterator const & par_it)
97 if (toc("tableofcontents").empty()) {
98 // FIXME: should not happen,
99 // a call to TocBackend::update() is missing somewhere
100 LYXERR0("TocBackend::updateItem called but the TOC is empty!");
104 BufferParams const & bufparams = buffer_->params();
105 const int min_toclevel = bufparams.getTextClass().min_toclevel();
107 TocIterator toc_item = item("tableofcontents", par_it);
111 // For each paragraph, traverse its insets and let them add
113 InsetList::const_iterator it = toc_item->par_it_->insetList().begin();
114 InsetList::const_iterator end = toc_item->par_it_->insetList().end();
115 for (; it != end; ++it) {
116 Inset & inset = *it->inset;
117 if (inset.lyxCode() == OPTARG_CODE) {
118 if (!tocstring.empty())
120 Paragraph const & par =
121 *static_cast<InsetOptArg&>(inset).paragraphs().begin();
122 if (!toc_item->par_it_->getLabelstring().empty())
123 tocstring = toc_item->par_it_->getLabelstring() + ' ';
124 tocstring += par.asString(*buffer_, false);
129 int const toclevel = toc_item->par_it_->layout()->toclevel;
130 if (toclevel != Layout::NOT_IN_TOC
131 && toclevel >= min_toclevel
132 && tocstring.empty())
133 tocstring = toc_item->par_it_->asString(*buffer_, true);
135 const_cast<TocItem &>(*toc_item).str_ = tocstring;
139 void TocBackend::update()
143 BufferParams const & bufparams = buffer_->params();
144 const int min_toclevel = bufparams.getTextClass().min_toclevel();
146 Toc & toc = tocs_["tableofcontents"];
147 ParConstIterator pit = buffer_->par_iterator_begin();
148 ParConstIterator end = buffer_->par_iterator_end();
149 for (; pit != end; ++pit) {
151 // the string that goes to the toc (could be the optarg)
154 // For each paragraph, traverse its insets and let them add
156 InsetList::const_iterator it = pit->insetList().begin();
157 InsetList::const_iterator end = pit->insetList().end();
158 for (; it != end; ++it) {
159 Inset & inset = *it->inset;
160 inset.addToToc(tocs_, *buffer_, pit);
161 switch (inset.lyxCode()) {
163 if (!tocstring.empty())
165 Paragraph const & par =
166 *static_cast<InsetOptArg&>(inset).paragraphs().begin();
167 if (!pit->getLabelstring().empty())
168 tocstring = pit->getLabelstring() + ' ';
169 tocstring += par.asString(*buffer_, false);
177 /// now the toc entry for the paragraph
178 int const toclevel = pit->layout()->toclevel;
179 if (toclevel != Layout::NOT_IN_TOC
180 && toclevel >= min_toclevel) {
181 // insert this into the table of contents
182 if (tocstring.empty())
183 tocstring = pit->asString(*buffer_, true);
184 toc.push_back(TocItem(pit, toclevel - min_toclevel,
191 TocIterator const TocBackend::item(std::string const & type,
192 ParConstIterator const & par_it) const
194 TocList::const_iterator toclist_it = tocs_.find(type);
195 // Is the type supported?
196 BOOST_ASSERT(toclist_it != tocs_.end());
198 Toc const & toc_vector = toclist_it->second;
199 TocIterator last = toc_vector.begin();
200 TocIterator it = toc_vector.end();
206 ParConstIterator par_it_text = par_it;
207 if (par_it_text.inMathed())
208 // It would be better to do
209 // par_it_text.backwardInset();
210 // but this method does not exist.
211 while (par_it_text.inMathed())
212 par_it_text.backwardPos();
214 for (; it != last; --it) {
215 // We verify that we don't compare contents of two
216 // different document. This happens when you
217 // have parent and child documents.
218 if (&it->par_it_[0].inset() != &par_it_text[0].inset())
220 if (it->par_it_ <= par_it_text)
224 // We are before the first Toc Item:
229 void TocBackend::writePlaintextTocList(string const & type, odocstream & os) const
231 TocList::const_iterator cit = tocs_.find(type);
232 if (cit != tocs_.end()) {
233 TocIterator ccit = cit->second.begin();
234 TocIterator end = cit->second.end();
235 for (; ccit != end; ++ccit)
236 os << ccit->asString() << from_utf8("\n");