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.
18 #include "bufferparams.h"
19 #include "FloatList.h"
20 #include "funcrequest.h"
21 #include "LyXAction.h"
22 #include "paragraph.h"
26 #include "frontends/LyXView.h"
28 #include "insets/insetfloat.h"
29 #include "insets/insetoptarg.h"
30 #include "insets/insetwrap.h"
32 #include "support/convert.h"
45 ///////////////////////////////////////////////////////////////////////////
46 // TocBackend::Item implementation
48 TocBackend::Item::Item(ParConstIterator const & par_it, int d,
50 : par_it_(par_it), depth_(d), str_(s)
56 size_t pos = s.find(" ");
57 if (pos == string::npos) {
63 string s2 = s.substr(0, pos);
65 if (s2 == "Chapter" || s2 == "Part") {
66 size_t pos2 = s.find(" ", pos + 1);
67 if (pos2 == string::npos) {
68 // Unnumbered Chapter?? This should not happen.
69 uid_ = s.substr(pos + 1);
73 uid_ = s.substr(pos2 + 1);
77 uid_ = s.substr(pos + 1);
81 bool const TocBackend::Item::isValid() const
87 int const TocBackend::Item::id() const
93 int const TocBackend::Item::depth() const
99 docstring const & TocBackend::Item::str() const
105 docstring const TocBackend::Item::asString() const
107 return docstring(4 * depth_, ' ') + str_;
111 void TocBackend::Item::goTo(LyXView & lv_) const
113 string const tmp = convert<string>(id());
114 lv_.dispatch(FuncRequest(LFUN_PARAGRAPH_GOTO, tmp));
117 FuncRequest TocBackend::Item::action() const
119 return FuncRequest(LFUN_PARAGRAPH_GOTO, convert<string>(id()));
126 ///////////////////////////////////////////////////////////////////////////
127 // TocBackend implementation
129 TocBackend::Toc const & TocBackend::toc(std::string const & type)
131 // Is the type already supported?
132 TocList::const_iterator it = tocs_.find(type);
133 BOOST_ASSERT(it != tocs_.end());
139 bool TocBackend::addType(std::string const & type)
141 // Is the type already supported?
142 TocList::iterator toclist_it = tocs_.find(type);
143 if (toclist_it != tocs_.end())
146 tocs_.insert(make_pair(type, Toc()));
147 types_.push_back(type);
153 void TocBackend::update()
158 BufferParams const & bufparams = buffer_->params();
159 const int min_toclevel = bufparams.getLyXTextClass().min_toclevel();
161 ParConstIterator pit = buffer_->par_iterator_begin();
162 ParConstIterator end = buffer_->par_iterator_end();
163 for (; pit != end; ++pit) {
165 // the string that goes to the toc (could be the optarg)
168 // For each paragraph, traverse its insets and look for
169 // FLOAT_CODE or WRAP_CODE
170 InsetList::const_iterator it = pit->insetlist.begin();
171 InsetList::const_iterator end = pit->insetlist.end();
172 for (; it != end; ++it) {
173 switch (it->inset->lyxCode()) {
174 case InsetBase::FLOAT_CODE:
175 static_cast<InsetFloat*>(it->inset)
176 ->addToToc(tocs_, *buffer_);
178 case InsetBase::WRAP_CODE:
179 static_cast<InsetWrap*>(it->inset)
180 ->addToToc(tocs_, *buffer_);
182 case InsetBase::OPTARG_CODE: {
183 if (!tocstring.empty())
185 Paragraph const & par = *static_cast<InsetOptArg*>(it->inset)->paragraphs().begin();
186 if (!pit->getLabelstring().empty())
188 tocstring = pit->getLabelstring() + ' ';
189 tocstring += par.asString(*buffer_, false);
197 /// now the toc entry for the paragraph
198 int const toclevel = pit->layout()->toclevel;
199 if (toclevel != LyXLayout::NOT_IN_TOC
200 && toclevel >= min_toclevel
201 && toclevel <= bufparams.tocdepth) {
202 // insert this into the table of contents
203 if (tocstring.empty())
204 tocstring = pit->asString(*buffer_, true);
205 Item const item(pit, toclevel - min_toclevel, tocstring);
206 tocs_["TOC"].push_back(item);
210 TocList::iterator it = tocs_.begin();
211 for (; it != tocs_.end(); ++it)
212 types_.push_back(it->first);
216 TocBackend::TocIterator const TocBackend::item(std::string const & type, ParConstIterator const & par_it)
218 TocList::iterator toclist_it = tocs_.find(type);
219 // Is the type supported?
220 BOOST_ASSERT(toclist_it != tocs_.end());
222 Toc const & toc_vector = toclist_it->second;
223 TocBackend::TocIterator last = toc_vector.begin();
224 TocBackend::TocIterator it = toc_vector.end();
227 for (; it != last; --it) {
229 // A good solution for Items inside insets would be to do:
231 //if (std::distance(it->par_it_, current) <= 0)
234 // But for an unknown reason, std::distance(current, it->par_it_) always
235 // returns a positive value and std::distance(it->par_it_, current) takes forever...
236 // So for now, we do:
237 if (it->par_it_.pit() <= par_it.pit())
241 // We are before the first Toc Item:
246 void TocBackend::asciiTocList(string const & type, odocstream & os) const
248 TocList::const_iterator cit = tocs_.find(type);
249 if (cit != tocs_.end()) {
250 Toc::const_iterator ccit = cit->second.begin();
251 Toc::const_iterator end = cit->second.end();
252 for (; ccit != end; ++ccit)
253 os << ccit->asString() << '\n';