* de.po: sync with branch.
[lyx.git] / src / TocBackend.cpp
blob90ba1ed0afd50951d7c55c0c3000329d4d8d1fec
1 /**
2 * \file TocBackend.cpp
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.
13 #include <config.h>
15 #include "TocBackend.h"
17 #include "Buffer.h"
18 #include "BufferParams.h"
19 #include "FloatList.h"
20 #include "FuncRequest.h"
21 #include "InsetList.h"
22 #include "Layout.h"
23 #include "LyXAction.h"
24 #include "Paragraph.h"
25 #include "ParIterator.h"
26 #include "TextClass.h"
28 #include "insets/InsetOptArg.h"
30 #include "support/convert.h"
31 #include "support/debug.h"
32 #include "support/docstream.h"
34 #include "support/lassert.h"
36 using namespace std;
39 namespace lyx {
41 ///////////////////////////////////////////////////////////////////////////
43 // TocItem implementation
45 ///////////////////////////////////////////////////////////////////////////
47 TocItem::TocItem(DocIterator const & dit, int d, docstring const & s)
48 : dit_(dit), depth_(d), str_(s)
53 int TocItem::id() const
55 return dit_.paragraph().id();
59 int TocItem::depth() const
61 return depth_;
65 docstring const & TocItem::str() const
67 return str_;
71 docstring const TocItem::asString() const
73 return docstring(4 * depth_, ' ') + str_;
77 DocIterator const & TocItem::dit() const
79 return dit_;
83 FuncRequest TocItem::action() const
85 string const arg = convert<string>(dit_.paragraph().id())
86 + ' ' + convert<string>(dit_.pos());
87 return FuncRequest(LFUN_PARAGRAPH_GOTO, arg);
91 ///////////////////////////////////////////////////////////////////////////
93 // TocBackend implementation
95 ///////////////////////////////////////////////////////////////////////////
97 Toc const & TocBackend::toc(string const & type) const
99 // Is the type already supported?
100 TocList::const_iterator it = tocs_.find(type);
101 LASSERT(it != tocs_.end(), /**/);
103 return it->second;
107 Toc & TocBackend::toc(string const & type)
109 return tocs_[type];
113 bool TocBackend::updateItem(DocIterator const & dit)
115 if (dit.paragraph().layout().toclevel == Layout::NOT_IN_TOC)
116 return false;
118 if (toc("tableofcontents").empty()) {
119 // FIXME: should not happen,
120 // a call to TocBackend::update() is missing somewhere
121 LYXERR0("TocBackend::updateItem called but the TOC is empty!");
122 return false;
125 BufferParams const & bufparams = buffer_->params();
126 const int min_toclevel = bufparams.documentClass().min_toclevel();
128 TocIterator toc_item = item("tableofcontents", dit);
130 docstring tocstring;
132 // For each paragraph, traverse its insets and let them add
133 // their toc items
134 Paragraph & par = toc_item->dit_.paragraph();
135 InsetList::const_iterator it = par.insetList().begin();
136 InsetList::const_iterator end = par.insetList().end();
137 for (; it != end; ++it) {
138 Inset & inset = *it->inset;
139 if (inset.lyxCode() == OPTARG_CODE) {
140 if (!tocstring.empty())
141 break;
142 Paragraph const & inset_par =
143 *static_cast<InsetOptArg&>(inset).paragraphs().begin();
144 if (!par.labelString().empty())
145 tocstring = par.labelString() + ' ';
146 tocstring += inset_par.asString(AS_STR_INSETS);
147 break;
151 int const toclevel = par.layout().toclevel;
152 if (toclevel != Layout::NOT_IN_TOC && toclevel >= min_toclevel
153 && tocstring.empty())
154 tocstring = par.asString(AS_STR_LABEL | AS_STR_INSETS);
156 const_cast<TocItem &>(*toc_item).str_ = tocstring;
158 buffer_->updateTocItem("tableofcontents", dit);
159 return true;
163 void TocBackend::update()
165 tocs_.clear();
166 if (! buffer_->isInternal()) {
167 DocIterator dit;
168 buffer_->inset().addToToc(dit);
173 TocIterator TocBackend::item(string const & type,
174 DocIterator const & dit) const
176 TocList::const_iterator toclist_it = tocs_.find(type);
177 // Is the type supported?
178 LASSERT(toclist_it != tocs_.end(), /**/);
179 return toclist_it->second.item(dit);
183 TocIterator Toc::item(DocIterator const & dit) const
185 TocIterator last = begin();
186 TocIterator it = end();
187 if (it == last)
188 return it;
190 --it;
192 DocIterator dit_text = dit;
193 if (dit_text.inMathed()) {
194 // We are only interested in text so remove the math CursorSlice.
195 while (dit_text.inMathed())
196 dit_text.pop_back();
199 for (; it != last; --it) {
200 // We verify that we don't compare contents of two
201 // different document. This happens when you
202 // have parent and child documents.
203 if (&it->dit_[0].inset() != &dit_text[0].inset())
204 continue;
205 if (it->dit_ <= dit_text)
206 return it;
209 // We are before the first Toc Item:
210 return last;
214 Toc::iterator Toc::item(int depth, docstring const & str)
216 if (empty())
217 return end();
218 iterator it = begin();
219 iterator itend = end();
220 for (; it != itend; ++it) {
221 if (it->depth() == depth && it->str() == str)
222 break;
224 return it;
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() << from_utf8("\n");
240 } // namespace lyx