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
9 * \author Guillaume Munch
11 * Full author contact details are available in file CREDITS.
16 #include "TocBackend.h"
19 #include "BufferParams.h"
20 #include "FloatList.h"
21 #include "FuncRequest.h"
22 #include "InsetList.h"
24 #include "LyXAction.h"
25 #include "Paragraph.h"
26 #include "ParIterator.h"
27 #include "TextClass.h"
29 #include "insets/InsetArgument.h"
31 #include "support/convert.h"
32 #include "support/debug.h"
33 #include "support/docstream.h"
34 #include "support/lassert.h"
35 #include "support/lstrings.h"
43 ///////////////////////////////////////////////////////////////////////////
45 // TocItem implementation
47 ///////////////////////////////////////////////////////////////////////////
49 TocItem::TocItem(DocIterator const & dit, int d, docstring const & s,
50 bool output_active, docstring const & t, FuncRequest action) :
51 dit_(dit), depth_(d), str_(s), tooltip_(t), output_(output_active),
57 int TocItem::id() const
59 return dit_.paragraph().id();
63 docstring const & TocItem::tooltip() const
65 return tooltip_.empty() ? str_ : tooltip_;
69 docstring const TocItem::asString() const
71 // U+2327 X IN A RECTANGLE BOX
72 // char_type const cross = 0x2327;
73 // U+274E NEGATIVE SQUARED CROSS MARK
74 char_type const cross = 0x274e;
83 // convert a DocIterator into an argument to LFUN_PARAGRAPH_GOTO
84 docstring paragraph_goto_arg(DocIterator const & dit)
86 CursorSlice const & s = dit.innerTextSlice();
87 return convert<docstring>(s.paragraph().id()) + ' ' +
88 convert<docstring>(s.pos());
91 FuncRequest TocItem::action() const
93 if (action_.action() == LFUN_UNKNOWN_ACTION) {
94 return FuncRequest(LFUN_PARAGRAPH_GOTO, paragraph_goto_arg(dit_));
100 ///////////////////////////////////////////////////////////////////////////
102 // Toc implementation
104 ///////////////////////////////////////////////////////////////////////////
106 TocIterator Toc::item(DocIterator const & dit) const
108 TocIterator last = begin();
109 TocIterator it = end();
115 DocIterator dit_text = dit;
116 if (dit_text.inMathed()) {
117 // We are only interested in text so remove the math CursorSlice.
118 while (dit_text.inMathed())
122 for (; it != last; --it) {
123 // We verify that we don't compare contents of two
124 // different document. This happens when you
125 // have parent and child documents.
126 if (&it->dit_[0].inset() != &dit_text[0].inset())
128 if (it->dit_ <= dit_text)
132 // We are before the first Toc Item:
137 Toc::iterator Toc::item(int depth, docstring const & str)
141 iterator it = begin();
142 iterator itend = end();
143 for (; it != itend; ++it) {
144 if (it->depth() == depth && it->str() == str)
151 ///////////////////////////////////////////////////////////////////////////
153 // TocBuilder implementation
155 ///////////////////////////////////////////////////////////////////////////
157 TocBuilder::TocBuilder(shared_ptr<Toc> toc)
158 : toc_(toc ? toc : lyx::make_shared<Toc>()),
164 void TocBuilder::pushItem(DocIterator const & dit, docstring const & s,
165 bool output_active, bool is_captioned)
167 toc_->push_back(TocItem(dit, stack_.size(), s, output_active));
169 toc_->size() - 1, //pos
170 is_captioned, //is_captioned
175 void TocBuilder::captionItem(DocIterator const & dit, docstring const & s,
178 // first show the float before moving to the caption
179 docstring arg = "paragraph-goto " + paragraph_goto_arg(dit);
181 arg = "paragraph-goto " +
182 paragraph_goto_arg((*toc_)[stack_.top().pos].dit_) + ";" + arg;
183 FuncRequest func(LFUN_COMMAND_SEQUENCE, arg);
185 if (!stack_.empty() && !stack_.top().is_captioned) {
186 // The float we entered has not yet been assigned a caption.
187 // Assign the caption string to it.
188 TocItem & captionable = (*toc_)[stack_.top().pos];
190 captionable.setAction(func);
191 stack_.top().is_captioned = true;
193 // This is a new entry.
195 // the dit is at the float's level, e.g. for the contextual menu of
197 DocIterator captionable_dit = dit;
198 captionable_dit.pop_back();
199 pushItem(captionable_dit, s, output_active, true);
200 (*toc_)[stack_.top().pos].setAction(func);
204 void TocBuilder::pop()
212 ///////////////////////////////////////////////////////////////////////////
214 // TocBuilderStore implementation
216 ///////////////////////////////////////////////////////////////////////////
218 shared_ptr<TocBuilder> TocBuilderStore::get(string const & type,
221 map_t::const_iterator it = map_.find(type);
222 if (it == map_.end()) {
223 it = map_.insert(std::make_pair(type,
224 lyx::make_shared<TocBuilder>(toc))).first;
231 ///////////////////////////////////////////////////////////////////////////
233 // TocBackend implementation
235 ///////////////////////////////////////////////////////////////////////////
237 shared_ptr<Toc const> TocBackend::toc(string const & type) const
239 // Is the type already supported?
240 TocList::const_iterator it = tocs_.find(type);
241 LASSERT(it != tocs_.end(), { return lyx::make_shared<Toc>(); });
246 shared_ptr<Toc> TocBackend::toc(string const & type)
248 TocList::const_iterator it = tocs_.find(type);
249 if (it == tocs_.end()) {
250 it = tocs_.insert(std::make_pair(type, lyx::make_shared<Toc>())).first;
256 shared_ptr<TocBuilder> TocBackend::builder(string const & type)
258 return builders_.get(type, toc(type));
262 // FIXME: This function duplicates functionality from InsetText::iterateForToc.
263 // Both have their own way of computing the TocItem for "tableofcontents". The
264 // TocItem creation and update should be made in a dedicated function and
265 // updateItem should be rewritten to uniformly update the matching items from
267 bool TocBackend::updateItem(DocIterator const & dit)
269 if (dit.text()->getTocLevel(dit.pit()) == Layout::NOT_IN_TOC)
272 if (toc("tableofcontents")->empty()) {
273 // FIXME: should not happen,
274 // a call to TocBackend::update() is missing somewhere
275 LYXERR0("TocBackend::updateItem called but the TOC is empty!");
279 BufferParams const & bufparams = buffer_->params();
280 const int min_toclevel = bufparams.documentClass().min_toclevel();
282 TocIterator toc_item = item("tableofcontents", dit);
286 // For each paragraph, traverse its insets and let them add
289 // FIXME: This is supposed to accomplish the same as the body of
290 // InsetText::iterateForToc(), probably
291 Paragraph & par = toc_item->dit_.paragraph();
292 InsetList::const_iterator it = par.insetList().begin();
293 InsetList::const_iterator end = par.insetList().end();
294 for (; it != end; ++it) {
295 Inset & inset = *it->inset;
296 if (inset.lyxCode() == ARG_CODE) {
297 tocstring = par.labelString();
298 if (!tocstring.empty())
300 inset.asInsetText()->text().forOutliner(tocstring,TOC_ENTRY_LENGTH);
305 int const toclevel = toc_item->dit_.text()->
306 getTocLevel(toc_item->dit_.pit());
307 if (toclevel != Layout::NOT_IN_TOC && toclevel >= min_toclevel
308 && tocstring.empty())
309 par.forOutliner(tocstring, TOC_ENTRY_LENGTH);
311 support::truncateWithEllipsis(tocstring, TOC_ENTRY_LENGTH);
312 const_cast<TocItem &>(*toc_item).str(tocstring);
314 buffer_->updateTocItem("tableofcontents", dit);
319 void TocBackend::update(bool output_active, UpdateType utype)
321 for (TocList::iterator it = tocs_.begin(); it != tocs_.end(); ++it)
325 if (!buffer_->isInternal()) {
327 buffer_->inset().addToToc(dit, output_active, utype);
332 TocIterator TocBackend::item(string const & type,
333 DocIterator const & dit) const
335 TocList::const_iterator toclist_it = tocs_.find(type);
336 // Is the type supported?
337 // We will try to make the best of it in release mode
338 LASSERT(toclist_it != tocs_.end(), toclist_it = tocs_.begin());
339 return toclist_it->second->item(dit);
343 void TocBackend::writePlaintextTocList(string const & type,
344 odocstringstream & os, size_t max_length) const
346 TocList::const_iterator cit = tocs_.find(type);
347 if (cit != tocs_.end()) {
348 TocIterator ccit = cit->second->begin();
349 TocIterator end = cit->second->end();
350 for (; ccit != end; ++ccit) {
351 os << ccit->asString() << from_utf8("\n");
352 if (os.str().size() > max_length)