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 bool output_active, docstring const & t) :
49 dit_(dit), depth_(d), str_(s), tooltip_(t), output_(output_active)
54 int TocItem::id() const
56 return dit_.paragraph().id();
60 int TocItem::depth() const
66 docstring const & TocItem::str() const
72 docstring const & TocItem::tooltip() const
74 return tooltip_.empty() ? str_ : tooltip_;
78 docstring const TocItem::asString() const
80 return docstring(4 * depth_, ' ') + str_;
84 DocIterator const & TocItem::dit() const
90 FuncRequest TocItem::action() const
92 string const arg = convert<string>(dit_.paragraph().id())
93 + ' ' + convert<string>(dit_.pos());
94 return FuncRequest(LFUN_PARAGRAPH_GOTO, arg);
98 ///////////////////////////////////////////////////////////////////////////
100 // TocBackend implementation
102 ///////////////////////////////////////////////////////////////////////////
104 Toc const & TocBackend::toc(string const & type) const
106 // Is the type already supported?
107 TocList::const_iterator it = tocs_.find(type);
108 LASSERT(it != tocs_.end(), { static Toc dummy; return dummy; });
114 Toc & TocBackend::toc(string const & type)
120 bool TocBackend::updateItem(DocIterator const & dit)
122 if (dit.text()->getTocLevel(dit.pit()) == Layout::NOT_IN_TOC)
125 if (toc("tableofcontents").empty()) {
126 // FIXME: should not happen,
127 // a call to TocBackend::update() is missing somewhere
128 LYXERR0("TocBackend::updateItem called but the TOC is empty!");
132 BufferParams const & bufparams = buffer_->params();
133 const int min_toclevel = bufparams.documentClass().min_toclevel();
135 TocIterator toc_item = item("tableofcontents", dit);
139 // For each paragraph, traverse its insets and let them add
141 Paragraph & par = toc_item->dit_.paragraph();
142 InsetList::const_iterator it = par.insetList().begin();
143 InsetList::const_iterator end = par.insetList().end();
144 for (; it != end; ++it) {
145 Inset & inset = *it->inset;
146 if (inset.lyxCode() == ARG_CODE) {
147 if (!tocstring.empty())
149 Paragraph const & inset_par =
150 *static_cast<InsetArgument&>(inset).paragraphs().begin();
151 if (!par.labelString().empty())
152 tocstring = par.labelString() + ' ';
153 tocstring += inset_par.asString(AS_STR_INSETS);
158 int const toclevel = toc_item->dit_.text()->getTocLevel(toc_item->dit_.pit());
159 if (toclevel != Layout::NOT_IN_TOC && toclevel >= min_toclevel
160 && tocstring.empty())
161 tocstring = par.asString(AS_STR_LABEL | AS_STR_INSETS);
163 const_cast<TocItem &>(*toc_item).str_ = tocstring;
165 buffer_->updateTocItem("tableofcontents", dit);
170 void TocBackend::update(bool output_active)
173 if (!buffer_->isInternal()) {
175 buffer_->inset().addToToc(dit, output_active);
180 TocIterator TocBackend::item(string const & type,
181 DocIterator const & dit) const
183 TocList::const_iterator toclist_it = tocs_.find(type);
184 // Is the type supported?
185 // We will try to make the best of it in release mode
186 LASSERT(toclist_it != tocs_.end(), toclist_it = tocs_.begin());
187 return toclist_it->second.item(dit);
191 TocIterator Toc::item(DocIterator const & dit) const
193 TocIterator last = begin();
194 TocIterator it = end();
200 DocIterator dit_text = dit;
201 if (dit_text.inMathed()) {
202 // We are only interested in text so remove the math CursorSlice.
203 while (dit_text.inMathed())
207 for (; it != last; --it) {
208 // We verify that we don't compare contents of two
209 // different document. This happens when you
210 // have parent and child documents.
211 if (&it->dit_[0].inset() != &dit_text[0].inset())
213 if (it->dit_ <= dit_text)
217 // We are before the first Toc Item:
222 Toc::iterator Toc::item(int depth, docstring const & str)
226 iterator it = begin();
227 iterator itend = end();
228 for (; it != itend; ++it) {
229 if (it->depth() == depth && it->str() == str)
236 void TocBackend::writePlaintextTocList(string const & type,
237 odocstringstream & os, size_t max_length) const
239 TocList::const_iterator cit = tocs_.find(type);
240 if (cit != tocs_.end()) {
241 TocIterator ccit = cit->second.begin();
242 TocIterator end = cit->second.end();
243 for (; ccit != end; ++ccit) {
244 os << ccit->asString() << from_utf8("\n");
245 if (os.str().size() > max_length)