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 "support/debug.h"
20 #include "FloatList.h"
21 #include "FuncRequest.h"
22 #include "InsetList.h"
24 #include "LyXAction.h"
25 #include "Paragraph.h"
26 #include "TextClass.h"
28 #include "insets/InsetOptArg.h"
30 #include "support/convert.h"
31 #include "support/docstring.h"
37 ///////////////////////////////////////////////////////////////////////////
39 // TocItem implementation
41 ///////////////////////////////////////////////////////////////////////////
43 TocItem::TocItem(ParConstIterator const & par_it, int d,
45 : par_it_(par_it), depth_(d), str_(s)
50 int TocItem::id() const
56 int TocItem::depth() const
62 docstring const & TocItem::str() const
68 docstring const TocItem::asString() const
70 return docstring(4 * depth_, ' ') + str_;
74 FuncRequest TocItem::action() const
76 return FuncRequest(LFUN_PARAGRAPH_GOTO, convert<string>(id()));
80 ///////////////////////////////////////////////////////////////////////////
82 // TocBackend implementation
84 ///////////////////////////////////////////////////////////////////////////
86 Toc const & TocBackend::toc(std::string const & type) const
88 // Is the type already supported?
89 TocList::const_iterator it = tocs_.find(type);
90 BOOST_ASSERT(it != tocs_.end());
96 void TocBackend::updateItem(ParConstIterator const & par_it)
98 if (toc("tableofcontents").empty()) {
99 // FIXME: should not happen,
100 // a call to TocBackend::update() is missing somewhere
101 LYXERR0("TocBackend::updateItem called but the TOC is empty!");
105 BufferParams const & bufparams = buffer_->params();
106 const int min_toclevel = bufparams.getTextClass().min_toclevel();
108 TocIterator toc_item = item("tableofcontents", par_it);
112 // For each paragraph, traverse its insets and let them add
114 InsetList::const_iterator it = toc_item->par_it_->insetList().begin();
115 InsetList::const_iterator end = toc_item->par_it_->insetList().end();
116 for (; it != end; ++it) {
117 Inset & inset = *it->inset;
118 if (inset.lyxCode() == OPTARG_CODE) {
119 if (!tocstring.empty())
121 Paragraph const & par =
122 *static_cast<InsetOptArg&>(inset).paragraphs().begin();
123 if (!toc_item->par_it_->getLabelstring().empty())
124 tocstring = toc_item->par_it_->getLabelstring() + ' ';
125 tocstring += par.asString(*buffer_, false);
130 int const toclevel = toc_item->par_it_->layout()->toclevel;
131 if (toclevel != Layout::NOT_IN_TOC
132 && toclevel >= min_toclevel
133 && tocstring.empty())
134 tocstring = toc_item->par_it_->asString(*buffer_, true);
136 const_cast<TocItem &>(*toc_item).str_ = tocstring;
140 void TocBackend::update()
144 BufferParams const & bufparams = buffer_->params();
145 const int min_toclevel = bufparams.getTextClass().min_toclevel();
147 Toc & toc = tocs_["tableofcontents"];
148 ParConstIterator pit = buffer_->par_iterator_begin();
149 ParConstIterator end = buffer_->par_iterator_end();
150 for (; pit != end; ++pit) {
152 // the string that goes to the toc (could be the optarg)
155 // For each paragraph, traverse its insets and let them add
157 InsetList::const_iterator it = pit->insetList().begin();
158 InsetList::const_iterator end = pit->insetList().end();
159 for (; it != end; ++it) {
160 Inset & inset = *it->inset;
161 inset.addToToc(tocs_, *buffer_, pit);
162 switch (inset.lyxCode()) {
164 if (!tocstring.empty())
166 Paragraph const & par =
167 *static_cast<InsetOptArg&>(inset).paragraphs().begin();
168 if (!pit->getLabelstring().empty())
169 tocstring = pit->getLabelstring() + ' ';
170 tocstring += par.asString(*buffer_, false);
178 /// now the toc entry for the paragraph
179 int const toclevel = pit->layout()->toclevel;
180 if (toclevel != Layout::NOT_IN_TOC
181 && toclevel >= min_toclevel) {
182 // insert this into the table of contents
183 if (tocstring.empty())
184 tocstring = pit->asString(*buffer_, true);
185 toc.push_back(TocItem(pit, toclevel - min_toclevel,
192 TocIterator const TocBackend::item(std::string const & type,
193 ParConstIterator const & par_it) const
195 TocList::const_iterator toclist_it = tocs_.find(type);
196 // Is the type supported?
197 BOOST_ASSERT(toclist_it != tocs_.end());
199 Toc const & toc_vector = toclist_it->second;
200 TocIterator last = toc_vector.begin();
201 TocIterator it = toc_vector.end();
207 ParConstIterator par_it_text = par_it;
208 if (par_it_text.inMathed())
209 // It would be better to do
210 // par_it_text.backwardInset();
211 // but this method does not exist.
212 while (par_it_text.inMathed())
213 par_it_text.backwardPos();
215 for (; it != last; --it) {
216 // We verify that we don't compare contents of two
217 // different document. This happens when you
218 // have parent and child documents.
219 if (&it->par_it_[0].inset() != &par_it_text[0].inset())
221 if (it->par_it_ <= par_it_text)
225 // We are before the first Toc Item:
230 void TocBackend::writePlaintextTocList(string const & type, odocstream & os) const
232 TocList::const_iterator cit = tocs_.find(type);
233 if (cit != tocs_.end()) {
234 TocIterator ccit = cit->second.begin();
235 TocIterator end = cit->second.end();
236 for (; ccit != end; ++ccit)
237 os << ccit->asString() << from_utf8("\n");