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"
20 #include "FloatList.h"
21 #include "FuncRequest.h"
23 #include "LyXAction.h"
24 #include "Paragraph.h"
26 #include "insets/InsetOptArg.h"
28 #include "support/convert.h"
34 ///////////////////////////////////////////////////////////////////////////
36 // TocItem implementation
38 ///////////////////////////////////////////////////////////////////////////
40 TocItem::TocItem(ParConstIterator const & par_it, int d,
42 : par_it_(par_it), depth_(d), str_(s)
47 int TocItem::id() const
53 int TocItem::depth() const
59 docstring const & TocItem::str() const
65 docstring const TocItem::asString() const
67 return docstring(4 * depth_, ' ') + str_;
71 FuncRequest TocItem::action() const
73 return FuncRequest(LFUN_PARAGRAPH_GOTO, convert<string>(id()));
77 ///////////////////////////////////////////////////////////////////////////
79 // TocBackend implementation
81 ///////////////////////////////////////////////////////////////////////////
83 Toc const & TocBackend::toc(std::string const & type) const
85 // Is the type already supported?
86 TocList::const_iterator it = tocs_.find(type);
87 BOOST_ASSERT(it != tocs_.end());
93 void TocBackend::updateItem(ParConstIterator const & par_it)
95 if (toc("tableofcontents").empty()) {
96 // FIXME: should not happen,
97 // a call to TocBackend::update() is missing somewhere
98 lyxerr << "TocBackend::updateItem called but the TOC is empty!"
103 BufferParams const & bufparams = buffer_->params();
104 const int min_toclevel = bufparams.getTextClass().min_toclevel();
106 TocIterator toc_item = item("tableofcontents", par_it);
110 // For each paragraph, traverse its insets and let them add
112 InsetList::const_iterator it = toc_item->par_it_->insetlist.begin();
113 InsetList::const_iterator end = toc_item->par_it_->insetlist.end();
114 for (; it != end; ++it) {
115 Inset & inset = *it->inset;
116 if (inset.lyxCode() == OPTARG_CODE) {
117 if (!tocstring.empty())
119 Paragraph const & par =
120 *static_cast<InsetOptArg&>(inset).paragraphs().begin();
121 if (!toc_item->par_it_->getLabelstring().empty())
122 tocstring = toc_item->par_it_->getLabelstring() + ' ';
123 tocstring += par.asString(*buffer_, false);
128 int const toclevel = toc_item->par_it_->layout()->toclevel;
129 if (toclevel != Layout::NOT_IN_TOC
130 && toclevel >= min_toclevel
131 && tocstring.empty())
132 tocstring = toc_item->par_it_->asString(*buffer_, true);
134 const_cast<TocItem &>(*toc_item).str_ = tocstring;
138 void TocBackend::update()
142 BufferParams const & bufparams = buffer_->params();
143 const int min_toclevel = bufparams.getTextClass().min_toclevel();
145 Toc & toc = tocs_["tableofcontents"];
146 ParConstIterator pit = buffer_->par_iterator_begin();
147 ParConstIterator end = buffer_->par_iterator_end();
148 for (; pit != end; ++pit) {
150 // the string that goes to the toc (could be the optarg)
153 // For each paragraph, traverse its insets and let them add
155 InsetList::const_iterator it = pit->insetlist.begin();
156 InsetList::const_iterator end = pit->insetlist.end();
157 for (; it != end; ++it) {
158 Inset & inset = *it->inset;
159 inset.addToToc(tocs_, *buffer_, pit);
160 switch (inset.lyxCode()) {
162 if (!tocstring.empty())
164 Paragraph const & par =
165 *static_cast<InsetOptArg&>(inset).paragraphs().begin();
166 if (!pit->getLabelstring().empty())
167 tocstring = pit->getLabelstring() + ' ';
168 tocstring += par.asString(*buffer_, false);
176 /// now the toc entry for the paragraph
177 int const toclevel = pit->layout()->toclevel;
178 if (toclevel != Layout::NOT_IN_TOC
179 && toclevel >= min_toclevel) {
180 // insert this into the table of contents
181 if (tocstring.empty())
182 tocstring = pit->asString(*buffer_, true);
183 toc.push_back(TocItem(pit, toclevel - min_toclevel,
190 TocIterator const TocBackend::item(std::string const & type,
191 ParConstIterator const & par_it) const
193 TocList::const_iterator toclist_it = tocs_.find(type);
194 // Is the type supported?
195 BOOST_ASSERT(toclist_it != tocs_.end());
197 Toc const & toc_vector = toclist_it->second;
198 TocIterator last = toc_vector.begin();
199 TocIterator it = toc_vector.end();
205 ParConstIterator par_it_text = par_it;
206 if (par_it_text.inMathed())
207 // It would be better to do
208 // par_it_text.backwardInset();
209 // but this method does not exist.
210 while (par_it_text.inMathed())
211 par_it_text.backwardPos();
213 for (; it != last; --it) {
214 // We verify that we don't compare contents of two
215 // different document. This happens when you
216 // have parent and child documents.
217 if (&it->par_it_[0].inset() != &par_it_text[0].inset())
219 if (it->par_it_ <= par_it_text)
223 // We are before the first Toc Item:
228 void TocBackend::writePlaintextTocList(string const & type, odocstream & os) const
230 TocList::const_iterator cit = tocs_.find(type);
231 if (cit != tocs_.end()) {
232 TocIterator ccit = cit->second.begin();
233 TocIterator end = cit->second.end();
234 for (; ccit != end; ++ccit)
235 os << ccit->asString() << '\n';