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 "ParIterator.h"
26 #include "TextClass.h"
28 #include "insets/InsetArgument.h"
30 #include "support/convert.h"
31 #include "support/debug.h"
32 #include "support/docstream.h"
34 #include "support/lassert.h"
41 ///////////////////////////////////////////////////////////////////////////
43 // TocItem implementation
45 ///////////////////////////////////////////////////////////////////////////
47 TocItem::TocItem(DocIterator const & dit, int d, docstring const & s,
48 docstring const & t) : dit_(dit), depth_(d), str_(s), tooltip_(t)
53 int TocItem::id() const
55 return dit_.paragraph().id();
59 int TocItem::depth() const
65 docstring const & TocItem::str() const
71 docstring const & TocItem::tooltip() const
77 docstring const TocItem::asString() const
79 return docstring(4 * depth_, ' ') + str_;
83 DocIterator const & TocItem::dit() const
89 FuncRequest TocItem::action() const
91 string const arg = convert<string>(dit_.paragraph().id())
92 + ' ' + convert<string>(dit_.pos());
93 return FuncRequest(LFUN_PARAGRAPH_GOTO, arg);
97 ///////////////////////////////////////////////////////////////////////////
99 // TocBackend implementation
101 ///////////////////////////////////////////////////////////////////////////
103 Toc const & TocBackend::toc(string const & type) const
105 // Is the type already supported?
106 TocList::const_iterator it = tocs_.find(type);
107 LASSERT(it != tocs_.end(), /**/);
113 Toc & TocBackend::toc(string const & type)
119 bool TocBackend::updateItem(DocIterator const & dit)
121 if (dit.paragraph().layout().toclevel == Layout::NOT_IN_TOC)
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!");
131 BufferParams const & bufparams = buffer_->params();
132 const int min_toclevel = bufparams.documentClass().min_toclevel();
134 TocIterator toc_item = item("tableofcontents", dit);
138 // For each paragraph, traverse its insets and let them add
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())
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);
157 int const toclevel = par.layout().toclevel;
158 if (toclevel != Layout::NOT_IN_TOC && toclevel >= min_toclevel
159 && tocstring.empty())
160 tocstring = par.asString(AS_STR_LABEL | AS_STR_INSETS);
162 const_cast<TocItem &>(*toc_item).str_ = tocstring;
164 buffer_->updateTocItem("tableofcontents", dit);
169 void TocBackend::update()
172 if (!buffer_->isInternal()) {
174 buffer_->inset().addToToc(dit);
179 TocIterator TocBackend::item(string const & type,
180 DocIterator const & dit) const
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);
189 TocIterator Toc::item(DocIterator const & dit) const
191 TocIterator last = begin();
192 TocIterator it = end();
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())
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())
211 if (it->dit_ <= dit_text)
215 // We are before the first Toc Item:
220 Toc::iterator Toc::item(int depth, docstring const & str)
224 iterator it = begin();
225 iterator itend = end();
226 for (; it != itend; ++it) {
227 if (it->depth() == depth && it->str() == str)
234 void TocBackend::writePlaintextTocList(string const & type, odocstream & os) const
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");