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"
46 ///////////////////////////////////////////////////////////////////////////
47 // TocBackend::Item implementation
49 TocBackend::Item::Item(ParConstIterator const & par_it, int d,
51 : par_it_(par_it), depth_(d), str_(s)
57 size_t pos = s.find(" ");
58 if (pos == string::npos) {
64 string s2 = s.substr(0, pos);
66 if (s2 == "Chapter" || s2 == "Part") {
67 size_t pos2 = s.find(" ", pos + 1);
68 if (pos2 == string::npos) {
69 // Unnumbered Chapter?? This should not happen.
70 uid_ = s.substr(pos + 1);
74 uid_ = s.substr(pos2 + 1);
78 uid_ = s.substr(pos + 1);
82 bool const TocBackend::Item::isValid() const
88 int const TocBackend::Item::id() const
94 int const TocBackend::Item::depth() const
100 docstring const & TocBackend::Item::str() const
106 docstring const TocBackend::Item::asString() const
108 return docstring(4 * depth_, ' ') + str_;
112 void TocBackend::Item::goTo(LyXView & lv_) const
114 string const tmp = convert<string>(id());
115 lv_.dispatch(FuncRequest(LFUN_PARAGRAPH_GOTO, tmp));
118 FuncRequest TocBackend::Item::action() const
120 return FuncRequest(LFUN_PARAGRAPH_GOTO, convert<string>(id()));
127 ///////////////////////////////////////////////////////////////////////////
128 // TocBackend implementation
130 TocBackend::Toc const & TocBackend::toc(std::string const & type)
132 // Is the type already supported?
133 TocList::const_iterator it = tocs_.find(type);
134 BOOST_ASSERT(it != tocs_.end());
140 bool TocBackend::addType(std::string const & type)
142 // Is the type already supported?
143 TocList::iterator toclist_it = tocs_.find(type);
144 if (toclist_it != tocs_.end())
147 tocs_.insert(make_pair(type, Toc()));
148 types_.push_back(type);
154 void TocBackend::update()
159 BufferParams const & bufparams = buffer_->params();
160 const int min_toclevel = bufparams.getLyXTextClass().min_toclevel();
162 ParConstIterator pit = buffer_->par_iterator_begin();
163 ParConstIterator end = buffer_->par_iterator_end();
164 for (; pit != end; ++pit) {
166 // the string that goes to the toc (could be the optarg)
169 // For each paragraph, traverse its insets and look for
170 // FLOAT_CODE or WRAP_CODE
171 InsetList::const_iterator it = pit->insetlist.begin();
172 InsetList::const_iterator end = pit->insetlist.end();
173 for (; it != end; ++it) {
174 switch (it->inset->lyxCode()) {
175 case InsetBase::FLOAT_CODE:
176 static_cast<InsetFloat*>(it->inset)
177 ->addToToc(tocs_, *buffer_);
179 case InsetBase::WRAP_CODE:
180 static_cast<InsetWrap*>(it->inset)
181 ->addToToc(tocs_, *buffer_);
183 case InsetBase::OPTARG_CODE: {
184 if (!tocstring.empty())
186 Paragraph const & par = *static_cast<InsetOptArg*>(it->inset)->paragraphs().begin();
187 if (!pit->getLabelstring().empty())
189 tocstring = pit->getLabelstring() + ' ';
190 tocstring += par.asString(*buffer_, false);
198 /// now the toc entry for the paragraph
199 int const toclevel = pit->layout()->toclevel;
200 if (toclevel != LyXLayout::NOT_IN_TOC
201 && toclevel >= min_toclevel
202 && toclevel <= bufparams.tocdepth) {
203 // insert this into the table of contents
204 if (tocstring.empty())
205 tocstring = pit->asString(*buffer_, true);
206 Item const item(pit, toclevel - min_toclevel, tocstring);
207 tocs_["TOC"].push_back(item);
208 //cout << "item inserted str " << item.str()
209 // << " id " << item.id() << endl;
213 TocList::iterator it = tocs_.begin();
214 for (; it != tocs_.end(); ++it)
215 types_.push_back(it->first);
219 TocBackend::TocIterator const TocBackend::item(std::string const & type, ParConstIterator const & par_it)
221 TocList::iterator toclist_it = tocs_.find(type);
222 // Is the type supported?
223 BOOST_ASSERT(toclist_it != tocs_.end());
225 Toc const & toc_vector = toclist_it->second;
226 TocBackend::TocIterator last = toc_vector.begin();
227 TocBackend::TocIterator it = toc_vector.end();
230 for (; it != last; --it) {
232 // A good solution for Items inside insets would be to do:
234 //if (std::distance(it->par_it_, current) <= 0)
237 // But for an unknown reason, std::distance(current, it->par_it_) always
238 // returns a positive value and std::distance(it->par_it_, current) takes forever...
239 // So for now, we do:
240 if (it->par_it_.pit() <= par_it.pit())
244 // We are before the first Toc Item:
249 void TocBackend::asciiTocList(string const & type, odocstream & os) const
251 TocList::const_iterator cit = tocs_.find(type);
252 if (cit != tocs_.end()) {
253 Toc::const_iterator ccit = cit->second.begin();
254 Toc::const_iterator end = cit->second.end();
255 for (; ccit != end; ++ccit)
256 os << ccit->asString() << '\n';